Skip to content

Commit 6f54292

Browse files
committed
Restore ACP manual-spawn task suppression
1 parent e37c3d5 commit 6f54292

1 file changed

Lines changed: 65 additions & 3 deletions

File tree

‎src/gateway/server-methods/agent.ts‎

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import {
1414
GATEWAY_CLIENT_CAPS,
1515
GATEWAY_CLIENT_MODES,
16+
GATEWAY_CLIENT_NAMES,
1617
hasGatewayClientCap,
1718
} from "../../../packages/gateway-protocol/src/client-info.js";
1819
import {
@@ -641,13 +642,63 @@ function resolveGatewayAgentTaskTrackingMode(params: {
641642
client: GatewayRequestHandlerOptions["client"];
642643
sessionKey?: string;
643644
inputProvenance?: InputProvenance;
645+
confirmedAcpManualSpawn?: boolean;
644646
}): GatewayAgentTaskTrackingMode {
645647
if (!params.sessionKey?.trim() || params.inputProvenance?.kind === "inter_session") {
646648
return "none";
647649
}
648-
return params.client?.internal?.agentRunTracking === "plugin_subagent"
649-
? "plugin_subagent"
650-
: "cli";
650+
if (params.client?.internal?.agentRunTracking === "plugin_subagent") {
651+
return "plugin_subagent";
652+
}
653+
// A confirmed ACP manual-spawn child turn already owns its requester-visible
654+
// `acp` task row from the spawn control plane (src/agents/acp-spawn.ts). The
655+
// Gateway CLI path runs that same childRunId, so tracking it here would emit a
656+
// duplicate row for one run. Suppress only the CLI branch; plugin-subagent and
657+
// normal CLI tracking stay intact.
658+
if (params.confirmedAcpManualSpawn) {
659+
return "none";
660+
}
661+
return "cli";
662+
}
663+
664+
function isTrustedBackendAcpSpawnClient(client: GatewayRequestHandlerOptions["client"]): boolean {
665+
// The ACP spawn control plane reaches the gateway through the in-process
666+
// backend client (src/gateway/call.ts -> mode "backend", id "gateway-client").
667+
// Only that caller creates the replacement `acp` task row, so CLI suppression
668+
// is gated to it. An operator-write UI/CLI/mobile or device-token client that
669+
// merely sets acpTurnSource owns no such row and must keep CLI tracking.
670+
return (
671+
client?.connect?.client?.id === GATEWAY_CLIENT_NAMES.GATEWAY_CLIENT &&
672+
client.connect.client.mode === GATEWAY_CLIENT_MODES.BACKEND &&
673+
client.isDeviceTokenAuth !== true
674+
);
675+
}
676+
677+
function isConfirmedAcpManualSpawnTaskOwner(params: {
678+
acpTurnSource?: string;
679+
sessionKey?: string;
680+
client: GatewayRequestHandlerOptions["client"];
681+
logGateway: Pick<GatewayRequestContext["logGateway"], "warn">;
682+
}): boolean {
683+
const sessionKey = params.sessionKey;
684+
if (
685+
!isTrustedBackendAcpSpawnClient(params.client) ||
686+
params.acpTurnSource !== "manual_spawn" ||
687+
sessionKey == null ||
688+
!isAcpSessionKey(sessionKey)
689+
) {
690+
return false;
691+
}
692+
try {
693+
return readAcpSessionMeta({ sessionKey }) != null;
694+
} catch (err) {
695+
params.logGateway.warn(
696+
`failed to read ACP session metadata for manual-spawn task tracking ${sessionKey}; falling back to cli task tracking: ${formatForLog(
697+
err,
698+
)}`,
699+
);
700+
return false;
701+
}
651702
}
652703

653704
async function registerPluginSubagentRunFromGateway(params: {
@@ -2678,10 +2729,21 @@ export const agentHandlers: GatewayRequestHandlers = {
26782729
}
26792730

26802731
const resolvedThreadId = explicitThreadId ?? deliveryPlan.resolvedThreadId;
2732+
// Confirmed only when the caller is the trusted in-process backend ACP
2733+
// spawn client, the turn is an ACP manual spawn, the canonical session key
2734+
// is ACP-shaped, and persisted ACP metadata exists for it; the spawn
2735+
// control plane owns that childRunId's `acp` task row in those cases.
2736+
const confirmedAcpManualSpawn = isConfirmedAcpManualSpawnTaskOwner({
2737+
acpTurnSource: request.acpTurnSource,
2738+
sessionKey: resolvedSessionKey,
2739+
client,
2740+
logGateway: context.logGateway,
2741+
});
26812742
const taskTrackingMode = resolveGatewayAgentTaskTrackingMode({
26822743
client,
26832744
sessionKey: resolvedSessionKey,
26842745
inputProvenance,
2746+
confirmedAcpManualSpawn,
26852747
});
26862748
let dispatchTaskTrackingMode: Exclude<GatewayAgentTaskTrackingMode, "plugin_subagent"> =
26872749
taskTrackingMode === "cli" ? "cli" : "none";

0 commit comments

Comments
 (0)