/** * plugins/workflow-runner/test/apiEcho.test.ts —— echo() 必须真正进对话历史 * * 这是实测出来的行为缺陷,不是理论风险: * * 宿主把 `text` 事件当**流式预览**——TUI 累进 LiveArea、Desktop 累进 * `streamingText`——而预览在 `turn_done` 时被 `clearLive()` 清掉。只有 * `assistant_message` 才会落进 `history` / `setMessages`。 * * 于是一条纯确定性工作流(只有 echo、没有 llm/agent)跑完之后,对话历史里只剩 * 用户那句 `/workflow xxx`,助手侧一片空白 —— 用户完全看不出发生过什么。 * 有 llm 的工作流更糟:中间 agent 的碎话留下了,`echo()` 写的最终结论反而没了。 * * 更早的 `echo` 注释白纸黑字写着「进 MessageList」,而实现只发 `text` —— * 注释宣称了、实现没做到,正是最容易再次退化的那类。这个文件把它钉死。 */ import { afterEach, describe, expect, test } from 'bun:test'; import { mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import type { LoopEvent, Message } from '../../../src/plugin-sdk.js'; import { createWorkflowContext } from '../src/runtime/context.js'; import { Journal } from '../src/journal.js'; import type { RunContext } from '../src/types.js'; import { WorkflowState } from '../src/workflowState.js'; const dirs: string[] = []; afterEach(() => { for (const d of dirs.splice(0)) { try { rmSync(d, { recursive: true, force: true, maxRetries: 3 }); } catch { /* Windows 偶发 EBUSY;清理失败不影响断言 */ } } }); async function harness() { const wd = mkdtempSync(join(tmpdir(), 'wf-echo-')); dirs.push(wd); const state = new WorkflowState(wd, 'demo', 's1'); await state.init({ goal: '测试目标' }, 'hash-v1', []); const journal = new Journal(state.journalPath, { bundleHash: 'test', rootRunId: 'test-root' }); await journal.load(); const events: LoopEvent[] = []; const history: Message[] = [{ role: 'system', content: 'sys' }]; const ctx = { provider: { name: 'test' }, history, cwd: wd, tools: [], } as unknown as RunContext; const globals = createWorkflowContext({ ctx, state, journal, emit: (ev) => events.push(ev), args: {}, workflowName: 'demo', session: 's1', task: state.taskId, }); return { globals, events, history }; } describe('echo() 进历史', () => { test('★ 发 assistant_message,而不只是流式 text', async () => { const h = await harness(); h.globals.output.message('✅ 冒烟通过:2 行'); const committed = h.events.filter((e) => e.type === 'assistant_message'); expect(committed).toHaveLength(1); expect( (committed[0] as Extract).message .content, ).toBe('✅ 冒烟通过:2 行'); }); test('★ push 进 ctx.history —— 这才是跨会话不丢的原因', async () => { // Desktop 重开会话是从 history 加载的;只 emit 事件、不进 history 的话 // 当场看得见,一刷新就没了。 const h = await harness(); h.globals.output.message('结论 A'); h.globals.output.message('结论 B'); const assistant = h.history.filter((m) => m.role === 'assistant'); expect(assistant.map((m) => m.content)).toEqual(['结论 A', '结论 B']); }); test('每条消息都有 id(宿主拿它做 React key / 去重)', async () => { const h = await harness(); h.globals.output.message('一'); h.globals.output.message('二'); const ids = h.history .filter((m) => m.role === 'assistant') .map((m) => (m as { id?: string }).id); expect(ids.every((id) => typeof id === 'string' && id.length > 0)).toBe(true); expect(new Set(ids).size).toBe(2); // 不能是同一个 id }); test('★ text 事件照发不误 —— -p 的 result 累的是它', async () => { // print.ts 的 result 累 text delta;assistant_message 只计 num_turns、 // 不进 result buffer。两者并存不会重复,去掉 text 反而会让 -p 拿到空 result。 const h = await harness(); h.globals.output.message('给宿主的结论'); const texts = h.events .filter((e): e is Extract => e.type === 'text') .map((e) => e.delta) .join(''); expect(texts).toContain('给宿主的结论'); }); test('log() 不进历史 —— 它是状态行旁白,不是给用户的结论', async () => { const h = await harness(); h.globals.output.progress({ phase: 'test', status: 'update', message: '正在扫描目录…', }); expect(h.history.filter((m) => m.role === 'assistant')).toHaveLength(0); expect(h.events.some((e) => e.type === 'assistant_message')).toBe(false); expect(h.events.some((e) => e.type === 'workflow_progress')).toBe(true); }); }); describe('断点续跑:快进段不得重复入历史', () => { test('★ 重放前缀里的 echo 只发 text,不再 push(history 已从会话文件带着它)', async () => { const wd = mkdtempSync(join(tmpdir(), 'wf-echo-resume-')); dirs.push(wd); const state = new WorkflowState(wd, 'demo', 's1'); await state.init({ goal: '测试目标' }, 'hash-v1', []); // 第一轮:跑两个 journaled 调用,账本落两条 const j1 = new Journal(state.journalPath, { bundleHash: 'test', rootRunId: 'test-root' }); await j1.load(); await j1.step('tool', { a: 1 }, async () => 'r1'); await j1.step('tool', { a: 2 }, async () => 'r2'); // 第二轮 resume:重新载入账本,此刻处在重放前缀 const j2 = new Journal(state.journalPath, { bundleHash: 'test', rootRunId: 'test-root' }); await j2.load(); const events: LoopEvent[] = []; // resume 的 history 里已经有上一轮那条 echo(会话文件带过来的) const history: Message[] = [ { role: 'system', content: 'sys' }, { role: 'assistant', content: '上一轮的结论' }, ]; const globals = createWorkflowContext({ ctx: { provider: { name: 'test' }, history, cwd: wd, tools: [] } as unknown as RunContext, state, journal: j2, emit: (ev) => events.push(ev), args: {}, workflowName: 'demo', session: 's1', task: state.taskId, }); expect(j2.replaying).toBe(true); globals.output.message('上一轮的结论'); // 脚本重跑到同一行 // 文本照发(用户看得到叙述),但不再重复 push expect(events.some((e) => e.type === 'text')).toBe(true); expect(events.some((e) => e.type === 'assistant_message')).toBe(false); expect(history.filter((m) => m.role === 'assistant')).toHaveLength(1); }); test('★ 分叉之后(实跑段)恢复 push', async () => { const wd = mkdtempSync(join(tmpdir(), 'wf-echo-diverge-')); dirs.push(wd); const state = new WorkflowState(wd, 'demo', 's1'); await state.init({ goal: '测试目标' }, 'hash-v1', []); const j1 = new Journal(state.journalPath, { bundleHash: 'test', rootRunId: 'test-root' }); await j1.load(); await j1.step('tool', { a: 1 }, async () => 'r1'); const j2 = new Journal(state.journalPath, { bundleHash: 'test', rootRunId: 'test-root' }); await j2.load(); // 指纹失配 → 分叉 await j2.step('tool', { a: 999 }, async () => 'other'); expect(j2.replaying).toBe(false); const events: LoopEvent[] = []; const history: Message[] = [{ role: 'system', content: 'sys' }]; const globals = createWorkflowContext({ ctx: { provider: { name: 'test' }, history, cwd: wd, tools: [] } as unknown as RunContext, state, journal: j2, emit: (ev) => events.push(ev), args: {}, workflowName: 'demo', session: 's1', task: state.taskId, }); globals.output.message('新的结论'); expect(events.some((e) => e.type === 'assistant_message')).toBe(true); expect(history.filter((m) => m.role === 'assistant')).toHaveLength(1); }); });