ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/content/hooks/approve-guard.ts

hooks/approve-guard.ts

31 líneas, 1.9 KB.

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

  1. // CNT-R01 (PreToolUse on Write, Edit, MultiEdit). In /content/drafts and /content/ideas an agent may write the text
  2. // and the editor's fields, but not what a person decides: the states approved and published, and the fields
  3. // approvedBy, approvedOn, publishedOn, url stay as they were.
  4. import { existsSync, readFileSync } from 'node:fs';
  5. import { join } from 'node:path';
  6. import { parseYaml, pystr, splitFrontMatter } from '../../collections/checks/lib/pages.ts';
  7. import { BLOCK, contentAfter, projectPath, projectRoot, readPayload } from '../../hooks/lib/hooklib.ts';
  8. const FOLDERS = ['content/drafts/', 'content/ideas/'];
  9. const PERSON_FIELDS = ['approvedBy', 'approvedOn', 'publishedOn', 'url'];
  10. const PERSON_STATES = ['approved', 'published'];
  11. const { p } = readPayload();
  12. if (!p) process.exit(0);
  13. const root = projectRoot(p);
  14. const rel = projectPath(root, p.tool_input?.file_path);
  15. if (!rel || !FOLDERS.some((f) => rel.startsWith(f))) process.exit(0);
  16. const after = contentAfter(root, String(p.tool_name ?? ''), p.tool_input ?? {});
  17. if (after === null) process.exit(0);
  18. const fmOf = (t: string) => parseYaml(splitFrontMatter(t)[0]);
  19. const v = (x: unknown) => pystr(x ?? '').trim();
  20. const st = (x: unknown) => v(x).replace(/^\/+|\/+$/g, '').split('/').pop()!.toLowerCase();
  21. const before = existsSync(join(root, rel)) ? fmOf(readFileSync(join(root, rel), 'utf8')) : {};
  22. const now = fmOf(after);
  23. const changed = PERSON_FIELDS.filter((f) => v(before[f]) !== v(now[f]));
  24. const state = st(now.status);
  25. if (PERSON_STATES.includes(state) && st(before.status) !== state) changed.push(`status: ${state}`);
  26. if (changed.length) {
  27. process.stderr.write(`Not written (norm CNT-R01): ${changed.join(', ')} in ${rel} is set only by a person. Leave it as it was; the text and the editor fields (verdict, editorNotes) may be written, an agent sets only the states draft and review on a draft and drafted on an idea.\n`);
  28. process.exit(BLOCK);
  29. }