@parcha/agentrun-serve 0.1.0-beta.58 → 0.1.0-beta.59
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 +325 -1
- package/README.md +332 -88
- package/dist/acp-package.d.ts +5 -1
- package/dist/acp-package.js +19 -2
- package/dist/adapters.d.ts +23 -2
- package/dist/adapters.js +106 -33
- package/dist/agents-store.js +1 -1
- package/dist/answer-text.d.ts +3 -0
- package/dist/answer-text.js +22 -0
- package/dist/attach-conformance.js +5 -4
- package/dist/box-paths.d.ts +3 -2
- package/dist/box-paths.js +5 -4
- package/dist/browser-computer.d.ts +3 -8
- package/dist/browser-computer.js +12 -35
- package/dist/browser-cost.js +94 -20
- package/dist/capacity.d.ts +1 -1
- package/dist/capacity.js +3 -3
- package/dist/chatgpt-plan.d.ts +5 -0
- package/dist/chatgpt-plan.js +25 -11
- package/dist/claude-login-token.d.ts +15 -11
- package/dist/claude-login-token.js +167 -107
- package/dist/claude-login.d.ts +76 -31
- package/dist/claude-login.js +426 -79
- package/dist/claude-sign-in.d.ts +3 -0
- package/dist/claude-sign-in.js +10 -1
- package/dist/cli.js +323 -292
- package/dist/composio-made.d.ts +1 -6
- package/dist/composio-made.js +1 -6
- package/dist/composio-owners.d.ts +26 -0
- package/dist/composio-owners.js +36 -0
- package/dist/composio-preset.d.ts +56 -23
- package/dist/composio-preset.js +173 -77
- package/dist/composio-triggers.d.ts +4 -4
- package/dist/composio-triggers.js +6 -7
- package/dist/compute-cost.js +11 -2
- package/dist/computer-attach.d.ts +6 -3
- package/dist/computer-attach.js +41 -29
- package/dist/computer-held-routes.d.ts +1 -1
- package/dist/computer-held-routes.js +9 -4
- package/dist/computer-keys.d.ts +11 -1
- package/dist/computer-keys.js +26 -1
- package/dist/computer-record.js +3 -1
- package/dist/computer-view.d.ts +5 -3
- package/dist/computer-view.js +10 -6
- package/dist/computer-viewer.js +36 -7
- package/dist/computers.d.ts +2 -1
- package/dist/computers.js +6 -3
- package/dist/console/THIRD-PARTY-NOTICES.txt +50 -0
- package/dist/console/assets/backend-BztxEAvb.js +166 -0
- package/dist/console/assets/index-LwNL4L9q.js +619 -0
- package/dist/console/assets/share-L84tlUDW.js +6 -0
- package/dist/console/assets/styles-CaRKlHke.js +291 -0
- package/dist/console/assets/styles-DQo8rzY-.css +1 -0
- package/dist/console/index.html +4 -3
- package/dist/console/share.html +17 -0
- package/dist/decisions.d.ts +17 -7
- package/dist/decisions.js +41 -15
- package/dist/dial-relay.js +10 -4
- package/dist/docker-daemon.js +4 -5
- package/dist/docker-host-addresses.js +29 -11
- package/dist/email/account.d.ts +6 -0
- package/dist/email/account.js +6 -0
- package/dist/email/authentication.d.ts +21 -0
- package/dist/email/authentication.js +106 -0
- package/dist/email/email-faux.d.ts +11 -2
- package/dist/email/email-faux.js +21 -5
- package/dist/email/mail.d.ts +25 -0
- package/dist/email/mail.js +58 -2
- package/dist/email/names.js +2 -2
- package/dist/email/tools.d.ts +9 -4
- package/dist/email/tools.js +95 -13
- package/dist/env-switch.d.ts +12 -2
- package/dist/env-switch.js +23 -3
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +2 -0
- package/dist/fal-price.d.ts +11 -1
- package/dist/fal-price.js +33 -0
- package/dist/faux-computers.js +1 -1
- package/dist/filed-answer.d.ts +2 -2
- package/dist/filed-answer.js +2 -2
- package/dist/graph-served.d.ts +10 -5
- package/dist/graph-served.js +40 -10
- package/dist/handed-values.js +1 -2
- package/dist/harness-check.d.ts +2 -1
- package/dist/harness-check.js +45 -12
- package/dist/harness-keys.js +9 -6
- package/dist/harness-opencode/facts.js +2 -0
- package/dist/harness-recipe-entry.d.ts +9 -3
- package/dist/harness-recipe-entry.js +18 -9
- package/dist/harness-recipe.d.ts +21 -0
- package/dist/harness-recipe.js +25 -3
- package/dist/harness-table.d.ts +8 -0
- package/dist/harness-table.js +17 -6
- package/dist/helper-env.d.ts +4 -1
- package/dist/helper-env.js +4 -1
- package/dist/host-check.d.ts +2 -3
- package/dist/host-check.js +13 -7
- package/dist/hosted-search.js +1 -1
- package/dist/import.d.ts +11 -8
- package/dist/import.js +437 -186
- package/dist/key-refusal.d.ts +9 -2
- package/dist/key-refusal.js +86 -32
- package/dist/learn/audit.d.ts +8 -5
- package/dist/learn/audit.js +15 -7
- package/dist/learn/compile.d.ts +5 -2
- package/dist/learn/compile.js +45 -21
- package/dist/learn/field-check.d.ts +111 -0
- package/dist/learn/field-check.js +174 -0
- package/dist/learn/ledger.d.ts +32 -29
- package/dist/learn/ledger.js +26 -19
- package/dist/learn/promote.d.ts +79 -36
- package/dist/learn/promote.js +60 -65
- package/dist/learn/secrets.d.ts +4 -0
- package/dist/learn/secrets.js +12 -0
- package/dist/learned-nodes.d.ts +8 -5
- package/dist/learned-nodes.js +8 -5
- package/dist/learned-workflow.d.ts +2 -1
- package/dist/learned-workflow.js +13 -10
- package/dist/learning-cost.d.ts +25 -8
- package/dist/learning-cost.js +23 -8
- package/dist/learning-setting.d.ts +6 -0
- package/dist/learning-setting.js +11 -1
- package/dist/listen-first.d.ts +5 -4
- package/dist/listen-first.js +4 -5
- package/dist/live-reach.d.ts +7 -0
- package/dist/live-reach.js +7 -0
- package/dist/mcp-api-only-origins.d.ts +15 -0
- package/dist/mcp-api-only-origins.js +40 -0
- package/dist/mcp-gateway.d.ts +16 -2
- package/dist/mcp-gateway.js +41 -3
- package/dist/mcp-oauth.d.ts +1 -1
- package/dist/mcp-oauth.js +3 -2
- package/dist/mcp-probe.d.ts +3 -0
- package/dist/mcp-probe.js +3 -0
- package/dist/mcp-secret-writes.d.ts +24 -2
- package/dist/mcp-secret-writes.js +30 -2
- package/dist/mcp-servers-store.d.ts +52 -14
- package/dist/mcp-servers-store.js +165 -61
- package/dist/mcp-sources.d.ts +1 -2
- package/dist/mcp-sources.js +1 -2
- package/dist/mcp-store.d.ts +5 -5
- package/dist/mcp-store.js +3 -3
- package/dist/mcp.js +1 -1
- package/dist/model-gateway.d.ts +1 -1
- package/dist/model-gateway.js +20 -36
- package/dist/ollama-provider.d.ts +23 -9
- package/dist/ollama-provider.js +49 -20
- package/dist/openapi.js +202 -51
- package/dist/otel/genai.d.ts +83 -7
- package/dist/otel/genai.js +69 -9
- package/dist/otel/tracer.d.ts +42 -13
- package/dist/otel/tracer.js +241 -56
- package/dist/page.d.ts +7 -1
- package/dist/page.js +76 -22
- package/dist/paid-calls.d.ts +6 -4
- package/dist/paid-calls.js +15 -7
- package/dist/place-offers.d.ts +55 -34
- package/dist/place-offers.js +93 -54
- package/dist/place.d.ts +13 -14
- package/dist/place.js +56 -41
- package/dist/print.js +1 -1
- package/dist/provider.d.ts +3 -2
- package/dist/provider.js +17 -8
- package/dist/redact/match.d.ts +27 -9
- package/dist/redact/match.js +266 -57
- package/dist/run-census.d.ts +1 -1
- package/dist/run-census.js +4 -5
- package/dist/run-cut.js +4 -5
- package/dist/run-export-store.d.ts +15 -0
- package/dist/run-export-store.js +30 -0
- package/dist/run-export.d.ts +139 -0
- package/dist/run-export.js +379 -0
- package/dist/run-file-cut.d.ts +5 -0
- package/dist/run-file-cut.js +13 -0
- package/dist/run-launch.js +16 -3
- package/dist/run-model-key.d.ts +15 -6
- package/dist/run-model-key.js +30 -31
- package/dist/run-reads-over-store.js +4 -4
- package/dist/run-secrets.js +3 -5
- package/dist/run-timeline-served.js +3 -1
- package/dist/runnable.js +2 -2
- package/dist/runner/adapter-loop.d.ts +35 -18
- package/dist/runner/adapter-loop.js +94 -47
- package/dist/runner/adapter-run-spec.d.ts +14 -1
- package/dist/runner/adapter-run-spec.js +64 -28
- package/dist/runner/adapters/pi.js +4 -2
- package/dist/runner/audit.d.ts +8 -5
- package/dist/runner/audit.js +37 -17
- package/dist/runner/beside.js +5 -6
- package/dist/runner/claim-secrets.d.ts +10 -3
- package/dist/runner/claim-secrets.js +29 -13
- package/dist/runner/cli-run-spec.d.ts +18 -0
- package/dist/runner/cli-run-spec.js +81 -46
- package/dist/runner/compare-end.d.ts +2 -1
- package/dist/runner/compare-end.js +25 -10
- package/dist/runner/conformance/harness.js +2 -3
- package/dist/runner/conformance/subject.d.ts +4 -1
- package/dist/runner/harness-loops.js +3 -3
- package/dist/runner/held.d.ts +5 -2
- package/dist/runner/journal-kinds.d.ts +4 -1
- package/dist/runner/journal-kinds.js +22 -4
- package/dist/runner/learned-first.js +20 -4
- package/dist/runner/learning-end.d.ts +2 -8
- package/dist/runner/learning-end.js +11 -19
- package/dist/runner/live-ticker.js +7 -5
- package/dist/runner/loop-contract.d.ts +0 -2
- package/dist/runner/loop.d.ts +7 -0
- package/dist/runner/loop.js +1 -0
- package/dist/runner/main.js +12 -11
- package/dist/runner/mcp-claim.js +21 -5
- package/dist/runner/meta-tools.js +3 -2
- package/dist/runner/metrics.d.ts +7 -0
- package/dist/runner/metrics.js +19 -0
- package/dist/runner/model-route.d.ts +10 -7
- package/dist/runner/model-route.js +79 -15
- package/dist/runner/native-loop.js +10 -8
- package/dist/runner/presence.d.ts +1 -4
- package/dist/runner/presence.js +6 -8
- package/dist/runner/promotion.d.ts +25 -13
- package/dist/runner/promotion.js +125 -50
- package/dist/runner/redact.d.ts +3 -1
- package/dist/runner/redact.js +5 -3
- package/dist/runner/responses-fetch.d.ts +36 -0
- package/dist/runner/responses-fetch.js +511 -0
- package/dist/runner/run-child-host.d.ts +20 -5
- package/dist/runner/run-child-host.js +179 -77
- package/dist/runner/run-child.js +11 -4
- package/dist/runner/runner-check.js +5 -1
- package/dist/runner/runner.d.ts +2 -3
- package/dist/runner/runner.js +13 -9
- package/dist/runner/sdk-loop.js +8 -8
- package/dist/runner/spec-holds.d.ts +8 -1
- package/dist/runner/spec-holds.js +9 -4
- package/dist/runner/workflow-calls.d.ts +9 -0
- package/dist/runner/workflow-calls.js +150 -0
- package/dist/runner/workflow-first.d.ts +120 -32
- package/dist/runner/workflow-first.js +218 -94
- package/dist/runner/workflow-spec.js +1 -1
- package/dist/runs-over-store.js +8 -7
- package/dist/save-once.d.ts +14 -0
- package/dist/save-once.js +31 -0
- package/dist/scrub-command.d.ts +34 -31
- package/dist/scrub-command.js +116 -147
- package/dist/sdk/runs.d.ts +6 -0
- package/dist/sdk/runs.js +89 -10
- package/dist/seal-key.js +4 -4
- package/dist/secret-cut.d.ts +3 -0
- package/dist/secret-cut.js +7 -1
- package/dist/secret-rules.d.ts +8 -1
- package/dist/secret-rules.js +482 -53
- package/dist/secrets.d.ts +51 -9
- package/dist/secrets.js +311 -21
- package/dist/server-keys.d.ts +4 -3
- package/dist/server-keys.js +4 -3
- package/dist/server-secrets-store.js +2 -4
- package/dist/server.d.ts +15 -3
- package/dist/server.js +312 -80
- package/dist/share-context.d.ts +22 -0
- package/dist/share-context.js +76 -0
- package/dist/shell-lex.d.ts +105 -0
- package/dist/shell-lex.js +646 -0
- package/dist/sign-ins-store.d.ts +5 -7
- package/dist/sign-ins-store.js +12 -16
- package/dist/sign-ins.d.ts +7 -11
- package/dist/sign-ins.js +40 -41
- package/dist/skills/agentrun-serve/SKILL.md +5 -2
- package/dist/start-switches.d.ts +6 -5
- package/dist/start-switches.js +8 -4
- package/dist/stats.d.ts +5 -1
- package/dist/stats.js +1 -1
- package/dist/store/agent-body.js +2 -2
- package/dist/store/dialect.d.ts +4 -0
- package/dist/store/dialect.js +1 -0
- package/dist/store/index.d.ts +2 -2
- package/dist/store/index.js +1 -1
- package/dist/store/mcp-record-keys.d.ts +10 -0
- package/dist/store/mcp-record-keys.js +13 -0
- package/dist/store/postgres/dialect.js +2 -0
- package/dist/store/postgres/migrations/0029_run_launch_census_settle.sql +28 -0
- package/dist/store/postgres/migrations/0030_learning_ledger_epoch.sql +40 -0
- package/dist/store/postgres/migrations/0031_learning_ledger_calls.sql +12 -0
- package/dist/store/postgres/store.js +4 -0
- package/dist/store/schema.d.ts +33 -10
- package/dist/store/schema.js +70 -16
- package/dist/store/seal.d.ts +4 -1
- package/dist/store/sqlite/run-launch.d.ts +3 -10
- package/dist/store/sqlite/run-launch.js +12 -23
- package/dist/store/sqlite/run-patch.d.ts +17 -10
- package/dist/store/sqlite/run-patch.js +50 -13
- package/dist/store/sqlite/store.d.ts +3 -0
- package/dist/store/sqlite/store.js +116 -34
- package/dist/store/store-schema.js +3 -4
- package/dist/store/store.d.ts +67 -32
- package/dist/store/store.js +14 -11
- package/dist/store-reads/analytics.d.ts +2 -1
- package/dist/store-reads/analytics.js +7 -5
- package/dist/store-reads/learning.d.ts +35 -26
- package/dist/store-reads/learning.js +120 -92
- package/dist/store-reads/stats.d.ts +2 -2
- package/dist/store-reads/stats.js +13 -7
- package/dist/store-view.js +10 -7
- package/dist/suggestions.d.ts +1 -1
- package/dist/suggestions.js +29 -13
- package/dist/template-eval.d.ts +72 -9
- package/dist/template-eval.js +358 -64
- package/dist/timeline/labels.d.ts +1 -1
- package/dist/timeline/labels.js +4 -3
- package/dist/timeline/timeline.d.ts +23 -11
- package/dist/timeline/timeline.js +115 -31
- package/dist/timeline/web-calls.d.ts +8 -6
- package/dist/timeline/web-calls.js +10 -6
- package/dist/tool-gate.d.ts +13 -1
- package/dist/tool-gate.js +43 -3
- package/dist/tool-keys-store.js +2 -4
- package/dist/tool-keys.d.ts +11 -0
- package/dist/tool-keys.js +16 -0
- package/dist/tool-plane.d.ts +50 -11
- package/dist/tool-plane.js +94 -30
- package/dist/tools.d.ts +5 -1
- package/dist/tools.js +7 -7
- package/dist/trigger-email.js +12 -6
- package/dist/triggers.d.ts +15 -34
- package/dist/triggers.js +119 -126
- package/dist/validate.d.ts +8 -1
- package/dist/validate.js +163 -23
- package/dist/web-fake.d.ts +2 -1
- package/dist/web-fake.js +8 -6
- package/dist/web-fetch.d.ts +8 -6
- package/dist/web-fetch.js +13 -11
- package/dist/web-holds.d.ts +32 -0
- package/dist/web-holds.js +86 -0
- package/dist/web-run.d.ts +32 -5
- package/dist/web-run.js +75 -28
- package/dist/web-save.js +23 -12
- package/dist/web-search.js +14 -25
- package/dist/wires.d.ts +4 -3
- package/dist/wires.js +5 -4
- package/dist/workspace/archil-claim.d.ts +12 -5
- package/dist/workspace/archil-claim.js +45 -5
- package/dist/workspace/attach.js +52 -19
- package/dist/workspace/attached-computers.d.ts +6 -2
- package/dist/workspace/attached-computers.js +5 -4
- package/dist/workspace/computer-deps.d.ts +23 -6
- package/dist/workspace/computer-deps.js +35 -17
- package/dist/workspace/computer-for-claim.d.ts +21 -4
- package/dist/workspace/computer-for-claim.js +160 -54
- package/dist/workspace/computer-intervals.d.ts +2 -0
- package/dist/workspace/computer-intervals.js +1 -1
- package/dist/workspace/computer-janitor.d.ts +48 -14
- package/dist/workspace/computer-janitor.js +153 -44
- package/dist/workspace/computer-kept.d.ts +158 -32
- package/dist/workspace/computer-kept.js +288 -63
- package/dist/workspace/computer-providers.d.ts +8 -3
- package/dist/workspace/computer-providers.js +12 -5
- package/dist/workspace/index.d.ts +1 -1
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/local.js +41 -1
- package/dist/workspace/mount-revoke-left.d.ts +31 -0
- package/dist/workspace/mount-revoke-left.js +72 -0
- package/dist/workspace/paired-route.js +4 -5
- package/dist/workspace/resolve-computer.d.ts +5 -2
- package/dist/workspace/resolve-computer.js +9 -3
- package/dist/workspace/run-end-computers.d.ts +11 -5
- package/dist/workspace/run-end-computers.js +153 -28
- package/dist/workspace/run-tools.d.ts +22 -3
- package/dist/workspace/run-tools.js +17 -5
- package/dist/workspace/run-workspace.d.ts +14 -1
- package/dist/workspace/run-workspace.js +34 -1
- package/dist/workspace/workspace.d.ts +6 -0
- package/package.json +12 -13
- package/skills/agentrun-serve/SKILL.md +5 -2
- package/templates/README.md +22 -6
- package/dist/console/assets/backend-CSAWUqtZ.js +0 -166
- package/dist/console/assets/index-Bwn1oTem.css +0 -1
- package/dist/console/assets/index-DPh29cyz.js +0 -747
|
@@ -9,11 +9,14 @@
|
|
|
9
9
|
// flight per refresh token in this process, and across processes the renewer holds the CLI's own refresh lock on the
|
|
10
10
|
// folder and reads the file again once it holds it, so a token another holder (the CLI, another runner) renewed is
|
|
11
11
|
// taken and nothing is posted. It holds that lock as the CLI's lock library does, by the directory it made and the mtime
|
|
12
|
-
// it set (
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
12
|
+
// it set (within 10 µs, or the start of the second that mtime falls in, which is how a filesystem that keeps whole
|
|
13
|
+
// seconds alone shows it and how the CLI's library reads its own there; that second is taken on every filesystem), and
|
|
14
|
+
// a lock another holder took over is never touched, removed or posted under once a check reads it. A read that fails
|
|
15
|
+
// for a reason other than the lock being gone is no takeover: the lock is read once more before a release gives up on
|
|
16
|
+
// it. Runners make or take over a lock one at a time, under a takeover mark beside it, so between runners one holder at
|
|
17
|
+
// a time holds it, unless a runner stalls past the 60 s stale time inside a takeover or its release, or a runner crashes
|
|
18
|
+
// holding the mark and two runners then take that dead mark over at once (one's late removal lands in the mark the
|
|
19
|
+
// other made). The CLI never sees the mark: between a runner and the CLI the lock is as safe as the CLI's own (proper-lockfile), and the
|
|
17
20
|
// CLI's second lock, beside the folder, keeps a takeover's late removal (or, on such a filesystem, a lock made in the
|
|
18
21
|
// runner's second) from letting both post where that lock can be made. The answer is written back only to the file it
|
|
19
22
|
// came from, atomically (a temp file at mode 0600 in the same folder before any byte is in it, synced, then renamed over
|
|
@@ -37,9 +40,9 @@ import { lstat, mkdir, open, readFile, realpath, rename, rm, stat, utimes } from
|
|
|
37
40
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
38
41
|
import { dirname, join } from "node:path";
|
|
39
42
|
import { loginRefresh } from "@parcha/agentrun-harness-claude/facts";
|
|
40
|
-
import {
|
|
41
|
-
import { PRESENCE_STALE_MS } from "./runner/presence.js";
|
|
43
|
+
import { agentRunLoginDir, LOGIN_SIGNED_OUT } from "./claude-login.js";
|
|
42
44
|
import { holdSecret } from "./secrets.js";
|
|
45
|
+
import { RUNNER_STALE_MS } from "./store/index.js";
|
|
43
46
|
/** The pinned CLI's credentials file in its credentials directory (CLAUDE_SECURESTORAGE_CONFIG_DIR). */
|
|
44
47
|
export const LOGIN_FILE = ".credentials.json";
|
|
45
48
|
/** The least a token handed to a run has left: the pinned CLI's own margin before it calls a token expired (its
|
|
@@ -70,32 +73,6 @@ class LoginNotSaved extends Error {
|
|
|
70
73
|
/** The words a run gets when the login is kept in a form this runner does not read (not the pinned CLI's plaintext file). */
|
|
71
74
|
export const LOGIN_SEALED = "Claude Code's login on this machine is kept in a form this runner cannot hand to a run of its own user: set AGENTRUN_RUN_USER=same, or give the run a model_key.";
|
|
72
75
|
const strings = (values) => values.filter((value) => typeof value === "string");
|
|
73
|
-
/** The access token in a credentials file's text, in the pinned CLI's shape (`claudeAiOauth.{accessToken, expiresAt}`),
|
|
74
|
-
* every value in it held for redaction. No `claudeAiOauth`, or one the CLI cleared (an empty token), is signed out;
|
|
75
|
-
* anything else is a form this runner does not read. */
|
|
76
|
-
function readLogin(text) {
|
|
77
|
-
let parsed;
|
|
78
|
-
try {
|
|
79
|
-
parsed = JSON.parse(text);
|
|
80
|
-
}
|
|
81
|
-
catch {
|
|
82
|
-
return { refused: LOGIN_SEALED };
|
|
83
|
-
}
|
|
84
|
-
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed))
|
|
85
|
-
return { refused: LOGIN_SEALED };
|
|
86
|
-
const oauth = parsed.claudeAiOauth;
|
|
87
|
-
if (oauth === undefined || oauth === null)
|
|
88
|
-
return { refused: LOGIN_SIGNED_OUT };
|
|
89
|
-
if (typeof oauth !== "object" || Array.isArray(oauth))
|
|
90
|
-
return { refused: LOGIN_SEALED };
|
|
91
|
-
const { accessToken, refreshToken, expiresAt } = oauth;
|
|
92
|
-
holdSecret(...strings([accessToken, refreshToken]));
|
|
93
|
-
if (accessToken === "")
|
|
94
|
-
return { refused: LOGIN_SIGNED_OUT };
|
|
95
|
-
if (typeof accessToken !== "string" || typeof expiresAt !== "number" || !Number.isFinite(expiresAt))
|
|
96
|
-
return { refused: LOGIN_SEALED };
|
|
97
|
-
return { access: accessToken, expiresAt };
|
|
98
|
-
}
|
|
99
76
|
/** The runner's reader of one login's access token, and its renewer. */
|
|
100
77
|
export class LoginTokens {
|
|
101
78
|
#file;
|
|
@@ -165,16 +142,21 @@ export class LoginTokens {
|
|
|
165
142
|
if (!("refused" in read) && (read.access === sent || heldPairs.get(this.#file)?.renewed.access === sent))
|
|
166
143
|
this.#refusedAccess = sent;
|
|
167
144
|
}
|
|
145
|
+
/** The login's access token and its end, as its file holds them now (credentialsRead). No file, no `claudeAiOauth`, or
|
|
146
|
+
* one the CLI cleared (an empty token), is signed out; anything else, a file the runner may not open included, is a
|
|
147
|
+
* form this runner does not read. */
|
|
168
148
|
async #read() {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
149
|
+
const read = await credentialsRead(this.#file);
|
|
150
|
+
if (read === "gone")
|
|
151
|
+
return { refused: LOGIN_SIGNED_OUT };
|
|
152
|
+
if (typeof read === "string")
|
|
153
|
+
return { refused: LOGIN_SEALED };
|
|
154
|
+
const { accessToken, expiresAt } = read.oauth;
|
|
155
|
+
if (accessToken === "")
|
|
156
|
+
return { refused: LOGIN_SIGNED_OUT };
|
|
157
|
+
if (typeof accessToken !== "string" || typeof expiresAt !== "number" || !Number.isFinite(expiresAt))
|
|
158
|
+
return { refused: LOGIN_SEALED };
|
|
159
|
+
return { access: accessToken, expiresAt };
|
|
178
160
|
}
|
|
179
161
|
}
|
|
180
162
|
/** Refresh tokens this process never posts again: one Anthropic refused for good (`invalid_grant`), and one it took (a
|
|
@@ -187,25 +169,43 @@ const flights = new Map();
|
|
|
187
169
|
const heldPairs = new Map();
|
|
188
170
|
/** `held` written into `file` as the pinned CLI writes a renewal back (login-refresh.ts): only while the file still holds
|
|
189
171
|
* the refresh token it spent (or one the CLI cleared), the fields it gives over the file's, the rest kept as they are.
|
|
190
|
-
* "saved" once written
|
|
191
|
-
*
|
|
172
|
+
* "saved" once written (or found written: another ask saved it), "held" while the write or the read before it fails
|
|
173
|
+
* (kept in heldPairs), and "dropped" when the file holds another holder's pair (or is gone, or holds no login the
|
|
174
|
+
* runner reads): the file wins, as the CLI's own check lets it. */
|
|
192
175
|
async function saveHeld(file, held, options, owned) {
|
|
193
176
|
const say = options.log ?? (() => undefined);
|
|
194
177
|
const forget = () => {
|
|
195
178
|
if (heldPairs.get(file) === held)
|
|
196
179
|
heldPairs.delete(file);
|
|
197
180
|
};
|
|
198
|
-
const current = await
|
|
199
|
-
|
|
181
|
+
const current = await credentialsRead(file, fsOf(options).readFile);
|
|
182
|
+
// A read that failed says nothing of which pair the file holds, and the held pair's refresh token may be the only one
|
|
183
|
+
// left that renews the login (Anthropic rotated it): the pair stays held, and a later ask reads the file again. It is
|
|
184
|
+
// dropped only once a read shows the file gone or holding another pair.
|
|
185
|
+
if (current === "unread") {
|
|
186
|
+
heldPairs.set(file, held);
|
|
187
|
+
say({ claude_login: "renewed", saved: "no", reason: "file_unread" });
|
|
188
|
+
return "held";
|
|
189
|
+
}
|
|
190
|
+
// The file holds this very pair: another ask of this process saved it since this one took it from heldPairs. Saved,
|
|
191
|
+
// and said so, never as a newer pair, which reads as another holder's takeover.
|
|
192
|
+
if (typeof current !== "string" && current.oauth.accessToken === held.fields.accessToken && current.oauth.refreshToken === held.fields.refreshToken) {
|
|
193
|
+
forget();
|
|
194
|
+
say({ claude_login: "renewed", saved: "yes", reason: "already_saved" });
|
|
195
|
+
return "saved";
|
|
196
|
+
}
|
|
197
|
+
if (typeof current === "string" || (current.oauth.refreshToken !== held.posted && current.oauth.refreshToken !== "")) {
|
|
200
198
|
forget();
|
|
201
199
|
say({ claude_login: "renewed", saved: "no", reason: "newer" });
|
|
202
200
|
return "dropped";
|
|
203
201
|
}
|
|
204
202
|
// `owned`: the refresh lock this save holds, asked after the read and before the write. One lost since (taken over)
|
|
205
|
-
// means another holder may write now: the pair stays held, and a later ask tries
|
|
206
|
-
|
|
203
|
+
// means another holder may write now, and one whose read failed may be: the pair stays held, and a later ask tries
|
|
204
|
+
// again.
|
|
205
|
+
const lock = owned ? await owned() : "own";
|
|
206
|
+
if (lock !== "own") {
|
|
207
207
|
heldPairs.set(file, held);
|
|
208
|
-
say({ claude_login: "renewed", saved: "no", reason:
|
|
208
|
+
say({ claude_login: "renewed", saved: "no", reason: lockWord(lock) });
|
|
209
209
|
return "held";
|
|
210
210
|
}
|
|
211
211
|
try {
|
|
@@ -228,7 +228,7 @@ async function saveHeld(file, held, options, owned) {
|
|
|
228
228
|
* made at all (a full disk refuses a mkdir too). */
|
|
229
229
|
async function saveHeldUnderLock(dir, file, held, options) {
|
|
230
230
|
const say = options.log ?? (() => undefined);
|
|
231
|
-
const fs = options
|
|
231
|
+
const fs = fsOf(options);
|
|
232
232
|
const updateMs = options.lockUpdateMs ?? loginRefresh.lockUpdateMs;
|
|
233
233
|
const giveUp = Date.now() + (options.lockWaitMs ?? LOCK_WAIT_MS);
|
|
234
234
|
for (;;) {
|
|
@@ -256,21 +256,28 @@ async function saveHeldUnderLock(dir, file, held, options) {
|
|
|
256
256
|
await sleep(LOCK_POLL_MS);
|
|
257
257
|
}
|
|
258
258
|
}
|
|
259
|
+
const readText = (path) => readFile(path, "utf8");
|
|
259
260
|
/** The login's credentials file as it is now, in the pinned CLI's shape: its parsed object and its `claudeAiOauth`,
|
|
260
|
-
* every token in it held for redaction;
|
|
261
|
-
|
|
261
|
+
* every token in it held for redaction; the one reader of the file, the runner's asks' (LoginTokens) and its
|
|
262
|
+
* renewals' (read with LockFs.readFile). Else a word, never a value of the file: "gone" when it is not there or holds
|
|
263
|
+
* no `claudeAiOauth` (signed out); "unread" when the read fails another way (an I/O error) or its text is not JSON,
|
|
264
|
+
* which says nothing of which pair it holds; "sealed" when it is JSON in another shape, a form this runner does not
|
|
265
|
+
* read. A renewal takes any word as no pair to renew from. */
|
|
266
|
+
async function credentialsRead(file, read = readText) {
|
|
262
267
|
let json;
|
|
263
268
|
try {
|
|
264
|
-
json = JSON.parse(await
|
|
269
|
+
json = JSON.parse(await read(file));
|
|
265
270
|
}
|
|
266
|
-
catch {
|
|
267
|
-
return
|
|
271
|
+
catch (error) {
|
|
272
|
+
return error?.code === "ENOENT" ? "gone" : "unread";
|
|
268
273
|
}
|
|
269
274
|
if (json === null || typeof json !== "object" || Array.isArray(json))
|
|
270
|
-
return
|
|
275
|
+
return "sealed";
|
|
271
276
|
const oauth = json.claudeAiOauth;
|
|
272
|
-
if (oauth ===
|
|
273
|
-
return
|
|
277
|
+
if (oauth === undefined || oauth === null)
|
|
278
|
+
return "gone";
|
|
279
|
+
if (typeof oauth !== "object" || Array.isArray(oauth))
|
|
280
|
+
return "sealed";
|
|
274
281
|
const fields = oauth;
|
|
275
282
|
holdSecret(...strings([fields.accessToken, fields.refreshToken]));
|
|
276
283
|
return { json: json, oauth: fields };
|
|
@@ -278,7 +285,7 @@ async function credentialsNow(file) {
|
|
|
278
285
|
/** The pinned CLI's renewal of a login (the adapter's login-refresh.ts), as the runner does it in the CLI's place. */
|
|
279
286
|
export function renewLogin(options = {}) {
|
|
280
287
|
const now = options.now ?? Date.now;
|
|
281
|
-
|
|
288
|
+
const renew = async (login, seen, refusedAccess) => {
|
|
282
289
|
const file = join(login.dir, LOGIN_FILE);
|
|
283
290
|
// A pair renewed here and not saved yet: its token is handed and its write tried again (under the refresh locks, read
|
|
284
291
|
// again there: saveHeldUnderLock), and nothing is posted, for the file's refresh token is spent. Once saved, a token
|
|
@@ -299,42 +306,71 @@ export function renewLogin(options = {}) {
|
|
|
299
306
|
}
|
|
300
307
|
seen = held.renewed.access;
|
|
301
308
|
}
|
|
302
|
-
const read = await
|
|
303
|
-
// Another holder renewed it since it was read, or it changed: the caller reads it again.
|
|
304
|
-
if (read
|
|
309
|
+
const read = await credentialsRead(file, fsOf(options).readFile);
|
|
310
|
+
// Another holder renewed it since it was read, or it changed (no pair read: the caller reads it again too).
|
|
311
|
+
if (typeof read === "string" || read.oauth.accessToken !== seen)
|
|
305
312
|
return undefined;
|
|
306
313
|
const refresh = read.oauth.refreshToken;
|
|
307
314
|
if (typeof refresh !== "string" || refresh === "")
|
|
308
315
|
throw new Error("the login holds no refresh token");
|
|
309
|
-
|
|
310
|
-
|
|
316
|
+
// A renewal in flight on this refresh token is joined first: one Anthropic answered with a new refresh token has
|
|
317
|
+
// marked this one spent (postRefresh) before its save writes the new pair, and an ask that read the file in between
|
|
318
|
+
// is handed that pair. The spent check stops only a new request.
|
|
311
319
|
let flight = flights.get(refresh);
|
|
312
320
|
if (!flight) {
|
|
321
|
+
if (dead.has(refresh)) {
|
|
322
|
+
// The renewal that spent it may have held its pair unsaved and settled while this read was out, after the check
|
|
323
|
+
// of heldPairs above: that pair is this ask's, through the held branch, never a refusal.
|
|
324
|
+
if (heldPairs.get(file)?.posted === refresh)
|
|
325
|
+
return renew(login, seen, refusedAccess);
|
|
326
|
+
throw new Error("the login's refresh token is spent or was refused for good");
|
|
327
|
+
}
|
|
313
328
|
flight = renewUnderLock(login.dir, file, seen, refresh, options).finally(() => flights.delete(refresh));
|
|
314
329
|
flights.set(refresh, flight);
|
|
315
330
|
}
|
|
316
331
|
return flight;
|
|
317
332
|
};
|
|
333
|
+
return renew;
|
|
318
334
|
}
|
|
319
|
-
const LOCK_FS = { mkdir: (path) => mkdir(path), stat: (path) => stat(path), utimes: (path, atime, mtime) => utimes(path, atime, mtime), rm: (path, options) => rm(path, options) };
|
|
335
|
+
const LOCK_FS = { mkdir: (path) => mkdir(path), stat: (path) => stat(path), utimes: (path, atime, mtime) => utimes(path, atime, mtime), rm: (path, options) => rm(path, options), readFile: (path) => readText(path) };
|
|
336
|
+
/** A renewal's file operations: those `options` gives, fs/promises' own for the rest. */
|
|
337
|
+
const fsOf = (options) => ({ ...LOCK_FS, ...options.lockFs });
|
|
320
338
|
/** Whether two reads are of one directory with one mtime, exactly: what a takeover removes is only the lock it read. */
|
|
321
339
|
const sameLock = (read, kept) => read.dev === kept.dev && read.ino === kept.ino && read.mtimeMs === kept.mtimeMs;
|
|
322
340
|
/** Whether `read` is this holder's own lock: the directory it made (`kept`, once known) with the mtime it set (`set`)
|
|
323
341
|
* as the filesystem keeps it: within 10 µs (libuv keeps whole microseconds, and the round trip through a double is
|
|
324
|
-
* under one more), or
|
|
325
|
-
* second is the CLI's lock library's own rule there
|
|
342
|
+
* under one more), or the start of the second `set` falls in, as a filesystem that keeps whole seconds alone shows
|
|
343
|
+
* it. That second is the CLI's lock library's own rule there, and it is taken on every filesystem: another holder's
|
|
344
|
+
* mtime of exactly that whole second reads as this holder's too. Between runners the takeover mark keeps a second
|
|
345
|
+
* holder out. */
|
|
326
346
|
const ownRead = (read, kept, set) => (kept === undefined || (read.dev === kept.dev && read.ino === kept.ino)) &&
|
|
327
|
-
(Math.abs(read.mtimeMs - set) < 0.01 ||
|
|
347
|
+
(Math.abs(read.mtimeMs - set) < 0.01 || read.mtimeMs === Math.floor(set / 1000) * 1000);
|
|
348
|
+
/** The log word for a lock not read as its holder's own. */
|
|
349
|
+
const lockWord = (state) => (state === "lost" ? "lock_lost" : "lock_unread");
|
|
350
|
+
/** `path` read for the removal of a lock or mark this holder made: a read that fails for a reason other than the path
|
|
351
|
+
* being gone is made once more, so one transient failure never leaves it standing for the stale time; none when it is
|
|
352
|
+
* gone or both reads fail. */
|
|
353
|
+
async function readForRemoval(path, fs) {
|
|
354
|
+
try {
|
|
355
|
+
return await fs.stat(path);
|
|
356
|
+
}
|
|
357
|
+
catch (error) {
|
|
358
|
+
if (error?.code === "ENOENT")
|
|
359
|
+
return undefined;
|
|
360
|
+
return fs.stat(path).catch(() => undefined);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
328
363
|
/** Beside each lock, the runners' takeover mark: a directory `<lock>.takeover`, never inside the lock, made, taken over
|
|
329
364
|
* when dead and released as a lock of its own (makeLock, ownRead). It is held for a few file operations, so a mark
|
|
330
365
|
* older than lockStaleMs is a crashed (or 60 s stalled) runner's. */
|
|
331
366
|
const TAKEOVER_MARK = ".takeover";
|
|
332
367
|
/** Lock `path` made, or a dead one (older than lockStaleMs) taken over, its mtime set and read back: this holder's own
|
|
333
368
|
* directory and mtime, or none while another holds it. A dead lock is removed only while a read right before the
|
|
334
|
-
* removal shows the same directory
|
|
335
|
-
* only while it can still be its own: never once a touch finds it gone (what
|
|
336
|
-
*
|
|
337
|
-
* taken with it, and a lock only under the
|
|
369
|
+
* removal shows the same directory with the same mtime, so none touched or made again since. One this holder made and
|
|
370
|
+
* could not touch or read back is removed only while it can still be its own: never once a touch finds it gone (what
|
|
371
|
+
* is at the path then may be another holder's, which the ask waits for as for any other), and after a failed read only
|
|
372
|
+
* while a second read shows the mtime this holder set. The takeover mark is taken with it, and a lock only under the
|
|
373
|
+
* mark. */
|
|
338
374
|
async function makeLock(path, fs) {
|
|
339
375
|
for (let attempt = 0; attempt < 2; attempt++) {
|
|
340
376
|
try {
|
|
@@ -349,8 +385,9 @@ async function makeLock(path, fs) {
|
|
|
349
385
|
continue;
|
|
350
386
|
if (Date.now() - seen.mtimeMs <= loginRefresh.lockStaleMs)
|
|
351
387
|
return undefined;
|
|
388
|
+
// The same directory with the same mtime is still the stale one read above.
|
|
352
389
|
const again = await fs.stat(path).catch(() => undefined);
|
|
353
|
-
if (!again || !sameLock(again, seen)
|
|
390
|
+
if (!again || !sameLock(again, seen))
|
|
354
391
|
continue;
|
|
355
392
|
await fs.rm(path, { recursive: true, force: true });
|
|
356
393
|
continue;
|
|
@@ -360,8 +397,9 @@ async function makeLock(path, fs) {
|
|
|
360
397
|
await fs.utimes(path, new Date(set), new Date(set));
|
|
361
398
|
}
|
|
362
399
|
catch (error) {
|
|
363
|
-
if (error?.code
|
|
364
|
-
|
|
400
|
+
if (error?.code === "ENOENT")
|
|
401
|
+
return undefined;
|
|
402
|
+
await fs.rm(path, { recursive: true, force: true }).catch(() => undefined);
|
|
365
403
|
throw error;
|
|
366
404
|
}
|
|
367
405
|
let made;
|
|
@@ -385,13 +423,15 @@ async function makeLock(path, fs) {
|
|
|
385
423
|
* mtime touched every `updateMs` while it is held, taken over once older than lockStaleMs. Nothing is ever put inside
|
|
386
424
|
* it: proper-lockfile removes a lock with rmdir. It is this holder's while a read shows the directory it made with the
|
|
387
425
|
* mtime it last set (ownRead); any other read (another directory, another mtime, the lock gone) marks it lost for
|
|
388
|
-
* good, the CLI's "compromised", and it is never touched or removed again.
|
|
426
|
+
* good, the CLI's "compromised", and it is never touched or removed again. A read that fails while it is there is
|
|
427
|
+
* neither: the next read decides, and its release reads it once more. None while another holds it.
|
|
389
428
|
* Runners make, take over and read back a lock only under the takeover mark beside it, one runner at a time, so
|
|
390
429
|
* between runners one holder at a time holds it. Left open, as in every lease lock: a runner stalled past the stale
|
|
391
430
|
* time inside a takeover (its mark taken over as dead, its removal then lands in another's lock), or between its
|
|
392
|
-
* release's check and its removal
|
|
393
|
-
*
|
|
394
|
-
*
|
|
431
|
+
* release's check and its removal; and a runner that crashed holding the mark, whose dead mark two runners then take
|
|
432
|
+
* over at once (one's late removal lands in the mark the other made). The pinned CLI never sees the mark: against the
|
|
433
|
+
* CLI the lock keeps proper-lockfile's rules, a takeover's late removal included (the second lock, beside the folder,
|
|
434
|
+
* keeps the two from both posting where it can be made). */
|
|
395
435
|
async function lockDirectory(path, fs, updateMs) {
|
|
396
436
|
const markPath = `${path}${TAKEOVER_MARK}`;
|
|
397
437
|
const mark = await makeLock(markPath, fs);
|
|
@@ -404,8 +444,8 @@ async function lockDirectory(path, fs, updateMs) {
|
|
|
404
444
|
}
|
|
405
445
|
finally {
|
|
406
446
|
// Only the mark made here: one another runner made after taking this one over as dead (this runner stalled past the
|
|
407
|
-
// stale time) is that runner's.
|
|
408
|
-
const still = await
|
|
447
|
+
// stale time) is that runner's. A mark left standing holds every runner on the login for the stale time.
|
|
448
|
+
const still = await readForRemoval(markPath, fs);
|
|
409
449
|
if (still && ownRead(still, mark, mark.set))
|
|
410
450
|
await fs.rm(markPath, { recursive: true, force: true }).catch(() => undefined);
|
|
411
451
|
}
|
|
@@ -427,21 +467,21 @@ async function lockDirectory(path, fs, updateMs) {
|
|
|
427
467
|
let beating = false;
|
|
428
468
|
const check = async () => {
|
|
429
469
|
if (lost)
|
|
430
|
-
return
|
|
470
|
+
return "lost";
|
|
431
471
|
try {
|
|
432
472
|
if (ownRead(await fs.stat(path), mine, mine.set))
|
|
433
|
-
return
|
|
473
|
+
return "own";
|
|
434
474
|
}
|
|
435
475
|
catch (error) {
|
|
436
476
|
// Unread but not gone (a transient failure): not touched or removed at this read, and read again at the next, as
|
|
437
477
|
// proper-lockfile does.
|
|
438
478
|
if (error?.code !== "ENOENT")
|
|
439
|
-
return
|
|
479
|
+
return "unread";
|
|
440
480
|
}
|
|
441
481
|
// Gone, or another's: lost.
|
|
442
482
|
lost = true;
|
|
443
483
|
clearInterval(timer);
|
|
444
|
-
return
|
|
484
|
+
return "lost";
|
|
445
485
|
};
|
|
446
486
|
const owned = () => inTurn(check);
|
|
447
487
|
timer = setInterval(() => {
|
|
@@ -449,7 +489,7 @@ async function lockDirectory(path, fs, updateMs) {
|
|
|
449
489
|
return;
|
|
450
490
|
beating = true;
|
|
451
491
|
void inTurn(async () => {
|
|
452
|
-
if (
|
|
492
|
+
if ((await check()) !== "own")
|
|
453
493
|
return;
|
|
454
494
|
const at = Date.now();
|
|
455
495
|
try {
|
|
@@ -475,7 +515,12 @@ async function lockDirectory(path, fs, updateMs) {
|
|
|
475
515
|
release: async () => {
|
|
476
516
|
clearInterval(timer);
|
|
477
517
|
await inTurn(async () => {
|
|
478
|
-
|
|
518
|
+
// A read that fails while the lock is there is made once more (readForRemoval's rule): one transient failure
|
|
519
|
+
// never leaves the lock standing for the stale time.
|
|
520
|
+
let state = await check();
|
|
521
|
+
if (state === "unread")
|
|
522
|
+
state = await check();
|
|
523
|
+
if (state === "own")
|
|
479
524
|
await fs.rm(path, { recursive: true, force: true });
|
|
480
525
|
lost = true;
|
|
481
526
|
});
|
|
@@ -504,7 +549,10 @@ async function takeRefreshLocks(dir, fs, updateMs) {
|
|
|
504
549
|
const older = legacy;
|
|
505
550
|
return {
|
|
506
551
|
// Both asked each time: each one's own lost mark is set by its own read.
|
|
507
|
-
owned: async () =>
|
|
552
|
+
owned: async () => {
|
|
553
|
+
const first = await current.owned();
|
|
554
|
+
return first !== "own" || older === null ? first : older.owned();
|
|
555
|
+
},
|
|
508
556
|
release: async () => {
|
|
509
557
|
await older?.release().catch(() => undefined);
|
|
510
558
|
await current.release();
|
|
@@ -520,7 +568,7 @@ async function takeRefreshLocks(dir, fs, updateMs) {
|
|
|
520
568
|
* (`tengu_oauth_refresh_compromised_cas_saved`), and held in memory if that write fails (saveHeld). */
|
|
521
569
|
async function renewUnderLock(dir, file, seen, refresh, options) {
|
|
522
570
|
const say = options.log ?? (() => undefined);
|
|
523
|
-
const fs = options
|
|
571
|
+
const fs = fsOf(options);
|
|
524
572
|
const updateMs = options.lockUpdateMs ?? loginRefresh.lockUpdateMs;
|
|
525
573
|
const giveUp = Date.now() + (options.lockWaitMs ?? LOCK_WAIT_MS);
|
|
526
574
|
for (;;) {
|
|
@@ -528,23 +576,30 @@ async function renewUnderLock(dir, file, seen, refresh, options) {
|
|
|
528
576
|
if (locks) {
|
|
529
577
|
try {
|
|
530
578
|
// Read again under the lock: a holder that renewed before it was taken has written its pair, which is taken.
|
|
531
|
-
const held = await
|
|
532
|
-
if (
|
|
579
|
+
const held = await credentialsRead(file, fs.readFile);
|
|
580
|
+
if (typeof held === "string" || held.oauth.accessToken !== seen || held.oauth.refreshToken !== refresh)
|
|
533
581
|
return undefined;
|
|
534
|
-
|
|
582
|
+
// A lock read as another's is said as lost; one whose read failed, as unread: no takeover is said that did not
|
|
583
|
+
// happen. Either way nothing is posted under it, and the next try reads it again.
|
|
584
|
+
const before = await locks.owned();
|
|
585
|
+
if (before === "own") {
|
|
535
586
|
const answer = await postRefresh(held.oauth, refresh, options);
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
587
|
+
const after = await locks.owned();
|
|
588
|
+
// Written back (or held) before the line is said: a log that throws never loses the pair the post rotated.
|
|
589
|
+
const renewed = await writeBack(file, refresh, answer, options);
|
|
590
|
+
if (after !== "own")
|
|
591
|
+
say({ claude_login: lockWord(after), at: "after_post" });
|
|
592
|
+
return renewed;
|
|
539
593
|
}
|
|
540
|
-
say({ claude_login:
|
|
594
|
+
say({ claude_login: lockWord(before), at: "before_post" });
|
|
541
595
|
}
|
|
542
596
|
finally {
|
|
543
597
|
await locks.release().catch(() => undefined);
|
|
544
598
|
}
|
|
545
599
|
}
|
|
546
600
|
// Another holder renews it now: its write is taken when it lands, never a second post of the same refresh token.
|
|
547
|
-
|
|
601
|
+
const now = await credentialsRead(file, fs.readFile);
|
|
602
|
+
if (typeof now === "string" || now.oauth.accessToken !== seen)
|
|
548
603
|
return undefined;
|
|
549
604
|
if (Date.now() > giveUp) {
|
|
550
605
|
say({ claude_login: "renew_failed", reason: "locked" });
|
|
@@ -665,24 +720,29 @@ async function replaceFile(file, text) {
|
|
|
665
720
|
await folder.close();
|
|
666
721
|
}
|
|
667
722
|
}
|
|
668
|
-
/** Whether `login` is AgentRun's own login folder in `data` (claude-login.ts
|
|
723
|
+
/** Whether `login` is AgentRun's own login folder in `data` (claude-login.ts agentRunLoginDir), the one login a run
|
|
669
724
|
* under a user of its own is handed a token of; this machine's own Claude Code login is not. */
|
|
670
|
-
export const agentRunLogin = (login, data) => login.dir ===
|
|
725
|
+
export const agentRunLogin = (login, data) => login.dir === agentRunLoginDir(data);
|
|
671
726
|
/** One reader per login folder in this process, so every claim on it holds the same watchers' tokens and the same
|
|
672
|
-
* refused token; each renews with the pinned CLI's own request (renewLogin)
|
|
727
|
+
* refused token; each renews with the pinned CLI's own request (renewLogin), and says each renewal's outcome to `log`
|
|
728
|
+
* in fields that hold no token. The reader keeps the `log` of the claim that first asks for its folder, for as long as
|
|
729
|
+
* the process runs: a later claim's `log` is not used. Every claim in a runner hands the same one (the runner's own log),
|
|
730
|
+
* so a renewal is said once, on that log, whichever claim's ask set it off. */
|
|
673
731
|
const readers = new Map();
|
|
674
|
-
export function loginTokensFor(login) {
|
|
732
|
+
export function loginTokensFor(login, log) {
|
|
675
733
|
let reader = readers.get(login.dir);
|
|
676
734
|
if (!reader)
|
|
677
|
-
readers.set(login.dir, (reader = new LoginTokens({ login })));
|
|
735
|
+
readers.set(login.dir, (reader = new LoginTokens({ login, log })));
|
|
678
736
|
return reader;
|
|
679
737
|
}
|
|
680
|
-
/** The words a run's
|
|
681
|
-
*
|
|
682
|
-
|
|
738
|
+
/** The words a run's start is refused in by its runner (held.ts claudeLogin) when its claim read Claude Code's login and
|
|
739
|
+
* the runner's second read of the setting, at the start, names none or another folder: the run is put on no login its
|
|
740
|
+
* start did not read, and never falls to a model key it was not claimed on. They name no folder. */
|
|
741
|
+
export const LOGIN_CHANGED_SINCE_CLAIM = "This run's runner handed it no Claude Code login: the login was turned off or moved to another folder since this run's claim, and a run claimed on the login does not fall back to the model keys. Start the run again to run it as the login is now, or give it a model_key.";
|
|
683
742
|
/** A run's own process's login (held.ts claudeLogin's reader): each token it reads held by `hold` (the run's redactors)
|
|
684
743
|
* and this process's own (secrets.ts holdSecret) before the run's CLI is handed it, and a token Anthropic refused told
|
|
685
|
-
* to the runner, which renews it at the next ask.
|
|
744
|
+
* to the runner, which renews it at the next ask. A process its runner hands no reader runs on no login (cli-run-spec.ts):
|
|
745
|
+
* its claim read the login as off for it, or its run brought its own model key. */
|
|
686
746
|
export function heldTokenLogin(reader, hold) {
|
|
687
747
|
return {
|
|
688
748
|
token: async () => {
|
|
@@ -733,5 +793,5 @@ export async function loginRunnerInProcess(data, runners) {
|
|
|
733
793
|
}));
|
|
734
794
|
if (said.size === 0)
|
|
735
795
|
return undefined;
|
|
736
|
-
return (await runners.live(
|
|
796
|
+
return (await runners.live(RUNNER_STALE_MS)).find((row) => said.has(row.runner))?.runner;
|
|
737
797
|
}
|