Skip to content

Commit 7f12a36

Browse files
committed
fix(gateway): discard prepared media when a hook blocks the turn
#124100's disarm-at-ACK custody rests on dispatch persisting the referencing transcript on every post-ACK path. The blocked-turn path breaks that: a before_agent_run block persists only the redacted block reason — no media markers, no media facts — so the offloaded inbound refs stay unreferenced forever (the inbound sweep is off unless attachments.ttlHours is set). Dispatch settle now discards the prepared refs when the recorder ended blocked, applying the same custody rule as the pre-ACK owner. Regression test drives a blocked dispatch with an offloaded attachment and asserts the inbound dir returns to baseline; fails pre-fix.
1 parent 2585edb commit 7f12a36

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

‎src/gateway/server-methods/chat-send-agent-dispatch.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import type { ChatRunTiming } from "../server-chat-state.js";
1818
import { tryResolveSessionCompatibilityOwnerAgentId } from "../session-request-agent.js";
1919
import { broadcastChatError, broadcastChatFinal } from "./chat-broadcast.js";
2020
import type { AdmittedChatSend } from "./chat-send-admission.js";
21-
import type { prepareChatSendAttachments } from "./chat-send-attachments.js";
21+
import {
22+
discardPreparedChatSendAttachments,
23+
type prepareChatSendAttachments,
24+
} from "./chat-send-attachments.js";
2225
import {
2326
resolveWebchatPromptCacheKey,
2427
scheduleChatDashboardSessionTitle,
@@ -507,6 +510,13 @@ export function startChatDispatch(params: StartChatDispatchParams): void {
507510
.catch(dispatchErrorLifecycle.handleError)
508511
.finally(() => {
509512
dispatchErrorLifecycle.finalize();
513+
if (userTurnRecorder.isBlocked() && attachments.offloadedRefs.length > 0) {
514+
// A blocked turn persists only the redacted block reason — no media
515+
// markers — so the prepared inbound media stays unreferenced forever
516+
// (sweep is off by default). Same custody rule as the pre-ACK owner
517+
// in chat-send-admission.ts: unreferenced staged media is discarded.
518+
void discardPreparedChatSendAttachments(attachments.offloadedRefs);
519+
}
510520
// Cosmetic title work starts only after the accepted turn finishes. Starting it
511521
// before dispatch can make a cold utility runtime starve the user's real turn.
512522
scheduleChatDashboardSessionTitle({

‎src/gateway/server.chat.gateway-server-chat-b.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,6 +2112,60 @@ describe("gateway server chat", () => {
21122112
}
21132113
});
21142114

2115+
test("chat.send discards prepared inbound media when a hook blocks the turn", async () => {
2116+
openDirectChatSession();
2117+
try {
2118+
await writeStoredMainSession({
2119+
modelProvider: "test-provider",
2120+
model: "vision-model",
2121+
});
2122+
const context = createDirectChatContext({ getRuntimeConfig: () => ({}) });
2123+
const inboundDir = path.join(getMediaDir(), "inbound");
2124+
const inboundBaseline = new Set(await fs.readdir(inboundDir).catch(() => []));
2125+
// A before_agent_run block persists only the redacted reason — no media
2126+
// markers — so dispatch must discard the prepared refs on settle.
2127+
dispatchInboundMessageMock.mockImplementationOnce(async (params: unknown) => {
2128+
const recorder = (
2129+
params as {
2130+
replyOptions?: { userTurnTranscriptRecorder?: { markBlocked: () => void } };
2131+
}
2132+
).replyOptions?.userTurnTranscriptRecorder;
2133+
recorder?.markBlocked();
2134+
});
2135+
const responses: Array<{ ok: boolean; payload?: unknown; error?: unknown }> = [];
2136+
await callDirectChat("chat.send", {
2137+
id: "blocked-turn-media",
2138+
params: makeChatSendParams({
2139+
message: "blocked turn with media",
2140+
idempotencyKey: "idem-blocked-turn-media",
2141+
attachments: [
2142+
{
2143+
type: "file",
2144+
mimeType: "text/plain",
2145+
fileName: "notes.txt",
2146+
content: Buffer.from("offloaded inbound media").toString("base64"),
2147+
},
2148+
],
2149+
}),
2150+
client: {
2151+
connId: "conn-owner",
2152+
connect: { device: { id: "dev-owner" }, scopes: ["operator.write"] },
2153+
} as never,
2154+
respond: ((ok, payload, error) => {
2155+
responses.push({ ok, payload, error });
2156+
}) as RespondFn,
2157+
context,
2158+
});
2159+
expect(responses[0]?.ok).toBe(true);
2160+
await waitForFast(async () => {
2161+
const remaining = await fs.readdir(inboundDir).catch(() => []);
2162+
expect(remaining.filter((name) => !inboundBaseline.has(name))).toEqual([]);
2163+
}, FAST_WAIT_OPTS);
2164+
} finally {
2165+
resetDirectChatSession();
2166+
}
2167+
});
2168+
21152169
test("chat.send discards prepared inbound media when setup throws before the ACK", async () => {
21162170
openDirectChatSession();
21172171
try {

0 commit comments

Comments
 (0)