viewable-one 0.12.3 → 0.12.5
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 +86 -0
- package/glytch.mjs +22 -9
- package/lib/codexApps.mjs +16 -4
- package/lib/goodbye.mjs +120 -0
- package/lib/multiwatch.mjs +79 -8
- package/lib/registry.mjs +28 -0
- package/lib/restartPass.mjs +18 -5
- package/lib/runner.mjs +1 -1
- package/lib/runnerAdmission.mjs +27 -0
- package/lib/runnerApi.mjs +8 -1
- package/lib/runnerBeat.mjs +23 -4
- package/lib/runnerGates.mjs +7 -6
- package/lib/runnerOps.mjs +10 -1
- package/lib/runnerPace.mjs +134 -5
- package/lib/runnerRegister.mjs +18 -4
- package/lib/selfUninstall.mjs +239 -0
- package/lib/signIn.mjs +43 -18
- package/lib/update.mjs +261 -11
- package/lib/viewableFolder.mjs +6 -3
- package/package.json +1 -1
package/lib/runnerApi.mjs
CHANGED
|
@@ -122,7 +122,7 @@ async function call(op, body = {}, options = {}) {
|
|
|
122
122
|
// below. A 401 and every refusal leave it unset.
|
|
123
123
|
return { ok: false, transient: true, error: `Could not reach ${url}: ${error instanceof Error ? error.message : String(error)}${cause ? ` (${cause})` : ""}` };
|
|
124
124
|
}
|
|
125
|
-
if (result.authError) return { ok: false, error: result.authError };
|
|
125
|
+
if (result.authError) return { ok: false, unauthorized: true, error: result.authError };
|
|
126
126
|
|
|
127
127
|
const { response, text, payload } = result;
|
|
128
128
|
if (!response.ok) {
|
|
@@ -144,6 +144,9 @@ async function call(op, body = {}, options = {}) {
|
|
|
144
144
|
return {
|
|
145
145
|
ok: false,
|
|
146
146
|
...(transient ? { transient: true } : {}),
|
|
147
|
+
// A refused key. Only a cue to ask the board whether Remove took it
|
|
148
|
+
// away (lib/selfUninstall.mjs); never proof on its own (#4978).
|
|
149
|
+
...(response.status === 401 ? { unauthorized: true } : {}),
|
|
147
150
|
...(wait !== undefined ? { retryAfterMs: wait } : {}),
|
|
148
151
|
error:
|
|
149
152
|
response.status === 401
|
|
@@ -317,5 +320,9 @@ export function computerAppsResult(body, options = {}) { return call("computerAp
|
|
|
317
320
|
export function folderReadTake(body, options = {}) { return call("folderReadTake", body, options); }
|
|
318
321
|
export function folderReadResult(body, options = {}) { return call("folderReadResult", body, options); }
|
|
319
322
|
|
|
323
|
+
/** Take the board's Update request once no run is going (#4949). Taking
|
|
324
|
+
* deletes it, so Cancel update and the take cannot both win. */
|
|
325
|
+
export function updateTake(body, options = {}) { return call("updateTake", body, options); }
|
|
326
|
+
|
|
320
327
|
/** Recover a locally proven dead owner without waiting for lease expiry. */
|
|
321
328
|
export function recover(body, options = {}) { return call("recover", body, options); }
|
package/lib/runnerBeat.mjs
CHANGED
|
@@ -18,6 +18,7 @@ import { createMemberPreviewTunnel } from "./memberPreview.mjs";
|
|
|
18
18
|
import { beatPace } from "./runnerPace.mjs";
|
|
19
19
|
import { resumeLateMessages } from "./lateResume.mjs";
|
|
20
20
|
import { serviceForgetOps, serviceRowOps } from "./runnerOps.mjs";
|
|
21
|
+
import { serviceUninstallOps, uninstallIfRemoved } from "./selfUninstall.mjs";
|
|
21
22
|
|
|
22
23
|
/** How soon the beat looks again for late messages: while one waits, and
|
|
23
24
|
* while none does (a pushed reply brings it forward either way). */
|
|
@@ -122,6 +123,14 @@ export function makeBeatLoop(w) {
|
|
|
122
123
|
settings.registrar.invalidate?.();
|
|
123
124
|
};
|
|
124
125
|
|
|
126
|
+
// A refused key is only a cue: the board decides whether Remove took it
|
|
127
|
+
// away while this computer was off (#4978, lib/selfUninstall.mjs).
|
|
128
|
+
const askIfRemoved = async (answer) => {
|
|
129
|
+
if (answer.unauthorized !== true) return;
|
|
130
|
+
try { await (settings.uninstallIfRemoved ?? uninstallIfRemoved)(settings, log); }
|
|
131
|
+
catch (error) { log(`uninstall check failed: ${error instanceof Error ? error.message : String(error)}`); }
|
|
132
|
+
};
|
|
133
|
+
|
|
125
134
|
// The Retry-After of the last beat that failed, for the beat's own pace.
|
|
126
135
|
let beatRetryAfterMs;
|
|
127
136
|
const runBeat = async () => {
|
|
@@ -134,6 +143,7 @@ export function makeBeatLoop(w) {
|
|
|
134
143
|
const reg = await registerAndFindProject(root, settings);
|
|
135
144
|
if (!reg.ok) {
|
|
136
145
|
log(`could not register with the board: ${reg.error}`);
|
|
146
|
+
await askIfRemoved(reg);
|
|
137
147
|
beatRetryAfterMs = reg.retryAfterMs;
|
|
138
148
|
return reg.transient ? "failed" : "steady";
|
|
139
149
|
}
|
|
@@ -148,10 +158,16 @@ export function makeBeatLoop(w) {
|
|
|
148
158
|
const runnerApi = settings.runnerApi ?? runnerApiModule;
|
|
149
159
|
const seen = w.hintSeq;
|
|
150
160
|
await refreshProjectPaths(root, settings);
|
|
151
|
-
|
|
161
|
+
// The hub's goodbye (ruling #4979) sends through this same writer, with
|
|
162
|
+
// `goodbye` and a short `timeoutMs` (lib/goodbye.mjs).
|
|
163
|
+
const beat = await heartbeats.beat(w.project.id, (projectIds, { goodbye, timeoutMs } = {}) => runnerApi.heartbeat(
|
|
164
|
+
{ projectIds, presence: presenceBody(settings, { goodbye }) },
|
|
165
|
+
{ env: settings.env, fetchImpl: settings.fetchImpl, ...(timeoutMs ? { timeoutMs } : {}) },
|
|
166
|
+
));
|
|
152
167
|
if (!beat.ok) {
|
|
153
168
|
clearRefusedProject(beat);
|
|
154
169
|
log(`heartbeat failed: ${beat.error}`);
|
|
170
|
+
await askIfRemoved(beat);
|
|
155
171
|
beatRetryAfterMs = beat.retryAfterMs;
|
|
156
172
|
return beat.transient ? "failed" : "steady";
|
|
157
173
|
}
|
|
@@ -172,6 +188,9 @@ export function makeBeatLoop(w) {
|
|
|
172
188
|
// Stops are run ids: another folder's in this set is never acted on.
|
|
173
189
|
for (const runId of beat.stops ?? []) stopped.add(runId);
|
|
174
190
|
let opsFailed = false;
|
|
191
|
+
// Remove on this online computer (#4951): it uninstalls itself, which
|
|
192
|
+
// stops this daemon in a moment, so nothing else on this beat starts.
|
|
193
|
+
if (serviceUninstallOps(ops, settings, log)) return "steady";
|
|
175
194
|
if (ops.length > 0) {
|
|
176
195
|
// FIRST, and it returns early: there is no point merging a worktree or
|
|
177
196
|
// standing a preview up for a folder that is leaving this machine.
|
|
@@ -191,7 +210,7 @@ export function makeBeatLoop(w) {
|
|
|
191
210
|
}
|
|
192
211
|
// Retry retained Look answers even though claimed requests are absent
|
|
193
212
|
// from later heartbeat operations. Forget above always takes priority.
|
|
194
|
-
try { opsFailed = !(await serviceRowOps(root, w.project, ops, settings, log)).ok || opsFailed; }
|
|
213
|
+
try { opsFailed = !(await serviceRowOps(root, w.project, ops, settings, log, { updatesUnread: beat.updatesUnread === true })).ok || opsFailed; }
|
|
195
214
|
catch (error) {
|
|
196
215
|
opsFailed = true;
|
|
197
216
|
log(`ops sweep failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
@@ -351,7 +370,7 @@ async function refreshRegistration(root, settings, runnerApi) {
|
|
|
351
370
|
const refreshSettings = {
|
|
352
371
|
...settings,
|
|
353
372
|
registrar: {
|
|
354
|
-
register: (key, send) => settings.registrar.register(key, async () => {
|
|
373
|
+
register: (key, send, writer) => settings.registrar.register(key, async () => {
|
|
355
374
|
// Row ops may have awaited a slow merge after the beat snapshot.
|
|
356
375
|
// Bypass the heartbeat cache, only when register is actually due.
|
|
357
376
|
// The remaining read/write gap is closed server-side by
|
|
@@ -375,7 +394,7 @@ async function refreshRegistration(root, settings, runnerApi) {
|
|
|
375
394
|
return { ok: false, error: "Registration deferred: a folder removal is pending." };
|
|
376
395
|
}
|
|
377
396
|
return send();
|
|
378
|
-
}),
|
|
397
|
+
}, writer),
|
|
379
398
|
},
|
|
380
399
|
};
|
|
381
400
|
const reg = await registerAndFindProject(root, refreshSettings);
|
package/lib/runnerGates.mjs
CHANGED
|
@@ -13,7 +13,7 @@ import { AGENTS, agentFor, isCodingCli } from "./agents.mjs";
|
|
|
13
13
|
import { resolveAgentBinary } from "./detect.mjs";
|
|
14
14
|
import { imageAttachments } from "./imageAttachments.mjs";
|
|
15
15
|
import { checkRequest, isChatTurn, isSignIn, titleFor } from "./spawn.mjs";
|
|
16
|
-
import {
|
|
16
|
+
import { agentLogin } from "./signIn.mjs";
|
|
17
17
|
import { fromIdOf } from "./boardside.mjs";
|
|
18
18
|
import { chatBoardOf, makeBoardCwd } from "./viewableFolder.mjs";
|
|
19
19
|
import { defaultBranchOf, git, makeChatWorktree, resolveChatCwd, worktreeBranch, worktreePath, worktreeSummary } from "./worktree.mjs";
|
|
@@ -124,13 +124,14 @@ export async function admitRequest({ project, root, id, request, isChat, runnerA
|
|
|
124
124
|
return { patch: { status: "error", result: `ERROR\n\nRefused: ${problems.join("\n")}\n`, finished_at: new Date().toISOString() } };
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
// SIGN IN TO
|
|
128
|
-
// page in this computer's browser.
|
|
129
|
-
// app's sign-in box goes as soon
|
|
130
|
-
// on the probe's own
|
|
127
|
+
// SIGN IN TO AN AGENT (#482, #5216, lib/signIn.mjs): `codex login` or
|
|
128
|
+
// `claude auth login` opens the sign-in page in this computer's browser.
|
|
129
|
+
// Then the agents are probed again, so the app's sign-in box goes as soon
|
|
130
|
+
// as the agent says it is signed in rather than on the probe's own
|
|
131
|
+
// five-minute clock.
|
|
131
132
|
if (isSignIn(request)) {
|
|
132
133
|
log(`[${id}] signing in to ${request.agent}`);
|
|
133
|
-
const signedIn = await (settings.
|
|
134
|
+
const signedIn = await (settings.agentLogin ?? agentLogin)(request.agent, { env: settings.baseEnv ?? process.env });
|
|
134
135
|
await settings.refreshAgents?.();
|
|
135
136
|
return { patch: { status: signedIn.ok ? "done" : "error", result: `${signedIn.ok ? "OK" : "ERROR"}\n\n${signedIn.detail}\n`, finished_at: new Date().toISOString() } };
|
|
136
137
|
}
|
package/lib/runnerOps.mjs
CHANGED
|
@@ -132,7 +132,7 @@ async function writeOpResult(runnerApi, opId, result, rowsOpts, log, { attempts
|
|
|
132
132
|
return last;
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
-
export async function serviceRowOps(root, project, ops, settings, log = () => {}) {
|
|
135
|
+
export async function serviceRowOps(root, project, ops, settings, log = () => {}, { updatesUnread = false } = {}) {
|
|
136
136
|
const runnerApi = settings.runnerApi ?? runnerApiModule;
|
|
137
137
|
const rowsOpts = { env: settings.env, fetchImpl: settings.fetchImpl };
|
|
138
138
|
// OVERRIDABLE SO A TEST DOES NOT PAY FOR A REAL BACKOFF: every other
|
|
@@ -141,7 +141,16 @@ export async function serviceRowOps(root, project, ops, settings, log = () => {}
|
|
|
141
141
|
// being the one delay a test cannot shorten.
|
|
142
142
|
const retryOpts = { attempts: settings.opResultAttempts ?? 3, delayMs: settings.opResultRetryMs ?? 500 };
|
|
143
143
|
let ok = await flushComputerApps(root, project, runnerApi, rowsOpts);
|
|
144
|
+
// THE BOARD'S UPDATE BUTTON (#4949). The update is the whole computer's,
|
|
145
|
+
// so it goes to the one self-updater the daemon holds; this folder's ops
|
|
146
|
+
// missing its request is Cancel update, unless the board could not read
|
|
147
|
+
// the requests this time: then nothing is known and the wait goes on.
|
|
148
|
+
if (!updatesUnread) settings.selfUpdate?.seen(project, ops ?? []);
|
|
144
149
|
for (const op of ops ?? []) {
|
|
150
|
+
if (op.kind === "update") {
|
|
151
|
+
settings.selfUpdate?.request(op);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
145
154
|
if (op.kind === "computer-apps") {
|
|
146
155
|
ok = await serviceComputerApps(root, project, op, runnerApi, rowsOpts, settings.lookForComputerApps) && ok;
|
|
147
156
|
continue;
|
package/lib/runnerPace.mjs
CHANGED
|
@@ -40,6 +40,8 @@
|
|
|
40
40
|
// full runs-at-once count, a local lock) keeps the base interval, because none of
|
|
41
41
|
// those get better by calling less often and none of them load the server.
|
|
42
42
|
|
|
43
|
+
import { GOODBYE_EXIT_MS } from "./goodbye.mjs";
|
|
44
|
+
|
|
43
45
|
/** The wait after `failures` server errors in a row: base * 2^(n-1), capped,
|
|
44
46
|
* then jittered down by up to 20% so many runners do not retry in step. Never
|
|
45
47
|
* shorter than the base interval. */
|
|
@@ -148,18 +150,40 @@ function shareAnswer(send, { at, ttlMs, failureTtlMs }) {
|
|
|
148
150
|
}
|
|
149
151
|
const fresh = (entry, at) => entry.pending || at < entry.expiresAt;
|
|
150
152
|
|
|
153
|
+
/** How long before the exit a late goodbye is no longer started (#5308). */
|
|
154
|
+
const EXIT_MARGIN_MS = 500;
|
|
155
|
+
|
|
156
|
+
/** What a beat or register answers once the computer said goodbye. */
|
|
157
|
+
const SAID_GOODBYE = Object.freeze({ ok: false, transient: true, error: "This computer said goodbye." });
|
|
158
|
+
|
|
151
159
|
/** Coalesce `register` across the folders of one process (ticket e8 item 3).
|
|
152
160
|
* Every folder's beat registers the whole folder list, so the second and third
|
|
153
161
|
* folder beating inside the same window would send exactly the same call
|
|
154
162
|
* again. Within `ttlMs` of the last register for the same list, or while it is
|
|
155
163
|
* still in flight, callers share its answer; a failure for `failureTtlMs`. */
|
|
156
|
-
export function makeRegistrar({ ttlMs, failureTtlMs = ttlMs, now = Date.now }) {
|
|
164
|
+
export function makeRegistrar({ ttlMs, failureTtlMs = ttlMs, now = Date.now, heartbeats = null }) {
|
|
157
165
|
let last = null;
|
|
158
166
|
return {
|
|
159
|
-
register
|
|
167
|
+
/** A register writes presence too, without the goodbye. With the
|
|
168
|
+
* process's `heartbeats` hub, each one is held in the hub's in-flight
|
|
169
|
+
* set, so the goodbye waits for it, and none starts after the goodbye
|
|
170
|
+
* (ruling #4979). `writer(projectIds, {goodbye, timeoutMs})` is how to
|
|
171
|
+
* write a presence for the folders it registers: a stop during the first
|
|
172
|
+
* register, before any beat, still says goodbye through it (#5308). */
|
|
173
|
+
register(key, send, writer = null) {
|
|
174
|
+
if (heartbeats?.saidGoodbye) return Promise.resolve(SAID_GOODBYE);
|
|
160
175
|
const at = now();
|
|
161
176
|
if (last && last.key === key && fresh(last, at)) return last.promise;
|
|
162
|
-
|
|
177
|
+
const learning = writer && heartbeats?.learn
|
|
178
|
+
? async () => {
|
|
179
|
+
const answer = await send();
|
|
180
|
+
if (answer?.ok === true) heartbeats.learn((answer.projects ?? []).map((project) => project?.id).filter(Boolean), writer);
|
|
181
|
+
return answer;
|
|
182
|
+
}
|
|
183
|
+
: send;
|
|
184
|
+
last = Object.assign(shareAnswer(learning, { at, ttlMs, failureTtlMs }), { key });
|
|
185
|
+
// Held from now, not from when shareAnswer calls `send` a microtask later.
|
|
186
|
+
heartbeats?.hold(last.promise);
|
|
163
187
|
return last.promise;
|
|
164
188
|
},
|
|
165
189
|
// A login to another account makes every cached project id obsolete.
|
|
@@ -206,7 +230,20 @@ export function makeRegistrar({ ttlMs, failureTtlMs = ttlMs, now = Date.now }) {
|
|
|
206
230
|
* call and not the cached failure. A newer `invalidate(at)` makes existing answers
|
|
207
231
|
* stale, including one started in the same millisecond, so the next beat really
|
|
208
232
|
* asks: a join, a dropped socket or an op event. Every folder hearing the same event passes the same `at`, so they
|
|
209
|
-
* still share the one call that follows.
|
|
233
|
+
* still share the one call that follows.
|
|
234
|
+
*
|
|
235
|
+
* GOODBYE (ruling #4979, lib/goodbye.mjs). `farewell(projectIds, extra)` is
|
|
236
|
+
* the last beat: it closes the hub, so no beat or register starts after it,
|
|
237
|
+
* waits for every beat and register already on its way (`hold`, at most
|
|
238
|
+
* `extra.timeoutMs`), so none lands after it and replaces the presence, then
|
|
239
|
+
* sends once for every folder through the last `send` a folder gave, with
|
|
240
|
+
* `extra` (`{goodbye, timeoutMs}`) as its second argument. A write that is
|
|
241
|
+
* still on its way after that wait cannot be cancelled, so each time one
|
|
242
|
+
* settles the goodbye is sent again and lands last, until the process exit
|
|
243
|
+
* (`exitMs`, GOODBYE_EXIT_MS) less a margin (#5308). `ids()` lists the
|
|
244
|
+
* folders that have beaten and not left;
|
|
245
|
+
* `keepIds()` starts keeping every folder that beats from then on, leavers
|
|
246
|
+
* too, which the farewell adds to the ones it is given. */
|
|
210
247
|
export function makeHeartbeatHub({ ttlMs, memberTtlMs, failureTtlMs, now = Date.now }) {
|
|
211
248
|
const ttl = () => (typeof ttlMs === "function" ? ttlMs() : ttlMs);
|
|
212
249
|
const failureTtl = () => Math.min(typeof failureTtlMs === "function" ? failureTtlMs() : failureTtlMs ?? Infinity, ttl());
|
|
@@ -218,8 +255,23 @@ export function makeHeartbeatHub({ ttlMs, memberTtlMs, failureTtlMs, now = Date.
|
|
|
218
255
|
let generation = 0;
|
|
219
256
|
let batching = true;
|
|
220
257
|
const covered = (answer, ids) => (answer && typeof answer === "object" ? { ...answer, coveredProjectIds: ids } : answer);
|
|
258
|
+
const beaten = new Set(); // every folder that beat and has not left, for the goodbye
|
|
259
|
+
let kept = null; // once a stop is asked: every folder that beat since, even one that then left (#5308)
|
|
260
|
+
const inFlight = new Set();
|
|
261
|
+
let lastSend = null;
|
|
262
|
+
// Folders a register named and the way to write their presence, for a
|
|
263
|
+
// goodbye asked before any of them beat (#5308).
|
|
264
|
+
const registered = new Set();
|
|
265
|
+
let registerSend = null;
|
|
266
|
+
let farewell = null;
|
|
267
|
+
const track = (sending) => {
|
|
268
|
+
const settled = Promise.resolve(sending).catch(() => {}).finally(() => inFlight.delete(settled));
|
|
269
|
+
inFlight.add(settled);
|
|
270
|
+
return sending;
|
|
271
|
+
};
|
|
221
272
|
const single = async (projectId, send) => {
|
|
222
|
-
|
|
273
|
+
if (farewell) return covered(SAID_GOODBYE, [projectId]);
|
|
274
|
+
const answer = await track(send([projectId]));
|
|
223
275
|
if (batching) {
|
|
224
276
|
if (answer?.ok === false && answer.transient !== true) alone.add(projectId);
|
|
225
277
|
else if (answer?.ok === true) alone.delete(projectId);
|
|
@@ -231,6 +283,10 @@ export function makeHeartbeatHub({ ttlMs, memberTtlMs, failureTtlMs, now = Date.
|
|
|
231
283
|
return batching;
|
|
232
284
|
},
|
|
233
285
|
async beat(projectId, send) {
|
|
286
|
+
if (farewell) return covered(SAID_GOODBYE, [projectId]);
|
|
287
|
+
beaten.add(projectId);
|
|
288
|
+
kept?.add(projectId);
|
|
289
|
+
lastSend = send;
|
|
234
290
|
if (!batching || alone.has(projectId)) return single(projectId, send);
|
|
235
291
|
const at = now();
|
|
236
292
|
members.set(projectId, at);
|
|
@@ -241,6 +297,9 @@ export function makeHeartbeatHub({ ttlMs, memberTtlMs, failureTtlMs, now = Date.
|
|
|
241
297
|
const ids = [...members.keys()].sort();
|
|
242
298
|
const ttlNow = ttl();
|
|
243
299
|
entry = Object.assign(shareAnswer(() => send(ids), { at, ttlMs: ttlNow, failureTtlMs: failureTtl() }), { ids, generation });
|
|
300
|
+
// In flight from now: shareAnswer calls `send` a microtask later, and
|
|
301
|
+
// a goodbye asked in between must still wait for this beat.
|
|
302
|
+
track(entry.promise);
|
|
244
303
|
last = entry;
|
|
245
304
|
}
|
|
246
305
|
const answer = await entry.promise;
|
|
@@ -258,10 +317,80 @@ export function makeHeartbeatHub({ ttlMs, memberTtlMs, failureTtlMs, now = Date.
|
|
|
258
317
|
return covered(answer, entry.ids);
|
|
259
318
|
},
|
|
260
319
|
leave(projectId) {
|
|
320
|
+
beaten.delete(projectId);
|
|
321
|
+
registered.delete(projectId);
|
|
261
322
|
members.delete(projectId);
|
|
262
323
|
alone.delete(projectId);
|
|
263
324
|
if (last?.ids.includes(projectId)) last = null;
|
|
264
325
|
},
|
|
326
|
+
ids() {
|
|
327
|
+
return [...beaten].sort();
|
|
328
|
+
},
|
|
329
|
+
/** A stop was asked: from now on, remember every folder that beats, and
|
|
330
|
+
* keep the ones that leave, so the goodbye covers a folder whose first
|
|
331
|
+
* beat comes only while the process shuts down and whose watcher ends
|
|
332
|
+
* before the goodbye is sent (#5308). Folders beaten so far are kept. */
|
|
333
|
+
keepIds() {
|
|
334
|
+
kept ??= new Set(beaten);
|
|
335
|
+
},
|
|
336
|
+
get saidGoodbye() {
|
|
337
|
+
return farewell !== null;
|
|
338
|
+
},
|
|
339
|
+
/** Hold a presence write that is not a beat (a register) in flight. */
|
|
340
|
+
hold(sending) {
|
|
341
|
+
return track(sending);
|
|
342
|
+
},
|
|
343
|
+
/** A register that answered names its folders, so a goodbye still has
|
|
344
|
+
* them when that register was the only thing on its way. */
|
|
345
|
+
learn(projectIds, send) {
|
|
346
|
+
for (const id of projectIds) registered.add(id);
|
|
347
|
+
registerSend = send;
|
|
348
|
+
},
|
|
349
|
+
farewell(projectIds, extra, { waitMs = extra?.timeoutMs ?? 3_000, exitMs = GOODBYE_EXIT_MS } = {}) {
|
|
350
|
+
farewell ??= (async () => {
|
|
351
|
+
const started = now();
|
|
352
|
+
const deadline = started + exitMs - EXIT_MARGIN_MS;
|
|
353
|
+
// Read at each goodbye: a registration that lands late names folders
|
|
354
|
+
// too (#5308).
|
|
355
|
+
const idsNow = () => [...new Set([...projectIds, ...(kept ?? []), ...registered])].sort();
|
|
356
|
+
const ready = () => (lastSend ?? registerSend) && idsNow().length > 0;
|
|
357
|
+
const waitFor = async (pending) => {
|
|
358
|
+
const timer = new AbortController();
|
|
359
|
+
await Promise.race([...pending, sleep(deadline - now(), timer.signal)]);
|
|
360
|
+
timer.abort();
|
|
361
|
+
};
|
|
362
|
+
if (inFlight.size > 0) await Promise.race([Promise.all([...inFlight]), sleep(waitMs)]);
|
|
363
|
+
// No folder is known yet: a first registration still on its way will
|
|
364
|
+
// name them, so wait for it up to the exit deadline before giving up.
|
|
365
|
+
while (!ready() && inFlight.size > 0 && deadline - now() > 0) await waitFor([...inFlight]);
|
|
366
|
+
if (!ready()) return { ok: false, error: "No folder had beaten yet." };
|
|
367
|
+
// A write that outlasted the wait cannot be called back, and it may
|
|
368
|
+
// land after the goodbye and replace it. Fence it: each time one
|
|
369
|
+
// settles, say goodbye again, until the exit deadline (#5308).
|
|
370
|
+
const stragglers = [...inFlight];
|
|
371
|
+
const say = async () => {
|
|
372
|
+
try { return await (lastSend ?? registerSend)(idsNow(), extra); }
|
|
373
|
+
catch (error) { return { ok: false, error: error instanceof Error ? error.message : String(error) }; }
|
|
374
|
+
};
|
|
375
|
+
let result = await say();
|
|
376
|
+
const answered = new Set();
|
|
377
|
+
for (;;) {
|
|
378
|
+
const landed = stragglers.filter((late) => !inFlight.has(late) && !answered.has(late));
|
|
379
|
+
if (landed.length > 0) {
|
|
380
|
+
for (const late of landed) answered.add(late);
|
|
381
|
+
// The corrective send is the last word, ok or not: the board may
|
|
382
|
+
// read the late write's presence, so an earlier ok is not true.
|
|
383
|
+
result = await say();
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
const open = stragglers.filter((late) => inFlight.has(late));
|
|
387
|
+
if (open.length === 0 || deadline - now() <= 0) break;
|
|
388
|
+
await waitFor(open);
|
|
389
|
+
}
|
|
390
|
+
return result;
|
|
391
|
+
})();
|
|
392
|
+
return farewell;
|
|
393
|
+
},
|
|
265
394
|
invalidate(at = now()) {
|
|
266
395
|
if (at > notBefore) {
|
|
267
396
|
notBefore = at;
|
package/lib/runnerRegister.mjs
CHANGED
|
@@ -9,6 +9,7 @@ import { basename } from "node:path";
|
|
|
9
9
|
import * as runnerApiModule from "./runnerApi.mjs";
|
|
10
10
|
import { addFolder, foldersWithoutGit, isScratchFolder, markFolderRestore, readRegistry, readRegistryChecked, withRegistryLock } from "./registry.mjs";
|
|
11
11
|
import { platformMark } from "./platform.mjs";
|
|
12
|
+
import { SIGN_IN_AGENTS } from "./signIn.mjs";
|
|
12
13
|
|
|
13
14
|
/** `register` every folder this machine knows about, plus `root` itself as
|
|
14
15
|
* a fallback for a single-folder invocation (PM_BOARD_ROOT) that was never
|
|
@@ -31,8 +32,16 @@ import { platformMark } from "./platform.mjs";
|
|
|
31
32
|
* anyone who queries it at the wrong moment.
|
|
32
33
|
*
|
|
33
34
|
* Two writers of one body is how that happened, so there is one now.
|
|
35
|
+
*
|
|
36
|
+
* `signInAgents` names the agents whose sign-in the board can start here
|
|
37
|
+
* (lib/signIn.mjs); a computer on an older version leaves it out, and the
|
|
38
|
+
* board keeps its paste fallback for those (#5216, #5291).
|
|
39
|
+
*
|
|
40
|
+
* `goodbye` is the last beat of a computer that is being turned off,
|
|
41
|
+
* restarted or signed out of (ruling #4979, lib/goodbye.mjs). The next beat
|
|
42
|
+
* leaves it out, and since a beat replaces the presence, that clears it.
|
|
34
43
|
*/
|
|
35
|
-
export const presenceBody = (settings) => {
|
|
44
|
+
export const presenceBody = (settings, { goodbye } = {}) => {
|
|
36
45
|
const { platform: _platform, ...rest } = settings.markExtra?.() ?? {};
|
|
37
46
|
// A scratch folder is never registered, so it is never reported either:
|
|
38
47
|
// the app would otherwise show an old one still on disk again (#3644).
|
|
@@ -41,7 +50,7 @@ export const presenceBody = (settings) => {
|
|
|
41
50
|
try { return statSync(path).isDirectory(); }
|
|
42
51
|
catch (error) { return error.code !== "ENOENT" && error.code !== "ENOTDIR"; }
|
|
43
52
|
});
|
|
44
|
-
return { ...rest, ...(availableFolders ? { availableFolders } : {}), planApproval: true, computerAppsLook: true, folderReads: true, ...(settings.noGit?.length ? { noGit: settings.noGit } : {}) };
|
|
53
|
+
return { ...rest, ...(availableFolders ? { availableFolders } : {}), planApproval: true, computerAppsLook: true, folderReads: true, uninstallRequests: true, signInAgents: SIGN_IN_AGENTS, ...(settings.noGit?.length ? { noGit: settings.noGit } : {}), ...(goodbye ? { goodbye } : {}) };
|
|
45
54
|
};
|
|
46
55
|
|
|
47
56
|
/** Re-read the machine's folder list into `settings.projectPaths` before a
|
|
@@ -110,8 +119,13 @@ async function registerProject(root, settings, { readd = false, onlyRoot = false
|
|
|
110
119
|
// ONE REGISTER PER PROCESS PER BEAT (ticket e8). multiwatch hands every
|
|
111
120
|
// folder the same `registrar`, so the folders that beat inside one window
|
|
112
121
|
// share one call for the same folder list instead of each sending it.
|
|
113
|
-
|
|
114
|
-
|
|
122
|
+
// `writer` lets a goodbye reach these folders when no beat has yet (#5309).
|
|
123
|
+
const writer = (projectIds, { goodbye, timeoutMs } = {}) => runnerApi.heartbeat(
|
|
124
|
+
{ projectIds, presence: presenceBody(settings, { goodbye }) },
|
|
125
|
+
{ env, fetchImpl: settings.fetchImpl, ...(timeoutMs ? { timeoutMs } : {}) },
|
|
126
|
+
);
|
|
127
|
+
const out = settings.registrar ? await settings.registrar.register(JSON.stringify(list), send, writer) : await send();
|
|
128
|
+
if (!out.ok) return { ok: false, error: out.error, ...(readd ? { readdRequired: Boolean(readdForgottenAt) } : {}), ...(out.transient ? { transient: true } : {}), ...(out.unauthorized ? { unauthorized: true } : {}), ...(Number.isFinite(out.retryAfterMs) ? { retryAfterMs: out.retryAfterMs } : {}) };
|
|
115
129
|
const mine = (out.projects ?? []).find((p) => p.path === root);
|
|
116
130
|
return { ok: true, projects: out.projects ?? [], myProjectId: mine?.id, runnerId };
|
|
117
131
|
}
|