ShtukbaseStandard
файл пакета→ .shtukbase/packages/human-gate/hooks/ask.ts

hooks/ask.ts

40 строк, 2.5 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // HG-R01 (PreToolUse on Bash). A command that matches /base/human-gate.md runs only when a record of /work/approvals
  2. // names exactly this command, a person answered `answer: yes`, and the yes is not used yet. Then the hook writes
  3. // `usedOn` into the record and lets the command through: one yes, one run. Otherwise it blocks and tells the agent
  4. // how to ask. HG-R02 here too: a Bash command that writes into the approvals folder is blocked (the answer is the
  5. // person's).
  6. import { BLOCK, projectRoot, readPayload } from '../../hooks/lib/hooklib.ts';
  7. import { FOLDER, answer, approvals, empty, gated, markUsed, norm, today } from './lib.ts';
  8. const { p } = readPayload();
  9. if (!p || p.tool_name !== 'Bash') process.exit(0);
  10. const root = projectRoot(p);
  11. const cmd = norm(p.tool_input?.command);
  12. if (!cmd) process.exit(0);
  13. const WRITES = /((?<![0-9&-])>(?!&)|\btee\b|\bsed\s+-i|\bperl\s+-[a-z]*i|\b(mv|cp|rm|truncate|dd|python3?|node|bun|ruby|ed)\b)/;
  14. if (cmd.includes(FOLDER) && WRITES.test(cmd)) {
  15. process.stderr.write(`Not done (norm HG-R02): the command writes to ${FOLDER}, and the answers there are written only by a person. Create the request as a new record with the Write tool, without the fields answer, answeredOn and usedOn.\n`);
  16. process.exit(BLOCK);
  17. }
  18. const hit = gated(root).find((g) => g.pattern.test(cmd));
  19. if (!hit) process.exit(0);
  20. const same = approvals(root).filter((a) => norm(a.fm.command) === cmd);
  21. const yes = same.find((a) => answer(a) === 'yes' && empty(a.fm.usedOn));
  22. if (yes) {
  23. markUsed(yes, today());
  24. process.exit(0);
  25. }
  26. const no = same.find((a) => answer(a) === 'no');
  27. const waiting = same.find((a) => empty(a.fm.answer));
  28. let why: string;
  29. if (no) why = `a person answered "no" in ${no.addr}. Do not repeat it; if the circumstances have changed, create a new record with an explanation.`;
  30. else if (waiting) why = `the request ${waiting.addr} has no answer yet. Wait for the person's answer; do not fill in the answer field yourself.`;
  31. else if (same.length) why = `the consent in ${same[0].addr} is already used (usedOn). A new run needs a new record and a new "yes".`;
  32. else
  33. why =
  34. `create the record ${FOLDER}/<short-name>.md with a head: title (what and why, in one phrase), command (exactly this command: ${JSON.stringify(cmd)}), ` +
  35. `why (what will happen and whether it can be rolled back), askedOn (${today()}). Tell the person that you are waiting for an answer. They answer in the record: answer: yes or no.`;
  36. process.stderr.write(`Not done (norm HG-R01): "${hit.why}" — only after a person's "yes". Now: ${why}\n`);
  37. process.exit(BLOCK);