framework-aidf 0.0.0-stage → 0.2.0
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/CHANGELOG.md +174 -0
- package/INSTALLATION.md +140 -0
- package/LICENSE +21 -0
- package/QODER.md +88 -0
- package/README.md +142 -2
- package/UTILISATION.md +188 -0
- package/assets/adapters/qoder/agents/aidf-orchestrator.md +30 -0
- package/assets/adapters/qoder/rules/00-core.md +28 -0
- package/assets/adapters/qoder/rules/10-security.md +33 -0
- package/assets/adapters/qoder/rules/20-context.md +39 -0
- package/assets/core/AGENTS.fragment.md +18 -0
- package/assets/core/AIDF.md +107 -0
- package/assets/docs/APPFLOW.md +66 -0
- package/assets/docs/BACKEND.md +71 -0
- package/assets/docs/CHANGELOG.md +25 -0
- package/assets/docs/CONVENTIONS.md +95 -0
- package/assets/docs/DECISIONS.md +60 -0
- package/assets/docs/PLAN.md +53 -0
- package/assets/docs/PRD.md +88 -0
- package/assets/docs/SECURITY.md +94 -0
- package/assets/docs/TRD.md +78 -0
- package/assets/docs/UI-UX.md +71 -0
- package/assets/docs/constitution.md +86 -0
- package/assets/hooks/audit.mjs +45 -0
- package/assets/hooks/session-context.mjs +53 -0
- package/assets/integrations/spec-kit/mapping.yaml +92 -0
- package/assets/knowledge/best-practices/testing.md +53 -0
- package/assets/knowledge/best-practices/web-dev-baseline.md +39 -0
- package/assets/knowledge/rules/api.md +45 -0
- package/assets/knowledge/rules/sql.md +44 -0
- package/assets/knowledge/rules/typescript.md +50 -0
- package/assets/knowledge/security-references/owasp-baseline.md +58 -0
- package/assets/registry/activation-map.yaml +83 -0
- package/assets/registry/dependencies.yaml +35 -0
- package/assets/registry/skills.yaml +541 -0
- package/assets/registry/sources.yaml +396 -0
- package/assets/schemas/architecture/v1.json +110 -0
- package/assets/schemas/constraints/v1.json +93 -0
- package/assets/schemas/decision/v1.json +106 -0
- package/assets/schemas/discovery/v1.json +91 -0
- package/assets/schemas/requirement/v1.json +136 -0
- package/assets/schemas/security/threat-model/v1.json +127 -0
- package/assets/schemas/test-plan/v1.json +108 -0
- package/assets/skills/aidf-architecture-review/SKILL.md +49 -0
- package/assets/skills/aidf-backend-implementation/SKILL.md +47 -0
- package/assets/skills/aidf-database-design/SKILL.md +48 -0
- package/assets/skills/aidf-devops-engineering/SKILL.md +41 -0
- package/assets/skills/aidf-documentation-maintenance/SKILL.md +41 -0
- package/assets/skills/aidf-frontend-implementation/SKILL.md +44 -0
- package/assets/skills/aidf-phase-transition/SKILL.md +48 -0
- package/assets/skills/aidf-product-analysis/SKILL.md +47 -0
- package/assets/skills/aidf-project-context/SKILL.md +57 -0
- package/assets/skills/aidf-requirements-engineering/SKILL.md +52 -0
- package/assets/skills/aidf-security-review/SKILL.md +57 -0
- package/assets/skills/aidf-testing-validation/SKILL.md +48 -0
- package/assets/skills/aidf-ui-ux-implementation/SKILL.md +43 -0
- package/assets/workflows/gates.yaml +232 -0
- package/assets/workflows/phases.yaml +161 -0
- package/bin/aidf.mjs +12 -0
- package/demo/run-demo.mjs +97 -0
- package/docs/aidf/ADR-001-artefacts-architectur.md +126 -0
- package/docs/aidf/ADR-002-requirements-registry.md +145 -0
- package/docs/aidf/ADR-003-traceability-chain.md +145 -0
- package/docs/aidf/ADR-004-markdown-generation.md +134 -0
- package/docs/aidf/ADR-005-validation-and-tests.md +170 -0
- package/docs/aidf/ADR-006-cli-commands.md +189 -0
- package/docs/aidf/ADR-007-compatibility-and-migration.md +119 -0
- package/docs/aidf/ADR-008-schema-versioning.md +115 -0
- package/docs/aidf/ADR-009-artefacts-scope.md +84 -0
- package/docs/aidf/ARCHITECTURE.md +137 -0
- package/docs/aidf/COMPATIBILITY.md +90 -0
- package/docs/aidf/DECISIONS.md +200 -0
- package/docs/aidf/FRAMEWORK.md +38 -0
- package/docs/aidf/IDE-QODER.md +61 -0
- package/docs/aidf/INSTALL.md +99 -0
- package/docs/aidf/INVENTORY.md +72 -0
- package/docs/aidf/L1-ARCHITECTURE-RAPPORT.md +353 -0
- package/docs/aidf/LIMITATIONS.md +69 -0
- package/docs/aidf/MATRICE-0.1.0-vers-0.2.0.md +155 -0
- package/docs/aidf/SECURITY.md +86 -0
- package/docs/aidf/SKILLS.md +78 -0
- package/docs/aidf/SPECKIT.md +67 -0
- package/docs/aidf/USAGE.md +113 -0
- package/docs/aidf/VALIDATION.md +55 -0
- package/package.json +37 -4
- package/src/cli.mjs +148 -0
- package/src/commands/artefacts.mjs +129 -0
- package/src/commands/config.mjs +130 -0
- package/src/commands/context.mjs +55 -0
- package/src/commands/decision.mjs +142 -0
- package/src/commands/design.mjs +141 -0
- package/src/commands/docs.mjs +133 -0
- package/src/commands/doctor.mjs +210 -0
- package/src/commands/evidence.mjs +178 -0
- package/src/commands/install.mjs +154 -0
- package/src/commands/release.mjs +214 -0
- package/src/commands/requirements.mjs +196 -0
- package/src/commands/resources.mjs +181 -0
- package/src/commands/scan.mjs +139 -0
- package/src/commands/security.mjs +97 -0
- package/src/commands/setup.mjs +68 -0
- package/src/commands/skills.mjs +204 -0
- package/src/commands/speckit.mjs +78 -0
- package/src/commands/tests.mjs +81 -0
- package/src/commands/traceability.mjs +116 -0
- package/src/commands/uninstall.mjs +238 -0
- package/src/commands/validate.mjs +84 -0
- package/src/commands/workflow.mjs +137 -0
- package/src/core/adapters/base.mjs +32 -0
- package/src/core/adapters/generic.mjs +70 -0
- package/src/core/adapters/index.mjs +24 -0
- package/src/core/adapters/opencode.mjs +115 -0
- package/src/core/adapters/qoder.mjs +228 -0
- package/src/core/artefacts/index.mjs +1 -0
- package/src/core/artefacts/validation.mjs +161 -0
- package/src/core/bootstrap.mjs +179 -0
- package/src/core/config.mjs +123 -0
- package/src/core/context.mjs +187 -0
- package/src/core/design-engineering/complexity-classifier.mjs +78 -0
- package/src/core/design-engineering/engine.mjs +197 -0
- package/src/core/design-engineering/fallback/mockups.mjs +221 -0
- package/src/core/design-engineering/fallback/wireframes.mjs +114 -0
- package/src/core/design-engineering/figma/validators.mjs +178 -0
- package/src/core/design-engineering/index.mjs +9 -0
- package/src/core/design-engineering/visual-parity/capture.mjs +172 -0
- package/src/core/detect.mjs +220 -0
- package/src/core/docsgen.mjs +175 -0
- package/src/core/evidence.mjs +210 -0
- package/src/core/installer.mjs +377 -0
- package/src/core/manifest.mjs +53 -0
- package/src/core/paths.mjs +94 -0
- package/src/core/requirements/index.mjs +13 -0
- package/src/core/requirements/registry.mjs +234 -0
- package/src/core/resource-inventory.mjs +434 -0
- package/src/core/resources.mjs +280 -0
- package/src/core/security/threat-model.mjs +235 -0
- package/src/core/skills.mjs +353 -0
- package/src/core/source-skills.mjs +213 -0
- package/src/core/speckit.mjs +195 -0
- package/src/core/technology-decision/analyzer-deterministic.mjs +240 -0
- package/src/core/technology-decision/engine.mjs +157 -0
- package/src/core/technology-decision/generator.mjs +204 -0
- package/src/core/technology-decision/index.mjs +17 -0
- package/src/core/technology-decision/options/index.mjs +10 -0
- package/src/core/testplan.mjs +176 -0
- package/src/core/traceability/chain.mjs +488 -0
- package/src/core/traceability/index.mjs +12 -0
- package/src/core/workflow.mjs +471 -0
- package/src/util/args.mjs +69 -0
- package/src/util/frontmatter.mjs +26 -0
- package/src/util/fsx.mjs +208 -0
- package/src/util/logger.mjs +74 -0
- package/src/util/text.mjs +66 -0
- package/src/util/yaml.mjs +379 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import fsp from 'node:fs/promises';
|
|
4
|
+
import crypto from 'node:crypto';
|
|
5
|
+
import { readText, readDirSafe, exists, toPosix, writeJson, readJson } from '../util/fsx.mjs';
|
|
6
|
+
import { validateSkillContent } from './skills.mjs';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Resource Engine v2.
|
|
10
|
+
*
|
|
11
|
+
* The 0.1.0 engine inspected a resource and produced one tree hash over the
|
|
12
|
+
* first 5000 files. This module adds three verified capabilities:
|
|
13
|
+
* - a file-level manifest (path, size, sha256) for every included file;
|
|
14
|
+
* - an explicit licence policy that decides indexing, copying and blocking;
|
|
15
|
+
* - a coverage report and a lock/manifest reconciliation.
|
|
16
|
+
*
|
|
17
|
+
* Security rules:
|
|
18
|
+
* - secret-like paths are never opened; they are counted and skipped;
|
|
19
|
+
* - .git, node_modules and declared excludes are skipped;
|
|
20
|
+
* - no file from a resource is ever executed.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export const LICENSE_POLICIES = {
|
|
24
|
+
MIT: {
|
|
25
|
+
redistribution: 'allowed',
|
|
26
|
+
copy: true,
|
|
27
|
+
attribution: true,
|
|
28
|
+
shareAlike: false,
|
|
29
|
+
note: 'MIT permits copying and adaptation with the copyright notice.',
|
|
30
|
+
},
|
|
31
|
+
'Apache-2.0': {
|
|
32
|
+
redistribution: 'allowed',
|
|
33
|
+
copy: true,
|
|
34
|
+
attribution: true,
|
|
35
|
+
shareAlike: false,
|
|
36
|
+
note: 'Apache-2.0 permits copying with NOTICE and attribution.',
|
|
37
|
+
},
|
|
38
|
+
'CC-BY-SA-4.0': {
|
|
39
|
+
redistribution: 'allowed-with-attribution',
|
|
40
|
+
copy: true,
|
|
41
|
+
attribution: true,
|
|
42
|
+
shareAlike: true,
|
|
43
|
+
note: 'CC-BY-SA-4.0 requires attribution and share-alike for adaptations.',
|
|
44
|
+
},
|
|
45
|
+
'Licence Ouverte 2.0': {
|
|
46
|
+
redistribution: 'allowed-with-attribution',
|
|
47
|
+
copy: true,
|
|
48
|
+
attribution: true,
|
|
49
|
+
shareAlike: false,
|
|
50
|
+
note: 'Licence Ouverte 2.0 permits reuse, including commercial, with attribution.',
|
|
51
|
+
},
|
|
52
|
+
'GPL-2.0': {
|
|
53
|
+
redistribution: 'blocked-in-mit-distribution',
|
|
54
|
+
copy: false,
|
|
55
|
+
attribution: true,
|
|
56
|
+
shareAlike: true,
|
|
57
|
+
note: 'GPL-2.0 code cannot be bundled into the MIT AIDF package.',
|
|
58
|
+
},
|
|
59
|
+
'proprietary-owner': {
|
|
60
|
+
redistribution: 'no',
|
|
61
|
+
copy: false,
|
|
62
|
+
attribution: false,
|
|
63
|
+
shareAlike: false,
|
|
64
|
+
note: 'Owner-restricted corpus: index only, never redistribute.',
|
|
65
|
+
},
|
|
66
|
+
unknown: {
|
|
67
|
+
redistribution: 'blocked',
|
|
68
|
+
copy: false,
|
|
69
|
+
attribution: false,
|
|
70
|
+
shareAlike: false,
|
|
71
|
+
note: 'No verified licence: index only until the owner clarifies it.',
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const LICENSE_ALIASES = new Map([
|
|
76
|
+
['mit', 'MIT'],
|
|
77
|
+
['mit license', 'MIT'],
|
|
78
|
+
['apache-2.0', 'Apache-2.0'],
|
|
79
|
+
['apache 2.0', 'Apache-2.0'],
|
|
80
|
+
['cc-by-sa-4.0', 'CC-BY-SA-4.0'],
|
|
81
|
+
['cc by-sa 4.0', 'CC-BY-SA-4.0'],
|
|
82
|
+
['licence ouverte 2.0', 'Licence Ouverte 2.0'],
|
|
83
|
+
['licence ouverte 2.0/open licence 2.0', 'Licence Ouverte 2.0'],
|
|
84
|
+
['gpl-2.0', 'GPL-2.0'],
|
|
85
|
+
['gpl-2.0-only', 'GPL-2.0'],
|
|
86
|
+
['proprietary-owner', 'proprietary-owner'],
|
|
87
|
+
['unknown', 'unknown'],
|
|
88
|
+
['', 'unknown'],
|
|
89
|
+
]);
|
|
90
|
+
|
|
91
|
+
export function normalizeLicense(license) {
|
|
92
|
+
if (!license) return 'unknown';
|
|
93
|
+
const key = String(license).trim().toLowerCase();
|
|
94
|
+
return LICENSE_ALIASES.get(key) ?? 'unknown';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function licensePolicy(license) {
|
|
98
|
+
const id = normalizeLicense(license);
|
|
99
|
+
return { id, ...LICENSE_POLICIES[id] };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function isSecretPath(relPath) {
|
|
103
|
+
const posix = toPosix(relPath);
|
|
104
|
+
const base = posix.split('/').pop() ?? posix;
|
|
105
|
+
return (
|
|
106
|
+
/^\.env(\.|$)/i.test(base)
|
|
107
|
+
|| /\.(pem|p12|pfx|crt|key|keystore|jks)$/i.test(base)
|
|
108
|
+
|| /(^|\/)(id_rsa|id_ed25519|credentials|secrets?\.ya?ml)$/i.test(posix)
|
|
109
|
+
|| /(^|\/)\.npmrc$/i.test(posix)
|
|
110
|
+
|| /(^|\/)\.git-credentials$/i.test(posix)
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function globToRegExp(glob) {
|
|
115
|
+
let out = '^';
|
|
116
|
+
for (let i = 0; i < glob.length; i += 1) {
|
|
117
|
+
const ch = glob[i];
|
|
118
|
+
if (ch === '*') {
|
|
119
|
+
if (glob[i + 1] === '*') {
|
|
120
|
+
out += '.*';
|
|
121
|
+
i += 1;
|
|
122
|
+
if (glob[i + 1] === '/') i += 1;
|
|
123
|
+
} else {
|
|
124
|
+
out += '[^/]*';
|
|
125
|
+
}
|
|
126
|
+
} else if (ch === '?') {
|
|
127
|
+
out += '[^/]';
|
|
128
|
+
} else if ('\\^$.|+()[]{}'.includes(ch)) {
|
|
129
|
+
out += `\\${ch}`;
|
|
130
|
+
} else {
|
|
131
|
+
out += ch;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return new RegExp(`${out}$`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function matchesAny(relPath, globs = []) {
|
|
138
|
+
const posix = toPosix(relPath);
|
|
139
|
+
return globs.some((g) => globToRegExp(g).test(posix));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function selectFiles(resource, files) {
|
|
143
|
+
const include = resource.include ?? [];
|
|
144
|
+
const exclude = resource.exclude ?? [];
|
|
145
|
+
return files.filter((rel) => {
|
|
146
|
+
if (matchesAny(rel, exclude)) return false;
|
|
147
|
+
if (isSecretPath(rel)) return false;
|
|
148
|
+
return include.length === 0 || matchesAny(rel, include);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function listAllFiles(dir) {
|
|
153
|
+
const files = [];
|
|
154
|
+
async function walk(current, rel) {
|
|
155
|
+
for (const entry of await readDirSafe(current)) {
|
|
156
|
+
if (entry.name === '.git' || entry.name === 'node_modules') continue;
|
|
157
|
+
const childRel = rel ? `${rel}/${entry.name}` : entry.name;
|
|
158
|
+
const childAbs = path.join(current, entry.name);
|
|
159
|
+
if (entry.isDirectory()) await walk(childAbs, childRel);
|
|
160
|
+
else if (entry.isFile()) files.push(childRel);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
await walk(dir, '');
|
|
164
|
+
return files.sort();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function hashFileStreaming(file) {
|
|
168
|
+
const hash = crypto.createHash('sha256');
|
|
169
|
+
const handle = await fsp.open(file, 'r');
|
|
170
|
+
try {
|
|
171
|
+
const buffer = Buffer.allocUnsafe(1024 * 1024);
|
|
172
|
+
let position = 0;
|
|
173
|
+
for (;;) {
|
|
174
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, position);
|
|
175
|
+
if (bytesRead === 0) break;
|
|
176
|
+
position += bytesRead;
|
|
177
|
+
hash.update(buffer.subarray(0, bytesRead));
|
|
178
|
+
}
|
|
179
|
+
} finally {
|
|
180
|
+
await handle.close();
|
|
181
|
+
}
|
|
182
|
+
return hash.digest('hex');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async function mapLimit(items, limit, worker) {
|
|
186
|
+
const results = new Array(items.length);
|
|
187
|
+
let cursor = 0;
|
|
188
|
+
const runners = Array.from({ length: Math.min(limit, items.length) }, async () => {
|
|
189
|
+
for (;;) {
|
|
190
|
+
const index = cursor;
|
|
191
|
+
cursor += 1;
|
|
192
|
+
if (index >= items.length) return;
|
|
193
|
+
results[index] = await worker(items[index], index);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
await Promise.all(runners);
|
|
197
|
+
return results;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function resolveResourceDir(resource, roots) {
|
|
201
|
+
const candidates = [];
|
|
202
|
+
if (resource.localPath) {
|
|
203
|
+
if (path.isAbsolute(resource.localPath)) candidates.push(resource.localPath);
|
|
204
|
+
else for (const root of roots) candidates.push(path.resolve(root, resource.localPath));
|
|
205
|
+
}
|
|
206
|
+
return candidates;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export async function inventoryResource(resource, options = {}) {
|
|
210
|
+
const roots = options.roots ?? [path.dirname(options.frameworkRoot ?? process.cwd())];
|
|
211
|
+
const maxFileBytes = options.maxFileBytes ?? 64 * 1024 * 1024;
|
|
212
|
+
const concurrency = Math.max(1, Math.min(8, options.concurrency ?? 4));
|
|
213
|
+
const policy = licensePolicy(resource.license);
|
|
214
|
+
const base = {
|
|
215
|
+
id: resource.id,
|
|
216
|
+
name: resource.name,
|
|
217
|
+
kind: resource.kind,
|
|
218
|
+
origin: resource.origin,
|
|
219
|
+
license: policy.id,
|
|
220
|
+
licensePolicy: {
|
|
221
|
+
redistribution: policy.redistribution,
|
|
222
|
+
copy: policy.copy,
|
|
223
|
+
attribution: policy.attribution,
|
|
224
|
+
shareAlike: policy.shareAlike,
|
|
225
|
+
},
|
|
226
|
+
install: resource.install ?? 'reference',
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
if (resource.kind === 'reference' || !resource.localPath) {
|
|
230
|
+
return {
|
|
231
|
+
...base,
|
|
232
|
+
status: resource.status ?? 'NOT_INSTALLED',
|
|
233
|
+
path: null,
|
|
234
|
+
fileCount: 0,
|
|
235
|
+
totalBytes: 0,
|
|
236
|
+
treeHash: null,
|
|
237
|
+
files: [],
|
|
238
|
+
skills: [],
|
|
239
|
+
skipped: { secrets: 0, excluded: 0, oversized: 0 },
|
|
240
|
+
note: 'Reference-only resource; nothing is copied or hashed.',
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const dir = resolveResourceDir(resource, roots).find((candidate) => existsSync(candidate));
|
|
245
|
+
if (!dir) {
|
|
246
|
+
return {
|
|
247
|
+
...base,
|
|
248
|
+
status: 'ABSENT',
|
|
249
|
+
path: null,
|
|
250
|
+
fileCount: 0,
|
|
251
|
+
totalBytes: 0,
|
|
252
|
+
treeHash: null,
|
|
253
|
+
files: [],
|
|
254
|
+
skills: [],
|
|
255
|
+
skipped: { secrets: 0, excluded: 0, oversized: 0 },
|
|
256
|
+
note: 'No local copy found in the configured candidate paths.',
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const all = await listAllFiles(dir);
|
|
261
|
+
const secretCount = all.filter((rel) => isSecretPath(rel)).length;
|
|
262
|
+
const included = selectFiles(resource, all);
|
|
263
|
+
const skippedExcluded = all.length - included.length - secretCount;
|
|
264
|
+
|
|
265
|
+
const entries = [];
|
|
266
|
+
const oversized = [];
|
|
267
|
+
await mapLimit(included, concurrency, async (rel) => {
|
|
268
|
+
const abs = path.join(dir, rel);
|
|
269
|
+
const stat = await fsp.stat(abs);
|
|
270
|
+
if (stat.size > maxFileBytes) {
|
|
271
|
+
oversized.push(rel);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
const hash = await hashFileStreaming(abs);
|
|
275
|
+
entries.push({ path: rel, size: stat.size, sha256: hash });
|
|
276
|
+
});
|
|
277
|
+
entries.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
|
|
278
|
+
|
|
279
|
+
const treeHash = crypto.createHash('sha256')
|
|
280
|
+
.update(entries.map((e) => `${e.path}:${e.sha256}`).join('\n'))
|
|
281
|
+
.digest('hex');
|
|
282
|
+
|
|
283
|
+
const skills = [];
|
|
284
|
+
for (const entry of entries) {
|
|
285
|
+
if (!entry.path.endsWith('SKILL.md')) continue;
|
|
286
|
+
const content = await readText(path.join(dir, entry.path));
|
|
287
|
+
const validation = validateSkillContent(content, {
|
|
288
|
+
directoryName: path.basename(path.dirname(entry.path)),
|
|
289
|
+
});
|
|
290
|
+
skills.push({
|
|
291
|
+
name: validation.name ?? path.basename(path.dirname(entry.path)),
|
|
292
|
+
description: validation.description ?? '',
|
|
293
|
+
path: entry.path,
|
|
294
|
+
sha256: entry.sha256,
|
|
295
|
+
valid: validation.valid,
|
|
296
|
+
errors: validation.errors,
|
|
297
|
+
warnings: validation.warnings,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const totalBytes = entries.reduce((sum, entry) => sum + entry.size, 0);
|
|
302
|
+
let status = policy.redistribution === 'blocked' || policy.copy === false
|
|
303
|
+
? (policy.id === 'proprietary-owner' ? 'PARTIAL' : 'BLOCKED')
|
|
304
|
+
: 'PRESENT';
|
|
305
|
+
if (included.length === 0) status = 'PARTIAL';
|
|
306
|
+
|
|
307
|
+
return {
|
|
308
|
+
...base,
|
|
309
|
+
status,
|
|
310
|
+
path: toPosix(dir),
|
|
311
|
+
commit: resource.commit ?? null,
|
|
312
|
+
fileCount: entries.length,
|
|
313
|
+
totalBytes,
|
|
314
|
+
treeHash,
|
|
315
|
+
files: entries,
|
|
316
|
+
skills,
|
|
317
|
+
skipped: { secrets: secretCount, excluded: skippedExcluded, oversized: oversized.length },
|
|
318
|
+
note: policy.id === 'unknown'
|
|
319
|
+
? 'Licence is unknown: index-only, not redistributable.'
|
|
320
|
+
: policy.copy === false
|
|
321
|
+
? policy.note
|
|
322
|
+
: policy.note,
|
|
323
|
+
oversizedFiles: oversized,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function existsSync(candidate) {
|
|
328
|
+
try {
|
|
329
|
+
return fs.statSync(candidate).isDirectory();
|
|
330
|
+
} catch {
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export async function inventoryAll(sources, options = {}) {
|
|
336
|
+
const results = [];
|
|
337
|
+
for (const resource of sources.resources ?? []) {
|
|
338
|
+
results.push(await inventoryResource(resource, options));
|
|
339
|
+
}
|
|
340
|
+
return results;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
export function summarizeInventory(results) {
|
|
344
|
+
const byStatus = {};
|
|
345
|
+
const totals = {
|
|
346
|
+
resources: results.length,
|
|
347
|
+
files: 0,
|
|
348
|
+
bytes: 0,
|
|
349
|
+
skills: 0,
|
|
350
|
+
validSkills: 0,
|
|
351
|
+
secretsSkipped: 0,
|
|
352
|
+
oversized: 0,
|
|
353
|
+
};
|
|
354
|
+
for (const result of results) {
|
|
355
|
+
byStatus[result.status] = (byStatus[result.status] ?? 0) + 1;
|
|
356
|
+
totals.files += result.fileCount ?? 0;
|
|
357
|
+
totals.bytes += result.totalBytes ?? 0;
|
|
358
|
+
totals.skills += result.skills?.length ?? 0;
|
|
359
|
+
totals.validSkills += (result.skills ?? []).filter((s) => s.valid).length;
|
|
360
|
+
totals.secretsSkipped += result.skipped?.secrets ?? 0;
|
|
361
|
+
totals.oversized += result.skipped?.oversized ?? 0;
|
|
362
|
+
}
|
|
363
|
+
return { byStatus, ...totals };
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export function coverageReport(sources, results) {
|
|
367
|
+
const declared = (sources.resources ?? []).map((r) => r.id);
|
|
368
|
+
const inventoried = new Map(results.map((r) => [r.id, r]));
|
|
369
|
+
const missing = declared.filter((id) => !inventoried.has(id));
|
|
370
|
+
const skillNames = new Map();
|
|
371
|
+
for (const result of results) {
|
|
372
|
+
for (const skill of result.skills ?? []) {
|
|
373
|
+
const list = skillNames.get(skill.name) ?? [];
|
|
374
|
+
list.push({ resource: result.id, path: skill.path, sha256: skill.sha256, valid: skill.valid });
|
|
375
|
+
skillNames.set(skill.name, list);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
const duplicates = [...skillNames.entries()]
|
|
379
|
+
.filter(([, list]) => list.length > 1)
|
|
380
|
+
.map(([name, list]) => ({
|
|
381
|
+
name,
|
|
382
|
+
count: list.length,
|
|
383
|
+
identical: new Set(list.map((s) => s.sha256)).size === 1,
|
|
384
|
+
locations: list.map((s) => `${s.resource}:${s.path}`),
|
|
385
|
+
}));
|
|
386
|
+
const licenses = {};
|
|
387
|
+
for (const result of results) licenses[result.license] = (licenses[result.license] ?? 0) + 1;
|
|
388
|
+
return {
|
|
389
|
+
declaredResources: declared.length,
|
|
390
|
+
inventoriedResources: results.length,
|
|
391
|
+
missing,
|
|
392
|
+
complete: missing.length === 0 && results.length === declared.length,
|
|
393
|
+
licenses,
|
|
394
|
+
duplicateSkills: duplicates,
|
|
395
|
+
uniqueSkills: skillNames.size,
|
|
396
|
+
totalSkillFiles: [...skillNames.values()].reduce((sum, list) => sum + list.length, 0),
|
|
397
|
+
blockedResources: results.filter((r) => r.status === 'BLOCKED').map((r) => r.id),
|
|
398
|
+
absentResources: results.filter((r) => r.status === 'ABSENT').map((r) => r.id),
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export async function writeInventory(root, results) {
|
|
403
|
+
const file = path.join(root, '.aidf', 'registry', 'resources.manifest.json');
|
|
404
|
+
const payload = {
|
|
405
|
+
schemaVersion: 1,
|
|
406
|
+
generatedAt: new Date().toISOString(),
|
|
407
|
+
note: 'File-level manifest. Secret-like paths are never hashed or listed.',
|
|
408
|
+
resources: results,
|
|
409
|
+
};
|
|
410
|
+
await writeJson(file, payload);
|
|
411
|
+
return { file: toPosix(file), payload };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
export async function readInventory(root) {
|
|
415
|
+
return readJson(path.join(root, '.aidf', 'registry', 'resources.manifest.json'), null);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
export function reconcile(lock, manifest) {
|
|
419
|
+
const locked = lock?.resources ?? {};
|
|
420
|
+
const current = new Map((manifest?.resources ?? []).map((r) => [r.id, r]));
|
|
421
|
+
const drift = [];
|
|
422
|
+
for (const [id, entry] of Object.entries(locked)) {
|
|
423
|
+
const now = current.get(id);
|
|
424
|
+
if (!now) {
|
|
425
|
+
drift.push({ id, kind: 'MISSING', expected: entry.treeHash ?? null, actual: null });
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
if (entry.treeHash && now.treeHash && entry.treeHash !== now.treeHash) {
|
|
429
|
+
drift.push({ id, kind: 'CHANGED', expected: entry.treeHash, actual: now.treeHash });
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
const unchanged = Object.keys(locked).filter((id) => !drift.some((d) => d.id === id));
|
|
433
|
+
return { drift, unchanged, checked: Object.keys(locked).length };
|
|
434
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fsp from 'node:fs/promises';
|
|
3
|
+
import { parseYaml, dumpYaml } from '../util/yaml.mjs';
|
|
4
|
+
import {
|
|
5
|
+
readText, readJson, writeJson, exists, ensureDir, sha256Text, sha256File,
|
|
6
|
+
toPosix, listFiles, timestampSlug,
|
|
7
|
+
} from '../util/fsx.mjs';
|
|
8
|
+
import { aidfPaths, FRAMEWORK_ROOT } from './paths.mjs';
|
|
9
|
+
import { runTool } from './detect.mjs';
|
|
10
|
+
import { isSecretPath } from './resource-inventory.mjs';
|
|
11
|
+
|
|
12
|
+
export const RESOURCE_STATUSES = [
|
|
13
|
+
'PRESENT', 'ABSENT', 'PARTIAL', 'NOT_INSTALLED', 'BLOCKED', 'OUTDATED', 'NOT_APPLICABLE',
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
const MAX_HASH_FILE_BYTES = 64 * 1024 * 1024;
|
|
17
|
+
|
|
18
|
+
export async function loadSources(frameworkRoot = FRAMEWORK_ROOT) {
|
|
19
|
+
const file = path.join(frameworkRoot, 'assets', 'registry', 'sources.yaml');
|
|
20
|
+
return parseYaml(await readText(file));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function globToRegExp(glob) {
|
|
24
|
+
let out = '^';
|
|
25
|
+
for (let i = 0; i < glob.length; i += 1) {
|
|
26
|
+
const ch = glob[i];
|
|
27
|
+
if (ch === '*') {
|
|
28
|
+
if (glob[i + 1] === '*') {
|
|
29
|
+
out += '.*';
|
|
30
|
+
i += 1;
|
|
31
|
+
if (glob[i + 1] === '/') i += 1;
|
|
32
|
+
} else {
|
|
33
|
+
out += '[^/]*';
|
|
34
|
+
}
|
|
35
|
+
} else if (ch === '?') {
|
|
36
|
+
out += '[^/]';
|
|
37
|
+
} else if ('\\^$.|+()[]{}'.includes(ch)) {
|
|
38
|
+
out += `\\${ch}`;
|
|
39
|
+
} else {
|
|
40
|
+
out += ch;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return new RegExp(`${out}$`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function matchesGlobs(relPath, include = [], exclude = []) {
|
|
47
|
+
const p = toPosix(relPath);
|
|
48
|
+
if (exclude.some((g) => globToRegExp(g).test(p))) return false;
|
|
49
|
+
if (include.length === 0) return true;
|
|
50
|
+
return include.some((g) => globToRegExp(g).test(p));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function resolveCandidates(resource, roots) {
|
|
54
|
+
const candidates = [];
|
|
55
|
+
if (resource.localPath) {
|
|
56
|
+
if (path.isAbsolute(resource.localPath)) candidates.push(resource.localPath);
|
|
57
|
+
else for (const root of roots) candidates.push(path.resolve(root, resource.localPath));
|
|
58
|
+
}
|
|
59
|
+
return [...new Set(candidates)];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function gitMetadata(dir) {
|
|
63
|
+
if (!(await exists(path.join(dir, '.git')))) return null;
|
|
64
|
+
const commit = await runTool('git', ['rev-parse', 'HEAD'], { cwd: dir });
|
|
65
|
+
const remote = await runTool('git', ['remote', 'get-url', 'origin'], { cwd: dir });
|
|
66
|
+
return {
|
|
67
|
+
commit: commit.ok ? commit.stdout : null,
|
|
68
|
+
origin: remote.ok ? remote.stdout : null,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function findLicense(dir) {
|
|
73
|
+
for (const name of ['LICENSE', 'LICENSE.md', 'LICENSE.txt', 'COPYING']) {
|
|
74
|
+
const file = path.join(dir, name);
|
|
75
|
+
if (await exists(file)) {
|
|
76
|
+
const text = await readText(file);
|
|
77
|
+
return { file: name, firstLine: text.split(/\r?\n/)[0].slice(0, 120), hash: sha256Text(text) };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function inspectResource(resource, options = {}) {
|
|
84
|
+
const roots = options.roots ?? [FRAMEWORK_ROOT, path.dirname(FRAMEWORK_ROOT)];
|
|
85
|
+
const candidates = resolveCandidates(resource, roots);
|
|
86
|
+
const found = [];
|
|
87
|
+
for (const candidate of candidates) {
|
|
88
|
+
if (await exists(candidate)) found.push(candidate);
|
|
89
|
+
}
|
|
90
|
+
if (resource.kind === 'reference') {
|
|
91
|
+
return {
|
|
92
|
+
id: resource.id,
|
|
93
|
+
name: resource.name,
|
|
94
|
+
kind: resource.kind,
|
|
95
|
+
origin: resource.origin,
|
|
96
|
+
license: resource.license,
|
|
97
|
+
version: resource.version,
|
|
98
|
+
status: resource.status ?? 'NOT_INSTALLED',
|
|
99
|
+
path: null,
|
|
100
|
+
note: 'Reference-only resource; no local copy is installed.',
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
if (found.length === 0) {
|
|
104
|
+
return {
|
|
105
|
+
id: resource.id,
|
|
106
|
+
name: resource.name,
|
|
107
|
+
kind: resource.kind,
|
|
108
|
+
origin: resource.origin,
|
|
109
|
+
license: resource.license,
|
|
110
|
+
version: resource.version,
|
|
111
|
+
status: 'ABSENT',
|
|
112
|
+
path: null,
|
|
113
|
+
note: 'No local copy found in the configured candidate paths.',
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
const dir = found[0];
|
|
117
|
+
const git = await gitMetadata(dir);
|
|
118
|
+
const licenseFile = await findLicense(dir);
|
|
119
|
+
const files = await listFiles(dir, { ignore: resource.exclude ?? [] });
|
|
120
|
+
const included = files.filter((f) => matchesGlobs(f, resource.include ?? [], resource.exclude ?? []));
|
|
121
|
+
const hashed = [];
|
|
122
|
+
const skipped = { secrets: 0, oversized: 0 };
|
|
123
|
+
for (const f of included.slice(0, 5000)) {
|
|
124
|
+
if (isSecretPath(f)) {
|
|
125
|
+
skipped.secrets += 1;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const stat = await fsp.stat(path.join(dir, f)).catch(() => null);
|
|
129
|
+
if (!stat || stat.size > MAX_HASH_FILE_BYTES) {
|
|
130
|
+
skipped.oversized += 1;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
hashed.push(`${f}:${await sha256File(path.join(dir, f))}`);
|
|
134
|
+
}
|
|
135
|
+
const treeHash = sha256Text(hashed.join('\n'));
|
|
136
|
+
const licenseKnown = resource.license && resource.license !== 'unknown';
|
|
137
|
+
const status = !licenseKnown ? 'BLOCKED' : licenseFile ? 'PRESENT' : 'PARTIAL';
|
|
138
|
+
return {
|
|
139
|
+
id: resource.id,
|
|
140
|
+
name: resource.name,
|
|
141
|
+
kind: resource.kind,
|
|
142
|
+
origin: resource.origin,
|
|
143
|
+
license: resource.license,
|
|
144
|
+
licenseFile,
|
|
145
|
+
version: resource.version,
|
|
146
|
+
commit: git?.commit ?? resource.commit ?? null,
|
|
147
|
+
detectedOrigin: git?.origin ?? null,
|
|
148
|
+
status,
|
|
149
|
+
path: toPosix(dir),
|
|
150
|
+
files: included.length,
|
|
151
|
+
hashedFiles: hashed.length,
|
|
152
|
+
skipped,
|
|
153
|
+
treeHash,
|
|
154
|
+
note: !licenseKnown
|
|
155
|
+
? 'Licence is unknown: index-only, not redistributable.'
|
|
156
|
+
: licenseFile
|
|
157
|
+
? 'Local copy verified.'
|
|
158
|
+
: 'Local copy found but no licence file: redistribution blocked.',
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export async function setupResources(root, options = {}) {
|
|
163
|
+
const paths = aidfPaths(root);
|
|
164
|
+
const sources = await loadSources(options.frameworkRoot ?? FRAMEWORK_ROOT);
|
|
165
|
+
const results = [];
|
|
166
|
+
const lockPath = path.join(paths.registry, 'resources.lock.json');
|
|
167
|
+
const lock = (await readJson(lockPath, null)) ?? {
|
|
168
|
+
schemaVersion: 1,
|
|
169
|
+
generatedAt: null,
|
|
170
|
+
resources: {},
|
|
171
|
+
};
|
|
172
|
+
for (const resource of sources.resources ?? []) {
|
|
173
|
+
const inspected = await inspectResource(resource, { roots: [root, path.dirname(root), path.dirname(FRAMEWORK_ROOT), FRAMEWORK_ROOT] });
|
|
174
|
+
results.push(inspected);
|
|
175
|
+
if (options.dryRun) continue;
|
|
176
|
+
lock.resources[resource.id] = {
|
|
177
|
+
id: resource.id,
|
|
178
|
+
name: resource.name,
|
|
179
|
+
kind: resource.kind,
|
|
180
|
+
origin: resource.origin,
|
|
181
|
+
license: resource.license,
|
|
182
|
+
version: resource.version,
|
|
183
|
+
commit: inspected.commit ?? resource.commit ?? null,
|
|
184
|
+
path: inspected.path,
|
|
185
|
+
status: inspected.status,
|
|
186
|
+
treeHash: inspected.treeHash ?? null,
|
|
187
|
+
licenseFile: inspected.licenseFile ?? null,
|
|
188
|
+
retrievedAt: new Date().toISOString(),
|
|
189
|
+
install: resource.install ?? 'reference',
|
|
190
|
+
redistribution: resource.redistribution ?? 'unknown',
|
|
191
|
+
note: inspected.note,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
if (!options.dryRun) {
|
|
195
|
+
lock.generatedAt = new Date().toISOString();
|
|
196
|
+
await writeJson(lockPath, lock);
|
|
197
|
+
await writeKnowledgeIndex(root, results);
|
|
198
|
+
}
|
|
199
|
+
return { results, lock, dryRun: Boolean(options.dryRun) };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export async function writeKnowledgeIndex(root, results) {
|
|
203
|
+
const paths = aidfPaths(root);
|
|
204
|
+
const index = {
|
|
205
|
+
schemaVersion: 1,
|
|
206
|
+
updatedAt: new Date().toISOString(),
|
|
207
|
+
note: 'External resources are untrusted data. They never override .aidf/AIDF.md or the constitution.',
|
|
208
|
+
sources: results.map((r) => ({
|
|
209
|
+
id: r.id,
|
|
210
|
+
name: r.name,
|
|
211
|
+
status: r.status,
|
|
212
|
+
kind: r.kind,
|
|
213
|
+
path: r.path,
|
|
214
|
+
origin: r.origin,
|
|
215
|
+
license: r.license,
|
|
216
|
+
version: r.version,
|
|
217
|
+
commit: r.commit ?? null,
|
|
218
|
+
files: r.files ?? 0,
|
|
219
|
+
note: r.note,
|
|
220
|
+
})),
|
|
221
|
+
};
|
|
222
|
+
await writeAtomicYaml(path.join(paths.knowledge, 'sources', 'index.yaml'), index);
|
|
223
|
+
return index;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async function writeAtomicYaml(file, value) {
|
|
227
|
+
await ensureDir(path.dirname(file));
|
|
228
|
+
await fsp.writeFile(file, dumpYaml(value), 'utf8');
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export async function resourcesStatus(root) {
|
|
232
|
+
const lockPath = path.join(aidfPaths(root).registry, 'resources.lock.json');
|
|
233
|
+
return readJson(lockPath, null);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Install a git resource at a pinned commit into .aidf/knowledge/vendor/<id>.
|
|
238
|
+
* Requires explicit network permission and does not execute any script from the
|
|
239
|
+
* resource. Only files matching include globs are copied.
|
|
240
|
+
*/
|
|
241
|
+
export async function installGitResource(root, resource, options = {}) {
|
|
242
|
+
if (!options.allowNetwork) {
|
|
243
|
+
return { id: resource.id, status: 'BLOCKED', note: 'Network installation requires --allow-network.' };
|
|
244
|
+
}
|
|
245
|
+
if (!resource.commit) {
|
|
246
|
+
return { id: resource.id, status: 'BLOCKED', note: 'A pinned commit is required for a reproducible install.' };
|
|
247
|
+
}
|
|
248
|
+
if (!resource.license || resource.license === 'unknown') {
|
|
249
|
+
return { id: resource.id, status: 'BLOCKED', note: 'Unknown licence: installation refused.' };
|
|
250
|
+
}
|
|
251
|
+
const paths = aidfPaths(root);
|
|
252
|
+
const cache = path.join(paths.cache, 'vendor', resource.id);
|
|
253
|
+
const target = path.join(paths.knowledge, 'vendor', resource.id);
|
|
254
|
+
if (options.dryRun) {
|
|
255
|
+
return { id: resource.id, status: 'WOULD_INSTALL', note: `git clone ${resource.origin} @ ${resource.commit}` };
|
|
256
|
+
}
|
|
257
|
+
await ensureDir(path.dirname(cache));
|
|
258
|
+
if (!(await exists(cache))) {
|
|
259
|
+
const clone = await runTool('git', ['clone', '--quiet', resource.origin, cache], { timeout: 120000 });
|
|
260
|
+
if (!clone.ok) return { id: resource.id, status: 'FAILED', note: clone.stderr || clone.message };
|
|
261
|
+
}
|
|
262
|
+
const checkout = await runTool('git', ['checkout', '--quiet', resource.commit], { cwd: cache, timeout: 60000 });
|
|
263
|
+
if (!checkout.ok) return { id: resource.id, status: 'FAILED', note: checkout.stderr || checkout.message };
|
|
264
|
+
const files = await listFiles(cache, { ignore: resource.exclude ?? [] });
|
|
265
|
+
const included = files.filter((f) => matchesGlobs(f, resource.include ?? [], resource.exclude ?? []));
|
|
266
|
+
await ensureDir(target);
|
|
267
|
+
for (const rel of included) {
|
|
268
|
+
const dest = path.join(target, rel);
|
|
269
|
+
await ensureDir(path.dirname(dest));
|
|
270
|
+
await fsp.copyFile(path.join(cache, rel), dest);
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
id: resource.id,
|
|
274
|
+
status: 'INSTALLED',
|
|
275
|
+
path: toPosix(target),
|
|
276
|
+
files: included.length,
|
|
277
|
+
commit: resource.commit,
|
|
278
|
+
note: 'Pinned commit copied; no script executed.',
|
|
279
|
+
};
|
|
280
|
+
}
|