@staix/agent-hub 0.20.0 → 0.22.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 (183) hide show
  1. package/CHANGELOG.md +66 -3
  2. package/README.md +2 -2
  3. package/docs/agent-notes/README.md +11 -0
  4. package/docs/agent-notes/adapters.md +7 -3
  5. package/docs/agent-notes/benchmarks.md +27 -0
  6. package/docs/agent-notes/budget.md +2 -1
  7. package/docs/agent-notes/bus.md +3 -3
  8. package/docs/agent-notes/daemon.md +17 -8
  9. package/docs/agent-notes/local-worker.md +4 -0
  10. package/docs/agent-notes/models.md +2 -0
  11. package/docs/agent-notes/tasks.md +17 -9
  12. package/docs/agent-notes/tests.md +9 -2
  13. package/docs/bench.md +1 -1
  14. package/docs/data-policy.md +2 -0
  15. package/docs/dogfood.md +39 -4
  16. package/docs/events.md +2 -1
  17. package/docs/memory.md +9 -3
  18. package/docs/operations.md +610 -62
  19. package/docs/profiles.md +13 -1
  20. package/docs/quickstart.md +18 -9
  21. package/docs/research.md +7 -4
  22. package/docs/sdlc.md +54 -10
  23. package/docs/security.md +396 -34
  24. package/docs/smoke.md +133 -0
  25. package/docs/specs/2026-09-19-agent-hub-design.md +1101 -22
  26. package/docs/verification/2026-10-27-0.19.0.md +5 -5
  27. package/docs/verification/2026-10-28-claude-2.1.311-2.1.312-bash-permission.json +635 -0
  28. package/docs/verification/2026-10-28-claude-2.1.311-2.1.312-permission-transport.json +403 -0
  29. package/docs/verification/2026-10-28-claude-implementer-permission-triage.md +123 -0
  30. package/docs/verification/2026-10-28-jcode-0.99.1-usage.md +41 -0
  31. package/docs/verification/2026-10-28-realpath-name-audit.md +222 -0
  32. package/docs/verification/2026-10-28-routing-f0bb8ad0.md +1000 -0
  33. package/docs/verification/2026-10-29-0.21.0.md +61 -0
  34. package/docs/verification/2026-10-29-model-effort-capabilities.md +123 -0
  35. package/docs/verification/2026-10-29-modelrouting-b152ddb9.md +953 -0
  36. package/docs/verification/2026-10-30-0.22.0.md +49 -0
  37. package/docs/verification/2026-10-30-445-capability-probes.md +172 -0
  38. package/docs/verification/2026-10-30-bus-delivery-operational-settings.md +77 -0
  39. package/docs/verification/2026-10-30-jcode-ac4.md +24 -0
  40. package/docs/verification/2026-10-30-jcode-ac6.md +52 -0
  41. package/docs/verification/2026-10-30-recovery-wait-settings.md +141 -0
  42. package/docs/verification/claude-implementer-permission-triage.ts +160 -0
  43. package/docs/verification/claude-permission-transport-native.py +71 -14
  44. package/docs/verification/profiles/YYYY-MM-DD-research.md +1 -1
  45. package/docs/verified.json +15 -15
  46. package/package.json +2 -2
  47. package/plugins/agent-hub/.claude-plugin/plugin.json +1 -1
  48. package/plugins/agent-hub/server.js +1016 -280
  49. package/src/adapters/acp.ts +79 -3
  50. package/src/adapters/channel-peer.ts +4 -0
  51. package/src/adapters/claude-channel.ts +13 -6
  52. package/src/adapters/claude-implementer.ts +60 -7
  53. package/src/adapters/codex-appserver.ts +77 -4
  54. package/src/adapters/conformance/fakes/jcode-agent.ts +6 -2
  55. package/src/adapters/conformance/kit.ts +3 -9
  56. package/src/adapters/definitions/builtin-metadata.ts +3 -114
  57. package/src/adapters/definitions/native-config-defaults.ts +1 -1
  58. package/src/adapters/definitions/native-config-policy.ts +3 -2
  59. package/src/adapters/definitions/native-context.ts +12 -0
  60. package/src/adapters/external-metadata.ts +48 -0
  61. package/src/adapters/external-modules.ts +63 -3
  62. package/src/adapters/jcode.ts +6 -2
  63. package/src/adapters/local-worker.ts +40 -25
  64. package/src/adapters/native-proof-ports.ts +10 -0
  65. package/src/adapters/registry-types.ts +4 -0
  66. package/src/broker/protocol.ts +203 -0
  67. package/src/broker/server.ts +314 -0
  68. package/src/broker/store.ts +548 -0
  69. package/src/cli/bench.ts +3 -2
  70. package/src/cli/broker.ts +58 -0
  71. package/src/cli/conductor-recovery.ts +142 -0
  72. package/src/cli/configured-acp-entry.ts +26 -6
  73. package/src/cli/console-state.ts +59 -17
  74. package/src/cli/console.ts +17 -2
  75. package/src/cli/facts-hook.ts +3 -1
  76. package/src/cli/help.ts +29 -16
  77. package/src/cli/identity.ts +25 -10
  78. package/src/cli/init.ts +34 -7
  79. package/src/cli/intake-output.ts +33 -0
  80. package/src/cli/learn-output.ts +38 -0
  81. package/src/cli/main.ts +334 -73
  82. package/src/cli/output.ts +41 -1
  83. package/src/cli/recovery-package.ts +18 -1
  84. package/src/cli/recovery-waits.ts +26 -0
  85. package/src/cli/role-holders.ts +35 -0
  86. package/src/cli/room.ts +304 -0
  87. package/src/cli/say-input.ts +16 -0
  88. package/src/cli/schedule-output.ts +49 -0
  89. package/src/cli/sdlc-output.ts +44 -6
  90. package/src/cli/smoke-pty-bridge.ts +82 -0
  91. package/src/cli/smoke-pty.ts +153 -0
  92. package/src/cli/smoke-run.ts +139 -0
  93. package/src/cli/smoke-target.ts +130 -0
  94. package/src/cli/source-output.ts +29 -0
  95. package/src/cli/task-output.ts +3 -2
  96. package/src/cli/upgrade-runtime.ts +112 -28
  97. package/src/cli/upgrade.ts +68 -8
  98. package/src/hub/ask.ts +76 -12
  99. package/src/hub/board.ts +132 -22
  100. package/src/hub/bus.ts +416 -65
  101. package/src/hub/child-process.ts +2 -0
  102. package/src/hub/ci-watch.ts +42 -20
  103. package/src/hub/claude-failure-stage.ts +85 -0
  104. package/src/hub/claude-shell-wrapper.ts +11 -5
  105. package/src/hub/conductor.ts +17 -4
  106. package/src/hub/config-apply.ts +34 -6
  107. package/src/hub/config-reload.ts +7 -5
  108. package/src/hub/config-trust.ts +7 -1
  109. package/src/hub/context-window.ts +33 -3
  110. package/src/hub/control-client.ts +14 -2
  111. package/src/hub/corpus-config.ts +34 -0
  112. package/src/hub/corpus-evidence.ts +184 -0
  113. package/src/hub/corpus.ts +373 -0
  114. package/src/hub/cost-governance.ts +26 -0
  115. package/src/hub/daemon.ts +1209 -183
  116. package/src/hub/deliverable-files.ts +8 -2
  117. package/src/hub/delivery-journal.ts +200 -73
  118. package/src/hub/dogfood-profile.ts +32 -5
  119. package/src/hub/dogfood-read.ts +6 -2
  120. package/src/hub/envelope.ts +11 -0
  121. package/src/hub/eval-gate.ts +131 -0
  122. package/src/hub/events.ts +1 -1
  123. package/src/hub/external-instance.ts +69 -0
  124. package/src/hub/facts.ts +15 -5
  125. package/src/hub/folder-trigger.ts +223 -0
  126. package/src/hub/hub-tools.ts +2 -1
  127. package/src/hub/inference.ts +20 -5
  128. package/src/hub/instance-config.ts +69 -3
  129. package/src/hub/instance-execution.ts +6 -2
  130. package/src/hub/instances.ts +32 -4
  131. package/src/hub/intake.ts +125 -0
  132. package/src/hub/learning.ts +117 -0
  133. package/src/hub/lifecycle.ts +7 -3
  134. package/src/hub/limits.ts +12 -5
  135. package/src/hub/native-permission-request.ts +77 -0
  136. package/src/hub/operational-authority.ts +63 -5
  137. package/src/hub/operational-settings.ts +154 -0
  138. package/src/hub/operational-view.ts +1 -1
  139. package/src/hub/packs.ts +132 -0
  140. package/src/hub/path-policy.ts +7 -0
  141. package/src/hub/peer-doing.ts +130 -0
  142. package/src/hub/peers.ts +12 -0
  143. package/src/hub/queue-backpressure.ts +58 -0
  144. package/src/hub/read-tools.ts +2 -0
  145. package/src/hub/recovery-authority.ts +151 -0
  146. package/src/hub/recovery-policy.ts +220 -0
  147. package/src/hub/research.ts +24 -14
  148. package/src/hub/restart.ts +7 -0
  149. package/src/hub/routing.ts +54 -11
  150. package/src/hub/schedules.ts +266 -0
  151. package/src/hub/sdlc-merge.ts +38 -14
  152. package/src/hub/sdlc-metrics.ts +32 -10
  153. package/src/hub/sdlc-queue.ts +7 -1
  154. package/src/hub/sdlc-release.ts +1 -1
  155. package/src/hub/sdlc.ts +37 -7
  156. package/src/hub/security-baseline.ts +38 -0
  157. package/src/hub/settings.ts +27 -2
  158. package/src/hub/shipped-packs.ts +8 -0
  159. package/src/hub/snapshots.ts +72 -6
  160. package/src/hub/source-read.ts +160 -0
  161. package/src/hub/sources.ts +311 -0
  162. package/src/hub/tasks.ts +987 -110
  163. package/src/hub/workflow.ts +1 -1
  164. package/src/local/doc-read.ts +69 -0
  165. package/src/local/proxy.ts +4 -13
  166. package/src/local/tools.ts +37 -3
  167. package/src/local/web-fetch-config.ts +43 -0
  168. package/src/local/web-fetch.ts +183 -0
  169. package/src/memory/brief.ts +85 -13
  170. package/src/memory/capture.ts +1 -1
  171. package/src/rooms/application.ts +114 -0
  172. package/src/rooms/boundary.ts +103 -0
  173. package/src/rooms/commands.ts +294 -0
  174. package/src/rooms/crypto.ts +371 -0
  175. package/src/rooms/link.ts +584 -0
  176. package/src/rooms/policy.ts +82 -0
  177. package/src/rooms/remote-peer.ts +80 -0
  178. package/src/rooms/service.ts +426 -0
  179. package/src/rooms/status.ts +146 -0
  180. package/src/rooms/store.ts +1210 -0
  181. package/src/rooms/wire.ts +465 -0
  182. package/src/ui/index.html +91 -4
  183. package/templates/packs/example.pack.json +13 -0
@@ -58,8 +58,8 @@ export interface TaskRecord {
58
58
  /** #258 AC11: task briefs handed over for it, and how many of them carried a hub-memory fail note; absent without briefs. */
59
59
  briefs?: { delivered: number; withFail: number };
60
60
  }
61
- /** A person's later verdict on one task record: `createdAt` binds it to that task, not a later one with the same id. */
62
- export interface LabelRecord { schema: typeof RESEARCH_SCHEMA; kind: "label"; project: string; task: number; createdAt: string; label: Label; at: string }
61
+ /** A person's later verdict on one task record: `createdAt` binds it to that task, not a later one with the same id. `by`: the agent that labelled its own task (#933); absent for the person. */
62
+ export interface LabelRecord { schema: typeof RESEARCH_SCHEMA; kind: "label"; project: string; task: number; createdAt: string; label: Label; at: string; by?: string }
63
63
  export type ResearchRecord = TaskRecord | LabelRecord;
64
64
 
65
65
  /** The store name for a project: a hash of its id, never its path. */
@@ -293,15 +293,26 @@ export function formatResearch(r: ResearchReport): string[] {
293
293
  return [head, row("all approved tasks", r.overall), ...section("by class", r.byClass), ...section("by owner", r.byOwner), ...section("by model", r.byModel), ...section("by project", r.byProject), ...(Object.keys(r.byMemoryBrief).some((k) => k !== "no brief recorded") ? section("by memory brief", r.byMemoryBrief) : [])];
294
294
  }
295
295
 
296
- /** Each approved task once, its last record, and every task's latest label: what both reports count. */
297
- function latestRecords(records: ResearchRecord[], since: number): { all: TaskRecord[]; labels: Map<string, Label> } {
298
- const latest = new Map<string, TaskRecord>(), labels = new Map<string, Label>();
296
+ /**
297
+ * The label each record counts with: the person's latest label, whatever came after it; an agent's (#933, `by` set) only when the person
298
+ * never labelled that record, the latest of them then. An agent's verdict on its own task never replaces the person's.
299
+ */
300
+ function effectiveLabels(records: readonly ResearchRecord[]): Map<string, LabelRecord> {
301
+ const labels = new Map<string, LabelRecord>();
299
302
  for (const r of records) {
300
- const key = recordKey(r);
301
- if (r.kind === "label") { labels.set(key, r.label); continue; }
302
- if (Date.parse(r.approvedAt) >= since) latest.set(key, r);
303
+ if (r.kind !== "label") continue;
304
+ const key = recordKey(r), held = labels.get(key);
305
+ if (r.by === undefined || held === undefined || held.by !== undefined) labels.set(key, r);
303
306
  }
304
- return { all: [...latest.values()], labels };
307
+ return labels;
308
+ }
309
+ const labelValues = (labels: Map<string, LabelRecord>): Map<string, Label> => new Map([...labels].map(([key, r]) => [key, r.label]));
310
+
311
+ /** Each approved task once, its last record, and every task's effective label: what both reports count. */
312
+ function latestRecords(records: ResearchRecord[], since: number): { all: TaskRecord[]; labels: Map<string, Label> } {
313
+ const latest = new Map<string, TaskRecord>();
314
+ for (const r of records) if (r.kind === "task" && Date.parse(r.approvedAt) >= since) latest.set(recordKey(r), r);
315
+ return { all: [...latest.values()], labels: labelValues(effectiveLabels(records)) };
305
316
  }
306
317
 
307
318
  /** The peers that worked on a task: every peer with an attributed event, else (older records) owners, reviewer and token peers. */
@@ -364,15 +375,14 @@ export function formatAddons(rows: AddonComparison[]): string[] {
364
375
 
365
376
  export const CSV_COLUMNS = ["project", "task", "class", "pii", "outcome", "createdAt", "startedAt", "approvedAt", "wallMs", "activeMs", "owners",
366
377
  "reviewer", "reviewRounds", "changesRequested", "checkPassed", "checkFailed", "dones", "reassignments", "firstPass", "stuck", "overlaps", "conflicts",
367
- "testsPass", "testsFail", "tokens", "turns", "filesChanged", "models", "label", "writerVersion", "writerSource"] as const;
368
- /** Task records as CSV with a header row, one row per record, each with the task's latest label; JSONL has every field. */
378
+ "testsPass", "testsFail", "tokens", "turns", "filesChanged", "models", "label", "labelBy", "writerVersion", "writerSource"] as const;
379
+ /** Task records as CSV with a header row, one row per record, each with its effective label and who gave it (empty: the person); JSONL has every field. */
369
380
  export function toCsv(records: ResearchRecord[]): string {
370
- const labels = new Map<string, Label>();
371
- for (const r of records) if (r.kind === "label") labels.set(recordKey(r), r.label);
381
+ const labels = effectiveLabels(records);
372
382
  const cell = (v: unknown) => { const s = v === null || v === undefined ? "" : String(v); return /[",\n]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s; };
373
383
  const rows = records.filter((r): r is TaskRecord => r.kind === "task").map((r) => [r.project, r.task, r.class, r.pii, r.outcome, r.createdAt, r.startedAt,
374
384
  r.approvedAt, r.wallMs, r.activeMs, r.owners.join(" "), r.reviewer, r.reviewRounds, r.changesRequested, r.checkPassed, r.checkFailed, r.dones,
375
385
  r.reassignments, r.firstPass, r.stuck, r.overlaps, r.conflicts, r.tests.pass, r.tests.fail, r.tokens.total, r.turns, r.filesChanged, r.models.join(" "),
376
- labels.get(recordKey(r)) ?? "", r.writer.version, r.writer.source].map(cell).join(","));
386
+ labels.get(recordKey(r))?.label ?? "", labels.get(recordKey(r))?.by ?? "", r.writer.version, r.writer.source].map(cell).join(","));
377
387
  return [CSV_COLUMNS.join(","), ...rows].join("\n") + "\n";
378
388
  }
@@ -7,6 +7,7 @@ import { MAX_HOP } from "./envelope.ts";
7
7
  import { validateJournalSnapshot } from "./delivery-journal.ts";
8
8
  import { atomicPrivateJSON } from "./recovery-store.ts";
9
9
  import { RecoveryDeferredHubError, validateDeferredHub } from "./deferred-hub.ts";
10
+ import { validExternalInstanceBinding, type ExternalInstanceBinding } from "./external-instance.ts";
10
11
 
11
12
  export const RESTART_SCHEMA_VERSION = 1;
12
13
 
@@ -24,6 +25,8 @@ export interface RestartPeerSnapshot {
24
25
  * cannot be resumed and a fresh session is accepted instead. Absent in snapshots written
25
26
  * before this flag; those are re-derived from disk (#64). */
26
27
  sessionPersisted?: boolean;
28
+ /** Retained stable identity for an explicitly stopped module instance; never authorizes automatic resume. */
29
+ externalInstance?: ExternalInstanceBinding;
27
30
  }
28
31
 
29
32
  export interface RestartSnapshot {
@@ -41,6 +44,8 @@ export interface RestartSnapshot {
41
44
  manualPaused: PeerId[];
42
45
  boardDigest: string;
43
46
  budgetDigest: string;
47
+ /** Whole captured binding set; portable identities are covered alongside queue/board integrity. */
48
+ externalBindingsDigest?: string;
44
49
  };
45
50
  }
46
51
 
@@ -87,7 +92,9 @@ export function readRestartSnapshot(stateDir: string, expected: RestartReadExpec
87
92
  }, occupied);
88
93
  } catch (error) { if (error instanceof RecoveryDeferredHubError) throw error; return undefined; }
89
94
  if (!Array.isArray(s.manualPaused) || !s.manualPaused.every((id) => typeof id === "string") || !Array.isArray(s.peers) || !s.peers.every((peer) => peer && typeof peer === "object" && typeof peer.id === "string" && ["idle", "busy", "paused", "offline"].includes(peer.state as string) && Array.isArray(peer.queueIds) && peer.queueIds.every((id) => typeof id === "string"))) return undefined;
95
+ if (s.peers.some(peer => Object.hasOwn(peer, "externalInstance") && (peer.state !== "offline" || !validExternalInstanceBinding(peer.externalInstance, expected.projectRoot, peer.id)))) return undefined;
90
96
  if (s.integrity !== undefined && (!s.integrity || typeof s.integrity !== "object" || typeof s.integrity.boardDigest !== "string" || typeof s.integrity.budgetDigest !== "string" || !Array.isArray(s.integrity.manualPaused) || !s.integrity.manualPaused.every((id) => typeof id === "string") || !s.integrity.queues || typeof s.integrity.queues !== "object" || !Object.values(s.integrity.queues).every((ids) => Array.isArray(ids) && ids.every((id) => typeof id === "string")))) return undefined;
97
+ if (s.integrity && Object.hasOwn(s.integrity, "externalBindingsDigest") && (typeof s.integrity.externalBindingsDigest !== "string" || !/^[a-f0-9]{64}$/.test(s.integrity.externalBindingsDigest))) return undefined;
91
98
  if (bus.journal !== undefined) { try { validateJournalSnapshot(bus.journal); } catch { return undefined; } }
92
99
  return s as RestartSnapshot;
93
100
  }
@@ -1,3 +1,5 @@
1
+ import { contextHeadroom, type ContextAdmission } from "./context-window.ts";
2
+ import { queueBackpressure, type QueueBackpressureCode, type QueueBackpressurePolicy, type QueuePressure } from "./queue-backpressure.ts";
1
3
  import { selectReviewerPool, type ReviewerPoolSelection } from "./reviewer-pool.ts";
2
4
  import { ADAPTERS, definitionOf, kindOf, familyOf, type ProjectAdapterCatalog } from "../adapters/registry.ts";
3
5
  import { readFileSync, statSync } from "node:fs";
@@ -119,6 +121,17 @@ export interface InstanceRoutingOptions {
119
121
  instances?: Readonly<Record<string, { kind: string }>>;
120
122
  wip?: Record<string, number>;
121
123
  load?: Record<string, number>;
124
+ /** Separate from WIP and sibling load: bounded pending delivery metadata for new automatic offers. */
125
+ queues?: Readonly<Record<string, QueuePressure>>;
126
+ queueBackpressure?: QueueBackpressurePolicy;
127
+ /** Host-only explicit person/conductor nomination; a name or reservation alone grants no bypass. */
128
+ queueOverride?: "owner" | "reviewer" | "both";
129
+ /** Fresh native session evidence supplied by the host; never read telemetry inside assign(). */
130
+ contextAdmission?: ContextAdmission;
131
+ /** Host-authorized explicit nomination or self-claim, never inferred from namedOwner or a reservation. */
132
+ contextOverride?: boolean;
133
+ /** Host-proven person reservation: queue pressure holds this exact target instead of rerouting it. No bypass is granted. */
134
+ queueReservedHold?: PeerId;
122
135
  /** Positive host verification of the live extra runtime's noncanonical execution root. */
123
136
  ownerIsolation?: Readonly<Record<string, boolean>>;
124
137
  }
@@ -297,7 +310,7 @@ export function detectSignals(task: Pick<Task, "title" | "detail" | "refs">, rou
297
310
  /** A peer's entry in a peer-indexed map: own keys only, so a peer named `constructor` is never an inherited Object member. */
298
311
  export const peerEntry = <T>(map: Readonly<Record<string, T>> | undefined, peer: string): T | undefined => (map && Object.hasOwn(map, peer) ? map[peer] : undefined);
299
312
 
300
- export type RoutingRefusalCode = "instance" | "family" | "tests" | "spec" | "roster" | "pull-only" | "not-attached" | "excluded" | "owner-admission" | "offline" | "paused" | "failing" | "pii" | "policy";
313
+ export type RoutingRefusalCode = QueueBackpressureCode | "context-headroom" | "instance" | "family" | "tests" | "spec" | "roster" | "pull-only" | "not-attached" | "excluded" | "owner-admission" | "offline" | "paused" | "failing" | "pii" | "policy";
301
314
  export interface RoutingRefusal { peer: PeerId; role: "owner" | "reviewer"; code: RoutingRefusalCode; reason: string }
302
315
  export interface Assignment {
303
316
  reviewSelection?: ReviewerPoolSelection;
@@ -333,6 +346,9 @@ export function ownerRoleOfClass(cls: TaskClass): "planner" | "implementer" | "r
333
346
  /** Why a planner was chosen: the person named it, rotation reached it, the task's recorded planner is kept, or there is none (PII, no holder). */
334
347
  export type PlannerReason = "rotation" | "named" | "kept" | "none";
335
348
 
349
+ /** The remote namespace has no local task, reviewer or planner authority, even for malformed remote ids. */
350
+ export function isRemotePeerId(peer: unknown): boolean { return typeof peer === "string" && peer.includes("@"); }
351
+
336
352
  /**
337
353
  * I-2 (#525-3): the peers that implemented a task, read from its own history: any peer with an `accepted` or `done` entry. A reviewer is
338
354
  * never one of them, so a multi-role holder (implementer and reviewer) never reviews a change it made, even after the task changed hands.
@@ -367,10 +383,11 @@ export interface PlannerWalk { planner?: PeerId; reason: PlannerReason; walk: {
367
383
  * holder, availability never renumbers it (the #519 seed rule). When all are skipped, the seed holder is taken, "rotation (none available)".
368
384
  */
369
385
  export function plannerWalk(input: PlannerWalkInput): PlannerWalk {
370
- const holders = [...new Set(input.planners)].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
386
+ const holders = [...new Set(input.planners)].filter(peer => !isRemotePeerId(peer)).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
371
387
  const walk: { peer: PeerId; note: string }[] = [];
372
388
  const finish = (reason: PlannerReason, planner: PeerId | undefined, detail: string): PlannerWalk => ({ ...(planner ? { planner } : {}), reason, walk, line: planner ? `planner: ${planner} (${detail})` : `planner: none (${detail})` });
373
389
  if (input.pii) return finish("none", undefined, "pii: planning stays with the person");
390
+ if (isRemotePeerId(input.named)) return finish("none", undefined, "remote peers cannot hold local roles");
374
391
  if (!holders.length) return finish("none", undefined, "no planner holder");
375
392
  if (input.named !== undefined) {
376
393
  if (!holders.includes(input.named)) { walk.push({ peer: input.named, note: "skipped, not a planner holder" }); return finish("none", undefined, `named ${input.named} is not a planner holder`); }
@@ -463,6 +480,7 @@ export function assign(
463
480
  const former = implementers(task.history);
464
481
  const blocked = (peer: PeerId, role: "owner" | "reviewer"): string | undefined => {
465
482
  const deny = (code: RoutingRefusalCode, reason: string): string => { refusals.set(`${role}:${peer}`, { peer, role, code, reason }); return reason; };
483
+ if (isRemotePeerId(peer)) return deny("policy", "remote peers cannot hold local roles");
466
484
  const ceiling = (opts.adapterCatalog?.definitionOf ?? definitionOf)(peer, opts.instances)?.routing?.allowedRoles;
467
485
  const requiredRole = role === "reviewer" ? "reviewer" : ownerRoleOfClass(task.class);
468
486
  if (ceiling && !ceiling.includes(requiredRole)) return deny("policy", "adapter role ceiling excludes this assignment");
@@ -498,10 +516,20 @@ export function assign(
498
516
  if (routing.signals.long_context_tokens > limit) return deny("policy", `${nativeRouting.backendLabel} ${backend} capability limit ${limit} tokens`);
499
517
  }
500
518
  if (nativeRouting?.longContext === "skip-owner" && role === "owner" && task.signals.includes("long_context") && routing.constraints.long_context === "skip_local") return deny("policy", "long_context: skip local");
501
- // I-2 (#525-3): a peer that implemented this task never reviews it, whoever holds it now, a named reviewer included. It is the LAST check,
502
- // so it only ever removes a candidate that was otherwise eligible: a former implementer that is offline, paused, failing or off roster is
503
- // refused for that real reason and leaves the outcome exactly as it was before I-2 (a task with no other reviewer still approves directly).
519
+ // I-2 (#525-3) is the last capability/independence gate, before queue-capacity admission. A former implementer never reviews its task,
520
+ // including a named reviewer. Earlier offline, paused, failing and roster refusals retain their existing reason and outcome.
504
521
  if (role === "reviewer" && former.has(peer)) return deny("policy", "implemented this task");
522
+ if (role === "owner" && requiredRole !== "reviewer" && opts.contextAdmission) {
523
+ const decision = contextHeadroom(opts.contextAdmission.policy, adapter?.budget?.context ? peerEntry(opts.contextAdmission.peers, peer) : undefined, opts.now ?? NaN);
524
+ if (decision) {
525
+ trace.push(` owner context ${peer}: ${decision.note}${opts.contextOverride ? "; explicit override" : ""}`);
526
+ if (decision.blocked && !opts.contextOverride) return deny("context-headroom", decision.note);
527
+ }
528
+ }
529
+ if (opts.queueOverride !== role && opts.queueOverride !== "both") {
530
+ const pressure = queueBackpressure(peer, opts.queueBackpressure, peerEntry(opts.queues, peer), opts.now ?? NaN);
531
+ if (pressure) return deny(pressure.code, pressure.reason);
532
+ }
505
533
  return undefined;
506
534
  };
507
535
  /** Quota that resets soonest gets used first: headroom per hour left in the window, at least 15 min (a task assigned that close to a reset mostly runs after it). */
@@ -556,12 +584,15 @@ export function assign(
556
584
  return ranked.find((p) => states[p] === "idle") ?? ranked[0];
557
585
  };
558
586
 
559
- // The PII constraint, capability limits, exclusions and peer states still apply: when they pass it over, routing proceeds.
587
+ // Safety/state refusals still permit the existing reservation fallback. Queue pressure holds a host-proven person reservation instead.
560
588
  const unreserved = reserved ? blocked(reserved, "owner") : undefined;
561
- if (reserved) trace.push(`reserved owner ${reserved}: ${unreserved ? `not honored, ${unreserved}; routing proceeds` : "honored"}`);
589
+ const reservedRefusal = reserved ? refusals.get(`owner:${reserved}`) : undefined;
590
+ const queueReservedHeld = reserved === opts.queueReservedHold && reservedRefusal !== undefined
591
+ && ["queue-count", "queue-age", "queue-age-unknown"].includes(reservedRefusal.code);
592
+ if (reserved) trace.push(`reserved owner ${reserved}: ${queueReservedHeld ? `held, ${unreserved}; reservation retained` : unreserved ? `not honored, ${unreserved}; routing proceeds` : "honored"}`);
562
593
  // Never the task's current owner by default: a decline or an escalation has to reach the next peer in the list.
563
594
  const automaticOwner = !(reserved && !unreserved) && opts.candidates === undefined && !(instanceMode && opts.namedOwner !== undefined);
564
- const wanted = reserved && !unreserved ? [reserved] : opts.candidates ?? (instanceMode && opts.namedOwner !== undefined ? [opts.namedOwner] : expandClassPeers(policy?.peers ?? [], states, opts));
595
+ const wanted = queueReservedHeld ? [] : reserved && !unreserved ? [reserved] : opts.candidates ?? (instanceMode && opts.namedOwner !== undefined ? [opts.namedOwner] : expandClassPeers(policy?.peers ?? [], states, opts));
565
596
  const owner = pick(wanted, "owner", undefined, automaticOwner);
566
597
  trace.push(owner ? `owner: ${owner}` : "owner: none available, task stays proposed (ahub task assign <id> <peer>)");
567
598
  const ownerHold = owner ? peerEntry(opts.held, owner) : undefined;
@@ -680,6 +711,12 @@ export function assign(
680
711
  reviewBlocked = true;
681
712
  trace.push(" every eligible reviewer implemented this task: the person reviews");
682
713
  }
714
+ // Queue pressure holds a new review offer; it must never turn review exhaustion into direct approval.
715
+ if (!reviewer && [...refusals.values()].some(row => row.role === "reviewer" && (row.code === "queue-count" || row.code === "queue-age" || row.code === "queue-age-unknown"))) {
716
+ reviewer = "user";
717
+ reviewBlocked = true;
718
+ trace.push(" reviewer queue backpressure: automatic review held; the person may override");
719
+ }
683
720
  trace.push(reviewer ? `reviewer: ${reviewer}` : "reviewer: none, done will approve directly");
684
721
  const reviewerHold = reviewer ? peerEntry(opts.held, reviewer) : undefined;
685
722
  if (reviewerHold) trace.push(` hold: ${reviewer}'s queue is held: ${reviewerHold} (it receives the review once the hold is resolved)`);
@@ -719,6 +756,10 @@ export interface SplitInput {
719
756
  /** Other open work each peer has: a busy owner does not start from orientation plus two whole units. */
720
757
  backlog: Record<PeerId, number>;
721
758
  available: Record<PeerId, boolean>;
759
+ /** #489 H2: `tasks.split_min_observations`; absent is `SPLIT_MIN`. */
760
+ minObservations?: number;
761
+ /** #489 H2: `tasks.split_max_failure_share`; absent is `SPLIT_FAILURE_SHARE`. */
762
+ maxFailureShare?: number;
722
763
  }
723
764
 
724
765
  export interface SplitPrediction {
@@ -733,7 +774,7 @@ export interface SplitPrediction {
733
774
 
734
775
  /** Observations a peer needs, and the share of failures a history may hold, before a prediction is made. */
735
776
  export const SPLIT_MIN = 5;
736
- const SPLIT_FAILURE_SHARE = 0.3;
777
+ export const SPLIT_FAILURE_SHARE = 0.3;
737
778
 
738
779
  /**
739
780
  * Shadow split prediction (issue #109): never changes assignment. For two similar units of overlapping work, a split
@@ -744,6 +785,8 @@ const SPLIT_FAILURE_SHARE = 0.3;
744
785
  * comparable. A difference under a tenth of the single time is inconclusive.
745
786
  */
746
787
  export function predictSplit(input: SplitInput): SplitPrediction {
788
+ // #489 H2: `tasks.split_min_observations` and `tasks.split_max_failure_share`; absent, the constants above.
789
+ const minObservations = input.minObservations ?? SPLIT_MIN, maxFailureShare = input.maxFailureShare ?? SPLIT_FAILURE_SHARE;
747
790
  const trace: string[] = ["shadow split prediction (it never changes assignment):"];
748
791
  const unknown = (why: string): SplitPrediction => ({ verdict: "unknown", trace: [...trace, ` unknown: ${why}`] });
749
792
  const [a, b] = input.peers;
@@ -767,8 +810,8 @@ export function predictSplit(input: SplitInput): SplitPrediction {
767
810
  const all = input.observations[p] ?? [];
768
811
  const failed = all.filter((o) => o.outcome !== "approved").length;
769
812
  const ok = all.filter((o): o is Required<SplitObservation> => o.outcome === "approved" && o.orient !== undefined && o.work !== undefined);
770
- if (ok.length < SPLIT_MIN) return unknown(`${p} has ${ok.length} measured task(s) with this profile; ${SPLIT_MIN} are needed`);
771
- if (failed / all.length > SPLIT_FAILURE_SHARE) return unknown(`${failed} of ${all.length} of ${p}'s recorded tasks failed: its successes alone would understate its time`);
813
+ if (ok.length < minObservations) return unknown(`${p} has ${ok.length} measured task(s) with this profile; ${minObservations} are needed`);
814
+ if (failed / all.length > maxFailureShare) return unknown(`${failed} of ${all.length} of ${p}'s recorded tasks failed: its successes alone would understate its time`);
772
815
  const works = ok.map((o) => o.work).sort((x, y) => x - y);
773
816
  const u = median(works);
774
817
  const iqr = works[Math.floor((works.length * 3) / 4)]! - works[Math.floor(works.length / 4)]!;
@@ -0,0 +1,266 @@
1
+ import { Database } from "bun:sqlite";
2
+ import { HUB_DB_BUSY_TIMEOUT_MS } from "./db-timeout.ts";
3
+ import { corpusPathProblem } from "./corpus-config.ts";
4
+
5
+ // agent-hub #512 (hub task #330): machine-local schedules. Spec: "Schedules and folder triggers (#512)".
6
+
7
+ export const MIN_EVERY_MS = 15 * 60_000;
8
+ export const MAX_EVERY_MS = 365 * 86_400_000;
9
+ export const MAX_SCHEDULES = 64;
10
+ export const MAX_SCHEDULE_SOURCES = 16;
11
+ /** Under the board's 8000-character detail cap with the fixed lines and 16 source lines around it, so the marker line is never cut off. */
12
+ export const MAX_SCHEDULE_DETAIL = 7000;
13
+ /** The first line of a scheduled task's detail: how the board finds the task a schedule made, whatever hub.db remembers. */
14
+ export const SCHEDULE_MARKER = "Schedule key: ";
15
+
16
+ export interface ScheduleEntry {
17
+ id: string;
18
+ every: string;
19
+ class: string;
20
+ title: string;
21
+ detail?: string;
22
+ sources?: string[];
23
+ owner?: string;
24
+ queue: "backlog" | "ready";
25
+ }
26
+
27
+ /** The source id form of `src/hub/sources.ts` (`validSourceId`), repeated here because that module reaches the file system and the converters and this one is bundled into the Claude channel server through the config schema; `test/schedules.test.ts` pins that the two agree. */
28
+ export const SOURCE_ID = /^src:[0-9a-f]{16}$/;
29
+ const ID = /^[a-z0-9][a-z0-9_-]{0,47}$/, PEER = /^[a-z][a-z0-9-]{0,31}$/, CLASS = /^[a-z][a-z0-9_]{0,47}$/, KEYS = new Set(["id", "every", "cron", "class", "title", "detail", "sources", "owner", "queue"]);
30
+ const control = /[\x00-\x1f\x7f]/;
31
+
32
+ /** `<n>m|h|d` to milliseconds; a thrown reason otherwise. The 15-minute floor keeps a typo from flooding the board. */
33
+ export function parseEvery(value: unknown): number {
34
+ const match = typeof value === "string" ? /^([1-9][0-9]{0,5})([mhd])$/.exec(value) : null;
35
+ if (!match) throw new Error("every must be <n>m, <n>h or <n>d, such as 90m, 6h or 1d");
36
+ const ms = Number(match[1]) * { m: 60_000, h: 3_600_000, d: 86_400_000 }[match[2] as "m" | "h" | "d"];
37
+ if (ms < MIN_EVERY_MS) throw new Error("every must be at least 15m");
38
+ if (ms > MAX_EVERY_MS) throw new Error("every must be at most 365d");
39
+ return ms;
40
+ }
41
+
42
+ /** The checked `schedules` list. Every refusal names the entry; nothing here reads the board, a clock or a file. */
43
+ export function schedulesConfig(input: unknown): ScheduleEntry[] {
44
+ if (input === undefined) return [];
45
+ if (!Array.isArray(input)) throw new Error("schedules must be a list");
46
+ if (input.length > MAX_SCHEDULES) throw new Error(`schedules holds at most ${MAX_SCHEDULES} entries`);
47
+ const seen = new Set<string>(), out: ScheduleEntry[] = [];
48
+ input.forEach((raw, index) => {
49
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) throw new Error(`schedules[${index}] must be an object`);
50
+ const row = raw as Record<string, unknown>;
51
+ const name = typeof row.id === "string" && ID.test(row.id) ? `schedules.${row.id}` : `schedules[${index}]`;
52
+ const bad = (reason: string): never => { throw new Error(`${name}: ${reason}`); };
53
+ for (const key of Object.keys(row)) if (!KEYS.has(key)) bad(`unknown key ${key}`);
54
+ if (typeof row.id !== "string" || !ID.test(row.id)) bad("id is 1 to 48 lower-case letters, digits, - or _");
55
+ if (seen.has(row.id as string)) bad("duplicate id");
56
+ seen.add(row.id as string);
57
+ if (row.cron !== undefined) bad("cron not supported yet; use every");
58
+ try { parseEvery(row.every); } catch (error) { bad((error as Error).message); }
59
+ if (typeof row.class !== "string" || !CLASS.test(row.class)) bad("class is required");
60
+ if (typeof row.title !== "string" || !row.title.trim() || row.title.length > 300 || control.test(row.title)) bad("title is 1 to 300 printable characters on one line");
61
+ if (row.detail !== undefined && (typeof row.detail !== "string" || row.detail.length > MAX_SCHEDULE_DETAIL || /[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(row.detail))) bad(`detail is text of at most ${MAX_SCHEDULE_DETAIL} characters`);
62
+ if (row.owner !== undefined && (typeof row.owner !== "string" || !PEER.test(row.owner))) bad("owner is a peer id");
63
+ if (row.queue !== undefined && row.queue !== "backlog" && row.queue !== "ready") bad('queue is "backlog" or "ready"');
64
+ if (row.sources !== undefined) {
65
+ if (!Array.isArray(row.sources) || row.sources.length > MAX_SCHEDULE_SOURCES || !row.sources.every(id => typeof id === "string" && SOURCE_ID.test(id))) bad(`sources is a list of at most ${MAX_SCHEDULE_SOURCES} source ids (src:<16 hex>)`);
66
+ }
67
+ out.push({
68
+ id: row.id as string, every: row.every as string, class: row.class as string, title: (row.title as string).replace(/\s+/g, " ").trim(),
69
+ ...(row.detail !== undefined ? { detail: row.detail as string } : {}), ...(row.sources !== undefined ? { sources: [...(row.sources as string[])] } : {}),
70
+ ...(row.owner !== undefined ? { owner: row.owner as string } : {}), queue: (row.queue as "backlog" | "ready" | undefined) ?? "backlog",
71
+ });
72
+ });
73
+ return out;
74
+ }
75
+
76
+ // agent-hub #512c (hub task #336): machine-local folder triggers. The pure config only; the scan, the store and the registration are in `folder-trigger.ts`, which reaches the disk.
77
+ export const MAX_FOLDER_TRIGGERS = 16;
78
+ export const MAX_FOLDER_GLOB = 100;
79
+ /** The first line of a folder-triggered task's detail: which trigger made it. A task per file, so unlike a schedule's marker it blocks nothing. */
80
+ export const FOLDER_MARKER = "Folder trigger: ";
81
+
82
+ export interface FolderEntry {
83
+ id: string;
84
+ folder: string;
85
+ glob?: string;
86
+ class: string;
87
+ title: string;
88
+ detail?: string;
89
+ owner?: string;
90
+ queue: "backlog" | "ready";
91
+ }
92
+ const FOLDER_KEYS = new Set(["id", "folder", "glob", "class", "title", "detail", "owner", "queue"]);
93
+
94
+ /** A file-name pattern: letters, digits, `.`, `_`, `-`, space, `*` and `?`. Never a path, so it cannot reach outside the folder. */
95
+ export const folderGlobProblem = (value: unknown): string | undefined =>
96
+ typeof value !== "string" || !value || value.length > MAX_FOLDER_GLOB || !/^[A-Za-z0-9._ *?-]+$/.test(value) || value === "." || value === ".."
97
+ ? `glob is a file-name pattern of at most ${MAX_FOLDER_GLOB} characters (letters, digits, . _ - space, * and ?), never a path` : undefined;
98
+
99
+ /** Whether a file name matches a trigger's glob. Dot files never match: a hidden file is not something a person dropped in. */
100
+ export function folderGlobMatches(glob: string | undefined, name: string): boolean {
101
+ if (name.startsWith(".")) return false;
102
+ if (glob === undefined) return true;
103
+ return new RegExp(`^${glob.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".")}$`).test(name);
104
+ }
105
+
106
+ /** The checked `folder_triggers` list. Every refusal names the entry; nothing here reads the disk (the folder check does, at each poll). */
107
+ export function foldersConfig(input: unknown): FolderEntry[] {
108
+ if (input === undefined) return [];
109
+ if (!Array.isArray(input)) throw new Error("folder_triggers must be a list");
110
+ if (input.length > MAX_FOLDER_TRIGGERS) throw new Error(`folder_triggers holds at most ${MAX_FOLDER_TRIGGERS} entries`);
111
+ const seen = new Set<string>(), out: FolderEntry[] = [];
112
+ input.forEach((raw, index) => {
113
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) throw new Error(`folder_triggers[${index}] must be an object`);
114
+ const row = raw as Record<string, unknown>;
115
+ const name = typeof row.id === "string" && ID.test(row.id) ? `folder_triggers.${row.id}` : `folder_triggers[${index}]`;
116
+ const bad = (reason: string): never => { throw new Error(`${name}: ${reason}`); };
117
+ for (const key of Object.keys(row)) if (!FOLDER_KEYS.has(key)) bad(`unknown key ${key}`);
118
+ if (typeof row.id !== "string" || !ID.test(row.id)) bad("id is 1 to 48 lower-case letters, digits, - or _");
119
+ if (seen.has(row.id as string)) bad("duplicate id");
120
+ seen.add(row.id as string);
121
+ const folderProblem = corpusPathProblem(row.folder);
122
+ if (folderProblem) bad(folderProblem.replace("a corpus path", "folder"));
123
+ if (row.glob !== undefined) { const problem = folderGlobProblem(row.glob); if (problem) bad(problem); }
124
+ if (typeof row.class !== "string" || !CLASS.test(row.class)) bad("class is required");
125
+ if (typeof row.title !== "string" || !row.title.trim() || row.title.length > 300 || control.test(row.title)) bad("title is 1 to 300 printable characters on one line");
126
+ if (row.detail !== undefined && (typeof row.detail !== "string" || row.detail.length > MAX_SCHEDULE_DETAIL || /[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(row.detail))) bad(`detail is text of at most ${MAX_SCHEDULE_DETAIL} characters`);
127
+ if (row.owner !== undefined && (typeof row.owner !== "string" || !PEER.test(row.owner))) bad("owner is a peer id");
128
+ if (row.queue !== undefined && row.queue !== "backlog" && row.queue !== "ready") bad('queue is "backlog" or "ready"');
129
+ out.push({
130
+ id: row.id as string, folder: row.folder as string, ...(row.glob !== undefined ? { glob: row.glob as string } : {}), class: row.class as string,
131
+ title: (row.title as string).replace(/\s+/g, " ").trim(), ...(row.detail !== undefined ? { detail: row.detail as string } : {}),
132
+ ...(row.owner !== undefined ? { owner: row.owner as string } : {}), queue: (row.queue as "backlog" | "ready" | undefined) ?? "backlog",
133
+ });
134
+ });
135
+ return out;
136
+ }
137
+
138
+ /** What one poll of a trigger did: `baseline` (first poll, recorded only), `ran`, `none`, `held`, `refused` (the folder may not be read, with the reason) or `failed`. */
139
+ export interface FolderRunResult { id: string; outcome: "baseline" | "ran" | "none" | "held" | "refused" | "failed"; tasks: number[]; files: number; reason?: string; more?: true }
140
+ /**
141
+ * One file a poll found: its real path, name, size, mtime and SHA-256, and never its bytes (a scan of a big inbox would hold them all). `seen` marks a file the trigger
142
+ * already recorded at this path, size and mtime: its hash is the recorded one and the file was not read again.
143
+ */
144
+ export interface ScannedFile { real: string; name: string; size: number; mtime: number; sha256: string; seen?: true }
145
+ /** What a read of a trigger's folder gave. `refused` is the reason the folder may not be read; `truncated` says the hash budget ran out and some files wait for the next poll. */
146
+ export interface FolderScan { folder?: string; files: ScannedFile[]; skipped: { name: string; reason: string }[]; refused?: string; truncated: boolean }
147
+ /** The part of the folder state `Tasks` needs. The scan, the disk and the table are in `folder-trigger.ts`, which the Claude channel bundle must not reach. */
148
+ export interface FolderStorePort {
149
+ baselined(trigger: string): boolean;
150
+ begin(trigger: string, now: number): void;
151
+ markBaselined(trigger: string, now: number): void;
152
+ polled(trigger: string, now: number, error?: string): void;
153
+ isNew(trigger: string, sha256: string): boolean;
154
+ record(trigger: string, file: Pick<ScannedFile, "real" | "size" | "mtime" | "sha256">, task: number | null, now: number): void;
155
+ observe(trigger: string, file: Pick<ScannedFile, "real" | "size" | "mtime" | "sha256">, now: number): void;
156
+ prune(keep: readonly string[]): void;
157
+ }
158
+ /** New files one poll registers per trigger: a drop of a whole archive becomes a handful of tasks per tick, never a burst. */
159
+ export const MAX_NEW_PER_POLL = 20;
160
+
161
+ /** The detail of a folder-triggered task: the marker first, then the person's text, then the one source it names. Never the file's name or path: both can be private. */
162
+ export function folderDetail(entry: FolderEntry, source: string): string {
163
+ return [`${FOLDER_MARKER}${entry.id}`, `Made by the machine-local folder trigger "${entry.id}" for a new file, registered as the source below.`, ...(entry.detail ? ["", entry.detail] : []), "", `Source: ${source}`].join("\n");
164
+ }
165
+
166
+ export interface ScheduleRow {
167
+ id: string;
168
+ /** The start of the current period: the first time the schedule was seen, then the last run. The next run is this plus `every`. */
169
+ anchor: number;
170
+ last_run: number | null;
171
+ last_task: number | null;
172
+ /** Ticks that were due and skipped (held, or the previous task still open) since the last run: coalesced into the one run that follows. */
173
+ coalesced: number;
174
+ last_error: string | null;
175
+ /** #335: 1 while a person has paused it. */
176
+ paused?: number;
177
+ }
178
+
179
+ /** `schedule_runs` in hub.db: when each schedule last ran and what it made. The board stays the record of the tasks themselves. */
180
+ export class ScheduleStore {
181
+ private readonly db: Database;
182
+ constructor(dbPath: string) {
183
+ this.db = new Database(dbPath, { create: true });
184
+ this.db.run(`PRAGMA busy_timeout = ${HUB_DB_BUSY_TIMEOUT_MS}`);
185
+ this.db.run("CREATE TABLE IF NOT EXISTS schedule_runs (id TEXT PRIMARY KEY, anchor INTEGER NOT NULL, last_run INTEGER, last_task INTEGER, coalesced INTEGER NOT NULL DEFAULT 0, last_error TEXT)");
186
+ // #335: a person's pause. A column on the existing table, so an older database gains it and every older row reads as not paused.
187
+ if (!(this.db.query("PRAGMA table_info(schedule_runs)").all() as { name: string }[]).some(column => column.name === "paused")) this.db.run("ALTER TABLE schedule_runs ADD COLUMN paused INTEGER NOT NULL DEFAULT 0");
188
+ }
189
+ get(id: string): ScheduleRow | undefined { return (this.db.query("SELECT * FROM schedule_runs WHERE id = ?").get(id) as ScheduleRow | null) ?? undefined; }
190
+ rows(): ScheduleRow[] { return this.db.query("SELECT * FROM schedule_runs ORDER BY id").all() as ScheduleRow[]; }
191
+ /** A schedule seen for the first time starts its period now: it never runs at once, and a restart never replays a missed one. */
192
+ ensure(id: string, now: number): ScheduleRow {
193
+ this.db.query("INSERT OR IGNORE INTO schedule_runs(id, anchor) VALUES(?, ?)").run(id, now);
194
+ return this.get(id)!;
195
+ }
196
+ /**
197
+ * A schedule's pause generation, in memory: `setPaused` bumps it. A run captures it when it is admitted and its write fence asks again, so a pause or a resume that
198
+ * lands while the run awaits its PII screen cancels that run instead of being overtaken by it. A restart ends every run in flight, so memory is enough.
199
+ */
200
+ private readonly epochs = new Map<string, number>();
201
+ epoch(id: string): number { return this.epochs.get(id) ?? 0; }
202
+ /**
203
+ * #335: pause or resume one schedule. A pause stops the timer from proposing it and counts no skipped tick. Resuming starts a fresh period at `now`, so a long
204
+ * pause is never replayed as a run the moment it ends, as a restart is never replayed either.
205
+ */
206
+ setPaused(id: string, paused: boolean, now: number): void {
207
+ this.epochs.set(id, this.epoch(id) + 1);
208
+ this.db.query("INSERT OR IGNORE INTO schedule_runs(id, anchor) VALUES(?, ?)").run(id, now);
209
+ if (paused) this.db.query("UPDATE schedule_runs SET paused = 1 WHERE id = ?").run(id);
210
+ else this.db.query("UPDATE schedule_runs SET paused = 0, anchor = ?, coalesced = 0 WHERE id = ?").run(now, id);
211
+ }
212
+ skipped(id: string): void { this.db.query("UPDATE schedule_runs SET coalesced = coalesced + 1 WHERE id = ?").run(id); }
213
+ ran(id: string, now: number, task: number): void {
214
+ // The anchor never moves back: a resume that landed after the task was written already started a later period, and an older pass timestamp must not shorten it.
215
+ this.db.query("UPDATE schedule_runs SET anchor = MAX(anchor, ?), last_run = ?, last_task = ?, coalesced = 0, last_error = NULL WHERE id = ?").run(now, now, task, id);
216
+ }
217
+ failed(id: string, reason: string): void { this.db.query("UPDATE schedule_runs SET last_error = ? WHERE id = ?").run(reason.slice(0, 200), id); }
218
+ /** A schedule removed from the config keeps no row. */
219
+ prune(keep: readonly string[]): void {
220
+ for (const row of this.rows()) if (!keep.includes(row.id)) this.db.query("DELETE FROM schedule_runs WHERE id = ?").run(row.id);
221
+ }
222
+ close(): void { try { this.db.close(); } catch { /* already closed */ } }
223
+ }
224
+
225
+ /** What one run of a schedule did. `changed`: the person paused or resumed it after the run was admitted, so it wrote nothing (#335). */
226
+ export type ScheduleOutcome = "ran" | "held" | "waiting" | "failed" | "changed";
227
+ export interface ScheduleRunResult { id: string; outcome: ScheduleOutcome; task?: number }
228
+
229
+ export type ScheduleState = "scheduled" | "due" | "waiting" | "held" | "paused";
230
+ export interface ScheduleView {
231
+ id: string;
232
+ every: string;
233
+ class: string;
234
+ queue: "backlog" | "ready";
235
+ next: number;
236
+ last?: number;
237
+ task?: number;
238
+ state: ScheduleState;
239
+ coalesced?: number;
240
+ error?: string;
241
+ }
242
+
243
+ /**
244
+ * What status shows for each schedule. `open` maps a schedule id to the open task it made. A due schedule that is held or whose task is open says so;
245
+ * one that is simply due (before its next tick) reads `due`. Titles and details are never shown: they are task text.
246
+ */
247
+ export function scheduleViews(entries: readonly ScheduleEntry[], rows: readonly ScheduleRow[], now: number, open: ReadonlyMap<string, number>, held: boolean): ScheduleView[] {
248
+ const byId = new Map(rows.map(row => [row.id, row]));
249
+ return entries.map(entry => {
250
+ const row = byId.get(entry.id), anchor = row?.anchor ?? now, next = anchor + parseEvery(entry.every), openTask = open.get(entry.id);
251
+ const state: ScheduleState = row?.paused ? "paused" : now < next ? "scheduled" : held ? "held" : openTask !== undefined ? "waiting" : "due";
252
+ return {
253
+ id: entry.id, every: entry.every, class: entry.class, queue: entry.queue, next, state,
254
+ ...(row?.last_run != null ? { last: row.last_run } : {}), ...(openTask !== undefined ? { task: openTask } : row?.last_task != null ? { task: row.last_task } : {}),
255
+ ...(row?.coalesced ? { coalesced: row.coalesced } : {}), ...(row?.last_error ? { error: row.last_error } : {}),
256
+ };
257
+ });
258
+ }
259
+
260
+ /** The detail of a scheduled task: the marker line first, then the person's text and the sources it names, all fixed words around config text. */
261
+ export function scheduledDetail(entry: ScheduleEntry): string {
262
+ return [`${SCHEDULE_MARKER}${entry.id}`, `Scheduled every ${entry.every} by the machine-local schedule "${entry.id}".`, ...(entry.detail ? ["", entry.detail] : []),
263
+ ...(entry.sources?.length ? ["", ...entry.sources.map(id => `Source: ${id}`)] : [])].join("\n");
264
+ }
265
+
266
+ export const scheduleKeyOf = (detail: string | undefined): string | undefined => detail?.startsWith(SCHEDULE_MARKER) ? detail.slice(SCHEDULE_MARKER.length).split("\n")[0] : undefined;