owenloop 0.5.1 → 0.5.3
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 +30 -0
- package/README.md +9 -2
- package/dist/packages/work/src/agent/brief.d.ts +21 -0
- package/dist/packages/work/src/agent/brief.js +42 -1
- package/dist/packages/work/src/agent/capability-model.d.ts +123 -0
- package/dist/packages/work/src/agent/capability-model.js +148 -0
- package/dist/packages/work/src/agent/loop.d.ts +47 -4
- package/dist/packages/work/src/agent/loop.js +83 -53
- package/dist/packages/work/src/exec/loop.d.ts +17 -0
- package/dist/packages/work/src/exec/loop.js +195 -34
- package/dist/packages/work/src/exec/runner.js +15 -5
- package/dist/packages/work/src/harness/claude.js +69 -9
- package/dist/packages/work/src/harness/codex.js +56 -8
- package/dist/packages/work/src/harness/contract.d.ts +65 -1
- package/dist/packages/work/src/harness/contract.js +56 -0
- package/dist/packages/work/src/harness/session-store.d.ts +3 -1
- package/dist/packages/work/src/harness/session-store.js +46 -22
- package/dist/packages/work/src/hold/mcp.js +9 -2
- package/dist/packages/work/src/hub/client.d.ts +7 -1
- package/dist/packages/work/src/hub/client.js +1 -0
- package/dist/packages/work/src/hub/types.d.ts +71 -3
- package/dist/packages/work/src/roles/agent-run.js +8 -9
- package/dist/packages/work/src/roles/hold.d.ts +2 -0
- package/dist/packages/work/src/roles/hold.js +39 -8
- package/dist/packages/work/src/roles/settings.js +18 -6
- package/dist/packages/work/src/roles/shift.js +13 -1
- package/dist/packages/work/src/settings/settings.d.ts +60 -7
- package/dist/packages/work/src/settings/settings.js +69 -22
- package/dist/packages/work/src/shift/logretention.d.ts +253 -0
- package/dist/packages/work/src/shift/logretention.js +407 -0
- package/dist/packages/work/src/shift/logsink.d.ts +14 -0
- package/dist/packages/work/src/shift/logsink.js +98 -0
- package/dist/packages/work/src/shift/loop.d.ts +18 -1
- package/dist/packages/work/src/shift/loop.js +250 -12
- package/dist/packages/work/src/shift/protocol.d.ts +124 -1
- package/dist/packages/work/src/shift/protocol.js +14 -0
- package/dist/packages/work/src/shift/runtime.d.ts +14 -0
- package/dist/packages/work/src/shift/runtime.js +229 -6
- package/dist/packages/work/src/shift/server.d.ts +13 -0
- package/dist/packages/work/src/shift/server.js +61 -36
- package/dist/packages/work/src/shift/spawn.d.ts +78 -5
- package/dist/packages/work/src/shift/spawn.js +158 -12
- package/dist/packages/work/src/shift/state.d.ts +25 -0
- package/dist/packages/work/src/shift/state.js +29 -5
- package/dist/packages/work/src/shift/truncate.d.ts +35 -0
- package/dist/packages/work/src/shift/truncate.js +73 -0
- package/dist/packages/work/src/submit-proof.d.ts +31 -6
- package/dist/packages/work/src/submit-proof.js +33 -7
- package/dist/packages/work/src/submit-value.d.ts +48 -0
- package/dist/packages/work/src/submit-value.js +118 -0
- package/dist/packages/work/src/usage.d.ts +1 -1
- package/dist/packages/work/src/usage.js +11 -0
- package/dist/src/bundle/runtime.d.ts +1 -1
- package/dist/src/bundle/runtime.js +9 -0
- package/dist/src/capabilities.d.ts +93 -0
- package/dist/src/capabilities.js +101 -0
- package/dist/src/cli.js +37 -11
- package/dist/src/crypto/records.d.ts +3 -0
- package/dist/src/crypto/records.js +11 -0
- package/dist/src/defs.js +192 -4
- package/dist/src/engine.d.ts +112 -3
- package/dist/src/engine.js +224 -28
- package/dist/src/hub.d.ts +27 -11
- package/dist/src/hub.js +24 -18
- package/dist/src/index.d.ts +3 -1
- package/dist/src/index.js +4 -1
- package/dist/src/mcp/serve.js +11 -4
- package/dist/src/model.d.ts +36 -0
- package/dist/src/model.js +46 -0
- package/dist/src/schemas/index.d.ts +17 -0
- package/dist/src/schemas/order.v1.schema.json +17 -3
- package/dist/src/store.d.ts +33 -1
- package/dist/src/store.js +57 -4
- package/dist/src/types.d.ts +102 -2
- package/docs/agent-runner.md +26 -1
- package/docs/authoring.md +7 -0
- package/docs/bundles.md +103 -3
- package/docs/cli.md +127 -9
- package/docs/crypto.md +23 -7
- package/docs/shift-logs.md +642 -0
- package/docs/wire-contracts.md +34 -19
- package/package.json +1 -1
- package/plugins/claude-code/plugin/.claude-plugin/plugin.json +1 -1
- package/plugins/claude-code/plugin/.mcp.json +1 -1
- package/plugins/codex/plugins/owenloop/.codex-plugin/plugin.json +1 -1
- package/plugins/codex/plugins/owenloop/.mcp.json +1 -1
- package/dist/packages/work/src/agent/model-policy.d.ts +0 -49
- package/dist/packages/work/src/agent/model-policy.js +0 -138
|
@@ -62,7 +62,7 @@ import { isResumeUnavailable } from "../harness/contract.js";
|
|
|
62
62
|
import { preflightStepPermissions } from "../harness/permissions.js";
|
|
63
63
|
import { orderId } from "../harness/session-store.js";
|
|
64
64
|
import { buildOwenloopMcp, renderBrief, renderRejection, renderReplayBrief, } from "./brief.js";
|
|
65
|
-
import {
|
|
65
|
+
import { resolveCapabilityModel, } from "./capability-model.js";
|
|
66
66
|
/** Default confirm-phase cadence: one `get_order` per second. */
|
|
67
67
|
export const DEFAULT_CONFIRM_INTERVAL_MS = 1_000;
|
|
68
68
|
/** Default confirm-phase bound: give the hub 15s to show the submit's outcome. */
|
|
@@ -70,60 +70,20 @@ export const DEFAULT_SUBMIT_GRACE_MS = 15_000;
|
|
|
70
70
|
function errMsg(e) {
|
|
71
71
|
return e instanceof Error ? e.message : String(e);
|
|
72
72
|
}
|
|
73
|
-
function laneFromPacket(packet) {
|
|
74
|
-
const plan = packet.consumes?.['plan'];
|
|
75
|
-
if (plan === null || typeof plan !== 'object' || Array.isArray(plan))
|
|
76
|
-
return undefined;
|
|
77
|
-
const lane = plan['lane'];
|
|
78
|
-
return lane === 'express' || lane === 'standard' || lane === 'deep' ? lane : undefined;
|
|
79
|
-
}
|
|
80
|
-
function escalationFromPacket(packet, extensionKey) {
|
|
81
|
-
const extension = packet.x?.[extensionKey];
|
|
82
|
-
if (extension === null || typeof extension !== 'object' || Array.isArray(extension))
|
|
83
|
-
return undefined;
|
|
84
|
-
if (!('escalation' in extension))
|
|
85
|
-
return undefined;
|
|
86
|
-
const raw = extension['escalation'];
|
|
87
|
-
if (raw === null || typeof raw !== 'object' || Array.isArray(raw))
|
|
88
|
-
return undefined;
|
|
89
|
-
const bag = raw;
|
|
90
|
-
const model = typeof bag['model'] === 'string' && bag['model'] !== '' ? bag['model'] : undefined;
|
|
91
|
-
const attempts = typeof bag['attempts'] === 'number' && Number.isInteger(bag['attempts']) && bag['attempts'] > 0
|
|
92
|
-
? bag['attempts']
|
|
93
|
-
: undefined;
|
|
94
|
-
return {
|
|
95
|
-
...(model !== undefined ? { model } : {}),
|
|
96
|
-
...(attempts !== undefined ? { attempts } : {}),
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
73
|
/**
|
|
100
|
-
*
|
|
101
|
-
* outputs, with consult paths excluded.
|
|
74
|
+
* Resolve an order's composed capabilities against this machine's map.
|
|
102
75
|
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
* later firing of the step, permanently, on a producer that was never failing.
|
|
76
|
+
* ONLY `packet.capabilities` is consulted. `packet.model` — the def's authored
|
|
77
|
+
* tier name under the retired scheme — is deliberately ignored: a tier is an
|
|
78
|
+
* abstract rung with no meaning left, and reading it as a model id would put a
|
|
79
|
+
* literal `strong` on the wire to a vendor.
|
|
108
80
|
*/
|
|
109
|
-
function
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const rejects = owed.judgmentRejects;
|
|
116
|
-
if (typeof rejects === 'number' && Number.isFinite(rejects) && rejects > max)
|
|
117
|
-
max = rejects;
|
|
118
|
-
}
|
|
119
|
-
return max;
|
|
120
|
-
}
|
|
121
|
-
function modelForPacket(packet, policy) {
|
|
122
|
-
if (packet.model === undefined)
|
|
123
|
-
return undefined;
|
|
124
|
-
if (policy === undefined)
|
|
125
|
-
return { model: packet.model };
|
|
126
|
-
return pickModel(packet.model, laneFromPacket(packet), judgmentRejectsFromPacket(packet), policy, undefined, escalationFromPacket(packet, policy.escalationExtensionKey));
|
|
81
|
+
export function resolveOrderRouting(packet, map) {
|
|
82
|
+
const capabilities = packet.capabilities ?? [];
|
|
83
|
+
if (capabilities.length === 0)
|
|
84
|
+
return { kind: 'unrouted' };
|
|
85
|
+
const resolution = map === undefined ? undefined : resolveCapabilityModel(map, capabilities);
|
|
86
|
+
return resolution === undefined ? { kind: 'refused', capabilities } : { kind: 'resolved', resolution };
|
|
127
87
|
}
|
|
128
88
|
/**
|
|
129
89
|
* Poll `get_order` at `intervalMs` for at most `graceMs`, answering ONE
|
|
@@ -315,6 +275,40 @@ export function createAgentRunLoop(opts) {
|
|
|
315
275
|
return;
|
|
316
276
|
}
|
|
317
277
|
}
|
|
278
|
+
/**
|
|
279
|
+
* Tell the hub what this shift resolved the order to (plan §6), before the
|
|
280
|
+
* harness launches.
|
|
281
|
+
*
|
|
282
|
+
* BEST EFFORT, ALWAYS. A hub that is unreachable, older than the verb, or
|
|
283
|
+
* simply erroring must never be able to stop work the shift is otherwise
|
|
284
|
+
* ready to do — the report is observability, not a gate, and the hub's own
|
|
285
|
+
* verb says so. A failure is logged and swallowed.
|
|
286
|
+
*
|
|
287
|
+
* An `unrouted` order is not reported at all: the payload's `capability` is
|
|
288
|
+
* required and non-empty on the hub side, and there is no capability to name.
|
|
289
|
+
*/
|
|
290
|
+
async function reportResolution(routing, harness) {
|
|
291
|
+
if (routing.kind === 'unrouted')
|
|
292
|
+
return;
|
|
293
|
+
const payload = routing.kind === 'resolved'
|
|
294
|
+
? {
|
|
295
|
+
capability: routing.resolution.capability,
|
|
296
|
+
match: routing.resolution.match,
|
|
297
|
+
model: routing.resolution.model,
|
|
298
|
+
effort: routing.resolution.effort,
|
|
299
|
+
harness,
|
|
300
|
+
}
|
|
301
|
+
: // The refusal names the capability the order was OFFERED under — the
|
|
302
|
+
// first one — because that is the row an operator has to add. Model
|
|
303
|
+
// and effort are absent: nothing was selected.
|
|
304
|
+
{ capability: routing.capabilities[0] ?? '', match: 'refused', harness };
|
|
305
|
+
try {
|
|
306
|
+
await opts.hub.reportResolution({ workflow, run: runId, resolution: payload });
|
|
307
|
+
}
|
|
308
|
+
catch (e) {
|
|
309
|
+
opts.err(`owenloop work agent-run: reporting the resolution for ${order} failed (continuing): ${errMsg(e)}`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
318
312
|
/** Release and finish. Used by every path that hands the order back. */
|
|
319
313
|
async function releaseWith(reason, outcome) {
|
|
320
314
|
lease.stop(reason);
|
|
@@ -404,6 +398,10 @@ export function createAgentRunLoop(opts) {
|
|
|
404
398
|
origin: opts.origin,
|
|
405
399
|
account: opts.account,
|
|
406
400
|
...(opts.shiftId !== undefined ? { shiftId: opts.shiftId } : {}),
|
|
401
|
+
// Straight from the packet, not from settings: the modifier is the hub's
|
|
402
|
+
// statement about the RUN, and a shift has no say in it.
|
|
403
|
+
...(packet.modifier !== undefined ? { modifier: packet.modifier } : {}),
|
|
404
|
+
...(packet.escalated === true ? { escalated: true } : {}),
|
|
407
405
|
};
|
|
408
406
|
// Permissions arrive PRE-NORMALIZED in the spec. `prepare` ran
|
|
409
407
|
// `normalizeStepPermissions` over the step's `x.harness` options at cache
|
|
@@ -424,6 +422,36 @@ export function createAgentRunLoop(opts) {
|
|
|
424
422
|
}
|
|
425
423
|
return releaseWith('incompatible-harness-policy', 'incompatible-harness-policy');
|
|
426
424
|
}
|
|
425
|
+
// ---- ROUTING: which model serves this order, and telling the hub --------
|
|
426
|
+
//
|
|
427
|
+
// DELIBERATELY THE LAST GATE BEFORE DISPATCH, and deliberately AFTER every
|
|
428
|
+
// launch gate above (misroute, consumed verification, step spec, adapter,
|
|
429
|
+
// harness policy). Plan §3: the launch gates block on a malformed order
|
|
430
|
+
// before model selection is even attempted, so a refusal never has to be
|
|
431
|
+
// explained in terms of a model the order was never going to reach.
|
|
432
|
+
const routing = resolveOrderRouting(packet, opts.capabilityModels);
|
|
433
|
+
// Plan §6: the record must exist BEFORE tokens are spent, which is why this
|
|
434
|
+
// is a dedicated verb and not a field on the next heartbeat. It is reported
|
|
435
|
+
// on the refusal path too — a refusal is a true observation of what this
|
|
436
|
+
// shift resolved, and the whole point of the report is that the hub can say
|
|
437
|
+
// why an order did or did not run.
|
|
438
|
+
await reportResolution(routing, resolution.id);
|
|
439
|
+
if (routing.kind === 'refused') {
|
|
440
|
+
opts.err(`owenloop work agent-run: no settings row for ${order} — the order's capabilities ` +
|
|
441
|
+
`[${routing.capabilities.join(', ')}] match neither an exact row nor a bare name row in ` +
|
|
442
|
+
`'capabilityModels'. Add a row for one of them (a bare row such as ` +
|
|
443
|
+
`'${routing.capabilities[0]?.split(':')[0] ?? ''}' covers every modifier) and restart the shift. ` +
|
|
444
|
+
`Releasing — this shift will not pick a model it was not told to use.`);
|
|
445
|
+
return releaseWith('unresolvable-capability', 'unresolvable-capability');
|
|
446
|
+
}
|
|
447
|
+
if (routing.kind === 'unrouted') {
|
|
448
|
+
opts.err(`owenloop work agent-run: ${order} carries no capabilities, so no settings row applies — ` +
|
|
449
|
+
`running on '${resolution.id}''s own default model.`);
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
opts.out(`owenloop work agent-run: ${order} resolved '${routing.resolution.capability}' ` +
|
|
453
|
+
`(${routing.resolution.match} row) → ${routing.resolution.model} at ${routing.resolution.effort}`);
|
|
454
|
+
}
|
|
427
455
|
// ---- PHASE 4: resume or cold replay? -----------------------------------
|
|
428
456
|
//
|
|
429
457
|
// The delta first, because "is there anything new to say?" is one of the
|
|
@@ -485,7 +513,9 @@ export function createAgentRunLoop(opts) {
|
|
|
485
513
|
deliveredReasonAt = delta.deliveredReasonAt;
|
|
486
514
|
}
|
|
487
515
|
const owenloopMcp = buildOwenloopMcp(spec);
|
|
488
|
-
const resolvedModel =
|
|
516
|
+
const resolvedModel = routing.kind === 'resolved'
|
|
517
|
+
? { model: routing.resolution.model, effort: routing.resolution.effort }
|
|
518
|
+
: undefined;
|
|
489
519
|
const deliverArgs = {
|
|
490
520
|
cwd: recordCwd,
|
|
491
521
|
owenloopMcp,
|
|
@@ -4,6 +4,23 @@ import type { CommandRunner } from './runner.ts';
|
|
|
4
4
|
import type { InstructionResolver } from './instructions.ts';
|
|
5
5
|
import { type SubmissionKeyManager } from '../submit-proof.ts';
|
|
6
6
|
import type { SshProcessAdapter } from '../../../../src/crypto/ssh.ts';
|
|
7
|
+
/**
|
|
8
|
+
* The largest serialized `consumes` payload delivered inline in
|
|
9
|
+
* `OWENLOOP_CONSUMES`; anything strictly larger goes to a file named by
|
|
10
|
+
* `OWENLOOP_CONSUMES_FILE`. Measured with `Buffer.byteLength(json, 'utf8')` —
|
|
11
|
+
* never `json.length`, which counts UTF-16 code units and under-reports every
|
|
12
|
+
* multi-byte character.
|
|
13
|
+
*
|
|
14
|
+
* 64 KiB, chosen against the binding OS limit rather than a round number:
|
|
15
|
+
* Linux `execve` enforces `MAX_ARG_STRLEN`, a hard PER-ENTRY cap of 131072
|
|
16
|
+
* bytes (32 × 4 KiB pages) independent of the total `ARG_MAX`; macOS has no
|
|
17
|
+
* per-entry cap but shares ~1 MB of `ARG_MAX` across argv AND the whole
|
|
18
|
+
* environment block, and the child environment here starts from a full
|
|
19
|
+
* inherited `process.env`. 64 KiB clears the Linux per-entry cap with room for
|
|
20
|
+
* the `OWENLOOP_CONSUMES=` prefix and leaves the rest of the macOS budget to
|
|
21
|
+
* the inherited block.
|
|
22
|
+
*/
|
|
23
|
+
export declare const CONSUMES_INLINE_MAX_BYTES = 65536;
|
|
7
24
|
/** Discriminated result of an exec run; the role maps these to exit codes. */
|
|
8
25
|
export type ExecOutcome = 'submitted' | 'completed' | 'misroute' | 'unresolved-instructions' | 'killed' | 'lease-lost' | 'ownership-error' | 'hub-unreachable' | 'submit-rejected' | 'submit-failed' | 'rejected' | 'judge-rejected' | 'judge-no-verdict' | 'reject-failed' | 'stopped';
|
|
9
26
|
export interface ExecLoopOptions {
|
|
@@ -39,15 +39,117 @@
|
|
|
39
39
|
* The role (`src/roles/exec.ts`) maps each `ExecOutcome` to an exit code.
|
|
40
40
|
*/
|
|
41
41
|
import { createHash } from 'node:crypto';
|
|
42
|
+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
43
|
+
import { tmpdir } from 'node:os';
|
|
44
|
+
import { join, resolve } from 'node:path';
|
|
42
45
|
import { createLeaseLoop } from "../lease/loop.js";
|
|
43
46
|
import { parsePayloadLine } from "./payload.js";
|
|
44
47
|
import { buildReceipt } from "./receipt.js";
|
|
45
48
|
import { buildSubmitProof } from "../submit-proof.js";
|
|
46
49
|
/** sha256 of the empty byte string — the hash for a run with no captured output. */
|
|
47
50
|
const EMPTY_HASH = createHash('sha256').digest('hex');
|
|
51
|
+
/**
|
|
52
|
+
* The largest serialized `consumes` payload delivered inline in
|
|
53
|
+
* `OWENLOOP_CONSUMES`; anything strictly larger goes to a file named by
|
|
54
|
+
* `OWENLOOP_CONSUMES_FILE`. Measured with `Buffer.byteLength(json, 'utf8')` —
|
|
55
|
+
* never `json.length`, which counts UTF-16 code units and under-reports every
|
|
56
|
+
* multi-byte character.
|
|
57
|
+
*
|
|
58
|
+
* 64 KiB, chosen against the binding OS limit rather than a round number:
|
|
59
|
+
* Linux `execve` enforces `MAX_ARG_STRLEN`, a hard PER-ENTRY cap of 131072
|
|
60
|
+
* bytes (32 × 4 KiB pages) independent of the total `ARG_MAX`; macOS has no
|
|
61
|
+
* per-entry cap but shares ~1 MB of `ARG_MAX` across argv AND the whole
|
|
62
|
+
* environment block, and the child environment here starts from a full
|
|
63
|
+
* inherited `process.env`. 64 KiB clears the Linux per-entry cap with room for
|
|
64
|
+
* the `OWENLOOP_CONSUMES=` prefix and leaves the rest of the macOS budget to
|
|
65
|
+
* the inherited block.
|
|
66
|
+
*/
|
|
67
|
+
export const CONSUMES_INLINE_MAX_BYTES = 65_536;
|
|
48
68
|
function errMsg(e) {
|
|
49
69
|
return e instanceof Error ? e.message : String(e);
|
|
50
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Put the order's consumed inputs on `childEnv`, and return the temp DIRECTORY
|
|
73
|
+
* holding the overflow file — or `undefined` when the payload went inline.
|
|
74
|
+
*
|
|
75
|
+
* The contract a command script sees, and the reason it is two variables:
|
|
76
|
+
*
|
|
77
|
+
* - `OWENLOOP_CONSUMES` carries `JSON.stringify(order.consumes ?? {})`
|
|
78
|
+
* verbatim whenever that fits within `CONSUMES_INLINE_MAX_BYTES`. It is set
|
|
79
|
+
* even when there are no consumed inputs — the value is then `{}`, not an
|
|
80
|
+
* absent variable.
|
|
81
|
+
* - `OWENLOOP_CONSUMES_FILE` names a `0600` UTF-8 file holding that same JSON
|
|
82
|
+
* when it does not fit.
|
|
83
|
+
*
|
|
84
|
+
* Exactly ONE of the two is present on any spawn, because this function always
|
|
85
|
+
* assigns one and `delete`s the other. That mutual exclusion is what lets the
|
|
86
|
+
* presence of `OWENLOOP_CONSUMES_FILE` act as the discriminator with no third
|
|
87
|
+
* "mode" variable, and it is required anyway by the collision rule below.
|
|
88
|
+
*
|
|
89
|
+
* COLLISION RULE (the same reasoning as `OWENLOOP_WORKFLOW`/`OWENLOOP_RUN` at
|
|
90
|
+
* the spawn site): `childEnv` starts from `process.env`, and a shift can itself
|
|
91
|
+
* have been launched from inside another command step, so both names may
|
|
92
|
+
* already carry the PARENT order's values — a stale inputs object in the inline
|
|
93
|
+
* case, and in the file case a path whose directory has already been removed.
|
|
94
|
+
* Neither name is ever conditionally assigned.
|
|
95
|
+
*
|
|
96
|
+
* The JSON is the raw `order.consumes` object with no envelope, so a command
|
|
97
|
+
* script and an agent step see the identical shape. Key omission is preserved:
|
|
98
|
+
* an input that was declared but never produced is ABSENT from the object, not
|
|
99
|
+
* present as `null`, and a script tests for it with `'key' in consumes`.
|
|
100
|
+
*/
|
|
101
|
+
function deliverConsumes(childEnv, consumes) {
|
|
102
|
+
// `order.consumes` arrives JSON-decoded off the wire, so there is no
|
|
103
|
+
// realistic cycle or BigInt to throw on. The throw is still surfaced rather
|
|
104
|
+
// than swallowed: leaving the variable unset would push a script back onto
|
|
105
|
+
// deriving its context from its cwd — the exact failure this delivery exists
|
|
106
|
+
// to remove — so the caller turns it into a machinery failure and no child is
|
|
107
|
+
// spawned.
|
|
108
|
+
let json;
|
|
109
|
+
try {
|
|
110
|
+
json = JSON.stringify(consumes ?? {});
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
throw new Error(`cannot serialize the order's consumed inputs for OWENLOOP_CONSUMES: ${errMsg(e)}`);
|
|
114
|
+
}
|
|
115
|
+
if (Buffer.byteLength(json, 'utf8') <= CONSUMES_INLINE_MAX_BYTES) {
|
|
116
|
+
childEnv['OWENLOOP_CONSUMES'] = json;
|
|
117
|
+
delete childEnv['OWENLOOP_CONSUMES_FILE'];
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
// `mkdtempSync` rather than a composed run/step/random filename: several
|
|
121
|
+
// shifts run concurrently on one machine, and this gives a collision-free
|
|
122
|
+
// `0700` directory without owning a naming scheme. The file is `0600`
|
|
123
|
+
// because its content is agent-produced artifact data.
|
|
124
|
+
const dir = mkdtempSync(join(tmpdir(), 'owenloop-consumes-'));
|
|
125
|
+
try {
|
|
126
|
+
const file = join(dir, 'consumes.json');
|
|
127
|
+
writeFileSync(file, json, { encoding: 'utf8', mode: 0o600 });
|
|
128
|
+
childEnv['OWENLOOP_CONSUMES_FILE'] = file;
|
|
129
|
+
delete childEnv['OWENLOOP_CONSUMES'];
|
|
130
|
+
return dir;
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
// The directory exists but the caller will never learn its path, so remove
|
|
134
|
+
// it here rather than leaking it; the caller's `finally` only covers a
|
|
135
|
+
// directory this function returned.
|
|
136
|
+
removeConsumesDir(dir);
|
|
137
|
+
throw e;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/** Best-effort removal of an overflow directory; a cleanup failure never fails a step. */
|
|
141
|
+
function removeConsumesDir(dir) {
|
|
142
|
+
if (dir === undefined)
|
|
143
|
+
return;
|
|
144
|
+
try {
|
|
145
|
+
rmSync(dir, { recursive: true, force: true });
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
// Swallowed on purpose: the child has already exited and its receipt is
|
|
149
|
+
// authoritative. A temp-directory that outlives the run is a tidiness
|
|
150
|
+
// problem, not a result.
|
|
151
|
+
}
|
|
152
|
+
}
|
|
51
153
|
export function createExecLoop(opts) {
|
|
52
154
|
const { hub, runner, workflow } = opts;
|
|
53
155
|
const runId = opts.run;
|
|
@@ -282,43 +384,102 @@ export function createExecLoop(opts) {
|
|
|
282
384
|
return 'unresolved-instructions';
|
|
283
385
|
}
|
|
284
386
|
// Run the command and race it against the lease going terminal.
|
|
285
|
-
|
|
387
|
+
// `consumesDir` holds the OWENLOOP_CONSUMES_FILE overflow directory when
|
|
388
|
+
// the serialized inputs did not fit inline. The `finally` below removes it
|
|
389
|
+
// on every path out of this block, including a spawn that threw.
|
|
390
|
+
//
|
|
391
|
+
// On the RECEIPT path the removal follows the child's exit, because a
|
|
392
|
+
// script may read the file late in its run. On the LEASE-TERMINAL path it
|
|
393
|
+
// does not: `cmd.kill()` (see `exec/runner.ts`) posts SIGTERM, races `done`
|
|
394
|
+
// against the grace timer, then posts SIGKILL and returns WITHOUT awaiting
|
|
395
|
+
// `done`, so this unlink can precede full reaping of the killed process
|
|
396
|
+
// group. That is deliberate and harmless — the run is abandoned and submits
|
|
397
|
+
// no receipt — but it is not a guarantee to build on. Do not add work here
|
|
398
|
+
// that assumes the child is gone.
|
|
399
|
+
let consumesDir;
|
|
286
400
|
try {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
401
|
+
let cmd;
|
|
402
|
+
try {
|
|
403
|
+
// spawn's env replaces the child environment. Start from the actual exec
|
|
404
|
+
// process environment so config-only opts.env cannot strip PATH/HOME, then
|
|
405
|
+
// explicitly remove bundle provenance for loose definitions.
|
|
406
|
+
const childEnv = { ...process.env };
|
|
407
|
+
if (resolvedBundleDir === undefined)
|
|
408
|
+
delete childEnv['OWENLOOP_BUNDLE_DIR'];
|
|
409
|
+
else
|
|
410
|
+
childEnv['OWENLOOP_BUNDLE_DIR'] = resolvedBundleDir;
|
|
411
|
+
// Run identity is engine-derived from ExecLoopOptions, never a consumed
|
|
412
|
+
// artifact value, and always present. The consumed inputs that follow ARE
|
|
413
|
+
// artifact values, and they are admitted here only because
|
|
414
|
+
// `resolveCommand` above already ran the consume-side verification gate
|
|
415
|
+
// (`hardRule: true`) and refused the order outright on absent,
|
|
416
|
+
// unverifiable, or invalid evidence. They travel in the environment
|
|
417
|
+
// block and nowhere else — never in argv and never in the command text,
|
|
418
|
+
// which is returned verbatim from the verified store and is not
|
|
419
|
+
// interpolated. All four names below overwrite inherited values so a
|
|
420
|
+
// nested exec sees its own order rather than its parent's.
|
|
421
|
+
childEnv['OWENLOOP_WORKFLOW'] = workflow;
|
|
422
|
+
childEnv['OWENLOOP_RUN'] = runId;
|
|
423
|
+
consumesDir = deliverConsumes(childEnv, order.consumes);
|
|
424
|
+
// The order carries no workdir when the step declared neither `workdir:`
|
|
425
|
+
// nor `workdirFrom:` (the hub resolves `workdirFrom` and ships the
|
|
426
|
+
// result, so an absent field IS that signal). Substituting the shift's
|
|
427
|
+
// launch directory silently is how a run ends up operating on an
|
|
428
|
+
// unintended tree with nothing in the output saying so, hence the
|
|
429
|
+
// record. It stays a WARNING: `provisioner` creates the worktree its
|
|
430
|
+
// successors use and `deprovisioner` deletes its own, so both
|
|
431
|
+
// legitimately have no workdir to resolve, and a throw here would break
|
|
432
|
+
// every delivery run the moment it shipped.
|
|
433
|
+
//
|
|
434
|
+
// WHERE THIS LINE ACTUALLY GOES. `opts.err` is this process's stderr.
|
|
435
|
+
// Run by hand, that is the operator's terminal. Dispatched by
|
|
436
|
+
// `owenloop shift start`, `createDefaultSpawner` (`src/shift/spawn.ts`)
|
|
437
|
+
// now attaches fd 1 and fd 2 to `<log-dir>/<run>.log`, so this warning
|
|
438
|
+
// is on disk under the run id it belongs to and outlives both the
|
|
439
|
+
// worker and the shift. See `docs/shift-logs.md`.
|
|
440
|
+
//
|
|
441
|
+
// It was previously discarded: every worker launched with
|
|
442
|
+
// `stdio: ['ignore','ignore','ignore']`, so a shift-dispatched exec
|
|
443
|
+
// wrote this line to `/dev/null`. That was the blocker on turning this
|
|
444
|
+
// warning into a throw — an unseen warning is not a migration notice.
|
|
445
|
+
// The delivery half is now done; the enforcement release is still a
|
|
446
|
+
// separate decision. `docs/bundles.md` ("Working directory for command
|
|
447
|
+
// steps") tracks the same state for bundle authors.
|
|
448
|
+
//
|
|
449
|
+
// The RECEIPT still does not carry it: the receipt captures the
|
|
450
|
+
// *command's* streams, and this line is the exec worker's own.
|
|
451
|
+
const cwd = order.workdir ?? opts.cwd;
|
|
452
|
+
if (order.workdir === undefined) {
|
|
453
|
+
opts.err(`owenloop work exec: step '${order.step}' (${workflow}/${runId}) declared neither workdir nor ` +
|
|
454
|
+
`workdirFrom — inheriting the shift launch directory '${resolve(cwd)}' as the command's cwd. ` +
|
|
455
|
+
'A future release will require a step to declare this inheritance explicitly.');
|
|
456
|
+
}
|
|
457
|
+
const startOptions = { cwd, env: childEnv };
|
|
458
|
+
cmd = runner.start(resolvedCommand, startOptions);
|
|
459
|
+
}
|
|
460
|
+
catch (e) {
|
|
461
|
+
return submitReceipt(machineryFailure(e), order, resolvedCommand);
|
|
462
|
+
}
|
|
463
|
+
running = cmd;
|
|
464
|
+
opts.out(`owenloop work exec: running ${workflow}/${runId} (step '${order.step}')`);
|
|
465
|
+
const outcome = await Promise.race([
|
|
466
|
+
cmd.done.then((r) => ({ t: 'done', r })).catch((e) => ({ t: 'done', r: machineryFailure(e) })),
|
|
467
|
+
leasePromise.then((o) => ({ t: 'lease', o })),
|
|
468
|
+
]);
|
|
469
|
+
if (outcome.t === 'lease') {
|
|
470
|
+
await cmd.kill(); // no receipt — a submit now would race the re-offer
|
|
471
|
+
if (signalled) {
|
|
472
|
+
opts.err(`owenloop work exec: signalled mid-run — killed the command group, released ${workflow}/${runId}`);
|
|
473
|
+
return 'killed';
|
|
474
|
+
}
|
|
475
|
+
opts.err(`owenloop work exec: lease ${outcome.o} while the command ran — killed the command, no receipt submitted`);
|
|
476
|
+
return mapLeaseDuringRun(outcome.o);
|
|
317
477
|
}
|
|
318
|
-
|
|
319
|
-
|
|
478
|
+
return submitReceipt(outcome.r, order, resolvedCommand);
|
|
479
|
+
}
|
|
480
|
+
finally {
|
|
481
|
+
removeConsumesDir(consumesDir);
|
|
320
482
|
}
|
|
321
|
-
return submitReceipt(outcome.r, order, resolvedCommand);
|
|
322
483
|
}
|
|
323
484
|
function stop(reason) {
|
|
324
485
|
if (signalled)
|
|
@@ -43,11 +43,21 @@ const DEFAULT_GRACE_MS = 5_000; // SIGTERM → grace → SIGKILL on a group kill
|
|
|
43
43
|
* `/bin/sh` keeps the default runner portable (ubuntu-latest CI, macOS dev).
|
|
44
44
|
*/
|
|
45
45
|
export function buildCommandPlan(command, cwd, env) {
|
|
46
|
-
//
|
|
47
|
-
//
|
|
48
|
-
//
|
|
49
|
-
//
|
|
50
|
-
//
|
|
46
|
+
// Consumed values DO reach the child environment: exec/loop.ts delivers the
|
|
47
|
+
// order's consumed inputs as OWENLOOP_CONSUMES (or OWENLOOP_CONSUMES_FILE).
|
|
48
|
+
// That path inherits resolveCommand's consume-side verification gate — the
|
|
49
|
+
// env block is built only after `resolveCommand` returned, and that call
|
|
50
|
+
// refuses the whole order under `hardRule: true` on absent, unverifiable, or
|
|
51
|
+
// invalid consumed evidence — so no unverified dynamic value is ever placed
|
|
52
|
+
// here. OWENLOOP_BUNDLE_DIR is resolver-derived after the same gate and is
|
|
53
|
+
// not a consumed artifact value at all.
|
|
54
|
+
//
|
|
55
|
+
// Command TEXT stays uninterpolated: `command` is the authored bytes from the
|
|
56
|
+
// verified store, and `/bin/sh -c` expands `$OWENLOOP_CONSUMES` only where the
|
|
57
|
+
// authored text itself asks for it. A consumed value delivered through the
|
|
58
|
+
// environment block never becomes shell syntax. Any FUTURE path that puts a
|
|
59
|
+
// consumed value into command text or argv must re-argue that, and must
|
|
60
|
+
// likewise sit behind the consume-side gate.
|
|
51
61
|
return {
|
|
52
62
|
command: '/bin/sh',
|
|
53
63
|
args: ['-c', command],
|
|
@@ -29,7 +29,7 @@ import { delimiter, join } from 'node:path';
|
|
|
29
29
|
import { register } from "./registry.js";
|
|
30
30
|
import { filterOwenloopEnv } from "./child-env.js";
|
|
31
31
|
import { normalizeStepPermissions, validateHarnessOptions } from "./permissions.js";
|
|
32
|
-
import { ResumeUnavailableError } from "./contract.js";
|
|
32
|
+
import { ResumeUnavailableError, NEUTRAL_PERMISSION_MODES, isNeutralPermissionMode } from "./contract.js";
|
|
33
33
|
/** The registry key — the id a step def names in `x.harness.id`, and the key
|
|
34
34
|
* `harness-versions.json` pins the CLI under. */
|
|
35
35
|
const HARNESS_ID = 'claude-code';
|
|
@@ -178,6 +178,50 @@ const PERMISSION_MODES = [
|
|
|
178
178
|
'dontAsk',
|
|
179
179
|
'auto',
|
|
180
180
|
];
|
|
181
|
+
/**
|
|
182
|
+
* This adapter's neutral-vocabulary translation. Every value maps: this harness
|
|
183
|
+
* draws all three distinctions itself, so none of them is refused here.
|
|
184
|
+
*
|
|
185
|
+
* - `ask` → `default`. The SDK's `default` routes each unapproved tool to the
|
|
186
|
+
* `canUseTool` callback — a human decision point — and denies when no callback
|
|
187
|
+
* is wired. That is the "human is the gate" position.
|
|
188
|
+
* - `auto-safe` → `auto`. `auto` is the SDK's model-classifier mode: it decides
|
|
189
|
+
* per call and escalates the ones it judges dangerous. NOT `acceptEdits`,
|
|
190
|
+
* which auto-approves filesystem operations by category and applies no
|
|
191
|
+
* judgment to anything else, and NOT `dontAsk`, which DENIES an unapproved
|
|
192
|
+
* tool rather than consulting anyone — silently narrower than what the step
|
|
193
|
+
* asked for.
|
|
194
|
+
* - `full-access` → `bypassPermissions`. The only SDK mode that runs tools with
|
|
195
|
+
* no prompt at all, and the one the companion flag below exists for.
|
|
196
|
+
*
|
|
197
|
+
* `plan` has no neutral spelling on purpose: it is a different axis (what the
|
|
198
|
+
* agent may DO — explore only) rather than who approves.
|
|
199
|
+
*/
|
|
200
|
+
const NEUTRAL_TO_SDK_MODE = Object.freeze({
|
|
201
|
+
'ask': 'default',
|
|
202
|
+
'auto-safe': 'auto',
|
|
203
|
+
'full-access': 'bypassPermissions',
|
|
204
|
+
});
|
|
205
|
+
/** Values `preflight` accepts: the SDK's own union plus the neutral vocabulary. */
|
|
206
|
+
const ACCEPTED_PERMISSION_MODES = [
|
|
207
|
+
...PERMISSION_MODES,
|
|
208
|
+
...NEUTRAL_PERMISSION_MODES,
|
|
209
|
+
];
|
|
210
|
+
/**
|
|
211
|
+
* Authored `permissionMode` → the SDK mode to send.
|
|
212
|
+
*
|
|
213
|
+
* Total by construction: an out-of-union value returns `undefined` and the
|
|
214
|
+
* caller drops it with a diagnostic, preserving the pre-existing behavior for a
|
|
215
|
+
* bag that lint never saw. A neutral value this adapter refuses (a `null` in the
|
|
216
|
+
* table) also returns `undefined`, and `preflight` has already refused it.
|
|
217
|
+
*/
|
|
218
|
+
function toSdkPermissionMode(mode) {
|
|
219
|
+
if (isNeutralPermissionMode(mode)) {
|
|
220
|
+
const mapped = NEUTRAL_TO_SDK_MODE[mode];
|
|
221
|
+
return mapped === null ? undefined : mapped;
|
|
222
|
+
}
|
|
223
|
+
return PERMISSION_MODES.includes(mode) ? mode : undefined;
|
|
224
|
+
}
|
|
181
225
|
/** The five-value closed union the SDK types `Options.effort` as. */
|
|
182
226
|
const EFFORT_LEVELS = ['low', 'medium', 'high', 'xhigh', 'max'];
|
|
183
227
|
/** Audited built-ins that cannot mutate the filesystem. WebFetch/WebSearch
|
|
@@ -219,11 +263,23 @@ const OWENLOOP_CONTROL_DENY_SPECS = new Set([
|
|
|
219
263
|
]);
|
|
220
264
|
function claudePreflight(permissions) {
|
|
221
265
|
const issues = [];
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
266
|
+
const authoredMode = permissions.permissionMode;
|
|
267
|
+
if (authoredMode !== undefined) {
|
|
268
|
+
if (!ACCEPTED_PERMISSION_MODES.includes(authoredMode)) {
|
|
269
|
+
issues.push({
|
|
270
|
+
field: 'permissionMode',
|
|
271
|
+
message: `permissionMode must be one of ${ACCEPTED_PERMISSION_MODES.join('|')}`,
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
else if (isNeutralPermissionMode(authoredMode) && NEUTRAL_TO_SDK_MODE[authoredMode] === null) {
|
|
275
|
+
// Unreachable today — every neutral value maps here. Kept because the
|
|
276
|
+
// table's `null` arm is a real contract option, and a refusal that only
|
|
277
|
+
// exists once someone uses it is a refusal nobody has tested.
|
|
278
|
+
issues.push({
|
|
279
|
+
field: 'permissionMode',
|
|
280
|
+
message: `permissionMode '${authoredMode}' is unsupported by this adapter`,
|
|
281
|
+
});
|
|
282
|
+
}
|
|
227
283
|
}
|
|
228
284
|
if (permissions.effort !== undefined && !EFFORT_LEVELS.includes(permissions.effort)) {
|
|
229
285
|
issues.push({ field: 'effort', message: `effort must be one of ${EFFORT_LEVELS.join('|')}` });
|
|
@@ -359,8 +415,12 @@ export function buildClaudeOptions(inputs, extra) {
|
|
|
359
415
|
}
|
|
360
416
|
}
|
|
361
417
|
if (permissions.permissionMode !== undefined) {
|
|
362
|
-
|
|
363
|
-
|
|
418
|
+
// Neutral `full-access` lands here as `bypassPermissions`, so the companion
|
|
419
|
+
// flag below covers it too — that is the point of translating BEFORE the
|
|
420
|
+
// pairing rather than after it. Neutral `auto-safe` lands as `auto` and
|
|
421
|
+
// deliberately does NOT get the flag: it is still allowed to stop and ask.
|
|
422
|
+
const mode = toSdkPermissionMode(permissions.permissionMode);
|
|
423
|
+
if (mode !== undefined) {
|
|
364
424
|
options.permissionMode = mode;
|
|
365
425
|
// 'bypassPermissions' is IGNORED by the SDK without this companion flag.
|
|
366
426
|
// Missing it is a silent downgrade to prompting, which in a headless run
|
|
@@ -371,7 +431,7 @@ export function buildClaudeOptions(inputs, extra) {
|
|
|
371
431
|
else {
|
|
372
432
|
extra.onEvent({
|
|
373
433
|
kind: 'progress',
|
|
374
|
-
text: `dropped unknown permissionMode '${permissions.permissionMode}' (expected one of ${
|
|
434
|
+
text: `dropped unknown permissionMode '${permissions.permissionMode}' (expected one of ${ACCEPTED_PERMISSION_MODES.join('|')})`,
|
|
375
435
|
});
|
|
376
436
|
}
|
|
377
437
|
}
|