oc-flight-deck 0.6.3 → 0.6.7
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.
- package/README.md +21 -7
- package/flight-deck.example.jsonc +49 -11
- package/flight-deck.schema.json +76 -0
- package/package.json +1 -1
- package/src/tui/active-elapsed.ts +125 -0
- package/src/tui/config.ts +212 -16
- package/src/tui/format.ts +22 -4
- package/src/tui/index.tsx +28 -10
- package/src/tui/presentation.ts +84 -34
- package/src/tui/rows.ts +1 -1
- package/src/tui/session-reads.ts +60 -10
- package/src/tui/session-tps.ts +49 -32
- package/src/tui/stat-fields.ts +14 -4
- package/src/tui/stats.ts +9 -2
- package/src/tui/style.ts +130 -0
- package/src/tui/throughput.ts +164 -52
package/README.md
CHANGED
|
@@ -37,11 +37,11 @@ cost $0.245 · 2 subagents
|
|
|
37
37
|
tokens 533k in · 91k out
|
|
38
38
|
cache 98% hit · 32M read
|
|
39
39
|
context ██░░░░░░░░ 18%
|
|
40
|
-
elapsed
|
|
40
|
+
elapsed 2h 14m 37s
|
|
41
41
|
tps 18 tok/s
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
-
Every row is read from the open session at render time — except `caution`, which watches a clock rather than events (a hang emits none), and the opt-in `guard` row, which is polled from the local guard RPC.
|
|
44
|
+
Every row is read from the open session at render time — except `caution`, which watches a clock rather than events (a hang emits none); `elapsed`, which is seeded once from the session's own recorded assistant turn spans and then accumulates the busy windows this run observes, rather than a value the session reports; and the opt-in `guard` row, which is polled from the local guard RPC.
|
|
45
45
|
|
|
46
46
|
| Row | What it shows |
|
|
47
47
|
|---|---|
|
|
@@ -57,8 +57,8 @@ Every row is read from the open session at render time — except `caution`, whi
|
|
|
57
57
|
| `cache` | Hit rate first — the number that explains the bill — then cache reads |
|
|
58
58
|
| `context` | A gauge of how full the window is |
|
|
59
59
|
| `perms` | What is waiting for approval, not just how many |
|
|
60
|
-
| `elapsed` |
|
|
61
|
-
| `tps` |
|
|
60
|
+
| `elapsed` | Active time — the clock runs only while this session, a subagent in its tree, or one of its shells is working, and freezes when everything settles. It is seeded once from the assistant turn spans the host already recorded for that session, so it survives a restart instead of resetting to `—`; work observed live after that keeps counting in memory. It measures active time, not wall-clock since the session began |
|
|
61
|
+
| `tps` | Average speed **while working**: output tokens divided by the time the assistant's turns actually ran, subagents included. Idle time between turns is never counted, so the figure freezes when everything settles instead of decaying or hiding; a turn still in flight counts up to now. On a host that exposes no per-message timestamps it falls back to the lifetime average, which includes idle and can therefore sag. Not configurable |
|
|
62
62
|
| `spark` | Recent turn sizes as a shape · **off by default** |
|
|
63
63
|
| `reasoning` | Reasoning tokens, when the model emits them · **off by default** |
|
|
64
64
|
| `turns` | How many prompts you've sent this session · **off by default** |
|
|
@@ -106,19 +106,33 @@ The file is optional, and a missing file is normal and silent: with no file at a
|
|
|
106
106
|
|
|
107
107
|
> **Upgrading from 0.4.0 — the config file moved.** The per-project search is gone. A `flight-deck.jsonc` in a project root or in `.opencode/` is no longer read; move it to `~/.config/opencode/flight-deck.jsonc` (or the `$XDG_CONFIG_HOME` path above) to keep your settings.
|
|
108
108
|
|
|
109
|
-
`sidebar.rows` picks the rows and their order; `
|
|
109
|
+
`sidebar.rows` picks the rows and their order; `sidebar.maxLines` caps the whole rail (fixed lines plus rows, default 24, configurable from 1 to 24); `layout.labelWidth` fits your terminal; `format.duration` switches the `elapsed` row between `spaced` (`2h 14m 37s`, the default) and `compact` (`2h14m37s`).
|
|
110
|
+
|
|
111
|
+
Optional styling lives under `style`: `style.lines` controls fixed branding/separator lines, `style.rows."*"` sets every live row, and `style.rows.cost` (or another row name) overrides one field while inheriting omitted values from the wildcard. Colors are theme roles - `default`, `subdued`, `warning`, `error`, `success`, `info` - and attributes are OpenTUI descriptors: `bold`, `dim`, `italic`, `underline`, `blink`, `inverse`, `hidden`, `strikethrough`. Defaults keep the existing theme-native look; no ANSI escapes or raw colors are needed. Invalid colors, attributes, and row names are reported and safely ignored.
|
|
112
|
+
|
|
113
|
+
Everything else lives in the example file, documented inline - a typo is never fatal: the bad value is ignored, the default comes back, and you get a one-time toast naming the key to fix.
|
|
110
114
|
|
|
111
115
|
```jsonc
|
|
112
116
|
// ~/.config/opencode/flight-deck.jsonc
|
|
113
117
|
{
|
|
114
118
|
"refresh": 100,
|
|
115
119
|
"sidebar": {
|
|
120
|
+
"maxLines": 24,
|
|
116
121
|
"rows": [
|
|
117
122
|
"caution", "status", "agent", "model", "branch", "cost", "project",
|
|
118
123
|
"tokens", "cache", "context", "perms", "elapsed", "tps"
|
|
119
124
|
]
|
|
120
125
|
},
|
|
121
|
-
|
|
126
|
+
// spaced separates the units (2h 14m 37s, the default); "compact" hugs them (2h14m37s)
|
|
127
|
+
"format": { "duration": "spaced" },
|
|
128
|
+
"caution": { "toast": false },
|
|
129
|
+
"style": {
|
|
130
|
+
"lines": { "color": "default", "attributes": [] },
|
|
131
|
+
"rows": {
|
|
132
|
+
"*": { "color": "subdued", "attributes": [] },
|
|
133
|
+
"cost": { "color": "success", "attributes": ["bold"] }
|
|
134
|
+
}
|
|
135
|
+
}
|
|
122
136
|
}
|
|
123
137
|
```
|
|
124
138
|
|
|
@@ -130,7 +144,7 @@ Flight Deck shows what OpenCode already knows.
|
|
|
130
144
|
|
|
131
145
|
- **No network calls while the opt-in `guard` row stays off.** Nothing is fetched, nothing is sent. With `guard` enabled, the panel polls the local guard RPC — same machine, no telemetry — about every ten seconds, plus right away when the rendered session changes.
|
|
132
146
|
- **No telemetry.** Nothing is collected or phoned home.
|
|
133
|
-
- **Nothing on disk.** The one thing it normally writes is an animation counter in the host's in-memory plugin state, so the spinner and `elapsed` keep moving between
|
|
147
|
+
- **Nothing on disk.** The one thing it normally writes is an animation counter in the host's in-memory plugin state, so the spinner and a running `elapsed` clock keep moving between host events. It is scoped to this plugin and dies with the TUI; it is never persisted, and `"refresh": 0` removes even that. With `"refresh": 0` the rail re-reads only on host events, so a busy stretch that produces few events can undercount `elapsed` — no unobserved gap bills beyond its bound, and a longer gap banks nothing. With `guard` enabled there is a second in-memory write — that row's polled status — and `"refresh": 0` does not remove it; dropping `guard` from `sidebar.rows` does.
|
|
134
148
|
- **No polling of your session while `guard` stays off.** Cost, tokens, and permissions update from the host's own events. The timer only re-reads state the host already holds in memory, so the clock-derived rows keep moving.
|
|
135
149
|
- **Theme-native.** Every line uses your active theme's text tokens, so it blends with whatever look you already run.
|
|
136
150
|
|
|
@@ -46,7 +46,8 @@
|
|
|
46
46
|
// 100 default — the spinner turns once a second, the clock stays live
|
|
47
47
|
// 50 twice as lively, still negligible work per tick
|
|
48
48
|
// 1000 a calm wall-clock tick, but the spinner visibly stutters
|
|
49
|
-
// 0 no timer; `elapsed` and the spinner only move when the session does
|
|
49
|
+
// 0 no timer; `elapsed` and the spinner only move when the session does,
|
|
50
|
+
// so a busy stretch with few host events can undercount the clock
|
|
50
51
|
//
|
|
51
52
|
// Anything faster than 16 (one frame at 60Hz) is clamped: a tick wakes the
|
|
52
53
|
// host's renderer, so a smaller value spends work without adding motion.
|
|
@@ -59,8 +60,8 @@
|
|
|
59
60
|
|
|
60
61
|
// Fixed lines, rendered above the live rows. This is the only text Flight
|
|
61
62
|
// Deck shows of its own — put your own words here, or a rule.
|
|
62
|
-
//
|
|
63
|
-
//
|
|
63
|
+
// Fixed lines use style.lines; live rows use their style.rows entry or the
|
|
64
|
+
// wildcard. With no custom style, the shipped theme-native look is kept.
|
|
64
65
|
"lines": ["✈ FLIGHT DECK", "─────────────────"],
|
|
65
66
|
|
|
66
67
|
// The live rows, top to bottom. Every value is read from the open session,
|
|
@@ -81,11 +82,19 @@
|
|
|
81
82
|
// cache cache hit rate, then cache reads
|
|
82
83
|
// context a gauge of how full the context window is
|
|
83
84
|
// perms what is waiting for approval, not just how many
|
|
84
|
-
// elapsed
|
|
85
|
-
//
|
|
86
|
-
//
|
|
87
|
-
//
|
|
88
|
-
//
|
|
85
|
+
// elapsed active time: the clock runs only while this session — or a
|
|
86
|
+
// subagent in its tree, or one of its shells — is working, and
|
|
87
|
+
// freezes when everything settles. Seeded once from the
|
|
88
|
+
// assistant turn spans the host already recorded, so it
|
|
89
|
+
// survives a restart instead of resetting; work observed live
|
|
90
|
+
// after that keeps counting in memory
|
|
91
|
+
// tps average speed while working: output tokens divided by the
|
|
92
|
+
// time the assistant's turns actually ran, subagents included.
|
|
93
|
+
// Idle between turns is never counted, so the figure freezes
|
|
94
|
+
// when everything settles rather than decaying or hiding; a
|
|
95
|
+
// turn still in flight counts up to now. On a host with no
|
|
96
|
+
// per-message timestamps it falls back to the lifetime
|
|
97
|
+
// average, which includes idle and can sag
|
|
89
98
|
// spark recent turn sizes as a sparkline
|
|
90
99
|
// reasoning reasoning tokens, when the model emits them
|
|
91
100
|
// turns how many prompts you have sent this session
|
|
@@ -94,8 +103,8 @@
|
|
|
94
103
|
//
|
|
95
104
|
// Every named row renders persistently, so the rail keeps a stable shape:
|
|
96
105
|
// a row with no data yet shows the placeholder below, in the same label
|
|
97
|
-
// column as a live row. The fixed lines and
|
|
98
|
-
//
|
|
106
|
+
// column as a live row. The fixed lines and live rows share the maxLines
|
|
107
|
+
// budget below, and anything past it is dropped.
|
|
99
108
|
"rows": [
|
|
100
109
|
"caution",
|
|
101
110
|
"status",
|
|
@@ -118,7 +127,10 @@
|
|
|
118
127
|
"persist": true,
|
|
119
128
|
|
|
120
129
|
// Value shown for a persistent row with no data yet. Any non-empty string.
|
|
121
|
-
"placeholder": "—"
|
|
130
|
+
"placeholder": "—",
|
|
131
|
+
|
|
132
|
+
// Maximum total height, including fixed lines and live rows. Defaults to 24.
|
|
133
|
+
"maxLines": 24
|
|
122
134
|
},
|
|
123
135
|
|
|
124
136
|
// ── Footer ───────────────────────────────────────────────────────────────
|
|
@@ -196,6 +208,18 @@
|
|
|
196
208
|
"sparkWidth": 12
|
|
197
209
|
},
|
|
198
210
|
|
|
211
|
+
// -- Format ----------------------------------------------------------------
|
|
212
|
+
// How values are written on the rail. Display only: nothing here changes
|
|
213
|
+
// what is measured, only how a number is drawn.
|
|
214
|
+
"format": {
|
|
215
|
+
// How the elapsed row joins its segments.
|
|
216
|
+
// "spaced" one space between units: 2h 14m 37s (the default)
|
|
217
|
+
// "compact" no space between units: 2h14m37s
|
|
218
|
+
// Zero-padding is the same either way: minutes pad when hours are present,
|
|
219
|
+
// seconds pad when minutes are present, so 2h 14m 07s never reads 2h 14m 7s.
|
|
220
|
+
"duration": "spaced"
|
|
221
|
+
},
|
|
222
|
+
|
|
199
223
|
// -- Glyphs ----------------------------------------------------------------
|
|
200
224
|
// The annunciator's characters. Change these if your terminal renders a
|
|
201
225
|
// default as a box or at the wrong width - an ugly but readable panel beats
|
|
@@ -204,5 +228,19 @@
|
|
|
204
228
|
"watch": "▲",
|
|
205
229
|
"caution": "⚠",
|
|
206
230
|
"clear": "○"
|
|
231
|
+
},
|
|
232
|
+
|
|
233
|
+
// -- Style -----------------------------------------------------------------
|
|
234
|
+
// Optional appearance, resolved through your active theme (no ANSI escapes).
|
|
235
|
+
// Colors are theme roles: default, subdued, warning, error, success, info.
|
|
236
|
+
// Attributes: bold, dim, italic, underline, blink, inverse, hidden,
|
|
237
|
+
// strikethrough. The values below are the defaults.
|
|
238
|
+
"style": {
|
|
239
|
+
"lines": { "color": "default", "attributes": [] },
|
|
240
|
+
"rows": {
|
|
241
|
+
"*": { "color": "subdued", "attributes": [] }
|
|
242
|
+
// Uncomment an override to make one row stand out:
|
|
243
|
+
// ,"cost": { "color": "success", "attributes": ["bold"] }
|
|
244
|
+
}
|
|
207
245
|
}
|
|
208
246
|
}
|
package/flight-deck.schema.json
CHANGED
|
@@ -53,6 +53,13 @@
|
|
|
53
53
|
"minLength": 1,
|
|
54
54
|
"default": "—",
|
|
55
55
|
"description": "Value shown for a persistent row with no data yet. Used only when `sidebar.persist` is true. Must be a non-empty string; anything else falls back to the default em dash."
|
|
56
|
+
},
|
|
57
|
+
"maxLines": {
|
|
58
|
+
"type": "integer",
|
|
59
|
+
"minimum": 1,
|
|
60
|
+
"maximum": 24,
|
|
61
|
+
"default": 24,
|
|
62
|
+
"description": "Maximum height of the whole rail, including fixed lines and live rows. Defaults to 24 and may be lowered from 1 to 23; lines past the limit are dropped."
|
|
56
63
|
}
|
|
57
64
|
}
|
|
58
65
|
},
|
|
@@ -157,6 +164,20 @@
|
|
|
157
164
|
}
|
|
158
165
|
},
|
|
159
166
|
|
|
167
|
+
"format": {
|
|
168
|
+
"type": "object",
|
|
169
|
+
"additionalProperties": false,
|
|
170
|
+
"description": "How values are written on the rail. Display only: these options change how a number is drawn, never what is measured.",
|
|
171
|
+
"properties": {
|
|
172
|
+
"duration": {
|
|
173
|
+
"type": "string",
|
|
174
|
+
"enum": ["spaced", "compact"],
|
|
175
|
+
"default": "spaced",
|
|
176
|
+
"description": "How the `elapsed` row joins its segments. `spaced` puts one space between them (`2h 14m 37s`, the default); `compact` runs them together (`2h14m37s`). Zero-padding is identical either way: minutes pad when hours are present, seconds pad when minutes are present (`14m 07s`, never `14m 7s`). An invalid value falls back to `spaced` and is reported."
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
|
|
160
181
|
"glyphs": {
|
|
161
182
|
"type": "object",
|
|
162
183
|
"additionalProperties": false,
|
|
@@ -184,6 +205,61 @@
|
|
|
184
205
|
"description": "Reserved for a future always-visible annunciator state. Not currently drawn: the row is absent when nothing is wrong."
|
|
185
206
|
}
|
|
186
207
|
}
|
|
208
|
+
},
|
|
209
|
+
|
|
210
|
+
"style": {
|
|
211
|
+
"type": "object",
|
|
212
|
+
"additionalProperties": false,
|
|
213
|
+
"description": "Optional per-line text styling. Colors are theme roles, not ANSI codes; attributes use OpenTUI text descriptor names. An absent style keeps the theme-native default appearance.",
|
|
214
|
+
"properties": {
|
|
215
|
+
"lines": {
|
|
216
|
+
"type": "object",
|
|
217
|
+
"additionalProperties": false,
|
|
218
|
+
"default": { "color": "default", "attributes": [] },
|
|
219
|
+
"description": "Appearance shared by the fixed branding and separator lines. Defaults to the theme's primary text color with no attributes.",
|
|
220
|
+
"properties": {
|
|
221
|
+
"color": {
|
|
222
|
+
"type": "string",
|
|
223
|
+
"enum": ["default", "subdued", "warning", "error", "success", "info"],
|
|
224
|
+
"default": "default",
|
|
225
|
+
"description": "Theme color role for fixed lines. Valid roles are default, subdued, warning, error, success, and info; default follows the active theme's primary text color."
|
|
226
|
+
},
|
|
227
|
+
"attributes": {
|
|
228
|
+
"type": "array",
|
|
229
|
+
"items": { "enum": ["bold", "dim", "italic", "underline", "blink", "inverse", "hidden", "strikethrough"] },
|
|
230
|
+
"default": [],
|
|
231
|
+
"description": "OpenTUI text attributes applied to fixed lines. Choose any combination of bold, dim, italic, underline, blink, inverse, hidden, and strikethrough; an empty list adds none."
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
"rows": {
|
|
236
|
+
"type": "object",
|
|
237
|
+
"additionalProperties": {
|
|
238
|
+
"type": "object",
|
|
239
|
+
"additionalProperties": false,
|
|
240
|
+
"description": "Appearance for the wildcard or one live row. Missing fields inherit the wildcard style.",
|
|
241
|
+
"properties": {
|
|
242
|
+
"color": {
|
|
243
|
+
"type": "string",
|
|
244
|
+
"enum": ["default", "subdued", "warning", "error", "success", "info"],
|
|
245
|
+
"default": "subdued",
|
|
246
|
+
"description": "Theme color role for this row. The wildcard defaults to subdued; a row override that omits color inherits the wildcard role."
|
|
247
|
+
},
|
|
248
|
+
"attributes": {
|
|
249
|
+
"type": "array",
|
|
250
|
+
"items": { "enum": ["bold", "dim", "italic", "underline", "blink", "inverse", "hidden", "strikethrough"] },
|
|
251
|
+
"default": [],
|
|
252
|
+
"description": "OpenTUI text attributes for this row. The wildcard defaults to none; a row override that omits attributes inherits the wildcard list."
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
},
|
|
256
|
+
"propertyNames": {
|
|
257
|
+
"enum": ["*", "caution", "status", "agent", "model", "branch", "cost", "total", "project", "tokens", "cache", "context", "perms", "elapsed", "tps", "spark", "reasoning", "turns", "guard"]
|
|
258
|
+
},
|
|
259
|
+
"default": { "*": { "color": "subdued", "attributes": [] } },
|
|
260
|
+
"description": "Style all live rows under `*`, then optionally override any known row name such as `cost`. A row can change one field and inherit the other from `*`; unknown row names are reported and ignored."
|
|
261
|
+
}
|
|
262
|
+
}
|
|
187
263
|
}
|
|
188
264
|
}
|
|
189
265
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Accumulated active time for one session, measured rather than assumed.
|
|
2
|
+
//
|
|
3
|
+
// Wall-clock since `time.created` reports a session left open overnight as a
|
|
4
|
+
// night's work. This clock only advances while the session — or something in
|
|
5
|
+
// its family, or one of its shells — is actually running, and it freezes the
|
|
6
|
+
// instant everything settles. `busy()` decides that, so the `elapsed` and
|
|
7
|
+
// `status` rows can never disagree about whether work is happening.
|
|
8
|
+
//
|
|
9
|
+
// One session is watched at a time. Reading a different session means the gap
|
|
10
|
+
// on the previous one happened off-watch, so its next read rebaselines and
|
|
11
|
+
// banks nothing. A window is billed only when the session was busy at both
|
|
12
|
+
// ends and the window was observed within the bound — so no single unobserved
|
|
13
|
+
// gap is ever billed beyond the optional `maxBankedMs`, and a longer gap banks
|
|
14
|
+
// nothing at all.
|
|
15
|
+
//
|
|
16
|
+
// State is a per-session accumulator held in this process and keyed by the
|
|
17
|
+
// displayed session, so an unrelated project's session can never contribute.
|
|
18
|
+
// The figure starts from a seed the caller may supply: the merged spans of the
|
|
19
|
+
// assistant turns the host already recorded for that session's scope. That is
|
|
20
|
+
// read once, from the host's own timestamps, so a restart comes back with the
|
|
21
|
+
// work already on record instead of zero. Work observed live after that first
|
|
22
|
+
// read keeps accumulating on top in memory, and a host that offers no seed
|
|
23
|
+
// (or no timestamps) starts at zero and behaves exactly as it did before. The
|
|
24
|
+
// seed is asked for once per session, when that session is first seen; an
|
|
25
|
+
// unknown busy state (`undefined`) freezes rather than inventing motion.
|
|
26
|
+
|
|
27
|
+
/** How the caller answers "is this session working right now?". */
|
|
28
|
+
export type BusyRead = (sessionID: string) => boolean | undefined;
|
|
29
|
+
|
|
30
|
+
/** How the caller answers "how much work is already on record for this session?". */
|
|
31
|
+
export type SeedRead = (sessionID: string) => number | undefined;
|
|
32
|
+
|
|
33
|
+
export interface ActiveElapsedOptions {
|
|
34
|
+
/**
|
|
35
|
+
* Longest window that may be banked as work. A longer one was not observed
|
|
36
|
+
* — a suspended process, sparse host events — so it banks nothing.
|
|
37
|
+
*
|
|
38
|
+
* Defaults to 60_000 so the module's own contract holds even when the
|
|
39
|
+
* caller supplies no bound. A production caller still passes its own
|
|
40
|
+
* derived bound.
|
|
41
|
+
*/
|
|
42
|
+
readonly maxBankedMs?: number;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Starting tally for a session, read once — on the first observation of that
|
|
46
|
+
* session — so restart-stable work already on record is not lost. Floored at
|
|
47
|
+
* zero; a non-finite or throwing read is treated as zero, never propagated.
|
|
48
|
+
*/
|
|
49
|
+
readonly seedOf?: SeedRead;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** One session's running tally and the last instant it was observed. */
|
|
53
|
+
interface ActiveState {
|
|
54
|
+
accumulated: number;
|
|
55
|
+
lastAt: number;
|
|
56
|
+
running: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Build the active clock. One session is watched at a time. A read banks the
|
|
61
|
+
* time since the previous read only when the session was running at BOTH ends
|
|
62
|
+
* of that window — so overlapping activities record one wall-clock window, not
|
|
63
|
+
* one per activity, and a window in which the session settled is treated as
|
|
64
|
+
* idle rather than billed. Reads while the tally is zero return `undefined`,
|
|
65
|
+
* matching the row's own "nothing to show yet" behaviour.
|
|
66
|
+
*
|
|
67
|
+
* The observation boundary is unconditional: a read of a different session than
|
|
68
|
+
* the one before it rebaselines and banks nothing, because the gap happened
|
|
69
|
+
* off-watch. The optional `maxBankedMs` is an independent cap on top of that —
|
|
70
|
+
* any window longer than it was unobserved and banks nothing — not a switch for
|
|
71
|
+
* the boundary. Undercount is acceptable, over-count is not.
|
|
72
|
+
*/
|
|
73
|
+
export function createActiveElapsed(isBusy: BusyRead, options: ActiveElapsedOptions = {}) {
|
|
74
|
+
const states = new Map<string, ActiveState>();
|
|
75
|
+
// The module's own contract must hold without the caller opting in; the
|
|
76
|
+
// production caller still passes its own derived bound.
|
|
77
|
+
const maxBankedMs = options.maxBankedMs ?? 60_000;
|
|
78
|
+
let lastRead: string | undefined;
|
|
79
|
+
|
|
80
|
+
return function activeElapsedMs(sessionID: string, now: number): number | undefined {
|
|
81
|
+
if (!Number.isFinite(now)) return undefined;
|
|
82
|
+
|
|
83
|
+
// A throwing or unknown read means "not known to be busy", which freezes.
|
|
84
|
+
let busy = false;
|
|
85
|
+
try {
|
|
86
|
+
busy = isBusy(sessionID) === true;
|
|
87
|
+
} catch {
|
|
88
|
+
busy = false;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Observation boundary: a different session on screen means this one's
|
|
92
|
+
// gap since the last read happened off-watch, so take a fresh baseline.
|
|
93
|
+
const boundary = lastRead !== undefined && lastRead !== sessionID;
|
|
94
|
+
lastRead = sessionID;
|
|
95
|
+
|
|
96
|
+
let state = states.get(sessionID);
|
|
97
|
+
if (state === undefined) {
|
|
98
|
+
// First sight of this session: take the seed once, here, so the record
|
|
99
|
+
// covers everything up to the moment observation begins and the
|
|
100
|
+
// accumulator below covers everything after — no overlap, no double
|
|
101
|
+
// count. Floored at zero; a non-finite or throwing read is zero.
|
|
102
|
+
let seed = 0;
|
|
103
|
+
if (options.seedOf !== undefined) {
|
|
104
|
+
try {
|
|
105
|
+
const value = options.seedOf(sessionID);
|
|
106
|
+
if (typeof value === "number" && Number.isFinite(value) && value > 0) seed = value;
|
|
107
|
+
} catch {
|
|
108
|
+
seed = 0;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
state = { accumulated: seed, lastAt: now, running: false };
|
|
112
|
+
states.set(sessionID, state);
|
|
113
|
+
} else if (!boundary && state.running && busy) {
|
|
114
|
+
// Only a window that was busy at BOTH ends and short enough to have been
|
|
115
|
+
// observed is work. A settled gap is idle; a stalled or stepped-back
|
|
116
|
+
// clock must never subtract; an over-long window was not watched.
|
|
117
|
+
const delta = now - state.lastAt;
|
|
118
|
+
if (delta > 0 && delta <= maxBankedMs) state.accumulated += delta;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
state.running = busy;
|
|
122
|
+
state.lastAt = now;
|
|
123
|
+
return state.accumulated <= 0 ? undefined : state.accumulated;
|
|
124
|
+
};
|
|
125
|
+
}
|