ShtukbaseStandard
包文件→ .shtukbase/packages/alarms/checks/l01_alarm.ts

checks/l01_alarm.ts

36 行, 2.2 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // ALR-R01. An alarm says what fired and how it ended: type Alarm, a watch name, a time it was found; a closed or
  2. // dismissed alarm names who or what closed it and when, not before it was found.
  3. import { main } from '../../collections/checks/lib/runner.ts';
  4. import { type Project, type Result, addr, has, pageExists, phase, recordsOf, target, text, time } from '../../agents/lib/factlib.ts';
  5. export const RULE = {
  6. id: 'ALR-R01', title: 'An alarm says what fired and how it ended',
  7. scope: 'all alarms',
  8. };
  9. export function check(p: Project, res: Result): void {
  10. const recs = recordsOf(p, 'ALARM');
  11. res.scope = recs.map((r) => addr(r.id));
  12. res.scopeText = `alarms ${p.coll('ALARM') || '—'} (${recs.length})`;
  13. if (!res.part('alarms', p.coll('ALARM') ? null : 'no alarms collection')) return;
  14. for (const r of recs) {
  15. const at = addr(r.id);
  16. if (text(r.fm.type) !== 'Alarm') res.hole(at, `type "${text(r.fm.type) || '—'}", not Alarm`, 'set type: Alarm', 'bad-type');
  17. if (!has(r, 'watch')) res.hole(at, 'no watcher named (watch)', 'write watch: a short name of the watcher, for example disk-low', 'no-watch');
  18. const found = time(r.fm.foundAt);
  19. if (found === null) res.hole(at, `time "${text(r.fm.foundAt) || '—'}" is not an ISO time`, 'write foundAt: 2026-11-28T03:00:00Z', 'no-found');
  20. if (has(r, 'foundBy')) {
  21. const t = target(r, r.fm.foundBy);
  22. if (!t || !pageExists(p, t)) res.hole(at, `foundBy points to ${t ?? text(r.fm.foundBy)}, but there is no such page`, 'fix the foundBy link', 'missing-finder');
  23. }
  24. const st = phase(r.fm.status);
  25. if (st === 'closed' || st === 'dismissed') {
  26. if (!has(r, 'closedBy')) res.hole(at, `the alarm is ${st === 'closed' ? 'closed' : 'dismissed'}, but by whom is not said`, 'write closedBy: who or what, and why', 'no-closer');
  27. const closed = time(r.fm.closedAt);
  28. if (closed === null) res.hole(at, `no closing time "${text(r.fm.closedAt) || '—'}"`, 'write closedAt: an ISO time', 'no-closed-at');
  29. else if (found !== null && closed < found) res.hole(at, `closed (${text(r.fm.closedAt)}) before it was found (${text(r.fm.foundAt)})`, 'fix foundAt or closedAt', 'closed-before-found');
  30. }
  31. }
  32. }
  33. if (import.meta.main) main({ RULE, check });