@nexum-hub/agent 0.0.0-stage → 0.0.2
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/README.md +133 -2
- package/contracts/v2/common.schema.json +262 -0
- package/contracts/v2/fixtures/inputs.json +244 -0
- package/contracts/v2/fixtures/wire.json +6493 -0
- package/contracts/v2/model/agent.close.input.schema.json +14 -0
- package/contracts/v2/model/agent.close.output.schema.json +101 -0
- package/contracts/v2/model/agent.list.input.schema.json +57 -0
- package/contracts/v2/model/agent.list.output.schema.json +220 -0
- package/contracts/v2/model/agent.open.input.schema.json +35 -0
- package/contracts/v2/model/agent.open.output.schema.json +41 -0
- package/contracts/v2/model/agent.run.cancel.input.schema.json +18 -0
- package/contracts/v2/model/agent.run.cancel.output.schema.json +100 -0
- package/contracts/v2/model/agent.run.get.input.schema.json +41 -0
- package/contracts/v2/model/agent.run.get.output.schema.json +100 -0
- package/contracts/v2/model/agent.run.input.input.schema.json +30 -0
- package/contracts/v2/model/agent.run.input.output.schema.json +100 -0
- package/contracts/v2/model/agent.run.list.input.schema.json +47 -0
- package/contracts/v2/model/agent.run.list.output.schema.json +41 -0
- package/contracts/v2/model/agent.run.start.input.schema.json +73 -0
- package/contracts/v2/model/agent.run.start.output.schema.json +100 -0
- package/contracts/v2/wire/bodies.schema.json +383 -0
- package/contracts/v2/wire/envelope.schema.json +2280 -0
- package/contracts/v2/wire/events.schema.json +242 -0
- package/contracts/v2/wire/maintenance.schema.json +308 -0
- package/contracts/v2/wire/operations.schema.json +1791 -0
- package/contracts/v2/wire/stream.schema.json +74 -0
- package/contracts/v2/wire/types.schema.json +2616 -0
- package/dist/adapters/mcp-http.d.ts +22 -0
- package/dist/adapters/mcp-http.js +472 -0
- package/dist/adapters/mcp-http.js.map +1 -0
- package/dist/adapters/mcp-oauth.d.ts +29 -0
- package/dist/adapters/mcp-oauth.js +653 -0
- package/dist/adapters/mcp-oauth.js.map +1 -0
- package/dist/adapters/mcp-wire.d.ts +21 -0
- package/dist/adapters/mcp-wire.js +307 -0
- package/dist/adapters/mcp-wire.js.map +1 -0
- package/dist/adapters/mcp.d.ts +56 -0
- package/dist/adapters/mcp.js +1989 -0
- package/dist/adapters/mcp.js.map +1 -0
- package/dist/adapters/openapi-http.d.ts +29 -0
- package/dist/adapters/openapi-http.js +504 -0
- package/dist/adapters/openapi-http.js.map +1 -0
- package/dist/adapters/openapi-request.d.ts +26 -0
- package/dist/adapters/openapi-request.js +597 -0
- package/dist/adapters/openapi-request.js.map +1 -0
- package/dist/adapters/openapi-schema.d.ts +22 -0
- package/dist/adapters/openapi-schema.js +471 -0
- package/dist/adapters/openapi-schema.js.map +1 -0
- package/dist/adapters/openapi-typegen.d.ts +11 -0
- package/dist/adapters/openapi-typegen.js +185 -0
- package/dist/adapters/openapi-typegen.js.map +1 -0
- package/dist/adapters/openapi-types.d.ts +99 -0
- package/dist/adapters/openapi-types.js +2 -0
- package/dist/adapters/openapi-types.js.map +1 -0
- package/dist/adapters/openapi.d.ts +7 -0
- package/dist/adapters/openapi.js +768 -0
- package/dist/adapters/openapi.js.map +1 -0
- package/dist/application/activation.d.ts +28 -0
- package/dist/application/activation.js +289 -0
- package/dist/application/activation.js.map +1 -0
- package/dist/application/attachments.d.ts +24 -0
- package/dist/application/attachments.js +87 -0
- package/dist/application/attachments.js.map +1 -0
- package/dist/application/authority.d.ts +32 -0
- package/dist/application/authority.js +213 -0
- package/dist/application/authority.js.map +1 -0
- package/dist/application/catalog.d.ts +11 -0
- package/dist/application/catalog.js +177 -0
- package/dist/application/catalog.js.map +1 -0
- package/dist/application/config.d.ts +24 -0
- package/dist/application/config.js +140 -0
- package/dist/application/config.js.map +1 -0
- package/dist/application/connection.d.ts +35 -0
- package/dist/application/connection.js +465 -0
- package/dist/application/connection.js.map +1 -0
- package/dist/application/data-plane.d.ts +35 -0
- package/dist/application/data-plane.js +437 -0
- package/dist/application/data-plane.js.map +1 -0
- package/dist/application/diagnostics.d.ts +4 -0
- package/dist/application/diagnostics.js +65 -0
- package/dist/application/diagnostics.js.map +1 -0
- package/dist/application/dispatcher.d.ts +4 -0
- package/dist/application/dispatcher.js +16 -0
- package/dist/application/dispatcher.js.map +1 -0
- package/dist/application/main.d.ts +12 -0
- package/dist/application/main.js +167 -0
- package/dist/application/main.js.map +1 -0
- package/dist/application/maintenance.d.ts +27 -0
- package/dist/application/maintenance.js +244 -0
- package/dist/application/maintenance.js.map +1 -0
- package/dist/application/packages.d.ts +25 -0
- package/dist/application/packages.js +208 -0
- package/dist/application/packages.js.map +1 -0
- package/dist/application/processes.d.ts +49 -0
- package/dist/application/processes.js +180 -0
- package/dist/application/processes.js.map +1 -0
- package/dist/application/run-view.d.ts +17 -0
- package/dist/application/run-view.js +286 -0
- package/dist/application/run-view.js.map +1 -0
- package/dist/application/runtime.d.ts +78 -0
- package/dist/application/runtime.js +1747 -0
- package/dist/application/runtime.js.map +1 -0
- package/dist/application/tasks.d.ts +38 -0
- package/dist/application/tasks.js +23 -0
- package/dist/application/tasks.js.map +1 -0
- package/dist/application/user-state.d.ts +20 -0
- package/dist/application/user-state.js +52 -0
- package/dist/application/user-state.js.map +1 -0
- package/dist/application/work-git.d.ts +14 -0
- package/dist/application/work-git.js +67 -0
- package/dist/application/work-git.js.map +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +40 -0
- package/dist/cli.js.map +1 -0
- package/dist/contracts/caller.d.ts +51 -0
- package/dist/contracts/caller.js +95 -0
- package/dist/contracts/caller.js.map +1 -0
- package/dist/contracts/error-data.d.ts +6 -0
- package/dist/contracts/error-data.js +51 -0
- package/dist/contracts/error-data.js.map +1 -0
- package/dist/contracts/errors.d.ts +10 -0
- package/dist/contracts/errors.js +28 -0
- package/dist/contracts/errors.js.map +1 -0
- package/dist/contracts/json.d.ts +9 -0
- package/dist/contracts/json.js +104 -0
- package/dist/contracts/json.js.map +1 -0
- package/dist/contracts/types.d.ts +122 -0
- package/dist/contracts/types.js +19 -0
- package/dist/contracts/types.js.map +1 -0
- package/dist/contracts/validation.d.ts +4 -0
- package/dist/contracts/validation.js +157 -0
- package/dist/contracts/validation.js.map +1 -0
- package/dist/contracts/wire-validation.d.ts +9 -0
- package/dist/contracts/wire-validation.js +399 -0
- package/dist/contracts/wire-validation.js.map +1 -0
- package/dist/contracts/wire.d.ts +1083 -0
- package/dist/contracts/wire.js +62 -0
- package/dist/contracts/wire.js.map +1 -0
- package/dist/extension-api.d.ts +12 -0
- package/dist/extension-api.js +2 -0
- package/dist/extension-api.js.map +1 -0
- package/dist/extensions/api.d.ts +72 -0
- package/dist/extensions/api.js +2 -0
- package/dist/extensions/api.js.map +1 -0
- package/dist/extensions/host-requests.d.ts +23 -0
- package/dist/extensions/host-requests.js +60 -0
- package/dist/extensions/host-requests.js.map +1 -0
- package/dist/extensions/materials.d.ts +37 -0
- package/dist/extensions/materials.js +691 -0
- package/dist/extensions/materials.js.map +1 -0
- package/dist/extensions/package-loader.d.ts +28 -0
- package/dist/extensions/package-loader.js +177 -0
- package/dist/extensions/package-loader.js.map +1 -0
- package/dist/extensions/packages.d.ts +73 -0
- package/dist/extensions/packages.js +441 -0
- package/dist/extensions/packages.js.map +1 -0
- package/dist/extensions/registry.d.ts +92 -0
- package/dist/extensions/registry.js +1540 -0
- package/dist/extensions/registry.js.map +1 -0
- package/dist/extensions/resources.d.ts +105 -0
- package/dist/extensions/resources.js +595 -0
- package/dist/extensions/resources.js.map +1 -0
- package/dist/extensions/state.d.ts +25 -0
- package/dist/extensions/state.js +10 -0
- package/dist/extensions/state.js.map +1 -0
- package/dist/extensions/transfer.d.ts +81 -0
- package/dist/extensions/transfer.js +16 -0
- package/dist/extensions/transfer.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/maintenance/configuration.d.ts +79 -0
- package/dist/maintenance/configuration.js +1503 -0
- package/dist/maintenance/configuration.js.map +1 -0
- package/dist/maintenance/credentials.d.ts +23 -0
- package/dist/maintenance/credentials.js +177 -0
- package/dist/maintenance/credentials.js.map +1 -0
- package/dist/maintenance/files.d.ts +2 -0
- package/dist/maintenance/files.js +40 -0
- package/dist/maintenance/files.js.map +1 -0
- package/dist/maintenance/npm.d.ts +71 -0
- package/dist/maintenance/npm.js +1013 -0
- package/dist/maintenance/npm.js.map +1 -0
- package/dist/maintenance/oauth.d.ts +45 -0
- package/dist/maintenance/oauth.js +686 -0
- package/dist/maintenance/oauth.js.map +1 -0
- package/dist/maintenance/requests.d.ts +347 -0
- package/dist/maintenance/requests.js +959 -0
- package/dist/maintenance/requests.js.map +1 -0
- package/dist/maintenance/work-git.d.ts +29 -0
- package/dist/maintenance/work-git.js +776 -0
- package/dist/maintenance/work-git.js.map +1 -0
- package/dist/process/backend.d.ts +54 -0
- package/dist/process/backend.js +39 -0
- package/dist/process/backend.js.map +1 -0
- package/dist/process/environment-windows.d.ts +15 -0
- package/dist/process/environment-windows.js +151 -0
- package/dist/process/environment-windows.js.map +1 -0
- package/dist/process/environment.d.ts +28 -0
- package/dist/process/environment.js +410 -0
- package/dist/process/environment.js.map +1 -0
- package/dist/process/launch.d.ts +31 -0
- package/dist/process/launch.js +281 -0
- package/dist/process/launch.js.map +1 -0
- package/dist/process/managed.d.ts +36 -0
- package/dist/process/managed.js +459 -0
- package/dist/process/managed.js.map +1 -0
- package/dist/process/output-log.d.ts +50 -0
- package/dist/process/output-log.js +552 -0
- package/dist/process/output-log.js.map +1 -0
- package/dist/process/pipes.d.ts +16 -0
- package/dist/process/pipes.js +588 -0
- package/dist/process/pipes.js.map +1 -0
- package/dist/process/pty.d.ts +22 -0
- package/dist/process/pty.js +512 -0
- package/dist/process/pty.js.map +1 -0
- package/dist/process/windows-conout-worker.d.ts +1 -0
- package/dist/process/windows-conout-worker.js +90 -0
- package/dist/process/windows-conout-worker.js.map +1 -0
- package/dist/process/windows-conout.d.ts +46 -0
- package/dist/process/windows-conout.js +140 -0
- package/dist/process/windows-conout.js.map +1 -0
- package/dist/process/windows-helper.d.ts +43 -0
- package/dist/process/windows-helper.js +255 -0
- package/dist/process/windows-helper.js.map +1 -0
- package/dist/process/windows-job.d.ts +39 -0
- package/dist/process/windows-job.js +224 -0
- package/dist/process/windows-job.js.map +1 -0
- package/dist/process/windows-pipes.d.ts +16 -0
- package/dist/process/windows-pipes.js +475 -0
- package/dist/process/windows-pipes.js.map +1 -0
- package/dist/results/artifacts.d.ts +118 -0
- package/dist/results/artifacts.js +873 -0
- package/dist/results/artifacts.js.map +1 -0
- package/dist/results/encoder.d.ts +57 -0
- package/dist/results/encoder.js +1179 -0
- package/dist/results/encoder.js.map +1 -0
- package/dist/results/mcp.d.ts +95 -0
- package/dist/results/mcp.js +656 -0
- package/dist/results/mcp.js.map +1 -0
- package/dist/results/media.d.ts +19 -0
- package/dist/results/media.js +301 -0
- package/dist/results/media.js.map +1 -0
- package/dist/results/normalize.d.ts +29 -0
- package/dist/results/normalize.js +464 -0
- package/dist/results/normalize.js.map +1 -0
- package/dist/runtime/control-scripts.d.ts +23 -0
- package/dist/runtime/control-scripts.js +242 -0
- package/dist/runtime/control-scripts.js.map +1 -0
- package/dist/runtime/engine.d.ts +177 -0
- package/dist/runtime/engine.js +1159 -0
- package/dist/runtime/engine.js.map +1 -0
- package/dist/runtime/globals.d.ts +1 -0
- package/dist/runtime/globals.js +31 -0
- package/dist/runtime/globals.js.map +1 -0
- package/dist/runtime/material-retention.d.ts +34 -0
- package/dist/runtime/material-retention.js +671 -0
- package/dist/runtime/material-retention.js.map +1 -0
- package/dist/runtime/modules.d.ts +50 -0
- package/dist/runtime/modules.js +320 -0
- package/dist/runtime/modules.js.map +1 -0
- package/dist/runtime/names.d.ts +2 -0
- package/dist/runtime/names.js +22 -0
- package/dist/runtime/names.js.map +1 -0
- package/dist/runtime/operations.d.ts +20 -0
- package/dist/runtime/operations.js +137 -0
- package/dist/runtime/operations.js.map +1 -0
- package/dist/runtime/paths.d.ts +3 -0
- package/dist/runtime/paths.js +38 -0
- package/dist/runtime/paths.js.map +1 -0
- package/dist/runtime/promises.d.ts +9 -0
- package/dist/runtime/promises.js +78 -0
- package/dist/runtime/promises.js.map +1 -0
- package/dist/runtime/retention.d.ts +88 -0
- package/dist/runtime/retention.js +735 -0
- package/dist/runtime/retention.js.map +1 -0
- package/dist/runtime/run-log.d.ts +30 -0
- package/dist/runtime/run-log.js +207 -0
- package/dist/runtime/run-log.js.map +1 -0
- package/dist/runtime/scripts.d.ts +84 -0
- package/dist/runtime/scripts.js +387 -0
- package/dist/runtime/scripts.js.map +1 -0
- package/dist/runtime/tasks.d.ts +54 -0
- package/dist/runtime/tasks.js +290 -0
- package/dist/runtime/tasks.js.map +1 -0
- package/dist/sdk/maintenance.d.ts +3 -0
- package/dist/sdk/maintenance.js +80 -0
- package/dist/sdk/maintenance.js.map +1 -0
- package/dist/sdk/output-extension.d.ts +3 -0
- package/dist/sdk/output-extension.js +44 -0
- package/dist/sdk/output-extension.js.map +1 -0
- package/dist/sdk/output.d.ts +52 -0
- package/dist/sdk/output.js +350 -0
- package/dist/sdk/output.js.map +1 -0
- package/dist/sdk/runtime.d.ts +3 -0
- package/dist/sdk/runtime.js +26 -0
- package/dist/sdk/runtime.js.map +1 -0
- package/dist/sdk/sh.d.ts +3 -0
- package/dist/sdk/sh.js +384 -0
- package/dist/sdk/sh.js.map +1 -0
- package/dist/sdk/state.d.ts +2 -0
- package/dist/sdk/state.js +69 -0
- package/dist/sdk/state.js.map +1 -0
- package/dist/sdk/transfer.d.ts +3 -0
- package/dist/sdk/transfer.js +141 -0
- package/dist/sdk/transfer.js.map +1 -0
- package/dist/services/extensions.d.ts +19 -0
- package/dist/services/extensions.js +183 -0
- package/dist/services/extensions.js.map +1 -0
- package/dist/services/state.d.ts +37 -0
- package/dist/services/state.js +125 -0
- package/dist/services/state.js.map +1 -0
- package/dist/services/transfer.d.ts +86 -0
- package/dist/services/transfer.js +948 -0
- package/dist/services/transfer.js.map +1 -0
- package/dist/storage/schema.d.ts +8 -0
- package/dist/storage/schema.js +241 -0
- package/dist/storage/schema.js.map +1 -0
- package/dist/storage/sqlite.d.ts +241 -0
- package/dist/storage/sqlite.js +1855 -0
- package/dist/storage/sqlite.js.map +1 -0
- package/dist/storage/transfers.d.ts +90 -0
- package/dist/storage/transfers.js +2 -0
- package/dist/storage/transfers.js.map +1 -0
- package/dist/transfer/io.d.ts +74 -0
- package/dist/transfer/io.js +425 -0
- package/dist/transfer/io.js.map +1 -0
- package/dist/transfer/offers.d.ts +72 -0
- package/dist/transfer/offers.js +380 -0
- package/dist/transfer/offers.js.map +1 -0
- package/dist/transfer/stream-slots.d.ts +12 -0
- package/dist/transfer/stream-slots.js +28 -0
- package/dist/transfer/stream-slots.js.map +1 -0
- package/dist/work/catalog.d.ts +43 -0
- package/dist/work/catalog.js +198 -0
- package/dist/work/catalog.js.map +1 -0
- package/dist/work/discovery.d.ts +70 -0
- package/dist/work/discovery.js +465 -0
- package/dist/work/discovery.js.map +1 -0
- package/dist/work/service.d.ts +83 -0
- package/dist/work/service.js +703 -0
- package/dist/work/service.js.map +1 -0
- package/package.json +66 -4
|
@@ -0,0 +1,691 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { rmSync } from "node:fs";
|
|
3
|
+
import { lstat, mkdir, mkdtemp, opendir, realpath, rename, writeFile, } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { AgentError, requireCondition } from "../contracts/errors.js";
|
|
7
|
+
import { canonicalJson } from "../contracts/json.js";
|
|
8
|
+
import { extensionNamespace } from "../runtime/names.js";
|
|
9
|
+
import { contains, privateDirectory } from "../runtime/paths.js";
|
|
10
|
+
import { boundedFile } from "../work/discovery.js";
|
|
11
|
+
const digest = (value) => createHash("sha256").update(value).digest("hex");
|
|
12
|
+
const compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
13
|
+
const portable = (value) => value.normalize("NFC").toLowerCase();
|
|
14
|
+
const segment = (value) => Buffer.byteLength(value) > 120
|
|
15
|
+
? "h-" + digest(value)
|
|
16
|
+
: /^[A-Za-z0-9_.-]+$/.test(value) &&
|
|
17
|
+
value !== "." &&
|
|
18
|
+
value !== ".." &&
|
|
19
|
+
!/^(con|prn|aux|nul|com[0-9]|lpt[0-9])(?:\.|$)/i.test(value)
|
|
20
|
+
? value
|
|
21
|
+
: "x-" + Buffer.from(value).toString("hex");
|
|
22
|
+
const relative = (root, filename) => path.relative(root, filename).split(path.sep).join("/");
|
|
23
|
+
const label = (value) => value.replace(/[\r\n\[\]`]/g, " ");
|
|
24
|
+
function freeze(value) {
|
|
25
|
+
if (value && typeof value === "object") {
|
|
26
|
+
for (const child of Object.values(value))
|
|
27
|
+
freeze(child);
|
|
28
|
+
Object.freeze(value);
|
|
29
|
+
}
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
function text(bytes) {
|
|
33
|
+
try {
|
|
34
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
35
|
+
}
|
|
36
|
+
catch (cause) {
|
|
37
|
+
throw new AgentError("extension_material_encoding", "Extension Markdown must be valid UTF-8.", undefined, { cause });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function sourceSuffix(directory, filename) {
|
|
41
|
+
const result = relative(directory, filename);
|
|
42
|
+
requireCondition(result.length > 0 &&
|
|
43
|
+
!result
|
|
44
|
+
.split("/")
|
|
45
|
+
.some((part) => part === "." ||
|
|
46
|
+
part === ".." ||
|
|
47
|
+
part.includes("\\") ||
|
|
48
|
+
part.includes("\0")) &&
|
|
49
|
+
!path.isAbsolute(result), "extension_path_escape", "An extension reading file must retain its declared relative subtree.");
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
/** Core-owned preparation and synchronous discovery index; it never enables a package. */
|
|
53
|
+
export class ExtensionMaterials {
|
|
54
|
+
work;
|
|
55
|
+
#maxFiles;
|
|
56
|
+
#maxBytes;
|
|
57
|
+
#prepared = new WeakSet();
|
|
58
|
+
#active = new Map();
|
|
59
|
+
#published = freeze({
|
|
60
|
+
instructions: [],
|
|
61
|
+
skills: [],
|
|
62
|
+
docs: [],
|
|
63
|
+
});
|
|
64
|
+
#sources = Object.freeze([]);
|
|
65
|
+
#reserve;
|
|
66
|
+
#retention;
|
|
67
|
+
#holds = new Map();
|
|
68
|
+
#directories = new Map();
|
|
69
|
+
#stages = new Set();
|
|
70
|
+
constructor(work, limits = {}) {
|
|
71
|
+
// No directory creation or package evaluation during construction.
|
|
72
|
+
this.work = work;
|
|
73
|
+
this.#maxFiles = limits.maxFiles ?? 10000;
|
|
74
|
+
this.#maxBytes = limits.maxBytes ?? 256 * 1024 * 1024;
|
|
75
|
+
this.#reserve = limits.reserve;
|
|
76
|
+
this.#retention = limits.materialRetention;
|
|
77
|
+
requireCondition(path.isAbsolute(work) &&
|
|
78
|
+
[this.#maxFiles, this.#maxBytes].every((n) => Number.isSafeInteger(n) && n > 0), "invalid_extension_limits", "Materials need an absolute work path and positive bounded limits.");
|
|
79
|
+
}
|
|
80
|
+
async prepare(descriptor, handle, signal) {
|
|
81
|
+
signal?.throwIfAborted();
|
|
82
|
+
const origin = descriptor.source.origin;
|
|
83
|
+
if (origin.type === "platform")
|
|
84
|
+
requireCondition([
|
|
85
|
+
"nexum",
|
|
86
|
+
"sh",
|
|
87
|
+
"transfer",
|
|
88
|
+
"output",
|
|
89
|
+
"runtime",
|
|
90
|
+
"state",
|
|
91
|
+
"maintenance",
|
|
92
|
+
"mcp",
|
|
93
|
+
"api",
|
|
94
|
+
].includes(origin.namespace), "invalid_namespace", "Core platform materials must use their registered platform namespace.");
|
|
95
|
+
else
|
|
96
|
+
extensionNamespace(origin.namespace);
|
|
97
|
+
requireCondition(descriptor.work === this.work &&
|
|
98
|
+
(await realpath(this.work)) === this.work &&
|
|
99
|
+
["extension", "platform"].includes(origin.type) &&
|
|
100
|
+
origin.owner === descriptor.packageName &&
|
|
101
|
+
origin.version === descriptor.version &&
|
|
102
|
+
origin.revision === descriptor.revision &&
|
|
103
|
+
canonicalJson(origin) === canonicalJson(handle.origin), "extension_material_source_mismatch", "Reading materials must bind the verified source and prepared Registry handle from this profile.");
|
|
104
|
+
requireCondition((await realpath(descriptor.root)) === descriptor.root, "extension_path_escape", "A material source root must retain its canonical identity.");
|
|
105
|
+
if (origin.type === "extension") {
|
|
106
|
+
const modules = await realpath(path.join(this.work, "node_modules"));
|
|
107
|
+
requireCondition(contains(this.work, modules) &&
|
|
108
|
+
contains(modules, descriptor.root) &&
|
|
109
|
+
(await realpath(path.join(modules, descriptor.packageName))) ===
|
|
110
|
+
descriptor.root, "extension_path_escape", "Extension material sources must remain in their installed package.");
|
|
111
|
+
}
|
|
112
|
+
const methods = canonicalJson(handle.methods, 4 * 1024 * 1024);
|
|
113
|
+
const snapshotRevision = digest(canonicalJson({ origin, methods }, 8 * 1024 * 1024));
|
|
114
|
+
const parent = privateDirectory(this.work, "docs", "extensions", segment(origin.namespace), segment(descriptor.version));
|
|
115
|
+
const directory = path.join(parent, snapshotRevision);
|
|
116
|
+
const plan = [];
|
|
117
|
+
const mappings = new Map();
|
|
118
|
+
const targets = new Set();
|
|
119
|
+
let sourceBytes = 0;
|
|
120
|
+
const add = (source, target, boundary) => {
|
|
121
|
+
requireCondition(contains(descriptor.root, source.path) &&
|
|
122
|
+
contains(descriptor.root, boundary) &&
|
|
123
|
+
contains(boundary, source.path) &&
|
|
124
|
+
source.size >= 0 &&
|
|
125
|
+
Number.isSafeInteger(source.size) &&
|
|
126
|
+
/^sha256:[0-9a-f]{64}$/.test(source.revision), "extension_material_source_mismatch", "A reading file must belong to its exact declared package subtree.");
|
|
127
|
+
requireCondition(!path.posix.isAbsolute(target) &&
|
|
128
|
+
path.posix.normalize(target) === target &&
|
|
129
|
+
!target
|
|
130
|
+
.split("/")
|
|
131
|
+
.some((part) => !part ||
|
|
132
|
+
part === "." ||
|
|
133
|
+
part === ".." ||
|
|
134
|
+
part.includes("\\") ||
|
|
135
|
+
part.includes("\0")), "extension_path_escape", "A reading target must be a literal relative path within its staging tree.");
|
|
136
|
+
const key = portable(target);
|
|
137
|
+
requireCondition(!targets.has(key), "extension_material_conflict", "Two reading files cannot share a portable path.");
|
|
138
|
+
targets.add(key);
|
|
139
|
+
sourceBytes += source.size;
|
|
140
|
+
requireCondition(plan.length + 1 <= this.#maxFiles && sourceBytes <= this.#maxBytes, "extension_material_too_large", "The extension reading snapshot exceeds its file or byte budget.");
|
|
141
|
+
plan.push({ source, target, boundary });
|
|
142
|
+
// The same declared source may appear in more than one contribution. Prefer its first stable reading location.
|
|
143
|
+
const lexical = path.resolve(descriptor.root, source.relativePath);
|
|
144
|
+
if (!mappings.has(lexical))
|
|
145
|
+
mappings.set(lexical, target);
|
|
146
|
+
if (!mappings.has(source.path))
|
|
147
|
+
mappings.set(source.path, target);
|
|
148
|
+
};
|
|
149
|
+
const docs = descriptor.materials.docs;
|
|
150
|
+
mappings.set(path.resolve(descriptor.root, docs.relativePath), "manual");
|
|
151
|
+
mappings.set(docs.path, "manual");
|
|
152
|
+
for (const file of docs.files)
|
|
153
|
+
add(file, "manual/" + sourceSuffix(docs.relativePath || ".", file.relativePath), docs.path);
|
|
154
|
+
if (descriptor.materials.instructions) {
|
|
155
|
+
const file = descriptor.materials.instructions;
|
|
156
|
+
add(file, "instructions/" + path.posix.basename(file.relativePath), path.dirname(file.path));
|
|
157
|
+
}
|
|
158
|
+
for (const skill of descriptor.materials.skills?.entries ?? []) {
|
|
159
|
+
const prefix = "skills/" + skill.metadata.name;
|
|
160
|
+
mappings.set(path.resolve(descriptor.root, skill.directory.relativePath), prefix);
|
|
161
|
+
mappings.set(skill.directory.path, prefix);
|
|
162
|
+
for (const file of skill.directory.files)
|
|
163
|
+
add(file, prefix +
|
|
164
|
+
"/" +
|
|
165
|
+
sourceSuffix(skill.directory.relativePath, file.relativePath), skill.directory.path);
|
|
166
|
+
}
|
|
167
|
+
const scripts = descriptor.materials.scripts;
|
|
168
|
+
for (const item of scripts?.entries ?? [])
|
|
169
|
+
add(item.file, "reference/scripts/" + item.metadata.scriptName, scripts.directory);
|
|
170
|
+
for (const item of plan) {
|
|
171
|
+
// Directory links inside the author's tree remain usable too.
|
|
172
|
+
let source = path.dirname(path.resolve(descriptor.root, item.source.relativePath));
|
|
173
|
+
let target = path.posix.dirname(item.target);
|
|
174
|
+
while (contains(descriptor.root, source) && target !== ".") {
|
|
175
|
+
if (!mappings.has(source))
|
|
176
|
+
mappings.set(source, target);
|
|
177
|
+
source = path.dirname(source);
|
|
178
|
+
target = path.posix.dirname(target);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const diagnostics = [];
|
|
182
|
+
const rewrite = (body, item, budget) => {
|
|
183
|
+
let upperBytes = Buffer.byteLength(body);
|
|
184
|
+
requireCondition(upperBytes <= budget, "extension_material_too_large", "An entry or manual exceeds its reading budget.");
|
|
185
|
+
const translate = (href) => {
|
|
186
|
+
if (!href ||
|
|
187
|
+
href.startsWith("#") ||
|
|
188
|
+
(/^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(href) && !/^file:/i.test(href)))
|
|
189
|
+
return href;
|
|
190
|
+
const at = href.search(/[?#]/), raw = at < 0 ? href : href.slice(0, at), tail = at < 0 ? "" : href.slice(at);
|
|
191
|
+
let decoded;
|
|
192
|
+
try {
|
|
193
|
+
decoded = /^file:/i.test(raw)
|
|
194
|
+
? fileURLToPath(raw)
|
|
195
|
+
: decodeURIComponent(raw);
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
decoded = "\0";
|
|
199
|
+
}
|
|
200
|
+
const resolved = path.resolve(descriptor.root, path.posix.dirname(item.source.relativePath), decoded);
|
|
201
|
+
const mapped = mappings.get(resolved);
|
|
202
|
+
if (mapped) {
|
|
203
|
+
if (/^file:/.test(mapped))
|
|
204
|
+
return mapped + tail;
|
|
205
|
+
const result = path.posix.relative(path.posix.dirname(item.target), mapped) || ".";
|
|
206
|
+
return (result
|
|
207
|
+
.split("/")
|
|
208
|
+
.map((part) => encodeURIComponent(part))
|
|
209
|
+
.join("/") + tail);
|
|
210
|
+
}
|
|
211
|
+
requireCondition(diagnostics.length < 1000, "extension_material_too_large", "The manual has too many unresolved local links.");
|
|
212
|
+
diagnostics.push({
|
|
213
|
+
code: "extension_material_link_unavailable",
|
|
214
|
+
source: item.source.relativePath,
|
|
215
|
+
target: href.slice(0, 8192),
|
|
216
|
+
});
|
|
217
|
+
return href;
|
|
218
|
+
};
|
|
219
|
+
const replacement = (original, prefix, href, suffix, angled) => {
|
|
220
|
+
const converted = translate(href);
|
|
221
|
+
if (converted === href)
|
|
222
|
+
return original;
|
|
223
|
+
upperBytes +=
|
|
224
|
+
Buffer.byteLength(prefix) +
|
|
225
|
+
Buffer.byteLength(converted) +
|
|
226
|
+
Buffer.byteLength(suffix) +
|
|
227
|
+
(angled ? 2 : 0) -
|
|
228
|
+
Buffer.byteLength(original);
|
|
229
|
+
requireCondition(upperBytes <= budget, "extension_material_too_large", "Rewritten reading links exceed the complete entry budget.");
|
|
230
|
+
return (prefix +
|
|
231
|
+
(angled ? "<" : "") +
|
|
232
|
+
converted +
|
|
233
|
+
(angled ? ">" : "") +
|
|
234
|
+
suffix);
|
|
235
|
+
};
|
|
236
|
+
// A forward scanner avoids quadratic regex retries on a long run of unmatched '['.
|
|
237
|
+
const inlineLinks = (line) => {
|
|
238
|
+
const parts = [];
|
|
239
|
+
let opening = -1, written = 0;
|
|
240
|
+
for (let at = 0; at < line.length; at++) {
|
|
241
|
+
if (line[at] === "\\") {
|
|
242
|
+
at++;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (line[at] === "[")
|
|
246
|
+
opening = at;
|
|
247
|
+
if (opening < 0 || line[at] !== "]" || line[at + 1] !== "(")
|
|
248
|
+
continue;
|
|
249
|
+
const end = line.indexOf(")", at + 2);
|
|
250
|
+
if (end < 0)
|
|
251
|
+
break;
|
|
252
|
+
let start = at + 2;
|
|
253
|
+
while (start < end && /\s/.test(line[start]))
|
|
254
|
+
start++;
|
|
255
|
+
const angled = line[start] === "<";
|
|
256
|
+
let finish = start;
|
|
257
|
+
if (angled)
|
|
258
|
+
finish = line.indexOf(">", start + 1);
|
|
259
|
+
else
|
|
260
|
+
while (finish < end && !/\s/.test(line[finish]))
|
|
261
|
+
finish++;
|
|
262
|
+
const suffixStart = angled ? finish + 1 : finish;
|
|
263
|
+
if (finish >= start &&
|
|
264
|
+
finish <= end &&
|
|
265
|
+
/^\s*(?:(?:"[^"]*"|'[^']*')\s*)?$/.test(line.slice(suffixStart, end))) {
|
|
266
|
+
parts.push(line.slice(written, opening), replacement(line.slice(opening, end + 1), line.slice(opening, start), line.slice(angled ? start + 1 : start, finish), line.slice(suffixStart, end + 1), true));
|
|
267
|
+
written = end + 1;
|
|
268
|
+
}
|
|
269
|
+
at = end;
|
|
270
|
+
opening = -1;
|
|
271
|
+
}
|
|
272
|
+
parts.push(line.slice(written));
|
|
273
|
+
return parts.join("");
|
|
274
|
+
};
|
|
275
|
+
const rewriteLine = (line) => inlineLinks(line)
|
|
276
|
+
.replace(/^(\s{0,3}\[[^\]\n]+\]:\s*)(?:<([^>\n]*)>|([^\s]+))/gm, (all, prefix, angled, plain) => replacement(all, prefix, angled ?? plain ?? "", "", true))
|
|
277
|
+
.replace(/(\b(?:href|src)\s*=\s*["'])([^"'\n]*)(["'])/gi, (all, prefix, href, suffix) => replacement(all, prefix, href, suffix, false));
|
|
278
|
+
let fence = "";
|
|
279
|
+
let frontmatter = path.posix.basename(item.target) === "SKILL.md" &&
|
|
280
|
+
/^---\r?\n/.test(body);
|
|
281
|
+
return body
|
|
282
|
+
.split("\n")
|
|
283
|
+
.map((line, index) => {
|
|
284
|
+
if (frontmatter) {
|
|
285
|
+
if (index > 0 && /^---\r?$/.test(line))
|
|
286
|
+
frontmatter = false;
|
|
287
|
+
return line;
|
|
288
|
+
}
|
|
289
|
+
const marker = /^\s{0,3}(`{3,}|~{3,})/.exec(line)?.[1];
|
|
290
|
+
if (marker) {
|
|
291
|
+
if (!fence)
|
|
292
|
+
fence = marker;
|
|
293
|
+
else if (marker[0] === fence[0] && marker.length >= fence.length)
|
|
294
|
+
fence = "";
|
|
295
|
+
return line;
|
|
296
|
+
}
|
|
297
|
+
if (fence)
|
|
298
|
+
return line;
|
|
299
|
+
return line
|
|
300
|
+
.split(/(`[^`]*`)/g)
|
|
301
|
+
.map((part, index) => (index % 2 ? part : rewriteLine(part)))
|
|
302
|
+
.join("");
|
|
303
|
+
})
|
|
304
|
+
.join("\n");
|
|
305
|
+
};
|
|
306
|
+
const readSource = async (file, boundary) => {
|
|
307
|
+
signal?.throwIfAborted();
|
|
308
|
+
const lexical = path.resolve(descriptor.root, file.relativePath);
|
|
309
|
+
requireCondition(contains(descriptor.root, lexical) &&
|
|
310
|
+
(await realpath(lexical)) === file.path &&
|
|
311
|
+
(await realpath(boundary)) === boundary &&
|
|
312
|
+
contains(boundary, file.path), "extension_package_changed", "A declared reading source redirected after static inspection.");
|
|
313
|
+
const read = await boundedFile(file.path, Math.min(file.size, this.#maxBytes), signal);
|
|
314
|
+
requireCondition(read.bytes.length === file.size &&
|
|
315
|
+
read.revision === file.revision &&
|
|
316
|
+
(await realpath(lexical)) === file.path, "extension_package_changed", "A declared reading source changed after static inspection.");
|
|
317
|
+
return read.bytes;
|
|
318
|
+
};
|
|
319
|
+
requireCondition((await realpath(parent)) === parent, "extension_path_escape", "A reading parent must not redirect before staging.");
|
|
320
|
+
const hold = await this.#retention?.hold("extension", directory);
|
|
321
|
+
let transferred = false;
|
|
322
|
+
let temporary;
|
|
323
|
+
try {
|
|
324
|
+
temporary = await mkdtemp(path.join(parent, ".prepare-"));
|
|
325
|
+
}
|
|
326
|
+
catch (cause) {
|
|
327
|
+
hold?.release();
|
|
328
|
+
throw cause;
|
|
329
|
+
}
|
|
330
|
+
this.#stages.add(temporary);
|
|
331
|
+
let unprotect;
|
|
332
|
+
let totalBytes = 0;
|
|
333
|
+
const saved = [];
|
|
334
|
+
const save = async (filename, bytes, source) => {
|
|
335
|
+
signal?.throwIfAborted();
|
|
336
|
+
requireCondition(saved.length + 1 <= this.#maxFiles &&
|
|
337
|
+
totalBytes + bytes.length <= this.#maxBytes, "extension_material_too_large", "Generated references and copied materials exceed their complete snapshot budget.");
|
|
338
|
+
const target = path.join(temporary, filename);
|
|
339
|
+
requireCondition((await realpath(temporary)) === temporary, "extension_path_escape", "The private reading stage must not redirect.");
|
|
340
|
+
let parent = temporary;
|
|
341
|
+
for (const part of path.posix.dirname(filename).split("/")) {
|
|
342
|
+
if (part === ".")
|
|
343
|
+
continue;
|
|
344
|
+
const next = path.join(parent, part);
|
|
345
|
+
const info = await lstat(next).catch((error) => {
|
|
346
|
+
if (error.code === "ENOENT")
|
|
347
|
+
return null;
|
|
348
|
+
throw error;
|
|
349
|
+
});
|
|
350
|
+
requireCondition(!info || (info.isDirectory() && !info.isSymbolicLink()), "extension_path_escape", "A reading directory must not be a file or symbolic link.");
|
|
351
|
+
if (!info)
|
|
352
|
+
await mkdir(next, { mode: 0o700 });
|
|
353
|
+
requireCondition((await realpath(next)) === next && contains(temporary, next), "extension_path_escape", "A reading directory escaped its private stage.");
|
|
354
|
+
parent = next;
|
|
355
|
+
}
|
|
356
|
+
requireCondition(contains(temporary, await realpath(path.dirname(target))), "extension_path_escape", "A generated reading directory escaped its private staging area.");
|
|
357
|
+
const reservation = bytes.length
|
|
358
|
+
? await this.#reserve?.(bytes.length)
|
|
359
|
+
: undefined;
|
|
360
|
+
try {
|
|
361
|
+
signal?.throwIfAborted();
|
|
362
|
+
requireCondition((await realpath(temporary)) === temporary &&
|
|
363
|
+
(await realpath(path.dirname(target))) === path.dirname(target), "extension_path_escape", "The reading stage changed during storage admission.");
|
|
364
|
+
await writeFile(target, bytes, { flag: "wx", mode: 0o600 });
|
|
365
|
+
}
|
|
366
|
+
finally {
|
|
367
|
+
reservation?.release();
|
|
368
|
+
}
|
|
369
|
+
totalBytes += bytes.length;
|
|
370
|
+
saved.push({
|
|
371
|
+
path: filename,
|
|
372
|
+
revision: "sha256:" + digest(bytes),
|
|
373
|
+
size: bytes.length,
|
|
374
|
+
...(source
|
|
375
|
+
? { source: { path: source.relativePath, revision: source.revision } }
|
|
376
|
+
: {}),
|
|
377
|
+
});
|
|
378
|
+
};
|
|
379
|
+
try {
|
|
380
|
+
unprotect = this.#retention?.protectPending(temporary);
|
|
381
|
+
let instructionContent;
|
|
382
|
+
for (const item of plan) {
|
|
383
|
+
let bytes = await readSource(item.source, item.boundary);
|
|
384
|
+
if (/\.md$/i.test(item.target) ||
|
|
385
|
+
item.source === descriptor.materials.instructions) {
|
|
386
|
+
const original = text(bytes);
|
|
387
|
+
const rewritten = rewrite(original, item, Math.min(this.#maxBytes - totalBytes, item.source === descriptor.materials.instructions
|
|
388
|
+
? 65536
|
|
389
|
+
: 8 * 1024 * 1024));
|
|
390
|
+
if (rewritten !== original)
|
|
391
|
+
bytes = Buffer.from(rewritten);
|
|
392
|
+
}
|
|
393
|
+
if (item.source === descriptor.materials.instructions)
|
|
394
|
+
instructionContent = text(bytes);
|
|
395
|
+
await save(item.target, bytes, item.source);
|
|
396
|
+
}
|
|
397
|
+
const generated = (prefix, parts) => {
|
|
398
|
+
let size = Buffer.byteLength(prefix);
|
|
399
|
+
const chunks = [prefix];
|
|
400
|
+
for (const part of parts) {
|
|
401
|
+
size += Buffer.byteLength(part);
|
|
402
|
+
requireCondition(totalBytes + size <= this.#maxBytes, "extension_material_too_large", "Generated method references exceed their snapshot budget.");
|
|
403
|
+
chunks.push(part);
|
|
404
|
+
}
|
|
405
|
+
return chunks.join("");
|
|
406
|
+
};
|
|
407
|
+
const methodText = generated("# " +
|
|
408
|
+
label(origin.namespace) +
|
|
409
|
+
" 方法参考\n\n来源:" +
|
|
410
|
+
label(origin.owner) +
|
|
411
|
+
" " +
|
|
412
|
+
label(descriptor.version) +
|
|
413
|
+
";" +
|
|
414
|
+
origin.revision +
|
|
415
|
+
"。仅包含已准备的公开方法与 Schema,不含配置或凭据。\n\n", (function* () {
|
|
416
|
+
for (const method of handle.methods)
|
|
417
|
+
yield "## " +
|
|
418
|
+
label([origin.namespace, ...method.path].join(".")) +
|
|
419
|
+
"\n\n" +
|
|
420
|
+
String(method.description) +
|
|
421
|
+
"\n\n```json\n" +
|
|
422
|
+
canonicalJson(method, 4 * 1024 * 1024) +
|
|
423
|
+
"\n```\n";
|
|
424
|
+
})());
|
|
425
|
+
await save("reference/README.md", Buffer.from(methodText));
|
|
426
|
+
const scriptText = generated("# 组合脚本\n\n阅读副本不作为执行源;按 scriptName 执行仍核对当前启用包的原始声明目录。\n\n", (function* () {
|
|
427
|
+
for (const item of scripts?.entries ?? [])
|
|
428
|
+
yield "## " +
|
|
429
|
+
label(`${origin.namespace}/${item.metadata.scriptName}`) +
|
|
430
|
+
"\n\n" +
|
|
431
|
+
label(item.metadata.scriptDesc) +
|
|
432
|
+
"\n\n使用时机:" +
|
|
433
|
+
label(item.metadata.scriptWhenUse) +
|
|
434
|
+
"。\n\n[原始脚本与 JSON 头部阅读副本](<scripts/" +
|
|
435
|
+
encodeURIComponent(item.metadata.scriptName) +
|
|
436
|
+
">)\n\n```json\n" +
|
|
437
|
+
canonicalJson(item.metadata) +
|
|
438
|
+
"\n```\n";
|
|
439
|
+
})());
|
|
440
|
+
await save("reference/scripts.md", Buffer.from(scriptText));
|
|
441
|
+
const entryText = generated("# " +
|
|
442
|
+
label(origin.namespace) +
|
|
443
|
+
" 扩展手册\n\n" +
|
|
444
|
+
label(origin.owner) +
|
|
445
|
+
" " +
|
|
446
|
+
label(descriptor.version) +
|
|
447
|
+
";内容修订 " +
|
|
448
|
+
origin.revision +
|
|
449
|
+
"。\n\n以下内容是独立阅读副本;实际 SDK 与脚本仅在该来源已启用且当前授权通过时可用。\n\n## 作者手册\n\n", (function* () {
|
|
450
|
+
for (const item of plan)
|
|
451
|
+
if (item.target.startsWith("manual/") && /\.md$/i.test(item.target))
|
|
452
|
+
yield "- [" +
|
|
453
|
+
label(item.target.slice(7)) +
|
|
454
|
+
"](<" +
|
|
455
|
+
item.target.split("/").map(encodeURIComponent).join("/") +
|
|
456
|
+
">)\n";
|
|
457
|
+
yield "\n## 方法与脚本\n\n- [完整方法与 Schema](reference/README.md)\n- [全部组合脚本、用法与输入 Schema](reference/scripts.md)\n\n## 指令与 Skills\n\n";
|
|
458
|
+
for (const item of plan)
|
|
459
|
+
if (item.source === descriptor.materials.instructions ||
|
|
460
|
+
(item.target.startsWith("skills/") &&
|
|
461
|
+
path.posix.basename(item.target) === "SKILL.md"))
|
|
462
|
+
yield "- [" +
|
|
463
|
+
label(item.target) +
|
|
464
|
+
"](<" +
|
|
465
|
+
item.target.split("/").map(encodeURIComponent).join("/") +
|
|
466
|
+
">)\n";
|
|
467
|
+
})());
|
|
468
|
+
await save("README.md", Buffer.from(entryText));
|
|
469
|
+
// Verify the entire declared reading set again before exposing a fixed coordinate.
|
|
470
|
+
for (const item of plan)
|
|
471
|
+
await readSource(item.source, item.boundary);
|
|
472
|
+
signal?.throwIfAborted();
|
|
473
|
+
const manifest = Buffer.from(canonicalJson({ origin, revision: snapshotRevision, files: saved, diagnostics }, 16 * 1024 * 1024));
|
|
474
|
+
requireCondition(saved.length + 1 <= this.#maxFiles &&
|
|
475
|
+
totalBytes + manifest.length <= this.#maxBytes, "extension_material_too_large", "The complete reading manifest exceeds its snapshot budget.");
|
|
476
|
+
const reservation = await this.#reserve?.(manifest.length);
|
|
477
|
+
try {
|
|
478
|
+
signal?.throwIfAborted();
|
|
479
|
+
requireCondition((await realpath(temporary)) === temporary, "extension_path_escape", "The reading stage changed during manifest storage admission.");
|
|
480
|
+
await writeFile(path.join(temporary, "manifest.json"), manifest, {
|
|
481
|
+
flag: "wx",
|
|
482
|
+
mode: 0o600,
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
finally {
|
|
486
|
+
reservation?.release();
|
|
487
|
+
}
|
|
488
|
+
const verifyExisting = async () => {
|
|
489
|
+
requireCondition((await lstat(directory)).isDirectory() &&
|
|
490
|
+
(await realpath(directory)) === directory, "extension_material_conflict", "An existing reading revision cannot redirect or be replaced.");
|
|
491
|
+
const expected = new Map([
|
|
492
|
+
...saved.map((file) => [file.path, file]),
|
|
493
|
+
[
|
|
494
|
+
"manifest.json",
|
|
495
|
+
{
|
|
496
|
+
path: "manifest.json",
|
|
497
|
+
revision: "sha256:" + digest(manifest),
|
|
498
|
+
size: manifest.length,
|
|
499
|
+
},
|
|
500
|
+
],
|
|
501
|
+
]);
|
|
502
|
+
let found = 0;
|
|
503
|
+
const expectedDirectories = new Set();
|
|
504
|
+
for (const filename of expected.keys()) {
|
|
505
|
+
let parent = path.posix.dirname(filename);
|
|
506
|
+
while (parent !== ".") {
|
|
507
|
+
expectedDirectories.add(parent);
|
|
508
|
+
parent = path.posix.dirname(parent);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
const walk = async (current) => {
|
|
512
|
+
const entries = await opendir(current, { bufferSize: 32 });
|
|
513
|
+
for await (const item of entries) {
|
|
514
|
+
signal?.throwIfAborted();
|
|
515
|
+
const filename = path.join(current, item.name);
|
|
516
|
+
const info = await lstat(filename);
|
|
517
|
+
requireCondition(!info.isSymbolicLink(), "extension_material_conflict", "A retained reading snapshot contains a symbolic link.");
|
|
518
|
+
if (info.isDirectory()) {
|
|
519
|
+
requireCondition(expectedDirectories.has(relative(directory, filename)), "extension_material_conflict", "A retained reading revision has an unexpected directory.");
|
|
520
|
+
await walk(filename);
|
|
521
|
+
}
|
|
522
|
+
else {
|
|
523
|
+
const record = expected.get(relative(directory, filename));
|
|
524
|
+
requireCondition(info.isFile() && record, "extension_material_conflict", "A retained reading revision has unexpected files.");
|
|
525
|
+
const read = await boundedFile(filename, record.size, signal);
|
|
526
|
+
requireCondition(read.bytes.length === record.size &&
|
|
527
|
+
read.revision === record.revision, "extension_material_conflict", "A retained reading revision was modified; old coordinates cannot be overwritten.");
|
|
528
|
+
found++;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
await walk(directory);
|
|
533
|
+
requireCondition(found === expected.size, "extension_material_conflict", "A retained reading revision is incomplete.");
|
|
534
|
+
};
|
|
535
|
+
requireCondition((await realpath(parent)) === parent &&
|
|
536
|
+
(await realpath(temporary)) === temporary, "extension_path_escape", "A reading directory redirected before its immutable publication.");
|
|
537
|
+
const existing = await lstat(directory).catch((error) => {
|
|
538
|
+
if (error.code === "ENOENT")
|
|
539
|
+
return null;
|
|
540
|
+
throw error;
|
|
541
|
+
});
|
|
542
|
+
if (existing)
|
|
543
|
+
await verifyExisting();
|
|
544
|
+
else {
|
|
545
|
+
try {
|
|
546
|
+
await rename(temporary, directory);
|
|
547
|
+
}
|
|
548
|
+
catch (error) {
|
|
549
|
+
if (!["EEXIST", "ENOTEMPTY"].includes(error.code ?? ""))
|
|
550
|
+
throw error;
|
|
551
|
+
await verifyExisting();
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
const readingOrigin = { ...origin };
|
|
555
|
+
const prepared = freeze({
|
|
556
|
+
handleId: handle.id,
|
|
557
|
+
origin: readingOrigin,
|
|
558
|
+
directory,
|
|
559
|
+
entry: path.join(directory, "README.md"),
|
|
560
|
+
materials: {
|
|
561
|
+
instructions: descriptor.materials.instructions
|
|
562
|
+
? [
|
|
563
|
+
{
|
|
564
|
+
instructionId: `${origin.namespace}/instructions`,
|
|
565
|
+
kind: origin.type,
|
|
566
|
+
path: path.join(directory, "instructions", path.posix.basename(descriptor.materials.instructions.relativePath)),
|
|
567
|
+
content: instructionContent,
|
|
568
|
+
scope: { type: "agent", root: this.work },
|
|
569
|
+
origin: readingOrigin,
|
|
570
|
+
truncated: false,
|
|
571
|
+
},
|
|
572
|
+
]
|
|
573
|
+
: [],
|
|
574
|
+
skills: (descriptor.materials.skills?.entries ?? []).map((skill) => ({
|
|
575
|
+
skillId: `${origin.namespace}/${skill.metadata.name}`,
|
|
576
|
+
...skill.metadata,
|
|
577
|
+
path: path.join(directory, "skills", skill.metadata.name),
|
|
578
|
+
skillFile: path.join(directory, "skills", skill.metadata.name, "SKILL.md"),
|
|
579
|
+
scope: { type: "agent", root: this.work },
|
|
580
|
+
origin: readingOrigin,
|
|
581
|
+
})),
|
|
582
|
+
docs: [
|
|
583
|
+
{
|
|
584
|
+
name: origin.namespace,
|
|
585
|
+
description: "SDK 用法、作者手册、完整方法参考、组合脚本、指令与 Skills",
|
|
586
|
+
path: path.join(directory, "README.md"),
|
|
587
|
+
owner: origin.owner,
|
|
588
|
+
version: descriptor.version,
|
|
589
|
+
},
|
|
590
|
+
],
|
|
591
|
+
},
|
|
592
|
+
...(scripts
|
|
593
|
+
? {
|
|
594
|
+
scripts: {
|
|
595
|
+
type: origin.type,
|
|
596
|
+
owner: origin.owner,
|
|
597
|
+
namespace: origin.namespace,
|
|
598
|
+
version: descriptor.version,
|
|
599
|
+
directory: scripts.directory,
|
|
600
|
+
},
|
|
601
|
+
}
|
|
602
|
+
: {}),
|
|
603
|
+
diagnostics,
|
|
604
|
+
});
|
|
605
|
+
canonicalJson(prepared.materials, 4 * 1024 * 1024);
|
|
606
|
+
this.#prepared.add(prepared);
|
|
607
|
+
this.#holds.get(handle.id)?.release();
|
|
608
|
+
if (hold)
|
|
609
|
+
this.#holds.set(handle.id, hold);
|
|
610
|
+
this.#directories.set(handle.id, directory);
|
|
611
|
+
transferred = true;
|
|
612
|
+
return prepared;
|
|
613
|
+
}
|
|
614
|
+
finally {
|
|
615
|
+
try {
|
|
616
|
+
const info = await lstat(temporary).catch((cause) => {
|
|
617
|
+
if (cause.code === "ENOENT")
|
|
618
|
+
return null;
|
|
619
|
+
throw cause;
|
|
620
|
+
});
|
|
621
|
+
if (info) {
|
|
622
|
+
requireCondition(info.isDirectory() &&
|
|
623
|
+
!info.isSymbolicLink() &&
|
|
624
|
+
(await realpath(temporary)) === temporary, "extension_path_escape", "A failed reading stage cannot redirect its cleanup.");
|
|
625
|
+
rmSync(temporary, { recursive: true, force: true });
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
finally {
|
|
629
|
+
unprotect?.();
|
|
630
|
+
this.#stages.delete(temporary);
|
|
631
|
+
if (!transferred)
|
|
632
|
+
hold?.release();
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
#index(items) {
|
|
637
|
+
const published = freeze({
|
|
638
|
+
instructions: items.flatMap((item) => item.materials.instructions),
|
|
639
|
+
skills: items.flatMap((item) => item.materials.skills),
|
|
640
|
+
docs: items.flatMap((item) => item.materials.docs),
|
|
641
|
+
});
|
|
642
|
+
canonicalJson(published, 4 * 1024 * 1024);
|
|
643
|
+
return {
|
|
644
|
+
published,
|
|
645
|
+
sources: Object.freeze(items.flatMap((item) => (item.scripts ? [item.scripts] : []))),
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
publish(prepared) {
|
|
649
|
+
requireCondition(this.#prepared.has(prepared), "extension_material_source_mismatch", "Only this profile's completed material preparation may be published.");
|
|
650
|
+
const key = portable(prepared.origin.namespace);
|
|
651
|
+
const previous = this.#active.get(key);
|
|
652
|
+
requireCondition(!previous || previous === prepared, "namespace_conflict", "Another active extension already owns this material namespace.");
|
|
653
|
+
const items = [...this.#active.values()]
|
|
654
|
+
.filter((item) => item !== previous)
|
|
655
|
+
.concat(prepared)
|
|
656
|
+
.sort((a, b) => compare(a.origin.namespace, b.origin.namespace));
|
|
657
|
+
const index = this.#index(items);
|
|
658
|
+
this.#retention?.setPublished(`extension:${prepared.origin.namespace}`, prepared.directory);
|
|
659
|
+
// All validation precedes this synchronous commit; no user callback or await follows it.
|
|
660
|
+
this.#active.set(key, prepared);
|
|
661
|
+
this.#published = index.published;
|
|
662
|
+
this.#sources = index.sources;
|
|
663
|
+
}
|
|
664
|
+
withdraw(handleId) {
|
|
665
|
+
const previous = [...this.#active.values()].find((item) => item.handleId === handleId);
|
|
666
|
+
if (!previous) {
|
|
667
|
+
this.#holds.get(handleId)?.release();
|
|
668
|
+
this.#holds.delete(handleId);
|
|
669
|
+
this.#directories.delete(handleId);
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
const index = this.#index([...this.#active.values()].filter((item) => item !== previous));
|
|
673
|
+
this.#retention?.setPublished(`extension:${previous.origin.namespace}`, null);
|
|
674
|
+
this.#active.delete(portable(previous.origin.namespace));
|
|
675
|
+
this.#published = index.published;
|
|
676
|
+
this.#sources = index.sources;
|
|
677
|
+
this.#holds.get(handleId)?.release();
|
|
678
|
+
this.#holds.delete(handleId);
|
|
679
|
+
this.#directories.delete(handleId);
|
|
680
|
+
}
|
|
681
|
+
published() {
|
|
682
|
+
return this.#published;
|
|
683
|
+
}
|
|
684
|
+
scriptSources() {
|
|
685
|
+
return this.#sources;
|
|
686
|
+
}
|
|
687
|
+
activeDirectories() {
|
|
688
|
+
return new Set([...this.#directories.values(), ...this.#stages]);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
//# sourceMappingURL=materials.js.map
|