agent-coord-mcp 0.26.36 → 0.26.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/server.ts CHANGED
@@ -15,10 +15,12 @@ import { rotateSchema, rotateTool, rotateReconcileSchema, rotateReconcileTool }
15
15
  import { subscribeSchema, subscribeTool, unsubscribeSchema, unsubscribeTool, listSubscriptionsSchema, listSubscriptionsTool } from "./tools/events.js";
16
16
  import { scanRecordEventsSchema, scanRecordEventsTool } from "./tools/record-events.js";
17
17
  import { capabilitiesSchema, capabilitiesTool } from "./capabilities.js";
18
+ import { type AuthFault, logText, responseText, detailInResponse } from "./auth-faults.js";
18
19
  import {
19
20
  ensureDirs,
20
21
  getTokenMap,
21
22
  agentForBearer,
23
+ retiredBearerFor,
22
24
  tokenMapCensusLine,
23
25
  TOKENS_FILE,
24
26
  reloadTokenMapSync,
@@ -767,7 +769,7 @@ function buildServer(initialBound?: string, opts: { trackSession?: boolean } = {
767
769
 
768
770
  addTool(
769
771
  "subscribe",
770
- "Register for a record event: `task` (a phase checkbox newly ticked), `phase` (the last open box in a phase ticked), `item` (a queue item closed), or `pr` (a PR recorded in DONE.md). Events are DERIVED from the record and refused if the ref is not in it, so the stream can never claim something the authoritative markdown does not \u2014 but the TRIGGER is the record's COMMITTED CHANGE (`scan_record_events`), not any one verb, so a hand-edited DONE.md fires exactly like `land` does. Every offered kind has an emitter: the wire enum is generated from the emitter registry, so an unsatisfiable subscription cannot be registered. Re-subscribing returns the existing registration rather than a duplicate.",
772
+ "Register for a record event: `task` (a phase checkbox newly ticked), `phase` (the last open box in a phase ticked), `item` (a queue item closed), or `pr` (a PR recorded in DONE.md). Events are DERIVED from the record and refused if the ref is not in it, so the stream can never claim something the authoritative markdown does not \u2014 but the TRIGGER is the record's COMMITTED CHANGE (`scan_record_events`), not any one verb, so a hand-edited DONE.md fires exactly like `land` does. Every offered kind has an emitter: the wire enum is generated from the emitter registry, so an unsatisfiable subscription cannot be registered. Re-subscribing returns the existing registration rather than a duplicate. SCOPE picks how widely you are asking and is an explicit field rather than a magic target: `exact` (the default, and what every subscription stored before this field meant) matches one target; `kind` matches every event of the kind and REFUSES a target, because a target that was accepted and ignored would read narrower than it behaves; `prefix` matches targets starting with yours — which is how you watch a class whose ids do not exist yet, e.g. `davidbalzan/groundwork-kit#` for one repo's PRs. A prefix is over whatever `target` holds FOR THAT KIND (`pr` the full ref, `item` the queue id, `task` the KEY rather than the ticked line, `phase` the phase), and an EMPTY prefix is refused because it would match everything — that is the wildcard this field exists to avoid spelling in-band; ask for `kind` and say so.",
771
773
  subscribeSchema,
772
774
  gate("agentId", subscribeTool as (a: Record<string, unknown>) => Promise<unknown>),
773
775
  );
@@ -854,6 +856,11 @@ function loadTokenMap(initial: boolean): void {
854
856
  }
855
857
  }
856
858
 
859
+ /** The coord dir, for the bind-time log line that tells an operator where refusals are recorded. */
860
+ function coordRootForLog(): string {
861
+ return process.env.AGENT_COORD_DIR ?? `${process.env.HOME ?? "~"}/agent-coord`;
862
+ }
863
+
857
864
  async function main() {
858
865
  ensureDirs();
859
866
  loadTokenMap(true);
@@ -1117,8 +1124,12 @@ async function startHttp(port: number): Promise<void> {
1117
1124
  // Reverse-lookup: extract bearer from header, map → bound agent. Returns
1118
1125
  // undefined if no map is configured (advisory mode); throws-like return of
1119
1126
  // null if the bearer doesn't match any known agent (caller responds 401).
1120
- function resolveBoundAgent(authHeader: string | undefined): { ok: boolean; agent?: string } {
1121
- if (!authHeader || !authHeader.startsWith("Bearer ")) return { ok: false };
1127
+ function resolveBoundAgent(authHeader: string | undefined): { ok: boolean; agent?: string; fault?: AuthFault } {
1128
+ // ⟨q-a441f1e1⟩ The refusal now carries WHICH fault it was. The caller is told only about the
1129
+ // SHAPE of its request; everything about a credential's validity is uniform to the caller and
1130
+ // detailed in the daemon log — see src/auth-faults.ts for why that split is where it is.
1131
+ if (!authHeader) return { ok: false, fault: { kind: "no-header" } };
1132
+ if (!authHeader.startsWith("Bearer ")) return { ok: false, fault: { kind: "not-bearer" } };
1122
1133
  const bearer = authHeader.slice("Bearer ".length);
1123
1134
  // ⟨q-54adf191⟩ Pick up a tokens.json that changed while this daemon ran. Rate-limited, so the
1124
1135
  // steady state is one stat; the file is read only when its stamp moved. Every outcome that is
@@ -1143,10 +1154,16 @@ async function startHttp(port: number): Promise<void> {
1143
1154
  // actually connects gets a tail, so a 20-agent map does not spawn 20 timers for seats that
1144
1155
  // may not exist. Idempotent — steady state is one Map lookup per request.
1145
1156
  if (agent) startTailOnBind(agent);
1146
- return agent ? { ok: true, agent } : { ok: false };
1157
+ if (agent) return { ok: true, agent };
1158
+ if (bearer === "") return { ok: false, fault: { kind: "empty-bearer" } };
1159
+ // Retired is asked only AFTER the forced re-read above: a bearer minted seconds ago must not
1160
+ // be reported as retired because this process had not read the file yet.
1161
+ const retired = retiredBearerFor(bearer);
1162
+ return { ok: false, fault: retired ? { kind: "retired-bearer", retired } : { kind: "unknown-bearer" } };
1147
1163
  }
1148
1164
  // Advisory mode: only check the shared bearer matches.
1149
- return sharedExpected && authHeader === sharedExpected ? { ok: true } : { ok: false };
1165
+ if (sharedExpected && authHeader === sharedExpected) return { ok: true };
1166
+ return { ok: false, fault: { kind: "advisory-mismatch" } };
1150
1167
  }
1151
1168
 
1152
1169
  // ⛔ ONE HANDLER, N SERVERS. Node binds a single address per `http.Server`, so a list of
@@ -1171,8 +1188,13 @@ async function startHttp(port: number): Promise<void> {
1171
1188
  // observe traffic; TLS termination is the answer to that.
1172
1189
  const resolved = resolveBoundAgent(req.headers.authorization);
1173
1190
  if (!resolved.ok) {
1191
+ // ⟨q-a441f1e1⟩ The LOG is where an operator diagnosing "my seat stopped working" looks, and
1192
+ // it distinguishes every fault. The RESPONSE names only what the caller already knows about
1193
+ // its own request. Neither ever contains any part of a bearer.
1194
+ const fault = resolved.fault ?? { kind: "unknown-bearer" as const };
1195
+ console.error(`[agent-coord-mcp] ${logText(fault)}`);
1174
1196
  res.writeHead(401, { "Content-Type": "text/plain", "WWW-Authenticate": "Bearer" });
1175
- res.end("unauthorized\n");
1197
+ res.end(`${responseText(fault)}\n`);
1176
1198
  return;
1177
1199
  }
1178
1200
 
@@ -1217,6 +1239,25 @@ async function startHttp(port: number): Promise<void> {
1217
1239
  {
1218
1240
  const startupCensus = tokenMapCensusLine();
1219
1241
  if (startupCensus) console.error(startupCensus);
1242
+ // ⛔ ⟨q-a441f1e1⟩ A BUS RUNNING WITH THE ORACLE ON SAYS SO, AT BIND. qa: the switch existed and
1243
+ // NOTHING announced it — no startup line, no warning — so a daemon could distinguish validity
1244
+ // faults to unauthenticated callers while neither its log nor its capabilities mentioned it.
1245
+ // An operator must not have to read the environment of a running process to learn this.
1246
+ // ⭐ ⟨q-a441f1e1⟩ WHERE THE WITHHELD REASONS GO, stated at bind so an operator has the exact
1247
+ // destination without it ever appearing in a 401 body. The response points at "this bus's daemon
1248
+ // log" by role; this line is what turns that role into a path for the person entitled to it.
1249
+ console.error(
1250
+ `[agent-coord-mcp] 401 reasons (unknown / retired / advisory-mismatch bearers) are written HERE, to this ` +
1251
+ `process's stderr — pid ${process.pid}, agent-coord dir ${coordRootForLog()}. A refused caller is told to ` +
1252
+ `look here and is told nothing else.`,
1253
+ );
1254
+ if (detailInResponse()) {
1255
+ console.error(
1256
+ `[agent-coord-mcp] ⛔ AGENT_COORD_401_DETAIL=response — this bus tells an UNAUTHENTICATED caller ` +
1257
+ `whether a bearer was never minted, or was genuine and retired, and when. That confirms a real ` +
1258
+ `credential to whoever just failed to authenticate. Unset it to keep the detail in this log only.`,
1259
+ );
1260
+ }
1220
1261
  }
1221
1262
  await Promise.all(
1222
1263
  bindAddrs.map(
package/src/store.ts CHANGED
@@ -392,8 +392,30 @@ export type TokenMapRefresh = {
392
392
  count: number | null;
393
393
  /** Present only when the reload was REFUSED; the previous map still serves. */
394
394
  refused?: string;
395
+ /**
396
+ * ⟨q-a441f1e1⟩ Bearers that were in the map before this reload and are not in it after — the only
397
+ * moment the daemon can ever observe a retirement, because nothing on disk records one.
398
+ * Hashes, never secrets. Empty unless `changed` is true.
399
+ */
400
+ retired?: RetiredHash[];
395
401
  };
396
402
 
403
+ /** A bearer hash that left the map, with the agent it belonged to. ⛔ A HASH, NEVER A BEARER. */
404
+ export type RetiredHash = { hash: string; agentId: string };
405
+
406
+ /**
407
+ * ⟨q-a441f1e1⟩ Which entries LEFT the map. Pure, and separate from the reload decision so it can be
408
+ * exercised on posed maps: a rotation (`coord-token add` on an existing agent) and a revoke
409
+ * (`coord-token revoke`) both present here as a hash that is gone, and they are told apart by
410
+ * whether the agent still holds some other key afterwards.
411
+ */
412
+ export function retiredSince(before: Map<string, string> | null, after: Map<string, string> | null): RetiredHash[] {
413
+ if (!before || !after) return [];
414
+ const out: RetiredHash[] = [];
415
+ for (const [hash, agentId] of before) if (!after.has(hash)) out.push({ hash, agentId });
416
+ return out;
417
+ }
418
+
397
419
  // The file identity we compare against: mtime, size AND inode.
398
420
  //
399
421
  // ⛔ THE INODE IS LOAD-BEARING, NOT BELT-AND-BRACES. `coord-token` rotates by writing a temp file and
@@ -463,7 +485,12 @@ export function decideTokenMapRefresh(
463
485
  }
464
486
  return {
465
487
  next: { map: next, stamp, checkedAt: now },
466
- result: { changed: true, reason: state.stamp === null ? "appeared" : "changed", count: next.size },
488
+ result: {
489
+ changed: true,
490
+ reason: state.stamp === null ? "appeared" : "changed",
491
+ count: next.size,
492
+ retired: retiredSince(state.map, next),
493
+ },
467
494
  };
468
495
  } catch (e) {
469
496
  // The stamp is deliberately NOT advanced: a half-written file that is fixed in place, with no
@@ -472,6 +499,58 @@ export function decideTokenMapRefresh(
472
499
  }
473
500
  }
474
501
 
502
+ /**
503
+ * ⟨q-a441f1e1⟩ — WHAT THIS PROCESS WATCHED LEAVE THE TOKEN MAP. Hash → who held it and when it went.
504
+ *
505
+ * ⛔ IN MEMORY, DELIBERATELY, AND IT FORGETS ON RESTART. Nothing on disk records a superseded token:
506
+ * `coord-token add` overwrites an agent's entry and `revoke` deletes it, so after either the old
507
+ * bearer is simply absent — byte-for-byte identical to one that never existed. The ONLY moment the
508
+ * difference is observable is the reload that removes it, which this process is already doing.
509
+ * Persisting it would mean a new file or a schema change (⟨q-4b9cdeab⟩'s `previous` is documented in
510
+ * a comment and written by NOTHING), and release A writes neither form of token. So:
511
+ * · a bearer retired while this daemon was RUNNING is reported as retired;
512
+ * · a bearer retired while it was DOWN reads as unknown, and that is the honest answer — this
513
+ * process never saw it valid. A test asserts that limit rather than only the happy path.
514
+ * Hashes only: a retirement record is never a place a secret appears.
515
+ */
516
+ const RETIRED_MAX = 512;
517
+ const retiredBearers = new Map<string, RetiredBearerRecord>();
518
+ export type RetiredBearerRecord = { agentId: string; at: number; cause: "replaced" | "revoked" };
519
+
520
+ /** Record what a reload removed. `cause` is derived, not guessed: see `retirementCause`. */
521
+ export function noteRetired(entries: RetiredHash[], after: Map<string, string> | null, now = Date.now()): void {
522
+ for (const { hash, agentId } of entries) {
523
+ retiredBearers.set(hash, { agentId, at: now, cause: retirementCause(agentId, after) });
524
+ // Bounded so a long-lived daemon rotating often cannot grow this without limit. Insertion order
525
+ // is iteration order for a Map, so the oldest record is the one that goes.
526
+ if (retiredBearers.size > RETIRED_MAX) {
527
+ const oldest = retiredBearers.keys().next();
528
+ if (!oldest.done) retiredBearers.delete(oldest.value);
529
+ }
530
+ }
531
+ }
532
+
533
+ /**
534
+ * Replaced or revoked? The agent still holding a key after the reload was ROTATED; an agent with no
535
+ * key left was REVOKED. Derived from the map itself rather than from which command an operator ran,
536
+ * because the daemon never sees the command.
537
+ */
538
+ export function retirementCause(agentId: string, after: Map<string, string> | null): "replaced" | "revoked" {
539
+ if (!after) return "revoked";
540
+ for (const held of after.values()) if (held === agentId) return "replaced";
541
+ return "revoked";
542
+ }
543
+
544
+ /** What this process knows about a presented bearer that is no longer live, or undefined. */
545
+ export function retiredBearerFor(bearer: string): RetiredBearerRecord | undefined {
546
+ return retiredBearers.get(bearerHash(bearer));
547
+ }
548
+
549
+ /** Test seam only: forget every retirement, so a case can pose an empty process memory. */
550
+ export function clearRetiredBearers(): void {
551
+ retiredBearers.clear();
552
+ }
553
+
475
554
  export function refreshTokenMapIfChanged(opts: { force?: boolean; now?: number } = {}): TokenMapRefresh {
476
555
  if (!tokenMapInitialized) getTokenMap();
477
556
  const { next, result } = decideTokenMapRefresh(
@@ -479,6 +558,8 @@ export function refreshTokenMapIfChanged(opts: { force?: boolean; now?: number }
479
558
  { stamp: stampOf, read: readTokenMapSync },
480
559
  opts,
481
560
  );
561
+ // Before the cache moves: everything the old map held and the new one does not is retired.
562
+ if (result.retired?.length) noteRetired(result.retired, next.map, opts.now);
482
563
  tokenMapCache = next.map;
483
564
  tokenMapStamp = next.stamp;
484
565
  tokenMapCheckedAt = next.checkedAt;
@@ -729,6 +810,70 @@ function sanitize(id: string): string {
729
810
  return id.replace(/[^a-zA-Z0-9._-]/g, "_");
730
811
  }
731
812
 
813
+ /* ── ⟨q-524305dd⟩ ONE DELIVERER PER (SEAT, SOURCE) AT A TIME, ACROSS PROCESSES ─────────────────────
814
+ Two paths type a room message into a herdr pane: the SENDER's server at send time (herdr-delivery)
815
+ and the RECIPIENT's own tail (herdr-tail). Each read the push cursor, typed, waited seconds to
816
+ verify the screen, and only then advanced it; the other read the same not-yet-advanced cursor in
817
+ that window and typed the message again. Measured 2026-09-24: 6 of 39 room messages reached one
818
+ seat's pane twice, 2.7–3.5s apart. The only guard was a closure-local `inFlight` flag — per
819
+ PROCESS, against a race between processes.
820
+ So read → type → verify → advance runs under a lock BOTH paths can see: a directory lock
821
+ (proper-lockfile, the same one appendJsonl uses) per seat and source.
822
+ ⛔ A holder that DIES must not deafen the seat. The lock's mtime is refreshed while its owner's
823
+ event loop runs, and it is STALE after DELIVERY_LOCK_STALE_MS without a refresh, so a killed sender
824
+ costs a bounded stall, never a permanent one. It must outlast the slowest push: herdr's verify
825
+ sleeps by spinning, which blocks the refresh for the push's whole duration.
826
+ ⛔ A compromised lock is LOGGED, never thrown: the library's default throws asynchronously, which
827
+ would take the bus server down with it. */
828
+ const envMs = (name: string, fallback: number, min: number) => { const n = Number(process.env[name]); return Number.isFinite(n) && n >= min ? Math.floor(n) : fallback; };
829
+ export const DELIVERY_LOCK_STALE_MS = envMs("AGENT_COORD_DELIVERY_LOCK_STALE_MS", 60_000, 2_000);
830
+ export const DELIVERY_LOCK_WAIT_MS = envMs("AGENT_COORD_DELIVERY_LOCK_WAIT_MS", 30_000, 0);
831
+ export const DELIVERY_LOCK_DIR = path.join(ROOT, "delivery-locks");
832
+ export const deliveryLockPath = (agentId: string, sourceKey: string) => path.join(DELIVERY_LOCK_DIR, `${sanitize(agentId)}--${sanitize(sourceKey)}`);
833
+ export type DeliveryLockOutcome<T> = { ran: true; value: T } | { ran: false; why: string };
834
+
835
+ /**
836
+ * Run `fn` as the only deliverer to `agentId`'s `sourceKey` ("dm" or "room:<chan>") on this bus.
837
+ * `wait: false` (a tail tick) answers `ran: false` at once when another deliverer holds it — the next
838
+ * tick re-reads the cursor. `wait: true` (the send-time push) waits up to DELIVERY_LOCK_WAIT_MS rather
839
+ * than skipping, because the seat may have NO tail (⟨q-cdb5b007⟩) and a skipped send-time push would
840
+ * then be a silent drop. The lock is released on every path out of `fn`, including a throw.
841
+ */
842
+ export async function withDeliveryLock<T>(
843
+ agentId: string,
844
+ sourceKey: string,
845
+ fn: () => Promise<T>,
846
+ opts: { wait: boolean; log?: (line: string) => void },
847
+ ): Promise<DeliveryLockOutcome<T>> {
848
+ const target = deliveryLockPath(agentId, sourceKey);
849
+ // A compromise or a failed release is ALWAYS logged — to the caller's sink, or stderr when none is given.
850
+ // (Not an optional call: a lock event that is silently dropped is the invisible failure this lock exists to end.)
851
+ const log = opts.log ?? ((line: string) => console.error(line));
852
+ mkdirSync(DELIVERY_LOCK_DIR, { recursive: true });
853
+ const deadline = Date.now() + (opts.wait ? DELIVERY_LOCK_WAIT_MS : 0);
854
+ let release: () => Promise<void>;
855
+ for (;;) {
856
+ try {
857
+ release = await lockfile.lock(target, {
858
+ realpath: false,
859
+ stale: DELIVERY_LOCK_STALE_MS,
860
+ retries: 0,
861
+ onCompromised: (e: Error) => log(`[delivery-lock] ${sourceKey} for ${agentId} was COMPROMISED (${e.message}) — another deliverer may have taken it over; logged, not thrown`),
862
+ });
863
+ break;
864
+ } catch (e) {
865
+ if ((e as NodeJS.ErrnoException).code !== "ELOCKED") return { ran: false, why: `delivery lock unavailable: ${(e as Error).message}` };
866
+ if (Date.now() >= deadline) return { ran: false, why: `another deliverer holds ${sourceKey} for ${agentId}` };
867
+ await new Promise((r) => setTimeout(r, 40 + Math.floor(Math.random() * 60)));
868
+ }
869
+ }
870
+ try {
871
+ return { ran: true, value: await fn() };
872
+ } finally {
873
+ try { await release(); } catch (e) { log(`[delivery-lock] release of ${sourceKey} for ${agentId} failed: ${(e as Error).message}`); }
874
+ }
875
+ }
876
+
732
877
  export async function rewriteJsonl<T>(
733
878
  file: string,
734
879
  filter: (entry: T) => boolean
@@ -0,0 +1,21 @@
1
+ // ⟨q-f2cf9e0f⟩ — HOW A PASSING TEST IS ATTRIBUTED TO A FILE, in src/ so a probe can CALL it.
2
+ //
3
+ // `check-test-count` compares per-file pass counts against a committed record, and both the record's
4
+ // keys and the glob's selections are paths relative to the package. The attribution key has to be
5
+ // the SAME thing or the comparison is between two different namespaces.
6
+ //
7
+ // ⛔ IT WAS `test/${basename(file)}`, WHICH FLATTENS. Every test file sat directly in `test/`, so the
8
+ // flattened key and the real one were identical and the defect was invisible — until a suite moved to
9
+ // `test/live/`, whose passes then landed on the phantom key `test/<name>` while the real entry read
10
+ // zero. A per-file guard that mis-keys is worse than no guard: it reports drift that is not there and
11
+ // hides drift that is.
12
+ //
13
+ // Lives in src/ rather than beside the script for the reason measured in ⟨q-0c5476ec⟩: a probe in
14
+ // dist/ importing out of scripts/ breaks any install that ships dist without scripts.
15
+ import path from "node:path";
16
+
17
+ /** The record/glob key for a test file: its path relative to the package, POSIX-separated. */
18
+ export function testCountKey(pkgRoot: string, file: string | undefined): string {
19
+ if (!file) return "(unattributed)";
20
+ return path.relative(pkgRoot, file).split(path.sep).join("/");
21
+ }
@@ -23,11 +23,31 @@ const subsFile = () => path.join(ROOT, "subscriptions.json");
23
23
  // only if the enum cannot be widened without an emitter.
24
24
  export { EVENT_KINDS, EVENT_KIND_IDS };
25
25
  export type { RecordEvent, SubKind };
26
+ /**
27
+ * ⟨q-8e00b367⟩(a) — HOW WIDELY A SUBSCRIPTION IS ASKING, as an EXPLICIT FIELD.
28
+ *
29
+ * ⛔ THE ALTERNATIVE WAS AN IN-BAND SENTINEL AND IT IS FORBIDDEN BY DESIGN. `target: "*"` would make
30
+ * one string mean two things — a literal target that happens to be an asterisk, and "everything" —
31
+ * and nothing in the wire shape would say which was meant. A separate field cannot be ambiguous.
32
+ *
33
+ * ⚠ `prefix` IS A PREFIX OF WHATEVER `target` HOLDS FOR THAT KIND, which differs per kind and is not
34
+ * guessable from the name: `pr` → the full ref (`owner/repo#402`), `item` → the queue id, `task` →
35
+ * the KEY (its `ref` is the ticked LINE, so a prefix here is NOT a prefix of the line), `phase` →
36
+ * the phase. Measured from the emitters in record-events.ts, not from documentation.
37
+ */
38
+ export const SUB_SCOPES = ["exact", "kind", "prefix"] as const;
39
+ export type SubScope = (typeof SUB_SCOPES)[number];
40
+
26
41
  export type Subscription = {
27
42
  id: string;
28
43
  agentId: string;
29
44
  kind: SubKind;
30
45
  target: string;
46
+ /**
47
+ * Absent ⇒ `"exact"`, which is what every subscription stored before this field meant. The
48
+ * widening is ADDITIVE: no migration, and no stored registration changes meaning.
49
+ */
50
+ scope?: SubScope;
31
51
  createdAt: number;
32
52
  /**
33
53
  * null until an event OF THIS KIND was evaluated against this subscription.
@@ -195,9 +215,33 @@ export function eventIsDerived(recordText: string, ev: RecordEvent): { ok: true
195
215
  return { ok: true };
196
216
  }
197
217
 
198
- /** Subscriptions matching an event. Exact target match; no wildcards yet. */
218
+ /**
219
+ * Subscriptions matching an event, by the scope each one asked for.
220
+ *
221
+ * ⛔ THE GAP THIS CLOSES IS A DELIVERY OF NOTHING REPORTED AS A DELIVERY. With exact-only matching,
222
+ * "tell me when a new PR arrives" is inexpressible — a `pr` event's `target` IS its ref, so you would
223
+ * have to name `owner/repo#402` at subscribe time, and the entire point is that you do not know it
224
+ * yet. A poller could then scan correctly, emit a well-formed event, and match ZERO subscriptions
225
+ * while every instrument reported success: the tick exits 0, the scan finds a change, the emit
226
+ * succeeds, `list_subscriptions` shows healthy watches. Nothing anywhere reads as wrong.
227
+ *
228
+ * ⚠ `exact` REMAINS THE DEFAULT, so a widened match is always something a subscriber opted into.
229
+ */
199
230
  export const matching = (subs: Subscription[], ev: RecordEvent): Subscription[] =>
200
- subs.filter((s) => s.kind === ev.kind && s.target === ev.target);
231
+ subs.filter((s) => {
232
+ if (s.kind !== ev.kind) return false;
233
+ switch (s.scope ?? "exact") {
234
+ case "kind":
235
+ return true;
236
+ // `startsWith` on a non-empty prefix only: an EMPTY prefix matches every target, which is the
237
+ // `"*"` sentinel this design rejected, arriving through the field that replaced it. `subscribe`
238
+ // refuses to create one; this guard means a hand-edited store cannot smuggle one in either.
239
+ case "prefix":
240
+ return s.target !== "" && ev.target.startsWith(s.target);
241
+ default:
242
+ return s.target === ev.target;
243
+ }
244
+ });
201
245
 
202
246
  export type Delivery = { subscriptionId: string; agentId: string; key: string; status: "delivered" | "duplicate-suppressed" };
203
247
 
@@ -244,18 +288,59 @@ export function evaluate(subs: Subscription[], ev: RecordEvent, now: number): {
244
288
  export const subscribeSchema = {
245
289
  agentId: z.string().min(1),
246
290
  kind: z.enum(EVENT_KIND_IDS),
291
+ // ⚠ STILL REQUIRED AND STILL `min(1)`. `scope:"kind"` does not relax it — it REFUSES it (below), so
292
+ // there is no shape where an empty or absent target is accepted. Relaxing the schema instead would
293
+ // have made `target:""` representable, which is the wildcard this design exists to keep out.
247
294
  target: z.string().min(1),
295
+ // A CLOSED SET, never a free string. The registry generates the kind enum so that a subscription
296
+ // which no emitter can satisfy cannot be registered; a free-form scope would reopen exactly that
297
+ // hole one field over — an unsatisfiable scope is an unsatisfiable subscription.
298
+ scope: z.enum(SUB_SCOPES).optional(),
248
299
  };
249
300
 
250
- export async function subscribeTool(args: { agentId: string; kind: SubKind; target: string }) {
301
+ export async function subscribeTool(args: { agentId: string; kind: SubKind; target: string; scope?: SubScope }) {
302
+ const scope: SubScope = args.scope ?? "exact";
303
+
304
+ // ⛔ `kind` + A TARGET IS REFUSED, NOT IGNORED. An accepted-and-ignored target leaves a
305
+ // subscription that READS NARROWER THAN IT BEHAVES: an inspector sees one specific target and
306
+ // concludes the watch is narrow, while it fires on everything of that kind.
307
+ //
308
+ // ⭐ AND THE ASYMMETRY IS WHAT DECIDES IT RATHER THAN TASTE: refusing is relaxable later, whereas
309
+ // silently-ignoring is NOT tightenable later — the moment such subscriptions exist on disk,
310
+ // tightening changes what an already-stored registration means.
311
+ if (args.scope === "kind" && args.target)
312
+ return {
313
+ ok: false as const,
314
+ error:
315
+ `scope "kind" watches EVERY ${args.kind} event, so the target '${args.target}' would be ignored — refusing rather than ` +
316
+ `accepting a subscription that reads narrower than it behaves. Omit the target for kind-wide, ` +
317
+ `or use scope "prefix" with '${args.target}' to watch the ${args.kind} events whose target starts with it.`,
318
+ };
319
+
320
+ // ⛔ AN EMPTY PREFIX IS THE `"*"` WILDCARD WEARING THE FIELD THAT REPLACED IT. `"".startsWith()` is
321
+ // true of every string, so `scope:"prefix"` with an empty target subscribes to everything of the
322
+ // kind while LOOKING like a narrow, targeted watch — the same back door as `target:"*"`, one layer
323
+ // down and through an approved field. `kind` is how you ask for everything, and it says so.
324
+ if (scope === "prefix" && args.target.trim() === "")
325
+ return {
326
+ ok: false as const,
327
+ error:
328
+ `scope "prefix" with an empty target matches EVERY ${args.kind} event — that is the wildcard this ` +
329
+ `field exists to avoid spelling in-band. Use scope "kind" to watch all of them explicitly, or ` +
330
+ `give a non-empty prefix (e.g. 'davidbalzan/groundwork-kit#' for one repo's PRs).`,
331
+ };
332
+
251
333
  const subs = readSubs();
252
- const dupe = subs.find((s) => s.agentId === args.agentId && s.kind === args.kind && s.target === args.target);
334
+ // Identity includes the SCOPE: `exact` on 'q-1' and `prefix` on 'q-1' are different watches, and
335
+ // collapsing them would silently hand back the narrower one.
336
+ const dupe = subs.find((s) => s.agentId === args.agentId && s.kind === args.kind && s.target === args.target && (s.scope ?? "exact") === scope);
253
337
  if (dupe) return { ok: true as const, subscription: dupe, note: "already subscribed — returning the existing registration rather than a second one" };
254
338
  const sub: Subscription = {
255
339
  id: randomUUID(),
256
340
  agentId: args.agentId,
257
341
  kind: args.kind,
258
342
  target: args.target,
343
+ scope,
259
344
  createdAt: Date.now(),
260
345
  // EXPLICIT null, never left absent. `null` means "we know no scan has run
261
346
  // for this"; `undefined` means "this record predates the field and we
@@ -23,8 +23,8 @@
23
23
  import { mkdirSync, statSync } from "node:fs";
24
24
  import path from "node:path";
25
25
  import { pathToFileURL } from "node:url";
26
- import { activeTransport, HERDR, type TransportMarker } from "../transports/index.js";
27
- import { ROOT, inboxFile, roomFile } from "../store.js";
26
+ import { activeTransport, HERDR, targetOf, type TransportMarker } from "../transports/index.js";
27
+ import { ROOT, inboxFile, roomFile, withDeliveryLock } from "../store.js";
28
28
  import { loadLiveTransports } from "./registry.js";
29
29
  import { newMessagesIn } from "./jsonl-offsets.js";
30
30
  import type { Message } from "./shared.js";
@@ -46,6 +46,8 @@ const safeId = (id: string) => id.replace(/[^A-Za-z0-9._-]/g, "_");
46
46
 
47
47
 
48
48
  export type Where = { kind: "dm" } | { kind: "room"; chan: string };
49
+ /** The (seat, source) key the delivery lock is taken on — ONE spelling, shared by the send-time path and the tail. */
50
+ export const sourceKey = (where: Where) => (where.kind === "dm" ? "dm" : `room:${where.chan}`);
49
51
 
50
52
  /**
51
53
  * ⭐ ONE RENDERER AND ONE CURSOR RULE, SHARED BY BOTH PATHS. The send-time path
@@ -102,9 +104,15 @@ export async function readPushCursorFor(agentId: string): Promise<Record<string,
102
104
  }
103
105
 
104
106
  export type HerdrDeliveryOutcome = { agentId: string; delivered: boolean; enters?: number; error?: string; cursorAdvanced: boolean; deferred?: string };
107
+ /** Every push, whichever path made it, is logged NAMING THE PATH — so a duplicate is separable from a retried write without transcript archaeology (⟨q-524305dd⟩). */
108
+ export const pushLogLine = (path: "send-time" | "tail", id: unknown, target: string | undefined, outcome: string, error?: string) =>
109
+ `[herdr-${path === "tail" ? "tail" : "delivery"}] push id=${String(id ?? "?")} target=${target ?? "?"} outcome=${outcome}` +
110
+ (error ? ` reason=${JSON.stringify(error)}` : "") +
111
+ ` path=${path}`;
105
112
 
106
113
  /** Deliver one just-appended message to every herdr-attached recipient among `recipients`. */
107
- export async function deliverToHerdrSeats(msg: Message, recipients: string[], where: { kind: "dm" } | { kind: "room"; chan: string }): Promise<HerdrDeliveryOutcome[]> {
114
+ export async function deliverToHerdrSeats(msg: Message, recipients: string[], where: { kind: "dm" } | { kind: "room"; chan: string }, opts: { log?: (line: string) => void } = {}): Promise<HerdrDeliveryOutcome[]> {
115
+ const log = opts.log ?? ((l: string) => console.error(l));
108
116
  const t = activeTransport();
109
117
  if (!t || t.kind !== HERDR) return [];
110
118
  let markers: Map<string, TransportMarker>;
@@ -122,34 +130,41 @@ export async function deliverToHerdrSeats(msg: Message, recipients: string[], wh
122
130
  // The coordinator's gate named the old call as "still advances the cursor to EOF"; measured,
123
131
  // it did exactly that. So: find THIS message after the persisted cursor; if any earlier
124
132
  // message to someone else precedes it, defer to the recipient's tail, which delivers in order.
125
- const file = where.kind === "dm" ? inboxFile(agentId) : roomFile(where.chan);
126
- const c = await readPushCursorFor(agentId);
127
- const from = where.kind === "dm" ? Number(c.inboxOffset ?? 0) : Number(((c.roomOffsets as Record<string, number> | undefined) ?? {})[where.chan] ?? 0);
128
- const after = newMessagesIn(file, from);
129
- const at = after.findIndex((m) => m.msg.id === msg.id);
130
- const owed = at === -1 ? [] : after.slice(0, at).filter((m) => m.msg.from !== agentId);
131
- if (at === -1 || owed.length > 0) {
132
- out.push({
133
- agentId,
134
- delivered: false,
135
- cursorAdvanced: false,
136
- deferred: at === -1
137
- ? "this message is not after the push cursor (already delivered, or the file moved) — left to the recipient's tail"
138
- : `${owed.length} earlier message(s) to this seat are still owed — left to the recipient's own tail, which delivers in order`,
139
- });
140
- continue;
141
- }
142
- const rendered = await renderForPane(msg, agentId, where);
143
- // A control never comes through here: send_command hands a herdr seat's control to the
144
- // transport's sendControl directly (a raw-vs-rendered branch here was dead and its
145
- // mutation survived, so it is gone). Everything delivered here is a rendered message.
146
- let r: { delivered: boolean; error?: string; enters?: number; verified?: boolean };
147
- try { r = await t.push(marker, rendered); } catch (e) { r = { delivered: false, error: (e as Error).message }; }
148
- // ⟨q-15d763dc⟩ Only a VERIFIED delivery moves the cursor; anything less leaves the message owed.
149
- const verified = r.delivered && r.verified === true;
150
- // Exactly past THIS message's line — never to the end of the file, which may already hold a later one.
151
- const cursorAdvanced = verified ? await advancePushCursor(agentId, where, after[at]!.end) : false;
152
- out.push({ agentId, delivered: verified, enters: r.enters, error: r.error ?? (r.delivered ? "delivered but not verified — cursor held (q-15d763dc)" : undefined), cursorAdvanced });
133
+ // ⛔ ⟨q-524305dd⟩ read → type → verify → advance as the ONLY deliverer to this seat's source, across
134
+ // processes: the seat's own tail takes the same lock, so it cannot read this cursor mid-verify and
135
+ // type the message a second time. WAIT, never skip: the seat may have no tail at all (⟨q-cdb5b007⟩).
136
+ const locked = await withDeliveryLock(agentId, sourceKey(where), async () => {
137
+ const file = where.kind === "dm" ? inboxFile(agentId) : roomFile(where.chan);
138
+ const c = await readPushCursorFor(agentId);
139
+ const from = where.kind === "dm" ? Number(c.inboxOffset ?? 0) : Number(((c.roomOffsets as Record<string, number> | undefined) ?? {})[where.chan] ?? 0);
140
+ const after = newMessagesIn(file, from);
141
+ const at = after.findIndex((m) => m.msg.id === msg.id);
142
+ const owed = at === -1 ? [] : after.slice(0, at).filter((m) => m.msg.from !== agentId);
143
+ if (at === -1 || owed.length > 0) {
144
+ out.push({
145
+ agentId,
146
+ delivered: false,
147
+ cursorAdvanced: false,
148
+ deferred: at === -1
149
+ ? "this message is not after the push cursor (already delivered, or the file moved) — left to the recipient's tail"
150
+ : `${owed.length} earlier message(s) to this seat are still owed — left to the recipient's own tail, which delivers in order`,
151
+ });
152
+ return;
153
+ }
154
+ const rendered = await renderForPane(msg, agentId, where);
155
+ // A control never comes through here: send_command hands a herdr seat's control to the
156
+ // transport's sendControl directly (a raw-vs-rendered branch here was dead and its
157
+ // mutation survived, so it is gone). Everything delivered here is a rendered message.
158
+ let r: { delivered: boolean; error?: string; enters?: number; verified?: boolean };
159
+ try { r = await t.push(marker, rendered); } catch (e) { r = { delivered: false, error: (e as Error).message }; }
160
+ // ⟨q-15d763dc⟩ Only a VERIFIED delivery moves the cursor; anything less leaves the message owed.
161
+ const verified = r.delivered && r.verified === true;
162
+ log(pushLogLine("send-time", msg.id, targetOf(marker), verified ? "delivered" : r.delivered ? "typed-unconfirmed" : "not-delivered", r.error));
163
+ // Exactly past THIS message's line — never to the end of the file, which may already hold a later one.
164
+ const cursorAdvanced = verified ? await advancePushCursor(agentId, where, after[at]!.end) : false;
165
+ out.push({ agentId, delivered: verified, enters: r.enters, error: r.error ?? (r.delivered ? "delivered but not verified — cursor held (q-15d763dc)" : undefined), cursorAdvanced });
166
+ }, { wait: true, log });
167
+ if (!locked.ran) out.push({ agentId, delivered: false, cursorAdvanced: false, deferred: `${locked.why} — left to that deliverer, which types in order` });
153
168
  }
154
169
  return out;
155
170
  }