Skip to content

Commit ac4bd07

Browse files
steipeteKimiyu-186
authored andcommitted
perf(sessions): lazily scan transcript matches (#127162)
Amp-Thread-ID: https://ampcode.com/threads/T-01a021f5-984a-7628-a30c-491c166ff247 Co-authored-by: Amp <[email protected]> (cherry picked from commit 096a970)
1 parent b2de68f commit ac4bd07

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,14 @@ export function findSqliteTranscriptEventInDatabase(
357357
match: (event: TranscriptEvent) => boolean,
358358
): { event: TranscriptEvent } | undefined {
359359
const db = getSessionKysely(database.db);
360-
const rows = executeSqliteQuerySync(
360+
const rows = iterateSqliteQuerySync(
361361
database.db,
362362
db
363363
.selectFrom("transcript_events")
364364
.select(["event_json"])
365365
.where("session_id", "=", sessionId)
366366
.orderBy("seq", "desc"),
367-
).rows;
367+
);
368368
for (const row of rows) {
369369
try {
370370
const event = JSON.parse(row.event_json) as TranscriptEvent;

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,17 +501,48 @@ describe("session accessor seam", () => {
501501
[header, older, newer],
502502
);
503503

504+
const databasePath = expectDefined(
505+
resolveSqliteTargetFromSessionStorePath(storePath, { agentId: "main" }).path,
506+
"transcript find database path",
507+
);
508+
const database = openOpenClawAgentDatabase({ agentId: "main", path: databasePath });
509+
const originalPrepare = database.db.prepare.bind(database.db);
510+
let transcriptRowsRead = 0;
511+
// Count SQLite rows rather than matcher calls: eager materialization happens before matching.
512+
const prepareSpy = vi.spyOn(database.db, "prepare").mockImplementation((sql) => {
513+
const statement = originalPrepare(sql);
514+
return new Proxy(statement, {
515+
get(target, property) {
516+
if (property === "iterate") {
517+
return (...params: Parameters<typeof target.iterate>) => {
518+
const iterator = target.iterate(...params);
519+
return (function* () {
520+
for (const row of iterator) {
521+
if ("event_json" in row) {
522+
transcriptRowsRead += 1;
523+
}
524+
yield row;
525+
}
526+
})() as ReturnType<typeof target.iterate>;
527+
};
528+
}
529+
const value = Reflect.get(target, property, target) as unknown;
530+
return typeof value === "function" ? value.bind(target) : value;
531+
},
532+
});
533+
});
504534
const seen: unknown[] = [];
505535
const found = await findTranscriptEvent(
506536
{ sessionId: "session-find", sessionKey: "agent:main:main", storePath },
507537
(event) => {
508538
seen.push(event);
509539
return (event as { type?: string }).type === "message";
510540
},
511-
);
541+
).finally(() => prepareSpy.mockRestore());
512542
// Newest-first with early exit: the older message is never visited.
513543
expect(found).toEqual({ event: newer });
514544
expect(seen).toEqual([newer]);
545+
expect(transcriptRowsRead).toBe(1);
515546

516547
await replaceSqliteTranscriptEvents(
517548
{ agentId: "main", sessionId: "session-falsy", sessionKey: "agent:main:falsy", storePath },

0 commit comments

Comments
 (0)