Skip to content

Commit 59abf3d

Browse files
authored
feat: persist stateful transcript display semantics (#126943)
Persist stateful transcript display semantics through one shared reducer for incremental and rebuilt projections. Refs #126914.
1 parent 053b89d commit 59abf3d

30 files changed

Lines changed: 4501 additions & 1247 deletions

‎config/assertion-safety-baseline.txt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,8 +2882,7 @@ src/gateway/chat-attachments.ts 4
28822882
src/gateway/chat-display-projection.canvas.ts 14
28832883
src/gateway/chat-display-projection.core.ts 6
28842884
src/gateway/chat-display-projection.helpers.ts 10
2885-
src/gateway/chat-display-projection.history.ts 6
2886-
src/gateway/chat-display-projection.message-tool.ts 1
2885+
src/gateway/chat-display-projection.history.ts 2
28872886
src/gateway/chat-display-projection.sanitize.ts 16
28882887
src/gateway/chat-sanitize.ts 3
28892888
src/gateway/chat-tool-titles.ts 2
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
bcfdbb764d6e381d23ce1be8a2485310f065c660bc272dff6f244972dc400b9d sqlite-session-transcript-schema-baseline.sql
1+
1ba742b789c40e2b6006b6c24abd371319ecdaf8a8a939d0ff61d60d2f4eec7c sqlite-session-transcript-schema-baseline.sql

‎scripts/lib/sqlite-session-schema-baseline.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ const TARGET_TABLES = new Set([
4040
"session_transcript_active_events",
4141
"session_transcript_display_state",
4242
"session_transcript_display_rows",
43+
"session_transcript_display_row_sources",
44+
"session_transcript_display_canvas",
45+
"session_transcript_display_carry",
4346
"session_transcript_archives",
4447
]);
4548

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { DatabaseSync } from "node:sqlite";
2+
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import { OPENCLAW_AGENT_SCHEMA_SQL } from "../../state/openclaw-agent-schema.js";
4+
import {
5+
appendSessionTranscriptDisplayChunkInTransaction,
6+
claimSessionTranscriptDisplayInTransaction,
7+
} from "./session-transcript-display-rebuild-store.js";
8+
9+
vi.mock("../../infra/tmp-openclaw-dir.js", () => ({
10+
DEFAULT_POSIX_TMP_ROOT: "/tmp/openclaw",
11+
resolvePreferredOpenClawTmpDir: () => "/tmp/openclaw",
12+
}));
13+
14+
const ROW_COUNT = 300;
15+
const COMPANION_COUNT = 16;
16+
17+
describe("transcript display rebuild persistence", () => {
18+
let db: DatabaseSync | undefined;
19+
20+
afterEach(() => {
21+
db?.close();
22+
db = undefined;
23+
});
24+
25+
it("chunks oversized semantic-source and canvas inserts below SQLite's variable limit", () => {
26+
db = new DatabaseSync(":memory:");
27+
db.exec(OPENCLAW_AGENT_SCHEMA_SQL);
28+
const cases = [
29+
{
30+
sessionId: "display-rebuild-semantic-sources",
31+
table: "session_transcript_display_row_sources",
32+
companions: (sourceEventSeq: number) => ({
33+
canvases: [],
34+
semanticSources: Array.from({ length: COMPANION_COUNT }, (_, position) => ({
35+
position,
36+
relation: "message_tool_mirror" as const,
37+
sourceEventSeq,
38+
sourceOccurrence: position,
39+
})),
40+
}),
41+
},
42+
{
43+
sessionId: "display-rebuild-canvases",
44+
table: "session_transcript_display_canvas",
45+
companions: (sourceEventSeq: number) => ({
46+
canvases: Array.from({ length: COMPANION_COUNT }, (_, position) => ({
47+
position,
48+
sourceEventSeq,
49+
url: `/__openclaw__/canvas/documents/cv_${String(sourceEventSeq)}/${String(position)}.html`,
50+
})),
51+
semanticSources: [],
52+
}),
53+
},
54+
] as const;
55+
56+
for (const entry of cases) {
57+
const sessionKey = `agent:main:${entry.sessionId}`;
58+
db.prepare(
59+
"INSERT INTO session_nodes (session_key, current_session_id, entry_json, updated_at) VALUES (?, ?, '{}', 0)",
60+
).run(sessionKey, entry.sessionId);
61+
db.prepare(
62+
"INSERT INTO session_windows (session_id, session_key, created_at, updated_at) VALUES (?, ?, 0, 0)",
63+
).run(entry.sessionId, sessionKey);
64+
const insertEvent = db.prepare(
65+
"INSERT INTO transcript_events (session_id, seq, event_json, created_at) VALUES (?, ?, ?, ?)",
66+
);
67+
for (let sourceEventSeq = 0; sourceEventSeq < ROW_COUNT; sourceEventSeq += 1) {
68+
insertEvent.run(
69+
entry.sessionId,
70+
sourceEventSeq,
71+
JSON.stringify({
72+
id: `assistant-${String(sourceEventSeq)}`,
73+
message: { content: "reply", role: "assistant" },
74+
type: "message",
75+
}),
76+
sourceEventSeq,
77+
);
78+
}
79+
const generation = `generation-${entry.sessionId}`;
80+
const claimId = Date.now();
81+
db.exec("BEGIN IMMEDIATE");
82+
expect(
83+
claimSessionTranscriptDisplayInTransaction(db, {
84+
claimId,
85+
generation,
86+
sessionId: entry.sessionId,
87+
}),
88+
).toBe(true);
89+
expect(
90+
appendSessionTranscriptDisplayChunkInTransaction(db, {
91+
claimId,
92+
generation,
93+
rows: Array.from({ length: ROW_COUNT }, (_, sourceEventSeq) => ({
94+
...entry.companions(sourceEventSeq),
95+
displayOrdinal: sourceEventSeq,
96+
kind: "assistant" as const,
97+
revision: 1,
98+
rowId: `row-${String(sourceEventSeq)}`,
99+
rowVersion: 1,
100+
sourceEventSeq,
101+
})),
102+
sessionId: entry.sessionId,
103+
}),
104+
).toBe(true);
105+
db.exec("COMMIT");
106+
107+
const count = db
108+
.prepare(`SELECT COUNT(*) AS count FROM ${entry.table} WHERE session_id = ?`)
109+
.get(entry.sessionId) as { count: number };
110+
expect(count.count).toBe(ROW_COUNT * COMPANION_COUNT);
111+
}
112+
});
113+
});

0 commit comments

Comments
 (0)