@parcha/agentrun-harness-claude 0.1.0-beta.54 → 0.1.0-beta.56
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 +11 -0
- package/dist/env.d.ts +4 -1
- package/dist/env.js +8 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/login.d.ts +80 -11
- package/dist/login.js +177 -24
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## 0.1.0-beta.56 (2026-11-24)
|
|
6
|
+
|
|
7
|
+
- Settings → Model keys' "Open the CLI's sign-in here" signs the claude CLI in at the login's own folder, so the login reads signed in afterwards. The sign-in ran with a temporary config dir, where `auth login` wrote the account it signed in (`.claude.json`); that folder was removed when the sign-in ended, and the login's folder stayed without it, so AgentRun kept saying "not signed in" while running the shown command by hand worked. The sign-in, the shown command and the status check now take their variables from one function (`claudeSignInEnv`), and the status check reads the login's folder as its config dir. <!-- changes/claude-sign-in-folder.md -->
|
|
8
|
+
|
|
9
|
+
- `GET /v2/settings/claude-login` says what became of a sign-in the server started, as `sign_in`: `waiting` for the browser since `started_at`, `signed_in`, `timed_out`, `failed` with the CLI's `exit_code`, or `not_started`. The console shows "Waiting for the browser…" from it, after a reload too. A sign-in no browser answers within 10 minutes is ended and says so, with the button to start it again; before, it ended silently and the browser's page then failed to reach it. A second press, turning the mode off, or stopping the server ends a sign-in that still waits; the stop waits for its CLI to exit, so no CLI keeps the browser's callback port from the next server.
|
|
10
|
+
- The view names the folder it asks about (`folder`), and this machine's own login folder for the claude CLI (`machine`). That folder follows the CLI's rule: `CLAUDE_CONFIG_DIR` when the server's environment sets it to an absolute path, else `~/.claude`. The console shows it in place of the fixed "(~/.claude)", and where the server saw no `CLAUDE_CONFIG_DIR` it says that a CLI using another folder needs AgentRun started with `CLAUDE_CONFIG_DIR` set to it.
|
|
11
|
+
- "This machine's" login on the CLI's default folder (`~/.claude`, no `CLAUDE_CONFIG_DIR`) is signed in as the person's own claude CLI runs: with no config or credentials dir set, in their home, so the sign-in writes the account in `~/.claude.json` and the tokens where their CLI keeps them, and the shown line is their own, clearing both variables (`env -u CLAUDE_CONFIG_DIR -u CLAUDE_SECURESTORAGE_CONFIG_DIR ...`) so a terminal that exports either signs in at that same login. Before, AgentRun set both dirs to `~/.claude`, which reads `~/.claude/.claude.json` (where the person's CLI keeps no account) and, on macOS, a Keychain item named from that folder rather than the person's own (the one with no suffix), so it read signed out there while the person's CLI was signed in. A run on it sets `CLAUDE_SECURESTORAGE_CONFIG_DIR` empty on macOS, which names that same item whatever the run's config dir, and `~/.claude` elsewhere. The harness's `claudeLoginEnv` takes the platform (this host's by default), and its login takes `home` for this place. A login in a `CLAUDE_CONFIG_DIR` folder keeps both dirs set to it, which is where the person's CLI with that variable keeps its login.
|
|
12
|
+
- The status check on "this machine's" login (either folder) never runs the claude CLI on the person's own account file. The CLI rewrites `.claude.json` on every start whose config version is not its own (and the first time adds `settings.json`, a backup and a lock beside it), so asking it in place rewrote the person's file on every view, every 2 seconds while a sign-in waited, and at every run's start. Each check now copies the file (opened only to read) into a folder of its own, gives the CLI that folder as its config dir and the tokens as a run reads them, and removes the folder however the check ends. The copy leaves out the fields that hold secrets (`primaryApiKey`, the API key an older CLI signed in with; `env`; every `mcpServers`), and a file that doesn't parse as a JSON object is not copied; a run's config dir has none of them either, so a login whose only key is that `primaryApiKey` reads signed out, as a run on it would be. The harness's login takes `shared` for this place, and `claudeLoginStatus` takes the platform.
|
|
13
|
+
- On macOS a login read signed out in AgentRun, and its runs failed in about 2 seconds with "signed out", while `claude auth status` in a terminal said signed in. The CLI reads its tokens there from the login keychain, which is under the person's home, and the status check ran AgentRun's own folder and a `CLAUDE_CONFIG_DIR` folder in a temporary home, and a run in the session's. The status check now runs in the person's home (the server's `HOME`) on every place and platform, keeping each place's credentials dir, which names the Keychain item ahead of the config dir; a run on the login on macOS gets that home too, its config and other dirs still the session's. `claudeLoginStatus` takes `home`, as `startClaudeLogin` does.
|
|
14
|
+
- The harness's `startClaudeLogin` returns the sign-in (`ended`, which says how it ended, and `stop()`) in place of nothing, and starts the CLI in the person's home rather than a temporary one when no home is given.
|
|
15
|
+
|
|
5
16
|
## 0.1.0-beta.45 (2026-11-18)
|
|
6
17
|
|
|
7
18
|
- Claude Agent can run on the claude CLI's own login on the same machine instead of a model key. `claudeLoginStatus` asks the CLI `claude auth status` and returns what it said (signed in, method, email, plan), never a token. `claudeLoginEnv` gives the run's CLI the login's directories and no `ANTHROPIC_BASE_URL`, `ANTHROPIC_AUTH_TOKEN` or key. `claudeLoginCommand` and `startClaudeLogin` give or start the CLI's own `auth login`. A session on the login keeps its directory out of the sandbox's reads, never wipes it, and books its tokens with no dollars. <!-- changes/run-adapters-claude-agent.md -->
|
package/dist/env.d.ts
CHANGED
|
@@ -36,6 +36,8 @@ export interface DurableEnvOptions {
|
|
|
36
36
|
* environment has no model URL or key at all (login.ts).
|
|
37
37
|
*/
|
|
38
38
|
login?: ClaudeAgentLogin;
|
|
39
|
+
/** The platform the CLI runs on, this host's by default. On macOS a CLI on a login runs in the person's home (`HOME`). */
|
|
40
|
+
platform?: NodeJS.Platform;
|
|
39
41
|
/** The model, as the CLI's main and alias models. */
|
|
40
42
|
model: string;
|
|
41
43
|
/** The model for the CLI's small background calls (titles, summaries). Default `model`. */
|
|
@@ -61,7 +63,8 @@ export interface DurableEnvOptions {
|
|
|
61
63
|
}
|
|
62
64
|
/**
|
|
63
65
|
* - `HOME`, `CLAUDE_CONFIG_DIR`, `TMPDIR` and the XDG dirs in the session's
|
|
64
|
-
* cache dirs, which the relay wipes before every spawn (`/var/lib/dh/s`)
|
|
66
|
+
* cache dirs, which the relay wipes before every spawn (`/var/lib/dh/s`),
|
|
67
|
+
* but `HOME` on macOS on a login: the person's (login.ts personHome);
|
|
65
68
|
* - the projects key fixed (`CLAUDE_CODE_PROJECT_DIR_NAME`), so a transcript
|
|
66
69
|
* resumes on any VM, whatever the cwd;
|
|
67
70
|
* - the model through the VM's model port, with the attach's key; the
|
package/dist/env.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* about 50 `CLAUDE_*`, `ANTHROPIC_*` and `MCP_*` variables that change the
|
|
11
11
|
* CLI's behavior; none reaches the child unless it is listed here.
|
|
12
12
|
*/
|
|
13
|
-
import { loginEnv } from './login.js';
|
|
13
|
+
import { loginEnv, personHome } from './login.js';
|
|
14
14
|
/** What never reaches a CLI that runs on a person's login: a model URL, key or token of any kind. */
|
|
15
15
|
export const LOGIN_UNSET = Object.freeze(['ANTHROPIC_BASE_URL', 'ANTHROPIC_AUTH_TOKEN', 'ANTHROPIC_API_KEY', 'CLAUDE_CODE_OAUTH_TOKEN', 'ANTHROPIC_CUSTOM_HEADERS', 'CLAUDE_CODE_USE_BEDROCK', 'CLAUDE_CODE_USE_VERTEX']);
|
|
16
16
|
/** The `PATH` of the CLI and its commands in the VM: the image's, never the Function's. */
|
|
@@ -21,7 +21,8 @@ const BASH_DEFAULT_MS = 120_000;
|
|
|
21
21
|
export const PROJECT_DIR_NAME = 'session';
|
|
22
22
|
/**
|
|
23
23
|
* - `HOME`, `CLAUDE_CONFIG_DIR`, `TMPDIR` and the XDG dirs in the session's
|
|
24
|
-
* cache dirs, which the relay wipes before every spawn (`/var/lib/dh/s`)
|
|
24
|
+
* cache dirs, which the relay wipes before every spawn (`/var/lib/dh/s`),
|
|
25
|
+
* but `HOME` on macOS on a login: the person's (login.ts personHome);
|
|
25
26
|
* - the projects key fixed (`CLAUDE_CODE_PROJECT_DIR_NAME`), so a transcript
|
|
26
27
|
* resumes on any VM, whatever the cwd;
|
|
27
28
|
* - the model through the VM's model port, with the attach's key; the
|
|
@@ -52,9 +53,12 @@ export function durableEnv(options) {
|
|
|
52
53
|
else if (options.baseUrl === undefined || options.token === undefined) {
|
|
53
54
|
throw new Error('agentrun-harness-claude: the CLI needs its model port and key, or a login');
|
|
54
55
|
}
|
|
56
|
+
const platform = options.platform ?? process.platform;
|
|
55
57
|
Object.assign(env, {
|
|
56
58
|
PATH: VM_PATH,
|
|
57
|
-
|
|
59
|
+
// On macOS the CLI on a login reads its tokens from the login keychain, which is under the person's home: in the
|
|
60
|
+
// session's it finds none and reads signed out. Its config, XDG and temporary dirs stay the session's.
|
|
61
|
+
HOME: options.login && platform === 'darwin' ? personHome(options.login) : home,
|
|
58
62
|
TMPDIR: options.tmpDir,
|
|
59
63
|
CLAUDE_CONFIG_DIR: options.configDir,
|
|
60
64
|
CLAUDE_CODE_PROJECT_DIR_NAME: PROJECT_DIR_NAME,
|
|
@@ -62,7 +66,7 @@ export function durableEnv(options) {
|
|
|
62
66
|
XDG_CACHE_HOME: options.cacheDir,
|
|
63
67
|
XDG_DATA_HOME: `${home}/.local/share`,
|
|
64
68
|
XDG_STATE_HOME: `${home}/.local/state`,
|
|
65
|
-
...(options.login ? loginEnv(options.login) : { ANTHROPIC_BASE_URL: options.baseUrl, ANTHROPIC_AUTH_TOKEN: options.token }),
|
|
69
|
+
...(options.login ? loginEnv(options.login, platform) : { ANTHROPIC_BASE_URL: options.baseUrl, ANTHROPIC_AUTH_TOKEN: options.token }),
|
|
66
70
|
ANTHROPIC_DEFAULT_OPUS_MODEL: model,
|
|
67
71
|
ANTHROPIC_DEFAULT_SONNET_MODEL: model,
|
|
68
72
|
ANTHROPIC_DEFAULT_HAIKU_MODEL: small,
|
package/dist/index.d.ts
CHANGED
|
@@ -11,3 +11,5 @@ export { claudeLoginStatus, loginCommand as claudeLoginCommand, loginEnv as clau
|
|
|
11
11
|
export type { ClaudeAgentLogin, ClaudeLoginStatus } from "./login.js";
|
|
12
12
|
export { listedTools, MCP_OUTPUT_TOKENS, RESULT_SIZE_KEY, toolServer, type ToolServerModules } from "./host/tool-server.js";
|
|
13
13
|
export type { ServedTool } from "./host/tool-server.js";
|
|
14
|
+
export { SIGN_IN_MS as CLAUDE_SIGN_IN_MS, signInEnv as claudeSignInEnv } from "./login.js";
|
|
15
|
+
export type { ClaudeSignIn, ClaudeSignInEnd } from "./login.js";
|
package/dist/index.js
CHANGED
|
@@ -6,3 +6,4 @@ export { findClaudeExecutable as claudeAgentExecutable, platformSuffix as claude
|
|
|
6
6
|
export { anthropicPolicy, CLAUDE_WEB_SEARCH_TOOL, OWN_TOOL_PREFIX, thinks, withWebSearches } from "./host/policy.js";
|
|
7
7
|
export { claudeLoginStatus, loginCommand as claudeLoginCommand, loginEnv as claudeLoginEnv, parseLoginStatus as parseClaudeLoginStatus, startClaudeLogin } from "./login.js";
|
|
8
8
|
export { listedTools, MCP_OUTPUT_TOKENS, RESULT_SIZE_KEY, toolServer } from "./host/tool-server.js";
|
|
9
|
+
export { SIGN_IN_MS as CLAUDE_SIGN_IN_MS, signInEnv as claudeSignInEnv } from "./login.js";
|
package/dist/login.d.ts
CHANGED
|
@@ -6,12 +6,40 @@ export interface ClaudeAgentLogin {
|
|
|
6
6
|
dir: string;
|
|
7
7
|
/** The directory of the CLI's Anthropic profiles. */
|
|
8
8
|
profiles: string;
|
|
9
|
+
/** Set only for a person's own login on the CLI's default folder (`<home>/.claude`, no `CLAUDE_CONFIG_DIR`): their
|
|
10
|
+
* home. Their own CLI, run with no folder variable, keeps the account in `<home>/.claude.json` and, on macOS, its
|
|
11
|
+
* tokens in the Keychain item with no suffix; the variables below reach that same login (signInEnv, loginEnv). */
|
|
12
|
+
home?: string;
|
|
13
|
+
/** Set for this machine's own login, which the person's own CLI uses too (the default folder or their
|
|
14
|
+
* `CLAUDE_CONFIG_DIR`): a status check never runs the CLI on its account file, only on a copy (claudeLoginStatus). */
|
|
15
|
+
shared?: boolean;
|
|
9
16
|
}
|
|
10
|
-
/** The variables that point
|
|
11
|
-
* a credentials file out of when it resumes a session
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*
|
|
17
|
+
/** The variables that point a session's CLI at `login`: never its config dir (`CLAUDE_CONFIG_DIR`), which the Agent SDK
|
|
18
|
+
* copies a credentials file out of when it resumes a session, so the credentials dir (`CLAUDE_SECURESTORAGE_CONFIG_DIR`)
|
|
19
|
+
* is always set. On macOS the CLI names its Keychain item from that dir (`-<sha256 of it>` after the name) unless it is
|
|
20
|
+
* set and empty; unset, it would hash the session's own config dir. So a login on the CLI's default folder (`home`) gets
|
|
21
|
+
* an empty one on macOS (the item with no suffix, the person's own CLI's), and its folder elsewhere, where the CLI keeps
|
|
22
|
+
* `.credentials.json` in that dir. A status check on a login the person's own CLI uses has a config dir of its own too
|
|
23
|
+
* (a copy of the account file), so it takes this rule (claudeLoginStatus). `platform`: the one the CLI runs on, this
|
|
24
|
+
* host's. */
|
|
25
|
+
export declare function loginEnv(login: ClaudeAgentLogin, platform?: NodeJS.Platform): Record<string, string>;
|
|
26
|
+
/** The variables of the CLI's own sign-in at `login`. The line a person runs (loginCommand) and the sign-in this host
|
|
27
|
+
* starts (startClaudeLogin) both take these, and so does the status check (claudeLoginStatus) on AgentRun's own login,
|
|
28
|
+
* both in the person's home (personHome), so they cannot drift apart. Neither is a session, whose config dir stays its own (loginEnv). On a login the person's
|
|
29
|
+
* own CLI uses too (`home`, `shared`) the status check runs on a copy of the account file instead.
|
|
30
|
+
* - On the CLI's default folder (`home`): the person's own CLI as it runs, with no config or credentials dir, in their
|
|
31
|
+
* home (`HOME`; a terminal of theirs has it already, so the shown line leaves it out, and clears the two folder
|
|
32
|
+
* variables a terminal may export). It writes the account in `<home>/.claude.json` and the tokens where that person's
|
|
33
|
+
* CLI keeps them, on every platform.
|
|
34
|
+
* - Anywhere else: the login's folder as both the config dir and the credentials dir. `auth login` writes the account it
|
|
35
|
+
* signed in to its config dir's `.claude.json`, where the status check reads it (from a copy, on a folder the person's
|
|
36
|
+
* own CLI uses). For a folder the person's own
|
|
37
|
+
* CLI uses through `CLAUDE_CONFIG_DIR`, the credentials dir set to that folder is where that CLI keeps its tokens too
|
|
38
|
+
* (Linux: the same file; macOS: the same item, hashed from the same folder). */
|
|
39
|
+
export declare function signInEnv(login: ClaudeAgentLogin): Record<string, string>;
|
|
40
|
+
/** The line a person runs to sign the CLI in at `login` (the CLI opens a browser itself), with signInEnv's variables
|
|
41
|
+
* but HOME, which is their terminal's own. A folder variable signInEnv leaves unset (the CLI's default folder) is
|
|
42
|
+
* cleared with `env -u` (GNU's and macOS's env both have it), since a terminal that exports it would sign in there. */
|
|
15
43
|
export declare const loginCommand: (login: ClaudeAgentLogin, cli?: string) => string;
|
|
16
44
|
/** What the CLI says of its login: whether it is signed in, how, and the account's own words when it gives them. */
|
|
17
45
|
export interface ClaudeLoginStatus {
|
|
@@ -27,21 +55,62 @@ export interface ClaudeLoginStatus {
|
|
|
27
55
|
export declare function parseLoginStatus(stdout: string): ClaudeLoginStatus | undefined;
|
|
28
56
|
/** The longest a status check may take. */
|
|
29
57
|
export declare const LOGIN_STATUS_MS = 15000;
|
|
30
|
-
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
58
|
+
/** The home of the person whose CLI keeps `login`: the login's own (`home`, the CLI's default folder), else `given`, else
|
|
59
|
+
* the server's (`os.homedir()`). On macOS the CLI reads its tokens from the login keychain, which is under that home:
|
|
60
|
+
* in any other home (an empty one, a session's) it finds no keychain and reads signed out, whatever the variables. */
|
|
61
|
+
export declare const personHome: (login: ClaudeAgentLogin, given?: string) => string;
|
|
62
|
+
/** Ask the CLI at `cli` whether it is signed in at `login`: `auth status --json`, with only PATH and the login's
|
|
63
|
+
* variables beside it, in the person's home (personHome; `home` given, else the server's) on every place and platform.
|
|
64
|
+
* The CLI rewrites its account file (`.claude.json`, in its config dir) whenever that file's migration number is not its
|
|
65
|
+
* own, and adds a settings file, a backup and a lock beside it the first time, all in its config dir, so the check never
|
|
66
|
+
* runs it on a file the person's own CLI keeps:
|
|
67
|
+
* - AgentRun's own login: the sign-in's variables (signInEnv), so it reads the account `auth login` left in the login's
|
|
68
|
+
* folder, which is AgentRun's alone.
|
|
69
|
+
* - A login the person's own CLI uses too (the default folder, `home`; their `CLAUDE_CONFIG_DIR`, `shared`): the config
|
|
70
|
+
* dir is a copy of their account file in a folder of its own (`scratch`, one per check, removed after it answers or
|
|
71
|
+
* throws), less its secrets (accountCopy), and the credentials dir is a run's (loginEnv): on macOS it names the
|
|
72
|
+
* Keychain item ahead of the config dir (empty: the item with no suffix, the default folder's; else hashed from the
|
|
73
|
+
* folder), so the copy never renames it.
|
|
74
|
+
* A status check is no session: nothing resumes in its config dir. `platform`: the one the CLI runs on, this host's.
|
|
75
|
+
* Throws when the CLI gives no answer that reads as one. */
|
|
33
76
|
export declare function claudeLoginStatus(cli: string, login: ClaudeAgentLogin, options?: {
|
|
34
77
|
timeoutMs?: number;
|
|
35
78
|
path?: string;
|
|
79
|
+
platform?: NodeJS.Platform;
|
|
80
|
+
home?: string;
|
|
36
81
|
}): Promise<ClaudeLoginStatus>;
|
|
37
|
-
/**
|
|
38
|
-
|
|
82
|
+
/** How long the CLI's own sign-in waits for its browser before it is ended. */
|
|
83
|
+
export declare const SIGN_IN_MS: number;
|
|
84
|
+
/** How a sign-in ended: the CLI exited by itself (`exited`: `code` 0 when it signed in, or the signal that ended it),
|
|
85
|
+
* it was ended at its limit (`timed_out`) or by the host (`stopped`), or it never started (`failed`, in the spawn's
|
|
86
|
+
* words). */
|
|
87
|
+
export type ClaudeSignInEnd = {
|
|
88
|
+
end: "exited";
|
|
89
|
+
code: number | null;
|
|
90
|
+
signal: string | null;
|
|
91
|
+
} | {
|
|
92
|
+
end: "timed_out";
|
|
93
|
+
} | {
|
|
94
|
+
end: "stopped";
|
|
95
|
+
} | {
|
|
96
|
+
end: "failed";
|
|
97
|
+
message: string;
|
|
98
|
+
};
|
|
99
|
+
/** A sign-in startClaudeLogin started: `ended` settles once, with how it ended; `stop()` ends it (nothing once ended). */
|
|
100
|
+
export interface ClaudeSignIn {
|
|
101
|
+
ended: Promise<ClaudeSignInEnd>;
|
|
102
|
+
stop(): void;
|
|
103
|
+
}
|
|
104
|
+
/** Start the CLI's own sign-in at `login` (`auth login`), with the line's own variables (signInEnv) in the person's home
|
|
105
|
+
* (`home`) and desktop (`display`): it opens a browser on this machine and keeps what it gets in the login's folder.
|
|
106
|
+
* Nothing of it is read here: its output goes nowhere. It is ended after `timeoutMs` (SIGN_IN_MS by default), which
|
|
107
|
+
* `ended` says apart from any other end. */
|
|
39
108
|
export declare function startClaudeLogin(cli: string, login: ClaudeAgentLogin, options?: {
|
|
40
109
|
timeoutMs?: number;
|
|
41
110
|
path?: string;
|
|
42
111
|
home?: string;
|
|
43
112
|
display?: Readonly<Record<string, string | undefined>>;
|
|
44
|
-
}):
|
|
113
|
+
}): ClaudeSignIn;
|
|
45
114
|
/** How a session on a login books its requests: no proxy sees them, so each response the CLI streams is booked from its
|
|
46
115
|
* own events, billed to the plan, at no price here. No proxy can hold a request back either, so a request is admitted
|
|
47
116
|
* before the CLI can send it: when a message goes to the CLI (`sending`), and when a response stops for its tool calls,
|
package/dist/login.js
CHANGED
|
@@ -2,20 +2,59 @@
|
|
|
2
2
|
// Written for AgentRun.
|
|
3
3
|
//
|
|
4
4
|
// The pinned CLI keeps the login where it always does, in its own files, and calls Anthropic with it itself. AgentRun
|
|
5
|
-
// never reads, copies, stores or relays
|
|
5
|
+
// never reads, copies, stores or relays its tokens: a session only tells the CLI where its login is (two directories, by
|
|
6
6
|
// environment), and the host asks the CLI whether it is signed in (`claude auth status`), reading nothing but that answer.
|
|
7
|
+
// On a login the person's own CLI uses too, that check runs the CLI on a private copy of the account file
|
|
8
|
+
// (`.claude.json`), which the CLI rewrites on start, so the person's own file is only ever opened to read. The copy
|
|
9
|
+
// leaves out the fields that hold secrets (an API key, exported variables, MCP servers' tokens) and lasts the check.
|
|
10
|
+
// The check runs in the person's own home, as does a run on macOS, where the CLI's tokens are in the login keychain.
|
|
7
11
|
import { spawn } from "node:child_process";
|
|
8
|
-
import { mkdtempSync, rmSync } from "node:fs";
|
|
9
|
-
import { tmpdir } from "node:os";
|
|
12
|
+
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
13
|
+
import { homedir, tmpdir } from "node:os";
|
|
10
14
|
import { join } from "node:path";
|
|
11
|
-
/** The variables that point
|
|
12
|
-
* a credentials file out of when it resumes a session
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
/** The variables that point a session's CLI at `login`: never its config dir (`CLAUDE_CONFIG_DIR`), which the Agent SDK
|
|
16
|
+
* copies a credentials file out of when it resumes a session, so the credentials dir (`CLAUDE_SECURESTORAGE_CONFIG_DIR`)
|
|
17
|
+
* is always set. On macOS the CLI names its Keychain item from that dir (`-<sha256 of it>` after the name) unless it is
|
|
18
|
+
* set and empty; unset, it would hash the session's own config dir. So a login on the CLI's default folder (`home`) gets
|
|
19
|
+
* an empty one on macOS (the item with no suffix, the person's own CLI's), and its folder elsewhere, where the CLI keeps
|
|
20
|
+
* `.credentials.json` in that dir. A status check on a login the person's own CLI uses has a config dir of its own too
|
|
21
|
+
* (a copy of the account file), so it takes this rule (claudeLoginStatus). `platform`: the one the CLI runs on, this
|
|
22
|
+
* host's. */
|
|
23
|
+
export function loginEnv(login, platform = process.platform) {
|
|
24
|
+
return { CLAUDE_SECURESTORAGE_CONFIG_DIR: login.home !== undefined && platform === "darwin" ? "" : login.dir, ANTHROPIC_CONFIG_DIR: login.profiles };
|
|
15
25
|
}
|
|
16
|
-
/** The
|
|
17
|
-
*
|
|
18
|
-
|
|
26
|
+
/** The variables of the CLI's own sign-in at `login`. The line a person runs (loginCommand) and the sign-in this host
|
|
27
|
+
* starts (startClaudeLogin) both take these, and so does the status check (claudeLoginStatus) on AgentRun's own login,
|
|
28
|
+
* both in the person's home (personHome), so they cannot drift apart. Neither is a session, whose config dir stays its own (loginEnv). On a login the person's
|
|
29
|
+
* own CLI uses too (`home`, `shared`) the status check runs on a copy of the account file instead.
|
|
30
|
+
* - On the CLI's default folder (`home`): the person's own CLI as it runs, with no config or credentials dir, in their
|
|
31
|
+
* home (`HOME`; a terminal of theirs has it already, so the shown line leaves it out, and clears the two folder
|
|
32
|
+
* variables a terminal may export). It writes the account in `<home>/.claude.json` and the tokens where that person's
|
|
33
|
+
* CLI keeps them, on every platform.
|
|
34
|
+
* - Anywhere else: the login's folder as both the config dir and the credentials dir. `auth login` writes the account it
|
|
35
|
+
* signed in to its config dir's `.claude.json`, where the status check reads it (from a copy, on a folder the person's
|
|
36
|
+
* own CLI uses). For a folder the person's own
|
|
37
|
+
* CLI uses through `CLAUDE_CONFIG_DIR`, the credentials dir set to that folder is where that CLI keeps its tokens too
|
|
38
|
+
* (Linux: the same file; macOS: the same item, hashed from the same folder). */
|
|
39
|
+
export function signInEnv(login) {
|
|
40
|
+
if (login.home !== undefined)
|
|
41
|
+
return { HOME: login.home, ANTHROPIC_CONFIG_DIR: login.profiles };
|
|
42
|
+
return { CLAUDE_CONFIG_DIR: login.dir, CLAUDE_SECURESTORAGE_CONFIG_DIR: login.dir, ANTHROPIC_CONFIG_DIR: login.profiles };
|
|
43
|
+
}
|
|
44
|
+
/** The CLI's folder variables: where it keeps its config (the account) and its credentials. */
|
|
45
|
+
const FOLDER_VARS = ["CLAUDE_CONFIG_DIR", "CLAUDE_SECURESTORAGE_CONFIG_DIR"];
|
|
46
|
+
/** The line a person runs to sign the CLI in at `login` (the CLI opens a browser itself), with signInEnv's variables
|
|
47
|
+
* but HOME, which is their terminal's own. A folder variable signInEnv leaves unset (the CLI's default folder) is
|
|
48
|
+
* cleared with `env -u` (GNU's and macOS's env both have it), since a terminal that exports it would sign in there. */
|
|
49
|
+
export const loginCommand = (login, cli = "claude") => {
|
|
50
|
+
const env = signInEnv(login);
|
|
51
|
+
const cleared = FOLDER_VARS.filter((name) => !(name in env));
|
|
52
|
+
return [
|
|
53
|
+
...(cleared.length ? ["env", ...cleared.flatMap((name) => ["-u", name])] : []),
|
|
54
|
+
...Object.entries(env).filter(([name]) => name !== "HOME").map(([name, value]) => `${name}=${shellWord(value)}`),
|
|
55
|
+
shellWord(cli), "auth", "login",
|
|
56
|
+
].join(" ");
|
|
57
|
+
};
|
|
19
58
|
const shellWord = (word) => (/^[A-Za-z0-9_./:@%+=-]+$/.test(word) ? word : `'${word.replace(/'/g, `'\\''`)}'`);
|
|
20
59
|
const text = (value) => (typeof value === "string" && value.trim() ? value.trim().slice(0, 200) : undefined);
|
|
21
60
|
/** `claude auth status --json`'s answer, read for the fields above alone; undefined when it is no such answer. */
|
|
@@ -40,15 +79,92 @@ export function parseLoginStatus(stdout) {
|
|
|
40
79
|
}
|
|
41
80
|
/** The longest a status check may take. */
|
|
42
81
|
export const LOGIN_STATUS_MS = 15_000;
|
|
43
|
-
/**
|
|
44
|
-
*
|
|
45
|
-
|
|
82
|
+
/** The account file (`.claude.json`) the person's own CLI keeps for `login`, when they use it too: in their home on the
|
|
83
|
+
* CLI's default folder, in the folder itself for their `CLAUDE_CONFIG_DIR`. Undefined for AgentRun's own login. */
|
|
84
|
+
const sharedAccount = (login) => (login.home !== undefined ? join(login.home, ".claude.json") : login.shared ? join(login.dir, ".claude.json") : undefined);
|
|
85
|
+
/** The account file's fields that hold secrets, which its copy never gets: the API key an older CLI signed in with
|
|
86
|
+
* (`primaryApiKey`, the CLI's key when no other is set), the variables the CLI exports on start (`env`, which can hold
|
|
87
|
+
* `ANTHROPIC_API_KEY` or a token), and MCP servers, whose `env` and `headers` hold their tokens (`mcpServers`: the
|
|
88
|
+
* user's here, each folder's under `projects`). The rest holds none: `customApiKeyResponses` keeps the last 20
|
|
89
|
+
* characters of a key, `oauthAccount` the account's words; the tokens are in the credentials dir. A run's config dir
|
|
90
|
+
* has none of these either, so the check answers as a run would. */
|
|
91
|
+
const ACCOUNT_SECRETS = ["primaryApiKey", "env", "mcpServers"];
|
|
92
|
+
const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
93
|
+
/** The account file `raw` without its secrets (ACCOUNT_SECRETS), or undefined when it is no JSON object: nothing of a
|
|
94
|
+
* file that can't be read field by field is copied, as if it were missing. The CLI reads no account from such a file
|
|
95
|
+
* either (it calls it corrupted). */
|
|
96
|
+
function withoutSecrets(raw) {
|
|
97
|
+
// A UTF-8 byte order mark ahead of the JSON is skipped.
|
|
98
|
+
const text = raw[0] === 0xef && raw[1] === 0xbb && raw[2] === 0xbf ? raw.subarray(3) : raw;
|
|
99
|
+
let account;
|
|
100
|
+
try {
|
|
101
|
+
account = JSON.parse(text.toString("utf8"));
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
if (!isRecord(account))
|
|
107
|
+
return undefined;
|
|
108
|
+
const copy = { ...account };
|
|
109
|
+
for (const field of ACCOUNT_SECRETS)
|
|
110
|
+
delete copy[field];
|
|
111
|
+
if (isRecord(copy.projects)) {
|
|
112
|
+
copy.projects = Object.fromEntries(Object.entries(copy.projects).map(([folder, project]) => {
|
|
113
|
+
if (!isRecord(project))
|
|
114
|
+
return [folder, project];
|
|
115
|
+
const rest = { ...project };
|
|
116
|
+
delete rest.mcpServers;
|
|
117
|
+
return [folder, rest];
|
|
118
|
+
}));
|
|
119
|
+
}
|
|
120
|
+
return `${JSON.stringify(copy, null, 2)}\n`;
|
|
121
|
+
}
|
|
122
|
+
/** A new folder `to` (0700) holding a copy (0600) of the account file `from` without its secrets (withoutSecrets), or
|
|
123
|
+
* nothing when there is none or it is no JSON object. `from` is only read. */
|
|
124
|
+
function accountCopy(from, to) {
|
|
125
|
+
mkdirSync(to, { mode: 0o700 });
|
|
126
|
+
let raw;
|
|
127
|
+
try {
|
|
128
|
+
raw = readFileSync(from);
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
if (error.code !== "ENOENT")
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
const account = raw === undefined ? undefined : withoutSecrets(raw);
|
|
135
|
+
if (account !== undefined)
|
|
136
|
+
writeFileSync(join(to, ".claude.json"), account, { mode: 0o600, flag: "wx" });
|
|
137
|
+
return to;
|
|
138
|
+
}
|
|
139
|
+
/** The home of the person whose CLI keeps `login`: the login's own (`home`, the CLI's default folder), else `given`, else
|
|
140
|
+
* the server's (`os.homedir()`). On macOS the CLI reads its tokens from the login keychain, which is under that home:
|
|
141
|
+
* in any other home (an empty one, a session's) it finds no keychain and reads signed out, whatever the variables. */
|
|
142
|
+
export const personHome = (login, given) => login.home ?? given ?? homedir();
|
|
143
|
+
/** Ask the CLI at `cli` whether it is signed in at `login`: `auth status --json`, with only PATH and the login's
|
|
144
|
+
* variables beside it, in the person's home (personHome; `home` given, else the server's) on every place and platform.
|
|
145
|
+
* The CLI rewrites its account file (`.claude.json`, in its config dir) whenever that file's migration number is not its
|
|
146
|
+
* own, and adds a settings file, a backup and a lock beside it the first time, all in its config dir, so the check never
|
|
147
|
+
* runs it on a file the person's own CLI keeps:
|
|
148
|
+
* - AgentRun's own login: the sign-in's variables (signInEnv), so it reads the account `auth login` left in the login's
|
|
149
|
+
* folder, which is AgentRun's alone.
|
|
150
|
+
* - A login the person's own CLI uses too (the default folder, `home`; their `CLAUDE_CONFIG_DIR`, `shared`): the config
|
|
151
|
+
* dir is a copy of their account file in a folder of its own (`scratch`, one per check, removed after it answers or
|
|
152
|
+
* throws), less its secrets (accountCopy), and the credentials dir is a run's (loginEnv): on macOS it names the
|
|
153
|
+
* Keychain item ahead of the config dir (empty: the item with no suffix, the default folder's; else hashed from the
|
|
154
|
+
* folder), so the copy never renames it.
|
|
155
|
+
* A status check is no session: nothing resumes in its config dir. `platform`: the one the CLI runs on, this host's.
|
|
156
|
+
* Throws when the CLI gives no answer that reads as one. */
|
|
46
157
|
export async function claudeLoginStatus(cli, login, options = {}) {
|
|
47
158
|
const scratch = mkdtempSync(join(tmpdir(), "agentrun-claude-status-"));
|
|
48
159
|
try {
|
|
160
|
+
const account = sharedAccount(login);
|
|
161
|
+
const HOME = personHome(login, options.home);
|
|
162
|
+
const where = account === undefined
|
|
163
|
+
? { ...signInEnv(login), HOME }
|
|
164
|
+
: { HOME, ...loginEnv(login, options.platform), CLAUDE_CONFIG_DIR: accountCopy(account, join(scratch, "config")) };
|
|
49
165
|
const stdout = await new Promise((resolve, reject) => {
|
|
50
166
|
const child = spawn(cli, ["auth", "status", "--json"], {
|
|
51
|
-
env: { PATH: options.path ?? process.env.PATH ?? "/usr/bin:/bin",
|
|
167
|
+
env: { PATH: options.path ?? process.env.PATH ?? "/usr/bin:/bin", ...where, DISABLE_AUTOUPDATER: "1", DISABLE_TELEMETRY: "1", DISABLE_ERROR_REPORTING: "1", CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1" },
|
|
52
168
|
stdio: ["ignore", "pipe", "ignore"],
|
|
53
169
|
timeout: options.timeoutMs ?? LOGIN_STATUS_MS,
|
|
54
170
|
});
|
|
@@ -71,22 +187,59 @@ export async function claudeLoginStatus(cli, login, options = {}) {
|
|
|
71
187
|
rmSync(scratch, { recursive: true, force: true });
|
|
72
188
|
}
|
|
73
189
|
}
|
|
74
|
-
/**
|
|
75
|
-
|
|
190
|
+
/** How long the CLI's own sign-in waits for its browser before it is ended. */
|
|
191
|
+
export const SIGN_IN_MS = 10 * 60_000;
|
|
192
|
+
/** After asking an ending sign-in to stop, how long before it is killed. */
|
|
193
|
+
const SIGN_IN_KILL_MS = 5_000;
|
|
194
|
+
/** Start the CLI's own sign-in at `login` (`auth login`), with the line's own variables (signInEnv) in the person's home
|
|
195
|
+
* (`home`) and desktop (`display`): it opens a browser on this machine and keeps what it gets in the login's folder.
|
|
196
|
+
* Nothing of it is read here: its output goes nowhere. It is ended after `timeoutMs` (SIGN_IN_MS by default), which
|
|
197
|
+
* `ended` says apart from any other end. */
|
|
76
198
|
export function startClaudeLogin(cli, login, options = {}) {
|
|
77
|
-
const scratch = mkdtempSync(join(tmpdir(), "agentrun-claude-login-"));
|
|
78
199
|
// What a browser needs to open on this desktop, when the server has it.
|
|
79
200
|
const display = Object.fromEntries(["DISPLAY", "WAYLAND_DISPLAY", "XDG_RUNTIME_DIR", "DBUS_SESSION_BUS_ADDRESS", "BROWSER"].flatMap((name) => {
|
|
80
201
|
const value = options.display?.[name];
|
|
81
202
|
return typeof value === "string" ? [[name, value]] : [];
|
|
82
203
|
}));
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
204
|
+
let settle;
|
|
205
|
+
const ended = new Promise((resolve) => { settle = resolve; });
|
|
206
|
+
const failed = (error) => ({ end: "failed", message: String(error?.message ?? error).slice(0, 200) });
|
|
207
|
+
let child;
|
|
208
|
+
try {
|
|
209
|
+
child = spawn(cli, ["auth", "login", "--claudeai"], {
|
|
210
|
+
env: { PATH: options.path ?? process.env.PATH ?? "/usr/bin:/bin", HOME: personHome(login, options.home), ...display, ...signInEnv(login), DISABLE_AUTOUPDATER: "1", DISABLE_TELEMETRY: "1", DISABLE_ERROR_REPORTING: "1" },
|
|
211
|
+
stdio: "ignore",
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
catch (error) {
|
|
215
|
+
settle(failed(error));
|
|
216
|
+
return { ended, stop: () => { } };
|
|
217
|
+
}
|
|
218
|
+
let done = false;
|
|
219
|
+
let ending;
|
|
220
|
+
let kill;
|
|
221
|
+
const end = (why) => {
|
|
222
|
+
if (done || ending)
|
|
223
|
+
return;
|
|
224
|
+
ending = why;
|
|
225
|
+
child.kill("SIGTERM");
|
|
226
|
+
kill = setTimeout(() => child.kill("SIGKILL"), SIGN_IN_KILL_MS);
|
|
227
|
+
kill.unref();
|
|
228
|
+
};
|
|
229
|
+
const limit = setTimeout(() => end("timed_out"), options.timeoutMs ?? SIGN_IN_MS);
|
|
230
|
+
limit.unref();
|
|
231
|
+
const finish = (result) => {
|
|
232
|
+
if (done)
|
|
233
|
+
return;
|
|
234
|
+
done = true;
|
|
235
|
+
clearTimeout(limit);
|
|
236
|
+
if (kill)
|
|
237
|
+
clearTimeout(kill);
|
|
238
|
+
settle(result);
|
|
239
|
+
};
|
|
240
|
+
child.once("error", (error) => finish(ending ? { end: ending } : failed(error)));
|
|
241
|
+
child.once("close", (code, signal) => finish(ending ? { end: ending } : { end: "exited", code, signal }));
|
|
242
|
+
return { ended, stop: () => end("stopped") };
|
|
90
243
|
}
|
|
91
244
|
const count = (value) => (typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined);
|
|
92
245
|
/** How a session on a login books its requests: no proxy sees them, so each response the CLI streams is booked from its
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcha/agentrun-harness-claude",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.56",
|
|
4
4
|
"description": "The Claude Agent SDK as an AgentRun harness: the host's tools as its only tools, its model behind a loopback proxy, durable in AgentRun's own store.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agentrun",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"test": "node --test --test-concurrency=8 'test/*.test.mjs'"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@parcha/agentrun-kernel": "0.1.0-beta.
|
|
51
|
+
"@parcha/agentrun-kernel": "0.1.0-beta.56"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"@anthropic-ai/claude-agent-sdk": "0.3.286",
|
|
@@ -77,6 +77,6 @@
|
|
|
77
77
|
},
|
|
78
78
|
"devDependencies": {
|
|
79
79
|
"@anthropic-ai/claude-agent-sdk": "0.3.286",
|
|
80
|
-
"@parcha/agentrun-dsl": "0.1.0-beta.
|
|
80
|
+
"@parcha/agentrun-dsl": "0.1.0-beta.56"
|
|
81
81
|
}
|
|
82
82
|
}
|