pi-durable-subagents 1.0.43 → 1.1.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 +106 -0
- package/README.md +254 -27
- package/dist/agent/main/schema.js +6 -2
- package/dist/agent/main/snapshots.js +50 -4
- package/dist/agent/main/tool.js +18 -6
- package/dist/agent/main.js +6 -2
- package/dist/cli/hold.js +85 -80
- package/dist/cli/main.js +11 -4
- package/dist/cli/requests.js +70 -17
- package/dist/compat/fanout.js +13 -5
- package/dist/compat/spec.js +46 -1
- package/dist/events/waiting.js +16 -1
- package/dist/orchestrator/engine.js +200 -16
- package/dist/orchestrator/executor/command.js +61 -0
- package/dist/orchestrator/executor/ended.js +23 -0
- package/dist/orchestrator/executor/index.js +377 -11
- package/dist/orchestrator/executor/sweep.js +5 -1
- package/dist/orchestrator/main.js +4 -2
- package/dist/orchestrator/reader-ledger.js +65 -5
- package/dist/orchestrator/restart.js +4 -1
- package/dist/orchestrator/settled.js +6 -0
- package/dist/orchestrator/snapshot.js +104 -11
- package/dist/orchestrator/store.js +4 -4
- package/dist/platform/cmdline.js +16 -0
- package/dist/platform/command.js +98 -0
- package/dist/platform/containment.js +34 -0
- package/dist/platform/launch.js +13 -3
- package/dist/platform/lease.js +91 -44
- package/dist/platform/proctable.js +43 -5
- package/dist/requests.js +1 -1
- package/dist/types.js +2 -0
- package/dist/ui/screen.js +62 -13
- package/dist/ui/tool.js +8 -4
- package/dist/ui/view.js +18 -7
- package/package.json +1 -1
package/dist/cli/requests.js
CHANGED
|
@@ -8,8 +8,8 @@ import { findJournalEntry, readJournalFold, readJournalSnapshot } from "../kerne
|
|
|
8
8
|
import { isResolution, lifecycleResolution } from "../kernel/lifecycle.js";
|
|
9
9
|
import { journalPath, orchLedger, pinnedDir } from "../paths.js";
|
|
10
10
|
import { batchConflict, batchDigest, findRequest, manyIds, namesMany, REQUEST_ID, requestId, requestRid, RequestsBusy, SESSION_ID, specDigest } from "../requests.js";
|
|
11
|
-
import { NOTIFY_SINCE, HOLD_ENTRY_TYPES, heldIn, isLive, orchestratorTooOld, slotsView, workflowSnapshot, writerWaits, writerWaitLine, WRITER_WAIT_HINT } from "../orchestrator/snapshot.js";
|
|
12
|
-
import { leaseCalls, leaseState } from "../platform/lease.js";
|
|
11
|
+
import { NOTIFY_SINCE, WAKE_SINCE, HOLD_ENTRY_TYPES, commandLine, commandSendRefusal, isCommandTarget, heldIn, isLive, orchestratorTooOld, slotsView, workflowSnapshot, writerWaits, writerWaitLine, WRITER_WAIT_HINT } from "../orchestrator/snapshot.js";
|
|
12
|
+
import { leaseCalls, leaseState, callAddress } from "../platform/lease.js";
|
|
13
13
|
import { BATCH, checkAgents, followUpReceipt, outcomeLine, request, sendReceipt } from "../agent/main/tool.js";
|
|
14
14
|
import { discoverAgents } from "../compat/agents.js";
|
|
15
15
|
import { JT } from "../types.js";
|
|
@@ -17,6 +17,7 @@ import { startOrchestrator, submitIdentified } from "./control.js";
|
|
|
17
17
|
import { FENCE_LEDGER_TYPES, endedExecs, fenceReason } from "../events/fence.js";
|
|
18
18
|
import { parseLabels } from "../events/labels.js";
|
|
19
19
|
import { foldWaits, leaseWaits, waitsOf } from "../events/waiting.js";
|
|
20
|
+
import { awaitingRuns, wakeStatus } from "../orchestrator/reader-ledger.js";
|
|
20
21
|
import { LEDGER_ENTRY_TYPES, emptyLedger, foldLedger } from "../orchestrator/ledger.js";
|
|
21
22
|
export const EXIT = { ok: 0, rejected: 1, conflict: 3, pending: 75 };
|
|
22
23
|
/** Strict `--name value` / `--flag` parsing; unknown or repeated options are errors, except those given in `repeated`
|
|
@@ -88,7 +89,7 @@ export async function describe(home, key, now = Date.now()) {
|
|
|
88
89
|
// A send or stop has no workflow of its own: its decision is the outcome.
|
|
89
90
|
if (decided && found.request.kind !== "run")
|
|
90
91
|
return { state: "applied", ...head };
|
|
91
|
-
return { state: "pending", ...head };
|
|
92
|
+
return { state: "pending", ...head, ...(wakeStatus(home, found.request) ? { wake: wakeStatus(home, found.request) } : {}) };
|
|
92
93
|
}
|
|
93
94
|
/** `{labels}` of a run request that has any. */
|
|
94
95
|
function labelsOf(request) {
|
|
@@ -121,7 +122,7 @@ function describeWorkflow(home, wid, entries, now) {
|
|
|
121
122
|
const pruned = entries.find(e => e.type === "pruned" && e.wid === wid);
|
|
122
123
|
if (pruned)
|
|
123
124
|
return { state: "pruned", wid, pruned: { ...(pruned.status !== undefined ? { status: String(pruned.status) } : {}), endedAt: Number(pruned.endedAt) },
|
|
124
|
-
...(typeof pruned.request === "string" ? { request: pruned.request } : {}), ...(typeof pruned.spec_digest === "string" ? { spec_digest: pruned.spec_digest } : {}), ...labelsOf(runOf(home, entries, wid).run) };
|
|
125
|
+
...(typeof pruned.request === "string" ? { request: pruned.request } : {}), ...(typeof pruned.spec_digest === "string" ? { spec_digest: pruned.spec_digest } : {}), ...labelsOf(runOf(home, entries, wid).run), ...(pruned.wake ? { wake: pruned.wake } : {}) };
|
|
125
126
|
if (!/^[^/\\\0]+$/.test(wid) || wid === "." || wid === ".." || !existsSync(journalPath(home, wid)))
|
|
126
127
|
return { state: "absent", wid };
|
|
127
128
|
// The snapshot and the journal the wait fold reads must be the same bytes (a writer-wait appended between the two reads
|
|
@@ -144,7 +145,13 @@ function describeWorkflow(home, wid, entries, now) {
|
|
|
144
145
|
...(line(slots.slots, c.model) ? { slot: line(slots.slots, c.model) } : {}), ...(line(slots.exhausted, c.model) ? { exhausted: line(slots.exhausted, c.model) } : {}),
|
|
145
146
|
...(c.hibernated ? { hibernated: true } : {}), ...waits.get(c.callId)
|
|
146
147
|
};
|
|
147
|
-
|
|
148
|
+
const run = c.command !== undefined ? c.commandRun : undefined, live = !r && run !== undefined;
|
|
149
|
+
const awaiting = awaitingRuns(home, wid, c.key, wf.rev);
|
|
150
|
+
return { key: c.key, gen: c.gen, phase: c.phase, agent: c.agent, ...(c.phase === 'sealed' && c.endedAt !== undefined ? { sealedAt: c.endedAt } : {}), ...(awaiting ? { awaiting } : {}),
|
|
151
|
+
...(c.endedAtSeal ? { endedAtSeal: c.endedAtSeal } : {}), ...(c.endedAtSealMore ? { endedAtSealMore: c.endedAtSealMore } : {}), ...(c.model ? { model: c.model } : {}),
|
|
152
|
+
...(c.command !== undefined ? { command: commandLine(c.command, 200) } : {}), ...(run?.attempt !== undefined ? { attempt: run.attempt } : {}),
|
|
153
|
+
...(live && run.pid !== undefined ? { pid: run.pid } : {}), ...(live && c.startedAt !== undefined ? { elapsed: commandAge(now - c.startedAt) } : {}),
|
|
154
|
+
...(run?.log ? { log: run.log } : {}),
|
|
148
155
|
...(r ? { status: r.status, ok: r.ok, ...(r.error ? { error: r.error } : {}), output: r.output, ...(r.data !== undefined ? { data: r.data } : {}) } : {}),
|
|
149
156
|
...(waiting && Object.keys(waiting).length ? { waiting } : {}) };
|
|
150
157
|
});
|
|
@@ -154,7 +161,7 @@ function describeWorkflow(home, wid, entries, now) {
|
|
|
154
161
|
const state = questions.length ? "asking" : isLive(wf) || wf.status === "parked" ? "running" : "sealed";
|
|
155
162
|
const fence = lastFence(journal, entries);
|
|
156
163
|
return { state, wid, ...(id ? { request: id } : {}), ...(admitted ? { spec_digest: specDigest(admitted) } : {}), ...labelsOf(run), status: wf.status, ...(wf.error ? { error: wf.error } : {}),
|
|
157
|
-
calls, ...(questions.length ? { questions } : {}), ...(attention.length ? { attention } : {}), ...(fence ? { lastFence: fence } : {}) };
|
|
164
|
+
...(wakeStatus(home, run, wid) ? { wake: wakeStatus(home, run, wid) } : {}), calls, ...(questions.length ? { questions } : {}), ...(attention.length ? { attention } : {}), ...(fence ? { lastFence: fence } : {}) };
|
|
158
165
|
}
|
|
159
166
|
/** Best effort: why the latest fence that interrupted work happened. The per-execution classification and the
|
|
160
167
|
* reason are shared with the event log's `fenced` events (src/events/fence.ts). */
|
|
@@ -163,16 +170,33 @@ export function lastFence(journal, orch) {
|
|
|
163
170
|
const fence = journal.findLast(e => e.type === JT.fenced && !ended.has(String(e.exec)));
|
|
164
171
|
return fence ? { at: Number(fence.ts), exec: String(fence.exec), reason: fenceReason(journal, orch, fence) } : undefined;
|
|
165
172
|
}
|
|
173
|
+
/** A running command call's age: "42s", "3m05s", "1h02m". */
|
|
174
|
+
function commandAge(ms) {
|
|
175
|
+
const s = Math.max(0, Math.floor(ms / 1000));
|
|
176
|
+
return s < 60 ? `${s}s` : s < 3600 ? `${Math.floor(s / 60)}m${String(s % 60).padStart(2, "0")}s` : `${Math.floor(s / 3600)}h${String(Math.floor(s / 60) % 60).padStart(2, "0")}m`;
|
|
177
|
+
}
|
|
166
178
|
export function renderDescription(d) {
|
|
167
179
|
const lines = [`${d.request ?? d.wid}: ${d.state}${d.reason ? ` (${d.reason})` : ""}${d.wid && d.request ? ` — ${d.wid}` : ""}${d.status && d.state !== d.status ? ` · ${d.status}` : ""}`];
|
|
168
180
|
if (d.labels)
|
|
169
181
|
lines.push(` labels: ${Object.entries(d.labels).map(([k, v]) => `${k}=${v}`).join(" ")}`);
|
|
170
182
|
if (d.pruned)
|
|
171
183
|
lines.push(` pruned: ${d.pruned.status ?? "?"} at ${new Date(d.pruned.endedAt ?? 0).toISOString()}`);
|
|
184
|
+
if (d.wake)
|
|
185
|
+
lines.push(` wake: ${d.wake.to} ${d.wake.state}${d.wake.rev ? ` rev ${d.wake.rev}` : ''}${d.wake.generation ? ` generation ${d.wake.generation}` : ''}${d.wake.reason ? ` (${d.wake.reason})` : ''}`);
|
|
172
186
|
if (d.error)
|
|
173
187
|
lines.push(` error: ${d.error}`);
|
|
174
188
|
for (const c of d.calls ?? []) {
|
|
175
|
-
|
|
189
|
+
const facts = [c.pid !== undefined ? `pid ${c.pid}` : "", c.attempt !== undefined ? `attempt ${c.attempt}` : "", c.elapsed ?? ""].filter(Boolean).join(", ");
|
|
190
|
+
lines.push(c.command !== undefined
|
|
191
|
+
? ` ${c.key}@${c.gen} command ${c.phase}${c.status ? ` ${c.status}` : ""}${facts ? ` (${facts})` : ""}${c.error ? ` — ${c.error}` : ""}`
|
|
192
|
+
: ` ${c.key}@${c.gen} ${c.agent} ${c.phase}${c.status ? ` ${c.status}` : ""}${c.model ? ` (${c.model})` : ""}${c.error ? ` — ${c.error}` : ""}`);
|
|
193
|
+
if (c.command !== undefined)
|
|
194
|
+
lines.push(` command: ${c.command}`, ...(c.log ? [` log: ${c.log}`] : []));
|
|
195
|
+
if (c.awaiting)
|
|
196
|
+
for (const run of c.awaiting)
|
|
197
|
+
lines.push(` awaiting: ${run.request}${run.wid ? ` (${run.wid})` : ''} ${run.state}`);
|
|
198
|
+
if (c.endedAtSeal)
|
|
199
|
+
lines.push(` ended at seal: ${JSON.stringify(c.endedAtSeal)}${c.endedAtSealMore ? ` (${c.endedAtSealMore} more)` : ''}`);
|
|
176
200
|
if (c.waiting)
|
|
177
201
|
lines.push(` waiting: ${JSON.stringify(c.waiting)}`);
|
|
178
202
|
if (c.data !== undefined)
|
|
@@ -295,24 +319,43 @@ export function runSession(flag, env) {
|
|
|
295
319
|
return inherited && SESSION_ID.test(inherited) ? inherited : undefined;
|
|
296
320
|
}
|
|
297
321
|
/** `run --request <id> --spec <file|-> [--session <id>] [--cwd <dir>] [--json] [--wait-ms <n>]`. The spec is the `subagents` run
|
|
298
|
-
* form ({agent,task,…} or {tasks|chain:[…],…}); it is validated by the tool's own normalizer and agent check. */
|
|
322
|
+
* form ({agent,task,…}, {command,…} or {tasks|chain:[…],…}); it is validated by the tool's own normalizer and agent check. */
|
|
299
323
|
export const runCommand = (args, ctx) => refusable(args, ctx, runRequest);
|
|
300
324
|
async function runRequest(args, ctx, seen) {
|
|
301
|
-
const
|
|
325
|
+
const dash = args.indexOf("--"), argv = dash < 0 ? undefined : args.slice(dash + 1);
|
|
326
|
+
const { values, positionals } = flags(dash < 0 ? args : args.slice(0, dash), { request: "value", spec: "value", wake: "value", "timeout-ms": "value", name: "value", cwd: "value", labels: "value", session: "value", json: "flag", "wait-ms": "value" });
|
|
302
327
|
const id = text(values, "request"), file = text(values, "spec"), json = values.json === true, wait = waitMs(values, ctx);
|
|
303
328
|
seen.id = id;
|
|
304
329
|
seen.json = json;
|
|
305
|
-
if (
|
|
306
|
-
throw new Error("
|
|
330
|
+
if (file !== undefined && argv !== undefined)
|
|
331
|
+
throw new Error("--spec and -- are mutually exclusive");
|
|
332
|
+
if (argv === undefined && (values["timeout-ms"] !== undefined || values.name !== undefined))
|
|
333
|
+
throw new Error("--timeout-ms and --name require -- <program> [args…]");
|
|
334
|
+
if (!id || (file === undefined && argv === undefined) || positionals.length || (argv !== undefined && !argv[0]))
|
|
335
|
+
throw new Error("usage: run --request <id> [--wake caller] (--spec <file|-> | -- <program> [args…])");
|
|
336
|
+
const wake = text(values, "wake");
|
|
337
|
+
if (wake !== undefined && wake !== "caller")
|
|
338
|
+
throw new Error("--wake takes exactly caller");
|
|
339
|
+
if (wake && !ctx.env.DSA_CALL)
|
|
340
|
+
throw new Error("--wake caller needs DSA_CALL: run it from inside a subagent call");
|
|
341
|
+
if (wake && callAddress(ctx.env.DSA_CALL) === ctx.env.DSA_CALL)
|
|
342
|
+
throw new Error("--wake caller needs a valid DSA_CALL");
|
|
343
|
+
const timeout = text(values, "timeout-ms");
|
|
344
|
+
if (timeout !== undefined && (!/^[1-9]\d*$/.test(timeout) || !Number.isSafeInteger(Number(timeout))))
|
|
345
|
+
throw new Error("--timeout-ms needs a positive integer");
|
|
307
346
|
requestRid(id);
|
|
308
347
|
const raw = text(values, "labels"), labels = raw !== undefined ? parseLabels(raw) : undefined;
|
|
309
|
-
const bytes = file === "-" ? await (ctx.stdin ?? stdin)() : readFileSync(resolve(ctx.cwd ?? process.cwd(), file), "utf8");
|
|
310
348
|
let spec;
|
|
311
|
-
|
|
312
|
-
spec =
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
349
|
+
if (argv !== undefined)
|
|
350
|
+
spec = { command: argv, ...(timeout !== undefined ? { timeoutMs: Number(timeout) } : {}), ...(values.name !== undefined ? { name: text(values, "name") } : {}) };
|
|
351
|
+
else {
|
|
352
|
+
const bytes = file === "-" ? await (ctx.stdin ?? stdin)() : readFileSync(resolve(ctx.cwd ?? process.cwd(), file), "utf8");
|
|
353
|
+
try {
|
|
354
|
+
spec = JSON.parse(bytes);
|
|
355
|
+
}
|
|
356
|
+
catch (error) {
|
|
357
|
+
throw new Error(`--spec is not JSON: ${error.message}`);
|
|
358
|
+
}
|
|
316
359
|
}
|
|
317
360
|
if (!spec || typeof spec !== "object" || Array.isArray(spec))
|
|
318
361
|
throw new Error("--spec must be a JSON object");
|
|
@@ -331,10 +374,17 @@ async function runRequest(args, ctx, seen) {
|
|
|
331
374
|
const session = runSession(text(values, "session"), ctx.env);
|
|
332
375
|
if (session)
|
|
333
376
|
body.session = session;
|
|
377
|
+
if (wake) {
|
|
378
|
+
body.wake = { to: callAddress(ctx.env.DSA_CALL) };
|
|
379
|
+
body.caller = ctx.env.DSA_CALL;
|
|
380
|
+
}
|
|
334
381
|
seen.digest = specDigest({ kind: "run", body });
|
|
335
382
|
// An id already recorded is decided by that record: the same content gets its first outcome (the agents it named may
|
|
336
383
|
// have changed since), other content is a request-conflict. Only a new id is checked here.
|
|
337
384
|
const prior = (await findRequest(ctx.home, requestRid(id)))?.request;
|
|
385
|
+
const old = wake && !prior ? orchestratorTooOld(ctx.home, 'run --wake', WAKE_SINCE) : undefined;
|
|
386
|
+
if (old)
|
|
387
|
+
throw new Error(old);
|
|
338
388
|
if (!prior)
|
|
339
389
|
checkAgents(body, () => discoverAgents(dir, { home: ctx.env.HOME || undefined, agentDir: ctx.env.PI_CODING_AGENT_DIR || undefined }).agents.map(a => a.name));
|
|
340
390
|
const done = await submitAndWait(ctx, seen, id, "run", body, undefined, wait, json);
|
|
@@ -469,6 +519,9 @@ async function sendRequest(args, ctx, seen, batch) {
|
|
|
469
519
|
if (rev !== undefined && !/^\d+$/.test(rev))
|
|
470
520
|
throw new Error("--rev needs a positive integer");
|
|
471
521
|
let qid = text(values, "qid"), revision = rev !== undefined ? Number(rev) : undefined;
|
|
522
|
+
// R8: a command call has no questions: an answer is refused with the pinned reason, not "no open question".
|
|
523
|
+
if (kind === "answer" && (qid === undefined || revision === undefined) && isCommandTarget(ctx.home, where.to))
|
|
524
|
+
throw new Error(commandSendRefusal("answer"));
|
|
472
525
|
if (kind === "answer" && (qid === undefined || revision === undefined)) {
|
|
473
526
|
// The open question of the target call (or, on a retry, the one the original answered) supplies qid/rev.
|
|
474
527
|
const before = prior?.kind === "send" && prior.body.kind === "answer" && prior.body.to === where.to ? prior.cond : undefined;
|
package/dist/compat/fanout.js
CHANGED
|
@@ -17,13 +17,22 @@ export function compileFanout(input) {
|
|
|
17
17
|
return { key, spec: spec };
|
|
18
18
|
});
|
|
19
19
|
const prefix = `const steps = ${JSON.stringify(steps)};\n`;
|
|
20
|
-
const source = kind === "tasks"
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
const source = prefix + (kind === "tasks" ? TASKS_BODY : CHAIN_BODY);
|
|
21
|
+
return { source, steps };
|
|
22
|
+
}
|
|
23
|
+
const TASKS_BODY = "return await runs.all(steps.map(({key, spec}) => ({...spec, key})));\n";
|
|
24
|
+
/** A chain step's task gets the previous step's output as {previous}; a command step (no task) runs unchanged. */
|
|
25
|
+
const CHAIN_BODY = chainBody(`spec.command === undefined ? {...spec, task: spec.task.replaceAll("{previous}", () => previous)} : spec`);
|
|
26
|
+
/** The chain body before command calls (1.0.43 and earlier): workflows pinned with it keep running it. */
|
|
27
|
+
const LEGACY_CHAIN_BODY = chainBody(`{...spec, task: spec.task.replaceAll("{previous}", () => previous)}`);
|
|
28
|
+
/** Every script body compileFanout emits or emitted after its `const steps = [...];` line (a pinned fanout is one of them). */
|
|
29
|
+
export const FANOUT_BODIES = [TASKS_BODY, CHAIN_BODY, LEGACY_CHAIN_BODY];
|
|
30
|
+
function chainBody(stepSpec) {
|
|
31
|
+
return `const results = [];
|
|
23
32
|
let previous = "";
|
|
24
33
|
for (let i = 0; i < steps.length; i++) {
|
|
25
34
|
const {key, spec} = steps[i];
|
|
26
|
-
const result = await runs.run(key, {
|
|
35
|
+
const result = await runs.run(key, ${stepSpec});
|
|
27
36
|
results.push(result);
|
|
28
37
|
if (!result.ok) {
|
|
29
38
|
const skipped = steps.slice(i + 1).map(({key}) => ({key, gen: 0, status: "skipped", ok: false, output: ""}));
|
|
@@ -35,5 +44,4 @@ for (let i = 0; i < steps.length; i++) {
|
|
|
35
44
|
}
|
|
36
45
|
return results;
|
|
37
46
|
`;
|
|
38
|
-
return { source, steps };
|
|
39
47
|
}
|
package/dist/compat/spec.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { schemaProblems } from "../agent/child/schema.js";
|
|
2
|
-
const fields = new Set(["agent", "task", "model", "cwd", "timeoutMs", "output", "schema", "gate", "isolation", "context", "budget", "once", "tools", "skills", "key", "writer"]);
|
|
2
|
+
const fields = new Set(["agent", "task", "model", "cwd", "timeoutMs", "output", "schema", "gate", "isolation", "context", "budget", "once", "tools", "skills", "key", "writer", "command", "env", "rerun"]);
|
|
3
|
+
/** Fields a command call takes; every other known field belongs to agent calls. */
|
|
4
|
+
export const COMMAND_FIELDS = ["command", "cwd", "timeoutMs", "env", "rerun", "writer", "key"];
|
|
5
|
+
const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
3
6
|
const isObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
4
7
|
const positive = (v) => typeof v === "number" && Number.isFinite(v) && v > 0;
|
|
5
8
|
const text = (v) => typeof v === "string" && v.trim() !== "";
|
|
@@ -46,6 +49,11 @@ export function validateCallSpec(spec, options = {}) {
|
|
|
46
49
|
else if (k === "key" && !options.fanout)
|
|
47
50
|
errors.push("key is only allowed in tasks/chain steps");
|
|
48
51
|
}
|
|
52
|
+
if (has("command"))
|
|
53
|
+
return [...errors, ...commandProblems(s, options)];
|
|
54
|
+
for (const k of ["env", "rerun"])
|
|
55
|
+
if (has(k))
|
|
56
|
+
errors.push(`${k} is only allowed in a command call`);
|
|
49
57
|
if (!text(s.agent))
|
|
50
58
|
errors.push(`agent must be a non-empty string${has("agent") ? got(s.agent) : ""}`);
|
|
51
59
|
if (!text(s.task))
|
|
@@ -76,3 +84,40 @@ export function validateCallSpec(spec, options = {}) {
|
|
|
76
84
|
errors.push("key must be a non-empty string");
|
|
77
85
|
return errors;
|
|
78
86
|
}
|
|
87
|
+
/** A command call: command (argv array or sh string) plus cwd, timeoutMs, env, rerun, writer, key; nothing else. */
|
|
88
|
+
function commandProblems(s, options) {
|
|
89
|
+
const errors = [], has = (k) => s[k] !== undefined;
|
|
90
|
+
for (const k of Object.keys(s))
|
|
91
|
+
if (has(k) && fields.has(k) && !COMMAND_FIELDS.includes(k))
|
|
92
|
+
errors.push(`${k} is not allowed in a command call`);
|
|
93
|
+
const c = s.command;
|
|
94
|
+
if (Array.isArray(c)) {
|
|
95
|
+
if (!c.length || !c.every(v => typeof v === "string") || !text(c[0]))
|
|
96
|
+
errors.push(`command must be a non-empty string or an array of strings with a non-empty first element${got(c)}`);
|
|
97
|
+
}
|
|
98
|
+
else if (!text(c))
|
|
99
|
+
errors.push(`command must be a non-empty string or an array of strings with a non-empty first element${got(c)}`);
|
|
100
|
+
if (has("cwd") && typeof s.cwd !== "string")
|
|
101
|
+
errors.push(`cwd must be a string${got(s.cwd)}`);
|
|
102
|
+
if (has("timeoutMs") && !positive(s.timeoutMs))
|
|
103
|
+
errors.push(`timeoutMs must be a positive number (milliseconds)${got(s.timeoutMs)}`);
|
|
104
|
+
if (has("env")) {
|
|
105
|
+
if (!isObject(s.env))
|
|
106
|
+
errors.push(`env must be an object of strings${got(s.env)}`);
|
|
107
|
+
else
|
|
108
|
+
for (const [k, v] of Object.entries(s.env)) {
|
|
109
|
+
if (!ENV_NAME.test(k))
|
|
110
|
+
errors.push(`env name ${JSON.stringify(k)} is not a valid variable name`);
|
|
111
|
+
else if (k.startsWith("DSA_"))
|
|
112
|
+
errors.push(`env name ${k} is reserved (DSA_*)`);
|
|
113
|
+
if (typeof v !== "string")
|
|
114
|
+
errors.push(`env.${k} must be a string${got(v)}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
for (const k of ["rerun", "writer"])
|
|
118
|
+
if (has(k) && typeof s[k] !== "boolean")
|
|
119
|
+
errors.push(`${k} must be a boolean${got(s[k])}`);
|
|
120
|
+
if (options.fanout && has("key") && !text(s.key))
|
|
121
|
+
errors.push("key must be a non-empty string");
|
|
122
|
+
return errors;
|
|
123
|
+
}
|
package/dist/events/waiting.js
CHANGED
|
@@ -102,7 +102,19 @@ function applyWait(f, e) {
|
|
|
102
102
|
case "generation": {
|
|
103
103
|
const key = String(e.key), gen = Number(e.gen) || 1, call = `${f.wid}@${f.rev}/${key}@${gen}`, spec = e.spec;
|
|
104
104
|
const wanted = typeof e.model === "string" && e.model ? e.model : typeof spec?.model === "string" && spec.model ? spec.model : undefined;
|
|
105
|
-
f.calls.set(call, { call, key, gen, generation: e.type === "generation", ...(typeof spec?.agent === "string" ? { agent: spec.agent } : {}), ...(wanted ? { wanted } : {})
|
|
105
|
+
f.calls.set(call, { call, key, gen, generation: e.type === "generation", ...(typeof spec?.agent === "string" ? { agent: spec.agent } : {}), ...(wanted ? { wanted } : {}),
|
|
106
|
+
...(spec?.command !== undefined ? { command: true } : {}) });
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
case "command-intent": {
|
|
110
|
+
const c = f.calls.get(String(e.call));
|
|
111
|
+
if (c?.command) {
|
|
112
|
+
c.exec = String(e.id);
|
|
113
|
+
c.queued = e.ts;
|
|
114
|
+
c.selected = true;
|
|
115
|
+
c.fenced = false;
|
|
116
|
+
delete c.writer;
|
|
117
|
+
}
|
|
106
118
|
break;
|
|
107
119
|
}
|
|
108
120
|
case JT.exec: {
|
|
@@ -221,6 +233,9 @@ catch {
|
|
|
221
233
|
export function waitInput(f, c, env, agentModel) {
|
|
222
234
|
// An answered asker is queued (live) although its hibernated execution is fenced, unless a drain holds its workflow.
|
|
223
235
|
const answered = c.answered !== undefined && !env.held?.(f.wid);
|
|
236
|
+
// A command call queued behind a writer lock has no attempt yet: the lock is its wait (it takes no slot).
|
|
237
|
+
if (c.command && !c.exec)
|
|
238
|
+
return c.writer ? { asking: false, exec: { id: "", since: c.writer.since, selected: false }, writerWait: c.writer } : { asking: false };
|
|
224
239
|
const settings = settingsOf(env.ledger, env.config ?? {}), live = c.exec && (!c.fenced || answered), selected = c.selected && c.answered === undefined;
|
|
225
240
|
const attention = [];
|
|
226
241
|
let asking = false;
|