Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/app/api/answer/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
sourceGovernanceWarnings,
} from "@/lib/source-governance";
import { parseJsonBody } from "@/lib/validation/body";
import { toClientAnswerPayload } from "@/lib/answer-client-payload";
import { answerServerTimingEntries, buildServerTimingHeader } from "@/lib/server-timing";
import { createAdminClient } from "@/lib/supabase/admin";
import { logAnswerDiagnostics } from "@/lib/answer-telemetry";
Expand Down Expand Up @@ -166,7 +167,9 @@ export async function POST(request: Request) {
);
return NextResponse.json(
{
...answer,
// Boundary trim only — governance warnings and diagnostics above
// consumed the full answer (see answer-client-payload.ts).
...toClientAnswerPayload(answer),
degradedMode: answerDegradedModeSignal(answer),
scope: { ...scope, queryMode: answerBody.queryMode },
sourceGovernanceWarnings: warnings,
Expand Down
5 changes: 4 additions & 1 deletion src/app/api/answer/stream/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
type ApiRateLimitResult,
} from "@/lib/api-rate-limit";
import { publicAccessContext } from "@/lib/public-api-access";
import { toClientAnswerPayload } from "@/lib/answer-client-payload";
import { answerQuestionWithScope, type AnswerProgressEvent } from "@/lib/rag";
import { classifyRagQuery } from "@/lib/clinical-search";
import { annotateSearchResults, buildEvidenceRelevance } from "@/lib/evidence-relevance";
Expand Down Expand Up @@ -231,7 +232,9 @@ function streamAnswer(
}

send("final", {
...answer,
// Boundary trim only — governance warnings and diagnostics above
// consumed the full answer (see answer-client-payload.ts).
...toClientAnswerPayload(answer),
degradedMode: answerDegradedModeSignal(answer),
scope: scope ? { ...scope, queryMode: body.queryMode } : undefined,
sourceGovernanceWarnings: warnings,
Expand Down
42 changes: 42 additions & 0 deletions src/lib/answer-client-payload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { RagAnswer, SearchResult } from "@/lib/types";

// Route-boundary trim of the answer payload. The retrieval pipeline carries
// full chunk text plus server-only context on every source (adjacent_context
// for generation packing, memory cards, table facts, index-unit matches,
// document summaries), but the client renders only a snippet
// (retrieval_synopsis ?? content) plus identity, scoring, governance, and
// label fields. Trimming at the route boundary — never inside rag.ts — keeps
// caches, generation inputs, and eval behavior byte-identical while cutting
// the final SSE/JSON event the user waits on after the prose has streamed.
//
// Ordering contract: sourceGovernanceWarnings and logAnswerDiagnostics consume
// the FULL answer and must run before this trim (both routes do).

// Longest snippet the source cards can usefully show; the render policy falls
// back to `content` only when `retrieval_synopsis` is absent.
const clientSourceContentMaxChars = 700;

function truncateAtWordBoundary(text: string, maxChars: number) {
if (text.length <= maxChars) return text;
const slice = text.slice(0, maxChars);
const lastSpace = slice.lastIndexOf(" ");
return `${slice.slice(0, lastSpace > maxChars * 0.6 ? lastSpace : maxChars).trimEnd()}…`;
}

function trimSourceForClient(source: SearchResult): SearchResult {
const trimmed: SearchResult = {
...source,
content: truncateAtWordBoundary(source.content ?? "", clientSourceContentMaxChars),
};
Comment on lines +27 to +30

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep full source text for client safety extraction

When an answer source is longer than 700 chars, this replaces source.content before it reaches the browser. The dashboard still computes safety findings from answer.sources[].content (src/lib/clinical-safety.ts:117-119, called from ClinicalDashboard.tsx:2771), not just snippets; if the only contraindication/dose-limit/monitoring phrase is past the cutoff and no quote card duplicates it, the Safety findings panel drops that warning. A focused test that runs toClientAnswerPayload before extractSafetyFindings with a phrase after char 700 would reproduce it.

Useful? React with 👍 / 👎.

delete trimmed.adjacent_context;
delete trimmed.memory_cards;
delete trimmed.table_facts;
delete trimmed.index_unit;
delete trimmed.document_summary;
return trimmed;
}

export function toClientAnswerPayload<T extends Pick<RagAnswer, "sources">>(answer: T): T {
if (!answer.sources?.length) return answer;
return { ...answer, sources: answer.sources.map(trimSourceForClient) };
}
85 changes: 85 additions & 0 deletions tests/answer-client-payload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { describe, expect, it } from "vitest";

import { toClientAnswerPayload } from "@/lib/answer-client-payload";
import type { RagAnswer, SearchResult } from "@/lib/types";

function fullSource(overrides: Partial<SearchResult> = {}): SearchResult {
return {
id: "chunk-1",
document_id: "doc-1",
title: "Clozapine monitoring guideline",
file_name: "clozapine.pdf",
page_number: 4,
chunk_index: 7,
section_heading: "Monitoring",
content: "Full blood count weekly for 18 weeks. ".repeat(60),
retrieval_synopsis: "FBC weekly for 18 weeks, then monthly.",
image_ids: [],
similarity: 0.82,
source_metadata: { document_status: "current" } as SearchResult["source_metadata"],
adjacent_context: "Preceding paragraph context. ".repeat(20),
document_summary: "A long document summary. ".repeat(30),
memory_cards: [{ id: "m1" } as never],
table_facts: [{ id: "t1" } as never],
index_unit: { unit_type: "table" } as never,
...overrides,
} as SearchResult;
}

function answerWith(sources: SearchResult[]): Pick<RagAnswer, "sources"> {
return { sources };
}

describe("toClientAnswerPayload", () => {
it("drops server-only per-source fields the client never renders", () => {
const trimmed = toClientAnswerPayload(answerWith([fullSource()])).sources![0];
expect(trimmed.adjacent_context).toBeUndefined();
expect(trimmed.memory_cards).toBeUndefined();
expect(trimmed.table_facts).toBeUndefined();
expect(trimmed.index_unit).toBeUndefined();
expect(trimmed.document_summary).toBeUndefined();
});

it("keeps identity, snippet, scoring, and governance fields intact", () => {
const trimmed = toClientAnswerPayload(answerWith([fullSource()])).sources![0];
expect(trimmed.id).toBe("chunk-1");
expect(trimmed.title).toBe("Clozapine monitoring guideline");
expect(trimmed.retrieval_synopsis).toBe("FBC weekly for 18 weeks, then monthly.");
expect(trimmed.similarity).toBe(0.82);
expect(trimmed.source_metadata).toEqual({ document_status: "current" });
expect(trimmed.page_number).toBe(4);
});

it("truncates long chunk content at a word boundary", () => {
const trimmed = toClientAnswerPayload(answerWith([fullSource()])).sources![0];
expect(trimmed.content.length).toBeLessThanOrEqual(701);
expect(trimmed.content.endsWith("…")).toBe(true);
// Word-boundary cut: no partial trailing token before the ellipsis.
expect(trimmed.content.at(-2)).not.toBe(" ");
});

it("leaves short content untouched", () => {
const short = fullSource({ content: "Short snippet." });
expect(toClientAnswerPayload(answerWith([short])).sources![0].content).toBe("Short snippet.");
});

it("does not mutate the original answer (caches keep the full sources)", () => {
const source = fullSource();
const answer = answerWith([source]);
toClientAnswerPayload(answer);
expect(answer.sources![0].adjacent_context).toBeTruthy();
expect(answer.sources![0].content.length).toBeGreaterThan(700);
});

it("passes through answers without sources", () => {
const empty = answerWith([]);
expect(toClientAnswerPayload(empty)).toBe(empty);
});

it("materially shrinks a representative payload", () => {
const answer = answerWith(Array.from({ length: 8 }, (_, index) => fullSource({ id: `chunk-${index}` })));
const fullBytes = JSON.stringify(answer).length;
const trimmedBytes = JSON.stringify(toClientAnswerPayload(answer)).length;
expect(trimmedBytes).toBeLessThan(fullBytes * 0.5);
});
});
Loading