Skip to content

Commit 883a998

Browse files
committed
fix(agents): preserve manual abort outcomes
1 parent c08d8da commit 883a998

2 files changed

Lines changed: 66 additions & 4 deletions

File tree

‎src/agents/sessions/agent-session-compaction.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,64 @@ describe("AgentSession compaction", () => {
177177
subscription.unsubscribe();
178178
});
179179

180+
it("projects a rejected manual cancellation as aborted without recording compaction", async () => {
181+
const sessionManager = SessionManager.inMemory();
182+
sessionManager.appendMessage({ role: "user", content: "old prompt", timestamp: 1 });
183+
sessionManager.appendMessage({
184+
...createAssistant(testModel, [{ type: "text", text: "old answer" }]),
185+
timestamp: 2,
186+
});
187+
const handlers = createCompactionHandlers();
188+
const syntheticError = new Error("synthetic manual cancellation rejection");
189+
const abortActiveCompaction = () => session.abortCompaction();
190+
handlers.set("session_before_compact", [
191+
async () => {
192+
abortActiveCompaction();
193+
throw syntheticError;
194+
},
195+
]);
196+
streamMocks.streamSimple.mockImplementation(
197+
(_activeModel: Model, _context: Context, options?: SimpleStreamOptions) => {
198+
if (options?.signal?.aborted) {
199+
throw syntheticError;
200+
}
201+
return createAssistantResultStream(
202+
createAssistant(testModel, [{ type: "text", text: "unexpected compaction" }]),
203+
);
204+
},
205+
);
206+
const { session } = await createTestSession({
207+
sessionManager,
208+
resourceLoader: createResourceLoader(handlers),
209+
});
210+
const onAgentEvent = vi.fn();
211+
const subscription = subscribeEmbeddedAgentSession({
212+
session,
213+
runId: "run-rejected-manual-cancellation",
214+
onAgentEvent,
215+
});
216+
217+
await expect(session.compact()).rejects.toBe(syntheticError);
218+
219+
const compactionEvents = onAgentEvent.mock.calls
220+
.map(([event]) => event)
221+
.filter((event) => event.stream === "compaction");
222+
expect(compactionEvents).toHaveLength(2);
223+
expect(compactionEvents.at(-1)).toEqual({
224+
stream: "compaction",
225+
data: {
226+
phase: "end",
227+
outcome: "aborted",
228+
completed: false,
229+
willRetry: false,
230+
},
231+
});
232+
expect(subscription.getCompactionCount()).toBe(0);
233+
expect(subscription.getLastCompactionTokensAfter()).toBeUndefined();
234+
expect(sessionManager.getEntries().some((entry) => entry.type === "compaction")).toBe(false);
235+
subscription.unsubscribe();
236+
});
237+
180238
it("caps extension summaries before persistence and manual return", async () => {
181239
const sessionManager = SessionManager.inMemory();
182240
sessionManager.appendMessage({ role: "user", content: "old prompt", timestamp: 1 });

‎src/agents/sessions/agent-session-compaction.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ export abstract class AgentSessionCompaction extends AgentSessionInspection {
6666
): Promise<CompactionResult> {
6767
this.disconnectFromAgent();
6868
await this.abort();
69-
this.compactionAbortController = new AbortController();
69+
const abortController = new AbortController();
70+
this.compactionAbortController = abortController;
7071
this.emit({ type: "compaction_start", reason: "manual" });
7172

7273
try {
@@ -78,11 +79,12 @@ export abstract class AgentSessionCompaction extends AgentSessionInspection {
7879
mode: "manual",
7980
summaryOutputPolicy,
8081
settings,
81-
signal: this.compactionAbortController.signal,
82+
signal: abortController.signal,
8283
});
8384
} catch (error) {
8485
const message = compactionErrorMessage(error, "Compaction failed");
85-
const aborted = error instanceof Error && error.name === "AbortError";
86+
const aborted =
87+
abortController.signal.aborted || (error instanceof Error && error.name === "AbortError");
8688
this.emit({
8789
type: "compaction_end",
8890
reason: "manual",
@@ -113,7 +115,9 @@ export abstract class AgentSessionCompaction extends AgentSessionInspection {
113115
});
114116
return outcome.result;
115117
} finally {
116-
this.compactionAbortController = undefined;
118+
if (this.compactionAbortController === abortController) {
119+
this.compactionAbortController = undefined;
120+
}
117121
this.reconnectToAgent();
118122
}
119123
}

0 commit comments

Comments
 (0)