|
| 1 | +import path from "node:path"; |
| 2 | +import { expectDefined } from "@openclaw/normalization-core"; |
| 3 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 4 | +import { cleanupTempDirs, makeTempDir } from "../../../test/helpers/temp-dir.js"; |
| 5 | +import { |
| 6 | + closeOpenClawAgentDatabasesForTest, |
| 7 | + openOpenClawAgentDatabase, |
| 8 | +} from "../../state/openclaw-agent-db.js"; |
| 9 | +import { |
| 10 | + appendTranscriptMessage, |
| 11 | + appendTranscriptMessages, |
| 12 | + upsertSessionEntryCore, |
| 13 | +} from "./session-accessor.js"; |
| 14 | +import { resolveSqliteTargetFromSessionStorePath } from "./session-sqlite-target.js"; |
| 15 | + |
| 16 | +describe("SQLite transcript idempotency redaction", () => { |
| 17 | + const tempDirs: string[] = []; |
| 18 | + let storePath: string; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + storePath = path.join( |
| 22 | + makeTempDir(tempDirs, "openclaw-idempotency-redaction-"), |
| 23 | + "sessions.json", |
| 24 | + ); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + closeOpenClawAgentDatabasesForTest(); |
| 29 | + cleanupTempDirs(tempDirs); |
| 30 | + }); |
| 31 | + |
| 32 | + it.each([ |
| 33 | + { |
| 34 | + idempotencyKey: "7c311c86-7836-4f47-bffc-aa8c3cbf261e:user", |
| 35 | + name: "credential-like UUID", |
| 36 | + }, |
| 37 | + { |
| 38 | + idempotencyKey: "sk-abcdef1234567890xyz:user", |
| 39 | + name: "caller-supplied secret", |
| 40 | + }, |
| 41 | + ])("keeps $name identity outside redacted event JSON", async ({ idempotencyKey }) => { |
| 42 | + const scope = { |
| 43 | + agentId: "main", |
| 44 | + sessionId: "session-redacted-replay", |
| 45 | + sessionKey: "agent:main:redacted-replay", |
| 46 | + storePath, |
| 47 | + }; |
| 48 | + await upsertSessionEntryCore(scope, { sessionId: scope.sessionId, updatedAt: 10 }); |
| 49 | + const message = { |
| 50 | + role: "user", |
| 51 | + content: "same secret sk-abcdef1234567890xyz", |
| 52 | + idempotencyKey, |
| 53 | + timestamp: 1, |
| 54 | + }; |
| 55 | + const first = await appendTranscriptMessage(scope, { |
| 56 | + idempotencyLookup: "scan", |
| 57 | + message, |
| 58 | + }); |
| 59 | + const replay = await appendTranscriptMessage(scope, { |
| 60 | + idempotencyLookup: "scan", |
| 61 | + message, |
| 62 | + }); |
| 63 | + const databasePath = expectDefined( |
| 64 | + resolveSqliteTargetFromSessionStorePath(storePath, { agentId: "main" }).path, |
| 65 | + "idempotency redaction database path", |
| 66 | + ); |
| 67 | + const database = openOpenClawAgentDatabase({ agentId: "main", path: databasePath }); |
| 68 | + const stored = database.db |
| 69 | + .prepare( |
| 70 | + `SELECT identity.message_idempotency_key AS idempotencyKey, |
| 71 | + event.event_json AS eventJson |
| 72 | + FROM transcript_event_identities AS identity |
| 73 | + JOIN transcript_events AS event |
| 74 | + ON event.session_id = identity.session_id AND event.seq = identity.seq |
| 75 | + WHERE identity.session_id = ? AND identity.event_id = ?`, |
| 76 | + ) |
| 77 | + .get(scope.sessionId, first.messageId) as |
| 78 | + | { eventJson: string; idempotencyKey: string } |
| 79 | + | undefined; |
| 80 | + |
| 81 | + expect(first.appended).toBe(true); |
| 82 | + expect(JSON.stringify(first.message)).not.toContain(idempotencyKey); |
| 83 | + expect(JSON.stringify(first.message)).not.toContain("sk-abcdef1234567890xyz"); |
| 84 | + expect(stored).toMatchObject({ idempotencyKey }); |
| 85 | + expect(stored?.eventJson).not.toContain(idempotencyKey); |
| 86 | + expect(replay).toMatchObject({ appended: false, messageId: first.messageId }); |
| 87 | + }); |
| 88 | + |
| 89 | + it("replays a redacted assistant message through its raw identity index", async () => { |
| 90 | + const scope = { |
| 91 | + agentId: "main", |
| 92 | + sessionId: "session-redacted-assistant-replay", |
| 93 | + sessionKey: "agent:main:redacted-assistant-replay", |
| 94 | + storePath, |
| 95 | + }; |
| 96 | + const idempotencyKey = "sk-abcdef1234567890xyz:assistant"; |
| 97 | + await upsertSessionEntryCore(scope, { sessionId: scope.sessionId, updatedAt: 10 }); |
| 98 | + const message = { |
| 99 | + role: "assistant", |
| 100 | + content: [{ type: "text", text: "same secret sk-abcdef1234567890xyz" }], |
| 101 | + idempotencyKey, |
| 102 | + timestamp: 1, |
| 103 | + }; |
| 104 | + |
| 105 | + const first = await appendTranscriptMessage(scope, { |
| 106 | + idempotencyLookup: "scan-assistant", |
| 107 | + message, |
| 108 | + }); |
| 109 | + const replay = await appendTranscriptMessage(scope, { |
| 110 | + idempotencyLookup: "scan-assistant", |
| 111 | + message, |
| 112 | + }); |
| 113 | + const databasePath = expectDefined( |
| 114 | + resolveSqliteTargetFromSessionStorePath(storePath, { agentId: "main" }).path, |
| 115 | + "assistant idempotency redaction database path", |
| 116 | + ); |
| 117 | + const database = openOpenClawAgentDatabase({ agentId: "main", path: databasePath }); |
| 118 | + const stored = database.db |
| 119 | + .prepare( |
| 120 | + `SELECT identity.message_idempotency_key AS idempotencyKey, |
| 121 | + event.event_json AS eventJson |
| 122 | + FROM transcript_event_identities AS identity |
| 123 | + JOIN transcript_events AS event |
| 124 | + ON event.session_id = identity.session_id AND event.seq = identity.seq |
| 125 | + WHERE identity.session_id = ? AND identity.event_id = ?`, |
| 126 | + ) |
| 127 | + .get(scope.sessionId, first.messageId) as |
| 128 | + | { eventJson: string; idempotencyKey: string } |
| 129 | + | undefined; |
| 130 | + |
| 131 | + expect(first.appended).toBe(true); |
| 132 | + expect(replay).toMatchObject({ appended: false, messageId: first.messageId }); |
| 133 | + expect(JSON.stringify(first.message)).not.toContain(idempotencyKey); |
| 134 | + expect(stored).toMatchObject({ idempotencyKey }); |
| 135 | + expect(stored?.eventJson).not.toContain(idempotencyKey); |
| 136 | + }); |
| 137 | + |
| 138 | + it("preserves raw replay identity across a pre-redacted atomic batch", async () => { |
| 139 | + const scope = { |
| 140 | + agentId: "main", |
| 141 | + sessionId: "session-redacted-batch-replay", |
| 142 | + sessionKey: "agent:main:redacted-batch-replay", |
| 143 | + storePath, |
| 144 | + }; |
| 145 | + const idempotencyKey = "sk-abcdef1234567890xyz:batch"; |
| 146 | + await upsertSessionEntryCore(scope, { sessionId: scope.sessionId, updatedAt: 10 }); |
| 147 | + const message = { |
| 148 | + role: "user", |
| 149 | + content: "same secret sk-abcdef1234567890xyz", |
| 150 | + idempotencyKey, |
| 151 | + timestamp: 1, |
| 152 | + }; |
| 153 | + |
| 154 | + const [first] = await appendTranscriptMessages(scope, { |
| 155 | + messages: [{ idempotencyLookup: "scan", message }], |
| 156 | + }); |
| 157 | + const [replay] = await appendTranscriptMessages(scope, { |
| 158 | + messages: [{ idempotencyLookup: "scan", message }], |
| 159 | + }); |
| 160 | + const databasePath = expectDefined( |
| 161 | + resolveSqliteTargetFromSessionStorePath(storePath, { agentId: "main" }).path, |
| 162 | + "batch idempotency redaction database path", |
| 163 | + ); |
| 164 | + const database = openOpenClawAgentDatabase({ agentId: "main", path: databasePath }); |
| 165 | + const stored = database.db |
| 166 | + .prepare( |
| 167 | + `SELECT identity.message_idempotency_key AS idempotencyKey, |
| 168 | + event.event_json AS eventJson |
| 169 | + FROM transcript_event_identities AS identity |
| 170 | + JOIN transcript_events AS event |
| 171 | + ON event.session_id = identity.session_id AND event.seq = identity.seq |
| 172 | + WHERE identity.session_id = ? AND identity.event_id = ?`, |
| 173 | + ) |
| 174 | + .get(scope.sessionId, expectDefined(first?.messageId, "batch first message id")) as |
| 175 | + | { eventJson: string; idempotencyKey: string } |
| 176 | + | undefined; |
| 177 | + |
| 178 | + expect(first).toMatchObject({ appended: true }); |
| 179 | + expect(replay).toMatchObject({ appended: false, messageId: first?.messageId }); |
| 180 | + expect(stored).toMatchObject({ idempotencyKey }); |
| 181 | + expect(stored?.eventJson).not.toContain(idempotencyKey); |
| 182 | + expect(stored?.eventJson).not.toContain("sk-abcdef1234567890xyz"); |
| 183 | + }); |
| 184 | +}); |
0 commit comments