@tokentop/ttop 0.6.1 → 0.7.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/package.json +4 -4
- package/src/agents/aggregator.test.ts +269 -1
- package/src/agents/aggregator.ts +196 -8
- package/src/agents/costing.test.ts +584 -0
- package/src/agents/costing.ts +144 -30
- package/src/agents/types.ts +7 -0
- package/src/demo/simulator.test.ts +92 -0
- package/src/demo/simulator.ts +579 -211
- package/src/plugins/plugin-context-factory.ts +9 -1
- package/src/plugins/providers/anthropic.test.ts +145 -0
- package/src/plugins/providers/anthropic.ts +18 -12
- package/src/plugins/providers/chutes.ts +2 -0
- package/src/plugins/providers/github-copilot.ts +3 -0
- package/src/plugins/providers/minimax.ts +2 -0
- package/src/plugins/providers/openai-api.ts +4 -0
- package/src/plugins/providers/zai.ts +2 -0
- package/src/pricing/estimator.ts +7 -0
- package/src/pricing/index.ts +5 -0
- package/src/pricing/models-dev.test.ts +117 -0
- package/src/pricing/models-dev.ts +22 -0
- package/src/storage/persistence-service.test.ts +201 -0
- package/src/storage/persistence-service.ts +54 -5
- package/src/storage/repos/agentSessions.ts +49 -0
- package/src/tui/App.tsx +4 -4
- package/src/tui/components/SessionDetailsDrawer.tsx +29 -1
- package/src/tui/components/SessionsTable.tsx +25 -7
- package/src/tui/contexts/AgentSessionContext.tsx +59 -21
- package/src/tui/contexts/PluginContext.tsx +31 -10
- package/src/tui/utils/scrollFollow.test.ts +71 -0
- package/src/tui/utils/scrollFollow.ts +12 -0
- package/src/tui/views/Dashboard.tsx +0 -6
- package/src/tui/views/RealTimeDashboard.tsx +105 -38
- package/src/version.ts +1 -1
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
|
|
16
16
|
import { isDatabaseInitialized } from "./db.ts";
|
|
17
17
|
import {
|
|
18
|
+
getLatestSnapshotsForAllSessions,
|
|
18
19
|
getLatestStreamTotalsForAllSessions,
|
|
19
20
|
insertAgentSessionSnapshot,
|
|
20
21
|
upsertAgentSession,
|
|
@@ -36,6 +37,12 @@ import { computeStreamDelta } from "./types.ts";
|
|
|
36
37
|
/** Minimum interval between DB writes for the same session */
|
|
37
38
|
const SNAPSHOT_INTERVAL_MS = 300_000; // 5 minutes
|
|
38
39
|
|
|
40
|
+
/** Remove tracking entries for sessions not written in this window */
|
|
41
|
+
const STALE_ENTRY_MAX_AGE_MS = 24 * 60 * 60 * 1000; // 24 hours
|
|
42
|
+
|
|
43
|
+
/** Run eviction at most this often */
|
|
44
|
+
const EVICTION_INTERVAL_MS = 10 * 60 * 1000; // 10 minutes
|
|
45
|
+
|
|
39
46
|
// ---------------------------------------------------------------------------
|
|
40
47
|
// Global state (survives bun --hot HMR and React remounts)
|
|
41
48
|
// ---------------------------------------------------------------------------
|
|
@@ -49,6 +56,8 @@ interface PersistenceState {
|
|
|
49
56
|
previousStreamTotals: Map<string, StreamTotals>;
|
|
50
57
|
/** Whether startup seeding has completed */
|
|
51
58
|
seeded: boolean;
|
|
59
|
+
/** Timestamp of last stale entry eviction run */
|
|
60
|
+
lastEvictionAt: number;
|
|
52
61
|
}
|
|
53
62
|
|
|
54
63
|
declare global {
|
|
@@ -63,6 +72,7 @@ function getState(): PersistenceState {
|
|
|
63
72
|
sessionFingerprints: new Map(),
|
|
64
73
|
previousStreamTotals: new Map(),
|
|
65
74
|
seeded: false,
|
|
75
|
+
lastEvictionAt: 0,
|
|
66
76
|
};
|
|
67
77
|
}
|
|
68
78
|
return globalThis.__tokentopPersistence;
|
|
@@ -91,6 +101,27 @@ export interface SessionPersistData {
|
|
|
91
101
|
streams: Omit<AgentSessionStreamSnapshotRow, "agentSessionSnapshotId">[];
|
|
92
102
|
}
|
|
93
103
|
|
|
104
|
+
function evictStaleEntries(state: PersistenceState, now: number): void {
|
|
105
|
+
if (now - state.lastEvictionAt < EVICTION_INTERVAL_MS) return;
|
|
106
|
+
state.lastEvictionAt = now;
|
|
107
|
+
|
|
108
|
+
const cutoff = now - STALE_ENTRY_MAX_AGE_MS;
|
|
109
|
+
for (const [key, ts] of state.lastWriteTimestamps) {
|
|
110
|
+
if (ts < cutoff) {
|
|
111
|
+
state.lastWriteTimestamps.delete(key);
|
|
112
|
+
state.sessionFingerprints.delete(key);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Evict stream totals whose parent session was evicted
|
|
117
|
+
for (const streamKey of state.previousStreamTotals.keys()) {
|
|
118
|
+
const sessionKey = streamKey.slice(0, streamKey.indexOf(":", streamKey.indexOf(":") + 1));
|
|
119
|
+
if (!state.lastWriteTimestamps.has(sessionKey)) {
|
|
120
|
+
state.previousStreamTotals.delete(streamKey);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
94
125
|
/**
|
|
95
126
|
* Persist session data to the database with per-session throttling.
|
|
96
127
|
*
|
|
@@ -105,6 +136,7 @@ export function persistSessions(sessions: SessionPersistData[]): number {
|
|
|
105
136
|
|
|
106
137
|
const state = getState();
|
|
107
138
|
const now = Date.now();
|
|
139
|
+
evictStaleEntries(state, now);
|
|
108
140
|
let persistedCount = 0;
|
|
109
141
|
|
|
110
142
|
for (const { session, snapshot, streams } of sessions) {
|
|
@@ -177,11 +209,6 @@ export function persistSessions(sessions: SessionPersistData[]): number {
|
|
|
177
209
|
return persistedCount;
|
|
178
210
|
}
|
|
179
211
|
|
|
180
|
-
/**
|
|
181
|
-
* Seed previous stream totals from the database.
|
|
182
|
-
* Called once at startup after DB initialization to ensure
|
|
183
|
-
* accurate delta computation from the first persistence call.
|
|
184
|
-
*/
|
|
185
212
|
export function seedPreviousTotals(): void {
|
|
186
213
|
const state = getState();
|
|
187
214
|
if (state.seeded) return;
|
|
@@ -199,6 +226,28 @@ export function seedPreviousTotals(): void {
|
|
|
199
226
|
requestCount: row.requestCount,
|
|
200
227
|
});
|
|
201
228
|
}
|
|
229
|
+
|
|
230
|
+
// Seeds Gate 1/2 so the first post-startup tick does not rewrite every
|
|
231
|
+
// historical session. computeFingerprint only reads cost/count/input/output,
|
|
232
|
+
// so the other fields are placeholders to satisfy the snapshot shape.
|
|
233
|
+
const latestSnapshots = getLatestSnapshotsForAllSessions();
|
|
234
|
+
for (const snap of latestSnapshots) {
|
|
235
|
+
const sessionKey = `${snap.agentId}:${snap.sessionId}`;
|
|
236
|
+
const fp = computeFingerprint({
|
|
237
|
+
timestamp: snap.timestamp,
|
|
238
|
+
lastActivityAt: snap.timestamp,
|
|
239
|
+
status: null,
|
|
240
|
+
totalInputTokens: snap.totalInputTokens,
|
|
241
|
+
totalOutputTokens: snap.totalOutputTokens,
|
|
242
|
+
totalCacheReadTokens: 0,
|
|
243
|
+
totalCacheWriteTokens: 0,
|
|
244
|
+
totalCostUsd: snap.totalCostUsd,
|
|
245
|
+
requestCount: snap.requestCount,
|
|
246
|
+
});
|
|
247
|
+
state.sessionFingerprints.set(sessionKey, fp);
|
|
248
|
+
state.lastWriteTimestamps.set(sessionKey, snap.timestamp);
|
|
249
|
+
}
|
|
250
|
+
|
|
202
251
|
state.seeded = true;
|
|
203
252
|
} catch (err) {
|
|
204
253
|
console.error("Failed to seed previous totals from DB:", err);
|
|
@@ -172,6 +172,55 @@ export interface LatestStreamTotals {
|
|
|
172
172
|
requestCount: number;
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
+
export interface LatestSessionSnapshot {
|
|
176
|
+
agentId: string;
|
|
177
|
+
sessionId: string;
|
|
178
|
+
timestamp: number;
|
|
179
|
+
totalCostUsd: number;
|
|
180
|
+
requestCount: number;
|
|
181
|
+
totalInputTokens: number;
|
|
182
|
+
totalOutputTokens: number;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function getLatestSnapshotsForAllSessions(): LatestSessionSnapshot[] {
|
|
186
|
+
const db = getDatabase();
|
|
187
|
+
|
|
188
|
+
const rows = db
|
|
189
|
+
.prepare(`
|
|
190
|
+
SELECT
|
|
191
|
+
s.agent_id, s.session_id,
|
|
192
|
+
snap.timestamp,
|
|
193
|
+
snap.total_cost_usd, snap.request_count,
|
|
194
|
+
snap.total_input_tokens, snap.total_output_tokens
|
|
195
|
+
FROM agent_session_snapshots snap
|
|
196
|
+
JOIN agent_sessions s ON s.id = snap.agent_session_id
|
|
197
|
+
WHERE snap.id IN (
|
|
198
|
+
SELECT MAX(snap2.id)
|
|
199
|
+
FROM agent_session_snapshots snap2
|
|
200
|
+
GROUP BY snap2.agent_session_id
|
|
201
|
+
)
|
|
202
|
+
`)
|
|
203
|
+
.all() as Array<{
|
|
204
|
+
agent_id: string;
|
|
205
|
+
session_id: string;
|
|
206
|
+
timestamp: number;
|
|
207
|
+
total_cost_usd: number;
|
|
208
|
+
request_count: number;
|
|
209
|
+
total_input_tokens: number;
|
|
210
|
+
total_output_tokens: number;
|
|
211
|
+
}>;
|
|
212
|
+
|
|
213
|
+
return rows.map((r) => ({
|
|
214
|
+
agentId: r.agent_id,
|
|
215
|
+
sessionId: r.session_id,
|
|
216
|
+
timestamp: r.timestamp,
|
|
217
|
+
totalCostUsd: r.total_cost_usd,
|
|
218
|
+
requestCount: r.request_count,
|
|
219
|
+
totalInputTokens: r.total_input_tokens,
|
|
220
|
+
totalOutputTokens: r.total_output_tokens,
|
|
221
|
+
}));
|
|
222
|
+
}
|
|
223
|
+
|
|
175
224
|
export function getLatestStreamTotalsForAllSessions(): LatestStreamTotals[] {
|
|
176
225
|
const db = getDatabase();
|
|
177
226
|
|
package/src/tui/App.tsx
CHANGED
|
@@ -245,12 +245,12 @@ function AppContent() {
|
|
|
245
245
|
]);
|
|
246
246
|
|
|
247
247
|
useKeyboard((key) => {
|
|
248
|
-
if (key.ctrl && key.name === "p") {
|
|
249
|
-
|
|
248
|
+
if ((key.ctrl && key.shift && key.name === "p") || (key.shift && key.name === "p")) {
|
|
249
|
+
handleBurstRecord();
|
|
250
250
|
return;
|
|
251
251
|
}
|
|
252
|
-
if (key.ctrl && key.
|
|
253
|
-
|
|
252
|
+
if (key.ctrl && key.name === "p") {
|
|
253
|
+
handleCaptureFrame();
|
|
254
254
|
return;
|
|
255
255
|
}
|
|
256
256
|
|
|
@@ -158,6 +158,16 @@ export function SessionDetailsDrawer({ session, onClose: _onClose }: SessionDeta
|
|
|
158
158
|
};
|
|
159
159
|
}, [session.streams]);
|
|
160
160
|
|
|
161
|
+
const hasAnyLongContext = session.streams.some((s) => s.hasLongContext);
|
|
162
|
+
const totalLongContextRequests = session.streams.reduce(
|
|
163
|
+
(sum, s) => sum + (s.longContextRequestCount ?? 0),
|
|
164
|
+
0,
|
|
165
|
+
);
|
|
166
|
+
const totalLongContextTokens = session.streams.reduce((sum, s) => {
|
|
167
|
+
if (!s.longContextTokens) return sum;
|
|
168
|
+
return sum + s.longContextTokens.input + s.longContextTokens.output;
|
|
169
|
+
}, 0);
|
|
170
|
+
|
|
161
171
|
const isMultiDay = session.lastActivityAt - session.startedAt > 24 * 60 * 60 * 1000;
|
|
162
172
|
const startTimeStr = formatTimelineLabel(session.startedAt, isMultiDay);
|
|
163
173
|
const endTimeStr = formatTimelineLabel(session.lastActivityAt, isMultiDay);
|
|
@@ -338,6 +348,18 @@ export function SessionDetailsDrawer({ session, onClose: _onClose }: SessionDeta
|
|
|
338
348
|
</text>
|
|
339
349
|
</box>
|
|
340
350
|
)}
|
|
351
|
+
|
|
352
|
+
{hasAnyLongContext && (
|
|
353
|
+
<box flexDirection="row" height={1} marginBottom={0}>
|
|
354
|
+
<text height={1} overflow="hidden">
|
|
355
|
+
<span fg={colors.warning}>↑ Long ctx: </span>
|
|
356
|
+
<span fg={colors.text}>{totalLongContextRequests} reqs</span>
|
|
357
|
+
<span fg={colors.textMuted}> · </span>
|
|
358
|
+
<span fg={colors.text}>{formatTokens(totalLongContextTokens)} tokens</span>
|
|
359
|
+
<span fg={colors.textMuted}> at tiered rate</span>
|
|
360
|
+
</text>
|
|
361
|
+
</box>
|
|
362
|
+
)}
|
|
341
363
|
</box>
|
|
342
364
|
|
|
343
365
|
<box
|
|
@@ -407,11 +429,17 @@ export function SessionDetailsDrawer({ session, onClose: _onClose }: SessionDeta
|
|
|
407
429
|
</text>
|
|
408
430
|
<box width={10} height={1} justifyContent="flex-end">
|
|
409
431
|
<text fg={colors.textMuted}>
|
|
410
|
-
{formatTokens(
|
|
432
|
+
{formatTokens(
|
|
433
|
+
stream.tokens.input +
|
|
434
|
+
stream.tokens.output +
|
|
435
|
+
(stream.longContextTokens?.input ?? 0) +
|
|
436
|
+
(stream.longContextTokens?.output ?? 0),
|
|
437
|
+
)}
|
|
411
438
|
</text>
|
|
412
439
|
</box>
|
|
413
440
|
<box width={10} height={1} justifyContent="flex-end">
|
|
414
441
|
<text fg={colors.success}>{formatCost(stream.costUsd)}</text>
|
|
442
|
+
<text fg={colors.textMuted}>{stream.hasLongContext ? "↑" : " "}</text>
|
|
415
443
|
</box>
|
|
416
444
|
</box>
|
|
417
445
|
);
|
|
@@ -13,6 +13,8 @@ import { getSortDirectionIndicator, getSortFieldLabel } from "../types/sort.ts";
|
|
|
13
13
|
interface SessionsTableProps {
|
|
14
14
|
sessions: AgentSessionAggregate[];
|
|
15
15
|
selectedRow: number;
|
|
16
|
+
scrollOffset: number;
|
|
17
|
+
visibleRows: number;
|
|
16
18
|
isLoading: boolean;
|
|
17
19
|
isFiltering: boolean;
|
|
18
20
|
filterQuery: string;
|
|
@@ -159,6 +161,7 @@ const SessionRow = memo(function SessionRow({
|
|
|
159
161
|
const cacheRead = session.totals.cacheRead ?? 0;
|
|
160
162
|
const hasCacheData = cacheRead > 0;
|
|
161
163
|
const isEstimated = session.metadata?.isEstimated === true;
|
|
164
|
+
const hasLongContext = session.streams.some((s) => s.hasLongContext === true);
|
|
162
165
|
const costUsd = session.totalCostUsd ?? 0;
|
|
163
166
|
|
|
164
167
|
const animatedTokens = useAnimatedValue(effectiveTokens, { durationMs: 300, precision: 0 });
|
|
@@ -271,8 +274,9 @@ const SessionRow = memo(function SessionRow({
|
|
|
271
274
|
</span>
|
|
272
275
|
<span fg={textMutedColor}>{hasCacheData ? ` ↯${formatCacheBadge(cacheRead)}` : ""}</span>
|
|
273
276
|
</text>
|
|
274
|
-
<text width={8} height={1}
|
|
275
|
-
{costDisplay.padStart(7)}
|
|
277
|
+
<text width={8} height={1} {...bgProp}>
|
|
278
|
+
<span fg={costColor}>{costDisplay.padStart(7)}</span>
|
|
279
|
+
<span fg={textMutedColor}>{hasLongContext ? "↑" : " "}</span>
|
|
276
280
|
</text>
|
|
277
281
|
<text width={14} height={1} fg={textSubtleColor} {...bgProp}>
|
|
278
282
|
{projectDisplay}
|
|
@@ -329,8 +333,9 @@ const SessionRow = memo(function SessionRow({
|
|
|
329
333
|
{hasCacheData && showCacheBadge ? ` ↯${formatCacheBadge(cacheRead)}` : ""}
|
|
330
334
|
</span>
|
|
331
335
|
</text>
|
|
332
|
-
<text width={7} height={1}
|
|
333
|
-
{formatCostVal(costUsd).padStart(6)}
|
|
336
|
+
<text width={7} height={1} {...bgProp}>
|
|
337
|
+
<span fg={costColor}>{formatCostVal(costUsd).padStart(6)}</span>
|
|
338
|
+
<span fg={textMutedColor}>{hasLongContext ? "↑" : " "}</span>
|
|
334
339
|
</text>
|
|
335
340
|
<text flexGrow={1} height={1} fg={textSubtleColor} {...bgProp}>
|
|
336
341
|
{projectDisplay}
|
|
@@ -351,6 +356,8 @@ export const SessionsTable = forwardRef(function SessionsTable(
|
|
|
351
356
|
{
|
|
352
357
|
sessions,
|
|
353
358
|
selectedRow,
|
|
359
|
+
scrollOffset,
|
|
360
|
+
visibleRows,
|
|
354
361
|
isLoading,
|
|
355
362
|
isFiltering,
|
|
356
363
|
filterQuery,
|
|
@@ -398,10 +405,18 @@ export const SessionsTable = forwardRef(function SessionsTable(
|
|
|
398
405
|
bulkThreshold: 100,
|
|
399
406
|
});
|
|
400
407
|
|
|
401
|
-
let activeIndex = 0;
|
|
402
408
|
const selectedSession = sessions[selectedRow] ?? null;
|
|
403
409
|
const inspector = selectedSession ? getInspectorData(selectedSession) : null;
|
|
404
410
|
|
|
411
|
+
// Spacer boxes preserve scrollbox inner height so scrollTo(scrollOffset) still maps correctly.
|
|
412
|
+
const RENDER_BUFFER = 4;
|
|
413
|
+
const visibleStart = Math.max(0, scrollOffset - RENDER_BUFFER);
|
|
414
|
+
const visibleEnd = Math.min(animatedSessions.length, scrollOffset + visibleRows + RENDER_BUFFER);
|
|
415
|
+
const visibleSlice =
|
|
416
|
+
animatedSessions.length > 0 ? animatedSessions.slice(visibleStart, visibleEnd) : [];
|
|
417
|
+
const topSpacer = visibleStart;
|
|
418
|
+
const bottomSpacer = Math.max(0, animatedSessions.length - visibleEnd);
|
|
419
|
+
|
|
405
420
|
// Sort indicator helpers for column headers
|
|
406
421
|
const sortIndicator = (field: string) =>
|
|
407
422
|
sortField === field ? (sortDirection === "desc" ? "▼" : "▲") : " ";
|
|
@@ -527,8 +542,10 @@ export const SessionsTable = forwardRef(function SessionsTable(
|
|
|
527
542
|
</text>
|
|
528
543
|
</box>
|
|
529
544
|
)}
|
|
530
|
-
{
|
|
531
|
-
|
|
545
|
+
{topSpacer > 0 && <box height={topSpacer} flexShrink={0} />}
|
|
546
|
+
{visibleSlice.map((entry, i) => {
|
|
547
|
+
const absIndex = visibleStart + i;
|
|
548
|
+
const isSelectedRow = !entry.isExiting && absIndex === selectedRow;
|
|
532
549
|
return (
|
|
533
550
|
<SessionRow
|
|
534
551
|
key={entry.item.sessionId}
|
|
@@ -544,6 +561,7 @@ export const SessionsTable = forwardRef(function SessionsTable(
|
|
|
544
561
|
/>
|
|
545
562
|
);
|
|
546
563
|
})}
|
|
564
|
+
{bottomSpacer > 0 && <box height={bottomSpacer} flexShrink={0} />}
|
|
547
565
|
</box>
|
|
548
566
|
</scrollbox>
|
|
549
567
|
{terminalHeight >= 30 && (
|
|
@@ -5,10 +5,17 @@ import {
|
|
|
5
5
|
useCallback,
|
|
6
6
|
useContext,
|
|
7
7
|
useEffect,
|
|
8
|
+
useMemo,
|
|
8
9
|
useRef,
|
|
9
10
|
useState,
|
|
10
11
|
} from "react";
|
|
11
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
aggregateSessionUsage,
|
|
14
|
+
deduplicateAggregates,
|
|
15
|
+
isCrossAgentShadow,
|
|
16
|
+
markCrossAgentShadows,
|
|
17
|
+
selectEffectiveSessions,
|
|
18
|
+
} from "@/agents/aggregator.ts";
|
|
12
19
|
import { priceSessions } from "@/agents/costing.ts";
|
|
13
20
|
import type { AgentId, AgentInfo, AgentSessionAggregate } from "@/agents/types.ts";
|
|
14
21
|
import { createPluginContext } from "@/plugins/plugin-context-factory.ts";
|
|
@@ -48,10 +55,12 @@ interface AgentSessionProviderProps {
|
|
|
48
55
|
*/
|
|
49
56
|
function computeSessionFingerprint(sessions: AgentSessionAggregate[]): string {
|
|
50
57
|
// Build a compact string from the fields that actually affect the UI.
|
|
51
|
-
//
|
|
58
|
+
// Shadow bit is included so that flipping a session's dedup status
|
|
59
|
+
// (gaining or losing its cross-agent shadow mark) triggers a re-render.
|
|
52
60
|
let fp = `${sessions.length}:`;
|
|
53
61
|
for (const s of sessions) {
|
|
54
|
-
|
|
62
|
+
const shadow = isCrossAgentShadow(s) ? "1" : "0";
|
|
63
|
+
fp += `${s.sessionId},${s.status},${s.lastActivityAt},${s.totals.input},${s.totals.output},${s.totalCostUsd ?? 0},${s.requestCount},${shadow};`;
|
|
55
64
|
}
|
|
56
65
|
return fp;
|
|
57
66
|
}
|
|
@@ -68,6 +77,9 @@ export function AgentSessionProvider({ children, autoRefresh = false }: AgentSes
|
|
|
68
77
|
const { windowMs } = useTimeWindow();
|
|
69
78
|
const hasBackfilled = useRef(false);
|
|
70
79
|
const isRefreshingRef = useRef(false);
|
|
80
|
+
// Bugfix: track first completed load separately so an empty 1h window does not
|
|
81
|
+
// keep re-entering the initial loading state forever.
|
|
82
|
+
const hasLoadedOnceRef = useRef(false);
|
|
71
83
|
const lastFingerprintRef = useRef<string>("");
|
|
72
84
|
|
|
73
85
|
const discoverAgents = useCallback(async (): Promise<AgentInfo[]> => {
|
|
@@ -110,7 +122,17 @@ export function AgentSessionProvider({ children, autoRefresh = false }: AgentSes
|
|
|
110
122
|
|
|
111
123
|
const http = createSandboxedHttpClient(plugin.id, plugin.permissions);
|
|
112
124
|
const logger = createPluginLogger(plugin.id);
|
|
113
|
-
|
|
125
|
+
// Lazy signal: only create the 120s timer if the plugin actually reads ctx.signal.
|
|
126
|
+
let parseSignal: AbortSignal | undefined;
|
|
127
|
+
const ctx = {
|
|
128
|
+
http,
|
|
129
|
+
logger,
|
|
130
|
+
config: {},
|
|
131
|
+
get signal(): AbortSignal {
|
|
132
|
+
if (!parseSignal) parseSignal = AbortSignal.timeout(120_000);
|
|
133
|
+
return parseSignal;
|
|
134
|
+
},
|
|
135
|
+
};
|
|
114
136
|
|
|
115
137
|
const rawSessions = await plugin.parseSessions(options, ctx);
|
|
116
138
|
|
|
@@ -134,7 +156,7 @@ export function AgentSessionProvider({ children, autoRefresh = false }: AgentSes
|
|
|
134
156
|
if (isRefreshingRef.current) return;
|
|
135
157
|
isRefreshingRef.current = true;
|
|
136
158
|
|
|
137
|
-
const isInitialLoad =
|
|
159
|
+
const isInitialLoad = !hasLoadedOnceRef.current;
|
|
138
160
|
if (isInitialLoad) {
|
|
139
161
|
setIsLoading(true);
|
|
140
162
|
}
|
|
@@ -193,7 +215,6 @@ export function AgentSessionProvider({ children, autoRefresh = false }: AgentSes
|
|
|
193
215
|
}
|
|
194
216
|
}
|
|
195
217
|
|
|
196
|
-
// Deduplicate sessions claimed by multiple agents scanning the same directory.
|
|
197
218
|
const dedupedAggregates = deduplicateAggregates(allAggregates);
|
|
198
219
|
if (dedupedAggregates.length < allAggregates.length) {
|
|
199
220
|
debug(
|
|
@@ -203,6 +224,8 @@ export function AgentSessionProvider({ children, autoRefresh = false }: AgentSes
|
|
|
203
224
|
);
|
|
204
225
|
}
|
|
205
226
|
|
|
227
|
+
markCrossAgentShadows(dedupedAggregates);
|
|
228
|
+
|
|
206
229
|
const pricedSessions = await priceSessions(dedupedAggregates);
|
|
207
230
|
|
|
208
231
|
pricedSessions.sort((a, b) => b.lastActivityAt - a.lastActivityAt);
|
|
@@ -221,11 +244,13 @@ export function AgentSessionProvider({ children, autoRefresh = false }: AgentSes
|
|
|
221
244
|
return;
|
|
222
245
|
}
|
|
223
246
|
|
|
247
|
+
const effectiveSessions = selectEffectiveSessions(pricedSessions);
|
|
248
|
+
|
|
224
249
|
// Persist to storage — the persistence service handles throttling
|
|
225
250
|
// internally (fingerprint gate + 5-minute per-session throttle).
|
|
226
251
|
// Safe to call every tick; state lives on globalThis, survives HMR.
|
|
227
252
|
const now = Date.now();
|
|
228
|
-
const persistData =
|
|
253
|
+
const persistData = effectiveSessions.map((session) => ({
|
|
229
254
|
session: {
|
|
230
255
|
agentId: session.agentId,
|
|
231
256
|
sessionId: session.sessionId,
|
|
@@ -258,7 +283,7 @@ export function AgentSessionProvider({ children, autoRefresh = false }: AgentSes
|
|
|
258
283
|
}));
|
|
259
284
|
const persistedCount = persistSessions(persistData);
|
|
260
285
|
debug(
|
|
261
|
-
`Persisted ${persistedCount}/${
|
|
286
|
+
`Persisted ${persistedCount}/${effectiveSessions.length} sessions to storage`,
|
|
262
287
|
undefined,
|
|
263
288
|
"agent-sessions",
|
|
264
289
|
);
|
|
@@ -266,7 +291,11 @@ export function AgentSessionProvider({ children, autoRefresh = false }: AgentSes
|
|
|
266
291
|
setSessions(pricedSessions);
|
|
267
292
|
setLastRefreshAt(Date.now());
|
|
268
293
|
|
|
269
|
-
debug(
|
|
294
|
+
debug(
|
|
295
|
+
`Session refresh complete`,
|
|
296
|
+
{ count: pricedSessions.length, effective: effectiveSessions.length },
|
|
297
|
+
"agent-sessions",
|
|
298
|
+
);
|
|
270
299
|
} catch (err) {
|
|
271
300
|
const errorMsg = err instanceof Error ? err.message : "Unknown error";
|
|
272
301
|
logError(`Session refresh failed: ${errorMsg}`, undefined, "agent-sessions");
|
|
@@ -275,15 +304,19 @@ export function AgentSessionProvider({ children, autoRefresh = false }: AgentSes
|
|
|
275
304
|
isRefreshingRef.current = false;
|
|
276
305
|
if (isInitialLoad) {
|
|
277
306
|
setIsLoading(false);
|
|
307
|
+
hasLoadedOnceRef.current = true;
|
|
278
308
|
}
|
|
279
309
|
}
|
|
280
310
|
},
|
|
281
|
-
[
|
|
311
|
+
[discoverAgents, fetchAgentSessions, debug, warn, logError, demoMode, simulator],
|
|
282
312
|
);
|
|
283
313
|
|
|
284
314
|
const refreshSessionsRef = useRef(refreshSessions);
|
|
285
315
|
refreshSessionsRef.current = refreshSessions;
|
|
286
316
|
|
|
317
|
+
const sessionsRef = useRef(sessions);
|
|
318
|
+
sessionsRef.current = sessions;
|
|
319
|
+
|
|
287
320
|
useEffect(() => {
|
|
288
321
|
if (!(demoMode || pluginsInitialized)) return;
|
|
289
322
|
|
|
@@ -304,7 +337,7 @@ export function AgentSessionProvider({ children, autoRefresh = false }: AgentSes
|
|
|
304
337
|
let timerId: ReturnType<typeof setTimeout> | null = null;
|
|
305
338
|
|
|
306
339
|
const scheduleNext = () => {
|
|
307
|
-
const hasActive =
|
|
340
|
+
const hasActive = sessionsRef.current.some((s) => s.status === "active");
|
|
308
341
|
const interval = hasActive ? ACTIVE_INTERVAL_MS : IDLE_INTERVAL_MS;
|
|
309
342
|
|
|
310
343
|
timerId = setTimeout(() => {
|
|
@@ -324,16 +357,21 @@ export function AgentSessionProvider({ children, autoRefresh = false }: AgentSes
|
|
|
324
357
|
clearTimeout(timerId);
|
|
325
358
|
}
|
|
326
359
|
};
|
|
327
|
-
}, [autoRefresh,
|
|
328
|
-
|
|
329
|
-
const
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
360
|
+
}, [autoRefresh, debug]);
|
|
361
|
+
|
|
362
|
+
const effectiveSessions = useMemo(() => selectEffectiveSessions(sessions), [sessions]);
|
|
363
|
+
|
|
364
|
+
const value: AgentSessionContextValue = useMemo(
|
|
365
|
+
() => ({
|
|
366
|
+
sessions: effectiveSessions,
|
|
367
|
+
agents,
|
|
368
|
+
isLoading,
|
|
369
|
+
lastRefreshAt,
|
|
370
|
+
error,
|
|
371
|
+
refreshSessions,
|
|
372
|
+
}),
|
|
373
|
+
[effectiveSessions, agents, isLoading, lastRefreshAt, error, refreshSessions],
|
|
374
|
+
);
|
|
337
375
|
|
|
338
376
|
return <AgentSessionContext.Provider value={value}>{children}</AgentSessionContext.Provider>;
|
|
339
377
|
}
|
|
@@ -275,7 +275,11 @@ export function PluginProvider({ children, cliPlugins }: PluginProviderProps) {
|
|
|
275
275
|
return;
|
|
276
276
|
}
|
|
277
277
|
|
|
278
|
-
|
|
278
|
+
if (state.loading) {
|
|
279
|
+
debug(`Skipping refresh for ${providerId}: already in flight`, undefined, "refresh");
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
|
|
279
283
|
const now = Date.now();
|
|
280
284
|
if (state.rateLimitUntil > now) {
|
|
281
285
|
const remainingSec = Math.round((state.rateLimitUntil - now) / 1000);
|
|
@@ -360,19 +364,25 @@ export function PluginProvider({ children, cliPlugins }: PluginProviderProps) {
|
|
|
360
364
|
|
|
361
365
|
const http = createSandboxedHttpClient(providerId, state.plugin.permissions);
|
|
362
366
|
const logger = createPluginLogger(providerId);
|
|
367
|
+
// Lazy signal: only create the 30s timer if a plugin actually reads ctx.signal.
|
|
368
|
+
let fetchSignal: AbortSignal | undefined;
|
|
369
|
+
const fetchCtx = {
|
|
370
|
+
credentials: creds,
|
|
371
|
+
http,
|
|
372
|
+
logger,
|
|
373
|
+
config: {},
|
|
374
|
+
get signal(): AbortSignal {
|
|
375
|
+
if (!fetchSignal) fetchSignal = AbortSignal.timeout(30_000);
|
|
376
|
+
return fetchSignal;
|
|
377
|
+
},
|
|
378
|
+
};
|
|
363
379
|
|
|
364
380
|
const fetchResult = await safeInvoke(
|
|
365
381
|
providerId,
|
|
366
382
|
"fetchUsage",
|
|
367
383
|
() =>
|
|
368
384
|
runInPluginGuard(providerId, state.plugin.permissions, () =>
|
|
369
|
-
state.plugin.fetchUsage(
|
|
370
|
-
credentials: creds,
|
|
371
|
-
http,
|
|
372
|
-
logger,
|
|
373
|
-
config: {},
|
|
374
|
-
signal: AbortSignal.timeout(30_000),
|
|
375
|
-
}),
|
|
385
|
+
state.plugin.fetchUsage(fetchCtx),
|
|
376
386
|
),
|
|
377
387
|
{
|
|
378
388
|
// Count non-rate-limit errors as circuit breaker failures.
|
|
@@ -513,14 +523,25 @@ export function PluginProvider({ children, cliPlugins }: PluginProviderProps) {
|
|
|
513
523
|
await Promise.all(configuredProviders.map(refreshProvider));
|
|
514
524
|
}, [providers, refreshProvider, info]);
|
|
515
525
|
|
|
526
|
+
const refreshProviderRef = useRef(refreshProvider);
|
|
527
|
+
const refreshAllProvidersRef = useRef(refreshAllProviders);
|
|
528
|
+
refreshProviderRef.current = refreshProvider;
|
|
529
|
+
refreshAllProvidersRef.current = refreshAllProviders;
|
|
530
|
+
|
|
531
|
+
const stableRefreshProvider = useCallback(
|
|
532
|
+
(providerId: string) => refreshProviderRef.current(providerId),
|
|
533
|
+
[],
|
|
534
|
+
);
|
|
535
|
+
const stableRefreshAllProviders = useCallback(() => refreshAllProvidersRef.current(), []);
|
|
536
|
+
|
|
516
537
|
const value: PluginContextValue = {
|
|
517
538
|
providers,
|
|
518
539
|
agents,
|
|
519
540
|
themes,
|
|
520
541
|
notifications,
|
|
521
542
|
isInitialized,
|
|
522
|
-
refreshProvider,
|
|
523
|
-
refreshAllProviders,
|
|
543
|
+
refreshProvider: stableRefreshProvider,
|
|
544
|
+
refreshAllProviders: stableRefreshAllProviders,
|
|
524
545
|
};
|
|
525
546
|
|
|
526
547
|
return <PluginContext.Provider value={value}>{children}</PluginContext.Provider>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { computeScrollOffset } from "./scrollFollow.ts";
|
|
3
|
+
|
|
4
|
+
describe("computeScrollOffset", () => {
|
|
5
|
+
test("cursor inside viewport leaves offset unchanged", () => {
|
|
6
|
+
expect(computeScrollOffset(5, 0, 10, 100)).toBe(0);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
test("cursor at bottom edge leaves offset unchanged", () => {
|
|
10
|
+
expect(computeScrollOffset(9, 0, 10, 100)).toBe(0);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("cursor past bottom pulls offset down by one row", () => {
|
|
14
|
+
expect(computeScrollOffset(10, 0, 10, 100)).toBe(1);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("cursor past top pulls offset up", () => {
|
|
18
|
+
expect(computeScrollOffset(3, 5, 10, 100)).toBe(3);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("clamps offset to non-negative", () => {
|
|
22
|
+
expect(computeScrollOffset(0, 0, 10, 100)).toBe(0);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("clamps offset to total - visibleRows", () => {
|
|
26
|
+
expect(computeScrollOffset(99, 0, 10, 100)).toBe(90);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("offset stays 0 when content fits in viewport", () => {
|
|
30
|
+
expect(computeScrollOffset(3, 0, 10, 4)).toBe(0);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Regression test for cursor-disappears-then-jump bug: rapid j presses
|
|
34
|
+
// that React batches into a single render must still land with the cursor
|
|
35
|
+
// visible at the bottom edge. Simulating N sequential functional updates
|
|
36
|
+
// (the reducer runs each one in order) verifies the invariant.
|
|
37
|
+
test("batched rapid j-presses keep cursor at bottom edge each step", () => {
|
|
38
|
+
const vis = 10;
|
|
39
|
+
const total = 1000;
|
|
40
|
+
let state = { row: 0, offset: 0 };
|
|
41
|
+
for (let i = 0; i < 50; i++) {
|
|
42
|
+
const nextRow = Math.min(state.row + 1, total - 1);
|
|
43
|
+
const nextOffset = computeScrollOffset(nextRow, state.offset, vis, total);
|
|
44
|
+
state = { row: nextRow, offset: nextOffset };
|
|
45
|
+
expect(state.row).toBeGreaterThanOrEqual(state.offset);
|
|
46
|
+
expect(state.row).toBeLessThan(state.offset + vis);
|
|
47
|
+
}
|
|
48
|
+
expect(state.row).toBe(50);
|
|
49
|
+
expect(state.offset).toBe(41);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("batched rapid k-presses at top keep cursor at top edge", () => {
|
|
53
|
+
const vis = 10;
|
|
54
|
+
const total = 1000;
|
|
55
|
+
let state = { row: 50, offset: 41 };
|
|
56
|
+
for (let i = 0; i < 50; i++) {
|
|
57
|
+
const nextRow = Math.max(state.row - 1, 0);
|
|
58
|
+
const nextOffset = computeScrollOffset(nextRow, state.offset, vis, total);
|
|
59
|
+
state = { row: nextRow, offset: nextOffset };
|
|
60
|
+
expect(state.row).toBeGreaterThanOrEqual(state.offset);
|
|
61
|
+
expect(state.row).toBeLessThan(state.offset + vis);
|
|
62
|
+
}
|
|
63
|
+
expect(state.row).toBe(0);
|
|
64
|
+
expect(state.offset).toBe(0);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("re-clamp when list shrinks below current offset", () => {
|
|
68
|
+
expect(computeScrollOffset(5, 50, 10, 20)).toBe(5);
|
|
69
|
+
expect(computeScrollOffset(19, 50, 10, 20)).toBe(10);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function computeScrollOffset(
|
|
2
|
+
row: number,
|
|
3
|
+
prevOffset: number,
|
|
4
|
+
visibleRows: number,
|
|
5
|
+
total: number,
|
|
6
|
+
): number {
|
|
7
|
+
let next = prevOffset;
|
|
8
|
+
if (row < next) next = row;
|
|
9
|
+
else if (row >= next + visibleRows) next = row - visibleRows + 1;
|
|
10
|
+
const max = Math.max(0, total - visibleRows);
|
|
11
|
+
return Math.max(0, Math.min(next, max));
|
|
12
|
+
}
|