Skip to content

Commit 5fa137e

Browse files
authored
fix(agents): keep tool recovery paths actionable (#125731)
1 parent 1bf4c1d commit 5fa137e

22 files changed

Lines changed: 276 additions & 80 deletions

‎src/agents/agent-tools-agent-config.exec.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ function requireExecTool(tools: ReturnType<typeof createOpenClawCodingTools>) {
4949
return execTool;
5050
}
5151

52+
function schemaPropertyNames(tool: ReturnType<typeof requireExecTool>): string[] {
53+
const schema = tool.parameters as { properties?: Record<string, unknown> };
54+
return Object.keys(schema.properties ?? {});
55+
}
56+
5257
const tempDirs = createTempDirTracker();
5358

5459
function createTempAgentDirs(prefix: string) {
@@ -140,6 +145,41 @@ describe("Agent-specific exec tool defaults", () => {
140145
expect(resultDetails?.status).toBe("completed");
141146
});
142147

148+
it("makes exec completion-only when the final runtime allowlist removes process", async () => {
149+
const tools = createOpenClawCodingTools({
150+
config: {
151+
tools: {
152+
exec: {
153+
host: "gateway",
154+
mode: "full",
155+
},
156+
},
157+
},
158+
runtimeToolAllowlist: ["exec"],
159+
inheritRuntimeToolAllowlist: true,
160+
sessionKey: "agent:main:main",
161+
...createTempAgentDirs("test-main-runtime-exec-only"),
162+
});
163+
const execTool = requireExecTool(tools);
164+
165+
expect.soft(tools.map((tool) => tool.name)).not.toContain("process");
166+
expect.soft(execTool.description).not.toMatch(/background|yieldMs|process/);
167+
expect.soft(schemaPropertyNames(execTool)).not.toContain("background");
168+
expect.soft(schemaPropertyNames(execTool)).not.toContain("yieldMs");
169+
170+
const result = await execTool.execute("call-runtime-exec-only", {
171+
command: `${JSON.stringify(process.execPath)} -e "setTimeout(() => {}, 250)"`,
172+
background: true,
173+
yieldMs: 10,
174+
timeoutSeconds: 0.05,
175+
});
176+
177+
expect.soft(result.details).toMatchObject({ status: "failed", timedOut: true });
178+
const text = (result.content[0] as { text?: string } | undefined)?.text ?? "";
179+
expect.soft(text).toContain("Verify the resulting state before retrying");
180+
expect.soft(text).not.toMatch(/process|background|yieldMs|poll|trailing &/i);
181+
});
182+
143183
it("routes implicit auto exec to gateway without a sandbox runtime", async () => {
144184
const tools = createOpenClawCodingTools({
145185
config: {

‎src/agents/agent-tools.deferred-followup.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ export function applyToolAvailabilityDescriptions(
1616
params?: { agentId?: string },
1717
): AnyAgentTool[] {
1818
const hasCronTool = tools.some((tool) => isAutomationsToolName(tool.name));
19+
const hasProcessTool = tools.some((tool) => tool.name === "process");
1920
const hasSessionsSpawnTool = tools.some((tool) => tool.name === "sessions_spawn");
2021
return tools.map((tool) => {
2122
if (tool.name === "exec") {
22-
return replaceDescription(tool, describeExecTool({ agentId: params?.agentId, hasCronTool }));
23+
return replaceDescription(
24+
tool,
25+
describeExecTool({ agentId: params?.agentId, hasCronTool, hasProcessTool }),
26+
);
2327
}
2428
if (tool.name === "process") {
2529
return replaceDescription(tool, describeProcessTool({ hasCronTool }));

‎src/agents/agent-tools.ts‎

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ import { resolveToolFsConfig } from "./tool-fs-policy.js";
9191
import type { PreparedSessionPermissionPolicy } from "./tool-fs-policy.js";
9292
import { resolveToolLoopDetectionConfig } from "./tool-loop-detection-config.js";
9393
import { buildDeclaredToolAllowlistContext } from "./tool-policy-declared-context.js";
94-
import { isToolAllowedByPolicies } from "./tool-policy-match.js";
9594
import { applyToolPolicyPipeline } from "./tool-policy-pipeline.js";
9695
import {
9796
expandToolGroups,
@@ -490,19 +489,6 @@ function createOpenClawCodingToolsInternal(options?: OpenClawCodingToolsOptions)
490489
sessionId: options?.sessionId,
491490
agentId,
492491
});
493-
const allowBackground = isToolAllowedByPolicies("process", [
494-
conversationToolPolicies.profilePolicy,
495-
conversationToolPolicies.providerProfilePolicy,
496-
conversationToolPolicies.globalPolicy,
497-
conversationToolPolicies.globalProviderPolicy,
498-
conversationToolPolicies.agentPolicy,
499-
conversationToolPolicies.agentProviderPolicy,
500-
conversationToolPolicies.groupPolicy,
501-
conversationToolPolicies.senderPolicy,
502-
conversationToolPolicies.sandboxPolicy,
503-
conversationToolPolicies.subagentPolicy,
504-
conversationToolPolicies.inheritedToolPolicy,
505-
]);
506492
options?.recordToolPrepStage?.("tool-policy");
507493
const execConfig = resolveExecToolConfig({ cfg: options?.config, agentId });
508494
const execRuntimeConfig = options?.exec?.config ?? options?.config;
@@ -578,6 +564,8 @@ function createOpenClawCodingToolsInternal(options?: OpenClawCodingToolsOptions)
578564
options?.recordToolPrepStage?.("workspace-policy");
579565
const { cleanupMs: cleanupMsOverride, ...execDefaults } = options?.exec ?? {};
580566
const effectiveExecPolicy = applyExecPolicyLayer(execConfig, options?.exec);
567+
const processToolAvailabilityRef: NonNullable<ExecToolDefaults["processToolAvailabilityRef"]> =
568+
{};
581569
const coreTools = createCoreCodingTools({
582570
codingRoot,
583571
containmentRoot,
@@ -618,7 +606,7 @@ function createOpenClawCodingToolsInternal(options?: OpenClawCodingToolsOptions)
618606
safeBinTrustedDirs: options?.exec?.safeBinTrustedDirs ?? execConfig.safeBinTrustedDirs,
619607
safeBinProfiles: options?.exec?.safeBinProfiles ?? execConfig.safeBinProfiles,
620608
agentId,
621-
allowBackground,
609+
processToolAvailabilityRef,
622610
scopeKey,
623611
sessionKey: options?.sessionKey,
624612
runId: options?.runId,
@@ -960,6 +948,7 @@ function createOpenClawCodingToolsInternal(options?: OpenClawCodingToolsOptions)
960948
// Collector output is a run contract, not an operator-configurable capability.
961949
authorizedTools.push(swarmStructuredOutputTool);
962950
}
951+
processToolAvailabilityRef.value = authorizedTools.some((tool) => tool.name === "process");
963952
if (shouldInheritEffectiveToolAllowlist) {
964953
// Snapshot exporter only: this copies authorizedTools for descendants and
965954
// never filters the mandatory structured_output tool from this turn.

‎src/agents/bash-tools.descriptions.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,21 @@ function deriveExecShortName(fullPath: string): string {
1919
}
2020

2121
/** Builds the model-facing exec tool description for the current platform/config. */
22-
export function describeExecTool(params?: { agentId?: string; hasCronTool?: boolean }): string {
22+
export function describeExecTool(params?: {
23+
agentId?: string;
24+
hasCronTool?: boolean;
25+
hasProcessTool?: boolean;
26+
}): string {
27+
const continuation =
28+
params?.hasProcessTool === false
29+
? ["Run shell and wait for completion."]
30+
: [
31+
"Run shell now; background continuation supported.",
32+
"Use yieldMs/background, then process for logs/status/input/intervention.",
33+
"Long run: automatic completion wake when enabled and output/failure occurs; otherwise process confirms completion.",
34+
];
2335
const base = [
24-
"Run shell now; background continuation supported.",
25-
"Use yieldMs/background, then process for logs/status/input/intervention.",
26-
"Long run: automatic completion wake when enabled and output/failure occurs; otherwise process confirms completion.",
36+
...continuation,
2737
params?.hasCronTool ? "No sleep/delay loops for reminders/follow-ups; use cron." : undefined,
2838
"TTY CLI/UI/coding agent: pty=true.",
2939
]

‎src/agents/bash-tools.exec-foreground-failures.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ describe("exec foreground failures", () => {
187187

188188
expect(result.details.status).toBe("completed");
189189
expect(requireTextContent(result)).toContain(
190-
"Warning: background execution is disabled; running synchronously.",
190+
"Warning: continuation options are unavailable; running synchronously.",
191191
);
192192
});
193193

@@ -220,6 +220,7 @@ describe("exec foreground failures", () => {
220220
expect(text).toContain("Verify the resulting state before retrying");
221221
expect(text).toContain("Do not automatically rerun non-idempotent commands");
222222
expect(text).toContain("known to be safe to retry");
223+
expect(text).not.toMatch(/process|background|yieldMs|poll|trailing &/i);
223224
expect(text).not.toContain("OOM-score wrapper");
224225
expect(text).not.toContain("OPENCLAW_CHILD_OOM_SCORE_ADJ");
225226
const details = requireFailedDetails(result.details);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ type ProcessGatewayAllowlistParams = {
127127
approvalRunningNoticeMs: number;
128128
maxOutput: number;
129129
pendingMaxOutput: number;
130+
processContinuationAvailable?: boolean;
130131
trustedSafeBinDirs?: ReadonlySet<string>;
131132
};
132133

@@ -1488,6 +1489,7 @@ export async function processGatewayAllowlist(
14881489
sentApproverDms,
14891490
unavailableReason,
14901491
allowedDecisions: approvalAllowedDecisions,
1492+
processContinuationAvailable: params.processContinuationAvailable,
14911493
}),
14921494
};
14931495
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ export async function executeNodeHostCommand(
623623
unavailableReason,
624624
allowedDecisions,
625625
nodeId: target.nodeId,
626+
processContinuationAvailable: params.processContinuationAvailable,
626627
});
627628
}
628629
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type ExecuteNodeHostCommandParams = {
4343
warnings: string[];
4444
/** Warnings that apply only when the command runs inline, never while approval is pending. */
4545
foregroundWarnings?: string[];
46+
processContinuationAvailable?: boolean;
4647
notifySessionKey?: string;
4748
notifyOnExit?: boolean;
4849
trustedSafeBinDirs?: ReadonlySet<string>;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ export function buildExecApprovalPendingToolResult(params: {
582582
unavailableReason: ExecApprovalUnavailableReason | null;
583583
allowedDecisions?: readonly ExecApprovalDecision[];
584584
nodeId?: string;
585+
processContinuationAvailable?: boolean;
585586
}): AgentToolResult<ExecToolDetails> {
586587
const allowedDecisions = params.allowedDecisions ?? resolveExecApprovalAllowedDecisions();
587588
return {
@@ -609,6 +610,7 @@ export function buildExecApprovalPendingToolResult(params: {
609610
cwd: params.cwd,
610611
host: params.host,
611612
nodeId: params.nodeId,
613+
processContinuationAvailable: params.processContinuationAvailable,
612614
}),
613615
},
614616
],

‎src/agents/bash-tools.exec-run.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ export function createExecTool(
9696
10,
9797
120_000,
9898
);
99-
const allowBackground = defaults?.allowBackground ?? true;
99+
const allowBackground =
100+
defaults?.processToolAvailabilityRef?.value ?? defaults?.allowBackground ?? true;
100101
const defaultTimeoutSec =
101102
defaults?.timeoutSec && defaults.timeoutSec > 0 ? defaults.timeoutSec : 1800;
102103
const defaultPathPrepend = normalizePathPrepend(defaults?.pathPrepend);
@@ -206,14 +207,12 @@ export function createExecTool(
206207
const startedAt = Date.now();
207208
let execCommandOverride: string | undefined;
208209
let revalidateGatewayApproval: GatewayApprovalRevalidator | undefined;
209-
const backgroundRequested = params.background === true;
210-
const yieldRequested = typeof params.yieldMs === "number";
211210
const foregroundFallbackWarning =
212-
!allowBackground && (backgroundRequested || yieldRequested)
213-
? "Warning: background execution is disabled; running synchronously."
211+
!allowBackground && (params.background === true || typeof params.yieldMs === "number")
212+
? "Warning: continuation options are unavailable; running synchronously."
214213
: undefined;
215214
const yieldWindow = allowBackground
216-
? backgroundRequested
215+
? params.background === true
217216
? 0
218217
: clampWithDefault(
219218
params.yieldMs ?? defaultBackgroundMs,
@@ -476,6 +475,7 @@ export function createExecTool(
476475
approvalRunningNoticeMs,
477476
warnings,
478477
foregroundWarnings: foregroundFallbackWarning ? [foregroundFallbackWarning] : [],
478+
processContinuationAvailable: allowBackground,
479479
notifySessionKey,
480480
notifyOnExit,
481481
trustedSafeBinDirs,
@@ -528,6 +528,7 @@ export function createExecTool(
528528
approvalRunningNoticeMs,
529529
maxOutput,
530530
pendingMaxOutput,
531+
processContinuationAvailable: allowBackground,
531532
trustedSafeBinDirs,
532533
});
533534
if (gatewayResult.pendingResult) {
@@ -549,8 +550,7 @@ export function createExecTool(
549550
warnings.push(foregroundFallbackWarning);
550551
}
551552

552-
const explicitTimeoutSec = params.timeoutSeconds ?? null;
553-
effectiveTimeout = explicitTimeoutSec ?? defaultTimeoutSec;
553+
effectiveTimeout = params.timeoutSeconds ?? defaultTimeoutSec;
554554
const usePty = params.pty === true && !sandbox;
555555

556556
// Preflight: catch a common model failure mode (shell syntax leaking into Python/JS sources)
@@ -589,6 +589,7 @@ export function createExecTool(
589589
eventRouting: defaults?.eventRouting,
590590
notifyDeliveryContext,
591591
timeoutSec: effectiveTimeout,
592+
processContinuationAvailable: allowBackground,
592593
onUpdate,
593594
onSettledBeforeNotify: (outcome) => {
594595
settledOutcome = outcome;

0 commit comments

Comments
 (0)