|
| 1 | +import { asNullableRecord } from "@openclaw/normalization-core/record-coerce"; |
| 2 | +import type { OperatorScope } from "../gateway/operator-scopes.js"; |
| 3 | +import { renderExecUpdateText } from "./bash-tools.exec-output.js"; |
| 4 | +import type { ExecToolDetails } from "./bash-tools.exec-types.js"; |
| 5 | +import type { AgentToolResult } from "./runtime/index.js"; |
| 6 | +import { callGatewayTool } from "./tools/gateway.js"; |
| 7 | + |
| 8 | +type NodeInvokeFailure = |
| 9 | + | { |
| 10 | + reason: "not-dispatched"; |
| 11 | + retrySafe: true; |
| 12 | + code: "NOT_CONNECTED"; |
| 13 | + message: string; |
| 14 | + nodeCommandDispatched: false; |
| 15 | + requestSent?: boolean; |
| 16 | + } |
| 17 | + | { |
| 18 | + reason: "outcome-unknown"; |
| 19 | + retrySafe: false; |
| 20 | + code?: string; |
| 21 | + message: string; |
| 22 | + nodeCommandDispatched?: boolean; |
| 23 | + requestSent?: boolean; |
| 24 | + }; |
| 25 | + |
| 26 | +type NodeSystemRunInvokeResult = |
| 27 | + | { ok: true; raw: unknown } |
| 28 | + | { ok: false; failure: NodeInvokeFailure }; |
| 29 | + |
| 30 | +function readString(value: unknown): string | undefined { |
| 31 | + return typeof value === "string" && value.trim() ? value.trim() : undefined; |
| 32 | +} |
| 33 | + |
| 34 | +/** Only NOT_CONNECTED plus explicit pre-dispatch provenance proves a retry cannot duplicate work. */ |
| 35 | +function classifyNodeInvokeFailure(error: unknown): NodeInvokeFailure { |
| 36 | + const errorRecord = asNullableRecord(error); |
| 37 | + const details = asNullableRecord(errorRecord?.details); |
| 38 | + const nodeError = asNullableRecord(details?.nodeError); |
| 39 | + const code = readString(nodeError?.code); |
| 40 | + const message = |
| 41 | + readString(nodeError?.message) ?? |
| 42 | + (error instanceof Error ? error.message : readString(error)) ?? |
| 43 | + "node invoke failed"; |
| 44 | + const nodeCommandDispatched = |
| 45 | + typeof details?.nodeCommandDispatched === "boolean" ? details.nodeCommandDispatched : undefined; |
| 46 | + const requestSent = |
| 47 | + typeof errorRecord?.requestSent === "boolean" ? errorRecord.requestSent : undefined; |
| 48 | + |
| 49 | + if (code === "NOT_CONNECTED" && nodeCommandDispatched === false) { |
| 50 | + return { |
| 51 | + reason: "not-dispatched", |
| 52 | + retrySafe: true, |
| 53 | + code, |
| 54 | + message, |
| 55 | + nodeCommandDispatched, |
| 56 | + ...(requestSent !== undefined ? { requestSent } : {}), |
| 57 | + }; |
| 58 | + } |
| 59 | + return { |
| 60 | + reason: "outcome-unknown", |
| 61 | + retrySafe: false, |
| 62 | + ...(code ? { code } : {}), |
| 63 | + message, |
| 64 | + ...(nodeCommandDispatched !== undefined ? { nodeCommandDispatched } : {}), |
| 65 | + ...(requestSent !== undefined ? { requestSent } : {}), |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +function formatNodeInvokeFailureText(params: { |
| 70 | + failure: NodeInvokeFailure; |
| 71 | + nodeId: string; |
| 72 | + command: string; |
| 73 | +}): string { |
| 74 | + const summary = |
| 75 | + params.failure.reason === "outcome-unknown" |
| 76 | + ? [ |
| 77 | + `Node command outcome is unknown for ${params.nodeId}.`, |
| 78 | + "The command may have executed. Do not rerun it automatically.", |
| 79 | + ] |
| 80 | + : [ |
| 81 | + `Node command was not dispatched to ${params.nodeId}.`, |
| 82 | + "It can be retried after the node reconnects.", |
| 83 | + ]; |
| 84 | + return [ |
| 85 | + ...summary, |
| 86 | + "", |
| 87 | + "Command:", |
| 88 | + params.command, |
| 89 | + "", |
| 90 | + `Details: ${params.failure.message}`, |
| 91 | + ].join("\n"); |
| 92 | +} |
| 93 | + |
| 94 | +export function formatNodeInvokeFailureFollowup(params: { |
| 95 | + failure: NodeInvokeFailure; |
| 96 | + nodeId: string; |
| 97 | + approvalId: string; |
| 98 | + command: string; |
| 99 | +}): string { |
| 100 | + const prefix = |
| 101 | + params.failure.reason === "outcome-unknown" |
| 102 | + ? `Exec outcome unknown (node=${params.nodeId} id=${params.approvalId}, outcome-unknown)` |
| 103 | + : `Exec not dispatched (node=${params.nodeId} id=${params.approvalId}, not-dispatched)`; |
| 104 | + return `${prefix}\n${formatNodeInvokeFailureText(params)}`; |
| 105 | +} |
| 106 | + |
| 107 | +export function formatNodeInvokeFailureToolResult(params: { |
| 108 | + failure: NodeInvokeFailure; |
| 109 | + nodeId: string; |
| 110 | + command: string; |
| 111 | + startedAt: number; |
| 112 | + cwd: string | undefined; |
| 113 | + warnings?: string[]; |
| 114 | +}): AgentToolResult<ExecToolDetails> { |
| 115 | + const text = formatNodeInvokeFailureText(params); |
| 116 | + return { |
| 117 | + content: [ |
| 118 | + { |
| 119 | + type: "text", |
| 120 | + text: renderExecUpdateText({ tailText: text, warnings: params.warnings ?? [] }), |
| 121 | + }, |
| 122 | + ], |
| 123 | + details: { |
| 124 | + status: "failed", |
| 125 | + exitCode: null, |
| 126 | + failureKind: params.failure.reason, |
| 127 | + reason: params.failure.reason, |
| 128 | + nodeInvokeFailure: { |
| 129 | + ...(params.failure.code ? { failureCode: params.failure.code } : {}), |
| 130 | + message: params.failure.message, |
| 131 | + ...(params.failure.nodeCommandDispatched !== undefined |
| 132 | + ? { nodeCommandDispatched: params.failure.nodeCommandDispatched } |
| 133 | + : {}), |
| 134 | + ...(params.failure.requestSent !== undefined |
| 135 | + ? { requestSent: params.failure.requestSent } |
| 136 | + : {}), |
| 137 | + }, |
| 138 | + durationMs: Date.now() - params.startedAt, |
| 139 | + aggregated: text, |
| 140 | + cwd: params.cwd, |
| 141 | + }, |
| 142 | + }; |
| 143 | +} |
| 144 | + |
| 145 | +/** Invokes node system.run and turns every non-cancellation failure into provenance-aware data. */ |
| 146 | +export async function invokeNodeSystemRun(params: { |
| 147 | + invokeWaitMs: number; |
| 148 | + invoke: Record<string, unknown>; |
| 149 | + signal?: AbortSignal; |
| 150 | + scopes?: OperatorScope[]; |
| 151 | +}): Promise<NodeSystemRunInvokeResult> { |
| 152 | + try { |
| 153 | + const callOptions = |
| 154 | + params.scopes || params.signal |
| 155 | + ? { |
| 156 | + ...(params.scopes ? { scopes: params.scopes } : {}), |
| 157 | + ...(params.signal ? { signal: params.signal } : {}), |
| 158 | + } |
| 159 | + : undefined; |
| 160 | + const raw = callOptions |
| 161 | + ? await callGatewayTool( |
| 162 | + "node.invoke", |
| 163 | + { timeoutMs: params.invokeWaitMs }, |
| 164 | + params.invoke, |
| 165 | + callOptions, |
| 166 | + ) |
| 167 | + : await callGatewayTool("node.invoke", { timeoutMs: params.invokeWaitMs }, params.invoke); |
| 168 | + if (typeof asNullableRecord(asNullableRecord(raw)?.payload)?.success !== "boolean") { |
| 169 | + return { |
| 170 | + ok: false, |
| 171 | + failure: { |
| 172 | + reason: "outcome-unknown", |
| 173 | + retrySafe: false, |
| 174 | + message: "malformed node invoke response", |
| 175 | + }, |
| 176 | + }; |
| 177 | + } |
| 178 | + return { ok: true, raw }; |
| 179 | + } catch (error) { |
| 180 | + if (params.signal?.aborted) { |
| 181 | + throw error; |
| 182 | + } |
| 183 | + return { ok: false, failure: classifyNodeInvokeFailure(error) }; |
| 184 | + } |
| 185 | +} |
0 commit comments