@parcha/agentrun-serve 0.1.0-beta.56 → 0.1.0-beta.58
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/CHANGELOG.md +281 -0
- package/README.md +246 -66
- package/dist/acp-package.d.ts +2 -0
- package/dist/adapters.d.ts +15 -3
- package/dist/adapters.js +17 -6
- package/dist/agent-package.d.ts +3 -3
- package/dist/agent-package.js +17 -8
- package/dist/api-keys.d.ts +76 -0
- package/dist/api-keys.js +217 -0
- package/dist/api-only.d.ts +15 -5
- package/dist/api-only.js +32 -8
- package/dist/box-paths.d.ts +4 -3
- package/dist/box-paths.js +7 -4
- package/dist/browser-hold.d.ts +23 -0
- package/dist/browser-hold.js +48 -0
- package/dist/browser-person.d.ts +3 -0
- package/dist/browser-person.js +4 -1
- package/dist/chatgpt-plan.d.ts +18 -8
- package/dist/chatgpt-plan.js +91 -34
- package/dist/claude-login-token.d.ts +117 -0
- package/dist/claude-login-token.js +737 -0
- package/dist/claude-login.d.ts +120 -22
- package/dist/claude-login.js +347 -63
- package/dist/claude-sign-in.d.ts +17 -6
- package/dist/claude-sign-in.js +42 -39
- package/dist/cli.js +33 -10
- package/dist/composio-made.d.ts +4 -0
- package/dist/composio-made.js +4 -0
- package/dist/composio-preset.d.ts +43 -2
- package/dist/composio-preset.js +92 -4
- package/dist/computer-attach.d.ts +35 -17
- package/dist/computer-attach.js +301 -49
- package/dist/computer-held-routes.d.ts +5 -4
- package/dist/computer-held-routes.js +29 -8
- package/dist/computer-held.d.ts +6 -1
- package/dist/computer-held.js +14 -0
- package/dist/computer-record.js +4 -2
- package/dist/computer-viewer.js +4 -1
- package/dist/computers.d.ts +10 -10
- package/dist/computers.js +5 -5
- package/dist/console/assets/{backend-Boh7tC4i.js → backend-CSAWUqtZ.js} +19 -19
- package/dist/console/assets/index-Bwn1oTem.css +1 -0
- package/dist/console/assets/index-DPh29cyz.js +747 -0
- package/dist/console/index.html +2 -2
- package/dist/decisions.d.ts +1 -1
- package/dist/decisions.js +1 -1
- package/dist/dial-relay.d.ts +25 -4
- package/dist/dial-relay.js +129 -1
- package/dist/docker-host-addresses.d.ts +22 -11
- package/dist/docker-host-addresses.js +38 -26
- package/dist/env-switch.d.ts +8 -0
- package/dist/env-switch.js +31 -2
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +2 -0
- package/dist/fal-price.d.ts +56 -20
- package/dist/fal-price.js +400 -90
- package/dist/handed-values.d.ts +43 -1
- package/dist/handed-values.js +119 -2
- package/dist/harness-archive.d.ts +13 -10
- package/dist/harness-archive.js +60 -34
- package/dist/harness-check.d.ts +3 -1
- package/dist/harness-check.js +25 -46
- package/dist/harness-keys.d.ts +73 -27
- package/dist/harness-keys.js +194 -61
- package/dist/harness-recipe-entry.d.ts +8 -7
- package/dist/harness-recipe-entry.js +21 -32
- package/dist/harness-recipe.d.ts +38 -25
- package/dist/harness-recipe.js +49 -36
- package/dist/harness-sign-in.js +12 -23
- package/dist/harness-table.d.ts +10 -3
- package/dist/harness-table.js +26 -21
- package/dist/harness.d.ts +1 -0
- package/dist/harness.js +17 -5
- package/dist/hosted-search.d.ts +1 -1
- package/dist/hosted-search.js +1 -2
- package/dist/import-worker.d.ts +4 -2
- package/dist/import-worker.js +9 -2
- package/dist/in-flight.d.ts +26 -0
- package/dist/in-flight.js +52 -0
- package/dist/key-refusal.d.ts +9 -4
- package/dist/key-refusal.js +107 -25
- package/dist/learn/ledger.d.ts +170 -0
- package/dist/learn/ledger.js +218 -0
- package/dist/learn/promote.d.ts +3 -3
- package/dist/learn/promote.js +1 -1
- package/dist/learn/secrets.d.ts +13 -3
- package/dist/learn/secrets.js +118 -17
- package/dist/learning-cost.d.ts +28 -23
- package/dist/learning-cost.js +19 -23
- package/dist/link-check.js +1 -1
- package/dist/live-reach.d.ts +2 -0
- package/dist/live-reach.js +2 -0
- package/dist/llms.txt +1 -1
- package/dist/marketplace.d.ts +77 -0
- package/dist/marketplace.js +224 -0
- package/dist/mcp-api-only-origins.d.ts +35 -0
- package/dist/mcp-api-only-origins.js +57 -0
- package/dist/mcp-gateway.d.ts +7 -5
- package/dist/mcp-gateway.js +9 -12
- package/dist/mcp-secret-writes.js +5 -3
- package/dist/mcp-servers-store.d.ts +28 -6
- package/dist/mcp-servers-store.js +53 -4
- package/dist/media-duration.d.ts +3 -0
- package/dist/media-duration.js +90 -9
- package/dist/micro.d.ts +2 -0
- package/dist/micro.js +2 -0
- package/dist/model-form.d.ts +5 -4
- package/dist/model-form.js +11 -7
- package/dist/model.d.ts +10 -6
- package/dist/model.js +16 -11
- package/dist/monid.d.ts +14 -1
- package/dist/monid.js +103 -26
- package/dist/ollama-provider.d.ts +7 -1
- package/dist/ollama-provider.js +14 -2
- package/dist/openapi.js +210 -37
- package/dist/otel/genai.d.ts +65 -0
- package/dist/otel/genai.js +116 -0
- package/dist/otel/tracer.d.ts +55 -0
- package/dist/otel/tracer.js +308 -0
- package/dist/page.d.ts +2 -0
- package/dist/page.js +3 -1
- package/dist/paid-calls.d.ts +1 -32
- package/dist/paid-calls.js +4 -39
- package/dist/place-offers.d.ts +4 -0
- package/dist/place-offers.js +7 -4
- package/dist/provider.d.ts +11 -2
- package/dist/provider.js +59 -8
- package/dist/redact/match.d.ts +30 -3
- package/dist/redact/match.js +422 -41
- package/dist/refusal-words.d.ts +11 -0
- package/dist/refusal-words.js +11 -0
- package/dist/run-census.d.ts +67 -0
- package/dist/run-census.js +240 -0
- package/dist/run-computer.d.ts +6 -3
- package/dist/run-computer.js +4 -1
- package/dist/run-cut.d.ts +15 -6
- package/dist/run-cut.js +30 -9
- package/dist/run-files.d.ts +3 -0
- package/dist/run-files.js +3 -0
- package/dist/run-launch.d.ts +12 -1
- package/dist/run-launch.js +9 -10
- package/dist/run-learning-served.d.ts +4 -2
- package/dist/run-model-key.d.ts +8 -0
- package/dist/run-model-key.js +31 -1
- package/dist/run-reads-over-store.d.ts +24 -6
- package/dist/run-reads-over-store.js +67 -23
- package/dist/run-recordings.d.ts +5 -1
- package/dist/run-recordings.js +39 -14
- package/dist/run-timeline-served.d.ts +2 -0
- package/dist/run-timeline-served.js +2 -2
- package/dist/run-usage.d.ts +1 -0
- package/dist/run-view.d.ts +1 -2
- package/dist/run-view.js +2 -1
- package/dist/runnable.d.ts +10 -1
- package/dist/runnable.js +20 -8
- package/dist/runner/adapter-loop.d.ts +11 -0
- package/dist/runner/adapter-loop.js +58 -26
- package/dist/runner/adapter-run-spec.d.ts +55 -11
- package/dist/runner/adapter-run-spec.js +183 -52
- package/dist/runner/adapters/pi.js +25 -3
- package/dist/runner/beside.d.ts +9 -2
- package/dist/runner/beside.js +27 -9
- package/dist/runner/big-writes.js +1 -10
- package/dist/runner/claim-secrets.d.ts +27 -10
- package/dist/runner/claim-secrets.js +49 -22
- package/dist/runner/cli-run-spec.d.ts +6 -0
- package/dist/runner/cli-run-spec.js +97 -38
- package/dist/runner/computer-journal.d.ts +9 -1
- package/dist/runner/computer-journal.js +26 -0
- package/dist/runner/conformance/harness.js +1 -1
- package/dist/runner/feedback-round.js +3 -0
- package/dist/runner/harness-loops.d.ts +6 -5
- package/dist/runner/harness-loops.js +7 -7
- package/dist/runner/held.d.ts +24 -6
- package/dist/runner/journal-kinds.d.ts +5 -0
- package/dist/runner/journal-kinds.js +5 -0
- package/dist/runner/main.d.ts +11 -6
- package/dist/runner/main.js +19 -9
- package/dist/runner/mcp-claim.js +31 -3
- package/dist/runner/mcp-run.d.ts +9 -0
- package/dist/runner/mcp-run.js +11 -5
- package/dist/runner/model-route.d.ts +17 -0
- package/dist/runner/model-route.js +120 -2
- package/dist/runner/presence.d.ts +11 -1
- package/dist/runner/presence.js +7 -0
- package/dist/runner/reflect-hook.js +1 -1
- package/dist/runner/run-child-host.d.ts +22 -8
- package/dist/runner/run-child-host.js +128 -41
- package/dist/runner/run-child.js +5 -2
- package/dist/runner/runner.d.ts +17 -3
- package/dist/runner/runner.js +130 -34
- package/dist/runner/save-slots.js +2 -1
- package/dist/runner/store-port.d.ts +1 -1
- package/dist/runner/store-secrets.d.ts +6 -0
- package/dist/runner/store-secrets.js +49 -7
- package/dist/runner/subagent.d.ts +11 -0
- package/dist/runner/subagent.js +16 -38
- package/dist/runner/workflow-first.d.ts +35 -15
- package/dist/runner/workflow-first.js +38 -25
- package/dist/runner/workflow-spec.js +4 -3
- package/dist/runs-over-store.d.ts +15 -6
- package/dist/runs-over-store.js +33 -21
- package/dist/scrub-command.d.ts +73 -0
- package/dist/scrub-command.js +307 -0
- package/dist/secret-cut.d.ts +73 -0
- package/dist/secret-cut.js +291 -4
- package/dist/secret-rules.d.ts +143 -0
- package/dist/secret-rules.js +2564 -0
- package/dist/secrets.d.ts +7 -0
- package/dist/secrets.js +20 -0
- package/dist/server-keys.d.ts +20 -0
- package/dist/server-keys.js +25 -6
- package/dist/server.d.ts +65 -13
- package/dist/server.js +470 -157
- package/dist/skills/agentrun-serve/SKILL.md +4 -0
- package/dist/slots.d.ts +9 -0
- package/dist/slots.js +45 -0
- package/dist/store/index.d.ts +1 -1
- package/dist/store/index.js +1 -1
- package/dist/store/journal/storage.js +1 -1
- package/dist/store/postgres/migrations/0027_run_launch_census.sql +31 -0
- package/dist/store/postgres/migrations/0028_learning_ledger.sql +44 -0
- package/dist/store/schema.d.ts +23 -0
- package/dist/store/schema.js +58 -2
- package/dist/store/seal.d.ts +6 -1
- package/dist/store/seal.js +5 -3
- package/dist/store/sqlite/run-launch.d.ts +9 -1
- package/dist/store/sqlite/run-launch.js +24 -13
- package/dist/store/sqlite/run-patch.d.ts +12 -2
- package/dist/store/sqlite/run-patch.js +42 -5
- package/dist/store/sqlite/store.js +105 -12
- package/dist/store/store.d.ts +57 -2
- package/dist/store/store.js +20 -0
- package/dist/store-reads/learning.d.ts +84 -124
- package/dist/store-reads/learning.js +210 -274
- package/dist/store-view.d.ts +3 -0
- package/dist/store-view.js +3 -3
- package/dist/suggestions.js +3 -2
- package/dist/template-eval.d.ts +39 -9
- package/dist/template-eval.js +191 -60
- package/dist/template-import.js +6 -2
- package/dist/templates.d.ts +10 -2
- package/dist/templates.js +28 -8
- package/dist/timeline/labels.d.ts +6 -4
- package/dist/timeline/labels.js +18 -6
- package/dist/timeline/timeline.d.ts +5 -0
- package/dist/timeline/timeline.js +14 -8
- package/dist/timeline/util.js +1 -1
- package/dist/timeline/web-calls.d.ts +22 -7
- package/dist/timeline/web-calls.js +106 -35
- package/dist/tool-gate.d.ts +42 -3
- package/dist/tool-gate.js +66 -5
- package/dist/tool-keys-store.d.ts +2 -4
- package/dist/tool-keys-store.js +5 -21
- package/dist/tool-keys.d.ts +2 -2
- package/dist/tool-keys.js +2 -2
- package/dist/tool-list.js +1 -1
- package/dist/tool-plane.d.ts +2 -2
- package/dist/tool-plane.js +35 -33
- package/dist/tools.js +13 -7
- package/dist/trigger-targets.d.ts +4 -0
- package/dist/trigger-targets.js +9 -2
- package/dist/triggers.d.ts +67 -4
- package/dist/triggers.js +171 -22
- package/dist/until.d.ts +3 -0
- package/dist/until.js +11 -0
- package/dist/web-fake.d.ts +1 -3
- package/dist/web-fake.js +7 -18
- package/dist/web-fetch.d.ts +6 -72
- package/dist/web-fetch.js +11 -265
- package/dist/web-run.d.ts +31 -0
- package/dist/web-run.js +90 -0
- package/dist/web-save.d.ts +10 -13
- package/dist/web-save.js +13 -55
- package/dist/web-search.d.ts +9 -127
- package/dist/web-search.js +23 -259
- package/dist/wires.d.ts +13 -0
- package/dist/wires.js +11 -0
- package/dist/workflow-run.d.ts +5 -4
- package/dist/workflow-run.js +5 -4
- package/dist/workspace/attached-computers.d.ts +32 -9
- package/dist/workspace/attached-computers.js +77 -13
- package/dist/workspace/computer-deps.d.ts +8 -2
- package/dist/workspace/computer-deps.js +10 -3
- package/dist/workspace/computer-for-claim.d.ts +12 -1
- package/dist/workspace/computer-for-claim.js +83 -11
- package/dist/workspace/computer-kept.d.ts +3 -0
- package/dist/workspace/computer-kept.js +11 -2
- package/dist/workspace/index.d.ts +1 -1
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/kind-skills.d.ts +4 -0
- package/dist/workspace/kind-skills.js +9 -0
- package/dist/workspace/paired-route.d.ts +91 -0
- package/dist/workspace/paired-route.js +153 -0
- package/dist/workspace/synced-folder.js +3 -4
- package/dist/wrapped.d.ts +20 -6
- package/dist/wrapped.js +66 -25
- package/package.json +17 -11
- package/skills/agentrun-serve/SKILL.md +4 -0
- package/templates/README.md +32 -10
- package/dist/console/assets/index-CQ3cJRzI.js +0 -746
- package/dist/console/assets/index-DKkaS3a0.css +0 -1
- package/dist/exa-keyless.d.ts +0 -34
- package/dist/exa-keyless.js +0 -118
- package/dist/firecrawl-keyless.d.ts +0 -41
- package/dist/firecrawl-keyless.js +0 -152
- package/dist/keyless-mcp.d.ts +0 -36
- package/dist/keyless-mcp.js +0 -127
- package/dist/text-urls.d.ts +0 -38
- package/dist/text-urls.js +0 -170
- package/dist/web-fetch-direct.d.ts +0 -119
- package/dist/web-fetch-direct.js +0 -190
package/dist/claude-login.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
// Claude Code's login on this machine as claude-agent's model source: a person's own Claude plan, used by the pinned
|
|
2
|
-
// Claude Code CLI as that CLI always uses it. AgentRun never
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// the
|
|
2
|
+
// Claude Code CLI as that CLI always uses it. AgentRun never copies, stores, proxies or relays the login or a browser
|
|
3
|
+
// code: the CLI keeps it in its own files and calls Anthropic with it itself (the harness's login.ts). This server tells
|
|
4
|
+
// the CLI where its login is, asks it for its status (`claude auth status`), and keeps one setting: that the owner turned
|
|
5
|
+
// the mode on, where the login is, and on which machine. Where each run is a user of its own, the runner reads AgentRun's
|
|
6
|
+
// own login folder, renews it there in the CLI's place, with the CLI's own request and lock, and a run gets only its
|
|
7
|
+
// access token (claude-login-token.ts).
|
|
6
8
|
//
|
|
7
9
|
// One switch (AGENTRUN_CLAUDE_CODE_LOGIN): unset, the mode is there only on a server that listens on loopback, so a
|
|
8
10
|
// server bound past it (0.0.0.0 in a container, a hosted service) has it off with nothing to set; `off` or `on` says so
|
|
@@ -10,8 +12,9 @@
|
|
|
10
12
|
// harness selection reads no setting, so claude-agent runs on the model keys as it does without the mode. The mode is
|
|
11
13
|
// for a server one person runs: it is refused where more than one person can start runs, `on` included, and the owner
|
|
12
14
|
// confirms they alone run agents here. It runs only in a runner place, on the machine the owner turned it on for.
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
16
|
+
import { accessSync, chmodSync, closeSync, constants, existsSync, fstatSync, linkSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
17
|
+
import { homedir, userInfo } from "node:os";
|
|
15
18
|
import { delimiter, isAbsolute, join } from "node:path";
|
|
16
19
|
import { facts as claudeFacts } from "@parcha/agentrun-harness-claude/facts";
|
|
17
20
|
import { registerSecretWrite } from "./secret-writes.js";
|
|
@@ -43,50 +46,154 @@ const LOGIN_HARNESSES = Object.freeze([LOGIN_HARNESS]);
|
|
|
43
46
|
export const CLAUDE_LOGIN_PROVIDER = "claude-login";
|
|
44
47
|
/** The model a run on the login gets when it names none. */
|
|
45
48
|
export const LOGIN_DEFAULT_MODEL = "anthropic/claude-sonnet-5-5";
|
|
49
|
+
/** Whether a run of the login's harness that names `model` goes on the login: one naming none (it gets
|
|
50
|
+
* LOGIN_DEFAULT_MODEL, Anthropic's) or one of Anthropic's own (`anthropic/<id>`). Any other, Anthropic's through
|
|
51
|
+
* OpenRouter (`openrouter/anthropic/<id>`) included, goes on its provider's key whatever the login says. providerFor
|
|
52
|
+
* puts a run on the login by this rule, and loginOnRunnerOnly offers the move to the runner by it. The console reads no
|
|
53
|
+
* server code: its lib/claudeLogin.ts loginServes copies this, and both tests name the same cases. */
|
|
54
|
+
export const loginServes = (model) => !model || model.startsWith("anthropic/");
|
|
46
55
|
/** Where the login is: AgentRun's own directory in its data (`agentrun`, the default), or this machine's own Claude Code
|
|
47
56
|
* login (`machine`, which the owner opts into by name). */
|
|
48
57
|
export const LOGIN_PLACES = ["agentrun", "machine"];
|
|
58
|
+
/** The file in the data folder that holds this machine's id (machineId). */
|
|
59
|
+
export const MACHINE_ID_FILE = "machine-id";
|
|
60
|
+
const MACHINE_ID = /^[0-9a-f]{32}$/;
|
|
61
|
+
/** This machine's id, which names the machine the login was turned on for: a random one, made once in
|
|
62
|
+
* `<data>/machine-id` (0600) and read back by every later start. Never the hostname, which moves with the network (a
|
|
63
|
+
* laptop's at home is not its name at the office), while the data folder stays. A missing file, or one that holds no
|
|
64
|
+
* 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
|
|
65
|
+
* again. The file appears whole: written aside, then linked in place, so a start beside this one that made an id first
|
|
66
|
+
* keeps its own, and both read that one. */
|
|
67
|
+
export function machineId(data) {
|
|
68
|
+
const file = join(data, MACHINE_ID_FILE);
|
|
69
|
+
const read = () => {
|
|
70
|
+
try {
|
|
71
|
+
const id = readFileSync(file, "utf8").trim();
|
|
72
|
+
return MACHINE_ID.test(id) ? id : undefined;
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const known = read();
|
|
79
|
+
if (known)
|
|
80
|
+
return known;
|
|
81
|
+
const id = randomBytes(16).toString("hex");
|
|
82
|
+
const aside = `${file}.${process.pid}.${randomBytes(4).toString("hex")}`;
|
|
83
|
+
writeFileSync(aside, `${id}\n`, { mode: 0o600, flag: "wx" });
|
|
84
|
+
try {
|
|
85
|
+
// The umask may take more than 0600 leaves; never less.
|
|
86
|
+
chmodSync(aside, 0o600);
|
|
87
|
+
try {
|
|
88
|
+
linkSync(aside, file);
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
if (error.code !== "EEXIST")
|
|
92
|
+
throw error;
|
|
93
|
+
// Another start made one first: that one. Else what is there holds no id, and this one takes its place.
|
|
94
|
+
const theirs = read();
|
|
95
|
+
if (theirs)
|
|
96
|
+
return theirs;
|
|
97
|
+
renameSync(aside, file);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
rmSync(aside, { force: true });
|
|
102
|
+
}
|
|
103
|
+
return id;
|
|
104
|
+
}
|
|
49
105
|
/** 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
|
|
50
106
|
* config dir is `CLAUDE_CONFIG_DIR` when set, else `~/.claude`, and its credentials are kept there too unless
|
|
51
107
|
* `CLAUDE_SECURESTORAGE_CONFIG_DIR` says otherwise. Read from the server's env, so a Claude Code started with another
|
|
52
108
|
* `CLAUDE_CONFIG_DIR` is found only when AgentRun was started with it as well. A relative one is not taken: the CLI would
|
|
53
|
-
* read it against a working directory this server does not share. `source` says which rule gave the folder.
|
|
109
|
+
* read it against a working directory this server does not share. `source` says which rule gave the folder.
|
|
110
|
+
* `credentials`: the server's `CLAUDE_SECURESTORAGE_CONFIG_DIR` when it sets one the CLI takes as it is (an absolute
|
|
111
|
+
* path, or empty: `~/.claude`, and on macOS the Keychain item with no suffix), where the CLI keeps the tokens apart
|
|
112
|
+
* from the folder; a relative one is not taken, as above. */
|
|
54
113
|
export function machineFolder(env = process.env) {
|
|
114
|
+
const secure = env.CLAUDE_SECURESTORAGE_CONFIG_DIR;
|
|
115
|
+
const credentials = secure === "" || (secure && isAbsolute(secure)) ? { credentials: secure } : {};
|
|
55
116
|
if (env.CLAUDE_CONFIG_DIR && isAbsolute(env.CLAUDE_CONFIG_DIR))
|
|
56
|
-
return { folder: env.CLAUDE_CONFIG_DIR, source: "CLAUDE_CONFIG_DIR" };
|
|
57
|
-
|
|
58
|
-
|
|
117
|
+
return { folder: env.CLAUDE_CONFIG_DIR, source: "CLAUDE_CONFIG_DIR", ...credentials };
|
|
118
|
+
return { folder: join(homeOf(env).home, ".claude"), source: "default", ...credentials };
|
|
119
|
+
}
|
|
120
|
+
/** The person's home: the home of the user this server runs as (`uid`, its effective uid), whose files the login's CLI
|
|
121
|
+
* writes. The env's `HOME` when it is an absolute path to a folder that user owns, else that user's own home as the
|
|
122
|
+
* system has it (`os.userInfo()`), when it owns that. So a server under a sudo that keeps `HOME` (root, with the
|
|
123
|
+
* person's) keeps its login in root's home, never root-owned files in the person's, and a service account that
|
|
124
|
+
* inherited someone's `HOME` reads its own. Neither: `refused` says why in words, and `home` is only what the view
|
|
125
|
+
* shows (nothing is written there). Where there is no uid (Windows), the env's `HOME` when absolute, else
|
|
126
|
+
* `os.homedir()`. */
|
|
127
|
+
export function homeOf(env, uid = process.geteuid?.()) {
|
|
128
|
+
const given = env.HOME && isAbsolute(env.HOME) ? env.HOME : undefined;
|
|
129
|
+
if (uid === undefined)
|
|
130
|
+
return { home: given ?? homedir() };
|
|
131
|
+
const owned = (dir) => {
|
|
132
|
+
if (!dir)
|
|
133
|
+
return false;
|
|
134
|
+
try {
|
|
135
|
+
const stat = statSync(dir);
|
|
136
|
+
return stat.isDirectory() && stat.uid === uid;
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
if (owned(given))
|
|
143
|
+
return { home: given };
|
|
144
|
+
let own;
|
|
145
|
+
try {
|
|
146
|
+
own = userInfo().homedir || undefined;
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
// No entry for this user on this system.
|
|
150
|
+
}
|
|
151
|
+
if (owned(own))
|
|
152
|
+
return { home: own };
|
|
153
|
+
return {
|
|
154
|
+
home: own ?? given ?? homedir(),
|
|
155
|
+
refused: `Claude Code's login on this machine needs a home this server's user owns: it runs as uid ${uid}, and neither its HOME (${env.HOME || "unset"}) nor that user's own home (${own ?? "none on this system"}) is a folder it owns. Start the server as the person whose Claude plan it is.`,
|
|
156
|
+
};
|
|
59
157
|
}
|
|
60
158
|
/** The directories of a login at `place`: AgentRun's in `data`, or this machine's (machineFolder, and the Anthropic
|
|
61
159
|
* profiles where the CLI looks for them: `ANTHROPIC_CONFIG_DIR`, else `$XDG_CONFIG_HOME/anthropic`, else
|
|
62
160
|
* `~/.config/anthropic`). This machine's on the CLI's default folder carries the person's home too, so the harness
|
|
63
161
|
* points the CLI at it as their own CLI runs, with no folder variable (the harness's signInEnv and loginEnv). This
|
|
64
|
-
* machine's
|
|
65
|
-
* (the
|
|
162
|
+
* machine's with a credentials dir in the server's env (machineFolder's `credentials`) carries it as `secure`, absolute
|
|
163
|
+
* (empty: `~/.claude`, `unsuffixed`), so the CLI keeps the tokens where the person's own CLI with it does. A status
|
|
164
|
+
* check at either place runs on a copy of the account file (the harness's claudeLoginStatus). Every login carries the
|
|
165
|
+
* person's home as `userHome`, read here once (homeOf), so the status check, the sign-in and a run on macOS all run in
|
|
166
|
+
* that one home whatever reads it later (the harness's personHome). */
|
|
66
167
|
export function loginAt(place, data, env = process.env) {
|
|
168
|
+
const { home } = homeOf(env);
|
|
67
169
|
if (place === "agentrun")
|
|
68
|
-
return { dir: join(data, "claude-login"), profiles: join(data, "claude-login", "anthropic") };
|
|
69
|
-
const home = env.HOME && isAbsolute(env.HOME) ? env.HOME : homedir();
|
|
170
|
+
return { dir: join(data, "claude-login"), profiles: join(data, "claude-login", "anthropic"), userHome: home };
|
|
70
171
|
const config = env.XDG_CONFIG_HOME && isAbsolute(env.XDG_CONFIG_HOME) ? env.XDG_CONFIG_HOME : join(home, ".config");
|
|
71
|
-
const { folder, source } = machineFolder(env);
|
|
72
|
-
|
|
172
|
+
const { folder, source, credentials } = machineFolder(env);
|
|
173
|
+
const secure = credentials === undefined ? {} : credentials === "" ? { secure: join(home, ".claude"), unsuffixed: true } : { secure: credentials };
|
|
174
|
+
return { dir: folder, profiles: env.ANTHROPIC_CONFIG_DIR && isAbsolute(env.ANTHROPIC_CONFIG_DIR) ? env.ANTHROPIC_CONFIG_DIR : join(config, "anthropic"), ...(source === "default" ? { home } : {}), ...secure, userHome: home };
|
|
73
175
|
}
|
|
74
176
|
/** The setting as saved, or undefined when the mode is not on (or the row is not one this reads). */
|
|
75
177
|
export async function claudeLoginSetting(settings) {
|
|
76
178
|
const row = (await settings.get(CLAUDE_LOGIN_SETTING));
|
|
77
|
-
if (!row || row.v !== 1 || !LOGIN_PLACES.includes(row.place) || typeof row.
|
|
179
|
+
if (!row || row.v !== 1 || !LOGIN_PLACES.includes(row.place) || (row.machine !== undefined && typeof row.machine !== "string") || row.only_me !== true)
|
|
78
180
|
return undefined;
|
|
79
181
|
return row;
|
|
80
182
|
}
|
|
81
|
-
/**
|
|
183
|
+
/** Whether `setting` was turned on for this machine: the one whose data folder (`data`) holds its id (machineId). */
|
|
184
|
+
const thisMachine = (setting, data) => setting.machine === machineId(data);
|
|
185
|
+
/** The login a run here goes on: the setting's, when the switch is not off, this is the machine it was turned on for,
|
|
186
|
+
* and the server's user owns a home for it (homeOf). */
|
|
82
187
|
export function loginOf(setting, data, env) {
|
|
83
|
-
if (!setting || claudeLoginOff(env) || setting
|
|
188
|
+
if (!setting || claudeLoginOff(env) || !thisMachine(setting, data) || homeOf(env).refused)
|
|
84
189
|
return undefined;
|
|
85
190
|
return loginAt(setting.place, data, env);
|
|
86
191
|
}
|
|
87
192
|
const LOGIN = Symbol.for("agentrun.claude-login");
|
|
193
|
+
/** Whether `login` is a reader of its access token, not its directories. */
|
|
194
|
+
export const loginByToken = (login) => typeof login.token === "function";
|
|
88
195
|
/** `env` carrying `login` (none: carrying none), as an enumerable symbol property: a spread keeps it, a spawn never sees
|
|
89
|
-
* it (it is no variable), and it holds
|
|
196
|
+
* it (it is no variable), and it holds two directory names or a token reader, never a token. */
|
|
90
197
|
export function withClaudeLogin(env, login) {
|
|
91
198
|
if (login)
|
|
92
199
|
Object.defineProperty(env, LOGIN, { value: Object.freeze({ ...login }), enumerable: true, configurable: true, writable: false });
|
|
@@ -109,6 +216,16 @@ export function claudeLoginOf(env) {
|
|
|
109
216
|
}
|
|
110
217
|
/** Whether `harness` runs on the login `env` carries. */
|
|
111
218
|
export const onClaudeLogin = (env, harness) => LOGIN_HARNESSES.includes(harness) && claudeLoginOf(env) !== undefined;
|
|
219
|
+
/** For a run refused for its model: the words that say why the login `env` carries does not run it, when its harness
|
|
220
|
+
* runs on that login, the login would run its `model` (loginServes: Anthropic's own, or none), and its place is a box
|
|
221
|
+
* (the login is a model only on the runner, loginForRun), naming the box and the move that runs it there; else
|
|
222
|
+
* undefined. A model the login never runs (an OpenRouter one) still needs its key after a move, so none is offered. The
|
|
223
|
+
* login's state is not asked: a signed-out one fails a run on the runner in its own words. */
|
|
224
|
+
export function loginOnRunnerOnly(env, harness, model, place) {
|
|
225
|
+
if (harness === undefined || !onClaudeLogin(env, harness) || !loginServes(model) || place === undefined || place.mode === "runner")
|
|
226
|
+
return undefined;
|
|
227
|
+
return `Claude Code's login on this machine runs agents on the runner only, and this agent runs in the ${place.box ?? place.mode} box: move it to the runner to run it on the login, or add a key.`;
|
|
228
|
+
}
|
|
112
229
|
/** The CLI that answers for a login: the pinned one this server installed for claude-agent (`pinned`), else a `claude` on
|
|
113
230
|
* PATH, else none. */
|
|
114
231
|
export function loginCli(pinned, env) {
|
|
@@ -137,8 +254,19 @@ export function whyNotOneOwner(facts) {
|
|
|
137
254
|
return "This server has no owner (the API-only profile), so anyone with its token starts runs: a Claude plan is one person's.";
|
|
138
255
|
if (facts.tokens > 1)
|
|
139
256
|
return `This server takes ${facts.tokens} API tokens, so more than one person can start runs here: a Claude plan is one person's.`;
|
|
257
|
+
// A key made in the console for an app (api-keys.ts) is one more caller that starts runs, as a second token is.
|
|
258
|
+
// `keysDormant`: the keys open nothing on this server (api-keys.ts dormant), and would at a start that takes them.
|
|
259
|
+
const keys = facts.keys ?? 0;
|
|
260
|
+
if (keys > 0) {
|
|
261
|
+
const [what, it] = keys === 1 ? ["an API key", "it"] : [`${keys} API keys`, "them"];
|
|
262
|
+
return facts.keysDormant
|
|
263
|
+
? `This server keeps ${what} made for apps (Settings → AgentRun API keys). ${keys === 1 ? "It opens" : "They open"} nothing here, and would open the server at a start that takes keys again: a Claude plan is one person's. Revoke ${it} first.`
|
|
264
|
+
: `This server has ${what} made for apps (Settings → AgentRun API keys), so more than one caller can start runs here: a Claude plan is one person's. Revoke ${it} first.`;
|
|
265
|
+
}
|
|
140
266
|
return undefined;
|
|
141
267
|
}
|
|
268
|
+
/** 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. */
|
|
269
|
+
export 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 → Model keys first.";
|
|
142
270
|
/** The account's words, as the view shows them. */
|
|
143
271
|
export const accountOf = (status) => ({ ...(status.method ? { method: status.method } : {}), ...(status.email ? { email: status.email } : {}), ...(status.organization ? { organization: status.organization } : {}), ...(status.plan ? { plan: status.plan } : {}) });
|
|
144
272
|
/** The body of a turn-on, checked: where the login is, and the owner's word that they alone run agents here. */
|
|
@@ -150,12 +278,12 @@ export function turnOnBody(body) {
|
|
|
150
278
|
return { problem: "only_me must be true: confirm you alone run agents on this server, since its runs would use your Claude plan" };
|
|
151
279
|
return { place: given.place ?? "agentrun" };
|
|
152
280
|
}
|
|
153
|
-
/** The setting a turn-on saves, for this machine. AgentRun's own login directory is made (0700) so the CLI
|
|
154
|
-
* login there; nothing in it is read. */
|
|
281
|
+
/** The setting a turn-on saves, for this machine (machineId). AgentRun's own login directory is made (0700) so the CLI
|
|
282
|
+
* can keep its login there; nothing in it is read. */
|
|
155
283
|
export function settingFor(place, data) {
|
|
156
284
|
if (place === "agentrun")
|
|
157
285
|
mkdirSync(join(data, "claude-login"), { recursive: true, mode: 0o700 });
|
|
158
|
-
return { v: 1, place,
|
|
286
|
+
return { v: 1, place, machine: machineId(data), only_me: true };
|
|
159
287
|
}
|
|
160
288
|
/** The login a run of `harness` on this runner goes on: the saved setting's, read now, when the switch is not off, the
|
|
161
289
|
* harness runs on it, the run is in a runner place (`place`), and this is the machine it was turned on for; else none,
|
|
@@ -168,6 +296,15 @@ export async function loginForRun(settings, data, env, harness, place) {
|
|
|
168
296
|
/** The routes here (server.ts): every one answers 404, by its name, while the mode is off (`why`). */
|
|
169
297
|
export const CLAUDE_LOGIN_ROUTES = new Set(["getClaudeLogin", "turnOnClaudeLogin", "turnOffClaudeLogin", "startClaudeLogin"]);
|
|
170
298
|
export const claudeLoginRouteOff = (name, why) => `${name}: Claude Code's login on this machine is off on this server: ${why}.`;
|
|
299
|
+
/** Why the routes may not turn the mode on or sign in now: the server's own refusal, else no home its user owns. */
|
|
300
|
+
const refusedHere = (deps) => deps.refused() ?? homeOf(deps.env, deps.uid).refused;
|
|
301
|
+
/** What the one-owner rule gives the login's routes over a server's keys: `refused`, which counts the keys each time
|
|
302
|
+
* it is asked, and `turn`, the keys' own. The server's routes take both from here, and so does the test that runs the
|
|
303
|
+
* turn-on against a held revocation: neither is a value read once, or a turn of its own. */
|
|
304
|
+
export const oneOwnerDeps = (facts, keys) => ({
|
|
305
|
+
refused: () => whyNotOneOwner({ ...facts, keys: keys.count }),
|
|
306
|
+
turn: (work) => keys.inTurn(work),
|
|
307
|
+
});
|
|
171
308
|
/** A route's refusal in words, answered as its `code` (server.ts makes it an ApiError). */
|
|
172
309
|
export class ClaudeLoginRefused extends Error {
|
|
173
310
|
code;
|
|
@@ -183,86 +320,233 @@ export async function claudeLoginView(deps) {
|
|
|
183
320
|
const login = loginAt(place, deps.data, deps.env);
|
|
184
321
|
const cli = loginCli(await deps.pinned(), deps.env);
|
|
185
322
|
const signIn = deps.signIns.view();
|
|
186
|
-
const
|
|
323
|
+
const homeless = homeOf(deps.env, deps.uid).refused;
|
|
324
|
+
const refused = deps.refused() ?? homeless;
|
|
325
|
+
const base = { place, command: (await deps.claude()).claudeLoginCommand(login, cli ?? "claude"), can_start: deps.sameMachine && cli !== undefined && refused === undefined, ...(refused ? { refused } : {}), folder: login.dir, machine: machineFolder(deps.env), ...(signIn ? { sign_in: signIn } : {}) };
|
|
187
326
|
if (!setting)
|
|
188
327
|
return { state: "off", ...base };
|
|
189
|
-
if (setting.
|
|
328
|
+
if (!thisMachine(setting, deps.data))
|
|
190
329
|
return { state: "other_machine", ...base, can_start: false };
|
|
330
|
+
// No home the server's user owns: no login here (loginOf), and the CLI is not asked, since its check runs in that home.
|
|
331
|
+
if (homeless)
|
|
332
|
+
return { state: "off", ...base };
|
|
191
333
|
if (!cli)
|
|
192
334
|
return { state: "no_cli", ...base };
|
|
193
335
|
try {
|
|
336
|
+
// Asked once: a person waits on this read. Its answer is a run's too (loginBeforeRun), so one who just saw the
|
|
337
|
+
// login signed in here has a run on a slow host go on.
|
|
194
338
|
const said = await (await deps.claude()).claudeLoginStatus(cli, login, pathOf(deps.env));
|
|
339
|
+
noteAnswer(login, said, Date.now(), deps.data);
|
|
195
340
|
return said.signedIn ? { state: "signed_in", ...base, account: accountOf(said) } : { state: "signed_out", ...base };
|
|
196
341
|
}
|
|
197
342
|
catch {
|
|
198
343
|
return { state: "unknown", ...base };
|
|
199
344
|
}
|
|
200
345
|
}
|
|
201
|
-
/** PUT /settings/claude-login: turned on, refused in one line where more than one person can start runs.
|
|
346
|
+
/** PUT /settings/claude-login: turned on, refused in one line where more than one person can start runs. A turn-on that
|
|
347
|
+
* moves the login to another place first ends a sign-in this server started that still waits: its CLI signs in at the
|
|
348
|
+
* old place, which the login no longer reads. One at the same place leaves it waiting, as it still signs in there. */
|
|
202
349
|
export async function turnOnClaudeLogin(deps, body) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
350
|
+
// `refused` is asked only in the keys' turn (deps.turn), where a key being made or revoked has landed, or been undone.
|
|
351
|
+
// Asked once before the sign-in is touched, so a turn-on refused or malformed then leaves a waiting sign-in as it was;
|
|
352
|
+
// and again with the write, in one turn, so no key is made between the decision and the setting. A key made between
|
|
353
|
+
// the two turns, on a turn-on that moves the place, is the one case that both stops the sign-in and refuses: nothing
|
|
354
|
+
// is written, and the person starts the sign-in again once the key is revoked. The sign-in's stop and
|
|
355
|
+
// what follows the write (the reading again, the CLI's status) are outside the keys' turn, so a key's revocation never
|
|
356
|
+
// waits on the CLI.
|
|
357
|
+
const refusal = () => {
|
|
358
|
+
const refused = refusedHere(deps);
|
|
359
|
+
if (refused)
|
|
360
|
+
throw new ClaudeLoginRefused("forbidden", refused);
|
|
361
|
+
};
|
|
362
|
+
const asked = await deps.turn(async () => {
|
|
363
|
+
refusal();
|
|
364
|
+
const asked = turnOnBody(body);
|
|
365
|
+
if ("problem" in asked)
|
|
366
|
+
throw new ClaudeLoginRefused("input_invalid", asked.problem);
|
|
367
|
+
return asked;
|
|
368
|
+
});
|
|
369
|
+
await deps.signIns.serial(async (turn) => {
|
|
370
|
+
if ((await claudeLoginSetting(deps.settings))?.place !== asked.place)
|
|
371
|
+
await turn.stop();
|
|
372
|
+
await deps.turn(async () => {
|
|
373
|
+
refusal();
|
|
374
|
+
const setting = settingFor(asked.place, deps.data);
|
|
375
|
+
await deps.settings.put(CLAUDE_LOGIN_SETTING, setting);
|
|
376
|
+
deps.saved(loginOf(setting, deps.data, deps.env));
|
|
377
|
+
});
|
|
378
|
+
});
|
|
209
379
|
await deps.changed();
|
|
210
380
|
return claudeLoginView(deps);
|
|
211
381
|
}
|
|
212
382
|
/** DELETE /settings/claude-login: turned off, and a sign-in this server started that still waits is ended. The login
|
|
213
383
|
* itself stays as the CLI keeps it; nothing here signs it out. */
|
|
214
384
|
export async function turnOffClaudeLogin(deps) {
|
|
215
|
-
await deps.signIns.
|
|
216
|
-
|
|
385
|
+
await deps.signIns.serial(async (turn) => {
|
|
386
|
+
await turn.stop();
|
|
387
|
+
await deps.settings.remove(CLAUDE_LOGIN_SETTING);
|
|
388
|
+
deps.saved(undefined);
|
|
389
|
+
});
|
|
217
390
|
await deps.changed();
|
|
218
391
|
return claudeLoginView(deps);
|
|
219
392
|
}
|
|
220
393
|
/** POST /settings/claude-login/login: the CLI's own sign-in, started on this machine, where it opens a browser, with the
|
|
221
394
|
* shown line's own variables (the harness's signInEnv), so it signs in where the status reads. A sign-in that
|
|
222
395
|
* still waits is ended first (deps.signIns). The browser code goes to the CLI, never here; the answer is the view as it
|
|
223
|
-
* is when the sign-in starts, its `sign_in` waiting. */
|
|
396
|
+
* is when the sign-in starts, its `sign_in` waiting. The setting is read, and the CLI started on it, in one turn. */
|
|
224
397
|
export async function startLoginHere(deps) {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
398
|
+
const refused = refusedHere(deps);
|
|
399
|
+
if (refused)
|
|
400
|
+
throw new ClaudeLoginRefused("forbidden", refused);
|
|
401
|
+
await deps.signIns.serial(async (signIn) => {
|
|
402
|
+
const setting = await claudeLoginSetting(deps.settings);
|
|
403
|
+
if (!setting)
|
|
404
|
+
throw new ClaudeLoginRefused("agent_not_runnable", "Turn on Claude Code's login on this machine first.");
|
|
405
|
+
if (!thisMachine(setting, deps.data))
|
|
406
|
+
throw new ClaudeLoginRefused("agent_not_runnable", "It was turned on for another machine: sign in there.");
|
|
407
|
+
if (!deps.sameMachine)
|
|
408
|
+
throw new ClaudeLoginRefused("forbidden", "The CLI's sign-in opens a browser on the server's machine: run the command shown in a terminal there.");
|
|
409
|
+
const cli = loginCli(await deps.pinned(), deps.env);
|
|
410
|
+
if (!cli)
|
|
411
|
+
throw new ClaudeLoginRefused("agent_not_runnable", "No Claude Code CLI here yet: claude-agent's first run installs it, or run the command shown.");
|
|
412
|
+
// The person's home (the login's `userHome`) and desktop, so the CLI can open their browser.
|
|
413
|
+
const claude = await deps.claude();
|
|
414
|
+
const login = loginAt(setting.place, deps.data, deps.env);
|
|
415
|
+
await signIn.start((timeoutMs) => claude.startClaudeLogin(cli, login, { ...pathOf(deps.env), display: deps.env, timeoutMs }));
|
|
416
|
+
});
|
|
241
417
|
return claudeLoginView(deps);
|
|
242
418
|
}
|
|
243
419
|
/** At the server's start: a setting saved where the mode is now off or refused (`why`: the switch, a listen address past
|
|
244
|
-
* loopback, a second token, the API-only profile) goes, so no runner, in this process or another, runs on it; the reason
|
|
245
|
-
* is logged. */
|
|
246
|
-
export async function dropRefusedLogin(settings, why, log) {
|
|
247
|
-
if (!why
|
|
420
|
+
* loopback, a second token, the API-only profile, no home the server's user owns) goes, so no runner, in this process or another, runs on it; the reason
|
|
421
|
+
* is logged. A turn on `signIns`' queue, as every change of the setting is. */
|
|
422
|
+
export async function dropRefusedLogin(signIns, settings, why, log) {
|
|
423
|
+
if (!why)
|
|
248
424
|
return;
|
|
249
|
-
await
|
|
250
|
-
|
|
425
|
+
await signIns.serial(async () => {
|
|
426
|
+
if (!(await claudeLoginSetting(settings)))
|
|
427
|
+
return;
|
|
428
|
+
await settings.remove(CLAUDE_LOGIN_SETTING);
|
|
429
|
+
log(`Claude Code's login on this machine turned off: ${why}`);
|
|
430
|
+
});
|
|
251
431
|
}
|
|
252
432
|
const pathOf = (env) => (env.PATH ? { path: env.PATH } : {});
|
|
433
|
+
/** How long a signed-in answer stands in for a check that gives none (loginBeforeRun). */
|
|
434
|
+
export const LOGIN_ANSWER_STANDS_MS = 10 * 60_000;
|
|
435
|
+
/** The folder in the data folder that holds, per login, when it last answered signed in (noteAnswer). The server and its
|
|
436
|
+
* runner may be two processes on one data folder (`--no-runner` beside `agentrun-serve runner`): the Settings view's
|
|
437
|
+
* answer is the server's, a run's check the runner's, so the answer is kept where both read it. */
|
|
438
|
+
export const LOGIN_ANSWERS_DIR = "claude-login-answers";
|
|
439
|
+
/** What tells one login from another here: its folder and its credentials dir. */
|
|
440
|
+
const loginKey = (login) => JSON.stringify([login.dir, login.secure ?? null, login.unsuffixed === true]);
|
|
441
|
+
/** The file of `login`'s answer in `data`: named by a hash of this machine's id (machineId) and the login, so nothing of
|
|
442
|
+
* the login's paths is in its name, and an answer is only ever this machine's and this login's. */
|
|
443
|
+
const answerFile = (data, login) => join(data, LOGIN_ANSWERS_DIR, createHash("sha256").update(JSON.stringify([machineId(data), loginKey(login)])).digest("hex").slice(0, 32));
|
|
444
|
+
/** The most an answer file holds: a time in milliseconds and a newline. A longer file is no note of ours. */
|
|
445
|
+
const ANSWER_MAX_BYTES = 32;
|
|
446
|
+
/** The answers folder in `data`, made 0700, or tightened to 0700 when it was there wider; false when what is at its name
|
|
447
|
+
* is not a folder (a link, say), which a note is never written through. */
|
|
448
|
+
function answersFolder(data) {
|
|
449
|
+
const dir = join(data, LOGIN_ANSWERS_DIR);
|
|
450
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
451
|
+
const at = lstatSync(dir);
|
|
452
|
+
if (!at.isDirectory())
|
|
453
|
+
return false;
|
|
454
|
+
if ((at.mode & 0o777) !== 0o700)
|
|
455
|
+
chmodSync(dir, 0o700);
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
/** Notes what the CLI said of `login` at `now` in `data`: signed in, the time, alone, in a file of its own (0600, in a
|
|
459
|
+
* folder of 0700, answersFolder), written aside and renamed in place so a reader never sees half of it, the aside file
|
|
460
|
+
* removed when the rename fails; signed out, the file goes (the earlier time with it). The answer only spares a later
|
|
461
|
+
* run a refusal, so a note that can't be written is left out. */
|
|
462
|
+
function noteAnswer(login, said, now, data) {
|
|
463
|
+
try {
|
|
464
|
+
const file = answerFile(data, login);
|
|
465
|
+
if (!said.signedIn)
|
|
466
|
+
return void rmSync(file, { force: true });
|
|
467
|
+
if (!answersFolder(data))
|
|
468
|
+
return;
|
|
469
|
+
const aside = `${file}.${process.pid}.${randomBytes(4).toString("hex")}`;
|
|
470
|
+
try {
|
|
471
|
+
writeFileSync(aside, `${now}\n`, { mode: 0o600, flag: "wx" });
|
|
472
|
+
renameSync(aside, file);
|
|
473
|
+
}
|
|
474
|
+
finally {
|
|
475
|
+
rmSync(aside, { force: true });
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
catch {
|
|
479
|
+
// Not noted: a run whose checks give no answer is refused, as with no earlier answer.
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
/** When `login` last answered signed in on this machine (noteAnswer), or undefined. Only a regular file at the answer's
|
|
483
|
+
* name, of at most ANSWER_MAX_BYTES, is read: a link there is not followed, and a FIFO opens without waiting (it would
|
|
484
|
+
* hold this read, on a run's start, for ever) and is no file, so neither is an answer. */
|
|
485
|
+
function signedInAt(data, login) {
|
|
486
|
+
let fd;
|
|
487
|
+
try {
|
|
488
|
+
fd = openSync(answerFile(data, login), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
489
|
+
const at = fstatSync(fd);
|
|
490
|
+
if (!at.isFile() || at.size > ANSWER_MAX_BYTES)
|
|
491
|
+
return undefined;
|
|
492
|
+
const bytes = Buffer.alloc(ANSWER_MAX_BYTES + 1);
|
|
493
|
+
const read = readSync(fd, bytes, 0, bytes.length, 0);
|
|
494
|
+
if (read > ANSWER_MAX_BYTES)
|
|
495
|
+
return undefined;
|
|
496
|
+
const time = Number(bytes.subarray(0, read).toString("utf8").trim());
|
|
497
|
+
return Number.isSafeInteger(time) ? time : undefined;
|
|
498
|
+
}
|
|
499
|
+
catch {
|
|
500
|
+
return undefined;
|
|
501
|
+
}
|
|
502
|
+
finally {
|
|
503
|
+
if (fd !== undefined)
|
|
504
|
+
closeSync(fd);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
/** The harness's codes for a status check that gave no answer (claudeLoginStatus's ClaudeLoginTimedOut and
|
|
508
|
+
* ClaudeLoginNoStatus), read by code as the harness loads only when asked. */
|
|
509
|
+
const NO_ANSWER = new Set(["timed_out", "no_status"]);
|
|
510
|
+
const gaveNoAnswer = (error) => NO_ANSWER.has(error?.code);
|
|
511
|
+
/** The run's refusal when its login's CLI could not say, with the check's own words. */
|
|
512
|
+
const unknownLogin = (error) => new Error(`${LOGIN_UNKNOWN}: ${String(error?.message ?? error).slice(0, 200)}`);
|
|
253
513
|
/** 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
|
|
254
514
|
* key). `cli`: the CLI that answers (loginCli); none, nothing to ask yet. `status`: the CLI's status check (the harness's
|
|
255
|
-
* claudeLoginStatus).
|
|
256
|
-
|
|
515
|
+
* claudeLoginStatus, which asks once and, when the CLI gives no answer, throws with `code` "timed_out" or "no_status").
|
|
516
|
+
* A check that gives no answer (it timed out, or the CLI said nothing that reads as a status) is asked once more, since
|
|
517
|
+
* a loaded host can take past the limit with the login fine. Still none: the run goes on when this login answered
|
|
518
|
+
* signed in on this machine within LOGIN_ANSWER_STANDS_MS (a run's check, or the Settings view's, in this process or
|
|
519
|
+
* another on the same data folder, `data`), as its own CLI still fails in words if the login is gone; else it is
|
|
520
|
+
* refused with LOGIN_UNKNOWN and why. Any other failure (a CLI that is not at its path, say) is refused with
|
|
521
|
+
* LOGIN_UNKNOWN and its own words at once, asked once and never waved through by an earlier answer. A signed-out answer
|
|
522
|
+
* is the answer: never asked again nor overridden, refused at once, and the earlier signed-in time goes. `now`: the
|
|
523
|
+
* clock (Date.now). */
|
|
524
|
+
export async function loginBeforeRun(login, cli, status, options) {
|
|
257
525
|
if (!login || !cli)
|
|
258
526
|
return;
|
|
527
|
+
const { data } = options;
|
|
528
|
+
const now = options.now ?? Date.now;
|
|
259
529
|
let said;
|
|
260
|
-
|
|
261
|
-
|
|
530
|
+
let lastError;
|
|
531
|
+
for (let attempt = 0; attempt < 2 && said === undefined; attempt++) {
|
|
532
|
+
try {
|
|
533
|
+
said = await status(cli, login);
|
|
534
|
+
}
|
|
535
|
+
catch (error) {
|
|
536
|
+
if (!gaveNoAnswer(error))
|
|
537
|
+
throw unknownLogin(error);
|
|
538
|
+
lastError = error;
|
|
539
|
+
}
|
|
262
540
|
}
|
|
263
|
-
|
|
264
|
-
|
|
541
|
+
if (said === undefined) {
|
|
542
|
+
// An answer from after now (the clock set back since) stands for nothing.
|
|
543
|
+
const at = signedInAt(data, login);
|
|
544
|
+
const age = at === undefined ? undefined : now() - at;
|
|
545
|
+
if (age !== undefined && age >= 0 && age <= LOGIN_ANSWER_STANDS_MS)
|
|
546
|
+
return;
|
|
547
|
+
throw unknownLogin(lastError);
|
|
265
548
|
}
|
|
549
|
+
noteAnswer(login, said, now(), data);
|
|
266
550
|
if (!said.signedIn)
|
|
267
551
|
throw new Error(LOGIN_SIGNED_OUT);
|
|
268
552
|
}
|
package/dist/claude-sign-in.d.ts
CHANGED
|
@@ -14,6 +14,12 @@ export type SignInView = {
|
|
|
14
14
|
signal?: string;
|
|
15
15
|
error?: string;
|
|
16
16
|
};
|
|
17
|
+
/** What a turn on the sign-ins' queue (ClaudeSignIns.serial) does to the sign-in, at once, as the turn holds the queue:
|
|
18
|
+
* end the one that waits and start another (`start`), or end it and forget it (`stop`). */
|
|
19
|
+
export type SignInTurn = {
|
|
20
|
+
start(begin: (limitMs: number) => ClaudeSignIn): Promise<SignInView>;
|
|
21
|
+
stop(): Promise<void>;
|
|
22
|
+
};
|
|
17
23
|
/** The one sign-in this server started, and its start and stop. `limitMs`: how long a sign-in waits for its browser
|
|
18
24
|
* (SIGN_IN_LIMIT_MS by default; a test's is short). `now`: the clock its times are read from. */
|
|
19
25
|
export declare class ClaudeSignIns {
|
|
@@ -25,11 +31,16 @@ export declare class ClaudeSignIns {
|
|
|
25
31
|
});
|
|
26
32
|
/** The sign-in as it stands, or undefined when none was started here (or it was stopped). */
|
|
27
33
|
view(): SignInView | undefined;
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
*
|
|
34
|
+
/** One turn on the queue: `work` runs alone, after every turn asked before it and before every one asked after, and
|
|
35
|
+
* starts or ends the sign-in through its `turn`. Every change of the login's setting and every start of a sign-in is a
|
|
36
|
+
* turn (claude-login.ts: the turn-on, the turn-off, the sign-in's start and the drop at the server's start), each
|
|
37
|
+
* reading the setting inside it, so none lands between another's read and its start or save. A turn lasts while it
|
|
38
|
+
* decides and starts: a sign-in's wait for its browser is no turn's. A turn that throws rejects its own answer alone;
|
|
39
|
+
* the next turn runs as if it had ended. A sign-in starts and ends only through a turn's `start` and `stop`: there is no
|
|
40
|
+
* start or stop beside them, since one called inside a turn would wait for that turn to end, for ever. */
|
|
41
|
+
serial<T>(work: (turn: SignInTurn) => Promise<T>): Promise<T>;
|
|
42
|
+
/** The server's stop: a turn that ends a sign-in that waits and forgets it, resolving once its CLI is gone; a start
|
|
43
|
+
* asked after it (a request still in flight) spawns nothing. A CLI left waiting would keep the browser's callback port
|
|
44
|
+
* from the next server's sign-in. */
|
|
34
45
|
close(): Promise<void>;
|
|
35
46
|
}
|