@erclx/canon 4.81.0 → 4.82.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Regex layer for the doc classifier, ported from the groundwork spike at
3
+ * `.canon/groundwork/93-canonical-doc-drift/scripts/heuristic.py`. It never
4
+ * needs a model installed, so it is the layer that always runs.
5
+ *
6
+ * Diff mode reads one changed chunk and answers KEEP, HISTORY, or MOVE. It
7
+ * never answers REPLACE, since telling a restated figure from a genuinely new
8
+ * one needs the section the hunk landed in, which the model layer reads and
9
+ * this one does not. Sweep mode reads one whole section and answers KEEP,
10
+ * REWRITE, or MOVE, collapsing REPLACE and HISTORY the way the sweep prompt
11
+ * does, since a section already carries its own history in view.
12
+ */
13
+
14
+ export type DiffVerdict = 'KEEP' | 'REPLACE' | 'HISTORY' | 'MOVE'
15
+ export type SweepVerdict = 'KEEP' | 'REWRITE' | 'MOVE'
16
+
17
+ export interface PatternVerdict<V extends string> {
18
+ readonly verdict: V
19
+ /** Absent on KEEP, since nothing decided against the text. */
20
+ readonly quote?: string
21
+ readonly reason: string
22
+ }
23
+
24
+ /**
25
+ * Narrates how a fact got here rather than stating it: a branch or PR name, a
26
+ * closed or retired marker, a review-pass story. Ported from
27
+ * `heuristic.py`'s `NARRATION` pattern with one change: the bare
28
+ * `on \d{4}-\d{2}-\d{2}` alternative is dropped.
29
+ *
30
+ * That alternative was measured to false-flag a gotcha ending "Measured at
31
+ * `<sha>` on <date>.", a dating anchor both prompts call correct on a current
32
+ * statement. Every tuned hunk the bare date branch caught also carries a
33
+ * narration verb elsewhere in the same text (`closed on`, `moved again`,
34
+ * `did not survive`), so dropping it costs no measured catch and removes the
35
+ * one measured false flag.
36
+ */
37
+ const NARRATION =
38
+ /moved (again|twice|on)|has since|at this branch|on `feat|Measured in PR|did not survive|the operator picked|picked arm|\barm \d|closed on|was retired|no longer|used to|\bround\b|a review pass|this pass/i
39
+
40
+ /**
41
+ * A source-file path inside prose, the shape of implementation detail landing
42
+ * on a surface that should describe layout and intent instead. Ported
43
+ * verbatim from `heuristic.py`.
44
+ */
45
+ const MECHANISM_IN_WIREFRAME = /`[\w./-]+\.(tsx?|py|css|spec\.ts)`/
46
+
47
+ function narrationQuote(text: string): string | undefined {
48
+ return text.match(NARRATION)?.[0]
49
+ }
50
+
51
+ /**
52
+ * Classifies one diff-mode chunk from its added text alone.
53
+ *
54
+ * `file` decides whether the wireframe-only MOVE test applies. A file argument
55
+ * rather than a boolean matches `run.ts`'s other call sites, which hold the
56
+ * path and not a pre-computed flag.
57
+ */
58
+ export function diffPatternVerdict(
59
+ file: string,
60
+ added: string,
61
+ ): PatternVerdict<DiffVerdict> {
62
+ const quote = narrationQuote(added)
63
+ if (quote !== undefined) {
64
+ return { verdict: 'HISTORY', quote, reason: 'narrates how this got here' }
65
+ }
66
+
67
+ if (file.includes('wireframes/')) {
68
+ const match = added.match(MECHANISM_IN_WIREFRAME)
69
+ if (match) {
70
+ return {
71
+ verdict: 'MOVE',
72
+ quote: match[0],
73
+ reason: 'names a source file, which is implementation detail',
74
+ }
75
+ }
76
+ }
77
+
78
+ return { verdict: 'KEEP', reason: 'no narration or wrong-surface pattern' }
79
+ }
80
+
81
+ /**
82
+ * Classifies one sweep-mode section from its whole body.
83
+ *
84
+ * REWRITE stands in for both REPLACE and HISTORY, matching `sweep-v1.md`'s
85
+ * own three-verdict vocabulary: a section already shows its own history in
86
+ * full view, so the model layer does not need the diff-mode split and the
87
+ * regex layer follows it.
88
+ */
89
+ export function sweepPatternVerdict(
90
+ file: string,
91
+ body: string,
92
+ ): PatternVerdict<SweepVerdict> {
93
+ const quote = narrationQuote(body)
94
+ if (quote !== undefined) {
95
+ return {
96
+ verdict: 'REWRITE',
97
+ quote,
98
+ reason: 'carries its own history rather than the current state alone',
99
+ }
100
+ }
101
+
102
+ if (file.includes('wireframes/')) {
103
+ const match = body.match(MECHANISM_IN_WIREFRAME)
104
+ if (match) {
105
+ return {
106
+ verdict: 'MOVE',
107
+ quote: match[0],
108
+ reason: 'names a source file, which is implementation detail',
109
+ }
110
+ }
111
+ }
112
+
113
+ return { verdict: 'KEEP', reason: 'no narration or wrong-surface pattern' }
114
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * System prompts and user-message builders for the model layer, ported
3
+ * verbatim as data from the groundwork spike at
4
+ * `.canon/groundwork/93-canonical-doc-drift/scripts/prompts/v3.md` (diff
5
+ * mode) and `sweep-v1.md` (sweep mode). Neither prompt text is invented here.
6
+ *
7
+ * Each prompt already states its own rules per canonical doc type inside one
8
+ * string, which is the shape the spike measured, so there is one prompt per
9
+ * mode rather than one per doc type.
10
+ */
11
+
12
+ export const DIFF_SYSTEM_PROMPT = `You review one hunk just added to a project's canonical documentation. The docs state the project AS IT STANDS NOW. Judge only the ADDED text.
13
+
14
+ File types and what they hold:
15
+ - context/<domain>.md: one domain's structure, decisions (choice + the alternative that lost), gotchas. No history of how the domain got here, no change numbers, branch names, or dates attached to a change.
16
+ - ARCHITECTURE.md: cross-domain decisions only, each a few sentences: what was chosen, over what, why. A measurement or mechanism specific to one domain belongs in that domain's context entry. Loaded every session, so weight matters.
17
+ - wireframes/<surface>.md: layout sketches, reachable states, exact on-screen copy, interaction intent. Would the line still be true if the surface were rebuilt in another framework? Component names, test files, pixel constants, and mechanism belong in context.
18
+ - DESIGN.md: tokens and visual rules, current state only.
19
+
20
+ Verdicts (pick exactly one):
21
+ - KEEP: states current design, a rule, copy, a decision with its rejected alternative, or a live gotcha, on the right surface.
22
+ - REPLACE: restates a fact or figure the existing section already carries ("moved again", "now", "the count moved", a newer number appended after an older one). The old statement should be rewritten in place, not appended to.
23
+ - HISTORY: narrates how things got here: which branch/PR changed what, "closed on", "did not survive", review-pass stories, pick-by-pick rounds. The current state is what belongs; the trail goes to the PR or a decision log.
24
+ - MOVE: correct content on the wrong surface (domain mechanism in ARCHITECTURE.md, implementation detail in a wireframe).
25
+
26
+ Return only JSON: {"verdict": "...", "quote": "<the shortest added phrase that decided it>", "reason": "<one sentence>"}
27
+
28
+ Decision checks, in order:
29
+ 1. If the hunk REMOVED text and the added text is the same statement rewritten (a table row, a figure, a sentence updated in place), that is the correct way to update: KEEP, unless the new wording itself narrates history.
30
+ 2. A trailing "Measured at <sha> on <date>" or a branch name does not make a sentence current. If the section already carries an earlier figure or statement on the same subject and the hunk removed nothing, the answer is REPLACE.
31
+ 3. Judge only what the added text says. Narration elsewhere in the section is not evidence about this hunk.
32
+ `
33
+
34
+ export const SWEEP_SYSTEM_PROMPT = `You review one whole section of a project's canonical documentation, as it stands today. The docs must state the project AS IT IS NOW, once, on the right surface.
35
+
36
+ File types and what they hold:
37
+ - context/<domain>.md: one domain's structure, decisions (choice + the alternative that lost), gotchas. No history of how the domain got here, no change numbers, branch names, or dates attached to a change.
38
+ - ARCHITECTURE.md: cross-domain decisions only, each a few sentences: what was chosen, over what, why. A measurement or mechanism specific to one domain belongs in that domain's context entry.
39
+ - wireframes/<surface>.md: layout, reachable states, exact on-screen copy, interaction intent. Would the line still be true if the surface were rebuilt in another framework? Component names, test files, and pixel constants belong in context.
40
+ - DESIGN.md: tokens and visual rules, current state only.
41
+ - REQUIREMENTS.md: problem, goals, non-goals, scope, constraints. Never measured results.
42
+
43
+ Verdicts (pick exactly one):
44
+ - KEEP: the section states the current design, rules, copy, or decisions once, on the right surface. A single commit anchor on a figure is fine.
45
+ - REWRITE: the section carries its own history: a figure followed by a later corrected figure, "superseded", "now", "moved", "no longer", "reverses the earlier reading", branch or PR narration, pick-by-pick rounds, a heading that narrates an event. The subject belongs here, but it must be restated as the current state.
46
+ - MOVE: the section, or most of it, belongs on another surface (results in requirements, one domain's mechanism in architecture, implementation detail in a wireframe).
47
+
48
+ Decision checks, in order:
49
+ 1. Judge what the text says, not how long it is. A long section that states current design once is KEEP.
50
+ 2. A trailing "Measured at <sha> on <date>" does not make a paragraph current if another paragraph in the section states an older value on the same subject.
51
+ 3. Short rationale for a current rule ("the form checks length itself because...") is KEEP. A story of the rounds that produced the rule is REWRITE.
52
+
53
+ Return only JSON: {"verdict": "...", "quotes": ["<up to three short phrases copied from the section that decided it>"], "reason": "<one sentence>"}
54
+ `
55
+
56
+ /** Ported from `classify.py`'s `_user_message`, the unmarked diff-mode branch. */
57
+ export function diffUserMessage(opts: {
58
+ readonly file: string
59
+ readonly removed: string
60
+ readonly added: string
61
+ readonly sectionAfter: string
62
+ }): string {
63
+ const removed = opts.removed === '' ? '(nothing)' : opts.removed
64
+ return `FILE: ${opts.file}\n\nREMOVED IN THIS HUNK:\n${removed}\n\nADDED IN THIS HUNK:\n${opts.added}\n\nTHE SECTION AFTER THE CHANGE (for context):\n${opts.sectionAfter}`
65
+ }
66
+
67
+ /** Ported from `classify.py`'s `_user_message`, the sweep-mode branch. */
68
+ export function sweepUserMessage(opts: {
69
+ readonly file: string
70
+ readonly body: string
71
+ }): string {
72
+ return `FILE: ${opts.file}\n\nSECTION:\n${opts.body}`
73
+ }
@@ -0,0 +1,348 @@
1
+ import {
2
+ type CanonicalDocType,
3
+ extractDiffChunks,
4
+ extractSweepSections,
5
+ type ExtractRefusal,
6
+ } from '@/context/classify/extract'
7
+ import {
8
+ chat as ollamaChat,
9
+ type ChatOutcome,
10
+ DEFAULT_OLLAMA_BASE_URL,
11
+ probeOllama,
12
+ } from '@/context/classify/ollama'
13
+ import {
14
+ diffPatternVerdict,
15
+ type DiffVerdict,
16
+ type PatternVerdict,
17
+ sweepPatternVerdict,
18
+ type SweepVerdict,
19
+ } from '@/context/classify/patterns'
20
+ import {
21
+ DIFF_SYSTEM_PROMPT,
22
+ diffUserMessage,
23
+ SWEEP_SYSTEM_PROMPT,
24
+ sweepUserMessage,
25
+ } from '@/context/classify/prompts'
26
+ import {
27
+ type ClassifierBackend,
28
+ type ClassifierFlags,
29
+ type ClassifierResolution,
30
+ resolveClassifier,
31
+ type SettingSource,
32
+ } from '@/context/classify/settings'
33
+
34
+ const DIFF_VERDICTS: readonly DiffVerdict[] = [
35
+ 'KEEP',
36
+ 'REPLACE',
37
+ 'HISTORY',
38
+ 'MOVE',
39
+ ]
40
+ const SWEEP_VERDICTS: readonly SweepVerdict[] = ['KEEP', 'REWRITE', 'MOVE']
41
+
42
+ function isDiffVerdict(value: string): value is DiffVerdict {
43
+ return (DIFF_VERDICTS as readonly string[]).includes(value)
44
+ }
45
+
46
+ function isSweepVerdict(value: string): value is SweepVerdict {
47
+ return (SWEEP_VERDICTS as readonly string[]).includes(value)
48
+ }
49
+
50
+ /**
51
+ * A pattern verdict never carries a quote on KEEP, since nothing decided
52
+ * against the text. The finding's `regex` field is a `LayerVerdict` rather
53
+ * than a `PatternVerdict`, so it prints the same as a model layer verdict
54
+ * without every reader having to branch on an absent field.
55
+ */
56
+ function withQuote<V extends string>(
57
+ verdict: PatternVerdict<V>,
58
+ ): LayerVerdict<V> {
59
+ return {
60
+ verdict: verdict.verdict,
61
+ quote: verdict.quote ?? '',
62
+ reason: verdict.reason,
63
+ }
64
+ }
65
+
66
+ export interface LayerVerdict<V extends string> {
67
+ readonly verdict: V
68
+ readonly quote: string
69
+ readonly reason: string
70
+ }
71
+
72
+ /**
73
+ * Why the model layer did not decide a run, named alongside `'ran'` so a
74
+ * caller reading the record never has to infer the reason from an absent
75
+ * field. `'off'` and `'skipped-no-model'` come from settings resolution
76
+ * alone; `'skipped-unreachable'` is the one state a probe call decides.
77
+ */
78
+ export type ModelLayerState =
79
+ | 'off'
80
+ | 'ran'
81
+ | 'skipped-no-model'
82
+ | 'skipped-unreachable'
83
+
84
+ export interface DiffFinding {
85
+ readonly file: string
86
+ readonly docType: CanonicalDocType
87
+ readonly regex: LayerVerdict<DiffVerdict>
88
+ readonly model?: LayerVerdict<DiffVerdict>
89
+ /** Set when the model ran but its reply carried no verdict this could read. */
90
+ readonly modelUnparsed?: boolean
91
+ readonly verdict: DiffVerdict
92
+ readonly decidedBy: 'regex' | 'model'
93
+ }
94
+
95
+ export interface SweepFinding {
96
+ readonly file: string
97
+ readonly docType: CanonicalDocType
98
+ readonly heading: string
99
+ readonly regex: LayerVerdict<SweepVerdict>
100
+ readonly model?: LayerVerdict<SweepVerdict>
101
+ readonly modelUnparsed?: boolean
102
+ readonly verdict: SweepVerdict
103
+ readonly decidedBy: 'regex' | 'model'
104
+ }
105
+
106
+ interface ClassifyRecordBase {
107
+ readonly backend: ClassifierBackend | undefined
108
+ readonly model: string | undefined
109
+ readonly modelLayer: ModelLayerState
110
+ readonly settingsSource: SettingSource
111
+ }
112
+
113
+ export interface DiffRecord extends ClassifyRecordBase {
114
+ readonly mode: 'diff'
115
+ readonly findings: readonly DiffFinding[]
116
+ }
117
+
118
+ export interface SweepRecord extends ClassifyRecordBase {
119
+ readonly mode: 'sweep'
120
+ readonly findings: readonly SweepFinding[]
121
+ }
122
+
123
+ export type ClassifyRefusal = ExtractRefusal
124
+
125
+ export type ClassifyOutcome<Record> =
126
+ | { readonly kind: 'ok'; readonly record: Record }
127
+ | {
128
+ readonly kind: 'refused'
129
+ readonly reason: ClassifyRefusal
130
+ readonly message: string
131
+ }
132
+
133
+ /**
134
+ * The network boundary, injected so a test drives the merge logic below
135
+ * without a live Ollama. `src/version/skew.ts` takes the same shape for the
136
+ * npm registry lookup it wraps.
137
+ */
138
+ export interface ModelClient {
139
+ readonly probe: (baseUrl: string, timeoutMs?: number) => Promise<boolean>
140
+ readonly chat: (opts: {
141
+ readonly baseUrl: string
142
+ readonly model: string
143
+ readonly system: string
144
+ readonly user: string
145
+ readonly timeoutMs?: number
146
+ }) => Promise<ChatOutcome>
147
+ }
148
+
149
+ const DEFAULT_CLIENT: ModelClient = { probe: probeOllama, chat: ollamaChat }
150
+
151
+ export interface ClassifyOptions {
152
+ readonly flags?: ClassifierFlags
153
+ readonly docTypes?: readonly CanonicalDocType[]
154
+ readonly baseUrl?: string
155
+ readonly client?: ModelClient
156
+ }
157
+
158
+ interface ModelLayerPlan {
159
+ readonly modelLayer: ModelLayerState
160
+ readonly backend?: ClassifierBackend
161
+ readonly model?: string
162
+ }
163
+
164
+ /**
165
+ * Decides whether the model layer runs, probing reachability before any real
166
+ * call. A configured-but-unreachable backend is a plan the caller reports and
167
+ * falls back from, never a refusal: the groundwork decision reads "warn and
168
+ * continue," and a refused run here would fail `docs-fold` for a reason that
169
+ * has nothing to do with the branch it is checking.
170
+ */
171
+ async function planModelLayer(
172
+ resolution: ClassifierResolution,
173
+ client: ModelClient,
174
+ baseUrl: string,
175
+ ): Promise<ModelLayerPlan> {
176
+ if (resolution.kind === 'off') return { modelLayer: 'off' }
177
+ if (resolution.kind === 'no-model') {
178
+ return { modelLayer: 'skipped-no-model', backend: resolution.backend }
179
+ }
180
+
181
+ const reachable = await client.probe(baseUrl)
182
+ if (!reachable) {
183
+ return {
184
+ modelLayer: 'skipped-unreachable',
185
+ backend: resolution.backend,
186
+ model: resolution.model,
187
+ }
188
+ }
189
+
190
+ return {
191
+ modelLayer: 'ran',
192
+ backend: resolution.backend,
193
+ model: resolution.model,
194
+ }
195
+ }
196
+
197
+ /**
198
+ * One chunk or section, one model call, matching the measured constraint that
199
+ * batching multiple items into one prompt returned KEEP for everything. The
200
+ * caller (`classifyDiff`/`classifySweep`) awaits this once per item rather
201
+ * than issuing every call in parallel, since the items already share one
202
+ * backend and the groundwork measurements are all serial per-call numbers.
203
+ */
204
+ async function modelVerdictFor<V extends string>(opts: {
205
+ readonly plan: ModelLayerPlan
206
+ readonly client: ModelClient
207
+ readonly baseUrl: string
208
+ readonly system: string
209
+ readonly user: string
210
+ readonly isVerdict: (value: string) => value is V
211
+ }): Promise<{
212
+ readonly verdict?: LayerVerdict<V>
213
+ readonly unparsed: boolean
214
+ }> {
215
+ if (opts.plan.modelLayer !== 'ran' || !opts.plan.model) {
216
+ return { unparsed: false }
217
+ }
218
+
219
+ const outcome = await opts.client.chat({
220
+ baseUrl: opts.baseUrl,
221
+ model: opts.plan.model,
222
+ system: opts.system,
223
+ user: opts.user,
224
+ })
225
+
226
+ if (outcome.kind !== 'ok' || !opts.isVerdict(outcome.parsed.verdict)) {
227
+ return { unparsed: true }
228
+ }
229
+
230
+ return {
231
+ verdict: {
232
+ verdict: outcome.parsed.verdict,
233
+ quote: outcome.parsed.quote,
234
+ reason: outcome.parsed.reason,
235
+ },
236
+ unparsed: false,
237
+ }
238
+ }
239
+
240
+ /**
241
+ * Runs diff mode over one git range: the regex layer over every extracted
242
+ * chunk, and the model layer over the same chunks when configured and
243
+ * reachable.
244
+ *
245
+ * A finding's `verdict` takes the model's reading when the model ran and
246
+ * parsed, and the regex reading otherwise, since the groundwork measurements
247
+ * show the model catching what the regex layer structurally cannot
248
+ * (REPLACE, and MOVE outside a wireframe). Both readings stay on the finding
249
+ * regardless of which one decided, so a caller can see where they disagreed.
250
+ */
251
+ export async function classifyDiff(
252
+ root: string,
253
+ ref: string | undefined,
254
+ opts: ClassifyOptions = {},
255
+ ): Promise<ClassifyOutcome<DiffRecord>> {
256
+ const extraction = await extractDiffChunks(root, ref, opts.docTypes)
257
+ if (extraction.kind === 'refused') return extraction
258
+
259
+ const resolution = resolveClassifier(root, opts.flags ?? {})
260
+ const client = opts.client ?? DEFAULT_CLIENT
261
+ const baseUrl = opts.baseUrl ?? DEFAULT_OLLAMA_BASE_URL
262
+ const plan = await planModelLayer(resolution, client, baseUrl)
263
+
264
+ const findings: DiffFinding[] = []
265
+ for (const chunk of extraction.chunks) {
266
+ const regex = withQuote(diffPatternVerdict(chunk.file, chunk.added))
267
+ const model = await modelVerdictFor({
268
+ plan,
269
+ client,
270
+ baseUrl,
271
+ system: DIFF_SYSTEM_PROMPT,
272
+ user: diffUserMessage(chunk),
273
+ isVerdict: isDiffVerdict,
274
+ })
275
+
276
+ findings.push({
277
+ file: chunk.file,
278
+ docType: chunk.docType,
279
+ regex,
280
+ model: model.verdict,
281
+ modelUnparsed: model.unparsed || undefined,
282
+ verdict: model.verdict?.verdict ?? regex.verdict,
283
+ decidedBy: model.verdict ? 'model' : 'regex',
284
+ })
285
+ }
286
+
287
+ return {
288
+ kind: 'ok',
289
+ record: {
290
+ mode: 'diff',
291
+ backend: plan.backend,
292
+ model: plan.model,
293
+ modelLayer: plan.modelLayer,
294
+ settingsSource: resolution.source,
295
+ findings,
296
+ },
297
+ }
298
+ }
299
+
300
+ /** Runs sweep mode over every extracted section, mirroring `classifyDiff`. */
301
+ export async function classifySweep(
302
+ root: string,
303
+ opts: ClassifyOptions = {},
304
+ ): Promise<ClassifyOutcome<SweepRecord>> {
305
+ const extraction = await extractSweepSections(root, opts.docTypes)
306
+ if (extraction.kind === 'refused') return extraction
307
+
308
+ const resolution = resolveClassifier(root, opts.flags ?? {})
309
+ const client = opts.client ?? DEFAULT_CLIENT
310
+ const baseUrl = opts.baseUrl ?? DEFAULT_OLLAMA_BASE_URL
311
+ const plan = await planModelLayer(resolution, client, baseUrl)
312
+
313
+ const findings: SweepFinding[] = []
314
+ for (const section of extraction.sections) {
315
+ const regex = withQuote(sweepPatternVerdict(section.file, section.body))
316
+ const model = await modelVerdictFor({
317
+ plan,
318
+ client,
319
+ baseUrl,
320
+ system: SWEEP_SYSTEM_PROMPT,
321
+ user: sweepUserMessage(section),
322
+ isVerdict: isSweepVerdict,
323
+ })
324
+
325
+ findings.push({
326
+ file: section.file,
327
+ docType: section.docType,
328
+ heading: section.heading,
329
+ regex,
330
+ model: model.verdict,
331
+ modelUnparsed: model.unparsed || undefined,
332
+ verdict: model.verdict?.verdict ?? regex.verdict,
333
+ decidedBy: model.verdict ? 'model' : 'regex',
334
+ })
335
+ }
336
+
337
+ return {
338
+ kind: 'ok',
339
+ record: {
340
+ mode: 'sweep',
341
+ backend: plan.backend,
342
+ model: plan.model,
343
+ modelLayer: plan.modelLayer,
344
+ settingsSource: resolution.source,
345
+ findings,
346
+ },
347
+ }
348
+ }