probe-research 0.19.0 → 0.21.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 +172 -52
- package/lib/setup-check.js +69 -0
- package/lib/sign-in.js +22 -7
- package/package.json +1 -1
package/bin/probe-research.js
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
* one PostHog event so we hear who asked (plan R17).
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
|
-
const { spawnSync } = require("node:child_process");
|
|
28
|
+
const { spawn, spawnSync } = require("node:child_process");
|
|
29
29
|
const crypto = require("node:crypto");
|
|
30
30
|
const fs = require("node:fs");
|
|
31
31
|
const os = require("node:os");
|
|
@@ -33,6 +33,7 @@ const path = require("node:path");
|
|
|
33
33
|
|
|
34
34
|
const agentRun = require("../lib/agent-run");
|
|
35
35
|
const download = require("../lib/background-download");
|
|
36
|
+
const setupCheck = require("../lib/setup-check");
|
|
36
37
|
const signIn = require("../lib/sign-in");
|
|
37
38
|
|
|
38
39
|
const DIST = "probe-research";
|
|
@@ -130,8 +131,12 @@ const FIX_MIN = "0.385.0";
|
|
|
130
131
|
* merged) lacks.
|
|
131
132
|
* 0.424.0 since PR12 (#4381): runs-only.md's `install --profile runs` and already-set-up.md's
|
|
132
133
|
* `install --profile` and `status --repaste-choice`; 0.423.0 was cut from the commit before it merged.
|
|
134
|
+
* 0.425.0 since PR15: past-work.md's `probe import folders|review|start|status|resume`; 0.424.0 was
|
|
135
|
+
* cut before it merged.
|
|
136
|
+
* 0.432.0 since PR25: a set-up computer's agent calls run PR22's CLI (Recommended = on) and PR26's
|
|
137
|
+
* `update --json`; 0.431.0 was cut at PR22's merge, before PR26.
|
|
133
138
|
*/
|
|
134
|
-
const AGENT_MIN_CLI = "0.
|
|
139
|
+
const AGENT_MIN_CLI = "0.432.0";
|
|
135
140
|
/** `install` flags only AGENT_MIN_CLI knows (D36). */
|
|
136
141
|
const AGENT_INSTALL_FLAGS = Object.freeze(["--recommended", "--who-records", "--session-default", "--profile",
|
|
137
142
|
"--attempt"]);
|
|
@@ -207,21 +212,26 @@ function installedVersion(command = "probe") {
|
|
|
207
212
|
* its bin folder (a first download's, before the agent's shell PATH knows that folder); null.
|
|
208
213
|
*/
|
|
209
214
|
function findProbe(min) {
|
|
210
|
-
|
|
211
|
-
for (const candidate of candidates.filter(Boolean)) {
|
|
212
|
-
if (candidate !== "probe") {
|
|
213
|
-
try {
|
|
214
|
-
fs.accessSync(candidate, fs.constants.X_OK);
|
|
215
|
-
} catch {
|
|
216
|
-
continue;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
215
|
+
for (const candidate of getProbeCommands()) {
|
|
219
216
|
const found = installedVersion(candidate);
|
|
220
217
|
if (found && atLeast(found, min)) return candidate;
|
|
221
218
|
}
|
|
222
219
|
return null;
|
|
223
220
|
}
|
|
224
221
|
|
|
222
|
+
/** Every `probe` here, answering or not: the one on PATH, then the runnable ones in uv's bin folders. */
|
|
223
|
+
function getProbeCommands() {
|
|
224
|
+
const installed = getUvBinFolders(process.env).map((d) => path.join(d, "probe")).filter((candidate) => {
|
|
225
|
+
try {
|
|
226
|
+
fs.accessSync(candidate, fs.constants.X_OK);
|
|
227
|
+
return true;
|
|
228
|
+
} catch {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
return [...(has("probe") ? ["probe"] : []), ...installed];
|
|
233
|
+
}
|
|
234
|
+
|
|
225
235
|
function baseUrl() {
|
|
226
236
|
const env = process.env.PROBE_BASE_URL;
|
|
227
237
|
if (env && /^https?:\/\//.test(env)) return env.replace(/\/+$/, "");
|
|
@@ -289,11 +299,17 @@ const LauncherVerb = Object.freeze({
|
|
|
289
299
|
INSTALL: "install",
|
|
290
300
|
/** Launcher-only: the install prompt's sign-in (`runSignIn`), before any CLI exists. */
|
|
291
301
|
SIGN_IN: "sign-in",
|
|
302
|
+
/** The wizard's Update (`probe update` is retired): `update --json` is the agent's (PR26). */
|
|
303
|
+
UPDATE: "update",
|
|
292
304
|
});
|
|
293
305
|
/** What `sign-in` takes (the hosted install.md names them; its test reads this list). */
|
|
294
|
-
const SIGN_IN_FLAGS = Object.freeze(["--json", "--wait", "--no-browser"]);
|
|
306
|
+
const SIGN_IN_FLAGS = Object.freeze(["--json", "--wait", "--no-browser", "--if-needed"]);
|
|
307
|
+
/** `sign-in`'s flag for install.md's first call: on a computer already set up, status's answer and no tab. */
|
|
308
|
+
const IF_NEEDED_FLAG = "--if-needed";
|
|
295
309
|
/** What `install` means -- exactly what `probe install` always ran (`main.py`). */
|
|
296
310
|
const INSTALL_ACTION = Object.freeze(["--action", "configure"]);
|
|
311
|
+
/** What `update` means: the wizard's Update, which `probe update` became. */
|
|
312
|
+
const UPDATE_ACTION = Object.freeze(["--action", "update"]);
|
|
297
313
|
/** The website's retired one-time sign-in code; the CLI ignores the same shape. */
|
|
298
314
|
const SIGN_IN_CODE = /^[A-Z0-9]{10}$/;
|
|
299
315
|
/** The CLI root's own options: a call that starts with one names its own
|
|
@@ -313,6 +329,7 @@ function signInCodeAt(args) {
|
|
|
313
329
|
*
|
|
314
330
|
* (nothing), CODE -> wizard
|
|
315
331
|
* install [CODE] [flags] -> wizard --action configure [flags]
|
|
332
|
+
* update [flags] -> wizard --action update [flags]
|
|
316
333
|
* CODE install [flags] -> wizard --action configure [flags]
|
|
317
334
|
* --flag ... -> wizard --flag ... (the CLI root's own excepted)
|
|
318
335
|
* anything else -> unchanged (`doctor`, `backfill`, `wizard ...`)
|
|
@@ -327,6 +344,7 @@ function cliArgs(args) {
|
|
|
327
344
|
const [first, ...rest] = words;
|
|
328
345
|
if (first === undefined) return [LauncherVerb.WIZARD];
|
|
329
346
|
if (first === LauncherVerb.INSTALL) return [LauncherVerb.WIZARD, ...INSTALL_ACTION, ...rest];
|
|
347
|
+
if (first === LauncherVerb.UPDATE) return [LauncherVerb.WIZARD, ...UPDATE_ACTION, ...rest];
|
|
330
348
|
// Flags alone (`npx probe-research --agent codex`) used to reach the CLI's
|
|
331
349
|
// root as `probe --agent codex`, which is not a command at all.
|
|
332
350
|
if (first.startsWith("-") && !ROOT_ONLY_FLAGS.includes(first.split("=")[0])) {
|
|
@@ -411,8 +429,68 @@ async function sendUnsupportedPlatform(env) {
|
|
|
411
429
|
/** The install prompt's first funnel step (plan D14), sent by the launcher before any CLI exists. */
|
|
412
430
|
const SIGN_IN_STARTED_EVENT = "agent_install.signin_started";
|
|
413
431
|
|
|
414
|
-
/**
|
|
415
|
-
|
|
432
|
+
/** The launcher's own refusal of an agent's command (PR24); the CLI's ride on its step's event. */
|
|
433
|
+
const REFUSED_EVENT = "agent_install.refused";
|
|
434
|
+
/** `agent_install.refused`'s `step`: the install prompt's command the launcher refused. */
|
|
435
|
+
const AgentStep = Object.freeze({
|
|
436
|
+
SIGN_IN: "sign_in",
|
|
437
|
+
SIGN_IN_WAIT: "sign_in_wait",
|
|
438
|
+
INSTALL: "install",
|
|
439
|
+
STATUS: "status",
|
|
440
|
+
IMPORT: "import",
|
|
441
|
+
EXAMPLE: "example",
|
|
442
|
+
OTHER: "other",
|
|
443
|
+
});
|
|
444
|
+
/** The `probe` shim's exits the agent reads as a reason (`vocabularies.ShimReason.BY_EXIT`). */
|
|
445
|
+
const SHIM_REASONS = Object.freeze({ 69: "no_service", 70: "service_lost" });
|
|
446
|
+
|
|
447
|
+
/** The step an agent's `probe` arguments are (`AgentStep`). */
|
|
448
|
+
function getAgentStep(forwarded) {
|
|
449
|
+
if (forwarded[0] === LauncherVerb.WIZARD) {
|
|
450
|
+
return forwarded.includes("--pending") ? AgentStep.SIGN_IN_WAIT : AgentStep.INSTALL;
|
|
451
|
+
}
|
|
452
|
+
return [AgentStep.STATUS, AgentStep.IMPORT, AgentStep.EXAMPLE].includes(forwarded[0]) ? forwarded[0] : AgentStep.OTHER;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/** One bounded `agent_install.refused` for `step`, then the refusal itself (`agentRun.refuse`). */
|
|
456
|
+
async function refuseAgent(step, reason, message, code) {
|
|
457
|
+
await sendLauncherEvent(process.env, REFUSED_EVENT, {
|
|
458
|
+
step, reason, outcome: code === agentRun.EXIT_AGAIN ? "run_again" : "failed",
|
|
459
|
+
chat_agent: signIn.detectAgent(process.env),
|
|
460
|
+
});
|
|
461
|
+
agentRun.refuse(reason, message, code);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/** The hidden word the launcher re-runs itself with, detached, to send one of these events after it exits. */
|
|
465
|
+
const EVENT_VERB = "--probe-launcher-event";
|
|
466
|
+
const BACKGROUND_EVENTS = Object.freeze([SIGN_IN_STARTED_EVENT]);
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Send one launcher event from a detached copy of the launcher, so the answer never waits on PostHog
|
|
470
|
+
* (~0.2 s from a fresh connection, up to TELEMETRY_TIMEOUT_MS; PR25). `props` ride as JSON. Never throws.
|
|
471
|
+
*/
|
|
472
|
+
function sendEventInBackground(env, event, props = {}) {
|
|
473
|
+
if (!maySendTelemetry(env)) return;
|
|
474
|
+
try {
|
|
475
|
+
const child = spawn(process.execPath, [__filename, EVENT_VERB, event, JSON.stringify(props)],
|
|
476
|
+
{ detached: true, stdio: "ignore", env });
|
|
477
|
+
child.on("error", () => {});
|
|
478
|
+
child.unref();
|
|
479
|
+
} catch {}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** The detached copy's work: one event from BACKGROUND_EVENTS, its JSON `props` (`sendEventInBackground`). */
|
|
483
|
+
async function sendBackgroundEvent(args) {
|
|
484
|
+
if (!BACKGROUND_EVENTS.includes(args[0])) return;
|
|
485
|
+
let props = {};
|
|
486
|
+
try {
|
|
487
|
+
props = JSON.parse(args[1] || "{}");
|
|
488
|
+
} catch {}
|
|
489
|
+
await sendLauncherEvent(process.env, args[0], props && typeof props === "object" ? props : {});
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** POST one anonymous launcher event with `props`, awaited and bounded (TELEMETRY_TIMEOUT_MS); never throws. */
|
|
493
|
+
async function sendLauncherEvent(env, event, props = {}) {
|
|
416
494
|
if (!maySendTelemetry(env)) return;
|
|
417
495
|
try {
|
|
418
496
|
const machine = machineId(env);
|
|
@@ -429,6 +507,7 @@ async function sendLauncherEvent(env, event) {
|
|
|
429
507
|
distinct_id: `machine:${machine}`,
|
|
430
508
|
timestamp: new Date().toISOString(),
|
|
431
509
|
properties: {
|
|
510
|
+
...props,
|
|
432
511
|
surface: "launcher",
|
|
433
512
|
os: OS_NAMES[process.platform] || process.platform,
|
|
434
513
|
arch: process.arch,
|
|
@@ -505,6 +584,7 @@ async function main() {
|
|
|
505
584
|
const startedMs = Date.now();
|
|
506
585
|
const args = process.argv.slice(2);
|
|
507
586
|
if (args[0] === download.DOWNLOAD_VERB) return runBackgroundDownload();
|
|
587
|
+
if (args[0] === EVENT_VERB) return sendBackgroundEvent(args.slice(1));
|
|
508
588
|
if (args[0] === LauncherVerb.SIGN_IN) return runSignIn(args.slice(1), startedMs);
|
|
509
589
|
// `npx probe-research` with no arguments runs the wizard — that is the entire
|
|
510
590
|
// reason this package exists, so it should not require remembering a verb.
|
|
@@ -517,17 +597,23 @@ async function main() {
|
|
|
517
597
|
process.exit(1);
|
|
518
598
|
}
|
|
519
599
|
if (signInCodeAt(args) >= 0) console.error(SIGN_IN_CODE_NOTE);
|
|
520
|
-
if (isStatusJson(forwarded) &&
|
|
600
|
+
if (isStatusJson(forwarded) && getProbeCommands().length === 0) {
|
|
521
601
|
// No Probe here: the install prompt's first read (step 1b) answers at once, with no download (D37).
|
|
522
|
-
|
|
602
|
+
// A `probe` that does not answer (its service restarting) is Probe here: its status runs (P1b).
|
|
603
|
+
// A saved sign-in with no CLI is Probe here too: the CLI first, then its status, never `none` (F4).
|
|
604
|
+
// Windows has no CLI build: a sign-in the SDK saved there is no install to read.
|
|
605
|
+
if (process.platform === "win32" || !signIn.hasSavedSignIn(process.env)) {
|
|
606
|
+
agentRun.answer({ v: 1, setup: NONE_SETUP }, 0);
|
|
607
|
+
}
|
|
608
|
+
return runAgentCall(await getAgentProbe(startedMs, AgentStep.STATUS), forwarded, startedMs, AgentStep.STATUS);
|
|
523
609
|
}
|
|
524
610
|
const agentCli = needsAgentCli(forwarded);
|
|
525
611
|
// An agent's call with --json: inside its 110 s, its process group stopped at the deadline (D67).
|
|
526
612
|
const launch = isAgentCall(forwarded)
|
|
527
|
-
? (cmd, cmdArgs) => runAgentCall(cmd, cmdArgs, startedMs) : run;
|
|
613
|
+
? (cmd, cmdArgs) => runAgentCall(cmd, cmdArgs, startedMs, getAgentStep(forwarded)) : run;
|
|
528
614
|
if (agentCli) {
|
|
529
615
|
// The first download is still installing the CLI: wait for it within the budget (D41).
|
|
530
|
-
if (isAgentCall(forwarded)) await waitForRunningDownload(startedMs);
|
|
616
|
+
if (isAgentCall(forwarded)) await waitForRunningDownload(startedMs, getAgentStep(forwarded));
|
|
531
617
|
const installed = findProbe(AGENT_MIN_CLI);
|
|
532
618
|
if (installed) return launch(installed, forwarded);
|
|
533
619
|
}
|
|
@@ -631,12 +717,15 @@ const DOWNLOAD_POLL_MS = 1000;
|
|
|
631
717
|
/** The uninstall barrier (`probe.shared.updates.uninstall_barrier.path`). */
|
|
632
718
|
const BARRIER_FILE = [".local", "state", "probe", "uninstall", "barrier.json"];
|
|
633
719
|
|
|
634
|
-
/** `status` flags only AGENT_MIN_CLI knows: the re-paste menu's answer (D84). */
|
|
635
|
-
const AGENT_STATUS_FLAGS = Object.freeze(["--repaste-choice"]);
|
|
720
|
+
/** `status` flags only AGENT_MIN_CLI knows: the re-paste menu's answer (D84), the past-work offer's (PR24). */
|
|
721
|
+
const AGENT_STATUS_FLAGS = Object.freeze(["--repaste-choice", "--past-work-choice"]);
|
|
722
|
+
/** `import` verbs only AGENT_MIN_CLI has: bringing in past work (D49, D74). `import wandb` is older. */
|
|
723
|
+
const AGENT_IMPORT_VERBS = Object.freeze(["folders", "review", "start", "status", "resume"]);
|
|
636
724
|
|
|
637
725
|
/** `probe` arguments only AGENT_MIN_CLI runs (D36). */
|
|
638
726
|
function needsAgentCli(forwarded) {
|
|
639
727
|
if (forwarded[0] === "example") return true;
|
|
728
|
+
if (forwarded[0] === "import") return AGENT_IMPORT_VERBS.includes(forwarded[1]);
|
|
640
729
|
if (forwarded[0] === "status") return forwarded.some((word) => AGENT_STATUS_FLAGS.includes(word.split("=")[0]));
|
|
641
730
|
if (forwarded[0] !== LauncherVerb.WIZARD) return false;
|
|
642
731
|
return forwarded.includes("--pending")
|
|
@@ -660,14 +749,20 @@ function sleep(ms) {
|
|
|
660
749
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
661
750
|
}
|
|
662
751
|
|
|
663
|
-
/** Run an agent's call under the backstop; a call it had to stop answers `run_again` (75). */
|
|
664
|
-
async function runAgentCall(cmd, cmdArgs, startedMs) {
|
|
752
|
+
/** Run an agent's call (`step`) under the backstop; a call it had to stop answers `run_again` (75). */
|
|
753
|
+
async function runAgentCall(cmd, cmdArgs, startedMs, step) {
|
|
665
754
|
const ran = await agentRun.runWithBackstop(cmd, cmdArgs, { deadlineMs: startedMs + agentRun.AGENT_BUDGET_MS });
|
|
666
755
|
if (ran.stopped) {
|
|
667
|
-
|
|
756
|
+
await refuseAgent(step, agentRun.Reason.RUN_AGAIN, "Stopped at the agent's time limit: run the same command again.",
|
|
668
757
|
agentRun.EXIT_AGAIN);
|
|
669
758
|
}
|
|
670
|
-
if (ran.error)
|
|
759
|
+
if (ran.error) await refuseAgent(step, agentRun.Reason.INCOMPLETE, `could not run ${cmd}: ${ran.error}`, 1);
|
|
760
|
+
if (SHIM_REASONS[ran.code]) {
|
|
761
|
+
// The shim answered the agent itself (no service, or it went away): the CLI never said.
|
|
762
|
+
await sendLauncherEvent(process.env, REFUSED_EVENT, {
|
|
763
|
+
step, reason: SHIM_REASONS[ran.code], outcome: "failed", chat_agent: signIn.detectAgent(process.env),
|
|
764
|
+
});
|
|
765
|
+
}
|
|
671
766
|
process.exit(ran.code);
|
|
672
767
|
}
|
|
673
768
|
|
|
@@ -709,20 +804,20 @@ async function finishStoppedUninstall(deadlineMs) {
|
|
|
709
804
|
"repair", "--json"],
|
|
710
805
|
{ stdio: ["ignore", 2, 2], timeout: Math.max(1000, deadlineMs - WAIT_RESERVE_MS - Date.now()) });
|
|
711
806
|
if (hasBarrier()) {
|
|
712
|
-
|
|
807
|
+
await refuseAgent(AgentStep.SIGN_IN, agentRun.Reason.UNINSTALL_RUNNING,
|
|
713
808
|
"An uninstall of Probe stopped halfway and is being finished first: run the same command again.",
|
|
714
809
|
agentRun.EXIT_AGAIN);
|
|
715
810
|
}
|
|
716
811
|
}
|
|
717
812
|
|
|
718
|
-
/** An agent's call while the first download runs: wait within the budget, else `downloading` (75). */
|
|
719
|
-
async function waitForRunningDownload(startedMs) {
|
|
813
|
+
/** An agent's call (`step`) while the first download runs: wait within the budget, else `downloading` (75). */
|
|
814
|
+
async function waitForRunningDownload(startedMs, step) {
|
|
720
815
|
const until = startedMs + agentRun.AGENT_BUDGET_MS - WAIT_RESERVE_MS;
|
|
721
816
|
for (;;) {
|
|
722
817
|
const record = download.loadDownload();
|
|
723
818
|
if (!record || record.state !== download.DownloadState.RUNNING) return record;
|
|
724
819
|
if (Date.now() >= until) {
|
|
725
|
-
|
|
820
|
+
await refuseAgent(step, agentRun.Reason.DOWNLOADING, "Probe is still downloading: run the same command again.",
|
|
726
821
|
agentRun.EXIT_AGAIN);
|
|
727
822
|
}
|
|
728
823
|
await sleep(DOWNLOAD_POLL_MS);
|
|
@@ -733,7 +828,7 @@ async function waitForRunningDownload(startedMs) {
|
|
|
733
828
|
async function runSignIn(rest, startedMs) {
|
|
734
829
|
const deadlineMs = startedMs + agentRun.AGENT_BUDGET_MS;
|
|
735
830
|
const unknown = rest.find((word) => !SIGN_IN_FLAGS.includes(word));
|
|
736
|
-
if (unknown)
|
|
831
|
+
if (unknown) await refuseAgent(AgentStep.SIGN_IN, agentRun.Reason.INCOMPLETE, `sign-in takes ${SIGN_IN_FLAGS.join(", ")}, not ${unknown}`, 2);
|
|
737
832
|
if (process.platform === "win32") {
|
|
738
833
|
console.error(UNSUPPORTED_PLATFORM);
|
|
739
834
|
await sendUnsupportedPlatform(process.env);
|
|
@@ -741,15 +836,16 @@ async function runSignIn(rest, startedMs) {
|
|
|
741
836
|
}
|
|
742
837
|
const hosted = signIn.findHostedAgent(process.env);
|
|
743
838
|
if (hosted) {
|
|
744
|
-
|
|
745
|
-
+ "cannot be set up from it. Paste this into an agent on your own computer.", 1);
|
|
839
|
+
await refuseAgent(AgentStep.SIGN_IN, agentRun.Reason.HOSTED_AGENT, `This chat runs in ${hosted}, not on your `
|
|
840
|
+
+ "computer, so Probe cannot be set up from it. Paste this into an agent on your own computer.", 1);
|
|
746
841
|
}
|
|
747
842
|
if (rest.includes("--wait")) return waitForSignIn(rest.includes("--json"), startedMs);
|
|
748
843
|
if (hasBarrier()) await finishStoppedUninstall(deadlineMs);
|
|
844
|
+
if (rest.includes(IF_NEEDED_FLAG)) await answerIfSetUp(deadlineMs);
|
|
749
845
|
const started = await signIn.startSignIn({ noBrowser: rest.includes("--no-browser") });
|
|
750
|
-
if (!started.ok)
|
|
846
|
+
if (!started.ok) await refuseAgent(AgentStep.SIGN_IN, agentRun.Reason.INCOMPLETE, started.message, 1);
|
|
751
847
|
const downloading = findProbe(AGENT_MIN_CLI) ? "not_needed" : startBackgroundDownload();
|
|
752
|
-
|
|
848
|
+
sendEventInBackground(process.env, SIGN_IN_STARTED_EVENT, { chat_agent: signIn.detectAgent(process.env) });
|
|
753
849
|
if (!rest.includes("--json")) {
|
|
754
850
|
agentRun.writeLine(`Approve this computer in the browser: ${started.verification_uri_complete}`);
|
|
755
851
|
agentRun.writeLine(`The page shows the code ${started.user_code}. Then run: ${SIGN_IN_WAIT.replace(" --json", "")}`);
|
|
@@ -759,6 +855,21 @@ async function runSignIn(rest, startedMs) {
|
|
|
759
855
|
next: SIGN_IN_WAIT }, 0);
|
|
760
856
|
}
|
|
761
857
|
|
|
858
|
+
/**
|
|
859
|
+
* `sign-in --if-needed`: this computer's status first (install.md's old step 1, in one call). Set up: its
|
|
860
|
+
* answer, and no tab (D48). Refused: its answer, for its `reason`. Otherwise this returns and the sign-in starts.
|
|
861
|
+
*/
|
|
862
|
+
async function answerIfSetUp(deadlineMs) {
|
|
863
|
+
const ran = setupCheck.runSetupCheck({ launcher: __filename, deadlineMs: deadlineMs - WAIT_RESERVE_MS });
|
|
864
|
+
if (ran.check === setupCheck.SetupCheck.TIMED_OUT) {
|
|
865
|
+
await refuseAgent(AgentStep.SIGN_IN, agentRun.Reason.RUN_AGAIN,
|
|
866
|
+
"Stopped at the agent's time limit: run the same command again.", agentRun.EXIT_AGAIN);
|
|
867
|
+
}
|
|
868
|
+
if (ran.check === setupCheck.SetupCheck.NOT_SET_UP) return;
|
|
869
|
+
agentRun.writeLine(ran.stdout.trim());
|
|
870
|
+
process.exit(ran.code);
|
|
871
|
+
}
|
|
872
|
+
|
|
762
873
|
/** The downloaded `probe`, asked again while the budget lasts when the download finished (`done`). */
|
|
763
874
|
async function findDownloadedProbe(done, startedMs) {
|
|
764
875
|
const until = startedMs + agentRun.AGENT_BUDGET_MS - WAIT_RESERVE_MS;
|
|
@@ -772,33 +883,42 @@ async function findDownloadedProbe(done, startedMs) {
|
|
|
772
883
|
/** `sign-in --wait`: the download first (within the budget), then the CLI polls for the approval. */
|
|
773
884
|
async function waitForSignIn(json, startedMs) {
|
|
774
885
|
if (!signIn.loadPending()) {
|
|
775
|
-
|
|
886
|
+
await refuseAgent(AgentStep.SIGN_IN_WAIT, agentRun.Reason.SIGN_IN_EXPIRED,
|
|
776
887
|
"No sign-in is waiting (or it expired): run `npx -y probe-research sign-in --json` for a new one.", 1);
|
|
777
888
|
}
|
|
778
889
|
if (hasBarrier()) {
|
|
779
|
-
|
|
890
|
+
await refuseAgent(AgentStep.SIGN_IN_WAIT, agentRun.Reason.UNINSTALL_RUNNING,
|
|
780
891
|
"An uninstall of Probe stopped halfway: run `npx -y probe-research sign-in --json` again to finish it.",
|
|
781
892
|
agentRun.EXIT_AGAIN);
|
|
782
893
|
}
|
|
783
|
-
|
|
894
|
+
const probe = await getAgentProbe(startedMs, AgentStep.SIGN_IN_WAIT);
|
|
895
|
+
return runAgentCall(probe, ["wizard", "--action", "login", "--pending", ...(json ? ["--json"] : [])], startedMs,
|
|
896
|
+
AgentStep.SIGN_IN_WAIT);
|
|
897
|
+
}
|
|
898
|
+
|
|
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) or `incomplete` with the download's log. */
|
|
901
|
+
async function getAgentProbe(startedMs, step) {
|
|
902
|
+
const found = findProbe(AGENT_MIN_CLI);
|
|
903
|
+
if (found) return found;
|
|
904
|
+
const record = download.loadDownload();
|
|
905
|
+
// A finished download whose `probe` is gone (removed since) is downloaded again, never waited on.
|
|
906
|
+
const isGone = record && record.state === download.DownloadState.DONE && getProbeCommands().length === 0;
|
|
907
|
+
if (!record || record.state === download.DownloadState.FAILED || isGone) startBackgroundDownload();
|
|
908
|
+
const done = await waitForRunningDownload(startedMs, step);
|
|
909
|
+
const probe = await findDownloadedProbe(done, startedMs);
|
|
910
|
+
if (!probe && done && done.state === download.DownloadState.DONE) {
|
|
911
|
+
// Installed, but its `probe` did not answer yet: the Probe service it just started restarts
|
|
912
|
+
// once for its first update, and `probe --version` goes through it (PR3b dry run, 1 in 4).
|
|
913
|
+
await refuseAgent(step, agentRun.Reason.DOWNLOADING, "Probe is finishing its install: run the same command again.",
|
|
914
|
+
agentRun.EXIT_AGAIN);
|
|
915
|
+
}
|
|
784
916
|
if (!probe) {
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
probe = await findDownloadedProbe(done, startedMs);
|
|
789
|
-
if (!probe && done && done.state === download.DownloadState.DONE) {
|
|
790
|
-
// Installed, but its `probe` did not answer yet: the Probe service it just started restarts
|
|
791
|
-
// once for its first update, and `probe --version` goes through it (PR3b dry run, 1 in 4).
|
|
792
|
-
agentRun.refuse(agentRun.Reason.DOWNLOADING, "Probe is finishing its install: run the same command again.",
|
|
793
|
-
agentRun.EXIT_AGAIN);
|
|
794
|
-
}
|
|
795
|
-
if (!probe) {
|
|
796
|
-
agentRun.refuse(agentRun.Reason.INCOMPLETE,
|
|
797
|
-
`Probe did not download (${(done && done.error) || "no probe command after it"}); ` +
|
|
798
|
-
`its log: ${download.getFile(download.LOG_NAME)}`, 1);
|
|
799
|
-
}
|
|
917
|
+
await refuseAgent(step, agentRun.Reason.INCOMPLETE,
|
|
918
|
+
`Probe did not download (${(done && done.error) || "no probe command after it"}); ` +
|
|
919
|
+
`its log: ${download.getFile(download.LOG_NAME)}`, 1);
|
|
800
920
|
}
|
|
801
|
-
return
|
|
921
|
+
return probe;
|
|
802
922
|
}
|
|
803
923
|
|
|
804
924
|
/**
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sign-in --if-needed`: this computer's own status first, so a set-up computer opens no tab (D48) and the
|
|
3
|
+
* agent's first call is the sign-in itself, one model turn sooner (PR25: the link reached the chat 12.5-18.6 s
|
|
4
|
+
* after the paste, ~2.5 s of it the turn between `status` and `sign-in`).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const { spawnSync } = require("node:child_process");
|
|
8
|
+
|
|
9
|
+
/** What install.md's first step ran before: the same status, with the same flags. */
|
|
10
|
+
const STATUS_ARGS = Object.freeze(["status", "--json", "--refresh"]);
|
|
11
|
+
/** Status's answer is ~25 KB on a computer with every agent set up; past spawnSync's 1 MB default it would be cut. */
|
|
12
|
+
const MAX_ANSWER_BYTES = 16 * 1024 * 1024;
|
|
13
|
+
|
|
14
|
+
/** `setup.why`'s word for "no saved sign-in" (the CLI's `SetupGap.SIGNED_OUT`). */
|
|
15
|
+
const SIGNED_OUT_GAP = "signed_out";
|
|
16
|
+
|
|
17
|
+
/** What the status said about signing in here. */
|
|
18
|
+
const SetupCheck = Object.freeze({
|
|
19
|
+
/** Set up (`isSetUp`): answer the status itself, no tab. */
|
|
20
|
+
SET_UP: "set_up",
|
|
21
|
+
/** The status refused (`ok: false` with a `reason`): answer it, so the agent acts on its reason. */
|
|
22
|
+
REFUSED: "refused",
|
|
23
|
+
/** Anything else (none, partial signed out, unknown, an older CLI with no `setup`): sign in. */
|
|
24
|
+
NOT_SET_UP: "not_set_up",
|
|
25
|
+
/** The status did not answer before `deadlineMs`. */
|
|
26
|
+
TIMED_OUT: "timed_out",
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
/** install.md's rule for the re-paste menu: `done`, or `partial` still signed in (PR23b, P3: a plugin off). */
|
|
30
|
+
function isSetUp(setup) {
|
|
31
|
+
if (!setup || typeof setup !== "object") return false;
|
|
32
|
+
if (setup.state === "done") return true;
|
|
33
|
+
return setup.state === "partial" && !(Array.isArray(setup.why) && setup.why.includes(SIGNED_OUT_GAP));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The last stdout line as an object, or null. */
|
|
37
|
+
function getLastAnswer(stdout) {
|
|
38
|
+
const lines = String(stdout || "").trim().split("\n");
|
|
39
|
+
try {
|
|
40
|
+
const answer = JSON.parse(lines[lines.length - 1]);
|
|
41
|
+
return answer && typeof answer === "object" ? answer : null;
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Run the launcher's own `status --json --refresh` (whatever it does on this computer: answer `none` at once
|
|
49
|
+
* with no Probe here, or run the installed CLI's status) until `deadlineMs`: `{check, stdout, code}`.
|
|
50
|
+
*/
|
|
51
|
+
function runSetupCheck({ launcher, deadlineMs, env = process.env }) {
|
|
52
|
+
const ran = spawnSync(process.execPath, [launcher, ...STATUS_ARGS], {
|
|
53
|
+
encoding: "utf8",
|
|
54
|
+
env,
|
|
55
|
+
maxBuffer: MAX_ANSWER_BYTES,
|
|
56
|
+
stdio: ["ignore", "pipe", "inherit"],
|
|
57
|
+
timeout: Math.max(1000, deadlineMs - Date.now()),
|
|
58
|
+
});
|
|
59
|
+
if (ran.error || ran.signal) return { check: SetupCheck.TIMED_OUT, stdout: "", code: 1 };
|
|
60
|
+
const answer = getLastAnswer(ran.stdout);
|
|
61
|
+
const code = ran.status === null ? 1 : ran.status;
|
|
62
|
+
if (answer && isSetUp(answer.setup)) return { check: SetupCheck.SET_UP, stdout: ran.stdout, code };
|
|
63
|
+
if (answer && answer.ok === false && typeof answer.reason === "string") {
|
|
64
|
+
return { check: SetupCheck.REFUSED, stdout: ran.stdout, code };
|
|
65
|
+
}
|
|
66
|
+
return { check: SetupCheck.NOT_SET_UP, stdout: ran.stdout, code };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = { SIGNED_OUT_GAP, SetupCheck, isSetUp, runSetupCheck };
|
package/lib/sign-in.js
CHANGED
|
@@ -117,6 +117,18 @@ function getConfigFile(env) {
|
|
|
117
117
|
return path.join(env.XDG_CONFIG_HOME || path.join(os.homedir(), ".config"), "probe", "config.json");
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
+
/** The CLI config's active account (`settings.load_context`), or null. Callers read one field each. */
|
|
121
|
+
function loadActiveContext(env) {
|
|
122
|
+
try {
|
|
123
|
+
const data = JSON.parse(fs.readFileSync(getConfigFile(env), "utf8"));
|
|
124
|
+
const contexts = data && typeof data.contexts === "object" ? data.contexts : null;
|
|
125
|
+
const active = contexts ? contexts[data.current_context || "default"] : data;
|
|
126
|
+
return active && typeof active === "object" ? active : null;
|
|
127
|
+
} catch {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
120
132
|
/**
|
|
121
133
|
* The API this computer signs in to: `PROBE_BASE_URL`, else the saved account's, else Probe's.
|
|
122
134
|
* Never a key: only `base_url` is read from the config.
|
|
@@ -124,16 +136,18 @@ function getConfigFile(env) {
|
|
|
124
136
|
function getSignInBase(env) {
|
|
125
137
|
const fromEnv = env.PROBE_BASE_URL;
|
|
126
138
|
if (fromEnv && /^https?:\/\//.test(fromEnv)) return fromEnv.replace(/\/+$/, "");
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const active = contexts ? contexts[data.current_context || "default"] : data;
|
|
131
|
-
const saved = active && typeof active.base_url === "string" ? active.base_url : null;
|
|
132
|
-
if (saved && /^https?:\/\//.test(saved)) return saved.replace(/\/+$/, "");
|
|
133
|
-
} catch {}
|
|
139
|
+
const active = loadActiveContext(env);
|
|
140
|
+
const saved = active && typeof active.base_url === "string" ? active.base_url : null;
|
|
141
|
+
if (saved && /^https?:\/\//.test(saved)) return saved.replace(/\/+$/, "");
|
|
134
142
|
return DEFAULT_BASE;
|
|
135
143
|
}
|
|
136
144
|
|
|
145
|
+
/** Is a sign-in saved here (the active account holds a key)? Only whether: the key itself is never read out. */
|
|
146
|
+
function hasSavedSignIn(env) {
|
|
147
|
+
const active = loadActiveContext(env);
|
|
148
|
+
return Boolean(active && typeof active.token === "string" && active.token);
|
|
149
|
+
}
|
|
150
|
+
|
|
137
151
|
/** `content` written whole into `file`, readable by this user only. */
|
|
138
152
|
function savePrivate(file, content) {
|
|
139
153
|
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
@@ -236,6 +250,7 @@ module.exports = {
|
|
|
236
250
|
getSayLine,
|
|
237
251
|
getSignInBase,
|
|
238
252
|
getStateFolder,
|
|
253
|
+
hasSavedSignIn,
|
|
239
254
|
loadPending,
|
|
240
255
|
makePkcePair,
|
|
241
256
|
startSignIn,
|