ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/blueprint-software-factory/actions/review.ts

actions/review.ts

93 líneas, 5.2 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // `shtuk run blueprint-software-factory review [--dry-run] [--max 20]` (the action /actions/sf-review-commits of the
  2. // review automation): the commits of the main branch since the last review go to the reviewer agent. The prompt is
  3. // the code norms of the installed code-* packages (`code-lib review` over the changed code files) plus the diff of
  4. // those files; the agent's answer becomes a record in /work/reviews. Code, branches and tasks are not touched.
  5. // No new commits: nothing runs. No code in the new commits: a record with verdict no-code, no agent.
  6. import { mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
  7. import { tmpdir } from 'node:os';
  8. import { join } from 'node:path';
  9. import { readPage } from '../../collections/checks/lib/pages.ts';
  10. import { args, create } from '../../agents/lib/write.ts';
  11. import { factory, git, pkgAction, str } from '../lib/factory.ts';
  12. const root = process.cwd();
  13. const a = args(process.argv.slice(2));
  14. const dry = a['dry-run'] === true;
  15. const max = Number(a.max ?? 20) || 20;
  16. const FOLDER = '/work/reviews';
  17. const AGENT = '/agents/sf-reviewer.md';
  18. const EMPTY_TREE = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
  19. const CODE = /\.(ts|tsx|js|jsx|mjs|cjs|py|rs|go|java|kt|rb|php|c|cc|cpp|h|hpp|cs|swift|sh)$/;
  20. const NOT_CODE = [/^\.shtukbase\//, /^\.claude\//, /^runs\//, /^alarms\//];
  21. const today = new Date().toISOString().slice(0, 10);
  22. const branch = str(factory(root).branch) || 'main';
  23. const head = git(['rev-parse', '--verify', `${branch}^{commit}`], root) ?? git(['rev-parse', '--verify', 'HEAD^{commit}'], root);
  24. if (!head) {
  25. console.log(`review: no commits (the branch ${branch} is not found or this is not a git repository) — nothing to do`);
  26. process.exit(0);
  27. }
  28. // the last reviewed head: the newest record of /work/reviews (by file name, then by `reviewed`)
  29. let last = '';
  30. try {
  31. const recs = readdirSync(join(root, FOLDER.slice(1))).filter((n) => n.endsWith('.md')).sort();
  32. for (const n of recs) {
  33. const h = str(readPage(join(root, FOLDER.slice(1), n)).fm.head);
  34. if (h) last = h;
  35. }
  36. } catch {}
  37. if (last && git(['merge-base', '--is-ancestor', last, head], root) === null) last = ''; // history rewritten: start over
  38. if (last === head) {
  39. console.log(`review: no new commits on ${branch} (the last review is ${head.slice(0, 8)})`);
  40. process.exit(0);
  41. }
  42. const commits = (git(['rev-list', `--max-count=${max}`, last ? `${last}..${head}` : head], root) ?? '').split('\n').filter(Boolean);
  43. const oldest = commits[commits.length - 1];
  44. const base = last || git(['rev-parse', '--verify', `${oldest}^`], root) || EMPTY_TREE;
  45. const range = `${base}..${head}`;
  46. const files = (git(['diff', '--name-only', base, head], root) ?? '').split('\n').filter((f) => f && CODE.test(f) && !NOT_CODE.some((r) => r.test(f)));
  47. const short = (s: string) => (s === EMPTY_TREE ? 'start' : s.slice(0, 8));
  48. const title = `Review ${short(base)}..${short(head)}`;
  49. const fields = (verdict: string, run?: string) => ({
  50. type: 'Review', title, head, base: base === EMPTY_TREE ? '' : base, commits: commits.length, verdict, ...(run ? { run } : {}), reviewed: today,
  51. });
  52. const log = git(['log', '--format=%h %s', range], root) ?? '';
  53. console.log(`review ${range}: commits ${commits.length}, code files ${files.length}`);
  54. if (!files.length) {
  55. if (dry) process.exit(0);
  56. const p = create(root, FOLDER, `${today}-${head.slice(0, 8)}`, fields('no-code'), `There is no code in the changes: the agent was not run.\n\n${log}`);
  57. console.log(`written: ${p}`);
  58. process.exit(0);
  59. }
  60. // the prompt: norms of the code packages over the changed files, then the diff of those files
  61. const norms = pkgAction('code-lib', 'actions/review.ts', files, root);
  62. const diff = (git(['diff', '--unified=5', base, head, '--', ...files], root) ?? '').slice(0, 40_000);
  63. const prompt = [
  64. `Review of the commits of the branch ${branch}: ${range}.`, '', 'Commits:', log, '',
  65. norms.stdout?.trim() || '(the code norms are not built)', '', '## The change (diff)', '', '```diff', diff, '```', '',
  66. 'Answer with text; do not edit files. The last line is exactly "Verdict: accept" or "Verdict: rework".',
  67. ].join('\n');
  68. if (dry) {
  69. console.log(`dry run: a prompt of ${prompt.length} characters for the agent ${AGENT}; nothing was run`);
  70. process.exit(0);
  71. }
  72. const dir = mkdtempSync(join(tmpdir(), 'sf-review-'));
  73. const file = join(dir, 'prompt.md');
  74. writeFileSync(file, prompt);
  75. const r = pkgAction('harness', 'actions/ask.ts', ['--agent', AGENT, '--prompt-file', file, '--action', '/actions/sf-review-commits', '--title', title], root);
  76. rmSync(dir, { recursive: true, force: true });
  77. const out = (r.stdout ?? '').trim().split('\n');
  78. const run = /^run: (\S+)/.exec(out[0] ?? '')?.[1];
  79. const answer = out.slice(1).join('\n').trim();
  80. if (r.status !== 0 || !answer) {
  81. console.log(out.join('\n'));
  82. console.error(`the reviewer did not answer (code ${r.status}): ${(r.stderr ?? '').trim().split('\n').pop() ?? ''}`);
  83. process.exit(1);
  84. }
  85. const lastLine = answer.split('\n').filter(Boolean).pop() ?? '';
  86. const verdict = /rework/i.test(lastLine) ? 'rework' : /accept/i.test(lastLine) ? 'accept' : 'unclear';
  87. const p = create(root, FOLDER, `${today}-${head.slice(0, 8)}`, fields(verdict, run), `${answer}\n\nCommits:\n\n${log.split('\n').map((l) => `- ${l}`).join('\n')}`);
  88. console.log(`reviewer: ${run ?? '—'}; verdict: ${verdict}; written: ${p}`);