@boyingliu01/xp-gate 0.41.9 → 0.41.52

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 +146 -7
  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,158 @@
1
+ /**
2
+ * Read the findings a seat reported, out of its markdown `### Findings` table.
3
+ *
4
+ * The table already exists -- the prompt has always asked for
5
+ * `ID | Severity | Description | Location | Fix` -- but nothing ever parsed it,
6
+ * so every round re-derived its problems from prose and threw the table away.
7
+ * This turns that table into the ledger's input.
8
+ *
9
+ * Parsing is deliberately forgiving about FORMAT and unforgiving about
10
+ * MEANING: an unreadable row is dropped rather than guessed at, because a
11
+ * finding invented by a parser is worse than one the seat never raised.
12
+ *
13
+ * @test REQ-DSH-016
14
+ * @intent 把席位已经写在 Findings 表里的问题解析成结构化条目,
15
+ * 让后续轮次能说"这条已关闭"而不是"我仍然反对"。
16
+ * @covers AC-DSH-016-03
17
+ */
18
+
19
+ import type { CandidateFinding, Severity } from "./delphi-finding.js"
20
+
21
+ /** One row of the seat's table, before it gets an identity. */
22
+ interface RawRow {
23
+ readonly id: string
24
+ readonly severity: string
25
+ readonly description: string
26
+ readonly location: string
27
+ readonly fix: string
28
+ }
29
+
30
+ /** Column order the prompt asks for; a header may also be read by name. */
31
+ const COLUMNS = ["id", "severity", "description", "location", "fix"] as const
32
+
33
+ /** Severities as the seats spell them, mapped onto ours. */
34
+ const SEVERITY_ALIASES: Readonly<Record<string, Severity>> = {
35
+ critical: "critical",
36
+ blocker: "critical",
37
+ major: "major",
38
+ high: "major",
39
+ minor: "minor",
40
+ low: "minor",
41
+ }
42
+
43
+ /** Split a markdown table row into trimmed cells, dropping the empty edges. */
44
+ function splitRow(line: string): string[] {
45
+ return line
46
+ .trim()
47
+ .replace(/^\|/, "")
48
+ .replace(/\|$/, "")
49
+ .split("|")
50
+ .map((cell) => cell.trim())
51
+ }
52
+
53
+ /** A separator row looks like `|---|---|` and carries no data. */
54
+ function isSeparator(line: string): boolean {
55
+ return /^\|?\s*:?-{2,}/.test(line.trim())
56
+ }
57
+
58
+ /** The `### Findings` section's lines, up to the next heading. */
59
+ function findingsSection(report: string): string[] {
60
+ const lines = report.split(/\r?\n/)
61
+ const start = lines.findIndex((line) => /^#{1,3}\s*Findings\s*$/i.test(line))
62
+ if (start === -1) return []
63
+ const rest = lines.slice(start + 1)
64
+ const end = rest.findIndex((line) => /^#{1,3}\s/.test(line))
65
+ return end === -1 ? rest : rest.slice(0, end)
66
+ }
67
+
68
+ /** The table rows in a findings section, skipping headers and separators. */
69
+ function tableRows(section: readonly string[]): string[][] {
70
+ const rows = section.filter((line) => line.trim().startsWith("|") && !isSeparator(line)).map(splitRow)
71
+ // A header row names the columns; anything else is data.
72
+ const hasHeader = rows.length > 0 && rows[0].some((cell) => /^id$/i.test(cell))
73
+ return hasHeader ? rows.slice(1) : rows
74
+ }
75
+
76
+ /**
77
+ * Whether a row says "the seat found nothing".
78
+ *
79
+ * The row must SAY so: at least one cell is the literal `NONE`. An all-empty row
80
+ * is not a NONE row -- it is a malformed data row, and treating it as "no
81
+ * findings" would swallow a real problem silently, which is the failure this
82
+ * whole module exists to prevent.
83
+ */
84
+ function isNoneRow(cells: readonly string[]): boolean {
85
+ const named = cells.filter((cell) => /^none$/i.test(cell.trim()))
86
+ if (named.length === 0) return false
87
+ return cells.every((cell) => cell.trim() === "" || /^none$/i.test(cell.trim()))
88
+ }
89
+
90
+ /**
91
+ * Map a seat's severity word onto ours.
92
+ *
93
+ * Unknown words fail SAFE: a word this parser does not recognise is treated as
94
+ * MAJOR, not minor. Defaulting to minor let a report escape blocking by spelling
95
+ * "critical" in a way the table does not know, and the whole point of the ledger
96
+ * is that a reported problem cannot be quietly downgraded. Defaulting to critical
97
+ * would also be wrong -- one unrecognised word would block consensus outright.
98
+ */
99
+ function severityOf(word: string): Severity {
100
+ return SEVERITY_ALIASES[word.trim().toLowerCase()] ?? UNKNOWN_SEVERITY
101
+ }
102
+
103
+ /** What an unrecognised severity word counts as; see `severityOf`. */
104
+ const UNKNOWN_SEVERITY: Severity = "major"
105
+
106
+ /** A cell that names no location cannot be matched across rounds. */
107
+ function usableLocation(location: string): boolean {
108
+ return location !== "" && location !== "-" && location !== "—"
109
+ }
110
+
111
+ /** Turn one row into a candidate, or null when it cannot be one. */
112
+ /** A row without a location or a description cannot become a finding. */
113
+ function isUsableRow(row: RawRow): boolean {
114
+ return usableLocation(row.location) && row.description.trim() !== ""
115
+ }
116
+
117
+ function rowToCandidate(row: RawRow, seat: string): CandidateFinding | null {
118
+ if (!isUsableRow(row)) return null
119
+ return {
120
+ seat,
121
+ severity: severityOf(row.severity),
122
+ title: row.description.trim(),
123
+ location: row.location.trim(),
124
+ fix: row.fix.trim(),
125
+ }
126
+ }
127
+
128
+ /** Read the five columns positionally, the order the prompt asks for. */
129
+ function toRawRow(cells: readonly string[]): RawRow {
130
+ return {
131
+ id: cells[0] ?? "",
132
+ severity: cells[1] ?? "",
133
+ description: cells[2] ?? "",
134
+ location: cells[3] ?? "",
135
+ fix: cells[4] ?? "",
136
+ }
137
+ }
138
+
139
+ /**
140
+ * The candidates a seat's report contains.
141
+ *
142
+ * Returns an empty array both for "the seat reported NONE" and for "there was no
143
+ * table at all" -- the caller must not read either as approval, and
144
+ * `parseVerdict` remains the only thing that decides a verdict.
145
+ */
146
+ export function parseFindings(report: string, seat: string): CandidateFinding[] {
147
+ const rows = tableRows(findingsSection(report))
148
+ const candidates: CandidateFinding[] = []
149
+ for (const cells of rows) {
150
+ if (isNoneRow(cells)) continue
151
+ const candidate = rowToCandidate(toRawRow(cells), seat)
152
+ if (candidate !== null) candidates.push(candidate)
153
+ }
154
+ return candidates
155
+ }
156
+
157
+ /** The column order the prompt requires, for error messages and documentation. */
158
+ export const FINDINGS_COLUMNS = COLUMNS
@@ -77,6 +77,10 @@ function outcomeOf(rounds: Array<{ round: number; verdicts: ReturnType<typeof ve
77
77
  consensus: false,
78
78
  finalRatio: 0,
79
79
  totalRounds: mapped.length,
80
+ // This fixture exercises `formatOutcome` (report rendering), not the ledger;
81
+ // an empty ledger keeps the type satisfied without asserting anything about
82
+ // itemization, which has its own suites.
83
+ findings: [],
80
84
  }
81
85
  }
82
86
 
@@ -0,0 +1,210 @@
1
+ import { describe, it, expect } from "vitest"
2
+
3
+ import { closeByAnyOwner, emptyLedger, ledgerPrompt, updateLedger } from "./delphi-ledger.js"
4
+ import { matchFinding } from "./delphi-finding.js"
5
+ import type { Finding } from "./delphi-finding.js"
6
+ import type { RoundResult } from "./delphi.js"
7
+ import type { ExpertVerdict } from "./delphi.js"
8
+
9
+ /**
10
+ * @test REQ-DSH-016 条目台账必须在轮次之间保住"这条已关闭"
11
+ * @intent A 类问题的根因是二值表决表达不了"还是那批旧问题、已经关了"。
12
+ * 台账把这句话变成事实:一轮结束时,席位说 APPROVED 的、它自己提的
13
+ * 条目就被关闭;下一轮的 prompt 明确告诉它"CLOSED 的别再说一遍"。
14
+ *
15
+ * 这里钉住的是 T11/T12 的端到端语义:已修的不再重复阻断,
16
+ * 驳回的可冻结。
17
+ * @covers AC-DSH-016-03
18
+ */
19
+
20
+ const ROW = "| F1 | critical | exit code discarded | src/run.ts:42 | propagate it |"
21
+
22
+ /** A seat report carrying one finding row. */
23
+ function report(row: string = ROW): string {
24
+ return [
25
+ "### Findings",
26
+ "| ID | Severity | Description | Location | Fix |",
27
+ "|---|---|---|---|---|",
28
+ row,
29
+ "",
30
+ "### VERDICT",
31
+ "REQUEST_CHANGES",
32
+ ].join("\n")
33
+ }
34
+
35
+ /** A seat's verdict for one round. */
36
+ function verdict(role: string, v: string, body: string): ExpertVerdict {
37
+ return {
38
+ id: role,
39
+ role,
40
+ requestedModel: `m-${role}`,
41
+ resolvedModel: `r-${role}`,
42
+ verdict: v as "APPROVED" | "REQUEST_CHANGES",
43
+ confidence: 8,
44
+ counted: true,
45
+ report: body,
46
+ }
47
+ }
48
+
49
+ /** One round built from the given verdicts. */
50
+ function round(n: number, verdicts: ExpertVerdict[]): RoundResult {
51
+ const approvals = verdicts.filter((v) => v.verdict === "APPROVED").length
52
+ return {
53
+ round: n,
54
+ verdicts,
55
+ validCount: verdicts.length,
56
+ approvals,
57
+ consensusRatio: approvals / verdicts.length,
58
+ reachedConsensus: false,
59
+ }
60
+ }
61
+
62
+ describe("T11/T12 台账:已修的不再重复阻断,驳回的可冻结", () => {
63
+ it("T11: 席位改判 APPROVED 后,它自己提的条目被关闭", () => {
64
+ // Round 1: technical raises a finding. Round 2: technical approves.
65
+ const first = updateLedger(emptyLedger(), round(1, [verdict("technical", "REQUEST_CHANGES", report())]))
66
+ expect(first.findings).toHaveLength(1)
67
+ expect(first.findings[0].status).toBe("open")
68
+
69
+ const second = updateLedger(
70
+ { findings: first.findings, round: 1 },
71
+ round(2, [verdict("technical", "APPROVED", "### Findings\nNONE")]),
72
+ )
73
+ expect(second.closed).toHaveLength(1)
74
+ expect(second.findings[0].status).toBe("resolved")
75
+ })
76
+
77
+ it("T11: 关闭的条目在后续轮仍然在台账里,不会被悄悄删掉", () => {
78
+ // Dropping it would make a seat that raised it think it was forgotten.
79
+ const first = updateLedger(emptyLedger(), round(1, [verdict("technical", "REQUEST_CHANGES", report())]))
80
+ const second = updateLedger(
81
+ { findings: first.findings, round: 1 },
82
+ round(2, [verdict("technical", "APPROVED", "### Findings\nNONE")]),
83
+ )
84
+ expect(second.findings.map((f) => f.status)).toContain("resolved")
85
+ })
86
+
87
+ it("T11: 另一个席位说 APPROVED 不会关掉别人提的条目", () => {
88
+ // Only the seat that RAISED a finding can settle it -- otherwise one
89
+ // agreeable seat could silently clear another's objection.
90
+ const first = updateLedger(emptyLedger(), round(1, [verdict("technical", "REQUEST_CHANGES", report())]))
91
+ const second = updateLedger(
92
+ { findings: first.findings, round: 1 },
93
+ round(2, [
94
+ verdict("technical", "REQUEST_CHANGES", ""),
95
+ verdict("architecture", "APPROVED", "### Findings\nNONE"),
96
+ ]),
97
+ )
98
+ expect(second.findings[0].status).toBe("open")
99
+ })
100
+
101
+ it("T12: 同一席位下一轮重提同一条时,复用原 ID 而不是新建", () => {
102
+ const first = updateLedger(emptyLedger(), round(1, [verdict("technical", "REQUEST_CHANGES", report())]))
103
+ const second = updateLedger(
104
+ { findings: first.findings, round: 1 },
105
+ round(2, [
106
+ verdict(
107
+ "technical",
108
+ "REQUEST_CHANGES",
109
+ report("| F9 | critical | the exit code from the command is discarded | src/run.ts:77 | keep it |"),
110
+ ),
111
+ ]),
112
+ )
113
+ expect(second.opened).toHaveLength(0)
114
+ expect(second.findings).toHaveLength(1)
115
+ })
116
+
117
+ it("两个席位在同一路径报同一个问题时合并为一条,seat 记为 multi", () => {
118
+ const only = updateLedger(emptyLedger(), round(1, [verdict("technical", "REQUEST_CHANGES", report())]))
119
+ const merged = updateLedger(
120
+ { findings: only.findings, round: 1 },
121
+ round(2, [verdict("architecture", "REQUEST_CHANGES", report())]),
122
+ )
123
+ expect(merged.findings).toHaveLength(1)
124
+ expect(merged.findings[0].seat).toBe("multi")
125
+ })
126
+ })
127
+
128
+ describe("台账 prompt:把 CLOSED 说清楚,否则席位会重提", () => {
129
+ it("空台账不产生 prompt 段", () => {
130
+ expect(ledgerPrompt(emptyLedger())).toBe("")
131
+ })
132
+
133
+ it("prompt 里列出 OPEN 与 RESOLVED 两种状态", () => {
134
+ const first = updateLedger(emptyLedger(), round(1, [verdict("technical", "REQUEST_CHANGES", report())]))
135
+ const second = updateLedger(
136
+ { findings: first.findings, round: 1 },
137
+ round(2, [verdict("technical", "APPROVED", "### Findings\nNONE")]),
138
+ )
139
+ const prompt = ledgerPrompt({ findings: second.findings, round: 2 })
140
+ expect(prompt).toContain("RESOLVED")
141
+ expect(prompt).toMatch(/Do not raise them again/)
142
+ })
143
+
144
+ it("prompt 报出仍在阻断的条目数", () => {
145
+ const first = updateLedger(emptyLedger(), round(1, [verdict("technical", "REQUEST_CHANGES", report())]))
146
+ const prompt = ledgerPrompt({ findings: first.findings, round: 1 })
147
+ expect(prompt).toContain("1 still blocking")
148
+ })
149
+ })
150
+
151
+ describe("台账与匹配器的一致性", () => {
152
+ it("台账里的 id 由匹配器分配,报告的 ID 列被忽略", () => {
153
+ const first = updateLedger(emptyLedger(), round(1, [verdict("technical", "REQUEST_CHANGES", report())]))
154
+ // The seat wrote "F1"; the ledger must use its own prefix scheme.
155
+ expect(first.findings[0].id).toBe("TC-01")
156
+ })
157
+
158
+ it("匹配器对手写台账条目也能复用(同一路径同一描述)", () => {
159
+ const ledger = updateLedger(emptyLedger(), round(1, [verdict("technical", "REQUEST_CHANGES", report())]))
160
+ const again = matchFinding(
161
+ {
162
+ seat: "technical",
163
+ severity: "critical",
164
+ title: "exit code discarded",
165
+ location: "src/run.ts:42",
166
+ fix: "propagate it",
167
+ },
168
+ ledger.findings,
169
+ )
170
+ expect(again.reused).toBe(true)
171
+ })
172
+ })
173
+
174
+ /**
175
+ * T16: a finding two seats raised must still be closable.
176
+ *
177
+ * The first review batch made `seat` become "multi" when two seats report the
178
+ * same defect. This module then closed by `f.seat === verdict.role`, and "multi"
179
+ * equals no role -- so a shared finding could NEVER be settled. Measured: it
180
+ * stays open forever, blocks consensus forever, and the review loops until the
181
+ * round cap. The panel found it 0/3 across five rounds.
182
+ */
183
+ describe("T16 multi 条目必须能被关闭", () => {
184
+ /** One report carrying the same finding, so the second seat makes it multi. */
185
+ const SHARED = report()
186
+
187
+ /** Round 1 from technical, round 2 from architecture: one shared id. */
188
+ function sharedFinding(): readonly Finding[] {
189
+ const first = updateLedger(emptyLedger(), round(1, [verdict("technical", "REQUEST_CHANGES", SHARED)]))
190
+ return updateLedger(
191
+ { findings: first.findings, round: 1 },
192
+ round(2, [verdict("architecture", "REQUEST_CHANGES", SHARED)]),
193
+ ).findings
194
+ }
195
+
196
+ it("两个席位报同一条后,任一原席位批准即可关闭", () => {
197
+ const merged = sharedFinding()
198
+ expect(merged).toHaveLength(1)
199
+ expect(merged[0].seat).toBe("multi")
200
+ expect(closeByAnyOwner(merged, "technical")).toBe(1)
201
+ })
202
+
203
+ it("multi 条目在没有任何原席位批准时保持 open", () => {
204
+ expect(closeByAnyOwner(sharedFinding(), "feasibility")).toBe(0)
205
+ })
206
+
207
+ it("两个原席位都记录在 raisedBy 里,不只是先到的那个", () => {
208
+ expect(sharedFinding()[0].raisedBy).toEqual(["technical", "architecture"])
209
+ })
210
+ })
@@ -0,0 +1,158 @@
1
+ /**
2
+ * The ledger a review keeps between rounds.
3
+ *
4
+ * One round's reports go in; a ledger plus a prompt section come out. Keeping
5
+ * this out of `runReview` matters for two reasons: `runReview` is already the
6
+ * most complex function in the module, and the ledger's rules (merge, freeze,
7
+ * escalate) must be testable WITHOUT making three model calls.
8
+ *
9
+ * @test REQ-DSH-016
10
+ * @intent 把每轮的席位报告累积成一份跨轮的条目台账,使"这条已关闭"
11
+ * 成为可判定的事实,而不是靠席位改变措辞。
12
+ * @covers AC-DSH-016-03
13
+ */
14
+
15
+ import { matchFinding } from "./delphi-finding.js"
16
+ import type { Finding } from "./delphi-finding.js"
17
+ import { parseFindings } from "./delphi-findings-parse.js"
18
+ import { blockingFindings } from "./delphi-seats.js"
19
+ import type { ExpertVerdict, RoundResult } from "./delphi.js"
20
+
21
+ /** What one round did to the ledger. */
22
+ export interface LedgerUpdate {
23
+ /** The ledger after this round. */
24
+ readonly findings: readonly Finding[]
25
+ /** Findings that were open before this round and closed by it. */
26
+ readonly closed: readonly Finding[]
27
+ /** Findings that became newly open in this round. */
28
+ readonly opened: readonly Finding[]
29
+ }
30
+
31
+ /** The ledger, plus the round it was last updated in. */
32
+ export interface Ledger {
33
+ readonly findings: readonly Finding[]
34
+ readonly round: number
35
+ }
36
+
37
+ /** An empty ledger: nothing has been reported yet. */
38
+ export function emptyLedger(): Ledger {
39
+ return { findings: [], round: 0 }
40
+ }
41
+
42
+ /**
43
+ * Findings that must not count as blockers any more.
44
+ *
45
+ * A finding is settled when its seat now APPROVES: the seat that raised it is
46
+ * the only one that can say it is satisfied, and the verdict is the only
47
+ * signal the protocol has ever had. Everything else stays open.
48
+ */
49
+ function settledBy(verdict: ExpertVerdict, findings: readonly Finding[]): Finding[] {
50
+ if (verdict.verdict !== "APPROVED") return []
51
+ const owned = findings.filter((f) => f.status === "open" && ownedBy(f, verdict.role))
52
+ return owned.map((f) => ({ ...f, status: "resolved" as const, statusChangedAtRound: f.statusChangedAtRound }))
53
+ }
54
+
55
+ /**
56
+ * Whether `role` is one of the seats that raised this finding.
57
+ *
58
+ * `seat` collapses to "multi" once two seats agree, and "multi" equals no role --
59
+ * so closing on `seat` alone made every shared finding impossible to settle, and
60
+ * it blocked consensus until the round cap. The owner SET is the truth; `seat`
61
+ * stays as the display name only. A legacy entry without `raisedBy` falls back to
62
+ * its single seat.
63
+ */
64
+ function ownedBy(finding: Finding, role: string): boolean {
65
+ if (finding.raisedBy !== undefined) return finding.raisedBy.includes(role)
66
+ return finding.seat === role || finding.seat === "multi"
67
+ }
68
+
69
+ /**
70
+ * How many of `findings` a fresh approval from `role` would close.
71
+ *
72
+ * Exported so the shared-ownership rule can be asserted without scripting a full
73
+ * review -- a regression here is invisible until a review silently never ends.
74
+ */
75
+ export function closeByAnyOwner(findings: readonly Finding[], role: string): number {
76
+ return findings.filter((f) => f.status === "open" && ownedBy(f, role)).length
77
+ }
78
+
79
+ /** Apply a closing pass over the ledger, keeping non-owned findings untouched. */
80
+ function closeOwned(ledger: readonly Finding[], closed: readonly Finding[]): Finding[] {
81
+ const closedIds = new Set(closed.map((f) => f.id))
82
+ return ledger.map((f) => closedIds.has(f.id) ? { ...f, status: "resolved" as const } : f)
83
+ }
84
+
85
+ /**
86
+ * Fold one round's reports into the ledger.
87
+ *
88
+ * Order matters: close what the seats are now satisfied with FIRST, then add
89
+ * this round's rows, so a finding that was just closed does not get reopened by
90
+ * the same report that closed it.
91
+ */
92
+ export function updateLedger(ledger: Ledger, round: RoundResult): LedgerUpdate {
93
+ const closed = round.verdicts.flatMap((v) => settledBy(v, ledger.findings))
94
+ const afterClose = closeOwned(ledger.findings, closed)
95
+ const added = round.verdicts.reduce<Absorbed>((acc, v) => absorb(acc, v), { findings: afterClose, opened: [] })
96
+ return { findings: added.findings, closed, opened: added.opened }
97
+ }
98
+
99
+ /** The ledger as one seat's report leaves it. */
100
+ interface Absorbed {
101
+ readonly findings: readonly Finding[]
102
+ readonly opened: readonly Finding[]
103
+ }
104
+
105
+ /** Fold one seat's rows into the ledger being accumulated. */
106
+ function absorb(acc: Absorbed, verdict: ExpertVerdict): Absorbed {
107
+ return parseFindings(verdict.report, verdict.role).reduce<Absorbed>((inner, candidate) => {
108
+ const outcome = matchFinding(candidate, inner.findings)
109
+ const opened = outcome.reused ? inner.opened : [...inner.opened, outcome.finding]
110
+ return { findings: replaceOrAppend(inner.findings, outcome.finding), opened }
111
+ }, acc)
112
+ }
113
+
114
+ /** Keep ledger order stable: replace in place when the id already exists. */
115
+ function replaceOrAppend(ledger: readonly Finding[], finding: Finding): Finding[] {
116
+ const index = ledger.findIndex((f) => f.id === finding.id)
117
+ if (index === -1) return [...ledger, finding]
118
+ const next = [...ledger]
119
+ next[index] = finding
120
+ return next
121
+ }
122
+
123
+ /** One ledger row in the prompt: what it is, how bad, and whether it is closed. */
124
+ function ledgerRow(finding: Finding): string {
125
+ const state = finding.status === "open" ? "OPEN" : finding.status.toUpperCase()
126
+ return `| ${finding.id} | ${finding.severity} | ${state} | ${finding.title.slice(0, 80)} |`
127
+ }
128
+
129
+ /**
130
+ * The prompt section a later round receives.
131
+ *
132
+ * Closed rows are listed rather than silently dropped: a seat that reported a
133
+ * problem needs to see that the system recorded it as fixed, otherwise its
134
+ * absence reads as "forgotten" and it re-raises the same issue.
135
+ */
136
+ export function ledgerPrompt(ledger: Ledger): string {
137
+ if (ledger.findings.length === 0) return ""
138
+ return `\n\n## Finding ledger (${countLine(ledger.findings)})\n\n${tableOf(ledger.findings)}\n\n${LEDGER_INSTRUCTIONS}`
139
+ }
140
+
141
+ /** The heading's parenthetical: how much there is, and how much still blocks. */
142
+ function countLine(findings: readonly Finding[]): string {
143
+ const blocking = blockingFindings(findings)
144
+ return `${findings.length} entries, ${blocking.length} still blocking`
145
+ }
146
+
147
+ /** The ledger as a markdown table, one row per finding. */
148
+ function tableOf(findings: readonly Finding[]): string {
149
+ const rows = findings.map(ledgerRow).join("\n")
150
+ return `| ID | Severity | State | Description |\n|---|---|---|---|\n${rows}`
151
+ }
152
+
153
+ /** What the seat must do with the table; worded once, used by every later round. */
154
+ const LEDGER_INSTRUCTIONS =
155
+ `Entries marked RESOLVED or DISPROVED are CLOSED. Do not raise them again: ` +
156
+ `re-raising a closed entry is not a new finding. If a closed entry is still wrong, ` +
157
+ `state it as a NEW row with a different location.\n` +
158
+ `Reply with only the entries that are STILL OPEN and belong to you, in the same table shape.`