instar 1.0.13 → 1.1.1

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 (70) hide show
  1. package/README.md +1 -1
  2. package/dist/cli.js +52 -2
  3. package/dist/cli.js.map +1 -1
  4. package/dist/commands/init.d.ts +17 -0
  5. package/dist/commands/init.d.ts.map +1 -1
  6. package/dist/commands/init.js +156 -64
  7. package/dist/commands/init.js.map +1 -1
  8. package/dist/commands/server.d.ts.map +1 -1
  9. package/dist/commands/server.js +68 -4
  10. package/dist/commands/server.js.map +1 -1
  11. package/dist/commands/setup.d.ts +3 -1
  12. package/dist/commands/setup.d.ts.map +1 -1
  13. package/dist/commands/setup.js +67 -28
  14. package/dist/commands/setup.js.map +1 -1
  15. package/dist/commands/worktree.d.ts +17 -4
  16. package/dist/commands/worktree.d.ts.map +1 -1
  17. package/dist/commands/worktree.js +28 -4
  18. package/dist/commands/worktree.js.map +1 -1
  19. package/dist/core/AgentWorktreeDetector.d.ts +63 -0
  20. package/dist/core/AgentWorktreeDetector.d.ts.map +1 -0
  21. package/dist/core/AgentWorktreeDetector.js +329 -0
  22. package/dist/core/AgentWorktreeDetector.js.map +1 -0
  23. package/dist/core/InstarWorktreeManager.d.ts +99 -0
  24. package/dist/core/InstarWorktreeManager.d.ts.map +1 -0
  25. package/dist/core/InstarWorktreeManager.js +447 -0
  26. package/dist/core/InstarWorktreeManager.js.map +1 -0
  27. package/dist/core/MachineIdentity.d.ts.map +1 -1
  28. package/dist/core/MachineIdentity.js +2 -0
  29. package/dist/core/MachineIdentity.js.map +1 -1
  30. package/dist/core/PostUpdateMigrator.d.ts +25 -0
  31. package/dist/core/PostUpdateMigrator.d.ts.map +1 -1
  32. package/dist/core/PostUpdateMigrator.js +258 -0
  33. package/dist/core/PostUpdateMigrator.js.map +1 -1
  34. package/dist/memory/NativeModuleHealer.d.ts +15 -0
  35. package/dist/memory/NativeModuleHealer.d.ts.map +1 -1
  36. package/dist/memory/NativeModuleHealer.js +22 -0
  37. package/dist/memory/NativeModuleHealer.js.map +1 -1
  38. package/dist/monitoring/NativeHealDegradationBridge.d.ts +32 -0
  39. package/dist/monitoring/NativeHealDegradationBridge.d.ts.map +1 -0
  40. package/dist/monitoring/NativeHealDegradationBridge.js +61 -0
  41. package/dist/monitoring/NativeHealDegradationBridge.js.map +1 -0
  42. package/dist/monitoring/PromptGate.d.ts +12 -0
  43. package/dist/monitoring/PromptGate.d.ts.map +1 -1
  44. package/dist/monitoring/PromptGate.js +44 -0
  45. package/dist/monitoring/PromptGate.js.map +1 -1
  46. package/dist/scaffold/templates.d.ts.map +1 -1
  47. package/dist/scaffold/templates.js +17 -0
  48. package/dist/scaffold/templates.js.map +1 -1
  49. package/dist/server/AgentServer.d.ts.map +1 -1
  50. package/dist/server/AgentServer.js +6 -0
  51. package/dist/server/AgentServer.js.map +1 -1
  52. package/package.json +1 -1
  53. package/scripts/lint-no-direct-destructive.js +7 -0
  54. package/scripts/resolve-release-tier.mjs +192 -0
  55. package/src/data/builtin-manifest.json +57 -49
  56. package/src/scaffold/templates.ts +17 -0
  57. package/src/templates/scripts/instar-watchdog.sh +169 -6
  58. package/src/templates/scripts/instar-worktree-create.sh +79 -0
  59. package/upgrades/1.1.0.md +320 -0
  60. package/upgrades/1.1.1.md +58 -0
  61. package/upgrades/side-effects/agent-worktree-convention-layer-1-2-5.md +280 -0
  62. package/upgrades/side-effects/agent-worktree-convention-layer-3-4.md +285 -0
  63. package/upgrades/side-effects/feat-fix-parent-framework-intercept.md +59 -0
  64. package/upgrades/side-effects/feat-install-claude-gating.md +67 -0
  65. package/upgrades/side-effects/feat-install-framework-flag.md +65 -0
  66. package/upgrades/side-effects/feat-portability-shadow-capabilities.md +63 -0
  67. package/upgrades/side-effects/feat-setup-framework-flag.md +65 -0
  68. package/upgrades/side-effects/lockdown-layer-2.md +123 -0
  69. package/upgrades/side-effects/native-heal-degradation-bridge.md +206 -0
  70. package/upgrades/side-effects/watchdog-bind-failure-probe.md +174 -0
@@ -0,0 +1,174 @@
1
+ # Side-Effects Review — Fleet watchdog bind-failure probe
2
+
3
+ **Version / slug:** `watchdog-bind-failure-probe`
4
+ **Date:** 2026-05-19
5
+ **Author:** echo
6
+ **Second-pass reviewer:** subagent (high-risk: touches "watchdog" trigger)
7
+ **Spec:** [docs/specs/watchdog-bind-failure-probe.md](../../docs/specs/watchdog-bind-failure-probe.md)
8
+ **ELI16:** [docs/specs/watchdog-bind-failure-probe.eli16.md](../../docs/specs/watchdog-bind-failure-probe.eli16.md)
9
+
10
+ ## Summary of the change
11
+
12
+ Adds a `probe_server_identity` function to the fleet watchdog. For every agent that launchd reports as loaded-and-running, the probe asks the agent's `/health` endpoint to confirm the server on its configured port actually belongs to it. If the port is unreachable or returns a project name belonging to a different agent, the watchdog logs a `BIND-FAIL` signal and runs it through the same `try_self_heal` → consecutive-fail-counter → `escalate_via_peer` pipeline as a launchd crash-loop.
13
+
14
+ When escalation fires for a BIND-FAIL with a known conflicting party, the alert payload uses a more specific summary: *"AI Guy hasn't been able to start its server because codex-server-smoke is using its port. My repair attempts haven't fixed it."* The fallback (no conflict context) is the existing generic "offline for about X minutes" copy. Both variants pass the existing B12-B14 health-alert ruleset; the `/attention` route's safe-template retry from PR #245 still backstops any tone-gate rejection.
15
+
16
+ Decision points the change touches:
17
+ - **`probe_server_identity`** (new) — structural detector, emits a signal, no blocking authority.
18
+ - **`handle_bind_fail`** (new) — routes the signal through the existing heal+escalate pipeline. No new authority surface.
19
+ - **`escalate_via_peer`** (modified) — reads optional conflict context to build a richer summary. The summary still goes through `MessagingToneGate` via `/attention`; the gate remains the only authority.
20
+ - **`reset_fail_counter`** (modified) — also clears the new `bind-fail-conflict` state file.
21
+
22
+ ## Decision-point inventory
23
+
24
+ - `probe_server_identity` — **add** — structural detector returning 0/2/3 + structured stdout.
25
+ - `handle_bind_fail` — **add** — routes BIND-FAIL into the existing heal+escalate pipeline.
26
+ - Main loop's healthy-PID branch — **modify** — now calls the probe before treating PID-alive as healthy.
27
+ - `escalate_via_peer` — **modify** — picks up optional conflict context for a more specific summary.
28
+ - `reset_fail_counter` — **modify** — also clears `bind-fail-conflict` marker.
29
+
30
+ ---
31
+
32
+ ## 1. Over-block
33
+
34
+ **What legitimate inputs does this change reject that it shouldn't?**
35
+
36
+ - **Probe — "wrong-project" false positive.** If the agent's label-derived expected project name doesn't match the `project` field in /health (e.g. someone renamed an agent without updating the launchd label, or an old instar version reports project under a different field name), the probe flags BIND-FAIL on a perfectly healthy agent. Mitigation: the probe FAILS OPEN when `project` is empty in the response (old instar versions don't expose it). Documented in test `returns 0 when project field is absent`. The label-mismatch case is harder — if you legitimately renamed an agent, you must update the launchd label too. The alert text would still be coherent ("X is offline because Y is using its port" — Y is the agent's actual project name, the user can act on it).
37
+ - **Probe — "unreachable" false positive during legitimate maintenance.** If a user manually stops an agent's server (e.g. for an upgrade) while the lifeline stays alive, the probe will report BIND-FAIL. After 3 cycles (~15 min) the user gets a Telegram alert. Acceptable — the user knows they stopped it and can ignore. Not common enough to justify a per-agent "muted" flag in this PR (deferrable to v3 Remediator).
38
+ - **Probe — `--max-time 5` curl timeout.** A server doing legitimate slow work that takes >5s to respond to `/health` would be flagged unreachable. The `/health` endpoint is intentionally fast (no DB hits, no LLM calls); 5s is comfortable. If a future change makes `/health` slower, this test fails first.
39
+ - **No false alarms for lifeline-only agents.** Agents with no `port` in config.json skip the probe entirely (return 0). Test confirms.
40
+
41
+ ---
42
+
43
+ ## 2. Under-block
44
+
45
+ **What failure modes does this still miss?**
46
+
47
+ - **Slow port-collision.** If two agents race for the same port at boot, the probe will only see the winner. The loser's lifeline is still up. The probe correctly catches this (the loser's expected project doesn't match the winner's). But if both processes alternate (one fails immediately, the other binds, then they swap on next launchd respawn), the probe sees inconsistent states across cycles and may take longer to escalate. Acceptable — the consecutive-fail counter handles the noise.
48
+ - **Wrong port in config.json.** If config.json says port 5050 but the lifeline actually starts the server on a different port (config drift), the probe will hit the wrong port and false-alarm. This shouldn't happen in practice — the lifeline reads the same config.json the probe reads. But if a future change adds runtime port mutation, this needs revisiting.
49
+ - **Server identifies itself with a project name that's not the label minus prefix.** If the agent's `agentName` in config differs from its launchd label (e.g. user customized one), the probe false-alarms. Mitigation deferred — current convention is they match, and a sentinel test could enforce this convention going forward.
50
+ - **Tone gate rejects both attempts (initial + safe template).** Same backstop as PR #245: counter preserved, retried next cycle. No regression from this PR.
51
+
52
+ ---
53
+
54
+ ## 3. Level-of-abstraction fit
55
+
56
+ **Is this at the right layer?**
57
+
58
+ - **Probe in the watchdog (this PR).** The watchdog is the OUTSIDE-the-lifeline observer with the cross-agent visibility (it sees all plists + can correlate `expected agent X on port P` against `who's actually on port P`). The lifeline can't do this cross-agent view from inside itself. So watchdog is the right layer.
59
+ - **In-supervisor bind-detection (PR #111) remains.** That layer catches the per-agent case where the server's own preflight detects bind failure. This PR catches the failure mode where the supervisor's alerts are being SUPPRESSED as duplicates (the exact failure that bit AI Guy). Two layers, two signals, same authority for the user-facing alert.
60
+ - **Not at the v3 Remediator layer yet.** v3 will absorb both layers under a unified probe registry + runbook system. This PR is the minimum plumbing until v3 ships. Both specs explicitly agree on the absorption point.
61
+
62
+ ---
63
+
64
+ ## 4. Signal vs authority compliance
65
+
66
+ **Required reference:** [docs/signal-vs-authority.md](../../docs/signal-vs-authority.md)
67
+
68
+ **Does this change hold blocking authority with brittle logic?**
69
+
70
+ - [x] **No — this change produces a signal consumed by an existing smart gate.**
71
+
72
+ Detailed:
73
+ - `probe_server_identity` is a structural detector: file existence (config.json), HTTP code, JSON field equality. No judgment, no blocking. Returns numeric codes + structured stdout.
74
+ - The consecutive-fail counter is a deterministic threshold (mechanic, explicitly exempt per the principle's §"When this principle does NOT apply").
75
+ - `escalate_via_peer` builds a CANDIDATE message. The decision to ship that candidate, reshape it, or fall back to the SAFE template rests entirely with `MessagingToneGate` via the `/attention` route — the existing authority. This PR does not modify the gate; it just supplies a different candidate.
76
+ - The new conflict-aware summary copy was hand-crafted to pass B12 (no jargon: no "lifeline", "launchd", "pid", "crash-loop", "shadow-install"). A unit test screens the payload heredoc for these terms; future drift would fail the test.
77
+
78
+ This PR adds NO new blocking authority.
79
+
80
+ ---
81
+
82
+ ## 5. Interactions
83
+
84
+ **Does this interact with existing checks, recovery paths, or infrastructure?**
85
+
86
+ - **Shadowing:**
87
+ - The bind-probe runs INSIDE the healthy-PID branch. Before: PID exists → OK. After: PID exists + probe passes → OK; PID exists + probe fails → BIND-FAIL → heal pipeline. The OK path is now slower (one curl per agent per cycle, capped at 5s). Acceptable: cycles run every 5 min, so an extra ~30ms per agent is negligible.
88
+ - The crash-loop branch (exit_status != 0) is unchanged. PR #245's path is preserved exactly.
89
+ - **Double-fire with supervisor's internal bind-detection (PR #111):**
90
+ - The supervisor's in-process detection emits a `DegradationReporter` event INSIDE the lifeline. That event tries to route through ToneGate → /attention on the SAME agent's server. But the failing agent's server is locked out — so the in-process route is broken precisely when it's needed. The watchdog's external probe + peer-escalation is what actually surfaces the failure. The two paths can coexist; the peer-routed POST will win (it actually reaches Telegram). Idempotency-by-id in `/attention` prevents duplicate topic creation if both ever land.
91
+ - **Races:**
92
+ - State files (`*.consecutive-heal-fails`, `*.bind-fail-conflict`) are per-label. Concurrent watchdog runs are prevented by launchd's single-instance default. Manual invocation could race, but the script's writes are atomic single-line files.
93
+ - The probe-vs-healing race: if heal succeeds between probe-time and the launchd reload, the next cycle's probe sees healthy state and resets counters. No persistent stuck state.
94
+ - **Feedback loops:**
95
+ - When the user replies to the escalation Telegram topic, the reply lands at the PEER that escalated, not the failing agent. Documented in PR #245's review (same surface, no new behavior here).
96
+ - The bind-fail-conflict marker is cleared on (a) probe-pass, (b) escalation success, (c) crash-loop reset. No way for stale conflict context to leak into future escalations.
97
+
98
+ ---
99
+
100
+ ## 6. External surfaces
101
+
102
+ **Does this change anything visible outside the immediate code path?**
103
+
104
+ - **Other agents on the same machine:** YES — adds one `curl /health` per agent per 5-minute cycle. Negligible load, no auth required for /health.
105
+ - **Other users of the install base:** YES — ships in next release via PostUpdateMigrator. macOS-only effect.
106
+ - **External systems:**
107
+ - Telegram: a new failure-mode class (port collision) now produces a Telegram topic. Same authority gates it.
108
+ - `/attention` route: unchanged behaviorally; this PR is a new caller.
109
+ - **Persistent state:**
110
+ - New marker file `bind-fail-conflict` in `~/.instar/watchdog-state/`. Single-line, agent label, harmless.
111
+ - Existing `consecutive-heal-fails` markers are now shared between crash-loop and bind-fail paths — they both increment / reset the same counter. Test confirms reset_fail_counter clears both.
112
+ - **Timing:** Probe curl adds ~30ms-5s per agent per cycle (capped at 5s). For a 10-agent machine, max overhead is 50s of a 300s cycle. Acceptable.
113
+
114
+ ---
115
+
116
+ ## 7. Rollback cost
117
+
118
+ **If this turns out wrong in production, what's the back-out?**
119
+
120
+ - **Hot-fix:** revert `src/templates/scripts/instar-watchdog.sh` to the PR-#245 state. The next agent update overwrites the user-level script via `PostUpdateMigrator.migrateFleetWatchdog`. Ship as a patch release.
121
+ - **Data migration:** none. `bind-fail-conflict` marker files in `~/.instar/watchdog-state/` are harmless if left behind (auto-pruned by the existing 24-hour cleanup that already covers `last-heal` files; the regex could be widened later if needed).
122
+ - **Agent state repair:** none required.
123
+ - **User visibility during rollback:** minimal. The watchdog reverts to its pre-this-PR behavior — no bind-fail detection, no false alarms either.
124
+ - **Estimated total:** ~30 min revert + release cycle.
125
+
126
+ ---
127
+
128
+ ## Addendum 2026-05-19 — Reviewer findings + fixes
129
+
130
+ Second-pass reviewer found one critical defect plus two minors. All addressed:
131
+
132
+ **Critical — auth gating on `/health.project`.** The probe called `/health` without an Authorization header. The route only sets `base.project` inside `if (isAuthed)` (`src/server/routes.ts:1157`), so production probes would have received responses with no `project` field, hitting the fail-open branch, and the wrong-project detection — the entire reason for this PR — would never have fired in production. Fix: probe now reads `authToken` alongside `port` from `config.json` and sends `Authorization: Bearer <token>` with the `/health` request. Two new tests cover this (one shell-level asserting the header lands in the actual HTTP request; one template-content asserting the script references `authToken` + `Authorization: Bearer`).
133
+
134
+ **Minor 1 — state-file cleanup regex.** The existing 24h cleanup matched only `*.last-heal`. Widened to `*.last-heal | *.consecutive-heal-fails | *.bind-fail-conflict` so uninstalled-agent state doesn't accumulate. Documented inline.
135
+
136
+ **Minor 2 — uninstalled-peer cleanup path not tested.** Deferred. The widened regex is exercised by the existing watchdog-cycle path; standalone test of the cleanup is mechanical and adds little value given the simple find expression.
137
+
138
+ **Confirmed sound (no change needed):** signal-vs-authority compliance, PR #245 idempotency-by-id holds via `TelegramAdapter.createAttentionItem`'s `attentionItems.has(item.id)` short-circuit, 5s curl timeout is appropriate for macOS-only production target, migration parity covered by `PostUpdateMigrator.migrateFleetWatchdog`.
139
+
140
+ ## Conclusion
141
+
142
+ This PR adds the second layer of bind-failure detection — outside the lifeline, with cross-agent visibility — that closes the failure-mode that kept AI Guy offline for two days post-PR-#245. No new authority over message flow; the probe is a structural detector that feeds the existing tone-gated escalation pipeline. The conflict-aware summary copy was crafted to pass B12 and is unit-tested for jargon. Two new state markers, both harmless on rollback.
143
+
144
+ 17 new tests added (11 template-content unit tests + 4 darwin-gated behavioural unit tests + 2 darwin-gated integration tests covering the full pipeline). Cross-platform template assertions ensure the production-code paths stay consistent across Linux CI runs.
145
+
146
+ Clear to ship.
147
+
148
+ ---
149
+
150
+ ## Second-pass review (if required)
151
+
152
+ **Reviewer:** second-pass subagent (Opus 4.7 1M, 2026-05-19)
153
+ **Independent read of the artifact: concern**
154
+
155
+ Signal-vs-authority compliance and rollback cost check out. The probe is structurally a detector, the counter is mechanic, and the candidate message goes through `MessagingToneGate` via `/attention` — same authority, no new blocker. PR #245 idempotency-by-id is real: `TelegramAdapter.createAttentionItem` at `src/messaging/TelegramAdapter.ts:2949` short-circuits on `attentionItems.has(item.id)`, so a double-fire from PR #111's in-process path and this PR's external path cannot duplicate the topic. However:
156
+
157
+ - **(CRITICAL) Wrong-project detection is dead-on-arrival in production.** The `/health` route only includes `base.project` inside the `isAuthed` branch (`src/server/routes.ts:1140` opens `if (isAuthed)`, `1157` sets `base.project`). The probe's curl at `src/templates/scripts/instar-watchdog.sh:444` sends no `Authorization` header, so every peer agent's `/health` response will have `project` absent → probe hits the fail-open branch at `instar-watchdog.sh:460-464` (`project_field == ""` → `return 0`). The original AI-Guy-behind-codex-server-smoke incident was exactly a wrong-project case (200 response, foreign project name), so this PR would NOT have caught it. The unit test passes because the mock at `tests/unit/watchdog-bind-probe.test.ts:160` returns `project` unconditionally regardless of auth. **Recommended resolution:** either (a) read `authToken` from the agent's own `config.json` (we already read `port` from there) and send `Authorization: Bearer $auth` on the probe, or (b) expose `project` in the unauthenticated `/health` body — `projectName` is already public via launchd plist paths, so this is not a secret. (a) is the smaller change; (b) is cleaner. Either way, add a darwin-gated probe test that exercises the actual route handler (or a fixture that mirrors its auth-gated shape) instead of a mock that hand-waves the auth.
158
+
159
+ - **(MINOR) Stale state-file claim under-counts the gap.** Artifact §7 says `bind-fail-conflict` markers are "auto-pruned by the existing 24-hour cleanup that already covers `last-heal` files." `instar-watchdog.sh:623` is `find ... -name "*.last-heal" -mmin +1440 -delete` — it does not match `*.bind-fail-conflict` or `*.consecutive-heal-fails`. In practice `reset_fail_counter` clears both on every successful probe/escalation, so growth is bounded for active agents. But a launched-then-deleted agent (plist gone, marker left behind) will leak both files until a human cleans `~/.instar/watchdog-state/`. **Recommended resolution:** widen the regex to `\( -name "*.last-heal" -o -name "*.bind-fail-conflict" -o -name "*.consecutive-heal-fails" \)`, or document the leak as accepted with an issue link.
160
+
161
+ - **(MINOR) Coverage gap — uninstalled-peer leak.** No test asserts that markers for a label whose plist no longer exists eventually get cleaned. Combined with the regex above, this is the realistic stale-state path. Add a unit test that creates a marker, removes the plist, runs a cycle, and asserts the marker is gone (after fixing the regex).
162
+
163
+ Counter exclusion to surface: 5s timeout is fine for macOS-only production; `/health` is intentionally cheap (no DB/LLM) and the cached session count already protects against event-loop stalls (`routes.ts:1097`). No concern there.
164
+
165
+ Once the auth header (or unauth `project` exposure) is wired, this PR delivers what the spec promises. Until then, it ships a probe that emits the right log line in dev fixtures and silently fails open in production.
166
+
167
+ ---
168
+
169
+ ## Evidence pointers
170
+
171
+ - Spec: `docs/specs/watchdog-bind-failure-probe.md` (with ELI16 companion).
172
+ - Tests: `tests/unit/watchdog-bind-probe.test.ts` (15 tests), `tests/integration/watchdog-bind-fail-escalation.test.ts` (2 tests).
173
+ - Incident reference: AI Guy port collision with codex-server-smoke, 2026-05-17 → 2026-05-19, topic 5447. Lifeline supervisor logged "Suppressing duplicate server down notification (4163 suppressed this outage)" — captured in `~/Documents/Projects/ai-guy/.instar/logs/lifeline-launchd.log`.
174
+ - Recovery: manual unload of codex-server-smoke plist + restart of ai-guy launchd job. After this PR ships, the same configuration error would have surfaced to the user via Telegram within ~15 min instead of staying silent for 2 days.