@parcha/agentrun-harness-codex 0.1.0-beta.80 → 0.1.0-beta.81
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 +8 -0
- package/dist/code-mode.d.ts +19 -0
- package/dist/code-mode.js +76 -4
- package/dist/harness.js +40 -4
- package/dist/host/machine.js +13 -4
- package/dist/scripts.d.ts +45 -2
- package/dist/scripts.js +71 -10
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## 0.1.0-beta.81 (2026-12-01)
|
|
6
|
+
|
|
7
|
+
- A code-mode script that `codex-code-mode-host` did not run to its end ends its turn failed instead of completed, and a host that goes quiet on a script no longer holds the turn open without end. Codex answers an `exec` in its own words, with no script status, when the host is off, did not start, answered outside its protocol, or died with the script in it, and it ended the turn completed; and it waited on a host that took the script and went quiet for as long as the host stayed up. The adapter now counts an `exec` (or the `wait` on its cell) as run only when its output's first text starts with one of Codex's script status headers (`SCRIPT_STATUS`), or with Codex's answer to an `exec` the model wrote wrong (`EXEC_MISTAKES`: no script, or a bad first-line pragma, which Codex answers before it asks the host and the model can put right). Any other answer stops the turn and fails it, naming `codex-code-mode-host` and Codex's words, whether or not a nested call of the script ran first. A host that leaves an `exec` or `wait` unanswered for a minute past the yield the script asked for (Codex's default when it names none), with no nested call of it running, fails the turn the same way. Both lists are checked against the pinned binary. <!-- changes/codex-code-mode-host-down.md -->
|
|
8
|
+
- The loopback model proxy can now check who sends the session's key, not only the key (`ModelProxyOptions.peers`, Linux). Before, any process of the same user could read the key from the harness's environment and use the session's model credential. Now every process that holds the client's socket must be one the host named with `ModelProxy.admitProcess`: the exact process, pinned by its pid and start time, never one of its children and never the host itself. A connection with no holder, or one that is gone, is refused. The check only logs by default: each connection it would refuse is logged once as `would_refuse`, with the session, the reason and the holders' pids, never a key. Setting `AGENTRUN_MODEL_PROXY_PEER_CHECK=enforce` refuses them with a 403. The key is still required either way. The Codex and Claude Agent local machines, OpenCode and Goose turn the check on and name the process they spawn. A socket of another user is refused, unless that user is the one the host says the session runs as (`peers.runUid`), which is admitted on the key alone. A host whose `/proc` can't be read: under `enforce` on Linux the proxy refuses to start (`PROC_UNREADABLE`); otherwise it logs once that the check is off and admits on the key alone. <!-- changes/model-proxy-peer-check.md -->
|
|
9
|
+
- `ModelProxy` has a new method, `admitProcess(pid)`. A host that implements `ModelProxy` itself adds it.
|
|
10
|
+
- `AcpProxy`'s result may carry `admit(pid)`. The ACP harness calls it with the agent's pid once the agent is spawned.
|
|
11
|
+
- `@parcha/agentrun-kernel/host` exports `PEER_CHECK_ENV` and `peerModeOf(env)`.
|
|
12
|
+
|
|
5
13
|
## 0.1.0-beta.80 (2026-12-01)
|
|
6
14
|
|
|
7
15
|
**Breaking:**
|
package/dist/code-mode.d.ts
CHANGED
|
@@ -68,7 +68,20 @@ export interface CodeModeDeps {
|
|
|
68
68
|
/** The bridge's budget (CodexHarnessOptions.resultBudget): a result handed Codex from the log is held to it as the
|
|
69
69
|
* bridge holds one it runs. */
|
|
70
70
|
budget?: ResultBudget;
|
|
71
|
+
/** Told, live, when the code-mode host did not run a script to its end, and the turn fails on it: Codex answered its `exec`
|
|
72
|
+
* (or the `wait` on its cell) with no script status (ScriptEvent.unrun, Codex's words; scripts.ts SCRIPT_STATUS), whether or
|
|
73
|
+
* not a nested call of it ran first (a script that lost its host part way is no completed turn); or the host left the
|
|
74
|
+
* `exec` or `wait` unanswered for `ms` (HOST_SILENCE_MS past its yield), with no nested call of it running. Not for an
|
|
75
|
+
* output this drive wrote itself (a recovered exec's). `text`: what to say of it. */
|
|
76
|
+
hostFailed?(text: string): void;
|
|
77
|
+
/** When the invocation ends (the harness's deadline): a script's wait is not bounded past it. */
|
|
78
|
+
deadline?: number;
|
|
71
79
|
}
|
|
80
|
+
/** How long past its yield (the time Codex waits on an `exec` or a `wait` before it answers "Script running") the code-mode host
|
|
81
|
+
* may leave one unanswered, no nested call of it running, before the turn fails. The first `exec` of a session also starts
|
|
82
|
+
* the host: Codex gives up on its handshake by itself at about 30 s, inside this. Codex itself waits on the host without
|
|
83
|
+
* bound: a host that took the session and then went quiet held the turn open until the invocation was stopped. */
|
|
84
|
+
export declare const HOST_SILENCE_MS = 60000;
|
|
72
85
|
interface Dangling {
|
|
73
86
|
callId: string;
|
|
74
87
|
name: string;
|
|
@@ -146,6 +159,12 @@ export declare function codeMode(deps: CodeModeDeps): {
|
|
|
146
159
|
}, owed: {
|
|
147
160
|
text?: string;
|
|
148
161
|
}[]): Promise<string>;
|
|
162
|
+
/** After a crash, in a turn Codex ended completed: what is said of the first `exec` of its last Codex turn that Codex answered
|
|
163
|
+
* with no script status (as hostFailed says it live), if one is there and no recovery answered it (RECOVERED_END_STREAM: that
|
|
164
|
+
* answer is the adapter's, with no header of its own). Codex can end its turn completed before the harness's interrupt
|
|
165
|
+
* reaches it, and its end line is in the log before the turn's failed end is written: a crash between the two must not
|
|
166
|
+
* leave the recovery to end completed a turn whose script the host never ran. */
|
|
167
|
+
unrunInLastTurn(lines: readonly string[]): Promise<string | undefined>;
|
|
149
168
|
/** A turn ended: the calls answered from the log were for it alone. */
|
|
150
169
|
turnEnded(): void;
|
|
151
170
|
};
|
package/dist/code-mode.js
CHANGED
|
@@ -2,7 +2,7 @@ import { boundKey, boundResult, failureText, openTurn, recoveryNote, recoverySys
|
|
|
2
2
|
// cycle with harness.ts: read repairOutput only inside functions, never at module top level (pinned by "code mode and the harness
|
|
3
3
|
// load in either order and the repair reads repairOutput"). A host loads harness.js first, so at that point none of its const bindings is set.
|
|
4
4
|
import { repairOutput } from "./harness.js";
|
|
5
|
-
import { summarizeRollout } from "./rollout.js";
|
|
5
|
+
import { parseLine, summarizeRollout } from "./rollout.js";
|
|
6
6
|
import { callKey, completedNested, execRecovery, NESTED_CALL, outputTextOf, parkedKey, recoveredFailed, recoveredRowText, recoveryFacts, Scripts, settle, UNWATCHED, waitResumed } from "./scripts.js";
|
|
7
7
|
/** The stream that ties each nested call to the `exec` it ran under: `{ callId, exec, since }` (`since`: the turn it ran in), written before the call runs. */
|
|
8
8
|
export const NESTED_STREAM = "codex.nested";
|
|
@@ -19,6 +19,11 @@ export const SHOWN_STREAM = "codex.nested-shown";
|
|
|
19
19
|
* header for its rollout line to be read by, so the exec's end on the host's record, live or replayed after a later crash, reads this. */
|
|
20
20
|
export const RECOVERED_END_STREAM = "codex.exec-recovered-end";
|
|
21
21
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
22
|
+
/** How long past its yield (the time Codex waits on an `exec` or a `wait` before it answers "Script running") the code-mode host
|
|
23
|
+
* may leave one unanswered, no nested call of it running, before the turn fails. The first `exec` of a session also starts
|
|
24
|
+
* the host: Codex gives up on its handshake by itself at about 30 s, inside this. Codex itself waits on the host without
|
|
25
|
+
* bound: a host that took the session and then went quiet held the turn open until the invocation was stopped. */
|
|
26
|
+
export const HOST_SILENCE_MS = 60_000;
|
|
22
27
|
/** Only a custom tool call named `exec` is code mode's: a host tool that happens to be called `exec` is a function call. */
|
|
23
28
|
const isExec = (d) => d.type === "custom_tool_call" && d.name === "exec";
|
|
24
29
|
/** The item that answers a dangling call that is not an `exec`: of the kind the call asks for (the Responses API refuses an output of the
|
|
@@ -121,6 +126,32 @@ export function codeMode(deps) {
|
|
|
121
126
|
const scripts = new Scripts();
|
|
122
127
|
/** The exec each nested call was admitted under. */
|
|
123
128
|
const execOf = new Map();
|
|
129
|
+
/** The execs this drive answered itself (recoverItems): their output is the session's words, with no script status. */
|
|
130
|
+
const answeredHere = new Set();
|
|
131
|
+
/** The `exec` or `wait` waiting on the code-mode host now (its script, and how long Codex waits before it yields), the
|
|
132
|
+
* nested calls of a script running now, and the timer on the host's silence (HOST_SILENCE_MS). */
|
|
133
|
+
let awaited;
|
|
134
|
+
let nestedLive = 0;
|
|
135
|
+
let silence;
|
|
136
|
+
/** Times the host's silence again: from now, while an `exec` or `wait` waits on it and no nested call runs. */
|
|
137
|
+
const watch = () => {
|
|
138
|
+
if (silence)
|
|
139
|
+
clearTimeout(silence);
|
|
140
|
+
silence = undefined;
|
|
141
|
+
if (!awaited || nestedLive > 0 || !deps.hostFailed)
|
|
142
|
+
return;
|
|
143
|
+
const left = deps.deadline === undefined ? Infinity : Math.max(0, deps.deadline - Date.now());
|
|
144
|
+
const ms = Math.min(awaited.yieldMs, left) + HOST_SILENCE_MS;
|
|
145
|
+
silence = setTimeout(() => {
|
|
146
|
+
silence = undefined;
|
|
147
|
+
deps.hostFailed?.(`the host left it unanswered for ${Math.round(ms / 1000)} s`);
|
|
148
|
+
}, ms);
|
|
149
|
+
silence.unref?.();
|
|
150
|
+
};
|
|
151
|
+
machine.signal?.addEventListener?.("abort", () => {
|
|
152
|
+
awaited = undefined;
|
|
153
|
+
watch();
|
|
154
|
+
}, { once: true });
|
|
124
155
|
/** After a recovery: the `never` calls the log holds a completed outcome for, by tool and arguments, until the turn ends. Each answers one re-issue. */
|
|
125
156
|
const recovered = new Map();
|
|
126
157
|
/** Each `exec` whose row this drive opened on the host's record (SessionIO.ownCalls): its end waits for that write. */
|
|
@@ -164,11 +195,18 @@ export function codeMode(deps) {
|
|
|
164
195
|
// recovery ran or found cut off, failed as the recovery recorded (RECOVERED_END_STREAM).
|
|
165
196
|
machine.onRolloutLine?.((line) => {
|
|
166
197
|
const said = scripts.note(line);
|
|
167
|
-
if (!said
|
|
198
|
+
if (!said)
|
|
199
|
+
return;
|
|
200
|
+
// Codex waits on the host from an exec's line or a wait's on its cell, until an output answers it.
|
|
201
|
+
awaited = said.kind === "opened" || said.kind === "waiting" ? { exec: said.exec, yieldMs: said.yieldMs } : awaited?.exec === said.exec ? undefined : awaited;
|
|
202
|
+
watch();
|
|
203
|
+
if (said.kind === "finished" && said.unrun !== undefined && !answeredHere.has(said.exec))
|
|
204
|
+
deps.hostFailed?.(`Codex answered it with no script status: ${said.unrun}`);
|
|
205
|
+
if (!deps.ownCalls)
|
|
168
206
|
return;
|
|
169
207
|
if (said.kind === "opened")
|
|
170
208
|
void openRow(said.exec, said.input).catch((error) => deps.log("codex: an exec's row was not written", { exec: said.exec, error: String(error) }));
|
|
171
|
-
else
|
|
209
|
+
else if (said.kind === "finished")
|
|
172
210
|
endRow(said.exec, said.output, said.failed);
|
|
173
211
|
});
|
|
174
212
|
/** The script a nested call is tied to now, as the log and the proxy's latest answer have it. */
|
|
@@ -280,11 +318,21 @@ export function codeMode(deps) {
|
|
|
280
318
|
// whether or not the answer was built, so a call whose answer throws leaves none in the bridge for the session's life.
|
|
281
319
|
let extras;
|
|
282
320
|
let answer;
|
|
321
|
+
// A script's nested call running is no silence of the host's: its timer starts again once the call is answered.
|
|
322
|
+
const nested = NESTED_CALL.test(callId);
|
|
323
|
+
if (nested) {
|
|
324
|
+
nestedLive += 1;
|
|
325
|
+
watch();
|
|
326
|
+
}
|
|
283
327
|
try {
|
|
284
328
|
answer = await next(req);
|
|
285
329
|
}
|
|
286
330
|
finally {
|
|
287
331
|
extras = deps.bridge.take?.(callId);
|
|
332
|
+
if (nested) {
|
|
333
|
+
nestedLive -= 1;
|
|
334
|
+
watch();
|
|
335
|
+
}
|
|
288
336
|
}
|
|
289
337
|
// A script's nested call gets the tool's value, images and all, to compute on (scriptAnswer); the model's own
|
|
290
338
|
// call gets the words and the images to look at.
|
|
@@ -379,8 +427,10 @@ export function codeMode(deps) {
|
|
|
379
427
|
}
|
|
380
428
|
// This output finishes the cell's exec, and it carries no status header: that exec ends as its last call says.
|
|
381
429
|
const exec = cell === undefined ? undefined : yielders.execOn(cell);
|
|
382
|
-
if (exec !== undefined)
|
|
430
|
+
if (exec !== undefined) {
|
|
383
431
|
journal.append(RECOVERED_END_STREAM, { exec, failed: recoveredFailed(ties.filter((tie) => tie.exec === exec).map(fact)) });
|
|
432
|
+
answeredHere.add(exec);
|
|
433
|
+
}
|
|
384
434
|
items[i] = withOutputKind(waited !== undefined ? { ...item, output: waited } : pictured !== undefined ? { ...item, output: pictured } : item, c);
|
|
385
435
|
return;
|
|
386
436
|
}
|
|
@@ -389,6 +439,7 @@ export function codeMode(deps) {
|
|
|
389
439
|
const recovered = execRecovery(own, loose, budgets[i]);
|
|
390
440
|
items[i] = { type: "custom_tool_call_output", call_id: c.callId, output: recovered.output };
|
|
391
441
|
journal.append(RECOVERED_END_STREAM, { exec: c.callId, failed: recoveredFailed(own) });
|
|
442
|
+
answeredHere.add(c.callId);
|
|
392
443
|
built.add(i);
|
|
393
444
|
shown.push(...recovered.shown);
|
|
394
445
|
});
|
|
@@ -434,9 +485,30 @@ export function codeMode(deps) {
|
|
|
434
485
|
}
|
|
435
486
|
return recoveryUserTurn(owed.map((s) => s.text ?? ""), note.pending.map((s) => s.text ?? ""));
|
|
436
487
|
},
|
|
488
|
+
/** After a crash, in a turn Codex ended completed: what is said of the first `exec` of its last Codex turn that Codex answered
|
|
489
|
+
* with no script status (as hostFailed says it live), if one is there and no recovery answered it (RECOVERED_END_STREAM: that
|
|
490
|
+
* answer is the adapter's, with no header of its own). Codex can end its turn completed before the harness's interrupt
|
|
491
|
+
* reaches it, and its end line is in the log before the turn's failed end is written: a crash between the two must not
|
|
492
|
+
* leave the recovery to end completed a turn whose script the host never ran. */
|
|
493
|
+
async unrunInLastTurn(lines) {
|
|
494
|
+
const answered = new Set((await journal.read(RECOVERED_END_STREAM)).map((r) => r.data.exec));
|
|
495
|
+
const read = new Scripts();
|
|
496
|
+
let unrun;
|
|
497
|
+
for (const line of lines) {
|
|
498
|
+
const l = parseLine(line);
|
|
499
|
+
if (l?.type === "event_msg" && l.payload?.type === "task_started")
|
|
500
|
+
unrun = undefined;
|
|
501
|
+
const said = read.note(line);
|
|
502
|
+
if (unrun === undefined && said?.kind === "finished" && said.unrun !== undefined && !answered.has(said.exec))
|
|
503
|
+
unrun = `Codex answered it with no script status: ${said.unrun}`;
|
|
504
|
+
}
|
|
505
|
+
return unrun;
|
|
506
|
+
},
|
|
437
507
|
/** A turn ended: the calls answered from the log were for it alone. */
|
|
438
508
|
turnEnded() {
|
|
439
509
|
recovered.clear();
|
|
510
|
+
awaited = undefined;
|
|
511
|
+
watch();
|
|
440
512
|
},
|
|
441
513
|
};
|
|
442
514
|
}
|
package/dist/harness.js
CHANGED
|
@@ -56,6 +56,14 @@ export const SANDBOX_STREAM = 'codex.sandbox';
|
|
|
56
56
|
export const TAIL_STREAM = 'codex.tail';
|
|
57
57
|
/** Where app-server's git looks for a repository: nowhere (it would otherwise read `/workspace/.git`'s config, which the model writes, outside bwrap). */
|
|
58
58
|
const NO_GIT_DIR = '/nonexistent/dh-no-git';
|
|
59
|
+
/** The end of a turn whose script the code-mode host did not run (CodeModeDeps.hostFailed; after a crash, unrunInLastTurn). */
|
|
60
|
+
/** The outcomes hostFailedEnd made: a session turn keeps one across the Codex turns its carried steers run (`turn`). */
|
|
61
|
+
const hostFailures = new WeakSet();
|
|
62
|
+
const hostFailedEnd = (said) => {
|
|
63
|
+
const outcome = { status: 'failed', error: `codex-code-mode-host did not run the model's script: ${said}`.slice(0, 500) };
|
|
64
|
+
hostFailures.add(outcome);
|
|
65
|
+
return outcome;
|
|
66
|
+
};
|
|
59
67
|
/** A built-in call whose `function_call` line never reached the log: declined, never run. */
|
|
60
68
|
const LAGGING = Object.freeze({ run: false, lagging: true });
|
|
61
69
|
/** The rollout tail failed (the file shrank, was replaced, or is not the session user's): what the drive halts with. */
|
|
@@ -379,7 +387,22 @@ export function codexHarness(options = {}) {
|
|
|
379
387
|
}
|
|
380
388
|
mirror();
|
|
381
389
|
};
|
|
382
|
-
const code = codeMode({ ctx, machine, journal, logged, synced, bridge, log, lines, rpc: () => rpc, guard: (p) => guard(p), threadId: () => threadId, tries: BARRIER_TRIES, declined: LAGGING_RESULT, ...(options.ownCalls ? { ownCalls: options.ownCalls } : {}), ...(options.resultBudget ? { budget: options.resultBudget } : {})
|
|
390
|
+
const code = codeMode({ ctx, machine, journal, logged, synced, bridge, log, lines, rpc: () => rpc, guard: (p) => guard(p), threadId: () => threadId, tries: BARRIER_TRIES, declined: LAGGING_RESULT, ...(options.ownCalls ? { ownCalls: options.ownCalls } : {}), ...(options.resultBudget ? { budget: options.resultBudget } : {}),
|
|
391
|
+
// The code-mode host did not run a script (Codex answered it with no script status, or the host went quiet on it):
|
|
392
|
+
// Codex would end the turn completed, or wait on the host without end. The turn is stopped here, and fails naming
|
|
393
|
+
// the host. A turn the harness already stopped (its answer's cut, a command past the deadline) is not: what Codex
|
|
394
|
+
// writes for the exec it stopped ("aborted by user ...") is the stop's.
|
|
395
|
+
deadline,
|
|
396
|
+
hostFailed: (text) => {
|
|
397
|
+
const t = active;
|
|
398
|
+
log('codex: codex-code-mode-host did not run a script: the turn fails', { said: text.slice(0, 200) });
|
|
399
|
+
if (!t || t.hostFailed !== undefined || t.cut || t.stopped)
|
|
400
|
+
return;
|
|
401
|
+
t.hostFailed = text;
|
|
402
|
+
if (t.id)
|
|
403
|
+
void rpc.request('turn/interrupt', { threadId, turnId: t.id }).catch((err) => log('codex: the turn whose script did not run could not be interrupted', { error: String(err?.message ?? err).slice(0, 200) }));
|
|
404
|
+
},
|
|
405
|
+
});
|
|
383
406
|
/**
|
|
384
407
|
* The hard barrier before a `never` call (§5.6): its `function_call`
|
|
385
408
|
* line in the log, flushed, before its intent. Codex's rollout writer is
|
|
@@ -464,6 +487,7 @@ export function codexHarness(options = {}) {
|
|
|
464
487
|
}
|
|
465
488
|
log('codex: a command ran out of time in this invocation: stopped', { callId: id });
|
|
466
489
|
timedOut.add(id);
|
|
490
|
+
t.stopped = true;
|
|
467
491
|
void rpc.request('turn/interrupt', { threadId, turnId: t.id }).catch((err) => {
|
|
468
492
|
timedOut.delete(id);
|
|
469
493
|
log('codex: the turn could not be interrupted', { callId: id, error: String(err?.message ?? err).slice(0, 200) });
|
|
@@ -764,6 +788,9 @@ export function codexHarness(options = {}) {
|
|
|
764
788
|
if (said)
|
|
765
789
|
journal.message({ type: 'message', role: 'assistant', text: said });
|
|
766
790
|
active = undefined;
|
|
791
|
+
// A script the code-mode host did not run: the turn was stopped for it, and fails naming the host.
|
|
792
|
+
if (t.hostFailed !== undefined)
|
|
793
|
+
return hostFailedEnd(t.hostFailed);
|
|
767
794
|
// Its answer cut at the output limit: the turn was interrupted for it, and ended there, its answer unfinished.
|
|
768
795
|
if (t.cut)
|
|
769
796
|
return { status: 'completed', stopReason: 'length', ...(t.cut.outputTokens !== undefined ? { outputTokens: t.cut.outputTokens } : {}), ...(t.cut.capCut ? { capCut: t.cut.capCut } : {}) };
|
|
@@ -792,9 +819,15 @@ export function codexHarness(options = {}) {
|
|
|
792
819
|
*/
|
|
793
820
|
const turn = async (text, clientId) => {
|
|
794
821
|
let outcome = await codexTurn(text, clientId);
|
|
795
|
-
|
|
822
|
+
// A script the code-mode host did not run fails the whole session turn: a carried steer's Codex turn that completes
|
|
823
|
+
// after it does not end the turn completed.
|
|
824
|
+
let hostFailure = hostFailures.has(outcome) ? outcome : undefined;
|
|
825
|
+
for (let s = carry.shift(); s; s = carry.shift()) {
|
|
796
826
|
outcome = await codexTurn(s.text ?? '', s.id);
|
|
797
|
-
|
|
827
|
+
if (!hostFailure && hostFailures.has(outcome))
|
|
828
|
+
hostFailure = outcome;
|
|
829
|
+
}
|
|
830
|
+
await endTurn(hostFailure ?? outcome);
|
|
798
831
|
};
|
|
799
832
|
// Steers that arrive while a turn runs go into it (`turn/steer`).
|
|
800
833
|
const steer = (t) => {
|
|
@@ -871,7 +904,10 @@ export function codexHarness(options = {}) {
|
|
|
871
904
|
const said = note.turn.entries.some((e) => e.data.type === 'message' && e.data.role === 'assistant' && e.data.text === last);
|
|
872
905
|
if (last && !said)
|
|
873
906
|
journal.message({ type: 'message', role: 'assistant', text: last });
|
|
874
|
-
|
|
907
|
+
// A script of the turn the code-mode host did not run: Codex ended it completed before the interrupt reached it, and the
|
|
908
|
+
// crash took the failed end the live turn was given. It ends failed here as it did there.
|
|
909
|
+
const unrun = await code.unrunInLastTurn(lines);
|
|
910
|
+
await endTurn(unrun === undefined ? { status: 'completed' } : hostFailedEnd(unrun));
|
|
875
911
|
}
|
|
876
912
|
else {
|
|
877
913
|
let text = note.text;
|
package/dist/host/machine.js
CHANGED
|
@@ -12,7 +12,9 @@
|
|
|
12
12
|
// - Each child runs in its own process group, which kill() and close() end whole, and dies with this process: started
|
|
13
13
|
// with a watch on its parent (./host's parentWatched, inside the host's confinement), so a host that is SIGKILLed,
|
|
14
14
|
// and runs no close, leaves no Codex behind.
|
|
15
|
-
// - The model proxy (@parcha/agentrun-kernel/host, with the Codex policy) starts with the machine and stops with it.
|
|
15
|
+
// - The model proxy (@parcha/agentrun-kernel/host, with the Codex policy) starts with the machine and stops with it. It
|
|
16
|
+
// checks that the session's key comes from a process this machine spawned, not from one of its children
|
|
17
|
+
// (ModelProxyOptions.peers).
|
|
16
18
|
import { spawn as spawnChild } from "node:child_process";
|
|
17
19
|
import { randomBytes } from "node:crypto";
|
|
18
20
|
import { lstatSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
@@ -20,7 +22,7 @@ import { dirname, isAbsolute, join, normalize, sep } from "node:path";
|
|
|
20
22
|
import { createInterface } from "node:readline";
|
|
21
23
|
import { FileTail } from "@parcha/agentrun-kernel/process";
|
|
22
24
|
import { brandMachine } from "./process.js";
|
|
23
|
-
import { parentWatched, startModelProxy } from "@parcha/agentrun-kernel/host";
|
|
25
|
+
import { parentWatched, peerModeOf, startModelProxy } from "@parcha/agentrun-kernel/host";
|
|
24
26
|
import { codexPolicy, withWebSearches } from "./policy.js";
|
|
25
27
|
import { UNWATCHED } from "../scripts.js";
|
|
26
28
|
import { MODEL_CATALOG_FILE, patchlessCatalog } from "../catalog.js";
|
|
@@ -80,7 +82,7 @@ function inside(base, rel) {
|
|
|
80
82
|
}
|
|
81
83
|
/** Start a child of the session, ended when this process ends. Its git stops at `ceiling` (the session's directory): it never looks for a repository
|
|
82
84
|
* above the session, in a parent the model could write, whose branch and remote would otherwise reach the records. */
|
|
83
|
-
function start(options, from, cwd, ceiling, confine, watch) {
|
|
85
|
+
function start(options, from, cwd, ceiling, confine, watch, admit) {
|
|
84
86
|
// The watch is inside the confinement, so it is set after any switch of user the prefix makes.
|
|
85
87
|
const run = confined(parentWatched({ command: options.command, args: options.args ?? [] }, watch), confine);
|
|
86
88
|
const child = spawnChild(run.command, run.args, {
|
|
@@ -89,6 +91,9 @@ function start(options, from, cwd, ceiling, confine, watch) {
|
|
|
89
91
|
detached: true,
|
|
90
92
|
stdio: ["pipe", "pipe", "pipe"],
|
|
91
93
|
});
|
|
94
|
+
// Named to the model proxy before this turn of the event loop ends, so before the child can have asked it anything.
|
|
95
|
+
if (child.pid !== undefined)
|
|
96
|
+
admit?.(child.pid);
|
|
92
97
|
let stderr = "";
|
|
93
98
|
child.stderr.setEncoding("utf8");
|
|
94
99
|
child.stderr.on("data", (chunk) => {
|
|
@@ -256,6 +261,10 @@ export function localMachine(options) {
|
|
|
256
261
|
for (const listener of cutListeners)
|
|
257
262
|
listener(stop);
|
|
258
263
|
},
|
|
264
|
+
// The key is in app-server's environment, which every process of its user can read: the proxy checks that it comes
|
|
265
|
+
// from a process this machine spawned (app-server), never from that process's children (its code-mode host, a
|
|
266
|
+
// tool). Logged only, unless the host's environment asks to enforce it (PEER_CHECK_ENV).
|
|
267
|
+
peers: { mode: peerModeOf(from), session: ctx.runId },
|
|
259
268
|
}, key);
|
|
260
269
|
const lost = new AbortController();
|
|
261
270
|
const children = new Set();
|
|
@@ -286,7 +295,7 @@ export function localMachine(options) {
|
|
|
286
295
|
spawn: async (spawnOptions) => {
|
|
287
296
|
if (closed)
|
|
288
297
|
throw new Error("agentrun-harness-codex: the session machine is closed");
|
|
289
|
-
const proc = withoutTurnsAfterStop(start(spawnOptions, from, root, ceiling, options.confine, options.parentWatch), ctx.signal);
|
|
298
|
+
const proc = withoutTurnsAfterStop(start(spawnOptions, from, root, ceiling, options.confine, options.parentWatch, (pid) => proxy.admitProcess(pid)), ctx.signal);
|
|
290
299
|
children.add(proc);
|
|
291
300
|
void proc.exited.then(() => children.delete(proc));
|
|
292
301
|
return proc;
|
package/dist/scripts.d.ts
CHANGED
|
@@ -19,18 +19,61 @@ export type Tie = {
|
|
|
19
19
|
} | {
|
|
20
20
|
kind: "none";
|
|
21
21
|
};
|
|
22
|
-
/** What a rollout line says of a script: its `exec` line opened it (`input`, the script
|
|
23
|
-
*
|
|
22
|
+
/** What a rollout line says of a script: its `exec` line opened it (`input`, the script; `yieldMs`, how long Codex waits on
|
|
23
|
+
* it before it yields); it yielded (`cell`: its exec's output, or a `wait`'s, said "Script running with cell ID N"); a `wait`
|
|
24
|
+
* on its cell began (`yieldMs`, that wait's); or an output finished it (`output`, that output's text; `failed`, the script
|
|
25
|
+
* failed, as its status header says; `unrun`, Codex's own words when no script status came back, SCRIPT_STATUS). */
|
|
24
26
|
export type ScriptEvent = {
|
|
25
27
|
kind: "opened";
|
|
26
28
|
exec: string;
|
|
27
29
|
input: string;
|
|
30
|
+
yieldMs: number;
|
|
31
|
+
} | {
|
|
32
|
+
kind: "yielded";
|
|
33
|
+
exec: string;
|
|
34
|
+
cell: string;
|
|
35
|
+
} | {
|
|
36
|
+
kind: "waiting";
|
|
37
|
+
exec: string;
|
|
38
|
+
cell: string;
|
|
39
|
+
yieldMs: number;
|
|
28
40
|
} | {
|
|
29
41
|
kind: "finished";
|
|
30
42
|
exec: string;
|
|
31
43
|
output: string;
|
|
32
44
|
failed: boolean;
|
|
45
|
+
unrun?: string;
|
|
33
46
|
};
|
|
47
|
+
/**
|
|
48
|
+
* The status headers Codex 0.164.0 puts first on an exec's or a wait's output when its code-mode host ran the script and
|
|
49
|
+
* said how it went: it completed, failed (threw, or its cell was gone), was terminated (a `wait` with `terminate`), or still
|
|
50
|
+
* runs (RUNNING). Each is followed by a line of its own ("Wall time ..."). An output that starts with none of them is Codex's
|
|
51
|
+
* own words, not the script's: the host never ran it to an end it could report (it was not there, did not start, answered
|
|
52
|
+
* outside its protocol, or died with the script in it). Codex hands those words to the model and ends the turn completed; the
|
|
53
|
+
* session fails the turn instead (CodeModeDeps.hostFailed), a nested call of that script run first or not. Only the first text
|
|
54
|
+
* of an output is read: what a script prints comes after Codex's header, never before it. test/code-mode-host-down.test.mjs,
|
|
55
|
+
* which CI runs on the pinned binary under AGENTRUN_CODEX_REQUIRE_BINARY=1, finds each header in the pinned binary (Codex
|
|
56
|
+
* builds "Script failed" at run time: a real script that throws holds that one), so a release that rewords one fails there.
|
|
57
|
+
*/
|
|
58
|
+
export declare const SCRIPT_STATUS: readonly string[];
|
|
59
|
+
/**
|
|
60
|
+
* Codex's answers to an `exec` the model wrote wrong (no script, or a bad first-line pragma), each by its fixed part (the words
|
|
61
|
+
* before any detail Codex adds): the model's own mistake, which it reads and can put right in the same turn. No script status
|
|
62
|
+
* comes with them, and none is owed: Codex answers them from the exec's text alone, before it starts or asks the host, so
|
|
63
|
+
* they come back the same with no host beside Codex at all (the sweep and the live rows of test/code-mode-host-down.test.mjs).
|
|
64
|
+
* Each is in the pinned binary (the same test).
|
|
65
|
+
*/
|
|
66
|
+
export declare const EXEC_MISTAKES: readonly string[];
|
|
67
|
+
/** Whether an output's first text is a script status (a header, then its line's end) or Codex's answer to an exec the model
|
|
68
|
+
* wrote wrong: anything else, the host never reported the script. */
|
|
69
|
+
export declare const scriptStatusSaid: (status: string) => boolean;
|
|
70
|
+
/** How long Codex waits on an `exec` before it yields when the script's first-line pragma names no `yield_time_ms` (the pinned
|
|
71
|
+
* Codex's default: a script that never ends yields at about 30 s), and on a `wait` that names none (its tool's description:
|
|
72
|
+
* 10000 ms). */
|
|
73
|
+
export declare const EXEC_YIELD_MS = 30000;
|
|
74
|
+
export declare const WAIT_YIELD_MS = 10000;
|
|
75
|
+
/** The `yield_time_ms` an exec's first-line pragma (`// @exec: {...}`) names, if it names a valid one. */
|
|
76
|
+
export declare function execYieldMs(input: string): number;
|
|
34
77
|
export declare class Scripts {
|
|
35
78
|
private readonly open;
|
|
36
79
|
/** The `exec` calls whose script finished. */
|
package/dist/scripts.js
CHANGED
|
@@ -25,6 +25,57 @@ const RUNNING = /^Script running with cell ID (\S+)/;
|
|
|
25
25
|
/** A script that failed (it threw, or its cell was gone), as its status header says. The rollout's output line has no
|
|
26
26
|
* status field of its own: this header is the only place Codex says how a script ended. */
|
|
27
27
|
const FAILED = /^Script failed\b/;
|
|
28
|
+
/**
|
|
29
|
+
* The status headers Codex 0.164.0 puts first on an exec's or a wait's output when its code-mode host ran the script and
|
|
30
|
+
* said how it went: it completed, failed (threw, or its cell was gone), was terminated (a `wait` with `terminate`), or still
|
|
31
|
+
* runs (RUNNING). Each is followed by a line of its own ("Wall time ..."). An output that starts with none of them is Codex's
|
|
32
|
+
* own words, not the script's: the host never ran it to an end it could report (it was not there, did not start, answered
|
|
33
|
+
* outside its protocol, or died with the script in it). Codex hands those words to the model and ends the turn completed; the
|
|
34
|
+
* session fails the turn instead (CodeModeDeps.hostFailed), a nested call of that script run first or not. Only the first text
|
|
35
|
+
* of an output is read: what a script prints comes after Codex's header, never before it. test/code-mode-host-down.test.mjs,
|
|
36
|
+
* which CI runs on the pinned binary under AGENTRUN_CODEX_REQUIRE_BINARY=1, finds each header in the pinned binary (Codex
|
|
37
|
+
* builds "Script failed" at run time: a real script that throws holds that one), so a release that rewords one fails there.
|
|
38
|
+
*/
|
|
39
|
+
export const SCRIPT_STATUS = Object.freeze(["Script completed", "Script failed", "Script terminated", "Script running with cell ID"]);
|
|
40
|
+
/**
|
|
41
|
+
* Codex's answers to an `exec` the model wrote wrong (no script, or a bad first-line pragma), each by its fixed part (the words
|
|
42
|
+
* before any detail Codex adds): the model's own mistake, which it reads and can put right in the same turn. No script status
|
|
43
|
+
* comes with them, and none is owed: Codex answers them from the exec's text alone, before it starts or asks the host, so
|
|
44
|
+
* they come back the same with no host beside Codex at all (the sweep and the live rows of test/code-mode-host-down.test.mjs).
|
|
45
|
+
* Each is in the pinned binary (the same test).
|
|
46
|
+
*/
|
|
47
|
+
export const EXEC_MISTAKES = Object.freeze([
|
|
48
|
+
"exec expects raw JavaScript source text",
|
|
49
|
+
"exec pragma must be valid JSON with supported fields `yield_time_ms` and `max_output_tokens`: ",
|
|
50
|
+
"exec pragma must be a JSON object with supported fields `yield_time_ms` and `max_output_tokens`",
|
|
51
|
+
"exec pragma must be followed by JavaScript source on subsequent lines",
|
|
52
|
+
"exec pragma only supports `yield_time_ms` and `max_output_tokens`; got `",
|
|
53
|
+
"exec pragma fields `yield_time_ms` and `max_output_tokens` must be non-negative safe integers: ",
|
|
54
|
+
"exec pragma field `yield_time_ms` must be a non-negative safe integer",
|
|
55
|
+
"exec pragma field `max_output_tokens` must be a non-negative safe integer",
|
|
56
|
+
]);
|
|
57
|
+
/** Whether an output's first text is a script status (a header, then its line's end) or Codex's answer to an exec the model
|
|
58
|
+
* wrote wrong: anything else, the host never reported the script. */
|
|
59
|
+
export const scriptStatusSaid = (status) => SCRIPT_STATUS.some((header) => status.startsWith(header) && /^(?:\s|$)/.test(status.slice(header.length))) || EXEC_MISTAKES.some((words) => status.startsWith(words));
|
|
60
|
+
/** How long Codex waits on an `exec` before it yields when the script's first-line pragma names no `yield_time_ms` (the pinned
|
|
61
|
+
* Codex's default: a script that never ends yields at about 30 s), and on a `wait` that names none (its tool's description:
|
|
62
|
+
* 10000 ms). */
|
|
63
|
+
export const EXEC_YIELD_MS = 30_000;
|
|
64
|
+
export const WAIT_YIELD_MS = 10_000;
|
|
65
|
+
/** The `yield_time_ms` an exec's first-line pragma (`// @exec: {...}`) names, if it names a valid one. */
|
|
66
|
+
export function execYieldMs(input) {
|
|
67
|
+
const pragma = /^\s*\/\/\s*@exec:(.*)$/.exec(input.split("\n", 1)[0] ?? "")?.[1];
|
|
68
|
+
try {
|
|
69
|
+
return pragma === undefined ? EXEC_YIELD_MS : yieldOf(JSON.parse(pragma), EXEC_YIELD_MS);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return EXEC_YIELD_MS;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const yieldOf = (args, fallback) => {
|
|
76
|
+
const ms = args?.yield_time_ms;
|
|
77
|
+
return typeof ms === "number" && Number.isSafeInteger(ms) && ms >= 0 ? ms : fallback;
|
|
78
|
+
};
|
|
28
79
|
const textOf = (output) => typeof output === "string" ? output : Array.isArray(output) ? output.map((part) => (typeof part?.text === "string" ? part.text : "")).join("\n") : "";
|
|
29
80
|
export class Scripts {
|
|
30
81
|
open = [];
|
|
@@ -47,36 +98,46 @@ export class Scripts {
|
|
|
47
98
|
return undefined;
|
|
48
99
|
const input = typeof p.input === "string" ? p.input : "";
|
|
49
100
|
this.open.push({ exec: id, input });
|
|
50
|
-
return { kind: "opened", exec: id, input };
|
|
101
|
+
return { kind: "opened", exec: id, input, yieldMs: execYieldMs(input) };
|
|
51
102
|
}
|
|
52
103
|
else if (p.type === "custom_tool_call_output") {
|
|
53
104
|
const at = this.open.findIndex((x) => x.exec === id);
|
|
54
105
|
if (at < 0)
|
|
55
106
|
return undefined;
|
|
56
|
-
if (running)
|
|
57
|
-
this.open[at].cell = running[1];
|
|
58
|
-
else
|
|
107
|
+
if (!running)
|
|
59
108
|
return this.finish(at, p.output);
|
|
109
|
+
this.open[at].cell = running[1];
|
|
110
|
+
return { kind: "yielded", exec: id, cell: running[1] };
|
|
60
111
|
}
|
|
61
112
|
else if (p.type === "function_call" && p.name === "wait") {
|
|
62
113
|
try {
|
|
63
|
-
const
|
|
64
|
-
if (
|
|
65
|
-
|
|
114
|
+
const args = JSON.parse(String(p.arguments));
|
|
115
|
+
if (args?.cell_id === undefined)
|
|
116
|
+
return undefined;
|
|
117
|
+
const cell = String(args.cell_id);
|
|
118
|
+
this.waitsOn.set(id, cell);
|
|
119
|
+
const exec = this.execOn(cell);
|
|
120
|
+
if (exec !== undefined)
|
|
121
|
+
return { kind: "waiting", exec, cell, yieldMs: yieldOf(args, WAIT_YIELD_MS) };
|
|
66
122
|
}
|
|
67
123
|
catch { }
|
|
68
124
|
}
|
|
69
|
-
else if (p.type === "function_call_output" && this.waitsOn.has(id)
|
|
125
|
+
else if (p.type === "function_call_output" && this.waitsOn.has(id)) {
|
|
70
126
|
const at = this.open.findIndex((x) => x.cell === this.waitsOn.get(id));
|
|
71
|
-
if (at
|
|
127
|
+
if (at < 0)
|
|
128
|
+
return undefined;
|
|
129
|
+
if (!running)
|
|
72
130
|
return this.finish(at, p.output);
|
|
131
|
+
this.open[at].cell = running[1];
|
|
132
|
+
return { kind: "yielded", exec: this.open[at].exec, cell: running[1] };
|
|
73
133
|
}
|
|
74
134
|
return undefined;
|
|
75
135
|
}
|
|
76
136
|
finish(at, output) {
|
|
77
137
|
const exec = this.open.splice(at, 1)[0].exec;
|
|
78
138
|
this.done.add(exec);
|
|
79
|
-
|
|
139
|
+
const status = statusOf(output);
|
|
140
|
+
return { kind: "finished", exec, output: textOf(output), failed: FAILED.test(status), ...(scriptStatusSaid(status) ? {} : { unrun: status }) };
|
|
80
141
|
}
|
|
81
142
|
/** The unfinished `exec` that yielded on cell `cell`, if any. */
|
|
82
143
|
execOn(cell) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcha/agentrun-harness-codex",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.81",
|
|
4
4
|
"description": "Codex as an AgentRun harness: a tool-less Codex app-server behind a loopback model proxy, durable in AgentRun's own store, with the host's tools as its only tools.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agentrun",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"test": "node --test --test-concurrency=8 'test/*.test.mjs'"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@parcha/agentrun-kernel": "0.1.0-beta.
|
|
47
|
+
"@parcha/agentrun-kernel": "0.1.0-beta.81"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"@openai/codex": "0.164.0"
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@openai/codex": "0.164.0",
|
|
59
|
-
"@parcha/agentrun-dsl": "0.1.0-beta.
|
|
59
|
+
"@parcha/agentrun-dsl": "0.1.0-beta.81"
|
|
60
60
|
},
|
|
61
61
|
"repository": {
|
|
62
62
|
"type": "git",
|