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
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util';
|
|
2
|
+
import { FEATURES_DIR, changedFeatures, listFeatures, loadFeature } from '../feature.mjs';
|
|
3
|
+
import { UsageError } from '../errors.mjs';
|
|
4
|
+
import { buildProgress, verifyState } from '../progress.mjs';
|
|
5
|
+
import { gateReadiness, pendingGate } from './gates.mjs';
|
|
6
|
+
import { fixRules, freshReview } from '../fix.mjs';
|
|
7
|
+
import { projectProblems } from '../baseline.mjs';
|
|
8
|
+
import { readFix } from './fix.mjs';
|
|
9
|
+
import { fixAttempts, reviewerFor, roundLimit, roundState } from '../review-policy.mjs';
|
|
10
|
+
import { pad, tone } from '../ui.mjs';
|
|
11
|
+
|
|
12
|
+
/** One indented row: a label column, then the value. `next` and `blocks` stand out. */
|
|
13
|
+
function row(ctx, label, value) {
|
|
14
|
+
const s = ctx.style;
|
|
15
|
+
const styled = { next: s.bold(s.cyan(label)), blocks: s.red(label) }[label] ?? s.gray(label);
|
|
16
|
+
ctx.out(` ${pad(styled, label, 8)} ${value}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The unit's heading: slug, key and stage (or kind). */
|
|
20
|
+
function heading(ctx, unit, detail) {
|
|
21
|
+
const s = ctx.style;
|
|
22
|
+
ctx.out(`${s.bold(unit.slug)} (${s.cyan(unit.key)}) ${detail}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** "review step X with agent Y", or the hand-over when the step used up its rounds. */
|
|
26
|
+
function reviewNext(ctx, feature, step) {
|
|
27
|
+
const state = roundState(feature, ctx.config, step);
|
|
28
|
+
if (state.exhausted) {
|
|
29
|
+
return `the ${step} review is still blocked after ${state.round} of ${state.limit} rounds: stop and hand over to the human (fix with them, accept findings, or change the documents)`;
|
|
30
|
+
}
|
|
31
|
+
const round = state.round ? `, round ${state.round + 1} of ${state.limit}` : '';
|
|
32
|
+
return `review step ${step} with ${reviewerFor(ctx.config, step)}${round}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The review step a feature in Spec or Design needs next. */
|
|
36
|
+
function gateStep(feature) {
|
|
37
|
+
if (feature.stage === 'design') return 'design';
|
|
38
|
+
return feature.design?.parsed.mode === 'light' ? 'design:light' : 'spec';
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The next action in Build: a due review first, then the next task, then Verify, then the PR. */
|
|
42
|
+
function nextInBuild(ctx, feature, progress) {
|
|
43
|
+
if (progress.due.length) return reviewNext(ctx, feature, progress.due[0].step);
|
|
44
|
+
if (progress.next) return `task ${progress.next.id}: ${progress.next.title} (commit as '<KEY>-${progress.next.id}: …')`;
|
|
45
|
+
const { state, record } = verifyState(ctx.root, ctx.config, feature);
|
|
46
|
+
if (state === 'blocked') return `the verify review ${record.id} is blocked: resolve its findings, then /agjan-verify again`;
|
|
47
|
+
if (state === 'stale') return `files changed after the verify review ${record.id}: /agjan-verify again`;
|
|
48
|
+
if (state === 'current') return `${ctx.style.green('verified')} (${record.id}): open the PR with 'npx agjan pr-summary' as its description; a human reviews and merges (Gate 3)`;
|
|
49
|
+
return 'all tasks done and reviewed: /agjan-verify';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** "2 open, 1 blocker(s)", red when a blocker is open. */
|
|
53
|
+
function findingCount(ctx, open) {
|
|
54
|
+
const blockers = open.filter((f) => f.severity === 'blocker').length;
|
|
55
|
+
const text = `${open.length} open, ${blockers} blocker(s)`;
|
|
56
|
+
return blockers ? ctx.style.red(text) : ctx.style.yellow(text);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** A fix or a map run: its checks, its review, and the next step. */
|
|
60
|
+
function describeRecord(ctx, unit) {
|
|
61
|
+
heading(ctx, unit, ctx.style.magenta(unit.kind));
|
|
62
|
+
const problems = unit.kind === 'fix' ? fixRules(ctx.root, ctx.config).problems : projectProblems(ctx.root, ctx.config);
|
|
63
|
+
for (const p of problems) row(ctx, 'blocks', p);
|
|
64
|
+
const review = freshReview(ctx.root, ctx.config, unit, unit.kind);
|
|
65
|
+
row(ctx, 'review', review.problem ?? `${review.record.id} ${tone(ctx.style, review.record.verdict)}, ${tone(ctx.style, 'current')}`);
|
|
66
|
+
const open = unit.findings.filter((f) => f.status === 'open');
|
|
67
|
+
if (open.length) row(ctx, 'findings', findingCount(ctx, open));
|
|
68
|
+
let next;
|
|
69
|
+
if (problems.length) next = unit.kind === 'fix' ? 'the change is not a fix: revert it and use /agjan-spec' : `complete docs/agjan/project.md`;
|
|
70
|
+
else if (review.problem) next = reviewNext(ctx, unit, unit.kind);
|
|
71
|
+
else if (unit.kind === 'fix' && readFix(unit)?.review?.tree_hash !== review.record.tree_hash) next = 'npx agjan fix record --verify "<command and result>"';
|
|
72
|
+
else next = unit.kind === 'map' ? 'npx agjan baseline refresh, commit, open a PR' : 'commit and open a PR';
|
|
73
|
+
row(ctx, 'next', next);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* A feature that did not change on this branch is already on the base branch (merged, or
|
|
78
|
+
* committed there directly): its tasks come from the whole history, and work on it
|
|
79
|
+
* continues as a new feature on a branch.
|
|
80
|
+
*/
|
|
81
|
+
function describeOnBase(ctx, feature) {
|
|
82
|
+
const s = ctx.style;
|
|
83
|
+
const progress = buildProgress(ctx.root, ctx.config, feature, { history: true });
|
|
84
|
+
const verify = feature.reviews.records.filter((r) => r.step === 'verify').at(-1);
|
|
85
|
+
const merged = progress.complete && verify && verify.verdict !== 'blocked';
|
|
86
|
+
heading(ctx, feature, merged ? `stage: ${s.green('merged')}` : `stage: ${s.magenta(feature.stage)} (on the base branch)`);
|
|
87
|
+
row(ctx, 'spec', tone(s, feature.state.spec));
|
|
88
|
+
row(ctx, 'design', `${tone(s, feature.state.design)}${feature.design?.parsed.mode ? ` (${feature.design.parsed.mode})` : ''}`);
|
|
89
|
+
const reviews = feature.reviews.records;
|
|
90
|
+
if (reviews.length) row(ctx, 'reviews', `${reviews.length}, last ${reviews.at(-1).id} ${reviews.at(-1).step}: ${tone(s, reviews.at(-1).verdict)}`);
|
|
91
|
+
for (const t of progress.tasks) {
|
|
92
|
+
row(ctx, 'task', `${tone(s, t.commit ? 'done' : 'open')} ${t.id} [${t.areaName}] ${t.title}${t.commit ? s.gray(` (${t.commit.slice(0, 7)})`) : ''}`);
|
|
93
|
+
}
|
|
94
|
+
if (!merged) row(ctx, 'next', 'continue on a feature branch, or start follow-up work as a new feature');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function describe(ctx, feature, onBase = false) {
|
|
98
|
+
if (feature.kind !== 'feature') return describeRecord(ctx, feature);
|
|
99
|
+
if (onBase) return describeOnBase(ctx, feature);
|
|
100
|
+
heading(ctx, feature, `stage: ${ctx.style.magenta(feature.stage)}`);
|
|
101
|
+
row(ctx, 'spec', tone(ctx.style, feature.state.spec));
|
|
102
|
+
const mode = feature.design?.parsed.mode ? ` (${feature.design.parsed.mode})` : '';
|
|
103
|
+
row(ctx, 'design', `${tone(ctx.style, feature.state.design)}${mode}`);
|
|
104
|
+
const steps = [...new Set(feature.reviews.records.map((r) => r.step))];
|
|
105
|
+
for (const step of steps) {
|
|
106
|
+
const records = feature.reviews.records.filter((r) => r.step === step);
|
|
107
|
+
const last = records[records.length - 1];
|
|
108
|
+
row(ctx, 'review', `${step}: ${tone(ctx.style, last.verdict)} (round ${last.round} of ${roundLimit(ctx.config)}, ${last.id})`);
|
|
109
|
+
}
|
|
110
|
+
const open = feature.findings.filter((f) => f.status === 'open');
|
|
111
|
+
if (open.length) {
|
|
112
|
+
row(ctx, 'findings', findingCount(ctx, open));
|
|
113
|
+
}
|
|
114
|
+
for (const a of feature.amendments.filter((x) => x.status !== 'approved')) {
|
|
115
|
+
row(ctx, 'amend', `${a.id} ${tone(ctx.style, a.status)}: ${a.paths.join(', ')}`);
|
|
116
|
+
}
|
|
117
|
+
if (feature.stage === 'build') {
|
|
118
|
+
const progress = buildProgress(ctx.root, ctx.config, feature);
|
|
119
|
+
for (const t of progress.tasks) {
|
|
120
|
+
row(ctx, 'task', `${tone(ctx.style, t.commit ? 'done' : 'open')} ${t.id} [${t.areaName}] ${t.title}${t.commit ? ctx.style.gray(` (${t.commit.slice(0, 7)})`) : ''}`);
|
|
121
|
+
}
|
|
122
|
+
for (const d of progress.due) row(ctx, 'due', ctx.style.yellow(`review ${d.step} (${d.reason})`));
|
|
123
|
+
row(ctx, 'next', nextInBuild(ctx, feature, progress).replace('<KEY>', feature.key));
|
|
124
|
+
} else {
|
|
125
|
+
// Spec and Design: either the gate is ready for the human, or what blocks it.
|
|
126
|
+
const gate = pendingGate(feature);
|
|
127
|
+
const { problems } = gateReadiness(ctx, feature, gate);
|
|
128
|
+
if (problems.length === 0) {
|
|
129
|
+
row(ctx, 'next', `${ctx.style.green('ready for approval')}: a human runs '${ctx.style.bold(`npx agjan approve ${gate}`)}' in a terminal`);
|
|
130
|
+
} else {
|
|
131
|
+
for (const p of problems) row(ctx, 'blocks', p);
|
|
132
|
+
const step = gateStep(feature);
|
|
133
|
+
const state = roundState(feature, ctx.config, step);
|
|
134
|
+
if (state.exhausted) row(ctx, 'next', ctx.style.red(reviewNext(ctx, feature, step)));
|
|
135
|
+
else row(ctx, 'next', `resolve the points above, then ${reviewNext(ctx, feature, step)}; then request 'approve ${gate}'`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** agjan status [--feature <slug> | --all] */
|
|
141
|
+
export function run(ctx, argv) {
|
|
142
|
+
const { values } = parseArgs({ args: argv, options: { feature: { type: 'string' }, all: { type: 'boolean' } } });
|
|
143
|
+
const root = ctx.root;
|
|
144
|
+
let slugs;
|
|
145
|
+
if (values.feature) {
|
|
146
|
+
if (!listFeatures(root).includes(values.feature)) throw new UsageError(`no feature '${values.feature}'`);
|
|
147
|
+
slugs = [values.feature];
|
|
148
|
+
} else {
|
|
149
|
+
slugs = values.all ? listFeatures(root) : changedFeatures(root, ctx.config);
|
|
150
|
+
}
|
|
151
|
+
const c = ctx.config;
|
|
152
|
+
ctx.out(ctx.style.gray(`settings: documents in '${c.language ?? 'en'}' (headings in English); reviews up to ${roundLimit(c)} rounds; ${fixAttempts(c)} attempts for failing checks`));
|
|
153
|
+
if (slugs.length === 0) {
|
|
154
|
+
ctx.out(values.all ? `no features in ${FEATURES_DIR}` : 'no changed feature on this branch (agjan status --all lists every feature)');
|
|
155
|
+
return 0;
|
|
156
|
+
}
|
|
157
|
+
const changed = new Set(changedFeatures(root, ctx.config));
|
|
158
|
+
slugs.forEach((slug, i) => {
|
|
159
|
+
if (i > 0) ctx.out();
|
|
160
|
+
describe(ctx, loadFeature(root, slug), !changed.has(slug));
|
|
161
|
+
});
|
|
162
|
+
return 0;
|
|
163
|
+
}
|
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
|
+
}
|