forge-workflow 0.1.0-beta.2 → 0.1.0-beta.4

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 (155) hide show
  1. package/.forge/hooks/check-tdd.js +79 -5
  2. package/.forge/hooks/forge-native-hook.js +194 -8
  3. package/AGENTS.md +2 -1
  4. package/CHANGELOG.md +28 -0
  5. package/QUICKSTART.md +6 -2
  6. package/README.md +3 -1
  7. package/bin/forge-cmd.js +1 -1
  8. package/bin/forge.js +95 -19
  9. package/docs/guides/SETUP.md +4 -1
  10. package/docs/guides/SUPPORT.md +5 -0
  11. package/docs/reference/COMMANDS.md +12 -1
  12. package/docs/reference/shepherd.md +114 -3
  13. package/lib/activation/ensure-forge-home.js +135 -0
  14. package/lib/adapters/beads-kernel-compat.js +67 -0
  15. package/lib/adapters/greptile-review-adapter.js +1 -1
  16. package/lib/adapters/pr-state-adapter.js +103 -8
  17. package/lib/adoption-profiles.js +17 -4
  18. package/lib/agents-config.js +5 -0
  19. package/lib/beads-detect.js +60 -0
  20. package/lib/commands/_aliases.js +248 -0
  21. package/lib/commands/_issue.js +65 -41
  22. package/lib/commands/_manifest.js +3 -1
  23. package/lib/commands/_registry.js +14 -0
  24. package/lib/commands/_resolve-command-opts.js +36 -60
  25. package/lib/commands/claim.js +2 -4
  26. package/lib/commands/gate.js +19 -2
  27. package/lib/commands/hooks.js +294 -12
  28. package/lib/commands/init.js +26 -20
  29. package/lib/commands/memory.js +81 -0
  30. package/lib/commands/migrate.js +0 -161
  31. package/lib/commands/plan.js +71 -123
  32. package/lib/commands/pr.js +88 -0
  33. package/lib/commands/prime.js +8 -1
  34. package/lib/commands/push.js +66 -0
  35. package/lib/commands/recall.js +67 -12
  36. package/lib/commands/recap.js +18 -4
  37. package/lib/commands/release.js +15 -3
  38. package/lib/commands/remember.js +86 -20
  39. package/lib/commands/serve.js +5 -2
  40. package/lib/commands/setup.js +135 -73
  41. package/lib/commands/shepherd.js +102 -4
  42. package/lib/commands/ship.js +40 -4
  43. package/lib/commands/skill.js +275 -0
  44. package/lib/commands/status.js +37 -32
  45. package/lib/commands/test.js +32 -0
  46. package/lib/commands/worktree.js +87 -5
  47. package/lib/core/runtime-graph.js +122 -17
  48. package/lib/forge-issues.js +13 -464
  49. package/lib/gate-events.js +54 -55
  50. package/lib/global-flags.js +30 -0
  51. package/lib/grounding/context-events.js +230 -0
  52. package/lib/grounding/read-first.js +112 -0
  53. package/lib/harness-capability-matrix.js +2 -2
  54. package/lib/hook-renderer.js +144 -12
  55. package/lib/issue-backend.js +42 -3
  56. package/lib/kernel/backing-issue.js +7 -1
  57. package/lib/kernel/broker.js +28 -0
  58. package/lib/kernel/migrations.js +30 -2
  59. package/lib/kernel/owned-kernel.js +43 -0
  60. package/lib/kernel/schema.js +35 -0
  61. package/lib/kernel/sqlite-driver.js +187 -1
  62. package/lib/memory-recall.js +151 -0
  63. package/lib/orientation.js +303 -6
  64. package/lib/pr-bundle.js +6 -2
  65. package/lib/pr-monitor/auto-actions.js +175 -0
  66. package/lib/pr-monitor/digest.js +206 -0
  67. package/lib/pr-monitor/reconcile-executor.js +659 -0
  68. package/lib/pr-monitor/reconcile-tick.js +138 -0
  69. package/lib/pr-monitor/reconcile.js +0 -0
  70. package/lib/pr-monitor/render-sticky.js +57 -8
  71. package/lib/pr-monitor/shepherd-lease.js +243 -0
  72. package/lib/pr-monitor/upsert-sticky.js +169 -0
  73. package/lib/pr-monitor/watch-lifecycle.js +1 -1
  74. package/lib/pr-pull.js +113 -17
  75. package/lib/project-memory.js +8 -0
  76. package/lib/release-readiness.js +17 -1
  77. package/lib/rules-sync.js +4 -0
  78. package/lib/runtime-health.js +15 -46
  79. package/lib/skill-eval.js +750 -0
  80. package/lib/status/identity.js +46 -0
  81. package/lib/status/presenter.js +0 -35
  82. package/lib/status/snapshot.js +11 -16
  83. package/lib/upgrade-safety.js +52 -1
  84. package/lib/using-forge.js +315 -0
  85. package/lib/workflow/enforce-stage.js +64 -7
  86. package/lib/workflow/state-manager.js +23 -23
  87. package/package.json +2 -2
  88. package/rules/using-forge.md +24 -0
  89. package/scripts/forge-team/index.sh +0 -5
  90. package/scripts/forge-team/tests/dispatcher.test.sh +1 -1
  91. package/scripts/forge-team/tests/workflow-integration.test.sh +0 -1
  92. package/scripts/pr-auto-actions.js +93 -0
  93. package/scripts/pr-verdict-label.js +50 -0
  94. package/scripts/test.js +8 -1
  95. package/skills/claim-safety/SKILL.md +4 -0
  96. package/skills/claim-safety/evals/scorecard.json +41 -0
  97. package/skills/coverage.json +83 -0
  98. package/skills/dev/SKILL.md +4 -0
  99. package/skills/dev/evals/scorecard.json +41 -0
  100. package/skills/gates/SKILL.md +80 -0
  101. package/skills/gates/evals/evals.json +38 -0
  102. package/skills/gates/evals/scorecard.json +41 -0
  103. package/skills/hermes-forge/SKILL.md +1 -0
  104. package/skills/hermes-forge/evals/scorecard.json +41 -0
  105. package/skills/issue-basics/SKILL.md +1 -0
  106. package/skills/issue-basics/evals/scorecard.json +41 -0
  107. package/skills/kernel/SKILL.md +38 -0
  108. package/skills/kernel/evals/scorecard.json +41 -0
  109. package/skills/memory/SKILL.md +16 -1
  110. package/skills/memory/evals/scorecard.json +41 -0
  111. package/skills/parallel-deep-research/SKILL.md +1 -0
  112. package/skills/parallel-deep-research/evals/scorecard.json +41 -0
  113. package/skills/plan/SKILL.md +6 -0
  114. package/skills/plan/evals/scorecard.json +41 -0
  115. package/skills/portability/SKILL.md +47 -0
  116. package/skills/portability/evals/evals.json +34 -0
  117. package/skills/portability/evals/scorecard.json +41 -0
  118. package/skills/research/SKILL.md +1 -0
  119. package/skills/research/evals/scorecard.json +41 -0
  120. package/skills/review/SKILL.md +6 -0
  121. package/skills/review/evals/scorecard.json +41 -0
  122. package/skills/rollback/SKILL.md +1 -0
  123. package/skills/rollback/evals/scorecard.json +41 -0
  124. package/skills/setup/SKILL.md +91 -0
  125. package/skills/setup/evals/evals.json +42 -0
  126. package/skills/setup/evals/scorecard.json +41 -0
  127. package/skills/shepherd/SKILL.md +76 -36
  128. package/skills/shepherd/evals/evals.json +21 -9
  129. package/skills/shepherd/evals/scorecard.json +41 -0
  130. package/skills/ship/SKILL.md +6 -0
  131. package/skills/ship/evals/scorecard.json +41 -0
  132. package/skills/smith/SKILL.md +8 -0
  133. package/skills/smith/evals/scorecard.json +41 -0
  134. package/skills/sonarcloud/SKILL.md +1 -0
  135. package/skills/sonarcloud/evals/scorecard.json +41 -0
  136. package/skills/sonarcloud-analysis/SKILL.md +1 -0
  137. package/skills/sonarcloud-analysis/evals/scorecard.json +41 -0
  138. package/skills/status/SKILL.md +3 -0
  139. package/skills/status/evals/scorecard.json +41 -0
  140. package/skills/triage-ready/SKILL.md +2 -0
  141. package/skills/triage-ready/evals/scorecard.json +41 -0
  142. package/skills/using-forge/SKILL.md +104 -0
  143. package/skills/using-forge/evals/scorecard.json +41 -0
  144. package/skills/validate/SKILL.md +4 -0
  145. package/skills/validate/evals/scorecard.json +41 -0
  146. package/skills/verify/SKILL.md +4 -0
  147. package/skills/verify/evals/scorecard.json +41 -0
  148. package/skills/worktree/SKILL.md +87 -0
  149. package/skills/worktree/evals/evals.json +38 -0
  150. package/skills/worktree/evals/scorecard.json +41 -0
  151. package/lib/adapters/beads-issue-adapter.js +0 -127
  152. package/lib/commands/board.js +0 -64
  153. package/lib/status/beads-snapshot.js +0 -145
  154. package/scripts/forge-team/lib/dashboard.sh +0 -316
  155. package/scripts/forge-team/tests/dashboard.test.sh +0 -155
@@ -0,0 +1,659 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * The IMPURE half of the autonomous shepherd reconciler (W-S4b, design §3/§4).
5
+ *
6
+ * W-S4a shipped the pure `reconcile(desired, observed, now)` and the `tick()`
7
+ * debounce guard. This module is the thin, side-effecting dispatcher over them:
8
+ * it gathers the two state sets (GitHub via `gh`, kernel via the broker), runs the
9
+ * actions `reconcile()` emits (spawn/stop/reap watchers, upsert/retire kernel_pr
10
+ * rows), owns the SINGLETON DAEMON lease lifecycle, and provides the per-command
11
+ * `fireAndForget()` trigger wired into `bin/forge.js`.
12
+ *
13
+ * The NON-BLOCKING / ERROR-SWALLOWING contract is paramount: `fireAndForget()`
14
+ * MUST never throw and never affect the command that triggered it (it is called
15
+ * from a `finally` in the dispatch chokepoint). Every spawn is modeled on
16
+ * `watch-lifecycle.startPrWatcherDetached` (detached, `stdio:'ignore'`,
17
+ * `windowsHide:true`, `.unref()`, no-op `'error'` listener) so a failed launch
18
+ * degrades to "not started" rather than crashing.
19
+ *
20
+ * SAFETY INVARIANTS (guarded by tests):
21
+ * - Orphan reaping NEVER `process.kill`s on a PID match alone. It re-verifies at
22
+ * kill time: the pid must be alive AND the journal start-time marker for that
23
+ * PR must still exist AND equal the watcher entry's `startedAt`. A null/legacy
24
+ * startedAt, or an absent/mismatched marker, means "do not kill" (PID reuse
25
+ * fail-safe) — the stale entry is dropped silently.
26
+ * - The singleton is arbitrated by the O_EXCL shepherd lease: a daemon that loses
27
+ * `acquire` exits immediately and spawns nothing.
28
+ * - Watcher launch classification branches on CAPABILITY presence
29
+ * (`ctx.harness.hasBgShell`), NEVER on harness name; uncertain → detached.
30
+ *
31
+ * @module pr-monitor/reconcile-executor
32
+ */
33
+
34
+ const fs = require('node:fs');
35
+ const path = require('node:path');
36
+ const { spawn } = require('node:child_process');
37
+
38
+ const shepherdLease = require('./shepherd-lease');
39
+ const journal = require('./journal');
40
+ const { reconcile: defaultReconcile } = require('./reconcile');
41
+ const { tick: defaultTick } = require('./reconcile-tick');
42
+ const { startPrWatcherDetached, defaultResolveSlug, forgeBin } = require('./watch-lifecycle');
43
+ const brokerMod = require('../kernel/broker');
44
+
45
+ const { STALE_MS } = shepherdLease;
46
+
47
+ /** Normalize a lease watcher entry to the W-S4b `{pr, repo, pid, startedAt}` shape. */
48
+ function normalizeWatcher(entry) {
49
+ if (typeof entry === 'number') return { pr: entry, repo: null, pid: null, startedAt: null };
50
+ return {
51
+ pr: entry.pr,
52
+ repo: entry.repo ?? null,
53
+ pid: entry.pid ?? null,
54
+ startedAt: entry.startedAt ?? null,
55
+ };
56
+ }
57
+
58
+ /** File that records a watcher's spawn-time ISO stamp for kill-time re-verification. */
59
+ function claimPath(dir) {
60
+ return path.join(dir, 'watch.startedat');
61
+ }
62
+
63
+ /**
64
+ * Write the start-time marker for `(repo, pr)` into its journal dir. This is the
65
+ * kill-time re-verification token — orphan reaping refuses to kill a pid unless
66
+ * this marker still equals the watcher entry's `startedAt`.
67
+ */
68
+ function writeClaimMarker(projectRoot, repo, pr, startedAt) {
69
+ if (repo == null || pr == null || startedAt == null) return;
70
+ try {
71
+ const dir = journal.journalDir({ root: projectRoot, repo, pr });
72
+ fs.writeFileSync(claimPath(dir), String(startedAt));
73
+ } catch {
74
+ /* best-effort — a missing marker just means the pid is treated as unverifiable (never reaped) */
75
+ }
76
+ }
77
+
78
+ /** Read the start-time marker for `(repo, pr)`, or null when absent/unreadable. */
79
+ function readClaimMarker(projectRoot, repo, pr) {
80
+ if (repo == null || pr == null) return null;
81
+ try {
82
+ const dir = journal.journalDir({ root: projectRoot, repo, pr });
83
+ return fs.readFileSync(claimPath(dir), 'utf8').trim();
84
+ } catch {
85
+ return null;
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Remove the start-time marker for `(repo, pr)` when its watcher is stopped/reaped, so
91
+ * a future PID reuse can't match a STALE marker and get treated as the live watcher.
92
+ * Best-effort; a missing marker is fine (an unverifiable pid is never reaped anyway).
93
+ */
94
+ function removeClaimMarker(projectRoot, repo, pr) {
95
+ if (repo == null || pr == null) return;
96
+ try {
97
+ const dir = journal.journalDir({ root: projectRoot, repo, pr });
98
+ fs.rmSync(claimPath(dir), { force: true });
99
+ } catch {
100
+ /* best-effort marker cleanup */
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Gather the DESIRED open-PR set: GitHub's open PRs (`gh pr list`) enriched with
106
+ * kernel linkage (issue/worktree/journal) where a `kernel_pr` row already exists.
107
+ * A hand-opened PR with no kernel row is still included (issue_id/worktree_id
108
+ * null) so the reconciler self-registers it — zero user invocation. External
109
+ * fields (branch names) are stored raw and NEVER evaluated.
110
+ */
111
+ async function gatherDesired(gitCommonDir, opts = {}) {
112
+ const runGh = opts.runGh || ((args) => require('node:child_process').execFileSync('gh', args, {
113
+ encoding: 'utf8', timeout: 30000, windowsHide: true,
114
+ }));
115
+ // The broker is a live kernel handle from createLocalBroker (listOpenPrs/upsertPr/
116
+ // retirePr are INSTANCE methods) — the daemon threads one in. Never fall back to the
117
+ // broker MODULE namespace: those methods don't exist there and every call would
118
+ // silently no-op behind the catch (this exact bug shipped once — keep it gone).
119
+ const broker = opts.broker || null;
120
+ const repo = opts.repo || defaultResolveSlug({ cwd: opts.projectRoot || process.cwd() });
121
+
122
+ let ghPrs = [];
123
+ // `listingOk` distinguishes "GitHub says zero open PRs" from "the gh call failed"
124
+ // (network/auth/rate-limit). A FAILED listing must be a no-op upstream, never a
125
+ // teardown of every watcher+row — the caller skips the reconcile pass when false.
126
+ let listingOk = true;
127
+ try {
128
+ // `--limit 1000` overrides gh's default 30-result cap so a repo with >30 open
129
+ // PRs is fully enumerated (otherwise the tail would go unwatched or get retired).
130
+ const raw = runGh(['pr', 'list', '--state', 'open', '--limit', '1000', '--json', 'number,headRefName,headRefOid']);
131
+ const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw;
132
+ if (Array.isArray(parsed)) ghPrs = parsed;
133
+ } catch {
134
+ ghPrs = [];
135
+ listingOk = false;
136
+ }
137
+
138
+ let prRows = [];
139
+ try {
140
+ if (broker) prRows = await broker.listOpenPrs(gitCommonDir);
141
+ } catch {
142
+ /* kernel unavailable → treat as no linkage; the GitHub-driven desired set still stands */
143
+ }
144
+ // Key by (repo, number) — matches reconcile.js `keyOf` — so a repo rename that
145
+ // leaves a same-number row under one git_common_dir can't attach the wrong repo's
146
+ // linkage to the desired PR.
147
+ const keyOf = (r, n) => `${r} ${n}`;
148
+ const rowByKey = new Map((Array.isArray(prRows) ? prRows : []).map((r) => [keyOf(r.repo, r.number), r]));
149
+
150
+ const openPrs = ghPrs.map((p) => {
151
+ const row = rowByKey.get(keyOf(repo, p.number));
152
+ return {
153
+ repo,
154
+ number: p.number,
155
+ branch: p.headRefName ?? null,
156
+ headSha: p.headRefOid ?? null,
157
+ issueId: row?.issue_id ?? null,
158
+ worktreeId: row?.worktree_id ?? null,
159
+ journalPtr: row?.journal_ptr ?? null,
160
+ };
161
+ });
162
+ return { openPrs, gitCommonDir, listingOk };
163
+ }
164
+
165
+ /**
166
+ * Gather the OBSERVED state: kernel `kernel_pr` rows, the lease watcher set, and
167
+ * which of those watcher pids are actually alive (probed via `pidAlive`).
168
+ */
169
+ async function gatherObserved(gitCommonDir, lock, opts = {}) {
170
+ const broker = opts.broker || null; // live kernel handle threaded by the daemon; never the module namespace
171
+ const isAlive = opts.isAlive || shepherdLease.pidAlive;
172
+ const readClaim = opts.readClaim || ((repo, pr) => readClaimMarker(opts.projectRoot, repo, pr));
173
+ const now = (opts.now || (() => Date.now()))();
174
+
175
+ let prRows = [];
176
+ try {
177
+ if (broker) prRows = await broker.listOpenPrs(gitCommonDir);
178
+ } catch {
179
+ /* kernel unavailable → observe an empty kernel set; reconcile still converges watchers */
180
+ }
181
+
182
+ const watchers = (lock && Array.isArray(lock.watchers) ? lock.watchers : []).map(normalizeWatcher);
183
+ // A pid is "live" ONLY when it is alive AND its journal start-time marker still
184
+ // equals the entry's startedAt — the SAME check verifiedKill makes at kill time.
185
+ // A reused PID (alive, but a mismatched/absent marker) must NOT be reported live,
186
+ // or reconcile would suppress the startWatcher and leave the PR unmonitored.
187
+ const liveWatcherPids = watchers
188
+ .filter((w) => {
189
+ if (w.pid == null || !isAlive(w.pid)) return false;
190
+ const marker = readClaim(w.repo, w.pr);
191
+ return marker != null && String(marker) === String(w.startedAt);
192
+ })
193
+ .map((w) => ({ pid: w.pid, startedAt: w.startedAt ?? null }));
194
+
195
+ const beat = lock ? Date.parse(lock.heartbeatAt) : NaN;
196
+ const leaseFresh = Number.isFinite(beat) && (now - beat) < STALE_MS;
197
+
198
+ return { lease: lock || null, leaseFresh, prRows: Array.isArray(prRows) ? prRows : [], liveWatcherPids };
199
+ }
200
+
201
+ /**
202
+ * Kill a watcher pid ONLY after re-verifying start-time (design risk #4). Returns
203
+ * true iff the kill actually happened. Never kills on a PID match alone.
204
+ */
205
+ function verifiedKill(entry, ctx) {
206
+ if (!entry || entry.pid == null || entry.startedAt == null) return false;
207
+ const isAlive = ctx.isAlive || shepherdLease.pidAlive;
208
+ if (!isAlive(entry.pid)) return false;
209
+ const readClaim = ctx.readClaim || ((e) => readClaimMarker(ctx.projectRoot, e.repo, e.pr));
210
+ const claim = readClaim(entry);
211
+ if (claim == null || String(claim) !== String(entry.startedAt)) return false;
212
+ try {
213
+ (ctx.kill || process.kill)(entry.pid);
214
+ } catch {
215
+ return false;
216
+ }
217
+ return true;
218
+ }
219
+
220
+ /**
221
+ * Per-action-type handlers, keyed by `action.type`. Extracted from `execute` so
222
+ * each is small and independently testable and the dispatcher stays a flat loop
223
+ * (keeps `execute`'s cognitive complexity under the SonarCloud gate). Each handler
224
+ * mutates the shared `s` state (`s.watchers` is reassigned by stop/reap) and the
225
+ * behavior is identical to the former if/else-if chain.
226
+ */
227
+ const ACTION_HANDLERS = {
228
+ startWatcher(action, s) {
229
+ const startedAt = new Date(s.now()).toISOString();
230
+ const repo = action.pr.repo ?? s.repo ?? null;
231
+ const res = s.spawnWatcher({ prNumber: action.pr.number, cwd: s.projectRoot });
232
+ const pid = res && res.pid != null ? res.pid : null;
233
+ // A pid-less result means the watcher is already running (ship/push/adopt started it,
234
+ // startPrWatcherDetached → {started:false, reason:'already-running'}) or the spawn
235
+ // failed. Do NOT record a {pid:null} entry: gatherObserved never counts it live, so
236
+ // each interval would re-emit startWatcher and append another null entry forever.
237
+ if (pid == null) return;
238
+ const entry = { pr: action.pr.number, repo, pid, startedAt };
239
+ s.watchers.push(entry);
240
+ s.writeClaim(entry);
241
+ },
242
+ stopWatcher(action, s) {
243
+ for (const entry of s.watchers.filter((w) => w.pr === action.pr.number)) {
244
+ verifiedKill(entry, s.ctx);
245
+ s.removeClaim(entry); // clear the start-time marker so a reused PID can't match it later
246
+ }
247
+ s.watchers = s.watchers.filter((w) => w.pr !== action.pr.number);
248
+ },
249
+ reapOrphan(action, s) {
250
+ const entry = s.watchers.find((w) => w.pid === action.pid && w.startedAt === action.startedAt);
251
+ verifiedKill(entry, s.ctx);
252
+ if (entry) s.removeClaim(entry);
253
+ s.watchers = s.watchers.filter((w) => !(w.pid === action.pid && w.startedAt === action.startedAt));
254
+ },
255
+ async upsertPrRow(action, s) {
256
+ try {
257
+ if (s.broker) await s.broker.upsertPr(action.row);
258
+ } catch {
259
+ /* derived reconcile state — a failed upsert retries on the next converge */
260
+ }
261
+ },
262
+ async retire(action, s) {
263
+ try {
264
+ if (s.broker) await s.broker.retirePr(
265
+ { git_common_dir: s.gitCommonDir, repo: action.pr.repo, number: action.pr.number },
266
+ { state: 'closed', retired_at: new Date(s.now()).toISOString() },
267
+ );
268
+ } catch {
269
+ /* retried on the next converge */
270
+ }
271
+ },
272
+ };
273
+
274
+ /**
275
+ * Dispatch a reconcile action set. Idempotent and order-free. Returns the updated
276
+ * watcher entry list (`{pr,repo,pid,startedAt}[]`) for the caller to publish via
277
+ * `updateWatchers`. `ctx.watchers` seeds the current set (from observed state).
278
+ */
279
+ async function execute(actions, ctx = {}) {
280
+ const s = {
281
+ broker: ctx.broker || null, // live kernel handle threaded by the daemon; never the module namespace
282
+ spawnWatcher: ctx.spawnWatcher || startPrWatcherDetached,
283
+ writeClaim: ctx.writeClaim || ((e) => writeClaimMarker(ctx.projectRoot, e.repo, e.pr, e.startedAt)),
284
+ removeClaim: ctx.removeClaim || ((e) => removeClaimMarker(ctx.projectRoot, e.repo, e.pr)),
285
+ now: ctx.now || (() => Date.now()),
286
+ repo: ctx.repo,
287
+ projectRoot: ctx.projectRoot,
288
+ gitCommonDir: ctx.gitCommonDir,
289
+ watchers: Array.isArray(ctx.watchers) ? ctx.watchers.map(normalizeWatcher) : [],
290
+ ctx, // verifiedKill reads isAlive/readClaim/kill/projectRoot off the original ctx
291
+ };
292
+
293
+ for (const action of (Array.isArray(actions) ? actions : [])) {
294
+ const handler = ACTION_HANDLERS[action && action.type];
295
+ if (handler) await handler(action, s);
296
+ }
297
+ return s.watchers;
298
+ }
299
+
300
+ /**
301
+ * One converge pass: gather → reconcile → execute → publish watchers. Used by the
302
+ * daemon loop and directly unit-testable with injected gather/reconcile/execute.
303
+ * Returns `{ actions, watchers, desiredCount }`.
304
+ */
305
+ async function convergeOnce(projectRoot, opts = {}) {
306
+ const gitCommonDir = opts.gitCommonDir;
307
+ const reconcile = opts.reconcile || defaultReconcile;
308
+ const now = opts.now || (() => Date.now());
309
+ const lock = opts.lock !== undefined ? opts.lock : null; // daemon threads the live lock in; default null
310
+
311
+ const desired = opts.gatherDesired
312
+ ? await opts.gatherDesired()
313
+ : await gatherDesired(gitCommonDir, { ...opts, projectRoot });
314
+
315
+ // A FAILED gh listing (listingOk === false) is a transient outage, not "zero open
316
+ // PRs". Skipping the reconcile+execute pass entirely makes it a true no-op — no
317
+ // observe, no retire/stopWatcher teardown of every row+watcher. desiredCount is
318
+ // left non-zero (null) so the daemon does NOT read it as "no PRs → self-retire".
319
+ if (desired && desired.listingOk === false) {
320
+ const keep = (lock && Array.isArray(lock.watchers)) ? lock.watchers : (opts.watchers || []);
321
+ return { actions: [], watchers: keep, desiredCount: null, listingOk: false };
322
+ }
323
+
324
+ const observed = opts.gatherObserved
325
+ ? await opts.gatherObserved()
326
+ : await gatherObserved(gitCommonDir, lock, { ...opts, now, projectRoot });
327
+
328
+ const { actions } = reconcile(desired, observed, now());
329
+ const seedWatchers = observed.lease && Array.isArray(observed.lease.watchers)
330
+ ? observed.lease.watchers
331
+ : (opts.watchers || []);
332
+ const watchers = await execute(actions, {
333
+ ...opts,
334
+ projectRoot,
335
+ gitCommonDir,
336
+ watchers: seedWatchers,
337
+ now,
338
+ });
339
+
340
+ let leaseLost = false;
341
+ const updateWatchers = opts.updateWatchers || shepherdLease.updateWatchers;
342
+ if (opts.token) {
343
+ try {
344
+ // updateWatchers returns false when the lock is gone or owned by a DIFFERENT
345
+ // token — i.e. THIS daemon was superseded (its stale lease reclaimed by a newer
346
+ // one). Signal the caller to stop: a superseded daemon must not keep spawning/
347
+ // reaping watchers behind the live owner's back.
348
+ const published = updateWatchers(projectRoot, watchers, { gitCommonDir, token: opts.token });
349
+ if (published === false) leaseLost = true;
350
+ } catch {
351
+ /* publishing the watcher set is best-effort — the next pass re-derives it */
352
+ }
353
+ }
354
+ return { actions, watchers, desiredCount: desired.openPrs.length, leaseLost };
355
+ }
356
+
357
+ /**
358
+ * The singleton daemon: acquire the lease (exit if a live foreign owner holds it),
359
+ * heartbeat, converge on a cadence, self-retire when no PRs remain. `opts.once`
360
+ * runs a single converge (for tests); otherwise an interval loop + signal handlers.
361
+ */
362
+ async function runDaemon(projectRoot, opts = {}) {
363
+ const gitCommonDir = opts.gitCommonDir || brokerMod.resolveGitCommonDir(projectRoot);
364
+ const acquire = opts.acquire || shepherdLease.acquire;
365
+ const startHeartbeat = opts.startHeartbeat || shepherdLease.startHeartbeat;
366
+ const stopHeartbeat = opts.stopHeartbeat || shepherdLease.stopHeartbeat;
367
+ const release = opts.release || shepherdLease.release;
368
+ const converge = opts.convergeOnce || convergeOnce;
369
+ const now = opts.now || (() => Date.now());
370
+ // Injectable exit so a lifecycle test can assert the daemon actually exits
371
+ // (finding 4). `opts.exit === false` keeps the process alive (legacy test mode).
372
+ const exit = typeof opts.exit === 'function'
373
+ ? opts.exit
374
+ : (opts.exit === false ? () => {} : (code) => process.exit(code));
375
+
376
+ const res = acquire(projectRoot, { gitCommonDir });
377
+ if (!res.ok) {
378
+ // A live, fresh foreign daemon owns this repo — exit immediately, spawn nothing.
379
+ return { ok: false, reason: 'foreign-lease' };
380
+ }
381
+ const token = res.token;
382
+ const heartbeat = startHeartbeat(projectRoot, { gitCommonDir, token });
383
+
384
+ // Build ONE real kernel broker for the daemon's lifetime — createLocalBroker-backed,
385
+ // because listOpenPrs/upsertPr/retirePr are INSTANCE methods (the module namespace has
386
+ // none). Injectable for tests via opts.broker/opts.buildBroker. `ownedDriver` is closed
387
+ // on retire ONLY when we created it (Windows EBUSY guard — a leaked handle wedges the
388
+ // sqlite file). A genuine build failure degrades to the watcher half (broker stays null).
389
+ let broker = opts.broker || null;
390
+ let ownedDriver = null;
391
+ if (!broker) {
392
+ try {
393
+ const built = await (opts.buildBroker || defaultBuildBroker)({ projectRoot, gitCommonDir });
394
+ broker = built.broker;
395
+ ownedDriver = built.driver;
396
+ } catch {
397
+ /* kernel genuinely unavailable → run degraded (watcher convergence only) */
398
+ }
399
+ }
400
+
401
+ const convergeArgs = { ...opts, gitCommonDir, token, broker };
402
+
403
+ // retire() must NEVER throw: a release / stopHeartbeat / driver.close error must
404
+ // not leave the daemon un-exited (finding 4). Each teardown step swallows its own
405
+ // error so the caller's exit(0) always runs — no un-retired zombie.
406
+ const retire = async () => {
407
+ try { release(projectRoot, { gitCommonDir, token }); } catch { /* best effort */ }
408
+ try { stopHeartbeat(heartbeat); } catch { /* best effort */ }
409
+ if (ownedDriver && typeof ownedDriver.close === 'function') {
410
+ try { ownedDriver.close(); } catch { /* best effort */ }
411
+ }
412
+ };
413
+
414
+ if (opts.once) {
415
+ const conv = await converge(projectRoot, convergeArgs);
416
+ if (conv.desiredCount === 0) await retire();
417
+ return { ok: true, token, ...conv };
418
+ }
419
+
420
+ const intervalMs = opts.intervalMs || 60000;
421
+ let stopped = false;
422
+ let inFlight = false; // finding 5: re-entrancy guard — never run two passes at once
423
+ let lastWatchers = []; // finding 2: thread the live watcher set across passes
424
+ let timer = null;
425
+
426
+ const runPass = async () => {
427
+ // A tick that fires while the previous pass is still in flight (converge slower
428
+ // than intervalMs) returns immediately, so passes never race on start/stop/reap.
429
+ if (stopped || inFlight) return;
430
+ inFlight = true;
431
+ try {
432
+ // Thread the live watcher set + a fresh heartbeat stamp so gatherObserved
433
+ // observes the REAL live set each pass (finding 2) — without this the daemon
434
+ // saw lease:null every tick and re-started a watcher for every PR forever.
435
+ const passLock = { watchers: lastWatchers, heartbeatAt: new Date(now()).toISOString() };
436
+ const conv = await converge(projectRoot, { ...convergeArgs, lock: passLock });
437
+ if (conv && Array.isArray(conv.watchers)) lastWatchers = conv.watchers;
438
+ // Superseded: a newer daemon reclaimed our stale lease. Stop and exit — retire()
439
+ // won't touch the foreign lock (release is token-guarded), so the new owner is
440
+ // left intact; we just stop spawning/reaping behind it.
441
+ if (conv && conv.leaseLost) {
442
+ stopped = true;
443
+ if (timer) clearInterval(timer);
444
+ await retire();
445
+ exit(0);
446
+ } else if (conv && conv.desiredCount === 0) {
447
+ stopped = true;
448
+ if (timer) clearInterval(timer);
449
+ await retire();
450
+ exit(0);
451
+ }
452
+ } catch {
453
+ /* a bad converge pass never crashes the daemon — the next tick retries */
454
+ } finally {
455
+ inFlight = false;
456
+ }
457
+ };
458
+
459
+ // finding 3: converge IMMEDIATELY on cold start — don't idle for up to intervalMs.
460
+ await runPass();
461
+ if (stopped) return { ok: true, token, retired: true };
462
+
463
+ timer = setInterval(runPass, intervalMs);
464
+ // The converge timer is intentionally left REF'd so it keeps the daemon process
465
+ // alive between passes (the heartbeat timer is unref'd inside startHeartbeat).
466
+
467
+ const onSignal = async () => { await retire(); exit(0); };
468
+ process.on('SIGINT', onSignal);
469
+ process.on('SIGTERM', onSignal);
470
+
471
+ return { ok: true, token, heartbeat, timer };
472
+ }
473
+
474
+ /**
475
+ * Launch the singleton daemon. Classify the execution home by CAPABILITY presence
476
+ * (`ctx.harness.hasBgShell`), NEVER by harness name; uncertain → detached spawn
477
+ * modeled on `startPrWatcherDetached`. Never throws.
478
+ */
479
+ function launchDaemon(ctx = {}) {
480
+ const harness = ctx.harness || {};
481
+ if (harness.hasBgShell && typeof harness.runBgShell === 'function') {
482
+ try {
483
+ harness.runBgShell([forgeBin(), 'shepherd', 'daemon']);
484
+ return { launched: true, via: 'bg-shell' };
485
+ } catch {
486
+ /* fall through to the detached fail-safe */
487
+ }
488
+ }
489
+ const spawnFn = ctx.spawnProcess || spawn;
490
+ try {
491
+ const child = spawnFn(
492
+ process.execPath,
493
+ [forgeBin(), 'shepherd', 'daemon'],
494
+ { cwd: ctx.projectRoot, detached: true, stdio: 'ignore', windowsHide: true },
495
+ );
496
+ if (child && typeof child.on === 'function') child.on('error', () => {});
497
+ if (child && typeof child.unref === 'function') child.unref();
498
+ return { launched: true, via: 'detached', pid: child && child.pid != null ? child.pid : null };
499
+ } catch {
500
+ return { launched: false };
501
+ }
502
+ }
503
+
504
+ /**
505
+ * Build a live, migrated kernel broker (+ its owned driver) for the daemon. Uses the
506
+ * same createLocalBroker-backed factory the CLI uses, so listOpenPrs/upsertPr/retirePr
507
+ * are the real instance methods. The caller closes `driver` on retire.
508
+ */
509
+ async function defaultBuildBroker({ projectRoot, gitCommonDir }) {
510
+ const { buildMigratedKernelIssueDeps } = require('../kernel/cli-broker-factory');
511
+ const deps = await buildMigratedKernelIssueDeps({ projectRoot, gitCommonDir });
512
+ return { broker: deps.kernelBroker, driver: deps.kernelDriver };
513
+ }
514
+
515
+ /**
516
+ * Whether the default-ON `rail.auto_shepherd` gate permits the autonomous trigger.
517
+ * Reuses ship.js's `autoShepherdRailEnabled` — the SAME resolver `forge push`,
518
+ * `forge ship`, and `forge shepherd adopt` honor — so one `forge gate disable
519
+ * rail.auto_shepherd` turns the whole autonomous surface off. Lazy-required to keep
520
+ * the per-command trigger cheap and avoid an eager/circular load; FAIL-OPEN (returns
521
+ * enabled) if the resolver can't be read, and never throws.
522
+ */
523
+ function railAutoShepherdEnabled(projectRoot) {
524
+ try {
525
+ return require('../commands/ship').autoShepherdRailEnabled(projectRoot);
526
+ } catch {
527
+ return true; // config unreadable → fail open (default-ON), never block the trigger's own path
528
+ }
529
+ }
530
+
531
+ /**
532
+ * True iff a kernel DB already exists for `projectRoot` — the SAME no-lazy-create
533
+ * invariant `forge prime` honors (orientation.hasExistingKernelDb). The trigger must
534
+ * CREATE NOTHING in an uninitialized or setup/init TARGET repo (else `setup --dry-run`
535
+ * and `init` would sprout a shepherd.lock and pollute output). SILENT: a no-op `warn`
536
+ * suppresses resolveGitCommonDir's fallback message on a non-git dir. Never throws.
537
+ */
538
+ function kernelInitialized(projectRoot) {
539
+ try {
540
+ // Resolve the git-common-dir with a FAST, SUBPROCESS-FREE read — NEVER `git
541
+ // rev-parse` (its 30s timeout would block every registry command on the dispatch
542
+ // finally, and a git subprocess pollutes bare-repo command tests). Common checkout:
543
+ // <root>/.git is a dir. Linked worktree: <root>/.git is a file `gitdir: …/worktrees/x`
544
+ // whose common dir is the part before `/worktrees/`.
545
+ const gitPath = path.join(projectRoot, '.git');
546
+ const st = fs.statSync(gitPath); // throws if absent → not a repo → false
547
+ let commonDir;
548
+ if (st.isDirectory()) {
549
+ commonDir = gitPath;
550
+ } else {
551
+ const m = /^gitdir:\s*(.+)$/m.exec(fs.readFileSync(gitPath, 'utf8'));
552
+ if (!m) return false;
553
+ const wtGitDir = path.resolve(projectRoot, m[1].trim());
554
+ const marker = `${path.sep}worktrees${path.sep}`;
555
+ const idx = wtGitDir.lastIndexOf(marker);
556
+ commonDir = idx >= 0 ? wtGitDir.slice(0, idx) : wtGitDir;
557
+ }
558
+ return fs.existsSync(path.join(commonDir, 'forge', 'kernel.sqlite'));
559
+ } catch {
560
+ return false;
561
+ }
562
+ }
563
+
564
+ /** Empty enumeration for a cold-tick loser that lost the lease race (backs off). */
565
+ function emptyEnum(gitCommonDir) {
566
+ return {
567
+ desired: { openPrs: [], gitCommonDir },
568
+ observed: { lease: null, leaseFresh: false, prRows: [], liveWatcherPids: [] },
569
+ };
570
+ }
571
+
572
+ /**
573
+ * The per-command / session-start trigger. Runs the `tick()` debounce; the hot
574
+ * path (a fresh daemon lease) short-circuits in-process with a single lock read
575
+ * and no spawn. Only on the cold (G3) path does it ARBITRATE via the O_EXCL lease:
576
+ * the acquire-winner launches the singleton daemon (which does the real
577
+ * `gh pr list` enumeration + converge), and a loser backs off — no spawn. The
578
+ * arbitration lease is released immediately after launch so the spawned daemon can
579
+ * take sole ownership; the daemon's own `acquire` is the final singleton authority,
580
+ * so even a race that double-launches still yields exactly one live daemon.
581
+ *
582
+ * The gh enumeration deliberately lives in the DAEMON, not here, so this trigger
583
+ * NEVER runs a blocking subprocess on the command's critical path.
584
+ *
585
+ * CONTRACT: never throws, never blocks (no await), never affects the command.
586
+ */
587
+ function fireAndForget(ctx = {}) {
588
+ try {
589
+ // Operator kill-switch (agent-agnostic): a set FORGE_SHEPHERD_DISABLE turns the
590
+ // autonomous trigger fully inert — no lease, no enumeration, no daemon spawn.
591
+ if (process.env.FORGE_SHEPHERD_DISABLE) return;
592
+ const projectRoot = ctx.projectRoot;
593
+ if (!projectRoot) return;
594
+ // A dry-run must have ZERO side effects, and the trigger must CREATE NOTHING in an
595
+ // uninitialized / setup-or-init TARGET repo (no-lazy-create invariant, same as prime).
596
+ // Both guards run BEFORE any git/lock touch so `setup --dry-run` / `init` stay
597
+ // side-effect- AND output-clean (kernelInitialized is silent). Checked here, not the
598
+ // caller, so every trigger site (dispatch, session-start) is covered uniformly.
599
+ if (ctx.dryRun) return;
600
+ if (!(ctx.kernelInitialized || kernelInitialized)(projectRoot)) return;
601
+ // Config kill-switch (same gate ship/push/adopt honor): a maintainer who ran
602
+ // `forge gate disable rail.auto_shepherd` gets a fully inert trigger — no lease,
603
+ // no enumeration, no daemon spawn. Cheap + fail-open, inside the dispatch try.
604
+ const railEnabled = ctx.railEnabled || railAutoShepherdEnabled;
605
+ if (!railEnabled(projectRoot)) return;
606
+ let gitCommonDir = ctx.gitCommonDir;
607
+ if (!gitCommonDir) {
608
+ try {
609
+ gitCommonDir = brokerMod.resolveGitCommonDir(projectRoot, { warn: () => {} });
610
+ } catch {
611
+ return;
612
+ }
613
+ }
614
+ const acquire = ctx.acquire || shepherdLease.acquire;
615
+ const release = ctx.release || shepherdLease.release;
616
+ const launch = ctx.launch || launchDaemon;
617
+ const tickFn = ctx.tick || defaultTick;
618
+
619
+ let token = null;
620
+ const enumerate = () => {
621
+ // COLD path only: arbitrate the singleton. The O_EXCL acquire is the atomic
622
+ // arbiter — exactly one concurrent trigger wins; the rest get {ok:false}.
623
+ const res = acquire(projectRoot, { gitCommonDir });
624
+ if (res.ok) token = res.token;
625
+ return emptyEnum(gitCommonDir);
626
+ };
627
+ const execute = () => {
628
+ if (token == null) return; // loser: no daemon launch
629
+ // RELEASE the arbitration lease BEFORE launching so the spawned daemon can
630
+ // acquire it. Holding it during launch races the child's runDaemon().acquire():
631
+ // the child would see a fresh foreign owner and exit, and after we then release,
632
+ // the bumped cold-tick sentinel suppresses re-launch until the next throttle
633
+ // window — leaving NO daemon running.
634
+ const held = token;
635
+ token = null;
636
+ try { release(projectRoot, { gitCommonDir, token: held }); } catch { /* best effort */ }
637
+ try { launch({ ...ctx, projectRoot, gitCommonDir }); } catch { /* best effort */ }
638
+ };
639
+
640
+ tickFn({ gitCommonDir, now: ctx.now, enumerate, execute, minInterval: ctx.minInterval });
641
+ } catch {
642
+ /* NEVER affect the command result or exit code */
643
+ }
644
+ }
645
+
646
+ module.exports = {
647
+ normalizeWatcher,
648
+ writeClaimMarker,
649
+ readClaimMarker,
650
+ gatherDesired,
651
+ gatherObserved,
652
+ verifiedKill,
653
+ execute,
654
+ convergeOnce,
655
+ runDaemon,
656
+ launchDaemon,
657
+ defaultBuildBroker,
658
+ fireAndForget,
659
+ };