Files
learnharness/app/api/chats/route.ts
T
amosblomqvist a2db80b72d compaction: the usage-anchored trigger + visible session state (round 15)
The relational-databases incident, diagnosed and fixed:

TRIGGER — pi's estimator law, adopted exactly: anchor on the last
assistant message's provider-reported usage (totalTokens || the field
sum) and chars/4-estimate ONLY what was appended after it. The anchor
scan skips aborted/zero-usage assistants and STOPS at the newest
compaction marker (pre-cut usage described context the next request
will not carry — pi's distrust rule). The old estimator
(JSON.stringify of every message /4) tripled the true context
(measured: 84,412 'tokens' estimated vs 38,799 provider-reported on
the incident record) — scaffolding, escaped LaTeX, thinking blocks
and app-side details all counted. estimateMessageTokens now counts
wire text only (text + thinking + toolCall name/args; content text
for user/toolResult/custom). The unanchored fallback runs the plain
heuristic over the CUT-HONORED projection plus the additive prompt
term; the anchored path adds nothing (usage already billed prompt
and tools).

OBSERVABILITY — compaction is a visible session-wide state (owner
spec): the transcript-bottom placeholder register gains COMPACTING
CONTEXT… (BrailleSpinner + Shimmer, the THINKING placeholder's twin,
mutually exclusive by construction — the gate precedes beginTurn),
and the sidebar row shows 'Compacting context · <ago>' with the
spinner (sessionMeta outranks busy; compacting-changed joins
turn-started/turn-settled as refresh triggers). Liveness rides the
API like turn-busy: ChatListItem.compacting + compactionInFlight on
both GET /api/chats and GET /api/chats/[id] (initiallyCompacting
covers the navigate-back-mid-compaction mount — the event already
fired), exported through the server barrel.

Also: the relational-databases record rolled back to pre-compaction
(marker, post-marker turns, tree reset; backup in /tmp) for a clean
retest. The summarizer-usage 'gap' was a misread — steps[] was
populated all along.
2026-10-06 00:38:15 +03:00

72 lines
3.6 KiB
TypeScript

import { chainsForSelection, formatChainLine } from "@/src/core";
import { newGraphContextMessage, type GraphContextPick } from "@/src/core";
import { compactionInFlight, createChat, isTurnRunning, listChats, readSettingsRecord, stores, titleFromPrompt } from "@/src/server";
import type { ChatListItem } from "@/src/core";
/** The session picker's list: record-derived summaries (phase, pending
* question) + live liveness — a running turn marks the session busy
* (contracts.ts ChatListItem; the store can't see liveness, the route is
* the merge point). Owner, minimal-color round: background jobs NO LONGER
* count — the sidebar's Background agents section is their channel now
* (a running spinner per row would double-signal them), so busy means
* exactly "a teach turn is in flight". */
export async function GET() {
const chats: ChatListItem[] = listChats().map((c) => ({
...c,
busy: isTurnRunning(c.id),
compacting: compactionInFlight(c.id),
}));
return Response.json({ chats });
}
export async function POST(req: Request) {
const body = (await req.json().catch(() => ({}))) as { title?: unknown; kind?: unknown; graphContext?: unknown };
const title = typeof body.title === "string" ? body.title : "";
// Practice sessions (owner, 2026-10-04): kind "practice" marks the
// record — the practice agent + its tools from turn 1, no extraction.
const kind = body.kind === "practice" ? ("practice" as const) : undefined;
const chat = createChat(titleFromPrompt(title || "Untitled session"), kind);
// PER-SESSION MODELS (owner, 2026-09-28): a session owns its model +
// thinking level from birth — stamped verbatim from the STRONG slot,
// the new-session default. From here the strong slot never reaches
// this session again: in-session picks write the session's own record
// (PUT /api/chats/:id/settings). No strong slot configured (fresh BYOK
// boot) → no stamp; the turn gate's 503 models_not_configured envelope
// refuses honestly until the setup flow fills the slot.
const strong = readSettingsRecord()?.models?.strong;
if (strong?.model) {
stores.chatStore.saveSettings(chat.id, {
model: { provider: strong.model.provider, id: strong.model.id },
thinkingLevel: strong.thinkingLevel,
});
}
// The session's map context (owner, 2026-10-04): the picked
// propositions ride the CREATION — message[0] is a graph-context
// marker (a separate custom message BEFORE the learner's first user
// message), carrying the picks plus the reasoning chains among them.
// The picks carry their own name/statement (what the learner saw at
// pick time); the chains derive from the CURRENT graph snapshot.
const rawPicks = (body.graphContext as { propositions?: unknown } | undefined)?.propositions;
const picks: GraphContextPick[] = Array.isArray(rawPicks)
? rawPicks
.filter(
(p): p is GraphContextPick =>
typeof p === "object" &&
p !== null &&
typeof (p as GraphContextPick).id === "string" &&
typeof (p as GraphContextPick).name === "string" &&
typeof (p as GraphContextPick).statement === "string",
)
.slice(0, 64)
: [];
if (picks.length > 0) {
const byId = new Map(stores.graphStore.readNodes().map((n) => [n.id, n]));
const pickedNodes = picks.map((p) => byId.get(p.id)).filter((n) => n !== undefined);
const chainLines = chainsForSelection(pickedNodes, stores.graphStore.readChains()).map((c) =>
formatChainLine(c, Object.fromEntries(byId)),
);
stores.chatStore.saveMessages(chat.id, [newGraphContextMessage(picks, chainLines)]);
}
return Response.json({ id: chat.id, title: chat.title });
}