@nexum-hub/agent 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +133 -2
- package/contracts/v2/common.schema.json +262 -0
- package/contracts/v2/fixtures/inputs.json +244 -0
- package/contracts/v2/fixtures/wire.json +6493 -0
- package/contracts/v2/model/agent.close.input.schema.json +14 -0
- package/contracts/v2/model/agent.close.output.schema.json +101 -0
- package/contracts/v2/model/agent.list.input.schema.json +57 -0
- package/contracts/v2/model/agent.list.output.schema.json +220 -0
- package/contracts/v2/model/agent.open.input.schema.json +35 -0
- package/contracts/v2/model/agent.open.output.schema.json +41 -0
- package/contracts/v2/model/agent.run.cancel.input.schema.json +18 -0
- package/contracts/v2/model/agent.run.cancel.output.schema.json +100 -0
- package/contracts/v2/model/agent.run.get.input.schema.json +41 -0
- package/contracts/v2/model/agent.run.get.output.schema.json +100 -0
- package/contracts/v2/model/agent.run.input.input.schema.json +30 -0
- package/contracts/v2/model/agent.run.input.output.schema.json +100 -0
- package/contracts/v2/model/agent.run.list.input.schema.json +47 -0
- package/contracts/v2/model/agent.run.list.output.schema.json +41 -0
- package/contracts/v2/model/agent.run.start.input.schema.json +73 -0
- package/contracts/v2/model/agent.run.start.output.schema.json +100 -0
- package/contracts/v2/wire/bodies.schema.json +383 -0
- package/contracts/v2/wire/envelope.schema.json +2280 -0
- package/contracts/v2/wire/events.schema.json +242 -0
- package/contracts/v2/wire/maintenance.schema.json +308 -0
- package/contracts/v2/wire/operations.schema.json +1791 -0
- package/contracts/v2/wire/stream.schema.json +74 -0
- package/contracts/v2/wire/types.schema.json +2616 -0
- package/dist/adapters/mcp-http.d.ts +22 -0
- package/dist/adapters/mcp-http.js +472 -0
- package/dist/adapters/mcp-http.js.map +1 -0
- package/dist/adapters/mcp-oauth.d.ts +29 -0
- package/dist/adapters/mcp-oauth.js +653 -0
- package/dist/adapters/mcp-oauth.js.map +1 -0
- package/dist/adapters/mcp-wire.d.ts +21 -0
- package/dist/adapters/mcp-wire.js +307 -0
- package/dist/adapters/mcp-wire.js.map +1 -0
- package/dist/adapters/mcp.d.ts +56 -0
- package/dist/adapters/mcp.js +1989 -0
- package/dist/adapters/mcp.js.map +1 -0
- package/dist/adapters/openapi-http.d.ts +29 -0
- package/dist/adapters/openapi-http.js +504 -0
- package/dist/adapters/openapi-http.js.map +1 -0
- package/dist/adapters/openapi-request.d.ts +26 -0
- package/dist/adapters/openapi-request.js +597 -0
- package/dist/adapters/openapi-request.js.map +1 -0
- package/dist/adapters/openapi-schema.d.ts +22 -0
- package/dist/adapters/openapi-schema.js +471 -0
- package/dist/adapters/openapi-schema.js.map +1 -0
- package/dist/adapters/openapi-typegen.d.ts +11 -0
- package/dist/adapters/openapi-typegen.js +185 -0
- package/dist/adapters/openapi-typegen.js.map +1 -0
- package/dist/adapters/openapi-types.d.ts +99 -0
- package/dist/adapters/openapi-types.js +2 -0
- package/dist/adapters/openapi-types.js.map +1 -0
- package/dist/adapters/openapi.d.ts +7 -0
- package/dist/adapters/openapi.js +768 -0
- package/dist/adapters/openapi.js.map +1 -0
- package/dist/application/activation.d.ts +28 -0
- package/dist/application/activation.js +289 -0
- package/dist/application/activation.js.map +1 -0
- package/dist/application/attachments.d.ts +24 -0
- package/dist/application/attachments.js +87 -0
- package/dist/application/attachments.js.map +1 -0
- package/dist/application/authority.d.ts +32 -0
- package/dist/application/authority.js +213 -0
- package/dist/application/authority.js.map +1 -0
- package/dist/application/catalog.d.ts +11 -0
- package/dist/application/catalog.js +177 -0
- package/dist/application/catalog.js.map +1 -0
- package/dist/application/config.d.ts +24 -0
- package/dist/application/config.js +140 -0
- package/dist/application/config.js.map +1 -0
- package/dist/application/connection.d.ts +35 -0
- package/dist/application/connection.js +465 -0
- package/dist/application/connection.js.map +1 -0
- package/dist/application/data-plane.d.ts +35 -0
- package/dist/application/data-plane.js +437 -0
- package/dist/application/data-plane.js.map +1 -0
- package/dist/application/diagnostics.d.ts +4 -0
- package/dist/application/diagnostics.js +65 -0
- package/dist/application/diagnostics.js.map +1 -0
- package/dist/application/dispatcher.d.ts +4 -0
- package/dist/application/dispatcher.js +16 -0
- package/dist/application/dispatcher.js.map +1 -0
- package/dist/application/main.d.ts +12 -0
- package/dist/application/main.js +167 -0
- package/dist/application/main.js.map +1 -0
- package/dist/application/maintenance.d.ts +27 -0
- package/dist/application/maintenance.js +244 -0
- package/dist/application/maintenance.js.map +1 -0
- package/dist/application/packages.d.ts +25 -0
- package/dist/application/packages.js +208 -0
- package/dist/application/packages.js.map +1 -0
- package/dist/application/processes.d.ts +49 -0
- package/dist/application/processes.js +180 -0
- package/dist/application/processes.js.map +1 -0
- package/dist/application/run-view.d.ts +17 -0
- package/dist/application/run-view.js +286 -0
- package/dist/application/run-view.js.map +1 -0
- package/dist/application/runtime.d.ts +78 -0
- package/dist/application/runtime.js +1747 -0
- package/dist/application/runtime.js.map +1 -0
- package/dist/application/tasks.d.ts +38 -0
- package/dist/application/tasks.js +23 -0
- package/dist/application/tasks.js.map +1 -0
- package/dist/application/user-state.d.ts +20 -0
- package/dist/application/user-state.js +52 -0
- package/dist/application/user-state.js.map +1 -0
- package/dist/application/work-git.d.ts +14 -0
- package/dist/application/work-git.js +67 -0
- package/dist/application/work-git.js.map +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +40 -0
- package/dist/cli.js.map +1 -0
- package/dist/contracts/caller.d.ts +51 -0
- package/dist/contracts/caller.js +95 -0
- package/dist/contracts/caller.js.map +1 -0
- package/dist/contracts/error-data.d.ts +6 -0
- package/dist/contracts/error-data.js +51 -0
- package/dist/contracts/error-data.js.map +1 -0
- package/dist/contracts/errors.d.ts +10 -0
- package/dist/contracts/errors.js +28 -0
- package/dist/contracts/errors.js.map +1 -0
- package/dist/contracts/json.d.ts +9 -0
- package/dist/contracts/json.js +104 -0
- package/dist/contracts/json.js.map +1 -0
- package/dist/contracts/types.d.ts +122 -0
- package/dist/contracts/types.js +19 -0
- package/dist/contracts/types.js.map +1 -0
- package/dist/contracts/validation.d.ts +4 -0
- package/dist/contracts/validation.js +157 -0
- package/dist/contracts/validation.js.map +1 -0
- package/dist/contracts/wire-validation.d.ts +9 -0
- package/dist/contracts/wire-validation.js +399 -0
- package/dist/contracts/wire-validation.js.map +1 -0
- package/dist/contracts/wire.d.ts +1083 -0
- package/dist/contracts/wire.js +62 -0
- package/dist/contracts/wire.js.map +1 -0
- package/dist/extension-api.d.ts +12 -0
- package/dist/extension-api.js +2 -0
- package/dist/extension-api.js.map +1 -0
- package/dist/extensions/api.d.ts +72 -0
- package/dist/extensions/api.js +2 -0
- package/dist/extensions/api.js.map +1 -0
- package/dist/extensions/host-requests.d.ts +23 -0
- package/dist/extensions/host-requests.js +60 -0
- package/dist/extensions/host-requests.js.map +1 -0
- package/dist/extensions/materials.d.ts +37 -0
- package/dist/extensions/materials.js +691 -0
- package/dist/extensions/materials.js.map +1 -0
- package/dist/extensions/package-loader.d.ts +28 -0
- package/dist/extensions/package-loader.js +177 -0
- package/dist/extensions/package-loader.js.map +1 -0
- package/dist/extensions/packages.d.ts +73 -0
- package/dist/extensions/packages.js +441 -0
- package/dist/extensions/packages.js.map +1 -0
- package/dist/extensions/registry.d.ts +92 -0
- package/dist/extensions/registry.js +1540 -0
- package/dist/extensions/registry.js.map +1 -0
- package/dist/extensions/resources.d.ts +105 -0
- package/dist/extensions/resources.js +595 -0
- package/dist/extensions/resources.js.map +1 -0
- package/dist/extensions/state.d.ts +25 -0
- package/dist/extensions/state.js +10 -0
- package/dist/extensions/state.js.map +1 -0
- package/dist/extensions/transfer.d.ts +81 -0
- package/dist/extensions/transfer.js +16 -0
- package/dist/extensions/transfer.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/maintenance/configuration.d.ts +79 -0
- package/dist/maintenance/configuration.js +1503 -0
- package/dist/maintenance/configuration.js.map +1 -0
- package/dist/maintenance/credentials.d.ts +23 -0
- package/dist/maintenance/credentials.js +177 -0
- package/dist/maintenance/credentials.js.map +1 -0
- package/dist/maintenance/files.d.ts +2 -0
- package/dist/maintenance/files.js +40 -0
- package/dist/maintenance/files.js.map +1 -0
- package/dist/maintenance/npm.d.ts +71 -0
- package/dist/maintenance/npm.js +1013 -0
- package/dist/maintenance/npm.js.map +1 -0
- package/dist/maintenance/oauth.d.ts +45 -0
- package/dist/maintenance/oauth.js +686 -0
- package/dist/maintenance/oauth.js.map +1 -0
- package/dist/maintenance/requests.d.ts +347 -0
- package/dist/maintenance/requests.js +959 -0
- package/dist/maintenance/requests.js.map +1 -0
- package/dist/maintenance/work-git.d.ts +29 -0
- package/dist/maintenance/work-git.js +776 -0
- package/dist/maintenance/work-git.js.map +1 -0
- package/dist/process/backend.d.ts +54 -0
- package/dist/process/backend.js +39 -0
- package/dist/process/backend.js.map +1 -0
- package/dist/process/environment-windows.d.ts +15 -0
- package/dist/process/environment-windows.js +151 -0
- package/dist/process/environment-windows.js.map +1 -0
- package/dist/process/environment.d.ts +28 -0
- package/dist/process/environment.js +410 -0
- package/dist/process/environment.js.map +1 -0
- package/dist/process/launch.d.ts +31 -0
- package/dist/process/launch.js +281 -0
- package/dist/process/launch.js.map +1 -0
- package/dist/process/managed.d.ts +36 -0
- package/dist/process/managed.js +459 -0
- package/dist/process/managed.js.map +1 -0
- package/dist/process/output-log.d.ts +50 -0
- package/dist/process/output-log.js +552 -0
- package/dist/process/output-log.js.map +1 -0
- package/dist/process/pipes.d.ts +16 -0
- package/dist/process/pipes.js +588 -0
- package/dist/process/pipes.js.map +1 -0
- package/dist/process/pty.d.ts +22 -0
- package/dist/process/pty.js +512 -0
- package/dist/process/pty.js.map +1 -0
- package/dist/process/windows-conout-worker.d.ts +1 -0
- package/dist/process/windows-conout-worker.js +90 -0
- package/dist/process/windows-conout-worker.js.map +1 -0
- package/dist/process/windows-conout.d.ts +46 -0
- package/dist/process/windows-conout.js +140 -0
- package/dist/process/windows-conout.js.map +1 -0
- package/dist/process/windows-helper.d.ts +43 -0
- package/dist/process/windows-helper.js +255 -0
- package/dist/process/windows-helper.js.map +1 -0
- package/dist/process/windows-job.d.ts +39 -0
- package/dist/process/windows-job.js +224 -0
- package/dist/process/windows-job.js.map +1 -0
- package/dist/process/windows-pipes.d.ts +16 -0
- package/dist/process/windows-pipes.js +475 -0
- package/dist/process/windows-pipes.js.map +1 -0
- package/dist/results/artifacts.d.ts +118 -0
- package/dist/results/artifacts.js +873 -0
- package/dist/results/artifacts.js.map +1 -0
- package/dist/results/encoder.d.ts +57 -0
- package/dist/results/encoder.js +1179 -0
- package/dist/results/encoder.js.map +1 -0
- package/dist/results/mcp.d.ts +95 -0
- package/dist/results/mcp.js +656 -0
- package/dist/results/mcp.js.map +1 -0
- package/dist/results/media.d.ts +19 -0
- package/dist/results/media.js +301 -0
- package/dist/results/media.js.map +1 -0
- package/dist/results/normalize.d.ts +29 -0
- package/dist/results/normalize.js +464 -0
- package/dist/results/normalize.js.map +1 -0
- package/dist/runtime/control-scripts.d.ts +23 -0
- package/dist/runtime/control-scripts.js +242 -0
- package/dist/runtime/control-scripts.js.map +1 -0
- package/dist/runtime/engine.d.ts +177 -0
- package/dist/runtime/engine.js +1159 -0
- package/dist/runtime/engine.js.map +1 -0
- package/dist/runtime/globals.d.ts +1 -0
- package/dist/runtime/globals.js +31 -0
- package/dist/runtime/globals.js.map +1 -0
- package/dist/runtime/material-retention.d.ts +34 -0
- package/dist/runtime/material-retention.js +671 -0
- package/dist/runtime/material-retention.js.map +1 -0
- package/dist/runtime/modules.d.ts +50 -0
- package/dist/runtime/modules.js +320 -0
- package/dist/runtime/modules.js.map +1 -0
- package/dist/runtime/names.d.ts +2 -0
- package/dist/runtime/names.js +22 -0
- package/dist/runtime/names.js.map +1 -0
- package/dist/runtime/operations.d.ts +20 -0
- package/dist/runtime/operations.js +137 -0
- package/dist/runtime/operations.js.map +1 -0
- package/dist/runtime/paths.d.ts +3 -0
- package/dist/runtime/paths.js +38 -0
- package/dist/runtime/paths.js.map +1 -0
- package/dist/runtime/promises.d.ts +9 -0
- package/dist/runtime/promises.js +78 -0
- package/dist/runtime/promises.js.map +1 -0
- package/dist/runtime/retention.d.ts +88 -0
- package/dist/runtime/retention.js +735 -0
- package/dist/runtime/retention.js.map +1 -0
- package/dist/runtime/run-log.d.ts +30 -0
- package/dist/runtime/run-log.js +207 -0
- package/dist/runtime/run-log.js.map +1 -0
- package/dist/runtime/scripts.d.ts +84 -0
- package/dist/runtime/scripts.js +387 -0
- package/dist/runtime/scripts.js.map +1 -0
- package/dist/runtime/tasks.d.ts +54 -0
- package/dist/runtime/tasks.js +290 -0
- package/dist/runtime/tasks.js.map +1 -0
- package/dist/sdk/maintenance.d.ts +3 -0
- package/dist/sdk/maintenance.js +80 -0
- package/dist/sdk/maintenance.js.map +1 -0
- package/dist/sdk/output-extension.d.ts +3 -0
- package/dist/sdk/output-extension.js +44 -0
- package/dist/sdk/output-extension.js.map +1 -0
- package/dist/sdk/output.d.ts +52 -0
- package/dist/sdk/output.js +350 -0
- package/dist/sdk/output.js.map +1 -0
- package/dist/sdk/runtime.d.ts +3 -0
- package/dist/sdk/runtime.js +26 -0
- package/dist/sdk/runtime.js.map +1 -0
- package/dist/sdk/sh.d.ts +3 -0
- package/dist/sdk/sh.js +384 -0
- package/dist/sdk/sh.js.map +1 -0
- package/dist/sdk/state.d.ts +2 -0
- package/dist/sdk/state.js +69 -0
- package/dist/sdk/state.js.map +1 -0
- package/dist/sdk/transfer.d.ts +3 -0
- package/dist/sdk/transfer.js +141 -0
- package/dist/sdk/transfer.js.map +1 -0
- package/dist/services/extensions.d.ts +19 -0
- package/dist/services/extensions.js +183 -0
- package/dist/services/extensions.js.map +1 -0
- package/dist/services/state.d.ts +37 -0
- package/dist/services/state.js +125 -0
- package/dist/services/state.js.map +1 -0
- package/dist/services/transfer.d.ts +86 -0
- package/dist/services/transfer.js +948 -0
- package/dist/services/transfer.js.map +1 -0
- package/dist/storage/schema.d.ts +8 -0
- package/dist/storage/schema.js +241 -0
- package/dist/storage/schema.js.map +1 -0
- package/dist/storage/sqlite.d.ts +241 -0
- package/dist/storage/sqlite.js +1855 -0
- package/dist/storage/sqlite.js.map +1 -0
- package/dist/storage/transfers.d.ts +90 -0
- package/dist/storage/transfers.js +2 -0
- package/dist/storage/transfers.js.map +1 -0
- package/dist/transfer/io.d.ts +74 -0
- package/dist/transfer/io.js +425 -0
- package/dist/transfer/io.js.map +1 -0
- package/dist/transfer/offers.d.ts +72 -0
- package/dist/transfer/offers.js +380 -0
- package/dist/transfer/offers.js.map +1 -0
- package/dist/transfer/stream-slots.d.ts +12 -0
- package/dist/transfer/stream-slots.js +28 -0
- package/dist/transfer/stream-slots.js.map +1 -0
- package/dist/work/catalog.d.ts +43 -0
- package/dist/work/catalog.js +198 -0
- package/dist/work/catalog.js.map +1 -0
- package/dist/work/discovery.d.ts +70 -0
- package/dist/work/discovery.js +465 -0
- package/dist/work/discovery.js.map +1 -0
- package/dist/work/service.d.ts +83 -0
- package/dist/work/service.js +703 -0
- package/dist/work/service.js.map +1 -0
- package/package.json +66 -4
|
@@ -0,0 +1,703 @@
|
|
|
1
|
+
import { lstatSync, readFileSync, realpathSync, statSync, rmSync, } from "node:fs";
|
|
2
|
+
import { readdir } from "node:fs/promises";
|
|
3
|
+
import { once } from "node:events";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import * as os from "node:os";
|
|
6
|
+
import { performance } from "node:perf_hooks";
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
8
|
+
import { AgentError, requireCondition } from "../contracts/errors.js";
|
|
9
|
+
import { canonicalJson, digest, newId } from "../contracts/json.js";
|
|
10
|
+
import { validateContract } from "../contracts/validation.js";
|
|
11
|
+
import { authorizeCall, callerKey, fixedCaller, } from "../contracts/caller.js";
|
|
12
|
+
import { atomicText, contains, privateDirectory } from "../runtime/paths.js";
|
|
13
|
+
import { boundedFile, discoverMaterials, enabledMaterials, readProjectSummary, } from "./discovery.js";
|
|
14
|
+
const json = (value, max = 16 * 1024 * 1024) => JSON.parse(canonicalJson(value, max));
|
|
15
|
+
function withoutDisabled(value) {
|
|
16
|
+
const { disabled: _disabled, ...rest } = value;
|
|
17
|
+
return rest;
|
|
18
|
+
}
|
|
19
|
+
function modelBytes(value) {
|
|
20
|
+
// The actual JSON fallback is counted alongside structuredContent, including escaping.
|
|
21
|
+
return Buffer.byteLength(JSON.stringify({
|
|
22
|
+
content: [{ type: "text", text: JSON.stringify(value) }],
|
|
23
|
+
structuredContent: value,
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
export class WorkService {
|
|
27
|
+
#store;
|
|
28
|
+
#scripts;
|
|
29
|
+
#catalog;
|
|
30
|
+
#options;
|
|
31
|
+
#refreshes = new Map();
|
|
32
|
+
#operations = new Set();
|
|
33
|
+
constructor(store, scripts, catalog, options) {
|
|
34
|
+
requireCondition(scripts.work === path.join(store.root, "work") &&
|
|
35
|
+
catalog.work === scripts.work &&
|
|
36
|
+
scripts.owner === `local:${store.binding.agentId}`, "runtime_profile_mismatch", "Work metadata must use this Agent's own profile/work environment.");
|
|
37
|
+
this.#store = store;
|
|
38
|
+
this.#scripts = scripts;
|
|
39
|
+
this.#catalog = catalog;
|
|
40
|
+
this.#options = options;
|
|
41
|
+
}
|
|
42
|
+
async open(caller, input, signal) {
|
|
43
|
+
validateContract("agent.open", input);
|
|
44
|
+
caller = fixedCaller(caller);
|
|
45
|
+
requireCondition(input.agentId === this.#store.binding.agentId, "agent_mismatch", "Open targets a different Agent.");
|
|
46
|
+
requireCondition(this.#operations.size < 64, "work_busy", "Too many work metadata operations are pending.");
|
|
47
|
+
const scope = signal
|
|
48
|
+
? AbortSignal.any([signal, AbortSignal.timeout(30000)])
|
|
49
|
+
: AbortSignal.timeout(30000);
|
|
50
|
+
scope.throwIfAborted();
|
|
51
|
+
const key = callerKey(caller);
|
|
52
|
+
const fixed = json(input);
|
|
53
|
+
this.#authorize(caller, { action: "open" });
|
|
54
|
+
if (fixed.contextId)
|
|
55
|
+
this.#store.activeWork(fixed.contextId, key);
|
|
56
|
+
const previous = fixed.contextId
|
|
57
|
+
? this.#refreshes.get(fixed.contextId)
|
|
58
|
+
: undefined;
|
|
59
|
+
const operation = (previous ?? Promise.resolve()).then(() => this.#build(caller, fixed, scope));
|
|
60
|
+
const tail = operation.then(() => { }, () => { });
|
|
61
|
+
this.#operations.add(tail);
|
|
62
|
+
if (fixed.contextId)
|
|
63
|
+
this.#refreshes.set(fixed.contextId, tail);
|
|
64
|
+
void tail.then(() => {
|
|
65
|
+
this.#operations.delete(tail);
|
|
66
|
+
if (fixed.contextId && this.#refreshes.get(fixed.contextId) === tail)
|
|
67
|
+
this.#refreshes.delete(fixed.contextId);
|
|
68
|
+
});
|
|
69
|
+
// Cancelling this invocation stops its build/queue entry, not another refresh.
|
|
70
|
+
const observation = new AbortController();
|
|
71
|
+
const abort = once(scope, "abort", { signal: observation.signal }).then(() => {
|
|
72
|
+
scope.throwIfAborted();
|
|
73
|
+
throw new AgentError("work_aborted", "The work request was aborted.");
|
|
74
|
+
});
|
|
75
|
+
void abort.catch(() => { });
|
|
76
|
+
try {
|
|
77
|
+
scope.throwIfAborted();
|
|
78
|
+
return await Promise.race([operation, abort]);
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
observation.abort();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** Owner Console inspection retains disabled discovery without injecting it into model context. */
|
|
85
|
+
async inspect(caller, input, signal) {
|
|
86
|
+
caller = fixedCaller(caller);
|
|
87
|
+
requireCondition(caller.source === "owner_session", "material_forbidden", "Only a verified owner session may inspect management metadata.");
|
|
88
|
+
requireCondition(typeof input.projectId === "string" &&
|
|
89
|
+
input.projectId.length > 0 &&
|
|
90
|
+
input.path === undefined, "invalid_work_inspection", "Project inspection needs a fixed project identity and cannot switch to a path.");
|
|
91
|
+
const opened = await this.open(caller, input, signal);
|
|
92
|
+
const work = this.#store.activeWork(opened.contextId, callerKey(caller));
|
|
93
|
+
requireCondition(work.revision === opened.revision, "context_revision_mismatch", "The work changed before management metadata could be projected.");
|
|
94
|
+
this.#authorize(caller, { action: "read", work });
|
|
95
|
+
signal?.throwIfAborted();
|
|
96
|
+
const bundle = this.#bundle(work);
|
|
97
|
+
const instruction = (item) => {
|
|
98
|
+
const { content: _content, ...metadata } = item;
|
|
99
|
+
return json({
|
|
100
|
+
...metadata,
|
|
101
|
+
itemId: item.instructionId,
|
|
102
|
+
disabled: "disabled" in item ? item.disabled : false,
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
const skill = (item) => json({
|
|
106
|
+
...item,
|
|
107
|
+
itemId: item.skillId,
|
|
108
|
+
disabled: "disabled" in item ? item.disabled : false,
|
|
109
|
+
});
|
|
110
|
+
const view = json(opened);
|
|
111
|
+
view.agent.management = {
|
|
112
|
+
instructions: [
|
|
113
|
+
...bundle.published.instructions,
|
|
114
|
+
...bundle.discovered.instructions.filter((item) => item.scope.type !== "project"),
|
|
115
|
+
].map(instruction),
|
|
116
|
+
skills: [
|
|
117
|
+
...bundle.published.skills,
|
|
118
|
+
...bundle.discovered.skills.filter((item) => item.scope.type !== "project"),
|
|
119
|
+
].map(skill),
|
|
120
|
+
};
|
|
121
|
+
const project = view.project;
|
|
122
|
+
project.descriptionItemId = bundle.description?.itemId ?? null;
|
|
123
|
+
project.management = {
|
|
124
|
+
instructions: bundle.discovered.instructions
|
|
125
|
+
.filter((item) => item.scope.type === "project")
|
|
126
|
+
.map(instruction),
|
|
127
|
+
skills: bundle.discovered.skills
|
|
128
|
+
.filter((item) => item.scope.type === "project")
|
|
129
|
+
.map(skill),
|
|
130
|
+
nestedInstructions: bundle.discovered.nestedInstructions.map(instruction),
|
|
131
|
+
};
|
|
132
|
+
return view;
|
|
133
|
+
}
|
|
134
|
+
#authorize(caller, target) {
|
|
135
|
+
authorizeCall(() => this.#options.authorize(caller, target));
|
|
136
|
+
}
|
|
137
|
+
async #build(caller, input, signal) {
|
|
138
|
+
signal.throwIfAborted();
|
|
139
|
+
const key = callerKey(caller);
|
|
140
|
+
const previous = input.contextId
|
|
141
|
+
? this.#store.activeWork(input.contextId, key)
|
|
142
|
+
: null;
|
|
143
|
+
if (previous) {
|
|
144
|
+
requireCondition(!input.projectId || previous.projectId === input.projectId, "context_binding_mismatch", "Refresh cannot switch the work's Project.");
|
|
145
|
+
requireCondition(previous.projectId !== null || (!input.projectId && !input.path), "context_binding_mismatch", "Refresh cannot attach a Project to an Agent-only work.");
|
|
146
|
+
if (input.path)
|
|
147
|
+
requireCondition(previous.projectId !== null &&
|
|
148
|
+
realpathSync.native(input.path) === previous.pwd, "context_binding_mismatch", "Refresh cannot switch the work directory or attach a Project.");
|
|
149
|
+
}
|
|
150
|
+
let resolved = null;
|
|
151
|
+
const projectId = previous?.projectId ?? input.projectId;
|
|
152
|
+
if (projectId || (!previous && input.path)) {
|
|
153
|
+
const target = projectId
|
|
154
|
+
? { projectId }
|
|
155
|
+
: { path: realpathSync.native(input.path) };
|
|
156
|
+
resolved = await this.#options.resolveProject(caller, target, signal);
|
|
157
|
+
requireCondition(resolved.project.projectId &&
|
|
158
|
+
(!projectId || resolved.project.projectId === projectId), "project_binding_mismatch", "The catalog returned a different Project identity.");
|
|
159
|
+
const actual = realpathSync.native(resolved.project.root);
|
|
160
|
+
requireCondition(statSync(actual).isDirectory() &&
|
|
161
|
+
(!input.path || actual === realpathSync.native(input.path)), "project_binding_mismatch", "The catalog root does not match the requested local directory.");
|
|
162
|
+
resolved = {
|
|
163
|
+
project: { ...resolved.project, root: actual },
|
|
164
|
+
disabledItems: resolved.disabledItems.map((item) => ({ ...item })),
|
|
165
|
+
};
|
|
166
|
+
if (previous)
|
|
167
|
+
requireCondition(actual === previous.pwd, "context_binding_mismatch", "A refresh cannot relocate an existing work.");
|
|
168
|
+
}
|
|
169
|
+
const project = resolved?.project ?? null;
|
|
170
|
+
const pwd = project?.root ?? this.#scripts.work;
|
|
171
|
+
this.#authorize(caller, {
|
|
172
|
+
action: previous ? "refresh" : "open",
|
|
173
|
+
...(previous ? { work: previous } : {}),
|
|
174
|
+
project,
|
|
175
|
+
path: pwd,
|
|
176
|
+
});
|
|
177
|
+
signal.throwIfAborted();
|
|
178
|
+
const discovered = await discoverMaterials({
|
|
179
|
+
agentId: this.#store.binding.agentId,
|
|
180
|
+
work: this.#scripts.work,
|
|
181
|
+
home: this.#options.home,
|
|
182
|
+
...(project
|
|
183
|
+
? { projectRoot: project.root, disabledItems: resolved.disabledItems }
|
|
184
|
+
: {}),
|
|
185
|
+
signal,
|
|
186
|
+
});
|
|
187
|
+
const summary = project
|
|
188
|
+
? await readProjectSummary(project.root, signal)
|
|
189
|
+
: null;
|
|
190
|
+
let description;
|
|
191
|
+
if (project && summary?.status === "updated" && summary.revision) {
|
|
192
|
+
try {
|
|
193
|
+
description = await this.#description(project.root, summary.revision, signal);
|
|
194
|
+
requireCondition(description, "material_changed", "The README disappeared before its full-description evidence was captured.");
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
signal.throwIfAborted();
|
|
198
|
+
discovered.warnings.push({
|
|
199
|
+
code: "readme_unavailable",
|
|
200
|
+
path: project.root,
|
|
201
|
+
message: "The optional README changed or became unavailable; no full-description reference was published.",
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (project && summary?.description !== null)
|
|
206
|
+
project.description = summary?.description ?? "";
|
|
207
|
+
if (summary?.status === "unavailable")
|
|
208
|
+
discovered.warnings.push({
|
|
209
|
+
code: "readme_unavailable",
|
|
210
|
+
path: project.root,
|
|
211
|
+
message: "The README summary could not be updated; the last known value is retained.",
|
|
212
|
+
});
|
|
213
|
+
const effective = enabledMaterials(discovered);
|
|
214
|
+
const published = json(this.#options.materials?.() ?? {
|
|
215
|
+
instructions: [],
|
|
216
|
+
skills: [],
|
|
217
|
+
docs: [],
|
|
218
|
+
}, 4 * 1024 * 1024);
|
|
219
|
+
this.#validatePublished(published);
|
|
220
|
+
const usage = this.#options.scriptUsage?.(caller) ?? this.#store.scriptUsage(key);
|
|
221
|
+
const canUse = (origin) => this.#options.canUseScript?.(caller, origin) ?? true;
|
|
222
|
+
const library = this.#scripts.discover(canUse);
|
|
223
|
+
const scripts = this.#scripts.select(usage, canUse, 128 * 1024, library);
|
|
224
|
+
// Capture all contribution discovery synchronously. Catalog I/O may yield
|
|
225
|
+
// while another work enables/disables a source; its versioned manual must
|
|
226
|
+
// still describe this same captured instruction/Skill/script generation.
|
|
227
|
+
const docs = await this.#catalog.refresh(published.docs, signal, library);
|
|
228
|
+
signal.throwIfAborted();
|
|
229
|
+
const directory = privateDirectory(this.#scripts.work, "tmp", "contexts", newId("snapshot"));
|
|
230
|
+
let committed = false;
|
|
231
|
+
let hold;
|
|
232
|
+
const write = async (filename, text) => {
|
|
233
|
+
signal.throwIfAborted();
|
|
234
|
+
const reservation = await this.#options.reserve?.(Buffer.byteLength(text));
|
|
235
|
+
try {
|
|
236
|
+
signal.throwIfAborted();
|
|
237
|
+
this.#authorize(caller, {
|
|
238
|
+
action: previous ? "refresh" : "open",
|
|
239
|
+
...(previous ? { work: previous } : {}),
|
|
240
|
+
project,
|
|
241
|
+
path: pwd,
|
|
242
|
+
});
|
|
243
|
+
requireCondition(realpathSync.native(this.#store.root) === this.#store.root &&
|
|
244
|
+
realpathSync.native(directory) === directory &&
|
|
245
|
+
lstatSync(directory).isDirectory() &&
|
|
246
|
+
!lstatSync(directory).isSymbolicLink(), "material_path_escape", "The work snapshot directory changed before its reserved write.");
|
|
247
|
+
atomicText(filename, text);
|
|
248
|
+
}
|
|
249
|
+
finally {
|
|
250
|
+
reservation?.release();
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
try {
|
|
254
|
+
hold = await this.#options.materialRetention?.hold("work", directory);
|
|
255
|
+
const agentIndex = path.join(directory, "agent.json"), projectIndex = path.join(directory, "project.json");
|
|
256
|
+
const agentInstructions = [
|
|
257
|
+
...published.instructions,
|
|
258
|
+
...effective.instructions
|
|
259
|
+
.filter((item) => item.scope.type !== "project")
|
|
260
|
+
.map(withoutDisabled),
|
|
261
|
+
];
|
|
262
|
+
const agentSkills = [
|
|
263
|
+
...published.skills,
|
|
264
|
+
...effective.skills
|
|
265
|
+
.filter((item) => item.scope.type !== "project")
|
|
266
|
+
.map(withoutDisabled),
|
|
267
|
+
];
|
|
268
|
+
const projectInstructions = effective.instructions
|
|
269
|
+
.filter((item) => item.scope.type === "project")
|
|
270
|
+
.map(withoutDisabled);
|
|
271
|
+
const projectSkills = effective.skills
|
|
272
|
+
.filter((item) => item.scope.type === "project")
|
|
273
|
+
.map(withoutDisabled);
|
|
274
|
+
const allAgent = json({
|
|
275
|
+
instructions: agentInstructions,
|
|
276
|
+
skills: agentSkills,
|
|
277
|
+
scripts: library.entries,
|
|
278
|
+
warnings: library.warnings,
|
|
279
|
+
});
|
|
280
|
+
const allProject = json({
|
|
281
|
+
instructions: projectInstructions,
|
|
282
|
+
skills: projectSkills,
|
|
283
|
+
nestedInstructions: effective.nestedInstructions.map(withoutDisabled),
|
|
284
|
+
});
|
|
285
|
+
await write(agentIndex, canonicalJson(allAgent, 16 * 1024 * 1024));
|
|
286
|
+
if (project)
|
|
287
|
+
await write(projectIndex, canonicalJson(allProject, 16 * 1024 * 1024));
|
|
288
|
+
const cpus = os.cpus();
|
|
289
|
+
const view = json({
|
|
290
|
+
observedAt: new Date().toISOString(),
|
|
291
|
+
pwd,
|
|
292
|
+
agent: {
|
|
293
|
+
agentId: this.#store.binding.agentId,
|
|
294
|
+
name: this.#options.agent.name,
|
|
295
|
+
description: this.#options.agent.description ?? null,
|
|
296
|
+
software: {
|
|
297
|
+
version: this.#options.agent.version,
|
|
298
|
+
build: this.#options.agent.build ?? null,
|
|
299
|
+
protocolVersion: this.#options.agent.protocolVersion,
|
|
300
|
+
extensionApiVersion: this.#options.agent.extensionApiVersion ?? null,
|
|
301
|
+
},
|
|
302
|
+
system: {
|
|
303
|
+
platform: process.platform,
|
|
304
|
+
type: os.type(),
|
|
305
|
+
release: os.release(),
|
|
306
|
+
version: os.version(),
|
|
307
|
+
machine: os.machine(),
|
|
308
|
+
source: "node:os",
|
|
309
|
+
cpu: {
|
|
310
|
+
models: [...new Set(cpus.map((cpu) => cpu.model))].slice(0, 4),
|
|
311
|
+
logicalCount: cpus.length,
|
|
312
|
+
availableParallelism: os.availableParallelism(),
|
|
313
|
+
},
|
|
314
|
+
memory: { totalBytes: os.totalmem(), freeBytes: os.freemem() },
|
|
315
|
+
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
316
|
+
locale: Intl.DateTimeFormat().resolvedOptions().locale,
|
|
317
|
+
},
|
|
318
|
+
runtime: {
|
|
319
|
+
nodeVersion: process.version,
|
|
320
|
+
v8Version: process.versions.v8,
|
|
321
|
+
modulesAbi: process.versions.modules,
|
|
322
|
+
arch: process.arch,
|
|
323
|
+
startedAt: new Date(performance.timeOrigin).toISOString(),
|
|
324
|
+
},
|
|
325
|
+
paths: {
|
|
326
|
+
work: this.#scripts.work,
|
|
327
|
+
docs: this.#scripts.docs,
|
|
328
|
+
scripts: this.#scripts.local,
|
|
329
|
+
resources: path.join(this.#scripts.work, "resources"),
|
|
330
|
+
},
|
|
331
|
+
modules: {
|
|
332
|
+
builtinFamilies: docs.builtinFamilies,
|
|
333
|
+
packagesIndex: docs.packagesIndex,
|
|
334
|
+
},
|
|
335
|
+
instructions: agentInstructions,
|
|
336
|
+
skills: agentSkills,
|
|
337
|
+
skillsComplete: discovered.skillsComplete,
|
|
338
|
+
materialsIndex: agentIndex,
|
|
339
|
+
docs: {
|
|
340
|
+
entry: docs.entry,
|
|
341
|
+
revision: docs.revision,
|
|
342
|
+
content: docs.content,
|
|
343
|
+
truncated: docs.truncated,
|
|
344
|
+
nextLine: docs.nextLine,
|
|
345
|
+
},
|
|
346
|
+
scripts: {
|
|
347
|
+
total: scripts.total,
|
|
348
|
+
items: scripts.items,
|
|
349
|
+
complete: scripts.complete,
|
|
350
|
+
index: path.join(this.#scripts.docs, "scripts.md"),
|
|
351
|
+
},
|
|
352
|
+
},
|
|
353
|
+
project: project
|
|
354
|
+
? {
|
|
355
|
+
...project,
|
|
356
|
+
summary,
|
|
357
|
+
descriptionItemId: description?.itemId ?? null,
|
|
358
|
+
instructions: projectInstructions,
|
|
359
|
+
skills: projectSkills,
|
|
360
|
+
nestedInstructions: effective.nestedInstructions.map(withoutDisabled),
|
|
361
|
+
skillsComplete: discovered.skillsComplete,
|
|
362
|
+
nestedInstructionsComplete: discovered.nestedComplete,
|
|
363
|
+
materialsIndex: projectIndex,
|
|
364
|
+
}
|
|
365
|
+
: null,
|
|
366
|
+
warnings: [...discovered.warnings, ...scripts.warnings],
|
|
367
|
+
});
|
|
368
|
+
this.#fit(view);
|
|
369
|
+
const bundle = {
|
|
370
|
+
agentId: this.#store.binding.agentId,
|
|
371
|
+
bootId: this.#store.bootId,
|
|
372
|
+
callerKey: key,
|
|
373
|
+
projectId: project?.projectId ?? null,
|
|
374
|
+
view,
|
|
375
|
+
discovered,
|
|
376
|
+
published,
|
|
377
|
+
...(description ? { description } : {}),
|
|
378
|
+
};
|
|
379
|
+
const filename = path.join(directory, "bundle.json");
|
|
380
|
+
await write(filename, canonicalJson(bundle, 16 * 1024 * 1024));
|
|
381
|
+
signal.throwIfAborted();
|
|
382
|
+
this.#authorize(caller, {
|
|
383
|
+
action: "commit",
|
|
384
|
+
...(previous ? { work: previous } : {}),
|
|
385
|
+
project,
|
|
386
|
+
path: pwd,
|
|
387
|
+
});
|
|
388
|
+
const relative = path
|
|
389
|
+
.relative(this.#store.root, filename)
|
|
390
|
+
.split(path.sep)
|
|
391
|
+
.join("/");
|
|
392
|
+
const work = previous
|
|
393
|
+
? this.#store.refreshWork(previous.contextId, key, previous.revision, relative)
|
|
394
|
+
: this.#store.openWork({
|
|
395
|
+
callerKey: key,
|
|
396
|
+
...(project ? { projectId: project.projectId } : {}),
|
|
397
|
+
pwd,
|
|
398
|
+
snapshotRef: relative,
|
|
399
|
+
});
|
|
400
|
+
committed = true;
|
|
401
|
+
return { contextId: work.contextId, revision: work.revision, ...view };
|
|
402
|
+
}
|
|
403
|
+
finally {
|
|
404
|
+
try {
|
|
405
|
+
if (!committed && lstatSync(directory, { throwIfNoEntry: false })) {
|
|
406
|
+
requireCondition(realpathSync.native(directory) === directory &&
|
|
407
|
+
lstatSync(directory).isDirectory() &&
|
|
408
|
+
!lstatSync(directory).isSymbolicLink(), "material_path_escape", "A failed work snapshot cannot redirect its cleanup.");
|
|
409
|
+
rmSync(directory, { recursive: true, force: true });
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
finally {
|
|
413
|
+
hold?.release();
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
async #description(root, revision, signal) {
|
|
418
|
+
signal.throwIfAborted();
|
|
419
|
+
const priority = (name) => name.toLowerCase() === "readme.md"
|
|
420
|
+
? 0
|
|
421
|
+
: name.toLowerCase() === "readme"
|
|
422
|
+
? 1
|
|
423
|
+
: 2;
|
|
424
|
+
const entries = (await readdir(root, { withFileTypes: true })).filter((item) => item.isFile() && /^readme(?:\..*)?$/i.test(item.name));
|
|
425
|
+
entries.sort((a, b) => priority(a.name) - priority(b.name) ||
|
|
426
|
+
(a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
427
|
+
if (!entries.length)
|
|
428
|
+
return undefined;
|
|
429
|
+
const filename = path.join(root, entries[0].name), actual = realpathSync.native(filename);
|
|
430
|
+
requireCondition(actual === filename && contains(root, actual), "material_path_escape", "The actual README must stay inside its canonical Project.");
|
|
431
|
+
const read = await boundedFile(actual, 1024 * 1024, signal);
|
|
432
|
+
requireCondition(read.revision === revision, "material_changed", "The README changed during work discovery; refresh the work.");
|
|
433
|
+
return {
|
|
434
|
+
itemId: "readme:" + digest({ path: actual, revision }),
|
|
435
|
+
path: actual,
|
|
436
|
+
revision,
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
#validatePublished(published) {
|
|
440
|
+
canonicalJson(published, 4 * 1024 * 1024);
|
|
441
|
+
for (const item of [...published.instructions, ...published.skills]) {
|
|
442
|
+
requireCondition(["platform", "extension"].includes(item.origin.type) &&
|
|
443
|
+
item.origin.owner &&
|
|
444
|
+
item.origin.version &&
|
|
445
|
+
item.origin.revision &&
|
|
446
|
+
item.scope.type === "agent", "invalid_material_source", "Published guidance must keep its actual platform/extension owner, version and Agent scope.");
|
|
447
|
+
}
|
|
448
|
+
for (const item of published.instructions)
|
|
449
|
+
requireCondition(typeof item.content === "string" &&
|
|
450
|
+
Buffer.byteLength(item.content) <= 65536, "material_too_large", "An entry instruction must be delivered completely within its byte budget.");
|
|
451
|
+
for (const skill of published.skills) {
|
|
452
|
+
const root = realpathSync.native(skill.path), file = realpathSync.native(skill.skillFile);
|
|
453
|
+
requireCondition(contains(this.#scripts.docs, root) &&
|
|
454
|
+
contains(root, file) &&
|
|
455
|
+
statSync(file).isFile(), "material_path_escape", "An extension Skill must use its published reading copy.");
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
#fit(view) {
|
|
459
|
+
const agent = view.agent, project = view.project;
|
|
460
|
+
const scripts = agent.scripts, docs = agent.docs;
|
|
461
|
+
const scriptItems = scripts.items;
|
|
462
|
+
const warnings = view.warnings;
|
|
463
|
+
let omitted = false;
|
|
464
|
+
const budgetWarning = {
|
|
465
|
+
code: "work_display_budget",
|
|
466
|
+
message: "Some complete optional definitions were omitted. Read the corresponding Agent/Project materialsIndex; displayed schemas and mandatory instructions remain complete.",
|
|
467
|
+
};
|
|
468
|
+
const over = () => modelBytes({
|
|
469
|
+
...view,
|
|
470
|
+
warnings: [...warnings, ...(omitted ? [budgetWarning] : [])],
|
|
471
|
+
contextId: "ctx_" + "0".repeat(32),
|
|
472
|
+
revision: 2147483647,
|
|
473
|
+
}) >
|
|
474
|
+
128 * 1024;
|
|
475
|
+
while (over() && scriptItems.length) {
|
|
476
|
+
scriptItems.pop();
|
|
477
|
+
scripts.complete = false;
|
|
478
|
+
omitted = true;
|
|
479
|
+
}
|
|
480
|
+
while (over() && agent.skills.length) {
|
|
481
|
+
agent.skills.pop();
|
|
482
|
+
agent.skillsComplete = false;
|
|
483
|
+
omitted = true;
|
|
484
|
+
}
|
|
485
|
+
while (over() && project && project.skills.length) {
|
|
486
|
+
project.skills.pop();
|
|
487
|
+
project.skillsComplete = false;
|
|
488
|
+
omitted = true;
|
|
489
|
+
}
|
|
490
|
+
while (over() &&
|
|
491
|
+
project &&
|
|
492
|
+
project.nestedInstructions.length) {
|
|
493
|
+
project.nestedInstructions.pop();
|
|
494
|
+
project.nestedInstructionsComplete = false;
|
|
495
|
+
omitted = true;
|
|
496
|
+
}
|
|
497
|
+
if (over() && typeof docs.content === "string") {
|
|
498
|
+
docs.content = "";
|
|
499
|
+
docs.truncated = true;
|
|
500
|
+
docs.nextLine = 1;
|
|
501
|
+
omitted = true;
|
|
502
|
+
}
|
|
503
|
+
requireCondition(!over(), "work_materials_too_large", "The complete mandatory entry instructions exceed the work result budget.");
|
|
504
|
+
if (omitted)
|
|
505
|
+
warnings.push(budgetWarning);
|
|
506
|
+
}
|
|
507
|
+
#bundle(work) {
|
|
508
|
+
const root = realpathSync.native(path.join(this.#scripts.work, "tmp", "contexts"));
|
|
509
|
+
const filename = realpathSync.native(path.resolve(this.#store.root, work.snapshotRef));
|
|
510
|
+
requireCondition(contains(this.#scripts.work, root) &&
|
|
511
|
+
contains(root, filename) &&
|
|
512
|
+
statSync(filename).isFile() &&
|
|
513
|
+
statSync(filename).size <= 16 * 1024 * 1024, "work_snapshot_unavailable", "The retained work snapshot is unavailable or outside its profile.");
|
|
514
|
+
const bundle = JSON.parse(readFileSync(filename, "utf8"));
|
|
515
|
+
requireCondition(bundle.agentId === this.#store.binding.agentId &&
|
|
516
|
+
bundle.bootId === work.bootId &&
|
|
517
|
+
bundle.callerKey === work.callerKey &&
|
|
518
|
+
bundle.projectId === work.projectId, "work_snapshot_mismatch", "The work snapshot belongs to a different binding.");
|
|
519
|
+
return bundle;
|
|
520
|
+
}
|
|
521
|
+
load(work) {
|
|
522
|
+
// Accepted runs pass their captured WorkRecord; refresh/close do not retarget it.
|
|
523
|
+
const bundle = this.#bundle(work), project = bundle.view.project;
|
|
524
|
+
return {
|
|
525
|
+
project: project
|
|
526
|
+
? {
|
|
527
|
+
projectId: project.projectId,
|
|
528
|
+
name: project.name,
|
|
529
|
+
root: project.root,
|
|
530
|
+
}
|
|
531
|
+
: null,
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
get(caller, contextId) {
|
|
535
|
+
const work = this.#store.activeWork(contextId, callerKey(caller));
|
|
536
|
+
this.#authorize(caller, { action: "read", work });
|
|
537
|
+
return {
|
|
538
|
+
contextId: work.contextId,
|
|
539
|
+
revision: work.revision,
|
|
540
|
+
...this.#bundle(work).view,
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
close(caller, contextId) {
|
|
544
|
+
const work = this.#store.getWork(contextId);
|
|
545
|
+
this.#authorize(caller, { action: "close", work });
|
|
546
|
+
return this.#store.closeWork(contextId, callerKey(caller));
|
|
547
|
+
}
|
|
548
|
+
async closeProject(caller, projectId, signal) {
|
|
549
|
+
caller = fixedCaller(caller);
|
|
550
|
+
requireCondition(caller.source === "owner_session", "forbidden", "Direct Project close requires the current owner Console session.");
|
|
551
|
+
const { project } = await this.#options.resolveProject(caller, { projectId }, signal);
|
|
552
|
+
signal.throwIfAborted();
|
|
553
|
+
requireCondition(project?.projectId === projectId, "project_not_found", "The current Project policy does not match the close target.");
|
|
554
|
+
this.#authorize(caller, { action: "close", project, path: project.root });
|
|
555
|
+
return this.#store.closeProject(projectId);
|
|
556
|
+
}
|
|
557
|
+
async drain() {
|
|
558
|
+
await Promise.allSettled([...this.#operations]);
|
|
559
|
+
}
|
|
560
|
+
async readMaterial(caller, contextId, itemId, relativePath, signal) {
|
|
561
|
+
caller = fixedCaller(caller);
|
|
562
|
+
signal?.throwIfAborted();
|
|
563
|
+
const work = this.#store.activeWork(contextId, callerKey(caller));
|
|
564
|
+
this.#authorize(caller, { action: "read-material", work });
|
|
565
|
+
const bundle = this.#bundle(work);
|
|
566
|
+
const local = caller.source === "owner_session"
|
|
567
|
+
? bundle.discovered
|
|
568
|
+
: enabledMaterials(bundle.discovered);
|
|
569
|
+
if (bundle.description?.itemId === itemId) {
|
|
570
|
+
requireCondition(!relativePath && work.projectId !== null, "material_path_escape", "A README identity grants only its exact Project file.");
|
|
571
|
+
const description = bundle.description, actual = realpathSync.native(description.path);
|
|
572
|
+
requireCondition(actual === description.path && contains(work.pwd, actual), "material_path_escape", "The README no longer belongs to this Project.");
|
|
573
|
+
const read = await boundedFile(actual, 1024 * 1024, signal);
|
|
574
|
+
requireCondition(read.revision === description.revision, "material_changed", "The README changed after inspection; refresh the work.");
|
|
575
|
+
this.#authorize(caller, { action: "read-material", work });
|
|
576
|
+
return { path: actual, ...read };
|
|
577
|
+
}
|
|
578
|
+
const instructions = [
|
|
579
|
+
...local.instructions,
|
|
580
|
+
...local.nestedInstructions,
|
|
581
|
+
...bundle.published.instructions,
|
|
582
|
+
];
|
|
583
|
+
const instruction = instructions.find((item) => item.instructionId === itemId);
|
|
584
|
+
if (instruction) {
|
|
585
|
+
requireCondition(!relativePath, "material_path_escape", "An instruction grants only its exact file.");
|
|
586
|
+
if (instruction.content !== undefined)
|
|
587
|
+
return {
|
|
588
|
+
path: instruction.path,
|
|
589
|
+
bytes: Buffer.from(instruction.content),
|
|
590
|
+
revision: instruction.origin.revision,
|
|
591
|
+
};
|
|
592
|
+
requireCondition(instruction.path, "material_unavailable", "The instruction source is unavailable.");
|
|
593
|
+
const actual = realpathSync.native(instruction.path);
|
|
594
|
+
requireCondition(actual === instruction.path &&
|
|
595
|
+
(caller.source === "owner_session"
|
|
596
|
+
? instruction.scope.type === "personal" ||
|
|
597
|
+
contains(instruction.scope.root, actual)
|
|
598
|
+
: work.projectId !== null && contains(work.pwd, actual)), "material_path_escape", "The nested instruction no longer belongs to this project.");
|
|
599
|
+
const unchanged = () => {
|
|
600
|
+
if (instruction.origin.type !== "local" ||
|
|
601
|
+
!instruction.origin.revision.startsWith("stat:"))
|
|
602
|
+
return;
|
|
603
|
+
const info = statSync(actual);
|
|
604
|
+
const revision = "stat:" +
|
|
605
|
+
createHash("sha256")
|
|
606
|
+
.update([info.ino, info.size, info.mtimeMs, info.ctimeMs].join(":"))
|
|
607
|
+
.digest("hex");
|
|
608
|
+
requireCondition(revision === instruction.origin.revision, "material_changed", "The instruction changed after discovery; refresh the work before reading it.");
|
|
609
|
+
};
|
|
610
|
+
unchanged();
|
|
611
|
+
const read = await boundedFile(actual, 65536, signal);
|
|
612
|
+
unchanged();
|
|
613
|
+
this.#authorize(caller, { action: "read-material", work });
|
|
614
|
+
return { path: actual, ...read };
|
|
615
|
+
}
|
|
616
|
+
const skill = [...local.skills, ...bundle.published.skills].find((item) => item.skillId === itemId);
|
|
617
|
+
requireCondition(skill, "material_forbidden", "This material is not an enabled, resolved member of the work snapshot.");
|
|
618
|
+
const root = realpathSync.native(skill.path);
|
|
619
|
+
const entry = realpathSync.native(skill.skillFile);
|
|
620
|
+
const actual = realpathSync.native(path.resolve(root, relativePath ?? "SKILL.md"));
|
|
621
|
+
requireCondition(root === skill.path &&
|
|
622
|
+
entry === skill.skillFile &&
|
|
623
|
+
contains(root, entry) &&
|
|
624
|
+
contains(root, actual), "material_path_escape", "A Skill resource must remain inside the resolved winner's root.");
|
|
625
|
+
const metadata = await boundedFile(entry, 1024 * 1024, signal);
|
|
626
|
+
if (skill.origin.type === "local")
|
|
627
|
+
requireCondition(metadata.revision === skill.origin.revision, "material_changed", "The Skill changed after discovery; refresh the work before reading it.");
|
|
628
|
+
const result = actual === entry
|
|
629
|
+
? metadata
|
|
630
|
+
: await boundedFile(actual, 1024 * 1024, signal);
|
|
631
|
+
if (actual !== entry && skill.origin.type === "local")
|
|
632
|
+
requireCondition((await boundedFile(entry, 1024 * 1024, signal)).revision ===
|
|
633
|
+
metadata.revision, "material_changed", "The Skill changed while its resource was being read.");
|
|
634
|
+
this.#authorize(caller, { action: "read-material", work });
|
|
635
|
+
return { path: actual, ...result };
|
|
636
|
+
}
|
|
637
|
+
/** A bounded Skill subtree, identified by the same resolved snapshot winner as body reads. */
|
|
638
|
+
async readMaterialEntries(caller, contextId, itemId, relativePath, signal) {
|
|
639
|
+
caller = fixedCaller(caller);
|
|
640
|
+
signal?.throwIfAborted();
|
|
641
|
+
const work = this.#store.activeWork(contextId, callerKey(caller));
|
|
642
|
+
this.#authorize(caller, { action: "read-material", work });
|
|
643
|
+
const bundle = this.#bundle(work), local = caller.source === "owner_session"
|
|
644
|
+
? bundle.discovered
|
|
645
|
+
: enabledMaterials(bundle.discovered);
|
|
646
|
+
const skill = [...local.skills, ...bundle.published.skills].find((item) => item.skillId === itemId);
|
|
647
|
+
requireCondition(skill, "material_forbidden", "This Skill is not a resolved member available to this caller.");
|
|
648
|
+
const root = realpathSync.native(skill.path), entry = realpathSync.native(skill.skillFile);
|
|
649
|
+
requireCondition(root === skill.path && entry === skill.skillFile && contains(root, entry), "material_path_escape", "The Skill root or entry has redirected.");
|
|
650
|
+
// Reuse the complete entry/revision check before exposing its resource names.
|
|
651
|
+
await this.readMaterial(caller, contextId, itemId, undefined, signal);
|
|
652
|
+
requireCondition(this.#store.activeWork(contextId, callerKey(caller)).revision ===
|
|
653
|
+
work.revision, "context_revision_mismatch", "The work changed before its Skill subtree could be listed.");
|
|
654
|
+
const start = path.resolve(root, relativePath ?? ".");
|
|
655
|
+
requireCondition(contains(root, start) &&
|
|
656
|
+
realpathSync.native(start) === start &&
|
|
657
|
+
lstatSync(start).isDirectory(), "material_path_escape", "A Skill tree must stay inside its canonical root without redirects.");
|
|
658
|
+
const entries = [];
|
|
659
|
+
let bytes = Buffer.byteLength(JSON.stringify({ entries }));
|
|
660
|
+
const visit = async (directory) => {
|
|
661
|
+
signal?.throwIfAborted();
|
|
662
|
+
this.#authorize(caller, { action: "read-material", work });
|
|
663
|
+
requireCondition(realpathSync.native(directory) === directory &&
|
|
664
|
+
contains(root, directory), "material_path_escape", "The Skill subtree escaped its canonical root.");
|
|
665
|
+
const before = lstatSync(directory);
|
|
666
|
+
requireCondition(before.isDirectory(), "material_path_escape", "Skill subtree directories cannot redirect.");
|
|
667
|
+
const children = await readdir(directory, { withFileTypes: true });
|
|
668
|
+
children.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
669
|
+
const after = lstatSync(directory);
|
|
670
|
+
requireCondition(after.isDirectory() &&
|
|
671
|
+
before.dev === after.dev &&
|
|
672
|
+
before.ino === after.ino &&
|
|
673
|
+
realpathSync.native(directory) === directory, "material_changed", "The Skill subtree changed while being listed.");
|
|
674
|
+
for (const child of children) {
|
|
675
|
+
signal?.throwIfAborted();
|
|
676
|
+
const filename = path.join(directory, child.name), current = lstatSync(filename);
|
|
677
|
+
requireCondition(!current.isSymbolicLink() &&
|
|
678
|
+
realpathSync.native(filename) === filename &&
|
|
679
|
+
contains(root, filename), "material_path_escape", "A Skill subtree entry cannot redirect outside the selected tree.");
|
|
680
|
+
requireCondition(current.isFile() || current.isDirectory(), "material_unavailable", "A Skill subtree supports only regular files and directories.");
|
|
681
|
+
const item = {
|
|
682
|
+
displayPath: path.relative(root, filename).split(path.sep).join("/"),
|
|
683
|
+
kind: current.isDirectory()
|
|
684
|
+
? "directory"
|
|
685
|
+
: "file",
|
|
686
|
+
};
|
|
687
|
+
bytes +=
|
|
688
|
+
Buffer.byteLength(JSON.stringify(item)) + (entries.length ? 1 : 0);
|
|
689
|
+
entries.push(item);
|
|
690
|
+
requireCondition(entries.length <= 1000 && bytes <= 64 * 1024, "material_tree_too_large", "The Skill subtree exceeds its 1000-entry or 64-KiB discovery budget.");
|
|
691
|
+
if (current.isDirectory())
|
|
692
|
+
await visit(filename);
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
await visit(start);
|
|
696
|
+
await this.readMaterial(caller, contextId, itemId, undefined, signal);
|
|
697
|
+
requireCondition(this.#store.activeWork(contextId, callerKey(caller)).revision ===
|
|
698
|
+
work.revision, "context_revision_mismatch", "The work changed while its Skill subtree was being listed.");
|
|
699
|
+
this.#authorize(caller, { action: "read-material", work });
|
|
700
|
+
return { entries };
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
//# sourceMappingURL=service.js.map
|