fix(orchestrator): harden provider edge cases
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
bb2bdc93ab
commit
9de147e306
9 files changed
+180
-112
No files matched your search
@@ -7,6 +7,8 @@ import * as NodeURL from "node:url";
|
||||
|
||||
import { expect, it } from "vite-plus/test";
|
||||
|
||||
import { acpReplayAgentArgs } from "../src/orchestration-v2/Adapters/AcpAdapterV2.testkit.ts";
|
||||
|
||||
const replayAgentPath = NodeURL.fileURLToPath(new URL("./acp-replay-agent.ts", import.meta.url));
|
||||
|
||||
async function runRuntimeExit(status: "success" | "error" | "cancelled") {
|
||||
@@ -16,7 +18,8 @@ async function runRuntimeExit(status: "success" | "error" | "cancelled") {
|
||||
scenario: `${status}-runtime-exit`,
|
||||
entries: [{ type: "runtime_exit", status }],
|
||||
};
|
||||
const child = NodeChildProcess.spawn(process.execPath, [replayAgentPath], {
|
||||
const args = acpReplayAgentArgs(replayAgentPath);
|
||||
const child = NodeChildProcess.spawn(process.execPath, args, {
|
||||
env: {
|
||||
...process.env,
|
||||
T3_ACP_REPLAY_STATUS_PATH: statusPath,
|
||||
@@ -42,6 +45,7 @@ async function runRuntimeExit(status: "success" | "error" | "cancelled") {
|
||||
status: JSON.parse(NodeFS.readFileSync(statusPath, "utf8")) as unknown,
|
||||
stderr,
|
||||
transcript,
|
||||
args,
|
||||
};
|
||||
} finally {
|
||||
NodeFS.rmSync(scratch, { recursive: true, force: true });
|
||||
@@ -51,6 +55,7 @@ async function runRuntimeExit(status: "success" | "error" | "cancelled") {
|
||||
it("accepts a recorded cancelled runtime exit", async () => {
|
||||
const result = await runRuntimeExit("cancelled");
|
||||
expect(result.code, result.stderr).toBe(0);
|
||||
expect(result.args).toEqual(["--experimental-strip-types", replayAgentPath]);
|
||||
expect(result.status).toEqual({
|
||||
scenario: result.transcript.scenario,
|
||||
cursor: 1,
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { assert, it } from "@effect/vitest";
|
||||
|
||||
import { codexReplayRecordingOutputRecords } from "./codexReplayRecordingRecords.ts";
|
||||
|
||||
it("preserves monotonic request ids across native Codex forks", () => {
|
||||
const records = [
|
||||
{
|
||||
type: "expect_outbound",
|
||||
label: "initialize",
|
||||
frame: { id: 1, method: "initialize" },
|
||||
},
|
||||
{
|
||||
type: "expect_outbound",
|
||||
label: "thread/fork",
|
||||
frame: { id: 4, method: "thread/fork" },
|
||||
},
|
||||
{
|
||||
type: "expect_outbound",
|
||||
label: "turn/start",
|
||||
frame: { id: 5, method: "turn/start" },
|
||||
},
|
||||
];
|
||||
|
||||
const output = codexReplayRecordingOutputRecords(records);
|
||||
|
||||
assert.deepEqual(output, records);
|
||||
assert.deepEqual(
|
||||
output.map((record) => record.label),
|
||||
["initialize", "thread/fork", "turn/start"],
|
||||
);
|
||||
assert.deepEqual(
|
||||
output.map((record) => (record.frame as { readonly id: number }).id),
|
||||
[1, 4, 5],
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Codex supports multiple provider threads in one app-server session, so native
|
||||
* fork recording keeps the request ids emitted by that single client.
|
||||
*/
|
||||
export function codexReplayRecordingOutputRecords(
|
||||
records: ReadonlyArray<Record<string, unknown>>,
|
||||
): ReadonlyArray<Record<string, unknown>> {
|
||||
return [...records];
|
||||
}
|
||||
@@ -51,6 +51,7 @@ import {
|
||||
TURN_INTERRUPT_MID_TOOL_PROMPT,
|
||||
TURN_INTERRUPT_PROMPT,
|
||||
} from "../src/orchestration-v2/testkit/fixtures/shared.ts";
|
||||
import { codexReplayRecordingOutputRecords } from "./codexReplayRecordingRecords.ts";
|
||||
import { makeReplayRecorderDeferredRegistry } from "./replayRecorderDeferredRegistry.ts";
|
||||
|
||||
const CODEX_REPLAY_PLAN_MODE_DEVELOPER_INSTRUCTIONS =
|
||||
@@ -859,10 +860,7 @@ function makeRecorder({
|
||||
records.push(record);
|
||||
});
|
||||
const flush = () => {
|
||||
const outputRecords = sessionizeForkReplayRecords({
|
||||
scenario: scenario.name,
|
||||
records,
|
||||
});
|
||||
const outputRecords = codexReplayRecordingOutputRecords(records);
|
||||
return fs.writeFileString(
|
||||
outPath,
|
||||
`${[
|
||||
@@ -889,96 +887,6 @@ function makeRecorder({
|
||||
});
|
||||
}
|
||||
|
||||
function sessionizeForkReplayRecords(input: {
|
||||
readonly scenario: ScenarioName;
|
||||
readonly records: ReadonlyArray<Record<string, unknown>>;
|
||||
}): ReadonlyArray<Record<string, unknown>> {
|
||||
if (
|
||||
input.scenario !== "thread_merge_back_continue" &&
|
||||
input.scenario !== "thread_merge_back_siblings"
|
||||
) {
|
||||
return input.records;
|
||||
}
|
||||
const initializeRequest = input.records.find(
|
||||
(record) => record.type === "expect_outbound" && record.label === "initialize",
|
||||
);
|
||||
const initializeResponse = input.records.find(
|
||||
(record) => record.type === "emit_inbound" && record.label === "initialize",
|
||||
);
|
||||
const initializedNotification = input.records.find(
|
||||
(record) => record.type === "expect_outbound" && record.label === "initialized",
|
||||
);
|
||||
if (
|
||||
initializeRequest === undefined ||
|
||||
initializeResponse === undefined ||
|
||||
initializedNotification === undefined
|
||||
) {
|
||||
throw new Error(`Scenario ${input.scenario} is missing initialization records.`);
|
||||
}
|
||||
|
||||
let nextRequestId = 1;
|
||||
let forkSessionOrdinal = 0;
|
||||
const requestIdMap = new Map<number, number>();
|
||||
const output: Array<Record<string, unknown>> = [];
|
||||
const appendRequest = (record: Record<string, unknown>, id: number) => {
|
||||
const frame = record.frame as Record<string, unknown>;
|
||||
output.push({ ...record, frame: { ...frame, id } });
|
||||
};
|
||||
const appendResponse = (record: Record<string, unknown>, id: number) => {
|
||||
const frame = record.frame as Record<string, unknown>;
|
||||
output.push({ ...record, frame: { ...frame, id } });
|
||||
};
|
||||
|
||||
for (const record of input.records) {
|
||||
const frame = record.frame;
|
||||
if (record.type === "expect_outbound" && isRecord(frame) && frame.method === "thread/fork") {
|
||||
forkSessionOrdinal += 1;
|
||||
const initializeId = nextRequestId++;
|
||||
appendRequest(
|
||||
{ ...initializeRequest, label: `initialize/fork:${forkSessionOrdinal}` },
|
||||
initializeId,
|
||||
);
|
||||
appendResponse(
|
||||
{ ...initializeResponse, label: `initialize/fork:${forkSessionOrdinal}` },
|
||||
initializeId,
|
||||
);
|
||||
output.push({
|
||||
...initializedNotification,
|
||||
label: `initialized/fork:${forkSessionOrdinal}`,
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
record.type === "expect_outbound" &&
|
||||
isRecord(frame) &&
|
||||
typeof frame.id === "number" &&
|
||||
typeof frame.method === "string"
|
||||
) {
|
||||
const requestId = nextRequestId++;
|
||||
requestIdMap.set(frame.id, requestId);
|
||||
appendRequest(record, requestId);
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
record.type === "emit_inbound" &&
|
||||
isRecord(frame) &&
|
||||
typeof frame.id === "number" &&
|
||||
!("method" in frame)
|
||||
) {
|
||||
const responseId = requestIdMap.get(frame.id);
|
||||
if (responseId === undefined) {
|
||||
throw new Error(
|
||||
`Scenario ${input.scenario} has an unmatched response id ${String(frame.id)}.`,
|
||||
);
|
||||
}
|
||||
appendResponse(record, responseId);
|
||||
continue;
|
||||
}
|
||||
output.push(record);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
function makeCodexLayer({ recorder }: { readonly recorder: Recorder }) {
|
||||
const clientRequestMethodById = new Map<string, string>();
|
||||
const serverRequestMethodById = new Map<string, string>();
|
||||
|
||||
Reference in new issue
Block a user