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,324 +1,324 @@
1
1
  import { preserveSnapshotSessions } from "./session-cache-guard.js";
2
2
  import { overviewDelta, applyOverviewDelta } from "./overview-delta.js";
3
3
  const SNAPSHOT_SYNC_PROTOCOL = 1;
4
-
5
- function compactRollout(rollout) {
6
- if (!rollout) return null;
7
- return {
8
- id: rollout.id || "",
9
- rootSessionId: rollout.rootSessionId || "",
10
- parentThreadId: rollout.parentThreadId || "",
11
- agentPath: rollout.agentPath || "",
12
- agentNickname: rollout.agentNickname || "",
13
- isSubagent: Boolean(rollout.isSubagent),
14
- cwd: rollout.cwd || "",
15
- startedAt: rollout.startedAt || "",
16
- cliVersion: rollout.cliVersion || "",
17
- path: rollout.path || "",
18
- mtimeMs: rollout.mtimeMs || 0,
19
- lastUser: rollout.lastUser || "",
20
- lastAssistant: rollout.lastAssistant || "",
21
- lastAction: rollout.lastAction || "",
22
- lastError: rollout.lastError || "",
23
- firstUser: rollout.firstUser || "",
24
- userMessages: [],
25
- assistantMessages: [],
26
- model: rollout.model || "",
27
- reasoningEffort: rollout.reasoningEffort || "",
28
- approvalPolicy: rollout.approvalPolicy || "",
29
- sandboxPolicy: rollout.sandboxPolicy || "",
30
- collaborationMode: rollout.collaborationMode || "",
31
- launches: [],
32
- account: rollout.account || null,
33
- tokenUsage: rollout.tokenUsage || null,
34
- turnState: rollout.turnState || "idle",
4
+
5
+ function compactRollout(rollout) {
6
+ if (!rollout) return null;
7
+ return {
8
+ id: rollout.id || "",
9
+ rootSessionId: rollout.rootSessionId || "",
10
+ parentThreadId: rollout.parentThreadId || "",
11
+ agentPath: rollout.agentPath || "",
12
+ agentNickname: rollout.agentNickname || "",
13
+ isSubagent: Boolean(rollout.isSubagent),
14
+ cwd: rollout.cwd || "",
15
+ startedAt: rollout.startedAt || "",
16
+ cliVersion: rollout.cliVersion || "",
17
+ path: rollout.path || "",
18
+ mtimeMs: rollout.mtimeMs || 0,
19
+ lastUser: rollout.lastUser || "",
20
+ lastAssistant: rollout.lastAssistant || "",
21
+ lastAction: rollout.lastAction || "",
22
+ lastError: rollout.lastError || "",
23
+ firstUser: rollout.firstUser || "",
24
+ userMessages: [],
25
+ assistantMessages: [],
26
+ model: rollout.model || "",
27
+ reasoningEffort: rollout.reasoningEffort || "",
28
+ approvalPolicy: rollout.approvalPolicy || "",
29
+ sandboxPolicy: rollout.sandboxPolicy || "",
30
+ collaborationMode: rollout.collaborationMode || "",
31
+ launches: [],
32
+ account: rollout.account || null,
33
+ tokenUsage: rollout.tokenUsage || null,
34
+ turnState: rollout.turnState || "idle",
35
35
  lastEventAt: rollout.lastEventAt || "",
36
36
  cacheRecovery: rollout.cacheRecovery,
37
- };
38
- }
39
-
40
- function compactRemoteJob(job) {
41
- return {
42
- ...job,
43
- evidence: "",
44
- };
45
- }
46
-
47
- function compactHost(host) {
48
- return {
49
- ...host,
50
- processes: [],
51
- sessions: (host.sessions || []).map(compactRollout),
52
- agents: (host.agents || []).map((agent) => ({
53
- ...agent,
54
- rollout: compactRollout(agent.rollout),
55
- subagents: (agent.subagents || []).map(compactRollout),
56
- remoteJobs: (agent.remoteJobs || []).map(compactRemoteJob),
57
- })),
58
- };
59
- }
60
-
61
- /**
62
- * Remove resources that are either large, independently versioned, or already
63
- * available through a dedicated detail endpoint. The overview remains a valid
64
- * Snapshot once gpuUsage and submissionTracker are merged back in.
65
- */
66
- export function compactSnapshotOverview(snapshot) {
67
- const rest = { ...snapshot };
68
- delete rest.gpuUsage;
69
- delete rest.submissionTracker;
70
- return {
71
- ...rest,
72
- hosts: (snapshot.hosts || []).map(compactHost),
73
- };
74
- }
75
-
76
- export function splitSnapshotResources(snapshot) {
77
- return {
78
- overview: compactSnapshotOverview(snapshot),
79
- gpuUsage: snapshot.gpuUsage || {
80
- updatedAt: null,
81
- retentionHours: 168,
82
- episodes: [],
83
- },
84
- submissionTracker: snapshot.submissionTracker || {
85
- scannedAt: null,
86
- scanning: false,
87
- items: [],
88
- calls: [],
89
- launches: [],
90
- alerts: [],
91
- hosts: [],
92
- error: null,
93
- },
94
- };
95
- }
96
-
97
- function episodeMap(gpuUsage) {
98
- return new Map(
99
- (gpuUsage?.episodes || []).map((episode) => [episode.id, episode]),
100
- );
101
- }
102
-
103
- function gpuMeta(gpuUsage) {
104
- return {
105
- updatedAt: gpuUsage?.updatedAt || null,
106
- retentionHours: gpuUsage?.retentionHours || 168,
107
- };
108
- }
109
-
110
- function fullPayload(epoch, revision, resources) {
111
- return {
112
- protocol: SNAPSHOT_SYNC_PROTOCOL,
113
- epoch,
114
- revision,
115
- reset: true,
116
- overview: resources.overview,
117
- gpuUsage: resources.gpuUsage,
118
- submissionTracker: resources.submissionTracker,
119
- };
120
- }
121
-
122
- /**
123
- * Keep a bounded, in-memory revision log. Old clients fall back to one reset;
124
- * active clients receive only the compact overview and changed resource rows.
125
- */
126
- export function createIncrementalSnapshotStore({
127
- epoch,
128
- maxHistory = 32,
129
- maxHistoryBytes = 16 * 1024 * 1024,
130
- } = {}) {
131
- const storeEpoch = epoch || `${Date.now()}-${Math.random().toString(36).slice(2)}`;
132
- let revision = 0;
133
- let current = null;
134
- let submissionJson = "";
135
- let gpuMetaJson = "";
136
- let gpuEpisodeJson = new Map();
137
- let history = [];
138
- let historyBytes = 0;
139
-
140
- function publish(snapshot) {
141
- const next = splitSnapshotResources(snapshot);
142
- const nextSubmissionJson = JSON.stringify(next.submissionTracker);
143
- const nextGpuMeta = gpuMeta(next.gpuUsage);
144
- const nextGpuMetaJson = JSON.stringify(nextGpuMeta);
145
- const nextGpuEpisodes = episodeMap(next.gpuUsage);
146
- const nextGpuEpisodeJson = new Map(
147
- [...nextGpuEpisodes].map(([id, episode]) => [id, JSON.stringify(episode)]),
148
- );
149
-
150
- if (!current) {
151
- revision = 1;
152
- current = next;
153
- submissionJson = nextSubmissionJson;
154
- gpuMetaJson = nextGpuMetaJson;
155
- gpuEpisodeJson = nextGpuEpisodeJson;
156
- return revision;
157
- }
158
-
159
- const patch = {};
37
+ };
38
+ }
39
+
40
+ function compactRemoteJob(job) {
41
+ return {
42
+ ...job,
43
+ evidence: "",
44
+ };
45
+ }
46
+
47
+ function compactHost(host) {
48
+ return {
49
+ ...host,
50
+ processes: [],
51
+ sessions: (host.sessions || []).map(compactRollout),
52
+ agents: (host.agents || []).map((agent) => ({
53
+ ...agent,
54
+ rollout: compactRollout(agent.rollout),
55
+ subagents: (agent.subagents || []).map(compactRollout),
56
+ remoteJobs: (agent.remoteJobs || []).map(compactRemoteJob),
57
+ })),
58
+ };
59
+ }
60
+
61
+ /**
62
+ * Remove resources that are either large, independently versioned, or already
63
+ * available through a dedicated detail endpoint. The overview remains a valid
64
+ * Snapshot once gpuUsage and submissionTracker are merged back in.
65
+ */
66
+ export function compactSnapshotOverview(snapshot) {
67
+ const rest = { ...snapshot };
68
+ delete rest.gpuUsage;
69
+ delete rest.submissionTracker;
70
+ return {
71
+ ...rest,
72
+ hosts: (snapshot.hosts || []).map(compactHost),
73
+ };
74
+ }
75
+
76
+ export function splitSnapshotResources(snapshot) {
77
+ return {
78
+ overview: compactSnapshotOverview(snapshot),
79
+ gpuUsage: snapshot.gpuUsage || {
80
+ updatedAt: null,
81
+ retentionHours: 168,
82
+ episodes: [],
83
+ },
84
+ submissionTracker: snapshot.submissionTracker || {
85
+ scannedAt: null,
86
+ scanning: false,
87
+ items: [],
88
+ calls: [],
89
+ launches: [],
90
+ alerts: [],
91
+ hosts: [],
92
+ error: null,
93
+ },
94
+ };
95
+ }
96
+
97
+ function episodeMap(gpuUsage) {
98
+ return new Map(
99
+ (gpuUsage?.episodes || []).map((episode) => [episode.id, episode]),
100
+ );
101
+ }
102
+
103
+ function gpuMeta(gpuUsage) {
104
+ return {
105
+ updatedAt: gpuUsage?.updatedAt || null,
106
+ retentionHours: gpuUsage?.retentionHours || 168,
107
+ };
108
+ }
109
+
110
+ function fullPayload(epoch, revision, resources) {
111
+ return {
112
+ protocol: SNAPSHOT_SYNC_PROTOCOL,
113
+ epoch,
114
+ revision,
115
+ reset: true,
116
+ overview: resources.overview,
117
+ gpuUsage: resources.gpuUsage,
118
+ submissionTracker: resources.submissionTracker,
119
+ };
120
+ }
121
+
122
+ /**
123
+ * Keep a bounded, in-memory revision log. Old clients fall back to one reset;
124
+ * active clients receive only the compact overview and changed resource rows.
125
+ */
126
+ export function createIncrementalSnapshotStore({
127
+ epoch,
128
+ maxHistory = 32,
129
+ maxHistoryBytes = 16 * 1024 * 1024,
130
+ } = {}) {
131
+ const storeEpoch = epoch || `${Date.now()}-${Math.random().toString(36).slice(2)}`;
132
+ let revision = 0;
133
+ let current = null;
134
+ let submissionJson = "";
135
+ let gpuMetaJson = "";
136
+ let gpuEpisodeJson = new Map();
137
+ let history = [];
138
+ let historyBytes = 0;
139
+
140
+ function publish(snapshot) {
141
+ const next = splitSnapshotResources(snapshot);
142
+ const nextSubmissionJson = JSON.stringify(next.submissionTracker);
143
+ const nextGpuMeta = gpuMeta(next.gpuUsage);
144
+ const nextGpuMetaJson = JSON.stringify(nextGpuMeta);
145
+ const nextGpuEpisodes = episodeMap(next.gpuUsage);
146
+ const nextGpuEpisodeJson = new Map(
147
+ [...nextGpuEpisodes].map(([id, episode]) => [id, JSON.stringify(episode)]),
148
+ );
149
+
150
+ if (!current) {
151
+ revision = 1;
152
+ current = next;
153
+ submissionJson = nextSubmissionJson;
154
+ gpuMetaJson = nextGpuMetaJson;
155
+ gpuEpisodeJson = nextGpuEpisodeJson;
156
+ return revision;
157
+ }
158
+
159
+ const patch = {};
160
160
  const overviewPatch = overviewDelta(current.overview, next.overview);
161
161
  if (overviewPatch) patch.overviewDelta = overviewPatch;
162
- if (nextSubmissionJson !== submissionJson) {
163
- patch.submissionTracker = next.submissionTracker;
164
- }
165
-
166
- const upserts = [];
167
- const removes = [];
168
- for (const [id, episode] of nextGpuEpisodes) {
169
- if (nextGpuEpisodeJson.get(id) !== gpuEpisodeJson.get(id)) {
170
- upserts.push(episode);
171
- }
172
- }
173
- for (const id of gpuEpisodeJson.keys()) {
174
- if (!nextGpuEpisodeJson.has(id)) removes.push(id);
175
- }
176
- if (
177
- nextGpuMetaJson !== gpuMetaJson ||
178
- upserts.length > 0 ||
179
- removes.length > 0
180
- ) {
181
- patch.gpuUsage = {
182
- meta: nextGpuMeta,
183
- upserts,
184
- removes,
185
- };
186
- }
187
-
188
- if (Object.keys(patch).length === 0) return revision;
189
-
190
- revision += 1;
191
- const entry = {
192
- revision,
193
- patch,
162
+ if (nextSubmissionJson !== submissionJson) {
163
+ patch.submissionTracker = next.submissionTracker;
164
+ }
165
+
166
+ const upserts = [];
167
+ const removes = [];
168
+ for (const [id, episode] of nextGpuEpisodes) {
169
+ if (nextGpuEpisodeJson.get(id) !== gpuEpisodeJson.get(id)) {
170
+ upserts.push(episode);
171
+ }
172
+ }
173
+ for (const id of gpuEpisodeJson.keys()) {
174
+ if (!nextGpuEpisodeJson.has(id)) removes.push(id);
175
+ }
176
+ if (
177
+ nextGpuMetaJson !== gpuMetaJson ||
178
+ upserts.length > 0 ||
179
+ removes.length > 0
180
+ ) {
181
+ patch.gpuUsage = {
182
+ meta: nextGpuMeta,
183
+ upserts,
184
+ removes,
185
+ };
186
+ }
187
+
188
+ if (Object.keys(patch).length === 0) return revision;
189
+
190
+ revision += 1;
191
+ const entry = {
192
+ revision,
193
+ patch,
194
194
  bytes: new TextEncoder().encode(JSON.stringify(patch)).byteLength,
195
- };
196
- history.push(entry);
197
- historyBytes += entry.bytes;
198
- while (
199
- history.length > maxHistory ||
200
- (historyBytes > maxHistoryBytes && history.length > 1)
201
- ) {
202
- historyBytes -= history.shift().bytes;
203
- }
204
-
205
- current = next;
206
- submissionJson = nextSubmissionJson;
207
- gpuMetaJson = nextGpuMetaJson;
208
- gpuEpisodeJson = nextGpuEpisodeJson;
209
- return revision;
210
- }
211
-
195
+ };
196
+ history.push(entry);
197
+ historyBytes += entry.bytes;
198
+ while (
199
+ history.length > maxHistory ||
200
+ (historyBytes > maxHistoryBytes && history.length > 1)
201
+ ) {
202
+ historyBytes -= history.shift().bytes;
203
+ }
204
+
205
+ current = next;
206
+ submissionJson = nextSubmissionJson;
207
+ gpuMetaJson = nextGpuMetaJson;
208
+ gpuEpisodeJson = nextGpuEpisodeJson;
209
+ return revision;
210
+ }
211
+
212
212
  function read({ clientEpoch, since, fieldDeltas = false } = {}) {
213
- if (!current) return null;
214
- const requestedRevision = Number(since);
215
- const minimumBaseRevision = history.length
216
- ? history[0].revision - 1
217
- : revision;
218
- if (
219
- clientEpoch !== storeEpoch ||
220
- !Number.isInteger(requestedRevision) ||
221
- requestedRevision < minimumBaseRevision ||
222
- requestedRevision > revision
223
- ) {
224
- return fullPayload(storeEpoch, revision, current);
225
- }
226
-
227
- const patches = history.filter(
228
- (entry) => entry.revision > requestedRevision,
229
- );
230
- const merged = {};
231
- const gpuUpserts = new Map();
232
- const gpuRemoves = new Set();
233
- let gpuMetaPatch = null;
234
- for (const { patch } of patches) {
213
+ if (!current) return null;
214
+ const requestedRevision = Number(since);
215
+ const minimumBaseRevision = history.length
216
+ ? history[0].revision - 1
217
+ : revision;
218
+ if (
219
+ clientEpoch !== storeEpoch ||
220
+ !Number.isInteger(requestedRevision) ||
221
+ requestedRevision < minimumBaseRevision ||
222
+ requestedRevision > revision
223
+ ) {
224
+ return fullPayload(storeEpoch, revision, current);
225
+ }
226
+
227
+ const patches = history.filter(
228
+ (entry) => entry.revision > requestedRevision,
229
+ );
230
+ const merged = {};
231
+ const gpuUpserts = new Map();
232
+ const gpuRemoves = new Set();
233
+ let gpuMetaPatch = null;
234
+ for (const { patch } of patches) {
235
235
  if (patch.overviewDelta) {
236
236
  if (fieldDeltas) (merged.overviewPatches ||= []).push(patch.overviewDelta);
237
237
  else merged.overview = current.overview; // Existing clients retain the old contract.
238
238
  }
239
- if (patch.submissionTracker) {
240
- merged.submissionTracker = patch.submissionTracker;
241
- }
242
- if (!patch.gpuUsage) continue;
243
- gpuMetaPatch = patch.gpuUsage.meta;
244
- for (const id of patch.gpuUsage.removes) {
245
- gpuUpserts.delete(id);
246
- gpuRemoves.add(id);
247
- }
248
- for (const episode of patch.gpuUsage.upserts) {
249
- gpuRemoves.delete(episode.id);
250
- gpuUpserts.set(episode.id, episode);
251
- }
252
- }
253
- if (gpuMetaPatch || gpuUpserts.size || gpuRemoves.size) {
254
- merged.gpuUsage = {
255
- meta: gpuMetaPatch || gpuMeta(current.gpuUsage),
256
- upserts: [...gpuUpserts.values()],
257
- removes: [...gpuRemoves],
258
- };
259
- }
260
- return {
261
- protocol: SNAPSHOT_SYNC_PROTOCOL,
262
- epoch: storeEpoch,
263
- fromRevision: requestedRevision,
264
- revision,
265
- reset: false,
266
- ...merged,
267
- };
268
- }
269
-
270
- function info() {
271
- return {
272
- protocol: SNAPSHOT_SYNC_PROTOCOL,
273
- epoch: storeEpoch,
274
- revision,
275
- retainedRevisions: history.length,
276
- retainedBytes: historyBytes,
277
- };
278
- }
279
-
280
- return { publish, read, info };
281
- }
282
-
283
- /** Merge either a reset or a delta response into the browser's local cache. */
284
- export function applySnapshotSync(previousSnapshot, payload) {
285
- if (!payload || payload.protocol !== SNAPSHOT_SYNC_PROTOCOL) {
286
- throw new Error("不支持的 Snapshot 增量协议");
287
- }
288
- if (payload.reset) {
289
- if (!payload.overview || !payload.gpuUsage || !payload.submissionTracker) {
290
- throw new Error("Snapshot reset 缺少完整资源");
291
- }
239
+ if (patch.submissionTracker) {
240
+ merged.submissionTracker = patch.submissionTracker;
241
+ }
242
+ if (!patch.gpuUsage) continue;
243
+ gpuMetaPatch = patch.gpuUsage.meta;
244
+ for (const id of patch.gpuUsage.removes) {
245
+ gpuUpserts.delete(id);
246
+ gpuRemoves.add(id);
247
+ }
248
+ for (const episode of patch.gpuUsage.upserts) {
249
+ gpuRemoves.delete(episode.id);
250
+ gpuUpserts.set(episode.id, episode);
251
+ }
252
+ }
253
+ if (gpuMetaPatch || gpuUpserts.size || gpuRemoves.size) {
254
+ merged.gpuUsage = {
255
+ meta: gpuMetaPatch || gpuMeta(current.gpuUsage),
256
+ upserts: [...gpuUpserts.values()],
257
+ removes: [...gpuRemoves],
258
+ };
259
+ }
260
+ return {
261
+ protocol: SNAPSHOT_SYNC_PROTOCOL,
262
+ epoch: storeEpoch,
263
+ fromRevision: requestedRevision,
264
+ revision,
265
+ reset: false,
266
+ ...merged,
267
+ };
268
+ }
269
+
270
+ function info() {
271
+ return {
272
+ protocol: SNAPSHOT_SYNC_PROTOCOL,
273
+ epoch: storeEpoch,
274
+ revision,
275
+ retainedRevisions: history.length,
276
+ retainedBytes: historyBytes,
277
+ };
278
+ }
279
+
280
+ return { publish, read, info };
281
+ }
282
+
283
+ /** Merge either a reset or a delta response into the browser's local cache. */
284
+ export function applySnapshotSync(previousSnapshot, payload) {
285
+ if (!payload || payload.protocol !== SNAPSHOT_SYNC_PROTOCOL) {
286
+ throw new Error("不支持的 Snapshot 增量协议");
287
+ }
288
+ if (payload.reset) {
289
+ if (!payload.overview || !payload.gpuUsage || !payload.submissionTracker) {
290
+ throw new Error("Snapshot reset 缺少完整资源");
291
+ }
292
292
  return preserveSnapshotSessions(previousSnapshot, {
293
293
  ...payload.overview,
294
294
  gpuUsage: payload.gpuUsage,
295
295
  submissionTracker: payload.submissionTracker,
296
296
  });
297
- }
298
- if (!previousSnapshot) {
299
- throw new Error("本地 Snapshot 缓存缺失,无法应用增量");
300
- }
301
-
302
- let gpuUsage = previousSnapshot.gpuUsage;
303
- if (payload.gpuUsage) {
304
- const episodes = episodeMap(gpuUsage);
305
- for (const id of payload.gpuUsage.removes || []) episodes.delete(id);
306
- for (const episode of payload.gpuUsage.upserts || []) {
307
- episodes.set(episode.id, episode);
308
- }
309
- gpuUsage = {
310
- ...gpuUsage,
311
- ...(payload.gpuUsage.meta || {}),
312
- episodes: [...episodes.values()],
313
- };
314
- }
315
-
297
+ }
298
+ if (!previousSnapshot) {
299
+ throw new Error("本地 Snapshot 缓存缺失,无法应用增量");
300
+ }
301
+
302
+ let gpuUsage = previousSnapshot.gpuUsage;
303
+ if (payload.gpuUsage) {
304
+ const episodes = episodeMap(gpuUsage);
305
+ for (const id of payload.gpuUsage.removes || []) episodes.delete(id);
306
+ for (const episode of payload.gpuUsage.upserts || []) {
307
+ episodes.set(episode.id, episode);
308
+ }
309
+ gpuUsage = {
310
+ ...gpuUsage,
311
+ ...(payload.gpuUsage.meta || {}),
312
+ episodes: [...episodes.values()],
313
+ };
314
+ }
315
+
316
316
  let overview = { ...previousSnapshot, ...(payload.overview || {}) };
317
317
  for (const patch of payload.overviewPatches || []) overview = applyOverviewDelta(overview, patch);
318
318
  return preserveSnapshotSessions(previousSnapshot, {
319
319
  ...overview,
320
- gpuUsage,
321
- submissionTracker:
322
- payload.submissionTracker || previousSnapshot.submissionTracker,
320
+ gpuUsage,
321
+ submissionTracker:
322
+ payload.submissionTracker || previousSnapshot.submissionTracker,
323
323
  });
324
324
  }