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,88 @@
|
|
|
1
|
+
import { stringify as toYaml } from 'yaml';
|
|
2
|
+
import { skillBody } from '../managed.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* GitHub Copilot adapter (design 13), verified with the Copilot CLI
|
|
6
|
+
* (docs/host-experiments.md): reviewer agents in `.github/agents/` with the agjan MCP
|
|
7
|
+
* server inline, and a `preToolUse` guard hook in `.github/hooks/`, because the
|
|
8
|
+
* Copilot CLI has no persistent deny setting. Skills need nothing: Copilot reads
|
|
9
|
+
* `.claude/skills/`.
|
|
10
|
+
*
|
|
11
|
+
* VS Code ignores `mcp-servers` in agent files and does not run hooks; see the open
|
|
12
|
+
* item in design section 19.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export const HOST = 'copilot';
|
|
16
|
+
export const HOOK_FILE = '.github/hooks/agjan-guard.json';
|
|
17
|
+
export const GUARD = 'node_modules/agjan/kit/hooks/guard.mjs';
|
|
18
|
+
|
|
19
|
+
/** Copilot model names differ from Claude Code's; unset means the session's model. */
|
|
20
|
+
export const DEFAULT_MODELS = {};
|
|
21
|
+
|
|
22
|
+
const REVIEWERS = [
|
|
23
|
+
{ name: 'agjan-reviewer', tier: 'balanced' },
|
|
24
|
+
{ name: 'agjan-reviewer-deep', tier: 'deep' },
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
function reviewerAgent(name, tier, model, skill) {
|
|
28
|
+
const frontmatter = {
|
|
29
|
+
name,
|
|
30
|
+
description:
|
|
31
|
+
`agjan's read-only reviewer (${tier} tier). Use only when an agjan workflow skill asks for the ` +
|
|
32
|
+
'review of a step; it records its result itself.',
|
|
33
|
+
tools: ['read', 'search', 'agjan/review_context', 'agjan/review_record'],
|
|
34
|
+
...(model ? { model } : {}),
|
|
35
|
+
// Scoped to this agent only: the writer session must not have review_record.
|
|
36
|
+
'mcp-servers': {
|
|
37
|
+
agjan: {
|
|
38
|
+
type: 'local',
|
|
39
|
+
command: 'npx',
|
|
40
|
+
args: ['--no-install', 'agjan', 'mcp', '--host', HOST, '--isolation', 'agent'],
|
|
41
|
+
tools: ['*'],
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
return [
|
|
46
|
+
'---',
|
|
47
|
+
toYaml(frontmatter, { lineWidth: 0 }).trimEnd(),
|
|
48
|
+
'---',
|
|
49
|
+
'<!-- Generated by agjan sync from the solution-reviewer skill. Do not edit; run agjan sync. -->',
|
|
50
|
+
'',
|
|
51
|
+
skillBody(skill),
|
|
52
|
+
].join('\n');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The guard hook. If agjan is not installed the guard cannot start; the hook then
|
|
57
|
+
* allows (`|| true`, `exit 0`) instead of failing closed and blocking every tool call.
|
|
58
|
+
*/
|
|
59
|
+
export function hookConfig() {
|
|
60
|
+
const hook = {
|
|
61
|
+
version: 1,
|
|
62
|
+
hooks: {
|
|
63
|
+
preToolUse: [
|
|
64
|
+
{
|
|
65
|
+
type: 'command',
|
|
66
|
+
bash: `node ${GUARD} || true`,
|
|
67
|
+
powershell: `node ${GUARD}; exit 0`,
|
|
68
|
+
cwd: '.',
|
|
69
|
+
timeoutSec: 10,
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
return `${JSON.stringify(hook, null, 2)}\n`;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The plan items this adapter contributes. */
|
|
78
|
+
export function plan(config, skills) {
|
|
79
|
+
const models = { ...DEFAULT_MODELS, ...(config.models?.[HOST] ?? {}) };
|
|
80
|
+
const skill = skills['solution-reviewer/SKILL.md'];
|
|
81
|
+
const items = REVIEWERS.map(({ name, tier }) => ({
|
|
82
|
+
kind: 'file',
|
|
83
|
+
path: `.github/agents/${name}.agent.md`,
|
|
84
|
+
content: reviewerAgent(name, tier, models[tier], skill),
|
|
85
|
+
}));
|
|
86
|
+
items.push({ kind: 'file', path: HOOK_FILE, content: hookConfig() });
|
|
87
|
+
return items;
|
|
88
|
+
}
|
package/src/baseline.mjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { git } from './git.mjs';
|
|
5
|
+
import { readJson, writeJson } from './json-file.mjs';
|
|
6
|
+
import { headings } from './documents.mjs';
|
|
7
|
+
import { matcher } from './scope.mjs';
|
|
8
|
+
import { repoFiles } from './tests-map.mjs';
|
|
9
|
+
import { FEATURES_DIR } from './feature.mjs';
|
|
10
|
+
import { PROJECT_MD } from './fix.mjs';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The baseline (design 8.6): docs/agjan/project.md, written by people and agents, and
|
|
14
|
+
* docs/agjan/baseline.json, written by `agjan baseline refresh`, with the hashes of the
|
|
15
|
+
* domains' watched files (models, services, app manifests, mta.yaml).
|
|
16
|
+
*/
|
|
17
|
+
export const BASELINE_FILE = `${FEATURES_DIR}/baseline.json`;
|
|
18
|
+
|
|
19
|
+
/** Required `##` headings of project.md: general ones plus the BTP domain's areas. */
|
|
20
|
+
export function projectHeadings(config) {
|
|
21
|
+
const general = ['Overview', 'Conventions', 'Do-not-touch list'];
|
|
22
|
+
return config.domains.includes('btp') ? ['Overview', 'CAP', 'Fiori', 'BTP', 'Conventions', 'Do-not-touch list'] : general;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function projectProblems(root, config) {
|
|
26
|
+
const file = join(root, PROJECT_MD);
|
|
27
|
+
if (!existsSync(file)) return [`${PROJECT_MD} does not exist`];
|
|
28
|
+
const present = new Set(headings(readFileSync(file, 'utf8')));
|
|
29
|
+
return projectHeadings(config).filter((h) => !present.has(h)).map((h) => `${PROJECT_MD}: missing heading '## ${h}'`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function fileHash(root, file) {
|
|
33
|
+
return `sha256:${createHash('sha256').update(readFileSync(join(root, file))).digest('hex')}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function watchedHashes(root, config) {
|
|
37
|
+
const isWatched = matcher(config.watched ?? []);
|
|
38
|
+
const out = {};
|
|
39
|
+
for (const file of repoFiles(root).filter(isWatched).sort()) {
|
|
40
|
+
if (existsSync(join(root, file))) out[file] = fileHash(root, file);
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** agjan baseline refresh: records the current state of the watched files. */
|
|
46
|
+
export function refreshBaseline(root, config, now) {
|
|
47
|
+
const head = git(root, ['rev-parse', '--verify', '--quiet', 'HEAD'], { allowFail: true })?.trim() ?? null;
|
|
48
|
+
const data = { schema: 1, refreshed_at_utc: now, based_on_commit: head, watched: watchedHashes(root, config) };
|
|
49
|
+
mkdirSync(dirname(join(root, BASELINE_FILE)), { recursive: true });
|
|
50
|
+
writeJson(join(root, BASELINE_FILE), data);
|
|
51
|
+
return data;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Watched files that differ from the baseline: changed, added or removed since the refresh. */
|
|
55
|
+
export function baselineDrift(root, config) {
|
|
56
|
+
const baseline = readJson(join(root, BASELINE_FILE));
|
|
57
|
+
if (!baseline?.watched) return null;
|
|
58
|
+
const current = watchedHashes(root, config);
|
|
59
|
+
const drift = [];
|
|
60
|
+
for (const [file, hash] of Object.entries(current)) {
|
|
61
|
+
if (baseline.watched[file] === undefined) drift.push(`${file} (new)`);
|
|
62
|
+
else if (baseline.watched[file] !== hash) drift.push(file);
|
|
63
|
+
}
|
|
64
|
+
for (const file of Object.keys(baseline.watched)) if (current[file] === undefined) drift.push(`${file} (removed)`);
|
|
65
|
+
return drift;
|
|
66
|
+
}
|
package/src/cli.mjs
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { createContext, pkg } from './context.mjs';
|
|
2
|
+
import { AgjanError } from './errors.mjs';
|
|
3
|
+
import { banner, createStyle } from './ui.mjs';
|
|
4
|
+
import * as newCommand from './commands/new.mjs';
|
|
5
|
+
import * as gates from './commands/gates.mjs';
|
|
6
|
+
import * as check from './commands/check.mjs';
|
|
7
|
+
import * as status from './commands/status.mjs';
|
|
8
|
+
import * as review from './commands/review.mjs';
|
|
9
|
+
import * as log from './commands/log.mjs';
|
|
10
|
+
import * as prSummary from './commands/pr-summary.mjs';
|
|
11
|
+
import * as setup from './commands/setup.mjs';
|
|
12
|
+
import * as report from './commands/report.mjs';
|
|
13
|
+
import * as fix from './commands/fix.mjs';
|
|
14
|
+
import * as mapCommand from './commands/map.mjs';
|
|
15
|
+
|
|
16
|
+
const COMMANDS = {
|
|
17
|
+
init: setup.init,
|
|
18
|
+
sync: setup.sync,
|
|
19
|
+
doctor: setup.doctor,
|
|
20
|
+
hook: setup.hook,
|
|
21
|
+
new: newCommand.run,
|
|
22
|
+
scaffold: newCommand.scaffold,
|
|
23
|
+
status: status.run,
|
|
24
|
+
check: check.run,
|
|
25
|
+
amend: gates.amend,
|
|
26
|
+
approve: gates.approve,
|
|
27
|
+
accept: gates.accept,
|
|
28
|
+
reject: gates.reject,
|
|
29
|
+
review: review.show,
|
|
30
|
+
log: log.run,
|
|
31
|
+
'pr-summary': prSummary.run,
|
|
32
|
+
report: report.run,
|
|
33
|
+
fix: fix.run,
|
|
34
|
+
map: mapCommand.map,
|
|
35
|
+
baseline: mapCommand.baseline,
|
|
36
|
+
mcp: review.mcp,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const HELP = `agjan ${pkg.version}
|
|
40
|
+
|
|
41
|
+
Spec-driven workflow kit for AI coding agents, with human approval gates
|
|
42
|
+
and a read-only reviewer at every step. Closed beta (0.x); the user guide is\nin this package: docs/guide.md
|
|
43
|
+
|
|
44
|
+
Usage: agjan <command> [options]
|
|
45
|
+
|
|
46
|
+
Setup (human):
|
|
47
|
+
init [--domains btp] [--hosts claude-code,copilot] [--base <ref>]
|
|
48
|
+
[--no-hook] [--no-ci] [--prepare]
|
|
49
|
+
set up agjan in this repository
|
|
50
|
+
sync [--force] regenerate managed files (skills, agents, blocks, settings)
|
|
51
|
+
hook install install the pre-commit hook
|
|
52
|
+
doctor check the environment and the project setup
|
|
53
|
+
|
|
54
|
+
Agent and human:
|
|
55
|
+
new <slug> --key <KEY> create a feature folder with approvals.json and spec.md
|
|
56
|
+
scaffold design create design.md and tasks.md from the templates
|
|
57
|
+
status [--feature <s> | --all]
|
|
58
|
+
show the state of the changed (or every) feature
|
|
59
|
+
check [--staged | --ci] [--base <ref>]
|
|
60
|
+
enforce the workflow rules
|
|
61
|
+
amend --task <id> --paths <p,...> --reason "<why>"
|
|
62
|
+
propose a scope amendment during Build
|
|
63
|
+
fix start <slug> --key <KEY> --what "<text>" | fix check | fix record [--verify "…"]
|
|
64
|
+
fast path for small changes (record folder under fixes/)
|
|
65
|
+
map start start mapping an existing application (project.md)
|
|
66
|
+
baseline refresh record the watched files in baseline.json
|
|
67
|
+
review show [--step <step>] list review records, or print a step's review context
|
|
68
|
+
log --stage <stage> [--role r] [--task T1] [--skill s]
|
|
69
|
+
log --gate-requested <gate> [--message "<commit message>"]
|
|
70
|
+
record a step start or a gate handed to the human
|
|
71
|
+
pr-summary print the PR description (Markdown), with check --ci results
|
|
72
|
+
report write report.md: durations, gate waits, models and tokens
|
|
73
|
+
|
|
74
|
+
Reviewer agent (through the host's MCP configuration):
|
|
75
|
+
mcp [--host <h>] [--isolation agent|shared|fork|none]
|
|
76
|
+
agjan MCP server on stdio: review_context, review_record
|
|
77
|
+
|
|
78
|
+
Human only, in a terminal:
|
|
79
|
+
approve spec | design | spec design | amendment <id>
|
|
80
|
+
accept <finding-id> --reason "<why>"
|
|
81
|
+
reject spec|design --reason "<why>"
|
|
82
|
+
each records the decision, then commits the feature
|
|
83
|
+
folder with a message you can edit (--no-commit skips)
|
|
84
|
+
|
|
85
|
+
Other:
|
|
86
|
+
--version, --help
|
|
87
|
+
`;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Runs the CLI and returns the process exit code.
|
|
91
|
+
* `options` (streams, clock, confirmation) are passed to the command context for tests.
|
|
92
|
+
*/
|
|
93
|
+
export async function main(args, options = {}) {
|
|
94
|
+
const stdout = options.stdout ?? process.stdout;
|
|
95
|
+
const stderr = options.stderr ?? process.stderr;
|
|
96
|
+
const [command, ...rest] = args;
|
|
97
|
+
|
|
98
|
+
if (command === undefined || command === '--help' || command === '-h' || command === 'help') {
|
|
99
|
+
const s = createStyle(stdout);
|
|
100
|
+
if (!s.enabled) {
|
|
101
|
+
stdout.write(HELP);
|
|
102
|
+
return 0;
|
|
103
|
+
}
|
|
104
|
+
// On a terminal: the logo instead of the first line, and bold section headings.
|
|
105
|
+
const body = HELP.split('\n').slice(1).map((l) => (/^\S.*:$/.test(l) ? s.bold(l) : l));
|
|
106
|
+
stdout.write(`${[...banner(s, pkg.version), ...body.slice(1)].join('\n')}`);
|
|
107
|
+
return 0;
|
|
108
|
+
}
|
|
109
|
+
if (command === '--version' || command === '-v' || command === 'version') {
|
|
110
|
+
stdout.write(`${pkg.version}\n`);
|
|
111
|
+
return 0;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const handler = COMMANDS[command];
|
|
115
|
+
if (!handler) {
|
|
116
|
+
stderr.write(`agjan: unknown command '${command}'. Run 'agjan --help'.\n`);
|
|
117
|
+
return 2;
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
return await handler(createContext({ ...options, stdout, stderr }), rest);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if (error instanceof AgjanError) {
|
|
123
|
+
const s = createStyle(stderr);
|
|
124
|
+
stderr.write(`${s.bold(s.red(`agjan ${command}:`))} ${error.message}\n`);
|
|
125
|
+
return error.exitCode;
|
|
126
|
+
}
|
|
127
|
+
if (error?.code?.startsWith?.('ERR_PARSE_ARGS')) {
|
|
128
|
+
stderr.write(`agjan ${command}: ${error.message}\n`);
|
|
129
|
+
return 2;
|
|
130
|
+
}
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import { readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { parseArgs } from 'node:util';
|
|
4
|
+
import { git, changedFiles, mergeBase, resolveBase } from '../git.mjs';
|
|
5
|
+
import { CONFIG_FILE, resolveArea } from '../config.mjs';
|
|
6
|
+
import { FEATURES_DIR, changedFeatures, listFeatures, loadFeature } from '../feature.mjs';
|
|
7
|
+
import { documentProblems, designProblems } from '../validate.mjs';
|
|
8
|
+
import { allowedPatterns, matcher } from '../scope.mjs';
|
|
9
|
+
import { acTestFiles } from '../tests-map.mjs';
|
|
10
|
+
import { acResults, loadResults } from '../test-results.mjs';
|
|
11
|
+
import { buildProgress, verifyState } from '../progress.mjs';
|
|
12
|
+
import { FIX_FILE, fixRules, freshReview } from '../fix.mjs';
|
|
13
|
+
import { BASELINE_FILE, baselineDrift, projectProblems } from '../baseline.mjs';
|
|
14
|
+
import { UsageError } from '../errors.mjs';
|
|
15
|
+
import { readJson } from '../json-file.mjs';
|
|
16
|
+
import { verdictColor } from '../ui.mjs';
|
|
17
|
+
import { planStatus } from '../managed.mjs';
|
|
18
|
+
import { buildPlan } from '../plan.mjs';
|
|
19
|
+
|
|
20
|
+
/** Files whose change in a PR alters the rules that check it (design 9.3). */
|
|
21
|
+
const POLICY_PATTERNS = [
|
|
22
|
+
CONFIG_FILE,
|
|
23
|
+
'agjan.lock.json',
|
|
24
|
+
'AGENTS.md',
|
|
25
|
+
'CLAUDE.md',
|
|
26
|
+
'package.json',
|
|
27
|
+
'package-lock.json',
|
|
28
|
+
'.claude/**',
|
|
29
|
+
'.github/agents/**',
|
|
30
|
+
'.github/hooks/**',
|
|
31
|
+
'.github/workflows/**',
|
|
32
|
+
'.github/CODEOWNERS',
|
|
33
|
+
'.mcp.json',
|
|
34
|
+
'.vscode/mcp.json',
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
class Report {
|
|
38
|
+
constructor() {
|
|
39
|
+
this.items = [];
|
|
40
|
+
}
|
|
41
|
+
error(scope, message) {
|
|
42
|
+
this.items.push({ level: 'error', scope, message });
|
|
43
|
+
}
|
|
44
|
+
warn(scope, message) {
|
|
45
|
+
this.items.push({ level: 'warn', scope, message });
|
|
46
|
+
}
|
|
47
|
+
get errors() {
|
|
48
|
+
return this.items.filter((i) => i.level === 'error').length;
|
|
49
|
+
}
|
|
50
|
+
get warnings() {
|
|
51
|
+
return this.items.filter((i) => i.level === 'warn').length;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Soft size limits (design 9.2, `limits`): large documents cost tokens in every stage that
|
|
57
|
+
* reads them. Warnings only; `design_kb` covers design.md and tasks.md together.
|
|
58
|
+
*/
|
|
59
|
+
function checkSizes(report, feature, config) {
|
|
60
|
+
const kb = (text) => Buffer.byteLength(text ?? '') / 1024;
|
|
61
|
+
const { spec_kb: specKb, design_kb: designKb } = config.limits ?? {};
|
|
62
|
+
if (specKb && feature.spec && kb(feature.spec.text) > specKb) {
|
|
63
|
+
report.warn(feature.slug, `spec.md is ${kb(feature.spec.text).toFixed(1)} KB (soft limit ${specKb} KB); keep it to what the feature needs`);
|
|
64
|
+
}
|
|
65
|
+
const design = kb(feature.design?.text) + kb(feature.tasks?.text);
|
|
66
|
+
if (designKb && feature.design && design > designKb) {
|
|
67
|
+
report.warn(feature.slug, `design.md and tasks.md are ${design.toFixed(1)} KB (soft limit ${designKb} KB)`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A fix (design 6.7): the fast-path rules, no open blocker, and in CI a fresh review and the record. */
|
|
72
|
+
function checkFix(report, root, config, fix, level) {
|
|
73
|
+
const scope = fix.slug;
|
|
74
|
+
for (const p of fixRules(root, config).problems) report.error(scope, `${p} (not a fix: use /agjan-spec)`);
|
|
75
|
+
if (level === 'staged') return fix;
|
|
76
|
+
for (const f of fix.findings.filter((x) => x.status === 'open' && x.severity === 'blocker')) {
|
|
77
|
+
report.error(scope, `open blocker ${f.id}: ${f.issue}`);
|
|
78
|
+
}
|
|
79
|
+
if (level === 'ci') {
|
|
80
|
+
const review = freshReview(root, config, fix, 'fix');
|
|
81
|
+
if (review.problem) report.error(scope, review.problem);
|
|
82
|
+
const record = readJson(join(fix.dir, FIX_FILE));
|
|
83
|
+
if (!record?.recorded_at_utc || record.review?.tree_hash !== review.record?.tree_hash) {
|
|
84
|
+
report.error(scope, 'the fix is not recorded for its latest review; run: npx agjan fix record');
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return fix;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A map run (design 6.2): a complete project.md, and in CI a fresh review and a current baseline. */
|
|
91
|
+
function checkMap(report, root, config, run, level) {
|
|
92
|
+
const scope = run.slug;
|
|
93
|
+
if (level === 'staged') return run;
|
|
94
|
+
for (const p of projectProblems(root, config)) report.error(scope, p);
|
|
95
|
+
if (level === 'ci') {
|
|
96
|
+
const review = freshReview(root, config, run, 'map');
|
|
97
|
+
if (review.problem) report.error(scope, review.problem);
|
|
98
|
+
const drift = baselineDrift(root, config);
|
|
99
|
+
if (drift === null) report.error(scope, `${BASELINE_FILE} is missing; run: npx agjan baseline refresh`);
|
|
100
|
+
else if (drift.length) report.error(scope, `${BASELINE_FILE} is not current (${drift.join(', ')}); run: npx agjan baseline refresh`);
|
|
101
|
+
}
|
|
102
|
+
return run;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function checkFeature(report, root, config, slug, level) {
|
|
106
|
+
const feature = loadFeature(root, slug);
|
|
107
|
+
if (feature.kind === 'fix') return checkFix(report, root, config, feature, level);
|
|
108
|
+
if (feature.kind === 'map') return checkMap(report, root, config, feature, level);
|
|
109
|
+
const scope = slug;
|
|
110
|
+
const ci = level === 'ci';
|
|
111
|
+
|
|
112
|
+
// Approved documents must not change silently (design 9.5), at every level.
|
|
113
|
+
if (feature.state.spec === 'stale') report.error(scope, 'spec.md changed after its approval; re-approve it or revert the change');
|
|
114
|
+
if (feature.state.design === 'stale') {
|
|
115
|
+
report.error(scope, 'the design is stale (design.md, tasks.md or the spec changed after approval); re-approve it or revert');
|
|
116
|
+
}
|
|
117
|
+
if (level === 'staged') return feature;
|
|
118
|
+
|
|
119
|
+
for (const p of documentProblems(feature)) report.error(scope, p);
|
|
120
|
+
for (const p of designProblems(feature, config)) report.error(scope, p);
|
|
121
|
+
checkSizes(report, feature, config);
|
|
122
|
+
|
|
123
|
+
if (ci) {
|
|
124
|
+
if (feature.state.spec !== 'approved') report.error(scope, `spec is ${feature.state.spec}, not approved`);
|
|
125
|
+
if (feature.state.design !== 'approved') report.error(scope, `design is ${feature.state.design}, not approved`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
for (const f of feature.findings.filter((x) => x.status === 'open')) {
|
|
129
|
+
const text = `open ${f.severity} ${f.id} (${f.step}): ${f.issue}`;
|
|
130
|
+
if (f.severity === 'blocker') report.error(scope, text);
|
|
131
|
+
else if (ci) report.warn(scope, text);
|
|
132
|
+
}
|
|
133
|
+
for (const a of feature.amendments.filter((x) => x.status === 'pending')) {
|
|
134
|
+
report.warn(scope, `amendment ${a.id} is waiting for approval: agjan approve amendment ${a.id}`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (feature.stage === 'build' && feature.spec && feature.tasks) {
|
|
138
|
+
const areaNames = [...new Set(feature.tasks.parsed.tasks.map((t) => resolveArea(config, t.area)?.name).filter(Boolean))];
|
|
139
|
+
const inTests = acTestFiles(root, config, areaNames, feature.key);
|
|
140
|
+
for (const ac of feature.spec.parsed.acs) {
|
|
141
|
+
if (ac.verification === 'manual') continue;
|
|
142
|
+
if (inTests.has(ac.id)) continue;
|
|
143
|
+
// During Build tests may still be missing; a finished feature must have them.
|
|
144
|
+
if (ci) report.error(scope, `${ac.id} has no test whose name contains the AC id`);
|
|
145
|
+
else report.warn(scope, `${ac.id} has no test yet`);
|
|
146
|
+
}
|
|
147
|
+
checkResults(report, root, config, feature, areaNames, ci);
|
|
148
|
+
if (ci) {
|
|
149
|
+
const progress = buildProgress(root, config, feature);
|
|
150
|
+
for (const t of progress.tasks.filter((x) => !x.commit)) {
|
|
151
|
+
report.error(scope, `task ${t.id} has no commit '${feature.key}-${t.id}: …'`);
|
|
152
|
+
}
|
|
153
|
+
for (const d of progress.due) report.error(scope, `review ${d.step} is due (${d.reason})`);
|
|
154
|
+
const { state, record } = verifyState(root, config, feature);
|
|
155
|
+
if (state === 'none') report.error(scope, 'no final verify review recorded');
|
|
156
|
+
else if (state === 'blocked') report.error(scope, `the final verify review ${record.id} is blocked`);
|
|
157
|
+
else if (state === 'stale') report.error(scope, `the final verify review ${record.id} is stale: files changed after it; run the verify review again`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return feature;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* With results configured (design 6.6): every automated AC needs a passing test case
|
|
165
|
+
* and none failing. Without results files the coverage is by test names only, which is
|
|
166
|
+
* reported, not failed: CI usually has no results of the change.
|
|
167
|
+
*/
|
|
168
|
+
function checkResults(report, root, config, feature, areaNames, ci) {
|
|
169
|
+
const loaded = loadResults(root, config, areaNames);
|
|
170
|
+
if (!loaded.configured) return;
|
|
171
|
+
const scope = feature.slug;
|
|
172
|
+
for (const file of loaded.missing) report.warn(scope, `test results ${file} not found; AC coverage is by test names only (run the tests)`);
|
|
173
|
+
for (const file of loaded.unreadable) report.warn(scope, `test results ${file} are in an unknown format (JUnit XML, Jest or Mocha JSON)`);
|
|
174
|
+
if (loaded.files.length === 0) return;
|
|
175
|
+
|
|
176
|
+
const counts = acResults(loaded, feature.key);
|
|
177
|
+
for (const ac of feature.spec.parsed.acs.filter((a) => a.verification !== 'manual')) {
|
|
178
|
+
const c = counts.get(ac.id);
|
|
179
|
+
if (c?.failed) {
|
|
180
|
+
report.error(scope, `${ac.id} has ${c.failed} failing test(s) in the results`);
|
|
181
|
+
} else if (!c?.passed) {
|
|
182
|
+
const message = `${ac.id} has no passing test in the results`;
|
|
183
|
+
if (ci) report.error(scope, message);
|
|
184
|
+
else report.warn(scope, message);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
// Results older than the newest change do not describe it.
|
|
188
|
+
const oldest = Math.min(...loaded.files.map((f) => f.mtimeMs));
|
|
189
|
+
const changed = changedFiles(root, resolveBase(root, config.git?.base)).filter((f) => !f.startsWith(`${FEATURES_DIR}/`) && !f.startsWith('.agjan/'));
|
|
190
|
+
const newest = Math.max(0, ...changed.map((f) => {
|
|
191
|
+
try {
|
|
192
|
+
return statSync(join(root, f)).mtimeMs;
|
|
193
|
+
} catch {
|
|
194
|
+
return 0; // deleted
|
|
195
|
+
}
|
|
196
|
+
}));
|
|
197
|
+
if (newest > oldest) report.warn(scope, 'test results are older than the latest change; run the tests again');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Changed files outside every Build feature's approved scope (design 6.5). */
|
|
201
|
+
function checkScope(report, features, config, files, level) {
|
|
202
|
+
const docs = `${FEATURES_DIR}/`;
|
|
203
|
+
// .agjan/ is local scratch (git-ignored by init); never part of a change.
|
|
204
|
+
const outside = files.filter((f) => !f.startsWith(docs) && !f.startsWith('.agjan/'));
|
|
205
|
+
// A branch carries features, or exactly one fix, or a map run (decision 57).
|
|
206
|
+
const fixes = features.filter((f) => f.kind === 'fix');
|
|
207
|
+
const maps = features.filter((f) => f.kind === 'map');
|
|
208
|
+
if (fixes.length > 1 || (fixes.length && fixes.length !== features.length) || (maps.length && maps.length !== features.length)) {
|
|
209
|
+
report.error('scope', `a branch carries either features, or one fix, or a map run; this one has ${features.map((f) => f.slug).join(', ')}`);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (fixes.length === 1) return; // the fast-path rules replace the task paths
|
|
213
|
+
if (maps.length) {
|
|
214
|
+
for (const file of outside) report.error('scope', `${file} changed, but a map run only writes ${FEATURES_DIR}/project.md and ${BASELINE_FILE}`);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (features.length === 0) {
|
|
218
|
+
// A change without any feature (setup, tooling, or work done without agjan). Only
|
|
219
|
+
// CI decides whether that is allowed; fixes get their own record in milestone 0.3.
|
|
220
|
+
if (level === 'ci' && outside.length > 0) {
|
|
221
|
+
const message = `${outside.length} changed file(s) are not covered by any feature`;
|
|
222
|
+
if (config.ci.untracked_changes === 'warn') report.warn('scope', message);
|
|
223
|
+
else report.error('scope', message);
|
|
224
|
+
}
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const building = features.filter((f) => f.stage === 'build');
|
|
228
|
+
const allowed = matcher(building.flatMap((f) => allowedPatterns(f, config)));
|
|
229
|
+
for (const file of outside) {
|
|
230
|
+
if (allowed(file)) continue;
|
|
231
|
+
if (building.length === 0) report.error('scope', `${file} changed, but no feature on this branch has an approved design`);
|
|
232
|
+
else report.error('scope', `${file} is outside the approved task paths; revert it or propose an amendment (agjan amend)`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* What agjan a manifest pins: the version range in package.json, or the resolved
|
|
238
|
+
* version, source and integrity in package-lock.json. Unparsable text pins "unknown".
|
|
239
|
+
*/
|
|
240
|
+
function agjanPin(file, text) {
|
|
241
|
+
try {
|
|
242
|
+
const data = JSON.parse(text);
|
|
243
|
+
if (file === 'package-lock.json') {
|
|
244
|
+
const entry = data.packages?.['node_modules/agjan'] ?? data.dependencies?.agjan ?? null;
|
|
245
|
+
return JSON.stringify(entry && { version: entry.version, resolved: entry.resolved, integrity: entry.integrity });
|
|
246
|
+
}
|
|
247
|
+
return JSON.stringify(data.devDependencies?.agjan ?? data.dependencies?.agjan ?? null);
|
|
248
|
+
} catch {
|
|
249
|
+
return 'unknown';
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Policy changes relative to the merge-base: reported, and failing only when configured.
|
|
255
|
+
* Returns the messages so the PR summary can list them.
|
|
256
|
+
*/
|
|
257
|
+
function checkPolicy(report, root, config, files, base) {
|
|
258
|
+
const isPolicy = matcher(POLICY_PATTERNS);
|
|
259
|
+
const from = mergeBase(root, base);
|
|
260
|
+
const messages = [];
|
|
261
|
+
for (const file of files.filter(isPolicy)) {
|
|
262
|
+
// Manifests count only when what they pin for agjan changed.
|
|
263
|
+
if ((file === 'package.json' || file === 'package-lock.json') && from) {
|
|
264
|
+
const before = git(root, ['show', `${from}:${file}`], { allowFail: true });
|
|
265
|
+
let after = null;
|
|
266
|
+
try {
|
|
267
|
+
after = readFileSync(join(root, file), 'utf8');
|
|
268
|
+
} catch {
|
|
269
|
+
// deleted
|
|
270
|
+
}
|
|
271
|
+
if (before !== null && after !== null && agjanPin(file, before) === agjanPin(file, after)) continue;
|
|
272
|
+
}
|
|
273
|
+
const message = `policy change: ${file}`;
|
|
274
|
+
messages.push(message);
|
|
275
|
+
if (config.ci.policy_changes === 'fail') report.error('policy', message);
|
|
276
|
+
else report.warn('policy', message);
|
|
277
|
+
}
|
|
278
|
+
// Editing a feature that already exists on the base branch (an archived feature).
|
|
279
|
+
if (from) {
|
|
280
|
+
for (const slug of new Set(files.filter((f) => f.startsWith(`${FEATURES_DIR}/`)).map((f) => f.split('/')[2]))) {
|
|
281
|
+
if (git(root, ['cat-file', '-e', `${from}:${FEATURES_DIR}/${slug}/approvals.json`], { allowFail: true }) !== null) {
|
|
282
|
+
const message = `feature '${slug}' already exists on the base branch; follow-up work should start a new feature`;
|
|
283
|
+
messages.push(message);
|
|
284
|
+
report.warn('policy', message);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return messages;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function print(ctx, report, level, slugs) {
|
|
292
|
+
const s = ctx.style;
|
|
293
|
+
for (const item of report.items) {
|
|
294
|
+
const label = item.level === 'error' ? s.bold(s.red('error')) : s.bold(s.yellow('warn '));
|
|
295
|
+
ctx.out(`${label} ${s.cyan(item.scope)}: ${item.message}`);
|
|
296
|
+
}
|
|
297
|
+
const what = slugs.length ? `feature(s) ${slugs.map((slug) => s.bold(slug)).join(', ')}` : 'no changed feature';
|
|
298
|
+
const color = verdictColor(s, report.errors, report.warnings);
|
|
299
|
+
const counts = color(`${report.errors} error(s), ${report.warnings} warning(s)`);
|
|
300
|
+
ctx.out(`${s.bold(`agjan check${level === 'default' ? '' : ` --${level}`}`)}: ${what}; ${counts}`);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** Managed files and the recorded kit version (design 16.3); warnings only. */
|
|
304
|
+
function checkManaged(report, ctx) {
|
|
305
|
+
const raw = readJson(join(ctx.root, CONFIG_FILE));
|
|
306
|
+
if (!raw) return; // not initialized: nothing is managed yet
|
|
307
|
+
if (raw.kit !== ctx.kit) report.warn('kit', `${CONFIG_FILE} records kit ${raw.kit}, this agjan is ${ctx.kit}; run: npx agjan sync`);
|
|
308
|
+
for (const item of planStatus(ctx.root, buildPlan(ctx.config))) {
|
|
309
|
+
if (item.state !== 'ok') report.warn('managed', `${item.path} is ${item.state}; run: npx agjan sync`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Runs the checks and returns the report without printing it, for `check` and for
|
|
315
|
+
* `pr-summary`. `level` is 'staged', 'default' or 'ci'.
|
|
316
|
+
*/
|
|
317
|
+
export function runChecks(ctx, level, explicitFeature) {
|
|
318
|
+
const root = ctx.root;
|
|
319
|
+
const config = ctx.config;
|
|
320
|
+
const base = resolveBase(root, config.git?.base);
|
|
321
|
+
const report = new Report();
|
|
322
|
+
|
|
323
|
+
if (level === 'ci' && !mergeBase(root, base)) {
|
|
324
|
+
report.error('git', `no merge-base with '${base ?? '(no base branch found)'}'; set git.base in ${CONFIG_FILE} or fetch the base branch`);
|
|
325
|
+
}
|
|
326
|
+
if (explicitFeature && !listFeatures(root).includes(explicitFeature)) throw new UsageError(`no feature '${explicitFeature}'`);
|
|
327
|
+
// Every feature changed on the branch, also with --staged: scope is judged against
|
|
328
|
+
// the whole branch's approved tasks, while only staged files are checked.
|
|
329
|
+
const slugs = explicitFeature ? [explicitFeature] : changedFeatures(root, config);
|
|
330
|
+
|
|
331
|
+
const features = slugs.map((slug) => checkFeature(report, root, config, slug, level));
|
|
332
|
+
const files = changedFiles(root, base, { staged: level === 'staged' });
|
|
333
|
+
checkScope(report, features, config, files, level);
|
|
334
|
+
const policy = level === 'ci' ? checkPolicy(report, root, config, files, base) : [];
|
|
335
|
+
if (level !== 'staged') {
|
|
336
|
+
checkManaged(report, ctx);
|
|
337
|
+
// The baseline describes the application; changes to watched files make it stale.
|
|
338
|
+
const drift = features.some((f) => f.kind === 'map') ? null : baselineDrift(root, config);
|
|
339
|
+
if (drift?.length) {
|
|
340
|
+
report.warn('baseline', `watched files changed since the baseline (${drift.join(', ')}); update ${FEATURES_DIR}/project.md and run: npx agjan baseline refresh`);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return { report, slugs, features, files, policy };
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** `--base <ref>` overrides git.base for this run, for CI where the base ref is known. */
|
|
347
|
+
export function overrideBase(ctx, base) {
|
|
348
|
+
if (base) ctx.config.git = { ...ctx.config.git, base };
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** agjan check [--staged | --ci] [--feature <slug>] [--base <ref>] */
|
|
352
|
+
export function run(ctx, argv) {
|
|
353
|
+
const { values } = parseArgs({
|
|
354
|
+
args: argv,
|
|
355
|
+
options: { staged: { type: 'boolean' }, ci: { type: 'boolean' }, feature: { type: 'string' }, base: { type: 'string' } },
|
|
356
|
+
});
|
|
357
|
+
if (values.staged && values.ci) throw new UsageError('use either --staged or --ci');
|
|
358
|
+
overrideBase(ctx, values.base);
|
|
359
|
+
const level = values.staged ? 'staged' : values.ci ? 'ci' : 'default';
|
|
360
|
+
const { report, slugs } = runChecks(ctx, level, values.feature);
|
|
361
|
+
print(ctx, report, level, slugs);
|
|
362
|
+
return report.errors > 0 ? 1 : 0;
|
|
363
|
+
}
|