probe-research 0.21.0 → 0.22.0
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/bin/probe-research.js +12 -1
- package/lib/background-download.js +87 -8
- package/package.json +1 -1
package/bin/probe-research.js
CHANGED
|
@@ -897,7 +897,8 @@ async function waitForSignIn(json, startedMs) {
|
|
|
897
897
|
}
|
|
898
898
|
|
|
899
899
|
/** A `probe` at the agent floor: the one here, else the first download's (started if none runs), waited
|
|
900
|
-
* for within the budget; else `downloading` (75, run it again)
|
|
900
|
+
* for within the budget; else `downloading` (75, run it again), the service's reason when the download
|
|
901
|
+
* ended on one (`no_service`, `service_lost`, `updating`), or `incomplete` with the download's log. */
|
|
901
902
|
async function getAgentProbe(startedMs, step) {
|
|
902
903
|
const found = findProbe(AGENT_MIN_CLI);
|
|
903
904
|
if (found) return found;
|
|
@@ -913,6 +914,16 @@ async function getAgentProbe(startedMs, step) {
|
|
|
913
914
|
await refuseAgent(step, agentRun.Reason.DOWNLOADING, "Probe is finishing its install: run the same command again.",
|
|
914
915
|
agentRun.EXIT_AGAIN);
|
|
915
916
|
}
|
|
917
|
+
const serviceExit = !probe && done && done.state === download.DownloadState.FAILED
|
|
918
|
+
? download.getServiceExit(done.reason) : null;
|
|
919
|
+
if (serviceExit !== null) {
|
|
920
|
+
// Downloaded, but the Probe service did not take the install (PR29): its reason, so install.md's
|
|
921
|
+
// row for it applies (repair, or run it again), never "did not download".
|
|
922
|
+
await refuseAgent(step, done.reason, serviceExit === agentRun.EXIT_AGAIN
|
|
923
|
+
? `Probe is finishing an update (${done.error}): run the same command again.`
|
|
924
|
+
: `Probe downloaded, but its service did not answer, so its install did not finish (${done.error}); ` +
|
|
925
|
+
`its log: ${download.getFile(download.LOG_NAME)}`, serviceExit);
|
|
926
|
+
}
|
|
916
927
|
if (!probe) {
|
|
917
928
|
await refuseAgent(step, agentRun.Reason.INCOMPLETE,
|
|
918
929
|
`Probe did not download (${(done && done.error) || "no probe command after it"}); ` +
|
|
@@ -6,14 +6,16 @@
|
|
|
6
6
|
* Its own process, in its own session (`detached`), so an agent's 110 s kill of the command that
|
|
7
7
|
* started it never takes it; it ends by itself after DOWNLOAD_CAP_MS. One at a time
|
|
8
8
|
* (`download.lock`, the holder's pid), one log (`download.log`), one record the later commands read
|
|
9
|
-
* (`download.json`: running | done | failed
|
|
10
|
-
* `probe repair` instead: it finishes that uninstall
|
|
9
|
+
* (`download.json`: running | done | failed, a failed one the service's `reason` when its exit is one).
|
|
10
|
+
* A machine with an uninstall stopped halfway runs `probe repair` instead: it finishes that uninstall
|
|
11
|
+
* before anything is signed in (D47).
|
|
11
12
|
*/
|
|
12
13
|
|
|
13
14
|
const fs = require("node:fs");
|
|
14
15
|
const path = require("node:path");
|
|
15
16
|
const { spawn, spawnSync } = require("node:child_process");
|
|
16
17
|
|
|
18
|
+
const { AGENT_BUDGET_MS, DEADLINE_ENV } = require("./agent-run");
|
|
17
19
|
const { getStateFolder } = require("./sign-in");
|
|
18
20
|
|
|
19
21
|
const RECORD_NAME = "download.json";
|
|
@@ -25,6 +27,16 @@ const DOWNLOAD_CAP_MS = 10 * 60 * 1000;
|
|
|
25
27
|
const DOWNLOAD_VERB = "--probe-background-download";
|
|
26
28
|
|
|
27
29
|
const DownloadState = Object.freeze({ RUNNING: "running", DONE: "done", FAILED: "failed" });
|
|
30
|
+
/**
|
|
31
|
+
* A step's exit that is the Probe service's answer, not the download's (PR29): the `probe` shim's own
|
|
32
|
+
* (`ShimReason.BY_EXIT`: no service answered, it went away mid-command, an update held it) and
|
|
33
|
+
* `service start`'s busy (75). The record keeps the reason; the agent's call answers it with this exit.
|
|
34
|
+
*/
|
|
35
|
+
const SERVICE_EXITS = Object.freeze({ 69: "no_service", 70: "service_lost", 75: "updating" });
|
|
36
|
+
/** The step's exits it runs again after, once: no service answered, or it went away mid-command. */
|
|
37
|
+
const RETRIED_EXITS = Object.freeze([69, 70]);
|
|
38
|
+
/** How much of the log's end a failed step's own answer is looked for in. */
|
|
39
|
+
const ANSWER_TAIL_BYTES = 64 * 1024;
|
|
28
40
|
|
|
29
41
|
function getFile(name) {
|
|
30
42
|
return path.join(getStateFolder(), name);
|
|
@@ -138,13 +150,14 @@ function runDownload(steps) {
|
|
|
138
150
|
const started = Date.now();
|
|
139
151
|
saveDownload({ state: DownloadState.RUNNING, pid: process.pid, started_at: started / 1000 });
|
|
140
152
|
try {
|
|
141
|
-
for (const
|
|
142
|
-
|
|
143
|
-
|
|
153
|
+
for (const step of steps) {
|
|
154
|
+
let { ran, logged } = runStep(step, started);
|
|
155
|
+
// Once more with a fresh deadline when its service never answered: uv's own download may have used up
|
|
156
|
+
// the first one before `probe` started, and uv's copy is ready by now (PR29).
|
|
157
|
+
if (ran && !ran.error && RETRIED_EXITS.includes(ran.status)) ({ ran, logged } = runStep(step, started));
|
|
144
158
|
if (!ran || ran.error || ran.status !== 0) {
|
|
145
|
-
|
|
146
|
-
:
|
|
147
|
-
saveDownload({ state: DownloadState.FAILED, pid: process.pid, error: why, finished_at: Date.now() / 1000 });
|
|
159
|
+
saveDownload({ state: DownloadState.FAILED, pid: process.pid, ...getFailure(step[0], ran, logged),
|
|
160
|
+
finished_at: Date.now() / 1000 });
|
|
148
161
|
return 1;
|
|
149
162
|
}
|
|
150
163
|
}
|
|
@@ -155,12 +168,78 @@ function runDownload(steps) {
|
|
|
155
168
|
}
|
|
156
169
|
}
|
|
157
170
|
|
|
171
|
+
/** One step (`[cmd, ...args]`) within what is left of DOWNLOAD_CAP_MS: `{ran}` (null: no time left) and
|
|
172
|
+
* where its output starts in the log (`logged`). */
|
|
173
|
+
function runStep([cmd, ...args], started) {
|
|
174
|
+
const left = DOWNLOAD_CAP_MS - (Date.now() - started);
|
|
175
|
+
const logged = getLogSize();
|
|
176
|
+
// As an agent's call: the CLI's `probe` waits for a first service still starting until this deadline
|
|
177
|
+
// (a fresh Mac's outlasted its own 10 s, so the install that runs inside it never ran: PR29).
|
|
178
|
+
const env = { ...process.env, [DEADLINE_ENV]: String((Date.now() + AGENT_BUDGET_MS) / 1000) };
|
|
179
|
+
const ran = left > 0 ? spawnSync(cmd, args, { stdio: "inherit", env, timeout: left, killSignal: "SIGKILL" }) : null;
|
|
180
|
+
return { ran, logged };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** How a step failed: `{error}`, with the service's `reason` when its exit is one (SERVICE_EXITS). */
|
|
184
|
+
function getFailure(cmd, ran, logged) {
|
|
185
|
+
if (!ran) return { error: "the download ran out of time" };
|
|
186
|
+
if (ran.error) return { error: `${cmd}: ${ran.error.message}` };
|
|
187
|
+
const exited = `${path.basename(cmd)} exited ${ran.status}`;
|
|
188
|
+
const reason = SERVICE_EXITS[ran.status];
|
|
189
|
+
if (!reason) return { error: exited };
|
|
190
|
+
return { reason, error: loadStepMessage(logged) || `the Probe service did not answer (${exited})` };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The exit an agent's call answers a failed download's service `reason` with, or null: not one. */
|
|
194
|
+
function getServiceExit(reason) {
|
|
195
|
+
const found = Object.keys(SERVICE_EXITS).find((code) => SERVICE_EXITS[code] === reason);
|
|
196
|
+
return found === undefined ? null : Number(found);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function getLogSize() {
|
|
200
|
+
try {
|
|
201
|
+
return fs.statSync(getFile(LOG_NAME)).size;
|
|
202
|
+
} catch {
|
|
203
|
+
return 0;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The first line of the last `message` a step's `--json` answer put in the log past `offset`, or null. */
|
|
208
|
+
function loadStepMessage(offset) {
|
|
209
|
+
let text = "";
|
|
210
|
+
try {
|
|
211
|
+
const fd = fs.openSync(getFile(LOG_NAME), "r");
|
|
212
|
+
try {
|
|
213
|
+
const size = fs.fstatSync(fd).size;
|
|
214
|
+
const length = Math.max(0, Math.min(size - offset, ANSWER_TAIL_BYTES));
|
|
215
|
+
const buffer = Buffer.alloc(length);
|
|
216
|
+
fs.readSync(fd, buffer, 0, length, size - length);
|
|
217
|
+
text = buffer.toString("utf8");
|
|
218
|
+
} finally {
|
|
219
|
+
fs.closeSync(fd);
|
|
220
|
+
}
|
|
221
|
+
} catch {
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
for (const line of text.split("\n").reverse()) {
|
|
225
|
+
try {
|
|
226
|
+
const answer = JSON.parse(line);
|
|
227
|
+
if (answer && typeof answer.message === "string" && answer.message.trim()) {
|
|
228
|
+
return answer.message.trim().split("\n")[0].slice(0, 300);
|
|
229
|
+
}
|
|
230
|
+
} catch {}
|
|
231
|
+
}
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
|
|
158
235
|
module.exports = {
|
|
159
236
|
DOWNLOAD_CAP_MS,
|
|
160
237
|
DOWNLOAD_VERB,
|
|
161
238
|
DownloadState,
|
|
162
239
|
LOG_NAME,
|
|
240
|
+
SERVICE_EXITS,
|
|
163
241
|
getFile,
|
|
242
|
+
getServiceExit,
|
|
164
243
|
loadDownload,
|
|
165
244
|
runDownload,
|
|
166
245
|
startDownload,
|