Skip to content

Commit 809a734

Browse files
omarshahineCopilot
andcommitted
fix(exec): arm the gateway node invoke deadline from exec host=node
The gateway deadline machinery from #115248 reads a top-level timeoutMs on the node.invoke payload, but buildNodeSystemRunInvoke() only ever emitted the nested program budget. With p.timeoutMs undefined the registry falls back to a fixed 30s pending-invoke timer, drops the pending entry, and discards the node's later success as ignored, so the caller never sees a terminal outcome. Serialize the resolved invocation deadline as the top-level timeoutMs and keep three ordered budgets: node program runtime < gateway deadline < caller wait. A zero timeout still means "no program-runtime timer" and is never rewritten, while the gateway deadline and caller wait stay positive and finite so neither side can hang forever. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 8ea5a828-596c-4bf9-9382-0081bd0d50cd
1 parent fd343ca commit 809a734

5 files changed

Lines changed: 109 additions & 27 deletions

‎src/agents/bash-tools.exec-host-node-phases.cancellation.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ function createDirectNodeRun(signal?: AbortSignal): DirectNodeRun {
2929
nodeId: "node-1",
3030
argv: ["tool", "--version"],
3131
env: undefined,
32-
invokeTimeoutMs: 30_000,
32+
invokeDeadlineMs: 30_000,
33+
invokeWaitMs: 35_000,
3334
runTimeoutSec: 30,
3435
supportsSystemRunPrepare: true,
3536
},
@@ -48,7 +49,7 @@ describe("direct node run cancellation", () => {
4849

4950
expect(callGatewayTool).toHaveBeenCalledWith(
5051
"node.invoke",
51-
{ timeoutMs: 30_000 },
52+
{ timeoutMs: 35_000 },
5253
expect.objectContaining({ command: "system.run" }),
5354
{ signal: controller.signal },
5455
);
@@ -59,7 +60,7 @@ describe("direct node run cancellation", () => {
5960

6061
expect(callGatewayTool).toHaveBeenCalledWith(
6162
"node.invoke",
62-
{ timeoutMs: 30_000 },
63+
{ timeoutMs: 35_000 },
6364
expect.objectContaining({ command: "system.run" }),
6465
);
6566
});

‎src/agents/bash-tools.exec-host-node-phases.ts‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ type NodeExecutionTarget = {
4949
platform?: string | null;
5050
argv: string[];
5151
env: Record<string, string> | undefined;
52-
invokeTimeoutMs: number;
52+
invokeDeadlineMs: number;
53+
invokeWaitMs: number;
5354
runTimeoutSec: number;
5455
supportsSystemRunPrepare: boolean;
5556
};
@@ -88,7 +89,10 @@ function resolveNodeRunTimeoutSec(
8889
: defaultTimeoutSec;
8990
}
9091

91-
function resolveNodeInvokeTimeoutMs(runTimeoutSec: number, defaultTimeoutSec: number): number {
92+
// Gateway invocation deadline: the node program budget plus transport grace. A
93+
// `timeout: 0` run keeps no program timer, so the deadline falls back to the
94+
// default budget instead of becoming unbounded.
95+
function resolveNodeInvokeDeadlineMs(runTimeoutSec: number, defaultTimeoutSec: number): number {
9296
const baseTimeoutSec =
9397
Number.isFinite(runTimeoutSec) && runTimeoutSec > 0 ? runTimeoutSec : defaultTimeoutSec;
9498
if (!Number.isFinite(baseTimeoutSec) || baseTimeoutSec <= 0) {
@@ -97,6 +101,13 @@ function resolveNodeInvokeTimeoutMs(runTimeoutSec: number, defaultTimeoutSec: nu
97101
return Math.max(10_000, addSafeTimeoutDelayGraceMs(baseTimeoutSec * 1000, 5_000));
98102
}
99103

104+
// Caller wait must outlast the Gateway deadline so the deadline expiry answer
105+
// wins the race instead of the caller giving up first. Both saturate together at
106+
// MAX_SAFE_TIMEOUT_DELAY_MS, where the ordering degenerates by design.
107+
function resolveNodeInvokeWaitMs(invokeDeadlineMs: number): number {
108+
return addSafeTimeoutDelayGraceMs(invokeDeadlineMs, 5_000);
109+
}
110+
100111
function resolveNodeRunTimeoutMs(runTimeoutSec: number): number {
101112
return Number.isFinite(runTimeoutSec) && runTimeoutSec > 0
102113
? addSafeTimeoutDelayGraceMs(runTimeoutSec * 1000, 0, { minMs: 0 })
@@ -327,12 +338,14 @@ export async function resolveNodeExecutionTarget(
327338
}
328339

329340
const runTimeoutSec = resolveNodeRunTimeoutSec(params.timeoutSec, params.defaultTimeoutSec);
341+
const invokeDeadlineMs = resolveNodeInvokeDeadlineMs(runTimeoutSec, params.defaultTimeoutSec);
330342
return {
331343
nodeId,
332344
platform: nodeInfo?.platform,
333345
argv: buildNodeShellCommand(params.command, nodeInfo?.platform),
334346
env: params.requestedEnv ? { ...params.requestedEnv } : undefined,
335-
invokeTimeoutMs: resolveNodeInvokeTimeoutMs(runTimeoutSec, params.defaultTimeoutSec),
347+
invokeDeadlineMs,
348+
invokeWaitMs: resolveNodeInvokeWaitMs(invokeDeadlineMs),
336349
runTimeoutSec,
337350
supportsSystemRunPrepare: declaredCommands.includes("system.run.prepare"),
338351
};
@@ -363,6 +376,10 @@ export function buildNodeSystemRunInvoke(params: {
363376
return {
364377
nodeId: params.target.nodeId,
365378
command: "system.run",
379+
// Top-level timeout arms the Gateway invocation deadline; the nested value is
380+
// the node program timer. Without this the Gateway falls back to a fixed 30s
381+
// pending-invoke timer and discards a later node result as `ignored`.
382+
timeoutMs: params.target.invokeDeadlineMs,
366383
params: {
367384
command: params.command,
368385
rawCommand: params.rawCommand,
@@ -408,10 +425,10 @@ export async function invokeNodeSystemRunDirect(params: {
408425
});
409426
params.request.signal?.throwIfAborted();
410427
const raw = params.request.signal
411-
? await callGatewayTool("node.invoke", { timeoutMs: params.target.invokeTimeoutMs }, invoke, {
428+
? await callGatewayTool("node.invoke", { timeoutMs: params.target.invokeWaitMs }, invoke, {
412429
signal: params.request.signal,
413430
})
414-
: await callGatewayTool("node.invoke", { timeoutMs: params.target.invokeTimeoutMs }, invoke);
431+
: await callGatewayTool("node.invoke", { timeoutMs: params.target.invokeWaitMs }, invoke);
415432
return formatNodeRunToolResult({
416433
raw,
417434
startedAt,

‎src/agents/bash-tools.exec-host-node.cancellation.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ vi.mock("./bash-tools.exec-host-node-phases.js", () => ({
7272
resolveNodeExecutionTarget: vi.fn(async () => ({
7373
nodeId: "node-1",
7474
argv: ["tool", "--version"],
75-
invokeTimeoutMs: 30_000,
75+
invokeDeadlineMs: 30_000,
76+
invokeWaitMs: 35_000,
7677
supportsSystemRunPrepare: true,
7778
})),
7879
shouldSkipNodeApprovalPrepare: vi.fn(
@@ -223,7 +224,7 @@ describe("node-host dispatch cancellation", () => {
223224

224225
expect(mocks.callGatewayTool).toHaveBeenCalledWith(
225226
"node.invoke",
226-
{ timeoutMs: 30_000 },
227+
{ timeoutMs: 35_000 },
227228
expect.objectContaining({ command: "system.run" }),
228229
{ scopes: ["operator.write", "operator.approvals"], signal: controller.signal },
229230
);
@@ -243,7 +244,7 @@ describe("node-host dispatch cancellation", () => {
243244

244245
expect(mocks.callGatewayTool).toHaveBeenCalledWith(
245246
"node.invoke",
246-
{ timeoutMs: 30_000 },
247+
{ timeoutMs: 35_000 },
247248
expect.objectContaining({ command: "system.run" }),
248249
{ signal: controller.signal },
249250
);

‎src/agents/bash-tools.exec-host-node.test.ts‎

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ function createNodeHostRequest(
355355

356356
type MockNodeInvokeParams = {
357357
command?: string;
358+
timeoutMs?: number;
358359
params?: Record<string, unknown>;
359360
};
360361

@@ -417,10 +418,21 @@ function requireRegisteredApprovalRequest(): Record<string, unknown> {
417418
return firstCall[0];
418419
}
419420

420-
function expectSystemRunInvoke(params: { invokeTimeoutMs: number; runTimeoutMs: number }) {
421+
function expectSystemRunInvoke(params: {
422+
invokeDeadlineMs: number;
423+
invokeWaitMs: number;
424+
runTimeoutMs: number;
425+
}) {
421426
const call = requireGatewayCommand("system.run");
422-
expect(call.options.timeoutMs).toBe(params.invokeTimeoutMs);
427+
// Three ordered budgets: node program runtime < Gateway invocation deadline <
428+
// caller wait, so the Gateway deadline answer wins over a caller giving up.
423429
expect(requireRunParams(call).timeoutMs).toBe(params.runTimeoutMs);
430+
expect(call.params?.timeoutMs).toBe(params.invokeDeadlineMs);
431+
expect(call.options.timeoutMs).toBe(params.invokeWaitMs);
432+
expect(params.runTimeoutMs).toBeLessThanOrEqual(params.invokeDeadlineMs);
433+
expect(params.invokeDeadlineMs).toBeLessThanOrEqual(params.invokeWaitMs);
434+
expect(params.invokeDeadlineMs).toBeGreaterThan(0);
435+
expect(Number.isFinite(params.invokeWaitMs)).toBe(true);
424436
}
425437

426438
function mockGatewayInvokesWithNodeApprovals(file: Record<string, unknown>) {
@@ -952,7 +964,8 @@ describe("executeNodeHostCommand", () => {
952964
});
953965

954966
const call = requireGatewayCall(2);
955-
expect(call.options.timeoutMs).toBe(35_000);
967+
expect(call.options.timeoutMs).toBe(40_000);
968+
expect(call.params?.timeoutMs).toBe(35_000);
956969
expect(call.callOptions).toEqual({ scopes: ["operator.write", "operator.approvals"] });
957970
const runParams = requireRunParams(call);
958971
expect(runParams.approved).toBe(true);
@@ -1867,7 +1880,7 @@ describe("executeNodeHostCommand", () => {
18671880
expect(resolveExecApprovalsFromFileMock).toHaveBeenCalledWith(
18681881
expect.objectContaining({ agentId: "prepared-agent" }),
18691882
);
1870-
expectSystemRunInvoke({ invokeTimeoutMs: 35_000, runTimeoutMs: 30_000 });
1883+
expectSystemRunInvoke({ invokeDeadlineMs: 35_000, invokeWaitMs: 40_000, runTimeoutMs: 30_000 });
18711884
});
18721885

18731886
it("does not let transport wrapper allowlist matches approve shell payloads", async () => {
@@ -2036,7 +2049,7 @@ describe("executeNodeHostCommand", () => {
20362049

20372050
expect(result.details?.status).toBe("completed");
20382051
expect(createAndRegisterDefaultExecApprovalRequestMock).not.toHaveBeenCalled();
2039-
expectSystemRunInvoke({ invokeTimeoutMs: 35_000, runTimeoutMs: 30_000 });
2052+
expectSystemRunInvoke({ invokeDeadlineMs: 35_000, invokeWaitMs: 40_000, runTimeoutMs: 30_000 });
20402053
});
20412054

20422055
it.each(["bash", "sh", "/bin/sh"])(
@@ -2252,7 +2265,7 @@ describe("executeNodeHostCommand", () => {
22522265

22532266
expect(result.details?.status).toBe("completed");
22542267
expect(createAndRegisterDefaultExecApprovalRequestMock).not.toHaveBeenCalled();
2255-
expectSystemRunInvoke({ invokeTimeoutMs: 35_000, runTimeoutMs: 30_000 });
2268+
expectSystemRunInvoke({ invokeDeadlineMs: 35_000, invokeWaitMs: 40_000, runTimeoutMs: 30_000 });
22562269
});
22572270

22582271
it("requests human approval when node auto-review asks on an approval miss", async () => {
@@ -3562,7 +3575,8 @@ describe("executeNodeHostCommand", () => {
35623575

35633576
expect(callGatewayToolMock).toHaveBeenCalledTimes(1);
35643577
const call = requireGatewayCall(0);
3565-
expect(call.options.timeoutMs).toBe(35_000);
3578+
expect(call.options.timeoutMs).toBe(40_000);
3579+
expect(call.params?.timeoutMs).toBe(35_000);
35663580
const runParams = requireRunParams(call);
35673581
expect(runParams.command).toEqual(["/bin/sh", "-lc", "bun ./script.ts"]);
35683582
expect(runParams.rawCommand).toBe("bun ./script.ts");
@@ -3676,7 +3690,7 @@ describe("executeNodeHostCommand", () => {
36763690
}),
36773691
);
36783692

3679-
expectSystemRunInvoke({ invokeTimeoutMs: 17_000, runTimeoutMs: 12_000 });
3693+
expectSystemRunInvoke({ invokeDeadlineMs: 17_000, invokeWaitMs: 22_000, runTimeoutMs: 12_000 });
36803694
});
36813695

36823696
it("normalizes unsafe explicit timeouts before invoking node system.run", async () => {
@@ -3686,7 +3700,7 @@ describe("executeNodeHostCommand", () => {
36863700
}),
36873701
);
36883702

3689-
expectSystemRunInvoke({ invokeTimeoutMs: 35_000, runTimeoutMs: 30_000 });
3703+
expectSystemRunInvoke({ invokeDeadlineMs: 35_000, invokeWaitMs: 40_000, runTimeoutMs: 30_000 });
36903704

36913705
callGatewayToolMock.mockClear();
36923706

@@ -3697,7 +3711,8 @@ describe("executeNodeHostCommand", () => {
36973711
);
36983712

36993713
expectSystemRunInvoke({
3700-
invokeTimeoutMs: MAX_SAFE_TIMEOUT_DELAY_MS,
3714+
invokeDeadlineMs: MAX_SAFE_TIMEOUT_DELAY_MS,
3715+
invokeWaitMs: MAX_SAFE_TIMEOUT_DELAY_MS,
37013716
runTimeoutMs: MAX_SAFE_TIMEOUT_DELAY_MS,
37023717
});
37033718

@@ -3710,7 +3725,8 @@ describe("executeNodeHostCommand", () => {
37103725
);
37113726

37123727
expectSystemRunInvoke({
3713-
invokeTimeoutMs: MAX_SAFE_TIMEOUT_DELAY_MS,
3728+
invokeDeadlineMs: MAX_SAFE_TIMEOUT_DELAY_MS,
3729+
invokeWaitMs: MAX_SAFE_TIMEOUT_DELAY_MS,
37143730
runTimeoutMs: MAX_SAFE_TIMEOUT_DELAY_MS,
37153731
});
37163732
});
@@ -3722,7 +3738,54 @@ describe("executeNodeHostCommand", () => {
37223738
}),
37233739
);
37243740

3725-
expectSystemRunInvoke({ invokeTimeoutMs: 35_000, runTimeoutMs: 0 });
3741+
expectSystemRunInvoke({ invokeDeadlineMs: 35_000, invokeWaitMs: 40_000, runTimeoutMs: 0 });
3742+
const call = requireGatewayCommand("system.run");
3743+
// Zero means "no program-runtime timer" on the node and must never be
3744+
// rewritten into a finite process timeout, but the Gateway deadline and the
3745+
// caller wait still have to stay positive and finite.
3746+
expect(requireRunParams(call).timeoutMs).toBe(0);
3747+
expect(call.params?.timeoutMs).toBeGreaterThan(0);
3748+
expect(Number.isFinite(call.params?.timeoutMs)).toBe(true);
3749+
});
3750+
3751+
it("arms the gateway invocation deadline for a budget longer than the 30s registry fallback", async () => {
3752+
await executeNodeHostCommand(
3753+
createNodeHostRequest({
3754+
timeoutSec: 120,
3755+
}),
3756+
);
3757+
3758+
// A 120s program budget must not be cut short by the registry's fixed 30s
3759+
// pending-invoke fallback in resolveTimerTimeoutMs(params.timeoutMs, 30_000, 0).
3760+
expectSystemRunInvoke({
3761+
invokeDeadlineMs: 125_000,
3762+
invokeWaitMs: 130_000,
3763+
runTimeoutMs: 120_000,
3764+
});
3765+
});
3766+
3767+
it("leaves system.run.prepare without a gateway invocation deadline", async () => {
3768+
mockGatewayInvokesWithNodeApprovals({ version: 1, agents: {} });
3769+
resolveExecHostApprovalContextMock.mockReturnValue({
3770+
approvals: { allowlist: [], file: { version: 1, agents: {} } },
3771+
hostSecurity: "allowlist",
3772+
hostAsk: "on-miss",
3773+
askFallback: "deny",
3774+
});
3775+
3776+
await executeNodeHostCommand(
3777+
createNodeHostRequest({
3778+
security: "allowlist",
3779+
ask: "on-miss",
3780+
timeoutSec: 120,
3781+
}),
3782+
).catch(() => undefined);
3783+
3784+
// The short prepare round-trip keeps the registry default; only the actual
3785+
// system.run dispatch carries the command budget.
3786+
const prepare = requireGatewayCommand("system.run.prepare");
3787+
expect(prepare.params?.timeoutMs).toBeUndefined();
3788+
expect(prepare.options.timeoutMs).toBe(15_000);
37263789
});
37273790

37283791
it("allows exec when requestedNode is display name matching boundNode's device", async () => {

‎src/agents/bash-tools.exec-host-node.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ export async function executeNodeHostCommand(
570570
nodeInvocationStarted = true;
571571
const raw = await callGatewayTool(
572572
"node.invoke",
573-
{ timeoutMs: target.invokeTimeoutMs },
573+
{ timeoutMs: target.invokeWaitMs },
574574
buildNodeSystemRunInvoke({
575575
target,
576576
command: prepared.argv,
@@ -693,15 +693,15 @@ export async function executeNodeHostCommand(
693693
params.signal?.throwIfAborted();
694694
const raw =
695695
(inlineApprovedByAsk || inlineApprovalSource) && inlineApprovalId
696-
? await callGatewayTool("node.invoke", { timeoutMs: target.invokeTimeoutMs }, invoke, {
696+
? await callGatewayTool("node.invoke", { timeoutMs: target.invokeWaitMs }, invoke, {
697697
scopes: APPROVED_NODE_INVOKE_SCOPES,
698698
...(params.signal ? { signal: params.signal } : {}),
699699
})
700700
: params.signal
701-
? await callGatewayTool("node.invoke", { timeoutMs: target.invokeTimeoutMs }, invoke, {
701+
? await callGatewayTool("node.invoke", { timeoutMs: target.invokeWaitMs }, invoke, {
702702
signal: params.signal,
703703
})
704-
: await callGatewayTool("node.invoke", { timeoutMs: target.invokeTimeoutMs }, invoke);
704+
: await callGatewayTool("node.invoke", { timeoutMs: target.invokeWaitMs }, invoke);
705705
return formatNodeRunToolResult({
706706
raw,
707707
startedAt,

0 commit comments

Comments
 (0)