Skip to content

Commit dc6888f

Browse files
committed
fix(reply): preserve preflight failure handling
1 parent d961507 commit dc6888f

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/auto-reply/reply/agent-runner-direct-runtime-config.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,29 @@ describe("runReplyAgent runtime config", () => {
491491
expect(runAgentTurnWithFallbackMock).toHaveBeenCalledOnce();
492492
});
493493

494+
it("surfaces unrelated preflight failures after an exhausted memory flush", async () => {
495+
const { replyParams } = createDirectRuntimeReplyParams({
496+
shouldFollowup: false,
497+
isActive: false,
498+
});
499+
runMemoryFlushIfNeededMock.mockResolvedValue({
500+
sessionEntry: { sessionId: "session-1", updatedAt: 1, compactionCount: 4 },
501+
outcome: "exhausted",
502+
});
503+
runPreflightCompactionIfNeededMock.mockRejectedValue(
504+
new Error("Preflight compaction required but failed: auth profile mismatch"),
505+
);
506+
507+
const result = await runReplyAgent(replyParams);
508+
509+
if (!result || Array.isArray(result)) {
510+
throw new Error("expected a single preflight compaction failure reply payload");
511+
}
512+
expect(result.text).toContain("auto-compaction could not recover");
513+
expect(resetReplyRunSessionMock).not.toHaveBeenCalled();
514+
expect(runAgentTurnWithFallbackMock).not.toHaveBeenCalled();
515+
});
516+
494517
it("does not start the main turn after cancellation during memory flush", async () => {
495518
const { replyParams } = createDirectRuntimeReplyParams({
496519
shouldFollowup: false,

src/auto-reply/reply/agent-runner.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "../../agents/agent-scope.js";
1111
import { resolveContextTokensForModel } from "../../agents/context.js";
1212
import { DEFAULT_CONTEXT_TOKENS } from "../../agents/defaults.js";
13+
import { isLikelyContextOverflowError } from "../../agents/embedded-agent-helpers/errors.js";
1314
import {
1415
hasCommittedSourceReplyDeliveryEvidence,
1516
hasVisibleCommittedMessagingToolDeliveryEvidence,
@@ -1661,7 +1662,11 @@ export async function runReplyAgent(params: {
16611662
preflightCompactionApplied =
16621663
(activeSessionEntry?.compactionCount ?? 0) > prePreflightCompactionCount;
16631664
} catch (err) {
1664-
if (memoryFlushResult.outcome !== "exhausted" || replyOperation.abortSignal.aborted) {
1665+
const canRotateAfterPreflightFailure =
1666+
memoryFlushResult.outcome === "exhausted" &&
1667+
!replyOperation.abortSignal.aborted &&
1668+
isLikelyContextOverflowError(String(err));
1669+
if (!canRotateAfterPreflightFailure) {
16651670
throw err;
16661671
}
16671672
logVerbose(`Preflight compaction could not recover exhausted memory flush: ${String(err)}`);

0 commit comments

Comments
 (0)