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

@@ -1,5 +1,6 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import { api, type Suggestion } from '../api/client'
import { mechanicsFindings } from '../components/Companion/prose'
const DEBOUNCE_MS = 4000
@@ -27,6 +28,13 @@ export function useCheckpoint(docId: string | null) {
const docIdRef = useRef(docId)
docIdRef.current = docId
// Latest plaintext, kept fresh by schedule(), so the deterministic mechanics
// pass (detected client-side, see prose.ts) reads the current document when the
// debounce fires — no need to re-thread text through every call. null until the
// first edit of the current doc, so a tone-only check before any edit doesn't
// submit an empty batch and wipe the doc's persisted mechanics rows.
const latestTextRef = useRef<string | null>(null)
// Token to discard responses from a doc we've since navigated away from.
const runRef = useRef(0)
@@ -46,6 +54,21 @@ export function useCheckpoint(docId: string | null) {
setChecking(true)
let retrying = false
try {
// Deterministic mechanics first: detect client-side and persist as the
// 'mechanics' family before the grammar pass, so the checkpoint's unified
// response already carries them. Best-effort and only on the initial try —
// a grammar retry shouldn't re-submit unchanged findings. A mechanics
// failure must not block the grammar pass.
if (attempt === 0 && latestTextRef.current !== null) {
try {
// Render the mechanics fixes immediately — they're instant (no LLM), so
// the unified set they return shouldn't wait on the slow grammar pass.
const withMech = await api.submitMechanics(id, mechanicsFindings(latestTextRef.current))
if (run === runRef.current && id === docIdRef.current) setSuggestions(withMech)
} catch (err) {
console.error('mechanics submit failed', err)
}
}
const fresh = await api.checkDoc(id)
if (run === runRef.current && id === docIdRef.current) {
setSuggestions(fresh)
@@ -116,8 +139,11 @@ export function useCheckpoint(docId: string | null) {
[runExplicitPass],
)
// Call on every edit; schedules a check 4s after typing settles.
const schedule = useCallback(() => {
// Call on every edit; schedules a check 4s after typing settles. Pass the
// current plaintext so the mechanics pass sees the latest document; omit it
// (e.g. a tone-only change) to reuse the last text.
const schedule = useCallback((text?: string) => {
if (text !== undefined) latestTextRef.current = text
clearTimeout(debounceRef.current)
clearTimeout(retryRef.current) // a fresh edit supersedes any queued retry
debounceRef.current = setTimeout(() => void runCheck(), DEBOUNCE_MS)
@@ -129,6 +155,7 @@ export function useCheckpoint(docId: string | null) {
clearTimeout(debounceRef.current)
clearTimeout(retryRef.current)
runRef.current++
latestTextRef.current = null // unknown until the new doc's first edit
setSuggestions([])
setChecking(false)
setVoicing(false)