@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,959 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { lstatSync, realpathSync } from "node:fs";
|
|
3
|
+
import { writeFile } from "node:fs/promises";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { AgentError, isAgentError, requireCondition, } from "../contracts/errors.js";
|
|
6
|
+
import { authorizeCall, callerKey } from "../contracts/caller.js";
|
|
7
|
+
import { canonicalJson, isPlainRecord } from "../contracts/json.js";
|
|
8
|
+
import { privateDirectory } from "../runtime/paths.js";
|
|
9
|
+
import { maintenanceText } from "./files.js";
|
|
10
|
+
export const maintenanceOrigin = Object.freeze({
|
|
11
|
+
type: "platform",
|
|
12
|
+
owner: "nexum",
|
|
13
|
+
namespace: "maintenance",
|
|
14
|
+
version: "1",
|
|
15
|
+
revision: "1",
|
|
16
|
+
});
|
|
17
|
+
export const maintenanceMethods = [
|
|
18
|
+
"packages.install",
|
|
19
|
+
"packages.update",
|
|
20
|
+
"packages.remove",
|
|
21
|
+
"extensions.enable",
|
|
22
|
+
"extensions.disable",
|
|
23
|
+
"mcp.add",
|
|
24
|
+
"mcp.update",
|
|
25
|
+
"mcp.remove",
|
|
26
|
+
"api.import",
|
|
27
|
+
"api.update",
|
|
28
|
+
"api.remove",
|
|
29
|
+
];
|
|
30
|
+
export function unavailableMaintenance(current) {
|
|
31
|
+
const unavailable = () => {
|
|
32
|
+
current();
|
|
33
|
+
throw new AgentError("maintenance_unavailable", "The trusted maintenance authority is not configured.");
|
|
34
|
+
};
|
|
35
|
+
return Object.freeze({
|
|
36
|
+
packages: Object.freeze({
|
|
37
|
+
install: unavailable,
|
|
38
|
+
update: unavailable,
|
|
39
|
+
remove: unavailable,
|
|
40
|
+
}),
|
|
41
|
+
extensions: Object.freeze({ enable: unavailable, disable: unavailable }),
|
|
42
|
+
mcp: Object.freeze({
|
|
43
|
+
add: unavailable,
|
|
44
|
+
update: unavailable,
|
|
45
|
+
remove: unavailable,
|
|
46
|
+
}),
|
|
47
|
+
api: Object.freeze({
|
|
48
|
+
import: unavailable,
|
|
49
|
+
update: unavailable,
|
|
50
|
+
remove: unavailable,
|
|
51
|
+
}),
|
|
52
|
+
get: unavailable,
|
|
53
|
+
list: unavailable,
|
|
54
|
+
cancel: unavailable,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const requestPattern = /^maint_req_[A-Za-z0-9_-]{1,128}$/;
|
|
58
|
+
const hashPattern = /^[0-9a-f]{64}$/;
|
|
59
|
+
const names = /^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/;
|
|
60
|
+
function bounded(value, max = 256) {
|
|
61
|
+
return (typeof value === "string" &&
|
|
62
|
+
value.length > 0 &&
|
|
63
|
+
Buffer.byteLength(value) <= max &&
|
|
64
|
+
!/[\0\r\n]/.test(value));
|
|
65
|
+
}
|
|
66
|
+
function json(value, cap = 65536) {
|
|
67
|
+
return JSON.parse(canonicalJson(value, cap));
|
|
68
|
+
}
|
|
69
|
+
const changeKinds = new Set([
|
|
70
|
+
"package",
|
|
71
|
+
"dependency",
|
|
72
|
+
"lockfile",
|
|
73
|
+
"extension",
|
|
74
|
+
"capability",
|
|
75
|
+
"script",
|
|
76
|
+
"instruction",
|
|
77
|
+
"docs",
|
|
78
|
+
"skill",
|
|
79
|
+
"connection",
|
|
80
|
+
"authentication",
|
|
81
|
+
"api",
|
|
82
|
+
"permissions",
|
|
83
|
+
"configuration",
|
|
84
|
+
"remove",
|
|
85
|
+
]);
|
|
86
|
+
function summary(value, title) {
|
|
87
|
+
requireCondition(value &&
|
|
88
|
+
Array.isArray(value.changeKinds) &&
|
|
89
|
+
value.changeKinds.length <= 32 &&
|
|
90
|
+
value.changeKinds.every((item) => changeKinds.has(item)) &&
|
|
91
|
+
Array.isArray(value.permissions) &&
|
|
92
|
+
value.permissions.length <= 256 &&
|
|
93
|
+
value.permissions.every((item) => bounded(item, 256)) &&
|
|
94
|
+
(value.version === null || bounded(value.version, 256)) &&
|
|
95
|
+
(value.namespace === null ||
|
|
96
|
+
(typeof value.namespace === "string" &&
|
|
97
|
+
/^[A-Za-z_$][A-Za-z0-9_$]{0,63}$/.test(value.namespace))) &&
|
|
98
|
+
typeof value.restartMayBeRequired === "boolean", "invalid_maintenance_summary", "The maintenance summary must contain only bounded declared metadata.");
|
|
99
|
+
return {
|
|
100
|
+
title,
|
|
101
|
+
changeKinds: [...new Set(value.changeKinds)],
|
|
102
|
+
permissions: [...new Set(value.permissions)],
|
|
103
|
+
version: value.version,
|
|
104
|
+
namespace: value.namespace,
|
|
105
|
+
restartMayBeRequired: value.restartMayBeRequired,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function publicResult(value) {
|
|
109
|
+
const result = json(value);
|
|
110
|
+
requireCondition(typeof result.changed === "boolean" &&
|
|
111
|
+
typeof result.restartRequired === "boolean" &&
|
|
112
|
+
Object.keys(result).every((key) => [
|
|
113
|
+
"changed",
|
|
114
|
+
"restartRequired",
|
|
115
|
+
"package",
|
|
116
|
+
"extension",
|
|
117
|
+
"connection",
|
|
118
|
+
"api",
|
|
119
|
+
"affectedExtensions",
|
|
120
|
+
].includes(key)), "invalid_maintenance_result", "Maintenance results contain only actual package and capability facts.");
|
|
121
|
+
for (const key of ["package", "extension", "connection", "api"]) {
|
|
122
|
+
const item = result[key];
|
|
123
|
+
if (item === undefined || item === null)
|
|
124
|
+
continue;
|
|
125
|
+
const fields = key === "package"
|
|
126
|
+
? ["name", "declared", "installedVersion"]
|
|
127
|
+
: key === "extension"
|
|
128
|
+
? ["namespace", "desiredEnabled", "state", "activeVersion"]
|
|
129
|
+
: ["name", "state", "revision"];
|
|
130
|
+
requireCondition(isPlainRecord(item) &&
|
|
131
|
+
Object.keys(item).every((field) => fields.includes(field)) &&
|
|
132
|
+
Object.values(item).every((field) => field === null || typeof field === "boolean" || bounded(field, 256)), "invalid_maintenance_result", "Maintenance detail fields must be bounded facts, not configuration or credentials.");
|
|
133
|
+
const facts = item;
|
|
134
|
+
const valid = key === "package"
|
|
135
|
+
? bounded(facts.name) &&
|
|
136
|
+
typeof facts.declared === "boolean" &&
|
|
137
|
+
(facts.installedVersion === null ||
|
|
138
|
+
bounded(facts.installedVersion, 256))
|
|
139
|
+
: key === "extension"
|
|
140
|
+
? bounded(facts.namespace) &&
|
|
141
|
+
typeof facts.desiredEnabled === "boolean" &&
|
|
142
|
+
bounded(facts.state, 64) &&
|
|
143
|
+
(facts.activeVersion === null || bounded(facts.activeVersion, 256))
|
|
144
|
+
: bounded(facts.name) &&
|
|
145
|
+
bounded(facts.state, 64) &&
|
|
146
|
+
(facts.revision === null || bounded(facts.revision, 256));
|
|
147
|
+
requireCondition(valid, "invalid_maintenance_result", "The observed maintenance facts must use their declared field types.");
|
|
148
|
+
}
|
|
149
|
+
if (result.affectedExtensions !== undefined)
|
|
150
|
+
requireCondition(Array.isArray(result.affectedExtensions) &&
|
|
151
|
+
result.affectedExtensions.length <= 1000 &&
|
|
152
|
+
result.affectedExtensions.every((item) => isPlainRecord(item) &&
|
|
153
|
+
Object.keys(item).every((key) => [
|
|
154
|
+
"name",
|
|
155
|
+
"namespace",
|
|
156
|
+
"desiredEnabled",
|
|
157
|
+
"state",
|
|
158
|
+
"activeVersion",
|
|
159
|
+
].includes(key)) &&
|
|
160
|
+
bounded(item.name) &&
|
|
161
|
+
bounded(item.namespace) &&
|
|
162
|
+
typeof item.desiredEnabled === "boolean" &&
|
|
163
|
+
bounded(item.state, 64) &&
|
|
164
|
+
(item.activeVersion === null || bounded(item.activeVersion, 256))), "invalid_maintenance_result", "Affected extensions must contain bounded actual lifecycle facts.");
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
function draftView(value) {
|
|
168
|
+
const draft = json(value, 512 * 1024);
|
|
169
|
+
requireCondition(Object.keys(draft).every((key) => [
|
|
170
|
+
"config",
|
|
171
|
+
"currentConfig",
|
|
172
|
+
"authentication",
|
|
173
|
+
"security",
|
|
174
|
+
"diagnostics",
|
|
175
|
+
].includes(key)) &&
|
|
176
|
+
isPlainRecord(draft.config) &&
|
|
177
|
+
Array.isArray(draft.authentication) &&
|
|
178
|
+
draft.authentication.length <= 100 &&
|
|
179
|
+
Array.isArray(draft.security) &&
|
|
180
|
+
Array.isArray(draft.diagnostics) &&
|
|
181
|
+
draft.diagnostics.length <= 100 &&
|
|
182
|
+
draft.diagnostics.every((item) => typeof item === "string" &&
|
|
183
|
+
Buffer.byteLength(item) <= 2048 &&
|
|
184
|
+
!item.includes("\0")), "invalid_maintenance_draft", "A safe draft needs bounded configuration and authentication metadata.");
|
|
185
|
+
canonicalJson(draft.config, 256 * 1024);
|
|
186
|
+
if (draft.currentConfig !== undefined) {
|
|
187
|
+
requireCondition(isPlainRecord(draft.currentConfig), "invalid_maintenance_draft", "The current configuration must be an object.");
|
|
188
|
+
canonicalJson(draft.currentConfig, 256 * 1024);
|
|
189
|
+
}
|
|
190
|
+
for (const scheme of draft.authentication) {
|
|
191
|
+
requireCondition(isPlainRecord(scheme) &&
|
|
192
|
+
Object.keys(scheme).every((key) => [
|
|
193
|
+
"id",
|
|
194
|
+
"type",
|
|
195
|
+
"location",
|
|
196
|
+
"name",
|
|
197
|
+
"flow",
|
|
198
|
+
"oauth",
|
|
199
|
+
"fields",
|
|
200
|
+
].includes(key)) &&
|
|
201
|
+
bounded(scheme.id) &&
|
|
202
|
+
bounded(scheme.type) &&
|
|
203
|
+
typeof scheme.oauth === "boolean" &&
|
|
204
|
+
Array.isArray(scheme.fields) &&
|
|
205
|
+
scheme.fields.length <= 100, "invalid_maintenance_draft", "Authentication schemes expose field metadata, never credential values.");
|
|
206
|
+
requireCondition([scheme.location, scheme.name, scheme.flow].every((field) => field === undefined || bounded(field, 256)), "invalid_maintenance_draft", "Authentication metadata fields must be bounded strings.");
|
|
207
|
+
for (const field of scheme.fields)
|
|
208
|
+
requireCondition(isPlainRecord(field) &&
|
|
209
|
+
Object.keys(field).every((key) => ["key", "label", "configured", "required"].includes(key)) &&
|
|
210
|
+
bounded(field.key) &&
|
|
211
|
+
bounded(field.label, 256) &&
|
|
212
|
+
typeof field.configured === "boolean" &&
|
|
213
|
+
typeof field.required === "boolean", "invalid_maintenance_draft", "Credential fields must expose only their configured state.");
|
|
214
|
+
}
|
|
215
|
+
requireCondition(draft.security.length <= 100 &&
|
|
216
|
+
draft.security.every((group) => Array.isArray(group) &&
|
|
217
|
+
group.length <= 100 &&
|
|
218
|
+
group.every((item) => bounded(item, 256))), "invalid_maintenance_draft", "Authentication alternatives require bounded scheme names.");
|
|
219
|
+
return draft;
|
|
220
|
+
}
|
|
221
|
+
function requestId(value) {
|
|
222
|
+
requireCondition(typeof value === "string" && requestPattern.test(value), "invalid_maintenance_request", "Use the existing maintenance request identity.");
|
|
223
|
+
}
|
|
224
|
+
function target(method) {
|
|
225
|
+
return method.startsWith("packages.")
|
|
226
|
+
? "package"
|
|
227
|
+
: method.startsWith("extensions.")
|
|
228
|
+
? "extension"
|
|
229
|
+
: method.startsWith("mcp.")
|
|
230
|
+
? "mcp"
|
|
231
|
+
: "api";
|
|
232
|
+
}
|
|
233
|
+
export function maintenanceIntent(method, value, pwd) {
|
|
234
|
+
requireCondition(maintenanceMethods.includes(method) && isPlainRecord(value), "invalid_maintenance_input", "Use a declared maintenance method and its object arguments.");
|
|
235
|
+
const data = json(value), kind = target(method), action = method.split(".")[1];
|
|
236
|
+
const allowed = kind === "package" && action !== "remove"
|
|
237
|
+
? ["name", "version"]
|
|
238
|
+
: kind === "mcp" && action !== "remove"
|
|
239
|
+
? ["name", "config"]
|
|
240
|
+
: kind === "api" && action !== "remove"
|
|
241
|
+
? ["name", "url", "path", "config"]
|
|
242
|
+
: ["name"];
|
|
243
|
+
requireCondition(Object.keys(data).every((key) => allowed.includes(key)) &&
|
|
244
|
+
bounded(data.name, 214), "invalid_maintenance_input", "Maintenance arguments contain unsupported fields or an invalid name.");
|
|
245
|
+
const name = data.name;
|
|
246
|
+
requireCondition(kind === "package" || kind === "extension"
|
|
247
|
+
? names.test(name) &&
|
|
248
|
+
!name.split("/").some((part) => part === "." || part === "..")
|
|
249
|
+
: /^[A-Za-z_$][A-Za-z0-9_$-]{0,127}$/.test(name), "invalid_maintenance_name", "Use the actual npm package name or one connection/API name.");
|
|
250
|
+
if (data.version !== undefined)
|
|
251
|
+
requireCondition(bounded(data.version, 256) &&
|
|
252
|
+
/^[A-Za-z0-9][A-Za-z0-9.+_-]*$/.test(data.version), "invalid_maintenance_version", "Use one npm version or registry tag, not a command or arbitrary source.");
|
|
253
|
+
if (data.config !== undefined)
|
|
254
|
+
requireCondition(isPlainRecord(data.config), "invalid_maintenance_config", "A maintenance configuration must be an object.");
|
|
255
|
+
if (kind === "mcp" && action !== "remove")
|
|
256
|
+
requireCondition(data.config !== undefined, "invalid_maintenance_config", "The connection change needs its full proposed configuration.");
|
|
257
|
+
if (kind === "api" && action !== "remove") {
|
|
258
|
+
requireCondition(Number(data.url !== undefined) + Number(data.path !== undefined) <=
|
|
259
|
+
(action === "import" ? 1 : 1) &&
|
|
260
|
+
(action !== "import" ||
|
|
261
|
+
data.url !== undefined ||
|
|
262
|
+
data.path !== undefined), "invalid_maintenance_source", "API import requires exactly one URL or local specification path; update accepts at most one.");
|
|
263
|
+
if (data.url !== undefined) {
|
|
264
|
+
requireCondition(bounded(data.url, 8192), "invalid_maintenance_source", "The specification URL is invalid.");
|
|
265
|
+
let url;
|
|
266
|
+
try {
|
|
267
|
+
url = new URL(data.url);
|
|
268
|
+
}
|
|
269
|
+
catch {
|
|
270
|
+
throw new AgentError("invalid_maintenance_source", "The specification URL is invalid.");
|
|
271
|
+
}
|
|
272
|
+
requireCondition(["http:", "https:"].includes(url.protocol) &&
|
|
273
|
+
!url.username &&
|
|
274
|
+
!url.password &&
|
|
275
|
+
!url.hash, "invalid_maintenance_source", "Use an HTTP(S) specification endpoint without embedded credentials.");
|
|
276
|
+
}
|
|
277
|
+
if (data.path !== undefined) {
|
|
278
|
+
requireCondition(bounded(data.path, 8192), "invalid_maintenance_source", "The local specification path is invalid.");
|
|
279
|
+
data.path = path.resolve(pwd, data.path);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return Object.freeze({
|
|
283
|
+
...data,
|
|
284
|
+
method,
|
|
285
|
+
target: kind,
|
|
286
|
+
action,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
/** Owns requests and approved independent execution, never a second task scheduler. */
|
|
290
|
+
export class MaintenanceRequests {
|
|
291
|
+
store;
|
|
292
|
+
authority;
|
|
293
|
+
backend;
|
|
294
|
+
options;
|
|
295
|
+
#busy = false;
|
|
296
|
+
constructor(store, authority, backend, options) {
|
|
297
|
+
this.store = store;
|
|
298
|
+
this.authority = authority;
|
|
299
|
+
this.backend = backend;
|
|
300
|
+
this.options = options;
|
|
301
|
+
const origin = new URL(options.approvalOrigin);
|
|
302
|
+
requireCondition(["https:", "http:"].includes(origin.protocol) &&
|
|
303
|
+
!origin.username &&
|
|
304
|
+
!origin.password, "invalid_maintenance_options", "Use the configured Cloud origin for approval links.");
|
|
305
|
+
requireCondition(options.maxCandidateBytes === undefined ||
|
|
306
|
+
(Number.isSafeInteger(options.maxCandidateBytes) &&
|
|
307
|
+
options.maxCandidateBytes >= 1024 &&
|
|
308
|
+
options.maxCandidateBytes <= 64 * 1024 * 1024), "invalid_maintenance_options", "Private maintenance candidates need a positive bounded storage limit.");
|
|
309
|
+
}
|
|
310
|
+
#now() {
|
|
311
|
+
return this.options.now?.() ?? Date.now();
|
|
312
|
+
}
|
|
313
|
+
#pwd(scope) {
|
|
314
|
+
if (scope.pwd !== undefined) {
|
|
315
|
+
requireCondition(path.isAbsolute(scope.pwd), "invalid_maintenance_context", "Maintenance paths need the actual working context directory.");
|
|
316
|
+
return scope.pwd;
|
|
317
|
+
}
|
|
318
|
+
if (scope.runId) {
|
|
319
|
+
const run = this.store.getRun(scope.runId);
|
|
320
|
+
requireCondition(run.callerKey === callerKey(scope.caller), "invalid_maintenance_context", "The maintenance working directory belongs to another caller.");
|
|
321
|
+
return this.store.getWork(run.contextId).pwd;
|
|
322
|
+
}
|
|
323
|
+
return path.join(this.store.root, "work");
|
|
324
|
+
}
|
|
325
|
+
#identity(scope) {
|
|
326
|
+
scope.signal.throwIfAborted();
|
|
327
|
+
authorizeCall(() => scope.assertCurrent());
|
|
328
|
+
requireCondition(bounded(scope.caller.principalId) &&
|
|
329
|
+
bounded(scope.caller.clientId) &&
|
|
330
|
+
bounded(scope.caller.grantId), "maintenance_authority_unavailable", "Maintenance needs the current verified caller and Cloud Grant.");
|
|
331
|
+
return Object.freeze({
|
|
332
|
+
...this.store.binding,
|
|
333
|
+
callerKey: callerKey(scope.caller),
|
|
334
|
+
principalId: scope.caller.principalId,
|
|
335
|
+
clientId: scope.caller.clientId,
|
|
336
|
+
grantId: scope.caller.grantId,
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
#current(scope, access, value = "requests") {
|
|
340
|
+
scope.signal.throwIfAborted();
|
|
341
|
+
authorizeCall(() => scope.assertCurrent());
|
|
342
|
+
authorizeCall(() => scope.authorize({ type: "maintenance", value, access }));
|
|
343
|
+
}
|
|
344
|
+
#view(view) {
|
|
345
|
+
const value = json(view, 262144);
|
|
346
|
+
requestId(value.requestId);
|
|
347
|
+
requireCondition(Object.keys(value).every((key) => [
|
|
348
|
+
"requestId",
|
|
349
|
+
"agentId",
|
|
350
|
+
"profile",
|
|
351
|
+
"target",
|
|
352
|
+
"action",
|
|
353
|
+
"name",
|
|
354
|
+
"status",
|
|
355
|
+
"revision",
|
|
356
|
+
"candidateRevision",
|
|
357
|
+
"objectId",
|
|
358
|
+
"baseRevision",
|
|
359
|
+
"summary",
|
|
360
|
+
"authStatus",
|
|
361
|
+
"createdAt",
|
|
362
|
+
"expiresAt",
|
|
363
|
+
"approvalUrl",
|
|
364
|
+
"agentName",
|
|
365
|
+
"source",
|
|
366
|
+
"decidedAt",
|
|
367
|
+
"decision",
|
|
368
|
+
"execution",
|
|
369
|
+
].includes(key)) &&
|
|
370
|
+
value.agentId === this.store.binding.agentId &&
|
|
371
|
+
value.profile === this.store.binding.profile &&
|
|
372
|
+
["pending", "approved", "denied", "cancelled", "expired"].includes(value.status) &&
|
|
373
|
+
Number.isSafeInteger(value.revision) &&
|
|
374
|
+
value.revision > 0 &&
|
|
375
|
+
hashPattern.test(value.candidateRevision) &&
|
|
376
|
+
Number.isFinite(Date.parse(value.expiresAt)), "invalid_maintenance_authority", "The Cloud response does not match this Agent/profile request.");
|
|
377
|
+
const method = `${value.target === "package" ? "packages" : value.target === "extension" ? "extensions" : value.target}.${value.action}`;
|
|
378
|
+
requireCondition(maintenanceMethods.includes(method) &&
|
|
379
|
+
bounded(value.name, 214) &&
|
|
380
|
+
(value.objectId === null || bounded(value.objectId, 256)) &&
|
|
381
|
+
(value.baseRevision === null || bounded(value.baseRevision, 256)) &&
|
|
382
|
+
["not_required", "required", "ready", "failed"].includes(value.authStatus) &&
|
|
383
|
+
Number.isFinite(Date.parse(value.createdAt)), "invalid_maintenance_authority", "The authority returned an invalid maintenance target or preparation state.");
|
|
384
|
+
if (value.decision)
|
|
385
|
+
requireCondition(Object.keys(value.decision).every((key) => [
|
|
386
|
+
"status",
|
|
387
|
+
"revision",
|
|
388
|
+
"candidateRevision",
|
|
389
|
+
"decidedAt",
|
|
390
|
+
"decisionId",
|
|
391
|
+
].includes(key)) &&
|
|
392
|
+
["approved", "denied"].includes(value.decision.status) &&
|
|
393
|
+
Number.isSafeInteger(value.decision.revision) &&
|
|
394
|
+
value.decision.revision > 0 &&
|
|
395
|
+
hashPattern.test(value.decision.candidateRevision) &&
|
|
396
|
+
bounded(value.decision.decisionId) &&
|
|
397
|
+
Number.isFinite(Date.parse(value.decision.decidedAt)), "invalid_maintenance_authority", "The user decision has an invalid fixed revision.");
|
|
398
|
+
if (value.execution) {
|
|
399
|
+
requireCondition(Object.keys(value.execution).every((key) => ["operationId", "startedAt", "status", "result", "error"].includes(key)) &&
|
|
400
|
+
/^op_[0-9a-f]{32}$/.test(value.execution.operationId) &&
|
|
401
|
+
Number.isFinite(Date.parse(value.execution.startedAt)) &&
|
|
402
|
+
(value.execution.status === undefined ||
|
|
403
|
+
bounded(value.execution.status, 64)), "invalid_maintenance_authority", "The authority returned an invalid execution identity.");
|
|
404
|
+
if (value.execution.result)
|
|
405
|
+
publicResult(value.execution.result);
|
|
406
|
+
if (value.execution.error)
|
|
407
|
+
requireCondition(isPlainRecord(value.execution.error) &&
|
|
408
|
+
Object.keys(value.execution.error).every((key) => key === "code" || key === "message") &&
|
|
409
|
+
typeof value.execution.error.code === "string" &&
|
|
410
|
+
/^[A-Za-z][A-Za-z0-9_:-]{0,127}$/.test(value.execution.error.code) &&
|
|
411
|
+
typeof value.execution.error.message === "string" &&
|
|
412
|
+
Buffer.byteLength(value.execution.error.message) <= 4096 &&
|
|
413
|
+
!value.execution.error.message.includes("\0"), "invalid_maintenance_authority", "The execution error must contain only its safe code and message.");
|
|
414
|
+
}
|
|
415
|
+
const safeSummary = summary(value.summary, `${value.target === "package" ? "packages" : value.target === "extension" ? "extensions" : value.target}.${value.action}: ${value.name}`);
|
|
416
|
+
requireCondition((value.agentName === undefined || bounded(value.agentName, 1024)) &&
|
|
417
|
+
(value.source === undefined ||
|
|
418
|
+
(isPlainRecord(value.source) &&
|
|
419
|
+
Object.keys(value.source).every((key) => key === "name") &&
|
|
420
|
+
(value.source.name === undefined ||
|
|
421
|
+
bounded(value.source.name, 1024)))), "invalid_maintenance_authority", "The authority source labels must contain only their verified display name.");
|
|
422
|
+
if (value.approvalUrl !== undefined) {
|
|
423
|
+
let url;
|
|
424
|
+
try {
|
|
425
|
+
url = new URL(value.approvalUrl);
|
|
426
|
+
}
|
|
427
|
+
catch {
|
|
428
|
+
throw new AgentError("invalid_maintenance_authority", "The authority returned an invalid approval location.");
|
|
429
|
+
}
|
|
430
|
+
const origin = new URL(this.options.approvalOrigin);
|
|
431
|
+
requireCondition(url.origin === origin.origin &&
|
|
432
|
+
url.pathname === `/authorize/maintenance/${value.requestId}` &&
|
|
433
|
+
!url.search &&
|
|
434
|
+
!url.hash &&
|
|
435
|
+
!url.username &&
|
|
436
|
+
!url.password, "invalid_maintenance_authority", "The authority returned an invalid approval location.");
|
|
437
|
+
}
|
|
438
|
+
return { ...value, summary: safeSummary };
|
|
439
|
+
}
|
|
440
|
+
#filename(revision) {
|
|
441
|
+
requireCondition(hashPattern.test(revision), "invalid_maintenance_revision", "Use an existing fixed candidate revision.");
|
|
442
|
+
return path.join(this.store.root, "state", "maintenance", "candidates", `${revision}.json`);
|
|
443
|
+
}
|
|
444
|
+
#candidateDirectoryExists() {
|
|
445
|
+
let directory = this.store.root;
|
|
446
|
+
requireCondition(realpathSync.native(directory) === directory, "storage_path_escape", "The candidate profile root cannot redirect.");
|
|
447
|
+
for (const part of ["state", "maintenance", "candidates"]) {
|
|
448
|
+
directory = path.join(directory, part);
|
|
449
|
+
const info = lstatSync(directory, { throwIfNoEntry: false });
|
|
450
|
+
if (!info)
|
|
451
|
+
return false;
|
|
452
|
+
requireCondition(info.isDirectory() &&
|
|
453
|
+
!info.isSymbolicLink() &&
|
|
454
|
+
realpathSync.native(directory) === directory, "storage_path_escape", "Private candidate directories cannot redirect outside this profile.");
|
|
455
|
+
}
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
async #save(candidate) {
|
|
459
|
+
const body = canonicalJson(candidate, this.options.maxCandidateBytes ?? 64 * 1024 * 1024), revision = createHash("sha256").update(body).digest("hex"), file = this.#filename(revision);
|
|
460
|
+
const existing = this.#candidateDirectoryExists()
|
|
461
|
+
? await maintenanceText(file, this.options.maxCandidateBytes ?? 64 * 1024 * 1024, true)
|
|
462
|
+
: null;
|
|
463
|
+
if (existing !== null) {
|
|
464
|
+
requireCondition(this.#candidateDirectoryExists() && existing === body, "maintenance_candidate_conflict", "A fixed maintenance candidate no longer matches its revision.");
|
|
465
|
+
return revision;
|
|
466
|
+
}
|
|
467
|
+
const reservation = await this.options.reserve?.(Buffer.byteLength(body) + 1024);
|
|
468
|
+
try {
|
|
469
|
+
privateDirectory(this.store.root, "state", "maintenance", "candidates");
|
|
470
|
+
await writeFile(file, body, { flag: "wx", mode: 0o600 });
|
|
471
|
+
}
|
|
472
|
+
catch (cause) {
|
|
473
|
+
if (cause.code !== "EEXIST")
|
|
474
|
+
throw cause;
|
|
475
|
+
requireCondition((await maintenanceText(file, this.options.maxCandidateBytes ?? 64 * 1024 * 1024)) === body, "maintenance_candidate_conflict", "A fixed maintenance candidate no longer matches its revision.");
|
|
476
|
+
}
|
|
477
|
+
finally {
|
|
478
|
+
reservation?.release();
|
|
479
|
+
}
|
|
480
|
+
return revision;
|
|
481
|
+
}
|
|
482
|
+
async #load(view, identity) {
|
|
483
|
+
privateDirectory(this.store.root, "state", "maintenance", "candidates");
|
|
484
|
+
const body = (await maintenanceText(this.#filename(view.candidateRevision), this.options.maxCandidateBytes ?? 64 * 1024 * 1024));
|
|
485
|
+
requireCondition(createHash("sha256").update(body).digest("hex") ===
|
|
486
|
+
view.candidateRevision, "maintenance_candidate_conflict", "The approved candidate bytes changed.");
|
|
487
|
+
const candidate = JSON.parse(body);
|
|
488
|
+
requireCondition(canonicalJson(candidate.identity) === canonicalJson(identity) &&
|
|
489
|
+
candidate.intent.target === view.target &&
|
|
490
|
+
candidate.intent.action === view.action &&
|
|
491
|
+
candidate.intent.name === view.name &&
|
|
492
|
+
candidate.prepared.objectId === view.objectId &&
|
|
493
|
+
candidate.prepared.baseRevision === view.baseRevision, "maintenance_candidate_conflict", "The approved target or caller binding differs from this candidate.");
|
|
494
|
+
return candidate;
|
|
495
|
+
}
|
|
496
|
+
async #create(method, input, scope) {
|
|
497
|
+
const identity = this.#identity(scope);
|
|
498
|
+
requireCondition(bounded(scope.operationId, 256), "invalid_maintenance_context", "Request creation needs its actual SDK operation identity.");
|
|
499
|
+
requireCondition(bounded(scope.runId, 256), "invalid_maintenance_context", "Request creation needs its actual originating run identity.");
|
|
500
|
+
const intent = maintenanceIntent(method, input, this.#pwd(scope));
|
|
501
|
+
this.#current(scope, method, intent.name);
|
|
502
|
+
const prepared = json(await this.backend.prepare(intent, scope), this.options.maxCandidateBytes ?? 64 * 1024 * 1024);
|
|
503
|
+
requireCondition(isPlainRecord(prepared.payload) &&
|
|
504
|
+
["not_required", "required", "ready", "failed"].includes(prepared.authStatus) &&
|
|
505
|
+
(prepared.objectId === null || bounded(prepared.objectId, 256)) &&
|
|
506
|
+
(prepared.baseRevision === null || bounded(prepared.baseRevision, 256)), "invalid_maintenance_candidate", "The prepared maintenance candidate needs exact bounded identity and authentication state.");
|
|
507
|
+
this.#current(scope, method, intent.name);
|
|
508
|
+
const candidate = {
|
|
509
|
+
identity,
|
|
510
|
+
originRunId: scope.runId,
|
|
511
|
+
intent,
|
|
512
|
+
prepared,
|
|
513
|
+
}, candidateRevision = await this.#save(candidate);
|
|
514
|
+
const safeSummary = summary(prepared.summary, `${method}: ${intent.name}`);
|
|
515
|
+
const view = this.#view(await this.authority.create(identity, {
|
|
516
|
+
requestKey: scope.operationId,
|
|
517
|
+
originRunId: scope.runId,
|
|
518
|
+
target: intent.target,
|
|
519
|
+
action: intent.action,
|
|
520
|
+
name: intent.name,
|
|
521
|
+
objectId: prepared.objectId,
|
|
522
|
+
baseRevision: prepared.baseRevision,
|
|
523
|
+
candidateRevision,
|
|
524
|
+
summary: safeSummary,
|
|
525
|
+
authStatus: prepared.authStatus,
|
|
526
|
+
}, scope.signal));
|
|
527
|
+
this.#current(scope, method, intent.name);
|
|
528
|
+
requireCondition(view.candidateRevision === candidateRevision, "maintenance_candidate_conflict", "Request creation did not retain this exact prepared candidate.");
|
|
529
|
+
requireCondition(view.status !== "pending" || typeof view.approvalUrl === "string", "invalid_maintenance_authority", "A pending request needs its real user confirmation link.");
|
|
530
|
+
return {
|
|
531
|
+
requestId: view.requestId,
|
|
532
|
+
status: view.status,
|
|
533
|
+
...(view.approvalUrl ? { approvalUrl: view.approvalUrl } : {}),
|
|
534
|
+
expiresAt: view.expiresAt,
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
bind(scope) {
|
|
538
|
+
const create = (method) => (input) => this.#create(method, input, scope);
|
|
539
|
+
return Object.freeze({
|
|
540
|
+
packages: Object.freeze({
|
|
541
|
+
install: create("packages.install"),
|
|
542
|
+
update: create("packages.update"),
|
|
543
|
+
remove: create("packages.remove"),
|
|
544
|
+
}),
|
|
545
|
+
extensions: Object.freeze({
|
|
546
|
+
enable: create("extensions.enable"),
|
|
547
|
+
disable: create("extensions.disable"),
|
|
548
|
+
}),
|
|
549
|
+
mcp: Object.freeze({
|
|
550
|
+
add: create("mcp.add"),
|
|
551
|
+
update: create("mcp.update"),
|
|
552
|
+
remove: create("mcp.remove"),
|
|
553
|
+
}),
|
|
554
|
+
api: Object.freeze({
|
|
555
|
+
import: create("api.import"),
|
|
556
|
+
update: create("api.update"),
|
|
557
|
+
remove: create("api.remove"),
|
|
558
|
+
}),
|
|
559
|
+
get: (id) => this.get(id, scope),
|
|
560
|
+
list: (input = {}) => this.list(input, scope),
|
|
561
|
+
cancel: (id) => this.cancel(id, scope),
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
async get(id, scope) {
|
|
565
|
+
requestId(id);
|
|
566
|
+
const identity = this.#identity(scope);
|
|
567
|
+
this.#current(scope, "get", id);
|
|
568
|
+
const view = this.#view(await this.authority.get(identity, id, scope.signal));
|
|
569
|
+
this.#current(scope, "get", id);
|
|
570
|
+
const delivered = await this.#withExecution(view, identity);
|
|
571
|
+
this.#current(scope, "get", id);
|
|
572
|
+
return delivered;
|
|
573
|
+
}
|
|
574
|
+
async list(input, scope) {
|
|
575
|
+
const identity = this.#identity(scope), options = json(input);
|
|
576
|
+
this.#current(scope, "list");
|
|
577
|
+
requireCondition(Boolean(isPlainRecord(options)) &&
|
|
578
|
+
Object.keys(options).every((key) => ["target", "name", "status", "limit", "cursor"].includes(key)) &&
|
|
579
|
+
(options.name === undefined || bounded(options.name, 214)) &&
|
|
580
|
+
(options.cursor === undefined || bounded(options.cursor, 4096)) &&
|
|
581
|
+
(options.limit === undefined ||
|
|
582
|
+
(Number.isSafeInteger(options.limit) &&
|
|
583
|
+
options.limit >= 1 &&
|
|
584
|
+
options.limit <= 100)) &&
|
|
585
|
+
(options.target === undefined ||
|
|
586
|
+
["package", "extension", "mcp", "api"].includes(options.target)) &&
|
|
587
|
+
(options.status === undefined ||
|
|
588
|
+
["pending", "approved", "denied", "cancelled", "expired"].includes(options.status)), "invalid_maintenance_filter", "Use bounded maintenance filters.");
|
|
589
|
+
const page = await this.authority.list(identity, options, scope.signal);
|
|
590
|
+
this.#current(scope, "list");
|
|
591
|
+
requireCondition(Array.isArray(page.requests) &&
|
|
592
|
+
page.requests.length <= (options.limit ?? 100) &&
|
|
593
|
+
(page.nextCursor === undefined ||
|
|
594
|
+
page.nextCursor === null ||
|
|
595
|
+
bounded(page.nextCursor, 4096)), "invalid_maintenance_authority", "The request page exceeds its requested bound.");
|
|
596
|
+
const delivered = {
|
|
597
|
+
requests: await Promise.all(page.requests.map((view) => this.#withExecution(this.#view(view), identity))),
|
|
598
|
+
nextCursor: page.nextCursor ?? null,
|
|
599
|
+
};
|
|
600
|
+
this.#current(scope, "list");
|
|
601
|
+
return delivered;
|
|
602
|
+
}
|
|
603
|
+
async cancel(id, scope) {
|
|
604
|
+
requestId(id);
|
|
605
|
+
const identity = this.#identity(scope);
|
|
606
|
+
this.#current(scope, "cancel", id);
|
|
607
|
+
const view = this.#view(await this.authority.cancel(identity, id, scope.signal));
|
|
608
|
+
this.#current(scope, "cancel", id);
|
|
609
|
+
const delivered = await this.#withExecution(view, identity);
|
|
610
|
+
this.#current(scope, "cancel", id);
|
|
611
|
+
return delivered;
|
|
612
|
+
}
|
|
613
|
+
#candidateView(candidate, revision) {
|
|
614
|
+
return {
|
|
615
|
+
target: candidate.intent.target,
|
|
616
|
+
action: candidate.intent.action,
|
|
617
|
+
name: candidate.intent.name,
|
|
618
|
+
objectId: candidate.prepared.objectId,
|
|
619
|
+
baseRevision: candidate.prepared.baseRevision,
|
|
620
|
+
candidateRevision: revision,
|
|
621
|
+
summary: summary(candidate.prepared.summary, `${candidate.intent.method}: ${candidate.intent.name}`),
|
|
622
|
+
authStatus: candidate.prepared.authStatus,
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
/** Browser/Core-only controlled projection. Full candidate and secret values stay private. */
|
|
626
|
+
async readDraft(id, scope) {
|
|
627
|
+
const identity = this.#identity(scope), view = this.#view(await this.authority.get(identity, id, scope.signal)), candidate = await this.#load(view, identity);
|
|
628
|
+
this.#current(scope, "get", id);
|
|
629
|
+
const draft = this.backend.readDraft
|
|
630
|
+
? await this.backend.readDraft(candidate.intent, candidate.prepared, scope)
|
|
631
|
+
: candidate.intent.target === "package" ||
|
|
632
|
+
candidate.intent.target === "extension"
|
|
633
|
+
? { config: {}, authentication: [], security: [], diagnostics: [] }
|
|
634
|
+
: undefined;
|
|
635
|
+
requireCondition(draft, "maintenance_draft_unavailable", "The configuration backend cannot provide a safe editable draft.");
|
|
636
|
+
this.#current(scope, "get", id);
|
|
637
|
+
const actual = await this.#withExecution(view, identity);
|
|
638
|
+
return {
|
|
639
|
+
draft: draftView(draft),
|
|
640
|
+
...(actual.execution ? { execution: actual.execution } : {}),
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
async prepareDraft(id, input, scope) {
|
|
644
|
+
requestId(id);
|
|
645
|
+
const identity = this.#identity(scope), view = this.#view(await this.authority.get(identity, id, scope.signal));
|
|
646
|
+
requireCondition(view.status === "pending" &&
|
|
647
|
+
view.revision === input.revision &&
|
|
648
|
+
Date.parse(view.expiresAt) > this.#now(), "maintenance_revision_conflict", "Only the current pending revision can be edited.");
|
|
649
|
+
const original = await this.#load(view, identity);
|
|
650
|
+
this.#current(scope, original.intent.method, original.intent.name);
|
|
651
|
+
const edit = json(input, 512 * 1024);
|
|
652
|
+
requireCondition(Object.keys(edit).every((key) => ["revision", "version", "config", "credentials"].includes(key)), "invalid_maintenance_draft", "The draft cannot change operation or target identity.");
|
|
653
|
+
if (edit.credentials !== undefined)
|
|
654
|
+
requireCondition(Array.isArray(edit.credentials) &&
|
|
655
|
+
edit.credentials.length <= 100 &&
|
|
656
|
+
new Set(edit.credentials.map((field) => field.key)).size ===
|
|
657
|
+
edit.credentials.length &&
|
|
658
|
+
edit.credentials.every((field) => isPlainRecord(field) &&
|
|
659
|
+
Object.keys(field).every((key) => ["key", "operation", "value"].includes(key)) &&
|
|
660
|
+
bounded(field.key) &&
|
|
661
|
+
typeof field.operation === "string" &&
|
|
662
|
+
["keep", "replace", "clear"].includes(field.operation) &&
|
|
663
|
+
(field.operation === "replace"
|
|
664
|
+
? typeof field.value === "string" &&
|
|
665
|
+
field.value.length > 0 &&
|
|
666
|
+
Buffer.byteLength(field.value) <= 32768 &&
|
|
667
|
+
!field.value.includes("\0") &&
|
|
668
|
+
!/^[*• ]+$/.test(field.value)
|
|
669
|
+
: field.value === undefined)), "invalid_maintenance_credentials", "Use explicit bounded credential keep, replace or clear operations.");
|
|
670
|
+
const base = { name: original.intent.name };
|
|
671
|
+
for (const key of ["version", "url", "path", "config"])
|
|
672
|
+
if (original.intent[key] !== undefined)
|
|
673
|
+
base[key] = original.intent[key];
|
|
674
|
+
if (edit.version !== undefined)
|
|
675
|
+
base.version = edit.version;
|
|
676
|
+
if (edit.config !== undefined)
|
|
677
|
+
base.config = edit.config;
|
|
678
|
+
const intent = maintenanceIntent(original.intent.method, base, path.join(this.store.root, "work"));
|
|
679
|
+
const prepared = this.backend.prepareDraft
|
|
680
|
+
? await this.backend.prepareDraft(intent, original.prepared, edit, scope)
|
|
681
|
+
: (edit.credentials?.length ?? 0) > 0
|
|
682
|
+
? undefined
|
|
683
|
+
: await this.backend.prepare(intent, scope);
|
|
684
|
+
requireCondition(prepared, "maintenance_credentials_unavailable", "This backend cannot accept protected credential changes.");
|
|
685
|
+
requireCondition(prepared.objectId === original.prepared.objectId &&
|
|
686
|
+
prepared.baseRevision === original.prepared.baseRevision, "maintenance_precondition_changed", "The target changed while the user edited this request.");
|
|
687
|
+
const candidate = {
|
|
688
|
+
...original,
|
|
689
|
+
intent,
|
|
690
|
+
prepared: json(prepared, this.options.maxCandidateBytes ?? 64 * 1024 * 1024),
|
|
691
|
+
}, revision = await this.#save(candidate);
|
|
692
|
+
const draft = this.backend.readDraft
|
|
693
|
+
? await this.backend.readDraft(intent, prepared, scope)
|
|
694
|
+
: { config: {}, authentication: [], security: [], diagnostics: [] };
|
|
695
|
+
this.#current(scope, intent.method, intent.name);
|
|
696
|
+
return {
|
|
697
|
+
candidate: this.#candidateView(candidate, revision),
|
|
698
|
+
draft: draftView(draft),
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
async verify(id, scope) {
|
|
702
|
+
requestId(id);
|
|
703
|
+
const identity = this.#identity(scope), view = this.#view(await this.authority.get(identity, id, scope.signal)), candidate = await this.#load(view, identity);
|
|
704
|
+
this.#current(scope, candidate.intent.method, candidate.intent.name);
|
|
705
|
+
await this.backend.verify(candidate.intent, candidate.prepared, scope);
|
|
706
|
+
this.#current(scope, candidate.intent.method, candidate.intent.name);
|
|
707
|
+
return this.#candidateView(candidate, view.candidateRevision);
|
|
708
|
+
}
|
|
709
|
+
async startOAuth(id, revision, binding, scope) {
|
|
710
|
+
requestId(id);
|
|
711
|
+
const identity = this.#identity(scope), view = this.#view(await this.authority.get(identity, id, scope.signal));
|
|
712
|
+
requireCondition(view.status === "pending" &&
|
|
713
|
+
view.revision === revision &&
|
|
714
|
+
view.candidateRevision === binding.candidateRevision &&
|
|
715
|
+
Date.parse(view.expiresAt) > this.#now(), "maintenance_revision_conflict", "OAuth must use the current pending candidate revision.");
|
|
716
|
+
requireCondition(this.backend.startOAuth, "maintenance_oauth_unavailable", "No trusted OAuth provider is configured for this maintenance backend.");
|
|
717
|
+
requireCondition(bounded(binding.state, 4096) &&
|
|
718
|
+
bounded(binding.schemeId) &&
|
|
719
|
+
hashPattern.test(binding.candidateRevision), "invalid_maintenance_oauth", "OAuth requires the fixed scheme, candidate and one authorization state.");
|
|
720
|
+
let callback;
|
|
721
|
+
try {
|
|
722
|
+
callback = new URL(binding.callbackUrl);
|
|
723
|
+
}
|
|
724
|
+
catch {
|
|
725
|
+
throw new AgentError("invalid_maintenance_oauth", "The trusted platform OAuth callback is invalid.");
|
|
726
|
+
}
|
|
727
|
+
requireCondition(callback.origin === new URL(this.options.approvalOrigin).origin &&
|
|
728
|
+
callback.pathname === "/api/console/maintenance/oauth/callback" &&
|
|
729
|
+
!callback.username &&
|
|
730
|
+
!callback.password &&
|
|
731
|
+
!callback.hash &&
|
|
732
|
+
!callback.search, "invalid_maintenance_oauth", "OAuth must return to the configured platform callback.");
|
|
733
|
+
const candidate = await this.#load(view, identity);
|
|
734
|
+
this.#current(scope, candidate.intent.method, candidate.intent.name);
|
|
735
|
+
const launch = await this.backend.startOAuth(candidate.intent, candidate.prepared, json(binding), {
|
|
736
|
+
...scope,
|
|
737
|
+
requestId: id,
|
|
738
|
+
revision,
|
|
739
|
+
assertCurrent: () => scope.assertCurrent(),
|
|
740
|
+
authorize: (target) => scope.authorize(target),
|
|
741
|
+
});
|
|
742
|
+
this.#current(scope, candidate.intent.method, candidate.intent.name);
|
|
743
|
+
const delivered = json(launch, 65536);
|
|
744
|
+
requireCondition(Object.keys(delivered).every((key) => [
|
|
745
|
+
"authorizationUrl",
|
|
746
|
+
"verificationUri",
|
|
747
|
+
"userCode",
|
|
748
|
+
"issuer",
|
|
749
|
+
"requireIssuer",
|
|
750
|
+
].includes(key)) &&
|
|
751
|
+
bounded(delivered.issuer, 8192) &&
|
|
752
|
+
typeof delivered.requireIssuer === "boolean" &&
|
|
753
|
+
(bounded(delivered.authorizationUrl, 32768) ||
|
|
754
|
+
(bounded(delivered.verificationUri, 32768) &&
|
|
755
|
+
bounded(delivered.userCode, 1024))), "invalid_maintenance_oauth", "An interactive OAuth launch needs its real authorization URL or device instructions.");
|
|
756
|
+
for (const value of [
|
|
757
|
+
delivered.issuer,
|
|
758
|
+
delivered.authorizationUrl,
|
|
759
|
+
delivered.verificationUri,
|
|
760
|
+
])
|
|
761
|
+
if (value !== undefined) {
|
|
762
|
+
let url;
|
|
763
|
+
try {
|
|
764
|
+
url = new URL(value);
|
|
765
|
+
}
|
|
766
|
+
catch {
|
|
767
|
+
throw new AgentError("invalid_maintenance_oauth", "The OAuth provider returned an invalid URL.");
|
|
768
|
+
}
|
|
769
|
+
requireCondition(["https:", "http:"].includes(url.protocol) &&
|
|
770
|
+
!url.username &&
|
|
771
|
+
!url.password, "invalid_maintenance_oauth", "OAuth provider URLs must use configured HTTP(S) endpoints.");
|
|
772
|
+
}
|
|
773
|
+
return delivered;
|
|
774
|
+
}
|
|
775
|
+
async completeOAuth(id, revision, input, scope) {
|
|
776
|
+
requestId(id);
|
|
777
|
+
const identity = this.#identity(scope), view = this.#view(await this.authority.get(identity, id, scope.signal));
|
|
778
|
+
requireCondition(view.status === "pending" &&
|
|
779
|
+
view.revision === revision &&
|
|
780
|
+
view.candidateRevision === input.candidateRevision &&
|
|
781
|
+
Date.parse(view.expiresAt) > this.#now(), "maintenance_revision_conflict", "OAuth completion must use the original current pending candidate.");
|
|
782
|
+
requireCondition(this.backend.completeOAuth && this.backend.readDraft, "maintenance_oauth_unavailable", "No trusted OAuth completion provider is configured.");
|
|
783
|
+
requireCondition(bounded(input.schemeId) &&
|
|
784
|
+
hashPattern.test(input.candidateRevision) &&
|
|
785
|
+
isPlainRecord(input.callback) &&
|
|
786
|
+
Object.keys(input.callback).every((key) => ["state", "code", "error", "issuer"].includes(key)) &&
|
|
787
|
+
bounded(input.callback.state, 4096) &&
|
|
788
|
+
(input.callback.code === undefined ||
|
|
789
|
+
bounded(input.callback.code, 65536)) &&
|
|
790
|
+
(input.callback.error === undefined ||
|
|
791
|
+
bounded(input.callback.error, 256)) &&
|
|
792
|
+
(input.callback.issuer === undefined ||
|
|
793
|
+
bounded(input.callback.issuer, 8192)), "invalid_maintenance_oauth", "OAuth completion requires the original bounded callback fields.");
|
|
794
|
+
const original = await this.#load(view, identity);
|
|
795
|
+
this.#current(scope, original.intent.method, original.intent.name);
|
|
796
|
+
const prepared = await this.backend.completeOAuth(original.intent, original.prepared, json(input, 262144), {
|
|
797
|
+
...scope,
|
|
798
|
+
requestId: id,
|
|
799
|
+
revision,
|
|
800
|
+
assertCurrent: () => scope.assertCurrent(),
|
|
801
|
+
authorize: (target) => scope.authorize(target),
|
|
802
|
+
});
|
|
803
|
+
requireCondition(prepared.objectId === original.prepared.objectId &&
|
|
804
|
+
prepared.baseRevision === original.prepared.baseRevision, "maintenance_precondition_changed", "OAuth cannot change this maintenance target identity.");
|
|
805
|
+
const candidate = {
|
|
806
|
+
...original,
|
|
807
|
+
prepared: json(prepared, this.options.maxCandidateBytes ?? 64 * 1024 * 1024),
|
|
808
|
+
}, candidateRevision = await this.#save(candidate), draft = await this.backend.readDraft(original.intent, prepared, scope);
|
|
809
|
+
this.#current(scope, original.intent.method, original.intent.name);
|
|
810
|
+
return {
|
|
811
|
+
candidate: this.#candidateView(candidate, candidateRevision),
|
|
812
|
+
draft: draftView(draft),
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
async #withExecution(view, identity) {
|
|
816
|
+
const operation = this.store.getMaintenanceOperation(view.requestId);
|
|
817
|
+
if (!operation)
|
|
818
|
+
return view;
|
|
819
|
+
requireCondition(operation.callerKey === identity.callerKey &&
|
|
820
|
+
operation.approvedRevision === view.candidateRevision, "maintenance_execution_conflict", "The existing execution belongs to another approved binding.");
|
|
821
|
+
let result;
|
|
822
|
+
if (operation.endedAt !== null && operation.detailRef) {
|
|
823
|
+
try {
|
|
824
|
+
privateDirectory(this.store.root, "state", "maintenance", view.requestId);
|
|
825
|
+
const body = JSON.parse((await maintenanceText(path.join(this.store.root, operation.detailRef), 65536)));
|
|
826
|
+
if (body.operationId === operation.operationId &&
|
|
827
|
+
isPlainRecord(body.result))
|
|
828
|
+
result = publicResult(body.result);
|
|
829
|
+
}
|
|
830
|
+
catch { }
|
|
831
|
+
}
|
|
832
|
+
const error = operation.error &&
|
|
833
|
+
typeof operation.error.code === "string" &&
|
|
834
|
+
bounded(operation.error.code, 256) &&
|
|
835
|
+
typeof operation.error.message === "string" &&
|
|
836
|
+
Buffer.byteLength(operation.error.message) <= 4096 &&
|
|
837
|
+
!operation.error.message.includes("\0")
|
|
838
|
+
? { code: operation.error.code, message: operation.error.message }
|
|
839
|
+
: undefined;
|
|
840
|
+
return {
|
|
841
|
+
...view,
|
|
842
|
+
execution: {
|
|
843
|
+
operationId: operation.operationId,
|
|
844
|
+
status: operation.status,
|
|
845
|
+
startedAt: new Date(operation.createdAt).toISOString(),
|
|
846
|
+
...(result ? { result } : {}),
|
|
847
|
+
...(error ? { error } : {}),
|
|
848
|
+
},
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
/** Called by the independent Core lifecycle after a real Browser decision. */
|
|
852
|
+
async execute(id, scope) {
|
|
853
|
+
requestId(id);
|
|
854
|
+
const identity = this.#identity(scope);
|
|
855
|
+
const view = this.#view(await this.authority.decision(identity, id, scope.signal));
|
|
856
|
+
const method = `${view.target === "package" ? "packages" : view.target === "extension" ? "extensions" : view.target}.${view.action}`;
|
|
857
|
+
this.#current(scope, method, view.name);
|
|
858
|
+
const existing = this.store.getMaintenanceOperation(id);
|
|
859
|
+
if (existing) {
|
|
860
|
+
const delivered = await this.#withExecution(view, identity);
|
|
861
|
+
this.#current(scope, method, view.name);
|
|
862
|
+
return delivered;
|
|
863
|
+
}
|
|
864
|
+
requireCondition(!this.#busy, "maintenance_busy", "Another maintenance change is using this Agent work environment.");
|
|
865
|
+
requireCondition(view.status === "approved" &&
|
|
866
|
+
view.decision?.status === "approved" &&
|
|
867
|
+
view.decision.revision === view.revision &&
|
|
868
|
+
view.decision.candidateRevision === view.candidateRevision &&
|
|
869
|
+
Date.parse(view.expiresAt) > this.#now(), "maintenance_not_approved", "Only the current, unexpired user-confirmed revision can begin execution.");
|
|
870
|
+
this.#busy = true;
|
|
871
|
+
let executionReservation;
|
|
872
|
+
try {
|
|
873
|
+
const candidate = await this.#load(view, identity);
|
|
874
|
+
await this.backend.verify(candidate.intent, candidate.prepared, scope);
|
|
875
|
+
this.#current(scope, candidate.intent.method, candidate.intent.name);
|
|
876
|
+
// Keep bounded result/diagnostic space until actual native work and its
|
|
877
|
+
// final SQLite settlement finish; quota pressure cannot strand an accepted change.
|
|
878
|
+
executionReservation = await this.options.reserve?.(80 * 1024 + 1024);
|
|
879
|
+
this.#current(scope, candidate.intent.method, candidate.intent.name);
|
|
880
|
+
const detailRef = `state/maintenance/${id}/execution.json`;
|
|
881
|
+
const started = this.store.startMaintenanceOperation({
|
|
882
|
+
requestId: id,
|
|
883
|
+
approvedRevision: view.candidateRevision,
|
|
884
|
+
callerKey: identity.callerKey,
|
|
885
|
+
originRunId: candidate.originRunId,
|
|
886
|
+
source: {
|
|
887
|
+
origin: maintenanceOrigin,
|
|
888
|
+
target: view.target,
|
|
889
|
+
name: view.name,
|
|
890
|
+
objectId: view.objectId,
|
|
891
|
+
baseRevision: view.baseRevision,
|
|
892
|
+
candidateRevision: view.candidateRevision,
|
|
893
|
+
},
|
|
894
|
+
method: candidate.intent.method,
|
|
895
|
+
detailRef,
|
|
896
|
+
}, this.#now());
|
|
897
|
+
scope.onAccepted?.(started.operation.operationId);
|
|
898
|
+
if (started.duplicate) {
|
|
899
|
+
const delivered = await this.#withExecution(view, identity);
|
|
900
|
+
this.#current(scope, method, view.name);
|
|
901
|
+
return delivered;
|
|
902
|
+
}
|
|
903
|
+
let result, error;
|
|
904
|
+
try {
|
|
905
|
+
const receipt = this.#view(await this.authority.beginExecution(identity, {
|
|
906
|
+
requestId: id,
|
|
907
|
+
approvedRevision: view.candidateRevision,
|
|
908
|
+
decisionRevision: view.revision,
|
|
909
|
+
operationId: started.operation.operationId,
|
|
910
|
+
}, scope.signal));
|
|
911
|
+
requireCondition(receipt.status === "approved" &&
|
|
912
|
+
receipt.candidateRevision === view.candidateRevision &&
|
|
913
|
+
receipt.execution?.operationId === started.operation.operationId, "maintenance_execution_conflict", "The authority did not accept this exact execution.");
|
|
914
|
+
requireCondition(candidate.prepared.authStatus === "ready" ||
|
|
915
|
+
candidate.prepared.authStatus === "not_required", "maintenance_auth_required", "Authentication must be ready before the confirmed change can execute.");
|
|
916
|
+
this.#current(scope, candidate.intent.method, candidate.intent.name);
|
|
917
|
+
await this.backend.verify(candidate.intent, candidate.prepared, scope);
|
|
918
|
+
const executionScope = {
|
|
919
|
+
...scope,
|
|
920
|
+
operationId: started.operation.operationId,
|
|
921
|
+
assertCurrent: () => scope.assertCurrent(),
|
|
922
|
+
authorize: (target) => scope.authorize(target),
|
|
923
|
+
};
|
|
924
|
+
result = publicResult(await this.backend.apply(candidate.intent, candidate.prepared, executionScope));
|
|
925
|
+
this.#current(scope, candidate.intent.method, candidate.intent.name);
|
|
926
|
+
}
|
|
927
|
+
catch (cause) {
|
|
928
|
+
if (isAgentError(cause) && isPlainRecord(cause.details?.partial))
|
|
929
|
+
try {
|
|
930
|
+
result = publicResult(cause.details.partial);
|
|
931
|
+
}
|
|
932
|
+
catch { }
|
|
933
|
+
error = {
|
|
934
|
+
code: isAgentError(cause)
|
|
935
|
+
? cause.code
|
|
936
|
+
: "maintenance_execution_failed",
|
|
937
|
+
message: "The approved maintenance did not complete; inspect its actual execution state.",
|
|
938
|
+
};
|
|
939
|
+
}
|
|
940
|
+
const directory = privateDirectory(this.store.root, "state", "maintenance", id), file = path.join(directory, "execution.json");
|
|
941
|
+
await writeFile(file, canonicalJson({
|
|
942
|
+
operationId: started.operation.operationId,
|
|
943
|
+
requestId: id,
|
|
944
|
+
...(result ? { result } : {}),
|
|
945
|
+
...(error ? { error } : {}),
|
|
946
|
+
}, 80 * 1024), { mode: 0o600, flag: "wx" });
|
|
947
|
+
this.store.finishOperation(started.operation.operationId, error ? "failed" : "succeeded", error, this.#now());
|
|
948
|
+
const latest = this.#view(await this.authority.get(identity, id, scope.signal));
|
|
949
|
+
const delivered = await this.#withExecution(latest, identity);
|
|
950
|
+
this.#current(scope, method, view.name);
|
|
951
|
+
return delivered;
|
|
952
|
+
}
|
|
953
|
+
finally {
|
|
954
|
+
executionReservation?.release();
|
|
955
|
+
this.#busy = false;
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
//# sourceMappingURL=requests.js.map
|