Skip to content

Commit aca905c

Browse files
authored
perf(sessions): find matching checkpoints without sorting (#96964)
Co-authored-by: ly-wang19 <[email protected]>
1 parent ab966c2 commit aca905c

2 files changed

Lines changed: 96 additions & 3 deletions

File tree

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,92 @@ describe("session accessor file-backed seam", () => {
10361036
});
10371037
});
10381038

1039+
it("branches from the newest matching compaction checkpoint without sorting all checkpoints", async () => {
1040+
const sourceSessionId = "11111111-1111-4111-8111-111111111111";
1041+
const branchSessionId = "22222222-2222-4222-8222-222222222222";
1042+
const branchPath = path.join(tempDir, "branch-newest.jsonl");
1043+
fs.writeFileSync(branchPath, `{"type":"session","id":"${branchSessionId}"}\n`, "utf8");
1044+
const oldMatchingCheckpoint = {
1045+
checkpointId: "checkpoint-1",
1046+
sessionKey: "agent:main:main",
1047+
sessionId: sourceSessionId,
1048+
createdAt: 10,
1049+
reason: "manual",
1050+
preCompaction: {
1051+
sessionId: sourceSessionId,
1052+
leafId: "old-leaf",
1053+
},
1054+
postCompaction: { sessionId: "33333333-3333-4333-8333-333333333333" },
1055+
} satisfies NonNullable<SessionEntry["compactionCheckpoints"]>[number];
1056+
const newestMatchingCheckpoint = {
1057+
...oldMatchingCheckpoint,
1058+
createdAt: 20,
1059+
preCompaction: {
1060+
sessionId: sourceSessionId,
1061+
leafId: "new-leaf",
1062+
},
1063+
postCompaction: { sessionId: "44444444-4444-4444-8444-444444444444" },
1064+
} satisfies NonNullable<SessionEntry["compactionCheckpoints"]>[number];
1065+
const newestDifferentCheckpoint = {
1066+
...oldMatchingCheckpoint,
1067+
checkpointId: "checkpoint-2",
1068+
createdAt: 30,
1069+
preCompaction: {
1070+
sessionId: sourceSessionId,
1071+
leafId: "different-leaf",
1072+
},
1073+
postCompaction: { sessionId: "55555555-5555-4555-8555-555555555555" },
1074+
} satisfies NonNullable<SessionEntry["compactionCheckpoints"]>[number];
1075+
fs.writeFileSync(
1076+
storePath,
1077+
JSON.stringify(
1078+
{
1079+
main: {
1080+
sessionId: sourceSessionId,
1081+
updatedAt: 30,
1082+
compactionCheckpoints: [
1083+
oldMatchingCheckpoint,
1084+
newestDifferentCheckpoint,
1085+
newestMatchingCheckpoint,
1086+
],
1087+
},
1088+
} satisfies Record<string, SessionEntry>,
1089+
null,
1090+
2,
1091+
),
1092+
"utf8",
1093+
);
1094+
1095+
const result = await branchSessionFromCompactionCheckpoint({
1096+
storePath,
1097+
sourceKey: "agent:main:main",
1098+
sourceStoreKey: "main",
1099+
nextKey: "agent:main:branch-newest",
1100+
checkpointId: "checkpoint-1",
1101+
forkTranscriptFromCheckpoint: async (selectedCheckpoint) => {
1102+
expect(selectedCheckpoint).toEqual(newestMatchingCheckpoint);
1103+
return {
1104+
status: "created",
1105+
transcript: {
1106+
sessionFile: branchPath,
1107+
sessionId: branchSessionId,
1108+
},
1109+
};
1110+
},
1111+
buildEntry: ({ currentEntry, forkedTranscript }) => ({
1112+
...currentEntry,
1113+
sessionFile: forkedTranscript.sessionFile,
1114+
sessionId: forkedTranscript.sessionId,
1115+
}),
1116+
});
1117+
1118+
expect(result).toMatchObject({
1119+
status: "created",
1120+
key: "agent:main:branch-newest",
1121+
checkpoint: newestMatchingCheckpoint,
1122+
});
1123+
});
1124+
10391125
it("does not persist checkpoint restores when the transcript boundary is missing", async () => {
10401126
fs.writeFileSync(
10411127
storePath,

src/config/sessions/session-accessor.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,9 +1355,16 @@ function findSessionCompactionCheckpoint(params: {
13551355
if (!checkpointId || !Array.isArray(params.entry.compactionCheckpoints)) {
13561356
return undefined;
13571357
}
1358-
return [...params.entry.compactionCheckpoints]
1359-
.toSorted((a, b) => b.createdAt - a.createdAt)
1360-
.find((checkpoint) => checkpoint.checkpointId === checkpointId);
1358+
let newest: SessionCompactionCheckpoint | undefined;
1359+
for (const checkpoint of params.entry.compactionCheckpoints) {
1360+
if (checkpoint.checkpointId !== checkpointId) {
1361+
continue;
1362+
}
1363+
if (!newest || checkpoint.createdAt > newest.createdAt) {
1364+
newest = checkpoint;
1365+
}
1366+
}
1367+
return newest;
13611368
}
13621369

13631370
type ApplySessionCompactionCheckpointMutationParams = {

0 commit comments

Comments
 (0)