probe-research 0.22.0 → 0.24.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 +121 -28
- package/lib/agent-run.js +9 -1
- package/lib/approval-watch.js +93 -0
- package/lib/background-download.js +13 -6
- package/lib/sign-in.js +51 -0
- package/package.json +1 -1
package/bin/probe-research.js
CHANGED
|
@@ -32,6 +32,7 @@ const os = require("node:os");
|
|
|
32
32
|
const path = require("node:path");
|
|
33
33
|
|
|
34
34
|
const agentRun = require("../lib/agent-run");
|
|
35
|
+
const approval = require("../lib/approval-watch");
|
|
35
36
|
const download = require("../lib/background-download");
|
|
36
37
|
const setupCheck = require("../lib/setup-check");
|
|
37
38
|
const signIn = require("../lib/sign-in");
|
|
@@ -135,8 +136,12 @@ const FIX_MIN = "0.385.0";
|
|
|
135
136
|
* cut before it merged.
|
|
136
137
|
* 0.432.0 since PR25: a set-up computer's agent calls run PR22's CLI (Recommended = on) and PR26's
|
|
137
138
|
* `update --json`; 0.431.0 was cut at PR22's merge, before PR26.
|
|
139
|
+
* 0.440.0 since PR34 (#4456): the re-paste menu's `status --repaste-choice settings|account`, its `--json`
|
|
140
|
+
* sign-out and agent removal (`isMenuChange`), and Change settings' `install` keeping this chat's own switch;
|
|
141
|
+
* 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
|
|
142
|
+
* floor never published).
|
|
138
143
|
*/
|
|
139
|
-
const AGENT_MIN_CLI = "0.
|
|
144
|
+
const AGENT_MIN_CLI = "0.440.0";
|
|
140
145
|
/** `install` flags only AGENT_MIN_CLI knows (D36). */
|
|
141
146
|
const AGENT_INSTALL_FLAGS = Object.freeze(["--recommended", "--who-records", "--session-default", "--profile",
|
|
142
147
|
"--attempt"]);
|
|
@@ -447,18 +452,19 @@ const SHIM_REASONS = Object.freeze({ 69: "no_service", 70: "service_lost" });
|
|
|
447
452
|
/** The step an agent's `probe` arguments are (`AgentStep`). */
|
|
448
453
|
function getAgentStep(forwarded) {
|
|
449
454
|
if (forwarded[0] === LauncherVerb.WIZARD) {
|
|
455
|
+
if (isMenuChange(forwarded)) return AgentStep.OTHER;
|
|
450
456
|
return forwarded.includes("--pending") ? AgentStep.SIGN_IN_WAIT : AgentStep.INSTALL;
|
|
451
457
|
}
|
|
452
458
|
return [AgentStep.STATUS, AgentStep.IMPORT, AgentStep.EXAMPLE].includes(forwarded[0]) ? forwarded[0] : AgentStep.OTHER;
|
|
453
459
|
}
|
|
454
460
|
|
|
455
|
-
/** One bounded `agent_install.refused` for `step`, then the refusal itself (`agentRun.refuse`)
|
|
456
|
-
async function refuseAgent(step, reason, message, code) {
|
|
461
|
+
/** One bounded `agent_install.refused` for `step`, then the refusal itself (`agentRun.refuse`), with `facts`. */
|
|
462
|
+
async function refuseAgent(step, reason, message, code, facts = {}) {
|
|
457
463
|
await sendLauncherEvent(process.env, REFUSED_EVENT, {
|
|
458
464
|
step, reason, outcome: code === agentRun.EXIT_AGAIN ? "run_again" : "failed",
|
|
459
465
|
chat_agent: signIn.detectAgent(process.env),
|
|
460
466
|
});
|
|
461
|
-
agentRun.refuse(reason, message, code);
|
|
467
|
+
agentRun.refuse(reason, message, code, facts);
|
|
462
468
|
}
|
|
463
469
|
|
|
464
470
|
/** The hidden word the launcher re-runs itself with, detached, to send one of these events after it exits. */
|
|
@@ -712,6 +718,12 @@ const NONE_SETUP = Object.freeze({
|
|
|
712
718
|
});
|
|
713
719
|
/** Kept back from the budget for the CLI's own work once the download is in. */
|
|
714
720
|
const WAIT_RESERVE_MS = 20 * 1000;
|
|
721
|
+
/**
|
|
722
|
+
* What an approved sign-in's CLI needs left once a download is in (PR39): its first start through a new
|
|
723
|
+
* service, then the exchange before its own save reserve (12 s) and margin (3 s). With less, it would
|
|
724
|
+
* answer `waiting_for_approval` though the person approved, so the agent runs it again instead.
|
|
725
|
+
*/
|
|
726
|
+
const APPROVED_RESERVE_MS = 30 * 1000;
|
|
715
727
|
/** How often a running download is looked at again. */
|
|
716
728
|
const DOWNLOAD_POLL_MS = 1000;
|
|
717
729
|
/** The uninstall barrier (`probe.shared.updates.uninstall_barrier.path`). */
|
|
@@ -722,13 +734,30 @@ const AGENT_STATUS_FLAGS = Object.freeze(["--repaste-choice", "--past-work-choic
|
|
|
722
734
|
/** `import` verbs only AGENT_MIN_CLI has: bringing in past work (D49, D74). `import wandb` is older. */
|
|
723
735
|
const AGENT_IMPORT_VERBS = Object.freeze(["folders", "review", "start", "status", "resume"]);
|
|
724
736
|
|
|
737
|
+
/** The wizard action a `probe wizard` call names (`--action X` or `--action=X`), or null. */
|
|
738
|
+
function getWizardAction(forwarded) {
|
|
739
|
+
for (let i = 1; i < forwarded.length; i++) {
|
|
740
|
+
if (forwarded[i] === "--action") return forwarded[i + 1] ?? null;
|
|
741
|
+
if (forwarded[i].startsWith("--action=")) return forwarded[i].slice("--action=".length);
|
|
742
|
+
}
|
|
743
|
+
return null;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/** The re-paste menu's sign-out and agent removal (PR34): answered as an agent's call (`done.reason`) only
|
|
747
|
+
* under `--json`, so only then does the agent floor run them. */
|
|
748
|
+
function isMenuChange(forwarded) {
|
|
749
|
+
if (forwarded[0] !== LauncherVerb.WIZARD || !forwarded.includes("--json")) return false;
|
|
750
|
+
const action = getWizardAction(forwarded);
|
|
751
|
+
return action === "logout" || (action === "uninstall" && forwarded.some((word) => word.split("=")[0] === "--agent"));
|
|
752
|
+
}
|
|
753
|
+
|
|
725
754
|
/** `probe` arguments only AGENT_MIN_CLI runs (D36). */
|
|
726
755
|
function needsAgentCli(forwarded) {
|
|
727
756
|
if (forwarded[0] === "example") return true;
|
|
728
757
|
if (forwarded[0] === "import") return AGENT_IMPORT_VERBS.includes(forwarded[1]);
|
|
729
758
|
if (forwarded[0] === "status") return forwarded.some((word) => AGENT_STATUS_FLAGS.includes(word.split("=")[0]));
|
|
730
759
|
if (forwarded[0] !== LauncherVerb.WIZARD) return false;
|
|
731
|
-
return forwarded.includes("--pending")
|
|
760
|
+
return forwarded.includes("--pending") || isMenuChange(forwarded)
|
|
732
761
|
|| forwarded.some((word) => AGENT_INSTALL_FLAGS.includes(word.split("=")[0]));
|
|
733
762
|
}
|
|
734
763
|
|
|
@@ -786,13 +815,23 @@ function startBackgroundDownload() {
|
|
|
786
815
|
return download.startDownload({ launcher: __filename, args: [] });
|
|
787
816
|
}
|
|
788
817
|
|
|
789
|
-
/**
|
|
818
|
+
/**
|
|
819
|
+
* The detached download itself: the newest CLI, installed for good by its own bootstrap. Two steps, so
|
|
820
|
+
* `sign-in --wait` can say which runs (PR39): uv fetches Python and the CLI into its env (running only
|
|
821
|
+
* Python there, no service), then the same env starts the CLI's first service. The first may fail on its
|
|
822
|
+
* own (an old uv that runs no `python` from a tool env): the second downloads too.
|
|
823
|
+
*/
|
|
790
824
|
async function runBackgroundDownload() {
|
|
791
825
|
const { pinned, spec, missingBuild } = await getLatestSpec();
|
|
792
826
|
const refresh = getRefreshFlags({ repair: false, missingBuild, pinned });
|
|
793
827
|
const uv = getUv();
|
|
794
|
-
process.exit(download.runDownload([
|
|
795
|
-
|
|
828
|
+
process.exit(download.runDownload([
|
|
829
|
+
{ stage: download.DownloadStage.DOWNLOADING, isOptional: true,
|
|
830
|
+
command: [uv, "tool", "run", "--python", PYTHON_REQUEST, ...refresh, "--from", spec, "python", "-c", ""] },
|
|
831
|
+
{ stage: download.DownloadStage.STARTING,
|
|
832
|
+
command: [uv, "tool", "run", "--python", PYTHON_REQUEST, ...refresh, "--from", spec, "probe", "service",
|
|
833
|
+
"start", "--min-version", pinned || AGENT_MIN_CLI, "--json"] },
|
|
834
|
+
]));
|
|
796
835
|
}
|
|
797
836
|
|
|
798
837
|
/** An uninstall stopped halfway is finished (`probe repair`) before the tab opens (D47). */
|
|
@@ -810,20 +849,53 @@ async function finishStoppedUninstall(deadlineMs) {
|
|
|
810
849
|
}
|
|
811
850
|
}
|
|
812
851
|
|
|
813
|
-
/** An agent's call (`step`) while the first download runs: wait within the budget, else `downloading` (75).
|
|
814
|
-
|
|
852
|
+
/** An agent's call (`step`) while the first download runs: wait within the budget, else `downloading` (75).
|
|
853
|
+
* `watch` (`sign-in --wait`'s approval, PR39) is read meanwhile and told each stage the download runs. */
|
|
854
|
+
async function waitForRunningDownload(startedMs, step, watch = null) {
|
|
815
855
|
const until = startedMs + agentRun.AGENT_BUDGET_MS - WAIT_RESERVE_MS;
|
|
816
856
|
for (;;) {
|
|
817
857
|
const record = download.loadDownload();
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
}
|
|
858
|
+
const isRunning = record && record.state === download.DownloadState.RUNNING;
|
|
859
|
+
await updateWatch(watch, step, isRunning ? record.stage || download.DownloadStage.DOWNLOADING : null);
|
|
860
|
+
if (!isRunning) return record;
|
|
861
|
+
if (Date.now() >= until) await refuseUnfinished(step, watch, record.stage || download.DownloadStage.DOWNLOADING);
|
|
823
862
|
await sleep(DOWNLOAD_POLL_MS);
|
|
824
863
|
}
|
|
825
864
|
}
|
|
826
865
|
|
|
866
|
+
/** Read `watch` and report `stage` (none without a watch); a denied or expired sign-in stops here (PR39). */
|
|
867
|
+
async function updateWatch(watch, step, stage) {
|
|
868
|
+
if (!watch) return;
|
|
869
|
+
await watch.update(stage);
|
|
870
|
+
const again = "run `npx -y probe-research sign-in --json` for a new one.";
|
|
871
|
+
if (watch.status === signIn.ApprovalStatus.DENIED) {
|
|
872
|
+
signIn.clearPending();
|
|
873
|
+
await refuseAgent(step, agentRun.Reason.INCOMPLETE, `The sign-in was denied in the browser: ${again}`, 1);
|
|
874
|
+
}
|
|
875
|
+
if (watch.status === signIn.ApprovalStatus.EXPIRED) {
|
|
876
|
+
signIn.clearPending();
|
|
877
|
+
await refuseAgent(step, agentRun.Reason.SIGN_IN_EXPIRED, `The sign-in link expired: ${again}`, 1);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
/**
|
|
882
|
+
* Out of time while Probe installs (75): what is true now (PR39). Approved: `downloading` with
|
|
883
|
+
* `approved`, who and the stage's `say`, never "waiting for approval". Not approved yet: waiting for it.
|
|
884
|
+
* Unknown (no watch, an older server): `message`.
|
|
885
|
+
*/
|
|
886
|
+
async function refuseUnfinished(step, watch, stage, message = "Probe is still downloading: run the same command again.") {
|
|
887
|
+
if (watch && watch.isApproved()) {
|
|
888
|
+
await refuseAgent(step, agentRun.Reason.DOWNLOADING, "Probe is still installing: run the same command again.",
|
|
889
|
+
agentRun.EXIT_AGAIN, watch.getRunAgainFacts(stage));
|
|
890
|
+
}
|
|
891
|
+
if (watch && watch.isPending()) {
|
|
892
|
+
await refuseAgent(step, agentRun.Reason.WAITING_FOR_APPROVAL,
|
|
893
|
+
"Waiting for the browser approval while Probe installs: run this again.", agentRun.EXIT_AGAIN,
|
|
894
|
+
watch.getRunAgainFacts(stage));
|
|
895
|
+
}
|
|
896
|
+
await refuseAgent(step, agentRun.Reason.DOWNLOADING, message, agentRun.EXIT_AGAIN);
|
|
897
|
+
}
|
|
898
|
+
|
|
827
899
|
/** `sign-in`: the tab within seconds, the download behind it; `sign-in --wait`: the CLI finishes it. */
|
|
828
900
|
async function runSignIn(rest, startedMs) {
|
|
829
901
|
const deadlineMs = startedMs + agentRun.AGENT_BUDGET_MS;
|
|
@@ -870,19 +942,26 @@ async function answerIfSetUp(deadlineMs) {
|
|
|
870
942
|
process.exit(ran.code);
|
|
871
943
|
}
|
|
872
944
|
|
|
873
|
-
/** The downloaded `probe`, asked again while the budget lasts when the download finished (`done`)
|
|
874
|
-
|
|
945
|
+
/** The downloaded `probe`, asked again while the budget lasts when the download finished (`done`); its
|
|
946
|
+
* first service restarts meanwhile, which `watch` reports as still starting (PR39). */
|
|
947
|
+
async function findDownloadedProbe(done, startedMs, step, watch = null) {
|
|
875
948
|
const until = startedMs + agentRun.AGENT_BUDGET_MS - WAIT_RESERVE_MS;
|
|
876
949
|
for (;;) {
|
|
877
950
|
const probe = findProbe(AGENT_MIN_CLI);
|
|
878
951
|
if (probe || !done || done.state !== download.DownloadState.DONE || Date.now() >= until) return probe;
|
|
952
|
+
await updateWatch(watch, step, approval.InstallStage.STARTING);
|
|
879
953
|
await sleep(DOWNLOAD_POLL_MS);
|
|
880
954
|
}
|
|
881
955
|
}
|
|
882
956
|
|
|
883
|
-
/**
|
|
957
|
+
/**
|
|
958
|
+
* `sign-in --wait`: the CLI polls for the approval and saves it. With no `probe` yet, the download first
|
|
959
|
+
* (within the budget), while the approval is read from the server and each install stage is said once the
|
|
960
|
+
* person approved (PR39).
|
|
961
|
+
*/
|
|
884
962
|
async function waitForSignIn(json, startedMs) {
|
|
885
|
-
|
|
963
|
+
const pending = signIn.loadPending();
|
|
964
|
+
if (!pending) {
|
|
886
965
|
await refuseAgent(AgentStep.SIGN_IN_WAIT, agentRun.Reason.SIGN_IN_EXPIRED,
|
|
887
966
|
"No sign-in is waiting (or it expired): run `npx -y probe-research sign-in --json` for a new one.", 1);
|
|
888
967
|
}
|
|
@@ -891,29 +970,43 @@ async function waitForSignIn(json, startedMs) {
|
|
|
891
970
|
"An uninstall of Probe stopped halfway: run `npx -y probe-research sign-in --json` again to finish it.",
|
|
892
971
|
agentRun.EXIT_AGAIN);
|
|
893
972
|
}
|
|
894
|
-
const
|
|
973
|
+
const watch = new approval.ApprovalWatch(pending);
|
|
974
|
+
const probe = await getAgentProbe(startedMs, AgentStep.SIGN_IN_WAIT, watch);
|
|
975
|
+
// Read only while it waited for the install: a `probe` already here is handed the sign-in at once.
|
|
976
|
+
if (watch.hasStarted()) await sayInstalled(watch, startedMs);
|
|
895
977
|
return runAgentCall(probe, ["wizard", "--action", "login", "--pending", ...(json ? ["--json"] : [])], startedMs,
|
|
896
978
|
AgentStep.SIGN_IN_WAIT);
|
|
897
979
|
}
|
|
898
980
|
|
|
981
|
+
/** Probe installed during the wait (PR39): approved, say so, and hand the CLI the save only with time for it. */
|
|
982
|
+
async function sayInstalled(watch, startedMs) {
|
|
983
|
+
await updateWatch(watch, AgentStep.SIGN_IN_WAIT, approval.InstallStage.DONE);
|
|
984
|
+
if (!watch.isApproved() || startedMs + agentRun.AGENT_BUDGET_MS - Date.now() >= APPROVED_RESERVE_MS) return;
|
|
985
|
+
await refuseAgent(AgentStep.SIGN_IN_WAIT, agentRun.Reason.RUN_AGAIN,
|
|
986
|
+
"Probe is installed, with too little time left to save the sign-in: run the same command again.",
|
|
987
|
+
agentRun.EXIT_AGAIN, watch.getRunAgainFacts(approval.InstallStage.DONE));
|
|
988
|
+
}
|
|
989
|
+
|
|
899
990
|
/** A `probe` at the agent floor: the one here, else the first download's (started if none runs), waited
|
|
900
991
|
* 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.
|
|
902
|
-
|
|
992
|
+
* ended on one (`no_service`, `service_lost`, `updating`), or `incomplete` with the download's log.
|
|
993
|
+
* `watch`: `sign-in --wait`'s approval, read meanwhile (PR39); its facts ride every refusal. */
|
|
994
|
+
async function getAgentProbe(startedMs, step, watch = null) {
|
|
903
995
|
const found = findProbe(AGENT_MIN_CLI);
|
|
904
996
|
if (found) return found;
|
|
905
997
|
const record = download.loadDownload();
|
|
906
998
|
// A finished download whose `probe` is gone (removed since) is downloaded again, never waited on.
|
|
907
999
|
const isGone = record && record.state === download.DownloadState.DONE && getProbeCommands().length === 0;
|
|
908
1000
|
if (!record || record.state === download.DownloadState.FAILED || isGone) startBackgroundDownload();
|
|
909
|
-
const done = await waitForRunningDownload(startedMs, step);
|
|
910
|
-
const probe = await findDownloadedProbe(done, startedMs);
|
|
1001
|
+
const done = await waitForRunningDownload(startedMs, step, watch);
|
|
1002
|
+
const probe = await findDownloadedProbe(done, startedMs, step, watch);
|
|
911
1003
|
if (!probe && done && done.state === download.DownloadState.DONE) {
|
|
912
1004
|
// Installed, but its `probe` did not answer yet: the Probe service it just started restarts
|
|
913
1005
|
// once for its first update, and `probe --version` goes through it (PR3b dry run, 1 in 4).
|
|
914
|
-
await
|
|
915
|
-
|
|
1006
|
+
await refuseUnfinished(step, watch, approval.InstallStage.STARTING,
|
|
1007
|
+
"Probe is finishing its install: run the same command again.");
|
|
916
1008
|
}
|
|
1009
|
+
const facts = watch ? watch.getFacts() : {};
|
|
917
1010
|
const serviceExit = !probe && done && done.state === download.DownloadState.FAILED
|
|
918
1011
|
? download.getServiceExit(done.reason) : null;
|
|
919
1012
|
if (serviceExit !== null) {
|
|
@@ -922,12 +1015,12 @@ async function getAgentProbe(startedMs, step) {
|
|
|
922
1015
|
await refuseAgent(step, done.reason, serviceExit === agentRun.EXIT_AGAIN
|
|
923
1016
|
? `Probe is finishing an update (${done.error}): run the same command again.`
|
|
924
1017
|
: `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);
|
|
1018
|
+
`its log: ${download.getFile(download.LOG_NAME)}`, serviceExit, facts);
|
|
926
1019
|
}
|
|
927
1020
|
if (!probe) {
|
|
928
1021
|
await refuseAgent(step, agentRun.Reason.INCOMPLETE,
|
|
929
1022
|
`Probe did not download (${(done && done.error) || "no probe command after it"}); ` +
|
|
930
|
-
`its log: ${download.getFile(download.LOG_NAME)}`, 1);
|
|
1023
|
+
`its log: ${download.getFile(download.LOG_NAME)}`, 1, facts);
|
|
931
1024
|
}
|
|
932
1025
|
return probe;
|
|
933
1026
|
}
|
package/lib/agent-run.js
CHANGED
|
@@ -14,6 +14,8 @@ const { spawn } = require("node:child_process");
|
|
|
14
14
|
|
|
15
15
|
/** The whole command's time, the download wait included. */
|
|
16
16
|
const AGENT_BUDGET_MS = 110 * 1000;
|
|
17
|
+
/** Test-only: a shorter budget in seconds (never a longer one), so the run-again answers test in seconds. */
|
|
18
|
+
const TEST_BUDGET_ENV = "PROBE_LAUNCHER_TEST_BUDGET_S";
|
|
17
19
|
/** After SIGTERM, this long before SIGKILL. */
|
|
18
20
|
const KILL_GRACE_MS = 5 * 1000;
|
|
19
21
|
/** The variable the CLI reads its deadline from (epoch seconds; `agent_install.budget.DEADLINE_ENV`). */
|
|
@@ -96,8 +98,14 @@ function runWithBackstop(cmd, args, { deadlineMs, env = process.env } = {}) {
|
|
|
96
98
|
});
|
|
97
99
|
}
|
|
98
100
|
|
|
101
|
+
/** The budget this run has: AGENT_BUDGET_MS, or a shorter test one (TEST_BUDGET_ENV). */
|
|
102
|
+
function getBudgetMs(env) {
|
|
103
|
+
const seconds = Number(env[TEST_BUDGET_ENV]);
|
|
104
|
+
return seconds > 0 && seconds * 1000 < AGENT_BUDGET_MS ? seconds * 1000 : AGENT_BUDGET_MS;
|
|
105
|
+
}
|
|
106
|
+
|
|
99
107
|
module.exports = {
|
|
100
|
-
AGENT_BUDGET_MS,
|
|
108
|
+
AGENT_BUDGET_MS: getBudgetMs(process.env),
|
|
101
109
|
CLI_MARGIN_MS,
|
|
102
110
|
DEADLINE_ENV,
|
|
103
111
|
EXIT_AGAIN,
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sign-in --wait` while Probe installs (PR39): the approval read from the server at once and every few
|
|
3
|
+
* seconds, and once it is in, one JSON line per install stage as it happens, each with the `say` the agent
|
|
4
|
+
* shows. Richard's fresh Mac approved at once and heard "waiting for approval" for minutes: the old wait
|
|
5
|
+
* read the approval only through the CLI, after the download and its first service start.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const { writeLine } = require("./agent-run");
|
|
9
|
+
const { DownloadStage } = require("./background-download");
|
|
10
|
+
const { ApprovalStatus, fetchApproval } = require("./sign-in");
|
|
11
|
+
|
|
12
|
+
/** Where Probe's install on this computer is: the download's stages, then done (its `probe` answers). */
|
|
13
|
+
const InstallStage = Object.freeze({ ...DownloadStage, DONE: "done" });
|
|
14
|
+
/** Each stage as the person reads it (Richard's words, 2026-11-07). */
|
|
15
|
+
const STAGE_WORDS = Object.freeze({
|
|
16
|
+
[InstallStage.DOWNLOADING]: "Probe is still installing on this computer: downloading.",
|
|
17
|
+
[InstallStage.STARTING]: "Probe is still installing on this computer: starting its service for the first time.",
|
|
18
|
+
[InstallStage.DONE]: "Probe is installed on this computer: done.",
|
|
19
|
+
});
|
|
20
|
+
/** A progress line's `event`: it is never the answer, which stays the last line (and has `ok`). */
|
|
21
|
+
const PROGRESS_EVENT = "progress";
|
|
22
|
+
/** The shortest gap between two reads; the server's poll interval when longer. */
|
|
23
|
+
const MIN_READ_GAP_MS = 2000;
|
|
24
|
+
/** Answers that never change: not asked again. */
|
|
25
|
+
const SETTLED = Object.freeze([ApprovalStatus.APPROVED, ApprovalStatus.DENIED, ApprovalStatus.EXPIRED,
|
|
26
|
+
ApprovalStatus.ONBOARDING_REQUIRED, ApprovalStatus.UNREADABLE]);
|
|
27
|
+
|
|
28
|
+
/** The line the agent shows once the person approved: who, and where the install is. */
|
|
29
|
+
function getApprovedSay(account, stage) {
|
|
30
|
+
return `You're approved${account ? ` (${account})` : ""}. ${STAGE_WORDS[stage]}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The line the agent shows while the approval has not come: the tab to approve in. */
|
|
34
|
+
function getWaitingSay(pending) {
|
|
35
|
+
const link = pending.verification_uri_complete ? `: ${pending.verification_uri_complete}` : "";
|
|
36
|
+
return `Waiting for your approval in the browser${link}${pending.user_code ? ` (code ${pending.user_code})` : ""}.`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
class ApprovalWatch {
|
|
40
|
+
/** `pending`: the sign-in `startSignIn` left. `write` takes each progress line (stdout by default). */
|
|
41
|
+
constructor(pending, { fetchImpl = fetch, write = writeLine, now = Date.now } = {}) {
|
|
42
|
+
this.pending = pending;
|
|
43
|
+
this.fetchImpl = fetchImpl;
|
|
44
|
+
this.write = write;
|
|
45
|
+
this.now = now;
|
|
46
|
+
this.status = null;
|
|
47
|
+
this.account = null;
|
|
48
|
+
this.readGapMs = Math.max(MIN_READ_GAP_MS, (Number(pending.interval) || 0) * 1000);
|
|
49
|
+
this.nextReadMs = 0;
|
|
50
|
+
this.reportedStage = null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Read the approval when a read is due, then, once approved, report `stage` (InstallStage) if it is new. */
|
|
54
|
+
async update(stage = null) {
|
|
55
|
+
if (!SETTLED.includes(this.status) && this.now() >= this.nextReadMs) {
|
|
56
|
+
this.nextReadMs = this.now() + this.readGapMs;
|
|
57
|
+
const read = await fetchApproval(this.pending, { fetchImpl: this.fetchImpl });
|
|
58
|
+
if (read) ({ status: this.status, account: this.account } = read);
|
|
59
|
+
}
|
|
60
|
+
if (this.isApproved() && stage && stage !== this.reportedStage) {
|
|
61
|
+
this.reportedStage = stage;
|
|
62
|
+
this.write(JSON.stringify({ event: PROGRESS_EVENT, say: getApprovedSay(this.account, stage), approved: true,
|
|
63
|
+
account: this.account, stage }));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Whether the wait read the approval at all (it does only while Probe installs). */
|
|
68
|
+
hasStarted() {
|
|
69
|
+
return this.nextReadMs > 0;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
isApproved() {
|
|
73
|
+
return this.status === ApprovalStatus.APPROVED;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
isPending() {
|
|
77
|
+
return this.status === ApprovalStatus.PENDING;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** What any answer adds once the approval is known: `approved`, and who approved. */
|
|
81
|
+
getFacts() {
|
|
82
|
+
if (this.isApproved()) return { approved: true, account: this.account };
|
|
83
|
+
return this.isPending() ? { approved: false } : {};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** What a run-again answer adds: the facts and the line to show, approved at `stage` or still waiting. */
|
|
87
|
+
getRunAgainFacts(stage) {
|
|
88
|
+
if (this.isApproved()) return { say: getApprovedSay(this.account, stage), ...this.getFacts(), stage };
|
|
89
|
+
return this.isPending() ? { say: getWaitingSay(this.pending), ...this.getFacts() } : {};
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = { ApprovalWatch, InstallStage, PROGRESS_EVENT, getApprovedSay };
|
|
@@ -27,6 +27,9 @@ const DOWNLOAD_CAP_MS = 10 * 60 * 1000;
|
|
|
27
27
|
const DOWNLOAD_VERB = "--probe-background-download";
|
|
28
28
|
|
|
29
29
|
const DownloadState = Object.freeze({ RUNNING: "running", DONE: "done", FAILED: "failed" });
|
|
30
|
+
/** A running download's step (`stage` on its record, PR39): uv fetching Python and the CLI, then the CLI's
|
|
31
|
+
* first Probe service starting. `sign-in --wait` tells the person which, once they approved. */
|
|
32
|
+
const DownloadStage = Object.freeze({ DOWNLOADING: "downloading", STARTING: "starting" });
|
|
30
33
|
/**
|
|
31
34
|
* A step's exit that is the Probe service's answer, not the download's (PR29): the `probe` shim's own
|
|
32
35
|
* (`ShimReason.BY_EXIT`: no service answered, it went away mid-command, an update held it) and
|
|
@@ -137,8 +140,9 @@ function startDownload({ launcher, args, env = process.env }) {
|
|
|
137
140
|
}
|
|
138
141
|
|
|
139
142
|
/**
|
|
140
|
-
* The detached process's work: each step (`[cmd, ...args]`) in turn within
|
|
141
|
-
* output in the log, its end in the record.
|
|
143
|
+
* The detached process's work: each step (`{stage, command: [cmd, ...args], isOptional}`) in turn within
|
|
144
|
+
* DOWNLOAD_CAP_MS, its stage on the record while it runs, its output in the log, its end in the record.
|
|
145
|
+
* An optional step that fails is passed over (the next one does its work too). Returns the exit status.
|
|
142
146
|
*/
|
|
143
147
|
function runDownload(steps) {
|
|
144
148
|
if (!takeLock()) {
|
|
@@ -150,13 +154,15 @@ function runDownload(steps) {
|
|
|
150
154
|
const started = Date.now();
|
|
151
155
|
saveDownload({ state: DownloadState.RUNNING, pid: process.pid, started_at: started / 1000 });
|
|
152
156
|
try {
|
|
153
|
-
for (const
|
|
154
|
-
|
|
157
|
+
for (const { stage, command, isOptional = false } of steps) {
|
|
158
|
+
saveDownload({ state: DownloadState.RUNNING, stage, pid: process.pid, started_at: started / 1000 });
|
|
159
|
+
let { ran, logged } = runStep(command, started);
|
|
160
|
+
if (isOptional) continue;
|
|
155
161
|
// Once more with a fresh deadline when its service never answered: uv's own download may have used up
|
|
156
162
|
// 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(
|
|
163
|
+
if (ran && !ran.error && RETRIED_EXITS.includes(ran.status)) ({ ran, logged } = runStep(command, started));
|
|
158
164
|
if (!ran || ran.error || ran.status !== 0) {
|
|
159
|
-
saveDownload({ state: DownloadState.FAILED, pid: process.pid, ...getFailure(
|
|
165
|
+
saveDownload({ state: DownloadState.FAILED, pid: process.pid, ...getFailure(command[0], ran, logged),
|
|
160
166
|
finished_at: Date.now() / 1000 });
|
|
161
167
|
return 1;
|
|
162
168
|
}
|
|
@@ -235,6 +241,7 @@ function loadStepMessage(offset) {
|
|
|
235
241
|
module.exports = {
|
|
236
242
|
DOWNLOAD_CAP_MS,
|
|
237
243
|
DOWNLOAD_VERB,
|
|
244
|
+
DownloadStage,
|
|
238
245
|
DownloadState,
|
|
239
246
|
LOG_NAME,
|
|
240
247
|
SERVICE_EXITS,
|
package/lib/sign-in.js
CHANGED
|
@@ -21,6 +21,20 @@ const STATE_FOLDER = [".local", "state", "probe", "agent-install"];
|
|
|
21
21
|
const PENDING_NAME = "sign-in.json";
|
|
22
22
|
const PENDING_VERSION = 1;
|
|
23
23
|
const START_PATH = "/auth/device/code";
|
|
24
|
+
/** Where the waiting sign-in stands, read without exchanging it (PR39). */
|
|
25
|
+
const STATUS_PATH = "/auth/device/status";
|
|
26
|
+
/** The server's answers (`DeviceAuthorizationStatusOut.status`), and UNREADABLE: this server cannot say
|
|
27
|
+
* (an older one, no such route) or never will for this request, so it is not asked again. */
|
|
28
|
+
const ApprovalStatus = Object.freeze({
|
|
29
|
+
PENDING: "pending",
|
|
30
|
+
APPROVED: "approved",
|
|
31
|
+
DENIED: "denied",
|
|
32
|
+
EXPIRED: "expired",
|
|
33
|
+
ONBOARDING_REQUIRED: "onboarding_required",
|
|
34
|
+
UNREADABLE: "unreadable",
|
|
35
|
+
});
|
|
36
|
+
/** How long one approval read may take: the wait goes on without it. */
|
|
37
|
+
const APPROVAL_TIMEOUT_MS = 3000;
|
|
24
38
|
/** The request, as the CLI's `AGENT_CLIENT_CONTEXT` / `AGENT_GRANTS` spell it. */
|
|
25
39
|
const AGENT_CLIENT_CONTEXT = Object.freeze({ install: true, surface: "agent" });
|
|
26
40
|
const AGENT_GRANTS = Object.freeze(["api", "mcp"]);
|
|
@@ -235,17 +249,54 @@ function loadPending(now = Date.now() / 1000) {
|
|
|
235
249
|
}
|
|
236
250
|
}
|
|
237
251
|
|
|
252
|
+
/** The request `startSignIn` left, removed: a denied or expired one is good for nothing (as the CLI clears it). */
|
|
253
|
+
function clearPending() {
|
|
254
|
+
try {
|
|
255
|
+
fs.unlinkSync(path.join(getStateFolder(), PENDING_NAME));
|
|
256
|
+
} catch {}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Where `pending` stands (`POST /auth/device/status`, PR39): `{status, account}` (`ApprovalStatus`; `account`
|
|
261
|
+
* is the approver's email), or null when it could not be asked this time (offline, slow, a 5xx). It exchanges
|
|
262
|
+
* nothing, so the CLI still mints and saves the keys; the secret goes only to the server it came from.
|
|
263
|
+
*/
|
|
264
|
+
async function fetchApproval(pending, { fetchImpl = fetch, timeoutMs = APPROVAL_TIMEOUT_MS } = {}) {
|
|
265
|
+
const unreadable = { status: ApprovalStatus.UNREADABLE, account: null };
|
|
266
|
+
const { base_url: base, device_code: deviceCode, code_verifier: verifier } = pending || {};
|
|
267
|
+
if (![base, deviceCode, verifier].every((value) => typeof value === "string" && value)) return unreadable;
|
|
268
|
+
try {
|
|
269
|
+
const response = await fetchImpl(`${base}${STATUS_PATH}`, {
|
|
270
|
+
method: "POST",
|
|
271
|
+
headers: { "content-type": "application/json", accept: "application/json" },
|
|
272
|
+
body: JSON.stringify({ device_code: deviceCode, code_verifier: verifier }),
|
|
273
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
274
|
+
});
|
|
275
|
+
if (response.status === 429 || response.status >= 500) return null;
|
|
276
|
+
const data = response.ok ? await response.json().catch(() => null) : null;
|
|
277
|
+
const status = data && data.status;
|
|
278
|
+
if (status === ApprovalStatus.UNREADABLE || !Object.values(ApprovalStatus).includes(status)) return unreadable;
|
|
279
|
+
return { status, account: typeof data.account === "string" && data.account ? data.account : null };
|
|
280
|
+
} catch {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
238
285
|
module.exports = {
|
|
239
286
|
AGENT_CLIENT_CONTEXT,
|
|
240
287
|
AGENT_MARKERS,
|
|
241
288
|
AGENT_GRANTS,
|
|
289
|
+
ApprovalStatus,
|
|
242
290
|
PENDING_NAME,
|
|
243
291
|
STATE_FOLDER,
|
|
244
292
|
START_PATH,
|
|
293
|
+
STATUS_PATH,
|
|
245
294
|
TOKEN_NAME_PREFIX,
|
|
246
295
|
HOSTED_AGENTS,
|
|
247
296
|
buildRequestBody,
|
|
297
|
+
clearPending,
|
|
248
298
|
detectAgent,
|
|
299
|
+
fetchApproval,
|
|
249
300
|
findHostedAgent,
|
|
250
301
|
getSayLine,
|
|
251
302
|
getSignInBase,
|