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,210 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { aidfPaths } from './paths.mjs';
|
|
3
|
+
import {
|
|
4
|
+
readJson, writeJson, readDirSafe, readText, sha256Text, exists, timestampSlug,
|
|
5
|
+
} from '../util/fsx.mjs';
|
|
6
|
+
import { runTool } from './detect.mjs';
|
|
7
|
+
|
|
8
|
+
export const EVIDENCE_STATUSES = ['PASS', 'BLOCKED', 'FAIL', 'NOT_PROVEN', 'NOT_APPLICABLE'];
|
|
9
|
+
|
|
10
|
+
export async function loadEvidenceIndex(root) {
|
|
11
|
+
const file = path.join(aidfPaths(root).evidence, 'index.json');
|
|
12
|
+
return (await readJson(file, null)) ?? { schemaVersion: 1, updatedAt: null, lastHash: null, entries: [] };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function saveEvidenceIndex(root, index) {
|
|
16
|
+
index.updatedAt = new Date().toISOString();
|
|
17
|
+
await writeJson(path.join(aidfPaths(root).evidence, 'index.json'), index);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function recordEvidence(root, entry) {
|
|
21
|
+
const index = await loadEvidenceIndex(root);
|
|
22
|
+
const createdAt = new Date().toISOString();
|
|
23
|
+
const body = {
|
|
24
|
+
schemaVersion: 1,
|
|
25
|
+
id: entry.id ?? `ev-${timestampSlug()}-${Math.random().toString(16).slice(2, 8)}`,
|
|
26
|
+
type: entry.type,
|
|
27
|
+
status: entry.status ?? 'NOT_PROVEN',
|
|
28
|
+
gate: entry.gate ?? null,
|
|
29
|
+
phase: entry.phase ?? null,
|
|
30
|
+
summary: entry.summary ?? '',
|
|
31
|
+
note: entry.note ?? '',
|
|
32
|
+
createdBy: entry.createdBy ?? 'aidf',
|
|
33
|
+
humanApproval: Boolean(entry.humanApproval),
|
|
34
|
+
approvedBy: entry.approvedBy ?? null,
|
|
35
|
+
// `approvedBy` is kept for historical compatibility. It is a *declared*
|
|
36
|
+
// identity only; authentication is never inferred from it.
|
|
37
|
+
approval: entry.approval ?? {
|
|
38
|
+
declaredBy: entry.humanApproval ? (entry.approvedBy ?? null) : null,
|
|
39
|
+
declaredAt: entry.humanApproval ? createdAt : null,
|
|
40
|
+
authenticated: false,
|
|
41
|
+
method: entry.humanApproval ? 'declared' : 'none',
|
|
42
|
+
keyId: null,
|
|
43
|
+
},
|
|
44
|
+
createdAt,
|
|
45
|
+
checks: entry.checks ?? [],
|
|
46
|
+
commands: entry.commands ?? [],
|
|
47
|
+
files: entry.files ?? [],
|
|
48
|
+
limitations: entry.limitations ?? [],
|
|
49
|
+
previous: index.lastHash,
|
|
50
|
+
};
|
|
51
|
+
const hash = sha256Text(JSON.stringify(body));
|
|
52
|
+
body.hash = hash;
|
|
53
|
+
const file = path.join(aidfPaths(root).evidence, `${timestampSlug()}-${entry.type}-${hash.slice(0, 8)}.json`);
|
|
54
|
+
await writeJson(file, body);
|
|
55
|
+
index.entries.push({
|
|
56
|
+
id: body.id,
|
|
57
|
+
type: body.type,
|
|
58
|
+
status: body.status,
|
|
59
|
+
gate: body.gate,
|
|
60
|
+
phase: body.phase,
|
|
61
|
+
createdAt,
|
|
62
|
+
humanApproval: body.humanApproval,
|
|
63
|
+
approvedBy: body.approvedBy,
|
|
64
|
+
authenticatedApproval: Boolean(body.approval?.authenticated),
|
|
65
|
+
approvalMethod: body.approval?.method ?? null,
|
|
66
|
+
file: path.relative(root, file).replace(/\\/g, '/'),
|
|
67
|
+
hash,
|
|
68
|
+
});
|
|
69
|
+
index.lastHash = hash;
|
|
70
|
+
await saveEvidenceIndex(root, index);
|
|
71
|
+
return { ...body, file: path.relative(root, file).replace(/\\/g, '/') };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function listEvidence(root, filter = {}) {
|
|
75
|
+
const index = await loadEvidenceIndex(root);
|
|
76
|
+
return index.entries
|
|
77
|
+
.filter((e) => !filter.type || e.type === filter.type)
|
|
78
|
+
.filter((e) => !filter.status || e.status === filter.status)
|
|
79
|
+
.filter((e) => !filter.gate || e.gate === filter.gate)
|
|
80
|
+
.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function findEvidence(root, filter = {}) {
|
|
84
|
+
return listEvidence(root, filter);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export async function captureFiles(root, options = {}) {
|
|
88
|
+
const git = await runTool('git', ['rev-parse', '--is-inside-work-tree'], { cwd: root });
|
|
89
|
+
if (!git.ok || git.stdout !== 'true') return { files: [], source: 'not-a-git-repository' };
|
|
90
|
+
const args = ['diff', '--name-only'];
|
|
91
|
+
if (options.staged) args.push('--cached');
|
|
92
|
+
if (options.since) args.push(options.since);
|
|
93
|
+
const diff = await runTool('git', args, { cwd: root });
|
|
94
|
+
const untracked = await runTool('git', ['ls-files', '--others', '--exclude-standard'], { cwd: root });
|
|
95
|
+
const files = [...new Set([
|
|
96
|
+
...(diff.ok ? diff.stdout.split(/\r?\n/).filter(Boolean) : []),
|
|
97
|
+
...(untracked.ok ? untracked.stdout.split(/\r?\n/).filter(Boolean) : []),
|
|
98
|
+
])];
|
|
99
|
+
return { files, source: 'git' };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export async function verifyEvidenceFile(root, entry) {
|
|
103
|
+
if (!entry.file) return { ok: false, reason: 'No file recorded.' };
|
|
104
|
+
const file = path.join(root, entry.file);
|
|
105
|
+
if (!(await exists(file))) return { ok: false, reason: 'Evidence file is missing.' };
|
|
106
|
+
const body = JSON.parse(await readText(file));
|
|
107
|
+
const copy = { ...body };
|
|
108
|
+
delete copy.hash;
|
|
109
|
+
const expected = sha256Text(JSON.stringify(copy));
|
|
110
|
+
if (expected !== body.hash) return { ok: false, reason: 'Evidence hash does not match content.' };
|
|
111
|
+
return { ok: true };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Verify the whole evidence chain: every record hash, every `previous` link,
|
|
116
|
+
* chronological order, index consistency and unindexed/missing files.
|
|
117
|
+
* Never repairs or rewrites history: inconsistencies are reported.
|
|
118
|
+
*/
|
|
119
|
+
export async function verifyEvidenceChain(root) {
|
|
120
|
+
const problems = [];
|
|
121
|
+
const add = (code, file, reason) => problems.push({ code, file: file ?? null, reason });
|
|
122
|
+
const index = await loadEvidenceIndex(root);
|
|
123
|
+
if (!Array.isArray(index.entries)) {
|
|
124
|
+
add('INVALID_INDEX', null, 'index.entries is not an array.');
|
|
125
|
+
return { ok: false, checked: 0, problems };
|
|
126
|
+
}
|
|
127
|
+
const entries = index.entries;
|
|
128
|
+
const seenIds = new Set();
|
|
129
|
+
const seenFiles = new Set();
|
|
130
|
+
let previous = null;
|
|
131
|
+
let previousAt = null;
|
|
132
|
+
|
|
133
|
+
for (const entry of entries) {
|
|
134
|
+
if (!entry || typeof entry !== 'object' || !entry.file || !entry.id) {
|
|
135
|
+
add('INVALID_ENTRY', entry?.file ?? null, 'Index entry requires an id and a file.');
|
|
136
|
+
previous = entry?.hash ?? previous;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (seenIds.has(entry.id)) add('DUPLICATE_ID', entry.file, `Duplicate evidence id: ${entry.id}.`);
|
|
140
|
+
seenIds.add(entry.id);
|
|
141
|
+
if (seenFiles.has(entry.file)) add('DUPLICATE_FILE', entry.file, 'The same evidence file is indexed more than once.');
|
|
142
|
+
seenFiles.add(entry.file);
|
|
143
|
+
|
|
144
|
+
const abs = path.join(root, entry.file);
|
|
145
|
+
if (!(await exists(abs))) {
|
|
146
|
+
add('MISSING_FILE', entry.file, 'Evidence file is missing.');
|
|
147
|
+
previous = entry.hash ?? previous;
|
|
148
|
+
previousAt = entry.createdAt ?? previousAt;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
let body;
|
|
153
|
+
try {
|
|
154
|
+
body = JSON.parse(await readText(abs));
|
|
155
|
+
} catch (error) {
|
|
156
|
+
add('INVALID_JSON', entry.file, `Evidence file is not valid JSON: ${error.message}`);
|
|
157
|
+
previous = entry.hash ?? previous;
|
|
158
|
+
previousAt = entry.createdAt ?? previousAt;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const copy = { ...body };
|
|
163
|
+
delete copy.hash;
|
|
164
|
+
if (sha256Text(JSON.stringify(copy)) !== body.hash) {
|
|
165
|
+
add('HASH_MISMATCH', entry.file, 'Record content does not match its stored hash.');
|
|
166
|
+
}
|
|
167
|
+
if (entry.hash !== body.hash) add('INDEX_HASH_MISMATCH', entry.file, 'Index hash differs from record hash.');
|
|
168
|
+
if (entry.id !== body.id) add('INDEX_ID_MISMATCH', entry.file, 'Index id differs from record id.');
|
|
169
|
+
if (entry.status !== undefined && entry.status !== body.status) {
|
|
170
|
+
add('INDEX_STATUS_MISMATCH', entry.file, 'Index status differs from record status.');
|
|
171
|
+
}
|
|
172
|
+
if ((body.previous ?? null) !== (previous ?? null)) {
|
|
173
|
+
add('CHAIN_BROKEN', entry.file, `Expected previous ${previous ?? 'null'}, found ${body.previous ?? 'null'}.`);
|
|
174
|
+
}
|
|
175
|
+
if (previousAt && entry.createdAt && String(entry.createdAt) < String(previousAt)) {
|
|
176
|
+
add('ORDER', entry.file, 'Entry is older than its predecessor in the chain.');
|
|
177
|
+
}
|
|
178
|
+
previous = body.hash;
|
|
179
|
+
previousAt = entry.createdAt ?? previousAt;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (entries.length > 0 && index.lastHash !== previous) {
|
|
183
|
+
add('INDEX_LAST_HASH_MISMATCH', null, 'index.lastHash does not match the last record hash.');
|
|
184
|
+
}
|
|
185
|
+
if (entries.length === 0 && index.lastHash) {
|
|
186
|
+
add('INDEX_LAST_HASH_MISMATCH', null, 'index.lastHash is set but the index contains no entries.');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const dir = aidfPaths(root).evidence;
|
|
190
|
+
let files = [];
|
|
191
|
+
try {
|
|
192
|
+
files = (await readDirSafe(dir))
|
|
193
|
+
.filter((e) => e.isFile() && e.name.endsWith('.json') && e.name !== 'index.json')
|
|
194
|
+
.map((e) => path.relative(root, path.join(dir, e.name)).replace(/\\/g, '/'));
|
|
195
|
+
} catch {
|
|
196
|
+
files = [];
|
|
197
|
+
}
|
|
198
|
+
const referenced = new Set([...seenFiles].map((f) => String(f).replace(/\\/g, '/')));
|
|
199
|
+
for (const file of files) {
|
|
200
|
+
if (!referenced.has(file)) add('UNINDEXED_RECORD', file, 'Record file is not referenced by the index.');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return { ok: problems.length === 0, checked: entries.length, problems };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export async function listEvidenceFiles(root) {
|
|
207
|
+
const dir = aidfPaths(root).evidence;
|
|
208
|
+
const entries = await readDirSafe(dir);
|
|
209
|
+
return entries.filter((e) => e.isFile() && e.name.endsWith('.json')).map((e) => e.name);
|
|
210
|
+
}
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fsp from 'node:fs/promises';
|
|
3
|
+
import {
|
|
4
|
+
resolveInside, exists, ensureDir, readText, writeAtomic, sha256Text, sha256File,
|
|
5
|
+
timestampSlug, toPosix,
|
|
6
|
+
} from '../util/fsx.mjs';
|
|
7
|
+
import { replaceManagedBlock } from '../util/text.mjs';
|
|
8
|
+
import { recordFile, fileRecord } from './manifest.mjs';
|
|
9
|
+
|
|
10
|
+
export const ACTION_STATUS = {
|
|
11
|
+
CREATED: 'CREATED',
|
|
12
|
+
UPDATED: 'UPDATED',
|
|
13
|
+
UNCHANGED: 'UNCHANGED',
|
|
14
|
+
CONFLICT: 'CONFLICT',
|
|
15
|
+
LINKED: 'LINKED',
|
|
16
|
+
WOULD_CREATE: 'WOULD_CREATE',
|
|
17
|
+
WOULD_UPDATE: 'WOULD_UPDATE',
|
|
18
|
+
WOULD_CONFLICT: 'WOULD_CONFLICT',
|
|
19
|
+
WOULD_LINK: 'WOULD_LINK',
|
|
20
|
+
FAILED: 'FAILED',
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function stripJsonComments(text) {
|
|
24
|
+
let out = '';
|
|
25
|
+
let inString = false;
|
|
26
|
+
let escaped = false;
|
|
27
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
28
|
+
const ch = text[i];
|
|
29
|
+
const next = text[i + 1];
|
|
30
|
+
if (inString) {
|
|
31
|
+
out += ch;
|
|
32
|
+
if (escaped) escaped = false;
|
|
33
|
+
else if (ch === '\\') escaped = true;
|
|
34
|
+
else if (ch === '"') inString = false;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (ch === '"') {
|
|
38
|
+
inString = true;
|
|
39
|
+
out += ch;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (ch === '/' && next === '/') {
|
|
43
|
+
while (i < text.length && text[i] !== '\n') i += 1;
|
|
44
|
+
out += '\n';
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (ch === '/' && next === '*') {
|
|
48
|
+
i += 2;
|
|
49
|
+
while (i < text.length && !(text[i] === '*' && text[i + 1] === '/')) i += 1;
|
|
50
|
+
i += 1;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
out += ch;
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function parseJsonLoose(text) {
|
|
59
|
+
try {
|
|
60
|
+
return JSON.parse(text);
|
|
61
|
+
} catch {
|
|
62
|
+
return JSON.parse(stripJsonComments(text).replace(/,\s*([}\]])/g, '$1'));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function mergeArrays(base, patch) {
|
|
67
|
+
const out = [...base];
|
|
68
|
+
const seen = new Set(base.map((item) => JSON.stringify(item)));
|
|
69
|
+
for (const item of patch) {
|
|
70
|
+
const key = JSON.stringify(item);
|
|
71
|
+
if (!seen.has(key)) {
|
|
72
|
+
out.push(item);
|
|
73
|
+
seen.add(key);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function mergeJson(base, patch) {
|
|
80
|
+
if (Array.isArray(base) && Array.isArray(patch)) return mergeArrays(base, patch);
|
|
81
|
+
if (base && typeof base === 'object' && patch && typeof patch === 'object') {
|
|
82
|
+
const out = { ...base };
|
|
83
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
84
|
+
if (!(key in out)) out[key] = value;
|
|
85
|
+
else if (out[key] && typeof out[key] === 'object' && value && typeof value === 'object') {
|
|
86
|
+
out[key] = mergeJson(out[key], value);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return out;
|
|
90
|
+
}
|
|
91
|
+
return base;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export class Installer {
|
|
95
|
+
constructor(options) {
|
|
96
|
+
this.root = path.resolve(options.root);
|
|
97
|
+
this.manifest = options.manifest;
|
|
98
|
+
this.logger = options.logger;
|
|
99
|
+
this.dryRun = Boolean(options.dryRun);
|
|
100
|
+
this.force = Boolean(options.force);
|
|
101
|
+
this.backupDir = path.join(this.root, '.aidf', 'backups', timestampSlug());
|
|
102
|
+
this.results = [];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async backup(relPath, absPath) {
|
|
106
|
+
if (this.dryRun) return;
|
|
107
|
+
const target = resolveInside(this.backupDir, relPath);
|
|
108
|
+
await ensureDir(path.dirname(target));
|
|
109
|
+
await fsp.copyFile(absPath, target);
|
|
110
|
+
this.manifest.backups.push({ path: relPath, backup: toPosix(path.relative(this.root, target)), ts: new Date().toISOString() });
|
|
111
|
+
if (this.manifest.backups.length > 200) this.manifest.backups = this.manifest.backups.slice(-200);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
push(result) {
|
|
115
|
+
this.results.push(result);
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async apply(actions) {
|
|
120
|
+
const start = this.results.length;
|
|
121
|
+
for (const action of actions) {
|
|
122
|
+
try {
|
|
123
|
+
await this.applyOne(action);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
this.push({
|
|
126
|
+
path: action.path ?? action.blockId ?? '?',
|
|
127
|
+
kind: action.kind,
|
|
128
|
+
status: ACTION_STATUS.FAILED,
|
|
129
|
+
reason: error.message,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return this.results.slice(start);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async applyOne(action) {
|
|
137
|
+
switch (action.kind) {
|
|
138
|
+
case 'mkdir':
|
|
139
|
+
return this.mkdir(action);
|
|
140
|
+
case 'write':
|
|
141
|
+
return this.write(action);
|
|
142
|
+
case 'copy':
|
|
143
|
+
return this.copy(action);
|
|
144
|
+
case 'managed-block':
|
|
145
|
+
return this.managedBlock(action);
|
|
146
|
+
case 'merge-json':
|
|
147
|
+
return this.mergeJsonFile(action);
|
|
148
|
+
case 'junction':
|
|
149
|
+
return this.junction(action);
|
|
150
|
+
default:
|
|
151
|
+
throw new Error(`Unknown action kind: ${action.kind}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async mkdir(action) {
|
|
156
|
+
const abs = resolveInside(this.root, action.path);
|
|
157
|
+
if (await exists(abs)) {
|
|
158
|
+
return this.push({ path: action.path, kind: 'mkdir', status: ACTION_STATUS.UNCHANGED });
|
|
159
|
+
}
|
|
160
|
+
if (this.dryRun) {
|
|
161
|
+
return this.push({ path: action.path, kind: 'mkdir', status: ACTION_STATUS.WOULD_CREATE });
|
|
162
|
+
}
|
|
163
|
+
await ensureDir(abs);
|
|
164
|
+
return this.push({ path: action.path, kind: 'mkdir', status: ACTION_STATUS.CREATED });
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async write(action) {
|
|
168
|
+
const abs = resolveInside(this.root, action.path);
|
|
169
|
+
const content = action.content ?? '';
|
|
170
|
+
const nextHash = sha256Text(content);
|
|
171
|
+
const record = fileRecord(this.manifest, action.path);
|
|
172
|
+
const fileExists = await exists(abs);
|
|
173
|
+
|
|
174
|
+
if (fileExists) {
|
|
175
|
+
const currentHash = await sha256File(abs);
|
|
176
|
+
// Frozen actions are user-owned content created once by AIDF (project
|
|
177
|
+
// documents, editable registries, workflows). AIDF never rewrites them,
|
|
178
|
+
// from install, update, or update --force: the human owns the content.
|
|
179
|
+
if (action.frozen && currentHash !== nextHash) {
|
|
180
|
+
return this.push({
|
|
181
|
+
path: action.path,
|
|
182
|
+
kind: 'write',
|
|
183
|
+
status: ACTION_STATUS.UNCHANGED,
|
|
184
|
+
reason: 'AIDF-owned template exists; user content is never rewritten (frozen).',
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
if (currentHash === nextHash) {
|
|
188
|
+
if (!record) {
|
|
189
|
+
recordFile(this.manifest, action.path, {
|
|
190
|
+
sha256: nextHash,
|
|
191
|
+
category: action.category ?? 'unknown',
|
|
192
|
+
owner: 'aidf',
|
|
193
|
+
adopted: true,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
return this.push({ path: action.path, kind: 'write', status: ACTION_STATUS.UNCHANGED });
|
|
197
|
+
}
|
|
198
|
+
if (record && currentHash === record.sha256) {
|
|
199
|
+
if (this.dryRun) return this.push({ path: action.path, kind: 'write', status: ACTION_STATUS.WOULD_UPDATE });
|
|
200
|
+
await writeAtomic(abs, content);
|
|
201
|
+
recordFile(this.manifest, action.path, {
|
|
202
|
+
...record,
|
|
203
|
+
sha256: nextHash,
|
|
204
|
+
category: action.category ?? record.category,
|
|
205
|
+
updatedAt: new Date().toISOString(),
|
|
206
|
+
});
|
|
207
|
+
return this.push({ path: action.path, kind: 'write', status: ACTION_STATUS.UPDATED });
|
|
208
|
+
}
|
|
209
|
+
if (this.force) {
|
|
210
|
+
if (this.dryRun) return this.push({ path: action.path, kind: 'write', status: ACTION_STATUS.WOULD_UPDATE, reason: 'force' });
|
|
211
|
+
await this.backup(action.path, abs);
|
|
212
|
+
await writeAtomic(abs, content);
|
|
213
|
+
recordFile(this.manifest, action.path, {
|
|
214
|
+
sha256: nextHash,
|
|
215
|
+
category: action.category ?? 'unknown',
|
|
216
|
+
owner: 'aidf',
|
|
217
|
+
forced: true,
|
|
218
|
+
});
|
|
219
|
+
return this.push({ path: action.path, kind: 'write', status: ACTION_STATUS.UPDATED, reason: 'force' });
|
|
220
|
+
}
|
|
221
|
+
return this.push({
|
|
222
|
+
path: action.path,
|
|
223
|
+
kind: 'write',
|
|
224
|
+
status: this.dryRun ? ACTION_STATUS.WOULD_CONFLICT : ACTION_STATUS.CONFLICT,
|
|
225
|
+
reason: record
|
|
226
|
+
? 'File is AIDF-managed but was modified locally; use --force to overwrite (a backup is kept).'
|
|
227
|
+
: 'File exists and is not managed by AIDF; it will not be overwritten.',
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (this.dryRun) return this.push({ path: action.path, kind: 'write', status: ACTION_STATUS.WOULD_CREATE });
|
|
232
|
+
await writeAtomic(abs, content);
|
|
233
|
+
recordFile(this.manifest, action.path, {
|
|
234
|
+
sha256: nextHash,
|
|
235
|
+
category: action.category ?? 'unknown',
|
|
236
|
+
owner: 'aidf',
|
|
237
|
+
});
|
|
238
|
+
return this.push({ path: action.path, kind: 'write', status: ACTION_STATUS.CREATED });
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async copy(action) {
|
|
242
|
+
const sourceAbs = path.resolve(action.source);
|
|
243
|
+
const sourceContent = await fsp.readFile(sourceAbs);
|
|
244
|
+
return this.write({ ...action, content: sourceContent.toString('utf8'), path: action.path });
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async managedBlock(action) {
|
|
248
|
+
const abs = resolveInside(this.root, action.path);
|
|
249
|
+
const blockId = action.blockId ?? 'default';
|
|
250
|
+
const fileExists = await exists(abs);
|
|
251
|
+
const current = fileExists ? await readText(abs) : '';
|
|
252
|
+
const next = replaceManagedBlock(current, action.content, blockId);
|
|
253
|
+
if (next === current) {
|
|
254
|
+
return this.push({ path: action.path, kind: 'managed-block', status: ACTION_STATUS.UNCHANGED });
|
|
255
|
+
}
|
|
256
|
+
if (this.dryRun) {
|
|
257
|
+
return this.push({
|
|
258
|
+
path: action.path,
|
|
259
|
+
kind: 'managed-block',
|
|
260
|
+
status: fileExists ? ACTION_STATUS.WOULD_UPDATE : ACTION_STATUS.WOULD_CREATE,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
if (fileExists) await this.backup(action.path, abs);
|
|
264
|
+
await writeAtomic(abs, next);
|
|
265
|
+
recordFile(this.manifest, action.path, {
|
|
266
|
+
sha256: sha256Text(next),
|
|
267
|
+
category: action.category ?? 'instructions',
|
|
268
|
+
owner: 'aidf',
|
|
269
|
+
managedBlock: blockId,
|
|
270
|
+
});
|
|
271
|
+
return this.push({
|
|
272
|
+
path: action.path,
|
|
273
|
+
kind: 'managed-block',
|
|
274
|
+
status: fileExists ? ACTION_STATUS.UPDATED : ACTION_STATUS.CREATED,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async mergeJsonFile(action) {
|
|
279
|
+
const abs = resolveInside(this.root, action.path);
|
|
280
|
+
const fileExists = await exists(abs);
|
|
281
|
+
let current = {};
|
|
282
|
+
if (fileExists) {
|
|
283
|
+
const text = await readText(abs);
|
|
284
|
+
try {
|
|
285
|
+
current = parseJsonLoose(text);
|
|
286
|
+
} catch (error) {
|
|
287
|
+
return this.push({
|
|
288
|
+
path: action.path,
|
|
289
|
+
kind: 'merge-json',
|
|
290
|
+
status: this.dryRun ? ACTION_STATUS.WOULD_CONFLICT : ACTION_STATUS.CONFLICT,
|
|
291
|
+
reason: `Existing file is not valid JSON/JSONC: ${error.message}`,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
if (current === null || typeof current !== 'object' || Array.isArray(current)) {
|
|
295
|
+
return this.push({
|
|
296
|
+
path: action.path,
|
|
297
|
+
kind: 'merge-json',
|
|
298
|
+
status: this.dryRun ? ACTION_STATUS.WOULD_CONFLICT : ACTION_STATUS.CONFLICT,
|
|
299
|
+
reason: 'Existing JSON root is not an object.',
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
const merged = mergeJson(current, action.patch ?? {});
|
|
304
|
+
const nextText = `${JSON.stringify(merged, null, 2)}\n`;
|
|
305
|
+
if (fileExists) {
|
|
306
|
+
const currentText = await readText(abs);
|
|
307
|
+
if (currentText === nextText) {
|
|
308
|
+
return this.push({ path: action.path, kind: 'merge-json', status: ACTION_STATUS.UNCHANGED });
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (this.dryRun) {
|
|
312
|
+
return this.push({
|
|
313
|
+
path: action.path,
|
|
314
|
+
kind: 'merge-json',
|
|
315
|
+
status: fileExists ? ACTION_STATUS.WOULD_UPDATE : ACTION_STATUS.WOULD_CREATE,
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
if (fileExists) await this.backup(action.path, abs);
|
|
319
|
+
await writeAtomic(abs, nextText);
|
|
320
|
+
recordFile(this.manifest, action.path, {
|
|
321
|
+
sha256: sha256Text(nextText),
|
|
322
|
+
category: action.category ?? 'config',
|
|
323
|
+
owner: 'aidf',
|
|
324
|
+
mergeJson: true,
|
|
325
|
+
});
|
|
326
|
+
return this.push({
|
|
327
|
+
path: action.path,
|
|
328
|
+
kind: 'merge-json',
|
|
329
|
+
status: fileExists ? ACTION_STATUS.UPDATED : ACTION_STATUS.CREATED,
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async junction(action) {
|
|
334
|
+
const abs = resolveInside(this.root, action.path);
|
|
335
|
+
const target = path.resolve(action.target);
|
|
336
|
+
if (await exists(abs)) {
|
|
337
|
+
try {
|
|
338
|
+
const stat = await fsp.lstat(abs);
|
|
339
|
+
if (stat.isSymbolicLink()) {
|
|
340
|
+
const current = await fsp.readlink(abs);
|
|
341
|
+
if (path.resolve(current) === target) {
|
|
342
|
+
return this.push({ path: action.path, kind: 'junction', status: ACTION_STATUS.UNCHANGED });
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
} catch {
|
|
346
|
+
// fall through to conflict
|
|
347
|
+
}
|
|
348
|
+
return this.push({
|
|
349
|
+
path: action.path,
|
|
350
|
+
kind: 'junction',
|
|
351
|
+
status: this.dryRun ? ACTION_STATUS.WOULD_CONFLICT : ACTION_STATUS.CONFLICT,
|
|
352
|
+
reason: 'Path already exists and is not the expected link.',
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
if (this.dryRun) return this.push({ path: action.path, kind: 'junction', status: ACTION_STATUS.WOULD_LINK });
|
|
356
|
+
await ensureDir(path.dirname(abs));
|
|
357
|
+
await fsp.symlink(target, abs, process.platform === 'win32' ? 'junction' : 'dir');
|
|
358
|
+
this.manifest.links[action.path] = { target: toPosix(target), ts: new Date().toISOString() };
|
|
359
|
+
return this.push({ path: action.path, kind: 'junction', status: ACTION_STATUS.LINKED });
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export function summarizeResults(results) {
|
|
364
|
+
const summary = {
|
|
365
|
+
created: 0, updated: 0, unchanged: 0, conflicts: 0, failed: 0, linked: 0, would: 0,
|
|
366
|
+
};
|
|
367
|
+
for (const r of results) {
|
|
368
|
+
if (r.status === ACTION_STATUS.CREATED) summary.created += 1;
|
|
369
|
+
else if (r.status === ACTION_STATUS.UPDATED) summary.updated += 1;
|
|
370
|
+
else if (r.status === ACTION_STATUS.UNCHANGED) summary.unchanged += 1;
|
|
371
|
+
else if (r.status === ACTION_STATUS.CONFLICT || r.status === ACTION_STATUS.WOULD_CONFLICT) summary.conflicts += 1;
|
|
372
|
+
else if (r.status === ACTION_STATUS.FAILED) summary.failed += 1;
|
|
373
|
+
else if (r.status === ACTION_STATUS.LINKED) summary.linked += 1;
|
|
374
|
+
else if (r.status.startsWith('WOULD_')) summary.would += 1;
|
|
375
|
+
}
|
|
376
|
+
return summary;
|
|
377
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { aidfPaths, AIDF_VERSION } from './paths.mjs';
|
|
2
|
+
import { readJson, writeJson } from '../util/fsx.mjs';
|
|
3
|
+
|
|
4
|
+
export const MANIFEST_SCHEMA_VERSION = 1;
|
|
5
|
+
|
|
6
|
+
export function emptyManifest(project = {}) {
|
|
7
|
+
return {
|
|
8
|
+
schemaVersion: MANIFEST_SCHEMA_VERSION,
|
|
9
|
+
framework: {
|
|
10
|
+
name: 'aidf',
|
|
11
|
+
version: AIDF_VERSION,
|
|
12
|
+
},
|
|
13
|
+
project: {
|
|
14
|
+
name: project.name ?? 'unnamed-project',
|
|
15
|
+
slug: project.slug ?? 'unnamed-project',
|
|
16
|
+
},
|
|
17
|
+
adapter: project.adapter ?? 'generic',
|
|
18
|
+
installedAt: new Date().toISOString(),
|
|
19
|
+
updatedAt: new Date().toISOString(),
|
|
20
|
+
files: {},
|
|
21
|
+
managedBlocks: {},
|
|
22
|
+
links: {},
|
|
23
|
+
backups: [],
|
|
24
|
+
history: [],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function loadManifest(root) {
|
|
29
|
+
const manifest = await readJson(aidfPaths(root).manifest, null);
|
|
30
|
+
if (!manifest) throw new Error(`AIDF manifest not found in ${root}.`);
|
|
31
|
+
return manifest;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function saveManifest(root, manifest) {
|
|
35
|
+
manifest.updatedAt = new Date().toISOString();
|
|
36
|
+
await writeJson(aidfPaths(root).manifest, manifest);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function recordFile(manifest, relPath, entry) {
|
|
40
|
+
manifest.files[relPath] = {
|
|
41
|
+
...entry,
|
|
42
|
+
installedAt: entry.installedAt ?? new Date().toISOString(),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function fileRecord(manifest, relPath) {
|
|
47
|
+
return manifest.files[relPath] ?? null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function appendHistory(manifest, event) {
|
|
51
|
+
manifest.history.push({ ts: new Date().toISOString(), ...event });
|
|
52
|
+
if (manifest.history.length > 500) manifest.history = manifest.history.slice(-500);
|
|
53
|
+
}
|