Repository navigation
Withhold retained-field messages in session chat history #1292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
lib/sessions/chats/__tests__/retainedFieldHistory.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| import { beforeEach, afterEach, expect, it, vi } from "vitest"; | ||
| import { NextRequest } from "next/server"; | ||
| import { baseSessionRow } from "@/lib/sessions/__tests__/baseSessionRow"; | ||
| import { baseChatRow } from "@/lib/sessions/__tests__/baseChatRow"; | ||
| import type { Tables } from "@/types/database.types"; | ||
| import * as disclosure from "@/lib/sessions/chats/retained-field/readRetainedFieldDisclosure"; | ||
| import { GET } from "@/app/api/sessions/[sessionId]/chats/[chatId]/route"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| auth: vi.fn(), | ||
| sessions: vi.fn(), | ||
| chats: vi.fn(), | ||
| messages: vi.fn(), | ||
| })); | ||
| vi.mock("@/lib/auth/validateAuthContext", () => ({ validateAuthContext: mocks.auth })); | ||
| vi.mock("@/lib/supabase/sessions/selectSessions", () => ({ selectSessions: mocks.sessions })); | ||
| vi.mock("@/lib/supabase/chats/selectChats", () => ({ selectChats: mocks.chats })); | ||
| vi.mock("@/lib/supabase/chat_messages/selectChatMessages", () => ({ | ||
| selectChatMessages: mocks.messages, | ||
| })); | ||
| vi.mock("@/lib/sessions/chats/patchSessionChatHandler", () => ({ | ||
| patchSessionChatHandler: vi.fn(), | ||
| })); | ||
| vi.mock("@/lib/sessions/chats/deleteSessionChatHandler", () => ({ | ||
| deleteSessionChatHandler: vi.fn(), | ||
| })); | ||
|
|
||
| const companion = { | ||
| type: "data-retained-field-snapshot", | ||
| capability: "retained-field-companion/1", | ||
| data: { | ||
| fieldContext: { | ||
| contract: "catalog-retained-field-projection/1", | ||
| items: [{ value: { amount: 987654321 } }], | ||
| }, | ||
| surface: { schemaVersion: 1, fallbackText: "PRIVATE DERIVED 987654321" }, | ||
| }, | ||
| }; | ||
| const legacy = { | ||
| id: "legacy", | ||
| role: "assistant", | ||
| parts: [ | ||
| { | ||
| type: "data-catalog-snapshot", | ||
| data: { schemaVersion: 1, title: "Legacy report", controls: { sort: "streams" } }, | ||
| }, | ||
| { type: "text", text: "Legacy answer" }, | ||
| ], | ||
| }; | ||
| function row(parts: unknown, id = "retained"): Tables<"chat_messages"> { | ||
| return { | ||
| id, | ||
| chat_id: "chat_1", | ||
| role: "assistant", | ||
| parts: parts as Tables<"chat_messages">["parts"], | ||
| created_at: "2026-11-19T00:00:00Z", | ||
| }; | ||
| } | ||
| function request() { | ||
| return GET(new NextRequest("https://fixture.invalid/api/sessions/sess_1/chats/chat_1"), { | ||
| params: Promise.resolve({ sessionId: "sess_1", chatId: "chat_1" }), | ||
| }); | ||
| } | ||
| beforeEach(() => { | ||
| vi.restoreAllMocks(); | ||
| vi.clearAllMocks(); | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| vi.fn(() => { | ||
| throw new Error("NETWORK FORBIDDEN"); | ||
| }), | ||
| ); | ||
| mocks.auth.mockResolvedValue({ accountId: "actual-actor", orgId: null, authToken: "synthetic" }); | ||
| mocks.sessions.mockResolvedValue([baseSessionRow({ account_id: "actual-actor" })]); | ||
| mocks.chats.mockResolvedValue([baseChatRow()]); | ||
| }); | ||
| afterEach(() => { | ||
| expect(fetch).not.toHaveBeenCalled(); | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
| it("withholds whole retained messages including sibling text, numeric tables and metadata, preserving legacy order", async () => { | ||
| const retained = { | ||
| id: "retained", | ||
| role: "assistant", | ||
| metadata: { summary: "PRIVATE DERIVED 987654321" }, | ||
| parts: [ | ||
| { type: "text", text: "PRIVATE DERIVED 987654321" }, | ||
| companion, | ||
| { type: "data-table", data: { rows: [[987654321]] } }, | ||
| ], | ||
| }; | ||
| mocks.messages.mockResolvedValue([ | ||
| row(legacy, "legacy"), | ||
| row(retained), | ||
| row({ ...legacy, id: "after" }, "after"), | ||
| ]); | ||
| const response = await request(); | ||
| expect(response.status).toBe(200); | ||
| const body = await response.json(); | ||
| expect(body.messages).toEqual([legacy, { ...legacy, id: "after" }]); | ||
| expect(JSON.stringify(body)).not.toContain("987654321"); | ||
| expect(response.headers.get("Cache-Control")).toBe("private, no-store"); | ||
| }); | ||
| it.each([ | ||
| { ...companion, capability: "unknown-version" }, | ||
| { type: "data-retained-field-snapshot", data: null }, | ||
| { capability: "retained-field-companion/1", data: {} }, | ||
| { data: { fieldContext: { contract: "catalog-retained-field-projection/1" } } }, | ||
| { mode: "retained_field" }, | ||
| ])( | ||
| "withholds malformed or unsupported family members without relying on successful schema parsing: %j", | ||
| async part => { | ||
| mocks.messages.mockResolvedValue([ | ||
| row({ | ||
| id: "retained", | ||
| role: "assistant", | ||
| parts: [{ type: "text", text: "PRIVATE DERIVED" }, part], | ||
| }), | ||
| ]); | ||
| expect((await (await request()).json()).messages).toEqual([]); | ||
| }, | ||
| ); | ||
| it("also withholds legacy array-shaped stored parts when they contain the new family", async () => { | ||
| mocks.messages.mockResolvedValue([row([{ type: "text", text: "PRIVATE DERIVED" }, companion])]); | ||
| expect((await (await request()).json()).messages).toEqual([]); | ||
| }); | ||
| it("cached ready, successful preparation and local disclosure flags never permit history disclosure", async () => { | ||
| mocks.messages.mockResolvedValue([ | ||
| row({ | ||
| id: "retained", | ||
| role: "assistant", | ||
| state: "ready", | ||
| preparation: "ready", | ||
| disclosure: "available", | ||
| parts: [companion], | ||
| }), | ||
| ]); | ||
| expect((await (await request()).json()).messages).toEqual([]); | ||
| }); | ||
| it("preserves legacy text even when it mentions retained fields", async () => { | ||
| const message = { | ||
| id: "normal", | ||
| role: "assistant", | ||
| parts: [{ type: "text", text: "Explain data-retained-field-snapshot" }], | ||
| }; | ||
| mocks.messages.mockResolvedValue([row(message)]); | ||
| expect((await (await request()).json()).messages).toEqual([message]); | ||
| }); | ||
| it("does not read history for a different authenticated actor", async () => { | ||
| mocks.auth.mockResolvedValue({ accountId: "wrong-actor", orgId: null, authToken: "synthetic" }); | ||
| expect((await request()).status).toBe(403); | ||
| expect(mocks.messages).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it.each(["unavailable", "withdrawn", "stale", "error", "ready"])( | ||
| "denies the whole message for injected current-boundary result %s (including unsupported states)", | ||
| async state => { | ||
| const stored = { | ||
| id: "spoofed-message", | ||
| accountId: "forged-owner", | ||
| sessionId: "other-session", | ||
| chatId: "other-chat", | ||
| role: "assistant", | ||
| parts: [companion, { type: "text", text: "PRIVATE DERIVED" }], | ||
| }; | ||
| const read = vi | ||
| .spyOn(disclosure, "readRetainedFieldDisclosure") | ||
| .mockResolvedValue({ state } as never); | ||
| mocks.messages.mockResolvedValue([row(stored, "database-message-id")]); | ||
| const body = await (await request()).json(); | ||
| expect(body.messages).toEqual([]); | ||
| expect(read).toHaveBeenCalledExactlyOnceWith( | ||
| { | ||
| actorAccountId: "actual-actor", | ||
| sessionId: "sess_1", | ||
| chatId: "chat_1", | ||
| messageId: "database-message-id", | ||
| }, | ||
| stored, | ||
| ); | ||
| expect(JSON.stringify(body)).not.toContain("forged-owner"); | ||
| }, | ||
| ); | ||
| it("withholds every projection on a thrown current-source error without retry or diagnostics", async () => { | ||
| const read = vi | ||
| .spyOn(disclosure, "readRetainedFieldDisclosure") | ||
| .mockRejectedValue(new Error("PRIVATE AUTHORITY ERROR")); | ||
| mocks.messages.mockResolvedValue([ | ||
| row({ id: "retained", parts: [companion, { type: "text", text: "PRIVATE DERIVED" }] }), | ||
| row(legacy, "legacy"), | ||
| ]); | ||
| const response = await request(); | ||
| expect((await response.json()).messages).toEqual([legacy]); | ||
| expect(read).toHaveBeenCalledTimes(1); | ||
| }); | ||
| it("does not invoke current-field authority for legacy reports or text", async () => { | ||
| const read = vi.spyOn(disclosure, "readRetainedFieldDisclosure"); | ||
| mocks.messages.mockResolvedValue([row(legacy)]); | ||
| expect((await (await request()).json()).messages).toEqual([legacy]); | ||
| expect(read).not.toHaveBeenCalled(); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # Retained-field history withholding (2207) | ||
|
|
||
| The existing GET `/api/sessions/{sessionId}/chats/{chatId}` now invokes this gate | ||
| before serializing saved messages. It does not activate retained-field production | ||
| or grant disclosure. The current-authority boundary has no implementation and | ||
| returns unavailable. There is deliberately no positive release branch, including | ||
| for an unexpected or cached `ready` value. | ||
|
|
||
| ## Actual call graph | ||
|
|
||
| `route.GET` → `getSessionChatHandler` → `validateGetSessionChatRequest` | ||
| (authenticated account, session ownership, chat membership) → `selectChatMessages` | ||
| → `filterRetainedFieldHistory` → `hasRetainedFieldContent` → | ||
| `readRetainedFieldDisclosure` → omission of the entire marked message. | ||
|
|
||
| The validator returns the actual authenticated account separately from the chat. | ||
| The gate passes that account, validated session/chat IDs and database message ID; | ||
| none comes from the saved message's claimed owner, workspace, actor or anchors. | ||
| The saved content remains untrusted input to any future authority implementation. | ||
| No workspace authority is inferred from session ownership. | ||
|
|
||
| `persistAssistantMessage` stores a full AI SDK UIMessage in `chat_messages.parts`. | ||
| Removing only its companion would leak sibling text, tables or metadata. The gate | ||
| therefore omits the whole stored message. Legacy array-shaped parts are inspected | ||
| as well. Unrelated messages retain their original values and order. A successful | ||
| response is explicitly private/no-store; empty filtered history remains a 200. | ||
| Existing auth and storage error behavior is preserved. | ||
|
|
||
| ## Reserved family and limits | ||
|
|
||
| The proposed App envelope is `data-retained-field-snapshot` with capability | ||
| `retained-field-companion/1`, holding the unchanged schema-v1 surface alongside | ||
| `catalog-retained-field-projection/1`. Detection examines structured discriminants | ||
| throughout the saved JSON, including malformed payloads and unknown versions in | ||
| these reserved families. It also recognizes structured `mode: retained_field`. | ||
| It does not parse text, infer numeric meaning or extend schema-v1. This is a deny | ||
| classifier, not an admission schema or producer negotiation implementation. | ||
|
|
||
| This default-denied source integration intentionally withholds even a legitimate | ||
| historical retained-field message. Current-source read and publication locking, | ||
| exact selection/method/projection validation, batch authorization and a reviewed | ||
| transport are still required before any positive branch can be added. A callback, | ||
| local flag or successful earlier computation cannot replace them. | ||
|
|
||
| The boundary never fetches latest, re-keys, retains, expands, calls a provider or | ||
| falls back to legacy raw-report authority. No SQL, grants, flags or routes are | ||
| introduced. Frozen API1291/Database143 consumers are not imported or modified. | ||
|
|
||
| This qualifies this GET history consumer only. It cannot identify unmarked prose | ||
| in a separate message as derived, revoke an already delivered client object, or | ||
| qualify initial-send/replay/export/model inputs and other history endpoints. Those | ||
| paths and a provenance-preserving producer remain separate activation gates. | ||
|
|
||
| ## Negative acceptance | ||
|
|
||
| The route tests invoke the real GET, handler, validator and gate with synthetic | ||
| auth/storage boundaries, prohibit network access and verify whole-message removal, | ||
| mixed text/numeric projections, malformed/unsupported family markers, stored ready | ||
| and local disclosure flags, real actor/anchor propagation, thrown failure with no | ||
| retry, and unchanged legacy reports/controls. Injected withdrawn/stale/ready states | ||
| are adversarial boundary results, not claims of actual SQL withdrawal qualification. | ||
| No authenticated production/history request is executed. | ||
|
|
||
| Run `pnpm exec vitest run lib/sessions/chats/__tests__`. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.