oc-flight-deck 0.6.6 → 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 CHANGED
@@ -41,7 +41,7 @@ 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); `elapsed`, which is a process-local tally of the busy windows this run observed rather than a value the session reports; 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,7 +57,7 @@ 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` | 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 process-local and starts at zero when the plugin starts, so it measures activity observed this run, not wall-clock since the session began |
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
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** |
@@ -84,8 +84,10 @@
84
84
  // perms what is waiting for approval, not just how many
85
85
  // elapsed active time: the clock runs only while this session — or a
86
86
  // subagent in its tree, or one of its shells — is working, and
87
- // freezes when everything settles. Process-local: it starts at
88
- // zero with the plugin and counts only activity this run saw
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
89
91
  // tps average speed while working: output tokens divided by the
90
92
  // time the assistant's turns actually ran, subagents included.
91
93
  // Idle between turns is never counted, so the figure freezes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oc-flight-deck",
3
- "version": "0.6.6",
3
+ "version": "0.6.7",
4
4
  "description": "Live OpenCode V2 session telemetry — agent, model, branch, cost, tokens, cache hit rate and context usage in your sidebar.",
5
5
  "keywords": [
6
6
  "opencode",
@@ -15,14 +15,21 @@
15
15
  //
16
16
  // State is a per-session accumulator held in this process and keyed by the
17
17
  // displayed session, so an unrelated project's session can never contribute.
18
- // That also means the figure begins at zero when the TUI starts: it measures
19
- // activity observed this process, not activity the host recorded. A session
20
- // that is idle when first seen stays at zero, and an unknown busy state
21
- // (`undefined`) freezes rather than inventing motion.
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.
22
26
 
23
27
  /** How the caller answers "is this session working right now?". */
24
28
  export type BusyRead = (sessionID: string) => boolean | undefined;
25
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
+
26
33
  export interface ActiveElapsedOptions {
27
34
  /**
28
35
  * Longest window that may be banked as work. A longer one was not observed
@@ -33,6 +40,13 @@ export interface ActiveElapsedOptions {
33
40
  * derived bound.
34
41
  */
35
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;
36
50
  }
37
51
 
38
52
  /** One session's running tally and the last instant it was observed. */
@@ -81,7 +95,20 @@ export function createActiveElapsed(isBusy: BusyRead, options: ActiveElapsedOpti
81
95
 
82
96
  let state = states.get(sessionID);
83
97
  if (state === undefined) {
84
- state = { accumulated: 0, lastAt: now, running: false };
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 };
85
112
  states.set(sessionID, state);
86
113
  } else if (!boundary && state.running && busy) {
87
114
  // Only a window that was busy at BOTH ends and short enough to have been
@@ -10,6 +10,7 @@
10
10
  import type { Plugin } from "@opencode/plugin/tui";
11
11
  import { createActiveElapsed } from "./active-elapsed.js";
12
12
  import { asCount, asRecord, asText, type SessionLike } from "./coerce.js";
13
+ import { turnKey, turnSpan, unionSpanMs, type ThroughputSpan } from "./stats.js";
13
14
 
14
15
  /**
15
16
  * The location the rail reads shell records and VCS from: the host's current
@@ -218,7 +219,57 @@ export function createSessionReads(context: Plugin.Context, maxBankedMs?: number
218
219
  // Accumulated active wall time: the clock runs only while `busy()` says the
219
220
  // session, its family, or its shells is working, and freezes otherwise. The
220
221
  // `now` parameter keeps the transition observation deterministic for tests.
221
- const activeElapsed = createActiveElapsed(busy, { maxBankedMs });
222
+ //
223
+ // The seed gives that same clock the session's already-recorded work so a
224
+ // restart does not reset the row to `—`. It scans exactly the scope `busy()`
225
+ // consults — the displayed session plus every id `family()` returns — and
226
+ // takes the merged union of those assistant turns' spans, tokens ignored: a
227
+ // zero-output turn still took wall time. The scan is memoised one result per
228
+ // session id, and every read degrades to zero rather than throwing, so an
229
+ // absent family or an unstamped host leaves today's behaviour untouched.
230
+ const seeds = new Map<string, number>();
231
+ const seedElapsed = (sessionID: string): number => {
232
+ const cached = seeds.get(sessionID);
233
+ if (cached !== undefined) return cached;
234
+
235
+ // One `now` for the whole scan, so every in-flight turn ends at the same
236
+ // instant and the union cannot be skewed by the reads drifting apart.
237
+ const now = Date.now();
238
+ const ids = new Set<string>([sessionID]);
239
+ try {
240
+ for (const id of context.data.session.family(sessionID) ?? []) ids.add(id);
241
+ } catch {
242
+ // No family on this host: the session's own turns still stand.
243
+ }
244
+
245
+ let seed = 0;
246
+ try {
247
+ const spans: ThroughputSpan[] = [];
248
+ for (const id of ids) {
249
+ for (const entry of messagesOf(id)) {
250
+ const message = asRecord(entry);
251
+ if (message?.["type"] !== "assistant") continue;
252
+ const stamp = asRecord(message["time"]);
253
+ const created = asCount(stamp?.["created"]);
254
+ const completed = asCount(stamp?.["completed"]);
255
+ const streamed = asCount(stamp?.["streamed"]);
256
+ const span = turnSpan(created, completed, streamed, now);
257
+ if (span === undefined) continue;
258
+ const output = asRecord(message["tokens"])?.["output"];
259
+ spans.push({ key: turnKey(message["id"], created, completed, output), ...span });
260
+ }
261
+ }
262
+ seed = unionSpanMs(spans);
263
+ } catch {
264
+ // A throwing read never reaches the rail: it degrades to today's zero.
265
+ seed = 0;
266
+ }
267
+
268
+ seeds.set(sessionID, seed);
269
+ return seed;
270
+ };
271
+
272
+ const activeElapsed = createActiveElapsed(busy, { maxBankedMs, seedOf: seedElapsed });
222
273
  const sessionElapsed = (sessionID: string, now: number = Date.now()): number | undefined =>
223
274
  activeElapsed(sessionID, now);
224
275
 
package/src/tui/stats.ts CHANGED
@@ -14,5 +14,12 @@ export { clip, formatCost, formatCount, formatDuration, fuelBar, sparkline } fro
14
14
  export { statLine, statRows } from "./rows.js";
15
15
  export { ANIMATED_FIELDS, DEFAULT_PLACEHOLDER, STAT_FIELDS, isStatField } from "./stat-fields.js";
16
16
  export type { StatSource } from "./stat-fields.js";
17
- export { sessionThroughput, turnKey, turnSpan, unionSpanThroughput, unionSpanTotals } from "./throughput.js";
17
+ export {
18
+ sessionThroughput,
19
+ turnKey,
20
+ turnSpan,
21
+ unionSpanMs,
22
+ unionSpanThroughput,
23
+ unionSpanTotals,
24
+ } from "./throughput.js";
18
25
  export type { SpanTotals, ThroughputSpan } from "./throughput.js";
@@ -82,6 +82,41 @@ export interface SpanTotals {
82
82
  readonly unionMs: number;
83
83
  }
84
84
 
85
+ /**
86
+ * The merged length of already-accepted spans.
87
+ *
88
+ * The shared second half of both unions: it pre-sorts so the result cannot
89
+ * depend on the order the host returned the spans, then merges overlapping and
90
+ * touching spans (`next.start <= openEnd`, so a span that starts exactly when
91
+ * another ends is continuous work) into one. `0` when nothing was accepted.
92
+ */
93
+ function mergedSpanMs(accepted: Array<{ start: number; end: number }>): number {
94
+ // Pre-sort so the merge cannot depend on the order the host returned them.
95
+ accepted.sort((a, b) => a.start - b.start || a.end - b.end);
96
+ const first = accepted[0];
97
+ if (first === undefined) return 0;
98
+
99
+ let unionMs = 0;
100
+ let openStart = first.start;
101
+ let openEnd = first.end;
102
+ for (let index = 1; index < accepted.length; index += 1) {
103
+ const next = accepted[index];
104
+ if (next === undefined) continue;
105
+ // Touching spans (`next.start === openEnd`) merge as well as overlapping
106
+ // ones: a turn that starts exactly when another ends is continuous work.
107
+ if (next.start <= openEnd) {
108
+ if (next.end > openEnd) openEnd = next.end;
109
+ } else {
110
+ unionMs += openEnd - openStart;
111
+ openStart = next.start;
112
+ openEnd = next.end;
113
+ }
114
+ }
115
+ unionMs += openEnd - openStart;
116
+
117
+ return unionMs;
118
+ }
119
+
85
120
  /**
86
121
  * Sum the tokens of distinct turns and the union length of their spans.
87
122
  *
@@ -112,30 +147,36 @@ export function unionSpanTotals(spans: readonly ThroughputSpan[]): SpanTotals {
112
147
  tokens += output;
113
148
  }
114
149
 
115
- // Pre-sort so the merge cannot depend on the order the host returned them.
116
- accepted.sort((a, b) => a.start - b.start || a.end - b.end);
117
- const first = accepted[0];
118
- if (first === undefined) return { tokens, unionMs: 0 };
150
+ return { tokens, unionMs: mergedSpanMs(accepted) };
151
+ }
119
152
 
120
- let unionMs = 0;
121
- let openStart = first.start;
122
- let openEnd = first.end;
123
- for (let index = 1; index < accepted.length; index += 1) {
124
- const next = accepted[index];
125
- if (next === undefined) continue;
126
- // Touching spans (`next.start === openEnd`) merge as well as overlapping
127
- // ones: a turn that starts exactly when another ends is continuous work.
128
- if (next.start <= openEnd) {
129
- if (next.end > openEnd) openEnd = next.end;
130
- } else {
131
- unionMs += openEnd - openStart;
132
- openStart = next.start;
133
- openEnd = next.end;
134
- }
153
+ /**
154
+ * The union length of distinct turn spans, with tokens ignored entirely.
155
+ *
156
+ * The same identity rule, clamp, pre-sort and merge as `unionSpanTotals`, but
157
+ * a turn's output count never enters the decision: a zero-output turn still
158
+ * took wall time and still contributes its span. This is what seeds `elapsed`,
159
+ * which must not undercount real work the way a token-gated union would.
160
+ * A missing key is skipped; the first-seen key wins; a span without a usable
161
+ * start and end is skipped; `0` when nothing is usable.
162
+ */
163
+ export function unionSpanMs(spans: readonly ThroughputSpan[]): number {
164
+ const seen = new Set<string>();
165
+ const accepted: Array<{ start: number; end: number }> = [];
166
+
167
+ for (const span of spans) {
168
+ const key = asText(span.key);
169
+ if (key === undefined || seen.has(key)) continue;
170
+
171
+ const start = asCount(span.start);
172
+ const end = asCount(span.end);
173
+ if (start === undefined || end === undefined) continue;
174
+
175
+ seen.add(key);
176
+ accepted.push({ start, end: Math.max(start, end) });
135
177
  }
136
- unionMs += openEnd - openStart;
137
178
 
138
- return { tokens, unionMs };
179
+ return mergedSpanMs(accepted);
139
180
  }
140
181
 
141
182
  /**