agentic-mobile-kit 0.7.19 → 0.14.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.
Files changed (69) hide show
  1. package/README.md +18 -3
  2. package/bin/cli.js +5 -3
  3. package/lib/doctor.js +31 -5
  4. package/lib/init.js +21 -25
  5. package/lib/menu.js +3 -3
  6. package/package.json +1 -1
  7. package/stacks/react-native/expo/.eas/workflows/pr-preview.yml +54 -0
  8. package/stacks/react-native/expo/AGENTS.md +30 -17
  9. package/stacks/react-native/expo/docs/amk/expo.md +11 -1
  10. package/stacks/react-native/template/.agents/skills/m-bugfix/SKILL.md +8 -5
  11. package/stacks/react-native/template/.agents/skills/m-change/SKILL.md +3 -2
  12. package/stacks/react-native/template/.agents/skills/m-continue/SKILL.md +6 -4
  13. package/stacks/react-native/template/.agents/skills/m-explore/SKILL.md +1 -1
  14. package/stacks/react-native/template/.agents/skills/m-feature/SKILL.md +29 -26
  15. package/stacks/react-native/template/.agents/skills/m-feature/build.md +35 -0
  16. package/stacks/react-native/template/.agents/skills/m-feature/plan.md +43 -0
  17. package/stacks/react-native/template/.agents/skills/m-learn/SKILL.md +3 -1
  18. package/stacks/react-native/template/.agents/skills/m-onboard/SKILL.md +3 -1
  19. package/stacks/react-native/template/.agents/skills/m-pause/SKILL.md +2 -0
  20. package/stacks/react-native/template/.agents/skills/m-prototype/SKILL.md +1 -1
  21. package/stacks/react-native/template/.agents/skills/m-release/SKILL.md +5 -4
  22. package/stacks/react-native/template/.agents/skills/m-report/SKILL.md +25 -0
  23. package/stacks/react-native/template/.agents/skills/m-review/SKILL.md +4 -3
  24. package/stacks/react-native/template/.agents/skills/m-test/SKILL.md +24 -0
  25. package/stacks/react-native/template/.agents/skills/m-test/agents/openai.yaml +3 -0
  26. package/stacks/react-native/template/.githooks/pre-commit +12 -0
  27. package/stacks/react-native/template/.github/pull_request_template.md +4 -0
  28. package/stacks/react-native/template/.github/workflows/amk-ai-deps.yml +90 -0
  29. package/stacks/react-native/template/.github/workflows/amk-ai-review.yml +82 -0
  30. package/stacks/react-native/template/.github/workflows/amk-kit-update.yml +95 -0
  31. package/stacks/react-native/template/.github/workflows/amk-ready.yml +39 -0
  32. package/stacks/react-native/template/.github/workflows/ci.yml +79 -13
  33. package/stacks/react-native/template/.github/workflows/e2e.yml +10 -1
  34. package/stacks/react-native/template/.rulesync/hooks.jsonc +3 -1
  35. package/stacks/react-native/template/.rulesync/subagents/m-device-checker.md +2 -1
  36. package/stacks/react-native/template/.rulesync/subagents/m-reviewer.md +4 -2
  37. package/stacks/react-native/template/.rulesync/subagents/m-spec-writer.md +15 -0
  38. package/stacks/react-native/template/.rulesync/subagents/m-tester.md +23 -0
  39. package/stacks/react-native/template/AGENTS.md +23 -10
  40. package/stacks/react-native/template/codemagic.yaml +82 -1
  41. package/stacks/react-native/template/docs/amk/codemagic.md +8 -0
  42. package/stacks/react-native/template/docs/amk/react-native.md +11 -1
  43. package/stacks/react-native/template/docs/amk/team-process.md +33 -1
  44. package/stacks/react-native/template/rulesync.jsonc +1 -1
  45. package/stacks/react-native/template/scripts/amk/ai-job.js +106 -0
  46. package/stacks/react-native/template/scripts/amk/autopilot.js +837 -0
  47. package/stacks/react-native/template/scripts/amk/baseline.js +19 -7
  48. package/stacks/react-native/template/scripts/amk/ci-scope.js +59 -0
  49. package/stacks/react-native/template/scripts/amk/coverage.js +111 -0
  50. package/stacks/react-native/template/scripts/amk/guard.js +42 -0
  51. package/stacks/react-native/template/scripts/amk/handoff-check.js +87 -14
  52. package/stacks/react-native/template/scripts/amk/issue.js +31 -0
  53. package/stacks/react-native/template/scripts/amk/metrics.js +164 -0
  54. package/stacks/react-native/template/scripts/amk/pr.js +244 -0
  55. package/stacks/react-native/template/scripts/amk/redact.js +57 -0
  56. package/stacks/react-native/template/scripts/amk/release-build.sh +14 -0
  57. package/stacks/react-native/template/scripts/amk/report.js +48 -11
  58. package/stacks/react-native/template/scripts/amk/rn-check.js +195 -47
  59. package/stacks/react-native/template/scripts/amk/shots.js +131 -0
  60. package/stacks/react-native/template/scripts/amk/specs.js +717 -34
  61. package/stacks/react-native/template/scripts/amk/tool-configs.js +41 -0
  62. package/stacks/react-native/template/scripts/amk/verify.sh +127 -23
  63. package/stacks/react-native/template/specs/_templates/design.md +22 -0
  64. package/stacks/react-native/template/specs/_templates/flow.yaml +2 -1
  65. package/stacks/react-native/template/specs/_templates/plan.md +54 -0
  66. package/stacks/react-native/template/specs/_templates/progress.md +1 -1
  67. package/stacks/react-native/template/specs/_templates/requirements.md +40 -0
  68. package/stacks/react-native/template/specs/_templates/test-report.md +25 -0
  69. package/stacks/react-native/template/.github/workflows/ios.yml +0 -41
@@ -5,13 +5,16 @@
5
5
  // lint ESLint errors in the files changed since the base, compared with those files at the base
6
6
  // typecheck TypeScript errors (file, code, message), compared with a type check of the base
7
7
  // test failing Jest tests and test files, compared with a test run of the base
8
- // The base: AMK_BASE, else the first of the remote's default branch, origin/main, origin/master, main and master
9
- // whose merge base with HEAD has a package.json (a default branch left at an old first commit isn't the app's base).
8
+ // The base: AMK_BASE; on a release branch (release/…), the last release's tag (v…); else the first of the remote's
9
+ // default branch, origin/main, origin/master, main and master whose merge base with HEAD has a package.json (a default
10
+ // branch left at an old first commit isn't the app's base).
10
11
  // Its merge base is checked out once into a temporary git worktree that shares node_modules, and its results are kept
11
12
  // for the next run.
13
+ // `test --related` runs only the Jest tests related to the files changed since the base (Jest's --changedSince, which
14
+ // follows the imports), for each task while building; the whole suite runs before a pull request.
12
15
  // `base` prints the base, for reviews of a branch's changes. `staged <files>` is the pre-commit hook's lint: it passes
13
16
  // when the files have no ESLint error they didn't have at HEAD, and exits with 3 when it can't find ESLint's CLI.
14
- // Usage: node scripts/amk/baseline.js lint|typecheck|test|base|staged <files>
17
+ // Usage: node scripts/amk/baseline.js lint|typecheck|test [--related]|base|staged <files>
15
18
  'use strict';
16
19
  const fs = require('fs');
17
20
  const os = require('os');
@@ -20,6 +23,7 @@ const crypto = require('crypto');
20
23
  const { spawnSync, execFileSync } = require('child_process');
21
24
 
22
25
  const check = process.argv[2];
26
+ const related = check === 'test' && process.argv.includes('--related');
23
27
  if (!['lint', 'typecheck', 'test', 'base', 'staged'].includes(check)) {
24
28
  console.error('usage: node scripts/amk/baseline.js lint|typecheck|test|base|staged <files>');
25
29
  process.exit(2);
@@ -49,12 +53,12 @@ const minus = (a, b) => {
49
53
  });
50
54
  };
51
55
 
52
- // the package.json script, through pm-run.sh, in dir (the app, or the base's worktree)
53
- function runScript(dir) {
56
+ // the package.json script, through pm-run.sh, in dir (the app, or the base's worktree), with extra arguments for it
57
+ function runScript(dir, extra = []) {
54
58
  fs.mkdirSync(scratch, { recursive: true });
55
59
  const json = path.join(scratch, `jest-${hash(dir)}.json`);
56
60
  fs.rmSync(json, { force: true });
57
- const r = spawnSync('sh', [path.join(root, 'scripts/amk/pm-run.sh'), check, ...(jest ? ['--json', `--outputFile=${json}`] : [])],
61
+ const r = spawnSync('sh', [path.join(root, 'scripts/amk/pm-run.sh'), check, ...extra, ...(jest ? ['--json', `--outputFile=${json}`] : [])],
58
62
  { cwd: dir, encoding: 'utf8', env: { ...process.env, CI: 'true' }, maxBuffer: 2 ** 28 });
59
63
  return { code: r.status, out: `${r.stdout || ''}${r.stderr || ''}`, json, dir };
60
64
  }
@@ -84,6 +88,10 @@ const hasApp = sha => Boolean(sha && git('rev-parse', '--verify', '--quiet', `${
84
88
  // { ref, note }: the note says when the remote's default branch was passed over
85
89
  function baseRef() {
86
90
  if (process.env.AMK_BASE) return { ref: process.env.AMK_BASE, note: '' };
91
+ // a release branch is compared with the last release, not with main: what main gained since that release is the
92
+ // release's change, so a test that broke on main since then is new
93
+ const tag = /^release\//.test(git('symbolic-ref', '--quiet', '--short', 'HEAD')) && git('describe', '--tags', '--abbrev=0', '--match', 'v*', 'HEAD');
94
+ if (tag && hasApp(tag)) return { ref: tag, note: '' };
87
95
  const remoteDefault = git('symbolic-ref', '--quiet', '--short', 'refs/remotes/origin/HEAD');
88
96
  const refs = [...new Set([remoteDefault, 'origin/main', 'origin/master', 'main', 'master'])]
89
97
  .filter(r => r && git('rev-parse', '--verify', '--quiet', `${r}^{commit}`));
@@ -222,8 +230,12 @@ if (check === 'staged') {
222
230
  process.exit(1);
223
231
  }
224
232
 
225
- const now = runScript(root);
233
+ // --related: the tests of the files changed since where this branch left the base (the whole suite without one)
234
+ const since = related && jest && (() => { const f = baseRef(); return f && git('merge-base', 'HEAD', f.ref); })();
235
+ const now = runScript(root, since ? [`--changedSince=${since}`] : []);
226
236
  process.stdout.write(now.out);
237
+ if (related) console.log(since ? `test: only the tests related to the files changed since ${since.slice(0, 7)} (verify.sh quick without --related runs them all)`
238
+ : `test: every test ran, as ${jest ? 'there\'s no base branch to compare with' : 'the test script isn\'t Jest'}`);
227
239
  if (now.code === 0) process.exit(0);
228
240
 
229
241
  const found = baseRef();
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env node
2
+ // CI by what a change touches (.github/workflows/ci.yml), so every check it needs runs and none it doesn't:
3
+ // node scripts/amk/ci-scope.js <base> the jobs the change since <base> calls for, as GitHub Actions outputs:
4
+ // code (the tests and the Android build) and native (the iOS build). Only specs or docs: neither. Native files
5
+ // (ios/, android/, package.json and lockfiles, an Expo app's config, config plugins and patches, the CI workflow
6
+ // itself): both. Anything else: code. A change it can't read (a first push, a base it can't find): both
7
+ // node scripts/amk/ci-scope.js verdict ci.yml's summary job ("checks", the one branch protection requires): the jobs'
8
+ // results, from NEEDS (toJSON(needs)), fail it when one failed or was cancelled, or was skipped though the change
9
+ // called for it
10
+ 'use strict';
11
+ const { execFileSync } = require('child_process');
12
+
13
+ const DOCS = /^(docs|specs)\/|\.md$|^\.github\/(ISSUE_TEMPLATE\/|pull_request_template\.md$)|^(LICENSE|CODEOWNERS|\.github\/CODEOWNERS)$/i;
14
+ const NATIVE = /^(ios|android|plugins|patches)\/|^(package\.json|package-lock\.json|yarn\.lock|pnpm-lock\.yaml|bun\.lockb?|app\.json|app\.config\.(js|ts|json)|eas\.json|react-native\.config\.js|Gemfile(\.lock)?|\.github\/workflows\/ci\.yml)$/;
15
+
16
+ function scope(files) {
17
+ if (!files) return { code: true, native: true, why: 'the change couldn\'t be read, so everything runs' };
18
+ if (!files.length || files.every(f => DOCS.test(f))) return { code: false, native: false, why: `only specs or docs (${files.length} file(s))` };
19
+ const native = files.filter(f => NATIVE.test(f));
20
+ return { code: true, native: native.length > 0, why: native.length ? `native files: ${native.slice(0, 5).join(', ')}${native.length > 5 ? ', …' : ''}` : 'code, no native files' };
21
+ }
22
+
23
+ // the jobs' results: a failure, a cancel, or a skip the change didn't allow
24
+ function verdict(needs) {
25
+ const out = (needs.scope && needs.scope.outputs) || {};
26
+ const wanted = { tests: out.code === 'true', android: out.code === 'true', ios: out.native === 'true' };
27
+ const problems = [];
28
+ const lines = [];
29
+ for (const [job, r] of Object.entries(needs)) {
30
+ const result = r.result;
31
+ lines.push(`${job}: ${result}`);
32
+ if (['failure', 'cancelled'].includes(result)) problems.push(`${job} ${result === 'failure' ? 'failed' : 'was cancelled'}`);
33
+ else if (result === 'skipped' && (job === 'scope' || job === 'basics' || wanted[job])) problems.push(`${job} was skipped, though this change calls for it`);
34
+ }
35
+ return { ok: !problems.length, problems, lines };
36
+ }
37
+
38
+ module.exports = { scope, verdict, DOCS, NATIVE };
39
+
40
+ if (require.main === module) {
41
+ const [arg] = process.argv.slice(2);
42
+ if (arg === 'verdict') {
43
+ let needs = {};
44
+ try { needs = JSON.parse(process.env.NEEDS || '{}'); } catch {}
45
+ const v = verdict(needs);
46
+ console.log(v.lines.join('\n'));
47
+ if (!v.ok) { console.log(`✗ ${v.problems.join('; ')}`); process.exit(1); }
48
+ console.log('✓ every job this change calls for passed');
49
+ } else {
50
+ let files = null;
51
+ // a branch's first push has no "before" (all zeros): everything runs
52
+ if (arg && !/^0+$/.test(arg)) {
53
+ try { files = execFileSync('git', ['diff', '--name-only', `${arg}...HEAD`], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).split('\n').filter(Boolean); } catch {}
54
+ }
55
+ const s = scope(files);
56
+ console.error(`scope: ${s.why}`);
57
+ console.log(`code=${s.code}\nnative=${s.native}`);
58
+ }
59
+ }
@@ -0,0 +1,111 @@
1
+ #!/usr/bin/env node
2
+ // Coverage floors: a check that tests weren't deleted or weakened. .amk-coverage, which the project commits, holds the
3
+ // lowest each coverage measure of the Jest tests may go (statements, branches, functions and lines, in percent), and the
4
+ // slack: how many points under its floor a measure may drop and still pass. Without the file there's no check.
5
+ // node scripts/amk/coverage.js write a coverage run of the tests as they are writes .amk-coverage
6
+ // node scripts/amk/coverage.js check runs them with Jest's --coverageThreshold set from it
7
+ // node scripts/amk/coverage.js raise raises the floors to the coverage now, where it's higher
8
+ // node scripts/amk/coverage.js lower --reason "<why>" lowers them to the coverage now, and records why
9
+ // Floors are the coverage rounded up, and come down only with a reason, which the file keeps. The verdict is the coverage
10
+ // alone: failing tests are verify.sh quick's to judge, against the base branch.
11
+ 'use strict';
12
+ const fs = require('fs');
13
+ const os = require('os');
14
+ const path = require('path');
15
+ const { spawnSync } = require('child_process');
16
+
17
+ const FILE = '.amk-coverage';
18
+ const MEASURES = ['statements', 'branches', 'functions', 'lines'];
19
+ const [cmd, ...args] = process.argv.slice(2);
20
+ const root = process.cwd();
21
+ const read = f => { try { return fs.readFileSync(f, 'utf8').replace(/\r\n/g, '\n'); } catch { return null; } };
22
+ const up = n => Math.min(100, Math.ceil(n));
23
+
24
+ // the floors file: its values ({ statements: 73, …, slack: 2 }) and its text
25
+ function floors() {
26
+ const text = read(path.join(root, FILE));
27
+ if (text === null) return null;
28
+ const values = {};
29
+ for (const l of text.split('\n')) {
30
+ const m = /^\s*([a-z]+)\s+(\d+(?:\.\d+)?)\s*$/.exec(l);
31
+ if (m) values[m[1]] = Number(m[2]);
32
+ }
33
+ return { values, text };
34
+ }
35
+
36
+ // a coverage run of the app's test script, with these extra arguments: each measure's percentage, or why there's none
37
+ function coverage(extra = []) {
38
+ let pkg = {};
39
+ try { pkg = JSON.parse(read(path.join(root, 'package.json'))); } catch {}
40
+ if (!/\bjest\b/.test((pkg.scripts || {}).test || '')) return { why: 'the test script isn\'t Jest' };
41
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'amk-coverage-'));
42
+ try {
43
+ const r = spawnSync('sh', [path.join(root, 'scripts/amk/pm-run.sh'), 'test', '--coverage', '--coverageReporters=json-summary',
44
+ '--coverageReporters=text-summary', `--coverageDirectory=${dir}`, ...extra], { cwd: root, encoding: 'utf8', env: { ...process.env, CI: 'true' }, maxBuffer: 2 ** 28 });
45
+ let total;
46
+ try { total = JSON.parse(fs.readFileSync(path.join(dir, 'coverage-summary.json'), 'utf8')).total; } catch {
47
+ return { why: `Jest wrote no coverage report: ${`${r.stderr || ''}${r.stdout || ''}`.trim().split('\n').pop()}` };
48
+ }
49
+ // "Unknown" for a measure with nothing to count (no branches at all)
50
+ return { pct: Object.fromEntries(MEASURES.map(m => [m, Number((total[m] || {}).pct) || 0])) };
51
+ } finally {
52
+ fs.rmSync(dir, { recursive: true, force: true });
53
+ }
54
+ }
55
+
56
+ // the floors file with these values, and a line about the change
57
+ function save(f, values, note) {
58
+ let text = f.text;
59
+ for (const [k, v] of Object.entries(values)) text = text.replace(new RegExp(`^(\\s*${k}\\s+)\\d+(?:\\.\\d+)?(\\s*)$`, 'm'), `$1${v}$2`);
60
+ fs.writeFileSync(path.join(root, FILE), note ? `${text.replace(/\n*$/, '\n')}${note}\n` : text);
61
+ }
62
+
63
+ function main() {
64
+ if (!['write', 'check', 'raise', 'lower'].includes(cmd)) {
65
+ const head = fs.readFileSync(process.argv[1], 'utf8').split('\n').slice(1);
66
+ console.log(head.slice(0, head.findIndex(l => !l.startsWith('//'))).map(l => l.replace(/^\/\/ ?/, '')).join('\n'));
67
+ return 2;
68
+ }
69
+ const f = floors();
70
+ if (cmd === 'write') {
71
+ if (f) { console.error(`coverage: ${FILE} is there already: raise or lower its floors instead`); return 1; }
72
+ const run = coverage();
73
+ if (run.why) { console.error(`coverage: ${run.why}`); return 1; }
74
+ fs.writeFileSync(path.join(root, FILE), [
75
+ '# Coverage floors: node scripts/amk/coverage.js check fails when a measure of the Jest tests drops more than the slack',
76
+ '# below its floor, in percent. Floors only rise (coverage.js raise), or come down with a reason (coverage.js lower',
77
+ '# --reason "<why>"), which is kept here.',
78
+ ...MEASURES.map(m => `${m} ${up(run.pct[m])}`), 'slack 2', '',
79
+ ].join('\n'));
80
+ console.log(`${FILE}: ${MEASURES.map(m => `${m} ${up(run.pct[m])}`).join(', ')}, slack 2. Commit it.`);
81
+ return 0;
82
+ }
83
+ if (!f) {
84
+ console.log(`coverage: no ${FILE}, so nothing to check (node scripts/amk/coverage.js write makes one)`);
85
+ return cmd === 'check' ? 0 : 1;
86
+ }
87
+ const missing = MEASURES.filter(m => !(m in f.values));
88
+ if (missing.length) { console.error(`coverage: ${FILE} has no floor for ${missing.join(', ')}: add it, or write the file again`); return 1; }
89
+ const slack = f.values.slack || 0;
90
+ if (cmd === 'check') {
91
+ const min = Object.fromEntries(MEASURES.map(m => [m, Math.max(0, f.values[m] - slack)]));
92
+ const run = coverage([`--coverageThreshold=${JSON.stringify({ global: min })}`]);
93
+ if (run.why) { console.error(`coverage: ${run.why}`); return 1; }
94
+ const low = MEASURES.filter(m => run.pct[m] < min[m]);
95
+ console.log(`coverage: ${MEASURES.map(m => `${m} ${run.pct[m]}% (floor ${f.values[m]})`).join(', ')}, slack ${slack}`);
96
+ for (const m of low) console.log(` ✗ ${m} ${run.pct[m]}% is more than ${slack} points under its floor of ${f.values[m]}: tests were deleted or weakened, or new code has none`);
97
+ return low.length ? 1 : 0;
98
+ }
99
+ const reason = args.includes('--reason') ? (args[args.indexOf('--reason') + 1] || '').trim() : '';
100
+ if (cmd === 'lower' && !reason) { console.error('coverage: lower needs --reason "<why>", which the floors file keeps'); return 2; }
101
+ const run = coverage();
102
+ if (run.why) { console.error(`coverage: ${run.why}`); return 1; }
103
+ const next = Object.fromEntries(MEASURES.map(m => [m, cmd === 'raise' ? Math.max(f.values[m], up(run.pct[m])) : Math.min(f.values[m], up(run.pct[m]))]));
104
+ const changes = MEASURES.filter(m => next[m] !== f.values[m]).map(m => `${m} ${f.values[m]} → ${next[m]}`);
105
+ if (!changes.length) { console.log(`coverage: nothing to ${cmd}: ${MEASURES.map(m => `${m} ${run.pct[m]}%`).join(', ')}`); return 0; }
106
+ save(f, next, cmd === 'lower' && `# lowered ${new Date().toISOString().slice(0, 10)}, ${changes.join(', ')}: ${reason}`);
107
+ console.log(`${FILE}: ${cmd === 'raise' ? 'raised' : 'lowered'} ${changes.join(', ')}`);
108
+ return 0;
109
+ }
110
+
111
+ process.exitCode = main();
@@ -64,4 +64,46 @@ for (const c of commands) {
64
64
  for (const p of paths) {
65
65
  if (secret(p)) block(`access to a secret file: ${p}`);
66
66
  }
67
+
68
+ // An unattended build (scripts/amk/autopilot.js sets AMK_AUTOPILOT) works under your GitHub login, so it never does what
69
+ // only a person should: call GitHub's API directly, approve a pull request or request changes, merge without
70
+ // auto-merge, touch the trigger labels, push anything but a feature/ or fix/ branch by name, or change the guardrails
71
+ // (autopilot.js and the pre-commit hook hold the same list)
72
+ if (process.env.AMK_AUTOPILOT) {
73
+ const GUARDRAILS = /^(?:scripts\/amk\/|\.rulesync\/|\.githooks\/|\.github\/workflows\/|\.agents\/|\.claude\/|\.codex\/|(?:\.github\/|docs\/)?CODEOWNERS$|AGENTS\.md$|CLAUDE\.md$|rulesync\.jsonc$|\.amk-(?:coverage|size-caps|secrets)$)/;
74
+ const roots = [root, fs.realpathSync(root)];
75
+ const rel = p => roots.map(r => path.relative(r, path.resolve(r, p)).replace(/\\/g, '/')).find(x => !x.startsWith('..')) || '';
76
+ // relative to the project, or as written without leading ../ (after a cd the guard doesn't follow)
77
+ const kept = p => { const f = String(p).replace(/^['"]|['"]$/g, ''); return GUARDRAILS.test(rel(f)) || GUARDRAILS.test(f.replace(/^(?:\.{1,2}\/)+/, '')); };
78
+ const refuse = (what, c) => block(`an unattended build ${what}: ${c.trim()}`);
79
+ const writes = /^\s*(?:cp|mv|rm|rmdir|ln|chmod|chown|truncate|touch|mkdir|install|tee|dd|patch|sed\s+(?:-[a-zA-Z]*i|--in-place)|perl\s+-[a-zA-Z]*i|git\s+(?:apply|checkout|restore|rm|mv))\b/;
80
+ const edits = /^(Edit|Write|MultiEdit|NotebookEdit|apply_patch|write_to_file|replace_file_content|multi_replace_file_content|sed_file|notebook_edit)$/;
81
+ if (edits.test(payload.tool_name || '') && paths.some(kept)) refuse('doesn\'t change the kit\'s guardrails (its scripts, hooks and tool settings, the workflows, the agents\' instructions)', paths.join(' '));
82
+ for (const c of commands) {
83
+ for (const f of c.matchAll(/^\*\*\* (?:Add|Update|Delete) File: (.+)$|^\*\*\* Move to: (.+)$/gm)) {
84
+ if (kept((f[1] || f[2]).trim())) refuse('doesn\'t change the kit\'s guardrails (its scripts, hooks and tool settings, the workflows, the agents\' instructions)', f[0]);
85
+ }
86
+ // a redirection such as 2>&1 isn't a command separator
87
+ for (const s of c.replace(/\d*>&\d*-?/g, ' ').split(/&&|\|\||[;&|\n]/)) {
88
+ if (/\bgh\s+api\b/.test(s)) refuse('doesn\'t call GitHub\'s API directly', s);
89
+ if (/\bgh\s+pr\s+review\b/.test(s)) refuse('doesn\'t approve pull requests or request changes: a person does', s);
90
+ if (/\bgh\s+pr\s+merge\b/.test(s) && !/\s--auto\b/.test(s)) refuse('merges only through auto-merge, after a person approves', s);
91
+ if (/\bgh\b/.test(s) && /\bamk-(?:ready|fix)\b/.test(s)) refuse('doesn\'t add or remove the amk-ready or amk-fix labels: a person does', s);
92
+ const push = /\bgit\s+push\b(.*)/.exec(s);
93
+ if (push) {
94
+ // its words, without redirections (2>/dev/null, > out.txt)
95
+ const words = push[1].replace(/\d*>{1,2}\s*\S+|<\s*\S+/g, ' ').trim().split(/\s+/).filter(Boolean);
96
+ const refs = words.filter(w => !w.startsWith('-')).slice(1);
97
+ if (words.some(w => w.startsWith('-') && !/^(-u|--set-upstream|-q|--quiet|-v|--verbose|--progress|--no-progress|--porcelain)$/.test(w))
98
+ || !refs.length || refs.some(r => !/^(?:feature|fix)\/[A-Za-z0-9._/-]+$/.test(r))) {
99
+ refuse('pushes only its own feature/ or fix/ branch, named (git push -u origin feature/<id>)', s);
100
+ }
101
+ }
102
+ const targets = [...s.matchAll(/>{1,2}\s*([^\s;&|<>]+)/g)].map(m => m[1]);
103
+ if (targets.some(kept) || (writes.test(s) && s.trim().split(/\s+/).slice(1).some(kept))) {
104
+ refuse('doesn\'t change the kit\'s guardrails (its scripts, hooks and tool settings, the workflows, the agents\' instructions)', s);
105
+ }
106
+ }
107
+ }
108
+ }
67
109
  process.exit(0);
@@ -1,31 +1,104 @@
1
1
  #!/usr/bin/env node
2
- // Agent stop hook. If code changed but the active spec's handoff block wasn't updated, ask the agent to update it.
2
+ // Agent hooks on the handoff in specs/<id>/progress.md.
3
+ // Stop: if code changed but the active spec's handoff block wasn't updated, ask the agent to update it. In an unattended
4
+ // build (scripts/amk/autopilot.js sets AMK_AUTOPILOT), it also keeps the feature of the branch going: a stop while it
5
+ // isn't done or blocked goes back to the agent, unless .amk/build/STOP is there, or the agent stops again with nothing
6
+ // changed since its last stop (no commit, no task ticked, the same next step), which autopilot.js treats as a stall.
7
+ // Both: a feature marked done whose requirements never reached the living spec is asked to merge them first.
8
+ // SessionStart, after a compaction: the active spec's handoff, its next task and the definition of done, read fresh from
9
+ // the files, so nothing the summary lost is missing.
3
10
  const fs = require('fs');
4
11
  const path = require('path');
5
12
  const { execSync } = require('child_process');
6
13
  // the project this script is in (scripts/amk/): the agent may have moved into a subfolder
7
14
  const root = path.join(__dirname, '..', '..');
15
+ const read = f => { try { return fs.readFileSync(path.join(root, f), 'utf8').replace(/\r\n/g, '\n'); } catch { return null; } };
16
+ // (only the end trimmed: git status's lines start with a space)
17
+ const sh = cmd => { try { return execSync(cmd, { cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).replace(/\s+$/, ''); } catch { return ''; } };
8
18
 
9
19
  let payload = {};
10
20
  try { payload = JSON.parse(fs.readFileSync(0, 'utf8')); } catch {}
11
- if (payload.stop_hook_active) process.exit(0); // already reminded once; don't loop
21
+ const unattended = Boolean(process.env.AMK_AUTOPILOT);
12
22
 
13
23
  // a spec counts only when its handoff says "Status: in progress" (not done, not an untouched template copy)
14
24
  const inProgress = d => {
15
25
  const f = path.join(root, 'specs', d, 'progress.md');
16
26
  return d !== '_templates' && fs.existsSync(f) && /^Status:\s*in progress/im.test(fs.readFileSync(f, 'utf8'));
17
27
  };
18
- const specs = fs.existsSync(path.join(root, 'specs')) ? fs.readdirSync(path.join(root, 'specs')).filter(inProgress) : [];
19
- if (specs.length === 0) process.exit(0); // no spec in progress (quick change, bug fix, or finished work)
20
-
21
- const changed = execSync('git status --porcelain --untracked-files=all', { cwd: root, encoding: 'utf8' }) // every new file, not just its new folder
22
- .split('\n').filter(Boolean).map(l => l.slice(3));
23
- const codeChanged = changed.some(f => !f.startsWith('specs/') && !f.startsWith('.amk/'));
24
- const handoffUpdated = changed.some(f => /^specs\/[^/]+\/progress\.md$/.test(f));
25
-
26
- if (codeChanged && !handoffUpdated) {
27
- process.stderr.write('There are uncommitted code changes, but no specs/<id>/progress.md was updated. ' +
28
- 'Update its handoff block (status, next step, decisions, last checks) before stopping.\n');
29
- process.exit(2);
28
+ const working = fs.existsSync(path.join(root, 'specs')) ? fs.readdirSync(path.join(root, 'specs')).filter(inProgress) : [];
29
+ // the feature of this branch (feature/<id>), where an unattended build works
30
+ const feature = (/^feature\/(.+)$/.exec(sh('git symbolic-ref --quiet --short HEAD')) || [])[1];
31
+ const own = feature && read(`specs/${feature}/progress.md`) !== null ? feature : null;
32
+
33
+ if (payload.hook_event_name === 'SessionStart') {
34
+ const id = own || working[0];
35
+ if (!id) process.exit(0);
36
+ const p = read(`specs/${id}/progress.md`) || '';
37
+ const handoff = (/^## Handoff[\s\S]*?(?=^## |(?![\s\S]))/m.exec(p) || [''])[0].trim();
38
+ const next = ((read(`specs/${id}/tasks.md`) || '').split('\n').find(l => /^\s*- \[ \]/.test(l)) || 'none left').trim();
39
+ const done = (/^## Definition of done\s*$([\s\S]*?)(?=^## |(?![\s\S]))/m.exec(read('AGENTS.md') || '') || [])[1] || '';
40
+ console.log([`After the compaction, from the files as they are now. The work in progress: specs/${id}.`, '', handoff, '',
41
+ `Its next task (specs/${id}/tasks.md): ${next}`, ...(done.trim() ? ['', `Definition of done (AGENTS.md):${done.replace(/\s+$/, '')}`] : []),
42
+ ...(unattended ? ['', 'Nobody is watching this build: record a blocked question under Blocked: in its progress.md instead of asking one.'] : [])].join('\n'));
43
+ process.exit(0);
44
+ }
45
+
46
+ if (payload.stop_hook_active && !unattended) process.exit(0); // already reminded once; don't loop
47
+
48
+ // unattended: the feature's state at this stop (its last commit, ticked tasks and next step)
49
+ const progress = unattended && own ? read(`specs/${own}/progress.md`) || '' : null;
50
+ const state = progress === null ? null : JSON.stringify([sh('git rev-parse HEAD'),
51
+ ((read(`specs/${own}/tasks.md`) || '').match(/^\s*- \[[xX]\]/gm) || []).length, (/^Next step:\s*(.*)$/m.exec(progress) || [])[1] || '']);
52
+
53
+ // code changed without the handoff: asked once (stop_hook_active: the agent goes on because of an earlier ask)
54
+ if ((working.length || progress !== null) && !payload.stop_hook_active) {
55
+ const changed = sh('git status --porcelain --untracked-files=all') // every new file, not just its new folder
56
+ .split('\n').filter(Boolean).map(l => l.slice(3));
57
+ const codeChanged = changed.some(f => !f.startsWith('specs/') && !f.startsWith('.amk/'));
58
+ const handoffUpdated = changed.some(f => /^specs\/[^/]+\/progress\.md$/.test(f));
59
+ if (codeChanged && !handoffUpdated) {
60
+ process.stderr.write('There are uncommitted code changes, but no specs/<id>/progress.md was updated. ' +
61
+ 'Update its handoff block (status, next step, decisions, last checks) before stopping.\n');
62
+ process.exit(2);
63
+ }
64
+ }
65
+
66
+ // a feature marked done whose requirements never reached the living spec (specs.js merge writes "Source: specs/<id>"
67
+ // beside each one it adds or changes): asked once, as the living spec must say what ships
68
+ if (own && !payload.stop_hook_active) {
69
+ const req = read(`specs/${own}/requirements.md`) || '';
70
+ const changes = /^## (?:ADDED|MODIFIED) Requirements/m.test(req) && /^### Requirement:/m.test(req);
71
+ let merged = false;
72
+ try { merged = fs.readdirSync(path.join(root, 'specs', 'current')).some(f => f.endsWith('.md') && (read(`specs/current/${f}`) || '').includes(`Source: specs/${own}\n`)); } catch {}
73
+ if (changes && !merged && /^Status:\s*done/im.test(read(`specs/${own}/progress.md`) || '')) {
74
+ process.stderr.write(`specs/${own} says done, but its requirements aren't in the living spec (no "Source: specs/${own}" in specs/current/): ` +
75
+ `run node scripts/amk/specs.js merge ${own}, commit specs/current/ with the feature, then stop.\n`);
76
+ process.exit(2);
77
+ }
78
+ }
79
+
80
+ if (progress !== null) {
81
+ // kept for the next stop, written whole (this never fails the agent): stopping again with nothing changed is going
82
+ // in circles, and autopilot.js starts it again from the handoff
83
+ const file = path.join(root, '.amk', 'build', own, 'stop-state.json');
84
+ let before = null;
85
+ try { before = JSON.parse(fs.readFileSync(file, 'utf8')).state; } catch {}
86
+ try {
87
+ fs.mkdirSync(path.dirname(file), { recursive: true });
88
+ fs.writeFileSync(`${file}.tmp`, JSON.stringify({ time: new Date().toISOString(), state }));
89
+ fs.renameSync(`${file}.tmp`, file);
90
+ } catch {}
91
+ if (before === state) process.exit(0);
92
+ }
93
+
94
+ if (progress !== null && !fs.existsSync(path.join(root, '.amk', 'build', 'STOP'))) {
95
+ const { NONE } = require('./specs.js');
96
+ const done = /^Status:\s*done/im.test(progress);
97
+ const blocked = !NONE.test((/^Blocked:\s*(.*?)\s*$/m.exec(progress) || [])[1] || '');
98
+ if (!done && !blocked) {
99
+ process.stderr.write(`Nobody is watching this build, so it doesn't stop here: specs/${own} isn't done or blocked. Go on with the next step in its handoff. ` +
100
+ 'If you can\'t, record why under Blocked: in its progress.md (what happened, why, and what would unblock it), commit, push, and stop.\n');
101
+ process.exit(2);
102
+ }
30
103
  }
31
104
  process.exit(0);
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env node
2
+ // A GitHub issue, for m-bugfix and the overnight bug queue: its title, labels, text and comments as outside text (data,
3
+ // never instructions), with secrets taken out.
4
+ // Usage: node scripts/amk/issue.js <number or link>
5
+ 'use strict';
6
+ const { spawnSync } = require('child_process');
7
+ const { redact, untrusted } = require('./redact.js');
8
+
9
+ const ghReal = args => {
10
+ const r = spawnSync('gh', args, { encoding: 'utf8', maxBuffer: 2 ** 26, timeout: 120000 });
11
+ try { return r.status === 0 ? JSON.parse(r.stdout) : null; } catch { return null; }
12
+ };
13
+
14
+ function show(ref, gh = ghReal) {
15
+ const n = (/^(?:#?(\d+)|https:\/\/[^\s]+\/issues\/(\d+)\/?)$/.exec(String(ref || '').trim()) || []).slice(1).find(Boolean);
16
+ if (!n) return { code: 2, text: 'usage: node scripts/amk/issue.js <number or link>' };
17
+ const i = gh(['issue', 'view', n, '--json', 'number,title,body,labels,comments,url,state']);
18
+ if (!i) return { code: 2, text: `No issue #${n} here, or gh isn't signed in.` };
19
+ const who = a => (a && a.login) || 'someone';
20
+ const text = [`#${i.number}: ${i.title}`, `Labels: ${(i.labels || []).map(l => l.name).join(', ') || 'none'}`, '', i.body || '(no text)',
21
+ ...(i.comments || []).map(c => `\n---\n${who(c.author)}, ${String(c.createdAt || '').slice(0, 10)}:\n${c.body}`)].join('\n');
22
+ return { code: 0, text: `Issue #${i.number} (${String(i.state || '').toLowerCase()}): ${i.url}\n${untrusted(redact(text), `issue #${i.number}`)}` };
23
+ }
24
+
25
+ module.exports = { show };
26
+
27
+ if (require.main === module) {
28
+ const r = show(process.argv[2]);
29
+ (r.code ? console.error : console.log)(r.text);
30
+ process.exitCode = r.code;
31
+ }
@@ -0,0 +1,164 @@
1
+ #!/usr/bin/env node
2
+ // The team's numbers for the monthly review (docs/amk/team-process.md), from git, GitHub (through gh, when it's signed
3
+ // in) and the unattended build's logs: pull request size, the wait for a first review, CI's failure rate, reopened
4
+ // issues, the time from a spec's approval to its pull request, where the features stand, how many of the agents' pull
5
+ // requests merged and how many needed rework first, and the unattended sessions. Markdown, for the review.
6
+ // node scripts/amk/metrics.js [--since <date, or a span such as 30d>] (default: the last 30 days)
7
+ // node scripts/amk/metrics.js --comments [--since …] the period's review comments and the reasons kept with amk-ok,
8
+ // as outside text: m-learn reads them for the ones that keep coming back
9
+ 'use strict';
10
+ const fs = require('fs');
11
+ const path = require('path');
12
+ const { spawnSync } = require('child_process');
13
+ const { untrusted } = require('./redact.js');
14
+
15
+ const ROOT = path.join(__dirname, '..', '..');
16
+ const sh = (cmd, args) => spawnSync(cmd, args, { cwd: ROOT, encoding: 'utf8', maxBuffer: 2 ** 27, timeout: 180000 });
17
+ const IO = {
18
+ gh: args => { const r = sh('gh', args); try { return r.status === 0 ? JSON.parse(r.stdout) : null; } catch { return null; } },
19
+ git: args => { const r = sh('git', args); return r.status === 0 ? r.stdout : ''; },
20
+ read: f => { try { return fs.readFileSync(path.join(ROOT, f), 'utf8'); } catch { return null; } },
21
+ list: d => { try { return fs.readdirSync(path.join(ROOT, d)); } catch { return []; } },
22
+ now: () => Date.now(),
23
+ };
24
+
25
+ const DAY = 864e5;
26
+ const median = xs => { const s = xs.filter(x => x != null && !Number.isNaN(x)).sort((a, b) => a - b); return s.length ? (s.length % 2 ? s[(s.length - 1) / 2] : (s[s.length / 2 - 1] + s[s.length / 2]) / 2) : null; };
27
+ const at = s => Date.parse(s);
28
+ const round = (n, d = 1) => Math.round(n * 10 ** d) / 10 ** d;
29
+ const hours = ms => (ms < 2 * DAY ? `${round(ms / 36e5)} h` : `${round(ms / DAY)} days`);
30
+ // "30d" or "2026-10-01": the period's first day, as YYYY-MM-DD
31
+ function sinceDate(arg, now) {
32
+ const n = /^(\d+)d$/.exec(arg || '30d');
33
+ if (n) return new Date(now - Number(n[1]) * DAY).toISOString().slice(0, 10);
34
+ return /^\d{4}-\d{2}-\d{2}$/.test(arg) ? arg : null;
35
+ }
36
+ // the period's pull requests, through a query of its own: gh pr list --json reviews,commits asks GitHub for every commit's
37
+ // authors too, past what one query may ask (500,000 nodes). ponytail: the first 100 pull requests of the period
38
+ const QUERY = `query($q: String!) { search(query: $q, type: ISSUE, first: 100) { nodes { ... on PullRequest {
39
+ number headRefName author { login } createdAt mergedAt state additions deletions
40
+ reviews(first: 30) { nodes { author { login } state submittedAt body } }
41
+ commits(first: 50) { nodes { commit { message committedDate } } } } } } }`;
42
+ function pullRequests(repo, from, io) {
43
+ const r = io.gh(['api', 'graphql', '-f', `query=${QUERY}`, '-f', `q=repo:${repo} is:pr created:>=${from}`]);
44
+ const nodes = r && r.data && r.data.search && r.data.search.nodes;
45
+ return Array.isArray(nodes) ? nodes.filter(p => p.number).map(p => ({ ...p, reviews: (p.reviews || {}).nodes || [],
46
+ commits: ((p.commits || {}).nodes || []).map(c => c.commit || {}) })) : null;
47
+ }
48
+ // an agent's commit: a trailer naming an AI tool as co-author, or an AI job's fix
49
+ const AGENT = /^(?:Co-Authored-By:.*\b(?:Claude|Codex|OpenAI|GPT|Gemini|Antigravity|Cursor|Copilot)\b|AMK-Agent-Fix:)/mi;
50
+
51
+ function collect(from, io = IO) {
52
+ const t0 = at(`${from}T00:00:00Z`);
53
+ const out = { from };
54
+ const repo = (io.gh(['repo', 'view', '--json', 'nameWithOwner']) || {}).nameWithOwner;
55
+ const prs = repo && pullRequests(repo, from, io);
56
+ out.gh = Array.isArray(prs);
57
+ if (out.gh) {
58
+ const merged = prs.filter(p => p.mergedAt && at(p.mergedAt) >= t0);
59
+ const sizes = merged.map(p => (p.additions || 0) + (p.deletions || 0));
60
+ out.merged = { count: merged.length, median: median(sizes), over: sizes.filter(n => n > 600).length };
61
+ const firstReview = p => (p.reviews || []).filter(r => (r.author || {}).login !== (p.author || {}).login && r.submittedAt).map(r => at(r.submittedAt)).sort((a, b) => a - b)[0];
62
+ const waits = prs.map(p => firstReview(p) && firstReview(p) - at(p.createdAt)).filter(Boolean);
63
+ out.review = { median: median(waits), reviewed: waits.length, unreviewed: merged.filter(p => !firstReview(p)).length };
64
+ const runs = io.gh(['run', 'list', '--workflow', 'ci.yml', '--created', `>=${from}`, '--status', 'completed', '--limit', '500', '--json', 'conclusion']);
65
+ const decided = (runs || []).filter(r => ['success', 'failure'].includes(r.conclusion));
66
+ out.ci = runs ? { runs: decided.length, failed: decided.filter(r => r.conclusion === 'failure').length } : null;
67
+ // ponytail: the repository's newest 100 issue events; a busier month needs paging
68
+ const events = io.gh(['api', `repos/${repo}/issues/events?per_page=100`]) || [];
69
+ out.reopened = new Set(events.filter(e => e.event === 'reopened' && at(e.created_at) >= t0).map(e => (e.issue || {}).number)).size;
70
+ // the agents' pull requests: merged or closed without merging, and rework (changes requested, or commits after the first review)
71
+ const agents = prs.filter(p => (p.commits || []).some(c => AGENT.test(c.message || '')));
72
+ const rework = agents.filter(p => (p.reviews || []).some(r => r.state === 'CHANGES_REQUESTED')
73
+ || (firstReview(p) && (p.commits || []).some(c => at(c.committedDate) > firstReview(p))));
74
+ out.agents = { total: agents.length, merged: agents.filter(p => p.mergedAt).length, closed: agents.filter(p => p.state === 'CLOSED').length, rework: rework.length };
75
+ // from a spec's approval commit to its feature's pull request
76
+ out.approvalToPr = median(io.git(['log', '--all', `--since=${from}`, '--format=%cI%x09%s', '--grep=^docs(spec): .* approved']).split('\n').filter(Boolean)
77
+ .flatMap(l => { const [when, subject] = l.split('\t'); const ids = (/^docs\(spec\): (.+?) approved/.exec(subject) || [])[1] || ''; return ids.split(/,\s*|\s+and\s+/).map(id => [id.trim(), at(when)]); })
78
+ .map(([id, t]) => { const p = prs.find(x => x.headRefName === `feature/${id}`); return p ? at(p.createdAt) - t : null; })
79
+ .filter(ms => ms != null && ms >= 0));
80
+ out.repo = repo;
81
+ out.prs = prs;
82
+ }
83
+ // where the features stand now, from their handoffs
84
+ const states = { done: 0, blocked: 0, 'in progress': 0 };
85
+ for (const d of io.list('specs')) {
86
+ if (['_templates', 'current'].includes(d)) continue;
87
+ const p = io.read(`specs/${d}/progress.md`);
88
+ if (!p) continue;
89
+ const blocked = (/^Blocked:\s*(.*?)\s*$/m.exec(p) || [])[1] || '';
90
+ if (/^Status:\s*done/im.test(p)) states.done++;
91
+ else if (blocked && !/^(none|nothing|n\/a|-+|<.*>)?$/i.test(blocked)) states.blocked++;
92
+ else if (/^Status:\s*in progress/im.test(p)) states['in progress']++;
93
+ }
94
+ out.features = states;
95
+ // the unattended build's sessions, from its logs (on this machine)
96
+ const sessions = { count: 0, tokens: 0, cost: 0 };
97
+ for (const d of io.list('.amk/build')) {
98
+ let day = null;
99
+ for (const l of (io.read(`.amk/build/${d}/build.log`) || '').split('\n')) {
100
+ const h = /^=== (\d{4}-\d{2}-\d{2}) /.exec(l);
101
+ if (h) day = h[1];
102
+ const m = /■ session: \d+ min, (\d+) tokens(?:, \$([\d.]+))?/.exec(l);
103
+ if (m && day && day >= from) { sessions.count++; sessions.tokens += Number(m[1]); sessions.cost += Number(m[2] || 0); }
104
+ }
105
+ }
106
+ out.sessions = sessions;
107
+ // findings kept with amk-ok in the period, and why
108
+ out.kept = io.git(['grep', '-n', '-I', '-E', 'amk-ok [0-9]{4}-[0-9]{2}-[0-9]{2}']).split('\n').filter(Boolean)
109
+ .map(l => { const m = /^([^:]+):(\d+):.*amk-ok (\d{4}-\d{2}-\d{2})\s*:\s*(.*?)\s*(?:\*\/\s*\}?)?$/.exec(l); return m && { file: m[1], line: m[2], date: m[3], why: m[4] }; })
110
+ .filter(k => k && k.date >= from);
111
+ return out;
112
+ }
113
+
114
+ function report(m, today) {
115
+ const row = (a, b) => `| ${a} | ${b} |`;
116
+ const needsGh = 'gh isn\'t signed in here, or this isn\'t a GitHub repository';
117
+ const pct = (a, b) => (b ? `${Math.round((100 * a) / b)}%` : 'n/a');
118
+ const lines = [`# Metrics: ${m.from} to ${today}`, '', '| Measure | This period |', '|---|---|'];
119
+ if (m.gh) {
120
+ lines.push(row('Pull requests merged', `${m.merged.count}${m.merged.count ? `, median ${m.merged.median} changed lines, ${m.merged.over} over 600` : ''}`));
121
+ lines.push(row('Wait for a first review', m.review.median != null ? `median ${hours(m.review.median)} (${m.review.reviewed} reviewed; ${m.review.unreviewed} merged without a review)` : `none reviewed (${m.review.unreviewed} merged without a review)`));
122
+ lines.push(row('CI failure rate', m.ci ? `${pct(m.ci.failed, m.ci.runs)} of ${m.ci.runs} runs of ci.yml` : 'no ci.yml runs'));
123
+ lines.push(row('Reopened issues', String(m.reopened)));
124
+ lines.push(row('Approval to pull request', m.approvalToPr != null ? `median ${hours(m.approvalToPr)}` : 'no approved spec got its pull request yet'));
125
+ const a = m.agents;
126
+ lines.push(row('The agents\' pull requests', a.total ? `${a.merged} of ${a.total} merged (${pct(a.merged, a.merged + a.closed)} of those decided), ${a.closed} closed without merging; ${a.rework} needed rework first (changes requested, or commits after the first review)` : 'none in this period'));
127
+ } else lines.push(row('Pull requests, reviews, CI, reopened issues, approval to pull request', needsGh));
128
+ const f = m.features;
129
+ lines.push(row('Features, as they stand', `${f.done} done, ${f.blocked} blocked, ${f['in progress']} in progress`));
130
+ const s = m.sessions;
131
+ lines.push(row('Unattended sessions (this machine)', s.count ? `${s.count}, ${round(s.tokens / 1e6)} M tokens${s.cost ? `, $${s.cost.toFixed(2)} API-equivalent` : ''}` : 'none'));
132
+ lines.push(row('Findings kept with amk-ok', String(m.kept.length)));
133
+ lines.push('', `Review comments and kept findings for \`m-learn\`: \`node scripts/amk/metrics.js --comments --since ${m.from}\`.`);
134
+ return lines.join('\n');
135
+ }
136
+
137
+ // the period's review comments (review texts and line comments) and amk-ok reasons, for m-learn
138
+ function comments(m, io = IO) {
139
+ if (!m.gh) return `${'gh isn\'t signed in here, or this isn\'t a GitHub repository'}: only the kept findings.\n${untrusted(m.kept.map(k => `${k.file}:${k.line} (${k.date}): ${k.why}`).join('\n') || 'none', 'the reasons kept with amk-ok')}`;
140
+ const items = [];
141
+ for (const p of m.prs) {
142
+ for (const r of p.reviews || []) if ((r.body || '').trim()) items.push(`#${p.number}, ${(r.author || {}).login || 'someone'} (review, ${String(r.state || '').toLowerCase()}): ${r.body.trim()}`);
143
+ for (const c of io.gh(['api', `repos/${m.repo}/pulls/${p.number}/comments?per_page=100`]) || []) {
144
+ items.push(`#${p.number} ${c.path}${c.line ? `:${c.line}` : ''}, ${(c.user || {}).login || 'someone'}: ${String(c.body || '').trim()}`);
145
+ }
146
+ }
147
+ return [untrusted(items.join('\n\n---\n\n') || 'none', `the review comments since ${m.from}`),
148
+ untrusted(m.kept.map(k => `${k.file}:${k.line} (${k.date}): ${k.why}`).join('\n') || 'none', 'the reasons kept with amk-ok')].join('\n');
149
+ }
150
+
151
+ module.exports = { collect, report, comments, sinceDate, median, IO };
152
+
153
+ if (require.main === module) {
154
+ const args = process.argv.slice(2);
155
+ const i = args.indexOf('--since');
156
+ const from = sinceDate(i >= 0 ? args[i + 1] : '30d', IO.now());
157
+ if (!from || args.some((a, k) => !['--since', '--comments'].includes(a) && args[k - 1] !== '--since')) {
158
+ console.error('usage: node scripts/amk/metrics.js [--comments] [--since <YYYY-MM-DD, or a span such as 30d>]');
159
+ process.exitCode = 2;
160
+ } else {
161
+ const m = collect(from);
162
+ console.log(args.includes('--comments') ? comments(m) : report(m, new Date(IO.now()).toISOString().slice(0, 10)));
163
+ }
164
+ }