fix(auto-reply): guard missing dispatcher getFailedCounts without weakening the SDK type

getDispatcherFinalOutcomeCounts called dispatcher.getFailedCounts()
without optional chaining while the adjacent getCancelledCounts?.() was
guarded, so dispatcher variants omitting getFailedCounts threw
"TypeError: dispatcher.getFailedCounts is not a function".

Keep the public ReplyDispatcher.getFailedCounts contract required (no
plugin-facing SDK break) and add an internal DispatcherOutcomeCountsView
plus a readDispatcherFailedCounts helper for the defensive accounting
paths in core dispatch (dispatch-from-config, dispatch-acp-delivery) and
the Feishu extension bot, each falling back to zero counts when a
non-conforming runtime dispatcher omits the method.

Fixes #89116

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Alix-007
2026-06-02 00:26:15 +08:00
committed by clawsweeper
parent ffbd02fe8e
commit 14487888b5
5 changed files with 66 additions and 7 deletions
+2 -2
View File
@@ -1575,7 +1575,7 @@ export async function handleFeishuMessage(params: {
turnResult.dispatched &&
shouldSendNoVisibleReplyFallback({
...turnResult.dispatchResult,
failedCounts: dispatcher.getFailedCounts(),
failedCounts: dispatcher.getFailedCounts?.() ?? { tool: 0, block: 0, final: 0 },
})
) {
await ensureNoVisibleReplyFallback("broadcast-dispatch-complete-no-visible-reply");
@@ -1771,7 +1771,7 @@ export async function handleFeishuMessage(params: {
if (
shouldSendNoVisibleReplyFallback({
...dispatchResult,
failedCounts: dispatcher.getFailedCounts(),
failedCounts: dispatcher.getFailedCounts?.() ?? { tool: 0, block: 0, final: 0 },
})
) {
await ensureNoVisibleReplyFallback("dispatch-complete-no-visible-reply");
@@ -16,6 +16,7 @@ import type { FinalizedMsgContext } from "../templating.js";
import type { ReplyPayload } from "../types.js";
import { waitForReplyDispatcherIdle } from "./reply-dispatcher.js";
import type { ReplyDispatchKind, ReplyDispatcher } from "./reply-dispatcher.types.js";
import { readDispatcherFailedCounts } from "./reply-dispatcher.types.js";
import { resolveRoutedDeliveryThreadId } from "./routed-delivery-thread.js";
const routeReplyRuntimeLoader = createLazyImportLoader(() => import("./route-reply.runtime.js"));
@@ -257,7 +258,7 @@ export function createAcpDispatchDeliveryCoordinator(params: {
state.settledDirectVisibleText = true;
hasPendingDirectBlockReplyDelivery = false;
await params.dispatcher.waitForIdle();
const failedCounts = params.dispatcher.getFailedCounts();
const failedCounts = readDispatcherFailedCounts(params.dispatcher);
const failedVisibleCount = failedCounts.block + failedCounts.final;
if (failedVisibleCount > 0) {
state.failedVisibleTextDelivery = true;
@@ -0,0 +1,35 @@
import { describe, expect, it } from "vitest";
import { getDispatcherFinalOutcomeCounts } from "./dispatch-from-config.js";
import type { ReplyDispatcher } from "./reply-dispatcher.types.js";
describe("getDispatcherFinalOutcomeCounts (#89116)", () => {
it("returns failed: 0 when the dispatcher does not implement getFailedCounts", () => {
// Some ReplyDispatcher variants omit the optional count methods entirely; the
// previous code called dispatcher.getFailedCounts() unguarded and threw
// "TypeError: dispatcher.getFailedCounts is not a function".
const dispatcher = {
getCancelledCounts: () => ({ tool: 0, block: 0, final: 2 }),
// getFailedCounts intentionally absent
} as unknown as ReplyDispatcher;
expect(() => getDispatcherFinalOutcomeCounts(dispatcher)).not.toThrow();
expect(getDispatcherFinalOutcomeCounts(dispatcher)).toEqual({ cancelled: 2, failed: 0 });
});
it("returns cancelled: 0 when getCancelledCounts is absent (existing behavior preserved)", () => {
const dispatcher = {
getFailedCounts: () => ({ tool: 0, block: 1, final: 3 }),
} as unknown as ReplyDispatcher;
expect(getDispatcherFinalOutcomeCounts(dispatcher)).toEqual({ cancelled: 0, failed: 3 });
});
it("uses the real final counts when both methods are present", () => {
const dispatcher = {
getCancelledCounts: () => ({ tool: 0, block: 0, final: 1 }),
getFailedCounts: () => ({ tool: 0, block: 0, final: 5 }),
} as unknown as ReplyDispatcher;
expect(getDispatcherFinalOutcomeCounts(dispatcher)).toEqual({ cancelled: 1, failed: 5 });
});
});
+9 -4
View File
@@ -132,7 +132,12 @@ import { withFullRuntimeReplyConfig } from "./get-reply-fast-path.js";
import { claimInboundDedupe, commitInboundDedupe, releaseInboundDedupe } from "./inbound-dedupe.js";
import { resolveOriginMessageProvider } from "./origin-routing.js";
import { waitForReplyDispatcherIdle } from "./reply-dispatcher.js";
import type { ReplyDispatchKind, ReplyDispatcher } from "./reply-dispatcher.types.js";
import type {
DispatcherOutcomeCountsView,
ReplyDispatchKind,
ReplyDispatcher,
} from "./reply-dispatcher.types.js";
import { readDispatcherFailedCounts } from "./reply-dispatcher.types.js";
import { replyRunRegistry, type ReplyOperation } from "./reply-run-registry.js";
import { isReplyProfilerEnabled } from "./reply-timing-tracker.js";
import { admitReplyTurn, resolveReplyTurnKind } from "./reply-turn-admission.js";
@@ -774,13 +779,13 @@ async function mirrorInternalSourceReplyToTranscript(params: {
}
}
function getDispatcherFinalOutcomeCounts(dispatcher: ReplyDispatcher): {
export function getDispatcherFinalOutcomeCounts(dispatcher: DispatcherOutcomeCountsView): {
cancelled: number;
failed: number;
} {
return {
cancelled: dispatcher.getCancelledCounts?.().final ?? 0,
failed: dispatcher.getFailedCounts().final,
failed: dispatcher.getFailedCounts?.().final ?? 0,
};
}
@@ -903,7 +908,7 @@ function createAbortAwareDispatcher(params: {
sendFinalReply: sendIfActive(params.dispatcher.sendFinalReply),
waitForIdle: () => params.dispatcher.waitForIdle(),
getQueuedCounts: () => params.dispatcher.getQueuedCounts(),
getFailedCounts: () => params.dispatcher.getFailedCounts(),
getFailedCounts: () => readDispatcherFailedCounts(params.dispatcher),
markComplete: () => {
if (!params.isAborted()) {
params.dispatcher.markComplete();
@@ -18,3 +18,21 @@ export type ReplyDispatcher = {
getFailedCounts: () => Record<ReplyDispatchKind, number>;
markComplete: () => void;
};
/**
* Internal view for defensive outcome-count accounting. Some non-conforming
* runtime dispatcher variants (for example plugin-provided dispatchers) may omit
* these readers even though the public ReplyDispatcher contract requires
* getFailedCounts. Read the counters through this view so the guards stay
* type-correct without weakening the SDK-visible ReplyDispatcher type.
*/
export type DispatcherOutcomeCountsView = {
getCancelledCounts?: () => Record<ReplyDispatchKind, number>;
getFailedCounts?: () => Record<ReplyDispatchKind, number>;
};
export function readDispatcherFailedCounts(
dispatcher: DispatcherOutcomeCountsView,
): Record<ReplyDispatchKind, number> {
return dispatcher.getFailedCounts?.() ?? { tool: 0, block: 0, final: 0 };
}