agjan 0.0.11 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/bin/agjan.mjs +9 -2
- package/dist/adapters/claude-code.js +105 -0
- package/dist/adapters/copilot.js +80 -0
- package/dist/auto.js +24 -0
- package/dist/baseline.js +66 -0
- package/dist/browser.js +46 -0
- package/dist/cli.js +164 -0
- package/dist/commands/branch.js +71 -0
- package/dist/commands/check.js +420 -0
- package/dist/commands/fix.js +122 -0
- package/dist/commands/gates.js +462 -0
- package/dist/commands/lint.js +76 -0
- package/dist/commands/log.js +72 -0
- package/dist/commands/map.js +47 -0
- package/dist/commands/new.js +65 -0
- package/dist/commands/pr-summary.js +168 -0
- package/dist/commands/progress.js +42 -0
- package/dist/commands/report.js +105 -0
- package/dist/commands/review.js +146 -0
- package/dist/commands/setup.js +393 -0
- package/dist/commands/status.js +303 -0
- package/dist/commands/ui-test.js +39 -0
- package/dist/config.js +110 -0
- package/dist/context.js +56 -0
- package/dist/copilot-mcp.js +56 -0
- package/dist/documents.js +207 -0
- package/dist/errors.js +15 -0
- package/dist/feature.js +221 -0
- package/dist/fix.js +117 -0
- package/dist/git.js +88 -0
- package/{src/hash.mjs → dist/hash.js} +11 -14
- package/dist/hook.js +47 -0
- package/dist/json-file.js +26 -0
- package/dist/log.js +50 -0
- package/dist/managed.js +182 -0
- package/dist/mcp-config.js +76 -0
- package/dist/mcp.js +142 -0
- package/dist/plan.js +40 -0
- package/dist/progress-view.js +494 -0
- package/dist/progress.js +106 -0
- package/{src/review-policy.mjs → dist/review-policy.js} +39 -47
- package/dist/review.js +435 -0
- package/dist/scope.js +30 -0
- package/dist/terminal.js +127 -0
- package/dist/test-results.js +101 -0
- package/dist/tests-map.js +29 -0
- package/dist/types.js +5 -0
- package/dist/ui.js +136 -0
- package/dist/usage.js +116 -0
- package/dist/validate.js +46 -0
- package/docs/guide.md +62 -38
- package/kit/hooks/guard.mjs +48 -119
- package/kit/skills/agjan-build/SKILL.md +4 -2
- package/kit/skills/agjan-design/SKILL.md +6 -2
- package/kit/skills/agjan-fix/SKILL.md +8 -16
- package/kit/skills/agjan-map/SKILL.md +8 -16
- package/kit/skills/agjan-spec/SKILL.md +21 -17
- package/kit/skills/agjan-verify/SKILL.md +20 -13
- package/kit/templates/agents-block.md +3 -3
- package/package.json +11 -3
- package/schemas/approvals.schema.json +23 -2
- package/schemas/config.schema.json +0 -5
- package/schemas/log.schema.json +1 -1
- package/src/adapters/claude-code.mjs +0 -121
- package/src/adapters/copilot.mjs +0 -89
- package/src/baseline.mjs +0 -66
- package/src/browser.mjs +0 -49
- package/src/cli.mjs +0 -165
- package/src/commands/branch.mjs +0 -95
- package/src/commands/check.mjs +0 -376
- package/src/commands/fix.mjs +0 -116
- package/src/commands/gates.mjs +0 -267
- package/src/commands/lint.mjs +0 -74
- package/src/commands/log.mjs +0 -60
- package/src/commands/map.mjs +0 -47
- package/src/commands/new.mjs +0 -53
- package/src/commands/pr-summary.mjs +0 -137
- package/src/commands/progress.mjs +0 -40
- package/src/commands/report.mjs +0 -115
- package/src/commands/review.mjs +0 -141
- package/src/commands/setup.mjs +0 -393
- package/src/commands/status.mjs +0 -250
- package/src/commands/ui-test.mjs +0 -38
- package/src/config.mjs +0 -122
- package/src/context.mjs +0 -67
- package/src/copilot-mcp.mjs +0 -56
- package/src/documents.mjs +0 -188
- package/src/errors.mjs +0 -16
- package/src/feature.mjs +0 -225
- package/src/fix.mjs +0 -108
- package/src/git.mjs +0 -83
- package/src/hook.mjs +0 -48
- package/src/json-file.mjs +0 -24
- package/src/log.mjs +0 -51
- package/src/managed.mjs +0 -178
- package/src/mcp-config.mjs +0 -83
- package/src/mcp.mjs +0 -147
- package/src/plan.mjs +0 -45
- package/src/progress-view.mjs +0 -466
- package/src/progress.mjs +0 -85
- package/src/review.mjs +0 -416
- package/src/scope.mjs +0 -30
- package/src/terminal.mjs +0 -118
- package/src/test-results.mjs +0 -111
- package/src/tests-map.mjs +0 -29
- package/src/ui.mjs +0 -136
- package/src/usage.mjs +0 -118
- package/src/validate.mjs +0 -41
package/dist/terminal.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { openSync, readSync, writeSync, closeSync } from 'node:fs';
|
|
2
|
+
import { createInterface } from 'node:readline';
|
|
3
|
+
import { ReadStream, WriteStream, isatty } from 'node:tty';
|
|
4
|
+
import { AgjanError } from './errors.js';
|
|
5
|
+
/**
|
|
6
|
+
* Opens the controlling terminal, bypassing stdin and stdout, so that piped input
|
|
7
|
+
* cannot answer a confirmation (design 9.4). Returns null when there is none, which is
|
|
8
|
+
* the case for commands run by an agent's shell tool.
|
|
9
|
+
*/
|
|
10
|
+
function openTerminal() {
|
|
11
|
+
try {
|
|
12
|
+
if (process.platform === 'win32') {
|
|
13
|
+
return { input: openSync('CONIN$', 'r'), output: openSync('CONOUT$', 'w') };
|
|
14
|
+
}
|
|
15
|
+
const fd = openSync('/dev/tty', 'r+');
|
|
16
|
+
return { input: fd, output: fd };
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function readLine(fd) {
|
|
23
|
+
const bytes = [];
|
|
24
|
+
const buffer = Buffer.alloc(1);
|
|
25
|
+
for (;;) {
|
|
26
|
+
let n;
|
|
27
|
+
try {
|
|
28
|
+
n = readSync(fd, buffer, 0, 1, null);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (error.code === 'EAGAIN')
|
|
32
|
+
continue;
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
if (n === 0 || buffer[0] === 0x0a)
|
|
36
|
+
break;
|
|
37
|
+
bytes.push(buffer[0]);
|
|
38
|
+
}
|
|
39
|
+
return Buffer.from(bytes).toString('utf8').replace(/\r$/, '');
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Shows `summary` on the terminal and asks the human to type `expected`.
|
|
43
|
+
* Returns true when they did. Throws when no terminal is available.
|
|
44
|
+
*/
|
|
45
|
+
export function confirmOnTerminal(summary, expected) {
|
|
46
|
+
const tty = openTerminal();
|
|
47
|
+
if (!tty) {
|
|
48
|
+
throw new AgjanError('this command needs a human at an interactive terminal; agents must not run it.\n' +
|
|
49
|
+
'Run it yourself in a terminal (for example the VS Code integrated terminal).');
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
writeSync(tty.output, `${summary}\nType ${expected} to confirm, anything else to cancel: `);
|
|
53
|
+
return readLine(tty.input).trim() === expected;
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
closeSync(tty.input);
|
|
57
|
+
if (tty.output !== tty.input)
|
|
58
|
+
closeSync(tty.output);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Asks a yes/no question on the controlling terminal. Returns true or false, or null when
|
|
63
|
+
* there is no terminal (a script, CI or an agent): the caller then keeps its default.
|
|
64
|
+
*/
|
|
65
|
+
export function askOnTerminal(question, { defaultYes = false } = {}) {
|
|
66
|
+
const tty = openTerminal();
|
|
67
|
+
if (!tty)
|
|
68
|
+
return null;
|
|
69
|
+
try {
|
|
70
|
+
// The capital letter is the default (Enter), also in bold.
|
|
71
|
+
const bold = (t) => (process.env.NO_COLOR !== undefined ? t : `\x1b[1m\x1b[36m${t}\x1b[0m`);
|
|
72
|
+
writeSync(tty.output, `${question} ${defaultYes ? `[${bold('Y')}/n]` : `[y/${bold('N')}]`} `);
|
|
73
|
+
const answer = readLine(tty.input).trim();
|
|
74
|
+
if (answer === '')
|
|
75
|
+
return defaultYes;
|
|
76
|
+
return /^y(es)?$/i.test(answer);
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
closeSync(tty.input);
|
|
80
|
+
if (tty.output !== tty.input)
|
|
81
|
+
closeSync(tty.output);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Asks for one line on the controlling terminal with `initial` already typed, so the human
|
|
86
|
+
* can edit it in place (cursor keys, backspace) or press Enter to keep it. Without line
|
|
87
|
+
* editing (a console that is not a TTY stream) it falls back to "Enter keeps, text replaces".
|
|
88
|
+
*/
|
|
89
|
+
export async function editOnTerminal(prompt, initial) {
|
|
90
|
+
const tty = openTerminal();
|
|
91
|
+
if (!tty)
|
|
92
|
+
throw new AgjanError('this command needs a human at an interactive terminal');
|
|
93
|
+
let input;
|
|
94
|
+
let output;
|
|
95
|
+
try {
|
|
96
|
+
if (!isatty(tty.input))
|
|
97
|
+
throw new Error('not a TTY');
|
|
98
|
+
input = new ReadStream(tty.input);
|
|
99
|
+
// A second stream on the same descriptor must not close it; ReadStream owns it.
|
|
100
|
+
output = new WriteStream(tty.output === tty.input ? openSync(process.platform === 'win32' ? 'CONOUT$' : '/dev/tty', 'w') : tty.output);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
try {
|
|
104
|
+
writeSync(tty.output, `${prompt}${initial}\nPress Enter to keep it, or type a new one: `);
|
|
105
|
+
const answer = readLine(tty.input).trim();
|
|
106
|
+
return answer === '' ? initial : answer;
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
closeSync(tty.input);
|
|
110
|
+
if (tty.output !== tty.input)
|
|
111
|
+
closeSync(tty.output);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const rl = createInterface({ input, output, terminal: true });
|
|
115
|
+
try {
|
|
116
|
+
return await new Promise((resolve) => {
|
|
117
|
+
rl.on('SIGINT', () => resolve(''));
|
|
118
|
+
rl.question(prompt, resolve);
|
|
119
|
+
rl.write(initial);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
rl.close();
|
|
124
|
+
input.destroy();
|
|
125
|
+
output.destroy();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
const ENTITIES = { lt: '<', gt: '>', amp: '&', quot: '"', apos: "'" };
|
|
4
|
+
function decode(text) {
|
|
5
|
+
return text.replace(/&(#x?[0-9a-fA-F]+|[a-z]+);/g, (whole, code) => {
|
|
6
|
+
if (code[0] === '#')
|
|
7
|
+
return String.fromCodePoint(code[1] === 'x' ? parseInt(code.slice(2), 16) : Number(code.slice(1)));
|
|
8
|
+
return ENTITIES[code] ?? whole;
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function attribute(tag, name) {
|
|
12
|
+
const match = new RegExp(`\\s${name}\\s*=\\s*("([^"]*)"|'([^']*)')`).exec(tag);
|
|
13
|
+
return match ? decode(match[2] ?? match[3]) : '';
|
|
14
|
+
}
|
|
15
|
+
function parseJUnit(text) {
|
|
16
|
+
const results = [];
|
|
17
|
+
// Attributes as quoted strings: XML allows a literal '>' inside a value (name="<next>").
|
|
18
|
+
const testcase = /<testcase\b((?:\s+[^\s=/>]+\s*=\s*(?:"[^"]*"|'[^']*'))*)\s*(\/>|>([\s\S]*?)<\/testcase>)/g;
|
|
19
|
+
for (const match of text.matchAll(testcase)) {
|
|
20
|
+
const body = match[3] ?? '';
|
|
21
|
+
const status = /<(failure|error)\b/.test(body) ? 'failed' : /<skipped\b/.test(body) ? 'skipped' : 'passed';
|
|
22
|
+
const name = [attribute(match[1], 'classname'), attribute(match[1], 'name')].filter(Boolean).join(' ');
|
|
23
|
+
results.push({ name, status });
|
|
24
|
+
}
|
|
25
|
+
return results;
|
|
26
|
+
}
|
|
27
|
+
function parseJson(data) {
|
|
28
|
+
// Jest: { testResults: [{ assertionResults: [{ fullName, status }] }] }
|
|
29
|
+
if (Array.isArray(data?.testResults)) {
|
|
30
|
+
return data.testResults.flatMap((file) => (file.assertionResults ?? []).map((a) => ({
|
|
31
|
+
name: a.fullName ?? a.title ?? '',
|
|
32
|
+
status: a.status === 'passed' ? 'passed' : a.status === 'failed' ? 'failed' : 'skipped',
|
|
33
|
+
})));
|
|
34
|
+
}
|
|
35
|
+
// Mocha: { passes: [{ fullTitle }], failures: [...], pending: [...] }
|
|
36
|
+
if (Array.isArray(data?.passes) || Array.isArray(data?.failures)) {
|
|
37
|
+
const list = (items, status) => (items ?? []).map((t) => ({ name: t.fullTitle ?? t.title ?? '', status }));
|
|
38
|
+
return [...list(data.passes, 'passed'), ...list(data.failures, 'failed'), ...list(data.pending, 'skipped')];
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
/** Parses one results file; returns null when the format is not recognized. */
|
|
43
|
+
export function parseResults(text) {
|
|
44
|
+
const trimmed = text.trimStart();
|
|
45
|
+
if (trimmed.startsWith('<'))
|
|
46
|
+
return parseJUnit(text);
|
|
47
|
+
try {
|
|
48
|
+
return parseJson(JSON.parse(trimmed));
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** The configured results files of the given areas, read and parsed. */
|
|
55
|
+
export function loadResults(root, config, areaNames) {
|
|
56
|
+
const files = [...new Set(areaNames.flatMap((name) => {
|
|
57
|
+
const results = config.verify?.[name]?.results;
|
|
58
|
+
return results ? (Array.isArray(results) ? results : [results]) : [];
|
|
59
|
+
}))];
|
|
60
|
+
const loaded = [];
|
|
61
|
+
const missing = [];
|
|
62
|
+
const unreadable = [];
|
|
63
|
+
for (const file of files) {
|
|
64
|
+
const path = join(root, file);
|
|
65
|
+
if (!existsSync(path)) {
|
|
66
|
+
missing.push(file);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const results = parseResults(readFileSync(path, 'utf8'));
|
|
70
|
+
if (results === null)
|
|
71
|
+
unreadable.push(file);
|
|
72
|
+
else
|
|
73
|
+
loaded.push({ file, mtimeMs: statSync(path).mtimeMs, results });
|
|
74
|
+
}
|
|
75
|
+
return { configured: files.length > 0, files: loaded, missing, unreadable };
|
|
76
|
+
}
|
|
77
|
+
/** Per AC id of the feature: how many matching test cases passed, failed or were skipped. */
|
|
78
|
+
export function acResults(loaded, key) {
|
|
79
|
+
const pattern = new RegExp(`\\b${key}-AC[1-9][0-9]*\\b`, 'g');
|
|
80
|
+
const counts = new Map();
|
|
81
|
+
for (const { results } of loaded.files) {
|
|
82
|
+
for (const r of results) {
|
|
83
|
+
for (const id of new Set(r.name.match(pattern) ?? [])) {
|
|
84
|
+
const c = counts.get(id) ?? { passed: 0, failed: 0, skipped: 0 };
|
|
85
|
+
c[r.status] += 1;
|
|
86
|
+
counts.set(id, c);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return counts;
|
|
91
|
+
}
|
|
92
|
+
/** A short verdict per AC for summaries: passed (n), failed, skipped, not run. */
|
|
93
|
+
export function resultLabel(counts) {
|
|
94
|
+
if (!counts)
|
|
95
|
+
return 'not run';
|
|
96
|
+
if (counts.failed)
|
|
97
|
+
return 'failed';
|
|
98
|
+
if (counts.passed)
|
|
99
|
+
return `passed (${counts.passed})`;
|
|
100
|
+
return 'skipped';
|
|
101
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { git } from './git.js';
|
|
4
|
+
import { matcher } from './scope.js';
|
|
5
|
+
/** Tracked and untracked (not ignored) files of the repository. */
|
|
6
|
+
export function repoFiles(root) {
|
|
7
|
+
return git(root, ['ls-files', '--cached', '--others', '--exclude-standard']).split('\n').filter(Boolean);
|
|
8
|
+
}
|
|
9
|
+
/** For each AC id of the feature key, the test files that mention it. */
|
|
10
|
+
export function acTestFiles(root, config, areaNames, key) {
|
|
11
|
+
const isTest = matcher(areaNames.flatMap((name) => config.tests[name] ?? []));
|
|
12
|
+
const pattern = new RegExp(`\\b${key}-AC[1-9][0-9]*\\b`, 'g');
|
|
13
|
+
const found = new Map();
|
|
14
|
+
for (const file of repoFiles(root).filter(isTest)) {
|
|
15
|
+
let text;
|
|
16
|
+
try {
|
|
17
|
+
text = readFileSync(join(root, file), 'utf8');
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
continue; // deleted in the working tree
|
|
21
|
+
}
|
|
22
|
+
for (const match of text.matchAll(pattern)) {
|
|
23
|
+
if (!found.has(match[0]))
|
|
24
|
+
found.set(match[0], new Set());
|
|
25
|
+
found.get(match[0]).add(file);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return found;
|
|
29
|
+
}
|
package/dist/types.js
ADDED
package/dist/ui.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal styling for human-facing output. Colors only when the stream is a terminal, so
|
|
3
|
+
* agents (whose shell tools are not terminals), pipes, CI logs and tests get plain text.
|
|
4
|
+
* NO_COLOR turns colors off (https://no-color.org), FORCE_COLOR turns them on.
|
|
5
|
+
*/
|
|
6
|
+
const CODES = {
|
|
7
|
+
bold: [1, 22],
|
|
8
|
+
dim: [2, 22],
|
|
9
|
+
red: [31, 39],
|
|
10
|
+
green: [32, 39],
|
|
11
|
+
yellow: [33, 39],
|
|
12
|
+
blue: [34, 39],
|
|
13
|
+
magenta: [35, 39],
|
|
14
|
+
cyan: [36, 39],
|
|
15
|
+
gray: [90, 39],
|
|
16
|
+
};
|
|
17
|
+
export function colorEnabled(stream, env = process.env) {
|
|
18
|
+
// FORCE_COLOR wins over NO_COLOR, as in Node itself.
|
|
19
|
+
if ('FORCE_COLOR' in env)
|
|
20
|
+
return env.FORCE_COLOR !== '0' && env.FORCE_COLOR !== 'false';
|
|
21
|
+
if ('NO_COLOR' in env && env.NO_COLOR !== '')
|
|
22
|
+
return false;
|
|
23
|
+
return Boolean(stream?.isTTY) && env.TERM !== 'dumb';
|
|
24
|
+
}
|
|
25
|
+
/** Style functions for one stream: `s.red('text')`; identity functions when colors are off. */
|
|
26
|
+
export function createStyle(stream, env = process.env) {
|
|
27
|
+
const enabled = colorEnabled(stream, env);
|
|
28
|
+
const style = {
|
|
29
|
+
enabled,
|
|
30
|
+
truecolor: enabled && /^(truecolor|24bit)$/i.test(env.COLORTERM ?? ''),
|
|
31
|
+
// The agjan mark: on a terminal (also with NO_COLOR, then without colors), never for
|
|
32
|
+
// agents, pipes or CI; AGJAN_BANNER=0 turns it off.
|
|
33
|
+
brand: env.AGJAN_BANNER !== '0' && (enabled || Boolean(stream?.isTTY)),
|
|
34
|
+
};
|
|
35
|
+
for (const [name, [open, close]] of Object.entries(CODES)) {
|
|
36
|
+
style[name] = enabled ? (text) => `\x1b[${open}m${text}\x1b[${close}m` : (text) => String(text);
|
|
37
|
+
}
|
|
38
|
+
const plain = (text) => String(text);
|
|
39
|
+
// Brand teal (#2EC4A0); cyan where the terminal has no true color.
|
|
40
|
+
style.teal = !enabled ? plain : style.truecolor ? (text) => `\x1b[38;2;46;196;160m${text}\x1b[39m` : style.cyan;
|
|
41
|
+
style.violet = !enabled ? plain : style.truecolor ? (text) => `\x1b[38;2;168;145;245m${text}\x1b[39m` : style.magenta;
|
|
42
|
+
return style;
|
|
43
|
+
}
|
|
44
|
+
/** Colors for the words the commands print: states, verdicts, severities, levels. */
|
|
45
|
+
const TONES = {
|
|
46
|
+
green: ['ok', 'approved', 'pass', 'done', 'resolved', 'accepted', 'current', 'created', 'installed', 'merged', 'passed'],
|
|
47
|
+
yellow: ['warn', 'draft', 'pass-with-notes', 'open', 'due', 'proposed', 'updated', 'major', 'pending', 'kept', 'removed'],
|
|
48
|
+
red: ['error', 'fail', 'stale', 'blocked', 'rejected', 'blocker', 'blocks', 'modified', 'missing', 'failed'],
|
|
49
|
+
gray: ['unchanged', 'minor', 'none'],
|
|
50
|
+
};
|
|
51
|
+
const TONE_OF = new Map(Object.entries(TONES).flatMap(([tone, words]) => words.map((w) => [w, tone])));
|
|
52
|
+
/** Colors a known status word (`approved` green, `stale` red, ...); other text unchanged. */
|
|
53
|
+
export function tone(style, word) {
|
|
54
|
+
const name = TONE_OF.get(String(word).trim());
|
|
55
|
+
return name ? style[name](word) : String(word);
|
|
56
|
+
}
|
|
57
|
+
/** Pads by the plain text's length, so colored columns stay aligned. */
|
|
58
|
+
export function pad(styled, plain, width) {
|
|
59
|
+
return styled + ' '.repeat(Math.max(width - String(plain).length, 0));
|
|
60
|
+
}
|
|
61
|
+
/** Summary line color: red with errors, yellow with warnings only, green otherwise. */
|
|
62
|
+
export function verdictColor(style, errors, warnings) {
|
|
63
|
+
if (errors)
|
|
64
|
+
return style.red;
|
|
65
|
+
if (warnings)
|
|
66
|
+
return style.yellow;
|
|
67
|
+
return style.green;
|
|
68
|
+
}
|
|
69
|
+
/** Status words inside Markdown text: whole words only, `pass-with-notes` before `pass`. */
|
|
70
|
+
const WORDS = new RegExp(`(?<![\\w-])(${[...TONE_OF.keys()].sort((a, b) => b.length - a.length).join('|')})(?![\\w-])`, 'g');
|
|
71
|
+
/**
|
|
72
|
+
* Highlights Markdown for reading on a terminal: headings, bold text, table rules and the
|
|
73
|
+
* known status words. The text stays valid Markdown once the colors are stripped.
|
|
74
|
+
*/
|
|
75
|
+
export function highlightMarkdown(style, text) {
|
|
76
|
+
if (!style.enabled)
|
|
77
|
+
return text;
|
|
78
|
+
return text
|
|
79
|
+
.split('\n')
|
|
80
|
+
.map((line) => {
|
|
81
|
+
if (/^#{1,6} /.test(line))
|
|
82
|
+
return style.bold(style.cyan(line));
|
|
83
|
+
if (/^\|[-| ]+\|$/.test(line))
|
|
84
|
+
return style.gray(line);
|
|
85
|
+
return line
|
|
86
|
+
.replace(/\*\*([^*]+)\*\*/g, (_, inner) => style.bold(`**${inner}**`))
|
|
87
|
+
.replace(WORDS, (word) => tone(style, word))
|
|
88
|
+
.replace(/^\||(?<= )\|/g, (bar) => style.gray(bar));
|
|
89
|
+
})
|
|
90
|
+
.join('\n');
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The agjan mark (the "ag" of the app icon: the bowl of the a with its teal stem, and the
|
|
94
|
+
* g with its descender) with two lines of text beside it. Empty when the brand is off.
|
|
95
|
+
*/
|
|
96
|
+
export function mark(style, version, line2 = 'spec-driven work with AI agents, gated by you', line3 = '') {
|
|
97
|
+
if (!style.brand)
|
|
98
|
+
return [];
|
|
99
|
+
const stem = style.teal('┃');
|
|
100
|
+
return [
|
|
101
|
+
'',
|
|
102
|
+
` ╭─╮${stem}╭─╮╷ ${style.bold('agjan')} ${version}`,
|
|
103
|
+
` ╰─╯${stem}╰─╯│ ${line2}`,
|
|
104
|
+
` ╶─╯ ${line3 ? style.gray(line3) : ''}`.trimEnd(),
|
|
105
|
+
'',
|
|
106
|
+
];
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The mark blinks twice (its two bowls close and open), on a real terminal only: not in
|
|
110
|
+
* CI, not when the brand is off, not for streams that tests pass. `lines` is what
|
|
111
|
+
* mark() returned and was just written.
|
|
112
|
+
*/
|
|
113
|
+
export async function blink(ctx, lines) {
|
|
114
|
+
if (!lines.length || !ctx.isTTY || process.env.CI)
|
|
115
|
+
return;
|
|
116
|
+
const open = [lines[1], lines[2]];
|
|
117
|
+
const closed = [lines[1].replace(/╭─╮/g, ' '), lines[2].replace(/╰─╯/g, '╶─╴')];
|
|
118
|
+
const draw = ([top, bottom]) => ctx.write(`\x1b[${lines.length - 1}A\r\x1b[2K${top}\n\x1b[2K${bottom}\n\x1b[${lines.length - 3}B`);
|
|
119
|
+
const wait = (ms) => new Promise((done) => setTimeout(done, ms));
|
|
120
|
+
ctx.write('\x1b[?25l');
|
|
121
|
+
try {
|
|
122
|
+
for (const [frame, ms] of [[closed, 120], [open, 220], [closed, 120], [open, 0]]) {
|
|
123
|
+
draw(frame);
|
|
124
|
+
await wait(ms);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
finally {
|
|
128
|
+
ctx.write('\x1b[?25h');
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/** The one-line mark for frequent commands: `┃ agjan 0.0.6 · status`. Empty when the brand is off. */
|
|
132
|
+
export function markLine(style, version, what) {
|
|
133
|
+
if (!style.brand)
|
|
134
|
+
return [];
|
|
135
|
+
return [`${style.teal('┃')} ${style.bold('agjan')} ${style.gray(`${version} · ${what}`)}`];
|
|
136
|
+
}
|
package/dist/usage.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync, realpathSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
function readLines(file) {
|
|
5
|
+
try {
|
|
6
|
+
return readFileSync(file, 'utf8').split('\n');
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function parse(line) {
|
|
13
|
+
try {
|
|
14
|
+
return JSON.parse(line);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function sameDir(a, b) {
|
|
21
|
+
try {
|
|
22
|
+
return realpathSync(a) === realpathSync(b);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return a === b;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** Claude Code: ~/.claude/projects/<cwd with every non [A-Za-z0-9-] as '-'>/…jsonl. */
|
|
29
|
+
export function claudeUsage(root, { home = process.env.CLAUDE_CONFIG_DIR ?? join(homedir(), '.claude') } = {}) {
|
|
30
|
+
const dir = join(home, 'projects', realpathOr(root).replace(/[^A-Za-z0-9-]/g, '-'));
|
|
31
|
+
if (!existsSync(dir))
|
|
32
|
+
return [];
|
|
33
|
+
const files = [];
|
|
34
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
35
|
+
if (entry.isFile() && entry.name.endsWith('.jsonl'))
|
|
36
|
+
files.push(join(dir, entry.name));
|
|
37
|
+
const sub = join(dir, entry.name, 'subagents');
|
|
38
|
+
if (entry.isDirectory() && existsSync(sub)) {
|
|
39
|
+
for (const f of readdirSync(sub))
|
|
40
|
+
if (f.endsWith('.jsonl'))
|
|
41
|
+
files.push(join(sub, f));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const seen = new Set();
|
|
45
|
+
const records = [];
|
|
46
|
+
for (const file of files) {
|
|
47
|
+
for (const line of readLines(file)) {
|
|
48
|
+
const entry = parse(line);
|
|
49
|
+
const usage = entry?.message?.usage;
|
|
50
|
+
if (entry?.type !== 'assistant' || !usage || !entry.timestamp)
|
|
51
|
+
continue;
|
|
52
|
+
// One response can be written as several lines with the same request id.
|
|
53
|
+
const key = entry.requestId ?? entry.message.id ?? `${file}:${entry.uuid}`;
|
|
54
|
+
if (seen.has(key))
|
|
55
|
+
continue;
|
|
56
|
+
seen.add(key);
|
|
57
|
+
records.push({
|
|
58
|
+
at: Date.parse(entry.timestamp),
|
|
59
|
+
host: 'claude-code',
|
|
60
|
+
model: entry.message.model ?? 'unknown',
|
|
61
|
+
input: usage.input_tokens ?? 0,
|
|
62
|
+
cacheRead: usage.cache_read_input_tokens ?? 0,
|
|
63
|
+
cacheWrite: usage.cache_creation_input_tokens ?? 0,
|
|
64
|
+
output: usage.output_tokens ?? 0,
|
|
65
|
+
agent: entry.isSidechain ? 'subagent' : 'main',
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return records;
|
|
70
|
+
}
|
|
71
|
+
function realpathOr(path) {
|
|
72
|
+
try {
|
|
73
|
+
return realpathSync(path);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return path;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** GitHub Copilot CLI: ~/.copilot/session-state/<id>/events.jsonl of sessions in this repository. */
|
|
80
|
+
export function copilotUsage(root, { home = process.env.COPILOT_HOME ?? join(homedir(), '.copilot') } = {}) {
|
|
81
|
+
const dir = join(home, 'session-state');
|
|
82
|
+
if (!existsSync(dir))
|
|
83
|
+
return [];
|
|
84
|
+
const records = [];
|
|
85
|
+
for (const id of readdirSync(dir)) {
|
|
86
|
+
const lines = readLines(join(dir, id, 'events.jsonl'));
|
|
87
|
+
const start = lines.map(parse).find((e) => e?.type === 'session.start');
|
|
88
|
+
const context = start?.data?.context ?? {};
|
|
89
|
+
const where = context.gitRoot ?? context.cwd;
|
|
90
|
+
if (!where || !sameDir(where, root))
|
|
91
|
+
continue;
|
|
92
|
+
for (const line of lines) {
|
|
93
|
+
const event = parse(line);
|
|
94
|
+
const usage = event?.type === 'session.usage_record' ? event.data?.usage : null;
|
|
95
|
+
if (!usage || !event.timestamp)
|
|
96
|
+
continue;
|
|
97
|
+
records.push({
|
|
98
|
+
at: Date.parse(event.timestamp),
|
|
99
|
+
host: 'copilot',
|
|
100
|
+
model: usage.model ?? 'unknown',
|
|
101
|
+
input: usage.inputTokens ?? 0,
|
|
102
|
+
cacheRead: usage.cacheReadTokens ?? 0,
|
|
103
|
+
cacheWrite: usage.cacheWriteTokens ?? 0,
|
|
104
|
+
output: usage.outputTokens ?? 0,
|
|
105
|
+
agent: usage.initiator === 'sub-agent' ? 'subagent' : 'main',
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return records;
|
|
110
|
+
}
|
|
111
|
+
/** Usage of all hosts in this repository between `from` and `to` (ms). */
|
|
112
|
+
export function usageBetween(root, from, to, homes = {}) {
|
|
113
|
+
return [...claudeUsage(root, { home: homes.claude }), ...copilotUsage(root, { home: homes.copilot })]
|
|
114
|
+
.filter((r) => r.at >= from && r.at <= to)
|
|
115
|
+
.sort((a, b) => a.at - b.at);
|
|
116
|
+
}
|
package/dist/validate.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { resolveArea } from './config.js';
|
|
2
|
+
/** Structural problems of the feature's documents, prefixed with the file name. */
|
|
3
|
+
export function documentProblems(feature) {
|
|
4
|
+
const problems = [];
|
|
5
|
+
for (const name of ['spec', 'design', 'tasks']) {
|
|
6
|
+
const doc = feature[name];
|
|
7
|
+
if (doc)
|
|
8
|
+
problems.push(...doc.parsed.problems.map((p) => `${name}.md: ${p}`));
|
|
9
|
+
}
|
|
10
|
+
return problems;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Consistency between design, tasks, spec and configuration: areas exist and are
|
|
14
|
+
* enabled, every task belongs to an approved area, every `covers` id is a spec AC, and
|
|
15
|
+
* every AC is covered by at least one task.
|
|
16
|
+
*/
|
|
17
|
+
export function designProblems(feature, config) {
|
|
18
|
+
const problems = [];
|
|
19
|
+
if (!feature.design || !feature.tasks || !feature.spec)
|
|
20
|
+
return problems;
|
|
21
|
+
const designAreas = new Set();
|
|
22
|
+
for (const id of feature.design.parsed.areas) {
|
|
23
|
+
const area = resolveArea(config, id);
|
|
24
|
+
if (!area)
|
|
25
|
+
problems.push(`design.md: area '${id}' is not an area of the enabled domains (${config.domains.join(', ')})`);
|
|
26
|
+
else
|
|
27
|
+
designAreas.add(area.id);
|
|
28
|
+
}
|
|
29
|
+
const acIds = new Set(feature.spec.parsed.acs.map((ac) => ac.id));
|
|
30
|
+
const covered = new Set();
|
|
31
|
+
for (const task of feature.tasks.parsed.tasks) {
|
|
32
|
+
const area = resolveArea(config, task.area ?? '');
|
|
33
|
+
if (!area || !designAreas.has(area.id))
|
|
34
|
+
problems.push(`tasks.md: ${task.id} area '${task.area}' is not one of the design's areas`);
|
|
35
|
+
for (const id of task.covers) {
|
|
36
|
+
if (!acIds.has(id))
|
|
37
|
+
problems.push(`tasks.md: ${task.id} covers '${id}', which is not an AC of the spec`);
|
|
38
|
+
covered.add(id);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
for (const id of acIds) {
|
|
42
|
+
if (!covered.has(id))
|
|
43
|
+
problems.push(`${id} is not covered by any task`);
|
|
44
|
+
}
|
|
45
|
+
return problems;
|
|
46
|
+
}
|