Skip to content

Commit 897656b

Browse files
Han KimHan Kim
authored andcommitted
fix(codex): emit side effects after harness rotation
1 parent 09f0c0b commit 897656b

2 files changed

Lines changed: 50 additions & 25 deletions

File tree

src/agents/pi-embedded-runner/compact.hooks.test.ts

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,11 @@ describe("compactEmbeddedPiSession hooks (ownsCompaction engine)", () => {
13261326
});
13271327
});
13281328

1329-
it("rotates the active transcript after harness compaction without a delegated successor", async () => {
1329+
it("rotates the active transcript after harness compaction without a delegated successor and emits side effects", async () => {
1330+
const listener = vi.fn();
1331+
const cleanup = onSessionTranscriptUpdate(listener);
1332+
const sync = vi.fn(async () => {});
1333+
getMemorySearchManagerMock.mockResolvedValue({ manager: { sync } });
13301334
maybeCompactAgentHarnessSessionMock.mockResolvedValueOnce({
13311335
ok: true,
13321336
compacted: true,
@@ -1344,31 +1348,46 @@ describe("compactEmbeddedPiSession hooks (ownsCompaction engine)", () => {
13441348
leafId: "leaf-1",
13451349
});
13461350

1347-
const result = await compactEmbeddedPiSession(
1348-
wrappedCompactionArgs({
1349-
currentTokenCount: 333,
1350-
config: {
1351-
agents: {
1352-
defaults: {
1353-
compaction: {
1354-
truncateAfterCompaction: true,
1351+
try {
1352+
const result = await compactEmbeddedPiSession(
1353+
wrappedCompactionArgs({
1354+
currentTokenCount: 333,
1355+
config: {
1356+
agents: {
1357+
defaults: {
1358+
compaction: {
1359+
truncateAfterCompaction: true,
1360+
postIndexSync: "await",
1361+
},
13551362
},
13561363
},
1357-
},
1358-
},
1359-
}),
1360-
);
1364+
} as never,
1365+
}),
1366+
);
13611367

1362-
expect(result.ok).toBe(true);
1363-
expect(result.compacted).toBe(true);
1364-
expect(rotateTranscriptFileAfterCompactionMock).toHaveBeenCalledWith({
1365-
sessionFile: TEST_SESSION_FILE,
1366-
synthesizeMissingBoundary: true,
1367-
});
1368-
expect(result.result?.sessionId).toBe("rotated-session");
1369-
expect(result.result?.sessionFile).toBe("/tmp/rotated-session.jsonl");
1370-
expect(result.result?.tokensBefore).toBe(333);
1371-
expect(result.result?.details).toEqual({ backend: "codex-app-server" });
1368+
expect(result.ok).toBe(true);
1369+
expect(result.compacted).toBe(true);
1370+
expect(rotateTranscriptFileAfterCompactionMock).toHaveBeenCalledWith({
1371+
sessionFile: TEST_SESSION_FILE,
1372+
synthesizeMissingBoundary: true,
1373+
});
1374+
expect(result.result?.sessionId).toBe("rotated-session");
1375+
expect(result.result?.sessionFile).toBe("/tmp/rotated-session.jsonl");
1376+
expect(result.result?.tokensBefore).toBe(333);
1377+
expect(result.result?.details).toEqual({ backend: "codex-app-server" });
1378+
expect(listener).toHaveBeenCalledTimes(1);
1379+
expect(listener).toHaveBeenCalledWith({
1380+
sessionFile: "/tmp/rotated-session.jsonl",
1381+
sessionKey: TEST_SESSION_KEY,
1382+
});
1383+
expect(sync).toHaveBeenCalledTimes(1);
1384+
expect(sync).toHaveBeenCalledWith({
1385+
reason: "post-compaction",
1386+
sessionFiles: ["/tmp/rotated-session.jsonl"],
1387+
});
1388+
} finally {
1389+
cleanup();
1390+
}
13721391
});
13731392

13741393
it("does not fire after_compaction when compaction fails", async () => {

src/agents/pi-embedded-runner/compact.queued.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,16 @@ async function finalizeHarnessCompactionTranscript(params: {
350350
if (!rotation.rotated) {
351351
return result;
352352
}
353+
const postCompactionSessionFile = rotation.sessionFile ?? params.params.sessionFile;
353354
log.info(
354355
`[compaction] rotated active transcript after harness compaction ` +
355356
`(sessionKey=${params.params.sessionKey ?? params.params.sessionId})`,
356357
);
358+
await runPostCompactionSideEffects({
359+
config: params.params.config,
360+
sessionKey: params.params.sessionKey,
361+
sessionFile: postCompactionSessionFile,
362+
});
357363
return {
358364
...result,
359365
result: {
@@ -367,8 +373,8 @@ async function finalizeHarnessCompactionTranscript(params: {
367373
...(rotation.sessionId && rotation.sessionId !== params.params.sessionId
368374
? { sessionId: rotation.sessionId }
369375
: {}),
370-
...(rotation.sessionFile && rotation.sessionFile !== params.params.sessionFile
371-
? { sessionFile: rotation.sessionFile }
376+
...(postCompactionSessionFile !== params.params.sessionFile
377+
? { sessionFile: postCompactionSessionFile }
372378
: {}),
373379
},
374380
};

0 commit comments

Comments
 (0)