⛽ feat: Negotiate Background Command Timeouts (#16100)

* ⏳ feat: Negotiate BYOM Background Command Timeouts

* ⏱️ fix: Preserve Foreground Programmatic Timeout
This commit is contained in:
Danny Avila 2026-09-19 15:01:41 -04:00 • committed by GitHub
parent 61d034c20f
commit 13c74c9574
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 147 additions and 12 deletions

View file

@ -21,6 +21,7 @@ import {
WORKSPACE_COMMAND_MAX_TIMEOUT_MS,
WORKSPACE_QUEUE_MAX_WAIT_MS,
} from './workspace';
import { BACKGROUND_TOOL_INVOCATION_CONFIG_KEY } from '~/agents/invocation';
const DEFAULT_OUTPUT_BYTES = 256 * 1024;
@ -74,7 +75,7 @@ function buildAttachedTimeoutSchema(maxTimeoutMs: number): BoundedTimeoutSchema
type: 'integer',
minimum: 1,
maximum: maxTimeoutMs,
description: `Optional execution timeout in milliseconds, from 1 through ${maxTimeoutMs}. Defaults to ${defaultTimeoutMs}. Waiting for an available worker does not consume this execution budget.`,
description: `Optional execution timeout in milliseconds, from 1 through ${maxTimeoutMs}. Defaults to ${defaultTimeoutMs} for foreground calls and ${maxTimeoutMs} for detached background calls. Waiting for an available worker does not consume this execution budget.`,
};
}
@ -87,11 +88,41 @@ function normalizeAttachedWorkspaceCommandTimeoutMax(maxTimeoutMs: number): numb
export function resolveAttachedWorkspaceCommandTimeoutMax(
configSchema?: CodeEnvironmentUserConfigSchema,
upstreamMaxTimeoutMs?: number,
): number {
const configured = configSchema?.limits?.maxCommandTimeoutMs;
return configured == null
? WORKSPACE_COMMAND_DEFAULT_TIMEOUT_MS
: normalizeAttachedWorkspaceCommandTimeoutMax(configured);
const upstream =
upstreamMaxTimeoutMs == null
? WORKSPACE_COMMAND_MAX_TIMEOUT_MS
: normalizeAttachedWorkspaceCommandTimeoutMax(upstreamMaxTimeoutMs);
let requested = WORKSPACE_COMMAND_DEFAULT_TIMEOUT_MS;
if (configured != null) {
requested = normalizeAttachedWorkspaceCommandTimeoutMax(configured);
} else if (upstreamMaxTimeoutMs != null) {
requested = upstream;
}
return Math.min(requested, upstream);
}
/**
* Programmatic calls do not currently carry the detached-invocation marker.
* Preserve their historical foreground default while still enforcing both an
* explicit administrator override and the live upstream ceiling.
*/
export function resolveAttachedWorkspaceProgrammaticTimeout(
configSchema?: CodeEnvironmentUserConfigSchema,
upstreamMaxTimeoutMs?: number,
): number {
const configured = configSchema?.limits?.maxCommandTimeoutMs;
const requested =
configured == null
? WORKSPACE_COMMAND_DEFAULT_TIMEOUT_MS
: normalizeAttachedWorkspaceCommandTimeoutMax(configured);
const upstream =
upstreamMaxTimeoutMs == null
? WORKSPACE_COMMAND_MAX_TIMEOUT_MS
: normalizeAttachedWorkspaceCommandTimeoutMax(upstreamMaxTimeoutMs);
return Math.min(requested, upstream);
}
/**
@ -207,8 +238,9 @@ export function createContextProgrammaticBashTool(
...(attached
? {
workspaceId: context.codeWorkspace?.workspaceId,
runTimeoutMs: resolveAttachedWorkspaceCommandTimeoutMax(
runTimeoutMs: resolveAttachedWorkspaceProgrammaticTimeout(
context.codeEnvironmentConfigSchema,
context.codeWorkspace?.maxCommandTimeoutMs,
),
}
: {}),
@ -259,7 +291,7 @@ export function createAttachedWorkspaceBashTool({
workspaceId: string;
environment?: CodeWorkspaceDescriptor['environment'];
gitIdentity?: AgentGitIdentity | null;
/** Deployment ceiling already intersected with the protocol hard cap. */
/** Effective admin/upstream ceiling already intersected with the protocol hard cap. */
maxTimeoutMs?: number;
/** Deployment admission budget; omitted keeps the built-in default. */
maxQueueWaitMs?: number;
@ -305,7 +337,10 @@ export function createAttachedWorkspaceBashTool({
action ??
commandWithGitIdentity(commandWithArguments(rawInput.command!, rawInput.args), gitIdentity);
const timeoutMs =
rawInput.timeoutMs ?? Math.min(WORKSPACE_COMMAND_DEFAULT_TIMEOUT_MS, effectiveMaxTimeoutMs);
rawInput.timeoutMs ??
(config?.configurable?.[BACKGROUND_TOOL_INVOCATION_CONFIG_KEY] === true
? effectiveMaxTimeoutMs
: Math.min(WORKSPACE_COMMAND_DEFAULT_TIMEOUT_MS, effectiveMaxTimeoutMs));
const signal = config?.signal;
const trace = {
runId: config?.metadata?.run_id,