@sema-agent/core 7.73.0 → 7.74.0
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 +246 -0
- package/dist/agents/list-agents-tool.js +1 -1
- package/dist/agents/retain-ledger.js +3 -3
- package/dist/agents/send-message-tool.js +4 -4
- package/dist/brain/anthropic.d.ts +42 -1
- package/dist/brain/anthropic.js +54 -4
- package/dist/brain/failover.d.ts +5 -0
- package/dist/brain/failover.js +51 -22
- package/dist/brain/model-lane.d.ts +12 -3
- package/dist/brain/model-lane.js +5 -5
- package/dist/brain/open-responses.js +3 -3
- package/dist/brain/openai.js +1 -1
- package/dist/brain/route-adjudicator.d.ts +33 -4
- package/dist/brain/route-adjudicator.js +49 -12
- package/dist/brain/stream-engine.js +9 -7
- package/dist/brain/stream-shared.d.ts +4 -3
- package/dist/brain/stream-shared.js +2 -2
- package/dist/core/allow-entry.js +1 -1
- package/dist/core/ask-class.d.ts +2 -2
- package/dist/core/ask-headline.d.ts +1 -1
- package/dist/core/ask-origin.d.ts +44 -4
- package/dist/core/ask-origin.js +29 -10
- package/dist/core/background-agent-store.d.ts +1 -0
- package/dist/core/background-agent-store.js +49 -8
- package/dist/core/checkpoint-row-verdict.d.ts +1 -1
- package/dist/core/checkpoint-row-verdict.js +2 -2
- package/dist/core/checkpoint-store.d.ts +2 -0
- package/dist/core/checkpoint-store.js +8 -2
- package/dist/core/config-refusal-codes.d.ts +1 -1
- package/dist/core/config-refusal-codes.js +1 -0
- package/dist/core/cron.js +9 -1
- package/dist/core/deployment-shape.d.ts +7 -0
- package/dist/core/deployment-shape.js +19 -0
- package/dist/core/engine-capabilities.d.ts +9 -1
- package/dist/core/engine-capabilities.js +1 -1
- package/dist/core/engine-notice.d.ts +14 -9
- package/dist/core/engine-version.js +1 -1
- package/dist/core/gate-fold.js +5 -4
- package/dist/core/gate-lanes.js +8 -12
- package/dist/core/gate-outcome.d.ts +8 -2
- package/dist/core/gate-outcome.js +10 -3
- package/dist/core/governance-codes.d.ts +8 -2
- package/dist/core/governance-codes.js +9 -1
- package/dist/core/hook-condition.d.ts +37 -0
- package/dist/core/hook-condition.js +567 -0
- package/dist/core/hooks.d.ts +2 -0
- package/dist/core/mode-policy.d.ts +4 -0
- package/dist/core/mode-policy.js +18 -2
- package/dist/core/model-seat.d.ts +8 -5
- package/dist/core/model-shell.d.ts +129 -0
- package/dist/core/model-shell.js +249 -0
- package/dist/core/mods/kernel/digest.d.ts +4 -0
- package/dist/core/mods/kernel/digest.js +5 -0
- package/dist/core/mods/kernel/dispatch.d.ts +3 -0
- package/dist/core/mods/kernel/dispatch.js +15 -6
- package/dist/core/mods/kernel/events.d.ts +21 -5
- package/dist/core/mods/kernel/events.js +19 -10
- package/dist/core/mods/kernel/hash-mirror.d.ts +1 -0
- package/dist/core/mods/kernel/hash-mirror.js +55 -0
- package/dist/core/mods/kernel/index.d.ts +1 -0
- package/dist/core/mods/kernel/index.js +1 -0
- package/dist/core/mods/kernel/package-reader.d.ts +48 -0
- package/dist/core/mods/kernel/package-reader.js +149 -0
- package/dist/core/mods/kernel/site-rules.d.ts +4 -0
- package/dist/core/mods/kernel/site-rules.js +13 -0
- package/dist/core/mods/package.d.ts +7 -3
- package/dist/core/mods/package.js +47 -80
- package/dist/core/permission-rule-org.js +13 -3
- package/dist/core/permission-rules.js +4 -1
- package/dist/core/retain-seat.d.ts +42 -0
- package/dist/core/retain-seat.js +76 -0
- package/dist/core/rewind-refusal-codes.d.ts +8 -0
- package/dist/core/rewind-refusal-codes.js +12 -0
- package/dist/core/roles.d.ts +2 -2
- package/dist/core/roles.js +15 -1
- package/dist/core/rule-store-fault.d.ts +9 -7
- package/dist/core/rule-store-fault.js +8 -3
- package/dist/core/runner/checkpoint-scope.d.ts +1 -0
- package/dist/core/runner/checkpoint-scope.js +4 -2
- package/dist/core/runner/compact-session.js +1 -1
- package/dist/core/runner/construction-doors.js +2 -0
- package/dist/core/runner/cross-boundary-lane.d.ts +8 -1
- package/dist/core/runner/cross-boundary-lane.js +4 -0
- package/dist/core/runner/gate-exit.d.ts +7 -0
- package/dist/core/runner/gate-exit.js +2 -1
- package/dist/core/runner/model-lane-seat.d.ts +3 -0
- package/dist/core/runner/model-lane-seat.js +4 -0
- package/dist/core/runner/person-path-rows.d.ts +6 -0
- package/dist/core/runner/person-path-rows.js +13 -0
- package/dist/core/runner/prepare-config-doors.js +9 -5
- package/dist/core/runner/prepare-gate-stations.d.ts +6 -0
- package/dist/core/runner/prepare-gate-stations.js +3 -1
- package/dist/core/runner/prepare-hands-readface.js +3 -2
- package/dist/core/runner/prepare-offload-wrappers.js +1 -1
- package/dist/core/runner/prepare-task.js +6 -3
- package/dist/core/runner/prepare-wiring-manifest.js +3 -4
- package/dist/core/runner/resume-policy-outcome.js +5 -2
- package/dist/core/runner/role-seat-defaulted.d.ts +3 -3
- package/dist/core/runner/role-seat-defaulted.js +1 -1
- package/dist/core/runner/rule-store-unreadable-notice.d.ts +1 -1
- package/dist/core/runner/rule-store-unreadable-notice.js +3 -3
- package/dist/core/runner/run-harness-handlers.js +1 -1
- package/dist/core/runner/run-leg.js +2 -2
- package/dist/core/runner/shell-env-withheld.d.ts +3 -2
- package/dist/core/runner/shell-env-withheld.js +24 -1
- package/dist/core/runner/sub-agent-deny-lists.js +9 -5
- package/dist/core/runner/tool-end-body.d.ts +1 -1
- package/dist/core/runner/tool-not-executed.d.ts +27 -8
- package/dist/core/runner/tool-not-executed.js +22 -11
- package/dist/core/runner/write-protection-station.d.ts +1 -1
- package/dist/core/runner/write-protection-station.js +1 -1
- package/dist/core/runner-deps.d.ts +4 -0
- package/dist/core/secret-env.d.ts +8 -0
- package/dist/core/secret-env.js +5 -2
- package/dist/core/shell-read-silence.d.ts +40 -2
- package/dist/core/shell-read-silence.js +23 -4
- package/dist/core/task-event.d.ts +4 -4
- package/dist/core/task-notification.js +6 -1
- package/dist/core/task-registry-agent.d.ts +1 -0
- package/dist/core/task-registry-agent.js +1 -1
- package/dist/core/task-registry.d.ts +1 -0
- package/dist/core/task-registry.js +4 -3
- package/dist/core/task-result.d.ts +5 -3
- package/dist/core/task-spec-members.js +8 -0
- package/dist/core/tool-gate-phase.d.ts +6 -3
- package/dist/core/tool-gate-phase.js +1 -0
- package/dist/core/tool-mount-facts.d.ts +7 -3
- package/dist/core/tool-mount-facts.js +6 -2
- package/dist/core/tool-mount-plan.d.ts +1 -1
- package/dist/core/tool-mount-plan.js +4 -3
- package/dist/core/tool-result-store.d.ts +37 -1
- package/dist/core/tool-result-store.js +168 -23
- package/dist/core/trace.d.ts +3 -3
- package/dist/engine/execution-env/node-execution-env.d.ts +75 -1
- package/dist/engine/execution-env/node-execution-env.js +225 -13
- package/dist/engine/harness/types.d.ts +25 -0
- package/dist/engine/llm/types.d.ts +8 -0
- package/dist/engine/llm/validation.d.ts +8 -1
- package/dist/engine/llm/validation.js +31 -2
- package/dist/host-env.d.ts +10 -0
- package/dist/host-env.js +1 -0
- package/dist/index.d.ts +10 -2
- package/dist/index.js +9 -2
- package/dist/internal/harness-types.d.ts +1 -1
- package/dist/orchestration/goal.js +1 -1
- package/dist/orchestration/workflow-primitives.js +10 -1
- package/dist/orchestration/workflow-script-runner.d.ts +2 -2
- package/dist/orchestration/workflow.d.ts +1 -1
- package/dist/orchestration/workflow.js +2 -2
- package/dist/shell/lex.d.ts +2 -2
- package/dist/shell/lex.js +1 -1
- package/dist/tools/fs/bash-model-shell.d.ts +23 -0
- package/dist/tools/fs/bash-model-shell.js +25 -0
- package/dist/tools/fs/bash-readonly-classifier.d.ts +6 -0
- package/dist/tools/fs/bash-readonly-classifier.js +3 -0
- package/dist/tools/fs/fs-bash.js +13 -11
- package/dist/tools/fs/shell-read-argv.js +1 -0
- package/dist/tools/fs/shell-read-check.js +39 -9
- package/dist/tools/fs/shell-read-plan.d.ts +2 -3
- package/dist/tools/fs/shell-read-plan.js +8 -5
- package/dist/tools/fs/shell-read-word.d.ts +8 -0
- package/dist/tools/fs/shell-read-word.js +13 -4
- package/dist/tools/monitor.js +12 -8
- package/dist/tools/scheduler-tools.d.ts +1 -0
- package/dist/tools/scheduler-tools.js +5 -2
- package/package.json +3 -2
- package/test/export-surface.snapshot.json +62 -3
- package/wire-changes/answers/core-stores-sql@7.74.0.json +7 -0
- package/wire-changes/answers/core@7.74.0.json +68 -0
- package/wire-changes/answers/mods-kernel@7.74.0.json +21 -0
- package/wire-changes/answers/primitives@7.74.0.json +7 -0
- package/wire-changes/answers/wiki-gate@7.74.0.json +8 -0
- package/wire-changes/core-stores-sql@7.74.0.json +143 -0
- package/wire-changes/core@7.74.0.json +2210 -0
- package/wire-changes/mods-kernel@7.74.0.json +421 -0
- package/wire-changes/primitives@7.74.0.json +174 -0
- package/wire-changes/wiki-gate@7.74.0.json +145 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const K = new Uint32Array([
|
|
2
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe,
|
|
3
|
+
0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
|
|
4
|
+
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
|
5
|
+
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
6
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
7
|
+
]);
|
|
8
|
+
const H0 = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
|
|
9
|
+
const rotr = (x, n) => (x >>> n) | (x << (32 - n));
|
|
10
|
+
export function sha256Hex(data) {
|
|
11
|
+
const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
12
|
+
const blocks = Math.ceil((bytes.length + 9) / 64);
|
|
13
|
+
const padded = new Uint8Array(blocks * 64);
|
|
14
|
+
padded.set(bytes);
|
|
15
|
+
padded[bytes.length] = 0x80;
|
|
16
|
+
const view = new DataView(padded.buffer);
|
|
17
|
+
const bits = bytes.length * 8;
|
|
18
|
+
view.setUint32(padded.length - 8, Math.floor(bits / 0x1_0000_0000));
|
|
19
|
+
view.setUint32(padded.length - 4, bits >>> 0);
|
|
20
|
+
const h = [...H0];
|
|
21
|
+
const w = new Uint32Array(64);
|
|
22
|
+
for (let block = 0; block < blocks; block++) {
|
|
23
|
+
for (let t = 0; t < 16; t++)
|
|
24
|
+
w[t] = view.getUint32(block * 64 + t * 4);
|
|
25
|
+
for (let t = 16; t < 64; t++) {
|
|
26
|
+
const x = w[t - 15];
|
|
27
|
+
const y = w[t - 2];
|
|
28
|
+
const s0 = rotr(x, 7) ^ rotr(x, 18) ^ (x >>> 3);
|
|
29
|
+
const s1 = rotr(y, 17) ^ rotr(y, 19) ^ (y >>> 10);
|
|
30
|
+
w[t] = (w[t - 16] + s0 + w[t - 7] + s1) >>> 0;
|
|
31
|
+
}
|
|
32
|
+
let [a, b, c, d, e, f, g, hh] = h;
|
|
33
|
+
for (let t = 0; t < 64; t++) {
|
|
34
|
+
const t1 = (hh + (rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)) + ((e & f) ^ (~e & g)) + K[t] + w[t]) >>> 0;
|
|
35
|
+
const t2 = ((rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) >>> 0;
|
|
36
|
+
hh = g;
|
|
37
|
+
g = f;
|
|
38
|
+
f = e;
|
|
39
|
+
e = (d + t1) >>> 0;
|
|
40
|
+
d = c;
|
|
41
|
+
c = b;
|
|
42
|
+
b = a;
|
|
43
|
+
a = (t1 + t2) >>> 0;
|
|
44
|
+
}
|
|
45
|
+
h[0] = (h[0] + a) >>> 0;
|
|
46
|
+
h[1] = (h[1] + b) >>> 0;
|
|
47
|
+
h[2] = (h[2] + c) >>> 0;
|
|
48
|
+
h[3] = (h[3] + d) >>> 0;
|
|
49
|
+
h[4] = (h[4] + e) >>> 0;
|
|
50
|
+
h[5] = (h[5] + f) >>> 0;
|
|
51
|
+
h[6] = (h[6] + g) >>> 0;
|
|
52
|
+
h[7] = (h[7] + hh) >>> 0;
|
|
53
|
+
}
|
|
54
|
+
return h.map((x) => x.toString(16).padStart(8, "0")).join("");
|
|
55
|
+
}
|
|
@@ -4,3 +4,4 @@ export { DEFAULT_MOD_BUDGET_MS, CATCH_BUDGET_MS, RUNAWAY_AFTER_MS, POST_ABORT_GR
|
|
|
4
4
|
export { MOD_FAILURE_CAUSES, type ModFailureCause, type ModEventPayload, type ModAnswerCheck, type ModAnswerRanks, type ModDispatchRun, type ModAnswerer, type ModMatcher, type ModSkip, } from "./site-rules.js";
|
|
5
5
|
export { ENGINE_ORIGIN, MODS_INVALID_CODE, MODS_REGISTRATION_REFUSED_CODE, MODS_REGISTRATION_REFUSED_DEPLOYMENT_CODE, MOD_TRACE_OUTCOMES, type ModTraceOutcome, type ModUses, type ModOrigin, type ModPluginFacts, type ModDollarBase, type ModTraceEntry, type ModCaughtError, type ModNext, type ModHandler, type ModHandlerOptions, type ModEventPair, type ModEventMap, type ModTypedNext, type ModTypedHandler, type On, type MappedOn, type ModRegistration, type ModAdmissionRecord, type ModSeats, } from "./registration.js";
|
|
6
6
|
export { admitMods, type ModFrame, type ModBottomContext, type ModMethodBottom, type ModMethodSeat, type ModMethodBottoms, type ModCallEntry, type ModCallEntries, type ModDispatchOutcome, type ModDispatchOptions, type ModParticipant, type ModBus, type ModMountedHandler, type ModBusInput, type ModTimer, } from "./dispatch.js";
|
|
7
|
+
export { MOD_PACKAGE_FILES, readModPackage, type ModPackageSource, type ModPackageRead, type ModPackageUnreadMember } from "./package-reader.js";
|
|
@@ -3,3 +3,4 @@ export { DEFAULT_MOD_BUDGET_MS, CATCH_BUDGET_MS, RUNAWAY_AFTER_MS, POST_ABORT_GR
|
|
|
3
3
|
export { MOD_FAILURE_CAUSES, } from "./site-rules.js";
|
|
4
4
|
export { ENGINE_ORIGIN, MODS_INVALID_CODE, MODS_REGISTRATION_REFUSED_CODE, MODS_REGISTRATION_REFUSED_DEPLOYMENT_CODE, MOD_TRACE_OUTCOMES, } from "./registration.js";
|
|
5
5
|
export { admitMods, } from "./dispatch.js";
|
|
6
|
+
export { MOD_PACKAGE_FILES, readModPackage } from "./package-reader.js";
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type ModBusSide } from "./events.js";
|
|
2
|
+
import { type ModUses } from "./registration.js";
|
|
3
|
+
/** The two manifests, by their path inside the package (`/`-separated on every platform). */
|
|
4
|
+
export declare const MOD_PACKAGE_FILES: Readonly<{
|
|
5
|
+
readonly plugin: ".claude-plugin/plugin.json";
|
|
6
|
+
readonly hooks: "hooks/hooks.json";
|
|
7
|
+
}>;
|
|
8
|
+
/** What the host hands the reader: never read from the package. */
|
|
9
|
+
export interface ModPackageSource {
|
|
10
|
+
/** The bytes of `.claude-plugin/plugin.json` and of `hooks/hooks.json`. */
|
|
11
|
+
readonly plugin: Uint8Array;
|
|
12
|
+
readonly hooks: Uint8Array;
|
|
13
|
+
/** Reads the hooks module, named by its path inside the package (`hooks/…`, `/`-separated); asked once, after both manifests
|
|
14
|
+
* are judged. It IS the containment: it must refuse a path that leaves the package directory, a link included — the reader
|
|
15
|
+
* judges the name's form only, never where the bytes came from. What it throws comes out as thrown. */
|
|
16
|
+
readonly readModule: (path: string) => Uint8Array | Promise<Uint8Array>;
|
|
17
|
+
/** The bus the host reads the package for. */
|
|
18
|
+
readonly side: ModBusSide;
|
|
19
|
+
/** The digest the host pinned: other bytes are refused (`detail.digest { expected, actual }`) before a module byte is handed
|
|
20
|
+
* out. A match proves the bytes are the pinned ones, nothing more: the digest is not admission. */
|
|
21
|
+
readonly expectDigest?: string;
|
|
22
|
+
}
|
|
23
|
+
/** A member of a manifest the reader does not read, by its name as written. */
|
|
24
|
+
export interface ModPackageUnreadMember {
|
|
25
|
+
readonly file: string;
|
|
26
|
+
readonly name: string;
|
|
27
|
+
}
|
|
28
|
+
/** A package read: its facts as it states them, and the bytes they were read from. */
|
|
29
|
+
export interface ModPackageRead {
|
|
30
|
+
/** `plugin.json`'s `name` as stated (the kernel's screen judges it at admission). */
|
|
31
|
+
readonly name: unknown;
|
|
32
|
+
/** `hooks.json`'s `uses`: every item selects a name on the host's side, one at least of that side's own. */
|
|
33
|
+
readonly uses: ModUses;
|
|
34
|
+
/** `hooks.json`'s `contract` as stated: the importing copy compares it with its own `KERNEL_CONTRACT_VERSION`. */
|
|
35
|
+
readonly contract: unknown;
|
|
36
|
+
/** The members of the two files the reader does not read (`plugin.json`'s first, each in its parsed object's key order; a member
|
|
37
|
+
* of `hooks.json`'s `uses` other than `events` and `calls` by its dotted name, `uses.<name>`, after that file's own). */
|
|
38
|
+
readonly unread: readonly ModPackageUnreadMember[];
|
|
39
|
+
/** The hooks module's path inside the package (as `readModule` was asked for it) and its bytes, those the digest was taken over. */
|
|
40
|
+
readonly modulePath: string;
|
|
41
|
+
readonly module: Uint8Array;
|
|
42
|
+
/** The sha256 (lowercase hex) of the newline-joined sha256 of `plugin.json`, `hooks.json` and the module, in that order. */
|
|
43
|
+
readonly digest: string;
|
|
44
|
+
}
|
|
45
|
+
/** Read a package from its bytes; see the file's head. */
|
|
46
|
+
export declare function readModPackage(source: ModPackageSource): Promise<ModPackageRead>;
|
|
47
|
+
/** A value's kind for a sentence — never a string's text. */
|
|
48
|
+
export declare function kind(v: unknown): string;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { modPackageDigest } from "./digest.js";
|
|
2
|
+
import { expandModPattern, expandOnSide, MOD_EVENT_TABLE } from "./events.js";
|
|
3
|
+
import { modsInvalid } from "./registration.js";
|
|
4
|
+
export const MOD_PACKAGE_FILES = Object.freeze({ plugin: ".claude-plugin/plugin.json", hooks: "hooks/hooks.json" });
|
|
5
|
+
const SOURCE_MEMBERS = ["plugin", "hooks", "readModule", "side", "expectDigest"];
|
|
6
|
+
const SHA256_HEX = /^[0-9a-f]{64}$/;
|
|
7
|
+
const HOST_FACT = "which is the host's to give (the seat, tier, provenance, options and budget are never read from a package)";
|
|
8
|
+
const HOST_FACTS = Object.freeze({ tier: HOST_FACT, provenance: HOST_FACT, seat: HOST_FACT, options: HOST_FACT, budgetMs: HOST_FACT });
|
|
9
|
+
const PLUGIN_SHAPE = Object.freeze({ file: MOD_PACKAGE_FILES.plugin, takes: Object.freeze(["name", "version", "description", "author"]), refuses: HOST_FACTS });
|
|
10
|
+
const HOOKS_SHAPE = Object.freeze({
|
|
11
|
+
file: MOD_PACKAGE_FILES.hooks,
|
|
12
|
+
takes: Object.freeze(["modules", "contract", "uses", "description"]),
|
|
13
|
+
refuses: Object.freeze({ ...HOST_FACTS, surface: "which names a surface module: a surface module is mounted by the client that renders it, never through hooks.json" }),
|
|
14
|
+
});
|
|
15
|
+
const USES_SHAPE = Object.freeze({ file: MOD_PACKAGE_FILES.hooks, takes: Object.freeze(["events", "calls"]), refuses: Object.freeze({}) });
|
|
16
|
+
const MODULE_MEMBER = `${MOD_PACKAGE_FILES.hooks}: modules[0]`;
|
|
17
|
+
const PATH_RULE = 'a hooks module is named by one path relative to hooks.json: "/"-separated, an optional leading "./", no empty, "." or ".." segment, no "\\" or ":", ending ".mjs"';
|
|
18
|
+
export async function readModPackage(source) {
|
|
19
|
+
const { plugin, hooks, readModule, side, expectDigest } = sourceOf(source);
|
|
20
|
+
const unread = [];
|
|
21
|
+
const pluginJson = jsonRecord(plugin, PLUGIN_SHAPE, unread);
|
|
22
|
+
const hooksJson = jsonRecord(hooks, HOOKS_SHAPE, unread);
|
|
23
|
+
const modulePath = modulePathOf(own(hooksJson, "modules"));
|
|
24
|
+
const uses = usesOn(own(hooksJson, "uses"), side, unread);
|
|
25
|
+
const answered = await readModule(modulePath);
|
|
26
|
+
if (!isBytes(answered))
|
|
27
|
+
throw modsInvalid(`${MODULE_MEMBER}: the host's readModule answered ${kind(answered)}; it answers the module's bytes`);
|
|
28
|
+
const module = new Uint8Array(answered);
|
|
29
|
+
const digest = modPackageDigest(plugin, hooks, module);
|
|
30
|
+
if (expectDigest !== undefined && digest !== expectDigest) {
|
|
31
|
+
throw Object.assign(modsInvalid(`its bytes are not the ones the host expected (digest ${digest}, expected ${expectDigest}); nothing of it is handed on`), {
|
|
32
|
+
detail: Object.freeze({ digest: Object.freeze({ expected: expectDigest, actual: digest }) }),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return Object.freeze({
|
|
36
|
+
name: own(pluginJson, "name"),
|
|
37
|
+
uses,
|
|
38
|
+
contract: own(hooksJson, "contract"),
|
|
39
|
+
unread: Object.freeze(unread.map((u) => Object.freeze(u))),
|
|
40
|
+
modulePath,
|
|
41
|
+
module,
|
|
42
|
+
digest,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function sourceOf(source) {
|
|
46
|
+
const form = `it is { ${SOURCE_MEMBERS.join(", ")} }`;
|
|
47
|
+
if (typeof source !== "object" || source === null || Array.isArray(source))
|
|
48
|
+
throw modsInvalid(`the package reader's argument is ${kind(source)}; ${form}`);
|
|
49
|
+
for (const k of Reflect.ownKeys(source)) {
|
|
50
|
+
if (typeof k === "symbol" || !SOURCE_MEMBERS.includes(k)) {
|
|
51
|
+
throw modsInvalid(`the package reader's argument carries ${typeof k === "symbol" ? "a symbol-keyed member" : JSON.stringify(k).slice(0, 66)}, a member it does not take; ${form}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const plugin = Reflect.get(source, "plugin");
|
|
55
|
+
const hooks = Reflect.get(source, "hooks");
|
|
56
|
+
const readModule = Reflect.get(source, "readModule");
|
|
57
|
+
const side = Reflect.get(source, "side");
|
|
58
|
+
if (!isBytes(plugin) || !isBytes(hooks))
|
|
59
|
+
throw modsInvalid(`the package reader's plugin and hooks are the two manifests' bytes (a Uint8Array each)`);
|
|
60
|
+
if (typeof readModule !== "function")
|
|
61
|
+
throw modsInvalid(`the package reader's readModule is ${kind(readModule)}; it is a function that reads the hooks module`);
|
|
62
|
+
if (side !== "engine" && side !== "surface")
|
|
63
|
+
throw modsInvalid(`the package reader's side is ${kind(side)}; it is the bus the host reads the package for, "engine" or "surface"`);
|
|
64
|
+
const expectDigest = Reflect.get(source, "expectDigest");
|
|
65
|
+
if (expectDigest !== undefined && (typeof expectDigest !== "string" || !SHA256_HEX.test(expectDigest))) {
|
|
66
|
+
throw modsInvalid(`the package reader's expectDigest is ${typeof expectDigest === "string" ? `a string of ${expectDigest.length} characters` : kind(expectDigest)}; it is a package digest, a sha256 in lowercase hex (64 characters)`);
|
|
67
|
+
}
|
|
68
|
+
return { plugin: new Uint8Array(plugin), hooks: new Uint8Array(hooks), readModule: readModule, side, expectDigest };
|
|
69
|
+
}
|
|
70
|
+
function isBytes(v) {
|
|
71
|
+
return ArrayBuffer.isView(v) && Object.prototype.toString.call(v) === "[object Uint8Array]";
|
|
72
|
+
}
|
|
73
|
+
function own(record, key) {
|
|
74
|
+
return Object.hasOwn(record, key) ? record[key] : undefined;
|
|
75
|
+
}
|
|
76
|
+
function jsonRecord(bytes, shape, unread) {
|
|
77
|
+
const { file, takes, refuses } = shape;
|
|
78
|
+
let parsed;
|
|
79
|
+
try {
|
|
80
|
+
parsed = JSON.parse(new TextDecoder("utf-8", { ignoreBOM: true }).decode(bytes));
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
throw modsInvalid(`${file}: is not JSON`);
|
|
84
|
+
}
|
|
85
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
|
|
86
|
+
throw modsInvalid(`${file}: is ${kind(parsed)}; it is a JSON object`);
|
|
87
|
+
unreadOf(parsed, { file, takes, refuses }, "", unread);
|
|
88
|
+
return parsed;
|
|
89
|
+
}
|
|
90
|
+
function unreadOf(record, shape, prefix, unread) {
|
|
91
|
+
const { file, takes, refuses } = shape;
|
|
92
|
+
for (const k of Object.keys(record)) {
|
|
93
|
+
if (takes.includes(k))
|
|
94
|
+
continue;
|
|
95
|
+
const why = Object.hasOwn(refuses, k) ? refuses[k] : undefined;
|
|
96
|
+
if (why !== undefined)
|
|
97
|
+
throw modsInvalid(`${file}${prefix === "" ? "" : ` ${prefix.slice(0, -1)}`}: carries "${k}", ${why}`);
|
|
98
|
+
unread.push({ file, name: `${prefix}${k}` });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function modulePathOf(modules) {
|
|
102
|
+
if (!Array.isArray(modules) || modules.length !== 1 || typeof modules[0] !== "string")
|
|
103
|
+
throw modsInvalid(`${MOD_PACKAGE_FILES.hooks}: modules is ${kind(modules)}; it lists exactly one path, relative to hooks.json`);
|
|
104
|
+
const given = modules[0];
|
|
105
|
+
const rest = given.startsWith("./") ? given.slice(2) : given;
|
|
106
|
+
const fits = rest.endsWith(".mjs") && rest.split("/").every((s) => s !== "" && s !== "." && s !== ".." && !s.includes("\\") && !s.includes(":"));
|
|
107
|
+
if (!fits)
|
|
108
|
+
throw modsInvalid(`${MODULE_MEMBER} is not a path this reader takes; ${PATH_RULE}`);
|
|
109
|
+
return `hooks/${rest}`;
|
|
110
|
+
}
|
|
111
|
+
function usesOn(uses, side, unread) {
|
|
112
|
+
const at = `${MOD_PACKAGE_FILES.hooks}: uses`;
|
|
113
|
+
if (typeof uses !== "object" || uses === null || Array.isArray(uses))
|
|
114
|
+
throw modsInvalid(`${at} is ${kind(uses)}; it declares { events, calls }, each a list of event names`);
|
|
115
|
+
unreadOf(uses, USES_SHAPE, "uses.", unread);
|
|
116
|
+
let ownSide = false;
|
|
117
|
+
const lists = { events: [], calls: [] };
|
|
118
|
+
for (const field of ["events", "calls"]) {
|
|
119
|
+
const list = Object.hasOwn(uses, field) ? Reflect.get(uses, field) : undefined;
|
|
120
|
+
if (!Array.isArray(list))
|
|
121
|
+
throw modsInvalid(`${at}.${field} is ${kind(list)}; it is a list of event names`);
|
|
122
|
+
list.forEach((item, i) => {
|
|
123
|
+
const where = `${at}.${field}[${i}]`;
|
|
124
|
+
if (typeof item !== "string")
|
|
125
|
+
throw modsInvalid(`${where} is ${kind(item)}; it is an event name or a "<prefix>.*" pattern`);
|
|
126
|
+
const selected = expandOnSide(item, side);
|
|
127
|
+
if ("refusal" in selected) {
|
|
128
|
+
throw modsInvalid("refusal" in expandModPattern(item)
|
|
129
|
+
? `${where} is not an event name, or a "<prefix>.*" pattern, that selects a name the kernel knows`
|
|
130
|
+
: `${where} selects only the ${side === "engine" ? "surface" : "engine"} side's names, and the host reads this package for the ${side} bus`);
|
|
131
|
+
}
|
|
132
|
+
if (selected.names.some((n) => MOD_EVENT_TABLE[n].side === side))
|
|
133
|
+
ownSide = true;
|
|
134
|
+
lists[field].push(item);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
if (!ownSide)
|
|
138
|
+
throw modsInvalid(`${at} selects no name of the ${side} side's own — only names both buses read, or none — so it does not say it is a package for the ${side} bus; a package declares one ${side} event or method at least`);
|
|
139
|
+
return Object.freeze({ events: Object.freeze(lists.events), calls: Object.freeze(lists.calls) });
|
|
140
|
+
}
|
|
141
|
+
export function kind(v) {
|
|
142
|
+
if (v === undefined)
|
|
143
|
+
return "absent";
|
|
144
|
+
if (v === null)
|
|
145
|
+
return "null";
|
|
146
|
+
if (Array.isArray(v))
|
|
147
|
+
return `a list of ${v.length}`;
|
|
148
|
+
return typeof v === "number" || typeof v === "boolean" ? String(v) : `a ${typeof v}`;
|
|
149
|
+
}
|
|
@@ -112,6 +112,10 @@ export declare function pinnedHandOn(pinned: readonly string[], held: ModEventPa
|
|
|
112
112
|
};
|
|
113
113
|
/** Why a handler's answer breaks the site, or undefined. No answer at all is never an answer. */
|
|
114
114
|
export declare function answerViolation(answer: unknown, check: ModAnswerCheck | undefined, e: ModEventPayload, by: ModAnswerer, below: readonly unknown[], run: ModDispatchRun): string | undefined;
|
|
115
|
+
/** A `closed` station's reading: the handed-on payload holds the dispatch's own event's members, no other and none fewer. */
|
|
116
|
+
export declare function sameMembers(handed: ModEventPayload, e0: ModEventPayload): boolean;
|
|
117
|
+
/** An `answers` station's reading of an answer of a handler's own (received): a record of the row's members alone. Never quotes it. */
|
|
118
|
+
export declare function ownAnswerViolation(answer: unknown, answers: readonly string[]): string | undefined;
|
|
115
119
|
/** A station's reading of what a handler hands on (and of who hands it on): its sentence, or undefined (no check, or the
|
|
116
120
|
* payload passes). A check that throws is a sentence too — the station could not read the payload, so it is not handed on. */
|
|
117
121
|
export declare function handOnViolation(payload: ModEventPayload, check: ((payload: ModEventPayload, by: ModAnswerer) => string | undefined) | undefined, by: ModAnswerer): string | undefined;
|
|
@@ -211,6 +211,19 @@ export function answerViolation(answer, check, e, by, below, run) {
|
|
|
211
211
|
return "returned an answer the station cannot read";
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
|
+
export function sameMembers(handed, e0) {
|
|
215
|
+
const mine = Object.keys(e0);
|
|
216
|
+
return Object.keys(handed).length === mine.length && mine.every((k) => Object.hasOwn(handed, k));
|
|
217
|
+
}
|
|
218
|
+
export function ownAnswerViolation(answer, answers) {
|
|
219
|
+
const takes = answers.length === 0 ? "it answers only what its next(e) answered" : `an answer of its own is a record of ${answers.join(", ")} alone, or what its next(e) answered`;
|
|
220
|
+
if (answers.length === 0)
|
|
221
|
+
return `returned an answer of its own — ${takes}`;
|
|
222
|
+
if (!isPlainRecord(answer))
|
|
223
|
+
return `returned an answer of its own that is not a record — ${takes}`;
|
|
224
|
+
const outside = Object.keys(answer).filter((k) => !answers.includes(k)).length;
|
|
225
|
+
return outside > 0 ? `returned an answer of its own with ${outside} member(s) this station does not take — ${takes}` : undefined;
|
|
226
|
+
}
|
|
214
227
|
export function handOnViolation(payload, check, by) {
|
|
215
228
|
if (check === undefined)
|
|
216
229
|
return undefined;
|
|
@@ -8,10 +8,14 @@ export interface ModPackageHost {
|
|
|
8
8
|
readonly provenance?: string;
|
|
9
9
|
/** The values handed to the package's `register(on, options)`. */
|
|
10
10
|
readonly options?: unknown;
|
|
11
|
+
/** The package digest the host pinned (lowercase hex): a package whose bytes give another is refused, `detail.digest
|
|
12
|
+
* { expected, actual }`, before anything of it is imported. A match proves only that the bytes are the pinned ones — it is not
|
|
13
|
+
* admission: which package may run in this process is the host's decision, made before it asks for one. */
|
|
14
|
+
readonly expectDigest?: string;
|
|
11
15
|
}
|
|
12
16
|
/** A member of `plugin.json` or `hooks.json` this reader does not read: the package loaded without it. */
|
|
13
17
|
export interface ModPackageUnreadKey {
|
|
14
|
-
/** The file it stands in, relative to the package (`.claude-plugin/plugin.json` or `hooks/hooks.json`,
|
|
18
|
+
/** The file it stands in, relative to the package (`.claude-plugin/plugin.json` or `hooks/hooks.json`, `/`-separated on every platform). */
|
|
15
19
|
readonly file: string;
|
|
16
20
|
/** The member's name, spelled as a foreign line: one line, the name whole up to 60 characters (a longer one: its first 60
|
|
17
21
|
* and the cut mark; the fence's defusing of a sentinel such as `<<<` may lengthen what is kept). Never its value. */
|
|
@@ -20,13 +24,13 @@ export interface ModPackageUnreadKey {
|
|
|
20
24
|
/** A package read into a registration, with the identity of the bytes it was read from. */
|
|
21
25
|
export interface ModPackageRegistration extends EngineModRegistration {
|
|
22
26
|
/**
|
|
23
|
-
* The sha256 (lowercase hex) of the three files
|
|
27
|
+
* The sha256 (lowercase hex) of the three files the reader read, in this order — `plugin.json`, `hooks.json`, the
|
|
24
28
|
* hooks module — taken as the newline-joined sha256 of each file's bytes. One changed byte in any of the three changes
|
|
25
29
|
* it. The module is imported from those bytes, so this names the module that runs; what its code loads beyond them (an
|
|
26
30
|
* absolute URL, a file it reads) is not among them.
|
|
27
31
|
*/
|
|
28
32
|
readonly digest: string;
|
|
29
|
-
/** The members of the two files
|
|
33
|
+
/** The members of the two files the reader does not read (`plugin.json`'s first, each in its parsed object's key order),
|
|
30
34
|
* each also told as a `mods.package_key_unread` notice. Present iff there is one. */
|
|
31
35
|
readonly unread?: readonly ModPackageUnreadKey[];
|
|
32
36
|
}
|
|
@@ -1,68 +1,60 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
1
|
import { constants } from "node:fs";
|
|
3
2
|
import { open, stat } from "node:fs/promises";
|
|
4
|
-
import {
|
|
5
|
-
import { KERNEL_CONTRACT_VERSION } from "./kernel/index.js";
|
|
3
|
+
import { isAbsolute, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { KERNEL_CONTRACT_VERSION, MOD_PACKAGE_FILES, MODS_INVALID_CODE, readModPackage } from "./kernel/index.js";
|
|
5
|
+
import { kind } from "./kernel/package-reader.js";
|
|
6
6
|
import { modsInvalid } from "./kernel/registration.js";
|
|
7
7
|
import { physicalPathOfAsync } from "../physical-path.js";
|
|
8
8
|
import { describeThrown } from "../tool-policy.js";
|
|
9
9
|
import { foreignLine } from "../protocol-http-auth.js";
|
|
10
10
|
import { deliverEngineNotice } from "../engine-notice.js";
|
|
11
11
|
import { callerKey } from "../caller-text.js";
|
|
12
|
-
|
|
13
|
-
const HOOKS_FILE = join("hooks", "hooks.json");
|
|
14
|
-
const HOST_FACT = "which is the host's to give (the seat, tier, provenance, options and budget are never read from a package)";
|
|
15
|
-
const HOST_FACTS = { tier: HOST_FACT, provenance: HOST_FACT, seat: HOST_FACT, options: HOST_FACT, budgetMs: HOST_FACT };
|
|
16
|
-
const PLUGIN_SHAPE = { file: PLUGIN_FILE, takes: ["name", "version", "description", "author"], refuses: HOST_FACTS };
|
|
17
|
-
const HOOKS_SHAPE = {
|
|
18
|
-
file: HOOKS_FILE,
|
|
19
|
-
takes: ["modules", "contract", "uses", "description"],
|
|
20
|
-
refuses: { ...HOST_FACTS, surface: "which names a surface module: a surface module is mounted by the client that renders it, never by this engine" },
|
|
21
|
-
};
|
|
12
|
+
import { refuseStrayMembers } from "../own-property-receipt.js";
|
|
22
13
|
const KEY_SPELLING_MAX = 60;
|
|
23
14
|
const ROOT_SPELLING_MAX = 1024;
|
|
24
|
-
const
|
|
15
|
+
const MODULE_MEMBER = `${MOD_PACKAGE_FILES.hooks}: modules[0]`;
|
|
25
16
|
const MODULE_URL_HEAD = "data:text/javascript;base64,";
|
|
26
17
|
const MODULE_URL_TEXT = /data:text\/javascript;base64,[A-Za-z0-9+/=]*/g;
|
|
27
18
|
export async function loadModPackage(dir, host = {}) {
|
|
19
|
+
closedHost(dir, host);
|
|
28
20
|
const root = await realRoot(dir);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
throw
|
|
21
|
+
try {
|
|
22
|
+
return await loadFrom(root, host);
|
|
23
|
+
}
|
|
24
|
+
catch (err) {
|
|
25
|
+
if (typeof err === "object" && err !== null && Reflect.get(err, "code") === MODS_INVALID_CODE) {
|
|
26
|
+
const detail = Reflect.get(err, "detail");
|
|
27
|
+
throw Object.assign(modsInvalid(`mod package ${root}: ${err.message}`), detail !== undefined ? { detail } : {});
|
|
28
|
+
}
|
|
29
|
+
throw err;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function loadFrom(root, host) {
|
|
33
|
+
const plugin = await packageFile(root, MOD_PACKAGE_FILES.plugin, MOD_PACKAGE_FILES.plugin);
|
|
34
|
+
const hooks = await packageFile(root, MOD_PACKAGE_FILES.hooks, MOD_PACKAGE_FILES.hooks);
|
|
35
|
+
const expectDigest = host.expectDigest;
|
|
36
|
+
const read = await readModPackage({ plugin, hooks, readModule: (path) => packageFile(root, path, MODULE_MEMBER), side: "engine", ...(expectDigest !== undefined ? { expectDigest } : {}) });
|
|
37
|
+
if (read.contract !== KERNEL_CONTRACT_VERSION) {
|
|
38
|
+
const stated = typeof read.contract === "number" ? read.contract : null;
|
|
39
|
+
throw Object.assign(modsInvalid(`${MOD_PACKAGE_FILES.hooks}: contract is ${kind(read.contract)}; this engine's mods kernel is contract ${KERNEL_CONTRACT_VERSION}, and a package loads only under the contract it was written against`), { detail: Object.freeze({ contract: Object.freeze({ stated, kernel: KERNEL_CONTRACT_VERSION }) }) });
|
|
38
40
|
}
|
|
39
|
-
const modules = hooks["modules"];
|
|
40
|
-
if (!Array.isArray(modules) || modules.length !== 1 || typeof modules[0] !== "string")
|
|
41
|
-
throw refuse(HOOKS_FILE, `modules is ${kind(modules)}; it lists exactly one path, relative to hooks.json`);
|
|
42
|
-
const given = modules[0];
|
|
43
|
-
const shown = foreignLine(given, 120);
|
|
44
|
-
if (given === "" || isAbsolute(given))
|
|
45
|
-
throw refuse(HOOKS_FILE, `modules names "${shown}"; the path is relative to hooks.json`);
|
|
46
|
-
if (extname(given) !== MODULE_EXTENSION)
|
|
47
|
-
throw refuse(HOOKS_FILE, `modules names "${shown}"; a hooks module is an ES module named ${MODULE_EXTENSION}`);
|
|
48
|
-
const moduleFile = await packageFile(root, join("hooks", given), (_file, why) => refuse(HOOKS_FILE, `modules names "${shown}": ${why}`));
|
|
49
|
-
const digest = sha256([pluginFile.bytes, hooksFile.bytes, moduleFile.bytes].map(sha256).join("\n"));
|
|
50
41
|
let ns;
|
|
51
42
|
try {
|
|
52
|
-
ns = await import(`${MODULE_URL_HEAD}${
|
|
43
|
+
ns = await import(`${MODULE_URL_HEAD}${Buffer.from(read.module.buffer, read.module.byteOffset, read.module.byteLength).toString("base64")}`);
|
|
53
44
|
}
|
|
54
45
|
catch (err) {
|
|
55
|
-
const said = describeThrown(err).replace(MODULE_URL_TEXT,
|
|
56
|
-
throw
|
|
46
|
+
const said = describeThrown(err).replace(MODULE_URL_TEXT, MODULE_MEMBER);
|
|
47
|
+
throw modsInvalid(`${MODULE_MEMBER}: could not be imported (${said}); a hooks module is imported from its own bytes, so it is one self-contained file: a relative or package import has nothing to resolve from`);
|
|
57
48
|
}
|
|
49
|
+
const unread = read.unread.map((u) => ({ file: u.file, key: foreignLine(u.name, KEY_SPELLING_MAX), name: u.name }));
|
|
58
50
|
const registration = Object.freeze({
|
|
59
|
-
name:
|
|
60
|
-
uses:
|
|
51
|
+
name: read.name,
|
|
52
|
+
uses: read.uses,
|
|
61
53
|
...(host.tier !== undefined ? { tier: host.tier } : {}),
|
|
62
54
|
...(host.provenance !== undefined ? { provenance: host.provenance } : {}),
|
|
63
55
|
...(host.options !== undefined ? { options: host.options } : {}),
|
|
64
56
|
register: (typeof ns === "object" && ns !== null ? Reflect.get(ns, "register") : undefined),
|
|
65
|
-
digest,
|
|
57
|
+
digest: read.digest,
|
|
66
58
|
...(unread.length > 0 ? { unread: Object.freeze(unread.map((u) => Object.freeze({ file: u.file, key: u.key }))) } : {}),
|
|
67
59
|
});
|
|
68
60
|
const shownRoot = foreignLine(root, ROOT_SPELLING_MAX);
|
|
@@ -75,6 +67,13 @@ export async function loadModPackage(dir, host = {}) {
|
|
|
75
67
|
}
|
|
76
68
|
return registration;
|
|
77
69
|
}
|
|
70
|
+
const HOST_MEMBERS = ["tier", "provenance", "options", "expectDigest"];
|
|
71
|
+
function closedHost(dir, host) {
|
|
72
|
+
if (typeof host !== "object" || host === null || Array.isArray(host)) {
|
|
73
|
+
throw modsInvalid(`mod package ${dir}: the host's argument is ${kind(host)}; it is { ${HOST_MEMBERS.map((k) => `${k}?`).join(", ")} }`);
|
|
74
|
+
}
|
|
75
|
+
refuseStrayMembers(host, { subject: "the host's argument", unit: "member", moment: "at loadModPackage" }, HOST_MEMBERS, (sentence) => modsInvalid(`mod package ${dir}: ${sentence}`));
|
|
76
|
+
}
|
|
78
77
|
async function realRoot(dir) {
|
|
79
78
|
try {
|
|
80
79
|
const root = await physicalPathOfAsync(dir);
|
|
@@ -85,61 +84,29 @@ async function realRoot(dir) {
|
|
|
85
84
|
}
|
|
86
85
|
throw modsInvalid(`mod package ${dir}: not a directory`);
|
|
87
86
|
}
|
|
88
|
-
async function packageFile(root,
|
|
87
|
+
async function packageFile(root, path, said) {
|
|
89
88
|
let real;
|
|
90
89
|
try {
|
|
91
|
-
real = await physicalPathOfAsync(resolve(root,
|
|
90
|
+
real = await physicalPathOfAsync(resolve(root, ...path.split("/")));
|
|
92
91
|
}
|
|
93
92
|
catch {
|
|
94
|
-
throw
|
|
93
|
+
throw modsInvalid(`${said}: is not in the package; a mod package carries it`);
|
|
95
94
|
}
|
|
96
95
|
const inside = relative(root, real);
|
|
97
96
|
if (inside === "" || inside === ".." || inside.startsWith(`..${sep}`) || isAbsolute(inside))
|
|
98
|
-
throw
|
|
97
|
+
throw modsInvalid(`${said}: resolves outside the package`);
|
|
99
98
|
let handle;
|
|
100
99
|
try {
|
|
101
100
|
handle = await open(real, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
102
101
|
if (!(await handle.stat()).isFile())
|
|
103
|
-
throw new Error("not a regular file");
|
|
104
|
-
return
|
|
102
|
+
throw Object.assign(new Error("not a regular file"), { code: "not a regular file" });
|
|
103
|
+
return await handle.readFile();
|
|
105
104
|
}
|
|
106
105
|
catch (err) {
|
|
107
|
-
|
|
106
|
+
const code = typeof err === "object" && err !== null ? Reflect.get(err, "code") : undefined;
|
|
107
|
+
throw modsInvalid(`${said}: could not be read as a regular file of the package (${typeof code === "string" ? code : describeThrown(err)})`);
|
|
108
108
|
}
|
|
109
109
|
finally {
|
|
110
110
|
await handle?.close();
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
|
-
function jsonRecord(bytes, shape, refuse, unread) {
|
|
114
|
-
const { file, takes, refuses } = shape;
|
|
115
|
-
let parsed;
|
|
116
|
-
try {
|
|
117
|
-
parsed = JSON.parse(bytes.toString("utf8"));
|
|
118
|
-
}
|
|
119
|
-
catch {
|
|
120
|
-
throw refuse(file, "is not JSON");
|
|
121
|
-
}
|
|
122
|
-
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
|
|
123
|
-
throw refuse(file, `is ${kind(parsed)}; it is a JSON object`);
|
|
124
|
-
for (const k of Object.keys(parsed)) {
|
|
125
|
-
if (takes.includes(k))
|
|
126
|
-
continue;
|
|
127
|
-
const why = Object.hasOwn(refuses, k) ? refuses[k] : undefined;
|
|
128
|
-
if (why !== undefined)
|
|
129
|
-
throw refuse(file, `carries ${callerKey(k)}, ${why}`);
|
|
130
|
-
unread.push({ file, key: foreignLine(k, KEY_SPELLING_MAX), name: k });
|
|
131
|
-
}
|
|
132
|
-
return parsed;
|
|
133
|
-
}
|
|
134
|
-
function sha256(data) {
|
|
135
|
-
return createHash("sha256").update(data).digest("hex");
|
|
136
|
-
}
|
|
137
|
-
function kind(v) {
|
|
138
|
-
if (v === undefined)
|
|
139
|
-
return "absent";
|
|
140
|
-
if (v === null)
|
|
141
|
-
return "null";
|
|
142
|
-
if (Array.isArray(v))
|
|
143
|
-
return `a list of ${v.length}`;
|
|
144
|
-
return typeof v === "number" || typeof v === "boolean" ? String(v) : `a ${typeof v}`;
|
|
145
|
-
}
|
|
@@ -281,6 +281,9 @@ function orgStateCopyOf(state) {
|
|
|
281
281
|
const snapshot = typeof state === "object" && state !== null ? state.snapshot : undefined;
|
|
282
282
|
return { ...scalarMembersOf(state, ORG_STATE_MEMBERS), snapshot: orgSnapshotCopyOf(snapshot ?? null) };
|
|
283
283
|
}
|
|
284
|
+
function orgReadsRule(rule) {
|
|
285
|
+
return rule.tool === COMMAND_RULE_TOOL && rule.match !== "tool";
|
|
286
|
+
}
|
|
284
287
|
function readOrgRow(row, index) {
|
|
285
288
|
const { rule: text, behavior: said } = orgRowPolicyOf(row);
|
|
286
289
|
const behavior = said === "ask" ? "ask" : "deny";
|
|
@@ -299,9 +302,16 @@ function readOrgRow(row, index) {
|
|
|
299
302
|
if (said !== "deny" && said !== "ask")
|
|
300
303
|
return unreadable("invalid.grammar", said === undefined ? "no behavior" : `behavior "${escapeForDisclosure(said)}" is not deny or ask`);
|
|
301
304
|
const parsed = parseRuleText(text, said);
|
|
302
|
-
if ("reject" in parsed)
|
|
303
|
-
|
|
304
|
-
|
|
305
|
+
if ("reject" in parsed) {
|
|
306
|
+
const respelling = parsed.reject.respelling;
|
|
307
|
+
if (respelling === undefined)
|
|
308
|
+
return unreadable(parsed.reject.code);
|
|
309
|
+
const respelled = parseRuleText(respelling, said);
|
|
310
|
+
if (!("reject" in respelled) && orgReadsRule(respelled.rule))
|
|
311
|
+
return unreadable(parsed.reject.code, undefined, respelling);
|
|
312
|
+
return unreadable(parsed.reject.code, "the organization layer reads only Bash command rules, so no respelling of this row would be read here");
|
|
313
|
+
}
|
|
314
|
+
if (!orgReadsRule(parsed.rule))
|
|
305
315
|
return unreadable("unsupported.tool");
|
|
306
316
|
return { kind: "read", index, rule: text, behavior: said, parsed: parsed.rule };
|
|
307
317
|
}
|
|
@@ -312,7 +312,10 @@ function compile(rules, caps, primaryFieldGeneric, ruleFaces, pathBases = {}) {
|
|
|
312
312
|
}
|
|
313
313
|
if (faceOf(toolName).face?.primaryParams.includes(param) === true && primaryFieldGeneric === "reject") {
|
|
314
314
|
const form = ruleShapeOf({ toolName, args: {} }, verb);
|
|
315
|
-
|
|
315
|
+
const isShell = isShellFaceToolName(toolName);
|
|
316
|
+
bad(text, "invalid.primary_field", (isShell
|
|
317
|
+
? `"${param}" is ${toolName}'s primary field, and a rule on it matches the WHOLE value lexically — no shell or path semantics — so \`${toolName}(${param}:git push*)\` would NOT match \`cd /x && git push\`. `
|
|
318
|
+
: `"${param}" is ${toolName}'s primary field, and a rule on it matches the WHOLE value lexically. `) +
|
|
316
319
|
`Write the rule ${form === undefined ? "" : `as \`${form}\`, or `}on the whole tool (\`${toolName}\`).`, { name: "primaryFieldGeneric", value: "allow-lexical" });
|
|
317
320
|
continue;
|
|
318
321
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** The field path the sentences open with. */
|
|
2
|
+
export declare const RETAIN_SEAT_FIELD = "TaskSpec.retainSubagentSessions";
|
|
3
|
+
/** The option as the engine reads it once it is on: `true` (the defaults) or a frozen record of the members the caller gave. */
|
|
4
|
+
export type RetainSeat = true | {
|
|
5
|
+
readonly ttlMs?: number;
|
|
6
|
+
readonly max?: number;
|
|
7
|
+
};
|
|
8
|
+
/** The screen's answer: the seat (absent ⇔ the option is off), or the refusal — its code, the field path it is about, and the sentence
|
|
9
|
+
* (opening with that path, saying the kind of the value and never the value). Never thrown. */
|
|
10
|
+
export type RetainSeatScreen = {
|
|
11
|
+
readonly ok: true;
|
|
12
|
+
readonly seat?: RetainSeat;
|
|
13
|
+
} | {
|
|
14
|
+
readonly ok: false;
|
|
15
|
+
readonly code: "config.invalid_argument";
|
|
16
|
+
readonly field: string;
|
|
17
|
+
readonly message: string;
|
|
18
|
+
};
|
|
19
|
+
type RetainSeatRefusal = Extract<RetainSeatScreen, {
|
|
20
|
+
ok: false;
|
|
21
|
+
}>;
|
|
22
|
+
/**
|
|
23
|
+
* Judge the retain option. `undefined` and `false` are off (no seat); `true` is on with the defaults; a plain record is on, and its
|
|
24
|
+
* members are the only keys `ttlMs` and `max` — each present member (not `undefined`) a whole number of at least 1 — and the seat is a
|
|
25
|
+
* frozen copy holding exactly the members given. Refused, each `config.invalid_argument` with its field path: `null`, a string, a number
|
|
26
|
+
* or an array as the option; a key the option does not take; a record that is not plain (a class instance, a Proxy); a member whose
|
|
27
|
+
* read throws (the sentence says it could not be read, never what it threw); and a member that is `null`, not a number, not finite, a
|
|
28
|
+
* fraction or below 1. Each member is read once, off the record's captured descriptors, and the answer is a copy of what was read, so the
|
|
29
|
+
* caller's record is never read again. `at` is the field path of the option for the sentences.
|
|
30
|
+
*/
|
|
31
|
+
export declare function screenRetainSeat(value: unknown, at?: string): RetainSeatScreen;
|
|
32
|
+
/** The refusal as the error a door throws: the code, and `detail.field` naming the field. */
|
|
33
|
+
export declare function retainSeatError(refusal: RetainSeatRefusal): Error & {
|
|
34
|
+
code: "config.invalid_argument";
|
|
35
|
+
detail: {
|
|
36
|
+
field: string;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
/** The screen as a ledger's constructor takes it: the seat to build from (`true` or the frozen record), or the refusal thrown. A value that
|
|
40
|
+
* asks for no retention (`false`, absent) is refused too — a ledger is made only for an opt-in. */
|
|
41
|
+
export declare function retainSeatToBuild(value: unknown): RetainSeat;
|
|
42
|
+
export {};
|