package file→ .shtukbase/packages/blueprint-software-factory/actions/review.ts
actions/review.ts
93 lines, 5.2 KB.
- // `shtuk run blueprint-software-factory review [--dry-run] [--max 20]` (the action /actions/sf-review-commits of the
- // review automation): the commits of the main branch since the last review go to the reviewer agent. The prompt is
- // the code norms of the installed code-* packages (`code-lib review` over the changed code files) plus the diff of
- // those files; the agent's answer becomes a record in /work/reviews. Code, branches and tasks are not touched.
- // No new commits: nothing runs. No code in the new commits: a record with verdict no-code, no agent.
- import { mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
- import { tmpdir } from 'node:os';
- import { join } from 'node:path';
- import { readPage } from '../../collections/checks/lib/pages.ts';
- import { args, create } from '../../agents/lib/write.ts';
- import { factory, git, pkgAction, str } from '../lib/factory.ts';
- const root = process.cwd();
- const a = args(process.argv.slice(2));
- const dry = a['dry-run'] === true;
- const max = Number(a.max ?? 20) || 20;
- const FOLDER = '/work/reviews';
- const AGENT = '/agents/sf-reviewer.md';
- const EMPTY_TREE = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
- const CODE = /\.(ts|tsx|js|jsx|mjs|cjs|py|rs|go|java|kt|rb|php|c|cc|cpp|h|hpp|cs|swift|sh)$/;
- const NOT_CODE = [/^\.shtukbase\//, /^\.claude\//, /^runs\//, /^alarms\//];
- const today = new Date().toISOString().slice(0, 10);
- const branch = str(factory(root).branch) || 'main';
- const head = git(['rev-parse', '--verify', `${branch}^{commit}`], root) ?? git(['rev-parse', '--verify', 'HEAD^{commit}'], root);
- if (!head) {
- console.log(`review: no commits (the branch ${branch} is not found or this is not a git repository) — nothing to do`);
- process.exit(0);
- }
- // the last reviewed head: the newest record of /work/reviews (by file name, then by `reviewed`)
- let last = '';
- try {
- const recs = readdirSync(join(root, FOLDER.slice(1))).filter((n) => n.endsWith('.md')).sort();
- for (const n of recs) {
- const h = str(readPage(join(root, FOLDER.slice(1), n)).fm.head);
- if (h) last = h;
- }
- } catch {}
- if (last && git(['merge-base', '--is-ancestor', last, head], root) === null) last = ''; // history rewritten: start over
- if (last === head) {
- console.log(`review: no new commits on ${branch} (the last review is ${head.slice(0, 8)})`);
- process.exit(0);
- }
- const commits = (git(['rev-list', `--max-count=${max}`, last ? `${last}..${head}` : head], root) ?? '').split('\n').filter(Boolean);
- const oldest = commits[commits.length - 1];
- const base = last || git(['rev-parse', '--verify', `${oldest}^`], root) || EMPTY_TREE;
- const range = `${base}..${head}`;
- const files = (git(['diff', '--name-only', base, head], root) ?? '').split('\n').filter((f) => f && CODE.test(f) && !NOT_CODE.some((r) => r.test(f)));
- const short = (s: string) => (s === EMPTY_TREE ? 'start' : s.slice(0, 8));
- const title = `Review ${short(base)}..${short(head)}`;
- const fields = (verdict: string, run?: string) => ({
- type: 'Review', title, head, base: base === EMPTY_TREE ? '' : base, commits: commits.length, verdict, ...(run ? { run } : {}), reviewed: today,
- });
- const log = git(['log', '--format=%h %s', range], root) ?? '';
- console.log(`review ${range}: commits ${commits.length}, code files ${files.length}`);
- if (!files.length) {
- if (dry) process.exit(0);
- 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}`);
- console.log(`written: ${p}`);
- process.exit(0);
- }
- // the prompt: norms of the code packages over the changed files, then the diff of those files
- const norms = pkgAction('code-lib', 'actions/review.ts', files, root);
- const diff = (git(['diff', '--unified=5', base, head, '--', ...files], root) ?? '').slice(0, 40_000);
- const prompt = [
- `Review of the commits of the branch ${branch}: ${range}.`, '', 'Commits:', log, '',
- norms.stdout?.trim() || '(the code norms are not built)', '', '## The change (diff)', '', '```diff', diff, '```', '',
- 'Answer with text; do not edit files. The last line is exactly "Verdict: accept" or "Verdict: rework".',
- ].join('\n');
- if (dry) {
- console.log(`dry run: a prompt of ${prompt.length} characters for the agent ${AGENT}; nothing was run`);
- process.exit(0);
- }
- const dir = mkdtempSync(join(tmpdir(), 'sf-review-'));
- const file = join(dir, 'prompt.md');
- writeFileSync(file, prompt);
- const r = pkgAction('harness', 'actions/ask.ts', ['--agent', AGENT, '--prompt-file', file, '--action', '/actions/sf-review-commits', '--title', title], root);
- rmSync(dir, { recursive: true, force: true });
- const out = (r.stdout ?? '').trim().split('\n');
- const run = /^run: (\S+)/.exec(out[0] ?? '')?.[1];
- const answer = out.slice(1).join('\n').trim();
- if (r.status !== 0 || !answer) {
- console.log(out.join('\n'));
- console.error(`the reviewer did not answer (code ${r.status}): ${(r.stderr ?? '').trim().split('\n').pop() ?? ''}`);
- process.exit(1);
- }
- const lastLine = answer.split('\n').filter(Boolean).pop() ?? '';
- const verdict = /rework/i.test(lastLine) ? 'rework' : /accept/i.test(lastLine) ? 'accept' : 'unclear';
- 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')}`);
- console.log(`reviewer: ${run ?? '—'}; verdict: ${verdict}; written: ${p}`);