Skip to content

Commit 21afbd6

Browse files
committed
fix(compaction): fence accounting commit
1 parent b82ff42 commit 21afbd6

2 files changed

Lines changed: 64 additions & 11 deletions

File tree

‎src/auto-reply/reply/session-updates.lifecycle.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,37 @@ describe("session-updates lifecycle hooks", () => {
265265
});
266266
expect(loadSessionEntry({ storePath, sessionKey })).not.toHaveProperty("totalTokens");
267267
});
268+
269+
it("does not commit accounting when authority closes after the queued updater", async () => {
270+
const { storePath, sessionKey, sessionStore, entry } = await createFixture();
271+
let authorized = true;
272+
let authorizeCalls = 0;
273+
274+
const result = await incrementCompactionCount({
275+
sessionEntry: entry,
276+
sessionStore,
277+
sessionKey,
278+
storePath,
279+
tokensAfter: 123,
280+
expectedSession: entry,
281+
authorize: () => {
282+
authorizeCalls += 1;
283+
if (authorizeCalls === 2) {
284+
queueMicrotask(() => {
285+
authorized = false;
286+
});
287+
}
288+
return authorized;
289+
},
290+
});
291+
292+
expect(result).toBeUndefined();
293+
expect(authorizeCalls).toBe(3);
294+
expect(sessionStore[sessionKey]).toBe(entry);
295+
expect(loadSessionEntry({ storePath, sessionKey })).toMatchObject({
296+
sessionId: "s1",
297+
compactionCount: 0,
298+
});
299+
expect(loadSessionEntry({ storePath, sessionKey })).not.toHaveProperty("totalTokens");
300+
});
268301
});

‎src/auto-reply/reply/session-updates.ts‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -397,17 +397,37 @@ export async function incrementCompactionCount(params: {
397397
: undefined;
398398
if (effectiveStorePath) {
399399
let committed = false;
400-
const persistedEntry = await patchSessionEntryCore(
401-
{ ...(agentId ? { agentId } : {}), storePath: effectiveStorePath, sessionKey },
402-
(current) => {
403-
if (!canApply(current)) {
404-
return null;
405-
}
406-
committed = true;
407-
return updates;
408-
},
409-
expectedSession ? {} : { fallbackEntry: nextEntry },
410-
);
400+
const authorityRevoked = new Error("compaction accounting authority revoked");
401+
let persistedEntry: SessionEntry | null;
402+
try {
403+
persistedEntry = await patchSessionEntryCore(
404+
{ ...(agentId ? { agentId } : {}), storePath: effectiveStorePath, sessionKey },
405+
(current) => {
406+
if (!canApply(current)) {
407+
return null;
408+
}
409+
committed = true;
410+
return updates;
411+
},
412+
{
413+
...(expectedSession ? {} : { fallbackEntry: nextEntry }),
414+
...(authorize
415+
? {
416+
assertCommitAllowed: () => {
417+
if (!authorize()) {
418+
throw authorityRevoked;
419+
}
420+
},
421+
}
422+
: {}),
423+
},
424+
);
425+
} catch (error) {
426+
if (error === authorityRevoked) {
427+
return undefined;
428+
}
429+
throw error;
430+
}
411431
if (!committed || !persistedEntry) {
412432
return undefined;
413433
}

0 commit comments

Comments
 (0)