@nexum-hub/agent 0.0.0-stage → 0.0.1
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 +129 -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,1013 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { realpathSync } from "node:fs";
|
|
3
|
+
import { mkdtemp, writeFile, rename, rm, lstat } from "node:fs/promises";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { StringDecoder } from "node:string_decoder";
|
|
6
|
+
import { AgentError, isAgentError, requireCondition, } from "../contracts/errors.js";
|
|
7
|
+
import { canonicalJson, isPlainRecord, newId } from "../contracts/json.js";
|
|
8
|
+
import { privateDirectory } from "../runtime/paths.js";
|
|
9
|
+
import { maintenanceText } from "./files.js";
|
|
10
|
+
import { authorizeCall } from "../contracts/caller.js";
|
|
11
|
+
import { defaultEofAlreadyClosed } from "../process/backend.js";
|
|
12
|
+
import { maintenanceOrigin } from "./requests.js";
|
|
13
|
+
import { inspectExtensionPackage, } from "../extensions/packages.js";
|
|
14
|
+
const cap = 32 * 1024 * 1024;
|
|
15
|
+
// Runs in the existing managed native-child path. npm owns configuration and
|
|
16
|
+
// dependency resolution; only the Agent parent is allowed to save root records.
|
|
17
|
+
const npmPlannerSource = String.raw `
|
|
18
|
+
const fs = require('node:fs');
|
|
19
|
+
const path = require('node:path');
|
|
20
|
+
const { createRequire } = require('node:module');
|
|
21
|
+
const { createHash } = require('node:crypto');
|
|
22
|
+
const { once } = require('node:events');
|
|
23
|
+
const failure = code => Object.assign(new Error(code), { safeCode: code });
|
|
24
|
+
let sourceReady = false;
|
|
25
|
+
(async () => {
|
|
26
|
+
const cli = fs.realpathSync(process.argv[1]);
|
|
27
|
+
const root = fs.realpathSync(path.resolve(path.dirname(cli), '..'));
|
|
28
|
+
const fromNpm = createRequire(cli);
|
|
29
|
+
const npmPackage = path.join(root, 'package.json');
|
|
30
|
+
const pkg = JSON.parse(fs.readFileSync(npmPackage, 'utf8'));
|
|
31
|
+
if (pkg.name !== 'npm' || !/^\d+\.\d+\.\d+(?:[-+][A-Za-z0-9.-]+)?$/.test(pkg.version)) throw failure('npm_planner_unsupported');
|
|
32
|
+
const files = [cli, npmPackage];
|
|
33
|
+
const resolve = specifier => {
|
|
34
|
+
const filename = fs.realpathSync(fromNpm.resolve(specifier));
|
|
35
|
+
const relative = path.relative(root, filename);
|
|
36
|
+
if (relative === '..' || relative.startsWith('..' + path.sep) || path.isAbsolute(relative)) throw failure('npm_planner_unsupported');
|
|
37
|
+
files.push(filename); return filename;
|
|
38
|
+
};
|
|
39
|
+
const arbEntry = resolve('@npmcli/arborist');
|
|
40
|
+
const arbPackage = resolve('@npmcli/arborist/package.json');
|
|
41
|
+
const configEntry = resolve('@npmcli/config');
|
|
42
|
+
const configPackage = resolve('@npmcli/config/package.json');
|
|
43
|
+
const definitionsEntry = resolve('@npmcli/config/lib/definitions');
|
|
44
|
+
const Arborist = fromNpm(arbEntry), Config = fromNpm(configEntry);
|
|
45
|
+
const definitions = fromNpm(definitionsEntry);
|
|
46
|
+
if (typeof Arborist.prototype.lockfileString !== 'function' || typeof Config !== 'function' ||
|
|
47
|
+
typeof definitions.flatten !== 'function' || !definitions.definitions || !definitions.shorthands) throw failure('npm_planner_unsupported');
|
|
48
|
+
const fingerprint = () => {
|
|
49
|
+
const digest = createHash('sha256').update(root);
|
|
50
|
+
for (const filename of files) digest.update('\0').update(fs.readFileSync(filename));
|
|
51
|
+
return digest.digest('hex');
|
|
52
|
+
};
|
|
53
|
+
const source = {
|
|
54
|
+
npmVersion: pkg.version,
|
|
55
|
+
arboristVersion: JSON.parse(fs.readFileSync(arbPackage, 'utf8')).version,
|
|
56
|
+
configurationVersion: JSON.parse(fs.readFileSync(configPackage, 'utf8')).version,
|
|
57
|
+
revision: fingerprint(),
|
|
58
|
+
};
|
|
59
|
+
sourceReady = true;
|
|
60
|
+
if (process.argv[2] === 'source') { process.stdout.write(JSON.stringify({ source })); return; }
|
|
61
|
+
const maximum = Number(process.argv[6]);
|
|
62
|
+
const config = new Config({ npmPath: root, ...definitions, cwd: process.cwd(), env: process.env,
|
|
63
|
+
argv: [process.execPath, cli, '--global=false', '--prefix=' + process.cwd(), '--workspaces=false', '--ignore-scripts', '--save-exact', '--no-audit', '--no-fund'] });
|
|
64
|
+
await config.load(); config.validate();
|
|
65
|
+
const arb = new Arborist({ ...config.flat, path: process.cwd(), global: false, workspaces: [], workspacesEnabled: false, ignoreScripts: true, audit: false, fund: false });
|
|
66
|
+
const lock = await arb.lockfileString(process.argv[3] === 'remove'
|
|
67
|
+
? { rm: [process.argv[4]] } : { add: [process.argv[4] + '@' + process.argv[5]] });
|
|
68
|
+
const manifest = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8'));
|
|
69
|
+
const ideal = arb.idealTree.package;
|
|
70
|
+
for (const key of ['dependencies','devDependencies','optionalDependencies','peerDependencies','peerDependenciesMeta','bundleDependencies','bundledDependencies']) {
|
|
71
|
+
if (Object.hasOwn(ideal, key)) manifest[key] = ideal[key]; else delete manifest[key];
|
|
72
|
+
}
|
|
73
|
+
const body = JSON.stringify(manifest, null, 2) + '\n';
|
|
74
|
+
if (!Number.isSafeInteger(maximum) || maximum < 1 || Buffer.byteLength(body) > maximum || Buffer.byteLength(lock) > maximum) throw failure('npm_plan_too_large');
|
|
75
|
+
if (fingerprint() !== source.revision) throw failure('npm_planner_changed');
|
|
76
|
+
const result = Buffer.from(JSON.stringify({ source, manifest: body, lock }));
|
|
77
|
+
for (let offset = 0; offset < result.length; offset += 65536)
|
|
78
|
+
if (!process.stdout.write(result.subarray(offset, offset + 65536))) await once(process.stdout, 'drain');
|
|
79
|
+
})().catch(error => {
|
|
80
|
+
const code = ['npm_planner_unsupported','npm_plan_too_large','npm_planner_changed'].includes(error.safeCode) ? error.safeCode : sourceReady ? 'npm_plan_failed' : 'npm_planner_unsupported';
|
|
81
|
+
process.stdout.write(JSON.stringify({ error: code }));
|
|
82
|
+
});
|
|
83
|
+
`;
|
|
84
|
+
const hash = (value) => createHash("sha256")
|
|
85
|
+
.update(value === null ? "missing" : `present:${value}`)
|
|
86
|
+
.digest("hex");
|
|
87
|
+
const textFile = (filename, optional = false) => maintenanceText(filename, cap, optional);
|
|
88
|
+
function declaration(value) {
|
|
89
|
+
const scripts = value.materials.scripts?.entries ?? [], skills = value.materials.skills?.entries ?? [];
|
|
90
|
+
return {
|
|
91
|
+
namespace: value.source.origin.namespace,
|
|
92
|
+
version: value.version,
|
|
93
|
+
revision: value.revision,
|
|
94
|
+
permissions: [...value.source.permissions],
|
|
95
|
+
entry: value.entry.relativePath,
|
|
96
|
+
types: value.types.relativePath,
|
|
97
|
+
docs: {
|
|
98
|
+
directory: value.materials.docs.relativePath,
|
|
99
|
+
fileCount: value.materials.docs.files.length,
|
|
100
|
+
},
|
|
101
|
+
instructions: value.materials.instructions?.relativePath ?? null,
|
|
102
|
+
scripts: {
|
|
103
|
+
names: scripts.slice(0, 200).map((item) => item.scriptName),
|
|
104
|
+
count: scripts.length,
|
|
105
|
+
complete: scripts.length <= 200,
|
|
106
|
+
},
|
|
107
|
+
skills: {
|
|
108
|
+
names: skills.slice(0, 200).map((item) => item.publicName),
|
|
109
|
+
count: skills.length,
|
|
110
|
+
complete: skills.length <= 200,
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
async function replace(filename, body, reserve) {
|
|
115
|
+
const reservation = await reserve?.(Buffer.byteLength(body) + 1024);
|
|
116
|
+
let temp;
|
|
117
|
+
try {
|
|
118
|
+
const current = await lstat(filename).catch((cause) => {
|
|
119
|
+
if (cause.code === "ENOENT")
|
|
120
|
+
return null;
|
|
121
|
+
throw cause;
|
|
122
|
+
});
|
|
123
|
+
requireCondition(!current || current.isFile(), "maintenance_path_invalid", "Maintenance cannot replace a linked or special target.");
|
|
124
|
+
temp = `${filename}.${newId("tmp")}`;
|
|
125
|
+
await writeFile(temp, body, { mode: current?.mode ?? 0o600, flag: "wx" });
|
|
126
|
+
await rename(temp, filename);
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
try {
|
|
130
|
+
if (temp)
|
|
131
|
+
await rm(temp, { force: true });
|
|
132
|
+
}
|
|
133
|
+
finally {
|
|
134
|
+
reservation?.release();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export class ExtensionEnablement {
|
|
139
|
+
root;
|
|
140
|
+
reserve;
|
|
141
|
+
#state = {};
|
|
142
|
+
constructor(root, reserve) {
|
|
143
|
+
this.root = root;
|
|
144
|
+
this.reserve = reserve;
|
|
145
|
+
}
|
|
146
|
+
static async open(root, options = {}) {
|
|
147
|
+
const value = new ExtensionEnablement(realpathSync.native(root), options.reserve), body = await textFile(path.join(value.root, "state", "maintenance", "extensions.json"), true);
|
|
148
|
+
if (body) {
|
|
149
|
+
const parsed = JSON.parse(body);
|
|
150
|
+
requireCondition(isPlainRecord(parsed), "invalid_extension_intent", "The saved extension enablement is invalid.");
|
|
151
|
+
value.#state = parsed;
|
|
152
|
+
}
|
|
153
|
+
return value;
|
|
154
|
+
}
|
|
155
|
+
enabled() {
|
|
156
|
+
return new Set(Object.entries(this.#state)
|
|
157
|
+
.filter(([, value]) => value.enabled)
|
|
158
|
+
.map(([name]) => name));
|
|
159
|
+
}
|
|
160
|
+
get(name) {
|
|
161
|
+
const value = this.#state[name];
|
|
162
|
+
return value ? JSON.parse(canonicalJson(value)) : null;
|
|
163
|
+
}
|
|
164
|
+
async set(name, enabled, config, expectedRevision, options = {}) {
|
|
165
|
+
const existing = this.get(name);
|
|
166
|
+
requireCondition((existing?.revision ?? null) === expectedRevision, "maintenance_precondition_changed", "Extension enablement changed after user confirmation.");
|
|
167
|
+
const value = {
|
|
168
|
+
enabled,
|
|
169
|
+
objectId: existing?.objectId ?? newId("extension"),
|
|
170
|
+
revision: newId("revision"),
|
|
171
|
+
config: JSON.parse(canonicalJson(config)),
|
|
172
|
+
};
|
|
173
|
+
const next = { ...this.#state, [name]: value }, file = path.join(privateDirectory(this.root, "state", "maintenance"), "extensions.json");
|
|
174
|
+
await replace(file, canonicalJson(next), options.reserved ? undefined : this.reserve);
|
|
175
|
+
this.#state = next;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** Native npm planning/apply plus the existing extension lifecycle; no alternate resolver. */
|
|
179
|
+
export class NpmMaintenanceBackend {
|
|
180
|
+
options;
|
|
181
|
+
work;
|
|
182
|
+
root;
|
|
183
|
+
#busy = false;
|
|
184
|
+
#commands = new Set();
|
|
185
|
+
constructor(options) {
|
|
186
|
+
this.options = options;
|
|
187
|
+
this.root = realpathSync.native(options.root);
|
|
188
|
+
this.work = realpathSync.native(path.join(this.root, "work"));
|
|
189
|
+
requireCondition(options.modules.work === this.work &&
|
|
190
|
+
options.extensions.packages.modules === options.modules &&
|
|
191
|
+
options.enablement.root === this.root, "maintenance_profile_mismatch", "Package maintenance must use this actual work loader, extensions and profile.");
|
|
192
|
+
}
|
|
193
|
+
async #command(args, cwd, scope) {
|
|
194
|
+
scope.signal.throwIfAborted();
|
|
195
|
+
authorizeCall(() => scope.assertCurrent());
|
|
196
|
+
// registry/auth still follow native npm configuration. Its installation
|
|
197
|
+
// target cannot follow a user global/prefix/workspace override.
|
|
198
|
+
return await this.#child([
|
|
199
|
+
this.#npmCli(),
|
|
200
|
+
...args,
|
|
201
|
+
"--global=false",
|
|
202
|
+
`--prefix=${cwd}`,
|
|
203
|
+
"--workspaces=false",
|
|
204
|
+
], cwd, scope);
|
|
205
|
+
}
|
|
206
|
+
#npmCli() {
|
|
207
|
+
const cli = this.options.npmCli ??
|
|
208
|
+
(process.platform === "win32"
|
|
209
|
+
? path.join(path.dirname(process.execPath), "node_modules", "npm", "bin", "npm-cli.js")
|
|
210
|
+
: path.resolve(path.dirname(process.execPath), "..", "lib", "node_modules", "npm", "bin", "npm-cli.js"));
|
|
211
|
+
requireCondition(path.isAbsolute(cli), "npm_unavailable", "Native npm requires its actual absolute CLI entry.");
|
|
212
|
+
return cli;
|
|
213
|
+
}
|
|
214
|
+
async #child(args, cwd, scope, maximum = 1024 * 1024) {
|
|
215
|
+
scope.signal.throwIfAborted();
|
|
216
|
+
authorizeCall(() => scope.assertCurrent());
|
|
217
|
+
const decoder = new StringDecoder("utf8");
|
|
218
|
+
let output = "", size = 0;
|
|
219
|
+
const options = {
|
|
220
|
+
onOutput: async (stream, bytes) => {
|
|
221
|
+
if (stream !== "stdout")
|
|
222
|
+
return;
|
|
223
|
+
size += bytes.length;
|
|
224
|
+
requireCondition(size <= maximum, "npm_output_too_large", "Native npm output exceeds its bounded control response.");
|
|
225
|
+
output += decoder.write(bytes);
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
const backend = process.platform === "win32"
|
|
229
|
+
? new (await import("../process/windows-pipes.js")).WindowsProcessBackend(options)
|
|
230
|
+
: new (await import("../process/pipes.js")).PipeProcessBackend(options);
|
|
231
|
+
const owner = this.options.extensions.registry.resources.instance({ ...maintenanceOrigin, operationId: scope.operationId }, {
|
|
232
|
+
signal: scope.signal,
|
|
233
|
+
assertActive: () => authorizeCall(() => scope.assertCurrent()),
|
|
234
|
+
authorize: () => authorizeCall(() => scope.assertCurrent()),
|
|
235
|
+
});
|
|
236
|
+
this.#commands.add(backend);
|
|
237
|
+
void backend.closed.then(() => this.#commands.delete(backend), () => { });
|
|
238
|
+
let registered = false;
|
|
239
|
+
try {
|
|
240
|
+
await owner.resources.register({
|
|
241
|
+
kind: "maintenance.command",
|
|
242
|
+
value: backend,
|
|
243
|
+
});
|
|
244
|
+
registered = true;
|
|
245
|
+
const env = {};
|
|
246
|
+
for (const [key, value] of Object.entries({
|
|
247
|
+
...process.env,
|
|
248
|
+
...this.options.env,
|
|
249
|
+
}))
|
|
250
|
+
if (typeof value === "string")
|
|
251
|
+
env[key] = value;
|
|
252
|
+
await backend.start({
|
|
253
|
+
file: process.execPath,
|
|
254
|
+
args,
|
|
255
|
+
cwd,
|
|
256
|
+
env,
|
|
257
|
+
tty: false,
|
|
258
|
+
cols: 80,
|
|
259
|
+
rows: 24,
|
|
260
|
+
});
|
|
261
|
+
try {
|
|
262
|
+
await backend.write(new Uint8Array(), true, scope.signal);
|
|
263
|
+
}
|
|
264
|
+
catch (cause) {
|
|
265
|
+
scope.signal.throwIfAborted();
|
|
266
|
+
// Empty EOF only closes our write end. Still await actual process/tree
|
|
267
|
+
// closure, output completion and a successful exit before accepting it.
|
|
268
|
+
if (backend.failure !== undefined ||
|
|
269
|
+
!defaultEofAlreadyClosed(cause, backend.facts()))
|
|
270
|
+
throw cause;
|
|
271
|
+
}
|
|
272
|
+
await backend.closed;
|
|
273
|
+
const facts = backend.facts();
|
|
274
|
+
requireCondition(facts.closed && facts.treeStopped && facts.outputComplete, "process_stop_unconfirmed", "The native npm process tree and output have not actually closed.");
|
|
275
|
+
if (scope.signal.aborted)
|
|
276
|
+
throw new AgentError("maintenance_cancelled", "The native npm process tree closed after a cancellation request.");
|
|
277
|
+
requireCondition(!backend.failure && facts.exitCode === 0, "npm_command_failed", "Native npm did not complete the confirmed operation.", { exitCode: facts.exitCode });
|
|
278
|
+
authorizeCall(() => scope.assertCurrent());
|
|
279
|
+
return output + decoder.end();
|
|
280
|
+
}
|
|
281
|
+
finally {
|
|
282
|
+
if (!registered)
|
|
283
|
+
await backend.dispose();
|
|
284
|
+
const cleanup = await owner.stop();
|
|
285
|
+
requireCondition(cleanup.unconfirmed.length === 0, "process_stop_unconfirmed", "A native maintenance command has unconfirmed process-tree cleanup.");
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
async #planner(mode, cwd, scope, intent, version) {
|
|
289
|
+
const output = await this.#child([
|
|
290
|
+
"--input-type=commonjs",
|
|
291
|
+
"--eval",
|
|
292
|
+
npmPlannerSource,
|
|
293
|
+
this.#npmCli(),
|
|
294
|
+
mode,
|
|
295
|
+
intent?.action ?? "",
|
|
296
|
+
intent?.name ?? "",
|
|
297
|
+
version ?? "",
|
|
298
|
+
String(cap),
|
|
299
|
+
], cwd, scope, mode === "plan" ? 4 * cap + 4096 : 16384);
|
|
300
|
+
let value;
|
|
301
|
+
try {
|
|
302
|
+
value = JSON.parse(output);
|
|
303
|
+
}
|
|
304
|
+
catch {
|
|
305
|
+
/* No producer text or parser details leave this boundary. */
|
|
306
|
+
}
|
|
307
|
+
requireCondition(isPlainRecord(value), "npm_plan_invalid", "Native npm did not return one bounded planning result.");
|
|
308
|
+
if ([
|
|
309
|
+
"npm_planner_unsupported",
|
|
310
|
+
"npm_plan_too_large",
|
|
311
|
+
"npm_plan_failed",
|
|
312
|
+
"npm_planner_changed",
|
|
313
|
+
].includes(value.error))
|
|
314
|
+
throw new AgentError(value.error, value.error === "npm_planner_unsupported"
|
|
315
|
+
? "The selected npm distribution does not provide the supported in-memory planning API."
|
|
316
|
+
: value.error === "npm_plan_too_large"
|
|
317
|
+
? "The native npm manifest or lock exceeds the managed planning byte limit; no generated root file was saved."
|
|
318
|
+
: value.error === "npm_planner_changed"
|
|
319
|
+
? "The selected npm distribution changed during planning; no generated root file was saved."
|
|
320
|
+
: "Native npm could not prepare the dependency plan; protected source and registry details were not exposed.");
|
|
321
|
+
const source = value.source;
|
|
322
|
+
requireCondition(isPlainRecord(source) &&
|
|
323
|
+
Object.keys(source).length === 4 &&
|
|
324
|
+
["npmVersion", "arboristVersion", "configurationVersion"].every((key) => typeof source[key] === "string" &&
|
|
325
|
+
/^\d+\.\d+\.\d+(?:[-+][A-Za-z0-9.-]+)?$/.test(source[key])) &&
|
|
326
|
+
typeof source.revision === "string" &&
|
|
327
|
+
/^[0-9a-f]{64}$/.test(source.revision) &&
|
|
328
|
+
Object.keys(value).every((key) => ["source", "manifest", "lock"].includes(key)) &&
|
|
329
|
+
(mode === "source" ||
|
|
330
|
+
(typeof value.manifest === "string" &&
|
|
331
|
+
typeof value.lock === "string" &&
|
|
332
|
+
Buffer.byteLength(value.manifest) <= cap &&
|
|
333
|
+
Buffer.byteLength(value.lock) <= cap)), "npm_plan_invalid", "Native npm source evidence and generated root records must match their bounded contract.");
|
|
334
|
+
return {
|
|
335
|
+
source: source,
|
|
336
|
+
manifest: mode === "plan" ? value.manifest : null,
|
|
337
|
+
lock: mode === "plan" ? value.lock : null,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
async #files() {
|
|
341
|
+
const manifest = (await textFile(path.join(this.work, "package.json"))), lock = await textFile(path.join(this.work, "package-lock.json"), true), npmrc = await textFile(path.join(this.work, ".npmrc"), true);
|
|
342
|
+
return {
|
|
343
|
+
manifest,
|
|
344
|
+
lock,
|
|
345
|
+
npmrc,
|
|
346
|
+
revision: hash(canonicalJson({
|
|
347
|
+
manifest: hash(manifest),
|
|
348
|
+
lock: hash(lock),
|
|
349
|
+
npmrc: hash(npmrc),
|
|
350
|
+
})),
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
#direct(manifest, name) {
|
|
354
|
+
return ["dependencies", "devDependencies", "optionalDependencies"].some((group) => isPlainRecord(manifest[group]) && Object.hasOwn(manifest[group], name));
|
|
355
|
+
}
|
|
356
|
+
async #installed(name) {
|
|
357
|
+
try {
|
|
358
|
+
const value = JSON.parse((await textFile(path.join(this.work, "node_modules", name, "package.json"))));
|
|
359
|
+
requireCondition(value.name === name && typeof value.version === "string", "npm_install_invalid", "The installed package identity is invalid.");
|
|
360
|
+
return value.version;
|
|
361
|
+
}
|
|
362
|
+
catch (cause) {
|
|
363
|
+
if (cause.code === "ENOENT")
|
|
364
|
+
return null;
|
|
365
|
+
throw cause;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
async #verifiedPackage(intent, candidate) {
|
|
369
|
+
const actual = await this.#files();
|
|
370
|
+
requireCondition(actual.lock !== null &&
|
|
371
|
+
canonicalJson(JSON.parse(actual.manifest), cap) ===
|
|
372
|
+
canonicalJson(JSON.parse(candidate.payload.manifest), cap) &&
|
|
373
|
+
canonicalJson(JSON.parse(actual.lock), cap) ===
|
|
374
|
+
canonicalJson(JSON.parse(candidate.payload.lock), cap), "npm_plan_changed", "Native npm or package lifecycle changed the confirmed dependency plan; the partial disk result requires inspection.");
|
|
375
|
+
const installed = await this.#installed(intent.name), declared = this.#direct(JSON.parse(actual.manifest), intent.name), retained = JSON.parse(actual.lock).packages[`node_modules/${intent.name}`];
|
|
376
|
+
requireCondition(intent.target === "package" &&
|
|
377
|
+
["install", "update", "remove"].includes(intent.action) &&
|
|
378
|
+
(intent.action === "remove"
|
|
379
|
+
? !declared &&
|
|
380
|
+
(isPlainRecord(retained)
|
|
381
|
+
? installed === retained.version
|
|
382
|
+
: installed === null)
|
|
383
|
+
: declared &&
|
|
384
|
+
typeof installed === "string" &&
|
|
385
|
+
installed === candidate.payload.targetVersion), "npm_install_invalid", "The actual installed package does not match the approved target.");
|
|
386
|
+
return {
|
|
387
|
+
snapshot: {
|
|
388
|
+
target: {
|
|
389
|
+
name: intent.name,
|
|
390
|
+
action: intent.action,
|
|
391
|
+
version: intent.action === "remove" ? null : installed,
|
|
392
|
+
},
|
|
393
|
+
manifest: actual.manifest,
|
|
394
|
+
lock: actual.lock,
|
|
395
|
+
},
|
|
396
|
+
installed,
|
|
397
|
+
declared,
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
async #inspectInstalled(name, scope) {
|
|
401
|
+
const manifest = JSON.parse((await textFile(path.join(this.work, "node_modules", name, "package.json"))));
|
|
402
|
+
requireCondition(manifest.name === name &&
|
|
403
|
+
typeof manifest.version === "string" &&
|
|
404
|
+
isPlainRecord(manifest.nexum), "invalid_extension_package", "The installed package must declare its own static extension entry.");
|
|
405
|
+
const permissions = manifest.nexum.permissions;
|
|
406
|
+
requireCondition(Array.isArray(permissions) &&
|
|
407
|
+
permissions.every((value) => typeof value === "string"), "invalid_extension_permissions", "The installed extension must declare its static permissions.");
|
|
408
|
+
const approved = new Map(this.options.extensions.packages.options.policy.approvedPackages());
|
|
409
|
+
approved.set(name, manifest.version);
|
|
410
|
+
const policy = {
|
|
411
|
+
builtins: this.options.extensions.packages.options.policy.builtins,
|
|
412
|
+
approvedPackages: () => approved,
|
|
413
|
+
};
|
|
414
|
+
const resolved = await this.#child([
|
|
415
|
+
"--input-type=module",
|
|
416
|
+
"--eval",
|
|
417
|
+
"process.stdout.write(import.meta.resolve(process.argv[1]));",
|
|
418
|
+
`${name}/nexum`,
|
|
419
|
+
], this.work, scope);
|
|
420
|
+
return await inspectExtensionPackage({
|
|
421
|
+
work: this.work,
|
|
422
|
+
packageName: name,
|
|
423
|
+
policy,
|
|
424
|
+
resolveImport: (specifier) => {
|
|
425
|
+
requireCondition(specifier === `${name}/nexum`, "invalid_extension_source", "Only the installed package's static entry is resolved.");
|
|
426
|
+
return resolved;
|
|
427
|
+
},
|
|
428
|
+
permissions: new Set([
|
|
429
|
+
...this.options.extensions.packages.options.permissions,
|
|
430
|
+
...permissions,
|
|
431
|
+
]),
|
|
432
|
+
signal: scope.signal,
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
async prepare(intent, scope) {
|
|
436
|
+
requireCondition(intent.target === "package" || intent.target === "extension", "maintenance_backend_unavailable", "This backend only prepares npm packages and SDK extensions.");
|
|
437
|
+
const files = await this.#files(), manifest = JSON.parse(files.manifest), declared = this.#direct(manifest, intent.name), installed = await this.#installed(intent.name), enabled = this.options.enablement.get(intent.name), active = this.options.extensions.get(intent.name);
|
|
438
|
+
let oldExtensionRevision = null, oldPermissions = [], oldNamespace = null, oldDeclaration = null;
|
|
439
|
+
if (installed) {
|
|
440
|
+
const metadata = JSON.parse((await textFile(path.join(this.work, "node_modules", intent.name, "package.json"))));
|
|
441
|
+
if (metadata.nexum !== undefined) {
|
|
442
|
+
const descriptor = await this.#inspectInstalled(intent.name, scope);
|
|
443
|
+
oldExtensionRevision = descriptor.revision;
|
|
444
|
+
oldPermissions = descriptor.source.permissions;
|
|
445
|
+
oldNamespace = descriptor.source.origin.namespace;
|
|
446
|
+
oldDeclaration = declaration(descriptor);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
if (intent.method === "packages.install")
|
|
450
|
+
requireCondition(!declared, "package_already_declared", "Use packages.update for an existing direct dependency.");
|
|
451
|
+
else
|
|
452
|
+
requireCondition(declared && installed, "package_not_installed", "The target must be an installed direct dependency.");
|
|
453
|
+
if (intent.target === "extension") {
|
|
454
|
+
const descriptor = await this.#inspectInstalled(intent.name, scope);
|
|
455
|
+
return {
|
|
456
|
+
objectId: enabled?.objectId ?? `npm:${intent.name}`,
|
|
457
|
+
baseRevision: files.revision,
|
|
458
|
+
authStatus: "not_required",
|
|
459
|
+
summary: {
|
|
460
|
+
changeKinds: [
|
|
461
|
+
"extension",
|
|
462
|
+
"capability",
|
|
463
|
+
"script",
|
|
464
|
+
"instruction",
|
|
465
|
+
"docs",
|
|
466
|
+
"skill",
|
|
467
|
+
],
|
|
468
|
+
permissions: descriptor.source.permissions,
|
|
469
|
+
version: descriptor.version,
|
|
470
|
+
namespace: descriptor.source.origin.namespace,
|
|
471
|
+
restartMayBeRequired: false,
|
|
472
|
+
},
|
|
473
|
+
payload: {
|
|
474
|
+
filesRevision: files.revision,
|
|
475
|
+
installedVersion: installed,
|
|
476
|
+
extensionRevision: descriptor.revision,
|
|
477
|
+
enablementRevision: enabled?.revision ?? null,
|
|
478
|
+
enabled: enabled?.enabled ?? false,
|
|
479
|
+
config: enabled?.config ?? {},
|
|
480
|
+
namespace: descriptor.source.origin.namespace,
|
|
481
|
+
contributions: declaration(descriptor),
|
|
482
|
+
currentContributions: oldDeclaration,
|
|
483
|
+
},
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
const reservation = await this.options.reserve?.(Buffer.byteLength(files.manifest) +
|
|
487
|
+
Buffer.byteLength(files.lock ?? "") +
|
|
488
|
+
Buffer.byteLength(files.npmrc ?? "") +
|
|
489
|
+
2 * cap +
|
|
490
|
+
4096);
|
|
491
|
+
let staging;
|
|
492
|
+
try {
|
|
493
|
+
scope.signal.throwIfAborted();
|
|
494
|
+
authorizeCall(() => scope.assertCurrent());
|
|
495
|
+
const stagingBase = privateDirectory(this.root, "state", "maintenance", "npm");
|
|
496
|
+
staging = await mkdtemp(path.join(stagingBase, "plan-"));
|
|
497
|
+
await writeFile(path.join(staging, "package.json"), files.manifest, {
|
|
498
|
+
mode: 0o600,
|
|
499
|
+
});
|
|
500
|
+
if (files.lock !== null)
|
|
501
|
+
await writeFile(path.join(staging, "package-lock.json"), files.lock, {
|
|
502
|
+
mode: 0o600,
|
|
503
|
+
});
|
|
504
|
+
if (files.npmrc !== null)
|
|
505
|
+
await writeFile(path.join(staging, ".npmrc"), files.npmrc, {
|
|
506
|
+
mode: 0o600,
|
|
507
|
+
});
|
|
508
|
+
let version = null;
|
|
509
|
+
if (intent.action !== "remove") {
|
|
510
|
+
const raw = JSON.parse(await this.#command([
|
|
511
|
+
"view",
|
|
512
|
+
`${intent.name}@${intent.version ?? "latest"}`,
|
|
513
|
+
"version",
|
|
514
|
+
"--json",
|
|
515
|
+
], this.work, scope));
|
|
516
|
+
requireCondition(typeof raw === "string" &&
|
|
517
|
+
/^\d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?(?:\+[A-Za-z0-9.-]+)?$/.test(raw), "npm_version_unresolved", "npm did not resolve one exact version.");
|
|
518
|
+
version = raw;
|
|
519
|
+
}
|
|
520
|
+
const planned = await this.#planner("plan", staging, scope, intent, version);
|
|
521
|
+
const nextManifest = planned.manifest, nextLock = planned.lock, lock = JSON.parse(nextLock);
|
|
522
|
+
requireCondition(isPlainRecord(lock.packages), "npm_lock_invalid", "npm must produce an exact package-lock before confirmation.");
|
|
523
|
+
const locked = lock.packages[`node_modules/${intent.name}`];
|
|
524
|
+
if (version)
|
|
525
|
+
requireCondition(locked?.version === version &&
|
|
526
|
+
typeof locked.resolved === "string" &&
|
|
527
|
+
typeof locked.integrity === "string", "npm_lock_invalid", "The confirmed package requires its exact npm source and integrity.");
|
|
528
|
+
// Only this parent saves root records, after the actual producer bytes
|
|
529
|
+
// passed their limits. npm's in-memory planner never saves these files.
|
|
530
|
+
await replace(path.join(staging, "package.json"), nextManifest);
|
|
531
|
+
await replace(path.join(staging, "package-lock.json"), nextLock);
|
|
532
|
+
let extensionRevision = null, permissions = oldPermissions, namespace = oldNamespace;
|
|
533
|
+
let contributions = null;
|
|
534
|
+
if (version) {
|
|
535
|
+
// Fetch the confirmed tree into a private planner; no lifecycle or factory runs here.
|
|
536
|
+
await this.#command([
|
|
537
|
+
"install",
|
|
538
|
+
"--ignore-scripts",
|
|
539
|
+
"--no-save",
|
|
540
|
+
"--package-lock=true",
|
|
541
|
+
"--no-audit",
|
|
542
|
+
"--no-fund",
|
|
543
|
+
], staging, scope);
|
|
544
|
+
const metadata = JSON.parse((await textFile(path.join(staging, "node_modules", intent.name, "package.json"))));
|
|
545
|
+
if (metadata.nexum !== undefined) {
|
|
546
|
+
// Only this candidate is inspected; optional OS-specific direct packages may be absent.
|
|
547
|
+
const approved = new Map([
|
|
548
|
+
[intent.name, metadata.version],
|
|
549
|
+
]);
|
|
550
|
+
const policy = {
|
|
551
|
+
builtins: this.options.extensions.packages.options.policy.builtins,
|
|
552
|
+
approvedPackages: () => approved,
|
|
553
|
+
};
|
|
554
|
+
// A disposable native resolver cannot accumulate temporary ESM cache entries in the Agent.
|
|
555
|
+
const resolved = await this.#child([
|
|
556
|
+
"--input-type=module",
|
|
557
|
+
"--eval",
|
|
558
|
+
"process.stdout.write(import.meta.resolve(process.argv[1]));",
|
|
559
|
+
`${intent.name}/nexum`,
|
|
560
|
+
], staging, scope);
|
|
561
|
+
const declared = metadata.nexum.permissions;
|
|
562
|
+
requireCondition(Array.isArray(declared) &&
|
|
563
|
+
declared.every((item) => typeof item === "string"), "invalid_extension_permissions", "The candidate extension must statically declare its permissions.");
|
|
564
|
+
const descriptor = await inspectExtensionPackage({
|
|
565
|
+
work: staging,
|
|
566
|
+
packageName: intent.name,
|
|
567
|
+
policy,
|
|
568
|
+
resolveImport: (specifier) => {
|
|
569
|
+
requireCondition(specifier === `${intent.name}/nexum`, "invalid_extension_source", "Only the candidate's own static entry is resolved.");
|
|
570
|
+
return resolved;
|
|
571
|
+
},
|
|
572
|
+
permissions: new Set([
|
|
573
|
+
...this.options.extensions.packages.options.permissions,
|
|
574
|
+
...declared,
|
|
575
|
+
]),
|
|
576
|
+
signal: scope.signal,
|
|
577
|
+
});
|
|
578
|
+
extensionRevision = descriptor.revision;
|
|
579
|
+
permissions = descriptor.source.permissions;
|
|
580
|
+
namespace = descriptor.source.origin.namespace;
|
|
581
|
+
contributions = declaration(descriptor);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
const oldPackages = files.lock
|
|
585
|
+
? (JSON.parse(files.lock).packages ?? {})
|
|
586
|
+
: {}, nextPackages = lock.packages;
|
|
587
|
+
const changes = [
|
|
588
|
+
...new Set([...Object.keys(oldPackages), ...Object.keys(nextPackages)]),
|
|
589
|
+
]
|
|
590
|
+
.filter((key) => key &&
|
|
591
|
+
canonicalJson(oldPackages[key] ?? null, cap) !==
|
|
592
|
+
canonicalJson(nextPackages[key] ?? null, cap))
|
|
593
|
+
.map((key) => ({
|
|
594
|
+
name: key.slice(key.lastIndexOf("node_modules/") + 13),
|
|
595
|
+
fromVersion: (oldPackages[key]?.version ?? null),
|
|
596
|
+
toVersion: (nextPackages[key]?.version ??
|
|
597
|
+
null),
|
|
598
|
+
}));
|
|
599
|
+
const registryRaw = (await this.#command(["config", "get", "registry"], this.work, scope)).trim();
|
|
600
|
+
let registryOrigin = null;
|
|
601
|
+
try {
|
|
602
|
+
registryOrigin = new URL(registryRaw).origin;
|
|
603
|
+
}
|
|
604
|
+
catch { }
|
|
605
|
+
const lockChanged = files.lock === null ||
|
|
606
|
+
canonicalJson(JSON.parse(files.lock), cap) !==
|
|
607
|
+
canonicalJson(JSON.parse(nextLock), cap), loaded = this.options.modules
|
|
608
|
+
.loadedPackages()
|
|
609
|
+
.map((item) => ({ name: item.name, version: item.version })), activeExtensions = this.options.extensions.list().map((item) => ({
|
|
610
|
+
name: item.name,
|
|
611
|
+
extensionId: item.extensionId,
|
|
612
|
+
}));
|
|
613
|
+
return {
|
|
614
|
+
objectId: declared ? `npm:${intent.name}` : null,
|
|
615
|
+
baseRevision: files.revision,
|
|
616
|
+
authStatus: "not_required",
|
|
617
|
+
summary: {
|
|
618
|
+
changeKinds: [
|
|
619
|
+
"package",
|
|
620
|
+
"dependency",
|
|
621
|
+
"lockfile",
|
|
622
|
+
...(extensionRevision || oldExtensionRevision
|
|
623
|
+
? [
|
|
624
|
+
"extension",
|
|
625
|
+
"capability",
|
|
626
|
+
"script",
|
|
627
|
+
"instruction",
|
|
628
|
+
"docs",
|
|
629
|
+
"skill",
|
|
630
|
+
]
|
|
631
|
+
: []),
|
|
632
|
+
...(intent.action === "remove" ? ["remove"] : []),
|
|
633
|
+
],
|
|
634
|
+
permissions,
|
|
635
|
+
version,
|
|
636
|
+
namespace,
|
|
637
|
+
restartMayBeRequired: lockChanged && loaded.length > 0,
|
|
638
|
+
},
|
|
639
|
+
payload: {
|
|
640
|
+
filesRevision: files.revision,
|
|
641
|
+
manifest: nextManifest,
|
|
642
|
+
lock: nextLock,
|
|
643
|
+
targetVersion: version,
|
|
644
|
+
installedVersion: installed,
|
|
645
|
+
enabled: enabled?.enabled ?? false,
|
|
646
|
+
enablementRevision: enabled?.revision ?? null,
|
|
647
|
+
extensionConfig: enabled?.config ?? {},
|
|
648
|
+
activeExtensionId: active?.extensionId ?? null,
|
|
649
|
+
oldExtensionRevision,
|
|
650
|
+
extensionRevision,
|
|
651
|
+
registryOrigin,
|
|
652
|
+
npmSource: planned.source,
|
|
653
|
+
dependencyChanges: changes.slice(0, 200),
|
|
654
|
+
dependencyChangesCount: changes.length,
|
|
655
|
+
currentDeclared: declared,
|
|
656
|
+
currentNamespace: oldNamespace,
|
|
657
|
+
currentPermissions: [...oldPermissions],
|
|
658
|
+
lockChanged,
|
|
659
|
+
loaded,
|
|
660
|
+
activeExtensions,
|
|
661
|
+
contributions,
|
|
662
|
+
currentContributions: oldDeclaration,
|
|
663
|
+
},
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
finally {
|
|
667
|
+
try {
|
|
668
|
+
if (staging)
|
|
669
|
+
await rm(staging, { recursive: true, force: true });
|
|
670
|
+
}
|
|
671
|
+
finally {
|
|
672
|
+
reservation?.release();
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
async verify(intent, candidate, scope) {
|
|
677
|
+
scope.signal.throwIfAborted();
|
|
678
|
+
scope.assertCurrent();
|
|
679
|
+
this.options.extensions.assertPackageChangeReady();
|
|
680
|
+
if (intent.target === "package") {
|
|
681
|
+
const producer = await this.#planner("source", this.work, scope);
|
|
682
|
+
requireCondition(canonicalJson(producer.source) ===
|
|
683
|
+
canonicalJson(candidate.payload.npmSource), "maintenance_precondition_changed", "The selected native npm distribution changed after confirmation.");
|
|
684
|
+
const loaded = this.options.modules.loadedPackages();
|
|
685
|
+
requireCondition(Array.isArray(candidate.payload.loaded) &&
|
|
686
|
+
loaded.every((item) => candidate.payload.loaded.some((prior) => prior.name === item.name && prior.version === item.version)) &&
|
|
687
|
+
canonicalJson(this.options.extensions.list().map((item) => ({
|
|
688
|
+
name: item.name,
|
|
689
|
+
extensionId: item.extensionId,
|
|
690
|
+
}))) === canonicalJson(candidate.payload.activeExtensions), "maintenance_precondition_changed", "Package evaluation or active extensions changed after the user reviewed restart impact.");
|
|
691
|
+
}
|
|
692
|
+
const files = await this.#files();
|
|
693
|
+
requireCondition(files.revision === candidate.payload.filesRevision, "maintenance_precondition_changed", "The work manifest, npm lock or configuration changed after confirmation.");
|
|
694
|
+
const enabled = this.options.enablement.get(intent.name);
|
|
695
|
+
requireCondition((enabled?.revision ?? null) === candidate.payload.enablementRevision, "maintenance_precondition_changed", "The extension enablement changed after confirmation.");
|
|
696
|
+
requireCondition((await this.#installed(intent.name)) ===
|
|
697
|
+
candidate.payload.installedVersion, "maintenance_precondition_changed", "The installed package changed after confirmation.");
|
|
698
|
+
if (typeof candidate.payload.oldExtensionRevision === "string") {
|
|
699
|
+
const descriptor = await this.#inspectInstalled(intent.name, scope);
|
|
700
|
+
requireCondition(descriptor.revision === candidate.payload.oldExtensionRevision, "maintenance_precondition_changed", "The installed extension source changed after user confirmation.");
|
|
701
|
+
}
|
|
702
|
+
if (intent.target !== "package") {
|
|
703
|
+
const descriptor = await this.#inspectInstalled(intent.name, scope);
|
|
704
|
+
requireCondition(descriptor.revision === candidate.payload.extensionRevision, "maintenance_precondition_changed", "The extension declarations or source changed after confirmation.");
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
async readDraft(intent, candidate, scope) {
|
|
708
|
+
scope.signal.throwIfAborted();
|
|
709
|
+
scope.assertCurrent();
|
|
710
|
+
return {
|
|
711
|
+
config: {
|
|
712
|
+
name: intent.name,
|
|
713
|
+
version: candidate.summary.version,
|
|
714
|
+
registryOrigin: candidate.payload.registryOrigin ?? null,
|
|
715
|
+
npmSource: candidate.payload.npmSource ?? null,
|
|
716
|
+
namespace: candidate.summary.namespace,
|
|
717
|
+
permissions: [...candidate.summary.permissions],
|
|
718
|
+
dependencyChanges: candidate.payload.dependencyChanges ?? [],
|
|
719
|
+
dependencyChangesCount: candidate.payload.dependencyChangesCount ?? 0,
|
|
720
|
+
contributions: candidate.payload.contributions ?? null,
|
|
721
|
+
restartMayBeRequired: candidate.summary.restartMayBeRequired,
|
|
722
|
+
extensionsToStop: candidate.summary.restartMayBeRequired
|
|
723
|
+
? (candidate.payload.activeExtensions ?? [])
|
|
724
|
+
: [],
|
|
725
|
+
lockRevision: typeof candidate.payload.lock === "string"
|
|
726
|
+
? hash(candidate.payload.lock)
|
|
727
|
+
: null,
|
|
728
|
+
},
|
|
729
|
+
...(candidate.objectId
|
|
730
|
+
? {
|
|
731
|
+
currentConfig: {
|
|
732
|
+
name: intent.name,
|
|
733
|
+
installedVersion: candidate.payload.installedVersion ?? null,
|
|
734
|
+
declared: candidate.payload.currentDeclared ?? true,
|
|
735
|
+
desiredEnabled: candidate.payload.enabled ?? false,
|
|
736
|
+
namespace: candidate.payload.currentNamespace ??
|
|
737
|
+
candidate.summary.namespace,
|
|
738
|
+
permissions: candidate.payload.currentPermissions ?? [
|
|
739
|
+
...candidate.summary.permissions,
|
|
740
|
+
],
|
|
741
|
+
contributions: candidate.payload.currentContributions ?? null,
|
|
742
|
+
},
|
|
743
|
+
}
|
|
744
|
+
: {}),
|
|
745
|
+
authentication: [],
|
|
746
|
+
security: [],
|
|
747
|
+
diagnostics: [],
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
async apply(intent, candidate, scope) {
|
|
751
|
+
requireCondition(!this.#busy && this.#commands.size === 0, "maintenance_busy", "Native npm is already changing this work environment.");
|
|
752
|
+
this.#busy = true;
|
|
753
|
+
let reservation;
|
|
754
|
+
let changed = false;
|
|
755
|
+
let packageRestart = false;
|
|
756
|
+
const stopped = [];
|
|
757
|
+
try {
|
|
758
|
+
return await this.options.modules.withPackageChanges(() => this.options.extensions.withPackageChanges(async () => {
|
|
759
|
+
await this.verify(intent, candidate, scope);
|
|
760
|
+
const desired = candidate.payload.enabled === true, config = (candidate.payload.config ??
|
|
761
|
+
candidate.payload.extensionConfig ??
|
|
762
|
+
{});
|
|
763
|
+
const enablement = await textFile(path.join(this.root, "state", "maintenance", "extensions.json"), true);
|
|
764
|
+
reservation = await this.options.reserve?.(Buffer.byteLength(enablement ?? "") +
|
|
765
|
+
Buffer.byteLength(canonicalJson(config)) +
|
|
766
|
+
4096 +
|
|
767
|
+
(intent.target === "extension"
|
|
768
|
+
? 0
|
|
769
|
+
: cap +
|
|
770
|
+
Buffer.byteLength(candidate.payload.manifest) +
|
|
771
|
+
Buffer.byteLength(candidate.payload.lock)));
|
|
772
|
+
scope.signal.throwIfAborted();
|
|
773
|
+
authorizeCall(() => scope.assertCurrent());
|
|
774
|
+
await this.verify(intent, candidate, scope);
|
|
775
|
+
scope.signal.throwIfAborted();
|
|
776
|
+
authorizeCall(() => scope.assertCurrent());
|
|
777
|
+
if (intent.target === "extension") {
|
|
778
|
+
const enable = intent.action === "enable", before = this.options.extensions.get(intent.name);
|
|
779
|
+
if (enable) {
|
|
780
|
+
await this.options.enablement.set(intent.name, true, config, candidate.payload.enablementRevision, { reserved: Boolean(reservation) });
|
|
781
|
+
changed = true;
|
|
782
|
+
await this.options.extensions.enable(intent.name, config);
|
|
783
|
+
}
|
|
784
|
+
else {
|
|
785
|
+
if (before) {
|
|
786
|
+
await this.options.extensions.disable(intent.name);
|
|
787
|
+
changed = true;
|
|
788
|
+
}
|
|
789
|
+
await this.options.enablement.set(intent.name, false, config, candidate.payload.enablementRevision, { reserved: Boolean(reservation) });
|
|
790
|
+
changed = true;
|
|
791
|
+
}
|
|
792
|
+
const after = this.options.extensions.get(intent.name);
|
|
793
|
+
return {
|
|
794
|
+
changed: desired !== enable || Boolean(before) !== Boolean(after),
|
|
795
|
+
restartRequired: false,
|
|
796
|
+
package: {
|
|
797
|
+
name: intent.name,
|
|
798
|
+
declared: true,
|
|
799
|
+
installedVersion: await this.#installed(intent.name),
|
|
800
|
+
},
|
|
801
|
+
extension: {
|
|
802
|
+
namespace: candidate.payload.namespace,
|
|
803
|
+
desiredEnabled: enable,
|
|
804
|
+
state: after?.status ?? "disabled",
|
|
805
|
+
activeVersion: after
|
|
806
|
+
? after.origin.version
|
|
807
|
+
: null,
|
|
808
|
+
},
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
if (candidate.payload.lockChanged === false) {
|
|
812
|
+
const actual = await this.#verifiedPackage(intent, candidate);
|
|
813
|
+
scope.signal.throwIfAborted();
|
|
814
|
+
authorizeCall(() => scope.assertCurrent());
|
|
815
|
+
await this.options.refreshPackages(actual.snapshot);
|
|
816
|
+
const before = this.options.extensions.get(intent.name);
|
|
817
|
+
return {
|
|
818
|
+
changed: false,
|
|
819
|
+
restartRequired: false,
|
|
820
|
+
package: {
|
|
821
|
+
name: intent.name,
|
|
822
|
+
declared: actual.declared,
|
|
823
|
+
installedVersion: actual.installed,
|
|
824
|
+
},
|
|
825
|
+
extension: before
|
|
826
|
+
? {
|
|
827
|
+
namespace: before.origin
|
|
828
|
+
.namespace,
|
|
829
|
+
desiredEnabled: desired,
|
|
830
|
+
state: before.status,
|
|
831
|
+
activeVersion: before.status === "enabled"
|
|
832
|
+
? before.origin.version
|
|
833
|
+
: null,
|
|
834
|
+
}
|
|
835
|
+
: null,
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
packageRestart =
|
|
839
|
+
candidate.payload.lockChanged === true &&
|
|
840
|
+
this.options.modules.loadedPackages().length > 0;
|
|
841
|
+
const affected = packageRestart
|
|
842
|
+
? this.options.extensions.list()
|
|
843
|
+
: this.options.extensions
|
|
844
|
+
.list()
|
|
845
|
+
.filter((item) => item.name === intent.name);
|
|
846
|
+
for (const entry of affected) {
|
|
847
|
+
const report = await this.options.extensions.disable(entry.name);
|
|
848
|
+
changed = true;
|
|
849
|
+
stopped.push({
|
|
850
|
+
name: entry.name,
|
|
851
|
+
namespace: entry.origin.namespace,
|
|
852
|
+
desiredEnabled: this.options.enablement.get(entry.name)?.enabled ??
|
|
853
|
+
false,
|
|
854
|
+
state: report.status,
|
|
855
|
+
activeVersion: null,
|
|
856
|
+
});
|
|
857
|
+
requireCondition(report.status === "disabled", "extension_cleanup_unconfirmed", "An affected extension must actually close before npm changes its dependency tree.");
|
|
858
|
+
}
|
|
859
|
+
const npmOptions = [
|
|
860
|
+
"--no-save",
|
|
861
|
+
"--package-lock=true",
|
|
862
|
+
"--no-audit",
|
|
863
|
+
"--no-fund",
|
|
864
|
+
];
|
|
865
|
+
if (packageRestart)
|
|
866
|
+
this.options.modules.markPackageRestartRequired();
|
|
867
|
+
if (intent.action === "remove") {
|
|
868
|
+
// Keep the original direct edge visible to native npm's actual
|
|
869
|
+
// tree diff. Removing it from the files first can hide the old
|
|
870
|
+
// package from npm's workspace-disabled reconciliation filter.
|
|
871
|
+
changed = true;
|
|
872
|
+
await this.#command(["uninstall", intent.name, ...npmOptions], this.work, scope);
|
|
873
|
+
requireCondition((await this.#files()).revision === candidate.baseRevision, "npm_plan_changed", "Native npm changed the original root records before the approved plan could be saved.");
|
|
874
|
+
}
|
|
875
|
+
await replace(path.join(this.work, "package.json"), candidate.payload.manifest);
|
|
876
|
+
changed = true;
|
|
877
|
+
await replace(path.join(this.work, "package-lock.json"), candidate.payload.lock);
|
|
878
|
+
scope.signal.throwIfAborted();
|
|
879
|
+
authorizeCall(() => scope.assertCurrent());
|
|
880
|
+
if (intent.action !== "remove")
|
|
881
|
+
await this.#command(["install", ...npmOptions], this.work, scope);
|
|
882
|
+
const { snapshot, installed, declared } = await this.#verifiedPackage(intent, candidate);
|
|
883
|
+
scope.signal.throwIfAborted();
|
|
884
|
+
authorizeCall(() => scope.assertCurrent());
|
|
885
|
+
await this.options.refreshPackages(snapshot);
|
|
886
|
+
let restartRequired = packageRestart, extension = null;
|
|
887
|
+
if (desired) {
|
|
888
|
+
const state = this.options.enablement.get(intent.name);
|
|
889
|
+
if (intent.action === "remove")
|
|
890
|
+
await this.options.enablement.set(intent.name, false, config, state?.revision ?? null, { reserved: Boolean(reservation) });
|
|
891
|
+
else if (!restartRequired) {
|
|
892
|
+
try {
|
|
893
|
+
await this.options.extensions.enable(intent.name, config);
|
|
894
|
+
}
|
|
895
|
+
catch (cause) {
|
|
896
|
+
if (isAgentError(cause) &&
|
|
897
|
+
cause.code === "module_restart_required")
|
|
898
|
+
restartRequired = true;
|
|
899
|
+
else
|
|
900
|
+
throw cause;
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
const active = this.options.extensions.get(intent.name);
|
|
904
|
+
extension = {
|
|
905
|
+
namespace: active
|
|
906
|
+
? active.origin.namespace
|
|
907
|
+
: (candidate.summary.namespace ?? intent.name),
|
|
908
|
+
desiredEnabled: intent.action !== "remove",
|
|
909
|
+
state: active?.status ??
|
|
910
|
+
(restartRequired ? "restart_required" : "disabled"),
|
|
911
|
+
activeVersion: active
|
|
912
|
+
? active.origin.version
|
|
913
|
+
: null,
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
return {
|
|
917
|
+
changed: true,
|
|
918
|
+
restartRequired,
|
|
919
|
+
package: {
|
|
920
|
+
name: intent.name,
|
|
921
|
+
declared,
|
|
922
|
+
installedVersion: installed,
|
|
923
|
+
},
|
|
924
|
+
extension,
|
|
925
|
+
...(stopped.length ? { affectedExtensions: stopped } : {}),
|
|
926
|
+
};
|
|
927
|
+
}));
|
|
928
|
+
}
|
|
929
|
+
catch (cause) {
|
|
930
|
+
const partial = {
|
|
931
|
+
changed,
|
|
932
|
+
restartRequired: packageRestart ||
|
|
933
|
+
(isAgentError(cause) && cause.code === "module_restart_required"),
|
|
934
|
+
...(stopped.length ? { affectedExtensions: stopped } : {}),
|
|
935
|
+
};
|
|
936
|
+
try {
|
|
937
|
+
const declared = this.#direct(JSON.parse((await textFile(path.join(this.work, "package.json")))), intent.name);
|
|
938
|
+
partial.package = {
|
|
939
|
+
name: intent.name,
|
|
940
|
+
declared,
|
|
941
|
+
installedVersion: await this.#installed(intent.name),
|
|
942
|
+
};
|
|
943
|
+
}
|
|
944
|
+
catch { }
|
|
945
|
+
const active = this.options.extensions.get(intent.name), desired = this.options.enablement.get(intent.name)?.enabled ?? false;
|
|
946
|
+
if (active || desired)
|
|
947
|
+
partial.extension = {
|
|
948
|
+
namespace: active
|
|
949
|
+
? active.origin.namespace
|
|
950
|
+
: (candidate.summary.namespace ?? intent.name),
|
|
951
|
+
desiredEnabled: desired,
|
|
952
|
+
state: active?.status ?? "disabled",
|
|
953
|
+
activeVersion: active?.status === "enabled"
|
|
954
|
+
? active.origin.version
|
|
955
|
+
: null,
|
|
956
|
+
};
|
|
957
|
+
throw new AgentError(isAgentError(cause) ? cause.code : "maintenance_execution_failed", "The approved npm or extension change did not complete.", { partial: JSON.parse(canonicalJson(partial)) });
|
|
958
|
+
}
|
|
959
|
+
finally {
|
|
960
|
+
reservation?.release();
|
|
961
|
+
this.#busy = false;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
/** MCP/API remain real application ports; absent configuration support fails closed. */
|
|
966
|
+
export class MaintenanceBackends {
|
|
967
|
+
npm;
|
|
968
|
+
configuration;
|
|
969
|
+
constructor(npm, configuration) {
|
|
970
|
+
this.npm = npm;
|
|
971
|
+
this.configuration = configuration;
|
|
972
|
+
}
|
|
973
|
+
#for(intent) {
|
|
974
|
+
if (intent.target === "package" || intent.target === "extension")
|
|
975
|
+
return this.npm;
|
|
976
|
+
requireCondition(this.configuration, "maintenance_backend_unavailable", "This Agent has no approved MCP/API configuration backend.");
|
|
977
|
+
return this.configuration;
|
|
978
|
+
}
|
|
979
|
+
prepare(intent, scope) {
|
|
980
|
+
return this.#for(intent).prepare(intent, scope);
|
|
981
|
+
}
|
|
982
|
+
verify(intent, candidate, scope) {
|
|
983
|
+
return this.#for(intent).verify(intent, candidate, scope);
|
|
984
|
+
}
|
|
985
|
+
apply(intent, candidate, scope) {
|
|
986
|
+
return this.#for(intent).apply(intent, candidate, scope);
|
|
987
|
+
}
|
|
988
|
+
async readDraft(intent, candidate, scope) {
|
|
989
|
+
const backend = this.#for(intent);
|
|
990
|
+
if (backend.readDraft)
|
|
991
|
+
return await backend.readDraft(intent, candidate, scope);
|
|
992
|
+
requireCondition(intent.target === "package" || intent.target === "extension", "maintenance_draft_unavailable", "A safe configuration draft provider is required.");
|
|
993
|
+
return { config: {}, authentication: [], security: [], diagnostics: [] };
|
|
994
|
+
}
|
|
995
|
+
async prepareDraft(intent, candidate, input, scope) {
|
|
996
|
+
const backend = this.#for(intent);
|
|
997
|
+
if (backend.prepareDraft)
|
|
998
|
+
return await backend.prepareDraft(intent, candidate, input, scope);
|
|
999
|
+
requireCondition((input.credentials?.length ?? 0) === 0, "maintenance_credentials_unavailable", "This backend cannot accept protected credential edits.");
|
|
1000
|
+
return await backend.prepare(intent, scope);
|
|
1001
|
+
}
|
|
1002
|
+
async startOAuth(intent, candidate, binding, scope) {
|
|
1003
|
+
const backend = this.#for(intent);
|
|
1004
|
+
requireCondition(backend.startOAuth, "maintenance_oauth_unavailable", "No trusted OAuth provider is configured.");
|
|
1005
|
+
return await backend.startOAuth(intent, candidate, binding, scope);
|
|
1006
|
+
}
|
|
1007
|
+
async completeOAuth(intent, candidate, input, scope) {
|
|
1008
|
+
const backend = this.#for(intent);
|
|
1009
|
+
requireCondition(backend.completeOAuth, "maintenance_oauth_unavailable", "No trusted OAuth completion provider is configured.");
|
|
1010
|
+
return await backend.completeOAuth(intent, candidate, input, scope);
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
//# sourceMappingURL=npm.js.map
|