Skip to content

Commit 63ed9ad

Browse files
clawsweeper[bot]Alix-007claudeTakhoffman
authored
fix(auto-reply): guard missing dispatcher getFailedCounts without weakening the SDK type (#89318)
Summary: - Adds defensive failed-count reads in auto-reply/ACP accounting and Feishu fallback paths, plus a focused regression test, while keeping `ReplyDispatcher.getFailedCounts` required. - PR surface: Source +24, Tests +35. Total +59 across 5 files. - Reproducibility: yes. from source inspection. Current main calls `dispatcher.getFailedCounts().final` and si ... issing that method follows a clear TypeError path; the source PR also supplied terminal before/after proof. Automerge notes: - PR branch already contained follow-up commit before automerge: fix(auto-reply): guard missing dispatcher getFailedCounts without wea… Validation: - ClawSweeper review passed for head 0bdfb4a. - Required merge gates passed before the squash merge. Prepared head SHA: 0bdfb4a Review: #89318 (comment) Co-authored-by: Alix-007 <li.long15@xydigit.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: clawsweeper <274271284+clawsweeper[bot]@users.noreply.github.com> Co-authored-by: clawsweeper[bot] <274271284+clawsweeper[bot]@users.noreply.github.com> Approved-by: takhoffman Co-authored-by: takhoffman <781889+takhoffman@users.noreply.github.com>
1 parent e6b5083 commit 63ed9ad

5 files changed

Lines changed: 66 additions & 7 deletions

File tree

extensions/feishu/src/bot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,7 @@ export async function handleFeishuMessage(params: {
15751575
turnResult.dispatched &&
15761576
shouldSendNoVisibleReplyFallback({
15771577
...turnResult.dispatchResult,
1578-
failedCounts: dispatcher.getFailedCounts(),
1578+
failedCounts: dispatcher.getFailedCounts?.() ?? { tool: 0, block: 0, final: 0 },
15791579
})
15801580
) {
15811581
await ensureNoVisibleReplyFallback("broadcast-dispatch-complete-no-visible-reply");
@@ -1771,7 +1771,7 @@ export async function handleFeishuMessage(params: {
17711771
if (
17721772
shouldSendNoVisibleReplyFallback({
17731773
...dispatchResult,
1774-
failedCounts: dispatcher.getFailedCounts(),
1774+
failedCounts: dispatcher.getFailedCounts?.() ?? { tool: 0, block: 0, final: 0 },
17751775
})
17761776
) {
17771777
await ensureNoVisibleReplyFallback("dispatch-complete-no-visible-reply");

src/auto-reply/reply/dispatch-acp-delivery.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type { FinalizedMsgContext } from "../templating.js";
1616
import type { ReplyPayload } from "../types.js";
1717
import { waitForReplyDispatcherIdle } from "./reply-dispatcher.js";
1818
import type { ReplyDispatchKind, ReplyDispatcher } from "./reply-dispatcher.types.js";
19+
import { readDispatcherFailedCounts } from "./reply-dispatcher.types.js";
1920
import { resolveRoutedDeliveryThreadId } from "./routed-delivery-thread.js";
2021

2122
const routeReplyRuntimeLoader = createLazyImportLoader(() => import("./route-reply.runtime.js"));
@@ -257,7 +258,7 @@ export function createAcpDispatchDeliveryCoordinator(params: {
257258
state.settledDirectVisibleText = true;
258259
hasPendingDirectBlockReplyDelivery = false;
259260
await params.dispatcher.waitForIdle();
260-
const failedCounts = params.dispatcher.getFailedCounts();
261+
const failedCounts = readDispatcherFailedCounts(params.dispatcher);
261262
const failedVisibleCount = failedCounts.block + failedCounts.final;
262263
if (failedVisibleCount > 0) {
263264
state.failedVisibleTextDelivery = true;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { describe, expect, it } from "vitest";
2+
import { getDispatcherFinalOutcomeCounts } from "./dispatch-from-config.js";
3+
import type { ReplyDispatcher } from "./reply-dispatcher.types.js";
4+
5+
describe("getDispatcherFinalOutcomeCounts (#89116)", () => {
6+
it("returns failed: 0 when the dispatcher does not implement getFailedCounts", () => {
7+
// Some ReplyDispatcher variants omit the optional count methods entirely; the
8+
// previous code called dispatcher.getFailedCounts() unguarded and threw
9+
// "TypeError: dispatcher.getFailedCounts is not a function".
10+
const dispatcher = {
11+
getCancelledCounts: () => ({ tool: 0, block: 0, final: 2 }),
12+
// getFailedCounts intentionally absent
13+
} as unknown as ReplyDispatcher;
14+
15+
expect(() => getDispatcherFinalOutcomeCounts(dispatcher)).not.toThrow();
16+
expect(getDispatcherFinalOutcomeCounts(dispatcher)).toEqual({ cancelled: 2, failed: 0 });
17+
});
18+
19+
it("returns cancelled: 0 when getCancelledCounts is absent (existing behavior preserved)", () => {
20+
const dispatcher = {
21+
getFailedCounts: () => ({ tool: 0, block: 1, final: 3 }),
22+
} as unknown as ReplyDispatcher;
23+
24+
expect(getDispatcherFinalOutcomeCounts(dispatcher)).toEqual({ cancelled: 0, failed: 3 });
25+
});
26+
27+
it("uses the real final counts when both methods are present", () => {
28+
const dispatcher = {
29+
getCancelledCounts: () => ({ tool: 0, block: 0, final: 1 }),
30+
getFailedCounts: () => ({ tool: 0, block: 0, final: 5 }),
31+
} as unknown as ReplyDispatcher;
32+
33+
expect(getDispatcherFinalOutcomeCounts(dispatcher)).toEqual({ cancelled: 1, failed: 5 });
34+
});
35+
});

src/auto-reply/reply/dispatch-from-config.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ import { withFullRuntimeReplyConfig } from "./get-reply-fast-path.js";
132132
import { claimInboundDedupe, commitInboundDedupe, releaseInboundDedupe } from "./inbound-dedupe.js";
133133
import { resolveOriginMessageProvider } from "./origin-routing.js";
134134
import { waitForReplyDispatcherIdle } from "./reply-dispatcher.js";
135-
import type { ReplyDispatchKind, ReplyDispatcher } from "./reply-dispatcher.types.js";
135+
import type {
136+
DispatcherOutcomeCountsView,
137+
ReplyDispatchKind,
138+
ReplyDispatcher,
139+
} from "./reply-dispatcher.types.js";
140+
import { readDispatcherFailedCounts } from "./reply-dispatcher.types.js";
136141
import { replyRunRegistry, type ReplyOperation } from "./reply-run-registry.js";
137142
import { isReplyProfilerEnabled } from "./reply-timing-tracker.js";
138143
import { admitReplyTurn, resolveReplyTurnKind } from "./reply-turn-admission.js";
@@ -774,13 +779,13 @@ async function mirrorInternalSourceReplyToTranscript(params: {
774779
}
775780
}
776781

777-
function getDispatcherFinalOutcomeCounts(dispatcher: ReplyDispatcher): {
782+
export function getDispatcherFinalOutcomeCounts(dispatcher: DispatcherOutcomeCountsView): {
778783
cancelled: number;
779784
failed: number;
780785
} {
781786
return {
782787
cancelled: dispatcher.getCancelledCounts?.().final ?? 0,
783-
failed: dispatcher.getFailedCounts().final,
788+
failed: readDispatcherFailedCounts(dispatcher).final,
784789
};
785790
}
786791

@@ -903,7 +908,7 @@ function createAbortAwareDispatcher(params: {
903908
sendFinalReply: sendIfActive(params.dispatcher.sendFinalReply),
904909
waitForIdle: () => params.dispatcher.waitForIdle(),
905910
getQueuedCounts: () => params.dispatcher.getQueuedCounts(),
906-
getFailedCounts: () => params.dispatcher.getFailedCounts(),
911+
getFailedCounts: () => readDispatcherFailedCounts(params.dispatcher),
907912
markComplete: () => {
908913
if (!params.isAborted()) {
909914
params.dispatcher.markComplete();

src/auto-reply/reply/reply-dispatcher.types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,21 @@ export type ReplyDispatcher = {
1818
getFailedCounts: () => Record<ReplyDispatchKind, number>;
1919
markComplete: () => void;
2020
};
21+
22+
/**
23+
* Internal view for defensive outcome-count accounting. Some non-conforming
24+
* runtime dispatcher variants (for example plugin-provided dispatchers) may omit
25+
* these readers even though the public ReplyDispatcher contract requires
26+
* getFailedCounts. Read the counters through this view so the guards stay
27+
* type-correct without weakening the SDK-visible ReplyDispatcher type.
28+
*/
29+
export type DispatcherOutcomeCountsView = {
30+
getCancelledCounts?: () => Record<ReplyDispatchKind, number>;
31+
getFailedCounts?: () => Record<ReplyDispatchKind, number>;
32+
};
33+
34+
export function readDispatcherFailedCounts(
35+
dispatcher: DispatcherOutcomeCountsView,
36+
): Record<ReplyDispatchKind, number> {
37+
return dispatcher.getFailedCounts?.() ?? { tool: 0, block: 0, final: 0 };
38+
}

0 commit comments

Comments
 (0)