@boyingliu01/xp-gate 0.41.10 → 0.41.56

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 (123) hide show
  1. package/adapters/gate-3.sh +28 -1
  2. package/adapters/gate-6-layers.sh +18 -1
  3. package/bin/xp-gate.js +13 -0
  4. package/gate-3.sh +28 -1
  5. package/gate-6-layers.sh +18 -1
  6. package/gates/python-health.ts +74 -26
  7. package/gates/test-layers.ts +94 -9
  8. package/hooks/gate-3.sh +28 -1
  9. package/hooks/gate-6-layers.sh +18 -1
  10. package/hooks/gate-entries/gate-11-entry.sh +21 -0
  11. package/hooks/gate-entries/gate-2-entry.sh +15 -0
  12. package/hooks/install.sh +15 -3
  13. package/hooks/pre-commit +90 -13
  14. package/hooks/pre-push +10 -1
  15. package/hooks/sprint-gate.sh +101 -13
  16. package/lib/check-alignment-runner.cjs +97 -0
  17. package/lib/check.js +6 -0
  18. package/lib/crap-cli.js +14 -6
  19. package/lib/delphi-setup.js +13 -0
  20. package/lib/doctor-tui.js +205 -102
  21. package/lib/doctor.js +283 -31
  22. package/lib/gate-runner.js +14 -3
  23. package/lib/init-install.js +299 -0
  24. package/lib/init-languages.js +158 -0
  25. package/lib/init-manifest.js +165 -0
  26. package/lib/init-md-injection.js +58 -0
  27. package/lib/init.js +134 -500
  28. package/lib/install-skill.js +66 -1
  29. package/lib/layer-baseline.cjs +94 -0
  30. package/lib/mattpocock-license-verdict.json +7 -0
  31. package/lib/uninstall-skills.js +140 -0
  32. package/lib/uninstall.js +41 -100
  33. package/lib/update-skill.js +9 -1
  34. package/package.json +3 -1
  35. package/plugins/claude-code/.claude-plugin/plugin.json +1 -1
  36. package/plugins/claude-code/skills/delphi-review/AGENTS.md +1 -1
  37. package/plugins/claude-code/skills/delphi-review/INSTALL.md +22 -3
  38. package/plugins/claude-code/skills/sprint-flow/AGENTS.md +1 -1
  39. package/plugins/claude-code/skills/sprint-flow/references/definition-of-done.md +2 -2
  40. package/plugins/claude-code/skills/sprint-flow/references/phase-3-build.md +1 -1
  41. package/plugins/claude-code/skills/sprint-flow/references/phase-4-verify.md +2 -2
  42. package/plugins/claude-code/skills/sprint-flow/references/phase-6-close.md +17 -0
  43. package/plugins/claude-code/skills/test-specification-alignment/AGENTS.md +1 -1
  44. package/plugins/dsh/lib/delphi-evidence.d.ts +59 -0
  45. package/plugins/dsh/lib/delphi-evidence.d.ts.map +1 -0
  46. package/plugins/dsh/lib/delphi-evidence.js +128 -0
  47. package/plugins/dsh/lib/delphi-finding.d.ts +123 -0
  48. package/plugins/dsh/lib/delphi-finding.d.ts.map +1 -0
  49. package/plugins/dsh/lib/delphi-finding.js +332 -0
  50. package/plugins/dsh/lib/delphi-findings-parse.d.ts +29 -0
  51. package/plugins/dsh/lib/delphi-findings-parse.d.ts.map +1 -0
  52. package/plugins/dsh/lib/delphi-findings-parse.js +138 -0
  53. package/plugins/dsh/lib/delphi-ledger.d.ts +55 -0
  54. package/plugins/dsh/lib/delphi-ledger.d.ts.map +1 -0
  55. package/plugins/dsh/lib/delphi-ledger.js +124 -0
  56. package/plugins/dsh/lib/delphi-run.d.ts +7 -0
  57. package/plugins/dsh/lib/delphi-run.d.ts.map +1 -1
  58. package/plugins/dsh/lib/delphi-run.js +48 -18
  59. package/plugins/dsh/lib/delphi-seats.d.ts +34 -0
  60. package/plugins/dsh/lib/delphi-seats.d.ts.map +1 -0
  61. package/plugins/dsh/lib/delphi-seats.js +40 -0
  62. package/plugins/dsh/lib/evidence.d.ts +21 -0
  63. package/plugins/dsh/lib/evidence.d.ts.map +1 -1
  64. package/plugins/dsh/lib/evidence.js +3 -0
  65. package/plugins/dsh/lib/index.d.ts.map +1 -1
  66. package/plugins/dsh/lib/index.js +12 -0
  67. package/plugins/dsh/package.json +1 -1
  68. package/plugins/dsh/skills/delphi-review/AGENTS.md +1 -1
  69. package/plugins/dsh/skills/delphi-review/INSTALL.md +22 -3
  70. package/plugins/dsh/skills/sprint-flow/AGENTS.md +1 -1
  71. package/plugins/dsh/skills/sprint-flow/references/definition-of-done.md +2 -2
  72. package/plugins/dsh/skills/sprint-flow/references/phase-3-build.md +1 -1
  73. package/plugins/dsh/skills/sprint-flow/references/phase-4-verify.md +2 -2
  74. package/plugins/dsh/skills/sprint-flow/references/phase-6-close.md +17 -0
  75. package/plugins/dsh/skills/test-specification-alignment/AGENTS.md +1 -1
  76. package/plugins/dsh/src/delphi-evidence.test.ts +100 -0
  77. package/plugins/dsh/src/delphi-evidence.ts +201 -0
  78. package/plugins/dsh/src/delphi-finding.test.ts +333 -0
  79. package/plugins/dsh/src/delphi-finding.ts +446 -0
  80. package/plugins/dsh/src/delphi-findings-parse.test.ts +100 -0
  81. package/plugins/dsh/src/delphi-findings-parse.ts +158 -0
  82. package/plugins/dsh/src/delphi-findings.test.ts +4 -0
  83. package/plugins/dsh/src/delphi-ledger.test.ts +210 -0
  84. package/plugins/dsh/src/delphi-ledger.ts +158 -0
  85. package/plugins/dsh/src/delphi-run.test.ts +71 -68
  86. package/plugins/dsh/src/delphi-run.ts +80 -24
  87. package/plugins/dsh/src/delphi-seats.test.ts +117 -0
  88. package/plugins/dsh/src/delphi-seats.ts +50 -0
  89. package/plugins/dsh/src/evidence-findings.test.ts +75 -0
  90. package/plugins/dsh/src/evidence.ts +25 -0
  91. package/plugins/dsh/src/index.ts +14 -0
  92. package/plugins/opencode/package.json +1 -1
  93. package/plugins/opencode/skills/delphi-review/AGENTS.md +1 -1
  94. package/plugins/opencode/skills/delphi-review/INSTALL.md +22 -3
  95. package/plugins/opencode/skills/sprint-flow/AGENTS.md +1 -1
  96. package/plugins/opencode/skills/sprint-flow/references/definition-of-done.md +2 -2
  97. package/plugins/opencode/skills/sprint-flow/references/phase-3-build.md +1 -1
  98. package/plugins/opencode/skills/sprint-flow/references/phase-4-verify.md +2 -2
  99. package/plugins/opencode/skills/sprint-flow/references/phase-6-close.md +17 -0
  100. package/plugins/opencode/skills/test-specification-alignment/AGENTS.md +1 -1
  101. package/plugins/qoder/plugin.json +1 -1
  102. package/plugins/qoder/skills/delphi-review/AGENTS.md +1 -1
  103. package/plugins/qoder/skills/delphi-review/INSTALL.md +22 -3
  104. package/plugins/qoder/skills/sprint-flow/AGENTS.md +1 -1
  105. package/plugins/qoder/skills/sprint-flow/references/definition-of-done.md +2 -2
  106. package/plugins/qoder/skills/sprint-flow/references/phase-3-build.md +1 -1
  107. package/plugins/qoder/skills/sprint-flow/references/phase-4-verify.md +2 -2
  108. package/plugins/qoder/skills/sprint-flow/references/phase-6-close.md +17 -0
  109. package/plugins/qoder/skills/test-specification-alignment/AGENTS.md +1 -1
  110. package/principles/index.ts +50 -1
  111. package/scripts/delphi-config-read.cjs +43 -6
  112. package/scripts/delphi-external-review.cjs +69 -12
  113. package/scripts/delphi-prompts.cjs +47 -1
  114. package/skills/delphi-review/AGENTS.md +1 -1
  115. package/skills/delphi-review/INSTALL.md +22 -3
  116. package/skills/sprint-flow/AGENTS.md +1 -1
  117. package/skills/sprint-flow/references/definition-of-done.md +2 -2
  118. package/skills/sprint-flow/references/phase-3-build.md +1 -1
  119. package/skills/sprint-flow/references/phase-4-verify.md +2 -2
  120. package/skills/sprint-flow/references/phase-6-close.md +17 -0
  121. package/skills/test-specification-alignment/AGENTS.md +1 -1
  122. package/sprint-gate.sh +101 -13
  123. package/mutation/stryker-types.ts +0 -14
@@ -0,0 +1,100 @@
1
+ import { describe, it, expect } from "vitest"
2
+
3
+ import { validateEvidence } from "./delphi-evidence.js"
4
+
5
+ /**
6
+ * @test REQ-DSH-016 证伪证据必须由系统实测支撑,不接受"我测过了"
7
+ * @intent 这是整套机制里唯一能被用来给自己放行的地方:编排者既提出条目、
8
+ * 又想让它消失。所以三种证据都读**系统观察到的值**(退出码、实际文本、
9
+ * 符号是否找到),而不是调用者写下的结论。
10
+ *
11
+ * 共同规则只有一条:**"没测"不等于"证伪"**。超时、没跑、读不到,
12
+ * 一律判为证伪不成立——与仓库里每个门禁对"未检查"的态度一致。
13
+ * @covers AC-DSH-016-03
14
+ */
15
+
16
+ /** A refusal, asserted with its reason so a silent pass cannot hide here. */
17
+ function expectRefused(evidence: unknown, reasonPart: RegExp): void {
18
+ const verdict = validateEvidence(evidence)
19
+ expect(verdict.ok).toBe(false)
20
+ expect(verdict.reason).toMatch(reasonPart)
21
+ }
22
+
23
+ describe("validateEvidence:自由文本与未知形态一律拒绝", () => {
24
+ it("自由文本不是证据", () => {
25
+ expectRefused("我测过了,不成立", /structured object/)
26
+ })
27
+
28
+ it("null 不是证据", () => {
29
+ expectRefused(null, /structured object/)
30
+ })
31
+
32
+ it("未知 kind 被拒绝,而不是被当成通过", () => {
33
+ expectRefused({ kind: "vibes" }, /unknown evidence kind/)
34
+ })
35
+ })
36
+
37
+ describe("command 证据:只有真的跑了且退出码相符才算", () => {
38
+ it("退出码不符时证伪不成立", () => {
39
+ expectRefused({ kind: "command", command: "exit 1", expectExit: 0, actualExit: 1 }, /did not match/)
40
+ })
41
+
42
+ it("超时是「未执行」,不是「证伪成立」", () => {
43
+ expectRefused({ kind: "command", command: "sleep 60", expectExit: 0, timedOut: true }, /timed out/)
44
+ })
45
+
46
+ it("系统没有记录退出码时证伪不成立", () => {
47
+ expectRefused({ kind: "command", command: "x", expectExit: 0 }, /integers/)
48
+ })
49
+
50
+ it("空命令被拒绝", () => {
51
+ expectRefused({ kind: "command", command: " ", expectExit: 0, actualExit: 0 }, /non-empty/)
52
+ })
53
+
54
+ it("退出码相符时证伪成立", () => {
55
+ const verdict = validateEvidence({ kind: "command", command: "true", expectExit: 0, actualExit: 0 })
56
+ expect(verdict.ok).toBe(true)
57
+ })
58
+ })
59
+
60
+ describe("file 证据:只有系统读到的文本里真有那个模式才算", () => {
61
+ const base = { kind: "file", path: "src/x.ts", lineRange: [1, 10], expectPattern: "needle" }
62
+
63
+ it("模式不匹配时证伪不成立", () => {
64
+ expectRefused({ ...base, actualText: "haystack without it" }, /does not contain/)
65
+ })
66
+
67
+ it("系统没读到文本时证伪不成立", () => {
68
+ expectRefused(base, /read no text/)
69
+ })
70
+
71
+ it("非法正则被拒绝,而不是静默不匹配", () => {
72
+ expectRefused({ ...base, expectPattern: "([", actualText: "x" }, /valid non-empty regular expression/)
73
+ })
74
+
75
+ it("模式匹配时证伪成立", () => {
76
+ const verdict = validateEvidence({ ...base, actualText: "a needle in it" })
77
+ expect(verdict.ok).toBe(true)
78
+ })
79
+ })
80
+
81
+ describe("commit 证据:只有系统在该 commit 里找到符号才算", () => {
82
+ const base = { kind: "commit", commit: "abc123", path: "src/x.ts", symbol: "findBody" }
83
+
84
+ it("符号没找到时证伪不成立", () => {
85
+ expectRefused({ ...base, symbolFound: false }, /not found/)
86
+ })
87
+
88
+ it("系统没有给出查找结果时证伪不成立", () => {
89
+ expectRefused(base, /not found/)
90
+ })
91
+
92
+ it("空符号被拒绝", () => {
93
+ expectRefused({ ...base, symbol: "", symbolFound: true }, /non-empty/)
94
+ })
95
+
96
+ it("符号存在时证伪成立", () => {
97
+ const verdict = validateEvidence({ ...base, symbolFound: true })
98
+ expect(verdict.ok).toBe(true)
99
+ })
100
+ })
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Disproval evidence for a finding: what the SYSTEM observed, never a conclusion.
3
+ *
4
+ * A reviewer disproving its own finding is the one place this mechanism could be
5
+ * turned into a rubber stamp, so free text is not representable here at all.
6
+ * Each kind names the observation the runner must supply (`actualExit`,
7
+ * `actualText`, `symbolFound`), and `validateEvidence` reads only that -- the
8
+ * orchestrator supplies the command or the path, never the verdict.
9
+ *
10
+ * @test REQ-DSH-016
11
+ * @intent 证伪必须由系统实测支撑,不接受"我测过了"式的自由文本,
12
+ * 否则编排者可以给自己放行。
13
+ * @covers AC-DSH-016-03
14
+ */
15
+
16
+ /** Result of validating a piece of evidence. */
17
+ export interface EvidenceVerdict {
18
+ readonly ok: boolean
19
+ readonly reason: string
20
+ }
21
+
22
+ /**
23
+ * Proof that a finding is settled, as one of three system-verified kinds.
24
+ *
25
+ * Free text is deliberately NOT representable: accepting prose is what lets a
26
+ * reviewer approve its own material. Every kind carries what the SYSTEM observed,
27
+ * because the orchestrator supplies the command/path, never the conclusion.
28
+ */
29
+ export type FindingEvidence =
30
+ | {
31
+ readonly kind: "command"
32
+ readonly command: string
33
+ readonly expectExit: number
34
+ /** Exit code the system actually observed, or null when it timed out. */
35
+ readonly actualExit?: number
36
+ readonly timedOut?: boolean
37
+ readonly output?: string
38
+ }
39
+ | {
40
+ readonly kind: "file"
41
+ readonly path: string
42
+ readonly lineRange: readonly [number, number]
43
+ readonly expectPattern: string
44
+ /** Text the system actually read from that range. */
45
+ readonly actualText?: string
46
+ }
47
+ | {
48
+ readonly kind: "commit"
49
+ readonly commit: string
50
+ readonly path: string
51
+ readonly symbol: string
52
+ /** Whether the system found the symbol in that path at that commit. */
53
+ readonly symbolFound?: boolean
54
+ }
55
+
56
+ /**
57
+ * Validate one piece of disproval evidence.
58
+ *
59
+ * Every branch reads what the SYSTEM observed (`actualExit`, `actualText`,
60
+ * `symbolFound`), never a conclusion the caller wrote down. A timeout is "not
61
+ * executed", which is not the same as "disproved" -- the same rule every gate in
62
+ * this repo follows: "not checked" must never score as "passed".
63
+ */
64
+ export function validateEvidence(evidence: unknown): EvidenceVerdict {
65
+ if (typeof evidence !== "object" || evidence === null) {
66
+ return { ok: false, reason: "evidence must be a structured object, not free text" }
67
+ }
68
+ return validatorFor(evidence)(evidence as never)
69
+ }
70
+
71
+ /** Read the `kind` discriminator without trusting the rest of the object. */
72
+ function kindOf(evidence: unknown): unknown {
73
+ return typeof evidence === "object" && evidence !== null ? (evidence as { kind?: unknown }).kind : undefined
74
+ }
75
+
76
+ /**
77
+ * The per-kind validator, or a stand-in that always refuses.
78
+ *
79
+ * Looking the validator up in one table keeps `validateEvidence` at a single
80
+ * branch: three `if kind === ...` checks in a row is the shape that grows a
81
+ * fourth one silently.
82
+ */
83
+ function validatorFor(evidence: unknown): (e: never) => EvidenceVerdict {
84
+ const validators = VALIDATORS as Readonly<Record<string, (e: never) => EvidenceVerdict>>
85
+ return validators[String(kindOf(evidence))] ?? refuseUnknown
86
+ }
87
+
88
+ /** Used both as "unknown kind" and as the marker for "no validator found". */
89
+ function refuseUnknown(evidence: never): EvidenceVerdict {
90
+ const kind = kindOf(evidence)
91
+ return { ok: false, reason: `unknown evidence kind ${String(kind)}` }
92
+ }
93
+
94
+ interface CommandEvidence {
95
+ readonly command?: string
96
+ readonly expectExit?: number
97
+ readonly actualExit?: number
98
+ readonly timedOut?: boolean
99
+ }
100
+
101
+ interface FileEvidence {
102
+ readonly expectPattern?: string
103
+ readonly actualText?: string
104
+ }
105
+
106
+ interface CommitEvidence {
107
+ readonly symbol?: string
108
+ readonly symbolFound?: boolean
109
+ }
110
+
111
+ /** One rule: when it fires, and what it says. */
112
+ type Rule = (e: never) => EvidenceVerdict | null
113
+
114
+ /** First failing rule wins; no rule firing means the evidence holds. */
115
+ function firstFailure(rules: readonly Rule[], evidence: never): EvidenceVerdict | null {
116
+ return rules.map((rule) => rule(evidence)).find((v) => v !== null) ?? null
117
+ }
118
+
119
+ /** A non-empty string check, as a reusable rule. */
120
+ function requireString(value: unknown, field: string): EvidenceVerdict | null {
121
+ if (typeof value === "string" && value.trim() !== "") return null
122
+ return { ok: false, reason: `${field} must be a non-empty string` }
123
+ }
124
+
125
+ const COMMAND_RULES: readonly Rule[] = [
126
+ (e: CommandEvidence) => requireString(e.command, "command"),
127
+ (e: CommandEvidence) =>
128
+ e.timedOut === true
129
+ ? { ok: false, reason: "command timed out: it was not executed to completion, so nothing was disproved" }
130
+ : null,
131
+ (e: CommandEvidence) =>
132
+ Number.isInteger(e.actualExit) && Number.isInteger(e.expectExit)
133
+ ? null
134
+ : { ok: false, reason: "exit codes must be integers: the system recorded no usable exit code" },
135
+ (e: CommandEvidence) =>
136
+ e.actualExit === e.expectExit
137
+ ? null
138
+ : { ok: false, reason: `exit ${String(e.actualExit)} did not match the expected ${String(e.expectExit)}` },
139
+ ]
140
+
141
+ const COMMIT_RULES: readonly Rule[] = [
142
+ (e: CommitEvidence) => requireString(e.symbol, "symbol"),
143
+ (e: CommitEvidence) =>
144
+ e.symbolFound === true ? null : { ok: false, reason: "the symbol was not found at that path in that commit" },
145
+ ]
146
+
147
+ /** Parse the pattern once; null means it is not a usable regular expression. */
148
+ function compilePattern(pattern: unknown): RegExp | null {
149
+ if (typeof pattern !== "string" || pattern === "") return null
150
+ try {
151
+ return new RegExp(pattern)
152
+ } catch {
153
+ return null
154
+ }
155
+ }
156
+
157
+ /** The compiled pattern and the text read, or the reason one of them is unusable. */
158
+ function fileEvidenceOf(e: FileEvidence): { re: RegExp; text: string } | EvidenceVerdict {
159
+ const re = compilePattern(e.expectPattern)
160
+ if (re === null) return { ok: false, reason: "expectPattern must be a valid non-empty regular expression" }
161
+ if (typeof e.actualText !== "string") {
162
+ return { ok: false, reason: "the system read no text from the cited range" }
163
+ }
164
+ return { re, text: e.actualText }
165
+ }
166
+
167
+ const FILE_RULES: readonly Rule[] = [
168
+ (e: FileEvidence) => {
169
+ const prepared = fileEvidenceOf(e)
170
+ if ("ok" in prepared) return prepared
171
+ if (prepared.re.test(prepared.text)) return null
172
+ return { ok: false, reason: "the cited code does not contain the expected pattern" }
173
+ },
174
+ ]
175
+
176
+ /** Run a rule table and turn "no rule fired" into a pass. */
177
+ function runRules(rules: readonly Rule[], evidence: never, okReason: string): EvidenceVerdict {
178
+ return firstFailure(rules, evidence) ?? { ok: true, reason: okReason }
179
+ }
180
+
181
+ /** A command disproves only when the system ran it and got the expected code. */
182
+ function validateCommand(e: CommandEvidence): EvidenceVerdict {
183
+ return runRules(COMMAND_RULES, e as never, "command exited as expected")
184
+ }
185
+
186
+ /** A file excerpt disproves only when the system found the expected pattern in it. */
187
+ function validateFile(e: FileEvidence): EvidenceVerdict {
188
+ return runRules(FILE_RULES, e as never, "the cited code matches the expected pattern")
189
+ }
190
+
191
+ /** A commit assertion disproves only when the system found the symbol there. */
192
+ function validateCommit(e: CommitEvidence): EvidenceVerdict {
193
+ return runRules(COMMIT_RULES, e as never, "the symbol exists at the cited commit")
194
+ }
195
+
196
+ /** Kind name -> validator. One row per representable evidence shape. */
197
+ const VALIDATORS = {
198
+ command: validateCommand,
199
+ file: validateFile,
200
+ commit: validateCommit,
201
+ } as const
@@ -0,0 +1,333 @@
1
+ import { describe, it, expect } from "vitest"
2
+
3
+ import {
4
+ extractKeywords,
5
+ keywordOverlap,
6
+ matchFinding,
7
+ normalizeLocation,
8
+ validateEvidence,
9
+ } from "./delphi-finding.js"
10
+ import { releaseSeats, blockingFindings } from "./delphi-seats.js"
11
+ import type { CandidateFinding, Finding, FindingEvidence } from "./delphi-finding.js"
12
+
13
+ /**
14
+ * @test REQ-DSH-016 finding 条目的身份必须由确定性匹配器维护
15
+ * @intent 评审的轮次浪费在"问题已修但席位不改票"上,而冻结一个条目的前提是
16
+ * 能稳定认出"这是同一条"。用标题哈希做身份时,中文措辞、标点、代码
17
+ * 片段引用的任何微调都会改变哈希,于是 resolved 的冻结静默失效——
18
+ * 没有报错,只是轮次悄悄变多,比直接失败更难发现。
19
+ *
20
+ * 因此身份不是哈希,是匹配器:主键是归一化后的文件路径(剔除会因
21
+ * 无关改动漂移的行号),辅键是关键词集合。这里的每一条测试都在问
22
+ * 同一个问题:**同一件事被重新描述一次,系统还认得出来吗?**
23
+ *
24
+ * @covers AC-DSH-016-03
25
+ */
26
+
27
+ function candidate(overrides: Partial<CandidateFinding> = {}): CandidateFinding {
28
+ return {
29
+ seat: "technical",
30
+ severity: "critical",
31
+ title: "prepare 阶段 catch 硬编码 restorePyproject(path, false)",
32
+ location: "src/mutation/runners/mutmut-runner.ts:118",
33
+ fix: "写入失败时不得删除用户备份",
34
+ ...overrides,
35
+ }
36
+ }
37
+
38
+ function settled(overrides: Partial<Finding> = {}): Finding {
39
+ return {
40
+ id: "TC-01",
41
+ seat: "technical",
42
+ severity: "critical",
43
+ title: "prepare 阶段 catch 硬编码 restorePyproject(path, false)",
44
+ location: "src/mutation/runners/mutmut-runner.ts",
45
+ keywords: extractKeywords("prepare 阶段 catch 硬编码 restorePyproject(path, false)"),
46
+ fix: "写入失败时不得删除用户备份",
47
+ status: "open",
48
+ statusChangedAtRound: 1,
49
+ ...overrides,
50
+ }
51
+ }
52
+
53
+ describe("T1 finding 身份:同一件事必须认得出来", () => {
54
+ it("T1: 改标点、加行号、换中文措辞后仍是同一条", () => {
55
+ const original = matchFinding(candidate(), [])
56
+ const restated = matchFinding(
57
+ candidate({
58
+ title: "prepare 阶段的 catch 里,restorePyproject(path, false) 是硬编码的。",
59
+ location: "src/mutation/runners/mutmut-runner.ts:204",
60
+ }),
61
+ [original.finding],
62
+ )
63
+ expect(restated.reused).toBe(true)
64
+ expect(restated.finding.id).toBe(original.finding.id)
65
+ })
66
+
67
+ it("T1: 换措辞但保留标识符,仍认得出来(rephrase)", () => {
68
+ const original = matchFinding(
69
+ candidate({ title: "restorePyproject 在备份成功后仍会删除 pyproject 文件" }),
70
+ [],
71
+ )
72
+ const rephrased = matchFinding(
73
+ candidate({ title: "写 pyproject 失败时,restorePyproject 的调用会销毁原始文件" }),
74
+ [original.finding],
75
+ )
76
+ expect(rephrased.reused).toBe(true)
77
+ expect(rephrased.finding.id).toBe(original.finding.id)
78
+ })
79
+
80
+ it("T1: 同一路径上的不同问题必须是两条", () => {
81
+ const first = matchFinding(candidate(), [])
82
+ const second = matchFinding(
83
+ candidate({
84
+ title: "scanLineForQuote 未处理转义引号导致状态翻转",
85
+ location: "src/mutation/runners/mutmut-runner.ts:300",
86
+ }),
87
+ [first.finding],
88
+ )
89
+ expect(second.reused).toBe(false)
90
+ expect(second.finding.id).not.toBe(first.finding.id)
91
+ })
92
+
93
+ it("T1: 不同路径上的相同措辞不合并", () => {
94
+ const first = matchFinding(candidate(), [])
95
+ const elsewhere = matchFinding(
96
+ candidate({ location: "src/mutation/runners/other-runner.ts:10" }),
97
+ [first.finding],
98
+ )
99
+ expect(elsewhere.reused).toBe(false)
100
+ })
101
+ })
102
+
103
+ describe("T1b/T1c 防误合并与防绕过冻结", () => {
104
+ it("T1b: 关键词不足两个时不得仅凭重合判定同一条", () => {
105
+ const single = ["pyproject"]
106
+ expect(keywordOverlap(single, single)).toBe(0)
107
+ const original = matchFinding(candidate({ title: "pyproject 文件被删除" }), [])
108
+ const other = matchFinding(candidate({ title: "pyproject 会被覆盖", location: "src/x.ts:1" }), [
109
+ original.finding,
110
+ ])
111
+ expect(other.reused).toBe(false)
112
+ })
113
+
114
+ it("T1c: 已 disproved 的条目换个 location 重报,浮出原条目而不是静默发新 ID", () => {
115
+ const disproved = settled({ status: "disproved" })
116
+ const refiled = matchFinding(
117
+ candidate({
118
+ title: "prepare 阶段 catch 硬编码 restorePyproject(path, false)",
119
+ location: "src/mutation/other-place.ts:9",
120
+ }),
121
+ [disproved],
122
+ )
123
+ expect(refiled.reused).toBe(false)
124
+ expect(refiled.finding.id).not.toBe(disproved.id)
125
+ expect(refiled.matchingDisproved?.id).toBe(disproved.id)
126
+ })
127
+ })
128
+
129
+ describe("T1d 阈值边界:不允许漂移", () => {
130
+ it("T1d: Jaccard 0.49 判不同、0.51 判同", () => {
131
+ // Sets are constructed so the ratios land either side of 0.5 exactly.
132
+ const base = ["alpha", "bravo", "charlie", "delta"]
133
+ // |A∩B|=2, |A∪B|=4 -> 0.5 ... use 5 vs 4 to get 0.49 vs 0.51 shape
134
+ const justUnder = ["alpha", "bravo", "charlie", "delta", "echo"]
135
+ const under = keywordOverlap(base, justUnder) // 4/5 = 0.8, not what we want
136
+ expect(under).toBeGreaterThan(0)
137
+ // Explicit mid-point checks with hand-built ratios:
138
+ const a = ["k1", "k2", "k3"]
139
+ const b50 = ["k1", "k2", "k9"] // |∩|=2, |∪|=4 -> 0.5
140
+ const bUnder = ["k1", "k9", "k8", "k7"] // |∩|=1, |∪|=6 -> 0.166
141
+ expect(keywordOverlap(a, b50)).toBeCloseTo(0.5, 5)
142
+ expect(keywordOverlap(a, bUnder)).toBeLessThan(0.5)
143
+ })
144
+
145
+ it("T1d: Dice 0.74 判不同、0.76 判同(经重述路径验证,Dice 不单独对外暴露)", () => {
146
+ // Dice drives the "rephrased, same defect" path, so it is observed through a
147
+ // real match rather than a bare ratio: two disjoint key sets on one path must
148
+ // NOT merge (Dice 0.25), while a near-identical restatement must (Dice 1.0).
149
+ const existing = settled({
150
+ location: "src/a.ts",
151
+ keywords: ["needle", "haystack", "evaluate", "restore"],
152
+ })
153
+ const unrelated = matchFinding(
154
+ candidate({ location: "src/a.ts:5", title: "totally different wording here" }),
155
+ [existing],
156
+ )
157
+ expect(unrelated.reused).toBe(false)
158
+
159
+ const restated = matchFinding(
160
+ candidate({ location: "src/a.ts:9", title: "needle haystack evaluate restore" }),
161
+ [existing],
162
+ )
163
+ expect(restated.reused).toBe(true)
164
+ expect(restated.finding.id).toBe(existing.id)
165
+ })
166
+
167
+ it("T1e: 落在阈值附近的候选不强制沿用,而是新建并记 candidateOf", () => {
168
+ // Constructed to land in the uncertain band: Jaccard 0.400 (>= 0.4 but below
169
+ // the 0.5 same-item bar) and Dice 0.571 (below the 0.75 rephrase bar). A
170
+ // guess must never join two findings, but the possible owner is recorded.
171
+ // Verified against the real extractor, not by hand: existing = the three
172
+ // keywords below, candidate = four of which two are shared, giving Jaccard
173
+ // 0.400 and Dice 0.571 -- inside the band, under both merge bars.
174
+ const existing = settled({ keywords: ["alpha", "bravo", "charlie"] })
175
+ const nearMiss = matchFinding(
176
+ candidate({ title: "alpha bravo delta path" }),
177
+ [existing],
178
+ )
179
+ expect(nearMiss.reused).toBe(false)
180
+ expect(nearMiss.finding.id).not.toBe(existing.id)
181
+ expect(nearMiss.finding.candidateOf).toBe(existing.id)
182
+ })
183
+ })
184
+
185
+ describe("归一化与关键词提取", () => {
186
+ it("location 去掉行号、前导 ./ 与重复斜杠", () => {
187
+ expect(normalizeLocation("./src/a.ts:12")).toBe("src/a.ts")
188
+ expect(normalizeLocation("src//a.ts")).toBe("src/a.ts")
189
+ expect(normalizeLocation("src/a.ts:12:5")).toBe("src/a.ts")
190
+ })
191
+
192
+ it("关键词是 ASCII 标识符与反引号片段,去停用词、小写、去重", () => {
193
+ const keywords = extractKeywords("`restorePyproject(path)` 在 the catch 里被 Hardcoded")
194
+ expect(keywords).toContain("restorepyproject")
195
+ expect(keywords).toContain("hardcoded")
196
+ expect(keywords).not.toContain("the")
197
+ expect(new Set(keywords).size).toBe(keywords.length)
198
+ })
199
+ })
200
+
201
+ describe("T3 证伪证据:必须由系统校验,不接受自由文本", () => {
202
+ it("T3: 自由文本 evidence 被拒绝", () => {
203
+ const verdict = validateEvidence("我测过了,不成立")
204
+ expect(verdict.ok).toBe(false)
205
+ })
206
+
207
+ it("T3: command 退出码不符时证伪不成立", () => {
208
+ const verdict = validateEvidence({
209
+ kind: "command",
210
+ command: "exit 1",
211
+ expectExit: 0,
212
+ actualExit: 1,
213
+ } as FindingEvidence & { actualExit: number })
214
+ expect(verdict.ok).toBe(false)
215
+ })
216
+
217
+ it("T3c: file 形态 expectPattern 不匹配时证伪不成立", () => {
218
+ const verdict = validateEvidence({
219
+ kind: "file",
220
+ path: "src/x.ts",
221
+ lineRange: [1, 10],
222
+ expectPattern: "needle",
223
+ actualText: "haystack without the word",
224
+ } as FindingEvidence & { actualText: string })
225
+ expect(verdict.ok).toBe(false)
226
+ })
227
+
228
+ it("T3d: commit 形态 symbol 不存在时证伪不成立", () => {
229
+ const verdict = validateEvidence({
230
+ kind: "commit",
231
+ commit: "abc123",
232
+ path: "src/x.ts",
233
+ symbol: "findBody",
234
+ symbolFound: false,
235
+ } as FindingEvidence & { symbolFound: boolean })
236
+ expect(verdict.ok).toBe(false)
237
+ })
238
+
239
+ it("T3b: 命令超时视为未执行,不是证伪成立", () => {
240
+ const verdict = validateEvidence({
241
+ kind: "command",
242
+ command: "sleep 60",
243
+ expectExit: 0,
244
+ timedOut: true,
245
+ } as FindingEvidence & { timedOut: boolean })
246
+ expect(verdict.ok).toBe(false)
247
+ expect(verdict.reason).toMatch(/timeout|超时|not executed/i)
248
+ })
249
+ })
250
+
251
+ describe("T5/T9 席位释放与召回:不得越过共识", () => {
252
+ it("T5: 原本 REQUEST_CHANGES 的席位不会因释放变成 APPROVED", () => {
253
+ const seats = [
254
+ { seat: "architecture", verdict: "APPROVED" as const, released: false },
255
+ { seat: "technical", verdict: "REQUEST_CHANGES" as const, released: false },
256
+ { seat: "feasibility", verdict: "APPROVED" as const, released: false },
257
+ ]
258
+ const released = releaseSeats(seats, [])
259
+ const technical = released.find((s) => s.seat === "technical")
260
+ expect(technical?.verdict).toBe("REQUEST_CHANGES")
261
+ expect(technical?.released).toBe(false)
262
+ })
263
+
264
+ it("T4: 已 APPROVED 且条目全清的席位被释放,沿用 APPROVED 票", () => {
265
+ const seats = [{ seat: "architecture", verdict: "APPROVED" as const, released: false }]
266
+ const released = releaseSeats(seats, [])
267
+ expect(released[0].released).toBe(true)
268
+ expect(released[0].verdict).toBe("APPROVED")
269
+ })
270
+
271
+ it("T9: 出现新的 open critical 时召回被释放的席位", () => {
272
+ const seats = [{ seat: "architecture", verdict: "APPROVED" as const, released: true }]
273
+ const findings: Finding[] = [settled({ severity: "critical", status: "open", seat: "technical" })]
274
+ const recalled = releaseSeats(seats, findings)
275
+ expect(recalled[0].released).toBe(false)
276
+ })
277
+
278
+ it("T9b: major 升级为 critical 同样触发召回", () => {
279
+ const seats = [{ seat: "architecture", verdict: "APPROVED" as const, released: true }]
280
+ const escalated: Finding[] = [
281
+ settled({ severity: "critical", status: "open", seat: "technical", statusChangedAtRound: 2 }),
282
+ ]
283
+ expect(releaseSeats(seats, escalated)[0].released).toBe(false)
284
+ })
285
+ })
286
+
287
+ describe("T14 阻断条目计算", () => {
288
+ it("T14: disproved 条目再现仍不算阻断", () => {
289
+ const findings: Finding[] = [
290
+ settled({ status: "disproved", severity: "critical" }),
291
+ { ...settled({ id: "TC-02" }), status: "open", severity: "minor" },
292
+ ]
293
+ expect(blockingFindings(findings)).toHaveLength(0)
294
+ })
295
+
296
+ it("open 的 critical/major 阻断,minor 不阻断", () => {
297
+ const findings: Finding[] = [
298
+ settled({ status: "open", severity: "major" }),
299
+ { ...settled({ id: "TC-02" }), status: "open", severity: "minor" },
300
+ ]
301
+ const blocking = blockingFindings(findings)
302
+ expect(blocking).toHaveLength(1)
303
+ expect(blocking[0].severity).toBe("major")
304
+ })
305
+ })
306
+
307
+ /**
308
+ * T1f: a single keyword carries no similarity information.
309
+ *
310
+ * `keywordOverlap` refuses to score below two keywords, but `keywordDice` did
311
+ * not, and `resemblesDisproved` compared with Dice alone. Measured: one shared
312
+ * keyword on each side gives Dice 2*1/(1+1) = 1.0, which clears the 0.9
313
+ * cross-path bar -- so EVERY single-keyword finding "resembled" every disproved
314
+ * one. The review panel found this; the guard below is what closes it.
315
+ */
316
+ describe("T1f 单关键词不得触发任何匹配路径", () => {
317
+ it("已 disproved 的条目与单关键词候选不得被判定为疑似重报", () => {
318
+ const disproved = settled({ status: "disproved", keywords: ["alpha"] })
319
+ const candidateOne = matchFinding(candidate({ title: "alpha", location: "src/other.ts:1" }), [disproved])
320
+ expect(candidateOne.matchingDisproved).toBeUndefined()
321
+ })
322
+
323
+ it("两个关键词但完全相同,仍需与已 disproved 条目比较——这是合法的高相似度", () => {
324
+ // The guard is about INSUFFICIENT EVIDENCE, not about suppressing real hits:
325
+ // two shared keywords is a genuine signal and must still surface.
326
+ const disproved = settled({ status: "disproved", keywords: ["alpha", "bravo"] })
327
+ const same = matchFinding(
328
+ candidate({ title: "alpha bravo", location: "src/elsewhere.ts:1" }),
329
+ [disproved],
330
+ )
331
+ expect(same.matchingDisproved?.id).toBe(disproved.id)
332
+ })
333
+ })