侧边对话的四处用户痛点,加上「实时 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 通道。
193 lines
6.9 KiB
TypeScript
193 lines
6.9 KiB
TypeScript
/**
|
|
* Side-chat push spec: the hub turns the two host feeds (the session append
|
|
* feed and the process-local assistant stream) into per-thread INVALIDATION
|
|
* notices, coalesced per thread. What matters here is exactly that shape —
|
|
* a watched thread hears about both feeds, an unwatched one stays silent, a
|
|
* burst folds into one notice, and a socket that goes away stops hearing.
|
|
*/
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { createSidechatPushHub } from '../src/sidechat-push.ts'
|
|
import type { Context } from '../src/context-types.ts'
|
|
import type { WebSocket } from 'ws'
|
|
|
|
/** One captured listener of a fake context event. */
|
|
type Listener = (...args: unknown[]) => void
|
|
|
|
/** A context exposing just the `on` table the hub subscribes through. */
|
|
function fakeCtx(): { ctx: Context; emit: (event: string, ...args: unknown[]) => void; listeners: Map<string, Set<Listener>> } {
|
|
const listeners = new Map<string, Set<Listener>>()
|
|
const ctx = {
|
|
on: (event: string, listener: Listener): (() => void) => {
|
|
let set = listeners.get(event)
|
|
if (set === undefined) listeners.set(event, set = new Set())
|
|
set.add(listener)
|
|
return () => { set.delete(listener) }
|
|
},
|
|
} as unknown as Context
|
|
return {
|
|
ctx,
|
|
listeners,
|
|
emit: (event, ...args) => {
|
|
for (const listener of [...(listeners.get(event) ?? [])]) listener(...args)
|
|
},
|
|
}
|
|
}
|
|
|
|
/** A minimal WebSocket double: records sends, can be driven and closed. */
|
|
class FakeSocket {
|
|
readonly sent: string[] = []
|
|
private readonly handlers = new Map<string, Set<Listener>>()
|
|
|
|
on(event: string, handler: Listener): void {
|
|
let set = this.handlers.get(event)
|
|
if (set === undefined) this.handlers.set(event, set = new Set())
|
|
set.add(handler)
|
|
}
|
|
|
|
send(data: string): void {
|
|
this.sent.push(data)
|
|
}
|
|
|
|
/** Deliver one inbound frame to the hub. */
|
|
receive(frame: unknown): void {
|
|
for (const handler of [...(this.handlers.get('message') ?? [])]) {
|
|
handler(typeof frame === 'string' ? frame : JSON.stringify(frame))
|
|
}
|
|
}
|
|
|
|
/** Fire an event (`close`/`error`) at the socket. */
|
|
fire(event: string): void {
|
|
for (const handler of [...(this.handlers.get(event) ?? [])]) handler()
|
|
}
|
|
|
|
asWebSocket(): WebSocket {
|
|
return this as unknown as WebSocket
|
|
}
|
|
|
|
frames(): { threadId: string }[] {
|
|
return this.sent.map(raw => JSON.parse(raw) as { threadId: string })
|
|
}
|
|
}
|
|
|
|
beforeEach(() => { vi.useFakeTimers() })
|
|
afterEach(() => { vi.useRealTimers() })
|
|
|
|
describe('sidechat push hub', () => {
|
|
it('notifies a watched thread on a durable append and on a stream frame', () => {
|
|
const { ctx, emit } = fakeCtx()
|
|
const hub = createSidechatPushHub(ctx)
|
|
const socket = new FakeSocket()
|
|
hub.attach(socket.asWebSocket())
|
|
socket.receive({ op: 'watch', threadIds: ['t1'] })
|
|
|
|
emit('session/event', { id: 't1' }, { type: 'assistant/message' })
|
|
vi.advanceTimersByTime(60)
|
|
expect(socket.frames()).toEqual([{ threadId: 't1' }])
|
|
|
|
emit('agent/assistant-stream', { agent: { session: { id: 't1' } }, frame: { type: 'chunk' } })
|
|
vi.advanceTimersByTime(250)
|
|
expect(socket.frames()).toEqual([{ threadId: 't1' }, { threadId: 't1' }])
|
|
hub.dispose()
|
|
})
|
|
|
|
it('stays silent for threads the socket did not watch', () => {
|
|
const { ctx, emit } = fakeCtx()
|
|
const hub = createSidechatPushHub(ctx)
|
|
const socket = new FakeSocket()
|
|
hub.attach(socket.asWebSocket())
|
|
socket.receive({ op: 'watch', threadIds: ['t1'] })
|
|
|
|
emit('session/event', { id: 'other' }, { type: 'turn/end' })
|
|
emit('agent/assistant-stream', { agent: { session: { id: 'other' } }, frame: { type: 'chunk' } })
|
|
vi.advanceTimersByTime(1_000)
|
|
expect(socket.frames()).toEqual([])
|
|
hub.dispose()
|
|
})
|
|
|
|
it('folds a burst of stream frames into one notice', () => {
|
|
const { ctx, emit } = fakeCtx()
|
|
const hub = createSidechatPushHub(ctx)
|
|
const socket = new FakeSocket()
|
|
hub.attach(socket.asWebSocket())
|
|
socket.receive({ op: 'watch', threadIds: ['t1'] })
|
|
|
|
for (let index = 0; index < 20; index++) {
|
|
emit('agent/assistant-stream', { agent: { session: { id: 't1' } }, frame: { type: 'chunk' } })
|
|
vi.advanceTimersByTime(10)
|
|
}
|
|
// The first frame armed the 250ms window; the burst never postpones it.
|
|
expect(socket.frames()).toEqual([])
|
|
vi.advanceTimersByTime(60)
|
|
expect(socket.frames()).toEqual([{ threadId: 't1' }])
|
|
hub.dispose()
|
|
})
|
|
|
|
it('replaces the watch set per frame (an unwatched thread drops its pending notice)', () => {
|
|
const { ctx, emit } = fakeCtx()
|
|
const hub = createSidechatPushHub(ctx)
|
|
const socket = new FakeSocket()
|
|
hub.attach(socket.asWebSocket())
|
|
socket.receive({ op: 'watch', threadIds: ['t1', 't2'] })
|
|
emit('session/event', { id: 't1' }, { type: 'turn/end' })
|
|
|
|
// Re-watch with t2 only BEFORE the first notice fires: t1 is forgotten
|
|
// together with the notice it had scheduled.
|
|
socket.receive({ op: 'watch', threadIds: ['t2'] })
|
|
vi.advanceTimersByTime(1_000)
|
|
expect(socket.frames()).toEqual([])
|
|
|
|
emit('session/event', { id: 't2' }, { type: 'turn/end' })
|
|
vi.advanceTimersByTime(60)
|
|
expect(socket.frames()).toEqual([{ threadId: 't2' }])
|
|
hub.dispose()
|
|
})
|
|
|
|
it('ignores malformed frames and keeps only string ids', () => {
|
|
const { ctx, emit } = fakeCtx()
|
|
const hub = createSidechatPushHub(ctx)
|
|
const socket = new FakeSocket()
|
|
hub.attach(socket.asWebSocket())
|
|
socket.receive('not json')
|
|
socket.receive({ op: 'nope' })
|
|
// A non-array threadIds clears the set; the mixed array then watches t1.
|
|
expect(() => { socket.receive({ op: 'watch', threadIds: 't1' }) }).not.toThrow()
|
|
socket.receive({ op: 'watch', threadIds: [1, null, 't1'] })
|
|
|
|
emit('session/event', { id: 't1' }, { type: 'turn/end' })
|
|
vi.advanceTimersByTime(60)
|
|
expect(socket.frames()).toEqual([{ threadId: 't1' }])
|
|
hub.dispose()
|
|
})
|
|
|
|
it('caps the watch set and stops pushing once the socket closes', () => {
|
|
const { ctx, emit } = fakeCtx()
|
|
const hub = createSidechatPushHub(ctx)
|
|
const socket = new FakeSocket()
|
|
hub.attach(socket.asWebSocket())
|
|
const overflow = Array.from({ length: 80 }, (_, index) => `t${index}`)
|
|
socket.receive({ op: 'watch', threadIds: overflow })
|
|
|
|
emit('session/event', { id: 't79' }, { type: 'turn/end' })
|
|
vi.advanceTimersByTime(60)
|
|
// 80 ids capped to 64: the 65th+ ids are not watched.
|
|
expect(socket.frames()).toEqual([])
|
|
|
|
emit('session/event', { id: 't0' }, { type: 'turn/end' })
|
|
vi.advanceTimersByTime(60)
|
|
expect(socket.frames()).toEqual([{ threadId: 't0' }])
|
|
|
|
socket.fire('close')
|
|
emit('session/event', { id: 't0' }, { type: 'turn/end' })
|
|
vi.advanceTimersByTime(1_000)
|
|
expect(socket.frames()).toEqual([{ threadId: 't0' }])
|
|
hub.dispose()
|
|
})
|
|
|
|
it('unbinds both feeds on dispose', () => {
|
|
const { ctx, listeners } = fakeCtx()
|
|
const hub = createSidechatPushHub(ctx)
|
|
hub.dispose()
|
|
for (const set of listeners.values()) expect(set.size).toBe(0)
|
|
})
|
|
})
|