@aiwg/cli 2026.10.16 → 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/dist/src/artifacts/corpus-tools/resolve.js +21 -4
- 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/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.js +2 -1
- 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/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 -1984
- package/dist/src/cli/scope-resolver.js +1 -142
- package/dist/src/cli/user-scope-mirror.js +173 -0
- package/dist/src/config/dispatch-host-thresholds.js +2 -0
- package/dist/src/config/dispatch-thresholds-runtime.mjs +4 -0
- package/dist/src/config/host-resource-report.d.mts +5 -0
- package/dist/src/config/host-resource-report.mjs +36 -6
- package/dist/src/config/host-resource-runtime.d.mts +3 -0
- package/dist/src/config/host-resource-runtime.mjs +9 -6
- 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/sensitivity/comparative-gates.js +135 -0
- package/dist/src/decision/sensitivity/comparative.js +18 -41
- package/dist/src/extensions/claude-hooks-installer.js +18 -0
- package/dist/src/gates/evaluate.js +2 -358
- 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/mcp/json-document.mjs +29 -0
- package/dist/src/mcp/muse-native-config.mjs +1 -28
- package/dist/src/mission-protocol/worker-review-gate.js +7 -0
- package/dist/src/models/worker-plan.js +5 -5
- 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/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/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/identity.js +8 -2
- package/dist/src/research/reference-match.js +42 -7
- package/dist/src/rules/activation.mjs +15 -6
- package/dist/src/rules/core-startup.mjs +2 -2
- package/dist/src/serve/mission-conductor.js +4 -0
- package/dist/src/serve/mission-summary.js +56 -0
- package/dist/src/smiths/context-pipeline/aiwg-md.js +23 -0
- package/dist/src/smiths/context-pipeline/context-file-safety.js +3 -0
- package/dist/src/smiths/context-pipeline/generator.js +1 -1
- package/dist/src/smiths/hook-bridge/index.js +9 -0
- package/package.json +4 -1
- package/schemas/decision/D17StudyProtocol.v1.schema.json +39 -67
- package/tools/agents/deploy-agents.mjs +16 -677
- 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 +10 -2
- package/tools/agents/providers/hermes-startup.mjs +0 -5
- package/tools/agents/providers/muse.mjs +7 -2
- package/tools/agents/providers/rule-delivery-surface.mjs +31 -4
- package/tools/agents/providers/rule-deployment-commit.mjs +19 -15
- package/tools/agents/providers/rule-deployment-prepare.mjs +11 -2
- package/tools/agents/providers/rule-projection.mjs +3 -0
- package/tools/ralph-external/lib/worker-invocation-processes.mjs +4 -2
- package/tools/ralph-external/lib/worker-termination.mjs +15 -8
- package/vscode-extension/schemas/aiwg.config.v1.json +2 -4
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// Extend deploy sidecars with current-write ownership and settings preimages.
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { createHash } from 'node:crypto';
|
|
5
|
+
import { parseTOML, getStaticTOMLValue } from 'toml-eslint-parser';
|
|
6
|
+
import { parse as parseYaml } from 'yaml';
|
|
7
|
+
import { parseJson } from '../mcp/json-document.mjs';
|
|
8
|
+
import { extractFrameworkSlug, mergeArtifactOwnership } from './skill-ownership.mjs';
|
|
9
|
+
|
|
10
|
+
const SIDECAR = '.aiwg-manifest.json';
|
|
11
|
+
const hash = bytes => `sha256:${createHash('sha256').update(bytes).digest('hex')}`;
|
|
12
|
+
const portable = value => value.split(path.sep).join('/');
|
|
13
|
+
const stable = value => JSON.stringify(value, (_key, child) => child && typeof child === 'object' && !Array.isArray(child)
|
|
14
|
+
? Object.fromEntries(Object.entries(child).sort(([a], [b]) => a.localeCompare(b))) : child);
|
|
15
|
+
const parse = bytes => { try { return JSON.parse(bytes?.toString() || '{}'); } catch { return {}; } };
|
|
16
|
+
const bundleOwner = source => extractFrameworkSlug(source) || source.replaceAll('\\', '/').match(/(?:agentic\/code|\.aiwg)\/(?:frameworks|addons|extensions|plugins)\/([^/]+)\//)?.[1];
|
|
17
|
+
export const deploymentOwner = opts => {
|
|
18
|
+
const names = { sdlc: 'sdlc-complete', marketing: 'media-marketing-kit', mmk: 'media-marketing-kit',
|
|
19
|
+
research: 'research-complete', forensics: 'forensics-complete', dfir: 'forensics-complete', validation: 'validation-complete', ops: 'ops-complete', general: 'writing-quality' };
|
|
20
|
+
return opts.frameworkSlug || bundleOwner(`${opts.logicalInputPath?.(opts.srcRoot) || opts.srcRoot}/manifest.json`) || names[opts.mode] || opts.mode;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** Only provider output surfaces, never arbitrary project source directories. */
|
|
24
|
+
export function deploymentOutputRoots(provider, target) {
|
|
25
|
+
const roots = Object.values(provider.paths || {}).filter(Boolean).map(dir => path.isAbsolute(dir)
|
|
26
|
+
? dir : path.join(target, dir.split(/[\\/]/)[0]));
|
|
27
|
+
if (provider.kernelSkillsPath) roots.push(path.resolve(target, provider.kernelSkillsPath));
|
|
28
|
+
return [...new Set([...roots, path.join(target, `.${provider.name}`), path.join(target, '.agents'),
|
|
29
|
+
...['AGENTS.md', 'AIWG.md', 'WORKSPACE.md', 'CLAUDE.md', 'WARP.md', '.hermes.md', 'opencode.json',
|
|
30
|
+
'.windsurfrules', 'models.json', '.aiwg/frameworks', '.aiwg/.aiwg-manifest.json'].map(name => path.join(target, name))])];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Capture regular output files before this invocation; never follow symlinks. */
|
|
34
|
+
export function captureDeploymentOutputs(roots) {
|
|
35
|
+
const files = new Map();
|
|
36
|
+
const visit = file => {
|
|
37
|
+
let stat;
|
|
38
|
+
try { stat = fs.lstatSync(file); } catch (error) { if (error.code === 'ENOENT') return; throw error; }
|
|
39
|
+
if (stat.isDirectory()) for (const name of fs.readdirSync(file)) visit(path.join(file, name));
|
|
40
|
+
else if (stat.isFile()) files.set(path.resolve(file), fs.readFileSync(file));
|
|
41
|
+
};
|
|
42
|
+
for (const root of roots) visit(root);
|
|
43
|
+
// A root-level file's sidecar is itself outside that file's snapshot root.
|
|
44
|
+
for (const file of [...files.keys()]) {
|
|
45
|
+
const receipt = path.join(path.dirname(file), SIDECAR);
|
|
46
|
+
if (!files.has(receipt) && fs.existsSync(receipt) && fs.lstatSync(receipt).isFile()) files.set(receipt, fs.readFileSync(receipt));
|
|
47
|
+
}
|
|
48
|
+
return files;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function settingsDocument(file, bytes) {
|
|
52
|
+
if (!bytes) return {};
|
|
53
|
+
try {
|
|
54
|
+
if (file.endsWith('.toml')) return getStaticTOMLValue(parseTOML(bytes.toString()));
|
|
55
|
+
if (/\.ya?ml$/.test(file)) return parseYaml(bytes.toString()) ?? {};
|
|
56
|
+
return parseJson(bytes.toString());
|
|
57
|
+
} catch { return null; }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Recompute value digests after staged logical-path normalization. */
|
|
61
|
+
export function refreshSettingsDigests(record) {
|
|
62
|
+
for (const entry of Object.values(record.settings || {})) {
|
|
63
|
+
entry.hash = hash(stable(entry.value));
|
|
64
|
+
for (const contribution of entry.contributions || []) contribution.hash = hash(stable(contribution.value));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function leaves(value, pointer = '', out = {}) {
|
|
69
|
+
if (value && typeof value === 'object' && !Array.isArray(value) && Object.keys(value).length) {
|
|
70
|
+
for (const [key, child] of Object.entries(value)) leaves(child, `${pointer}/${key.replaceAll('~', '~0').replaceAll('/', '~1')}`, out);
|
|
71
|
+
} else out[pointer] = value;
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Settings arrays retain individual contributions so operator entries aren't claimed. */
|
|
76
|
+
function settingsRecords(previous, current, prior, owner, claimUnchanged) {
|
|
77
|
+
const records = { ...prior };
|
|
78
|
+
if (current === null) return records;
|
|
79
|
+
const before = previous === null ? {} : leaves(previous), after = leaves(current);
|
|
80
|
+
for (const [pointer, value] of Object.entries(after)) {
|
|
81
|
+
const old = records[pointer];
|
|
82
|
+
if (stable(before[pointer]) === stable(value)) {
|
|
83
|
+
if (claimUnchanged && old?.hash === hash(stable(value))) {
|
|
84
|
+
records[pointer] = { ...old, owners: [...new Set([...(old.owners || []), owner])].sort(),
|
|
85
|
+
...(old.contributions ? { contributions: old.contributions.map(entry => ({ ...entry,
|
|
86
|
+
owners: [...new Set([...entry.owners, ...(Array.isArray(value) || entry.hash === old.hash && !old.contributions.some(item => item.owners.includes(owner)) ? [owner] : [])])].sort() })) } : {}) };
|
|
87
|
+
}
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const digest = hash(stable(value));
|
|
91
|
+
const verified = Object.hasOwn(before, pointer) && old?.hash === hash(stable(before[pointer]));
|
|
92
|
+
const preimage = verified ? old.previous
|
|
93
|
+
: previous === null ? { unverifiable: true } : { present: Object.hasOwn(before, pointer), ...(Object.hasOwn(before, pointer) ? { value: before[pointer] } : {}) };
|
|
94
|
+
const record = { hash: digest, value, previous: preimage, owners: [...new Set([...(verified ? old.owners || [] : []), owner])].sort() };
|
|
95
|
+
if (!Array.isArray(value) && verified) {
|
|
96
|
+
const contributions = old.contributions || [{ hash: old.hash, value: old.value, owners: old.owners }];
|
|
97
|
+
record.contributions = [...contributions.flatMap(entry => {
|
|
98
|
+
const owners = entry.owners.filter(candidate => candidate !== owner);
|
|
99
|
+
return owners.length ? [{ ...entry, owners }] : [];
|
|
100
|
+
}), { hash: digest, value, owners: [owner] }];
|
|
101
|
+
}
|
|
102
|
+
if (Array.isArray(value)) {
|
|
103
|
+
const previousValues = Array.isArray(before[pointer]) ? before[pointer] : [];
|
|
104
|
+
record.contributions = value.flatMap(item => {
|
|
105
|
+
const itemHash = hash(stable(item));
|
|
106
|
+
const existing = old?.contributions?.find(entry => entry.hash === itemHash);
|
|
107
|
+
if (existing) return [existing];
|
|
108
|
+
return previousValues.some(entry => stable(entry) === stable(item)) ? [] : [{ hash: itemHash, value: item, owners: [owner] }];
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
records[pointer] = record;
|
|
112
|
+
}
|
|
113
|
+
return records;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function artifactKind(file) {
|
|
117
|
+
if (/(?:^|[/\\])hooks?[/\\]/.test(file)) return 'hook';
|
|
118
|
+
if (/(?:^|[/\\])(?:skills|skill)[/\\]/.test(file)) return 'skill';
|
|
119
|
+
if (/(?:^|[/\\])(?:agents|droids)[/\\]/.test(file)) return 'agent';
|
|
120
|
+
if (/(?:^|[/\\])(?:commands|prompts|workflows)[/\\]/.test(file)) return 'command';
|
|
121
|
+
if (/(?:^|[/\\])(?:rules|rule|copilot-rules)[/\\]/.test(file)) return 'rule';
|
|
122
|
+
return 'generated';
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Record only writes evidenced by this invocation or newly emitted file receipts. */
|
|
126
|
+
export function recordDeploymentOutputs(before, roots, opts = {}) {
|
|
127
|
+
if (opts.dryRun) return;
|
|
128
|
+
const after = captureDeploymentOutputs(roots);
|
|
129
|
+
const owner = deploymentOwner(opts);
|
|
130
|
+
const manifests = new Map();
|
|
131
|
+
const manifest = dir => {
|
|
132
|
+
const file = path.join(dir, SIDECAR);
|
|
133
|
+
if (!manifests.has(file)) manifests.set(file, parse(after.get(file)));
|
|
134
|
+
return manifests.get(file);
|
|
135
|
+
};
|
|
136
|
+
const projectRoot = opts.target && path.resolve(opts.target);
|
|
137
|
+
const receiptRoot = (file, receipt) => projectRoot && file === path.join(projectRoot, '.aiwg', SIDECAR) && receipt.scope === 'project'
|
|
138
|
+
? projectRoot : path.dirname(file);
|
|
139
|
+
const declared = new Map();
|
|
140
|
+
for (const [file, bytes] of after) {
|
|
141
|
+
if (path.basename(file) !== SIDECAR) continue;
|
|
142
|
+
const receipt = parse(bytes), previous = parse(before.get(file));
|
|
143
|
+
// Legacy writers replace their managed map. Retain already recorded,
|
|
144
|
+
// untouched metadata files/resources they omit; this never adopts a file.
|
|
145
|
+
for (const [name, entry] of Object.entries(previous.managed || {})) {
|
|
146
|
+
const base = receiptRoot(file, receipt);
|
|
147
|
+
const target = path.resolve(base, name);
|
|
148
|
+
if (!target.startsWith(`${base}${path.sep}`) || receipt.managed?.[name]) continue;
|
|
149
|
+
if (!before.has(target) || !after.get(target)?.equals(before.get(target))) continue;
|
|
150
|
+
receipt.managed ||= {};
|
|
151
|
+
receipt.managed[name] = entry;
|
|
152
|
+
manifests.set(file, receipt);
|
|
153
|
+
}
|
|
154
|
+
for (const [name, entry] of Object.entries(receipt.managed || {})) {
|
|
155
|
+
const base = receiptRoot(file, receipt);
|
|
156
|
+
const target = path.resolve(base, name);
|
|
157
|
+
if (!target.startsWith(`${base}${path.sep}`) || !after.has(target)) continue;
|
|
158
|
+
if (stable(previous.managed?.[name]) !== stable(entry)) declared.set(target, { file, name, entry, receipt });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
for (const [file, bytes] of after) {
|
|
162
|
+
if (path.basename(file) === SIDECAR || /(?:^|[/\\])aiwg\.config$/.test(file)
|
|
163
|
+
|| /(?:\.bak\.|\.aiwg-backup-)/.test(path.basename(file))) continue;
|
|
164
|
+
const changed = !before.get(file)?.equals(bytes);
|
|
165
|
+
const declaration = declared.get(file);
|
|
166
|
+
if (opts.onlyDeclared && !declaration) continue;
|
|
167
|
+
if (!changed && !declaration && !opts.writtenFiles?.includes(file)) continue;
|
|
168
|
+
// Existing integrity receipts that disagree with bytes never bless operator edits.
|
|
169
|
+
if (declaration?.entry.hash && declaration.entry.hash !== hash(bytes)) continue;
|
|
170
|
+
let skillRoot = path.dirname(file);
|
|
171
|
+
while (skillRoot !== path.dirname(skillRoot)) {
|
|
172
|
+
const parentReceipt = parse(after.get(path.join(skillRoot, SIDECAR)));
|
|
173
|
+
if (parentReceipt.managed?.['SKILL.md']) break;
|
|
174
|
+
skillRoot = path.dirname(skillRoot);
|
|
175
|
+
}
|
|
176
|
+
const skillReceipt = parse(after.get(path.join(skillRoot, SIDECAR)));
|
|
177
|
+
const projectScoped = projectRoot && path.dirname(file) === projectRoot;
|
|
178
|
+
const dir = projectScoped ? path.join(projectRoot, '.aiwg') : declaration ? path.dirname(declaration.file) : skillReceipt.managed?.['SKILL.md'] ? skillRoot : path.dirname(file);
|
|
179
|
+
const name = declaration?.name || portable(path.relative(projectScoped ? projectRoot : dir, file));
|
|
180
|
+
const existing = manifests.get(path.join(dir, SIDECAR)) || parse(after.get(path.join(dir, SIDECAR)));
|
|
181
|
+
const previous = parse(before.get(path.join(dir, SIDECAR))).managed?.[name];
|
|
182
|
+
const current = existing.managed?.[name] || {};
|
|
183
|
+
const identity = current.sourcePath || `${declaration?.receipt.skillSource}/SKILL.md`;
|
|
184
|
+
const logicalSource = opts.logicalInputPath?.(identity) || identity;
|
|
185
|
+
const sourceOwner = (declaration || current.hash === hash(bytes)) && (current.frameworkSlug || bundleOwner(logicalSource))
|
|
186
|
+
|| skillReceipt.managed?.['SKILL.md']?.frameworkSlug;
|
|
187
|
+
const generatedIndex = /^RULES-(?:INDEX|ONDEMAND)\.(?:md|mdc)$/.test(path.basename(file));
|
|
188
|
+
const kind = generatedIndex ? 'rule-index' : current.kind || artifactKind(file);
|
|
189
|
+
const selectedOwner = sourceOwner || (opts.onlyDeclared ? current.owners?.[0] : owner);
|
|
190
|
+
if (!selectedOwner) {
|
|
191
|
+
console.warn(`Ownership not recorded for ${file}: no framework owner resolved.`);
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
const receipt = manifest(dir);
|
|
195
|
+
if (projectScoped) receipt.scope = 'project';
|
|
196
|
+
const record = mergeArtifactOwnership(previous, { ...current, hash: hash(bytes), owners: current.hash === hash(bytes) ? current.owners : [], kind }, selectedOwner);
|
|
197
|
+
// Structural rule indexes are generated outputs; their assembly scratch
|
|
198
|
+
// directory is not a durable source identity.
|
|
199
|
+
if (generatedIndex) {
|
|
200
|
+
delete record.sourcePath;
|
|
201
|
+
record.kind = 'rule-index';
|
|
202
|
+
}
|
|
203
|
+
// Settings have key/value receipts in the same entry, never a separate store.
|
|
204
|
+
if (/^(?:settings(?:\.local)?|hooks|opencode|config|aiwg\.cordis\.patch)\.(?:json|toml|ya?ml)$/.test(path.basename(file))) {
|
|
205
|
+
record.kind = 'settings';
|
|
206
|
+
record.settingsPreimage = previous?.settingsPreimage || (!previous?.settings ? {
|
|
207
|
+
present: before.has(file), ...(before.has(file) ? { bytes: before.get(file).toString('base64'), hash: hash(before.get(file)) } : {})
|
|
208
|
+
} : undefined);
|
|
209
|
+
const claimUnchanged = opts.claimUnchangedSettings !== false
|
|
210
|
+
&& !/(?:agentic\/code|\.aiwg)\/(?:addons|extensions)\//.test((opts.logicalInputPath?.(opts.srcRoot) || opts.srcRoot)?.replaceAll('\\', '/') || '');
|
|
211
|
+
record.settings = settingsRecords(settingsDocument(file, before.get(file)), settingsDocument(file, bytes), previous?.settings, selectedOwner, claimUnchanged);
|
|
212
|
+
const settingsOwners = Object.values(record.settings).flatMap(entry => entry.owners);
|
|
213
|
+
record.owners = [...new Set(settingsOwners.length ? settingsOwners
|
|
214
|
+
: [...(previous?.owners || []), ...(changed ? [selectedOwner] : [])])].sort();
|
|
215
|
+
}
|
|
216
|
+
receipt.managed ||= {};
|
|
217
|
+
receipt.managed[name] = record;
|
|
218
|
+
}
|
|
219
|
+
for (const [file, receipt] of manifests) {
|
|
220
|
+
const text = `${JSON.stringify(receipt, null, 2)}\n`;
|
|
221
|
+
if (after.get(file)?.toString() === text) continue;
|
|
222
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
223
|
+
if (receipt.scope === 'project' && opts.writeReceipt) opts.writeReceipt(file, text);
|
|
224
|
+
else fs.writeFileSync(file, text);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// Build file removal decisions exclusively from provider ownership sidecars.
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { createHash } from 'node:crypto';
|
|
5
|
+
import { getProviderArtifactPathStrings } from './provider-definitions.js';
|
|
6
|
+
import { deployedSkillRoots } from './deployed-skill-roots.js';
|
|
7
|
+
import { releaseSkillOwnership } from './skill-ownership.mjs';
|
|
8
|
+
import { restoreSettings } from './settings-removal.js';
|
|
9
|
+
const SIDECAR = '.aiwg-manifest.json';
|
|
10
|
+
const CONTEXT_BRIDGES = new Set(['AGENTS.md', 'AIWG.md', 'WORKSPACE.md', 'CLAUDE.md', 'WARP.md', '.hermes.md', '.windsurfrules', 'copilot-instructions.md']);
|
|
11
|
+
const hash = (bytes) => `sha256:${createHash('sha256').update(bytes).digest('hex')}`;
|
|
12
|
+
const inside = (root, file) => file.startsWith(`${root}${path.sep}`);
|
|
13
|
+
function symlinkBetween(root, file) {
|
|
14
|
+
let current = file;
|
|
15
|
+
while (current === root || inside(root, current)) {
|
|
16
|
+
try {
|
|
17
|
+
if (fs.lstatSync(current).isSymbolicLink())
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
if (error.code !== 'ENOENT')
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
if (current === root)
|
|
25
|
+
break;
|
|
26
|
+
current = path.dirname(current);
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
export function planDeploymentRemoval(project, provider, owner, includeProject = true) {
|
|
31
|
+
const paths = Object.values(getProviderArtifactPathStrings(provider) || {}).filter(Boolean);
|
|
32
|
+
const roots = [...new Set([...paths.map(dir => path.isAbsolute(dir) ? dir : path.join(project, dir.split(/[\\/]/)[0])),
|
|
33
|
+
...deployedSkillRoots(project, provider), path.join(project, `.${provider}`), ...(includeProject ? [path.join(project, '.aiwg/frameworks')] : [])])].map(root => path.resolve(root));
|
|
34
|
+
const files = new Set();
|
|
35
|
+
const decisions = [], writes = [], remaining = [], recorded = [];
|
|
36
|
+
const visit = (file) => {
|
|
37
|
+
let stat;
|
|
38
|
+
try {
|
|
39
|
+
stat = fs.lstatSync(file);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (error.code === 'ENOENT')
|
|
43
|
+
return;
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
if (stat.isDirectory())
|
|
47
|
+
for (const name of fs.readdirSync(file).sort())
|
|
48
|
+
visit(path.join(file, name));
|
|
49
|
+
else
|
|
50
|
+
files.add(file);
|
|
51
|
+
};
|
|
52
|
+
for (const root of roots)
|
|
53
|
+
visit(root);
|
|
54
|
+
const projectReceipt = path.join(project, '.aiwg', SIDECAR);
|
|
55
|
+
if (includeProject && fs.existsSync(projectReceipt))
|
|
56
|
+
files.add(projectReceipt);
|
|
57
|
+
const accounted = new Set();
|
|
58
|
+
let modern = false;
|
|
59
|
+
const receipts = [];
|
|
60
|
+
for (const file of [...files].filter(file => path.basename(file) === SIDECAR).sort()) {
|
|
61
|
+
if (symlinkBetween(roots.find(root => inside(root, file)) || project, file))
|
|
62
|
+
continue;
|
|
63
|
+
try {
|
|
64
|
+
const before = fs.readFileSync(file), receipt = JSON.parse(before.toString());
|
|
65
|
+
if (receipt.managed && typeof receipt.managed === 'object' && !Array.isArray(receipt.managed)) {
|
|
66
|
+
receipts.push({ file, before, receipt });
|
|
67
|
+
if (Object.values(receipt.managed).some(record => Array.isArray(record?.owners)))
|
|
68
|
+
modern = true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch { /* Invalid receipts are reported below, never treated as ownership. */ }
|
|
72
|
+
}
|
|
73
|
+
const claims = new Map();
|
|
74
|
+
for (const item of receipts) {
|
|
75
|
+
const base = item.file === projectReceipt && item.receipt.scope === 'project' ? project : path.dirname(item.file);
|
|
76
|
+
for (const [name, record] of Object.entries(item.receipt.managed)) {
|
|
77
|
+
if (!record || typeof record !== 'object')
|
|
78
|
+
continue;
|
|
79
|
+
const target = path.resolve(base, name);
|
|
80
|
+
claims.set(target, [...(claims.get(target) || []), record]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// Legacy skill receipts already contain verifiable hashes and a root owner.
|
|
84
|
+
// Keep their established metadata release semantics; never infer flat artifacts.
|
|
85
|
+
for (const { file, before, receipt } of receipts) {
|
|
86
|
+
const root = path.dirname(file);
|
|
87
|
+
const skillOwner = receipt.managed?.['SKILL.md']?.frameworkSlug;
|
|
88
|
+
if (skillOwner === owner && Object.values(receipt.managed).every(record => !record?.kind && (!record?.owners || record.owners.length === 1 && record.owners[0] === owner))) {
|
|
89
|
+
const legacyPlan = [];
|
|
90
|
+
releaseSkillOwnership(root, { frameworkSlug: owner }, { dryRun: true, plan: legacyPlan });
|
|
91
|
+
for (const decision of legacyPlan) {
|
|
92
|
+
accounted.add(decision.path);
|
|
93
|
+
decisions.push({ ...decision, owned: true, legacy: true });
|
|
94
|
+
if (decision.action === 'remove')
|
|
95
|
+
writes.push({ path: decision.path, before: fs.readFileSync(decision.path), after: null });
|
|
96
|
+
}
|
|
97
|
+
accounted.add(file);
|
|
98
|
+
recorded.push({ ...receipt.managed['SKILL.md'], kind: 'skill' });
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const base = file === projectReceipt && receipt.scope === 'project' ? project : root;
|
|
102
|
+
let changed = false;
|
|
103
|
+
for (const [name, record] of Object.entries(receipt.managed)) {
|
|
104
|
+
if (!record || typeof record !== 'object')
|
|
105
|
+
continue;
|
|
106
|
+
const target = path.resolve(base, name);
|
|
107
|
+
const owners = Array.isArray(record.owners) ? record.owners : record.frameworkSlug ? [record.frameworkSlug] : [];
|
|
108
|
+
if (!owners.includes(owner))
|
|
109
|
+
continue;
|
|
110
|
+
accounted.add(target);
|
|
111
|
+
recorded.push(record);
|
|
112
|
+
let action = 'preserve', reason = 'modified or unverifiable managed file';
|
|
113
|
+
if (!inside(base, target) || target === file || !roots.some(root => inside(root, target)) && base !== project)
|
|
114
|
+
reason = 'unsafe receipt path; unverifiable';
|
|
115
|
+
else if (symlinkBetween(roots.find(root => inside(root, target)) || project, target))
|
|
116
|
+
reason = 'symlink preserved';
|
|
117
|
+
else if (record.kind === 'settings' && record.settings && (claims.get(target) || []).length === 1) {
|
|
118
|
+
const restored = restoreSettings(target, record, owner);
|
|
119
|
+
decisions.push(...restored.decisions);
|
|
120
|
+
if (restored.write)
|
|
121
|
+
writes.push(restored.write);
|
|
122
|
+
if (restored.retained)
|
|
123
|
+
remaining.push(record);
|
|
124
|
+
if (!record.owners?.length)
|
|
125
|
+
delete receipt.managed[name];
|
|
126
|
+
changed = true;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
else if (record.kind === 'settings')
|
|
130
|
+
reason = 'settings contributions missing or conflicting; unverifiable';
|
|
131
|
+
else if (CONTEXT_BRIDGES.has(path.basename(target)))
|
|
132
|
+
reason = 'context bridge preserved; pre-deploy operator content unverifiable';
|
|
133
|
+
else if ((claims.get(target) || []).some(claim => claim.hash !== record.hash))
|
|
134
|
+
reason = 'conflicting receipt digests; unverifiable';
|
|
135
|
+
else if ((claims.get(target) || []).some(claim => (claim.owners || (claim.frameworkSlug ? [claim.frameworkSlug] : [])).some(item => item !== owner))) {
|
|
136
|
+
action = 'release';
|
|
137
|
+
reason = 'shared with another framework; release this owner only';
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
try {
|
|
141
|
+
if (fs.lstatSync(target).isFile() && /^sha256:[a-f0-9]{64}$/.test(record.hash) && hash(fs.readFileSync(target)) === record.hash) {
|
|
142
|
+
action = 'remove';
|
|
143
|
+
reason = 'unchanged managed file';
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
if (error.code === 'ENOENT') {
|
|
148
|
+
action = 'release';
|
|
149
|
+
reason = 'recorded file already absent';
|
|
150
|
+
}
|
|
151
|
+
else
|
|
152
|
+
reason = 'unreadable path; unverifiable';
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
decisions.push({ path: target, action, reason, kind: record.kind, owned: true });
|
|
156
|
+
if (action === 'preserve') {
|
|
157
|
+
remaining.push(record);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
changed = true;
|
|
161
|
+
if (action === 'remove')
|
|
162
|
+
writes.push({ path: target, before: fs.readFileSync(target), after: null });
|
|
163
|
+
const otherOwners = owners.filter(item => item !== owner);
|
|
164
|
+
if (otherOwners.length) {
|
|
165
|
+
record.owners = otherOwners;
|
|
166
|
+
if (record.frameworkSlug === owner)
|
|
167
|
+
record.frameworkSlug = otherOwners[0];
|
|
168
|
+
}
|
|
169
|
+
else
|
|
170
|
+
delete receipt.managed[name];
|
|
171
|
+
}
|
|
172
|
+
if (changed) {
|
|
173
|
+
const empty = Object.keys(receipt.managed).length === 0;
|
|
174
|
+
writes.push({ path: file, before, after: empty ? null : Buffer.from(`${JSON.stringify(receipt, null, 2)}\n`) });
|
|
175
|
+
if (empty)
|
|
176
|
+
decisions.push({ path: file, action: 'remove', reason: 'ownership metadata released' });
|
|
177
|
+
}
|
|
178
|
+
accounted.add(file);
|
|
179
|
+
}
|
|
180
|
+
for (const file of [...files].sort()) {
|
|
181
|
+
if (accounted.has(file))
|
|
182
|
+
continue;
|
|
183
|
+
// Foreign receipts are ownership evidence, but never this framework's files.
|
|
184
|
+
const skill = receipts.find(item => inside(path.dirname(item.file), file) && item.receipt.managed?.['SKILL.md']);
|
|
185
|
+
const ownSkill = skill?.receipt.managed?.['SKILL.md']?.frameworkSlug === owner;
|
|
186
|
+
const foreign = skill && !ownSkill;
|
|
187
|
+
const foreignFile = (claims.get(file) || []).some(record => (record.owners || (record.frameworkSlug ? [record.frameworkSlug] : [])).length);
|
|
188
|
+
decisions.push({ path: file, action: 'preserve', reason: foreign ? 'foreign or unverifiable ownership receipt'
|
|
189
|
+
: foreignFile ? 'foreign ownership receipt; owned by another bundle' : ownSkill ? 'unrecorded operator file' : modern ? 'unrecorded operator file; unverifiable ownership' : 'unverifiable: no ownership receipt' });
|
|
190
|
+
}
|
|
191
|
+
const unique = new Map();
|
|
192
|
+
for (const decision of decisions) {
|
|
193
|
+
const key = `${decision.path}#${decision.pointer ?? ''}`;
|
|
194
|
+
const previous = unique.get(key);
|
|
195
|
+
if (!previous || decision.action === 'preserve' || previous.action === 'remove')
|
|
196
|
+
unique.set(key, decision);
|
|
197
|
+
}
|
|
198
|
+
return { decisions: [...unique.values()], writes, remaining, recorded, legacy: !modern };
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=deployment-removal.js.map
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Back up a removal's complete write set before applying it; roll back failures.
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
function verifyPreimage(write) {
|
|
6
|
+
let ancestor = write.path;
|
|
7
|
+
while (ancestor !== path.dirname(ancestor)) {
|
|
8
|
+
if (fs.existsSync(ancestor) && fs.lstatSync(ancestor).isSymbolicLink())
|
|
9
|
+
throw new Error(`Symlink changed during removal planning: ${write.path}`);
|
|
10
|
+
ancestor = path.dirname(ancestor);
|
|
11
|
+
}
|
|
12
|
+
const current = fs.existsSync(write.path) ? fs.readFileSync(write.path) : null;
|
|
13
|
+
if (write.before === null ? current !== null : !current?.equals(write.before))
|
|
14
|
+
throw new Error(`Changed during removal planning: ${write.path}`);
|
|
15
|
+
}
|
|
16
|
+
export async function applyRemovalTransaction(writes, commit) {
|
|
17
|
+
if (!writes.length)
|
|
18
|
+
return;
|
|
19
|
+
const backup = fs.mkdtempSync(path.join(os.tmpdir(), 'aiwg-remove-'));
|
|
20
|
+
fs.chmodSync(backup, 0o700);
|
|
21
|
+
const entries = writes.map((write, index) => ({ path: write.path, backup: write.before ? `${index}` : null,
|
|
22
|
+
mode: undefined, operation: write.after ? 'write' : 'delete' }));
|
|
23
|
+
const journal = path.join(backup, 'removal.json');
|
|
24
|
+
// No output is mutated until every preimage and the durable intent are saved.
|
|
25
|
+
try {
|
|
26
|
+
for (const [index, write] of writes.entries()) {
|
|
27
|
+
verifyPreimage(write);
|
|
28
|
+
if (write.before)
|
|
29
|
+
entries[index].mode = fs.statSync(write.path).mode;
|
|
30
|
+
if (write.before)
|
|
31
|
+
fs.writeFileSync(path.join(backup, `${index}`), write.before, { mode: 0o600 });
|
|
32
|
+
}
|
|
33
|
+
fs.writeFileSync(journal, JSON.stringify({ status: 'prepared', entries }, null, 2), { mode: 0o600 });
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
fs.rmSync(backup, { recursive: true, force: true });
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
const applied = [];
|
|
40
|
+
try {
|
|
41
|
+
for (const [index, write] of writes.entries()) {
|
|
42
|
+
if (write.commit)
|
|
43
|
+
continue;
|
|
44
|
+
verifyPreimage(write);
|
|
45
|
+
applied.push(index); // Also restore a write that throws after truncation.
|
|
46
|
+
if (write.after === null)
|
|
47
|
+
fs.unlinkSync(write.path);
|
|
48
|
+
else
|
|
49
|
+
fs.writeFileSync(write.path, write.after);
|
|
50
|
+
}
|
|
51
|
+
for (const [index, write] of writes.entries()) {
|
|
52
|
+
if (!write.commit)
|
|
53
|
+
continue;
|
|
54
|
+
verifyPreimage(write);
|
|
55
|
+
applied.push(index);
|
|
56
|
+
}
|
|
57
|
+
await commit?.();
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
const failures = [];
|
|
61
|
+
for (const index of applied.reverse()) {
|
|
62
|
+
const write = writes[index];
|
|
63
|
+
try {
|
|
64
|
+
if (write.before === null) {
|
|
65
|
+
if (fs.existsSync(write.path))
|
|
66
|
+
fs.unlinkSync(write.path);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
fs.writeFileSync(write.path, write.before);
|
|
70
|
+
fs.chmodSync(write.path, entries[index].mode);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
failures.push(write.path);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
fs.writeFileSync(journal, JSON.stringify({ status: failures.length ? 'rollback-incomplete' : 'rolled-back', entries, failures }, null, 2));
|
|
79
|
+
}
|
|
80
|
+
catch { /* The prepared intent and preimages remain available. */ }
|
|
81
|
+
throw new Error(`Removal failed: ${error instanceof Error ? error.message : String(error)}. ${failures.length
|
|
82
|
+
? `Rollback incomplete for ${failures.join(', ')}.` : 'All changes rolled back.'} Recovery preimages and record: ${journal}`);
|
|
83
|
+
}
|
|
84
|
+
fs.rmSync(backup, { recursive: true, force: true });
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=removal-transaction.js.map
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Parse provider settings and render safe semantic edits in their native format.
|
|
2
|
+
import { parseTOML, getStaticTOMLValue } from 'toml-eslint-parser';
|
|
3
|
+
import { parseDocument } from 'yaml';
|
|
4
|
+
import { parseJson } from '../mcp/json-document.mjs';
|
|
5
|
+
|
|
6
|
+
export const stableSettings = value => JSON.stringify(value, (_key, child) => child && typeof child === 'object' && !Array.isArray(child)
|
|
7
|
+
? Object.fromEntries(Object.entries(child).sort(([a], [b]) => a.localeCompare(b))) : child);
|
|
8
|
+
export function parseSettings(file, bytes) {
|
|
9
|
+
const text = bytes.toString();
|
|
10
|
+
if (file.endsWith('.toml')) return getStaticTOMLValue(parseTOML(text));
|
|
11
|
+
if (/\.ya?ml$/.test(file)) {
|
|
12
|
+
const doc = parseDocument(text);
|
|
13
|
+
if (doc.errors.length) throw new Error('Invalid YAML settings');
|
|
14
|
+
return doc.toJS() ?? {};
|
|
15
|
+
}
|
|
16
|
+
return parseJson(text);
|
|
17
|
+
}
|
|
18
|
+
const tomlValue = value => {
|
|
19
|
+
if (value instanceof Date) return value.toISOString();
|
|
20
|
+
if (Array.isArray(value)) return `[${value.map(tomlValue).join(', ')}]`;
|
|
21
|
+
if (value && typeof value === 'object') return `{ ${Object.entries(value).map(([key, child]) => `${JSON.stringify(key)} = ${tomlValue(child)}`).join(', ')} }`;
|
|
22
|
+
if (value === null || value === undefined) throw new Error('Unrepresentable TOML value');
|
|
23
|
+
return JSON.stringify(value);
|
|
24
|
+
};
|
|
25
|
+
export function renderSettings(file, bytes, value) {
|
|
26
|
+
if (file.endsWith('.toml')) {
|
|
27
|
+
// Root inline tables are valid TOML and preserve arbitrary dotted keys.
|
|
28
|
+
const text = Object.entries(value).map(([key, child]) => `${JSON.stringify(key)} = ${tomlValue(child)}`).join('\n') + '\n';
|
|
29
|
+
if (stableSettings(parseSettings(file, Buffer.from(text))) !== stableSettings(value)) throw new Error('TOML settings round trip failed');
|
|
30
|
+
return Buffer.from(text);
|
|
31
|
+
}
|
|
32
|
+
if (/\.ya?ml$/.test(file)) {
|
|
33
|
+
const doc = parseDocument(bytes.toString());
|
|
34
|
+
const update = (before, after, keys = []) => {
|
|
35
|
+
for (const key of Object.keys(before || {})) if (!Object.hasOwn(after, key)) doc.deleteIn([...keys, key]);
|
|
36
|
+
for (const [key, child] of Object.entries(after)) {
|
|
37
|
+
const prior = before?.[key];
|
|
38
|
+
if (stableSettings(prior) === stableSettings(child)) continue;
|
|
39
|
+
if (prior && child && typeof prior === 'object' && typeof child === 'object' && !Array.isArray(prior) && !Array.isArray(child)) update(prior, child, [...keys, key]);
|
|
40
|
+
else doc.setIn([...keys, key], child);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
update(doc.toJS(), value);
|
|
44
|
+
return Buffer.from(doc.toString());
|
|
45
|
+
}
|
|
46
|
+
return Buffer.from(`${JSON.stringify(value, null, 2)}\n`);
|
|
47
|
+
}
|