agjan 0.0.11 → 0.0.12
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/README.md +2 -2
- package/bin/agjan.mjs +9 -2
- package/dist/adapters/claude-code.js +105 -0
- package/dist/adapters/copilot.js +80 -0
- package/dist/auto.js +24 -0
- package/dist/baseline.js +66 -0
- package/dist/browser.js +46 -0
- package/dist/cli.js +164 -0
- package/dist/commands/branch.js +71 -0
- package/dist/commands/check.js +420 -0
- package/dist/commands/fix.js +122 -0
- package/dist/commands/gates.js +462 -0
- package/dist/commands/lint.js +76 -0
- package/dist/commands/log.js +72 -0
- package/dist/commands/map.js +47 -0
- package/dist/commands/new.js +65 -0
- package/dist/commands/pr-summary.js +168 -0
- package/dist/commands/progress.js +42 -0
- package/dist/commands/report.js +105 -0
- package/dist/commands/review.js +146 -0
- package/dist/commands/setup.js +393 -0
- package/dist/commands/status.js +303 -0
- package/dist/commands/ui-test.js +39 -0
- package/dist/config.js +110 -0
- package/dist/context.js +56 -0
- package/dist/copilot-mcp.js +56 -0
- package/dist/documents.js +207 -0
- package/dist/errors.js +15 -0
- package/dist/feature.js +221 -0
- package/dist/fix.js +117 -0
- package/dist/git.js +88 -0
- package/{src/hash.mjs → dist/hash.js} +11 -14
- package/dist/hook.js +47 -0
- package/dist/json-file.js +26 -0
- package/dist/log.js +50 -0
- package/dist/managed.js +182 -0
- package/dist/mcp-config.js +76 -0
- package/dist/mcp.js +142 -0
- package/dist/plan.js +40 -0
- package/dist/progress-view.js +494 -0
- package/dist/progress.js +106 -0
- package/{src/review-policy.mjs → dist/review-policy.js} +39 -47
- package/dist/review.js +435 -0
- package/dist/scope.js +30 -0
- package/dist/terminal.js +127 -0
- package/dist/test-results.js +101 -0
- package/dist/tests-map.js +29 -0
- package/dist/types.js +5 -0
- package/dist/ui.js +136 -0
- package/dist/usage.js +116 -0
- package/dist/validate.js +46 -0
- package/docs/guide.md +62 -38
- package/kit/hooks/guard.mjs +48 -119
- package/kit/skills/agjan-build/SKILL.md +4 -2
- package/kit/skills/agjan-design/SKILL.md +6 -2
- package/kit/skills/agjan-fix/SKILL.md +8 -16
- package/kit/skills/agjan-map/SKILL.md +8 -16
- package/kit/skills/agjan-spec/SKILL.md +21 -17
- package/kit/skills/agjan-verify/SKILL.md +20 -13
- package/kit/templates/agents-block.md +3 -3
- package/package.json +11 -3
- package/schemas/approvals.schema.json +23 -2
- package/schemas/config.schema.json +0 -5
- package/schemas/log.schema.json +1 -1
- package/src/adapters/claude-code.mjs +0 -121
- package/src/adapters/copilot.mjs +0 -89
- package/src/baseline.mjs +0 -66
- package/src/browser.mjs +0 -49
- package/src/cli.mjs +0 -165
- package/src/commands/branch.mjs +0 -95
- package/src/commands/check.mjs +0 -376
- package/src/commands/fix.mjs +0 -116
- package/src/commands/gates.mjs +0 -267
- package/src/commands/lint.mjs +0 -74
- package/src/commands/log.mjs +0 -60
- package/src/commands/map.mjs +0 -47
- package/src/commands/new.mjs +0 -53
- package/src/commands/pr-summary.mjs +0 -137
- package/src/commands/progress.mjs +0 -40
- package/src/commands/report.mjs +0 -115
- package/src/commands/review.mjs +0 -141
- package/src/commands/setup.mjs +0 -393
- package/src/commands/status.mjs +0 -250
- package/src/commands/ui-test.mjs +0 -38
- package/src/config.mjs +0 -122
- package/src/context.mjs +0 -67
- package/src/copilot-mcp.mjs +0 -56
- package/src/documents.mjs +0 -188
- package/src/errors.mjs +0 -16
- package/src/feature.mjs +0 -225
- package/src/fix.mjs +0 -108
- package/src/git.mjs +0 -83
- package/src/hook.mjs +0 -48
- package/src/json-file.mjs +0 -24
- package/src/log.mjs +0 -51
- package/src/managed.mjs +0 -178
- package/src/mcp-config.mjs +0 -83
- package/src/mcp.mjs +0 -147
- package/src/plan.mjs +0 -45
- package/src/progress-view.mjs +0 -466
- package/src/progress.mjs +0 -85
- package/src/review.mjs +0 -416
- package/src/scope.mjs +0 -30
- package/src/terminal.mjs +0 -118
- package/src/test-results.mjs +0 -111
- package/src/tests-map.mjs +0 -29
- package/src/ui.mjs +0 -136
- package/src/usage.mjs +0 -118
- package/src/validate.mjs +0 -41
package/src/feature.mjs
DELETED
|
@@ -1,225 +0,0 @@
|
|
|
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
|
-
* Whether a review still blocks: its verdict is blocked and the human has not accepted
|
|
116
|
-
* every blocker it raised (design 7.4: no open blocker, unless accepted). An accepted
|
|
117
|
-
* blocker leaves the correction loop, so the step needs no further round for it.
|
|
118
|
-
*/
|
|
119
|
-
export function stillBlocked(feature, record) {
|
|
120
|
-
if (record?.verdict !== 'blocked') return false;
|
|
121
|
-
const status = new Map(feature.findings.map((f) => [f.id, f.status]));
|
|
122
|
-
return (record.findings ?? []).some((f) => f.severity === 'blocker' && status.get(f.id) !== 'accepted');
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Findings with their current status. A finding is resolved when a later record lists
|
|
127
|
-
* it in `resolves`, accepted when the human accepted it, and open otherwise.
|
|
128
|
-
*/
|
|
129
|
-
export function findingStatus(reviews, approvals) {
|
|
130
|
-
const accepted = new Set(approvals.accepted.map((a) => a.finding));
|
|
131
|
-
const resolved = new Set(reviews.records.flatMap((r) => r.resolves ?? []));
|
|
132
|
-
return reviews.records.flatMap((record) =>
|
|
133
|
-
(record.findings ?? []).map((f) => ({
|
|
134
|
-
...f,
|
|
135
|
-
step: record.step,
|
|
136
|
-
record: record.id,
|
|
137
|
-
status: accepted.has(f.id) ? 'accepted' : resolved.has(f.id) ? 'resolved' : 'open',
|
|
138
|
-
})),
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/** The latest review record for any of the given steps, or null. */
|
|
143
|
-
export function latestReview(reviews, steps) {
|
|
144
|
-
const matching = reviews.records.filter((r) => steps.includes(r.step));
|
|
145
|
-
return matching.length === 0 ? null : matching[matching.length - 1];
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
function docState(doc, approval, hash, extraOk = true) {
|
|
149
|
-
if (!doc) return 'missing';
|
|
150
|
-
if (!approval) return 'draft';
|
|
151
|
-
return approval.hash === hash && extraOk ? 'approved' : 'stale';
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/** Loads a feature and derives its state; nothing derived is stored (design 8.4). */
|
|
155
|
-
export function loadFeature(root, slug) {
|
|
156
|
-
const dir = featureDir(root, slug);
|
|
157
|
-
const approvals = validateApprovals(readJson(join(dir, APPROVALS_FILE)), slug);
|
|
158
|
-
const reviews = readJson(join(dir, REVIEWS_FILE)) ?? { schema: 1, records: [] };
|
|
159
|
-
if (!Array.isArray(reviews.records)) throw new AgjanError(`${FEATURES_DIR}/${slug}/${REVIEWS_FILE}: 'records' must be a list`);
|
|
160
|
-
const key = approvals.feature.key;
|
|
161
|
-
// Fixes and map runs have no documents of their own; their stage is their kind.
|
|
162
|
-
const kind = approvals.feature.kind ?? 'feature';
|
|
163
|
-
const isFeature = kind === 'feature';
|
|
164
|
-
|
|
165
|
-
const spec = isFeature ? readDoc(dir, 'spec.md') : null;
|
|
166
|
-
const design = isFeature ? readDoc(dir, 'design.md') : null;
|
|
167
|
-
const tasks = isFeature ? readDoc(dir, 'tasks.md') : null;
|
|
168
|
-
if (spec) spec.parsed = parseSpec(spec.text, key);
|
|
169
|
-
if (design) design.parsed = parseDesign(design.text);
|
|
170
|
-
if (tasks) tasks.parsed = parseTasks(tasks.text);
|
|
171
|
-
const designHash = design && tasks ? hashParts([['design.md', design.text], ['tasks.md', tasks.text]]) : null;
|
|
172
|
-
|
|
173
|
-
const specApproval = approvals.approvals.spec;
|
|
174
|
-
const designApproval = approvals.approvals.design;
|
|
175
|
-
const specState = docState(spec, specApproval, spec?.hash);
|
|
176
|
-
const designState = design && tasks
|
|
177
|
-
? docState(design, designApproval, designHash, designApproval?.spec_hash === spec?.hash && specState === 'approved')
|
|
178
|
-
: 'missing';
|
|
179
|
-
const stage = !isFeature ? kind : specState !== 'approved' ? 'spec' : designState !== 'approved' ? 'design' : 'build';
|
|
180
|
-
|
|
181
|
-
const amendments = approvals.amendments.map((a) => ({
|
|
182
|
-
...a,
|
|
183
|
-
status: !a.approved_at_utc ? 'pending' : a.design_hash === designApproval?.hash ? 'approved' : 'lapsed',
|
|
184
|
-
}));
|
|
185
|
-
|
|
186
|
-
return {
|
|
187
|
-
slug,
|
|
188
|
-
key,
|
|
189
|
-
kind,
|
|
190
|
-
dir,
|
|
191
|
-
approvals,
|
|
192
|
-
reviews,
|
|
193
|
-
spec,
|
|
194
|
-
design,
|
|
195
|
-
tasks,
|
|
196
|
-
designHash,
|
|
197
|
-
state: { spec: specState, design: designState },
|
|
198
|
-
stage,
|
|
199
|
-
amendments,
|
|
200
|
-
findings: findingStatus(reviews, approvals),
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Whether a gate has a passing, fresh review: the latest record for the gate's steps
|
|
206
|
-
* reviewed exactly the current documents and is not blocked (design 7.4).
|
|
207
|
-
*/
|
|
208
|
-
export function gateReview(feature, gate) {
|
|
209
|
-
const steps = GATE_STEPS[gate];
|
|
210
|
-
const record = latestReview(feature.reviews, steps);
|
|
211
|
-
const problems = [];
|
|
212
|
-
if (!record) {
|
|
213
|
-
problems.push(`no ${steps.join(' or ')} review recorded`);
|
|
214
|
-
} else {
|
|
215
|
-
const docs = record.documents ?? {};
|
|
216
|
-
if (docs.spec !== feature.spec?.hash) problems.push(`the latest ${record.step} review (${record.id}) did not review the current spec.md`);
|
|
217
|
-
if (gate === 'design' && docs.design !== feature.designHash) {
|
|
218
|
-
problems.push(`the latest ${record.step} review (${record.id}) did not review the current design.md and tasks.md`);
|
|
219
|
-
}
|
|
220
|
-
if (stillBlocked(feature, record)) problems.push(`the latest ${record.step} review (${record.id}) is blocked`);
|
|
221
|
-
}
|
|
222
|
-
const blockers = feature.findings.filter((f) => f.status === 'open' && f.severity === 'blocker' && steps.includes(f.step));
|
|
223
|
-
for (const f of blockers) problems.push(`open blocker ${f.id}: ${f.issue}`);
|
|
224
|
-
return { record, problems };
|
|
225
|
-
}
|
package/src/fix.mjs
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
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, stillBlocked } 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 (stillBlocked(unit, record)) 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
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
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
|
-
/**
|
|
40
|
-
* The checked-out branch: the local branch name, or in a pull request job (detached HEAD)
|
|
41
|
-
* the PR's head branch from the CI environment. Null when there is none.
|
|
42
|
-
*/
|
|
43
|
-
export function currentBranch(root, env = process.env) {
|
|
44
|
-
const name = git(root, ['symbolic-ref', '--quiet', '--short', 'HEAD'], { allowFail: true })?.trim();
|
|
45
|
-
return name || env.GITHUB_HEAD_REF || env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME || null;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** The merge-base commit with the base branch, or null when there is none yet. */
|
|
49
|
-
export function mergeBase(root, base) {
|
|
50
|
-
if (!base) return null;
|
|
51
|
-
return git(root, ['merge-base', 'HEAD', base], { allowFail: true })?.trim() || null;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function lines(out) {
|
|
55
|
-
return out ? out.split('\n').filter(Boolean) : [];
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Files the operating system writes on its own (macOS Finder, Windows Explorer). They are
|
|
60
|
-
* never part of a change: neither in scope checks nor in a review's working-tree hash,
|
|
61
|
-
* even when someone committed them.
|
|
62
|
-
*/
|
|
63
|
-
const OS_FILES = new Set(['.DS_Store', 'Thumbs.db', 'desktop.ini']);
|
|
64
|
-
const osFile = (path) => OS_FILES.has(path.slice(path.lastIndexOf('/') + 1));
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Files changed relative to the merge-base: committed, staged, unstaged and untracked.
|
|
68
|
-
* With `staged`, only the files in the index. Paths are POSIX (git always uses '/').
|
|
69
|
-
*/
|
|
70
|
-
export function changedFiles(root, base, { staged = false } = {}) {
|
|
71
|
-
if (staged) return lines(git(root, ['diff', '--cached', '--name-only', '--no-renames'])).filter((f) => !osFile(f));
|
|
72
|
-
const from = mergeBase(root, base);
|
|
73
|
-
const files = new Set();
|
|
74
|
-
if (from) {
|
|
75
|
-
for (const f of lines(git(root, ['diff', '--name-only', '--no-renames', from]))) files.add(f);
|
|
76
|
-
} else if (git(root, ['rev-parse', '--verify', '--quiet', 'HEAD'], { allowFail: true })) {
|
|
77
|
-
for (const f of lines(git(root, ['diff', '--name-only', '--no-renames', 'HEAD']))) files.add(f);
|
|
78
|
-
} else {
|
|
79
|
-
for (const f of lines(git(root, ['diff', '--cached', '--name-only', '--no-renames']))) files.add(f);
|
|
80
|
-
}
|
|
81
|
-
for (const f of lines(git(root, ['ls-files', '--others', '--exclude-standard']))) files.add(f);
|
|
82
|
-
return [...files].filter((f) => !osFile(f)).sort();
|
|
83
|
-
}
|
package/src/hook.mjs
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
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
|
-
}
|
package/src/json-file.mjs
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
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
|
-
'waiting', // the agent stopped and waits for the user (from `agjan log --waiting`)
|
|
11
|
-
'approved',
|
|
12
|
-
'rejected',
|
|
13
|
-
'accepted',
|
|
14
|
-
'amendment-proposed',
|
|
15
|
-
'review-recorded',
|
|
16
|
-
'round-granted', // the user allowed one more review round (from `agjan review extend`)
|
|
17
|
-
];
|
|
18
|
-
|
|
19
|
-
let listener = null;
|
|
20
|
-
|
|
21
|
-
/** Called after every appended event (the CLI regenerates the progress pages with it). */
|
|
22
|
-
export function onEvent(fn) {
|
|
23
|
-
listener = fn;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** Appends one event to the feature's log.jsonl. The time is always set by the script. */
|
|
27
|
-
export function appendLog(featureDir, at, event, fields = {}) {
|
|
28
|
-
const entry = { at_utc: at, event, ...fields };
|
|
29
|
-
appendFileSync(join(featureDir, LOG_FILE), `${JSON.stringify(entry)}\n`);
|
|
30
|
-
try {
|
|
31
|
-
listener?.(featureDir);
|
|
32
|
-
} catch {
|
|
33
|
-
// the progress pages never break the command that recorded the event
|
|
34
|
-
}
|
|
35
|
-
return entry;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function readLog(featureDir) {
|
|
39
|
-
const file = join(featureDir, LOG_FILE);
|
|
40
|
-
if (!existsSync(file)) return [];
|
|
41
|
-
return readFileSync(file, 'utf8')
|
|
42
|
-
.split('\n')
|
|
43
|
-
.filter(Boolean)
|
|
44
|
-
.flatMap((line) => {
|
|
45
|
-
try {
|
|
46
|
-
return [JSON.parse(line)];
|
|
47
|
-
} catch {
|
|
48
|
-
return []; // a damaged line is skipped, not fatal: the log is observability only
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
}
|