包文件→ .shtukbase/packages/human-gate/hooks/answer-guard.ts
hooks/answer-guard.ts
23 行, 1.4 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // HG-R02 (PreToolUse on Write, Edit, MultiEdit). In a record of /work/approvals the agent may write the request
- // (title, command, why, askedOn) and nothing a person answers: answer, answeredOn and usedOn stay as they were.
- import { existsSync, readFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { parseYaml, splitFrontMatter } from '../../collections/checks/lib/pages.ts';
- import { BLOCK, contentAfter, projectPath, projectRoot, readPayload } from '../../hooks/lib/hooklib.ts';
- import { FOLDER, PERSON_FIELDS, norm } from './lib.ts';
- const { p } = readPayload();
- if (!p) process.exit(0);
- const root = projectRoot(p);
- const rel = projectPath(root, p.tool_input?.file_path);
- if (!rel || !rel.startsWith(FOLDER + '/')) process.exit(0);
- const after = contentAfter(root, String(p.tool_name ?? ''), p.tool_input ?? {});
- if (after === null) process.exit(0);
- const fmOf = (t: string) => parseYaml(splitFrontMatter(t)[0]);
- const before = existsSync(join(root, rel)) ? fmOf(readFileSync(join(root, rel), 'utf8')) : {};
- const now = fmOf(after);
- const changed = PERSON_FIELDS.filter((f) => norm(before[f]) !== norm(now[f]));
- if (changed.length) {
- 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`);
- process.exit(BLOCK);
- }