oc-flight-deck 0.6.3 → 0.6.6
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 +47 -11
- package/flight-deck.schema.json +76 -0
- package/package.json +1 -1
- package/src/tui/active-elapsed.ts +98 -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 +9 -10
- package/src/tui/session-tps.ts +49 -32
- package/src/tui/stat-fields.ts +14 -4
- package/src/tui/stats.ts +2 -2
- package/src/tui/style.ts +130 -0
- package/src/tui/throughput.ts +122 -51
package/src/tui/session-tps.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
// Session throughput, measured from
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// host proven to expose no message timestamps uses
|
|
1
|
+
// Session throughput, measured from the host's own message timestamps and never
|
|
2
|
+
// from a clock this process keeps. A host that stamps its assistant turns gets
|
|
3
|
+
// an active-work average: output tokens divided by the union of the turns' own
|
|
4
|
+
// spans, so idle between turns is not in the denominator and the figure freezes
|
|
5
|
+
// once everything settles. A host proven to expose no message timestamps uses
|
|
6
|
+
// the lifetime average instead.
|
|
6
7
|
//
|
|
7
8
|
// The capability is learned once and remembered for the process. `undefined`
|
|
8
9
|
// means "not yet known": a read that failed, or carried no assistant turns,
|
|
@@ -14,26 +15,31 @@
|
|
|
14
15
|
//
|
|
15
16
|
// The rail re-runs on the fast tick, and walking every family message for
|
|
16
17
|
// every session would repeat the same work fifty times a second, so the
|
|
17
|
-
// result is held briefly. `undefined` is cached too: an idle
|
|
18
|
+
// result is held briefly. `undefined` is cached too: an idle session must not
|
|
18
19
|
// be re-derived on each tick either.
|
|
19
20
|
//
|
|
20
21
|
// One factory closes over the plugin `context`; `isFamilyRoot` comes from
|
|
21
22
|
// ./session-reads.js so a child session keeps its own figure while a root
|
|
22
|
-
// sums its tree. The math itself lives in ./throughput.js (pure)
|
|
23
|
-
//
|
|
23
|
+
// sums its tree. The math itself lives in ./throughput.js (pure).
|
|
24
|
+
//
|
|
25
|
+
// RESIDUAL RISK, disclosed: the lifetime fallback for an unstamped host still
|
|
26
|
+
// includes idle time and can therefore sag below a peak per-turn rate. There is
|
|
27
|
+
// no timestamp on such a host with which to exclude it.
|
|
24
28
|
|
|
25
29
|
import type { Plugin } from "@opencode/plugin/tui";
|
|
26
30
|
import { asCount, asRecord, type SessionLike } from "./coerce.js";
|
|
27
|
-
import { sessionThroughput,
|
|
31
|
+
import { sessionThroughput, turnKey, turnSpan, unionSpanThroughput, type ThroughputSpan } from "./stats.js";
|
|
28
32
|
|
|
29
|
-
/** What one session's message list contributes to
|
|
33
|
+
/** What one session's message list contributes to its active-work average. */
|
|
30
34
|
interface TpsScan {
|
|
31
35
|
/** False only when the host refused to list the session's messages. */
|
|
32
36
|
readonly ok: boolean;
|
|
33
37
|
/** Assistant messages seen, whether or not they carried a timestamp. */
|
|
34
38
|
readonly assistant: number;
|
|
35
|
-
/**
|
|
36
|
-
readonly
|
|
39
|
+
/** Assistant turns that carried any usable timestamp, which proves the host stamps. */
|
|
40
|
+
readonly stamped: number;
|
|
41
|
+
/** Spans for turns that carried a usable `created` start. */
|
|
42
|
+
readonly spans: readonly ThroughputSpan[];
|
|
37
43
|
}
|
|
38
44
|
|
|
39
45
|
export function createTpsReader(
|
|
@@ -46,29 +52,36 @@ export function createTpsReader(
|
|
|
46
52
|
| { readonly sessionID: string; readonly at: number; readonly value: number | undefined }
|
|
47
53
|
| undefined;
|
|
48
54
|
|
|
49
|
-
const scanMessages = (id: string): TpsScan => {
|
|
55
|
+
const scanMessages = (id: string, now: number): TpsScan => {
|
|
50
56
|
let messages: readonly unknown[];
|
|
51
57
|
try {
|
|
52
58
|
messages = context.data.session.message.list(id) ?? [];
|
|
53
59
|
} catch {
|
|
54
60
|
// A refused read is not "this host has no messages".
|
|
55
|
-
return { ok: false, assistant: 0,
|
|
61
|
+
return { ok: false, assistant: 0, stamped: 0, spans: [] };
|
|
56
62
|
}
|
|
57
|
-
const
|
|
63
|
+
const spans: ThroughputSpan[] = [];
|
|
58
64
|
let assistant = 0;
|
|
65
|
+
let stamped = 0;
|
|
59
66
|
for (const entry of messages) {
|
|
60
67
|
const message = asRecord(entry);
|
|
61
68
|
if (message?.["type"] !== "assistant") continue;
|
|
62
69
|
assistant += 1;
|
|
63
70
|
const stamp = asRecord(message["time"]);
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
71
|
+
const created = asCount(stamp?.["created"]);
|
|
72
|
+
const completed = asCount(stamp?.["completed"]);
|
|
73
|
+
const streamed = asCount(stamp?.["streamed"]);
|
|
74
|
+
// "Stamped" depends only on the timestamps, never on the output count: a
|
|
67
75
|
// batch of zero-output assistant turns still proves the host stamps.
|
|
68
|
-
if (
|
|
69
|
-
|
|
76
|
+
if (created === undefined && completed === undefined && streamed === undefined) continue;
|
|
77
|
+
stamped += 1;
|
|
78
|
+
// Without `created` there is no start; the turn is skipped, not guessed.
|
|
79
|
+
const span = turnSpan(created, completed, streamed, now);
|
|
80
|
+
if (span === undefined) continue;
|
|
81
|
+
const output = asRecord(message["tokens"])?.["output"];
|
|
82
|
+
spans.push({ key: turnKey(message["id"], created, completed, output), tokens: output, ...span });
|
|
70
83
|
}
|
|
71
|
-
return { ok: true, assistant,
|
|
84
|
+
return { ok: true, assistant, stamped, spans };
|
|
72
85
|
};
|
|
73
86
|
|
|
74
87
|
// The lifetime average, the metric of last resort for a host that exposes no
|
|
@@ -121,26 +134,32 @@ export function createTpsReader(
|
|
|
121
134
|
// lifetime average whether or not the read would have succeeded.
|
|
122
135
|
if (hostStamps === false) return lifetimeTps(sessionID, session, created, time);
|
|
123
136
|
|
|
137
|
+
// One `now` for the whole scope, so every in-flight turn ends at the same
|
|
138
|
+
// instant and the union cannot be skewed by the scans drifting apart.
|
|
139
|
+
const now = Date.now();
|
|
140
|
+
|
|
124
141
|
// Timestamped assistant output this session has produced, plus — only from
|
|
125
142
|
// a family root — every subagent session's. A child asking `family()` gets
|
|
126
|
-
// its ancestors and siblings back, so it keeps its own
|
|
127
|
-
const scans: TpsScan[] = [scanMessages(sessionID)];
|
|
143
|
+
// its ancestors and siblings back, so it keeps its own spans.
|
|
144
|
+
const scans: TpsScan[] = [scanMessages(sessionID, now)];
|
|
128
145
|
if (isFamilyRoot(sessionID)) {
|
|
129
146
|
try {
|
|
130
147
|
for (const id of context.data.session.family(sessionID) ?? []) {
|
|
131
|
-
if (id !== sessionID) scans.push(scanMessages(id));
|
|
148
|
+
if (id !== sessionID) scans.push(scanMessages(id, now));
|
|
132
149
|
}
|
|
133
150
|
} catch {
|
|
134
|
-
// No family on this host: the session's own
|
|
151
|
+
// No family on this host: the session's own turns still stand.
|
|
135
152
|
}
|
|
136
153
|
}
|
|
137
154
|
|
|
138
|
-
const
|
|
155
|
+
const spans: ThroughputSpan[] = [];
|
|
139
156
|
let assistant = 0;
|
|
157
|
+
let stamped = 0;
|
|
140
158
|
let readOk = false;
|
|
141
159
|
for (const scan of scans) {
|
|
142
|
-
|
|
160
|
+
spans.push(...scan.spans);
|
|
143
161
|
assistant += scan.assistant;
|
|
162
|
+
stamped += scan.stamped;
|
|
144
163
|
readOk = readOk || scan.ok;
|
|
145
164
|
}
|
|
146
165
|
|
|
@@ -148,14 +167,12 @@ export function createTpsReader(
|
|
|
148
167
|
// A stamped assistant turn proves the host stamps. Assistant turns with
|
|
149
168
|
// no timestamp prove it does not. Neither, and the capability stays
|
|
150
169
|
// unknown so the row hides rather than guessing a metric.
|
|
151
|
-
if (
|
|
170
|
+
if (stamped > 0) hostStamps = true;
|
|
152
171
|
else if (readOk && assistant > 0) hostStamps = false;
|
|
153
172
|
else return undefined;
|
|
154
173
|
}
|
|
155
174
|
|
|
156
|
-
if (hostStamps === true)
|
|
157
|
-
return windowedThroughput(samples, Date.now(), TPS_WINDOW_MS, created);
|
|
158
|
-
}
|
|
175
|
+
if (hostStamps === true) return unionSpanThroughput(spans);
|
|
159
176
|
return lifetimeTps(sessionID, session, created, time);
|
|
160
177
|
};
|
|
161
178
|
|
|
@@ -170,4 +187,4 @@ export function createTpsReader(
|
|
|
170
187
|
};
|
|
171
188
|
|
|
172
189
|
return { sessionTps };
|
|
173
|
-
}
|
|
190
|
+
}
|
package/src/tui/stat-fields.ts
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
// ticker; `LayoutHint` carries per-call row geometry. Untrusted values are read
|
|
6
6
|
// through ./coerce.js rather than re-checked here.
|
|
7
7
|
|
|
8
|
+
import type { DurationStyle } from "./format.js";
|
|
9
|
+
|
|
8
10
|
/** The subset of a session snapshot the rail reads. All fields are untrusted. */
|
|
9
11
|
export interface StatSource {
|
|
10
12
|
/**
|
|
@@ -28,10 +30,12 @@ export interface StatSource {
|
|
|
28
30
|
readonly busy?: unknown;
|
|
29
31
|
readonly perms?: unknown;
|
|
30
32
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
33
|
+
* Session throughput. On a host that stamps its messages this is an
|
|
34
|
+
* active-work average: output tokens divided by the union of the assistant
|
|
35
|
+
* turns' own spans, subagents summed in, so idle between turns is not
|
|
36
|
+
* counted and the figure freezes when work stops. On a host without message
|
|
37
|
+
* timestamps it falls back to the lifetime average, which can still sag.
|
|
38
|
+
* Either way it is derived from state the host already holds.
|
|
35
39
|
*/
|
|
36
40
|
readonly tps?: unknown;
|
|
37
41
|
readonly elapsedMs?: unknown;
|
|
@@ -95,6 +99,12 @@ export interface LayoutHint {
|
|
|
95
99
|
readonly labelWidth?: number;
|
|
96
100
|
readonly barWidth?: number;
|
|
97
101
|
readonly sparkWidth?: number;
|
|
102
|
+
/**
|
|
103
|
+
* How the `elapsed` row joins its duration segments. The rail supplies this
|
|
104
|
+
* from `format.duration` (default `"spaced"`); when the hint is absent the
|
|
105
|
+
* formatter's own default, `"compact"`, applies.
|
|
106
|
+
*/
|
|
107
|
+
readonly durationStyle?: DurationStyle;
|
|
98
108
|
/**
|
|
99
109
|
* True when the rail already draws a `total` row.
|
|
100
110
|
*
|
package/src/tui/stats.ts
CHANGED
|
@@ -14,5 +14,5 @@ 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,
|
|
18
|
-
export type {
|
|
17
|
+
export { sessionThroughput, turnKey, turnSpan, unionSpanThroughput, unionSpanTotals } from "./throughput.js";
|
|
18
|
+
export type { SpanTotals, ThroughputSpan } from "./throughput.js";
|
package/src/tui/style.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Per-line appearance for the rail: colour roles and text attributes.
|
|
2
|
+
//
|
|
3
|
+
// A config file names a role, not a raw theme token: `default`, `subdued`,
|
|
4
|
+
// `warning`, `error`, `success`, `info`. This module maps a role onto the host
|
|
5
|
+
// theme at render time. The host renamed its text tokens in 2.0.16
|
|
6
|
+
// (`default` -> `base`, `subdued` -> `muted`, feedback `default` -> `base`), so
|
|
7
|
+
// each role prefers the name this plugin has always read and falls back to the
|
|
8
|
+
// renamed one. On a host that provides neither, the colour stays `undefined`
|
|
9
|
+
// and the renderer's own default stands — which is exactly what an absent
|
|
10
|
+
// token has always done.
|
|
11
|
+
//
|
|
12
|
+
// Everything is a table lookup, so the config surface is trivial to test.
|
|
13
|
+
// `attributeMask` is the one place that reads @opentui/core, because the bits
|
|
14
|
+
// must be the vendored renderer's own constants — never re-derived, never ANSI.
|
|
15
|
+
|
|
16
|
+
import { TextAttributes } from "@opentui/core";
|
|
17
|
+
|
|
18
|
+
/** Colours a config may name. Roles, not raw theme tokens. */
|
|
19
|
+
export const STYLE_COLORS = ["default", "subdued", "warning", "error", "success", "info"] as const;
|
|
20
|
+
|
|
21
|
+
export type StyleColor = (typeof STYLE_COLORS)[number];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Text attributes a config may name, exactly the vendored renderer's enum
|
|
25
|
+
* minus `NONE` (an empty list is how "no attributes" is written).
|
|
26
|
+
*/
|
|
27
|
+
export const STYLE_ATTRIBUTES = [
|
|
28
|
+
"bold",
|
|
29
|
+
"dim",
|
|
30
|
+
"italic",
|
|
31
|
+
"underline",
|
|
32
|
+
"blink",
|
|
33
|
+
"inverse",
|
|
34
|
+
"hidden",
|
|
35
|
+
"strikethrough",
|
|
36
|
+
] as const;
|
|
37
|
+
|
|
38
|
+
export type StyleAttribute = (typeof STYLE_ATTRIBUTES)[number];
|
|
39
|
+
|
|
40
|
+
/** One line's look: a named colour plus zero or more attributes. */
|
|
41
|
+
export interface LineStyle {
|
|
42
|
+
readonly color: StyleColor;
|
|
43
|
+
readonly attributes: readonly StyleAttribute[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The `style` config section, resolved. */
|
|
47
|
+
export interface StyleConfig {
|
|
48
|
+
/** The fixed branding/separator lines above the live rows. */
|
|
49
|
+
readonly lines: LineStyle;
|
|
50
|
+
readonly rows: {
|
|
51
|
+
/** Every live row inherits this unless it has its own entry. */
|
|
52
|
+
readonly wildcard: LineStyle;
|
|
53
|
+
/** Per-row entries, only for known row names. */
|
|
54
|
+
readonly overrides: Readonly<Partial<Record<string, LineStyle>>>;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The shipped look: fixed lines in the theme's primary text colour, live rows
|
|
60
|
+
* subdued, no attributes. This is what an absent `style` section must keep.
|
|
61
|
+
*/
|
|
62
|
+
export const DEFAULT_STYLE: StyleConfig = {
|
|
63
|
+
lines: { color: "default", attributes: [] },
|
|
64
|
+
rows: { wildcard: { color: "subdued", attributes: [] }, overrides: {} },
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** The style for one live row: its own entry when it has one, else the wildcard. */
|
|
68
|
+
export function rowStyle(style: StyleConfig, field: string): LineStyle {
|
|
69
|
+
return style.rows.overrides[field] ?? style.rows.wildcard;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The host theme surface the rail reads. Structural on purpose: ./config.ts
|
|
74
|
+
* must not import a renderer, and tests can pass plain values.
|
|
75
|
+
*
|
|
76
|
+
* Both naming generations are declared because the host renamed the text tokens
|
|
77
|
+
* in 2.0.16; a host provides one or the other.
|
|
78
|
+
*/
|
|
79
|
+
export interface ThemeLike {
|
|
80
|
+
readonly text?: {
|
|
81
|
+
readonly default?: unknown;
|
|
82
|
+
readonly subdued?: unknown;
|
|
83
|
+
readonly base?: unknown;
|
|
84
|
+
readonly muted?: unknown;
|
|
85
|
+
readonly feedback?: Readonly<
|
|
86
|
+
Partial<Record<"error" | "warning" | "success" | "info", { readonly default?: unknown; readonly base?: unknown }>>
|
|
87
|
+
>;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The theme colour for a named role, or `undefined` when the host provides
|
|
93
|
+
* neither the current nor the renamed token.
|
|
94
|
+
*/
|
|
95
|
+
export function themeColor(color: StyleColor, theme: ThemeLike): unknown {
|
|
96
|
+
const text = theme.text;
|
|
97
|
+
if (text === undefined) return undefined;
|
|
98
|
+
switch (color) {
|
|
99
|
+
case "default":
|
|
100
|
+
return text.default ?? text.base;
|
|
101
|
+
case "subdued":
|
|
102
|
+
return text.subdued ?? text.muted;
|
|
103
|
+
default: {
|
|
104
|
+
const feedback = text.feedback?.[color];
|
|
105
|
+
return feedback?.default ?? feedback?.base;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Renderer bits for each name, read from the vendored enum rather than
|
|
112
|
+
* re-derived, so a renderer change cannot silently desync the config surface.
|
|
113
|
+
*/
|
|
114
|
+
const ATTRIBUTE_BITS: Readonly<Record<StyleAttribute, number>> = {
|
|
115
|
+
bold: TextAttributes.BOLD,
|
|
116
|
+
dim: TextAttributes.DIM,
|
|
117
|
+
italic: TextAttributes.ITALIC,
|
|
118
|
+
underline: TextAttributes.UNDERLINE,
|
|
119
|
+
blink: TextAttributes.BLINK,
|
|
120
|
+
inverse: TextAttributes.INVERSE,
|
|
121
|
+
hidden: TextAttributes.HIDDEN,
|
|
122
|
+
strikethrough: TextAttributes.STRIKETHROUGH,
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/** The renderer bitmask for a list of attributes, or `undefined` when there are none. */
|
|
126
|
+
export function attributeMask(attributes: readonly StyleAttribute[]): number | undefined {
|
|
127
|
+
let mask = 0;
|
|
128
|
+
for (const attribute of attributes) mask |= ATTRIBUTE_BITS[attribute];
|
|
129
|
+
return mask === 0 ? undefined : mask;
|
|
130
|
+
}
|
package/src/tui/throughput.ts
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
|
-
// Session throughput: output tokens per second,
|
|
2
|
-
//
|
|
1
|
+
// Session throughput: output tokens per second, measured over the active work
|
|
2
|
+
// the host actually recorded rather than over the wall clock.
|
|
3
3
|
//
|
|
4
4
|
// Pure: no clock, no I/O, no rendering. Untrusted sample fields are coerced
|
|
5
5
|
// through ./coerce.js so a garbage value is skipped rather than guessed at.
|
|
6
|
+
//
|
|
7
|
+
// Two rates live here. `sessionThroughput` is the whole-lifetime average, kept
|
|
8
|
+
// as the metric of last resort for a host that exposes no per-message
|
|
9
|
+
// timestamps. `unionSpanThroughput` is the active-work average: output tokens
|
|
10
|
+
// divided by the union of the assistant turns' own spans, so idle between turns
|
|
11
|
+
// is never in the denominator and the figure freezes once everything settles.
|
|
6
12
|
|
|
7
|
-
import { asCount } from "./coerce.js";
|
|
13
|
+
import { asCount, asText } from "./coerce.js";
|
|
8
14
|
|
|
9
15
|
/**
|
|
10
16
|
* Overall session throughput: output tokens over the session's lifetime.
|
|
11
17
|
*
|
|
12
|
-
* Unlike
|
|
13
|
-
* includes every subagent and every pause. A just-started session is floored
|
|
18
|
+
* Unlike the active-work average, this is the whole conversation's average, so
|
|
19
|
+
* it includes every subagent and every pause. A just-started session is floored
|
|
14
20
|
* at one second so it cannot divide by a sliver of time and flash an absurd
|
|
15
21
|
* rate. Returns `undefined` when there is nothing to divide.
|
|
16
22
|
*/
|
|
@@ -22,64 +28,129 @@ export function sessionThroughput(outputTokens: unknown, elapsedMs: unknown): nu
|
|
|
22
28
|
return output / (Math.max(elapsed, 1_000) / 1_000);
|
|
23
29
|
}
|
|
24
30
|
|
|
25
|
-
/** Length of the trailing window, in milliseconds, that `windowedThroughput` measures. */
|
|
26
|
-
export const TPS_WINDOW_MS = 60_000;
|
|
27
|
-
|
|
28
31
|
/**
|
|
29
|
-
* One assistant turn's contribution to
|
|
32
|
+
* One assistant turn's contribution to the active-work average.
|
|
30
33
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
34
|
+
* `key` is the turn's identity (see `turnKey`); `tokens` is its output count;
|
|
35
|
+
* `start` and `end` are its span on the host's epoch clock. Every field is
|
|
36
|
+
* untrusted and coerced before use.
|
|
33
37
|
*/
|
|
34
|
-
export interface
|
|
38
|
+
export interface ThroughputSpan {
|
|
39
|
+
readonly key?: unknown;
|
|
35
40
|
readonly tokens?: unknown;
|
|
36
|
-
readonly
|
|
41
|
+
readonly start?: unknown;
|
|
42
|
+
readonly end?: unknown;
|
|
37
43
|
}
|
|
38
44
|
|
|
39
45
|
/**
|
|
40
|
-
*
|
|
41
|
-
* `windowMs`, measured from `nowMs`.
|
|
42
|
-
*
|
|
43
|
-
* The denominator is how much of the window the session has actually lived, so
|
|
44
|
-
* a just-started session ramps up against its real age instead of being divided
|
|
45
|
-
* by a full minute it has not run — floored at one second, the same floor the
|
|
46
|
-
* lifetime average uses, so a sliver of time cannot flash an absurd rate. Once
|
|
47
|
-
* the session is older than the window, it settles into a clean rolling minute.
|
|
46
|
+
* Resolve one assistant turn's span from its timestamp rungs.
|
|
48
47
|
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
48
|
+
* `start` is `time.created`. `end` is `time.completed`, else `time.streamed`,
|
|
49
|
+
* else the caller's `nowMs` for a turn still in flight. A clock that steps
|
|
50
|
+
* backwards is clamped to a zero-length span rather than a negative one, so
|
|
51
|
+
* skew can never subtract time from the union. Returns `undefined` when there
|
|
52
|
+
* is no usable start — the turn is skipped, never guessed at.
|
|
53
|
+
*/
|
|
54
|
+
export function turnSpan(
|
|
55
|
+
created: unknown,
|
|
56
|
+
completed: unknown,
|
|
57
|
+
streamed: unknown,
|
|
58
|
+
nowMs: unknown,
|
|
59
|
+
): { readonly start: number; readonly end: number } | undefined {
|
|
60
|
+
const start = asCount(created);
|
|
61
|
+
if (start === undefined) return undefined;
|
|
62
|
+
const ended = asCount(completed) ?? asCount(streamed) ?? asCount(nowMs);
|
|
63
|
+
if (ended === undefined) return undefined;
|
|
64
|
+
return { start, end: Math.max(start, ended) };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Identity of one assistant turn: the host's own message id when it carries
|
|
69
|
+
* one, otherwise the tuple `(created, completed, output)` that defines the
|
|
70
|
+
* record. Retries, replays and re-sent records share a key, so they contribute
|
|
71
|
+
* their tokens once and their time once.
|
|
72
|
+
*/
|
|
73
|
+
export function turnKey(id: unknown, created: unknown, completed: unknown, output: unknown): string {
|
|
74
|
+
const own = asText(id);
|
|
75
|
+
if (own !== undefined) return `id:${own}`;
|
|
76
|
+
return `t:${asCount(created) ?? "?"}:${asCount(completed) ?? "?"}:${asCount(output) ?? "?"}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Output tokens and wall time the accepted turns actually cover. */
|
|
80
|
+
export interface SpanTotals {
|
|
81
|
+
readonly tokens: number;
|
|
82
|
+
readonly unionMs: number;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Sum the tokens of distinct turns and the union length of their spans.
|
|
53
87
|
*
|
|
54
|
-
*
|
|
88
|
+
* De-duplicates by `key` first, so a re-sent record adds its tokens once and
|
|
89
|
+
* enters the union once — a dropped duplicate drops both sides together.
|
|
90
|
+
* Spans are clamped (`end >= start`), then sorted and merged so overlapping
|
|
91
|
+
* and touching turns count once and input order cannot change the result. A
|
|
92
|
+
* turn with no usable token count contributes to neither side: the numerator
|
|
93
|
+
* and the denominator always cover exactly the same turns.
|
|
55
94
|
*/
|
|
56
|
-
export function
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
95
|
+
export function unionSpanTotals(spans: readonly ThroughputSpan[]): SpanTotals {
|
|
96
|
+
const seen = new Set<string>();
|
|
97
|
+
const accepted: Array<{ start: number; end: number }> = [];
|
|
98
|
+
let tokens = 0;
|
|
99
|
+
|
|
100
|
+
for (const span of spans) {
|
|
101
|
+
const key = asText(span.key);
|
|
102
|
+
if (key === undefined || seen.has(key)) continue;
|
|
103
|
+
|
|
104
|
+
const start = asCount(span.start);
|
|
105
|
+
const end = asCount(span.end);
|
|
106
|
+
const output = asCount(span.tokens);
|
|
107
|
+
if (start === undefined || end === undefined) continue;
|
|
108
|
+
if (output === undefined || output <= 0) continue;
|
|
109
|
+
|
|
110
|
+
seen.add(key);
|
|
111
|
+
accepted.push({ start, end: Math.max(start, end) });
|
|
112
|
+
tokens += output;
|
|
113
|
+
}
|
|
64
114
|
|
|
65
|
-
//
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
const denominatorMs = Math.max(1_000, Math.min(windowMs, alive));
|
|
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 };
|
|
70
119
|
|
|
71
|
-
|
|
72
|
-
let
|
|
73
|
-
let
|
|
74
|
-
for (
|
|
75
|
-
const
|
|
76
|
-
if (
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
+
}
|
|
81
135
|
}
|
|
136
|
+
unionMs += openEnd - openStart;
|
|
82
137
|
|
|
83
|
-
|
|
84
|
-
|
|
138
|
+
return { tokens, unionMs };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Active-work throughput: output tokens of the scope divided by the union of
|
|
143
|
+
* its active turn spans. Idle time between turns is not in the denominator, so
|
|
144
|
+
* the figure stops moving once everything settles instead of decaying or
|
|
145
|
+
* hiding. A turn still in flight ends at the caller's `now`, so the value keeps
|
|
146
|
+
* climbing while work happens.
|
|
147
|
+
*
|
|
148
|
+
* Floored at one second, the same floor the lifetime average uses, so a sliver
|
|
149
|
+
* of time cannot flash an absurd rate. Returns `undefined` when there is
|
|
150
|
+
* nothing to divide — no positive output, or no span with positive length.
|
|
151
|
+
*/
|
|
152
|
+
export function unionSpanThroughput(spans: readonly ThroughputSpan[]): number | undefined {
|
|
153
|
+
const { tokens, unionMs } = unionSpanTotals(spans);
|
|
154
|
+
if (tokens <= 0 || unionMs <= 0) return undefined;
|
|
155
|
+
return tokens / (Math.max(unionMs, 1_000) / 1_000);
|
|
85
156
|
}
|