File size: 11,312 Bytes
34810d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import fs from "node:fs";
import path from "node:path";
import { DatabaseSync } from "node:sqlite";
import { gzipSync } from "node:zlib";
import { afterEach, describe, expect, it } from "vitest";
import { cleanupTempDirs, makeTempDir } from "../../test/helpers/temp-dir.js";
import { OPENCLAW_STATE_SCHEMA_VERSION } from "../state/openclaw-state-db-contract.js";
import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths.js";
import { createDebugProxyCaptureReader } from "./store-readonly.js";

const cleanupDirs: string[] = [];

afterEach(() => {
  cleanupTempDirs(cleanupDirs);
});

function makeStateEnv(prefix: string): NodeJS.ProcessEnv {
  return { OPENCLAW_STATE_DIR: makeTempDir(cleanupDirs, prefix) };
}

function createCaptureDatabase(env: NodeJS.ProcessEnv, schemaVersion: number): string {
  const databasePath = resolveOpenClawStateSqlitePath(env);
  fs.mkdirSync(path.dirname(databasePath), { recursive: true });
  const db = new DatabaseSync(databasePath);
  try {
    db.exec(`
      PRAGMA user_version = ${schemaVersion};
      CREATE TABLE workspace_attestations (
        workspace_key TEXT PRIMARY KEY,
        attested_at_ms INTEGER NOT NULL,
        updated_at_ms INTEGER NOT NULL
      ) STRICT;
      CREATE TABLE capture_sessions (
        id TEXT NOT NULL PRIMARY KEY,
        started_at INTEGER NOT NULL,
        ended_at INTEGER,
        mode TEXT NOT NULL,
        source_scope TEXT NOT NULL,
        source_process TEXT NOT NULL,
        proxy_url TEXT
      ) STRICT;
      CREATE TABLE capture_blobs (
        blob_id TEXT NOT NULL PRIMARY KEY,
        content_type TEXT,
        encoding TEXT NOT NULL,
        size_bytes INTEGER NOT NULL,
        sha256 TEXT NOT NULL,
        data BLOB NOT NULL,
        created_at INTEGER NOT NULL
      ) STRICT;
      CREATE TABLE capture_events (
        id INTEGER NOT NULL PRIMARY KEY,
        session_id TEXT NOT NULL,
        ts INTEGER NOT NULL,
        source_scope TEXT NOT NULL,
        source_process TEXT NOT NULL,
        protocol TEXT NOT NULL,
        direction TEXT NOT NULL,
        kind TEXT NOT NULL,
        flow_id TEXT NOT NULL,
        method TEXT,
        host TEXT,
        path TEXT,
        status INTEGER,
        close_code INTEGER,
        content_type TEXT,
        headers_json TEXT,
        data_text TEXT,
        data_blob_id TEXT,
        data_sha256 TEXT,
        error_text TEXT,
        meta_json TEXT
      ) STRICT;
      CREATE INDEX capture_events_session_ts_idx ON capture_events(session_id, ts);
      CREATE INDEX capture_events_flow_idx ON capture_events(flow_id, ts);
      INSERT INTO workspace_attestations VALUES ('workspace-1', 100, 200);
      INSERT INTO capture_sessions VALUES (
        'session-1', 1, NULL, 'qa', 'openclaw', 'candidate', NULL
      );
    `);
    db.prepare(
      `INSERT INTO capture_blobs (
         blob_id, content_type, encoding, size_bytes, sha256, data, created_at
       ) VALUES (?, ?, ?, ?, ?, ?, ?)`,
    ).run(
      "blob-1",
      "application/json",
      "gzip",
      11,
      "fixture-sha",
      gzipSync(Buffer.from('{"ok":true}')),
      2,
    );
    db.prepare(
      `INSERT INTO capture_events (
         id, session_id, ts, source_scope, source_process, protocol, direction, kind, flow_id,
         method, host, path, status, content_type, data_blob_id
       ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
    ).run(
      1,
      "session-1",
      3,
      "openclaw",
      "candidate",
      "https",
      "outbound",
      "request",
      "flow-1",
      "POST",
      "slack.com",
      "/api/chat.postMessage",
      200,
      "application/json",
      "blob-1",
    );
  } finally {
    db.close();
  }
  return databasePath;
}

function inspectDatabase(databasePath: string) {
  const db = new DatabaseSync(databasePath, { readOnly: true });
  try {
    return {
      fingerprint: JSON.stringify(
        db
          .prepare(
            `SELECT type, name, tbl_name AS tableName, sql
             FROM sqlite_schema
             WHERE name NOT LIKE 'sqlite_%'
             ORDER BY type, name`,
          )
          .all(),
      ),
      userVersion: db.prepare("PRAGMA user_version").get(),
      workspaceAttestations: db
        .prepare("SELECT * FROM workspace_attestations ORDER BY workspace_key")
        .all(),
    };
  } finally {
    db.close();
  }
}

describe("createDebugProxyCaptureReader", () => {
  it.each([3, 4, 5, 6])(
    "reads schema %s without changing its version, schema, or workspace attestations",
    (schemaVersion) => {
      const env = makeStateEnv(`openclaw-proxy-reader-v${schemaVersion}-`);
      const databasePath = createCaptureDatabase(env, schemaVersion);
      const before = inspectDatabase(databasePath);
      const reader = createDebugProxyCaptureReader({ env });

      expect(reader.getSessionEvents("session-1")).toEqual([
        expect.objectContaining({
          id: 1,
          sessionId: "session-1",
          flowId: "flow-1",
          host: "slack.com",
        }),
      ]);
      expect(reader.readBlob("blob-1")).toBe('{"ok":true}');
      expect(inspectDatabase(databasePath)).toEqual(before);
    },
  );

  it("returns empty results for a missing database without creating state artifacts", () => {
    const env = makeStateEnv("openclaw-proxy-reader-missing-");
    const reader = createDebugProxyCaptureReader({ env });
    const databasePath = resolveOpenClawStateSqlitePath(env);

    expect(reader.getSessionEvents("missing")).toEqual([]);
    expect(reader.readBlob("missing")).toBeNull();
    expect(fs.existsSync(databasePath)).toBe(false);
    expect(fs.existsSync(path.dirname(databasePath))).toBe(false);
  });

  it("observes a database created after the reader", () => {
    const env = makeStateEnv("openclaw-proxy-reader-late-db-");
    const reader = createDebugProxyCaptureReader({ env });
    expect(reader.getSessionEvents("session-1")).toEqual([]);

    createCaptureDatabase(env, 3);

    expect(reader.getSessionEvents("session-1")).toHaveLength(1);
    expect(reader.readBlob("blob-1")).toBe('{"ok":true}');
  });

  it("observes later committed WAL writes on subsequent calls", () => {
    const env = makeStateEnv("openclaw-proxy-reader-wal-");
    const databasePath = createCaptureDatabase(env, 3);
    const writer = new DatabaseSync(databasePath);
    const reader = createDebugProxyCaptureReader({ env });
    try {
      writer.exec("PRAGMA journal_mode = WAL; PRAGMA wal_autocheckpoint = 0;");
      expect(reader.getSessionEvents("session-1")).toHaveLength(1);

      writer
        .prepare(
          `INSERT INTO capture_events (
             id, session_id, ts, source_scope, source_process, protocol, direction, kind, flow_id
           ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
        )
        .run(2, "session-1", 4, "openclaw", "candidate", "https", "inbound", "response", "flow-1");

      expect(reader.getSessionEvents("session-1").map((event) => event.id)).toEqual([2, 1]);
    } finally {
      writer.close();
    }
  });

  it("projects exact camelCase keys with timestamp, id, and limit ordering", () => {
    const env = makeStateEnv("openclaw-proxy-reader-ordering-");
    const databasePath = createCaptureDatabase(env, 3);
    const writer = new DatabaseSync(databasePath);
    try {
      const insert = writer.prepare(
        `INSERT INTO capture_events (
           id, session_id, ts, source_scope, source_process, protocol, direction, kind, flow_id,
           method, host, path, status, close_code, content_type, headers_json, data_text,
           data_blob_id, data_sha256, error_text, meta_json
         ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
      );
      insert.run(
        2,
        "session-1",
        5,
        "openclaw",
        "candidate",
        "https",
        "outbound",
        "request",
        "flow-2",
        "POST",
        "slack.com",
        "/api/chat.postMessage",
        200,
        1000,
        "application/json",
        '{"authorization":"redacted"}',
        '{"text":"older"}',
        "blob-1",
        "sha-2",
        "error-2",
        '{"attempt":2}',
      );
      insert.run(
        3,
        "session-1",
        5,
        "openclaw",
        "candidate",
        "https",
        "inbound",
        "response",
        "flow-3",
        "GET",
        "slack.com",
        "/api/conversations.history",
        201,
        1001,
        "application/json",
        '{"accept":"application/json"}',
        '{"text":"newer"}',
        "blob-1",
        "sha-3",
        "error-3",
        '{"attempt":3}',
      );
      insert.run(
        4,
        "session-1",
        6,
        "openclaw",
        "candidate",
        "https",
        "inbound",
        "response",
        "flow-4",
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
      );
    } finally {
      writer.close();
    }

    const events = createDebugProxyCaptureReader({ env }).getSessionEvents("session-1", 2);

    expect(events.map((event) => event.id)).toEqual([4, 3]);
    expect(events[1]).toEqual({
      id: 3,
      sessionId: "session-1",
      ts: 5,
      sourceScope: "openclaw",
      sourceProcess: "candidate",
      protocol: "https",
      direction: "inbound",
      kind: "response",
      flowId: "flow-3",
      method: "GET",
      host: "slack.com",
      path: "/api/conversations.history",
      status: 201,
      closeCode: 1001,
      contentType: "application/json",
      headersJson: '{"accept":"application/json"}',
      dataText: '{"text":"newer"}',
      dataBlobId: "blob-1",
      dataSha256: "sha-3",
      errorText: "error-3",
      metaJson: '{"attempt":3}',
    });
  });

  it("propagates newer schema errors", () => {
    const env = makeStateEnv("openclaw-proxy-reader-newer-");
    const databasePath = resolveOpenClawStateSqlitePath(env);
    fs.mkdirSync(path.dirname(databasePath), { recursive: true });
    const db = new DatabaseSync(databasePath);
    db.exec(`PRAGMA user_version = ${OPENCLAW_STATE_SCHEMA_VERSION + 1};`);
    db.close();

    expect(() => createDebugProxyCaptureReader({ env }).getSessionEvents("session-1")).toThrow(
      `uses newer schema version ${OPENCLAW_STATE_SCHEMA_VERSION + 1}`,
    );
  });

  it("propagates database corruption", () => {
    const env = makeStateEnv("openclaw-proxy-reader-corrupt-");
    const databasePath = resolveOpenClawStateSqlitePath(env);
    fs.mkdirSync(path.dirname(databasePath), { recursive: true });
    fs.writeFileSync(databasePath, "not a sqlite database");

    expect(() => createDebugProxyCaptureReader({ env }).readBlob("blob-1")).toThrow();
  });

  it("propagates capture query failures", () => {
    const env = makeStateEnv("openclaw-proxy-reader-sql-");
    const databasePath = resolveOpenClawStateSqlitePath(env);
    fs.mkdirSync(path.dirname(databasePath), { recursive: true });
    const db = new DatabaseSync(databasePath);
    db.exec("PRAGMA user_version = 3;");
    db.close();

    expect(() => createDebugProxyCaptureReader({ env }).getSessionEvents("session-1")).toThrow(
      /no such table: capture_events/u,
    );
  });
});