ShtukbaseStandard
package file→ .shtukbase/packages/runs/actions/finish.ts

actions/finish.ts

34 lines, 2.0 KB.

  1. // `shtuk run runs finish <run page> --status done --evidence "…" [--evidence "…"] [--body "…"]`
  2. // `shtuk run runs finish <run page> --status failed|cancelled --message "why"`
  3. // `shtuk run runs finish <run page> --status waiting` (a gate waits for a person; ended is not written)
  4. // Writes the end of a run once: status, ended, evidence or message; keys it does not own stay as they are.
  5. // A run that already ended is refused.
  6. import { existsSync } from 'node:fs';
  7. import { join } from 'node:path';
  8. import { readPage } from '../../collections/checks/lib/pages.ts';
  9. import { args, now, update } from '../../agents/lib/write.ts';
  10. function fail(msg: string): never {
  11. console.error(msg);
  12. process.exit(2);
  13. }
  14. const a = args(process.argv.slice(2));
  15. const page = a._[0] ?? fail('the address of the run page is required: finish /runs/<name>.md --status …');
  16. const file = join(process.cwd(), page.replace(/^\/+/, '').replace(/(\.md)?$/, '.md'));
  17. if (!existsSync(file)) fail(`no page ${page}`);
  18. const status = typeof a.status === 'string' ? a.status : fail('--status is required: waiting, done, failed or cancelled');
  19. if (!['waiting', 'done', 'failed', 'cancelled'].includes(status)) fail('--status: waiting, done, failed or cancelled');
  20. const was = String(readPage(file).fm.status ?? '');
  21. if (['done', 'failed', 'cancelled'].includes(was)) fail(`the run has already ended (${was}); a repeat is a new run with --retryOf`);
  22. if (status === 'done' && a.evidence === undefined && !a.body) fail('a done run needs --evidence "…"');
  23. if ((status === 'failed' || status === 'cancelled') && typeof a.message !== 'string') fail(`${status}: --message "why" is required`);
  24. const evidence = a.evidence === undefined ? undefined : (Array.isArray(a.evidence) ? a.evidence : [a.evidence]).map(String);
  25. update(file, {
  26. status,
  27. ended: status === 'waiting' ? undefined : now(),
  28. evidence,
  29. message: typeof a.message === 'string' ? a.message : undefined,
  30. }, typeof a.body === 'string' ? a.body : undefined);
  31. console.log(page);