|
1 | | -import { describe, test, expect } from "bun:test"; |
| 1 | +import { describe, test, expect, spyOn } from "bun:test"; |
2 | 2 | import fs from "node:fs"; |
3 | 3 | import os from "node:os"; |
4 | 4 | import path from "node:path"; |
@@ -616,6 +616,188 @@ async function gitLsTree(dir: string): Promise<string[]> { |
616 | 616 | } |
617 | 617 |
|
618 | 618 | describe("createOptimizedContextStore unpublished rewrite", () => { |
| 619 | + test("partial archive staging and failed rollback fence shared audit publication until reconciliation succeeds", async () => { |
| 620 | + const dir = tempDir(); |
| 621 | + const { storage } = await createSessionStores(dir); |
| 622 | + const archiveDir = path.join(dir, "evidence-archive"); |
| 623 | + fs.mkdirSync(archiveDir, { recursive: true }); |
| 624 | + fs.writeFileSync(path.join(archiveDir, "index.jsonl"), "original"); |
| 625 | + await storage.writeTurns([turn("old-a"), turn("old-b")]); |
| 626 | + await storage.writeMetadata(EMPTY_CHECKPOINT_METADATA); |
| 627 | + const published = await storage.commit({ message: "original" }); |
| 628 | + const { audit } = await createSessionStores(dir); |
| 629 | + fs.writeFileSync(path.join(archiveDir, "index.jsonl"), "changed"); |
| 630 | + fs.writeFileSync(path.join(archiveDir, "added.jsonl"), "new"); |
| 631 | + await storage.writeTurns([turn("[Compacted prior context]")]); |
| 632 | + const originalAdd = git.add; |
| 633 | + const originalReset = git.resetIndex; |
| 634 | + let resetFails = true; |
| 635 | + const staged: string[] = []; |
| 636 | + const addSpy = spyOn(git, "add").mockImplementation(async (args) => { |
| 637 | + if (args.dir === dir && args.filepath === "evidence-archive/index.jsonl") |
| 638 | + throw new Error("partial staging failure"); |
| 639 | + await originalAdd(args); |
| 640 | + if (args.dir === dir) |
| 641 | + staged.push(...(Array.isArray(args.filepath) ? args.filepath : [args.filepath])); |
| 642 | + }); |
| 643 | + const resetSpy = spyOn(git, "resetIndex").mockImplementation(async (args) => { |
| 644 | + if (args.dir === dir && args.filepath.startsWith("evidence-archive/") && resetFails) |
| 645 | + throw new Error("injected reset failure"); |
| 646 | + return originalReset(args); |
| 647 | + }); |
| 648 | + const errorRecord = { |
| 649 | + source: "reactor" as const, |
| 650 | + category: "test", |
| 651 | + message: "error", |
| 652 | + fatal: false, |
| 653 | + timestamp: new Date().toISOString(), |
| 654 | + sessionId: "s", |
| 655 | + seq: 1, |
| 656 | + }; |
| 657 | + try { |
| 658 | + await expect(storage.commit({ message: "partial" })).rejects.toThrow( |
| 659 | + "rollback remains pending", |
| 660 | + ); |
| 661 | + expect(staged).toContain("evidence-archive/added.jsonl"); |
| 662 | + await expect(audit.commitErrors([errorRecord])).rejects.toThrow("injected reset failure"); |
| 663 | + await expect( |
| 664 | + audit.commitAudit([ |
| 665 | + { |
| 666 | + callId: "a", |
| 667 | + tool: "read_file", |
| 668 | + arguments: {}, |
| 669 | + authz: null, |
| 670 | + result: { content: "ok", isError: false }, |
| 671 | + timestamp: new Date().toISOString(), |
| 672 | + sessionId: "s", |
| 673 | + seq: 2, |
| 674 | + }, |
| 675 | + ]), |
| 676 | + ).rejects.toThrow("injected reset failure"); |
| 677 | + expect(await git.resolveRef({ fs, dir, ref: "HEAD" })).toBe(published.hash); |
| 678 | + resetFails = false; |
| 679 | + await audit.commitErrors([errorRecord]); |
| 680 | + expect( |
| 681 | + (await git.listFiles({ fs, dir, ref: "HEAD" })).filter((name) => |
| 682 | + name.startsWith("evidence-archive"), |
| 683 | + ), |
| 684 | + ).toEqual(["evidence-archive/index.jsonl"]); |
| 685 | + const { blob } = await git.readBlob({ |
| 686 | + fs, |
| 687 | + dir, |
| 688 | + oid: await git.resolveRef({ fs, dir, ref: "HEAD" }), |
| 689 | + filepath: "evidence-archive/index.jsonl", |
| 690 | + }); |
| 691 | + expect(new TextDecoder().decode(blob)).toBe("original"); |
| 692 | + expect(fs.readFileSync(path.join(archiveDir, "index.jsonl"), "utf8")).toBe("changed"); |
| 693 | + } finally { |
| 694 | + addSpy.mockRestore(); |
| 695 | + resetSpy.mockRestore(); |
| 696 | + } |
| 697 | + await storage.commit({ message: "retry" }); |
| 698 | + expect( |
| 699 | + (await git.listFiles({ fs, dir, ref: "HEAD" })).filter((name) => |
| 700 | + name.startsWith("evidence-archive"), |
| 701 | + ), |
| 702 | + ).toEqual(["evidence-archive/added.jsonl", "evidence-archive/index.jsonl"]); |
| 703 | + }); |
| 704 | + test.each([true, false])( |
| 705 | + "failed compact restores concrete archive index leaves (already tracked: %s)", |
| 706 | + async (tracked) => { |
| 707 | + const dir = tempDir(); |
| 708 | + const { loadOrCreateCommitSigner } = await import("./commit-signer.js"); |
| 709 | + const sign = await loadOrCreateCommitSigner(dir); |
| 710 | + let fail = false; |
| 711 | + const { storage, audit } = await createSessionStores(dir, { |
| 712 | + signer: (payload) => { |
| 713 | + if (fail) throw new Error("injected signing failure"); |
| 714 | + return sign(payload); |
| 715 | + }, |
| 716 | + }); |
| 717 | + const archiveDir = path.join(dir, "evidence-archive"); |
| 718 | + fs.mkdirSync(archiveDir, { recursive: true }); |
| 719 | + if (tracked) { |
| 720 | + fs.writeFileSync(path.join(archiveDir, "index.jsonl"), "original evidence"); |
| 721 | + fs.writeFileSync(path.join(archiveDir, "deleted.jsonl"), "old evidence"); |
| 722 | + } |
| 723 | + await storage.writeTurns([turn("old-a"), turn("old-b")]); |
| 724 | + await storage.writeMetadata(EMPTY_CHECKPOINT_METADATA); |
| 725 | + const original = await storage.commit({ message: "original" }); |
| 726 | + const originalLeaves = (await git.listFiles({ fs, dir })).filter((name) => |
| 727 | + name.startsWith("evidence-archive"), |
| 728 | + ); |
| 729 | + fs.writeFileSync(path.join(archiveDir, "index.jsonl"), "new evidence"); |
| 730 | + fs.writeFileSync(path.join(archiveDir, "added.jsonl"), "added evidence"); |
| 731 | + if (tracked) fs.unlinkSync(path.join(archiveDir, "deleted.jsonl")); |
| 732 | + await storage.writeTurns([turn("[Compacted prior context]")]); |
| 733 | + fail = true; |
| 734 | + await expect(storage.commit({ message: "failed compact" })).rejects.toThrow( |
| 735 | + "injected signing failure", |
| 736 | + ); |
| 737 | + expect( |
| 738 | + (await git.listFiles({ fs, dir })).filter((name) => name.startsWith("evidence-archive")), |
| 739 | + ).toEqual(originalLeaves); |
| 740 | + expect(await git.resolveRef({ fs, dir, ref: "HEAD" })).toBe(original.hash); |
| 741 | + for (const [, head, , stage] of await git.statusMatrix({ |
| 742 | + fs, |
| 743 | + dir, |
| 744 | + filepaths: ["evidence-archive"], |
| 745 | + })) |
| 746 | + expect(stage).toBe(head); |
| 747 | + expect(fs.readFileSync(path.join(archiveDir, "index.jsonl"), "utf8")).toBe("new evidence"); |
| 748 | + fail = false; |
| 749 | + await audit.commitAudit([ |
| 750 | + { |
| 751 | + callId: "audit", |
| 752 | + tool: "read_file", |
| 753 | + arguments: {}, |
| 754 | + authz: null, |
| 755 | + result: { content: "ok", isError: false }, |
| 756 | + timestamp: new Date().toISOString(), |
| 757 | + sessionId: "s", |
| 758 | + seq: 1, |
| 759 | + }, |
| 760 | + ]); |
| 761 | + await audit.commitErrors([ |
| 762 | + { |
| 763 | + source: "reactor", |
| 764 | + category: "test", |
| 765 | + message: "test error", |
| 766 | + fatal: false, |
| 767 | + timestamp: new Date().toISOString(), |
| 768 | + sessionId: "s", |
| 769 | + seq: 2, |
| 770 | + }, |
| 771 | + ]); |
| 772 | + expect( |
| 773 | + (await git.listFiles({ fs, dir, ref: "HEAD" })).filter((name) => |
| 774 | + name.startsWith("evidence-archive"), |
| 775 | + ), |
| 776 | + ).toEqual(originalLeaves); |
| 777 | + if (tracked) { |
| 778 | + const { blob } = await git.readBlob({ |
| 779 | + fs, |
| 780 | + dir, |
| 781 | + oid: await git.resolveRef({ fs, dir, ref: "HEAD" }), |
| 782 | + filepath: "evidence-archive/index.jsonl", |
| 783 | + }); |
| 784 | + expect(new TextDecoder().decode(blob)).toBe("original evidence"); |
| 785 | + } |
| 786 | + await storage.commit({ message: "retry compact" }); |
| 787 | + expect( |
| 788 | + (await git.listFiles({ fs, dir, ref: "HEAD" })).filter((name) => |
| 789 | + name.startsWith("evidence-archive"), |
| 790 | + ), |
| 791 | + ).toEqual(["evidence-archive/added.jsonl", "evidence-archive/index.jsonl"]); |
| 792 | + const { blob } = await git.readBlob({ |
| 793 | + fs, |
| 794 | + dir, |
| 795 | + oid: await git.resolveRef({ fs, dir, ref: "HEAD" }), |
| 796 | + filepath: "evidence-archive/index.jsonl", |
| 797 | + }); |
| 798 | + expect(new TextDecoder().decode(blob)).toBe("new evidence"); |
| 799 | + }, |
| 800 | + ); |
619 | 801 | test("rewrite writeTurns stays off the live generation until commit", async () => { |
620 | 802 | const dir = tempDir(); |
621 | 803 | const store = await createOptimizedContextStore(dir); |
|
0 commit comments