owenloop 0.4.0 → 0.5.0
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 +45 -0
- package/README.md +98 -25
- package/bin/owenloop.mjs +2 -2
- package/dist/packages/work/src/agent/brief.d.ts +172 -0
- package/dist/packages/work/src/agent/brief.js +193 -0
- package/dist/packages/work/src/agent/loop.d.ts +135 -0
- package/dist/packages/work/src/agent/loop.js +542 -0
- package/dist/packages/work/src/agent/workdir.d.ts +210 -0
- package/dist/packages/work/src/agent/workdir.js +319 -0
- package/dist/packages/work/src/bundle/cache.d.ts +90 -0
- package/dist/packages/work/src/bundle/cache.js +324 -0
- package/dist/packages/work/src/bundle/fetch.d.ts +85 -0
- package/dist/packages/work/src/bundle/fetch.js +325 -0
- package/dist/packages/work/src/bundle/types.d.ts +160 -0
- package/dist/packages/work/src/bundle/types.js +1 -0
- package/dist/packages/work/src/credentials/owenloop.d.ts +11 -0
- package/dist/packages/work/src/credentials/owenloop.js +28 -0
- package/dist/packages/work/src/credentials/reader.d.ts +36 -0
- package/dist/packages/work/src/credentials/reader.js +32 -0
- package/dist/packages/work/src/credentials/resolve.d.ts +19 -0
- package/dist/packages/work/src/credentials/resolve.js +56 -0
- package/dist/packages/work/src/exec/loop.d.ts +29 -0
- package/dist/packages/work/src/exec/loop.js +215 -0
- package/dist/packages/work/src/exec/receipt.d.ts +38 -0
- package/dist/packages/work/src/exec/receipt.js +21 -0
- package/dist/packages/work/src/exec/runner.d.ts +65 -0
- package/dist/packages/work/src/exec/runner.js +185 -0
- package/dist/packages/work/src/harness/child-env.d.ts +100 -0
- package/dist/packages/work/src/harness/child-env.js +127 -0
- package/dist/packages/work/src/harness/claude.d.ts +128 -0
- package/dist/packages/work/src/harness/claude.js +635 -0
- package/dist/packages/work/src/harness/codex.d.ts +110 -0
- package/dist/packages/work/src/harness/codex.js +896 -0
- package/dist/packages/work/src/harness/contract.d.ts +283 -0
- package/dist/packages/work/src/harness/contract.js +32 -0
- package/dist/packages/work/src/harness/fake.d.ts +59 -0
- package/dist/packages/work/src/harness/fake.js +90 -0
- package/dist/packages/work/src/harness/jsonrpc-stdio.d.ts +108 -0
- package/dist/packages/work/src/harness/jsonrpc-stdio.js +344 -0
- package/dist/packages/work/src/harness/permissions.d.ts +62 -0
- package/dist/packages/work/src/harness/permissions.js +93 -0
- package/dist/packages/work/src/harness/registry.d.ts +47 -0
- package/dist/packages/work/src/harness/registry.js +45 -0
- package/dist/packages/work/src/harness/session-store.d.ts +163 -0
- package/dist/packages/work/src/harness/session-store.js +319 -0
- package/dist/packages/work/src/harness/types.d.ts +29 -0
- package/dist/packages/work/src/harness/types.js +14 -0
- package/dist/packages/work/src/harnesses.d.ts +38 -0
- package/dist/packages/work/src/harnesses.js +38 -0
- package/dist/packages/work/src/hold/loop.d.ts +36 -0
- package/dist/packages/work/src/hold/loop.js +23 -0
- package/dist/packages/work/src/hold/mcp.d.ts +55 -0
- package/dist/packages/work/src/hold/mcp.js +149 -0
- package/dist/packages/work/src/hub/client.d.ts +22 -0
- package/dist/packages/work/src/hub/client.js +78 -0
- package/dist/packages/work/src/hub/types.d.ts +290 -0
- package/dist/packages/work/src/hub/types.js +28 -0
- package/dist/packages/work/src/lease/loop.d.ts +94 -0
- package/dist/packages/work/src/lease/loop.js +277 -0
- package/dist/packages/work/src/main.d.ts +1 -0
- package/dist/packages/work/src/main.js +48 -0
- package/dist/packages/work/src/mcp/server.d.ts +125 -0
- package/dist/packages/work/src/mcp/server.js +320 -0
- package/dist/packages/work/src/roles/agent-run.d.ts +118 -0
- package/dist/packages/work/src/roles/agent-run.js +447 -0
- package/dist/packages/work/src/roles/exec.d.ts +40 -0
- package/dist/packages/work/src/roles/exec.js +203 -0
- package/dist/packages/work/src/roles/hold.d.ts +68 -0
- package/dist/packages/work/src/roles/hold.js +324 -0
- package/dist/packages/work/src/roles/join.d.ts +18 -0
- package/dist/packages/work/src/roles/join.js +204 -0
- package/dist/packages/work/src/roles/lint.d.ts +59 -0
- package/dist/packages/work/src/roles/lint.js +200 -0
- package/dist/packages/work/src/roles/prepare.d.ts +1 -0
- package/dist/packages/work/src/roles/prepare.js +249 -0
- package/dist/packages/work/src/roles/release.d.ts +48 -0
- package/dist/packages/work/src/roles/release.js +137 -0
- package/dist/packages/work/src/roles/sessions.d.ts +61 -0
- package/dist/packages/work/src/roles/sessions.js +180 -0
- package/dist/packages/work/src/roles/settings.d.ts +6 -0
- package/dist/packages/work/src/roles/settings.js +66 -0
- package/dist/packages/work/src/roles/shift.d.ts +12 -0
- package/dist/packages/work/src/roles/shift.js +228 -0
- package/dist/packages/work/src/roles/signals.d.ts +55 -0
- package/dist/packages/work/src/roles/signals.js +62 -0
- package/dist/packages/work/src/settings/provision.d.ts +21 -0
- package/dist/packages/work/src/settings/provision.js +47 -0
- package/dist/packages/work/src/settings/settings.d.ts +120 -0
- package/dist/packages/work/src/settings/settings.js +116 -0
- package/dist/packages/work/src/shift/client.d.ts +24 -0
- package/dist/packages/work/src/shift/client.js +124 -0
- package/dist/packages/work/src/shift/loop.d.ts +120 -0
- package/dist/packages/work/src/shift/loop.js +468 -0
- package/dist/packages/work/src/shift/protocol.d.ts +91 -0
- package/dist/packages/work/src/shift/protocol.js +24 -0
- package/dist/packages/work/src/shift/routing.d.ts +35 -0
- package/dist/packages/work/src/shift/routing.js +50 -0
- package/dist/packages/work/src/shift/runtime.d.ts +43 -0
- package/dist/packages/work/src/shift/runtime.js +274 -0
- package/dist/packages/work/src/shift/server.d.ts +23 -0
- package/dist/packages/work/src/shift/server.js +716 -0
- package/dist/packages/work/src/shift/spawn.d.ts +86 -0
- package/dist/packages/work/src/shift/spawn.js +100 -0
- package/dist/packages/work/src/shift/state.d.ts +77 -0
- package/dist/packages/work/src/shift/state.js +162 -0
- package/dist/packages/work/src/usage.d.ts +6 -0
- package/dist/packages/work/src/usage.js +175 -0
- package/dist/{add.d.ts → src/add.d.ts} +12 -40
- package/dist/{add.js → src/add.js} +11 -176
- package/dist/src/cli.d.ts +147 -0
- package/dist/{cli.js → src/cli.js} +1643 -353
- package/dist/src/credentials.d.ts +280 -0
- package/dist/src/credentials.js +697 -0
- package/dist/{defs.js → src/defs.js} +59 -36
- package/dist/{engine.d.ts → src/engine.d.ts} +8 -8
- package/dist/{engine.js → src/engine.js} +29 -20
- package/dist/{hub.d.ts → src/hub.d.ts} +330 -2
- package/dist/{hub.js → src/hub.js} +531 -1
- package/dist/{index.d.ts → src/index.d.ts} +3 -1
- package/dist/{index.js → src/index.js} +2 -1
- package/dist/src/lock.d.ts +80 -0
- package/dist/src/lock.js +217 -0
- package/dist/src/mcp/serve.d.ts +85 -0
- package/dist/src/mcp/serve.js +702 -0
- package/dist/src/mcp/server.d.ts +132 -0
- package/dist/src/mcp/server.js +327 -0
- package/dist/{model.d.ts → src/model.d.ts} +59 -4
- package/dist/{model.js → src/model.js} +149 -22
- package/dist/{types.d.ts → src/types.d.ts} +66 -24
- package/dist/{util.d.ts → src/util.d.ts} +25 -0
- package/dist/{util.js → src/util.js} +26 -0
- package/dist/src/work-settings.d.ts +57 -0
- package/dist/src/work-settings.js +84 -0
- package/docs/authoring.md +102 -53
- package/docs/cli.md +883 -29
- package/docs/design.md +119 -45
- package/docs/embedding.md +39 -14
- package/examples/workflows/{command-worker.yaml → command-executor.yaml} +15 -15
- package/examples/workflows/compile-dev-playbook.yaml +338 -0
- package/examples/workflows/judges/gate-sharpness.md +39 -0
- package/examples/workflows/judges/security.md +34 -0
- package/package.json +17 -8
- package/dist/cli.d.ts +0 -83
- /package/dist/{defs.d.ts → src/defs.d.ts} +0 -0
- /package/dist/{factory.d.ts → src/factory.d.ts} +0 -0
- /package/dist/{factory.js → src/factory.js} +0 -0
- /package/dist/{paths.d.ts → src/paths.d.ts} +0 -0
- /package/dist/{paths.js → src/paths.js} +0 -0
- /package/dist/{schema.d.ts → src/schema.d.ts} +0 -0
- /package/dist/{schema.js → src/schema.js} +0 -0
- /package/dist/{store.d.ts → src/store.d.ts} +0 -0
- /package/dist/{store.js → src/store.js} +0 -0
- /package/dist/{types.js → src/types.js} +0 -0
- /package/dist/{untar.d.ts → src/untar.d.ts} +0 -0
- /package/dist/{untar.js → src/untar.js} +0 -0
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The self-driven shift loop core (D2 dispatch split).
|
|
3
|
+
*
|
|
4
|
+
* `createShiftLoop` is a standing Shift loop with every side-effecting
|
|
5
|
+
* dependency injected (hub client, spawner, sleep/clock, output streams, dirs),
|
|
6
|
+
* so the same core can run behind the local shift daemon. Per iteration it: pings
|
|
7
|
+
* presence when due → cheap `wake(cursor)` pre-check → only when something
|
|
8
|
+
* changed AND there is free capacity, sweeps `whats_next`, classifies/meters
|
|
9
|
+
* orders, and DISPATCHES BY KIND → reaps work dirs → adopts the cursor → sleeps
|
|
10
|
+
* the poll interval.
|
|
11
|
+
*
|
|
12
|
+
* DISPATCH SPLIT (D2 decision 3 — the D12 routing contract). BOTH kinds spawn a
|
|
13
|
+
* detached child; there is exactly ONE way each kind runs, and no flag selects
|
|
14
|
+
* between paths:
|
|
15
|
+
* - COMMAND orders spawn a detached `owenloop work exec <wf>/<run> --origin <url>`
|
|
16
|
+
* child that self-leases (unchanged from C3).
|
|
17
|
+
* - AGENT orders spawn a detached `owenloop work agent-run <wf>/<run>` child that
|
|
18
|
+
* hosts the step agent itself through a harness adapter. It reads the step's
|
|
19
|
+
* normalized spec straight out of the bundle cache and renders the brief in
|
|
20
|
+
* process.
|
|
21
|
+
* - The shift makes NO first-contact `get_order` for any kind. It holds no
|
|
22
|
+
* leases; the spawned child makes first contact and closes the B2 pickup
|
|
23
|
+
* window. A failed hand-off lapses back via that window instead of sitting
|
|
24
|
+
* claimed.
|
|
25
|
+
*
|
|
26
|
+
* METERING (decision 3): free capacity `k = cap − liveInFlight`, where
|
|
27
|
+
* liveInFlight counts every live child record, each one pid-probed (see
|
|
28
|
+
* state.ts). When `k <= 0` the loop skips `whats_next` ENTIRELY (keeps wake +
|
|
29
|
+
* presence). Metering is efficiency-only (engine race-safety + the hub's
|
|
30
|
+
* pickup/lease TTLs are the correctness backstop), so this approximation is
|
|
31
|
+
* acceptable.
|
|
32
|
+
*
|
|
33
|
+
* RESILIENCE: presence/wake/whats_next failures are logged and never kill the
|
|
34
|
+
* loop. `stop()` flips a flag checked between awaits; the in-flight sweep
|
|
35
|
+
* finishes, `run()` resolves 0, and no hub call is made afterward. Detached
|
|
36
|
+
* children are never killed on shutdown — that is the drain semantic.
|
|
37
|
+
*
|
|
38
|
+
* PINNED-HASH DISPATCH (E, DD-4): `whats_next` serves only a def NAME (no hash,
|
|
39
|
+
* no parent linkage), so the sweep resolves which cached bundle to serve via
|
|
40
|
+
* `readDispatchBundle(cacheDir, defName)` instead of a bare latest read: 0 pins
|
|
41
|
+
* → latest (unchanged); exactly 1 pinned hash → that frozen version (even when a
|
|
42
|
+
* newer unpinned hash is cached); >1 conflicting pins across different cached
|
|
43
|
+
* parents → refuse (no bundle + a warning), leave orders for the pickup window.
|
|
44
|
+
* Everything downstream keys records by `bundle.def.hash`, so a pinned bundle
|
|
45
|
+
* flows through `ChildRecord.hash` untouched.
|
|
46
|
+
*
|
|
47
|
+
* LIVE SHIFT IDENTITY: `name`/`serveCrews` on `ShiftLoopOptions` are INITIAL
|
|
48
|
+
* values only. The loop holds them as live closure state (`getShift`/`setShift`
|
|
49
|
+
* on `ShiftLoop`) so the shift socket's `clock_in` operation can change what a
|
|
50
|
+
* shift is called and which crews it serves without rebuilding the loop —
|
|
51
|
+
* `setShift` also resets the presence timer so the new identity reaches the hub
|
|
52
|
+
* on the very next `iterate()`.
|
|
53
|
+
*/
|
|
54
|
+
import { readDispatchBundle } from "../bundle/cache.js";
|
|
55
|
+
import { isCommandStep, resolveCommandRouting } from "./routing.js";
|
|
56
|
+
import { reconcileInFlight, writeChildRecord, removeChildRecord, ensureStateDir, } from "./state.js";
|
|
57
|
+
import { DEFAULT_WORK_DIR_TTL_MS, sweepWorkDirs as sweepWorkDirsImpl } from "../agent/workdir.js";
|
|
58
|
+
import { sessionsPath } from "../harness/session-store.js";
|
|
59
|
+
/** The `maxConcurrentAgents` fallback when the option is absent. */
|
|
60
|
+
const DEFAULT_MAX_AGENTS = 4;
|
|
61
|
+
function errMsg(e) {
|
|
62
|
+
return e instanceof Error ? e.message : String(e);
|
|
63
|
+
}
|
|
64
|
+
export function createShiftLoop(opts) {
|
|
65
|
+
let stopped = false;
|
|
66
|
+
let cap = opts.cap;
|
|
67
|
+
const isAlive = opts.isAlive;
|
|
68
|
+
// Live shift identity (MCP `clock_in`, D3-D7 of the plan). Seeded from opts,
|
|
69
|
+
// which are now INITIAL values only. Arrays are copied so neither the loop
|
|
70
|
+
// nor a caller of getShift/setShift can mutate the other's state afterward.
|
|
71
|
+
let shiftName = opts.name;
|
|
72
|
+
let serveCrews = [...opts.serveCrews];
|
|
73
|
+
// Park state persists across `iterate()` calls (the MCP park reuses it).
|
|
74
|
+
let cursor;
|
|
75
|
+
let lastPresence = Number.NEGATIVE_INFINITY;
|
|
76
|
+
let attendedAt;
|
|
77
|
+
function emit(event) {
|
|
78
|
+
if (opts.onEvent === undefined)
|
|
79
|
+
return;
|
|
80
|
+
try {
|
|
81
|
+
opts.onEvent(event);
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
opts.err(`shift event sink failed: ${errMsg(e)} (continuing)`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function reconcile() {
|
|
88
|
+
// No clock is passed: reconciliation is purely pid-probed, so there is no
|
|
89
|
+
// time-dependent decision for `opts.now` to influence.
|
|
90
|
+
const result = reconcileInFlight(opts.stateDir, { ...(isAlive !== undefined ? { isAlive } : {}) });
|
|
91
|
+
for (const rec of result.reaped) {
|
|
92
|
+
emit({
|
|
93
|
+
type: 'reaped',
|
|
94
|
+
workflow: rec.workflow,
|
|
95
|
+
run: rec.run,
|
|
96
|
+
kind: rec.kind ?? 'exec',
|
|
97
|
+
pid: rec.pid,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* One sweep. Collects dispatch candidates across the target instance(s)
|
|
104
|
+
* within the free-capacity `budget`, then dispatches by kind: a command order
|
|
105
|
+
* spawns a detached `owenloop work exec` child, an agent order a detached
|
|
106
|
+
* `owenloop work agent-run` child. Returns how many children were spawned, plus the
|
|
107
|
+
* polled/open-run sets the work-dir reaper needs.
|
|
108
|
+
*/
|
|
109
|
+
async function sweep(budget, live) {
|
|
110
|
+
let dispatched = 0;
|
|
111
|
+
/**
|
|
112
|
+
* PHASE 4, for the work-dir reaper only. `polled` is the set of workflows
|
|
113
|
+
* whose `whats_next` SUCCEEDED this sweep — a workflow whose call threw is
|
|
114
|
+
* deliberately left out, because a failed call is not evidence that a run
|
|
115
|
+
* has no open order, and the reaper must never treat silence as absence.
|
|
116
|
+
* `openRuns` is every run those calls reported an order for, including ones
|
|
117
|
+
* this shift declined to dispatch (capacity, routing, an in-flight worker):
|
|
118
|
+
* an order the shift left alone is still an open order, and its run's work
|
|
119
|
+
* directory must survive.
|
|
120
|
+
*/
|
|
121
|
+
const polled = new Set();
|
|
122
|
+
const openRuns = new Set();
|
|
123
|
+
let remaining = budget;
|
|
124
|
+
const liveRuns = new Set(live.map((r) => r.run));
|
|
125
|
+
// The agent lane's SECOND budget (D7). Counted from the live records, so an
|
|
126
|
+
// agent-run child that died frees its slot on the next reconcile exactly like
|
|
127
|
+
// an exec child does.
|
|
128
|
+
const maxAgents = opts.maxConcurrentAgents ?? DEFAULT_MAX_AGENTS;
|
|
129
|
+
let agentRoom = maxAgents - live.filter((r) => r.kind === 'agent-run').length;
|
|
130
|
+
/**
|
|
131
|
+
* Runs that ALREADY have a live `agent-run` child. An agent-run child is a
|
|
132
|
+
* PROCESS: re-dispatching one would put a second harness session on a single
|
|
133
|
+
* claim — two step agents briefed for the same order, racing to submit. So a
|
|
134
|
+
* re-offer for a run a worker already holds is skipped outright, the way the
|
|
135
|
+
* command lane does it.
|
|
136
|
+
*/
|
|
137
|
+
const workerRuns = new Set(live.filter((r) => r.kind === 'agent-run').map((r) => r.run));
|
|
138
|
+
// Resolve which instances to poll.
|
|
139
|
+
let instances;
|
|
140
|
+
if (opts.workflow !== undefined) {
|
|
141
|
+
instances = [opts.workflow];
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
try {
|
|
145
|
+
const inbox = await opts.hub.whatsNext();
|
|
146
|
+
instances = (inbox.instances ?? []).map((i) => i.workflow);
|
|
147
|
+
}
|
|
148
|
+
catch (e) {
|
|
149
|
+
opts.err(`inbox whats_next failed: ${errMsg(e)}`);
|
|
150
|
+
return { dispatched, polled, openRuns };
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// Collect candidates, metering NEW dispatches and deduping in-flight.
|
|
154
|
+
const claimed = new Set();
|
|
155
|
+
const candidates = [];
|
|
156
|
+
for (const wf of instances) {
|
|
157
|
+
let res;
|
|
158
|
+
try {
|
|
159
|
+
res = await opts.hub.whatsNext({ workflow: wf, serve_crews: serveCrews });
|
|
160
|
+
}
|
|
161
|
+
catch (e) {
|
|
162
|
+
opts.err(`whats_next for ${wf} failed: ${errMsg(e)}`);
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
const orders = res.orders ?? [];
|
|
166
|
+
polled.add(wf);
|
|
167
|
+
for (const o of orders)
|
|
168
|
+
openRuns.add(o.run);
|
|
169
|
+
const defName = res.def;
|
|
170
|
+
// Dispatch selection (DD-4): honor a UNIQUE pinned hash for this def name;
|
|
171
|
+
// 0 pins → latest (unchanged); conflicting pins → refuse (null + warning).
|
|
172
|
+
const dispatch = defName !== undefined ? readDispatchBundle(opts.cacheDir, defName) : { bundle: null };
|
|
173
|
+
const bundle = dispatch.bundle;
|
|
174
|
+
if (orders.length > 0 && dispatch.warning !== undefined) {
|
|
175
|
+
opts.err(`[${wf}] ${dispatch.warning}`);
|
|
176
|
+
}
|
|
177
|
+
else if (orders.length > 0 && bundle === null && defName !== undefined) {
|
|
178
|
+
opts.err(`no cached bundle for def '${defName}' — run \`owenloop work prepare ${defName}\` (agent orders left for pickup)`);
|
|
179
|
+
}
|
|
180
|
+
for (const order of orders) {
|
|
181
|
+
if (claimed.has(order.run))
|
|
182
|
+
continue; // seen this sweep
|
|
183
|
+
const step = bundle?.def.steps.find((s) => s.name === order.step);
|
|
184
|
+
const isCmd = step !== undefined && isCommandStep(step);
|
|
185
|
+
const reoffer = liveRuns.has(order.run);
|
|
186
|
+
if (isCmd) {
|
|
187
|
+
// A command already in flight (its exec child lives) is never respawned.
|
|
188
|
+
if (reoffer)
|
|
189
|
+
continue;
|
|
190
|
+
const r = resolveCommandRouting(opts.commandRouting, step);
|
|
191
|
+
for (const w of r.warnings)
|
|
192
|
+
opts.err(`[${wf}/${order.run}] ${w}`);
|
|
193
|
+
if (!r.autoDispatch) {
|
|
194
|
+
opts.out(`[${wf}/${order.run}] command step '${order.step}' routed to Shift — leaving for pickup window`);
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (remaining <= 0)
|
|
198
|
+
continue; // out of free capacity for new work
|
|
199
|
+
remaining--;
|
|
200
|
+
candidates.push({ order, workflow: wf, step, defName, defHash: bundle?.def.hash, kind: 'command', reoffer });
|
|
201
|
+
claimed.add(order.run);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
// Agent order. An order a live worker already holds is never re-dispatched
|
|
205
|
+
// (see `workerRuns`) — that would double-brief one claim.
|
|
206
|
+
if (workerRuns.has(order.run))
|
|
207
|
+
continue;
|
|
208
|
+
// No resolved bundle ⇒ no dispatch. Two distinct causes, one outcome:
|
|
209
|
+
// - nothing cached for this def name (the operator never ran
|
|
210
|
+
// `owenloop work prepare`), so there is no def+hash for the `agent-run`
|
|
211
|
+
// child to read `steps/<step>.json` from; and
|
|
212
|
+
// - conflicting pins across cached parents (DD-4), where refusing is
|
|
213
|
+
// the point — spawning a child that picks its own bundle would make
|
|
214
|
+
// the very version guess DD-4 forbids.
|
|
215
|
+
// Either way the order is LEFT for the hub's pickup window. The one
|
|
216
|
+
// warning per sweep was already written above.
|
|
217
|
+
if (bundle === null)
|
|
218
|
+
continue;
|
|
219
|
+
// A re-offer replaces its (stale) record without consuming a new slot;
|
|
220
|
+
// a genuinely new agent order needs free capacity.
|
|
221
|
+
if (!reoffer) {
|
|
222
|
+
if (remaining <= 0)
|
|
223
|
+
continue;
|
|
224
|
+
// The agent budget applies IN ADDITION to the global one. Skipped
|
|
225
|
+
// candidates are simply left alone — the hub re-offers them on a later
|
|
226
|
+
// sweep once a worker child exits.
|
|
227
|
+
if (agentRoom <= 0) {
|
|
228
|
+
opts.out(`[${wf}/${order.run}] at the agent-run cap (${maxAgents}) — leaving for a later sweep`);
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
remaining--;
|
|
232
|
+
agentRoom--;
|
|
233
|
+
}
|
|
234
|
+
candidates.push({ order, workflow: wf, step, defName, defHash: bundle?.def.hash, kind: 'agent', reoffer });
|
|
235
|
+
claimed.add(order.run);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (candidates.length === 0)
|
|
239
|
+
return { dispatched, polled, openRuns };
|
|
240
|
+
// Dispatch by kind.
|
|
241
|
+
for (const c of candidates) {
|
|
242
|
+
if (c.kind === 'command') {
|
|
243
|
+
try {
|
|
244
|
+
const { pid } = opts.spawner({ workflow: c.workflow, run: c.order.run });
|
|
245
|
+
const rec = { workflow: c.workflow, run: c.order.run, pid, spawnedAt: opts.now(), kind: 'exec' };
|
|
246
|
+
writeChildRecord(opts.stateDir, rec);
|
|
247
|
+
dispatched++;
|
|
248
|
+
emit({
|
|
249
|
+
type: 'dispatched',
|
|
250
|
+
workflow: c.workflow,
|
|
251
|
+
run: c.order.run,
|
|
252
|
+
step: c.order.step,
|
|
253
|
+
kind: 'exec',
|
|
254
|
+
pid,
|
|
255
|
+
});
|
|
256
|
+
opts.out(`dispatched command ${c.workflow}/${c.order.run} (step '${c.order.step}', pid ${pid})`);
|
|
257
|
+
}
|
|
258
|
+
catch (e) {
|
|
259
|
+
const message = errMsg(e);
|
|
260
|
+
emit({
|
|
261
|
+
type: 'failed',
|
|
262
|
+
workflow: c.workflow,
|
|
263
|
+
run: c.order.run,
|
|
264
|
+
step: c.order.step,
|
|
265
|
+
kind: 'exec',
|
|
266
|
+
message,
|
|
267
|
+
});
|
|
268
|
+
opts.err(`spawn for ${c.workflow}/${c.order.run} failed: ${message}`);
|
|
269
|
+
}
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
// Agent: spawn a detached `agent-run` child that hosts the step agent.
|
|
273
|
+
// This is the ONLY agent path — no flag, no alternative.
|
|
274
|
+
try {
|
|
275
|
+
const { pid } = opts.spawner({
|
|
276
|
+
workflow: c.workflow,
|
|
277
|
+
run: c.order.run,
|
|
278
|
+
kind: 'agent-run',
|
|
279
|
+
...(c.step?.harness !== undefined && c.step.harness !== '' ? { harness: c.step.harness } : {}),
|
|
280
|
+
});
|
|
281
|
+
const rec = {
|
|
282
|
+
workflow: c.workflow,
|
|
283
|
+
run: c.order.run,
|
|
284
|
+
pid,
|
|
285
|
+
spawnedAt: opts.now(),
|
|
286
|
+
kind: 'agent-run',
|
|
287
|
+
...(c.defName !== undefined ? { def: c.defName } : {}),
|
|
288
|
+
...(c.defHash !== undefined ? { hash: c.defHash } : {}),
|
|
289
|
+
step: c.order.step,
|
|
290
|
+
};
|
|
291
|
+
writeChildRecord(opts.stateDir, rec);
|
|
292
|
+
dispatched++;
|
|
293
|
+
emit({
|
|
294
|
+
type: 'dispatched',
|
|
295
|
+
workflow: c.workflow,
|
|
296
|
+
run: c.order.run,
|
|
297
|
+
step: c.order.step,
|
|
298
|
+
kind: 'agent-run',
|
|
299
|
+
pid,
|
|
300
|
+
});
|
|
301
|
+
opts.out(`dispatched agent-run ${c.workflow}/${c.order.run} (step '${c.order.step}', pid ${pid})`);
|
|
302
|
+
}
|
|
303
|
+
catch (e) {
|
|
304
|
+
const message = errMsg(e);
|
|
305
|
+
emit({
|
|
306
|
+
type: 'failed',
|
|
307
|
+
workflow: c.workflow,
|
|
308
|
+
run: c.order.run,
|
|
309
|
+
step: c.order.step,
|
|
310
|
+
kind: 'agent-run',
|
|
311
|
+
message,
|
|
312
|
+
});
|
|
313
|
+
opts.err(`agent-run spawn for ${c.workflow}/${c.order.run} failed: ${message}`);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return { dispatched, polled, openRuns };
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* PHASE 4 — remove the per-run work directories of runs that are over.
|
|
320
|
+
*
|
|
321
|
+
* The three inputs, and where each comes from:
|
|
322
|
+
* - `workflows` = `swept.polled`, the workflows whose `whats_next` succeeded.
|
|
323
|
+
* - `openRunIds` = `swept.openRuns`, every run those calls reported an order
|
|
324
|
+
* for, dispatched or not.
|
|
325
|
+
* - `liveRunIds` = the runs of live `agent-run` records, pid-probed by
|
|
326
|
+
* `reconcileInFlight`. Only `agent-run` records are counted: those are the
|
|
327
|
+
* only children that hold a work directory — an `exec` child works in the
|
|
328
|
+
* shift's own cwd.
|
|
329
|
+
*
|
|
330
|
+
* The fourth input is `sessionsFile`: `<cacheDir>/sessions.jsonl`, the same
|
|
331
|
+
* path `owenloop work agent-run` writes its session records to (`src/roles/
|
|
332
|
+
* agent-run.ts` builds it from the SAME `resolveCacheDir`). The sweep marks
|
|
333
|
+
* every session of a reaped run `dead` there BEFORE removing its directory —
|
|
334
|
+
* session lifetime equals cwd lifetime, and without that write the next firing
|
|
335
|
+
* of the step would resume into a recreated, empty directory.
|
|
336
|
+
*
|
|
337
|
+
* Best effort throughout — a removal failure is reported and the sweep goes on.
|
|
338
|
+
* Disabled entirely when `workRoot` is unset.
|
|
339
|
+
*/
|
|
340
|
+
function reapWorkDirs(swept, liveAfter) {
|
|
341
|
+
const workRoot = opts.workRoot;
|
|
342
|
+
if (workRoot === undefined || workRoot === '' || swept === undefined)
|
|
343
|
+
return;
|
|
344
|
+
if (swept.polled.size === 0)
|
|
345
|
+
return;
|
|
346
|
+
const doSweep = opts.sweepWorkDirs ?? sweepWorkDirsImpl;
|
|
347
|
+
const liveRunIds = new Set(liveAfter.filter((r) => r.kind === 'agent-run').map((r) => r.run));
|
|
348
|
+
let removed;
|
|
349
|
+
try {
|
|
350
|
+
removed = doSweep({
|
|
351
|
+
workRoot,
|
|
352
|
+
workflows: swept.polled,
|
|
353
|
+
openRunIds: swept.openRuns,
|
|
354
|
+
liveRunIds,
|
|
355
|
+
now: opts.now(),
|
|
356
|
+
ttlMs: opts.workDirTtlMs ?? DEFAULT_WORK_DIR_TTL_MS,
|
|
357
|
+
sessionsFile: sessionsPath(opts.cacheDir),
|
|
358
|
+
...(opts.workRepo !== undefined ? { workRepo: opts.workRepo } : {}),
|
|
359
|
+
err: opts.err,
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
catch (e) {
|
|
363
|
+
opts.err(`work-dir reaper failed: ${errMsg(e)} (ignored)`);
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
for (const dir of removed)
|
|
367
|
+
opts.out(`reaped work directory ${dir}`);
|
|
368
|
+
}
|
|
369
|
+
async function iteration() {
|
|
370
|
+
// Presence when due (starts immediately — this shift exists to conduct).
|
|
371
|
+
if (opts.now() - lastPresence >= opts.presenceIntervalMs) {
|
|
372
|
+
try {
|
|
373
|
+
await opts.hub.presencePing({
|
|
374
|
+
name: shiftName,
|
|
375
|
+
serve_crews: serveCrews,
|
|
376
|
+
...(opts.shiftId !== undefined ? { shift_id: opts.shiftId } : {}),
|
|
377
|
+
...(opts.startedAt !== undefined ? { started_at: opts.startedAt } : {}),
|
|
378
|
+
...(attendedAt !== undefined ? { attended_at: attendedAt } : {}),
|
|
379
|
+
});
|
|
380
|
+
lastPresence = opts.now();
|
|
381
|
+
}
|
|
382
|
+
catch (e) {
|
|
383
|
+
opts.err(`presence ping failed: ${errMsg(e)} (continuing)`);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
// Reconcile in-flight (startup recovery + per-sweep reap).
|
|
387
|
+
const { live } = reconcile();
|
|
388
|
+
const k = cap - live.length;
|
|
389
|
+
// Cheap wake pre-check. On failure, stay put this tick (don't sweep).
|
|
390
|
+
let changed = false;
|
|
391
|
+
try {
|
|
392
|
+
const w = await opts.hub.wake(cursor);
|
|
393
|
+
changed = w.changed;
|
|
394
|
+
cursor = w.cursor;
|
|
395
|
+
}
|
|
396
|
+
catch (e) {
|
|
397
|
+
opts.err(`wake failed: ${errMsg(e)} (retrying next tick)`);
|
|
398
|
+
}
|
|
399
|
+
// Sweep only when something changed AND there is free capacity.
|
|
400
|
+
let swept;
|
|
401
|
+
if (changed && k > 0) {
|
|
402
|
+
swept = await sweep(k, live);
|
|
403
|
+
}
|
|
404
|
+
else if (changed) {
|
|
405
|
+
opts.out(`at capacity (${live.length}/${cap} in flight) — skipping whats_next this tick`);
|
|
406
|
+
}
|
|
407
|
+
const liveAfter = reconcile().live;
|
|
408
|
+
// PHASE 4 — the work-directory reaper.
|
|
409
|
+
//
|
|
410
|
+
// ONLY ON A TICK THAT ACTUALLY SWEPT. The gate's first condition is "the hub
|
|
411
|
+
// reports no open order for this run", and the only thing that can support
|
|
412
|
+
// that claim is a fresh `whats_next` order list. A tick that skipped the
|
|
413
|
+
// sweep (nothing changed, or no free capacity) has no such list, and running
|
|
414
|
+
// the gate against a stale or empty one would read "no orders" as "no open
|
|
415
|
+
// orders" and delete live work. Skipping costs nothing: the next sweeping
|
|
416
|
+
// tick reaps whatever is still reapable.
|
|
417
|
+
reapWorkDirs(swept, liveAfter);
|
|
418
|
+
return swept?.dispatched ?? 0;
|
|
419
|
+
}
|
|
420
|
+
async function run() {
|
|
421
|
+
ensureStateDir(opts.stateDir);
|
|
422
|
+
if (opts.once === true) {
|
|
423
|
+
await iteration();
|
|
424
|
+
return 0;
|
|
425
|
+
}
|
|
426
|
+
for (;;) {
|
|
427
|
+
if (stopped)
|
|
428
|
+
return 0;
|
|
429
|
+
await iteration();
|
|
430
|
+
if (stopped)
|
|
431
|
+
return 0;
|
|
432
|
+
await opts.sleep(opts.pollIntervalMs);
|
|
433
|
+
// Re-check after the park so a stop during sleep makes no further hub call.
|
|
434
|
+
if (stopped)
|
|
435
|
+
return 0;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
function stop() {
|
|
439
|
+
stopped = true;
|
|
440
|
+
}
|
|
441
|
+
return {
|
|
442
|
+
run,
|
|
443
|
+
stop,
|
|
444
|
+
iterate: iteration,
|
|
445
|
+
freeCapacity: () => cap - reconcile().live.length,
|
|
446
|
+
getCap: () => cap,
|
|
447
|
+
setCap: (next) => {
|
|
448
|
+
cap = next;
|
|
449
|
+
},
|
|
450
|
+
getShift: () => ({ name: shiftName, serveCrews: [...serveCrews] }),
|
|
451
|
+
setShift: (next) => {
|
|
452
|
+
if (next.name !== undefined)
|
|
453
|
+
shiftName = next.name;
|
|
454
|
+
if (next.serveCrews !== undefined)
|
|
455
|
+
serveCrews = [...next.serveCrews];
|
|
456
|
+
lastPresence = Number.NEGATIVE_INFINITY; // D6: next iterate() pings immediately
|
|
457
|
+
return { name: shiftName, serveCrews: [...serveCrews] };
|
|
458
|
+
},
|
|
459
|
+
noteAttended: (at) => {
|
|
460
|
+
attendedAt = at;
|
|
461
|
+
lastPresence = Number.NEGATIVE_INFINITY;
|
|
462
|
+
},
|
|
463
|
+
getAttendedAt: () => attendedAt,
|
|
464
|
+
noteRunEnded: (run) => {
|
|
465
|
+
removeChildRecord(opts.stateDir, run);
|
|
466
|
+
},
|
|
467
|
+
};
|
|
468
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local JSON-lines protocol for the foreground shift daemon.
|
|
3
|
+
*
|
|
4
|
+
* The protocol is deliberately small: one request per Unix-socket connection,
|
|
5
|
+
* one response line, then the connection closes. The hub remains the owner of
|
|
6
|
+
* workflow state; this protocol only carries local dispatch observations and
|
|
7
|
+
* attendance commands between the CLI and one daemon process.
|
|
8
|
+
*/
|
|
9
|
+
export declare const SHIFT_SOCKET_NAME = "shift.sock";
|
|
10
|
+
export declare const DEFAULT_NEXT_WAIT_MS = 90000;
|
|
11
|
+
export declare const MAX_REQUEST_LINE_BYTES: number;
|
|
12
|
+
/**
|
|
13
|
+
* The server measures each response's serialized UTF-8 byte length against this
|
|
14
|
+
* ceiling and retains FIFO events that do not fit. A single event whose fields
|
|
15
|
+
* exceed the ceiling is rendered with an explicit truncation marker.
|
|
16
|
+
*/
|
|
17
|
+
export declare const MAX_RESPONSE_LINE_BYTES: number;
|
|
18
|
+
export declare const RESPONSE_TRUNCATION_MARKER = "[truncated]";
|
|
19
|
+
export declare const MAX_EVENT_QUEUE = 1000;
|
|
20
|
+
export declare const OVERLAP_ERROR = "whats_next is already parked \u2014 one park at a time (cancel it or wait for it to return)";
|
|
21
|
+
export declare const NO_DAEMON_SUFFIX = " \u2014 start one with: owenloop shift start <crew\u2026>";
|
|
22
|
+
export type ShiftRequest = {
|
|
23
|
+
op: 'next';
|
|
24
|
+
wait_ms: number;
|
|
25
|
+
} | {
|
|
26
|
+
op: 'status';
|
|
27
|
+
} | {
|
|
28
|
+
op: 'clock_in';
|
|
29
|
+
serve_crews: string[];
|
|
30
|
+
name: string;
|
|
31
|
+
} | {
|
|
32
|
+
op: 'end';
|
|
33
|
+
};
|
|
34
|
+
export interface DispatchedEvent {
|
|
35
|
+
type: 'dispatched';
|
|
36
|
+
workflow: string;
|
|
37
|
+
run: string;
|
|
38
|
+
step: string;
|
|
39
|
+
kind: 'exec' | 'agent-run';
|
|
40
|
+
pid: number;
|
|
41
|
+
}
|
|
42
|
+
export interface ReapedEvent {
|
|
43
|
+
type: 'reaped';
|
|
44
|
+
workflow: string;
|
|
45
|
+
run: string;
|
|
46
|
+
kind: 'exec' | 'agent-run';
|
|
47
|
+
pid: number;
|
|
48
|
+
}
|
|
49
|
+
export interface FailedEvent {
|
|
50
|
+
type: 'failed';
|
|
51
|
+
workflow: string;
|
|
52
|
+
run: string;
|
|
53
|
+
step: string;
|
|
54
|
+
kind: 'exec' | 'agent-run';
|
|
55
|
+
message: string;
|
|
56
|
+
}
|
|
57
|
+
/** Reserved for a future local representation of a pending hub gate. */
|
|
58
|
+
export interface GateEvent {
|
|
59
|
+
type: 'gate';
|
|
60
|
+
workflow?: string;
|
|
61
|
+
run?: string;
|
|
62
|
+
name?: string;
|
|
63
|
+
question?: string;
|
|
64
|
+
}
|
|
65
|
+
export interface EndedEvent {
|
|
66
|
+
type: 'ended';
|
|
67
|
+
}
|
|
68
|
+
export type ShiftEvent = DispatchedEvent | ReapedEvent | FailedEvent | GateEvent | EndedEvent;
|
|
69
|
+
export interface ShiftCapacity {
|
|
70
|
+
cap: number;
|
|
71
|
+
free: number;
|
|
72
|
+
running: number;
|
|
73
|
+
events: ShiftEvent[];
|
|
74
|
+
}
|
|
75
|
+
export interface ShiftStatus {
|
|
76
|
+
name: string;
|
|
77
|
+
serve_crews: string[];
|
|
78
|
+
cap: number;
|
|
79
|
+
free: number;
|
|
80
|
+
running: number;
|
|
81
|
+
attended_at: number | null;
|
|
82
|
+
started_at: number;
|
|
83
|
+
}
|
|
84
|
+
export interface ShiftError {
|
|
85
|
+
error: string;
|
|
86
|
+
}
|
|
87
|
+
export type ShiftResponse = ShiftCapacity | ShiftStatus | ShiftError | {
|
|
88
|
+
ok: true;
|
|
89
|
+
ended: true;
|
|
90
|
+
};
|
|
91
|
+
export declare function isShiftError(value: unknown): value is ShiftError;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local JSON-lines protocol for the foreground shift daemon.
|
|
3
|
+
*
|
|
4
|
+
* The protocol is deliberately small: one request per Unix-socket connection,
|
|
5
|
+
* one response line, then the connection closes. The hub remains the owner of
|
|
6
|
+
* workflow state; this protocol only carries local dispatch observations and
|
|
7
|
+
* attendance commands between the CLI and one daemon process.
|
|
8
|
+
*/
|
|
9
|
+
export const SHIFT_SOCKET_NAME = 'shift.sock';
|
|
10
|
+
export const DEFAULT_NEXT_WAIT_MS = 90_000;
|
|
11
|
+
export const MAX_REQUEST_LINE_BYTES = 64 * 1024;
|
|
12
|
+
/**
|
|
13
|
+
* The server measures each response's serialized UTF-8 byte length against this
|
|
14
|
+
* ceiling and retains FIFO events that do not fit. A single event whose fields
|
|
15
|
+
* exceed the ceiling is rendered with an explicit truncation marker.
|
|
16
|
+
*/
|
|
17
|
+
export const MAX_RESPONSE_LINE_BYTES = 512 * 1024;
|
|
18
|
+
export const RESPONSE_TRUNCATION_MARKER = '[truncated]';
|
|
19
|
+
export const MAX_EVENT_QUEUE = 1_000;
|
|
20
|
+
export const OVERLAP_ERROR = 'whats_next is already parked — one park at a time (cancel it or wait for it to return)';
|
|
21
|
+
export const NO_DAEMON_SUFFIX = ' — start one with: owenloop shift start <crew…>';
|
|
22
|
+
export function isShiftError(value) {
|
|
23
|
+
return typeof value === 'object' && value !== null && typeof value.error === 'string';
|
|
24
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command-order routing (plan decision 5). owenloop owns the `x.owenloop` bag
|
|
3
|
+
* on a step — owenloop treats `x` as opaque — and this module defines its one
|
|
4
|
+
* field so far: `routing: 'shift' | 'manual'`.
|
|
5
|
+
*
|
|
6
|
+
* The knob answers ONE question for a `executor: 'command'` step: does THIS shift
|
|
7
|
+
* auto-dispatch it, or leave it for a human/session to pick up? Two inputs feed
|
|
8
|
+
* the decision — the machine-level `commandRouting` setting (default `'shift'`)
|
|
9
|
+
* and the per-step `x.owenloop.routing` override — and MOST RESTRICTIVE WINS:
|
|
10
|
+
* if either says `'manual'`, the shift does not auto-run the command. An
|
|
11
|
+
* invalid/unknown value anywhere fails CLOSED to `'manual'` plus a warning —
|
|
12
|
+
* we never auto-run a command step on a value we could not parse.
|
|
13
|
+
*
|
|
14
|
+
* Agent orders are NOT routed through this knob (decision 5) — only command
|
|
15
|
+
* steps. Pure functions; no I/O, no side effects beyond the returned warnings.
|
|
16
|
+
*/
|
|
17
|
+
import type { FetchedStep } from '../bundle/types.ts';
|
|
18
|
+
export type Routing = 'shift' | 'manual';
|
|
19
|
+
/** True for a `executor: 'command'` step (owenloop RAW_STEP_KEYS `executor`). */
|
|
20
|
+
export declare function isCommandStep(step: FetchedStep): boolean;
|
|
21
|
+
export interface RoutingResolution {
|
|
22
|
+
/** The resolved routing after applying most-restrictive-wins. */
|
|
23
|
+
routing: Routing;
|
|
24
|
+
/** Convenience: whether the shift should auto-dispatch this command order. */
|
|
25
|
+
autoDispatch: boolean;
|
|
26
|
+
/** Human-readable warnings (invalid values that failed closed). */
|
|
27
|
+
warnings: string[];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Resolve whether the shift auto-dispatches a command step. `machineRaw` is the
|
|
31
|
+
* `commandRouting` setting value (unknown — it came from a JSON file); the step
|
|
32
|
+
* override is read from `x.owenloop.routing`. Most restrictive wins; invalid
|
|
33
|
+
* anywhere ⇒ manual + warning.
|
|
34
|
+
*/
|
|
35
|
+
export declare function resolveCommandRouting(machineRaw: unknown, step: FetchedStep): RoutingResolution;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** True for a `executor: 'command'` step (owenloop RAW_STEP_KEYS `executor`). */
|
|
2
|
+
export function isCommandStep(step) {
|
|
3
|
+
return step.executor === 'command';
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Coerce an unknown into a `Routing`. `undefined` maps to `absentDefault` with
|
|
7
|
+
* no warning (the field simply wasn't set); any other non-`Routing` value is an
|
|
8
|
+
* error that fails closed to `'manual'` and appends a warning.
|
|
9
|
+
*/
|
|
10
|
+
function coerceRouting(raw, absentDefault, label, warnings) {
|
|
11
|
+
if (raw === undefined)
|
|
12
|
+
return absentDefault;
|
|
13
|
+
if (raw === 'shift' || raw === 'manual')
|
|
14
|
+
return raw;
|
|
15
|
+
warnings.push(`invalid ${label} '${String(raw)}' — failing closed to 'manual' (command not auto-run)`);
|
|
16
|
+
return 'manual';
|
|
17
|
+
}
|
|
18
|
+
/** Read `x.owenloop.routing` off a step, distinguishing a malformed bag. */
|
|
19
|
+
function stepRoutingRaw(step) {
|
|
20
|
+
const x = step.x;
|
|
21
|
+
if (x === undefined || typeof x !== 'object' || x === null)
|
|
22
|
+
return { raw: undefined, malformedBag: false };
|
|
23
|
+
const bag = x['owenloop'];
|
|
24
|
+
if (bag === undefined)
|
|
25
|
+
return { raw: undefined, malformedBag: false };
|
|
26
|
+
if (typeof bag !== 'object' || bag === null || Array.isArray(bag))
|
|
27
|
+
return { raw: undefined, malformedBag: true };
|
|
28
|
+
return { raw: bag['routing'], malformedBag: false };
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Resolve whether the shift auto-dispatches a command step. `machineRaw` is the
|
|
32
|
+
* `commandRouting` setting value (unknown — it came from a JSON file); the step
|
|
33
|
+
* override is read from `x.owenloop.routing`. Most restrictive wins; invalid
|
|
34
|
+
* anywhere ⇒ manual + warning.
|
|
35
|
+
*/
|
|
36
|
+
export function resolveCommandRouting(machineRaw, step) {
|
|
37
|
+
const warnings = [];
|
|
38
|
+
const machine = coerceRouting(machineRaw, 'shift', 'commandRouting setting', warnings);
|
|
39
|
+
const { raw: overrideRaw, malformedBag } = stepRoutingRaw(step);
|
|
40
|
+
let override;
|
|
41
|
+
if (malformedBag) {
|
|
42
|
+
warnings.push(`step '${step.name}' has a malformed x.owenloop bag — failing closed to 'manual'`);
|
|
43
|
+
override = 'manual';
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
override = coerceRouting(overrideRaw, 'shift', `x.owenloop.routing on step '${step.name}'`, warnings);
|
|
47
|
+
}
|
|
48
|
+
const routing = machine === 'manual' || override === 'manual' ? 'manual' : 'shift';
|
|
49
|
+
return { routing, autoDispatch: routing === 'shift', warnings };
|
|
50
|
+
}
|