@ftrdotdev/pi-tui 0.7.0 → 0.9.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.
Files changed (76) hide show
  1. package/CHANGELOG.md +225 -0
  2. package/README.md +5 -0
  3. package/package.json +18 -15
  4. package/src/activity-service.test.ts +97 -0
  5. package/src/activity.test.ts +1 -1
  6. package/src/activity.ts +38 -0
  7. package/src/chrome/budget.test.ts +345 -0
  8. package/src/chrome/budget.ts +220 -0
  9. package/src/chrome/declarations.ts +303 -0
  10. package/src/chrome/facts.test.ts +165 -0
  11. package/src/chrome/facts.ts +267 -0
  12. package/src/chrome/frame.ts +45 -0
  13. package/src/chrome/laws.test.ts +231 -0
  14. package/src/chrome/port.test.ts +164 -0
  15. package/src/chrome/port.ts +67 -0
  16. package/src/chrome/service-provider.test.ts +383 -0
  17. package/src/chrome/service-provider.ts +55 -0
  18. package/src/commands/git-execution.test.ts +5 -2
  19. package/src/commands/git-execution.ts +27 -15
  20. package/src/commands/git-status.test.ts +47 -15
  21. package/src/commands/git-status.ts +83 -31
  22. package/src/components/bordered-box.test.ts +12 -15
  23. package/src/components/bordered-box.ts +11 -7
  24. package/src/components/define-component.ts +1 -1
  25. package/src/components/index.ts +7 -0
  26. package/src/components/row-list.test.ts +310 -0
  27. package/src/components/row-list.ts +272 -0
  28. package/src/config.test.ts +118 -3
  29. package/src/config.ts +50 -42
  30. package/src/frame/box-frame.test.ts +53 -7
  31. package/src/harness-policy.test.ts +47 -0
  32. package/src/icons.test.ts +19 -0
  33. package/src/icons.ts +50 -31
  34. package/src/index.test.ts +135 -0
  35. package/src/index.ts +118 -119
  36. package/src/layers.test.ts +94 -0
  37. package/src/layers.ts +92 -0
  38. package/src/runtime-service.test.ts +218 -0
  39. package/src/runtime-service.ts +103 -0
  40. package/src/runtime.test.ts +165 -66
  41. package/src/runtime.ts +127 -93
  42. package/src/session-lifecycle-service.test.ts +95 -0
  43. package/src/session-lifecycle.test.ts +77 -0
  44. package/src/session-lifecycle.ts +21 -0
  45. package/src/state.test.ts +132 -3
  46. package/src/state.ts +43 -13
  47. package/src/telemetry-service.test.ts +250 -0
  48. package/src/telemetry.test.ts +322 -0
  49. package/src/telemetry.ts +130 -23
  50. package/src/ui/editor-tint.test.ts +58 -45
  51. package/src/ui/editor-tint.ts +1 -1
  52. package/src/ui/editor.ts +117 -53
  53. package/src/ui/footer.test.ts +349 -120
  54. package/src/ui/footer.ts +147 -124
  55. package/src/ui/header.test.ts +203 -223
  56. package/src/ui/header.ts +32 -96
  57. package/src/ui/preview.test.ts +73 -70
  58. package/src/ui/preview.ts +48 -33
  59. package/src/ui/render-helpers.test.ts +82 -13
  60. package/src/ui/render-helpers.ts +110 -122
  61. package/src/ui/settings-command.test.ts +70 -11
  62. package/src/ui/settings-command.ts +30 -45
  63. package/src/ui/theme-color.test.ts +4 -3
  64. package/src/usage-totals-service.test.ts +230 -0
  65. package/src/usage-totals-service.ts +59 -0
  66. package/src/utils.test.ts +46 -50
  67. package/src/utils.ts +41 -239
  68. package/src/vim/editor-host.test.ts +201 -75
  69. package/src/vim/editor-state.ts +2 -2
  70. package/src/vim/mode-indicator.test.ts +6 -5
  71. package/src/vim/mode-indicator.ts +13 -9
  72. package/src/vim/selection-render.ts +8 -4
  73. package/src/vim/text-model.test.ts +52 -14
  74. package/src/vim/text-model.ts +1 -1
  75. package/src/vim/vim-router.test.ts +3 -0
  76. package/src/vim/vim-router.ts +1 -1
package/CHANGELOG.md ADDED
@@ -0,0 +1,225 @@
1
+ # @ftrdotdev/pi-tui
2
+
3
+ ## 0.9.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 9b1fcd6: Standardize the `@effect/tsgo` setup and clear every lint finding.
8
+
9
+ - The toolchain moves to `@effect/tsgo` 0.73.4, `oxlint` 1.94.0, and
10
+ `oxlint-tsgolint` 7.0.2008.
11
+ - The root `tsconfig.json` now carries the compiler options and the
12
+ `@effect/language-service` plugin, and every package extends it. Stricter
13
+ flags join it: `noUnusedLocals`, `noUnusedParameters`, `noImplicitReturns`,
14
+ `noUncheckedSideEffectImports`, and `exactOptionalPropertyTypes`. Effect
15
+ diagnostics come from oxlint alone: the 13 `recommended` errors fail the
16
+ lint run, and the remaining rules report as warnings.
17
+ - `oxlint` is the only owner of the Effect diagnostics. `tsconfig.json` sets
18
+ the language-service plugin's `diagnostics` to `false`, so `tsc` no longer
19
+ repeats what oxlint reports. The root `oxlint.config.ts` extends the Effect
20
+ `style`, `correctness`, `antipattern` and `recommended` presets in that order;
21
+ oxlint finds it by walking up from each package, so the per-package configs
22
+ are gone.
23
+ - `Schema.Class`, `Schema.TaggedClass`, and `Schema.TaggedError` declarations
24
+ now carry the nominal brand type argument. **Type-level breaking change** for
25
+ code that assigned plain object literals to these types; construct them with
26
+ `.make()` or `new`.
27
+ - `@ftrdotdev/pi-inquiry`: `Keyboard`'s `nextKey` is
28
+ `readonly nextKey: Effect.Effect<KeyEvent>` instead of a zero-argument
29
+ method. **Breaking for code that implements `Keyboard["Service"]`**; callers
30
+ drop the `()`.
31
+ - `@ftrdotdev/pi-tui`: new `withStatus` and `withCounts` helpers replace
32
+ class-instance spreads when a status or count record needs one field
33
+ replaced. The config schema now uses `Schema.Finite` where `Schema.Number`
34
+ accepted `NaN` and infinities.
35
+ - `@ftrdotdev/pi-qrspi`: `readGitMetadata` omits unknown frontmatter fields
36
+ instead of setting them to `undefined`.
37
+
38
+ - eeb018a: Add `makeRowList`, a generic selectable row list: identity-held selection, arrow and router-decoded movement, section headers, footer, house selection style and a width-and-content render cache. Multi-line rows keep the cursor on their first line and indent their detail span under a rule; an optional `indent` nests child rows, and an optional `follows` relation keeps an opened group's children inside the window.
39
+ - 36e53c0: Rebuild the status line around declared chrome facts and one fitting engine.
40
+
41
+ - Add the fact registry, per-surface declarations, budget engine, chrome port,
42
+ and frame planner. The footer, header, notify line, and editor border now
43
+ read facts through the same contract.
44
+ - The header is a deterministic 10-row brand box. The footer drops the
45
+ work-state words, the cache-hit chip, and facts another surface owns. The
46
+ notify line defaults to TPS, TTFT, and stalls. The editor border fits its
47
+ label by declared priority.
48
+ - Derive config defaults from the fact registry:
49
+ `footerSegments.runtime`, `telemetry.duration`, `telemetry.tokens`, and
50
+ `telemetry.cost` default off. Every existing key survives. The context row
51
+ label is now `Context usage`.
52
+ - The context segment reads `◧ tokens/window` (for example `◧ 100k/200k`);
53
+ unknown tokens show `◧ —/200k`.
54
+ - The ascii glyph mode uses distinct, one-column Unicode glyphs. Vim mode keeps
55
+ `N`/`V`/`I` for clarity.
56
+ - Move the Effect catalog to 4.1.5 and oxfmt to 0.80.0.
57
+
58
+ ### Patch Changes
59
+
60
+ - 891568b: Ship `CHANGELOG.md` in the package tarballs.
61
+
62
+ - Pi reads a package changelog after an update and lists the new entries under
63
+ "Extension Updates" on the next fresh launch. It reads the file from the
64
+ installed package.
65
+ - The `files` whitelist in each `package.json` left the changelog out, so the
66
+ published tarballs carried no release notes.
67
+ - The README release section now records the requirement.
68
+
69
+ - 81c4a61: Move the Pi packages to `1.5.0` and build tool parameter schemas with
70
+ `@earendil-works/pi-schema`.
71
+
72
+ - `@earendil-works/pi-ai`, `@earendil-works/pi-coding-agent`, and
73
+ `@earendil-works/pi-tui` move from `1.4.0` to `1.5.0`. Pi 1.5.0 validates
74
+ tool arguments with `@earendil-works/pi-schema`.
75
+ - `pi-tracker` and `pi-tui` build `parameters` with pi-schema builders and
76
+ derive the parameter types from pi-schema's `Static`. The `typebox` peer
77
+ dependency is removed.
78
+ - `pi-inquiry` drops its TypeBox type import. Its Effect-generated JSON Schema
79
+ passes to Pi unchanged.
80
+ - Tool validation errors now use pi-schema messages, such as
81
+ `recommended: expected boolean, got string`.
82
+
83
+ - f8094f4: Show the interface in each package README, and point `homepage` at that package
84
+ README instead of the monorepo root.
85
+ - 305cdb7: Move the tui session state behind Effect services and one composition layer.
86
+
87
+ - Add `SessionLifecycleService`, `ActivityService`, `TurnTelemetryService`,
88
+ `UsageTotalsService` and `RuntimeInfoService`. The chrome port builder, the
89
+ extension entry and the tracker widget read them instead of module-level
90
+ cells, so a session start resets every store from one counter.
91
+ - Build the per-program graph in `src/layers.ts`. The runtime probe goes through
92
+ `FileSystem`, `Path` and `ChildProcessSpawner`, with the home directory and a
93
+ live environment read injected at the entry point.
94
+ - Compose the tracker's tool graph in `makeTrackerRuntime`, leaving `src/index.ts`
95
+ as the bridge that injects `appendEntry`.
96
+ - Guard the inquiry core: no `@earendil-works`, `@effect/platform` or `node:`
97
+ import outside the box-frame carve-out, and no `Layer` construction.
98
+
99
+ Rendered output is unchanged: the frozen footer and header frames stay
100
+ byte-identical at 30/40/60/80/100/120.
101
+
102
+ ## 0.8.0
103
+
104
+ ### Patch Changes
105
+
106
+ - c294fe5: Bump the Effect packages to `4.1.1`, `@effect/tsgo` to `0.63.0`, `vitest` to
107
+ `5.0.5`, and the Pi packages to `1.4.0`.
108
+
109
+ - `effect`, `@effect/platform-node`, `@effect/platform-node-shared`, and
110
+ `@effect/vitest` move from `4.0.0` to `4.1.1`; `@effect/tsgo` moves from
111
+ `0.47.2` to `0.63.0`.
112
+ - `@earendil-works/pi-ai`, `@earendil-works/pi-coding-agent`, and
113
+ `@earendil-works/pi-tui` move from `1.1.0` to `1.4.0`. No extension API
114
+ changes were needed.
115
+ - `vitest` moves from the 4.1 line to `5.0.5`, the range that
116
+ `@effect/vitest` declares as its peer. `oxlint` moves to `1.90.0` and
117
+ `oxlint-tsgolint` to `7.0.2005`, the pair that `@effect/tsgo` 0.63.0 supports.
118
+ - Effect 4.1.1 marks `effect/FileSystem`, `effect/Path`, `effect/PlatformError`,
119
+ `effect/Graph`, `@effect/platform-node*`, and `@effect/vitest` as unstable.
120
+ Each package `tsconfig.json` now lists them in the language-service plugin
121
+ option `allowedUnstableApis`, so typecheck stays green without per-file
122
+ `@effect-diagnostics` directives.
123
+ - The internal `TuiConfig` schema in `pi-tui` is now a `Schema.Struct` with a type
124
+ alias instead of a `Schema.Class`. The class was only ever used as plain data,
125
+ and the 4.1.1 `schemaClassObjectLiteral` rule flagged every literal and spread.
126
+ `TuiConfig` is not part of the package exports, so no consumer code changes.
127
+
128
+ ## 0.7.0
129
+
130
+ ### Patch Changes
131
+
132
+ - edb419d: Render the preview widget when the preview tool runs inside a codemode script. Nested tool calls get no TUI row, so the tool now appends its display record as a `preview` custom entry, which the entry renderer draws.
133
+ - f4213db: Rename the two smart-compaction confidence knobs so each name states what it measures.
134
+
135
+ - `smartCompaction.keepContextThreshold` becomes `smartCompaction.needsContextProbabilityThreshold`. It cuts on the classifier's `needs-context` probability. Raise it to compact more often.
136
+ - `smartCompaction.keepContextMinConfidence` becomes `smartCompaction.minAnswerConfidence`. It floors the answer's self-reported confidence. Raise it to keep context more often.
137
+ - Both old keys still resolve as aliases, so an existing `tui.json` keeps working. The new key wins when both are present.
138
+ - The `/tui` Compaction tab labels become `Needs-context probability` and `Answer confidence floor`.
139
+ - The tracker README no longer states the inverted direction for the probability knob.
140
+
141
+ ## 0.6.0
142
+
143
+ ### Minor Changes
144
+
145
+ - 3803293: Add mouse-wheel navigation, clipboard copy, and new theme helpers.
146
+
147
+ - The footer lists every runtime that matches the working directory, in table order, and joins them with a dim `·`. Previously it showed the first match only. An empty result stays an empty segment.
148
+ - The editor's top border shows pi's working status indicator during a run: spinner and message when they fit, spinner alone on narrow widths. The mode glyph and the scroll hint move behind it in the label slot.
149
+ - `makeBorderedBox` takes a theme background token for `bg` instead of a caller-supplied function. Pass `"customMessageBg"` where the code passed `(s) => theme.bg("customMessageBg", s)`. The border, the embedded label, and the body now paint the same background.
150
+ - `@ftrdotdev/pi-tui` exports derived theme colors: `mixThemeTokens`, `recede`, `emphasize`, and `isDark`. Use them for shades that are not theme tokens, such as a dimmed border or a selected row. `recede` and `emphasize` follow `theme.appearance`, so a light theme needs no special case.
151
+ - The `/tracker` overlay moves the cursor with the mouse wheel and wraps at either end. `[c]` copies the selected list or item and reports `Copied ...` in the overlay.
152
+ - The selected row in the `/tracker` overlay uses the `selectedBg` token.
153
+ - The inquiry questionnaire moves the cursor with the mouse wheel and copies the open question and its options with `ctrl+y`.
154
+ - The header clears the visible screen once per process, on the first regular-mode mount, and never again. Fullscreen owns the alternate screen, where the clear only flickered. A re-mount (`reload`, `new`, `fork`, `/resume`, settings toggle) no longer clears: that write went outside pi-tui and desynced its differential redraw, which left the space for an extension widget blank until the next full repaint.
155
+
156
+ - 6876f36: Add smart-compaction settings to the `/tui` dialog.
157
+
158
+ - A dedicated `Compaction` tab holds the smart-compaction settings. It shows five rows: `Smart compaction` (On/Off toggle), `Compaction classifier` (picker), `Keep-context confidence` (number), `Min confidence` (number), and `Max candidates` (integer).
159
+ - The `+` and `-` keys step `Keep-context confidence` and `Min confidence` by `0.05`, clamped to `0.05..0.95`. The same keys step `Max candidates` by `1`, clamped to `1..20`.
160
+ - The `Compaction classifier` picker cycles credential-available classifiers. `Automatic` is first and means "prefer Clef Flash, then Jev". When no classifier has credentials, the picker falls back to the full catalog.
161
+ - The Compaction tab appears only while the tracker extension is loaded. A session without it never shows tracker-only settings.
162
+ - All five values persist to `tui.json` as `smartCompaction.enabled`, `smartCompaction.classifier`, `smartCompaction.keepContextThreshold`, `smartCompaction.keepContextMinConfidence`, and `smartCompaction.maxCandidates`. The tracker reads these keys.
163
+
164
+ ## 0.5.2
165
+
166
+ ### Patch Changes
167
+
168
+ - a860f5b: Move the Effect packages to the stable `4.0.0` release and the Pi packages to `1.0.0`.
169
+
170
+ - Bump `effect`, `@effect/platform-node`, `@effect/platform-node-shared`, and `@effect/vitest` to `4.0.0`, and `@effect/tsgo` to `0.47.2`.
171
+ - Bump `@earendil-works/pi-ai`, `@earendil-works/pi-coding-agent`, and `@earendil-works/pi-tui` to `1.0.0`.
172
+ - Follow the rc.118 module move: `effect/unstable/process` becomes `effect/process`.
173
+ - Follow the Pi 0.99.0 tool API: tool execution contexts are now `ExtensionToolContext`.
174
+ - `pi-inquiry` gains a `typecheck` script; its two latent type errors are fixed.
175
+
176
+ ## 0.5.1
177
+
178
+ ### Patch Changes
179
+
180
+ - 4e101f0: Pin `@effect/platform-node-shared` and move the Effect packages to `4.0.0-rc.116`.
181
+
182
+ The 0.5.0 packages pinned `effect` and `@effect/platform-node` to `4.0.0-rc.108`. `@effect/platform-node` depends on `@effect/platform-node-shared` with a caret range. Every `4.0.0-rc.*` release shares the `4.0.0` base, so that range resolved to a newer `@effect/platform-node-shared` that imports `effect/ByteSize`. The pinned `effect` has no `ByteSize` module, so loading an extension failed with `Cannot find module '.../effect/dist/ByteSize.js'`.
183
+
184
+ - Move `effect`, `@effect/platform-node`, and `@effect/vitest` to `4.0.0-rc.116`.
185
+ - Add `@effect/platform-node-shared` to the `effect` catalog at an exact version, and depend on it directly from `pi-tui` and `pi-tracker`. This stops the transitive range from floating to a release that needs a newer `effect`.
186
+
187
+ ## 0.5.0
188
+
189
+ ## 0.4.0
190
+
191
+ ### Minor Changes
192
+
193
+ - c89b1a6: Support inline image previews in the `preview` tool and the `/preview` command.
194
+
195
+ - Render PNG, JPEG, GIF, WebP, and AVIF files inline via the terminal graphics protocol; the image bytes live only in the TUI and never reach the model's context.
196
+ - Add an AVIF `ispe` box parser for dimensions, which pi-tui's `Image` component does not yet provide; other formats are sized from their base64 payload.
197
+ - Base64-encode image bytes with Web APIs only, chunked so large files do not overflow the call stack.
198
+
199
+ ### Patch Changes
200
+
201
+ - 2d3c636: Break the footer timer into live model-inference and tool-execution time.
202
+
203
+ - Replace the working/done timer's hand-rolled interval fields with a pure, clock-injected activity state machine (`ActivityTracker`): every instant of an agent run is charged to exactly one of four exclusive buckets — inference, tool, wait, or implicit overhead — preserving the waiting-exclusion behavior from the previous changeset.
204
+ - While a run is open, the timer segment shows the total plus a split of inference and tool time with one glyph per bucket (`~ 2s > 1s`); the split yields before the total on narrow widths. A finished run keeps a frozen split next to the done total.
205
+ - Subscribe to pi `message_start`/`message_end` (assistant spans) and `tool_execution_start`/`tool_execution_end` (call-id-paired executions, one union interval for parallel tools) to populate the split live; `session_start`/`session_shutdown` reset it.
206
+
207
+ - a113d38: Refine the footer working/done timer to exclude time spent waiting on blocking `ctx.ui` prompts.
208
+
209
+ - Subscribe to pi 0.84.4+ `ui_prompt_start`/`ui_prompt_end` events so wall-clock spans where the agent waits on the user (`select`, `confirm`, `input`, `editor`, `custom`) stop counting toward `working` and the finished `done` duration.
210
+ - Show a `waiting` label with the wait duration while a prompt is open.
211
+ - Bump the pi catalog to 0.84.4 so the new event types are available.
212
+
213
+ ## 0.3.0
214
+
215
+ ## 0.2.1
216
+
217
+ ### Patch Changes
218
+
219
+ - Fix bundling issue of relying on ioredis when peer dep isn't needed
220
+
221
+ ## 0.2.0
222
+
223
+ ### Minor Changes
224
+
225
+ - fe0ac01: Solidify initial tooling, visuals, and configurability between packages
package/README.md CHANGED
@@ -5,6 +5,11 @@ House TUI components and the pi interface extension, built on the
5
5
 
6
6
  Package: `@ftrdotdev/pi-tui` · [MIT](LICENSE)
7
7
 
8
+ <img src="https://raw.githubusercontent.com/felixnorden/pi-mono/main/docs/images/pi-tui.png" width="880" alt="A pi session with the house header: a bordered box with the pi logo and the line Let's make some Pi(e), above the bordered editor and the two-line footer">
9
+
10
+ The house chrome in a live session: the logo header, the bordered editor, and
11
+ the footer that reports cwd, git state, runtime, context use, model and cost.
12
+
8
13
  ## What it is
9
14
 
10
15
  `pi-tui` has two sides:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ftrdotdev/pi-tui",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "House TUI components and the pi interface extension: themed boxes, a pure box-frame model, and the header/footer/editor/settings/preview chrome",
5
5
  "keywords": [
6
6
  "components",
@@ -12,7 +12,7 @@
12
12
  "terminal",
13
13
  "tui"
14
14
  ],
15
- "homepage": "https://github.com/felixnorden/pi-mono#readme",
15
+ "homepage": "https://github.com/felixnorden/pi-mono/blob/main/packages/tui/README.md",
16
16
  "bugs": {
17
17
  "url": "https://github.com/felixnorden/pi-mono/issues"
18
18
  },
@@ -24,6 +24,7 @@
24
24
  "directory": "packages/tui"
25
25
  },
26
26
  "files": [
27
+ "CHANGELOG.md",
27
28
  "index.ts",
28
29
  "src",
29
30
  "README.md",
@@ -47,32 +48,34 @@
47
48
  "scripts": {
48
49
  "test": "vitest run",
49
50
  "test:watch": "vitest",
50
- "lint": "oxlint && oxlint --type-aware -A all -D typescript/unbound-method",
51
+ "lint": "oxlint",
51
52
  "lint:fix": "oxlint --fix",
52
53
  "fmt": "oxfmt",
54
+ "fmt:check": "oxfmt --check",
53
55
  "typecheck": "tsc --noEmit"
54
56
  },
55
57
  "dependencies": {
56
- "@effect/platform-node": "4.0.0",
57
- "@effect/platform-node-shared": "4.0.0",
58
- "effect": "4.0.0"
58
+ "@earendil-works/pi-schema": "1.6.0",
59
+ "@effect/platform-node": "4.1.5",
60
+ "@effect/platform-node-shared": "4.1.5",
61
+ "effect": "4.1.5"
59
62
  },
60
63
  "devDependencies": {
61
- "@earendil-works/pi-tui": "1.1.0",
62
- "@effect/tsgo": "0.47.2",
63
- "@effect/vitest": "4.0.0",
64
+ "@earendil-works/pi-tui": "1.6.0",
65
+ "@effect/tsgo": "0.73.4",
66
+ "@effect/vitest": "4.1.5",
67
+ "@ftrdotdev/pi-test-kit": "0.0.0",
64
68
  "@types/bun": "latest",
65
- "oxfmt": "^0.63.0",
66
- "oxlint": "1.82.0",
67
- "oxlint-tsgolint": "7.0.2001",
69
+ "oxfmt": "0.80.0",
70
+ "oxlint": "1.94.0",
71
+ "oxlint-tsgolint": "7.0.2008",
68
72
  "typescript": "7.0.2",
69
- "vitest": "^4.1.10"
73
+ "vitest": "^5.1.0"
70
74
  },
71
75
  "peerDependencies": {
72
76
  "@earendil-works/pi-ai": "*",
73
77
  "@earendil-works/pi-coding-agent": "*",
74
- "@earendil-works/pi-tui": "*",
75
- "typebox": "*"
78
+ "@earendil-works/pi-tui": "*"
76
79
  },
77
80
  "pi": {
78
81
  "extensions": [
@@ -0,0 +1,97 @@
1
+ // Each test is its own entry point: it builds a fresh layer for one scenario and
2
+ // runs its effects inside it, so providing there is the pattern the rule allows.
3
+ /* oxlint-disable effecttsgo/strict-effect-provide */
4
+ import { assert, it } from "@effect/vitest";
5
+ import { Effect } from "effect";
6
+ import { ActivityService } from "./activity.ts";
7
+ import { createTuiSessionHost, type TuiSessionHost } from "./layers.ts";
8
+
9
+ const ZERO = { inference: 0, tool: 0, wait: 0, total: 0 };
10
+
11
+ /** Resolves the service over a given host, the way `main` receives it. */
12
+ const svcOver = (host: TuiSessionHost) =>
13
+ Effect.service(ActivityService).pipe(Effect.provide(ActivityService.layerTest(() => host.state)));
14
+
15
+ // ---------------------------------------------------------------------------
16
+ // ActivityService — owns dispatch and the per-session reset of the tracker
17
+ // that lives in FooterState.tracker. The state machine itself stays pure
18
+ // (activity.test.ts); the service is the only writer of the slot.
19
+ // ---------------------------------------------------------------------------
20
+
21
+ it.effect("dispatching a run start opens the current tracker", () =>
22
+ Effect.gen(function* () {
23
+ // Arrange
24
+ const host = createTuiSessionHost();
25
+ const svc = yield* svcOver(host);
26
+
27
+ // Act
28
+ svc.handle({ type: "run_start", now: 1000 });
29
+
30
+ // Assert
31
+ assert.deepStrictEqual(svc.tracker().breakdown(4000), { ...ZERO, total: 3000 });
32
+ }),
33
+ );
34
+
35
+ it.effect("reset installs a fresh tracker and leaves the previous run unreadable", () =>
36
+ Effect.gen(function* () {
37
+ // Arrange
38
+ const host = createTuiSessionHost();
39
+ const svc = yield* svcOver(host);
40
+ svc.handle({ type: "run_start", now: 1000 });
41
+
42
+ // Act
43
+ svc.reset();
44
+
45
+ // Assert
46
+ assert.strictEqual(svc.tracker().isRunOpen(), false);
47
+ assert.deepStrictEqual(svc.tracker().breakdown(4000), ZERO);
48
+ }),
49
+ );
50
+
51
+ it.effect("the state slot and the service return the same instance", () =>
52
+ Effect.gen(function* () {
53
+ // Arrange
54
+ const host = createTuiSessionHost();
55
+ const svc = yield* svcOver(host);
56
+ const before = host.state.tracker;
57
+
58
+ // Act
59
+ svc.reset();
60
+
61
+ // Assert
62
+ assert.notStrictEqual(before, host.state.tracker);
63
+ assert.strictEqual(host.state.tracker, svc.tracker());
64
+ }),
65
+ );
66
+
67
+ it.effect("the tracker is read live, never captured at build", () =>
68
+ Effect.gen(function* () {
69
+ // Arrange
70
+ const host = createTuiSessionHost();
71
+ const svc = yield* svcOver(host);
72
+ const read = () => svc.tracker();
73
+
74
+ // Act
75
+ svc.reset();
76
+ read().handle({ type: "run_start", now: 0 });
77
+
78
+ // Assert
79
+ assert.strictEqual(read().isRunOpen(), true);
80
+ }),
81
+ );
82
+
83
+ it.effect("two hosts do not share a tracker (regression guard, not a red test)", () =>
84
+ Effect.gen(function* () {
85
+ // Arrange
86
+ const firstHost = createTuiSessionHost();
87
+ const secondHost = createTuiSessionHost();
88
+ const first = yield* svcOver(firstHost);
89
+ const second = yield* svcOver(secondHost);
90
+
91
+ // Act
92
+ first.handle({ type: "run_start", now: 1000 });
93
+
94
+ // Assert
95
+ assert.strictEqual(second.tracker().isRunOpen(), false);
96
+ }),
97
+ );
@@ -274,7 +274,7 @@ it("toTrackerEvent feeds the tracker a live inference/tool script", () => {
274
274
  ];
275
275
  for (const event of script) {
276
276
  const mapped = toTrackerEvent(event);
277
- if (mapped) tracker.handle(mapped);
277
+ if (mapped !== undefined) tracker.handle(mapped);
278
278
  }
279
279
  const breakdown = tracker.breakdown(6000);
280
280
  // tool 2000..4000 = 2s; inference 1000..2000 + 4000..5000 = 2s.
package/src/activity.ts CHANGED
@@ -7,6 +7,9 @@
7
7
  // takes no clock, every method takes `now`, and handle() dispatches typed
8
8
  // descriptors. Date.now() lives only in the pi event wiring (index.ts).
9
9
 
10
+ import { Context, Layer } from "effect";
11
+ import type { FooterState } from "./state.ts";
12
+
10
13
  export type MessageRole = "user" | "assistant" | "toolResult";
11
14
 
12
15
  export type ActivityEvent =
@@ -272,3 +275,38 @@ export function toTrackerEvent(source: TrackerEventSource): ActivityEvent | unde
272
275
  now: source.now,
273
276
  };
274
277
  }
278
+
279
+ /**
280
+ * Owns dispatch and the per-session reset of the `ActivityTracker` that lives
281
+ * in `FooterState.tracker`. The service keeps no state of its own: the state
282
+ * slot is the single owner, so plan 1's frozen port type
283
+ * (`activity: () => FooterState["tracker"]`) and this facade read the same
284
+ * object.
285
+ */
286
+ export class ActivityService extends Context.Service<
287
+ ActivityService,
288
+ {
289
+ /** The current tracker. Synchronous: the footer render reads it per frame. */
290
+ readonly tracker: () => ActivityTracker;
291
+ readonly handle: (event: ActivityEvent) => void;
292
+ /** Per-session reset: a new machine, installed in the live state slot. */
293
+ readonly reset: () => void;
294
+ }
295
+ >()("@ftrdotdev/pi-tui/activity/ActivityService") {
296
+ static make(getState: () => FooterState): Layer.Layer<ActivityService> {
297
+ return Layer.sync(ActivityService, () => {
298
+ const handle = (event: ActivityEvent): void => {
299
+ getState().tracker.handle(event);
300
+ };
301
+ return ActivityService.of({
302
+ tracker: () => getState().tracker,
303
+ handle,
304
+ reset: () => {
305
+ getState().tracker = new ActivityTracker();
306
+ },
307
+ });
308
+ });
309
+ }
310
+
311
+ static readonly layerTest = (getState: () => FooterState) => ActivityService.make(getState);
312
+ }