@parcha/agentrun-serve 0.1.0-beta.55 → 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.
- package/CHANGELOG.md +261 -0
- package/README.md +228 -45
- package/dist/acp-package.d.ts +2 -0
- package/dist/adapters.d.ts +18 -5
- package/dist/adapters.js +23 -9
- package/dist/agent-package.d.ts +3 -3
- package/dist/agent-package.js +17 -8
- package/dist/answer-json.d.ts +6 -2
- package/dist/answer-json.js +22 -4
- package/dist/api-keys.d.ts +76 -0
- package/dist/api-keys.js +217 -0
- package/dist/api-only.d.ts +17 -5
- package/dist/api-only.js +36 -8
- 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/calendar/tools.js +3 -2
- package/dist/chatgpt-plan.d.ts +28 -3
- package/dist/chatgpt-plan.js +182 -21
- package/dist/claude-login.d.ts +101 -14
- package/dist/claude-login.js +232 -49
- package/dist/claude-sign-in.d.ts +46 -0
- package/dist/claude-sign-in.js +93 -0
- package/dist/cli.js +63 -6
- 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-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/console/assets/{backend-D5EmB4N3.js → backend-DVhHzJX2.js} +19 -19
- package/dist/console/assets/index-Bwn1oTem.css +1 -0
- package/dist/console/assets/index-okP-y8PK.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/docker-host-addresses.d.ts +22 -11
- package/dist/docker-host-addresses.js +38 -26
- package/dist/env-switch.d.ts +10 -0
- package/dist/env-switch.js +33 -2
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +2 -0
- package/dist/fal-price.d.ts +58 -20
- package/dist/fal-price.js +341 -82
- 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/host-check.d.ts +4 -0
- package/dist/host-check.js +4 -0
- 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 +26 -5
- package/dist/key-refusal.js +313 -37
- package/dist/learn/secrets.d.ts +31 -11
- package/dist/learn/secrets.js +152 -191
- package/dist/learning-cost.d.ts +6 -3
- package/dist/learning-cost.js +4 -2
- package/dist/link-check.js +1 -1
- package/dist/llms.txt +2 -2
- package/dist/marketplace.d.ts +77 -0
- package/dist/marketplace.js +224 -0
- package/dist/mcp-api-only-origins.d.ts +30 -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/model-form.d.ts +5 -4
- package/dist/model-form.js +11 -7
- package/dist/model.d.ts +5 -2
- package/dist/model.js +9 -5
- package/dist/ollama-provider.d.ts +7 -1
- package/dist/ollama-provider.js +14 -2
- package/dist/openapi.d.ts +3 -3
- package/dist/openapi.js +245 -42
- 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 +14 -6
- package/dist/place-offers.js +26 -13
- package/dist/provider.d.ts +9 -0
- package/dist/provider.js +50 -3
- package/dist/redact/match.d.ts +101 -0
- package/dist/redact/match.js +1152 -0
- 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 +2 -0
- 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 +3 -1
- package/dist/run-timeline-served.js +4 -4
- package/dist/run-usage.d.ts +1 -0
- package/dist/runnable.d.ts +10 -1
- package/dist/runnable.js +20 -8
- package/dist/runner/adapter-loop.d.ts +14 -0
- package/dist/runner/adapter-loop.js +56 -11
- package/dist/runner/adapter-run-spec.d.ts +45 -8
- package/dist/runner/adapter-run-spec.js +154 -29
- package/dist/runner/adapters/pi.js +25 -3
- package/dist/runner/beside.js +11 -6
- package/dist/runner/big-writes.js +1 -10
- package/dist/runner/claim-secrets.d.ts +18 -3
- package/dist/runner/claim-secrets.js +35 -6
- package/dist/runner/cli-run-spec.d.ts +6 -0
- package/dist/runner/cli-run-spec.js +85 -36
- 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 +11 -5
- package/dist/runner/journal-kinds.d.ts +5 -0
- package/dist/runner/journal-kinds.js +5 -0
- package/dist/runner/loop-contract.d.ts +4 -0
- package/dist/runner/main.js +15 -6
- package/dist/runner/mcp-claim.js +30 -3
- package/dist/runner/mcp-run.d.ts +12 -0
- package/dist/runner/mcp-run.js +12 -5
- package/dist/runner/model-route.d.ts +17 -0
- package/dist/runner/model-route.js +120 -2
- package/dist/runner/presence.d.ts +4 -3
- package/dist/runner/presence.js +4 -2
- package/dist/runner/run-child-host.d.ts +5 -0
- package/dist/runner/run-child-host.js +71 -30
- package/dist/runner/run-child.js +5 -2
- package/dist/runner/runner.d.ts +18 -3
- package/dist/runner/runner.js +162 -36
- 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.js +4 -40
- package/dist/runner/workflow-first.d.ts +11 -4
- package/dist/runner/workflow-first.js +14 -8
- package/dist/runner/workflow-spec.js +3 -2
- 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 +57 -13
- package/dist/server.js +371 -113
- package/dist/skills/agentrun-serve/SKILL.md +4 -0
- package/dist/store/dialect.d.ts +3 -0
- package/dist/store/dialect.js +1 -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/dialect.js +3 -0
- package/dist/store/postgres/migrations/0027_run_launch_census.sql +31 -0
- package/dist/store/schema.d.ts +6 -0
- package/dist/store/schema.js +11 -1
- 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 +1 -1
- package/dist/store/sqlite/run-patch.js +13 -4
- package/dist/store/sqlite/store.js +76 -16
- package/dist/store/store.d.ts +53 -3
- package/dist/store/store.js +27 -0
- package/dist/store-reads/learning.d.ts +6 -3
- package/dist/store-reads/learning.js +22 -7
- package/dist/store-view.d.ts +3 -0
- package/dist/store-view.js +4 -4
- package/dist/suggestions.js +3 -2
- package/dist/template-eval.d.ts +22 -11
- package/dist/template-eval.js +124 -41
- package/dist/template-import.d.ts +10 -0
- package/dist/template-import.js +16 -7
- package/dist/templates.d.ts +10 -2
- package/dist/templates.js +28 -8
- package/dist/timeline/index.d.ts +1 -1
- package/dist/timeline/index.js +1 -1
- package/dist/timeline/labels.d.ts +6 -4
- package/dist/timeline/labels.js +18 -6
- package/dist/timeline/timeline.d.ts +17 -7
- package/dist/timeline/timeline.js +28 -14
- package/dist/timeline/util.js +1 -1
- package/dist/timeline/web-calls.d.ts +26 -9
- package/dist/timeline/web-calls.js +124 -37
- package/dist/tool-gate.d.ts +42 -3
- package/dist/tool-gate.js +69 -6
- 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/validate.js +1 -1
- 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 +6 -4
- package/dist/web-save.js +9 -12
- 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/computer-deps.d.ts +3 -1
- package/dist/workspace/computer-deps.js +12 -4
- package/dist/workspace/computer-for-claim.d.ts +19 -2
- package/dist/workspace/computer-for-claim.js +101 -13
- package/dist/workspace/computer-janitor.d.ts +15 -7
- package/dist/workspace/computer-janitor.js +33 -8
- package/dist/workspace/computer-kept.d.ts +55 -4
- package/dist/workspace/computer-kept.js +129 -8
- 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/run-end-computers.d.ts +5 -1
- package/dist/workspace/run-end-computers.js +27 -1
- package/dist/workspace/synced-folder.js +3 -4
- package/dist/wrapped.d.ts +65 -0
- package/dist/wrapped.js +77 -0
- package/package.json +17 -11
- package/skills/agentrun-serve/SKILL.md +4 -0
- package/templates/README.md +38 -15
- package/dist/console/assets/index-DUYLaqiR.js +0 -746
- package/dist/console/assets/index-DYhU4adm.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.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ClaudeAgentLogin, ClaudeLoginStatus } from "@parcha/agentrun-harness-claude";
|
|
2
|
+
import type { ClaudeSignIns, SignInView } from "./claude-sign-in.js";
|
|
2
3
|
import type { Settings } from "./store/index.js";
|
|
3
4
|
/** The switch: `off` (or 0, false, no) turns the mode off on this server, `on` (or 1, true, yes) leaves it there past
|
|
4
5
|
* loopback; unset, the listen address decides (claudeLoginSwitchedOff). */
|
|
@@ -20,13 +21,45 @@ export declare const LOGIN_DEFAULT_MODEL = "anthropic/claude-sonnet-5-5";
|
|
|
20
21
|
* login (`machine`, which the owner opts into by name). */
|
|
21
22
|
export declare const LOGIN_PLACES: readonly ["agentrun", "machine"];
|
|
22
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. */
|
|
23
26
|
type Setting = {
|
|
24
27
|
v: 1;
|
|
25
28
|
place: LoginPlace;
|
|
26
|
-
|
|
29
|
+
machine?: string;
|
|
27
30
|
only_me: true;
|
|
28
31
|
};
|
|
29
|
-
/** The
|
|
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;
|
|
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
|
|
42
|
+
* config dir is `CLAUDE_CONFIG_DIR` when set, else `~/.claude`, and its credentials are kept there too unless
|
|
43
|
+
* `CLAUDE_SECURESTORAGE_CONFIG_DIR` says otherwise. Read from the server's env, so a Claude Code started with another
|
|
44
|
+
* `CLAUDE_CONFIG_DIR` is found only when AgentRun was started with it as well. A relative one is not taken: the CLI would
|
|
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. */
|
|
49
|
+
export declare function machineFolder(env?: Readonly<Record<string, string | undefined>>): {
|
|
50
|
+
folder: string;
|
|
51
|
+
source: "CLAUDE_CONFIG_DIR" | "default";
|
|
52
|
+
credentials?: string;
|
|
53
|
+
};
|
|
54
|
+
/** The directories of a login at `place`: AgentRun's in `data`, or this machine's (machineFolder, and the Anthropic
|
|
55
|
+
* profiles where the CLI looks for them: `ANTHROPIC_CONFIG_DIR`, else `$XDG_CONFIG_HOME/anthropic`, else
|
|
56
|
+
* `~/.config/anthropic`). This machine's on the CLI's default folder carries the person's home too, so the harness
|
|
57
|
+
* points the CLI at it as their own CLI runs, with no folder variable (the harness's signInEnv and loginEnv). This
|
|
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). */
|
|
30
63
|
export declare function loginAt(place: LoginPlace, data: string, env?: Readonly<Record<string, string | undefined>>): ClaudeAgentLogin;
|
|
31
64
|
/** The setting as saved, or undefined when the mode is not on (or the row is not one this reads). */
|
|
32
65
|
export declare function claudeLoginSetting(settings: Pick<Settings, "get">): Promise<Setting | undefined>;
|
|
@@ -54,7 +87,11 @@ export declare const LOGIN_UNKNOWN = "Claude Code could not say whether its logi
|
|
|
54
87
|
export declare function whyNotOneOwner(facts: {
|
|
55
88
|
apiOnly: boolean;
|
|
56
89
|
tokens: number;
|
|
90
|
+
keys?: number;
|
|
91
|
+
keysDormant?: boolean;
|
|
57
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.";
|
|
58
95
|
/** GET /settings/claude-login. `state`: `off` (not turned on), `other_machine` (turned on for another machine), else
|
|
59
96
|
* what the CLI says: `signed_in`, `signed_out`, or `no_cli` (no CLI here to ask yet: claude-agent's first run installs
|
|
60
97
|
* the pinned one), `unknown` (it gave no answer). Never a token: an account's email, organization and plan only when
|
|
@@ -74,6 +111,17 @@ export type ClaudeLoginView = {
|
|
|
74
111
|
organization?: string;
|
|
75
112
|
plan?: string;
|
|
76
113
|
};
|
|
114
|
+
/** The folder whose login the CLI is asked about: the login's directory at `place`. */
|
|
115
|
+
folder: string;
|
|
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). */
|
|
118
|
+
machine: {
|
|
119
|
+
folder: string;
|
|
120
|
+
source: "CLAUDE_CONFIG_DIR" | "default";
|
|
121
|
+
credentials?: string;
|
|
122
|
+
};
|
|
123
|
+
/** The CLI's sign-in this server started, while this server runs (claude-sign-in.ts). */
|
|
124
|
+
sign_in?: SignInView;
|
|
77
125
|
};
|
|
78
126
|
/** The account's words, as the view shows them. */
|
|
79
127
|
export declare const accountOf: (status: ClaudeLoginStatus) => NonNullable<ClaudeLoginView["account"]>;
|
|
@@ -83,8 +131,8 @@ export declare function turnOnBody(body: unknown): {
|
|
|
83
131
|
} | {
|
|
84
132
|
problem: string;
|
|
85
133
|
};
|
|
86
|
-
/** The setting a turn-on saves, for this machine. AgentRun's own login directory is made (0700) so the CLI
|
|
87
|
-
* 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. */
|
|
88
136
|
export declare function settingFor(place: LoginPlace, data: string): Setting;
|
|
89
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
|
|
90
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,
|
|
@@ -98,17 +146,40 @@ export declare const claudeLoginRouteOff: (name: string, why: string) => string;
|
|
|
98
146
|
/** What the routes need of the server. `pinned`: the pinned CLI's path when installed. `refused`: whyNotOneOwner of this
|
|
99
147
|
* server. `sameMachine`: the request comes from a browser on this machine to a server bound to loopback with no public
|
|
100
148
|
* address, so a sign-in the CLI opens in a browser here reaches the person. `status`/`start`: the harness's
|
|
101
|
-
* claudeLoginStatus and startClaudeLogin. `
|
|
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). */
|
|
102
153
|
export type ClaudeLoginDeps = {
|
|
154
|
+
signIns: ClaudeSignIns;
|
|
103
155
|
settings: Pick<Settings, "get" | "put" | "remove">;
|
|
104
156
|
data: string;
|
|
105
157
|
env: Readonly<Record<string, string | undefined>>;
|
|
106
158
|
pinned: () => Promise<string | undefined>;
|
|
107
|
-
|
|
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>;
|
|
108
165
|
sameMachine: boolean;
|
|
109
166
|
claude: () => Promise<ClaudeLoginCli>;
|
|
167
|
+
saved: (login: ClaudeAgentLogin | undefined) => void;
|
|
110
168
|
changed: () => Promise<void>;
|
|
111
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">;
|
|
112
183
|
/** A route's refusal in words, answered as its `code` (server.ts makes it an ApiError). */
|
|
113
184
|
export declare class ClaudeLoginRefused extends Error {
|
|
114
185
|
readonly code: "forbidden" | "input_invalid" | "agent_not_runnable";
|
|
@@ -116,21 +187,37 @@ export declare class ClaudeLoginRefused extends Error {
|
|
|
116
187
|
}
|
|
117
188
|
/** GET /settings/claude-login: the mode's state and, when on, what the CLI says of its login. */
|
|
118
189
|
export declare function claudeLoginView(deps: ClaudeLoginDeps): Promise<ClaudeLoginView>;
|
|
119
|
-
/** 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. */
|
|
120
193
|
export declare function turnOnClaudeLogin(deps: ClaudeLoginDeps, body: unknown): Promise<ClaudeLoginView>;
|
|
121
|
-
/** DELETE /settings/claude-login: turned off
|
|
194
|
+
/** DELETE /settings/claude-login: turned off, and a sign-in this server started that still waits is ended. The login
|
|
195
|
+
* itself stays as the CLI keeps it; nothing here signs it out. */
|
|
122
196
|
export declare function turnOffClaudeLogin(deps: ClaudeLoginDeps): Promise<ClaudeLoginView>;
|
|
123
|
-
/** POST /settings/claude-login/login: the CLI's own sign-in, started on this machine, where it opens a browser
|
|
124
|
-
*
|
|
197
|
+
/** POST /settings/claude-login/login: the CLI's own sign-in, started on this machine, where it opens a browser, with the
|
|
198
|
+
* shown line's own variables (the harness's signInEnv), so it signs in where the status reads. A sign-in that
|
|
199
|
+
* still waits is ended first (deps.signIns). The browser code goes to the CLI, never here; the answer is the view as it
|
|
200
|
+
* is when the sign-in starts, its `sign_in` waiting. The setting is read, and the CLI started on it, in one turn. */
|
|
125
201
|
export declare function startLoginHere(deps: ClaudeLoginDeps): Promise<ClaudeLoginView>;
|
|
126
202
|
/** At the server's start: a setting saved where the mode is now off or refused (`why`: the switch, a listen address past
|
|
127
203
|
* loopback, a second token, the API-only profile) goes, so no runner, in this process or another, runs on it; the reason
|
|
128
|
-
* is logged. */
|
|
129
|
-
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>;
|
|
130
206
|
/** What the routes and a run's check use of the harness (its login.ts), loaded only when asked. */
|
|
131
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;
|
|
132
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
|
|
133
211
|
* key). `cli`: the CLI that answers (loginCli); none, nothing to ask yet. `status`: the CLI's status check (the harness's
|
|
134
|
-
* claudeLoginStatus).
|
|
135
|
-
|
|
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>;
|
|
136
223
|
export {};
|
package/dist/claude-login.js
CHANGED
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
// harness selection reads no setting, so claude-agent runs on the model keys as it does without the mode. The mode is
|
|
11
11
|
// for a server one person runs: it is refused where more than one person can start runs, `on` included, and the owner
|
|
12
12
|
// 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 {
|
|
13
|
+
import { randomBytes } from "node:crypto";
|
|
14
|
+
import { accessSync, chmodSync, constants, existsSync, linkSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
15
|
+
import { homedir } from "node:os";
|
|
15
16
|
import { delimiter, isAbsolute, join } from "node:path";
|
|
16
17
|
import { facts as claudeFacts } from "@parcha/agentrun-harness-claude/facts";
|
|
17
18
|
import { registerSecretWrite } from "./secret-writes.js";
|
|
@@ -46,24 +47,102 @@ export const LOGIN_DEFAULT_MODEL = "anthropic/claude-sonnet-5-5";
|
|
|
46
47
|
/** Where the login is: AgentRun's own directory in its data (`agentrun`, the default), or this machine's own Claude Code
|
|
47
48
|
* login (`machine`, which the owner opts into by name). */
|
|
48
49
|
export const LOGIN_PLACES = ["agentrun", "machine"];
|
|
49
|
-
/** The
|
|
50
|
+
/** The file in the data folder that holds this machine's id (machineId). */
|
|
51
|
+
export const MACHINE_ID_FILE = "machine-id";
|
|
52
|
+
const MACHINE_ID = /^[0-9a-f]{32}$/;
|
|
53
|
+
/** This machine's id, which names the machine the login was turned on for: a random one, made once in
|
|
54
|
+
* `<data>/machine-id` (0600) and read back by every later start. Never the hostname, which moves with the network (a
|
|
55
|
+
* laptop's at home is not its name at the office), while the data folder stays. A missing file, or one that holds no
|
|
56
|
+
* 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
|
|
57
|
+
* again. The file appears whole: written aside, then linked in place, so a start beside this one that made an id first
|
|
58
|
+
* keeps its own, and both read that one. */
|
|
59
|
+
export function machineId(data) {
|
|
60
|
+
const file = join(data, MACHINE_ID_FILE);
|
|
61
|
+
const read = () => {
|
|
62
|
+
try {
|
|
63
|
+
const id = readFileSync(file, "utf8").trim();
|
|
64
|
+
return MACHINE_ID.test(id) ? id : undefined;
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const known = read();
|
|
71
|
+
if (known)
|
|
72
|
+
return known;
|
|
73
|
+
const id = randomBytes(16).toString("hex");
|
|
74
|
+
const aside = `${file}.${process.pid}.${randomBytes(4).toString("hex")}`;
|
|
75
|
+
writeFileSync(aside, `${id}\n`, { mode: 0o600, flag: "wx" });
|
|
76
|
+
try {
|
|
77
|
+
// The umask may take more than 0600 leaves; never less.
|
|
78
|
+
chmodSync(aside, 0o600);
|
|
79
|
+
try {
|
|
80
|
+
linkSync(aside, file);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
if (error.code !== "EEXIST")
|
|
84
|
+
throw error;
|
|
85
|
+
// Another start made one first: that one. Else what is there holds no id, and this one takes its place.
|
|
86
|
+
const theirs = read();
|
|
87
|
+
if (theirs)
|
|
88
|
+
return theirs;
|
|
89
|
+
renameSync(aside, file);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
rmSync(aside, { force: true });
|
|
94
|
+
}
|
|
95
|
+
return id;
|
|
96
|
+
}
|
|
97
|
+
/** 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
|
|
98
|
+
* config dir is `CLAUDE_CONFIG_DIR` when set, else `~/.claude`, and its credentials are kept there too unless
|
|
99
|
+
* `CLAUDE_SECURESTORAGE_CONFIG_DIR` says otherwise. Read from the server's env, so a Claude Code started with another
|
|
100
|
+
* `CLAUDE_CONFIG_DIR` is found only when AgentRun was started with it as well. A relative one is not taken: the CLI would
|
|
101
|
+
* read it against a working directory this server does not share. `source` says which rule gave the folder.
|
|
102
|
+
* `credentials`: the server's `CLAUDE_SECURESTORAGE_CONFIG_DIR` when it sets one the CLI takes as it is (an absolute
|
|
103
|
+
* path, or empty: `~/.claude`, and on macOS the Keychain item with no suffix), where the CLI keeps the tokens apart
|
|
104
|
+
* from the folder; a relative one is not taken, as above. */
|
|
105
|
+
export function machineFolder(env = process.env) {
|
|
106
|
+
const secure = env.CLAUDE_SECURESTORAGE_CONFIG_DIR;
|
|
107
|
+
const credentials = secure === "" || (secure && isAbsolute(secure)) ? { credentials: secure } : {};
|
|
108
|
+
if (env.CLAUDE_CONFIG_DIR && isAbsolute(env.CLAUDE_CONFIG_DIR))
|
|
109
|
+
return { folder: env.CLAUDE_CONFIG_DIR, source: "CLAUDE_CONFIG_DIR", ...credentials };
|
|
110
|
+
return { folder: join(homeOf(env), ".claude"), source: "default", ...credentials };
|
|
111
|
+
}
|
|
112
|
+
/** The person's home as the server's env says it: its `HOME` when absolute, else `os.homedir()`. The person's own CLI
|
|
113
|
+
* reads its home as Node does (`os.homedir()`, which is `HOME` when set), so the two agree for the server's own env;
|
|
114
|
+
* reading the env handed in first keeps it the one source a test or an embedding host can set. */
|
|
115
|
+
const homeOf = (env) => (env.HOME && isAbsolute(env.HOME) ? env.HOME : homedir());
|
|
116
|
+
/** The directories of a login at `place`: AgentRun's in `data`, or this machine's (machineFolder, and the Anthropic
|
|
117
|
+
* profiles where the CLI looks for them: `ANTHROPIC_CONFIG_DIR`, else `$XDG_CONFIG_HOME/anthropic`, else
|
|
118
|
+
* `~/.config/anthropic`). This machine's on the CLI's default folder carries the person's home too, so the harness
|
|
119
|
+
* points the CLI at it as their own CLI runs, with no folder variable (the harness's signInEnv and loginEnv). This
|
|
120
|
+
* machine's with a credentials dir in the server's env (machineFolder's `credentials`) carries it as `secure`, absolute
|
|
121
|
+
* (empty: `~/.claude`, `unsuffixed`), so the CLI keeps the tokens where the person's own CLI with it does. A status
|
|
122
|
+
* check at either place runs on a copy of the account file (the harness's claudeLoginStatus). Every login carries the
|
|
123
|
+
* person's home as `userHome`, read here once (homeOf), so the status check, the sign-in and a run on macOS all run in
|
|
124
|
+
* that one home whatever reads it later (the harness's personHome). */
|
|
50
125
|
export function loginAt(place, data, env = process.env) {
|
|
126
|
+
const home = homeOf(env);
|
|
51
127
|
if (place === "agentrun")
|
|
52
|
-
return { dir: join(data, "claude-login"), profiles: join(data, "claude-login", "anthropic") };
|
|
53
|
-
const home = env.HOME && isAbsolute(env.HOME) ? env.HOME : homedir();
|
|
128
|
+
return { dir: join(data, "claude-login"), profiles: join(data, "claude-login", "anthropic"), userHome: home };
|
|
54
129
|
const config = env.XDG_CONFIG_HOME && isAbsolute(env.XDG_CONFIG_HOME) ? env.XDG_CONFIG_HOME : join(home, ".config");
|
|
55
|
-
|
|
130
|
+
const { folder, source, credentials } = machineFolder(env);
|
|
131
|
+
const secure = credentials === undefined ? {} : credentials === "" ? { secure: join(home, ".claude"), unsuffixed: true } : { secure: credentials };
|
|
132
|
+
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 };
|
|
56
133
|
}
|
|
57
134
|
/** The setting as saved, or undefined when the mode is not on (or the row is not one this reads). */
|
|
58
135
|
export async function claudeLoginSetting(settings) {
|
|
59
136
|
const row = (await settings.get(CLAUDE_LOGIN_SETTING));
|
|
60
|
-
if (!row || row.v !== 1 || !LOGIN_PLACES.includes(row.place) || typeof row.
|
|
137
|
+
if (!row || row.v !== 1 || !LOGIN_PLACES.includes(row.place) || (row.machine !== undefined && typeof row.machine !== "string") || row.only_me !== true)
|
|
61
138
|
return undefined;
|
|
62
139
|
return row;
|
|
63
140
|
}
|
|
141
|
+
/** Whether `setting` was turned on for this machine: the one whose data folder (`data`) holds its id (machineId). */
|
|
142
|
+
const thisMachine = (setting, data) => setting.machine === machineId(data);
|
|
64
143
|
/** The login a run here goes on: the setting's, when the switch is not off and this is the machine it was turned on for. */
|
|
65
144
|
export function loginOf(setting, data, env) {
|
|
66
|
-
if (!setting || claudeLoginOff(env) || setting
|
|
145
|
+
if (!setting || claudeLoginOff(env) || !thisMachine(setting, data))
|
|
67
146
|
return undefined;
|
|
68
147
|
return loginAt(setting.place, data, env);
|
|
69
148
|
}
|
|
@@ -120,8 +199,19 @@ export function whyNotOneOwner(facts) {
|
|
|
120
199
|
return "This server has no owner (the API-only profile), so anyone with its token starts runs: a Claude plan is one person's.";
|
|
121
200
|
if (facts.tokens > 1)
|
|
122
201
|
return `This server takes ${facts.tokens} API tokens, so more than one person can start runs here: a Claude plan is one person's.`;
|
|
202
|
+
// A key made in the console for an app (api-keys.ts) is one more caller that starts runs, as a second token is.
|
|
203
|
+
// `keysDormant`: the keys open nothing on this server (api-keys.ts dormant), and would at a start that takes them.
|
|
204
|
+
const keys = facts.keys ?? 0;
|
|
205
|
+
if (keys > 0) {
|
|
206
|
+
const [what, it] = keys === 1 ? ["an API key", "it"] : [`${keys} API keys`, "them"];
|
|
207
|
+
return facts.keysDormant
|
|
208
|
+
? `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.`
|
|
209
|
+
: `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.`;
|
|
210
|
+
}
|
|
123
211
|
return undefined;
|
|
124
212
|
}
|
|
213
|
+
/** 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. */
|
|
214
|
+
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.";
|
|
125
215
|
/** The account's words, as the view shows them. */
|
|
126
216
|
export const accountOf = (status) => ({ ...(status.method ? { method: status.method } : {}), ...(status.email ? { email: status.email } : {}), ...(status.organization ? { organization: status.organization } : {}), ...(status.plan ? { plan: status.plan } : {}) });
|
|
127
217
|
/** The body of a turn-on, checked: where the login is, and the owner's word that they alone run agents here. */
|
|
@@ -133,12 +223,12 @@ export function turnOnBody(body) {
|
|
|
133
223
|
return { problem: "only_me must be true: confirm you alone run agents on this server, since its runs would use your Claude plan" };
|
|
134
224
|
return { place: given.place ?? "agentrun" };
|
|
135
225
|
}
|
|
136
|
-
/** The setting a turn-on saves, for this machine. AgentRun's own login directory is made (0700) so the CLI
|
|
137
|
-
* login there; nothing in it is read. */
|
|
226
|
+
/** The setting a turn-on saves, for this machine (machineId). AgentRun's own login directory is made (0700) so the CLI
|
|
227
|
+
* can keep its login there; nothing in it is read. */
|
|
138
228
|
export function settingFor(place, data) {
|
|
139
229
|
if (place === "agentrun")
|
|
140
230
|
mkdirSync(join(data, "claude-login"), { recursive: true, mode: 0o700 });
|
|
141
|
-
return { v: 1, place,
|
|
231
|
+
return { v: 1, place, machine: machineId(data), only_me: true };
|
|
142
232
|
}
|
|
143
233
|
/** The login a run of `harness` on this runner goes on: the saved setting's, read now, when the switch is not off, the
|
|
144
234
|
* harness runs on it, the run is in a runner place (`place`), and this is the machine it was turned on for; else none,
|
|
@@ -151,6 +241,13 @@ export async function loginForRun(settings, data, env, harness, place) {
|
|
|
151
241
|
/** The routes here (server.ts): every one answers 404, by its name, while the mode is off (`why`). */
|
|
152
242
|
export const CLAUDE_LOGIN_ROUTES = new Set(["getClaudeLogin", "turnOnClaudeLogin", "turnOffClaudeLogin", "startClaudeLogin"]);
|
|
153
243
|
export const claudeLoginRouteOff = (name, why) => `${name}: Claude Code's login on this machine is off on this server: ${why}.`;
|
|
244
|
+
/** What the one-owner rule gives the login's routes over a server's keys: `refused`, which counts the keys each time
|
|
245
|
+
* it is asked, and `turn`, the keys' own. The server's routes take both from here, and so does the test that runs the
|
|
246
|
+
* turn-on against a held revocation: neither is a value read once, or a turn of its own. */
|
|
247
|
+
export const oneOwnerDeps = (facts, keys) => ({
|
|
248
|
+
refused: () => whyNotOneOwner({ ...facts, keys: keys.count }),
|
|
249
|
+
turn: (work) => keys.inTurn(work),
|
|
250
|
+
});
|
|
154
251
|
/** A route's refusal in words, answered as its `code` (server.ts makes it an ApiError). */
|
|
155
252
|
export class ClaudeLoginRefused extends Error {
|
|
156
253
|
code;
|
|
@@ -165,80 +262,166 @@ export async function claudeLoginView(deps) {
|
|
|
165
262
|
const place = setting?.place ?? "agentrun";
|
|
166
263
|
const login = loginAt(place, deps.data, deps.env);
|
|
167
264
|
const cli = loginCli(await deps.pinned(), deps.env);
|
|
168
|
-
const
|
|
265
|
+
const signIn = deps.signIns.view();
|
|
266
|
+
const refused = deps.refused();
|
|
267
|
+
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 } : {}) };
|
|
169
268
|
if (!setting)
|
|
170
269
|
return { state: "off", ...base };
|
|
171
|
-
if (setting.
|
|
270
|
+
if (!thisMachine(setting, deps.data))
|
|
172
271
|
return { state: "other_machine", ...base, can_start: false };
|
|
173
272
|
if (!cli)
|
|
174
273
|
return { state: "no_cli", ...base };
|
|
175
274
|
try {
|
|
275
|
+
// Asked once: a person waits on this read. Its answer is a run's too (loginBeforeRun), so one who just saw the
|
|
276
|
+
// login signed in here has a run on a slow host go on.
|
|
176
277
|
const said = await (await deps.claude()).claudeLoginStatus(cli, login, pathOf(deps.env));
|
|
278
|
+
noteAnswer(login, said, Date.now());
|
|
177
279
|
return said.signedIn ? { state: "signed_in", ...base, account: accountOf(said) } : { state: "signed_out", ...base };
|
|
178
280
|
}
|
|
179
281
|
catch {
|
|
180
282
|
return { state: "unknown", ...base };
|
|
181
283
|
}
|
|
182
284
|
}
|
|
183
|
-
/** PUT /settings/claude-login: turned on, refused in one line where more than one person can start runs.
|
|
285
|
+
/** PUT /settings/claude-login: turned on, refused in one line where more than one person can start runs. A turn-on that
|
|
286
|
+
* moves the login to another place first ends a sign-in this server started that still waits: its CLI signs in at the
|
|
287
|
+
* old place, which the login no longer reads. One at the same place leaves it waiting, as it still signs in there. */
|
|
184
288
|
export async function turnOnClaudeLogin(deps, body) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
289
|
+
// `refused` is asked only in the keys' turn (deps.turn), where a key being made or revoked has landed, or been undone.
|
|
290
|
+
// Asked once before the sign-in is touched, so a turn-on refused or malformed then leaves a waiting sign-in as it was;
|
|
291
|
+
// and again with the write, in one turn, so no key is made between the decision and the setting. A key made between
|
|
292
|
+
// the two turns, on a turn-on that moves the place, is the one case that both stops the sign-in and refuses: nothing
|
|
293
|
+
// is written, and the person starts the sign-in again once the key is revoked. The sign-in's stop and
|
|
294
|
+
// what follows the write (the reading again, the CLI's status) are outside the keys' turn, so a key's revocation never
|
|
295
|
+
// waits on the CLI.
|
|
296
|
+
const refusal = () => {
|
|
297
|
+
const refused = deps.refused();
|
|
298
|
+
if (refused)
|
|
299
|
+
throw new ClaudeLoginRefused("forbidden", refused);
|
|
300
|
+
};
|
|
301
|
+
const asked = await deps.turn(async () => {
|
|
302
|
+
refusal();
|
|
303
|
+
const asked = turnOnBody(body);
|
|
304
|
+
if ("problem" in asked)
|
|
305
|
+
throw new ClaudeLoginRefused("input_invalid", asked.problem);
|
|
306
|
+
return asked;
|
|
307
|
+
});
|
|
308
|
+
await deps.signIns.serial(async (turn) => {
|
|
309
|
+
if ((await claudeLoginSetting(deps.settings))?.place !== asked.place)
|
|
310
|
+
await turn.stop();
|
|
311
|
+
await deps.turn(async () => {
|
|
312
|
+
refusal();
|
|
313
|
+
const setting = settingFor(asked.place, deps.data);
|
|
314
|
+
await deps.settings.put(CLAUDE_LOGIN_SETTING, setting);
|
|
315
|
+
deps.saved(loginOf(setting, deps.data, deps.env));
|
|
316
|
+
});
|
|
317
|
+
});
|
|
191
318
|
await deps.changed();
|
|
192
319
|
return claudeLoginView(deps);
|
|
193
320
|
}
|
|
194
|
-
/** DELETE /settings/claude-login: turned off
|
|
321
|
+
/** DELETE /settings/claude-login: turned off, and a sign-in this server started that still waits is ended. The login
|
|
322
|
+
* itself stays as the CLI keeps it; nothing here signs it out. */
|
|
195
323
|
export async function turnOffClaudeLogin(deps) {
|
|
196
|
-
await deps.
|
|
324
|
+
await deps.signIns.serial(async (turn) => {
|
|
325
|
+
await turn.stop();
|
|
326
|
+
await deps.settings.remove(CLAUDE_LOGIN_SETTING);
|
|
327
|
+
deps.saved(undefined);
|
|
328
|
+
});
|
|
197
329
|
await deps.changed();
|
|
198
330
|
return claudeLoginView(deps);
|
|
199
331
|
}
|
|
200
|
-
/** POST /settings/claude-login/login: the CLI's own sign-in, started on this machine, where it opens a browser
|
|
201
|
-
*
|
|
332
|
+
/** POST /settings/claude-login/login: the CLI's own sign-in, started on this machine, where it opens a browser, with the
|
|
333
|
+
* shown line's own variables (the harness's signInEnv), so it signs in where the status reads. A sign-in that
|
|
334
|
+
* still waits is ended first (deps.signIns). The browser code goes to the CLI, never here; the answer is the view as it
|
|
335
|
+
* is when the sign-in starts, its `sign_in` waiting. The setting is read, and the CLI started on it, in one turn. */
|
|
202
336
|
export async function startLoginHere(deps) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
337
|
+
const refused = deps.refused();
|
|
338
|
+
if (refused)
|
|
339
|
+
throw new ClaudeLoginRefused("forbidden", refused);
|
|
340
|
+
await deps.signIns.serial(async (signIn) => {
|
|
341
|
+
const setting = await claudeLoginSetting(deps.settings);
|
|
342
|
+
if (!setting)
|
|
343
|
+
throw new ClaudeLoginRefused("agent_not_runnable", "Turn on Claude Code's login on this machine first.");
|
|
344
|
+
if (!thisMachine(setting, deps.data))
|
|
345
|
+
throw new ClaudeLoginRefused("agent_not_runnable", "It was turned on for another machine: sign in there.");
|
|
346
|
+
if (!deps.sameMachine)
|
|
347
|
+
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.");
|
|
348
|
+
const cli = loginCli(await deps.pinned(), deps.env);
|
|
349
|
+
if (!cli)
|
|
350
|
+
throw new ClaudeLoginRefused("agent_not_runnable", "No Claude Code CLI here yet: claude-agent's first run installs it, or run the command shown.");
|
|
351
|
+
// The person's home (the login's `userHome`) and desktop, so the CLI can open their browser.
|
|
352
|
+
const claude = await deps.claude();
|
|
353
|
+
const login = loginAt(setting.place, deps.data, deps.env);
|
|
354
|
+
await signIn.start((timeoutMs) => claude.startClaudeLogin(cli, login, { ...pathOf(deps.env), display: deps.env, timeoutMs }));
|
|
355
|
+
});
|
|
217
356
|
return claudeLoginView(deps);
|
|
218
357
|
}
|
|
219
358
|
/** At the server's start: a setting saved where the mode is now off or refused (`why`: the switch, a listen address past
|
|
220
359
|
* loopback, a second token, the API-only profile) goes, so no runner, in this process or another, runs on it; the reason
|
|
221
|
-
* is logged. */
|
|
222
|
-
export async function dropRefusedLogin(settings, why, log) {
|
|
223
|
-
if (!why
|
|
360
|
+
* is logged. A turn on `signIns`' queue, as every change of the setting is. */
|
|
361
|
+
export async function dropRefusedLogin(signIns, settings, why, log) {
|
|
362
|
+
if (!why)
|
|
224
363
|
return;
|
|
225
|
-
await
|
|
226
|
-
|
|
364
|
+
await signIns.serial(async () => {
|
|
365
|
+
if (!(await claudeLoginSetting(settings)))
|
|
366
|
+
return;
|
|
367
|
+
await settings.remove(CLAUDE_LOGIN_SETTING);
|
|
368
|
+
log(`Claude Code's login on this machine turned off: ${why}`);
|
|
369
|
+
});
|
|
227
370
|
}
|
|
228
371
|
const pathOf = (env) => (env.PATH ? { path: env.PATH } : {});
|
|
372
|
+
/** How long a signed-in answer stands in for a check that gives none (loginBeforeRun). */
|
|
373
|
+
export const LOGIN_ANSWER_STANDS_MS = 10 * 60_000;
|
|
374
|
+
/** When each login last answered signed in, in this server process: in memory only, never on disk, and nothing but the
|
|
375
|
+
* time. A signed-out answer removes its entry. */
|
|
376
|
+
const signedInAt = new Map();
|
|
377
|
+
/** What tells one login from another here: its folder and its credentials dir. */
|
|
378
|
+
const loginKey = (login) => JSON.stringify([login.dir, login.secure ?? null, login.unsuffixed === true]);
|
|
379
|
+
/** Notes what the CLI said of `login` at `now`: signed in, the time; signed out, nothing (the earlier time goes). */
|
|
380
|
+
function noteAnswer(login, said, now) {
|
|
381
|
+
if (said.signedIn)
|
|
382
|
+
signedInAt.set(loginKey(login), now);
|
|
383
|
+
else
|
|
384
|
+
signedInAt.delete(loginKey(login));
|
|
385
|
+
}
|
|
386
|
+
/** The harness's codes for a status check that gave no answer (claudeLoginStatus's ClaudeLoginTimedOut and
|
|
387
|
+
* ClaudeLoginNoStatus), read by code as the harness loads only when asked. */
|
|
388
|
+
const NO_ANSWER = new Set(["timed_out", "no_status"]);
|
|
389
|
+
const gaveNoAnswer = (error) => NO_ANSWER.has(error?.code);
|
|
390
|
+
/** The run's refusal when its login's CLI could not say, with the check's own words. */
|
|
391
|
+
const unknownLogin = (error) => new Error(`${LOGIN_UNKNOWN}: ${String(error?.message ?? error).slice(0, 200)}`);
|
|
229
392
|
/** 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
|
|
230
393
|
* key). `cli`: the CLI that answers (loginCli); none, nothing to ask yet. `status`: the CLI's status check (the harness's
|
|
231
|
-
* claudeLoginStatus).
|
|
232
|
-
|
|
394
|
+
* claudeLoginStatus, which asks once and, when the CLI gives no answer, throws with `code` "timed_out" or "no_status").
|
|
395
|
+
* A check that gives no answer (it timed out, or the CLI said nothing that reads as a status) is asked once more, since
|
|
396
|
+
* a loaded host can take past the limit with the login fine. Still none: the run goes on when this login answered
|
|
397
|
+
* signed in within LOGIN_ANSWER_STANDS_MS in this server process (a run's check here, or the Settings view's), as its
|
|
398
|
+
* own CLI still fails in words if the login is gone; else it is refused with LOGIN_UNKNOWN and why. Any other failure (a
|
|
399
|
+
* CLI that is not at its path, say) is refused with LOGIN_UNKNOWN and its own words at once, asked once and never
|
|
400
|
+
* waved through by an earlier answer. A signed-out answer is the answer: never asked again nor overridden, refused at
|
|
401
|
+
* once, and the earlier signed-in time goes. `now`: the clock (Date.now). */
|
|
402
|
+
export async function loginBeforeRun(login, cli, status, options = {}) {
|
|
233
403
|
if (!login || !cli)
|
|
234
404
|
return;
|
|
405
|
+
const now = options.now ?? Date.now;
|
|
235
406
|
let said;
|
|
236
|
-
|
|
237
|
-
|
|
407
|
+
let lastError;
|
|
408
|
+
for (let attempt = 0; attempt < 2 && said === undefined; attempt++) {
|
|
409
|
+
try {
|
|
410
|
+
said = await status(cli, login);
|
|
411
|
+
}
|
|
412
|
+
catch (error) {
|
|
413
|
+
if (!gaveNoAnswer(error))
|
|
414
|
+
throw unknownLogin(error);
|
|
415
|
+
lastError = error;
|
|
416
|
+
}
|
|
238
417
|
}
|
|
239
|
-
|
|
240
|
-
|
|
418
|
+
if (said === undefined) {
|
|
419
|
+
const at = signedInAt.get(loginKey(login));
|
|
420
|
+
if (at !== undefined && now() - at <= LOGIN_ANSWER_STANDS_MS)
|
|
421
|
+
return;
|
|
422
|
+
throw unknownLogin(lastError);
|
|
241
423
|
}
|
|
424
|
+
noteAnswer(login, said, now());
|
|
242
425
|
if (!said.signedIn)
|
|
243
426
|
throw new Error(LOGIN_SIGNED_OUT);
|
|
244
427
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { ClaudeSignIn } from "@parcha/agentrun-harness-claude";
|
|
2
|
+
/** How long a sign-in waits for its browser by default: the harness's own limit. */
|
|
3
|
+
export declare const SIGN_IN_LIMIT_MS: number;
|
|
4
|
+
/** A sign-in as GET /settings/claude-login says it. `state`: `waiting` (for the browser, since `started_at`),
|
|
5
|
+
* `signed_in` (the CLI exited 0), `timed_out` (ended after `limit_ms` with no answer from the browser), `failed` (the
|
|
6
|
+
* CLI ended without signing in: `exit_code`, or the `signal` that ended it), `not_started` (the CLI could not be
|
|
7
|
+
* started: `error`). */
|
|
8
|
+
export type SignInView = {
|
|
9
|
+
state: "waiting" | "signed_in" | "timed_out" | "failed" | "not_started";
|
|
10
|
+
started_at: string;
|
|
11
|
+
ended_at?: string;
|
|
12
|
+
limit_ms: number;
|
|
13
|
+
exit_code?: number;
|
|
14
|
+
signal?: string;
|
|
15
|
+
error?: string;
|
|
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
|
+
};
|
|
23
|
+
/** The one sign-in this server started, and its start and stop. `limitMs`: how long a sign-in waits for its browser
|
|
24
|
+
* (SIGN_IN_LIMIT_MS by default; a test's is short). `now`: the clock its times are read from. */
|
|
25
|
+
export declare class ClaudeSignIns {
|
|
26
|
+
#private;
|
|
27
|
+
readonly limitMs: number;
|
|
28
|
+
constructor(options?: {
|
|
29
|
+
limitMs?: number;
|
|
30
|
+
now?: () => number;
|
|
31
|
+
});
|
|
32
|
+
/** The sign-in as it stands, or undefined when none was started here (or it was stopped). */
|
|
33
|
+
view(): SignInView | undefined;
|
|
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. */
|
|
45
|
+
close(): Promise<void>;
|
|
46
|
+
}
|