ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/schedule/checks/s01_schedule.ts

checks/s01_schedule.ts

61 líneas, 3.8 KB.

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

  1. // SHD-R01. An automation on a schedule can run unattended: its schedule reads (cron, every or at), it has a limit
  2. // (perHour or perDay), and its reaction leads to something the tick can launch: an agent whose harness package is
  3. // installed and a prompt, or an action that is a program with its script or command, or an agent action.
  4. import { main } from '../../collections/checks/lib/runner.ts';
  5. import { readPage } from '../../collections/checks/lib/pages.ts';
  6. import { type Project, type Result, addr, count, findFile, map, normPath, pageExists, recordsOf, text } from '../../agents/lib/factlib.ts';
  7. import { harnessOf, harnessPkg } from '../../harness/lib/harness.ts';
  8. import { join } from 'node:path';
  9. import { schedOf } from '../lib/schedule.ts';
  10. export const RULE = {
  11. id: 'SHD-R01', title: 'A scheduled automation can run without a person',
  12. scope: 'automations with trigger.kind: schedule or clock',
  13. };
  14. function agentOk(p: Project, res: Result, at: string, agent: string | null): void {
  15. if (!agent || !pageExists(p, agent)) return res.hole(at, `agent ${agent ?? '—'} does not exist`, 'fix the link to the agent', 'missing-agent');
  16. const h = harnessOf(readPage(join(p.root, agent)).fm);
  17. if (!h) return res.hole(at, `agent ${agent} has no harness (runtime) named`, 'write runtime: claude | codex | opencode | dsh | hermes', 'no-harness');
  18. const hp = harnessPkg(p.root, h);
  19. if (!hp.installed) res.hole(at, `agent ${agent} is on the harness "${h}", but the package ${hp.pkg} is not installed`, `shtuk install …/${hp.pkg}`, 'no-harness');
  20. }
  21. export function check(p: Project, res: Result): void {
  22. const recs = recordsOf(p, 'AUTOMATION').filter((r) => schedOf(r.fm.trigger) !== null);
  23. res.scope = recs.map((r) => addr(r.id));
  24. res.scopeText = `scheduled automations (${recs.length})`;
  25. if (!res.part('automations', p.coll('AUTOMATION') ? null : 'no automations collection')) return;
  26. for (const r of recs) {
  27. const at = addr(r.id);
  28. const s = schedOf(r.fm.trigger);
  29. if (typeof s === 'string') res.hole(at, `the schedule cannot be read: ${s}`, 'trigger: {kind: schedule, cron: "0 7 * * *"} or every: 6h, or at: "07:00"', 'bad-schedule');
  30. const l = map(r.fm.limits);
  31. if (!l || (count(l.perHour) === null && count(l.perDay) === null))
  32. res.hole(at, 'the schedule has no limit', 'write limits: {perHour: 1, perDay: 4}', 'no-limit');
  33. const re = map(r.fm.reaction);
  34. if (!re) {
  35. res.hole(at, 'no response (reaction)', 'write reaction: {agent: /agents/<name>, prompt: "…"} or {action: /actions/<name>}', 'no-reaction');
  36. continue;
  37. }
  38. if (text(re.agent)) {
  39. agentOk(p, res, at, normPath(re.agent, null, r.id));
  40. if (!text(re.prompt) && !r.body.trim()) res.hole(at, 'the agent has nothing to be told: no reaction.prompt and no page body', 'write reaction.prompt', 'no-prompt');
  41. continue;
  42. }
  43. const act = normPath(re.action, null, r.id);
  44. if (!act || !pageExists(p, act)) {
  45. res.hole(at, `action ${act ?? '—'} does not exist`, 'fix reaction.action', 'missing-action');
  46. continue;
  47. }
  48. const fm = readPage(join(p.root, act)).fm;
  49. const kind = text(fm.kind);
  50. if (['program', 'cloud'].includes(kind)) {
  51. if (text(fm.path) && !findFile(p, text(fm.path))) res.hole(at, `script ${text(fm.path)} of action ${act} does not exist`, 'fix the path of the action', 'missing-script');
  52. else if (!text(fm.path) && !text(fm.command)) res.hole(at, `action ${act} has neither path nor command`, 'write path or command', 'missing-script');
  53. } else if (['agent', 'assistant', 'specialist', 'harness'].includes(kind)) agentOk(p, res, at, normPath(fm.agent, null, act));
  54. else res.hole(at, `action ${act} of kind "${kind || '—'}" is not run by the tick`, 'take an action of kind program or agent', 'bad-action');
  55. }
  56. }
  57. if (import.meta.main) main({ RULE, check });