archivo del paquete→ .shtukbase/packages/human-gate/hooks/ask.ts
hooks/ask.ts
40 líneas, 2.5 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // HG-R01 (PreToolUse on Bash). A command that matches /base/human-gate.md runs only when a record of /work/approvals
- // names exactly this command, a person answered `answer: yes`, and the yes is not used yet. Then the hook writes
- // `usedOn` into the record and lets the command through: one yes, one run. Otherwise it blocks and tells the agent
- // how to ask. HG-R02 here too: a Bash command that writes into the approvals folder is blocked (the answer is the
- // person's).
- import { BLOCK, projectRoot, readPayload } from '../../hooks/lib/hooklib.ts';
- import { FOLDER, answer, approvals, empty, gated, markUsed, norm, today } from './lib.ts';
- const { p } = readPayload();
- if (!p || p.tool_name !== 'Bash') process.exit(0);
- const root = projectRoot(p);
- const cmd = norm(p.tool_input?.command);
- if (!cmd) process.exit(0);
- 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)/;
- if (cmd.includes(FOLDER) && WRITES.test(cmd)) {
- 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`);
- process.exit(BLOCK);
- }
- const hit = gated(root).find((g) => g.pattern.test(cmd));
- if (!hit) process.exit(0);
- const same = approvals(root).filter((a) => norm(a.fm.command) === cmd);
- const yes = same.find((a) => answer(a) === 'yes' && empty(a.fm.usedOn));
- if (yes) {
- markUsed(yes, today());
- process.exit(0);
- }
- const no = same.find((a) => answer(a) === 'no');
- const waiting = same.find((a) => empty(a.fm.answer));
- let why: string;
- 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.`;
- 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.`;
- 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".`;
- else
- why =
- `create the record ${FOLDER}/<short-name>.md with a head: title (what and why, in one phrase), command (exactly this command: ${JSON.stringify(cmd)}), ` +
- `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.`;
- process.stderr.write(`Not done (norm HG-R01): "${hit.why}" — only after a person's "yes". Now: ${why}\n`);
- process.exit(BLOCK);