agjan 0.0.0-stage → 0.0.1
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/LICENSE +201 -0
- package/NOTICE +11 -0
- package/README.md +105 -2
- package/bin/agjan.mjs +12 -0
- package/docs/brand/agjan-icon.svg +14 -0
- package/docs/brand/agjan-logo-dark.svg +17 -0
- package/docs/brand/agjan-logo-light.svg +17 -0
- package/docs/guide.md +232 -0
- package/kit/domains/btp/domain.json +27 -0
- package/kit/hooks/guard.mjs +61 -0
- package/kit/mcp/servers.json +5 -0
- package/kit/skills/agjan-build/SKILL.md +61 -0
- package/kit/skills/agjan-design/SKILL.md +95 -0
- package/kit/skills/agjan-fix/SKILL.md +55 -0
- package/kit/skills/agjan-map/SKILL.md +51 -0
- package/kit/skills/agjan-spec/SKILL.md +111 -0
- package/kit/skills/agjan-verify/SKILL.md +60 -0
- package/kit/skills/btp-architect/SKILL.md +51 -0
- package/kit/skills/btp-architect/references/deployment.md +72 -0
- package/kit/skills/btp-architect/references/security.md +54 -0
- package/kit/skills/cap-developer/SKILL.md +49 -0
- package/kit/skills/cap-developer/references/handlers.md +47 -0
- package/kit/skills/cap-developer/references/modeling.md +52 -0
- package/kit/skills/cap-developer/references/services.md +53 -0
- package/kit/skills/cap-developer/references/testing.md +39 -0
- package/kit/skills/cap-developer/references/validation-security.md +49 -0
- package/kit/skills/fiori-developer/SKILL.md +64 -0
- package/kit/skills/fiori-developer/references/elements.md +77 -0
- package/kit/skills/fiori-developer/references/freestyle-ts.md +45 -0
- package/kit/skills/fiori-developer/references/testing.md +32 -0
- package/kit/skills/fiori-developer/references/ui5-basics.md +52 -0
- package/kit/skills/solution-reviewer/SKILL.md +51 -0
- package/kit/templates/agents-block.md +20 -0
- package/kit/templates/agjan-ci.yml +40 -0
- package/kit/templates/design.md +17 -0
- package/kit/templates/project.md +28 -0
- package/kit/templates/spec.md +33 -0
- package/kit/templates/tasks.md +13 -0
- package/package.json +50 -4
- package/schemas/approvals.schema.json +93 -0
- package/schemas/config.schema.json +79 -0
- package/schemas/log.schema.json +30 -0
- package/schemas/reviews.schema.json +68 -0
- package/schemas/tasks.schema.json +26 -0
- package/src/adapters/claude-code.mjs +95 -0
- package/src/adapters/copilot.mjs +88 -0
- package/src/baseline.mjs +66 -0
- package/src/cli.mjs +133 -0
- package/src/commands/check.mjs +363 -0
- package/src/commands/fix.mjs +116 -0
- package/src/commands/gates.mjs +264 -0
- package/src/commands/log.mjs +53 -0
- package/src/commands/map.mjs +47 -0
- package/src/commands/new.mjs +53 -0
- package/src/commands/pr-summary.mjs +134 -0
- package/src/commands/report.mjs +110 -0
- package/src/commands/review.mjs +49 -0
- package/src/commands/setup.mjs +269 -0
- package/src/commands/status.mjs +163 -0
- package/src/config.mjs +98 -0
- package/src/context.mjs +58 -0
- package/src/documents.mjs +188 -0
- package/src/errors.mjs +16 -0
- package/src/feature.mjs +214 -0
- package/src/fix.mjs +108 -0
- package/src/git.mjs +66 -0
- package/src/hash.mjs +33 -0
- package/src/hook.mjs +48 -0
- package/src/json-file.mjs +24 -0
- package/src/log.mjs +37 -0
- package/src/managed.mjs +178 -0
- package/src/mcp-config.mjs +83 -0
- package/src/mcp.mjs +142 -0
- package/src/plan.mjs +41 -0
- package/src/progress.mjs +85 -0
- package/src/review-policy.mjs +49 -0
- package/src/review.mjs +320 -0
- package/src/scope.mjs +30 -0
- package/src/terminal.mjs +98 -0
- package/src/test-results.mjs +111 -0
- package/src/tests-map.mjs +29 -0
- package/src/ui.mjs +110 -0
- package/src/usage.mjs +118 -0
- package/src/validate.mjs +41 -0
package/src/feature.mjs
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { hashText, hashParts } from './hash.mjs';
|
|
4
|
+
import { readJson } from './json-file.mjs';
|
|
5
|
+
import { parseSpec, parseDesign, parseTasks } from './documents.mjs';
|
|
6
|
+
import { changedFiles, resolveBase } from './git.mjs';
|
|
7
|
+
import { AgjanError, UsageError } from './errors.mjs';
|
|
8
|
+
|
|
9
|
+
export const FEATURES_DIR = 'docs/agjan';
|
|
10
|
+
export const APPROVALS_FILE = 'approvals.json';
|
|
11
|
+
export const REVIEWS_FILE = 'reviews.json';
|
|
12
|
+
/**
|
|
13
|
+
* Record folders under docs/agjan (decision 57). A unit's id is its path below
|
|
14
|
+
* docs/agjan: a feature `order-filter`, a fix `fixes/2026-10-17-order-label`, a map run
|
|
15
|
+
* `maps/2026-10-17`. All have approvals.json (key, accepted findings), reviews.json and
|
|
16
|
+
* log.jsonl; only features have spec, design and tasks.
|
|
17
|
+
*/
|
|
18
|
+
export const CONTAINERS = { fixes: 'fix', maps: 'map' };
|
|
19
|
+
const RESERVED = new Set(Object.keys(CONTAINERS));
|
|
20
|
+
|
|
21
|
+
export const SLUG = /^[a-z0-9][a-z0-9-]{0,47}$/;
|
|
22
|
+
export const KEY = /^[A-Z][A-Z0-9]{1,5}$/;
|
|
23
|
+
|
|
24
|
+
/** Review steps whose passing record allows each gate (design 7.3). */
|
|
25
|
+
const GATE_STEPS = {
|
|
26
|
+
spec: ['spec', 'design:light'],
|
|
27
|
+
design: ['design', 'design:light'],
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export function featureDir(root, slug) {
|
|
31
|
+
return join(root, FEATURES_DIR, slug);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function unitFolders(dir) {
|
|
35
|
+
if (!existsSync(dir)) return [];
|
|
36
|
+
return readdirSync(dir, { withFileTypes: true }).filter((e) => e.isDirectory() && existsSync(join(dir, e.name, APPROVALS_FILE)));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** All unit ids in the repository: features, then fixes and map runs. */
|
|
40
|
+
export function listFeatures(root) {
|
|
41
|
+
const dir = join(root, FEATURES_DIR);
|
|
42
|
+
const features = unitFolders(dir).filter((e) => !RESERVED.has(e.name)).map((e) => e.name).sort();
|
|
43
|
+
const others = Object.keys(CONTAINERS).flatMap((c) => unitFolders(join(dir, c)).map((e) => `${c}/${e.name}`).sort());
|
|
44
|
+
return [...features, ...others];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The unit id a repository path belongs to, or null (docs/agjan/project.md, ...). */
|
|
48
|
+
export function unitOf(file) {
|
|
49
|
+
const prefix = `${FEATURES_DIR}/`;
|
|
50
|
+
if (!file.startsWith(prefix)) return null;
|
|
51
|
+
const parts = file.slice(prefix.length).split('/');
|
|
52
|
+
if (parts.length < 2) return null;
|
|
53
|
+
if (RESERVED.has(parts[0])) return parts.length >= 3 ? `${parts[0]}/${parts[1]}` : null;
|
|
54
|
+
return parts[0];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Ids of the units whose folder changed relative to the merge-base (design 6.8). */
|
|
58
|
+
export function changedFeatures(root, config, options = {}) {
|
|
59
|
+
const ids = new Set();
|
|
60
|
+
for (const file of changedFiles(root, resolveBase(root, config.git?.base), options)) {
|
|
61
|
+
const id = unitOf(file);
|
|
62
|
+
if (id && existsSync(join(root, FEATURES_DIR, id, APPROVALS_FILE))) ids.add(id);
|
|
63
|
+
}
|
|
64
|
+
return [...ids].sort();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The one feature a command acts on: the explicit one, or the single changed one. */
|
|
68
|
+
export function selectFeature(root, config, explicit) {
|
|
69
|
+
if (explicit) {
|
|
70
|
+
if (!existsSync(join(featureDir(root, explicit), APPROVALS_FILE))) throw new AgjanError(`no feature '${explicit}' in ${FEATURES_DIR}`);
|
|
71
|
+
return explicit;
|
|
72
|
+
}
|
|
73
|
+
const changed = changedFeatures(root, config);
|
|
74
|
+
if (changed.length === 1) return changed[0];
|
|
75
|
+
if (changed.length === 0) throw new UsageError('no changed feature on this branch; pass --feature <slug>');
|
|
76
|
+
throw new UsageError(`several features changed (${changed.join(', ')}); pass --feature <slug>`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function readDoc(dir, name) {
|
|
80
|
+
const file = join(dir, name);
|
|
81
|
+
if (!existsSync(file)) return null;
|
|
82
|
+
const text = readFileSync(file, 'utf8');
|
|
83
|
+
return { file, text, hash: hashText(text) };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function emptyApprovals(slug, key, kit, now, kind = 'feature') {
|
|
87
|
+
return {
|
|
88
|
+
schema: 1,
|
|
89
|
+
feature: { slug, key, kit, created_at_utc: now, ...(kind === 'feature' ? {} : { kind }) },
|
|
90
|
+
approvals: {},
|
|
91
|
+
amendments: [],
|
|
92
|
+
accepted: [],
|
|
93
|
+
rejections: [],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function validateApprovals(data, slug) {
|
|
98
|
+
if (data?.schema !== 1 || typeof data.feature !== 'object' || data.feature === null) {
|
|
99
|
+
throw new AgjanError(`${FEATURES_DIR}/${slug}/${APPROVALS_FILE}: unsupported or damaged content`);
|
|
100
|
+
}
|
|
101
|
+
if (!KEY.test(data.feature.key ?? '')) throw new AgjanError(`${FEATURES_DIR}/${slug}/${APPROVALS_FILE}: invalid feature key`);
|
|
102
|
+
if (data.feature.kind !== undefined && !['fix', 'map'].includes(data.feature.kind)) {
|
|
103
|
+
throw new AgjanError(`${FEATURES_DIR}/${slug}/${APPROVALS_FILE}: unknown kind '${data.feature.kind}'`);
|
|
104
|
+
}
|
|
105
|
+
for (const list of ['amendments', 'accepted', 'rejections']) {
|
|
106
|
+
if (!Array.isArray(data[list])) throw new AgjanError(`${FEATURES_DIR}/${slug}/${APPROVALS_FILE}: '${list}' must be a list`);
|
|
107
|
+
}
|
|
108
|
+
if (typeof data.approvals !== 'object' || data.approvals === null) {
|
|
109
|
+
throw new AgjanError(`${FEATURES_DIR}/${slug}/${APPROVALS_FILE}: 'approvals' must be an object`);
|
|
110
|
+
}
|
|
111
|
+
return data;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Findings with their current status. A finding is resolved when a later record lists
|
|
116
|
+
* it in `resolves`, accepted when the human accepted it, and open otherwise.
|
|
117
|
+
*/
|
|
118
|
+
export function findingStatus(reviews, approvals) {
|
|
119
|
+
const accepted = new Set(approvals.accepted.map((a) => a.finding));
|
|
120
|
+
const resolved = new Set(reviews.records.flatMap((r) => r.resolves ?? []));
|
|
121
|
+
return reviews.records.flatMap((record) =>
|
|
122
|
+
(record.findings ?? []).map((f) => ({
|
|
123
|
+
...f,
|
|
124
|
+
step: record.step,
|
|
125
|
+
record: record.id,
|
|
126
|
+
status: accepted.has(f.id) ? 'accepted' : resolved.has(f.id) ? 'resolved' : 'open',
|
|
127
|
+
})),
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The latest review record for any of the given steps, or null. */
|
|
132
|
+
export function latestReview(reviews, steps) {
|
|
133
|
+
const matching = reviews.records.filter((r) => steps.includes(r.step));
|
|
134
|
+
return matching.length === 0 ? null : matching[matching.length - 1];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function docState(doc, approval, hash, extraOk = true) {
|
|
138
|
+
if (!doc) return 'missing';
|
|
139
|
+
if (!approval) return 'draft';
|
|
140
|
+
return approval.hash === hash && extraOk ? 'approved' : 'stale';
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Loads a feature and derives its state; nothing derived is stored (design 8.4). */
|
|
144
|
+
export function loadFeature(root, slug) {
|
|
145
|
+
const dir = featureDir(root, slug);
|
|
146
|
+
const approvals = validateApprovals(readJson(join(dir, APPROVALS_FILE)), slug);
|
|
147
|
+
const reviews = readJson(join(dir, REVIEWS_FILE)) ?? { schema: 1, records: [] };
|
|
148
|
+
if (!Array.isArray(reviews.records)) throw new AgjanError(`${FEATURES_DIR}/${slug}/${REVIEWS_FILE}: 'records' must be a list`);
|
|
149
|
+
const key = approvals.feature.key;
|
|
150
|
+
// Fixes and map runs have no documents of their own; their stage is their kind.
|
|
151
|
+
const kind = approvals.feature.kind ?? 'feature';
|
|
152
|
+
const isFeature = kind === 'feature';
|
|
153
|
+
|
|
154
|
+
const spec = isFeature ? readDoc(dir, 'spec.md') : null;
|
|
155
|
+
const design = isFeature ? readDoc(dir, 'design.md') : null;
|
|
156
|
+
const tasks = isFeature ? readDoc(dir, 'tasks.md') : null;
|
|
157
|
+
if (spec) spec.parsed = parseSpec(spec.text, key);
|
|
158
|
+
if (design) design.parsed = parseDesign(design.text);
|
|
159
|
+
if (tasks) tasks.parsed = parseTasks(tasks.text);
|
|
160
|
+
const designHash = design && tasks ? hashParts([['design.md', design.text], ['tasks.md', tasks.text]]) : null;
|
|
161
|
+
|
|
162
|
+
const specApproval = approvals.approvals.spec;
|
|
163
|
+
const designApproval = approvals.approvals.design;
|
|
164
|
+
const specState = docState(spec, specApproval, spec?.hash);
|
|
165
|
+
const designState = design && tasks
|
|
166
|
+
? docState(design, designApproval, designHash, designApproval?.spec_hash === spec?.hash && specState === 'approved')
|
|
167
|
+
: 'missing';
|
|
168
|
+
const stage = !isFeature ? kind : specState !== 'approved' ? 'spec' : designState !== 'approved' ? 'design' : 'build';
|
|
169
|
+
|
|
170
|
+
const amendments = approvals.amendments.map((a) => ({
|
|
171
|
+
...a,
|
|
172
|
+
status: !a.approved_at_utc ? 'pending' : a.design_hash === designApproval?.hash ? 'approved' : 'lapsed',
|
|
173
|
+
}));
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
slug,
|
|
177
|
+
key,
|
|
178
|
+
kind,
|
|
179
|
+
dir,
|
|
180
|
+
approvals,
|
|
181
|
+
reviews,
|
|
182
|
+
spec,
|
|
183
|
+
design,
|
|
184
|
+
tasks,
|
|
185
|
+
designHash,
|
|
186
|
+
state: { spec: specState, design: designState },
|
|
187
|
+
stage,
|
|
188
|
+
amendments,
|
|
189
|
+
findings: findingStatus(reviews, approvals),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Whether a gate has a passing, fresh review: the latest record for the gate's steps
|
|
195
|
+
* reviewed exactly the current documents and is not blocked (design 7.4).
|
|
196
|
+
*/
|
|
197
|
+
export function gateReview(feature, gate) {
|
|
198
|
+
const steps = GATE_STEPS[gate];
|
|
199
|
+
const record = latestReview(feature.reviews, steps);
|
|
200
|
+
const problems = [];
|
|
201
|
+
if (!record) {
|
|
202
|
+
problems.push(`no ${steps.join(' or ')} review recorded`);
|
|
203
|
+
} else {
|
|
204
|
+
const docs = record.documents ?? {};
|
|
205
|
+
if (docs.spec !== feature.spec?.hash) problems.push(`the latest ${record.step} review (${record.id}) did not review the current spec.md`);
|
|
206
|
+
if (gate === 'design' && docs.design !== feature.designHash) {
|
|
207
|
+
problems.push(`the latest ${record.step} review (${record.id}) did not review the current design.md and tasks.md`);
|
|
208
|
+
}
|
|
209
|
+
if (record.verdict === 'blocked') problems.push(`the latest ${record.step} review (${record.id}) is blocked`);
|
|
210
|
+
}
|
|
211
|
+
const blockers = feature.findings.filter((f) => f.status === 'open' && f.severity === 'blocker' && steps.includes(f.step));
|
|
212
|
+
for (const f of blockers) problems.push(`open blocker ${f.id}: ${f.issue}`);
|
|
213
|
+
return { record, problems };
|
|
214
|
+
}
|
package/src/fix.mjs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { git, mergeBase, resolveBase } from './git.mjs';
|
|
4
|
+
import { FEATURES_DIR, latestReview } from './feature.mjs';
|
|
5
|
+
import { treeHash } from './review.mjs';
|
|
6
|
+
import { matcher } from './scope.mjs';
|
|
7
|
+
|
|
8
|
+
export const FIX_FILE = 'fix.json';
|
|
9
|
+
export const FIX_MD = 'fix.md';
|
|
10
|
+
export const PROJECT_MD = `${FEATURES_DIR}/project.md`;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Paths named in the baseline's "Do-not-touch list": every `code span` in its bullets
|
|
14
|
+
* (design 8.6; writers often list several paths in one bullet). They are protected for
|
|
15
|
+
* fixes like `fix.protected`.
|
|
16
|
+
*/
|
|
17
|
+
export function doNotTouch(root) {
|
|
18
|
+
const file = join(root, PROJECT_MD);
|
|
19
|
+
if (!existsSync(file)) return [];
|
|
20
|
+
const lines = readFileSync(file, 'utf8').split(/\r?\n/);
|
|
21
|
+
const start = lines.findIndex((l) => l.trim() === '## Do-not-touch list');
|
|
22
|
+
if (start === -1) return [];
|
|
23
|
+
const out = [];
|
|
24
|
+
for (const line of lines.slice(start + 1)) {
|
|
25
|
+
if (/^## /.test(line)) break;
|
|
26
|
+
if (!/^\s*[-*]\s/.test(line)) continue;
|
|
27
|
+
for (const match of line.matchAll(/`([^`]+)`/g)) out.push(match[1]);
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function lineCount(root, file) {
|
|
33
|
+
try {
|
|
34
|
+
const text = readFileSync(join(root, file), 'utf8');
|
|
35
|
+
return text.length === 0 ? 0 : text.split('\n').length - (text.endsWith('\n') ? 1 : 0);
|
|
36
|
+
} catch {
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The fast-path rules (design 6.7) on the change relative to the merge-base: changed
|
|
43
|
+
* lines per file and in total within limits (ignored paths not counted), new files
|
|
44
|
+
* only under the configured test paths, and no protected or risk path touched.
|
|
45
|
+
*/
|
|
46
|
+
export function fixRules(root, config) {
|
|
47
|
+
const from = mergeBase(root, resolveBase(root, config.git?.base)) ?? 'HEAD';
|
|
48
|
+
const fix = config.fix ?? {};
|
|
49
|
+
const ignored = matcher(fix.ignored ?? []);
|
|
50
|
+
const own = (f) => f.startsWith(`${FEATURES_DIR}/`) || f.startsWith('.agjan/');
|
|
51
|
+
const newAllowed = matcher(fix.new_file_paths ?? Object.values(config.tests ?? {}).flat());
|
|
52
|
+
const protectedPaths = [...(fix.protected ?? []), ...(config.risk_paths ?? []), ...doNotTouch(root)];
|
|
53
|
+
const isProtected = matcher(protectedPaths);
|
|
54
|
+
|
|
55
|
+
const added = new Set();
|
|
56
|
+
for (const line of (git(root, ['diff', '--name-status', '--no-renames', from], { allowFail: true }) ?? '').split('\n')) {
|
|
57
|
+
const [status, file] = line.split('\t');
|
|
58
|
+
if (status === 'A') added.add(file);
|
|
59
|
+
}
|
|
60
|
+
const files = [];
|
|
61
|
+
for (const line of (git(root, ['diff', '--numstat', '--no-renames', from], { allowFail: true }) ?? '').split('\n').filter(Boolean)) {
|
|
62
|
+
const [a, d, file] = line.split('\t');
|
|
63
|
+
if (own(file)) continue;
|
|
64
|
+
// Binary files show '-': they count as one changed line each way, never as zero.
|
|
65
|
+
files.push({ path: file, added: a === '-' ? 1 : Number(a), deleted: d === '-' ? 1 : Number(d), isNew: added.has(file) });
|
|
66
|
+
}
|
|
67
|
+
for (const file of git(root, ['ls-files', '--others', '--exclude-standard']).split('\n').filter(Boolean)) {
|
|
68
|
+
if (own(file)) continue;
|
|
69
|
+
files.push({ path: file, added: lineCount(root, file), deleted: 0, isNew: true });
|
|
70
|
+
}
|
|
71
|
+
files.sort((x, y) => x.path.localeCompare(y.path));
|
|
72
|
+
|
|
73
|
+
const problems = [];
|
|
74
|
+
const counted = files.filter((f) => !ignored(f.path));
|
|
75
|
+
const total = counted.reduce((n, f) => n + f.added + f.deleted, 0);
|
|
76
|
+
if (files.length === 0) problems.push('nothing changed');
|
|
77
|
+
// Ignored paths (lockfiles, generated output) are outside every rule, not only the count.
|
|
78
|
+
for (const f of counted) {
|
|
79
|
+
if (f.isNew && !newAllowed(f.path)) problems.push(`${f.path} is a new file; a fix may only add files under the test paths`);
|
|
80
|
+
if (isProtected(f.path)) problems.push(`${f.path} is protected or a risk path; this needs /agjan-spec`);
|
|
81
|
+
}
|
|
82
|
+
for (const f of counted) {
|
|
83
|
+
const size = f.added + f.deleted;
|
|
84
|
+
if (size > fix.max_per_file) problems.push(`${f.path}: ${size} changed lines (limit per file ${fix.max_per_file})`);
|
|
85
|
+
}
|
|
86
|
+
if (total > fix.max_total) problems.push(`${total} changed lines in total (limit ${fix.max_total})`);
|
|
87
|
+
return {
|
|
88
|
+
files,
|
|
89
|
+
ignored: files.filter((f) => ignored(f.path)).map((f) => f.path),
|
|
90
|
+
total,
|
|
91
|
+
limits: { max_total: fix.max_total, max_per_file: fix.max_per_file },
|
|
92
|
+
problems,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The latest review of a fix or map run, if it passed and saw exactly the current
|
|
98
|
+
* change; otherwise the reason why not.
|
|
99
|
+
*/
|
|
100
|
+
export function freshReview(root, config, unit, step) {
|
|
101
|
+
const record = latestReview(unit.reviews, [step]);
|
|
102
|
+
if (!record) return { problem: `no ${step} review recorded` };
|
|
103
|
+
if (record.verdict === 'blocked') return { record, problem: `the latest ${step} review ${record.id} is blocked` };
|
|
104
|
+
if (record.tree_hash !== treeHash(root, config, unit.slug)) {
|
|
105
|
+
return { record, problem: `the latest ${step} review ${record.id} is stale: files changed after it` };
|
|
106
|
+
}
|
|
107
|
+
return { record };
|
|
108
|
+
}
|
package/src/git.mjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { AgjanError } from './errors.mjs';
|
|
3
|
+
|
|
4
|
+
/** Runs git and returns stdout. Throws AgjanError with git's message on failure. */
|
|
5
|
+
export function git(cwd, args, { allowFail = false } = {}) {
|
|
6
|
+
try {
|
|
7
|
+
return execFileSync('git', args, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
|
|
8
|
+
} catch (error) {
|
|
9
|
+
if (allowFail) return null;
|
|
10
|
+
const detail = error.stderr?.toString().trim() || error.message;
|
|
11
|
+
throw new AgjanError(`git ${args.join(' ')} failed: ${detail}`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function repoRoot(cwd) {
|
|
16
|
+
const out = git(cwd, ['rev-parse', '--show-toplevel'], { allowFail: true });
|
|
17
|
+
if (out === null) throw new AgjanError('not inside a git repository');
|
|
18
|
+
return out.trim();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function userEmail(root) {
|
|
22
|
+
return git(root, ['config', 'user.email'], { allowFail: true })?.trim() || 'unknown';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The base branch to compare against: the configured one, else the remote's default
|
|
27
|
+
* branch, else a local main or master.
|
|
28
|
+
*/
|
|
29
|
+
export function resolveBase(root, configured) {
|
|
30
|
+
if (configured) return configured;
|
|
31
|
+
const remoteHead = git(root, ['symbolic-ref', '--quiet', '--short', 'refs/remotes/origin/HEAD'], { allowFail: true });
|
|
32
|
+
if (remoteHead) return remoteHead.trim();
|
|
33
|
+
for (const name of ['main', 'master']) {
|
|
34
|
+
if (git(root, ['rev-parse', '--verify', '--quiet', `refs/heads/${name}`], { allowFail: true })) return name;
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The merge-base commit with the base branch, or null when there is none yet. */
|
|
40
|
+
export function mergeBase(root, base) {
|
|
41
|
+
if (!base) return null;
|
|
42
|
+
return git(root, ['merge-base', 'HEAD', base], { allowFail: true })?.trim() || null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function lines(out) {
|
|
46
|
+
return out ? out.split('\n').filter(Boolean) : [];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Files changed relative to the merge-base: committed, staged, unstaged and untracked.
|
|
51
|
+
* With `staged`, only the files in the index. Paths are POSIX (git always uses '/').
|
|
52
|
+
*/
|
|
53
|
+
export function changedFiles(root, base, { staged = false } = {}) {
|
|
54
|
+
if (staged) return lines(git(root, ['diff', '--cached', '--name-only', '--no-renames']));
|
|
55
|
+
const from = mergeBase(root, base);
|
|
56
|
+
const files = new Set();
|
|
57
|
+
if (from) {
|
|
58
|
+
for (const f of lines(git(root, ['diff', '--name-only', '--no-renames', from]))) files.add(f);
|
|
59
|
+
} else if (git(root, ['rev-parse', '--verify', '--quiet', 'HEAD'], { allowFail: true })) {
|
|
60
|
+
for (const f of lines(git(root, ['diff', '--name-only', '--no-renames', 'HEAD']))) files.add(f);
|
|
61
|
+
} else {
|
|
62
|
+
for (const f of lines(git(root, ['diff', '--cached', '--name-only', '--no-renames']))) files.add(f);
|
|
63
|
+
}
|
|
64
|
+
for (const f of lines(git(root, ['ls-files', '--others', '--exclude-standard']))) files.add(f);
|
|
65
|
+
return [...files].sort();
|
|
66
|
+
}
|
package/src/hash.mjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Normalizes text before hashing so that line endings and trailing whitespace never
|
|
5
|
+
* change a hash (design 8): LF line endings, no trailing whitespace on a line, no
|
|
6
|
+
* trailing blank lines, exactly one final newline.
|
|
7
|
+
*/
|
|
8
|
+
export function normalize(text) {
|
|
9
|
+
const lines = text.replace(/\r\n?/g, '\n').split('\n').map((line) => line.replace(/[ \t]+$/, ''));
|
|
10
|
+
while (lines.length > 0 && lines[lines.length - 1] === '') lines.pop();
|
|
11
|
+
return lines.length === 0 ? '' : `${lines.join('\n')}\n`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function sha256(data) {
|
|
15
|
+
return `sha256:${createHash('sha256').update(data).digest('hex')}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Hash of one document's content. */
|
|
19
|
+
export function hashText(text) {
|
|
20
|
+
return sha256(normalize(text));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Hash of several named documents taken together, for example the design hash over
|
|
25
|
+
* design.md and tasks.md. Names are part of the hash, so swapping contents changes it.
|
|
26
|
+
*/
|
|
27
|
+
export function hashParts(parts) {
|
|
28
|
+
const hash = createHash('sha256');
|
|
29
|
+
for (const [name, text] of parts) {
|
|
30
|
+
hash.update(name).update('\0').update(normalize(text)).update('\0');
|
|
31
|
+
}
|
|
32
|
+
return `sha256:${hash.digest('hex')}`;
|
|
33
|
+
}
|
package/src/hook.mjs
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { isAbsolute, join } from 'node:path';
|
|
3
|
+
import { git } from './git.mjs';
|
|
4
|
+
|
|
5
|
+
/** The command the hook runs; also what a husky pre-commit file must contain. */
|
|
6
|
+
export const HOOK_COMMAND = 'npx --no-install agjan check --staged';
|
|
7
|
+
const MARKER = '# agjan pre-commit hook';
|
|
8
|
+
|
|
9
|
+
const HOOK = `#!/bin/sh
|
|
10
|
+
${MARKER} (managed by agjan; reinstall with: npx agjan hook install)
|
|
11
|
+
# Fast checks on the staged change. CI runs the full check; --no-verify skips this.
|
|
12
|
+
exec ${HOOK_COMMAND}
|
|
13
|
+
`;
|
|
14
|
+
|
|
15
|
+
/** Where the pre-commit hook lives: husky if the project uses it, else git's hooks dir. */
|
|
16
|
+
function target(root) {
|
|
17
|
+
const husky = join(root, '.husky');
|
|
18
|
+
if (existsSync(husky)) return { kind: 'husky', path: join(husky, 'pre-commit') };
|
|
19
|
+
const dir = git(root, ['rev-parse', '--git-path', 'hooks']).trim();
|
|
20
|
+
return { kind: 'git', path: join(isAbsolute(dir) ? dir : join(root, dir), 'pre-commit') };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** installed | missing | foreign (a hook agjan did not write and will not touch). */
|
|
24
|
+
export function hookStatus(root) {
|
|
25
|
+
const { kind, path } = target(root);
|
|
26
|
+
if (!existsSync(path)) return { kind, path, state: 'missing' };
|
|
27
|
+
const text = readFileSync(path, 'utf8');
|
|
28
|
+
if (text.includes(HOOK_COMMAND)) return { kind, path, state: 'installed' };
|
|
29
|
+
return { kind, path, state: kind === 'husky' ? 'missing' : 'foreign' };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Installs the hook. Never overwrites a foreign git hook; appends to husky's file. */
|
|
33
|
+
export function installHook(root) {
|
|
34
|
+
const status = hookStatus(root);
|
|
35
|
+
if (status.state === 'installed') return { ...status, action: 'unchanged' };
|
|
36
|
+
if (status.state === 'foreign') {
|
|
37
|
+
return { ...status, action: `not installed: ${status.path} exists and is not agjan's; add this line to it: ${HOOK_COMMAND}` };
|
|
38
|
+
}
|
|
39
|
+
if (status.kind === 'husky') {
|
|
40
|
+
const current = existsSync(status.path) ? readFileSync(status.path, 'utf8') : '';
|
|
41
|
+
writeFileSync(status.path, `${current.replace(/\s*$/, '')}${current.trim() ? '\n' : ''}${HOOK_COMMAND}\n`);
|
|
42
|
+
return { ...status, state: 'installed', action: 'added to husky' };
|
|
43
|
+
}
|
|
44
|
+
mkdirSync(join(status.path, '..'), { recursive: true });
|
|
45
|
+
writeFileSync(status.path, HOOK);
|
|
46
|
+
chmodSync(status.path, 0o755);
|
|
47
|
+
return { ...status, state: 'installed', action: 'installed' };
|
|
48
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync, renameSync } from 'node:fs';
|
|
2
|
+
import { AgjanError } from './errors.mjs';
|
|
3
|
+
|
|
4
|
+
export function readJson(file) {
|
|
5
|
+
let text;
|
|
6
|
+
try {
|
|
7
|
+
text = readFileSync(file, 'utf8');
|
|
8
|
+
} catch (error) {
|
|
9
|
+
if (error.code === 'ENOENT') return null;
|
|
10
|
+
throw error;
|
|
11
|
+
}
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(text);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
throw new AgjanError(`${file} is not valid JSON: ${error.message}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Writes JSON with stable formatting, atomically (write then rename). */
|
|
20
|
+
export function writeJson(file, value) {
|
|
21
|
+
const tmp = `${file}.tmp-${process.pid}`;
|
|
22
|
+
writeFileSync(tmp, `${JSON.stringify(value, null, 2)}\n`);
|
|
23
|
+
renameSync(tmp, file);
|
|
24
|
+
}
|
package/src/log.mjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { appendFileSync, readFileSync, existsSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
|
|
4
|
+
export const LOG_FILE = 'log.jsonl';
|
|
5
|
+
|
|
6
|
+
/** Event types (design 12.1). Script events are written by the commands themselves. */
|
|
7
|
+
export const EVENTS = [
|
|
8
|
+
'step', // a skill starts a step (from `agjan log`)
|
|
9
|
+
'gate-requested', // a skill hands a gate to the human (from `agjan log`)
|
|
10
|
+
'approved',
|
|
11
|
+
'rejected',
|
|
12
|
+
'accepted',
|
|
13
|
+
'amendment-proposed',
|
|
14
|
+
'review-recorded',
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
/** Appends one event to the feature's log.jsonl. The time is always set by the script. */
|
|
18
|
+
export function appendLog(featureDir, at, event, fields = {}) {
|
|
19
|
+
const entry = { at_utc: at, event, ...fields };
|
|
20
|
+
appendFileSync(join(featureDir, LOG_FILE), `${JSON.stringify(entry)}\n`);
|
|
21
|
+
return entry;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function readLog(featureDir) {
|
|
25
|
+
const file = join(featureDir, LOG_FILE);
|
|
26
|
+
if (!existsSync(file)) return [];
|
|
27
|
+
return readFileSync(file, 'utf8')
|
|
28
|
+
.split('\n')
|
|
29
|
+
.filter(Boolean)
|
|
30
|
+
.flatMap((line) => {
|
|
31
|
+
try {
|
|
32
|
+
return [JSON.parse(line)];
|
|
33
|
+
} catch {
|
|
34
|
+
return []; // a damaged line is skipped, not fatal: the log is observability only
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|