|
2 | 2 | import Database from "better-sqlite3"; |
3 | 3 | import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
4 | 4 |
|
5 | | -import { createAgentCheckpoint, resumeFromCheckpoint, getCheckpointMetrics } from "@/lib/agent-checkpoints"; |
| 5 | +import { |
| 6 | + createAgentCheckpoint, |
| 7 | + getCheckpointMetrics, |
| 8 | + resumeFromCheckpoint, |
| 9 | + verifyCheckpointAuditChain, |
| 10 | +} from "@/lib/agent-checkpoints"; |
6 | 11 | import { initSchema } from "@/lib/db-schema"; |
| 12 | +import { recordEfficiencyEvent } from "@/lib/efficiency-telemetry"; |
7 | 13 |
|
8 | 14 | let db: Database.Database; |
9 | 15 |
|
@@ -61,4 +67,130 @@ describe("agent lightweight checkpoint/resume", () => { |
61 | 67 | expect(metrics.avgWriteLatencyMs).toBeGreaterThan(0); |
62 | 68 | expect(metrics.avgCheckpointSize).toBeGreaterThan(0); |
63 | 69 | }); |
| 70 | + |
| 71 | + it("writes a checkpoint audit entry with verified boundary provenance receipts", () => { |
| 72 | + const runId = "test-run-provenance"; |
| 73 | + |
| 74 | + recordEfficiencyEvent(db, { |
| 75 | + eventType: "source_read", |
| 76 | + taskId: runId, |
| 77 | + agentId: "codex", |
| 78 | + payload: { |
| 79 | + sourceId: "README.md", |
| 80 | + sourceHash: "sha256:readme", |
| 81 | + toolId: "read_file", |
| 82 | + }, |
| 83 | + createdAt: "2026-07-02T08:00:00.000Z", |
| 84 | + }); |
| 85 | + recordEfficiencyEvent(db, { |
| 86 | + eventType: "memory_write", |
| 87 | + taskId: runId, |
| 88 | + agentId: "codex", |
| 89 | + payload: { |
| 90 | + source: "agent_memory", |
| 91 | + dedupHash: "sha256:memory", |
| 92 | + firstSeenAt: "2026-07-02T07:59:00.000Z", |
| 93 | + isRediscovery: false, |
| 94 | + }, |
| 95 | + createdAt: "2026-07-02T08:01:00.000Z", |
| 96 | + }); |
| 97 | + |
| 98 | + const checkpoint = createAgentCheckpoint(db, { |
| 99 | + runId, |
| 100 | + ownerAgentId: "codex", |
| 101 | + objective: "Capture verified provenance", |
| 102 | + nextSafeAction: "Resume from checkpoint", |
| 103 | + provenancePointers: ["agent-supplied-pointer"], |
| 104 | + }); |
| 105 | + |
| 106 | + expect(checkpoint.provenanceAudit).toMatchObject({ |
| 107 | + provenanceReceiptCount: 2, |
| 108 | + previousEntryHash: null, |
| 109 | + }); |
| 110 | + expect(checkpoint.provenanceAudit?.checkpointHash).toMatch(/^sha256:[a-f0-9]{64}$/); |
| 111 | + expect(checkpoint.provenanceAudit?.entryHash).toMatch(/^sha256:[a-f0-9]{64}$/); |
| 112 | + |
| 113 | + const row = db |
| 114 | + .prepare("SELECT * FROM audit_entries WHERE event_type = 'agent.checkpointed' AND entity_id = ?") |
| 115 | + .get(`agent_checkpoint:${checkpoint.id}`) as { metadata_json: string } | undefined; |
| 116 | + expect(row).toBeDefined(); |
| 117 | + |
| 118 | + const metadata = JSON.parse(row!.metadata_json) as { |
| 119 | + provenanceReceipts: Array<{ kind: string; sourceId: string; sourceHash: string }>; |
| 120 | + legacyPointerCount: number; |
| 121 | + }; |
| 122 | + expect(metadata.legacyPointerCount).toBe(1); |
| 123 | + expect(metadata.provenanceReceipts).toHaveLength(2); |
| 124 | + expect(metadata.provenanceReceipts.map((receipt) => receipt.kind).sort()).toEqual([ |
| 125 | + "memory_write", |
| 126 | + "source_read", |
| 127 | + ]); |
| 128 | + expect(JSON.stringify(metadata.provenanceReceipts)).not.toContain("agent-supplied-pointer"); |
| 129 | + |
| 130 | + const resumed = resumeFromCheckpoint(db, "default-tenant", runId); |
| 131 | + expect(resumed?.provenanceAudit?.entryHash).toBe(checkpoint.provenanceAudit?.entryHash); |
| 132 | + |
| 133 | + expect(verifyCheckpointAuditChain(db, "default-tenant")).toMatchObject({ |
| 134 | + valid: true, |
| 135 | + checked: 1, |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + it("rolls back the checkpoint insert when the transactional audit write fails", () => { |
| 140 | + db.exec("DROP TABLE audit_entries"); |
| 141 | + |
| 142 | + expect(() => |
| 143 | + createAgentCheckpoint(db, { |
| 144 | + runId: "test-run-rollback", |
| 145 | + ownerAgentId: "codex", |
| 146 | + objective: "Prove checkpoint audit atomicity", |
| 147 | + nextSafeAction: "Retry after schema repair", |
| 148 | + }) |
| 149 | + ).toThrow(/audit_entries/); |
| 150 | + |
| 151 | + expect(db.prepare("SELECT COUNT(*) AS count FROM agent_checkpoints").get()).toEqual({ |
| 152 | + count: 0, |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + it("detects a broken checkpoint audit chain row", () => { |
| 157 | + createAgentCheckpoint(db, { |
| 158 | + runId: "test-run-chain", |
| 159 | + ownerAgentId: "codex", |
| 160 | + objective: "Create a valid checkpoint audit row", |
| 161 | + nextSafeAction: "Insert forged audit row", |
| 162 | + }); |
| 163 | + |
| 164 | + db.prepare( |
| 165 | + `INSERT INTO audit_entries |
| 166 | + (id, tenant_id, actor_id, actor_role, event_type, entity_type, entity_id, reason, metadata_json, created_at) |
| 167 | + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` |
| 168 | + ).run( |
| 169 | + "forged-checkpoint-audit", |
| 170 | + "default-tenant", |
| 171 | + "codex", |
| 172 | + "system", |
| 173 | + "agent.checkpointed", |
| 174 | + "agent_checkpoint", |
| 175 | + "agent_checkpoint:forged", |
| 176 | + "forged checkpoint audit row", |
| 177 | + JSON.stringify({ |
| 178 | + schemaVersion: 1, |
| 179 | + checkpointId: "forged", |
| 180 | + runId: "test-run-chain", |
| 181 | + checkpointHash: "sha256:forged", |
| 182 | + previousEntryHash: "sha256:not-the-prior-row", |
| 183 | + provenanceReceipts: [], |
| 184 | + legacyPointerCount: 0, |
| 185 | + entryHash: "sha256:not-a-real-entry-hash", |
| 186 | + }), |
| 187 | + "2026-07-02T08:02:00.000Z" |
| 188 | + ); |
| 189 | + |
| 190 | + expect(verifyCheckpointAuditChain(db, "default-tenant")).toMatchObject({ |
| 191 | + valid: false, |
| 192 | + checked: 2, |
| 193 | + firstBrokenEntryId: "forged-checkpoint-audit", |
| 194 | + }); |
| 195 | + }); |
64 | 196 | }); |
0 commit comments