probe-research 0.21.0 → 0.23.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 +36 -3
- package/lib/background-download.js +87 -8
- package/package.json +1 -1
package/bin/probe-research.js
CHANGED
|
@@ -135,8 +135,12 @@ const FIX_MIN = "0.385.0";
|
|
|
135
135
|
* cut before it merged.
|
|
136
136
|
* 0.432.0 since PR25: a set-up computer's agent calls run PR22's CLI (Recommended = on) and PR26's
|
|
137
137
|
* `update --json`; 0.431.0 was cut at PR22's merge, before PR26.
|
|
138
|
+
* 0.440.0 since PR34 (#4456): the re-paste menu's `status --repaste-choice settings|account`, its `--json`
|
|
139
|
+
* sign-out and agent removal (`isMenuChange`), and Change settings' `install` keeping this chat's own switch;
|
|
140
|
+
* 0.439.0 was cut before it merged. Launcher 0.23.0 carries it (0.22.0 was already on npm, so #4456's own
|
|
141
|
+
* floor never published).
|
|
138
142
|
*/
|
|
139
|
-
const AGENT_MIN_CLI = "0.
|
|
143
|
+
const AGENT_MIN_CLI = "0.440.0";
|
|
140
144
|
/** `install` flags only AGENT_MIN_CLI knows (D36). */
|
|
141
145
|
const AGENT_INSTALL_FLAGS = Object.freeze(["--recommended", "--who-records", "--session-default", "--profile",
|
|
142
146
|
"--attempt"]);
|
|
@@ -447,6 +451,7 @@ const SHIM_REASONS = Object.freeze({ 69: "no_service", 70: "service_lost" });
|
|
|
447
451
|
/** The step an agent's `probe` arguments are (`AgentStep`). */
|
|
448
452
|
function getAgentStep(forwarded) {
|
|
449
453
|
if (forwarded[0] === LauncherVerb.WIZARD) {
|
|
454
|
+
if (isMenuChange(forwarded)) return AgentStep.OTHER;
|
|
450
455
|
return forwarded.includes("--pending") ? AgentStep.SIGN_IN_WAIT : AgentStep.INSTALL;
|
|
451
456
|
}
|
|
452
457
|
return [AgentStep.STATUS, AgentStep.IMPORT, AgentStep.EXAMPLE].includes(forwarded[0]) ? forwarded[0] : AgentStep.OTHER;
|
|
@@ -722,13 +727,30 @@ const AGENT_STATUS_FLAGS = Object.freeze(["--repaste-choice", "--past-work-choic
|
|
|
722
727
|
/** `import` verbs only AGENT_MIN_CLI has: bringing in past work (D49, D74). `import wandb` is older. */
|
|
723
728
|
const AGENT_IMPORT_VERBS = Object.freeze(["folders", "review", "start", "status", "resume"]);
|
|
724
729
|
|
|
730
|
+
/** The wizard action a `probe wizard` call names (`--action X` or `--action=X`), or null. */
|
|
731
|
+
function getWizardAction(forwarded) {
|
|
732
|
+
for (let i = 1; i < forwarded.length; i++) {
|
|
733
|
+
if (forwarded[i] === "--action") return forwarded[i + 1] ?? null;
|
|
734
|
+
if (forwarded[i].startsWith("--action=")) return forwarded[i].slice("--action=".length);
|
|
735
|
+
}
|
|
736
|
+
return null;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
/** The re-paste menu's sign-out and agent removal (PR34): answered as an agent's call (`done.reason`) only
|
|
740
|
+
* under `--json`, so only then does the agent floor run them. */
|
|
741
|
+
function isMenuChange(forwarded) {
|
|
742
|
+
if (forwarded[0] !== LauncherVerb.WIZARD || !forwarded.includes("--json")) return false;
|
|
743
|
+
const action = getWizardAction(forwarded);
|
|
744
|
+
return action === "logout" || (action === "uninstall" && forwarded.some((word) => word.split("=")[0] === "--agent"));
|
|
745
|
+
}
|
|
746
|
+
|
|
725
747
|
/** `probe` arguments only AGENT_MIN_CLI runs (D36). */
|
|
726
748
|
function needsAgentCli(forwarded) {
|
|
727
749
|
if (forwarded[0] === "example") return true;
|
|
728
750
|
if (forwarded[0] === "import") return AGENT_IMPORT_VERBS.includes(forwarded[1]);
|
|
729
751
|
if (forwarded[0] === "status") return forwarded.some((word) => AGENT_STATUS_FLAGS.includes(word.split("=")[0]));
|
|
730
752
|
if (forwarded[0] !== LauncherVerb.WIZARD) return false;
|
|
731
|
-
return forwarded.includes("--pending")
|
|
753
|
+
return forwarded.includes("--pending") || isMenuChange(forwarded)
|
|
732
754
|
|| forwarded.some((word) => AGENT_INSTALL_FLAGS.includes(word.split("=")[0]));
|
|
733
755
|
}
|
|
734
756
|
|
|
@@ -897,7 +919,8 @@ async function waitForSignIn(json, startedMs) {
|
|
|
897
919
|
}
|
|
898
920
|
|
|
899
921
|
/** 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)
|
|
922
|
+
* for within the budget; else `downloading` (75, run it again), the service's reason when the download
|
|
923
|
+
* ended on one (`no_service`, `service_lost`, `updating`), or `incomplete` with the download's log. */
|
|
901
924
|
async function getAgentProbe(startedMs, step) {
|
|
902
925
|
const found = findProbe(AGENT_MIN_CLI);
|
|
903
926
|
if (found) return found;
|
|
@@ -913,6 +936,16 @@ async function getAgentProbe(startedMs, step) {
|
|
|
913
936
|
await refuseAgent(step, agentRun.Reason.DOWNLOADING, "Probe is finishing its install: run the same command again.",
|
|
914
937
|
agentRun.EXIT_AGAIN);
|
|
915
938
|
}
|
|
939
|
+
const serviceExit = !probe && done && done.state === download.DownloadState.FAILED
|
|
940
|
+
? download.getServiceExit(done.reason) : null;
|
|
941
|
+
if (serviceExit !== null) {
|
|
942
|
+
// Downloaded, but the Probe service did not take the install (PR29): its reason, so install.md's
|
|
943
|
+
// row for it applies (repair, or run it again), never "did not download".
|
|
944
|
+
await refuseAgent(step, done.reason, serviceExit === agentRun.EXIT_AGAIN
|
|
945
|
+
? `Probe is finishing an update (${done.error}): run the same command again.`
|
|
946
|
+
: `Probe downloaded, but its service did not answer, so its install did not finish (${done.error}); ` +
|
|
947
|
+
`its log: ${download.getFile(download.LOG_NAME)}`, serviceExit);
|
|
948
|
+
}
|
|
916
949
|
if (!probe) {
|
|
917
950
|
await refuseAgent(step, agentRun.Reason.INCOMPLETE,
|
|
918
951
|
`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,
|