@parcha/agentrun-serve 0.1.0-beta.51 → 0.1.0-beta.53
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 +160 -1
- package/README.md +306 -36
- package/dist/adapters.js +1 -1
- package/dist/agent-delete.d.ts +14 -4
- package/dist/agent-delete.js +30 -58
- package/dist/agent-package.d.ts +71 -0
- package/dist/agent-package.js +345 -0
- package/dist/agents-store.d.ts +28 -18
- package/dist/agents-store.js +48 -34
- package/dist/api-only.d.ts +30 -8
- package/dist/api-only.js +40 -16
- package/dist/archil-routes.d.ts +1 -0
- package/dist/archil-routes.js +28 -2
- package/dist/claude-login.d.ts +1 -0
- package/dist/claude-login.js +1 -1
- package/dist/cli-learning/cli.d.ts +3 -0
- package/dist/cli-learning/cli.js +17 -3
- package/dist/cli-learning/connections.js +3 -2
- package/dist/cli-learning/schema.js +2 -1
- package/dist/cli.js +70 -7
- package/dist/composio-preset.d.ts +2 -1
- package/dist/composio-preset.js +4 -3
- package/dist/composio-triggers.d.ts +4 -0
- package/dist/composio-triggers.js +4 -1
- package/dist/computer-needs.d.ts +32 -0
- package/dist/computer-needs.js +80 -0
- package/dist/computer-setting.d.ts +9 -0
- package/dist/computer-setting.js +19 -1
- package/dist/computers.d.ts +6 -2
- package/dist/computers.js +2 -2
- package/dist/console/assets/index-BFgyZzF_.js +734 -0
- package/dist/console/assets/index-DbilxljH.css +1 -0
- package/dist/console/index.html +2 -2
- package/dist/console-links.d.ts +1 -0
- package/dist/console-links.js +2 -0
- package/dist/env-switch.d.ts +14 -0
- package/dist/env-switch.js +23 -0
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +3 -0
- package/dist/eval-previous.js +4 -3
- package/dist/fal-price.d.ts +76 -0
- package/dist/fal-price.js +365 -0
- package/dist/handed-secret.d.ts +4 -0
- package/dist/handed-secret.js +10 -0
- package/dist/harness-archive.d.ts +15 -5
- package/dist/harness-archive.js +71 -34
- package/dist/harness-check.d.ts +2 -1
- package/dist/harness-check.js +36 -95
- package/dist/harness-egress.d.ts +23 -0
- package/dist/harness-egress.js +69 -0
- package/dist/harness-recipe-entry.d.ts +71 -0
- package/dist/harness-recipe-entry.js +184 -0
- package/dist/harness-recipe.d.ts +73 -6
- package/dist/harness-recipe.js +185 -31
- package/dist/harness-sign-in.d.ts +29 -0
- package/dist/harness-sign-in.js +122 -0
- package/dist/harness-table.d.ts +21 -0
- package/dist/harness-table.js +58 -3
- package/dist/harness-zip.d.ts +6 -0
- package/dist/harness-zip.js +203 -0
- package/dist/ids.d.ts +11 -0
- package/dist/ids.js +36 -0
- package/dist/learn/commit.d.ts +19 -1
- package/dist/learn/commit.js +160 -2
- package/dist/learn/compile.d.ts +13 -5
- package/dist/learn/compile.js +46 -15
- package/dist/learn/delete.d.ts +12 -6
- package/dist/learn/delete.js +17 -11
- package/dist/learn/folder.d.ts +16 -0
- package/dist/learn/folder.js +30 -4
- package/dist/learn/learning-of.d.ts +8 -0
- package/dist/learn/learning-of.js +8 -0
- package/dist/learn/learning.sh +29 -4
- package/dist/learn/promote.d.ts +51 -7
- package/dist/learn/promote.js +51 -8
- package/dist/learn/secrets.js +30 -7
- package/dist/learning-reset.d.ts +18 -0
- package/dist/learning-reset.js +115 -0
- package/dist/learning-routes.d.ts +23 -11
- package/dist/learning-routes.js +110 -20
- package/dist/learning-words.d.ts +5 -0
- package/dist/learning-words.js +5 -0
- package/dist/learning.d.ts +15 -10
- package/dist/learning.js +14 -8
- package/dist/llms.txt +1 -0
- package/dist/mcp-gateway.d.ts +39 -2
- package/dist/mcp-gateway.js +56 -12
- package/dist/mcp-pin-first.js +1 -1
- package/dist/mcp-probe.d.ts +3 -0
- package/dist/mcp-probe.js +7 -2
- package/dist/mcp-resources.d.ts +44 -0
- package/dist/mcp-resources.js +100 -0
- package/dist/mcp-servers-store.d.ts +17 -12
- package/dist/mcp-servers-store.js +2 -1
- package/dist/media-duration.d.ts +2 -0
- package/dist/media-duration.js +136 -0
- package/dist/model.d.ts +4 -4
- package/dist/model.js +6 -6
- package/dist/native/cli.js +9 -2
- package/dist/native/connection.d.ts +2 -2
- package/dist/native/connection.js +5 -2
- package/dist/native/discovery.d.ts +2 -0
- package/dist/native/discovery.js +10 -6
- package/dist/native/remote-worker-bridge.js +36 -4
- package/dist/native/remote.d.ts +18 -1
- package/dist/native/remote.js +126 -37
- package/dist/native/schema.d.ts +6 -0
- package/dist/native/schema.js +31 -6
- package/dist/native/source-account.d.ts +13 -0
- package/dist/native/source-account.js +114 -0
- package/dist/native/source-capture-hooks.d.ts +5 -0
- package/dist/native/source-capture-hooks.js +102 -0
- package/dist/native/source-mcp-selection.d.ts +13 -0
- package/dist/native/source-mcp-selection.js +72 -0
- package/dist/native/source-runtime-tools.d.ts +19 -0
- package/dist/native/source-runtime-tools.js +103 -0
- package/dist/native/source-snapshot-types.d.ts +138 -0
- package/dist/native/source-snapshot-types.js +12 -0
- package/dist/native/source-snapshot.d.ts +8 -0
- package/dist/native/source-snapshot.js +1201 -0
- package/dist/observation/learning.js +3 -2
- package/dist/openapi.js +211 -47
- package/dist/print.js +1 -1
- package/dist/provider.d.ts +8 -0
- package/dist/provider.js +14 -2
- package/dist/purged-workflow.d.ts +12 -0
- package/dist/purged-workflow.js +43 -0
- package/dist/refusal-words.d.ts +0 -3
- package/dist/refusal-words.js +0 -3
- package/dist/request-metadata.d.ts +20 -0
- package/dist/request-metadata.js +24 -0
- package/dist/run-keys.d.ts +46 -0
- package/dist/run-keys.js +109 -0
- package/dist/run-launch.d.ts +29 -5
- package/dist/run-launch.js +44 -4
- package/dist/run-learning-served.d.ts +2 -2
- package/dist/run-model-key.d.ts +2 -2
- package/dist/run-model-key.js +5 -5
- package/dist/run-secrets.d.ts +2 -0
- package/dist/run-secrets.js +7 -4
- package/dist/run-usage.d.ts +48 -4
- package/dist/run-usage.js +156 -39
- package/dist/run-view.d.ts +18 -1
- package/dist/run-view.js +28 -8
- package/dist/runner/adapter-loop.d.ts +14 -4
- package/dist/runner/adapter-loop.js +150 -31
- package/dist/runner/adapter-run-spec.d.ts +1 -1
- package/dist/runner/adapter-run-spec.js +35 -22
- package/dist/runner/audit.d.ts +10 -3
- package/dist/runner/audit.js +30 -7
- package/dist/runner/beside.js +2 -1
- package/dist/runner/claim-secrets.d.ts +31 -4
- package/dist/runner/claim-secrets.js +43 -7
- package/dist/runner/cli-run-spec.d.ts +1 -1
- package/dist/runner/cli-run-spec.js +32 -20
- package/dist/runner/conformance/harness.js +4 -2
- package/dist/runner/dispatch.js +4 -2
- package/dist/runner/harness-loops.d.ts +6 -0
- package/dist/runner/harness-loops.js +16 -3
- package/dist/runner/held.d.ts +4 -0
- package/dist/runner/journal-kinds.d.ts +5 -0
- package/dist/runner/learned-first.d.ts +2 -0
- package/dist/runner/learned-first.js +1 -1
- package/dist/runner/learning-end.d.ts +9 -3
- package/dist/runner/learning-end.js +66 -29
- package/dist/runner/learning-seed.d.ts +9 -3
- package/dist/runner/learning-seed.js +2 -2
- package/dist/runner/main.d.ts +1 -0
- package/dist/runner/main.js +20 -13
- package/dist/runner/mcp-claim.js +7 -2
- package/dist/runner/mcp-run.d.ts +12 -0
- package/dist/runner/mcp-run.js +14 -1
- package/dist/runner/model-route.js +7 -4
- package/dist/runner/monid-run.js +9 -11
- package/dist/runner/project-checkpoint.d.ts +35 -0
- package/dist/runner/project-checkpoint.js +116 -0
- package/dist/runner/project-drain.d.ts +8 -0
- package/dist/runner/project-drain.js +25 -0
- package/dist/runner/project-host.d.ts +10 -0
- package/dist/runner/project-host.js +52 -0
- package/dist/runner/promotion.d.ts +51 -24
- package/dist/runner/promotion.js +206 -78
- package/dist/runner/redact.d.ts +13 -0
- package/dist/runner/redact.js +67 -1
- package/dist/runner/run-channel.d.ts +9 -0
- package/dist/runner/run-channel.js +15 -2
- package/dist/runner/run-child-host.d.ts +8 -4
- package/dist/runner/run-child-host.js +174 -42
- package/dist/runner/run-errors.js +2 -2
- package/dist/runner/runner-check.js +5 -2
- package/dist/runner/runner.js +41 -12
- package/dist/runner/sdk-loop.d.ts +2 -2
- package/dist/runner/sdk-loop.js +3 -3
- package/dist/runner/store-port.d.ts +3 -0
- package/dist/runner/store-secrets.js +7 -3
- package/dist/runner/workflow-first.d.ts +4 -0
- package/dist/runner/workflow-first.js +13 -0
- package/dist/runner/workflow-loop.d.ts +2 -1
- package/dist/runner/workflow-loop.js +2 -1
- package/dist/runner/workflow-spec.js +4 -4
- package/dist/runs-over-store.d.ts +39 -8
- package/dist/runs-over-store.js +147 -32
- package/dist/sdk/check.d.ts +6 -5
- package/dist/sdk/check.js +17 -16
- package/dist/sdk/runs.d.ts +11 -1
- package/dist/sdk/runs.js +185 -78
- package/dist/sdk/schema.js +12 -2
- package/dist/sdk/workflows.d.ts +1 -1
- package/dist/server.d.ts +68 -32
- package/dist/server.js +678 -124
- package/dist/skills/agentrun-serve/SKILL.md +2 -1
- package/dist/source-sync/adoption.d.ts +15 -0
- package/dist/source-sync/adoption.js +36 -0
- package/dist/source-sync/auxiliary.d.ts +23 -0
- package/dist/source-sync/auxiliary.js +64 -0
- package/dist/source-sync/capture-git.d.ts +5 -0
- package/dist/source-sync/capture-git.js +272 -0
- package/dist/source-sync/capture-project.d.ts +53 -0
- package/dist/source-sync/capture-project.js +206 -0
- package/dist/source-sync/capture-refusal.d.ts +7 -0
- package/dist/source-sync/capture-refusal.js +16 -0
- package/dist/source-sync/capture.d.ts +11 -0
- package/dist/source-sync/capture.js +22 -0
- package/dist/source-sync/checkpoint-gate.d.ts +21 -0
- package/dist/source-sync/checkpoint-gate.js +52 -0
- package/dist/source-sync/checkpoints.d.ts +66 -0
- package/dist/source-sync/checkpoints.js +168 -0
- package/dist/source-sync/claim.d.ts +11 -0
- package/dist/source-sync/claim.js +48 -0
- package/dist/source-sync/destination.d.ts +47 -0
- package/dist/source-sync/destination.js +239 -0
- package/dist/source-sync/io.d.ts +15 -0
- package/dist/source-sync/io.js +52 -0
- package/dist/source-sync/local.d.ts +43 -0
- package/dist/source-sync/local.js +229 -0
- package/dist/source-sync/prepare.d.ts +59 -0
- package/dist/source-sync/prepare.js +93 -0
- package/dist/source-sync/projects.d.ts +61 -0
- package/dist/source-sync/projects.js +130 -0
- package/dist/source-sync/registry.d.ts +95 -0
- package/dist/source-sync/registry.js +226 -0
- package/dist/source-sync/service.d.ts +33 -0
- package/dist/source-sync/service.js +118 -0
- package/dist/source-sync/skill-edits.d.ts +12 -0
- package/dist/source-sync/skill-edits.js +122 -0
- package/dist/source-sync/snapshots.d.ts +48 -0
- package/dist/source-sync/snapshots.js +448 -0
- package/dist/source-sync/types.d.ts +9 -0
- package/dist/source-sync/types.js +14 -0
- package/dist/source-sync/workspace.d.ts +66 -0
- package/dist/source-sync/workspace.js +273 -0
- package/dist/store/agent-body.d.ts +8 -0
- package/dist/store/agent-body.js +20 -6
- package/dist/store/index.d.ts +2 -2
- package/dist/store/index.js +1 -1
- package/dist/store/journal/storage.d.ts +2 -0
- package/dist/store/journal/storage.js +2 -1
- package/dist/store/postgres/migrations/0025_run_attempts.sql +27 -0
- package/dist/store/postgres/migrations/0026_run_disk.sql +23 -0
- package/dist/store/schema.d.ts +15 -1
- package/dist/store/schema.js +43 -4
- package/dist/store/seal.d.ts +5 -1
- package/dist/store/seal.js +12 -3
- package/dist/store/sqlite/project-checkpoints.d.ts +11 -0
- package/dist/store/sqlite/project-checkpoints.js +70 -0
- package/dist/store/sqlite/run-launch.d.ts +14 -0
- package/dist/store/sqlite/run-launch.js +58 -7
- package/dist/store/sqlite/run-patch.d.ts +2 -1
- package/dist/store/sqlite/run-patch.js +4 -3
- package/dist/store/sqlite/store.js +319 -153
- package/dist/store/store-schema.js +2 -2
- package/dist/store/store.d.ts +173 -33
- package/dist/store/store.js +14 -1
- package/dist/store-reads/learning.d.ts +12 -8
- package/dist/store-reads/learning.js +33 -24
- package/dist/store-reads/loop.d.ts +4 -3
- package/dist/store-reads/loop.js +7 -5
- package/dist/store-view.d.ts +3 -1
- package/dist/store-view.js +17 -5
- package/dist/template-eval.js +4 -4
- package/dist/template-import.d.ts +10 -2
- package/dist/template-import.js +116 -13
- package/dist/templates.d.ts +29 -5
- package/dist/templates.js +68 -14
- package/dist/timeline/index.d.ts +1 -1
- package/dist/timeline/index.js +1 -1
- package/dist/timeline/labels.js +1 -1
- package/dist/timeline/timeline.d.ts +5 -0
- package/dist/timeline/timeline.js +8 -0
- package/dist/tool-plane.d.ts +12 -12
- package/dist/tools.d.ts +8 -8
- package/dist/tools.js +1 -1
- package/dist/trigger-launch-hook.d.ts +47 -0
- package/dist/trigger-launch-hook.js +90 -0
- package/dist/trigger-targets.d.ts +14 -1
- package/dist/trigger-targets.js +20 -0
- package/dist/triggers.d.ts +54 -5
- package/dist/triggers.js +143 -8
- package/dist/uploads-archil.js +2 -1
- package/dist/uploads.d.ts +0 -3
- package/dist/uploads.js +2 -11
- package/dist/validate.js +4 -1
- package/dist/web-save.d.ts +3 -0
- package/dist/web-save.js +17 -2
- package/dist/workspace/archil-exec.js +1 -1
- package/dist/workspace/archil.js +1 -1
- package/dist/workspace/computer-for-claim.d.ts +2 -0
- package/dist/workspace/computer-for-claim.js +43 -0
- package/dist/workspace/paths.d.ts +6 -0
- package/dist/workspace/paths.js +6 -0
- package/dist/workspace/resolve-computer.d.ts +1 -0
- package/dist/workspace/revision-skills.js +24 -11
- package/dist/workspace/run-tools.d.ts +2 -0
- package/dist/workspace/run-tools.js +8 -0
- package/dist/workspace/synced-folder.d.ts +3 -3
- package/dist/workspace/synced-folder.js +7 -6
- package/launcher/agentrun-serve +1 -1
- package/package.json +10 -10
- package/skills/agentrun-serve/SKILL.md +2 -1
- package/templates/README.md +5 -2
- package/dist/console/assets/index-Bt1B51b7.js +0 -733
- package/dist/console/assets/index-sVftE9Ht.css +0 -1
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/** Explicit same-machine export consent. A loopback URL alone is not proof: it can forward to another host.
|
|
2
|
+
* Only newly granted rows are scanned; native/capture enrollment without this consent retains its old behavior. */
|
|
3
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
4
|
+
import { realpath, stat } from "node:fs/promises";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import { isAbsolute } from "node:path";
|
|
7
|
+
import { getAgent } from "../agents-store.js";
|
|
8
|
+
import { ApiError } from "../errors.js";
|
|
9
|
+
import { nativeOf } from "../native/binding.js";
|
|
10
|
+
import { selectedNative } from "../native/connection.js";
|
|
11
|
+
import { nativeIdentity, nativeMachineId } from "../native/discovery.js";
|
|
12
|
+
import { nativeEnvironment } from "../native/environment.js";
|
|
13
|
+
import { NATIVE_RUNTIME_FACTS } from "../native/runtimes.js";
|
|
14
|
+
import { sourceNativeEnvironmentFacts } from "../native/source-runtime-tools.js";
|
|
15
|
+
import { exportSourceSnapshot, SourceSnapshotError } from "../native/source-snapshot.js";
|
|
16
|
+
import { runnerSecrets } from "../runner/redact.js";
|
|
17
|
+
import { canonical, cliLearningOf, isObject } from "../store/agent-body.js";
|
|
18
|
+
import { SourceSync, sourceKey } from "./registry.js";
|
|
19
|
+
import { sourceSyncOf } from "./types.js";
|
|
20
|
+
import { SourcePreparationError } from "./prepare.js";
|
|
21
|
+
export const sourceLocalKey = (agentId) => `source-sync.local.${agentId}`;
|
|
22
|
+
const digest = (value) => createHash("sha256").update(canonical(value)).digest("hex");
|
|
23
|
+
const denied = (message = "Enable sync from this agent's original source machine and user profile.") => new ApiError("forbidden", message);
|
|
24
|
+
const changed = () => new ApiError("precondition_failed", "The local source connection changed. Enable sync again from its source.");
|
|
25
|
+
const identity = (target) => ({ runtime: target.runtime, machine_id: target.machine_id, project: target.project, selector: target.selector });
|
|
26
|
+
const consentOf = (value) => isObject(value) && Object.keys(value).every(key => ["version", "scope"].includes(key)) && value.version === 1 && typeof value.scope === "string" && /^[a-f0-9]{64}$/.test(value.scope);
|
|
27
|
+
async function folders(target, env) {
|
|
28
|
+
if (process.platform !== "linux" || !process.geteuid || target.machine_id !== await nativeMachineId())
|
|
29
|
+
throw denied();
|
|
30
|
+
const home = env.HOME || homedir();
|
|
31
|
+
const explicit = NATIVE_RUNTIME_FACTS[target.runtime].protocol === "codex-json" ? env.CODEX_HOME : env.CLAUDE_CONFIG_DIR;
|
|
32
|
+
const paths = [target.project, home, ...(explicit ? [explicit] : [])];
|
|
33
|
+
if (paths.some(path => !isAbsolute(path)))
|
|
34
|
+
throw denied();
|
|
35
|
+
const held = [];
|
|
36
|
+
for (const path of paths) {
|
|
37
|
+
try {
|
|
38
|
+
const canonicalPath = await realpath(path), information = await stat(canonicalPath, { bigint: true });
|
|
39
|
+
if (!information.isDirectory())
|
|
40
|
+
throw denied();
|
|
41
|
+
held.push({ path: canonicalPath, device: information.dev.toString(), inode: information.ino.toString() });
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
throw denied("The approved source project or native profile folder is unavailable. Reconnect it on the source machine.");
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const source = { runtime: target.runtime, machine_id: target.machine_id, project: held[0].path,
|
|
48
|
+
selector: { kind: target.selector.kind, ...(target.selector.name === undefined ? {} : { name: target.selector.name }) } };
|
|
49
|
+
const scope = digest({ version: 1, source, uid: process.geteuid(), project: held[0], home: held[1], explicit: held[2] ?? null,
|
|
50
|
+
environment: sourceNativeEnvironmentFacts(env) });
|
|
51
|
+
return { consent: { version: 1, scope }, home: held[1].path, explicit: held[2]?.path,
|
|
52
|
+
roots: [...new Set(held.map(item => item.path))], expectedRoots: [...new Map(held.map(item => [item.path, item])).values()] };
|
|
53
|
+
}
|
|
54
|
+
/** The wire carries only this digest, never device/inode/user/profile metadata. Configuration contents are not in
|
|
55
|
+
* the scope: editing those contents is the authorized input to sync. Replacing a folder or changing user is not. */
|
|
56
|
+
export async function sourceLocalConsent(target, env = nativeEnvironment()) {
|
|
57
|
+
return (await folders(target, env)).consent;
|
|
58
|
+
}
|
|
59
|
+
export class LocalSources {
|
|
60
|
+
store;
|
|
61
|
+
options;
|
|
62
|
+
constructor(store, options = {}) {
|
|
63
|
+
this.store = store;
|
|
64
|
+
this.options = options;
|
|
65
|
+
}
|
|
66
|
+
async capture(agentId, row, freshConsent = false) {
|
|
67
|
+
const entry = await getAgent(this.store, agentId);
|
|
68
|
+
if (!entry?.revision || entry.agent.archivedAt || entry.agent.purgedAt || entry.agent.externalRef)
|
|
69
|
+
throw denied();
|
|
70
|
+
const moved = sourceSyncOf(entry.revision.body), source = await this.store.settings.get(sourceKey(agentId));
|
|
71
|
+
if (source?.connection)
|
|
72
|
+
throw denied("This agent already syncs through its source connector.");
|
|
73
|
+
if (moved && (!row || source?.id !== moved.id || source.capture_revision !== row.capture_revision))
|
|
74
|
+
throw denied();
|
|
75
|
+
const revision = moved ? await this.store.agents.revision(row.capture_revision) : entry.revision;
|
|
76
|
+
if (!revision || revision.agentId !== agentId)
|
|
77
|
+
throw denied();
|
|
78
|
+
const cli = cliLearningOf(revision.body), native = nativeOf(revision.body);
|
|
79
|
+
if (cli) {
|
|
80
|
+
const connection = await this.store.settings.get(`cli-learning-connection:${cli.connection_id}`);
|
|
81
|
+
if (!connection?.active || connection.agent_id !== agentId || connection.runtime !== cli.runtime || connection.project !== cli.project
|
|
82
|
+
|| connection.native.remote || connection.native.selector.kind !== "project" || connection.native.runtime !== cli.runtime || connection.native.project !== cli.project)
|
|
83
|
+
throw denied();
|
|
84
|
+
if (row && (row.cli?.connection_id !== connection.connection_id || !freshConsent && row.cli.generation !== connection.generation))
|
|
85
|
+
throw denied();
|
|
86
|
+
return { target: connection.native, revision: revision.id, cli: { connection_id: connection.connection_id, generation: connection.generation },
|
|
87
|
+
guards: [{ key: `cli-learning-connection:${cli.connection_id}`, fields: { active: true, generation: connection.generation, agent_id: agentId } }] };
|
|
88
|
+
}
|
|
89
|
+
if (!native?.connected || native.remote || row?.cli)
|
|
90
|
+
throw denied();
|
|
91
|
+
return { target: native, revision: revision.id, guards: [] };
|
|
92
|
+
}
|
|
93
|
+
/** Called after explicit CLI consent, before SourceSync.grant. An owner-disabled source record stays disabled;
|
|
94
|
+
* this proves export scope, not permission to undo the owner's pause or move execution back to the source. */
|
|
95
|
+
async grant(agentId, consent) {
|
|
96
|
+
if (!consentOf(consent))
|
|
97
|
+
throw denied("This connection did not grant configuration and project sync from its source.");
|
|
98
|
+
const previous = await this.store.settings.get(sourceLocalKey(agentId));
|
|
99
|
+
const capture = await this.capture(agentId, previous, true), env = { ...(this.options.env?.() ?? nativeEnvironment()) };
|
|
100
|
+
// Check the scope before discovery can inspect source config, then repeat over its independently found target.
|
|
101
|
+
if ((await sourceLocalConsent(identity(capture.target), env)).scope !== consent.scope)
|
|
102
|
+
throw denied();
|
|
103
|
+
const target = await (this.options.selected ?? selectedNative)(identity(capture.target), env);
|
|
104
|
+
if (nativeIdentity(target) !== nativeIdentity(capture.target) || (await sourceLocalConsent(identity(target), env)).scope !== consent.scope)
|
|
105
|
+
throw denied();
|
|
106
|
+
const candidate = { version: 1, agent_id: agentId, active: true, generation: randomBytes(24).toString("base64url"), scope: consent.scope,
|
|
107
|
+
source: identity(target), capture_revision: capture.revision, ...(capture.cli ? { cli: capture.cli } : {}) };
|
|
108
|
+
if (previous && canonical(previous.source) !== canonical(candidate.source))
|
|
109
|
+
throw denied();
|
|
110
|
+
if (previous?.active && previous.scope === candidate.scope && canonical(previous.cli ?? null) === canonical(candidate.cli ?? null))
|
|
111
|
+
return this.authorize(agentId);
|
|
112
|
+
if (previous) {
|
|
113
|
+
if (!await this.store.settings.replaceIf(sourceLocalKey(agentId), previous, candidate, undefined, capture.guards))
|
|
114
|
+
throw changed();
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
const saved = await this.store.settings.putIfAbsent(sourceLocalKey(agentId), candidate);
|
|
118
|
+
if (saved.generation !== candidate.generation)
|
|
119
|
+
throw changed();
|
|
120
|
+
}
|
|
121
|
+
return this.authorize(agentId);
|
|
122
|
+
}
|
|
123
|
+
async checked(agentId) {
|
|
124
|
+
const row = await this.store.settings.get(sourceLocalKey(agentId));
|
|
125
|
+
if (!row?.active || row.version !== 1 || row.agent_id !== agentId)
|
|
126
|
+
throw denied();
|
|
127
|
+
const capture = await this.capture(agentId, row), env = { ...(this.options.env?.() ?? nativeEnvironment()) };
|
|
128
|
+
if (nativeIdentity(capture.target) !== nativeIdentity(row.source))
|
|
129
|
+
throw denied();
|
|
130
|
+
const location = await folders(row.source, env);
|
|
131
|
+
if (location.consent.scope !== row.scope)
|
|
132
|
+
throw denied();
|
|
133
|
+
const target = await (this.options.selected ?? selectedNative)(row.source, env);
|
|
134
|
+
if (nativeIdentity(target) !== nativeIdentity(row.source) || (await sourceLocalConsent(identity(target), env)).scope !== row.scope)
|
|
135
|
+
throw denied();
|
|
136
|
+
const authority = { source: row.source, captureRevision: row.capture_revision,
|
|
137
|
+
guards: [{ key: sourceLocalKey(agentId), fields: { active: true, generation: row.generation, scope: row.scope } }, ...capture.guards] };
|
|
138
|
+
return { row, target, env, location, authority };
|
|
139
|
+
}
|
|
140
|
+
async authorize(agentId) { return (await this.checked(agentId)).authority; }
|
|
141
|
+
/** Native disconnect calls this before changing its binding. CLI disconnect also fences through active/generation.
|
|
142
|
+
* It does not edit a managed revision, kill a run or delete any captured snapshot. */
|
|
143
|
+
async revoke(agentId) {
|
|
144
|
+
for (let n = 0; n < 8; n++) {
|
|
145
|
+
const key = sourceLocalKey(agentId), row = await this.store.settings.get(key);
|
|
146
|
+
if (!row?.active)
|
|
147
|
+
return;
|
|
148
|
+
if (await this.store.settings.replaceIf(key, row, { ...row, active: false, generation: randomBytes(24).toString("base64url") }))
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
throw changed();
|
|
152
|
+
}
|
|
153
|
+
publisher(sync, options = {}) {
|
|
154
|
+
const limit = options.limit ?? 4;
|
|
155
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 32)
|
|
156
|
+
throw new TypeError("local source batch limit must be between 1 and 32");
|
|
157
|
+
let cursor = "";
|
|
158
|
+
return { tick: async (signal) => {
|
|
159
|
+
const rows = (await this.store.settings.all({ prefix: "source-sync.local." })).filter(item => item.value.active).sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
160
|
+
const ordered = [...rows.filter(row => row.key > cursor), ...rows.filter(row => row.key <= cursor)].slice(0, limit);
|
|
161
|
+
const result = { published: 0, unchanged: 0, refused: 0, failed: 0 };
|
|
162
|
+
for (const item of ordered) {
|
|
163
|
+
signal?.throwIfAborted();
|
|
164
|
+
cursor = item.key;
|
|
165
|
+
const agentId = item.value.agent_id, record = await sync.get(agentId);
|
|
166
|
+
if (!record?.enabled || record.connection) {
|
|
167
|
+
result.refused++;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
let checked;
|
|
171
|
+
try {
|
|
172
|
+
checked = await this.checked(agentId);
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
if (error instanceof ApiError && error.status < 500) {
|
|
176
|
+
result.refused++;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
const scoped = new SourceSync(this.store, { ...sync.options, authorize: async (id) => {
|
|
182
|
+
const now = await this.checked(id);
|
|
183
|
+
if (id !== agentId || now.row.generation !== checked.row.generation)
|
|
184
|
+
throw changed();
|
|
185
|
+
return now.authority;
|
|
186
|
+
} });
|
|
187
|
+
const write = (call) => options.agentWrite ? options.agentWrite(agentId, call) : call();
|
|
188
|
+
const reconcile = async () => {
|
|
189
|
+
try {
|
|
190
|
+
await scoped.reconcile(agentId);
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
if (!(error instanceof SourcePreparationError) && !(error instanceof ApiError && error.status < 500))
|
|
194
|
+
throw error;
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
const secrets = this.options.secrets?.() ?? runnerSecrets(checked.env, runnerSecrets(process.env));
|
|
198
|
+
try {
|
|
199
|
+
const snapshot = await (this.options.export ?? exportSourceSnapshot)({ target: checked.target, home: checked.location.home, environment: checked.env,
|
|
200
|
+
authorizedRoots: checked.location.roots, expectedRoots: checked.location.expectedRoots,
|
|
201
|
+
...(checked.location.explicit ? (NATIVE_RUNTIME_FACTS[checked.target.runtime].protocol === "codex-json" ? { codexHome: checked.location.explicit } : { claudeHome: checked.location.explicit }) : {}),
|
|
202
|
+
secretValues: secrets, ...(signal ? { signal } : {}) });
|
|
203
|
+
signal?.throwIfAborted();
|
|
204
|
+
if (snapshot.digest === record.attempt?.digest && !record.attempt.code) {
|
|
205
|
+
if (snapshot.complete && !snapshot.blockers.length)
|
|
206
|
+
await write(reconcile);
|
|
207
|
+
result.unchanged++;
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
await write(async () => {
|
|
211
|
+
await scoped.publish(agentId, { generation: record.generation, sequence: record.sequence + 1, snapshot });
|
|
212
|
+
if (snapshot.complete && !snapshot.blockers.length)
|
|
213
|
+
await reconcile();
|
|
214
|
+
});
|
|
215
|
+
result.published++;
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
signal?.throwIfAborted();
|
|
219
|
+
if (!(error instanceof SourceSnapshotError))
|
|
220
|
+
throw error;
|
|
221
|
+
const code = error.code === "limit" ? "snapshot_too_large" : error.code;
|
|
222
|
+
await write(() => scoped.failed(agentId, { generation: record.generation, sequence: record.sequence + 1, code }));
|
|
223
|
+
result.failed++;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return result;
|
|
227
|
+
} };
|
|
228
|
+
}
|
|
229
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { InputIssue } from "../errors.js";
|
|
2
|
+
import type { SourceSnapshot, SourceSnapshotBlocker, SourceSnapshotTool } from "../native/source-snapshot-types.js";
|
|
3
|
+
import { type AgenticBody } from "../store/agent-body.js";
|
|
4
|
+
import type { NewRevision, SettingsGuard, McpGuard } from "../store/store.js";
|
|
5
|
+
import { type ToolSettingsMap } from "../tool-plane.js";
|
|
6
|
+
import type { SourcePreparation } from "./registry.js";
|
|
7
|
+
export declare class SourcePreparationError extends Error {
|
|
8
|
+
readonly blockers: readonly SourceSnapshotBlocker[];
|
|
9
|
+
constructor(blockers: readonly SourceSnapshotBlocker[]);
|
|
10
|
+
}
|
|
11
|
+
type Refused = {
|
|
12
|
+
readonly blockers: readonly SourceSnapshotBlocker[];
|
|
13
|
+
};
|
|
14
|
+
type Guarded = {
|
|
15
|
+
readonly guards?: readonly SettingsGuard[];
|
|
16
|
+
readonly mcpGuards?: readonly McpGuard[];
|
|
17
|
+
};
|
|
18
|
+
type ResolvedModel = Guarded & {
|
|
19
|
+
readonly sourceModel: string | null;
|
|
20
|
+
readonly model: string;
|
|
21
|
+
readonly harness: string;
|
|
22
|
+
readonly thinking?: string;
|
|
23
|
+
};
|
|
24
|
+
type ResolvedTool = Guarded & {
|
|
25
|
+
readonly source: SourceSnapshotTool;
|
|
26
|
+
readonly tools: readonly string[];
|
|
27
|
+
};
|
|
28
|
+
type ResolvedPolicy = Guarded & Pick<AgenticBody, "approvals" | "place" | "computer"> & {
|
|
29
|
+
readonly toolSettings?: ToolSettingsMap;
|
|
30
|
+
/** The complete native policy handled by this mapping, not just its supported subset. */
|
|
31
|
+
readonly source: Readonly<Record<string, unknown>>;
|
|
32
|
+
/** Required built-in tools, including the executor the source needs. No harness filter may silently remove one. */
|
|
33
|
+
readonly tools: readonly string[];
|
|
34
|
+
};
|
|
35
|
+
export interface SourcePreparationDeps {
|
|
36
|
+
/** Resolve an actual model route and its independently configured destination authentication. A native default or
|
|
37
|
+
* alias must be resolved from authoritative source/destination configuration, never guessed from its spelling. */
|
|
38
|
+
model(snapshot: SourceSnapshot): Promise<ResolvedModel | Refused>;
|
|
39
|
+
/** Validate the complete capability and account identity against the authenticated destination tool registry.
|
|
40
|
+
* Matching a source name or endpoint alone is insufficient. Credential generations become cutover guards. */
|
|
41
|
+
tool(requirement: SourceSnapshotTool, snapshot: SourceSnapshot): Promise<ResolvedTool | Refused>;
|
|
42
|
+
/** Translate all native restrictions, or refuse. For example, a native `never` approval policy does not establish
|
|
43
|
+
* equivalence to the managed `never` setting, which automatically approves requests. */
|
|
44
|
+
policy(snapshot: SourceSnapshot, externalTools: readonly string[]): Promise<ResolvedPolicy | Refused>;
|
|
45
|
+
/** Check destination secret setup for every named source requirement. No native secret value is copied. */
|
|
46
|
+
credentials(names: readonly string[], snapshot: SourceSnapshot): Promise<Guarded & {
|
|
47
|
+
readonly resolved: readonly string[];
|
|
48
|
+
} | Refused>;
|
|
49
|
+
/** The server's ordinary saveable/runnable check, over the final definition. Unlike a normal save, Move rejects
|
|
50
|
+
* host warnings too. Pins come from that real check, never the native manifest. */
|
|
51
|
+
check(revision: NewRevision): Promise<Guarded & {
|
|
52
|
+
readonly warnings: readonly InputIssue[];
|
|
53
|
+
readonly stamp: Pick<NewRevision, "mcpPin" | "monidPin">;
|
|
54
|
+
}>;
|
|
55
|
+
}
|
|
56
|
+
/** A managed definition from source-owned configuration. Snapshot asset bytes remain pinned for project seeding;
|
|
57
|
+
* the revision indexes authored skill metadata without decoding binary assets or replacing their executable modes. */
|
|
58
|
+
export declare function prepareSource(snapshot: SourceSnapshot, deps: SourcePreparationDeps): Promise<SourcePreparation>;
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Translate a complete source through the server's actual destination registry. Names, URLs and native permission
|
|
2
|
+
// spellings alone are not authority to grant tools, select an account or weaken a policy.
|
|
3
|
+
import { uuidv7 } from "../ids.js";
|
|
4
|
+
import { bodyOf, canonical } from "../store/agent-body.js";
|
|
5
|
+
import { canonicalToolSettings, settingsIssues } from "../tool-plane.js";
|
|
6
|
+
import { validateSourceSnapshot } from "./snapshots.js";
|
|
7
|
+
export class SourcePreparationError extends Error {
|
|
8
|
+
blockers;
|
|
9
|
+
constructor(blockers) {
|
|
10
|
+
super(blockers.map(item => item.message).join(" "));
|
|
11
|
+
this.blockers = blockers;
|
|
12
|
+
this.name = "SourcePreparationError";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function refuse(code, message, path) { throw new SourcePreparationError([{ code, ...(path ? { path } : {}), message }]); }
|
|
16
|
+
function resolved(value) {
|
|
17
|
+
if ("blockers" in value) {
|
|
18
|
+
if (!value.blockers.length)
|
|
19
|
+
refuse("destination_mapping", "The destination did not supply a complete mapping.");
|
|
20
|
+
throw new SourcePreparationError(value.blockers);
|
|
21
|
+
}
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
const same = (a, b) => canonical(a) === canonical(b);
|
|
25
|
+
const names = (value) => Array.isArray(value) && value.every(name => typeof name === "string" && name.trim().length > 0) && new Set(value).size === value.length;
|
|
26
|
+
/** A managed definition from source-owned configuration. Snapshot asset bytes remain pinned for project seeding;
|
|
27
|
+
* the revision indexes authored skill metadata without decoding binary assets or replacing their executable modes. */
|
|
28
|
+
export async function prepareSource(snapshot, deps) {
|
|
29
|
+
validateSourceSnapshot(snapshot);
|
|
30
|
+
if (!snapshot.complete || snapshot.blockers.length)
|
|
31
|
+
throw new SourcePreparationError(snapshot.blockers.length ? snapshot.blockers : [{ code: "incomplete", message: "Sync a complete source setup before moving it." }]);
|
|
32
|
+
if (!snapshot.configuration.model?.trim())
|
|
33
|
+
refuse("model_mapping", "Sync the source's effective model before selecting its destination route.", "configuration.model");
|
|
34
|
+
const guards = [], mcpGuards = [];
|
|
35
|
+
const keep = (value) => { guards.push(...(value.guards ?? [])); mcpGuards.push(...(value.mcpGuards ?? [])); };
|
|
36
|
+
const model = resolved(await deps.model(snapshot));
|
|
37
|
+
if (model.sourceModel !== snapshot.configuration.model || typeof model.model !== "string" || !model.model.trim() || typeof model.harness !== "string" || !model.harness.trim()) {
|
|
38
|
+
refuse("model_mapping", "Resolve this source's actual model and destination harness before moving it.", "configuration.model");
|
|
39
|
+
}
|
|
40
|
+
keep(model);
|
|
41
|
+
const external = [];
|
|
42
|
+
for (const requirement of snapshot.configuration.tools) {
|
|
43
|
+
const tool = resolved(await deps.tool(requirement, snapshot));
|
|
44
|
+
if (!same(tool.source, requirement) || !names(tool.tools) || !tool.guards?.length) {
|
|
45
|
+
refuse("tool_identity", `Verify the destination capabilities and account for source tool ${JSON.stringify(requirement.name)}.`, "configuration.tools");
|
|
46
|
+
}
|
|
47
|
+
external.push(...tool.tools);
|
|
48
|
+
keep(tool);
|
|
49
|
+
}
|
|
50
|
+
const policy = resolved(await deps.policy(snapshot, external));
|
|
51
|
+
if (!same(policy.source, snapshot.configuration.permissions) || !names(policy.tools)) {
|
|
52
|
+
refuse("policy_mapping", "Map every source permission and required built-in tool before moving this agent.", "configuration.permissions");
|
|
53
|
+
}
|
|
54
|
+
keep(policy);
|
|
55
|
+
const required = [...new Set([...snapshot.configuration.credentials, ...snapshot.configuration.tools.flatMap(tool => tool.credentials)])];
|
|
56
|
+
const credentials = resolved(await deps.credentials(required, snapshot));
|
|
57
|
+
if (!names(credentials.resolved) || required.some(name => !credentials.resolved.includes(name))) {
|
|
58
|
+
refuse("credentials_missing", "Configure the destination credentials required by this source's model and tools.", "configuration.credentials");
|
|
59
|
+
}
|
|
60
|
+
keep(credentials);
|
|
61
|
+
const skills = Object.create(null);
|
|
62
|
+
const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
63
|
+
for (const skill of snapshot.skills) {
|
|
64
|
+
const entry = snapshot.entries.find(item => item.path === `${skill.path}/SKILL.md` && item.type === "file");
|
|
65
|
+
if (!entry?.sha256)
|
|
66
|
+
refuse("skill_missing", `Sync the complete authored skill ${JSON.stringify(skill.name)}.`, skill.path);
|
|
67
|
+
try {
|
|
68
|
+
skills[`${skill.name}/SKILL.md`] = decoder.decode(Buffer.from(snapshot.blobs[entry.sha256], "base64"));
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
refuse("skill_encoding", `The authored skill ${JSON.stringify(skill.name)} needs a UTF-8 SKILL.md.`, `${skill.path}/SKILL.md`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (policy.toolSettings) {
|
|
75
|
+
const issues = settingsIssues(policy.toolSettings, "definition.tool_settings", "agent");
|
|
76
|
+
if (issues.length)
|
|
77
|
+
throw new SourcePreparationError(issues.map(issue => ({ code: issue.reason, path: issue.field, message: issue.message })));
|
|
78
|
+
}
|
|
79
|
+
const made = bodyOf({ kind: "agentic", instructions: snapshot.instructions.text, skills,
|
|
80
|
+
tools: [...new Set([...policy.tools, ...external])], model: model.model, harness: model.harness,
|
|
81
|
+
...(model.thinking === undefined ? {} : { thinking: model.thinking }),
|
|
82
|
+
...(policy.toolSettings === undefined ? {} : { toolSettings: canonicalToolSettings(policy.toolSettings) }),
|
|
83
|
+
...(policy.approvals === undefined ? {} : { approvals: policy.approvals }),
|
|
84
|
+
...(policy.place === undefined ? {} : { place: policy.place }),
|
|
85
|
+
...(policy.computer === undefined ? {} : { computer: policy.computer }),
|
|
86
|
+
});
|
|
87
|
+
const revision = { id: uuidv7(), kind: "agentic", body: made.body, skills: made.skills };
|
|
88
|
+
const checked = await deps.check(revision);
|
|
89
|
+
if (checked.warnings.length)
|
|
90
|
+
throw new SourcePreparationError(checked.warnings.map(issue => ({ code: issue.reason, path: issue.field, message: issue.message })));
|
|
91
|
+
keep(checked);
|
|
92
|
+
return { revision: { ...revision, ...checked.stamp }, guards, mcpGuards };
|
|
93
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { NewRevision } from "../store/store.js";
|
|
2
|
+
import type { LaunchDisk, RunLaunch, SourceProjectLaunch } from "../run-launch.js";
|
|
3
|
+
import type { Workspace } from "../workspace/workspace.js";
|
|
4
|
+
import { type SeedPreparation } from "./snapshots.js";
|
|
5
|
+
import type { SourceIOReceipt } from "./io.js";
|
|
6
|
+
export interface ProjectDestination {
|
|
7
|
+
readonly workspace: Workspace;
|
|
8
|
+
/** Private control, outside every workspace exposed to the run's tools. */
|
|
9
|
+
readonly control: Workspace;
|
|
10
|
+
readonly launchDisk?: LaunchDisk;
|
|
11
|
+
readonly preparation: SeedPreparation;
|
|
12
|
+
/** Release preparation resources; no claim/settling proof. undo remains usable afterward. */
|
|
13
|
+
end(): Promise<void>;
|
|
14
|
+
/** Only this exclusive unadmitted run; wait for pending writes and refuse an admitted run. */
|
|
15
|
+
undo(): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
export interface ProjectPlacementOptions {
|
|
18
|
+
hold(input: {
|
|
19
|
+
agentId: string;
|
|
20
|
+
runId: string;
|
|
21
|
+
}): Promise<SourceIOReceipt>;
|
|
22
|
+
/** Existing-only readonly cache: opening/closing it must never allocate storage or dispatch a write. */
|
|
23
|
+
snapshots(agentId: string): Promise<{
|
|
24
|
+
workspace: Workspace;
|
|
25
|
+
end(): Promise<void>;
|
|
26
|
+
}>;
|
|
27
|
+
destination(input: {
|
|
28
|
+
agentId: string;
|
|
29
|
+
runId: string;
|
|
30
|
+
launch: RunLaunch;
|
|
31
|
+
}): Promise<ProjectDestination>;
|
|
32
|
+
/** Copy already held local uploads before project seeding. Await every copy and hold cleanup; a failure has an
|
|
33
|
+
* unknown write outcome and keeps ownership. Runs whose uploads were placed earlier do not call this capability. */
|
|
34
|
+
placeUploads?(input: {
|
|
35
|
+
runId: string;
|
|
36
|
+
launch: RunLaunch;
|
|
37
|
+
workspace: Workspace;
|
|
38
|
+
}): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
/** Verify a private marker only. Deliberately never verify/reset mutable project files from the original snapshot:
|
|
41
|
+
* later claims and follow-ups must keep the run's own edits, additions and deletions. */
|
|
42
|
+
export declare function verifySourceProject(control: Workspace, placed: SourceProjectLaunch): Promise<void>;
|
|
43
|
+
/** The StartDeps.project adapter. It resolves exact immutable source bytes before opening a destination, restores
|
|
44
|
+
* through that destination's explicit capabilities, and returns only after its private completion marker is durable. */
|
|
45
|
+
export declare function projectPlacement(options: ProjectPlacementOptions): {
|
|
46
|
+
hold: (input: {
|
|
47
|
+
agentId: string;
|
|
48
|
+
runId: string;
|
|
49
|
+
}) => Promise<SourceIOReceipt>;
|
|
50
|
+
place({ agentId, runId, revision, launch, hold }: {
|
|
51
|
+
hold?: SourceIOReceipt;
|
|
52
|
+
agentId: string;
|
|
53
|
+
runId: string;
|
|
54
|
+
revision: Pick<NewRevision, "id" | "kind" | "body">;
|
|
55
|
+
launch: RunLaunch;
|
|
56
|
+
}): Promise<{
|
|
57
|
+
launch: RunLaunch;
|
|
58
|
+
undo(): Promise<void>;
|
|
59
|
+
admitted(): Promise<void>;
|
|
60
|
+
} | undefined>;
|
|
61
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/** Pre-admission project placement over injected private workspaces. No claim calls this function: a claim verifies
|
|
2
|
+
* its completed receipt and leaves the run's current files alone. Checkpoint capture/publication is a separate step. */
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import { readSourceSnapshot, seedSourceSnapshot, SnapshotError } from "./snapshots.js";
|
|
5
|
+
import { sourceSyncOf } from "./types.js";
|
|
6
|
+
const hash = (data) => createHash("sha256").update(data).digest("hex");
|
|
7
|
+
async function completeReceipt(control, digest, inputs) {
|
|
8
|
+
const stat = await control.stat("source-seed.json");
|
|
9
|
+
if (!stat || stat.kind !== "file" || stat.size > 128 * 1024)
|
|
10
|
+
throw new SnapshotError("unsafe_resume", "The project's private completion receipt is missing or invalid.");
|
|
11
|
+
const bytes = await control.read("source-seed.json", { length: 128 * 1024 + 1 });
|
|
12
|
+
if (bytes.length !== stat.size || bytes.length > 128 * 1024)
|
|
13
|
+
throw new SnapshotError("unsafe_resume", "The project's completion receipt changed while it was read.");
|
|
14
|
+
let marker;
|
|
15
|
+
try {
|
|
16
|
+
marker = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
throw new SnapshotError("unsafe_resume", "The project's completion receipt is invalid.");
|
|
20
|
+
}
|
|
21
|
+
if (!marker || typeof marker !== "object" || Array.isArray(marker) || Object.keys(marker).length !== (inputs === undefined ? 3 : 4)
|
|
22
|
+
|| marker.version !== 1 || marker.digest !== digest
|
|
23
|
+
|| marker.phase !== "complete" || JSON.stringify(marker.inputs) !== JSON.stringify(inputs))
|
|
24
|
+
throw new SnapshotError("unsafe_resume", "The project's completion receipt does not match its pinned snapshot.");
|
|
25
|
+
return hash(bytes);
|
|
26
|
+
}
|
|
27
|
+
/** Verify a private marker only. Deliberately never verify/reset mutable project files from the original snapshot:
|
|
28
|
+
* later claims and follow-ups must keep the run's own edits, additions and deletions. */
|
|
29
|
+
export async function verifySourceProject(control, placed) {
|
|
30
|
+
if (await completeReceipt(control, placed.digest, placed.inputs) !== placed.receipt)
|
|
31
|
+
throw new SnapshotError("unsafe_resume", "The project's private completion receipt does not match its launch.");
|
|
32
|
+
}
|
|
33
|
+
async function placedInputs(work) {
|
|
34
|
+
if (!await work.stat("inputs"))
|
|
35
|
+
return [];
|
|
36
|
+
const files = [];
|
|
37
|
+
let visited = 0;
|
|
38
|
+
const visit = async (path) => {
|
|
39
|
+
if (++visited > 1024 || path.length > 4096)
|
|
40
|
+
throw new SnapshotError("limit", "Placed upload paths exceed the project limit.");
|
|
41
|
+
const entry = await work.stat(path);
|
|
42
|
+
if (entry?.kind === "file")
|
|
43
|
+
files.push(path);
|
|
44
|
+
else if (entry?.kind === "dir")
|
|
45
|
+
for (const child of await work.list(path))
|
|
46
|
+
await visit(`${path}/${child.name}`);
|
|
47
|
+
else
|
|
48
|
+
throw new SnapshotError("conflict", "Placed uploads must contain only regular files and directories.");
|
|
49
|
+
};
|
|
50
|
+
await visit("inputs");
|
|
51
|
+
if (files.length > 20 || files.includes("inputs/message.md"))
|
|
52
|
+
throw new SnapshotError("conflict", "Placed uploads conflict with the run's reserved message path or upload limit.");
|
|
53
|
+
return files.sort();
|
|
54
|
+
}
|
|
55
|
+
/** The StartDeps.project adapter. It resolves exact immutable source bytes before opening a destination, restores
|
|
56
|
+
* through that destination's explicit capabilities, and returns only after its private completion marker is durable. */
|
|
57
|
+
export function projectPlacement(options) {
|
|
58
|
+
return {
|
|
59
|
+
hold: options.hold,
|
|
60
|
+
async place({ agentId, runId, revision, launch, hold }) {
|
|
61
|
+
const source = sourceSyncOf(revision.body);
|
|
62
|
+
if (!source)
|
|
63
|
+
return undefined;
|
|
64
|
+
const ownership = hold ?? await options.hold({ agentId, runId });
|
|
65
|
+
const stored = await options.snapshots(agentId).catch(async (error) => {
|
|
66
|
+
// This opener is explicitly read-only. No destination allocation or write has been dispatched.
|
|
67
|
+
await ownership.settled();
|
|
68
|
+
throw error;
|
|
69
|
+
});
|
|
70
|
+
let destination, destinationStarted = false, uploadUncertain = false, seedStarted = false, preparationRolledBack = false;
|
|
71
|
+
try {
|
|
72
|
+
// Validate the whole source before destination allocation. seed reads it again while initializing; a corrupt
|
|
73
|
+
// immutable store is refused rather than producing an admitted run with only some content.
|
|
74
|
+
const snapshot = await readSourceSnapshot(stored.workspace, source.project);
|
|
75
|
+
// These exact paths are owned by the run host: message placement and shell cleanup would otherwise replace or
|
|
76
|
+
// remove authored source bytes after the seed was marked complete. Unrelated inputs/.agentrun siblings remain.
|
|
77
|
+
const reserved = ["project/inputs/message.md", "project/.agentrun/tmp", "project/.agentrun/output"];
|
|
78
|
+
if (snapshot.entries.some(entry => entry.role === "project" && reserved.some(path => entry.path === path || entry.path.startsWith(`${path}/`))))
|
|
79
|
+
throw new SnapshotError("conflict", "The source project includes a path reserved for run inputs or shell scratch; move that content before starting this project.");
|
|
80
|
+
destinationStarted = true;
|
|
81
|
+
destination = await options.destination({ agentId, runId, launch });
|
|
82
|
+
let prepared = launch;
|
|
83
|
+
if (!launch.filesPlaced && launch.files?.length) {
|
|
84
|
+
if (!options.placeUploads)
|
|
85
|
+
throw new SnapshotError("prerequisite", "This moved project's uploads must be placed before project admission.");
|
|
86
|
+
uploadUncertain = true;
|
|
87
|
+
await options.placeUploads({ runId, launch, workspace: destination.workspace });
|
|
88
|
+
uploadUncertain = false;
|
|
89
|
+
prepared = { ...launch, filesPlaced: true };
|
|
90
|
+
}
|
|
91
|
+
const inputs = await placedInputs(destination.workspace);
|
|
92
|
+
if (snapshot.entries.some(entry => entry.role === "project" && inputs.some(input => entry.path === `project/${input}`)))
|
|
93
|
+
throw new SnapshotError("conflict", "A placed upload would replace an authored project file.");
|
|
94
|
+
seedStarted = true;
|
|
95
|
+
await seedSourceSnapshot({ snapshots: stored.workspace, destination: destination.workspace, control: destination.control,
|
|
96
|
+
digest: source.project, beforeDispatch: true, exclusiveInitialization: true, preparation: destination.preparation });
|
|
97
|
+
if (inputs.length)
|
|
98
|
+
await destination.control.write("source-seed.json", JSON.stringify({ version: 1, digest: source.project, phase: "complete", inputs }));
|
|
99
|
+
const receipt = await completeReceipt(destination.control, source.project, inputs.length ? inputs : undefined);
|
|
100
|
+
const placed = { version: 1, sourceId: source.id, baselineRevision: revision.id, digest: source.project, receipt, ...(inputs.length ? { inputs } : {}) };
|
|
101
|
+
const own = destination;
|
|
102
|
+
return { launch: { ...prepared, ...(own.launchDisk ? { archilDisk: own.launchDisk } : {}), sourceProject: placed }, admitted: () => ownership.settled(), undo: async () => {
|
|
103
|
+
await own.undo();
|
|
104
|
+
await ownership.settled();
|
|
105
|
+
} };
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
preparationRolledBack = !destinationStarted;
|
|
109
|
+
// Admission has not even been attempted. This preparation owns its rollback and waits for writes to settle.
|
|
110
|
+
// A completed destination open can still be refused by read-only upload checks. Only confirmed rollback of
|
|
111
|
+
// that pre-seed preparation is safe to release; an unanswered open or dispatched seed keeps its receipt.
|
|
112
|
+
if (destination)
|
|
113
|
+
try {
|
|
114
|
+
await destination.undo();
|
|
115
|
+
preparationRolledBack = !seedStarted && !uploadUncertain;
|
|
116
|
+
}
|
|
117
|
+
catch { /* an unknown rollback keeps the writer recorded */ }
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
const closed = await Promise.allSettled([destination?.end(), stored.end()]);
|
|
122
|
+
const failed = closed.find(result => result.status === "rejected");
|
|
123
|
+
if (failed?.status === "rejected")
|
|
124
|
+
throw failed.reason;
|
|
125
|
+
if (preparationRolledBack)
|
|
126
|
+
await ownership.settled();
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|