@parcha/agentrun-serve 0.0.0-bootstrap.0 → 0.1.0-beta.29
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 +560 -0
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +1724 -0
- package/dist/adapters.d.ts +205 -0
- package/dist/adapters.js +459 -0
- package/dist/agents.d.ts +124 -0
- package/dist/agents.js +302 -0
- package/dist/app.d.ts +7 -0
- package/dist/app.js +324 -0
- package/dist/bound-files.d.ts +4 -0
- package/dist/bound-files.js +164 -0
- package/dist/box/record.d.ts +6 -0
- package/dist/box/record.js +41 -0
- package/dist/box/sweep.d.ts +53 -0
- package/dist/box/sweep.js +193 -0
- package/dist/box-image.d.ts +98 -0
- package/dist/box-image.js +283 -0
- package/dist/box-relay.d.ts +42 -0
- package/dist/box-relay.js +222 -0
- package/dist/boxes.d.ts +54 -0
- package/dist/boxes.js +182 -0
- package/dist/cap.d.ts +224 -0
- package/dist/cap.js +721 -0
- package/dist/catalog.d.ts +20 -0
- package/dist/catalog.js +53 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +167 -0
- package/dist/coding-tools.d.ts +31 -0
- package/dist/coding-tools.js +81 -0
- package/dist/console/THIRD-PARTY-NOTICES.txt +1141 -0
- package/dist/console/assets/index-BN7oVcO4.js +445 -0
- package/dist/console/assets/index-By-jkqki.css +1 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-400-normal-BEIGL1Tu.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-400-normal-CIEtRijs.woff +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-600-normal-BB5xRk30.woff +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-600-normal-EVf6-Yzo.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-ext-400-normal-BHyURHCj.woff +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-ext-400-normal-C_uLvvQ5.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-ext-600-normal-Dj6eZy5f.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-ext-600-normal-DpeVePZf.woff +0 -0
- package/dist/console/assets/jetbrains-mono-greek-400-normal-C190GLew.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-greek-400-normal-CNojz0t3.woff +0 -0
- package/dist/console/assets/jetbrains-mono-greek-600-normal-3KAm9r92.woff +0 -0
- package/dist/console/assets/jetbrains-mono-greek-600-normal-H7WoG9Et.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-400-normal-B11XCQ5g.woff +0 -0
- package/dist/console/assets/jetbrains-mono-latin-400-normal-V6pRDFza.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-600-normal-C8RAYTDA.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-600-normal-DXv3JcnD.woff +0 -0
- package/dist/console/assets/jetbrains-mono-latin-ext-400-normal-Bc8Ftmh3.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-ext-400-normal-DHMOLxAi.woff +0 -0
- package/dist/console/assets/jetbrains-mono-latin-ext-600-normal-BfB_LPfz.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-ext-600-normal-DiQlUoE6.woff +0 -0
- package/dist/console/assets/jetbrains-mono-vietnamese-400-normal-BeU3T8wf.woff +0 -0
- package/dist/console/assets/jetbrains-mono-vietnamese-400-normal-ByoDsISC.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-vietnamese-600-normal-CSpzN4cI.woff +0 -0
- package/dist/console/assets/jetbrains-mono-vietnamese-600-normal-Dtizs43-.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-400-normal-Cph9ettZ.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-400-normal-DfGZKnlb.woff +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-600-normal-BGBUNdzT.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-600-normal-COje25QW.woff +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-ext-400-normal-4l4lq8zr.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-ext-400-normal-BGzXWJeR.woff +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-ext-600-normal-8_E36CQj.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-ext-600-normal-C27an7Zc.woff +0 -0
- package/dist/console/index.html +15 -0
- package/dist/daytona-api.d.ts +21 -0
- package/dist/daytona-api.js +101 -0
- package/dist/decision-rows.d.ts +11 -0
- package/dist/decision-rows.js +116 -0
- package/dist/decisions.d.ts +94 -0
- package/dist/decisions.js +184 -0
- package/dist/effort-guidance.d.ts +5 -0
- package/dist/effort-guidance.js +14 -0
- package/dist/environment.d.ts +5 -0
- package/dist/environment.js +11 -0
- package/dist/errors.d.ts +72 -0
- package/dist/errors.js +66 -0
- package/dist/exa-keyless.d.ts +35 -0
- package/dist/exa-keyless.js +202 -0
- package/dist/fetch-page.d.ts +114 -0
- package/dist/fetch-page.js +182 -0
- package/dist/files.d.ts +60 -0
- package/dist/files.js +153 -0
- package/dist/grade.d.ts +96 -0
- package/dist/grade.js +348 -0
- package/dist/graph-view.d.ts +100 -0
- package/dist/graph-view.js +212 -0
- package/dist/harness-app.d.ts +38 -0
- package/dist/harness-app.js +626 -0
- package/dist/harness.d.ts +53 -0
- package/dist/harness.js +74 -0
- package/dist/home-path.d.ts +27 -0
- package/dist/home-path.js +78 -0
- package/dist/host-tools.d.ts +20 -0
- package/dist/host-tools.js +140 -0
- package/dist/host.d.ts +39 -0
- package/dist/host.js +81 -0
- package/dist/hosted-search.d.ts +27 -0
- package/dist/hosted-search.js +129 -0
- package/dist/import-sources.d.ts +104 -0
- package/dist/import-sources.js +629 -0
- package/dist/import-summary.d.ts +25 -0
- package/dist/import-summary.js +75 -0
- package/dist/import-worker.d.ts +26 -0
- package/dist/import-worker.js +41 -0
- package/dist/import.d.ts +145 -0
- package/dist/import.js +1221 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +6 -0
- package/dist/key-check.d.ts +36 -0
- package/dist/key-check.js +108 -0
- package/dist/key-refusal.d.ts +51 -0
- package/dist/key-refusal.js +187 -0
- package/dist/learn/check.d.ts +99 -0
- package/dist/learn/check.js +259 -0
- package/dist/learn/grade.d.ts +79 -0
- package/dist/learn/grade.js +433 -0
- package/dist/learn/index.d.ts +7 -0
- package/dist/learn/index.js +11 -0
- package/dist/learn/invisible.d.ts +5 -0
- package/dist/learn/invisible.js +42 -0
- package/dist/learn/notes.d.ts +59 -0
- package/dist/learn/notes.js +146 -0
- package/dist/learn/reflect.d.ts +49 -0
- package/dist/learn/reflect.js +167 -0
- package/dist/learn/secrets.d.ts +18 -0
- package/dist/learn/secrets.js +111 -0
- package/dist/learn/skills.d.ts +84 -0
- package/dist/learn/skills.js +198 -0
- package/dist/learning-routes.d.ts +196 -0
- package/dist/learning-routes.js +609 -0
- package/dist/learning-run.d.ts +304 -0
- package/dist/learning-run.js +797 -0
- package/dist/learning-words.d.ts +73 -0
- package/dist/learning-words.js +171 -0
- package/dist/learning.d.ts +377 -0
- package/dist/learning.js +801 -0
- package/dist/llm.d.ts +40 -0
- package/dist/llm.js +37 -0
- package/dist/llms.txt +37 -0
- package/dist/mcp-servers.d.ts +115 -0
- package/dist/mcp-servers.js +445 -0
- package/dist/mcp.d.ts +51 -0
- package/dist/mcp.js +392 -0
- package/dist/model.d.ts +184 -0
- package/dist/model.js +484 -0
- package/dist/monid.d.ts +74 -0
- package/dist/monid.js +277 -0
- package/dist/openapi/agentrun-v2.openapi.json +4805 -0
- package/dist/openapi.d.ts +10 -0
- package/dist/openapi.js +1417 -0
- package/dist/owner.d.ts +55 -0
- package/dist/owner.js +222 -0
- package/dist/packs.d.ts +71 -0
- package/dist/packs.js +180 -0
- package/dist/page.d.ts +32 -0
- package/dist/page.js +133 -0
- package/dist/paid-calls.d.ts +46 -0
- package/dist/paid-calls.js +110 -0
- package/dist/pause.d.ts +68 -0
- package/dist/pause.js +97 -0
- package/dist/pdf-pages.d.ts +44 -0
- package/dist/pdf-pages.js +147 -0
- package/dist/place.d.ts +46 -0
- package/dist/place.js +105 -0
- package/dist/print.d.ts +20 -0
- package/dist/print.js +29 -0
- package/dist/propose.d.ts +38 -0
- package/dist/propose.js +82 -0
- package/dist/quiet-sqlite-warning.d.ts +1 -0
- package/dist/quiet-sqlite-warning.js +14 -0
- package/dist/request-start.d.ts +14 -0
- package/dist/request-start.js +226 -0
- package/dist/result.d.ts +3 -0
- package/dist/result.js +5 -0
- package/dist/rows.d.ts +46 -0
- package/dist/rows.js +121 -0
- package/dist/run-box.d.ts +134 -0
- package/dist/run-box.js +440 -0
- package/dist/run-manifest.d.ts +27 -0
- package/dist/run-manifest.js +60 -0
- package/dist/runnable.d.ts +80 -0
- package/dist/runnable.js +162 -0
- package/dist/runs.d.ts +840 -0
- package/dist/runs.js +2279 -0
- package/dist/secret-cut.d.ts +27 -0
- package/dist/secret-cut.js +109 -0
- package/dist/secrets.d.ts +50 -0
- package/dist/secrets.js +140 -0
- package/dist/server-lock.d.ts +44 -0
- package/dist/server-lock.js +140 -0
- package/dist/server.d.ts +626 -0
- package/dist/server.js +1756 -0
- package/dist/sessions.d.ts +4 -0
- package/dist/sessions.js +105 -0
- package/dist/skills.d.ts +31 -0
- package/dist/skills.js +68 -0
- package/dist/stats.d.ts +101 -0
- package/dist/stats.js +82 -0
- package/dist/tool-keys.d.ts +151 -0
- package/dist/tool-keys.js +273 -0
- package/dist/tool-plane.d.ts +143 -0
- package/dist/tool-plane.js +242 -0
- package/dist/tools.d.ts +147 -0
- package/dist/tools.js +253 -0
- package/dist/turn-end.d.ts +23 -0
- package/dist/turn-end.js +14 -0
- package/dist/turns.d.ts +34 -0
- package/dist/turns.js +202 -0
- package/dist/uploads.d.ts +104 -0
- package/dist/uploads.js +474 -0
- package/dist/usage.d.ts +29 -0
- package/dist/usage.js +58 -0
- package/dist/validate.d.ts +55 -0
- package/dist/validate.js +175 -0
- package/dist/web-fetch.d.ts +64 -0
- package/dist/web-fetch.js +262 -0
- package/dist/web-search.d.ts +54 -0
- package/dist/web-search.js +136 -0
- package/dist/webhooks.d.ts +182 -0
- package/dist/webhooks.js +487 -0
- package/dist/workspace-paths.d.ts +33 -0
- package/dist/workspace-paths.js +104 -0
- package/dist/workspace.d.ts +88 -0
- package/dist/workspace.js +369 -0
- package/launcher/agentrun-serve +109 -0
- package/package.json +96 -1
- package/skills/agentrun-serve/SKILL.md +128 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export declare const HARNESSES: readonly ["pi", "claude-agent", "codex"];
|
|
2
|
+
export type HarnessId = (typeof HARNESSES)[number];
|
|
3
|
+
export declare const DEFAULT_HARNESS: HarnessId;
|
|
4
|
+
/** The memory one run of each harness reserves while its process lives, in MiB: what admission counts (host.ts).
|
|
5
|
+
* Measured 2026-11-03 on 529c3882e with the pinned binaries' sha256 checked a chunk at a time, not read whole: a
|
|
6
|
+
* real server in a cgroup of its own (MemoryMax 2048M, no swap), models on a loopback stand-in, one agent with read
|
|
7
|
+
* and write asked to write a file, read it back and answer; the cgroup's anon (memory.stat) sampled every 100 ms. A
|
|
8
|
+
* run's size is (the most anon with the runs going − the server's own after boot) / n, for n = 1 and n = 3, twice
|
|
9
|
+
* each, and the largest of the four is kept. The server's own growth while it serves the runs is in it.
|
|
10
|
+
* pi: n=1 106, 106; n=3 96, 99 MB a run → 128 (+21%)
|
|
11
|
+
* codex: n=1 132, 143; n=3 120, 131 MB a run → 192 (+34%)
|
|
12
|
+
* claude-agent: n=1 215, 208; n=3 211, 208 MB a run → 288 (+34%)
|
|
13
|
+
* The margin covers a longer conversation than the probe's three steps; the CLI harnesses (codex, claude-agent) grow
|
|
14
|
+
* more with one, so theirs is larger. At a 512M limit the same build completed pi x3, codex x2 and claude-agent x1
|
|
15
|
+
* (and x2). A check that reads a binary whole adds its size to the server and to each run (294 MB for codex, 230 MB
|
|
16
|
+
* for the claude-agent CLI), and these sizes do not cover it. The faux model and the faux
|
|
17
|
+
* harness reserve the size of the harness they stand in for, so tests admit as a real server does. */
|
|
18
|
+
export declare const RUN_MIB: Readonly<Record<HarnessId, number>>;
|
|
19
|
+
/** One harness as capabilities lists it: whether this server runs it, and when not, why; and whether it takes a message
|
|
20
|
+
* mid-turn (`steer`), or queues it for the turn's end. */
|
|
21
|
+
/** `default_model`: the model this server runs for the harness when a definition names none (`provider/model-id`), or
|
|
22
|
+
* null when it runs none there; harnessOffers sets it. `keys`, on one unavailable only for want of a model key: the keys
|
|
23
|
+
* any one of which, saved, lets it run here, so a key's row names it before it is saved (tool-keys.ts' rowUnlocks).
|
|
24
|
+
* The server's own: capabilities never serve it. */
|
|
25
|
+
export type HarnessOffer = {
|
|
26
|
+
id: HarnessId;
|
|
27
|
+
available: boolean;
|
|
28
|
+
steer: boolean;
|
|
29
|
+
reason?: string;
|
|
30
|
+
default_model?: string | null;
|
|
31
|
+
keys?: readonly string[];
|
|
32
|
+
};
|
|
33
|
+
/** A harness as capabilities list it: an offer with its `default_model` resolved (harnessOffers), never left out, and
|
|
34
|
+
* the memory a run of it reserves (`run_memory_mib`). */
|
|
35
|
+
export type HarnessCapability = HarnessOffer & {
|
|
36
|
+
default_model: string | null;
|
|
37
|
+
run_memory_mib: number;
|
|
38
|
+
};
|
|
39
|
+
/** Whether a harness takes a message mid-turn: pi does; another harness's run queues it for the turn's end. */
|
|
40
|
+
export declare const steersMidTurn: (id: string) => boolean;
|
|
41
|
+
/** A module whose `script(spec)` scripts run-adapters' faux harness: with it, every harness but pi runs on that faux
|
|
42
|
+
* harness, as `--faux` runs pi on the faux model. */
|
|
43
|
+
export declare const FAUX_HARNESS_ENV = "AGENTRUN_SERVE_FAUX_HARNESS";
|
|
44
|
+
/** What this server offers of each harness. */
|
|
45
|
+
export declare function harnessesOf(env: NodeJS.ProcessEnv): HarnessOffer[];
|
|
46
|
+
/** What this server offers of each harness, checked once per environment: each adapter's package, pins and endpoint,
|
|
47
|
+
* on a model of the harness's own provider (adapters.ts), and whether one of its runs fits in this host's memory at all
|
|
48
|
+
* (host.ts). Whether a revision's model fits its harness is that revision's (runnable.ts). */
|
|
49
|
+
export declare function harnessOffers(env: NodeJS.ProcessEnv): Promise<HarnessCapability[]>;
|
|
50
|
+
/** The harness a stored body names, as answers show it. */
|
|
51
|
+
export declare const harnessOfBody: (body: {
|
|
52
|
+
harness?: string;
|
|
53
|
+
}) => string;
|
package/dist/harness.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// The harness that runs a revision: pi (pi-durable-disk's, the default and the one a revision that names none gets) or
|
|
2
|
+
// one of the run adapters. A revision keeps the harness it names; the default is never stored, so a revision written
|
|
3
|
+
// before this field existed and one that names pi are the same revision. Which harnesses this server runs is said in
|
|
4
|
+
// capabilities, and a revision naming one it does not run saves with a warning and is refused at run time.
|
|
5
|
+
export const HARNESSES = ["pi", "claude-agent", "codex"];
|
|
6
|
+
export const DEFAULT_HARNESS = "pi";
|
|
7
|
+
/** The memory one run of each harness reserves while its process lives, in MiB: what admission counts (host.ts).
|
|
8
|
+
* Measured 2026-11-03 on 529c3882e with the pinned binaries' sha256 checked a chunk at a time, not read whole: a
|
|
9
|
+
* real server in a cgroup of its own (MemoryMax 2048M, no swap), models on a loopback stand-in, one agent with read
|
|
10
|
+
* and write asked to write a file, read it back and answer; the cgroup's anon (memory.stat) sampled every 100 ms. A
|
|
11
|
+
* run's size is (the most anon with the runs going − the server's own after boot) / n, for n = 1 and n = 3, twice
|
|
12
|
+
* each, and the largest of the four is kept. The server's own growth while it serves the runs is in it.
|
|
13
|
+
* pi: n=1 106, 106; n=3 96, 99 MB a run → 128 (+21%)
|
|
14
|
+
* codex: n=1 132, 143; n=3 120, 131 MB a run → 192 (+34%)
|
|
15
|
+
* claude-agent: n=1 215, 208; n=3 211, 208 MB a run → 288 (+34%)
|
|
16
|
+
* The margin covers a longer conversation than the probe's three steps; the CLI harnesses (codex, claude-agent) grow
|
|
17
|
+
* more with one, so theirs is larger. At a 512M limit the same build completed pi x3, codex x2 and claude-agent x1
|
|
18
|
+
* (and x2). A check that reads a binary whole adds its size to the server and to each run (294 MB for codex, 230 MB
|
|
19
|
+
* for the claude-agent CLI), and these sizes do not cover it. The faux model and the faux
|
|
20
|
+
* harness reserve the size of the harness they stand in for, so tests admit as a real server does. */
|
|
21
|
+
export const RUN_MIB = { pi: 128, "claude-agent": 288, codex: 192 };
|
|
22
|
+
/** Whether a harness takes a message mid-turn: pi does; another harness's run queues it for the turn's end. */
|
|
23
|
+
export const steersMidTurn = (id) => id === DEFAULT_HARNESS;
|
|
24
|
+
/** A module whose `script(spec)` scripts run-adapters' faux harness: with it, every harness but pi runs on that faux
|
|
25
|
+
* harness, as `--faux` runs pi on the faux model. */
|
|
26
|
+
export const FAUX_HARNESS_ENV = "AGENTRUN_SERVE_FAUX_HARNESS";
|
|
27
|
+
/** What this server offers of each harness. */
|
|
28
|
+
export function harnessesOf(env) {
|
|
29
|
+
return HARNESSES.map((id) => (id === DEFAULT_HARNESS || env[FAUX_HARNESS_ENV] ? { id, available: true, steer: steersMidTurn(id) } : { id, available: false, steer: steersMidTurn(id), reason: `This server does not run ${id} yet.` }));
|
|
30
|
+
}
|
|
31
|
+
const offered = new WeakMap();
|
|
32
|
+
/** What this server offers of each harness, checked once per environment: each adapter's package, pins and endpoint,
|
|
33
|
+
* on a model of the harness's own provider (adapters.ts), and whether one of its runs fits in this host's memory at all
|
|
34
|
+
* (host.ts). Whether a revision's model fits its harness is that revision's (runnable.ts). */
|
|
35
|
+
export function harnessOffers(env) {
|
|
36
|
+
let offers = offered.get(env);
|
|
37
|
+
if (!offers) {
|
|
38
|
+
offers = (async () => {
|
|
39
|
+
const { hostMemory, neverFits } = await import("./host.js");
|
|
40
|
+
const memory = hostMemory();
|
|
41
|
+
const probed = env[FAUX_HARNESS_ENV] ? harnessesOf(env) : await (async () => {
|
|
42
|
+
const { harnessRefusal } = await import("./adapters.js");
|
|
43
|
+
return Promise.all(HARNESSES.map(async (id) => {
|
|
44
|
+
if (id === DEFAULT_HARNESS)
|
|
45
|
+
return { id, available: true, steer: true };
|
|
46
|
+
// The start's probe: these offers hold until a restart (server.ts), so the reason says so where installing fixes it.
|
|
47
|
+
const refused = await harnessRefusal(id, env, undefined, { atStart: true });
|
|
48
|
+
return refused === undefined ? { id, available: true, steer: steersMidTurn(id) } : { id, available: false, steer: steersMidTurn(id), ...refused };
|
|
49
|
+
}));
|
|
50
|
+
})();
|
|
51
|
+
// A harness that runs here but whose one run is more than this host's memory for runs never starts: said here, so
|
|
52
|
+
// a save warns and a run is refused at once with why (runnable.ts), never a 429 to try again. One that lacks only a
|
|
53
|
+
// key keeps its reason, but no key would run it: it has no `keys`.
|
|
54
|
+
const base = probed.map((offer) => {
|
|
55
|
+
const tooBig = offer.available || offer.keys ? neverFits(offer.id, memory) : undefined;
|
|
56
|
+
if (!tooBig)
|
|
57
|
+
return offer;
|
|
58
|
+
const { keys: _keys, ...rest } = offer;
|
|
59
|
+
return offer.available ? { ...rest, available: false, reason: tooBig } : rest;
|
|
60
|
+
});
|
|
61
|
+
// Each harness's model when a definition names none, as a save and a run resolve it (runnable.ts): none for a
|
|
62
|
+
// harness that does not run here, or that no model here would run on.
|
|
63
|
+
const { harnessModel } = await import("./runnable.js");
|
|
64
|
+
return Promise.all(base.map(async (offer) => {
|
|
65
|
+
const resolved = offer.available ? await harnessModel(env, offer.id, undefined, true) : undefined;
|
|
66
|
+
return { ...offer, default_model: resolved && "model" in resolved ? `${resolved.model.provider}/${resolved.model.modelId}` : null, run_memory_mib: RUN_MIB[offer.id] };
|
|
67
|
+
}));
|
|
68
|
+
})();
|
|
69
|
+
offered.set(env, offers);
|
|
70
|
+
}
|
|
71
|
+
return offers;
|
|
72
|
+
}
|
|
73
|
+
/** The harness a stored body names, as answers show it. */
|
|
74
|
+
export const harnessOfBody = (body) => body.harness ?? DEFAULT_HARNESS;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { BigIntStats } from "node:fs";
|
|
2
|
+
import { ApiError } from "./errors.js";
|
|
3
|
+
/** A file path as the URL sends it, each segment percent-decoded. */
|
|
4
|
+
export declare function decodedPath(raw: string): string;
|
|
5
|
+
/** `rel`, refused unless it is a relative path of names; a segment that starts with . is `own`'s. */
|
|
6
|
+
export declare function checkedPath(rel: string, own: string): string;
|
|
7
|
+
/** `name` as a disk that folds names could take it: its compatibility form, upper then lower case, and without combining
|
|
8
|
+
* marks, so ı, İ, ſ, the Kelvin sign and fullwidth letters fold as the ASCII letters they stand for. */
|
|
9
|
+
export declare const foldName: (name: string) => string;
|
|
10
|
+
/** The name of `owned` that `path` is at or under, if any: its first segment, on the path as normalized, folded. */
|
|
11
|
+
export declare function ownedBy(owned: readonly string[], path: string): string | undefined;
|
|
12
|
+
/** The host-owned path of the home that `path` is at or under, if any (files.ts HOME_HOST_OWNED). */
|
|
13
|
+
export declare const hostOwnedOf: (path: string) => string | undefined;
|
|
14
|
+
/** Where this server serves a host-owned path of a home to read, when it does: `/v2/agents/{id}/learning`, the learning
|
|
15
|
+
* API, which redacts its notes. */
|
|
16
|
+
export type OwnedReadAt = (owned: string) => string | undefined;
|
|
17
|
+
/** The refusal of a read, write or delete at or under the host-owned path `owned`: never raw here. A read names the
|
|
18
|
+
* route that serves it (`readAt`) only when this server has one, so it never points at a 404. */
|
|
19
|
+
export declare function hostOwnedRefusal(owned: string, agentId: string, write: boolean, readAt?: OwnedReadAt): ApiError;
|
|
20
|
+
/** A path in an agent's home as its routes take it, checked as a run's file path is, made of plain segments, and at most
|
|
21
|
+
* 1024 characters with no segment over 255 bytes. Anything at or under a host-owned path is refused, read or written,
|
|
22
|
+
* so `./learning/x`, `a/../LeArNiNg/x` and `Learning/x` are refused as learning/ is; and a write goes no deeper than the
|
|
23
|
+
* list does. */
|
|
24
|
+
export declare function homePath(raw: string, agentId: string, write: boolean, readAt?: OwnedReadAt): string;
|
|
25
|
+
/** The host-owned path of `home` that the first segment of `rel` (a path homePath took) is on this disk, if any: both
|
|
26
|
+
* there, and one entry. A disk that takes another spelling for a host-owned name answers so, whatever the spelling. */
|
|
27
|
+
export declare function ownedByIdentity(home: string, rel: string, owned?: readonly string[], stat?: (path: string) => Promise<Pick<BigIntStats, "dev" | "ino"> | null>): Promise<string | undefined>;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { lstat } from "node:fs/promises";
|
|
2
|
+
import { join, posix } from "node:path";
|
|
3
|
+
import { ApiError } from "./errors.js";
|
|
4
|
+
import { HOME_HOST_OWNED } from "./files.js";
|
|
5
|
+
import { WALK_DEPTH } from "./workspace.js";
|
|
6
|
+
/** A file path as the URL sends it, each segment percent-decoded. */
|
|
7
|
+
export function decodedPath(raw) {
|
|
8
|
+
try {
|
|
9
|
+
return raw.split("/").map(decodeURIComponent).join("/");
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
throw new ApiError("invalid_file_path", "the path is not percent-encoded right");
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/** `rel`, refused unless it is a relative path of names; a segment that starts with . is `own`'s. */
|
|
16
|
+
export function checkedPath(rel, own) {
|
|
17
|
+
const parts = rel.split("/");
|
|
18
|
+
if (!rel || rel.startsWith("/") || rel.includes("\\") || rel.includes("\0") || parts.some((p) => p === "" || p === "." || p === ".."))
|
|
19
|
+
throw new ApiError("invalid_file_path", "the path has an empty, . or .. segment, a backslash, a NUL or a leading /");
|
|
20
|
+
if (parts.some((p) => p.startsWith(".")))
|
|
21
|
+
throw new ApiError("system_file_forbidden", `a path segment that starts with . is ${own}`);
|
|
22
|
+
return rel;
|
|
23
|
+
}
|
|
24
|
+
const IGNORABLE = /\p{Default_Ignorable_Code_Point}/u;
|
|
25
|
+
/** Whether `segment` is plain text: its own NFC form, with no default-ignorable code point. A disk may take two
|
|
26
|
+
* spellings that differ only in such code points for one name; a plain segment is the one spelling. */
|
|
27
|
+
const plainSegment = (segment) => segment === segment.normalize("NFC") && !IGNORABLE.test(segment);
|
|
28
|
+
const MARKS = /\p{M}/gu;
|
|
29
|
+
/** `name` as a disk that folds names could take it: its compatibility form, upper then lower case, and without combining
|
|
30
|
+
* marks, so ı, İ, ſ, the Kelvin sign and fullwidth letters fold as the ASCII letters they stand for. */
|
|
31
|
+
export const foldName = (name) => name.normalize("NFKC").toUpperCase().toLowerCase().normalize("NFD").replace(MARKS, "");
|
|
32
|
+
/** The name of `owned` that `path` is at or under, if any: its first segment, on the path as normalized, folded. */
|
|
33
|
+
export function ownedBy(owned, path) {
|
|
34
|
+
const first = foldName(posix.normalize(path).replace(/^\/+/, "").split("/")[0]);
|
|
35
|
+
return owned.find((name) => foldName(name) === first);
|
|
36
|
+
}
|
|
37
|
+
/** The host-owned path of the home that `path` is at or under, if any (files.ts HOME_HOST_OWNED). */
|
|
38
|
+
export const hostOwnedOf = (path) => ownedBy(HOME_HOST_OWNED, path);
|
|
39
|
+
/** The refusal of a read, write or delete at or under the host-owned path `owned`: never raw here. A read names the
|
|
40
|
+
* route that serves it (`readAt`) only when this server has one, so it never points at a 404. */
|
|
41
|
+
export function hostOwnedRefusal(owned, agentId, write, readAt) {
|
|
42
|
+
const route = readAt?.(owned);
|
|
43
|
+
const message = write ? `${owned}/ is written by the server, not through the API` : route ? `the server keeps ${owned}/ for itself: read it through ${route}` : `the server keeps ${owned}/ for itself; this server has no ${owned} API yet`;
|
|
44
|
+
return new ApiError("system_file_forbidden", message, { details: { reason: "host_owned" } });
|
|
45
|
+
}
|
|
46
|
+
/** A path in an agent's home as its routes take it, checked as a run's file path is, made of plain segments, and at most
|
|
47
|
+
* 1024 characters with no segment over 255 bytes. Anything at or under a host-owned path is refused, read or written,
|
|
48
|
+
* so `./learning/x`, `a/../LeArNiNg/x` and `Learning/x` are refused as learning/ is; and a write goes no deeper than the
|
|
49
|
+
* list does. */
|
|
50
|
+
export function homePath(raw, agentId, write, readAt) {
|
|
51
|
+
const rel = decodedPath(raw);
|
|
52
|
+
if (!rel.split("/").every(plainSegment))
|
|
53
|
+
throw new ApiError("invalid_file_path", "a path segment must be plain text (NFC, no invisible characters)");
|
|
54
|
+
const owned = hostOwnedOf(rel);
|
|
55
|
+
if (owned)
|
|
56
|
+
throw hostOwnedRefusal(owned, agentId, write, readAt);
|
|
57
|
+
checkedPath(rel, "the server's own, not a file of the home");
|
|
58
|
+
if (rel.length > 1024 || rel.split("/").some((p) => Buffer.byteLength(p) > 255))
|
|
59
|
+
throw new ApiError("invalid_file_path", "the path is over 1024 characters, or a segment of it over 255 bytes");
|
|
60
|
+
if (write && rel.split("/").length - 1 > WALK_DEPTH)
|
|
61
|
+
throw new ApiError("invalid_file_path", `a file is written at most ${WALK_DEPTH} directories deep, as deep as the list goes`);
|
|
62
|
+
return rel;
|
|
63
|
+
}
|
|
64
|
+
/** Whether two stats are of one entry on disk: the same device and inode. */
|
|
65
|
+
const sameEntry = (a, b) => a.dev === b.dev && a.ino === b.ino;
|
|
66
|
+
/** The host-owned path of `home` that the first segment of `rel` (a path homePath took) is on this disk, if any: both
|
|
67
|
+
* there, and one entry. A disk that takes another spelling for a host-owned name answers so, whatever the spelling. */
|
|
68
|
+
export async function ownedByIdentity(home, rel, owned = HOME_HOST_OWNED, stat = (path) => lstat(path, { bigint: true }).catch(() => null)) {
|
|
69
|
+
const first = await stat(join(home, rel.split("/")[0]));
|
|
70
|
+
if (!first)
|
|
71
|
+
return undefined;
|
|
72
|
+
for (const name of owned) {
|
|
73
|
+
const entry = await stat(join(home, name));
|
|
74
|
+
if (entry && sameEntry(first, entry))
|
|
75
|
+
return name;
|
|
76
|
+
}
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import { type ToolRegistration } from "@earendil-works/pi-durable";
|
|
3
|
+
import type { HostTool } from "@parcha/agentrun-kernel";
|
|
4
|
+
import type { DurableRun } from "@parcha/pi-durable-disk";
|
|
5
|
+
export { workspaceRelative } from "./workspace-paths.js";
|
|
6
|
+
/** Each tool a harness other than pi runs here, by catalog name, and whether a call a crash cut off may run again. */
|
|
7
|
+
export declare const REPLAY: Record<string, "safe" | "never">;
|
|
8
|
+
/** pi's limits on a tool's output (its DEFAULT_MAX_BYTES and DEFAULT_MAX_LINES), unless the tool names its own: read's
|
|
9
|
+
* head and bash's tail are held to them (the host-bounds test reads pi's own words for them). */
|
|
10
|
+
export declare const MAX_BYTES: number;
|
|
11
|
+
export declare const MAX_LINES = 2000;
|
|
12
|
+
/** `bytes` (UTF-8) cut to at most `max` bytes, its tail or its head, at a character boundary: a character the limit
|
|
13
|
+
* would split is left out whole, so the text never ends or starts in a U+FFFD. */
|
|
14
|
+
export declare function cutUtf8(bytes: Uint8Array, max: number, tail: boolean): Uint8Array;
|
|
15
|
+
/** One call's result, kept by its call id for the run's timeline (an adapter's tool_end carries none). */
|
|
16
|
+
export type ToolResults = Map<string, {
|
|
17
|
+
text: string;
|
|
18
|
+
isError: boolean;
|
|
19
|
+
}>;
|
|
20
|
+
export declare function hostTools(run: DurableRun, names: readonly string[], context: Context, results: ToolResults, coding?: readonly ToolRegistration[]): HostTool[];
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// The host tools of a run on a harness other than pi: its coding tools, from the run's coding source (coding-tools.ts:
|
|
2
|
+
// pi's own read, write and edit on the run's environment, or in a box those and bash on the box's), so their arguments,
|
|
3
|
+
// their confinement and their refusals are pi's, word for word, pi's web_fetch (also under its legacy name,
|
|
4
|
+
// fetch_page), the public-host guard included, web_search, and monid's three (monid.ts), which stay on this machine.
|
|
5
|
+
// pi's tools take only the environment, their output and their diagnostics from a call's API, and a paid one its call's
|
|
6
|
+
// id and the run's store, so that is all a call here gives them: the output's tail or head as the tool keeps it (pi's
|
|
7
|
+
// limits), then the diagnostics, also when the tool failed. read, the web tools, monid.discover and monid.inspect may
|
|
8
|
+
// run again after a crash; write, edit, bash and monid.run never: the kernel records each one's intent before it runs,
|
|
9
|
+
// and a call a crash cut off is answered interrupted, not run again. A tool is named by its catalog name here and goes
|
|
10
|
+
// to the harness under its wire name (catalog.ts: `monid.run` is `monid_run`), which the harness calls and journals. A
|
|
11
|
+
// refusal, an error and a write's or edit's result name a path in the workspace relative to it (`hello.md`), never by
|
|
12
|
+
// the server's data directory (`workspaceRelative`); in a box they name the box's paths.
|
|
13
|
+
import { StringDecoder } from "node:string_decoder";
|
|
14
|
+
import { withAbortSignal } from "@earendil-works/chord/context";
|
|
15
|
+
import { ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
|
|
16
|
+
import { wireName } from "./catalog.js";
|
|
17
|
+
import { BOXED_TOOLS, CodingTools } from "./coding-tools.js";
|
|
18
|
+
import { FETCH_PAGE_TOOL } from "./fetch-page.js";
|
|
19
|
+
import { monidTools } from "./monid.js";
|
|
20
|
+
import { booking } from "./paid-calls.js";
|
|
21
|
+
import { withSecrets } from "./secrets.js";
|
|
22
|
+
import { WEB_FETCH_TOOL, webFetchTool } from "./web-fetch.js";
|
|
23
|
+
import { WEB_SEARCH_TOOL, webSearchTool } from "./web-search.js";
|
|
24
|
+
import { realOf, workspaceRelative } from "./workspace-paths.js";
|
|
25
|
+
export { workspaceRelative } from "./workspace-paths.js";
|
|
26
|
+
/** Each tool a harness other than pi runs here, by catalog name, and whether a call a crash cut off may run again. */
|
|
27
|
+
export const REPLAY = { read: "safe", write: "never", edit: "never", bash: "never", [FETCH_PAGE_TOOL]: "safe", [WEB_FETCH_TOOL]: "safe", [WEB_SEARCH_TOOL]: "safe", "monid.discover": "safe", "monid.inspect": "safe", "monid.run": "never" };
|
|
28
|
+
/** pi's limits on a tool's output (its DEFAULT_MAX_BYTES and DEFAULT_MAX_LINES), unless the tool names its own: read's
|
|
29
|
+
* head and bash's tail are held to them (the host-bounds test reads pi's own words for them). */
|
|
30
|
+
export const MAX_BYTES = 50 * 1024;
|
|
31
|
+
export const MAX_LINES = 2000;
|
|
32
|
+
/** Whether `byte` continues a UTF-8 character (0b10xxxxxx), so a cut before it would split one. */
|
|
33
|
+
const continues = (byte) => byte !== undefined && (byte & 0xc0) === 0x80;
|
|
34
|
+
/** `bytes` (UTF-8) cut to at most `max` bytes, its tail or its head, at a character boundary: a character the limit
|
|
35
|
+
* would split is left out whole, so the text never ends or starts in a U+FFFD. */
|
|
36
|
+
export function cutUtf8(bytes, max, tail) {
|
|
37
|
+
if (bytes.length <= max)
|
|
38
|
+
return bytes;
|
|
39
|
+
if (tail) {
|
|
40
|
+
let start = bytes.length - max;
|
|
41
|
+
while (continues(bytes[start]))
|
|
42
|
+
start++;
|
|
43
|
+
return bytes.subarray(start);
|
|
44
|
+
}
|
|
45
|
+
// The byte at `end` is the first one left out: while it continues a character, that character goes too.
|
|
46
|
+
let end = max;
|
|
47
|
+
while (end > 0 && continues(bytes[end]))
|
|
48
|
+
end--;
|
|
49
|
+
return bytes.subarray(0, end);
|
|
50
|
+
}
|
|
51
|
+
/** A call's output, held within the tool's limits as it arrives: its tail (bash's) or its head. */
|
|
52
|
+
function bounded(limits) {
|
|
53
|
+
const maxBytes = limits?.maxBytes ?? MAX_BYTES;
|
|
54
|
+
const maxLines = limits?.maxLines ?? MAX_LINES;
|
|
55
|
+
const tail = limits?.retain === "tail";
|
|
56
|
+
// Bytes are decoded as one stream: a character whose bytes arrive in two chunks is one character.
|
|
57
|
+
const decoder = new StringDecoder("utf8");
|
|
58
|
+
let kept = "";
|
|
59
|
+
let cut = false;
|
|
60
|
+
const trim = (text) => {
|
|
61
|
+
const lines = text.split("\n");
|
|
62
|
+
let out = lines.length > maxLines ? (tail ? lines.slice(-maxLines) : lines.slice(0, maxLines)).join("\n") : text;
|
|
63
|
+
const bytes = Buffer.from(out, "utf8");
|
|
64
|
+
if (bytes.length > maxBytes)
|
|
65
|
+
out = Buffer.from(cutUtf8(bytes, maxBytes, tail)).toString("utf8");
|
|
66
|
+
if (out !== text)
|
|
67
|
+
cut = true;
|
|
68
|
+
return out;
|
|
69
|
+
};
|
|
70
|
+
return {
|
|
71
|
+
push(chunk) {
|
|
72
|
+
const text = typeof chunk === "string" ? chunk : decoder.write(Buffer.from(chunk));
|
|
73
|
+
if (!tail && cut)
|
|
74
|
+
return;
|
|
75
|
+
kept += text;
|
|
76
|
+
// Trimmed once it is twice the limit, so a long output costs a bounded amount of memory and work.
|
|
77
|
+
if (kept.length > 2 * maxBytes)
|
|
78
|
+
kept = trim(kept);
|
|
79
|
+
},
|
|
80
|
+
/** What is kept so far, and whether any was cut: pi 1.2's `retainedOutput`. */
|
|
81
|
+
retained: () => {
|
|
82
|
+
const text = trim(kept);
|
|
83
|
+
return { text, truncated: cut };
|
|
84
|
+
},
|
|
85
|
+
text() {
|
|
86
|
+
const out = trim(kept);
|
|
87
|
+
return cut ? (tail ? `[earlier output omitted]\n${out}` : `${out}\n[later output omitted]`) : out;
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** The tools whose result is pi's own words about a path (`Successfully wrote to <path>`), never a file's text. */
|
|
92
|
+
const SAYS_PATH = new Set(["write", "edit"]);
|
|
93
|
+
const textOf = (content) => Array.isArray(content) ? content.map((block) => (block && typeof block === "object" && "text" in block && typeof block.text === "string" ? block.text : "")).join("") : "";
|
|
94
|
+
export function hostTools(run, names, context, results, coding = CodingTools.tools ?? []) {
|
|
95
|
+
// The coding source gives the coding tools and nothing else: the network tools are this machine's own, below.
|
|
96
|
+
const stray = coding.map((tool) => tool.name).filter((name) => !BOXED_TOOLS.includes(name));
|
|
97
|
+
if (stray.length)
|
|
98
|
+
throw new Error(`the coding source holds ${stray.join(", ")}, which is no coding tool`);
|
|
99
|
+
// Their paid requests (Parallel's) are booked under the call, in the run's store, as pi's are (paid-calls.ts).
|
|
100
|
+
const web = [webFetchTool(process.env), webFetchTool(process.env, FETCH_PAGE_TOOL), webSearchTool(process.env)].map(booking);
|
|
101
|
+
// monid's, only for a run that names one: its key is the one this process was handed over its pipe (secrets.ts),
|
|
102
|
+
// never in its environment, and its long answers go in the run's work/, the claim's as pi's (`where.work`), read at
|
|
103
|
+
// the call. They are outside the coding source, which holds the coding tools only.
|
|
104
|
+
const monid = names.some((name) => name.startsWith("monid.")) ? monidTools(withSecrets(process.env), () => run.claim.work) : [];
|
|
105
|
+
const tools = new Map([...coding, ...web, ...monid].map((tool) => [tool.name, tool]));
|
|
106
|
+
return names.flatMap((name) => {
|
|
107
|
+
// The registrations are keyed by their wire names: the same name for every tool here but monid's.
|
|
108
|
+
const tool = tools.get(wireName(name));
|
|
109
|
+
const replay = REPLAY[name];
|
|
110
|
+
if (!tool || !replay)
|
|
111
|
+
return [];
|
|
112
|
+
const host = {
|
|
113
|
+
name: tool.name,
|
|
114
|
+
description: tool.description,
|
|
115
|
+
inputSchema: tool.parameters,
|
|
116
|
+
replay,
|
|
117
|
+
async call(args, { callId, signal }) {
|
|
118
|
+
const out = bounded(tool.outputLimits);
|
|
119
|
+
const notes = [];
|
|
120
|
+
const env = await run.env({ conversationId: ROOT_CONVERSATION_ID, read: undefined }, context);
|
|
121
|
+
// The call's id and the run's store, as pi's: a paid call marks and books its charge there (monid.ts).
|
|
122
|
+
const api = { env, callId, commit: (change, at) => run.harness.commit(change, at), output: (chunk) => out.push(chunk), outputWindow: undefined, retainedOutput: () => out.retained(), diagnostic: (note) => void notes.push(note.message) };
|
|
123
|
+
// A tool that throws (bash on a non-zero exit or a timeout) still answers with its output, as pi's harness does;
|
|
124
|
+
// monid's answer a failure as an error result, which is kept as it is.
|
|
125
|
+
const result = await tool.execute(args, api, withAbortSignal(signal, context)).catch((error) => ({ failed: error.message, isError: true }));
|
|
126
|
+
// pi 1.1 names the result's blocks `content`, pi 1.2 and later `output`; pi 1.2's bash answers its exit as a
|
|
127
|
+
// result's diagnostic, where 1.1 threw it.
|
|
128
|
+
const given = result;
|
|
129
|
+
const said = [textOf(given.content ?? given.output) || out.text(), given.failed, ...notes, ...(given.diagnostics ?? []).map((note) => note.message)].filter((part) => part).join("\n\n");
|
|
130
|
+
const isError = given.isError === true;
|
|
131
|
+
const work = env.cwd;
|
|
132
|
+
const works = typeof work === "string" ? [work, realOf(work)] : [];
|
|
133
|
+
const answer = { text: isError || SAYS_PATH.has(name) ? workspaceRelative(said, works) : said, isError };
|
|
134
|
+
results.set(callId, answer);
|
|
135
|
+
return answer;
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
return [host];
|
|
139
|
+
});
|
|
140
|
+
}
|
package/dist/host.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { statfs } from "node:fs/promises";
|
|
2
|
+
/** Memory the server keeps for itself, before any run, in MiB. Measured with RUN_MIB (harness.ts): the server's anon
|
|
3
|
+
* 3 s after its start, 32 starts at limits of 512M to 2048M, was 58 to 81 MB, with no peak at the start over that;
|
|
4
|
+
* reserved for the high end, 81, plus 47 (+58%), since what it holds grows with the agents and runs it keeps. That is
|
|
5
|
+
* with the pinned binaries' sha256 checked a chunk at a time: a start that reads the codex binary (294 MB) and the
|
|
6
|
+
* claude-agent CLI (230 MB) whole sat at 341 to 348 MB on a host of 1 GB or less. */
|
|
7
|
+
export declare const SERVER_MIB = 128;
|
|
8
|
+
/** Free disk under the data directory below which health fails, unless the server says otherwise. */
|
|
9
|
+
export declare const MIN_FREE_MIB = 64;
|
|
10
|
+
/** The memory this server may use: its container's limit when it has one (cgroup), else the machine's. */
|
|
11
|
+
export declare function hostMemory(): number;
|
|
12
|
+
/** The memory runs may hold on a host of `memory` bytes, in MiB: all of it less the server's own. */
|
|
13
|
+
export declare const runsMemoryMib: (memory: number) => number;
|
|
14
|
+
/** What a run of `harness` reserves, in MiB. A harness outside the table (no saved revision names one) reserves the most. */
|
|
15
|
+
export declare const runMibOf: (harness: string | undefined) => number;
|
|
16
|
+
/** How many runs of `harness` alone fit on a host of `memory` bytes. pi, the default, always has one place, as before
|
|
17
|
+
* runs were admitted by memory: a server too small for even that still runs one at a time. */
|
|
18
|
+
export declare function runsThatFit(harness: string, memory: number): number;
|
|
19
|
+
/** The default cap on how many runs at once: as many pi runs, the smallest, as fit. */
|
|
20
|
+
export declare const defaultMaxRuns: (memory: number) => number;
|
|
21
|
+
/** Why no run of `harness` can ever start on a host of `memory` bytes, in words; undefined when one fits. */
|
|
22
|
+
export declare function neverFits(harness: string, memory: number): string | undefined;
|
|
23
|
+
/** Why a run of `harness` cannot start now, with `heldMib` of the memory for runs held: the memory runs hold, in words. */
|
|
24
|
+
export declare function memoryFull(harness: string, memory: number, heldMib: number): string;
|
|
25
|
+
/** The cap on how many runs at once: `given`, else `AGENTRUN_MAX_RUNS`, else the default for this host's memory. The
|
|
26
|
+
* memory each run reserves is checked beside it (runs.ts): both apply. */
|
|
27
|
+
export declare function maxRunsOf(env: NodeJS.ProcessEnv, given?: number): number;
|
|
28
|
+
export type Health = {
|
|
29
|
+
status: "ok" | "failing";
|
|
30
|
+
checks: {
|
|
31
|
+
store: "ok" | "does_not_open";
|
|
32
|
+
disk: "ok" | "below_floor" | "cannot_check";
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
/** Whether the store in `data` opens for a read, and its disk has `minFreeBytes` free; a disk it cannot measure fails.
|
|
36
|
+
* Says no path and no number. `fsStat` is node's statfs unless a test gives another. */
|
|
37
|
+
export declare function healthOf(data: string, minFreeBytes: number, fsStat?: typeof statfs): Promise<Health>;
|
|
38
|
+
/** Whether `--open` opens a browser here: not in CI, and on Linux or FreeBSD only with a display. */
|
|
39
|
+
export declare const canOpen: (env: NodeJS.ProcessEnv, platform?: NodeJS.Platform) => boolean;
|
package/dist/host.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// What one host can hold, and whether it can work now. Runs are admitted by memory: each run is a process of its own,
|
|
2
|
+
// and each reserves what a run of its harness takes (RUN_MIB, harness.ts) out of the memory the host has less the
|
|
3
|
+
// server's own (SERVER_MIB). Health reads the agent store and the free disk under the data directory. A start here opens
|
|
4
|
+
// a browser only where there is one to open.
|
|
5
|
+
import { statfs } from "node:fs/promises";
|
|
6
|
+
import { totalmem } from "node:os";
|
|
7
|
+
import { openStoreReader } from "@parcha/pi-durable-agent";
|
|
8
|
+
import { REQUEST_BUSY_MS, whenFree } from "./agents.js";
|
|
9
|
+
import { DEFAULT_HARNESS, HARNESSES, RUN_MIB } from "./harness.js";
|
|
10
|
+
const MiB = 2 ** 20;
|
|
11
|
+
/** Memory the server keeps for itself, before any run, in MiB. Measured with RUN_MIB (harness.ts): the server's anon
|
|
12
|
+
* 3 s after its start, 32 starts at limits of 512M to 2048M, was 58 to 81 MB, with no peak at the start over that;
|
|
13
|
+
* reserved for the high end, 81, plus 47 (+58%), since what it holds grows with the agents and runs it keeps. That is
|
|
14
|
+
* with the pinned binaries' sha256 checked a chunk at a time: a start that reads the codex binary (294 MB) and the
|
|
15
|
+
* claude-agent CLI (230 MB) whole sat at 341 to 348 MB on a host of 1 GB or less. */
|
|
16
|
+
export const SERVER_MIB = 128;
|
|
17
|
+
/** Free disk under the data directory below which health fails, unless the server says otherwise. */
|
|
18
|
+
export const MIN_FREE_MIB = 64;
|
|
19
|
+
/** The memory this server may use: its container's limit when it has one (cgroup), else the machine's. */
|
|
20
|
+
export function hostMemory() {
|
|
21
|
+
const total = totalmem();
|
|
22
|
+
const limit = process.constrainedMemory?.() ?? 0;
|
|
23
|
+
return limit > 0 && limit < total ? limit : total;
|
|
24
|
+
}
|
|
25
|
+
/** The memory runs may hold on a host of `memory` bytes, in MiB: all of it less the server's own. */
|
|
26
|
+
export const runsMemoryMib = (memory) => Math.floor(memory / MiB) - SERVER_MIB;
|
|
27
|
+
/** What a run of `harness` reserves, in MiB. A harness outside the table (no saved revision names one) reserves the most. */
|
|
28
|
+
export const runMibOf = (harness) => RUN_MIB[(harness ?? DEFAULT_HARNESS)] ?? Math.max(...Object.values(RUN_MIB));
|
|
29
|
+
/** How many runs of `harness` alone fit on a host of `memory` bytes. pi, the default, always has one place, as before
|
|
30
|
+
* runs were admitted by memory: a server too small for even that still runs one at a time. */
|
|
31
|
+
export function runsThatFit(harness, memory) {
|
|
32
|
+
const fit = Math.max(0, Math.floor(runsMemoryMib(memory) / runMibOf(harness)));
|
|
33
|
+
return harness === DEFAULT_HARNESS ? Math.max(1, fit) : fit;
|
|
34
|
+
}
|
|
35
|
+
/** The default cap on how many runs at once: as many pi runs, the smallest, as fit. */
|
|
36
|
+
export const defaultMaxRuns = (memory) => runsThatFit(DEFAULT_HARNESS, memory);
|
|
37
|
+
const sizeOf = (mib) => `${Math.round(mib)} MB`;
|
|
38
|
+
/** Why no run of `harness` can ever start on a host of `memory` bytes, in words; undefined when one fits. */
|
|
39
|
+
export function neverFits(harness, memory) {
|
|
40
|
+
if (runsThatFit(harness, memory) > 0)
|
|
41
|
+
return undefined;
|
|
42
|
+
const fit = HARNESSES.filter((id) => runsThatFit(id, memory) > 0);
|
|
43
|
+
return `This server has ${sizeOf(memory / MiB)} of memory and keeps ${sizeOf(SERVER_MIB)} for itself; a ${harness} run needs about ${sizeOf(runMibOf(harness))} more, so it can't start here. ${fit.join(" and ")} runs fit.`;
|
|
44
|
+
}
|
|
45
|
+
/** Why a run of `harness` cannot start now, with `heldMib` of the memory for runs held: the memory runs hold, in words. */
|
|
46
|
+
export function memoryFull(harness, memory, heldMib) {
|
|
47
|
+
const free = Math.max(0, runsMemoryMib(memory) - heldMib);
|
|
48
|
+
return `This server's memory is held by the runs it has going: a ${harness} run needs about ${sizeOf(runMibOf(harness))}, and ${sizeOf(free)} of the ${sizeOf(runsMemoryMib(memory))} it has for runs is free; start this one when another ends.`;
|
|
49
|
+
}
|
|
50
|
+
/** The cap on how many runs at once: `given`, else `AGENTRUN_MAX_RUNS`, else the default for this host's memory. The
|
|
51
|
+
* memory each run reserves is checked beside it (runs.ts): both apply. */
|
|
52
|
+
export function maxRunsOf(env, given) {
|
|
53
|
+
const value = given ?? (env.AGENTRUN_MAX_RUNS ? Number(env.AGENTRUN_MAX_RUNS) : defaultMaxRuns(hostMemory()));
|
|
54
|
+
if (!Number.isSafeInteger(value) || value < 1)
|
|
55
|
+
throw new Error(`max runs is a positive integer (--max-runs, AGENTRUN_MAX_RUNS), not ${given ?? env.AGENTRUN_MAX_RUNS}`);
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
/** Whether the store in `data` opens for a read, and its disk has `minFreeBytes` free; a disk it cannot measure fails.
|
|
59
|
+
* Says no path and no number. `fsStat` is node's statfs unless a test gives another. */
|
|
60
|
+
export async function healthOf(data, minFreeBytes, fsStat = statfs) {
|
|
61
|
+
let store = "ok";
|
|
62
|
+
try {
|
|
63
|
+
await whenFree(() => {
|
|
64
|
+
const reader = openStoreReader(data, { busyTimeoutMs: 0 });
|
|
65
|
+
try {
|
|
66
|
+
reader.listAgents();
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
reader.close();
|
|
70
|
+
}
|
|
71
|
+
}, REQUEST_BUSY_MS);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
store = "does_not_open";
|
|
75
|
+
}
|
|
76
|
+
const free = await fsStat(data).then((fs) => fs.bavail * fs.bsize, () => undefined);
|
|
77
|
+
const disk = free === undefined ? "cannot_check" : free >= minFreeBytes ? "ok" : "below_floor";
|
|
78
|
+
return { status: store === "ok" && disk === "ok" ? "ok" : "failing", checks: { store, disk } };
|
|
79
|
+
}
|
|
80
|
+
/** Whether `--open` opens a browser here: not in CI, and on Linux or FreeBSD only with a display. */
|
|
81
|
+
export const canOpen = (env, platform = process.platform) => !env.CI && (!["linux", "freebsd"].includes(platform) || Boolean(env.DISPLAY || env.WAYLAND_DISPLAY));
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Models } from "@earendil-works/pi-ai";
|
|
2
|
+
import { type Search } from "./web-search.js";
|
|
3
|
+
export declare const HOSTED_MAX_USES = 5;
|
|
4
|
+
/** One search's results billed as input: about 25,000 tokens, a working estimate. */
|
|
5
|
+
export declare const SEARCH_INPUT_TOKENS = 25000;
|
|
6
|
+
/** What the request may read into its input beyond its context, for the run's cap: each of its searches' results. */
|
|
7
|
+
export declare const HOSTED_SEARCH_ALLOWANCE_TOKENS: number;
|
|
8
|
+
export declare const ANTHROPIC_SEARCH_TOOL: Readonly<{
|
|
9
|
+
type: "web_search_20250305";
|
|
10
|
+
name: "web_search";
|
|
11
|
+
max_uses: 5;
|
|
12
|
+
}>;
|
|
13
|
+
export declare const OPENAI_SEARCH_TOOL: Readonly<{
|
|
14
|
+
type: "web_search";
|
|
15
|
+
}>;
|
|
16
|
+
/** OpenAI's hosted search has no per-tool limit: the Responses API's `max_tool_calls` caps the built-in tool calls of the
|
|
17
|
+
* request at the HOSTED_MAX_USES its fee is reserved for, as Anthropic's tool's `max_uses` does. */
|
|
18
|
+
export declare const OPENAI_SEARCH_LIMIT: Readonly<{
|
|
19
|
+
max_tool_calls: 5;
|
|
20
|
+
}>;
|
|
21
|
+
/** $10 per 1,000 searches (Anthropic) or calls (OpenAI). */
|
|
22
|
+
export declare const SEARCH_USD = 0.01;
|
|
23
|
+
/** The run's models, marked and capped, handed over by the run's app before its tools are bound. */
|
|
24
|
+
export declare function searchOn(models: Models): void;
|
|
25
|
+
/** `query` searched by `ref`'s provider (`provider/model-id`, the model the server resolved web_search on) through the
|
|
26
|
+
* run's models, at most `n` results: a Search. `models`: the run's (searchOn). */
|
|
27
|
+
export declare function hostedSearch(query: string, n: number, ref: string | undefined, signal?: AbortSignal, models?: Models | undefined): Promise<Search>;
|