MVP M8: background detection — assumptions, risks, inconsistencies

The editor now runs Socrates' three Phase-0-validated detection prompts
against the live model, persists the findings, and surfaces them in a
new FindingsPanel beside the IssuesPanel. Click any finding to focus its
linked element across rail + diagram. Re-detect after model edits to
refresh against the new state.

apps/web/lib/llm/prompts/socrates
- detect-assumptions.md / detect-risks.md / detect-inconsistencies.md
  promoted verbatim from phase-0 (Phase 0 corpus validated them 10/10).

apps/web/lib/llm/detect.ts
- Three sequential detection passes (parallel was OOM-prone on 4B local
  models — Phase 0 lesson). Each pass uses the Phase 0 JSON schema with
  jsonObjectMode fallback + chatJSON repair-retry. Fail-soft per pass:
  one busted pass returns [] rather than blowing up the whole detect.
- post-validate strips hallucinated element refs (drops findings whose
  refs ALL fail to resolve; keeps findings with zero refs since some
  inconsistencies are genuinely about absences).

apps/web/prisma/schema.prisma
- Finding table: kind / text / linkedElementIds (JSON) / confidence /
  severity / validationCode / status / modelVersion / provider / model.
- ResearchFinding table reserved for the Tavily integration that comes
  next — schema in place so we don't have to migrate again.

apps/web/lib/db/repo.ts
- listOpenFindings(projectId), replaceFindings(...) — replaceFindings
  wipes prior open findings in a transaction and writes the new set so
  re-detect doesn't accumulate stale findings.

apps/web/app/api/projects/[projectId]/findings/route.ts
- GET returns persisted open findings.
- POST runs detect, persists, returns findings + meta (provider, model,
  durationMs, strippedRefs, droppedFindings).

apps/web/components/editor/FindingsPanel.tsx
- New panel, anchored bottom-right just left of IssuesPanel. Shows
  count summary (asm / risk / inc), detect / re-detect button, list
  grouped by kind (inconsistencies first, then risks, then assumptions),
  per-finding glyph + tag + severity + confidence + linked refs.
- Click a finding row → focus its first linked element via the same
  setFocusBlockId path the rail and IssuesPanel already use.
- "stale" indicator when the model version has advanced past the one the
  findings were detected against.

EditorShell wires version + projectId through to FindingsPanel.

Smoke-tested end-to-end: 12 findings returned (5 asm / 4 risk / 3 inc),
0 hallucinated refs stripped, ~37s on local gemma-4-e4b. Sample
assumption "students are willing to engage with an AI tutor that is
programmed to refuse providing complete solutions" — specific to
Aristotle's refusal_policy, not a generic startup truism.

Deferred to follow-ups: inline rail/diagram badges from findings,
auto-detect-on-save, Tavily research, experiment modal.
This commit is contained in:
2026-04-30 00:43:27 +02:00
parent 4b8e3f04ee
commit 4e725c0b2b
10 changed files with 1010 additions and 1 deletions

View File

@@ -1245,3 +1245,156 @@ button { font-family: inherit; }
opacity: 0.4;
cursor: not-allowed;
}
/* ─── Findings panel (M8) ─── */
.findings-panel {
position: fixed;
/* Sit just above the IssuesPanel; both anchored bottom-right */
bottom: 32px;
right: 412px;
width: 460px;
max-height: 50vh;
background: var(--surface);
border: 1px solid var(--border-strong);
border-radius: 6px;
box-shadow: 0 8px 24px var(--shadow-strong);
display: flex;
flex-direction: column;
z-index: 50;
font-family: var(--font-body);
overflow: hidden;
}
.findings-panel-collapsed { max-height: none; }
.findings-panel-head {
display: flex; align-items: center; gap: 10px;
padding: 8px 12px;
background: transparent;
border: none;
border-bottom: 1px solid var(--border);
cursor: pointer;
font-family: inherit;
color: var(--fg);
width: 100%;
text-align: left;
}
.findings-panel-collapsed .findings-panel-head { border-bottom: none; }
.findings-panel-head:hover { background: var(--surface-2); }
.findings-panel-counts { display: flex; gap: 8px; }
.findings-count {
font-family: var(--font-mono);
font-size: 11px;
font-weight: 500;
}
.findings-count-asm { color: var(--accent-strong); }
.findings-count-risk { color: var(--warn-strong); }
.findings-count-inc { color: var(--ok-strong); }
.findings-count-none { color: var(--muted); font-style: italic; }
.findings-panel-title {
font-family: var(--font-display);
font-weight: 500;
font-size: 13px;
flex: 1;
}
.findings-panel-stale {
margin-left: 6px;
font-family: var(--font-mono);
font-size: 10px;
color: var(--warn-strong);
}
.findings-panel-caret { font-size: 9px; color: var(--muted); }
.findings-panel-actions {
display: flex; align-items: center; gap: 10px;
padding: 6px 12px;
border-bottom: 1px solid var(--border);
background: var(--surface-2);
}
.findings-panel-btn {
padding: 3px 10px;
border-radius: 4px;
border: 1px solid var(--accent);
background: var(--accent);
color: var(--accent-on);
font-family: var(--font-mono);
font-size: 10.5px;
cursor: pointer;
}
.findings-panel-btn:hover:not(:disabled) { background: var(--accent-strong); border-color: var(--accent-strong); }
.findings-panel-btn:disabled { opacity: 0.45; cursor: wait; }
.findings-panel-meta {
font-family: var(--font-mono);
font-size: 10px;
color: var(--muted);
}
.findings-panel-error {
padding: 8px 12px;
background: var(--warn-soft);
color: var(--warn-strong);
font-family: var(--font-mono);
font-size: 11px;
border-bottom: 1px solid var(--border);
}
.findings-panel-list {
list-style: none;
margin: 0;
padding: 4px;
overflow-y: auto;
flex: 1;
}
.findings-item {
display: grid;
grid-template-columns: 14px 28px auto auto 1fr auto auto;
gap: 6px;
align-items: baseline;
padding: 5px 8px;
border-radius: 4px;
font-size: 12px;
line-height: 1.4;
}
.findings-item:hover { background: var(--surface-2); }
.findings-item-glyph {
font-size: 11px;
text-align: center;
}
.findings-item-glyph-assumption { color: var(--accent-strong); }
.findings-item-glyph-risk { color: var(--warn-strong); }
.findings-item-glyph-inconsistency { color: var(--ok-strong); }
.findings-item-tag {
font-family: var(--font-mono);
font-size: 9.5px;
letter-spacing: 0.06em;
text-transform: uppercase;
color: var(--muted-strong);
background: var(--surface-2);
padding: 1px 5px;
border-radius: 3px;
}
.findings-item-sev {
font-family: var(--font-mono);
font-size: 9.5px;
padding: 1px 5px;
border-radius: 3px;
}
.findings-item-sev-low { background: var(--surface-2); color: var(--muted-strong); }
.findings-item-sev-medium { background: var(--accent-soft); color: var(--accent-strong); }
.findings-item-sev-high { background: var(--warn-soft); color: var(--warn-strong); }
.findings-item-code {
font-family: var(--font-mono);
font-size: 9.5px;
color: var(--muted-strong);
background: var(--surface-2);
padding: 1px 5px;
border-radius: 3px;
}
.findings-item-text { color: var(--prose); }
.findings-item-conf {
font-family: var(--font-mono);
font-size: 9.5px;
color: var(--muted);
}
.findings-item-refs {
font-family: var(--font-mono);
font-size: 9.5px;
color: var(--muted);
}