Files
pikasTech-unidesk/scripts/code-queue-cli-steer-test.ts
T
2026-05-20 15:01:35 +00:00

176 lines
10 KiB
TypeScript

import { spawnSync } from "node:child_process";
import { writeFileSync, unlinkSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { codexSteerTaskForTest } from "./src/code-queue";
type JsonRecord = Record<string, unknown>;
function assertCondition(condition: unknown, message: string, detail: JsonRecord = {}): void {
if (!condition) throw new Error(`${message}: ${JSON.stringify(detail)}`);
}
function runCli(args: string[], stdin?: string): { status: number | null; stdout: string; stderr: string; json: JsonRecord | null } {
const result = spawnSync("bun", ["scripts/cli.ts", ...args], {
cwd: process.cwd(),
input: stdin,
encoding: "utf8",
});
const stdout = String(result.stdout || "");
let json: JsonRecord | null = null;
try {
json = JSON.parse(stdout) as JsonRecord;
} catch {
json = null;
}
return {
status: result.status,
stdout,
stderr: String(result.stderr || ""),
json,
};
}
function nestedRecord(value: unknown, path: string[]): JsonRecord {
let current: unknown = value;
for (const key of path) {
assertCondition(current !== null && typeof current === "object" && !Array.isArray(current), "expected object while traversing JSON", { path, key, current });
current = (current as JsonRecord)[key];
}
assertCondition(current !== null && typeof current === "object" && !Array.isArray(current), "expected nested object", { path, current });
return current as JsonRecord;
}
function stringArray(value: unknown): string[] {
return Array.isArray(value) ? value.map((item) => String(item)) : [];
}
function assertDryRunPrompt(response: JsonRecord, expectedText: string): void {
assertCondition(response.ok === true, "CLI dry-run should succeed", response);
const data = nestedRecord(response.data, []);
assertCondition(data.dryRun === true, "dry-run response should expose dryRun=true", data);
const request = nestedRecord(response.data, ["request"]);
assertCondition(request.method === "POST", "dry-run should expose request method", request);
assertCondition(request.path === "/api/tasks/codex_test_task/steer", "dry-run should expose request path", request);
assertCondition(request.stableProxyPath === "/api/microservices/code-queue/proxy/api/tasks/codex_test_task/steer", "dry-run should expose stable proxy path", request);
const prompt = nestedRecord(response.data, ["request", "body", "prompt"]);
assertCondition(prompt.text === expectedText, "dry-run prompt text mismatch", prompt);
assertCondition(prompt.chars === expectedText.length, "dry-run prompt char count mismatch", prompt);
assertCondition(prompt.truncated === false, "dry-run prompt must not truncate", prompt);
const bodySummary = nestedRecord(response.data, ["request", "bodySummary"]);
assertCondition(bodySummary.promptChars === expectedText.length, "dry-run should expose body prompt char count", bodySummary);
const commands = nestedRecord(response.data, ["commands"]);
assertCondition(String(commands.rawProxy || "").includes("microservice proxy code-queue /api/tasks/codex_test_task/steer --method POST"), "dry-run should expose raw proxy equivalent", commands);
}
function assertReason(result: unknown, reason: string, status: number | null): void {
const data = nestedRecord({ data: result }, ["data"]);
assertCondition(data.ok === false, "classified steer failure should be ok=false", data);
const diagnostics = nestedRecord(data, ["diagnostics"]);
assertCondition(diagnostics.reason === reason, "unexpected steer failure reason", diagnostics);
assertCondition(diagnostics.status === status, "unexpected steer failure status", diagnostics);
assertCondition(typeof diagnostics.retryable === "boolean", "diagnostics should expose retryable boolean", diagnostics);
assertCondition(Array.isArray(diagnostics.recommendedCrossChecks), "diagnostics should expose cross-check commands", diagnostics);
}
export function runCodeQueueCliSteerContract(): JsonRecord {
const positional = runCli(["codex", "steer", "codex_test_task", "correct the running task", "--dry-run"]);
assertDryRunPrompt(positional.json ?? {}, "correct the running task");
assertCondition(String(positional.json?.command || "").includes("<prompt:redacted>"), "outer command should redact positional steer prompt", positional.json ?? {});
assertCondition(!String(positional.json?.command || "").includes("correct the running task"), "outer command must not echo positional steer prompt", positional.json ?? {});
const stdin = runCli(["codex", "steer", "codex_test_task", "--prompt-stdin", "--dry-run"], "stdin steer prompt\n");
assertDryRunPrompt(stdin.json ?? {}, "stdin steer prompt\n");
const promptFile = join(tmpdir(), `unidesk-code-queue-steer-${process.pid}.txt`);
writeFileSync(promptFile, "file steer prompt", "utf8");
try {
const fromFile = runCli(["codex", "steer", "codex_test_task", "--prompt-file", promptFile, "--dry-run"]);
assertDryRunPrompt(fromFile.json ?? {}, "file steer prompt");
} finally {
unlinkSync(promptFile);
}
const duplicateSource = runCli(["codex", "steer", "codex_test_task", "positional", "--prompt-stdin", "--dry-run"], "stdin\n");
assertCondition(duplicateSource.status !== 0, "duplicate prompt source should fail", duplicateSource.json ?? { stdout: duplicateSource.stdout });
const duplicateMessage = String(nestedRecord(duplicateSource.json, ["error"]).message || "");
assertCondition(duplicateMessage.includes("exactly one prompt source"), "duplicate prompt source error should be explicit", { duplicateMessage });
const unknownOption = runCli(["codex", "steer", "codex_test_task", "--queue", "default", "prompt", "--dry-run"]);
assertCondition(unknownOption.status !== 0, "unknown steer option should fail", unknownOption.json ?? { stdout: unknownOption.stdout });
const unknownMessage = String(nestedRecord(unknownOption.json, ["error"]).message || "");
assertCondition(unknownMessage.includes("unsupported codex steer option: --queue"), "unknown option error should name option", { unknownMessage });
const help = runCli(["codex", "help"]);
assertCondition(help.status === 0 && help.json?.ok === true, "codex help should succeed", help.json ?? { stdout: help.stdout });
const usage = stringArray(nestedRecord(help.json?.data, []).usage);
assertCondition(usage.some((line) => line.includes("codex steer <taskId>")), "codex help should list steer", { usage });
let dryRunFetchCount = 0;
const dryRunDirect = codexSteerTaskForTest("direct_task", ["do not send", "--dry-run"], () => {
dryRunFetchCount += 1;
return { ok: true, status: 200, body: { ok: true } };
});
assertCondition(dryRunFetchCount === 0, "dry-run must not call stable proxy helper", { dryRunFetchCount, dryRunDirect });
const longPrompt = `${"x".repeat(480)}-tail-secret-marker`;
const longDryRun = codexSteerTaskForTest("direct_task", [longPrompt, "--dry-run"], () => {
throw new Error("dry-run should not fetch");
}) as JsonRecord;
const longPreview = nestedRecord(longDryRun, ["request", "body", "prompt"]);
assertCondition(longPreview.truncated === true, "long dry-run prompt should be truncated", longPreview);
assertCondition(!String(longPreview.text || "").includes("tail-secret-marker"), "long dry-run must not leak prompt tail", longPreview);
let fetchPath = "";
let fetchMethod = "";
let fetchPrompt = "";
const success = codexSteerTaskForTest("direct_task", ["send this"], (path, init) => {
fetchPath = path;
fetchMethod = String(init?.method || "");
fetchPrompt = String((init?.body as JsonRecord | undefined)?.prompt || "");
return {
ok: true,
status: 200,
body: {
ok: true,
task: { id: "direct_task", status: "running", prompt: "p" },
queue: { activeTaskIds: ["direct_task"] },
},
};
}) as JsonRecord;
assertCondition(fetchPath === "/api/microservices/code-queue/proxy/api/tasks/direct_task/steer", "non-dry-run should use stable proxy path", { fetchPath });
assertCondition(fetchMethod === "POST", "non-dry-run should POST", { fetchMethod });
assertCondition(fetchPrompt === "send this", "non-dry-run should send raw prompt in body", { fetchPrompt });
assertCondition(nestedRecord(success, ["steer"]).accepted === true, "successful steer should report accepted=true", success);
assertReason(codexSteerTaskForTest("direct_task", ["p"], () => ({ ok: false, exitCode: 1, stderrTail: "Cannot connect to the Docker daemon" })), "backend-core-unreachable", null);
assertReason(codexSteerTaskForTest("direct_task", ["p"], () => ({ ok: false, status: 404, body: { ok: false, error: "microservice not found: code-queue" } })), "code-queue-microservice-unregistered", 404);
assertReason(codexSteerTaskForTest("direct_task", ["p"], () => ({ ok: false, status: 401, body: { ok: false, error: "unauthorized" } })), "proxy-unauthorized", 401);
assertReason(codexSteerTaskForTest("direct_task", ["p"], () => ({ ok: false, status: 404, body: { ok: false, error: "proxy route not found", path: "/api/microservices/code-queue/proxy/api/tasks/direct_task/steer" } })), "proxy-404", 404);
assertReason(codexSteerTaskForTest("direct_task", ["p"], () => ({ ok: false, status: 404, body: { ok: false, error: "task not found" } })), "steer-endpoint-404", 404);
assertReason(codexSteerTaskForTest("direct_task", ["p"], () => ({ ok: false, status: 409, body: { ok: false, error: "task does not have an active steerable turn" } })), "upstream-runtime-rejected", 409);
assertReason(codexSteerTaskForTest("direct_task", ["p"], () => ({ ok: false, status: 504, body: { ok: false, error: "provider HTTP tunnel timed out or disconnected", stage: "http-tunnel-wait" } })), "stable-proxy-failed", 504);
return {
ok: true,
checks: [
"steer positional dry-run",
"steer stdin dry-run",
"steer prompt-file dry-run",
"duplicate prompt source failure",
"unsupported option failure",
"codex help lists steer",
"outer command redacts positional steer prompt",
"dry-run does not call stable proxy helper",
"dry-run prompt preview is bounded",
"non-dry-run uses stable proxy helper",
"steer failure classification is JSON-consumable",
],
};
}
if (import.meta.main) {
const result = runCodeQueueCliSteerContract();
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
}