Add deterministic mechanics suggestion family (rule-based, no LLM)

Reuse the companion's prose.ts rules engine as the single source of
deterministic detection instead of duplicating it. Applyable rules now
also emit exact-span fixes (original -> replacement) that surface as
suggestion cards; awareness-only rules (run-ons, splices, ...) stay
companion bubbles. The companion hides fix-bearing hints so a span is
never both a bubble and a card.

Spans are widened to a distinctive phrase ("a old" -> "an old",
"She have" -> "She has") so they re-anchor by string in the editor; a
lone lowercase "i" stays awareness-only since a single char can't anchor.

Backend: detection lives client-side, so the new persist-only
POST /docs/{id}/mechanics endpoint receives findings and stores them as
the 'mechanics' family with their exact offsets. It honours
actioned-suppression, leaves the LLM families untouched, and a checkpoint
no longer wipes it. fetchPending dedupes spans with mechanics winning any
collision against an LLM card (its span is exact). Migration 0008 adds the
'mechanics' suggestion type.

Client renders the mechanics fixes immediately (no LLM wait) and the cards
use a calm sage "Tidy-up" accent.

Verified end-to-end in a real browser on millenia: detect -> persist ->
render -> accept applies the fix.

Claude-Session: https://claude.ai/code/session_016Yr6jELuRc7hyzYLccQKZd
This commit is contained in:
prosolis
2026-06-26 20:43:37 -07:00
parent 3867cca2fa
commit 96f68a91ee
12 changed files with 767 additions and 181 deletions

View File

@@ -248,3 +248,119 @@ describe('capitalization basics', () => {
expectRule('The sun set slowly. it was a beautiful evening down by the river.', 'cap-sentence')
})
})
import { mechanicsFindings } from './prose'
// mechanicsFindings is the applyable subset feeding the suggestion cards. Each
// finding must carry an EXACT span (text.slice(from,to) === original) and a
// replacement that actually changes the text — these become one-click edits.
describe('mechanicsFindings — applyable fixes', () => {
// Every finding's span must be exact, regardless of which rule produced it.
function expectExactSpans(text: string) {
const found = mechanicsFindings(text)
for (const f of found) {
expect(text.slice(f.from, f.to), `span for ${JSON.stringify(f)}`).toBe(f.original)
expect(f.replacement).not.toBe(f.original)
}
return found
}
const one = (text: string, rulePred: (f: ReturnType<typeof mechanicsFindings>[number]) => boolean) =>
expectExactSpans(text).find(rulePred)
it('doubled word → single word, exact span', () => {
const f = one('I saw the the cat in the garden today.', (f) => f.original === 'the the')
expect(f?.replacement).toBe('the')
})
it('finds BOTH doublings with distinct spans', () => {
const found = mechanicsFindings('the the dog and the the cat ran around the yard.')
.filter((f) => f.original === 'the the')
expect(found).toHaveLength(2)
expect(found[0].from).not.toBe(found[1].from)
})
// Spans are widened to a distinctive phrase so they re-anchor by string in the
// editor (a bare "a"/"i"/" is" would match the wrong spot). The replacement
// carries the whole corrected phrase.
it('lowercase standalone i is awareness-only (no card)', () => {
// It still flags as a companion bubble (see analyzeProse), but a single
// character can't be anchored, so it must NOT become a card.
expect(mechanicsFindings('Yesterday i walked to the store and came home.').length).toBe(0)
expect(rulesFor('Yesterday i walked to the store and came home.')).toContain('cap-i')
})
it('a → an: spans the whole "a <noun>" phrase', () => {
const f = one('She found a umbrella under the old wooden table.', (f) => f.original === 'a umbrella')
expect(f?.replacement).toBe('an umbrella')
})
it('uncountable plural → singular', () => {
const f = one('She gave me many informations about the new job.', (f) => f.original === 'informations')
expect(f?.replacement).toBe('information')
})
it('lowercase proper noun → capitalized', () => {
const f = one('I am learning english during my free time this year.', (f) => f.original === 'english')
expect(f?.replacement).toBe('English')
})
it('subject-verb agreement → corrects the verb, spans subject+verb', () => {
const f = one('She have three cats and a dog at her house.', (f) => f.original === 'She have')
expect(f?.replacement).toBe('She has')
})
it('singular noun after a number → plural, spans number+noun', () => {
const f = one('I bought five apple at the market this morning.', (f) => f.original === 'five apple')
expect(f?.replacement).toBe('five apples')
})
it('comparative + then → than, spans the phrase', () => {
const f = one('This book is better then the one I read last week.', (f) => f.original === 'better then')
expect(f?.replacement).toBe('better than')
})
it('space before punctuation is removed (spans the word)', () => {
const f = one('I love it , it makes me very happy indeed.', (f) => f.original === 'it ,')
expect(f?.replacement).toBe('it,')
})
it('missing space after a comma is added (spans both words)', () => {
const f = one('I bought apples,bananas and pears at the store.', (f) => f.original === 'apples,bananas')
expect(f?.replacement).toBe('apples, bananas')
})
it('stacked determiner drops the article', () => {
const f = one('She left the my book on the kitchen table again.', (f) => f.original === 'the my')
expect(f?.replacement).toBe('my')
})
it('there is + plural → there are, spans the phrase', () => {
const f = one('There is many people waiting outside in the cold.', (f) => f.original === 'There is many')
expect(f?.replacement).toBe('There are many')
})
it("it's own → its own, spans the phrase", () => {
const f = one("The cat licked it's own paw very slowly today.", (f) => f.original === "it's own")
expect(f?.replacement).toBe('its own')
})
it("its a → it's a, spans the phrase", () => {
const f = one('I think its a wonderful day to go outside now.', (f) => f.original === 'its a')
expect(f?.replacement).toBe("it's a")
})
// Awareness-only rules never produce cards — they stay companion bubbles.
it('run-ons and splices produce NO card findings', () => {
const runOn = 'I went to the market and I bought some apples and then I saw my friend and we talked for a while and after that we went to the cafe and ordered coffee and cake together.'
expect(mechanicsFindings(runOn).some((f) => f.original.length > 30)).toBe(false)
const splice = 'I finished the report, I sent it to my boss right away today.'
// no card spans the comma-join; only (if any) small word-level fixes
expect(mechanicsFindings(splice).every((f) => f.to - f.from < 12)).toBe(true)
})
it('clean prose yields no findings', () => {
expect(mechanicsFindings('The garden was quiet and the rain fell softly on the leaves.')).toEqual([])
})
})