@parcha/agentrun-serve 0.1.0-beta.56 → 0.1.0-beta.57

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.
Files changed (275) hide show
  1. package/CHANGELOG.md +229 -0
  2. package/README.md +201 -52
  3. package/dist/acp-package.d.ts +2 -0
  4. package/dist/adapters.d.ts +15 -3
  5. package/dist/adapters.js +17 -6
  6. package/dist/agent-package.d.ts +3 -3
  7. package/dist/agent-package.js +17 -8
  8. package/dist/api-keys.d.ts +76 -0
  9. package/dist/api-keys.js +217 -0
  10. package/dist/api-only.d.ts +17 -5
  11. package/dist/api-only.js +34 -8
  12. package/dist/browser-hold.d.ts +23 -0
  13. package/dist/browser-hold.js +48 -0
  14. package/dist/browser-person.d.ts +3 -0
  15. package/dist/browser-person.js +4 -1
  16. package/dist/chatgpt-plan.d.ts +18 -8
  17. package/dist/chatgpt-plan.js +91 -34
  18. package/dist/claude-login.d.ts +74 -16
  19. package/dist/claude-login.js +215 -56
  20. package/dist/claude-sign-in.d.ts +17 -6
  21. package/dist/claude-sign-in.js +42 -39
  22. package/dist/cli.js +63 -6
  23. package/dist/composio-made.d.ts +4 -0
  24. package/dist/composio-made.js +4 -0
  25. package/dist/composio-preset.d.ts +43 -2
  26. package/dist/composio-preset.js +92 -4
  27. package/dist/computer-held-routes.d.ts +5 -4
  28. package/dist/computer-held-routes.js +29 -8
  29. package/dist/computer-held.d.ts +6 -1
  30. package/dist/computer-held.js +14 -0
  31. package/dist/computer-record.js +4 -2
  32. package/dist/computer-viewer.js +4 -1
  33. package/dist/console/assets/{backend-Boh7tC4i.js → backend-DVhHzJX2.js} +19 -19
  34. package/dist/console/assets/index-Bwn1oTem.css +1 -0
  35. package/dist/console/assets/index-okP-y8PK.js +747 -0
  36. package/dist/console/index.html +2 -2
  37. package/dist/decisions.d.ts +1 -1
  38. package/dist/decisions.js +1 -1
  39. package/dist/docker-host-addresses.d.ts +22 -11
  40. package/dist/docker-host-addresses.js +38 -26
  41. package/dist/env-switch.d.ts +10 -0
  42. package/dist/env-switch.js +33 -2
  43. package/dist/errors.d.ts +1 -0
  44. package/dist/errors.js +2 -0
  45. package/dist/fal-price.d.ts +58 -20
  46. package/dist/fal-price.js +341 -82
  47. package/dist/handed-values.d.ts +43 -1
  48. package/dist/handed-values.js +119 -2
  49. package/dist/harness-archive.d.ts +13 -10
  50. package/dist/harness-archive.js +60 -34
  51. package/dist/harness-check.d.ts +3 -1
  52. package/dist/harness-check.js +25 -46
  53. package/dist/harness-keys.d.ts +73 -27
  54. package/dist/harness-keys.js +194 -61
  55. package/dist/harness-recipe-entry.d.ts +8 -7
  56. package/dist/harness-recipe-entry.js +21 -32
  57. package/dist/harness-recipe.d.ts +38 -25
  58. package/dist/harness-recipe.js +49 -36
  59. package/dist/harness-sign-in.js +12 -23
  60. package/dist/harness-table.d.ts +10 -3
  61. package/dist/harness-table.js +26 -21
  62. package/dist/harness.d.ts +1 -0
  63. package/dist/harness.js +17 -5
  64. package/dist/hosted-search.d.ts +1 -1
  65. package/dist/hosted-search.js +1 -2
  66. package/dist/import-worker.d.ts +4 -2
  67. package/dist/import-worker.js +9 -2
  68. package/dist/in-flight.d.ts +26 -0
  69. package/dist/in-flight.js +52 -0
  70. package/dist/key-refusal.d.ts +9 -4
  71. package/dist/key-refusal.js +107 -25
  72. package/dist/learn/secrets.d.ts +13 -3
  73. package/dist/learn/secrets.js +118 -17
  74. package/dist/learning-cost.d.ts +6 -3
  75. package/dist/learning-cost.js +4 -2
  76. package/dist/link-check.js +1 -1
  77. package/dist/llms.txt +1 -1
  78. package/dist/marketplace.d.ts +77 -0
  79. package/dist/marketplace.js +224 -0
  80. package/dist/mcp-api-only-origins.d.ts +30 -0
  81. package/dist/mcp-api-only-origins.js +57 -0
  82. package/dist/mcp-gateway.d.ts +7 -5
  83. package/dist/mcp-gateway.js +9 -12
  84. package/dist/mcp-secret-writes.js +5 -3
  85. package/dist/mcp-servers-store.d.ts +28 -6
  86. package/dist/mcp-servers-store.js +53 -4
  87. package/dist/media-duration.d.ts +3 -0
  88. package/dist/media-duration.js +90 -9
  89. package/dist/model-form.d.ts +5 -4
  90. package/dist/model-form.js +11 -7
  91. package/dist/model.d.ts +5 -2
  92. package/dist/model.js +9 -5
  93. package/dist/ollama-provider.d.ts +7 -1
  94. package/dist/ollama-provider.js +14 -2
  95. package/dist/openapi.js +178 -27
  96. package/dist/otel/genai.d.ts +65 -0
  97. package/dist/otel/genai.js +116 -0
  98. package/dist/otel/tracer.d.ts +55 -0
  99. package/dist/otel/tracer.js +308 -0
  100. package/dist/page.d.ts +2 -0
  101. package/dist/page.js +3 -1
  102. package/dist/paid-calls.d.ts +1 -32
  103. package/dist/paid-calls.js +4 -39
  104. package/dist/provider.d.ts +9 -0
  105. package/dist/provider.js +50 -3
  106. package/dist/redact/match.d.ts +30 -3
  107. package/dist/redact/match.js +422 -41
  108. package/dist/refusal-words.d.ts +11 -0
  109. package/dist/refusal-words.js +11 -0
  110. package/dist/run-census.d.ts +67 -0
  111. package/dist/run-census.js +240 -0
  112. package/dist/run-computer.d.ts +6 -3
  113. package/dist/run-computer.js +4 -1
  114. package/dist/run-cut.d.ts +15 -6
  115. package/dist/run-cut.js +30 -9
  116. package/dist/run-files.d.ts +3 -0
  117. package/dist/run-files.js +3 -0
  118. package/dist/run-launch.d.ts +12 -1
  119. package/dist/run-launch.js +9 -10
  120. package/dist/run-learning-served.d.ts +2 -0
  121. package/dist/run-model-key.d.ts +8 -0
  122. package/dist/run-model-key.js +31 -1
  123. package/dist/run-reads-over-store.d.ts +24 -6
  124. package/dist/run-reads-over-store.js +67 -23
  125. package/dist/run-recordings.d.ts +5 -1
  126. package/dist/run-recordings.js +39 -14
  127. package/dist/run-timeline-served.d.ts +2 -0
  128. package/dist/run-timeline-served.js +2 -2
  129. package/dist/run-usage.d.ts +1 -0
  130. package/dist/runnable.d.ts +10 -1
  131. package/dist/runnable.js +20 -8
  132. package/dist/runner/adapter-loop.d.ts +11 -0
  133. package/dist/runner/adapter-loop.js +55 -10
  134. package/dist/runner/adapter-run-spec.d.ts +45 -8
  135. package/dist/runner/adapter-run-spec.js +150 -28
  136. package/dist/runner/adapters/pi.js +25 -3
  137. package/dist/runner/beside.js +4 -3
  138. package/dist/runner/big-writes.js +1 -10
  139. package/dist/runner/claim-secrets.d.ts +18 -3
  140. package/dist/runner/claim-secrets.js +35 -6
  141. package/dist/runner/cli-run-spec.d.ts +6 -0
  142. package/dist/runner/cli-run-spec.js +82 -35
  143. package/dist/runner/computer-journal.d.ts +9 -1
  144. package/dist/runner/computer-journal.js +26 -0
  145. package/dist/runner/conformance/harness.js +1 -1
  146. package/dist/runner/feedback-round.js +3 -0
  147. package/dist/runner/harness-loops.d.ts +6 -5
  148. package/dist/runner/harness-loops.js +7 -7
  149. package/dist/runner/held.d.ts +11 -5
  150. package/dist/runner/journal-kinds.d.ts +5 -0
  151. package/dist/runner/journal-kinds.js +5 -0
  152. package/dist/runner/main.js +7 -2
  153. package/dist/runner/mcp-claim.js +30 -3
  154. package/dist/runner/mcp-run.d.ts +12 -0
  155. package/dist/runner/mcp-run.js +12 -5
  156. package/dist/runner/model-route.d.ts +17 -0
  157. package/dist/runner/model-route.js +120 -2
  158. package/dist/runner/run-child-host.d.ts +3 -0
  159. package/dist/runner/run-child-host.js +69 -28
  160. package/dist/runner/run-child.js +5 -2
  161. package/dist/runner/runner.d.ts +14 -3
  162. package/dist/runner/runner.js +129 -33
  163. package/dist/runner/store-port.d.ts +1 -1
  164. package/dist/runner/store-secrets.d.ts +6 -0
  165. package/dist/runner/store-secrets.js +49 -7
  166. package/dist/runner/subagent.js +4 -40
  167. package/dist/runner/workflow-first.d.ts +11 -4
  168. package/dist/runner/workflow-first.js +14 -8
  169. package/dist/runner/workflow-spec.js +3 -2
  170. package/dist/runs-over-store.d.ts +15 -6
  171. package/dist/runs-over-store.js +33 -21
  172. package/dist/scrub-command.d.ts +73 -0
  173. package/dist/scrub-command.js +307 -0
  174. package/dist/secret-cut.d.ts +73 -0
  175. package/dist/secret-cut.js +291 -4
  176. package/dist/secret-rules.d.ts +143 -0
  177. package/dist/secret-rules.js +2564 -0
  178. package/dist/secrets.d.ts +7 -0
  179. package/dist/secrets.js +20 -0
  180. package/dist/server-keys.d.ts +20 -0
  181. package/dist/server-keys.js +25 -6
  182. package/dist/server.d.ts +52 -11
  183. package/dist/server.js +345 -118
  184. package/dist/skills/agentrun-serve/SKILL.md +4 -0
  185. package/dist/store/index.d.ts +1 -1
  186. package/dist/store/index.js +1 -1
  187. package/dist/store/journal/storage.js +1 -1
  188. package/dist/store/postgres/migrations/0027_run_launch_census.sql +31 -0
  189. package/dist/store/schema.d.ts +6 -0
  190. package/dist/store/schema.js +11 -1
  191. package/dist/store/seal.d.ts +6 -1
  192. package/dist/store/seal.js +5 -3
  193. package/dist/store/sqlite/run-launch.d.ts +9 -1
  194. package/dist/store/sqlite/run-launch.js +24 -13
  195. package/dist/store/sqlite/run-patch.d.ts +1 -1
  196. package/dist/store/sqlite/run-patch.js +13 -4
  197. package/dist/store/sqlite/store.js +34 -4
  198. package/dist/store/store.d.ts +43 -2
  199. package/dist/store/store.js +20 -0
  200. package/dist/store-reads/learning.d.ts +6 -3
  201. package/dist/store-reads/learning.js +22 -7
  202. package/dist/store-view.d.ts +3 -0
  203. package/dist/store-view.js +3 -3
  204. package/dist/suggestions.js +3 -2
  205. package/dist/template-eval.d.ts +20 -9
  206. package/dist/template-eval.js +115 -33
  207. package/dist/template-import.d.ts +10 -0
  208. package/dist/template-import.js +16 -7
  209. package/dist/templates.d.ts +10 -2
  210. package/dist/templates.js +28 -8
  211. package/dist/timeline/labels.d.ts +6 -4
  212. package/dist/timeline/labels.js +18 -6
  213. package/dist/timeline/timeline.d.ts +5 -0
  214. package/dist/timeline/timeline.js +14 -8
  215. package/dist/timeline/util.js +1 -1
  216. package/dist/timeline/web-calls.d.ts +26 -9
  217. package/dist/timeline/web-calls.js +124 -37
  218. package/dist/tool-gate.d.ts +42 -3
  219. package/dist/tool-gate.js +66 -5
  220. package/dist/tool-keys-store.d.ts +2 -4
  221. package/dist/tool-keys-store.js +5 -21
  222. package/dist/tool-keys.d.ts +2 -2
  223. package/dist/tool-keys.js +2 -2
  224. package/dist/tool-list.js +1 -1
  225. package/dist/tool-plane.d.ts +2 -2
  226. package/dist/tool-plane.js +35 -33
  227. package/dist/tools.js +13 -7
  228. package/dist/trigger-targets.d.ts +4 -0
  229. package/dist/trigger-targets.js +9 -2
  230. package/dist/triggers.d.ts +67 -4
  231. package/dist/triggers.js +171 -22
  232. package/dist/until.d.ts +3 -0
  233. package/dist/until.js +11 -0
  234. package/dist/web-fake.d.ts +1 -3
  235. package/dist/web-fake.js +7 -18
  236. package/dist/web-fetch.d.ts +6 -72
  237. package/dist/web-fetch.js +11 -265
  238. package/dist/web-run.d.ts +31 -0
  239. package/dist/web-run.js +90 -0
  240. package/dist/web-save.d.ts +6 -4
  241. package/dist/web-save.js +9 -12
  242. package/dist/web-search.d.ts +9 -127
  243. package/dist/web-search.js +23 -259
  244. package/dist/wires.d.ts +13 -0
  245. package/dist/wires.js +11 -0
  246. package/dist/workflow-run.d.ts +5 -4
  247. package/dist/workflow-run.js +5 -4
  248. package/dist/workspace/computer-deps.d.ts +2 -1
  249. package/dist/workspace/computer-deps.js +9 -2
  250. package/dist/workspace/computer-for-claim.d.ts +12 -1
  251. package/dist/workspace/computer-for-claim.js +77 -8
  252. package/dist/workspace/computer-kept.d.ts +3 -0
  253. package/dist/workspace/computer-kept.js +11 -2
  254. package/dist/workspace/index.d.ts +1 -1
  255. package/dist/workspace/index.js +1 -1
  256. package/dist/workspace/kind-skills.d.ts +4 -0
  257. package/dist/workspace/kind-skills.js +9 -0
  258. package/dist/workspace/synced-folder.js +3 -4
  259. package/dist/wrapped.d.ts +13 -5
  260. package/dist/wrapped.js +52 -26
  261. package/package.json +17 -11
  262. package/skills/agentrun-serve/SKILL.md +4 -0
  263. package/templates/README.md +30 -8
  264. package/dist/console/assets/index-CQ3cJRzI.js +0 -746
  265. package/dist/console/assets/index-DKkaS3a0.css +0 -1
  266. package/dist/exa-keyless.d.ts +0 -34
  267. package/dist/exa-keyless.js +0 -118
  268. package/dist/firecrawl-keyless.d.ts +0 -41
  269. package/dist/firecrawl-keyless.js +0 -152
  270. package/dist/keyless-mcp.d.ts +0 -36
  271. package/dist/keyless-mcp.js +0 -127
  272. package/dist/text-urls.d.ts +0 -38
  273. package/dist/text-urls.js +0 -170
  274. package/dist/web-fetch-direct.d.ts +0 -119
  275. package/dist/web-fetch-direct.js +0 -190
@@ -0,0 +1,48 @@
1
+ // Whether the owner drives a run's browser now, for the run read's `computer.state` (`taken_over`) and so for Running now's
2
+ // "You drive". A browser is taken over in its live view, in the run's process (browser-person.ts personHands.take), not
3
+ // through the server's command rows as a desktop or a phone is (computer-commands.ts), so the server learns it from one
4
+ // small setting per run (`browser_hold:<run id>`: { session, at, epoch }), as it learns an ended wait
5
+ // (browser-wait-ended.ts). The run's process writes it as a take goes live and removes it as the take ends, however it
6
+ // ends, one change after another (browserHoldWriter); it removes any row an earlier process of the run left as it
7
+ // starts, since its viewers start with nobody holding the browser, so a take a crashed process never ended is not read as
8
+ // still in force. The runner removes it at the run's end.
9
+ const keyOf = (runId) => `browser_hold:${runId}`;
10
+ /** The settings key a run's browser take is kept under (the run's delete removes it: server.ts). */
11
+ export const browserHoldKey = keyOf;
12
+ /** The run's process, as a take of its browser goes live. */
13
+ export async function markBrowserHeld(settings, runId, held, epoch) {
14
+ await settings.put(keyOf(runId), epoch === undefined ? held : { ...held, epoch });
15
+ }
16
+ /** The run's process, as a take ends (given back, idle, away, the cap, the session gone, an answered ask), as a browser
17
+ * session opens, or at the run's end. Given the claim's `epoch`, a take a later claim wrote stays. Given `session`, only
18
+ * that session's take goes (another session's take, live meanwhile, stays). */
19
+ export async function clearBrowserHeld(settings, runId, epoch, session) {
20
+ if (epoch !== undefined || session !== undefined) {
21
+ const written = (await settings.get(keyOf(runId)));
22
+ if (!written)
23
+ return;
24
+ if (epoch !== undefined && typeof written.epoch === "number" && written.epoch > epoch)
25
+ return;
26
+ if (session !== undefined && written.session !== session)
27
+ return;
28
+ }
29
+ await settings.remove(keyOf(runId));
30
+ }
31
+ /** Whether the owner holds the run's browser now: one settings read; a value of another shape is no take. */
32
+ export async function browserHeld(settings, runId) {
33
+ const value = await settings.get(keyOf(runId));
34
+ if (!value || typeof value !== "object")
35
+ return false;
36
+ const { session, at } = value;
37
+ return typeof session === "string" && typeof at === "string";
38
+ }
39
+ /** The run's process's writes of its browser's take, each after the one before it, so a quick take and hand-back can never
40
+ * land backward (the hand-back's removal reading before the take's write is in). The first removes any take an earlier
41
+ * process of the run left: this process's viewers start with nobody holding the browser. A write that fails leaves the
42
+ * ones after it to run. */
43
+ export function browserHoldWriter(settings, runId, epoch) {
44
+ let last = clearBrowserHeld(settings, runId, epoch).catch(() => undefined);
45
+ return {
46
+ held: (tag, on, at) => (last = last.then(() => (on ? markBrowserHeld(settings, runId, { session: tag, at: new Date(at).toISOString() }, epoch) : clearBrowserHeld(settings, runId, epoch, tag))).catch(() => undefined)),
47
+ };
48
+ }
@@ -58,6 +58,9 @@ export declare function personHands(options: {
58
58
  /** A wait on the person's answer ended with its browser let go (its window, the cap, its session gone): when (ms) and
59
59
  * the timeline row's words, for the Screen tab's ended state (browser-wait-ended.ts). Never throws out. */
60
60
  waitEnded?(at: number, why: string): void;
61
+ /** A take of session `tag` went live (`on`, at `at` ms) or ended, however it ended: for the run read's
62
+ * `computer.state` (browser-hold.ts). Never throws out. */
63
+ held?(tag: string, on: boolean, at: number): void;
61
64
  holdMs?: number;
62
65
  pollMs?: number;
63
66
  /** For tests: the timer functions the person's time is held again by. */
@@ -413,7 +413,9 @@ export function personHands(options) {
413
413
  if (ended || live)
414
414
  return;
415
415
  live = true;
416
- void note(HOST_PHASE, { summary: TAKEN, at: now() });
416
+ const at = now();
417
+ options.held?.(tag, true, at);
418
+ void note(HOST_PHASE, { summary: TAKEN, at });
417
419
  },
418
420
  // No hold started after all: what was held for it goes, and no row was written for it.
419
421
  cancel: () => {
@@ -432,6 +434,7 @@ export function personHands(options) {
432
434
  // they took it, so none says how it came back. An answered ask writes its own row.
433
435
  if (!live)
434
436
  return Promise.resolve();
437
+ options.held?.(tag, false, now());
435
438
  // Handed back by the person (the viewer's Hand back): their word that they are done, so the agent's open ask on
436
439
  // this session is answered done, as the hand-back route would answer it (first answer wins). Never on any other
437
440
  // end (cap, idle, away, released, answered): the person did not say they were done. The answer's own row
@@ -12,7 +12,8 @@ export declare const PLAN_FALLBACK_MODELS: readonly string[];
12
12
  /** The model a run on the plan gets when it names none. */
13
13
  export declare const PLAN_DEFAULT_MODEL = "gpt-6.1-sol";
14
14
  export type PlanState = "signed_out" | "pending" | "signed_in" | "expired";
15
- /** GET /settings/chatgpt: never a token. `problem`: why the last sign-in ended without signing in, in words. */
15
+ /** GET /settings/chatgpt: never a token. `problem`: why the last sign-in ended without signing in, in words. `hint`: on a
16
+ * sign-in waiting past its first minute, why it may be stuck (STILL_WAITING). */
16
17
  export type PlanView = {
17
18
  state: PlanState;
18
19
  email?: string;
@@ -21,6 +22,7 @@ export type PlanView = {
21
22
  expires_at?: string;
22
23
  models?: string[];
23
24
  problem?: string;
25
+ hint?: string;
24
26
  };
25
27
  /** The words a run or a request gets when there is no plan token to send. */
26
28
  export declare const SIGNED_OUT = "This server is not signed in to ChatGPT: sign in under Settings \u2192 Model keys, or name a model on a key.";
@@ -107,8 +109,9 @@ export declare class ChatGPTPlan implements PlanReader {
107
109
  /** This server's id at OpenAI (its agent host id): a UUID made once and kept in the store's settings. Not a credential. */
108
110
  private hostId;
109
111
  private cancel;
110
- /** Ends the sign-in waiting past its 10 minutes: a cancel, which says nothing by itself, that says it timed out. One
111
- * whose code OpenAI answered is past waiting (it is being kept) and never times out. */
112
+ /** Ends the sign-in waiting past its 10 minutes: a cancel, which says nothing by itself, that says it timed out (with
113
+ * no admin guess when a code came back and its exchange was cut off). One whose code OpenAI answered is past waiting
114
+ * (it is being kept) and never times out. */
112
115
  private expire;
113
116
  /** Starts a sign-in (one at a time: a new start cancels the one waiting) and gives the address to open. It lasts 10
114
117
  * minutes: the loopback callback finishes it where the browser runs on this machine; elsewhere finishSignIn does. */
@@ -124,9 +127,15 @@ export declare class ChatGPTPlan implements PlanReader {
124
127
  * of is revoked first, then replaced only while the row still holds it (a renewal of it that wrote back meanwhile is
125
128
  * read, revoked and replaced in turn), so no earlier pair is left alive. */
126
129
  private keep;
127
- /** Takes again a sign-in's change the server failed to take (changed_failed), at the server's read of the sign-in, so
128
- * what it offers does not wait for a restart. Nothing when none is owed; a failure again stays owed, said once. */
129
- changeAgain(): Promise<void>;
130
+ /** Tells the server the sign-in changed (its change callback: env read again, the listings relisted, the offers
131
+ * probed). What the server does with it is not the change's to fail: a failure is owed, said in the log (once: not
132
+ * again for `again`, a retry of one owed), and taken again at the server's next read (refresh). */
133
+ private tellChanged;
134
+ /** The sign-in as the store holds it now, read at the server's read of it: a runner may have renewed it, found it
135
+ * expired, or another server signed in or out. A change is told to the server, and so is one it failed to take, so
136
+ * what it offers does not wait for a restart. One at a time: a read while one is out waits for that one (the console
137
+ * polls, and each change relists and probes). */
138
+ refresh(): Promise<void>;
130
139
  /** The plan's models from OpenAI's list (GET /v1/models with the token), those of the current families; the fallback
131
140
  * list when it can't be read. */
132
141
  private listModels;
@@ -166,8 +175,9 @@ export declare class ChatGPTPlan implements PlanReader {
166
175
  * confirmed it. The endpoint is taken from OpenAI's discovery document only when it is https on auth.openai.com, so a
167
176
  * refresh token goes nowhere else. Never throws. */
168
177
  private revoke;
169
- /** Stops the sign-in waiting, at the server's close. */
170
- close(): void;
178
+ /** Stops the sign-in waiting, at the server's close. Resolves once every sign-in cancelled (by this, a newer start, a
179
+ * sign-out or its 10 minutes) has ended, whatever its end said in the log. */
180
+ close(): Promise<void>;
171
181
  }
172
182
  /** For a run on a plan model (`model`, the one its claim chose): the plan's token read once before anything starts, so a
173
183
  * sign-in that is gone, or that OpenAI refuses to renew, fails the run at once, in words (PlanUnavailable). */
@@ -38,6 +38,8 @@ const DIRECT_SCOPE = "chatgpt.tokens.use.direct";
38
38
  const AGENT_NAME = "AgentRun";
39
39
  const HOST_ID_SETTING = "chatgpt_host_id";
40
40
  const PENDING_MS = 10 * 60_000;
41
+ /** How long a sign-in waits before its view says why it may be stuck (STILL_WAITING): long before PENDING_MS ends it. */
42
+ const HINT_AFTER_MS = 60_000;
41
43
  const REFRESH_MARGIN_MS = 5 * 60_000;
42
44
  /** The longest one refresh call at OpenAI may take: its holder gives it up by then. */
43
45
  const REFRESH_CALL_MS = 15_000;
@@ -114,10 +116,20 @@ const PLAN_NOT_USABLE = "This ChatGPT plan can't be used here: OpenAI's sign-in
114
116
  /** What a refusal whose reason OpenAI doesn't name ends with: a Free plan's sign-in has ended in one, and nothing tells it
115
117
  * apart. It never says Free can't be used: OpenAI changes which plans can. */
116
118
  const FREE_OR_AGAIN = "a Free ChatGPT plan may be the reason; with a paid plan, start it again.";
117
- const TIMED_OUT = "The sign-in timed out before OpenAI sent the code back: start it again.";
119
+ /** The likely cause of a sign-in OpenAI never sends back: a workspace whose admin has turned off open-source apps gets
120
+ * OpenAI's refusal on OpenAI's own page, with no way back to this server, so nothing here can tell it from a tab left
121
+ * open. Named first in a sign-in still waiting (STILL_WAITING) and in its timeout. */
122
+ const ADMIN_LIKELY = "If OpenAI's page showed an error instead of signing you in, your ChatGPT workspace's admin has likely turned off open-source apps (External access → OSS → Use ChatGPT plan): ask them to turn it on, or sign in with another account.";
123
+ const TIMED_OUT = `The sign-in timed out: OpenAI never sent the code back. ${ADMIN_LIKELY} Otherwise, start it again.`;
124
+ /** A pending view's `hint` from HINT_AFTER_MS into its wait on, while no code is in: a sign-in that goes through is back
125
+ * well within it. */
126
+ const STILL_WAITING = `OpenAI hasn't sent the code back yet. ${ADMIN_LIKELY}`;
127
+ /** The timeout of a sign-in whose code came back (the browser's return or a paste) while OpenAI was still answering it:
128
+ * no admin guess, since OpenAI's page did send the browser back. */
129
+ const CUT_OFF = "The sign-in timed out while OpenAI was still answering its code: start it again.";
118
130
  /** The words for OpenAI's refusal `code` at its sign-in page: fixed words, never the code. Anything on this machine can
119
131
  * send the loopback callback an `error` while a sign-in waits, so the code goes to the log alone. */
120
- const deniedWords = (code) => (tag(code) === WORKSPACE_DENIED ? ADMIN_OFF : `ChatGPT did not sign you in: ${FREE_OR_AGAIN}`);
132
+ const deniedWords = (code) => (code === WORKSPACE_DENIED ? ADMIN_OFF : `ChatGPT did not sign you in: ${FREE_OR_AGAIN}`);
121
133
  /** Why a sign-in past its browser step ended without signing in, from what pi-ai's flow or keep threw: `reason` for the
122
134
  * log (OpenAI's status and error code, a missing field's name, a network error's code; never a token or a code) and
123
135
  * `words` a person reads. pi-ai's page says the sign-in completed before it exchanges the code, so these are the only
@@ -137,8 +149,9 @@ export function signInFailure(error) {
137
149
  return { reason, words: ADMIN_OFF };
138
150
  if (message.includes(DIRECT_SCOPE))
139
151
  return { reason: "no_direct_scope", words: PLAN_NOT_USABLE };
152
+ const unsaved = notKept ? tag(cause?.code) : "";
140
153
  if (notKept)
141
- return { reason: `not_saved${tag(cause?.code) ? `_${tag(cause.code)}` : ""}`, words: "OpenAI signed you in, but this server could not save the sign-in: start it again. The server's log says why." };
154
+ return { reason: `not_saved${unsaved ? `_${unsaved}` : ""}`, words: "OpenAI signed you in, but this server could not save the sign-in: start it again. The server's log says why." };
142
155
  if (denied)
143
156
  return { reason, words: deniedWords(code) };
144
157
  if (refused)
@@ -178,8 +191,15 @@ export class ChatGPTPlan {
178
191
  /** Why the last sign-in ended without signing in (signInFailure's words past its browser step, or its timeout), until a
179
192
  * sign-in starts, finishes or a sign-out. A cancelled one leaves none. */
180
193
  #problem;
181
- /** Set when the server's change callback failed after a sign-in was saved; cleared once it takes (changeAgain). */
194
+ /** Set when the server's change callback failed (tellChanged); cleared once one that started after it takes. */
182
195
  #changeOwed = false;
196
+ /** Moved on by each change callback that fails: one that takes clears what is owed only if none failed since it began,
197
+ * so a change that read an earlier sign-in never clears a later one's. */
198
+ #failedChanges = 0;
199
+ /** The server's read in flight (refresh), which a read while it is out waits for. */
200
+ #refreshing;
201
+ /** The ends of the sign-ins cancelled and not yet ended (close waits for them). */
202
+ #ending = new Set();
183
203
  #watchers = new Set();
184
204
  #deps;
185
205
  constructor(deps) {
@@ -261,8 +281,12 @@ export class ChatGPTPlan {
261
281
  this.expire();
262
282
  const row = this.#snapshot;
263
283
  const who = row ? { ...(row.email ? { email: row.email } : {}), ...(row.plan ? { plan: row.plan } : {}) } : {};
264
- if (this.#pending)
265
- return { state: "pending", authorize_url: this.#pending.authorizeUrl, expires_at: new Date(this.#pending.expiresAt).toISOString(), ...who };
284
+ if (this.#pending) {
285
+ // Past its first minute with no code back, it says why it may be stuck. Once a code is in (the browser's return or a
286
+ // paste), OpenAI is answering it, and one it answered is being kept: neither is stuck at OpenAI's page.
287
+ const stuck = !this.#pending.codeIn && !this.#pending.answered && this.now() >= this.#pending.startedAt + HINT_AFTER_MS;
288
+ return { state: "pending", authorize_url: this.#pending.authorizeUrl, expires_at: new Date(this.#pending.expiresAt).toISOString(), ...who, ...(stuck ? { hint: STILL_WAITING } : {}) };
289
+ }
266
290
  const problem = this.#problem ? { problem: this.#problem } : {};
267
291
  if (!row)
268
292
  return { state: "signed_out", ...problem };
@@ -284,14 +308,19 @@ export class ChatGPTPlan {
284
308
  this.#pending = undefined;
285
309
  clearTimeout(pending.timer);
286
310
  pending.abort.abort();
311
+ const ended = pending.result.then(() => undefined, () => undefined);
312
+ this.#ending.add(ended);
313
+ void ended.then(() => this.#ending.delete(ended));
287
314
  }
288
- /** Ends the sign-in waiting past its 10 minutes: a cancel, which says nothing by itself, that says it timed out. One
289
- * whose code OpenAI answered is past waiting (it is being kept) and never times out. */
315
+ /** Ends the sign-in waiting past its 10 minutes: a cancel, which says nothing by itself, that says it timed out (with
316
+ * no admin guess when a code came back and its exchange was cut off). One whose code OpenAI answered is past waiting
317
+ * (it is being kept) and never times out. */
290
318
  expire() {
291
319
  if (!this.#pending || this.#pending.answered)
292
320
  return;
321
+ const codeIn = this.#pending.codeIn;
293
322
  this.cancel();
294
- this.#problem = TIMED_OUT;
323
+ this.#problem = codeIn ? CUT_OFF : TIMED_OUT;
295
324
  this.#deps.log?.({ chatgpt: "sign_in_failed", reason: "timed_out" });
296
325
  }
297
326
  /** Starts a sign-in (one at a time: a new start cancels the one waiting) and gives the address to open. It lasts 10
@@ -315,7 +344,13 @@ export class ChatGPTPlan {
315
344
  const link = new Promise((resolve) => (linked = resolve));
316
345
  const login = oauth.login({
317
346
  signal: abort.signal,
318
- notify: (event) => void (event.type === "auth_url" ? linked(event.url) : undefined),
347
+ // pi-ai's progress note comes as a code is in, from the browser's return or a paste, before it asks OpenAI for tokens.
348
+ notify: (event) => {
349
+ if (event.type === "auth_url")
350
+ linked(event.url);
351
+ else if (event.type === "progress" && this.#pending?.abort === abort)
352
+ this.#pending.codeIn = true;
353
+ },
319
354
  prompt: (prompt) => (prompt.type === "manual_code" ? Promise.race([pasted, new Promise((_, reject) => prompt.signal?.addEventListener("abort", () => reject(new Error("closed")), { once: true }))]) : Promise.reject(new Error("not asked here"))),
320
355
  }, { getDeviceId: () => hostId, agentName: AGENT_NAME });
321
356
  const settle = () => {
@@ -328,7 +363,7 @@ export class ChatGPTPlan {
328
363
  const result = login.then((credential) => {
329
364
  if (this.#pending?.abort === abort)
330
365
  this.#pending.answered = true;
331
- return this.keep(credential, epoch).catch((error) => {
366
+ return this.keep(credential, epoch, settle).catch((error) => {
332
367
  throw new NotKept("not kept", { cause: error });
333
368
  });
334
369
  }).finally(settle);
@@ -347,10 +382,11 @@ export class ChatGPTPlan {
347
382
  })]);
348
383
  if (!authorizeUrl)
349
384
  throw new PlanSignInRefused("The sign-in could not start: try again.");
350
- const expiresAt = this.now() + PENDING_MS;
385
+ const startedAt = this.now();
386
+ const expiresAt = startedAt + PENDING_MS;
351
387
  const timer = setTimeout(() => (this.#pending?.abort === abort ? this.expire() : undefined), PENDING_MS);
352
388
  timer.unref();
353
- this.#pending = { abort, state: new URL(authorizeUrl).searchParams.get("state") ?? "", authorizeUrl, expiresAt, paste, result, timer };
389
+ this.#pending = { abort, state: new URL(authorizeUrl).searchParams.get("state") ?? "", authorizeUrl, startedAt, expiresAt, paste, result, timer };
354
390
  waiting = true;
355
391
  return { authorize_url: authorizeUrl, expires_at: new Date(expiresAt).toISOString() };
356
392
  }
@@ -372,16 +408,22 @@ export class ChatGPTPlan {
372
408
  throw new PlanSignInRefused(shape);
373
409
  const error = url.searchParams.get("error");
374
410
  if (error)
375
- throw new PlanSignInRefused(deniedWords(error));
411
+ throw new PlanSignInRefused(deniedWords(tag(error)));
376
412
  if (url.searchParams.get("state") !== pending.state)
377
413
  throw new PlanSignInRefused("That address is from another sign-in: paste the one from the sign-in you started last, or start again.");
378
414
  if (!url.searchParams.get("code") || !url.searchParams.get("client_id"))
379
415
  throw new PlanSignInRefused(`That address has no sign-in code in it. ${shape}`);
416
+ // A code is in from here (pi-ai's progress note says the same a moment later): a view meanwhile shows no hint.
417
+ pending.codeIn = true;
380
418
  pending.paste(url.href);
381
419
  try {
382
420
  await pending.result;
383
421
  }
384
422
  catch (error) {
423
+ // Cancelled while its code's exchange was out (a newer start, a sign-out, the 10 minutes): the log has no line of
424
+ // it, so the words say what the view says, or that it was cancelled.
425
+ if (pending.abort.signal.aborted)
426
+ throw new PlanSignInRefused(this.#problem ?? "That sign-in was replaced or signed out: start it again.");
385
427
  throw new PlanSignInRefused(signInFailure(error).words);
386
428
  }
387
429
  return this.view();
@@ -390,7 +432,7 @@ export class ChatGPTPlan {
390
432
  * sign-in since then wins: the credential is dropped, never written, and revoked at OpenAI. A sign-in it takes the place
391
433
  * of is revoked first, then replaced only while the row still holds it (a renewal of it that wrote back meanwhile is
392
434
  * read, revoked and replaced in turn), so no earlier pair is left alive. */
393
- async keep(credential, epoch) {
435
+ async keep(credential, epoch, saved) {
394
436
  if (credential.scopes && !credential.scopes.includes(DIRECT_SCOPE))
395
437
  throw new Error(`the grant did not include ${DIRECT_SCOPE}`);
396
438
  this.hold(credential);
@@ -421,30 +463,43 @@ export class ChatGPTPlan {
421
463
  this.#snapshot = row;
422
464
  this.#problem = undefined;
423
465
  this.#deps.log?.({ chatgpt: "signed_in", ...(previousRevoked === undefined ? {} : { previous_revoked: String(previousRevoked) }) });
424
- // Saved: what the server does with the change after it is not the sign-in's to fail. One it failed to take is owed,
425
- // and taken again at its next read (changeAgain).
466
+ // Saved: no longer waiting, so it reads signed in while the server takes the change (`saved`, the sign-in's settle).
467
+ saved?.();
468
+ await this.tellChanged();
469
+ }
470
+ /** Tells the server the sign-in changed (its change callback: env read again, the listings relisted, the offers
471
+ * probed). What the server does with it is not the change's to fail: a failure is owed, said in the log (once: not
472
+ * again for `again`, a retry of one owed), and taken again at the server's next read (refresh). */
473
+ async tellChanged(again = false) {
474
+ const failed = this.#failedChanges;
426
475
  try {
427
476
  await this.#deps.changed?.();
428
- this.#changeOwed = false;
477
+ if (this.#failedChanges === failed)
478
+ this.#changeOwed = false;
429
479
  }
430
480
  catch (error) {
481
+ this.#failedChanges += 1;
431
482
  this.#changeOwed = true;
483
+ if (again)
484
+ return;
432
485
  const code = error?.code;
433
- this.#deps.log?.({ chatgpt: "changed_failed", reason: String(typeof code === "string" ? code : error?.name ?? "error").replace(/[^A-Za-z0-9_]/g, "").slice(0, 40) || "error" });
486
+ this.#deps.log?.({ chatgpt: "changed_failed", reason: tag(typeof code === "string" ? code : error?.name) || "error" });
434
487
  }
435
488
  }
436
- /** Takes again a sign-in's change the server failed to take (changed_failed), at the server's read of the sign-in, so
437
- * what it offers does not wait for a restart. Nothing when none is owed; a failure again stays owed, said once. */
438
- async changeAgain() {
439
- if (!this.#changeOwed)
440
- return;
441
- try {
442
- await this.#deps.changed?.();
443
- this.#changeOwed = false;
444
- }
445
- catch {
446
- // Owed still: the next read takes it again.
447
- }
489
+ /** The sign-in as the store holds it now, read at the server's read of it: a runner may have renewed it, found it
490
+ * expired, or another server signed in or out. A change is told to the server, and so is one it failed to take, so
491
+ * what it offers does not wait for a restart. One at a time: a read while one is out waits for that one (the console
492
+ * polls, and each change relists and probes). */
493
+ refresh() {
494
+ // Cleared in a reaction, never inside the function: a read that throws at once would run a `finally` there before
495
+ // the assignment, and leave a settled promise in flight for good.
496
+ return (this.#refreshing ??= (async () => {
497
+ const was = this.state;
498
+ if ((await this.load()).state !== was)
499
+ await this.tellChanged();
500
+ else if (this.#changeOwed)
501
+ await this.tellChanged(true);
502
+ })().finally(() => (this.#refreshing = undefined)));
448
503
  }
449
504
  /** The plan's models from OpenAI's list (GET /v1/models with the token), those of the current families; the fallback
450
505
  * list when it can't be read. */
@@ -552,7 +607,7 @@ export class ChatGPTPlan {
552
607
  if (await this.swap(bytes, expired)) {
553
608
  this.#snapshot = expired;
554
609
  this.#deps.log?.({ chatgpt: "expired" });
555
- await this.#deps.changed?.();
610
+ await this.tellChanged();
556
611
  throw new PlanUnavailable(EXPIRED);
557
612
  }
558
613
  return this.adopt();
@@ -612,7 +667,7 @@ export class ChatGPTPlan {
612
667
  }
613
668
  this.#snapshot = undefined;
614
669
  this.#deps.log?.({ chatgpt: "signed_out", revoked: String(revoked) });
615
- await this.#deps.changed?.();
670
+ await this.tellChanged();
616
671
  return { revoked };
617
672
  }
618
673
  /** `credential`'s session revoked at OpenAI (its documented revocation endpoint, with the refresh token): whether OpenAI
@@ -631,9 +686,11 @@ export class ChatGPTPlan {
631
686
  return false;
632
687
  }
633
688
  }
634
- /** Stops the sign-in waiting, at the server's close. */
689
+ /** Stops the sign-in waiting, at the server's close. Resolves once every sign-in cancelled (by this, a newer start, a
690
+ * sign-out or its 10 minutes) has ended, whatever its end said in the log. */
635
691
  close() {
636
692
  this.cancel();
693
+ return Promise.all([...this.#ending]).then(() => undefined);
637
694
  }
638
695
  }
639
696
  /** For a run on a plan model (`model`, the one its claim chose): the plan's token read once before anything starts, so a
@@ -21,27 +21,45 @@ export declare const LOGIN_DEFAULT_MODEL = "anthropic/claude-sonnet-5-5";
21
21
  * login (`machine`, which the owner opts into by name). */
22
22
  export declare const LOGIN_PLACES: readonly ["agentrun", "machine"];
23
23
  export type LoginPlace = (typeof LOGIN_PLACES)[number];
24
+ /** `machine`: the id of the machine it was turned on for (machineId). A row saved before the id has none, and reads as
25
+ * turned on for another machine until turned on again. */
24
26
  type Setting = {
25
27
  v: 1;
26
28
  place: LoginPlace;
27
- host: string;
29
+ machine?: string;
28
30
  only_me: true;
29
31
  };
32
+ /** The file in the data folder that holds this machine's id (machineId). */
33
+ export declare const MACHINE_ID_FILE = "machine-id";
34
+ /** This machine's id, which names the machine the login was turned on for: a random one, made once in
35
+ * `<data>/machine-id` (0600) and read back by every later start. Never the hostname, which moves with the network (a
36
+ * laptop's at home is not its name at the office), while the data folder stays. A missing file, or one that holds no
37
+ * id or can't be read, gets a fresh id, so a setting saved under the old one reads as another machine's until turned on
38
+ * again. The file appears whole: written aside, then linked in place, so a start beside this one that made an id first
39
+ * keeps its own, and both read that one. */
40
+ export declare function machineId(data: string): string;
30
41
  /** This machine's own Claude Code login folder as the CLI finds it for the server's user, by the CLI's own rule: its
31
42
  * config dir is `CLAUDE_CONFIG_DIR` when set, else `~/.claude`, and its credentials are kept there too unless
32
43
  * `CLAUDE_SECURESTORAGE_CONFIG_DIR` says otherwise. Read from the server's env, so a Claude Code started with another
33
44
  * `CLAUDE_CONFIG_DIR` is found only when AgentRun was started with it as well. A relative one is not taken: the CLI would
34
- * read it against a working directory this server does not share. `source` says which rule gave the folder. */
45
+ * read it against a working directory this server does not share. `source` says which rule gave the folder.
46
+ * `credentials`: the server's `CLAUDE_SECURESTORAGE_CONFIG_DIR` when it sets one the CLI takes as it is (an absolute
47
+ * path, or empty: `~/.claude`, and on macOS the Keychain item with no suffix), where the CLI keeps the tokens apart
48
+ * from the folder; a relative one is not taken, as above. */
35
49
  export declare function machineFolder(env?: Readonly<Record<string, string | undefined>>): {
36
50
  folder: string;
37
51
  source: "CLAUDE_CONFIG_DIR" | "default";
52
+ credentials?: string;
38
53
  };
39
54
  /** The directories of a login at `place`: AgentRun's in `data`, or this machine's (machineFolder, and the Anthropic
40
55
  * profiles where the CLI looks for them: `ANTHROPIC_CONFIG_DIR`, else `$XDG_CONFIG_HOME/anthropic`, else
41
56
  * `~/.config/anthropic`). This machine's on the CLI's default folder carries the person's home too, so the harness
42
57
  * points the CLI at it as their own CLI runs, with no folder variable (the harness's signInEnv and loginEnv). This
43
- * machine's is `shared` with the person's own CLI, so a status check runs on a copy of its account file
44
- * (the harness's claudeLoginStatus). */
58
+ * machine's with a credentials dir in the server's env (machineFolder's `credentials`) carries it as `secure`, absolute
59
+ * (empty: `~/.claude`, `unsuffixed`), so the CLI keeps the tokens where the person's own CLI with it does. A status
60
+ * check at either place runs on a copy of the account file (the harness's claudeLoginStatus). Every login carries the
61
+ * person's home as `userHome`, read here once (homeOf), so the status check, the sign-in and a run on macOS all run in
62
+ * that one home whatever reads it later (the harness's personHome). */
45
63
  export declare function loginAt(place: LoginPlace, data: string, env?: Readonly<Record<string, string | undefined>>): ClaudeAgentLogin;
46
64
  /** The setting as saved, or undefined when the mode is not on (or the row is not one this reads). */
47
65
  export declare function claudeLoginSetting(settings: Pick<Settings, "get">): Promise<Setting | undefined>;
@@ -69,7 +87,11 @@ export declare const LOGIN_UNKNOWN = "Claude Code could not say whether its logi
69
87
  export declare function whyNotOneOwner(facts: {
70
88
  apiOnly: boolean;
71
89
  tokens: number;
90
+ keys?: number;
91
+ keysDormant?: boolean;
72
92
  }): string | undefined;
93
+ /** Why a server with Claude Code's login on makes no API key for an app, in one line: the same rule from the other side. */
94
+ export declare const LOGIN_REFUSES_API_KEYS = "Claude Code's login on this machine is on, and a Claude plan is one person's: an API key would let an app start runs on it. Turn the login off under Settings \u2192 Model keys first.";
73
95
  /** GET /settings/claude-login. `state`: `off` (not turned on), `other_machine` (turned on for another machine), else
74
96
  * what the CLI says: `signed_in`, `signed_out`, or `no_cli` (no CLI here to ask yet: claude-agent's first run installs
75
97
  * the pinned one), `unknown` (it gave no answer). Never a token: an account's email, organization and plan only when
@@ -91,10 +113,12 @@ export type ClaudeLoginView = {
91
113
  };
92
114
  /** The folder whose login the CLI is asked about: the login's directory at `place`. */
93
115
  folder: string;
94
- /** This machine's own Claude Code login as this server finds it (machineFolder). */
116
+ /** This machine's own Claude Code login as this server finds it (machineFolder), with `credentials` where the server's
117
+ * env sets `CLAUDE_SECURESTORAGE_CONFIG_DIR` (empty included). */
95
118
  machine: {
96
119
  folder: string;
97
120
  source: "CLAUDE_CONFIG_DIR" | "default";
121
+ credentials?: string;
98
122
  };
99
123
  /** The CLI's sign-in this server started, while this server runs (claude-sign-in.ts). */
100
124
  sign_in?: SignInView;
@@ -107,8 +131,8 @@ export declare function turnOnBody(body: unknown): {
107
131
  } | {
108
132
  problem: string;
109
133
  };
110
- /** The setting a turn-on saves, for this machine. AgentRun's own login directory is made (0700) so the CLI can keep its
111
- * login there; nothing in it is read. */
134
+ /** The setting a turn-on saves, for this machine (machineId). AgentRun's own login directory is made (0700) so the CLI
135
+ * can keep its login there; nothing in it is read. */
112
136
  export declare function settingFor(place: LoginPlace, data: string): Setting;
113
137
  /** The login a run of `harness` on this runner goes on: the saved setting's, read now, when the switch is not off, the
114
138
  * harness runs on it, the run is in a runner place (`place`), and this is the machine it was turned on for; else none,
@@ -122,19 +146,40 @@ export declare const claudeLoginRouteOff: (name: string, why: string) => string;
122
146
  /** What the routes need of the server. `pinned`: the pinned CLI's path when installed. `refused`: whyNotOneOwner of this
123
147
  * server. `sameMachine`: the request comes from a browser on this machine to a server bound to loopback with no public
124
148
  * address, so a sign-in the CLI opens in a browser here reaches the person. `status`/`start`: the harness's
125
- * claudeLoginStatus and startClaudeLogin. `changed`: env read again after a turn on or off. `signIns`: the sign-in this
126
- * server started, one per server process (claude-sign-in.ts). */
149
+ * claudeLoginStatus and startClaudeLogin. `saved`: the login runs here go on after a turn on or off (none once off),
150
+ * handed over inside the turn that saved it, so two changes never leave the server's copy out of order. `changed`: env
151
+ * read again after that turn, outside the queue, so a sign-in pressed meanwhile never waits on it. `signIns`: the
152
+ * sign-in this server started, one per server process (claude-sign-in.ts). */
127
153
  export type ClaudeLoginDeps = {
128
154
  signIns: ClaudeSignIns;
129
155
  settings: Pick<Settings, "get" | "put" | "remove">;
130
156
  data: string;
131
157
  env: Readonly<Record<string, string | undefined>>;
132
158
  pinned: () => Promise<string | undefined>;
133
- refused: string | undefined;
159
+ /** Why this server may not turn the mode on now, asked each time and never held: the server's API keys are made and
160
+ * revoked while it runs (whyNotOneOwner counts them). */
161
+ refused: () => string | undefined;
162
+ /** Runs `work` once nothing that changes `refused` is under way, and holds such changes off until it settles: the
163
+ * API keys' turn (api-keys.ts inTurn). The turn-on decides and writes inside it. */
164
+ turn: <T>(work: () => Promise<T>) => Promise<T>;
134
165
  sameMachine: boolean;
135
166
  claude: () => Promise<ClaudeLoginCli>;
167
+ saved: (login: ClaudeAgentLogin | undefined) => void;
136
168
  changed: () => Promise<void>;
137
169
  };
170
+ /** A server's API keys as the login needs them (api-keys.ts ApiKeys): how many there are now, and their turn. */
171
+ export type KeysForLogin = {
172
+ readonly count: number;
173
+ inTurn<T>(work: () => Promise<T>): Promise<T>;
174
+ };
175
+ /** What the one-owner rule gives the login's routes over a server's keys: `refused`, which counts the keys each time
176
+ * it is asked, and `turn`, the keys' own. The server's routes take both from here, and so does the test that runs the
177
+ * turn-on against a held revocation: neither is a value read once, or a turn of its own. */
178
+ export declare const oneOwnerDeps: (facts: {
179
+ apiOnly: boolean;
180
+ tokens: number;
181
+ keysDormant?: boolean;
182
+ }, keys: KeysForLogin) => Pick<ClaudeLoginDeps, "refused" | "turn">;
138
183
  /** A route's refusal in words, answered as its `code` (server.ts makes it an ApiError). */
139
184
  export declare class ClaudeLoginRefused extends Error {
140
185
  readonly code: "forbidden" | "input_invalid" | "agent_not_runnable";
@@ -142,7 +187,9 @@ export declare class ClaudeLoginRefused extends Error {
142
187
  }
143
188
  /** GET /settings/claude-login: the mode's state and, when on, what the CLI says of its login. */
144
189
  export declare function claudeLoginView(deps: ClaudeLoginDeps): Promise<ClaudeLoginView>;
145
- /** PUT /settings/claude-login: turned on, refused in one line where more than one person can start runs. */
190
+ /** PUT /settings/claude-login: turned on, refused in one line where more than one person can start runs. A turn-on that
191
+ * moves the login to another place first ends a sign-in this server started that still waits: its CLI signs in at the
192
+ * old place, which the login no longer reads. One at the same place leaves it waiting, as it still signs in there. */
146
193
  export declare function turnOnClaudeLogin(deps: ClaudeLoginDeps, body: unknown): Promise<ClaudeLoginView>;
147
194
  /** DELETE /settings/claude-login: turned off, and a sign-in this server started that still waits is ended. The login
148
195
  * itself stays as the CLI keeps it; nothing here signs it out. */
@@ -150,16 +197,27 @@ export declare function turnOffClaudeLogin(deps: ClaudeLoginDeps): Promise<Claud
150
197
  /** POST /settings/claude-login/login: the CLI's own sign-in, started on this machine, where it opens a browser, with the
151
198
  * shown line's own variables (the harness's signInEnv), so it signs in where the status reads. A sign-in that
152
199
  * still waits is ended first (deps.signIns). The browser code goes to the CLI, never here; the answer is the view as it
153
- * is when the sign-in starts, its `sign_in` waiting. */
200
+ * is when the sign-in starts, its `sign_in` waiting. The setting is read, and the CLI started on it, in one turn. */
154
201
  export declare function startLoginHere(deps: ClaudeLoginDeps): Promise<ClaudeLoginView>;
155
202
  /** At the server's start: a setting saved where the mode is now off or refused (`why`: the switch, a listen address past
156
203
  * loopback, a second token, the API-only profile) goes, so no runner, in this process or another, runs on it; the reason
157
- * is logged. */
158
- export declare function dropRefusedLogin(settings: Pick<Settings, "get" | "remove">, why: string | undefined, log: (line: string) => void): Promise<void>;
204
+ * is logged. A turn on `signIns`' queue, as every change of the setting is. */
205
+ export declare function dropRefusedLogin(signIns: Pick<ClaudeSignIns, "serial">, settings: Pick<Settings, "get" | "remove">, why: string | undefined, log: (line: string) => void): Promise<void>;
159
206
  /** What the routes and a run's check use of the harness (its login.ts), loaded only when asked. */
160
207
  export type ClaudeLoginCli = Pick<typeof import("@parcha/agentrun-harness-claude"), "claudeLoginStatus" | "startClaudeLogin" | "claudeLoginCommand">;
208
+ /** How long a signed-in answer stands in for a check that gives none (loginBeforeRun). */
209
+ export declare const LOGIN_ANSWER_STANDS_MS: number;
161
210
  /** Before a run on the login: its CLI says it is signed in, or the run is refused in words (never a silent switch to a
162
211
  * key). `cli`: the CLI that answers (loginCli); none, nothing to ask yet. `status`: the CLI's status check (the harness's
163
- * claudeLoginStatus). */
164
- export declare function loginBeforeRun(login: ClaudeAgentLogin | undefined, cli: string | undefined, status: (cli: string, login: ClaudeAgentLogin) => Promise<ClaudeLoginStatus>): Promise<void>;
212
+ * claudeLoginStatus, which asks once and, when the CLI gives no answer, throws with `code` "timed_out" or "no_status").
213
+ * A check that gives no answer (it timed out, or the CLI said nothing that reads as a status) is asked once more, since
214
+ * a loaded host can take past the limit with the login fine. Still none: the run goes on when this login answered
215
+ * signed in within LOGIN_ANSWER_STANDS_MS in this server process (a run's check here, or the Settings view's), as its
216
+ * own CLI still fails in words if the login is gone; else it is refused with LOGIN_UNKNOWN and why. Any other failure (a
217
+ * CLI that is not at its path, say) is refused with LOGIN_UNKNOWN and its own words at once, asked once and never
218
+ * waved through by an earlier answer. A signed-out answer is the answer: never asked again nor overridden, refused at
219
+ * once, and the earlier signed-in time goes. `now`: the clock (Date.now). */
220
+ export declare function loginBeforeRun(login: ClaudeAgentLogin | undefined, cli: string | undefined, status: (cli: string, login: ClaudeAgentLogin) => Promise<ClaudeLoginStatus>, options?: {
221
+ now?: () => number;
222
+ }): Promise<void>;
165
223
  export {};