@pi-chamber/web 1.0.8-rc.1 → 1.0.8-rc.2
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/dist/assets/{AgentsSurface-BmHImX8m.js → AgentsSurface-CcWBqAWV.js} +1 -1
- package/dist/assets/{ChoicesArtifact-C71fGOm2.js → ChoicesArtifact-Kk-qvrSB.js} +1 -1
- package/dist/assets/{DiagramView-BZQxoiZu.js → DiagramView-DZUBo1uP.js} +1 -1
- package/dist/assets/{DiffView-C70pbgk-.js → DiffView-CiZy4jFA.js} +1 -1
- package/dist/assets/{FileSaveConflictDialog-Cu8c3gBV.js → FileSaveConflictDialog-DZ9NDwOO.js} +1 -1
- package/dist/assets/{FilesView-nFJYEdRw.js → FilesView-DqiZ9kw9.js} +1 -1
- package/dist/assets/{GitHubDetailScaffold-Jvv_tgbl.js → GitHubDetailScaffold-DM2DH7o8.js} +1 -1
- package/dist/assets/{GitHubLinkPicker-BXD2ODRi.js → GitHubLinkPicker-ChB8NZ1h.js} +1 -1
- package/dist/assets/{GitHubListPrimitives-BWI706F0.js → GitHubListPrimitives-CibDiJGq.js} +1 -1
- package/dist/assets/{GitView-MIDG5RYW.js → GitView--EaZQrBr.js} +5 -5
- package/dist/assets/{IssuesSurface-uNlozeu0.js → IssuesSurface-CySTpaD1.js} +3 -3
- package/dist/assets/{JsonTreeView-CSLsTriz.js → JsonTreeView-_yMHKKQZ.js} +1 -1
- package/dist/assets/{MarkdownRendererImpl-Cs-7BfWi.js → MarkdownRendererImpl-D_-2fQw0.js} +1 -1
- package/dist/assets/{PatchDiff-axbsnbTJ.js → PatchDiff-bHObDlhD.js} +1 -1
- package/dist/assets/{PierreDiffViewer-lwn6w_5w.js → PierreDiffViewer-UY7Cse-s.js} +1 -1
- package/dist/assets/{PullRequestsSurface-o_AXDjbO.js → PullRequestsSurface-iXy6chwc.js} +2 -2
- package/dist/assets/{SessionSwitcherContent-C4CMlcbv.js → SessionSwitcherContent-DJGCa_wN.js} +1 -1
- package/dist/assets/{SessionSwitcherDropdown-DCBVuCoo.js → SessionSwitcherDropdown-CDOX-5Y2.js} +2 -2
- package/dist/assets/{SessionUnreadDot-DyeIrsro.js → SessionUnreadDot-Dwc3MdvA.js} +1 -1
- package/dist/assets/SettingsView-DA-U9lsj.js +11 -0
- package/dist/assets/{SettingsWindow-Cb4st5y5.js → SettingsWindow-CUg49Mfh.js} +1 -1
- package/dist/assets/{SubagentsArtifact-BzeMEoFP.js → SubagentsArtifact-DUZX9tDe.js} +1 -1
- package/dist/assets/{ToolOutputDialog-BP2mOWdV.js → ToolOutputDialog-BuZZaRw4.js} +1 -1
- package/dist/assets/{ToolPartDiffPreview-D49XBR9b.js → ToolPartDiffPreview-CKncG6i2.js} +1 -1
- package/dist/assets/{VirtualizedFileDiff-CLgDONEQ.js → VirtualizedFileDiff-BGZiLPe_.js} +1 -1
- package/dist/assets/VisualFrame-DBZkHFnG.js +22 -0
- package/dist/assets/VisualsArtifact-Bq0vMO-i.js +1 -0
- package/dist/assets/{collapsible-BeyKtQGB.js → collapsible-Bro9KrfM.js} +1 -1
- package/dist/assets/{computeVirtualFileMetrics-DmO6H8u6.js → computeVirtualFileMetrics-DJQqK49u.js} +1 -1
- package/dist/assets/{contextFileOpenGuard-DOK251YW.js → contextFileOpenGuard-ByWK3Sru.js} +1 -1
- package/dist/assets/{fileTreeStatus-CnzR7kxA.js → fileTreeStatus-CyJ3TOYD.js} +1 -1
- package/dist/assets/{gitStatusPredicates-4kPg2t6j.js → gitStatusPredicates-BsXrwngT.js} +1 -1
- package/dist/assets/{githubLink-QvaLfNNT.js → githubLink-g2bmC0qi.js} +1 -1
- package/dist/assets/index-B_L9GVYw.css +1 -0
- package/dist/assets/{index-DMX9wT1Z.js → index-C9rUUQ_Y.js} +1 -1
- package/dist/assets/index-CKKWVoND.js +11 -0
- package/dist/assets/{main-BW9J3Y72.js → main-DT-4_fhe.js} +2 -2
- package/dist/assets/{main-BirYxac2.js → main-DsLMU9CT.js} +4 -4
- package/dist/assets/{markdownCore-CxmcPur8.js → markdownCore-QmM_aOnZ.js} +1 -1
- package/dist/assets/{miniChat-BRB11eEs.js → miniChat-BqGslD2z.js} +2 -2
- package/dist/assets/{mobile-DJZYYCuB.js → mobile-C5JOcDIC.js} +2 -2
- package/dist/assets/{nativeFileSave-B6QpoPRn.js → nativeFileSave-DSMsJqXM.js} +1 -1
- package/dist/assets/{pi-session-focus-preference-PWoh3LNA.js → pi-session-focus-preference-Cc_S4zi1.js} +1 -1
- package/dist/assets/{renderDiffWithHighlighter-C48T6YFJ.js → renderDiffWithHighlighter-BXhnIAHg.js} +1 -1
- package/dist/assets/{renderElectronMiniChatApp-N-BQdJl0.js → renderElectronMiniChatApp-CiSkecEE.js} +2 -2
- package/dist/assets/{renderFileWithHighlighter-8Kt2MyTN.js → renderFileWithHighlighter-CK29YZtC.js} +1 -1
- package/dist/assets/renderMobileApp-C4JuoxsZ.js +11 -0
- package/dist/assets/runtimeEndpointReset-Dwj-jCDS.js +66 -0
- package/dist/assets/{runtimeSurface-kIEoYsPP.js → runtimeSurface-0K7vuUpd.js} +1 -1
- package/dist/assets/{selection-BTEdLRVK.js → selection-CnYkEsu_.js} +1 -1
- package/dist/assets/useAppFontEffects-CV5UyTRw.js +226 -0
- package/dist/assets/{useGitHubRemoteSearch-DIBHNu8r.js → useGitHubRemoteSearch-CgP-zEBj.js} +1 -1
- package/dist/assets/{useSwitcherItems-DfLbfX37.js → useSwitcherItems-C8G5F0hs.js} +1 -1
- package/dist/assets/{web-components-C_DsTJis.js → web-components-BpncHjnV.js} +1 -1
- package/dist/index.html +4 -4
- package/dist/mini-chat.html +3 -3
- package/dist/mobile.html +3 -3
- package/package.json +1 -1
- package/plugins/featured.json +12 -0
- package/plugins/subagents/package.json +1 -1
- package/plugins/visuals/extension.js +7 -4
- package/plugins/visuals/package.json +1 -1
- package/server/index.js +86 -27
- package/server/lib/git/DOCUMENTATION.md +4 -0
- package/server/lib/git/service.js +70 -5
- package/server/lib/git/service.test.js +179 -0
- package/server/lib/github/DOCUMENTATION.md +2 -0
- package/server/lib/marketplace/DOCUMENTATION.md +95 -0
- package/server/lib/marketplace/featured.js +176 -0
- package/server/lib/marketplace/featured.test.js +207 -0
- package/server/lib/marketplace/npm-registry.js +255 -0
- package/server/lib/marketplace/npm-registry.test.js +227 -0
- package/server/lib/marketplace/routes.js +64 -0
- package/server/lib/marketplace/routes.test.js +119 -0
- package/server/lib/marketplace/service.js +225 -0
- package/server/lib/marketplace/service.test.js +174 -0
- package/server/lib/notifications/DOCUMENTATION.md +10 -4
- package/server/lib/notifications/pi-notification-watcher.js +92 -33
- package/server/lib/notifications/pi-notification-watcher.test.js +79 -0
- package/server/lib/open-url/DOCUMENTATION.md +1 -0
- package/server/lib/pi/merged-event-stream.js +316 -0
- package/server/lib/pi/merged-event-stream.test.js +95 -0
- package/server/lib/pi/routes-daemon-pool.test.js +348 -0
- package/server/lib/pi/routes-merged-events.test.js +327 -0
- package/server/lib/pi/routes-sandbox.test.js +1684 -0
- package/server/lib/pi/routes.js +491 -90
- package/server/lib/pi/routes.test.js +87 -2
- package/server/lib/pi/sandbox/DOCUMENTATION.md +68 -0
- package/server/lib/pi/sandbox/launcher.js +122 -0
- package/server/lib/pi/sandbox/launcher.test.js +91 -0
- package/server/lib/pi/sandbox/nono-launcher.js +455 -0
- package/server/lib/pi/sandbox/nono-launcher.test.js +371 -0
- package/server/lib/pi/sandbox/nono-profile-macos.json +20 -0
- package/server/lib/pi/sandbox/nono-profile-wsl2.json +21 -0
- package/server/lib/pi/sandbox/nono-profile.json +22 -0
- package/server/lib/pi/sandbox/project-sandbox-manager.js +1461 -0
- package/server/lib/pi/sandbox/project-sandbox-setting.js +166 -0
- package/server/lib/pi/sandbox/project-sandbox.js +207 -0
- package/server/lib/pi/sandbox/sandbox-routes.js +110 -0
- package/server/lib/pi/sandbox/sandboxed-folders.js +100 -0
- package/server/lib/pi/sandbox/sandboxed-folders.test.js +65 -0
- package/server/lib/pi/sandbox/trust-ask.js +135 -0
- package/server/lib/pi/sandbox/trust-ask.test.js +54 -0
- package/server/lib/pi/sandbox/worktree-ids.js +96 -0
- package/server/lib/pi/sandbox/worktree-ids.test.js +73 -0
- package/server/lib/pi/session-daemon/DOCUMENTATION.md +26 -6
- package/server/lib/pi/session-daemon/daemon-pool.js +157 -0
- package/server/lib/pi/session-daemon/daemon-process.js +10 -1
- package/server/lib/pi/session-daemon/extension-config.js +206 -23
- package/server/lib/pi/session-daemon/extension-config.test.js +180 -0
- package/server/lib/pi/session-daemon/session-daemon-read-persisted.test.js +167 -0
- package/server/lib/pi/session-daemon/session-daemon.js +368 -56
- package/server/lib/pi/session-daemon/session-daemon.test.js +227 -0
- package/server/lib/pi/session-daemon/session-idle-residency.test.js +98 -1
- package/server/lib/pi/session-daemon/session-jsonl.js +49 -10
- package/server/lib/pi/session-daemon/session-jsonl.test.js +11 -0
- package/server/lib/pi/session-daemon/session-lease.js +93 -15
- package/server/lib/pi/session-daemon/session-lease.test.js +107 -1
- package/server/lib/pi/session-daemon/supervisor-launcher-stop.test.js +295 -0
- package/server/lib/pi/session-daemon/supervisor-sandbox-nono.e2e.test.js +301 -0
- package/server/lib/pi/session-daemon/supervisor-sandbox.test.js +444 -0
- package/server/lib/pi/session-daemon/supervisor.js +386 -35
- package/server/lib/pi/session-daemon/supervisor.test.js +28 -0
- package/server/lib/pi/ui-settings-store.js +88 -2
- package/server/lib/pi/ui-settings-store.test.js +118 -0
- package/server/lib/plugins/DOCUMENTATION.md +17 -5
- package/server/lib/plugins/daemon-sync.js +16 -6
- package/server/lib/plugins/daemon-sync.test.js +12 -0
- package/server/lib/plugins/impact.test.js +2 -1
- package/server/lib/plugins/manifest.js +25 -0
- package/server/lib/plugins/manifest.test.js +9 -0
- package/server/lib/plugins/routes.test.js +28 -0
- package/server/lib/plugins/service.js +24 -2
- package/server/lib/server/cross-process-lock.js +28 -3
- package/dist/assets/SettingsView-BHBpt2Um.js +0 -7
- package/dist/assets/VisualFrame-CJrAvWSo.js +0 -22
- package/dist/assets/VisualsArtifact-DqLGgU-Q.js +0 -1
- package/dist/assets/index-B2v0_mez.js +0 -11
- package/dist/assets/index-D3F4JrPN.css +0 -1
- package/dist/assets/renderMobileApp-B7cT7m5b.js +0 -11
- package/dist/assets/runtimeEndpointReset-BC6yGnxN.js +0 -62
- package/dist/assets/useAppFontEffects-EcTZ_I_I.js +0 -225
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One public `/api/pi/events` subscription over every live session daemon.
|
|
3
|
+
*
|
|
4
|
+
* The main daemon is subscribed exactly as before: same options, same
|
|
5
|
+
* frames, and its failure still ends the connection so the client
|
|
6
|
+
* reconnects. Its resume position keeps travelling as `fromSequence` +
|
|
7
|
+
* `streamEpoch`.
|
|
8
|
+
*
|
|
9
|
+
* Project daemons (sandboxed projects) are subscribed alongside it. Their
|
|
10
|
+
* frames gain `stream` (an opaque id per daemon) and `cursor` (an opaque
|
|
11
|
+
* value carrying every project daemon's epoch and sequence). A client
|
|
12
|
+
* resumes by sending the newest `cursor` it accepted next to its main
|
|
13
|
+
* position. Each project daemon is resumed from its own entry; a daemon
|
|
14
|
+
* whose lifetime changed answers that cursor with a resync snapshot, the
|
|
15
|
+
* same as the main daemon after a restart.
|
|
16
|
+
*
|
|
17
|
+
* Only the affected daemon's sessions resync:
|
|
18
|
+
* - a daemon appearing is subscribed without a cursor (snapshot baseline);
|
|
19
|
+
* - a daemon restarting (its runtime replaced in the pool) or stopping is
|
|
20
|
+
* unsubscribed, and every session this connection saw from it is
|
|
21
|
+
* announced with `session.moved` so clients refetch it from its owner;
|
|
22
|
+
* - a project daemon's subscription failing is the same as a stop, then a
|
|
23
|
+
* bounded retry while it stays registered; it never ends the connection.
|
|
24
|
+
*
|
|
25
|
+
* A project daemon runs inside a sandbox and is not trusted to say which
|
|
26
|
+
* sessions are its own. With `ownsSession`, its frames are delivered only
|
|
27
|
+
* for sessions the server finds in that daemon's folders; the answer is
|
|
28
|
+
* asked once per session and connection, and the daemon's later frames
|
|
29
|
+
* wait for it, so order and cursors are unchanged. Frames for any other
|
|
30
|
+
* session are dropped.
|
|
31
|
+
*
|
|
32
|
+
* A session whose frames arrive from a different daemon than before, or
|
|
33
|
+
* one the pool announces (`announceMove`), is also announced with
|
|
34
|
+
* `session.moved`, before any frame from its new owner.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
import { MAIN_DAEMON_KEY, publicDaemonStreamId } from './session-daemon/daemon-pool.js';
|
|
38
|
+
|
|
39
|
+
const CURSOR_VERSION = 1;
|
|
40
|
+
const MAX_CURSOR_LENGTH = 4096;
|
|
41
|
+
const MAX_CURSOR_STREAMS = 64;
|
|
42
|
+
const MAX_TRACKED_SESSIONS = 4096;
|
|
43
|
+
const RETRY_BASE_MS = 500;
|
|
44
|
+
const RETRY_CAP_MS = 30_000;
|
|
45
|
+
const MAX_WAITING_FRAMES = 4096;
|
|
46
|
+
|
|
47
|
+
const isEpoch = (value) => typeof value === 'string' && value.length > 0 && value.length <= 128;
|
|
48
|
+
const isStreamId = (value) => typeof value === 'string' && /^[A-Za-z0-9_-]{1,64}$/.test(value);
|
|
49
|
+
|
|
50
|
+
/** Decode a client cursor into `Map<streamId, { epoch, sequence }>`, or
|
|
51
|
+
* null when it is malformed. */
|
|
52
|
+
export const decodePiEventCursor = (value) => {
|
|
53
|
+
if (typeof value !== 'string' || value.length === 0 || value.length > MAX_CURSOR_LENGTH || !/^[A-Za-z0-9_-]+$/.test(value)) return null;
|
|
54
|
+
let parsed;
|
|
55
|
+
try {
|
|
56
|
+
parsed = JSON.parse(Buffer.from(value, 'base64url').toString('utf8'));
|
|
57
|
+
} catch {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
if (!parsed || typeof parsed !== 'object' || parsed.v !== CURSOR_VERSION || !parsed.d || typeof parsed.d !== 'object' || Array.isArray(parsed.d)) return null;
|
|
61
|
+
const entries = Object.entries(parsed.d);
|
|
62
|
+
if (entries.length > MAX_CURSOR_STREAMS) return null;
|
|
63
|
+
const positions = new Map();
|
|
64
|
+
for (const [id, position] of entries) {
|
|
65
|
+
if (!isStreamId(id) || !Array.isArray(position) || position.length !== 2) return null;
|
|
66
|
+
const [epoch, sequence] = position;
|
|
67
|
+
if (!isEpoch(epoch) || !Number.isSafeInteger(sequence) || sequence < 0) return null;
|
|
68
|
+
positions.set(id, { epoch, sequence });
|
|
69
|
+
}
|
|
70
|
+
return positions;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const encodeCursor = (positions) => {
|
|
74
|
+
const d = {};
|
|
75
|
+
for (const [id, { epoch, sequence }] of positions) d[id] = [epoch, sequence];
|
|
76
|
+
return Buffer.from(JSON.stringify({ v: CURSOR_VERSION, d }), 'utf8').toString('base64url');
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const movedFrame = (sessionId, directory) => ({
|
|
80
|
+
protocolVersion: 1, kind: 'event', name: 'session.moved', sequence: 0, sessionId, directory, payload: {},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Subscribe to every live daemon in `pool`. Same contract as a daemon
|
|
85
|
+
* runtime's `subscribe`: resolves to a close function; `onEvent` receives
|
|
86
|
+
* public (already projected) events; `onError` ends the connection.
|
|
87
|
+
*/
|
|
88
|
+
export const subscribeMergedPiEvents = async ({
|
|
89
|
+
pool,
|
|
90
|
+
projectFrame,
|
|
91
|
+
sessionId,
|
|
92
|
+
directory,
|
|
93
|
+
fromSequence,
|
|
94
|
+
streamEpoch,
|
|
95
|
+
cursor,
|
|
96
|
+
onEvent,
|
|
97
|
+
onError,
|
|
98
|
+
signal,
|
|
99
|
+
retryBaseMs = RETRY_BASE_MS,
|
|
100
|
+
ownsSession,
|
|
101
|
+
}) => {
|
|
102
|
+
const main = pool.main();
|
|
103
|
+
let closed = false;
|
|
104
|
+
// Project daemon positions by public stream id, seeded from the client's
|
|
105
|
+
// cursor so a daemon registering after this connection opened still
|
|
106
|
+
// resumes from where the client left it.
|
|
107
|
+
const positions = new Map(cursor ?? []);
|
|
108
|
+
// key -> { runtime, id, close?, failures, retryTimer? }
|
|
109
|
+
const attached = new Map();
|
|
110
|
+
// sessionId -> { key, directory }: the daemon whose frame this
|
|
111
|
+
// connection last delivered for the session.
|
|
112
|
+
const sessions = new Map();
|
|
113
|
+
|
|
114
|
+
const deliver = (event) => {
|
|
115
|
+
if (!closed) onEvent(event);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const announce = (sessionIdToMove, sessionDirectory) => {
|
|
119
|
+
sessions.delete(sessionIdToMove);
|
|
120
|
+
deliver(movedFrame(sessionIdToMove, sessionDirectory));
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const track = (key, event) => {
|
|
124
|
+
const previous = sessions.get(event.sessionId);
|
|
125
|
+
if (previous && previous.key !== key) announce(event.sessionId, event.directory);
|
|
126
|
+
sessions.delete(event.sessionId);
|
|
127
|
+
sessions.set(event.sessionId, { key, directory: event.directory });
|
|
128
|
+
if (sessions.size > MAX_TRACKED_SESSIONS) sessions.delete(sessions.keys().next().value);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const announceSessionsOf = (key) => {
|
|
132
|
+
for (const [id, entry] of [...sessions]) if (entry.key === key) announce(id, entry.directory);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const onMainFrame = (frame) => {
|
|
136
|
+
if (closed) return;
|
|
137
|
+
const event = projectFrame(frame);
|
|
138
|
+
if (!event) return;
|
|
139
|
+
track(MAIN_DAEMON_KEY, event);
|
|
140
|
+
deliver(event);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// The position advances for a dropped frame too: it is the daemon's own
|
|
144
|
+
// numbering, and a resume must not ask for the frame again.
|
|
145
|
+
const passProjectEvent = (key, entry, event, owned) => {
|
|
146
|
+
if (isEpoch(event.streamEpoch)) {
|
|
147
|
+
const known = positions.get(entry.id);
|
|
148
|
+
if (!known || known.epoch !== event.streamEpoch || event.sequence > known.sequence) {
|
|
149
|
+
positions.set(entry.id, { epoch: event.streamEpoch, sequence: event.sequence });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (!owned) return;
|
|
153
|
+
track(key, event);
|
|
154
|
+
deliver({ ...event, stream: entry.id, cursor: encodeCursor(positions) });
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const passWaiting = async (key, entry) => {
|
|
158
|
+
entry.checking = true;
|
|
159
|
+
try {
|
|
160
|
+
while (entry.waiting.length > 0) {
|
|
161
|
+
if (closed || attached.get(key) !== entry) return;
|
|
162
|
+
const event = entry.waiting[0];
|
|
163
|
+
let owned = entry.owned.get(event.sessionId);
|
|
164
|
+
if (owned === undefined) {
|
|
165
|
+
try {
|
|
166
|
+
owned = Boolean(await ownsSession(key, event.sessionId, event.directory));
|
|
167
|
+
} catch {
|
|
168
|
+
owned = false;
|
|
169
|
+
}
|
|
170
|
+
if (closed || attached.get(key) !== entry) return;
|
|
171
|
+
entry.owned.set(event.sessionId, owned);
|
|
172
|
+
if (entry.owned.size > MAX_TRACKED_SESSIONS) entry.owned.delete(entry.owned.keys().next().value);
|
|
173
|
+
}
|
|
174
|
+
entry.waiting.shift();
|
|
175
|
+
passProjectEvent(key, entry, event, owned);
|
|
176
|
+
}
|
|
177
|
+
} finally {
|
|
178
|
+
entry.checking = false;
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const onProjectFrame = (key, entry, frame) => {
|
|
183
|
+
if (closed || attached.get(key) !== entry) return;
|
|
184
|
+
const event = projectFrame(frame);
|
|
185
|
+
if (!event) return;
|
|
186
|
+
if (!ownsSession || typeof event.sessionId !== 'string') {
|
|
187
|
+
passProjectEvent(key, entry, event, true);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const owned = entry.waiting.length === 0 ? entry.owned.get(event.sessionId) : undefined;
|
|
191
|
+
if (owned !== undefined) {
|
|
192
|
+
passProjectEvent(key, entry, event, owned);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
// A daemon that outruns the check is resubscribed rather than buffered
|
|
196
|
+
// without bound.
|
|
197
|
+
if (entry.waiting.length >= MAX_WAITING_FRAMES) {
|
|
198
|
+
fail(key, entry);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
entry.waiting.push(event);
|
|
202
|
+
if (!entry.checking) void passWaiting(key, entry);
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const detach = (key) => {
|
|
206
|
+
const entry = attached.get(key);
|
|
207
|
+
if (!entry) return;
|
|
208
|
+
attached.delete(key);
|
|
209
|
+
if (entry.retryTimer) clearTimeout(entry.retryTimer);
|
|
210
|
+
try {
|
|
211
|
+
entry.close?.();
|
|
212
|
+
} catch {
|
|
213
|
+
// A subscription that throws on close must not break the others.
|
|
214
|
+
}
|
|
215
|
+
announceSessionsOf(key);
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const scheduleRetry = (key, runtime, failures) => {
|
|
219
|
+
if (closed) return;
|
|
220
|
+
const entry = { runtime, id: publicDaemonStreamId(key), failures, close: undefined, retryTimer: undefined, owned: new Map(), waiting: [], checking: false };
|
|
221
|
+
entry.retryTimer = setTimeout(() => {
|
|
222
|
+
if (attached.get(key) !== entry) return;
|
|
223
|
+
attached.delete(key);
|
|
224
|
+
void attach(key, runtime, failures);
|
|
225
|
+
}, Math.min(RETRY_CAP_MS, retryBaseMs * 2 ** Math.min(failures, 6)));
|
|
226
|
+
entry.retryTimer.unref?.();
|
|
227
|
+
attached.set(key, entry);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const fail = (key, entry) => {
|
|
231
|
+
if (closed || attached.get(key) !== entry) return;
|
|
232
|
+
detach(key);
|
|
233
|
+
scheduleRetry(key, entry.runtime, entry.failures + 1);
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const attach = async (key, runtime, failures = 0) => {
|
|
237
|
+
if (closed || typeof runtime?.subscribe !== 'function') return;
|
|
238
|
+
const entry = { runtime, id: publicDaemonStreamId(key), failures, close: undefined, retryTimer: undefined, owned: new Map(), waiting: [], checking: false };
|
|
239
|
+
attached.set(key, entry);
|
|
240
|
+
const position = positions.get(entry.id);
|
|
241
|
+
try {
|
|
242
|
+
const close = await runtime.subscribe({
|
|
243
|
+
sessionId,
|
|
244
|
+
directory,
|
|
245
|
+
...(position ? { fromSequence: position.sequence, streamEpoch: position.epoch } : {}),
|
|
246
|
+
onEvent: (frame) => onProjectFrame(key, entry, frame),
|
|
247
|
+
onError: () => fail(key, entry),
|
|
248
|
+
signal,
|
|
249
|
+
});
|
|
250
|
+
if (closed || attached.get(key) !== entry) {
|
|
251
|
+
close?.();
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
entry.close = close;
|
|
255
|
+
entry.failures = 0;
|
|
256
|
+
} catch {
|
|
257
|
+
if (closed || attached.get(key) !== entry) return;
|
|
258
|
+
attached.delete(key);
|
|
259
|
+
scheduleRetry(key, runtime, failures + 1);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const reconcile = () => {
|
|
264
|
+
if (closed) return;
|
|
265
|
+
const live = new Map(pool.live().filter((item) => item.key !== MAIN_DAEMON_KEY).map((item) => [item.key, item.runtime]));
|
|
266
|
+
for (const [key, entry] of [...attached]) {
|
|
267
|
+
if (live.get(key) === entry.runtime) continue;
|
|
268
|
+
detach(key);
|
|
269
|
+
// A stopped daemon's position is dead; a restarted one keeps it so the
|
|
270
|
+
// new process answers with a resync snapshot.
|
|
271
|
+
if (!live.has(key)) positions.delete(entry.id);
|
|
272
|
+
}
|
|
273
|
+
for (const [key, runtime] of live) {
|
|
274
|
+
if (!attached.has(key)) void attach(key, runtime);
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
let closeMain;
|
|
279
|
+
let stopPoolListener = () => {};
|
|
280
|
+
let stopMoveListener = () => {};
|
|
281
|
+
|
|
282
|
+
const close = () => {
|
|
283
|
+
if (closed) return;
|
|
284
|
+
closed = true;
|
|
285
|
+
stopPoolListener();
|
|
286
|
+
stopMoveListener();
|
|
287
|
+
for (const entry of attached.values()) {
|
|
288
|
+
if (entry.retryTimer) clearTimeout(entry.retryTimer);
|
|
289
|
+
try {
|
|
290
|
+
entry.close?.();
|
|
291
|
+
} catch {
|
|
292
|
+
// Teardown continues for every other daemon.
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
attached.clear();
|
|
296
|
+
try {
|
|
297
|
+
closeMain?.();
|
|
298
|
+
} catch {
|
|
299
|
+
// Teardown continues.
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
try {
|
|
304
|
+
closeMain = await main.subscribe({ sessionId, directory, fromSequence, streamEpoch, onEvent: onMainFrame, onError, signal });
|
|
305
|
+
} catch (error) {
|
|
306
|
+
close();
|
|
307
|
+
throw error;
|
|
308
|
+
}
|
|
309
|
+
if (closed) return close;
|
|
310
|
+
stopPoolListener = pool.onChange(reconcile);
|
|
311
|
+
stopMoveListener = pool.onMove((movedSessionId, movedDirectory) => {
|
|
312
|
+
if (!closed) announce(movedSessionId, movedDirectory);
|
|
313
|
+
});
|
|
314
|
+
reconcile();
|
|
315
|
+
return close;
|
|
316
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { subscribeMergedPiEvents } from './merged-event-stream.js';
|
|
4
|
+
|
|
5
|
+
const frame = (sessionId, sequence, directory = '/a') => ({
|
|
6
|
+
protocolVersion: 1, kind: 'event', name: 'message.delta', sequence, sessionId, directory, streamEpoch: 'epoch-box', payload: {},
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
const fakeRuntime = () => {
|
|
10
|
+
const runtime = {
|
|
11
|
+
emit: null,
|
|
12
|
+
subscribe: async ({ onEvent }) => {
|
|
13
|
+
runtime.emit = onEvent;
|
|
14
|
+
return () => {};
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
return runtime;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const open = async ({ ownsSession } = {}) => {
|
|
21
|
+
const main = fakeRuntime();
|
|
22
|
+
const box = fakeRuntime();
|
|
23
|
+
const events = [];
|
|
24
|
+
const pool = {
|
|
25
|
+
main: () => main,
|
|
26
|
+
live: () => [{ key: 'main', runtime: main }, { key: 'sandbox:proj-a', runtime: box }],
|
|
27
|
+
onChange: () => () => {},
|
|
28
|
+
onMove: () => () => {},
|
|
29
|
+
};
|
|
30
|
+
await subscribeMergedPiEvents({
|
|
31
|
+
pool,
|
|
32
|
+
projectFrame: (value) => value,
|
|
33
|
+
onEvent: (event) => events.push(event),
|
|
34
|
+
onError: () => {},
|
|
35
|
+
...(ownsSession ? { ownsSession } : {}),
|
|
36
|
+
});
|
|
37
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
38
|
+
return { box, events };
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const settle = () => new Promise((resolve) => setTimeout(resolve, 10));
|
|
42
|
+
|
|
43
|
+
describe('a project daemon\'s frames', () => {
|
|
44
|
+
it('are delivered only for sessions the server finds in that daemon\'s folders', async () => {
|
|
45
|
+
const asked = [];
|
|
46
|
+
const { box, events } = await open({
|
|
47
|
+
ownsSession: async (key, sessionId, directory) => {
|
|
48
|
+
asked.push([key, sessionId, directory]);
|
|
49
|
+
await new Promise((resolve) => setTimeout(resolve, 2));
|
|
50
|
+
return sessionId === 's-own';
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// The sandbox daemon reports on another project's session between two
|
|
55
|
+
// frames of its own.
|
|
56
|
+
box.emit(frame('s-own', 1));
|
|
57
|
+
box.emit(frame('s-foreign', 2));
|
|
58
|
+
box.emit(frame('s-own', 3));
|
|
59
|
+
await settle();
|
|
60
|
+
box.emit(frame('s-foreign', 4));
|
|
61
|
+
box.emit(frame('s-own', 5));
|
|
62
|
+
|
|
63
|
+
expect(events.map((event) => [event.sessionId, event.sequence])).toEqual([['s-own', 1], ['s-own', 3], ['s-own', 5]]);
|
|
64
|
+
// Asked once per session, with the daemon's key.
|
|
65
|
+
expect(asked).toEqual([['sandbox:proj-a', 's-own', '/a'], ['sandbox:proj-a', 's-foreign', '/a']]);
|
|
66
|
+
// The cursor still moves past the dropped frames.
|
|
67
|
+
const cursor = JSON.parse(Buffer.from(events.at(-1).cursor, 'base64url').toString('utf8'));
|
|
68
|
+
expect(Object.values(cursor.d)).toEqual([['epoch-box', 5]]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('keep their order while a session is being checked', async () => {
|
|
72
|
+
let answer;
|
|
73
|
+
const { box, events } = await open({ ownsSession: () => new Promise((resolve) => { answer = resolve; }) });
|
|
74
|
+
box.emit(frame('s-1', 1));
|
|
75
|
+
box.emit(frame('s-1', 2));
|
|
76
|
+
expect(events).toEqual([]);
|
|
77
|
+
answer(true);
|
|
78
|
+
await settle();
|
|
79
|
+
box.emit(frame('s-1', 3));
|
|
80
|
+
expect(events.map((event) => event.sequence)).toEqual([1, 2, 3]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('drops a session whose check fails', async () => {
|
|
84
|
+
const { box, events } = await open({ ownsSession: async () => { throw new Error('unreadable'); } });
|
|
85
|
+
box.emit(frame('s-1', 1));
|
|
86
|
+
await settle();
|
|
87
|
+
expect(events).toEqual([]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('pass straight through when nothing checks them', async () => {
|
|
91
|
+
const { box, events } = await open();
|
|
92
|
+
box.emit(frame('s-1', 1));
|
|
93
|
+
expect(events.map((event) => event.sequence)).toEqual([1]);
|
|
94
|
+
});
|
|
95
|
+
});
|