From fc3b79d7f4e6d4a09ef738c1abb36ac2cbdf2a69 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 8 Jul 2026 13:03:41 +0530 Subject: [PATCH] fix(agents): fall stale_run steer refusals through to direct delivery --- src/agents/subagent-announce-delivery.test.ts | 54 +++++++++++++++++++ src/agents/subagent-announce-delivery.ts | 6 +++ 2 files changed, 60 insertions(+) diff --git a/src/agents/subagent-announce-delivery.test.ts b/src/agents/subagent-announce-delivery.test.ts index 4ba394256138..dacdb7def303 100644 --- a/src/agents/subagent-announce-delivery.test.ts +++ b/src/agents/subagent-announce-delivery.test.ts @@ -1082,6 +1082,60 @@ describe("deliverSubagentAnnouncement active requester steering", () => { }); expect(callGateway).toHaveBeenCalledTimes(1); }); + + it("falls through to direct delivery when steering is refused for a stale run", async () => { + // An evidence-dead requester still registers as "active", but it will not + // drain its steer queue; dropping here would discard the handoff. + const queueEmbeddedAgentMessageWithOutcome = vi.fn(async (sessionId: string) => ({ + queued: false as const, + sessionId, + reason: "stale_run" as const, + gatewayHealth: "live" as const, + })); + const callGateway = createGatewayMock({ + result: { + payloads: [{ text: "child completion output" }], + }, + }); + testing.setDepsForTest({ + callGateway, + getRequesterSessionActivity: () => ({ + sessionId: "paperclip-session", + isActive: true, + }), + queueEmbeddedAgentMessageWithOutcome, + getRuntimeConfig: () => + ({ + messages: { + queue: { + mode: "steer", + debounceMs: 0, + }, + }, + }) as never, + }); + + const result = await deliverSubagentAnnouncement({ + requesterSessionKey: "agent:eng:paperclip:issue:123", + targetRequesterSessionKey: "agent:eng:paperclip:issue:123", + triggerMessage: "child done", + steerMessage: "child done", + requesterOrigin: slackThreadOrigin, + requesterIsSubagent: false, + expectsCompletionMessage: false, + directIdempotencyKey: "announce-stale-run-direct-fallback", + }); + + expectRecordFields(result, { + delivered: true, + path: "direct", + phases: [ + { phase: "steer-primary", delivered: false, path: "none", error: undefined }, + { phase: "direct-primary", delivered: true, path: "direct", error: undefined }, + ], + }); + expect(callGateway).toHaveBeenCalledTimes(1); + }); }); describe("deliverSubagentAnnouncement completion delivery", () => { diff --git a/src/agents/subagent-announce-delivery.ts b/src/agents/subagent-announce-delivery.ts index 3b6e3925b6d7..02eb27d6bbaa 100644 --- a/src/agents/subagent-announce-delivery.ts +++ b/src/agents/subagent-announce-delivery.ts @@ -720,6 +720,12 @@ async function maybeSteerSubagentAnnounce(params: { }; } + // A stale_run refusal means the requester run is evidence-dead: it will not + // drain its steer queue, so "dropped" would discard the handoff. Report + // not-active so dispatch takes the direct fallback instead. + if (queueOutcome.reason === "stale_run") { + return { status: "none" }; + } const currentActivity = resolveRequesterSessionActivity(canonicalKey); return { status: currentActivity.isActive ? "dropped" : "none" }; }