@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,365 @@
|
|
|
1
|
+
// fal's pricing, for the run's MCP gateway (mcp-gateway.ts McpMeter): a job sent to a fal MCP server (run_model,
|
|
2
|
+
// submit_job, each request of submit_jobs) is priced before it leaves, at fal's unit price for its endpoint times the
|
|
3
|
+
// units fal bills it by.
|
|
4
|
+
// - The unit price is the server's own get_pricing, asked with exactly `{ endpoint_id: "<id>" }` (a contract: a fake
|
|
5
|
+
// server matches arguments by equality), one endpoint a call, kept PRICE_TTL_MS.
|
|
6
|
+
// - The units are read from the job's input (unitsBefore). Nothing is guessed: an input that leaves out the size or
|
|
7
|
+
// length its model would default is unpriceable.
|
|
8
|
+
// - fal's reply states no cost, so the charge is this estimate (chargeOfJob).
|
|
9
|
+
// fal can bill what get_pricing does not expose (a minimum, a discount, a rate that depends on a setting such as
|
|
10
|
+
// resolution): a booked figure is fal's list price for the job, not its invoice.
|
|
11
|
+
// fal's hosted server also keeps one submission_key journal per fal account, which refuses a key it has seen with another
|
|
12
|
+
// request: a server at fal's own host scopes each key to the run before it leaves (falKeys).
|
|
13
|
+
import { createHash } from "node:crypto";
|
|
14
|
+
import { mediaSeconds } from "./media-duration.js";
|
|
15
|
+
import { SAVE_MAX_BYTES, SERVER_SLOTS } from "./web-save.js";
|
|
16
|
+
/** The host of fal's hosted MCP server (its key endpoint /mcp and its sign-in endpoint /mcp-relay). */
|
|
17
|
+
export const FAL_MCP_HOST = "mcp.fal.ai";
|
|
18
|
+
/** A pin's `meter` for a fal server. */
|
|
19
|
+
export const FAL_METER = "fal";
|
|
20
|
+
/** How long a unit price read from get_pricing is used. */
|
|
21
|
+
export const PRICE_TTL_MS = 5 * 60_000;
|
|
22
|
+
/** One deadline for reading an input media file's length. */
|
|
23
|
+
export const MEDIA_TIMEOUT_MS = 120_000;
|
|
24
|
+
/** Whether a saved server is fal's: its owner's toolkit for it is `fal`, or its URL is on fal's MCP host. */
|
|
25
|
+
export function falServer(url, toolkit) {
|
|
26
|
+
return toolkit === FAL_METER || URL.parse(url)?.hostname === FAL_MCP_HOST;
|
|
27
|
+
}
|
|
28
|
+
/** The `meter` a save pins for a server at `url` with `toolkit` (mcp-gateway.ts mcpPinOf). */
|
|
29
|
+
export const meterNamed = (url, toolkit) => (falServer(url, toolkit) ? FAL_METER : undefined);
|
|
30
|
+
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
31
|
+
const jobOf = (given) => {
|
|
32
|
+
const one = isObject(given) ? given : {};
|
|
33
|
+
return { endpoint: typeof one.endpoint_id === "string" && one.endpoint_id ? one.endpoint_id : undefined, input: one.input, ...(Number.isInteger(one.index) ? { index: one.index } : {}) };
|
|
34
|
+
};
|
|
35
|
+
/** The jobs a call of fal's `tool` submits: one for run_model and submit_job, one per request for submit_jobs. Undefined
|
|
36
|
+
* for a tool that submits none (get_pricing, wait_for_jobs, ...), which fal does not bill. */
|
|
37
|
+
export function falJobsOf(tool, args) {
|
|
38
|
+
if (tool === "run_model" || tool === "submit_job")
|
|
39
|
+
return [jobOf(args)];
|
|
40
|
+
if (tool !== "submit_jobs")
|
|
41
|
+
return undefined;
|
|
42
|
+
const requests = isObject(args) && Array.isArray(args.requests) ? args.requests : [];
|
|
43
|
+
return requests.length ? requests.map(jobOf) : [jobOf(undefined)];
|
|
44
|
+
}
|
|
45
|
+
/** fal's image_size presets, in pixels. */
|
|
46
|
+
const SIZES = { square_hd: [1024, 1024], square: [512, 512], portrait_4_3: [768, 1024], portrait_16_9: [576, 1024], landscape_4_3: [1024, 768], landscape_16_9: [1024, 576] };
|
|
47
|
+
/** A unit as one of it reads: "per image". */
|
|
48
|
+
const ONE = { images: "image", megapixels: "megapixel", seconds: "second", minutes: "minute", audios: "audio", "compute seconds": "compute second" };
|
|
49
|
+
const oneOf = (unit) => ONE[unit] ?? unit;
|
|
50
|
+
/** Units fal counts only once the job has run. */
|
|
51
|
+
const AFTER_ONLY = new Set(["compute seconds", "1m tokens"]);
|
|
52
|
+
const positive = (value) => typeof value === "number" && Number.isFinite(value) && value > 0;
|
|
53
|
+
/** An image's megapixels as billed: rounded up to a whole one. */
|
|
54
|
+
const megapixels = (width, height) => Math.ceil((width * height) / 1e6);
|
|
55
|
+
const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
56
|
+
/** How many outputs the input asks for (num_images, else num_outputs, else 1), or undefined for a count that is none. */
|
|
57
|
+
function countOf(input, unit) {
|
|
58
|
+
const field = input.num_images !== undefined ? "num_images" : input.num_outputs !== undefined ? "num_outputs" : undefined;
|
|
59
|
+
if (!field)
|
|
60
|
+
return { n: 1, from: `1 ${oneOf(unit === "megapixels" ? "images" : unit)} by default` };
|
|
61
|
+
const n = input[field];
|
|
62
|
+
return Number.isInteger(n) && n > 0 ? { n: n, from: field } : undefined;
|
|
63
|
+
}
|
|
64
|
+
/** The length in seconds the input names: `duration` ("5", "5s" or a number), duration_seconds, seconds_total,
|
|
65
|
+
* music_length_ms, or a music composition plan's sections. */
|
|
66
|
+
function namedSeconds(input) {
|
|
67
|
+
for (const [field, per] of [["duration", 1], ["duration_seconds", 1], ["seconds_total", 1], ["music_length_ms", 1000]]) {
|
|
68
|
+
const given = input[field];
|
|
69
|
+
if (given === undefined)
|
|
70
|
+
continue;
|
|
71
|
+
const said = typeof given === "string" && /^\d+(\.\d+)?s?$/.test(given.trim()) ? Number.parseFloat(given) : given;
|
|
72
|
+
return positive(said) ? { seconds: said / per, from: field } : "bad";
|
|
73
|
+
}
|
|
74
|
+
const sections = isObject(input.composition_plan) ? input.composition_plan.sections : undefined;
|
|
75
|
+
if (Array.isArray(sections) && sections.length) {
|
|
76
|
+
const each = sections.map((section) => (isObject(section) ? section.duration_ms : undefined));
|
|
77
|
+
return each.every(positive) ? { seconds: each.reduce((sum, ms) => sum + ms, 0) / 1000, from: "its composition plan's sections" } : "bad";
|
|
78
|
+
}
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
/** The endpoints fal bills per minute of media whose length the input's media bound (fal's model pages, read 2026-11-21):
|
|
82
|
+
* dubbing and voice isolation return the media they are given, re-voiced or cleaned; speech-to-text returns text, so
|
|
83
|
+
* its input is the only media to bill; sync-lipsync v2 bills "per minute of video", held at the longest it is given.
|
|
84
|
+
* Each settles at the length its output states, if it states one. Another endpoint billed by length
|
|
85
|
+
* whose input names none bills its output's (a talking video made from a voice, LatentSync's video, an upscale), which
|
|
86
|
+
* no input states: it is unpriceable. */
|
|
87
|
+
const BY_INPUT_MEDIA = new Set(["fal-ai/elevenlabs/dubbing", "fal-ai/elevenlabs/audio-isolation", "fal-ai/elevenlabs/speech-to-text", "fal-ai/sync-lipsync/v2"]);
|
|
88
|
+
/** The input's audio_url and video_url. */
|
|
89
|
+
const mediaOf = (input) => [input.audio_url, input.video_url].filter((url) => typeof url === "string" && url !== "");
|
|
90
|
+
/** ffmpeg-api/compose's timeline end in seconds: the last keyframe's end across its tracks (timestamp and duration are
|
|
91
|
+
* milliseconds, by its OpenAPI). */
|
|
92
|
+
function composeSeconds(input) {
|
|
93
|
+
const frames = (Array.isArray(input.tracks) ? input.tracks : []).flatMap((track) => (isObject(track) && Array.isArray(track.keyframes) ? track.keyframes : []));
|
|
94
|
+
const ends = frames.map((frame) => (isObject(frame) && typeof frame.timestamp === "number" && frame.timestamp >= 0 && positive(frame.duration) ? frame.timestamp + frame.duration : Number.NaN));
|
|
95
|
+
return ends.length && ends.every(Number.isFinite) ? Math.max(...ends) / 1000 : undefined;
|
|
96
|
+
}
|
|
97
|
+
const COMPOSE = "fal-ai/ffmpeg-api/compose";
|
|
98
|
+
/** The units fal bills a job of `endpoint` by, in `unit` (get_pricing's), read from its `input`. */
|
|
99
|
+
export function unitsBefore(endpoint, unit, input) {
|
|
100
|
+
const bills = `fal bills ${endpoint} per ${oneOf(unit)}`;
|
|
101
|
+
if (AFTER_ONLY.has(unit))
|
|
102
|
+
return { unknown: `${bills}, which can't be known before the job runs` };
|
|
103
|
+
if (!isObject(input))
|
|
104
|
+
return { unknown: `${bills}, and this job has no input to count them from` };
|
|
105
|
+
if (unit === "images" || unit === "audios") {
|
|
106
|
+
const count = countOf(input, unit);
|
|
107
|
+
return count ? { units: count.n, from: count.from } : { unknown: `${bills}, and this job's input does not say how many as a whole number above 0` };
|
|
108
|
+
}
|
|
109
|
+
if (unit === "megapixels") {
|
|
110
|
+
const count = countOf(input, unit);
|
|
111
|
+
const size = input.image_size;
|
|
112
|
+
const pixels = typeof size === "string" && Object.hasOwn(SIZES, size) ? SIZES[size] : isObject(size) && positive(size.width) && positive(size.height) ? [size.width, size.height] : undefined;
|
|
113
|
+
if (!count || !pixels)
|
|
114
|
+
return { unknown: `${bills}, and this job's input names no image_size (a preset such as landscape_4_3, or a width and height) and count to work them out from` };
|
|
115
|
+
return { units: megapixels(pixels[0], pixels[1]) * count.n, from: `image_size ${typeof size === "string" ? size : `${pixels[0]}x${pixels[1]}`}, ${plural(count.n, "image")}` };
|
|
116
|
+
}
|
|
117
|
+
if (unit === "1000 characters") {
|
|
118
|
+
const lines = typeof input.text === "string" ? [input.text] : Array.isArray(input.inputs) ? input.inputs.map((line) => (isObject(line) ? line.text : undefined)) : [];
|
|
119
|
+
if (!lines.length || !lines.every((line) => typeof line === "string"))
|
|
120
|
+
return { unknown: `${bills}, and this job's input has no text to count` };
|
|
121
|
+
const characters = lines.reduce((sum, line) => sum + line.length, 0);
|
|
122
|
+
return { units: characters / 1000, from: `the ${characters} characters of its ${typeof input.text === "string" ? "text" : plural(lines.length, "line")}` };
|
|
123
|
+
}
|
|
124
|
+
if (unit === "seconds" || unit === "minutes") {
|
|
125
|
+
const per = unit === "minutes" ? 60 : 1;
|
|
126
|
+
if (endpoint === COMPOSE) {
|
|
127
|
+
const end = composeSeconds(input);
|
|
128
|
+
return end ? { units: end / per, from: "its timeline's end" } : { unknown: `${bills}, and this job's tracks have no keyframes with a timestamp and a duration to read its length from` };
|
|
129
|
+
}
|
|
130
|
+
const named = namedSeconds(input);
|
|
131
|
+
if (named === "bad")
|
|
132
|
+
return { unknown: `${bills}, and the length this job's input names is not a number above 0` };
|
|
133
|
+
if (named)
|
|
134
|
+
return { units: named.seconds / per, from: named.from };
|
|
135
|
+
const media = BY_INPUT_MEDIA.has(endpoint) ? mediaOf(input) : [];
|
|
136
|
+
if (media.length)
|
|
137
|
+
return { media };
|
|
138
|
+
return { unknown: `${bills}, and this job's input names no length (duration, duration_seconds, seconds_total or music_length_ms)` };
|
|
139
|
+
}
|
|
140
|
+
return { unknown: `${bills}, a unit this server can't count` };
|
|
141
|
+
}
|
|
142
|
+
/** The units a completed job's `output` states, in `unit`: its images, their megapixels, a duration in seconds (its own,
|
|
143
|
+
* or its video's or audio's). Undefined when it states none. */
|
|
144
|
+
export function unitsAfter(unit, output) {
|
|
145
|
+
if (!isObject(output))
|
|
146
|
+
return undefined;
|
|
147
|
+
const images = Array.isArray(output.images) && output.images.length ? output.images : undefined;
|
|
148
|
+
if (unit === "images")
|
|
149
|
+
return images?.length;
|
|
150
|
+
if (unit === "megapixels")
|
|
151
|
+
return images?.every((image) => isObject(image) && positive(image.width) && positive(image.height)) ? images.reduce((sum, image) => sum + megapixels(image.width, image.height), 0) : undefined;
|
|
152
|
+
if (unit === "seconds" || unit === "minutes") {
|
|
153
|
+
const seconds = [output.duration, output.duration_seconds, ...[output.video, output.audio].map((file) => (isObject(file) ? file.duration : undefined))].find(positive);
|
|
154
|
+
return seconds === undefined ? undefined : seconds / (unit === "minutes" ? 60 : 1);
|
|
155
|
+
}
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
/** fal's unit price for `endpoint` in get_pricing's answer: its own entry of `prices`, in USD. */
|
|
159
|
+
export function priceIn(answer, endpoint) {
|
|
160
|
+
const prices = isObject(answer) && Array.isArray(answer.prices) ? answer.prices : [];
|
|
161
|
+
const own = prices.find((price) => isObject(price) && price.endpoint_id === endpoint);
|
|
162
|
+
if (!isObject(own) || own.currency !== "USD" || typeof own.unit !== "string" || !own.unit || typeof own.unit_price !== "number" || !Number.isFinite(own.unit_price) || own.unit_price < 0)
|
|
163
|
+
return undefined;
|
|
164
|
+
return { unitPrice: own.unit_price, unit: own.unit };
|
|
165
|
+
}
|
|
166
|
+
/** A tool result's body: its structured content, else its text as JSON; undefined when it is neither. */
|
|
167
|
+
function bodyOf(result) {
|
|
168
|
+
if (isObject(result.structuredContent))
|
|
169
|
+
return result.structuredContent;
|
|
170
|
+
const text = (Array.isArray(result.content) ? result.content : []).flatMap((block) => (isObject(block) && block.type === "text" && typeof block.text === "string" ? [block.text] : [])).join("");
|
|
171
|
+
try {
|
|
172
|
+
return JSON.parse(text);
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** In whole millionths of a dollar. */
|
|
179
|
+
const usd = (dollars) => Math.round(dollars * 1e6) / 1e6;
|
|
180
|
+
const shown = (n) => String(Math.round(n * 1e6) / 1e6);
|
|
181
|
+
/** A MediaReader over the public-host guard web_fetch and web_save download through (`guard`: their fetch options),
|
|
182
|
+
* within web_save's size cap and download slots. */
|
|
183
|
+
export const publicMedia = (guard, slots = SERVER_SLOTS) => async (url, signal) => {
|
|
184
|
+
const { publicDownload } = await import("@parcha/pi-browser");
|
|
185
|
+
const bound = AbortSignal.any([signal, AbortSignal.timeout(MEDIA_TIMEOUT_MS)]);
|
|
186
|
+
const release = await slots.take(bound);
|
|
187
|
+
try {
|
|
188
|
+
const file = await publicDownload(url, { ...(await guard()), maxBytes: SAVE_MAX_BYTES, signal: bound });
|
|
189
|
+
if (!file.bytes)
|
|
190
|
+
throw new Error(`the host answered ${file.status}`);
|
|
191
|
+
return file.bytes;
|
|
192
|
+
}
|
|
193
|
+
finally {
|
|
194
|
+
release();
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
/** A reply in which fal says it refused the job's input: status "rejected", or an error of type validation_error or
|
|
198
|
+
* with field_errors. */
|
|
199
|
+
const refusedInput = (item) => item.status === "rejected" || (isObject(item.error) && (item.error.type === "validation_error" || Array.isArray(item.error.field_errors)));
|
|
200
|
+
/** What one job charged, by its reply `item` (run_model's reply, or its own entry of submit_jobs'): $0 when the
|
|
201
|
+
* submission was replayed or fal refused its input; a completed job's stated units at its unit price; else its hold
|
|
202
|
+
* (still running, or nothing stated). Null for a job with no price whose reply says it was accepted. */
|
|
203
|
+
function chargeOfJob(quote, item) {
|
|
204
|
+
const hold = "holdUsd" in quote ? quote.holdUsd : null;
|
|
205
|
+
if (!isObject(item))
|
|
206
|
+
return hold;
|
|
207
|
+
if (item.submission_replayed === true)
|
|
208
|
+
return 0;
|
|
209
|
+
// fal refused the input before it made a job (no request_id, and a validation error or a rejection it says so in):
|
|
210
|
+
// nothing to bill. Any other reply without a job (a timeout, an unknown error) may hide one, so it stays at its hold.
|
|
211
|
+
if (typeof item.request_id !== "string" && refusedInput(item))
|
|
212
|
+
return 0;
|
|
213
|
+
if (!("holdUsd" in quote))
|
|
214
|
+
return null;
|
|
215
|
+
const after = item.status === "completed" ? unitsAfter(quote.unit, item.result) : undefined;
|
|
216
|
+
return after === undefined ? quote.holdUsd : usd(quote.unitPrice * after);
|
|
217
|
+
}
|
|
218
|
+
/** fal's tools that take a submission_key: the three that submit jobs, and get_submission, which reads one back. */
|
|
219
|
+
const KEYED = new Set(["run_model", "submit_job", "submit_jobs", "get_submission"]);
|
|
220
|
+
/** fal's longest submission_key: its tools declare `^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$`. */
|
|
221
|
+
export const KEY_MAX = 128;
|
|
222
|
+
const hex16 = (text) => createHash("sha256").update(text).digest("hex").slice(0, 16);
|
|
223
|
+
/** The submission keys of one run on fal's hosted server (McpMeter.keys). fal's journal is per account, so the same key
|
|
224
|
+
* from two runs (a model makes the same key for the same task) would bind the second run's request to the first's and be
|
|
225
|
+
* refused. Each key leaves as the agent's key and `.<16 hex of the run>`: the same run's key leaves the same each time,
|
|
226
|
+
* so a retry is deduped, and another run's never matches it. A key too long for KEY_MAX with that suffix keeps its start
|
|
227
|
+
* and a hash of the whole key in its place, never cut into another key, and ends `:<16 hex of the run>` instead: its
|
|
228
|
+
* shortened start is itself a valid key, and the different mark keeps an agent key that spells it from leaving as the
|
|
229
|
+
* same key. `back` reads each scoped key in a reply as the agent's: by the keys this gateway sent, else by taking the
|
|
230
|
+
* suffix off. */
|
|
231
|
+
export function falKeys(run) {
|
|
232
|
+
const mark = hex16(`agentrun fal submission_key\0${run}`);
|
|
233
|
+
const tail = mark.length + 1;
|
|
234
|
+
const own = new Map();
|
|
235
|
+
const scoped = (key) => {
|
|
236
|
+
if (typeof key !== "string" || !key)
|
|
237
|
+
return key;
|
|
238
|
+
const sent = key.length + tail <= KEY_MAX ? `${key}.${mark}` : `${key.slice(0, KEY_MAX - tail - 17)}.${hex16(key)}:${mark}`;
|
|
239
|
+
own.set(sent, key);
|
|
240
|
+
return sent;
|
|
241
|
+
};
|
|
242
|
+
const anyScoped = new RegExp(`[A-Za-z0-9._:-]+[.:]${mark}(?![A-Za-z0-9._:-])`, "g");
|
|
243
|
+
return {
|
|
244
|
+
out(tool, args) {
|
|
245
|
+
if (!KEYED.has(tool))
|
|
246
|
+
return args;
|
|
247
|
+
let sent = Object.hasOwn(args, "submission_key") ? { ...args, submission_key: scoped(args.submission_key) } : args;
|
|
248
|
+
if (tool === "submit_jobs" && Array.isArray(args.requests))
|
|
249
|
+
sent = { ...sent, requests: args.requests.map((one) => (isObject(one) && Object.hasOwn(one, "submission_key") ? { ...one, submission_key: scoped(one.submission_key) } : one)) };
|
|
250
|
+
return sent;
|
|
251
|
+
},
|
|
252
|
+
back(text) {
|
|
253
|
+
if (!text.includes(mark))
|
|
254
|
+
return text;
|
|
255
|
+
// One pass: a key read back once is never read again, so an agent's key that ends like a scoped one stays as it is.
|
|
256
|
+
const known = [...own.keys()].sort((a, b) => b.length - a.length);
|
|
257
|
+
return text.replace(anyScoped, (token) => {
|
|
258
|
+
const sent = known.find((key) => token.endsWith(key));
|
|
259
|
+
return sent ? `${token.slice(0, -sent.length)}${own.get(sent)}` : token.slice(0, -tail);
|
|
260
|
+
});
|
|
261
|
+
},
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
/** A fal server's meter: fal's unit prices read through the gateway's `ask`, kept per endpoint for PRICE_TTL_MS.
|
|
265
|
+
* `media` reads an input media file (none: such a job is unpriceable); `now`, the clock; `run`, the run whose
|
|
266
|
+
* submission keys it scopes (falKeys; none: they leave as the agent sent them). */
|
|
267
|
+
export function falMeter({ media, now = Date.now, run } = {}) {
|
|
268
|
+
const prices = new Map();
|
|
269
|
+
const unitPrice = async (endpoint, ask) => {
|
|
270
|
+
const kept = prices.get(endpoint);
|
|
271
|
+
if (kept && now() - kept.at < PRICE_TTL_MS)
|
|
272
|
+
return kept;
|
|
273
|
+
const answer = await ask("get_pricing", { endpoint_id: endpoint });
|
|
274
|
+
const unread = (why) => ({ why: `fal's price for ${endpoint} could not be read (its get_pricing ${why})` });
|
|
275
|
+
if (!answer)
|
|
276
|
+
return unread("did not answer");
|
|
277
|
+
if (answer.isError === true)
|
|
278
|
+
return unread("answered an error");
|
|
279
|
+
const price = priceIn(bodyOf(answer), endpoint);
|
|
280
|
+
if (!price)
|
|
281
|
+
return unread("lists no USD price for it");
|
|
282
|
+
const read = { ...price, at: now() };
|
|
283
|
+
prices.set(endpoint, read);
|
|
284
|
+
return read;
|
|
285
|
+
};
|
|
286
|
+
/** The longest of the media at `urls`, in seconds, or the URL whose length could not be read. */
|
|
287
|
+
const longest = async (urls, signal) => {
|
|
288
|
+
const lengths = await Promise.all([...new Set(urls)].map(async (url) => {
|
|
289
|
+
try {
|
|
290
|
+
return (media && mediaSeconds(await media(url, signal))) || { unread: url };
|
|
291
|
+
}
|
|
292
|
+
catch {
|
|
293
|
+
return { unread: url };
|
|
294
|
+
}
|
|
295
|
+
}));
|
|
296
|
+
return lengths.find((one) => typeof one !== "number") ?? Math.max(...lengths);
|
|
297
|
+
};
|
|
298
|
+
const quoteJob = async (job, ask, signal) => {
|
|
299
|
+
if (!job.endpoint)
|
|
300
|
+
return { job, why: "this job names no endpoint_id to price" };
|
|
301
|
+
const price = await unitPrice(job.endpoint, ask);
|
|
302
|
+
if ("why" in price)
|
|
303
|
+
return { job, why: price.why };
|
|
304
|
+
let units = unitsBefore(job.endpoint, price.unit, job.input);
|
|
305
|
+
if ("media" in units) {
|
|
306
|
+
const seconds = await longest(units.media, signal);
|
|
307
|
+
if (typeof seconds !== "number")
|
|
308
|
+
return { job, why: `fal bills ${job.endpoint} per ${oneOf(price.unit)} of its input media, and the length of ${seconds.unread.slice(0, 200)} could not be read` };
|
|
309
|
+
units = { units: seconds / (price.unit === "minutes" ? 60 : 1), from: units.media.length > 1 ? "the longest of its input media, read before the call" : "its input media's length, read before the call" };
|
|
310
|
+
}
|
|
311
|
+
if ("unknown" in units)
|
|
312
|
+
return { job, why: units.unknown };
|
|
313
|
+
return { job, holdUsd: usd(price.unitPrice * units.units), unitPrice: price.unitPrice, unit: price.unit, at: price.at, words: `fal's price for ${job.endpoint}: $${price.unitPrice} per ${oneOf(price.unit)} × ${shown(units.units)} (${units.from})` };
|
|
314
|
+
};
|
|
315
|
+
return {
|
|
316
|
+
...(run ? { keys: falKeys(run) } : {}),
|
|
317
|
+
bills: (tool) => falJobsOf(tool, {}) !== undefined,
|
|
318
|
+
async quote(tool, args, ask, signal) {
|
|
319
|
+
const quotes = [];
|
|
320
|
+
// One at a time: each endpoint's price is read once, the next job's from what the last one kept.
|
|
321
|
+
for (const job of falJobsOf(tool, args) ?? [])
|
|
322
|
+
quotes.push(await quoteJob(job, ask, signal));
|
|
323
|
+
const priced = quotes.filter((quote) => "holdUsd" in quote);
|
|
324
|
+
const unpriced = quotes.find((quote) => "why" in quote);
|
|
325
|
+
const basis = priced.length ? `${priced.map((quote) => quote.words).join("; ")}, from get_pricing at ${new Date(Math.min(...priced.map((quote) => quote.at))).toISOString()}` : "fal's price for it could not be worked out";
|
|
326
|
+
const holdUsd = usd(priced.reduce((sum, quote) => sum + quote.holdUsd, 0));
|
|
327
|
+
return {
|
|
328
|
+
holdUsd,
|
|
329
|
+
basis: unpriced && priced.length ? `${basis}; not counting what could not be priced` : basis,
|
|
330
|
+
...(unpriced ? { unpriceable: unpriced.why } : {}),
|
|
331
|
+
chargeOf(reply) {
|
|
332
|
+
if (!reply)
|
|
333
|
+
return null;
|
|
334
|
+
const body = bodyOf(reply);
|
|
335
|
+
if (tool !== "submit_jobs")
|
|
336
|
+
return chargeOfJob(quotes[0], body);
|
|
337
|
+
// submit_jobs: each job by its own entry of the reply, matched by the index the call gave it.
|
|
338
|
+
const lists = isObject(body) ? Object.values(body).filter((value) => Array.isArray(value) && value.length > 0 && value.every((item) => isObject(item) && Number.isInteger(item.index))) : [];
|
|
339
|
+
const items = lists[0];
|
|
340
|
+
// A reply that lists no job by its index: read as one, each job at its hold.
|
|
341
|
+
if (!items)
|
|
342
|
+
return unpriced ? null : holdUsd;
|
|
343
|
+
let total = 0;
|
|
344
|
+
for (const [at, quote] of quotes.entries()) {
|
|
345
|
+
const charge = chargeOfJob(quote, items.find((item) => item.index === (quote.job.index ?? at)) ?? "missing");
|
|
346
|
+
if (charge === null)
|
|
347
|
+
return null;
|
|
348
|
+
total += charge;
|
|
349
|
+
}
|
|
350
|
+
return usd(total);
|
|
351
|
+
},
|
|
352
|
+
};
|
|
353
|
+
},
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
/** The meter of a pinned server (mcp-gateway.ts McpPin entry): fal's for one marked `fal` at its save, or, for a pin made
|
|
357
|
+
* before servers were marked, one whose URL is on fal's MCP host. Only a server at fal's host scopes its submission keys
|
|
358
|
+
* to `deps.run`: the journal they collide in is that server's, and a server marked `fal` elsewhere (a template eval's
|
|
359
|
+
* fake, whose answers and log graders read by the agent's own key) receives them as the agent sent them. */
|
|
360
|
+
export const meterFor = (pinned, deps) => {
|
|
361
|
+
if (!falServer(pinned.url, pinned.meter))
|
|
362
|
+
return undefined;
|
|
363
|
+
const { run, ...rest } = deps ?? {};
|
|
364
|
+
return falMeter({ ...rest, ...(run && URL.parse(pinned.url)?.hostname === FAL_MCP_HOST ? { run } : {}) });
|
|
365
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { InputIssue } from "./errors.js";
|
|
2
|
+
/** Why `value` (text) is not a key a run can be handed at `field`, as a 422 issue that never shows it, or undefined for
|
|
3
|
+
* one it can: MIN_SECRET to `max` characters with no space or control character. */
|
|
4
|
+
export declare function handedSecretOf(value: string, field: string, max: number): InputIssue | undefined;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { MIN_SECRET } from "./learn/secrets.js";
|
|
2
|
+
/** Why `value` (text) is not a key a run can be handed at `field`, as a 422 issue that never shows it, or undefined for
|
|
3
|
+
* one it can: MIN_SECRET to `max` characters with no space or control character. */
|
|
4
|
+
export function handedSecretOf(value, field, max) {
|
|
5
|
+
const wrong = value.length < MIN_SECRET ? `at least ${MIN_SECRET} characters: a shorter key can't be cut from what the run writes`
|
|
6
|
+
: value.length > max ? `at most ${max} characters`
|
|
7
|
+
: /[\s\x00-\x1f\x7f]/.test(value) ? "text with no space or control character"
|
|
8
|
+
: undefined;
|
|
9
|
+
return wrong === undefined ? undefined : { field, reason: "invalid", message: `${field} is ${wrong}.` };
|
|
10
|
+
}
|
|
@@ -4,7 +4,7 @@ export declare const ARCHIVE_CODES: Readonly<{
|
|
|
4
4
|
readonly platform: "HARNESS_PLATFORM_UNSUPPORTED";
|
|
5
5
|
/** The archive could not be downloaded: the network, a status other than 200, a size past the limit. */
|
|
6
6
|
readonly download: "HARNESS_DOWNLOAD_FAILED";
|
|
7
|
-
/** The archive downloaded is not the pin's: its sha256 differs. */
|
|
7
|
+
/** The archive downloaded is not the pin's: its sha256 (or sha512) differs. */
|
|
8
8
|
readonly archive: "HARNESS_ARCHIVE_MISMATCH";
|
|
9
9
|
/** tar could not unpack the verified archive, or it holds no binary at the pin's path. */
|
|
10
10
|
readonly unpack: "HARNESS_UNPACK_FAILED";
|
|
@@ -14,12 +14,15 @@ export declare const ARCHIVE_CODES: Readonly<{
|
|
|
14
14
|
readonly busy: "HARNESS_INSTALL_BUSY";
|
|
15
15
|
}>;
|
|
16
16
|
export type ArchiveCode = (typeof ARCHIVE_CODES)[keyof typeof ARCHIVE_CODES];
|
|
17
|
-
/** One platform's release, as a pin holds it (the ACP registry's: acp's AcpArchivePin).
|
|
18
|
-
*
|
|
17
|
+
/** One platform's release, as a pin holds it (the ACP registry's: acp's AcpArchivePin). The archive is pinned by one
|
|
18
|
+
* digest: its `sha256`, else its `sha512` (Google's agy CLI manifest lists that one alone); a recipe's loader takes
|
|
19
|
+
* exactly one. `treeSha256`: the digest of every file it unpacks to (acp's treeDigest), for a binary that is a launcher
|
|
20
|
+
* of the code beside it (Cursor's). */
|
|
19
21
|
export type ArchiveRelease = {
|
|
20
22
|
readonly url: string;
|
|
21
23
|
readonly format: string;
|
|
22
|
-
readonly sha256
|
|
24
|
+
readonly sha256?: string;
|
|
25
|
+
readonly sha512?: string;
|
|
23
26
|
readonly cmd: string;
|
|
24
27
|
readonly binarySha256?: string;
|
|
25
28
|
readonly treeSha256?: string;
|
|
@@ -37,7 +40,7 @@ export type ArchiveHarness = {
|
|
|
37
40
|
readonly tree?: (root: string) => Promise<string>;
|
|
38
41
|
};
|
|
39
42
|
/** An archive format this installer unpacks. */
|
|
40
|
-
export type ArchiveFormat = "tar.bz2" | "tar.gz";
|
|
43
|
+
export type ArchiveFormat = "tar.bz2" | "tar.gz" | "zip";
|
|
41
44
|
/** The formats this installer unpacks, the one list a recipe's loader checks a release against (harness-recipe.ts). */
|
|
42
45
|
export declare const ARCHIVE_FORMATS: readonly ArchiveFormat[];
|
|
43
46
|
/** A failed install, by its code (ARCHIVE_CODES): its message says why, and the command that tries again. */
|
|
@@ -73,6 +76,13 @@ export interface ArchiveInstallOptions {
|
|
|
73
76
|
read(): string | undefined;
|
|
74
77
|
write(sha256: string): void;
|
|
75
78
|
};
|
|
79
|
+
/** For a release with no tree digest pinned (a recipe's): the record of the first tree (every file beside the binary,
|
|
80
|
+
* harness.tree's digest) unpacked from its verified archive, kept as binaryRecord keeps the binary's, so a sidecar
|
|
81
|
+
* that differs is refused (HARNESS_BINARY_MISMATCH) as the binary is. Needs harness.tree. */
|
|
82
|
+
treeRecord?: {
|
|
83
|
+
read(): string | undefined;
|
|
84
|
+
write(sha256: string): void;
|
|
85
|
+
};
|
|
76
86
|
}
|
|
77
87
|
/** Installs `harness` into `dir` (`<data>/harnesses/<id>`) from its pin's archive for this platform: downloaded, checked,
|
|
78
88
|
* unpacked beside `dir`, its binary checked, then renamed into place and made read-only. `already` when the pinned binary
|
package/dist/harness-archive.js
CHANGED
|
@@ -3,13 +3,16 @@
|
|
|
3
3
|
// platform: its URL and sha256, and the sha256 of the binary it unpacks to), and it lands whole or not at all, as a
|
|
4
4
|
// first-use install does (harness-install.ts):
|
|
5
5
|
// - the archive is downloaded with node's fetch into a fresh folder beside `<data>/harnesses/<harness>`, its sha256
|
|
6
|
-
//
|
|
6
|
+
// (or the sha512 a release is pinned by, as Google's agy CLI manifest lists it) taken as it arrives, and refused
|
|
7
|
+
// unless it is the pin's before anything reads it;
|
|
7
8
|
// - the verified archive is unpacked there by the system's tar (`tar -xjf` for Goose's tar.bz2, `-xzf` for Cursor's
|
|
8
9
|
// tar.gz; a fixed argument list, no shell, an environment of PATH alone): node has no bzip2 of its own, and the
|
|
9
|
-
// repository carries none;
|
|
10
|
+
// repository carries none. A zip (Google Antigravity's) is unpacked by node itself (harness-zip.ts), no tool needed;
|
|
10
11
|
// - the binary's sha256 is checked there against the pin, and where the pin holds one the digest of every file beside
|
|
11
|
-
// it (Cursor's launcher runs its own node on a hundred files of code
|
|
12
|
-
// and made read-only.
|
|
12
|
+
// it (Cursor's launcher runs its own node on a hundred files of code; Antigravity's server runs a sidecar), and only
|
|
13
|
+
// then is the folder renamed into place and made read-only. A recipe's release pins neither: the first binary and
|
|
14
|
+
// tree unpacked from its verified archive are recorded, and a later install that differs is refused. What was there
|
|
15
|
+
// before goes aside first, then away;
|
|
13
16
|
// - a crash part way leaves that fresh folder (its name the sweep's) and never a half install: the next install removes it.
|
|
14
17
|
// One install at a time on a data directory, under the hold first-use installs take (takeHold). The installer is handed no
|
|
15
18
|
// key of the server's: fetch carries no header of ours, and tar's environment is PATH alone.
|
|
@@ -21,13 +24,14 @@ import { dirname, join, relative, resolve, sep } from "node:path";
|
|
|
21
24
|
import { Readable, Transform } from "node:stream";
|
|
22
25
|
import { pipeline } from "node:stream/promises";
|
|
23
26
|
import { leftoverPrefix, releaseHold, removeLeftover, sweepLeftovers, takeHold, writable } from "./harness-install.js";
|
|
27
|
+
import { unzipInto } from "./harness-zip.js";
|
|
24
28
|
/** The codes an archive install's failures carry (ArchiveInstallFailed). */
|
|
25
29
|
export const ARCHIVE_CODES = Object.freeze({
|
|
26
30
|
/** The pin has no archive for this platform, or no binary digest to check what it unpacks to. */
|
|
27
31
|
platform: "HARNESS_PLATFORM_UNSUPPORTED",
|
|
28
32
|
/** The archive could not be downloaded: the network, a status other than 200, a size past the limit. */
|
|
29
33
|
download: "HARNESS_DOWNLOAD_FAILED",
|
|
30
|
-
/** The archive downloaded is not the pin's: its sha256 differs. */
|
|
34
|
+
/** The archive downloaded is not the pin's: its sha256 (or sha512) differs. */
|
|
31
35
|
archive: "HARNESS_ARCHIVE_MISMATCH",
|
|
32
36
|
/** tar could not unpack the verified archive, or it holds no binary at the pin's path. */
|
|
33
37
|
unpack: "HARNESS_UNPACK_FAILED",
|
|
@@ -36,10 +40,11 @@ export const ARCHIVE_CODES = Object.freeze({
|
|
|
36
40
|
/** Another install into the same data directory is running. */
|
|
37
41
|
busy: "HARNESS_INSTALL_BUSY",
|
|
38
42
|
});
|
|
39
|
-
/** How
|
|
40
|
-
|
|
43
|
+
/** How each format this installer takes is unpacked: tar's flag (node has neither bzip2 nor a tar of its own), or node's
|
|
44
|
+
* own zip reader. */
|
|
45
|
+
const UNPACK = Object.freeze({ "tar.bz2": "-xjf", "tar.gz": "-xzf", zip: "node" });
|
|
41
46
|
/** The formats this installer unpacks, the one list a recipe's loader checks a release against (harness-recipe.ts). */
|
|
42
|
-
export const ARCHIVE_FORMATS = Object.freeze(Object.keys(
|
|
47
|
+
export const ARCHIVE_FORMATS = Object.freeze(Object.keys(UNPACK));
|
|
43
48
|
/** A failed install, by its code (ARCHIVE_CODES): its message says why, and the command that tries again. */
|
|
44
49
|
export class ArchiveInstallFailed extends Error {
|
|
45
50
|
code;
|
|
@@ -82,24 +87,33 @@ export async function installArchive(harness, dir, options = {}) {
|
|
|
82
87
|
const release = platform ? harness.pin.archives[platform] : undefined;
|
|
83
88
|
if (!platform || !release)
|
|
84
89
|
throw fail(ARCHIVE_CODES.platform, `${named} has no release pinned for ${platform ?? `${process.platform}-${process.arch}`}`);
|
|
90
|
+
// The archive's one digest, by the algorithm its pin names: sha256, else sha512.
|
|
91
|
+
const algorithm = release.sha256 !== undefined ? "sha256" : "sha512";
|
|
92
|
+
const want = release.sha256 ?? release.sha512;
|
|
93
|
+
if (!want)
|
|
94
|
+
throw fail(ARCHIVE_CODES.platform, `${named}'s archive on ${platform} has no sha256 or sha512 pinned, so it cannot be checked`);
|
|
85
95
|
if (!release.binarySha256 && !options.binaryRecord)
|
|
86
96
|
throw fail(ARCHIVE_CODES.platform, `${named}'s binary on ${platform} has no sha256 pinned, so what its archive unpacks to cannot be checked`);
|
|
87
|
-
const flag =
|
|
88
|
-
if (!flag || !Object.hasOwn(
|
|
89
|
-
throw fail(ARCHIVE_CODES.platform, `${named}'s archive on ${platform} is ${release.format}, and this installer unpacks ${ARCHIVE_FORMATS.join(" and
|
|
90
|
-
|
|
91
|
-
|
|
97
|
+
const flag = UNPACK[release.format];
|
|
98
|
+
if (!flag || !Object.hasOwn(UNPACK, release.format))
|
|
99
|
+
throw fail(ARCHIVE_CODES.platform, `${named}'s archive on ${platform} is ${release.format}, and this installer unpacks ${ARCHIVE_FORMATS.slice(0, -1).join(", ")} and ${ARCHIVE_FORMATS.at(-1)} alone`);
|
|
100
|
+
const treeRecord = release.treeSha256 ? undefined : options.treeRecord;
|
|
101
|
+
if ((release.treeSha256 || treeRecord) && !harness.tree)
|
|
102
|
+
throw fail(ARCHIVE_CODES.platform, `${named}'s files on ${platform} are ${release.treeSha256 ? "pinned" : "recorded"} by a tree digest, and this install was given no way to take one`);
|
|
92
103
|
const binary = binaryUnder(dir, release.cmd);
|
|
93
104
|
if (!binary)
|
|
94
105
|
throw fail(ARCHIVE_CODES.platform, `${named}'s pinned path ${release.cmd} is not inside its folder`);
|
|
95
|
-
// The pin's
|
|
106
|
+
// The pin's digests, else the first ones recorded (none before the first install).
|
|
96
107
|
const pinned = release.binarySha256 ?? options.binaryRecord?.read();
|
|
97
|
-
|
|
98
|
-
|
|
108
|
+
const pinnedTree = release.treeSha256 ?? treeRecord?.read();
|
|
109
|
+
/** Whether the pinned binary is in place: a file whose sha256 is the pin's, in a folder whose tree is the pin's (or the
|
|
110
|
+
* one recorded) where it pins or records one. A recipe's install from before trees were recorded is not: it is
|
|
111
|
+
* installed again, and its tree recorded. */
|
|
99
112
|
const inPlace = async () => pinned !== undefined &&
|
|
113
|
+
(!treeRecord || pinnedTree !== undefined) &&
|
|
100
114
|
(await lstat(binary).catch(() => undefined)) !== undefined &&
|
|
101
115
|
(await digestOf(binary).catch(() => undefined)) === pinned &&
|
|
102
|
-
(
|
|
116
|
+
(pinnedTree === undefined || (await harness.tree(dir).catch(() => undefined)) === pinnedTree);
|
|
103
117
|
if (await inPlace())
|
|
104
118
|
return { installed: "already", binary };
|
|
105
119
|
const parent = dirname(dir);
|
|
@@ -118,8 +132,8 @@ export async function installArchive(harness, dir, options = {}) {
|
|
|
118
132
|
const archive = join(work, `archive.${release.format}`);
|
|
119
133
|
const timeoutMs = options.timeoutMs ?? ARCHIVE_TIMEOUT_MS;
|
|
120
134
|
const maxBytes = options.maxBytes ?? ARCHIVE_MAX_BYTES;
|
|
121
|
-
// The download, hashed as it is written; refused past its limit.
|
|
122
|
-
const hash = createHash(
|
|
135
|
+
// The download, hashed as it is written by its pin's algorithm; refused past its limit.
|
|
136
|
+
const hash = createHash(algorithm);
|
|
123
137
|
let bytes = 0;
|
|
124
138
|
try {
|
|
125
139
|
const response = await (options.fetch ?? fetch)(release.url, { redirect: "follow", signal: AbortSignal.timeout(timeoutMs) });
|
|
@@ -146,21 +160,31 @@ export async function installArchive(harness, dir, options = {}) {
|
|
|
146
160
|
throw fail(ARCHIVE_CODES.download, `${named}'s archive could not be downloaded from ${release.url}: ${error.message}${cause ? ` (${cause.code ?? cause.message})` : ""}`);
|
|
147
161
|
}
|
|
148
162
|
const got = hash.digest("hex");
|
|
149
|
-
if (got !==
|
|
150
|
-
throw fail(ARCHIVE_CODES.archive, `${named}'s archive from ${release.url} has
|
|
163
|
+
if (got !== want)
|
|
164
|
+
throw fail(ARCHIVE_CODES.archive, `${named}'s archive from ${release.url} has ${algorithm} ${got}, and the pin is ${want}; it was not unpacked`);
|
|
151
165
|
await options.onStep?.("downloaded");
|
|
152
|
-
// Unpacked
|
|
166
|
+
// Unpacked, verified archive only, into a folder of its own inside the fresh one: a zip by node, a tarball by tar.
|
|
153
167
|
const tree = join(work, "tree");
|
|
154
168
|
await mkdir(tree);
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
169
|
+
if (release.format === "zip") {
|
|
170
|
+
try {
|
|
171
|
+
await unzipInto(archive, tree);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
throw fail(ARCHIVE_CODES.unpack, `${named}'s zip was not unpacked: ${error.message}`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
const PATH = (options.env ?? process.env).PATH ?? "/usr/bin:/bin";
|
|
179
|
+
await new Promise((done, reject) => {
|
|
180
|
+
execFile(options.tar ?? "tar", [flag, archive, "-C", tree, "--no-same-owner", "--no-same-permissions"], { env: { PATH, LC_ALL: "C" }, timeout: timeoutMs, maxBuffer: 1024 * 1024 }, (error, _stdout, stderr) => {
|
|
181
|
+
if (error)
|
|
182
|
+
reject(fail(ARCHIVE_CODES.unpack, `tar could not unpack ${named}'s archive: ${String(stderr).trim() || error.message}`));
|
|
183
|
+
else
|
|
184
|
+
done();
|
|
185
|
+
});
|
|
162
186
|
});
|
|
163
|
-
}
|
|
187
|
+
}
|
|
164
188
|
await options.onStep?.("unpacked");
|
|
165
189
|
// The binary, a regular file at the pin's path, checked before anything puts it in place.
|
|
166
190
|
const unpacked = binaryUnder(tree, release.cmd);
|
|
@@ -184,11 +208,24 @@ export async function installArchive(harness, dir, options = {}) {
|
|
|
184
208
|
}
|
|
185
209
|
else if (binarySha !== pinned)
|
|
186
210
|
throw fail(ARCHIVE_CODES.binary, `${named}'s binary from its archive has sha256 ${binarySha}, and the ${release.binarySha256 ? "pin" : "first one recorded"} is ${pinned}; it was not put in place`);
|
|
187
|
-
// Every file beside it, where the pin holds their digest: a launcher's code
|
|
188
|
-
|
|
211
|
+
// Every file beside it, where the pin holds their digest or a record keeps the first: a launcher's code, or a server's
|
|
212
|
+
// sidecar, is as much the harness as the binary.
|
|
213
|
+
if (release.treeSha256 || treeRecord) {
|
|
189
214
|
const treeSha = await harness.tree(tree);
|
|
190
|
-
|
|
191
|
-
|
|
215
|
+
let first = pinnedTree;
|
|
216
|
+
if (first === undefined) {
|
|
217
|
+
try {
|
|
218
|
+
treeRecord.write(treeSha);
|
|
219
|
+
first = treeSha;
|
|
220
|
+
}
|
|
221
|
+
catch (error) {
|
|
222
|
+
if (error?.code !== "EEXIST")
|
|
223
|
+
throw error;
|
|
224
|
+
first = treeRecord.read();
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (treeSha !== first)
|
|
228
|
+
throw fail(ARCHIVE_CODES.binary, `${named}'s files from its archive have the tree sha256 ${treeSha}, and the ${release.treeSha256 ? "pin" : "first one recorded"} is ${first}; they were not put in place`);
|
|
192
229
|
}
|
|
193
230
|
await options.onStep?.("verified");
|
|
194
231
|
// Into place whole: what was there (a binary that is not the pin's, a folder by hand) goes aside first, then away.
|
package/dist/harness-check.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { egressProxy } from "./harness-egress.js";
|
|
1
2
|
export declare const CHECK_DEFAULT_CAP_USD = 0.25;
|
|
2
3
|
export interface HarnessCheckOptions {
|
|
3
4
|
/** The recipe file. */
|
|
@@ -63,6 +64,7 @@ export interface HarnessCheckReport {
|
|
|
63
64
|
checkedAt: string;
|
|
64
65
|
failures: string[];
|
|
65
66
|
}
|
|
67
|
+
export { egressProxy };
|
|
66
68
|
/** Runs the check. Never throws for what it judges: a refused recipe, a missing install or a failed stage is in the
|
|
67
69
|
* report (`ok: false`, `failures`). Every string of the report and every line it says has the server's secrets and the
|
|
68
70
|
* recipe's keys cut (runner/redact.ts secretRedactor), so an agent's words in it (a failed turn's stderr, a refusal, a
|
|
@@ -72,4 +74,3 @@ export declare function harnessCheck(options: HarnessCheckOptions): Promise<Harn
|
|
|
72
74
|
export declare function printedFacts(report: HarnessCheckReport): string[];
|
|
73
75
|
/** Writes the report to `file`. */
|
|
74
76
|
export declare const writeReport: (file: string, report: HarnessCheckReport) => Promise<void>;
|
|
75
|
-
export {};
|