ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/runs/checks/r02_evidence.ts

checks/r02_evidence.ts

44 líneas, 2.2 KB.

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

  1. // RUN-R02. The end of a run is explained: a done run carries evidence (the `evidence` field, an answer the run gave,
  2. // or a result written in the body), and every evidence item that is a path leads to something that exists; a failed
  3. // or cancelled run says why in `message`.
  4. import { main } from '../../collections/checks/lib/runner.ts';
  5. import { type Project, type Rec, type Result, addr, filled, findFile, map, pageExists, phase, recordsOf, text } from '../../agents/lib/factlib.ts';
  6. export const RULE = {
  7. id: 'RUN-R02', title: 'The end of a run is explained',
  8. scope: 'finished runs: done, failed, cancelled',
  9. };
  10. /** The body without headings and blank lines: what the run wrote as its result or transcript. */
  11. function bodyText(r: Rec): string {
  12. return r.body.split('\n').filter((l) => l.trim() && !/^\s{0,3}#/.test(l)).join('\n').trim();
  13. }
  14. /** Evidence items that look like paths: start with `/` or `./`, no spaces. */
  15. function paths(v: unknown): string[] {
  16. const items = Array.isArray(v) ? v : [v];
  17. return items.map((x) => text(x)).filter((s) => /^\.?\/\S+$/.test(s));
  18. }
  19. export function check(p: Project, res: Result): void {
  20. const recs = recordsOf(p, 'RUN').filter((r) => ['done', 'failed', 'cancelled'].includes(phase(r.fm.status)));
  21. res.scope = recs.map((r) => addr(r.id));
  22. res.scopeText = `finished runs ${p.coll('RUN') || '—'} (${recs.length})`;
  23. if (!res.part('runs', p.coll('RUN') ? null : 'no runs collection')) return;
  24. for (const r of recs) {
  25. const at = addr(r.id);
  26. const st = phase(r.fm.status);
  27. if (st === 'done') {
  28. const said = map(r.fm.answer)?.said;
  29. if (!filled(r.fm.evidence) && !filled(said) && !bodyText(r))
  30. res.hole(at, 'the run is done, but there is no evidence', 'write evidence: a commit, a path, a link or an output line', 'no-evidence');
  31. for (const s of paths(r.fm.evidence))
  32. if (!pageExists(p, s.replace(/^\.\//, '/')) && !findFile(p, s))
  33. res.hole(at, `evidence ${s} is not found`, 'fix the path in evidence', 'missing-evidence');
  34. } else if (!filled(r.fm.message)) {
  35. res.hole(at, `the run ${st === 'failed' ? 'failed' : 'was cancelled'}, but there is no reason`, 'write message: why', 'no-message');
  36. }
  37. }
  38. }
  39. if (import.meta.main) main({ RULE, check });