@edgehero/pi-dispatch 2.1.0 → 3.0.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 (71) 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 +8 -1
  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 +2261 -203
  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 +107 -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/identity.mjs +2 -1
  30. package/src/image-preflight.mjs +98 -24
  31. package/src/image-ref.mjs +37 -0
  32. package/src/import-pi.mjs +4 -2
  33. package/src/index.mjs +407 -62
  34. package/src/init.mjs +18 -0
  35. package/src/job-id.mjs +26 -3
  36. package/src/live-probes.mjs +24 -9
  37. package/src/model-catalog.mjs +297 -0
  38. package/src/model-endpoints.mjs +649 -0
  39. package/src/model-ref.mjs +151 -0
  40. package/src/models-json.mjs +262 -0
  41. package/src/money.mjs +144 -0
  42. package/src/octokit-log.mjs +65 -0
  43. package/src/outbox-plan.mjs +218 -0
  44. package/src/outbox.mjs +29 -9
  45. package/src/output-cap.mjs +157 -0
  46. package/src/pause-windows.mjs +81 -2
  47. package/src/pi-model-loader.mjs +77 -0
  48. package/src/podman-stack.mjs +16 -3
  49. package/src/portfolio-snapshot.mjs +304 -0
  50. package/src/prepare-local.mjs +247 -12
  51. package/src/prepare.mjs +35 -3
  52. package/src/priorities.mjs +569 -0
  53. package/src/processor.mjs +599 -170
  54. package/src/project-id.mjs +17 -0
  55. package/src/projects.mjs +238 -0
  56. package/src/provider-steering.mjs +179 -65
  57. package/src/queue.mjs +111 -6
  58. package/src/reserved-env.mjs +30 -0
  59. package/src/run-container.mjs +59 -5
  60. package/src/run-history.mjs +379 -24
  61. package/src/run-mirror.mjs +30 -0
  62. package/src/runtime-settings.mjs +104 -9
  63. package/src/schedules.mjs +33 -1
  64. package/src/scoped-limits.mjs +447 -27
  65. package/src/service.mjs +15 -4
  66. package/src/session-store.mjs +131 -6
  67. package/src/start.mjs +528 -40
  68. package/src/triggers-file.mjs +65 -4
  69. package/src/triggers.mjs +135 -7
  70. package/src/up.mjs +308 -34
  71. 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
 
@@ -228,7 +234,7 @@ const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
228
234
  * Every set is IMPORTED, never retyped, which is the rule `sandbox.test.mjs` keeps for the same reason: a
229
235
  * forge added to the table later must not need a second edit here to stay covered.
230
236
  */
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]);
237
+ 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
238
 
233
239
  function isNonEmptyString(value) {
234
240
  return typeof value === "string" && value.trim() !== "";
@@ -310,6 +316,13 @@ function normalizeTrigger(entry, index, path, state) {
310
316
  if (!isForgeKind(run.kind)) {
311
317
  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
318
  }
319
+ // Issue #505: `run.portfolio` is cron-only, refused here for every webhook type at once (and before the disarmed
320
+ // sentinel below, so a disarmed entry carrying it still refuses the file). A forge job is driven by text an issue or
321
+ // pull request author chose, and it has no `/outbox`, so a plan channel there would hand budget authority to whoever
322
+ // can open an issue. Any value is refused, `false` included: on a webhook kind the field has no meaning to state.
323
+ if (run.portfolio !== undefined) {
324
+ 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}`);
325
+ }
313
326
  let normalized;
314
327
  if (on.type === "label") normalized = normalizeLabel(on, run, index, path);
315
328
  else if (on.type === "comment") normalized = normalizeComment(on, run, index, path, state);
@@ -401,6 +414,12 @@ function normalizeCron(on, run, index, path, state) {
401
414
  // A cron job's session narrows like any other: the field selects what the container's agent can do,
402
415
  // which is orthogonal to what triggered it (issue #291).
403
416
  const excludeTools = validateExcludeTools(on, run, `cron trigger "${id}"`, path);
417
+ // Validated since #502; before it these three were copied untouched, so `run.provider: 7` loaded.
418
+ const ref = validateRunModel(on, run, `cron trigger "${id}"`, path);
419
+ // Issue #505: the portfolio flag. Strictly boolean, the house rule (`run.github`'s reason: a truthy string must not
420
+ // quietly arm budget authority). `true` beside `run.command` is refused: a plan is written by a flow's judgement,
421
+ // and a command has no committed skill to review. `false` is today's default and is carried, `run.resume`'s rule.
422
+ const portfolio = validatePortfolio(run, command, `cron trigger "${id}"`, path);
404
423
 
405
424
  // provider/model/maxTurns stay absent when omitted so the value resolves at job start against the
406
425
  // settings overlay/env, not a default frozen here (INT-CONFIG-OVERLAY-CONTRACT). github/packages/image stay
@@ -409,10 +428,27 @@ function normalizeCron(on, run, index, path, state) {
409
428
  // freeze today's default into every stored repeatable.
410
429
  return {
411
430
  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 }) },
431
+ 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
432
  };
414
433
  }
415
434
 
435
+ /**
436
+ * `run.portfolio` on a cron trigger (issue #505): the job may send a priorities plan back through
437
+ * `/outbox/priorities.json`. Returns the flag, or undefined when absent, so an unflagged entry normalizes
438
+ * byte-identically. Reviewed-file-only, like `run.secrets`: no tool and no panel key writes it.
439
+ */
440
+ function validatePortfolio(run, command, at, path) {
441
+ const portfolio = run.portfolio;
442
+ if (portfolio === undefined) return undefined;
443
+ if (typeof portfolio !== "boolean") {
444
+ throw configError(`${at}: run.portfolio must be true or false when present: ${path}`);
445
+ }
446
+ if (portfolio === true && command !== undefined) {
447
+ 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}`);
448
+ }
449
+ return portfolio;
450
+ }
451
+
416
452
  /**
417
453
  * Validate the per-trigger `run.packages` flag, shared by all four normalizers. It is an opt-OUT: the pi
418
454
  * packages an operator pinned into the global overlay load for every job, and `run.packages: false` is how a
@@ -1172,6 +1208,94 @@ function validateBackend(on, run, at, path, { localWorkspace }) {
1172
1208
  return backend;
1173
1209
  }
1174
1210
 
1211
+ /**
1212
+ * The `run` field a key spells or nearly spells (`run.provider`, `run.model`, `run.models`, `run.maxTurns`,
1213
+ * `run.maxCostUsd`), or null. The exact field names match too: the CALLER decides whether an exact name is legal
1214
+ * where it found it (the loader allows them under `run`, never under `on`; the console's trigger tools refuse
1215
+ * `run.models` and `run.maxCostUsd` in any spelling, issue #501 part 7). One sweep, so the two cannot disagree on
1216
+ * what a near miss is.
1217
+ */
1218
+ export function runModelFieldNearMiss(key) {
1219
+ // What the sweep compares against, case and separators removed. `providerid` and `modelid` are what an
1220
+ // operator copies from a provider's own API docs; `allowedmodels` is the issue's own name for the list.
1221
+ // Deliberately NOT a synonym hunt (`llm`, `engine`): that is the general unknown-key sweep this file's
1222
+ // forward-compatibility posture rejects.
1223
+ // Singular and plural of each, excludeTools' rule: `maxTurn` and `providers` are one grammatical step
1224
+ // from the field, and `modelName` is how several provider SDKs spell the id.
1225
+ const targets = RUN_MODEL_NEAR_MISS_TARGETS;
1226
+ const isSubsequence = (needle, hay) => {
1227
+ let i = 0;
1228
+ for (const ch of hay) if (i < needle.length && needle[i] === ch) i++;
1229
+ return i === needle.length;
1230
+ };
1231
+ const normalized = String(key).replace(/[^a-z0-9]/gi, "").toLowerCase();
1232
+ // The homoglyph branch is validateBackend's (its comment carries the rationale), floor 4 included:
1233
+ // `m<U+043E>del` normalizes to `mdel`, and a floor of 5 would let the shortest target's likeliest
1234
+ // homoglyph through. Only a non-ASCII key reaches this branch, so no ASCII field is at risk.
1235
+ // The suggestion names the target that actually MATCHED, so a homoglyph key is pointed at the field
1236
+ // its surviving letters spell rather than at whatever a prefix test would guess.
1237
+ const hit = /^[\x20-\x7E]*$/.test(key)
1238
+ ? (Object.hasOwn(targets, normalized) ? normalized : undefined)
1239
+ : normalized.length >= 4 ? Object.keys(targets).find((t) => isSubsequence(normalized, t)) : undefined;
1240
+ return hit === undefined ? null : targets[hit];
1241
+ }
1242
+
1243
+ const RUN_MODEL_NEAR_MISS_TARGETS = Object.freeze({
1244
+ provider: "run.provider", providers: "run.provider", providerid: "run.provider", providerids: "run.provider", providername: "run.provider",
1245
+ model: "run.model", modelid: "run.model", modelname: "run.model",
1246
+ models: "run.models", modelids: "run.models", modelnames: "run.models", allowedmodels: "run.models", allowedmodel: "run.models",
1247
+ maxturns: "run.maxTurns", maxturn: "run.maxTurns",
1248
+ maxcostusd: "run.maxCostUsd", maxcost: "run.maxCostUsd",
1249
+ });
1250
+
1251
+ /**
1252
+ * `run.provider`, `run.model` and `run.maxTurns` (issue #502): which model this trigger's job runs on, and
1253
+ * its turn limit. Every kind carries them; before #502 only cron did, and unchecked.
1254
+ *
1255
+ * The value grammar is `model-ref.mjs`'s, shared with the console. This wrapper adds the KEY sweep, in
1256
+ * `validateExcludeTools`' shape and for its reason: a misspelled `run.modelId` loads clean and the job runs
1257
+ * on the deployment default model while the file reads as though it chose one. That is a destructive
1258
+ * absence once a model is policy, and the price difference between two models is the whole point of
1259
+ * naming one.
1260
+ *
1261
+ * `run.maxCostUsd` (issue #501) is validated here too, since the sweep below already owns its near misses:
1262
+ * the per-job dollar cap, a plain decimal string or number (`parseUsdMicros`: above 0, at most 1,000,000,
1263
+ * at most 6 decimals), carried AS WRITTEN and emitted only when present (`null` is absent, the model keys'
1264
+ * rule). It can only NARROW: the worker runs the job under the smaller of it and the deployment's cap, and
1265
+ * refuses a file with one above the env cap when it loads it (`loadSchedules`, at boot and on a live reload;
1266
+ * this loader stays pure, so it cannot read env).
1267
+ *
1268
+ * `run.models` (the allowed-model list) is validated with them since #502's second part, and its misspellings
1269
+ * were refused here before it was.
1270
+ */
1271
+ function validateRunModel(on, run, at, path) {
1272
+ const exact = new Set(["provider", "model", "maxTurns", "models", "maxCostUsd"]);
1273
+ for (const [label, source, exactIsLegal] of [
1274
+ ["run", run, true],
1275
+ ["on", on, false],
1276
+ ]) {
1277
+ for (const key of Object.keys(source ?? {})) {
1278
+ if (exactIsLegal && exact.has(key)) continue;
1279
+ const meant = runModelFieldNearMiss(key);
1280
+ if (meant === null) continue;
1281
+ 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}`);
1282
+ }
1283
+ }
1284
+ return { ...validateModelRef(run, at, path), ...validateMaxCostUsd(run, at, path) };
1285
+ }
1286
+
1287
+ // `run.maxCostUsd` (issue #501): `{ maxCostUsd }` when present and valid, `{}` when absent or null. The
1288
+ // refusal names the trigger and the key and never the value, `parseUsdMicros`' rule.
1289
+ function validateMaxCostUsd(run, at, path) {
1290
+ if (run?.maxCostUsd === null || run?.maxCostUsd === undefined) return {};
1291
+ try {
1292
+ parseUsdMicros(run.maxCostUsd, "run.maxCostUsd");
1293
+ } catch (error) {
1294
+ throw configError(`${at}: ${error.message}: ${path}`);
1295
+ }
1296
+ return { maxCostUsd: run.maxCostUsd };
1297
+ }
1298
+
1175
1299
  /**
1176
1300
  * `run.excludeTools` -- pi tool names this trigger's session must NOT have (issue #291).
1177
1301
  *
@@ -1417,9 +1541,10 @@ function normalizeLabel(on, run, index, path) {
1417
1541
  const backend = validateBackend(on, run, at, path, { localWorkspace: false });
1418
1542
  const waitFor = validateWaitFor(on, run, at, path, { onType: on.type });
1419
1543
  const excludeTools = validateExcludeTools(on, run, at, path);
1544
+ const ref = validateRunModel(on, run, at, path);
1420
1545
  return {
1421
1546
  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 }) },
1547
+ 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
1548
  };
1424
1549
  }
1425
1550
 
@@ -1458,9 +1583,10 @@ function normalizeComment(on, run, index, path, state) {
1458
1583
  const backend = validateBackend(on, run, at, path, { localWorkspace: false });
1459
1584
  const waitFor = validateWaitFor(on, run, at, path, { onType: on.type });
1460
1585
  const excludeTools = validateExcludeTools(on, run, at, path);
1586
+ const ref = validateRunModel(on, run, at, path);
1461
1587
  return {
1462
1588
  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 }) },
1589
+ 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
1590
  };
1465
1591
  }
1466
1592
 
@@ -1526,6 +1652,7 @@ function normalizeIssue(on, run, index, path) {
1526
1652
  const backend = validateBackend(on, run, at, path, { localWorkspace: false });
1527
1653
  const waitFor = validateWaitFor(on, run, at, path, { onType: on.type });
1528
1654
  const excludeTools = validateExcludeTools(on, run, at, path);
1655
+ const ref = validateRunModel(on, run, at, path);
1529
1656
  return {
1530
1657
  on: {
1531
1658
  type: "issue",
@@ -1535,7 +1662,7 @@ function normalizeIssue(on, run, index, path) {
1535
1662
  ...(number !== undefined && { number }),
1536
1663
  ...(once !== undefined && { once }),
1537
1664
  },
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 }) },
1665
+ 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
1666
  };
1540
1667
  }
1541
1668
 
@@ -1612,6 +1739,7 @@ function normalizePullRequest(on, run, index, path) {
1612
1739
  const backend = validateBackend(on, run, at, path, { localWorkspace: false });
1613
1740
  const waitFor = validateWaitFor(on, run, at, path, { onType: on.type });
1614
1741
  const excludeTools = validateExcludeTools(on, run, at, path);
1742
+ const ref = validateRunModel(on, run, at, path);
1615
1743
  return {
1616
1744
  on: {
1617
1745
  type: "pull_request",
@@ -1626,7 +1754,7 @@ function normalizePullRequest(on, run, index, path) {
1626
1754
  ...(number !== undefined && { number }),
1627
1755
  ...(once !== undefined && { once }),
1628
1756
  },
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 }) },
1757
+ 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
1758
  };
1631
1759
  }
1632
1760