probe-research 0.24.0 → 0.25.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.
@@ -863,10 +863,17 @@ async function waitForRunningDownload(startedMs, step, watch = null) {
863
863
  }
864
864
  }
865
865
 
866
- /** Read `watch` and report `stage` (none without a watch); a denied or expired sign-in stops here (PR39). */
866
+ /**
867
+ * Read `watch` and report `stage` (none without a watch). The approval read while Probe still installs is
868
+ * answered at once (PR39b): `downloading` with `approved`, who and the stage's `say`, so the agent says it
869
+ * within seconds and runs the wait again for the install. A denied or expired sign-in stops here (PR39).
870
+ */
867
871
  async function updateWatch(watch, step, stage) {
868
872
  if (!watch) return;
869
- await watch.update(stage);
873
+ if (await watch.update(stage)) {
874
+ await refuseAgent(step, agentRun.Reason.DOWNLOADING, "Probe is still installing: run the same command again.",
875
+ agentRun.EXIT_AGAIN, watch.getRunAgainFacts(stage));
876
+ }
870
877
  const again = "run `npx -y probe-research sign-in --json` for a new one.";
871
878
  if (watch.status === signIn.ApprovalStatus.DENIED) {
872
879
  signIn.clearPending();
@@ -885,6 +892,7 @@ async function updateWatch(watch, step, stage) {
885
892
  */
886
893
  async function refuseUnfinished(step, watch, stage, message = "Probe is still downloading: run the same command again.") {
887
894
  if (watch && watch.isApproved()) {
895
+ watch.markSaid(stage);
888
896
  await refuseAgent(step, agentRun.Reason.DOWNLOADING, "Probe is still installing: run the same command again.",
889
897
  agentRun.EXIT_AGAIN, watch.getRunAgainFacts(stage));
890
898
  }
@@ -956,8 +964,8 @@ async function findDownloadedProbe(done, startedMs, step, watch = null) {
956
964
 
957
965
  /**
958
966
  * `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).
967
+ * (within the budget), while the approval is read from the server (PR39): the call that reads it answers at
968
+ * once (PR39b); the next one waits for the install and says each stage as it happens.
961
969
  */
962
970
  async function waitForSignIn(json, startedMs) {
963
971
  const pending = signIn.loadPending();
@@ -973,7 +981,7 @@ async function waitForSignIn(json, startedMs) {
973
981
  const watch = new approval.ApprovalWatch(pending);
974
982
  const probe = await getAgentProbe(startedMs, AgentStep.SIGN_IN_WAIT, watch);
975
983
  // 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);
984
+ if (watch.hasWatched()) await sayInstalled(watch, startedMs);
977
985
  return runAgentCall(probe, ["wizard", "--action", "login", "--pending", ...(json ? ["--json"] : [])], startedMs,
978
986
  AgentStep.SIGN_IN_WAIT);
979
987
  }
@@ -982,6 +990,7 @@ async function waitForSignIn(json, startedMs) {
982
990
  async function sayInstalled(watch, startedMs) {
983
991
  await updateWatch(watch, AgentStep.SIGN_IN_WAIT, approval.InstallStage.DONE);
984
992
  if (!watch.isApproved() || startedMs + agentRun.AGENT_BUDGET_MS - Date.now() >= APPROVED_RESERVE_MS) return;
993
+ watch.markSaid(approval.InstallStage.DONE);
985
994
  await refuseAgent(AgentStep.SIGN_IN_WAIT, agentRun.Reason.RUN_AGAIN,
986
995
  "Probe is installed, with too little time left to save the sign-in: run the same command again.",
987
996
  agentRun.EXIT_AGAIN, watch.getRunAgainFacts(approval.InstallStage.DONE));
@@ -1,13 +1,17 @@
1
1
  /**
2
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.
3
+ * seconds. The call that reads it while Probe still installs answers at once (PR39b), so the agent says
4
+ * "You're approved" within seconds; the approval is kept (`approval.json`), so the next call only waits for
5
+ * the install, one JSON line per stage as it happens. Richard's fresh Mac approved at once and heard
6
+ * "waiting for approval" for minutes: the old wait read the approval only through the CLI, after the download.
6
7
  */
7
8
 
9
+ const fs = require("node:fs");
10
+ const path = require("node:path");
11
+
8
12
  const { writeLine } = require("./agent-run");
9
13
  const { DownloadStage } = require("./background-download");
10
- const { ApprovalStatus, fetchApproval } = require("./sign-in");
14
+ const { ApprovalStatus, fetchApproval, getStateFolder } = require("./sign-in");
11
15
 
12
16
  /** Where Probe's install on this computer is: the download's stages, then done (its `probe` answers). */
13
17
  const InstallStage = Object.freeze({ ...DownloadStage, DONE: "done" });
@@ -21,6 +25,9 @@ const STAGE_WORDS = Object.freeze({
21
25
  const PROGRESS_EVENT = "progress";
22
26
  /** The shortest gap between two reads; the server's poll interval when longer. */
23
27
  const MIN_READ_GAP_MS = 2000;
28
+ /** This attempt's approval and the last stage said, kept for its next `sign-in --wait` (0600, no secret). */
29
+ const APPROVAL_NAME = "approval.json";
30
+ const APPROVAL_VERSION = 1;
24
31
  /** Answers that never change: not asked again. */
25
32
  const SETTLED = Object.freeze([ApprovalStatus.APPROVED, ApprovalStatus.DENIED, ApprovalStatus.EXPIRED,
26
33
  ApprovalStatus.ONBOARDING_REQUIRED, ApprovalStatus.UNREADABLE]);
@@ -36,37 +43,87 @@ function getWaitingSay(pending) {
36
43
  return `Waiting for your approval in the browser${link}${pending.user_code ? ` (code ${pending.user_code})` : ""}.`;
37
44
  }
38
45
 
46
+ /** The approval an earlier call of this attempt read, or null. */
47
+ function loadApproval(attempt) {
48
+ try {
49
+ const data = JSON.parse(fs.readFileSync(path.join(getStateFolder(), APPROVAL_NAME), "utf8"));
50
+ return data && data.v === APPROVAL_VERSION && attempt && data.attempt === attempt ? data : null;
51
+ } catch {
52
+ return null;
53
+ }
54
+ }
55
+
56
+ /** Keep this attempt's approval and the stage last said: whether it was kept. */
57
+ function saveApproval(attempt, account, stage) {
58
+ if (!attempt) return false;
59
+ try {
60
+ fs.mkdirSync(getStateFolder(), { recursive: true, mode: 0o700 });
61
+ const file = path.join(getStateFolder(), APPROVAL_NAME);
62
+ const temp = `${file}.${process.pid}`;
63
+ fs.writeFileSync(temp, JSON.stringify({ v: APPROVAL_VERSION, attempt, account, stage }), { mode: 0o600 });
64
+ fs.renameSync(temp, file);
65
+ return true;
66
+ } catch {
67
+ return false;
68
+ }
69
+ }
70
+
39
71
  class ApprovalWatch {
40
- /** `pending`: the sign-in `startSignIn` left. `write` takes each progress line (stdout by default). */
72
+ /** `pending`: the sign-in `startSignIn` left. `write` takes each progress line (stdout by default). An
73
+ * approval an earlier call of this attempt read is known at once, never read again. */
41
74
  constructor(pending, { fetchImpl = fetch, write = writeLine, now = Date.now } = {}) {
42
75
  this.pending = pending;
43
76
  this.fetchImpl = fetchImpl;
44
77
  this.write = write;
45
78
  this.now = now;
46
- this.status = null;
47
- this.account = null;
79
+ const kept = loadApproval(pending.attempt);
80
+ this.status = kept ? ApprovalStatus.APPROVED : null;
81
+ this.account = kept && typeof kept.account === "string" ? kept.account : null;
82
+ this.reportedStage = kept ? kept.stage || null : null;
48
83
  this.readGapMs = Math.max(MIN_READ_GAP_MS, (Number(pending.interval) || 0) * 1000);
49
84
  this.nextReadMs = 0;
50
- this.reportedStage = null;
85
+ this.isWatching = false;
51
86
  }
52
87
 
53
- /** Read the approval when a read is due, then, once approved, report `stage` (InstallStage) if it is new. */
88
+ /**
89
+ * Read the approval when a read is due. True when it came in this read while Probe still installs
90
+ * (`stage` set, not done): the caller answers at once with `getRunAgainFacts(stage)`, which this
91
+ * reports as said. Otherwise, once approved, one line for `stage` (InstallStage) if it is new.
92
+ */
54
93
  async update(stage = null) {
94
+ this.isWatching = true;
95
+ let isNew = false;
55
96
  if (!SETTLED.includes(this.status) && this.now() >= this.nextReadMs) {
56
97
  this.nextReadMs = this.now() + this.readGapMs;
57
98
  const read = await fetchApproval(this.pending, { fetchImpl: this.fetchImpl });
58
- if (read) ({ status: this.status, account: this.account } = read);
99
+ if (read) {
100
+ isNew = read.status === ApprovalStatus.APPROVED;
101
+ ({ status: this.status, account: this.account } = read);
102
+ }
59
103
  }
60
- if (this.isApproved() && stage && stage !== this.reportedStage) {
104
+ // Ends the call only when the next one will know: unkept, every call would read it anew and end again.
105
+ if (isNew && stage && stage !== InstallStage.DONE && saveApproval(this.pending.attempt, this.account, stage)) {
61
106
  this.reportedStage = stage;
107
+ return true;
108
+ }
109
+ if (this.isApproved() && stage && stage !== this.reportedStage) {
110
+ this.markSaid(stage);
62
111
  this.write(JSON.stringify({ event: PROGRESS_EVENT, say: getApprovedSay(this.account, stage), approved: true,
63
112
  account: this.account, stage }));
64
113
  }
114
+ if (isNew) saveApproval(this.pending.attempt, this.account, this.reportedStage);
115
+ return false;
116
+ }
117
+
118
+ /** `stage` was said to the person (a line or an answer): kept for this attempt's next call; whether it was. */
119
+ markSaid(stage) {
120
+ this.reportedStage = stage;
121
+ return saveApproval(this.pending.attempt, this.account, stage);
65
122
  }
66
123
 
67
- /** Whether the wait read the approval at all (it does only while Probe installs). */
68
- hasStarted() {
69
- return this.nextReadMs > 0;
124
+ /** Whether this call watched the install (it does only while Probe installs). */
125
+ hasWatched() {
126
+ return this.isWatching;
70
127
  }
71
128
 
72
129
  isApproved() {
@@ -90,4 +147,4 @@ class ApprovalWatch {
90
147
  }
91
148
  }
92
149
 
93
- module.exports = { ApprovalWatch, InstallStage, PROGRESS_EVENT, getApprovedSay };
150
+ module.exports = { APPROVAL_NAME, ApprovalWatch, InstallStage, PROGRESS_EVENT, getApprovedSay };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "probe-research",
3
- "version": "0.24.0",
3
+ "version": "0.25.0",
4
4
  "description": "Setup wizard for Probe Research in your coding agents. Runs from zero with npx.",
5
5
  "bin": {
6
6
  "probe-research": "bin/probe-research.js"