ShtukbaseStandard
файл пакета→ .shtukbase/packages/runs/checks/r01_run.ts

checks/r01_run.ts

60 строк, 3.1 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // RUN-R01. A run is written whole: type Run, a start time, what ran (an action, an automation or an agent that
  2. // exists), an end time not before the start once it is over, and no run left running past `staleAfterHours`.
  3. import { main } from '../../collections/checks/lib/runner.ts';
  4. import { readPage } from '../../collections/checks/lib/pages.ts';
  5. import { type Project, type Rec, type Result, addr, count, filled, pageExists, phase, recordsOf, target, text, time } from '../../agents/lib/factlib.ts';
  6. import { join } from 'node:path';
  7. export const RULE = {
  8. id: 'RUN-R01', title: 'A run is recorded in full',
  9. scope: 'all runs of the log',
  10. };
  11. const WHAT = ['action', 'automation', 'agent'];
  12. const OVER = ['done', 'failed', 'cancelled'];
  13. /** Hours after which a running run is stale: `staleAfterHours` of the collection page, 24 if not given. */
  14. function staleHours(p: Project): number {
  15. const c = p.coll('RUN');
  16. if (!c) return 24;
  17. const fm = readPage(join(p.root, c.replace(/^\/+/, '') + '.md')).fm;
  18. return count(fm.staleAfterHours) ?? 24;
  19. }
  20. function what(p: Project, r: Rec, res: Result, at: string): void {
  21. const named = WHAT.filter((k) => filled(r.fm[k]));
  22. if (!named.length) {
  23. res.hole(at, 'it is not said what was run', 'write action, automation or agent', 'no-what');
  24. return;
  25. }
  26. for (const k of named) {
  27. const t = target(r, r.fm[k]);
  28. if (!t || !pageExists(p, t)) res.hole(at, `${k} points to ${t ?? text(r.fm[k])}, but there is no such page`, `fix the link ${k}`, 'missing-what');
  29. }
  30. }
  31. export function check(p: Project, res: Result, nowMs = Date.now()): void {
  32. const recs = recordsOf(p, 'RUN');
  33. res.scope = recs.map((r) => addr(r.id));
  34. res.scopeText = `runs ${p.coll('RUN') || '—'} (${recs.length})`;
  35. if (!res.part('runs', p.coll('RUN') ? null : 'no runs collection')) return;
  36. const stale = staleHours(p);
  37. for (const r of recs) {
  38. const at = addr(r.id);
  39. if (text(r.fm.type) !== 'Run') res.hole(at, `type "${text(r.fm.type) || '—'}", not Run`, 'set type: Run', 'bad-type');
  40. const st = phase(r.fm.status);
  41. if (!st) res.hole(at, 'no state (status)', 'write status: running, waiting, done, failed or cancelled', 'no-status');
  42. const start = time(r.fm.started);
  43. if (start === null) res.hole(at, `start "${text(r.fm.started) || '—'}" is not an ISO time`, 'write started: 2026-11-28T03:00:00Z', 'no-start');
  44. what(p, r, res, at);
  45. if (OVER.includes(st)) {
  46. const end = time(r.fm.ended);
  47. if (end === null) res.hole(at, `the run ended (${st}), but the end "${text(r.fm.ended) || '—'}" is missing`, 'write ended: an ISO time', 'no-end');
  48. else if (start !== null && end < start) res.hole(at, `the end ${text(r.fm.ended)} is before the start ${text(r.fm.started)}`, 'fix started or ended', 'end-before-start');
  49. }
  50. if (st === 'running' && start !== null && nowMs - start > stale * 3600_000)
  51. res.hole(at, `running for more than ${stale} h (since ${text(r.fm.started)})`, 'finish the run: status failed with message, or done with evidence', 'hung');
  52. }
  53. }
  54. if (import.meta.main) main({ RULE, check: (p, res) => check(p, res) });