@edgehero/pi-dispatch 2.1.0 → 3.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.
Files changed (72) hide show
  1. package/.env.example +41 -5
  2. package/README.md +11 -5
  3. package/deploy/docker-compose.yml +12 -0
  4. package/deploy/egress-proxy.conf +28 -3
  5. package/deploy/pi-dispatch-egress-proxy.container +8 -2
  6. package/package.json +9 -2
  7. package/src/allocation.mjs +731 -0
  8. package/src/backends.mjs +243 -0
  9. package/src/budget.mjs +40 -4
  10. package/src/cli.mjs +222 -11
  11. package/src/config.mjs +126 -5
  12. package/src/daemon-facts.mjs +3 -0
  13. package/src/deployment-venue.mjs +1 -0
  14. package/src/doctor.mjs +2280 -195
  15. package/src/dollar-budget.mjs +373 -0
  16. package/src/dollar-fingerprint.mjs +83 -0
  17. package/src/egress-cli.mjs +316 -0
  18. package/src/egress-proxy-state.mjs +35 -5
  19. package/src/egress.mjs +12 -0
  20. package/src/env-allowlist.mjs +125 -6
  21. package/src/env-file.mjs +194 -25
  22. package/src/envelope.mjs +413 -0
  23. package/src/exit-code.mjs +22 -0
  24. package/src/fleet-lease.mjs +85 -25
  25. package/src/get-token.mjs +16 -5
  26. package/src/git-dirty.mjs +67 -0
  27. package/src/github-app-setup.mjs +6 -3
  28. package/src/github-host.mjs +5 -3
  29. package/src/host-pi.mjs +1 -1
  30. package/src/identity.mjs +2 -1
  31. package/src/image-preflight.mjs +98 -24
  32. package/src/image-ref.mjs +37 -0
  33. package/src/import-pi.mjs +4 -2
  34. package/src/index.mjs +407 -62
  35. package/src/init.mjs +18 -0
  36. package/src/job-id.mjs +26 -3
  37. package/src/live-probes.mjs +24 -9
  38. package/src/model-catalog.mjs +297 -0
  39. package/src/model-endpoints.mjs +671 -0
  40. package/src/model-ref.mjs +151 -0
  41. package/src/models-json.mjs +268 -0
  42. package/src/money.mjs +144 -0
  43. package/src/octokit-log.mjs +65 -0
  44. package/src/outbox-plan.mjs +218 -0
  45. package/src/outbox.mjs +29 -9
  46. package/src/output-cap.mjs +157 -0
  47. package/src/pause-windows.mjs +81 -2
  48. package/src/pi-model-loader.mjs +77 -0
  49. package/src/podman-stack.mjs +16 -3
  50. package/src/portfolio-snapshot.mjs +304 -0
  51. package/src/prepare-local.mjs +247 -12
  52. package/src/prepare.mjs +35 -3
  53. package/src/priorities.mjs +569 -0
  54. package/src/processor.mjs +599 -170
  55. package/src/project-id.mjs +17 -0
  56. package/src/projects.mjs +238 -0
  57. package/src/provider-steering.mjs +179 -65
  58. package/src/queue.mjs +111 -6
  59. package/src/reserved-env.mjs +31 -0
  60. package/src/run-container.mjs +59 -5
  61. package/src/run-history.mjs +379 -24
  62. package/src/run-mirror.mjs +30 -0
  63. package/src/runtime-settings.mjs +104 -9
  64. package/src/schedules.mjs +33 -1
  65. package/src/scoped-limits.mjs +447 -27
  66. package/src/service.mjs +15 -4
  67. package/src/session-store.mjs +131 -6
  68. package/src/start.mjs +528 -40
  69. package/src/triggers-file.mjs +65 -4
  70. package/src/triggers.mjs +141 -11
  71. package/src/up.mjs +308 -34
  72. package/src/valkey-endpoint.mjs +3 -2
@@ -433,7 +433,8 @@ export function makeCheckOnceSpent({ triggersPath, fs = nodeFs }) {
433
433
  }
434
434
 
435
435
  /**
436
- * Detect the version skew `run.waitFor` opens (issue #230), pre-spend, from the worker's own file read.
436
+ * Detect the version skew `run.waitFor` opens (issue #230), pre-spend, from the worker's own file read, and the
437
+ * same skew for every field in `AUTHORED_NARROWING_FIELDS` (issue #502).
437
438
  *
438
439
  * The hazard is `DES-TRIGGERS-UNIFIED-FILE`'s widening rule arriving somewhere it has never bitten. Unknown
439
440
  * keys DROP, which for every previous field was a harmless no-op: an old parser meeting `run.image` gives
@@ -455,6 +456,27 @@ export function makeCheckOnceSpent({ triggersPath, fs = nodeFs }) {
455
456
  * "run", because a broken read must never wedge every job. Only a positive, identity-confirmed mismatch --
456
457
  * this entry authored conditions, this job carries none -- refuses.
457
458
  */
459
+ /**
460
+ * The authored NARROWING fields the skew check covers besides `waitFor` (issues #502, #501): a `run` key whose absence
461
+ * WIDENS the job, so a service that drops it (a receiver below the version floor, or a current one reading a dead
462
+ * inode through compose's single-file `:ro` mount) turns a narrowed trigger into an unrestricted job that records
463
+ * as a clean run. `authored` says whether the entry in the file carries the field; the job is skewed when it does
464
+ * and the job arrived with the key absent or null. A new field of this class is ONE row here. `waitFor` keeps its
465
+ * own branch below because its refusal counts conditions and has its own reason (`wait-skew`); every row here is
466
+ * refused as `trigger-skew`, naming the field.
467
+ *
468
+ * The job checked must be the job as it ARRIVED (`job.data`), never one the worker filled: the effective job
469
+ * carries the deployment's PI_ALLOWED_MODELS as `models`, which would hide exactly the drop this exists to see.
470
+ * index.mjs hands this check the arrived value.
471
+ */
472
+ export const AUTHORED_NARROWING_FIELDS = Object.freeze([
473
+ Object.freeze({ key: "models", authored: (v) => Array.isArray(v) && v.length > 0 }),
474
+ // Issue #501: a trigger's per-job dollar cap. Dropped by a receiver from before #501 (which tolerated the key as
475
+ // unknown), the job would run under the deployment's wider cap, or with none, on a clean record. Any present
476
+ // value counts as authored; whether it is a valid amount is the loader's question, not this one.
477
+ Object.freeze({ key: "maxCostUsd", authored: (v) => v !== undefined && v !== null }),
478
+ ]);
479
+
458
480
  export function makeCheckWaitSkew({ triggersPath, fs = nodeFs }) {
459
481
  // Cached by mtime, unlike `checkOnceSpent` which re-reads every time. The difference is which jobs each
460
482
  // one runs for: the one-shot check is gated on `matched.once === true`, so it is rare by construction and
@@ -478,19 +500,28 @@ export function makeCheckWaitSkew({ triggersPath, fs = nodeFs }) {
478
500
  }
479
501
  };
480
502
 
503
+ // STRICT, like `waitFor` since #230 (PR #536's review, round 3): a job whose authored entry carries a narrowing
504
+ // field it arrived without is refused, even when the operator ADDED that field after the job was queued. A
505
+ // fail-open fenced on the file's mtime was tried and removed: ANY later write of the file (the worker's own
506
+ // one-shot disarm, a console edit, a touch, an `rsync -t` mtime in the future) reopened the hole for every job.
507
+ // Refusing a job queued before the edit is the safe direction, and its comment says to re-run it.
481
508
  return async function checkWaitSkew(job) {
482
509
  if (typeof triggersPath !== "string" || triggersPath === "") return { ok: true };
483
510
  // Cron and CLI jobs carry no matched index, and cannot carry `waitFor` at all.
484
511
  const index = job?.trigger?.matched?.index;
485
512
  if (!Number.isInteger(index)) return { ok: true };
486
513
  // The field arrived. Whether its conditions are SATISFIED is the gate's business, not this check's.
487
- if (Array.isArray(job?.waitFor) && job.waitFor.length > 0) return { ok: true };
514
+ const waitArrived = Array.isArray(job?.waitFor) && job.waitFor.length > 0;
515
+ const missing = AUTHORED_NARROWING_FIELDS.filter((row) => job?.[row.key] === undefined || job[row.key] === null);
516
+ if (waitArrived && missing.length === 0) return { ok: true };
488
517
 
489
518
  const triggers = read();
490
519
  if (triggers === null) return { ok: true };
491
520
  const entry = triggers[index];
492
521
  const authored = entry?.run?.waitFor;
493
- if (!Array.isArray(authored) || authored.length === 0) return { ok: true };
522
+ const waitSkewed = !waitArrived && Array.isArray(authored) && authored.length > 0;
523
+ const dropped = missing.find((row) => row.authored(entry?.run?.[row.key]));
524
+ if (!waitSkewed && dropped === undefined) return { ok: true };
494
525
 
495
526
  // The identity guard readDisarmState keeps, and WIDER than flow alone. `triggerIndex` is a RAW array
496
527
  // position, so an insertion, a reorder, or a stray `triggers.json` at the worker's cwd can all put a
@@ -505,7 +536,37 @@ export function makeCheckWaitSkew({ triggersPath, fs = nodeFs }) {
505
536
  const onType = job?.trigger?.matched?.type;
506
537
  if (typeof onType === "string" && entry?.on?.type !== onType) return { ok: true };
507
538
 
508
- return { skewed: true, conditions: authored.length };
539
+ if (waitSkewed) return { skewed: true, conditions: authored.length };
540
+ return { skewed: true, field: dropped.key };
541
+ };
542
+ }
543
+
544
+ /**
545
+ * Issue #505: `(job) => boolean`, whether the LIVE triggers file still flags the job's cron trigger
546
+ * `run.portfolio: true`. A portfolio job is queued with the flag on its data (a scheduler tick, or
547
+ * `pi-dispatch run --trigger`), and the flag is budget authority, so the data alone is never trusted: the operator
548
+ * removing the flag from the reviewed file must take effect for a job already queued.
549
+ *
550
+ * The same file the one-shot checks read (`PI_TRIGGERS_FILE`, else `./triggers.json` in the worker's working
551
+ * directory), parsed by the shared validator rather than read raw, because the answer gives authority and an entry
552
+ * the loader would refuse grants none. Found by the cron id, which the loader keeps unique, never by an index, and then
553
+ * held to the job: the entry's `folder`, `flow`, `command` and `task` must equal the job's. An id is a name, and a
554
+ * name can be reused: a job queued from an old entry must not borrow the flag of a different entry that took its id
555
+ * later (review of PR #575). Every doubt (no path, no file, a file that does not parse, no cron entry with that id, an
556
+ * entry that is not the one the job came from) answers false: the job then runs as an ordinary cron job, the direction
557
+ * that grants nothing. Never throws.
558
+ */
559
+ export function makeCheckPortfolioFlag({ triggersPath, fs = nodeFs }) {
560
+ return function checkPortfolioFlag(job) {
561
+ const id = job?.trigger?.id;
562
+ if (typeof triggersPath !== "string" || triggersPath === "" || typeof id !== "string") return false;
563
+ try {
564
+ const entry = parseTriggers(fs.readFileSync(triggersPath, "utf8"), triggersPath).find((t) => t.on.type === "cron" && t.on.id === id);
565
+ if (entry?.run?.portfolio !== true) return false;
566
+ return ["folder", "flow", "command", "task"].every((k) => entry.run[k] === job[k]);
567
+ } catch {
568
+ return false;
569
+ }
509
570
  };
510
571
  }
511
572
 
package/src/triggers.mjs CHANGED
@@ -32,14 +32,20 @@ import { imageRefProblem } from "./image-ref.mjs";
32
32
  import { SKILL_NAME_RE } from "./flow-gate.mjs";
33
33
  import { FORGE_HOST_VARS, FORGE_KINDS, MINTED_TOKEN_VARS, RUN_KINDS, forgeSpec, isForgeKind } from "./forges.mjs";
34
34
  import { findDuplicateKey } from "./json-duplicates.mjs";
35
+ import { validateModelRef } from "./model-ref.mjs";
36
+ import { parseUsdMicros } from "./money.mjs";
35
37
  import { PROVIDER_STEERING_VARS } from "./provider-steering.mjs";
36
- import { CONTAINER_ENV_NAMES } from "./reserved-env.mjs";
38
+ import { CONTAINER_ENV_NAMES, RUNNER_ENV_NAMES } from "./reserved-env.mjs";
37
39
  // The wait grammar's two shared halves (issue #230). `afterInstantMs` is imported rather than restated so
38
40
  // the loader and the pickup gate cannot disagree about what a legal instant is: a second spelling here is
39
41
  // how a file that loads clean starts holding for an instant nobody wrote. wait-for.mjs is pure and
40
42
  // fs-free, so importing it keeps parseTriggers pure.
41
43
  import { WAIT_CONDITION_KEYS, WAIT_CONDITION_MAX, afterInstantMs } from "./wait-for.mjs";
42
44
 
45
+ // Re-exported so the console checks a provider or model it is about to write with the loader's own
46
+ // grammar, through the subpath it already imports, rather than a copy that could drift.
47
+ export { validateModelRef };
48
+
43
49
  // EXPORTED for the same reason as PR_ACTIONS: the admin re-states this vocabulary to a MODEL.
44
50
  export const ON_TYPES = new Set(["cron", "label", "comment", "pull_request", "issue"]);
45
51
 
@@ -167,12 +173,14 @@ export const REVIEW_STATES = new Set(["approved", "changes_requested", "commente
167
173
  const REVIEW_ACTION = "review_submitted";
168
174
 
169
175
  /**
170
- * The pi tool names a trigger may exclude (issue #291) -- the built-in set of the PINNED pi, 0.99.1, in pi's
171
- * own `allToolNames` order. `powershell` joined it at the 0.99.1 bump (issue #509): pi registers it as a
176
+ * The pi tool names a trigger may exclude (issue #291) -- the built-in set of the PINNED pi (the version
177
+ * image/runner/package.json pins), in pi's own `allToolNames` order. `powershell` joined it at the 0.99.1 bump (issue #509): pi registers it as a
172
178
  * built-in but does not activate it by default (`DEFAULT_TOOL_NAMES` is read, bash, edit, write), so a
173
179
  * trigger that means "no shell" names it beside `bash`.
174
- * Hand-written because this validator is pure and pi-free (the worker does not depend on the agent
175
- * package), and therefore BOLTED twice to the artifact it restates: `worker/test/exclude-tools.pinned.test.mjs`
180
+ * A literal because this validator is pure and pi-free (the worker does not depend on the agent
181
+ * package). The line is GENERATED from the pinned pi by .github/scripts/pi-derived.mjs (issue #587; a pi bump
182
+ * reruns it), held to that generation by worker/test/pi-derived.test.mjs, and BOLTED twice to the artifact it
183
+ * restates: `worker/test/exclude-tools.pinned.test.mjs`
176
184
  * and `image/runner/test/pinned-api.test.mjs` both derive the set from the pinned package and fail with a
177
185
  * grow-these-together message when a pin bump moves it. Extension and custom tool names are deliberately
178
186
  * NOT here: they register at container start (a staged package, a serviced repo's own `.pi/extensions`),
@@ -228,7 +236,7 @@ const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
228
236
  * Every set is IMPORTED, never retyped, which is the rule `sandbox.test.mjs` keeps for the same reason: a
229
237
  * forge added to the table later must not need a second edit here to stay covered.
230
238
  */
231
- const RESERVED_ENV_NAMES = new Set([...MINTED_TOKEN_VARS, ...FORGE_HOST_VARS, ...WORKER_ONLY_SECRET_VARS, ...EGRESS_ENV_VARS, ...CONTAINER_ENV_NAMES, ...PROVIDER_STEERING_VARS]);
239
+ export const RESERVED_ENV_NAMES = new Set([...MINTED_TOKEN_VARS, ...FORGE_HOST_VARS, ...WORKER_ONLY_SECRET_VARS, ...EGRESS_ENV_VARS, ...CONTAINER_ENV_NAMES, ...RUNNER_ENV_NAMES, ...PROVIDER_STEERING_VARS]);
232
240
 
233
241
  function isNonEmptyString(value) {
234
242
  return typeof value === "string" && value.trim() !== "";
@@ -310,6 +318,13 @@ function normalizeTrigger(entry, index, path, state) {
310
318
  if (!isForgeKind(run.kind)) {
311
319
  throw configError(`${at}: a ${on.type} trigger is webhook-driven and produces a forge job; run.kind must be one of ${FORGE_KINDS.join("|")} (got ${JSON.stringify(run.kind)}): ${path}`);
312
320
  }
321
+ // Issue #505: `run.portfolio` is cron-only, refused here for every webhook type at once (and before the disarmed
322
+ // sentinel below, so a disarmed entry carrying it still refuses the file). A forge job is driven by text an issue or
323
+ // pull request author chose, and it has no `/outbox`, so a plan channel there would hand budget authority to whoever
324
+ // can open an issue. Any value is refused, `false` included: on a webhook kind the field has no meaning to state.
325
+ if (run.portfolio !== undefined) {
326
+ throw configError(`${at}: run.portfolio is only available on a cron trigger: a ${on.type} trigger runs on text a forge user wrote, and a budget plan must come from a schedule the operator reviewed: ${path}`);
327
+ }
313
328
  let normalized;
314
329
  if (on.type === "label") normalized = normalizeLabel(on, run, index, path);
315
330
  else if (on.type === "comment") normalized = normalizeComment(on, run, index, path, state);
@@ -401,6 +416,12 @@ function normalizeCron(on, run, index, path, state) {
401
416
  // A cron job's session narrows like any other: the field selects what the container's agent can do,
402
417
  // which is orthogonal to what triggered it (issue #291).
403
418
  const excludeTools = validateExcludeTools(on, run, `cron trigger "${id}"`, path);
419
+ // Validated since #502; before it these three were copied untouched, so `run.provider: 7` loaded.
420
+ const ref = validateRunModel(on, run, `cron trigger "${id}"`, path);
421
+ // Issue #505: the portfolio flag. Strictly boolean, the house rule (`run.github`'s reason: a truthy string must not
422
+ // quietly arm budget authority). `true` beside `run.command` is refused: a plan is written by a flow's judgement,
423
+ // and a command has no committed skill to review. `false` is today's default and is carried, `run.resume`'s rule.
424
+ const portfolio = validatePortfolio(run, command, `cron trigger "${id}"`, path);
404
425
 
405
426
  // provider/model/maxTurns stay absent when omitted so the value resolves at job start against the
406
427
  // settings overlay/env, not a default frozen here (INT-CONFIG-OVERLAY-CONTRACT). github/packages/image stay
@@ -409,10 +430,27 @@ function normalizeCron(on, run, index, path, state) {
409
430
  // freeze today's default into every stored repeatable.
410
431
  return {
411
432
  on: { type: "cron", id, pattern },
412
- run: { kind: "local", folder: run.folder, flow: run.flow, task: run.task, provider: run.provider, model: run.model, maxTurns: run.maxTurns, github: run.github, packages, image, resume, ...(command !== undefined && { command }), ...(skillsDir !== undefined && { skillsDir }), ...(secrets !== undefined && { secrets }), ...(secretsProfile !== undefined && { secretsProfile }), ...(backend !== undefined && { backend }), ...(excludeTools !== undefined && { excludeTools }) },
433
+ run: { kind: "local", folder: run.folder, flow: run.flow, task: run.task, provider: ref.provider, model: ref.model, maxTurns: ref.maxTurns, github: run.github, packages, image, resume, ...(command !== undefined && { command }), ...(skillsDir !== undefined && { skillsDir }), ...(secrets !== undefined && { secrets }), ...(secretsProfile !== undefined && { secretsProfile }), ...(backend !== undefined && { backend }), ...(excludeTools !== undefined && { excludeTools }), ...(ref.models !== undefined && { models: ref.models }), ...(ref.maxCostUsd !== undefined && { maxCostUsd: ref.maxCostUsd }), ...(portfolio !== undefined && { portfolio }) },
413
434
  };
414
435
  }
415
436
 
437
+ /**
438
+ * `run.portfolio` on a cron trigger (issue #505): the job may send a priorities plan back through
439
+ * `/outbox/priorities.json`. Returns the flag, or undefined when absent, so an unflagged entry normalizes
440
+ * byte-identically. Reviewed-file-only, like `run.secrets`: no tool and no panel key writes it.
441
+ */
442
+ function validatePortfolio(run, command, at, path) {
443
+ const portfolio = run.portfolio;
444
+ if (portfolio === undefined) return undefined;
445
+ if (typeof portfolio !== "boolean") {
446
+ throw configError(`${at}: run.portfolio must be true or false when present: ${path}`);
447
+ }
448
+ if (portfolio === true && command !== undefined) {
449
+ throw configError(`${at}: run.portfolio cannot be set beside run.command: a budget plan is written by a flow's judgement, and a command has no committed skill to review: ${path}`);
450
+ }
451
+ return portfolio;
452
+ }
453
+
416
454
  /**
417
455
  * Validate the per-trigger `run.packages` flag, shared by all four normalizers. It is an opt-OUT: the pi
418
456
  * packages an operator pinned into the global overlay load for every job, and `run.packages: false` is how a
@@ -1172,6 +1210,94 @@ function validateBackend(on, run, at, path, { localWorkspace }) {
1172
1210
  return backend;
1173
1211
  }
1174
1212
 
1213
+ /**
1214
+ * The `run` field a key spells or nearly spells (`run.provider`, `run.model`, `run.models`, `run.maxTurns`,
1215
+ * `run.maxCostUsd`), or null. The exact field names match too: the CALLER decides whether an exact name is legal
1216
+ * where it found it (the loader allows them under `run`, never under `on`; the console's trigger tools refuse
1217
+ * `run.models` and `run.maxCostUsd` in any spelling, issue #501 part 7). One sweep, so the two cannot disagree on
1218
+ * what a near miss is.
1219
+ */
1220
+ export function runModelFieldNearMiss(key) {
1221
+ // What the sweep compares against, case and separators removed. `providerid` and `modelid` are what an
1222
+ // operator copies from a provider's own API docs; `allowedmodels` is the issue's own name for the list.
1223
+ // Deliberately NOT a synonym hunt (`llm`, `engine`): that is the general unknown-key sweep this file's
1224
+ // forward-compatibility posture rejects.
1225
+ // Singular and plural of each, excludeTools' rule: `maxTurn` and `providers` are one grammatical step
1226
+ // from the field, and `modelName` is how several provider SDKs spell the id.
1227
+ const targets = RUN_MODEL_NEAR_MISS_TARGETS;
1228
+ const isSubsequence = (needle, hay) => {
1229
+ let i = 0;
1230
+ for (const ch of hay) if (i < needle.length && needle[i] === ch) i++;
1231
+ return i === needle.length;
1232
+ };
1233
+ const normalized = String(key).replace(/[^a-z0-9]/gi, "").toLowerCase();
1234
+ // The homoglyph branch is validateBackend's (its comment carries the rationale), floor 4 included:
1235
+ // `m<U+043E>del` normalizes to `mdel`, and a floor of 5 would let the shortest target's likeliest
1236
+ // homoglyph through. Only a non-ASCII key reaches this branch, so no ASCII field is at risk.
1237
+ // The suggestion names the target that actually MATCHED, so a homoglyph key is pointed at the field
1238
+ // its surviving letters spell rather than at whatever a prefix test would guess.
1239
+ const hit = /^[\x20-\x7E]*$/.test(key)
1240
+ ? (Object.hasOwn(targets, normalized) ? normalized : undefined)
1241
+ : normalized.length >= 4 ? Object.keys(targets).find((t) => isSubsequence(normalized, t)) : undefined;
1242
+ return hit === undefined ? null : targets[hit];
1243
+ }
1244
+
1245
+ const RUN_MODEL_NEAR_MISS_TARGETS = Object.freeze({
1246
+ provider: "run.provider", providers: "run.provider", providerid: "run.provider", providerids: "run.provider", providername: "run.provider",
1247
+ model: "run.model", modelid: "run.model", modelname: "run.model",
1248
+ models: "run.models", modelids: "run.models", modelnames: "run.models", allowedmodels: "run.models", allowedmodel: "run.models",
1249
+ maxturns: "run.maxTurns", maxturn: "run.maxTurns",
1250
+ maxcostusd: "run.maxCostUsd", maxcost: "run.maxCostUsd",
1251
+ });
1252
+
1253
+ /**
1254
+ * `run.provider`, `run.model` and `run.maxTurns` (issue #502): which model this trigger's job runs on, and
1255
+ * its turn limit. Every kind carries them; before #502 only cron did, and unchecked.
1256
+ *
1257
+ * The value grammar is `model-ref.mjs`'s, shared with the console. This wrapper adds the KEY sweep, in
1258
+ * `validateExcludeTools`' shape and for its reason: a misspelled `run.modelId` loads clean and the job runs
1259
+ * on the deployment default model while the file reads as though it chose one. That is a destructive
1260
+ * absence once a model is policy, and the price difference between two models is the whole point of
1261
+ * naming one.
1262
+ *
1263
+ * `run.maxCostUsd` (issue #501) is validated here too, since the sweep below already owns its near misses:
1264
+ * the per-job dollar cap, a plain decimal string or number (`parseUsdMicros`: above 0, at most 1,000,000,
1265
+ * at most 6 decimals), carried AS WRITTEN and emitted only when present (`null` is absent, the model keys'
1266
+ * rule). It can only NARROW: the worker runs the job under the smaller of it and the deployment's cap, and
1267
+ * refuses a file with one above the env cap when it loads it (`loadSchedules`, at boot and on a live reload;
1268
+ * this loader stays pure, so it cannot read env).
1269
+ *
1270
+ * `run.models` (the allowed-model list) is validated with them since #502's second part, and its misspellings
1271
+ * were refused here before it was.
1272
+ */
1273
+ function validateRunModel(on, run, at, path) {
1274
+ const exact = new Set(["provider", "model", "maxTurns", "models", "maxCostUsd"]);
1275
+ for (const [label, source, exactIsLegal] of [
1276
+ ["run", run, true],
1277
+ ["on", on, false],
1278
+ ]) {
1279
+ for (const key of Object.keys(source ?? {})) {
1280
+ if (exactIsLegal && exact.has(key)) continue;
1281
+ const meant = runModelFieldNearMiss(key);
1282
+ if (meant === null) continue;
1283
+ throw configError(`${at}: ${label}.${key} is not a field -- did you mean ${meant}? A model choice the loader drops runs the job on the deployment default while the file reads as though it chose, so a near miss is refused rather than dropped: ${path}`);
1284
+ }
1285
+ }
1286
+ return { ...validateModelRef(run, at, path), ...validateMaxCostUsd(run, at, path) };
1287
+ }
1288
+
1289
+ // `run.maxCostUsd` (issue #501): `{ maxCostUsd }` when present and valid, `{}` when absent or null. The
1290
+ // refusal names the trigger and the key and never the value, `parseUsdMicros`' rule.
1291
+ function validateMaxCostUsd(run, at, path) {
1292
+ if (run?.maxCostUsd === null || run?.maxCostUsd === undefined) return {};
1293
+ try {
1294
+ parseUsdMicros(run.maxCostUsd, "run.maxCostUsd");
1295
+ } catch (error) {
1296
+ throw configError(`${at}: ${error.message}: ${path}`);
1297
+ }
1298
+ return { maxCostUsd: run.maxCostUsd };
1299
+ }
1300
+
1175
1301
  /**
1176
1302
  * `run.excludeTools` -- pi tool names this trigger's session must NOT have (issue #291).
1177
1303
  *
@@ -1417,9 +1543,10 @@ function normalizeLabel(on, run, index, path) {
1417
1543
  const backend = validateBackend(on, run, at, path, { localWorkspace: false });
1418
1544
  const waitFor = validateWaitFor(on, run, at, path, { onType: on.type });
1419
1545
  const excludeTools = validateExcludeTools(on, run, at, path);
1546
+ const ref = validateRunModel(on, run, at, path);
1420
1547
  return {
1421
1548
  on: { type: "label", any: predicate.any, all: predicate.all, none: predicate.none },
1422
- run: { kind: run.kind, flow: run.flow, packages, image, resume, replicas, ...(command !== undefined && { command }), ...(skillsDir !== undefined && { skillsDir }), ...(instructions !== undefined && { instructions }), ...(repository !== undefined && { repository }), ...(secrets !== undefined && { secrets }), ...(secretsProfile !== undefined && { secretsProfile }), ...(waitFor !== undefined && { waitFor }), ...(backend !== undefined && { backend }), ...(excludeTools !== undefined && { excludeTools }) },
1549
+ run: { kind: run.kind, flow: run.flow, packages, image, resume, replicas, ...(command !== undefined && { command }), ...(skillsDir !== undefined && { skillsDir }), ...(instructions !== undefined && { instructions }), ...(repository !== undefined && { repository }), ...(secrets !== undefined && { secrets }), ...(secretsProfile !== undefined && { secretsProfile }), ...(waitFor !== undefined && { waitFor }), ...(backend !== undefined && { backend }), ...(excludeTools !== undefined && { excludeTools }), ...ref },
1423
1550
  };
1424
1551
  }
1425
1552
 
@@ -1458,9 +1585,10 @@ function normalizeComment(on, run, index, path, state) {
1458
1585
  const backend = validateBackend(on, run, at, path, { localWorkspace: false });
1459
1586
  const waitFor = validateWaitFor(on, run, at, path, { onType: on.type });
1460
1587
  const excludeTools = validateExcludeTools(on, run, at, path);
1588
+ const ref = validateRunModel(on, run, at, path);
1461
1589
  return {
1462
1590
  on: { type: "comment", phrase: on.phrase },
1463
- run: { kind: run.kind, flow: run.flow, packages, image, resume, replicas, ...(command !== undefined && { command }), ...(skillsDir !== undefined && { skillsDir }), ...(instructions !== undefined && { instructions }), ...(repository !== undefined && { repository }), ...(secrets !== undefined && { secrets }), ...(secretsProfile !== undefined && { secretsProfile }), ...(waitFor !== undefined && { waitFor }), ...(backend !== undefined && { backend }), ...(excludeTools !== undefined && { excludeTools }) },
1591
+ run: { kind: run.kind, flow: run.flow, packages, image, resume, replicas, ...(command !== undefined && { command }), ...(skillsDir !== undefined && { skillsDir }), ...(instructions !== undefined && { instructions }), ...(repository !== undefined && { repository }), ...(secrets !== undefined && { secrets }), ...(secretsProfile !== undefined && { secretsProfile }), ...(waitFor !== undefined && { waitFor }), ...(backend !== undefined && { backend }), ...(excludeTools !== undefined && { excludeTools }), ...ref },
1464
1592
  };
1465
1593
  }
1466
1594
 
@@ -1526,6 +1654,7 @@ function normalizeIssue(on, run, index, path) {
1526
1654
  const backend = validateBackend(on, run, at, path, { localWorkspace: false });
1527
1655
  const waitFor = validateWaitFor(on, run, at, path, { onType: on.type });
1528
1656
  const excludeTools = validateExcludeTools(on, run, at, path);
1657
+ const ref = validateRunModel(on, run, at, path);
1529
1658
  return {
1530
1659
  on: {
1531
1660
  type: "issue",
@@ -1535,7 +1664,7 @@ function normalizeIssue(on, run, index, path) {
1535
1664
  ...(number !== undefined && { number }),
1536
1665
  ...(once !== undefined && { once }),
1537
1666
  },
1538
- run: { kind: run.kind, flow: run.flow, packages, image, resume, replicas, ...(command !== undefined && { command }), ...(skillsDir !== undefined && { skillsDir }), ...(instructions !== undefined && { instructions }), ...(secrets !== undefined && { secrets }), ...(secretsProfile !== undefined && { secretsProfile }), ...(waitFor !== undefined && { waitFor }), ...(backend !== undefined && { backend }), ...(excludeTools !== undefined && { excludeTools }) },
1667
+ run: { kind: run.kind, flow: run.flow, packages, image, resume, replicas, ...(command !== undefined && { command }), ...(skillsDir !== undefined && { skillsDir }), ...(instructions !== undefined && { instructions }), ...(secrets !== undefined && { secrets }), ...(secretsProfile !== undefined && { secretsProfile }), ...(waitFor !== undefined && { waitFor }), ...(backend !== undefined && { backend }), ...(excludeTools !== undefined && { excludeTools }), ...ref },
1539
1668
  };
1540
1669
  }
1541
1670
 
@@ -1612,6 +1741,7 @@ function normalizePullRequest(on, run, index, path) {
1612
1741
  const backend = validateBackend(on, run, at, path, { localWorkspace: false });
1613
1742
  const waitFor = validateWaitFor(on, run, at, path, { onType: on.type });
1614
1743
  const excludeTools = validateExcludeTools(on, run, at, path);
1744
+ const ref = validateRunModel(on, run, at, path);
1615
1745
  return {
1616
1746
  on: {
1617
1747
  type: "pull_request",
@@ -1626,7 +1756,7 @@ function normalizePullRequest(on, run, index, path) {
1626
1756
  ...(number !== undefined && { number }),
1627
1757
  ...(once !== undefined && { once }),
1628
1758
  },
1629
- run: { kind: run.kind, flow: run.flow, packages, image, resume, replicas, ...(command !== undefined && { command }), ...(skillsDir !== undefined && { skillsDir }), ...(instructions !== undefined && { instructions }), ...(secrets !== undefined && { secrets }), ...(secretsProfile !== undefined && { secretsProfile }), ...(waitFor !== undefined && { waitFor }), ...(backend !== undefined && { backend }), ...(excludeTools !== undefined && { excludeTools }) },
1759
+ run: { kind: run.kind, flow: run.flow, packages, image, resume, replicas, ...(command !== undefined && { command }), ...(skillsDir !== undefined && { skillsDir }), ...(instructions !== undefined && { instructions }), ...(secrets !== undefined && { secrets }), ...(secretsProfile !== undefined && { secretsProfile }), ...(waitFor !== undefined && { waitFor }), ...(backend !== undefined && { backend }), ...(excludeTools !== undefined && { excludeTools }), ...ref },
1630
1760
  };
1631
1761
  }
1632
1762