侧边对话的四处用户痛点,加上「实时 fork」与「隐藏会话」两条路线的可行性结论。
全部结论都有真机复现取证(在运行中的宿主上建线程 + 读真实 session 日志)。
- 排队信息漏入:create 的 `inbox.clear()` 围栏确实生效(真机复现:子会话
request 只有 [boundary, 问题]);但幽灵来源比文档更宽——父会话 mid-turn 且
工具未返回时,种子被截断在 `turn/start` 之前,而 inbox 的「领取」splice 发生
在其后,于是**父会话本轮已领取的输入**在种子折叠里仍是 pending;而冷恢复
路径完全没有围栏。修法:create 保留硬围栏(本线程自己不可能有排队输入),
resume 改为 `inheritedPendingIds` 只折继承前缀得到幽灵 id → `inbox.remove(id)`
精确清扫,绝不误杀用户自己排的队。
- 消息不及时:新增 `/sidebar/ws/sidechat` 推送(`session/event` 60ms、
`agent/assistant-stream` 250ms 去抖,**纯失效帧**,客户端带 afterSeq 重拉),
2s 轮询降级为 15s 兜底,断线自愈。
- 模型选不了:宿主对 `origin:'subagent'` 会话**显式禁用**自带选择器,
`sessionController.selectModel` 又被 subagent fence 挡;插件因此自建
`sidechat.models`(直通宿主模型目录)与 `sidechat.model`(append
`model/selection` + `installModelSelection`),并让 `sidechat.info` 报**有效**
选择而不是冻结的创建期 options。
- 排队可见 / 回显 / spinner / 错误行:`sidechat.events` 增返 `pending`(claim
顺序)、`sidechat.dequeue` 按 id 撤销或清空、发送即本地回显、立即 thinking
行、`turn/end{error}` 与失败 attempt 渲染成可见错误行;另修掉「首问未 claim
时连发第二条会注入第二对 boundary+question」(新增 `boundaryPending`)。
- 底栏重构(用户反馈「注意力全在底栏,看不到上栏的保存为新会话」):顶栏整条
删除,`[+新建][线程][保存][归档] … [模型选择器][发送/停止]` 与主会话底栏对齐
(图标靠左、选择器靠右完整显示,agent 徽标不再显示);推理强度改为**平铺行**
——宿主 `Menu` 的 submenu 只能开在父项右侧(无翻转开关)且会让整个面板失去
滚动,钉在侧栏右缘必然被裁;每个控件加宿主 `Tooltip` 悬停气泡(锚在启用的
span 上,禁用态才解释得了「为什么不能点」),替代原生 title。
设计文档:docs/plans/2026-10-12-sidechat-native-surface.md(含取证、判定、
实施偏差与未验证项);AGENTS §3.4 第 4/10 条同步。
门禁:`tsc --noEmit` / `eslint .` / `vitest run`(2268 passed / 17 skipped)全绿;
宿主半 build + pack 后真实挂载进 sidechat-test profile 并验证新路由与 WS 通道。
607 lines
26 KiB
TypeScript
607 lines
26 KiB
TypeScript
/**
|
|
* Unit tests for the pure Side Chat core (src/sidechat-core.ts): the seed
|
|
* construction (full-log inheritance with honest synthetic closing of an
|
|
* in-progress parent turn), the structured in-progress snapshot (with the
|
|
* dangling-tool-call fallback), thread-row derivation, save eligibility, and
|
|
* the preset resolution.
|
|
*/
|
|
import { describe, expect, it } from 'vitest'
|
|
import { createToolResultMessage, ToolCallId } from '@deepseek-ai/dsh-llm'
|
|
import type { SidebarHistoryEntry, SidebarSessionEvent, SidebarSessionSummary } from '../src/context-types.ts'
|
|
import type { AssistantLiveChunk } from '../src/assistant-live.ts'
|
|
import {
|
|
boundaryDelivered,
|
|
buildOpenTurnSnapshot,
|
|
buildSidechatInheritance,
|
|
effectiveModelSelection,
|
|
hasDanglingToolCall,
|
|
inheritedPendingIds,
|
|
isContextInjectionMessage,
|
|
parentModelSelection,
|
|
pendingIdsInLog,
|
|
pendingInputs,
|
|
resolvePresetId,
|
|
sideLabel,
|
|
sideThreadRows,
|
|
threadHasCompletedTurn,
|
|
threadTrailingPending,
|
|
SIDE_BOUNDARY_PROMPT,
|
|
SIDE_INJECTION_SOURCE_KIND,
|
|
SIDE_LABEL_PREFIX,
|
|
type SeedEvent,
|
|
} from '../src/sidechat-core.ts'
|
|
|
|
/** One 0.1.6-era tool/result message: a user-role message whose single
|
|
* `tool-result` content block nests the result blocks and carries `isError`. */
|
|
function legacyToolResultMessage(callId: string, text: string, isError = false): Record<string, unknown> {
|
|
return {
|
|
role: 'user',
|
|
source: { kind: 'tool', callId },
|
|
content: [{ type: 'tool-result', toolCallId: callId, isError, content: [{ type: 'text', text }] }],
|
|
}
|
|
}
|
|
|
|
/** One 0.1.7 tool/result message: a first-class tool-role message whose
|
|
* `content` holds the result blocks and whose `isError` lives on the message. */
|
|
function toolRoleResultMessage(callId: string, text: string, isError = false): Record<string, unknown> {
|
|
return {
|
|
role: 'tool',
|
|
source: { kind: 'tool', callId },
|
|
toolCallId: callId,
|
|
content: [{ type: 'text', text }],
|
|
isError,
|
|
}
|
|
}
|
|
|
|
/** One log event fixture (structural, seq === index like the live contract).
|
|
* Surface-eligible events carry their required `surfaceOp: 'append'`
|
|
* marker, exactly like the live pipeline appends them. */
|
|
function ev(type: string, seq: number, data: Record<string, unknown> = {}): SidebarSessionEvent {
|
|
const event: SidebarSessionEvent = { type, seq, time: seq * 1000, data }
|
|
if (type === 'user/message' || type === 'assistant/message' || type === 'tool/result') {
|
|
return { ...event, surfaceOp: 'append' } as SidebarSessionEvent
|
|
}
|
|
return event
|
|
}
|
|
|
|
/** One live delta as the 0.1.5 stream buffer produces it: `index` is the
|
|
* dense frame position, the chunk carries its own block index. */
|
|
function live(
|
|
turn: number,
|
|
step: number,
|
|
text: string,
|
|
over: { kind?: 'text' | 'reasoning'; index?: number; block?: number } = {},
|
|
): AssistantLiveChunk {
|
|
return {
|
|
attemptId: 'a1',
|
|
turn,
|
|
step,
|
|
index: over.index ?? 0,
|
|
time: 0,
|
|
chunk: { type: over.kind === 'reasoning' ? 'reasoning-delta' : 'text-delta', index: over.block ?? 0, text },
|
|
}
|
|
}
|
|
|
|
/** A completed user→assistant turn block (turn T, step 1, optional tools). */
|
|
function completedTurn(seq: number, turn: number, over: {
|
|
text?: string
|
|
tools?: Array<{ callId: string; name?: string; args?: string; result?: string }>
|
|
} = {}): SidebarSessionEvent[] {
|
|
const events: SidebarSessionEvent[] = [
|
|
ev('user/message', seq, { content: [{ type: 'text', text: `q${turn}` }], source: { kind: 'user' } }),
|
|
ev('turn/start', seq + 1, { turn }),
|
|
ev('step/start', seq + 2, { turn, step: 1 }),
|
|
]
|
|
let next = seq + 3
|
|
for (const tool of over.tools ?? []) {
|
|
events.push(ev('tool/call', next++, { turn, step: 1, callId: tool.callId, name: tool.name ?? 'bash', arguments: tool.args ?? '{}' }))
|
|
events.push(ev('tool/result', next++, {
|
|
turn,
|
|
step: 1,
|
|
message: toolRoleResultMessage(tool.callId, tool.result ?? 'ok'),
|
|
}))
|
|
}
|
|
if (over.text !== undefined) {
|
|
events.push(ev('assistant/message', next++, {
|
|
turn,
|
|
step: 1,
|
|
message: { content: [{ type: 'text', text: over.text }] },
|
|
}))
|
|
}
|
|
events.push(ev('step/end', next++, { turn, step: 1 }))
|
|
events.push(ev('turn/end', next, { turn, reason: { kind: 'completed' } }))
|
|
return events
|
|
}
|
|
|
|
/** Assert the seed is contiguous from seq 0. */
|
|
function expectContiguous(seed: readonly SeedEvent[]): void {
|
|
seed.forEach((event, index) => expect(event.seq).toBe(index))
|
|
}
|
|
|
|
describe('buildSidechatInheritance', () => {
|
|
it('returns an empty seed for an empty log', () => {
|
|
const { seed, snapshot } = buildSidechatInheritance([])
|
|
expect(seed).toEqual([])
|
|
expect(snapshot).toBeNull()
|
|
})
|
|
|
|
it('copies a log that ends outside any turn verbatim (pending user message)', () => {
|
|
const events = [
|
|
...completedTurn(0, 1, { text: 'a' }),
|
|
ev('user/message', 6, { content: [{ type: 'text', text: 'pending?' }], source: { kind: 'user' } }),
|
|
]
|
|
const { seed, snapshot } = buildSidechatInheritance(events)
|
|
expectContiguous(seed)
|
|
expect(seed).toHaveLength(events.length)
|
|
expect(seed.at(-1)?.type).toBe('user/message')
|
|
expect(seed.some(event => event.type === 'turn/end')).toBe(true)
|
|
expect(snapshot).toBeNull()
|
|
})
|
|
|
|
it('preserves the surface envelope of message events in the seed', () => {
|
|
// Regression: the seed validator rejects surface-eligible events
|
|
// (user/message, assistant/message, tool/result) whose `surfaceOp`
|
|
// marker was stripped — the copy must keep the FULL event envelope.
|
|
const events = [
|
|
ev('user/message', 0, { content: [{ type: 'text', text: 'q' }], source: { kind: 'user' } }),
|
|
ev('turn/start', 1, { turn: 1 }),
|
|
ev('step/start', 2, { turn: 1, step: 1 }),
|
|
ev('tool/call', 3, { turn: 1, step: 1, callId: 'c1', name: 'read', arguments: '{}' }),
|
|
ev('tool/result', 4, {
|
|
turn: 1,
|
|
step: 1,
|
|
message: toolRoleResultMessage('c1', 'ok'),
|
|
}),
|
|
ev('assistant/message', 5, { turn: 1, step: 1, message: { content: [{ type: 'text', text: 'a' }] } }),
|
|
ev('step/end', 6, { turn: 1, step: 1 }),
|
|
ev('turn/end', 7, { turn: 1, reason: { kind: 'completed' } }),
|
|
]
|
|
const { seed } = buildSidechatInheritance(events)
|
|
expect(seed.filter(event => event.type === 'user/message').every(event => event.surfaceOp === 'append')).toBe(true)
|
|
expect(seed.filter(event => event.type === 'assistant/message').every(event => event.surfaceOp === 'append')).toBe(true)
|
|
expect(seed.filter(event => event.type === 'tool/result').every(event => event.surfaceOp === 'append')).toBe(true)
|
|
// Non-surface events never gain a marker.
|
|
expect(seed.filter(event => event.type === 'turn/end').every(event => event.surfaceOp === undefined)).toBe(true)
|
|
})
|
|
|
|
it('copies a log with no turns at all', () => {
|
|
const events = [ev('user/message', 0, { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } })]
|
|
const { seed } = buildSidechatInheritance(events)
|
|
expectContiguous(seed)
|
|
expect(seed).toHaveLength(1)
|
|
})
|
|
|
|
it('closes an open streaming turn with synthetic step/end + interrupted turn/end', () => {
|
|
const events = [
|
|
...completedTurn(0, 1, { text: 'done' }),
|
|
ev('user/message', 6, { content: [{ type: 'text', text: 'next?' }], source: { kind: 'user' } }),
|
|
ev('turn/start', 7, { turn: 2 }),
|
|
ev('step/start', 8, { turn: 2, step: 1 }),
|
|
]
|
|
const { seed, snapshot } = buildSidechatInheritance(events)
|
|
expectContiguous(seed)
|
|
expect(seed).toHaveLength(events.length + 2)
|
|
const stepEnd = seed.at(-2)!
|
|
const turnEnd = seed.at(-1)!
|
|
expect(stepEnd).toMatchObject({ type: 'step/end', data: { turn: 2, step: 1 } })
|
|
expect(turnEnd).toMatchObject({ type: 'turn/end', data: { turn: 2, reason: { kind: 'interrupted' } } })
|
|
// The pending user message survives as a REAL event. The in-flight
|
|
// step's deltas are process-local on 0.1.5, so they are NOT seed events —
|
|
// the snapshot fallback is the only path that carries them.
|
|
expect(seed.some(event => event.type === 'user/message'
|
|
&& (event.data.content as Array<{ text: string }>)[0]?.text === 'next?')).toBe(true)
|
|
expect(seed.every(event => event.type !== 'assistant/live-chunk')).toBe(true)
|
|
expect(snapshot).toBeNull()
|
|
})
|
|
|
|
it('closes an open turn whose tool calls already have results', () => {
|
|
const events = [
|
|
ev('user/message', 0, { content: [{ type: 'text', text: 'q' }], source: { kind: 'user' } }),
|
|
ev('turn/start', 1, { turn: 1 }),
|
|
ev('step/start', 2, { turn: 1, step: 1 }),
|
|
ev('tool/call', 3, { turn: 1, step: 1, callId: 'c1', name: 'grep', arguments: '{"q":"x"}' }),
|
|
ev('tool/result', 4, {
|
|
turn: 1,
|
|
step: 1,
|
|
message: toolRoleResultMessage('c1', 'hit'),
|
|
}),
|
|
ev('assistant/message', 5, { turn: 1, step: 1, message: { content: [{ type: 'text', text: 'found it' }] }, stream: [] }),
|
|
]
|
|
const { seed, snapshot } = buildSidechatInheritance(events)
|
|
expectContiguous(seed)
|
|
expect(seed.at(-1)?.type).toBe('turn/end')
|
|
expect(hasDanglingToolCall(events, 1)).toBe(false)
|
|
expect(snapshot).toBeNull()
|
|
})
|
|
|
|
it('falls back to a snapshot cut when a tool call is still executing', () => {
|
|
const events = [
|
|
ev('user/message', 0, { content: [{ type: 'text', text: 'q' }], source: { kind: 'user' } }),
|
|
ev('turn/start', 1, { turn: 1 }),
|
|
ev('step/start', 2, { turn: 1, step: 1 }),
|
|
ev('tool/call', 4, { turn: 1, step: 1, callId: 'c1', name: 'bash', arguments: '{"cmd":"sleep 9"}' }),
|
|
]
|
|
expect(hasDanglingToolCall(events, 1)).toBe(true)
|
|
// The in-flight text is process-local on 0.1.5: it reaches the snapshot
|
|
// through the live buffer, not through the log.
|
|
const { seed, snapshot } = buildSidechatInheritance(events, [live(1, 1, 'running ')])
|
|
expectContiguous(seed)
|
|
// The seed stops BEFORE the open turn; the pending user message stays.
|
|
expect(seed.at(-1)?.type).toBe('user/message')
|
|
expect(seed.some(event => event.type === 'turn/start')).toBe(false)
|
|
expect(snapshot).not.toBeNull()
|
|
expect(snapshot).toContain('running ')
|
|
expect(snapshot).toContain('`bash` (executing)')
|
|
expect(snapshot).toContain('sleep 9')
|
|
})
|
|
|
|
it('closes only the current step when the open turn has completed steps', () => {
|
|
const events = [
|
|
ev('user/message', 0, { content: [{ type: 'text', text: 'q' }], source: { kind: 'user' } }),
|
|
ev('turn/start', 1, { turn: 1 }),
|
|
ev('step/start', 2, { turn: 1, step: 1 }),
|
|
ev('assistant/message', 3, { turn: 1, step: 1, message: { content: [{ type: 'text', text: 'first' }] }, stream: [] }),
|
|
ev('step/end', 4, { turn: 1, step: 1 }),
|
|
ev('step/start', 5, { turn: 1, step: 2 }),
|
|
]
|
|
const { seed } = buildSidechatInheritance(events)
|
|
expectContiguous(seed)
|
|
expect(seed.at(-2)).toMatchObject({ type: 'step/end', data: { turn: 1, step: 2 } })
|
|
expect(seed.at(-1)).toMatchObject({ type: 'turn/end', data: { turn: 1 } })
|
|
})
|
|
})
|
|
|
|
describe('buildOpenTurnSnapshot', () => {
|
|
it('returns null without an open turn', () => {
|
|
expect(buildOpenTurnSnapshot([ev('user/message', 0, { content: [], source: { kind: 'user' } })])).toBeNull()
|
|
expect(buildOpenTurnSnapshot(completedTurn(0, 1, { text: 'x' }))).toBeNull()
|
|
})
|
|
|
|
it('preserves streamed text and tool detail verbatim', () => {
|
|
const events = [
|
|
ev('turn/start', 0, { turn: 1 }),
|
|
ev('step/start', 1, { turn: 1, step: 1 }),
|
|
ev('tool/call', 5, { turn: 1, step: 1, callId: 'c1', name: 'read', arguments: '{"path":"a.txt"}' }),
|
|
// The shape 0.1.7's createToolResultMessage writes: a first-class
|
|
// tool-role message with the result blocks at the top level.
|
|
ev('tool/result', 6, {
|
|
turn: 1,
|
|
step: 1,
|
|
message: toolRoleResultMessage('c1', 'file body'),
|
|
}),
|
|
ev('tool/call', 7, { turn: 1, step: 1, callId: 'c2', name: 'bash', arguments: '{"cmd":"long"}' }),
|
|
]
|
|
const snapshot = buildOpenTurnSnapshot(events, [
|
|
live(1, 1, 'think', { kind: 'reasoning', index: 0, block: 0 }),
|
|
live(1, 1, '```js\ncode', { index: 1, block: 1 }),
|
|
live(1, 1, '\n```', { index: 2, block: 1 }),
|
|
])
|
|
expect(snapshot).not.toBeNull()
|
|
expect(snapshot).toContain('```js\ncode\n```')
|
|
expect(snapshot).toContain('Reasoning so far')
|
|
expect(snapshot).toContain('`read` — arguments: `{"path":"a.txt"}`')
|
|
expect(snapshot).toContain('Result: file body')
|
|
expect(snapshot).toContain('`bash` (executing)')
|
|
})
|
|
|
|
it('reads the tool/result message dsh-llm actually produces (producer round-trip)', () => {
|
|
// createToolResultMessage is the producer of every live tool/result row:
|
|
// the fixture shapes above must match what it emits, not a guess.
|
|
const message = createToolResultMessage({
|
|
callId: ToolCallId('c1'),
|
|
content: [{ type: 'text', text: 'produced body' }],
|
|
isError: false,
|
|
})
|
|
const events = [
|
|
ev('turn/start', 0, { turn: 1 }),
|
|
ev('step/start', 1, { turn: 1, step: 1 }),
|
|
ev('tool/call', 2, { turn: 1, step: 1, callId: 'c1', name: 'read', arguments: '{"path":"a"}' }),
|
|
ev('tool/result', 3, { turn: 1, step: 1, message: message as unknown as Record<string, unknown> }),
|
|
]
|
|
const snapshot = buildOpenTurnSnapshot(events)
|
|
expect(snapshot).not.toBeNull()
|
|
expect(snapshot).toContain('Result: produced body')
|
|
})
|
|
|
|
it('reads a LEGACY 0.1.6-wrapped tool result (historical logs keep that shape)', () => {
|
|
const events = [
|
|
ev('turn/start', 0, { turn: 1 }),
|
|
ev('step/start', 1, { turn: 1, step: 1 }),
|
|
ev('tool/call', 5, { turn: 1, step: 1, callId: 'c1', name: 'read', arguments: '{"path":"old.txt"}' }),
|
|
ev('tool/result', 6, {
|
|
turn: 1,
|
|
step: 1,
|
|
message: legacyToolResultMessage('c1', 'legacy body'),
|
|
}),
|
|
ev('tool/call', 7, { turn: 1, step: 1, callId: 'c2', name: 'bash', arguments: '{"cmd":"long"}' }),
|
|
]
|
|
const snapshot = buildOpenTurnSnapshot(events)
|
|
expect(snapshot).not.toBeNull()
|
|
expect(snapshot).toContain('Result: legacy body')
|
|
})
|
|
|
|
// Logs written by other producers (or by an older plugin) can carry rows the
|
|
// current shape does not describe: a non-array `content`, a message without
|
|
// one, a block that is not an object, a text block without a string. The
|
|
// guards in messageTexts/resultBlocks must skip them rather than throw — a
|
|
// single malformed row must not blank the whole open-turn snapshot.
|
|
it('skips malformed assistant content instead of throwing', () => {
|
|
const events = [
|
|
ev('turn/start', 0, { turn: 1 }),
|
|
ev('step/start', 1, { turn: 1, step: 1 }),
|
|
ev('assistant/message', 2, { turn: 1, step: 1, message: { content: 'not-an-array' } }),
|
|
ev('assistant/message', 3, { turn: 1, step: 1 }),
|
|
ev('assistant/message', 4, {
|
|
turn: 1,
|
|
step: 1,
|
|
message: { content: [null, { type: 'text' }, { type: 'text', text: 'kept' }] },
|
|
}),
|
|
ev('tool/call', 5, { turn: 1, step: 1, callId: 'c1', name: 'bash', arguments: '{}' }),
|
|
]
|
|
expect(buildOpenTurnSnapshot(events)).toContain('kept')
|
|
})
|
|
})
|
|
|
|
describe('sideLabel', () => {
|
|
it('prefixes and truncates', () => {
|
|
expect(sideLabel('hello')).toBe('Side: hello')
|
|
expect(sideLabel(' a b ')).toBe('Side: a b')
|
|
const long = 'x'.repeat(100)
|
|
const label = sideLabel(long)
|
|
expect(label.startsWith(SIDE_LABEL_PREFIX)).toBe(true)
|
|
expect(label.length).toBeLessThanOrEqual(48)
|
|
expect(label.endsWith('…')).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('sideThreadRows', () => {
|
|
const summary = (over: Partial<SidebarSessionSummary>): SidebarSessionSummary => ({
|
|
id: 's1',
|
|
displayTitle: 'x',
|
|
...over,
|
|
})
|
|
|
|
it('keeps only subagent-origin children of the session with the label prefix', () => {
|
|
const byId: Record<string, SidebarSessionSummary> = {
|
|
main: summary({ id: 'main', displayTitle: 'Main' }),
|
|
thread: summary({ id: 'thread', origin: 'subagent', parentId: 'main', displayTitle: 'Side: q', running: true }),
|
|
plainSubagent: summary({ id: 'plain', origin: 'subagent', parentId: 'main', displayTitle: 'Read files' }),
|
|
foreignThread: summary({ id: 'foreign', origin: 'subagent', parentId: 'other', displayTitle: 'Side: other' }),
|
|
}
|
|
const rows = sideThreadRows(byId, 'main')
|
|
expect(rows).toEqual([{ id: 'thread', title: 'Side: q', running: true }])
|
|
})
|
|
})
|
|
|
|
describe('save eligibility', () => {
|
|
const entry = (event: SidebarSessionEvent): SidebarHistoryEntry => ({ event })
|
|
|
|
it('requires a completed turn after the seed boundary', () => {
|
|
const seed = [ev('session/end-seed', 0)]
|
|
expect(threadHasCompletedTurn([entry(seed[0]!)])).toBe(false)
|
|
expect(threadHasCompletedTurn([entry(seed[0]!), entry(ev('user/message', 1, { content: [], source: { kind: 'user' } }))])).toBe(false)
|
|
const withTurn = [...completedTurn(2, 1, { text: 'a' })]
|
|
const entries = [entry(seed[0]!), ...withTurn.map(entry)]
|
|
expect(threadHasCompletedTurn(entries)).toBe(true)
|
|
})
|
|
|
|
it('detects a trailing unanswered follow-up', () => {
|
|
const entries = [
|
|
...completedTurn(2, 1, { text: 'a' }).map(entry),
|
|
entry(ev('user/message', 9, { content: [], source: { kind: 'user' } })),
|
|
]
|
|
expect(threadTrailingPending(entries)).toBe(true)
|
|
expect(threadTrailingPending(completedTurn(2, 1, { text: 'a' }).map(entry))).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('resolvePresetId', () => {
|
|
it('prefers the newest selection event over the header', () => {
|
|
const events = [ev('agent-preset/selected', 0, { agentPreset: 'later' })]
|
|
expect(resolvePresetId({ agentPreset: 'early' }, events)).toBe('later')
|
|
expect(resolvePresetId({ agentPreset: 'early' }, [])).toBe('early')
|
|
expect(resolvePresetId({}, [])).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('SIDE_BOUNDARY_PROMPT contract', () => {
|
|
it('opens with the boundary prefix the transcript drops', () => {
|
|
expect(SIDE_BOUNDARY_PROMPT.startsWith('Side conversation boundary')).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('boundaryDelivered', () => {
|
|
it('detects the boundary message and ignores everything else', () => {
|
|
expect(boundaryDelivered([])).toBe(false)
|
|
expect(boundaryDelivered([ev('user/message', 0, {
|
|
content: [{ type: 'text', text: 'ordinary question' }],
|
|
})])).toBe(false)
|
|
// The inherited seed never contains a boundary: parent messages are
|
|
// ordinary user rows.
|
|
expect(boundaryDelivered([
|
|
ev('user/message', 0, { content: [{ type: 'text', text: 'parent q' }] }),
|
|
ev('user/message', 1, {
|
|
content: [{ type: 'text', text: `${SIDE_BOUNDARY_PROMPT}\n\nfirst` }],
|
|
}),
|
|
])).toBe(true)
|
|
// Bare-string content is tolerated too.
|
|
expect(boundaryDelivered([ev('user/message', 0, {
|
|
content: `${SIDE_BOUNDARY_PROMPT}\n\nfirst`,
|
|
})])).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('isContextInjectionMessage', () => {
|
|
it('recognizes plugin-stamped sources structurally', () => {
|
|
expect(isContextInjectionMessage({
|
|
content: [{ type: 'text', text: 'runtime context' }],
|
|
source: { kind: SIDE_INJECTION_SOURCE_KIND },
|
|
})).toBe(true)
|
|
expect(isContextInjectionMessage({
|
|
content: [{ type: 'text', text: 'q' }],
|
|
source: { kind: 'user' },
|
|
})).toBe(false)
|
|
})
|
|
|
|
it('falls back to the boundary prefix for pre-split logs and sourceless rows', () => {
|
|
expect(isContextInjectionMessage({
|
|
content: [{ type: 'text', text: `${SIDE_BOUNDARY_PROMPT}\n\nlegacy question` }],
|
|
source: { kind: 'user' },
|
|
})).toBe(true)
|
|
expect(isContextInjectionMessage({
|
|
content: `${SIDE_BOUNDARY_PROMPT}\n\nlegacy`,
|
|
})).toBe(true)
|
|
expect(isContextInjectionMessage({
|
|
content: [{ type: 'text', text: 'ordinary question' }],
|
|
source: { kind: 'user' },
|
|
})).toBe(false)
|
|
expect(isContextInjectionMessage({ content: [{ type: 'text', text: 'no source row' }] })).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('parentModelSelection', () => {
|
|
it('returns undefined for a log without selection state', () => {
|
|
expect(parentModelSelection([])).toBeUndefined()
|
|
expect(parentModelSelection([
|
|
ev('user/message', 0, { content: [{ type: 'text', text: 'q' }], source: { kind: 'user' } }),
|
|
ev('turn/start', 1, { turn: 1 }),
|
|
])).toBeUndefined()
|
|
})
|
|
|
|
it('adopts an explicit pending selection with its effort', () => {
|
|
// The composer switch logged after the last request: nothing consumed it
|
|
// yet, so it IS the current selection (the view the selector shows).
|
|
expect(parentModelSelection([
|
|
ev('request/header', 0, { header: { config: { provider: 'p1', model: 'm1' } } }),
|
|
ev('model/selection', 1, { provider: 'p2', model: 'm2', reasoningEffort: 'high' }),
|
|
])).toEqual({ provider: 'p2', model: 'm2', reasoningEffort: 'high' })
|
|
})
|
|
|
|
it('lets a same-route request header consume the pending selection preserving explicit effort', () => {
|
|
// Consumed pending -> the header's route is current (effort preserved when not an adapter default).
|
|
expect(parentModelSelection([
|
|
ev('model/selection', 0, { provider: 'p2', model: 'm2', reasoningEffort: 'high' }),
|
|
ev('request/header', 1, { header: { config: { provider: 'p2', model: 'm2', reasoningEffort: 'high' } } }),
|
|
])).toEqual({ provider: 'p2', model: 'm2', reasoningEffort: 'high' })
|
|
})
|
|
|
|
it('drops adapter-default effort from request headers', () => {
|
|
expect(parentModelSelection([
|
|
ev('request/header', 0, {
|
|
header: {
|
|
config: { provider: 'p1', model: 'm1', reasoningEffort: 'low' },
|
|
adapterDefaults: { reasoningEffort: true },
|
|
},
|
|
}),
|
|
])).toEqual({ provider: 'p1', model: 'm1' })
|
|
})
|
|
|
|
it('keeps a pending selection a different-route header cannot consume', () => {
|
|
expect(parentModelSelection([
|
|
ev('model/selection', 0, { provider: 'p2', model: 'm2' }),
|
|
ev('request/header', 1, { header: { config: { provider: 'p1', model: 'm1' } } }),
|
|
])).toEqual({ provider: 'p2', model: 'm2' })
|
|
})
|
|
|
|
it('falls back to the last request header when no explicit selection is pending', () => {
|
|
expect(parentModelSelection([
|
|
ev('request/header', 0, { header: { config: { provider: 'p1', model: 'm1', reasoningEffort: 'low' } } }),
|
|
ev('request/header', 1, { header: { config: { provider: 'p1', model: 'm1' } } }),
|
|
])).toEqual({ provider: 'p1', model: 'm1' })
|
|
})
|
|
|
|
it('preserves non-default effort on request header fallback', () => {
|
|
expect(parentModelSelection([
|
|
ev('request/header', 0, { header: { config: { provider: 'p1', model: 'm1', reasoningEffort: 'high' } } }),
|
|
])).toEqual({ provider: 'p1', model: 'm1', reasoningEffort: 'high' })
|
|
})
|
|
|
|
it('ignores malformed selection and header rows instead of throwing', () => {
|
|
expect(parentModelSelection([
|
|
ev('model/selection', 0, { provider: '', model: 'm2' }),
|
|
ev('model/selection', 1, { provider: 'p2' }),
|
|
ev('request/header', 2, { header: { config: 'garbage' } }),
|
|
ev('request/header', 3, {}),
|
|
ev('model/selection', 4, { provider: 'p3', model: 'm3' }),
|
|
])).toEqual({ provider: 'p3', model: 'm3' })
|
|
})
|
|
})
|
|
|
|
describe('pending inbox folding', () => {
|
|
const message = (id: string, text: string): Record<string, unknown> => ({
|
|
id,
|
|
content: [{ type: 'text', text }],
|
|
})
|
|
const splice = (seq: number, target: string, start: number, removed: number, inserted: unknown[]) =>
|
|
ev('agent/inbox/spliced', seq, {
|
|
target,
|
|
start,
|
|
...(removed === 0 ? {} : { removedCount: removed }),
|
|
inserted,
|
|
})
|
|
|
|
it('projects an inbox in claim order (next-step before next-turn)', () => {
|
|
expect(pendingInputs(
|
|
[message('s1', 'steering')],
|
|
[message('t1', 'turn one'), message('t2', 'turn two')],
|
|
)).toEqual([
|
|
{ id: 's1', target: 'next-step', text: 'steering' },
|
|
{ id: 't1', target: 'next-turn', text: 'turn one' },
|
|
{ id: 't2', target: 'next-turn', text: 'turn two' },
|
|
])
|
|
})
|
|
|
|
it('skips messages without an identity (they could not be dequeued)', () => {
|
|
expect(pendingInputs([], [{ content: [{ type: 'text', text: 'no id' }] }])).toEqual([])
|
|
})
|
|
|
|
it('flattens multi-block content and tolerates a missing content array', () => {
|
|
expect(pendingInputs([], [
|
|
{ id: 'm1', content: [{ type: 'text', text: 'a' }, { type: 'image' }, { type: 'text', text: 'b' }] },
|
|
{ id: 'm2' },
|
|
])).toEqual([
|
|
{ id: 'm1', target: 'next-turn', text: 'a\nb' },
|
|
{ id: 'm2', target: 'next-turn', text: '' },
|
|
])
|
|
})
|
|
|
|
it('folds splices to the messages still pending', () => {
|
|
const log = [
|
|
splice(0, 'next-turn', 0, 0, [message('a', 'first')]),
|
|
splice(1, 'next-turn', 0, 1, []),
|
|
splice(2, 'next-turn', 0, 0, [message('b', 'second')]),
|
|
splice(3, 'next-step', 0, 0, [message('c', 'steering')]),
|
|
]
|
|
expect(pendingIdsInLog(log)).toEqual(['c', 'b'])
|
|
})
|
|
|
|
it('honours the fold limit for the inherited prefix', () => {
|
|
const log = [
|
|
splice(0, 'next-turn', 0, 0, [message('ghost', 'already claimed by the parent')]),
|
|
ev('session/end-seed', 1, { inherited: true }),
|
|
splice(2, 'next-turn', 0, 1, []),
|
|
splice(3, 'next-turn', 0, 0, [message('mine', 'queued in the thread')]),
|
|
]
|
|
// Whole log: the ghost was claimed inside the child's own history.
|
|
expect(pendingIdsInLog(log)).toEqual(['mine'])
|
|
// Inherited prefix only: the ghost is exactly what the resume sweep drops.
|
|
expect(inheritedPendingIds(log, 2)).toEqual(['ghost'])
|
|
})
|
|
|
|
it('ignores malformed splices instead of throwing', () => {
|
|
const log = [
|
|
ev('agent/inbox/spliced', 0, { target: 'nope', start: 0, inserted: [message('x', 'x')] }),
|
|
ev('agent/inbox/spliced', 1, { target: 'next-turn', start: 'garbage', inserted: 'not-an-array' }),
|
|
ev('agent/inbox/spliced', 2, { target: 'next-turn', start: 99, inserted: [message('y', 'y')] }),
|
|
]
|
|
expect(pendingIdsInLog(log)).toEqual(['y'])
|
|
})
|
|
|
|
it('reads the effective headline selection through the shared helper', () => {
|
|
expect(effectiveModelSelection([
|
|
ev('request/header', 0, { header: { config: { provider: 'p1', model: 'm1' } } }),
|
|
ev('model/selection', 1, { provider: 'p2', model: 'm2' }),
|
|
])).toEqual({ provider: 'p2', model: 'm2' })
|
|
})
|
|
})
|