Skip to content

Commit 25776ee

Browse files
committed
fix(sessions): preserve canonical imported branch tips
1 parent fd00f7d commit 25776ee

2 files changed

Lines changed: 29 additions & 9 deletions

File tree

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
withOpenClawTestState,
99
type OpenClawTestState,
1010
} from "../../test-utils/openclaw-test-state.js";
11+
import { listSessionBranches } from "./session-accessor.js";
1112
import { loadExactSessionEntry } from "./session-accessor.sqlite-entry.js";
1213
import {
1314
importSqliteSessionRows,
@@ -197,10 +198,16 @@ it("hands off exact SQLite bytes, duplicate IDs, timestamps and owner without ap
197198
await withOpenClawTestState({ label: "import-exact" }, async (state) => {
198199
const params = target(state, "exact");
199200
const owner = { actor: { type: "human" as const, id: "owner" }, assignedAt: 40 };
201+
const firstMessage = { ...message, timestamp: "2026-08-30T00:00:01.000Z" };
202+
const canonicalMessage = {
203+
...message,
204+
timestamp: "2026-08-30T00:00:02.000Z",
205+
message: { role: "user", content: "canonical duplicate" },
206+
};
200207
const rows = [
201208
{ createdAt: 41, eventJson: '{ "type": "session", "id": "exact", "version": 3 }' },
202-
{ createdAt: 43, eventJson: JSON.stringify(message, null, 2) },
203-
{ createdAt: 45, eventJson: JSON.stringify(message) },
209+
{ createdAt: 43, eventJson: JSON.stringify(firstMessage, null, 2) },
210+
{ createdAt: 45, eventJson: JSON.stringify(canonicalMessage) },
204211
];
205212
await importSqliteSessionRows({
206213
...params,
@@ -231,6 +238,16 @@ it("hands off exact SQLite bytes, duplicate IDs, timestamps and owner without ap
231238
}),
232239
).toMatchObject({ skippedExisting: true, transcriptEvents: 0 });
233240
expect(loadTranscriptEventsSync({ ...params, sessionId: "exact" })).toHaveLength(3);
241+
await expect(listSessionBranches(params)).resolves.toEqual({
242+
status: "ok",
243+
branches: Array.from({ length: 2 }, () => ({
244+
leafEntryId: "one",
245+
headline: "canonical duplicate",
246+
messageCount: 1,
247+
updatedAt: canonicalMessage.timestamp,
248+
active: true,
249+
})),
250+
});
234251
});
235252
});
236253

‎src/config/sessions/session-accessor.sqlite-message-cut.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,16 @@ function validateBranchTip(
417417
function summarizeSessionBranches(events: readonly TranscriptEvent[]): SessionBranchSummary[] {
418418
const tree = scanSessionTranscriptTree(events);
419419
const pathSummaries = new Map<string, SessionBranchPathSummary>();
420-
return sessionBranchTipNodes(tree)
421-
.toSorted(
422-
(left, right) =>
423-
Number(right.id === tree.leafId) - Number(left.id === tree.leafId) ||
424-
right.index - left.index,
425-
)
426-
.map((node) => summarizeSessionBranch(tree, node, pathSummaries));
420+
return (
421+
sessionBranchTipNodes(tree)
422+
.toSorted(
423+
(left, right) =>
424+
Number(right.id === tree.leafId) - Number(left.id === tree.leafId) ||
425+
right.index - left.index,
426+
)
427+
// SAFETY: scanSessionTranscriptTree inserts every returned node into byId.
428+
.map((node) => summarizeSessionBranch(tree, tree.byId.get(node.id)!, pathSummaries))
429+
);
427430
}
428431

429432
function sessionBranchTipNodes(tree: SessionTranscriptTree<TranscriptEvent>) {

0 commit comments

Comments
 (0)