@parcha/agentrun-serve 0.1.0-beta.43 → 0.1.0-beta.45
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 +231 -0
- package/README.md +580 -320
- package/dist/acp-package.d.ts +28 -0
- package/dist/acp-package.js +23 -0
- package/dist/adapter-facts.d.ts +8 -1
- package/dist/adapter-facts.js +9 -1
- package/dist/adapters.d.ts +5 -0
- package/dist/adapters.js +36 -3
- package/dist/agent-delete.d.ts +50 -0
- package/dist/agent-delete.js +125 -0
- package/dist/agents-store.d.ts +9 -3
- package/dist/agents-store.js +22 -8
- package/dist/agents.d.ts +5 -1
- package/dist/agents.js +8 -4
- package/dist/api-only.d.ts +8 -3
- package/dist/api-only.js +10 -4
- package/dist/archil-routes.d.ts +5 -1
- package/dist/archil-routes.js +57 -2
- package/dist/browser-wait-ended.d.ts +2 -0
- package/dist/browser-wait-ended.js +2 -0
- package/dist/calendar/ics-faux.d.ts +3 -1
- package/dist/calendar/ics-faux.js +12 -2
- package/dist/calendar/tools.js +5 -2
- package/dist/chatgpt-plan.js +3 -1
- package/dist/claude-login.d.ts +135 -0
- package/dist/claude-login.js +244 -0
- package/dist/cli.js +92 -22
- package/dist/composio-preset.d.ts +4 -3
- package/dist/composio-preset.js +81 -11
- package/dist/composio-triggers.d.ts +34 -2
- package/dist/composio-triggers.js +261 -31
- package/dist/computer-attach.d.ts +40 -5
- package/dist/computer-attach.js +130 -15
- package/dist/computer-keys.d.ts +3 -0
- package/dist/computer-keys.js +7 -1
- package/dist/computer-live.js +3 -2
- package/dist/computer-lost.d.ts +11 -0
- package/dist/computer-lost.js +19 -0
- package/dist/computer-record.d.ts +10 -2
- package/dist/computer-record.js +50 -18
- package/dist/computer-setting.d.ts +1 -0
- package/dist/computer-setting.js +2 -2
- package/dist/computers.d.ts +21 -10
- package/dist/computers.js +39 -28
- package/dist/console/assets/index-3O2Yl6hZ.js +701 -0
- package/dist/console/assets/index-DaFsZuzQ.css +1 -0
- package/dist/console/index.html +2 -2
- package/dist/decisions.d.ts +1 -1
- package/dist/dial-relay.d.ts +29 -0
- package/dist/dial-relay.js +94 -0
- package/dist/email/tools.js +10 -5
- package/dist/env-proxy.d.ts +2 -0
- package/dist/env-proxy.js +35 -0
- package/dist/errors.d.ts +4 -1
- package/dist/errors.js +10 -4
- package/dist/faux-computers.d.ts +8 -2
- package/dist/faux-computers.js +83 -6
- package/dist/harness-archive.d.ts +67 -0
- package/dist/harness-archive.js +177 -0
- package/dist/harness-goose.d.ts +34 -0
- package/dist/harness-goose.js +87 -0
- package/dist/harness-install.d.ts +21 -0
- package/dist/harness-install.js +24 -11
- package/dist/harness-opencode/adapter.js +1 -1
- package/dist/harness-table.d.ts +59 -3
- package/dist/harness-table.js +119 -14
- package/dist/harness.d.ts +17 -1
- package/dist/harness.js +22 -11
- package/dist/host.d.ts +12 -10
- package/dist/host.js +19 -16
- package/dist/import-worker.d.ts +3 -0
- package/dist/import-worker.js +7 -5
- package/dist/import.d.ts +22 -1
- package/dist/import.js +141 -28
- package/dist/key-check.js +3 -0
- package/dist/learn/audit.d.ts +6 -6
- package/dist/learn/audit.js +3 -7
- package/dist/learn/check.d.ts +26 -89
- package/dist/learn/check.js +58 -336
- package/dist/learn/commit.d.ts +63 -90
- package/dist/learn/commit.js +124 -316
- package/dist/learn/compile.d.ts +53 -50
- package/dist/learn/compile.js +88 -103
- package/dist/learn/corrections.d.ts +13 -0
- package/dist/learn/corrections.js +23 -0
- package/dist/learn/delete.d.ts +17 -0
- package/dist/learn/delete.js +42 -0
- package/dist/learn/folder.d.ts +35 -47
- package/dist/learn/folder.js +118 -134
- package/dist/learn/index.d.ts +3 -5
- package/dist/learn/index.js +6 -9
- package/dist/learn/learning.sh +45 -15
- package/dist/learn/promote.d.ts +20 -2
- package/dist/learn/promote.js +17 -4
- package/dist/learn/{skills.d.ts → workflow-draft.d.ts} +34 -38
- package/dist/learn/{skills.js → workflow-draft.js} +65 -59
- package/dist/learn/workflow-nodes.d.ts +22 -1
- package/dist/learn/workflow-nodes.js +29 -10
- package/dist/learned-workflow.js +2 -2
- package/dist/learning-cost.d.ts +127 -13
- package/dist/learning-cost.js +142 -10
- package/dist/learning-routes.d.ts +99 -170
- package/dist/learning-routes.js +199 -420
- package/dist/learning-run.d.ts +19 -40
- package/dist/learning-run.js +23 -93
- package/dist/learning-setting.d.ts +4 -4
- package/dist/learning-setting.js +6 -6
- package/dist/learning-words.d.ts +28 -74
- package/dist/learning-words.js +64 -162
- package/dist/learning.d.ts +62 -393
- package/dist/learning.js +101 -651
- package/dist/listen-first.d.ts +30 -0
- package/dist/listen-first.js +115 -0
- package/dist/live-reach.d.ts +25 -2
- package/dist/live-reach.js +46 -4
- package/dist/llms.txt +2 -1
- package/dist/local-models.d.ts +3 -11
- package/dist/local-models.js +5 -6
- package/dist/mcp-servers-store.d.ts +16 -16
- package/dist/model-form.js +6 -5
- package/dist/model.d.ts +12 -4
- package/dist/model.js +41 -16
- package/dist/native/binding.js +1 -1
- package/dist/native/connection.js +1 -1
- package/dist/native/discovery.js +1 -1
- package/dist/native/instructions.d.ts +3 -0
- package/dist/native/instructions.js +5 -3
- package/dist/native/remote-worker-bridge.js +9 -43
- package/dist/native/remote.d.ts +8 -15
- package/dist/native/remote.js +3 -3
- package/dist/native/schema.js +1 -1
- package/dist/native/types.d.ts +2 -2
- package/dist/openapi.js +375 -347
- package/dist/owner.d.ts +8 -0
- package/dist/owner.js +11 -0
- package/dist/paid-calls.d.ts +14 -0
- package/dist/paid-calls.js +31 -1
- package/dist/particle-provider.d.ts +11 -0
- package/dist/particle-provider.js +135 -0
- package/dist/place.d.ts +8 -3
- package/dist/place.js +13 -9
- package/dist/port-models.d.ts +12 -4
- package/dist/port-models.js +28 -11
- package/dist/print.d.ts +27 -0
- package/dist/print.js +21 -0
- package/dist/provider.d.ts +7 -0
- package/dist/provider.js +30 -2
- package/dist/reflect-range.js +2 -3
- package/dist/run-launch.d.ts +14 -28
- package/dist/run-launch.js +9 -12
- package/dist/run-learning-served.d.ts +16 -90
- package/dist/run-listing.d.ts +34 -0
- package/dist/run-listing.js +128 -0
- package/dist/run-model-key.d.ts +64 -0
- package/dist/run-model-key.js +147 -0
- package/dist/run-timeline-served.js +5 -3
- package/dist/run-trace.d.ts +97 -0
- package/dist/run-trace.js +253 -0
- package/dist/run-view.d.ts +15 -0
- package/dist/run-view.js +9 -0
- package/dist/runnable.js +2 -1
- package/dist/runner/adapter-loop.d.ts +14 -6
- package/dist/runner/adapter-loop.js +25 -14
- package/dist/runner/adapter-run-spec.d.ts +22 -7
- package/dist/runner/adapter-run-spec.js +130 -77
- package/dist/runner/adapters/index.d.ts +4 -2
- package/dist/runner/adapters/index.js +35 -4
- package/dist/runner/adapters/pi.d.ts +4 -12
- package/dist/runner/adapters/pi.js +2 -2
- package/dist/runner/audit.d.ts +5 -1
- package/dist/runner/audit.js +31 -12
- package/dist/runner/beside.d.ts +7 -2
- package/dist/runner/beside.js +41 -9
- package/dist/runner/claim-secrets.d.ts +66 -1
- package/dist/runner/claim-secrets.js +136 -3
- package/dist/runner/cli-run-spec.d.ts +2 -1
- package/dist/runner/cli-run-spec.js +82 -59
- package/dist/runner/commands.d.ts +3 -6
- package/dist/runner/conformance/harness.d.ts +6 -0
- package/dist/runner/conformance/harness.js +16 -5
- package/dist/runner/dispatch.d.ts +2 -2
- package/dist/runner/end-of-run.d.ts +6 -14
- package/dist/runner/end-of-run.js +8 -33
- package/dist/runner/feedback-round.d.ts +5 -4
- package/dist/runner/feedback-round.js +14 -10
- package/dist/runner/github-app.js +3 -1
- package/dist/runner/harness-loops.d.ts +0 -8
- package/dist/runner/harness-loops.js +30 -56
- package/dist/runner/held.d.ts +170 -0
- package/dist/runner/held.js +57 -0
- package/dist/runner/journal-kinds.d.ts +7 -3
- package/dist/runner/journal-kinds.js +2 -3
- package/dist/runner/learned-first.d.ts +40 -0
- package/dist/runner/learned-first.js +32 -0
- package/dist/runner/learning-end.d.ts +77 -66
- package/dist/runner/learning-end.js +250 -299
- package/dist/runner/learning-seed.d.ts +15 -33
- package/dist/runner/learning-seed.js +24 -45
- package/dist/runner/loop-contract.d.ts +8 -3
- package/dist/runner/loop-contract.js +4 -0
- package/dist/runner/loop.js +6 -2
- package/dist/runner/main.d.ts +18 -9
- package/dist/runner/main.js +82 -22
- package/dist/runner/mcp-run.d.ts +11 -0
- package/dist/runner/mcp-run.js +67 -0
- package/dist/runner/memory-claim.d.ts +14 -6
- package/dist/runner/memory-claim.js +18 -9
- package/dist/runner/model-route.d.ts +7 -4
- package/dist/runner/model-route.js +22 -10
- package/dist/runner/native-loop.d.ts +0 -7
- package/dist/runner/native-loop.js +9 -101
- package/dist/runner/promotion.d.ts +13 -6
- package/dist/runner/promotion.js +63 -17
- package/dist/runner/reflect-hook.d.ts +64 -91
- package/dist/runner/reflect-hook.js +112 -206
- package/dist/runner/run-channel.d.ts +43 -0
- package/dist/runner/run-channel.js +467 -0
- package/dist/runner/run-child-host.d.ts +169 -0
- package/dist/runner/run-child-host.js +1222 -0
- package/dist/runner/run-child.d.ts +1 -0
- package/dist/runner/run-child.js +254 -0
- package/dist/runner/run-errors.d.ts +2 -0
- package/dist/runner/run-errors.js +20 -0
- package/dist/runner/runner.js +58 -9
- package/dist/runner/sdk-ask.d.ts +78 -0
- package/dist/runner/sdk-ask.js +158 -0
- package/dist/runner/sdk-loop.d.ts +2 -4
- package/dist/runner/sdk-loop.js +29 -49
- package/dist/runner/store-secrets.d.ts +5 -3
- package/dist/runner/store-secrets.js +14 -6
- package/dist/runner/workflow-first.d.ts +5 -0
- package/dist/runner/workflow-first.js +4 -0
- package/dist/runner/workflow-spec.js +17 -4
- package/dist/runs-over-store.d.ts +21 -32
- package/dist/runs-over-store.js +44 -41
- package/dist/sdk/runs.d.ts +6 -11
- package/dist/sdk/runs.js +20 -41
- package/dist/sdk/schema.js +3 -4
- package/dist/sdk/workflows.d.ts +1 -1
- package/dist/seal-key.d.ts +5 -0
- package/dist/seal-key.js +7 -0
- package/dist/secrets.d.ts +3 -0
- package/dist/secrets.js +16 -3
- package/dist/server.d.ts +119 -83
- package/dist/server.js +1121 -419
- package/dist/sign-ins-store.d.ts +8 -0
- package/dist/sign-ins-store.js +36 -1
- package/dist/skills.js +1 -1
- package/dist/store/index.d.ts +3 -3
- package/dist/store/index.js +2 -2
- package/dist/store/journal/storage.js +3 -0
- package/dist/store/postgres/migrations/0017_run_listing.sql +83 -0
- package/dist/store/postgres/migrations/0018_trigger_template.sql +6 -0
- package/dist/store/postgres/migrations/0019_runner_relays.sql +28 -0
- package/dist/store/schema.d.ts +53 -4
- package/dist/store/schema.js +137 -6
- package/dist/store/seal.d.ts +20 -2
- package/dist/store/seal.js +24 -8
- package/dist/store/sqlite/connection.d.ts +2 -0
- package/dist/store/sqlite/connection.js +3 -1
- package/dist/store/sqlite/run-launch.d.ts +4 -1
- package/dist/store/sqlite/run-launch.js +12 -0
- package/dist/store/sqlite/run-patch.d.ts +0 -17
- package/dist/store/sqlite/run-patch.js +4 -33
- package/dist/store/sqlite/store.d.ts +3 -2
- package/dist/store/sqlite/store.js +360 -75
- package/dist/store/sqlite/worker.js +25 -0
- package/dist/store/store.d.ts +231 -39
- package/dist/store/store.js +25 -3
- package/dist/store-reads/analytics.js +11 -1
- package/dist/store-reads/learning.d.ts +131 -194
- package/dist/store-reads/learning.js +292 -301
- package/dist/store-reads/loop.d.ts +0 -1
- package/dist/store-reads/loop.js +1 -1
- package/dist/store-reads/stats.js +13 -3
- package/dist/store-view.d.ts +34 -2
- package/dist/store-view.js +108 -41
- package/dist/telemetry.d.ts +59 -0
- package/dist/telemetry.js +153 -0
- package/dist/template-eval.d.ts +3 -6
- package/dist/template-eval.js +22 -36
- package/dist/templates.d.ts +2 -1
- package/dist/templates.js +6 -3
- package/dist/timeline/index.d.ts +1 -1
- package/dist/timeline/index.js +1 -1
- package/dist/timeline/timeline.d.ts +5 -1
- package/dist/timeline/timeline.js +16 -0
- package/dist/tool-gate.d.ts +0 -7
- package/dist/tool-gate.js +1 -15
- package/dist/tool-keys-store.d.ts +1 -1
- package/dist/tool-keys.d.ts +6 -4
- package/dist/tool-keys.js +12 -6
- package/dist/tool-plane.d.ts +12 -12
- package/dist/tools.d.ts +13 -13
- package/dist/tools.js +29 -9
- package/dist/trigger-adapter.d.ts +3 -0
- package/dist/trigger-adapters.d.ts +4 -0
- package/dist/trigger-adapters.js +13 -0
- package/dist/trigger-composio.d.ts +58 -1
- package/dist/trigger-composio.js +160 -6
- package/dist/triggers.d.ts +64 -5
- package/dist/triggers.js +141 -17
- package/dist/validate.d.ts +11 -7
- package/dist/validate.js +9 -8
- package/dist/webhooks.d.ts +4 -1
- package/dist/webhooks.js +11 -4
- package/dist/workspace/agent-computer.d.ts +2 -0
- package/dist/workspace/agent-computer.js +2 -0
- package/dist/workspace/archil-claim.d.ts +7 -0
- package/dist/workspace/archil-claim.js +29 -0
- package/dist/workspace/archil.d.ts +9 -1
- package/dist/workspace/archil.js +165 -3
- package/dist/workspace/attach.d.ts +3 -1
- package/dist/workspace/attach.js +2 -0
- package/dist/workspace/attached-computers.d.ts +16 -5
- package/dist/workspace/attached-computers.js +25 -9
- package/dist/workspace/computer-deps.js +1 -1
- package/dist/workspace/computer-for-claim.d.ts +22 -8
- package/dist/workspace/computer-for-claim.js +339 -46
- package/dist/workspace/computer-hold.js +2 -1
- package/dist/workspace/computer-kept.d.ts +11 -1
- package/dist/workspace/computer-kept.js +24 -3
- package/dist/workspace/resolve-computer.d.ts +41 -21
- package/dist/workspace/resolve-computer.js +172 -64
- package/dist/workspace/revision-skills.d.ts +1 -2
- package/dist/workspace/revision-skills.js +3 -4
- package/dist/workspace/run-workspace.d.ts +1 -1
- package/dist/workspace/run-workspace.js +5 -0
- package/dist/workspace/synced-folder.d.ts +85 -0
- package/dist/workspace/synced-folder.js +230 -0
- package/dist/workspace/workspace.d.ts +4 -0
- package/launcher/agentrun-serve +1 -1
- package/package.json +10 -9
- package/templates/README.md +7 -7
- package/templates/inbound-lead/agent.md +3 -3
- package/templates/inbound-lead/evals/learned-correction/case.yaml +3 -3
- package/templates/inbound-lead/evals/learned-correction/fixtures/corrections.md +1 -0
- package/templates/inbound-lead/evals/learned-correction/graders/applies-correction.md +22 -0
- package/templates/inbound-lead/evals/learned-correction/graders/learned-origin.md +1 -1
- package/templates/inbound-lead/evals/learned-correction/prompt.md +1 -1
- package/templates/inbound-lead/lead.schema.json +6 -5
- package/templates/inbound-lead/rules/RUBRIC.md +4 -4
- package/templates/inbound-lead/skills/lead/SKILL.md +4 -4
- package/templates/market-monitor/evals/learned-preference/case.yaml +4 -4
- package/templates/market-monitor/evals/learned-preference/fixtures/corrections.md +1 -0
- package/templates/market-monitor/evals/learned-preference/graders/omits-junior-post.md +3 -3
- package/templates/market-monitor/evals/learned-preference/prompt.md +1 -1
- package/templates/release-notes/evals/learned-style/case.yaml +4 -4
- package/templates/release-notes/evals/learned-style/fixtures/corrections.md +1 -0
- package/templates/release-notes/evals/learned-style/graders/same-changes.md +2 -2
- package/templates/release-notes/evals/learned-style/prompt.md +1 -1
- package/dist/console/assets/index-BrcDSBD8.css +0 -1
- package/dist/console/assets/index-Bz6IWncl.js +0 -711
- package/dist/learn/grade.d.ts +0 -138
- package/dist/learn/grade.js +0 -682
- package/dist/learn/notes.d.ts +0 -81
- package/dist/learn/notes.js +0 -178
- package/dist/learn/reflect.d.ts +0 -77
- package/dist/learn/reflect.js +0 -228
- package/dist/learn/relevance.d.ts +0 -24
- package/dist/learn/relevance.js +0 -91
- package/dist/learn/seed-check.d.ts +0 -74
- package/dist/learn/seed-check.js +0 -103
- package/dist/learning-batch.d.ts +0 -291
- package/dist/learning-batch.js +0 -600
- package/dist/learning-evals.d.ts +0 -33
- package/dist/learning-evals.js +0 -98
- package/dist/native/context.d.ts +0 -6
- package/dist/native/context.js +0 -22
- package/dist/propose.d.ts +0 -52
- package/dist/propose.js +0 -224
- package/dist/runner/learning-ask.d.ts +0 -47
- package/dist/runner/learning-ask.js +0 -128
- package/dist/runner/learning-dispatch.d.ts +0 -45
- package/dist/runner/learning-dispatch.js +0 -99
- package/dist/runner/learning-factory.d.ts +0 -16
- package/dist/runner/learning-factory.js +0 -50
- package/dist/runner/learning-loop.d.ts +0 -273
- package/dist/runner/learning-loop.js +0 -521
- package/dist/usage.d.ts +0 -11
- package/dist/usage.js +0 -11
- package/templates/inbound-lead/evals/learned-correction/fixtures/learning/MEMORY.md +0 -1
- package/templates/inbound-lead/evals/learned-correction/fixtures/learning/no-agencies.md +0 -14
- package/templates/inbound-lead/evals/learned-correction/graders/applies-note.md +0 -21
- package/templates/market-monitor/evals/learned-preference/fixtures/learning/MEMORY.md +0 -1
- package/templates/market-monitor/evals/learned-preference/fixtures/learning/skip-junior-job-posts.md +0 -8
- package/templates/release-notes/evals/learned-style/fixtures/learning/MEMORY.md +0 -1
- package/templates/release-notes/evals/learned-style/fixtures/learning/say-agent-not-bot.md +0 -13
package/dist/archil-routes.js
CHANGED
|
@@ -28,7 +28,11 @@ import { readLaunchBody } from "./run-launch.js";
|
|
|
28
28
|
import { putSealedKey, removeSealedKey } from "./server-keys.js";
|
|
29
29
|
import { unknownFields } from "./validate.js";
|
|
30
30
|
import { ArchilClaimRefused } from "./workspace/archil-claim.js";
|
|
31
|
-
import {
|
|
31
|
+
import { archilDisk } from "./workspace/archil-disk.js";
|
|
32
|
+
import { deleteDisk, getDisk, listOneDisk } from "./workspace/archil-disks.js";
|
|
33
|
+
import { archilWorkspace } from "./workspace/archil.js";
|
|
34
|
+
import { agentDirName } from "./workspace/paths.js";
|
|
35
|
+
import { WorkspaceError } from "./workspace/workspace.js";
|
|
32
36
|
import { ARCHIL_API_KEY_ENV, archilApiKey } from "./workspace/archil-key.js";
|
|
33
37
|
import { launchDiskProblem } from "./workspace/run-disk.js";
|
|
34
38
|
/** The routes, by their operation names. */
|
|
@@ -240,7 +244,58 @@ export function createArchilRoutes(options) {
|
|
|
240
244
|
if (problem !== undefined)
|
|
241
245
|
throw invalid([{ field: "archil_disk.disk_id", reason: "not_an_option", message: problem }]);
|
|
242
246
|
}
|
|
243
|
-
|
|
247
|
+
/** Run `runId`'s own folder on disk `diskId`, a disk its launch named (the caller's: never made, renamed or removed
|
|
248
|
+
* here), deleted over the disk's file API for agent `agentId`, which is being deleted: agents/<agent>/runs/<run> and
|
|
249
|
+
* everything under it (its work folder and its exec spill folder, paths.ts runKey and archil.ts spillFolderOf), then
|
|
250
|
+
* the agent's runs folder and its own folder there once nothing else is in them, since their names carry the agent's
|
|
251
|
+
* id. Nothing outside the agent's own folder is touched. Done again, it does nothing: a folder not there is gone, and
|
|
252
|
+
* so is the folder of a disk Archil says is gone, which has nothing left to delete (one the caller deleted, or the
|
|
253
|
+
* agent's own disk named in the launch, which the agent's delete removed whole before its runs' folders).
|
|
254
|
+
* Archil off, or no account key, is 503 unavailable; a delete Archil refuses (a computer that still holds the folder,
|
|
255
|
+
* Archil not reached) is 424 upstream_failed, what was not deleted left for the next call. */
|
|
256
|
+
async function removeRunFolder(agentId, runId, diskId) {
|
|
257
|
+
const what = `The run ${runId}'s folder on the Archil disk ${diskId}, which its launch named,`;
|
|
258
|
+
const settings = await loadArchilSettings(store, env);
|
|
259
|
+
if (settings === undefined)
|
|
260
|
+
throw new ApiError("unavailable", `${what} is not deleted yet: Archil is off on this server, so it can't be reached. Turn Archil on again (PUT /v2/workspace/archil)`);
|
|
261
|
+
const key = await keyOr("unavailable", `${what} can't be deleted`);
|
|
262
|
+
const disk = (options.control?.makeDisk ?? archilDisk)({ disk: diskId, region: settings.region, s3BaseUrl: settings.s3Url, key: () => key });
|
|
263
|
+
const dir = agentDirName(agentId);
|
|
264
|
+
// A hold another client has on a path (a computer's mount) is waited on this long, not a run's 60 s.
|
|
265
|
+
const on = (root) => archilWorkspace(disk, root, { queueMs: 5_000 });
|
|
266
|
+
try {
|
|
267
|
+
await on(`agents/${dir}/runs`).remove(runId, { recursive: true, force: true });
|
|
268
|
+
for (const [root, name] of [[`agents/${dir}`, "runs"], ["agents", dir]]) {
|
|
269
|
+
try {
|
|
270
|
+
await on(root).remove(name, { force: true });
|
|
271
|
+
}
|
|
272
|
+
catch (error) {
|
|
273
|
+
// Not empty: another of its runs' folders, or something else of the agent, is still there.
|
|
274
|
+
if (error instanceof WorkspaceError && error.code === "REFUSED")
|
|
275
|
+
break;
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
catch (error) {
|
|
281
|
+
if (await diskGone(settings, key, diskId))
|
|
282
|
+
return;
|
|
283
|
+
throw new ApiError("upstream_failed", `${what} is not deleted there yet: ${describe(error).split(key).join("[the key]")}`);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/** Whether Archil's control plane says disk `diskId` is gone: its read answers that the account has no such disk (404,
|
|
287
|
+
* getDisk's undefined) or reads it as deleted. Only Archil's word counts: a read that fails (not reached, a 5xx, the key
|
|
288
|
+
* refused) says nothing, so the disk is taken as still there. */
|
|
289
|
+
async function diskGone(settings, key, diskId) {
|
|
290
|
+
try {
|
|
291
|
+
const disk = await getDisk(control(settings, key), diskId);
|
|
292
|
+
return disk === undefined || disk.status === "deleted";
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return { view, save, off, test, agentView, removeAgentDisk, checkLaunchDisk, removeRunFolder };
|
|
244
299
|
}
|
|
245
300
|
/** One of the owner's Archil routes at its handler: its answer (sent 200), or an ApiError (sent with its status). An
|
|
246
301
|
* agent's route takes the agent as the dispatch read it. */
|
|
@@ -9,6 +9,8 @@ export type WaitEnded = {
|
|
|
9
9
|
at: string;
|
|
10
10
|
why: string;
|
|
11
11
|
};
|
|
12
|
+
/** The settings key a run's ended browser wait is kept under (the run's delete with its agent's: agent-delete.ts). */
|
|
13
|
+
export declare const waitEndedKey: (runId: string) => string;
|
|
12
14
|
/** The run's process, as its wait ends with the browser let go: kept for the Screen tab. */
|
|
13
15
|
export declare function markWaitEnded(settings: WaitEndedSettings, runId: string, ended: WaitEnded, epoch?: number): Promise<void>;
|
|
14
16
|
/** The run's process, as any browser session of the run opens, or at the run's end: nothing ended any more. Given the
|
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
// the wait ends and removed as any browser session of the run opens, whatever let the old one go (its idle release, the
|
|
8
8
|
// tool, a close at the park). So a Screen poll, every few seconds on a parked run, reads one row, never the run's journal.
|
|
9
9
|
const keyOf = (runId) => `browser_wait_ended:${runId}`;
|
|
10
|
+
/** The settings key a run's ended browser wait is kept under (the run's delete with its agent's: agent-delete.ts). */
|
|
11
|
+
export const waitEndedKey = keyOf;
|
|
10
12
|
/** The run's process, as its wait ends with the browser let go: kept for the Screen tab. */
|
|
11
13
|
export async function markWaitEnded(settings, runId, ended, epoch) {
|
|
12
14
|
await settings.put(keyOf(runId), epoch === undefined ? ended : { ...ended, epoch });
|
|
@@ -27,7 +27,9 @@ export type IcsFaux = {
|
|
|
27
27
|
move(path: string, to: string): void;
|
|
28
28
|
/** Every request answers `status` (its body naming the address asked for), or, with undefined, as before. */
|
|
29
29
|
failing(status: number | undefined): void;
|
|
30
|
-
/** The requests it answered,
|
|
30
|
+
/** The requests it answered for its own paths (a calendar it serves, a path it moves and that move's target), oldest
|
|
31
|
+
* first. A request for any other path is answered 404 and not counted: a host process that polls every loopback
|
|
32
|
+
* listener (GET /) is no reader of the calendar. */
|
|
31
33
|
requests(): IcsRequest[];
|
|
32
34
|
close(): Promise<void>;
|
|
33
35
|
};
|
|
@@ -182,9 +182,12 @@ export async function startIcsFaux(options = {}) {
|
|
|
182
182
|
const files = new Map();
|
|
183
183
|
const moves = new Map();
|
|
184
184
|
const log = [];
|
|
185
|
+
// Every path it has served or moved, and each move's target on it: the paths whose requests it counts. Never shrinks.
|
|
186
|
+
const own = new Set();
|
|
185
187
|
let failure;
|
|
186
188
|
let version = 0;
|
|
187
189
|
const hold = (text, path) => {
|
|
190
|
+
own.add(path);
|
|
188
191
|
version += 1;
|
|
189
192
|
// Whole seconds, as an HTTP date says it, each version a second on so If-Modified-Since tells them apart.
|
|
190
193
|
const modified = new Date(Date.UTC(2026, 9, 1, 0, 0, version));
|
|
@@ -196,7 +199,8 @@ export async function startIcsFaux(options = {}) {
|
|
|
196
199
|
const ifNoneMatch = req.headers["if-none-match"];
|
|
197
200
|
const ifModifiedSince = req.headers["if-modified-since"];
|
|
198
201
|
const done = (status, headers = {}, body) => {
|
|
199
|
-
|
|
202
|
+
if (own.has(path))
|
|
203
|
+
log.push({ path, status, ...(typeof ifNoneMatch === "string" ? { ifNoneMatch } : {}), ...(typeof ifModifiedSince === "string" ? { ifModifiedSince } : {}) });
|
|
200
204
|
res.writeHead(status, headers).end(body);
|
|
201
205
|
};
|
|
202
206
|
if (failure !== undefined)
|
|
@@ -225,7 +229,13 @@ export async function startIcsFaux(options = {}) {
|
|
|
225
229
|
address: `${origin}${secret}`,
|
|
226
230
|
at: (path) => `${origin}${path}`,
|
|
227
231
|
serve: (text, path = secret) => hold(text, path),
|
|
228
|
-
move: (path, to) =>
|
|
232
|
+
move: (path, to) => {
|
|
233
|
+
own.add(path);
|
|
234
|
+
// A target on it is counted by its path, as `answer` reads every request's (its query left off).
|
|
235
|
+
if (!/^[a-z]+:\/\//i.test(to))
|
|
236
|
+
own.add(to.split("?")[0]);
|
|
237
|
+
moves.set(path, to);
|
|
238
|
+
},
|
|
229
239
|
failing: (status) => void (failure = status),
|
|
230
240
|
requests: () => log.map((one) => ({ ...one })),
|
|
231
241
|
close: () => new Promise((resolve) => {
|
package/dist/calendar/tools.js
CHANGED
|
@@ -12,6 +12,7 @@ import { forModel } from "../result.js";
|
|
|
12
12
|
import { isObject } from "../validate.js";
|
|
13
13
|
import { openCalendar, scrubAddress } from "./address.js";
|
|
14
14
|
import { CALENDAR_ANSWER_MAX_CHARS, CALENDAR_DESCRIPTION, CALENDAR_ERROR_MAX_CHARS, CALENDAR_EVENTS, NAME_MAX, NO_CALENDAR } from "./names.js";
|
|
15
|
+
import { held } from "../runner/held.js";
|
|
15
16
|
/** `value` as JSON with a line break before each field and item: the longest line is one field's cut value. The run's
|
|
16
17
|
* redactor (key-refusal.ts redactedAnswer) reads an answer in pieces split at whitespace and gives "[redacted]" for a run
|
|
17
18
|
* of 8,192 characters with none, which compact JSON of a busy week (one-word titles, no description) is. */
|
|
@@ -63,9 +64,11 @@ function calendarTool(host) {
|
|
|
63
64
|
},
|
|
64
65
|
replay: "safe",
|
|
65
66
|
execute: async (args, _api, context) => {
|
|
66
|
-
|
|
67
|
+
// A run's own process holds no seal key: its runner opens the calendar for a run its revision grants it (held.ts).
|
|
68
|
+
const atRunner = host.sealKey ? undefined : held();
|
|
69
|
+
if (!host.store || (!host.sealKey && !atRunner))
|
|
67
70
|
return forModel(`${CALENDAR_EVENTS}: the calendar is read on the runner, which this run is not on.`, { isError: true });
|
|
68
|
-
const saved = await openCalendar(host.store, host.sealKey);
|
|
71
|
+
const saved = host.sealKey ? await openCalendar(host.store, host.sealKey) : await atRunner.calendar();
|
|
69
72
|
if (!saved)
|
|
70
73
|
return forModel(`${CALENDAR_EVENTS}: ${NO_CALENDAR}`, { isError: true });
|
|
71
74
|
// The saved address, and once read every address it was redirected to: each cut from the answer and the calendar's texts.
|
package/dist/chatgpt-plan.js
CHANGED
|
@@ -16,6 +16,7 @@ import { CredentialUnavailable } from "@parcha/agentrun-kernel/host";
|
|
|
16
16
|
import { holdSecret } from "./secrets.js";
|
|
17
17
|
import { registerSecretWrite } from "./secret-writes.js";
|
|
18
18
|
import { putSealedKey } from "./server-keys.js";
|
|
19
|
+
import { held } from "./runner/held.js";
|
|
19
20
|
import { sealColumn, sealedAt, STORE_WRITE_DEADLINE_MS, unsealColumn } from "./store/index.js";
|
|
20
21
|
// Every operation of the sign-in is the owner's session's alone (secret-writes.ts), the token in none of them: its view
|
|
21
22
|
// holds no token, but a pending sign-in's link is the owner's to open.
|
|
@@ -542,8 +543,9 @@ export async function planBeforeRun(env, model) {
|
|
|
542
543
|
}
|
|
543
544
|
/** A runner's reader of the plan over its store, loaded now: none when the server holds no sign-in. */
|
|
544
545
|
export async function planReaderOf(store, sealKey) {
|
|
546
|
+
// A run's own process holds no seal key: its runner's reader, for a claim whose models reach the plan (held.ts).
|
|
545
547
|
if (!sealKey)
|
|
546
|
-
return
|
|
548
|
+
return held()?.plan();
|
|
547
549
|
const plan = await new ChatGPTPlan({ store, sealKey }).load();
|
|
548
550
|
return plan.state === "signed_out" ? undefined : plan;
|
|
549
551
|
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { ClaudeAgentLogin, ClaudeLoginStatus } from "@parcha/agentrun-harness-claude";
|
|
2
|
+
import type { Settings } from "./store/index.js";
|
|
3
|
+
/** The switch: `off` (or 0, false, no) turns the mode off on this server, `on` (or 1, true, yes) leaves it there past
|
|
4
|
+
* loopback; unset, the listen address decides (claudeLoginSwitchedOff). */
|
|
5
|
+
export declare const CLAUDE_LOGIN_ENV = "AGENTRUN_CLAUDE_CODE_LOGIN";
|
|
6
|
+
export declare const claudeLoginOff: (env: Readonly<Record<string, string | undefined>>) => boolean;
|
|
7
|
+
/** Why the mode is off on a server listening on `listen`, or undefined where it is there: the switch's `off`, or, unset,
|
|
8
|
+
* a listen address past loopback. */
|
|
9
|
+
export declare function claudeLoginSwitchedOff(env: Readonly<Record<string, string | undefined>>, listen: string): string | undefined;
|
|
10
|
+
/** The setting row: the mode on, where the login is, and the machine it was turned on for. */
|
|
11
|
+
export declare const CLAUDE_LOGIN_SETTING = "claude_login";
|
|
12
|
+
/** The harness that runs on the login: claude-agent, whose pinned CLI holds it. */
|
|
13
|
+
export declare const LOGIN_HARNESS: string;
|
|
14
|
+
/** The provider a run on the login is priced under (provider.ts): billed to the person's plan, never priced here. */
|
|
15
|
+
export declare const CLAUDE_LOGIN_PROVIDER = "claude-login";
|
|
16
|
+
/** The model a run on the login gets when it names none. */
|
|
17
|
+
export declare const LOGIN_DEFAULT_MODEL = "anthropic/claude-sonnet-5-5";
|
|
18
|
+
/** Where the login is: AgentRun's own directory in its data (`agentrun`, the default), or this machine's own Claude Code
|
|
19
|
+
* login (`machine`, which the owner opts into by name). */
|
|
20
|
+
export declare const LOGIN_PLACES: readonly ["agentrun", "machine"];
|
|
21
|
+
export type LoginPlace = (typeof LOGIN_PLACES)[number];
|
|
22
|
+
type Setting = {
|
|
23
|
+
v: 1;
|
|
24
|
+
place: LoginPlace;
|
|
25
|
+
host: string;
|
|
26
|
+
only_me: true;
|
|
27
|
+
};
|
|
28
|
+
/** The directories of a login at `place`: AgentRun's in `data`, or this machine's (the CLI's own defaults for its user). */
|
|
29
|
+
export declare function loginAt(place: LoginPlace, data: string, env?: Readonly<Record<string, string | undefined>>): ClaudeAgentLogin;
|
|
30
|
+
/** The setting as saved, or undefined when the mode is not on (or the row is not one this reads). */
|
|
31
|
+
export declare function claudeLoginSetting(settings: Pick<Settings, "get">): Promise<Setting | undefined>;
|
|
32
|
+
/** 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. */
|
|
33
|
+
export declare function loginOf(setting: Setting | undefined, data: string, env: Readonly<Record<string, string | undefined>>): ClaudeAgentLogin | undefined;
|
|
34
|
+
/** `env` carrying `login` (none: carrying none), as an enumerable symbol property: a spread keeps it, a spawn never sees
|
|
35
|
+
* it (it is no variable), and it holds only two directory names. */
|
|
36
|
+
export declare function withClaudeLogin<T extends object>(env: T, login: ClaudeAgentLogin | undefined): T;
|
|
37
|
+
/** `env` as a run at `place` reads it: a runner place keeps the login; any other place (a sandbox) gets a copy without it,
|
|
38
|
+
* so a revision's checks there ask for a key, as its run will. */
|
|
39
|
+
export declare function envAtPlace<T extends object>(env: T, place: {
|
|
40
|
+
readonly mode: string;
|
|
41
|
+
} | undefined): T;
|
|
42
|
+
/** The login `env` carries, unless the switch is off. */
|
|
43
|
+
export declare function claudeLoginOf(env: NodeJS.ProcessEnv): ClaudeAgentLogin | undefined;
|
|
44
|
+
/** Whether `harness` runs on the login `env` carries. */
|
|
45
|
+
export declare const onClaudeLogin: (env: NodeJS.ProcessEnv, harness: string) => boolean;
|
|
46
|
+
/** The CLI that answers for a login: the pinned one this server installed for claude-agent (`pinned`), else a `claude` on
|
|
47
|
+
* PATH, else none. */
|
|
48
|
+
export declare function loginCli(pinned: string | undefined, env: Readonly<Record<string, string | undefined>>): string | undefined;
|
|
49
|
+
/** The words a run gets when the login is signed out, and when its CLI could not say. */
|
|
50
|
+
export declare const LOGIN_SIGNED_OUT = "Claude Code's login on this machine is signed out: sign in under Settings \u2192 Model keys, or turn the mode off there to run claude-agent on a model key.";
|
|
51
|
+
export declare const LOGIN_UNKNOWN = "Claude Code could not say whether its login on this machine is signed in";
|
|
52
|
+
/** Why this server may not turn the mode on, in one line, or undefined when it may: a Claude plan is one person's. */
|
|
53
|
+
export declare function whyNotOneOwner(facts: {
|
|
54
|
+
apiOnly: boolean;
|
|
55
|
+
tokens: number;
|
|
56
|
+
}): string | undefined;
|
|
57
|
+
/** GET /settings/claude-login. `state`: `off` (not turned on), `other_machine` (turned on for another machine), else
|
|
58
|
+
* what the CLI says: `signed_in`, `signed_out`, or `no_cli` (no CLI here to ask yet: claude-agent's first run installs
|
|
59
|
+
* the pinned one), `unknown` (it gave no answer). Never a token: an account's email, organization and plan only when
|
|
60
|
+
* the CLI says them. */
|
|
61
|
+
export type ClaudeLoginView = {
|
|
62
|
+
state: "off" | "other_machine" | "signed_in" | "signed_out" | "no_cli" | "unknown";
|
|
63
|
+
place: LoginPlace;
|
|
64
|
+
/** The line that signs the CLI in at the login's place, for a terminal on this machine. */
|
|
65
|
+
command: string;
|
|
66
|
+
/** Whether this server may start the CLI's own sign-in itself (a browser on this machine). */
|
|
67
|
+
can_start: boolean;
|
|
68
|
+
/** Why the mode can't be turned on here, in one line. */
|
|
69
|
+
refused?: string;
|
|
70
|
+
account?: {
|
|
71
|
+
method?: string;
|
|
72
|
+
email?: string;
|
|
73
|
+
organization?: string;
|
|
74
|
+
plan?: string;
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
/** The account's words, as the view shows them. */
|
|
78
|
+
export declare const accountOf: (status: ClaudeLoginStatus) => NonNullable<ClaudeLoginView["account"]>;
|
|
79
|
+
/** The body of a turn-on, checked: where the login is, and the owner's word that they alone run agents here. */
|
|
80
|
+
export declare function turnOnBody(body: unknown): {
|
|
81
|
+
place: LoginPlace;
|
|
82
|
+
} | {
|
|
83
|
+
problem: string;
|
|
84
|
+
};
|
|
85
|
+
/** The setting a turn-on saves, for this machine. AgentRun's own login directory is made (0700) so the CLI can keep its
|
|
86
|
+
* login there; nothing in it is read. */
|
|
87
|
+
export declare function settingFor(place: LoginPlace, data: string): Setting;
|
|
88
|
+
/** The login a run of `harness` on this runner goes on: the saved setting's, read now, when the switch is not off, the
|
|
89
|
+
* harness runs on it, the run is in a runner place (`place`), and this is the machine it was turned on for; else none,
|
|
90
|
+
* and the run goes on the model keys. */
|
|
91
|
+
export declare function loginForRun(settings: Pick<Settings, "get">, data: string, env: Readonly<Record<string, string | undefined>>, harness: string | undefined, place: {
|
|
92
|
+
readonly mode: string;
|
|
93
|
+
}): Promise<ClaudeAgentLogin | undefined>;
|
|
94
|
+
/** The routes here (server.ts): every one answers 404, by its name, while the mode is off (`why`). */
|
|
95
|
+
export declare const CLAUDE_LOGIN_ROUTES: ReadonlySet<string>;
|
|
96
|
+
export declare const claudeLoginRouteOff: (name: string, why: string) => string;
|
|
97
|
+
/** What the routes need of the server. `pinned`: the pinned CLI's path when installed. `refused`: whyNotOneOwner of this
|
|
98
|
+
* server. `sameMachine`: the request comes from a browser on this machine to a server bound to loopback with no public
|
|
99
|
+
* address, so a sign-in the CLI opens in a browser here reaches the person. `status`/`start`: the harness's
|
|
100
|
+
* claudeLoginStatus and startClaudeLogin. `changed`: env read again after a turn on or off. */
|
|
101
|
+
export type ClaudeLoginDeps = {
|
|
102
|
+
settings: Pick<Settings, "get" | "put" | "remove">;
|
|
103
|
+
data: string;
|
|
104
|
+
env: Readonly<Record<string, string | undefined>>;
|
|
105
|
+
pinned: () => Promise<string | undefined>;
|
|
106
|
+
refused: string | undefined;
|
|
107
|
+
sameMachine: boolean;
|
|
108
|
+
claude: () => Promise<ClaudeLoginCli>;
|
|
109
|
+
changed: () => Promise<void>;
|
|
110
|
+
};
|
|
111
|
+
/** A route's refusal in words, answered as its `code` (server.ts makes it an ApiError). */
|
|
112
|
+
export declare class ClaudeLoginRefused extends Error {
|
|
113
|
+
readonly code: "forbidden" | "input_invalid" | "agent_not_runnable";
|
|
114
|
+
constructor(code: "forbidden" | "input_invalid" | "agent_not_runnable", message: string);
|
|
115
|
+
}
|
|
116
|
+
/** GET /settings/claude-login: the mode's state and, when on, what the CLI says of its login. */
|
|
117
|
+
export declare function claudeLoginView(deps: ClaudeLoginDeps): Promise<ClaudeLoginView>;
|
|
118
|
+
/** PUT /settings/claude-login: turned on, refused in one line where more than one person can start runs. */
|
|
119
|
+
export declare function turnOnClaudeLogin(deps: ClaudeLoginDeps, body: unknown): Promise<ClaudeLoginView>;
|
|
120
|
+
/** DELETE /settings/claude-login: turned off. The login itself stays as the CLI keeps it; nothing here signs it out. */
|
|
121
|
+
export declare function turnOffClaudeLogin(deps: ClaudeLoginDeps): Promise<ClaudeLoginView>;
|
|
122
|
+
/** POST /settings/claude-login/login: the CLI's own sign-in, started on this machine, where it opens a browser. The
|
|
123
|
+
* browser code goes to the CLI, never here; the answer is the view as it is when the sign-in starts. */
|
|
124
|
+
export declare function startLoginHere(deps: ClaudeLoginDeps): Promise<ClaudeLoginView>;
|
|
125
|
+
/** At the server's start: a setting saved where the mode is now off or refused (`why`: the switch, a listen address past
|
|
126
|
+
* loopback, a second token, the API-only profile) goes, so no runner, in this process or another, runs on it; the reason
|
|
127
|
+
* is logged. */
|
|
128
|
+
export declare function dropRefusedLogin(settings: Pick<Settings, "get" | "remove">, why: string | undefined, log: (line: string) => void): Promise<void>;
|
|
129
|
+
/** What the routes and a run's check use of the harness (its login.ts), loaded only when asked. */
|
|
130
|
+
export type ClaudeLoginCli = Pick<typeof import("@parcha/agentrun-harness-claude"), "claudeLoginStatus" | "startClaudeLogin" | "claudeLoginCommand">;
|
|
131
|
+
/** 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
|
|
132
|
+
* key). `cli`: the CLI that answers (loginCli); none, nothing to ask yet. `status`: the CLI's status check (the harness's
|
|
133
|
+
* claudeLoginStatus). */
|
|
134
|
+
export declare function loginBeforeRun(login: ClaudeAgentLogin | undefined, cli: string | undefined, status: (cli: string, login: ClaudeAgentLogin) => Promise<ClaudeLoginStatus>): Promise<void>;
|
|
135
|
+
export {};
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// Claude Code's login on this machine as claude-agent's model source: a person's own Claude plan, used by the pinned
|
|
2
|
+
// Claude Code CLI as that CLI always uses it. AgentRun never reads, copies, stores, proxies or relays the login or a
|
|
3
|
+
// browser code: the CLI keeps it in its own files and calls Anthropic with it itself (the harness's login.ts). This
|
|
4
|
+
// server only tells the CLI where its login is, asks it for its status (`claude auth status`), and keeps one setting: that
|
|
5
|
+
// the owner turned the mode on, where the login is, and on which machine.
|
|
6
|
+
//
|
|
7
|
+
// One switch (AGENTRUN_CLAUDE_CODE_LOGIN): unset, the mode is there only on a server that listens on loopback, so a
|
|
8
|
+
// server bound past it (0.0.0.0 in a container, a hosted service) has it off with nothing to set; `off` or `on` says so
|
|
9
|
+
// outright. Off, the Settings row is gone, every route here answers 404, a saved turn-on is dropped at the start, and
|
|
10
|
+
// harness selection reads no setting, so claude-agent runs on the model keys as it does without the mode. The mode is
|
|
11
|
+
// for a server one person runs: it is refused where more than one person can start runs, `on` included, and the owner
|
|
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 { accessSync, constants, existsSync, mkdirSync } from "node:fs";
|
|
14
|
+
import { homedir, hostname } from "node:os";
|
|
15
|
+
import { delimiter, isAbsolute, join } from "node:path";
|
|
16
|
+
import { facts as claudeFacts } from "@parcha/agentrun-harness-claude/facts";
|
|
17
|
+
import { registerSecretWrite } from "./secret-writes.js";
|
|
18
|
+
// Every operation here is the owner's session's alone (secret-writes.ts): no answer holds a credential, but turning a
|
|
19
|
+
// person's plan on for this server's runs, and starting its sign-in, are the owner's to do.
|
|
20
|
+
for (const operation of ["getClaudeLogin", "turnOnClaudeLogin", "turnOffClaudeLogin", "startClaudeLogin"])
|
|
21
|
+
registerSecretWrite(operation, () => true);
|
|
22
|
+
/** The switch: `off` (or 0, false, no) turns the mode off on this server, `on` (or 1, true, yes) leaves it there past
|
|
23
|
+
* loopback; unset, the listen address decides (claudeLoginSwitchedOff). */
|
|
24
|
+
export const CLAUDE_LOGIN_ENV = "AGENTRUN_CLAUDE_CODE_LOGIN";
|
|
25
|
+
export const claudeLoginOff = (env) => /^(off|0|false|no)$/i.test((env[CLAUDE_LOGIN_ENV] ?? "").trim());
|
|
26
|
+
const claudeLoginOn = (env) => /^(on|1|true|yes)$/i.test((env[CLAUDE_LOGIN_ENV] ?? "").trim());
|
|
27
|
+
const loopback = (host) => host === "localhost" || host === "::1" || /^127\./.test(host); // key-gate: loopback-rule the default follows a loopback listen address
|
|
28
|
+
/** Why the mode is off on a server listening on `listen`, or undefined where it is there: the switch's `off`, or, unset,
|
|
29
|
+
* a listen address past loopback. */
|
|
30
|
+
export function claudeLoginSwitchedOff(env, listen) {
|
|
31
|
+
if (claudeLoginOff(env))
|
|
32
|
+
return `${CLAUDE_LOGIN_ENV}=off`;
|
|
33
|
+
if (claudeLoginOn(env) || loopback(listen))
|
|
34
|
+
return undefined;
|
|
35
|
+
return `it listens on ${listen}, past loopback; ${CLAUDE_LOGIN_ENV}=on turns it on`;
|
|
36
|
+
}
|
|
37
|
+
/** The setting row: the mode on, where the login is, and the machine it was turned on for. */
|
|
38
|
+
export const CLAUDE_LOGIN_SETTING = "claude_login";
|
|
39
|
+
/** The harness that runs on the login: claude-agent, whose pinned CLI holds it. */
|
|
40
|
+
export const LOGIN_HARNESS = claudeFacts.id;
|
|
41
|
+
const LOGIN_HARNESSES = Object.freeze([LOGIN_HARNESS]);
|
|
42
|
+
/** The provider a run on the login is priced under (provider.ts): billed to the person's plan, never priced here. */
|
|
43
|
+
export const CLAUDE_LOGIN_PROVIDER = "claude-login";
|
|
44
|
+
/** The model a run on the login gets when it names none. */
|
|
45
|
+
export const LOGIN_DEFAULT_MODEL = "anthropic/claude-sonnet-5-5";
|
|
46
|
+
/** Where the login is: AgentRun's own directory in its data (`agentrun`, the default), or this machine's own Claude Code
|
|
47
|
+
* login (`machine`, which the owner opts into by name). */
|
|
48
|
+
export const LOGIN_PLACES = ["agentrun", "machine"];
|
|
49
|
+
/** The directories of a login at `place`: AgentRun's in `data`, or this machine's (the CLI's own defaults for its user). */
|
|
50
|
+
export function loginAt(place, data, env = process.env) {
|
|
51
|
+
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();
|
|
54
|
+
const config = env.XDG_CONFIG_HOME && isAbsolute(env.XDG_CONFIG_HOME) ? env.XDG_CONFIG_HOME : join(home, ".config");
|
|
55
|
+
return { dir: join(home, ".claude"), profiles: env.ANTHROPIC_CONFIG_DIR && isAbsolute(env.ANTHROPIC_CONFIG_DIR) ? env.ANTHROPIC_CONFIG_DIR : join(config, "anthropic") };
|
|
56
|
+
}
|
|
57
|
+
/** The setting as saved, or undefined when the mode is not on (or the row is not one this reads). */
|
|
58
|
+
export async function claudeLoginSetting(settings) {
|
|
59
|
+
const row = (await settings.get(CLAUDE_LOGIN_SETTING));
|
|
60
|
+
if (!row || row.v !== 1 || !LOGIN_PLACES.includes(row.place) || typeof row.host !== "string" || row.only_me !== true)
|
|
61
|
+
return undefined;
|
|
62
|
+
return row;
|
|
63
|
+
}
|
|
64
|
+
/** 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
|
+
export function loginOf(setting, data, env) {
|
|
66
|
+
if (!setting || claudeLoginOff(env) || setting.host !== hostname())
|
|
67
|
+
return undefined;
|
|
68
|
+
return loginAt(setting.place, data, env);
|
|
69
|
+
}
|
|
70
|
+
const LOGIN = Symbol.for("agentrun.claude-login");
|
|
71
|
+
/** `env` carrying `login` (none: carrying none), as an enumerable symbol property: a spread keeps it, a spawn never sees
|
|
72
|
+
* it (it is no variable), and it holds only two directory names. */
|
|
73
|
+
export function withClaudeLogin(env, login) {
|
|
74
|
+
if (login)
|
|
75
|
+
Object.defineProperty(env, LOGIN, { value: Object.freeze({ ...login }), enumerable: true, configurable: true, writable: false });
|
|
76
|
+
else
|
|
77
|
+
delete env[LOGIN];
|
|
78
|
+
return env;
|
|
79
|
+
}
|
|
80
|
+
/** `env` as a run at `place` reads it: a runner place keeps the login; any other place (a sandbox) gets a copy without it,
|
|
81
|
+
* so a revision's checks there ask for a key, as its run will. */
|
|
82
|
+
export function envAtPlace(env, place) {
|
|
83
|
+
if (place?.mode === "runner" || env[LOGIN] === undefined)
|
|
84
|
+
return env;
|
|
85
|
+
return withClaudeLogin({ ...env }, undefined);
|
|
86
|
+
}
|
|
87
|
+
/** The login `env` carries, unless the switch is off. */
|
|
88
|
+
export function claudeLoginOf(env) {
|
|
89
|
+
if (claudeLoginOff(env))
|
|
90
|
+
return undefined;
|
|
91
|
+
return env[LOGIN];
|
|
92
|
+
}
|
|
93
|
+
/** Whether `harness` runs on the login `env` carries. */
|
|
94
|
+
export const onClaudeLogin = (env, harness) => LOGIN_HARNESSES.includes(harness) && claudeLoginOf(env) !== undefined;
|
|
95
|
+
/** The CLI that answers for a login: the pinned one this server installed for claude-agent (`pinned`), else a `claude` on
|
|
96
|
+
* PATH, else none. */
|
|
97
|
+
export function loginCli(pinned, env) {
|
|
98
|
+
if (pinned && existsSync(pinned))
|
|
99
|
+
return pinned;
|
|
100
|
+
for (const dir of (env.PATH ?? "").split(delimiter)) {
|
|
101
|
+
if (!dir || !isAbsolute(dir))
|
|
102
|
+
continue;
|
|
103
|
+
const file = join(dir, process.platform === "win32" ? "claude.exe" : "claude");
|
|
104
|
+
try {
|
|
105
|
+
accessSync(file, constants.X_OK);
|
|
106
|
+
return file;
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
// Not here.
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
/** The words a run gets when the login is signed out, and when its CLI could not say. */
|
|
115
|
+
export const LOGIN_SIGNED_OUT = "Claude Code's login on this machine is signed out: sign in under Settings → Model keys, or turn the mode off there to run claude-agent on a model key.";
|
|
116
|
+
export const LOGIN_UNKNOWN = "Claude Code could not say whether its login on this machine is signed in";
|
|
117
|
+
/** Why this server may not turn the mode on, in one line, or undefined when it may: a Claude plan is one person's. */
|
|
118
|
+
export function whyNotOneOwner(facts) {
|
|
119
|
+
if (facts.apiOnly)
|
|
120
|
+
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
|
+
if (facts.tokens > 1)
|
|
122
|
+
return `This server takes ${facts.tokens} API tokens, so more than one person can start runs here: a Claude plan is one person's.`;
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
/** The account's words, as the view shows them. */
|
|
126
|
+
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
|
+
/** The body of a turn-on, checked: where the login is, and the owner's word that they alone run agents here. */
|
|
128
|
+
export function turnOnBody(body) {
|
|
129
|
+
const given = (body ?? {});
|
|
130
|
+
if (given.place !== undefined && !LOGIN_PLACES.includes(given.place))
|
|
131
|
+
return { problem: `place is one of ${LOGIN_PLACES.join(", ")}` };
|
|
132
|
+
if (given.only_me !== true)
|
|
133
|
+
return { problem: "only_me must be true: confirm you alone run agents on this server, since its runs would use your Claude plan" };
|
|
134
|
+
return { place: given.place ?? "agentrun" };
|
|
135
|
+
}
|
|
136
|
+
/** The setting a turn-on saves, for this machine. AgentRun's own login directory is made (0700) so the CLI can keep its
|
|
137
|
+
* login there; nothing in it is read. */
|
|
138
|
+
export function settingFor(place, data) {
|
|
139
|
+
if (place === "agentrun")
|
|
140
|
+
mkdirSync(join(data, "claude-login"), { recursive: true, mode: 0o700 });
|
|
141
|
+
return { v: 1, place, host: hostname(), only_me: true };
|
|
142
|
+
}
|
|
143
|
+
/** 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
|
+
* harness runs on it, the run is in a runner place (`place`), and this is the machine it was turned on for; else none,
|
|
145
|
+
* and the run goes on the model keys. */
|
|
146
|
+
export async function loginForRun(settings, data, env, harness, place) {
|
|
147
|
+
if (harness === undefined || !LOGIN_HARNESSES.includes(harness) || claudeLoginOff(env) || place.mode !== "runner")
|
|
148
|
+
return undefined;
|
|
149
|
+
return loginOf(await claudeLoginSetting(settings), data, env);
|
|
150
|
+
}
|
|
151
|
+
/** The routes here (server.ts): every one answers 404, by its name, while the mode is off (`why`). */
|
|
152
|
+
export const CLAUDE_LOGIN_ROUTES = new Set(["getClaudeLogin", "turnOnClaudeLogin", "turnOffClaudeLogin", "startClaudeLogin"]);
|
|
153
|
+
export const claudeLoginRouteOff = (name, why) => `${name}: Claude Code's login on this machine is off on this server: ${why}.`;
|
|
154
|
+
/** A route's refusal in words, answered as its `code` (server.ts makes it an ApiError). */
|
|
155
|
+
export class ClaudeLoginRefused extends Error {
|
|
156
|
+
code;
|
|
157
|
+
constructor(code, message) {
|
|
158
|
+
super(message);
|
|
159
|
+
this.code = code;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/** GET /settings/claude-login: the mode's state and, when on, what the CLI says of its login. */
|
|
163
|
+
export async function claudeLoginView(deps) {
|
|
164
|
+
const setting = await claudeLoginSetting(deps.settings);
|
|
165
|
+
const place = setting?.place ?? "agentrun";
|
|
166
|
+
const login = loginAt(place, deps.data, deps.env);
|
|
167
|
+
const cli = loginCli(await deps.pinned(), deps.env);
|
|
168
|
+
const base = { place, command: (await deps.claude()).claudeLoginCommand(login, cli ?? "claude"), can_start: deps.sameMachine && cli !== undefined && deps.refused === undefined, ...(deps.refused ? { refused: deps.refused } : {}) };
|
|
169
|
+
if (!setting)
|
|
170
|
+
return { state: "off", ...base };
|
|
171
|
+
if (setting.host !== hostname())
|
|
172
|
+
return { state: "other_machine", ...base, can_start: false };
|
|
173
|
+
if (!cli)
|
|
174
|
+
return { state: "no_cli", ...base };
|
|
175
|
+
try {
|
|
176
|
+
const said = await (await deps.claude()).claudeLoginStatus(cli, login, pathOf(deps.env));
|
|
177
|
+
return said.signedIn ? { state: "signed_in", ...base, account: accountOf(said) } : { state: "signed_out", ...base };
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
return { state: "unknown", ...base };
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/** PUT /settings/claude-login: turned on, refused in one line where more than one person can start runs. */
|
|
184
|
+
export async function turnOnClaudeLogin(deps, body) {
|
|
185
|
+
if (deps.refused)
|
|
186
|
+
throw new ClaudeLoginRefused("forbidden", deps.refused);
|
|
187
|
+
const asked = turnOnBody(body);
|
|
188
|
+
if ("problem" in asked)
|
|
189
|
+
throw new ClaudeLoginRefused("input_invalid", asked.problem);
|
|
190
|
+
await deps.settings.put(CLAUDE_LOGIN_SETTING, settingFor(asked.place, deps.data));
|
|
191
|
+
await deps.changed();
|
|
192
|
+
return claudeLoginView(deps);
|
|
193
|
+
}
|
|
194
|
+
/** DELETE /settings/claude-login: turned off. The login itself stays as the CLI keeps it; nothing here signs it out. */
|
|
195
|
+
export async function turnOffClaudeLogin(deps) {
|
|
196
|
+
await deps.settings.remove(CLAUDE_LOGIN_SETTING);
|
|
197
|
+
await deps.changed();
|
|
198
|
+
return claudeLoginView(deps);
|
|
199
|
+
}
|
|
200
|
+
/** POST /settings/claude-login/login: the CLI's own sign-in, started on this machine, where it opens a browser. The
|
|
201
|
+
* browser code goes to the CLI, never here; the answer is the view as it is when the sign-in starts. */
|
|
202
|
+
export async function startLoginHere(deps) {
|
|
203
|
+
if (deps.refused)
|
|
204
|
+
throw new ClaudeLoginRefused("forbidden", deps.refused);
|
|
205
|
+
const setting = await claudeLoginSetting(deps.settings);
|
|
206
|
+
if (!setting)
|
|
207
|
+
throw new ClaudeLoginRefused("agent_not_runnable", "Turn on Claude Code's login on this machine first.");
|
|
208
|
+
if (setting.host !== hostname())
|
|
209
|
+
throw new ClaudeLoginRefused("agent_not_runnable", "It was turned on for another machine: sign in there.");
|
|
210
|
+
if (!deps.sameMachine)
|
|
211
|
+
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.");
|
|
212
|
+
const cli = loginCli(await deps.pinned(), deps.env);
|
|
213
|
+
if (!cli)
|
|
214
|
+
throw new ClaudeLoginRefused("agent_not_runnable", "No Claude Code CLI here yet: claude-agent's first run installs it, or run the command shown.");
|
|
215
|
+
// The person's home and desktop, so the CLI can open their browser.
|
|
216
|
+
(await deps.claude()).startClaudeLogin(cli, loginAt(setting.place, deps.data, deps.env), { ...pathOf(deps.env), ...(deps.env.HOME ? { home: deps.env.HOME } : {}), display: deps.env });
|
|
217
|
+
return claudeLoginView(deps);
|
|
218
|
+
}
|
|
219
|
+
/** At the server's start: a setting saved where the mode is now off or refused (`why`: the switch, a listen address past
|
|
220
|
+
* 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 || !(await claudeLoginSetting(settings)))
|
|
224
|
+
return;
|
|
225
|
+
await settings.remove(CLAUDE_LOGIN_SETTING);
|
|
226
|
+
log(`Claude Code's login on this machine turned off: ${why}`);
|
|
227
|
+
}
|
|
228
|
+
const pathOf = (env) => (env.PATH ? { path: env.PATH } : {});
|
|
229
|
+
/** 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
|
+
* key). `cli`: the CLI that answers (loginCli); none, nothing to ask yet. `status`: the CLI's status check (the harness's
|
|
231
|
+
* claudeLoginStatus). */
|
|
232
|
+
export async function loginBeforeRun(login, cli, status) {
|
|
233
|
+
if (!login || !cli)
|
|
234
|
+
return;
|
|
235
|
+
let said;
|
|
236
|
+
try {
|
|
237
|
+
said = await status(cli, login);
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
throw new Error(`${LOGIN_UNKNOWN}: ${String(error?.message ?? error).slice(0, 200)}`);
|
|
241
|
+
}
|
|
242
|
+
if (!said.signedIn)
|
|
243
|
+
throw new Error(LOGIN_SIGNED_OUT);
|
|
244
|
+
}
|