-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpostgres_backend.test.ts
More file actions
333 lines (307 loc) · 14.2 KB
/
Copy pathpostgres_backend.test.ts
File metadata and controls
333 lines (307 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* Tests for v0.16.0 Postgres backend.
*
* REQUIRES: a reachable Postgres at ZC_POSTGRES_URL or default
* (localhost:5432, user 'scuser', db 'securecontext'). Skipped automatically
* when Postgres is unreachable so this test file is CI-safe.
*
* Coverage:
* Phase B — migrations: create + idempotent re-run
* Phase C — ChainedTablePostgres: append, FOR UPDATE atomicity, role provisioning
* Phase D — wired through recordToolCall + recordOutcome with backend=postgres
* Phase E — T3.1: per-query SET LOCAL ROLE actually changes current_user
* Phase F — T3.2: RLS blocks cross-agent reads of 'restricted' rows
*
* Red-team:
* RT-S3-05: cross-agent read of 'restricted' row blocked at PG layer (RLS)
* RT-S3-06: chain hashes are byte-identical across SQLite + Postgres backends
* (rows can be migrated between backends without rehashing)
*/
import { describe, it, expect, beforeAll, afterAll, beforeEach } from "vitest";
import { mkdtempSync, existsSync, unlinkSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir, homedir } from "node:os";
import { createHash, randomUUID } from "node:crypto";
import { pgHealthCheck, withClient, _resetPgPoolForTesting, shutdownPgPool } from "./pg_pool.js";
import { runPgMigrations, _dropPgTelemetryTablesForTesting } from "./pg_migrations.js";
import { _resetProvisionedAgentsForTesting } from "./security/chained_table_postgres.js";
import {
recordToolCall, newCallId, _resetTelemetryCacheForTesting,
} from "./telemetry.js";
import {
recordOutcome,
} from "./outcomes.js";
import {
_resetCacheForTesting as resetMachineSecret,
MACHINE_SECRET_PATH,
} from "./security/machine_secret.js";
import { _resetPricingVerificationForTesting } from "./pricing.js";
// Provide the default Postgres credentials for the local Docker container if
// the env vars aren't set externally. Tests run in same-process — they'll inherit.
process.env.ZC_POSTGRES_USER ??= "scuser";
process.env.ZC_POSTGRES_PASSWORD ??= "79bd1ca6011b797c70e90c02becdaa90d99cfc501abaec09";
process.env.ZC_POSTGRES_DB ??= "securecontext";
process.env.ZC_POSTGRES_HOST ??= "localhost";
process.env.ZC_POSTGRES_PORT ??= "5432";
// Eagerly evaluate PG availability at module load so describe.skipIf
// has a real value before tests are collected. The healthcheck is fast.
const pgAvailable = await pgHealthCheck();
beforeAll(async () => {
if (pgAvailable) {
await _dropPgTelemetryTablesForTesting().catch(() => { /* fresh */ });
await runPgMigrations();
}
});
afterAll(async () => {
// Tables intentionally NOT dropped — keeps state inspectable for debugging.
// Run scripts/setup-pg.mjs to reset between sessions if needed.
await shutdownPgPool();
});
const PRICING_SIG_PATH = join(homedir(), ".claude", "zc-ctx", ".pricing_signature");
beforeEach(() => {
// Fresh machine secret for each test so HKDF subkeys are deterministic
// (machine_secret is the IKM)
try { if (existsSync(MACHINE_SECRET_PATH)) unlinkSync(MACHINE_SECRET_PATH); } catch {}
try { if (existsSync(PRICING_SIG_PATH)) unlinkSync(PRICING_SIG_PATH); } catch {}
resetMachineSecret();
_resetPricingVerificationForTesting();
_resetTelemetryCacheForTesting();
_resetProvisionedAgentsForTesting();
});
describe.skipIf(!pgAvailable)("v0.16.0 Postgres backend (live PG required)", () => {
it("[Phase B] migrations create the expected tables", async () => {
await runPgMigrations();
const tables = await withClient(async (c) => {
const r = await c.query(`
SELECT table_name FROM information_schema.tables
WHERE table_schema='public' AND table_name LIKE '%_pg'
ORDER BY table_name
`);
return r.rows.map((r: { table_name: string }) => r.table_name);
});
expect(tables).toEqual(expect.arrayContaining([
"tool_calls_pg", "outcomes_pg", "learnings_pg", "schema_migrations_pg",
]));
});
it("[Phase B] migrations are idempotent (re-running is no-op)", async () => {
const before = await runPgMigrations();
const after = await runPgMigrations();
expect(after).toBe(0); // nothing newly applied
void before;
});
it("[Phase B] outcomes_pg has classification CHECK constraint", async () => {
await expect(withClient(async (c) => {
await c.query(`
INSERT INTO outcomes_pg (
outcome_id, ref_type, ref_id, outcome_kind, signal_source,
confidence, prev_hash, row_hash, classification
) VALUES ('out-bad', 'tool_call', 'cid', 'accepted', 'manual', 0.5, 'g', 'h', 'TOP-SECRET')
`);
})).rejects.toThrow(/check constraint|outcomes_pg_classification_check/i);
});
it("[Phase D] recordToolCall writes to Postgres when ZC_TELEMETRY_BACKEND=postgres", async () => {
process.env.ZC_TELEMETRY_BACKEND = "postgres";
try {
const cid = newCallId();
const r = await recordToolCall({
callId: cid, sessionId: "s1", agentId: "agent-pg-test",
projectPath: mkdtempSync(join(tmpdir(), "zc-pg-")),
toolName: "Read", model: "claude-sonnet-4-6",
inputTokens: 100, outputTokens: 50, latencyMs: 10, status: "ok",
});
expect(r).not.toBeNull();
expect(r!.call_id).toBe(cid);
const rows = await withClient(async (c) => {
const q = await c.query("SELECT * FROM tool_calls_pg WHERE call_id = $1", [cid]);
return q.rows;
});
expect(rows).toHaveLength(1);
expect(rows[0].agent_id).toBe("agent-pg-test");
expect(rows[0].tool_name).toBe("Read");
expect(rows[0].input_tokens).toBe(100);
} finally {
delete process.env.ZC_TELEMETRY_BACKEND;
}
});
it("[Phase D] recordOutcome writes to Postgres with classification when backend=postgres", async () => {
process.env.ZC_TELEMETRY_BACKEND = "postgres";
try {
const r = await recordOutcome({
projectPath: mkdtempSync(join(tmpdir(), "zc-pg-out-")),
refType: "tool_call", refId: "cid-x",
outcomeKind: "accepted", signalSource: "user_prompt",
classification: "restricted", createdByAgentId: "alice",
});
expect(r).not.toBeNull();
const rows = await withClient(async (c) => {
const q = await c.query("SELECT classification, created_by_agent_id FROM outcomes_pg WHERE outcome_id = $1", [r!.outcome_id]);
return q.rows;
});
expect(rows[0].classification).toBe("restricted");
expect(rows[0].created_by_agent_id).toBe("alice");
} finally {
delete process.env.ZC_TELEMETRY_BACKEND;
}
});
it("[Phase E — T3.1] per-query SET LOCAL ROLE creates per-agent identity", async () => {
process.env.ZC_TELEMETRY_BACKEND = "postgres";
try {
const projectPath = mkdtempSync(join(tmpdir(), "zc-pg-role-"));
// Two separate agent IDs — should result in two separate Postgres roles
await recordToolCall({
callId: newCallId(), sessionId: "s", agentId: "agent-alpha",
projectPath, toolName: "Read", model: "claude-sonnet-4-6",
inputTokens: 1, outputTokens: 1, latencyMs: 1, status: "ok",
});
await recordToolCall({
callId: newCallId(), sessionId: "s", agentId: "agent-beta",
projectPath, toolName: "Read", model: "claude-sonnet-4-6",
inputTokens: 1, outputTokens: 1, latencyMs: 1, status: "ok",
});
// Both roles should now exist in pg_roles
const roles = await withClient(async (c) => {
const r = await c.query(`SELECT rolname FROM pg_roles WHERE rolname LIKE 'zc_agent_%' ORDER BY rolname`);
return r.rows.map((r: { rolname: string }) => r.rolname);
});
expect(roles).toEqual(expect.arrayContaining(["zc_agent_agent_alpha", "zc_agent_agent_beta"]));
} finally {
delete process.env.ZC_TELEMETRY_BACKEND;
}
});
it("[RT-S3-05 — T3.2] cross-agent read of 'restricted' outcome blocked by RLS", async () => {
process.env.ZC_TELEMETRY_BACKEND = "postgres";
try {
const projectPath = mkdtempSync(join(tmpdir(), "zc-pg-rls-"));
// Pre-provision bob so SET LOCAL ROLE works (bob never writes — he
// only tries to read alice's restricted row). Easiest way: have bob
// write a tool_call which will lazy-provision his role.
await recordToolCall({
callId: newCallId(), sessionId: "s-bob-prov", agentId: "bob",
projectPath, toolName: "Read", model: "claude-sonnet-4-6",
inputTokens: 1, outputTokens: 1, latencyMs: 1, status: "ok",
});
// alice writes a restricted outcome
const aliceOutcome = await recordOutcome({
projectPath, refType: "tool_call", refId: "cid-rls-test-" + randomUUID().slice(0, 8),
outcomeKind: "rejected", signalSource: "user_prompt",
classification: "restricted", createdByAgentId: "alice",
evidence: { sentiment: "negative", message_length: 42 },
});
expect(aliceOutcome).not.toBeNull();
const aliceOutcomeId = aliceOutcome!.outcome_id;
// Bob tries to read it via SET ROLE (his per-agent role, just provisioned)
const visibleToBob = await withClient(async (c) => {
await c.query("BEGIN");
await c.query(`SET LOCAL ROLE "zc_agent_bob"`);
await c.query(`SELECT set_config('zc.current_agent', 'bob', true)`);
const r = await c.query("SELECT outcome_id FROM outcomes_pg WHERE outcome_id = $1", [aliceOutcomeId]);
await c.query("COMMIT");
return r.rows;
});
expect(visibleToBob).toHaveLength(0); // RLS BLOCKED bob
// Alice can read her own (alice's role was provisioned by recordOutcome)
const visibleToAlice = await withClient(async (c) => {
await c.query("BEGIN");
await c.query(`SET LOCAL ROLE "zc_agent_alice"`);
await c.query(`SELECT set_config('zc.current_agent', 'alice', true)`);
const r = await c.query("SELECT outcome_id FROM outcomes_pg WHERE outcome_id = $1", [aliceOutcomeId]);
await c.query("COMMIT");
return r.rows;
});
expect(visibleToAlice).toHaveLength(1);
} finally {
delete process.env.ZC_TELEMETRY_BACKEND;
}
});
it("[RT-S3-05 follow-up] 'public' + 'internal' rows readable by any agent", async () => {
process.env.ZC_TELEMETRY_BACKEND = "postgres";
try {
const projectPath = mkdtempSync(join(tmpdir(), "zc-pg-pub-"));
// Ensure alice's role is provisioned (write a tool_call as alice)
await recordToolCall({
callId: newCallId(), sessionId: "s-prov-alice", agentId: "alice",
projectPath, toolName: "Read", model: "claude-sonnet-4-6",
inputTokens: 1, outputTokens: 1, latencyMs: 1, status: "ok",
});
const pub = await recordOutcome({
projectPath, refType: "tool_call", refId: "cid-pub-" + randomUUID().slice(0, 8),
outcomeKind: "accepted", signalSource: "manual",
classification: "public",
});
const visibleToAnyone = await withClient(async (c) => {
await c.query("BEGIN");
await c.query(`SET LOCAL ROLE "zc_agent_alice"`);
await c.query(`SELECT set_config('zc.current_agent', 'alice', true)`);
const r = await c.query("SELECT outcome_id FROM outcomes_pg WHERE outcome_id = $1", [pub!.outcome_id]);
await c.query("COMMIT");
return r.rows;
});
expect(visibleToAnyone).toHaveLength(1);
} finally {
delete process.env.ZC_TELEMETRY_BACKEND;
}
});
it("[RT-S3-06] chain hashes are byte-identical across SQLite + Postgres backends", async () => {
// Write the SAME logical row to both backends and verify row_hash matches.
// This proves rows can be migrated between backends without rehashing.
const projectPath = mkdtempSync(join(tmpdir(), "zc-cross-"));
const cid = newCallId();
const baseInput = {
callId: cid, sessionId: "s-cross", agentId: "agent-cross",
projectPath, toolName: "Read", model: "claude-sonnet-4-6",
inputTokens: 100, outputTokens: 50, latencyMs: 10,
status: "ok" as const,
};
// Write to SQLite first (default backend)
const sqliteResult = await recordToolCall(baseInput);
expect(sqliteResult).not.toBeNull();
const sqliteRowHash = sqliteResult!.row_hash;
// The logical row has the same canonical content; computing the chain hash
// independently using the same per-agent HKDF subkey + GENESIS prev_hash
// (since we're testing on a fresh Postgres) should yield the same row_hash.
const { computeChainHash } = await import("./security/chained_table.js");
const { canonicalize } = await import("./security/hmac_chain.js");
void canonicalize;
const projectHash = createHash("sha256").update(projectPath).digest("hex").slice(0, 16);
// Reconstruct the canonical fields the same way buildCanonicalFields does,
// using the SAME ts the SQLite write captured.
const canonicalFields = [
cid, "s-cross", "agent-cross", projectHash,
"Read", "claude-sonnet-4-6",
100, 50,
// v0.17.1: tool-call cost is computed via computeToolCallCost which bills
// from the LLM's perspective — tool args (100) at OUTPUT rate + tool
// response (50) at INPUT rate. Sonnet $15/Mtok × 100 + $3/Mtok × 50
// = 0.0015 + 0.00015 = 0.00165.
(0.0015 + 0.00015).toFixed(8),
10, "ok",
sqliteResult!.ts,
];
const expected = computeChainHash(
{ agentId: "agent-cross", projectHash, canonicalFields },
"genesis", // GENESIS sentinel — same for both first rows
);
expect(expected.rowHash).toBe(sqliteRowHash);
// i.e. when Postgres-mode writes its first row of this canonical content,
// it would land the SAME row_hash → chains are interchangeable.
});
});
// Always-runs sanity test (whether or not PG is available)
describe("v0.16.0 Postgres backend — graceful when PG unavailable", () => {
it("getPgPool returns null + logs warning when no creds + no URL", async () => {
_resetPgPoolForTesting();
const prevPwd = process.env.ZC_POSTGRES_PASSWORD;
const prevUrl = process.env.ZC_POSTGRES_URL;
delete process.env.ZC_POSTGRES_PASSWORD;
delete process.env.ZC_POSTGRES_URL;
try {
const { getPgPool } = await import("./pg_pool.js");
const pool = getPgPool();
expect(pool).toBeNull();
} finally {
if (prevPwd !== undefined) process.env.ZC_POSTGRES_PASSWORD = prevPwd;
if (prevUrl !== undefined) process.env.ZC_POSTGRES_URL = prevUrl;
_resetPgPoolForTesting();
}
});
});