/** * plugins/workflow-runner/test/apiReadText.test.ts —— readText / readJson * * 钉死一个只有端到端才暴露的 bug,根因比表面深两层: * * 表面:`readJson()` 对一个**完全合法**的 JSON 文件返回 `null`, * 下游报「JSON 格式错误或数组为空」,而手工 `cat` + `python -m json.tool` 都没问题。 * * 第一层:`Read` 工具的输出是**给模型看**的,每行带 `<行号>` 前缀 —— * `JSON.parse` 必然失败。(剥离正则一度还写成了箭头 `→` 而不是 TAB。) * * 第二层(真根因):`Read` 在**重复读同一文件**时,会在开头插一句 * 「⚠️ 重复读取空转:该区间未变化,前一次 Read 结果仍然有效。」 * 这条只在**第二次**出现,于是同一段代码时对时错。真实触发路径: * research 阶段2 的 agent 写完 branches.json 后自己 Read 校验了一遍, * 脚本随后 readJson() 就命中提示 → 解析失败 → null。 * * 修法不是去剥这些前缀(那是在追着「给模型看的格式」跑),而是让脚本的 * 确定性读取**根本不走那条通道** —— 直接读文件。下面第一组就是钉这条的。 */ import { afterEach, describe, expect, test } from 'bun:test'; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { createWorkflowContext } from '../src/runtime/context.js'; import { Journal } from '../src/journal.js'; import type { RunContext } from '../src/types.js'; import type { WorkflowState } from '../src/workflowState.js'; import type { WorkflowContext } from '../src/workflowContext.js'; const dirs: string[] = []; afterEach(() => { for (const d of dirs.splice(0)) { try { rmSync(d, { recursive: true, force: true, maxRetries: 3 }); } catch { /* Windows 偶发 EBUSY,清理失败不影响断言 */ } } }); function workdir(): string { const d = mkdtempSync(join(tmpdir(), 'wf-readtext-')); dirs.push(d); return d; } /** 造一份最小 globals,cwd 指向临时目录。 */ function makeGlobals(cwd: string, workflowDir?: string) { const ctx = { provider: { name: 'test' }, cwd, history: [], tools: [], } as unknown as RunContext; return createWorkflowContext({ ctx, state: { appendProgress: async () => undefined, } as unknown as WorkflowState, journal: new Journal(join(cwd, 'journal.jsonl'), { bundleHash: 'test', rootRunId: 'test-root' }), emit: () => undefined, args: {}, workflowName: 'demo', session: 's', task: 't', workflowDir, }); } const readTextOf = (g: WorkflowContext) => g.fs.readText as (p: string) => Promise; const readJsonOf = (g: WorkflowContext) => g.fs.readJson as (p: string) => Promise; describe('readText', () => { test('返回**纯**文件内容 —— 没有行号、没有任何提示前缀', async () => { const wd = workdir(); writeFileSync(join(wd, 'a.txt'), 'hello\nworld\n', 'utf8'); expect(await readTextOf(makeGlobals(wd))('a.txt')).toBe('hello\nworld\n'); }); test('★ 连读三次内容完全一致 —— 「重复读取」提示绝不能混进来', async () => { // 这是真根因那一条。走 Read 工具的话,第二次开头会多一句 // 「⚠️ 重复读取空转…」,于是第一次能 parse、第二次就 null。 const wd = workdir(); writeFileSync(join(wd, 'x.json'), '{"a":1}', 'utf8'); const readText = readTextOf(makeGlobals(wd)); const a = await readText('x.json'); const b = await readText('x.json'); const c = await readText('x.json'); expect(a).toBe('{"a":1}'); expect(b).toBe(a); expect(c).toBe(a); }); test('相对路径基于工作流 cwd 解析', async () => { const wd = workdir(); mkdirSync(join(wd, 'sub'), { recursive: true }); writeFileSync(join(wd, 'sub', 'p.md'), '内容', 'utf8'); expect(await readTextOf(makeGlobals(wd))('sub/p.md')).toBe('内容'); }); test('绝对路径原样使用', async () => { const wd = workdir(); const abs = join(wd, 'abs.txt'); writeFileSync(abs, 'ok', 'utf8'); expect(await readTextOf(makeGlobals(workdir()))(abs)).toBe('ok'); }); test('Desktop 工作目录不在包内时仍能读取工作流随附资源', async () => { const cwd = workdir(); const packageRoot = workdir(); const workflowDir = join(packageRoot, 'workflows', 'demo'); mkdirSync(join(workflowDir, 'prompts'), { recursive: true }); writeFileSync(join(workflowDir, 'prompts', 'step.md'), 'bundled prompt', 'utf8'); const readText = readTextOf(makeGlobals(cwd, workflowDir)); expect(await readText('workflows/demo/prompts/step.md')).toBe('bundled prompt'); }); test('读不到返回 null,不抛', async () => { expect(await readTextOf(makeGlobals(workdir()))('nope.txt')).toBeNull(); }); }); describe('readJson', () => { test('合法 JSON 解析成功', async () => { const wd = workdir(); writeFileSync(join(wd, 'b.json'), '[{"a":1},{"b":2}]', 'utf8'); expect(await readJsonOf(makeGlobals(wd))('b.json')).toEqual([ { a: 1 }, { b: 2 }, ]); }); test('★ 连读三次都解析成功 —— 端到端炸在这里', async () => { // research 的真实失败:阶段2 的 agent 自己 Read 过 branches.json, // 脚本再 readJson 就成了「第二次」,静默返回 null。 const wd = workdir(); writeFileSync( join(wd, 'branches.json'), '[{"name":"甲"},{"name":"乙"}]', 'utf8', ); const readJson = readJsonOf(makeGlobals(wd)); for (let i = 1; i <= 3; i++) { const v = (await readJson('branches.json')) as unknown[]; expect(Array.isArray(v)).toBe(true); expect(v).toHaveLength(2); } }); test('多行带缩进的 JSON(真实文件的样子)', async () => { const wd = workdir(); writeFileSync( join(wd, 'c.json'), '[\n {\n "name": "js-yaml(默认推荐)",\n "safe_name": "js-yaml"\n }\n]\n', 'utf8', ); const v = (await readJsonOf(makeGlobals(wd))('c.json')) as { name: string; }[]; expect(v[0].name).toBe('js-yaml(默认推荐)'); }); test('内容不是 JSON → null(不抛,让脚本自己决定怎么办)', async () => { const wd = workdir(); writeFileSync(join(wd, 'd.json'), '这不是 JSON', 'utf8'); expect(await readJsonOf(makeGlobals(wd))('d.json')).toBeNull(); }); test('读不到 → null', async () => { expect(await readJsonOf(makeGlobals(workdir()))('nope.json')).toBeNull(); }); });