包文件→ .shtukbase/packages/schedule/checks/s01_schedule.ts
checks/s01_schedule.ts
61 行, 3.8 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // SHD-R01. An automation on a schedule can run unattended: its schedule reads (cron, every or at), it has a limit
- // (perHour or perDay), and its reaction leads to something the tick can launch: an agent whose harness package is
- // installed and a prompt, or an action that is a program with its script or command, or an agent action.
- import { main } from '../../collections/checks/lib/runner.ts';
- import { readPage } from '../../collections/checks/lib/pages.ts';
- import { type Project, type Result, addr, count, findFile, map, normPath, pageExists, recordsOf, text } from '../../agents/lib/factlib.ts';
- import { harnessOf, harnessPkg } from '../../harness/lib/harness.ts';
- import { join } from 'node:path';
- import { schedOf } from '../lib/schedule.ts';
- export const RULE = {
- id: 'SHD-R01', title: 'A scheduled automation can run without a person',
- scope: 'automations with trigger.kind: schedule or clock',
- };
- function agentOk(p: Project, res: Result, at: string, agent: string | null): void {
- if (!agent || !pageExists(p, agent)) return res.hole(at, `agent ${agent ?? '—'} does not exist`, 'fix the link to the agent', 'missing-agent');
- const h = harnessOf(readPage(join(p.root, agent)).fm);
- if (!h) return res.hole(at, `agent ${agent} has no harness (runtime) named`, 'write runtime: claude | codex | opencode | dsh | hermes', 'no-harness');
- const hp = harnessPkg(p.root, h);
- 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');
- }
- export function check(p: Project, res: Result): void {
- const recs = recordsOf(p, 'AUTOMATION').filter((r) => schedOf(r.fm.trigger) !== null);
- res.scope = recs.map((r) => addr(r.id));
- res.scopeText = `scheduled automations (${recs.length})`;
- if (!res.part('automations', p.coll('AUTOMATION') ? null : 'no automations collection')) return;
- for (const r of recs) {
- const at = addr(r.id);
- const s = schedOf(r.fm.trigger);
- 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');
- const l = map(r.fm.limits);
- if (!l || (count(l.perHour) === null && count(l.perDay) === null))
- res.hole(at, 'the schedule has no limit', 'write limits: {perHour: 1, perDay: 4}', 'no-limit');
- const re = map(r.fm.reaction);
- if (!re) {
- res.hole(at, 'no response (reaction)', 'write reaction: {agent: /agents/<name>, prompt: "…"} or {action: /actions/<name>}', 'no-reaction');
- continue;
- }
- if (text(re.agent)) {
- agentOk(p, res, at, normPath(re.agent, null, r.id));
- 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');
- continue;
- }
- const act = normPath(re.action, null, r.id);
- if (!act || !pageExists(p, act)) {
- res.hole(at, `action ${act ?? '—'} does not exist`, 'fix reaction.action', 'missing-action');
- continue;
- }
- const fm = readPage(join(p.root, act)).fm;
- const kind = text(fm.kind);
- if (['program', 'cloud'].includes(kind)) {
- 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');
- else if (!text(fm.path) && !text(fm.command)) res.hole(at, `action ${act} has neither path nor command`, 'write path or command', 'missing-script');
- } else if (['agent', 'assistant', 'specialist', 'harness'].includes(kind)) agentOk(p, res, at, normPath(fm.agent, null, act));
- 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');
- }
- }
- if (import.meta.main) main({ RULE, check });