@parcha/agentrun-harness-codex 0.0.0-stage → 0.1.0-beta.30
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 +89 -0
- package/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +72 -2
- package/dist/adapter.d.ts +67 -0
- package/dist/adapter.js +295 -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 +65 -0
- package/dist/host/machine.js +303 -0
- package/dist/host/policy.d.ts +87 -0
- package/dist/host/policy.js +609 -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,145 @@
|
|
|
1
|
+
import type { TomlValue } from "../config.js";
|
|
2
|
+
import type { DriveContext } from "./kernel.js";
|
|
3
|
+
export { boundResult, BuiltinCalls, failureText, Mutex, NativeJournal, openTurn, placeSubmissions, recoveryNote, recoverySystemNote, recoveryUserTurn, restartEvent, toolBridge, toolKey, transcriptOf, } from "@parcha/agentrun-kernel/process";
|
|
4
|
+
export type { BuiltinStart, ToolResult, TranscriptEntry } from "@parcha/agentrun-kernel/process";
|
|
5
|
+
/** A child's line protocol: one JSON value per line out, lines in. */
|
|
6
|
+
export interface LineChannel {
|
|
7
|
+
send(line: unknown): boolean;
|
|
8
|
+
end(): void;
|
|
9
|
+
lines(): AsyncIterable<string>;
|
|
10
|
+
readonly exited: Promise<{
|
|
11
|
+
code: number | null;
|
|
12
|
+
signal: string | null;
|
|
13
|
+
}>;
|
|
14
|
+
stderrTail(): string;
|
|
15
|
+
}
|
|
16
|
+
export type KillSignal = "SIGTERM" | "SIGKILL" | "SIGINT";
|
|
17
|
+
export interface SessionProcess extends LineChannel {
|
|
18
|
+
alive(): boolean;
|
|
19
|
+
kill(signal?: KillSignal): void;
|
|
20
|
+
}
|
|
21
|
+
/** A session machine's private directories. */
|
|
22
|
+
export type CacheDir = "home" | "config" | "codex-home" | "tmp" | "cache";
|
|
23
|
+
export interface CacheFile {
|
|
24
|
+
path: string;
|
|
25
|
+
bytes: Uint8Array;
|
|
26
|
+
mode?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface MachineSpawn {
|
|
29
|
+
command: string;
|
|
30
|
+
args?: string[];
|
|
31
|
+
/** The child's environment beyond the machine's allowlist (PATH, LANG, TZ). Nothing else is inherited. */
|
|
32
|
+
env?: Record<string, string>;
|
|
33
|
+
cwd?: string;
|
|
34
|
+
signal?: AbortSignal;
|
|
35
|
+
isolatedCommands?: boolean;
|
|
36
|
+
}
|
|
37
|
+
/** A line-complete tail of a file in one of the machine's directories. */
|
|
38
|
+
export interface MachineTail {
|
|
39
|
+
readonly offset: number;
|
|
40
|
+
read(): string[];
|
|
41
|
+
sync(): Promise<void>;
|
|
42
|
+
close(): void;
|
|
43
|
+
}
|
|
44
|
+
/** Where a session's harness runs: here, a local machine with private directories and a loopback model proxy. */
|
|
45
|
+
export interface SessionMachine {
|
|
46
|
+
readonly sessionId: string;
|
|
47
|
+
readonly root: string;
|
|
48
|
+
readonly dirs: Readonly<Record<CacheDir, string>>;
|
|
49
|
+
readonly signal: AbortSignal;
|
|
50
|
+
/** The model proxy's base URL (loopback), and the per-session key it admits. */
|
|
51
|
+
readonly modelUrl: string;
|
|
52
|
+
readonly modelKey: string;
|
|
53
|
+
/** What the model must hear about this machine (none locally), and its acknowledgement key. */
|
|
54
|
+
readonly notes: readonly string[];
|
|
55
|
+
readonly notesKey?: string;
|
|
56
|
+
spawn(options: MachineSpawn): Promise<SessionProcess>;
|
|
57
|
+
readonly files: {
|
|
58
|
+
upload(dir: CacheDir, files: readonly CacheFile[]): Promise<void>;
|
|
59
|
+
wipe(): Promise<void>;
|
|
60
|
+
};
|
|
61
|
+
tail(dir: CacheDir, path: string, options?: {
|
|
62
|
+
offset?: number;
|
|
63
|
+
}): Promise<MachineTail>;
|
|
64
|
+
/** Per acquire: a machine re-attached for a later drive announces nothing until its proxy has seen an answer.
|
|
65
|
+
* The code-mode `exec` call the model proxy's latest answer carried, read before Codex could act on it (none when that
|
|
66
|
+
* answer held none): what a nested call of a script is tied to, even before the `exec`'s line is in the rollout. */
|
|
67
|
+
announcedExec?(): string | undefined;
|
|
68
|
+
/** Told each line the rollout's tail reads, as it reads it: the harness's view of code mode follows the log from here. */
|
|
69
|
+
onRolloutLine?(listener: (line: string) => void): void;
|
|
70
|
+
/** Given Codex's `config.toml` settings before the harness writes them, to set what the session's own options decide
|
|
71
|
+
* and the harness does not (its web search, a key reserved from callers). */
|
|
72
|
+
configure?(config: Record<string, TomlValue>): void;
|
|
73
|
+
fsync(): Promise<void>;
|
|
74
|
+
close(): Promise<void>;
|
|
75
|
+
}
|
|
76
|
+
/** What a harness's drive asks its machine factory for: the drive, the model route its proxy serves, and its deadline. */
|
|
77
|
+
export interface AcquireMachineRequest {
|
|
78
|
+
ctx: DriveContext;
|
|
79
|
+
model?: GatewayHandler;
|
|
80
|
+
deadline?: number;
|
|
81
|
+
}
|
|
82
|
+
export type AcquireMachine = (request: AcquireMachineRequest) => Promise<SessionMachine>;
|
|
83
|
+
/** Mark a machine as made by this package's factory. */
|
|
84
|
+
export declare function brandMachine<T extends object>(machine: T): T;
|
|
85
|
+
/** Whether a machine was made by this package's factory: the harness refuses any other. */
|
|
86
|
+
export declare const isBrandedMachine: (machine: unknown) => machine is SessionMachine;
|
|
87
|
+
/** One route the model proxy forwards. */
|
|
88
|
+
export interface GatewayRoute {
|
|
89
|
+
method: string;
|
|
90
|
+
path: string;
|
|
91
|
+
/** Request headers passed on to the provider. */
|
|
92
|
+
headers?: readonly string[];
|
|
93
|
+
}
|
|
94
|
+
/** Codex's one route: the Responses API. */
|
|
95
|
+
export declare const CODEX_ROUTES: readonly GatewayRoute[];
|
|
96
|
+
/** The session's dollar ledger, kept by the harness in its records. */
|
|
97
|
+
export interface GatewayLedger {
|
|
98
|
+
spent(): {
|
|
99
|
+
turnUsd: number;
|
|
100
|
+
sessionUsd: number;
|
|
101
|
+
};
|
|
102
|
+
add(usd: number, info: {
|
|
103
|
+
method: string;
|
|
104
|
+
path: string;
|
|
105
|
+
model?: string;
|
|
106
|
+
estimated?: boolean;
|
|
107
|
+
}): void;
|
|
108
|
+
}
|
|
109
|
+
/** What the harness says about its model route; the machine's proxy serves it (machine.ts). */
|
|
110
|
+
export interface GatewayHandlerOptions {
|
|
111
|
+
routes: readonly GatewayRoute[];
|
|
112
|
+
models: {
|
|
113
|
+
model: string;
|
|
114
|
+
};
|
|
115
|
+
budget?: {
|
|
116
|
+
turnUsd: number;
|
|
117
|
+
sessionUsd: number;
|
|
118
|
+
};
|
|
119
|
+
ledger?: GatewayLedger;
|
|
120
|
+
network?: string;
|
|
121
|
+
credential: () => Promise<string | undefined> | string | undefined;
|
|
122
|
+
target: string;
|
|
123
|
+
track?: unknown;
|
|
124
|
+
onCap?: (cap: "turnUsd" | "sessionUsd" | "concurrency") => void;
|
|
125
|
+
log?: (message: string, fields?: Record<string, unknown>) => void;
|
|
126
|
+
}
|
|
127
|
+
export interface GatewayHandler {
|
|
128
|
+
readonly kind: "agentrun-model-route";
|
|
129
|
+
readonly options: GatewayHandlerOptions;
|
|
130
|
+
}
|
|
131
|
+
export declare const gatewayHandler: (options: GatewayHandlerOptions) => GatewayHandler;
|
|
132
|
+
/** The provider credential when the harness is given none: the adapter always gives one, so this refuses. */
|
|
133
|
+
export declare const gatewayCredential: () => Promise<string>;
|
|
134
|
+
/** The provider's base URL when the harness is given none: the adapter always gives one. */
|
|
135
|
+
export declare const gatewayUrl: () => string;
|
|
136
|
+
/** Model steps: a sandbox's tracing of each request. A local session reports usage through its proxy instead. */
|
|
137
|
+
export declare class ModelSteps {
|
|
138
|
+
constructor(_ctx: DriveContext, _options?: {
|
|
139
|
+
log?: (message: string, fields?: Record<string, unknown>) => void;
|
|
140
|
+
});
|
|
141
|
+
readonly request: () => void;
|
|
142
|
+
settleAbandoned(): Promise<void>;
|
|
143
|
+
close(): Promise<void>;
|
|
144
|
+
}
|
|
145
|
+
export declare function settleAbandonedModelSteps(_ctx: DriveContext): Promise<void>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export { boundResult, BuiltinCalls, failureText, Mutex, NativeJournal, openTurn, placeSubmissions, recoveryNote, recoverySystemNote, recoveryUserTurn, restartEvent, toolBridge, toolKey, transcriptOf, } from "@parcha/agentrun-kernel/process";
|
|
2
|
+
const BRAND = Symbol.for("agentrun.codex.session-machine");
|
|
3
|
+
/** Mark a machine as made by this package's factory. */
|
|
4
|
+
export function brandMachine(machine) {
|
|
5
|
+
Object.defineProperty(machine, BRAND, { value: true });
|
|
6
|
+
return machine;
|
|
7
|
+
}
|
|
8
|
+
/** Whether a machine was made by this package's factory: the harness refuses any other. */
|
|
9
|
+
export const isBrandedMachine = (machine) => typeof machine === "object" && machine !== null && machine[BRAND] === true;
|
|
10
|
+
/** Codex's one route: the Responses API. */
|
|
11
|
+
export const CODEX_ROUTES = Object.freeze([{ method: "POST", path: "/v1/responses", headers: ["originator", "x-client-request-id"] }]);
|
|
12
|
+
export const gatewayHandler = (options) => ({ kind: "agentrun-model-route", options });
|
|
13
|
+
/** The provider credential when the harness is given none: the adapter always gives one, so this refuses. */
|
|
14
|
+
export const gatewayCredential = async () => {
|
|
15
|
+
throw new Error("agentrun-harness-codex: no provider credential was given to the Codex harness");
|
|
16
|
+
};
|
|
17
|
+
/** The provider's base URL when the harness is given none: the adapter always gives one. */
|
|
18
|
+
export const gatewayUrl = () => {
|
|
19
|
+
throw new Error("agentrun-harness-codex: no provider URL was given to the Codex harness");
|
|
20
|
+
};
|
|
21
|
+
/** Model steps: a sandbox's tracing of each request. A local session reports usage through its proxy instead. */
|
|
22
|
+
export class ModelSteps {
|
|
23
|
+
constructor(_ctx, _options = {}) { }
|
|
24
|
+
request = () => { };
|
|
25
|
+
async settleAbandoned() { }
|
|
26
|
+
async close() { }
|
|
27
|
+
}
|
|
28
|
+
export async function settleAbandonedModelSteps(_ctx) { }
|
|
@@ -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;
|