paseo-turn-speed 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/OVERVIEW.md +6 -2
- package/README.md +29 -6
- package/client/details.tsx +45 -2
- package/client/links.ts +20 -0
- package/client/meter.ts +87 -5
- package/client/pill.tsx +32 -4
- package/client/team.ts +87 -0
- package/index.client.tsx +201 -6
- package/package.json +1 -1
package/OVERVIEW.md
CHANGED
|
@@ -16,6 +16,10 @@ does report usage.
|
|
|
16
16
|
Options live under Settings → Plugins → Turn speed: what the pill leads with
|
|
17
17
|
(live estimate, decode-window average, or whole-turn rate, switchable from the
|
|
18
18
|
popover), smoothing window for the live number, token format, idle-pill
|
|
19
|
-
visibility, and a manual chars/token override. Requires Paseo 0.11 or later
|
|
19
|
+
visibility, and a manual chars/token override. Requires Paseo 0.11 or later;
|
|
20
|
+
see the README for installing from GitHub or npm.
|
|
20
21
|
Turns are observed live; history from before a plugin reload is not
|
|
21
|
-
reconstructed
|
|
22
|
+
reconstructed. Subagents keep their own pills, but the parent also knows about
|
|
23
|
+
them: time delegated to running subagents is excluded from the parent's
|
|
24
|
+
whole-turn rate, its pill grows a `· N sub` badge, and the popover adds a
|
|
25
|
+
per-subagent breakdown with a combined team rate.
|
package/README.md
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
# turn-speed — live token-rate meter for Paseo agents
|
|
2
2
|
|
|
3
3
|
A composer pill per agent that shows how fast the model is actually decoding:
|
|
4
|
-
`⚡ 138 tok/s
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
`⚡ 138 tok/s` while a turn runs, `⏸ 43 tok/s` while it waits for your answer,
|
|
5
|
+
`12.4 tok/s · 2 sub` while subagents do the heavy lifting, and the final rate
|
|
6
|
+
once the turn ends — with a popover breakdown (prefill/TTFT, in/cached/out
|
|
7
|
+
tokens, steps, tool wait, user wait, per-subagent rates, context occupancy).
|
|
8
|
+
Numbers measured from real provider usage are exact; before the first usage
|
|
9
|
+
event they are character estimates marked with `≈`.
|
|
8
10
|
|
|
9
11
|
## Install
|
|
10
12
|
|
|
@@ -85,13 +87,23 @@ provider config) — it feeds the `context 34k / 131k` line in the popover.
|
|
|
85
87
|
model call are excluded. This is why the pill shows ~139 tok/s on short
|
|
86
88
|
answers instead of 6.
|
|
87
89
|
- **Whole-turn tok/s** — optional alternative basis in settings; it includes
|
|
88
|
-
tool time and is always lower. Time spent waiting for you
|
|
90
|
+
tool time and is always lower. Time spent waiting for you and time delegated
|
|
91
|
+
to subagents are excluded.
|
|
89
92
|
- **Waiting for you** — when the model asks a question or waits for a permission
|
|
90
93
|
or plan approval it is asleep until you answer. Those intervals are taken from
|
|
91
94
|
the provider's permission/question requests (and restored from agent snapshots
|
|
92
95
|
after reloads and reconnects), and are removed from decode windows, tool-wait
|
|
93
96
|
time and the whole-turn rate. `turn time` stays wall-clock; the popover shows a
|
|
94
97
|
separate `waiting for you` row and the pill switches to a ⏸ icon meanwhile.
|
|
98
|
+
- **Delegation to subagents** — while the agent's work runs in subagents, the
|
|
99
|
+
parent's own model produces nothing, so that stretch is removed from its
|
|
100
|
+
whole-turn rate like a user wait. Delegation is detected from the parent's
|
|
101
|
+
timeline (`sub_agent` tool details where the provider reports them, and the
|
|
102
|
+
normalized `subagent` tool calls Paseo emits for provider subagents such as
|
|
103
|
+
OpenCode's Task) and from linked Paseo child agents. The parent's pill grows
|
|
104
|
+
a `· N sub` badge, its accessible title carries the team's combined tok/s,
|
|
105
|
+
and the popover lists every subagent with its own live rate and tokens plus a
|
|
106
|
+
`team` total. Each linked subagent still gets its own pill in its own composer.
|
|
95
107
|
- **Live number inside a model call** — real tokens only arrive at the end of a
|
|
96
108
|
call, so the live value estimates tokens from characters over a 1/2/5 s
|
|
97
109
|
smoothing window and is marked `≈` until the call's usage lands.
|
|
@@ -124,6 +136,9 @@ paseo plugin reload turn-speed
|
|
|
124
136
|
paseo plugin ls # status of every installed plugin
|
|
125
137
|
```
|
|
126
138
|
|
|
139
|
+
Releasing a new version (npm + registry flow, with the gotchas): see
|
|
140
|
+
[RELEASE.md](RELEASE.md).
|
|
141
|
+
|
|
127
142
|
`npm run check:client` compiles the client entry with the same options Paseo's
|
|
128
143
|
compiler uses and rejects output the mobile app's JS engine (Hermes) cannot
|
|
129
144
|
parse — notably anonymous class expressions, which is exactly what a `class`
|
|
@@ -137,7 +152,15 @@ check:client` runs the real Hermes syntax check too.
|
|
|
137
152
|
provider's per-call usage updates. Providers without usage at all stay `≈`.
|
|
138
153
|
- The plugin only counts what it watches live: after a reload or a reconnect the
|
|
139
154
|
interrupted turn is not reconstructed (final `lastUsage` still gives totals).
|
|
140
|
-
- Subagent turns get their own pills
|
|
155
|
+
- Subagent turns get their own pills; the parent's numbers stay its own and
|
|
156
|
+
subagent *tokens* are never added to them — delegation time is excluded and
|
|
157
|
+
team rates are shown alongside, not merged. Paseo-native (delegated) agents
|
|
158
|
+
are linked through their `paseo.parent-agent-id` label, so they get the full
|
|
159
|
+
per-child breakdown. Provider subagents (e.g. OpenCode Task calls) do not
|
|
160
|
+
appear as Paseo agents and their token stream is not reachable from the
|
|
161
|
+
plugin API, so for them the badge, the `delegated` time and the rate
|
|
162
|
+
correction work, but the per-child token breakdown stays empty
|
|
163
|
+
(verified on Paseo 0.12 + OpenCode).
|
|
141
164
|
- Usage that arrives late from a previous turn (typically after an interrupted
|
|
142
165
|
message) is dropped by the plausibility guard, so that turn's token totals can
|
|
143
166
|
be slightly low; rates stay honest.
|
package/client/details.tsx
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { ComponentType } from "react";
|
|
2
|
-
import { useMemo } from "react";
|
|
2
|
+
import { useEffect, useMemo, useReducer, useSyncExternalStore } from "react";
|
|
3
3
|
import { Pressable, Text, View } from "react-native";
|
|
4
|
-
import { useSyncExternalStore } from "react";
|
|
5
4
|
import type { PluginButtonContentProps } from "@getpaseo/plugin/client";
|
|
6
5
|
import { useSettings } from "@getpaseo/plugin/client";
|
|
7
6
|
import { approx, fmtDuration, fmtRate, fmtTokPerSec, fmtTokens } from "./format";
|
|
8
7
|
import type { AgentSpeedStore } from "./store";
|
|
8
|
+
import { EMPTY_TEAM, type TeamView } from "./team";
|
|
9
9
|
import { preferences, type Preferences } from "../shared/preferences";
|
|
10
10
|
|
|
11
11
|
type Styles = ReturnType<typeof buildStyles>;
|
|
@@ -65,6 +65,7 @@ export function makeSpeedDetails(
|
|
|
65
65
|
getPrefs: () => Preferences,
|
|
66
66
|
charsPerToken: () => number,
|
|
67
67
|
onPrefsSaved: () => void,
|
|
68
|
+
getTeamView: () => TeamView | null = () => null,
|
|
68
69
|
): ComponentType<PluginButtonContentProps> {
|
|
69
70
|
return function SpeedDetails({ theme, layout }: PluginButtonContentProps) {
|
|
70
71
|
useSyncExternalStore(store.subscribe, store.getSnapshot);
|
|
@@ -74,6 +75,16 @@ export function makeSpeedDetails(
|
|
|
74
75
|
const now = Date.now();
|
|
75
76
|
const state = store.state;
|
|
76
77
|
const summary = store.summary(now);
|
|
78
|
+
const team = getTeamView() ?? EMPTY_TEAM;
|
|
79
|
+
// Children emit on their own stores; while any runs, a slow tick keeps
|
|
80
|
+
// their numbers moving between the parent's own repaints.
|
|
81
|
+
const [, tick] = useReducer((n: number) => n + 1, 0);
|
|
82
|
+
const teamActive = team.running > 0;
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
if (!teamActive) return undefined;
|
|
85
|
+
const timer = setInterval(() => tick(), 500);
|
|
86
|
+
return () => clearInterval(timer);
|
|
87
|
+
}, [teamActive]);
|
|
77
88
|
const cpt = charsPerToken();
|
|
78
89
|
const running = state.phase === "running";
|
|
79
90
|
const live = running ? store.liveTokPerSec(prefs.liveWindowSec) : null;
|
|
@@ -168,6 +179,38 @@ export function makeSpeedDetails(
|
|
|
168
179
|
value={fmtDuration(summary.userWaitMs)}
|
|
169
180
|
/>
|
|
170
181
|
) : null}
|
|
182
|
+
{team.members.length > 0 || summary.delegated || summary.delegatedMs > 0 ? (
|
|
183
|
+
<Row
|
|
184
|
+
styles={styles}
|
|
185
|
+
label={summary.delegated ? "subagents" : "delegated"}
|
|
186
|
+
value={
|
|
187
|
+
team.running > 0
|
|
188
|
+
? `${team.running} running \u00b7 ${fmtDuration(summary.delegatedMs)}`
|
|
189
|
+
: fmtDuration(summary.delegatedMs)
|
|
190
|
+
}
|
|
191
|
+
/>
|
|
192
|
+
) : null}
|
|
193
|
+
{team.members.slice(0, 5).map((member) => (
|
|
194
|
+
<Row
|
|
195
|
+
key={member.id}
|
|
196
|
+
styles={styles}
|
|
197
|
+
label={member.title ?? member.id.slice(0, 8)}
|
|
198
|
+
value={`${member.running ? fmtTokPerSec(member.liveTokPerSec, member.approx) : "\u2713"} \u00b7 ${approx(
|
|
199
|
+
fmtTokens(member.estTokens, prefs.tokenFormat),
|
|
200
|
+
member.approx,
|
|
201
|
+
)} tok`}
|
|
202
|
+
/>
|
|
203
|
+
))}
|
|
204
|
+
{team.running > 0 && (team.liveTokPerSec !== null || team.estTokens > 0) ? (
|
|
205
|
+
<Row
|
|
206
|
+
styles={styles}
|
|
207
|
+
label="team"
|
|
208
|
+
value={`${fmtTokPerSec(team.liveTokPerSec, team.approx)} \u00b7 ${approx(
|
|
209
|
+
fmtTokens(team.estTokens, prefs.tokenFormat),
|
|
210
|
+
team.approx,
|
|
211
|
+
)} tok`}
|
|
212
|
+
/>
|
|
213
|
+
) : null}
|
|
171
214
|
{summary.totalMs !== null ? (
|
|
172
215
|
<Row styles={styles} label="turn time" value={fmtDuration(summary.totalMs)} />
|
|
173
216
|
) : null}
|
package/client/links.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parent linkage for delegated (subagent) agents. Pure, unit-testable.
|
|
3
|
+
*
|
|
4
|
+
* Paseo marks delegated agents with the `paseo.parent-agent-id` label (the
|
|
5
|
+
* same mechanism the app's Subagents popover uses); a dedicated snapshot
|
|
6
|
+
* field is not delivered on the wire, so the label is the primary source and
|
|
7
|
+
* the field only a defensive fallback.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export const PARENT_AGENT_ID_LABEL = "paseo.parent-agent-id";
|
|
11
|
+
|
|
12
|
+
export function parentAgentIdOf(agent: {
|
|
13
|
+
labels?: Record<string, string> | null;
|
|
14
|
+
parentAgentId?: string | null;
|
|
15
|
+
}): string | null {
|
|
16
|
+
const labeled = agent.labels?.[PARENT_AGENT_ID_LABEL];
|
|
17
|
+
if (typeof labeled === "string" && labeled.trim() !== "") return labeled.trim();
|
|
18
|
+
const field = agent.parentAgentId;
|
|
19
|
+
return typeof field === "string" && field.trim() !== "" ? field.trim() : null;
|
|
20
|
+
}
|
package/client/meter.ts
CHANGED
|
@@ -16,7 +16,11 @@
|
|
|
16
16
|
* Whenever the provider reports the model is blocked on the user (a question
|
|
17
17
|
* or permission request), that interval is tracked separately: it is removed
|
|
18
18
|
* from step decode/wait windows and from the whole-turn rate, while the turn
|
|
19
|
-
* clock itself keeps running in wall time.
|
|
19
|
+
* clock itself keeps running in wall time. While the agent's work runs in
|
|
20
|
+
* subagents (a delegated interval), that time is likewise removed from the
|
|
21
|
+
* whole-turn rate — the subagents' own pills carry their speed — but it stays
|
|
22
|
+
* inside decode/wait windows, because a delegated call is just another tool
|
|
23
|
+
* run between the agent's model calls.
|
|
20
24
|
*/
|
|
21
25
|
|
|
22
26
|
/** A live-window text gap longer than this is treated as a stall. */
|
|
@@ -99,6 +103,10 @@ export interface MeterState {
|
|
|
99
103
|
userWaits: readonly UserWaitInterval[];
|
|
100
104
|
/** Start of the open user-wait interval, null when the model is live. */
|
|
101
105
|
pausedAt: number | null;
|
|
106
|
+
/** Completed intervals where the agent's work ran in subagents. */
|
|
107
|
+
delegatedWaits: readonly UserWaitInterval[];
|
|
108
|
+
/** Start of the open delegation interval, null while the agent runs alone. */
|
|
109
|
+
delegatedAt: number | null;
|
|
102
110
|
/** True once any real usage step landed during this turn. */
|
|
103
111
|
sawUsage: boolean;
|
|
104
112
|
/** Set when a usage delta was dropped as physically impossible. */
|
|
@@ -118,6 +126,10 @@ export type MeterAction =
|
|
|
118
126
|
| { t: "user_wait_started"; at: number }
|
|
119
127
|
/** The user answered: the model woke up. */
|
|
120
128
|
| { t: "user_wait_ended"; at: number }
|
|
129
|
+
/** Subagents of this agent started running: the model is delegated. */
|
|
130
|
+
| { t: "delegated_started"; at: number }
|
|
131
|
+
/** All subagents finished: the agent works alone again. */
|
|
132
|
+
| { t: "delegated_ended"; at: number }
|
|
121
133
|
| { t: "turn_ended"; at: number; kind: "completed" | "failed" | "canceled"; usage?: UsageSnapshot; maxTokPerSec?: number; note?: string | null }
|
|
122
134
|
/** Drop the locally observed turn (epoch replacement, reconnect, reload). */
|
|
123
135
|
| { t: "reset" };
|
|
@@ -146,6 +158,8 @@ export function initialMeterState(): MeterState {
|
|
|
146
158
|
totalChars: 0,
|
|
147
159
|
userWaits: [],
|
|
148
160
|
pausedAt: null,
|
|
161
|
+
delegatedWaits: [],
|
|
162
|
+
delegatedAt: null,
|
|
149
163
|
sawUsage: false,
|
|
150
164
|
stepRejected: null,
|
|
151
165
|
calibInput: null,
|
|
@@ -243,6 +257,45 @@ function closeWait(state: MeterState, at: number): MeterState {
|
|
|
243
257
|
};
|
|
244
258
|
}
|
|
245
259
|
|
|
260
|
+
/** Move the open delegation interval into the completed list. */
|
|
261
|
+
function closeDelegated(state: MeterState, at: number): MeterState {
|
|
262
|
+
return state.delegatedAt === null
|
|
263
|
+
? state
|
|
264
|
+
: {
|
|
265
|
+
...state,
|
|
266
|
+
delegatedAt: null,
|
|
267
|
+
delegatedWaits: [
|
|
268
|
+
...state.delegatedWaits,
|
|
269
|
+
{ start: state.delegatedAt, end: Math.max(at, state.delegatedAt) },
|
|
270
|
+
],
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Milliseconds inside [from, to] covered by at least one interval, counting
|
|
275
|
+
* overlapping intervals once (a user wait during a delegation is one pause,
|
|
276
|
+
* not two). */
|
|
277
|
+
function unionOverlapMs(intervals: readonly UserWaitInterval[], from: number, to: number): number {
|
|
278
|
+
if (to <= from) return 0;
|
|
279
|
+
const clipped = intervals
|
|
280
|
+
.map((wait) => [Math.max(wait.start, from), Math.min(wait.end, to)] as const)
|
|
281
|
+
.filter(([start, end]) => end > start)
|
|
282
|
+
.sort((a, b) => a[0] - b[0]);
|
|
283
|
+
let total = 0;
|
|
284
|
+
let runStart = Number.NEGATIVE_INFINITY;
|
|
285
|
+
let runEnd = Number.NEGATIVE_INFINITY;
|
|
286
|
+
for (const [start, end] of clipped) {
|
|
287
|
+
if (start > runEnd) {
|
|
288
|
+
if (runEnd > runStart) total += runEnd - runStart;
|
|
289
|
+
runStart = start;
|
|
290
|
+
runEnd = end;
|
|
291
|
+
} else if (end > runEnd) {
|
|
292
|
+
runEnd = end;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
if (runEnd > runStart) total += runEnd - runStart;
|
|
296
|
+
return total;
|
|
297
|
+
}
|
|
298
|
+
|
|
246
299
|
function applyUsage(
|
|
247
300
|
state: MeterState,
|
|
248
301
|
at: number,
|
|
@@ -333,8 +386,14 @@ function endTurn(
|
|
|
333
386
|
// A repeated cumulative payload contributes no delta and opens no step.
|
|
334
387
|
const withUsage = usage ? applyUsage(state, at, usage, maxTokPerSec) : state;
|
|
335
388
|
if (withUsage.phase !== "running") return state;
|
|
336
|
-
// A turn that ends while a question is still open closes the wait there
|
|
337
|
-
|
|
389
|
+
// A turn that ends while a question is still open closes the wait there;
|
|
390
|
+
// same for an ongoing delegation.
|
|
391
|
+
return {
|
|
392
|
+
...closeDelegated(closeWait(withUsage, at), at),
|
|
393
|
+
phase: kind,
|
|
394
|
+
endedAt: at,
|
|
395
|
+
endNote: note,
|
|
396
|
+
};
|
|
338
397
|
}
|
|
339
398
|
|
|
340
399
|
export function reduceMeter(state: MeterState, action: MeterAction): MeterState {
|
|
@@ -372,6 +431,13 @@ export function reduceMeter(state: MeterState, action: MeterAction): MeterState
|
|
|
372
431
|
case "user_wait_ended":
|
|
373
432
|
// An end for a wait we never saw (resolved before attach) is a no-op.
|
|
374
433
|
return state.pausedAt === null ? state : closeWait(state, action.at);
|
|
434
|
+
case "delegated_started":
|
|
435
|
+
return state.phase === "running" && state.delegatedAt === null
|
|
436
|
+
? { ...state, delegatedAt: action.at }
|
|
437
|
+
: state;
|
|
438
|
+
case "delegated_ended":
|
|
439
|
+
// An end for a delegation we never saw is a no-op.
|
|
440
|
+
return state.delegatedAt === null ? state : closeDelegated(state, action.at);
|
|
375
441
|
case "turn_ended":
|
|
376
442
|
return endTurn(state, action.at, action.kind, action.usage, action.maxTokPerSec, action.note ?? null);
|
|
377
443
|
case "reset":
|
|
@@ -412,6 +478,10 @@ export interface TurnSummary {
|
|
|
412
478
|
userWaitMs: number;
|
|
413
479
|
/** True while an open user-wait interval is running. */
|
|
414
480
|
waiting: boolean;
|
|
481
|
+
/** Wall milliseconds this turn's work ran inside subagents. */
|
|
482
|
+
delegatedMs: number;
|
|
483
|
+
/** True while at least one subagent is running right now. */
|
|
484
|
+
delegated: boolean;
|
|
415
485
|
tokPerSecDecode: number | null;
|
|
416
486
|
/** Tokens per second over turn time minus user waits. */
|
|
417
487
|
tokPerSecTurn: number | null;
|
|
@@ -460,9 +530,19 @@ export function summarizeTurn(state: MeterState, charsPerToken: number, now: num
|
|
|
460
530
|
let userWaitMs = 0;
|
|
461
531
|
for (const wait of state.userWaits) userWaitMs += Math.max(0, wait.end - wait.start);
|
|
462
532
|
if (state.pausedAt !== null && end !== null) userWaitMs += Math.max(0, end - state.pausedAt);
|
|
533
|
+
let delegatedMs = 0;
|
|
534
|
+
for (const wait of state.delegatedWaits) delegatedMs += Math.max(0, wait.end - wait.start);
|
|
535
|
+
if (state.delegatedAt !== null && end !== null) delegatedMs += Math.max(0, end - state.delegatedAt);
|
|
463
536
|
// The turn's productive time: wall clock minus the stretches where the
|
|
464
|
-
//
|
|
465
|
-
|
|
537
|
+
// agent produced nothing itself — blocked on the user or delegated to
|
|
538
|
+
// subagents. Overlapping stretches count once.
|
|
539
|
+
const asleep: UserWaitInterval[] = [...state.userWaits, ...state.delegatedWaits];
|
|
540
|
+
if (state.pausedAt !== null && end !== null) asleep.push({ start: state.pausedAt, end });
|
|
541
|
+
if (state.delegatedAt !== null && end !== null) asleep.push({ start: state.delegatedAt, end });
|
|
542
|
+
const activeMs =
|
|
543
|
+
totalMs !== null && state.startedAt !== null && end !== null
|
|
544
|
+
? Math.max(0, totalMs - unionOverlapMs(asleep, state.startedAt, end))
|
|
545
|
+
: null;
|
|
466
546
|
const tokPerSecDecode =
|
|
467
547
|
decodeMs > 0 && estTokens > 0 ? (outTokens ?? estTokens) / (decodeMs / 1000) : null;
|
|
468
548
|
const tokPerSecTurn =
|
|
@@ -477,6 +557,8 @@ export function summarizeTurn(state: MeterState, charsPerToken: number, now: num
|
|
|
477
557
|
totalMs,
|
|
478
558
|
userWaitMs,
|
|
479
559
|
waiting: state.pausedAt !== null && state.phase === "running",
|
|
560
|
+
delegatedMs,
|
|
561
|
+
delegated: state.delegatedAt !== null && state.phase === "running",
|
|
480
562
|
tokPerSecDecode,
|
|
481
563
|
tokPerSecTurn,
|
|
482
564
|
steps: state.steps.length,
|
package/client/pill.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { PluginButton, PluginButtonRegistration } from "@getpaseo/plugin/client";
|
|
2
2
|
import { approx, fmtDuration, fmtRate, fmtTokens } from "./format";
|
|
3
3
|
import type { AgentSpeedStore } from "./store";
|
|
4
|
+
import type { TeamView } from "./team";
|
|
4
5
|
import type { Preferences } from "../shared/preferences";
|
|
5
6
|
|
|
6
7
|
const PILL_REFRESH_MS = 500;
|
|
@@ -23,7 +24,12 @@ function headlineTokPerSec(store: AgentSpeedStore, prefs: Preferences, now: numb
|
|
|
23
24
|
return summary.tokPerSecDecode ?? summary.tokPerSecTurn;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
export function computePillView(
|
|
27
|
+
export function computePillView(
|
|
28
|
+
store: AgentSpeedStore,
|
|
29
|
+
prefs: Preferences,
|
|
30
|
+
now: number,
|
|
31
|
+
team?: TeamView | null,
|
|
32
|
+
): PillView {
|
|
27
33
|
const state = store.state;
|
|
28
34
|
const phase = state.phase;
|
|
29
35
|
if (phase === "idle") {
|
|
@@ -40,15 +46,30 @@ export function computePillView(store: AgentSpeedStore, prefs: Preferences, now:
|
|
|
40
46
|
const rateText = rate === null ? "\u2014" : `${approx(fmtRate(rate), isApprox)} tok/s`;
|
|
41
47
|
const running = phase === "running";
|
|
42
48
|
const waiting = summary.waiting;
|
|
49
|
+
const teamRunning = team && phase === "running" && team.running > 0 ? team : null;
|
|
43
50
|
// The composer track is narrow: the pill carries the decode rate only, and
|
|
44
51
|
// the turn state (live vs waiting) is shown solely by the leading icon.
|
|
45
52
|
// Tokens, prefill and wait time live in the popover; the full picture goes
|
|
46
53
|
// into the accessible title.
|
|
47
54
|
const labelParts: string[] = [rateText];
|
|
55
|
+
if (teamRunning) labelParts.push(`\u00b7 ${teamRunning.running} sub`);
|
|
48
56
|
if (phase === "failed") labelParts.push("\u00b7 failed");
|
|
49
57
|
if (phase === "canceled") labelParts.push("\u00b7 canceled");
|
|
50
58
|
const titleParts = [`Turn speed ${rateText} decode`];
|
|
51
59
|
if (waiting) titleParts.push("waiting for you");
|
|
60
|
+
if (teamRunning) {
|
|
61
|
+
const teamRate = teamRunning.liveTokPerSec;
|
|
62
|
+
const teamLabel = `${teamRunning.running} subagent${teamRunning.running === 1 ? "" : "s"}`;
|
|
63
|
+
if (teamRate !== null) {
|
|
64
|
+
titleParts.push(`${teamLabel} at ${approx(fmtRate(teamRate), teamRunning.approx)} tok/s`);
|
|
65
|
+
} else if (teamRunning.estTokens > 0) {
|
|
66
|
+
titleParts.push(
|
|
67
|
+
`${teamLabel} working (${approx(fmtTokens(teamRunning.estTokens, prefs.tokenFormat), teamRunning.approx)} tok)`,
|
|
68
|
+
);
|
|
69
|
+
} else {
|
|
70
|
+
titleParts.push(`${teamLabel} working`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
52
73
|
if (summary.estTokens > 0) {
|
|
53
74
|
titleParts.push(`${approx(fmtTokens(summary.estTokens, prefs.tokenFormat), isApprox)} tok`);
|
|
54
75
|
}
|
|
@@ -76,8 +97,10 @@ export function bindPill(
|
|
|
76
97
|
store: AgentSpeedStore,
|
|
77
98
|
getPrefs: () => Preferences,
|
|
78
99
|
now: () => number,
|
|
100
|
+
getTeam?: () => TeamView | null,
|
|
79
101
|
): PillRegistrationHandle {
|
|
80
|
-
const
|
|
102
|
+
const view = (t: number) => computePillView(store, getPrefs(), t, getTeam ? getTeam() : null);
|
|
103
|
+
const first = view(now());
|
|
81
104
|
let registration = register({ ...buttonBase, ...first });
|
|
82
105
|
let lastPaint = 0;
|
|
83
106
|
let pendingTimer: ReturnType<typeof setTimeout> | null = null;
|
|
@@ -94,8 +117,13 @@ export function bindPill(
|
|
|
94
117
|
return;
|
|
95
118
|
}
|
|
96
119
|
lastPaint = t;
|
|
97
|
-
const
|
|
98
|
-
registration.update({
|
|
120
|
+
const viewNow = view(t);
|
|
121
|
+
registration.update({
|
|
122
|
+
label: viewNow.label,
|
|
123
|
+
icon: viewNow.icon,
|
|
124
|
+
visible: viewNow.visible,
|
|
125
|
+
title: viewNow.title,
|
|
126
|
+
});
|
|
99
127
|
};
|
|
100
128
|
|
|
101
129
|
const unsubscribe = store.subscribe(() => paint(false));
|
package/client/team.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure aggregation of subagent stores into a parent-facing team view.
|
|
3
|
+
*
|
|
4
|
+
* Subagents are ordinary Paseo agents with their own stores; this module
|
|
5
|
+
* only folds those stores together at read time. No subscriptions, no I/O,
|
|
6
|
+
* unit-testable with `node --test`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { AgentSpeedStore } from "./store";
|
|
10
|
+
|
|
11
|
+
export interface TeamMemberInput {
|
|
12
|
+
id: string;
|
|
13
|
+
title: string | null;
|
|
14
|
+
model: string | null;
|
|
15
|
+
store: AgentSpeedStore;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface TeamMemberView {
|
|
19
|
+
id: string;
|
|
20
|
+
title: string | null;
|
|
21
|
+
model: string | null;
|
|
22
|
+
running: boolean;
|
|
23
|
+
liveTokPerSec: number | null;
|
|
24
|
+
estTokens: number;
|
|
25
|
+
approx: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface TeamView {
|
|
29
|
+
members: readonly TeamMemberView[];
|
|
30
|
+
/** Members whose turn phase is running. */
|
|
31
|
+
running: number;
|
|
32
|
+
/** Sum of running members' live rates; null unless at least one has one. */
|
|
33
|
+
liveTokPerSec: number | null;
|
|
34
|
+
/** Sum of running members' estimated turn tokens. */
|
|
35
|
+
estTokens: number;
|
|
36
|
+
/** True when any member's number is character-estimated. */
|
|
37
|
+
approx: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A team with nobody in it. */
|
|
41
|
+
export const EMPTY_TEAM: TeamView = {
|
|
42
|
+
members: [],
|
|
43
|
+
running: 0,
|
|
44
|
+
liveTokPerSec: null,
|
|
45
|
+
estTokens: 0,
|
|
46
|
+
approx: false,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Fold child stores into one view. Non-running members are reported (the
|
|
51
|
+
* popover shows who just finished) but never contribute to the sums.
|
|
52
|
+
*/
|
|
53
|
+
export function summarizeTeam(
|
|
54
|
+
members: readonly TeamMemberInput[],
|
|
55
|
+
windowSec: number,
|
|
56
|
+
now: number,
|
|
57
|
+
): TeamView {
|
|
58
|
+
const views: TeamMemberView[] = [];
|
|
59
|
+
let running = 0;
|
|
60
|
+
let live: number | null = null;
|
|
61
|
+
let estTokens = 0;
|
|
62
|
+
let approx = false;
|
|
63
|
+
for (const member of members) {
|
|
64
|
+
const isRunning = member.store.isRunning();
|
|
65
|
+
const summary = member.store.summary(now);
|
|
66
|
+
const liveTokPerSec = isRunning ? member.store.liveTokPerSec(windowSec) : null;
|
|
67
|
+
views.push({
|
|
68
|
+
id: member.id,
|
|
69
|
+
title: member.title,
|
|
70
|
+
model: member.model,
|
|
71
|
+
running: isRunning,
|
|
72
|
+
liveTokPerSec,
|
|
73
|
+
estTokens: summary.estTokens,
|
|
74
|
+
approx: summary.approx,
|
|
75
|
+
});
|
|
76
|
+
if (!isRunning) continue;
|
|
77
|
+
running += 1;
|
|
78
|
+
estTokens += summary.estTokens;
|
|
79
|
+
approx = approx || summary.approx;
|
|
80
|
+
if (liveTokPerSec !== null) live = (live ?? 0) + liveTokPerSec;
|
|
81
|
+
}
|
|
82
|
+
views.sort((a, b) => {
|
|
83
|
+
if (a.running !== b.running) return a.running ? -1 : 1;
|
|
84
|
+
return b.estTokens - a.estTokens;
|
|
85
|
+
});
|
|
86
|
+
return { members: views, running, liveTokPerSec: live, estTokens, approx };
|
|
87
|
+
}
|
package/index.client.tsx
CHANGED
|
@@ -3,6 +3,8 @@ import { settingsRpc } from "@getpaseo/plugin";
|
|
|
3
3
|
import { createAgentSpeedStore, type AgentSpeedStore } from "./client/store";
|
|
4
4
|
import { bindPill } from "./client/pill";
|
|
5
5
|
import { makeSpeedDetails } from "./client/details";
|
|
6
|
+
import { parentAgentIdOf } from "./client/links";
|
|
7
|
+
import { summarizeTeam, type TeamMemberInput } from "./client/team";
|
|
6
8
|
import { makeSettingsScreen } from "./client/settings";
|
|
7
9
|
import {
|
|
8
10
|
DEFAULT_METER_STATE,
|
|
@@ -29,6 +31,15 @@ interface Attachment {
|
|
|
29
31
|
detach(): void;
|
|
30
32
|
}
|
|
31
33
|
|
|
34
|
+
/** Paseo normalizes subagent-spawning tool calls to the name "subagent";
|
|
35
|
+
* providers that map them richly use the `sub_agent` detail type instead.
|
|
36
|
+
* (Measured on Paseo 0.12 + OpenCode: name="subagent", detail.type="unknown".) */
|
|
37
|
+
const SUB_AGENT_TOOL_NAME = /^sub[-_. ]?agent$/i;
|
|
38
|
+
|
|
39
|
+
function isSubAgentCall(item: { name: string; detail: { type?: string } }): boolean {
|
|
40
|
+
return item.detail.type === "sub_agent" || SUB_AGENT_TOOL_NAME.test(item.name);
|
|
41
|
+
}
|
|
42
|
+
|
|
32
43
|
const CAL_FLUSH_MS = 2000;
|
|
33
44
|
|
|
34
45
|
export default function contribute(client: PluginClientContext) {
|
|
@@ -36,6 +47,13 @@ export default function contribute(client: PluginClientContext) {
|
|
|
36
47
|
const attached = new Map<string, Attachment>();
|
|
37
48
|
const calibration = new Map<string, number>();
|
|
38
49
|
const pendingCalibration = new Map<string, number>();
|
|
50
|
+
// Subagent topology, fed by the agent-list subscription: the latest
|
|
51
|
+
// snapshot per agent, which agent is whose child, which provider session
|
|
52
|
+
// id belongs to which agent, and each attachment's team-change callback.
|
|
53
|
+
const snapshots = new Map<string, AgentSnapshot>();
|
|
54
|
+
const childParents = new Map<string, string>();
|
|
55
|
+
const sessionOwners = new Map<string, string>();
|
|
56
|
+
const teamWatchers = new Map<string, Set<() => void>>();
|
|
39
57
|
let prefs: Preferences = DEFAULT_PREFERENCES;
|
|
40
58
|
let calFlushTimer: ReturnType<typeof setTimeout> | null = null;
|
|
41
59
|
let listSubscription: { release(): Promise<void> } | null = null;
|
|
@@ -45,6 +63,65 @@ export default function contribute(client: PluginClientContext) {
|
|
|
45
63
|
if (!aborted()) console.error("turn-speed:", ...args);
|
|
46
64
|
};
|
|
47
65
|
|
|
66
|
+
const notifyTeam = (parentId: string): void => {
|
|
67
|
+
for (const listener of teamWatchers.get(parentId) ?? []) {
|
|
68
|
+
try {
|
|
69
|
+
listener();
|
|
70
|
+
} catch (error) {
|
|
71
|
+
logError(`team update failed for ${parentId}`, error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const linkChild = (childId: string, parentId: string): void => {
|
|
77
|
+
if (childId === parentId) return;
|
|
78
|
+
const current = childParents.get(childId);
|
|
79
|
+
if (current === parentId) return;
|
|
80
|
+
childParents.set(childId, parentId);
|
|
81
|
+
console.log(`turn-speed: linked child ${childId.slice(0, 8)} -> parent ${parentId.slice(0, 8)}`);
|
|
82
|
+
notifyTeam(parentId);
|
|
83
|
+
if (current) notifyTeam(current);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** Fold a fresh agent snapshot into the topology maps. */
|
|
87
|
+
const remember = (agent: AgentSnapshot): void => {
|
|
88
|
+
const previous = snapshots.get(agent.id);
|
|
89
|
+
snapshots.set(agent.id, agent);
|
|
90
|
+
const sessionId = agent.runtimeInfo?.sessionId ?? agent.persistence?.sessionId ?? null;
|
|
91
|
+
if (sessionId) sessionOwners.set(sessionId, agent.id);
|
|
92
|
+
const parent = parentAgentIdOf(agent);
|
|
93
|
+
if (parent) linkChild(agent.id, parent);
|
|
94
|
+
// A child starting or finishing its turn changes the parent's team even
|
|
95
|
+
// when the link itself did not change. Deferred one microtask so the
|
|
96
|
+
// calling handler finishes seeding the child's own store first.
|
|
97
|
+
if (
|
|
98
|
+
parent &&
|
|
99
|
+
previous &&
|
|
100
|
+
(previous.status !== agent.status || previous.archivedAt !== agent.archivedAt)
|
|
101
|
+
) {
|
|
102
|
+
queueMicrotask(() => notifyTeam(parent));
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/** Live attached children of `parentId`, for the team view. */
|
|
107
|
+
const teamMembers = (parentId: string): TeamMemberInput[] => {
|
|
108
|
+
const members: TeamMemberInput[] = [];
|
|
109
|
+
for (const [childId, pid] of childParents) {
|
|
110
|
+
if (pid !== parentId) continue;
|
|
111
|
+
const attachment = attached.get(childId);
|
|
112
|
+
const snapshot = snapshots.get(childId);
|
|
113
|
+
if (!attachment || !snapshot) continue;
|
|
114
|
+
if (snapshot.archivedAt || snapshot.status === "closed") continue;
|
|
115
|
+
members.push({
|
|
116
|
+
id: childId,
|
|
117
|
+
title: snapshot.title ?? null,
|
|
118
|
+
model: snapshot.model ?? null,
|
|
119
|
+
store: attachment.store,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return members;
|
|
123
|
+
};
|
|
124
|
+
|
|
48
125
|
const effectiveCpt = (model: string | null): number => {
|
|
49
126
|
if (prefs.manualCharsPerToken !== null) return prefs.manualCharsPerToken;
|
|
50
127
|
if (model) {
|
|
@@ -146,11 +223,15 @@ export default function contribute(client: PluginClientContext) {
|
|
|
146
223
|
event: StreamEvent,
|
|
147
224
|
reseed: () => void,
|
|
148
225
|
waits: Set<string>,
|
|
226
|
+
agentId: string,
|
|
227
|
+
subCalls: Set<string>,
|
|
228
|
+
onSubChange: () => void,
|
|
149
229
|
): void {
|
|
150
230
|
const at = Date.now();
|
|
151
231
|
switch (event.type) {
|
|
152
232
|
case "turn_started": {
|
|
153
233
|
waits.clear();
|
|
234
|
+
subCalls.clear();
|
|
154
235
|
const startedAt = handle.activeTurn?.startedAt;
|
|
155
236
|
const parsed = startedAt ? Date.parse(startedAt) : Number.NaN;
|
|
156
237
|
store.dispatch({
|
|
@@ -158,10 +239,12 @@ export default function contribute(client: PluginClientContext) {
|
|
|
158
239
|
at: Number.isFinite(parsed) ? parsed : at,
|
|
159
240
|
turnId: event.turnId ?? handle.activeTurn?.turnId ?? null,
|
|
160
241
|
});
|
|
242
|
+
onSubChange();
|
|
161
243
|
break;
|
|
162
244
|
}
|
|
163
245
|
case "turn_completed":
|
|
164
246
|
waits.clear();
|
|
247
|
+
subCalls.clear();
|
|
165
248
|
store.dispatch({
|
|
166
249
|
t: "turn_ended",
|
|
167
250
|
at,
|
|
@@ -169,15 +252,20 @@ export default function contribute(client: PluginClientContext) {
|
|
|
169
252
|
usage: event.usage ? mapUsage(event.usage) : undefined,
|
|
170
253
|
maxTokPerSec: prefs.maxPlausibleTokPerSec,
|
|
171
254
|
});
|
|
255
|
+
onSubChange();
|
|
172
256
|
void refreshPrefs();
|
|
173
257
|
break;
|
|
174
258
|
case "turn_failed":
|
|
175
259
|
waits.clear();
|
|
260
|
+
subCalls.clear();
|
|
176
261
|
store.dispatch({ t: "turn_ended", at, kind: "failed", note: event.error });
|
|
262
|
+
onSubChange();
|
|
177
263
|
break;
|
|
178
264
|
case "turn_canceled":
|
|
179
265
|
waits.clear();
|
|
266
|
+
subCalls.clear();
|
|
180
267
|
store.dispatch({ t: "turn_ended", at, kind: "canceled", note: event.reason });
|
|
268
|
+
onSubChange();
|
|
181
269
|
break;
|
|
182
270
|
case "permission_requested":
|
|
183
271
|
case "permission_resolved": {
|
|
@@ -210,6 +298,29 @@ export default function contribute(client: PluginClientContext) {
|
|
|
210
298
|
});
|
|
211
299
|
} else if (item.type === "compaction") {
|
|
212
300
|
store.dispatch({ t: "compaction", at });
|
|
301
|
+
} else if (item.type === "tool_call" && isSubAgentCall(item)) {
|
|
302
|
+
// A subagent call: running/closed drives the parent's delegation
|
|
303
|
+
// interval, and its child session id (when the provider reports
|
|
304
|
+
// one) links the child agent to this parent for the team view.
|
|
305
|
+
const detail = item.detail;
|
|
306
|
+
console.log(
|
|
307
|
+
`turn-speed: subagent call [${agentId.slice(0, 8)}]` +
|
|
308
|
+
` ${String(item.callId).slice(0, 8)} ${item.status}`,
|
|
309
|
+
);
|
|
310
|
+
const childId = detail.type === "sub_agent" && detail.childSessionId
|
|
311
|
+
? sessionOwners.get(detail.childSessionId)
|
|
312
|
+
: undefined;
|
|
313
|
+
if (childId) linkChild(childId, agentId);
|
|
314
|
+
if (item.callId) {
|
|
315
|
+
if (item.status === "running") {
|
|
316
|
+
if (!subCalls.has(item.callId)) {
|
|
317
|
+
subCalls.add(item.callId);
|
|
318
|
+
onSubChange();
|
|
319
|
+
}
|
|
320
|
+
} else if (subCalls.delete(item.callId)) {
|
|
321
|
+
onSubChange();
|
|
322
|
+
}
|
|
323
|
+
}
|
|
213
324
|
}
|
|
214
325
|
break;
|
|
215
326
|
}
|
|
@@ -217,7 +328,9 @@ export default function contribute(client: PluginClientContext) {
|
|
|
217
328
|
case "subscription_restored":
|
|
218
329
|
// Do not replay history we never saw; the live window is local anyway.
|
|
219
330
|
waits.clear();
|
|
331
|
+
subCalls.clear();
|
|
220
332
|
store.dispatch({ t: "reset" });
|
|
333
|
+
onSubChange();
|
|
221
334
|
// Provider counters kept moving while we were blind: re-read them so
|
|
222
335
|
// the next delta is the new work, not the whole unseen gap.
|
|
223
336
|
reseed();
|
|
@@ -232,6 +345,7 @@ export default function contribute(client: PluginClientContext) {
|
|
|
232
345
|
|
|
233
346
|
function attach(agent: AgentSnapshot): void {
|
|
234
347
|
if (aborted() || attached.has(agent.id) || !agent.workspaceId) return;
|
|
348
|
+
remember(agent);
|
|
235
349
|
const workspaceId = agent.workspaceId;
|
|
236
350
|
const handle = client.paseo.agents.ref(agent.id);
|
|
237
351
|
const store = createAgentSpeedStore({
|
|
@@ -315,9 +429,57 @@ export default function contribute(client: PluginClientContext) {
|
|
|
315
429
|
};
|
|
316
430
|
reseed();
|
|
317
431
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
432
|
+
// Delegation: open sub_agent tool calls (or linked children that are
|
|
433
|
+
// running) mean the agent's own model is idle while subagents work, so
|
|
434
|
+
// the meter parks that time like a user wait. `subCalls` survives
|
|
435
|
+
// snapshot re-delivery of the same running tool item.
|
|
436
|
+
const subCalls = new Set<string>();
|
|
437
|
+
let delegatedNow = false;
|
|
438
|
+
const syncDelegated = (): void => {
|
|
439
|
+
let should = subCalls.size > 0;
|
|
440
|
+
if (!should) {
|
|
441
|
+
for (const member of teamMembers(agent.id)) {
|
|
442
|
+
if (member.store.isRunning()) {
|
|
443
|
+
should = true;
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
if (store.state.phase !== "running") {
|
|
449
|
+
// Outside a turn there is no interval to open or close; drop the
|
|
450
|
+
// flag so the next turn re-evaluates from scratch.
|
|
451
|
+
delegatedNow = false;
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (should === delegatedNow) return;
|
|
455
|
+
delegatedNow = should;
|
|
456
|
+
console.log(`turn-speed: delegation ${should ? "on" : "off"} [${agent.id.slice(0, 8)}]`);
|
|
457
|
+
store.dispatch({ t: should ? "delegated_started" : "delegated_ended", at: Date.now() });
|
|
458
|
+
};
|
|
459
|
+
const team = () => {
|
|
460
|
+
const view = summarizeTeam(teamMembers(agent.id), prefs.liveWindowSec, Date.now());
|
|
461
|
+
// Open sub_agent tool calls count too: on provider subagents (no
|
|
462
|
+
// linked Paseo child) they are the only running signal.
|
|
463
|
+
const running = Math.max(view.running, subCalls.size);
|
|
464
|
+
if (view.members.length === 0 && running === 0) return null;
|
|
465
|
+
if (view.members.length === 0) {
|
|
466
|
+
return { members: [], running, liveTokPerSec: null, estTokens: 0, approx: true };
|
|
467
|
+
}
|
|
468
|
+
return { ...view, running };
|
|
469
|
+
};
|
|
470
|
+
const teamWatchersForParent = teamWatchers.get(agent.id) ?? new Set<() => void>();
|
|
471
|
+
teamWatchersForParent.add(syncDelegated);
|
|
472
|
+
teamWatchers.set(agent.id, teamWatchersForParent);
|
|
473
|
+
|
|
474
|
+
const details = makeSpeedDetails(
|
|
475
|
+
store,
|
|
476
|
+
() => prefs,
|
|
477
|
+
() => effectiveCpt(store.model),
|
|
478
|
+
() => {
|
|
479
|
+
void refreshPrefs();
|
|
480
|
+
},
|
|
481
|
+
team,
|
|
482
|
+
);
|
|
321
483
|
let pillHandle: { destroy(): void } | null = null;
|
|
322
484
|
try {
|
|
323
485
|
pillHandle = bindPill(
|
|
@@ -329,6 +491,7 @@ export default function contribute(client: PluginClientContext) {
|
|
|
329
491
|
store,
|
|
330
492
|
() => prefs,
|
|
331
493
|
() => Date.now(),
|
|
494
|
+
team,
|
|
332
495
|
);
|
|
333
496
|
} catch (error) {
|
|
334
497
|
logError(`pill registration failed for agent ${agent.id}`, error);
|
|
@@ -340,6 +503,7 @@ export default function contribute(client: PluginClientContext) {
|
|
|
340
503
|
return;
|
|
341
504
|
}
|
|
342
505
|
const snapshot = update.agent;
|
|
506
|
+
remember(snapshot);
|
|
343
507
|
store.setIdentity(snapshot.provider, snapshot.model ?? null);
|
|
344
508
|
observeUsage(snapshot.lastUsage ?? null);
|
|
345
509
|
syncWaits(snapshot);
|
|
@@ -349,7 +513,7 @@ export default function contribute(client: PluginClientContext) {
|
|
|
349
513
|
|
|
350
514
|
const timelineSub = handle.timeline.subscribe((event) => {
|
|
351
515
|
try {
|
|
352
|
-
onStreamEvent(store, handle, event.event, reseed, pendingWaits);
|
|
516
|
+
onStreamEvent(store, handle, event.event, reseed, pendingWaits, agent.id, subCalls, syncDelegated);
|
|
353
517
|
} catch (error) {
|
|
354
518
|
logError(`timeline event failed for ${agent.id}`, error);
|
|
355
519
|
}
|
|
@@ -359,17 +523,38 @@ export default function contribute(client: PluginClientContext) {
|
|
|
359
523
|
attached.set(agent.id, {
|
|
360
524
|
store,
|
|
361
525
|
detach() {
|
|
526
|
+
storeTeamWatch?.();
|
|
527
|
+
storeTeamWatch = null;
|
|
528
|
+
teamWatchersForParent.delete(syncDelegated);
|
|
362
529
|
pillHandle?.destroy();
|
|
363
530
|
releaseAgentUpdates();
|
|
364
531
|
void timelineSub.release().catch(() => undefined);
|
|
365
532
|
},
|
|
366
533
|
});
|
|
534
|
+
// A child's phase turning running/idle may not coincide with any agent
|
|
535
|
+
// status update, so the parent re-evaluates delegation on every change
|
|
536
|
+
// of this store too. The lookup is dynamic: links learned after attach
|
|
537
|
+
// (e.g. from a sub_agent tool item) are covered as well.
|
|
538
|
+
let storeTeamWatch: (() => void) | null = store.subscribe(() => {
|
|
539
|
+
const parentLink = childParents.get(agent.id);
|
|
540
|
+
if (parentLink) notifyTeam(parentLink);
|
|
541
|
+
});
|
|
542
|
+
// Children may already run (attach raced a reload): pick that up now.
|
|
543
|
+
syncDelegated();
|
|
367
544
|
}
|
|
368
545
|
|
|
369
546
|
function detach(agentId: string): void {
|
|
370
547
|
const attachment = attached.get(agentId);
|
|
371
548
|
if (!attachment) return;
|
|
372
549
|
attached.delete(agentId);
|
|
550
|
+
const snapshot = snapshots.get(agentId);
|
|
551
|
+
snapshots.delete(agentId);
|
|
552
|
+
teamWatchers.delete(agentId);
|
|
553
|
+
const parent = childParents.get(agentId);
|
|
554
|
+
childParents.delete(agentId);
|
|
555
|
+
if (parent) notifyTeam(parent);
|
|
556
|
+
const sessionId = snapshot?.runtimeInfo?.sessionId ?? snapshot?.persistence?.sessionId ?? null;
|
|
557
|
+
if (sessionId && sessionOwners.get(sessionId) === agentId) sessionOwners.delete(sessionId);
|
|
373
558
|
try {
|
|
374
559
|
attachment.detach();
|
|
375
560
|
} catch (error) {
|
|
@@ -381,7 +566,10 @@ export default function contribute(client: PluginClientContext) {
|
|
|
381
566
|
for (const entry of entries) {
|
|
382
567
|
const agent = entry.agent;
|
|
383
568
|
if (agent.archivedAt || agent.status === "closed") detach(agent.id);
|
|
384
|
-
else
|
|
569
|
+
else {
|
|
570
|
+
remember(agent);
|
|
571
|
+
attach(agent);
|
|
572
|
+
}
|
|
385
573
|
}
|
|
386
574
|
}
|
|
387
575
|
|
|
@@ -410,7 +598,10 @@ export default function contribute(client: PluginClientContext) {
|
|
|
410
598
|
}
|
|
411
599
|
const agent = payload.agent;
|
|
412
600
|
if (agent.archivedAt || agent.status === "closed") detach(agent.id);
|
|
413
|
-
else
|
|
601
|
+
else {
|
|
602
|
+
remember(agent);
|
|
603
|
+
attach(agent);
|
|
604
|
+
}
|
|
414
605
|
},
|
|
415
606
|
error: (error) => logError("agent list subscription failed", error),
|
|
416
607
|
});
|
|
@@ -440,6 +631,10 @@ export default function contribute(client: PluginClientContext) {
|
|
|
440
631
|
calFlushTimer = null;
|
|
441
632
|
}
|
|
442
633
|
for (const agentId of [...attached.keys()]) detach(agentId);
|
|
634
|
+
snapshots.clear();
|
|
635
|
+
childParents.clear();
|
|
636
|
+
sessionOwners.clear();
|
|
637
|
+
teamWatchers.clear();
|
|
443
638
|
if (listSubscription) {
|
|
444
639
|
void listSubscription.release().catch(() => undefined);
|
|
445
640
|
listSubscription = null;
|