/** * plugins/workflow-runner/test/journalLanes.test.ts —— 并发下账本定位必须确定 * * 守的是一条**实测存在过的静默降级**: * * 账本序号原本在拿到并发闸(上限 8)之后才自增,于是扇出超过 8 项时,第 9 项的 * 序号取决于前 8 项谁先返回 —— 也就是取决于 LLM 延迟。同一个脚本两次运行的序号 * 分配可能完全不同 → 首个失配 → 后面全部实跑,**断点续跑静默失效**。 * * ⚠️ **所以这里的扇出规模必须大于并发上限**。N ≤ 8 时所有项同时起跑、顺序恰好稳定, * 用例恒绿,等于没测 —— 这与上一轮 PDF 那条「144 DPI 恰好等于 scale:2,尺寸判据 * 撤掉修复照样绿」是同一类假保证。 */ 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 { Journal, MAIN_LANE } from '../src/journal.js'; const dirs: string[] = []; function tmpJournal(): string { const d = mkdtempSync(join(tmpdir(), 'wf-lane-')); dirs.push(d); return join(d, 'journal.jsonl'); } afterEach(() => { for (const d of dirs.splice(0)) { try { rmSync(d, { recursive: true, force: true, maxRetries: 3 }); } catch { /* Windows 偶发 EBUSY */ } } }); /** 制造「完成顺序与发起顺序不同」的调度:下标越大越快返回。 */ const reversedDelay = (i: number, n: number) => new Promise((r) => setTimeout(r, (n - i) * 2)); describe('车道让并发下的定位与调度无关', () => { test('★ 扇出 20 项(> 并发上限 8)乱序完成,第二次仍 100% 重放', async () => { const path = tmpJournal(); const N = 20; const run1 = new Journal(path, { bundleHash: 'test', rootRunId: 'test-root' }); await run1.load(); await Promise.all( Array.from({ length: N }, async (_, i) => { await reversedDelay(i, N); return run1.step('agent', { i }, async () => `r${i}`, `main/p1i${i}`); }), ); // 第二次:换一种完成顺序(正序),车道定位不该受影响 const run2 = new Journal(path, { bundleHash: 'test', rootRunId: 'test-root' }); await run2.load(); const results = await Promise.all( Array.from({ length: N }, async (_, i) => { await new Promise((r) => setTimeout(r, i * 2)); return run2.step( 'agent', { i }, async (): Promise => { throw new Error(`第 ${i} 项不该实跑`); }, `main/p1i${i}`, ); }), ); expect(results).toEqual(Array.from({ length: N }, (_, i) => `r${i}`)); expect(run2.replayedCount).toBe(N); }); test('★ 对照:同一条车道上并发 20 项 —— 定位就会错乱(这正是不能裸 Promise.all 的原因)', async () => { const path = tmpJournal(); const N = 20; const run1 = new Journal(path, { bundleHash: 'test', rootRunId: 'test-root' }); await run1.load(); await Promise.all( Array.from({ length: N }, async (_, i) => { await reversedDelay(i, N); return run1.step('agent', { i }, async () => `r${i}`, MAIN_LANE); }), ); const run2 = new Journal(path, { bundleHash: 'test', rootRunId: 'test-root' }); await run2.load(); let liveRuns = 0; await Promise.all( Array.from({ length: N }, async (_, i) => { await new Promise((r) => setTimeout(r, i * 2)); return run2.step( 'agent', { i }, async () => { liveRuns++; return `r${i}`; }, MAIN_LANE, ); }), ); // 顺序变了 → 序号与内容对不上 → 大量实跑。这条断言故意宽松: // 要证明的是「会退化」,不是退化到某个精确数字。 expect(liveRuns).toBeGreaterThan(0); expect(run2.replayedCount).toBeLessThan(N); }); test('一条车道失配只作废自己,不牵连兄弟车道', async () => { const path = tmpJournal(); const run1 = new Journal(path, { bundleHash: 'test', rootRunId: 'test-root' }); await run1.load(); for (const i of [0, 1]) { await run1.step('agent', { i, s: 1 }, async () => `a${i}`, `main/p1i${i}`); await run1.step('agent', { i, s: 2 }, async () => `b${i}`, `main/p1i${i}`); } const run2 = new Journal(path, { bundleHash: 'test', rootRunId: 'test-root' }); await run2.load(); // 第 0 项第一阶段内容变了 → 只有 0 号车道分叉 const a0 = await run2.step('agent', { i: 0, s: 99 }, async () => 'NEW', 'main/p1i0'); const b0 = await run2.step('agent', { i: 0, s: 2 }, async () => 'LIVE0', 'main/p1i0'); const a1 = await run2.step('agent', { i: 1, s: 1 }, async () => 'X', 'main/p1i1'); const b1 = await run2.step('agent', { i: 1, s: 2 }, async () => 'X', 'main/p1i1'); expect(a0).toBe('NEW'); expect(b0).toBe('LIVE0'); // 同车道后续作废 → 实跑 expect(a1).toBe('a1'); // 兄弟车道不受影响 → 重放 expect(b1).toBe('b1'); expect(run2.laneDiverged('main/p1i0')).toBe(true); expect(run2.laneDiverged('main/p1i1')).toBe(false); }); test('★ 主车道保持「首个失配即作废其后」—— 这条安全属性不许丢', async () => { // 反例:run1 产出 plan v1 → 审查得 R2;run2 第一步产出 v2,那句审查的 // 指纹一模一样(路径是常量)。若按内容寻址,R2 会被当成对 v2 的审查返回。 const path = tmpJournal(); const run1 = new Journal(path, { bundleHash: 'test', rootRunId: 'test-root' }); await run1.load(); await run1.step('agent', { p: '写计划' }, async () => 'plan v1'); await run1.step('agent', { p: '审查 out/plan.md' }, async () => '审查(v1)'); const run2 = new Journal(path, { bundleHash: 'test', rootRunId: 'test-root' }); await run2.load(); await run2.step('agent', { p: '写计划(改了提示词)' }, async () => 'plan v2'); const review = await run2.step( 'agent', { p: '审查 out/plan.md' }, // 指纹与上次完全相同 async () => '审查(v2)', ); expect(review).toBe('审查(v2)'); // 必须实跑,不得拿 v1 的审查冒充 }); test('v1 账本不会被 V2 重放', async () => { const path = tmpJournal(); const { writeFile, mkdir } = await import('node:fs/promises'); const { dirname } = await import('node:path'); const { fingerprint } = await import('../src/journal.js'); await mkdir(dirname(path), { recursive: true }); await writeFile( path, [ JSON.stringify({ seq: 1, kind: 'tool', fp: fingerprint('tool', { a: 1 }), result: 'old1' }), JSON.stringify({ seq: 2, kind: 'tool', fp: fingerprint('tool', { a: 2 }), result: 'old2' }), ].join('\n') + '\n', 'utf8', ); const j = new Journal(path, { bundleHash: 'test', rootRunId: 'test-root' }); await j.load(); expect(await j.step('tool', { a: 1 }, async () => 'live1')).toBe('live1'); expect(await j.step('tool', { a: 2 }, async () => 'live2')).toBe('live2'); expect(j.replayedCount).toBe(0); }); test('replaying 判据是「有没有实跑过」,并发下也成立', async () => { const path = tmpJournal(); const run1 = new Journal(path, { bundleHash: 'test', rootRunId: 'test-root' }); await run1.load(); await run1.step('tool', { a: 1 }, async () => 'r1', 'main/p1i0'); await run1.step('tool', { a: 2 }, async () => 'r2', 'main/p1i1'); const run2 = new Journal(path, { bundleHash: 'test', rootRunId: 'test-root' }); await run2.load(); expect(run2.replaying).toBe(true); // 先命中一次重放(乱序:先跑 1 号车道) await run2.step('tool', { a: 2 }, async () => 'x', 'main/p1i1'); expect(run2.replaying).toBe(true); // 一旦有实跑就翻 await run2.step('tool', { a: 999 }, async () => 'live'); expect(run2.replaying).toBe(false); }); });