File size: 13,268 Bytes
e249c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import crypto from "node:crypto";
import type { invokeNodeClaudeCliRun } from "../../gateway/node-agent-cli-runtime.js";
import { prepareNodeClaudeSkillRuntime } from "../../gateway/node-claude-skill-runtime.js";
import { createAbortError } from "../../infra/abort-signal.js";
import type { ExecAsk, ExecSecurity, SystemRunApprovalPlan } from "../../infra/exec-approvals.js";
import type { RunExit } from "../../process/supervisor/types.js";
import type {
  registerExecApprovalRequestForHostOrThrow,
  resolveRegisteredExecApprovalDecision,
} from "../bash-tools.exec-approval-request.js";
import { createCliRunCurrentAssertion } from "./execution-target.js";
import type { NodeClaudePlacement, PreparedCliRunContext } from "./types.js";

const NODE_CLI_MAX_TIMEOUT_MS = 24 * 60 * 60 * 1000;
const NODE_CLI_MAX_IDLE_TIMEOUT_MS = 30 * 60 * 1000;

const NODE_CLI_OMIT_BARE_ARGS = new Set([
  "--strict-mcp-config",
  // The Gateway's version probe says nothing about the paired node's Claude binary.
  // Omit the optimization instead of making an older node reject every turn.
  "--exclude-dynamic-system-prompt-sections",
]);
const NODE_CLI_OMIT_VALUE_ARGS = new Set([
  "--permission-mode",
  "--plugin-dir",
  "--plugin-dir-no-mcp",
]);
// --tools and --disallowedTools stay: they carry the gateway's native tool
// policy onto the node. --allowedTools is stripped because Claude treats it as
// auto-approval, which must never cross the node's own approval boundary.
const NODE_CLI_OMIT_VARIADIC_ARGS = new Set(["--mcp-config", "--allowedTools", "--allowed-tools"]);

/** Remove Gateway-local file, plugin, MCP, and allow-list arguments. */
export function stripGatewayLocalClaudeArgs(args: readonly string[]): string[] {
  const result: string[] = [];
  for (let index = 0; index < args.length; index += 1) {
    const arg = args[index] ?? "";
    const equalsIndex = arg.indexOf("=");
    const name = equalsIndex > 0 ? arg.slice(0, equalsIndex) : arg;
    if (NODE_CLI_OMIT_BARE_ARGS.has(name)) {
      continue;
    }
    if (NODE_CLI_OMIT_VALUE_ARGS.has(name)) {
      if (equalsIndex < 0) {
        index += 1;
      }
      continue;
    }
    if (NODE_CLI_OMIT_VARIADIC_ARGS.has(name)) {
      if (equalsIndex < 0) {
        while (typeof args[index + 1] === "string" && !args[index + 1]?.startsWith("-")) {
          index += 1;
        }
      }
      continue;
    }
    result.push(arg);
  }
  return result;
}

function parseNodeClaudeResultPayload(result: { payload?: unknown; payloadJSON?: string | null }): {
  exitCode: number;
  stderrTail: string;
  truncated: boolean;
  timeoutKind?: "hard" | "idle";
} {
  const value = result.payloadJSON ? (JSON.parse(result.payloadJSON) as unknown) : result.payload;
  if (!value || typeof value !== "object" || Array.isArray(value)) {
    throw new Error("paired node returned an invalid Claude CLI result");
  }
  const record = value as Record<string, unknown>;
  if (
    !Number.isInteger(record.exitCode) ||
    typeof record.stderrTail !== "string" ||
    typeof record.truncated !== "boolean" ||
    (record.timeoutKind !== undefined &&
      record.timeoutKind !== "hard" &&
      record.timeoutKind !== "idle")
  ) {
    throw new Error("paired node returned an invalid Claude CLI result");
  }
  return {
    exitCode: record.exitCode as number,
    stderrTail: record.stderrTail,
    truncated: record.truncated,
    ...(record.timeoutKind ? { timeoutKind: record.timeoutKind as "hard" | "idle" } : {}),
  };
}

type NodeClaudeApprovalRequired = {
  systemRunPlan: SystemRunApprovalPlan;
  security: ExecSecurity;
  ask: ExecAsk;
};

function parseNodeClaudeApprovalRequired(result: {
  ok: boolean;
  payload?: unknown;
  payloadJSON?: string | null;
}): NodeClaudeApprovalRequired | null {
  if (!result.ok) {
    return null;
  }
  const value = result.payloadJSON ? (JSON.parse(result.payloadJSON) as unknown) : result.payload;
  if (!value || typeof value !== "object" || Array.isArray(value)) {
    return null;
  }
  const record = value as Record<string, unknown>;
  if (
    record.approvalRequired !== true ||
    !record.systemRunPlan ||
    typeof record.systemRunPlan !== "object" ||
    Array.isArray(record.systemRunPlan) ||
    (record.security !== "deny" && record.security !== "allowlist" && record.security !== "full") ||
    (record.ask !== "off" && record.ask !== "on-miss" && record.ask !== "always")
  ) {
    return null;
  }
  return {
    systemRunPlan: record.systemRunPlan as SystemRunApprovalPlan,
    security: record.security,
    ask: record.ask,
  };
}

export function createCliAbortError(): Error {
  return createAbortError("CLI run aborted");
}

async function waitForNodeOperation<T>(params: {
  operation: Promise<T>;
  signal?: AbortSignal;
}): Promise<T> {
  if (!params.signal) {
    return await params.operation;
  }
  if (params.signal.aborted) {
    throw createCliAbortError();
  }
  return await new Promise<T>((resolve, reject) => {
    const onAbort = () => reject(createCliAbortError());
    params.signal?.addEventListener("abort", onAbort, { once: true });
    void params.operation.then(resolve, reject).finally(() => {
      params.signal?.removeEventListener("abort", onAbort);
    });
  });
}

type ExecuteNodeClaudeRunDeps = {
  invokeNodeClaudeCliRun: typeof invokeNodeClaudeCliRun;
  registerExecApprovalRequestForHostOrThrow: typeof registerExecApprovalRequestForHostOrThrow;
  resolveRegisteredExecApprovalDecision: typeof resolveRegisteredExecApprovalDecision;
};

export async function executeNodeClaudeRun(params: {
  context: PreparedCliRunContext;
  nodePlacement: NodeClaudePlacement;
  executionArgs: string[];
  stdinPayload: string;
  nodeSystemPrompt?: string;
  nodeEnv?: Record<string, string>;
  nodeClearEnv?: string[];
  noOutputTimeoutMs: number;
  consumeStdout: (chunk: string) => void;
  consumeStderr: (chunk: string) => void;
  deps: ExecuteNodeClaudeRunDeps;
}): Promise<{
  result: RunExit;
  nodeRunAbortSignal: AbortSignal;
  nodeRunTruncated: boolean;
}> {
  const contextParams = params.context.params;
  const startedAt = Date.now();
  const hardTimeoutMs = Math.min(contextParams.timeoutMs, NODE_CLI_MAX_TIMEOUT_MS);
  const hardDeadlineAt = startedAt + hardTimeoutMs;
  const nodeAbortController = new AbortController();
  const nodeRunAbortSignal = nodeAbortController.signal;
  const assertCurrent = createCliRunCurrentAssertion(contextParams, nodeRunAbortSignal);
  let hardDeadlineReached = false;
  const hardDeadlineTimer = setTimeout(() => {
    hardDeadlineReached = true;
    nodeAbortController.abort();
  }, hardTimeoutMs);
  const abortNodeRun = () => nodeAbortController.abort();
  contextParams.abortSignal?.addEventListener("abort", abortNodeRun, { once: true });
  if (contextParams.abortSignal?.aborted) {
    abortNodeRun();
  }
  const replyBackendHandle = contextParams.replyOperation
    ? {
        kind: "cli" as const,
        runId: contextParams.runId,
        toolAuthorityFingerprint: contextParams.toolAuthorityFingerprint,
        cancel: abortNodeRun,
      }
    : undefined;
  if (replyBackendHandle) {
    contextParams.replyOperation?.attachBackend(replyBackendHandle);
  }
  let nodeResult: Awaited<ReturnType<typeof invokeNodeClaudeCliRun>>;
  let skillRuntime: Awaited<ReturnType<typeof prepareNodeClaudeSkillRuntime>>;
  try {
    assertCurrent();
    skillRuntime = await prepareNodeClaudeSkillRuntime(params.context, nodeRunAbortSignal);
    const invokeNode = async (approval?: {
      decision: "allow-once" | "allow-always";
      plan: SystemRunApprovalPlan;
    }) => {
      const remainingTimeoutMs = hardDeadlineAt - Date.now();
      if (remainingTimeoutMs <= 0) {
        hardDeadlineReached = true;
        nodeAbortController.abort();
        return {
          ok: false,
          error: {
            code: "TIMEOUT",
            message: "paired-node Claude CLI invocation exceeded its hard timeout",
          },
        };
      }
      assertCurrent();
      return await params.deps.invokeNodeClaudeCliRun({
        assertCurrent,
        nodeId: params.nodePlacement.nodeId,
        argv: params.executionArgs,
        stdin: params.stdinPayload,
        ...(params.nodePlacement.cwd ? { cwd: params.nodePlacement.cwd } : {}),
        ...(params.nodeSystemPrompt !== undefined ? { systemPrompt: params.nodeSystemPrompt } : {}),
        ...(params.nodeEnv ? { env: params.nodeEnv } : {}),
        ...(params.nodeClearEnv ? { clearEnv: params.nodeClearEnv } : {}),
        ...(contextParams.agentId ? { agentId: contextParams.agentId } : {}),
        ...(contextParams.sessionKey ? { sessionKey: contextParams.sessionKey } : {}),
        ...(approval
          ? {
              approvalDecision: approval.decision,
              systemRunPlan: approval.plan,
            }
          : {}),
        timeoutMs: remainingTimeoutMs,
        idleTimeoutMs: Math.max(
          1_000,
          Math.min(params.noOutputTimeoutMs, NODE_CLI_MAX_IDLE_TIMEOUT_MS),
        ),
        onProgress: params.consumeStdout,
        signal: skillRuntime?.signal ?? nodeAbortController.signal,
        ...(skillRuntime ? { skillRuntime } : {}),
      });
    };
    nodeResult = await invokeNode();
    const approval = parseNodeClaudeApprovalRequired(nodeResult);
    if (approval) {
      skillRuntime?.assertCurrent();
      const approvalId = crypto.randomUUID();
      const registration = await waitForNodeOperation({
        operation: params.deps.registerExecApprovalRequestForHostOrThrow({
          approvalId,
          command: approval.systemRunPlan.commandText,
          commandArgv: approval.systemRunPlan.argv,
          systemRunPlan: approval.systemRunPlan,
          workdir: approval.systemRunPlan.cwd ?? undefined,
          host: "node",
          nodeId: params.nodePlacement.nodeId,
          security: approval.security,
          ask: approval.ask,
          unavailableDecisions: ["allow-always"],
          agentId: contextParams.agentId,
          sessionKey: contextParams.sessionKey,
          ...(contextParams.approvalReviewerDeviceId
            ? { approvalReviewerDeviceIds: [contextParams.approvalReviewerDeviceId] }
            : {}),
        }),
        signal: skillRuntime?.signal ?? nodeAbortController.signal,
      });
      const decision = await waitForNodeOperation({
        operation: params.deps.resolveRegisteredExecApprovalDecision({
          approvalId: registration.id,
          preResolvedDecision: registration.finalDecision,
        }),
        signal: skillRuntime?.signal ?? nodeAbortController.signal,
      });
      if (decision === "allow-once" || decision === "allow-always") {
        nodeResult = await invokeNode({ decision, plan: approval.systemRunPlan });
      } else {
        nodeResult = {
          ok: false,
          error: {
            code: "PERMISSION_DENIED",
            message: "paired-node Claude CLI agent run was not approved",
          },
        };
      }
    }
  } catch (error) {
    if (!hardDeadlineReached) {
      throw error;
    }
    nodeResult = {
      ok: false,
      error: {
        code: "TIMEOUT",
        message: "paired-node Claude CLI invocation exceeded its hard timeout",
      },
    };
  } finally {
    if (skillRuntime?.signal.aborted) {
      nodeAbortController.abort();
    }
    skillRuntime?.close();
    clearTimeout(hardDeadlineTimer);
    if (replyBackendHandle) {
      contextParams.replyOperation?.detachBackend(replyBackendHandle);
    }
    contextParams.abortSignal?.removeEventListener("abort", abortNodeRun);
  }
  if (hardDeadlineReached) {
    nodeResult = {
      ok: false,
      error: {
        code: "TIMEOUT",
        message: "paired-node Claude CLI invocation exceeded its hard timeout",
      },
    };
  }
  if (!nodeResult.ok) {
    const code = nodeResult.error?.code;
    const timedOut = code === "TIMEOUT" || code === "IDLE_TIMEOUT";
    const result: RunExit = {
      reason:
        code === "IDLE_TIMEOUT"
          ? "no-output-timeout"
          : code === "TIMEOUT"
            ? "overall-timeout"
            : code === "ABORTED"
              ? "manual-cancel"
              : "exit",
      exitCode: timedOut || code === "ABORTED" ? null : 1,
      exitSignal: null,
      durationMs: Date.now() - startedAt,
      stdout: "",
      stderr: nodeResult.error?.message ?? "paired-node Claude CLI invocation failed",
      timedOut,
      noOutputTimedOut: code === "IDLE_TIMEOUT",
    };
    params.consumeStderr(result.stderr);
    return { result, nodeRunAbortSignal, nodeRunTruncated: false };
  }
  const payload = parseNodeClaudeResultPayload(nodeResult);
  if (payload.stderrTail) {
    params.consumeStderr(payload.stderrTail);
  }
  const result: RunExit = {
    reason:
      payload.timeoutKind === "idle"
        ? "no-output-timeout"
        : payload.timeoutKind === "hard"
          ? "overall-timeout"
          : "exit",
    exitCode: payload.timeoutKind ? null : payload.exitCode,
    exitSignal: null,
    durationMs: Date.now() - startedAt,
    stdout: "",
    stderr: payload.stderrTail,
    timedOut: payload.timeoutKind !== undefined,
    noOutputTimedOut: payload.timeoutKind === "idle",
  };
  return { result, nodeRunAbortSignal, nodeRunTruncated: payload.truncated };
}