файл пакета→ .shtukbase/packages/runs/checks/r01_run.ts
checks/r01_run.ts
60 строк, 3.1 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // RUN-R01. A run is written whole: type Run, a start time, what ran (an action, an automation or an agent that
- // exists), an end time not before the start once it is over, and no run left running past `staleAfterHours`.
- import { main } from '../../collections/checks/lib/runner.ts';
- import { readPage } from '../../collections/checks/lib/pages.ts';
- import { type Project, type Rec, type Result, addr, count, filled, pageExists, phase, recordsOf, target, text, time } from '../../agents/lib/factlib.ts';
- import { join } from 'node:path';
- export const RULE = {
- id: 'RUN-R01', title: 'A run is recorded in full',
- scope: 'all runs of the log',
- };
- const WHAT = ['action', 'automation', 'agent'];
- const OVER = ['done', 'failed', 'cancelled'];
- /** Hours after which a running run is stale: `staleAfterHours` of the collection page, 24 if not given. */
- function staleHours(p: Project): number {
- const c = p.coll('RUN');
- if (!c) return 24;
- const fm = readPage(join(p.root, c.replace(/^\/+/, '') + '.md')).fm;
- return count(fm.staleAfterHours) ?? 24;
- }
- function what(p: Project, r: Rec, res: Result, at: string): void {
- const named = WHAT.filter((k) => filled(r.fm[k]));
- if (!named.length) {
- res.hole(at, 'it is not said what was run', 'write action, automation or agent', 'no-what');
- return;
- }
- for (const k of named) {
- const t = target(r, r.fm[k]);
- 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');
- }
- }
- export function check(p: Project, res: Result, nowMs = Date.now()): void {
- const recs = recordsOf(p, 'RUN');
- res.scope = recs.map((r) => addr(r.id));
- res.scopeText = `runs ${p.coll('RUN') || '—'} (${recs.length})`;
- if (!res.part('runs', p.coll('RUN') ? null : 'no runs collection')) return;
- const stale = staleHours(p);
- for (const r of recs) {
- const at = addr(r.id);
- if (text(r.fm.type) !== 'Run') res.hole(at, `type "${text(r.fm.type) || '—'}", not Run`, 'set type: Run', 'bad-type');
- const st = phase(r.fm.status);
- if (!st) res.hole(at, 'no state (status)', 'write status: running, waiting, done, failed or cancelled', 'no-status');
- const start = time(r.fm.started);
- 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');
- what(p, r, res, at);
- if (OVER.includes(st)) {
- const end = time(r.fm.ended);
- 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');
- 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');
- }
- if (st === 'running' && start !== null && nowMs - start > stale * 3600_000)
- 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');
- }
- }
- if (import.meta.main) main({ RULE, check: (p, res) => check(p, res) });