diff --git a/.scratch/codex-execution-history/spec.md b/.scratch/codex-execution-history/spec.md new file mode 100644 index 0000000..ed67714 --- /dev/null +++ b/.scratch/codex-execution-history/spec.md @@ -0,0 +1,25 @@ +# 按需读取 Codex 执行历史 + +Status: ready-for-agent + +用户要求任务详情按需读取当前 Codex Thread,运行中接收实时消息。Agent Loop 不长期保存另一套执行轨迹,不处理旧 Thread 或旧历史兼容。 + +## 验收 + +- 新数据库没有 execution_events 表,运行中不向数据库或本地文件写入执行轨迹副本。 +- 看板任务详情从 Codex 读取可见历史。读取不恢复 Thread、不启动轮次、不回答审批。 +- 实时消息与历史按稳定消息身份合并,完成文字不重复。 +- 实时游标在进程重启或缓存丢失后触发重新加载。 +- Codex 不可用时仍能查看业务事实,飞书不以空历史覆盖现有内容。 +- 飞书发布成功后,数据库只保存发布标识、摘要和状态,不保存已发布正文。待确认操作可保存准确的重试正文,确认后清除。 +- 任务生命周期、业务输入输出、显式汇报、审批和项目记录继续持久保存。 + +## 范围 + +修改本地源码、测试和文档。生产实例重启、数据清理与旧历史迁移不在本次范围。 + +## Comments + +2026-11-20:实现完成。删除执行轨迹持久化,按需读取当前 Codex Thread,飞书确认后清除待发正文。类型检查和构建通过,全量测试735项通过。默认并发下的一项原有子进程启动超时在独立重跑及限制并发的全套运行中通过。最终独立复查因模型连接故障未返回结论,不宣称最终审查通过。 + +最终浏览器验收14场景40断言通过,SQLite全表执行正文检查通过。真实只读探针通过,仅读取本次验证创建的Thread。 diff --git a/README.md b/README.md index 6e2f3e7..04c2c84 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ npm start -- run --config config.json `documentBaseUrl` 中的 `https://example.feishu.cn` 是占位值,替换为企业实际的 HTTPS 飞书文档域名,不带文档路径、端口、查询参数或片段。文档由飞书托管,不依赖运行 Agent Loop 的电脑能否被手机访问。文档模式不会在失败时改发进度卡片。 -`maxLines` 是文档展示的逻辑行数上限,按文本中的换行计算,包含状态、角色标签和空行;屏幕自动折行不另计。已有文档在恢复后从本地保存的输入和 AI 执行事件重建历史。文档显示附件名称,附件原件保留在任务材料中。 +`maxLines` 是文档展示的逻辑行数上限,按文本中的换行计算,包含状态、角色标签和空行;屏幕自动折行不另计。文档从本地保存的业务输入、当前 Codex Thread 历史和实时执行消息生成。Agent Loop 不另存执行历史。文档显示附件名称,附件原件保留在任务材料中。 机器人需要以下应用身份权限: @@ -165,7 +165,7 @@ npm start -- run --config config.json 默认直接在飞书卡片中查看进度,不显示网页按钮。需要完整可见文字入口时,可另加 `dashboardUrl`,填写飞书客户端能访问的 HTTP 或 HTTPS 地址。网络地址能否访问取决于运行主机的网络和防火墙配置。看板不提供登录或 token 鉴权。配置修改后按 [服务操作说明](docs/agents/operations.md) 重启执行服务。 -机器人需要 `cardkit:card:write` 权限及已有的消息发送权限。每轮执行发送一张进度卡片,后续合并更新同一张卡片。卡片展示执行状态、最近执行事件时间、模型文字和有长度限制的工具调用状态;完整可见文字保留在调试台。没有新事件时,最近执行事件时间保持原值。最终答复继续作为独立普通消息发送。 +机器人需要 `cardkit:card:write` 权限及已有的消息发送权限。每轮执行发送一张进度卡片,后续合并更新同一张卡片。卡片展示执行状态、最近执行事件时间、模型文字和有长度限制的工具调用状态。调试台按需从 Codex 读取当前 Thread 的可见历史,并接收实时更新。没有新事件时,最近执行事件时间保持原值。最终答复继续作为独立普通消息发送。 卡片身份和更新顺序持久化,服务恢复后继续更新原卡片。首次启用不补发已结束的历史轮次。进度投递失败记录到诊断,不触发任务重跑,也不阻断最终答复。飞书消息发送结果不明确时,先核对原群消息与已保存的卡片实体;找到原消息便继续更新,确认没有发送且仍在平台去重保证范围内才重试。无法完成核对时不会盲目补建卡片。 diff --git a/docs/adr/0016-project-dashboard-boundaries.md b/docs/adr/0016-project-dashboard-boundaries.md index eee2066..a126b04 100644 --- a/docs/adr/0016-project-dashboard-boundaries.md +++ b/docs/adr/0016-project-dashboard-boundaries.md @@ -2,6 +2,8 @@ Status: accepted +执行文字的持久化约定已由 [ADR 0022](0022-codex-owns-execution-history.md) 取代。以下保留原决策,其余看板边界继续有效。 + 项目看板首版采用项目总览和任务深钻两层只读视图。总览包含项目协调者、Worker、verifier、已路由 intake 以及已结束历史任务,只显示任务卡片和每个任务的最近活动,不建立跨 Worker 的项目总时间线。任务深钻显示 Codex CLI 风格的可见执行说明、任务生命周期、工具开始/结束元数据、错误和显式 report,并永久保留完整可见文字和报告历史;工具输出正文、隐藏模型推理和私有 textDelta 不进入看板持久化记录。看板监听网络地址,任何能连通端口的电脑都可以读取所有项目和任务数据,不要求登录、token 或项目级鉴权。 这样可以复用现有项目关系和任务执行事实,长期保留真正需要复盘的可见过程文字和报告,同时避免把高频工具输出写入数据库。 diff --git a/docs/adr/0022-codex-owns-execution-history.md b/docs/adr/0022-codex-owns-execution-history.md new file mode 100644 index 0000000..cd67133 --- /dev/null +++ b/docs/adr/0022-codex-owns-execution-history.md @@ -0,0 +1,13 @@ +--- +status: accepted +--- + +# Codex 保存执行历史 + +2026-11-20,用户确认执行历史由 Codex 保存,Agent Loop 查看任务详情时读取当前 Thread,运行中的展示使用有界内存数据。此决策取代 ADR 0016 中由看板永久保存可见过程文字的约定,不迁移旧 Thread 或旧执行轨迹。 + +SQLite 继续保存任务、业务输入输出、显式汇报、审批和项目记录。飞书进度发布成功后只保留标识、摘要和发布状态。需要确认外部写入结果的正文仅作为待确认操作保存,确认后清除,不能归档为另一套执行历史。 + +历史读取不得恢复 Thread、启动 Turn 或处理原生请求。Codex 历史不可用时,看板明确显示读取失败并保留业务事实,飞书保留已发布内容等待重试。项目总览不逐个读取 Thread 历史。实时游标失效时,客户端重新读取当前历史。 + +历史快照与实时通知没有共同的增量偏移。没有收到消息起点时,增量只触发重新读取,不猜测文字重叠。未解决缺口的身份被内存上限淘汰后,运行中的历史保留重读提示,完整结束的历史可以恢复可用状态。此保守行为避免用缺字的轨迹冒充已同步结果。 diff --git a/docs/architecture.md b/docs/architecture.md index d3773d5..480ae73 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -28,6 +28,10 @@ Runtime 状态为 `pending`、`running`、`idle`、`paused`、`failed`。原生 输出按稳定 AgentOutput ID 去重,只转发用户可见进展与最终答复。原始工具日志和内部推理不作为普通消息发送。旧版 `turn:` 出站记录继续作为历史通知屏障,避免升级后重发已处理轮次。pending、sending、sent 和 uncertain 区分准备发送与实际确认。 +执行历史由 Codex 保存。任务详情按需读取当前 Thread 的历史,实时执行消息只保存在有界内存中。SQLite 不保存执行轨迹,项目总览不批量读取 Thread 历史。历史读取与执行恢复分开,读取不会启动或恢复任务,也不处理原生请求。历史读取失败时,业务输入、汇报和任务状态仍可查看。实时游标在进程重启或缓存淘汰后失效,客户端重新加载详情。 + +飞书进度由当前 Thread 历史、实时消息和业务输入生成。发布记录保存远端标识、内容摘要、顺序和重试状态。待确认的写入正文在确认后清除,不长期保留已发布文字。历史不可用时保留远端现有内容。此边界见 [ADR 0022](adr/0022-codex-owns-execution-history.md)。 + ## 输入与来源 所有者在项目协调群交办,CLI submit、answer 和机器人私聊不再接单。协调者评估后才通过内部工具派发 Worker。GitHub 收集与历史请求仍先进入项目协调,不自动启动执行。Worker 默认无群,直接交流或原生审批时才建立任务群。 diff --git a/scripts/verify-project-coordinator.ts b/scripts/verify-project-coordinator.ts index d68b9d3..21524f9 100644 --- a/scripts/verify-project-coordinator.ts +++ b/scripts/verify-project-coordinator.ts @@ -159,7 +159,7 @@ function newLoop(): AgentLoop { function snapshot(current: AgentLoop): unknown { return { project: current.projects.ledger.project(projectId), tasks: current.tasks().map(task => ({ task, inputs: current.store.inputs(task.id), outputs: current.store.outputs(task.id), - approvals: current.store.approvals(task.id), events: current.store.executionEvents(task.id, 0, 100_000) })), + approvals: current.store.approvals(task.id), events: current.execution.current(task.id) })), diagnostics: current.store.diagnostics(), fakeFeishu: { groups: [...feishu.groups], sent: feishu.sent, approvals: feishu.approvals }, fakeGithub: { sources: github.sources, verifications: github.verifications } }; } @@ -183,7 +183,6 @@ function readDatabase(): unknown { inbox: 'SELECT * FROM inbox ORDER BY seq', outbox: 'SELECT * FROM outbox ORDER BY rowid', operations: 'SELECT * FROM operations ORDER BY rowid', projectMetadata: "SELECT * FROM meta WHERE key LIKE 'project:%' OR key LIKE 'project-generation:%' OR key LIKE 'project-archived-thread:%' ORDER BY key", - executionEvents: 'SELECT * FROM execution_events ORDER BY seq', diagnostics: 'SELECT * FROM diagnostics ORDER BY id', }; return Object.fromEntries(Object.entries(queries).map(([name, sql]) => [name, { sql, rows: db.prepare(sql).all() }])); @@ -286,9 +285,9 @@ async function verify(): Promise { assert.ok(reportInput); assert.equal(reportInput.provenance.kind, 'agent'); assert.equal(reportInput.status, 'delivered'); - const receiptTurnStart = loop.store.executionEvents(coordinatorId, 0, 100_000).find(event => event.kind === 'turn_start' && event.turnId === recorded.request.turnId); - assert.ok(receiptTurnStart && receiptTurnStart.createdAt >= reportArrival.at, 'Receipt turn must start after the actual worker report'); - const workerEvents = loop.store.executionEvents(worker.taskId, 0, 100_000); + const receiptTurnStart = loop.execution.current(coordinatorId).find(event => event.kind === 'turn_start' && event.turnId === recorded.request.turnId); + assert.ok(receiptTurnStart && (receiptTurnStart.createdAt ?? 0) >= reportArrival.at, 'Receipt turn must start after the actual worker report'); + const workerEvents = loop.execution.current(worker.taskId); assert.ok(workerEvents.some(event => ['tool_output', 'tool_end'].includes(event.kind) && event.text.includes(marker)), 'Marker must be observed in actual worker tool output'); const workerOutputs = loop.store.outputs(worker.taskId).map(output => output.text).join('\n'); @@ -296,7 +295,7 @@ async function verify(): Promise { const coordinatorOutputs = loop.store.outputs(coordinatorId).map(output => output.text).join('\n'); assert.match(coordinatorOutputs, /COORDINATOR_RECEIPT_RECORDED/); assert.ok(coordinatorOutputs.includes(marker)); - for (const task of tasks) for (const event of loop.store.executionEvents(task.id, 0, 100_000)) { + for (const task of tasks) for (const event of loop.execution.current(task.id)) { if (event.kind === 'tool_start') assert.ok(event.label.startsWith('project_action') || event.text.startsWith('$ '), 'No external tool or nested agent may be used'); } assert.deepEqual({ files: (await readdir(directory)).sort(), diff --git a/src/adapters/codex.ts b/src/adapters/codex.ts index eca577c..ca51b92 100644 --- a/src/adapters/codex.ts +++ b/src/adapters/codex.ts @@ -3,7 +3,7 @@ import { setTimeout as delay } from 'node:timers/promises'; import { z } from 'zod'; import { RejectedInputError, TurnChangedError, UncertainOperationError, workspaceTargetSchema, type AgentInput, type AgentOutput, type ExecutionSnapshot, type PendingRequest, type WorkspaceRequest } from '../domain.js'; import { CodexProviderMismatchError, type Activity, type CodexPort, type TurnIdentity } from '../ports.js'; -import { executionEvent, type ExecutionEvent } from '../execution-events.js'; +import { executionEvent, executionKey, type ExecutionHistory, type ExecutionEvent } from '../execution-events.js'; import { projectActionSchema } from '../project.js'; import type { ReasoningEffort } from '../execution.js'; import { normalizeDaemonSettings, sharedDaemonConnector, type CodexConnection, type CodexConnector, type DaemonSettings } from './codex-daemon.js'; @@ -179,9 +179,38 @@ export class CodexAppServer implements CodexPort { private projectHandler: Parameters>[0] | undefined; private readonly outputs = new Map(); private sequence = 0; + private observationReset: (() => void) | undefined; private eventHandler: ((event: ExecutionEvent) => void) | undefined; private completionPolicy: { isCompleted(threadId: string): boolean } | undefined; + async readHistory(threadId: string): Promise { + await this.ready(); + const epoch = this.epoch; + const historyTurn = turnSchema.extend({ itemsView: z.literal('full'), startedAt: z.number().nullable(), completedAt: z.number().nullable() }); + const { thread } = z.object({ thread: z.object({ id: z.string(), turns: z.array(historyTurn) }) }) + .parse(await this.rpc('thread/read', { threadId, includeTurns: true })); + if (thread.id !== threadId) throw new Error('Codex history Thread identity changed'); + const entries: ExecutionHistory = [], turns = new Set(); + for (const turn of thread.turns) { + if (turns.has(turn.id)) throw new Error('Codex history repeated a turn'); + turns.add(turn.id); + for (const item of turn.items) { + const event = executionEvent('item/completed', { threadId, turnId: turn.id, item }); + if (!event) { + if (['agentMessage', 'commandExecution', 'fileChange', 'mcpToolCall', 'dynamicToolCall', 'webSearch'].includes(item.type)) throw new Error('Codex visible history item is incomplete'); + continue; + } + const timing = z.object({ startedAtMs: z.number().nullish(), completedAtMs: z.number().nullish() }).parse(item); + entries.push({ ...event, kind: event.kind === 'agent_message' && turn.status === 'inProgress' && timing.completedAtMs == null ? 'agent_delta' : event.kind, + key: executionKey(event), createdAt: timing.startedAtMs ?? (turn.startedAt === null ? null : turn.startedAt * 1000) }); + } + const event = executionEvent(turn.status === 'inProgress' ? 'turn/started' : 'turn/completed', { threadId, turn }); + if (event) entries.push({ ...event, key: executionKey(event), createdAt: (turn.completedAt ?? turn.startedAt) === null ? null : (turn.completedAt ?? turn.startedAt)! * 1000 }); + } + if (epoch !== this.epoch) throw new Error('Codex connection changed during history read'); + return entries; + } + setObservationResetHandler(handler: () => void): void { this.observationReset = handler; } setEventHandler(handler: (event: ExecutionEvent) => void): void { this.eventHandler = handler; } setThreadCompletionPolicy(policy: { isCompleted(threadId: string): boolean }): void { this.completionPolicy = policy; } assertThreadCompletionAllowed(threadId: string): void { @@ -838,6 +867,7 @@ export class CodexAppServer implements CodexPort { } private async reconnect(): Promise { + this.observationReset?.(); this.connection = undefined; this.initialized = undefined; this.failure = undefined; diff --git a/src/cli.ts b/src/cli.ts index 1ee1f89..13b85bc 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -135,7 +135,7 @@ async function main(): Promise { const stop = () => { void close(); }; process.on('SIGINT', stop); process.on('SIGTERM', stop); try { - dashboardStart = startDashboard({ dataDir: config.dataDir, host: config.dashboard.host, port: config.dashboard.port }); + dashboardStart = startDashboard({ execution: loop.execution, dataDir: config.dataDir, host: config.dashboard.host, port: config.dashboard.port }); const dashboard = await dashboardStart; if (closing) { await closing; return; } executionWatcher.start(); diff --git a/src/dashboard-page.ts b/src/dashboard-page.ts index d0631a4..238fe1a 100644 --- a/src/dashboard-page.ts +++ b/src/dashboard-page.ts @@ -31,6 +31,7 @@ const labels = { pending: '待启动', running: '执行中', idle: '等待输入 const roleLabels = { coordinator: '协调者', worker: 'Worker', intake: 'intake' }; const typeLabels = { INPUT: 'INPUT', PHASE: 'PHASE', NARRATIVE: 'NARRATIVE', TOOL_CALL: 'TOOL CALL', WAITING: 'WAITING', BLOCKER: 'BLOCKER', OUTPUT: 'OUTPUT', EVIDENCE: 'EVIDENCE', RETRY: 'RETRY' }; let snapshot = null, selectedId = null, stream = null, filter = 'all', lastSeq = 0, polling = false, entries = [], detail = null, selectionGeneration = 0; +let detailRetryAt = 0, detailRetryDelay = 1000, detailLoading = false, needsDetail = false; const stageDisclosure = new Map(); const time = value => new Date(value).toLocaleTimeString('zh-CN', { hour12: false }); const node = (tag, className, text) => { const el = document.createElement(tag); if (className) el.className = className; if (text !== undefined) el.textContent = text; return el; }; @@ -45,14 +46,59 @@ function currentTask() { return allTasks().find(task => task.id === selectedId) function renderMetadata() { const task = currentTask(); if (!task) return; $('meta-role').textContent = roleLabels[task.role] || 'Worker'; $('meta-status').textContent = labels[task.status] || task.status; $('meta-status-dot').className = statusClass(task.status); const resources = task.resources || []; const values = [['Worker ID', task.id], ['项目', task.projectId ? projectName(task.projectId) : '未关联项目'], ['Thread', task.threadId], ['Turn', task.turnId || task.currentTurn], ['任务群', task.chatId], ['仓库', task.repository], ...resources.map(resource => [resource.kind === 'project' ? '项目工作区' : '仓库工作区', resource.workspace]), ['更新时间', time(task.updatedAt)]]; const dl = document.createDocumentFragment(); for (const [label, value] of values) { dl.append(node('dt','',label), node('dd','',value || '尚未关联')); } $('metadata-list').replaceChildren(dl); } function renderTaskHeader() { const task = currentTask(); $('empty').hidden = !!task; $('task-panel').hidden = !task; if (!task) return; $('source-kind').textContent = task.sourceKind === 'github' ? 'GITHUB ISSUE' : task.sourceKind === 'cli' ? 'CLI SUBMISSION' : 'WORKER TASK'; $('task-badge').className = 'badge ' + task.status; $('task-badge').textContent = labels[task.status] || task.status; $('task-title').textContent = task.title; $('task-id').textContent = task.id; $('updated-at').textContent = '状态更新 ' + time(task.updatedAt); const source = $('source-link'); source.hidden = !task.sourceUrl; if (task.sourceUrl && /^https?:\/\//.test(task.sourceUrl)) source.href = task.sourceUrl; else source.removeAttribute('href'); const alerts = []; if (task.reason) alerts.push(task.reason); for (const request of task.requests || []) alerts.push((request.kind === 'question' ? '等待回答:' : '等待审批:') + request.prompt); $('task-alert').hidden = !alerts.length; $('task-alert').textContent = alerts.join('\n'); $('task-alert').className = 'banner' + (task.status === 'failed' ? ' error' : ''); renderMetadata(); } function normalizeEntry(entry) { return { ...entry, type: entry.type || (entry.kind === 'agent_message' ? 'OUTPUT' : entry.kind === 'agent_delta' ? 'NARRATIVE' : entry.kind?.startsWith('tool_') ? 'TOOL_CALL' : entry.kind === 'error' ? 'BLOCKER' : 'PHASE') }; } -function collapse(list) { const result = []; for (const entry of list) { const previous = result[result.length - 1]; if (previous && previous.type === entry.type && previous.phase === entry.phase && previous.turnId === entry.turnId && previous.itemId === entry.itemId && entry.type === 'NARRATIVE') { previous.text += entry.text; previous.collapsedCount = (previous.collapsedCount || 1) + 1; previous.seq = entry.seq; previous.createdAt = entry.createdAt; } else result.push({ ...entry }); } return result; } -function renderTimeline() { const target = $('output'), visible = collapse(entries.filter(entry => filter === 'all' || entry.type === filter)); if (!visible.length) { target.replaceChildren(node('div','sidebar-empty', entries.length ? '没有匹配的事件' : '等待执行输出')); $('event-count').textContent = '0 条事件'; return; } const groups = []; for (const entry of visible) { const key = entry.phase || (entry.turnId ? 'TURN ' + entry.turnId.slice(0,8) : '执行记录'); let group = groups.at(-1); if (!group || group.key !== key) { group = { key, entries: [] }; groups.push(group); } group.entries.push(entry); } const fragment = document.createDocumentFragment(); for (const group of groups) { const stage = document.createElement('details'); stage.className = 'stage'; stage.open = stageDisclosure.get(group.key) ?? true; stage.addEventListener('toggle', () => stageDisclosure.set(group.key, stage.open)); const summary = node('summary','',group.key); stage.append(summary); for (const entry of group.entries) { const el = node('article','event'); el.dataset.type = entry.type; const head = node('div','event-header'); head.append(node('span','event-type',typeLabels[entry.type] || entry.type), node('span','',entry.label || ''), node('time','',time(entry.createdAt)), node('code','',entry.turnId || '')); el.append(head,node('pre','',entry.text || '')); if (entry.collapsedCount > 1) el.append(node('div','collapsed',entry.collapsedCount + ' 条相邻叙事已折叠')); stage.append(el); } fragment.append(stage); } target.replaceChildren(fragment); $('event-count').textContent = visible.length + ' 条事件'; if ($('auto-scroll').checked) target.scrollTop = target.scrollHeight; } -function mergeEvent(event) { if (event.taskId !== selectedId || event.seq <= lastSeq) return; lastSeq = event.seq; entries.push(normalizeEntry(event)); renderTimeline(); $('last-event').textContent = '最后事件 ' + time(event.createdAt); } -async function loadDetail(id, generation) { const response = await fetch('/api/tasks/' + encodeURIComponent(id)); if (!response.ok) throw new Error('读取 Worker 详情失败'); const nextDetail = await response.json(); if (generation !== selectionGeneration || selectedId !== id) return false; detail = nextDetail; entries = (detail.timeline || detail.events || []).map(normalizeEntry); lastSeq = Math.max(0, ...entries.map(entry => entry.seq || 0)); renderTaskHeader(); renderTimeline(); renderHistory(); renderDiagnostics(); return true; } +function collapse(list) { return list; } +function renderTimeline() { const stages = new Set(entries.map(entry => entry.phase || (entry.turnId ? 'TURN ' + entry.turnId.slice(0,8) : '执行记录'))); for (const key of stageDisclosure.keys()) if (!stages.has(key)) stageDisclosure.delete(key); const target = $('output'), visible = collapse(entries.filter(entry => filter === 'all' || entry.type === filter)); if (!visible.length) { target.replaceChildren(node('div','sidebar-empty', entries.length ? '没有匹配的事件' : '等待执行输出')); $('event-count').textContent = '0 条事件'; return; } const groups = []; for (const entry of visible) { const key = entry.phase || (entry.turnId ? 'TURN ' + entry.turnId.slice(0,8) : '执行记录'); let group = groups.at(-1); if (!group || group.key !== key) { group = { key, entries: [] }; groups.push(group); } group.entries.push(entry); } const fragment = document.createDocumentFragment(); for (const group of groups) { const stage = document.createElement('details'); stage.className = 'stage'; stage.open = stageDisclosure.get(group.key) ?? true; stage.addEventListener('toggle', () => stageDisclosure.set(group.key, stage.open)); const summary = node('summary','',group.key); stage.append(summary); for (const entry of group.entries) { const el = node('article','event'); el.dataset.type = entry.type; const head = node('div','event-header'); head.append(node('span','event-type',typeLabels[entry.type] || entry.type), node('span','',entry.label || ''), node('time','',entry.createdAt === null ? '时间未知' : time(entry.createdAt)), node('code','',entry.turnId || '')); el.append(head,node('pre','',entry.text || '')); if (entry.collapsedCount > 1) el.append(node('div','collapsed',entry.collapsedCount + ' 条相邻叙事已折叠')); stage.append(el); } fragment.append(stage); } target.replaceChildren(fragment); $('event-count').textContent = visible.length + ' 条事件'; if ($('auto-scroll').checked) target.scrollTop = target.scrollHeight; } +function mergeEvent(event) { if (event.taskId !== selectedId) return; const id = 'event:' + event.key; const index = entries.findIndex(entry => entry.id === id); const next = normalizeEntry({ ...event, id }); if (index < 0) entries.push(next); else entries[index] = next; entries = entries.slice(-500); renderTimeline(); $('last-event').textContent = '最后事件 ' + time(event.createdAt); } +function retryDetail() { needsDetail = true; detailRetryAt = Date.now() + detailRetryDelay; detailRetryDelay = Math.min(detailRetryDelay * 2, 30000); } +async function loadDetail(id, generation) { + const response = await fetch('/api/tasks/' + encodeURIComponent(id)); + if (generation !== selectionGeneration || selectedId !== id) return false; + if (response.status === 404) { if (stream) stream.close(); stream = null; selectedId = null; detail = null; entries = []; needsDetail = false; renderTaskHeader(); renderTimeline(); return false; } + if (!response.ok) throw new Error('读取 Worker 详情失败'); + const nextDetail = await response.json(); + if (generation !== selectionGeneration || selectedId !== id) return false; + if (detail?.threadId !== nextDetail.threadId) entries = []; + detail = nextDetail; + const timeline = (detail.timeline || detail.events || []).map(normalizeEntry); + entries = (detail.history?.kind === 'unavailable' ? [...new Map([...entries, ...timeline].map(entry => [entry.id, entry])).values()] : timeline).slice(-500); + lastSeq = detail.cursor; + if (detail.history?.kind === 'unavailable') retryDetail(); else { needsDetail = false; detailRetryDelay = 1000; } + showError(detail.history?.kind === 'unavailable' ? 'Codex 历史暂不可用;业务记录和实时更新仍可查看' : ''); + renderTaskHeader(); renderTimeline(); renderHistory(); renderDiagnostics(); return true; +} function renderHistory() { const target = $('history'); if (!detail) return; const fragment = document.createDocumentFragment(); for (const input of detail.inputs || []) fragment.append(node('article','history-message',input.text)); for (const output of detail.outputs || []) fragment.append(node('article','history-message',output.text)); if (!fragment.childNodes.length) fragment.append(node('div','sidebar-empty','还没有消息')); target.replaceChildren(fragment); } function renderDiagnostics() { const target = $('diagnostics'), rows = (detail?.diagnostics || []); if (!rows.length) { target.replaceChildren(node('div','sidebar-empty','没有诊断异常')); return; } const fragment = document.createDocumentFragment(); for (const row of rows) { const el = node('div','diagnostic'); el.append(node('time','',time(row.created_at) + ' · ' + row.scope),node('span','',row.message)); fragment.append(el); } target.replaceChildren(fragment); } -async function selectTask(id) { const generation = ++selectionGeneration; if (stream) stream.close(); selectedId = id; detail = null; entries = []; lastSeq = 0; stageDisclosure.clear(); renderWorkers(); renderTaskHeader(); $('event-count').textContent = '0 条事件'; $('last-event').textContent = '只保留可见执行说明'; try { if (!await loadDetail(id, generation) || generation !== selectionGeneration || selectedId !== id) return; } catch (error) { if (generation !== selectionGeneration || selectedId !== id) return; showError(error.message); return; } if (generation !== selectionGeneration || selectedId !== id) return; stream = new EventSource('/api/events?taskId=' + encodeURIComponent(id)); stream.onopen = () => { if (generation !== selectionGeneration || selectedId !== id) return; connection(true,'实时连接已建立'); $('stream-status').textContent = 'LIVE · 接收执行事件'; }; stream.onerror = () => { if (generation !== selectionGeneration || selectedId !== id) return; connection(false,'连接中断,正在重连'); $('stream-status').textContent = 'RECONNECTING · 自动续接'; }; stream.addEventListener('execution', message => { if (generation !== selectionGeneration || selectedId !== id) return; try { mergeEvent(JSON.parse(message.data)); } catch (error) { showError('事件读取失败:' + error.message); } }); } -async function refresh() { if (polling) return; polling = true; try { const response = await fetch('/api/snapshot'); if (!response.ok) throw new Error('读取看板失败 · HTTP ' + response.status); snapshot = await response.json(); showError(''); renderRuntime(); renderWorkers(); if (selectedId && !allTasks().some(task => task.id === selectedId)) { if (stream) stream.close(); stream = null; selectedId = null; detail = null; } if (!selectedId && allTasks().length) await selectTask(allTasks()[0].id); else renderTaskHeader(); if (!selectedId) connection(true,'看板已连接'); } catch (error) { connection(false,'连接失败'); showError(error.message); } finally { polling = false; } } +async function selectTask(id, recovering = false) { + const generation = ++selectionGeneration; + if (stream) stream.close(); stream = null; + selectedId = id; detailLoading = true; + if (!recovering) { detail = null; entries = []; lastSeq = 0; stageDisclosure.clear(); needsDetail = false; detailRetryDelay = 1000; } + renderWorkers(); renderTaskHeader(); + $('event-count').textContent = entries.length + ' 条事件'; $('last-event').textContent = '只保留可见执行说明'; + try { if (!await loadDetail(id, generation) || generation !== selectionGeneration || selectedId !== id) return; } + catch (error) { if (generation !== selectionGeneration || selectedId !== id) return; showError(error.message); retryDetail(); return; } + finally { if (generation === selectionGeneration) detailLoading = false; } + if (generation !== selectionGeneration || selectedId !== id) return; + stream = new EventSource('/api/events?taskId=' + encodeURIComponent(id) + '&after=' + encodeURIComponent(lastSeq) + '&threadId=' + encodeURIComponent(detail.threadId || '')); + stream.onopen = () => { if (generation !== selectionGeneration || selectedId !== id) return; connection(true,'实时连接已建立'); $('stream-status').textContent = 'LIVE · 接收执行事件'; }; + stream.onerror = () => { if (generation !== selectionGeneration || selectedId !== id) return; connection(false,'连接中断,正在重连'); $('stream-status').textContent = 'RECONNECTING · 自动续接'; }; + stream.addEventListener('reset', () => { if (generation !== selectionGeneration || selectedId !== id) return; if (stream) stream.close(); stream = null; if (!needsDetail) retryDetail(); }); + stream.addEventListener('execution', message => { if (generation !== selectionGeneration || selectedId !== id) return; try { mergeEvent(JSON.parse(message.data)); } catch (error) { showError('事件读取失败:' + error.message); } }); +} +async function refresh() { + if (polling) return; polling = true; + try { + const response = await fetch('/api/snapshot'); if (!response.ok) throw new Error('读取看板失败 · HTTP ' + response.status); + snapshot = await response.json(); + showError(detail?.history?.kind === 'unavailable' ? 'Codex 历史暂不可用;业务记录和实时更新仍可查看' : ''); renderRuntime(); renderWorkers(); + if (selectedId && !allTasks().some(task => task.id === selectedId)) { if (stream) stream.close(); stream = null; selectedId = null; detail = null; entries = []; needsDetail = false; ++selectionGeneration; detailLoading = false; } + if (!selectedId && allTasks().length) await selectTask(allTasks()[0].id); + else if (selectedId && !detailLoading && ((needsDetail && Date.now() >= detailRetryAt) || (detail && allTasks().find(task => task.id === selectedId)?.threadId !== detail.threadId))) await selectTask(selectedId, true); + else renderTaskHeader(); + if (!selectedId) connection(true,'看板已连接'); + } catch (error) { connection(false,'连接失败'); showError(error.message); } + finally { polling = false; } +} $('search').addEventListener('input', renderWorkers); $('status-filter').addEventListener('change', renderWorkers); $('role-filter').addEventListener('change', renderWorkers); for (const button of document.querySelectorAll('[data-filter]')) button.addEventListener('click', () => { filter = button.dataset.filter; for (const option of document.querySelectorAll('[data-filter]')) option.classList.toggle('active', option === button); renderTimeline(); }); for (const name of ['live','history','diagnostics']) $('tab-' + name).addEventListener('click', () => { for (const view of ['live','history','diagnostics']) { $('tab-' + view).classList.toggle('active', view === name); $(view === 'live' ? 'output' : view).hidden = view !== name; } $('live-toolbar').hidden = name !== 'live'; }); diff --git a/src/dashboard-reader.ts b/src/dashboard-reader.ts index 540e3ed..6a82180 100644 --- a/src/dashboard-reader.ts +++ b/src/dashboard-reader.ts @@ -1,7 +1,8 @@ +import { LiveExecution, type ExecutionObservation } from './live-execution.js'; import { intakeRouteSchema, presentTaskSource, type InputProvenance, type ReportEscalation, type ReportRouting, type ReportSource, type Task, type TaskResource } from './domain.js'; import { ProjectLedger, type ProjectState } from './project.js'; import { Store, type Inbox } from './store.js'; -import type { StoredExecutionEvent } from './execution-events.js'; +import type { VisibleExecution } from './execution-events.js'; import { z } from 'zod'; export const visibleTextLimit = 32_768; @@ -38,16 +39,17 @@ export interface DashboardReport { } export interface DashboardExecutionEvent { + key: string; seq: number; taskId: string; threadId: string; turnId: string | null; itemId: string | null; - kind: Exclude; + kind: Exclude; type: DashboardEventType; label: string; text: string; - createdAt: number; + createdAt: number | null; } export const dashboardEventTypes = ['INPUT', 'PHASE', 'NARRATIVE', 'TOOL_CALL', 'WAITING', 'BLOCKER', 'OUTPUT', 'EVIDENCE', 'RETRY'] as const; @@ -63,7 +65,7 @@ export interface DashboardTimelineEntry { type: DashboardEventType; label: string; text: string; - createdAt: number; + createdAt: number | null; phase: string | null; } @@ -103,10 +105,13 @@ export interface ProjectDashboard { } export interface DashboardTaskDetail { + threadId: string | null; task: ProjectTaskCard | null; projectId: string | null; inputs: ReturnType; outputs: ReturnType; + cursor: string; + history: ExecutionObservation['history']; events: DashboardExecutionEvent[]; timeline: DashboardTimelineEntry[]; reports: DashboardReport[]; @@ -131,7 +136,7 @@ function reportFromInput(input: Inbox): DashboardReport | null { return report; } -function eventType(event: Pick): DashboardEventType { +function eventType(event: Pick): DashboardEventType { if (event.kind === 'agent_delta') return 'NARRATIVE'; if (event.kind === 'agent_message') return event.label === '最终回答' ? 'OUTPUT' : 'NARRATIVE'; if (event.kind === 'tool_start' || event.kind === 'tool_end') return 'TOOL_CALL'; @@ -140,7 +145,7 @@ function eventType(event: Pick) return 'PHASE'; } -function dashboardEvent(event: StoredExecutionEvent): DashboardExecutionEvent | null { +function dashboardEvent(event: VisibleExecution): DashboardExecutionEvent | null { if (event.kind === 'tool_output') return null; let text = event.text; if (event.kind === 'tool_start' || event.kind === 'tool_end') { @@ -151,37 +156,19 @@ function dashboardEvent(event: StoredExecutionEvent): DashboardExecutionEvent | return { ...event, type: eventType(event), text: boundedDashboardText(text) } as DashboardExecutionEvent; } -function coalesce(events: StoredExecutionEvent[]): DashboardExecutionEvent[] { - const result: DashboardExecutionEvent[] = []; - for (const raw of events) { - const event = dashboardEvent(raw); - if (!event) continue; - const previous = result.at(-1); - if (previous && previous.kind === 'agent_delta' && event.kind === 'agent_delta' - && previous.threadId === event.threadId && previous.turnId === event.turnId && previous.itemId === event.itemId) { - previous.text = boundedDashboardText(previous.text.replace(/\n…$/, '') + event.text); - previous.seq = event.seq; - previous.createdAt = event.createdAt; - continue; - } - result.push(event); - } - return result; -} - type DashboardOwnership = { projectId: string; role: ProjectTaskCard['role'] }; interface DashboardContext { tasks: Task[]; states: Map; ownership: Map; reportsByTask: Map; - latestActivity: Map; + latestActivity: Map; } const membershipMetadataSchema = z.strictObject({ projectId: z.string(), role: z.enum(['coordinator', 'worker']) }); export class ProjectDashboardReader { readonly ledger: ProjectLedger; - constructor(private readonly store: Store) { this.ledger = new ProjectLedger(store); } + constructor(private readonly store: Store, private readonly execution = new LiveExecution()) { this.ledger = new ProjectLedger(store); } private route(taskId: string): { projectId: string; coordinatorTaskId: string } | null { const raw = this.store.meta(`project-intake-route:${taskId}`); @@ -228,7 +215,7 @@ export class ProjectDashboardReader { const list = reportsByTask.get(input.provenance.taskId) ?? []; list.push(report); reportsByTask.set(input.provenance.taskId, list); } - return { tasks, states, ownership, reportsByTask, latestActivity: this.store.latestVisibleExecutionEvents(tasks.map(task => task.id)) }; + return { tasks, states, ownership, reportsByTask, latestActivity: new Map(tasks.flatMap(task => { const event = this.execution.current(task.id, task.threadId).at(-1); return event ? [[task.id, event]] : []; })) }; } private reportsForTask(taskId: string, context: DashboardContext): DashboardReport[] { @@ -275,34 +262,12 @@ export class ProjectDashboardReader { project(projectId: string): ProjectDashboard | null { return this.projects().find(project => project.id === projectId) ?? null; } - private allEvents(taskId: string): StoredExecutionEvent[] { - const events: StoredExecutionEvent[] = []; - let after = 0; - while (true) { - const batch = this.store.visibleExecutionEvents(taskId, after); - if (!batch.length) return events; - events.push(...batch); - after = batch.at(-1)!.seq; - if (batch.length < 500) return events; - } - } - - events(taskId: string, after = 0, limit = 500): { cursor: number; events: DashboardExecutionEvent[] } { - const raw = this.store.visibleExecutionEvents(taskId, after, limit); - const cursor = raw.at(-1)?.seq ?? after; - const events = raw.flatMap(event => { - const visible = dashboardEvent(event); - return visible ? [visible] : []; - }); - return { cursor, events }; - } - - timeline(taskId: string, limit = 500): DashboardTimelineEntry[] { + timeline(taskId: string, events: VisibleExecution[] = [], limit = 500): DashboardTimelineEntry[] { const task = this.store.tasks().find(candidate => candidate.id === taskId); if (!task) return []; - const detailEvents: DashboardTimelineEntry[] = this.allEvents(taskId).flatMap(raw => { + const detailEvents: DashboardTimelineEntry[] = events.flatMap(raw => { const event = dashboardEvent(raw); - return event ? [{ ...event, id: 'event:' + event.seq, phase: null }] : []; + return event ? [{ ...event, id: 'event:' + event.key, phase: null }] : []; }); const inputs = this.store.inputs(taskId).map((input, index): DashboardTimelineEntry => ({ id: 'input:' + input.id, seq: -(index + 1), taskId, threadId: null, turnId: null, itemId: null, @@ -324,21 +289,25 @@ export class ProjectDashboardReader { type: request.kind === 'question' ? 'WAITING' : 'BLOCKER', label: request.kind === 'question' ? '等待回答' : '等待审批', text: boundedDashboardText(request.prompt), createdAt: task.updatedAt, phase: null, })); return [...inputs, ...reports, ...waiting, ...detailEvents] - .sort((a, b) => a.createdAt - b.createdAt || a.seq - b.seq) + .sort((a, b) => (a.createdAt ?? Infinity) - (b.createdAt ?? Infinity) || a.seq - b.seq) .slice(-limit); } - taskDetail(taskId: string, tasks = this.store.tasks()): DashboardTaskDetail | null { + async taskDetail(taskId: string, tasks = this.store.tasks()): Promise { const task = tasks.find(candidate => candidate.id === taskId); if (!task) return null; + const observation: ExecutionObservation = task.threadId ? await this.execution.read(taskId, task.threadId) : { cursor: this.execution.cursor, canonicalTurnIds: [], history: { kind: 'available' }, entries: [] }; + const current = this.store.tasks().find(candidate => candidate.id === taskId); + if (!current) return null; + if (current.threadId !== task.threadId) return this.taskDetail(taskId); const context = this.context(tasks); const ownership = context.ownership.get(taskId); const project = ownership ? context.states.get(ownership.projectId) : null; const reports = this.reportsForTask(taskId, context); return { task: ownership && project ? this.card(task, project.id, ownership.role, context, project.members.find(member => member.taskId === taskId)?.issueId ?? null) : null, - projectId: ownership?.projectId ?? null, - inputs: this.store.inputs(taskId), outputs: this.store.outputs(taskId), events: coalesce(this.allEvents(taskId)), timeline: this.timeline(taskId), reports, + threadId: task.threadId, projectId: ownership?.projectId ?? null, + inputs: this.store.inputs(taskId), outputs: this.store.outputs(taskId), cursor: observation.cursor, history: observation.history, events: observation.entries.flatMap(event => { const visible = dashboardEvent(event); return visible ? [visible] : []; }), timeline: this.timeline(taskId, observation.entries), reports, diagnostics: this.store.diagnostics().filter(row => row.scope === taskId || row.scope.endsWith(':' + taskId)), }; } diff --git a/src/dashboard.ts b/src/dashboard.ts index bc303b2..53cf964 100644 --- a/src/dashboard.ts +++ b/src/dashboard.ts @@ -1,3 +1,4 @@ +import { LiveExecution } from './live-execution.js'; import { createServer, type ServerResponse } from 'node:http'; import { existsSync } from 'node:fs'; import { join } from 'node:path'; @@ -8,17 +9,17 @@ import { boundedDashboardText, dashboardResources, ProjectDashboardReader } from import { errorMessage, presentTaskSource } from './domain.js'; import { dashboardHtml, dashboardCss, dashboardScript } from './dashboard-page.js'; -const cursorSchema = z.coerce.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER); const heartbeatSchema = z.object({ pid: z.number(), lastSeenAt: z.number(), intervalMs: z.number(), status: z.enum(['running', 'stopped']) }); -export async function startDashboard(options: { dataDir: string; port: number; host?: '127.0.0.1' | '0.0.0.0' }) { +export async function startDashboard(options: { execution?: LiveExecution; dataDir: string; port: number; host?: '127.0.0.1' | '0.0.0.0' }) { + const execution = options.execution ?? new LiveExecution(); const listenHost = options.host ?? '0.0.0.0'; const networkAddresses = () => Object.values(networkInterfaces()).flatMap(addresses => (addresses ?? []).filter(address => address.family === 'IPv4' && !address.internal).map(address => address.address)); let store: Store | null = null; const clients = new Set(); let dashboardReader: ProjectDashboardReader | null = null; - const readerFor = (db: Store) => dashboardReader ??= new ProjectDashboardReader(db); - let readModelCache: { version: number; expiresAt: number; tasks: ReturnType; projects: ReturnType; diagnostics: ReturnType } | null = null; + const readerFor = (db: Store) => dashboardReader ??= new ProjectDashboardReader(db, execution); + let readModelCache: { version: number; cursor: string; expiresAt: number; tasks: ReturnType; projects: ReturnType; diagnostics: ReturnType } | null = null; const readStore = () => { if (!store && existsSync(join(options.dataDir, 'tasks.sqlite'))) store = new Store(options.dataDir, { readOnly: true }); return store; @@ -34,11 +35,11 @@ export async function startDashboard(options: { dataDir: string; port: number; h const now = Date.now(); const db = readStore(); const version = db?.dataVersion() ?? 0; - if (readModelCache && readModelCache.version === version && readModelCache.expiresAt > now) return readModelCache; + if (readModelCache && readModelCache.version === version && readModelCache.cursor === execution.cursor && readModelCache.expiresAt > now) return readModelCache; const tasks = db?.tasks() ?? []; const projects = db ? readerFor(db).projects(tasks) : []; const diagnostics = db?.diagnostics() ?? []; - readModelCache = { version, expiresAt: now + 250, tasks, projects, diagnostics }; + readModelCache = { version, cursor: execution.cursor, expiresAt: now + 250, tasks, projects, diagnostics }; return readModelCache; }; const snapshot = () => { @@ -62,7 +63,7 @@ export async function startDashboard(options: { dataDir: string; port: number; h }; return value; }; - const server = createServer((request, response) => { + const server = createServer(async (request, response) => { response.setHeader('cache-control', 'no-store'); response.setHeader('x-content-type-options', 'nosniff'); response.setHeader('referrer-policy', 'no-referrer'); @@ -101,47 +102,66 @@ export async function startDashboard(options: { dataDir: string; port: number; h const db = readStore(); const tasks = db?.tasks() ?? []; if (!db || !tasks.some(task => task.id === taskId)) { json(response, 404, { error: '任务不存在' }); return; } - const detail = readerFor(db).taskDetail(taskId, tasks); + const detail = await readerFor(db).taskDetail(taskId, tasks); + if (!detail) { json(response, 404, { error: '任务不存在' }); return; } json(response, 200, { inputs: db.inputs(taskId).map(input => ({ id: input.id, text: boundedDashboardText(input.message.text), createdAt: input.message.createdAt, origin: input.origin, status: input.status, error: input.error ? boundedDashboardText(input.error) : null, attachments: input.message.attachments.map(attachment => ({ name: attachment.name, kind: attachment.kind })) })), outputs: db.outputs(taskId).map(output => ({ ...output, text: boundedDashboardText(output.text), error: output.error ? boundedDashboardText(output.error) : null })), projectId: detail?.projectId ?? null, task: detail?.task ?? null, - events: detail?.events ?? [], executionEvents: detail?.events ?? [], timeline: detail?.timeline ?? [], reports: detail?.reports ?? [], diagnostics: detail?.diagnostics ?? [], + threadId: detail?.threadId ?? null, cursor: detail?.cursor ?? execution.cursor, history: detail?.history, timeline: detail?.timeline ?? [], reports: detail?.reports ?? [], diagnostics: detail?.diagnostics ?? [], }); return; } if (url.pathname !== '/api/events') { json(response, 404, { error: '未找到' }); return; } const taskId = url.searchParams.get('taskId'); - const after = cursorSchema.safeParse(request.headers['last-event-id'] ?? url.searchParams.get('after') ?? 0); - if (!taskId || !after.success) { json(response, 400, { error: '无效的任务或事件游标' }); return; } + const after = request.headers['last-event-id'] ?? url.searchParams.get('after') ?? execution.cursor; + if (!taskId || typeof after !== 'string' || !/^[^:]+:\d+$/.test(after)) { json(response, 400, { error: '无效的任务或事件游标' }); return; } const db = readStore(); if (!db?.tasks().some(task => task.id === taskId)) { json(response, 404, { error: '任务不存在' }); return; } response.writeHead(200, { 'content-type': 'text/event-stream; charset=utf-8', connection: 'keep-alive', 'x-accel-buffering': 'no' }); response.write('retry: 1000\n\n'); clients.add(response); - let cursor = after.data; + let cursor = after; + const threadId = url.searchParams.has('threadId') ? url.searchParams.get('threadId') || null : db.task(taskId).threadId; let blocked = false; let lastKeepAlive = Date.now(); + let closed = false; + const finish = () => { + if (closed) return; + closed = true; + clearInterval(timer); unsubscribe(); clients.delete(response); + }; + const end = (event: string) => { + finish(); + if (response.destroyed || response.writableEnded) return; + response.end(event); + }; response.on('drain', () => { blocked = false; }); const send = () => { - if (blocked || response.destroyed) return; + if (closed || response.destroyed || response.writableEnded) { finish(); return; } + if (blocked) { if (execution.since(cursor) === null || response.writableLength > 262144) end('event: reset\ndata: {}\n\n'); return; } try { - const page = reader.events(taskId, cursor); - for (const event of page.events) { - cursor = event.seq; - if (!response.write('id: ' + event.seq + '\nevent: execution\ndata: ' + JSON.stringify(event) + '\n\n')) { blocked = true; break; } + const changes = execution.since(cursor); + const task = db.tasks().find(candidate => candidate.id === taskId); + if (changes === null || !task || task.threadId !== threadId) { + end('event: reset\ndata: {}\n\n'); return; + } + for (const change of changes) { + cursor = change.cursor; + if (change.event.taskId !== taskId || change.event.threadId !== threadId) continue; + if (!response.write('id: ' + cursor + '\nevent: execution\ndata: ' + JSON.stringify(change.event) + '\n\n')) { blocked = true; break; } } - if (!blocked) cursor = page.cursor; if (Date.now() - lastKeepAlive > 10_000 && !blocked) { lastKeepAlive = Date.now(); blocked = !response.write(': heartbeat\n\n'); } } catch (error) { - response.write('event: stream-error\ndata: ' + JSON.stringify({ error: errorMessage(error) }) + '\n\n'); - response.end(); + end('event: stream-error\ndata: ' + JSON.stringify({ error: errorMessage(error) }) + '\n\n'); } }; - const reader = new ProjectDashboardReader(db); + const unsubscribe = execution.subscribe(() => send()); const timer = setInterval(send, 1_000); - response.on('close', () => { clearInterval(timer); clients.delete(response); }); + response.on('close', finish); + response.on('finish', finish); + response.on('error', () => { finish(); response.destroy(); }); send(); } catch (error) { json(response, 500, { error: errorMessage(error) }); } }); diff --git a/src/execution-events.ts b/src/execution-events.ts index 6baeb5c..c392fc2 100644 --- a/src/execution-events.ts +++ b/src/execution-events.ts @@ -2,12 +2,16 @@ import { z } from 'zod'; export const executionEventSchema = z.object({ threadId: z.string(), turnId: z.string().nullable(), itemId: z.string().nullable(), - kind: z.enum(['agent_delta', 'agent_message', 'tool_start', 'tool_output', 'tool_end', 'turn_start', 'turn_end', 'turn_status', 'error']), + kind: z.enum(['agent_start', 'agent_delta', 'agent_message', 'tool_start', 'tool_output', 'tool_end', 'turn_start', 'turn_end', 'turn_status', 'error']), label: z.string(), text: z.string(), createdAt: z.number(), }); export type ExecutionEvent = z.infer; -export const storedExecutionEventSchema = executionEventSchema.extend({ seq: z.number(), taskId: z.string() }); -export type StoredExecutionEvent = z.infer; +export type VisibleExecution = Omit & { key: string; seq: number; taskId: string; createdAt: number | null }; +export type ExecutionHistory = Omit[]; +export function executionKey(event: Pick): string { + const category = event.kind.startsWith('agent_') ? 'agent' : event.kind.startsWith('tool_') ? 'tool' : event.kind.startsWith('turn_') ? 'turn' : event.kind; + return JSON.stringify([event.threadId, event.turnId, event.itemId, category]); +} const scopeSchema = z.object({ threadId: z.string(), turnId: z.string() }); const deltaSchema = scopeSchema.extend({ itemId: z.string(), delta: z.string() }); @@ -53,8 +57,7 @@ export function executionEvent(method: string, params: unknown): ExecutionEvent const { threadId, turnId, item } = parsed.data; const scope = { ...base, threadId, turnId, itemId: item.id }; if (item.type === 'agentMessage') { - if (method !== 'item/completed') return null; - return { ...scope, kind: 'agent_message', label: item.phase === 'final_answer' ? '最终回答' : 'Agent', text: item.text }; + return { ...scope, kind: method === 'item/started' ? 'agent_start' : 'agent_message', label: item.phase === 'final_answer' ? '最终回答' : 'Agent', text: item.text }; } const kind = method === 'item/started' ? 'tool_start' : 'tool_end'; switch (item.type) { diff --git a/src/feishu-document-conversation.ts b/src/feishu-document-conversation.ts index 1da4bab..5493ad0 100644 --- a/src/feishu-document-conversation.ts +++ b/src/feishu-document-conversation.ts @@ -1,13 +1,13 @@ import { z } from 'zod'; import type { Task } from './domain.js'; -import type { StoredExecutionEvent } from './execution-events.js'; +import type { VisibleExecution } from './execution-events.js'; import { finished, progressStatusSchema, taskProgressStatus, type ProgressStatus } from './feishu-progress-view.js'; import type { Inbox } from './store.js'; import { formatFeishuTime } from './feishu-time.js'; export const conversationMessageSchema = z.strictObject({ key: z.string(), role: z.enum(['user', 'assistant', 'cli']), text: z.string(), - createdAt: z.number(), sequence: z.number(), completed: z.boolean(), + createdAt: z.number().nullable(), sequence: z.number(), completed: z.boolean(), }); export type ConversationMessage = z.infer; export const documentConversationSchema = z.strictObject({ @@ -17,7 +17,7 @@ export const documentConversationSchema = z.strictObject({ export type DocumentConversation = z.infer; const normalize = (text: string) => text.replace(/\r\n?/g, '\n'); -export function projectDocumentConversation(task: Task, inputs: Inbox[], events: Iterable): DocumentConversation { +export function projectDocumentConversation(task: Task, inputs: Inbox[], events: Iterable): DocumentConversation { const messages = new Map(); const addUser = (message: Inbox['message'], sequence: number) => { const key = JSON.stringify(['user', message.id]); @@ -44,8 +44,8 @@ export function projectDocumentConversation(task: Task, inputs: Inbox[], events: messages.set(key, message); } if (event.kind === 'agent_message') { message.text = event.text; message.completed = true; } - else if (!message.completed) message.text += event.text; - lastExecutionEventAt = Math.max(lastExecutionEventAt ?? event.createdAt, event.createdAt); + else if (!message.completed) message.text = event.text; + if (event.createdAt !== null) lastExecutionEventAt = Math.max(lastExecutionEventAt ?? event.createdAt, event.createdAt); } if (event.threadId !== task.threadId || event.turnId !== task.turnId) continue; if (event.kind === 'turn_start') status = 'running'; @@ -64,7 +64,7 @@ export function projectDocumentConversation(task: Task, inputs: Inbox[], events: if (!finished(status) || snapshotStatus !== 'running') status = snapshotStatus; return documentConversationSchema.parse({ status, cursor, lastExecutionEventAt, messages: [...messages.values()].map(message => ({ ...message, text: normalize(message.text) })) - .sort((a, b) => a.createdAt - b.createdAt || (a.role === b.role ? a.sequence - b.sequence : a.role === 'assistant' ? 1 : -1)) }); + .sort((a, b) => (a.createdAt ?? Infinity) - (b.createdAt ?? Infinity) || (a.role === b.role ? a.sequence - b.sequence : a.role === 'assistant' ? 1 : -1)) }); } const labels: Record = { running: '执行中', waiting_input: '等待输入', waiting_approval: '等待审批', completed: '已完成', failed: '失败', paused: '已暂停' }; @@ -72,7 +72,7 @@ export function documentConversationText(view: DocumentConversation, maxLines: n const header = [`状态:${labels[view.status]}`, '', `最近对话事件:${view.lastExecutionEventAt === null ? '尚无事件' : formatFeishuTime(view.lastExecutionEventAt)}`]; const messages = view.messages.filter(message => message.text).toReversed().map(message => ['', - `${message.role === 'cli' ? 'CLI 提交' : message.role === 'user' ? '用户' : '助手'} · ${formatFeishuTime(message.createdAt)}`, ...message.text.split('\n')]); + `${message.role === 'cli' ? 'CLI 提交' : message.role === 'user' ? '用户' : '助手'} · ${message.createdAt === null ? '时间未知' : formatFeishuTime(message.createdAt)}`, ...message.text.split('\n')]); const lines = [...header, ...messages.flat()]; if (lines.length <= maxLines) return lines.join('\n'); const marker = '[仅展示最近对话,较早内容已省略]'; diff --git a/src/feishu-document-progress.ts b/src/feishu-document-progress.ts index ad17d20..9b3ab71 100644 --- a/src/feishu-document-progress.ts +++ b/src/feishu-document-progress.ts @@ -1,59 +1,51 @@ +import { progressDiagnostic } from './feishu-progress-errors.js'; +import { LiveExecution } from './live-execution.js'; import { createHash, randomUUID } from 'node:crypto'; import { z } from 'zod'; import { documentBaseUrlSchema, documentMaxLinesSchema } from './config.js'; -import { errorMessage, type Task } from './domain.js'; -import { finished, progressViewSchema, taskProgressStatus } from './feishu-progress-view.js'; +import { type Task } from './domain.js'; +import { coversSource, sourceFrontier, sourceFrontierSchema, HistoryRetries, finished, progressViewSchema, taskProgressStatus } from './feishu-progress-view.js'; import { documentConversationText, projectDocumentConversation } from './feishu-document-conversation.js'; import { documentChunks } from './feishu-document-text.js'; import { DocumentRequestRejected, TaskGroupBindingChanged, TaskGroupUnavailable, type FeishuDocumentProgressPort } from './ports.js'; import { assertGroupBinding, retireTaskGroup } from './task-groups.js'; -import { documentSourcePositionSchema, type DocumentSourcePosition, type Store } from './store.js'; +import type { Store } from './store.js'; const id = z.string().min(1); -const writeSchema = z.strictObject({ clientToken: id, text: z.string(), sent: z.boolean(), chunks: z.array(z.string()).min(1).optional() }); -const partSchema = z.strictObject({ blockId: id, text: z.string() }); +const writeSchema = z.strictObject({ clientToken: id, text: z.string(), sent: z.boolean() }); +const partSchema = z.strictObject({ blockId: id, digest: z.string() }); +const hash = (text: string) => createHash('sha256').update(text).digest('hex'); const operationSchema = z.discriminatedUnion('kind', [ - z.strictObject({ kind: z.literal('replace'), ...partSchema.shape, clientToken: id, sent: z.boolean() }), + z.strictObject({ kind: z.literal('replace'), blockId: id, text: z.string(), clientToken: id, sent: z.boolean() }), z.strictObject({ kind: z.literal('append'), index: z.number().int().nonnegative(), text: z.string(), clientToken: id, sent: z.boolean() }), z.strictObject({ kind: z.literal('remove'), blockIds: z.array(id).min(1), clientToken: id, sent: z.boolean() }), ]); const documentFields = { documentId: id, documentUrl: z.url() }; const blockFields = { ...documentFields, blockId: id }; const deliverySchema = z.discriminatedUnion('kind', [ + z.strictObject({ kind: z.literal('disabled'), documentId: id.nullable(), documentUrl: z.url().nullable(), + reason: z.literal('Task group is unavailable or its binding changed'), apiCode: z.number().nullable() }), z.strictObject({ kind: z.literal('document'), title: id, pending: z.boolean() }), z.strictObject({ kind: z.literal('block'), ...documentFields, write: writeSchema }), z.strictObject({ kind: z.literal('access'), ...blockFields, pending: z.boolean() }), z.strictObject({ kind: z.literal('tab'), ...blockFields, pending: z.boolean() }), - z.strictObject({ kind: z.literal('ready'), ...blockFields, tabId: id, content: z.string(), write: writeSchema.nullable(), + z.strictObject({ kind: z.literal('ready'), ...blockFields, tabId: id, contentHash: z.string(), parts: z.array(partSchema).default([]), operation: operationSchema.nullable().default(null) }), ]); -const projectionRevisionSchema = documentSourcePositionSchema.extend({ - taskKey: z.string(), rendererVersion: z.number().int().nonnegative(), maxLines: documentMaxLinesSchema, -}); -type DocumentProjectionRevision = z.infer; -export const documentPublicationSchema = z.strictObject({ - version: z.literal(1), taskId: id, chatId: id, turnId: id, +const runtimePublicationSchema = z.strictObject({ + version: z.literal(2), taskId: id, chatId: id, threadId: id, turnId: id, view: progressViewSchema, text: z.string().default(''), delivery: deliverySchema, lastAttemptAt: z.number(), nextAttemptAt: z.number(), failures: z.number().int().nonnegative(), - projectionRevision: projectionRevisionSchema.nullable().default(null), + sourceFrontier: sourceFrontierSchema.default({}), + projectionRevision: z.string().nullable().default(null), }); -export type DocumentPublication = z.infer; -const discoverySchema = z.strictObject({ cursor: z.number().int().nonnegative(), excludedTurns: z.array(z.string()) }); -const discoveryKey = 'feishu-document-progress:discovery:v1'; +export const documentPublicationSchema = runtimePublicationSchema.omit({ view: true, text: true }); +type DocumentPublication = z.infer; +const discoverySchema = z.strictObject({ excludedTurns: z.array(z.string()) }); +const discoveryKey = 'feishu-document-progress:discovery:v2'; const turnKey = (taskId: string, turnId: string) => JSON.stringify([taskId, turnId]); const emptyView = () => progressViewSchema.parse({ status: 'running', cursor: 0, lastExecutionEventAt: null, assistant: [], tool: null }); const content = (record: DocumentPublication) => record.text; -function projectionRevision(task: Task, position: DocumentSourcePosition, maxLines: number): DocumentProjectionRevision { - const taskKey = createHash('sha256').update(JSON.stringify([ - task.threadId, task.turnId, taskProgressStatus(task), task.source.kind === 'manual' ? task.source.message : null, - ])).digest('hex'); - return { ...position, taskKey, rendererVersion: 1, maxLines }; -} -function sameRevision(previous: DocumentProjectionRevision | null, current: DocumentProjectionRevision): boolean { - return previous !== null && previous.eventSeq === current.eventSeq && previous.inputSeq === current.inputSeq - && previous.taskKey === current.taskKey && previous.rendererVersion === current.rendererVersion && previous.maxLines === current.maxLines; -} - export class FeishuDocumentProgressPublisher { private readonly inFlight = new Map>(); private readonly publications = new Map(); @@ -63,7 +55,12 @@ export class FeishuDocumentProgressPublisher { private readonly maxLines: number; private timer: ReturnType | undefined; private stopped = false; - constructor(private readonly options: { store: Store; transport: FeishuDocumentProgressPort; documentBaseUrl: string; intervalMs?: number; maxLines?: number; now?: () => number }) { + private discoveryWork: Promise | null = null; + private readonly unsubscribe: () => void; + private readonly observed = new Map(); + private readonly historyRetries = new HistoryRetries(); + constructor(private readonly options: { store: Store; execution: LiveExecution; transport: FeishuDocumentProgressPort; documentBaseUrl: string; intervalMs?: number; maxLines?: number; now?: () => number }) { + this.unsubscribe = options.execution.subscribe(({ event }) => { if (!event) { this.observed.clear(); this.publications.clear(); } }); this.now = options.now ?? Date.now; this.intervalMs = Math.max(3000, options.intervalMs ?? 3000); this.baseUrl = documentBaseUrlSchema.parse(options.documentBaseUrl); @@ -71,99 +68,171 @@ export class FeishuDocumentProgressPublisher { } start(): void { if (this.timer || this.stopped) return; - this.discoverSafely(); + void this.discoverSafely(); this.timer = setInterval(() => { void this.poll().catch(error => this.report(error)); }, this.intervalMs); this.timer.unref(); } async poll(): Promise { - if (this.stopped || !this.discoverSafely()) return; + if (this.stopped) return; const work: Promise[] = []; + for (const taskId of this.options.store.documentProgressTaskIds()) { + const record = this.load(taskId); + if (!record) continue; + const task = this.options.store.task(taskId); + if (task.chatId !== record.chatId || task.threadId === null) { + if (record.delivery.kind === 'block' || (record.delivery.kind === 'ready' && record.delivery.operation)) this.disable(record); + this.publications.delete(taskId); this.observed.delete(taskId); continue; + } + if (task.threadId !== record.threadId) { + if (record.delivery.kind === 'ready' && record.delivery.operation) { record.delivery.operation = null; this.save(record); } + else if (record.delivery.kind === 'block') this.disable(record); + continue; + } + if (this.inFlight.has(taskId) || record.nextAttemptAt > this.now()) continue; + if (this.inFlight.size >= 2) break; + if ((record.delivery.kind === 'block' && record.delivery.write.sent) || (record.delivery.kind === 'ready' && record.delivery.operation?.sent)) work.push(this.dispatch(record)); + } + await Promise.all(work); + if (this.stopped) return; + const discovered = await this.discoverSafely(); + if (!discovered || this.stopped) { await Promise.all(work); return; } const taskIds = new Set(this.options.store.documentProgressTaskIds()); for (const taskId of this.publications.keys()) if (!taskIds.has(taskId)) this.publications.delete(taskId); const records = [...taskIds].flatMap(taskId => { const record = this.load(taskId); return record ? [record] : []; }); for (const record of records.sort((a, b) => a.lastAttemptAt - b.lastAttemptAt)) { - if (this.options.store.task(record.taskId).chatId !== record.chatId) continue; + if (this.options.store.task(record.taskId).chatId !== record.chatId) { + if (record.delivery.kind === 'block' || (record.delivery.kind === 'ready' && record.delivery.operation)) this.disable(record); + this.publications.delete(record.taskId); continue; + } + if (record.delivery.kind === 'disabled') continue; + const pending = record.delivery.kind === 'block' || (record.delivery.kind === 'ready' && record.delivery.operation !== null); + if (!pending && (!this.publications.has(record.taskId) || !this.observed.has(record.taskId))) continue; if (this.inFlight.size >= 2) break; if (this.inFlight.has(record.taskId) || record.nextAttemptAt > this.now()) continue; const delivery = record.delivery; if (delivery.kind === 'document' && delivery.pending) continue; - if (delivery.kind === 'ready' && !delivery.write && delivery.content === content(record)) continue; - const request = this.deliver(structuredClone(record)).catch(error => this.report(error)).finally(() => this.inFlight.delete(record.taskId)); - this.inFlight.set(record.taskId, request); - work.push(request); + if (delivery.kind === 'ready' && !delivery.operation && delivery.contentHash === record.projectionRevision) continue; + work.push(this.dispatch(record)); } await Promise.all(work); + let bytes = 0; + for (const [taskId, cached] of this.publications) { + const record = cached.record; + bytes += Buffer.byteLength(record.text); + if ((record.delivery.kind === 'ready' && !record.delivery.operation && record.delivery.contentHash === record.projectionRevision) + || bytes > 2_000_000 || this.publications.size > 64) this.publications.delete(taskId); + } + } + private dispatch(record: DocumentPublication): Promise { + const request = this.deliver(structuredClone(record)).catch(error => this.report(error, record.taskId, record.delivery.kind)) + .finally(() => this.inFlight.delete(record.taskId)); + this.inFlight.set(record.taskId, request); + return request; } async close(): Promise { this.stopped = true; + this.unsubscribe(); if (this.timer) clearInterval(this.timer); + await this.discoveryWork; await Promise.allSettled(this.inFlight.values()); this.publications.clear(); } - private report(error: unknown): void { this.options.store.diagnostic('feishu-document-progress', errorMessage(error)); } + private report(error: unknown, taskId?: string, operation = 'discovery', retry: 'pending' | 'disabled' = 'pending'): void { + this.options.store.diagnostic(taskId ?? 'feishu-document-progress', progressDiagnostic(error, `document.${operation}`, retry)); + } private load(taskId: string): DocumentPublication | null { const payload = this.options.store.documentProgressPayload(taskId); if (payload === null) { this.publications.delete(taskId); return null; } let cached = this.publications.get(taskId); if (cached?.payload !== payload) { - cached = { payload, record: documentPublicationSchema.parse(JSON.parse(payload)) }; - this.publications.set(taskId, cached); + cached = { payload, record: { ...documentPublicationSchema.parse(JSON.parse(payload)), view: emptyView(), text: cached?.record.text ?? '' } }; + if (cached.record.text) this.publications.set(taskId, cached); } return cached.record; } - private save(record: DocumentPublication): void { this.options.store.saveDocumentProgressRecord(record.taskId, record); } + private save(record: DocumentPublication): void { + const { view: _view, text: _text, ...receipt } = record; + this.options.store.saveDocumentProgressRecord(record.taskId, receipt); + this.publications.set(record.taskId, { payload: JSON.stringify(receipt), record }); + } private saveDelivery(record: DocumentPublication): void { const latest = this.load(record.taskId); if (!latest) throw new Error('Document publication disappeared'); - if (latest.chatId !== record.chatId) throw new TaskGroupBindingChanged(); + if (latest.chatId !== record.chatId || this.options.store.task(record.taskId).threadId !== record.threadId) throw new TaskGroupBindingChanged(); assertGroupBinding(this.options.store, record.taskId, record.chatId); this.save({ ...latest, delivery: record.delivery, lastAttemptAt: record.lastAttemptAt, nextAttemptAt: record.nextAttemptAt, failures: record.failures }); } - private discoverSafely(): boolean { - try { this.discover(); return true; } - catch (error) { this.report(error); return false; } + private discoverSafely(): Promise { + return this.discoveryWork ??= this.discover().then(() => true, error => { this.report(error); return false; }) + .finally(() => { this.discoveryWork = null; }); } - private discover(): void { + private async discover(): Promise { const store = this.options.store; - store.transaction(() => { - const tasks = store.tasks(); - const raw = store.meta(discoveryKey); - const discovery = raw === null ? { cursor: store.latestExecutionSequence(), excludedTurns: tasks.flatMap(task => - task.turnId && finished(taskProgressStatus(task)) && !this.load(task.id) ? [turnKey(task.id, task.turnId)] : []) } - : discoverySchema.parse(JSON.parse(raw)); - const excluded = new Set(discovery.excludedTurns); - for (const task of tasks) { - if (!task.chatId || !task.turnId || excluded.has(turnKey(task.id, task.turnId))) continue; - let record = this.load(task.id); - if (record && record.chatId !== task.chatId) { - store.setMeta(`retired-document-progress:${task.id}:${record.chatId}`, JSON.stringify(record)); - record = null; - } - if (!record) { - record = { version: 1, taskId: task.id, chatId: task.chatId, turnId: task.turnId, - view: emptyView(), text: '', projectionRevision: null, delivery: { kind: 'document', title: '执行进度', pending: false }, - lastAttemptAt: 0, nextAttemptAt: 0, failures: 0 }; - } - const revision = projectionRevision(task, store.documentConversationPosition(task.id), this.maxLines); - if (sameRevision(record.projectionRevision, revision)) continue; - record = structuredClone(record); - record.turnId = task.turnId; - const conversation = projectDocumentConversation(task, store.inputs(task.id), store.documentConversationEvents(task.id)); - record.view = { status: conversation.status, cursor: conversation.cursor, - lastExecutionEventAt: conversation.lastExecutionEventAt, assistant: [], tool: null, notice: null }; - record.text = documentConversationText(conversation, this.maxLines); - record.projectionRevision = revision; - this.save(record); + const tasks = store.tasks(); + const raw = store.meta(discoveryKey); + const discovery = raw === null ? { excludedTurns: tasks.flatMap(task => + task.turnId && finished(taskProgressStatus(task)) && !this.load(task.id) ? [turnKey(task.id, task.turnId)] : []) } + : discoverySchema.parse(JSON.parse(raw)); + if (raw === null) store.setMeta(discoveryKey, JSON.stringify(discovery)); + let reads = 0; + for (const task of tasks.sort((a, b) => (this.load(a.id)?.lastAttemptAt ?? 0) - (this.load(b.id)?.lastAttemptAt ?? 0))) { + if (!task.chatId || !task.threadId || !task.turnId || discovery.excludedTurns.includes(turnKey(task.id, task.turnId))) continue; + const inputSequence = store.inputSequence(task.id); + const signature = JSON.stringify([task.threadId, task.turnId, task.chatId, taskProgressStatus(task), task.source.kind === 'manual' ? task.source.message : null, inputSequence, this.options.execution.revision(task.id, task.threadId)]); + const existing = this.load(task.id); + if (existing?.delivery.kind === 'disabled' && existing.chatId === task.chatId && existing.threadId === task.threadId) continue; + const settled = existing?.delivery.kind === 'ready' && !existing.delivery.operation && existing.delivery.contentHash === existing.projectionRevision; + if (this.observed.get(task.id) === signature && (this.publications.get(task.id)?.record.text || settled)) continue; + if (!this.historyRetries.ready(task.id, signature, this.now())) continue; + if (reads >= 2 || (this.inFlight.size >= 2 && !this.inFlight.has(task.id))) continue; + reads++; + const observation = await this.options.execution.read(task.id, task.threadId); + if (this.stopped) return; + const current = store.task(task.id); + if (current.threadId !== task.threadId || current.turnId !== task.turnId || current.chatId !== task.chatId || taskProgressStatus(current) !== taskProgressStatus(task) + || JSON.stringify(current.source) !== JSON.stringify(task.source) || store.inputSequence(task.id) !== inputSequence) continue; + if (observation.history.kind === 'unavailable') { + this.historyRetries.failed(task.id, signature, this.now()); this.observed.delete(task.id); this.publications.delete(task.id); continue; + } + let record = this.load(task.id); + if (record && record.chatId !== task.chatId) { + const { view: _view, text: _text, ...receipt } = record; + const delivery = receipt.delivery; + if (delivery.kind === 'ready') { delivery.operation = null; } + if (delivery.kind !== 'block') store.setMeta(`retired-document-progress:${task.id}:${record.chatId}`, JSON.stringify(receipt)); + record = null; + } + if (record && record.threadId !== task.threadId && record.delivery.kind !== 'ready') record = null; + if (record && record.chatId === task.chatId && record.threadId === task.threadId && !coversSource(observation.entries, record.sourceFrontier)) { + this.historyRetries.failed(task.id, signature, this.now()); this.observed.delete(task.id); this.publications.delete(task.id); continue; } - discovery.cursor = store.latestExecutionSequence(); - store.setMeta(discoveryKey, JSON.stringify(discovery)); - }); + record = record ? structuredClone(record) : { version: 2, taskId: task.id, chatId: task.chatId, threadId: task.threadId, turnId: task.turnId, + view: emptyView(), text: '', sourceFrontier: {}, projectionRevision: null, delivery: { kind: 'document', title: '执行进度', pending: false }, + lastAttemptAt: 0, nextAttemptAt: 0, failures: 0 }; + record.threadId = task.threadId; record.turnId = task.turnId; record.sourceFrontier = sourceFrontier(observation.entries); + const conversation = projectDocumentConversation(task, store.inputs(task.id), observation.entries); + record.view = { status: conversation.status, cursor: conversation.cursor, lastExecutionEventAt: conversation.lastExecutionEventAt, assistant: [], tool: null, notice: null }; + record.text = documentConversationText(conversation, this.maxLines); + record.projectionRevision = hash(record.text); + this.save(record); + this.observed.set(task.id, signature); this.historyRetries.succeeded(task.id); + } + } + private disable(record: DocumentPublication, apiCode: number | null = null): void { + const latest = this.load(record.taskId); + if (!latest || latest.chatId !== record.chatId || latest.threadId !== record.threadId) return; + const delivery = latest.delivery; + this.save({ ...latest, text: '', view: emptyView(), delivery: { kind: 'disabled', + documentId: 'documentId' in delivery ? delivery.documentId : null, + documentUrl: 'documentUrl' in delivery ? delivery.documentUrl : null, + reason: 'Task group is unavailable or its binding changed', apiCode } }); + this.publications.delete(record.taskId); this.observed.delete(record.taskId); } private async deliver(record: DocumentPublication): Promise { if (this.options.store.task(record.taskId).chatId !== record.chatId) return; const transport = this.options.transport; - const guard = () => assertGroupBinding(this.options.store, record.taskId, record.chatId); + const guard = () => { assertGroupBinding(this.options.store, record.taskId, record.chatId); if (this.options.store.task(record.taskId).threadId !== record.threadId) throw new TaskGroupBindingChanged(); }; let rollback: DocumentPublication['delivery'] | undefined; record.lastAttemptAt = this.now(); record.nextAttemptAt = this.now() + this.intervalMs; @@ -171,6 +240,7 @@ export class FeishuDocumentProgressPublisher { try { const delivery = record.delivery; switch (delivery.kind) { + case 'disabled': return; case 'document': { if (delivery.pending) return; rollback = structuredClone(delivery); @@ -219,34 +289,23 @@ export class FeishuDocumentProgressPublisher { } if (!tabId) throw new Error('Document tab creation remains unknown'); record.delivery = { kind: 'ready', documentId: delivery.documentId, documentUrl: delivery.documentUrl, - blockId: delivery.blockId, tabId, content: '', write: null, parts: [], operation: null }; + blockId: delivery.blockId, tabId, contentHash: '', parts: [], operation: null }; break; } case 'ready': { - delivery.write ??= { clientToken: randomUUID(), text: content(record), chunks: documentChunks(content(record)), sent: false }; - const write = delivery.write; - if (!write.chunks) { - const input = { documentId: delivery.documentId, blockId: delivery.blockId }; - if (!write.sent || await transport.readBlock(input) !== write.text) { - rollback = structuredClone(delivery); write.sent = true; this.saveDelivery(record); - await transport.replaceContent({ ...input, ...write }, guard); - } - delivery.parts = [{ blockId: delivery.blockId, text: write.text }]; - delivery.content = write.text; delivery.write = null; break; - } - const chunks = write.chunks; - if (!delivery.parts.length) delivery.parts.push({ blockId: delivery.blockId, text: delivery.content }); + const chunks = this.observed.has(record.taskId) && record.text ? documentChunks(content(record)) : []; + if (!delivery.parts.length) delivery.parts.push({ blockId: delivery.blockId, digest: delivery.contentHash }); if (!delivery.operation) { - const changed = delivery.parts.findIndex((part, index) => index < chunks.length && part.text !== chunks[index]); + const changed = delivery.parts.findIndex((part, index) => index < chunks.length && part.digest !== hash(chunks[index]!)); const part = delivery.parts[changed], text = chunks[changed]; if (part && text !== undefined) delivery.operation = { kind: 'replace', blockId: part.blockId, text, clientToken: randomUUID(), sent: false }; - else if (delivery.parts.length < write.chunks.length) { - const next = write.chunks[delivery.parts.length]; + else if (delivery.parts.length < chunks.length) { + const next = chunks[delivery.parts.length]; if (next === undefined) throw new Error('Missing document chunk'); delivery.operation = { kind: 'append', index: delivery.parts.length - 1, text: next, clientToken: randomUUID(), sent: false }; - } else if (delivery.parts.length > write.chunks.length) { - delivery.operation = { kind: 'remove', blockIds: delivery.parts.slice(write.chunks.length).map(part => part.blockId), clientToken: randomUUID(), sent: false }; - } else { delivery.content = write.text; delivery.write = null; break; } + } else if (delivery.parts.length > chunks.length) { + delivery.operation = { kind: 'remove', blockIds: delivery.parts.slice(chunks.length).map(part => part.blockId), clientToken: randomUUID(), sent: false }; + } else { delivery.contentHash = hash(content(record)); break; } } const operation = delivery.operation; rollback = structuredClone(delivery); @@ -259,7 +318,7 @@ export class FeishuDocumentProgressPublisher { } const part = delivery.parts.find(part => part.blockId === operation.blockId); if (!part) throw new Error('Document block ownership disappeared'); - part.text = operation.text; + part.digest = hash(operation.text); break; } case 'append': { @@ -271,7 +330,7 @@ export class FeishuDocumentProgressPublisher { blockId = await transport.createBlock({ ...input, clientToken: operation.clientToken }, guard); } if (!blockId) throw new Error('Overflow block creation remains unknown'); - delivery.parts.push({ blockId, text: operation.text }); + delivery.parts.push({ blockId, digest: hash(operation.text) }); break; } case 'remove': { @@ -284,8 +343,8 @@ export class FeishuDocumentProgressPublisher { default: { const exhaustive: never = operation; return exhaustive; } } delivery.operation = null; - if (delivery.parts.length === chunks.length && delivery.parts.every((part, index) => part.text === chunks[index])) { - delivery.content = write.text; delivery.write = null; + if (delivery.parts.length === chunks.length && delivery.parts.every((part, index) => part.digest === hash(chunks[index]!))) { + delivery.contentHash = hash(content(record)); } break; } @@ -295,16 +354,17 @@ export class FeishuDocumentProgressPublisher { record.nextAttemptAt = this.now() + this.intervalMs; this.saveDelivery(record); } catch (error) { - if (error instanceof TaskGroupBindingChanged) return; + if (error instanceof TaskGroupBindingChanged) { this.disable(record); return; } if (error instanceof TaskGroupUnavailable) { retireTaskGroup(this.options.store, record.taskId, record.chatId); - this.report(`${record.taskId}: ${error.message}`); return; + this.disable(record, error.code); + this.report(error, record.taskId, record.delivery.kind, 'disabled'); return; } if (error instanceof DocumentRequestRejected && rollback) record.delivery = rollback; record.failures++; record.nextAttemptAt = this.now() + Math.max(this.intervalMs, Math.min(60_000, 3000 * 2 ** Math.min(record.failures - 1, 5))); this.saveDelivery(record); - this.report(`${record.taskId}: ${errorMessage(error)}`); + this.report(error, record.taskId, record.delivery.kind); } } } diff --git a/src/feishu-progress-errors.ts b/src/feishu-progress-errors.ts new file mode 100644 index 0000000..d17935e --- /dev/null +++ b/src/feishu-progress-errors.ts @@ -0,0 +1,24 @@ +import { FeishuApiError, TaskGroupBindingChanged, DocumentRequestRejected } from './ports.js'; + +export function progressFailure(error: unknown): string { + if (error instanceof FeishuApiError) return `Feishu API error ${error.code}`; + if (error instanceof TaskGroupBindingChanged) return 'Task group or Thread binding changed'; + const message = error instanceof Error ? error.message : String(error); + const code = /^(?:Feishu API error |Feishu document API )?(\d{6,9})\b/.exec(message)?.[1]; + if (code) return `Feishu API error ${code}`; + if (/permission|forbidden|denied|scope/i.test(message)) return 'Feishu progress permission denied'; + if (/timeout|timed out/i.test(message)) return 'Feishu progress request timed out'; + if (/unknown|ACK|acknowledge/i.test(message)) return 'Feishu progress delivery outcome is unknown; reconciliation pending'; + if (/network|disconnect|ECONN|fetch|socket/i.test(message)) return 'Feishu progress transport disconnected'; + if (/expired|expiry|retry window/i.test(message)) return 'Feishu progress safe delivery window expired'; + if (/sequence/i.test(message)) return 'Feishu progress operation sequence rejected or exhausted'; + if (/scan|lookup|readback/i.test(message)) return 'Feishu progress reconciliation read failed'; + if (error instanceof DocumentRequestRejected) return 'Feishu rejected the document operation'; + return 'Feishu progress operation failed'; +} + +export function progressDiagnostic(error: unknown, operation: string, retry: 'pending' | 'disabled'): string { + const reason = progressFailure(error); + const code = /^Feishu API error (\d+)$/.exec(reason)?.[1]; + return JSON.stringify({ operation, retry, reason, ...(code ? { apiCode: Number(code) } : {}) }); +} diff --git a/src/feishu-progress-view.ts b/src/feishu-progress-view.ts index d0095b2..c1fc305 100644 --- a/src/feishu-progress-view.ts +++ b/src/feishu-progress-view.ts @@ -1,6 +1,7 @@ import { z } from 'zod'; +import { createHash } from 'node:crypto'; import type { Task } from './domain.js'; -import type { StoredExecutionEvent } from './execution-events.js'; +import type { VisibleExecution } from './execution-events.js'; import { formatFeishuTime } from './feishu-time.js'; export const progressStatusSchema = z.enum(['running', 'waiting_input', 'waiting_approval', 'completed', 'failed', 'paused']); @@ -39,16 +40,16 @@ export function boundedText(text: string, bytes: number): string { const tail = text.slice(-low).replace(/^[\uDC00-\uDFFF]/, ''); return marker + tail; } -export function projectProgress(view: ProgressView, event: StoredExecutionEvent): void { +export function projectProgress(view: ProgressView, event: VisibleExecution): void { if (event.seq <= view.cursor) return; view.cursor = event.seq; - if (event.kind !== 'turn_status') view.lastExecutionEventAt = Math.max(view.lastExecutionEventAt ?? 0, event.createdAt); + if (event.kind !== 'turn_status' && event.createdAt !== null) view.lastExecutionEventAt = Math.max(view.lastExecutionEventAt ?? 0, event.createdAt); switch (event.kind) { - case 'agent_delta': case 'agent_message': { + case 'agent_start': case 'agent_delta': case 'agent_message': { view.notice = null; let item = view.assistant.find(item => item.id === event.itemId); if (!item) { item = { id: event.itemId, text: '' }; view.assistant.push(item); } - item.text = boundedText(event.kind === 'agent_message' ? event.text : item.text + event.text, 2000); + item.text = boundedText(event.text, 2000); view.assistant = view.assistant.slice(-3); break; } @@ -83,3 +84,45 @@ export function progressContent(view: ProgressView): string { ...view.assistant.map(item => item.text), ...(view.tool ? [`工具:${view.tool.label}`, view.tool.text] : [])].filter(Boolean).join('\n\n'); } + + +export const sourceFrontierSchema = z.record(z.string(), z.object({ + complete: z.boolean(), length: z.number().int().nonnegative(), digest: z.string(), createdAt: z.number().nullable(), +})); +export type SourceFrontier = z.infer; +const sourceDigest = (text: string) => createHash('sha256').update(text).digest('hex'); +export function sourceFrontier(entries: VisibleExecution[]): SourceFrontier { + return Object.fromEntries(entries.filter(event => !['error', 'turn_status', 'agent_start', 'tool_output'].includes(event.kind)).map(event => [event.key, { + complete: event.kind === 'agent_message' || event.kind === 'tool_end' || event.kind === 'turn_end', + length: event.text.length, digest: sourceDigest(event.text), createdAt: event.createdAt, + }])); +} +export function coversSource(entries: VisibleExecution[], frontier: SourceFrontier): boolean { + const current = new Map(entries.map(event => [event.key, event])); + return Object.entries(frontier).every(([key, previous]) => { + const event = current.get(key); + if (!event) return false; + const complete = event.kind === 'agent_message' || event.kind === 'tool_end' || event.kind === 'turn_end'; + if (previous.complete && !complete) return false; + if (sourceDigest(event.text) === previous.digest) return true; + if (event.kind === 'agent_start' || event.kind === 'agent_delta') { + return event.text.length >= previous.length && sourceDigest(event.text.slice(0, previous.length)) === previous.digest; + } + if (event.createdAt !== null && previous.createdAt !== null && event.createdAt > previous.createdAt) return true; + return !previous.complete && (complete || (event.text.length >= previous.length && sourceDigest(event.text.slice(0, previous.length)) === previous.digest)); + }); +} + +export class HistoryRetries { + private readonly tasks = new Map(); + ready(taskId: string, signature: string, now: number): boolean { + const retry = this.tasks.get(taskId); + return !retry || retry.signature !== signature || retry.nextAttemptAt <= now; + } + failed(taskId: string, signature: string, now: number): void { + const previous = this.tasks.get(taskId); + const failures = previous?.signature === signature ? previous.failures + 1 : 1; + this.tasks.set(taskId, { signature, failures, nextAttemptAt: now + Math.min(60_000, 3000 * 2 ** Math.min(failures - 1, 5)) }); + } + succeeded(taskId: string): void { this.tasks.delete(taskId); } +} diff --git a/src/feishu-progress.ts b/src/feishu-progress.ts index 746c61f..41cf55c 100644 --- a/src/feishu-progress.ts +++ b/src/feishu-progress.ts @@ -1,21 +1,23 @@ +import { progressFailure, progressDiagnostic } from './feishu-progress-errors.js'; +import { LiveExecution } from './live-execution.js'; import { createHash } from 'node:crypto'; import { z } from 'zod'; -import { errorMessage, type Task } from './domain.js'; +import { type Task } from './domain.js'; import { TaskGroupBindingChanged, TaskGroupUnavailable, type FeishuProgressPort } from './ports.js'; import { assertGroupBinding, retireTaskGroup } from './task-groups.js'; import type { Store } from './store.js'; -import { finished, progressContent, progressViewSchema, projectProgress, taskProgressStatus } from './feishu-progress-view.js'; +import { coversSource, sourceFrontier, sourceFrontierSchema, HistoryRetries, finished, progressContent, progressViewSchema, projectProgress, taskProgressStatus } from './feishu-progress-view.js'; const entityFields = { cardId: z.string().min(1), createdAt: z.number() }; const operationSchema = z.discriminatedUnion('kind', [ - z.object({ kind: z.literal('content'), sequence: z.number().int().positive(), uuid: z.string(), content: z.string() }), + z.object({ kind: z.literal('content'), sequence: z.number().int().positive(), uuid: z.string(), contentHash: z.string() }), z.object({ kind: z.literal('settings'), sequence: z.number().int().positive(), uuid: z.string(), enabled: z.boolean() }), ]); const publishedFields = { ...entityFields, messageId: z.string().min(1), streamingAt: z.number().nullable(), - content: z.string(), pending: operationSchema.nullable() }; -export const progressPublicationSchema = z.object({ - taskId: z.string(), turnId: z.string(), chatId: z.string(), uuid: z.string(), - sequence: z.number().int().nonnegative(), view: progressViewSchema, + contentHash: z.string(), pending: operationSchema.nullable() }; +const runtimePublicationSchema = z.object({ + taskId: z.string(), threadId: z.string(), turnId: z.string(), chatId: z.string(), uuid: z.string(), + sourceFrontier: sourceFrontierSchema.default({}), projectionHash: z.string().default(''), terminal: z.boolean().default(false), sequence: z.number().int().nonnegative(), view: progressViewSchema, lastAttemptAt: z.number(), nextAttemptAt: z.number(), failures: z.number().int().nonnegative(), delivery: z.discriminatedUnion('kind', [ z.object({ kind: z.literal('uncreated') }), @@ -23,12 +25,13 @@ export const progressPublicationSchema = z.object({ z.object({ kind: z.literal('sending'), ...entityFields, firstAttemptAt: z.number() }), z.object({ kind: z.literal('published'), ...publishedFields }), z.object({ kind: z.literal('closed'), ...publishedFields }), - z.object({ kind: z.literal('disabled'), cardId: z.string().nullable(), messageId: z.string().nullable(), reason: z.string() }), + z.object({ kind: z.literal('disabled'), cardId: z.string().nullable(), messageId: z.string().nullable(), reason: z.string(), apiCode: z.number().nullable().default(null) }), ]), }); -export type ProgressPublication = z.infer; -const discoverySchema = z.object({ cursor: z.number().int().nonnegative(), snapshots: z.record(z.string(), z.string()), excludedTurns: z.array(z.string()).default([]) }); -const discoveryKey = 'feishu-progress:discovery:v1'; +export const progressPublicationSchema = runtimePublicationSchema.omit({ view: true }); +type ProgressPublication = z.infer; +const discoverySchema = z.object({ snapshots: z.record(z.string(), z.string()), excludedTurns: z.array(z.string()) }); +const discoveryKey = 'feishu-progress:discovery:v2'; const hash = (text: string) => createHash('sha256').update(text).digest('hex'); const key = (record: Pick) => JSON.stringify([record.taskId, record.turnId]); const renewalMs = 8 * 60_000; @@ -42,22 +45,42 @@ export class FeishuProgressPublisher { private operationTimes: number[] = []; private timer: ReturnType | undefined; private stopped = false; - constructor(private readonly options: { store: Store; transport: FeishuProgressPort; intervalMs?: number; dashboardUrl?: string; now?: () => number }) { + private discoveryWork: Promise | null = null; + private readonly views = new Map(); + private readonly hydrated = new Set(); + private readonly observed = new Map(); + private readonly historyRetries = new HistoryRetries(); + private unsubscribe: (() => void) | undefined; + constructor(private readonly options: { store: Store; execution: LiveExecution; transport: FeishuProgressPort; intervalMs?: number; dashboardUrl?: string; now?: () => number }) { this.store = options.store; this.transport = options.transport; this.now = options.now ?? Date.now; + this.unsubscribe = options.execution.subscribe(({ event }) => { + if (!event) { this.observed.clear(); this.views.clear(); this.hydrated.clear(); return; } + if (this.store.meta(discoveryKey) === null) return; + const task = this.store.task(event.taskId); + if (!task.chatId || task.threadId !== event.threadId || !event.turnId || this.excluded(task.id, event.turnId)) return; + let record = this.load(task.id, event.turnId); + if (!record || record.chatId !== task.chatId || record.threadId !== task.threadId) record = this.newRecord(task, event.turnId); + this.observed.delete(task.id); + this.hydrated.delete(key(record)); + if (record.delivery.kind === 'uncreated') this.save(record); + }); } start(): void { if (this.timer || this.stopped) return; - this.discoverSafely(); + void this.discoverSafely(); this.timer = setInterval(() => { void this.poll().catch(error => this.report(error)); }, this.options.intervalMs ?? 1000); this.timer.unref(); } async poll(): Promise { - if (this.stopped || !this.discoverSafely()) return; + if (this.stopped) return; + const discovered = await this.discoverSafely(); + if (!discovered || this.stopped) return; const now = this.now(); this.operationTimes = this.operationTimes.filter(time => now - time < 1000); const work: Promise[] = []; - for (const record of this.store.activeProgressRecords().map(raw => progressPublicationSchema.parse(raw)).sort((a, b) => a.lastAttemptAt - b.lastAttemptAt)) { + for (const record of this.store.activeProgressRecords().map(raw => progressPublicationSchema.parse(raw)).flatMap(receipt => { const record = this.load(receipt.taskId, receipt.turnId); return record ? [record] : []; }).sort((a, b) => a.lastAttemptAt - b.lastAttemptAt)) { if (this.store.task(record.taskId).chatId !== record.chatId) { this.disable(record, '任务群已失效'); continue; } + if (!this.views.has(key(record)) || !this.hydrated.has(key(record))) continue; if (this.inFlight.size >= 4 || this.operationTimes.length >= 12) break; if (this.inFlight.has(key(record)) || record.nextAttemptAt > now || !this.needsWrite(record)) continue; this.operationTimes.push(now); @@ -68,85 +91,129 @@ export class FeishuProgressPublisher { work.push(request); } await Promise.all(work); + for (const receipt of this.store.activeProgressRecords().map(raw => progressPublicationSchema.parse(raw))) { + if (this.needsProjection(receipt)) continue; + this.views.delete(key(receipt)); this.hydrated.delete(key(receipt)); + } } async close(): Promise { this.stopped = true; + this.unsubscribe?.(); if (this.timer) clearInterval(this.timer); + await this.discoveryWork; await Promise.allSettled(this.inFlight.values()); } - private report(error: unknown): void { this.store.diagnostic('feishu-progress', errorMessage(error)); } - private discoverSafely(): boolean { - try { this.discover(); return true; } - catch (error) { this.report(error); return false; } + private report(error: unknown, taskId?: string, operation = 'discovery', retry: 'pending' | 'disabled' = 'pending'): void { + this.store.diagnostic(taskId ?? 'feishu-progress', progressDiagnostic(error, `card.${operation}`, retry)); + } + private discoverSafely(): Promise { + return this.discoveryWork ??= this.discover().then(() => true, error => { this.report(error); return false; }) + .finally(() => { this.discoveryWork = null; }); } private load(taskId: string, turnId: string): ProgressPublication | null { const raw = this.store.progressRecord(taskId, turnId); - return raw === null ? null : progressPublicationSchema.parse(raw); + if (raw === null) return null; + const receipt = progressPublicationSchema.parse(raw); + const view = this.views.get(key(receipt)) ?? progressViewSchema.parse({ status: 'running', cursor: 0, lastExecutionEventAt: null, assistant: [], tool: null }); + return { ...receipt, view }; } private save(record: ProgressPublication): void { - if (record.delivery.kind === 'closed' && record.delivery.content !== progressContent(record.view)) { + if (record.delivery.kind === 'closed' && record.delivery.contentHash !== record.projectionHash) { record.delivery = { ...record.delivery, kind: 'published', streamingAt: null }; } - this.store.saveProgressRecord(record.taskId, record.turnId, record, record.delivery.kind !== 'closed' && record.delivery.kind !== 'disabled'); + if (record.delivery.kind === 'closed' || record.delivery.kind === 'disabled') { this.views.delete(key(record)); this.hydrated.delete(key(record)); } + else if (this.hydrated.has(key(record))) { + this.views.set(key(record), record.view); + while (this.views.size > 128) { + const oldest = this.views.keys().next().value!; + this.views.delete(oldest); this.hydrated.delete(oldest); + } + } + this.store.saveProgressRecord(record.taskId, record.turnId, progressPublicationSchema.parse(record), record.delivery.kind !== 'closed' && record.delivery.kind !== 'disabled'); } private saveDelivery(record: ProgressPublication): void { const latest = this.load(record.taskId, record.turnId); if (!latest) throw new Error('Progress publication disappeared'); - if (latest.chatId !== record.chatId || this.store.task(record.taskId).chatId !== record.chatId) return; + if (latest.chatId !== record.chatId || this.store.task(record.taskId).chatId !== record.chatId || this.store.task(record.taskId).threadId !== record.threadId) return; this.save({ ...latest, delivery: record.delivery, sequence: record.sequence, lastAttemptAt: record.lastAttemptAt, nextAttemptAt: record.nextAttemptAt, failures: record.failures }); } private newRecord(task: Task, turnId: string): ProgressPublication { - return progressPublicationSchema.parse({ taskId: task.id, turnId, chatId: task.chatId, + return runtimePublicationSchema.parse({ taskId: task.id, threadId: task.threadId, turnId, chatId: task.chatId, uuid: hash(`feishu-progress:${key({ taskId: task.id, turnId })}:${task.chatId}`).slice(0, 32), sequence: 0, lastAttemptAt: 0, nextAttemptAt: 0, failures: 0, delivery: { kind: 'uncreated' }, view: { status: 'running', cursor: 0, lastExecutionEventAt: null, assistant: [], tool: null } }); } - private discover(): void { - this.store.transaction(() => { - const raw = this.store.meta(discoveryKey); - const initial = raw === null; - const tasks = new Map(this.store.tasks().map(task => [task.id, task])); - const discovery = initial ? { cursor: this.store.latestExecutionSequence(), snapshots: {}, - excludedTurns: [...tasks.values()].flatMap(task => task.turnId && finished(taskProgressStatus(task)) && !this.load(task.id, task.turnId) - ? [key({ taskId: task.id, turnId: task.turnId })] : []) } : discoverySchema.parse(JSON.parse(raw)); - const excluded = new Set(discovery.excludedTurns); - for (const event of this.store.progressEvents(discovery.cursor)) { - discovery.cursor = event.seq; - if (!event.turnId) continue; - if (excluded.has(key({ taskId: event.taskId, turnId: event.turnId }))) continue; - const task = tasks.get(event.taskId); - if (!task?.chatId) continue; - const existing = this.load(task.id, event.turnId); - const record = existing?.chatId === task.chatId ? existing : this.newRecord(task, event.turnId); - projectProgress(record.view, event); - this.save(record); + private initializeDiscovery(): void { + if (this.store.meta(discoveryKey) !== null) return; + this.store.setMeta(discoveryKey, JSON.stringify({ snapshots: Object.fromEntries(this.store.tasks().flatMap(task => + task.turnId && task.threadId && task.chatId ? [[JSON.stringify([task.id, task.threadId, task.chatId]), task.turnId]] : [])), excludedTurns: this.store.tasks().flatMap(task => + task.turnId && finished(taskProgressStatus(task)) && !this.load(task.id, task.turnId) ? [key({ taskId: task.id, turnId: task.turnId })] : []) })); + } + private excluded(taskId: string, turnId: string): boolean { + return discoverySchema.parse(JSON.parse(this.store.meta(discoveryKey)!)).excludedTurns.includes(key({ taskId, turnId })); + } + private async discover(): Promise { + this.initializeDiscovery(); + for (const task of this.store.tasks()) { + if (!task.chatId || !task.threadId || !task.turnId || this.excluded(task.id, task.turnId)) continue; + const signature = JSON.stringify([task.threadId, task.turnId, task.chatId, taskProgressStatus(task), this.options.execution.revision(task.id, task.threadId)]); + const pending = this.store.activeProgressRecords().map(raw => progressPublicationSchema.parse(raw)).filter(record => record.taskId === task.id && record.threadId === task.threadId && record.chatId === task.chatId); + if (this.observed.get(task.id) === signature && pending.every(record => !this.needsProjection(record) || this.hydrated.has(key(record)))) continue; + if (!this.historyRetries.ready(task.id, signature, this.now())) continue; + const observation = await this.options.execution.read(task.id, task.threadId); + if (this.stopped) return; + const current = this.store.task(task.id); + if (current.threadId !== task.threadId || current.turnId !== task.turnId || current.chatId !== task.chatId || taskProgressStatus(current) !== taskProgressStatus(task)) continue; + if (observation.history.kind === 'unavailable') { this.historyRetries.failed(task.id, signature, this.now()); continue; } + const discovery = discoverySchema.parse(JSON.parse(this.store.meta(discoveryKey)!)); + const discoveryBinding = JSON.stringify([task.id, task.threadId, task.chatId]); + const watermark = discovery.snapshots[discoveryBinding]; + const turns = observation.canonicalTurnIds; + if (watermark && watermark !== task.turnId && !turns.includes(task.turnId)) { + this.historyRetries.failed(task.id, signature, this.now()); continue; } - const snapshots: Record = discovery.snapshots; - for (const task of tasks.values()) { - if (!task.chatId || !task.turnId) continue; - if (excluded.has(key({ taskId: task.id, turnId: task.turnId }))) continue; - const status = taskProgressStatus(task); - const signature = JSON.stringify([task.turnId, status, task.chatId]); - const waiting = status === 'waiting_input' || status === 'waiting_approval'; - if (snapshots[task.id] === signature && !waiting) continue; - const previous = snapshots[task.id]; - snapshots[task.id] = signature; - let record = this.load(task.id, task.turnId); - if (record && record.chatId !== task.chatId) { - this.store.setMeta(`retired-progress:${task.id}:${record.chatId}:${record.turnId}`, JSON.stringify(record)); + const eligible = watermark ? turns.slice(Math.max(0, turns.indexOf(watermark) + 1)) : []; + eligible.push(task.turnId, ...this.store.activeProgressRecords().map(raw => progressPublicationSchema.parse(raw)).filter(record => record.taskId === task.id && record.threadId === task.threadId && record.chatId === task.chatId).map(record => record.turnId)); + let covered = true; + let hydrated = 0; + for (const turnId of new Set(eligible)) { + if (this.excluded(task.id, turnId)) continue; + let record = this.load(task.id, turnId); + if (record && (record.chatId !== task.chatId || record.threadId !== task.threadId)) { + this.store.setMeta(`retired-progress:${task.id}:${record.chatId}:${turnId}`, JSON.stringify(progressPublicationSchema.parse(record))); record = null; } - if (!record && (initial ? !finished(status) : previous !== signature)) { - record = this.newRecord(task, task.turnId); - for (const event of this.store.recentTurnEvents(task.id, task.turnId)) projectProgress(record.view, event); + record ??= this.newRecord(task, turnId); + if (record.delivery.kind === 'disabled') continue; + if (record.delivery.kind === 'closed' && turnId !== task.turnId) continue; + if (this.observed.get(task.id) === signature && !this.needsProjection(record)) continue; + if (hydrated >= 64 || (!this.views.has(key(record)) && this.views.size >= 128)) { + if (record.delivery.kind === 'uncreated') this.save(record); + continue; } - if (!record) continue; - if (!finished(record.view.status) || status !== 'running') record.view.status = status; + const entries = observation.entries.filter(event => event.turnId === turnId); + if (!coversSource(entries, record.sourceFrontier)) { covered = false; continue; } + const view = progressViewSchema.parse({ status: 'running', cursor: 0, lastExecutionEventAt: null, assistant: [], tool: null }); + for (const event of entries) projectProgress(view, event); + if (turnId === task.turnId && (!finished(view.status) || taskProgressStatus(task) !== 'running')) view.status = taskProgressStatus(task); + record.view = view; record.sourceFrontier = sourceFrontier(entries); record.projectionHash = hash(progressContent(view)); record.terminal = finished(view.status); hydrated++; + this.hydrated.add(key(record)); this.save(record); } - this.store.setMeta(discoveryKey, JSON.stringify({ ...discovery, snapshots })); - }); + if (covered) { + discovery.snapshots[discoveryBinding] = turns.at(-1) ?? task.turnId; + this.store.setMeta(discoveryKey, JSON.stringify(discovery)); + } + if (covered) { this.observed.set(task.id, signature); this.historyRetries.succeeded(task.id); } + else this.historyRetries.failed(task.id, signature, this.now()); + } + } + private needsProjection(record: z.infer): boolean { + const delivery = record.delivery; + if (delivery.kind === 'closed' || delivery.kind === 'disabled') return false; + return delivery.kind !== 'published' || delivery.pending !== null || delivery.contentHash !== record.projectionHash + || delivery.streamingAt === null || this.now() - delivery.streamingAt >= renewalMs || record.terminal; } private needsWrite(record: ProgressPublication): boolean { const delivery = record.delivery; @@ -154,19 +221,19 @@ export class FeishuProgressPublisher { case 'uncreated': case 'entity': case 'sending': return true; case 'closed': case 'disabled': return false; case 'published': return delivery.pending !== null || delivery.streamingAt === null || this.now() - delivery.streamingAt >= renewalMs - || delivery.content !== progressContent(record.view) || finished(record.view.status); + || delivery.contentHash !== hash(progressContent(record.view)) || finished(record.view.status); default: { const exhaustive: never = delivery; return exhaustive; } } } - private disable(record: ProgressPublication, reason: string): void { + private disable(record: ProgressPublication, reason: string, apiCode: number | null = null): void { const delivery = record.delivery; - record.delivery = { kind: 'disabled', reason, cardId: 'cardId' in delivery ? delivery.cardId : null, messageId: 'messageId' in delivery ? delivery.messageId : null }; + record.delivery = { kind: 'disabled', reason, apiCode, cardId: 'cardId' in delivery ? delivery.cardId : null, messageId: 'messageId' in delivery ? delivery.messageId : null }; const latest = this.load(record.taskId, record.turnId); if (latest?.chatId === record.chatId) this.save({ ...latest, delivery: record.delivery }); - this.report(`${record.taskId}/${record.turnId}: ${reason}`); + this.report(reason, record.taskId, 'disable', 'disabled'); } private async deliver(record: ProgressPublication): Promise { - const guard = () => assertGroupBinding(this.store, record.taskId, record.chatId); + const guard = () => { assertGroupBinding(this.store, record.taskId, record.chatId); if (this.store.task(record.taskId).threadId !== record.threadId) throw new TaskGroupBindingChanged(); }; try { if (this.store.task(record.taskId).chatId !== record.chatId) { this.disable(record, '任务群已失效'); return; } let delivery = record.delivery; @@ -175,7 +242,7 @@ export class FeishuProgressPublisher { if (this.store.task(record.taskId).chatId !== record.chatId) return; if (messageId !== null) { record.delivery = { kind: 'published', cardId: delivery.cardId, createdAt: delivery.createdAt, messageId, - streamingAt: delivery.createdAt, content: '', pending: null }; + streamingAt: delivery.createdAt, contentHash: '', pending: null }; record.failures = 0; record.nextAttemptAt = 0; this.saveDelivery(record); return; @@ -198,26 +265,26 @@ export class FeishuProgressPublisher { this.saveDelivery(record); const messageId = await this.transport.sendProgressCard({ chatId: record.chatId, cardId: delivery.cardId, uuid: record.uuid }, guard); record.delivery = { kind: 'published', cardId: delivery.cardId, createdAt: delivery.createdAt, messageId, - streamingAt: delivery.createdAt, content: '', pending: null }; + streamingAt: delivery.createdAt, contentHash: '', pending: null }; } else if (delivery.kind === 'published') { if (record.sequence >= 2147483647) { this.disable(record, 'Card operation sequence exhausted'); return; } if (delivery.pending) { // Unknown writes are superseded with higher sequences, never treated as acknowledged. - delivery = { ...delivery, pending: null, content: '', streamingAt: null }; + delivery = { ...delivery, pending: null, contentHash: '', streamingAt: null }; } const sequence = ++record.sequence; const uuid = hash(`${record.uuid}:${sequence}`).slice(0, 32); const content = progressContent(record.view); const operation: z.infer = delivery.streamingAt === null || this.now() - delivery.streamingAt >= renewalMs ? { kind: 'settings', sequence, uuid, enabled: true } - : delivery.content !== content ? { kind: 'content', sequence, uuid, content } + : delivery.contentHash !== hash(content) ? { kind: 'content', sequence, uuid, contentHash: hash(content) } : { kind: 'settings', sequence, uuid, enabled: false }; record.delivery = { ...delivery, pending: operation }; this.saveDelivery(record); const attemptedAt = this.now(); if (operation.kind === 'content') { - await this.transport.updateProgressContent({ cardId: delivery.cardId, ...operation }, guard); - record.delivery = { ...delivery, content, pending: null }; + await this.transport.updateProgressContent({ cardId: delivery.cardId, content, ...operation }, guard); + record.delivery = { ...delivery, contentHash: hash(content), pending: null }; } else { await this.transport.setProgressStreaming({ cardId: delivery.cardId, ...operation }, guard); record.delivery = { ...delivery, kind: operation.enabled ? 'published' : 'closed', streamingAt: operation.enabled ? attemptedAt : null, pending: null }; @@ -226,15 +293,15 @@ export class FeishuProgressPublisher { record.failures = 0; record.nextAttemptAt = 0; this.saveDelivery(record); } catch (error) { - if (error instanceof TaskGroupBindingChanged) { this.disable(record, error.message); return; } + if (error instanceof TaskGroupBindingChanged) { this.disable(record, progressFailure(error)); return; } if (error instanceof TaskGroupUnavailable) { retireTaskGroup(this.store, record.taskId, record.chatId); - this.disable(record, error.message); return; + this.disable(record, progressFailure(error), error.code); return; } record.failures++; record.nextAttemptAt = this.now() + Math.min(30_000, 1000 * 2 ** Math.min(record.failures - 1, 5)); this.saveDelivery(record); - this.report(`${record.taskId}/${record.turnId}: ${errorMessage(error)}`); + this.report(error, record.taskId, record.delivery.kind); } } } diff --git a/src/live-execution.ts b/src/live-execution.ts new file mode 100644 index 0000000..f2ff98f --- /dev/null +++ b/src/live-execution.ts @@ -0,0 +1,142 @@ +import { createHash, randomUUID, type Hash } from 'node:crypto'; +import { executionKey, type ExecutionEvent, type VisibleExecution } from './execution-events.js'; +import type { ExecutionHistoryPort } from './ports.js'; + +export interface ExecutionObservation { + cursor: string; + canonicalTurnIds: string[]; + history: { kind: 'available' } | { kind: 'unavailable'; reason: string }; + entries: VisibleExecution[]; +} +export type ExecutionChange = { cursor: string; event: VisibleExecution }; +type GapCoverage = { kind: 'prefix'; length: number; hash: Hash } | { kind: 'unknown' } | { kind: 'history' }; +const prefixCoverage = (text: string): GapCoverage => ({ kind: 'prefix', length: text.length, hash: createHash('sha256').update(text, 'utf16le') }); +type LiveItem = { kind: 'snapshot'; event: VisibleExecution; streamText: string | null } | { kind: 'gap'; taskId: string; threadId: string; seq: number; after: number; coverage: GapCoverage; terminal: boolean }; +const terminal = (kind: ExecutionEvent['kind']) => kind === 'agent_message' || kind === 'tool_end' || kind === 'turn_end'; + +export class LiveExecution { + private epoch = randomUUID(); + private sequence = 0; + private floor = 0; + private evictedGap = false; + private readonly items = new Map(); + private readonly changes: ExecutionChange[] = []; + private readonly listeners = new Set<(change: ExecutionChange | { cursor: string; event: null }) => void>(); + constructor(private readonly history?: ExecutionHistoryPort, private readonly capacity = 4096) {} + get cursor(): string { return `${this.epoch}:${this.sequence}`; } + private notify(change: ExecutionChange | { cursor: string; event: null }): void { + for (const listener of this.listeners) { + try { listener(change); } catch { this.listeners.delete(listener); } + } + } + private remember(key: string, item: LiveItem): void { + this.items.delete(key); this.items.set(key, item); + while (this.items.size > this.capacity) { + const oldest = this.items.keys().next().value!, evicted = this.items.get(oldest)!; + if (evicted.kind === 'gap' && !evicted.terminal && evicted.coverage.kind !== 'history') this.evictedGap = true; + this.items.delete(oldest); + } + } + reset(): void { + this.epoch = randomUUID(); this.sequence = 0; this.floor = 0; this.evictedGap = false; this.items.clear(); this.changes.length = 0; + this.notify({ cursor: this.cursor, event: null }); + } + publish(taskId: string, event: ExecutionEvent): void { + if (event.kind === 'tool_output') return; + const key = executionKey(event), local = this.items.get(key), previous = local?.kind === 'snapshot' ? local.event : undefined; + if ((event.kind === 'agent_delta' || event.kind === 'agent_start') && (previous?.kind === 'agent_message' || (local?.kind === 'gap' && local.terminal))) return; + const safeText = event.kind === 'tool_start' || event.kind === 'tool_end' ? event.text.split('\n', 1)[0]! : event.text; + const streamText = event.kind === 'agent_delta' ? local?.kind === 'snapshot' && local.streamText !== null ? local.streamText + safeText : null : safeText; + const conflict = event.kind === 'agent_delta' && streamText !== null && previous && !streamText.startsWith(previous.text) && !previous.text.startsWith(streamText); + if (streamText === null || streamText.length > 32768 || conflict) { + let coverage: GapCoverage; + if (streamText !== null) coverage = prefixCoverage(streamText); + else if (local?.kind === 'gap' && local.coverage.kind === 'prefix') { + coverage = { kind: 'prefix', length: local.coverage.length + safeText.length, hash: local.coverage.hash.copy().update(safeText, 'utf16le') }; + } else coverage = { kind: 'unknown' }; + this.remember(key, { kind: 'gap', taskId, threadId: event.threadId, seq: ++this.sequence, + after: local?.kind === 'gap' ? local.after : this.sequence - 1, + coverage, terminal: terminal(event.kind) }); + this.floor = this.sequence; + this.notify({ cursor: this.cursor, event: null }); + return; + } + const text = (event.kind === 'agent_delta' || event.kind === 'agent_start') && previous?.text.startsWith(streamText) ? previous.text : streamText; + const item: VisibleExecution = { ...event, kind: event.kind === 'agent_start' ? 'agent_delta' : event.kind, key, taskId, seq: ++this.sequence, text }; + this.remember(key, { kind: 'snapshot', event: item, streamText }); + const change = { cursor: this.cursor, event: item }; + this.changes.push(change); + while (this.changes.length > this.capacity) this.floor = Math.max(this.floor, this.changes.shift()!.event.seq); + this.notify(change); + } + subscribe(listener: (change: ExecutionChange | { cursor: string; event: null }) => void): () => void { this.listeners.add(listener); return () => this.listeners.delete(listener); } + since(cursor: string): ExecutionChange[] | null { + const [epoch, number] = cursor.split(':'); + const sequence = Number(number); + if (epoch !== this.epoch || !Number.isSafeInteger(sequence) || sequence < this.floor || sequence > this.sequence) return null; + return this.changes.filter(change => change.event.seq > sequence); + } + current(taskId: string, threadId?: string | null): VisibleExecution[] { + return [...this.items.values()].flatMap(item => item.kind === 'snapshot' && item.event.taskId === taskId && (threadId === undefined || item.event.threadId === threadId) ? [item.event] : []); + } + revision(taskId: string, threadId?: string | null): number { + return Math.max(0, ...[...this.items.values()].map(item => item.kind === 'snapshot' ? item.event : item).filter(item => item.taskId === taskId && (threadId === undefined || item.threadId === threadId)).map(item => item.seq)); + } + async read(taskId: string, threadId: string): Promise { + const startedAtSequence = this.sequence, startedAtCursor = this.cursor, epoch = this.epoch; + let entries: VisibleExecution[] = [], history: ExecutionObservation['history']; + try { + if (!this.history) throw new Error('Codex history is unavailable'); + entries = (await this.history.readHistory(threadId)).map(event => ({ ...event, taskId, seq: startedAtSequence })); + history = { kind: 'available' }; + } catch { history = { kind: 'unavailable', reason: 'Codex 历史暂不可用' }; } + const canonicalTurnIds = [...new Set(entries.flatMap(event => event.turnId === null ? [] : [event.turnId]))]; + const merged = new Map(entries.map(event => [event.key, event])); + let gap = epoch !== this.epoch || startedAtSequence < this.floor, acknowledged = startedAtSequence; + const unresolved = new Set(); + for (const [key, local] of this.items) { + const live = local.kind === 'snapshot' ? local.event : local; + if (live.taskId !== taskId || live.threadId !== threadId) continue; + const persisted = merged.get(key); + if (local.kind === 'gap') { + const covered = persisted && (local.coverage.kind === 'prefix' + ? persisted.text.length >= local.coverage.length && createHash('sha256').update(persisted.text.slice(0, local.coverage.length), 'utf16le').digest('hex') === local.coverage.hash.copy().digest('hex') + : local.coverage.kind === 'history'); + if (!persisted || local.seq > startedAtSequence || (!terminal(persisted.kind) && !covered)) { + gap = true; unresolved.add(key); acknowledged = Math.min(acknowledged, local.after); + } + continue; + } + const event = local.event; + if (persisted?.kind === 'agent_message' && event.kind === 'agent_delta') continue; + if (persisted?.kind === 'agent_delta' && event.kind === 'agent_message') { merged.set(key, event); continue; } + if (persisted?.kind === 'agent_delta' && event.kind === 'agent_delta') { + if (persisted.text.startsWith(event.text)) continue; + if (event.text.startsWith(persisted.text)) { merged.set(key, event); continue; } + gap = true; + this.remember(key, { kind: 'gap', taskId, threadId, seq: ++this.sequence, after: startedAtSequence, coverage: local.streamText === null ? { kind: 'unknown' } : prefixCoverage(local.streamText), terminal: false }); + unresolved.add(key); + this.floor = this.sequence; + continue; + } + if (!persisted || event.seq > startedAtSequence || history.kind === 'unavailable') { + merged.set(key, event); + } + } + if (history.kind === 'available' && epoch === this.epoch) { + for (const item of merged.values()) { + if (unresolved.has(item.key)) continue; + const local = this.items.get(item.key); + if (item.text.length > 32768) { + this.remember(item.key, { kind: 'gap', taskId, threadId, seq: item.seq, after: item.seq, + coverage: local?.kind === 'gap' ? local.coverage : local?.streamText !== null && local?.streamText !== undefined ? prefixCoverage(local.streamText) : { kind: 'history' }, terminal: terminal(item.kind) }); + } else this.remember(item.key, { kind: 'snapshot', event: item, streamText: local?.kind === 'snapshot' ? local.streamText : null }); + } + } + if (this.evictedGap && (entries.some(item => item.kind === 'agent_delta') || entries.at(-1)?.kind !== 'turn_end' || entries.at(-1)?.text !== 'completed')) { + gap = true; acknowledged = Math.min(acknowledged, this.floor - 1); + } + if (gap) history = { kind: 'unavailable', reason: '实时记录存在缺口,正在重新读取 Codex 历史' }; + return { cursor: gap ? epoch === this.epoch ? `${epoch}:${acknowledged}` : startedAtCursor : this.cursor, canonicalTurnIds, history, entries: [...merged.values()].map((event, index) => ({ ...event, seq: index + 1 })) }; + } +} diff --git a/src/loop.ts b/src/loop.ts index d89ff96..cfa1790 100644 --- a/src/loop.ts +++ b/src/loop.ts @@ -1,3 +1,4 @@ +import { LiveExecution } from './live-execution.js'; import { createHash, randomUUID } from 'node:crypto'; import { constants } from 'node:fs'; import { access, mkdir, readFile, stat, rename, rm, writeFile } from 'node:fs/promises'; @@ -17,7 +18,6 @@ import { GroupCleanup } from './group-cleanup.js'; import { Store, type Inbox } from './store.js'; import { FeishuProgressPublisher } from './feishu-progress.js'; import { FeishuDocumentProgressPublisher } from './feishu-document-progress.js'; -import { taskProgressStatus } from './feishu-progress-view.js'; import { IntakeOwnershipChanged, intakeMaterialVersion, ProjectRuntime } from './project-runtime.js'; import type { ProjectAction, ProjectConfig } from './project.js'; import { matchesProjectIssue } from './project.js'; @@ -56,6 +56,7 @@ function sourceRevision(source: Source): string { } export class AgentLoop { readonly store: Store; + readonly execution: LiveExecution; readonly feishuArtifacts: FeishuArtifactPort | undefined; private readonly workspaceBridge: WorkspaceBridge; private readonly progress: FeishuProgressPublisher | FeishuDocumentProgressPublisher | null; @@ -80,6 +81,7 @@ export class AgentLoop { private readonly groups: TaskGroups; constructor(readonly config: Config, readonly ports: { github: GitHubPort; feishu: FeishuPort; feishuArtifacts?: FeishuArtifactPort; codex: CodexPort; workspace: WorkspacePort }, store?: Store) { this.store = store ?? new Store(config.dataDir); + this.execution = new LiveExecution(ports.codex); this.feishuArtifacts = ports.feishuArtifacts; this.closeout = new TaskCloseout(this.store, { assertCompletionAllowed: task => { @@ -120,18 +122,15 @@ export class AgentLoop { const progress = config.feishu.progress; this.progress = !progress.enabled ? null : progress.surface === 'document' ? ports.feishu.documentProgress && progress.documentBaseUrl - ? new FeishuDocumentProgressPublisher({ store: this.store, transport: ports.feishu.documentProgress, + ? new FeishuDocumentProgressPublisher({ store: this.store, execution: this.execution, transport: ports.feishu.documentProgress, documentBaseUrl: progress.documentBaseUrl, intervalMs: progress.intervalMs, maxLines: progress.maxLines }) : null - : ports.feishu.progress ? new FeishuProgressPublisher({ store: this.store, transport: ports.feishu.progress, + : ports.feishu.progress ? new FeishuProgressPublisher({ store: this.store, execution: this.execution, transport: ports.feishu.progress, intervalMs: progress.intervalMs, dashboardUrl: progress.dashboardUrl }) : null; + ports.codex.setObservationResetHandler?.(() => this.execution.reset()); ports.codex.setEventHandler?.(event => { try { const taskId = this.store.taskIdForThread(event.threadId); - if (taskId) this.store.recordExecutionEvent(taskId, event); - else { - const archived = this.store.meta(`project-archived-thread:${event.threadId}`); - if (archived) this.store.recordExecutionEvent(archived, event); - } + if (taskId) this.execution.publish(taskId, event); } catch (error) { this.problem('debug:' + event.threadId, error); } }); this.workspaceBridge = new WorkspaceBridge(config.dataDir, async (taskId, repository) => { @@ -232,7 +231,6 @@ export class AgentLoop { private persist(task: Task): void { const latest = this.store.task(task.id); task.chatId = latest.chatId; - this.recordProgressStatus(task); task.resources = [...new Map([...task.resources, ...latest.resources].map(resource => [resource.id, resource])).values()] .filter(resource => this.store.meta(`released-resource:${task.id}:${resource.id}`) !== 'released'); const current = this.store.task(task.id); @@ -365,7 +363,6 @@ export class AgentLoop { this.closeout.assertOpen(task.id); const profile = this.projects.executionProfile(task.id); const snapshot = await this.admitted(task.id, () => this.ports.codex.inspect(threadId, instructions, profile?.model, profile?.reasoningEffort)); - this.recordProgressStatus({ ...task, turnId: snapshot.turnId, executionStatus: snapshot.status, requests: snapshot.requests }); return snapshot; } private async observation(task: Task, reconcile = false): Promise { @@ -375,23 +372,11 @@ export class AgentLoop { const profile = this.projects.executionProfile(task.id); const activity = await this.ports.codex.activity(task.threadId, await this.instructions(task), profile?.model, profile?.reasoningEffort); if (activity.kind === 'running') { - this.recordProgressStatus({ ...task, turnId: activity.turnId, executionStatus: 'running', requests: activity.requests }); return activity; } } return this.inspect(task.threadId); } - private recordProgressStatus(task: Task): void { - if (!this.config.feishu.progress.enabled || !task.threadId || !task.turnId) return; - try { - const key = `feishu-progress:status:${JSON.stringify([task.id, task.turnId])}`; - const status = taskProgressStatus(task); - if (this.store.meta(key) === status) return; - this.store.recordExecutionEvent(task.id, { threadId: task.threadId, turnId: task.turnId, itemId: null, - kind: 'turn_status', label: '执行状态', text: status, createdAt: Date.now() }); - this.store.setMeta(key, status); - } catch (error) { this.problem('feishu-progress', error); } - } private async allocateTask(sourceId: string, source: TaskSource, purpose: 'intake' | 'execution'): Promise { const id = randomUUID(); const prompt = await this.currentPrompt(); diff --git a/src/ports.ts b/src/ports.ts index 01ea80a..6f9226f 100644 --- a/src/ports.ts +++ b/src/ports.ts @@ -1,5 +1,5 @@ import type { AgentOutput, AgentInput, ApprovalAction, Attachment, CollectionRepository, ExecutionSnapshot, IncomingMessage, PendingRequest, ProjectDecisionAction, Repository, Source, SourceComment, TaskResource, WorkspaceRequest } from './domain.js'; -import type { ExecutionEvent } from './execution-events.js'; +import type { ExecutionEvent, ExecutionHistory } from './execution-events.js'; import type { ProjectAction } from './project.js'; import type { ReasoningEffort } from './execution.js'; @@ -96,9 +96,11 @@ export interface FeishuProgressPort { updateProgressContent(input: { cardId: string; content: string; sequence: number; uuid: string }, guard?: ProgressWriteGuard): Promise; setProgressStreaming(input: { cardId: string; enabled: boolean; sequence: number; uuid: string }, guard?: ProgressWriteGuard): Promise; } -export interface CodexPort { +export interface ExecutionHistoryPort { readHistory(threadId: string): Promise; } +export interface CodexPort extends ExecutionHistoryPort { // Returns raw model output. Callers normalize it and fall back when it is unusable. summarizeTitle?(input: { taskId: string; cwd: string; text: string; signal?: AbortSignal }): Promise; + setObservationResetHandler?(handler: () => void): void; setEventHandler?(handler: (event: ExecutionEvent) => void): void; createThread(input: { cwd: string; instructions: string; model?: string; reasoningEffort?: ReasoningEffort }): Promise; inspect(threadId: string, instructions?: string, model?: string, reasoningEffort?: ReasoningEffort): Promise; diff --git a/src/store.ts b/src/store.ts index f21cd2f..82674ac 100644 --- a/src/store.ts +++ b/src/store.ts @@ -3,7 +3,6 @@ import { mkdirSync } from 'node:fs'; import { join } from 'node:path'; import { createHash } from 'node:crypto'; import { z } from 'zod'; -import { executionEventSchema, storedExecutionEventSchema, type ExecutionEvent, type StoredExecutionEvent } from './execution-events.js'; import { approvalActionSchema, feishuCardRecordSchema, inputProvenanceSchema, incomingSchema, repositorySchema, sourceSchema, taskSchema, type ApprovalAction, type FeishuCardRecord, type IncomingMessage, type InputProvenance, type Source, type Task } from './domain.js'; const payloadRow = z.object({ payload: z.string() }); @@ -14,7 +13,6 @@ const dataVersionRow = z.object({ data_version: z.number() }); const nameRow = z.object({ name: z.string() }); const sourceRow = payloadRow.extend({ reason: z.string() }); const sequenceRow = z.object({ seq: z.number() }); -const executionRow = z.object({ seq: z.number(), task_id: z.string(), payload: z.string() }); const countRow = z.object({ count: z.number() }); const approvalRow = z.object({ id: z.string(), payload: z.string(), status: z.string(), error: z.string().nullable() }); const diagnosticRow = z.object({ created_at: z.number(), scope: z.string(), message: z.string() }); @@ -23,10 +21,6 @@ const legacyTaskSchema = z.object({ id: z.string(), sourceId: z.string(), source: sourceSchema, repository: repositorySchema, workspace: z.string().nullable(), branch: z.string(), status: z.string(), prUrl: z.string().nullable().default(null), }).passthrough(); -export const documentSourcePositionSchema = z.strictObject({ - eventSeq: z.number().int().nonnegative(), inputSeq: z.number().int().nonnegative(), -}); -export type DocumentSourcePosition = z.infer; const operationSchema = z.object({ id: z.string(), task_id: z.string(), kind: z.string(), status: z.enum(['attempting', 'confirmed', 'uncertain']), result: z.string().nullable(), error: z.string().nullable() }); export type Operation = z.infer; const inboxRow = z.object({ seq: z.number(), id: z.string(), task_id: z.string(), payload: z.string(), status: z.enum(['pending', 'delivering', 'delivered', 'failed', 'uncertain', 'held', 'dropped']), origin: z.enum(['user', 'source', 'initial', 'resume']), error: z.string().nullable() }); @@ -35,14 +29,9 @@ export type InboxFilter = { statuses?: readonly Inbox['status'][]; origin?: Inbo const outboxSchema = z.object({ id: z.string(), task_id: z.string(), text: z.string(), status: z.enum(['pending', 'sending', 'sent', 'uncertain', 'dropped']), error: z.string().nullable() }); export type Outbox = z.infer; -/** Runtime execution traces are a bounded projection; user/audit facts live in their own tables. */ -export const executionEventRetentionLimit = 2_000; -export const executionDeltaSampleMs = 250; export const diagnosticRetentionLimit = 1_000; const diagnosticReadLimit = 100; const diagnosticMessageLimit = 8_192; -type LiveExecutionListener = (event: StoredExecutionEvent) => void; -const liveExecutionListeners = new Map>(); export class Store { private readonly db: DatabaseSync; @@ -51,15 +40,10 @@ export class Store { private readonly taskSnapshots = new Map(); private readonly documentPayloads = new Map(); private lastDocumentDataVersion: number | null = null; - private readonly lastDeltaAt = new Map(); private readonly metaRevisions = new Map(); private readonly taskThreads = new Map(); private readonly taskIdsByThread = new Map(); private lastMetaDataVersion: number | null = null; - private readonly ephemeralEvents: StoredExecutionEvent[] = []; - private logicalSequence = 0; - private logicalSequenceLoaded = false; - private unsubscribeLive: (() => void) | null = null; constructor(private readonly dataDir: string, options: { readOnly?: boolean } = {}) { mkdirSync(dataDir, { recursive: true, mode: 0o700 }); this.lock = options.readOnly ? null : new DatabaseSync(join(dataDir, 'owner.sqlite')); @@ -67,17 +51,7 @@ export class Store { catch { this.lock?.close(); throw new Error('已有 Agent Loop 进程使用此数据目录'); } try { this.db = new DatabaseSync(join(dataDir, 'tasks.sqlite'), { readOnly: options.readOnly ?? false }); - if (options.readOnly) { - const listeners = liveExecutionListeners.get(dataDir) ?? new Set(); - liveExecutionListeners.set(dataDir, listeners); - const listener: LiveExecutionListener = event => { - this.ephemeralEvents.push(event); - if (this.ephemeralEvents.length > 4_096) this.ephemeralEvents.splice(0, this.ephemeralEvents.length - 4_096); - }; - listeners.add(listener); - this.unsubscribeLive = () => { listeners.delete(listener); if (!listeners.size) liveExecutionListeners.delete(dataDir); }; - return; - } + if (options.readOnly) return; this.db.exec(`PRAGMA journal_mode=WAL; PRAGMA foreign_keys=ON; PRAGMA busy_timeout=5000; CREATE TABLE IF NOT EXISTS sources(id TEXT PRIMARY KEY,payload TEXT NOT NULL,reason TEXT NOT NULL); CREATE TABLE IF NOT EXISTS source_revisions(source_id TEXT NOT NULL,hash TEXT NOT NULL,payload TEXT NOT NULL,PRIMARY KEY(source_id,hash)); @@ -96,9 +70,6 @@ export class Store { CREATE TABLE IF NOT EXISTS diagnostic_rollups(scope TEXT NOT NULL,fingerprint TEXT NOT NULL,first_created_at INTEGER NOT NULL,last_created_at INTEGER NOT NULL,count INTEGER NOT NULL,message TEXT NOT NULL,PRIMARY KEY(scope,fingerprint)); CREATE INDEX IF NOT EXISTS diagnostic_rollups_recent ON diagnostic_rollups(last_created_at DESC); CREATE TABLE IF NOT EXISTS meta(key TEXT PRIMARY KEY,value TEXT NOT NULL); - CREATE TABLE IF NOT EXISTS execution_events(seq INTEGER PRIMARY KEY AUTOINCREMENT,task_id TEXT NOT NULL REFERENCES tasks(id),created_at INTEGER NOT NULL,payload TEXT NOT NULL); - CREATE INDEX IF NOT EXISTS execution_events_task ON execution_events(task_id,seq); - CREATE INDEX IF NOT EXISTS execution_events_task_kind_seq ON execution_events(task_id,json_extract(payload,'$.kind'),seq); CREATE TABLE IF NOT EXISTS feishu_progress(task_id TEXT NOT NULL REFERENCES tasks(id),turn_id TEXT NOT NULL,payload TEXT NOT NULL,active INTEGER NOT NULL,PRIMARY KEY(task_id,turn_id)); CREATE INDEX IF NOT EXISTS feishu_progress_active ON feishu_progress(active); CREATE TABLE IF NOT EXISTS feishu_document_progress(task_id TEXT PRIMARY KEY REFERENCES tasks(id),payload TEXT NOT NULL); @@ -342,117 +313,8 @@ export class Store { this.metaRevisions.set(key, revision); return revision; } - recordExecutionEvent(taskId: string, event: ExecutionEvent): void { - const sequence = this.nextExecutionSequence(); - let persist = event.kind !== 'tool_output' && event.kind !== 'agent_delta'; - // Keep enough live narrative for the dashboard/progress surfaces without turning every token - // delta into a durable row. Completed messages and lifecycle events remain lossless. - if (event.kind === 'agent_delta') { - const key = `${taskId}\u0000${event.threadId}\u0000${event.turnId ?? ''}\u0000${event.itemId ?? ''}`; - const previous = this.lastDeltaAt.get(key); - persist = previous === undefined || event.createdAt - previous >= executionDeltaSampleMs; - if (persist) this.lastDeltaAt.set(key, event.createdAt); - } - const compact = event.kind === 'tool_start' || event.kind === 'tool_end' - ? { ...event, text: event.text.split('\n', 1)[0]!.slice(0, 4_096) } - : event; - const stored = { ...compact, seq: sequence, taskId } as StoredExecutionEvent; - if (!persist) { - const live = { ...stored, text: stored.text.length <= 16_384 ? stored.text : stored.text.slice(-16_384) }; - this.ephemeralEvents.push(live); - if (this.ephemeralEvents.length > 4_096) this.ephemeralEvents.splice(0, this.ephemeralEvents.length - 4_096); - for (const listener of liveExecutionListeners.get(this.dataDir) ?? []) listener(live); - return; - } - this.prepare('INSERT INTO execution_events(seq,task_id,created_at,payload) VALUES(?,?,?,?)').run(sequence, taskId, event.createdAt, JSON.stringify(compact)); - this.pruneExecutionEvents(taskId); - } - private nextExecutionSequence(): number { - if (!this.logicalSequenceLoaded) { - const row = sequenceRow.parse(this.prepare('SELECT COALESCE(MAX(seq),0) AS seq FROM execution_events').get()); - this.logicalSequence = row.seq; this.logicalSequenceLoaded = true; - } - return ++this.logicalSequence; - } - private eventRows(taskId: string, after = 0, limit = 500, includeToolOutput = true): StoredExecutionEvent[] { - const durable = this.prepare('SELECT seq,task_id,payload FROM execution_events WHERE task_id=? AND seq>? ORDER BY seq LIMIT ?').all(taskId, after, limit).map(raw => { - const row = executionRow.parse(raw); - return storedExecutionEventSchema.parse({ ...executionEventSchema.parse(JSON.parse(row.payload)), seq: row.seq, taskId: row.task_id }); - }); - const transient = this.ephemeralEvents.filter(event => event.taskId === taskId && event.seq > after && (includeToolOutput || event.kind !== 'tool_output')); - return [...durable, ...transient].sort((a, b) => a.seq - b.seq).slice(0, limit); - } - private pruneExecutionEvents(taskId: string): void { - const count = countRow.parse(this.prepare('SELECT COUNT(*) AS count FROM execution_events WHERE task_id=?').get(taskId)).count; - const overflow = count - executionEventRetentionLimit; - if (overflow <= 0) return; - // Keep final answers and lifecycle facts longest. Drop sampled/tool metadata first, - // then the oldest status rows if the durable budget still overflows. - this.prepare(`DELETE FROM execution_events WHERE seq IN ( - SELECT seq FROM execution_events WHERE task_id=? - ORDER BY CASE json_extract(payload,'$.kind') - WHEN 'agent_delta' THEN 0 - WHEN 'tool_start' THEN 0 - WHEN 'tool_end' THEN 0 - WHEN 'turn_status' THEN 1 - WHEN 'error' THEN 2 - WHEN 'turn_start' THEN 2 - WHEN 'turn_end' THEN 2 - WHEN 'agent_message' THEN 3 - ELSE 1 END, seq LIMIT ?)`) - .run(taskId, overflow); - } - executionEvents(taskId: string, after = 0, limit = 500): StoredExecutionEvent[] { - if (!this.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='execution_events'").get()) return []; - return this.eventRows(taskId, after, limit); - } - visibleExecutionEvents(taskId: string, after = 0, limit = 500): StoredExecutionEvent[] { - if (!this.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='execution_events'").get()) return []; - return this.eventRows(taskId, after, limit, false); - } - latestVisibleExecutionEvent(taskId: string): StoredExecutionEvent | null { - if (!this.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='execution_events'").get()) return null; - const raw = this.prepare(`SELECT seq,task_id,payload FROM execution_events - WHERE task_id=? AND COALESCE(json_extract(payload,'$.kind'),'') <> 'tool_output' - ORDER BY seq DESC LIMIT 1`).get(taskId); - if (!raw) return null; - const row = executionRow.parse(raw); - return storedExecutionEventSchema.parse({ ...executionEventSchema.parse(JSON.parse(row.payload)), seq: row.seq, taskId: row.task_id }); - } - latestVisibleExecutionEvents(taskIds: readonly string[]): Map { - const result = new Map(); - if (!taskIds.length || !this.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='execution_events'").get()) return result; - const placeholders = taskIds.map(() => '?').join(','); - const rows = this.prepare(`SELECT event.seq,event.task_id,event.payload FROM execution_events event - JOIN (SELECT task_id,MAX(seq) AS seq FROM execution_events - WHERE task_id IN (${placeholders}) AND COALESCE(json_extract(payload,'$.kind'),'') <> 'tool_output' - GROUP BY task_id) latest ON latest.task_id=event.task_id AND latest.seq=event.seq`).all(...taskIds); - for (const raw of rows) { - const row = executionRow.parse(raw); - result.set(row.task_id, storedExecutionEventSchema.parse({ ...executionEventSchema.parse(JSON.parse(row.payload)), seq: row.seq, taskId: row.task_id })); - } - for (const event of this.ephemeralEvents) { - if (event.kind === 'tool_output' || !taskIds.includes(event.taskId)) continue; - const previous = result.get(event.taskId); - if (!previous || event.seq > previous.seq) result.set(event.taskId, event); - } - return result; - } - documentConversationPosition(taskId: string): DocumentSourcePosition { - const position = documentSourcePositionSchema.parse(this.prepare(`SELECT - COALESCE((SELECT seq FROM execution_events WHERE task_id=? ORDER BY seq DESC LIMIT 1),0) AS eventSeq, - COALESCE((SELECT seq FROM inbox WHERE task_id=? ORDER BY seq DESC LIMIT 1),0) AS inputSeq`).get(taskId, taskId)); - const transient = this.ephemeralEvents.filter(event => event.taskId === taskId).at(-1)?.seq ?? 0; - return { ...position, eventSeq: Math.max(position.eventSeq, transient) }; - } - *documentConversationEvents(taskId: string): IterableIterator { - const rows = this.prepare("SELECT seq,payload FROM execution_events WHERE task_id=? AND json_extract(payload,'$.kind') IN ('agent_delta','agent_message','turn_start','turn_end','turn_status') ORDER BY seq").all(taskId) - .map(raw => ({ seq: sequenceRow.parse(raw).seq, payload: payloadRow.parse(raw).payload })); - const transient = this.ephemeralEvents.filter(event => event.taskId === taskId && event.kind !== 'tool_output'); - for (const item of [...rows.map(row => ({ seq: row.seq, payload: row.payload })), ...transient.map(event => ({ seq: event.seq, event }))].sort((a, b) => a.seq - b.seq)) { - const event = 'event' in item ? item.event : storedExecutionEventSchema.parse({ ...JSON.parse(item.payload), seq: item.seq, taskId }); - if (['agent_delta', 'agent_message', 'turn_start', 'turn_end', 'turn_status'].includes(event.kind)) yield event; - } + inputSequence(taskId: string): number { + return sequenceRow.parse(this.prepare('SELECT COALESCE(MAX(seq),0) AS seq FROM inbox WHERE task_id=?').get(taskId)).seq; } setMeta(key: string, value: string): void { const result = this.prepare('INSERT INTO meta VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value WHERE meta.value IS NOT excluded.value').run(key, value); @@ -466,20 +328,6 @@ export class Store { const row = this.prepare('PRAGMA data_version').get(); return dataVersionRow.parse(row).data_version; } - latestExecutionSequence(): number { - const persisted = sequenceRow.parse(this.prepare('SELECT COALESCE(MAX(seq),0) AS seq FROM execution_events').get()).seq; - return Math.max(persisted, this.logicalSequence, this.ephemeralEvents.at(-1)?.seq ?? 0); - } - progressEvents(after: number, limit = 500): StoredExecutionEvent[] { - const durable = this.prepare("SELECT seq,task_id,payload FROM execution_events WHERE seq>? AND COALESCE(json_extract(payload,'$.kind'),'') <> 'tool_output' ORDER BY seq LIMIT ?").all(after, limit).map(raw => { - const row = executionRow.parse(raw); - return storedExecutionEventSchema.parse({ ...JSON.parse(row.payload), seq: row.seq, taskId: row.task_id }); - }); - return [...durable, ...this.ephemeralEvents.filter(event => event.seq > after)].sort((a, b) => a.seq - b.seq).slice(0, limit); - } - recentTurnEvents(taskId: string, turnId: string): StoredExecutionEvent[] { - return this.eventRows(taskId, 0, executionEventRetentionLimit, false).filter(event => event.turnId === turnId).slice(-200); - } progressRecord(taskId: string, turnId: string): unknown | null { const row = this.prepare('SELECT payload FROM feishu_progress WHERE task_id=? AND turn_id=?').get(taskId, turnId); return row ? JSON.parse(payloadRow.parse(row).payload) : null; @@ -491,7 +339,6 @@ export class Store { this.prepare('INSERT INTO feishu_progress VALUES(?,?,?,?) ON CONFLICT(task_id,turn_id) DO UPDATE SET payload=excluded.payload,active=excluded.active').run(taskId, turnId, JSON.stringify(record), Number(active)); } close(): void { - this.unsubscribeLive?.(); this.unsubscribeLive = null; this.db.close(); this.lock?.close(); this.statements.clear(); this.taskSnapshots.clear(); this.documentPayloads.clear(); } diff --git a/test/codex-history.test.ts b/test/codex-history.test.ts new file mode 100644 index 0000000..adea3f9 --- /dev/null +++ b/test/codex-history.test.ts @@ -0,0 +1,70 @@ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { CodexAppServer } from '../src/adapters/codex.js'; +import type { CodexConnector } from '../src/adapters/codex-daemon.js'; + +function fixture(thread: unknown) { + const calls: { method?: string; params?: unknown }[] = []; + const connector: CodexConnector = async handlers => ({ + close: async () => {}, + send: async body => { + const request = JSON.parse(body); calls.push(request); + if (request.id === undefined) return; + if (request.method !== 'initialize' && request.method !== 'thread/read') throw new Error('History read performed an execution operation'); + queueMicrotask(() => handlers.message(JSON.stringify({ id: request.id, result: request.method === 'initialize' ? {} : { thread } }))); + }, + }); + const adapter = new CodexAppServer({ approvalPolicy: 'never', sandbox: 'read-only', requestTimeoutMs: 1000 }, connector); + return { adapter, calls }; +} +const turn = { id: 'turn', itemsView: 'full', status: 'failed', startedAt: null, completedAt: null, error: { message: 'Visible failure' }, items: [ + { id: 'private', type: 'reasoning', text: 'PRIVATE_REASONING' }, + { id: 'user', type: 'userMessage', content: [{ type: 'text', text: 'PRIVATE_PROMPT' }] }, + { id: 'message', type: 'agentMessage', phase: 'final_answer', text: 'Complete answer' }, + { id: 'command', type: 'commandExecution', command: 'npm test', status: 'completed', aggregatedOutput: 'PRIVATE_OUTPUT', exitCode: 0, startedAtMs: 1234 }, +] }; + +test('unloaded and sealed current Threads expose visible history without resume, ownership, native replies or synthetic time', async t => { + const f = fixture({ id: 'thread', historyMode: 'paginated', status: { type: 'notLoaded' }, turns: [turn] }); + f.adapter.setThreadCompletionPolicy({ isCompleted: () => true }); + t.after(() => f.adapter.close()); + const first = await f.adapter.readHistory('thread'), second = await f.adapter.readHistory('thread'); + assert.deepEqual(first, second); + assert.deepEqual(first.map(event => [event.kind, event.text, event.createdAt]), [ + ['agent_message', 'Complete answer', null], ['tool_end', '$ npm test', 1234], ['turn_end', 'failed\nVisible failure', null], + ]); + assert.doesNotMatch(JSON.stringify(first), /PRIVATE_/); + assert.deepEqual(f.calls.filter(call => call.method !== 'initialize' && call.method !== 'initialized').map(call => [call.method, call.params]), [ + ['thread/read', { threadId: 'thread', includeTurns: true }], ['thread/read', { threadId: 'thread', includeTurns: true }], + ]); + assert.throws(() => f.adapter.assertThreadCompletionAllowed('thread'), /unavailable/); +}); + +for (const [name, thread] of [ + ['wrong Thread', { id: 'other', turns: [turn] }], + ['incomplete items', { id: 'thread', turns: [{ ...turn, itemsView: 'summary' }] }], + ['repeated turns', { id: 'thread', turns: [turn, turn] }], + ['malformed visible item', { id: 'thread', turns: [{ ...turn, items: [{ type: 'agentMessage', id: 'bad', text: 123 }] }] }], +] as const) test(`history rejects ${name} instead of presenting incomplete history`, async t => { + const f = fixture(thread); t.after(() => f.adapter.close()); await assert.rejects(f.adapter.readHistory('thread')); +}); + +test('active canonical assistant snapshots stay partial and retain actual turn time', async t => { + const f = fixture({ id: 'thread', turns: [{ ...turn, status: 'inProgress', error: null, startedAt: 123, items: [{ type: 'agentMessage', id: 'partial', phase: 'commentary', text: 'hello' }] }] }); + t.after(() => f.adapter.close()); + const history = await f.adapter.readHistory('thread'); + assert.equal(history[0]?.kind, 'agent_delta'); assert.equal(history[0]?.createdAt, 123000); +}); + +for (const phase of ['commentary', 'final_answer']) { + for (const completedAtMs of [null, 125000]) { + test(`active ${phase} history uses item completion time ${completedAtMs}`, async t => { + const f = fixture({ id: 'thread', turns: [{ ...turn, status: 'inProgress', error: null, startedAt: 123, + items: [{ type: 'agentMessage', id: 'message', phase, text: 'hello', startedAtMs: 124000, completedAtMs }] }] }); + t.after(() => f.adapter.close()); + const history = await f.adapter.readHistory('thread'); + assert.equal(history[0]?.kind, completedAtMs === null ? 'agent_delta' : 'agent_message'); + assert.equal(history[0]?.createdAt, 124000); + }); + } +} diff --git a/test/dashboard-coordinator.test.ts b/test/dashboard-coordinator.test.ts index ddf1fcf..af5556c 100644 --- a/test/dashboard-coordinator.test.ts +++ b/test/dashboard-coordinator.test.ts @@ -1,3 +1,4 @@ +import { ExecutionFixture } from './execution-fixture.js'; import assert from 'node:assert/strict'; import { mkdtemp, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; @@ -20,7 +21,7 @@ function task(id: string, title: string, purpose: 'execution' | 'intake' = 'exec test('dashboard resource DTO keeps project card repositories usable by the client', async t => { const root = await mkdtemp(join(tmpdir(), 'agent-dashboard-resource-dto-')); - const store = new Store(root); + const store = new Store(root); const source = new ExecutionFixture(); const worker = task('worker', 'Implement dashboard', 'execution', [{ id: 'worker-repository', kind: 'git-worktree', repository: repositorySchema.parse({ name: 'example/project', path: '/tmp/source', baseBranch: 'main' }), @@ -33,7 +34,7 @@ test('dashboard resource DTO keeps project card repositories usable by the clien ledger.create({ id: 'project-a', name: 'Project A', directory: root, repositories: [], instructions: '' }, coordinator.id); ledger.addMember('project-a', worker.id, 'worker'); }); - const dashboard = await startDashboard({ dataDir: root, port: 0 }); + const dashboard = await startDashboard({ execution: source.execution, dataDir: root, port: 0 }); t.after(async () => { await dashboard.close(); store.close(); await rm(root, { recursive: true, force: true }); }); const projects = await (await fetch(dashboard.url + '/api/projects')).json(); @@ -54,7 +55,7 @@ test('dashboard resource DTO keeps project card repositories usable by the clien test('project dashboard uses ledger membership, reports, and redacted task events', async t => { const root = await mkdtemp(join(tmpdir(), 'agent-dashboard-coordinator-')); - const store = new Store(root); + const store = new Store(root); const source = new ExecutionFixture(); const coordinator = task('coordinator', 'Coordinate release'); const worker = task('worker', 'Implement dashboard'); const secondWorker = task('second-worker', 'Verify dashboard'); @@ -69,15 +70,15 @@ test('project dashboard uses ledger membership, reports, and redacted task event store.setMeta('project-intake-route:' + intake.id, JSON.stringify({ projectId: 'project-a', coordinatorTaskId: coordinator.id })); const report = { id: 'report', chatId: 'chat:' + coordinator.id, senderId: 'runtime:project', text: 'Worker worker update. Added the reader.', attachments: [], createdAt: Date.now(), chatType: 'group' as const }; store.receive(coordinator.id, report, 'source', { kind: 'agent', projectId: 'project-a', taskId: worker.id, eventId: 'report:worker:update', category: 'update', routing: 'final', escalation: undefined, sources: [{ messageId: 'owner-message', chatId: worker.chatId!, senderId: 'owner' }], currentStep: 'Reader', nextStep: 'Run tests', blockers: [], evidence: ['test/dashboard-coordinator.test.ts'] }); - store.recordExecutionEvent(worker.id, { threadId: worker.threadId!, turnId: worker.turnId, itemId: 'agent', kind: 'agent_message', label: 'Agent', text: 'Visible progress', createdAt: Date.now() }); - store.recordExecutionEvent(worker.id, { threadId: worker.threadId!, turnId: worker.turnId, itemId: 'tool', kind: 'tool_end', label: '命令 · completed · exit 0', text: '$ npm test', createdAt: Date.now() }); - store.recordExecutionEvent(worker.id, { threadId: worker.threadId!, turnId: worker.turnId, itemId: 'legacy-tool', kind: 'tool_end', label: '命令 · completed · exit 0', text: '$ npm test\nSECRET_OUTPUT', createdAt: Date.now() }); - store.recordExecutionEvent(worker.id, { threadId: worker.threadId!, turnId: worker.turnId, itemId: 'tool', kind: 'tool_output', label: '工具输出', text: 'SECRET_OUTPUT', createdAt: Date.now() }); - for (let index = 0; index < 501; index++) store.recordExecutionEvent(worker.id, { threadId: worker.threadId!, turnId: worker.turnId, itemId: 'legacy-tool-' + index, kind: 'tool_output', label: '工具输出', text: 'OLD_OUTPUT_' + index, createdAt: Date.now() }); - store.recordExecutionEvent(worker.id, { threadId: worker.threadId!, turnId: worker.turnId, itemId: 'after-gap', kind: 'agent_message', label: '最终回答', text: 'VISIBLE_AFTER_GAP', createdAt: Date.now() }); + source.publish(worker.id, { threadId: worker.threadId!, turnId: worker.turnId, itemId: 'agent', kind: 'agent_message', label: 'Agent', text: 'Visible progress', createdAt: Date.now() }); + source.publish(worker.id, { threadId: worker.threadId!, turnId: worker.turnId, itemId: 'tool', kind: 'tool_end', label: '命令 · completed · exit 0', text: '$ npm test', createdAt: Date.now() }); + source.publish(worker.id, { threadId: worker.threadId!, turnId: worker.turnId, itemId: 'legacy-tool', kind: 'tool_end', label: '命令 · completed · exit 0', text: '$ npm test\nSECRET_OUTPUT', createdAt: Date.now() }); + source.publish(worker.id, { threadId: worker.threadId!, turnId: worker.turnId, itemId: 'tool', kind: 'tool_output', label: '工具输出', text: 'SECRET_OUTPUT', createdAt: Date.now() }); + for (let index = 0; index < 501; index++) source.publish(worker.id, { threadId: worker.threadId!, turnId: worker.turnId, itemId: 'legacy-tool-' + index, kind: 'tool_output', label: '工具输出', text: 'OLD_OUTPUT_' + index, createdAt: Date.now() }); + source.publish(worker.id, { threadId: worker.threadId!, turnId: worker.turnId, itemId: 'after-gap', kind: 'agent_message', label: '最终回答', text: 'VISIBLE_AFTER_GAP', createdAt: Date.now() }); store.receive(worker.id, { id: 'long-input', chatId: worker.chatId!, senderId: 'owner', text: 'x'.repeat(40_000), attachments: [], createdAt: Date.now(), chatType: 'group' }, 'user'); }); - const dashboard = await startDashboard({ dataDir: root, port: 0 }); + const dashboard = await startDashboard({ execution: source.execution, dataDir: root, port: 0 }); t.after(async () => { await dashboard.close(); store.close(); await rm(root, { recursive: true, force: true }); }); const projects = await (await fetch(dashboard.url + '/api/projects')).json(); @@ -98,7 +99,7 @@ test('project dashboard uses ledger membership, reports, and redacted task event assert.equal(detail.reports[0].routing, 'final'); assert.deepEqual(detail.reports[0].sources, [{ messageId: 'owner-message', chatId: worker.chatId, senderId: 'owner' }]); assert.equal(detail.inputs.find((input: { id: string }) => input.id === 'long-input').text.length, 32_770); - assert.equal(detail.events.some((event: { kind: string }) => event.kind === 'tool_output'), false); + assert.equal(detail.timeline.some((event: { kind: string }) => event.kind === 'tool_output'), false); assert.equal(detail.timeline.some((event: { type: string }) => event.type === 'INPUT'), true); assert.equal(detail.timeline.some((event: { type: string }) => event.type === 'NARRATIVE'), true); assert.equal(detail.timeline.some((event: { type: string }) => event.type === 'TOOL_CALL'), true); @@ -109,14 +110,10 @@ test('project dashboard uses ledger membership, reports, and redacted task event assert.doesNotMatch(JSON.stringify(detail), /SECRET_OUTPUT/); assert.match(JSON.stringify(detail), /VISIBLE_AFTER_GAP/); - const reader = new ProjectDashboardReader(store); - const hiddenStart = store.executionEvents(worker.id).find(event => event.itemId === 'legacy-tool-0')?.seq; - assert.ok(hiddenStart); - const firstPage = reader.events(worker.id, hiddenStart - 1); - assert.equal(firstPage.events.some(event => event.text === 'VISIBLE_AFTER_GAP'), true); - const controller = new AbortController(); - const stream = await fetch(dashboard.url + '/api/events?taskId=' + worker.id, { signal: controller.signal }); + const cursor = source.execution.cursor; + source.publish(worker.id, { threadId: worker.threadId!, turnId: worker.turnId, itemId: 'new', kind: 'agent_message', label: 'Agent', text: 'VISIBLE_AFTER_GAP', createdAt: Date.now() }); + const stream = await fetch(dashboard.url + '/api/events?taskId=' + worker.id + '&after=' + encodeURIComponent(cursor), { signal: controller.signal }); const streamReader = stream.body!.getReader(); let streamText = ''; for (let attempt = 0; attempt < 5 && !streamText.includes('VISIBLE_AFTER_GAP'); attempt++) { const chunk = await streamReader.read(); streamText += new TextDecoder().decode(chunk.value); diff --git a/test/dashboard-history.test.ts b/test/dashboard-history.test.ts new file mode 100644 index 0000000..deec6df --- /dev/null +++ b/test/dashboard-history.test.ts @@ -0,0 +1,119 @@ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { DatabaseSync } from 'node:sqlite'; +import { mkdtemp, rm } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { Store } from '../src/store.js'; +import { taskSchema } from '../src/domain.js'; +import { startDashboard } from '../src/dashboard.js'; +import { LiveExecution } from '../src/live-execution.js'; +import { executionKey } from '../src/execution-events.js'; +import { ExecutionFixture } from './execution-fixture.js'; + +const task = () => taskSchema.parse({ id: 'task', sourceId: 'source', source: { kind: 'manual', message: { id: 'original', chatId: 'private', senderId: 'owner', text: 'Business question', attachments: [], createdAt: 1 } }, + contextDirectory: '/tmp', resources: [], status: 'running', reason: null, prompt: '', promptHash: '', skillPath: null, chatId: 'chat', threadId: 'thread', turnId: 'turn', executionStatus: 'running', lastHandledTurnId: null, requests: [], createdAt: 1, updatedAt: 1 }); +const event = (text: string) => ({ threadId: 'thread', turnId: 'turn', itemId: 'item', kind: 'agent_message' as const, label: 'Agent', text, createdAt: 2 }); + +async function streamText(url: string): Promise { + const abort = new AbortController(); + const timeout = setTimeout(() => abort.abort(), 2000); + try { + const response = await fetch(url, { signal: abort.signal }); + const reader = response.body!.getReader(); + let text = ''; + while (!text.includes('event: reset')) { const result = await reader.read(); if (result.done) break; text += new TextDecoder().decode(result.value); } + return text; + } finally { clearTimeout(timeout); abort.abort(); } +} + +test('detail rehydrates from Codex across process restart, retains business records on outage and resets old SSE cursors', async t => { + const directory = await mkdtemp(join(tmpdir(), 'dashboard-history-')); + const store = new Store(directory); store.saveTask(task()); + store.receive('task', { id: 'input', chatId: 'chat', chatType: 'group', senderId: 'owner', text: 'Business input', attachments: [], createdAt: 1 }); + store.enqueue('task', 'Business final output', 'output'); + const source = new ExecutionFixture(); source.publish('task', event('Codex owned historical text')); + let dashboard = await startDashboard({ dataDir: directory, port: 0, host: '127.0.0.1', execution: source.execution }); + t.after(async () => { await dashboard.close(); store.close(); await rm(directory, { recursive: true, force: true }); }); + for (let i = 0; i < 3; i++) await fetch(dashboard.url + '/api/snapshot'); + assert.deepEqual(source.reads, []); + const first = await (await fetch(dashboard.url + '/api/tasks/task')).json(); + assert.equal(first.history.kind, 'available'); assert.match(JSON.stringify(first.timeline), /Codex owned historical text/); + await dashboard.close(); source.restart(); + dashboard = await startDashboard({ dataDir: directory, port: 0, host: '127.0.0.1', execution: source.execution }); + assert.deepEqual(source.execution.current('task'), []); + const second = await (await fetch(dashboard.url + '/api/tasks/task')).json(); + assert.match(JSON.stringify(second.timeline), /Codex owned historical text/); assert.notEqual(first.cursor, second.cursor); + assert.match(await streamText(dashboard.url + '/api/events?taskId=task&after=' + encodeURIComponent(first.cursor)), /event: reset/); + source.unavailable = true; + const unavailable = await (await fetch(dashboard.url + '/api/tasks/task')).json(); + assert.equal(unavailable.history.kind, 'unavailable'); + assert.equal(unavailable.inputs[0].text, 'Business input'); assert.equal(unavailable.outputs[0].text, 'Business final output'); + assert.match(JSON.stringify(unavailable.timeline), /Business input/); +}); + +test('task binding is revalidated after history awaits, and fetch-to-subscribe rotation requests reset', async t => { + const directory = await mkdtemp(join(tmpdir(), 'dashboard-binding-')); + const store = new Store(directory); store.saveTask(task()); + let release!: () => void, entered!: () => void; + const reading = new Promise(resolve => { entered = resolve; }); + const execution = new LiveExecution({ readHistory: async threadId => { + if (threadId === 'thread') { entered(); await new Promise(resolve => { release = resolve; }); } + const item = { ...event(threadId === 'thread' ? 'obsolete' : 'current'), threadId }; + return [{ ...item, key: executionKey(item) }]; + } }); + const dashboard = await startDashboard({ dataDir: directory, port: 0, host: '127.0.0.1', execution }); + t.after(async () => { await dashboard.close(); store.close(); await rm(directory, { recursive: true, force: true }); }); + const request = fetch(dashboard.url + '/api/tasks/task'); await reading; + store.saveTask({ ...task(), threadId: 'replacement' }); release(); + const detail = await (await request).json(); + assert.equal(detail.threadId, 'replacement'); assert.doesNotMatch(JSON.stringify(detail.timeline), /obsolete/); + store.saveTask({ ...task(), threadId: 'newer' }); + assert.match(await streamText(dashboard.url + '/api/events?taskId=task&threadId=replacement&after=' + encodeURIComponent(detail.cursor)), /event: reset/); +}); + + +test('an unbound subscription stays open and detaches synchronously when its task disappears', async t => { + const directory = await mkdtemp(join(tmpdir(), 'dashboard-unbound-')); + const store = new Store(directory); store.saveTask({ ...task(), threadId: null, turnId: null }); + const execution = new LiveExecution(); + const subscribe = execution.subscribe.bind(execution); + let subscribed = 0, delivered = 0; + execution.subscribe = listener => { + subscribed++; + const detach = subscribe(change => { delivered++; listener(change); }); + return () => { subscribed--; detach(); }; + }; + const dashboard = await startDashboard({ dataDir: directory, port: 0, host: '127.0.0.1', execution }); + const abort = new AbortController(); + t.after(async () => { abort.abort(); await dashboard.close(); store.close(); await rm(directory, { recursive: true, force: true }); }); + const response = await fetch(dashboard.url + '/api/events?taskId=task&threadId=&after=' + encodeURIComponent(execution.cursor), { signal: abort.signal }); + const reader = response.body!.getReader(); + const first = new TextDecoder().decode((await reader.read()).value); + assert.doesNotMatch(first, /event: reset/); + assert.equal(subscribed, 1); + const db = new DatabaseSync(join(directory, 'tasks.sqlite')); + try { db.exec('DELETE FROM tasks'); } finally { db.close(); } + execution.publish('other', event('one')); + assert.equal(subscribed, 0); + execution.publish('other', event('two')); + assert.equal(delivered, 1); + const rest = new TextDecoder().decode((await reader.read()).value); + assert.match(rest, /event: reset/); + assert.equal((await reader.read()).done, true); +}); + +test('a task removed during a history read returns not found', async t => { + const directory = await mkdtemp(join(tmpdir(), 'dashboard-removed-')); + const store = new Store(directory); store.saveTask(task()); + let release!: () => void, entered!: () => void; + const reading = new Promise(resolve => { entered = resolve; }); + const execution = new LiveExecution({ readHistory: async () => { entered(); await new Promise(resolve => { release = resolve; }); return []; } }); + const dashboard = await startDashboard({ dataDir: directory, port: 0, host: '127.0.0.1', execution }); + t.after(async () => { await dashboard.close(); store.close(); await rm(directory, { recursive: true, force: true }); }); + const request = fetch(dashboard.url + '/api/tasks/task'); await reading; + const db = new DatabaseSync(join(directory, 'tasks.sqlite')); + try { db.exec('DELETE FROM tasks'); } finally { db.close(); } + release(); + assert.equal((await request).status, 404); +}); diff --git a/test/dashboard-performance.test.ts b/test/dashboard-performance.test.ts index 5809e88..36635ce 100644 --- a/test/dashboard-performance.test.ts +++ b/test/dashboard-performance.test.ts @@ -1,3 +1,4 @@ +import { ExecutionFixture } from './execution-fixture.js'; import assert from 'node:assert/strict'; import { performance } from 'node:perf_hooks'; import { DatabaseSync } from 'node:sqlite'; @@ -8,7 +9,7 @@ import { test } from 'node:test'; import { taskSchema } from '../src/domain.js'; import type { ExecutionEvent } from '../src/execution-events.js'; import { startDashboard } from '../src/dashboard.js'; -import { Store, diagnosticRetentionLimit, executionEventRetentionLimit } from '../src/store.js'; +import { Store, diagnosticRetentionLimit } from '../src/store.js'; function task(root: string) { const message = { id: 'source', chatId: 'chat', senderId: 'owner', text: 'Keep this user message', attachments: [], createdAt: 1, chatType: 'p2p' as const }; @@ -25,6 +26,7 @@ function event(kind: ExecutionEvent['kind'], text: string, createdAt = 10): Exec test('thread event routing resolves only the task identity', async t => { const root = await mkdtemp(join(tmpdir(), 'agent-dashboard-thread-lookup-')); + const source = new ExecutionFixture(); const store = new Store(root); const saved = task(root); store.saveTask(saved); t.after(async () => { store.close(); await rm(root, { recursive: true, force: true }); }); assert.equal(store.taskIdForThread(saved.threadId!), saved.id); @@ -39,6 +41,7 @@ test('thread event routing resolves only the task identity', async t => { test('a rejected task save retains both existing thread routes', async t => { const root = await mkdtemp(join(tmpdir(), 'agent-dashboard-thread-conflict-')); + const source = new ExecutionFixture(); const store = new Store(root); const saved = task(root); t.after(async () => { store.close(); await rm(root, { recursive: true, force: true }); }); store.saveTask(saved); @@ -51,6 +54,7 @@ test('a rejected task save retains both existing thread routes', async t => { test('outbox status selection preserves insertion order and isolates tasks', async t => { const root = await mkdtemp(join(tmpdir(), 'agent-dashboard-outbox-filter-')); + const source = new ExecutionFixture(); const store = new Store(root); const saved = task(root); t.after(async () => { store.close(); await rm(root, { recursive: true, force: true }); }); store.saveTask(saved); @@ -67,29 +71,15 @@ test('outbox status selection preserves insertion order and isolates tasks', asy assert.equal(store.hasOutputs(saved.id, []), false); }); -test('runtime event durability drops private output, samples deltas, and keeps durable messages after restart', async t => { - const root = await mkdtemp(join(tmpdir(), 'agent-dashboard-retention-')); - const store = new Store(root); const saved = task(root); store.saveTask(saved); - t.after(async () => { store.close(); await rm(root, { recursive: true, force: true }); }); - store.recordExecutionEvent(saved.id, event('agent_delta', 'first', 10)); - store.recordExecutionEvent(saved.id, event('agent_delta', 'second', 11)); - store.recordExecutionEvent(saved.id, event('tool_output', 'PRIVATE_OUTPUT'.repeat(5), 12)); - store.recordExecutionEvent(saved.id, event('agent_message', 'final answer', 13)); - assert.ok(store.executionEvents(saved.id).some(item => item.kind === 'tool_output'), 'ephemeral progress remains available in the owning process'); - const readOnly = new Store(root, { readOnly: true }); - assert.equal(readOnly.executionEvents(saved.id).some(item => item.kind === 'tool_output'), false); - assert.equal(readOnly.executionEvents(saved.id).filter(item => item.kind === 'agent_delta').length, 1); - assert.equal(readOnly.executionEvents(saved.id).find(item => item.kind === 'agent_message')?.text, 'final answer'); - readOnly.close(); -}); test('diagnostics roll up duplicates and data_version invalidates the dashboard read model', async t => { const root = await mkdtemp(join(tmpdir(), 'agent-dashboard-cache-')); + const source = new ExecutionFixture(); const store = new Store(root); const saved = task(root); store.saveTask(saved); t.after(async () => { store.close(); await rm(root, { recursive: true, force: true }); }); for (let index = 0; index < diagnosticRetentionLimit + 100; index++) store.diagnostic('scope', 'same failure'); assert.equal(store.diagnostics().filter(item => item.scope === 'scope' && item.message === 'same failure').length, 1); - const dashboard = await startDashboard({ dataDir: root, port: 0, host: '127.0.0.1' }); + const dashboard = await startDashboard({ execution: source.execution, dataDir: root, port: 0, host: '127.0.0.1' }); t.after(async () => { await dashboard.close(); }); const first = await (await fetch(dashboard.url + '/api/snapshot')).json(); saved.updatedAt = 2; saved.reason = 'changed'; store.saveTask(saved); @@ -97,41 +87,17 @@ test('diagnostics roll up duplicates and data_version invalidates the dashboard assert.equal(first.tasks[0].reason, null); assert.equal(second.tasks[0].reason, 'changed'); }); -test('durable execution history compacts per task without exceeding its event budget', async t => { - const root = await mkdtemp(join(tmpdir(), 'agent-dashboard-events-')); - const store = new Store(root); const saved = task(root); store.saveTask(saved); - t.after(async () => { store.close(); await rm(root, { recursive: true, force: true }); }); - for (let index = 0; index < 2_050; index++) store.recordExecutionEvent(saved.id, event('turn_status', 'running', index + 1)); - const readOnly = new Store(root, { readOnly: true }); - assert.ok(readOnly.executionEvents(saved.id).length <= executionEventRetentionLimit); - readOnly.close(); -}); - -test('retention trims the 2002-row final-plus-sampled-delta case to the exact durable budget', async t => { - const root = await mkdtemp(join(tmpdir(), 'agent-dashboard-retention-bound-')); - const store = new Store(root); const saved = task(root); store.saveTask(saved); - t.after(async () => { store.close(); await rm(root, { recursive: true, force: true }); }); - store.recordExecutionEvent(saved.id, event('agent_delta', 'sampled delta', 10)); - store.recordExecutionEvent(saved.id, event('agent_message', 'final answer', 11)); - for (let index = 0; index < executionEventRetentionLimit; index++) store.recordExecutionEvent(saved.id, event('turn_status', 'running', index + 12)); - const db = new DatabaseSync(join(root, 'tasks.sqlite')); - try { - const count = (db.prepare('SELECT COUNT(*) AS count FROM execution_events WHERE task_id=?').get(saved.id) as { count: number }).count; - assert.equal(count, executionEventRetentionLimit); - const final = db.prepare("SELECT payload FROM execution_events WHERE task_id=? AND json_extract(payload,'$.kind')='agent_message'").get(saved.id) as { payload: string } | undefined; - assert.equal(JSON.parse(final!.payload).text, 'final answer'); - } finally { db.close(); } -}); test('Dashboard v1 timeline exposes typed visible events and omits private tool output', async t => { const root = await mkdtemp(join(tmpdir(), 'agent-dashboard-v1-timeline-')); + const source = new ExecutionFixture(); const store = new Store(root); const saved = task(root); store.saveTask(saved); t.after(async () => { store.close(); await rm(root, { recursive: true, force: true }); }); - store.recordExecutionEvent(saved.id, event('turn_start', 'inProgress', 10)); - store.recordExecutionEvent(saved.id, { ...event('agent_message', 'Visible output', 11), label: '最终回答' }); - store.recordExecutionEvent(saved.id, event('tool_output', 'PRIVATE_OUTPUT', 12)); - const dashboard = await startDashboard({ dataDir: root, port: 0, host: '127.0.0.1' }); + source.publish(saved.id, event('turn_start', 'inProgress', 10)); + source.publish(saved.id, { ...event('agent_message', 'Visible output', 11), label: '最终回答' }); + source.publish(saved.id, event('tool_output', 'PRIVATE_OUTPUT', 12)); + const dashboard = await startDashboard({ execution: source.execution, dataDir: root, port: 0, host: '127.0.0.1' }); t.after(async () => dashboard.close()); const detail = await (await fetch(dashboard.url + '/api/tasks/' + saved.id)).json(); assert.ok(detail.timeline.some((entry: { type: string }) => entry.type === 'PHASE')); @@ -141,9 +107,10 @@ test('Dashboard v1 timeline exposes typed visible events and omits private tool test('Dashboard v1 API remains under the 500 ms interface budget with a retained event fixture', async t => { const root = await mkdtemp(join(tmpdir(), 'agent-dashboard-v1-api-')); + const source = new ExecutionFixture(); const store = new Store(root); const saved = task(root); store.saveTask(saved); - for (let index = 0; index < 500; index++) store.recordExecutionEvent(saved.id, event('turn_status', 'running', index + 1)); - const dashboard = await startDashboard({ dataDir: root, port: 0, host: '127.0.0.1' }); + for (let index = 0; index < 500; index++) source.publish(saved.id, event('turn_status', 'running', index + 1)); + const dashboard = await startDashboard({ execution: source.execution, dataDir: root, port: 0, host: '127.0.0.1' }); t.after(async () => { await dashboard.close(); store.close(); await rm(root, { recursive: true, force: true }); }); const samples: number[] = []; for (let index = 0; index < 12; index++) { @@ -158,3 +125,18 @@ test('Dashboard v1 API remains under the 500 ms interface budget with a retained console.log(JSON.stringify({ benchmark: 'dashboard-v1-interface', count: samples.length, p50Ms: Number(samples[Math.floor(samples.length / 2)]!.toFixed(3)), p95Ms: Number(p95.toFixed(3)), maxMs: Number(samples.at(-1)!.toFixed(3)), thresholdMs: 500 })); assert.ok(p95 < 500, `dashboard p95 ${p95.toFixed(3)}ms exceeded 500ms`); }); + +test('execution is process local and fresh databases contain only business and delivery records', async t => { + const root = await mkdtemp(join(tmpdir(), 'agent-no-trace-')); + const store = new Store(root); store.saveTask(task(root)); + const source = new ExecutionFixture(); + const db = new DatabaseSync(join(root, 'tasks.sqlite')); + t.after(async () => { db.close(); store.close(); await rm(root, { recursive: true, force: true }); }); + const version = db.prepare('PRAGMA data_version').get(); + source.publish('task', event('agent_message', 'TRACE_SENTINEL')); + assert.deepEqual(db.prepare('PRAGMA data_version').get(), version); + assert.equal(db.prepare("SELECT name FROM sqlite_master WHERE name='execution_events'").get(), undefined); + source.restart(); + assert.deepEqual(source.execution.current('task'), []); + assert.equal((await source.execution.read('task', 'thread')).entries[0]?.text, 'TRACE_SENTINEL'); +}); diff --git a/test/dashboard-recovery.test.ts b/test/dashboard-recovery.test.ts new file mode 100644 index 0000000..ccb884a --- /dev/null +++ b/test/dashboard-recovery.test.ts @@ -0,0 +1,103 @@ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { createContext, runInContext } from 'node:vm'; +import { dashboardScript } from '../src/dashboard-page.js'; + +class Element { + childNodes: Element[] = []; + textContent = ''; + className = ''; + value = ''; + hidden = false; + checked = false; + dataset = {}; + classList = { toggle() {} }; + append(...children: Element[]) { this.childNodes.push(...children); } + replaceChildren(...children: Element[]) { this.childNodes = children; } + addEventListener() {} + removeAttribute() {} +} + +async function browser(options: { threadId: string | null; unavailable?: boolean }) { + const elements = new Map(); + const element = (id: string) => { let result = elements.get(id); if (!result) { result = new Element(); elements.set(id, result); } return result; }; + const requests: string[] = []; + const streams: Stream[] = []; + class Stream { + closed = false; + listeners = new Map void>(); + constructor(readonly url: string) { streams.push(this); } + close() { this.closed = true; } + addEventListener(name: string, listener: (message: { data: string }) => void) { this.listeners.set(name, listener); } + emit(name: string, value: unknown = {}) { this.listeners.get(name)?.({ data: JSON.stringify(value) }); } + } + let now = 0, unavailable = options.unavailable ?? false; + const task = { id: 'task', title: 'Task', threadId: options.threadId, status: 'running', role: 'worker', updatedAt: 0 }; + const context = createContext({ + document: { getElementById: element, createElement: () => new Element(), createDocumentFragment: () => new Element(), querySelectorAll: () => [] }, + EventSource: Stream, + Date: class extends Date { static override now() { return now; } }, + window: { addEventListener() {}, setInterval() {}, setTimeout() {} }, + fetch: async (url: string) => { + requests.push(url); + return { ok: true, status: 200, json: async () => url === '/api/snapshot' + ? { tasks: [task], projects: [], now, runtime: null, dataDir: '/test' } + : { task, threadId: task.threadId, cursor: 'epoch:0', history: { kind: unavailable ? 'unavailable' : 'available' }, timeline: unavailable ? [] : [{ id: 'historical', kind: 'agent_message', text: 'Recovered answer', createdAt: 0 }], inputs: [], outputs: [], diagnostics: [] } }; + }, + }); + runInContext(dashboardScript, context); + await new Promise(resolve => setImmediate(resolve)); + return { + streams, requests, element, + recover() { unavailable = false; }, + rebind(threadId: string) { task.threadId = threadId; unavailable = true; }, + async refresh(elapsed = 0) { now += elapsed; await runInContext('refresh()', context); }, + }; +} + +test('the selected bound task retries unavailable history with backoff and recovers without another selection', async () => { + const page = await browser({ threadId: 'thread', unavailable: true }); + const reads = () => page.requests.filter(url => url === '/api/tasks/task').length; + assert.equal(reads(), 1); + await page.refresh(500); assert.equal(reads(), 1); + await page.refresh(500); assert.equal(reads(), 2); + await page.refresh(1000); assert.equal(reads(), 2); + page.recover(); + await page.refresh(1000); assert.equal(reads(), 3); + assert.equal(page.element('page-error').hidden, true); + assert.equal(page.element('event-count').textContent, '1 条事件'); + await page.refresh(60000); assert.equal(reads(), 3); +}); + +test('reset notifications do not create an immediate detail subscription loop for an unbound task', async () => { + const page = await browser({ threadId: null }); + assert.equal(page.streams.length, 1); + const first = page.streams[0]!; + assert.match(first.url, /threadId=$/); + first.emit('reset'); first.emit('reset'); + await page.refresh(999); + assert.equal(page.streams.length, 1); + await page.refresh(1); + assert.equal(page.streams.length, 2); + assert.equal(first.closed, true); + await page.refresh(60000); + assert.equal(page.streams.length, 2); +}); + +test('live item upserts retain at most five hundred visible timeline entries', async () => { + const page = await browser({ threadId: 'thread' }); + const stream = page.streams[0]!; + for (let index = 0; index < 510; index++) stream.emit('execution', { taskId: 'task', key: String(index), turnId: 'turn', kind: 'agent_message', text: 'Message ' + index, createdAt: index }); + assert.equal(page.element('event-count').textContent, '500 条事件'); + stream.emit('execution', { taskId: 'task', key: '509', turnId: 'turn', kind: 'agent_message', text: 'Updated message', createdAt: 509 }); + assert.equal(page.element('event-count').textContent, '500 条事件'); +}); + +test('unavailable history after Thread rebind does not preserve the previous Thread timeline', async () => { + const page = await browser({ threadId: 'thread-a' }); + assert.equal(page.element('event-count').textContent, '1 条事件'); + page.rebind('thread-b'); + await page.refresh(1000); + assert.equal(page.element('event-count').textContent, '0 条事件'); + assert.equal(page.element('page-error').hidden, false); +}); diff --git a/test/dashboard.test.ts b/test/dashboard.test.ts index 688a600..ed39620 100644 --- a/test/dashboard.test.ts +++ b/test/dashboard.test.ts @@ -17,7 +17,7 @@ import { AgentLoop } from '../src/loop.js'; import { configSchema } from '../src/config.js'; import { CodexAppServer } from './helpers/codex.js'; import { Store } from '../src/store.js'; -import { storedExecutionEventSchema, executionEvent } from '../src/execution-events.js'; +import { executionEventSchema, executionEvent } from '../src/execution-events.js'; import { CodexFake, FeishuFake, GitHubFake } from './fakes.js'; import { WorkspaceProvider } from '../src/workspace.js'; @@ -27,7 +27,7 @@ test('dashboard projects discovered resource identity and workspace without repo const config = configSchema.parse({ execution: testExecution, dataDir: join(root, 'data'), engineering: { projectRoot }, projects: [testProject(join(projectRoot, 'local'))], feishu: { userOpenId: 'owner' } }); const codex = new SurfaceCodex(); const loop = new AgentLoop(config, { codex, feishu: new FeishuFake(), github: new GitHubFake(), workspace: new WorkspaceProvider(config.dataDir) }); - const dashboard = await startDashboard({ dataDir: config.dataDir, port: 0 }); + const dashboard = await startDashboard({ execution: loop.execution, dataDir: config.dataDir, port: 0 }); t.after(async () => { await dashboard.close(); await loop.close(); await rm(root, { recursive: true, force: true }); }); await restoreSurface(loop, { id: 'dashboard-project-input', chatId: 'private', chatType: 'p2p', senderId: 'owner', text: 'Inspect the local project', attachments: [], createdAt: Date.now() }); @@ -102,23 +102,24 @@ test('group and dashboard titles share normalization while persisted manual sour } }); -test('real adapter to SQLite to SSE streams partial text, tools and capacity errors, resumes and isolates tasks', async t => { +test('real adapter through transient feed to SSE streams partial text, tools and capacity errors, resumes and isolates tasks', async t => { const root = await mkdtemp(join(tmpdir(), 'agent-dashboard-live-')); const config = configSchema.parse({ execution: testExecution, dataDir: root, projects: [testProject(root)], feishu: { userOpenId: 'owner' } }); const codex = new CodexAppServer({ command: [process.execPath, resolve('test/fixtures/debug-codex.mjs')], approvalPolicy: 'never', sandbox: 'read-only', requestTimeoutMs: 2000 }); const feishu = new FeishuFake(); const loop = new AgentLoop(config, { codex, feishu, github: new GitHubFake(), workspace: { async ensure(input) { if (input.kind === 'project-directory') return input.directory; throw new Error('No workspace needed'); }, async head() { return 'unused'; } } }); - const dashboard = await startDashboard({ dataDir: root, port: 0 }); + const dashboard = await startDashboard({ execution: loop.execution, dataDir: root, port: 0 }); const abort = new AbortController(); t.after(async () => { abort.abort(); await dashboard.close(); await loop.close(); await rm(root, { recursive: true, force: true }); }); const message = (id: string, text: string, chatId = 'private', chatType: 'p2p' | 'group' = 'p2p') => ({ id, text, chatId, chatType, senderId: 'owner', attachments: [], createdAt: Date.now() }); + const initialCursor = loop.execution.cursor; await restoreSurface(loop, message('one', 'First task')); await loop.tick(); await restoreSurface(loop, message('two', '')); await loop.tick(); const [first, second] = loop.tasks().filter(task => !task.sourceId.startsWith('project:')); assert.ok(first?.threadId && first.chatId && second?.threadId); assert.throws(() => new Store(root), /已有 Agent Loop/); const initial = await (await fetch(dashboard.url + '/api/snapshot')).json(); assert.equal(initial.tasks.length, 3); - const response = await fetch(dashboard.url + '/api/events?taskId=' + first.id, { signal: abort.signal }); + const response = await fetch(dashboard.url + '/api/events?taskId=' + first.id + '&after=' + encodeURIComponent(initialCursor), { signal: abort.signal }); assert.equal(response.status, 200); assert.ok(response.body); const reader = response.body.getReader(); const chunk = await reader.read(); @@ -126,31 +127,27 @@ test('real adapter to SQLite to SSE streams partial text, tools and capacity err assert.match(raw, /agent_delta/); assert.match(raw, /tool_start/); assert.doesNotMatch(raw, /tool_output/); assert.match(raw, /Selected model is at capacity/); assert.doesNotMatch(raw, /private reasoning/); assert.ok(!raw.includes(second.threadId)); - const before = loop.store.executionEvents(first.id); + const before = loop.execution.current(first.id); assert.equal(before.some(event => event.kind === 'agent_message'), false); assert.equal(before.some(event => event.kind === 'turn_end'), false); assert.equal(loop.tasks()[0]?.status, 'running'); + const resumeCursor = loop.execution.cursor; await restoreSurface(loop, message('finish', 'finish', first.chatId, 'group')); await loop.tick(); - const events = loop.store.executionEvents(first.id); + const events = loop.execution.current(first.id); assert.equal(events.filter(event => event.kind === 'agent_message').length, 1); assert.equal(events.at(-1)?.kind, 'turn_end'); assert.equal(events.find(event => event.kind === 'agent_message')?.text, '正在检查运行状态,校验已完成。'); assert.equal(feishu.sent.some(item => item.text.includes('Selected model is at capacity')), false); const cursor = before.at(-1)?.seq; assert.ok(cursor); - const resumed = await fetch(dashboard.url + '/api/events?taskId=' + first.id, { headers: { 'Last-Event-ID': String(cursor) }, signal: abort.signal }); + const resumed = await fetch(dashboard.url + '/api/events?taskId=' + first.id, { headers: { 'Last-Event-ID': resumeCursor }, signal: abort.signal }); assert.ok(resumed.body); const resumedChunk = await resumed.body.getReader().read(); const resumedRaw = new TextDecoder().decode(resumedChunk.value); - const resumedEvents = resumedRaw.split('\n').filter(line => line.startsWith('data: ')).map(line => storedExecutionEventSchema.parse(JSON.parse(line.slice(6)))); + const resumedEvents = resumedRaw.split('\n').filter(line => line.startsWith('data: ')).map(line => executionEventSchema.extend({ seq: z.number(), taskId: z.string() }).parse(JSON.parse(line.slice(6)))); assert.ok(resumedEvents.length > 0); assert.ok(resumedEvents.every(event => event.seq > cursor && event.taskId === first.id)); assert.equal(resumedEvents.at(-1)?.kind, 'turn_end'); assert.equal((await (await fetch(dashboard.url + '/api/tasks/' + first.id)).json()).outputs.some((row: { text: string }) => row.text === '正在检查运行状态,校验已完成。'), true); - const readOnly = new Store(root, { readOnly: true }); - const durable = readOnly.executionEvents(first.id); - assert.equal(durable.some(event => event.kind === 'tool_output'), false); - assert.equal(durable.filter(event => event.kind === 'agent_message').length, 1); - assert.equal(durable.at(-1)?.kind, 'turn_end'); readOnly.close(); assert.equal(loop.store.task(second.id).status, 'running'); }); diff --git a/test/execution-fixture.ts b/test/execution-fixture.ts new file mode 100644 index 0000000..48681a4 --- /dev/null +++ b/test/execution-fixture.ts @@ -0,0 +1,28 @@ +import { LiveExecution } from '../src/live-execution.js'; +import { executionKey, type ExecutionEvent, type ExecutionHistory } from '../src/execution-events.js'; + +export class ExecutionFixture { + readonly history = new Map(); + readonly reads: string[] = []; + unavailable = false; + execution = this.newFeed(); + private newFeed(): LiveExecution { return new LiveExecution({ readHistory: async threadId => { + this.reads.push(threadId); + if (this.unavailable) throw new Error('History unavailable'); + return structuredClone(this.history.get(threadId) ?? []); + } }); } + restart(): void { this.execution = this.newFeed(); } + publish(taskId: string, event: ExecutionEvent): void { + if (event.kind !== 'tool_output') { + const entries = this.history.get(event.threadId) ?? []; + const key = executionKey(event), index = entries.findIndex(item => item.key === key), previous = entries[index]; + if (event.kind === 'agent_delta' && !previous) this.execution.publish(taskId, { ...event, kind: 'agent_start', text: '' }); + if (!(event.kind === 'agent_delta' && previous?.kind === 'agent_message')) { + const item = { ...event, key, text: event.kind === 'agent_delta' ? (previous?.text ?? '') + event.text : event.text }; + if (index < 0) entries.push(item); else entries[index] = item; + this.history.set(event.threadId, entries); + } + } + this.execution.publish(taskId, event); + } +} diff --git a/test/fakes.ts b/test/fakes.ts index db9fa55..e24cba0 100644 --- a/test/fakes.ts +++ b/test/fakes.ts @@ -1,3 +1,4 @@ +import { executionKey, type ExecutionHistory } from '../src/execution-events.js'; import { writeFile } from 'node:fs/promises'; import { TurnChangedError, UncertainOperationError, type ApprovalAction, type ExecutionSnapshot, type IncomingMessage, type PendingRequest, type ProjectDecisionAction, type Source, type SourceComment, type TaskResource, type WorkspaceRequest } from '../src/domain.js'; export interface LegacyReport { status: 'waiting_user' | 'awaiting_review' | 'failed'; summary: string; question: string | null; prUrl: string | null; validation: string[]; limitations: string[] } @@ -95,6 +96,13 @@ export class FeishuFake implements FeishuPort { } export class CodexFake implements CodexPort { + async readHistory(threadId: string): Promise { + const snapshot = this.snapshot(threadId); + return snapshot.outputs.map(output => { + const event = { threadId, turnId: output.turnId, itemId: output.id, kind: 'agent_message' as const, label: '最终回答', text: output.text, createdAt: null }; + return { ...event, key: executionKey(event) }; + }); + } private completionPolicy: { isCompleted(threadId: string): boolean } | undefined; readonly nativeDenials: PendingRequest[] = []; uncertainNativeDenial = false; diff --git a/test/feishu-document-conversation.test.ts b/test/feishu-document-conversation.test.ts index 1472642..0cb8adf 100644 --- a/test/feishu-document-conversation.test.ts +++ b/test/feishu-document-conversation.test.ts @@ -1,7 +1,7 @@ import assert from 'node:assert/strict'; import { test } from 'node:test'; import { incomingSchema, taskSchema } from '../src/domain.js'; -import type { StoredExecutionEvent } from '../src/execution-events.js'; +import type { VisibleExecution } from '../src/execution-events.js'; import { documentConversationText, projectDocumentConversation } from '../src/feishu-document-conversation.js'; import type { Inbox } from '../src/store.js'; import { progressContent } from '../src/feishu-progress-view.js'; @@ -11,8 +11,8 @@ const task = taskSchema.parse({ id: 'task', sourceId: 'source', source: { kind: contextDirectory: '/tmp', resources: [], status: 'running', reason: null, prompt: '', promptHash: '', skillPath: null, chatId: 'chat', threadId: 'thread', turnId: 'second', executionStatus: 'running', lastHandledTurnId: null, requests: [], createdAt: 1, updatedAt: 10 }); -const event = (seq: number, kind: StoredExecutionEvent['kind'], text: string, turnId = 'first'): StoredExecutionEvent => - ({ seq, taskId: task.id, threadId: 'thread', turnId, itemId: 'same-id', kind, text, label: kind, createdAt: seq + 1 }); +const event = (seq: number, kind: VisibleExecution['kind'], text: string, turnId = 'first'): VisibleExecution => + ({ key: String(seq), seq, taskId: task.id, threadId: 'thread', turnId, itemId: 'same-id', kind, text, label: kind, createdAt: seq + 1 }); test('Feishu timestamps display UTC+8 across date and year boundaries without changing stored timestamps', () => { const createdAt = Date.parse('2026-12-31T16:30:00.123Z'); @@ -85,10 +85,10 @@ test('line retention removes oldest messages rather than newest messages in a de }); test('one-shot event iterable preserves exact messages, ordering, completion and metadata', () => { - const events: StoredExecutionEvent[] = [ + const events: VisibleExecution[] = [ { ...event(1, 'agent_delta', 'Discarded'), createdAt: 10 }, { ...event(2, 'agent_delta', 'Other\r'), itemId: 'interleaved', createdAt: 10 }, - { ...event(3, 'agent_delta', '\nline'), itemId: 'interleaved', createdAt: 8 }, + { ...event(3, 'agent_delta', 'Other\r\nline'), itemId: 'interleaved', createdAt: 8 }, { ...event(4, 'agent_message', ''), createdAt: 7 }, { ...event(5, 'agent_delta', 'Ignored after completion'), createdAt: 90 }, { ...event(6, 'agent_message', 'Second turn', 'second'), createdAt: 10 }, @@ -100,7 +100,7 @@ test('one-shot event iterable preserves exact messages, ordering, completion and { ...event(12, 'turn_end', 'failed'), createdAt: 120 }, ]; let iterations = 0; - const once: Iterable = { + const once: Iterable = { [Symbol.iterator]() { assert.equal(++iterations, 1); return events.values(); }, }; const streamed = projectDocumentConversation(task, [], once); diff --git a/test/feishu-document-progress.test.ts b/test/feishu-document-progress.test.ts index 3f2a4db..8d38c2b 100644 --- a/test/feishu-document-progress.test.ts +++ b/test/feishu-document-progress.test.ts @@ -1,3 +1,4 @@ +import { ExecutionFixture } from './execution-fixture.js'; import { testExecution } from './test-config.js'; import { SurfaceCodex, testProject, restoreSurface, tickSurface } from './coordinated-surfaces.js'; import assert from 'node:assert/strict'; @@ -82,17 +83,18 @@ async function fixture(t: TestContext, initialize = true) { const directory = await realpath(await mkdtemp(join(tmpdir(), 'document-progress-'))); let store = new Store(directory), now = Date.UTC(2026, 10, 7); const transport = new Documents(); - let publisher = new FeishuDocumentProgressPublisher({ store, transport, documentBaseUrl: baseUrl, intervalMs: 1000, now: () => now }); + const source = new ExecutionFixture(); + let publisher = new FeishuDocumentProgressPublisher({ store, execution: source.execution, transport, documentBaseUrl: baseUrl, intervalMs: 1000, now: () => now }); t.after(async () => { await publisher.close(); store.close(); await rm(directory, { recursive: true, force: true }); }); if (initialize) await publisher.poll(); - return { directory, transport, get store() { return store; }, get publisher() { return publisher; }, + return { source, directory, transport, get store() { return store; }, get publisher() { return publisher; }, event(kind: ExecutionEvent['kind'], text: string, changes: Partial = {}) { - store.recordExecutionEvent('task', { threadId: 'thread-task', turnId: 'turn', itemId: 'item', kind, text, label: kind, createdAt: now, ...changes }); + source.publish('task', { threadId: 'thread-task', turnId: 'turn', itemId: 'item', kind, text, label: kind, createdAt: now, ...changes }); }, advance(ms = 3000) { now += ms; }, async pump(count = 1) { for (let i = 0; i < count; i++) { await publisher.poll(); now += 3000; } }, record(id = 'task') { return documentPublicationSchema.parse(store.documentProgressRecord(id)); }, - async restart(options: { maxLines?: number } = {}) { await publisher.close(); store.close(); store = new Store(directory); publisher = new FeishuDocumentProgressPublisher({ store, transport, documentBaseUrl: baseUrl, now: () => now, ...options }); }, + async restart(options: { maxLines?: number } = {}) { await publisher.close(); store.close(); store = new Store(directory); source.restart(); publisher = new FeishuDocumentProgressPublisher({ store, execution: source.execution, transport, documentBaseUrl: baseUrl, now: () => now, ...options }); }, }; } @@ -139,7 +141,7 @@ test('first enable excludes completed history across restart, but captures fast f.event('agent_message', 'Fast current', { turnId: 'fast' }); await f.pump(); f.store.saveTask({ ...task('task', 'fast'), executionStatus: 'completed', status: 'idle' }); await f.pump(5); assert.match([...f.transport.blocks.values()][0]!, /Fast current/); - assert.equal(f.record().view.status, 'completed'); + assert.match(f.transport.text(), /已完成/); }); test('pending native requests win stale completion/running events and task snapshot states', async t => { @@ -147,7 +149,7 @@ test('pending native requests win stale completion/running events and task snaps f.store.saveTask({ ...task(), executionStatus: 'completed', requests: [{ id: 'request', threadId: 'thread-task', turnId: 'turn', kind: 'approval', method: 'item/commandExecution/requestApproval', prompt: 'permission', isBlocking: true, connectionEpoch: 'epoch', payload: {} }] }); f.event('turn_end', 'completed'); await f.pump(); - assert.equal(f.record().view.status, 'waiting_approval'); + await f.pump(5); assert.match(f.transport.text(), /等待审批/); }); test('unknown create is durably pending before dispatch and never creates again after restart', async t => { @@ -189,14 +191,14 @@ test('new events and turn resets during writes survive stale acknowledgement; up f.event('agent_message', 'Sending old view'); const hold = gate(); f.transport.hold = { kind: 'write', gate: hold }; const sending = f.publisher.poll(); - await Promise.resolve(); + for (let step = 0; step < 20; step++) await Promise.resolve(); f.store.saveTask(task('task', 'second')); f.event('agent_message', 'Newest view', { turnId: 'second' }); await f.publisher.poll(); const revision = f.record().projectionRevision; assert.notEqual(revision, null); hold.resolve(); await sending; f.transport.hold = null; assert.deepEqual(f.record().projectionRevision, revision); - assert.match(f.record().text, /Newest view[\s\S]*Sending old view/); + await f.pump(3); assert.match(f.transport.text(), /Newest view[\s\S]*Sending old view/); const count = f.transport.count('write'); await f.publisher.poll(); f.advance(2999); await f.publisher.poll(); assert.equal(f.transport.count('write'), count); f.advance(1); await f.pump(); @@ -260,7 +262,7 @@ for (const when of ['before', 'after'] as const) { if (record.delivery.kind !== 'ready') return; if (when === 'after') { assert.ok(f.transport.text().includes(long)); - assert.equal(f.transport.count('block'), documentChunks(record.text).length); + assert.equal(f.transport.count('block'), documentChunks(f.transport.text()).length); f.event('agent_message', 'Short replacement'); await f.pump(8); assert.ok(f.transport.text().includes('Short replacement')); assert.equal(f.transport.children.get('block-doc-1')?.length, 0); @@ -272,47 +274,11 @@ for (const when of ['before', 'after'] as const) { }); } -test('legacy pending content is reconciled before conversation history replaces it', async t => { - const f = await fixture(t); f.store.saveTask(task()); await f.pump(5); - const record = f.record(); assert.equal(record.delivery.kind, 'ready'); - if (record.delivery.kind !== 'ready') return; - const legacy = { ...record, delivery: { ...record.delivery, write: { clientToken: 'legacy-token', text: 'Legacy pending text', sent: true } } }; - f.store.saveDocumentProgressRecord('task', legacy); - f.event('agent_message', 'Restored conversation'); - await f.restart(); await f.pump(5); - assert.match(f.transport.text(), /Restored conversation/); - assert.equal(f.transport.count('document'), 1); - assert.ok(f.transport.calls.some(call => call.kind === 'write' && JSON.stringify(call.input).includes('legacy-token'))); -}); -test('legacy publications rebuild once and unchanged publications skip history across restart', async t => { - const f = await fixture(t); f.store.saveTask(task()); f.event('agent_message', 'Retained answer'); - await f.pump(5); - const before = f.record(); - const { projectionRevision, ...legacy } = before; - assert.notEqual(projectionRevision, null); - assert.equal(documentPublicationSchema.parse(legacy).projectionRevision, null); - f.store.saveDocumentProgressRecord('task', legacy); - await f.restart(); - const events = t.mock.method(f.store, 'documentConversationEvents'); - const inputs = t.mock.method(f.store, 'inputs'); - await f.pump(3); - assert.equal(events.mock.callCount(), 1); assert.equal(inputs.mock.callCount(), 1); - assert.deepEqual(f.record(), before); - f.store.saveTask({ ...task(), updatedAt: 12345, prompt: 'Changed engineering guidance', promptHash: 'changed' }); - await f.pump(); - assert.equal(events.mock.callCount(), 1); assert.equal(inputs.mock.callCount(), 1); - await f.restart(); - const restartedEvents = t.mock.method(f.store, 'documentConversationEvents'); - const restartedInputs = t.mock.method(f.store, 'inputs'); - await f.pump(2); - assert.equal(restartedEvents.mock.callCount(), 0); assert.equal(restartedInputs.mock.callCount(), 0); - assert.deepEqual(f.record(), before); -}); test('input-only changes retain original provenance, attachments and CLI submissions without execution events', async t => { const f = await fixture(t); f.store.saveTask(task()); await f.pump(5); - assert.deepEqual(f.store.documentConversationPosition('task'), { eventSeq: 0, inputSeq: 0 }); + assert.equal(f.store.inputSequence('task'), 0); const original = incomingSchema.parse({ id: 'reply', chatId: 'chat-task', senderId: 'owner', text: 'Original directory reply', attachments: [{ key: 'file-key', kind: 'file', name: 'evidence.txt' }], createdAt: 2 }); f.store.receive('task', { ...original, text: 'PRIVATE_EXPANDED_PROMPT', attachments: [] }, 'user', @@ -326,24 +292,23 @@ test('input-only changes retain original provenance, attachments and CLI submiss await f.pump(); assert.match(f.transport.text(), /CLI 提交[^\n]*\nCLI continuation/); assert.notDeepEqual(f.record().projectionRevision, userRevision); - assert.equal(f.record().projectionRevision?.eventSeq, 0); f.store.markInput('reply', 'delivered'); - const reads = t.mock.method(f.store, 'documentConversationEvents'); + const reads = t.mock.method(f.source.execution, 'read'); await f.pump(); assert.equal(reads.mock.callCount(), 0); }); test('status, turn and thread identity changes invalidate projection without new events', async t => { const f = await fixture(t); f.store.saveTask(task()); f.event('turn_end', 'completed'); await f.pump(5); - assert.equal(f.record().view.status, 'completed'); - const reads = t.mock.method(f.store, 'documentConversationEvents'); + assert.match(f.transport.text(), /已完成/); + const reads = t.mock.method(f.source.execution, 'read'); await f.pump(); assert.equal(reads.mock.callCount(), 0, 'terminal event and running snapshot remain stable'); f.store.saveTask({ ...task(), status: 'paused' }); await f.pump(2); - assert.equal(f.record().view.status, 'paused'); assert.equal(reads.mock.callCount(), 1); + assert.match(f.transport.text(), /已暂停/); assert.equal(reads.mock.callCount(), 1); f.store.saveTask(task('task', 'second')); await f.pump(2); - assert.equal(f.record().turnId, 'second'); assert.equal(f.record().view.status, 'running'); + assert.equal(f.record().turnId, 'second'); assert.match(f.transport.text(), /执行中/); assert.equal(reads.mock.callCount(), 2); f.store.saveTask({ ...task(), threadId: 'new-thread' }); await f.pump(2); - assert.equal(f.record().view.status, 'running'); assert.equal(reads.mock.callCount(), 3); + assert.match(f.transport.text(), /执行中/); assert.equal(reads.mock.callCount(), 3); }); test('manual source changes invalidate projection and a new chat binding creates a separate document', async t => { @@ -356,7 +321,7 @@ test('manual source changes invalidate projection and a new chat binding creates await f.pump(); assert.equal(f.record().chatId, 'other-chat'); assert.equal(f.transport.documents.length, 2); - assert.equal(f.store.meta('retired-document-progress:task:chat-task'), JSON.stringify(before)); + assert.deepEqual(JSON.parse(f.store.meta('retired-document-progress:task:chat-task')!), before); }); test('dissolved and absent chats stop saved document publications without affecting execution', async t => { @@ -382,7 +347,7 @@ test('retiring a task group during unknown-write readback prevents a later write const writes = f.transport.count('write'); const hold = gate(); f.transport.hold = { kind: 'read', gate: hold }; const reading = f.publisher.poll(); - await Promise.resolve(); + for (let step = 0; step < 20; step++) await Promise.resolve(); assert.ok(f.transport.count('read') > 0); f.store.saveTask({ ...f.store.task('task'), chatId: null }); hold.resolve(); await reading; f.transport.hold = null; @@ -421,91 +386,21 @@ test('group retirement cancels document writes still waiting in the real adapter assert.ok(patches.every(patch => !patch.retired)); }); -test('maxLines and renderer revision changes re-render unchanged histories once', async t => { - const f = await fixture(t); f.store.saveTask(task()); - f.event('agent_message', Array.from({ length: 20 }, (_, i) => `line-${i}`).join('\n')); await f.pump(5); - await f.restart({ maxLines: 8 }); - const reads = t.mock.method(f.store, 'documentConversationEvents'); - await f.pump(2); - assert.equal(f.record().text.split('\n').length, 8); assert.equal(f.record().projectionRevision?.maxLines, 8); - assert.equal(reads.mock.callCount(), 1); assert.equal(f.transport.text(), f.record().text); - const before = f.record(); assert.ok(before.projectionRevision); - f.store.saveDocumentProgressRecord('task', { ...before, projectionRevision: { ...before.projectionRevision, rendererVersion: 0 } }); - await f.pump(2); - assert.equal(reads.mock.callCount(), 2); assert.deepEqual(f.record(), before); -}); - -test('unchanged document publications reuse validation and still publish new progress', async t => { + +test('settled document publications release text and avoid idle history reads', async t => { const f = await fixture(t); f.store.saveTask(task()); f.event('agent_message', 'First progress'); await f.pump(6); const before = f.store.documentProgressRecord('task'); const calls = f.transport.calls.length; - const parsing = t.mock.method(documentPublicationSchema, 'parse'); - const cloning = t.mock.method(globalThis, 'structuredClone'); + const reads = f.source.reads.length; await f.pump(3); - assert.equal(parsing.mock.callCount(), 0); - assert.equal(cloning.mock.calls.filter(call => { - const value: unknown = call.arguments[0]; - return value !== null && typeof value === 'object' && 'delivery' in value && 'projectionRevision' in value; - }).length, 0); + assert.equal(f.source.reads.length, reads); assert.deepEqual(f.store.documentProgressRecord('task'), before); assert.equal(f.transport.calls.length, calls); f.event('agent_message', 'Updated progress', { itemId: 'next' }); await f.pump(2); - assert.ok(parsing.mock.callCount() > 0); assert.match(f.transport.text(), /Updated progress/); assert.equal(f.transport.count('document'), 1); }); -test('publication cache observes external payload replacement without a source revision change', async t => { - const f = await fixture(t); f.store.saveTask(task()); f.event('agent_message', 'Original'); await f.pump(6); - const previous = f.record(); - const db = new DatabaseSync(join(f.directory, 'tasks.sqlite')); - try { - db.prepare('UPDATE feishu_document_progress SET payload=? WHERE task_id=?') - .run(JSON.stringify({ ...previous, text: 'Externally corrected content' }), 'task'); - } finally { db.close(); } - await f.pump(2); - assert.equal(f.transport.text(), 'Externally corrected content'); - assert.deepEqual(f.record().projectionRevision, previous.projectionRevision); - assert.equal(f.transport.count('document'), 1); -}); - -test('source positions are per task and include tool events without moving the conversation cursor', async t => { - const f = await fixture(t); f.store.saveTask(task()); f.store.saveTask(task('other')); - f.event('agent_message', 'Answer'); await f.pump(5); - const before = f.record(); f.event('tool_output', 'PRIVATE_TOOL'); - f.store.recordExecutionEvent('other', { kind: 'agent_message', threadId: 'thread-other', turnId: 'turn', itemId: 'other', text: 'Other', label: 'message', createdAt: 10 }); - await f.pump(); - assert.equal(f.record().projectionRevision?.eventSeq, 2); - assert.equal(f.store.documentConversationPosition('other').eventSeq, 3); - assert.equal(f.record().view.cursor, before.view.cursor); assert.equal(f.record().text, before.text); -}); -test('invalid persisted events roll back all projections and discovery position until repaired', async t => { - const f = await fixture(t); f.store.saveTask(task()); f.store.saveTask(task('other')); await f.pump(10); - const before = f.store.documentProgressRecords(), discovery = f.store.meta('feishu-document-progress:discovery:v1'); - f.event('agent_message', 'New task answer'); - const valid = { kind: 'agent_message', threadId: 'thread-other', turnId: 'turn', itemId: 'other', text: 'New other answer', label: 'message', createdAt: 10 } satisfies ExecutionEvent; - f.store.recordExecutionEvent('other', valid); - const db = new DatabaseSync(join(f.directory, 'tasks.sqlite')); - let badSequence: number | bigint; - try { - badSequence = db.prepare('INSERT INTO execution_events(task_id,created_at,payload) VALUES(?,?,?)') - .run('other', 11, JSON.stringify({ ...valid, kind: 'agent_delta', text: 123 })).lastInsertRowid; - } finally { db.close(); } - const stream = f.store.documentConversationEvents('other'); - assert.deepEqual(stream.next(), { done: false, value: { ...valid, seq: 2, taskId: 'other' } }); - assert.throws(() => stream.next()); - const calls = f.transport.calls.length; - await f.pump(); - assert.deepEqual(f.store.documentProgressRecords(), before); - assert.equal(f.store.meta('feishu-document-progress:discovery:v1'), discovery); - assert.equal(f.transport.calls.length, calls); - assert.ok(f.store.diagnostics().some(row => row.scope === 'feishu-document-progress')); - const repair = new DatabaseSync(join(f.directory, 'tasks.sqlite')); - try { repair.prepare('DELETE FROM execution_events WHERE seq=?').run(badSequence); } finally { repair.close(); } - await f.pump(2); - assert.match(f.transport.text(), /New task answer/); assert.match(f.transport.text(), /New other answer/); - assert.equal(f.record('other').projectionRevision?.eventSeq, 2); -}); async function runtime(t: TestContext, enabled = true) { t.mock.timers.enable({ apis: ['Date', 'setInterval'], now: Date.UTC(2026, 10, 7) }); @@ -568,7 +463,7 @@ test('whole AgentLoop keeps final/native delivery working with missing document await f.loop.tick(); assert.ok(f.feishu.sent.some(message => message.text.includes('Which value?'))); f.codex.complete(current.threadId, 'Unchanged final'); await f.loop.tick(); assert.equal(f.feishu.sent.filter(message => message.text === 'Unchanged final').length, 1); assert.equal(f.cards, 0); - assert.ok(f.loop.store.diagnostics().some(row => row.scope === 'feishu-document-progress')); + assert.ok(f.loop.store.diagnostics().some(row => row.scope === current.id && row.message.includes('document.'))); }); test('whole AgentLoop first enable does not backfill historical completed tasks', async t => { @@ -581,3 +476,199 @@ test('whole AgentLoop first enable does not backfill historical completed tasks' assert.equal([...f.documentProgress.tabs.keys()].filter(key => JSON.parse(key).chatId === current.chatId).length, 1); assert.equal(f.cards, 0); }); + +test('settled and retired document receipts contain no transcript; unavailable history preserves the full remote document', async t => { + const f = await fixture(t); f.store.saveTask(task()); + const sentinel = 'UNIQUE_DOCUMENT_TRANSCRIPT_39617'; + f.event('agent_message', sentinel.repeat(4000)); await f.pump(30); + assert.match(f.transport.text(), /UNIQUE_DOCUMENT_TRANSCRIPT_39617/); + assert.doesNotMatch(JSON.stringify(f.record()), /UNIQUE_DOCUMENT_TRANSCRIPT|"text"|"view"|"chunks"/); + const full = f.transport.text(); + await f.restart(); f.source.unavailable = true; + f.event('agent_delta', 'only a live suffix', { itemId: 'new-item' }); await f.pump(3); + assert.equal(f.transport.text(), full); + f.source.unavailable = false; await f.pump(20); + assert.match(f.transport.text(), /only a live suffix/); + assert.match(f.transport.text(), /UNIQUE_DOCUMENT_TRANSCRIPT_39617/); + f.store.saveTask({ ...f.store.task('task'), chatId: 'replacement' }); await f.pump(30); + assert.doesNotMatch(f.store.meta('retired-document-progress:task:chat-task') ?? '', /UNIQUE_DOCUMENT_TRANSCRIPT|"text"|"view"|"chunks"/); + const db = new DatabaseSync(join(f.directory, 'tasks.sqlite')); + try { + for (const row of db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'").all()) { + const table = String(row.name); assert.match(table, /^[a-z_]+$/); + assert.doesNotMatch(JSON.stringify(db.prepare(`SELECT * FROM ${table}`).all()), /UNIQUE_DOCUMENT_TRANSCRIPT_39617/); + } + } finally { db.close(); } +}); + +test('lost append acknowledgment recovers its exact bounded payload before applying a changed projection', async t => { + const f = await fixture(t); f.store.saveTask(task()); await f.pump(5); + f.event('agent_message', 'APPEND_ORIGINAL_'.repeat(8000)); await f.pump(); + f.transport.fail = { kind: 'block', when: 'after', error: new Error('ACK lost') }; await f.pump(); + const pending = f.record().delivery; + assert.equal(pending.kind, 'ready'); + if (pending.kind !== 'ready' || pending.operation?.kind !== 'append') throw new Error('Expected append receipt'); + assert.ok(Buffer.byteLength(pending.operation.text) < 24000); + await f.restart(); f.event('agent_message', 'Changed after lost append ACK'); await f.pump(12); + assert.match(f.transport.text(), /Changed after lost append ACK/); + assert.doesNotMatch(f.transport.text(), /APPEND_ORIGINAL_/); + assert.equal(f.transport.count('block'), 2); + assert.doesNotMatch(JSON.stringify(f.record()), /APPEND_ORIGINAL_|"text"|"chunks"/); +}); + +test('shutdown drains an outstanding history read without creating or writing a document afterwards', async t => { + const f = await fixture(t); f.store.saveTask(task()); + const held = gate(), entered = gate(), read = f.source.execution.read.bind(f.source.execution); + t.mock.method(f.source.execution, 'read', async (...args: Parameters) => { entered.resolve(); await held.promise; return read(...args); }); + const polling = f.publisher.poll(); await entered.promise; + let closed = false; const closing = f.publisher.close().then(() => { closed = true; }); + await Promise.resolve(); assert.equal(closed, false); + held.resolve(); await Promise.all([closing, polling]); + assert.equal(f.transport.calls.length, 0); assert.equal(f.store.documentProgressRecord('task'), null); +}); + + +test('successful lagging history cannot erase published document and remains retryable', async t => { + const f = await fixture(t); f.store.saveTask(task()); + f.event('agent_message', 'Complete acknowledged history'); await f.pump(6); + const full = f.transport.text(), history = structuredClone(f.source.history.get('thread-task')!); + await f.restart(); f.source.history.set('thread-task', []); await f.pump(3); + assert.equal(f.transport.text(), full); + const beforeRecovery = f.source.reads.length; + f.source.history.set('thread-task', history); f.advance(60_000); await f.pump(3); + assert.ok(f.source.reads.length > beforeRecovery); assert.equal(f.transport.text(), full); + f.event('agent_message', 'Recovered update', { itemId: 'new' }); await f.pump(3); + assert.match(f.transport.text(), /Recovered update/); +}); + +test('lost append ACK reconciles and clears its exact payload during Codex outage with history backoff', async t => { + const f = await fixture(t); f.store.saveTask(task()); await f.pump(5); + f.event('agent_message', 'OUTAGE_APPEND_'.repeat(8000)); await f.pump(); + f.transport.fail = { kind: 'block', when: 'after', error: new Error('ACK lost') }; await f.pump(); + const pending = f.record().delivery; assert.equal(pending.kind, 'ready'); + if (pending.kind !== 'ready' || pending.operation?.kind !== 'append') throw new Error('Expected append'); + const text = f.transport.text(), reads = f.source.reads.length, finds = f.transport.count('findBlock'); + await f.restart(); f.source.unavailable = true; await f.pump(8); + assert.ok(f.transport.count('findBlock') > finds); + const recovered = f.record().delivery; assert.equal(recovered.kind, 'ready'); + if (recovered.kind !== 'ready') throw new Error('Expected ready'); + assert.equal(recovered.operation, null); + assert.doesNotMatch(f.store.documentProgressPayload('task')!, /OUTAGE_APPEND_/); + assert.equal(f.transport.text(), text); + assert.ok(f.source.reads.length - reads < 8); +}); + + +test('same-thread status changes during history read cannot publish a stale document', async t => { + const f = await fixture(t); f.store.saveTask(task()); + const held = gate(), entered = gate(), read = f.source.execution.read.bind(f.source.execution); + const mocked = t.mock.method(f.source.execution, 'read', async (...args: Parameters) => { entered.resolve(); await held.promise; return read(...args); }); + const polling = f.publisher.poll(); await entered.promise; + f.store.saveTask({ ...task(), status: 'paused' }); held.resolve(); await polling; + assert.equal(f.transport.documents.length, 0); mocked.mock.restore(); await f.pump(6); + assert.match(f.transport.text(), /已暂停/); +}); + + +test('oversized single-line Unicode history is delivered in full without retaining its projection', async t => { + const f = await fixture(t); f.store.saveTask(task()); + const text = '长文🙂'.repeat(260_000) + 'FINAL_UNICODE_SUFFIX'; + f.event('agent_message', text); await f.pump(); + const cache: unknown = Reflect.get(f.publisher, 'publications'); assert.ok(cache instanceof Map); assert.equal(cache.size, 0); + await f.pump(documentChunks(text).length + 8); + assert.ok(f.transport.text().includes(text)); + assert.equal(cache.size, 0); + const reads = f.source.reads.length; await f.pump(3); assert.equal(f.source.reads.length, reads); +}); + + +test('disabled and retired document writes clear pending payloads and safe diagnostics identify the task', async t => { + const f = await fixture(t); f.store.saveTask(task()); await f.pump(6); + const sentinel = 'DISABLED_DOCUMENT_TRACE_91353'; f.event('agent_message', sentinel); + f.transport.fail = { kind: 'write', when: 'before', error: new TaskGroupUnavailable(230002, sentinel) }; await f.pump(); + assert.equal(f.record().delivery.kind, 'disabled'); + assert.ok(f.store.diagnostics().some(row => row.scope === 'task' && row.message.includes('230002') && row.message.includes('disabled'))); + f.store.saveTask({ ...task(), chatId: 'replacement' }); await f.pump(6); + const db = new DatabaseSync(join(f.directory, 'tasks.sqlite')); + try { + for (const row of db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'").all()) { + const table = String(row.name); assert.match(table, /^[a-z_]+$/); + assert.doesNotMatch(JSON.stringify(db.prepare(`SELECT * FROM ${table}`).all()), new RegExp(sentinel)); + } + } finally { db.close(); } +}); + + +test('saved pending append reconciles before a stalled Codex read returns', async t => { + const f = await fixture(t); f.store.saveTask(task()); await f.pump(5); + f.event('agent_message', 'STALL_PENDING_'.repeat(8000)); await f.pump(); + f.transport.fail = { kind: 'block', when: 'after', error: new Error('ACK lost') }; await f.pump(); await f.restart(); + const held = gate(), entered = gate(), read = f.source.execution.read.bind(f.source.execution); + t.mock.method(f.source.execution, 'read', async (...args: Parameters) => { entered.resolve(); await held.promise; return read(...args); }); + const finds = f.transport.count('findBlock'), polling = f.publisher.poll(); await entered.promise; + try { + for (let n = 0; n < 20; n++) await Promise.resolve(); + assert.ok(f.transport.count('findBlock') > finds); + const delivery = f.record().delivery; assert.equal(delivery.kind, 'ready'); + if (delivery.kind !== 'ready') throw new Error('Expected ready'); + assert.equal(delivery.operation, null); + assert.doesNotMatch(f.store.documentProgressPayload('task')!, /STALL_PENDING_/); + } finally { held.resolve(); await polling; } +}); + +test('document close before the first poll continuation prevents new history reads', async t => { + const f = await fixture(t); f.store.saveTask(task()); + const reads = f.source.reads.length; + const polling = f.publisher.poll(); + await f.publisher.close(); await polling; + assert.equal(f.source.reads.length, reads); + assert.equal(f.transport.calls.length, 0); +}); + +test('transient retry errors do not prevent document recovery after restart', async t => { + const f = await fixture(t); f.store.saveTask(task()); + f.event('agent_message', 'Initial answer'); + f.source.execution.publish('task', { threadId: 'thread-task', turnId: 'turn', itemId: null, kind: 'error', text: 'temporary retry', label: 'retry', createdAt: 1 }); + await f.pump(6); await f.restart(); + f.event('agent_message', 'Recovered answer', { itemId: 'recovered' }); + await f.pump(12); + assert.match(f.transport.text(), /Recovered answer/); +}); + +test('pending document text is cancelled when its Thread is unbound', async t => { + const f = await fixture(t); f.store.saveTask(task()); await f.pump(5); + const sentinel = 'TRACE_UNBOUND_PENDING_BODY'; + f.event('agent_message', sentinel); + f.transport.fail = { kind: 'write', when: 'before', error: new Error('lost ACK') }; + await f.pump(); + assert.match(f.store.documentProgressPayload('task')!, new RegExp(sentinel)); + f.store.saveTask({ ...f.store.task('task'), threadId: null, turnId: null }); + await f.pump(); + assert.equal(f.record().delivery.kind, 'disabled'); + const db = new DatabaseSync(join(f.directory, 'tasks.sqlite'), { readOnly: true }); + try { assert.doesNotMatch(JSON.stringify(db.prepare('SELECT payload FROM feishu_document_progress').all()), new RegExp(sentinel)); } + finally { db.close(); } +}); + +test('a new Thread in the same chat resumes the ready document with current history', async t => { + const f = await fixture(t); f.store.saveTask(task()); f.event('agent_message', 'Old Thread answer'); await f.pump(6); + f.store.saveTask({ ...f.store.task('task'), threadId: 'replacement-thread', turnId: 'replacement-turn' }); + f.event('agent_message', 'Replacement Thread answer', { threadId: 'replacement-thread', turnId: 'replacement-turn' }); + await f.pump(15); + assert.equal(f.record().delivery.kind, 'ready'); + assert.match(f.transport.text(), /Replacement Thread answer/); + assert.doesNotMatch(f.transport.text(), /Old Thread answer/); + assert.equal(f.transport.documents.length, 1); +}); + +test('document rejection diagnostics retain only the structured API code and safe reason', async t => { + const f = await fixture(t); f.store.saveTask(task()); + f.transport.fail = { kind: 'document', when: 'before', error: new DocumentRequestRejected('Feishu document API 99991672: permission denied TRACE_PRIVATE_BODY') }; + await f.pump(); + const diagnostic = f.store.diagnostics().find(item => item.scope === 'task')!; + const parsed = JSON.parse(diagnostic.message); + assert.equal(parsed.apiCode, 99991672); + assert.equal(parsed.operation, 'document.document'); + assert.equal(parsed.retry, 'pending'); + assert.doesNotMatch(diagnostic.message, /TRACE_PRIVATE_BODY/); +}); diff --git a/test/feishu-progress.test.ts b/test/feishu-progress.test.ts index ad5e15a..ef5f70d 100644 --- a/test/feishu-progress.test.ts +++ b/test/feishu-progress.test.ts @@ -1,9 +1,11 @@ +import { ExecutionFixture } from './execution-fixture.js'; import { testExecution } from './test-config.js'; import { SurfaceCodex, testProject, restoreSurface, tickSurface } from './coordinated-surfaces.js'; import assert from 'node:assert/strict'; import { realpath, mkdtemp, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; +import { DatabaseSync } from 'node:sqlite'; import { test, type TestContext } from 'node:test'; import { setTimeout } from 'node:timers/promises'; import { configSchema } from '../src/config.js'; @@ -11,7 +13,7 @@ import { taskSchema, type Task } from '../src/domain.js'; import type { ExecutionEvent } from '../src/execution-events.js'; import { FeishuProgressPublisher, progressPublicationSchema } from '../src/feishu-progress.js'; import { AgentLoop } from '../src/loop.js'; -import type { FeishuProgressPort } from '../src/ports.js'; +import { TaskGroupUnavailable, type FeishuProgressPort } from '../src/ports.js'; import { Store } from '../src/store.js'; import { CodexFake, FeishuFake, GitHubFake } from './fakes.js'; @@ -90,17 +92,18 @@ async function fixture(t: TestContext, initialize = true) { const directory = await realpath(await mkdtemp(join(tmpdir(), 'feishu-progress-'))); let store = new Store(directory); const transport = new Transport(); + const source = new ExecutionFixture(); let now = Date.UTC(2026, 10, 7); - let publisher = new FeishuProgressPublisher({ store, transport, now: () => now }); + let publisher = new FeishuProgressPublisher({ store, execution: source.execution, transport, now: () => now }); t.after(async () => { await publisher.close(); store.close(); await rm(directory, { recursive: true, force: true }); }); if (initialize) await publisher.poll(); const event = (kind: ExecutionEvent['kind'], text: string, changes: Partial = {}, taskId = 'task') => { - store.recordExecutionEvent(taskId, { threadId: `thread-${taskId}`, turnId: 'turn', itemId: 'item', kind, label: kind, text, createdAt: now, ...changes }); + source.publish(taskId, { threadId: `thread-${taskId}`, turnId: 'turn', itemId: 'item', kind, label: kind, text, createdAt: now, ...changes }); }; - return { transport, event, get store() { return store; }, get publisher() { return publisher; }, get now() { return now; }, + return { source, directory, transport, event, get store() { return store; }, get publisher() { return publisher; }, get now() { return now; }, advance(ms: number) { now += ms; }, async pump(count = 1) { for (let i = 0; i < count; i++) { await publisher.poll(); now += 1000; } }, - async restart() { await publisher.close(); store.close(); store = new Store(directory); publisher = new FeishuProgressPublisher({ store, transport, now: () => now }); }, + async restart() { await publisher.close(); store.close(); store = new Store(directory); source.restart(); publisher = new FeishuProgressPublisher({ store, execution: source.execution, transport, now: () => now }); }, publication(taskId = 'task', turnId = 'turn') { return progressPublicationSchema.parse(store.progressRecord(taskId, turnId)); }, }; } @@ -164,15 +167,15 @@ test('discovery retains fast turns between polls and old terminal state after ta f.event('turn_start', 'running', { turnId: 'next' }); await f.pump(6); assert.equal(f.transport.messages.size, 2); - const old = f.publication(); assert.equal(old.delivery.kind, 'closed'); assert.equal(old.view.status, 'failed'); - assert.equal(f.publication('task', 'next').view.status, 'running'); + const old = f.publication(); assert.equal(old.delivery.kind, 'closed'); assert.match(f.transport.cards.get('card-1')?.content ?? '', /失败/); + assert.match(f.transport.cards.get('card-2')?.content ?? '', /执行中/); await f.restart(); await f.pump(2); assert.equal(f.transport.messages.size, 2); }); test('native waiting status wins over interrupted or idle snapshots and tool errors are not terminal', async t => { const f = await fixture(t); const current = task(); f.store.saveTask(current); f.event('tool_end', 'exit 1'); f.event('error', 'temporary tool failure'); await f.pump(3); - assert.equal(f.publication().view.status, 'running'); + assert.match(f.transport.cards.get('card-1')?.content ?? '', /执行中/); assert.match(f.transport.cards.get('card-1')?.content ?? '', /temporary tool failure/); f.event('agent_delta', 'execution resumed'); await f.pump(); assert.doesNotMatch(f.transport.cards.get('card-1')?.content ?? '', /temporary tool failure/); @@ -191,6 +194,8 @@ test('restart retains card and sequence; stale streaming is renewed and then clo const before = f.publication().sequence; await f.restart(); f.advance(9 * 60_000); f.event('agent_delta', ' second'); await f.pump(2); + assert.equal(f.publication().sequence, before, 'unknown suffix position waits for canonical completion'); + f.event('agent_message', 'first second'); await f.pump(4); assert.equal(f.transport.cards.size, 1); assert.equal(f.transport.messages.size, 1); assert.ok(f.publication().sequence > before); assert.ok(f.transport.calls.some(call => call.kind === 'settings' && call.enabled)); @@ -245,7 +250,7 @@ test('a stale absent lookup followed by 230099 does not acknowledge the send', a f.transport.findProgressMessage = async () => null; await f.restart(); await f.pump(); assert.deepEqual(f.publication().delivery, original); - assert.ok(f.store.diagnostics().some(row => row.message.includes('230099'))); + assert.ok(f.store.diagnostics().some(row => row.scope === 'task' && row.message.includes('card.'))); assert.equal(f.transport.messages.size, 1); f.transport.findProgressMessage = lookup; f.advance(3000); await f.pump(); assert.equal(f.publication().delivery.kind, 'published'); @@ -319,10 +324,10 @@ test('bounded discovery and UTF-8/JSON projections preserve latest inert tool ou f.event('tool_output', 'unsafe\n' + '中文🙂\\"\n'.repeat(8000) + 'LATEST_OUTPUT'); await f.pump(5); const card = f.transport.cards.get('card-1'); assert.ok(card); - assert.match(card.content, /LATEST_OUTPUT/); assert.match(card.content, /已截断/); + assert.doesNotMatch(card.content, /LATEST_OUTPUT/); assert.match(card.content, /已截断/); assert.match(card.content, /649/); assert.ok(Buffer.byteLength(JSON.stringify(card.content)) < 24_000); - assert.ok(Buffer.byteLength(JSON.stringify(f.publication().view)) < 20_000); + assert.ok(Buffer.byteLength(JSON.stringify(f.publication())) < 20_000); }); test('fair scheduler bounds publisher operation rate and concurrent card operations', async t => { @@ -358,13 +363,20 @@ test('whole runtime delivers final answer unchanged while progress API fails', a codex.complete(current.threadId, 'The original final answer.'); await tickSurface(loop); assert.equal(feishu.sent.filter(message => message.text === 'The original final answer.').length, 1); assert.equal(loop.tasks().find(task => !task.sourceId.startsWith('project:'))?.status, 'idle'); - assert.ok(loop.store.diagnostics().some(row => row.scope === 'feishu-progress')); + assert.ok(loop.store.diagnostics().some(row => row.scope === current.id && row.message.includes('card.'))); }); test('enabling progress after completed runtime history ignores recovery observations and publishes only new turns', async t => { const directory = await realpath(await mkdtemp(join(tmpdir(), 'feishu-enable-'))); const progress = new Transport(); const feishu = Object.assign(new FeishuFake(), { progress }); const codex = new SurfaceCodex(); + const readHistory = codex.readHistory.bind(codex); + t.mock.method(codex, 'readHistory', async (threadId: string) => { + const entries = await readHistory(threadId), snapshot = codex.snapshot(threadId); + if (snapshot.turnId) entries.push({ threadId, turnId: snapshot.turnId, itemId: null, kind: snapshot.status === 'running' ? 'turn_start' : 'turn_end', + label: 'turn', text: snapshot.status, createdAt: null, key: JSON.stringify([threadId, snapshot.turnId, null, 'turn']) }); + return entries; + }); const config = configSchema.parse({ execution: testExecution, dataDir: directory, projects: [testProject(directory)], feishu: { userOpenId: 'owner' } }); const ports = { feishu, codex, github: new GitHubFake(), workspace: { ensure: async () => directory, head: async () => 'head' } }; let loop = new AgentLoop(config, ports); @@ -384,3 +396,150 @@ test('enabling progress after completed runtime history ignores recovery observa assert.ok(workerMessages.every(message => progress.cards.has(message.cardId))); assert.equal(feishu.sent.filter(message => message.text === 'Historical final answer').length, 1); }); + +test('acknowledged cards keep only receipts and source outages never replace the full card after restart', async t => { + const f = await fixture(t); f.store.saveTask(task()); + const sentinel = 'UNIQUE_CARD_TRANSCRIPT_879423'; + f.event('agent_message', sentinel); await f.pump(4); + assert.doesNotMatch(JSON.stringify(f.publication()), /UNIQUE_CARD_TRANSCRIPT|assistant|content"|view"/); + const full = f.transport.cards.get('card-1')!.content; + await f.restart(); f.source.unavailable = true; + f.event('agent_delta', 'only a suffix', { itemId: 'new-live' }); await f.pump(2); + assert.equal(f.transport.cards.get('card-1')!.content, full); + f.source.unavailable = false; await f.pump(3); + assert.match(f.transport.cards.get('card-1')!.content, /UNIQUE_CARD_TRANSCRIPT_879423/); + assert.match(f.transport.cards.get('card-1')!.content, /only a suffix/); + f.store.saveTask({ ...f.store.task('task'), chatId: 'replacement-chat' }); await f.pump(4); + assert.doesNotMatch(f.store.meta('retired-progress:task:chat-task:turn') ?? '', /UNIQUE_CARD_TRANSCRIPT/); + const db = new DatabaseSync(join(f.directory, 'tasks.sqlite')); + try { + for (const row of db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'").all()) { + const table = String(row.name); assert.match(table, /^[a-z_]+$/); + assert.doesNotMatch(JSON.stringify(db.prepare(`SELECT * FROM ${table}`).all()), new RegExp(sentinel)); + } + } finally { db.close(); } +}); + +test('shutdown drains an outstanding history read without creating or writing a card afterwards', async t => { + const f = await fixture(t); f.store.saveTask(task()); + const held = gate(), entered = gate(), read = f.source.execution.read.bind(f.source.execution); + t.mock.method(f.source.execution, 'read', async (...args: Parameters) => { entered.resolve(); await held.promise; return read(...args); }); + const polling = f.publisher.poll(); await entered.promise; + let closed = false; const closing = f.publisher.close().then(() => { closed = true; }); + await Promise.resolve(); assert.equal(closed, false); + held.resolve(); await Promise.all([closing, polling]); + assert.equal(f.transport.calls.length, 0); assert.equal(f.store.progressRecord('task', 'turn'), null); +}); + + +test('first enable records the excluded head so completed turns during downtime are discovered', async t => { + const f = await fixture(t, false); + f.store.saveTask({ ...task(), status: 'idle', executionStatus: 'completed' }); + f.event('turn_end', 'completed'); await f.pump(); await f.publisher.close(); + for (const turnId of ['missed', 'latest']) { + f.event('agent_message', turnId, { turnId }); f.event('turn_end', 'completed', { turnId }); + } + f.store.saveTask({ ...task('task', 'latest'), status: 'idle', executionStatus: 'completed' }); + await f.restart(); await f.pump(8); + assert.equal(f.store.progressRecord('task', 'turn'), null); + assert.equal(f.publication('task', 'missed').delivery.kind, 'closed'); + assert.equal(f.publication('task', 'latest').delivery.kind, 'closed'); +}); + +test('disabled and retired card receipts never retain an upstream body echo in raw SQLite', async t => { + const f = await fixture(t); f.store.saveTask(task()); + const sentinel = 'PRIVATE_CARD_TRACE_98761'; f.event('agent_message', sentinel); + f.transport.createProgressCard = async () => { throw new TaskGroupUnavailable(230002, sentinel); }; + await f.pump(); assert.equal(f.publication().delivery.kind, 'disabled'); + f.store.saveTask({ ...task(), chatId: 'replacement' }); await f.pump(); + const db = new DatabaseSync(join(f.directory, 'tasks.sqlite')); + try { + for (const row of db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'").all()) { + const table = String(row.name); assert.match(table, /^[a-z_]+$/); + assert.doesNotMatch(JSON.stringify(db.prepare(`SELECT * FROM ${table}`).all()), new RegExp(sentinel)); + } + } finally { db.close(); } +}); + + +test('same-thread status changes during history read cannot publish a stale card', async t => { + const f = await fixture(t); f.store.saveTask(task()); + const held = gate(), entered = gate(), read = f.source.execution.read.bind(f.source.execution); + const mocked = t.mock.method(f.source.execution, 'read', async (...args: Parameters) => { entered.resolve(); await held.promise; return read(...args); }); + const polling = f.publisher.poll(); await entered.promise; + f.store.saveTask({ ...task(), status: 'paused' }); held.resolve(); await polling; + assert.equal(f.transport.cards.size, 0); mocked.mock.restore(); await f.pump(4); + assert.match(f.transport.cards.get('card-1')?.content ?? '', /已暂停/); +}); + +test('settled cards release projections without polling idle history and reject lagging partial history', async t => { + const f = await fixture(t); f.store.saveTask(task()); f.event('agent_message', 'Full acknowledged card'); await f.pump(4); + const full = f.transport.cards.get('card-1')!.content, history = structuredClone(f.source.history.get('thread-task')!); + const reads = f.source.reads.length; await f.pump(5); assert.equal(f.source.reads.length, reads); + await f.restart(); f.source.history.set('thread-task', history.map(event => ({ ...event, kind: 'agent_delta', text: 'suffix' }))); + await f.pump(4); assert.equal(f.transport.cards.get('card-1')!.content, full); + const beforeRecovery = f.source.reads.length; f.source.history.set('thread-task', history); f.advance(60_000); await f.pump(3); + assert.ok(f.source.reads.length > beforeRecovery); assert.equal(f.transport.cards.get('card-1')!.content, full); +}); + +test('more delayed turns than resident card capacity all rehydrate and publish', async t => { + const f = await fixture(t); f.store.saveTask(task('task', 'turn-149')); + for (let n = 0; n < 150; n++) { + f.event('agent_message', `Output for ${n}`, { turnId: `turn-${n}` }); + f.event('turn_end', 'completed', { turnId: `turn-${n}` }); + } + const views: unknown = Reflect.get(f.publisher, 'views'); assert.ok(views instanceof Map); assert.ok(views.size <= 128); + await f.pump(180); + assert.equal(f.transport.messages.size, 150); + assert.ok([...f.transport.cards.values()].every(card => !card.streaming)); + assert.equal(views.size, 0); +}); + +test('lagging restart history cannot acknowledge an unseen head or skip intervening turns', async t => { + const f = await fixture(t, false); + f.store.saveTask({ ...task('task', 'before'), executionStatus: 'completed' }); + f.event('turn_end', 'completed', { turnId: 'before', itemId: null }); + await f.pump(); await f.restart(); + f.store.saveTask({ ...task('task', 'latest'), executionStatus: 'completed' }); + await f.pump(8); + assert.equal(f.transport.cards.size, 0); + const prior = f.source.history.get('thread-task')!; + f.source.history.set('thread-task', [...prior, + { ...prior[0]!, turnId: 'missed', key: JSON.stringify(['thread-task', 'missed', null, 'turn']) }, + { ...prior[0]!, turnId: 'latest', key: JSON.stringify(['thread-task', 'latest', null, 'turn']) }, + ]); + await f.pump(60); + assert.ok(f.store.progressRecord('task', 'missed')); + assert.equal(f.transport.cards.size, 2); +}); + +test('transient retry errors do not prevent completed cards recovering after restart', async t => { + const f = await fixture(t); f.store.saveTask(task()); + f.event('turn_start', 'inProgress', { itemId: null }); + f.source.execution.publish('task', { threadId: 'thread-task', turnId: 'turn', itemId: null, kind: 'error', text: 'temporary retry', label: 'retry', createdAt: f.now }); + await f.pump(4); assert.match(f.transport.cards.get('card-1')!.content, /temporary retry/); + await f.restart(); + f.event('agent_message', 'Recovered completion'); f.event('turn_end', 'completed', { itemId: null }); + f.store.saveTask({ ...task(), executionStatus: 'completed' }); + await f.pump(12); + assert.match(f.transport.cards.get('card-1')!.content, /Recovered completion/); + assert.equal(f.transport.cards.get('card-1')!.streaming, false); +}); + +test('a live latest turn cannot advance discovery past missing canonical turns', async t => { + const f = await fixture(t, false); + f.store.saveTask({ ...task('task', 'before'), executionStatus: 'completed' }); + f.event('turn_end', 'completed', { turnId: 'before', itemId: null }); + await f.pump(); await f.restart(); + f.store.saveTask({ ...task('task', 'latest'), executionStatus: 'completed' }); + f.source.execution.publish('task', { threadId: 'thread-task', turnId: 'latest', itemId: null, kind: 'turn_end', text: 'completed', label: 'ended', createdAt: f.now }); + await f.pump(8); + const prior = f.source.history.get('thread-task')!; + f.source.history.set('thread-task', [...prior, + { ...prior[0]!, turnId: 'missed', key: JSON.stringify(['thread-task', 'missed', null, 'turn']) }, + { ...prior[0]!, turnId: 'latest', key: JSON.stringify(['thread-task', 'latest', null, 'turn']) }, + ]); + await f.pump(60); + assert.ok(f.store.progressRecord('task', 'missed')); + assert.equal(f.transport.cards.size, 2); +}); diff --git a/test/fixtures/debug-codex.mjs b/test/fixtures/debug-codex.mjs index 50a3b6b..f8ac609 100644 --- a/test/fixtures/debug-codex.mjs +++ b/test/fixtures/debug-codex.mjs @@ -16,9 +16,10 @@ createInterface({ input: process.stdin }).on('line', line => { threads.set(thread.id, thread); result = { thread: view(thread), cwd: p.cwd, approvalPolicy: p.approvalPolicy, sandbox: { type: 'readOnly', networkAccess: false } }; break; } case 'thread/resume': case 'thread/read': result = { thread: view(threads.get(p.threadId)) }; break; + case 'thread/turns/list': result = { data: threads.get(p.threadId).turns.map(turn => ({ ...turn, itemsView: 'full', startedAt: null, completedAt: null })), nextCursor: null }; break; case 'turn/start': { const thread = threads.get(p.threadId); - const turn = { id: 'turn-' + (thread.turns.length + 1), status: 'inProgress', items: [{ id: 'user', type: 'userMessage', clientId: p.clientUserMessageId, content: p.input }] }; + const turn = { startedAt: null, completedAt: null, itemsView: 'full', id: 'turn-' + (thread.turns.length + 1), status: 'inProgress', items: [{ id: 'user', type: 'userMessage', clientId: p.clientUserMessageId, content: p.input }] }; thread.turns.push(turn); send({ id, result: { turn } }); const scope = { threadId: thread.id, turnId: turn.id }; @@ -35,6 +36,7 @@ createInterface({ input: process.stdin }).on('line', line => { return; } notify('turn/started', { threadId: thread.id, turn }); + notify('item/started', { ...scope, item: { id: 'agent', type: 'agentMessage', text: '', phase: 'commentary' } }); notify('item/agentMessage/delta', { ...scope, itemId: 'agent', delta: '正在检查运行状态,' }); notify('item/started', { ...scope, item: { id: 'command', type: 'commandExecution', command: 'npm run check', status: 'inProgress' } }); notify('item/commandExecution/outputDelta', { ...scope, itemId: 'command', delta: 'TypeScript checking…\n' }); diff --git a/test/live-execution.test.ts b/test/live-execution.test.ts new file mode 100644 index 0000000..b08679a --- /dev/null +++ b/test/live-execution.test.ts @@ -0,0 +1,276 @@ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { LiveExecution } from '../src/live-execution.js'; +import { executionKey, type ExecutionEvent, type ExecutionHistory } from '../src/execution-events.js'; +const event = (text: string, kind: ExecutionEvent['kind'] = 'agent_delta', itemId = 'item'): ExecutionEvent => ({ threadId: 'thread', turnId: 'turn', itemId, kind, label: 'Agent', text, createdAt: 123 }); +const history = (text: string, kind: ExecutionEvent['kind'] = 'agent_delta'): ExecutionHistory => [{ ...event(text, kind), key: executionKey(event(text, kind)), createdAt: null }]; + +test('history and live snapshots replace by item identity without duplicated deltas or invented timestamps', async () => { + const feed = new LiveExecution({ readHistory: async () => history('hello') }); + feed.publish('task', event('hello', 'agent_start')); feed.publish('task', event(' world')); + const snapshot = await feed.read('task', 'thread'); + assert.equal(snapshot.entries.length, 1); assert.equal(snapshot.entries[0]?.text, 'hello world'); + assert.equal(feed.since(snapshot.cursor)?.length, 0); + const final = new LiveExecution({ readHistory: async () => history('hello world', 'agent_message') }); + final.publish('task', event(' world')); + assert.deepEqual((await final.read('task', 'thread')).entries.map(item => [item.text, item.createdAt]), [['hello world', null]]); +}); + +test('bounded replay resets evicted and previous process cursors while isolating current Threads', () => { + const feed = new LiveExecution(undefined, 2), beginning = feed.cursor; + feed.publish('task', event('one', 'agent_message', 'one')); + const resumable = feed.cursor; + feed.publish('task', event('two', 'agent_message', 'two')); + feed.publish('other', { ...event('three', 'agent_message', 'three'), threadId: 'other-thread' }); + assert.equal(feed.current('task', 'thread').length, 1); + assert.equal(feed.since(beginning), null); + assert.equal(feed.since(resumable)?.length, 2); + assert.equal(new LiveExecution().since(feed.cursor), null); + assert.deepEqual(feed.current('task', 'replacement-thread'), []); +}); + +test('a failed history read leaves current live entries visible and an explicit unavailable state', async () => { + const feed = new LiveExecution({ readHistory: async () => { throw new Error('sensitive upstream body'); } }); + feed.publish('task', event('current progress', 'agent_start')); + const snapshot = await feed.read('task', 'thread'); + assert.equal(snapshot.history.kind, 'unavailable'); assert.equal(snapshot.entries[0]?.text, 'current progress'); + assert.doesNotMatch(JSON.stringify(snapshot), /sensitive/); +}); + +test('notification arriving while history is read is included at the returned stream cursor', async () => { + let release!: (value: ExecutionHistory) => void; + const feed = new LiveExecution({ readHistory: () => new Promise(resolve => { release = resolve; }) }); + const reading = feed.read('task', 'thread'); + feed.publish('task', event('hello world', 'agent_start')); + release(history('hello')); + const snapshot = await reading; + assert.equal(snapshot.entries[0]?.text, 'hello world'); + assert.deepEqual(feed.since(snapshot.cursor), []); +}); + +test('a history-only partial reloads an unanchored delta and reconnect resets subscribers', async () => { + let canonical = 'hello'; + const feed = new LiveExecution({ readHistory: async () => history(canonical) }); + await feed.read('task', 'thread'); + feed.publish('task', event(' world')); + assert.deepEqual(feed.current('task'), []); + canonical = 'hello world'; + assert.equal((await feed.read('task', 'thread')).entries[0]?.text, canonical); + const before = feed.cursor; let reset = false; + feed.subscribe(change => { if (change.event === null) reset = true; }); + feed.reset(); + assert.equal(reset, true); assert.equal(feed.since(before), null); assert.deepEqual(feed.current('task'), []); +}); + +test('a complete item arriving during history read replaces an older complete snapshot', async () => { + let release!: (value: ExecutionHistory) => void; + const feed = new LiveExecution({ readHistory: () => new Promise(resolve => { release = resolve; }) }); + const reading = feed.read('task', 'thread'); + feed.publish('task', event('final corrected answer', 'agent_message')); + release(history('older complete answer', 'agent_message')); + assert.equal((await reading).entries[0]?.text, 'final corrected answer'); +}); + +test('an unanchored suffix arriving during hydration remains a replay gap until a fresh read', async () => { + let release!: (value: ExecutionHistory) => void; + const feed = new LiveExecution({ readHistory: () => new Promise(resolve => { release = resolve; }) }); + const reading = feed.read('task', 'thread'); + feed.publish('task', event(' world')); + release(history('hello')); + const snapshot = await reading; + assert.equal(snapshot.history.kind, 'unavailable'); + assert.equal(snapshot.entries[0]?.text, 'hello'); + assert.equal(feed.since(snapshot.cursor), null); + feed.publish('task', event('!')); + assert.deepEqual(feed.current('task'), []); + const recovering = feed.read('task', 'thread'); + release(history('hello world!')); + assert.equal((await recovering).entries[0]?.text, 'hello world!'); + feed.publish('task', event(' Next')); + assert.deepEqual(feed.current('task'), []); + const completed = feed.read('task', 'thread'); + release(history('hello world! Next', 'agent_message')); + assert.equal((await completed).history.kind, 'available'); +}); + +test('a final item recovered from history rejects late delta text', async () => { + const feed = new LiveExecution({ readHistory: async () => history('complete answer', 'agent_message') }); + await feed.read('task', 'thread'); + feed.publish('task', event('late suffix')); + assert.equal(feed.current('task')[0]?.text, 'complete answer'); + assert.equal(feed.current('task')[0]?.kind, 'agent_message'); +}); + +test('an evicted prefix cannot be replaced by the next suffix', async () => { + const feed = new LiveExecution({ readHistory: async () => history('hello') }, 1); + await feed.read('task', 'thread'); + feed.publish('other', event('other', 'agent_message', 'other')); + const before = feed.cursor; + feed.publish('task', event(' world')); + assert.deepEqual(feed.current('task'), []); + assert.equal(feed.since(before), null); +}); + +test('throwing subscribers cannot interrupt publish or reconnect reset', () => { + const feed = new LiveExecution(); + const received: string[] = []; + feed.subscribe(() => { throw new Error('subscriber failed'); }); + feed.subscribe(change => { received.push(change.event?.text ?? 'reset'); }); + assert.doesNotThrow(() => feed.publish('task', event('complete', 'agent_message'))); + assert.doesNotThrow(() => feed.reset()); + assert.deepEqual(received, ['complete', 'reset']); +}); + +test('an active history snapshot ahead of notifications cannot regress to a shorter live prefix', async () => { + let release!: (value: ExecutionHistory) => void; + const feed = new LiveExecution({ readHistory: () => new Promise(resolve => { release = resolve; }) }); + feed.publish('task', event('hello', 'agent_start')); + const reading = feed.read('task', 'thread'); + feed.publish('task', event(' world')); + release(history('hello world!')); + assert.equal((await reading).entries[0]?.text, 'hello world!'); +}); + +test('conflicting active snapshots request rehydration without guessing an overlap', async () => { + const feed = new LiveExecution({ readHistory: async () => history('canonical prefix') }); + feed.publish('task', event('different prefix', 'agent_start')); + const snapshot = await feed.read('task', 'thread'); + assert.equal(snapshot.history.kind, 'unavailable'); + assert.equal(snapshot.entries[0]?.text, 'canonical prefix'); + assert.equal(feed.since(snapshot.cursor), null); + assert.deepEqual(feed.current('task'), []); +}); + +test('repeated lagging partial reads cannot acknowledge an observed suffix', async () => { + let canonical = 'hello'; + let kind: ExecutionEvent['kind'] = 'agent_delta'; + const feed = new LiveExecution({ readHistory: async () => history(canonical, kind) }); + feed.publish('task', event(' world')); + const first = await feed.read('task', 'thread'); + const second = await feed.read('task', 'thread'); + assert.equal(first.history.kind, 'unavailable'); + assert.equal(second.history.kind, 'unavailable'); + assert.equal(feed.since(second.cursor), null); + feed.publish('task', event('!')); + assert.deepEqual(feed.current('task'), []); + canonical = 'hello world!'; + assert.equal((await feed.read('task', 'thread')).history.kind, 'unavailable'); + kind = 'agent_message'; + const recovered = await feed.read('task', 'thread'); + assert.equal(recovered.history.kind, 'available'); + assert.equal(recovered.entries[0]?.text, canonical); + feed.publish('task', event(' Next')); + assert.equal(feed.current('task')[0]?.text, 'hello world!'); +}); + +test('oversized live snapshots request canonical history instead of publishing truncated final text', async () => { + const complete = 'a'.repeat(40000) + ' final'; + const feed = new LiveExecution({ readHistory: async () => history(complete, 'agent_message') }); + const before = feed.cursor; + feed.publish('task', event(complete, 'agent_message')); + assert.equal(feed.since(before), null); + assert.deepEqual(feed.current('task'), []); + const recovered = await feed.read('task', 'thread'); + assert.equal(recovered.history.kind, 'available'); + assert.equal(recovered.entries[0]?.text, complete); + feed.publish('task', event('late delta')); + assert.deepEqual(feed.current('task'), []); +}); + + +test('oversized anchored partials recover without accepting a shorter repeated prefix', async () => { + const chunk = '文😀\n'.repeat(10000); + let canonical = chunk; + const feed = new LiveExecution({ readHistory: async () => history(canonical) }); + feed.publish('task', event('', 'agent_start')); + feed.publish('task', event(chunk)); + const recovered = await feed.read('task', 'thread'); + assert.equal(recovered.history.kind, 'available'); + assert.equal(recovered.entries[0]?.text, chunk); + assert.deepEqual(feed.current('task'), []); + feed.publish('task', event(chunk)); + const stale = await feed.read('task', 'thread'); + assert.equal(stale.history.kind, 'unavailable'); + assert.equal(feed.since(stale.cursor), null); + canonical += chunk; + const advanced = await feed.read('task', 'thread'); + assert.equal(advanced.history.kind, 'available'); + assert.equal(advanced.entries[0]?.text, canonical); + assert.equal((await feed.read('task', 'thread')).history.kind, 'available'); +}); + +test('oversized prefix coverage preserves Unicode across split surrogate deltas', async () => { + const prefix = 'x'.repeat(40000); + let canonical = prefix; + const feed = new LiveExecution({ readHistory: async () => history(canonical) }); + feed.publish('task', event('', 'agent_start')); + feed.publish('task', event(prefix + '\ud83d')); + feed.publish('task', event('\ude00')); + canonical += '😀'; + const recovered = await feed.read('task', 'thread'); + assert.equal(recovered.history.kind, 'available'); + assert.equal(recovered.entries[0]?.text, canonical); +}); + + +test('repeated unanchored text cannot establish the position of a lost prefix', async () => { + let release!: (value: ExecutionHistory) => void; + const feed = new LiveExecution({ readHistory: () => new Promise(resolve => { release = resolve; }) }); + const first = feed.read('task', 'thread'); + feed.publish('task', event('ha')); + release(history('haha')); + assert.equal((await first).history.kind, 'unavailable'); + const second = feed.read('task', 'thread'); + release(history('haha')); + const repeated = await second; + assert.equal(repeated.history.kind, 'unavailable'); + assert.equal(feed.since(repeated.cursor), null); + feed.publish('task', event('!')); + assert.deepEqual(feed.current('task'), []); + const final = feed.read('task', 'thread'); + release(history('hahaha!', 'agent_message')); + assert.equal((await final).entries[0]?.text, 'hahaha!'); +}); + +test('history ahead of an anchored stream does not duplicate delayed notifications', async () => { + const feed = new LiveExecution({ readHistory: async () => history('hello world') }); + feed.publish('task', event('hello', 'agent_start')); + assert.equal((await feed.read('task', 'thread')).entries[0]?.text, 'hello world'); + feed.publish('task', event(' world')); + assert.equal(feed.current('task')[0]?.text, 'hello world'); + feed.publish('task', event('!')); + assert.equal(feed.current('task')[0]?.text, 'hello world!'); +}); + + +test('evicting an unresolved suffix cannot certify a lagging partial history', async () => { + let canonical = history('hello'); + const feed = new LiveExecution({ readHistory: async () => canonical }, 1); + feed.publish('task', event(' world')); + feed.publish('other', { ...event('other finished', 'agent_message', 'other'), threadId: 'other-thread' }); + const lagging = await feed.read('task', 'thread'); + assert.equal(lagging.history.kind, 'unavailable'); + assert.equal(lagging.entries[0]?.text, 'hello'); + assert.equal(feed.since(lagging.cursor), null); + assert.equal((await feed.read('task', 'thread')).history.kind, 'unavailable'); + canonical = history('hello world', 'agent_message'); + const ended = { ...event('completed', 'turn_end'), itemId: null }; + canonical.push({ ...ended, key: executionKey(ended) }); + const completed = await feed.read('task', 'thread'); + assert.equal(completed.history.kind, 'available'); + assert.equal(completed.entries[0]?.text, 'hello world'); +}); + +test('canonical turn identities exclude merged live turns and failed reads', async () => { + let unavailable = false; + const feed = new LiveExecution({ readHistory: async () => { + if (unavailable) throw new Error('offline'); + return history('historical answer', 'agent_message'); + } }); + feed.publish('task', { ...event('current answer', 'agent_message', 'new-item'), turnId: 'new-turn' }); + const snapshot = await feed.read('task', 'thread'); + assert.deepEqual(snapshot.canonicalTurnIds, ['turn']); + assert.ok(snapshot.entries.some(item => item.turnId === 'new-turn')); + unavailable = true; + assert.deepEqual((await feed.read('task', 'thread')).canonicalTurnIds, []); +}); diff --git a/test/poll-state-reuse.test.ts b/test/poll-state-reuse.test.ts index f15b2c7..eff093d 100644 --- a/test/poll-state-reuse.test.ts +++ b/test/poll-state-reuse.test.ts @@ -1,3 +1,4 @@ +import { ExecutionFixture } from './execution-fixture.js'; import assert from 'node:assert/strict'; import { mkdtemp, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; @@ -151,22 +152,23 @@ test('closeout pending cleanup changes remain visible after status was cached', test('event and inbox polling reuse compiled validation and preserve ordered facts', async t => { const { store, task, db } = await fixture(t); + const source = new ExecutionFixture(); for (let i = 0; i < 12; i++) { - store.recordExecutionEvent(task.id, { threadId: 'thread', turnId: 'turn', itemId: `item-${i}`, kind: 'agent_message', + source.publish(task.id, { threadId: 'thread', turnId: 'turn', itemId: `item-${i}`, kind: 'agent_message', label: 'Assistant', text: `Reply ${i}`, createdAt: i + 1 }); const message = incomingSchema.parse({ id: `input-${i}`, chatId: 'chat', senderId: 'owner', text: `Request ${i}`, attachments: [], createdAt: i + 1 }); assert.equal(store.receive(task.id, message), true); } const legacy = { id: 'legacy', chatId: 'chat', senderId: 'owner', text: 'Legacy request', attachments: [], createdAt: 13 }; db.prepare("INSERT INTO inbox(id,task_id,payload,status,origin) VALUES(?,?,?,'pending','user')").run(legacy.id, task.id, JSON.stringify(legacy)); - const events = [...store.documentConversationEvents(task.id)]; + const events = source.execution.current(task.id); const inputs = store.inputs(task.id); assert.deepEqual(events.map(event => event.text), Array.from({ length: 12 }, (_, i) => `Reply ${i}`)); assert.deepEqual(inputs.map(input => input.message.text), [...Array.from({ length: 12 }, (_, i) => `Request ${i}`), 'Legacy request']); assert.ok(inputs.every(input => input.provenance.kind === 'user' && input.provenance.message.id === input.id)); const compile = t.mock.method(globalThis, 'Function'); for (let i = 0; i < 3; i++) { - assert.deepEqual([...store.documentConversationEvents(task.id)], events); + assert.deepEqual(source.execution.current(task.id), events); assert.deepEqual(store.inputs(task.id), inputs); } assert.equal(compile.mock.callCount(), 0);