fixate-on 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,327 @@
1
+ # TUI Integration Guide
2
+
3
+ The `fixate-on` provides a dual-mode system: a **server plugin**
4
+ that manages goal state and a **TUI companion plugin** that renders
5
+ interactive status panels, keyboard controls, and toast notifications.
6
+
7
+ ## Architecture
8
+
9
+ **Which file loads what:** stock OpenCode v1 loads the server plugin from the
10
+ `opencode.json` `plugin` array and the TUI companion ONLY via the `tui.json`
11
+ `plugin` array (`~/.config/opencode/tui.json`, overridable with
12
+ `$OPENCODE_TUI_CONFIG`).
13
+ Registering the companion in `opencode.json` does not load it. With the
14
+ companion in `tui.json`, all 11 `Fixate:` palette entries of the v1 companion render
15
+ (palette filtered on `fixate:`; the v2 companion registers 13) with chord
16
+ hints shown on the six bound actions. One
17
+ caveat stands: the chords themselves are not byte-deliverable through
18
+ tmux/terminal (`ctrl+shift+g` arrives as `0x07`), so the palette — not the
19
+ chord — is the guaranteed path, and every live campaign in
20
+ `hack/e2e-harness/` drives the palette. The v1 companion therefore keeps
21
+ its slash + state-file surfaces first-class.
22
+
23
+ On opencode2 the server→TUI channel rides `tool Result.metadata`
24
+ (`tool Result.metadata` → `session.tool.success` → `context.data.on` → store),
25
+ not `client.tui.publish`. On stock v1 the transport is disk hydration +
26
+ a state-file refresh loop plus curated human-readable toasts — the publish
27
+ channel is never used as a data transport there (v1 renders every published
28
+ toast on screen, so data events would flood the UI).
29
+
30
+ ```
31
+ OpenCode Host (v1: toast channel)
32
+
33
+ Server Plugin (src/plugin.ts) TUI Companion Plugin (src/tui.ts)
34
+ - goal state - state store
35
+ - state machine - dashboard formatter
36
+ - persistence - keymap handlers
37
+ - command hooks - toast manager
38
+ - broadcaster
39
+ | publish
40
+ v
41
+ Host Event Bus (SSE): client.tui.publish() -> api.event.on("tui.toast.show")
42
+ ```
43
+
44
+ ## Installation
45
+
46
+ Register the server plugin in `opencode.json` and the TUI companion in `tui.json`
47
+ (the v1 loader reads them from different files — putting the companion in
48
+ `opencode.json` does not load it):
49
+
50
+ `opencode.json`:
51
+
52
+ ```json
53
+ {
54
+ "plugin": [
55
+ "fixate-on"
56
+ ]
57
+ }
58
+ ```
59
+
60
+ `tui.json` (`~/.config/opencode/tui.json`, or `$OPENCODE_TUI_CONFIG`):
61
+
62
+ ```json
63
+ {
64
+ "plugin": [
65
+ "fixate-on/tui"
66
+ ]
67
+ }
68
+ ```
69
+
70
+ The server plugin (`fixate-on`) manages goal state, persistence,
71
+ and the command execution loop. The TUI companion (`fixate-on/tui`)
72
+ subscribes to goal lifecycle events and renders interactive panels.
73
+
74
+ ## Dual-Mode Command Execution
75
+
76
+ Slash commands (`/goal status`, `/goal history`, `/goal list`, etc.) operate
77
+ in two modes:
78
+
79
+ ### TUI Mode (companion attached)
80
+
81
+ When the TUI companion is registered, the server plugin detects
82
+ `client.tui.publish` and sets the `hasTuiCompanion` flag. Command output
83
+ includes a header noting the interactive dashboard is available:
84
+
85
+ ```
86
+ [TUI mode] /status
87
+ Active goal: fix the failing tests
88
+ Auto-continues sent: 3/10
89
+ Context tokens: 12,400/200,000
90
+ ...
91
+ ```
92
+
93
+ The TUI companion receives goal lifecycle events in real time via the host
94
+ event bus, maintaining a synchronized state store that powers the interactive
95
+ dashboard.
96
+
97
+ ### Server Mode (headless / no TUI)
98
+
99
+ When no TUI companion is registered, commands produce plain-text output with
100
+ an explicit header indicating the output is routed to the model as a chat
101
+ turn:
102
+
103
+ ```
104
+ [Server mode - output routed to model as chat turn] /status
105
+ Active goal: fix the failing tests
106
+ Auto-continues sent: 3/10
107
+ Context tokens: 12,400/200,000
108
+ ...
109
+ ```
110
+
111
+ This is the default behavior when running in headless/automated environments.
112
+
113
+ ## Keyboard Shortcuts
114
+
115
+ The TUI companion registers two-key chords: the `ctrl+shift+g` prefix, then the
116
+ action key. The prefix is what keeps these out of the prompt's way.
117
+
118
+ | Chord | Action | Description |
119
+ |--------------------|-------------|--------------------------------|
120
+ | `ctrl+shift+g` `d` | Dashboard | Show the status dashboard |
121
+ | `ctrl+shift+g` `p` | Pause | Pause the active goal |
122
+ | `ctrl+shift+g` `r` | Resume | Resume the paused goal |
123
+ | `ctrl+shift+g` `c` | Clear | Clear the active goal |
124
+ | `ctrl+shift+g` `e` | Edit | Edit the goal objective |
125
+ | `ctrl+shift+g` `k` | Checkpoint | Add a checkpoint |
126
+
127
+ **The prefix is not universally deliverable.** On some terminals `ctrl+shift+g`
128
+ arrives as `0x07` and the chord never fires. The command palette
129
+ (`ctrl+p`, then type `fixate:`) reaches all six actions on every supported host,
130
+ and is the path every live campaign in `../hack` takes.
131
+
132
+ The `0x07` behaviour was recorded during product design. The prefix is unclaimed
133
+ in common emulators and still undeliverable on some terminals; both can be
134
+ true. The chord is an optimisation; the palette is the guarantee.
135
+
136
+ Keymaps are optional overrides — the user can interact via `/goal` commands
137
+ without ever touching a key (AGENTS.md Principle 4).
138
+
139
+ ## State Synchronization
140
+
141
+ The server plugin publishes goal lifecycle events over
142
+ `pluginOptions.tuiTransport` — the v2 adapter's buffering channel, surfaced
143
+ through tool `Result.metadata`. That envelope carries a `fixate-on` marker the
144
+ companion filters on:
145
+
146
+ ```json
147
+ {
148
+ "fixate-on": { "events": [ { "kind": "transition", "goalId": "…", "snapshot": { } } ] }
149
+ }
150
+ ```
151
+
152
+ `GOAL_TUI_METADATA_KEY` is the property name and `GOAL_TUI_EVENT_TITLE` is the
153
+ title used on the equivalent direct event envelope; both are the package name by
154
+ design, and both are defined once in `src/tui-state.ts`.
155
+
156
+ ### On v1 hosts there is no event transport
157
+
158
+ The v1 `client.tui.publish` endpoint renders **every** published envelope as a
159
+ visible toast, so the server sends **no** data events on that channel — it is
160
+ contractually silent, and a test locks that. On a v1 host the companion instead
161
+ reads the persisted state file (`readDiskGoalSnapshots`) on a refresh loop
162
+ (`createDiskRefreshLoop`) and curates its own rare, human-readable toasts
163
+ (`tui-toast-policy.ts`). Live indicators still update; the source of truth is the
164
+ state file rather than an event stream.
165
+
166
+ This is why the architecture note above says the publish channel "is never used
167
+ as a data transport there". It is not a lesser path — it is the only path that
168
+ does not put JSON on a user's screen.
169
+
170
+ ### Which moments reach the user
171
+
172
+ `createDiskRefreshLoop` synthesises an event per diff, and `tui-toast-policy.ts`
173
+ decides which of those are worth a line of text. The rule is the **working
174
+ boundary**: a moment that changes whether the goal is working.
175
+
176
+ | Moment | Announced |
177
+ |---|---|
178
+ | Goal created or cleared | yes |
179
+ | Started working — session resume, sequence auto-promote, resume after a brake | yes |
180
+ | Stopped working — stall, deferral, execution error, `/goal pause`, brake, complete, blocked | yes |
181
+ | Paused for your next message, and re-armed when your turn ends | no, either half |
182
+ | The plugin's own recovery pause while restoring a session | no |
183
+ | Every continuation of a goal that is already working | no |
184
+ | Token and turn accounting | no |
185
+
186
+ The two silent rows follow from the same rule. On every human message
187
+ `hooks.ts` pauses each active goal and the idle handler re-arms it, so the user
188
+ just spoke and the goal hands the turn straight back; announcing either half
189
+ produced two toasts per goal per message sent. The recovery pause is the
190
+ plugin's own bookkeeping during hydration. Both are recognised from the stop's
191
+ own `stopReason` — the companion sees the pause and remembers which goals are
192
+ mid-yield — so no producer had to change to tell a yield from a resume.
193
+
194
+ Telling "started" from "still working" needs the status the goal held *before*
195
+ the event. A running goal rewrites its persisted state on every turn, so
196
+ `status: active` alone cannot say whether the goal just started or is still
197
+ going. Both producers send that: the broadcaster as `previousStatus` alongside
198
+ its `transition`, and the disk loop from the snapshot it diffed against. An
199
+ event with neither is announced only when its snapshot states its own outcome —
200
+ `complete`, `blocked`, or a limit-class stop — because an `active` snapshot on
201
+ its own cannot distinguish an entry from a continuation, and guessing "entry" is
202
+ what a storm looks like.
203
+
204
+ `ToastCoalescer` sits between the policy and `ui.toast`. It enforces a 2 s
205
+ minimum interval, a 500 ms duplicate window and a bounded queue, **per
206
+ session** — a goal settling in one session of a workspace is not silenced by
207
+ another session's traffic. When one goal produces a second moment inside the
208
+ same window, the newer replaces the older in the queue: the newest state of a
209
+ goal is the only one worth saying. A toast already shown is not replaced,
210
+ because it cannot be un-shown, so a goal that stops and is resumed in separate
211
+ windows is reported twice — once for each thing that actually happened.
212
+
213
+ ### On load
214
+
215
+ Hydration is silent about moments. A goal recovered from disk does not announce
216
+ itself as newly started, because you did not start it — and the refresh loop
217
+ refuses to take a baseline from a read that could see neither a state file nor a
218
+ state directory, so a state file caught mid-write cannot turn every running goal
219
+ into a fresh "Goal started". It is not silent about facts: after the companion's
220
+ first authoritative read it emits at most one summary naming how many goals in
221
+ the workspace are stopped and waiting on you, grouped by reason. Goals that
222
+ resume on their own — `recovery`, `user intervention`, `queued` — are excluded,
223
+ because the boundary announcement already covers them. The exclusion for
224
+ `user intervention` is conditional: the pause is a promise that the plugin
225
+ will re-arm the goal when your turn ends, and on a host that delivers no idle
226
+ events the poller is the only thing that can keep that promise. The poller
227
+ can refuse — the newest message is yours and no reply has arrived, the reply
228
+ carries no id, or the turn never reports itself settled — and each refusal is
229
+ correct, because a gate that stops a continuation being injected into a live
230
+ turn is protecting against a worse harm than a goal that waits.
231
+
232
+ So a yield past `YIELD_PROMISE_MS` (15 minutes, read from the goal's own
233
+ persisted clock so a restart cannot forgive it) stops being counted as
234
+ self-resolving. It briefs as "it did not pick your message back up", the card
235
+ reads `did not re-arm` rather than `re-arms automatically`, and `/goal status`
236
+ offers a resume. A yield inside its window is unchanged, which is the half that
237
+ matters: the exclusion exists so a goal about to resume is not announced as
238
+ though it needed you.
239
+
240
+ ### Event Types
241
+
242
+ | Kind | Trigger | Payload |
243
+ |--------------|----------------------------|-------------------|
244
+ | `create` | Goal created | Snapshot |
245
+ | `transition` | Status change (pause/resume/complete/block) | Snapshot + `previousStatus`; the server broadcaster also sends `transition` |
246
+ | `usage` | Token/turn count updated | Snapshot |
247
+ | `remove` | Goal deleted | goalId only |
248
+
249
+ ### Resilience
250
+
251
+ - **Broadcaster failures are swallowed** — a TUI disconnect cannot break the
252
+ state machine.
253
+ - **Revision monotonicity** — stale events (lower revision) are ignored for
254
+ existing goals.
255
+ - **Late subscribers hydrate** from the store's current state (no event
256
+ replay needed), and on v1 from the state file on disk.
257
+ - **Snapshots are frozen** — the TUI render layer cannot mutate shared state.
258
+ - **A disk refresh that races a write** is a read of a partially written file,
259
+ not a crash: the next tick re-reads.
260
+
261
+ ### Display lag and the staleness floor
262
+
263
+ The file-sourced dashboard does not show live memory; it shows the last state
264
+ file the server persisted, read on a poll. Three numbers bound how stale that
265
+ can be, and they are stated here because the acceptance for state alignment
266
+ requires the lag documented with numbers rather than left implicit.
267
+
268
+ - **Usage-only churn does not move the gauge.** `updateUsage`
269
+ (src/state-machine.ts, `updateUsage`) mutates the in-memory state and deliberately skips
270
+ persistence: "No onCommit — usage updates are frequent; persistence happens on
271
+ the next semantic transition" (the comment on `updateUsage` in src/state-machine.ts). The dashboard reads
272
+ `totalTokens` from the state file (src/tui-views.ts), so during a long
273
+ turn that produces only token churn the gauge stays where the last semantic
274
+ persist left it. It moves on the next pause/resume/edit/checkpoint or any
275
+ lifecycle transition. That lag is by design, not a defect; the alternative is a
276
+ disk write per usage event.
277
+ - **Staleness floor.** A state-file change reaches the dashboard within one
278
+ refresh poll, default 1000 ms (`intervalMs` in src/tui-companion-refresh.ts). A lifecycle
279
+ moment reaches a toast within that poll plus the coalescer's 2000 ms
280
+ minimum-interval gate (`TOAST_MIN_INTERVAL_MS` in src/tui-toast-policy.ts), with a 500 ms
281
+ duplicate-suppression window. So the floor is roughly 1 s for the dashboard
282
+ and up to 3 s for a toast after a persist.
283
+ - **A WAL-durable removal with a failed snapshot.** `removeGoalOwned`
284
+ (src/session-lifecycle.ts) appends a durable removal tombstone to the
285
+ ledger, sets the invalidation fence, then persists the snapshot. If that
286
+ snapshot write fails, the
287
+ on-disk state file still lists the goal, and the fence marker is retained
288
+ (`pendingInvalidationCheckpoints`, held by SessionStateManager in
289
+ src/session-state-manager.ts) rather
290
+ than released. Two things keep this honest: the live removal is broadcast
291
+ immediately (`broadcaster.emitRemove`), so
292
+ the in-memory store drops the goal at once; and the fence's
293
+ `invalidatedGoalIds` is written into the next durable snapshot, so recovery and
294
+ any later read treat the goal as removed — it cannot be resurrected or driven.
295
+ The v1 companion reads snapshots only, never the ledger, so between a failed
296
+ snapshot and the next successful persist there is a bounded window where the
297
+ disk-sourced dashboard can still show the goal; the fence, not the display, is
298
+ the guarantee, and it holds.
299
+
300
+ ## Module Structure
301
+
302
+ | Module | Role | Project dependencies |
303
+ |---|---|---|
304
+ | `src/tui-state.ts` | Snapshot projection, event contract, in-process store (leaf) | `accounting.ts`, `state-machine.ts` |
305
+ | `src/tui-components.ts` | Toast helpers + `PRODUCT_DISPLAY_NAME` (leaf) | None |
306
+ | `src/tui-views.ts` | Dashboard formatter (pure) | `brake-format.ts`, `status-label.ts`, `tui-analytics.ts`, `tui-components.ts`, `tui-state.ts` |
307
+ | `src/tui-dashboard-route.tsx` | Host-native full-screen dashboard route | `tui-components.ts`, `tui-state.ts`, `tui-views.ts` (the card's text comes from `goalCardLines`, which is what makes the attribution and the brake ceiling testable without the host's terminal renderer) |
308
+ | `src/tui-toast-policy.ts` | Which store moments deserve a visible toast | `status-label.ts`, `tui-components.ts`, `tui-state.ts`, plus the `GoalStatus` type from `state-machine.ts` |
309
+ | `src/tui-action-labels.ts` | Outcome-neutral palette labels and watchdog hints | `tui-components.ts` |
310
+ | `src/tui-analytics.ts` | Cost per turn, burn rate, ETA projection (pure) | `accounting.ts`, `tui-components.ts`, `tui-state.ts` |
311
+ | `src/tui-sound.ts` | Lifecycle event to attention-sound name (pure) | `state-machine.ts`, `tui-state.ts` |
312
+ | `src/tui-disk-state.ts` | Enumerates and reads every session's persisted goal state | `fs-portable.ts` (the bounded, symlink-refusing read), `session-paths.ts` (the layout), `state-machine.ts`, `tui-state.ts` |
313
+ | `src/tui-companion-refresh.ts` | v1 disk refresh loop | `tui-disk-state.ts`, `tui-state.ts` |
314
+ | `src/tui.ts` | v1 TUI companion entrypoint | `tui-action-labels.ts`, `tui-companion-refresh.ts`, `tui-components.ts`, `tui-disk-state.ts`, `tui-sound.ts`, `tui-state.ts`, `tui-toast-policy.ts`, `tui-views.ts`, plus `@opencode-ai/plugin` and `@opencode-ai/plugin/tui` |
315
+ | `src/tui-v2.ts` | v2 TUI companion entrypoint | `tui-action-labels.ts`, `tui-components.ts`, `tui-dashboard-route.tsx`, `tui-disk-state.ts`, `tui-state.ts`, `tui-v2-events.ts`, `tui-views.ts` |
316
+ | `src/tui-v2-events.ts` | v2 event-driven truth channels: disk-refresh loop, curated toasts, briefing, tool-metadata subscription | `state-machine.ts`, `tui-action-labels.ts`, `tui-companion-refresh.ts`, `tui-components.ts`, `tui-sound.ts`, `tui-state.ts`, `tui-toast-policy.ts` |
317
+
318
+ ## Host API Requirements
319
+
320
+ - **OpenCode** >= 1.17.18
321
+ - **`@opencode-ai/plugin`** >= 1.17.18 < 2
322
+ - The host client should expose `ui.toast` / `ui.dialog`. Absent those, the
323
+ companion still tracks state and the dashboard, it just cannot show anything.
324
+ - **`tui.publish` is not a data transport and never was** — see
325
+ [On v1 hosts there is no event transport](#on-v1-hosts-there-is-no-event-transport).
326
+ The v2 companion's data channel is the `tuiTransport` option; the v1
327
+ companion's is the state file.