包文件→ .shtukbase/packages/runs/actions/finish.ts
actions/finish.ts
34 行, 2.0 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // `shtuk run runs finish <run page> --status done --evidence "…" [--evidence "…"] [--body "…"]`
- // `shtuk run runs finish <run page> --status failed|cancelled --message "why"`
- // `shtuk run runs finish <run page> --status waiting` (a gate waits for a person; ended is not written)
- // Writes the end of a run once: status, ended, evidence or message; keys it does not own stay as they are.
- // A run that already ended is refused.
- import { existsSync } from 'node:fs';
- import { join } from 'node:path';
- import { readPage } from '../../collections/checks/lib/pages.ts';
- import { args, now, update } from '../../agents/lib/write.ts';
- function fail(msg: string): never {
- console.error(msg);
- process.exit(2);
- }
- const a = args(process.argv.slice(2));
- const page = a._[0] ?? fail('the address of the run page is required: finish /runs/<name>.md --status …');
- const file = join(process.cwd(), page.replace(/^\/+/, '').replace(/(\.md)?$/, '.md'));
- if (!existsSync(file)) fail(`no page ${page}`);
- const status = typeof a.status === 'string' ? a.status : fail('--status is required: waiting, done, failed or cancelled');
- if (!['waiting', 'done', 'failed', 'cancelled'].includes(status)) fail('--status: waiting, done, failed or cancelled');
- const was = String(readPage(file).fm.status ?? '');
- if (['done', 'failed', 'cancelled'].includes(was)) fail(`the run has already ended (${was}); a repeat is a new run with --retryOf`);
- if (status === 'done' && a.evidence === undefined && !a.body) fail('a done run needs --evidence "…"');
- if ((status === 'failed' || status === 'cancelled') && typeof a.message !== 'string') fail(`${status}: --message "why" is required`);
- const evidence = a.evidence === undefined ? undefined : (Array.isArray(a.evidence) ? a.evidence : [a.evidence]).map(String);
- update(file, {
- status,
- ended: status === 'waiting' ? undefined : now(),
- evidence,
- message: typeof a.message === 'string' ? a.message : undefined,
- }, typeof a.body === 'string' ? a.body : undefined);
- console.log(page);