scope-agent-workbench 0.2.0 → 0.2.3

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.
Files changed (88) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/bridge/agent-email-relay.mjs +160 -160
  3. package/bridge/app-server-websocket-transport.mjs +219 -219
  4. package/bridge/archive/service.mjs +130 -130
  5. package/bridge/auth-profile-failover.mjs +58 -58
  6. package/bridge/auth-profile-manager.mjs +215 -215
  7. package/bridge/capacity-recovery.mjs +90 -90
  8. package/bridge/gpu-usage-history.mjs +322 -322
  9. package/bridge/local/history.mjs +20 -5
  10. package/bridge/plugins/aliyun-dlc/monitor.mjs +236 -236
  11. package/bridge/plugins/aliyun-dlc/probe.mjs +701 -701
  12. package/bridge/private-gateway.mjs +371 -371
  13. package/bridge/private-start.mjs +22 -22
  14. package/bridge/probes/app-server-handoff-probe.mjs +87 -87
  15. package/bridge/probes/app-server-writer-probe.mjs +69 -69
  16. package/bridge/probes/assistant-search-probe.mjs +321 -321
  17. package/bridge/probes/dialogue-probe.mjs +462 -462
  18. package/bridge/probes/history-probe.mjs +430 -345
  19. package/bridge/probes/new-session-preflight.mjs +14 -14
  20. package/bridge/probes/profile-cancel-probe.mjs +17 -17
  21. package/bridge/probes/profile-delete-probe.mjs +26 -26
  22. package/bridge/probes/profile-list-probe.mjs +114 -114
  23. package/bridge/probes/profile-write-probe.mjs +109 -109
  24. package/bridge/probes/raw-context-probe.mjs +51 -51
  25. package/bridge/probes/remote-probe.mjs +627 -626
  26. package/bridge/probes/submission-probe.mjs +2 -2
  27. package/bridge/probes/takeover-probe.mjs +326 -326
  28. package/bridge/probes/vscode-open-preflight.mjs +21 -21
  29. package/bridge/providers/claude-interactive.py +463 -341
  30. package/bridge/providers/cli-command.mjs +103 -103
  31. package/bridge/providers/conversations.mjs +231 -231
  32. package/bridge/providers/native-commands.py +38 -38
  33. package/bridge/providers/native-session-state.mjs +14 -14
  34. package/bridge/providers/native-session-state.py +96 -96
  35. package/bridge/providers/remote-job.py +130 -130
  36. package/bridge/remote-app-server-command.mjs +121 -121
  37. package/bridge/remote-directory-listing.mjs +148 -148
  38. package/bridge/rollout-summary.mjs +20 -2
  39. package/bridge/routes/dialogue.mjs +48 -3
  40. package/bridge/routes/models.mjs +312 -312
  41. package/bridge/server.mjs +3357 -3332
  42. package/bridge/session-attachments.mjs +150 -150
  43. package/bridge/session-catalog/agy-history.py +141 -2
  44. package/bridge/session-catalog/catalog.mjs +17 -0
  45. package/bridge/session-catalog/probe.py +1207 -331
  46. package/bridge/session-catalog/routes.mjs +6 -0
  47. package/bridge/start.mjs +205 -205
  48. package/bridge/state-persistence.mjs +9 -1
  49. package/bridge/tmux-session-control.mjs +29 -29
  50. package/bridge/vscode-opener.mjs +157 -157
  51. package/bridge/web-terminal.mjs +52 -52
  52. package/bridge/workbench/manager-context.mjs +51 -51
  53. package/bridge/workbench/service.mjs +301 -301
  54. package/dist/client/.vite/manifest.json +4 -4
  55. package/dist/client/assets/{index-Enu1x0PS.js → index-BMior55c.js} +2 -2
  56. package/dist/client/assets/{layout-segment-context-CmJzExWq.js → layout-segment-context-DjGudgXn.js} +1 -1
  57. package/dist/client/assets/page-CJqXlhDs.js +551 -0
  58. package/dist/client/assets/page-LYzRz6_S.css +1 -0
  59. package/dist/client/favicon.svg +6 -6
  60. package/dist/server/__vite_rsc_assets_manifest.js +8 -8
  61. package/dist/server/index.js +7 -7
  62. package/dist/server/ssr/__vite_rsc_assets_manifest.js +8 -8
  63. package/dist/server/ssr/assets/{page-CYYyCnKD.js → page-BB9Dri2u.js} +2416 -271
  64. package/dist/server/ssr/assets/page-LYzRz6_S.css +1 -0
  65. package/dist/server/ssr/index.js +1 -1
  66. package/dist/server/ssr/vinext-server.json +1 -1
  67. package/dist/server/vinext-server.json +1 -1
  68. package/dist/server/wrangler.json +1 -1
  69. package/lib/browser-snapshot-cache.js +53 -53
  70. package/lib/formula-preview.js +1 -1
  71. package/lib/incremental-snapshot.js +300 -300
  72. package/lib/markdown-math.js +151 -151
  73. package/lib/remote-links.js +95 -95
  74. package/lib/runtime-config.js +70 -70
  75. package/lib/session-filters.js +38 -38
  76. package/lib/ssh-config.js +26 -26
  77. package/lib/stable-order.js +39 -39
  78. package/lib/subagent-display-prefs.ts +130 -0
  79. package/package.json +1 -1
  80. package/scripts/agent-email-relay.mjs +104 -104
  81. package/scripts/email-notification-cli.mjs +28 -28
  82. package/scripts/enable-private-access.ps1 +109 -109
  83. package/scripts/fixtures/native-conversations.mjs +164 -6
  84. package/scripts/fixtures/workbench-conversations.mjs +475 -352
  85. package/scripts/preview-workbench.mjs +669 -669
  86. package/dist/client/assets/page-COBv6VWj.css +0 -1
  87. package/dist/client/assets/page-DVjU6VG_.js +0 -550
  88. package/dist/server/ssr/assets/page-COBv6VWj.css +0 -1
@@ -1,322 +1,322 @@
1
- const HOUR_MS = 60 * 60 * 1_000;
2
-
3
- export const GPU_USAGE_RETENTION_HOURS = 7 * 24;
4
- export const GPU_USAGE_MAX_EPISODES = 5_000;
5
-
6
- export function emptyGpuUsageHistory() {
7
- return {
8
- version: 1,
9
- updatedAt: null,
10
- retentionHours: GPU_USAGE_RETENTION_HOURS,
11
- episodes: [],
12
- };
13
- }
14
-
15
- function finiteNumber(value, fallback = 0) {
16
- const parsed = Number(value);
17
- return Number.isFinite(parsed) ? parsed : fallback;
18
- }
19
-
20
- function cleanText(value, limit) {
21
- return String(value || "")
22
- .replace(/\s+/g, " ")
23
- .trim()
24
- .slice(0, limit);
25
- }
26
-
27
- function confidenceRank(value) {
28
- return { unmatched: 0, inferred: 1, strong: 2, exact: 3 }[value] || 0;
29
- }
30
-
31
- function normalizeEpisode(value) {
32
- if (!value || typeof value !== "object") return null;
33
- const host = cleanText(value.host, 180);
34
- const gpuIndex = finiteNumber(value.gpuIndex, -1);
35
- const pid = finiteNumber(value.pid, 0);
36
- const groupId = finiteNumber(value.groupId, pid);
37
- const firstSeenAt = Date.parse(value.firstSeenAt || "");
38
- const lastSeenAt = Date.parse(value.lastSeenAt || "");
39
- if (
40
- !host ||
41
- gpuIndex < 0 ||
42
- pid <= 0 ||
43
- !Number.isFinite(firstSeenAt) ||
44
- !Number.isFinite(lastSeenAt)
45
- ) {
46
- return null;
47
- }
48
- return {
49
- id:
50
- cleanText(value.id, 500) ||
51
- `${host}:${gpuIndex}:${groupId}:${pid}:${firstSeenAt}`,
52
- host,
53
- gpuIndex,
54
- gpuUuid: cleanText(value.gpuUuid, 160),
55
- gpuName: cleanText(value.gpuName, 240) || "GPU",
56
- workloadKey:
57
- cleanText(value.workloadKey, 400) ||
58
- `${host}:${gpuIndex}:${groupId}:${pid}`,
59
- pid,
60
- groupId,
61
- workerPids: Array.isArray(value.workerPids)
62
- ? value.workerPids
63
- .map((item) => finiteNumber(item, 0))
64
- .filter((item) => item > 0)
65
- .slice(0, 128)
66
- : [],
67
- kind: value.kind === "evaluation" ? "evaluation" : "training",
68
- command: cleanText(value.command, 180),
69
- cmdline: cleanText(value.cmdline, 1_200),
70
- cwd: cleanText(value.cwd, 800),
71
- paneSession: cleanText(value.paneSession, 160),
72
- pane: cleanText(value.pane, 80),
73
- ownerSessionId: cleanText(value.ownerSessionId, 180) || null,
74
- ownerConfidence: ["exact", "strong", "inferred"].includes(
75
- value.ownerConfidence,
76
- )
77
- ? value.ownerConfidence
78
- : "unmatched",
79
- ownerEvidence: Array.isArray(value.ownerEvidence)
80
- ? value.ownerEvidence.map((item) => cleanText(item, 300)).slice(0, 6)
81
- : [],
82
- sessionTheme: cleanText(value.sessionTheme, 180),
83
- sessionCwd: cleanText(value.sessionCwd, 800),
84
- startedAt:
85
- Number.isFinite(Date.parse(value.startedAt || ""))
86
- ? value.startedAt
87
- : new Date(firstSeenAt).toISOString(),
88
- firstSeenAt: new Date(firstSeenAt).toISOString(),
89
- lastSeenAt: new Date(lastSeenAt).toISOString(),
90
- endedAt:
91
- Number.isFinite(Date.parse(value.endedAt || "")) ? value.endedAt : null,
92
- active: Boolean(value.active),
93
- samples: Math.max(1, finiteNumber(value.samples, 1)),
94
- lastMemoryMiB: Math.max(0, finiteNumber(value.lastMemoryMiB, 0)),
95
- maxMemoryMiB: Math.max(0, finiteNumber(value.maxMemoryMiB, 0)),
96
- };
97
- }
98
-
99
- export function normalizeGpuUsageHistory(value) {
100
- const history = emptyGpuUsageHistory();
101
- if (!value || typeof value !== "object") return history;
102
- const episodes = Array.isArray(value.episodes)
103
- ? value.episodes.map(normalizeEpisode).filter(Boolean)
104
- : [];
105
- return {
106
- version: 1,
107
- updatedAt:
108
- Number.isFinite(Date.parse(value.updatedAt || ""))
109
- ? value.updatedAt
110
- : null,
111
- retentionHours: Math.max(
112
- 1,
113
- finiteNumber(value.retentionHours, GPU_USAGE_RETENTION_HOURS),
114
- ),
115
- episodes,
116
- };
117
- }
118
-
119
- function workloadGpuEntries(workload) {
120
- const byIndex = new Map();
121
- for (const entry of workload.gpus || []) {
122
- const index = finiteNumber(entry.index, -1);
123
- if (index < 0) continue;
124
- const current = byIndex.get(index) || {
125
- index,
126
- memoryMiB: 0,
127
- workerPids: new Set(),
128
- };
129
- current.memoryMiB += Math.max(0, finiteNumber(entry.memoryMiB, 0));
130
- if (finiteNumber(entry.pid, 0) > 0) {
131
- current.workerPids.add(finiteNumber(entry.pid, 0));
132
- }
133
- byIndex.set(index, current);
134
- }
135
- return [...byIndex.values()];
136
- }
137
-
138
- function sessionDetails(host, workload) {
139
- const ownerSessionId = workload.ownerSessionId || null;
140
- const agent = ownerSessionId
141
- ? (host.agents || []).find(
142
- (candidate) => candidate.rollout?.id === ownerSessionId,
143
- )
144
- : null;
145
- return {
146
- ownerSessionId,
147
- sessionTheme:
148
- workload.ownerSessionTheme ||
149
- agent?.suggestedSummary?.theme ||
150
- agent?.rollout?.lastUser ||
151
- "",
152
- sessionCwd: workload.ownerSessionCwd || agent?.cwd || workload.cwd || "",
153
- };
154
- }
155
-
156
- export function updateGpuUsageHistory(
157
- input,
158
- hosts,
159
- now = Date.now(),
160
- {
161
- retentionHours = GPU_USAGE_RETENTION_HOURS,
162
- maxEpisodes = GPU_USAGE_MAX_EPISODES,
163
- } = {},
164
- ) {
165
- const history = normalizeGpuUsageHistory(input);
166
- const nowIso = new Date(now).toISOString();
167
- const reachableHosts = new Set(
168
- (hosts || []).filter((host) => host.reachable).map((host) => host.alias),
169
- );
170
- const openByKey = new Map(
171
- history.episodes
172
- .filter((episode) => episode.active)
173
- .map((episode) => [episode.workloadKey, episode]),
174
- );
175
- const observedKeys = new Set();
176
-
177
- for (const host of hosts || []) {
178
- if (!host.reachable) continue;
179
- const gpuByIndex = new Map(
180
- (host.gpus || []).map((gpu) => [finiteNumber(gpu.index, -1), gpu]),
181
- );
182
- for (const workload of host.workloads || []) {
183
- const session = sessionDetails(host, workload);
184
- for (const gpuEntry of workloadGpuEntries(workload)) {
185
- const pid = finiteNumber(workload.pid, 0);
186
- const groupId = finiteNumber(workload.groupId, pid);
187
- if (pid <= 0 || groupId <= 0) continue;
188
- const workloadKey = `${host.alias}:${gpuEntry.index}:${groupId}:${pid}`;
189
- observedKeys.add(workloadKey);
190
- const estimatedStartedAt =
191
- now - Math.max(0, finiteNumber(workload.elapsedSeconds, 0)) * 1_000;
192
- let episode = openByKey.get(workloadKey);
193
- if (
194
- episode &&
195
- Math.abs(Date.parse(episode.startedAt) - estimatedStartedAt) >
196
- 5 * 60_000
197
- ) {
198
- episode.active = false;
199
- episode.endedAt = nowIso;
200
- episode = null;
201
- }
202
- const gpu = gpuByIndex.get(gpuEntry.index) || {};
203
- if (!episode) {
204
- episode = normalizeEpisode({
205
- id: `${workloadKey}:${now}`,
206
- host: host.alias,
207
- gpuIndex: gpuEntry.index,
208
- gpuUuid: gpu.uuid || "",
209
- gpuName: gpu.name || "GPU",
210
- workloadKey,
211
- pid,
212
- groupId,
213
- workerPids: [
214
- ...(workload.workerPids || []),
215
- ...gpuEntry.workerPids,
216
- ],
217
- kind: workload.kind,
218
- command: workload.command,
219
- cmdline: workload.cmdline,
220
- cwd: workload.cwd,
221
- paneSession: workload.pane?.session || "",
222
- pane: workload.pane?.pane || "",
223
- ownerSessionId: session.ownerSessionId,
224
- ownerConfidence: workload.ownerConfidence,
225
- ownerEvidence: workload.ownerEvidence,
226
- sessionTheme: session.sessionTheme,
227
- sessionCwd: session.sessionCwd,
228
- startedAt: new Date(estimatedStartedAt).toISOString(),
229
- firstSeenAt: nowIso,
230
- lastSeenAt: nowIso,
231
- active: true,
232
- samples: 1,
233
- lastMemoryMiB: gpuEntry.memoryMiB,
234
- maxMemoryMiB: gpuEntry.memoryMiB,
235
- });
236
- history.episodes.push(episode);
237
- openByKey.set(workloadKey, episode);
238
- continue;
239
- }
240
-
241
- episode.gpuUuid = cleanText(gpu.uuid || episode.gpuUuid, 160);
242
- episode.gpuName = cleanText(gpu.name || episode.gpuName, 240);
243
- episode.workerPids = [
244
- ...new Set([
245
- ...episode.workerPids,
246
- ...(workload.workerPids || []),
247
- ...gpuEntry.workerPids,
248
- ]),
249
- ].slice(0, 128);
250
- episode.kind =
251
- workload.kind === "evaluation" ? "evaluation" : "training";
252
- episode.command = cleanText(
253
- workload.command || episode.command,
254
- 180,
255
- );
256
- episode.cmdline = cleanText(workload.cmdline || episode.cmdline, 1_200);
257
- episode.cwd = cleanText(workload.cwd || episode.cwd, 800);
258
- episode.paneSession = cleanText(
259
- workload.pane?.session || episode.paneSession,
260
- 160,
261
- );
262
- episode.pane = cleanText(
263
- workload.pane?.pane || episode.pane,
264
- 80,
265
- );
266
- if (
267
- session.ownerSessionId &&
268
- (!episode.ownerSessionId ||
269
- confidenceRank(workload.ownerConfidence) >=
270
- confidenceRank(episode.ownerConfidence))
271
- ) {
272
- episode.ownerSessionId = session.ownerSessionId;
273
- episode.ownerConfidence = workload.ownerConfidence;
274
- episode.ownerEvidence = (workload.ownerEvidence || [])
275
- .map((item) => cleanText(item, 300))
276
- .slice(0, 6);
277
- }
278
- episode.sessionTheme = cleanText(
279
- session.sessionTheme || episode.sessionTheme,
280
- 180,
281
- );
282
- episode.sessionCwd = cleanText(
283
- session.sessionCwd || episode.sessionCwd,
284
- 800,
285
- );
286
- episode.lastSeenAt = nowIso;
287
- episode.endedAt = null;
288
- episode.active = true;
289
- episode.samples += 1;
290
- episode.lastMemoryMiB = gpuEntry.memoryMiB;
291
- episode.maxMemoryMiB = Math.max(
292
- episode.maxMemoryMiB,
293
- gpuEntry.memoryMiB,
294
- );
295
- }
296
- }
297
- }
298
-
299
- for (const episode of history.episodes) {
300
- if (
301
- episode.active &&
302
- reachableHosts.has(episode.host) &&
303
- !observedKeys.has(episode.workloadKey)
304
- ) {
305
- episode.active = false;
306
- episode.endedAt = nowIso;
307
- }
308
- }
309
-
310
- const cutoff = now - Math.max(1, retentionHours) * HOUR_MS;
311
- history.episodes = history.episodes
312
- .filter((episode) => Date.parse(episode.lastSeenAt) >= cutoff)
313
- .sort(
314
- (left, right) =>
315
- Number(right.active) - Number(left.active) ||
316
- Date.parse(right.lastSeenAt) - Date.parse(left.lastSeenAt),
317
- )
318
- .slice(0, Math.max(100, maxEpisodes));
319
- history.updatedAt = nowIso;
320
- history.retentionHours = retentionHours;
321
- return history;
322
- }
1
+ const HOUR_MS = 60 * 60 * 1_000;
2
+
3
+ export const GPU_USAGE_RETENTION_HOURS = 7 * 24;
4
+ export const GPU_USAGE_MAX_EPISODES = 5_000;
5
+
6
+ export function emptyGpuUsageHistory() {
7
+ return {
8
+ version: 1,
9
+ updatedAt: null,
10
+ retentionHours: GPU_USAGE_RETENTION_HOURS,
11
+ episodes: [],
12
+ };
13
+ }
14
+
15
+ function finiteNumber(value, fallback = 0) {
16
+ const parsed = Number(value);
17
+ return Number.isFinite(parsed) ? parsed : fallback;
18
+ }
19
+
20
+ function cleanText(value, limit) {
21
+ return String(value || "")
22
+ .replace(/\s+/g, " ")
23
+ .trim()
24
+ .slice(0, limit);
25
+ }
26
+
27
+ function confidenceRank(value) {
28
+ return { unmatched: 0, inferred: 1, strong: 2, exact: 3 }[value] || 0;
29
+ }
30
+
31
+ function normalizeEpisode(value) {
32
+ if (!value || typeof value !== "object") return null;
33
+ const host = cleanText(value.host, 180);
34
+ const gpuIndex = finiteNumber(value.gpuIndex, -1);
35
+ const pid = finiteNumber(value.pid, 0);
36
+ const groupId = finiteNumber(value.groupId, pid);
37
+ const firstSeenAt = Date.parse(value.firstSeenAt || "");
38
+ const lastSeenAt = Date.parse(value.lastSeenAt || "");
39
+ if (
40
+ !host ||
41
+ gpuIndex < 0 ||
42
+ pid <= 0 ||
43
+ !Number.isFinite(firstSeenAt) ||
44
+ !Number.isFinite(lastSeenAt)
45
+ ) {
46
+ return null;
47
+ }
48
+ return {
49
+ id:
50
+ cleanText(value.id, 500) ||
51
+ `${host}:${gpuIndex}:${groupId}:${pid}:${firstSeenAt}`,
52
+ host,
53
+ gpuIndex,
54
+ gpuUuid: cleanText(value.gpuUuid, 160),
55
+ gpuName: cleanText(value.gpuName, 240) || "GPU",
56
+ workloadKey:
57
+ cleanText(value.workloadKey, 400) ||
58
+ `${host}:${gpuIndex}:${groupId}:${pid}`,
59
+ pid,
60
+ groupId,
61
+ workerPids: Array.isArray(value.workerPids)
62
+ ? value.workerPids
63
+ .map((item) => finiteNumber(item, 0))
64
+ .filter((item) => item > 0)
65
+ .slice(0, 128)
66
+ : [],
67
+ kind: value.kind === "evaluation" ? "evaluation" : "training",
68
+ command: cleanText(value.command, 180),
69
+ cmdline: cleanText(value.cmdline, 1_200),
70
+ cwd: cleanText(value.cwd, 800),
71
+ paneSession: cleanText(value.paneSession, 160),
72
+ pane: cleanText(value.pane, 80),
73
+ ownerSessionId: cleanText(value.ownerSessionId, 180) || null,
74
+ ownerConfidence: ["exact", "strong", "inferred"].includes(
75
+ value.ownerConfidence,
76
+ )
77
+ ? value.ownerConfidence
78
+ : "unmatched",
79
+ ownerEvidence: Array.isArray(value.ownerEvidence)
80
+ ? value.ownerEvidence.map((item) => cleanText(item, 300)).slice(0, 6)
81
+ : [],
82
+ sessionTheme: cleanText(value.sessionTheme, 180),
83
+ sessionCwd: cleanText(value.sessionCwd, 800),
84
+ startedAt:
85
+ Number.isFinite(Date.parse(value.startedAt || ""))
86
+ ? value.startedAt
87
+ : new Date(firstSeenAt).toISOString(),
88
+ firstSeenAt: new Date(firstSeenAt).toISOString(),
89
+ lastSeenAt: new Date(lastSeenAt).toISOString(),
90
+ endedAt:
91
+ Number.isFinite(Date.parse(value.endedAt || "")) ? value.endedAt : null,
92
+ active: Boolean(value.active),
93
+ samples: Math.max(1, finiteNumber(value.samples, 1)),
94
+ lastMemoryMiB: Math.max(0, finiteNumber(value.lastMemoryMiB, 0)),
95
+ maxMemoryMiB: Math.max(0, finiteNumber(value.maxMemoryMiB, 0)),
96
+ };
97
+ }
98
+
99
+ export function normalizeGpuUsageHistory(value) {
100
+ const history = emptyGpuUsageHistory();
101
+ if (!value || typeof value !== "object") return history;
102
+ const episodes = Array.isArray(value.episodes)
103
+ ? value.episodes.map(normalizeEpisode).filter(Boolean)
104
+ : [];
105
+ return {
106
+ version: 1,
107
+ updatedAt:
108
+ Number.isFinite(Date.parse(value.updatedAt || ""))
109
+ ? value.updatedAt
110
+ : null,
111
+ retentionHours: Math.max(
112
+ 1,
113
+ finiteNumber(value.retentionHours, GPU_USAGE_RETENTION_HOURS),
114
+ ),
115
+ episodes,
116
+ };
117
+ }
118
+
119
+ function workloadGpuEntries(workload) {
120
+ const byIndex = new Map();
121
+ for (const entry of workload.gpus || []) {
122
+ const index = finiteNumber(entry.index, -1);
123
+ if (index < 0) continue;
124
+ const current = byIndex.get(index) || {
125
+ index,
126
+ memoryMiB: 0,
127
+ workerPids: new Set(),
128
+ };
129
+ current.memoryMiB += Math.max(0, finiteNumber(entry.memoryMiB, 0));
130
+ if (finiteNumber(entry.pid, 0) > 0) {
131
+ current.workerPids.add(finiteNumber(entry.pid, 0));
132
+ }
133
+ byIndex.set(index, current);
134
+ }
135
+ return [...byIndex.values()];
136
+ }
137
+
138
+ function sessionDetails(host, workload) {
139
+ const ownerSessionId = workload.ownerSessionId || null;
140
+ const agent = ownerSessionId
141
+ ? (host.agents || []).find(
142
+ (candidate) => candidate.rollout?.id === ownerSessionId,
143
+ )
144
+ : null;
145
+ return {
146
+ ownerSessionId,
147
+ sessionTheme:
148
+ workload.ownerSessionTheme ||
149
+ agent?.suggestedSummary?.theme ||
150
+ agent?.rollout?.lastUser ||
151
+ "",
152
+ sessionCwd: workload.ownerSessionCwd || agent?.cwd || workload.cwd || "",
153
+ };
154
+ }
155
+
156
+ export function updateGpuUsageHistory(
157
+ input,
158
+ hosts,
159
+ now = Date.now(),
160
+ {
161
+ retentionHours = GPU_USAGE_RETENTION_HOURS,
162
+ maxEpisodes = GPU_USAGE_MAX_EPISODES,
163
+ } = {},
164
+ ) {
165
+ const history = normalizeGpuUsageHistory(input);
166
+ const nowIso = new Date(now).toISOString();
167
+ const reachableHosts = new Set(
168
+ (hosts || []).filter((host) => host.reachable).map((host) => host.alias),
169
+ );
170
+ const openByKey = new Map(
171
+ history.episodes
172
+ .filter((episode) => episode.active)
173
+ .map((episode) => [episode.workloadKey, episode]),
174
+ );
175
+ const observedKeys = new Set();
176
+
177
+ for (const host of hosts || []) {
178
+ if (!host.reachable) continue;
179
+ const gpuByIndex = new Map(
180
+ (host.gpus || []).map((gpu) => [finiteNumber(gpu.index, -1), gpu]),
181
+ );
182
+ for (const workload of host.workloads || []) {
183
+ const session = sessionDetails(host, workload);
184
+ for (const gpuEntry of workloadGpuEntries(workload)) {
185
+ const pid = finiteNumber(workload.pid, 0);
186
+ const groupId = finiteNumber(workload.groupId, pid);
187
+ if (pid <= 0 || groupId <= 0) continue;
188
+ const workloadKey = `${host.alias}:${gpuEntry.index}:${groupId}:${pid}`;
189
+ observedKeys.add(workloadKey);
190
+ const estimatedStartedAt =
191
+ now - Math.max(0, finiteNumber(workload.elapsedSeconds, 0)) * 1_000;
192
+ let episode = openByKey.get(workloadKey);
193
+ if (
194
+ episode &&
195
+ Math.abs(Date.parse(episode.startedAt) - estimatedStartedAt) >
196
+ 5 * 60_000
197
+ ) {
198
+ episode.active = false;
199
+ episode.endedAt = nowIso;
200
+ episode = null;
201
+ }
202
+ const gpu = gpuByIndex.get(gpuEntry.index) || {};
203
+ if (!episode) {
204
+ episode = normalizeEpisode({
205
+ id: `${workloadKey}:${now}`,
206
+ host: host.alias,
207
+ gpuIndex: gpuEntry.index,
208
+ gpuUuid: gpu.uuid || "",
209
+ gpuName: gpu.name || "GPU",
210
+ workloadKey,
211
+ pid,
212
+ groupId,
213
+ workerPids: [
214
+ ...(workload.workerPids || []),
215
+ ...gpuEntry.workerPids,
216
+ ],
217
+ kind: workload.kind,
218
+ command: workload.command,
219
+ cmdline: workload.cmdline,
220
+ cwd: workload.cwd,
221
+ paneSession: workload.pane?.session || "",
222
+ pane: workload.pane?.pane || "",
223
+ ownerSessionId: session.ownerSessionId,
224
+ ownerConfidence: workload.ownerConfidence,
225
+ ownerEvidence: workload.ownerEvidence,
226
+ sessionTheme: session.sessionTheme,
227
+ sessionCwd: session.sessionCwd,
228
+ startedAt: new Date(estimatedStartedAt).toISOString(),
229
+ firstSeenAt: nowIso,
230
+ lastSeenAt: nowIso,
231
+ active: true,
232
+ samples: 1,
233
+ lastMemoryMiB: gpuEntry.memoryMiB,
234
+ maxMemoryMiB: gpuEntry.memoryMiB,
235
+ });
236
+ history.episodes.push(episode);
237
+ openByKey.set(workloadKey, episode);
238
+ continue;
239
+ }
240
+
241
+ episode.gpuUuid = cleanText(gpu.uuid || episode.gpuUuid, 160);
242
+ episode.gpuName = cleanText(gpu.name || episode.gpuName, 240);
243
+ episode.workerPids = [
244
+ ...new Set([
245
+ ...episode.workerPids,
246
+ ...(workload.workerPids || []),
247
+ ...gpuEntry.workerPids,
248
+ ]),
249
+ ].slice(0, 128);
250
+ episode.kind =
251
+ workload.kind === "evaluation" ? "evaluation" : "training";
252
+ episode.command = cleanText(
253
+ workload.command || episode.command,
254
+ 180,
255
+ );
256
+ episode.cmdline = cleanText(workload.cmdline || episode.cmdline, 1_200);
257
+ episode.cwd = cleanText(workload.cwd || episode.cwd, 800);
258
+ episode.paneSession = cleanText(
259
+ workload.pane?.session || episode.paneSession,
260
+ 160,
261
+ );
262
+ episode.pane = cleanText(
263
+ workload.pane?.pane || episode.pane,
264
+ 80,
265
+ );
266
+ if (
267
+ session.ownerSessionId &&
268
+ (!episode.ownerSessionId ||
269
+ confidenceRank(workload.ownerConfidence) >=
270
+ confidenceRank(episode.ownerConfidence))
271
+ ) {
272
+ episode.ownerSessionId = session.ownerSessionId;
273
+ episode.ownerConfidence = workload.ownerConfidence;
274
+ episode.ownerEvidence = (workload.ownerEvidence || [])
275
+ .map((item) => cleanText(item, 300))
276
+ .slice(0, 6);
277
+ }
278
+ episode.sessionTheme = cleanText(
279
+ session.sessionTheme || episode.sessionTheme,
280
+ 180,
281
+ );
282
+ episode.sessionCwd = cleanText(
283
+ session.sessionCwd || episode.sessionCwd,
284
+ 800,
285
+ );
286
+ episode.lastSeenAt = nowIso;
287
+ episode.endedAt = null;
288
+ episode.active = true;
289
+ episode.samples += 1;
290
+ episode.lastMemoryMiB = gpuEntry.memoryMiB;
291
+ episode.maxMemoryMiB = Math.max(
292
+ episode.maxMemoryMiB,
293
+ gpuEntry.memoryMiB,
294
+ );
295
+ }
296
+ }
297
+ }
298
+
299
+ for (const episode of history.episodes) {
300
+ if (
301
+ episode.active &&
302
+ reachableHosts.has(episode.host) &&
303
+ !observedKeys.has(episode.workloadKey)
304
+ ) {
305
+ episode.active = false;
306
+ episode.endedAt = nowIso;
307
+ }
308
+ }
309
+
310
+ const cutoff = now - Math.max(1, retentionHours) * HOUR_MS;
311
+ history.episodes = history.episodes
312
+ .filter((episode) => Date.parse(episode.lastSeenAt) >= cutoff)
313
+ .sort(
314
+ (left, right) =>
315
+ Number(right.active) - Number(left.active) ||
316
+ Date.parse(right.lastSeenAt) - Date.parse(left.lastSeenAt),
317
+ )
318
+ .slice(0, Math.max(100, maxEpisodes));
319
+ history.updatedAt = nowIso;
320
+ history.retentionHours = retentionHours;
321
+ return history;
322
+ }