@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,1540 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { once } from "node:events";
|
|
3
|
+
import { types } from "node:util";
|
|
4
|
+
import { Blob, File } from "node:buffer";
|
|
5
|
+
import { AgentError, isAgentError, requireCondition, } from "../contracts/errors.js";
|
|
6
|
+
import { authorizeCall } from "../contracts/caller.js";
|
|
7
|
+
import { canonicalJson, digest, newId, isPlainRecord, } from "../contracts/json.js";
|
|
8
|
+
import { compileMethodSchema } from "../contracts/validation.js";
|
|
9
|
+
import { bindHostRequests, } from "./host-requests.js";
|
|
10
|
+
import { unavailableMaintenance, } from "../maintenance/requests.js";
|
|
11
|
+
import { extensionNamespace } from "../runtime/names.js";
|
|
12
|
+
import { NativePromiseAudit } from "../runtime/promises.js";
|
|
13
|
+
import { createOutput, outputIntent, rebuildOutputIntent, } from "../sdk/output.js";
|
|
14
|
+
import { unavailableState } from "./state.js";
|
|
15
|
+
import { unavailableTransfer } from "./transfer.js";
|
|
16
|
+
import { snapshotBlob } from "../results/artifacts.js";
|
|
17
|
+
import { mcpResult, rebuildMcpResult, mcpLogSnapshot, } from "../results/mcp.js";
|
|
18
|
+
import { transferOffer, rebuildTransferOffer, transferSafeJSON, } from "../transfer/offers.js";
|
|
19
|
+
function frozen(value) {
|
|
20
|
+
if (value && typeof value === "object") {
|
|
21
|
+
for (const child of Object.values(value))
|
|
22
|
+
frozen(child);
|
|
23
|
+
Object.freeze(value);
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
const json = (value) => frozen(JSON.parse(canonicalJson(value)));
|
|
28
|
+
// V8's native lazy stack accessor is data-bearing infrastructure, not an
|
|
29
|
+
// extension getter. It must remain usable without evaluating arbitrary getters.
|
|
30
|
+
const nativeStack = Object.getOwnPropertyDescriptor(new Error(), "stack");
|
|
31
|
+
const blobInternals = new Set(Reflect.ownKeys(new Blob([])));
|
|
32
|
+
const fileInternals = new Set(Reflect.ownKeys(new File([], "snapshot")));
|
|
33
|
+
const dataConstructors = [
|
|
34
|
+
Object,
|
|
35
|
+
Array,
|
|
36
|
+
Date,
|
|
37
|
+
RegExp,
|
|
38
|
+
Map,
|
|
39
|
+
Set,
|
|
40
|
+
Error,
|
|
41
|
+
EvalError,
|
|
42
|
+
RangeError,
|
|
43
|
+
ReferenceError,
|
|
44
|
+
SyntaxError,
|
|
45
|
+
TypeError,
|
|
46
|
+
URIError,
|
|
47
|
+
AggregateError,
|
|
48
|
+
ArrayBuffer,
|
|
49
|
+
SharedArrayBuffer,
|
|
50
|
+
DataView,
|
|
51
|
+
Int8Array,
|
|
52
|
+
Uint8Array,
|
|
53
|
+
Uint8ClampedArray,
|
|
54
|
+
Int16Array,
|
|
55
|
+
Uint16Array,
|
|
56
|
+
Int32Array,
|
|
57
|
+
Uint32Array,
|
|
58
|
+
Float32Array,
|
|
59
|
+
Float64Array,
|
|
60
|
+
BigInt64Array,
|
|
61
|
+
BigUint64Array,
|
|
62
|
+
];
|
|
63
|
+
const nativeDataConstructors = new Set(dataConstructors.map((value) => Function.prototype.toString.call(value)));
|
|
64
|
+
const dataPrototypes = new Set([
|
|
65
|
+
...dataConstructors.map((value) => value.prototype),
|
|
66
|
+
Object.getPrototypeOf(Uint8Array.prototype),
|
|
67
|
+
Buffer.prototype,
|
|
68
|
+
URL.prototype,
|
|
69
|
+
Blob.prototype,
|
|
70
|
+
File.prototype,
|
|
71
|
+
AgentError.prototype,
|
|
72
|
+
]);
|
|
73
|
+
for (const sample of [new Blob([]), new File([], "snapshot")]) {
|
|
74
|
+
for (const key of Reflect.ownKeys(sample)) {
|
|
75
|
+
const value = Reflect.get(sample, key);
|
|
76
|
+
if (value && typeof value === "object")
|
|
77
|
+
dataPrototypes.add(Object.getPrototypeOf(value));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const byteViews = new Map([
|
|
81
|
+
["Int8Array", Int8Array],
|
|
82
|
+
["Uint8Array", Uint8Array],
|
|
83
|
+
["Uint8ClampedArray", Uint8ClampedArray],
|
|
84
|
+
["Int16Array", Int16Array],
|
|
85
|
+
["Uint16Array", Uint16Array],
|
|
86
|
+
["Int32Array", Int32Array],
|
|
87
|
+
["Uint32Array", Uint32Array],
|
|
88
|
+
["Float32Array", Float32Array],
|
|
89
|
+
["Float64Array", Float64Array],
|
|
90
|
+
["BigInt64Array", BigInt64Array],
|
|
91
|
+
["BigUint64Array", BigUint64Array],
|
|
92
|
+
]);
|
|
93
|
+
const float16 = Reflect.get(globalThis, "Float16Array");
|
|
94
|
+
if (typeof float16 === "function") {
|
|
95
|
+
byteViews.set("Float16Array", float16);
|
|
96
|
+
dataPrototypes.add(float16.prototype);
|
|
97
|
+
nativeDataConstructors.add(Function.prototype.toString.call(float16));
|
|
98
|
+
}
|
|
99
|
+
const typedPrototype = Object.getPrototypeOf(Uint8Array.prototype);
|
|
100
|
+
nativeDataConstructors.add(Function.prototype.toString.call(Object.getOwnPropertyDescriptor(typedPrototype, "constructor").value));
|
|
101
|
+
const intrinsicGet = (prototype, key) => Object.getOwnPropertyDescriptor(prototype, key).get;
|
|
102
|
+
const typedBuffer = intrinsicGet(typedPrototype, "buffer");
|
|
103
|
+
const typedOffset = intrinsicGet(typedPrototype, "byteOffset");
|
|
104
|
+
const typedLength = intrinsicGet(typedPrototype, "byteLength");
|
|
105
|
+
const typedName = intrinsicGet(typedPrototype, Symbol.toStringTag);
|
|
106
|
+
const typedValues = Uint8Array.prototype.values;
|
|
107
|
+
const typedSet = Uint8Array.prototype.set;
|
|
108
|
+
const viewBuffer = intrinsicGet(DataView.prototype, "buffer");
|
|
109
|
+
const viewOffset = intrinsicGet(DataView.prototype, "byteOffset");
|
|
110
|
+
const viewLength = intrinsicGet(DataView.prototype, "byteLength");
|
|
111
|
+
const bufferLength = intrinsicGet(ArrayBuffer.prototype, "byteLength");
|
|
112
|
+
const sharedLength = intrinsicGet(SharedArrayBuffer.prototype, "byteLength");
|
|
113
|
+
const urlHref = intrinsicGet(URL.prototype, "href");
|
|
114
|
+
const regexpSource = intrinsicGet(RegExp.prototype, "source");
|
|
115
|
+
const regexpFlags = [
|
|
116
|
+
["hasIndices", "d"],
|
|
117
|
+
["global", "g"],
|
|
118
|
+
["ignoreCase", "i"],
|
|
119
|
+
["multiline", "m"],
|
|
120
|
+
["dotAll", "s"],
|
|
121
|
+
["unicode", "u"],
|
|
122
|
+
["unicodeSets", "v"],
|
|
123
|
+
["sticky", "y"],
|
|
124
|
+
].map(([name, flag]) => [intrinsicGet(RegExp.prototype, name), flag]);
|
|
125
|
+
// Match the largest supported inline byte budget; transport/encoder still
|
|
126
|
+
// applies the configured media limits and counts repeated delivery occurrences.
|
|
127
|
+
const snapshotByteLimit = 128 * 1024 * 1024;
|
|
128
|
+
const segment = (value) => typeof value === "string" &&
|
|
129
|
+
value.length > 0 &&
|
|
130
|
+
Buffer.byteLength(value) <= 1024 &&
|
|
131
|
+
!/[\0\r\n]/.test(value);
|
|
132
|
+
const same = (left, right) => left.length === right.length &&
|
|
133
|
+
left.every((value, index) => value === right[index]);
|
|
134
|
+
const prefix = (left, right) => left.length <= right.length &&
|
|
135
|
+
left.every((value, index) => value.toLowerCase() === right[index].toLowerCase());
|
|
136
|
+
/** Core factory/method lifecycle. Package parsing and five-contribution preparation own publication readiness. */
|
|
137
|
+
export class ExtensionRegistry {
|
|
138
|
+
resources;
|
|
139
|
+
#options;
|
|
140
|
+
#entries = new Set();
|
|
141
|
+
#handles = new WeakMap();
|
|
142
|
+
#preparationFailures = new WeakMap();
|
|
143
|
+
#packageMaintenance = new Set();
|
|
144
|
+
#replacements = new WeakMap();
|
|
145
|
+
constructor(resources, options) {
|
|
146
|
+
this.resources = resources;
|
|
147
|
+
this.#options = { ...options, permissions: new Set(options.permissions) };
|
|
148
|
+
requireCondition(Number.isSafeInteger(options.cleanupTimeoutMs ?? 5000) &&
|
|
149
|
+
(options.cleanupTimeoutMs ?? 5000) > 0 &&
|
|
150
|
+
(options.cleanupTimeoutMs ?? 5000) <= 30000, "invalid_extension_options", "Extension cleanup observation must be positive and bounded.");
|
|
151
|
+
}
|
|
152
|
+
#authorize(policy) {
|
|
153
|
+
authorizeCall(() => this.#options.authorize(policy));
|
|
154
|
+
}
|
|
155
|
+
prepare(value, factory) {
|
|
156
|
+
return this.#prepare(value, factory);
|
|
157
|
+
}
|
|
158
|
+
/** Prepare exactly one hidden successor while its published predecessor keeps serving. */
|
|
159
|
+
prepareReplacement(previousHandle, value, factory) {
|
|
160
|
+
const previous = this.#entry(previousHandle);
|
|
161
|
+
this.#instance(previous);
|
|
162
|
+
requireCondition(previous.state === "published", "extension_not_ready", "A replacement must name this Registry's published source instance.");
|
|
163
|
+
if (previous.replacement) {
|
|
164
|
+
const candidate = previous.replacement;
|
|
165
|
+
requireCondition(candidate.key === digest(json(value)) && candidate.factory === factory, "extension_busy", "Another successor already reserves this source instance.");
|
|
166
|
+
return candidate.replacementPreparation;
|
|
167
|
+
}
|
|
168
|
+
const prepared = this.#prepare(value, factory, previous);
|
|
169
|
+
const preparation = prepared.then((handle) => {
|
|
170
|
+
const candidate = this.#entry(handle);
|
|
171
|
+
const token = Object.freeze({
|
|
172
|
+
previous: previous.handle,
|
|
173
|
+
candidate: handle,
|
|
174
|
+
});
|
|
175
|
+
this.#replacements.set(token, {
|
|
176
|
+
previous,
|
|
177
|
+
candidate,
|
|
178
|
+
state: "prepared",
|
|
179
|
+
});
|
|
180
|
+
return token;
|
|
181
|
+
});
|
|
182
|
+
const candidate = previous.replacement;
|
|
183
|
+
if (candidate)
|
|
184
|
+
candidate.replacementPreparation = preparation;
|
|
185
|
+
void preparation.catch(() => { });
|
|
186
|
+
return preparation;
|
|
187
|
+
}
|
|
188
|
+
#prepare(value, factory, replaces) {
|
|
189
|
+
const source = json(value);
|
|
190
|
+
requireCondition(source.apiVersion === 1 &&
|
|
191
|
+
source.origin &&
|
|
192
|
+
segment(source.origin.version) &&
|
|
193
|
+
segment(source.origin.revision) &&
|
|
194
|
+
segment(source.origin.owner) &&
|
|
195
|
+
segment(source.origin.namespace) &&
|
|
196
|
+
["platform", "extension"].includes(source.origin.type) &&
|
|
197
|
+
typeof factory === "function", "invalid_extension", "An extension needs its actual source/version, supported API and factory.");
|
|
198
|
+
if (source.origin.type === "extension")
|
|
199
|
+
extensionNamespace(source.origin.namespace);
|
|
200
|
+
else
|
|
201
|
+
requireCondition([
|
|
202
|
+
"nexum",
|
|
203
|
+
"sh",
|
|
204
|
+
"output",
|
|
205
|
+
"transfer",
|
|
206
|
+
"runtime",
|
|
207
|
+
"state",
|
|
208
|
+
"maintenance",
|
|
209
|
+
"mcp",
|
|
210
|
+
"api",
|
|
211
|
+
].includes(source.origin.namespace), "invalid_namespace", "A platform source must use its registered namespace.");
|
|
212
|
+
requireCondition(Array.isArray(source.permissions) &&
|
|
213
|
+
new Set(source.permissions).size === source.permissions.length &&
|
|
214
|
+
source.permissions.every((permission) => typeof permission === "string" &&
|
|
215
|
+
this.#options.permissions.has(permission)), "invalid_extension_permissions", "The extension must declare known permissions explicitly.");
|
|
216
|
+
const root = source.bindingPath ?? [source.origin.namespace];
|
|
217
|
+
requireCondition(root.length > 0 &&
|
|
218
|
+
root.length <= 8 &&
|
|
219
|
+
root.every(segment) &&
|
|
220
|
+
root[0] === source.origin.namespace &&
|
|
221
|
+
(source.origin.type === "platform" || root.length === 1), "invalid_namespace", "The SDK path must stay inside its actual source namespace.");
|
|
222
|
+
this.#authorize({
|
|
223
|
+
origin: source.origin,
|
|
224
|
+
action: "prepare",
|
|
225
|
+
permissions: source.permissions,
|
|
226
|
+
});
|
|
227
|
+
const key = digest(source);
|
|
228
|
+
if (replaces)
|
|
229
|
+
requireCondition(same(replaces.root, root) &&
|
|
230
|
+
replaces.source.origin.type === source.origin.type &&
|
|
231
|
+
replaces.source.origin.owner === source.origin.owner &&
|
|
232
|
+
replaces.source.origin.namespace === source.origin.namespace, "extension_replacement_mismatch", "A successor must keep its exact source owner and SDK root.");
|
|
233
|
+
const previous = [...this.#entries].find((entry) => entry !== replaces &&
|
|
234
|
+
(prefix(entry.root, root) || prefix(root, entry.root)));
|
|
235
|
+
if (previous) {
|
|
236
|
+
requireCondition(previous.key === key &&
|
|
237
|
+
previous.factory === factory &&
|
|
238
|
+
["preparing", "ready", "published", "failed"].includes(previous.state), "namespace_conflict", "A source already owns this namespace or a parent/child SDK path.");
|
|
239
|
+
if (previous.state === "failed") {
|
|
240
|
+
if (!previous.recovery) {
|
|
241
|
+
const recovery = this.#cleanup(previous).then((cleanup) => {
|
|
242
|
+
if (cleanup.status !== "disabled") {
|
|
243
|
+
const failure = new AgentError("extension_cleanup_pending", "The failed instance must finish cleanup before another initialization.", { extensionId: previous.handle.id, cleanup });
|
|
244
|
+
this.#preparationFailures.set(failure, previous.handle);
|
|
245
|
+
throw failure;
|
|
246
|
+
}
|
|
247
|
+
this.#forget(previous);
|
|
248
|
+
return this.prepare(source, factory);
|
|
249
|
+
});
|
|
250
|
+
previous.recovery = recovery;
|
|
251
|
+
void recovery
|
|
252
|
+
.finally(() => {
|
|
253
|
+
if (previous.recovery === recovery)
|
|
254
|
+
previous.recovery = undefined;
|
|
255
|
+
})
|
|
256
|
+
.catch(() => { });
|
|
257
|
+
}
|
|
258
|
+
return previous.recovery;
|
|
259
|
+
}
|
|
260
|
+
return previous.preparation;
|
|
261
|
+
}
|
|
262
|
+
const controller = new AbortController();
|
|
263
|
+
const handle = {
|
|
264
|
+
id: newId("extension"),
|
|
265
|
+
origin: source.origin,
|
|
266
|
+
methods: [],
|
|
267
|
+
};
|
|
268
|
+
const preparation = Promise.withResolvers();
|
|
269
|
+
void preparation.promise.catch(() => { });
|
|
270
|
+
const entry = {
|
|
271
|
+
source,
|
|
272
|
+
key,
|
|
273
|
+
root: Object.freeze([...root]),
|
|
274
|
+
factory,
|
|
275
|
+
controller,
|
|
276
|
+
state: "preparing",
|
|
277
|
+
calls: 0,
|
|
278
|
+
scripts: 0,
|
|
279
|
+
methods: [],
|
|
280
|
+
protected: new WeakSet(),
|
|
281
|
+
storage: new Set(),
|
|
282
|
+
storagePending: new Set(),
|
|
283
|
+
preparation: preparation.promise,
|
|
284
|
+
handle,
|
|
285
|
+
resources: undefined,
|
|
286
|
+
...(replaces ? { replaces } : {}),
|
|
287
|
+
};
|
|
288
|
+
this.#entries.add(entry);
|
|
289
|
+
this.#handles.set(handle, entry);
|
|
290
|
+
if (replaces)
|
|
291
|
+
replaces.replacement = entry;
|
|
292
|
+
try {
|
|
293
|
+
entry.resources = this.resources.instance(source.origin, {
|
|
294
|
+
signal: controller.signal,
|
|
295
|
+
assertActive: () => this.#instance(entry),
|
|
296
|
+
authorize: () => this.#authorize({
|
|
297
|
+
origin: source.origin,
|
|
298
|
+
action: "resource",
|
|
299
|
+
permissions: source.permissions,
|
|
300
|
+
}),
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
catch (error) {
|
|
304
|
+
this.#forget(entry);
|
|
305
|
+
this.#handles.delete(handle);
|
|
306
|
+
preparation.reject(error);
|
|
307
|
+
return preparation.promise;
|
|
308
|
+
}
|
|
309
|
+
// Defer initialization until this one shared promise and namespace reservation exist.
|
|
310
|
+
void Promise.resolve()
|
|
311
|
+
.then(() => this.#initialize(entry))
|
|
312
|
+
.then(preparation.resolve, preparation.reject);
|
|
313
|
+
return entry.preparation;
|
|
314
|
+
}
|
|
315
|
+
#forget(entry) {
|
|
316
|
+
this.#entries.delete(entry);
|
|
317
|
+
if (entry.replaces?.replacement === entry)
|
|
318
|
+
entry.replaces.replacement = undefined;
|
|
319
|
+
entry.replaces = undefined;
|
|
320
|
+
entry.replacementPreparation = undefined;
|
|
321
|
+
}
|
|
322
|
+
#instance(entry) {
|
|
323
|
+
requireCondition(!this.#packageMaintenance.has(entry), "extension_busy", "The package is reserved for an approved dependency change.");
|
|
324
|
+
requireCondition(this.#entries.has(entry) &&
|
|
325
|
+
["preparing", "ready", "published"].includes(entry.state) &&
|
|
326
|
+
!entry.controller.signal.aborted, "extension_unavailable", "The originating extension instance is not available.");
|
|
327
|
+
}
|
|
328
|
+
#protectResources(entry, inner) {
|
|
329
|
+
const protect = (value) => {
|
|
330
|
+
entry.protected.add(value);
|
|
331
|
+
entry.protected.add(value.value);
|
|
332
|
+
return value;
|
|
333
|
+
};
|
|
334
|
+
const api = Object.freeze({
|
|
335
|
+
register: async (input) => {
|
|
336
|
+
// A synchronous handler may start registration without awaiting it and
|
|
337
|
+
// immediately return its controller. Protect the value before yielding.
|
|
338
|
+
if (input?.value &&
|
|
339
|
+
(typeof input.value === "object" || typeof input.value === "function"))
|
|
340
|
+
entry.protected.add(input.value);
|
|
341
|
+
return protect(await inner.register(input));
|
|
342
|
+
},
|
|
343
|
+
get: async (id) => protect(await inner.get(id)),
|
|
344
|
+
handoff: (id) => inner.handoff(id),
|
|
345
|
+
describe: (id) => inner.describe(id),
|
|
346
|
+
list: (query) => inner.list(query),
|
|
347
|
+
});
|
|
348
|
+
entry.protected.add(api);
|
|
349
|
+
return api;
|
|
350
|
+
}
|
|
351
|
+
#storage(entry, signal, assertAllowed) {
|
|
352
|
+
const check = () => {
|
|
353
|
+
signal.throwIfAborted();
|
|
354
|
+
this.#instance(entry);
|
|
355
|
+
requireCondition(entry.source.permissions.includes("storage.reserve"), "undeclared_permission", "The source must declare storage.reserve before reserving managed space.");
|
|
356
|
+
assertAllowed();
|
|
357
|
+
signal.throwIfAborted();
|
|
358
|
+
this.#instance(entry);
|
|
359
|
+
};
|
|
360
|
+
const api = Object.freeze({
|
|
361
|
+
reserve: async (bytes) => {
|
|
362
|
+
check();
|
|
363
|
+
requireCondition(Number.isSafeInteger(bytes) && bytes > 0, "invalid_storage_reservation", "Reserve a positive safe byte count before managed allocation.");
|
|
364
|
+
const provider = this.#options.storage;
|
|
365
|
+
requireCondition(provider, "storage_unavailable", "No profile storage reservation provider is configured.");
|
|
366
|
+
requireCondition(typeof provider === "object" &&
|
|
367
|
+
!types.isProxy(provider) &&
|
|
368
|
+
!types.isPromise(provider), "storage_configuration", "Storage must use the trusted synchronous service facade.");
|
|
369
|
+
let owner = provider, descriptor;
|
|
370
|
+
while (owner && !descriptor) {
|
|
371
|
+
requireCondition(!types.isProxy(owner), "storage_configuration", "Storage service prototypes cannot be proxies.");
|
|
372
|
+
descriptor = Object.getOwnPropertyDescriptor(owner, "reserve");
|
|
373
|
+
owner = Object.getPrototypeOf(owner);
|
|
374
|
+
}
|
|
375
|
+
requireCondition(descriptor &&
|
|
376
|
+
"value" in descriptor &&
|
|
377
|
+
typeof descriptor.value === "function" &&
|
|
378
|
+
!types.isProxy(descriptor.value), "storage_configuration", "Storage must expose its ordinary reserve method.");
|
|
379
|
+
const reserve = descriptor.value;
|
|
380
|
+
entry.protected.add(provider);
|
|
381
|
+
entry.protected.add(reserve);
|
|
382
|
+
const pending = (async () => {
|
|
383
|
+
const ticket = await Reflect.apply(reserve, provider, [
|
|
384
|
+
bytes,
|
|
385
|
+
]);
|
|
386
|
+
requireCondition(ticket &&
|
|
387
|
+
typeof ticket === "object" &&
|
|
388
|
+
!types.isProxy(ticket) &&
|
|
389
|
+
!types.isPromise(ticket), "storage_configuration", "Storage must return its narrow reservation ticket.");
|
|
390
|
+
entry.protected.add(ticket);
|
|
391
|
+
let owner = ticket, releaseDescriptor;
|
|
392
|
+
while (owner && !releaseDescriptor) {
|
|
393
|
+
requireCondition(!types.isProxy(owner), "storage_configuration", "Storage ticket prototypes cannot be proxies.");
|
|
394
|
+
releaseDescriptor = Object.getOwnPropertyDescriptor(owner, "release");
|
|
395
|
+
owner = Object.getPrototypeOf(owner);
|
|
396
|
+
}
|
|
397
|
+
requireCondition(releaseDescriptor &&
|
|
398
|
+
"value" in releaseDescriptor &&
|
|
399
|
+
typeof releaseDescriptor.value === "function" &&
|
|
400
|
+
!types.isProxy(releaseDescriptor.value), "storage_configuration", "Storage tickets must expose their ordinary synchronous release method.");
|
|
401
|
+
const innerRelease = releaseDescriptor.value;
|
|
402
|
+
entry.protected.add(innerRelease);
|
|
403
|
+
let released = false;
|
|
404
|
+
const release = () => {
|
|
405
|
+
if (released)
|
|
406
|
+
return;
|
|
407
|
+
released = true;
|
|
408
|
+
entry.storage.delete(release);
|
|
409
|
+
// Cleanup intentionally does not consult the ended invocation or
|
|
410
|
+
// revoked authority. The Core ticket itself owns the one refund.
|
|
411
|
+
authorizeCall(() => Reflect.apply(innerRelease, ticket, []));
|
|
412
|
+
};
|
|
413
|
+
const facade = Object.freeze({ release });
|
|
414
|
+
entry.protected.add(facade);
|
|
415
|
+
entry.protected.add(release);
|
|
416
|
+
entry.storage.add(release);
|
|
417
|
+
try {
|
|
418
|
+
check();
|
|
419
|
+
return facade;
|
|
420
|
+
}
|
|
421
|
+
catch (cause) {
|
|
422
|
+
release();
|
|
423
|
+
throw cause;
|
|
424
|
+
}
|
|
425
|
+
})();
|
|
426
|
+
if (!entry.storagePending.size)
|
|
427
|
+
entry.storageDone = new AbortController();
|
|
428
|
+
entry.storagePending.add(pending);
|
|
429
|
+
entry.calls++;
|
|
430
|
+
try {
|
|
431
|
+
return await pending;
|
|
432
|
+
}
|
|
433
|
+
finally {
|
|
434
|
+
entry.calls--;
|
|
435
|
+
entry.storagePending.delete(pending);
|
|
436
|
+
if (!entry.storagePending.size)
|
|
437
|
+
entry.storageDone?.abort();
|
|
438
|
+
}
|
|
439
|
+
},
|
|
440
|
+
});
|
|
441
|
+
entry.protected.add(api);
|
|
442
|
+
entry.protected.add(api.reserve);
|
|
443
|
+
return api;
|
|
444
|
+
}
|
|
445
|
+
#protectMaintenance(entry, inner, assertAllowed) {
|
|
446
|
+
if (types.isPromise(inner))
|
|
447
|
+
void Promise.prototype.then.call(inner, undefined, () => { });
|
|
448
|
+
requireCondition(isPlainRecord(inner), "maintenance_configuration", "A maintenance provider must synchronously return the public service.");
|
|
449
|
+
const api = {};
|
|
450
|
+
entry.protected.add(inner);
|
|
451
|
+
const method = (owner, name, access) => {
|
|
452
|
+
const descriptor = Object.getOwnPropertyDescriptor(owner, name);
|
|
453
|
+
requireCondition(descriptor &&
|
|
454
|
+
"value" in descriptor &&
|
|
455
|
+
typeof descriptor.value === "function" &&
|
|
456
|
+
!types.isProxy(descriptor.value), "maintenance_configuration", "A maintenance provider must expose the documented ordinary methods.");
|
|
457
|
+
const handler = descriptor.value;
|
|
458
|
+
entry.protected.add(handler);
|
|
459
|
+
const wrapper = (...args) => {
|
|
460
|
+
assertAllowed(access);
|
|
461
|
+
return Reflect.apply(handler, owner, args);
|
|
462
|
+
};
|
|
463
|
+
entry.protected.add(wrapper);
|
|
464
|
+
return wrapper;
|
|
465
|
+
};
|
|
466
|
+
for (const [group, names] of Object.entries({
|
|
467
|
+
packages: ["install", "update", "remove"],
|
|
468
|
+
extensions: ["enable", "disable"],
|
|
469
|
+
mcp: ["add", "update", "remove"],
|
|
470
|
+
api: ["import", "update", "remove"],
|
|
471
|
+
})) {
|
|
472
|
+
const descriptor = Object.getOwnPropertyDescriptor(inner, group);
|
|
473
|
+
requireCondition(descriptor && "value" in descriptor && isPlainRecord(descriptor.value), "maintenance_configuration", "The maintenance provider is missing a public method group.");
|
|
474
|
+
const owner = descriptor.value;
|
|
475
|
+
entry.protected.add(owner);
|
|
476
|
+
const groupApi = {};
|
|
477
|
+
for (const name of names)
|
|
478
|
+
groupApi[name] = method(owner, name, `${group}.${name}`);
|
|
479
|
+
api[group] = Object.freeze(groupApi);
|
|
480
|
+
entry.protected.add(groupApi);
|
|
481
|
+
}
|
|
482
|
+
for (const name of ["get", "list", "cancel"])
|
|
483
|
+
api[name] = method(inner, name, name);
|
|
484
|
+
entry.protected.add(api);
|
|
485
|
+
return Object.freeze(api);
|
|
486
|
+
}
|
|
487
|
+
#protectTransfer(entry, inner, assertAllowed) {
|
|
488
|
+
if (types.isPromise(inner))
|
|
489
|
+
void Promise.prototype.then.call(inner, undefined, () => { });
|
|
490
|
+
requireCondition(inner &&
|
|
491
|
+
typeof inner === "object" &&
|
|
492
|
+
!types.isProxy(inner) &&
|
|
493
|
+
!types.isPromise(inner) &&
|
|
494
|
+
["createUpload", "createDownload", "get", "list", "cancel"].every((name) => typeof Reflect.get(inner, name) === "function"), "transfer_configuration", "A transfer provider must synchronously return the five public methods.");
|
|
495
|
+
const invoke = (method, action) => {
|
|
496
|
+
assertAllowed(method);
|
|
497
|
+
return action();
|
|
498
|
+
};
|
|
499
|
+
const api = Object.freeze({
|
|
500
|
+
createUpload: (options) => invoke("createUpload", () => inner.createUpload(options)),
|
|
501
|
+
createDownload: (options) => invoke("createDownload", () => inner.createDownload(options)),
|
|
502
|
+
get: (id, options) => invoke("get", () => inner.get(id, options)),
|
|
503
|
+
list: (options) => invoke("list", () => inner.list(options)),
|
|
504
|
+
cancel: (id) => invoke("cancel", () => inner.cancel(id)),
|
|
505
|
+
});
|
|
506
|
+
entry.protected.add(inner);
|
|
507
|
+
entry.protected.add(api);
|
|
508
|
+
for (const method of Object.values(api))
|
|
509
|
+
entry.protected.add(method);
|
|
510
|
+
return api;
|
|
511
|
+
}
|
|
512
|
+
async #initialize(entry) {
|
|
513
|
+
const audit = new NativePromiseAudit();
|
|
514
|
+
const root = this.resources.store.root;
|
|
515
|
+
const host = Object.freeze({
|
|
516
|
+
context: json({
|
|
517
|
+
agentId: this.resources.store.binding.agentId,
|
|
518
|
+
profile: this.resources.store.binding.profile,
|
|
519
|
+
paths: {
|
|
520
|
+
work: path.join(root, "work"),
|
|
521
|
+
docs: path.join(root, "work", "docs"),
|
|
522
|
+
resources: path.join(root, "work", "resources"),
|
|
523
|
+
logs: path.join(root, "logs"),
|
|
524
|
+
},
|
|
525
|
+
}),
|
|
526
|
+
config: json(entry.source.config ?? {}),
|
|
527
|
+
signal: entry.controller.signal,
|
|
528
|
+
resources: this.#protectResources(entry, entry.resources.resources),
|
|
529
|
+
storage: this.#storage(entry, entry.controller.signal, () => this.#authorize({
|
|
530
|
+
origin: entry.source.origin,
|
|
531
|
+
action: "resource",
|
|
532
|
+
permissions: ["storage.reserve"],
|
|
533
|
+
})),
|
|
534
|
+
authorize: (target) => {
|
|
535
|
+
this.#instance(entry);
|
|
536
|
+
const fixed = json(target);
|
|
537
|
+
requireCondition(Object.keys(fixed).every((key) => ["type", "value", "access"].includes(key)) &&
|
|
538
|
+
["path", "url", "resource", "connection"].includes(fixed.type) &&
|
|
539
|
+
segment(fixed.value) &&
|
|
540
|
+
segment(fixed.access), "invalid_access_target", "Instance authorization needs an explicit path, URL, resource or connection target.");
|
|
541
|
+
this.#authorize({
|
|
542
|
+
origin: entry.source.origin,
|
|
543
|
+
action: "resource",
|
|
544
|
+
permissions: entry.source.permissions,
|
|
545
|
+
target: fixed,
|
|
546
|
+
});
|
|
547
|
+
this.#instance(entry);
|
|
548
|
+
},
|
|
549
|
+
log: (value) => this.#log(entry, value),
|
|
550
|
+
});
|
|
551
|
+
entry.protected.add(host);
|
|
552
|
+
entry.protected.add(host.config);
|
|
553
|
+
entry.protected.add(host.context);
|
|
554
|
+
try {
|
|
555
|
+
const candidate = await audit.run(() => entry.factory(host));
|
|
556
|
+
requireCondition(candidate && typeof candidate === "object" && !types.isProxy(candidate), "invalid_extension", "A factory must return a complete method table.");
|
|
557
|
+
entry.instance = candidate;
|
|
558
|
+
entry.protected.add(candidate);
|
|
559
|
+
let prototype = candidate;
|
|
560
|
+
while (prototype) {
|
|
561
|
+
requireCondition(!types.isProxy(prototype), "invalid_extension", "An instance cannot hide lifecycle callbacks behind a proxy.");
|
|
562
|
+
const dispose = Object.getOwnPropertyDescriptor(prototype, "dispose");
|
|
563
|
+
if (dispose) {
|
|
564
|
+
requireCondition("value" in dispose &&
|
|
565
|
+
(dispose.value === undefined ||
|
|
566
|
+
typeof dispose.value === "function"), "invalid_extension", "dispose must be an actual lifecycle function.");
|
|
567
|
+
entry.dispose = dispose.value;
|
|
568
|
+
break;
|
|
569
|
+
}
|
|
570
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
571
|
+
}
|
|
572
|
+
const table = Object.getOwnPropertyDescriptor(candidate, "methods");
|
|
573
|
+
requireCondition(table && "value" in table && isPlainRecord(table.value), "invalid_extension", "The method table must be a stable data property.");
|
|
574
|
+
const declarations = table.value;
|
|
575
|
+
const methods = [];
|
|
576
|
+
for (const name of Object.keys(declarations)) {
|
|
577
|
+
requireCondition(segment(name), "invalid_method", "A method name must be bounded text.");
|
|
578
|
+
const descriptor = Object.getOwnPropertyDescriptor(declarations, name);
|
|
579
|
+
requireCondition(descriptor &&
|
|
580
|
+
"value" in descriptor &&
|
|
581
|
+
descriptor.value &&
|
|
582
|
+
typeof descriptor.value === "object" &&
|
|
583
|
+
!types.isProxy(descriptor.value), "invalid_method", "Method declarations must be stable data properties.");
|
|
584
|
+
const fields = Object.getOwnPropertyDescriptors(descriptor.value);
|
|
585
|
+
requireCondition(Object.values(fields).every((field) => "value" in field), "invalid_method", "Method metadata cannot execute getters.");
|
|
586
|
+
const def = Object.fromEntries(Object.entries(fields).map(([key, field]) => [key, field.value]));
|
|
587
|
+
requireCondition(typeof def.description === "string" &&
|
|
588
|
+
def.description.length > 0 &&
|
|
589
|
+
def.description.length <= 4096 &&
|
|
590
|
+
typeof def.handle === "function" &&
|
|
591
|
+
Array.isArray(def.permissions) &&
|
|
592
|
+
new Set(def.permissions).size === def.permissions.length &&
|
|
593
|
+
def.permissions.every((permission) => entry.source.permissions.includes(permission)) &&
|
|
594
|
+
(def.normalize === undefined ||
|
|
595
|
+
typeof def.normalize === "function"), "invalid_method", "A method needs a description, explicit permission subset and handler.");
|
|
596
|
+
const target = def.path ?? [name];
|
|
597
|
+
requireCondition(target.length > 0 &&
|
|
598
|
+
target.length <= 8 &&
|
|
599
|
+
target.every(segment) &&
|
|
600
|
+
!methods.some((method) => (method.path.length <= target.length &&
|
|
601
|
+
method.path.every((value, index) => value === target[index])) ||
|
|
602
|
+
(target.length <= method.path.length &&
|
|
603
|
+
target.every((value, index) => value === method.path[index]))), "method_conflict", "Two methods cannot claim the same SDK path or its parent/child.");
|
|
604
|
+
const metadata = json({
|
|
605
|
+
name,
|
|
606
|
+
path: [...target],
|
|
607
|
+
description: def.description,
|
|
608
|
+
permissions: [...def.permissions],
|
|
609
|
+
inputSchema: def.inputSchema,
|
|
610
|
+
...(def.schemas ? { schemas: def.schemas } : {}),
|
|
611
|
+
...(def.schemaDialect ? { schemaDialect: def.schemaDialect } : {}),
|
|
612
|
+
...(def.outputSchema === undefined
|
|
613
|
+
? {}
|
|
614
|
+
: { outputSchema: def.outputSchema }),
|
|
615
|
+
...(def.outputSchemaDialect
|
|
616
|
+
? { outputSchemaDialect: def.outputSchemaDialect }
|
|
617
|
+
: {}),
|
|
618
|
+
});
|
|
619
|
+
const input = compileMethodSchema(metadata.inputSchema, def.schemaDialect, metadata.schemas);
|
|
620
|
+
const output = def.outputSchema === undefined
|
|
621
|
+
? undefined
|
|
622
|
+
: compileMethodSchema(metadata.outputSchema, def.outputSchemaDialect ??
|
|
623
|
+
(typeof def.outputSchema === "object" &&
|
|
624
|
+
def.outputSchema &&
|
|
625
|
+
"$schema" in def.outputSchema
|
|
626
|
+
? undefined
|
|
627
|
+
: def.schemaDialect), metadata.schemas);
|
|
628
|
+
entry.protected.add(def.handle);
|
|
629
|
+
methods.push({
|
|
630
|
+
name,
|
|
631
|
+
path: Object.freeze([...target]),
|
|
632
|
+
metadata,
|
|
633
|
+
permissions: Object.freeze([...def.permissions]),
|
|
634
|
+
normalize: def.normalize,
|
|
635
|
+
handle: def.handle,
|
|
636
|
+
input,
|
|
637
|
+
output,
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
await audit.checkpoint();
|
|
641
|
+
this.#instance(entry);
|
|
642
|
+
this.#authorize({
|
|
643
|
+
origin: entry.source.origin,
|
|
644
|
+
action: "prepare",
|
|
645
|
+
permissions: entry.source.permissions,
|
|
646
|
+
});
|
|
647
|
+
entry.methods = methods;
|
|
648
|
+
Object.assign(entry.handle, {
|
|
649
|
+
methods: Object.freeze(methods.map((method) => method.metadata)),
|
|
650
|
+
});
|
|
651
|
+
Object.freeze(entry.handle);
|
|
652
|
+
entry.state = "ready";
|
|
653
|
+
return entry.handle;
|
|
654
|
+
}
|
|
655
|
+
catch (cause) {
|
|
656
|
+
entry.state = "failed";
|
|
657
|
+
entry.controller.abort(new AgentError("extension_initialization_failed", "The extension failed before publication."));
|
|
658
|
+
const cleanup = await this.#cleanup(entry).catch(() => null);
|
|
659
|
+
if (cleanup?.status === "disabled")
|
|
660
|
+
this.#forget(entry);
|
|
661
|
+
const failure = new AgentError("extension_initialization_failed", "The extension's factory or declared method table could not be prepared.", { extensionId: entry.handle.id, ...(cleanup ? { cleanup } : {}) }, { cause });
|
|
662
|
+
if (this.#entries.has(entry))
|
|
663
|
+
this.#preparationFailures.set(failure, entry.handle);
|
|
664
|
+
throw failure;
|
|
665
|
+
}
|
|
666
|
+
finally {
|
|
667
|
+
audit.close();
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
#entry(handle) {
|
|
671
|
+
const entry = this.#handles.get(handle);
|
|
672
|
+
requireCondition(entry, "extension_unavailable", "This is not an extension instance from this Agent/profile.");
|
|
673
|
+
return entry;
|
|
674
|
+
}
|
|
675
|
+
/** Core-only identity proof for retrying this exact failed initialization. */
|
|
676
|
+
preparationFailure(cause, expectedSource) {
|
|
677
|
+
if (typeof cause !== "object" || cause === null)
|
|
678
|
+
return null;
|
|
679
|
+
const handle = this.#preparationFailures.get(cause);
|
|
680
|
+
const entry = handle ? this.#handles.get(handle) : undefined;
|
|
681
|
+
return entry &&
|
|
682
|
+
this.#entries.has(entry) &&
|
|
683
|
+
(expectedSource === undefined ||
|
|
684
|
+
entry.key === digest(json(expectedSource))) &&
|
|
685
|
+
(entry.state === "failed" || entry.state === "stopping")
|
|
686
|
+
? handle
|
|
687
|
+
: null;
|
|
688
|
+
}
|
|
689
|
+
/** Whether this exact unpublished instance still needs a Core cleanup decision. */
|
|
690
|
+
preparationRetained(handle) {
|
|
691
|
+
const entry = this.#handles.get(handle);
|
|
692
|
+
return !!(entry &&
|
|
693
|
+
this.#entries.has(entry) &&
|
|
694
|
+
["ready", "failed", "stopping"].includes(entry.state));
|
|
695
|
+
}
|
|
696
|
+
/** Core Resources bridge; never exposed through host, Invocation or script bindings. */
|
|
697
|
+
async resolveMcpResource(source, scope) {
|
|
698
|
+
requireCondition(source.agentId === this.resources.store.binding.agentId &&
|
|
699
|
+
source.profileRoot === this.resources.store.root, "mcp_profile_mismatch", "The MCP resource must target this actual Agent/profile.");
|
|
700
|
+
const entry = [...this.#entries].find((candidate) => candidate.handle.id === source.extensionId);
|
|
701
|
+
requireCondition(entry &&
|
|
702
|
+
canonicalJson(entry.source.origin) === canonicalJson(source.origin), "mcp_source_unavailable", "The original MCP extension instance is no longer available.");
|
|
703
|
+
const current = () => {
|
|
704
|
+
scope.signal?.throwIfAborted();
|
|
705
|
+
authorizeCall(() => scope.assertCurrent());
|
|
706
|
+
this.#instance(entry);
|
|
707
|
+
requireCondition(entry.state === "published", "mcp_source_unavailable", "The MCP source is not admitting new resource reads.");
|
|
708
|
+
authorizeCall(() => scope.authorizeConnection(source.connection, undefined, "result"));
|
|
709
|
+
this.#authorize({
|
|
710
|
+
origin: entry.source.origin,
|
|
711
|
+
action: "resource",
|
|
712
|
+
permissions: entry.source.permissions,
|
|
713
|
+
target: {
|
|
714
|
+
type: "resource",
|
|
715
|
+
value: source.connectionResourceId,
|
|
716
|
+
access: "get",
|
|
717
|
+
},
|
|
718
|
+
});
|
|
719
|
+
};
|
|
720
|
+
current();
|
|
721
|
+
// Standard Resources requests do not have a JS run. Use the exact original
|
|
722
|
+
// instance's resource facade after fresh request authorization; never forge
|
|
723
|
+
// a runId or relax the run-bound facade contract.
|
|
724
|
+
const ref = await entry.resources.resources.get(source.connectionResourceId);
|
|
725
|
+
current();
|
|
726
|
+
requireCondition(ref.kind === "mcp.connection" &&
|
|
727
|
+
ref.value.connection === source.connection &&
|
|
728
|
+
typeof ref.value.assertCurrent === "function" &&
|
|
729
|
+
typeof ref.value.readResource === "function", "mcp_source_unavailable", "The original resource is not a live MCP connection reader.");
|
|
730
|
+
const reader = ref.value;
|
|
731
|
+
const invoke = async (authority, access, uri, action) => {
|
|
732
|
+
requireCondition(authority === scope, "mcp_authorization_configuration", "A resource reader cannot replace the current request authority.");
|
|
733
|
+
entry.calls++;
|
|
734
|
+
try {
|
|
735
|
+
current();
|
|
736
|
+
authorizeCall(() => scope.authorizeConnection(source.connection, uri, access));
|
|
737
|
+
const result = await action();
|
|
738
|
+
current();
|
|
739
|
+
authorizeCall(() => scope.authorizeConnection(source.connection, uri, access));
|
|
740
|
+
return result;
|
|
741
|
+
}
|
|
742
|
+
finally {
|
|
743
|
+
entry.calls--;
|
|
744
|
+
}
|
|
745
|
+
};
|
|
746
|
+
return Object.freeze({
|
|
747
|
+
connection: reader.connection,
|
|
748
|
+
assertCurrent: () => {
|
|
749
|
+
current();
|
|
750
|
+
authorizeCall(() => reader.assertCurrent());
|
|
751
|
+
current();
|
|
752
|
+
},
|
|
753
|
+
readResource: (uri, authority) => invoke(authority, "resources.read", uri, () => reader.readResource(uri, authority)),
|
|
754
|
+
...(reader.listResources
|
|
755
|
+
? {
|
|
756
|
+
listResources: (authority, cursor) => invoke(authority, "resources.list", undefined, () => reader.listResources(authority, cursor)),
|
|
757
|
+
}
|
|
758
|
+
: {}),
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
#commitContributions(commit) {
|
|
762
|
+
if (commit === undefined)
|
|
763
|
+
return;
|
|
764
|
+
requireCondition(typeof commit === "function" &&
|
|
765
|
+
!types.isProxy(commit) &&
|
|
766
|
+
!types.isAsyncFunction(commit), "extension_publication_configuration", "Core contribution publication must be a synchronous commit.");
|
|
767
|
+
const result = commit();
|
|
768
|
+
if (types.isPromise(result))
|
|
769
|
+
void Promise.prototype.then.call(result, undefined, () => { });
|
|
770
|
+
requireCondition(result === undefined, "extension_publication_configuration", "Core contribution publication must finish synchronously.");
|
|
771
|
+
}
|
|
772
|
+
/** Core-only collaboration: commit prepared discovery indexes after all admission checks. */
|
|
773
|
+
publish(handle, commitContributions) {
|
|
774
|
+
const entry = this.#entry(handle);
|
|
775
|
+
this.#instance(entry);
|
|
776
|
+
requireCondition(!entry.replaces, "extension_replacement_required", "A prepared successor must retire its exact predecessor before publication.");
|
|
777
|
+
this.#authorize({
|
|
778
|
+
origin: entry.source.origin,
|
|
779
|
+
action: "publish",
|
|
780
|
+
permissions: entry.source.permissions,
|
|
781
|
+
});
|
|
782
|
+
requireCondition(entry.state === "ready" || entry.state === "published", "extension_not_ready", "Only a completely prepared extension may be published.");
|
|
783
|
+
// The supplied Core commit only changes already-prepared indexes. There is
|
|
784
|
+
// no await or extension callback between it and the SDK visibility change.
|
|
785
|
+
this.#commitContributions(commitContributions);
|
|
786
|
+
entry.state = "published";
|
|
787
|
+
}
|
|
788
|
+
#replacement(token) {
|
|
789
|
+
const replacement = this.#replacements.get(token);
|
|
790
|
+
requireCondition(replacement, "extension_replacement_unavailable", "This is not a replacement prepared by this Agent/profile Registry.");
|
|
791
|
+
return replacement;
|
|
792
|
+
}
|
|
793
|
+
/** Fence new business calls synchronously, then observe the old instance's real cleanup. */
|
|
794
|
+
retireReplacement(token) {
|
|
795
|
+
const replacement = this.#replacement(token);
|
|
796
|
+
const { previous, candidate } = replacement;
|
|
797
|
+
this.#instance(candidate);
|
|
798
|
+
requireCondition(candidate.state === "ready" && candidate.replaces === previous, "extension_not_ready", "The prepared successor is no longer available for this cutover.");
|
|
799
|
+
if (replacement.state === "retired")
|
|
800
|
+
return Promise.resolve({ status: "disabled" });
|
|
801
|
+
requireCondition(replacement.state !== "published", "extension_replacement_committed", "This successor has already been published.");
|
|
802
|
+
if (replacement.retirement)
|
|
803
|
+
return replacement.retirement;
|
|
804
|
+
requireCondition(!this.#packageMaintenance.has(previous), "extension_busy", "An approved package change already reserves the old instance.");
|
|
805
|
+
requireCondition(["published", "stopping", "disabled"].includes(previous.state), "extension_unavailable", "The original source instance cannot be retired for this successor.");
|
|
806
|
+
// No await occurs between these checks and disable's state transition.
|
|
807
|
+
// Instance-owned shared connections are not business occupancy; their real
|
|
808
|
+
// closure is joined by disable, without interrupting them during prepare.
|
|
809
|
+
this.assertIdle(candidate.handle);
|
|
810
|
+
this.assertIdle(previous.handle);
|
|
811
|
+
const work = this.disable(previous.handle);
|
|
812
|
+
replacement.state = "retiring";
|
|
813
|
+
const retirement = work.then((report) => {
|
|
814
|
+
if (report.status === "disabled")
|
|
815
|
+
replacement.state = "retired";
|
|
816
|
+
return report;
|
|
817
|
+
});
|
|
818
|
+
replacement.retirement = retirement;
|
|
819
|
+
void retirement
|
|
820
|
+
.finally(() => {
|
|
821
|
+
if (replacement.retirement === retirement)
|
|
822
|
+
replacement.retirement = undefined;
|
|
823
|
+
})
|
|
824
|
+
.catch(() => { });
|
|
825
|
+
return retirement;
|
|
826
|
+
}
|
|
827
|
+
/** Publish already-prepared discovery and SDK bindings only after confirmed old closure. */
|
|
828
|
+
publishReplacement(token, commitContributions) {
|
|
829
|
+
const replacement = this.#replacement(token);
|
|
830
|
+
const { previous, candidate } = replacement;
|
|
831
|
+
this.#instance(candidate);
|
|
832
|
+
if (replacement.state === "published") {
|
|
833
|
+
requireCondition(candidate.state === "published", "extension_unavailable", "The published successor is no longer available.");
|
|
834
|
+
return;
|
|
835
|
+
}
|
|
836
|
+
requireCondition(replacement.state === "retired" &&
|
|
837
|
+
previous.state === "disabled" &&
|
|
838
|
+
!this.#entries.has(previous) &&
|
|
839
|
+
candidate.state === "ready" &&
|
|
840
|
+
candidate.replaces === previous, "extension_replacement_not_retired", "The old instance must finish actual disposal and resource closure before publication.");
|
|
841
|
+
this.#authorize({
|
|
842
|
+
origin: candidate.source.origin,
|
|
843
|
+
action: "publish",
|
|
844
|
+
permissions: candidate.source.permissions,
|
|
845
|
+
});
|
|
846
|
+
// The caller has already prepared these indexes and committed its approved
|
|
847
|
+
// configuration. There is no extension callback or await during visibility.
|
|
848
|
+
this.#commitContributions(commitContributions);
|
|
849
|
+
candidate.replaces = undefined;
|
|
850
|
+
candidate.replacementPreparation = undefined;
|
|
851
|
+
previous.replacement = undefined;
|
|
852
|
+
candidate.state = "published";
|
|
853
|
+
replacement.state = "published";
|
|
854
|
+
}
|
|
855
|
+
bindings(services) {
|
|
856
|
+
const result = Object.create(null);
|
|
857
|
+
for (const entry of this.#entries)
|
|
858
|
+
if (entry.state === "published")
|
|
859
|
+
for (const method of entry.methods) {
|
|
860
|
+
const keys = [...entry.root, ...method.path];
|
|
861
|
+
let node = result;
|
|
862
|
+
for (const key of keys.slice(0, -1)) {
|
|
863
|
+
if (!Object.hasOwn(node, key))
|
|
864
|
+
Object.defineProperty(node, key, {
|
|
865
|
+
value: Object.create(null),
|
|
866
|
+
enumerable: true,
|
|
867
|
+
});
|
|
868
|
+
node = node[key];
|
|
869
|
+
}
|
|
870
|
+
Object.defineProperty(node, keys.at(-1), {
|
|
871
|
+
value: (...args) => this.#invoke(entry, method, services, args),
|
|
872
|
+
enumerable: true,
|
|
873
|
+
});
|
|
874
|
+
}
|
|
875
|
+
const freeze = (node) => {
|
|
876
|
+
for (const value of Object.values(node))
|
|
877
|
+
if (value && typeof value === "object")
|
|
878
|
+
freeze(value);
|
|
879
|
+
Object.freeze(node);
|
|
880
|
+
};
|
|
881
|
+
freeze(result);
|
|
882
|
+
return result;
|
|
883
|
+
}
|
|
884
|
+
#invoke(entry, method, services, args) {
|
|
885
|
+
services.assertActive();
|
|
886
|
+
this.#instance(entry);
|
|
887
|
+
requireCondition(entry.state === "published", "extension_unavailable", "This extension is no longer admitting SDK calls.");
|
|
888
|
+
let input;
|
|
889
|
+
if (method.normalize) {
|
|
890
|
+
try {
|
|
891
|
+
input = Reflect.apply(method.normalize, undefined, [
|
|
892
|
+
Object.freeze([...args]),
|
|
893
|
+
]);
|
|
894
|
+
}
|
|
895
|
+
catch (cause) {
|
|
896
|
+
throw this.#failure(entry, cause);
|
|
897
|
+
}
|
|
898
|
+
if (types.isPromise(input)) {
|
|
899
|
+
void Promise.prototype.then.call(input, undefined, () => { });
|
|
900
|
+
throw new AgentError("invalid_normalizer", "A method normalizer must return data synchronously.");
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
else {
|
|
904
|
+
requireCondition(args.length <= 1, "invalid_arguments", "This method accepts at most one input argument.");
|
|
905
|
+
input = args.length ? args[0] : {};
|
|
906
|
+
}
|
|
907
|
+
method.input(input);
|
|
908
|
+
const policy = {
|
|
909
|
+
origin: entry.source.origin,
|
|
910
|
+
action: "invoke",
|
|
911
|
+
permissions: method.permissions,
|
|
912
|
+
method: method.name,
|
|
913
|
+
runId: services.runId,
|
|
914
|
+
context: services.context,
|
|
915
|
+
input,
|
|
916
|
+
};
|
|
917
|
+
this.#authorize(policy);
|
|
918
|
+
entry.calls++;
|
|
919
|
+
let active = true;
|
|
920
|
+
const current = () => {
|
|
921
|
+
requireCondition(active, "invocation_closed", "The originating method invocation has already settled.");
|
|
922
|
+
services.assertCurrent();
|
|
923
|
+
this.#instance(entry);
|
|
924
|
+
this.#authorize(policy);
|
|
925
|
+
};
|
|
926
|
+
const close = () => {
|
|
927
|
+
if (active) {
|
|
928
|
+
active = false;
|
|
929
|
+
entry.calls--;
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
let entered = false;
|
|
933
|
+
try {
|
|
934
|
+
return services.operation(entry.source.origin, method.name, (operationId) => {
|
|
935
|
+
entered = true;
|
|
936
|
+
const resources = this.#protectResources(entry, entry.resources.bind({
|
|
937
|
+
runId: services.runId,
|
|
938
|
+
signal: services.signal,
|
|
939
|
+
assertActive: current,
|
|
940
|
+
authorize: (action, id) => this.#authorize({
|
|
941
|
+
...policy,
|
|
942
|
+
action: "resource",
|
|
943
|
+
...(id
|
|
944
|
+
? {
|
|
945
|
+
target: { type: "resource", value: id, access: action },
|
|
946
|
+
}
|
|
947
|
+
: {}),
|
|
948
|
+
}),
|
|
949
|
+
}));
|
|
950
|
+
const authorize = (target) => {
|
|
951
|
+
current();
|
|
952
|
+
requireCondition(target &&
|
|
953
|
+
[
|
|
954
|
+
"path",
|
|
955
|
+
"url",
|
|
956
|
+
"resource",
|
|
957
|
+
"connection",
|
|
958
|
+
"agent-state",
|
|
959
|
+
"user-state",
|
|
960
|
+
"transfer",
|
|
961
|
+
"maintenance",
|
|
962
|
+
].includes(target.type) &&
|
|
963
|
+
segment(target.value) &&
|
|
964
|
+
segment(target.access), "invalid_access_target", "An access check needs a bounded target and action.");
|
|
965
|
+
if (target.type === "agent-state" || target.type === "user-state") {
|
|
966
|
+
const permission = `state.${target.type === "agent-state" ? "agent" : "user"}.${target.access}`;
|
|
967
|
+
requireCondition(["read", "write"].includes(target.access) &&
|
|
968
|
+
method.permissions.includes(permission), "undeclared_permission", "A method must declare the shared-state capability it uses.");
|
|
969
|
+
}
|
|
970
|
+
if (target.type === "transfer") {
|
|
971
|
+
requireCondition([
|
|
972
|
+
"createUpload",
|
|
973
|
+
"createDownload",
|
|
974
|
+
"get",
|
|
975
|
+
"list",
|
|
976
|
+
"cancel",
|
|
977
|
+
].includes(target.access) &&
|
|
978
|
+
method.permissions.includes(`transfer.${target.access}`), "undeclared_permission", "A method must declare the transfer capability it uses.");
|
|
979
|
+
}
|
|
980
|
+
if (target.type === "maintenance") {
|
|
981
|
+
requireCondition(method.permissions.includes(`maintenance.${target.access}`), "undeclared_permission", "A method must declare the maintenance capability it uses.");
|
|
982
|
+
}
|
|
983
|
+
this.#authorize({ ...policy, target: json(target) });
|
|
984
|
+
};
|
|
985
|
+
const state = this.#options.state?.({
|
|
986
|
+
context: services.context,
|
|
987
|
+
signal: services.signal,
|
|
988
|
+
assertCurrent: current,
|
|
989
|
+
authorize,
|
|
990
|
+
}) ?? unavailableState(current);
|
|
991
|
+
entry.protected.add(state);
|
|
992
|
+
entry.protected.add(state.agent);
|
|
993
|
+
entry.protected.add(state.user);
|
|
994
|
+
const transfer = this.#protectTransfer(entry, this.#options.transfer?.({
|
|
995
|
+
context: services.context,
|
|
996
|
+
runId: services.runId,
|
|
997
|
+
signal: services.signal,
|
|
998
|
+
deadline: services.deadlineAt,
|
|
999
|
+
assertCurrent: current,
|
|
1000
|
+
authorize,
|
|
1001
|
+
}) ?? unavailableTransfer(current), (access) => {
|
|
1002
|
+
current();
|
|
1003
|
+
requireCondition(method.permissions.includes(`transfer.${access}`), "undeclared_permission", "A method must declare the transfer capability it uses.");
|
|
1004
|
+
});
|
|
1005
|
+
const maintenance = this.#protectMaintenance(entry, this.#options.maintenance?.({
|
|
1006
|
+
caller: services.context.caller,
|
|
1007
|
+
pwd: services.context.pwd,
|
|
1008
|
+
operationId,
|
|
1009
|
+
runId: services.runId,
|
|
1010
|
+
signal: services.signal,
|
|
1011
|
+
assertCurrent: current,
|
|
1012
|
+
authorize,
|
|
1013
|
+
}) ?? unavailableMaintenance(current), (access) => {
|
|
1014
|
+
current();
|
|
1015
|
+
requireCondition(method.permissions.includes(`maintenance.${access}`), "undeclared_permission", "A method must declare the maintenance capability it uses.");
|
|
1016
|
+
});
|
|
1017
|
+
const hostBinding = {
|
|
1018
|
+
context: services.context,
|
|
1019
|
+
runId: services.runId,
|
|
1020
|
+
signal: services.signal,
|
|
1021
|
+
deadline: services.deadlineAt,
|
|
1022
|
+
assertCurrent: current,
|
|
1023
|
+
};
|
|
1024
|
+
const hostProvider = this.#options.hostRequests?.(hostBinding);
|
|
1025
|
+
const hostRequests = hostProvider
|
|
1026
|
+
? bindHostRequests(hostProvider, hostBinding)
|
|
1027
|
+
: undefined;
|
|
1028
|
+
if (hostProvider)
|
|
1029
|
+
entry.protected.add(hostProvider);
|
|
1030
|
+
if (hostRequests) {
|
|
1031
|
+
entry.protected.add(hostRequests);
|
|
1032
|
+
for (const value of Object.values(hostRequests))
|
|
1033
|
+
if (typeof value === "function")
|
|
1034
|
+
entry.protected.add(value);
|
|
1035
|
+
}
|
|
1036
|
+
const call = Object.freeze({
|
|
1037
|
+
context: services.context,
|
|
1038
|
+
operationId,
|
|
1039
|
+
signal: services.signal,
|
|
1040
|
+
deadline: services.deadlineAt,
|
|
1041
|
+
resources,
|
|
1042
|
+
storage: this.#storage(entry, services.signal, () => {
|
|
1043
|
+
current();
|
|
1044
|
+
requireCondition(method.permissions.includes("storage.reserve"), "undeclared_permission", "The method must declare storage.reserve before reserving managed space.");
|
|
1045
|
+
this.#authorize({
|
|
1046
|
+
...policy,
|
|
1047
|
+
action: "resource",
|
|
1048
|
+
permissions: ["storage.reserve"],
|
|
1049
|
+
});
|
|
1050
|
+
current();
|
|
1051
|
+
}),
|
|
1052
|
+
output: createOutput(services.context.pwd, current, {
|
|
1053
|
+
agentId: this.resources.store.binding.agentId,
|
|
1054
|
+
profileRoot: this.resources.store.root,
|
|
1055
|
+
extensionId: entry.handle.id,
|
|
1056
|
+
origin: entry.source.origin,
|
|
1057
|
+
}),
|
|
1058
|
+
state,
|
|
1059
|
+
transfer,
|
|
1060
|
+
maintenance,
|
|
1061
|
+
...(hostRequests ? { hostRequests } : {}),
|
|
1062
|
+
log: (value) => {
|
|
1063
|
+
current();
|
|
1064
|
+
this.#log(entry, value, services.runId, operationId);
|
|
1065
|
+
},
|
|
1066
|
+
progress: (value) => {
|
|
1067
|
+
current();
|
|
1068
|
+
const data = frozen(JSON.parse(transferSafeJSON(value, 16384, mcpLogSnapshot)));
|
|
1069
|
+
canonicalJson(data, 16384);
|
|
1070
|
+
this.#options.progress(entry.source.origin, services.runId, operationId, data);
|
|
1071
|
+
},
|
|
1072
|
+
authorize,
|
|
1073
|
+
requestInput: (request) => {
|
|
1074
|
+
current();
|
|
1075
|
+
return services.requestInput(request);
|
|
1076
|
+
},
|
|
1077
|
+
});
|
|
1078
|
+
entry.protected.add(call);
|
|
1079
|
+
entry.protected.add(call.output);
|
|
1080
|
+
return Reflect.apply(method.handle, undefined, [input, call]);
|
|
1081
|
+
}, {
|
|
1082
|
+
complete: (value) => {
|
|
1083
|
+
value = this.#result(entry, value);
|
|
1084
|
+
if (method.output) {
|
|
1085
|
+
const result = outputIntent(value);
|
|
1086
|
+
const remote = mcpResult(value);
|
|
1087
|
+
if (remote) {
|
|
1088
|
+
if (Object.hasOwn(remote.model, "structuredContent"))
|
|
1089
|
+
method.output(remote.model.structuredContent);
|
|
1090
|
+
}
|
|
1091
|
+
else if (result?.kind === "result") {
|
|
1092
|
+
const payload = result.value;
|
|
1093
|
+
if (!payload.isError &&
|
|
1094
|
+
Object.hasOwn(payload, "structuredContent"))
|
|
1095
|
+
method.output(payload.structuredContent);
|
|
1096
|
+
}
|
|
1097
|
+
else if (result?.kind === "json")
|
|
1098
|
+
method.output(result.value);
|
|
1099
|
+
else if (!result && !this.#media(value))
|
|
1100
|
+
method.output(value);
|
|
1101
|
+
}
|
|
1102
|
+
return value;
|
|
1103
|
+
},
|
|
1104
|
+
failure: (cause) => this.#failure(entry, cause),
|
|
1105
|
+
settled: close,
|
|
1106
|
+
});
|
|
1107
|
+
}
|
|
1108
|
+
catch (error) {
|
|
1109
|
+
// Once admitted, only the operation's actual settlement releases its
|
|
1110
|
+
// occupancy. An observation failure does not prove the work stopped.
|
|
1111
|
+
if (!entered)
|
|
1112
|
+
close();
|
|
1113
|
+
throw error;
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
#media(value) {
|
|
1117
|
+
if (mcpResult(value))
|
|
1118
|
+
return true;
|
|
1119
|
+
if (transferOffer(value))
|
|
1120
|
+
return true;
|
|
1121
|
+
if (outputIntent(value))
|
|
1122
|
+
return true;
|
|
1123
|
+
if (Array.isArray(value))
|
|
1124
|
+
return value.length > 0 && value.every((item) => this.#media(item));
|
|
1125
|
+
if (!isPlainRecord(value))
|
|
1126
|
+
return false;
|
|
1127
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, "type");
|
|
1128
|
+
if (!descriptor ||
|
|
1129
|
+
!("value" in descriptor) ||
|
|
1130
|
+
!["text", "image", "audio", "resource_link", "resource"].includes(descriptor.value))
|
|
1131
|
+
return false;
|
|
1132
|
+
// Reuse the public content validator; a type tag by itself is not media.
|
|
1133
|
+
createOutput(this.resources.store.root, () => { }).result({
|
|
1134
|
+
content: [value],
|
|
1135
|
+
});
|
|
1136
|
+
return true;
|
|
1137
|
+
}
|
|
1138
|
+
#failure(entry, cause) {
|
|
1139
|
+
try {
|
|
1140
|
+
return this.#result(entry, cause, {
|
|
1141
|
+
copies: new Map(),
|
|
1142
|
+
internalOnly: new Set(),
|
|
1143
|
+
bytes: 0,
|
|
1144
|
+
nodes: 0,
|
|
1145
|
+
diagnostic: true,
|
|
1146
|
+
});
|
|
1147
|
+
}
|
|
1148
|
+
catch {
|
|
1149
|
+
return new AgentError("invalid_sdk_result", "The SDK failure contains a private control value or unsupported data.");
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
#bytes(value, snapshot, bufferView) {
|
|
1153
|
+
try {
|
|
1154
|
+
let backing, offset = 0, length;
|
|
1155
|
+
const dataView = types.isDataView(value);
|
|
1156
|
+
const typed = types.isTypedArray(value);
|
|
1157
|
+
if (typed) {
|
|
1158
|
+
// Validation is O(1). Intrinsic length alone reports zero for detached
|
|
1159
|
+
// or out-of-bounds resizable views, which must not become empty success.
|
|
1160
|
+
Reflect.apply(typedValues, value, []);
|
|
1161
|
+
backing = Reflect.apply(typedBuffer, value, []);
|
|
1162
|
+
offset = Reflect.apply(typedOffset, value, []);
|
|
1163
|
+
length = Reflect.apply(typedLength, value, []);
|
|
1164
|
+
}
|
|
1165
|
+
else if (dataView) {
|
|
1166
|
+
backing = Reflect.apply(viewBuffer, value, []);
|
|
1167
|
+
offset = Reflect.apply(viewOffset, value, []);
|
|
1168
|
+
length = Reflect.apply(viewLength, value, []);
|
|
1169
|
+
}
|
|
1170
|
+
else {
|
|
1171
|
+
backing = value;
|
|
1172
|
+
length = Reflect.apply(types.isSharedArrayBuffer(value) ? sharedLength : bufferLength, value, []);
|
|
1173
|
+
}
|
|
1174
|
+
requireCondition(Number.isSafeInteger(length) &&
|
|
1175
|
+
length >= 0 &&
|
|
1176
|
+
length <= snapshotByteLimit - snapshot.bytes, "data_too_large", "SDK byte snapshots exceed the supported result budget.");
|
|
1177
|
+
snapshot.bytes += length;
|
|
1178
|
+
const copy = types.isSharedArrayBuffer(backing)
|
|
1179
|
+
? new SharedArrayBuffer(length)
|
|
1180
|
+
: new ArrayBuffer(length);
|
|
1181
|
+
// Both views are core-created. No iterator, constructor, species, getter,
|
|
1182
|
+
// or metadata on the source view participates in reading its byte range.
|
|
1183
|
+
Reflect.apply(typedSet, new Uint8Array(copy), [
|
|
1184
|
+
new Uint8Array(backing, offset, length),
|
|
1185
|
+
]);
|
|
1186
|
+
if (!typed && !dataView)
|
|
1187
|
+
return copy;
|
|
1188
|
+
if (dataView)
|
|
1189
|
+
return new DataView(copy);
|
|
1190
|
+
if (bufferView)
|
|
1191
|
+
return Buffer.from(copy, 0, length);
|
|
1192
|
+
const name = Reflect.apply(typedName, value, []);
|
|
1193
|
+
const constructor = byteViews.get(name);
|
|
1194
|
+
requireCondition(constructor, "invalid_sdk_result", "The native byte view type is unsupported.");
|
|
1195
|
+
return new constructor(copy, 0, length / constructor.BYTES_PER_ELEMENT);
|
|
1196
|
+
}
|
|
1197
|
+
catch (cause) {
|
|
1198
|
+
if (cause instanceof AgentError)
|
|
1199
|
+
throw cause;
|
|
1200
|
+
throw new AgentError("invalid_sdk_result", "The native byte source is detached, out of bounds, or unavailable.", undefined, { cause });
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
#result(entry, value, snapshot = {
|
|
1204
|
+
copies: new Map(),
|
|
1205
|
+
internalOnly: new Set(),
|
|
1206
|
+
bytes: 0,
|
|
1207
|
+
nodes: 0,
|
|
1208
|
+
}, internal = false, depth = 0) {
|
|
1209
|
+
requireCondition(++snapshot.nodes <= 100000 && depth <= 64, "data_too_large", "The SDK result exceeds its item or depth budget.");
|
|
1210
|
+
if (value === null ||
|
|
1211
|
+
(typeof value !== "object" && typeof value !== "function"))
|
|
1212
|
+
return value;
|
|
1213
|
+
requireCondition(!entry.protected.has(value) &&
|
|
1214
|
+
typeof value !== "function" &&
|
|
1215
|
+
!types.isProxy(value), "invalid_sdk_result", "A method cannot return its host, invocation, handler or raw resource controls.");
|
|
1216
|
+
requireCondition(internal || !snapshot.internalOnly.has(value), "invalid_sdk_result", "Native implementation handles cannot become public SDK data.");
|
|
1217
|
+
const existing = snapshot.copies.get(value);
|
|
1218
|
+
if (existing)
|
|
1219
|
+
return existing;
|
|
1220
|
+
const remote = mcpResult(value);
|
|
1221
|
+
if (remote) {
|
|
1222
|
+
const data = this.#result(entry, snapshot.diagnostic ? mcpLogSnapshot(value) : remote.model, snapshot, false, depth + 1);
|
|
1223
|
+
const copy = snapshot.diagnostic ? data : rebuildMcpResult(value, data);
|
|
1224
|
+
snapshot.copies.set(value, copy);
|
|
1225
|
+
return copy;
|
|
1226
|
+
}
|
|
1227
|
+
const offer = transferOffer(value);
|
|
1228
|
+
if (offer) {
|
|
1229
|
+
const data = this.#result(entry, offer.snapshot, snapshot, false, depth + 1);
|
|
1230
|
+
const copy = snapshot.diagnostic
|
|
1231
|
+
? data
|
|
1232
|
+
: rebuildTransferOffer(value, data);
|
|
1233
|
+
snapshot.copies.set(value, copy);
|
|
1234
|
+
return copy;
|
|
1235
|
+
}
|
|
1236
|
+
requireCondition(snapshot.copies.size < 100000, "data_too_large", "The SDK result exceeds its object or depth budget.");
|
|
1237
|
+
let prototype = Object.getPrototypeOf(value), prototypeDepth = 0, bufferView = false, file = false;
|
|
1238
|
+
while (prototype) {
|
|
1239
|
+
requireCondition(++prototypeDepth <= 64 &&
|
|
1240
|
+
!types.isProxy(prototype) &&
|
|
1241
|
+
!entry.protected.has(prototype), "invalid_sdk_result", "SDK result prototypes cannot expose private controls.");
|
|
1242
|
+
const constructor = Object.getOwnPropertyDescriptor(prototype, "constructor");
|
|
1243
|
+
const native = constructor &&
|
|
1244
|
+
"value" in constructor &&
|
|
1245
|
+
typeof constructor.value === "function" &&
|
|
1246
|
+
!types.isProxy(constructor.value) &&
|
|
1247
|
+
nativeDataConstructors.has(Function.prototype.toString.call(constructor.value)) &&
|
|
1248
|
+
Object.getOwnPropertyDescriptor(constructor.value, "prototype")
|
|
1249
|
+
?.value === prototype;
|
|
1250
|
+
requireCondition(dataPrototypes.has(prototype) || native, "invalid_sdk_result", "SDK data must retain its ordinary built-in prototype, not a custom control prototype.");
|
|
1251
|
+
if (prototype === Buffer.prototype)
|
|
1252
|
+
bufferView = true;
|
|
1253
|
+
if (prototype === File.prototype)
|
|
1254
|
+
file = true;
|
|
1255
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
1256
|
+
}
|
|
1257
|
+
if (types.isArrayBufferView(value) || types.isAnyArrayBuffer(value)) {
|
|
1258
|
+
const copy = this.#bytes(value, snapshot, bufferView);
|
|
1259
|
+
snapshot.copies.set(value, copy);
|
|
1260
|
+
return copy;
|
|
1261
|
+
}
|
|
1262
|
+
const intent = outputIntent(value);
|
|
1263
|
+
const array = Array.isArray(value), plain = isPlainRecord(value), map = types.isMap(value), set = types.isSet(value);
|
|
1264
|
+
const date = types.isDate(value), regexp = types.isRegExp(value), error = types.isNativeError(value);
|
|
1265
|
+
const blob = value instanceof Blob, url = value instanceof URL;
|
|
1266
|
+
requireCondition(internal ||
|
|
1267
|
+
array ||
|
|
1268
|
+
plain ||
|
|
1269
|
+
intent ||
|
|
1270
|
+
map ||
|
|
1271
|
+
set ||
|
|
1272
|
+
date ||
|
|
1273
|
+
regexp ||
|
|
1274
|
+
error ||
|
|
1275
|
+
url ||
|
|
1276
|
+
blob, "invalid_sdk_result", "An SDK result contains an unsupported class or control object.");
|
|
1277
|
+
const remaining = 100000 - snapshot.nodes;
|
|
1278
|
+
if (array)
|
|
1279
|
+
requireCondition(Object.getOwnPropertyDescriptor(value, "length").value <= remaining, "data_too_large", "The SDK array exceeds its remaining item budget.");
|
|
1280
|
+
if (map)
|
|
1281
|
+
requireCondition(Reflect.apply(intrinsicGet(Map.prototype, "size"), value, []) * 2 <=
|
|
1282
|
+
remaining, "data_too_large", "The SDK map exceeds its remaining item budget.");
|
|
1283
|
+
if (set)
|
|
1284
|
+
requireCondition(Reflect.apply(intrinsicGet(Set.prototype, "size"), value, []) <=
|
|
1285
|
+
remaining, "data_too_large", "The SDK set exceeds its remaining item budget.");
|
|
1286
|
+
if (intent) {
|
|
1287
|
+
// Intents are issued and frozen by core. Rebuild only through that module
|
|
1288
|
+
// so copied payloads retain their brand without accepting forged shapes.
|
|
1289
|
+
const copy = rebuildOutputIntent(intent, this.#result(entry, intent.value, snapshot, false, depth + 1), this.#result(entry, intent.options, snapshot, false, depth + 1));
|
|
1290
|
+
snapshot.copies.set(value, copy);
|
|
1291
|
+
return copy;
|
|
1292
|
+
}
|
|
1293
|
+
let copy;
|
|
1294
|
+
if (array)
|
|
1295
|
+
copy = [];
|
|
1296
|
+
else if (plain)
|
|
1297
|
+
copy = Object.create(Object.getPrototypeOf(value) === null ? null : Object.prototype);
|
|
1298
|
+
else if (map)
|
|
1299
|
+
copy = new Map();
|
|
1300
|
+
else if (set)
|
|
1301
|
+
copy = new Set();
|
|
1302
|
+
else if (date)
|
|
1303
|
+
copy = new Date(Reflect.apply(Date.prototype.getTime, value, []));
|
|
1304
|
+
else if (regexp)
|
|
1305
|
+
copy = new RegExp(Reflect.apply(regexpSource, value, []), regexpFlags
|
|
1306
|
+
.filter(([getter]) => Reflect.apply(getter, value, []))
|
|
1307
|
+
.map(([, flag]) => flag)
|
|
1308
|
+
.join(""));
|
|
1309
|
+
else if (error) {
|
|
1310
|
+
copy = isAgentError(value)
|
|
1311
|
+
? new AgentError("sdk_operation_failed", "The SDK operation failed.")
|
|
1312
|
+
: new Error();
|
|
1313
|
+
Object.setPrototypeOf(copy, Object.getPrototypeOf(value));
|
|
1314
|
+
}
|
|
1315
|
+
else if (url)
|
|
1316
|
+
copy = new URL(Reflect.apply(urlHref, value, []));
|
|
1317
|
+
else if (blob) {
|
|
1318
|
+
// Node's public Blob/File intrinsics read forgeable Symbol fields. Only
|
|
1319
|
+
// the shared descriptor/receiver-checked helper may reach those internals.
|
|
1320
|
+
const fixed = snapshotBlob(value, snapshotByteLimit - snapshot.bytes);
|
|
1321
|
+
requireCondition(fixed, "invalid_sdk_result", "The Blob result has no valid native source.");
|
|
1322
|
+
snapshot.bytes += fixed.size;
|
|
1323
|
+
copy = file
|
|
1324
|
+
? new File([fixed.blob], fixed.name, {
|
|
1325
|
+
type: fixed.mimeType,
|
|
1326
|
+
lastModified: fixed.lastModified,
|
|
1327
|
+
})
|
|
1328
|
+
: fixed.blob;
|
|
1329
|
+
}
|
|
1330
|
+
else {
|
|
1331
|
+
// Keep this internal-only cache distinct from a publishable data copy.
|
|
1332
|
+
// A later ordinary field must not return the original Blob handle merely
|
|
1333
|
+
// because an earlier Blob validated the same implementation object.
|
|
1334
|
+
copy = value;
|
|
1335
|
+
snapshot.internalOnly.add(value);
|
|
1336
|
+
}
|
|
1337
|
+
snapshot.copies.set(value, copy);
|
|
1338
|
+
const keys = Reflect.ownKeys(value);
|
|
1339
|
+
requireCondition(keys.length <= 100000 - snapshot.nodes, "data_too_large", "The SDK object exceeds its remaining property budget.");
|
|
1340
|
+
for (const key of keys) {
|
|
1341
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
1342
|
+
// Node's createBlob/slice adds this own intrinsic constructor. It is
|
|
1343
|
+
// implementation metadata, not user-provided behavior or public data.
|
|
1344
|
+
if (blob &&
|
|
1345
|
+
key === "constructor" &&
|
|
1346
|
+
"value" in descriptor &&
|
|
1347
|
+
descriptor.value === Blob)
|
|
1348
|
+
continue;
|
|
1349
|
+
if (error &&
|
|
1350
|
+
key === "stack" &&
|
|
1351
|
+
descriptor.get === nativeStack?.get &&
|
|
1352
|
+
descriptor.set === nativeStack?.set &&
|
|
1353
|
+
!descriptor.enumerable)
|
|
1354
|
+
continue;
|
|
1355
|
+
const nativeBlobField = blob && (file ? fileInternals : blobInternals).has(key);
|
|
1356
|
+
requireCondition((typeof key === "string" || nativeBlobField) && "value" in descriptor, "invalid_sdk_result", "SDK result data cannot hide controls behind accessors or Symbol keys.");
|
|
1357
|
+
if (nativeBlobField)
|
|
1358
|
+
continue;
|
|
1359
|
+
const child = this.#result(entry, descriptor.value, snapshot, internal, depth + 1);
|
|
1360
|
+
if (copy !== value)
|
|
1361
|
+
Object.defineProperty(copy, key, { ...descriptor, value: child });
|
|
1362
|
+
}
|
|
1363
|
+
if (map)
|
|
1364
|
+
Map.prototype.forEach.call(value, (item, key) => {
|
|
1365
|
+
Map.prototype.set.call(copy, this.#result(entry, key, snapshot, false, depth + 1), this.#result(entry, item, snapshot, false, depth + 1));
|
|
1366
|
+
});
|
|
1367
|
+
else if (set)
|
|
1368
|
+
Set.prototype.forEach.call(value, (item) => {
|
|
1369
|
+
Set.prototype.add.call(copy, this.#result(entry, item, snapshot, false, depth + 1));
|
|
1370
|
+
});
|
|
1371
|
+
return copy;
|
|
1372
|
+
}
|
|
1373
|
+
#log(entry, value, runId, operationId) {
|
|
1374
|
+
const data = frozen(JSON.parse(transferSafeJSON(value, 16384, mcpLogSnapshot)));
|
|
1375
|
+
requireCondition(["debug", "info", "warn", "error"].includes(data.level) &&
|
|
1376
|
+
typeof data.message === "string", "invalid_log", "A log entry needs an explicit level and text message.");
|
|
1377
|
+
canonicalJson(data, 16384);
|
|
1378
|
+
this.#options.log(entry.source.origin, data, runId, operationId);
|
|
1379
|
+
}
|
|
1380
|
+
lease(origin) {
|
|
1381
|
+
const entries = [...this.#entries].filter((item) => item.state === "published" &&
|
|
1382
|
+
same([
|
|
1383
|
+
item.source.origin.owner,
|
|
1384
|
+
item.source.origin.namespace,
|
|
1385
|
+
item.source.origin.version,
|
|
1386
|
+
], [origin.owner, origin.namespace, origin.version]));
|
|
1387
|
+
const entry = entries.length === 1 ? entries[0] : undefined;
|
|
1388
|
+
requireCondition(entry && entry.state === "published", "extension_unavailable", "The script source is not the published extension revision.");
|
|
1389
|
+
this.#instance(entry);
|
|
1390
|
+
this.#authorize({
|
|
1391
|
+
origin: entry.source.origin,
|
|
1392
|
+
action: "invoke",
|
|
1393
|
+
permissions: [],
|
|
1394
|
+
scriptOrigin: json(origin),
|
|
1395
|
+
});
|
|
1396
|
+
entry.scripts++;
|
|
1397
|
+
let active = true;
|
|
1398
|
+
return () => {
|
|
1399
|
+
if (active) {
|
|
1400
|
+
active = false;
|
|
1401
|
+
entry.scripts--;
|
|
1402
|
+
}
|
|
1403
|
+
};
|
|
1404
|
+
}
|
|
1405
|
+
/** Synchronous preflight; does not stop an instance or withdraw its contributions. */
|
|
1406
|
+
assertIdle(handle) {
|
|
1407
|
+
const entry = this.#entry(handle);
|
|
1408
|
+
requireCondition(entry.state !== "preparing" &&
|
|
1409
|
+
entry.calls === 0 &&
|
|
1410
|
+
entry.scripts === 0 &&
|
|
1411
|
+
!entry.resources.busy(), "extension_busy", "Calls, active source scripts or independent business resources still use this version.");
|
|
1412
|
+
}
|
|
1413
|
+
/** Reserve all affected idle instances before the first asynchronous package mutation. */
|
|
1414
|
+
reservePackageChanges(handles) {
|
|
1415
|
+
const entries = [...new Set(handles.map((handle) => this.#entry(handle)))];
|
|
1416
|
+
for (const entry of entries) {
|
|
1417
|
+
requireCondition(!this.#packageMaintenance.has(entry), "extension_busy", "Another approved package change already holds this instance.");
|
|
1418
|
+
this.assertIdle(entry.handle);
|
|
1419
|
+
}
|
|
1420
|
+
for (const entry of entries)
|
|
1421
|
+
this.#packageMaintenance.add(entry);
|
|
1422
|
+
let active = true;
|
|
1423
|
+
return () => {
|
|
1424
|
+
if (!active)
|
|
1425
|
+
return;
|
|
1426
|
+
active = false;
|
|
1427
|
+
for (const entry of entries)
|
|
1428
|
+
this.#packageMaintenance.delete(entry);
|
|
1429
|
+
};
|
|
1430
|
+
}
|
|
1431
|
+
/** Core-only withdrawal occurs before abort/dispose can invoke extension callbacks. */
|
|
1432
|
+
disable(handle, withdrawContributions) {
|
|
1433
|
+
const entry = this.#entry(handle);
|
|
1434
|
+
this.#authorize({
|
|
1435
|
+
origin: entry.source.origin,
|
|
1436
|
+
action: "disable",
|
|
1437
|
+
permissions: entry.source.permissions,
|
|
1438
|
+
});
|
|
1439
|
+
if (entry.state === "disabled") {
|
|
1440
|
+
this.#commitContributions(withdrawContributions);
|
|
1441
|
+
return Promise.resolve({ status: "disabled" });
|
|
1442
|
+
}
|
|
1443
|
+
this.assertIdle(handle);
|
|
1444
|
+
if (entry.disposal)
|
|
1445
|
+
return entry.disposal;
|
|
1446
|
+
this.#commitContributions(withdrawContributions);
|
|
1447
|
+
entry.state = "stopping";
|
|
1448
|
+
entry.controller.abort(new AgentError("extension_stopping", "The originating extension instance is stopping."));
|
|
1449
|
+
const work = this.#cleanup(entry).then((report) => {
|
|
1450
|
+
if (report.status === "disabled") {
|
|
1451
|
+
entry.state = "disabled";
|
|
1452
|
+
this.#forget(entry);
|
|
1453
|
+
}
|
|
1454
|
+
return report;
|
|
1455
|
+
});
|
|
1456
|
+
entry.disposal = work;
|
|
1457
|
+
void work
|
|
1458
|
+
.finally(() => {
|
|
1459
|
+
if (entry.disposal === work)
|
|
1460
|
+
entry.disposal = undefined;
|
|
1461
|
+
})
|
|
1462
|
+
.catch(() => { });
|
|
1463
|
+
return work;
|
|
1464
|
+
}
|
|
1465
|
+
async #cleanup(entry) {
|
|
1466
|
+
if (!entry.disposed && !entry.disposeTask) {
|
|
1467
|
+
const done = new AbortController();
|
|
1468
|
+
entry.disposeDone = done;
|
|
1469
|
+
entry.disposeFailed = false;
|
|
1470
|
+
const task = Promise.resolve().then(() => entry.dispose
|
|
1471
|
+
? Reflect.apply(entry.dispose, entry.instance, [])
|
|
1472
|
+
: undefined);
|
|
1473
|
+
entry.disposeTask = task;
|
|
1474
|
+
void task.then(() => {
|
|
1475
|
+
entry.disposed = true;
|
|
1476
|
+
done.abort();
|
|
1477
|
+
}, () => {
|
|
1478
|
+
entry.disposeFailed = true;
|
|
1479
|
+
if (entry.disposeTask === task)
|
|
1480
|
+
entry.disposeTask = undefined;
|
|
1481
|
+
done.abort();
|
|
1482
|
+
});
|
|
1483
|
+
}
|
|
1484
|
+
if (!entry.resourceTask) {
|
|
1485
|
+
const done = new AbortController();
|
|
1486
|
+
entry.resourceDone = done;
|
|
1487
|
+
entry.resourceFailed = false;
|
|
1488
|
+
entry.resourceTask = Promise.resolve()
|
|
1489
|
+
.then(() => entry.resources.stop())
|
|
1490
|
+
.then((report) => {
|
|
1491
|
+
entry.resourceReport = report;
|
|
1492
|
+
done.abort();
|
|
1493
|
+
}, () => {
|
|
1494
|
+
entry.resourceFailed = true;
|
|
1495
|
+
done.abort();
|
|
1496
|
+
});
|
|
1497
|
+
}
|
|
1498
|
+
const observer = new AbortController();
|
|
1499
|
+
const timeout = new AbortController();
|
|
1500
|
+
const timer = setTimeout(() => timeout.abort(), this.#options.cleanupTimeoutMs ?? 5000);
|
|
1501
|
+
try {
|
|
1502
|
+
const waits = [entry.disposeDone, entry.resourceDone, entry.storageDone]
|
|
1503
|
+
.filter((done) => !!done && !done.signal.aborted)
|
|
1504
|
+
.map((done) => once(done.signal, "abort", { signal: observer.signal }));
|
|
1505
|
+
if (waits.length)
|
|
1506
|
+
await Promise.race([
|
|
1507
|
+
Promise.all(waits),
|
|
1508
|
+
once(timeout.signal, "abort", { signal: observer.signal }),
|
|
1509
|
+
]);
|
|
1510
|
+
}
|
|
1511
|
+
finally {
|
|
1512
|
+
clearTimeout(timer);
|
|
1513
|
+
observer.abort();
|
|
1514
|
+
}
|
|
1515
|
+
const resourcePending = !entry.resourceDone?.signal.aborted;
|
|
1516
|
+
const report = entry.resourceReport ?? { pending: resourcePending };
|
|
1517
|
+
const unconfirmed = Array.isArray(report.unconfirmed)
|
|
1518
|
+
? report.unconfirmed.length > 0
|
|
1519
|
+
: true;
|
|
1520
|
+
const complete = entry.disposed &&
|
|
1521
|
+
!entry.disposeFailed &&
|
|
1522
|
+
!resourcePending &&
|
|
1523
|
+
!entry.resourceFailed &&
|
|
1524
|
+
!unconfirmed &&
|
|
1525
|
+
entry.storagePending.size === 0;
|
|
1526
|
+
if (complete)
|
|
1527
|
+
for (const release of [...entry.storage])
|
|
1528
|
+
release();
|
|
1529
|
+
if (!resourcePending && (entry.resourceFailed || unconfirmed))
|
|
1530
|
+
entry.resourceTask = undefined;
|
|
1531
|
+
return {
|
|
1532
|
+
status: complete ? "disabled" : "stopping",
|
|
1533
|
+
disposeFailed: !!entry.disposeFailed,
|
|
1534
|
+
disposePending: !entry.disposed && !entry.disposeFailed,
|
|
1535
|
+
cleanup: report,
|
|
1536
|
+
...(entry.resourceFailed ? { resourceCleanupFailed: true } : {}),
|
|
1537
|
+
};
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
//# sourceMappingURL=registry.js.map
|