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.
- package/CHANGELOG.md +41 -0
- package/bridge/agent-email-relay.mjs +160 -160
- package/bridge/app-server-websocket-transport.mjs +219 -219
- package/bridge/archive/service.mjs +130 -130
- package/bridge/auth-profile-failover.mjs +58 -58
- package/bridge/auth-profile-manager.mjs +215 -215
- package/bridge/capacity-recovery.mjs +90 -90
- package/bridge/gpu-usage-history.mjs +322 -322
- package/bridge/local/history.mjs +20 -5
- package/bridge/plugins/aliyun-dlc/monitor.mjs +236 -236
- package/bridge/plugins/aliyun-dlc/probe.mjs +701 -701
- package/bridge/private-gateway.mjs +371 -371
- package/bridge/private-start.mjs +22 -22
- package/bridge/probes/app-server-handoff-probe.mjs +87 -87
- package/bridge/probes/app-server-writer-probe.mjs +69 -69
- package/bridge/probes/assistant-search-probe.mjs +321 -321
- package/bridge/probes/dialogue-probe.mjs +462 -462
- package/bridge/probes/history-probe.mjs +430 -345
- package/bridge/probes/new-session-preflight.mjs +14 -14
- package/bridge/probes/profile-cancel-probe.mjs +17 -17
- package/bridge/probes/profile-delete-probe.mjs +26 -26
- package/bridge/probes/profile-list-probe.mjs +114 -114
- package/bridge/probes/profile-write-probe.mjs +109 -109
- package/bridge/probes/raw-context-probe.mjs +51 -51
- package/bridge/probes/remote-probe.mjs +627 -626
- package/bridge/probes/submission-probe.mjs +2 -2
- package/bridge/probes/takeover-probe.mjs +326 -326
- package/bridge/probes/vscode-open-preflight.mjs +21 -21
- package/bridge/providers/claude-interactive.py +463 -341
- package/bridge/providers/cli-command.mjs +103 -103
- package/bridge/providers/conversations.mjs +231 -231
- package/bridge/providers/native-commands.py +38 -38
- package/bridge/providers/native-session-state.mjs +14 -14
- package/bridge/providers/native-session-state.py +96 -96
- package/bridge/providers/remote-job.py +130 -130
- package/bridge/remote-app-server-command.mjs +121 -121
- package/bridge/remote-directory-listing.mjs +148 -148
- package/bridge/rollout-summary.mjs +20 -2
- package/bridge/routes/dialogue.mjs +48 -3
- package/bridge/routes/models.mjs +312 -312
- package/bridge/server.mjs +3357 -3332
- package/bridge/session-attachments.mjs +150 -150
- package/bridge/session-catalog/agy-history.py +141 -2
- package/bridge/session-catalog/catalog.mjs +17 -0
- package/bridge/session-catalog/probe.py +1207 -331
- package/bridge/session-catalog/routes.mjs +6 -0
- package/bridge/start.mjs +205 -205
- package/bridge/state-persistence.mjs +9 -1
- package/bridge/tmux-session-control.mjs +29 -29
- package/bridge/vscode-opener.mjs +157 -157
- package/bridge/web-terminal.mjs +52 -52
- package/bridge/workbench/manager-context.mjs +51 -51
- package/bridge/workbench/service.mjs +301 -301
- package/dist/client/.vite/manifest.json +4 -4
- package/dist/client/assets/{index-Enu1x0PS.js → index-BMior55c.js} +2 -2
- package/dist/client/assets/{layout-segment-context-CmJzExWq.js → layout-segment-context-DjGudgXn.js} +1 -1
- package/dist/client/assets/page-CJqXlhDs.js +551 -0
- package/dist/client/assets/page-LYzRz6_S.css +1 -0
- package/dist/client/favicon.svg +6 -6
- package/dist/server/__vite_rsc_assets_manifest.js +8 -8
- package/dist/server/index.js +7 -7
- package/dist/server/ssr/__vite_rsc_assets_manifest.js +8 -8
- package/dist/server/ssr/assets/{page-CYYyCnKD.js → page-BB9Dri2u.js} +2416 -271
- package/dist/server/ssr/assets/page-LYzRz6_S.css +1 -0
- package/dist/server/ssr/index.js +1 -1
- package/dist/server/ssr/vinext-server.json +1 -1
- package/dist/server/vinext-server.json +1 -1
- package/dist/server/wrangler.json +1 -1
- package/lib/browser-snapshot-cache.js +53 -53
- package/lib/formula-preview.js +1 -1
- package/lib/incremental-snapshot.js +300 -300
- package/lib/markdown-math.js +151 -151
- package/lib/remote-links.js +95 -95
- package/lib/runtime-config.js +70 -70
- package/lib/session-filters.js +38 -38
- package/lib/ssh-config.js +26 -26
- package/lib/stable-order.js +39 -39
- package/lib/subagent-display-prefs.ts +130 -0
- package/package.json +1 -1
- package/scripts/agent-email-relay.mjs +104 -104
- package/scripts/email-notification-cli.mjs +28 -28
- package/scripts/enable-private-access.ps1 +109 -109
- package/scripts/fixtures/native-conversations.mjs +164 -6
- package/scripts/fixtures/workbench-conversations.mjs +475 -352
- package/scripts/preview-workbench.mjs +669 -669
- package/dist/client/assets/page-COBv6VWj.css +0 -1
- package/dist/client/assets/page-DVjU6VG_.js +0 -550
- 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
|
}
|