Skip to content

Commit 2b6f80f

Browse files
committed
fix(gateway): keep history readers out of the plugin SDK barrel and fix CI gates
Direct imports for the sqlite history readers (the session-accessor barrel is SDK-reachable via session-transcript-lock-runtime); reset marker added to the kept-tail chat.history expectation; lint naming fixes; marker tests split into session-transcript-readers.markers.test.ts.
1 parent 1e00d95 commit 2b6f80f

7 files changed

Lines changed: 196 additions & 153 deletions

‎src/config/sessions/session-accessor.ts‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,6 @@ export {
231231
type ClosedTranscriptTurnReadResult,
232232
} from "./session-accessor.transcript-range.js";
233233
export { readActiveTranscriptEntryAnchor } from "./session-accessor.sqlite-transcript-anchor.js";
234-
export {
235-
readRecentSessionTranscriptHistoryEvents,
236-
readSessionTranscriptHistoryAnchorPage,
237-
readSessionTranscriptHistoryEventById,
238-
readSessionTranscriptHistoryEventCount,
239-
readSessionTranscriptHistoryEventPage,
240-
readSessionTranscriptHistoryEvents,
241-
} from "./session-accessor.sqlite-history-events.js";
242234
export {
243235
isSessionTranscriptProjectionUnavailableError,
244236
readRecentSessionTranscriptActiveEvents,

‎src/gateway/server.chat.gateway-server-chat.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ describe("gateway server chat", () => {
830830
});
831831
});
832832

833-
test("chat.history applies the reset boundary kept-tail cut", async () => {
833+
test("chat.history applies the reset kept-tail cut and preserves its marker", async () => {
834834
await withMainSessionStore(async () => {
835835
const storePath = testState.sessionStorePath;
836836
if (!storePath) {
@@ -883,6 +883,7 @@ describe("gateway server chat", () => {
883883
expect(collectHistoryTextValues(history.payload?.messages ?? [])).toEqual([
884884
"kept question",
885885
"kept answer",
886+
"Reset",
886887
"new turn",
887888
]);
888889
});

‎src/gateway/session-transcript-anchor-reader.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import {
2-
readSessionTranscriptHistoryAnchorPage,
3-
type SessionTranscriptReadScope,
4-
} from "../config/sessions/session-accessor.js";
1+
import type { SessionTranscriptReadScope } from "../config/sessions/session-accessor.js";
2+
import { readSessionTranscriptHistoryAnchorPage } from "../config/sessions/session-accessor.sqlite-history-events.js";
53
import { projectTranscriptEntryMessage } from "./session-transcript-message.js";
64
import {
75
resolveTranscriptReadTarget,
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import path from "node:path";
2+
import { afterEach, beforeEach, describe, expect, test } from "vitest";
3+
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
4+
import { replaceTranscriptEvents } from "../config/sessions/session-accessor.js";
5+
import { closeOpenClawAgentDatabasesForTest } from "../state/openclaw-agent-db.js";
6+
import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js";
7+
import { captureEnv, setTestEnvValue } from "../test-utils/env.js";
8+
import { readSessionMessagesAroundIdWithStatsAsync } from "./session-transcript-anchor-reader.js";
9+
import {
10+
readRecentSessionMessagesWithStatsAsync,
11+
readSessionMessageByIdAsync,
12+
readSessionMessageCountAsync,
13+
readSessionMessagesAsync,
14+
readSessionMessagesPageWithStatsAsync,
15+
type SessionTranscriptReadScope,
16+
} from "./session-transcript-readers.js";
17+
18+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
19+
20+
describe("session transcript reader marker projection", () => {
21+
let tempDir: string;
22+
let storePath: string;
23+
let envSnapshot: ReturnType<typeof captureEnv>;
24+
25+
beforeEach(() => {
26+
envSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]);
27+
tempDir = tempDirs.make("openclaw-transcript-markers-");
28+
storePath = path.join(tempDir, "sessions.json");
29+
setTestEnvValue("OPENCLAW_STATE_DIR", tempDir);
30+
});
31+
32+
afterEach(() => {
33+
closeOpenClawAgentDatabasesForTest();
34+
closeOpenClawStateDatabaseForTest();
35+
envSnapshot.restore();
36+
});
37+
38+
async function writeTranscript(
39+
sessionId: string,
40+
events: unknown[],
41+
): Promise<SessionTranscriptReadScope> {
42+
const scope = {
43+
agentId: "main",
44+
sessionId,
45+
sessionKey: `agent:main:${sessionId}`,
46+
storePath,
47+
};
48+
await replaceTranscriptEvents(scope, events);
49+
return scope;
50+
}
51+
52+
test.each([
53+
{
54+
name: "compaction",
55+
sessionId: "reader-compaction-boundary",
56+
markerId: "compaction-boundary",
57+
markerKind: "compaction",
58+
markerText: "Compaction",
59+
events: (sessionId: string) => [
60+
{ type: "session", version: 3, id: sessionId },
61+
{
62+
type: "message",
63+
id: "before-compaction",
64+
parentId: null,
65+
message: { role: "user", content: "before compaction" },
66+
},
67+
{
68+
type: "compaction",
69+
id: "compaction-boundary",
70+
parentId: "before-compaction",
71+
timestamp: "2026-08-11T18:00:00.000Z",
72+
summary: "summary",
73+
firstKeptEntryId: "before-compaction",
74+
tokensBefore: 100,
75+
},
76+
{
77+
type: "message",
78+
id: "after-compaction",
79+
parentId: "compaction-boundary",
80+
message: { role: "assistant", content: "after compaction" },
81+
},
82+
],
83+
expected: ["before compaction", "compaction", "after compaction"],
84+
},
85+
{
86+
name: "reset",
87+
sessionId: "reader-reset-boundary",
88+
markerId: "reset-boundary",
89+
markerKind: "reset",
90+
markerText: "Reset",
91+
events: (sessionId: string) => [
92+
{ type: "session", version: 3, id: sessionId },
93+
{
94+
type: "message",
95+
id: "old",
96+
parentId: null,
97+
message: { role: "user", content: "hidden old turn" },
98+
},
99+
{
100+
type: "message",
101+
id: "kept-user",
102+
parentId: "old",
103+
message: { role: "user", content: "kept question" },
104+
},
105+
{
106+
type: "message",
107+
id: "kept-assistant",
108+
parentId: "kept-user",
109+
message: { role: "assistant", content: "kept answer" },
110+
},
111+
{
112+
type: "reset",
113+
id: "reset-boundary",
114+
parentId: "kept-assistant",
115+
timestamp: "2026-08-11T18:00:00.000Z",
116+
reason: "reset",
117+
firstKeptEntryId: "kept-user",
118+
},
119+
{
120+
type: "message",
121+
id: "post-reset",
122+
parentId: "reset-boundary",
123+
message: { role: "assistant", content: "new answer" },
124+
},
125+
],
126+
expected: ["kept question", "kept answer", "reset", "new answer"],
127+
},
128+
])("projects $name boundaries through every SQLite history read", async (fixture) => {
129+
const scope = await writeTranscript(fixture.sessionId, fixture.events(fixture.sessionId));
130+
const summarize = (messages: unknown[]) =>
131+
messages.map((message) => {
132+
const record = message as { content?: unknown; __openclaw?: { kind?: string } };
133+
return record["__openclaw"]?.kind ?? record.content;
134+
});
135+
136+
const full = await readSessionMessagesAsync(scope, {
137+
mode: "full",
138+
reason: `${fixture.name} boundary projection test`,
139+
});
140+
const recent = await readRecentSessionMessagesWithStatsAsync(scope, {
141+
maxBytes: 16_384,
142+
maxLines: 10,
143+
maxMessages: 10,
144+
});
145+
const markerIndex = fixture.expected.indexOf(fixture.markerKind);
146+
const page = await readSessionMessagesPageWithStatsAsync(scope, {
147+
maxMessages: 1,
148+
offset: fixture.expected.length - markerIndex - 1,
149+
});
150+
const byId = await readSessionMessageByIdAsync(scope, fixture.markerId);
151+
const anchored = await readSessionMessagesAroundIdWithStatsAsync(scope, {
152+
messageId: fixture.markerId,
153+
maxMessages: 10,
154+
});
155+
156+
expect(summarize(full)).toEqual(fixture.expected);
157+
expect(summarize(recent.messages)).toEqual(fixture.expected);
158+
expect(recent.totalMessages).toBe(fixture.expected.length);
159+
expect(summarize(page.messages)).toEqual([fixture.markerKind]);
160+
expect(page.totalMessages).toBe(fixture.expected.length);
161+
expect(await readSessionMessageCountAsync(scope)).toBe(fixture.expected.length);
162+
expect(byId).toMatchObject({
163+
found: true,
164+
message: {
165+
role: "system",
166+
content: [{ type: "text", text: fixture.markerText }],
167+
timestamp: Date.parse("2026-08-11T18:00:00.000Z"),
168+
__openclaw: {
169+
kind: fixture.markerKind,
170+
id: fixture.markerId,
171+
seq: markerIndex + 1,
172+
},
173+
},
174+
seq: markerIndex + 1,
175+
});
176+
expect(anchored.found).toBe(true);
177+
expect(summarize(anchored.messages)).toEqual(fixture.expected);
178+
expect(anchored.totalMessages).toBe(fixture.expected.length);
179+
});
180+
});

‎src/gateway/session-transcript-readers.test.ts‎

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
readSessionMessageCountAsync,
2323
readSessionMessagesAsync,
2424
readSessionMessagesPageWithStatsAsync,
25-
readRecentSessionMessagesWithStatsAsync,
2625
readLatestSessionUsageFromTranscriptAsync,
2726
type SessionTranscriptReadScope,
2827
} from "./session-transcript-readers.js";
@@ -203,135 +202,6 @@ describe("session transcript reader facade", () => {
203202
});
204203
});
205204

206-
test.each([
207-
{
208-
name: "compaction",
209-
sessionId: "reader-compaction-boundary",
210-
markerId: "compaction-boundary",
211-
markerKind: "compaction",
212-
markerText: "Compaction",
213-
events: (sessionId: string) => [
214-
{ type: "session", version: 3, id: sessionId },
215-
{
216-
type: "message",
217-
id: "before-compaction",
218-
parentId: null,
219-
message: { role: "user", content: "before compaction" },
220-
},
221-
{
222-
type: "compaction",
223-
id: "compaction-boundary",
224-
parentId: "before-compaction",
225-
timestamp: "2026-08-11T18:00:00.000Z",
226-
summary: "summary",
227-
firstKeptEntryId: "before-compaction",
228-
tokensBefore: 100,
229-
},
230-
{
231-
type: "message",
232-
id: "after-compaction",
233-
parentId: "compaction-boundary",
234-
message: { role: "assistant", content: "after compaction" },
235-
},
236-
],
237-
expected: ["before compaction", "compaction", "after compaction"],
238-
},
239-
{
240-
name: "reset",
241-
sessionId: "reader-reset-boundary",
242-
markerId: "reset-boundary",
243-
markerKind: "reset",
244-
markerText: "Reset",
245-
events: (sessionId: string) => [
246-
{ type: "session", version: 3, id: sessionId },
247-
{
248-
type: "message",
249-
id: "old",
250-
parentId: null,
251-
message: { role: "user", content: "hidden old turn" },
252-
},
253-
{
254-
type: "message",
255-
id: "kept-user",
256-
parentId: "old",
257-
message: { role: "user", content: "kept question" },
258-
},
259-
{
260-
type: "message",
261-
id: "kept-assistant",
262-
parentId: "kept-user",
263-
message: { role: "assistant", content: "kept answer" },
264-
},
265-
{
266-
type: "reset",
267-
id: "reset-boundary",
268-
parentId: "kept-assistant",
269-
timestamp: "2026-08-11T18:00:00.000Z",
270-
reason: "reset",
271-
firstKeptEntryId: "kept-user",
272-
},
273-
{
274-
type: "message",
275-
id: "post-reset",
276-
parentId: "reset-boundary",
277-
message: { role: "assistant", content: "new answer" },
278-
},
279-
],
280-
expected: ["kept question", "kept answer", "reset", "new answer"],
281-
},
282-
])("projects $name boundaries through every SQLite history read", async (fixture) => {
283-
const scope = await writeTranscript(fixture.sessionId, fixture.events(fixture.sessionId));
284-
const summarize = (messages: unknown[]) =>
285-
messages.map((message) => {
286-
const record = message as { content?: unknown; __openclaw?: { kind?: string } };
287-
return record.__openclaw?.kind ?? record.content;
288-
});
289-
290-
const full = await readSessionMessagesAsync(scope, {
291-
mode: "full",
292-
reason: `${fixture.name} boundary projection test`,
293-
});
294-
const recent = await readRecentSessionMessagesWithStatsAsync(scope, {
295-
maxBytes: 16_384,
296-
maxLines: 10,
297-
maxMessages: 10,
298-
});
299-
const markerIndex = fixture.expected.indexOf(fixture.markerKind);
300-
const page = await readSessionMessagesPageWithStatsAsync(scope, {
301-
maxMessages: 1,
302-
offset: fixture.expected.length - markerIndex - 1,
303-
});
304-
const byId = await readSessionMessageByIdAsync(scope, fixture.markerId);
305-
const anchored = await readSessionMessagesAroundIdWithStatsAsync(scope, {
306-
messageId: fixture.markerId,
307-
maxMessages: 10,
308-
});
309-
310-
expect(summarize(full)).toEqual(fixture.expected);
311-
expect(summarize(recent.messages)).toEqual(fixture.expected);
312-
expect(recent.totalMessages).toBe(fixture.expected.length);
313-
expect(summarize(page.messages)).toEqual([fixture.markerKind]);
314-
expect(page.totalMessages).toBe(fixture.expected.length);
315-
expect(await readSessionMessageCountAsync(scope)).toBe(fixture.expected.length);
316-
expect(byId).toMatchObject({
317-
found: true,
318-
message: {
319-
role: "system",
320-
content: [{ type: "text", text: fixture.markerText }],
321-
timestamp: Date.parse("2026-08-11T18:00:00.000Z"),
322-
__openclaw: {
323-
kind: fixture.markerKind,
324-
id: fixture.markerId,
325-
seq: markerIndex + 1,
326-
},
327-
},
328-
seq: markerIndex + 1,
329-
});
330-
expect(anchored.found).toBe(true);
331-
expect(summarize(anchored.messages)).toEqual(fixture.expected);
332-
expect(anchored.totalMessages).toBe(fixture.expected.length);
333-
});
334-
335205
test("preserves Date.parse semantics for numeric-looking record timestamps", async () => {
336206
const scope = await writeTranscript("reader-numeric-looking-timestamps", [
337207
{ type: "session", version: 3, id: "reader-numeric-looking-timestamps" },

‎src/gateway/session-transcript-readers.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import path from "node:path";
22
import { parseDateFirstTimestampMs } from "@openclaw/normalization-core/number-coercion";
33
import {
4-
readRecentSessionTranscriptHistoryEvents,
54
isSessionTranscriptProjectionUnavailableError,
65
readRecentSessionTranscriptMessageEvents,
7-
readSessionTranscriptHistoryEventById,
8-
readSessionTranscriptHistoryEventCount,
9-
readSessionTranscriptHistoryEventPage,
10-
readSessionTranscriptHistoryEvents,
116
readSessionTranscriptMessageEvents,
127
resolveConcreteSessionStorePath,
138
resolveSessionTranscriptReadTarget,
@@ -16,6 +11,13 @@ import {
1611
type SessionTranscriptReadScope,
1712
type TranscriptEvent,
1813
} from "../config/sessions/session-accessor.js";
14+
import {
15+
readRecentSessionTranscriptHistoryEvents,
16+
readSessionTranscriptHistoryEventById,
17+
readSessionTranscriptHistoryEventCount,
18+
readSessionTranscriptHistoryEventPage,
19+
readSessionTranscriptHistoryEvents,
20+
} from "../config/sessions/session-accessor.sqlite-history-events.js";
1921
import { resolveAgentIdFromSessionKey } from "../routing/session-key.js";
2022
import { aggregateSqliteUsageSnapshots } from "./session-transcript-derived-readers.js";
2123
import {

0 commit comments

Comments
 (0)