@parcha/agentrun-serve 0.1.0-beta.52 → 0.1.0-beta.54
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 +149 -1
- package/README.md +295 -37
- package/dist/agent-delete.d.ts +5 -6
- package/dist/agent-delete.js +30 -67
- package/dist/agent-package.d.ts +71 -0
- package/dist/agent-package.js +345 -0
- package/dist/agent-rows.d.ts +1 -1
- package/dist/agents-store.d.ts +26 -20
- package/dist/agents-store.js +42 -34
- package/dist/api-only.d.ts +25 -6
- package/dist/api-only.js +34 -13
- 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 +76 -8
- package/dist/composio-preset.d.ts +3 -1
- package/dist/composio-preset.js +5 -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 +5 -2
- package/dist/computer-setting.js +8 -1
- package/dist/computers.d.ts +0 -2
- package/dist/computers.js +1 -1
- package/dist/console/assets/index-B_85An2t.js +745 -0
- package/dist/console/assets/index-n1YsbnYh.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 +5 -0
- package/dist/errors.js +10 -0
- package/dist/eval-previous.js +4 -3
- package/dist/fal-price.js +10 -13
- package/dist/github-connection.d.ts +50 -1
- package/dist/github-connection.js +96 -3
- package/dist/github-faux.d.ts +11 -2
- package/dist/github-faux.js +138 -7
- package/dist/github-pull-request.d.ts +37 -0
- package/dist/github-pull-request.js +110 -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 +14 -1
- package/dist/learn/commit.js +156 -1
- package/dist/learn/compile.js +22 -8
- package/dist/learn/folder.d.ts +5 -0
- package/dist/learn/folder.js +10 -1
- package/dist/learn/learning.sh +15 -3
- package/dist/learn/promote.d.ts +2 -2
- package/dist/learn/secrets.js +30 -7
- package/dist/learning-routes.d.ts +23 -11
- package/dist/learning-routes.js +110 -20
- package/dist/learning.d.ts +9 -6
- package/dist/learning.js +9 -4
- package/dist/live-reach.d.ts +34 -7
- package/dist/live-reach.js +76 -11
- package/dist/mcp-gateway.js +15 -2
- package/dist/mcp-probe.d.ts +3 -0
- package/dist/mcp-probe.js +8 -3
- package/dist/mcp-resources.d.ts +44 -0
- package/dist/mcp-resources.js +100 -0
- package/dist/mcp-servers-store.d.ts +16 -12
- 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 +275 -40
- package/dist/print.js +1 -1
- package/dist/purged-workflow.d.ts +12 -0
- package/dist/purged-workflow.js +43 -0
- package/dist/run-keys.d.ts +46 -0
- package/dist/run-keys.js +109 -0
- package/dist/run-launch.d.ts +16 -1
- package/dist/run-launch.js +30 -1
- 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-view.d.ts +17 -0
- package/dist/run-view.js +25 -1
- package/dist/runner/adapter-loop.d.ts +11 -3
- package/dist/runner/adapter-loop.js +138 -22
- package/dist/runner/adapter-run-spec.js +26 -13
- package/dist/runner/audit.d.ts +2 -0
- package/dist/runner/audit.js +9 -1
- package/dist/runner/beside.js +2 -1
- package/dist/runner/claim-secrets.d.ts +29 -3
- package/dist/runner/claim-secrets.js +38 -4
- package/dist/runner/cli-run-spec.js +24 -13
- package/dist/runner/harness-loops.d.ts +6 -0
- package/dist/runner/harness-loops.js +11 -1
- package/dist/runner/held.d.ts +4 -0
- package/dist/runner/journal-kinds.d.ts +2 -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 +7 -2
- package/dist/runner/learning-end.js +22 -10
- 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 +6 -1
- package/dist/runner/mcp-run.d.ts +10 -7
- package/dist/runner/mcp-run.js +3 -5
- package/dist/runner/metrics.d.ts +4 -2
- package/dist/runner/metrics.js +15 -3
- package/dist/runner/monid-run.js +9 -11
- package/dist/runner/pr-checkout.d.ts +21 -4
- package/dist/runner/pr-checkout.js +103 -4
- 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 +14 -6
- package/dist/runner/promotion.js +98 -41
- 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 +178 -45
- package/dist/runner/run-errors.js +2 -2
- package/dist/runner/runner-check.d.ts +3 -0
- package/dist/runner/runner-check.js +28 -2
- package/dist/runner/runner.js +26 -9
- 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 +1 -1
- package/dist/runs-over-store.d.ts +34 -6
- package/dist/runs-over-store.js +133 -25
- package/dist/sdk/runs.d.ts +11 -1
- package/dist/sdk/runs.js +117 -26
- package/dist/sdk/schema.js +12 -2
- package/dist/sdk/workflows.d.ts +1 -1
- package/dist/server.d.ts +79 -32
- package/dist/server.js +673 -113
- 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 +6 -2
- package/dist/store/agent-body.js +18 -7
- package/dist/store/index.d.ts +2 -2
- package/dist/store/index.js +1 -1
- package/dist/store/schema.d.ts +2 -1
- package/dist/store/schema.js +2 -1
- 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 +4 -12
- package/dist/store/sqlite/run-launch.js +5 -19
- package/dist/store/sqlite/store.js +224 -170
- package/dist/store/store.d.ts +116 -39
- package/dist/store/store.js +14 -1
- package/dist/store-reads/learning.d.ts +2 -2
- package/dist/store-view.d.ts +3 -1
- package/dist/store-view.js +17 -5
- package/dist/suggestions.d.ts +282 -0
- package/dist/suggestions.js +919 -0
- package/dist/telemetry.d.ts +8 -1
- package/dist/telemetry.js +12 -4
- package/dist/template-import.d.ts +27 -2
- package/dist/template-import.js +148 -13
- package/dist/templates.d.ts +103 -77
- package/dist/templates.js +71 -17
- package/dist/timeline/index.d.ts +1 -1
- package/dist/timeline/index.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/trigger-launch-hook.d.ts +49 -0
- package/dist/trigger-launch-hook.js +91 -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 +1 -1
- package/dist/uploads.js +1 -1
- package/dist/validate.d.ts +1 -1
- package/dist/validate.js +3 -4
- 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/run-tools.d.ts +2 -0
- package/dist/workspace/run-tools.js +8 -0
- package/dist/workspace/synced-folder.d.ts +1 -1
- package/dist/workspace/synced-folder.js +11 -14
- package/launcher/agentrun-serve +1 -1
- package/package.json +10 -10
- package/skills/agentrun-serve/SKILL.md +2 -1
- package/templates/README.md +8 -4
- package/templates/agent-scout/agent.md +46 -0
- package/templates/agent-scout/skills/scout/SKILL.md +88 -0
- package/templates/agent-scout/skills/scout/suggestions.schema.json +112 -0
- package/dist/console/assets/index-B-D0EM3j.js +0 -733
- package/dist/console/assets/index-DlQepP2J.css +0 -1
package/dist/github-faux.js
CHANGED
|
@@ -3,20 +3,70 @@
|
|
|
3
3
|
// signed delivery fired) run in a real browser with nothing sent to GitHub. It answers the API calls the server makes of
|
|
4
4
|
// the App (github.ts, github-connection.ts): the App's installations, one installation's read, an installation token's
|
|
5
5
|
// mint narrowed to the repositories asked, that token's read of the repositories it covers and its revoke, and which
|
|
6
|
-
// installation covers a repository.
|
|
7
|
-
//
|
|
8
|
-
|
|
6
|
+
// installation covers a repository. And what a suggested agent's create writes (suggestions.ts, github-pull-request.ts):
|
|
7
|
+
// a repository's read, its default branch's head, which files it has, a tree, a commit, a branch and a pull request,
|
|
8
|
+
// each kept in memory and read back whole at GET /_faux/state, where a test or a person sees what a create pushed. The
|
|
9
|
+
// installation holds contents write, as an App connected with `contents: write` does; POST /_faux/permissions sets what
|
|
10
|
+
// it holds, so a test (or a demo) shows a create refused for a permission. The App, its private key and its webhook
|
|
11
|
+
// secret are made fresh at each start; a test signs a delivery with the webhook secret, kept beside the data
|
|
12
|
+
// (FAUX_GITHUB_FILE). Never on a server without --faux.
|
|
13
|
+
import { execFileSync } from "node:child_process";
|
|
14
|
+
import { createHash, generateKeyPairSync, randomBytes } from "node:crypto";
|
|
15
|
+
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
16
|
+
import { dirname, join } from "node:path";
|
|
9
17
|
import { createServer } from "node:http";
|
|
10
18
|
/** The file beside the data that tells a test the fake App's webhook secret, installation and repositories. */
|
|
11
19
|
export const FAUX_GITHUB_FILE = "faux-github.json";
|
|
12
20
|
/** The fake App's one installation and the repositories it covers. */
|
|
13
21
|
export const FAUX_GITHUB = { appId: "1", slug: "agentrun-faux", installation: 1, account: "faux-org", repositories: ["faux-org/demo"] };
|
|
14
22
|
const TOKEN_MS = 60 * 60_000;
|
|
15
|
-
/**
|
|
16
|
-
|
|
23
|
+
/** The fake repository's files on its default branch: what its contents API answers, and what its bare repository holds
|
|
24
|
+
* (a small help desk whose support queue and refunds a person works by hand, for a scan to find). */
|
|
25
|
+
export const FAUX_REPO_FILES = {
|
|
26
|
+
"README.md": "# Demo shop\n\nA help desk and billing app for small shops.\n",
|
|
27
|
+
"package.json": `${JSON.stringify({ name: "demo-shop", private: true, type: "module" }, null, 2)}\n`,
|
|
28
|
+
"src/support/inbox.ts": `${Array.from({ length: 41 }, (_, i) => `// inbox line ${i + 1}`).join("\n")}\nexport const saved = (ticket: { subject: string; body: string }) => ({ ...ticket, status: "needs_triage" }); // a person sets category and priority\n`,
|
|
29
|
+
"src/support/classify.ts": "// Keyword rules for a ticket's category.\nexport function classify(text: string): string {\n if (/refund|invoice/i.test(text)) return \"billing\";\n return \"other\";\n}\n",
|
|
30
|
+
"app/refunds.py": "def request_refund(refund):\n refund.status = 'pending_review' # someone in ops reads it\n return refund\n",
|
|
31
|
+
};
|
|
32
|
+
/** The fake repository as a bare git repository under `dir` (<dir>/<owner>/<name>.git), one commit on main: what a run's
|
|
33
|
+
* checkout fetches with `web` set to file://<dir>. Undefined, with the folder left empty, where git does not run. */
|
|
34
|
+
function bareRepository(dir) {
|
|
35
|
+
const env = { PATH: process.env.PATH ?? "", HOME: dir, GIT_CONFIG_NOSYSTEM: "1", GIT_CONFIG_GLOBAL: "/dev/null", GIT_AUTHOR_NAME: "faux", GIT_AUTHOR_EMAIL: "faux@example.invalid", GIT_COMMITTER_NAME: "faux", GIT_COMMITTER_EMAIL: "faux@example.invalid" };
|
|
36
|
+
const work = join(dir, ".work");
|
|
37
|
+
try {
|
|
38
|
+
rmSync(dir, { recursive: true, force: true });
|
|
39
|
+
for (const [path, text] of Object.entries(FAUX_REPO_FILES)) {
|
|
40
|
+
mkdirSync(dirname(join(work, path)), { recursive: true });
|
|
41
|
+
writeFileSync(join(work, path), text);
|
|
42
|
+
}
|
|
43
|
+
const git = (cwd, ...args) => execFileSync("git", args, { cwd, env, stdio: "ignore" });
|
|
44
|
+
git(work, "init", "-q", "-b", "main");
|
|
45
|
+
git(work, "add", "-A");
|
|
46
|
+
git(work, "commit", "-q", "-m", "The demo shop");
|
|
47
|
+
const bare = join(dir, `${FAUX_GITHUB.repositories[0]}.git`);
|
|
48
|
+
mkdirSync(dirname(bare), { recursive: true });
|
|
49
|
+
git(dir, "clone", "-q", "--bare", work, bare);
|
|
50
|
+
return `file://${dir}`;
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
rmSync(work, { recursive: true, force: true });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** Starts the fake GitHub API on loopback, with its App; with `webDir`, its repository as a bare git repository there. */
|
|
60
|
+
export async function startFauxGitHub({ webDir } = {}) {
|
|
61
|
+
const web = webDir ? bareRepository(webDir) : undefined;
|
|
17
62
|
const { privateKey } = generateKeyPairSync("rsa", { modulusLength: 2048, privateKeyEncoding: { type: "pkcs1", format: "pem" }, publicKeyEncoding: { type: "spki", format: "pem" } });
|
|
18
63
|
const app = { id: FAUX_GITHUB.appId, slug: FAUX_GITHUB.slug, clientId: `Iv1.faux${randomBytes(6).toString("hex")}`, pem: privateKey, webhookSecret: randomBytes(24).toString("hex"), settingsUrl: `https://github.com/organizations/${FAUX_GITHUB.account}/settings/apps/${FAUX_GITHUB.slug}` };
|
|
19
|
-
const installation = { id: FAUX_GITHUB.installation, account: { login: FAUX_GITHUB.account, type: "Organization" }, app_id: Number(FAUX_GITHUB.appId), suspended_at: null, permissions: { metadata: "read", contents: "
|
|
64
|
+
const installation = { id: FAUX_GITHUB.installation, account: { login: FAUX_GITHUB.account, type: "Organization" }, app_id: Number(FAUX_GITHUB.appId), suspended_at: null, permissions: { metadata: "read", contents: "write", pull_requests: "write", issues: "read" } };
|
|
65
|
+
// Each repository the installation covers, on its default branch main: its files at one commit, and what writes add.
|
|
66
|
+
const sha = (text) => createHash("sha1").update(text).digest("hex");
|
|
67
|
+
const BASE = sha("faux-base");
|
|
68
|
+
const files = new Set(Object.keys(FAUX_REPO_FILES));
|
|
69
|
+
const state = { branches: new Map([["main", BASE]]), trees: new Map(), commits: new Map(), pulls: [] };
|
|
20
70
|
/** Each token minted, by its value: the repositories it was narrowed to (by name), or every one the installation covers. */
|
|
21
71
|
const tokens = new Map();
|
|
22
72
|
const covered = (names) => FAUX_GITHUB.repositories.filter((full) => !names || names.includes(full.split("/")[1]));
|
|
@@ -43,7 +93,7 @@ export async function startFauxGitHub() {
|
|
|
43
93
|
return answer(res, 422, { message: "There is at least one repository that does not exist or is not accessible to the parent installation." });
|
|
44
94
|
const token = `faux-installation-token-${randomBytes(16).toString("hex")}`;
|
|
45
95
|
tokens.set(token, names);
|
|
46
|
-
return answer(res, 201, { token, expires_at: new Date(Date.now() + TOKEN_MS).toISOString(), permissions: {
|
|
96
|
+
return answer(res, 201, { token, expires_at: new Date(Date.now() + TOKEN_MS).toISOString(), permissions: { ...installation.permissions }, repository_selection: "selected" });
|
|
47
97
|
}
|
|
48
98
|
const bearer = String(req.headers.authorization ?? "").replace(/^Bearer /, "");
|
|
49
99
|
if (req.method === "GET" && path === "/installation/repositories") {
|
|
@@ -60,6 +110,86 @@ export async function startFauxGitHub() {
|
|
|
60
110
|
const repo = /^\/repos\/([^/]+)\/([^/]+)\/installation$/.exec(path);
|
|
61
111
|
if (req.method === "GET" && repo)
|
|
62
112
|
return FAUX_GITHUB.repositories.includes(`${repo[1]}/${repo[2]}`) ? answer(res, 200, installation) : answer(res, 404, { message: "Not Found" });
|
|
113
|
+
// What a test or a demo reads and sets of the fake: everything written, and the installation's permissions.
|
|
114
|
+
if (req.method === "GET" && path === "/_faux/state")
|
|
115
|
+
return answer(res, 200, { permissions: installation.permissions, branches: Object.fromEntries(state.branches), commits: Object.fromEntries(state.commits), trees: Object.fromEntries(state.trees), pulls: state.pulls });
|
|
116
|
+
if (req.method === "POST" && path === "/_faux/permissions") {
|
|
117
|
+
try {
|
|
118
|
+
installation.permissions = { ...installation.permissions, ...JSON.parse(body || "{}") };
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return answer(res, 400, { message: "Problems parsing JSON" });
|
|
122
|
+
}
|
|
123
|
+
return answer(res, 200, installation.permissions);
|
|
124
|
+
}
|
|
125
|
+
// A repository's reads and writes, each with a token of the installation narrowed to it (or covering every one).
|
|
126
|
+
const on = /^\/repos\/([^/]+)\/([^/]+)(\/.*)?$/.exec(path);
|
|
127
|
+
if (on && FAUX_GITHUB.repositories.includes(`${on[1]}/${on[2]}`)) {
|
|
128
|
+
const full = `${on[1]}/${on[2]}`;
|
|
129
|
+
const rest = on[3] ?? "";
|
|
130
|
+
if (!tokens.has(bearer) || !covered(tokens.get(bearer)).includes(full))
|
|
131
|
+
return answer(res, 404, { message: "Not Found" });
|
|
132
|
+
const writes = installation.permissions.contents === "write";
|
|
133
|
+
const json = () => { try {
|
|
134
|
+
return JSON.parse(body || "{}");
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
return {};
|
|
138
|
+
} };
|
|
139
|
+
if (req.method === "GET" && rest === "")
|
|
140
|
+
return answer(res, 200, { full_name: full, private: true, default_branch: "main" });
|
|
141
|
+
const ref = /^\/git\/ref\/heads\/(.+)$/.exec(rest);
|
|
142
|
+
if (req.method === "GET" && ref)
|
|
143
|
+
return state.branches.has(decodeURIComponent(ref[1])) ? answer(res, 200, { ref: `refs/heads/${decodeURIComponent(ref[1])}`, object: { sha: state.branches.get(decodeURIComponent(ref[1])), type: "commit" } }) : answer(res, 404, { message: "Not Found" });
|
|
144
|
+
const commit = /^\/git\/commits\/([0-9a-f]{40})$/.exec(rest);
|
|
145
|
+
if (req.method === "GET" && commit)
|
|
146
|
+
return commit[1] === BASE ? answer(res, 200, { sha: BASE, tree: { sha: sha("faux-base-tree") } }) : state.commits.has(commit[1]) ? answer(res, 200, state.commits.get(commit[1])) : answer(res, 404, { message: "Not Found" });
|
|
147
|
+
const content = /^\/contents\/(.+)$/.exec(rest);
|
|
148
|
+
if (req.method === "GET" && content)
|
|
149
|
+
return files.has(decodeURIComponent(content[1])) ? answer(res, 200, { type: "file", path: decodeURIComponent(content[1]) }) : answer(res, 404, { message: "Not Found" });
|
|
150
|
+
// Writes: GitHub answers an installation that can't write contents 403 ("Resource not accessible by integration").
|
|
151
|
+
if (req.method === "POST" && (rest === "/git/trees" || rest === "/git/commits" || rest === "/git/refs") && !writes)
|
|
152
|
+
return answer(res, 403, { message: "Resource not accessible by integration" });
|
|
153
|
+
if (req.method === "POST" && rest === "/git/trees") {
|
|
154
|
+
const made = sha(`tree ${body}`);
|
|
155
|
+
state.trees.set(made, json());
|
|
156
|
+
return answer(res, 201, { sha: made });
|
|
157
|
+
}
|
|
158
|
+
if (req.method === "POST" && rest === "/git/commits") {
|
|
159
|
+
const asked = json();
|
|
160
|
+
const made = sha(`commit ${body}`);
|
|
161
|
+
state.commits.set(made, { sha: made, tree: { sha: asked.tree }, message: asked.message, parents: asked.parents });
|
|
162
|
+
return answer(res, 201, { sha: made });
|
|
163
|
+
}
|
|
164
|
+
if (req.method === "POST" && rest === "/git/refs") {
|
|
165
|
+
const asked = json();
|
|
166
|
+
const name = String(asked.ref ?? "").replace(/^refs\/heads\//, "");
|
|
167
|
+
if (state.branches.has(name))
|
|
168
|
+
return answer(res, 422, { message: "Reference already exists" });
|
|
169
|
+
state.branches.set(name, String(asked.sha));
|
|
170
|
+
return answer(res, 201, { ref: asked.ref, object: { sha: asked.sha } });
|
|
171
|
+
}
|
|
172
|
+
const gone = /^\/git\/refs\/heads\/(.+)$/.exec(rest);
|
|
173
|
+
if (req.method === "DELETE" && gone)
|
|
174
|
+
return state.branches.delete(decodeURIComponent(gone[1])) ? answer(res, 204) : answer(res, 422, { message: "Reference does not exist" });
|
|
175
|
+
if (req.method === "POST" && rest === "/pulls") {
|
|
176
|
+
if (installation.permissions.pull_requests !== "write")
|
|
177
|
+
return answer(res, 403, { message: "Resource not accessible by integration" });
|
|
178
|
+
const asked = json();
|
|
179
|
+
if (!state.branches.has(String(asked.head)))
|
|
180
|
+
return answer(res, 422, { message: "Validation Failed" });
|
|
181
|
+
if (state.pulls.some((p) => p.repo === full && p.head === asked.head))
|
|
182
|
+
return answer(res, 422, { message: "A pull request already exists" });
|
|
183
|
+
const number = state.pulls.length + 1;
|
|
184
|
+
const pull = { number, title: String(asked.title), head: String(asked.head), base: String(asked.base), body: String(asked.body), html_url: `https://github.com/${full}/pull/${number}`, repo: full };
|
|
185
|
+
state.pulls.push(pull);
|
|
186
|
+
return answer(res, 201, pull);
|
|
187
|
+
}
|
|
188
|
+
if (req.method === "GET" && rest === "/pulls") {
|
|
189
|
+
const head = new URL(req.url ?? "/", "http://faux").searchParams.get("head")?.split(":")[1];
|
|
190
|
+
return answer(res, 200, state.pulls.filter((p) => p.repo === full && (!head || p.head === head)));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
63
193
|
return answer(res, 404, { message: "Not Found" });
|
|
64
194
|
};
|
|
65
195
|
const server = createServer((req, res) => {
|
|
@@ -70,6 +200,7 @@ export async function startFauxGitHub() {
|
|
|
70
200
|
await new Promise((resolve, reject) => server.once("error", reject).listen(0, host, () => resolve()));
|
|
71
201
|
return {
|
|
72
202
|
url: `http://${host}:${server.address().port}`,
|
|
203
|
+
...(web ? { web } : {}),
|
|
73
204
|
app,
|
|
74
205
|
close: () => new Promise((resolve) => (server.close(() => resolve()), server.closeAllConnections())),
|
|
75
206
|
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** A file the pull request adds: its path from the repository's root, and its text. */
|
|
2
|
+
export type NewFile = {
|
|
3
|
+
readonly path: string;
|
|
4
|
+
readonly content: string;
|
|
5
|
+
};
|
|
6
|
+
/** Why a write was refused: a file already there, the token's permissions, a branch name taken five times, or GitHub. */
|
|
7
|
+
export declare class GithubWriteRefused extends Error {
|
|
8
|
+
readonly kind: "file_exists" | "not_permitted" | "branch_taken" | "github_failed";
|
|
9
|
+
readonly path?: string | undefined;
|
|
10
|
+
constructor(kind: "file_exists" | "not_permitted" | "branch_taken" | "github_failed", message: string, path?: string | undefined);
|
|
11
|
+
}
|
|
12
|
+
/** The commit pushed: on `branch`, made over the default branch `base` at `baseSha`. */
|
|
13
|
+
export type Pushed = {
|
|
14
|
+
readonly branch: string;
|
|
15
|
+
readonly commit: string;
|
|
16
|
+
readonly base: string;
|
|
17
|
+
readonly baseSha: string;
|
|
18
|
+
};
|
|
19
|
+
/** The files of `files` that `repo` already has at `sha` (each asked by path: 200 there, 404 not). */
|
|
20
|
+
export declare function filesThere(api: string, token: string, repo: string, sha: string, files: readonly NewFile[], signal?: AbortSignal): Promise<string[]>;
|
|
21
|
+
/** `files` committed over the head of `repo`'s default branch, on a new branch named `branch` (or `branch-2` ... when
|
|
22
|
+
* that name is taken). Refused, with nothing written, when a file is already there. */
|
|
23
|
+
export declare function pushFiles(api: string, token: string, repo: string, change: {
|
|
24
|
+
branch: string;
|
|
25
|
+
message: string;
|
|
26
|
+
files: readonly NewFile[];
|
|
27
|
+
}, signal?: AbortSignal): Promise<Pushed>;
|
|
28
|
+
/** The pull request of `pushed.branch` into its base, or the open one GitHub already has for that branch. */
|
|
29
|
+
export declare function openPull(api: string, token: string, repo: string, pushed: Pick<Pushed, "branch" | "base">, text: {
|
|
30
|
+
title: string;
|
|
31
|
+
body: string;
|
|
32
|
+
}, signal?: AbortSignal): Promise<{
|
|
33
|
+
number: number;
|
|
34
|
+
url: string;
|
|
35
|
+
}>;
|
|
36
|
+
/** The branch removed: what a create that failed after its push leaves behind. Best effort; false when GitHub did not. */
|
|
37
|
+
export declare function deleteBranch(api: string, token: string, repo: string, branch: string): Promise<boolean>;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// A pull request that adds files to one repository, made through GitHub's REST API with one installation token of the
|
|
2
|
+
// server's GitHub App, narrowed to that repository with contents and pull requests write (suggestions.ts mints it): the
|
|
3
|
+
// default branch's head, a tree of the new files over it, a commit, a branch at that commit, then the pull request. No git
|
|
4
|
+
// binary and no clone: GitHub's Git Data API. A file the base already has is refused before anything is written, so a pull
|
|
5
|
+
// request made here only ever adds files, and never touches a workflow (`.github/`), which the caller refuses first.
|
|
6
|
+
// What GitHub answers is worded here; its own words are never passed on whole.
|
|
7
|
+
import { githubCall } from "./github.js";
|
|
8
|
+
import { isObject } from "./validate.js";
|
|
9
|
+
/** Why a write was refused: a file already there, the token's permissions, a branch name taken five times, or GitHub. */
|
|
10
|
+
export class GithubWriteRefused extends Error {
|
|
11
|
+
kind;
|
|
12
|
+
path;
|
|
13
|
+
constructor(kind, message, path) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.kind = kind;
|
|
16
|
+
this.path = path;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** How many names a branch is tried under (`b`, `b-2`, ... `b-5`) before it is refused. */
|
|
20
|
+
const BRANCH_TRIES = 5;
|
|
21
|
+
const segments = (path) => path.split("/").map(encodeURIComponent).join("/");
|
|
22
|
+
const field = (json, name) => (isObject(json) ? json[name] : undefined);
|
|
23
|
+
/** A refusal for an answer GitHub gave a write: 403 and 404 are the token's permissions (GitHub answers 404 for a write
|
|
24
|
+
* an installation may not make on a repository it can read), anything else GitHub's failure. */
|
|
25
|
+
function refused(step, repo, status) {
|
|
26
|
+
if (status === 403 || status === 404)
|
|
27
|
+
return new GithubWriteRefused("not_permitted", `GitHub refused to ${step} on ${repo} (HTTP ${status}): the GitHub App's installation needs Contents: write and Pull requests: write there.`);
|
|
28
|
+
return new GithubWriteRefused("github_failed", `GitHub did not ${step} on ${repo} (HTTP ${status}).`);
|
|
29
|
+
}
|
|
30
|
+
/** The default branch of `repo` and its head, read with `token`. */
|
|
31
|
+
async function defaultHead(api, token, repo, signal) {
|
|
32
|
+
const opts = { jwt: token, ...(signal ? { signal } : {}) };
|
|
33
|
+
const about = await githubCall(api, `/repos/${repo}`, opts);
|
|
34
|
+
const base = field(about.json, "default_branch");
|
|
35
|
+
if (about.status !== 200 || typeof base !== "string" || !base)
|
|
36
|
+
throw refused("read the repository", repo, about.status);
|
|
37
|
+
const ref = await githubCall(api, `/repos/${repo}/git/ref/heads/${segments(base)}`, opts);
|
|
38
|
+
const baseSha = field(field(ref.json, "object"), "sha");
|
|
39
|
+
if (ref.status !== 200 || typeof baseSha !== "string")
|
|
40
|
+
throw refused(`read the head of ${base}`, repo, ref.status);
|
|
41
|
+
const commit = await githubCall(api, `/repos/${repo}/git/commits/${baseSha}`, opts);
|
|
42
|
+
const tree = field(field(commit.json, "tree"), "sha");
|
|
43
|
+
if (commit.status !== 200 || typeof tree !== "string")
|
|
44
|
+
throw refused(`read the commit ${baseSha}`, repo, commit.status);
|
|
45
|
+
return { base, baseSha, tree };
|
|
46
|
+
}
|
|
47
|
+
/** The files of `files` that `repo` already has at `sha` (each asked by path: 200 there, 404 not). */
|
|
48
|
+
export async function filesThere(api, token, repo, sha, files, signal) {
|
|
49
|
+
const there = [];
|
|
50
|
+
for (const file of files) {
|
|
51
|
+
const read = await githubCall(api, `/repos/${repo}/contents/${segments(file.path)}?ref=${sha}`, { jwt: token, ...(signal ? { signal } : {}) });
|
|
52
|
+
if (read.status === 200)
|
|
53
|
+
there.push(file.path);
|
|
54
|
+
else if (read.status !== 404)
|
|
55
|
+
throw refused(`read ${file.path}`, repo, read.status);
|
|
56
|
+
}
|
|
57
|
+
return there;
|
|
58
|
+
}
|
|
59
|
+
/** `files` committed over the head of `repo`'s default branch, on a new branch named `branch` (or `branch-2` ... when
|
|
60
|
+
* that name is taken). Refused, with nothing written, when a file is already there. */
|
|
61
|
+
export async function pushFiles(api, token, repo, change, signal) {
|
|
62
|
+
const opts = { jwt: token, ...(signal ? { signal } : {}) };
|
|
63
|
+
const { base, baseSha, tree } = await defaultHead(api, token, repo, signal);
|
|
64
|
+
const there = await filesThere(api, token, repo, baseSha, change.files, signal);
|
|
65
|
+
if (there.length)
|
|
66
|
+
throw new GithubWriteRefused("file_exists", `${repo} already has ${there.join(", ")} on ${base}: a suggested agent's pull request only adds files, so nothing was written.`, there[0]);
|
|
67
|
+
const made = await githubCall(api, `/repos/${repo}/git/trees`, { ...opts, method: "POST", body: { base_tree: tree, tree: change.files.map((file) => ({ path: file.path, mode: "100644", type: "blob", content: file.content })) } });
|
|
68
|
+
const newTree = field(made.json, "sha");
|
|
69
|
+
if (made.status !== 201 || typeof newTree !== "string")
|
|
70
|
+
throw refused("write the files", repo, made.status);
|
|
71
|
+
const committed = await githubCall(api, `/repos/${repo}/git/commits`, { ...opts, method: "POST", body: { message: change.message, tree: newTree, parents: [baseSha] } });
|
|
72
|
+
const commit = field(committed.json, "sha");
|
|
73
|
+
if (committed.status !== 201 || typeof commit !== "string")
|
|
74
|
+
throw refused("commit the files", repo, committed.status);
|
|
75
|
+
for (let i = 1; i <= BRANCH_TRIES; i++) {
|
|
76
|
+
const branch = i === 1 ? change.branch : `${change.branch}-${i}`;
|
|
77
|
+
const ref = await githubCall(api, `/repos/${repo}/git/refs`, { ...opts, method: "POST", body: { ref: `refs/heads/${branch}`, sha: commit } });
|
|
78
|
+
if (ref.status === 201)
|
|
79
|
+
return { branch, commit, base, baseSha };
|
|
80
|
+
// 422: the name is taken (GitHub's "Reference already exists"): the next name.
|
|
81
|
+
if (ref.status !== 422)
|
|
82
|
+
throw refused(`make the branch ${branch}`, repo, ref.status);
|
|
83
|
+
}
|
|
84
|
+
throw new GithubWriteRefused("branch_taken", `${repo} already has branches ${change.branch} to ${change.branch}-${BRANCH_TRIES}: delete the old ones, then create again.`);
|
|
85
|
+
}
|
|
86
|
+
/** The pull request of `pushed.branch` into its base, or the open one GitHub already has for that branch. */
|
|
87
|
+
export async function openPull(api, token, repo, pushed, text, signal) {
|
|
88
|
+
const opts = { jwt: token, ...(signal ? { signal } : {}) };
|
|
89
|
+
const opened = await githubCall(api, `/repos/${repo}/pulls`, { ...opts, method: "POST", body: { title: text.title, head: pushed.branch, base: pushed.base, body: text.body } });
|
|
90
|
+
const number = field(opened.json, "number");
|
|
91
|
+
const url = field(opened.json, "html_url");
|
|
92
|
+
if (opened.status === 201 && Number.isSafeInteger(number) && typeof url === "string")
|
|
93
|
+
return { number: number, url };
|
|
94
|
+
if (opened.status === 422) {
|
|
95
|
+
// One already open for this branch (a create sent again): that one.
|
|
96
|
+
const owner = repo.split("/")[0];
|
|
97
|
+
const open = await githubCall(api, `/repos/${repo}/pulls?state=open&head=${encodeURIComponent(`${owner}:${pushed.branch}`)}`, opts);
|
|
98
|
+
const first = Array.isArray(open.json) ? open.json[0] : undefined;
|
|
99
|
+
const n = field(first, "number");
|
|
100
|
+
const u = field(first, "html_url");
|
|
101
|
+
if (open.status === 200 && Number.isSafeInteger(n) && typeof u === "string")
|
|
102
|
+
return { number: n, url: u };
|
|
103
|
+
}
|
|
104
|
+
throw refused(`open the pull request from ${pushed.branch}`, repo, opened.status);
|
|
105
|
+
}
|
|
106
|
+
/** The branch removed: what a create that failed after its push leaves behind. Best effort; false when GitHub did not. */
|
|
107
|
+
export async function deleteBranch(api, token, repo, branch) {
|
|
108
|
+
const gone = await githubCall(api, `/repos/${repo}/git/refs/heads/${segments(branch)}`, { method: "DELETE", jwt: token }).catch(() => undefined);
|
|
109
|
+
return gone?.status === 204;
|
|
110
|
+
}
|
|
@@ -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 {};
|