Files
learnharness/components/graph-context-marker.tsx
T
amosblomqvist 31978f3fc3 fix: background-delivery ground rules — the dock is the source of truth, delivered content shows the moment it persists
The owner's three-part test of the context-injection flow surfaced
timing/visibility bugs, fixed as standing ground rules for ALL background
processes (owner rules, 2026-09-15):

1. THE JOB STARTS AT SEND. triggerGraphContext moves from onFinish to
   right after the write-ahead persist in handleChatRequest: its only
   input is the first typed message (the topic), just made durable — so
   embedding + cosine scan run CONCURRENT with the prober's turn and
   the dock blob appears the moment the session starts, not after turn
   one finishes. Extraction stays at onFinish (it needs answers
   accumulated DURING the turn).

2. DELIVERED CONTENT SHOWS MID-TURN. Steers inserted server-side
   (claimed at turn start, persisted write-ahead) used to wait for the
   next turn's completion to appear — the full record sync mid-stream
   is forbidden (it breaks the SDK's in-place continuation merge), so
   adoption was deferred to idle. New append-only seam: core
   mergeAdopted() inserts the record's new message ids in canonical
   order (in-flight streaming tail kept last) and never rewrites a
   local message; any other shape bails to the full-sync-at-idle net.
   Wired at three points, no new standing polls:
   - turn-start window: three bounded record fetches (400ms/1s/2s)
     after a send catch the claimed steer while the turn streams;
     self-terminating, survives the submitted->streaming flip.
   - jobs-poll settle: mid-turn settles adopt append-only NOW, the
     owed full sync still runs at idle.
   - wake-adoption poll: each tick adopts persisted wake content, so
     steers show at the wake turn's START, not its finish.
   The dock stays honest with zero gap: jobs hold "running" while
   queued behind a pending question ("delivering context"), then flip
   done at claim/delivery — visible from start to delivery, and the
   turn-start nudge (useActivity().refresh) clears it within ~150ms.

3. THE MARKER IS CLICKABLE (observability). The knowledge-context
   SystemLine becomes GraphContextMarker: quiet line, popover with the
   delivered propositions IN DELIVERY ORDER (most similar first — the
   order the budget filled), each with its folded understanding level;
   Esc / outside pointerdown close. The part data was already the audit
   trail — now it's readable at the seam where it landed.

Tests: mergeAdopted unit pins (6), marker render pins (3) — 468/468,
tsc, eslint, build all green; live-smoked end-to-end: blob at send,
marker at t+500ms mid-turn, popover order + levels, instant dock clear.
2026-09-15 16:34:25 +03:00

104 lines
3.8 KiB
TypeScript

"use client";
import { useEffect, useRef, useState, type ReactNode } from "react";
import { BookOpenIcon } from "lucide-react";
import { graphContextOfMessage, type GraphContextData } from "@/src/core";
import { formatTokens } from "@/components/usage-format";
/**
* The knowledge-context delivery marker (vision-context-injection.md): the
* quiet transcript line for the injected prior-knowledge block — CLICKABLE
* (owner observability ask, 2026-09-15): the popover shows exactly which
* propositions were delivered, IN DELIVERY ORDER (most similar first —
* the order the selection filled the budget), each with its folded current
* understanding level. The record part is the audit trail; this makes it
* readable at the seam where it landed.
*
* Same quiet-line discipline as the SystemLine family (centered, muted,
* small); same popover discipline as the stats-detail (close on outside
* pointerdown + Escape, anchored below the marker).
*/
export function GraphContextMarker({
message,
testId = "graph-context",
}: {
/** The steer message (marker-only user message). */
message: { parts: ReadonlyArray<{ type: string; data?: unknown }> };
testId?: string;
}) {
const [open, setOpen] = useState(false);
const data = graphContextOfMessage(message);
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!open) return;
const onPointerDown = (e: PointerEvent) => {
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
};
const onKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape") setOpen(false);
};
document.addEventListener("pointerdown", onPointerDown);
document.addEventListener("keydown", onKeyDown);
return () => {
document.removeEventListener("pointerdown", onPointerDown);
document.removeEventListener("keydown", onKeyDown);
};
}, [open]);
const summary = data
? `Knowledge context — ~${formatTokens(data.tokens)} · ${data.propositions.length} propositions`
: "Knowledge context";
return (
<div ref={ref} data-testid={testId} className="relative flex items-center justify-center py-1">
<button
type="button"
onClick={() => setOpen((o) => !o)}
aria-expanded={open}
title="What was delivered — the propositions, in delivery order"
className="flex items-center justify-center gap-1.5 text-xs text-muted-foreground transition-colors hover:text-foreground"
>
<BookOpenIcon className="size-3" />
{summary}
</button>
{open && data && <GraphContextList data={data} />}
</div>
);
}
/** The popover body — exported for render tests. Delivery order preserved. */
export function GraphContextList({ data }: { data: GraphContextData }): ReactNode {
return (
<div
data-testid="graph-context-list"
className="absolute top-full left-1/2 z-40 mt-1.5 w-[36rem] max-w-[80vw] -translate-x-1/2 rounded-xl border bg-card p-3 text-xs shadow-xl"
>
<div className="mb-1 flex items-baseline justify-between gap-2">
<span className="font-medium uppercase tracking-wide text-muted-foreground">
Prior knowledge delivered
</span>
<span className="tabular-nums text-muted-foreground">
~{formatTokens(data.tokens)} tok of {data.budget} budget
</span>
</div>
<p className="mb-2 text-muted-foreground">
Most similar first — the order the budget filled. The tags are your current
understanding level for each proposition.
</p>
<ol className="flex max-h-72 flex-col gap-1.5 overflow-y-auto">
{data.propositions.map((p, i) => (
<li key={p.id} className="flex items-baseline gap-2">
<span className="shrink-0 tabular-nums text-muted-foreground">{i + 1}.</span>
{p.level && (
<span className="shrink-0 rounded bg-foreground/10 px-1 py-0.5 text-xxs text-muted-foreground">
{p.level}
</span>
)}
<span className="min-w-0 flex-1">{p.statement}</span>
</li>
))}
</ol>
</div>
);
}