feat: extraction coverage — message-granular ledger + token-batched runs (d132373 vision)
THE MESSAGE IS THE UNIT OF COVERAGE (vision-extraction-coverage.md): a per-chat append-only ledger (coverage.jsonl) records which messages the pipeline has extracted, by content fingerprint over exactly what the extraction view sees (one shared line builder — core/coverage.ts); the uncovered remainder is DERIVED at read time, never dual-tracked. A quiz answer merging into the asking message re-opens only that message; plan-block data-part attach never re-opens anything. - The trigger (turn-service) replaces every-3-quiz-answers with uncovered-tokens >= EXTRACTION.chunkTokens (4096) — quiz-less sessions extract too, content size is the honest signal. - runExtraction is now chunked: token-budgeted batches of the oldest uncovered consecutive messages (a tool call + result never split), each merged batch checkpointing its messages' coverage atomically — an abort or error loses at most the in-flight batch; merged batches stay covered. - Observation refs are batch-tagged (obs:<batchTag>:<sourceId>): re-runs of the same batch dedupe; a re-opened message's fresh understanding lands as a new event. Fixes the old obs:p1 position-collision. - New API: GET /api/graph/coverage (exact per-session derived coverage + running flags, most-uncovered first) and POST /api/graph/coverage/extract (manual run for one session, 409 while in flight). - Graph view chrome: the coverage button opens a panel with every session's covered/uncovered tokens and a per-session Extract button — the manual answer to aborted runs, errors, and dormant tails. Renders even when the graph is empty: an empty graph with uncovered sessions is exactly when it matters. Polls only while open. - Notes imports keep the whole-file checkpoint path (coverage undefined). - Tests: pure-core coverage suite (11), chunked-pipeline suite (3: multi-batch + no-op, abort-keeps-earlier-batches + remainder re-run, re-open alone + data-part invisibility), panel render suite (5); pendingQuizAnswers and its tests deleted as dead code. 486/486. Live-smoked on real GLM: manual extract of "teach me modal realism" ran one batch, wrote 4 coverage entries, panel flipped to "1.3k covered · complete" with the button disabled, graph refetched (nodes 11), usage persisted with real stats.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* POST /api/graph/coverage/extract — the manual extraction affordance
|
||||
* (vision-extraction-coverage.md): run the pipeline over ONE session's
|
||||
* uncovered content, whatever its size — the user's answer to an aborted
|
||||
* run, a pipeline error, or a session that went dormant under the chunk
|
||||
* budget. The pipeline itself batches under EXTRACTION.chunkTokens and
|
||||
* checkpoints coverage per merged batch, so this is the same run the
|
||||
* trigger fires, just user-initiated. 409 while a run is already in
|
||||
* flight (the dock is the source of truth for that); failures are logged
|
||||
* by the pipeline and leave coverage untouched — the button stays honest.
|
||||
*/
|
||||
import { extraction, extractionStatuses } from "@/src/server";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
let body: unknown;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
return Response.json({ error: "Invalid JSON body" }, { status: 400 });
|
||||
}
|
||||
const chatId = (body as { chatId?: unknown }).chatId;
|
||||
if (typeof chatId !== "string" || chatId === "") {
|
||||
return Response.json({ error: 'Body must be {chatId: string}' }, { status: 400 });
|
||||
}
|
||||
if (extractionStatuses().some((run) => run.kind === "chat" && run.chatId === chatId)) {
|
||||
return Response.json({ error: "An extraction run is already in flight for this session" }, { status: 409 });
|
||||
}
|
||||
// Fire-and-forget, like the trigger: the dock + the coverage panel's
|
||||
// running flags track progress; the promise never blocks the response.
|
||||
void extraction.runExtraction(chatId).catch((err: unknown) => {
|
||||
console.error(`[chat ${chatId}] manual extraction failed:`, err);
|
||||
});
|
||||
return Response.json({ started: true }, { status: 202 });
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* GET /api/graph/coverage — the exact extraction coverage of every session
|
||||
* (vision-extraction-coverage.md, settled 2026-09-15): which sessions have
|
||||
* unextracted content, how much (tokens + message counts), and which runs
|
||||
* are in flight. "Uncovered" is DERIVED here per request — fingerprint the
|
||||
* current transcript, diff against the coverage ledger — never tracked as
|
||||
* a second ledger. This is the data behind the graph view's coverage panel
|
||||
* and its per-session "extract what's missing" buttons.
|
||||
*/
|
||||
import type { UIMessage } from "ai";
|
||||
import { chatStore, extractionStatuses, stores } from "@/src/server";
|
||||
import { coverageOf } from "@/src/core";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET() {
|
||||
const running = new Set(
|
||||
extractionStatuses().filter((run) => run.kind === "chat").map((run) => run.chatId),
|
||||
);
|
||||
const entries = stores.graphStore.readCoverage();
|
||||
const chats = chatStore.listChats().map((summary) => {
|
||||
const record = chatStore.getChat(summary.id);
|
||||
const messages = (record?.messages ?? []) as UIMessage[];
|
||||
const cov = coverageOf(messages, entries, summary.id);
|
||||
return {
|
||||
chatId: summary.id,
|
||||
title: summary.title,
|
||||
updatedAt: summary.updatedAt,
|
||||
totalMessages: cov.slots.length,
|
||||
coveredMessages: cov.coveredCount,
|
||||
uncoveredMessages: cov.uncoveredCount,
|
||||
coveredTokens: cov.coveredTokens,
|
||||
uncoveredTokens: cov.uncoveredTokens,
|
||||
running: running.has(summary.id),
|
||||
};
|
||||
});
|
||||
// Most uncovered first — the actionable sessions lead the panel.
|
||||
chats.sort((a, b) => b.uncoveredTokens - a.uncoveredTokens || (a.updatedAt < b.updatedAt ? 1 : -1));
|
||||
return Response.json({ chats });
|
||||
}
|
||||
Reference in New Issue
Block a user