fix: pipe AgentRun bridge stdin directly

This commit is contained in:
Codex
2026-06-10 02:10:00 +00:00
parent 0a562eb080
commit 7b869f95a0
2 changed files with 61 additions and 8 deletions
+60 -7
View File
@@ -1445,15 +1445,23 @@ interface AgentRunCliMaterializedFile {
base64: string;
}
interface AgentRunCliForwardedStdin {
flag: string;
source: string;
bytes: number;
base64: string;
}
interface PreparedAgentRunCliArgs {
args: string[];
materializedFiles: AgentRunCliMaterializedFile[];
stdinPayload: AgentRunCliForwardedStdin | null;
}
async function runOfficialAgentRunCli(config: UniDeskConfig, group: "queue" | "sessions", args: string[]): Promise<Record<string, unknown>> {
const prepared = prepareOfficialAgentRunCliArgs([group, ...args]);
const command = `agentrun v01 ${prepared.args.join(" ")}`.trim();
const bridge = agentRunQueueBridgeMetadata(prepared.materializedFiles);
const bridge = agentRunQueueBridgeMetadata(prepared.materializedFiles, prepared.stdinPayload);
const script = officialAgentRunCliScript(prepared);
const result = await capture(config, g14SourceRoute, ["script", "--", script]);
const payload = captureJsonPayload(result);
@@ -1489,6 +1497,7 @@ function prepareOfficialAgentRunCliArgs(args: string[]): PreparedAgentRunCliArgs
const materializedFiles: AgentRunCliMaterializedFile[] = [];
const prepared: string[] = [];
let stdinBuffer: Buffer | null = null;
let stdinPayload: AgentRunCliForwardedStdin | null = null;
const stdin = (): Buffer => {
if (stdinBuffer === null) stdinBuffer = readFileSync(0);
@@ -1507,6 +1516,23 @@ function prepareOfficialAgentRunCliArgs(args: string[]): PreparedAgentRunCliArgs
return remotePath;
};
const forwardStdin = (flag: string, source: string, buffer: Buffer): void => {
if (stdinPayload !== null) throw new Error(`${flag} cannot be combined with ${stdinPayload.flag}; pass one stdin payload per AgentRun CLI call`);
stdinPayload = {
flag,
source,
bytes: buffer.length,
base64: buffer.toString("base64"),
};
};
const stdinFlagForFileFlag = (flag: string): string | null => {
if (flag === "--json-file") return "--json-stdin";
if (flag === "--prompt-file") return "--prompt-stdin";
if (flag === "--runner-json-file") return "--runner-json-stdin";
return null;
};
for (let i = 0; i < args.length; i += 1) {
const arg = args[i] ?? "";
const equalsFlag = Array.from(fileFlags).find((flag) => arg.startsWith(`${flag}=`));
@@ -1514,6 +1540,13 @@ function prepareOfficialAgentRunCliArgs(args: string[]): PreparedAgentRunCliArgs
const source = arg.slice(equalsFlag.length + 1);
if (source.length === 0) throw new Error(`${equalsFlag} requires a path`);
const buffer = source === "-" ? stdin() : readFileSync(source);
if (source === "-") {
const stdinFlag = stdinFlagForFileFlag(equalsFlag);
if (!stdinFlag) throw new Error(`${equalsFlag} does not support stdin`);
forwardStdin(stdinFlag, "stdin", buffer);
prepared.push(stdinFlag);
continue;
}
prepared.push(equalsFlag, materialize(equalsFlag, source === "-" ? "stdin" : source, buffer));
continue;
}
@@ -1521,26 +1554,37 @@ function prepareOfficialAgentRunCliArgs(args: string[]): PreparedAgentRunCliArgs
const source = args[i + 1];
if (source === undefined || source.length === 0) throw new Error(`${arg} requires a path`);
const buffer = source === "-" ? stdin() : readFileSync(source);
if (source === "-") {
const stdinFlag = stdinFlagForFileFlag(arg);
if (!stdinFlag) throw new Error(`${arg} does not support stdin`);
forwardStdin(stdinFlag, "stdin", buffer);
prepared.push(stdinFlag);
i += 1;
continue;
}
prepared.push(arg, materialize(arg, source === "-" ? "stdin" : source, buffer));
i += 1;
continue;
}
if (arg === "--prompt-stdin" || arg === "--stdin") {
prepared.push("--prompt-file", materialize("--prompt-stdin", "stdin", stdin()));
forwardStdin("--prompt-stdin", "stdin", stdin());
prepared.push("--prompt-stdin");
continue;
}
if (arg === "--json-stdin") {
prepared.push("--json-file", materialize("--json-stdin", "stdin", stdin()));
forwardStdin("--json-stdin", "stdin", stdin());
prepared.push("--json-stdin");
continue;
}
if (arg === "--runner-json-stdin") {
prepared.push("--runner-json-file", materialize("--runner-json-stdin", "stdin", stdin()));
forwardStdin("--runner-json-stdin", "stdin", stdin());
prepared.push("--runner-json-stdin");
continue;
}
prepared.push(arg);
}
return { args: prepared, materializedFiles };
return { args: prepared, materializedFiles, stdinPayload };
}
function officialAgentRunCliScript(prepared: PreparedAgentRunCliArgs): string {
@@ -1552,11 +1596,15 @@ function officialAgentRunCliScript(prepared: PreparedAgentRunCliArgs): string {
"trap 'rm -rf \"$tmp_dir\"' EXIT",
...prepared.materializedFiles.map((file) => `printf %s ${shQuote(file.base64)} | base64 -d > ${shQuote(file.remotePath)}`),
];
const cliCommand = `./scripts/agentrun --manager-url auto ${prepared.args.map(shQuote).join(" ")}`;
const invocation = prepared.stdinPayload
? `printf %s ${shQuote(prepared.stdinPayload.base64)} | base64 -d | ${cliCommand}`
: cliCommand;
return [
"set -euo pipefail",
...setup,
"cd /root/agentrun-v01",
`./scripts/agentrun --manager-url auto ${prepared.args.map(shQuote).join(" ")}`,
invocation,
].join("\n");
}
@@ -1565,7 +1613,7 @@ function parentDir(pathValue: string): string {
return index > 0 ? pathValue.slice(0, index) : ".";
}
function agentRunQueueBridgeMetadata(materializedFiles: AgentRunCliMaterializedFile[]): Record<string, unknown> {
function agentRunQueueBridgeMetadata(materializedFiles: AgentRunCliMaterializedFile[], stdinPayload: AgentRunCliForwardedStdin | null): Record<string, unknown> {
return {
route: g14SourceRoute,
sourceWorktree: "/root/agentrun-v01",
@@ -1574,6 +1622,11 @@ function agentRunQueueBridgeMetadata(materializedFiles: AgentRunCliMaterializedF
officialCli: "./scripts/agentrun",
mode: "direct-official-cli",
compatibility: "no-code-queue-adapter-no-double-write",
stdinForwarded: stdinPayload ? {
flag: stdinPayload.flag,
source: stdinPayload.source,
bytes: stdinPayload.bytes,
} : null,
fileFlagsMaterialized: materializedFiles.map((file) => ({
flag: file.flag,
source: file.source,