@parcha/agentrun-serve 0.1.0-beta.49 → 0.1.0-beta.51
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 +173 -9
- package/README.md +246 -39
- package/dist/agent-delete.d.ts +3 -2
- package/dist/agent-delete.js +29 -4
- package/dist/agents-store.js +7 -3
- package/dist/api-only.d.ts +8 -10
- package/dist/api-only.js +11 -12
- package/dist/capacity.d.ts +50 -0
- package/dist/capacity.js +49 -0
- package/dist/cli-learning/claude-wire.d.ts +41 -0
- package/dist/cli-learning/claude-wire.js +258 -0
- package/dist/cli-learning/cli.d.ts +11 -0
- package/dist/cli-learning/cli.js +241 -0
- package/dist/cli-learning/codex-worktree.d.ts +10 -0
- package/dist/cli-learning/codex-worktree.js +119 -0
- package/dist/cli-learning/connections.d.ts +62 -0
- package/dist/cli-learning/connections.js +87 -0
- package/dist/cli-learning/entry.d.ts +1 -0
- package/dist/cli-learning/entry.js +4 -0
- package/dist/cli-learning/evidence.d.ts +78 -0
- package/dist/cli-learning/evidence.js +492 -0
- package/dist/cli-learning/hook.d.ts +33 -0
- package/dist/cli-learning/hook.js +544 -0
- package/dist/cli-learning/install.d.ts +33 -0
- package/dist/cli-learning/install.js +335 -0
- package/dist/cli-learning/observation-proof.d.ts +14 -0
- package/dist/cli-learning/observation-proof.js +10 -0
- package/dist/cli-learning/policy.d.ts +48 -0
- package/dist/cli-learning/policy.js +363 -0
- package/dist/cli-learning/schema.d.ts +16 -0
- package/dist/cli-learning/schema.js +27 -0
- package/dist/cli-learning/state.d.ts +23 -0
- package/dist/cli-learning/state.js +61 -0
- package/dist/cli.js +24 -2
- package/dist/compute-cost.d.ts +101 -0
- package/dist/compute-cost.js +250 -0
- package/dist/console/assets/index-Bt1B51b7.js +733 -0
- package/dist/console/assets/index-sVftE9Ht.css +1 -0
- package/dist/console/index.html +2 -2
- package/dist/email/email-faux.d.ts +3 -1
- package/dist/email/email-faux.js +3 -2
- package/dist/email/routes.js +22 -12
- package/dist/eval-previous.d.ts +52 -0
- package/dist/eval-previous.js +164 -0
- package/dist/file-types.d.ts +2 -0
- package/dist/file-types.js +3 -1
- package/dist/harness-install.d.ts +6 -2
- package/dist/harness-install.js +40 -8
- package/dist/harness.d.ts +1 -1
- package/dist/harness.js +3 -2
- package/dist/learn/check.d.ts +8 -5
- package/dist/learn/check.js +25 -24
- package/dist/learn/repick.d.ts +22 -13
- package/dist/learn/repick.js +57 -56
- package/dist/learn/secrets.js +14 -3
- package/dist/learn/workflow-draft.d.ts +4 -0
- package/dist/learn/workflow-draft.js +10 -7
- package/dist/learned-workflow.js +8 -8
- package/dist/learning-setting.js +2 -2
- package/dist/learning-words.d.ts +1 -1
- package/dist/learning-words.js +1 -1
- package/dist/mcp-pin-first.js +2 -1
- package/dist/mcp-private-hosts.d.ts +24 -3
- package/dist/mcp-private-hosts.js +53 -3
- package/dist/model-gateway.d.ts +2 -0
- package/dist/model-gateway.js +3 -1
- package/dist/native/runtimes.d.ts +2 -0
- package/dist/native/runtimes.js +2 -0
- package/dist/observation/learning.d.ts +32 -0
- package/dist/observation/learning.js +66 -0
- package/dist/observation/observer.d.ts +8 -1
- package/dist/observation/observer.js +89 -9
- package/dist/observation/schema.js +12 -4
- package/dist/observation/usage-identity.d.ts +9 -0
- package/dist/observation/usage-identity.js +11 -0
- package/dist/openapi.js +113 -29
- package/dist/paid-calls.d.ts +13 -6
- package/dist/paid-calls.js +14 -8
- package/dist/run-launch.d.ts +16 -2
- package/dist/run-launch.js +6 -1
- package/dist/run-timeline-served.js +5 -3
- package/dist/run-usage.d.ts +23 -8
- package/dist/run-usage.js +100 -18
- package/dist/run-view.d.ts +2 -0
- package/dist/run-view.js +12 -7
- package/dist/runner/adapter-loop.d.ts +4 -0
- package/dist/runner/adapter-loop.js +44 -27
- package/dist/runner/adapter-run-spec.d.ts +8 -3
- package/dist/runner/adapter-run-spec.js +15 -8
- package/dist/runner/beside.d.ts +13 -0
- package/dist/runner/beside.js +27 -8
- package/dist/runner/big-writes.d.ts +33 -0
- package/dist/runner/big-writes.js +105 -0
- package/dist/runner/cli-run-spec.js +16 -8
- package/dist/runner/disk-free.d.ts +44 -0
- package/dist/runner/disk-free.js +96 -0
- package/dist/runner/feedback-round.d.ts +12 -8
- package/dist/runner/feedback-round.js +16 -30
- package/dist/runner/harness-loops.js +3 -1
- package/dist/runner/held.d.ts +4 -1
- package/dist/runner/journal-kinds.d.ts +1 -1
- package/dist/runner/learning-end.js +13 -4
- package/dist/runner/main.d.ts +17 -6
- package/dist/runner/main.js +187 -45
- package/dist/runner/metrics.d.ts +4 -0
- package/dist/runner/metrics.js +9 -0
- package/dist/runner/presence.d.ts +11 -2
- package/dist/runner/presence.js +31 -3
- package/dist/runner/run-child-host.d.ts +27 -17
- package/dist/runner/run-child-host.js +70 -116
- package/dist/runner/run-child.js +3 -0
- package/dist/runner/run-errors.js +2 -0
- package/dist/runner/runner-check.d.ts +51 -0
- package/dist/runner/runner-check.js +206 -0
- package/dist/runner/runner-metrics.d.ts +19 -0
- package/dist/runner/runner-metrics.js +49 -0
- package/dist/runner/runner.d.ts +37 -0
- package/dist/runner/runner.js +43 -3
- package/dist/runner/save-slots.d.ts +9 -0
- package/dist/runner/save-slots.js +91 -0
- package/dist/runner/sdk-loop.js +34 -22
- package/dist/runs-over-store.d.ts +8 -1
- package/dist/runs-over-store.js +57 -9
- package/dist/sdk/runs.d.ts +25 -6
- package/dist/sdk/runs.js +107 -35
- package/dist/sdk/runtimes.d.ts +19 -0
- package/dist/sdk/runtimes.js +23 -0
- package/dist/sdk/schema.d.ts +14 -8
- package/dist/sdk/schema.js +18 -6
- package/dist/server.d.ts +7 -0
- package/dist/server.js +151 -28
- package/dist/skills/agentrun-serve/SKILL.md +52 -7
- package/dist/start-switches.js +2 -0
- package/dist/stats.d.ts +4 -0
- package/dist/stats.js +1 -1
- package/dist/store/agent-body.d.ts +13 -2
- package/dist/store/agent-body.js +19 -4
- package/dist/store/dialect.d.ts +2 -0
- package/dist/store/dialect.js +1 -0
- package/dist/store/index.d.ts +3 -3
- package/dist/store/index.js +2 -2
- package/dist/store/postgres/connection.d.ts +5 -2
- package/dist/store/postgres/connection.js +19 -7
- package/dist/store/postgres/dialect.js +1 -0
- package/dist/store/postgres/migrate.d.ts +30 -0
- package/dist/store/postgres/migrate.js +72 -5
- package/dist/store/postgres/migration-lint.d.ts +8 -0
- package/dist/store/postgres/migration-lint.js +119 -0
- package/dist/store/postgres/migrations/0023_computer_interval_resources.sql +14 -0
- package/dist/store/postgres/migrations/0024_runner_capacity.sql +28 -0
- package/dist/store/postgres/store.d.ts +8 -3
- package/dist/store/postgres/store.js +31 -11
- package/dist/store/schema.d.ts +26 -4
- package/dist/store/schema.js +45 -4
- package/dist/store/sqlite/connection.d.ts +3 -3
- package/dist/store/sqlite/connection.js +3 -3
- package/dist/store/sqlite/run-launch.d.ts +9 -1
- package/dist/store/sqlite/run-launch.js +44 -0
- package/dist/store/sqlite/store.d.ts +19 -4
- package/dist/store/sqlite/store.js +180 -93
- package/dist/store/store-schema.d.ts +34 -0
- package/dist/store/store-schema.js +125 -0
- package/dist/store/store.d.ts +61 -2
- package/dist/store/store.js +13 -0
- package/dist/store-reads/analytics.d.ts +3 -2
- package/dist/store-reads/analytics.js +20 -8
- package/dist/store-reads/cap.d.ts +9 -5
- package/dist/store-reads/cap.js +28 -2
- package/dist/store-reads/decisions.d.ts +21 -0
- package/dist/store-reads/decisions.js +22 -0
- package/dist/store-reads/journal.d.ts +8 -2
- package/dist/store-reads/journal.js +27 -8
- package/dist/store-view.d.ts +9 -4
- package/dist/store-view.js +48 -25
- package/dist/template-eval.d.ts +15 -1
- package/dist/template-eval.js +92 -13
- package/dist/templates.d.ts +4 -3
- package/dist/templates.js +4 -3
- package/dist/timeline/labels.js +17 -0
- package/dist/tool-gate.d.ts +4 -2
- package/dist/tool-list.js +1 -0
- package/dist/tools.d.ts +6 -2
- package/dist/tools.js +13 -1
- package/dist/trigger-targets.d.ts +21 -5
- package/dist/trigger-targets.js +13 -10
- package/dist/unified-usage.d.ts +24 -0
- package/dist/unified-usage.js +400 -0
- package/dist/uploads-archil.d.ts +100 -0
- package/dist/uploads-archil.js +430 -0
- package/dist/uploads.d.ts +55 -1
- package/dist/uploads.js +185 -98
- package/dist/validate.js +1 -1
- package/dist/web-fake.d.ts +25 -0
- package/dist/web-fake.js +38 -1
- package/dist/web-save.d.ts +75 -0
- package/dist/web-save.js +235 -0
- package/dist/workspace/archil-claim.d.ts +5 -2
- package/dist/workspace/archil-claim.js +5 -2
- package/dist/workspace/archil-disk.d.ts +11 -1
- package/dist/workspace/archil-disk.js +43 -10
- package/dist/workspace/archil.d.ts +3 -0
- package/dist/workspace/archil.js +102 -16
- package/dist/workspace/computer-for-claim.d.ts +14 -0
- package/dist/workspace/computer-for-claim.js +46 -7
- package/dist/workspace/computer-intervals.d.ts +9 -3
- package/dist/workspace/computer-intervals.js +17 -8
- package/dist/workspace/pieced.d.ts +14 -0
- package/dist/workspace/pieced.js +23 -0
- package/dist/workspace/run-disk.js +32 -12
- package/dist/workspace/run-workspace.d.ts +1 -1
- package/dist/workspace/run-workspace.js +11 -6
- package/package.json +10 -10
- package/skills/agentrun-serve/SKILL.md +52 -7
- package/templates/README.md +36 -7
- package/dist/console/assets/index-Bzn4eUYF.js +0 -715
- package/dist/console/assets/index-CR2ZGei1.css +0 -1
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { Refusal } from "@parcha/agentrun-kernel";
|
|
2
|
+
import { type PaidBooking } from "./paid-calls.js";
|
|
3
|
+
/** The resources computer time is priced by. */
|
|
4
|
+
export type ComputeResource = "vcpu" | "memory" | "gpu" | "disk";
|
|
5
|
+
/** The unit each resource's time is counted in. */
|
|
6
|
+
export declare const COMPUTE_UNITS: {
|
|
7
|
+
readonly vcpu: "vcpu_second";
|
|
8
|
+
readonly memory: "gib_second";
|
|
9
|
+
readonly gpu: "gpu_second";
|
|
10
|
+
readonly disk: "gb_month";
|
|
11
|
+
};
|
|
12
|
+
export type ComputeUnit = (typeof COMPUTE_UNITS)[ComputeResource];
|
|
13
|
+
/** A month of disk, as providers bill one: 730 hours. */
|
|
14
|
+
export declare const SECONDS_PER_MONTH: number;
|
|
15
|
+
/** The settings' prefixes, by resource (the provider's name, and a GPU's type, follow in capitals). */
|
|
16
|
+
export declare const COMPUTE_PRICE_ENV: {
|
|
17
|
+
readonly vcpu: "AGENTRUN_COMPUTE_USD_PER_VCPU_SECOND_";
|
|
18
|
+
readonly memory: "AGENTRUN_COMPUTE_USD_PER_GIB_SECOND_";
|
|
19
|
+
readonly gpu: "AGENTRUN_COMPUTE_USD_PER_GPU_SECOND_";
|
|
20
|
+
readonly disk: "AGENTRUN_COMPUTE_USD_PER_DISK_GB_MONTH_";
|
|
21
|
+
};
|
|
22
|
+
/** The setting that prices `resource` of `provider` (a GPU's by its type). */
|
|
23
|
+
export declare const computePriceEnvOf: (resource: ComputeResource, provider: string, gpuType?: string | null) => string;
|
|
24
|
+
/** What a computer held, as its interval keeps it (store computer_intervals): each null where nothing says it. */
|
|
25
|
+
export type ComputeFigures = {
|
|
26
|
+
readonly cpus: number | null;
|
|
27
|
+
readonly memoryMb: number | null;
|
|
28
|
+
readonly gpuType: string | null;
|
|
29
|
+
readonly gpuCount: number | null;
|
|
30
|
+
readonly diskGb: number | null;
|
|
31
|
+
};
|
|
32
|
+
/** One resource of a computer: its line's key (a GPU's by its type), and how many of its unit each second of the
|
|
33
|
+
* computer's life is (null when its figure is unknown). */
|
|
34
|
+
export type ResourceOf = {
|
|
35
|
+
readonly resource: ComputeResource;
|
|
36
|
+
readonly gpuType: string | null;
|
|
37
|
+
readonly perSecond: number | null;
|
|
38
|
+
};
|
|
39
|
+
/** The resources a computer held: its vCPUs and memory always (a figure no one said is null, never 0); its GPUs when it
|
|
40
|
+
* has any; its disk when its provider said it. */
|
|
41
|
+
export declare function resourcesOf(figures: ComputeFigures): ResourceOf[];
|
|
42
|
+
/** Whether `provider`'s descriptor says it bills nothing (Local's Docker): its time is $0, never by its id alone. */
|
|
43
|
+
export declare const unmetered: (provider: string) => boolean;
|
|
44
|
+
/** The operator's price of `resource` of `provider`, in USD per its unit: a non-negative decimal, $0 for a provider
|
|
45
|
+
* that bills nothing; null when none is set or the setting can't be read (unpriced, never $0). */
|
|
46
|
+
export declare function computeRate(provider: string, resource: ComputeResource, gpuType: string | null, env: NodeJS.ProcessEnv): number | null;
|
|
47
|
+
/** One priced resource of a computer: what a second of its life costs (its rate times its figure), with its words. */
|
|
48
|
+
type Priced = ResourceOf & {
|
|
49
|
+
readonly rate: number;
|
|
50
|
+
readonly usdPerSecond: number;
|
|
51
|
+
};
|
|
52
|
+
/** A computer's priced resources: those with a known figure and a price. The rest are unpriced and held by nothing. */
|
|
53
|
+
export declare function pricedOf(provider: string, figures: ComputeFigures, env: NodeJS.ProcessEnv): Priced[];
|
|
54
|
+
/** How long a stretch is: what the claim holds ahead, and how often it books what ran. */
|
|
55
|
+
export declare const COMPUTE_STRETCH_S = 300;
|
|
56
|
+
/** The tool a computer's time names on the run's ledger (GET /runs/{run_id}/usage reads its bookings as compute lines). */
|
|
57
|
+
export declare const COMPUTE_TOOL = "compute";
|
|
58
|
+
/** The tag of a booking of one resource: its resource, and a GPU's type. */
|
|
59
|
+
export declare const computeTag: (one: Pick<ResourceOf, "resource" | "gpuType">) => string;
|
|
60
|
+
/** The run's ledger as the meter books on it, the run's pause for a refusal of the next stretch, and whether its cap can
|
|
61
|
+
* judge a step now (`judges`, RunLedger.judges: false while the run's spec, whose cap it is, is still built). */
|
|
62
|
+
export type ComputeLedger = {
|
|
63
|
+
readonly paid?: PaidBooking;
|
|
64
|
+
readonly pause?: (refusal: Refusal) => Promise<void>;
|
|
65
|
+
readonly judges?: () => boolean;
|
|
66
|
+
};
|
|
67
|
+
/** A hold-only step on the ledger, given back by `drop` (answered once the ledger has it back). */
|
|
68
|
+
type Hold = {
|
|
69
|
+
drop(): Promise<void>;
|
|
70
|
+
};
|
|
71
|
+
/** A computer the claim refused to create: the cap had no room for its first stretch. Its words are the cap's. */
|
|
72
|
+
export declare class ComputeRefused extends Error {
|
|
73
|
+
readonly code = "spend_cap_reached";
|
|
74
|
+
}
|
|
75
|
+
export interface ComputeMeter {
|
|
76
|
+
/** Before the claim creates a computer of `provider` asking for `figures`: one stretch of its priced resources held on
|
|
77
|
+
* the run's ledger (none where nothing is priced, or with no ledger). Refused for room or for good: ComputeRefused, in
|
|
78
|
+
* the cap's words, and nothing should be made. The hold answered is the caller's to hand to `start` (or `drop`). */
|
|
79
|
+
before(provider: string, figures: ComputeFigures): Promise<Hold | undefined>;
|
|
80
|
+
/** The claim holds computer `providerId` of `provider` from now, with `figures` (its provider's where it said them):
|
|
81
|
+
* its time is booked each stretch, the next one held; `ahead` (before's hold) is let go once the first is. */
|
|
82
|
+
start(provider: string, providerId: string, figures: ComputeFigures, ahead?: Hold): void;
|
|
83
|
+
/** The claim lets the computer go (its delete answered ok, or its run's park kept it): the time since its last
|
|
84
|
+
* booking booked, its hold let go. Once; one the meter never started is nothing. */
|
|
85
|
+
stop(provider: string, providerId: string): Promise<void>;
|
|
86
|
+
/** Every computer it holds let go as `stop` does (the claim's release). */
|
|
87
|
+
stopAll(): Promise<void>;
|
|
88
|
+
}
|
|
89
|
+
export type ComputeMeterOptions = {
|
|
90
|
+
readonly env: NodeJS.ProcessEnv;
|
|
91
|
+
readonly ledger: ComputeLedger;
|
|
92
|
+
/** Told each booked stretch of a computer's time, in ms (its interval's booked time, computer-intervals.ts). */
|
|
93
|
+
readonly booked?: (provider: string, providerId: string, ms: number) => Promise<void>;
|
|
94
|
+
readonly now?: () => number;
|
|
95
|
+
/** Runs `tick` every `ms` until the answer is called; default setInterval, unref'd. */
|
|
96
|
+
readonly every?: (tick: () => void, ms: number) => () => void;
|
|
97
|
+
readonly log?: (line: string) => void;
|
|
98
|
+
};
|
|
99
|
+
/** One claim's meter of the computers it holds (ComputeMeter). */
|
|
100
|
+
export declare function computeMeter(options: ComputeMeterOptions): ComputeMeter;
|
|
101
|
+
export {};
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// Computer time, priced the way providers bill it: by the resources a computer holds, for as long as it holds them. A
|
|
2
|
+
// computer has no size here. Its CPUs, memory, GPUs and disk are the provider's own figures where its answer gives them
|
|
3
|
+
// (CreatedComputer.resources), else what its create asked, else unknown (computer-intervals.ts figuresOf), and the operator
|
|
4
|
+
// sets a price per provider and per resource, as browser time is priced (browser-cost.ts):
|
|
5
|
+
//
|
|
6
|
+
// AGENTRUN_COMPUTE_USD_PER_VCPU_SECOND_<PROVIDER> USD per vCPU-second
|
|
7
|
+
// AGENTRUN_COMPUTE_USD_PER_GIB_SECOND_<PROVIDER> USD per GiB-second of memory
|
|
8
|
+
// AGENTRUN_COMPUTE_USD_PER_GPU_SECOND_<PROVIDER>_<GPU_TYPE> USD per GPU-second, by the provider's own GPU type name
|
|
9
|
+
// AGENTRUN_COMPUTE_USD_PER_DISK_GB_MONTH_<PROVIDER> USD per GB-month of disk (a month: 730 hours)
|
|
10
|
+
//
|
|
11
|
+
// <PROVIDER> and <GPU_TYPE> in capitals, every other character an underscore (daytona, H100 → DAYTONA_H100). No price is
|
|
12
|
+
// built in: a provider whose descriptor bills nothing (metered false) is $0, as is a resource set at 0 (booked at $0, its
|
|
13
|
+
// usage line $0, never `unpriced`), and any other resource with no price set, or
|
|
14
|
+
// with a figure its provider never said, is unpriced: its time is never $0 and never refused. An unpriced resource runs
|
|
15
|
+
// outside the cap, as a tool call with no price does (TOOL_UNMETERED): nothing of it is held or booked, and its usage line
|
|
16
|
+
// says `unpriced`.
|
|
17
|
+
//
|
|
18
|
+
// The cap (computeMeter): a priced computer's time is held on the run's one ledger, under the run's max_cost_usd, one
|
|
19
|
+
// stretch ahead, as browser time is. A claim's first computer is made while the run's spec is built, before its cap can
|
|
20
|
+
// judge: its first stretch is held once the cap can (owed: it is made), so the run's first request is judged beside it,
|
|
21
|
+
// and a cap with no room parks the run there. A computer made once the cap judges (a lost sandbox's new one) has one
|
|
22
|
+
// stretch at what its create asks for held first (a refusal makes nothing, in the cap's words). Each stretch it ran is
|
|
23
|
+
// then booked (owed: its provider is owed it, so the cap never refuses it) and the next stretch held, a refusal of which
|
|
24
|
+
// pauses the run as a browser call's does. Before the run's end or park is written (the spec's beforeRelease: settle),
|
|
25
|
+
// what it ran since is booked and its hold let go, so the end's cost holds it; the moments between that and its delete
|
|
26
|
+
// are no claim's. Time no claim holds (a computer kept
|
|
27
|
+
// past its claim for a follow-up, one deleted after its run ended, one whose delete failed) is booked by none: it is on the
|
|
28
|
+
// usage read (`unheld_quantity`) and in no cap.
|
|
29
|
+
import { PROVIDERS } from "@parcha/agentrun-computer";
|
|
30
|
+
import { paidStep } from "./paid-calls.js";
|
|
31
|
+
import { logLine } from "./print.js";
|
|
32
|
+
/** The unit each resource's time is counted in. */
|
|
33
|
+
export const COMPUTE_UNITS = { vcpu: "vcpu_second", memory: "gib_second", gpu: "gpu_second", disk: "gb_month" };
|
|
34
|
+
/** A month of disk, as providers bill one: 730 hours. */
|
|
35
|
+
export const SECONDS_PER_MONTH = 730 * 3600;
|
|
36
|
+
/** The settings' prefixes, by resource (the provider's name, and a GPU's type, follow in capitals). */
|
|
37
|
+
export const COMPUTE_PRICE_ENV = {
|
|
38
|
+
vcpu: "AGENTRUN_COMPUTE_USD_PER_VCPU_SECOND_",
|
|
39
|
+
memory: "AGENTRUN_COMPUTE_USD_PER_GIB_SECOND_",
|
|
40
|
+
gpu: "AGENTRUN_COMPUTE_USD_PER_GPU_SECOND_",
|
|
41
|
+
disk: "AGENTRUN_COMPUTE_USD_PER_DISK_GB_MONTH_",
|
|
42
|
+
};
|
|
43
|
+
const named = (text) => text.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
44
|
+
/** The setting that prices `resource` of `provider` (a GPU's by its type). */
|
|
45
|
+
export const computePriceEnvOf = (resource, provider, gpuType) => `${COMPUTE_PRICE_ENV[resource]}${named(provider)}${resource === "gpu" ? `_${named(gpuType ?? "")}` : ""}`;
|
|
46
|
+
/** The resources a computer held: its vCPUs and memory always (a figure no one said is null, never 0); its GPUs when it
|
|
47
|
+
* has any; its disk when its provider said it. */
|
|
48
|
+
export function resourcesOf(figures) {
|
|
49
|
+
const gpu = figures.gpuType !== null || figures.gpuCount !== null;
|
|
50
|
+
return [
|
|
51
|
+
{ resource: "vcpu", gpuType: null, perSecond: figures.cpus },
|
|
52
|
+
{ resource: "memory", gpuType: null, perSecond: figures.memoryMb === null ? null : figures.memoryMb / 1024 },
|
|
53
|
+
...(gpu ? [{ resource: "gpu", gpuType: figures.gpuType, perSecond: figures.gpuType === null ? null : figures.gpuCount }] : []),
|
|
54
|
+
...(figures.diskGb !== null ? [{ resource: "disk", gpuType: null, perSecond: figures.diskGb / SECONDS_PER_MONTH }] : []),
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
/** Whether `provider`'s descriptor says it bills nothing (Local's Docker): its time is $0, never by its id alone. */
|
|
58
|
+
export const unmetered = (provider) => PROVIDERS.some((entry) => entry.id === provider && !entry.metered);
|
|
59
|
+
/** The operator's price of `resource` of `provider`, in USD per its unit: a non-negative decimal, $0 for a provider
|
|
60
|
+
* that bills nothing; null when none is set or the setting can't be read (unpriced, never $0). */
|
|
61
|
+
export function computeRate(provider, resource, gpuType, env) {
|
|
62
|
+
if (unmetered(provider))
|
|
63
|
+
return 0;
|
|
64
|
+
if (resource === "gpu" && gpuType === null)
|
|
65
|
+
return null;
|
|
66
|
+
const text = env[computePriceEnvOf(resource, provider, gpuType)]?.trim();
|
|
67
|
+
if (!text || !/^(\d+(\.\d*)?|\.\d+)$/.test(text))
|
|
68
|
+
return null;
|
|
69
|
+
const rate = Number(text);
|
|
70
|
+
return Number.isFinite(rate) ? rate : null;
|
|
71
|
+
}
|
|
72
|
+
/** A computer's priced resources: those with a known figure and a price. The rest are unpriced and held by nothing. */
|
|
73
|
+
export function pricedOf(provider, figures, env) {
|
|
74
|
+
return resourcesOf(figures).flatMap((one) => {
|
|
75
|
+
const rate = computeRate(provider, one.resource, one.gpuType, env);
|
|
76
|
+
return rate === null || one.perSecond === null ? [] : [{ ...one, rate, usdPerSecond: rate * one.perSecond }];
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/** How long a stretch is: what the claim holds ahead, and how often it books what ran. */
|
|
80
|
+
export const COMPUTE_STRETCH_S = 300;
|
|
81
|
+
/** The tool a computer's time names on the run's ledger (GET /runs/{run_id}/usage reads its bookings as compute lines). */
|
|
82
|
+
export const COMPUTE_TOOL = "compute";
|
|
83
|
+
/** The tag of a booking of one resource: its resource, and a GPU's type. */
|
|
84
|
+
export const computeTag = (one) => (one.resource === "gpu" ? `gpu:${one.gpuType ?? ""}` : one.resource);
|
|
85
|
+
/** Up to the millionth of a dollar, so the cap's words name a figure never below the hold. */
|
|
86
|
+
const microUsd = (usd) => Math.ceil(Math.round(usd * 1e12) / 1e6) / 1e6;
|
|
87
|
+
/** A computer the claim refused to create: the cap had no room for its first stretch. Its words are the cap's. */
|
|
88
|
+
export class ComputeRefused extends Error {
|
|
89
|
+
code = "spend_cap_reached";
|
|
90
|
+
}
|
|
91
|
+
const everyDefault = (tick, ms) => {
|
|
92
|
+
const timer = setInterval(tick, ms);
|
|
93
|
+
timer.unref?.();
|
|
94
|
+
return () => clearInterval(timer);
|
|
95
|
+
};
|
|
96
|
+
/** One claim's meter of the computers it holds (ComputeMeter). */
|
|
97
|
+
export function computeMeter(options) {
|
|
98
|
+
const { env, ledger } = options;
|
|
99
|
+
const now = options.now ?? Date.now;
|
|
100
|
+
const every = options.every ?? everyDefault;
|
|
101
|
+
const log = options.log ?? ((line) => void process.stderr.write(`agentrun-serve runner: ${line}\n`));
|
|
102
|
+
let seq = 0;
|
|
103
|
+
/** A hold of `usd` for `provider`'s next stretch: admitted (refused in the cap's words, with its refusal); `owed`, time
|
|
104
|
+
* its provider is already owed (a computer already made), never refused. */
|
|
105
|
+
const holdOf = async (provider, usd, basis, given = {}) => {
|
|
106
|
+
const paid = ledger.paid;
|
|
107
|
+
// Nothing to hold: no ledger, or a stretch of resources whose operator set them at $0.
|
|
108
|
+
if (!paid || usd === 0)
|
|
109
|
+
return { hold: { drop: async () => { } } };
|
|
110
|
+
const id = `compute:${provider}#hold-${(seq += 1)}`;
|
|
111
|
+
try {
|
|
112
|
+
await paid.admit({ id, name: `${provider}'s computer time`, holdUsd: usd, basis, chargedBy: provider, tool: COMPUTE_TOOL, hold: true, ...(given.owed ? { owed: true } : {}) });
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
const capped = error?.code === "spend_cap_reached";
|
|
116
|
+
const given = error;
|
|
117
|
+
const refused = capped ? String(error.message) : `${provider}'s computer time could not be held: ${String(error?.message ?? error)}`;
|
|
118
|
+
return { refused, ...(capped ? { refusal: { refuse: refused, code: "spend_cap_reached", forGood: given.forGood ?? true, ...(given.figures ? { figures: given.figures } : {}) } } : {}) };
|
|
119
|
+
}
|
|
120
|
+
let dropped;
|
|
121
|
+
return { hold: { drop: () => (dropped ??= paid.settle(id, 0, { givenBack: true }).then(() => undefined, () => undefined)) } };
|
|
122
|
+
};
|
|
123
|
+
/** Whether the run's cap can judge a step now: false while the run's spec, whose cap it is, is built. */
|
|
124
|
+
const judging = () => ledger.judges?.() !== false;
|
|
125
|
+
const stretchUsd = (priced) => microUsd(priced.reduce((sum, one) => sum + one.usdPerSecond, 0) * COMPUTE_STRETCH_S);
|
|
126
|
+
const basisOf = (provider, priced) => `${provider}'s computer time for its next ${COMPUTE_STRETCH_S} s: ${priced.map((one) => `${one.perSecond} ${COMPUTE_UNITS[one.resource].replace("_second", "")}${one.gpuType ? ` ${one.gpuType}` : ""} at $${one.rate} a ${COMPUTE_UNITS[one.resource].replace(/^[a-z]+_/, "")}`).join(", ")}`;
|
|
127
|
+
const held = new Map();
|
|
128
|
+
const key = (provider, providerId) => `${provider}\u0000${providerId}`;
|
|
129
|
+
/** The time `one` ran since its last booking, up to `to`, booked on the ledger: one owed step per priced resource, each
|
|
130
|
+
* named by its computer, resource and time (ref), so it is booked once; then its interval's booked time. */
|
|
131
|
+
const book = async (one, to) => {
|
|
132
|
+
// A cap that can't judge yet would keep this booking waiting on the run's spec, which may be what is let go now (a
|
|
133
|
+
// computer deleted because its spec's build failed): nothing is booked, and its time is the run's unheld time.
|
|
134
|
+
if (!judging())
|
|
135
|
+
return;
|
|
136
|
+
const from = one.from;
|
|
137
|
+
const ms = Math.max(0, Math.floor(to - from));
|
|
138
|
+
one.from = from + ms;
|
|
139
|
+
if (ms === 0 || one.priced.length === 0)
|
|
140
|
+
return;
|
|
141
|
+
const paid = ledger.paid;
|
|
142
|
+
if (paid) {
|
|
143
|
+
await Promise.all(one.priced.map(async (resource) => {
|
|
144
|
+
const usd = resource.usdPerSecond * (ms / 1000);
|
|
145
|
+
const tag = computeTag(resource);
|
|
146
|
+
const stepped = await paidStep(paid, undefined, { name: `${one.provider}'s computer time (${tag})`, holdUsd: usd, basis: `${ms / 1000} s of ${resource.perSecond} ${COMPUTE_UNITS[resource.resource]} at $${resource.rate}`, by: one.provider, tool: COMPUTE_TOOL, tag, owed: true, ref: `compute:${one.provider}:${one.providerId}:${tag}@${from}-${from + ms}` }, async () => undefined, () => usd).catch((error) => ({ refused: String(error?.message ?? error) }));
|
|
147
|
+
if ("refused" in stepped)
|
|
148
|
+
log(logLine("compute_spend_unbooked", { said: `${ms / 1000} s of ${one.provider} ${one.providerId}'s ${tag} time was not booked on the run's ledger (${stepped.refused.slice(0, 200)})` }));
|
|
149
|
+
}));
|
|
150
|
+
}
|
|
151
|
+
await options.booked?.(one.provider, one.providerId, ms).catch(() => undefined);
|
|
152
|
+
};
|
|
153
|
+
/** Each stretch: what ran booked, the next stretch held (a refusal pauses the run, once until a hold is taken again). */
|
|
154
|
+
const tick = async (one) => {
|
|
155
|
+
// A stretch that passes while the run's spec is still built (a long image build) waits for the next: what ran is
|
|
156
|
+
// booked from where it was then.
|
|
157
|
+
if (!judging())
|
|
158
|
+
return;
|
|
159
|
+
await book(one, now());
|
|
160
|
+
// The stretch just booked is no longer ahead: its hold is given back, and the ledger has it back, before the next
|
|
161
|
+
// stretch is asked for. Else the cap would count that stretch twice (booked and still held), and refuse a next one
|
|
162
|
+
// it has room for.
|
|
163
|
+
const last = one.hold;
|
|
164
|
+
one.hold = undefined;
|
|
165
|
+
await last?.drop();
|
|
166
|
+
const next = await holdOf(one.provider, stretchUsd(one.priced), basisOf(one.provider, one.priced));
|
|
167
|
+
if ("hold" in next) {
|
|
168
|
+
if (one.stopped)
|
|
169
|
+
await next.hold.drop();
|
|
170
|
+
else
|
|
171
|
+
one.hold = next.hold;
|
|
172
|
+
// Held again (the run's cap was raised, or room came back): a later refusal pauses the run again.
|
|
173
|
+
one.paused = false;
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (!one.paused && next.refusal && ledger.pause) {
|
|
177
|
+
one.paused = true;
|
|
178
|
+
await ledger.pause(next.refusal).catch(() => undefined);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
const stop = (provider, providerId) => {
|
|
182
|
+
const one = held.get(key(provider, providerId));
|
|
183
|
+
if (!one)
|
|
184
|
+
return Promise.resolve();
|
|
185
|
+
return (one.stopped ??= (async () => {
|
|
186
|
+
one.cancel();
|
|
187
|
+
await one.ticking;
|
|
188
|
+
await book(one, now());
|
|
189
|
+
const last = one.hold;
|
|
190
|
+
one.hold = undefined;
|
|
191
|
+
await last?.drop();
|
|
192
|
+
held.delete(key(provider, providerId));
|
|
193
|
+
})());
|
|
194
|
+
};
|
|
195
|
+
return {
|
|
196
|
+
async before(provider, figures) {
|
|
197
|
+
const priced = pricedOf(provider, figures, env);
|
|
198
|
+
const usd = stretchUsd(priced);
|
|
199
|
+
// A cap that can't judge yet (the claim's first computer, made while the run's spec is built) holds nothing ahead:
|
|
200
|
+
// the computer's first stretch is held, owed, once it can (start), before the run's first request is judged.
|
|
201
|
+
if (!ledger.paid || usd === 0 || ledger.judges?.() === false)
|
|
202
|
+
return undefined;
|
|
203
|
+
const held = await holdOf(provider, usd, basisOf(provider, priced));
|
|
204
|
+
if ("refused" in held)
|
|
205
|
+
throw new ComputeRefused(held.refused);
|
|
206
|
+
return held.hold;
|
|
207
|
+
},
|
|
208
|
+
start(provider, providerId, figures, ahead) {
|
|
209
|
+
const priced = pricedOf(provider, figures, env);
|
|
210
|
+
const k = key(provider, providerId);
|
|
211
|
+
// A computer with nothing priced is held and booked by nothing (its time unpriced), as is a provider that bills
|
|
212
|
+
// nothing. One whose resources the operator priced at $0 is booked at $0 each stretch, so its usage read says $0
|
|
213
|
+
// (never `unpriced`): nothing of it is held.
|
|
214
|
+
if (held.has(k) || priced.length === 0 || unmetered(provider)) {
|
|
215
|
+
void ahead?.drop();
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const one = { provider, providerId, priced, from: now(), hold: undefined, cancel: () => { }, ticking: Promise.resolve(), paused: false };
|
|
219
|
+
held.set(k, one);
|
|
220
|
+
// Its first stretch held at what it was made with (owed: it is made, so never refused), then the hold taken before
|
|
221
|
+
// its create let go. The stretch's ticks wait for it, so a tick never drops a hold this has not set yet. Made
|
|
222
|
+
// while the run's spec is built, the hold waits for the spec's cap: then nothing waits on it (a stop while the spec
|
|
223
|
+
// fails would wait forever), and the hold, once the cap can judge, is let go at once if the computer was stopped.
|
|
224
|
+
const first = holdOf(provider, stretchUsd(priced), basisOf(provider, priced), { owed: true }).then((answer) => {
|
|
225
|
+
if ("hold" in answer) {
|
|
226
|
+
if (one.stopped)
|
|
227
|
+
void answer.hold.drop();
|
|
228
|
+
else
|
|
229
|
+
one.hold = answer.hold;
|
|
230
|
+
}
|
|
231
|
+
void ahead?.drop();
|
|
232
|
+
});
|
|
233
|
+
if (judging())
|
|
234
|
+
one.ticking = first;
|
|
235
|
+
else
|
|
236
|
+
void first.catch(() => undefined);
|
|
237
|
+
const cancel = every(() => {
|
|
238
|
+
one.ticking = one.ticking.then(() => (one.stopped ? undefined : tick(one))).catch(() => undefined);
|
|
239
|
+
}, COMPUTE_STRETCH_S * 1000);
|
|
240
|
+
one.cancel = () => {
|
|
241
|
+
cancel();
|
|
242
|
+
one.cancel = () => { };
|
|
243
|
+
};
|
|
244
|
+
},
|
|
245
|
+
stop,
|
|
246
|
+
async stopAll() {
|
|
247
|
+
await Promise.all([...held.values()].map((one) => stop(one.provider, one.providerId)));
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
}
|