@parcha/agentrun-harness-codex 0.0.0-stage → 0.1.0-beta.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +58 -0
- package/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +72 -2
- package/dist/adapter.d.ts +59 -0
- package/dist/adapter.js +276 -0
- package/dist/app-server.d.ts +53 -0
- package/dist/app-server.js +105 -0
- package/dist/binary.d.ts +31 -0
- package/dist/binary.js +74 -0
- package/dist/code-mode.d.ts +90 -0
- package/dist/code-mode.js +204 -0
- package/dist/config.d.ts +81 -0
- package/dist/config.js +208 -0
- package/dist/harness.d.ts +200 -0
- package/dist/harness.js +1100 -0
- package/dist/host/kernel.d.ts +20 -0
- package/dist/host/kernel.js +3 -0
- package/dist/host/machine.d.ts +58 -0
- package/dist/host/machine.js +284 -0
- package/dist/host/policy.d.ts +60 -0
- package/dist/host/policy.js +478 -0
- package/dist/host/process.d.ts +145 -0
- package/dist/host/process.js +28 -0
- package/dist/host/sandbox.d.ts +35 -0
- package/dist/host/sandbox.js +34 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +6 -0
- package/dist/rollout.d.ts +102 -0
- package/dist/rollout.js +185 -0
- package/dist/schema.d.ts +14 -0
- package/dist/schema.js +178 -0
- package/dist/scripts.d.ts +94 -0
- package/dist/scripts.js +162 -0
- package/dist/verify.d.ts +37 -0
- package/dist/verify.js +67 -0
- package/dist/versions.d.ts +33 -0
- package/dist/versions.js +57 -0
- package/package.json +68 -4
- package/vendor/durable-harness/UPSTREAM.json +39 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { DriveContext } from "./kernel.js";
|
|
2
|
+
import type { AcquireMachine, GatewayHandler, SessionMachine } from "./process.js";
|
|
3
|
+
/** Acquire the session's machine for this drive, with the model route its proxy serves. */
|
|
4
|
+
export declare function attachInDrive(ctx: DriveContext, acquire: AcquireMachine, options?: {
|
|
5
|
+
model?: GatewayHandler;
|
|
6
|
+
deadline?: number;
|
|
7
|
+
}): Promise<SessionMachine>;
|
|
8
|
+
/** A local machine shows the model no notes, so there is nothing to acknowledge. */
|
|
9
|
+
export declare function acknowledgeInDrive(_ctx: DriveContext, _key?: string): Promise<void>;
|
|
10
|
+
/** The pinned Codex binary, as the harness names it. */
|
|
11
|
+
export interface PinnedCli {
|
|
12
|
+
dir: string;
|
|
13
|
+
bin: string;
|
|
14
|
+
}
|
|
15
|
+
export declare const CODEX_CLI: PinnedCli;
|
|
16
|
+
/** Make a harness (`make`, which reads the binary once, synchronously) that runs `path`, the binary the adapter checked
|
|
17
|
+
* against the pins when it was made. CODEX_BIN is read once, there, so a session never runs a binary that was not checked. */
|
|
18
|
+
export declare function withCheckedBinary<T>(path: string, make: () => T): T;
|
|
19
|
+
/** The binary to run: the checked one (`withCheckedBinary`). A harness made any other way runs nothing unchecked. */
|
|
20
|
+
export declare const cliPath: (_cli: PinnedCli) => string;
|
|
21
|
+
/** A sandbox's Files manifest. A local session's files are its workspace, which the host shows itself. */
|
|
22
|
+
export declare const SANDBOX_DOC: unique symbol;
|
|
23
|
+
export declare class DriveFiles {
|
|
24
|
+
constructor(_ctx: DriveContext, _machine: SessionMachine, _options?: {
|
|
25
|
+
driveId?: string;
|
|
26
|
+
log?: unknown;
|
|
27
|
+
});
|
|
28
|
+
changed(_call: {
|
|
29
|
+
tool: string;
|
|
30
|
+
callId: string;
|
|
31
|
+
paths?: readonly string[];
|
|
32
|
+
}): void;
|
|
33
|
+
catchUp(): Promise<void>;
|
|
34
|
+
snapshot(_reason?: string): Promise<void>;
|
|
35
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** Acquire the session's machine for this drive, with the model route its proxy serves. */
|
|
2
|
+
export async function attachInDrive(ctx, acquire, options = {}) {
|
|
3
|
+
return acquire({ ctx, ...(options.model ? { model: options.model } : {}), ...(options.deadline === undefined ? {} : { deadline: options.deadline }) });
|
|
4
|
+
}
|
|
5
|
+
/** A local machine shows the model no notes, so there is nothing to acknowledge. */
|
|
6
|
+
export async function acknowledgeInDrive(_ctx, _key) { }
|
|
7
|
+
export const CODEX_CLI = Object.freeze({ dir: "", bin: "codex" });
|
|
8
|
+
/** The binary the harness being made runs: the one the adapter checked against the pins when it was made. */
|
|
9
|
+
let checked;
|
|
10
|
+
/** Make a harness (`make`, which reads the binary once, synchronously) that runs `path`, the binary the adapter checked
|
|
11
|
+
* against the pins when it was made. CODEX_BIN is read once, there, so a session never runs a binary that was not checked. */
|
|
12
|
+
export function withCheckedBinary(path, make) {
|
|
13
|
+
checked = path;
|
|
14
|
+
try {
|
|
15
|
+
return make();
|
|
16
|
+
}
|
|
17
|
+
finally {
|
|
18
|
+
checked = undefined;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** The binary to run: the checked one (`withCheckedBinary`). A harness made any other way runs nothing unchecked. */
|
|
22
|
+
export const cliPath = (_cli) => {
|
|
23
|
+
if (!checked)
|
|
24
|
+
throw new Error("agentrun-harness-codex: the Codex harness is made through the adapter, which checks its binary against the pins first");
|
|
25
|
+
return checked;
|
|
26
|
+
};
|
|
27
|
+
/** A sandbox's Files manifest. A local session's files are its workspace, which the host shows itself. */
|
|
28
|
+
export const SANDBOX_DOC = Symbol("agentrun.codex.no-sandbox-doc");
|
|
29
|
+
export class DriveFiles {
|
|
30
|
+
constructor(_ctx, _machine, _options = {}) { }
|
|
31
|
+
changed(_call) { }
|
|
32
|
+
async catchUp() { }
|
|
33
|
+
async snapshot(_reason) { }
|
|
34
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { CODEX_TOOL_OUTPUT_TOKENS, codexAdapter } from "./adapter.js";
|
|
2
|
+
/** The check the adapter runs on the host's skills when it is made: @parcha/agentrun-kernel' own, as the Claude adapter's. */
|
|
3
|
+
export { skillProblems } from "@parcha/agentrun-kernel";
|
|
4
|
+
export type { HostSkill } from "@parcha/agentrun-kernel";
|
|
5
|
+
export type { CodexAdapterOptions, CodexProvider, CodexWebSearch } from "./adapter.js";
|
|
6
|
+
export type { ChildCommand, ConfineChild } from "./host/machine.js";
|
|
7
|
+
export { CODEX_VERSION, codexBinary } from "./binary.js";
|
|
8
|
+
export { verifyCodex, verifyCodexBinary } from "./verify.js";
|
|
9
|
+
export { CODEX_BINARY_SHA256, CODEX_INSTALL, CODEX_NPM_INTEGRITY, CodexNotInstalled, CodexPinMismatch } from "./versions.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { CODEX_TOOL_OUTPUT_TOKENS, codexAdapter } from "./adapter.js";
|
|
2
|
+
/** The check the adapter runs on the host's skills when it is made: @parcha/agentrun-kernel' own, as the Claude adapter's. */
|
|
3
|
+
export { skillProblems } from "@parcha/agentrun-kernel";
|
|
4
|
+
export { CODEX_VERSION, codexBinary } from "./binary.js";
|
|
5
|
+
export { verifyCodex, verifyCodexBinary } from "./verify.js";
|
|
6
|
+
export { CODEX_BINARY_SHA256, CODEX_INSTALL, CODEX_NPM_INTEGRITY, CodexNotInstalled, CodexPinMismatch } from "./versions.js";
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codex's rollout, as the run's log holds it. Pure functions, no Node
|
|
3
|
+
* imports.
|
|
4
|
+
*
|
|
5
|
+
* A Codex thread persists as one append-only JSONL file, the rollout:
|
|
6
|
+
* `$CODEX_HOME/sessions/YYYY/MM/DD/rollout-<ts>-<threadId>.jsonl`, one
|
|
7
|
+
* `{timestamp, type, payload}` object per line. Its `response_item` lines are
|
|
8
|
+
* the model-visible history (messages, `function_call`,
|
|
9
|
+
* `function_call_output`, reasoning); its `event_msg` lines mark turns
|
|
10
|
+
* (`task_started`, `task_complete`, `turn_aborted`) and carry the
|
|
11
|
+
* `client_id` of every user message. Copied alone into an empty
|
|
12
|
+
* `CODEX_HOME`, it is enough to resume the thread (`thread/resume` by id).
|
|
13
|
+
*
|
|
14
|
+
* The harness mirrors it into the session's native journal
|
|
15
|
+
* (the process layer), one record per line, on the stream
|
|
16
|
+
* `rollout:<threadId>`; the `codex.thread` stream records which thread is
|
|
17
|
+
* the session's and where its file goes. Restoring is writing those lines
|
|
18
|
+
* back, byte for byte, into a wiped `CODEX_HOME`.
|
|
19
|
+
*/
|
|
20
|
+
/** The journal stream naming the session's thread: `{ threadId, path }` records, the last one wins. */
|
|
21
|
+
export declare const THREAD_STREAM = "codex.thread";
|
|
22
|
+
/** The journal stream of a thread's rollout lines. */
|
|
23
|
+
export declare const rolloutStream: (threadId: string) => string;
|
|
24
|
+
/** A `codex.thread` record. */
|
|
25
|
+
export interface ThreadRecord {
|
|
26
|
+
threadId: string;
|
|
27
|
+
/** The rollout file, relative to `CODEX_HOME` (`sessions/…/rollout-…-<threadId>.jsonl`). */
|
|
28
|
+
path: string;
|
|
29
|
+
}
|
|
30
|
+
/** One rollout line, parsed. */
|
|
31
|
+
export interface RolloutLine {
|
|
32
|
+
timestamp?: string;
|
|
33
|
+
type: string;
|
|
34
|
+
payload?: Record<string, unknown> & {
|
|
35
|
+
type?: string;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** A model tool call in the rollout, with its output if the rollout has one. */
|
|
39
|
+
export interface RolloutCall {
|
|
40
|
+
callId: string;
|
|
41
|
+
name: string;
|
|
42
|
+
/** `function_call` (a model tool, or a host tool) or `custom_tool_call` (code mode's `exec`, Codex's `apply_patch`). */
|
|
43
|
+
type: string;
|
|
44
|
+
arguments: string;
|
|
45
|
+
/** Whether a `function_call_output` for it follows. */
|
|
46
|
+
answered: boolean;
|
|
47
|
+
/** Line index of the `function_call`. */
|
|
48
|
+
line: number;
|
|
49
|
+
}
|
|
50
|
+
/** What the harness reads off a rollout. */
|
|
51
|
+
export interface RolloutSummary {
|
|
52
|
+
/** The thread id `session_meta` names, if the first line is one. */
|
|
53
|
+
threadId?: string;
|
|
54
|
+
/** Every `function_call` of the model, in order. */
|
|
55
|
+
calls: RolloutCall[];
|
|
56
|
+
/** Calls with no output: what a crash left dangling. */
|
|
57
|
+
dangling: RolloutCall[];
|
|
58
|
+
/** The `client_id` of every user message (the harness sends submission ids). */
|
|
59
|
+
clientIds: Set<string>;
|
|
60
|
+
/** Turns started (`task_started`) and ended (`task_complete` or `turn_aborted`). */
|
|
61
|
+
turnsStarted: number;
|
|
62
|
+
turnsEnded: number;
|
|
63
|
+
/** The last turn has ended (or none started). */
|
|
64
|
+
idle: boolean;
|
|
65
|
+
/** How the last turn that ended ended: `task_complete` or `turn_aborted`. */
|
|
66
|
+
lastTurn?: 'completed' | 'aborted';
|
|
67
|
+
/** The `client_id`s of the user messages in the last turn that started. */
|
|
68
|
+
lastTurnClientIds: Set<string>;
|
|
69
|
+
/** The last `task_complete`'s `last_agent_message`, if the last turn completed. */
|
|
70
|
+
lastAgentMessage?: string;
|
|
71
|
+
}
|
|
72
|
+
export declare function parseLine(line: string): RolloutLine | undefined;
|
|
73
|
+
/** Whether a line is the rollout's `session_meta` (its first line: without it there is no thread to resume). */
|
|
74
|
+
export declare function isSessionMeta(line: string | undefined): boolean;
|
|
75
|
+
export declare function summarizeRollout(lines: readonly string[]): RolloutSummary;
|
|
76
|
+
/** The call id of a `function_call` or `custom_tool_call` line, if the line is one. */
|
|
77
|
+
export declare function callIdOf(line: string): string | undefined;
|
|
78
|
+
/** Whether the rollout has a `function_call` line for this call. */
|
|
79
|
+
export declare function hasCall(lines: readonly string[], callId: string): boolean;
|
|
80
|
+
/** Whether a line ends a turn (`task_complete` or `turn_aborted`). */
|
|
81
|
+
export declare function endsTurn(line: string): boolean;
|
|
82
|
+
/** The file a set of mirrored lines restores to: each line and its newline. */
|
|
83
|
+
export declare function rolloutText(lines: readonly string[]): string;
|
|
84
|
+
/**
|
|
85
|
+
* The rollout path relative to `CODEX_HOME`, from the absolute path
|
|
86
|
+
* app-server reports. Codex resumes a thread only from a file with its
|
|
87
|
+
* canonical name, `rollout-<YYYY-MM-DDTHH-MM-SS>-<threadId>.jsonl` (any
|
|
88
|
+
* directory under `sessions/`, any timestamp): anything else is "no rollout
|
|
89
|
+
* found" or "not a canonical rollout filename".
|
|
90
|
+
*/
|
|
91
|
+
export declare function relativeRolloutPath(absolute: string, threadId: string): string;
|
|
92
|
+
/** Whether `rel` is a rollout path the driver itself would derive for `threadId`: `sessions/[YYYY/MM/DD/]rollout-<ts>-<threadId>.jsonl`, nothing else. */
|
|
93
|
+
export declare function isRolloutPath(rel: string, threadId: string): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* The rollout path relative to `CODEX_HOME`, from the absolute path a VM's
|
|
96
|
+
* app-server reports (docs/workspaces.md §5.6): the driver derives the path
|
|
97
|
+
* it tails itself and refuses anything else. `homes` are what `CODEX_HOME`
|
|
98
|
+
* may be written as (the path the harness set, and the one app-server's
|
|
99
|
+
* `initialize` says, which may be its canonical form). Throws for a path
|
|
100
|
+
* outside them or not of the form `isRolloutPath` accepts.
|
|
101
|
+
*/
|
|
102
|
+
export declare function rolloutPathIn(homes: readonly string[], absolute: string, threadId: string): string;
|
package/dist/rollout.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// Copyright 2026 Vercel Inc. Licensed under the Apache License, Version 2.0; see LICENSE in this package.
|
|
2
|
+
// From github.com/vercel-labs/durable-harness, packages/harness-codex/src/rollout.ts at ea8aa4060c (UPSTREAM.json records its blob and sha256).
|
|
3
|
+
// Changed by Parcha, patches: 004-names, 006-comments, 009-rollout-call-type
|
|
4
|
+
/**
|
|
5
|
+
* Codex's rollout, as the run's log holds it. Pure functions, no Node
|
|
6
|
+
* imports.
|
|
7
|
+
*
|
|
8
|
+
* A Codex thread persists as one append-only JSONL file, the rollout:
|
|
9
|
+
* `$CODEX_HOME/sessions/YYYY/MM/DD/rollout-<ts>-<threadId>.jsonl`, one
|
|
10
|
+
* `{timestamp, type, payload}` object per line. Its `response_item` lines are
|
|
11
|
+
* the model-visible history (messages, `function_call`,
|
|
12
|
+
* `function_call_output`, reasoning); its `event_msg` lines mark turns
|
|
13
|
+
* (`task_started`, `task_complete`, `turn_aborted`) and carry the
|
|
14
|
+
* `client_id` of every user message. Copied alone into an empty
|
|
15
|
+
* `CODEX_HOME`, it is enough to resume the thread (`thread/resume` by id).
|
|
16
|
+
*
|
|
17
|
+
* The harness mirrors it into the session's native journal
|
|
18
|
+
* (the process layer), one record per line, on the stream
|
|
19
|
+
* `rollout:<threadId>`; the `codex.thread` stream records which thread is
|
|
20
|
+
* the session's and where its file goes. Restoring is writing those lines
|
|
21
|
+
* back, byte for byte, into a wiped `CODEX_HOME`.
|
|
22
|
+
*/
|
|
23
|
+
/** The journal stream naming the session's thread: `{ threadId, path }` records, the last one wins. */
|
|
24
|
+
export const THREAD_STREAM = 'codex.thread';
|
|
25
|
+
/** The journal stream of a thread's rollout lines. */
|
|
26
|
+
export const rolloutStream = (threadId) => `rollout:${threadId}`;
|
|
27
|
+
export function parseLine(line) {
|
|
28
|
+
try {
|
|
29
|
+
const value = JSON.parse(line);
|
|
30
|
+
return value && typeof value === 'object' && typeof value.type === 'string' ? value : undefined;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Whether a line is the rollout's `session_meta` (its first line: without it there is no thread to resume). */
|
|
37
|
+
export function isSessionMeta(line) {
|
|
38
|
+
return !!line && parseLine(line)?.type === 'session_meta';
|
|
39
|
+
}
|
|
40
|
+
export function summarizeRollout(lines) {
|
|
41
|
+
const calls = [];
|
|
42
|
+
const outputs = new Set();
|
|
43
|
+
const clientIds = new Set();
|
|
44
|
+
let threadId;
|
|
45
|
+
let started = 0;
|
|
46
|
+
let ended = 0;
|
|
47
|
+
let lastTurn;
|
|
48
|
+
let lastTurnClientIds = new Set();
|
|
49
|
+
let lastAgentMessage;
|
|
50
|
+
lines.forEach((raw, i) => {
|
|
51
|
+
const line = parseLine(raw);
|
|
52
|
+
if (!line)
|
|
53
|
+
return;
|
|
54
|
+
const p = line.payload ?? {};
|
|
55
|
+
if (line.type === 'session_meta' && i === 0) {
|
|
56
|
+
const id = p.id ?? p.meta?.id;
|
|
57
|
+
if (typeof id === 'string')
|
|
58
|
+
threadId = id;
|
|
59
|
+
}
|
|
60
|
+
else if (line.type === 'response_item') {
|
|
61
|
+
if ((p.type === 'function_call' || p.type === 'custom_tool_call') && typeof p.call_id === 'string') {
|
|
62
|
+
calls.push({ callId: p.call_id, name: String(p.name ?? ''), type: p.type, arguments: String(p.arguments ?? p.input ?? ''), answered: false, line: i });
|
|
63
|
+
}
|
|
64
|
+
else if ((p.type === 'function_call_output' || p.type === 'custom_tool_call_output') && typeof p.call_id === 'string') {
|
|
65
|
+
outputs.add(p.call_id);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else if (line.type === 'event_msg') {
|
|
69
|
+
if (p.type === 'task_started') {
|
|
70
|
+
started++;
|
|
71
|
+
lastTurnClientIds = new Set();
|
|
72
|
+
lastAgentMessage = undefined;
|
|
73
|
+
}
|
|
74
|
+
else if (p.type === 'task_complete') {
|
|
75
|
+
ended++;
|
|
76
|
+
lastTurn = 'completed';
|
|
77
|
+
lastAgentMessage = typeof p.last_agent_message === 'string' ? p.last_agent_message : undefined;
|
|
78
|
+
}
|
|
79
|
+
else if (p.type === 'turn_aborted') {
|
|
80
|
+
ended++;
|
|
81
|
+
lastTurn = 'aborted';
|
|
82
|
+
lastAgentMessage = undefined;
|
|
83
|
+
}
|
|
84
|
+
else if (p.type === 'item_completed') {
|
|
85
|
+
const item = p.item;
|
|
86
|
+
if (item?.type === 'UserMessage' && typeof item.client_id === 'string') {
|
|
87
|
+
clientIds.add(item.client_id);
|
|
88
|
+
lastTurnClientIds.add(item.client_id);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
for (const c of calls)
|
|
94
|
+
c.answered = outputs.has(c.callId);
|
|
95
|
+
return {
|
|
96
|
+
...(threadId ? { threadId } : {}),
|
|
97
|
+
calls,
|
|
98
|
+
dangling: calls.filter((c) => !c.answered),
|
|
99
|
+
clientIds,
|
|
100
|
+
turnsStarted: started,
|
|
101
|
+
turnsEnded: ended,
|
|
102
|
+
idle: ended >= started,
|
|
103
|
+
...(lastTurn ? { lastTurn } : {}),
|
|
104
|
+
lastTurnClientIds,
|
|
105
|
+
...(ended >= started && lastAgentMessage !== undefined ? { lastAgentMessage } : {}),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/** The call id of a `function_call` or `custom_tool_call` line, if the line is one. */
|
|
109
|
+
export function callIdOf(line) {
|
|
110
|
+
if (!line.includes('call_id') || (!line.includes('function_call') && !line.includes('custom_tool_call')))
|
|
111
|
+
return undefined;
|
|
112
|
+
const l = parseLine(line);
|
|
113
|
+
const p = l?.payload;
|
|
114
|
+
return l?.type === 'response_item' && p && (p.type === 'function_call' || p.type === 'custom_tool_call') && typeof p.call_id === 'string' ? p.call_id : undefined;
|
|
115
|
+
}
|
|
116
|
+
/** Whether the rollout has a `function_call` line for this call. */
|
|
117
|
+
export function hasCall(lines, callId) {
|
|
118
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
119
|
+
const l = lines[i];
|
|
120
|
+
if (!l.includes(callId))
|
|
121
|
+
continue;
|
|
122
|
+
const p = parseLine(l)?.payload;
|
|
123
|
+
if (p && (p.type === 'function_call' || p.type === 'custom_tool_call') && p.call_id === callId)
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
/** Whether a line ends a turn (`task_complete` or `turn_aborted`). */
|
|
129
|
+
export function endsTurn(line) {
|
|
130
|
+
if (!line.includes('task_complete') && !line.includes('turn_aborted'))
|
|
131
|
+
return false;
|
|
132
|
+
const l = parseLine(line);
|
|
133
|
+
return l?.type === 'event_msg' && (l.payload?.type === 'task_complete' || l.payload?.type === 'turn_aborted');
|
|
134
|
+
}
|
|
135
|
+
/** The file a set of mirrored lines restores to: each line and its newline. */
|
|
136
|
+
export function rolloutText(lines) {
|
|
137
|
+
let out = '';
|
|
138
|
+
for (const l of lines)
|
|
139
|
+
out += `${l}\n`;
|
|
140
|
+
return out;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* The rollout path relative to `CODEX_HOME`, from the absolute path
|
|
144
|
+
* app-server reports. Codex resumes a thread only from a file with its
|
|
145
|
+
* canonical name, `rollout-<YYYY-MM-DDTHH-MM-SS>-<threadId>.jsonl` (any
|
|
146
|
+
* directory under `sessions/`, any timestamp): anything else is "no rollout
|
|
147
|
+
* found" or "not a canonical rollout filename".
|
|
148
|
+
*/
|
|
149
|
+
export function relativeRolloutPath(absolute, threadId) {
|
|
150
|
+
const at = absolute.lastIndexOf('/sessions/');
|
|
151
|
+
const rel = at >= 0 ? absolute.slice(at + 1) : '';
|
|
152
|
+
const name = rel.slice(rel.lastIndexOf('/') + 1);
|
|
153
|
+
if (rel.includes('..') || !/^rollout-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-/.test(name) || name !== `${name.slice(0, 28)}${threadId}.jsonl`) {
|
|
154
|
+
return `sessions/rollout-1970-01-01T00-00-00-${threadId}.jsonl`;
|
|
155
|
+
}
|
|
156
|
+
return rel;
|
|
157
|
+
}
|
|
158
|
+
/** A thread id as the harness accepts it from app-server (untrusted: it names a file to tail). */
|
|
159
|
+
const THREAD_ID = /^[0-9A-Za-z-]{8,64}$/;
|
|
160
|
+
/** Whether `rel` is a rollout path the driver itself would derive for `threadId`: `sessions/[YYYY/MM/DD/]rollout-<ts>-<threadId>.jsonl`, nothing else. */
|
|
161
|
+
export function isRolloutPath(rel, threadId) {
|
|
162
|
+
if (!THREAD_ID.test(threadId))
|
|
163
|
+
return false;
|
|
164
|
+
const m = /^sessions\/(?:\d{4}\/\d{2}\/\d{2}\/)?rollout-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-([0-9A-Za-z-]+)\.jsonl$/.exec(rel);
|
|
165
|
+
return !!m && m[1] === threadId;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The rollout path relative to `CODEX_HOME`, from the absolute path a VM's
|
|
169
|
+
* app-server reports (docs/workspaces.md §5.6): the driver derives the path
|
|
170
|
+
* it tails itself and refuses anything else. `homes` are what `CODEX_HOME`
|
|
171
|
+
* may be written as (the path the harness set, and the one app-server's
|
|
172
|
+
* `initialize` says, which may be its canonical form). Throws for a path
|
|
173
|
+
* outside them or not of the form `isRolloutPath` accepts.
|
|
174
|
+
*/
|
|
175
|
+
export function rolloutPathIn(homes, absolute, threadId) {
|
|
176
|
+
for (const home of homes) {
|
|
177
|
+
const prefix = `${home.replace(/\/+$/, '')}/`;
|
|
178
|
+
if (!home || !absolute.startsWith(prefix))
|
|
179
|
+
continue;
|
|
180
|
+
const rel = absolute.slice(prefix.length);
|
|
181
|
+
if (isRolloutPath(rel, threadId))
|
|
182
|
+
return rel;
|
|
183
|
+
}
|
|
184
|
+
throw new Error('agentrun-harness-codex: app-server named a rollout file the driver would not derive for the thread (outside CODEX_HOME, or not sessions/…/rollout-<ts>-<threadId>.jsonl)');
|
|
185
|
+
}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const CODEX_DESCRIBED_KEYWORDS: readonly string[];
|
|
2
|
+
/** The heading the described constraints go under, in the tool's description. */
|
|
3
|
+
export declare const DESCRIBED_HEADING = "Constraints on the arguments (checked on every call; not part of the schema you were given):";
|
|
4
|
+
/** The shapes of `schema` Codex would drop or retype, as one refusal each: `<tool>: <pointer>: <keyword> is not supported by
|
|
5
|
+
* Codex 0.164.0 (it drops or retypes it)`. Empty when Codex carries the schema (less what is described). */
|
|
6
|
+
export declare function codexSchemaProblems(tool: string, schema: unknown): string[];
|
|
7
|
+
/** One line per position that has a stripped keyword, sorted by JSON pointer: `<path>: <type>, <words>`. */
|
|
8
|
+
export declare function describeDropped(schema: unknown): string[];
|
|
9
|
+
/** The description Codex forwards for a tool: its own, then the described constraints under one heading. */
|
|
10
|
+
export declare function describedDescription(description: string, schema: unknown): string;
|
|
11
|
+
/** `schema` as Codex forwards it, once its strippable keywords are gone: the original less the described keywords, a position
|
|
12
|
+
* left empty typed `{ "type": "string" }` and an object position with no `properties` given `properties: {}`, as measured.
|
|
13
|
+
* For tests, and the recorded column. */
|
|
14
|
+
export declare function stripDescribed(schema: unknown): unknown;
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// What the Codex adapter does about the schema of a tool, written for AgentRun. Codex 0.164.0 sanitizes the JSON Schema
|
|
2
|
+
// of every tool it offers its model (measured on the pinned binary, test/fixtures/schema-column.json): it drops a set of
|
|
3
|
+
// keywords and it drops or retypes a few shapes. So that no difference is silent:
|
|
4
|
+
// - a shape Codex drops or retypes is REFUSED when the session is checked, by tool, JSON pointer and keyword;
|
|
5
|
+
// - a keyword Codex only strips (a bound, a pattern, a format, a title, a default) is DESCRIBED, in plain words, in the
|
|
6
|
+
// tool's description, which Codex forwards whole. The host and the DSL still validate against the original schema;
|
|
7
|
+
// only the model's copy is described.
|
|
8
|
+
/** The keywords Codex strips and the adapter describes, with the words for each. */
|
|
9
|
+
const DESCRIBED = Object.freeze({
|
|
10
|
+
minLength: (v) => `at least ${show(v)} characters`,
|
|
11
|
+
maxLength: (v) => `at most ${show(v)} characters`,
|
|
12
|
+
maxItems: (v) => `at most ${show(v)} items`,
|
|
13
|
+
minProperties: (v) => `at least ${show(v)} properties`,
|
|
14
|
+
maxProperties: (v) => `at most ${show(v)} properties`,
|
|
15
|
+
minimum: (v) => `at least ${show(v)}`,
|
|
16
|
+
maximum: (v) => `at most ${show(v)}`,
|
|
17
|
+
exclusiveMinimum: (v) => `greater than ${show(v)}`,
|
|
18
|
+
exclusiveMaximum: (v) => `less than ${show(v)}`,
|
|
19
|
+
multipleOf: (v) => `a multiple of ${show(v)}`,
|
|
20
|
+
pattern: (v) => `matches ${show(v)}`,
|
|
21
|
+
format: (v) => `format ${show(v)}`,
|
|
22
|
+
uniqueItems: (v) => (v === true ? "items are unique" : undefined),
|
|
23
|
+
title: (v) => `titled ${JSON.stringify(v)}`,
|
|
24
|
+
default: (v) => `default ${show(v)}`,
|
|
25
|
+
examples: (v) => `examples ${show(v)}`,
|
|
26
|
+
nullable: (v) => (v === true ? "may be null" : undefined),
|
|
27
|
+
deprecated: (v) => (v === true ? "deprecated" : undefined),
|
|
28
|
+
readOnly: (v) => (v === true ? "read only" : undefined),
|
|
29
|
+
writeOnly: (v) => (v === true ? "write only" : undefined),
|
|
30
|
+
});
|
|
31
|
+
/** Keywords Codex strips that mean nothing to a model: a comment for people, the dialect marker `$schema` (Codex drops it
|
|
32
|
+
* whatever it names, and the shared check has already refused one that names no dialect the harnesses take), and the
|
|
33
|
+
* DSL's own `criteria`, which the DSL removes from the model's copy itself. Dropped without a word. */
|
|
34
|
+
const NOISE = Object.freeze(["$comment", "$schema", "criteria"]);
|
|
35
|
+
/** The order the words of one position are written in. */
|
|
36
|
+
const ORDER = ["pattern", "format", "exclusiveMinimum", "exclusiveMaximum", "minimum", "maximum", "multipleOf", "minLength", "maxLength", "maxItems", "minProperties", "maxProperties", "uniqueItems", "nullable", "readOnly", "writeOnly", "deprecated", "default", "examples", "title"];
|
|
37
|
+
export const CODEX_DESCRIBED_KEYWORDS = Object.freeze(Object.keys(DESCRIBED));
|
|
38
|
+
/** The heading the described constraints go under, in the tool's description. */
|
|
39
|
+
export const DESCRIBED_HEADING = "Constraints on the arguments (checked on every call; not part of the schema you were given):";
|
|
40
|
+
const show = (v) => (typeof v === "string" ? v : JSON.stringify(v));
|
|
41
|
+
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
42
|
+
const SCALAR = (v) => v === null || ["string", "number", "boolean"].includes(typeof v);
|
|
43
|
+
/** The keywords whose value is a map of schemas, a schema, or a list of schemas: where a schema position is. */
|
|
44
|
+
const SCHEMA_MAPS = ["properties", "$defs", "definitions", "dependentSchemas"];
|
|
45
|
+
const SCHEMA_LISTS = ["anyOf", "oneOf", "allOf"];
|
|
46
|
+
const SCHEMA_ONES = ["items", "additionalProperties", "contains", "propertyNames", "unevaluatedItems", "unevaluatedProperties"];
|
|
47
|
+
const esc = (key) => key.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
48
|
+
/** The schema positions directly below `node`, in the order they are visited. */
|
|
49
|
+
function childrenOf({ pointer, path, node, inAllOf }) {
|
|
50
|
+
const out = [];
|
|
51
|
+
const add = (sub, at, readable, allOf = inAllOf) => {
|
|
52
|
+
if (isObject(sub))
|
|
53
|
+
out.push({ pointer: at, path: readable, node: sub, inAllOf: allOf });
|
|
54
|
+
};
|
|
55
|
+
for (const key of SCHEMA_MAPS) {
|
|
56
|
+
const map = node[key];
|
|
57
|
+
if (isObject(map))
|
|
58
|
+
for (const [name, sub] of Object.entries(map))
|
|
59
|
+
add(sub, `${pointer}/${key}/${esc(name)}`, key === "properties" ? `${path ? `${path}.` : ""}${name}` : `${path ? `${path}.` : ""}${key}.${name}`);
|
|
60
|
+
}
|
|
61
|
+
for (const key of SCHEMA_LISTS) {
|
|
62
|
+
const list = node[key];
|
|
63
|
+
if (Array.isArray(list))
|
|
64
|
+
list.forEach((sub, i) => add(sub, `${pointer}/${key}/${i}`, `${path}${path ? " " : ""}(${key} ${i + 1})`, inAllOf || key === "allOf"));
|
|
65
|
+
}
|
|
66
|
+
for (const key of SCHEMA_ONES) {
|
|
67
|
+
const sub = node[key];
|
|
68
|
+
if (isObject(sub))
|
|
69
|
+
add(sub, `${pointer}/${key}`, key === "items" ? `${path}[]` : `${path}${path ? "." : ""}(${key})`);
|
|
70
|
+
else if (Array.isArray(sub))
|
|
71
|
+
sub.forEach((s, i) => add(s, `${pointer}/${key}/${i}`, `${path}[${i}]`));
|
|
72
|
+
}
|
|
73
|
+
for (const key of ["not", "if", "then", "else"])
|
|
74
|
+
add(node[key], `${pointer}/${key}`, `${path}${path ? "." : ""}(${key})`);
|
|
75
|
+
if (Array.isArray(node.prefixItems))
|
|
76
|
+
node.prefixItems.forEach((s, i) => add(s, `${pointer}/prefixItems/${i}`, `${path}[${i}]`));
|
|
77
|
+
if (isObject(node.patternProperties))
|
|
78
|
+
for (const [name, sub] of Object.entries(node.patternProperties))
|
|
79
|
+
add(sub, `${pointer}/patternProperties/${esc(name)}`, `${path}.(${name})`);
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
/** Every schema position of `schema`, depth first, with `$defs` and union branches. A loop with its own stack, so no
|
|
83
|
+
* depth of schema overflows the call stack. */
|
|
84
|
+
function positions(schema) {
|
|
85
|
+
const out = [];
|
|
86
|
+
if (!isObject(schema))
|
|
87
|
+
return out;
|
|
88
|
+
const stack = [{ pointer: "", path: "", node: schema, inAllOf: false }];
|
|
89
|
+
// A node already visited is skipped, so a cycle (an object that contains itself, which only code can build) ends
|
|
90
|
+
// here whichever caller reaches it. A node shared by two places (not a cycle) is walked at the first.
|
|
91
|
+
const seen = new WeakSet();
|
|
92
|
+
for (let here = stack.pop(); here; here = stack.pop()) {
|
|
93
|
+
if (seen.has(here.node))
|
|
94
|
+
continue;
|
|
95
|
+
seen.add(here.node);
|
|
96
|
+
out.push(here);
|
|
97
|
+
const below = childrenOf(here);
|
|
98
|
+
for (let i = below.length - 1; i >= 0; i -= 1)
|
|
99
|
+
stack.push(below[i]);
|
|
100
|
+
}
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
/** The shapes of `schema` Codex would drop or retype, as one refusal each: `<tool>: <pointer>: <keyword> is not supported by
|
|
104
|
+
* Codex 0.164.0 (it drops or retypes it)`. Empty when Codex carries the schema (less what is described). */
|
|
105
|
+
export function codexSchemaProblems(tool, schema) {
|
|
106
|
+
const problems = [];
|
|
107
|
+
const refuse = (pointer, keyword) => problems.push(`${tool}: ${pointer || "/"}: ${keyword} is not supported by Codex 0.164.0 (it drops or retypes it)`);
|
|
108
|
+
for (const { pointer, node } of positions(schema)) {
|
|
109
|
+
if (Array.isArray(node.items))
|
|
110
|
+
refuse(`${pointer}/items`, "items (an array)");
|
|
111
|
+
if (node.prefixItems !== undefined)
|
|
112
|
+
refuse(`${pointer}/prefixItems`, "prefixItems");
|
|
113
|
+
if (node.patternProperties !== undefined)
|
|
114
|
+
refuse(`${pointer}/patternProperties`, "patternProperties");
|
|
115
|
+
const conditional = ["if", "then", "else"].find((key) => node[key] !== undefined);
|
|
116
|
+
if (conditional)
|
|
117
|
+
refuse(`${pointer}/${conditional}`, "if/then/else");
|
|
118
|
+
if (node.not !== undefined)
|
|
119
|
+
refuse(`${pointer}/not`, "not");
|
|
120
|
+
if ((Array.isArray(node.enum) && node.enum.some((v) => !SCALAR(v))) || (node.const !== undefined && !SCALAR(node.const)))
|
|
121
|
+
refuse(`${pointer}/${node.const !== undefined && !SCALAR(node.const) ? "const" : "enum"}`, "enum with a non-scalar value");
|
|
122
|
+
// With no `type`, Codex types an enum or a const `string`: a number, a boolean or a null in it would be mistyped.
|
|
123
|
+
const values = Array.isArray(node.enum) ? node.enum : node.const !== undefined ? [node.const] : [];
|
|
124
|
+
if (node.type === undefined && values.every(SCALAR) && values.some((v) => typeof v !== "string"))
|
|
125
|
+
refuse(`${pointer}/${Array.isArray(node.enum) ? "enum" : "const"}`, "enum with non-string values and no type (Codex types it string)");
|
|
126
|
+
for (const key of ["contains", "dependentRequired", "propertyNames"])
|
|
127
|
+
if (node[key] !== undefined)
|
|
128
|
+
refuse(`${pointer}/${key}`, key);
|
|
129
|
+
if (node.items === true || node.items === false)
|
|
130
|
+
refuse(`${pointer}/items`, "items (a boolean)");
|
|
131
|
+
if (Array.isArray(node.allOf) && node.allOf.some((member) => isObject(member) && positions(member).some((p) => CODEX_DESCRIBED_KEYWORDS.some((k) => k !== "title" && k !== "default" && p.node[k] !== undefined))))
|
|
132
|
+
refuse(`${pointer}/allOf`, "allOf with constraints in its members");
|
|
133
|
+
}
|
|
134
|
+
return problems;
|
|
135
|
+
}
|
|
136
|
+
/** One line per position that has a stripped keyword, sorted by JSON pointer: `<path>: <type>, <words>`. */
|
|
137
|
+
export function describeDropped(schema) {
|
|
138
|
+
const lines = [];
|
|
139
|
+
for (const { pointer, path, node } of positions(schema)) {
|
|
140
|
+
const words = ORDER.flatMap((key) => (node[key] === undefined ? [] : [DESCRIBED[key](node[key])].filter((w) => w !== undefined)));
|
|
141
|
+
if (!words.length)
|
|
142
|
+
continue;
|
|
143
|
+
const type = typeof node.type === "string" ? [node.type] : [];
|
|
144
|
+
lines.push([pointer, `${path || "(the whole value)"}: ${[...type, ...words].join(", ")}`]);
|
|
145
|
+
}
|
|
146
|
+
return lines.sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0)).map(([, line]) => line);
|
|
147
|
+
}
|
|
148
|
+
/** The description Codex forwards for a tool: its own, then the described constraints under one heading. */
|
|
149
|
+
export function describedDescription(description, schema) {
|
|
150
|
+
const lines = describeDropped(schema);
|
|
151
|
+
return lines.length ? `${description}\n\n${DESCRIBED_HEADING}\n${lines.map((l) => `- ${l}`).join("\n")}` : description;
|
|
152
|
+
}
|
|
153
|
+
/** `schema` as Codex forwards it, once its strippable keywords are gone: the original less the described keywords, a position
|
|
154
|
+
* left empty typed `{ "type": "string" }` and an object position with no `properties` given `properties: {}`, as measured.
|
|
155
|
+
* For tests, and the recorded column. */
|
|
156
|
+
export function stripDescribed(schema) {
|
|
157
|
+
const copy = structuredClone(schema);
|
|
158
|
+
for (const { node } of positions(copy)) {
|
|
159
|
+
let stripped = false;
|
|
160
|
+
for (const key of [...CODEX_DESCRIBED_KEYWORDS, ...NOISE])
|
|
161
|
+
if (key in node) {
|
|
162
|
+
delete node[key];
|
|
163
|
+
stripped = true;
|
|
164
|
+
}
|
|
165
|
+
// A const is an enum of one; an enum or const with no type is typed string (strings only: the others are refused).
|
|
166
|
+
if (node.const !== undefined) {
|
|
167
|
+
node.enum = [node.const];
|
|
168
|
+
delete node.const;
|
|
169
|
+
}
|
|
170
|
+
if (Array.isArray(node.enum) && node.type === undefined)
|
|
171
|
+
node.type = "string";
|
|
172
|
+
if (stripped && Object.keys(node).length === 0)
|
|
173
|
+
node.type = "string";
|
|
174
|
+
if (node.type === "object" && node.properties === undefined)
|
|
175
|
+
node.properties = {};
|
|
176
|
+
}
|
|
177
|
+
return copy;
|
|
178
|
+
}
|