6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
476 lines
14 KiB
TypeScript
476 lines
14 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
|
|
import { describe, expect, it } from 'vitest'
|
|
import {
|
|
buildEffectiveChatTranscript,
|
|
getLiveAssistantMessageId,
|
|
isLiveAssistantMessageId,
|
|
} from '@/lib/copilot/chat/effective-transcript'
|
|
import { normalizeMessage } from '@/lib/copilot/chat/persisted-message'
|
|
import {
|
|
MothershipStreamV1CompletionStatus,
|
|
MothershipStreamV1EventType,
|
|
MothershipStreamV1RunKind,
|
|
MothershipStreamV1SessionKind,
|
|
MothershipStreamV1SpanLifecycleEvent,
|
|
MothershipStreamV1SpanPayloadKind,
|
|
MothershipStreamV1TextChannel,
|
|
MothershipStreamV1ToolOutcome,
|
|
} from '@/lib/copilot/generated/mothership-stream-v1'
|
|
import type { StreamBatchEvent } from '@/lib/copilot/request/session/types'
|
|
|
|
function toBatchEvent(eventId: number, event: StreamBatchEvent['event']): StreamBatchEvent {
|
|
return {
|
|
eventId,
|
|
streamId: event.stream.streamId,
|
|
event,
|
|
}
|
|
}
|
|
|
|
function buildUserMessage(id: string, content: string) {
|
|
return normalizeMessage({
|
|
id,
|
|
role: 'user',
|
|
content,
|
|
timestamp: '2026-04-15T12:00:00.000Z',
|
|
})
|
|
}
|
|
|
|
describe('buildEffectiveChatTranscript', () => {
|
|
it('returns the existing transcript when the stream owner is no longer the trailing user', () => {
|
|
const messages = [
|
|
buildUserMessage('stream-1', 'Hello'),
|
|
normalizeMessage({
|
|
id: 'assistant-1',
|
|
role: 'assistant',
|
|
content: 'Persisted response',
|
|
timestamp: '2026-04-15T12:00:01.000Z',
|
|
}),
|
|
]
|
|
|
|
const result = buildEffectiveChatTranscript({
|
|
messages,
|
|
activeStreamId: 'stream-1',
|
|
streamSnapshot: {
|
|
events: [
|
|
toBatchEvent(1, {
|
|
v: 1,
|
|
seq: 1,
|
|
ts: '2026-04-15T12:00:01.000Z',
|
|
type: MothershipStreamV1EventType.text,
|
|
stream: { streamId: 'stream-1' },
|
|
payload: {
|
|
channel: MothershipStreamV1TextChannel.assistant,
|
|
text: 'Live response',
|
|
},
|
|
}),
|
|
],
|
|
previewSessions: [],
|
|
status: 'active',
|
|
},
|
|
})
|
|
|
|
expect(result).toEqual(messages)
|
|
})
|
|
|
|
it('appends a placeholder assistant while an active stream has not produced text yet', () => {
|
|
const result = buildEffectiveChatTranscript({
|
|
messages: [buildUserMessage('stream-1', 'Hello')],
|
|
activeStreamId: 'stream-1',
|
|
streamSnapshot: {
|
|
events: [
|
|
toBatchEvent(1, {
|
|
v: 1,
|
|
seq: 1,
|
|
ts: '2026-04-15T12:00:01.000Z',
|
|
type: MothershipStreamV1EventType.session,
|
|
stream: { streamId: 'stream-1' },
|
|
payload: {
|
|
kind: MothershipStreamV1SessionKind.start,
|
|
},
|
|
}),
|
|
],
|
|
previewSessions: [],
|
|
status: 'active',
|
|
},
|
|
})
|
|
|
|
expect(result).toHaveLength(2)
|
|
expect(result[1]).toEqual(
|
|
expect.objectContaining({
|
|
id: getLiveAssistantMessageId('stream-1'),
|
|
role: 'assistant',
|
|
content: '',
|
|
})
|
|
)
|
|
})
|
|
|
|
it('materializes a live assistant response from redis-backed stream events', () => {
|
|
const result = buildEffectiveChatTranscript({
|
|
messages: [buildUserMessage('stream-1', 'Hello')],
|
|
activeStreamId: 'stream-1',
|
|
streamSnapshot: {
|
|
events: [
|
|
toBatchEvent(1, {
|
|
v: 1,
|
|
seq: 1,
|
|
ts: '2026-04-15T12:00:01.000Z',
|
|
type: MothershipStreamV1EventType.session,
|
|
stream: { streamId: 'stream-1' },
|
|
trace: { requestId: 'req-1' },
|
|
payload: {
|
|
kind: MothershipStreamV1SessionKind.trace,
|
|
requestId: 'req-1',
|
|
},
|
|
}),
|
|
toBatchEvent(2, {
|
|
v: 1,
|
|
seq: 2,
|
|
ts: '2026-04-15T12:00:02.000Z',
|
|
type: MothershipStreamV1EventType.text,
|
|
stream: { streamId: 'stream-1' },
|
|
trace: { requestId: 'req-1' },
|
|
payload: {
|
|
channel: MothershipStreamV1TextChannel.assistant,
|
|
text: 'Live response',
|
|
},
|
|
}),
|
|
],
|
|
previewSessions: [],
|
|
status: 'active',
|
|
},
|
|
})
|
|
|
|
expect(result).toHaveLength(2)
|
|
expect(result[1]).toEqual(
|
|
expect.objectContaining({
|
|
id: getLiveAssistantMessageId('stream-1'),
|
|
role: 'assistant',
|
|
content: 'Live response',
|
|
requestId: 'req-1',
|
|
})
|
|
)
|
|
})
|
|
|
|
it('never surfaces thinking-channel text: a thinking-only stream stays a placeholder', () => {
|
|
const result = buildEffectiveChatTranscript({
|
|
messages: [buildUserMessage('stream-1', 'Hello')],
|
|
activeStreamId: 'stream-1',
|
|
streamSnapshot: {
|
|
events: [
|
|
toBatchEvent(1, {
|
|
v: 1,
|
|
seq: 1,
|
|
ts: '2026-04-15T12:00:01.000Z',
|
|
type: MothershipStreamV1EventType.text,
|
|
stream: { streamId: 'stream-1' },
|
|
payload: {
|
|
channel: MothershipStreamV1TextChannel.thinking,
|
|
text: 'Internal reasoning',
|
|
},
|
|
}),
|
|
],
|
|
previewSessions: [],
|
|
status: 'active',
|
|
},
|
|
})
|
|
|
|
expect(result).toHaveLength(2)
|
|
expect(result[1]).toEqual(
|
|
expect.objectContaining({
|
|
id: getLiveAssistantMessageId('stream-1'),
|
|
role: 'assistant',
|
|
content: '',
|
|
})
|
|
)
|
|
expect(JSON.stringify(result[1])).not.toContain('Internal reasoning')
|
|
})
|
|
|
|
it('keeps assistant text while dropping interleaved thinking chunks', () => {
|
|
const textEvent = (seq: number, channel: MothershipStreamV1TextChannel, text: string) =>
|
|
toBatchEvent(seq, {
|
|
v: 1,
|
|
seq,
|
|
ts: '2026-04-15T12:00:01.000Z',
|
|
type: MothershipStreamV1EventType.text,
|
|
stream: { streamId: 'stream-1' },
|
|
payload: { channel, text },
|
|
})
|
|
const result = buildEffectiveChatTranscript({
|
|
messages: [buildUserMessage('stream-1', 'Hello')],
|
|
activeStreamId: 'stream-1',
|
|
streamSnapshot: {
|
|
events: [
|
|
textEvent(1, MothershipStreamV1TextChannel.thinking, '**Planning**\n\nHidden reasoning.'),
|
|
textEvent(2, MothershipStreamV1TextChannel.assistant, 'Visible answer.'),
|
|
textEvent(3, MothershipStreamV1TextChannel.thinking, 'More hidden reasoning.'),
|
|
],
|
|
previewSessions: [],
|
|
status: 'active',
|
|
},
|
|
})
|
|
|
|
expect(result).toHaveLength(2)
|
|
expect(result[1]).toEqual(
|
|
expect.objectContaining({
|
|
content: 'Visible answer.',
|
|
contentBlocks: [
|
|
expect.objectContaining({
|
|
type: MothershipStreamV1EventType.text,
|
|
content: 'Visible answer.',
|
|
}),
|
|
],
|
|
})
|
|
)
|
|
expect(JSON.stringify(result[1])).not.toContain('reasoning')
|
|
})
|
|
|
|
it('treats user-cancelled tool results as cancelled', () => {
|
|
const result = buildEffectiveChatTranscript({
|
|
messages: [buildUserMessage('stream-1', 'Hello')],
|
|
activeStreamId: 'stream-1',
|
|
streamSnapshot: {
|
|
events: [
|
|
toBatchEvent(1, {
|
|
v: 1,
|
|
seq: 1,
|
|
ts: '2026-04-15T12:00:01.000Z',
|
|
type: MothershipStreamV1EventType.tool,
|
|
stream: { streamId: 'stream-1' },
|
|
payload: {
|
|
phase: 'result',
|
|
toolCallId: 'tool-1',
|
|
toolName: 'workspace_file',
|
|
executor: 'go',
|
|
mode: 'sync',
|
|
success: false,
|
|
output: {
|
|
reason: 'user_cancelled',
|
|
},
|
|
},
|
|
}),
|
|
],
|
|
previewSessions: [],
|
|
status: 'active',
|
|
},
|
|
})
|
|
|
|
expect(result[1]?.contentBlocks).toEqual([
|
|
expect.objectContaining({
|
|
type: MothershipStreamV1EventType.tool,
|
|
toolCall: expect.objectContaining({
|
|
id: 'tool-1',
|
|
name: 'workspace_file',
|
|
state: MothershipStreamV1CompletionStatus.cancelled,
|
|
}),
|
|
}),
|
|
])
|
|
})
|
|
|
|
it('pairs a scoped compaction inside the owning subagent during stream replay', () => {
|
|
const scope = {
|
|
lane: 'subagent' as const,
|
|
parentToolCallId: 'tc-workflow',
|
|
spanId: 'span-workflow',
|
|
parentSpanId: 'span-superagent',
|
|
agentId: 'superagent',
|
|
}
|
|
const stream = { streamId: 'stream-1' }
|
|
const result = buildEffectiveChatTranscript({
|
|
messages: [buildUserMessage('stream-1', 'Hello')],
|
|
activeStreamId: 'stream-1',
|
|
streamSnapshot: {
|
|
events: [
|
|
toBatchEvent(1, {
|
|
v: 1,
|
|
seq: 1,
|
|
ts: '2026-04-15T12:00:01.000Z',
|
|
type: MothershipStreamV1EventType.span,
|
|
stream,
|
|
scope,
|
|
payload: {
|
|
kind: MothershipStreamV1SpanPayloadKind.subagent,
|
|
event: MothershipStreamV1SpanLifecycleEvent.start,
|
|
agent: 'workflow',
|
|
data: { tool_call_id: 'tc-workflow' },
|
|
},
|
|
}),
|
|
toBatchEvent(2, {
|
|
v: 1,
|
|
seq: 2,
|
|
ts: '2026-04-15T12:00:02.000Z',
|
|
type: MothershipStreamV1EventType.run,
|
|
stream,
|
|
scope,
|
|
payload: { kind: MothershipStreamV1RunKind.compaction_start },
|
|
}),
|
|
toBatchEvent(3, {
|
|
v: 1,
|
|
seq: 3,
|
|
ts: '2026-04-15T12:00:03.000Z',
|
|
type: MothershipStreamV1EventType.run,
|
|
stream,
|
|
scope,
|
|
payload: {
|
|
kind: MothershipStreamV1RunKind.compaction_done,
|
|
data: { summary_chars: 42 },
|
|
},
|
|
}),
|
|
],
|
|
previewSessions: [],
|
|
status: 'active',
|
|
},
|
|
})
|
|
|
|
const compactions = result[1]?.contentBlocks?.filter(
|
|
(block) => block.type === MothershipStreamV1EventType.tool
|
|
)
|
|
expect(compactions).toHaveLength(1)
|
|
expect(compactions?.[0]).toEqual(
|
|
expect.objectContaining({
|
|
parentToolCallId: 'tc-workflow',
|
|
spanId: 'span-workflow',
|
|
parentSpanId: 'span-superagent',
|
|
toolCall: expect.objectContaining({
|
|
id: 'compaction_2',
|
|
name: 'context_compaction',
|
|
calledBy: 'workflow',
|
|
display: { title: 'Summarizing context' },
|
|
state: MothershipStreamV1ToolOutcome.success,
|
|
}),
|
|
})
|
|
)
|
|
})
|
|
|
|
it('materializes a cancelled assistant tail when the stream ends before persistence', () => {
|
|
const result = buildEffectiveChatTranscript({
|
|
messages: [buildUserMessage('stream-1', 'Hello')],
|
|
activeStreamId: 'stream-1',
|
|
streamSnapshot: {
|
|
events: [
|
|
toBatchEvent(1, {
|
|
v: 1,
|
|
seq: 1,
|
|
ts: '2026-04-15T12:00:01.000Z',
|
|
type: MothershipStreamV1EventType.complete,
|
|
stream: { streamId: 'stream-1' },
|
|
payload: {
|
|
status: MothershipStreamV1CompletionStatus.cancelled,
|
|
},
|
|
}),
|
|
],
|
|
previewSessions: [],
|
|
status: MothershipStreamV1CompletionStatus.cancelled,
|
|
},
|
|
})
|
|
|
|
expect(result).toHaveLength(2)
|
|
expect(result[1]?.contentBlocks).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({
|
|
type: MothershipStreamV1EventType.complete,
|
|
status: MothershipStreamV1CompletionStatus.cancelled,
|
|
}),
|
|
])
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('isLiveAssistantMessageId', () => {
|
|
it('recognizes the synthetic live-assistant id and nothing else', () => {
|
|
expect(isLiveAssistantMessageId(getLiveAssistantMessageId('stream-1'))).toBe(true)
|
|
expect(isLiveAssistantMessageId('f620fceb-4e9d-4e7f-ab7f-890a2a823564')).toBe(false)
|
|
expect(isLiveAssistantMessageId('')).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('tool ownership is call-frame authoritative', () => {
|
|
const toolEvent = (
|
|
seq: number,
|
|
phase: 'call' | 'result',
|
|
toolCallId: string,
|
|
scope?: Record<string, unknown>
|
|
): StreamBatchEvent =>
|
|
toBatchEvent(seq, {
|
|
v: 1,
|
|
seq,
|
|
ts: '2026-04-15T12:00:01.000Z',
|
|
type: MothershipStreamV1EventType.tool,
|
|
stream: { streamId: 'stream-1' },
|
|
payload:
|
|
phase === 'call'
|
|
? { phase: 'call', toolCallId, toolName: 'read', arguments: { path: 'a.md' } }
|
|
: { phase: 'result', toolCallId, toolName: 'read', success: true, output: 'ok' },
|
|
...(scope ? { scope } : {}),
|
|
// double-cast-allowed: synthetic test envelope; the reducer reads only the fields set here
|
|
} as unknown as StreamBatchEvent['event'])
|
|
|
|
const superagentScope = {
|
|
lane: 'subagent',
|
|
agentId: 'superagent',
|
|
parentToolCallId: 'dispatch-1',
|
|
spanId: 'S1',
|
|
}
|
|
|
|
const ownership = (result: ReturnType<typeof buildEffectiveChatTranscript>) => {
|
|
const blocks = (result[1].contentBlocks ?? []) as Array<Record<string, unknown>>
|
|
const tool = blocks.find((b) => b.type === MothershipStreamV1EventType.tool)
|
|
const tc = tool?.toolCall as Record<string, unknown> | undefined
|
|
return {
|
|
calledBy: tc?.calledBy,
|
|
parentToolCallId: tool?.parentToolCallId,
|
|
spanId: tool?.spanId,
|
|
}
|
|
}
|
|
|
|
it('an unscoped main call clears provisional subagent attribution', () => {
|
|
// The observed dev bug: a mis-scoped replayed result seeded the main
|
|
// read under Superagent, and nothing could ever move it back.
|
|
const result = buildEffectiveChatTranscript({
|
|
messages: [buildUserMessage('stream-1', 'Hello')],
|
|
activeStreamId: 'stream-1',
|
|
streamSnapshot: {
|
|
events: [toolEvent(1, 'result', 'fc_1', superagentScope), toolEvent(2, 'call', 'fc_1')],
|
|
previewSessions: [],
|
|
status: 'active',
|
|
},
|
|
})
|
|
const own = ownership(result)
|
|
expect(own.calledBy).toBeUndefined()
|
|
expect(own.parentToolCallId).toBeUndefined()
|
|
expect(own.spanId).toBeUndefined()
|
|
})
|
|
|
|
it('a later mis-scoped result cannot re-parent a settled main tool', () => {
|
|
const result = buildEffectiveChatTranscript({
|
|
messages: [buildUserMessage('stream-1', 'Hello')],
|
|
activeStreamId: 'stream-1',
|
|
streamSnapshot: {
|
|
events: [toolEvent(1, 'call', 'fc_1'), toolEvent(2, 'result', 'fc_1', superagentScope)],
|
|
previewSessions: [],
|
|
status: 'active',
|
|
},
|
|
})
|
|
const own = ownership(result)
|
|
expect(own.calledBy).toBeUndefined()
|
|
expect(own.parentToolCallId).toBeUndefined()
|
|
})
|
|
|
|
it('a genuinely scoped subagent call keeps its ownership', () => {
|
|
const result = buildEffectiveChatTranscript({
|
|
messages: [buildUserMessage('stream-1', 'Hello')],
|
|
activeStreamId: 'stream-1',
|
|
streamSnapshot: {
|
|
events: [toolEvent(1, 'call', 'fc_2', superagentScope), toolEvent(2, 'result', 'fc_2')],
|
|
previewSessions: [],
|
|
status: 'active',
|
|
},
|
|
})
|
|
const own = ownership(result)
|
|
expect(own.calledBy).toBe('superagent')
|
|
expect(own.parentToolCallId).toBe('dispatch-1')
|
|
})
|
|
})
|