probe-research 0.23.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 +97 -26
- 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");
|
|
@@ -457,13 +458,13 @@ function getAgentStep(forwarded) {
|
|
|
457
458
|
return [AgentStep.STATUS, AgentStep.IMPORT, AgentStep.EXAMPLE].includes(forwarded[0]) ? forwarded[0] : AgentStep.OTHER;
|
|
458
459
|
}
|
|
459
460
|
|
|
460
|
-
/** One bounded `agent_install.refused` for `step`, then the refusal itself (`agentRun.refuse`)
|
|
461
|
-
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 = {}) {
|
|
462
463
|
await sendLauncherEvent(process.env, REFUSED_EVENT, {
|
|
463
464
|
step, reason, outcome: code === agentRun.EXIT_AGAIN ? "run_again" : "failed",
|
|
464
465
|
chat_agent: signIn.detectAgent(process.env),
|
|
465
466
|
});
|
|
466
|
-
agentRun.refuse(reason, message, code);
|
|
467
|
+
agentRun.refuse(reason, message, code, facts);
|
|
467
468
|
}
|
|
468
469
|
|
|
469
470
|
/** The hidden word the launcher re-runs itself with, detached, to send one of these events after it exits. */
|
|
@@ -717,6 +718,12 @@ const NONE_SETUP = Object.freeze({
|
|
|
717
718
|
});
|
|
718
719
|
/** Kept back from the budget for the CLI's own work once the download is in. */
|
|
719
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;
|
|
720
727
|
/** How often a running download is looked at again. */
|
|
721
728
|
const DOWNLOAD_POLL_MS = 1000;
|
|
722
729
|
/** The uninstall barrier (`probe.shared.updates.uninstall_barrier.path`). */
|
|
@@ -808,13 +815,23 @@ function startBackgroundDownload() {
|
|
|
808
815
|
return download.startDownload({ launcher: __filename, args: [] });
|
|
809
816
|
}
|
|
810
817
|
|
|
811
|
-
/**
|
|
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
|
+
*/
|
|
812
824
|
async function runBackgroundDownload() {
|
|
813
825
|
const { pinned, spec, missingBuild } = await getLatestSpec();
|
|
814
826
|
const refresh = getRefreshFlags({ repair: false, missingBuild, pinned });
|
|
815
827
|
const uv = getUv();
|
|
816
|
-
process.exit(download.runDownload([
|
|
817
|
-
|
|
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
|
+
]));
|
|
818
835
|
}
|
|
819
836
|
|
|
820
837
|
/** An uninstall stopped halfway is finished (`probe repair`) before the tab opens (D47). */
|
|
@@ -832,20 +849,53 @@ async function finishStoppedUninstall(deadlineMs) {
|
|
|
832
849
|
}
|
|
833
850
|
}
|
|
834
851
|
|
|
835
|
-
/** An agent's call (`step`) while the first download runs: wait within the budget, else `downloading` (75).
|
|
836
|
-
|
|
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) {
|
|
837
855
|
const until = startedMs + agentRun.AGENT_BUDGET_MS - WAIT_RESERVE_MS;
|
|
838
856
|
for (;;) {
|
|
839
857
|
const record = download.loadDownload();
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
}
|
|
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);
|
|
845
862
|
await sleep(DOWNLOAD_POLL_MS);
|
|
846
863
|
}
|
|
847
864
|
}
|
|
848
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
|
+
|
|
849
899
|
/** `sign-in`: the tab within seconds, the download behind it; `sign-in --wait`: the CLI finishes it. */
|
|
850
900
|
async function runSignIn(rest, startedMs) {
|
|
851
901
|
const deadlineMs = startedMs + agentRun.AGENT_BUDGET_MS;
|
|
@@ -892,19 +942,26 @@ async function answerIfSetUp(deadlineMs) {
|
|
|
892
942
|
process.exit(ran.code);
|
|
893
943
|
}
|
|
894
944
|
|
|
895
|
-
/** The downloaded `probe`, asked again while the budget lasts when the download finished (`done`)
|
|
896
|
-
|
|
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) {
|
|
897
948
|
const until = startedMs + agentRun.AGENT_BUDGET_MS - WAIT_RESERVE_MS;
|
|
898
949
|
for (;;) {
|
|
899
950
|
const probe = findProbe(AGENT_MIN_CLI);
|
|
900
951
|
if (probe || !done || done.state !== download.DownloadState.DONE || Date.now() >= until) return probe;
|
|
952
|
+
await updateWatch(watch, step, approval.InstallStage.STARTING);
|
|
901
953
|
await sleep(DOWNLOAD_POLL_MS);
|
|
902
954
|
}
|
|
903
955
|
}
|
|
904
956
|
|
|
905
|
-
/**
|
|
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
|
+
*/
|
|
906
962
|
async function waitForSignIn(json, startedMs) {
|
|
907
|
-
|
|
963
|
+
const pending = signIn.loadPending();
|
|
964
|
+
if (!pending) {
|
|
908
965
|
await refuseAgent(AgentStep.SIGN_IN_WAIT, agentRun.Reason.SIGN_IN_EXPIRED,
|
|
909
966
|
"No sign-in is waiting (or it expired): run `npx -y probe-research sign-in --json` for a new one.", 1);
|
|
910
967
|
}
|
|
@@ -913,29 +970,43 @@ async function waitForSignIn(json, startedMs) {
|
|
|
913
970
|
"An uninstall of Probe stopped halfway: run `npx -y probe-research sign-in --json` again to finish it.",
|
|
914
971
|
agentRun.EXIT_AGAIN);
|
|
915
972
|
}
|
|
916
|
-
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);
|
|
917
977
|
return runAgentCall(probe, ["wizard", "--action", "login", "--pending", ...(json ? ["--json"] : [])], startedMs,
|
|
918
978
|
AgentStep.SIGN_IN_WAIT);
|
|
919
979
|
}
|
|
920
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
|
+
|
|
921
990
|
/** A `probe` at the agent floor: the one here, else the first download's (started if none runs), waited
|
|
922
991
|
* 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.
|
|
924
|
-
|
|
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) {
|
|
925
995
|
const found = findProbe(AGENT_MIN_CLI);
|
|
926
996
|
if (found) return found;
|
|
927
997
|
const record = download.loadDownload();
|
|
928
998
|
// A finished download whose `probe` is gone (removed since) is downloaded again, never waited on.
|
|
929
999
|
const isGone = record && record.state === download.DownloadState.DONE && getProbeCommands().length === 0;
|
|
930
1000
|
if (!record || record.state === download.DownloadState.FAILED || isGone) startBackgroundDownload();
|
|
931
|
-
const done = await waitForRunningDownload(startedMs, step);
|
|
932
|
-
const probe = await findDownloadedProbe(done, startedMs);
|
|
1001
|
+
const done = await waitForRunningDownload(startedMs, step, watch);
|
|
1002
|
+
const probe = await findDownloadedProbe(done, startedMs, step, watch);
|
|
933
1003
|
if (!probe && done && done.state === download.DownloadState.DONE) {
|
|
934
1004
|
// Installed, but its `probe` did not answer yet: the Probe service it just started restarts
|
|
935
1005
|
// once for its first update, and `probe --version` goes through it (PR3b dry run, 1 in 4).
|
|
936
|
-
await
|
|
937
|
-
|
|
1006
|
+
await refuseUnfinished(step, watch, approval.InstallStage.STARTING,
|
|
1007
|
+
"Probe is finishing its install: run the same command again.");
|
|
938
1008
|
}
|
|
1009
|
+
const facts = watch ? watch.getFacts() : {};
|
|
939
1010
|
const serviceExit = !probe && done && done.state === download.DownloadState.FAILED
|
|
940
1011
|
? download.getServiceExit(done.reason) : null;
|
|
941
1012
|
if (serviceExit !== null) {
|
|
@@ -944,12 +1015,12 @@ async function getAgentProbe(startedMs, step) {
|
|
|
944
1015
|
await refuseAgent(step, done.reason, serviceExit === agentRun.EXIT_AGAIN
|
|
945
1016
|
? `Probe is finishing an update (${done.error}): run the same command again.`
|
|
946
1017
|
: `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);
|
|
1018
|
+
`its log: ${download.getFile(download.LOG_NAME)}`, serviceExit, facts);
|
|
948
1019
|
}
|
|
949
1020
|
if (!probe) {
|
|
950
1021
|
await refuseAgent(step, agentRun.Reason.INCOMPLETE,
|
|
951
1022
|
`Probe did not download (${(done && done.error) || "no probe command after it"}); ` +
|
|
952
|
-
`its log: ${download.getFile(download.LOG_NAME)}`, 1);
|
|
1023
|
+
`its log: ${download.getFile(download.LOG_NAME)}`, 1, facts);
|
|
953
1024
|
}
|
|
954
1025
|
return probe;
|
|
955
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,
|