feat(coach): attach multiple documents to a conversation

Replace the scalar document_id discriminator on a coach conversation with a
CoachConversationDocument join table and a sticky documentScoped flag, so a
conversation can be grounded on several stored documents at once while the
prompt-injection fence stays intact across the data-model change.

The fence is re-established at both enforcement points, fail-closed:

- Client: resolveCoachSendTarget routes any turn on a fenced conversation (or a
  fresh chat with staged attachments) to the new /api/insights/chat/fenced
  endpoint; the fenced body is strict and never carries scope, guided-question,
  or prefill hints.
- Server: the tool route fetches with documentScoped:false AND 404s when the
  fetched row still carries an attachment (flag/join drift), emitting a
  fence_drift audit alarm. Document text now enters an LLM prompt in exactly one
  code path — the fenced pipeline — with no tools and no health snapshot; a
  module-graph test pins that the tool loop is unreachable from it.

The fenced turn fences each document separately with a server-built, scrubbed
header line (title and filename are untrusted text), runs a per-document egress
consent check on the single picked provider, and grounds numerically over the
live attachment union only. Attachments are capped at five documents and 192 KB
of combined text with honest truncation.

Attach and detach ride dedicated endpoints: attaching validates the document is
owned, live, indexed, and within cap, then flips the conversation fenced (the
one legal, privilege-reducing transition, audit-logged); detaching removes the
document but never clears the flag. The document-sheet chat keeps its public
wire contract and shares the same fenced pipeline over the join.

Migration 0245 adds the join table and the flag, backfills every legacy
single-document chat into a one-attachment fenced conversation, and drops the
scalar column in the same transaction — no dual-truth window.
This commit is contained in:
Marc-André Bombeck committed 2026-07-16 23:32:28 +02:00
1 parent d72f078764
commit 502783a3ea
45 files changed
+4541 -861

No files matched your search

+48
View File
@@ -680,3 +680,51 @@ export const documentChatHistoryQuerySchema = z.object({
export type DocumentChatHistoryQuery = z.infer<
typeof documentChatHistoryQuerySchema
>;
// ─── Fenced multi-document coach chat (S7) ──────────────────────────────────
/** Max documents attachable to one coach conversation. Bounds prompt size + the
* numeric-grounding union; five labelled documents is already past the point a
* chat answer stays useful. */
export const MAX_COACH_ATTACHMENTS = 5;
/**
* Request body for `POST /api/insights/chat/fenced`. `.strict()` — the schema
* REFUSES the tool-mode fields (`scope` / `guidedQuestion` / `prefill`) and
* `userId` rather than silently dropping them, so a confused client fails loudly
* in test. `conversationId` continues an existing FENCED thread; `attachmentIds`
* (first-turn only, min 1) creates a fresh fenced thread — supplying BOTH is a
* 422 (attach-to-existing goes through the attach endpoint; one write path per
* concern). No `documentId` / `userId` field — the owner is narrowed from the
* session, the documents come from the join table.
*/
export const fencedChatRequestSchema = z
.object({
conversationId: z.string().trim().min(1).max(64).optional(),
message: z.string().trim().min(1).max(DOCUMENT_CHAT_MESSAGE_MAX),
locale: z.enum(["en", "de"]).optional(),
attachmentIds: z
.array(z.string().trim().min(1).max(64))
.min(1)
.max(MAX_COACH_ATTACHMENTS)
.optional(),
})
.strict()
.meta({ id: "FencedCoachChatRequest" });
export type FencedChatRequestInput = z.infer<typeof fencedChatRequestSchema>;
/**
* Request body for `POST /api/insights/chat/{id}/attachments` — attach ONE
* already-stored document to an existing conversation. `.strict()`; no `userId`.
*/
export const coachAttachmentCreateSchema = z
.object({
documentId: z.string().trim().min(1).max(64),
})
.strict()
.meta({ id: "CoachAttachmentCreateRequest" });
export type CoachAttachmentCreateInput = z.infer<
typeof coachAttachmentCreateSchema
>;