agjan 0.0.0-stage → 0.0.2
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 +115 -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 +176 -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 +336 -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/config.mjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { readJson } from './json-file.mjs';
|
|
3
|
+
import { AgjanError } from './errors.mjs';
|
|
4
|
+
|
|
5
|
+
export const CONFIG_FILE = 'agjan.config.json';
|
|
6
|
+
|
|
7
|
+
const KIT_DIR = new URL('../kit/', import.meta.url);
|
|
8
|
+
|
|
9
|
+
/** Package defaults. Domain defaults and project overrides are layered on top. */
|
|
10
|
+
const DEFAULTS = {
|
|
11
|
+
domains: ['btp'],
|
|
12
|
+
hosts: ['claude-code'],
|
|
13
|
+
language: 'en',
|
|
14
|
+
git: { base: null },
|
|
15
|
+
scope: { implicit: {} },
|
|
16
|
+
risk_paths: [],
|
|
17
|
+
tests: {},
|
|
18
|
+
fix: {
|
|
19
|
+
max_total: 80,
|
|
20
|
+
max_per_file: 50,
|
|
21
|
+
protected: [],
|
|
22
|
+
ignored: ['package-lock.json', '**/package-lock.json', 'gen/**', 'dist/**'],
|
|
23
|
+
},
|
|
24
|
+
verify: {},
|
|
25
|
+
build: { fix_attempts: 3 },
|
|
26
|
+
review: { build: 'area', rounds: 3 },
|
|
27
|
+
ci: { policy_changes: 'warn', untracked_changes: 'fail' },
|
|
28
|
+
limits: { spec_kb: 12, design_kb: 20 },
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function isObject(value) {
|
|
32
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Deep merge: objects merge key by key, everything else (arrays included) replaces. */
|
|
36
|
+
export function merge(base, override) {
|
|
37
|
+
if (!isObject(base) || !isObject(override)) return override === undefined ? base : override;
|
|
38
|
+
const out = { ...base };
|
|
39
|
+
for (const [key, value] of Object.entries(override)) out[key] = merge(base[key], value);
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function loadDomain(id) {
|
|
44
|
+
const domain = readJson(new URL(`domains/${id}/domain.json`, KIT_DIR));
|
|
45
|
+
if (!domain) throw new AgjanError(`unknown domain '${id}'`);
|
|
46
|
+
return domain;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Loads the effective configuration of the project at `root`: package defaults, then
|
|
51
|
+
* the enabled domains' defaults, then agjan.config.json. Also resolves the areas of all
|
|
52
|
+
* enabled domains, keyed by both the short id and the canonical `domain:area` id.
|
|
53
|
+
*/
|
|
54
|
+
export function loadConfig(root) {
|
|
55
|
+
const project = readJson(join(root, CONFIG_FILE)) ?? {};
|
|
56
|
+
if (!isObject(project)) throw new AgjanError(`${CONFIG_FILE} must contain a JSON object`);
|
|
57
|
+
const domainIds = project.domains ?? DEFAULTS.domains;
|
|
58
|
+
|
|
59
|
+
let config = merge(DEFAULTS, {});
|
|
60
|
+
const areas = {};
|
|
61
|
+
const shortCount = {};
|
|
62
|
+
for (const id of domainIds) {
|
|
63
|
+
const domain = loadDomain(id);
|
|
64
|
+
const implicit = {};
|
|
65
|
+
const tests = {};
|
|
66
|
+
const verify = {};
|
|
67
|
+
for (const [name, area] of Object.entries(domain.areas)) {
|
|
68
|
+
const canonical = `${id}:${name}`;
|
|
69
|
+
areas[canonical] = { id: canonical, domain: id, name, role: area.role };
|
|
70
|
+
shortCount[name] = (shortCount[name] ?? 0) + 1;
|
|
71
|
+
implicit[name] = area.implicit ?? [];
|
|
72
|
+
tests[name] = area.tests ?? [];
|
|
73
|
+
verify[name] = area.verify ?? {};
|
|
74
|
+
}
|
|
75
|
+
config = merge(config, {
|
|
76
|
+
scope: { implicit },
|
|
77
|
+
tests,
|
|
78
|
+
verify,
|
|
79
|
+
risk_paths: [...config.risk_paths, ...(domain.risk_paths ?? [])],
|
|
80
|
+
fix: { protected: [...config.fix.protected, ...(domain.protected ?? [])] },
|
|
81
|
+
watched: [...(config.watched ?? []), ...(domain.watched ?? [])],
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
// Short ids resolve only when they are unique among the enabled domains (design 3).
|
|
85
|
+
for (const area of Object.values(areas)) {
|
|
86
|
+
if (shortCount[area.name] === 1) areas[area.name] = area;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
config = merge(config, project);
|
|
90
|
+
config.domains = domainIds;
|
|
91
|
+
config.areas = areas;
|
|
92
|
+
return config;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Resolves a short or canonical area id; returns null when unknown or ambiguous. */
|
|
96
|
+
export function resolveArea(config, id) {
|
|
97
|
+
return config.areas[id] ?? null;
|
|
98
|
+
}
|
package/src/context.mjs
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { repoRoot, userEmail } from './git.mjs';
|
|
3
|
+
import { loadConfig } from './config.mjs';
|
|
4
|
+
import { confirmOnTerminal, editOnTerminal } from './terminal.mjs';
|
|
5
|
+
import { createStyle } from './ui.mjs';
|
|
6
|
+
|
|
7
|
+
export const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
|
|
8
|
+
|
|
9
|
+
/** UTC timestamp without milliseconds, for example 2026-10-17T09:12:03Z. */
|
|
10
|
+
export function utcNow() {
|
|
11
|
+
return new Date().toISOString().replace(/\.\d{3}Z$/, 'Z');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Everything a command needs from the outside world. Tests pass their own streams,
|
|
16
|
+
* clock, confirmation and line editor; the CLI uses the real ones. There is deliberately
|
|
17
|
+
* no option
|
|
18
|
+
* to skip the confirmation from the command line or the environment.
|
|
19
|
+
*/
|
|
20
|
+
export function createContext({
|
|
21
|
+
cwd = process.cwd(),
|
|
22
|
+
stdout = process.stdout,
|
|
23
|
+
stderr = process.stderr,
|
|
24
|
+
now = utcNow,
|
|
25
|
+
confirm = confirmOnTerminal,
|
|
26
|
+
edit = editOnTerminal,
|
|
27
|
+
} = {}) {
|
|
28
|
+
let root;
|
|
29
|
+
let config;
|
|
30
|
+
return {
|
|
31
|
+
cwd,
|
|
32
|
+
/** Colors for stdout; plain text unless stdout is a terminal (see ui.mjs). */
|
|
33
|
+
style: createStyle(stdout),
|
|
34
|
+
now,
|
|
35
|
+
confirm,
|
|
36
|
+
/** Asks the human for one line, prefilled with `initial` (async). */
|
|
37
|
+
edit,
|
|
38
|
+
kit: pkg.version,
|
|
39
|
+
get root() {
|
|
40
|
+
root ??= repoRoot(cwd);
|
|
41
|
+
return root;
|
|
42
|
+
},
|
|
43
|
+
get config() {
|
|
44
|
+
config ??= loadConfig(this.root);
|
|
45
|
+
return config;
|
|
46
|
+
},
|
|
47
|
+
email() {
|
|
48
|
+
return userEmail(this.root);
|
|
49
|
+
},
|
|
50
|
+
/** Writes each argument as one line; no argument writes an empty line. */
|
|
51
|
+
out(...lines) {
|
|
52
|
+
stdout.write(`${(lines.length ? lines : ['']).join('\n')}\n`);
|
|
53
|
+
},
|
|
54
|
+
err(...lines) {
|
|
55
|
+
stderr.write(`${(lines.length ? lines : ['']).join('\n')}\n`);
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { parse as parseYaml } from 'yaml';
|
|
2
|
+
|
|
3
|
+
/** Required `##` headings per document (design 8). Keys stay English in every language. */
|
|
4
|
+
export const SPEC_HEADINGS = [
|
|
5
|
+
'Summary',
|
|
6
|
+
'Why',
|
|
7
|
+
'Users and roles',
|
|
8
|
+
'Scope',
|
|
9
|
+
'Acceptance criteria',
|
|
10
|
+
'Non-functional requirements',
|
|
11
|
+
'Authorization',
|
|
12
|
+
'Business constraints',
|
|
13
|
+
'Open technical questions',
|
|
14
|
+
];
|
|
15
|
+
export const DESIGN_HEADINGS_LIGHT = ['Impact summary', 'Mode rationale', 'Areas and rationale'];
|
|
16
|
+
export const DESIGN_HEADINGS_FULL = [
|
|
17
|
+
...DESIGN_HEADINGS_LIGHT,
|
|
18
|
+
'Decisions',
|
|
19
|
+
'Answers to open technical questions',
|
|
20
|
+
'Risks and trade-offs',
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
const TASK_ID = /^T[1-9][0-9]*$/;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Splits a leading YAML frontmatter block from the body. Returns `data: null` when there
|
|
27
|
+
* is none, and an `error` string when the YAML is invalid or not a mapping.
|
|
28
|
+
*/
|
|
29
|
+
export function splitFrontmatter(text) {
|
|
30
|
+
const match = /^---\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/.exec(text);
|
|
31
|
+
if (!match) return { data: null, body: text };
|
|
32
|
+
const body = text.slice(match[0].length);
|
|
33
|
+
try {
|
|
34
|
+
const data = parseYaml(match[1]) ?? {};
|
|
35
|
+
if (typeof data !== 'object' || Array.isArray(data)) return { data: null, body, error: 'frontmatter must be a mapping' };
|
|
36
|
+
return { data, body };
|
|
37
|
+
} catch (error) {
|
|
38
|
+
return { data: null, body, error: `invalid frontmatter: ${error.message.split('\n')[0]}` };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Splits the body into lines, skipping fenced code blocks so their content is not parsed. */
|
|
43
|
+
function outsideFences(body) {
|
|
44
|
+
const out = [];
|
|
45
|
+
let fence = null;
|
|
46
|
+
body.split(/\r?\n/).forEach((line, index) => {
|
|
47
|
+
const marker = /^(```|~~~)/.exec(line);
|
|
48
|
+
if (marker) {
|
|
49
|
+
if (fence === null) fence = marker[1];
|
|
50
|
+
else if (line.startsWith(fence)) fence = null;
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (fence === null) out.push({ line, number: index + 1 });
|
|
54
|
+
});
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function headings(body, level = 2) {
|
|
59
|
+
const prefix = `${'#'.repeat(level)} `;
|
|
60
|
+
return outsideFences(body)
|
|
61
|
+
.filter(({ line }) => line.startsWith(prefix) && !line.startsWith(`${prefix}#`))
|
|
62
|
+
.map(({ line }) => line.slice(prefix.length).trim());
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function missingHeadings(body, required) {
|
|
66
|
+
const present = new Set(headings(body));
|
|
67
|
+
return required.filter((h) => !present.has(h)).map((h) => `missing heading '## ${h}'`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The lines of one `##` section (without its heading), or null when absent. */
|
|
71
|
+
function section(body, title) {
|
|
72
|
+
const lines = outsideFences(body);
|
|
73
|
+
const start = lines.findIndex(({ line }) => line.trim() === `## ${title}`);
|
|
74
|
+
if (start === -1) return null;
|
|
75
|
+
const end = lines.findIndex(({ line }, i) => i > start && /^## /.test(line));
|
|
76
|
+
return lines.slice(start + 1, end === -1 ? undefined : end);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Parses `spec.md`. Returns the authored frontmatter, the acceptance criteria and the
|
|
81
|
+
* list of problems. `key` is the feature key; AC ids must be `<key>-AC<n>`.
|
|
82
|
+
*/
|
|
83
|
+
export function parseSpec(text, key) {
|
|
84
|
+
const problems = [];
|
|
85
|
+
const { data, body, error } = splitFrontmatter(text);
|
|
86
|
+
if (error) problems.push(error);
|
|
87
|
+
const kind = data?.kind;
|
|
88
|
+
if (kind !== 'business' && kind !== 'technical') problems.push("frontmatter 'kind' must be 'business' or 'technical'");
|
|
89
|
+
|
|
90
|
+
problems.push(...missingHeadings(body, kind === 'technical' ? [...SPEC_HEADINGS, 'Technical constraints'] : SPEC_HEADINGS));
|
|
91
|
+
|
|
92
|
+
const acs = [];
|
|
93
|
+
const lines = section(body, 'Acceptance criteria') ?? [];
|
|
94
|
+
const idPattern = new RegExp(`^${key}-AC[1-9][0-9]*$`);
|
|
95
|
+
let current = null;
|
|
96
|
+
for (const { line, number } of lines) {
|
|
97
|
+
const heading = /^### (.+?)\s*$/.exec(line);
|
|
98
|
+
if (heading) {
|
|
99
|
+
current = { id: heading[1], line: number, given: false, when: false, then: false, verification: 'automated' };
|
|
100
|
+
acs.push(current);
|
|
101
|
+
if (!idPattern.test(current.id)) problems.push(`line ${number}: AC id '${current.id}' must look like ${key}-AC1`);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (!current) continue;
|
|
105
|
+
const field = /^\s*(Given|When|Then|And|Verification:)\s*(.*)$/.exec(line);
|
|
106
|
+
if (!field) continue;
|
|
107
|
+
if (field[1] === 'Given') current.given = true;
|
|
108
|
+
if (field[1] === 'When') current.when = true;
|
|
109
|
+
if (field[1] === 'Then') current.then = true;
|
|
110
|
+
if (field[1] === 'Verification:') {
|
|
111
|
+
const value = field[2].trim();
|
|
112
|
+
if (value === 'automated' || value === 'manual') current.verification = value;
|
|
113
|
+
else problems.push(`line ${number}: ${current.id} verification must be 'automated' or 'manual'`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (acs.length === 0) problems.push("no acceptance criteria ('### <KEY>-AC<n>' under '## Acceptance criteria')");
|
|
117
|
+
const seen = new Set();
|
|
118
|
+
for (const ac of acs) {
|
|
119
|
+
if (seen.has(ac.id)) problems.push(`duplicate AC id ${ac.id}`);
|
|
120
|
+
seen.add(ac.id);
|
|
121
|
+
for (const part of ['given', 'when', 'then']) {
|
|
122
|
+
if (!ac[part]) problems.push(`${ac.id} has no '${part[0].toUpperCase()}${part.slice(1)}' line`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return { kind, acs, problems };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Parses `design.md`: mode, areas and the required headings for the mode. */
|
|
129
|
+
export function parseDesign(text) {
|
|
130
|
+
const problems = [];
|
|
131
|
+
const { data, body, error } = splitFrontmatter(text);
|
|
132
|
+
if (error) problems.push(error);
|
|
133
|
+
const mode = data?.mode;
|
|
134
|
+
if (mode !== 'light' && mode !== 'full') problems.push("frontmatter 'mode' must be 'light' or 'full'");
|
|
135
|
+
const areas = data?.areas;
|
|
136
|
+
if (!Array.isArray(areas) || areas.length === 0 || !areas.every((a) => typeof a === 'string')) {
|
|
137
|
+
problems.push("frontmatter 'areas' must be a non-empty list of area ids");
|
|
138
|
+
}
|
|
139
|
+
problems.push(...missingHeadings(body, mode === 'full' ? DESIGN_HEADINGS_FULL : DESIGN_HEADINGS_LIGHT));
|
|
140
|
+
return { mode, areas: Array.isArray(areas) ? areas : [], problems };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function isRepoPath(value) {
|
|
144
|
+
return typeof value === 'string' && value.length > 0 && !value.includes('\\') &&
|
|
145
|
+
!value.startsWith('/') && !/^[A-Za-z]:/.test(value) && !value.split('/').includes('..');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Parses `tasks.md`: exactly one fenced YAML block with a `tasks` list (design 8.3). */
|
|
149
|
+
export function parseTasks(text) {
|
|
150
|
+
const problems = [];
|
|
151
|
+
const blocks = [...text.matchAll(/^```ya?ml[ \t]*\r?\n([\s\S]*?)^```[ \t]*$/gm)];
|
|
152
|
+
if (blocks.length !== 1) {
|
|
153
|
+
return { tasks: [], problems: [`tasks.md must contain exactly one fenced yaml block (found ${blocks.length})`] };
|
|
154
|
+
}
|
|
155
|
+
let data;
|
|
156
|
+
try {
|
|
157
|
+
data = parseYaml(blocks[0][1]);
|
|
158
|
+
} catch (error) {
|
|
159
|
+
return { tasks: [], problems: [`invalid tasks yaml: ${error.message.split('\n')[0]}`] };
|
|
160
|
+
}
|
|
161
|
+
if (!Array.isArray(data?.tasks) || data.tasks.length === 0) {
|
|
162
|
+
return { tasks: [], problems: ["the yaml block must have a non-empty 'tasks' list"] };
|
|
163
|
+
}
|
|
164
|
+
const tasks = [];
|
|
165
|
+
const ids = new Set();
|
|
166
|
+
data.tasks.forEach((task, index) => {
|
|
167
|
+
const where = typeof task?.id === 'string' ? task.id : `task #${index + 1}`;
|
|
168
|
+
if (typeof task !== 'object' || task === null) {
|
|
169
|
+
problems.push(`${where}: must be a mapping`);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (!TASK_ID.test(task.id ?? '')) problems.push(`${where}: id must look like T1`);
|
|
173
|
+
else if (ids.has(task.id)) problems.push(`${where}: duplicate task id`);
|
|
174
|
+
ids.add(task.id);
|
|
175
|
+
if (typeof task.area !== 'string') problems.push(`${where}: 'area' is required`);
|
|
176
|
+
if (typeof task.title !== 'string' || task.title.trim() === '') problems.push(`${where}: 'title' is required`);
|
|
177
|
+
const covers = Array.isArray(task.covers) ? task.covers : [];
|
|
178
|
+
if (covers.length === 0) problems.push(`${where}: 'covers' must list at least one AC id`);
|
|
179
|
+
const paths = Array.isArray(task.paths) ? task.paths : [];
|
|
180
|
+
if (paths.length === 0) problems.push(`${where}: 'paths' must list at least one path or glob`);
|
|
181
|
+
for (const p of paths) {
|
|
182
|
+
if (!isRepoPath(p)) problems.push(`${where}: path '${p}' must be relative, POSIX, without '..'`);
|
|
183
|
+
}
|
|
184
|
+
if (task.verify !== undefined && typeof task.verify !== 'string') problems.push(`${where}: 'verify' must be a string`);
|
|
185
|
+
tasks.push({ id: task.id, area: task.area, title: task.title, covers, paths, verify: task.verify });
|
|
186
|
+
});
|
|
187
|
+
return { tasks, problems };
|
|
188
|
+
}
|
package/src/errors.mjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** An expected failure: printed without a stack trace, with its own exit code. */
|
|
2
|
+
export class AgjanError extends Error {
|
|
3
|
+
constructor(message, { exitCode = 1 } = {}) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'AgjanError';
|
|
6
|
+
this.exitCode = exitCode;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Wrong command line: exit code 2. */
|
|
11
|
+
export class UsageError extends AgjanError {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message, { exitCode: 2 });
|
|
14
|
+
this.name = 'UsageError';
|
|
15
|
+
}
|
|
16
|
+
}
|
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
|
+
}
|