@aiwg/cli 2026.10.15 → 2026.10.18
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/agentic/code/providers/pi/aiwg-bridge.ts +45 -7
- package/dist/src/artifacts/citation-parser.js +7 -6
- package/dist/src/artifacts/corpus-tools/citation-densify.js +4 -2
- package/dist/src/artifacts/corpus-tools/cli.js +8 -3
- package/dist/src/artifacts/corpus-tools/discovery-log.js +26 -24
- package/dist/src/artifacts/corpus-tools/expand-cli.js +3 -2
- package/dist/src/artifacts/corpus-tools/expand-dates.js +107 -0
- package/dist/src/artifacts/corpus-tools/expand-queue.js +4 -1
- package/dist/src/artifacts/corpus-tools/expand-review.js +104 -0
- package/dist/src/artifacts/corpus-tools/expand-state.js +17 -1
- package/dist/src/artifacts/corpus-tools/expand.js +16 -17
- package/dist/src/artifacts/corpus-tools/induction-audit.js +7 -6
- package/dist/src/artifacts/corpus-tools/integrity-scan.js +3 -3
- package/dist/src/artifacts/corpus-tools/radar-shared.js +7 -9
- package/dist/src/artifacts/corpus-tools/ref-files.js +11 -0
- package/dist/src/artifacts/corpus-tools/resolve-cli.js +11 -3
- package/dist/src/artifacts/corpus-tools/resolve.js +42 -11
- package/dist/src/artifacts/corpus-tools/retrieval-lab.js +2 -1
- package/dist/src/artifacts/corpus-tools/sidecar-lint.js +4 -4
- package/dist/src/artifacts/corpus-tools/sidecar-repair.js +2 -1
- package/dist/src/artifacts/corpus-views/ref-parser.js +3 -17
- package/dist/src/artifacts/graph-query.js +5 -3
- package/dist/src/artifacts/hybrid-query.js +11 -2
- package/dist/src/artifacts/index-refresh.js +2 -0
- package/dist/src/artifacts/query-engine-corpus.js +217 -0
- package/dist/src/artifacts/query-engine-discover-output.js +229 -0
- package/dist/src/artifacts/query-engine-discover.js +285 -0
- package/dist/src/artifacts/query-engine-entries.js +188 -0
- package/dist/src/artifacts/query-engine-index.js +249 -0
- package/dist/src/artifacts/query-engine-matching.js +143 -0
- package/dist/src/artifacts/query-engine-options.js +2 -0
- package/dist/src/artifacts/query-engine-rules.js +202 -0
- package/dist/src/artifacts/query-engine-scoring.js +436 -0
- package/dist/src/artifacts/query-engine-show-card.js +194 -0
- package/dist/src/artifacts/query-engine-show-load.js +211 -0
- package/dist/src/artifacts/query-engine-show.js +300 -0
- package/dist/src/artifacts/query-engine.js +7 -2576
- package/dist/src/cli/handlers/daemon.js +17 -1
- package/dist/src/cli/handlers/doctor-args.js +10 -5
- package/dist/src/cli/handlers/doctor-help.js +4 -1
- package/dist/src/cli/handlers/index.js +2 -2
- package/dist/src/cli/handlers/mc-agents.js +111 -0
- package/dist/src/cli/handlers/mc-args.js +54 -0
- package/dist/src/cli/handlers/mc-control.js +227 -0
- package/dist/src/cli/handlers/mc-dispatch.js +203 -0
- package/dist/src/cli/handlers/mc-help.js +171 -0
- package/dist/src/cli/handlers/mc-run.js +343 -0
- package/dist/src/cli/handlers/mc-session.js +138 -0
- package/dist/src/cli/handlers/mc-status.js +258 -0
- package/dist/src/cli/handlers/mc-summary.js +43 -0
- package/dist/src/cli/handlers/mc-types.js +3 -0
- package/dist/src/cli/handlers/mc-worker.js +205 -0
- package/dist/src/cli/handlers/mc.js +12 -1665
- package/dist/src/cli/handlers/models-refusal.js +13 -0
- package/dist/src/cli/handlers/models-resource-sample.js +10 -0
- package/dist/src/cli/handlers/models.js +5 -3
- package/dist/src/cli/handlers/regenerate.js +8 -16
- package/dist/src/cli/handlers/remove-bundled.js +133 -0
- package/dist/src/cli/handlers/remove-options.js +31 -0
- package/dist/src/cli/handlers/remove-user.js +174 -0
- package/dist/src/cli/handlers/remove.js +201 -0
- package/dist/src/cli/handlers/sessions.js +7 -7
- package/dist/src/cli/handlers/subcommands-bundles.js +257 -0
- package/dist/src/cli/handlers/subcommands-discovery.js +165 -0
- package/dist/src/cli/handlers/subcommands-list-format.js +129 -0
- package/dist/src/cli/handlers/subcommands-list.js +250 -0
- package/dist/src/cli/handlers/subcommands-plugins.js +213 -0
- package/dist/src/cli/handlers/subcommands-quickref.js +58 -0
- package/dist/src/cli/handlers/subcommands-rlm.js +113 -0
- package/dist/src/cli/handlers/subcommands-services.js +66 -0
- package/dist/src/cli/handlers/subcommands-storage.js +259 -0
- package/dist/src/cli/handlers/subcommands.js +18 -1976
- package/dist/src/cli/handlers/update.js +157 -0
- package/dist/src/cli/handlers/use.js +14 -4
- package/dist/src/cli/handlers/utilities.js +3 -560
- package/dist/src/cli/handlers/utility-doctor.js +412 -0
- package/dist/src/cli/scope-resolver.js +1 -142
- package/dist/src/cli/services/deployment-verification.js +0 -1
- package/dist/src/cli/user-scope-mirror.js +173 -0
- package/dist/src/config/aiwg-config.js +2 -1
- package/dist/src/config/authorization-cli.js +7 -3
- package/dist/src/config/dispatch-host-thresholds.js +2 -0
- package/dist/src/config/dispatch-thresholds-runtime.d.mts +3 -2
- package/dist/src/config/dispatch-thresholds-runtime.mjs +21 -7
- package/dist/src/config/framework-aliases.js +12 -0
- package/dist/src/config/host-resource-report.d.mts +24 -0
- package/dist/src/config/host-resource-report.mjs +110 -0
- package/dist/src/config/host-resource-runtime.d.mts +37 -0
- package/dist/src/config/host-resource-runtime.mjs +296 -0
- package/dist/src/config/installed-selection.js +22 -0
- package/dist/src/config/worker-invocation-marker.d.mts +1 -0
- package/dist/src/config/worker-invocation-marker.mjs +2 -0
- package/dist/src/decision/ensemble-study/artifacts.js +7 -0
- package/dist/src/decision/ensemble-study/gate-policy.js +58 -0
- package/dist/src/decision/ensemble-study/gates.js +78 -0
- package/dist/src/decision/ensemble-study/protocol.js +18 -21
- package/dist/src/decision/ensemble-study/score.js +3 -2
- package/dist/src/decision/ensemble-study/statistics.js +7 -26
- package/dist/src/decision/heldout/access.js +74 -0
- package/dist/src/decision/heldout/calibration.js +1 -0
- package/dist/src/decision/heldout/collector.js +8 -1
- package/dist/src/decision/sensitivity/comparative-gates.js +135 -0
- package/dist/src/decision/sensitivity/comparative.js +18 -41
- package/dist/src/effects/verifiers/file.js +22 -5
- package/dist/src/extensions/claude-attribution.js +82 -0
- package/dist/src/extensions/claude-hooks-installer.js +133 -69
- package/dist/src/extensions/commands/daemon.js +109 -0
- package/dist/src/extensions/commands/definitions.js +5 -89
- package/dist/src/gates/evaluate.js +2 -358
- package/dist/src/gates/heldout.js +17 -7
- package/dist/src/gates/observations.js +363 -0
- package/dist/src/gates/pack-composition.js +270 -0
- package/dist/src/gates/pack-tightening.js +197 -0
- package/dist/src/gates/pack-validation.js +262 -0
- package/dist/src/gates/registry-error.js +5 -0
- package/dist/src/gates/registry.js +13 -685
- package/dist/src/lint/check-patterns.js +163 -0
- package/dist/src/lint/checks.js +268 -0
- package/dist/src/lint/reference-resolution.js +54 -0
- package/dist/src/lint/runner.js +3 -465
- package/dist/src/mcp/helpers.mjs +11 -1
- package/dist/src/mcp/json-document.mjs +29 -0
- package/dist/src/mcp/muse-native-config.mjs +1 -28
- package/dist/src/memory/project-registry.js +44 -4
- package/dist/src/mission-protocol/worker-review-gate.js +7 -0
- package/dist/src/models/worker-plan.js +36 -5
- package/dist/src/models/wrapper-deployment.js +25 -12
- package/dist/src/models/wrapper-enforcement.js +47 -7
- package/dist/src/network-analysis/research.js +2 -1
- package/dist/src/plugin/command-ownership.js +34 -0
- package/dist/src/plugin/skill-command-frontmatter.js +197 -0
- package/dist/src/plugin/skill-command-translator.js +6 -232
- package/dist/src/policy/hitl-check.js +14 -4
- package/dist/src/policy/hitl-consume.js +54 -0
- package/dist/src/policy/hitl-receipt.js +2 -0
- package/dist/src/policy/hitl.js +11 -3
- package/dist/src/providers/context-output-ownership.js +26 -0
- package/dist/src/providers/deployment-ownership.d.mts +19 -0
- package/dist/src/providers/deployment-ownership.mjs +226 -0
- package/dist/src/providers/deployment-removal.js +200 -0
- package/dist/src/providers/provider-context-contracts.js +162 -0
- package/dist/src/providers/provider-definition-schema.js +136 -0
- package/dist/src/providers/provider-definitions.js +8 -1298
- package/dist/src/providers/provider-seeds-1.js +360 -0
- package/dist/src/providers/provider-seeds-2.js +387 -0
- package/dist/src/providers/provider-seeds-3.js +262 -0
- package/dist/src/providers/removal-transaction.js +86 -0
- package/dist/src/providers/settings-document.d.mts +3 -0
- package/dist/src/providers/settings-document.mjs +47 -0
- package/dist/src/providers/settings-removal.js +190 -0
- package/dist/src/providers/skill-ownership.d.mts +19 -2
- package/dist/src/providers/skill-ownership.mjs +12 -4
- package/dist/src/providers/transformation-receipt-integration.js +15 -271
- package/dist/src/providers/transformation-receipt-outputs.js +99 -0
- package/dist/src/providers/transformation-receipt-source.js +194 -0
- package/dist/src/research/clients/arxiv.js +4 -30
- package/dist/src/research/clients/base.js +48 -22
- package/dist/src/research/identity.js +42 -4
- package/dist/src/research/query-cli.js +3 -87
- package/dist/src/research/query-options.js +87 -0
- package/dist/src/research/reference-match.js +105 -24
- package/dist/src/research/services/acquisition.js +9 -7
- package/dist/src/research/services/citation.js +5 -2
- package/dist/src/research/types.js +2 -0
- package/dist/src/rules/activation.mjs +15 -6
- package/dist/src/rules/core-startup.mjs +2 -2
- package/dist/src/serve/mission-conductor.js +52 -4
- package/dist/src/serve/mission-summary.js +56 -0
- package/dist/src/sessions/adapters/grok-build-native.js +23 -0
- package/dist/src/sessions/adapters/grok-build.js +14 -7
- package/dist/src/sessions/workspace-discovery.js +5 -5
- package/dist/src/smiths/context-pipeline/aiwg-md.js +39 -46
- package/dist/src/smiths/context-pipeline/claude-hook.js +1 -1
- package/dist/src/smiths/context-pipeline/code-regions.js +28 -0
- package/dist/src/smiths/context-pipeline/context-file-safety.js +3 -0
- package/dist/src/smiths/context-pipeline/finalization.js +81 -51
- package/dist/src/smiths/context-pipeline/generator.js +10 -11
- package/dist/src/smiths/context-pipeline/managed-hook.js +1 -1
- package/dist/src/smiths/context-pipeline/muse-bridge.js +2 -2
- package/dist/src/smiths/context-pipeline/provider-policy.js +6 -12
- package/dist/src/smiths/context-pipeline/workspace-audit.js +299 -0
- package/dist/src/smiths/context-pipeline/workspace-bootstrap.js +148 -0
- package/dist/src/smiths/context-pipeline/workspace-context-shared.js +111 -0
- package/dist/src/smiths/context-pipeline/workspace-context.js +8 -1373
- package/dist/src/smiths/context-pipeline/workspace-doctor.js +192 -0
- package/dist/src/smiths/context-pipeline/workspace-document.js +201 -0
- package/dist/src/smiths/context-pipeline/workspace-extraction.js +205 -0
- package/dist/src/smiths/context-pipeline/workspace-migration.js +250 -0
- package/dist/src/smiths/hook-bridge/index.js +9 -0
- package/package.json +13 -1
- package/schemas/decision/D17StudyProtocol.v1.schema.json +39 -67
- package/schemas/decision/D29Study.v10.schema.json +65 -0
- package/tools/agents/deploy-agents.mjs +24 -675
- package/tools/agents/deploy-command-mirrors.mjs +229 -0
- package/tools/agents/deploy-options.mjs +258 -0
- package/tools/agents/deploy-support.mjs +165 -0
- package/tools/agents/providers/base-addon-discovery.mjs +139 -0
- package/tools/agents/providers/base-behaviors.mjs +168 -0
- package/tools/agents/providers/base-context.mjs +205 -0
- package/tools/agents/providers/base-contract.mjs +116 -0
- package/tools/agents/providers/base-files.mjs +252 -0
- package/tools/agents/providers/base-flat-artifacts.mjs +233 -0
- package/tools/agents/providers/base-framework-discovery.mjs +309 -0
- package/tools/agents/providers/base-frontmatter.mjs +196 -0
- package/tools/agents/providers/base-inventory.mjs +269 -0
- package/tools/agents/providers/base-kernel-skills.mjs +354 -0
- package/tools/agents/providers/base-migrations.mjs +258 -0
- package/tools/agents/providers/base-models-platforms.mjs +273 -0
- package/tools/agents/providers/base-rule-index.mjs +450 -0
- package/tools/agents/providers/base-runtime.mjs +60 -0
- package/tools/agents/providers/base-skill-copy.mjs +273 -0
- package/tools/agents/providers/base.mjs +16 -3364
- package/tools/agents/providers/claude-post-deploy.mjs +20 -1
- package/tools/agents/providers/context-write.mjs +4 -0
- package/tools/agents/providers/hermes-startup.mjs +0 -5
- package/tools/agents/providers/muse.mjs +7 -2
- package/tools/agents/providers/pi.mjs +3 -1
- package/tools/agents/providers/rule-delivery-surface.mjs +359 -0
- package/tools/agents/providers/rule-delivery.mjs +20 -1144
- package/tools/agents/providers/rule-deployment-commit.mjs +148 -0
- package/tools/agents/providers/rule-deployment-prepare.mjs +461 -0
- package/tools/agents/providers/rule-deployment-state.mjs +300 -0
- package/tools/agents/providers/rule-projection.mjs +3 -0
- package/tools/daemon/README.md +511 -0
- package/tools/daemon/adapters/base-adapter.mjs +84 -0
- package/tools/daemon/adapters/webhook-adapter.mjs +107 -0
- package/tools/daemon/agent-supervisor.mjs +299 -0
- package/tools/daemon/automation-engine.mjs +310 -0
- package/tools/daemon/autonomous-engine.mjs +346 -0
- package/tools/daemon/behavior-loader.mjs +316 -0
- package/tools/daemon/behavior.mjs +174 -0
- package/tools/daemon/concierge/intent-router.mjs +401 -0
- package/tools/daemon/concierge/orchestrator.mjs +272 -0
- package/tools/daemon/concierge/response-translator.mjs +462 -0
- package/tools/daemon/config.mjs +377 -0
- package/tools/daemon/cron-scheduler.mjs +178 -0
- package/tools/daemon/daemon-main.mjs +1169 -0
- package/tools/daemon/event-router.mjs +84 -0
- package/tools/daemon/file-watcher.mjs +383 -0
- package/tools/daemon/index.mjs +1181 -0
- package/tools/daemon/init-profile.mjs +124 -0
- package/tools/daemon/ipc-client.mjs +197 -0
- package/tools/daemon/ipc-ops-handlers.mjs +236 -0
- package/tools/daemon/ipc-server.mjs +275 -0
- package/tools/daemon/maintenance-owner.mjs +79 -0
- package/tools/daemon/memory-manager.mjs +386 -0
- package/tools/daemon/message-router.mjs +194 -0
- package/tools/daemon/provider-watcher.mjs +585 -0
- package/tools/daemon/pty-adapter.mjs +420 -0
- package/tools/daemon/repl-chat.mjs +359 -0
- package/tools/daemon/resource-bounds.mjs +88 -0
- package/tools/daemon/sandbox-transport.mjs +295 -0
- package/tools/daemon/scheduled-task-runner.mjs +535 -0
- package/tools/daemon/task-store.mjs +450 -0
- package/tools/daemon/tmux-manager.mjs +324 -0
- package/tools/daemon/update-policy.mjs +517 -0
- package/tools/daemon/web-server.mjs +399 -0
- package/tools/daemon/web-ui/index.html +585 -0
- package/tools/daemon/web-ui/styles.css +156 -0
- package/tools/docker/Dockerfile.daemon +35 -0
- package/tools/docker/container-manager.mjs +291 -0
- package/tools/messaging/adapter-registry.mjs +230 -0
- package/tools/messaging/adapters/base.mjs +326 -0
- package/tools/messaging/adapters/discord.mjs +462 -0
- package/tools/messaging/adapters/slack.mjs +402 -0
- package/tools/messaging/adapters/telegram.mjs +552 -0
- package/tools/messaging/chat-handler.mjs +297 -0
- package/tools/messaging/command-router.mjs +269 -0
- package/tools/messaging/event-bus.mjs +199 -0
- package/tools/messaging/index.mjs +456 -0
- package/tools/messaging/message-formatter.mjs +320 -0
- package/tools/messaging/room-manager.mjs +277 -0
- package/tools/messaging/types.mjs +94 -0
- package/tools/providers/pi-bridge-policy.mjs +4 -0
- package/tools/ralph-external/daemon-supervisor.mjs +394 -0
- package/tools/ralph-external/lib/behavior-registry.mjs +270 -0
- package/tools/ralph-external/lib/worker-invocation-processes.mjs +7 -4
- package/tools/ralph-external/lib/worker-isolation-args.mjs +217 -0
- package/tools/ralph-external/lib/worker-isolation-status.mjs +19 -0
- package/tools/ralph-external/lib/worker-isolation.mjs +11 -237
- package/tools/ralph-external/lib/worker-overflow.mjs +10 -2
- package/tools/ralph-external/lib/worker-termination.mjs +15 -8
- package/tools/ralph-external/lib/worker-unit-cleanup.mjs +72 -0
- package/tools/ralph-external/session-launcher.mjs +7 -7
- package/tools/scripts/resource-preflight.mjs +3 -242
- package/vscode-extension/schemas/aiwg.config.v1.json +2 -4
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
// Own immutable input pools and serializable deployment plan state.
|
|
2
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { resolveAiwgRoot } from './base.mjs';
|
|
7
|
+
import { fileDigest, resolveProjectAiwgDir, trackTemporaryDirectory } from './rule-delivery-surface.mjs';
|
|
8
|
+
|
|
9
|
+
// Plans contain only JSON values and a scratch directory. The preparing process
|
|
10
|
+
// owns that directory until disposal; cross-process transfer needs a separate lease.
|
|
11
|
+
export const stagedCleanups = new Map();
|
|
12
|
+
export class RuleDeploymentStaleError extends Error {
|
|
13
|
+
constructor(message, paths = []) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = 'RuleDeploymentStaleError';
|
|
16
|
+
this.code = 'AIWG_RULE_DEPLOYMENT_STALE';
|
|
17
|
+
this.paths = paths;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export function capturePreimage(file, ancestors = new Set()) {
|
|
21
|
+
let stat;
|
|
22
|
+
try { stat = fs.lstatSync(file); } catch (error) {
|
|
23
|
+
if (error.code === 'ENOENT') return { kind: 'missing' };
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
const mode = stat.mode & 0o7777;
|
|
27
|
+
if (stat.isSymbolicLink()) {
|
|
28
|
+
const target = fs.readlinkSync(file);
|
|
29
|
+
const resolved = path.resolve(path.dirname(file), target);
|
|
30
|
+
if (ancestors.has(resolved)) throw new Error(`Cyclic deployment preimage: ${file}`);
|
|
31
|
+
return { kind: 'symlink', mode, target, resolved: capturePreimage(resolved, new Set([...ancestors, resolved])) };
|
|
32
|
+
}
|
|
33
|
+
if (stat.isFile()) return { kind: 'file', mode, digest: fileDigest(file) };
|
|
34
|
+
if (stat.isDirectory()) return { kind: 'directory', mode, entries: fs.readdirSync(file).sort().map(name => [name, capturePreimage(path.join(file, name), new Set([...ancestors, path.resolve(file)]))]) };
|
|
35
|
+
throw new Error(`Unsupported deployment preimage: ${file}`);
|
|
36
|
+
}
|
|
37
|
+
// cpSync materializes links; compare its output with the recursively resolved
|
|
38
|
+
// content while retaining the original link-shaped preimage for commit drift.
|
|
39
|
+
export function materializeInput(from, to, ancestors = new Set(), parentReady = false) {
|
|
40
|
+
let stat;
|
|
41
|
+
try { stat = fs.statSync(from); } catch (error) {
|
|
42
|
+
if (error.code === 'ENOENT') return;
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
if (stat.isDirectory()) {
|
|
46
|
+
// Only directories can lead back to an ancestor. Resolve them once and
|
|
47
|
+
// share the immutable ancestry set with their children, including aliases.
|
|
48
|
+
const real = fs.realpathSync(from);
|
|
49
|
+
if (ancestors.has(real)) throw new Error(`Cyclic deployment input: ${from}`);
|
|
50
|
+
const children = new Set([...ancestors, real]);
|
|
51
|
+
fs.mkdirSync(to, { recursive: true });
|
|
52
|
+
for (const name of fs.readdirSync(from)) materializeInput(path.join(from, name), path.join(to, name), children, true);
|
|
53
|
+
fs.chmodSync(to, stat.mode & 0o7777);
|
|
54
|
+
} else if (stat.isFile()) {
|
|
55
|
+
if (!parentReady) fs.mkdirSync(path.dirname(to), { recursive: true });
|
|
56
|
+
fs.copyFileSync(from, to); fs.chmodSync(to, stat.mode & 0o7777);
|
|
57
|
+
} else throw new Error(`Unsupported deployment input: ${from}`);
|
|
58
|
+
}
|
|
59
|
+
export function materializedPreimage(state) {
|
|
60
|
+
if (state.kind === 'symlink') return materializedPreimage(state.resolved);
|
|
61
|
+
if (state.kind === 'directory') return { ...state, entries: state.entries.map(([name, child]) => [name, materializedPreimage(child)]) };
|
|
62
|
+
return state;
|
|
63
|
+
}
|
|
64
|
+
export function snapshotPreimages(paths) {
|
|
65
|
+
return [...new Set(paths.map(file => path.resolve(file)))].map(file => ({ path: file, state: capturePreimage(file) }));
|
|
66
|
+
}
|
|
67
|
+
export function planSeal(plan) { return createHash('sha256').update(JSON.stringify(plan)).digest('hex'); }
|
|
68
|
+
export function planOwner(plan) {
|
|
69
|
+
const owner = stagedCleanups.get(plan?.lease);
|
|
70
|
+
return owner && owner.seal === planSeal(plan) ? owner : null;
|
|
71
|
+
}
|
|
72
|
+
export function disposeRuleDeploymentPlan(plan) {
|
|
73
|
+
const owner = planOwner(plan);
|
|
74
|
+
if (owner) { stagedCleanups.delete(plan.lease); owner.cleanup(); }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Release process-local ownership while retaining a coordinator-owned stage. */
|
|
78
|
+
export function detachRuleDeploymentPlan(plan) {
|
|
79
|
+
const owner = planOwner(plan);
|
|
80
|
+
if (!owner || !owner.transferable) throw new Error('Rule deployment plan is not transferable');
|
|
81
|
+
stagedCleanups.delete(plan.lease);
|
|
82
|
+
owner.detach();
|
|
83
|
+
return plan;
|
|
84
|
+
}
|
|
85
|
+
export function captureAnchors(paths) {
|
|
86
|
+
const names = new Set();
|
|
87
|
+
for (const name of paths) {
|
|
88
|
+
let current = path.resolve(name);
|
|
89
|
+
for (;;) {
|
|
90
|
+
names.add(current);
|
|
91
|
+
const parent = path.dirname(current);
|
|
92
|
+
if (parent === current) break;
|
|
93
|
+
current = parent;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return [...names].sort().map(file => {
|
|
97
|
+
try {
|
|
98
|
+
const stat = fs.lstatSync(file, { bigint: true });
|
|
99
|
+
return { path: file, dev: String(stat.dev), ino: String(stat.ino), mode: String(stat.mode),
|
|
100
|
+
link: stat.isSymbolicLink() ? fs.readlinkSync(file) : null, real: fs.realpathSync(file) };
|
|
101
|
+
} catch (error) {
|
|
102
|
+
if (error.code === 'ENOENT') return { path: file, missing: true };
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Opaque coordinator-owned input pool. A plan retains a lease until committed or
|
|
109
|
+
// disposed, so disposing the creator's handle cannot invalidate active stages.
|
|
110
|
+
export const inputPools = new WeakMap();
|
|
111
|
+
// Executable links are identity-bound but their installed code is trusted host
|
|
112
|
+
// state. Data files, modes, directory membership, and links are sealed in full.
|
|
113
|
+
export function poolInventory(file) {
|
|
114
|
+
let stat;
|
|
115
|
+
try { stat = fs.lstatSync(file); } catch (error) {
|
|
116
|
+
if (error.code === 'ENOENT') return { kind: 'missing' };
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
const mode = stat.mode & 0o7777;
|
|
120
|
+
if (stat.isSymbolicLink()) return { kind: 'symlink', mode, target: fs.readlinkSync(file) };
|
|
121
|
+
if (stat.isFile()) return { kind: 'file', mode, digest: fileDigest(file) };
|
|
122
|
+
if (stat.isDirectory()) return { kind: 'directory', mode, entries: fs.readdirSync(file).sort().map(name => [name, poolInventory(path.join(file, name))]) };
|
|
123
|
+
throw new Error(`Unsupported shared input pool entry: ${file}`);
|
|
124
|
+
}
|
|
125
|
+
export function setPoolWritable(file, writable) {
|
|
126
|
+
let stat;
|
|
127
|
+
try { stat = fs.lstatSync(file); } catch (error) { if (error.code === 'ENOENT') return; throw error; }
|
|
128
|
+
if (stat.isSymbolicLink()) return;
|
|
129
|
+
if (stat.isDirectory() && writable) fs.chmodSync(file, (stat.mode & 0o7777) | 0o700);
|
|
130
|
+
if (stat.isDirectory()) for (const name of fs.readdirSync(file)) setPoolWritable(path.join(file, name), writable);
|
|
131
|
+
if (!writable) fs.chmodSync(file, (stat.mode & 0o7777) & ~0o222);
|
|
132
|
+
}
|
|
133
|
+
export function verifyInputPool(pool) {
|
|
134
|
+
try {
|
|
135
|
+
if (planSeal(poolInventory(pool.snapshotRoot)) === pool.inventorySeal &&
|
|
136
|
+
JSON.stringify(captureAnchors(pool.execAnchors.map(anchor => anchor.path))) === JSON.stringify(pool.execAnchors)) return;
|
|
137
|
+
} catch (error) { throw new Error(`Shared input pool changed: ${error.message}`); }
|
|
138
|
+
throw new Error('Shared input pool changed');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function createRuleDeploymentInputPool(srcRoot) {
|
|
142
|
+
const corpusRoot = resolveAiwgRoot(srcRoot) || srcRoot;
|
|
143
|
+
const sourceTree = fs.existsSync(path.join(corpusRoot, 'agentic', 'code')) ? path.join(corpusRoot, 'agentic', 'code') : srcRoot;
|
|
144
|
+
const isCorpusTree = path.relative(corpusRoot, sourceTree) === path.join('agentic', 'code');
|
|
145
|
+
const paths = [sourceTree, path.join(corpusRoot, 'prebuilt', 'framework-relations'), path.join(corpusRoot, 'package.json'), path.join(corpusRoot, 'schemas')];
|
|
146
|
+
const preimages = snapshotPreimages(paths);
|
|
147
|
+
const temp = fs.mkdtempSync(path.join(os.tmpdir(), 'aiwg-rule-input-'));
|
|
148
|
+
const cleanup = trackTemporaryDirectory(temp);
|
|
149
|
+
try {
|
|
150
|
+
const snapshotRoot = path.join(temp, 'input');
|
|
151
|
+
const snapshotTree = isCorpusTree ? path.join(snapshotRoot, 'agentic', 'code') : snapshotRoot;
|
|
152
|
+
materializeInput(sourceTree, snapshotTree);
|
|
153
|
+
if (JSON.stringify(capturePreimage(snapshotTree)) !== JSON.stringify(materializedPreimage(preimages[0].state))) throw new Error('Source changed while preparing immutable snapshot');
|
|
154
|
+
const trustedExec = [];
|
|
155
|
+
if (isCorpusTree) {
|
|
156
|
+
for (const rel of ['package.json', 'prebuilt/framework-relations', 'schemas']) {
|
|
157
|
+
const from = path.join(corpusRoot, rel), to = path.join(snapshotRoot, rel);
|
|
158
|
+
materializeInput(from, to);
|
|
159
|
+
const expected = preimages.find(item => item.path === path.resolve(from));
|
|
160
|
+
if (JSON.stringify(capturePreimage(to)) !== JSON.stringify(materializedPreimage(expected.state))) throw new Error(`Package input changed while preparing snapshot: ${from}`);
|
|
161
|
+
}
|
|
162
|
+
for (const rel of ['tools', 'dist', 'node_modules', 'src']) {
|
|
163
|
+
const from = path.join(corpusRoot, rel), to = path.join(snapshotRoot, rel);
|
|
164
|
+
if (fs.existsSync(from)) { fs.symlinkSync(from, to, 'dir'); trustedExec.push(from); }
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
setPoolWritable(snapshotRoot, false);
|
|
168
|
+
const inventorySeal = planSeal(poolInventory(snapshotRoot));
|
|
169
|
+
const execAnchors = captureAnchors(trustedExec);
|
|
170
|
+
const safeCleanup = () => { setPoolWritable(snapshotRoot, true); cleanup(); };
|
|
171
|
+
const token = Object.freeze({});
|
|
172
|
+
inputPools.set(token, { corpusRoot: path.resolve(corpusRoot), sourceTree: path.resolve(sourceTree), snapshotRoot, snapshotTree, isCorpusTree, preimages, inventorySeal, execAnchors, refs: 1, closed: false, cleanup: safeCleanup, transferable: true });
|
|
173
|
+
return token;
|
|
174
|
+
} catch (error) { setPoolWritable(path.join(temp, 'input'), true); cleanup(); throw error; }
|
|
175
|
+
}
|
|
176
|
+
export function releaseInputPool(pool) { if (--pool.refs === 0) pool.cleanup(); }
|
|
177
|
+
export function disposeRuleDeploymentInputPool(token) {
|
|
178
|
+
const pool = inputPools.get(token);
|
|
179
|
+
if (pool && !pool.closed) { pool.closed = true; releaseInputPool(pool); }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** A descriptor contains immutable snapshot identity, never its cleanup lease. */
|
|
183
|
+
export function describeRuleDeploymentInputPool(token) {
|
|
184
|
+
const pool = inputPools.get(token);
|
|
185
|
+
if (!pool || pool.closed || !pool.transferable) throw new Error('Invalid or closed rule deployment input pool');
|
|
186
|
+
verifyInputPool(pool);
|
|
187
|
+
return {
|
|
188
|
+
version: 1,
|
|
189
|
+
corpusRoot: pool.corpusRoot,
|
|
190
|
+
sourceTree: pool.sourceTree,
|
|
191
|
+
snapshotRoot: pool.snapshotRoot,
|
|
192
|
+
snapshotTree: pool.snapshotTree,
|
|
193
|
+
isCorpusTree: pool.isCorpusTree,
|
|
194
|
+
inventorySeal: pool.inventorySeal,
|
|
195
|
+
execAnchors: pool.execAnchors,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Import a read-only pool view in a preparation worker. The coordinator keeps
|
|
200
|
+
* the only cleanup lease and later restores the live-source preimages. */
|
|
201
|
+
export function importRuleDeploymentInputPool(descriptor) {
|
|
202
|
+
if (!descriptor || descriptor.version !== 1 || typeof descriptor.inventorySeal !== 'string' ||
|
|
203
|
+
!Array.isArray(descriptor.execAnchors)) throw new Error('Invalid rule deployment input pool descriptor');
|
|
204
|
+
for (const key of ['corpusRoot', 'sourceTree', 'snapshotRoot', 'snapshotTree']) {
|
|
205
|
+
if (typeof descriptor[key] !== 'string' || !path.isAbsolute(descriptor[key])) throw new Error('Invalid rule deployment input pool descriptor');
|
|
206
|
+
}
|
|
207
|
+
const token = Object.freeze({});
|
|
208
|
+
const pool = { ...descriptor, corpusRoot: path.resolve(descriptor.corpusRoot), sourceTree: path.resolve(descriptor.sourceTree),
|
|
209
|
+
snapshotRoot: path.resolve(descriptor.snapshotRoot), snapshotTree: path.resolve(descriptor.snapshotTree), preimages: [],
|
|
210
|
+
refs: 1, closed: false, cleanup: () => {}, transferable: false };
|
|
211
|
+
verifyInputPool(pool);
|
|
212
|
+
inputPools.set(token, pool);
|
|
213
|
+
return token;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function within(root, file) {
|
|
217
|
+
const relative = path.relative(path.resolve(root), path.resolve(file));
|
|
218
|
+
return relative === '' || (!relative.startsWith(`..${path.sep}`) && relative !== '..' && !path.isAbsolute(relative));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function safeRelative(root, name) {
|
|
222
|
+
return typeof name === 'string' && name !== '' && name !== '.' && path.normalize(name) === name && !path.isAbsolute(name) && within(root, path.join(root, name));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function exactObjectKeys(value, keys) {
|
|
226
|
+
return value && typeof value === 'object' && !Array.isArray(value) &&
|
|
227
|
+
JSON.stringify(Object.keys(value).sort()) === JSON.stringify([...keys].sort());
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export function validPreimageState(state) {
|
|
231
|
+
if (!state || typeof state !== 'object' || Array.isArray(state) || typeof state.kind !== 'string') return false;
|
|
232
|
+
if (state.kind === 'missing') return Object.keys(state).length === 1;
|
|
233
|
+
if (!Number.isSafeInteger(state.mode) || state.mode < 0) return false;
|
|
234
|
+
if (state.kind === 'file') return Object.keys(state).length === 3 && /^[a-f0-9]{64}$/.test(state.digest);
|
|
235
|
+
if (state.kind === 'symlink') return Object.keys(state).length === 4 && typeof state.target === 'string' && validPreimageState(state.resolved);
|
|
236
|
+
return state.kind === 'directory' && Object.keys(state).length === 3 && Array.isArray(state.entries) && state.entries.every(entry =>
|
|
237
|
+
Array.isArray(entry) && entry.length === 2 && typeof entry[0] === 'string' && entry[0] !== '' && !entry[0].includes(path.sep) && validPreimageState(entry[1]));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function expectedAnchorPaths(paths) {
|
|
241
|
+
const names = new Set();
|
|
242
|
+
for (const name of paths) {
|
|
243
|
+
let current = path.resolve(name);
|
|
244
|
+
for (;;) {
|
|
245
|
+
names.add(current);
|
|
246
|
+
const parent = path.dirname(current);
|
|
247
|
+
if (parent === current) break;
|
|
248
|
+
current = parent;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return [...names].sort();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Adopt a detached worker plan into the coordinator's process-local lease. */
|
|
255
|
+
export function adoptRuleDeploymentPlan(detached, { scratch, target, inputPool, cleanup }) {
|
|
256
|
+
const invalid = () => { throw new Error('Invalid transferred rule deployment plan'); };
|
|
257
|
+
if (!exactObjectKeys(detached, ['version', 'lease', 'temp', 'target', 'stage', 'rootNames', 'workspaceDirs', 'preimages', 'poolSeal', 'anchors', 'snapshotRoot', 'corpusRoot', 'sourceMappings', 'staged', 'report', 'mappings', 'contextWrites']) ||
|
|
258
|
+
detached.version !== 1 || typeof detached.lease !== 'string' || !(detached.poolSeal === null || typeof detached.poolSeal === 'string') ||
|
|
259
|
+
!detached.report || typeof detached.report !== 'object' || Array.isArray(detached.report) || typeof scratch !== 'string' || typeof target !== 'string' || typeof cleanup !== 'function') invalid();
|
|
260
|
+
const root = path.resolve(scratch), expectedTarget = path.resolve(target);
|
|
261
|
+
if (path.resolve(detached.temp || '') !== root || path.resolve(detached.stage || '') !== path.join(root, 'project') || path.resolve(detached.target || '') !== expectedTarget) invalid();
|
|
262
|
+
if (!Array.isArray(detached.contextWrites) || detached.contextWrites.some(file => typeof file !== 'string' || !within(root, file)) || !Array.isArray(detached.staged) || !Array.isArray(detached.mappings) || !Array.isArray(detached.rootNames) || !Array.isArray(detached.preimages) ||
|
|
263
|
+
!Array.isArray(detached.workspaceDirs) || !Array.isArray(detached.anchors) || !Array.isArray(detached.sourceMappings) ||
|
|
264
|
+
typeof detached.snapshotRoot !== 'string' || !path.isAbsolute(detached.snapshotRoot) || typeof detached.corpusRoot !== 'string' || !path.isAbsolute(detached.corpusRoot)) invalid();
|
|
265
|
+
if (detached.rootNames.some(name => !safeRelative(expectedTarget, name))) invalid();
|
|
266
|
+
const pool = inputPool && inputPools.get(inputPool);
|
|
267
|
+
if (inputPool && (!pool || pool.closed || !pool.transferable)) invalid();
|
|
268
|
+
if (pool) {
|
|
269
|
+
verifyInputPool(pool);
|
|
270
|
+
if (detached.poolSeal !== pool.inventorySeal || path.resolve(detached.snapshotRoot || '') !== pool.snapshotRoot) invalid();
|
|
271
|
+
} else if (detached.poolSeal) invalid();
|
|
272
|
+
const expectedArtifactRoot = path.resolve(resolveProjectAiwgDir(expectedTarget));
|
|
273
|
+
const validSourceMapping = item => within(root, item.to) || (pool && within(pool.snapshotRoot, item.to)) ||
|
|
274
|
+
(path.resolve(item.from) === path.join(expectedTarget, '.aiwg') && path.resolve(item.to) === expectedArtifactRoot);
|
|
275
|
+
if (detached.workspaceDirs.some(name => !safeRelative(expectedTarget, name)) ||
|
|
276
|
+
detached.preimages.some(item => !item || Object.keys(item).length !== 2 || typeof item.path !== 'string' || !path.isAbsolute(item.path) || !validPreimageState(item.state)) ||
|
|
277
|
+
detached.staged.some(item => !item || Object.keys(item).length !== 2 || typeof item.path !== 'string' || !within(root, item.path) || !validPreimageState(item.state)) ||
|
|
278
|
+
detached.sourceMappings.some(item => !item || Object.keys(item).length !== 2 || typeof item.from !== 'string' || !path.isAbsolute(item.from) ||
|
|
279
|
+
typeof item.to !== 'string' || !path.isAbsolute(item.to) || !validSourceMapping(item)) ||
|
|
280
|
+
detached.mappings.some(item => !item || Object.keys(item).length !== 3 || typeof item.from !== 'string' || !path.isAbsolute(item.from) ||
|
|
281
|
+
typeof item.to !== 'string' || !within(root, item.to) || within(root, item.from) || !Array.isArray(item.before) || item.before.some(entry =>
|
|
282
|
+
!Array.isArray(entry) || entry.length !== 2 || !safeRelative(item.to, entry[0]) || !/^[a-f0-9]{64}$/.test(entry[1])))) invalid();
|
|
283
|
+
const expectedStaged = [path.join(root, 'project'), ...detached.mappings.map(item => path.resolve(item.to))].sort();
|
|
284
|
+
if (JSON.stringify(detached.staged.map(item => path.resolve(item.path)).sort()) !== JSON.stringify([...new Set(expectedStaged)])) invalid();
|
|
285
|
+
const anchorPaths = expectedAnchorPaths([expectedTarget, ...detached.mappings.map(item => item.from), ...detached.workspaceDirs.map(name => path.join(expectedTarget, name))]);
|
|
286
|
+
if (detached.anchors.some(item => !item || typeof item.path !== 'string' || !path.isAbsolute(item.path) ||
|
|
287
|
+
(item.missing === true ? Object.keys(item).length !== 2 : Object.keys(item).sort().join(',') !== 'dev,ino,link,mode,path,real' ||
|
|
288
|
+
!['dev', 'ino', 'mode', 'real'].every(key => typeof item[key] === 'string') || !(item.link === null || typeof item.link === 'string'))) ||
|
|
289
|
+
JSON.stringify(detached.anchors.map(item => path.resolve(item.path)).sort()) !== JSON.stringify(anchorPaths)) invalid();
|
|
290
|
+
const plan = structuredClone(detached);
|
|
291
|
+
plan.lease = randomUUID();
|
|
292
|
+
if (pool) {
|
|
293
|
+
const existing = new Set(plan.preimages.map(item => path.resolve(item.path)));
|
|
294
|
+
plan.preimages.unshift(...pool.preimages.filter(item => !existing.has(path.resolve(item.path))));
|
|
295
|
+
pool.refs++;
|
|
296
|
+
}
|
|
297
|
+
const ownerCleanup = () => { try { cleanup(); } finally { if (pool) releaseInputPool(pool); } };
|
|
298
|
+
stagedCleanups.set(plan.lease, { temp: root, cleanup: ownerCleanup, pool, seal: planSeal(plan), transferable: false, detach: () => {} });
|
|
299
|
+
return plan;
|
|
300
|
+
}
|
|
@@ -47,6 +47,9 @@ function registerOpenCode(opts, provider, rulesDir) {
|
|
|
47
47
|
writeContextFileSync(file, `${JSON.stringify(config, null, 2)}\n`);
|
|
48
48
|
}
|
|
49
49
|
export function projectRules(opts, provider) {
|
|
50
|
+
const put = (file, content) => {
|
|
51
|
+
fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, content); opts.writtenFiles?.push(file);
|
|
52
|
+
};
|
|
50
53
|
const nativePaths = nativePathLoader(provider.name);
|
|
51
54
|
const rules = projectOnDemandReferences(opts.ruleDelivery.rules, nativePaths);
|
|
52
55
|
const rulesDir = ruleDirectory(opts, provider);
|