fix: add Code Queue steer confirmation

This commit is contained in:
Codex
2026-05-23 09:34:40 +00:00
parent fae12561e2
commit 0289118a1e
12 changed files with 702 additions and 50 deletions
+171 -5
View File
@@ -2,7 +2,7 @@ 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";
import { codexSteerTaskForTest, codexSteerTraceConfirmForTest } from "./src/code-queue";
type JsonRecord = Record<string, unknown>;
@@ -45,6 +45,10 @@ function stringArray(value: unknown): string[] {
return Array.isArray(value) ? value.map((item) => String(item)) : [];
}
function deterministicSteerId(taskId: string, prompt: string): string {
return `steer_${Bun.SHA256.hash(`unidesk-code-queue-steer:v1\0${taskId}\0${prompt}`, "hex").slice(0, 24)}`;
}
function assertDryRunPrompt(response: JsonRecord, expectedText: string): void {
assertCondition(response.ok === true, "CLI dry-run should succeed", response);
const data = nestedRecord(response.data, []);
@@ -57,10 +61,13 @@ function assertDryRunPrompt(response: JsonRecord, expectedText: string): void {
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);
assertCondition(String(request.steerId || "").startsWith("steer_"), "dry-run should expose deterministic steerId", request);
const bodySummary = nestedRecord(response.data, ["request", "bodySummary"]);
assertCondition(bodySummary.promptChars === expectedText.length, "dry-run should expose body prompt char count", bodySummary);
assertCondition(bodySummary.steerId === request.steerId, "dry-run body summary should repeat steerId", { bodySummary, request });
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);
assertCondition(String(commands.traceConfirm || "").includes(`--steer-id ${String(request.steerId)}`), "dry-run should expose trace confirmation command", commands);
}
function assertReason(result: unknown, reason: string, status: number | null): void {
@@ -124,15 +131,30 @@ export function runCodeQueueCliSteerContract(): JsonRecord {
let fetchPath = "";
let fetchMethod = "";
let fetchPrompt = "";
let fetchSteerId = "";
const success = codexSteerTaskForTest("direct_task", ["send this"], (path, init) => {
fetchPath = path;
fetchMethod = String(init?.method || "");
fetchPrompt = String((init?.body as JsonRecord | undefined)?.prompt || "");
fetchSteerId = String((init?.body as JsonRecord | undefined)?.steerId || "");
return {
ok: true,
status: 200,
body: {
ok: true,
accepted: true,
deliveryState: "accepted",
steerId: fetchSteerId,
traceConfirmation: {
taskId: "direct_task",
steerId: fetchSteerId,
found: true,
accepted: true,
deliveryState: "accepted",
matchCount: 1,
trace: { seq: 5, at: "2026-05-23T00:00:00.000Z", method: "turn/steer", steerId: fetchSteerId, promptChars: 9, promptHash: "hash", promptOmitted: true, source: "promptHistory" },
duplicateSuppressionKey: fetchSteerId,
},
task: { id: "direct_task", status: "running", prompt: "p" },
queue: { activeTaskIds: ["direct_task"] },
},
@@ -141,7 +163,12 @@ export function runCodeQueueCliSteerContract(): 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(fetchSteerId === deterministicSteerId("direct_task", "send this"), "non-dry-run should send deterministic steerId", { fetchSteerId });
assertCondition(nestedRecord(success, ["steer"]).accepted === true, "successful steer should report accepted=true", success);
assertCondition(nestedRecord(success, ["steer"]).steerId === fetchSteerId, "successful steer should report steerId", success);
assertCondition(nestedRecord(success, ["steer"]).deliveryState === "accepted", "successful steer should report delivery state", success);
assertCondition(nestedRecord(success, ["traceConfirmation"]).accepted === true, "successful steer should expose bounded trace confirmation", success);
assertCondition(String(nestedRecord(success, ["commands"]).traceConfirm || "").includes(`--steer-id ${fetchSteerId}`), "successful steer should expose trace confirmation command", success);
const successJson = JSON.stringify(success);
assertCondition(nestedRecord(success, ["steer"]).promptOmitted === true, "successful steer should mark prompt omitted", success);
assertCondition(!successJson.includes("send this"), "successful steer must not echo prompt text", success);
@@ -166,38 +193,82 @@ export function runCodeQueueCliSteerContract(): JsonRecord {
attempts: [{ attempt: 1, ok: false, durationMs: 30003, timeoutMs: 30000, result: { ok: false, error: "The operation was aborted" } }],
};
let retryCalls = 0;
const retryThenSuccess = codexSteerTaskForTest("direct_task", ["transient correction", "--retry-delay-ms", "0"], () => {
const retrySteerIds: string[] = [];
const retryThenSuccess = codexSteerTaskForTest("direct_task", ["transient correction", "--retry-delay-ms", "0"], (_path, init) => {
retryCalls += 1;
retrySteerIds.push(String((init?.body as JsonRecord | undefined)?.steerId || ""));
if (retryCalls === 1) return { ok: false, status: 502, body: abortedTunnelBody };
return {
ok: true,
status: 200,
body: {
ok: true,
accepted: true,
deliveryState: "accepted",
steerId: retrySteerIds[0],
traceConfirmation: {
taskId: "direct_task",
steerId: retrySteerIds[0],
found: true,
accepted: true,
deliveryState: "accepted",
matchCount: 1,
trace: { seq: 6, at: "2026-05-23T00:00:02.000Z", method: "turn/steer", steerId: retrySteerIds[0], promptChars: 20, promptHash: "hash2", promptOmitted: true, source: "output" },
duplicateSuppressionKey: retrySteerIds[0],
},
task: { id: "direct_task", status: "running", prompt: "hidden" },
queue: { activeTaskIds: ["direct_task"] },
},
};
}) as JsonRecord;
assertCondition(retryCalls === 2, "retryable 502 tunnel abort should be retried once by default", { retryCalls, retryThenSuccess });
assertCondition(retrySteerIds.length === 2 && retrySteerIds[0] === retrySteerIds[1] && retrySteerIds[0] === deterministicSteerId("direct_task", "transient correction"), "retry should reuse a single steerId", { retrySteerIds });
assertCondition(nestedRecord(retryThenSuccess, ["steer"]).accepted === true, "retry success should accept steer", retryThenSuccess);
assertCondition(nestedRecord(retryThenSuccess, ["steer"]).steerId === retrySteerIds[0], "retry success should report reused steerId", retryThenSuccess);
const retrySuccessAttempts = nestedRecord(retryThenSuccess, ["steer"]).attempts;
assertCondition(Array.isArray(retrySuccessAttempts) && retrySuccessAttempts.length === 2, "retry success should expose both attempts", retryThenSuccess);
assertCondition(String(JSON.stringify(retryThenSuccess)).includes("The operation was aborted"), "retry attempts should preserve aborted tunnel evidence", retryThenSuccess);
assertCondition(!String(JSON.stringify(retryThenSuccess)).includes("transient correction"), "retry success must not echo steer prompt", retryThenSuccess);
let exhaustedCalls = 0;
const exhausted = codexSteerTaskForTest("direct_task", ["final correction", "--retry-attempts", "2", "--retry-delay-ms", "0"], () => {
const exhaustedSteerIds: string[] = [];
const exhausted = codexSteerTaskForTest("direct_task", ["final correction", "--retry-attempts", "2", "--retry-delay-ms", "0"], (path, init) => {
exhaustedCalls += 1;
exhaustedSteerIds.push(String((init?.body as JsonRecord | undefined)?.steerId || ""));
if (path.includes("/steer-confirmation")) {
return {
ok: true,
status: 200,
body: {
ok: true,
confirmation: {
taskId: "direct_task",
steerId: exhaustedSteerIds[0],
found: false,
accepted: false,
deliveryState: "unknown",
matchCount: 0,
trace: null,
duplicateSuppressionKey: exhaustedSteerIds[0],
promptOmitted: true,
},
},
};
}
return { ok: false, status: 502, body: abortedTunnelBody };
}) as JsonRecord;
assertCondition(exhaustedCalls === 2, "retryable 502 tunnel abort should honor retry-attempts", { exhaustedCalls, exhausted });
assertCondition(exhaustedCalls === 3, "retryable 502 tunnel abort should honor retry-attempts and run confirmation lookup", { exhaustedCalls, exhausted });
assertCondition(exhaustedSteerIds[0] === exhaustedSteerIds[1], "exhausted retry should reuse steerId", { exhaustedSteerIds });
assertReason(exhausted, "stable-proxy-failed", 502);
assertCondition(nestedRecord(exhausted, ["steer"]).status === "unknown", "unconfirmed transport failure should report unknown status", exhausted);
assertCondition(nestedRecord(exhausted, ["steer"]).steerId === exhaustedSteerIds[0], "exhausted failure should expose steerId", exhausted);
assertCondition(nestedRecord(exhausted, ["traceConfirmation"]).deliveryState === "unknown", "exhausted failure should include trace confirmation lookup", exhausted);
const exhaustedDiagnostics = nestedRecord(exhausted, ["diagnostics"]);
const exhaustedAttempts = exhaustedDiagnostics.attempts;
assertCondition(Array.isArray(exhaustedAttempts) && exhaustedAttempts.length === 2, "exhausted retry diagnostics should expose attempts", exhaustedDiagnostics);
assertCondition(String(exhaustedDiagnostics.message || "").includes("The operation was aborted"), "diagnostics should include provider abort message", exhaustedDiagnostics);
assertCondition(nestedRecord(exhaustedDiagnostics, ["operatorGuidance"]).rawProxyEquivalentIsFallback === false, "raw proxy equivalent should be diagnostic, not fallback", exhaustedDiagnostics);
assertCondition(String(nestedRecord(exhausted, ["commands"]).traceConfirm || "").includes(`--steer-id ${exhaustedSteerIds[0]}`), "failure should expose bounded trace confirmation command", exhausted);
assertCondition(String(nestedRecord(exhausted, ["commands"]).rawProxy || "").includes("microservice proxy code-queue /api/tasks/direct_task/steer"), "failure should still expose raw proxy diagnostic command", exhausted);
assertCondition(nestedRecord(exhausted, ["steer"]).promptOmitted === true, "failed steer should omit prompt by default", exhausted);
assertCondition(!("request" in exhausted), "failed steer should omit request by default", exhausted);
@@ -218,6 +289,96 @@ export function runCodeQueueCliSteerContract(): JsonRecord {
}) as JsonRecord;
assertCondition("rawFailure" in exhaustedRaw, "--raw failed steer should expose raw response", exhaustedRaw);
const timeoutAcceptedSteerId = deterministicSteerId("direct_task", "accepted but timed out");
let timeoutAcceptedCalls = 0;
const timeoutAccepted = codexSteerTaskForTest("direct_task", ["accepted but timed out", "--retry-attempts", "1", "--retry-delay-ms", "0"], (path) => {
timeoutAcceptedCalls += 1;
if (path.includes("/steer-confirmation")) {
return {
ok: true,
status: 200,
body: {
ok: true,
confirmation: {
taskId: "direct_task",
steerId: timeoutAcceptedSteerId,
found: true,
accepted: true,
deliveryState: "accepted",
matchCount: 1,
trace: { seq: 77, at: "2026-05-23T00:00:03.000Z", method: "turn/steer", steerId: timeoutAcceptedSteerId, promptChars: 22, promptHash: "hash3", promptOmitted: true, source: "promptHistory" },
duplicateSuppressionKey: timeoutAcceptedSteerId,
promptOmitted: true,
},
},
};
}
return { ok: false, status: 502, body: abortedTunnelBody };
}) as JsonRecord;
assertCondition(timeoutAcceptedCalls === 2, "timeout accepted contract should perform one send and one confirmation lookup", { timeoutAcceptedCalls });
assertCondition(timeoutAccepted.ok === true, "trace-confirmed timeout should be treated as accepted", timeoutAccepted);
assertCondition(nestedRecord(timeoutAccepted, ["steer"]).status === "accepted_response_timeout", "trace-confirmed timeout should expose accepted_response_timeout", timeoutAccepted);
assertCondition(nestedRecord(timeoutAccepted, ["steer"]).deliveryUnconfirmed === false, "trace-confirmed timeout should not remain deliveryUnconfirmed", timeoutAccepted);
assertCondition(nestedRecord(timeoutAccepted, ["traceConfirmation"]).accepted === true, "trace-confirmed timeout should include confirmation", timeoutAccepted);
const explicitSteerId = "steer_manual_12345";
const duplicateSuppressed = codexSteerTaskForTest("direct_task", ["same prompt", "--steer-id", explicitSteerId], (_path, init) => {
const body = init?.body as JsonRecord | undefined;
assertCondition(body?.steerId === explicitSteerId, "explicit steerId should be sent unchanged", body ?? {});
return {
ok: true,
status: 200,
body: {
ok: true,
accepted: true,
duplicateSuppressed: true,
deliveryState: "accepted",
steerId: explicitSteerId,
traceConfirmation: {
taskId: "direct_task",
steerId: explicitSteerId,
found: true,
accepted: true,
deliveryState: "accepted",
matchCount: 1,
trace: { seq: 88, at: "2026-05-23T00:00:04.000Z", method: "turn/steer", steerId: explicitSteerId, promptChars: 11, promptHash: "hash4", promptOmitted: true, source: "promptHistory" },
duplicateSuppressionKey: explicitSteerId,
},
task: { id: "direct_task", status: "running", prompt: "hidden" },
queue: { activeTaskIds: ["direct_task"] },
},
};
}) as JsonRecord;
assertCondition(nestedRecord(duplicateSuppressed, ["steer"]).duplicateSuppressed === true, "duplicate suppression should be visible", duplicateSuppressed);
assertCondition(nestedRecord(duplicateSuppressed, ["steer"]).steerId === explicitSteerId, "duplicate suppression should preserve steerId", duplicateSuppressed);
const confirmLookup = codexSteerTraceConfirmForTest("direct_task", ["--steer-id", explicitSteerId], (path) => {
assertCondition(path.includes("/api/microservices/code-queue/proxy/api/tasks/direct_task/steer-confirmation"), "confirm lookup should use proxy confirmation endpoint", { path });
assertCondition(path.includes(`steerId=${encodeURIComponent(explicitSteerId)}`), "confirm lookup should include steerId query", { path });
return {
ok: true,
status: 200,
body: {
ok: true,
confirmation: {
taskId: "direct_task",
steerId: explicitSteerId,
found: true,
accepted: true,
deliveryState: "accepted",
matchCount: 1,
trace: { seq: 88, at: "2026-05-23T00:00:04.000Z", method: "turn/steer", steerId: explicitSteerId, promptChars: 11, promptHash: "hash4", promptOmitted: true, source: "promptHistory" },
duplicateSuppressionKey: explicitSteerId,
promptOmitted: true,
},
},
};
}) as JsonRecord;
assertCondition(confirmLookup.ok === true, "trace confirmation lookup should succeed when accepted", confirmLookup);
assertCondition(nestedRecord(confirmLookup, ["delivery"]).status === "accepted", "trace confirmation output should expose accepted status", confirmLookup);
assertCondition(nestedRecord(confirmLookup, ["traceConfirmation", "trace"]).seq === 88, "trace confirmation output should expose bounded trace seq", confirmLookup);
assertCondition(!JSON.stringify(confirmLookup).includes("same prompt"), "trace confirmation lookup must not echo prompt", confirmLookup);
const terminalPrompt = `${"do not leak ".repeat(40)}tail-secret-marker`;
const terminalRejection = codexSteerTaskForTest("completed_task", [terminalPrompt], () => ({
ok: false,
@@ -241,7 +402,10 @@ export function runCodeQueueCliSteerContract(): JsonRecord {
const terminalSteer = nestedRecord(terminalRejection, ["steer"]);
assertCondition(terminalRejection.ok === false, "terminal steer rejection should fail", terminalRejection);
assertCondition(terminalSteer.reason === "task-already-terminal", "terminal steer rejection should use compact terminal reason", terminalSteer);
assertCondition(terminalSteer.status === "succeeded", "terminal steer rejection should expose task status", terminalSteer);
assertCondition(terminalSteer.deliveryState === "not_accepted", "terminal steer rejection should expose not_accepted delivery state", terminalSteer);
assertCondition(String(terminalSteer.steerId || "").startsWith("steer_"), "terminal steer rejection should expose steerId", terminalSteer);
assertCondition(terminalSteer.status === "not_accepted", "terminal steer rejection should expose not_accepted status", terminalSteer);
assertCondition(terminalSteer.taskStatus === "succeeded", "terminal steer rejection should expose task status", terminalSteer);
assertCondition(terminalSteer.terminalStatus === "completed", "terminal steer rejection should expose terminal status", terminalSteer);
assertCondition(terminalSteer.lastUpdate === "2026-05-22T00:00:00.000Z", "terminal steer rejection should expose last update", terminalSteer);
assertCondition(terminalSteer.updatedAt === "2026-05-22T00:00:00.000Z", "terminal steer rejection should expose last update time", terminalSteer);
@@ -308,6 +472,8 @@ export function runCodeQueueCliSteerContract(): JsonRecord {
"successful steer confirms write without echoing prompt",
"steer failure classification is JSON-consumable",
"retryable tunnel aborts are retried with bounded diagnostics",
"retry reuses steerId and trace confirmation distinguishes accepted_response_timeout from unknown",
"duplicate suppression and trace confirmation lookup expose bounded output shape",
"terminal steer rejection is compact and actionable",
"terminal steer rejection full/raw disclosure is explicit",
],