@jeffober/thatch 0.2.3 → 0.2.5

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 (54) hide show
  1. package/README.md +8 -1
  2. package/artifacts/skills/thatch-clear-writing.md +103 -5
  3. package/artifacts/skills/thatch-coordination.md +69 -0
  4. package/artifacts/skills/thatch-pr-description.md +21 -13
  5. package/artifacts/skills/thatch-review-no-slop.md +15 -0
  6. package/bin/thatch +95 -44
  7. package/docs/assets/deepwiki-badge.svg +1 -0
  8. package/docs/dev/README.md +7 -2
  9. package/docs/dev/features/cli.md +2 -0
  10. package/docs/dev/features/cross-session-chat.md +118 -11
  11. package/docs/dev/features/notifications.md +1 -2
  12. package/docs/dev/features/opencode-plugin.md +18 -3
  13. package/docs/dev/features/prediction-engine.md +44 -5
  14. package/docs/dev/features/session-tabs.md +155 -0
  15. package/docs/dev/features/setup.md +12 -0
  16. package/docs/dev/features/watchers.md +62 -6
  17. package/docs/dev/gotchas.md +68 -0
  18. package/docs/dev/mcp-parity.md +2 -2
  19. package/docs/dev/setup-and-hooks.md +7 -2
  20. package/docs/dev/skills.md +1 -1
  21. package/docs/plans/generalized-session-heartbeat.md +212 -0
  22. package/docs/plans/prediction-consolidation.md +88 -68
  23. package/docs/user/README.md +12 -3
  24. package/docs/user/cli.md +5 -1
  25. package/docs/user/commands.md +11 -4
  26. package/docs/user/cross-session-chat.md +31 -8
  27. package/docs/user/prediction-engine.md +11 -0
  28. package/docs/user/setup.md +29 -0
  29. package/docs/user/skills.md +1 -1
  30. package/docs/user/watchers.md +22 -7
  31. package/package.json +10 -2
  32. package/src/alerts.ts +1 -1
  33. package/src/chat.ts +320 -27
  34. package/src/commands.ts +14 -0
  35. package/src/db.ts +51 -1
  36. package/src/git.ts +3 -1
  37. package/src/hygiene.ts +5 -0
  38. package/src/mcp.ts +95 -7
  39. package/src/opencode/tui-plugin.ts +70 -0
  40. package/src/opencode/v2.ts +197 -4
  41. package/src/prediction.ts +90 -0
  42. package/src/prompts.ts +110 -23
  43. package/src/runtime.ts +187 -9
  44. package/src/session-db.ts +4 -4
  45. package/src/session-tab-shared.ts +180 -0
  46. package/src/setup.ts +59 -3
  47. package/src/skills.ts +1 -0
  48. package/src/tool-defs.ts +412 -55
  49. package/src/tools.ts +2 -0
  50. package/src/watchers.ts +263 -26
  51. package/docs/plans/intuition-drives.md +0 -147
  52. package/docs/plans/llm-alerts.md +0 -287
  53. package/docs/plans/thatch-actions.md +0 -179
  54. package/docs/plans/worktree-deletion-recovery.md +0 -246
@@ -88,9 +88,15 @@ A `setInterval` loop (unref'd - it never keeps the process alive)
88
88
  runs `poll()` every 60 seconds (configurable). Each cycle:
89
89
  fetch the current state, run `diffPrState()` against the watcher's
90
90
  last-seen state, filter events to the watched types, queue them, and
91
- attempt delivery. Poll errors are per-watcher; one bad PR never
92
- blocks the others. The diff is a pure function, so it is unit-tested
93
- without any network access.
91
+ attempt delivery. When the fetched snapshot differs from the last-seen
92
+ state, the cycle also rewrites the session's journal row: registry
93
+ rebuilds (v2 reload rehydration, the watch tools' journal reconcile)
94
+ hydrate the journaled state verbatim, so a journal that lags the
95
+ poller would make the next diff re-fire already-delivered events -
96
+ observed live in October 2026 as a watch re-delivering the same CI
97
+ completion on every reload. Poll errors are per-watcher; one bad PR
98
+ never blocks the others. The diff is a pure function, so it is
99
+ unit-tested without any network access.
94
100
 
95
101
  The interval is surfaced to the model through the registry's
96
102
  `pollSeconds` getter: watch-creation output states when the first
@@ -126,6 +132,41 @@ when this run finishes" request from leaving a standing watch polling
126
132
  a target nobody is waiting on. `watch_list` marks one-shot watchers
127
133
  with a `[once]` tag, and the registration output states the mode.
128
134
 
135
+ ### Expiry
136
+
137
+ A watch whose TTL passes is dropped at the next poll cycle, and the
138
+ owning session is told: a `watch_expired` event queues through the
139
+ normal pending path, naming the target and the watch's lifetime in
140
+ minutes. The nudge framing for an all-expiry delivery states that the
141
+ watched condition never occurred - deliberately without the
142
+ greenlit-gating carve-out, because "the watch ran out of clock" must
143
+ never read as "the wait is over". Watches that expire while a session
144
+ is away are a separate path: they are not re-armed, and the re-arm
145
+ notice reports the expired count.
146
+
147
+ ### Death detection (closed v2 tabs)
148
+
149
+ A closed opencode v2 tab fires no `session.deleted`, so its watchers
150
+ would poll until the TTL with deliveries queued forever. The registry
151
+ declares death when ALL of: the session's pending queue has been
152
+ non-empty longer than the threshold (`THATCH_WATCH_DEATH_MINUTES`,
153
+ default 120), its delivery attempts have THROWN consecutively (a skip -
154
+ busy or compacting - resets the chain; skips are never death evidence),
155
+ the throw is 404-class (verified live 2026-10-07: prompt_async to a
156
+ closed v2 tab returns 404 while the session row persists; a 5xx loop is
157
+ a wedged server, never death), and the session is hosted by this
158
+ process (sibling v1 processes and MCP sessions are never touched).
159
+ MCP-owner watches keep TTL-only death: their turn-driven heartbeat
160
+ cannot distinguish a closed Cursor tab from a parked one. On death:
161
+ watchers and pending drop, and live same-project sessions get a
162
+ `watcherDeathNotice` carrying the dead session's chat name and session
163
+ id (the thatch-coordination loop's respawn key); a durable
164
+ `watcher_death` runtime_state row covers sessions that were busy at
165
+ death time, surfaced and cleared at other sessions' next prompt. The
166
+ notice says the watches were CANCELLED and points at `watch_create` -
167
+ the re-arm-on-resume promise belongs only to the restart-dormant path,
168
+ where the definitions survive and resuming the session re-arms them.
169
+
129
170
  ### Command watches
130
171
 
131
172
  The command source stretches the fetch/diff recipe in two ways:
@@ -180,6 +221,11 @@ Events queue in an in-memory pending map keyed by session. Delivery
180
221
  is gated by a `canDeliver` predicate the plugin supplies: the session
181
222
  must be idle (tracked from `session.status` events) and not
182
223
  compacting. Proactive prompts are never injected into a running turn.
224
+ The queue is visible to the session itself: `watch_list` appends an
225
+ "N events detected, waiting for idle to deliver" line with a
226
+ per-target breakdown while events sit undelivered, so a busy session
227
+ with queued events is distinguishable from a watcher that fired
228
+ nothing.
183
229
 
184
230
  When the gate is open, the plugin calls `client.session.promptAsync`
185
231
  on the session with a synthetic text part built by
@@ -206,9 +252,11 @@ cycle or when the session next goes idle (the event hook calls
206
252
  `pr_review_resolved`, `pr_commit`, `pr_status`, `pr_description`,
207
253
  `pr_ci`. The tool's `events` argument selects a subset; the default is
208
254
  all eight. Branch watchers add `branch_commit`, `branch_ci`, and
209
- `branch_workflow` (the full vocabulary is `WATCHER_EVENT_TYPES` in
210
- src/watchers.ts, test-enforced); command watchers register the single
211
- `command_success` type.
255
+ `branch_workflow`; command watchers register the single
256
+ `command_success` type. The registry itself emits one lifecycle type
257
+ across all sources, `watch_expired`, which is not selectable via the
258
+ `events` argument (the full vocabulary is `WATCHER_EVENT_TYPES` in
259
+ src/watchers.ts, test-enforced).
212
260
 
213
261
  ## Interactions with other features
214
262
 
@@ -229,6 +277,8 @@ src/watchers.ts, test-enforced); command watchers register the single
229
277
  - `THATCH_WATCH_TTL_MINUTES` - watcher time-to-live (default 480)
230
278
  - `THATCH_WATCH_MAX_PER_SESSION` - active watchers per session
231
279
  (default 5; all sources share the budget)
280
+ - `THATCH_WATCH_DEATH_MINUTES` - pending-age threshold before a dead
281
+ session's watchers are cancelled and announced (default 120)
232
282
  - `THATCH_WATCH_COMMAND_TIMEOUT_SECONDS` - per-run kill timeout for
233
283
  watched commands (default 30)
234
284
 
@@ -248,6 +298,12 @@ src/watchers.ts, test-enforced); command watchers register the single
248
298
  mocked gh runner
249
299
  - `tests/qa/auto/uc-101-command-watchers.ts` - command-watch lifecycle
250
300
  against a mocked command runner
301
+ - `tests/qa/auto/uc-114-watcher-expiry-replay.ts` - expiry notification
302
+ and the no-replay guarantee across journal rehydration
303
+ - `tests/qa/auto/uc-118-watcher-death-detection.ts` - closed-tab death
304
+ detection (404-class delivery persistence) and the once-only notice
305
+ - `tests/qa/auto/uc-115-watch-list-pending-events.ts` - pending-event
306
+ visibility in watch_list while the session is busy
251
307
 
252
308
  ## Adding a new source type
253
309
 
@@ -293,3 +293,71 @@ instead of throwing "Cannot use a closed database". Two rules keep this intact:
293
293
  renderer quotes every description (`yamlQuote` in src/commands.ts) and a
294
294
  unit test pins the quoted form. Keep it that way for any new frontmatter
295
295
  field whose value is free-form prose.
296
+
297
+ ## In-memory maps gate nothing durable: reloads and restarts wipe them
298
+
299
+ - **Any guard whose correctness depends on a runtime.ts Map silently
300
+ disappears for sessions whose events arrive after a plugin reload or a
301
+ daemon restart.** The chat auto-registration child check learned this the
302
+ hard way: `childToParent` kept extraction children out of the roster, but
303
+ an instance that lost the map to a reload registered every event-bearing
304
+ child, and v2 cannot delete sessions (sessionDelete is a no-op), so the
305
+ poller heartbeat those corpse rows forever and the stale-row reaper never
306
+ reaped them (81 `thatch-extraction` rows by 2026-10-07). The fix pattern:
307
+ the plugin writes a DURABLE marker at creation time
308
+ (`chat_machinery`, src/db.ts), and every registration path checks the
309
+ marker plus a stable title (`isMachinerySessionTitle`) instead of
310
+ trusting in-memory state alone. If you add a new plugin-created session
311
+ kind, mark it in the same table and route its events through the same
312
+ guards - do not rely on Maps surviving a reload.
313
+
314
+ ## A running TUI never adopts a NEW ./tui plugin entrypoint - restart the TUI
315
+
316
+ - **The v2 TUI discovers its CLI plugins at process startup only.** The
317
+ server side hot-reloads plugin code (the digest watcher), and the TUI
318
+ hot-reloads the SOURCE of plugins it already loaded - but a plugin that
319
+ GAINS a `./tui` entrypoint (or a fresh install of one) is a discovery-
320
+ level change: a TUI process started before the entrypoint existed never
321
+ loads it. Symptom (2026-10-08, the thog TUI): `thatch_session_tab` ran
322
+ fine server-side (the session, the chat registration, the response all
323
+ real) but the tab never appeared in the strip - the TUI had no
324
+ `ui.tabs` consumer loaded, so the rpc event landed nowhere. The
325
+ persisted `tabs.json` (in `$XDG_STATE_HOME/opencode/<channel>/tui/`)
326
+ proved the registration logic was correct the whole time: a FRESH TUI
327
+ (the docker sandbox attach) showed the tab immediately.
328
+ - **Fix**: convert the install to the directory shim (docs/user/setup.md),
329
+ THEN restart the TUI. A restart ALONE fixes nothing when the install is
330
+ a FILE shim - the file shape never loads a `./tui` entrypoint, whatever
331
+ the process age. First diagnostic: `ls ~/.config/opencode/plugins/` -
332
+ a FILE (`thatch.ts`) means no TUI consumer has ever loaded there,
333
+ restart or not; the directory form (`thatch/index.ts` + `tui.ts`) is
334
+ required. (2026-10-08, the thog TUI: the restart-and-respawn test
335
+ DISPROVED the stale-process theory and pointed here - the conversion
336
+ was the missing install step.)
337
+ - **Fix**: restart the TUI window once after adopting (or changing the
338
+ shape of) a `./tui` entrypoint. DIAGNOSTIC WARNING: `tabs.json` cannot
339
+ discriminate here - it is SHARED across windows and written by ANY tab
340
+ open, manual (a picker navigation) or event-driven - an entry for the
341
+ session proves only that SOMETHING opened it, not that the spawn-time
342
+ event worked. The discriminating test: restart the TUI and re-spawn; a
343
+ tab appearing post-restart confirms the stale-process diagnosis; a tab
344
+ still absent is a real bug worth a report.
345
+
346
+ ## The two opencode lines discover plugins differently: no one shim shape auto-loads on both
347
+
348
+ - **v1 auto-discovers plugin FILES only** - its config plugin scan globs
349
+ `{plugin,plugins}/*.{ts,js}`, which never matches a directory. **v2
350
+ auto-discovers files AND directories** (and symlinks), and its activation
351
+ dies on duplicate ids - so a plugins directory holding BOTH `thatch.ts`
352
+ and `thatch/` loads the server plugin twice on v2 and dies with
353
+ "duplicate instance plugin ids" before any plugin code runs. There is no
354
+ single on-disk shape that auto-loads on both lines as a directory-based
355
+ multi-entry plugin. Consequences: the dev-checkout install uses the file
356
+ shim for v1-only or v2-only use, or the directory shim for v2's `./tui`
357
+ entrypoint (session tabs) - deleting the file shim in the same step; the
358
+ QA fixture keeps the FILE shim because the QA suite is headless (no TUI
359
+ process ever loads `./tui`, and the file shape is the only one that loads
360
+ on both lines - a directory shim deterministically killed UC-100's v1
361
+ chat canary). Local plugin specs handed to a loader explicitly
362
+ (`"plugin": ["./path"]`) can point at a directory on v1 - it is only the
363
+ AUTO-discovery scan that is files-only.
@@ -18,7 +18,7 @@ This document maps the feature parity and documents the remaining gaps.
18
18
 
19
19
  | Feature | OpenCode plugin | Claude Code MCP + hooks | Cursor MCP + hooks | Parity |
20
20
  |---------|----------------|------------------------|---------------------|--------|
21
- | **Tools** (single source of truth) | Plugin tool registration, `thatch_` prefix | MCP `tools/list` + `tools/call`, `mcp__thatch__` prefix (opencode-only tools filtered out) | MCP `tools/list` + `tools/call`, `mcp__thatch__` prefix (opencode-only tools filtered out) | **Full** for the shared tools; `get_session_info` (session identity) and `session_search`/`session_get` (conversation archaeology) are opencode-only — MCP hosts have no session concept. The `thatch session` CLI subcommands cover the same archaeology on any host with an opencode.db file. The `watch_*` tools (PR, branch, and command watchers) are opencode-only — MCP hosts have no poller or proactive-prompt channel. The `chat_*` tools (cross-session chat) are opencode-only — no host session identity and no wake-up channel |
21
+ | **Tools** (single source of truth) | Plugin tool registration, `thatch_` prefix | MCP `tools/list` + `tools/call`, `mcp__thatch__` prefix (opencode-only and v2-only tools filtered out) | MCP `tools/list` + `tools/call`, `mcp__thatch__` prefix (opencode-only and v2-only tools filtered out) | **Full** for the shared tools; `get_session_info` (session identity) and `session_search`/`session_get` (conversation archaeology) are opencode-only — MCP hosts have no session concept. The `thatch session` CLI subcommands cover the same archaeology on any host with an opencode.db file. The `watch_*` tools (PR, branch, and command watchers) are opencode-only — MCP hosts have no poller or proactive-prompt channel. The `chat_*` tools (cross-session chat) are opencode-only — no host session identity and no wake-up channel. `session_tab` and `session_tab_close` are v2-opencode-only — they drive TUI session tabs over the v2 rpc-event bridge |
22
22
  | **System prompt** (store names, usage rules) | `experimental.chat.system.transform` — dynamic, repo baked in at runtime | CLAUDE.md static text appended by `thatch setup`; repo auto-detected by the MCP server at startup | AGENTS.md static text appended by `thatch setup`; repo auto-detected at startup | **Approximate** — static text persists through compaction, so the agent retains the usage instructions, but the dynamic per-turn refresh is lost |
23
23
  | **Session-start reminder** (recall nudge + hygiene heartbeat) | `session.created` event → `client.session.prompt` injects a synthetic message | `SessionStart` hook → `thatch reminder`; stdout becomes context | `sessionStart` hook → `thatch reminder --json`; output is `additional_context` JSON | **Full** |
24
24
  | **Compaction context** (re-familiarize after compaction) | `experimental.session.compacting` hook appends context to the compaction output | `PostCompact` hook — side-effects only, **cannot inject context** | No equivalent hook | **Gapped** — see below |
@@ -31,7 +31,7 @@ This document maps the feature parity and documents the remaining gaps.
31
31
  | **Prediction auto-fire** (user decision model) | `chat.message` hook scores the prompt embedding against prediction matchers; injects a `[thatch] User decision model` nudge alongside the recall nudge (separate synthetic part, same embedding call) | `UserPromptSubmit` hook → `thatch flush-tools` fires prediction query via the sideband socket's `predictions` method in parallel with the recall nudge | `beforeSubmitPrompt` hook → `thatch flush-tools --json` (same sideband path) | **Full** — same `scorePredictionNudge` entry point in both paths |
32
32
  | **Behavior auto-fire** (self-discipline rules) | `chat.message` hook scores the prompt embedding against behavior matchers; injects a `[thatch] Situational behaviors` nudge (separate synthetic part, same embedding call) | `UserPromptSubmit` hook → `thatch flush-tools` fires behavior query via the sideband socket's `behaviors` method in parallel with recall and predictions | `beforeSubmitPrompt` hook → `thatch flush-tools --json` (same sideband path) | **Full** — same `scoreBehaviorNudge` entry point in both paths |
33
33
  | **Skills** | Installed to `$XDG_CONFIG_HOME/opencode/skills` at plugin init (shared **+** opencode-only) | Installed by `thatch setup` (shared only) to the repo's `.claude/skills/` (local) or `$CLAUDE_CONFIG_DIR/skills/` (global) | Installed by `thatch setup` (shared only) to the repo's `.cursor/skills/` (local) or `~/.cursor/skills/` (global) | **Full** — same SKILL.md format; the code-review coordinator is opencode-only (needs sub-agents) |
34
- | **Store detection** (repo identity from git remote) | `worktree` parameter from the opencode plugin | `CLAUDE_PROJECT_DIR` env (set by Claude Code for stdio MCP servers) | `CURSOR_PROJECT_DIR` then `CLAUDE_PROJECT_DIR` then cwd | **Full** |
34
+ | **Store detection** (repo identity from git remote) | `worktree` parameter from the opencode plugin | `THATCH_PROJECT_DIR` (pinned by project-local setup) → `CLAUDE_PROJECT_DIR` env → cwd | `THATCH_PROJECT_DIR` (pinned by project-local setup) → `CURSOR_PROJECT_DIR` then `CLAUDE_PROJECT_DIR` then cwd | **Full** |
35
35
  | **Setup detection at startup** | n/a — plugin auto-installs at init | `checkSetup` in MCP server: detects missing or broken instructions in CLAUDE.md | Same as Claude Code (checks AGENTS.md) | **Full** — warns the agent to tell the user to run `thatch setup` |
36
36
  | **Hook config format** | n/a — plugin registers hooks in code | Nested: `.claude/settings.json` `{hooks:{Event:{hooks:[{type,command}]}}}` | Flat: `.cursor/hooks.json` `{version:1,hooks:{event:[{command}]}}` | n/a |
37
37
 
@@ -80,7 +80,7 @@ project and every contributor gets them).
80
80
  |-------|---------|--------|------|
81
81
  | `SessionStart` | `thatch reminder` | plain text to stdout (becomes context) | Recall instructions + hygiene heartbeat + chat identity/mail line (reads the hook's stdin `session_id`) |
82
82
  | `PostToolBatch` | `thatch buffer-batch` | **silent** (no stdout) | Appends a batch of tool calls to the file-backed JSONL queue |
83
- | `UserPromptSubmit` | `thatch flush-tools` | nudge text to stdout | Peeks queue (extraction nudge), else fires recall, prediction, and behavior nudges via sideband in parallel, else write nudge |
83
+ | `UserPromptSubmit` | `thatch flush-tools` | nudge text to stdout | Peeks queue (extraction nudge; degraded to a restart instruction under version skew - the old server may lack the referenced tools), else fires recall, prediction, and behavior nudges via sideband in parallel, else write nudge |
84
84
  | `Stop` | `thatch chat-notify` | `{ hookSpecificOutput: { additionalContext } }` when chat mail is unread, else `{}` | Post-turn chat wake: the turn continues so the model reads its mail (Claude Code's analog of opencode's poller wake); host loop protections (`stop_hook_active`, 8-consecutive cap) plus the delivered stamp bound repeats |
85
85
 
86
86
  `PostToolBatch` is silent so the agent loop is not delayed; the buffered
@@ -122,7 +122,7 @@ is honored for symmetry and forward-compatibility. Cursor has no equivalent of
122
122
  |-------|---------|--------|------|
123
123
  | `sessionStart` | `thatch reminder --json` | `{ additional_context: "..." }` | Recall + heartbeat + chat identity/mail line, JSON-wrapped for Cursor |
124
124
  | `postToolUse` | `thatch buffer-tool` | **silent** | Appends a **single** tool call to the file-backed queue |
125
- | `beforeSubmitPrompt` | `thatch flush-tools --json` | JSON `additional_context` | Peeks queue, else recall, prediction, and behavior via sideband, else write nudge |
125
+ | `beforeSubmitPrompt` | `thatch flush-tools --json` | JSON `additional_context` | Peeks queue (extraction nudge; degraded to a restart instruction under version skew), else recall, prediction, and behavior via sideband, else write nudge |
126
126
  | `stop` | `thatch chat-notify` | `{ followup_message }` or `{}` | Post-turn chat wake: when chat mail is unread, Cursor auto-submits the follow-up as the next user message (Cursor's analog of opencode's poller wake); `loop_limit: 3` bounds consecutive follow-ups |
127
127
 
128
128
  Differences from Claude Code:
@@ -179,6 +179,11 @@ The host is determined from env vars set by the host process:
179
179
 
180
180
  Cursor takes priority because Cursor also sets `CLAUDE_PROJECT_DIR` as an alias.
181
181
 
182
+ Separately from host detection, the project directory used for store/chat
183
+ identity resolution resolves as `THATCH_PROJECT_DIR ?? CURSOR_PROJECT_DIR
184
+ ?? CLAUDE_PROJECT_DIR ?? cwd` in the MCP server; project-local `thatch
185
+ setup` runs pin `THATCH_PROJECT_DIR` into the written MCP config.
186
+
182
187
  ### What it checks
183
188
 
184
189
  `checkSetup` looks for the instruction markers (start and end) in the host's
@@ -69,7 +69,7 @@ description: Extract durable project facts ... Use when ...
69
69
  | `thatch-review-response` | Author-side review response: triage findings, fix bugs one by one, reply on each thread, post a top-level summary comment. |
70
70
  | `thatch-memory-verify` | Fact-check a single memory against the current codebase and correct stale claims. Uses git archaeology to preserve historical context when changes were intentional. |
71
71
  | `thatch-knowledge-export` | Compile everything thatch knows about a topic into a curated markdown file for knowledge transfer. Searches across stores, curates out personal noise, fact-checks code-related memories via `thatch-memory-verify`. |
72
- | `thatch-clear-writing` | Prose rules for any human-facing text not covered by a dedicated writing skill: PR and ticket comments, documentation, plans, reports, and chat replies. Clarity over compression; dedicated skills win when loaded. |
72
+ | `thatch-clear-writing` | Prose rules for any human-facing text not covered by a dedicated writing skill: PR and ticket comments, documentation, plans, reports, chat replies, and code comments. Clarity over compression; dedicated skills win when loaded. |
73
73
 
74
74
  **opencode-only** — the coordinator needs sub-agent support:
75
75
 
@@ -0,0 +1,212 @@
1
+ # Plan: watcher death detection for closed v2 tabs
2
+
3
+ Status: v3.1 - empirical premise VERIFIED live (2026-10-07: prompt_async
4
+ to a closed v2 tab returns 404; no silent success; the session row
5
+ persists, so the pre-cancel re-check keys on the throw class). The
6
+ build's gate is open. Two refinement rounds converged (round 2: BUILD,
7
+ no blockers).
8
+
9
+ ## Problem
10
+
11
+ v2 tab close fires no `session.deleted`, so the closed tab's entry in
12
+ the runtime's `sessionStatus` map (src/runtime.ts:217-222, keys "leave
13
+ only in session.deleted") never leaves, the chat poller keeps hosting
14
+ and heartbeat-ing the dead tab's row (src/runtime.ts:363-368,
15
+ src/chat.ts:879-883), and its watchers poll until the 8h TTL with
16
+ deliveries queuing forever. The session.deleted handler itself names
17
+ the failure mode: "a leaked status key would keep heartbeat-ing a dead
18
+ session as permanently fresh" (src/runtime.ts:1891).
19
+
20
+ ## Rejected: heartbeat-staleness death detection (the original plan)
21
+
22
+ The original design gated the heartbeat stamp on `fetchStatuses`
23
+ presence so a closed tab would age out. Round 1 proved the premise
24
+ inverted: the live status map carries only ACTIVE (mid-turn) sessions -
25
+ the host deletes idle entries (thatch's own wake-gate contract,
26
+ src/chat.ts:366-368; host source session/status.ts:42-46) - so absence
27
+ means "idle OR closed". Gating stamps on presence would mark every
28
+ idle-open tab stale in 60s (regressing e486148, whose grace exists
29
+ precisely because idle-open tabs emit nothing) and death-cancel the
30
+ watchers of every idle session. And on v2 - the only host with the bug
31
+ - `fetchStatuses` is a permanent `{}` stub (src/opencode/v2.ts:524;
32
+ "session.status exists in v2's schema but nothing publishes it",
33
+ v2.ts:417), so the mechanism does not exist where the bug lives.
34
+ Chat-off sessions (chatPoller.start gated on chatOn,
35
+ src/runtime.ts:407-409) get no stamps at all - a third false-death
36
+ path.
37
+
38
+ Conclusion: the heartbeat cannot carry tab-death detection on v2. The
39
+ heartbeat keeps its current role (roster liveness for chat, unconditional
40
+ stamps for hosted sessions); death detection needs a different clock.
41
+
42
+ ## New design: delivery-persistence death detection
43
+
44
+ The signal that DOES distinguish a closed v2 tab from a live one, with
45
+ no host API: **what happens to a pending watcher event.**
46
+
47
+ - A live v2 session with queued events consumes them: the delivery gate
48
+ opens (on v2 the stub map reads as idle) and `promptAsync` succeeds -
49
+ there is no busy-rejection on the v2 prompt path. Queues drain.
50
+ - A closed v2 session: every `deliver()` attempt throws (the session no
51
+ longer exists server-side) and the queue retries forever
52
+ (src/watchers.ts:1296-1298 - failed delivery stays pending).
53
+
54
+ So: **death = a session's pending watcher queue has been non-empty for
55
+ longer than the threshold AND recent delivery attempts have THROWN**
56
+ (not been skipped - a skipped attempt is a busy/compacting session and
57
+ must never count). The heartbeat is untouched; this is one extra clock
58
+ on one queue, per session.
59
+
60
+ - Threshold: 2 hours default (env `THATCH_WATCH_DEATH_MINUTES`). Far
61
+ above any legitimate busy turn; far below the 8h TTL, which remains
62
+ the hard backstop. The failure counter counts CONSECUTIVE thrown
63
+ deliveries per session (side map keyed by session id - WatcherEvent
64
+ carries no timestamp); a SKIPPED attempt (busy/compacting) or a
65
+ success RESETS it - skips are never death evidence.
66
+ - Pre-cancel re-check (round 2): when the threshold fires, re-verify
67
+ the session via `caps.sessionGet` before cancelling - only a
68
+ not-found-class result confirms death. A wedged server or 500-loop
69
+ throws exactly like a dead session does, and without the re-check a
70
+ two-hour API outage would mass-cancel every hosted watcher.
71
+ - v1 is out of scope: v1 fires session.deleted on tab close (the
72
+ known-gaps table), so v1 tabs already clean up; crashed v1 daemons
73
+ are covered by the dormant-row machinery.
74
+ - On death: cancel the session's watchers (watchers.cancelSession -
75
+ also drops the pending queue, src/watchers.ts:1195-1201), emit a
76
+ death notice via the existing promptAsync path to other live
77
+ same-project sessions (`watcherDeathNotice`, src/prompts.ts:1051;
78
+ dedup per dead session like notifiedWatcherDeaths,
79
+ src/runtime.ts:801-806), and persist a `watcher_death` runtime_state
80
+ row (generic runtimeStatePut/All/Delete - no new db helpers).
81
+ - Reader for the persisted row (round 2): the rehydration switch
82
+ (src/runtime.ts:609-713) must learn the `watcher_death` kind
83
+ (foreign-pid rows KEPT like dormant watcher rows, not deleted) and
84
+ the dormant-scan path (src/runtime.ts:779-825) surfaces pending
85
+ death rows to the scanning session, deduping against
86
+ `notifiedWatcherDeaths`, deleting the row on surfacing. Without this
87
+ the persistence half is write-only: unknown foreign-pid kinds are
88
+ deleted on restart (:615-641) and `dormantScanned` runs once per
89
+ session per process.
90
+ - Scope guard: only sessions this process HOSTS via the
91
+ `hostedSessionIds` helper (src/runtime.ts:363-370) are eligible -
92
+ sibling v1 processes and MCP sessions are never touched.
93
+ - Residual case (round 2, documented): a tab closed MID-TURN whose
94
+ host never emits a terminal execution event keeps its mapped status
95
+ `busy` forever - deliveries skip forever, death never fires, TTL
96
+ covers it. Accepted; the notice will not fire for that shape.
97
+ - The roster zombie (a closed tab's row shows Active for days; the
98
+ 7-day auto-reaper bounds auto rows) is NOT fixed by this plan - no v2-
99
+ reachable signal distinguishes it. Documented as a known limitation;
100
+ the reviewer confirmed no last_seen consumer treats staleness as a
101
+ trigger today, so the honest heartbeat stays unconditional.
102
+ - Death notice payload (session-tab coordination, wanda-warpdrive-00009
103
+ 2026-10-07): the notice carries the dead session's CHAT NAME and
104
+ session id, not just the watch targets, so a coordinator session
105
+ running the thatch-coordination skill can mark its task-list entry
106
+ failed and respawn keyed by chat name, no SDK lookup.
107
+
108
+ ### Confirmed-close fast path (integration with session-tab)
109
+
110
+ The session-tab tool (docs/dev/features/session-tabs.md; merged to main
111
+ as 5abb381) emits `rpc.thatch-tabs.tab-closed {sessionID,
112
+ chatName?}` from its TUI plugin when a close is TOOL-initiated (shared
113
+ module session-tab-shared.ts; user-initiated closes stay out of scope
114
+ there - the delivery-persistence heuristic covers those). When the
115
+ event lands for a session with watchers, treat it as a CONFIRMED
116
+ close: skip the 2h heuristic entirely and run the death path
117
+ immediately. The rpc definition is imported from session-tab-shared
118
+ (same package). Fallback layering: confirmed close (instant) >
119
+ delivery-persistence heuristic (2h) > TTL (8h).
120
+
121
+ ## Empirical premise: VERIFIED (2026-10-07, live probe via the session-tab smoke flow)
122
+
123
+ Jeff closed a real v2 tab (session chatting as rosie-unit-one-00007,
124
+ session ses_ee7ec0baeffeziwE83AT2mgLxI) with a command watch armed
125
+ before the close. Probe results against the serve daemon's /api surface
126
+ (Basic auth: username `opencode`, password from
127
+ ~/.config/opencode/service.json; unprefixed /session/* paths are the
128
+ TUI SPA fallback - 200 HTML for anything, do not trust them):
129
+
130
+ - POST /api/session/{id}/prompt_async -> HTTP **404** (clean, no body):
131
+ the delivery throw is real and 404-class.
132
+ - Message count in the closed session before/after: 0 / 0 - **no silent
133
+ success, no headless turn**.
134
+ - GET /api/session/{id} -> 200 with the full record: the session ROW
135
+ persists server-side after tab close, so existence checks cannot
136
+ confirm death.
137
+
138
+ Consequence for design point 2's pre-cancel re-check: key on the throw
139
+ CLASS (404 = route/session gone) not on sessionGet existence - a
140
+ sessionGet 200 is expected even for dead tabs. Wedged-server 5xx loops
141
+ still do not count as death; only 404-class delivery throws do.
142
+
143
+ Location-scoping nuance (wanda-warpdrive-00009, post-probe): the v2
144
+ prompt route is session-location-middleware'd, so the probe's
145
+ cross-location 404 may encode location scoping rather than closed-ness
146
+ per se. The death signal keys on the HOSTING poller's own deliveries
147
+ throwing - same-location throws are the evidence that counts, and the
148
+ session-tab `tab-closed` event papers over the ambiguous cases. The
149
+ pre-cancel re-check and the threshold both run against the hosting
150
+ instance's observed throw class.
151
+
152
+ ## Surfaces touched
153
+
154
+ ### Pending-event durability (from the 2026-10-07 reload-loss incident)
155
+
156
+ A reload between DETECTION and DELIVERY silently eats notifications:
157
+ pending events live only in the in-memory `#pending` queue, and one-shot
158
+ watchers delete their journal row at detection time ("ends at first
159
+ detection, not first delivery") - so the durability layer is gone
160
+ exactly when undelivered events exist (observed: watch_qn449jio's CI
161
+ notification lost in the 09b805f reload window). Fix folded into this
162
+ build: journal PENDING EVENTS per session (alongside the watcher
163
+ definitions), delete them on successful delivery - which the death
164
+ detection needs anyway for its pending-age signal to survive reloads.
165
+ (Found by robot-devil-00012's code-verified analysis; my own watcher
166
+ was not one-shot, so its exact deletion path stays unproven until a
167
+ THATCH_DEBUG=1 repro.)
168
+
169
+ ## Surfaces touched
170
+
171
+ - `src/watchers.ts` - per-session delivery-failure tracking (consecutive
172
+ throws + oldest-pending age) and a death-scan hook the poller calls.
173
+ - `src/runtime.ts` - wire the death callback: notice delivery to live
174
+ same-project sessions, the `watcher_death` runtime_state row, dedup.
175
+ - `src/db.ts` - runtime_state row helpers for the death record.
176
+ - `tests/watchers.test.ts` - death detection (throwing deliver + old
177
+ pending vs skipped-deliver + old pending), threshold env, MCP-owner
178
+ exclusion.
179
+ - `tests/qa/auto/uc-118-watcher-death-detection.ts` (new,
180
+ barrel-imported).
181
+ - Docs: `docs/user/watchers.md` (death notices), `docs/dev/features/
182
+ watchers.md` (detection subsection), `docs/dev/features/
183
+ opencode-plugin.md` known-gaps row (bounded by death detection),
184
+ `docs/user/cli.md` + `docs/dev/features/cli.md` (new env var).
185
+
186
+ ## Acceptance criteria
187
+
188
+ 1. A closed v2 tab's watchers are cancelled within the threshold once
189
+ deliveries have been failing; a live same-project session hears the
190
+ death notice.
191
+ 2. A busy or idle LIVE session never triggers death (skipped delivery
192
+ attempts never count; successful delivery drains the queue).
193
+ 3. A transient delivery failure (< threshold) cancels nothing.
194
+ 4. MCP-owner and non-hosted sessions are never touched.
195
+ 5. `mise run check` + `mise run qa-auto` green.
196
+
197
+ ## Review rounds
198
+
199
+ - Round 1 (fresh reviewer): DO-NOT-BUILD on the heartbeat mechanism -
200
+ inverted premise (status absence means idle, not dead; v2 stub
201
+ returns {}), chat-off and sleep false-death paths, dormantScanned
202
+ once-per-session guard, incomplete death cleanup. The revision
203
+ replaces the mechanism with delivery-persistence detection
204
+ (v2-scoped) and drops the roster-zombie claim.
205
+ - Round 2 (fresh reviewer): BUILD conditional on the empirical probe;
206
+ all premises verified (thrown deliveries stay pending at
207
+ src/watchers.ts:1344-1349, live sessions drain at runtime.ts:1866-
208
+ 1874), with three required adjustments folded in: the death-row
209
+ reader + rehydration handling, the pre-cancel existence re-check,
210
+ and the known-gaps doc correction. Consensus to build - AFTER the
211
+ probe.
212
+