@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.
@@ -0,0 +1,200 @@
1
+ import { type Harness, type HostTool, type ReplayPolicy, type SessionState, type Submission } from './host/kernel.js';
2
+ import { type AcquireMachine } from './host/process.js';
3
+ import { type TomlValue } from './config.js';
4
+ import { type RolloutCall } from './rollout.js';
5
+ /**
6
+ * Codex on harness-world: a kernel `Harness` that drives `codex app-server
7
+ * (docs/workspaces.md §1 decision 2, §5.6). Every drive acquires the
8
+ * session's VM through the sandbox tier (`attachInDrive` with the session
9
+ * factory's acquire, `machine.acquire`), and refuses a machine the factory
10
+ * didn't brand: `Unavailable('sandbox_required')`.
11
+ *
12
+ * - **Where it runs.** app-server runs as the VM's unprivileged user, its
13
+ * stdio over the relay (`RemoteProcess`, a `LineChannel`), its working
14
+ * directory `/workspace` (the session's Drive, the same on every VM), its
15
+ * `HOME` and `CODEX_HOME` under `/var/lib/dh/s`, wiped before every spawn.
16
+ * `CODEX_HOME` is outside every writable root of Codex's own sandbox, so a
17
+ * command can't rewrite the rollout or `config.toml`.
18
+ * - **Tools.** Tool-less by default: the app's `HostTool`s (`tools`) are
19
+ * app-server dynamic tools, run in the Function, each call the kernel's
20
+ * effect sandwich keyed by Codex's own `call_id` (`tool:<call_id>`), and
21
+ * they are its only tools (shell, `apply_patch` and images off, a
22
+ * `read-only` sandbox). With `builtins: true`, Codex's own shell and
23
+ * `apply_patch` are on, on `/workspace`, under Codex's own sandbox
24
+ * (`sandboxMode`: by default `workspace-write` when bubblewrap's self-test
25
+ * passes in the VM, else `danger-full-access`, the VM being the boundary;
26
+ * the first verdict is recorded in the log, `codex.sandbox`, and every
27
+ * later drive keeps it).
28
+ * Every command and patch asks the drive first (`approvalPolicy:
29
+ * 'untrusted'`): its intent is committed once the rollout line that made
30
+ * the call is in the log, and only then approved, once. Anything else is
31
+ * declined: a second approval for the same call (Codex's "retry without
32
+ * sandbox?"), input to a running command, a sub-command, more
33
+ * permissions, network access. Its outcome is committed when Codex
34
+ * reports the item completed, after the Drive is flushed. Nothing reruns
35
+ * one after a crash: the Drive is durable, not transactional.
36
+ * - **The rollout is mirrored into the log.** Codex keeps its thread as an
37
+ * append-only JSONL file; the relay tails it (`TAIL`, inode and size
38
+ * checked: a replaced or shrunk file stops the drive, and the third time
39
+ * in a turn ends the turn) and every line
40
+ * becomes a record of the session's native journal (`rollout:<threadId>`).
41
+ * Before every `replay: 'never'` call the drive drains the tail (`SYNC`)
42
+ * and checks the call's own line is there (a bounded number of times):
43
+ * without it, the call is declined, never run, and the model is told to
44
+ * try again.
45
+ * - **Every drive restores from the log.** `CODEX_HOME` is wiped and gets a
46
+ * fresh `config.toml` and the mirrored rollout, byte for byte (`FILES`);
47
+ * app-server resumes the thread by id.
48
+ * - **Recovery.** A turn a crash cut off is resumed natively: calls the
49
+ * rollout left without an output get one injected from the log
50
+ * (`thread/inject_items`), then a recovery note built from the log starts
51
+ * a turn. A link to the VM lost mid-drive is re-attached in the same
52
+ * invocation (a bounded number of times, and only while there is work
53
+ * left), once the app's tools still running have settled, and the drive
54
+ * resumes from the log.
55
+ * - **The inbox.** Each submission is placed, then sent with its id as
56
+ * Codex's `clientUserMessageId`: `turn/start`, or `turn/steer` for a
57
+ * steer that arrives while a turn runs.
58
+ *
59
+ * Model traffic goes from the VM's loopback model port through the relay to
60
+ * this drive's `gatewayHandler` (`CODEX_ROUTES`: the fixed path table, the
61
+ * body policy, the model, the budget), which adds the credential: the VM
62
+ * never holds it. Codex's provider key is the attach's model key
63
+ * (`SessionMachine.modelKey`, which the relay checks with the caller's pid),
64
+ * hidden from the model's commands
65
+ * (`shell_environment_policy`). Each response's cost is a record in the log
66
+ * (`codex.spend`), so the dollar caps hold over the turn and the session,
67
+ * across invocations. app-server runs no git of the Drive's: `GIT_DIR`
68
+ * points nowhere and no git config is read.
69
+ */
70
+ export interface CodexHarnessOptions {
71
+ /**
72
+ * The app's tools, as app-server dynamic tools: each call a kernel effect
73
+ * keyed by Codex's `call_id`, with the tool's replay policy, run where the
74
+ * drive runs (the Function). Its only tools, or (with `builtins: true`)
75
+ * next to the built-ins. Default none.
76
+ */
77
+ tools?: HostTool[];
78
+ /**
79
+ * Codex's own shell and `apply_patch` (and `view_image`), on `/workspace`
80
+ * in the session's VM. Default false (tool-less: `tools` are its only
81
+ * tools). Image generation, web search, sub-agents and plugins stay off
82
+ * either way.
83
+ */
84
+ builtins?: boolean;
85
+ /**
86
+ * Codex's sandbox for its commands, in the VM (the Codex SDK's word; this
87
+ * option was `sandbox`). Needs `builtins: true`. Default: `workspace-write`
88
+ * (writes to `/workspace` and its home, network on) when bubblewrap's
89
+ * self-test passes in the VM at attach, else `danger-full-access` (the VM
90
+ * is the boundary, and holds no credential; the provider key's
91
+ * placeholder is then readable by the model's commands, docs/workspaces.md
92
+ * §5.5).
93
+ */
94
+ sandboxMode?: 'read-only' | 'workspace-write' | 'danger-full-access';
95
+ model?: string;
96
+ /** Developer instructions for the thread (`developerInstructions`). */
97
+ instructions?: string;
98
+ /** Replace Codex's own base instructions (they describe its built-in tools). */
99
+ baseInstructions?: string;
100
+ /** Reasoning effort per turn. Default Codex's. */
101
+ effort?: 'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh';
102
+ credential?: () => Promise<string>;
103
+ /** The Gateway's base URL. Default `gatewayUrl()`. */
104
+ gatewayUrl?: string;
105
+ /** The kernel name (its sessions' runs are `workflow//durable-harness@<version>//<name>`). Default `codex`. */
106
+ name?: string;
107
+ /** The model's context window (Gateway ids are unknown to Codex). Default 272,000. */
108
+ contextWindow?: number;
109
+ /** Extra `config.toml` settings, merged over the generated ones. The keys the harness sets itself (`RESERVED_CONFIG_KEYS`) throw. */
110
+ config?: Record<string, TomlValue>;
111
+ /** How often to read the inbox for steers while a turn runs, without hints. Default 500 ms (5 s with hints). */
112
+ inboxPollMs?: number;
113
+ /** Diagnostics (stderr noise, declined approvals, the sandbox's lifecycle). */
114
+ log?: (message: string, fields?: Record<string, unknown>) => void;
115
+ /**
116
+ * The session's VM (internal: durable-harness's `codex()` resolves
117
+ * `workspace: sandbox()` into it). Without it, or with an acquire that
118
+ * gives a machine the sandbox tier's session factory didn't brand, a
119
+ * drive fails its turn with `Unavailable('sandbox_required')`.
120
+ */
121
+ machine?: CodexMachine;
122
+ /**
123
+ */
124
+ test?: {
125
+ command: string;
126
+ args?: string[];
127
+ };
128
+ }
129
+ /** The session's VM, as a `sandbox()` descriptor resolves it (durable-harness's `codex()` builds it). */
130
+ export interface CodexMachine {
131
+ /** The sandbox tier's acquire: its session factory (`sandboxSession`), which brands the machines it gives. */
132
+ acquire: AcquireMachine;
133
+ /** `sandbox({ env })`: non-secret variables for app-server and for the model's commands (`shell_environment_policy.set`). */
134
+ env?: Readonly<Record<string, string>>;
135
+ /** `sandbox({ network })`: the gateway handler refuses what would reach past it through the provider. Default `allow-all`. */
136
+ network?: 'allow-all' | 'deny-all' | {
137
+ readonly allow: readonly string[];
138
+ };
139
+ /** `sandbox({ budget })`'s dollar caps, over the turn and the session (each response's cost is in the log). Default none (`Infinity`). */
140
+ budget?: {
141
+ readonly turnUsd?: number;
142
+ readonly sessionUsd?: number;
143
+ };
144
+ /** `sandbox({ commandTimeoutMs })`: a built-in command's longest run, within the time left in the invocation. */
145
+ commandTimeoutMs?: number;
146
+ }
147
+ export declare const DEFAULT_MODEL = "openai/gpt-5.4-mini";
148
+ /** A Codex item's replay policy: a command whose every action reads is `safe`; any other command, and every patch, `never`. */
149
+ export declare function codexItemReplay(item: {
150
+ type?: string;
151
+ commandActions?: {
152
+ type?: string;
153
+ }[];
154
+ }): ReplayPolicy;
155
+ /**
156
+ * The env var Codex reads its provider key from (`env_key`; it sends it as
157
+ * `authorization: Bearer`). It holds the attach's model key
158
+ * (`SessionMachine.modelKey`), never a credential: the gateway handler adds
159
+ * that in the Function. Hidden from the
160
+ * model's commands by `shell_environment_policy` (`HARNESS_*`), and exempt
161
+ * from the spawn check's credential-name rule (harness-world-sandbox's
162
+ * `PLACEHOLDER_NAMES`), never from its value rules.
163
+ */
164
+ export declare const KEY_ENV = "HARNESS_CODEX_GATEWAY_KEY";
165
+ /** What the model is told when a call was declined because its line wasn't in the log yet. */
166
+ export declare const LAGGING_NOTE = "[harness] The previous tool call was not run: the session's log had not caught up with it yet. Nothing happened; make the same call again.";
167
+ /** A dynamic tool's result when its call was declined for the same reason. */
168
+ export declare const LAGGING_RESULT = "Not run: the session's log had not caught up with this call yet. Nothing happened; make the same call again.";
169
+ /** The outcome of a command the invocation's deadline stopped. */
170
+ export declare const TIMEOUT_TEXT = "The command ran out of time in this invocation and was stopped: it may have partly taken effect. Check the state it left before running it again.";
171
+ /** The outcome of a command approved too close to the invocation's end: declined, never run. */
172
+ export declare const NO_TIME_TEXT = "Not run: too little time was left in this invocation to run it and record its outcome. Nothing happened; make the same call again.";
173
+ /** The journal stream of response costs: `{ usd, turn, estimated? }` (`turn`: the session turn it was spent in). */
174
+ export declare const SPEND_STREAM = "codex.spend";
175
+ /** The journal stream of the bubblewrap self-test's verdict: `{ sandboxMode, reason? }`, the first one kept. */
176
+ export declare const SANDBOX_STREAM = "codex.sandbox";
177
+ /** The journal stream of rollout tail failures: `{ turn, error }`. */
178
+ export declare const TAIL_STREAM = "codex.tail";
179
+ /** The rollout tail failed (the file shrank, was replaced, or is not the session user's): what the drive halts with. */
180
+ export declare class RolloutTailError extends Error {
181
+ constructor(cause: unknown);
182
+ }
183
+ export declare function codexHarness(options?: CodexHarnessOptions): Harness;
184
+ /** The client id prefix of a recovery note's turn: `recover:<n>`, then `:<id>,<id>` for the submissions it carries. */
185
+ export declare const RECOVER_PREFIX = "recover:";
186
+ /** The client id a recovery note's turn is sent with. */
187
+ export declare function recoveryClientId(n: number, carried: readonly Submission[]): string;
188
+ /**
189
+ * Placed submissions of the open turn that Codex never recorded: no user
190
+ * message in the rollout has their id as its `client_id`, alone or among
191
+ * the ids a recovery note's turn carried. A crash between placing and
192
+ * sending, or a steer it accepted but had not written yet.
193
+ */
194
+ export declare function owedSubmissions(state: SessionState, placed: string[], clientIds: Set<string>): Submission[];
195
+ /**
196
+ * The output injected for a call the rollout left dangling, from what the
197
+ * log knows: the committed result (exactly once), the recorded failure, or
198
+ * that it never started.
199
+ */
200
+ export declare function repairOutput(state: SessionState, call: RolloutCall): string;