qfai 2.2.1 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. package/README.md +37 -20
  2. package/assets/init/.claude/settings.json +35 -1
  3. package/assets/init/.codex/hooks.json +24 -0
  4. package/assets/init/.github/instructions/code-review.instructions.md +1 -1
  5. package/assets/init/.qfai/assistant/agent/architecture-reviewer.md +4 -4
  6. package/assets/init/.qfai/assistant/agent/fullstack-engineer.md +129 -0
  7. package/assets/init/.qfai/assistant/agent/implementation-reviewer.md +76 -4
  8. package/assets/init/.qfai/assistant/agent/product-surface-reviewer.md +9 -5
  9. package/assets/init/.qfai/assistant/agent/requirements-reviewer.md +13 -3
  10. package/assets/init/.qfai/assistant/rule/agent-selection.md +22 -42
  11. package/assets/init/.qfai/assistant/rule/communication.md +1 -1
  12. package/assets/init/.qfai/assistant/rule/constitution.md +77 -242
  13. package/assets/init/.qfai/assistant/rule/drift-protocol.md +31 -165
  14. package/assets/init/.qfai/assistant/rule/references/asset-authoring.md +4 -3
  15. package/assets/init/.qfai/assistant/rule/references/codex-delegation.md +4 -4
  16. package/assets/init/.qfai/assistant/rule/references/gate-failure-autorepair.md +2 -2
  17. package/assets/init/.qfai/assistant/rule/references/placeholder-scan.md +2 -3
  18. package/assets/init/.qfai/assistant/rule/references/reviewer-findings.md +3 -3
  19. package/assets/init/.qfai/assistant/rule/references/worker-edit-boundary.md +15 -6
  20. package/assets/init/.qfai/assistant/rule/research-first-protocol.md +7 -6
  21. package/assets/init/.qfai/assistant/rule/review-convergence.md +7 -7
  22. package/assets/init/.qfai/assistant/rule/shared-skill-delegation-baseline.md +82 -66
  23. package/assets/init/.qfai/assistant/rule/shared-skill-operating-baseline.md +19 -21
  24. package/assets/init/.qfai/assistant/rule/test-layers.md +1 -1
  25. package/assets/init/.qfai/assistant/rule/thinking.md +2 -2
  26. package/assets/init/.qfai/assistant/rule/ui-definition-protocol.md +5 -5
  27. package/assets/init/.qfai/assistant/rule/ui-procurement.md +1 -1
  28. package/assets/init/.qfai/assistant/rule/workflow.md +16 -16
  29. package/assets/init/.qfai/assistant/skill/qfai-configure/SKILL.md +27 -60
  30. package/assets/init/.qfai/assistant/skill/qfai-discussion/SKILL.md +3 -17
  31. package/assets/init/.qfai/assistant/skill/qfai-discussion/templates/03_Story-Workshop.md +1 -2
  32. package/assets/init/.qfai/assistant/skill/qfai-discussion/templates/uiux/50_review_input_bundle.md +4 -3
  33. package/assets/init/.qfai/assistant/skill/qfai-grill/SKILL.md +9 -10
  34. package/assets/init/.qfai/assistant/skill/qfai-grilling/SKILL.md +45 -58
  35. package/assets/init/.qfai/assistant/skill/qfai-grilling/references/confirmation-sheet.md +71 -0
  36. package/assets/init/.qfai/assistant/skill/qfai-grilling/references/delegated-session.md +35 -18
  37. package/assets/init/.qfai/assistant/skill/qfai-grilling/references/session-record.md +3 -3
  38. package/assets/init/.qfai/assistant/skill/qfai-implement/SKILL.md +2 -18
  39. package/assets/init/.qfai/assistant/skill/qfai-implement/references/parallelization-policy.md +53 -21
  40. package/assets/init/.qfai/assistant/skill/qfai-implement/references/ui-affecting.md +35 -2
  41. package/assets/init/.qfai/assistant/skill/qfai-maintain/SKILL.md +2 -5
  42. package/assets/init/.qfai/assistant/skill/qfai-migration-v1-to-v2/SKILL.md +2 -15
  43. package/assets/init/.qfai/assistant/skill/qfai-migration-v1-to-v2/references/migration-guide.md +10 -14
  44. package/assets/init/.qfai/assistant/skill/qfai-migration-v1-to-v2/references/migration-placement.md +0 -7
  45. package/assets/init/.qfai/assistant/skill/qfai-prototyping/SKILL.md +6 -15
  46. package/assets/init/.qfai/assistant/skill/qfai-prototyping/references/generator-prompt.md +76 -5
  47. package/assets/init/.qfai/assistant/skill/qfai-prototyping/references/handoff.md +2 -1
  48. package/assets/init/.qfai/assistant/skill/qfai-prototyping/references/iteration-loop.md +60 -5
  49. package/assets/init/.qfai/assistant/skill/qfai-prototyping/references/review-payload-schema.md +15 -6
  50. package/assets/init/.qfai/assistant/skill/qfai-prototyping/references/reviewer-prompt.md +42 -11
  51. package/assets/init/.qfai/assistant/skill/qfai-run/SKILL.md +8 -10
  52. package/assets/init/.qfai/assistant/skill/qfai-run/references/extraction.md +12 -7
  53. package/assets/init/.qfai/assistant/skill/qfai-run/references/operator-screens.md +4 -4
  54. package/assets/init/.qfai/assistant/skill/qfai-run/references/stage-points.md +1 -1
  55. package/assets/init/.qfai/assistant/skill/qfai-sdd/SKILL.md +10 -25
  56. package/assets/init/.qfai/assistant/skill/qfai-sdd/references/concrete-abstract-cycle.md +27 -29
  57. package/assets/init/.qfai/assistant/skill/qfai-sdd/references/contract-artifact-rules.md +13 -3
  58. package/assets/init/.qfai/assistant/skill/qfai-sdd/references/design-anti-patterns.md +36 -3
  59. package/assets/init/.qfai/assistant/skill/qfai-sdd/references/design-md-authoring.md +8 -9
  60. package/assets/init/.qfai/assistant/skill/qfai-sdd/references/requirements-decomposition.md +6 -6
  61. package/assets/init/.qfai/assistant/skill/qfai-sdd/references/sdd-execution-playbook.md +7 -7
  62. package/assets/init/.qfai/assistant/skill/qfai-sdd/references/sdd-phase-checklists.md +5 -5
  63. package/assets/init/.qfai/assistant/skill/qfai-sdd/references/sdd-pre-draft-grilling.md +7 -7
  64. package/assets/init/.qfai/assistant/skill/qfai-sdd/references/sdd-quality-gate.md +3 -3
  65. package/assets/init/.qfai/assistant/skill/qfai-sdd/references/sdd-triage.md +115 -199
  66. package/assets/init/.qfai/assistant/skill/qfai-sdd/references/spec-traceability-rules.md +25 -25
  67. package/assets/init/.qfai/assistant/skill/qfai-sdd/references/ui-contract-guide.md +23 -113
  68. package/assets/init/.qfai/assistant/skill/qfai-sdd/references/ui-design-contract-normalization.md +2 -2
  69. package/assets/init/.qfai/assistant/skill/qfai-sdd/templates/contracts/ui-contract.sample.yaml +8 -7
  70. package/assets/init/.qfai/assistant/skill/qfai-sdd/templates/spec/03_contract/tech.md +1 -0
  71. package/assets/init/.qfai/assistant/skill/qfai-sdd/templates/spec/decisions/page-NNNN.md +2 -2
  72. package/assets/init/.qfai/assistant/skill/qfai-sdd/templates/spec/decisions.md +59 -31
  73. package/assets/init/.qfai/assistant/skill/qfai-sdd/templates/spec/open-questions.md +15 -11
  74. package/assets/init/.qfai/assistant/skill/qfai-triage/SKILL.md +1 -14
  75. package/assets/init/.qfai/assistant/skill/qfai-verify/SKILL.md +2 -13
  76. package/assets/init/.qfai/assistant/skill/qfai-verify/references/verify-output-contract.md +1 -1
  77. package/assets/init/.qfai/assistant/skill/web-research/SKILL.md +19 -21
  78. package/assets/init/.qfai/assistant/step/common-design-md/STEP.md +9 -11
  79. package/assets/init/.qfai/assistant/step/common-gate-run/STEP.md +2 -3
  80. package/assets/init/.qfai/assistant/step/common-grilling-record/STEP.md +3 -4
  81. package/assets/init/.qfai/assistant/step/common-policy-check/STEP.md +1 -2
  82. package/assets/init/.qfai/assistant/step/common-review-cycle/STEP.md +8 -7
  83. package/assets/init/.qfai/assistant/step/discussion-interview/STEP.md +7 -9
  84. package/assets/init/.qfai/assistant/step/discussion-oq/STEP.md +0 -2
  85. package/assets/init/.qfai/assistant/step/discussion-pack/STEP.md +43 -2
  86. package/assets/init/.qfai/assistant/step/discussion-research/STEP.md +0 -2
  87. package/assets/init/.qfai/assistant/step/discussion-uiux/STEP.md +0 -2
  88. package/assets/init/.qfai/assistant/step/implement-acceptance/STEP.md +1 -5
  89. package/assets/init/.qfai/assistant/step/implement-backport/STEP.md +0 -5
  90. package/assets/init/.qfai/assistant/step/implement-benchmark/STEP.md +0 -4
  91. package/assets/init/.qfai/assistant/step/implement-bisect/STEP.md +0 -5
  92. package/assets/init/.qfai/assistant/step/implement-credentials/STEP.md +0 -4
  93. package/assets/init/.qfai/assistant/step/implement-dep-bump/STEP.md +11 -18
  94. package/assets/init/.qfai/assistant/step/implement-diagnose/STEP.md +0 -6
  95. package/assets/init/.qfai/assistant/step/implement-minimize/STEP.md +0 -5
  96. package/assets/init/.qfai/assistant/step/implement-oracle-parity/STEP.md +0 -6
  97. package/assets/init/.qfai/assistant/step/implement-procedure/STEP.md +1 -3
  98. package/assets/init/.qfai/assistant/step/implement-quarantine/STEP.md +0 -4
  99. package/assets/init/.qfai/assistant/step/implement-refactor/STEP.md +0 -4
  100. package/assets/init/.qfai/assistant/step/implement-regression-fix/STEP.md +2 -6
  101. package/assets/init/.qfai/assistant/step/implement-retire/STEP.md +0 -6
  102. package/assets/init/.qfai/assistant/step/implement-revert/STEP.md +0 -4
  103. package/assets/init/.qfai/assistant/step/implement-scaffold/STEP.md +5 -7
  104. package/assets/init/.qfai/assistant/step/implement-seam/STEP.md +0 -4
  105. package/assets/init/.qfai/assistant/step/implement-stress-harness/STEP.md +0 -5
  106. package/assets/init/.qfai/assistant/step/implement-sweep/STEP.md +0 -5
  107. package/assets/init/.qfai/assistant/step/implement-tdd/STEP.md +10 -18
  108. package/assets/init/.qfai/assistant/step/implement-test-fix/STEP.md +0 -5
  109. package/assets/init/.qfai/assistant/step/implement-tooling/STEP.md +0 -3
  110. package/assets/init/.qfai/assistant/step/maintain-edit/STEP.md +18 -21
  111. package/assets/init/.qfai/assistant/step/prototyping-grill/STEP.md +4 -6
  112. package/assets/init/.qfai/assistant/step/prototyping-handoff/STEP.md +0 -2
  113. package/assets/init/.qfai/assistant/step/prototyping-loop/STEP.md +106 -31
  114. package/assets/init/.qfai/assistant/step/prototyping-preflight/STEP.md +0 -2
  115. package/assets/init/.qfai/assistant/step/sdd-contract/STEP.md +18 -19
  116. package/assets/init/.qfai/assistant/step/sdd-cycle/STEP.md +8 -11
  117. package/assets/init/.qfai/assistant/step/sdd-flow/STEP.md +25 -6
  118. package/assets/init/.qfai/assistant/step/sdd-gate/STEP.md +0 -1
  119. package/assets/init/.qfai/assistant/step/sdd-story/STEP.md +6 -8
  120. package/assets/init/.qfai/assistant/step/sdd-triage/STEP.md +71 -82
  121. package/assets/init/.qfai/assistant/step/triage-answer/STEP.md +0 -2
  122. package/assets/init/.qfai/assistant/step/triage-close/STEP.md +0 -2
  123. package/assets/init/.qfai/assistant/step/triage-cluster/STEP.md +0 -2
  124. package/assets/init/.qfai/assistant/step/triage-decompose/STEP.md +0 -2
  125. package/assets/init/.qfai/assistant/step/triage-dedupe/STEP.md +0 -2
  126. package/assets/init/.qfai/assistant/step/triage-handoff/STEP.md +0 -2
  127. package/assets/init/.qfai/assistant/step/triage-investigate/STEP.md +0 -2
  128. package/assets/init/.qfai/assistant/step/triage-operate/STEP.md +29 -17
  129. package/assets/init/.qfai/assistant/step/triage-owned-cleanup/STEP.md +0 -2
  130. package/assets/init/.qfai/assistant/step/triage-request-info/STEP.md +0 -2
  131. package/assets/init/.qfai/assistant/step/triage-security-intake/STEP.md +0 -2
  132. package/assets/init/.qfai/assistant/step/verify-advisory/STEP.md +0 -2
  133. package/assets/init/.qfai/assistant/step/verify-change-note/STEP.md +0 -2
  134. package/assets/init/.qfai/assistant/step/verify-commit/STEP.md +2 -6
  135. package/assets/init/.qfai/assistant/step/verify-context/STEP.md +7 -26
  136. package/assets/init/.qfai/assistant/step/verify-external/STEP.md +0 -2
  137. package/assets/init/.qfai/assistant/step/verify-manual/STEP.md +0 -2
  138. package/assets/init/.qfai/assistant/step/verify-qfai-gate/STEP.md +0 -2
  139. package/assets/init/.qfai/assistant/step/verify-release-notes/STEP.md +0 -2
  140. package/assets/init/.qfai/assistant/step/verify-repeat-run/STEP.md +0 -2
  141. package/assets/init/.qfai/assistant/step/verify-repo-gate/STEP.md +0 -2
  142. package/assets/init/root/.agents/rules/ai-readable-markdown.md +8 -1
  143. package/assets/init/root/.agents/rules/delegation-steering.md +69 -0
  144. package/assets/init/root/.agents/rules/grilling.md +81 -52
  145. package/assets/init/root/.agents/rules/minimal-implementation.md +5 -5
  146. package/assets/init/root/.agents/rules/reply-forms.md +191 -0
  147. package/assets/init/root/.agents/rules/session-feedback.md +104 -27
  148. package/assets/init/root/.agents/rules/user-questions.md +6 -1
  149. package/assets/init/root/.gitattributes +5 -6
  150. package/assets/init/root/AGENTS.md +4 -2
  151. package/assets/mdschema/story/03_contract/tech.mdschema.yml +1 -1
  152. package/assets/mdschema/story/decisions/page-NNNN.mdschema.yml +3 -3
  153. package/assets/mdschema/story/decisions.mdschema.yml +4 -4
  154. package/assets/mdschema/story/open-questions.mdschema.yml +3 -3
  155. package/assets/schemas/workflow/extraction.schema.json +0 -2
  156. package/dist/cli/index.cjs +3826 -3929
  157. package/dist/cli/index.cjs.map +1 -1
  158. package/dist/cli/index.mjs +3793 -3904
  159. package/dist/cli/index.mjs.map +1 -1
  160. package/dist/graph/cli.cjs +14 -2
  161. package/dist/graph/cli.cjs.map +1 -1
  162. package/dist/graph/cli.d.cts +1 -1
  163. package/dist/graph/cli.d.ts +1 -1
  164. package/dist/graph/cli.mjs +14 -2
  165. package/dist/graph/cli.mjs.map +1 -1
  166. package/dist/graph/hook.cjs.map +1 -1
  167. package/dist/graph/hook.mjs.map +1 -1
  168. package/dist/hook/turn-end.cjs +1022 -0
  169. package/dist/hook/turn-end.cjs.map +1 -0
  170. package/dist/hook/turn-end.d.cts +2 -0
  171. package/dist/hook/turn-end.d.ts +2 -0
  172. package/dist/hook/turn-end.mjs +998 -0
  173. package/dist/hook/turn-end.mjs.map +1 -0
  174. package/dist/index.cjs +3635 -4578
  175. package/dist/index.cjs.map +1 -1
  176. package/dist/index.d.cts +20 -21
  177. package/dist/index.d.ts +20 -21
  178. package/dist/index.mjs +3696 -4641
  179. package/dist/index.mjs.map +1 -1
  180. package/dist/process-qqX2Owgq.d.cts +18 -0
  181. package/dist/process-qqX2Owgq.d.ts +18 -0
  182. package/package.json +2 -2
  183. package/assets/defaults/agent-routing/_contract.yml +0 -77
  184. package/assets/defaults/agent-routing/common.yml +0 -14
  185. package/assets/defaults/agent-routing/qfai-discussion.yml +0 -57
  186. package/assets/defaults/agent-routing/qfai-implement.yml +0 -236
  187. package/assets/defaults/agent-routing/qfai-maintain.yml +0 -12
  188. package/assets/defaults/agent-routing/qfai-prototyping.yml +0 -48
  189. package/assets/defaults/agent-routing/qfai-sdd.yml +0 -56
  190. package/assets/defaults/agent-routing/qfai-triage.yml +0 -108
  191. package/assets/defaults/agent-routing/qfai-verify.yml +0 -92
  192. package/assets/defaults/agent-routing/skills.yml +0 -59
  193. package/assets/defaults/review-profiles.yml +0 -25
  194. package/assets/init/.qfai/assistant/agent/acceptance-test-engineer.md +0 -81
  195. package/assets/init/.qfai/assistant/agent/backend-engineer.md +0 -90
  196. package/assets/init/.qfai/assistant/agent/completion-reviewer.md +0 -88
  197. package/assets/init/.qfai/assistant/agent/delivery-planner.md +0 -85
  198. package/assets/init/.qfai/assistant/agent/devops-ci-engineer.md +0 -78
  199. package/assets/init/.qfai/assistant/agent/discovery-analyst.md +0 -81
  200. package/assets/init/.qfai/assistant/agent/doc-steward.md +0 -78
  201. package/assets/init/.qfai/assistant/agent/frontend-engineer.md +0 -94
  202. package/assets/init/.qfai/assistant/agent/orchestrator.md +0 -92
  203. package/assets/init/.qfai/assistant/agent/product-experience-architect.md +0 -101
  204. package/assets/init/.qfai/assistant/agent/qa-gatekeeper.md +0 -153
  205. package/assets/init/.qfai/assistant/agent/qa-strategist.md +0 -82
  206. package/assets/init/.qfai/assistant/agent/requirements-analyst.md +0 -96
  207. package/assets/init/.qfai/assistant/agent/solution-architect.md +0 -89
  208. package/assets/init/.qfai/assistant/agent/test-design-analyst.md +0 -132
  209. package/assets/init/.qfai/assistant/skill/qfai-sdd/templates/change-request.md +0 -52
  210. package/dist/process-C21nTvkT.d.cts +0 -12
  211. package/dist/process-C21nTvkT.d.ts +0 -12
@@ -0,0 +1,998 @@
1
+ // src/cli/turnEndHook.ts
2
+ import { readFileSync } from "fs";
3
+ import os from "os";
4
+
5
+ // src/core/turnEndHook.ts
6
+ import { spawn, spawnSync } from "child_process";
7
+ import { createHash } from "crypto";
8
+ import { readFile as readFile2, rm, writeFile } from "fs/promises";
9
+ import path2 from "path";
10
+
11
+ // src/core/config.ts
12
+ import { access, readFile } from "fs/promises";
13
+ import path from "path";
14
+ import { parse as parseYaml } from "yaml";
15
+
16
+ // src/core/fs/errno.ts
17
+ function isEnoent(err) {
18
+ return hasErrnoCode(err) && err.code === "ENOENT";
19
+ }
20
+ function hasErrnoCode(err) {
21
+ return err !== null && typeof err === "object" && "code" in err && typeof err.code === "string";
22
+ }
23
+
24
+ // src/shared/shippedWorkflowNames.ts
25
+ var SHIPPED_WORKFLOW_NAMES = /* @__PURE__ */ new Set([
26
+ "qfai-validate.yml",
27
+ "qfai-tests.yml",
28
+ "qfai-docs.yml"
29
+ ]);
30
+
31
+ // src/core/config.ts
32
+ var defaultConfig = {
33
+ paths: {
34
+ contractsDir: ".qfai/spec/03_contract",
35
+ specsDir: ".qfai/spec",
36
+ discussionDir: ".qfai/discussion",
37
+ outDir: ".qfai/report",
38
+ skillsDir: ".qfai/assistant/skill",
39
+ srcDir: "src",
40
+ testsDir: "tests"
41
+ },
42
+ validation: {
43
+ failOn: "error",
44
+ testStrategy: {
45
+ forbidTestTodoStubs: true
46
+ },
47
+ traceability: {
48
+ testFileGlobs: [],
49
+ testFileExcludeGlobs: []
50
+ }
51
+ },
52
+ output: {
53
+ validateJsonPath: ".qfai/report/validate.json"
54
+ },
55
+ prototyping: {
56
+ execution: {
57
+ targetUrl: null,
58
+ browserTool: "playwright"
59
+ }
60
+ }
61
+ };
62
+ function getConfigPath(root) {
63
+ return path.join(root, "qfai.config.yaml");
64
+ }
65
+ async function findConfigRoot(startDir) {
66
+ const resolvedStart = path.resolve(startDir);
67
+ let current = resolvedStart;
68
+ for (; ; ) {
69
+ const configPath = getConfigPath(current);
70
+ if (await exists(configPath)) {
71
+ return { root: current, configPath, found: true };
72
+ }
73
+ const parent = path.dirname(current);
74
+ if (parent === current) {
75
+ break;
76
+ }
77
+ current = parent;
78
+ }
79
+ return {
80
+ root: resolvedStart,
81
+ configPath: getConfigPath(resolvedStart),
82
+ found: false
83
+ };
84
+ }
85
+ async function loadConfig(root) {
86
+ const configPath = getConfigPath(root);
87
+ const issues = [];
88
+ let parsed;
89
+ try {
90
+ const raw = await readFile(configPath, "utf-8");
91
+ parsed = parseYaml(raw);
92
+ } catch (error) {
93
+ if (isEnoent(error)) {
94
+ return { config: defaultConfig, issues, configPath };
95
+ }
96
+ issues.push(configIssue(configPath, "The configuration file could not be read or parsed."));
97
+ return { config: defaultConfig, issues, configPath };
98
+ }
99
+ const normalized = normalizeConfig(parsed, configPath, issues);
100
+ if (readWorkflowMode(parsed) === null) {
101
+ issues.push(configIssue(configPath, WORKFLOW_MODE_MESSAGE));
102
+ }
103
+ if (readSkippedWorkflows(parsed) === null) {
104
+ issues.push(configIssue(configPath, SKIPPED_WORKFLOWS_MESSAGE));
105
+ }
106
+ if (readAcceptedDrift(parsed) === null) {
107
+ issues.push(configIssue(configPath, ACCEPTED_DRIFT_MESSAGE));
108
+ }
109
+ if (readStopGate(parsed) === null) {
110
+ issues.push(configIssue(configPath, STOP_GATE_MESSAGE));
111
+ }
112
+ return { config: normalized, issues, configPath, document: parsed };
113
+ }
114
+ var STOP_GATE_MESSAGE = "workflow.stopGate must be true or false; an absent key means true.";
115
+ function readStopGate(document) {
116
+ const workflow = isRecord(document) ? document.workflow : void 0;
117
+ if (workflow === void 0) return true;
118
+ if (!isRecord(workflow)) return null;
119
+ if (workflow.stopGate === void 0) return true;
120
+ return typeof workflow.stopGate === "boolean" ? workflow.stopGate : null;
121
+ }
122
+ var WORKFLOW_MODES = ["active", "shadow", "off"];
123
+ var WORKFLOW_MODE_MESSAGE = "workflow.mode must be active, shadow or off; an absent key means active.";
124
+ function readWorkflowMode(document) {
125
+ const workflow = isRecord(document) ? document.workflow : void 0;
126
+ if (workflow === void 0) return "active";
127
+ if (!isRecord(workflow)) return null;
128
+ const mode = workflow.mode;
129
+ if (mode === void 0) return "active";
130
+ return WORKFLOW_MODES.find((known) => known === mode) ?? null;
131
+ }
132
+ var SKIPPED_WORKFLOWS_MESSAGE = `workflow.skipShipped must be a list of shipped workflow file names (${[...SHIPPED_WORKFLOW_NAMES].join(", ")}); an absent key means none are skipped.`;
133
+ function readSkippedWorkflows(document) {
134
+ const workflow = isRecord(document) ? document.workflow : void 0;
135
+ if (!isRecord(workflow) || workflow.skipShipped === void 0) return /* @__PURE__ */ new Set();
136
+ const listed = workflow.skipShipped;
137
+ if (!Array.isArray(listed)) return null;
138
+ const names = /* @__PURE__ */ new Set();
139
+ for (const name of listed) {
140
+ if (typeof name !== "string" || !SHIPPED_WORKFLOW_NAMES.has(name)) return null;
141
+ names.add(name);
142
+ }
143
+ return names;
144
+ }
145
+ var ACCEPTED_DRIFT_MESSAGE = `workflow.acceptedDrift must be a list of entries, each with a file, one of the shipped workflow file names (${[...SHIPPED_WORKFLOW_NAMES].join(", ")}) listed once, the digest qfai doctor prints for it (sha256: and 64 lowercase hexadecimal characters), and a reason that is not empty; an absent key means no difference is accepted.`;
146
+ var DRIFT_DIGEST = /^sha256:[0-9a-f]{64}$/;
147
+ function readAcceptedDrift(document) {
148
+ const workflow = isRecord(document) ? document.workflow : void 0;
149
+ if (!isRecord(workflow) || workflow.acceptedDrift === void 0) return /* @__PURE__ */ new Map();
150
+ const listed = workflow.acceptedDrift;
151
+ if (!Array.isArray(listed)) return null;
152
+ const accepted = /* @__PURE__ */ new Map();
153
+ for (const entry of listed) {
154
+ if (!isRecord(entry)) return null;
155
+ const { file, digest, reason } = entry;
156
+ if (typeof file !== "string" || !SHIPPED_WORKFLOW_NAMES.has(file) || accepted.has(file)) {
157
+ return null;
158
+ }
159
+ if (typeof digest !== "string" || !DRIFT_DIGEST.test(digest)) return null;
160
+ if (typeof reason !== "string" || reason.trim() === "") return null;
161
+ accepted.set(file, { digest, reason: reason.trim() });
162
+ }
163
+ return accepted;
164
+ }
165
+ function resolvePath(root, config, key) {
166
+ return path.resolve(root, config.paths[key]);
167
+ }
168
+ function normalizeConfig(raw, configPath, issues) {
169
+ if (!isRecord(raw)) {
170
+ issues.push(configIssue(configPath, "The configuration file format is invalid."));
171
+ return defaultConfig;
172
+ }
173
+ const uiux = normalizeUiux(raw.uiux, configPath, issues);
174
+ const prototyping = normalizePrototyping(raw.prototyping, configPath, issues);
175
+ const report = normalizeReport(raw.report, configPath, issues);
176
+ const base = {
177
+ paths: normalizePaths(raw.paths, configPath, issues),
178
+ validation: normalizeValidation(raw.validation, configPath, issues),
179
+ output: normalizeOutput(raw.output, configPath, issues)
180
+ };
181
+ if (uiux) {
182
+ base.uiux = uiux;
183
+ }
184
+ if (prototyping) {
185
+ base.prototyping = prototyping;
186
+ }
187
+ if (report) {
188
+ base.report = report;
189
+ }
190
+ const baseBranch = readOptionalString(raw.baseBranch, "baseBranch", configPath, issues);
191
+ if (baseBranch !== void 0) {
192
+ base.baseBranch = baseBranch;
193
+ }
194
+ return base;
195
+ }
196
+ function normalizePaths(raw, configPath, issues) {
197
+ const base = defaultConfig.paths;
198
+ if (!raw) {
199
+ return base;
200
+ }
201
+ if (!isRecord(raw)) {
202
+ issues.push(configIssue(configPath, "paths must be an object."));
203
+ return base;
204
+ }
205
+ const migrationsDir = readOptionalString(
206
+ raw.migrationsDir,
207
+ "paths.migrationsDir",
208
+ configPath,
209
+ issues
210
+ );
211
+ return {
212
+ ...migrationsDir !== void 0 ? { migrationsDir } : {},
213
+ contractsDir: readTreeDirString(
214
+ raw.contractsDir,
215
+ base.contractsDir,
216
+ "paths.contractsDir",
217
+ configPath,
218
+ issues
219
+ ),
220
+ specsDir: readTreeDirString(raw.specsDir, base.specsDir, "paths.specsDir", configPath, issues),
221
+ discussionDir: readDirString(
222
+ raw.discussionDir,
223
+ base.discussionDir,
224
+ "paths.discussionDir",
225
+ configPath,
226
+ issues
227
+ ),
228
+ outDir: readDirString(raw.outDir, base.outDir, "paths.outDir", configPath, issues),
229
+ skillsDir: readDirString(raw.skillsDir, base.skillsDir, "paths.skillsDir", configPath, issues),
230
+ srcDir: readDirString(raw.srcDir, base.srcDir, "paths.srcDir", configPath, issues),
231
+ testsDir: readDirString(raw.testsDir, base.testsDir, "paths.testsDir", configPath, issues)
232
+ };
233
+ }
234
+ function normalizeValidation(raw, configPath, issues) {
235
+ const base = defaultConfig.validation;
236
+ if (!raw) {
237
+ return base;
238
+ }
239
+ if (!isRecord(raw)) {
240
+ issues.push(configIssue(configPath, "validation must be an object."));
241
+ return base;
242
+ }
243
+ let traceabilityRaw;
244
+ if (raw.traceability === void 0) {
245
+ traceabilityRaw = void 0;
246
+ } else if (isRecord(raw.traceability)) {
247
+ traceabilityRaw = raw.traceability;
248
+ } else {
249
+ issues.push(configIssue(configPath, "validation.traceability must be an object."));
250
+ traceabilityRaw = void 0;
251
+ }
252
+ let testStrategyRaw;
253
+ if (raw.testStrategy === void 0) {
254
+ testStrategyRaw = void 0;
255
+ } else if (isRecord(raw.testStrategy)) {
256
+ testStrategyRaw = raw.testStrategy;
257
+ } else {
258
+ issues.push(configIssue(configPath, "validation.testStrategy must be an object."));
259
+ testStrategyRaw = void 0;
260
+ }
261
+ reportRetiredTraceabilityKeys(traceabilityRaw, configPath, issues);
262
+ const forbiddenIdentifiers = normalizeForbiddenIdentifiers(
263
+ raw.forbiddenIdentifiers,
264
+ configPath,
265
+ issues
266
+ );
267
+ const staleTerms = readStringArray(
268
+ raw.staleTerms,
269
+ [],
270
+ "validation.staleTerms",
271
+ configPath,
272
+ issues
273
+ );
274
+ const diagramBudget = readDiagramBudget(raw.diagramBudget, configPath, issues);
275
+ return {
276
+ failOn: readFailOn(raw.failOn, base.failOn, "validation.failOn", configPath, issues),
277
+ ...staleTerms.length > 0 ? { staleTerms } : {},
278
+ ...diagramBudget !== void 0 ? { diagramBudget } : {},
279
+ ...forbiddenIdentifiers !== void 0 ? { forbiddenIdentifiers } : {},
280
+ testStrategy: {
281
+ forbidTestTodoStubs: readBoolean(
282
+ testStrategyRaw?.forbidTestTodoStubs,
283
+ base.testStrategy.forbidTestTodoStubs,
284
+ "validation.testStrategy.forbidTestTodoStubs",
285
+ configPath,
286
+ issues
287
+ )
288
+ },
289
+ traceability: {
290
+ testFileGlobs: readStringArray(
291
+ traceabilityRaw?.testFileGlobs,
292
+ base.traceability.testFileGlobs,
293
+ "validation.traceability.testFileGlobs",
294
+ configPath,
295
+ issues
296
+ ),
297
+ testFileExcludeGlobs: readStringArray(
298
+ traceabilityRaw?.testFileExcludeGlobs,
299
+ base.traceability.testFileExcludeGlobs,
300
+ "validation.traceability.testFileExcludeGlobs",
301
+ configPath,
302
+ issues
303
+ )
304
+ }
305
+ };
306
+ }
307
+ function normalizeForbiddenIdentifiers(raw, configPath, issues) {
308
+ if (raw === void 0) return void 0;
309
+ const invalid = () => {
310
+ issues.push(configIssue(configPath, "validation.forbiddenIdentifiers is invalid."));
311
+ return void 0;
312
+ };
313
+ if (!Array.isArray(raw) || raw.length > 64) {
314
+ invalid();
315
+ return void 0;
316
+ }
317
+ const entries = [];
318
+ const pairs = /* @__PURE__ */ new Set();
319
+ const lengths = /* @__PURE__ */ new Set();
320
+ for (const entry of raw) {
321
+ if (!isRecord(entry) || Object.keys(entry).length !== 2 || Object.keys(entry).some((key) => key !== "sha256" && key !== "byteLength") || typeof entry.sha256 !== "string" || !/^[0-9a-f]{64}$/.test(entry.sha256) || typeof entry.byteLength !== "number" || !Number.isInteger(entry.byteLength) || entry.byteLength < 1 || entry.byteLength > 128) {
322
+ invalid();
323
+ return void 0;
324
+ }
325
+ const pair = `${entry.sha256}:${entry.byteLength}`;
326
+ if (pairs.has(pair)) {
327
+ invalid();
328
+ return void 0;
329
+ }
330
+ pairs.add(pair);
331
+ lengths.add(entry.byteLength);
332
+ if (lengths.size > 8) {
333
+ invalid();
334
+ return void 0;
335
+ }
336
+ entries.push({ sha256: entry.sha256, byteLength: entry.byteLength });
337
+ }
338
+ return entries;
339
+ }
340
+ function normalizeOutput(raw, configPath, issues) {
341
+ const base = defaultConfig.output;
342
+ if (!raw) {
343
+ return base;
344
+ }
345
+ if (!isRecord(raw)) {
346
+ issues.push(configIssue(configPath, "output must be an object."));
347
+ return base;
348
+ }
349
+ return {
350
+ validateJsonPath: readString(
351
+ raw.validateJsonPath,
352
+ base.validateJsonPath,
353
+ "output.validateJsonPath",
354
+ configPath,
355
+ issues
356
+ )
357
+ };
358
+ }
359
+ function normalizePrototyping(raw, configPath, issues) {
360
+ if (raw === void 0 || raw === null) {
361
+ return void 0;
362
+ }
363
+ if (!isRecord(raw)) {
364
+ issues.push(configIssue(configPath, "prototyping must be an object."));
365
+ return void 0;
366
+ }
367
+ const execution = normalizePrototypingExecution(raw.execution, configPath, issues);
368
+ if (Object.prototype.hasOwnProperty.call(raw, "primarySpecId")) {
369
+ issues.push(
370
+ configIssue(
371
+ configPath,
372
+ "prototyping.primarySpecId is retired; use prototyping.primaryUiContract: UI-NNNN."
373
+ )
374
+ );
375
+ }
376
+ const primaryUiContract = normalizePrimaryUiContract(raw.primaryUiContract, configPath, issues);
377
+ if (!execution && primaryUiContract === void 0) {
378
+ return void 0;
379
+ }
380
+ return {
381
+ ...execution ? { execution } : {},
382
+ ...primaryUiContract !== void 0 ? { primaryUiContract } : {}
383
+ };
384
+ }
385
+ function normalizePrimaryUiContract(raw, configPath, issues) {
386
+ if (raw === void 0 || raw === null) {
387
+ return void 0;
388
+ }
389
+ if (typeof raw !== "string" || !/^UI-\d{4}$/.test(raw)) {
390
+ issues.push(configIssue(configPath, primaryUiContractMessage(raw)));
391
+ return void 0;
392
+ }
393
+ return raw;
394
+ }
395
+ function primaryUiContractMessage(raw) {
396
+ return `prototyping.primaryUiContract must be a full UI-NNNN ID; received ${JSON.stringify(raw)}.`;
397
+ }
398
+ function normalizePrototypingExecution(raw, configPath, issues) {
399
+ const base = defaultConfig.prototyping?.execution;
400
+ if (raw === void 0 || raw === null) {
401
+ return base ? { ...base } : void 0;
402
+ }
403
+ if (!isRecord(raw)) {
404
+ issues.push(configIssue(configPath, "prototyping.execution must be an object."));
405
+ return base ? { ...base } : void 0;
406
+ }
407
+ const browserToolRaw = raw.browserTool;
408
+ const browserTool = "playwright";
409
+ if (browserToolRaw !== void 0 && browserToolRaw !== browserTool) {
410
+ issues.push(
411
+ configIssue(
412
+ configPath,
413
+ `prototyping.execution.browserTool must be "playwright". Received: ${JSON.stringify(browserToolRaw)}`
414
+ )
415
+ );
416
+ }
417
+ return {
418
+ targetUrl: raw.targetUrl === null ? null : readOptionalString(
419
+ raw.targetUrl,
420
+ "prototyping.execution.targetUrl",
421
+ configPath,
422
+ issues
423
+ ) ?? null,
424
+ browserTool
425
+ };
426
+ }
427
+ function readNonNegativeInteger(raw, field, configPath, issues) {
428
+ if (typeof raw === "number" && Number.isFinite(raw) && Number.isInteger(raw) && raw >= 0) {
429
+ return raw;
430
+ }
431
+ issues.push(configIssue(configPath, `${field} must be an integer greater than or equal to 0.`));
432
+ return void 0;
433
+ }
434
+ function normalizeReport(raw, configPath, issues) {
435
+ if (raw === void 0 || raw === null) {
436
+ return void 0;
437
+ }
438
+ if (!isRecord(raw)) {
439
+ issues.push(configIssue(configPath, "report must be an object."));
440
+ return void 0;
441
+ }
442
+ const result = {};
443
+ if (raw.staleTtlDays !== void 0) {
444
+ const value = readNonNegativeInteger(
445
+ raw.staleTtlDays,
446
+ "report.staleTtlDays",
447
+ configPath,
448
+ issues
449
+ );
450
+ if (value !== void 0) {
451
+ result.staleTtlDays = value;
452
+ }
453
+ }
454
+ if (raw.keepLatestRuns !== void 0) {
455
+ const value = readNonNegativeInteger(
456
+ raw.keepLatestRuns,
457
+ "report.keepLatestRuns",
458
+ configPath,
459
+ issues
460
+ );
461
+ if (value !== void 0) {
462
+ result.keepLatestRuns = value;
463
+ }
464
+ }
465
+ return Object.keys(result).length === 0 ? void 0 : result;
466
+ }
467
+ function readString(value, fallback, label, configPath, issues) {
468
+ if (typeof value === "string" && value.trim().length > 0) {
469
+ return value;
470
+ }
471
+ if (value !== void 0) {
472
+ issues.push(configIssue(configPath, `${label} must be a string.`));
473
+ }
474
+ return fallback;
475
+ }
476
+ function readDirString(value, fallback, label, configPath, issues) {
477
+ const raw = readString(value, fallback, label, configPath, issues);
478
+ const trimmed = raw.replace(/[\\/]+$/, "");
479
+ return trimmed.length > 0 ? trimmed : raw;
480
+ }
481
+ function readTreeDirString(value, fallback, label, configPath, issues) {
482
+ return readDirString(value, fallback, label, configPath, issues).replace(/\\/g, "/");
483
+ }
484
+ function readOptionalString(value, label, configPath, issues) {
485
+ if (value === void 0) {
486
+ return void 0;
487
+ }
488
+ if (typeof value === "string" && value.trim().length > 0) {
489
+ return value;
490
+ }
491
+ issues.push(configIssue(configPath, `${label} must be a non-empty string.`));
492
+ return void 0;
493
+ }
494
+ function readStringArray(value, fallback, label, configPath, issues) {
495
+ if (Array.isArray(value) && value.every((item) => typeof item === "string")) {
496
+ return value;
497
+ }
498
+ if (value !== void 0) {
499
+ issues.push(configIssue(configPath, `${label} must be an array of strings.`));
500
+ }
501
+ return fallback;
502
+ }
503
+ function readDiagramBudget(value, configPath, issues) {
504
+ if (value === void 0) {
505
+ return void 0;
506
+ }
507
+ if (!isRecord(value)) {
508
+ issues.push(configIssue(configPath, "validation.diagramBudget must be an object."));
509
+ return void 0;
510
+ }
511
+ for (const key of Object.keys(value)) {
512
+ if (key !== "nodes" && key !== "edges") {
513
+ issues.push(configIssue(configPath, `validation.diagramBudget.${key} is not a setting.`));
514
+ }
515
+ }
516
+ const budget = {};
517
+ for (const key of ["nodes", "edges"]) {
518
+ const limit = value[key];
519
+ if (limit === void 0) {
520
+ continue;
521
+ }
522
+ if (typeof limit === "number" && Number.isInteger(limit) && limit >= 1) {
523
+ budget[key] = limit;
524
+ } else {
525
+ issues.push(
526
+ configIssue(configPath, `validation.diagramBudget.${key} must be an integer of 1 or more.`)
527
+ );
528
+ }
529
+ }
530
+ return budget;
531
+ }
532
+ function readBoolean(value, fallback, label, configPath, issues) {
533
+ if (typeof value === "boolean") {
534
+ return value;
535
+ }
536
+ if (value !== void 0) {
537
+ issues.push(configIssue(configPath, `${label} must be a boolean.`));
538
+ }
539
+ return fallback;
540
+ }
541
+ function readFailOn(value, fallback, label, configPath, issues) {
542
+ if (value === "never" || value === "warning" || value === "error") {
543
+ return value;
544
+ }
545
+ if (value !== void 0) {
546
+ issues.push(configIssue(configPath, `${label} must be one of never|warning|error.`));
547
+ }
548
+ return fallback;
549
+ }
550
+ var RETIRED_TRACEABILITY_KEYS = ["scMustHaveTest", "unknownContractIdSeverity"];
551
+ function reportRetiredTraceabilityKeys(traceabilityRaw, configPath, issues) {
552
+ if (!traceabilityRaw) {
553
+ return;
554
+ }
555
+ for (const key of RETIRED_TRACEABILITY_KEYS) {
556
+ if (traceabilityRaw[key] === void 0) {
557
+ continue;
558
+ }
559
+ issues.push(
560
+ configDeprecatedIssue(
561
+ configPath,
562
+ `validation.traceability.${key} is retired. No check reads it, so setting it changes nothing. Remove it from qfai.config.yaml.`
563
+ )
564
+ );
565
+ }
566
+ }
567
+ function normalizeUiux(raw, configPath, issues) {
568
+ if (raw === void 0 || raw === null) {
569
+ return void 0;
570
+ }
571
+ if (!isRecord(raw)) {
572
+ issues.push(configIssue(configPath, "uiux must be an object."));
573
+ return void 0;
574
+ }
575
+ const result = {};
576
+ if (raw.platform !== void 0) {
577
+ if (typeof raw.platform === "string" && raw.platform.trim().length > 0) {
578
+ result.platform = raw.platform;
579
+ } else {
580
+ issues.push(configIssue(configPath, "uiux.platform must be a non-empty string."));
581
+ }
582
+ }
583
+ if (raw.designTokensDir !== void 0) {
584
+ if (typeof raw.designTokensDir === "string" && raw.designTokensDir.trim().length > 0) {
585
+ result.designTokensDir = raw.designTokensDir;
586
+ } else {
587
+ issues.push(configIssue(configPath, "uiux.designTokensDir must be a non-empty string."));
588
+ }
589
+ }
590
+ if (raw.htmlMockTimeout !== void 0) {
591
+ if (typeof raw.htmlMockTimeout === "number" && Number.isFinite(raw.htmlMockTimeout) && raw.htmlMockTimeout > 0) {
592
+ result.htmlMockTimeout = raw.htmlMockTimeout;
593
+ } else {
594
+ issues.push(configIssue(configPath, "uiux.htmlMockTimeout must be a positive number."));
595
+ }
596
+ }
597
+ if (raw.qualityProfile !== void 0) {
598
+ if (typeof raw.qualityProfile === "string" && ["strict", "high", "default"].includes(raw.qualityProfile)) {
599
+ result.qualityProfile = raw.qualityProfile;
600
+ } else {
601
+ issues.push(
602
+ configIssue(configPath, "uiux.qualityProfile must be one of strict|high|default.")
603
+ );
604
+ }
605
+ }
606
+ if (raw.requireResearchSummary !== void 0) {
607
+ if (typeof raw.requireResearchSummary === "boolean") {
608
+ result.requireResearchSummary = raw.requireResearchSummary;
609
+ } else {
610
+ issues.push(configIssue(configPath, "uiux.requireResearchSummary must be a boolean."));
611
+ }
612
+ }
613
+ if (raw.competitive_refs_min !== void 0) {
614
+ if (typeof raw.competitive_refs_min === "number" && Number.isInteger(raw.competitive_refs_min) && raw.competitive_refs_min >= 0) {
615
+ result.competitive_refs_min = raw.competitive_refs_min;
616
+ } else {
617
+ issues.push(
618
+ configIssue(
619
+ configPath,
620
+ "uiux.competitive_refs_min must be an integer greater than or equal to 0."
621
+ )
622
+ );
623
+ }
624
+ }
625
+ if (raw.catalogue_refs_min !== void 0) {
626
+ if (typeof raw.catalogue_refs_min === "number" && Number.isInteger(raw.catalogue_refs_min) && raw.catalogue_refs_min >= 0) {
627
+ result.catalogue_refs_min = raw.catalogue_refs_min;
628
+ } else {
629
+ issues.push(
630
+ configIssue(configPath, "uiux.catalogue_refs_min must be an integer of 0 or more.")
631
+ );
632
+ }
633
+ }
634
+ if (raw.surfacePaths !== void 0) {
635
+ const surfacePaths = raw.surfacePaths;
636
+ if (Array.isArray(surfacePaths) && surfacePaths.every(
637
+ (entry) => typeof entry === "string" && entry.trim().length > 0
638
+ )) {
639
+ result.surfacePaths = surfacePaths.map((entry) => entry.trim());
640
+ } else {
641
+ issues.push(
642
+ configIssue(configPath, "uiux.surfacePaths must be a list of non-empty glob strings.")
643
+ );
644
+ }
645
+ }
646
+ if (raw.registries !== void 0) {
647
+ const registries = normalizeUiuxRegistries(raw.registries, configPath, issues);
648
+ if (registries) {
649
+ result.registries = registries;
650
+ }
651
+ }
652
+ if (raw.audit !== void 0) {
653
+ const audit = normalizeUiuxAudit(raw.audit, configPath, issues);
654
+ if (audit) {
655
+ result.audit = audit;
656
+ }
657
+ }
658
+ return Object.keys(result).length > 0 ? result : void 0;
659
+ }
660
+ function normalizeUiuxRegistries(raw, configPath, issues) {
661
+ if (!isRecord(raw)) {
662
+ issues.push(configIssue(configPath, "uiux.registries must be an object."));
663
+ return void 0;
664
+ }
665
+ const result = {};
666
+ for (const [name, url] of Object.entries(raw)) {
667
+ if (typeof url !== "string" || url.trim().length === 0) {
668
+ issues.push(configIssue(configPath, `uiux.registries.${name} must be a non-empty string.`));
669
+ continue;
670
+ }
671
+ if (!url.includes("{name}")) {
672
+ issues.push(
673
+ configIssue(
674
+ configPath,
675
+ `uiux.registries.${name} must contain the {name} placeholder, or it resolves no component.`
676
+ )
677
+ );
678
+ continue;
679
+ }
680
+ result[name] = url;
681
+ }
682
+ return Object.keys(result).length > 0 ? result : void 0;
683
+ }
684
+ function normalizeUiuxAudit(raw, configPath, issues) {
685
+ if (!isRecord(raw)) {
686
+ issues.push(configIssue(configPath, "uiux.audit must be an object."));
687
+ return void 0;
688
+ }
689
+ const result = {};
690
+ if (raw.enabled !== void 0) {
691
+ if (typeof raw.enabled === "boolean") {
692
+ result.enabled = raw.enabled;
693
+ } else {
694
+ issues.push(configIssue(configPath, "uiux.audit.enabled must be a boolean."));
695
+ }
696
+ }
697
+ if (raw.slopDetection !== void 0) {
698
+ if (typeof raw.slopDetection === "boolean") {
699
+ result.slopDetection = raw.slopDetection;
700
+ } else {
701
+ issues.push(configIssue(configPath, "uiux.audit.slopDetection must be a boolean."));
702
+ }
703
+ }
704
+ if (raw.maxPrimaryCtas !== void 0) {
705
+ if (typeof raw.maxPrimaryCtas === "number" && Number.isFinite(raw.maxPrimaryCtas) && raw.maxPrimaryCtas >= 0) {
706
+ result.maxPrimaryCtas = raw.maxPrimaryCtas;
707
+ } else {
708
+ issues.push(
709
+ configIssue(
710
+ configPath,
711
+ "uiux.audit.maxPrimaryCtas must be a number greater than or equal to 0."
712
+ )
713
+ );
714
+ }
715
+ }
716
+ if (raw.maxRawTokenLiteralWarnings !== void 0) {
717
+ if (typeof raw.maxRawTokenLiteralWarnings === "number" && Number.isFinite(raw.maxRawTokenLiteralWarnings) && raw.maxRawTokenLiteralWarnings >= 0) {
718
+ result.maxRawTokenLiteralWarnings = raw.maxRawTokenLiteralWarnings;
719
+ } else {
720
+ issues.push(
721
+ configIssue(
722
+ configPath,
723
+ "uiux.audit.maxRawTokenLiteralWarnings must be a number greater than or equal to 0."
724
+ )
725
+ );
726
+ }
727
+ }
728
+ if (raw.maxDuplicateFindingsPerRule !== void 0) {
729
+ if (typeof raw.maxDuplicateFindingsPerRule === "number" && Number.isFinite(raw.maxDuplicateFindingsPerRule) && raw.maxDuplicateFindingsPerRule >= 0) {
730
+ result.maxDuplicateFindingsPerRule = raw.maxDuplicateFindingsPerRule;
731
+ } else {
732
+ issues.push(
733
+ configIssue(
734
+ configPath,
735
+ "uiux.audit.maxDuplicateFindingsPerRule must be a number greater than or equal to 0."
736
+ )
737
+ );
738
+ }
739
+ }
740
+ return Object.keys(result).length > 0 ? result : void 0;
741
+ }
742
+ function configIssue(file, message) {
743
+ return {
744
+ code: "QFAI-CFG-002",
745
+ severity: "error",
746
+ category: "canonical",
747
+ message,
748
+ file,
749
+ rule: "config.invalid"
750
+ };
751
+ }
752
+ function configDeprecatedIssue(file, message) {
753
+ return {
754
+ code: "QFAI-CFG-001",
755
+ severity: "error",
756
+ category: "canonical",
757
+ message,
758
+ file,
759
+ rule: "config.deprecatedKey"
760
+ };
761
+ }
762
+ async function exists(target) {
763
+ try {
764
+ await access(target);
765
+ return true;
766
+ } catch {
767
+ return false;
768
+ }
769
+ }
770
+ function isRecord(value) {
771
+ return value !== null && typeof value === "object" && !Array.isArray(value);
772
+ }
773
+
774
+ // src/core/turnEndHook.ts
775
+ var DEFAULT_TIMEOUT_MS = 6e4;
776
+ var TAIL_LINES = 40;
777
+ var TAIL_CHARS = 4e3;
778
+ var KEEP_CHARS = 64 * 1024;
779
+ var EXIT_GRACE_MS = 1e3;
780
+ var MAX_CONTINUED_BLOCKS = 3;
781
+ var COMMANDS_HEADING = /^##\s+Standard commands\b/i;
782
+ var TURN_END_LABEL = "turn-end check";
783
+ function readTurnEndChecks(techText) {
784
+ const commands = /* @__PURE__ */ new Map();
785
+ let marked = [];
786
+ let inSection = false;
787
+ for (const line of techText.split(/\r?\n/)) {
788
+ if (/^#{1,2}\s/.test(line)) {
789
+ inSection = COMMANDS_HEADING.test(line);
790
+ continue;
791
+ }
792
+ if (!inSection) continue;
793
+ const item = /^\s*-\s+([^:`]+):\s*(.*)$/.exec(line);
794
+ if (item === null) continue;
795
+ const label = (item[1] ?? "").trim();
796
+ const rest = item[2] ?? "";
797
+ if (label.toLowerCase() === TURN_END_LABEL) {
798
+ marked = rest.split(",").map((name) => name.replace(/`/g, "").trim()).filter((name) => name !== "");
799
+ continue;
800
+ }
801
+ const command = /`([^`]+)`/.exec(rest)?.[1];
802
+ if (command !== void 0) commands.set(label.toLowerCase(), command);
803
+ }
804
+ const checks = [];
805
+ for (const label of marked) {
806
+ const command = commands.get(label.toLowerCase());
807
+ if (command !== void 0) checks.push({ label, command });
808
+ }
809
+ return checks;
810
+ }
811
+ function stopTree(child) {
812
+ const pid = child.pid;
813
+ if (pid === void 0) return;
814
+ try {
815
+ if (process.platform === "win32") {
816
+ const killed = spawnSync("taskkill", ["/pid", String(pid), "/T", "/F"], {
817
+ windowsHide: true
818
+ });
819
+ if (killed.status !== 0) child.kill("SIGKILL");
820
+ } else {
821
+ process.kill(-pid, "SIGKILL");
822
+ }
823
+ } catch {
824
+ child.kill("SIGKILL");
825
+ }
826
+ }
827
+ function runCommand(command, cwd, timeoutMs) {
828
+ return new Promise((resolve) => {
829
+ let output = "";
830
+ let stdout = "";
831
+ let stderr = "";
832
+ let settled = false;
833
+ let grace;
834
+ const child = spawn(command, {
835
+ cwd,
836
+ shell: true,
837
+ windowsHide: true,
838
+ detached: process.platform !== "win32",
839
+ stdio: ["ignore", "pipe", "pipe"]
840
+ });
841
+ const finish = (code, signal, timedOut, extra = "") => {
842
+ if (settled) return;
843
+ settled = true;
844
+ clearTimeout(timer);
845
+ clearTimeout(grace);
846
+ child.stdout.destroy();
847
+ child.stderr.destroy();
848
+ resolve({
849
+ code,
850
+ signal,
851
+ output: output + extra,
852
+ ordered: stdout + stderr + extra,
853
+ timedOut
854
+ });
855
+ };
856
+ child.stdout.setEncoding("utf8");
857
+ child.stderr.setEncoding("utf8");
858
+ child.stdout.on("data", (chunk) => {
859
+ output = (output + chunk).slice(-KEEP_CHARS);
860
+ stdout = (stdout + chunk).slice(-KEEP_CHARS);
861
+ });
862
+ child.stderr.on("data", (chunk) => {
863
+ output = (output + chunk).slice(-KEEP_CHARS);
864
+ stderr = (stderr + chunk).slice(-KEEP_CHARS);
865
+ });
866
+ const timer = setTimeout(() => {
867
+ stopTree(child);
868
+ finish(null, null, true);
869
+ }, timeoutMs);
870
+ child.on("error", (error) => {
871
+ finish(null, null, false, `${error.message}
872
+ `);
873
+ });
874
+ child.on("exit", (code, signal) => {
875
+ clearTimeout(timer);
876
+ grace = setTimeout(() => {
877
+ if (process.platform !== "win32") stopTree(child);
878
+ finish(code, signal, false);
879
+ }, EXIT_GRACE_MS);
880
+ });
881
+ child.on("close", (code, signal) => {
882
+ finish(code, signal, false);
883
+ });
884
+ });
885
+ }
886
+ function endingOf(result, timeoutMs) {
887
+ if (result.timedOut) {
888
+ const seconds = Math.max(1, Math.round(timeoutMs / 1e3));
889
+ return `did not finish within ${seconds} second${seconds === 1 ? "" : "s"}`;
890
+ }
891
+ if (result.code !== null) return `ended with exit ${result.code}`;
892
+ return result.signal === null ? "ended with exit unknown" : `was stopped by ${result.signal}`;
893
+ }
894
+ function tail(output) {
895
+ const lines = output.replace(/\s+$/, "").split(/\r?\n/);
896
+ return lines.slice(-TAIL_LINES).join("\n").slice(-TAIL_CHARS);
897
+ }
898
+ function isRecord2(value) {
899
+ return typeof value === "object" && value !== null && !Array.isArray(value);
900
+ }
901
+ function stringField(event, key) {
902
+ const value = event[key];
903
+ return typeof value === "string" && value !== "" ? value : void 0;
904
+ }
905
+ function agentKey(event) {
906
+ const agent = stringField(event, "agent_id");
907
+ if (agent !== void 0) return agent;
908
+ return event.hook_event_name === "SubagentStop" ? "subagent" : "main";
909
+ }
910
+ function stateFile(stateDir, session, agent) {
911
+ const name = ["qfai-turn-end", session, agent].join("-").replace(/[^\w.-]/g, "_");
912
+ return path2.join(stateDir, name);
913
+ }
914
+ async function readState(file) {
915
+ if (file === null) return null;
916
+ try {
917
+ const value = JSON.parse(await readFile2(file, "utf8"));
918
+ if (!isRecord2(value)) return null;
919
+ const { digest, blocks } = value;
920
+ return typeof digest === "string" && typeof blocks === "number" ? { digest, blocks } : null;
921
+ } catch {
922
+ return null;
923
+ }
924
+ }
925
+ async function runTurnEndHook(input, options) {
926
+ const event = isRecord2(input) ? input : {};
927
+ const found = await findConfigRoot(stringField(event, "cwd") ?? options.processDir);
928
+ if (!found.found) return "";
929
+ const loaded = await loadConfig(found.root);
930
+ if (readStopGate(loaded.document) !== true) return "";
931
+ let techText;
932
+ try {
933
+ techText = await readFile2(
934
+ path2.join(resolvePath(found.root, loaded.config, "contractsDir"), "tech.md"),
935
+ "utf8"
936
+ );
937
+ } catch (error) {
938
+ if (isRecord2(error) && error.code === "ENOENT") return "";
939
+ throw error;
940
+ }
941
+ const checks = readTurnEndChecks(techText);
942
+ const session = stringField(event, "session_id");
943
+ const file = session === void 0 ? null : stateFile(options.stateDir, session, agentKey(event));
944
+ const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
945
+ for (const check of checks) {
946
+ const result = await runCommand(check.command, found.root, timeoutMs);
947
+ if (!result.timedOut && result.code === 0) continue;
948
+ const shown = tail(result.output);
949
+ const ending = endingOf(result, timeoutMs);
950
+ const digest = createHash("sha256").update([check.label, ending, tail(result.ordered)].join("\n").replace(/\d+/g, "#")).digest("hex");
951
+ const continued = event.stop_hook_active === true;
952
+ const last = await readState(file);
953
+ const blocks = continued && last !== null ? last.blocks + 1 : 1;
954
+ const release = file === null ? continued : continued && (last?.digest === digest || blocks > MAX_CONTINUED_BLOCKS);
955
+ if (release) {
956
+ if (file !== null) await rm(file, { force: true });
957
+ return `${JSON.stringify({
958
+ systemMessage: `QFAI turn-end check: ${check.label} still fails, and the turn ended anyway. Run ${check.command} in ${found.root} to see the failure.`
959
+ })}
960
+ `;
961
+ }
962
+ if (file !== null) await writeFile(file, JSON.stringify({ digest, blocks }), "utf8");
963
+ const reason = [
964
+ `The turn-end check ${check.label} failed: ${check.command} ${ending}.`,
965
+ shown === "" ? "It printed nothing." : `End of its output:
966
+ ${shown}`,
967
+ event.hook_event_name === "SubagentStop" ? "Fix the cause before ending the turn. If fixing it is outside your task, say so in your reply and end the turn. The check is listed under Turn-end check in tech.md." : "Fix the cause before ending the turn. The check is listed under Turn-end check in tech.md."
968
+ ].join("\n\n");
969
+ return `${JSON.stringify({ decision: "block", reason })}
970
+ `;
971
+ }
972
+ if (file !== null) await rm(file, { force: true });
973
+ return "";
974
+ }
975
+
976
+ // src/cli/turnEndHook.ts
977
+ async function main() {
978
+ try {
979
+ let input;
980
+ try {
981
+ input = JSON.parse(readFileSync(0, "utf8"));
982
+ } catch {
983
+ input = void 0;
984
+ }
985
+ const output = await runTurnEndHook(input, {
986
+ processDir: process.cwd(),
987
+ stateDir: os.tmpdir()
988
+ });
989
+ if (output !== "") process.stdout.write(output);
990
+ } catch (error) {
991
+ const message = error instanceof Error ? error.message : String(error);
992
+ process.stderr.write(`QFAI turn-end check could not run: ${message}
993
+ `);
994
+ }
995
+ process.exitCode = 0;
996
+ }
997
+ void main();
998
+ //# sourceMappingURL=turn-end.mjs.map