ShtukbaseStandard
包文件→ .shtukbase/packages/human-gate/hooks/answer-guard.ts

hooks/answer-guard.ts

23 行, 1.4 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // HG-R02 (PreToolUse on Write, Edit, MultiEdit). In a record of /work/approvals the agent may write the request
  2. // (title, command, why, askedOn) and nothing a person answers: answer, answeredOn and usedOn stay as they were.
  3. import { existsSync, readFileSync } from 'node:fs';
  4. import { join } from 'node:path';
  5. import { parseYaml, splitFrontMatter } from '../../collections/checks/lib/pages.ts';
  6. import { BLOCK, contentAfter, projectPath, projectRoot, readPayload } from '../../hooks/lib/hooklib.ts';
  7. import { FOLDER, PERSON_FIELDS, norm } from './lib.ts';
  8. const { p } = readPayload();
  9. if (!p) process.exit(0);
  10. const root = projectRoot(p);
  11. const rel = projectPath(root, p.tool_input?.file_path);
  12. if (!rel || !rel.startsWith(FOLDER + '/')) process.exit(0);
  13. const after = contentAfter(root, String(p.tool_name ?? ''), p.tool_input ?? {});
  14. if (after === null) process.exit(0);
  15. const fmOf = (t: string) => parseYaml(splitFrontMatter(t)[0]);
  16. const before = existsSync(join(root, rel)) ? fmOf(readFileSync(join(root, rel), 'utf8')) : {};
  17. const now = fmOf(after);
  18. const changed = PERSON_FIELDS.filter((f) => norm(before[f]) !== norm(now[f]));
  19. if (changed.length) {
  20. process.stderr.write(`Not written (norm HG-R02): the fields ${changed.join(', ')} in ${rel} are written only by a person. Leave them as they were; a request is title, command, why and askedOn.\n`);
  21. process.exit(BLOCK);
  22. }