oh-my-opencode 5.1.46 → 5.1.48

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 (124) hide show
  1. package/.agents/command/publish.md +3 -4
  2. package/.agents/skills/publish/SKILL.md +3 -4
  3. package/.opencode/command/publish.md +3 -4
  4. package/dist/agents/atlas/agent.d.ts +3 -2
  5. package/dist/agents/sisyphus/index.d.ts +1 -0
  6. package/dist/agents/sisyphus/kimi-k3-1.d.ts +9 -0
  7. package/dist/agents/sisyphus-agent-factory.d.ts +1 -1
  8. package/dist/agents/sisyphus-junior/agent.d.ts +1 -1
  9. package/dist/agents/sisyphus-junior/index.d.ts +1 -0
  10. package/dist/agents/sisyphus-junior/kimi-k3-1.d.ts +6 -0
  11. package/dist/agents/types.d.ts +2 -2
  12. package/dist/cli/index.js +30 -22
  13. package/dist/cli/service-auth/keystore.d.ts +8 -3
  14. package/dist/cli-node/index.js +30 -22
  15. package/dist/index.js +635 -18
  16. package/dist/skills/browser/runtime/omowright/manifest.json +1 -1
  17. package/dist/skills/ulw-research/SKILL.md +59 -2
  18. package/dist/skills/ulw-research/references/deliverable-phase.md +57 -8
  19. package/dist/skills/ulw-research/references/report-gates.md +2 -1
  20. package/dist/skills/ulw-research/scripts/contracts.mjs +3 -11
  21. package/dist/skills/ulw-research/scripts/contracts.test.ts +1 -0
  22. package/dist/skills/ulw-research/scripts/copy-returns-fidelity-policy.test.ts +156 -0
  23. package/dist/skills/ulw-research/scripts/copy-returns-polish.test.ts +209 -0
  24. package/dist/skills/ulw-research/scripts/fact-commands-fill.mjs +86 -0
  25. package/dist/skills/ulw-research/scripts/fact-commands-meaning.mjs +21 -44
  26. package/dist/skills/ulw-research/scripts/fact-commands-polish.mjs +132 -0
  27. package/dist/skills/ulw-research/scripts/fact-commands-verify.mjs +37 -4
  28. package/dist/skills/ulw-research/scripts/fact-commands.mjs +8 -47
  29. package/dist/skills/ulw-research/scripts/fact-fill.mjs +3 -2
  30. package/dist/skills/ulw-research/scripts/fact-pairs.mjs +7 -9
  31. package/dist/skills/ulw-research/scripts/fact-pairs.test.ts +7 -23
  32. package/dist/skills/ulw-research/scripts/fidelity-policy.mjs +183 -0
  33. package/dist/skills/ulw-research/scripts/references-fences.test.ts +25 -0
  34. package/dist/skills/ulw-research/scripts/report-tools.test.ts +329 -1
  35. package/dist/skills/ulw-research/scripts/source-commands.mjs +8 -3
  36. package/dist/skills/ulw-research/scripts/tests/fixtures/fidelity/polish-edits.json +12 -0
  37. package/dist/tui.js +14 -14
  38. package/package.json +14 -14
  39. package/packages/omo-codex/plugin/.codex-plugin/plugin.json +1 -1
  40. package/packages/omo-codex/plugin/components/bootstrap/dist/cli.js +1 -2
  41. package/packages/omo-codex/plugin/components/bootstrap/hooks/hooks.json +1 -1
  42. package/packages/omo-codex/plugin/components/bootstrap/package.json +1 -1
  43. package/packages/omo-codex/plugin/components/comment-checker/hooks/hooks.json +1 -1
  44. package/packages/omo-codex/plugin/components/comment-checker/package.json +1 -1
  45. package/packages/omo-codex/plugin/components/git-bash/hooks/hooks.json +2 -2
  46. package/packages/omo-codex/plugin/components/git-bash/package.json +1 -1
  47. package/packages/omo-codex/plugin/components/lazycodex-executor-verify/hooks/hooks.json +1 -1
  48. package/packages/omo-codex/plugin/components/lazycodex-executor-verify/package.json +1 -1
  49. package/packages/omo-codex/plugin/components/lsp/dist/.omo-runtime-manifest.json +2 -2
  50. package/packages/omo-codex/plugin/components/lsp/hooks/hooks.json +2 -2
  51. package/packages/omo-codex/plugin/components/lsp/package.json +1 -1
  52. package/packages/omo-codex/plugin/components/rules/hooks/hooks.json +4 -4
  53. package/packages/omo-codex/plugin/components/rules/package.json +1 -1
  54. package/packages/omo-codex/plugin/components/teammode/hooks/hooks.json +1 -1
  55. package/packages/omo-codex/plugin/components/teammode/package.json +1 -1
  56. package/packages/omo-codex/plugin/components/telemetry/hooks/hooks.json +1 -1
  57. package/packages/omo-codex/plugin/components/telemetry/package.json +1 -1
  58. package/packages/omo-codex/plugin/components/ultrawork/hooks/hooks.json +1 -1
  59. package/packages/omo-codex/plugin/components/ultrawork/package.json +1 -1
  60. package/packages/omo-codex/plugin/components/ulw-execute-continuation/hooks/hooks.json +1 -1
  61. package/packages/omo-codex/plugin/components/ulw-execute-continuation/package.json +1 -1
  62. package/packages/omo-codex/plugin/components/ulw-loop/hooks/hooks.json +5 -5
  63. package/packages/omo-codex/plugin/components/ulw-loop/package.json +1 -1
  64. package/packages/omo-codex/plugin/hooks/post-compact-resetting-git-bash-mcp-reminder.json +1 -1
  65. package/packages/omo-codex/plugin/hooks/post-compact-resetting-lsp-diagnostics-cache.json +1 -1
  66. package/packages/omo-codex/plugin/hooks/post-compact-resetting-project-rule-cache.json +1 -1
  67. package/packages/omo-codex/plugin/hooks/post-tool-use-checking-comments.json +1 -1
  68. package/packages/omo-codex/plugin/hooks/post-tool-use-checking-lsp-diagnostics.json +1 -1
  69. package/packages/omo-codex/plugin/hooks/post-tool-use-checking-thread-title-hygiene.json +1 -1
  70. package/packages/omo-codex/plugin/hooks/post-tool-use-matching-project-rules.json +1 -1
  71. package/packages/omo-codex/plugin/hooks/post-tool-use-recording-spawn-admission.json +1 -1
  72. package/packages/omo-codex/plugin/hooks/pre-tool-use-enforcing-unlimited-goal-budget.json +1 -1
  73. package/packages/omo-codex/plugin/hooks/pre-tool-use-guarding-ulw-loop-spawns.json +1 -1
  74. package/packages/omo-codex/plugin/hooks/pre-tool-use-recommending-git-bash-mcp.json +1 -1
  75. package/packages/omo-codex/plugin/hooks/session-start-checking-auto-update.json +1 -1
  76. package/packages/omo-codex/plugin/hooks/session-start-checking-bootstrap-provisioning.json +1 -1
  77. package/packages/omo-codex/plugin/hooks/session-start-loading-project-rules.json +1 -1
  78. package/packages/omo-codex/plugin/hooks/session-start-recording-session-telemetry.json +1 -1
  79. package/packages/omo-codex/plugin/hooks/stop-checking-ulw-execute-continuation.json +1 -1
  80. package/packages/omo-codex/plugin/hooks/stop-checking-ulw-loop-resume.json +1 -1
  81. package/packages/omo-codex/plugin/hooks/subagent-stop-verifying-lazycodex-executor-evidence.json +1 -1
  82. package/packages/omo-codex/plugin/hooks/user-prompt-submit-checking-ultrawork-trigger.json +1 -1
  83. package/packages/omo-codex/plugin/hooks/user-prompt-submit-checking-ulw-loop-steering.json +1 -1
  84. package/packages/omo-codex/plugin/hooks/user-prompt-submit-loading-project-rules.json +1 -1
  85. package/packages/omo-codex/plugin/package-lock.json +12 -12
  86. package/packages/omo-codex/plugin/package.json +1 -1
  87. package/packages/omo-codex/plugin/scripts/sync-skills.mjs +1 -1
  88. package/packages/omo-codex/plugin/skills/browser/runtime/omowright/manifest.json +1 -1
  89. package/packages/omo-codex/plugin/skills/ulw-research/SKILL.md +58 -1
  90. package/packages/omo-codex/plugin/skills/ulw-research/references/deliverable-phase.md +57 -8
  91. package/packages/omo-codex/plugin/skills/ulw-research/references/report-gates.md +2 -1
  92. package/packages/omo-codex/plugin/skills/ulw-research/scripts/contracts.mjs +3 -11
  93. package/packages/omo-codex/plugin/skills/ulw-research/scripts/fact-commands-fill.mjs +86 -0
  94. package/packages/omo-codex/plugin/skills/ulw-research/scripts/fact-commands-meaning.mjs +21 -44
  95. package/packages/omo-codex/plugin/skills/ulw-research/scripts/fact-commands-polish.mjs +132 -0
  96. package/packages/omo-codex/plugin/skills/ulw-research/scripts/fact-commands-verify.mjs +37 -4
  97. package/packages/omo-codex/plugin/skills/ulw-research/scripts/fact-commands.mjs +8 -47
  98. package/packages/omo-codex/plugin/skills/ulw-research/scripts/fact-fill.mjs +3 -2
  99. package/packages/omo-codex/plugin/skills/ulw-research/scripts/fact-pairs.mjs +7 -9
  100. package/packages/omo-codex/plugin/skills/ulw-research/scripts/fidelity-policy.mjs +183 -0
  101. package/packages/omo-codex/plugin/skills/ulw-research/scripts/source-commands.mjs +8 -3
  102. package/packages/omo-codex/plugin/test/sync-skills-test-support.mjs +1 -1
  103. package/packages/omo-codex/scripts/install-dist/install-local.mjs +4 -5
  104. package/packages/shared-skills/skills/browser/runtime/omowright/manifest.json +1 -1
  105. package/packages/shared-skills/skills/ulw-research/SKILL.md +59 -2
  106. package/packages/shared-skills/skills/ulw-research/references/deliverable-phase.md +57 -8
  107. package/packages/shared-skills/skills/ulw-research/references/report-gates.md +2 -1
  108. package/packages/shared-skills/skills/ulw-research/scripts/contracts.mjs +3 -11
  109. package/packages/shared-skills/skills/ulw-research/scripts/contracts.test.ts +1 -0
  110. package/packages/shared-skills/skills/ulw-research/scripts/copy-returns-fidelity-policy.test.ts +156 -0
  111. package/packages/shared-skills/skills/ulw-research/scripts/copy-returns-polish.test.ts +209 -0
  112. package/packages/shared-skills/skills/ulw-research/scripts/fact-commands-fill.mjs +86 -0
  113. package/packages/shared-skills/skills/ulw-research/scripts/fact-commands-meaning.mjs +21 -44
  114. package/packages/shared-skills/skills/ulw-research/scripts/fact-commands-polish.mjs +132 -0
  115. package/packages/shared-skills/skills/ulw-research/scripts/fact-commands-verify.mjs +37 -4
  116. package/packages/shared-skills/skills/ulw-research/scripts/fact-commands.mjs +8 -47
  117. package/packages/shared-skills/skills/ulw-research/scripts/fact-fill.mjs +3 -2
  118. package/packages/shared-skills/skills/ulw-research/scripts/fact-pairs.mjs +7 -9
  119. package/packages/shared-skills/skills/ulw-research/scripts/fact-pairs.test.ts +7 -23
  120. package/packages/shared-skills/skills/ulw-research/scripts/fidelity-policy.mjs +183 -0
  121. package/packages/shared-skills/skills/ulw-research/scripts/references-fences.test.ts +25 -0
  122. package/packages/shared-skills/skills/ulw-research/scripts/report-tools.test.ts +329 -1
  123. package/packages/shared-skills/skills/ulw-research/scripts/source-commands.mjs +8 -3
  124. package/packages/shared-skills/skills/ulw-research/scripts/tests/fixtures/fidelity/polish-edits.json +12 -0
@@ -0,0 +1,209 @@
1
+ // Architecture contract for the polish command (#11160 sweep): the edit contract, the ledger
2
+ // loader, the edition-format grammar, and the severity buckets each have one owner export, so a
3
+ // reintroduced local copy in fact-commands-polish.mjs fails one of these tests.
4
+ import { afterAll, afterEach, beforeEach, describe, expect, test } from "bun:test"
5
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"
6
+ import { tmpdir } from "node:os"
7
+ import { join } from "node:path"
8
+
9
+ import { CliError } from "./cli-support.mjs"
10
+ import { countDefects, DEFECT_CODES, SEVERITIES } from "./contracts.mjs"
11
+ import { runFactMeaning } from "./fact-commands-meaning.mjs"
12
+ import * as polishCommand from "./fact-commands-polish.mjs"
13
+ import { editionFormatOf, ledgerFrom, sessionOf } from "./fact-commands-verify.mjs"
14
+ import { applyEdits, POLISH_EDIT_FIELDS, POLISH_MARKUP_REASON, validatePolishEdits } from "./polish-apply.mjs"
15
+
16
+ const { polishDiffFails, runFactPolish } = polishCommand
17
+ const source = (name: string) => readFileSync(join(import.meta.dir, name), "utf8")
18
+ const LEDGER = { schemaVersion: 1, rows: [], events: [] }
19
+ const doc = (body: string) => `<html><body>\n${body}\n</body></html>`
20
+ const BRIEF = join(import.meta.dir, "tests", "fixtures", "fidelity-brief-locked.md")
21
+
22
+ const dir = mkdtempSync(join(tmpdir(), "polish-copy-returns-"))
23
+ const file = (name: string, content: string) => {
24
+ const path = join(dir, name)
25
+ writeFileSync(path, content)
26
+ return path
27
+ }
28
+ const ledgerPath = file("ledger.json", JSON.stringify(LEDGER))
29
+ const badLedgerPath = file("bad-ledger.json", JSON.stringify({ schemaVersion: 1, rows: "nope" }))
30
+ const reportPath = file("report.html", doc("<p>The span is 320 m [S1].</p>"))
31
+ const savedSession = process.env.SESSION_DIR
32
+ beforeEach(() => {
33
+ delete process.env.SESSION_DIR
34
+ })
35
+ afterEach(() => {
36
+ if (savedSession === undefined) delete process.env.SESSION_DIR
37
+ else process.env.SESSION_DIR = savedSession
38
+ })
39
+ afterAll(() => rmSync(dir, { recursive: true, force: true }))
40
+
41
+ function outcome(run: () => unknown) {
42
+ try {
43
+ run()
44
+ return "ok"
45
+ } catch (error) {
46
+ if (!(error instanceof CliError)) throw error
47
+ return error.message
48
+ }
49
+ }
50
+
51
+ describe("the polish edit contract is polish-apply's", () => {
52
+ const SHAPES = [
53
+ ["not an array", { before: "a", after: "b" }],
54
+ ["a null edit", [null]],
55
+ ["an array edit", [["a", "b"]]],
56
+ ["a missing after", [{ before: "span" }]],
57
+ ["a numeric before", [{ before: 3, after: "b" }]],
58
+ ["a numeric reason", [{ before: "span", after: "span", reason: 5 }]],
59
+ ["a valid edit", [{ before: "The span is", after: "The span is", reason: "none" }]],
60
+ ["a valid edit without reason", [{ before: "The span is", after: "The span is" }]],
61
+ ] as const
62
+
63
+ test("#given the command module #when its edit contract is read #then it is the polish-apply export", () => {
64
+ expect(polishCommand.validatePolishEdits).toBe(validatePolishEdits)
65
+ expect(polishCommand.POLISH_EDIT_FIELDS).toBe(POLISH_EDIT_FIELDS)
66
+ expect(source("fact-commands-polish.mjs")).not.toMatch(/typeof edit\?\.(before|after)/u)
67
+ })
68
+
69
+ for (const [name, edits] of SHAPES) {
70
+ test(`#given ${name} #when applied directly and through the CLI #then both accept or both refuse`, () => {
71
+ const editsPath = file(`edits-${name.replaceAll(" ", "-")}.json`, JSON.stringify(edits))
72
+ const direct = (() => {
73
+ try {
74
+ applyEdits(readFileSync(reportPath, "utf8"), edits as never, LEDGER)
75
+ return "ok"
76
+ } catch (error) {
77
+ expect(error).toBeInstanceOf(TypeError)
78
+ return "refused"
79
+ }
80
+ })()
81
+ const cli = outcome(() => runFactPolish("polish-apply", [reportPath, "--edits", editsPath, "--ledger", ledgerPath, "--brief", BRIEF, "--out", join(dir, "out.html")]))
82
+ expect(cli === "ok" ? "ok" : "refused").toBe(direct)
83
+ if (cli !== "ok") expect(cli).toContain(editsPath)
84
+ })
85
+ }
86
+ })
87
+
88
+ describe("the ledger loader is one shared helper", () => {
89
+ const pairs = (extra: string[]) => () => runFactMeaning("pairs", [reportPath, "--brief", BRIEF, "--out", join(dir, "pairs.json"), ...extra])
90
+ const diff = (extra: string[]) => () => runFactPolish("polish-diff", ["--before", reportPath, "--after", reportPath, "--brief", BRIEF, ...extra])
91
+
92
+ test("#given the command modules #when read #then neither defines its own ledger loader or session resolver", () => {
93
+ expect(polishCommand.ledgerFrom).toBe(ledgerFrom)
94
+ for (const name of ["fact-commands-meaning.mjs", "fact-commands-polish.mjs"]) {
95
+ const text = source(name)
96
+ expect(text, name).not.toMatch(/function (readLedgerFrom|ledgerFrom)\b|validateLedger\(/u)
97
+ expect(text, name).not.toMatch(/const sessionOf\s*=/u)
98
+ }
99
+ })
100
+
101
+ test("#given the same valid, invalid, and missing ledgers #when fact pairs and fact polish-diff load them #then both give the same outcome", () => {
102
+ for (const extra of [["--ledger", ledgerPath], ["--ledger", badLedgerPath], []]) {
103
+ const expected = outcome(() => ledgerFrom({ ...(extra.length > 0 ? { ledger: extra[1] } : {}) }))
104
+ expect(outcome(pairs(extra)), extra.join(" ")).toBe(expected)
105
+ expect(outcome(diff(extra)), extra.join(" ")).toBe(expected)
106
+ }
107
+ expect(outcome(() => ledgerFrom({ ledger: badLedgerPath }))).toContain("is not a valid fact ledger")
108
+ expect(outcome(() => ledgerFrom({}))).toContain("--ledger is required")
109
+ })
110
+
111
+ test("#given a session dir flag or SESSION_DIR #when resolved #then the flag wins and a bare flag is no session", () => {
112
+ expect(sessionOf({ "session-dir": dir })).toBe(dir)
113
+ expect(sessionOf({ "session-dir": true })).toBeNull()
114
+ process.env.SESSION_DIR = dir
115
+ expect(sessionOf({})).toBe(dir)
116
+ })
117
+ })
118
+
119
+ describe("the edition format comes from verify's grammar", () => {
120
+ test("#given report paths of each case #when polish-apply reads them #then it refuses exactly the Markdown classifications", () => {
121
+ const editsPath = file("edits-ok.json", "[]")
122
+ expect(source("fact-commands-polish.mjs")).not.toMatch(/md\|markdown/u)
123
+ for (const name of ["r.md", "R.MD", "r.markdown", "R.Markdown", "r.html", "r.HTML"]) {
124
+ const path = file(name, readFileSync(reportPath, "utf8"))
125
+ const result = outcome(() => runFactPolish("polish-apply", [path, "--edits", editsPath, "--ledger", ledgerPath, "--brief", BRIEF, "--out", join(dir, "out.html")]))
126
+ expect(result.includes("takes the filled HTML edition"), name).toBe(editionFormatOf(path, undefined) === "md")
127
+ }
128
+ })
129
+ })
130
+
131
+ describe("polish-diff counts with contracts and exits on blockers only", () => {
132
+ type DiffRun = { exitCode: number; json: { counts: Record<string, number>; defects: { severity: string }[] } }
133
+ const run = (before: string, after: string) => runFactPolish("polish-diff", ["--before", file("b.html", doc(before)), "--after", file("a.html", doc(after)), "--ledger", ledgerPath, "--brief", BRIEF]) as unknown as DiffRun
134
+
135
+ test("#given every registry code #when polishDiffFails classifies it #then only registry blockers fail", () => {
136
+ for (const [code, entry] of Object.entries(DEFECT_CODES) as [string, { severity: string }][]) expect(polishDiffFails([{ code, severity: "minor" }]), code).toBe(entry.severity === "blocker")
137
+ expect(polishDiffFails([])).toBe(false)
138
+ expect(source("fact-commands-polish.mjs")).not.toMatch(/blocker:\s*0|counts\.blocker > 0/u)
139
+ })
140
+
141
+ test("#given a major-only polish diff #when run #then it is reported with contract buckets and exits 0", () => {
142
+ const result = run("<p>The span may be 320 m [S1].</p>", "<p>The span is 320 m [S1].</p>")
143
+ const { defects } = result.json
144
+ expect(defects.length).toBeGreaterThan(0)
145
+ expect(defects.every((defect) => defect.severity === "major")).toBe(true)
146
+ expect(result.json.counts).toEqual(countDefects(defects))
147
+ expect(Object.keys(result.json.counts)).toEqual([...SEVERITIES, "total"])
148
+ expect(result.exitCode).toBe(0)
149
+ })
150
+
151
+ test("#given a changed numeral #when run #then the blocker exits 1", () => {
152
+ const result = run("<p>Throughput grew 3.5 times [S1].</p>", "<p>Throughput grew 35 times [S1].</p>")
153
+ expect(result.json.counts).toEqual(countDefects(result.json.defects))
154
+ expect(result.exitCode).toBe(1)
155
+ })
156
+ })
157
+
158
+ describe("fact polish-apply prints every rejection reason verbatim", () => {
159
+ type ApplyRun = { summary: string; exitCode: number; json: { defects: { index: number; code: string; reason: string }[]; data: { rejected: number; rejected_reasons: string[] } } }
160
+
161
+ test("#given an edit holding & #when fact polish-apply runs #then the printed line carries polish-apply's own markup reason", () => {
162
+ const before = "Revenue & profit rose across the year"
163
+ const editsPath = file("edits-markup-ampersand.json", JSON.stringify([{ before, after: "Revenue and profit rose across the year", reason: "wording" }]))
164
+ const run = runFactPolish("polish-apply", [reportPath, "--edits", editsPath, "--ledger", ledgerPath, "--brief", BRIEF, "--out", join(dir, "ampersand.html")]) as unknown as ApplyRun
165
+
166
+ expect(run.exitCode).toBe(0)
167
+ expect(run.json.data.rejected).toBe(1)
168
+ expect(run.json.defects.map((defect) => [defect.index, defect.code, defect.reason])).toEqual([[0, "polish_edit_rejected", POLISH_MARKUP_REASON]])
169
+ const line = ` rejected edit 0 ("${before}"): ${POLISH_MARKUP_REASON}`
170
+ expect(run.json.data.rejected_reasons).toEqual([line])
171
+ expect(run.summary.split("\n")[1]).toBe(line)
172
+ })
173
+ })
174
+
175
+ describe("--changed-since stays fail-closed", () => {
176
+ const { pairsChangedSince } = polishCommand as unknown as { pairsChangedSince: (pairs: unknown[], mapPath: string, ledger: unknown, claims: unknown) => unknown[] }
177
+ const artifact = file("pre-polish.html", doc("<p>The span is 320 m [S1].</p>"))
178
+
179
+ test("#given a fill map without artifact_sha256 #when --changed-since reads it #then it refuses", () => {
180
+ const mapPath = file("fill-map-no-sha.json", JSON.stringify({ artifact, format: "html", entries: [] }))
181
+ expect(outcome(() => pairsChangedSince([], mapPath, LEDGER, null))).toContain("fill map has no artifact_sha256")
182
+ })
183
+
184
+ test("#given a fill map whose artifact changed since fill #when read #then it still refuses", () => {
185
+ const mapPath = file("fill-map-stale.json", JSON.stringify({ artifact, artifact_sha256: "0".repeat(64), format: "html", entries: [] }))
186
+ expect(outcome(() => pairsChangedSince([], mapPath, LEDGER, null))).toContain("changed after fill")
187
+ })
188
+ })
189
+
190
+ describe("polish-diff refuses an unknown defect code", () => {
191
+ test("#given a defect whose code is not in the registry #when classified #then it refuses instead of reading its severity", () => {
192
+ expect(outcome(() => polishDiffFails([{ code: "not_a_real_code", severity: "minor" }]))).toContain("unknown defect code not_a_real_code")
193
+ expect(source("fact-commands-polish.mjs")).not.toMatch(/\?\?\s*defect\.severity/u)
194
+ })
195
+ })
196
+
197
+ describe("polish-apply writes atomically and honours --format", () => {
198
+ test("#given the command module #when read #then it writes the edition through writeTextAtomic", () => {
199
+ const text = source("fact-commands-polish.mjs")
200
+ expect(text).not.toMatch(/writeFileSync\s*\(/u)
201
+ expect(text).toMatch(/writeTextAtomic\(flags\.out/u)
202
+ })
203
+
204
+ test("#given --format md on an .html report #when polish-apply runs #then it refuses the Markdown edition", () => {
205
+ const editsPath = file("edits-format.json", "[]")
206
+ const result = outcome(() => runFactPolish("polish-apply", [reportPath, "--edits", editsPath, "--ledger", ledgerPath, "--brief", BRIEF, "--out", join(dir, "fmt.html"), "--format", "md"]))
207
+ expect(result).toContain("takes the filled HTML edition")
208
+ })
209
+ })
@@ -0,0 +1,86 @@
1
+ // fact-commands-fill.mjs - `fact fill`, the reader-facing fill of a placeholder source.
2
+ //
3
+ // It refuses an unlocked brief (fidelity_interview_incomplete) and applies the locked `## Fidelity`
4
+ // fields: `derived numbers: none` and the source floor refuse rows (derived_not_allowed,
5
+ // source_outside_floor, evaluated at every fill), `languages` decides whether a translation after a
6
+ // quote binds to it, and `fallback` picks the block quote or omission for --replace-failed. Any
7
+ // error writes no edition, fill map, or state; the one write that survives a refusal is the
8
+ // ledger's cached outside_floor per row (skipped under --view). The fill map records brief_sha256,
9
+ // the locked block fact verify checks it against. node: builtins and sibling imports only.
10
+
11
+ import { readFileSync, writeFileSync } from "node:fs"
12
+ import { dirname, extname, resolve } from "node:path"
13
+
14
+ import { CliError, sha256Of, writeJsonAtomic } from "./cli-support.mjs"
15
+ import { DEFECT_CODES } from "./contracts.mjs"
16
+ import { FILL_FORMATS, fill } from "./fact-fill.mjs"
17
+ import { readLedger, writeLedger } from "./fact-ledger.mjs"
18
+ import { briefHashOf, fillPolicy, lockedBrief, policyErrors } from "./fidelity-policy.mjs"
19
+ import { readState, writeState } from "./fidelity-state.mjs"
20
+ import { findPlaceholders } from "./placeholder.mjs"
21
+ import { sessionDirOf } from "./source-commands.mjs"
22
+
23
+ /** The CLI's input vocabulary: fill's output formats plus aliases (tex is filled as md), and the extension defaults. */
24
+ export const FILL_INPUT_FORMAT_RULES = Object.freeze({
25
+ aliases: Object.freeze({ tex: "md" }),
26
+ extensions: Object.freeze({ ".md": "md", ".tex": "tex" }),
27
+ })
28
+ export const FILL_INPUT_FORMATS = Object.freeze([...FILL_FORMATS, ...Object.keys(FILL_INPUT_FORMAT_RULES.aliases)])
29
+ const fillFormatOf = (format) => FILL_INPUT_FORMAT_RULES.aliases[format] ?? format
30
+
31
+ function readText(path) {
32
+ try {
33
+ return readFileSync(path, "utf8")
34
+ } catch (error) {
35
+ throw new CliError(`cannot read ${path}: ${error.code ?? error.message}`)
36
+ }
37
+ }
38
+
39
+ /** The inline edition: local stylesheets and scripts inlined, relative image paths made absolute. */
40
+ function inlineAssets(html, baseDir) {
41
+ const local = (ref) => !/^[a-z][a-z0-9+.-]*:|^\/\//i.test(ref)
42
+ return html
43
+ .replace(/<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi, (tag) => {
44
+ const href = /\bhref=["']([^"']+)["']/i.exec(tag)?.[1]
45
+ return href && local(href) ? `<style>${readText(resolve(baseDir, href))}</style>` : tag
46
+ })
47
+ .replace(/<script\b([^>]*)\bsrc=["']([^"']+)["']([^>]*)><\/script>/gi, (tag, pre, src, post) => (local(src) ? `<script${pre}${post}>${readText(resolve(baseDir, src))}</script>` : tag))
48
+ .replace(/(<img\b[^>]*\bsrc=["'])([^"']+)(["'])/gi, (tag, pre, src, post) => (local(src) && !src.startsWith("/") ? `${pre}${resolve(baseDir, src)}${post}` : tag))
49
+ }
50
+
51
+ /** @param {{ positionals: string[], flags: Record<string, any> }} args parsed by runFact */
52
+ export function runFactFill(args) {
53
+ const [source] = args.positionals
54
+ const { flags } = args
55
+ if (!source || (flags.out === undefined && !flags.view)) throw new CliError("usage: fact fill <src> (--out <path> | --view) [--format html|md|tex] [--inline] [--replace-failed] [--brief brief.md]")
56
+ const format = flags.format ?? FILL_INPUT_FORMAT_RULES.extensions[extname(source).toLowerCase()] ?? "html"
57
+ if (!FILL_INPUT_FORMATS.includes(format)) throw new CliError(`--format must be ${FILL_INPUT_FORMATS.slice(0, -1).join(", ")}, or ${FILL_INPUT_FORMATS.at(-1)}`)
58
+ if (flags.inline && format !== "html") throw new CliError("--inline applies to the html edition only")
59
+ const sessionDir = sessionDirOf(flags)
60
+ const { parsed, refusal } = lockedBrief("fact fill", flags)
61
+ if (refusal) return refusal
62
+ const policy = fillPolicy(parsed)
63
+ const text = readText(source)
64
+ const ledger = readLedger(sessionDir)
65
+ const refused = policyErrors(text, findPlaceholders(text), ledger, sessionDir, policy)
66
+ if (refused.cacheChanged && !flags.view) writeLedger(sessionDir, ledger)
67
+ const state = flags["replace-failed"] ? readState(sessionDir) : null
68
+ const result = fill(text, ledger, { format: fillFormatOf(format), view: flags.view === true, fallback: policy.fallback, ...(state ? { replaceFailed: state } : {}) })
69
+ const warnings = result.warnings
70
+ if (result.out === null || refused.errors.length > 0) {
71
+ const filled = result.errors.map((error) => ({ ...error, severity: DEFECT_CODES[error.code]?.severity ?? "blocker", hint: DEFECT_CODES[error.code]?.hint ?? null }))
72
+ const defects = [...refused.errors, ...filled]
73
+ return { json: { command: "fact fill", status: "fail", exitCode: 1, defects, warnings }, summary: `fact fill: FAIL - ${defects.length} error(s), nothing written`, exitCode: 1 }
74
+ }
75
+ const entries = policy.translations ? result.fillMap : result.fillMap.filter((entry) => entry.translates === undefined)
76
+ const out = flags.inline ? inlineAssets(result.out, dirname(resolve(source))) : result.out
77
+ if (flags.view) return { text: out, summary: `fact fill --view: ${entries.length} pointer(s), ${warnings.length} warning(s)`, exitCode: 0 }
78
+ writeFileSync(flags.out, out)
79
+ if (state) writeState(sessionDir, state)
80
+ const artifact = sha256Of(out)
81
+ const map = `${resolve(flags.out)}.fill-map.json`
82
+ const brief = briefHashOf(parsed)
83
+ writeJsonAtomic(map, { schemaVersion: 1, format, artifact: resolve(flags.out), artifact_sha256: artifact, brief_sha256: brief, entries })
84
+ const data = { out: resolve(flags.out), map, artifact_sha256: artifact, brief_sha256: brief, occurrences: entries.length, warnings }
85
+ return { json: { command: "fact fill", status: "pass", exitCode: 0, defects: [], data }, summary: `fact fill: wrote ${flags.out} (${entries.length} pointer(s))`, exitCode: 0 }
86
+ }
@@ -1,24 +1,26 @@
1
1
  // fact-commands-meaning.mjs - `fact pairs` and `fact meaning-check`, the script half of the
2
2
  // separate-session meaning verifier. fact pairs writes the (source, report sentence) pairs a
3
- // reader lane gets and nothing else; it honours the brief's `meaning check` field and writes
3
+ // reader lane gets and nothing else (with --changed-since, only the pairs whose sentence polish
4
+ // rewrote); it honours the brief's `meaning check` field and writes
4
5
  // `{ skipped: true }` when it is off. fact meaning-check ingests the readers' verdict files
5
6
  // (meaning-check.mjs), writes fidelity-meaning-defects.json (a fidelity record bound to the pairs'
6
7
  // artifact_sha256 and report, through writeFidelityRecord), and records the run in
7
8
  // fidelity-calibration.jsonl: a header with the promotion criterion on first write, then per run
8
9
  // one run line plus one line per flagged pair. The run id hashes the inputs only, so a rerun or a
9
10
  // --dispute rerun of the same inputs replaces that run's lines in place (disputes carry forward)
10
- // instead of counting a second run. Neither command calls a model or the network.
11
+ // instead of counting a second run. Both refuse an unlocked brief (fidelity_interview_incomplete) before
12
+ // reading anything else. Neither command calls a model or the network.
11
13
 
12
14
  import { existsSync, readFileSync } from "node:fs"
13
15
  import { dirname, join, resolve } from "node:path"
14
16
 
15
17
  import { CliError, parseArgs, readJsonFile, sha256Of, writeJsonAtomic, writeTextAtomic } from "./cli-support.mjs"
16
18
  import { countDefects } from "./contracts.mjs"
17
- import { markdownAsHtml, readEdition } from "./fact-commands-verify.mjs"
18
- import { readLedger } from "./fact-ledger.mjs"
19
- import { validateLedger } from "./fact-ledger-schema.mjs"
19
+ import { ledgerFrom, markdownAsHtml, readEdition, sessionOf } from "./fact-commands-verify.mjs"
20
+ import { pairsChangedSince } from "./fact-commands-polish.mjs"
20
21
  import { extractPairs, PAIR_KINDS } from "./fact-pairs.mjs"
21
- import { FIDELITY_INCOMPLETE, meaningCheckEnabled, requireLockedBrief } from "./fidelity-brief.mjs"
22
+ import { FIDELITY_INCOMPLETE } from "./fidelity-brief.mjs"
23
+ import { briefPathOf, lockedBrief, meaningCheckEnabled } from "./fidelity-policy.mjs"
22
24
  import { MEANING_RECORD, writeFidelityRecord } from "./fidelity-records.mjs"
23
25
  import { blocksDelivery } from "./gates-fidelity.mjs"
24
26
  import { ingestVerdicts, unverifiedDefects, VERDICT_SCHEMA } from "./meaning-check.mjs"
@@ -31,45 +33,20 @@ export const PROMOTION = Object.freeze({ code: "meaning_drift_confirmed", to: "i
31
33
  /** The command status: fail when any defect blocks delivery under the registry policy. */
32
34
  export const meaningStatus = (defects) => (defects.some(blocksDelivery) ? "fail" : "pass")
33
35
 
34
- const sessionOf = (flags) => {
35
- const dir = flags["session-dir"] ?? process.env.SESSION_DIR
36
- return dir && dir !== true ? resolve(dir) : null
37
- }
38
36
  const inSession = (session, name, flag) => {
39
37
  if (session === null) throw new CliError(`${flag} is required without --session-dir (or SESSION_DIR)`)
40
38
  return join(session, name)
41
39
  }
42
40
 
43
- function readLedgerFrom(flags, session) {
44
- if (flags.ledger === undefined) {
45
- if (session === null) throw new CliError("--ledger is required without --session-dir (or SESSION_DIR)")
46
- return readLedger(session)
47
- }
48
- const ledger = readJsonFile(flags.ledger)
49
- const errors = validateLedger(ledger)
50
- if (errors.length > 0) throw new CliError(`${flags.ledger} is not a valid fact ledger: ${errors[0]}`)
51
- return ledger
52
- }
53
-
54
- function briefState(flags, session) {
55
- const path = flags.brief ?? inSession(session, "brief.md", "--brief")
56
- const lock = requireLockedBrief(path)
57
- if (lock.exitCode === 2) throw new CliError(`${path}: malformed ## Fidelity block: ${lock.errors.join("; ")}`)
58
- return { path, locked: lock.locked, off: !meaningCheckEnabled(lock.parsed) }
59
- }
60
-
61
41
  function runPairs(args) {
62
42
  const [report] = args.positionals
63
43
  const { flags } = args
64
44
  if (!report) throw new CliError("usage: fact pairs <report> [--format html|md] [--ledger F] [--claims claims.json] [--changed-since <fill-map.json>] [--brief brief.md] [--out pairs.json]")
65
45
  const session = sessionOf(flags)
66
46
  const out = resolve(flags.out ?? inSession(session, PAIRS_FILE, "--out"))
67
- const brief = briefState(flags, session)
68
- if (!brief.locked) {
69
- const json = { command: "fact pairs", status: "fail", exitCode: 1, error: "fidelity_interview_incomplete", message: `${brief.path} has no locked ## Fidelity block` }
70
- return { json, summary: `fact pairs: ${json.message}`, exitCode: 1 }
71
- }
72
- if (brief.off) {
47
+ const { parsed, refusal } = lockedBrief("fact pairs", flags)
48
+ if (refusal) return refusal
49
+ if (!meaningCheckEnabled(parsed)) {
73
50
  const record = { schemaVersion: 1, skipped: true, reason: "meaning check: off", pairs: [] }
74
51
  writeJsonAtomic(out, record)
75
52
  return { json: { command: "fact pairs", status: "skipped", exitCode: 0, data: { out, ...record } }, summary: "fact pairs: skipped (meaning check: off)", exitCode: 0 }
@@ -77,10 +54,10 @@ function runPairs(args) {
77
54
  const edition = readEdition(report, flags)
78
55
  const claimsPath = flags.claims ?? (session === null ? null : join(session, "claims.json"))
79
56
  const claims = claimsPath !== null && (flags.claims !== undefined || existsSync(claimsPath)) ? readJsonFile(claimsPath) : null
80
- const changed = flags["changed-since"] === undefined ? undefined : readJsonFile(flags["changed-since"]).entries
81
- const ledger = readLedgerFrom(flags, session)
57
+ const ledger = ledgerFrom(flags, session)
82
58
  const html = edition.format === "md" ? markdownAsHtml(edition.text, edition.entries, ledger).html : edition.text
83
- const pairs = extractPairs(html, ledger, claims, changed)
59
+ const all = extractPairs(html, ledger, claims)
60
+ const pairs = flags["changed-since"] === undefined ? all : pairsChangedSince(all, flags["changed-since"], ledger, claims)
84
61
  const counts = { total: pairs.length, force_strong: pairs.filter((pair) => pair.force_strong).length }
85
62
  for (const kind of PAIR_KINDS) counts[kind] = pairs.filter((pair) => pair.kind === kind).length
86
63
  const record = { schemaVersion: 1, skipped: false, report: resolve(report), format: edition.format, artifact_sha256: sha256Of(edition.text), pairs_sha256: sha256Of(JSON.stringify(pairs)), counts, verdict_schema: VERDICT_SCHEMA, pairs }
@@ -140,23 +117,23 @@ function recordCalibration(path, lines, runId, pairsSha) {
140
117
  }
141
118
 
142
119
  /** A `{ skipped: true }` pairs file is accepted only when the locked brief turns the meaning check off. */
143
- function skippedRefusal(flags, session) {
144
- const brief = briefState(flags, session)
145
- if (!brief.locked) return { error: FIDELITY_INCOMPLETE, message: `${brief.path} has no locked ## Fidelity block, so a skipped meaning check cannot be accepted` }
146
- if (!brief.off) return { error: "meaning_check_required", message: `${flags.pairs} says skipped but ${brief.path} has meaning check on; rerun fact pairs` }
120
+ function skippedRefusal(flags, parsed) {
121
+ if (meaningCheckEnabled(parsed)) return { error: "meaning_check_required", message: `${flags.pairs} says skipped but ${briefPathOf(flags)} has meaning check on; rerun fact pairs` }
147
122
  return null
148
123
  }
149
124
 
150
125
  function runMeaningCheck(args) {
151
126
  const { flags } = args
152
- const usage = "usage: fact meaning-check --pairs pairs.json --verdicts cheap.json [--verdicts strong.json] [--dispute P<n> --reason TEXT] [--run-id ID] [--calibration F] [--out F]"
127
+ const usage = "usage: fact meaning-check --pairs pairs.json --verdicts cheap.json [--verdicts strong.json] [--dispute P<n> --reason TEXT] [--run-id ID] [--calibration F] [--brief brief.md] [--out F]"
153
128
  if (flags.pairs === undefined || !Array.isArray(flags.verdicts)) throw new CliError(usage)
154
129
  const session = sessionOf(flags)
130
+ const { parsed, refusal } = lockedBrief("fact meaning-check", flags)
131
+ if (refusal) return { ...refusal, json: { ...refusal.json, error: FIDELITY_INCOMPLETE } }
155
132
  const pairsText = readFileSync(flags.pairs, "utf8")
156
133
  const pairsDoc = readJsonFile(flags.pairs)
157
134
  if (pairsDoc?.skipped === true) {
158
- const refusal = skippedRefusal(flags, session)
159
- if (refusal !== null) return { json: { command: "fact meaning-check", status: "fail", exitCode: 1, ...refusal }, summary: `fact meaning-check: ${refusal.message}`, exitCode: 1 }
135
+ const skipped = skippedRefusal(flags, parsed)
136
+ if (skipped !== null) return { json: { command: "fact meaning-check", status: "fail", exitCode: 1, ...skipped }, summary: `fact meaning-check: ${skipped.message}`, exitCode: 1 }
160
137
  return { json: { command: "fact meaning-check", status: "skipped", exitCode: 0, defects: [] }, summary: JSON.stringify({ command: "fact meaning-check", status: "skipped" }), exitCode: 0 }
161
138
  }
162
139
  if (typeof pairsDoc?.artifact_sha256 !== "string" || typeof pairsDoc.report !== "string") throw new CliError(`${flags.pairs} names no artifact_sha256 and report, so the meaning record cannot be bound to an edition; rerun fact pairs`)
@@ -0,0 +1,132 @@
1
+ // fact-commands-polish.mjs - `fact polish-apply` and `fact polish-diff`, the script half of the
2
+ // proofread gate, plus the --changed-since filter of `fact pairs`.
3
+ //
4
+ // The writing lane returns polish-edits.json ([{before, after, reason}]) and never edits the
5
+ // report. polish-apply applies each edit through polish-apply.mjs and writes the result; a
6
+ // rejected (major) or unlocated (minor) edit leaves its region unchanged and is listed with its
7
+ // index and reason so the orchestrator can re-send it; every rejection reason is printed verbatim, one
8
+ // line per rejected edit (index and the first 60 characters of its before), in the summary and in
9
+ // data.rejected_reasons. It exits 0 whenever it wrote the output.
10
+ // polish-diff is the whole-document backstop (polish-diff.mjs) between the pre-polish artifact
11
+ // and any later copy; it exits 1 on a blocker. pairsChangedSince keeps only the pairs whose
12
+ // sentence text (normalized) is not in the pre-polish artifact that a fill map names, so the
13
+ // meaning readers re-check exactly what polish rewrote. No model, no network.
14
+
15
+ import { existsSync, readFileSync } from "node:fs"
16
+ import { join, resolve } from "node:path"
17
+
18
+ import { CliError, parseArgs, readJsonFile, sha256Of, writeJsonAtomic, writeTextAtomic } from "./cli-support.mjs"
19
+ import { countDefects, DEFECT_CODES } from "./contracts.mjs"
20
+ import { editionFormatOf, ledgerFrom, markdownAsHtml, sessionOf } from "./fact-commands-verify.mjs"
21
+ import { extractPairs } from "./fact-pairs.mjs"
22
+ import { lockedBrief } from "./fidelity-policy.mjs"
23
+ import { applyEdits, POLISH_EDIT_FIELDS, validatePolishEdits } from "./polish-apply.mjs"
24
+ import { diffPolish } from "./polish-diff.mjs"
25
+ import { normalizeText } from "./text-normalize.mjs"
26
+
27
+ export { ledgerFrom, POLISH_EDIT_FIELDS, validatePolishEdits }
28
+
29
+ export const POLISH_RECORD = "polish-defects.json"
30
+
31
+ /**
32
+ * The polish-diff exit policy: only a registry blocker fails the command; majors are reported.
33
+ * Fail-closed like the delivery barrier: a code the registry does not know refuses outright,
34
+ * because a caller-claimed severity may not demote it.
35
+ */
36
+ export const polishDiffFails = (defects) =>
37
+ defects.some((defect) => {
38
+ if (!Object.hasOwn(DEFECT_CODES, defect?.code)) throw new CliError(`unknown defect code ${defect?.code}`)
39
+ return DEFECT_CODES[defect.code].severity === "blocker"
40
+ })
41
+
42
+ function readText(path) {
43
+ try {
44
+ return readFileSync(path, "utf8")
45
+ } catch (error) {
46
+ throw new CliError(`cannot read ${path}: ${error.code ?? error.message}`)
47
+ }
48
+ }
49
+
50
+ const withSeverity = (entry) => ({ ...entry, severity: DEFECT_CODES[entry.code].severity, hint: DEFECT_CODES[entry.code].hint })
51
+
52
+ /** The first 60 characters of an edit's before, on one line. */
53
+ const beforePreview = (before) => (typeof before === "string" ? before : "").replace(/\s+/gu, " ").slice(0, 60)
54
+
55
+ /** One line per rejected edit: its index, the start of its before, and its reason verbatim. */
56
+ const rejectionLine = (entry, edit) => ` rejected edit ${entry.index} ("${beforePreview(edit?.before)}"): ${entry.reason}`
57
+
58
+ function runPolishApply(args) {
59
+ const [report] = args.positionals
60
+ const { flags } = args
61
+ if (!report || flags.edits === undefined || flags.out === undefined) throw new CliError("usage: fact polish-apply <report.html> --edits polish-edits.json --out <report.polished.html> [--ledger F] [--brief brief.md] [--session-dir D]")
62
+ if (editionFormatOf(report, flags.format) === "md") throw new CliError("fact polish-apply takes the filled HTML edition; polish a Markdown edition through its HTML source")
63
+ const { refusal } = lockedBrief("fact polish-apply", flags)
64
+ if (refusal) return refusal
65
+ const html = readText(report)
66
+ const editsText = readText(flags.edits)
67
+ const edits = readJsonFile(flags.edits)
68
+ try {
69
+ validatePolishEdits(edits)
70
+ } catch (error) {
71
+ throw new CliError(`${flags.edits} must be an array of {${Object.keys(POLISH_EDIT_FIELDS).join(", ")}} objects: ${error.message}`)
72
+ }
73
+ const result = applyEdits(html, edits, ledgerFrom(flags))
74
+ writeTextAtomic(flags.out, result.out)
75
+ const defects = [...result.rejected, ...result.unlocated].sort((a, b) => a.index - b.index).map(withSeverity)
76
+ const counts = { applied: result.applied.length, rejected: result.rejected.length, unlocated: result.unlocated.length }
77
+ const data = { out: resolve(flags.out), ...counts, applied_indexes: result.applied, input_sha256: sha256Of(html), edits_sha256: sha256Of(editsText), out_sha256: sha256Of(result.out) }
78
+ const rejected_reasons = result.rejected.map((entry) => rejectionLine(entry, edits[entry.index]))
79
+ const summary = [`fact polish-apply: ${counts.applied} applied, ${counts.rejected} rejected, ${counts.unlocated} unlocated of ${edits.length} edit(s)`, ...rejected_reasons].join("\n")
80
+ return { json: { command: "fact polish-apply", status: "pass", exitCode: 0, defects, data: { ...data, rejected_reasons } }, summary, exitCode: 0 }
81
+ }
82
+
83
+ function runPolishDiff(args) {
84
+ const { flags } = args
85
+ if (flags.before === undefined || flags.after === undefined) throw new CliError("usage: fact polish-diff --before <pre-polish.html> --after <polished.html> [--ledger F] [--out polish-defects.json] [--brief brief.md] [--session-dir D]")
86
+ const { refusal } = lockedBrief("fact polish-diff", flags)
87
+ if (refusal) return refusal
88
+ const after = readText(flags.after)
89
+ const defects = diffPolish(readText(flags.before), after, ledgerFrom(flags)).map((defect) => ({ ...defect, hint: DEFECT_CODES[defect.code]?.hint ?? null }))
90
+ const counts = countDefects(defects)
91
+ const status = polishDiffFails(defects) ? "fail" : "pass"
92
+ const exitCode = status === "fail" ? 1 : 0
93
+ const record = { command: "fact polish-diff", status, exitCode, before: resolve(flags.before), artifact: resolve(flags.after), artifact_sha256: sha256Of(after), defects, counts }
94
+ const session = sessionOf(flags)
95
+ const out = flags.out ?? (session === null ? null : join(session, POLISH_RECORD))
96
+ if (out !== null) writeJsonAtomic(resolve(out), record)
97
+ const summary = `fact polish-diff: ${status.toUpperCase()} - ${counts.blocker} blocker, ${counts.major} major, ${counts.minor} minor`
98
+ return { json: { ...record, out: out === null ? null : resolve(out) }, summary, exitCode }
99
+ }
100
+
101
+ const pairKey = (pair) => [pair.kind, pair.sentence_key, pair.claim_id ?? "", normalizeText(pair.sentence)].join("\u0000")
102
+
103
+ /**
104
+ * The pairs whose sentence text changed since the pre-polish artifact a fill map names: a pair is
105
+ * kept unless the same (kind, sentence key, normalized sentence) occurs in that artifact. The map
106
+ * must carry the artifact's `artifact_sha256` and the bytes must still match it, so the filter is
107
+ * fail-closed on a hand-made or stale map.
108
+ * @param {object[]} pairs extractPairs output on the polished edition
109
+ * @param {string} mapPath the pre-polish fill map (fact fill's `<out>.fill-map.json`)
110
+ */
111
+ export function pairsChangedSince(pairs, mapPath, ledger, claims) {
112
+ const map = readJsonFile(mapPath)
113
+ if (typeof map.artifact !== "string" || !existsSync(map.artifact)) throw new CliError(`${mapPath} names no readable pre-polish artifact`)
114
+ const text = readText(map.artifact)
115
+ if (typeof map.artifact_sha256 !== "string") throw new CliError(`${mapPath} fill map has no artifact_sha256; --changed-since needs the pre-polish hash`)
116
+ if (sha256Of(text) !== map.artifact_sha256) throw new CliError(`${map.artifact} changed after fill; --changed-since needs the pre-polish bytes`)
117
+ const html = map.format === "md" ? markdownAsHtml(text, map.entries, ledger).html : text
118
+ const before = new Map()
119
+ for (const pair of extractPairs(html, ledger, claims)) before.set(pairKey(pair), (before.get(pairKey(pair)) ?? 0) + 1)
120
+ return pairs.filter((pair) => {
121
+ const left = before.get(pairKey(pair)) ?? 0
122
+ if (left === 0) return true
123
+ before.set(pairKey(pair), left - 1)
124
+ return false
125
+ })
126
+ }
127
+
128
+ /** @param {string[]} argv the arguments after `fact polish-apply` or `fact polish-diff` */
129
+ export function runFactPolish(sub, argv) {
130
+ const args = parseArgs(argv)
131
+ return sub === "polish-apply" ? runPolishApply(args) : runPolishDiff(args)
132
+ }
@@ -8,6 +8,11 @@
8
8
  // escaped HTML projection (same line numbers) and checked with its fill map, which is the only
9
9
  // record of where its values sit; LaTeX is verified through its Markdown source.
10
10
  //
11
+ // The locked brief is re-judged here, not only at fill: the edition's fill map (--fill-map, else
12
+ // `<report>.fill-map.json` when present) must carry the current brief's brief_sha256
13
+ // (brief_changed), and policyErrors re-runs over every data-fact the edition carries, so an
14
+ // edition that violates the current derived-numbers or source-floor rule fails here too.
15
+ //
11
16
  // The delivery barrier that reads these records (and every other fidelity record) is
12
17
  // fidelity-records.mjs.
13
18
 
@@ -18,6 +23,8 @@ import { CliError, readJsonFile, sha256Of } from "./cli-support.mjs"
18
23
  import { countDefects } from "./contracts.mjs"
19
24
  import { recompute } from "./fact-derive.mjs"
20
25
  import { readLedger } from "./fact-ledger.mjs"
26
+ import { validateLedger } from "./fact-ledger-schema.mjs"
27
+ import { briefBindingErrors, fillPolicy, lockedBrief, policyErrors } from "./fidelity-policy.mjs"
21
28
  import { FIDELITY_RECORD, writeFidelityRecord } from "./fidelity-records.mjs"
22
29
  import { blocksDelivery, checkFidelity } from "./gates-fidelity.mjs"
23
30
  import { parseMarkdown } from "./md-lite.mjs"
@@ -120,6 +127,24 @@ export function markdownAsHtml(text, entries, ledger) {
120
127
  return { html: `${html}${open ? MD_HTML_BLOCK_RULES[open].group[1] : ""}</body></html>\n`, stale: [...stale] }
121
128
  }
122
129
 
130
+ /** The session dir from --session-dir or SESSION_DIR, or null; a bare flag names no session. */
131
+ export const sessionOf = (flags) => {
132
+ const dir = flags["session-dir"] ?? process.env.SESSION_DIR
133
+ return dir && dir !== true ? resolve(dir) : null
134
+ }
135
+
136
+ /** The ledger from --ledger (validated) or from the session dir; every fact command loads it here. */
137
+ export function ledgerFrom(flags, session = sessionOf(flags)) {
138
+ if (flags.ledger === undefined) {
139
+ if (session === null) throw new CliError("--ledger is required without --session-dir (or SESSION_DIR)")
140
+ return readLedger(session)
141
+ }
142
+ const ledger = readJsonFile(flags.ledger)
143
+ const errors = validateLedger(ledger)
144
+ if (errors.length > 0) throw new CliError(`${flags.ledger} is not a valid fact ledger: ${errors[0]}`)
145
+ return ledger
146
+ }
147
+
123
148
  const MARKDOWN_EXTENSION = /\.(md|markdown)$/iu
124
149
  /** The edition format: the explicit flag, else md for a .md or .markdown path (any case), else html. */
125
150
  export const editionFormatOf = (path, flag) => flag ?? (MARKDOWN_EXTENSION.test(path) ? "md" : "html")
@@ -140,8 +165,11 @@ export function runFactVerify(args) {
140
165
  const [report] = args.positionals
141
166
  const { flags } = args
142
167
  if (!report) throw new CliError("usage: fact verify <report> [--format html|md] [--sources D] [--claims claims.json] [--fill-map F] [--brief brief.md] [--out fidelity-defects.json]")
143
- const edition = readEdition(report, flags)
144
168
  const sessionDir = sessionDirOf(flags)
169
+ const { parsed, refusal } = lockedBrief("fact verify", flags)
170
+ if (refusal) return refusal
171
+ const policy = fillPolicy(parsed)
172
+ const edition = readEdition(report, flags)
145
173
  const claimsPath = flags.claims ?? join(sessionDir, "claims.json")
146
174
  const briefPath = flags.brief ?? join(sessionDir, "brief.md")
147
175
  const ledger = readLedger(sessionDir)
@@ -155,10 +183,15 @@ export function runFactVerify(args) {
155
183
  baseDir: dirname(resolve(report)),
156
184
  recompute,
157
185
  })
158
- const counts = countDefects(result.defects)
159
- const status = result.defects.some(blocksDelivery) ? "fail" : "pass"
186
+ const checked = projected?.html ?? edition.text
187
+ const pointers = [...checked.matchAll(/\bdata-fact="([^"]+)"/gu)].map((match) => ({ token: match[0], start: match.index, parsed: { id: match[1] } }))
188
+ const mapPath = flags["fill-map"] ?? `${report}.fill-map.json`
189
+ const bound = existsSync(mapPath) ? briefBindingErrors(readJsonFile(mapPath), mapPath, parsed) : []
190
+ const defects = [...bound, ...policyErrors(checked, pointers, ledger, sessionDir, policy).errors, ...result.defects]
191
+ const counts = countDefects(defects)
192
+ const status = defects.some(blocksDelivery) ? "fail" : "pass"
160
193
  const exitCode = status === "fail" ? 1 : 0
161
194
  const out = resolve(flags.out ?? join(sessionDir, FIDELITY_RECORD))
162
- const record = writeFidelityRecord(out, "fidelity", { command: "fact verify", status, exitCode, format: edition.format, report: resolve(report), artifact_sha256: sha256Of(edition.text), defects: result.defects, counts, gatesRun: result.gatesRun })
195
+ const record = writeFidelityRecord(out, "fidelity", { command: "fact verify", status, exitCode, format: edition.format, report: resolve(report), artifact_sha256: sha256Of(edition.text), defects, counts, gatesRun: result.gatesRun })
163
196
  return { json: { ...record, out }, summary: `fact verify: ${status.toUpperCase()} - ${counts.blocker} blocker, ${counts.major} major, ${counts.minor} minor`, exitCode }
164
197
  }