diff --git a/app/lib/workflows/__tests__/retainedFieldConversion.test.ts b/app/lib/workflows/__tests__/retainedFieldConversion.test.ts new file mode 100644 index 000000000..8a1e10ce8 --- /dev/null +++ b/app/lib/workflows/__tests__/retainedFieldConversion.test.ts @@ -0,0 +1,32 @@ +import { it, expect, vi } from "vitest"; +import { FatalError } from "workflow"; +import { convertMessagesStep } from "../convertMessagesStep"; +import { routeChatModelStep } from "../routeChatModelStep"; +const mocks = vi.hoisted(() => ({ convert: vi.fn(), route: vi.fn() })); +vi.mock("ai", () => ({ convertToModelMessages: mocks.convert })); +vi.mock("@/lib/ai/routing/selectChatModel", () => ({ selectChatModel: mocks.route })); +it.each(["conversion", "routing"])( + "rejects marked input before actual %s step effects", + async boundary => { + const messages = [ + { + id: "private", + role: "assistant", + metadata: { mode: "retained_field" }, + parts: [ + { type: "text", text: "PRIVATE" }, + { type: "file", url: "https://download.invalid/private", mediaType: "text/plain" }, + ], + }, + ] as never; + const getWriter = vi.fn(); + await expect( + boundary === "conversion" + ? convertMessagesStep(messages) + : routeChatModelStep(messages, { getWriter } as never), + ).rejects.toBeInstanceOf(FatalError); + expect(mocks.convert).not.toHaveBeenCalled(); + expect(mocks.route).not.toHaveBeenCalled(); + expect(getWriter).not.toHaveBeenCalled(); + }, +); diff --git a/app/lib/workflows/__tests__/retainedFieldModelWorkflow.test.ts b/app/lib/workflows/__tests__/retainedFieldModelWorkflow.test.ts new file mode 100644 index 000000000..2f099ed14 --- /dev/null +++ b/app/lib/workflows/__tests__/retainedFieldModelWorkflow.test.ts @@ -0,0 +1,309 @@ +import { clearChatActiveStream } from "@/lib/chat/clearChatActiveStream"; +import { sendStreamFinish } from "@/app/lib/workflows/sendStreamFinish"; +import { closeChatStream } from "@/app/lib/workflows/closeChatStream"; +import { deleteEphemeralKeyStep } from "@/app/lib/workflows/deleteEphemeralKeyStep"; +import { bindFencedWorkflowStep } from "@/app/lib/workflows/fence/bindFencedWorkflowStep"; +import { releaseFencedWorkflowStep } from "@/app/lib/workflows/fence/releaseFencedWorkflowStep"; +import { it, expect, vi, beforeEach, afterEach } from "vitest"; +import { runAgentWorkflow } from "@/app/lib/workflows/runAgentWorkflow"; +import { runAgentStep } from "@/app/lib/workflows/runAgentStep"; +import { convertMessagesStep } from "@/app/lib/workflows/convertMessagesStep"; +import { generateAssistantMessageId } from "@/app/lib/workflows/generateAssistantMessageId"; +import { persistAssistantMessageStep } from "@/app/lib/workflows/persistAssistantMessageStep"; +import { autoCommitChatTurn } from "@/lib/chat/auto-commit/autoCommitChatTurn"; +import { routeChatModelStep } from "@/app/lib/workflows/routeChatModelStep"; +vi.mock("@/app/lib/workflows/fence/bindFencedWorkflowStep", () => ({ + bindFencedWorkflowStep: vi.fn(), +})); +vi.mock("@/app/lib/workflows/fence/persistFencedMessageStep", () => ({ + persistFencedMessageStep: vi.fn(), +})); +vi.mock("@/app/lib/workflows/fence/releaseFencedWorkflowStep", () => ({ + releaseFencedWorkflowStep: vi.fn(), +})); +vi.mock("@/app/lib/workflows/routeChatModelStep", () => ({ routeChatModelStep: vi.fn() })); + +vi.mock("@/app/lib/workflows/prepareCatalogSurfaceStep", () => ({ + prepareCatalogSurfaceStep: vi.fn(), +})); +vi.mock("@/app/lib/workflows/sendCatalogSurfaceStep", () => ({ sendCatalogSurfaceStep: vi.fn() })); + +vi.mock("@/app/lib/workflows/runAgentStep", () => ({ runAgentStep: vi.fn() })); +vi.mock("@/app/lib/workflows/convertMessagesStep", () => ({ convertMessagesStep: vi.fn() })); +vi.mock("@/app/lib/workflows/persistAssistantMessageStep", () => ({ + persistAssistantMessageStep: vi.fn(), +})); +vi.mock("@/app/lib/workflows/sendStreamStart", () => ({ sendStreamStart: vi.fn() })); +vi.mock("@/app/lib/workflows/sendStreamFinish", () => ({ sendStreamFinish: vi.fn() })); +vi.mock("@/app/lib/workflows/generateAssistantMessageId", () => ({ + generateAssistantMessageId: vi.fn(), +})); +vi.mock("@/app/lib/workflows/deleteEphemeralKeyStep", () => ({ + deleteEphemeralKeyStep: vi.fn(), +})); +vi.mock("@/app/lib/workflows/closeChatStream", () => ({ closeChatStream: vi.fn() })); +vi.mock("@/lib/chat/clearChatActiveStream", () => ({ clearChatActiveStream: vi.fn() })); +vi.mock("@/lib/credits/handleChatCredits", () => ({ handleChatCredits: vi.fn() })); +vi.mock("@/lib/chat/auto-commit/autoCommitChatTurn", () => ({ autoCommitChatTurn: vi.fn() })); + +const writableStub = new WritableStream(); +vi.mock("workflow", () => ({ + FatalError: class FatalError extends Error {}, + getWritable: vi.fn(() => writableStub), + getWorkflowMetadata: vi.fn(() => ({ + workflowRunId: "wrun_loop_test", + workflowName: "runAgentWorkflow", + workflowStartedAt: new Date(0), + url: "https://example.invalid/workflow", + })), +})); + +const baseInput = { + messages: [{ id: "m1", role: "user", parts: [{ type: "text", text: "hi" }] } as never], + chatId: "chat-1", + sessionId: "session-1", + accountId: "acc-1", + modelId: "anthropic/claude-haiku-4.5", + agentContext: { + sandbox: { state: { type: "vercel" }, workingDirectory: "/sandbox/mono" }, + } as never, +}; + +/** An assistant message carrying `n` parts, used to assert threading. */ +const assistantMessage = (id: string) => + ({ id, role: "assistant", parts: [{ type: "text", text: id }] }) as never; + +/** Queue a sequence of runAgentStep results, one per loop iteration. */ +function queueSteps(results: Array>) { + const mocked = vi.mocked(runAgentStep); + results.forEach(r => mocked.mockResolvedValueOnce(r as never)); + // Anything beyond the queued results ends the loop, so a bug that + // over-iterates fails on a count assertion rather than hanging. + mocked.mockResolvedValue({ + finishReason: "stop", + aborted: false, + responseMessage: undefined, + responseMessages: [], + } as never); +} + +beforeEach(() => { + vi.clearAllMocks(); + vi.mocked(generateAssistantMessageId).mockResolvedValue("asst-loop-id"); + vi.mocked(convertMessagesStep).mockResolvedValue([{ role: "user", content: "hi" }] as never); +}); + +const marker = { + type: "data-retained-field-snapshot", + capability: "retained-field-companion/1", + data: { amount: 987654321 }, +}; +const marked = { + id: "private", + role: "assistant", + parts: [ + marker, + { type: "text", text: "PRIVATE 987654321" }, + { type: "file", url: "https://download.invalid/private", mediaType: "text/plain" }, + ], +} as never; +beforeEach(() => { + vi.stubGlobal( + "fetch", + vi.fn(() => { + throw new Error("NETWORK FORBIDDEN"); + }), + ); +}); +afterEach(() => { + expect(fetch).not.toHaveBeenCalled(); + vi.unstubAllGlobals(); +}); +it("refuses marked original input before a journaled converter or model call", async () => { + await expect( + runAgentWorkflow({ ...baseInput, messages: [...baseInput.messages, marked] }), + ).rejects.toThrow("Retained-field model input unavailable"); + expect(convertMessagesStep).not.toHaveBeenCalled(); + expect(runAgentStep).not.toHaveBeenCalled(); +}); +it("rechecks original inputs on replay even if a previously journaled conversion would erase the marker", async () => { + vi.mocked(convertMessagesStep).mockResolvedValue([ + { role: "assistant", content: "PRIVATE 987654321" }, + ] as never); + await expect(runAgentWorkflow({ ...baseInput, messages: [marked] })).rejects.toThrow( + "Retained-field model input unavailable", + ); + expect(convertMessagesStep).not.toHaveBeenCalled(); + expect(runAgentStep).not.toHaveBeenCalled(); +}); +it("refuses a marked journaled model conversion before calling the provider step", async () => { + vi.mocked(convertMessagesStep).mockResolvedValue([ + { + role: "tool", + content: [ + { + type: "tool-result", + toolCallId: "call", + toolName: "source", + output: { type: "json", value: marker }, + }, + ], + }, + ] as never); + await expect(runAgentWorkflow(baseInput)).rejects.toThrow( + "Retained-field model input unavailable", + ); + expect(runAgentStep).not.toHaveBeenCalled(); +}); +it.each(["responseMessage", "responseMessages"])( + "refuses marked %s from an earlier/journaled iteration before another provider call", + async field => { + queueSteps([ + { + finishReason: "tool-calls", + aborted: false, + responseMessage: assistantMessage("a1"), + responseMessages: [], + [field]: + field === "responseMessage" + ? marked + : [ + { + role: "tool", + content: [{ type: "tool-result", output: { type: "json", value: marker } }], + }, + ], + }, + ]); + await expect(runAgentWorkflow(baseInput)).rejects.toThrow( + "Retained-field model input unavailable", + ); + expect(runAgentStep).toHaveBeenCalledTimes(1); + expect(persistAssistantMessageStep).not.toHaveBeenCalled(); + }, +); +it("keeps complete unmarked tool-call/result pairs and server account scope unchanged", async () => { + const pair = [ + { + role: "assistant", + content: [{ type: "tool-call", toolCallId: "call", toolName: "safe", input: {} }], + }, + { + role: "tool", + content: [ + { + type: "tool-result", + toolCallId: "call", + toolName: "safe", + output: { type: "json", value: { total: 0 } }, + }, + ], + }, + ]; + queueSteps([ + { finishReason: "tool-calls", aborted: false, responseMessages: pair }, + { finishReason: "stop", aborted: false, responseMessages: [] }, + ]); + await runAgentWorkflow(baseInput); + expect(vi.mocked(runAgentStep).mock.calls[1][0].modelMessages).toEqual([ + { role: "user", content: "hi" }, + ...pair, + ]); + expect(vi.mocked(runAgentStep).mock.calls[1][0].accountId).toBe(baseInput.accountId); +}); + +it("auto routing cannot receive sibling text from a marked message", async () => { + await expect( + runAgentWorkflow({ ...baseInput, modelId: "auto", messages: [marked] }), + ).rejects.toThrow("Retained-field model input unavailable"); + expect(routeChatModelStep).not.toHaveBeenCalled(); + expect(runAgentStep).not.toHaveBeenCalled(); +}); +it("a terminal marked result cannot reach auto-commit's subsequent model work", async () => { + queueSteps([ + { finishReason: "stop", aborted: false, responseMessage: marked, responseMessages: [] }, + ]); + await expect(runAgentWorkflow(baseInput)).rejects.toThrow( + "Retained-field model input unavailable", + ); + expect(autoCommitChatTurn).not.toHaveBeenCalled(); + expect(runAgentStep).toHaveBeenCalledTimes(1); +}); + +it.each([ + ["raw", false], + ["raw", true], + ["converted", false], + ["converted", true], +] as const)("cleans legacy %s denial exactly once (headless=%s)", async (stage, headless) => { + const cleanupOrder: string[] = []; + vi.mocked(sendStreamFinish).mockImplementation(async () => { + cleanupOrder.push("finish"); + }); + vi.mocked(closeChatStream).mockImplementation(async () => { + cleanupOrder.push("close"); + }); + if (stage === "converted") + vi.mocked(convertMessagesStep).mockResolvedValue([{ mode: "retained_field" }] as never); + await expect( + runAgentWorkflow({ + ...baseInput, + messages: stage === "raw" ? [marked] : baseInput.messages, + agentContext: { + sandbox: { state: { type: "vercel" }, workingDirectory: "/sandbox/mono" }, + ...(headless ? { ephemeralKeyId: "owned-key" } : {}), + } as never, + }), + ).rejects.toThrow("Retained-field model input unavailable"); + expect(clearChatActiveStream).toHaveBeenCalledExactlyOnceWith("chat-1", "wrun_loop_test"); + expect(sendStreamFinish).toHaveBeenCalledExactlyOnceWith(writableStub); + expect(closeChatStream).toHaveBeenCalledExactlyOnceWith(writableStub); + expect(cleanupOrder).toEqual(["finish", "close"]); + if (headless) expect(deleteEphemeralKeyStep).toHaveBeenCalledExactlyOnceWith("owned-key"); + else expect(deleteEphemeralKeyStep).not.toHaveBeenCalled(); + expect(runAgentStep).not.toHaveBeenCalled(); + expect(routeChatModelStep).not.toHaveBeenCalled(); + expect(convertMessagesStep).toHaveBeenCalledTimes(stage === "raw" ? 0 : 1); +}); +it.each(["raw", "converted"])("preserves fenced ownership on %s denial", async stage => { + vi.mocked(bindFencedWorkflowStep).mockResolvedValue({ + writer: { synthetic: true }, + turn: { kind: "new" }, + } as never); + if (stage === "converted") + vi.mocked(convertMessagesStep).mockResolvedValue([{ mode: "retained_field" }] as never); + await expect( + runAgentWorkflow({ + kind: "internal-fenced/v1", + fence: {}, + input: { + ...baseInput, + messages: stage === "raw" ? [marked] : baseInput.messages, + agentContext: { ephemeralKeyId: "fenced-key" }, + }, + } as never), + ).rejects.toThrow("Retained-field model input unavailable"); + expect(closeChatStream).toHaveBeenCalledExactlyOnceWith(writableStub); + expect(clearChatActiveStream).not.toHaveBeenCalled(); + expect(sendStreamFinish).not.toHaveBeenCalled(); + expect(deleteEphemeralKeyStep).not.toHaveBeenCalled(); + expect(releaseFencedWorkflowStep).not.toHaveBeenCalled(); + expect(runAgentStep).not.toHaveBeenCalled(); +}); +it("does not acquire cleanup authority when fenced binding loses", async () => { + vi.mocked(bindFencedWorkflowStep).mockRejectedValue( + new Error("Fenced bind requires reconciliation"), + ); + await expect( + runAgentWorkflow({ + kind: "internal-fenced/v1", + fence: {}, + input: { ...baseInput, messages: [marked] }, + } as never), + ).rejects.toThrow("Fenced bind requires reconciliation"); + expect(closeChatStream).not.toHaveBeenCalled(); + expect(clearChatActiveStream).not.toHaveBeenCalled(); + expect(sendStreamFinish).not.toHaveBeenCalled(); + expect(deleteEphemeralKeyStep).not.toHaveBeenCalled(); + expect(releaseFencedWorkflowStep).not.toHaveBeenCalled(); +}); diff --git a/app/lib/workflows/__tests__/runAgentStep.test.ts b/app/lib/workflows/__tests__/runAgentStep.test.ts index ebbd548f0..0e3867725 100644 --- a/app/lib/workflows/__tests__/runAgentStep.test.ts +++ b/app/lib/workflows/__tests__/runAgentStep.test.ts @@ -16,6 +16,7 @@ vi.mock("@ai-sdk/gateway", () => ({ // getRun(runId).status to source its abort signal. Stub both so the tests // don't pull in the workflow runtime. vi.mock("workflow", () => ({ + FatalError: class FatalError extends Error {}, getWorkflowMetadata: vi.fn(() => ({ workflowRunId: "test-run-id", workflowName: "test", @@ -383,3 +384,18 @@ it.each(["low", "medium", "high"])( }); }, ); + +it.each(["modelMessages", "originalMessages"])( + "denies retained %s at the actual provider step before streamText", + async field => { + vi.clearAllMocks(); + await expect( + runAgentStep({ + ...baseInput, + [field]: [{ mode: "retained_field", text: "PRIVATE" }], + writable: makeWritable().stream, + } as never), + ).rejects.toThrow("Retained-field model input unavailable"); + expect(streamText).not.toHaveBeenCalled(); + }, +); diff --git a/app/lib/workflows/__tests__/runAgentStepStreaming.test.ts b/app/lib/workflows/__tests__/runAgentStepStreaming.test.ts index 5effcab3c..536c5cdfc 100644 --- a/app/lib/workflows/__tests__/runAgentStepStreaming.test.ts +++ b/app/lib/workflows/__tests__/runAgentStepStreaming.test.ts @@ -1,5 +1,8 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import { streamText } from "ai"; +import { FatalError } from "workflow"; +import { getRun } from "workflow/api"; +import { pollWorkflowCancellation } from "@/lib/chat/pollWorkflowCancellation"; import { runAgentStep } from "@/app/lib/workflows/runAgentStep"; import { persistAssistantMessage } from "@/lib/chat/persistAssistantMessage"; @@ -13,7 +16,8 @@ vi.mock("@ai-sdk/gateway", () => ({ vi.mock("@/lib/chat/persistAssistantMessage", () => ({ persistAssistantMessage: vi.fn(), })); -vi.mock("workflow", () => ({ +vi.mock("workflow", async () => ({ + FatalError: (await vi.importActual("workflow")).FatalError, getWorkflowMetadata: vi.fn(() => ({ workflowRunId: "test-run-id", workflowName: "test", @@ -116,3 +120,151 @@ describe("runAgentStep — streaming shape (mirrors upstream open-agents)", () = expect(result.responseMessages).toEqual([]); }); }); + +describe("retained structured stream output", () => { + const safePart = { type: "text-delta", id: "t1", delta: "already emitted independent text" }; + const markedPart = { + type: "tool-output-available", + toolCallId: "source-call", + output: { rows: [{ mode: "retained_field" }], sibling: "PRIVATE-SIBLING-987654321" }, + }; + const markedMessage = { + id: "assistant", + role: "assistant", + parts: [ + { + type: "tool-source", + toolCallId: "source-call", + state: "output-available", + input: {}, + output: markedPart.output, + }, + ], + }; + + it.each(["running", "cancelled", "poller-aborted"])( + "withholds the whole marked part and refuses rather than returning output when run is %s", + async status => { + vi.mocked(getRun).mockReturnValue({ status: Promise.resolve(status) } as never); + if (status === "poller-aborted") { + vi.mocked(pollWorkflowCancellation).mockImplementationOnce((_runId, controller) => { + controller.abort(); + return { stop: vi.fn(), done: Promise.resolve() }; + }); + } + let signal: AbortSignal | undefined; + const finish = vi.fn(); + vi.mocked(streamText).mockImplementation(options => { + signal = options.abortSignal; + // Reject after denial cleanup aborts the provider, as streamText can do. + const rejectOnAbort = () => + new Promise((_resolve, reject) => { + const fail = () => + reject(Object.assign(new Error("provider aborted"), { name: "AbortError" })); + if (signal?.aborted) queueMicrotask(fail); + else signal?.addEventListener("abort", fail, { once: true }); + }); + return { + toUIMessageStream: (opts: { onFinish: (event: unknown) => void }) => + (async function* () { + try { + yield safePart; + yield markedPart; + yield { type: "text-delta", id: "t1", delta: "later output" }; + } finally { + // SDK cancellation may finish its accumulated message, including withheld data. + finish(); + opts.onFinish({ responseMessage: markedMessage }); + } + })(), + finishReason: rejectOnAbort(), + response: rejectOnAbort(), + } as never; + }); + const { stream, written } = makeWritable(); + const close = vi.spyOn(stream, "close"); + const abort = vi.spyOn(stream, "abort"); + const result = runAgentStep({ ...baseInput, writable: stream } as never); + await expect(result).rejects.toBeInstanceOf(FatalError); + await expect(result).rejects.toThrow(/^Retained-field model input unavailable$/); + expect(written).toEqual([safePart]); + expect(JSON.stringify(written)).not.toContain("PRIVATE-SIBLING"); + expect(finish).toHaveBeenCalledOnce(); + expect(stream.locked).toBe(false); + expect(close).not.toHaveBeenCalled(); + expect(abort).not.toHaveBeenCalled(); + expect(signal?.aborted).toBe(true); + expect( + vi.mocked(pollWorkflowCancellation).mock.results.at(-1)?.value.stop, + ).toHaveBeenCalledOnce(); + expect(persistAssistantMessage).not.toHaveBeenCalled(); + }, + ); + + it("refuses a marked accumulated onFinish message even during cancellation", async () => { + vi.mocked(getRun).mockReturnValue({ status: Promise.resolve("cancelled") } as never); + vi.mocked(streamText).mockReturnValue({ + toUIMessageStream: (opts: { onFinish: (event: unknown) => void }) => + (async function* () { + yield safePart; + opts.onFinish({ responseMessage: markedMessage }); + throw Object.assign(new Error("stopped"), { name: "AbortError" }); + })(), + finishReason: Promise.resolve("stop"), + response: Promise.resolve({ messages: [] }), + } as never); + const { stream, written } = makeWritable(); + await expect(runAgentStep({ ...baseInput, writable: stream } as never)).rejects.toThrow( + "Retained-field model input unavailable", + ); + expect(written).toEqual([safePart]); + }); + + it("preserves an unmarked tool output and its completed message", async () => { + const part = { type: "tool-output-available", toolCallId: "safe", output: { total: 7 } }; + const message = { + id: "assistant", + role: "assistant", + parts: [ + { + type: "tool-source", + toolCallId: "safe", + state: "output-available", + input: {}, + output: part.output, + }, + ], + }; + const modelMessages = [ + { + role: "tool", + content: [ + { + type: "tool-result", + toolCallId: "safe", + toolName: "source", + output: { type: "json", value: part.output }, + }, + ], + }, + ]; + vi.mocked(streamText).mockReturnValue({ + toUIMessageStream: (opts: { onFinish: (event: unknown) => void }) => + (async function* () { + yield part; + opts.onFinish({ responseMessage: message }); + })(), + finishReason: Promise.resolve("tool-calls"), + response: Promise.resolve({ messages: modelMessages }), + } as never); + const { stream, written } = makeWritable(); + const result = await runAgentStep({ ...baseInput, writable: stream } as never); + expect(written).toEqual([part]); + expect(result).toEqual({ + finishReason: "tool-calls", + responseMessage: message, + responseMessages: modelMessages, + aborted: false, + }); + }); +}); diff --git a/app/lib/workflows/convertMessagesStep.ts b/app/lib/workflows/convertMessagesStep.ts index 17bf475cb..02d16453d 100644 --- a/app/lib/workflows/convertMessagesStep.ts +++ b/app/lib/workflows/convertMessagesStep.ts @@ -1,3 +1,4 @@ +import { assertNoRetainedFieldModelInput } from "@/lib/chat/retained-field/assertNoRetainedFieldModelInput"; import { convertToModelMessages, type ModelMessage, type UIMessage } from "ai"; /** @@ -14,5 +15,7 @@ import { convertToModelMessages, type ModelMessage, type UIMessage } from "ai"; export async function convertMessagesStep(messages: UIMessage[]): Promise { "use step"; + assertNoRetainedFieldModelInput(messages); + return convertToModelMessages(messages); } diff --git a/app/lib/workflows/routeChatModelStep.ts b/app/lib/workflows/routeChatModelStep.ts index 03e4f02b6..41d20bfdc 100644 --- a/app/lib/workflows/routeChatModelStep.ts +++ b/app/lib/workflows/routeChatModelStep.ts @@ -1,3 +1,4 @@ +import { assertNoRetainedFieldModelInput } from "@/lib/chat/retained-field/assertNoRetainedFieldModelInput"; import type { UIMessage, UIMessageChunk } from "ai"; import { selectChatModel } from "@/lib/ai/routing/selectChatModel"; @@ -8,6 +9,8 @@ export async function routeChatModelStep( ) { "use step"; + assertNoRetainedFieldModelInput(messages); + const writer = writable.getWriter(); try { await writer.write({ diff --git a/app/lib/workflows/runAgentStep.ts b/app/lib/workflows/runAgentStep.ts index f1b8525e8..55dde8bce 100644 --- a/app/lib/workflows/runAgentStep.ts +++ b/app/lib/workflows/runAgentStep.ts @@ -1,3 +1,4 @@ +import { assertNoRetainedFieldModelInput } from "@/lib/chat/retained-field/assertNoRetainedFieldModelInput"; import { streamText, type ModelMessage, type UIMessage, type UIMessageChunk } from "ai"; import { gateway } from "@ai-sdk/gateway"; import { agentCustomInstructions } from "@/lib/chat/agentCustomInstructions"; @@ -113,6 +114,8 @@ export type RunAgentStepResult = { export async function runAgentStep(input: RunAgentStepInput): Promise { "use step"; + assertNoRetainedFieldModelInput([input.modelMessages, input.originalMessages]); + console.log("[runAgentStep] start", { modelId: input.modelId, messageCount: input.modelMessages.length, @@ -194,6 +197,17 @@ export async function runAgentStep(input: RunAgentStepInput): Promise { + try { + assertNoRetainedFieldModelInput(output); + } catch (error) { + outputRefusal = error; + throw error; + } + }; let responseMessage: UIMessage | undefined; let finishReason: string; let responseMessages: ModelMessage[] = []; @@ -212,12 +226,14 @@ export async function runAgentStep(input: RunAgentStepInput): Promise { + assertOutput(finalMessage); responseMessage = finalMessage; }, })) { // A writer lock taken inside a step applies only within that step, so // sequential steps can share one stream. Release per part so the step's // request can terminate. + assertOutput(part); const writer = input.writable.getWriter(); try { await writer.write(part); @@ -233,6 +249,12 @@ export async function runAgentStep(input: RunAgentStepInput): Promise {}); + void Promise.resolve(result.response).catch(() => {}); + throw outputRefusal; + } // Three ways a user-stop surfaces here: the stream throws AbortError; the // poller already flipped our signal; or `run.cancel()` closed the workflow // writable underneath us and the write threw something unrelated before diff --git a/app/lib/workflows/runAgentWorkflow.ts b/app/lib/workflows/runAgentWorkflow.ts index 1ed44a66a..7c12c3545 100644 --- a/app/lib/workflows/runAgentWorkflow.ts +++ b/app/lib/workflows/runAgentWorkflow.ts @@ -1,3 +1,4 @@ +import { assertNoRetainedFieldModelInput } from "@/lib/chat/retained-field/assertNoRetainedFieldModelInput"; import { FatalError } from "workflow"; import type { TrustedFencedWorkflowInput } from "./fence/TrustedFencedWorkflowInput"; import { bindFencedWorkflowStep } from "./fence/bindFencedWorkflowStep"; @@ -122,8 +123,21 @@ export async function runAgentWorkflow( let streamFinished = false; let legacyCleanupEntered = false; + const assertInitialModelInput = (messages: unknown) => { + try { + assertNoRetainedFieldModelInput(messages); + } catch (error) { + // Only the new early privacy denial enters legacy cleanup. Other startup + // failures retain their existing ownership semantics; fenced paths never + // acquire legacy cleanup authority, and losing binds already threw above. + legacyCleanupEntered = !writer; + throw error; + } + }; // Fenced failures close only this run's stream; retained DB authority needs reconciliation. try { + // Pure workflow-body check reruns on replay; conversion itself is journaled. + assertInitialModelInput(input.messages); if (writer) { const latestUser = input.messages.findLast(message => message.role === "user"); if (!latestUser) throw new FatalError("Fenced turn requires a user message"); @@ -161,6 +175,7 @@ export async function runAgentWorkflow( // appends every iteration's `responseMessages` to it, which is how // iteration N+1 sees iteration N's tool results. const modelMessages = await convertMessagesStep(input.messages); + assertInitialModelInput(modelMessages); // The assistant message under construction. Threaded into each iteration // as `originalMessages` so the AI SDK keeps appending to it instead of @@ -226,6 +241,7 @@ export async function runAgentWorkflow( // it and the queue retried it 4 times, each attempt a full agent run // that mailed the customer again (chat#1918). for (let iteration = 0; iteration < CHAT_AGENT_MAX_ITERATIONS; iteration++) { + assertNoRetainedFieldModelInput([input.messages, modelMessages, pendingAssistantResponse]); result = await runAgentStep({ // Snapshot, not the live array — each iteration's input is a durable // step input and must describe the conversation as it was at THAT @@ -242,6 +258,8 @@ export async function runAgentWorkflow( assistantMessageId, }); + // Completed/replayed steps may introduce new tool results or UI companion parts. + assertNoRetainedFieldModelInput([result.responseMessage, result.responseMessages]); if (result.responseMessage) { pendingAssistantResponse = result.responseMessage; // Persist per iteration so a long turn's transcript stays live rather diff --git a/docs/contracts/catalog-field-consumer-2207.md b/docs/contracts/catalog-field-consumer-2207.md index d54d4f8e3..42ff52fda 100644 --- a/docs/contracts/catalog-field-consumer-2207.md +++ b/docs/contracts/catalog-field-consumer-2207.md @@ -136,3 +136,25 @@ Required consumer assertions, once gates allow implementation: exactly one descr Concrete source-inspection anchors for gate3: `save_context_metadata` in Database `supabase/migrations/20260920040000_context_guests.sql` accepts supplied URL/topic; `complete_context_enrichment` in `20260923070000_context_enrichment_scope.sql` writes supplied source items and accepts results. These are starting points for reserved-family guards, not an exhaustive writer/grant audit. At this API pin, no `read_binding`, `reconcile_retention` or `stream_binding_proposal` call exists under `app/` or `lib/`. Gate5's actual raw-history consumer is `lib/sessions/chats/getSessionChatHandler.ts`, which selects saved message parts following session/chat authorization without the proposed per-field source check. Next action: root/UI review this exact contract and schema ownership. Runtime work is blocked by selection origin, callable deployed authority and publication/history gates; document approval alone does not unblock a live implementation. + +## History withholding source increment — November 19, 2026 + +The existing session-chat GET handler now calls the [retained-field history +gate](../../lib/sessions/chats/retained-field/README.md) with the authenticated +account and validated session/chat/database-message anchors. Until a reviewed +current-source transport and publication protocol exist, marked retained-field +messages are omitted in full, including sibling text/numeric projections and +metadata. This is a default-denied history consumer, not a positive authority +implementation or activation of the proposed producer. Legacy raw-report +messages and controls retain their existing behavior. It does not qualify other +disclosure paths or derive provenance for unmarked text in separate messages. + +## Model-input withholding source increment — November 19, 2026 + +The existing workflow, Slack and evaluation callers now invoke the [deny-only +model boundary](../../lib/chat/retained-field/README.md) before conversion and +provider work. The workflow body checks again outside journaled steps and across +iteration results; general-agent tool loops check each next-call input. Marked +input fails as a whole, preserving legacy unmarked tool-call/result pairs rather +than pruning selected messages. This adds no positive source authority and does +not qualify unmarked prose, other model sources or stream/export disclosure. diff --git a/docs/plans/2026-11-18-pack-model-suitability.md b/docs/plans/2026-11-18-pack-model-suitability.md new file mode 100644 index 000000000..47e9d28df --- /dev/null +++ b/docs/plans/2026-11-18-pack-model-suitability.md @@ -0,0 +1,138 @@ +# CD-03: task-specific evidence, choices and recorded restrictions + +Source-only contribution toward recoupable/app#2266, based on API main +`30c4d442c75137fa803802091dbb3991f4646e8b`. No model, route, settings, price, +provider account, migration, deployment or activation changes. This does not +complete CD-03 or certify production availability or commercial permission. + +## Scope and behavior + +`prepare.brief.model_choices` is an optional strict object with `image`, +`image_edit`, `motion`, `caption`, `still_inspection`, `video_inspection`, and +`analysis` string values. REST and MCP use the same schema. Omission retains the +configured routes. Estimate rejects an explicit choice that differs from the +configured route or does not apply to the plan, before source import or estimate +persistence. Exact applicable choices remain in the brief fingerprint. No silent +substitution or alternative provider routing is introduced. + +Planning resolves production references from the selected creative context: + +| Planned work | Provider tasks assessed | +| --- | --- | +| Artwork, quote card, carousel | Image or image edit, caption, still inspection | +| Video | Image or image edit, caption, motion, video inspection | +| Visualizer | Image or image edit, caption, video inspection | +| Missing valid completed audio context | Add analysis | +| Local composition | No provider task | + +A targeted retry assesses only the selected asset. Its request accepts a job/asset +locator, not a replacement brief or model choices. Choices in the saved brief +continue to describe the original full pack: for a video-plus-artwork pack, an +artwork-only retry retains the original motion choice and full-brief fingerprint, +while its assets, budget and model assessment contain no motion work. A later +video retry still uses that exact saved motion choice. Preserving the choice is +not substitution or a promise to execute motion in every retry. + +Full-brief choices are validated against the full pack before selecting the retry +asset. Revalidating inherited choices as if they were newly supplied for the +subset would reject a legitimate retry with no retry-input mechanism to correct +it. Removing the inherited choice would change saved intent/provenance. Unsupported +saved choices still fail; a newly specified motion choice for an artwork-only +full pack remains nonapplicable and fails. No retry-specific model selection is +introduced. + +The same rule applies to a saved `model_choices.analysis`: after valid analysis +has completed, the original choice and brief fingerprint remain intact while the +new estimate omits analysis from its effective task assessment. Re-estimating does +not submit a new choice or repeat analysis. If new analysis is actually needed, +the relevant current restriction check still applies. + +Completed valid audio context is reused. Accepted-receipt recovery and retained caption reuse are not new +provider effects. Production checks use the actual image/reference route or actual +inspection file type, not the entire configured pipeline. + +## Assessment versus enforcement + +Unknown capability, commercial basis, account access and lifecycle remain unknown. +The new report is explicitly an assessment, not authorization. It does not create +a blanket unknown-evidence gate or a runtime bypass flag. **Enabling a full +verified-evidence admission policy remains a product/access decision**, requiring +real task/provider/account evidence, a migration and recovery contract, and +independent review. Deprecation alone is not retirement and does not block a call. + +Separately, a recorded commercial restriction or retirement on the exact current +model/endpoint refuses a genuinely new effect. A review expiry cannot erase a +recorded restriction. Changing an unrelated route's evidence cannot block the +current task. A mismatched binding is reported unknown rather than transferred to +a different model. No client assessment can authorize an effect or override the +server's restriction record. + +The current source records Music Flamingo's published noncommercial restriction. +New analysis on that exact model/endpoint is therefore refused before source +signing/fetching, context writes or inference. New generation/failed-retry +approval that needs analysis is refused before credit checks/reservation/dispatch. +**Fresh packs currently require that analysis and remain blocked by this specific +restriction.** No replacement or commercial grant was invented to remove it. +Packs with retained completed analysis can continue their applicable existing +provider path; their unknown evidence is not newly treated as verified. + +An already-funded running generate replay reaches the existing durable approve +idempotency branch without a new restriction admission check or credit check. +This exception is only the replay boundary: every genuinely new worker provider +effect still receives its own relevant restriction check. Failed-asset paid +retries are not exempt. Accepted receipts keep original route, request ID, input +fingerprint and charge; local composition and settlement do not require model +grants. Existing execution configuration checks remain in place. + +## Quote compatibility and provenance + +The existing route/settings/price `execution_config` and its digest are unchanged. +For the original configuration the digest remains +`012c31125aaae1e336fc23bfa69c7b22b9d6b0b4de9d9f93e7c00678caeb7a24`. +Legacy approved quotes lacking a model assessment remain supported under that +same integrity contract. Saved original quote fingerprints are not rewritten. +Genuine route/settings/price changes still follow the original requote rule. + +New quotes add optional `model_readiness`: review version/date, applicable task +and model/endpoint bindings, source references, statuses, issues, and a canonical +assessment digest. Their new quote fingerprint also includes that assessment +digest. Thus new estimates record changed evidence without invalidating existing +funded quotes merely because evidence/version/expiry changed. The persisted +assessment is historical evidence, not an execution capability token. Current +restriction checks read only server-owned evidence. + +Awaiting-approval readback exposes a separate current assessment and explains any +applicable recorded restriction. It preserves the stored quote assessment. Legacy +readback without a complete quote plan remains readable. + +## Public evidence reviewed 2026-11-18 + +- [NVIDIA model card](https://huggingface.co/nvidia/music-flamingo-2601-hf) + describes music audio understanding and identifies a noncommercial research + restriction and NVIDIA OneWay Noncommercial License. No separate commercial + grant or account availability was established. +- [OpenAI GPT-4o mini page](https://developers.openai.com/api/docs/models/gpt-4o-mini) + lists text/image inputs and text/structured outputs and marks the model deprecated. + Modality support does not prove application QA quality, Gateway availability or + commercial authorization. [Deprecations](https://developers.openai.com/api/docs/deprecations) + was also inspected; no retirement date is inferred from the older audit. +- Other routes are explicitly unverified in this review. Their exact source + bindings are not claims of provider availability or licensing. + +The 2026-12-18 review expiry is an internal review horizon, not a provider +retirement date. No account/config access was performed. + +## Verification and remaining limits + +Focused tests cover task variants, exact-choice and nonapplicable-choice refusal, +assessment provenance without execution-digest churn, new restricted analysis +before reservation, already-funded replay, and actual workflow preparation, +accepted polling, composition, inspection and settlement after unrelated evidence +changes. That recovery fixture uses real workflow/domain orchestration with +synthetic database/provider/media ports: it is not World execution, a real debit, +a provider call or production qualification. + +Existing isolated first-use/analysis fixtures mock only restriction admission to +continue testing their original behavior. No fixture manufactures production +commercial/account evidence. Independent review and release remain with the +coordinator. Held foundation/database and SDK checkouts are not dependencies. diff --git a/lib/agents/generalAgent/__tests__/getGeneralAgent.test.ts b/lib/agents/generalAgent/__tests__/getGeneralAgent.test.ts index 41f316891..16db5382f 100644 --- a/lib/agents/generalAgent/__tests__/getGeneralAgent.test.ts +++ b/lib/agents/generalAgent/__tests__/getGeneralAgent.test.ts @@ -1,6 +1,8 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import { ChatRequestBody } from "@/lib/chat/validateChatRequest"; -import { ToolLoopAgent } from "ai"; +import { ToolLoopAgent, tool } from "ai"; +import { MockLanguageModelV3 } from "ai/test"; +import { z } from "zod"; // Import after mocks import getGeneralAgent from "../getGeneralAgent"; @@ -80,6 +82,109 @@ describe("getGeneralAgent", () => { mockBuildSystemPromptWithImages.mockReturnValue(finalInstructions); }); + it("rejects raw marked messages before account/knowledge reads", async () => { + mockSelectAccountInfo.mockResolvedValue({ + knowledges: [{ id: "knowledge-1", url: "https://knowledge.invalid/private" }], + } as never); + await expect( + getGeneralAgent({ + accountId: "real-actor", + artistId: "artist-456", + orgId: null, + messages: [{ role: "assistant", parts: [{ mode: "retained_field" }] }], + } as never), + ).rejects.toThrow("Retained-field model input unavailable"); + expect(mockGetKnowledgeBaseText).not.toHaveBeenCalled(); + expect(mockSelectAccountInfo).not.toHaveBeenCalled(); + expect(mockSelectAccountEmails).not.toHaveBeenCalled(); + }); + it("actual ToolLoopAgent rejects mixed file and marked tool-result input before download/provider invocation", async () => { + const model = new MockLanguageModelV3(); + mockSetupToolsForRequest.mockResolvedValue({}); + const { agent } = await getGeneralAgent({ + accountId: "real-actor", + orgId: null, + messages: [], + model, + } as never); + const fetch = vi.fn(() => { + throw new Error("NETWORK FORBIDDEN"); + }); + vi.stubGlobal("fetch", fetch); + try { + await expect( + agent.generate({ + messages: [ + { + role: "user", + content: [ + { type: "text", text: "PRIVATE 987654321" }, + { + type: "file", + data: new URL("https://download.invalid/private"), + mediaType: "text/plain", + }, + ], + }, + { + role: "tool", + content: [ + { + type: "tool-result", + toolName: "source", + toolCallId: "call", + output: { type: "json", value: { mode: "retained_field" } }, + }, + ], + }, + ] as never, + }), + ).rejects.toThrow("Retained-field model input unavailable"); + expect(model.doGenerateCalls).toHaveLength(0); + expect(fetch).not.toHaveBeenCalled(); + } finally { + vi.unstubAllGlobals(); + } + }); + it("actual ToolLoopAgent stops before the second provider call on a marked tool result", async () => { + const model = new MockLanguageModelV3({ + doGenerate: { + content: [{ type: "tool-call", toolCallId: "call", toolName: "source", input: "{}" }], + finishReason: { unified: "tool-calls", raw: "tool-calls" }, + usage: { + inputTokens: { total: 1, noCache: 1, cacheRead: 0, cacheWrite: 0 }, + outputTokens: { total: 1, text: 1, reasoning: 0 }, + }, + warnings: [], + }, + }); + mockSetupToolsForRequest.mockResolvedValue({ + source: tool({ + inputSchema: z.object({}), + execute: async () => ({ mode: "retained_field", amount: 987654321, text: "PRIVATE" }), + }), + } as never); + const { agent } = await getGeneralAgent({ + accountId: "real-actor", + orgId: null, + messages: [], + model, + } as never); + const fetch = vi.fn(() => { + throw new Error("NETWORK FORBIDDEN"); + }); + vi.stubGlobal("fetch", fetch); + try { + await expect( + agent.generate({ messages: [{ role: "user", content: "ordinary request" }] }), + ).rejects.toThrow("Retained-field model input unavailable"); + expect(model.doGenerateCalls).toHaveLength(1); + expect(JSON.stringify(model.doGenerateCalls)).not.toContain("987654321"); + expect(fetch).not.toHaveBeenCalled(); + } finally { + vi.unstubAllGlobals(); + } + }); describe("basic functionality", () => { it("returns a RoutingDecision object with required properties", async () => { const body: ChatRequestBody = { diff --git a/lib/agents/generalAgent/getGeneralAgent.ts b/lib/agents/generalAgent/getGeneralAgent.ts index ea48d5257..e416e11f1 100644 --- a/lib/agents/generalAgent/getGeneralAgent.ts +++ b/lib/agents/generalAgent/getGeneralAgent.ts @@ -1,3 +1,4 @@ +import { assertNoRetainedFieldModelInput } from "@/lib/chat/retained-field/assertNoRetainedFieldModelInput"; import { ToolLoopAgent } from "ai"; import { CHAT_AGENT_STOP_WHEN } from "@/lib/chat/const"; import { GoogleGenerativeAIProviderOptions } from "@ai-sdk/google"; @@ -23,6 +24,7 @@ import { getAnthropicProviderOptions } from "@/lib/agents/generalAgent/getAnthro * @returns The general agent */ export default async function getGeneralAgent(body: ChatRequestBody): Promise { + assertNoRetainedFieldModelInput(body.messages); const { accountId, orgId, messages, artistId, model: bodyModel } = body; const accountEmails = await selectAccountEmails({ accountIds: accountId }); @@ -61,7 +63,12 @@ export default async function getGeneralAgent(body: ChatRequestBody): Promise { + assertNoRetainedFieldModelInput([ + options.messages, + options.steps.map(step => step.toolResults), + ]); const next = getPrepareStepResult(options); + assertNoRetainedFieldModelInput(next?.messages); if (next) return { ...options, ...next }; return options; }, diff --git a/lib/chat/__tests__/setupChatRequest.test.ts b/lib/chat/__tests__/setupChatRequest.test.ts index bd2ac4e14..38ba9def4 100644 --- a/lib/chat/__tests__/setupChatRequest.test.ts +++ b/lib/chat/__tests__/setupChatRequest.test.ts @@ -165,3 +165,50 @@ describe("setupChatRequest", () => { }); }); }); + +const retainedMessage = { + id: "private", + role: "assistant", + parts: [ + { type: "data-retained-field-snapshot", data: { amount: 987654321 } }, + { type: "text", text: "PRIVATE 987654321" }, + { type: "file", url: "https://download.invalid/private", mediaType: "text/plain" }, + ], +}; +it("rejects retained input before agent setup and converter downloads, without pruning tool pairs", async () => { + vi.clearAllMocks(); + const messages = [ + { + role: "assistant", + content: [{ type: "tool-call", toolCallId: "safe-call", toolName: "safe", input: {} }], + }, + { + role: "tool", + content: [ + { + type: "tool-result", + toolCallId: "safe-call", + toolName: "safe", + output: { type: "json", value: { total: 0 } }, + }, + ], + }, + retainedMessage, + ]; + const originalMessages = structuredClone(messages); + await expect( + setupChatRequest({ messages, accountId: "real-actor", orgId: null } as never), + ).rejects.toThrow("Retained-field model input unavailable"); + expect(messages).toEqual(originalMessages); + expect(mockGetGeneralAgent).not.toHaveBeenCalled(); + expect(mockConvertToModelMessages).not.toHaveBeenCalled(); +}); +it("rejects a marked converted result before returning a model-ready config", async () => { + mockGetGeneralAgent.mockResolvedValue({ agent: { tools: {} } } as never); + mockConvertToModelMessages.mockResolvedValue([ + { role: "tool", content: [{ output: { type: "json", value: { mode: "retained_field" } } }] }, + ] as never); + await expect( + setupChatRequest({ messages: [], accountId: "real-actor", orgId: null }), + ).rejects.toThrow("Retained-field model input unavailable"); +}); diff --git a/lib/chat/retained-field/README.md b/lib/chat/retained-field/README.md new file mode 100644 index 000000000..bbf98485f --- /dev/null +++ b/lib/chat/retained-field/README.md @@ -0,0 +1,102 @@ +# Retained-field model-input denial (2207) + +The existing model consumers now deny the entire invocation when structured +retained-field content is present. They reuse the history gate's reserved-family +recognizer. No messages or tool parts are removed: preserving complete unmarked +tool-call/result pairs avoids silently corrupting the model conversation. +The failure is the existing workflow `FatalError` with only the static message +`Retained-field model input unavailable`. It includes no content, amount, IDs, +source diagnostics or authority details and is not retryable by the workflow. + +## Actual callers and boundaries + +- `runAgentWorkflow` checks original UI messages in the workflow body before + conversion and routing. This is not a journaled step and is evaluated again + when the body replays. It also checks the returned converted messages, each + iteration's input and every iteration's returned UI/model messages before + persistence, another iteration or auto-commit's later model work. +- `convertMessagesStep` checks before `convertToModelMessages` can perform I/O. + Its result is journaled, so this check alone would be insufficient. +- `routeChatModelStep` checks before routing metadata or the Jev evaluator. +- `runAgentStep` checks both model and original UI messages before polling, + gateway construction and `streamText`, including independently resumed steps. + It also checks each structured UI stream part before acquiring a writer or + writing any of that part. A marked part is withheld in full, including sibling + payloads. The accumulated `onFinish` message is checked before capture because + the SDK can finish it during stream cancellation. A privacy refusal remains a + fatal rejection even when cancellation races it; no refused output is returned + as an ordinary aborted result for persistence. Existing poller/provider abort + cleanup runs, while shared-stream closure stays with the workflow. +- `setupChatRequest` checks original messages before `getGeneralAgent` or + conversion, then converted messages before legacy truncation/config return. + Its actual callers are Slack's `handleSlackChatMessage` and evaluation's + `callChatFunctionsWithResult`. +- Slack checks saved `memory.content` before reconstructing UI messages, because + that reconstruction intentionally drops metadata. Its authenticated API-key + account and room scope are unchanged. Existing redacted error/idle handling + remains; no automatic retry is added. +- Email's `generateEmailResponse` loads history through `getEmailRoomMessages`, + which checks raw stored content before extracting text and dropping metadata. + Marked history denies the entire generation, including sibling text; the + existing inbound handler retains its generic error response. Room lookup and + authenticated account/organization context are unchanged. +- `getGeneralAgent` checks before account/knowledge/tool setup. Its actual + `ToolLoopAgent.prepareStep` checks messages and tool results every iteration, + and checks any tool-chain replacement messages. In the installed SDK this + runs before prompt conversion/download and provider invocation. Thus a marked + tool result cannot be sent back to the model on its next call. + +No owner/workspace substitution, new authorization input or local approval flag +is introduced. Existing actual server account/session/chat context is passed +unchanged. Cached ready/preparation/disclosure flags never allow marked input. +There is no positive current-source transport, latest lookup, re-key, retention, +expansion, raw-report fallback, public route, grant or activation. + +## Qualification + +Stream-output regression tests invoke the actual `runAgentStep` with synthetic +stream/provider/cancellation seams. They cover running, cancelled and already +aborted runs, withheld sibling payloads, marked `onFinish` state, rejection of +pending provider promises during cleanup, and unchanged unmarked tool output. +This guard does not prevent the operation that produced the output or retract +independent prose already emitted; it denies recognized structured output only. + +Tests invoke real workflow orchestration/conversion/routing/provider-step entry +points with synthetic effect seams; replay cases supply the already-completed +step outputs that those callers can receive. They are not a claim of live WDK +journal migration or replay against production state. The compiled offline +workflow fixture additionally qualifies marked original input, converted output +and iteration results as actual failed runs. Independently captured journals show +completed conversion and agent-step results followed by workflow resumption, +stream closure and static failure, with no subsequent routing/provider call for +marked conversion and no second provider call for a marked iteration result. + +The fixture also re-enqueues each run after it has already failed. The runtime +skips terminal runs, so the unchanged effect trace proves terminal duplicate +delivery is a no-op. It does not prove another execution of the disclosure +assertion. That is distinct from the running workflow's journaled-step completion +and resumption evidence above. Synthetic effect seams remain in place; no live +authorization or existing production journal migration is claimed. + +Slack/evaluation tests use their real caller and setup functions with synthetic +account/storage/agent seams. Actual `ToolLoopAgent.generate` tests use the SDK's +`MockLanguageModelV3`: initial marked input causes zero provider calls/downloads; +an unmarked first call that returns a marked tool result stops before the second +provider call. Network is prohibited. No live model, billing, auth, history or +provider request is made. + +## Remaining gaps + +This qualifies marked message inputs for these callers only. It cannot reconstruct +provenance after another source has already removed all markers or converted the +content to unmarked prose/serialized text. It does not classify independent system +prompts, knowledge content, other model callers, streamed output already emitted, +metadata/export surfaces or already-delivered clients. It does not retract an +already-completed provider call. Early raw/conversion privacy denials in +`runAgentWorkflow` now enter the existing legacy cleanup: +CAS-scoped active-stream clear, one terminal finish before writable closure, and +headless ephemeral-key revocation when present. Unrelated startup failures keep +their prior behavior. Post-bind fenced failures close their stream without legacy cleanup +or release, and a losing fenced bind acquires no cleanup authority. No retry or +account/grant policy is added. Positive +current-authority/publication integration and other disclosure paths remain open. diff --git a/lib/chat/retained-field/__tests__/modelCallers.test.ts b/lib/chat/retained-field/__tests__/modelCallers.test.ts new file mode 100644 index 000000000..7c1d97ddb --- /dev/null +++ b/lib/chat/retained-field/__tests__/modelCallers.test.ts @@ -0,0 +1,114 @@ +import { beforeEach, afterEach, it, expect, vi } from "vitest"; +import { handleSlackChatMessage } from "@/lib/slack/chat/handlers/handleSlackChatMessage"; +import { callChatFunctionsWithResult } from "@/lib/evals/callChatFunctionsWithResult"; +const mocks = vi.hoisted(() => ({ + memories: vi.fn(), + setup: vi.fn(), + key: vi.fn(), + agent: vi.fn(), + convert: vi.fn(), + generate: vi.fn(), + save: vi.fn(), +})); +vi.mock("@/lib/supabase/memories/selectMemories", () => ({ default: mocks.memories })); +vi.mock("@/lib/chat/setupConversation", () => ({ setupConversation: mocks.setup })); +vi.mock("@/lib/keys/getApiKeyDetails", () => ({ getApiKeyDetails: mocks.key })); +vi.mock("@/lib/chat/saveChatCompletion", () => ({ saveChatCompletion: mocks.save })); +vi.mock("@/lib/agents/generalAgent/getGeneralAgent", () => ({ default: mocks.agent })); +vi.mock("@/lib/consts", () => ({ + DEFAULT_MODEL: "synthetic", + EVAL_ACCOUNT_ID: "eval-actor", + EVAL_ACCESS_TOKEN: "synthetic", +})); +vi.mock("ai", async original => ({ + ...(await original()), + convertToModelMessages: mocks.convert, +})); + +beforeEach(() => { + vi.clearAllMocks(); + vi.stubGlobal( + "fetch", + vi.fn(() => { + throw new Error("NETWORK FORBIDDEN"); + }), + ); + vi.spyOn(console, "error").mockImplementation(() => {}); + mocks.key.mockResolvedValue({ accountId: "key-actor" }); + mocks.setup.mockResolvedValue({ roomId: "real-room" }); + mocks.agent.mockResolvedValue({ agent: { tools: {}, generate: mocks.generate } }); + mocks.convert.mockResolvedValue([{ role: "user", content: "safe" }]); + mocks.generate.mockResolvedValue({ text: "safe answer", steps: [] }); +}); +afterEach(() => { + expect(fetch).not.toHaveBeenCalled(); + vi.unstubAllGlobals(); + vi.restoreAllMocks(); +}); +function thread() { + return { + state: Promise.resolve({ status: "idle", roomId: "real-room" } as never), + post: vi.fn(), + setState: vi.fn(), + }; +} +it.each(["metadata", "parts"])( + "Slack rejects retained %s before reconstruction, conversion or generation", + async location => { + const message = { + role: "assistant", + parts: [{ type: "text", text: "PRIVATE 987654321" }], + [location]: + location === "metadata" + ? { mode: "retained_field" } + : [{ type: "text", text: "PRIVATE 987654321" }, { type: "data-retained-field-snapshot" }], + }; + mocks.memories.mockResolvedValue([{ id: "saved", content: message }]); + const target = thread(); + await handleSlackChatMessage(target, "ordinary prompt"); + expect(mocks.agent).not.toHaveBeenCalled(); + expect(mocks.convert).not.toHaveBeenCalled(); + expect(mocks.generate).not.toHaveBeenCalled(); + expect(mocks.save).not.toHaveBeenCalled(); + expect(JSON.stringify(target.post.mock.calls)).not.toContain("987654321"); + expect(JSON.stringify(vi.mocked(console.error).mock.calls)).not.toContain("987654321"); + expect(target.setState).toHaveBeenLastCalledWith({ + status: "idle", + prompt: "ordinary prompt", + roomId: "real-room", + }); + }, +); +it("Slack preserves the server key account and ordinary unmarked generation", async () => { + mocks.memories.mockResolvedValue([ + { id: "saved", content: { role: "assistant", parts: [{ type: "text", text: "safe" }] } }, + ]); + await handleSlackChatMessage(thread(), "ordinary prompt"); + expect(mocks.agent).toHaveBeenCalledWith( + expect.objectContaining({ accountId: "key-actor", roomId: "real-room" }), + ); + expect(mocks.generate).toHaveBeenCalledTimes(1); +}); +it("evaluation caller cannot send an unexpected marked converted result to generate", async () => { + mocks.convert.mockResolvedValue([ + { + role: "tool", + content: [ + { + type: "tool-result", + output: { type: "json", value: { mode: "retained_field", amount: 987654321 } }, + }, + ], + }, + ]); + await expect(callChatFunctionsWithResult("ordinary prompt")).rejects.toThrow( + "Retained-field model input unavailable", + ); + expect(mocks.generate).not.toHaveBeenCalled(); +}); +it("evaluation caller preserves ordinary unmarked behavior", async () => { + await expect(callChatFunctionsWithResult("ordinary prompt")).resolves.toMatchObject({ + text: "safe answer", + }); + expect(mocks.generate).toHaveBeenCalledTimes(1); +}); diff --git a/lib/chat/retained-field/assertNoRetainedFieldModelInput.ts b/lib/chat/retained-field/assertNoRetainedFieldModelInput.ts new file mode 100644 index 000000000..85d5f6509 --- /dev/null +++ b/lib/chat/retained-field/assertNoRetainedFieldModelInput.ts @@ -0,0 +1,10 @@ +import { FatalError } from "workflow"; +import { hasRetainedFieldContent } from "@/lib/sessions/chats/retained-field/hasRetainedFieldContent"; + +/** Deny the entire invocation, never prune messages and orphan tool-call/result pairs. */ +export function assertNoRetainedFieldModelInput(input: unknown): void { + if (hasRetainedFieldContent(input)) { + // Static redacted failure. No positive authority, retries or cached-ready admission. + throw new FatalError("Retained-field model input unavailable"); + } +} diff --git a/lib/chat/setupChatRequest.ts b/lib/chat/setupChatRequest.ts index 949ca29cb..cbddef40e 100644 --- a/lib/chat/setupChatRequest.ts +++ b/lib/chat/setupChatRequest.ts @@ -1,3 +1,4 @@ +import { assertNoRetainedFieldModelInput } from "@/lib/chat/retained-field/assertNoRetainedFieldModelInput"; import { convertToModelMessages } from "ai"; import { MAX_MESSAGES } from "./const"; import { type ChatConfig } from "./types"; @@ -16,17 +17,17 @@ import getGeneralAgent from "@/lib/agents/generalAgent/getGeneralAgent"; * @returns ChatConfig ready for use with AI SDK */ export async function setupChatRequest(body: ChatRequestBody): Promise { + assertNoRetainedFieldModelInput(body.messages); const decision = await getGeneralAgent(body); - const convertedMessages = ( - await convertToModelMessages(body.messages, { - tools: decision.agent.tools, - ignoreIncompleteToolCalls: true, - }) - ).slice(-MAX_MESSAGES); + const convertedMessages = await convertToModelMessages(body.messages, { + tools: decision.agent.tools, + ignoreIncompleteToolCalls: true, + }); + assertNoRetainedFieldModelInput(convertedMessages); return { agent: decision.agent, - messages: convertedMessages, + messages: convertedMessages.slice(-MAX_MESSAGES), }; } diff --git a/lib/content/packs/__tests__/createPackRecoveryCommandMock.ts b/lib/content/packs/__tests__/createPackRecoveryCommandMock.ts new file mode 100644 index 000000000..1cf6aaef1 --- /dev/null +++ b/lib/content/packs/__tests__/createPackRecoveryCommandMock.ts @@ -0,0 +1,41 @@ +import type { runPackCommand } from "@/lib/supabase/release_pack_jobs/runPackCommand"; +import type { PackActor, PackSnapshot } from "../packTypes"; +/** In-memory persistence with enforced targets; not a production accounting simulation. */ +export function createPackRecoveryCommandMock( + saved: PackSnapshot, + expectedActor: PackActor, +): typeof runPackCommand { + return async (actor, command, jobId, input = {}) => { + if ( + actor.accountId !== expectedActor.accountId || + actor.organizationId !== expectedActor.organizationId + ) + throw new Error("Unexpected actor"); + if (jobId !== saved.job.id) throw new Error("Unexpected job"); + if (command === "phase" || command === "finish_asset") { + const target = saved.assets.find( + asset => asset.id === input.asset_id && asset.job_id === jobId, + ); + if (!target) throw new Error("Unexpected asset"); + if (command === "phase") { + if (typeof input.phase !== "string") throw new Error("Missing phase"); + target.provider_state[input.phase] = structuredClone(input.value); + } else Object.assign(target, structuredClone(input)); + } else if (command === "settle") { + const target = saved.assets.find(asset => asset.id === "asset"); + const receipt = target?.provider_state.image_submission_0; + if ( + target?.status !== "completed" || + typeof receipt !== "object" || + !receipt || + !("charge_credits" in receipt) || + receipt.charge_credits !== target.credits + ) + throw new Error("Recovery did not complete the original asset and receipt"); + // Acknowledge orchestration only. Accepted work was charged before recovery; + // this mock must not invent wallet/refund/debit behavior or bill it again. + } else if (command !== "run" && command !== "read") + throw new Error(`Unexpected command ${command}`); + return structuredClone(saved); + }; +} diff --git a/lib/content/packs/__tests__/createPackRecoverySnapshot.ts b/lib/content/packs/__tests__/createPackRecoverySnapshot.ts new file mode 100644 index 000000000..eac326598 --- /dev/null +++ b/lib/content/packs/__tests__/createPackRecoverySnapshot.ts @@ -0,0 +1,81 @@ +import { buildReleasePackQuote } from "../buildReleasePackQuote"; +import { releasePackBriefSchema, releasePackDeliverablesSchema } from "../releasePackSchema"; +import type { PackAsset, PackSnapshot } from "../packTypes"; +export function createPackRecoverySnapshot(): PackSnapshot { + const brief = releasePackBriefSchema.parse({ + artist_name: "Fixture", + song_title: "Song", + occasion: "rediscovery", + audio_url: "https://example.invalid/song.wav", + allow_creative_defaults: true, + }); + const deliverables = releasePackDeliverablesSchema.parse({ + videos: 0, + artwork: 1, + quote_cards: 0, + carousels: 0, + visualizers: 0, + }); + const { model_readiness: _assessment, ...quote } = buildReleasePackQuote(brief, deliverables); + const plan = quote.assets[0]; + // Accepted submission was already charged; this is the post-resume snapshot. + const asset: PackAsset & { billed_credits: number } = { + id: "asset", + job_id: "job", + attempt: 1, + credits: plan.credits, + billed_credits: plan.credits, + maximum_usd: plan.maximum_usd, + files: [], + caption: null, + qa: null, + error: null, + kind: "artwork", + ordinal: 0, + status: "running", + provider_state: { + image_submission_0: { + state: "accepted", + request_id: "retained", + model: "original-image-route", + charge_credits: plan.credits, + input_sha256: "original-input", + }, + caption: "Retained caption", + }, + }; + const saved: PackSnapshot = { + job: { + id: "job", + owner_id: "owner", + created_by: "owner", + workflow_run_id: "saved-workflow", + error: null, + revision: 1, + status: "running", + held_credits: quote.credits - plan.credits, + consumed_credits: plan.credits, + brief, + deliverables, + quote: { + ...quote, + id: "quote", + revision: 1, + expires_at: "2099-01-01T00:00:00Z", + fingerprint: "original-approved-fingerprint", + }, + production_context: { + state: "complete", + audio: { storage_key: "owner/release-packs/song.wav" }, + references: [], + summary: "Sparse piano and percussion", + visual_direction: "Cream paper and ink", + proposed_start_seconds: 0, + selected_start_seconds: 0, + duration_seconds: 30, + }, + }, + assets: [asset], + }; + return saved; +} diff --git a/lib/content/packs/__tests__/packAnalysisReadiness.test.ts b/lib/content/packs/__tests__/packAnalysisReadiness.test.ts index 436a79cb6..238b6ed5f 100644 --- a/lib/content/packs/__tests__/packAnalysisReadiness.test.ts +++ b/lib/content/packs/__tests__/packAnalysisReadiness.test.ts @@ -10,6 +10,8 @@ import { buildReleasePackQuote } from "../buildReleasePackQuote"; import { releasePackBriefSchema, releasePackDeliverablesSchema } from "../releasePackSchema"; import type { PackJob } from "../packTypes"; +// This fixture isolates existing workflow/analysis behavior; restriction admission is tested separately. +vi.mock("../assertPackModelRestrictions", () => ({ assertPackModelRestrictions: vi.fn() })); vi.mock("@/lib/flamingo/callFlamingoGenerate", () => ({ callFlamingoGenerate: vi.fn() })); vi.mock("@/lib/supabase/storage/createSignedFileUrlByKey", () => ({ createSignedFileUrlByKey: vi.fn(), diff --git a/lib/content/packs/__tests__/packConfigurationExecution.test.ts b/lib/content/packs/__tests__/packConfigurationExecution.test.ts index e535669c7..1d46a4a36 100644 --- a/lib/content/packs/__tests__/packConfigurationExecution.test.ts +++ b/lib/content/packs/__tests__/packConfigurationExecution.test.ts @@ -1,4 +1,7 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { createSignedFileUrlByKey } from "@/lib/supabase/storage/createSignedFileUrlByKey"; +import { fetchPackMedia } from "../fetchPackMedia"; +import * as modelEvidence from "../getPackModelEvidence"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import fal from "@/lib/fal/server"; import { generateText } from "ai"; import { createPackProductionServices } from "../createPackProductionServices"; @@ -18,10 +21,12 @@ vi.mock("@/lib/supabase/storage/createSignedFileUrlByKey", () => ({ createSignedFileUrlByKey: vi.fn(), })); vi.mock("@/lib/flamingo/callFlamingoGenerate", () => ({ callFlamingoGenerate: vi.fn() })); +vi.mock("../fetchPackMedia", () => ({ fetchPackMedia: vi.fn() })); vi.mock("../composePackMedia", () => ({ composePackMedia: vi.fn() })); vi.mock("../inspectPackMedia", () => ({ inspectPackMedia: vi.fn() })); vi.mock("../withPackMediaRuntime", () => ({ withPackMediaRuntime: vi.fn() })); beforeEach(() => vi.resetAllMocks()); +afterEach(() => vi.restoreAllMocks()); const brief = releasePackBriefSchema.parse({ artist_name: "Fixture", song_title: "Song", @@ -107,4 +112,65 @@ describe("configuration guard at production boundaries", () => { await services.generateImages("prompt", 1); expect(fal.queue.submit).toHaveBeenCalledTimes(1); }); + it("refuses only new restricted analysis before storage or provider effects", async () => { + const { job } = fixture({}, true); + delete job.quote.model_readiness; + await expect(preparePackProduction({ accountId: "owner" }, job)).rejects.toThrow( + /analysis.*commercial use restricted/, + ); + expect(callFlamingoGenerate).not.toHaveBeenCalled(); + expect(runPackCommand).not.toHaveBeenCalled(); + expect(createSignedFileUrlByKey).not.toHaveBeenCalled(); + expect(fetchPackMedia).not.toHaveBeenCalled(); + }); + it("keeps legacy accepted receipt recovery, caption and local composition independent of motion evidence", async () => { + const evidence = modelEvidence.getPackModelEvidence(); + evidence.tasks.motion.commercial_basis = "restricted"; + vi.spyOn(modelEvidence, "getPackModelEvidence").mockReturnValue(evidence); + const { job, services } = fixture( + { + image_submission_0: { + state: "accepted", + request_id: "accepted", + model: "original-route", + charge_credits: 100, + }, + }, + true, + ); + delete job.quote.model_readiness; + vi.mocked(fal.queue.status).mockResolvedValue({ status: "COMPLETED" } as never); + vi.mocked(fal.queue.result).mockResolvedValue({ + data: { images: [{ url: "https://example.com/retained.png" }] }, + } as never); + expect(await services.generateImages("prompt", 1)).toEqual([ + "https://example.com/retained.png", + ]); + expect(fal.queue.status).toHaveBeenCalledWith("original-route", { + requestId: "accepted", + logs: false, + }); + expect(fal.queue.submit).not.toHaveBeenCalled(); + vi.mocked(generateText).mockResolvedValue({ text: "Retained caption" } as never); + expect(await services.caption("prompt")).toBe("Retained caption"); + await services.compose({} as any); + expect(composePackMedia).toHaveBeenCalledOnce(); + await expect(services.generateMotion("prompt", "image")).rejects.toThrow( + /motion.*commercial use restricted/, + ); + expect(fal.queue.submit).not.toHaveBeenCalled(); + expect(callFlamingoGenerate).not.toHaveBeenCalled(); + }); + it("checks only the inspection provider required by the actual output", () => { + const evidence = modelEvidence.getPackModelEvidence(); + evidence.tasks.video_inspection.lifecycle = "retired"; + vi.spyOn(modelEvidence, "getPackModelEvidence").mockReturnValue(evidence); + const { services } = fixture({}, true); + services.inspect({ files: [{ content_type: "image/png" }] } as any); + expect(inspectPackMedia).toHaveBeenCalledOnce(); + expect(() => services.inspect({ files: [{ content_type: "video/mp4" }] } as any)).toThrow( + /video_inspection.*model retired/, + ); + expect(inspectPackMedia).toHaveBeenCalledOnce(); + }); }); diff --git a/lib/content/packs/__tests__/packCostAccounting.test.ts b/lib/content/packs/__tests__/packCostAccounting.test.ts index 3a5c566d7..27ce7771a 100644 --- a/lib/content/packs/__tests__/packCostAccounting.test.ts +++ b/lib/content/packs/__tests__/packCostAccounting.test.ts @@ -48,6 +48,7 @@ describe("pack quote cost accounting", () => { brief, deliverables, execution_config_digest: quote.execution_config_digest, + model_readiness_digest: quote.model_readiness.digest, }), ) .digest("hex"), diff --git a/lib/content/packs/__tests__/packFirstUse.test.ts b/lib/content/packs/__tests__/packFirstUse.test.ts index 09fffd093..68e8eae5f 100644 --- a/lib/content/packs/__tests__/packFirstUse.test.ts +++ b/lib/content/packs/__tests__/packFirstUse.test.ts @@ -5,6 +5,8 @@ import { importPackSources } from "../importPackSources"; import { start } from "workflow/api"; import { readAdmittedPackSnapshot } from "../foundation/readAdmittedPackSnapshot"; const fixture = vi.hoisted(() => ({ saved: null as any, composedAudio: "" })); +// This fixture isolates existing workflow/analysis behavior; restriction admission is tested separately. +vi.mock("../assertPackModelRestrictions", () => ({ assertPackModelRestrictions: vi.fn() })); vi.mock("@/lib/supabase/serverClient", () => ({ default: {} })); vi.mock("@/lib/organizations/validateOrganizationAccess", () => ({ validateOrganizationAccess: vi.fn(async () => true), diff --git a/lib/content/packs/__tests__/packModelSuitability.test.ts b/lib/content/packs/__tests__/packModelSuitability.test.ts new file mode 100644 index 000000000..8037b4fa2 --- /dev/null +++ b/lib/content/packs/__tests__/packModelSuitability.test.ts @@ -0,0 +1,97 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { buildReleasePackQuote } from "../buildReleasePackQuote"; +import { releasePackBriefSchema, releasePackDeliverablesSchema } from "../releasePackSchema"; +import { assertPackQuoteConfiguration } from "../assertPackQuoteConfiguration"; +import { getPackConfigurationDigest } from "../getPackConfigurationDigest"; +import * as evidence from "../getPackModelEvidence"; +const brief = releasePackBriefSchema.parse({ + artist_name: "Fixture", + song_title: "Song", + occasion: "rediscovery", + audio_url: "https://example.com/song.wav", + allow_creative_defaults: true, +}); +const only = (kind: string) => + releasePackDeliverablesSchema.parse({ + videos: 0, + artwork: 0, + quote_cards: 0, + carousels: 0, + visualizers: 0, + [kind]: 1, + }); +afterEach(() => vi.restoreAllMocks()); +describe("model suitability assessment without production activation", () => { + it.each([ + ["artwork", ["analysis", "image", "caption", "still_inspection"]], + ["videos", ["analysis", "image", "caption", "motion", "video_inspection"]], + ["visualizers", ["analysis", "image", "caption", "video_inspection"]], + ])("reports only planned provider tasks for %s", (kind, tasks) => { + const quote = buildReleasePackQuote(brief, only(kind as string)); + expect(Object.keys(quote.model_readiness.tasks)).toEqual(tasks); + expect(quote.model_readiness.assessment_only).toBe(true); + expect(quote.model_readiness.ready).toBe(false); + expect(quote.model_readiness.tasks.analysis?.issues).toContain("commercial basis restricted"); + expect(() => assertPackQuoteConfiguration(quote)).not.toThrow(); + }); + it("uses the editing route for selected production references", () => { + const quote = buildReleasePackQuote( + { ...brief, reference_image_urls: ["https://example.com/art.png"] }, + only("artwork"), + ); + expect(quote.model_readiness.tasks.image_edit).toBeDefined(); + expect(quote.model_readiness.tasks.image).toBeUndefined(); + expect(() => + buildReleasePackQuote( + { + ...brief, + reference_image_urls: ["https://example.com/art.png"], + model_choices: { image: "meta/muse-image/text-to-image" }, + }, + only("artwork"), + ), + ).toThrow(/does not apply/); + expect(() => + buildReleasePackQuote( + { ...brief, model_choices: { motion: "minimax/h3-max/image-to-video" } }, + only("artwork"), + ), + ).toThrow(/does not apply/); + }); + it("keeps old execution identity and changes only new assessment fingerprints", () => { + const quote = buildReleasePackQuote(brief, only("artwork")); + expect(quote.execution_config_digest).toBe( + "012c31125aaae1e336fc23bfa69c7b22b9d6b0b4de9d9f93e7c00678caeb7a24", + ); + const changed = evidence.getPackModelEvidence(); + changed.version = "updated"; + vi.spyOn(evidence, "getPackModelEvidence").mockReturnValue(changed); + expect(() => assertPackQuoteConfiguration(quote)).not.toThrow(); + const replacement = buildReleasePackQuote(brief, only("artwork")); + expect(replacement.execution_config_digest).toBe(quote.execution_config_digest); + expect(replacement.model_readiness.digest).not.toBe(quote.model_readiness.digest); + expect(replacement.fingerprint).not.toBe(quote.fingerprint); + const { digest, ...report } = replacement.model_readiness; + expect(digest).toBe(getPackConfigurationDigest(report)); + }); + it("retains exact explicit choices and rejects substitution", () => { + const selected = releasePackBriefSchema.parse({ + ...brief, + model_choices: { caption: "openai/gpt-4o-mini" }, + }); + const quote = buildReleasePackQuote(selected, only("artwork")); + expect(quote.execution_config.models.caption).toBe(selected.model_choices?.caption); + expect(quote.brief_fingerprint).not.toBe( + buildReleasePackQuote(brief, only("artwork")).brief_fingerprint, + ); + expect(() => + buildReleasePackQuote( + { ...brief, model_choices: { caption: "unsupported" } }, + only("artwork"), + ), + ).toThrow(/no substitution/i); + expect(() => + releasePackBriefSchema.parse({ ...brief, model_choices: { invented: "model" } }), + ).toThrow(); + }); +}); diff --git a/lib/content/packs/__tests__/packSuitabilityApproval.test.ts b/lib/content/packs/__tests__/packSuitabilityApproval.test.ts new file mode 100644 index 000000000..ac71c3283 --- /dev/null +++ b/lib/content/packs/__tests__/packSuitabilityApproval.test.ts @@ -0,0 +1,360 @@ +import type { PackQuote } from "../packTypes"; +import type { ReleasePackBrief } from "../releasePackSchema"; +import { createHash } from "node:crypto"; +import { getPackBriefFingerprint } from "@/lib/content/packs/getPackBriefFingerprint"; +import * as modelEvidence from "../getPackModelEvidence"; +import { importPackSources } from "../importPackSources"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { processPackOperation } from "../processPackOperation"; +import { buildReleasePackQuote } from "../buildReleasePackQuote"; +import { releasePackBriefSchema, releasePackDeliverablesSchema } from "../releasePackSchema"; +import { runPackCommand } from "@/lib/supabase/release_pack_jobs/runPackCommand"; +import { checkCreditsAvailable } from "@/lib/credits/checkCreditsAvailable"; +import { start } from "workflow/api"; +vi.mock("@/lib/supabase/serverClient", () => ({ default: {} })); +vi.mock("@/lib/organizations/validateOrganizationAccess", () => ({ + validateOrganizationAccess: vi.fn(), +})); +vi.mock("@/lib/artists/getArtists", () => ({ getArtists: vi.fn() })); +vi.mock("@/lib/credits/checkCreditsAvailable", () => ({ checkCreditsAvailable: vi.fn() })); +vi.mock("@/lib/supabase/release_pack_jobs/runPackCommand", () => ({ runPackCommand: vi.fn() })); +vi.mock("../importPackSources", () => ({ importPackSources: vi.fn() })); +vi.mock("../assertPackExecutionReady", () => ({ assertPackExecutionReady: vi.fn() })); +vi.mock("../readPackSnapshot", () => ({ readPackSnapshot: (snapshot: unknown) => snapshot })); +vi.mock("workflow/api", () => ({ start: vi.fn() })); +function capturedEstimateQuote() { + const payload = vi.mocked(runPackCommand).mock.calls.find(call => call[1] === "estimate")?.[3]; + if (!payload?.quote || typeof payload.quote !== "object") + throw new Error("Missing estimate quote"); + const quote = payload.quote as PackQuote; + if (!quote.model_readiness) throw new Error("Missing estimate model assessment"); + return { ...quote, model_readiness: quote.model_readiness }; +} +const id = "00000000-0000-4000-8000-000000000001"; +const brief = releasePackBriefSchema.parse({ + artist_name: "Fixture", + song_title: "Song", + occasion: "rediscovery", + audio_url: "https://example.com/song.wav", + allow_creative_defaults: true, +}); +const deliverables = releasePackDeliverablesSchema.parse({}); +const approval = { job_id: id, quote_id: id, expected_revision: 1, approved_max_usd: 10 }; +beforeEach(() => vi.clearAllMocks()); +afterEach(() => vi.restoreAllMocks()); +describe("model suitability before reservation", () => { + it("refuses unsupported explicit choices before importing sources or saving an estimate", async () => { + vi.mocked(runPackCommand).mockResolvedValue({ + job: { id, brief: { ...brief, model_choices: { caption: "unsupported" } }, deliverables }, + assets: [], + } as never); + await expect(processPackOperation("owner", "estimate", { job_id: id })).rejects.toThrow( + /no substitution/i, + ); + expect(vi.mocked(runPackCommand).mock.calls.map(call => call[1])).toEqual(["read"]); + expect(importPackSources).not.toHaveBeenCalled(); + expect(checkCreditsAvailable).not.toHaveBeenCalled(); + expect(start).not.toHaveBeenCalled(); + }); + it.each(["generate", "retry"] as const)( + "refuses %s before credit check, reservation or workflow", + async operation => { + const saved = { + job: { + id, + revision: 1, + status: "awaiting_approval", + brief, + deliverables, + quote: { + ...buildReleasePackQuote(brief, deliverables), + model_readiness: { ready: true, tasks: {} }, + id, + retry_asset_id: id, + }, + }, + assets: [{ id, kind: "artwork", ordinal: 0, status: "failed" }], + }; + vi.mocked(runPackCommand).mockResolvedValue(saved as never); + await expect( + processPackOperation("owner", operation, { + ...approval, + ...(operation === "retry" ? { asset_id: id } : {}), + }), + ).rejects.toThrow(/analysis.*commercial use restricted/i); + expect(vi.mocked(runPackCommand).mock.calls.map(call => call[1])).toEqual(["read"]); + expect(checkCreditsAvailable).not.toHaveBeenCalled(); + expect(start).not.toHaveBeenCalled(); + }, + ); + it("retains accepted-receipt resume with a legacy quote", async () => { + const saved = { + job: { id, revision: 1, status: "uncertain", brief, deliverables, quote: { id } }, + assets: [ + { + id, + status: "uncertain", + provider_state: { + image_submission_0: { + state: "accepted", + request_id: "retained", + model: "original-route", + }, + }, + }, + ], + }; + vi.mocked(runPackCommand).mockResolvedValue(saved as never); + await processPackOperation("owner", "retry", { ...approval, asset_id: id }); + expect(vi.mocked(runPackCommand).mock.calls.map(call => call[1])).toEqual(["read", "resume"]); + expect(checkCreditsAvailable).not.toHaveBeenCalled(); + expect(start).not.toHaveBeenCalled(); + }); + it.each(["generate", "retry"] as const)( + "preserves legacy %s approval with completed analysis and unknown remaining evidence", + async operation => { + const { model_readiness: _assessment, ...legacyQuote } = buildReleasePackQuote( + brief, + deliverables, + ); + const saved = { + job: { + id, + revision: 1, + status: "awaiting_approval", + brief, + deliverables, + production_context: { + state: "complete", + audio: { storage_key: "owner/release-packs/song.wav" }, + references: [], + summary: "Sparse piano and percussion", + visual_direction: "Cream paper and ink", + proposed_start_seconds: 0, + selected_start_seconds: 0, + duration_seconds: 30, + }, + quote: { + ...legacyQuote, + fingerprint: "legacy-approved-fingerprint", + id, + retry_asset_id: id, + }, + }, + assets: [{ id, kind: "artwork", ordinal: 0, status: "failed" }], + }; + vi.mocked(runPackCommand).mockResolvedValue(saved as never); + vi.mocked(checkCreditsAvailable).mockResolvedValue({ kind: "available" } as never); + await processPackOperation("owner", operation, { + ...approval, + ...(operation === "retry" ? { asset_id: id } : {}), + }); + expect(vi.mocked(runPackCommand).mock.calls.map(call => call[1])).toEqual([ + "read", + "approve", + ]); + expect(saved.job.quote.fingerprint).toBe("legacy-approved-fingerprint"); + expect(start).not.toHaveBeenCalled(); + }, + ); + it("assesses only a targeted retry and omits retained complete analysis", async () => { + const saved = { + job: { + id, + revision: 1, + owner_id: "owner", + brief, + deliverables, + production_context: { + state: "complete", + audio: { storage_key: "owner/release-packs/song.wav" }, + references: [], + summary: "Sparse piano and percussion", + visual_direction: "Cream paper and ink", + proposed_start_seconds: 0, + selected_start_seconds: 0, + duration_seconds: 30, + }, + quote: { + sources: { audio: { storage_key: "owner/release-packs/song.wav" }, references: [] }, + }, + }, + assets: [{ id, kind: "artwork", ordinal: 0, status: "failed" }], + }; + vi.mocked(runPackCommand).mockResolvedValue(saved as never); + await processPackOperation("owner", "estimate", { job_id: id, asset_id: id }); + const quote = capturedEstimateQuote(); + expect(Object.keys(quote.model_readiness.tasks)).toEqual([ + "image", + "caption", + "still_inspection", + ]); + expect(importPackSources).not.toHaveBeenCalled(); + }); + + it.each(["changed", "expired"])( + "replays funded running approval after %s evidence without new admission or effects", + async change => { + const { model_readiness: _assessment, ...legacyQuote } = buildReleasePackQuote( + brief, + deliverables, + ); + const saved = { + job: { + id, + revision: 1, + status: "running", + held_credits: 100, + brief, + deliverables, + quote: { ...legacyQuote, id }, + }, + assets: [], + dispatch: false, + }; + vi.mocked(runPackCommand).mockResolvedValue(saved as never); + const changed = modelEvidence.getPackModelEvidence(); + if (change === "changed") changed.version = "after-approval"; + else changed.tasks.analysis.valid_until = "2000-01-01T00:00:00Z"; + vi.spyOn(modelEvidence, "getPackModelEvidence").mockReturnValue(changed); + await processPackOperation("owner", "generate", approval); + expect(vi.mocked(runPackCommand).mock.calls.map(call => call[1])).toEqual([ + "read", + "approve", + ]); + expect(checkCreditsAvailable).not.toHaveBeenCalled(); + expect(start).not.toHaveBeenCalled(); + }, + ); +}); + +// A subset retry preserves the saved full-pack brief; it does not respecify model intent. +const mixed = releasePackDeliverablesSchema.parse({ + videos: 1, + artwork: 1, + quote_cards: 0, + carousels: 0, + visualizers: 0, +}); +const motion = "minimax/h3-max/image-to-video"; +const fullBrief = releasePackBriefSchema.parse({ ...brief, model_choices: { motion } }); +const complete = { + state: "complete", + audio: { storage_key: "owner/release-packs/song.wav" }, + references: [], + summary: "Sparse piano and percussion", + visual_direction: "Cream paper and ink", + proposed_start_seconds: 0, + selected_start_seconds: 0, + duration_seconds: 30, +}; +function saved(kind: string) { + const choices: NonNullable = { motion }; + return { + job: { + id, + owner_id: "owner", + revision: 1, + brief: { ...structuredClone(fullBrief), model_choices: choices }, + deliverables: mixed, + production_context: complete, + quote: { + sources: { audio: { storage_key: "owner/release-packs/song.wav" }, references: [] }, + }, + }, + assets: [{ id, kind, ordinal: 0, status: "failed" }], + }; +} +it.each(["artwork", "video"])( + "preserves full-pack intent while quoting only the %s retry", + async kind => { + const snapshot = saved(kind); + const before = structuredClone(snapshot.job.brief); + vi.mocked(runPackCommand).mockResolvedValue(snapshot as never); + await processPackOperation("owner", "estimate", { job_id: id, asset_id: id }); + const quote = capturedEstimateQuote(); + expect(snapshot.job.brief).toEqual(before); + expect(snapshot.job.brief.model_choices.motion).toBe(motion); + expect(quote.brief_fingerprint).toBe(getPackBriefFingerprint(before)); + expect(quote.assets).toHaveLength(1); + expect(quote.assets[0].kind).toBe(kind); + expect(quote.retry_asset_id).toBe(id); + expect(quote.execution_config.models.motion).toBe(motion); + expect(quote.line_items.find(item => item.category === "motion")?.units).toBe( + kind === "video" ? 8 : 0, + ); + if (kind === "video") expect(quote.model_readiness.tasks.motion?.model).toBe(motion); + else { + expect(Object.keys(quote.model_readiness.tasks)).toEqual([ + "image", + "caption", + "still_inspection", + ]); + expect(quote.line_items.find(item => item.category === "motion")?.usd).toBe(0); + } + expect(quote.fingerprint).toBe( + createHash("sha256") + .update( + JSON.stringify({ + brief: before, + deliverables: mixed, + sources: snapshot.job.quote.sources, + execution_config_digest: quote.execution_config_digest, + model_readiness_digest: quote.model_readiness.digest, + }), + ) + .digest("hex"), + ); + expect(vi.mocked(runPackCommand).mock.calls.map(call => call[1])).toEqual(["read", "estimate"]); + expect(importPackSources).not.toHaveBeenCalled(); + expect(checkCreditsAvailable).not.toHaveBeenCalled(); + expect(start).not.toHaveBeenCalled(); + }, +); +it("does not silently discard an unsupported saved motion choice for an artwork retry", async () => { + const snapshot = saved("artwork"); + snapshot.job.brief.model_choices.motion = "unsupported-motion"; + vi.mocked(runPackCommand).mockResolvedValue(snapshot as never); + await expect( + processPackOperation("owner", "estimate", { job_id: id, asset_id: id }), + ).rejects.toThrow(/Unsupported motion.*no substitution/i); + expect(vi.mocked(runPackCommand).mock.calls.map(call => call[1])).toEqual(["read"]); +}); +it("does not let the retry locator replace full-pack model choices", async () => { + await expect( + processPackOperation("owner", "estimate", { + job_id: id, + asset_id: id, + model_choices: { motion }, + }), + ).rejects.toThrow(); + expect(runPackCommand).not.toHaveBeenCalled(); +}); +it("still rejects a newly specified motion choice for an artwork-only full pack", () => { + const artworkOnly = releasePackDeliverablesSchema.parse({ + videos: 0, + artwork: 1, + quote_cards: 0, + carousels: 0, + visualizers: 0, + }); + expect(() => buildReleasePackQuote(fullBrief, artworkOnly)).toThrow(/motion.*does not apply/i); +}); + +it("retains original analysis choice while reusing completed analysis", async () => { + const snapshot = saved("artwork"); + snapshot.job.brief.model_choices = { + ...snapshot.job.brief.model_choices, + analysis: "nvidia/music-flamingo-2601-hf", + }; + const originalBrief = structuredClone(snapshot.job.brief); + vi.mocked(runPackCommand).mockResolvedValue(snapshot as never); + await processPackOperation("owner", "estimate", { job_id: id, asset_id: id }); + const quote = capturedEstimateQuote(); + expect(snapshot.job.brief).toEqual(originalBrief); + expect(quote.brief_fingerprint).toBe(getPackBriefFingerprint(originalBrief)); + expect(quote.model_readiness.tasks.analysis).toBeUndefined(); + expect(vi.mocked(runPackCommand).mock.calls.map(call => call[1])).toEqual(["read", "estimate"]); + expect(importPackSources).not.toHaveBeenCalled(); + expect(start).not.toHaveBeenCalled(); +}); diff --git a/lib/content/packs/__tests__/packSuitabilityRecovery.test.ts b/lib/content/packs/__tests__/packSuitabilityRecovery.test.ts new file mode 100644 index 000000000..55f01b0db --- /dev/null +++ b/lib/content/packs/__tests__/packSuitabilityRecovery.test.ts @@ -0,0 +1,147 @@ +import { buildReleasePackQuote } from "../buildReleasePackQuote"; +import { createPackRecoverySnapshot } from "./createPackRecoverySnapshot"; +import { createPackRecoveryCommandMock } from "./createPackRecoveryCommandMock"; +import { beforeEach, afterEach, expect, it, vi } from "vitest"; +import { releasePackWorkflow } from "@/app/workflows/packs/releasePackWorkflow"; +import { runPackCommand } from "@/lib/supabase/release_pack_jobs/runPackCommand"; +import { callFlamingoGenerate } from "@/lib/flamingo/callFlamingoGenerate"; +import fal from "@/lib/fal/server"; +import { generateText } from "ai"; +import { composePackMedia } from "../composePackMedia"; +import { inspectPackMedia } from "../inspectPackMedia"; +import * as evidence from "../getPackModelEvidence"; +vi.mock("workflow", () => ({ getWorkflowMetadata: () => ({ workflowRunId: "saved-workflow" }) })); +vi.mock("@/lib/supabase/release_pack_jobs/runPackCommand", () => ({ runPackCommand: vi.fn() })); +vi.mock("@/lib/supabase/storage/createSignedFileUrlByKey", () => ({ + createSignedFileUrlByKey: vi.fn(async ({ key }) => `https://example.invalid/${key}`), +})); +vi.mock("@/lib/flamingo/callFlamingoGenerate", () => ({ callFlamingoGenerate: vi.fn() })); +vi.mock("@/lib/fal/server", () => ({ + default: { queue: { submit: vi.fn(), status: vi.fn(), result: vi.fn() } }, +})); +vi.mock("ai", () => ({ generateText: vi.fn() })); +vi.mock("../composePackMedia", () => ({ composePackMedia: vi.fn() })); +vi.mock("../inspectPackMedia", () => ({ inspectPackMedia: vi.fn() })); +beforeEach(() => vi.clearAllMocks()); +afterEach(() => vi.restoreAllMocks()); +it("runs actual workflow preparation, accepted polling, local composition and settlement after unrelated evidence changes", async () => { + const saved = createPackRecoverySnapshot(); + const asset = saved.assets[0]; + const originalConsumed = saved.job.consumed_credits; + const originalHeld = saved.job.held_credits; + const originalReceipt = structuredClone(asset.provider_state.image_submission_0); + const changed = evidence.getPackModelEvidence(); + changed.version = "changed-after-approval"; + changed.tasks.motion.commercial_basis = "restricted"; + changed.tasks.motion.valid_until = "2000-01-01T00:00:00Z"; + // Even a new restriction on this provider cannot resubmit or erase an accepted receipt. + changed.tasks.image.commercial_basis = "restricted"; + vi.spyOn(evidence, "getPackModelEvidence").mockReturnValue(changed); + vi.mocked(runPackCommand).mockImplementation( + createPackRecoveryCommandMock(saved, { accountId: "owner" }), + ); + vi.mocked(fal.queue.status).mockResolvedValue({ status: "COMPLETED" } as never); + vi.mocked(fal.queue.result).mockResolvedValue({ + data: { images: [{ url: "https://example.invalid/retained.png" }] }, + } as never); + vi.mocked(composePackMedia).mockResolvedValue({ + files: [{ storage_key: "owner/release-packs/final.png", content_type: "image/png", bytes: 10 }], + probe: {}, + }); + vi.mocked(inspectPackMedia).mockResolvedValue({ passed: true, reason: "Synthetic QA" }); + expect(await releasePackWorkflow({ accountId: "owner" }, "job", "asset")).toEqual({ + job_id: "job", + }); + expect(fal.queue.status).toHaveBeenCalledWith("original-image-route", { + requestId: "retained", + logs: false, + }); + expect(fal.queue.result).toHaveBeenCalledWith("original-image-route", { requestId: "retained" }); + expect(fal.queue.submit).not.toHaveBeenCalled(); + expect(callFlamingoGenerate).not.toHaveBeenCalled(); + expect(generateText).not.toHaveBeenCalled(); + expect(composePackMedia).toHaveBeenCalledOnce(); + expect(inspectPackMedia).toHaveBeenCalledOnce(); + expect(asset.caption).toBe("Retained caption"); + expect(saved.job.quote?.fingerprint).toBe("original-approved-fingerprint"); + expect(saved.job.consumed_credits).toBe(originalConsumed); + expect(saved.job.held_credits).toBe(originalHeld); + expect(asset.provider_state.image_submission_0).toMatchObject({ + ...(originalReceipt as Record), + state: "complete", + }); + const commands = vi.mocked(runPackCommand).mock.calls.map(call => call[1]); + expect(commands.slice(0, 3)).toEqual(["run", "read", "read"]); + expect(commands.slice(-2)).toEqual(["finish_asset", "settle"]); + expect(commands).not.toContain("approve"); + expect(commands).not.toContain("claim"); + expect(commands).not.toContain("context"); +}); + +it.each([ + ["actor", { accountId: "wrong-owner" }, "job", "asset"], + ["organization", { accountId: "owner", organizationId: "other-org" }, "job", "asset"], + ["job", { accountId: "owner" }, "wrong-job", "asset"], + ["asset", { accountId: "owner" }, "job", "wrong-asset"], +] as const)( + "rejects wrong %s targets without mutating persistence", + async (_label, actor, job, assetId) => { + const saved = createPackRecoverySnapshot(); + const before = structuredClone(saved); + const command = createPackRecoveryCommandMock(saved, { accountId: "owner" }); + for (const action of ["phase", "finish_asset"]) + await expect( + command(actor, action, job, { + asset_id: assetId, + phase: "caption", + value: "wrong", + status: "completed", + }), + ).rejects.toThrow(/Unexpected/); + expect(saved).toEqual(before); + }, +); +it("mutates and returns the requested asset rather than a hard-coded recovery asset", async () => { + const saved = createPackRecoverySnapshot(); + saved.job.deliverables = { ...saved.job.deliverables, artwork: 2 }; + if (!saved.job.quote) throw new Error("Fixture requires a quote"); + saved.job.quote = { + ...saved.job.quote, + ...buildReleasePackQuote(saved.job.brief, saved.job.deliverables), + }; + saved.job.consumed_credits = saved.job.quote.credits; + saved.assets.push({ ...structuredClone(saved.assets[0]), id: "other-asset", ordinal: 1 }); + const command = createPackRecoveryCommandMock(saved, { accountId: "owner" }); + const returned = await command({ accountId: "owner" }, "phase", "job", { + asset_id: "other-asset", + phase: "caption", + value: "Other caption", + }); + expect(saved.assets[0].provider_state.caption).toBe("Retained caption"); + expect(saved.assets[1].provider_state.caption).toBe("Other caption"); + expect(returned).toEqual(saved); + expect(returned).not.toBe(saved); + await expect(command({ accountId: "owner" }, "settle", "job")).rejects.toThrow(/original asset/); +}); + +it("starts recovery with an accepted charge inside the original quote and asset ceiling", () => { + const snapshot = createPackRecoverySnapshot(); + const quote = snapshot.job.quote; + if (!quote) throw new Error("Fixture requires an approved quote"); + const asset = snapshot.assets[0]; + const plan = quote.assets[0]; + expect(asset).toMatchObject({ + credits: plan.credits, + maximum_usd: plan.maximum_usd, + billed_credits: plan.credits, + status: "running", + }); + expect(asset.provider_state.image_submission_0).toMatchObject({ + state: "accepted", + charge_credits: plan.credits, + }); + expect(snapshot.job.consumed_credits).toBe(plan.credits); + expect(snapshot.job.held_credits).toBe(quote.credits - snapshot.job.consumed_credits); + expect(snapshot.job.held_credits).toBe(0); + expect(snapshot.job.consumed_credits).toBeLessThanOrEqual(quote.credits); +}); diff --git a/lib/content/packs/assertPackModelRestrictions.ts b/lib/content/packs/assertPackModelRestrictions.ts new file mode 100644 index 000000000..c9d385a5b --- /dev/null +++ b/lib/content/packs/assertPackModelRestrictions.ts @@ -0,0 +1,11 @@ +import { getPackModelReadiness } from "./getPackModelReadiness"; +import type { PackModelTask } from "./getPackModelEvidence"; +/** Existing approvals do not waive recorded restrictions on a new provider effect. */ +export function assertPackModelRestrictions(tasks: PackModelTask[]) { + const report = getPackModelReadiness(tasks); + const blocked = Object.entries(report.tasks).filter(([, entry]) => entry.restriction); + if (blocked.length) + throw new Error( + `Pack provider restriction: ${blocked.map(([task, entry]) => `${task} (${entry.model}): ${entry.restriction}`).join("; ")}. No new provider work authorized; no substitution was made.`, + ); +} diff --git a/lib/content/packs/buildReleasePackQuote.ts b/lib/content/packs/buildReleasePackQuote.ts index e9a17f33b..1eee1f52b 100644 --- a/lib/content/packs/buildReleasePackQuote.ts +++ b/lib/content/packs/buildReleasePackQuote.ts @@ -1,3 +1,4 @@ +import { getPackQuoteModelReadiness } from "./getPackQuoteModelReadiness"; import { packCostAccounting } from "@/lib/const"; import { getPackExecutionConfiguration } from "./getPackExecutionConfiguration"; import { getPackConfigurationDigest } from "./getPackConfigurationDigest"; @@ -43,16 +44,25 @@ export function buildReleasePackQuote( ); assets.push({ kind, ordinal, files, maximum_usd: usd, credits: usdToCredits(usd) }); } + const model_readiness = getPackQuoteModelReadiness(brief, assets, execution_config.models); const stills = assets.reduce((sum, a) => sum + a.files, 0); const maximum_usd = Number(assets.reduce((sum, a) => sum + a.maximum_usd, 0).toFixed(6)); return { + model_readiness, cost_accounting: { ...packCostAccounting }, pricing_version: execution_config.pricing_version, execution_config, execution_config_digest, brief_fingerprint: getPackBriefFingerprint(brief), fingerprint: createHash("sha256") - .update(JSON.stringify({ brief, deliverables, execution_config_digest })) + .update( + JSON.stringify({ + brief, + deliverables, + execution_config_digest, + model_readiness_digest: model_readiness.digest, + }), + ) .digest("hex"), creative_piece_count: assets.length, generated_file_count: stills, diff --git a/lib/content/packs/createPackProductionServices.ts b/lib/content/packs/createPackProductionServices.ts index ad0113928..3267a0a41 100644 --- a/lib/content/packs/createPackProductionServices.ts +++ b/lib/content/packs/createPackProductionServices.ts @@ -1,3 +1,4 @@ +import { assertPackModelRestrictions } from "./assertPackModelRestrictions"; import { packExecutionSettings, PACK_CAPTION_MODEL } from "@/lib/const"; import { assertPackQuoteConfiguration } from "./assertPackQuoteConfiguration"; import { generateText } from "ai"; @@ -35,6 +36,10 @@ export function createPackProductionServices( ...packExecutionSettings.image, image_urls: job.brief.reference_image_urls, }); + if (!state[`image_submission_${index}`]) + assertPackModelRestrictions([ + job.brief.reference_image_urls.length ? "image_edit" : "image", + ]); const data = await runPackFalRequest({ model: built.model, input: built.input, @@ -51,6 +56,7 @@ export function createPackProductionServices( }, generateMotion: async (prompt, image) => { if (!state.motion_submission) assertPackQuoteConfiguration(job.quote); + if (!state.motion_submission) assertPackModelRestrictions(["motion"]); const input = buildVideoInput({ prompt, image_url: image, @@ -76,6 +82,7 @@ export function createPackProductionServices( }, caption: async prompt => { assertPackQuoteConfiguration(job.quote); + assertPackModelRestrictions(["caption"]); const result = await generateText({ model: PACK_CAPTION_MODEL, ...packExecutionSettings.caption, @@ -94,6 +101,11 @@ export function createPackProductionServices( }, inspect: input => { assertPackQuoteConfiguration(job.quote); + assertPackModelRestrictions( + input.files.map(file => + file.content_type === "video/mp4" ? "video_inspection" : "still_inspection", + ), + ); return inspectPackMedia(input); }, finish: result => diff --git a/lib/content/packs/getPackModelEvidence.ts b/lib/content/packs/getPackModelEvidence.ts new file mode 100644 index 000000000..9c5cf9448 --- /dev/null +++ b/lib/content/packs/getPackModelEvidence.ts @@ -0,0 +1,63 @@ +export type PackModelTask = + | "image" + | "image_edit" + | "motion" + | "caption" + | "still_inspection" + | "video_inspection" + | "analysis"; +type Verification = "verified" | "unknown" | "restricted"; +export type PackModelEvidence = { + model: string; + endpoint?: string; + capability: Verification; + commercial_basis: Verification; + account_availability: Verification; + lifecycle: "supported" | "deprecated" | "retired" | "unknown"; + valid_until: string; + evidence: string[]; +}; +/** Reviewed public facts only. Unknown account access and commercial grants are never inferred. */ +export function getPackModelEvidence(): { + version: string; + reviewed_at: string; + tasks: Record; +} { + const unknown = (model: string): PackModelEvidence => ({ + model, + capability: "unknown", + commercial_basis: "unknown", + account_availability: "unknown", + lifecycle: "unknown", + valid_until: "2026-12-18T00:00:00.000Z", + evidence: [], + }); + const text = (): PackModelEvidence => ({ + ...unknown("openai/gpt-4o-mini"), + capability: "verified", + lifecycle: "deprecated", + evidence: [ + "https://developers.openai.com/api/docs/models/gpt-4o-mini", + "https://developers.openai.com/api/docs/deprecations", + ], + }); + return { + version: "2026-11-18.1", + reviewed_at: "2026-11-18", + tasks: { + image: unknown("meta/muse-image/text-to-image"), + image_edit: unknown("meta/muse-image/edit"), + motion: unknown("minimax/h3-max/image-to-video"), + caption: text(), + still_inspection: text(), + video_inspection: unknown("pegasus1.5"), + analysis: { + ...unknown("nvidia/music-flamingo-2601-hf"), + endpoint: "https://shared-78369--music-flamingo-musicflamingo-generate.modal.run", + capability: "verified", + commercial_basis: "restricted", + evidence: ["https://huggingface.co/nvidia/music-flamingo-2601-hf"], + }, + }, + }; +} diff --git a/lib/content/packs/getPackModelReadiness.ts b/lib/content/packs/getPackModelReadiness.ts new file mode 100644 index 000000000..f6811aeb8 --- /dev/null +++ b/lib/content/packs/getPackModelReadiness.ts @@ -0,0 +1,55 @@ +import { getPackModelEvidence } from "./getPackModelEvidence"; +import { getPackExecutionConfiguration } from "./getPackExecutionConfiguration"; +import { getPackConfigurationDigest } from "./getPackConfigurationDigest"; +import type { PackModelTask, PackModelEvidence } from "./getPackModelEvidence"; +/** Assessment of requested tasks, separate from existing execution identity and authorization. */ +export function getPackModelReadiness(required: PackModelTask[]) { + const registry = getPackModelEvidence(); + const config = getPackExecutionConfiguration(); + const tasks: Partial< + Record< + PackModelTask, + { + model: string; + evidence: PackModelEvidence | null; + issues: string[]; + restriction: string | null; + } + > + > = {}; + for (const task of required) { + const model = config.models[task]; + const entry = registry.tasks[task]; + const matches = + !!entry && + entry.model === model && + (task !== "analysis" || entry.endpoint === config.models.analysis_endpoint); + const issues: string[] = []; + let restriction: string | null = null; + if (!matches) issues.push("missing evidence for the configured route"); + else { + if (entry.capability !== "verified") issues.push(`task capability ${entry.capability}`); + if (entry.commercial_basis !== "verified") + issues.push(`commercial basis ${entry.commercial_basis}`); + if (entry.account_availability !== "verified") + issues.push(`account availability ${entry.account_availability}`); + if (entry.lifecycle !== "supported") issues.push(`lifecycle ${entry.lifecycle}`); + if (!entry.evidence.length) issues.push("missing evidence sources"); + if (!(Date.parse(entry.valid_until) > Date.now())) issues.push("evidence expired or undated"); + // Expiry does not turn a recorded restriction into permission. + if (entry.commercial_basis === "restricted") restriction = "commercial use restricted"; + if (entry.lifecycle === "retired") restriction = "model retired"; + } + tasks[task] = { model, evidence: entry ?? null, issues, restriction }; + } + const report = { + version: registry.version, + reviewed_at: registry.reviewed_at, + assessment_only: true as const, + ready: Object.values(tasks).every(task => task.issues.length === 0), + execution_policy: + "Recorded commercial restrictions and retirement block matching new provider effects; unknown evidence and deprecation alone do not change existing execution authorization.", + tasks, + }; + return { ...report, digest: getPackConfigurationDigest(report) }; +} diff --git a/lib/content/packs/getPackPlannedModelTasks.ts b/lib/content/packs/getPackPlannedModelTasks.ts new file mode 100644 index 000000000..7a4990a9a --- /dev/null +++ b/lib/content/packs/getPackPlannedModelTasks.ts @@ -0,0 +1,23 @@ +import { resolvePackCreativeContext } from "./resolvePackCreativeContext"; +import type { ReleasePackBrief } from "./releasePackSchema"; +import type { PackAssetPlan } from "./buildReleasePackQuote"; +import type { PackModelTask } from "./getPackModelEvidence"; +/** Provider tasks for this plan. Local composition never requires a model grant. */ +export function getPackPlannedModelTasks( + brief: ReleasePackBrief, + assets: Pick[], + needsAudioAnalysis = true, +): PackModelTask[] { + if (!assets.length) return []; + const tasks: PackModelTask[] = needsAudioAnalysis ? ["analysis"] : []; + tasks.push( + resolvePackCreativeContext(brief).reference_image_urls.length ? "image_edit" : "image", + "caption", + ); + if (assets.some(asset => asset.kind === "video")) tasks.push("motion"); + if (assets.some(asset => asset.kind !== "video" && asset.kind !== "visualizer")) + tasks.push("still_inspection"); + if (assets.some(asset => asset.kind === "video" || asset.kind === "visualizer")) + tasks.push("video_inspection"); + return tasks; +} diff --git a/lib/content/packs/getPackQuoteModelReadiness.ts b/lib/content/packs/getPackQuoteModelReadiness.ts new file mode 100644 index 000000000..f64bccede --- /dev/null +++ b/lib/content/packs/getPackQuoteModelReadiness.ts @@ -0,0 +1,27 @@ +import { getPackModelReadiness } from "./getPackModelReadiness"; +import { getPackPlannedModelTasks } from "./getPackPlannedModelTasks"; +import type { ReleasePackBrief } from "./releasePackSchema"; +import type { PackAssetPlan } from "./buildReleasePackQuote"; +import type { getPackExecutionConfiguration } from "./getPackExecutionConfiguration"; +/** Validate saved full-pack choices before any retry narrows the execution assessment. */ +export function getPackQuoteModelReadiness( + brief: ReleasePackBrief, + assets: PackAssetPlan[], + models: ReturnType["models"], +) { + for (const [task, requested] of Object.entries(brief.model_choices ?? {})) { + const selected = models[task as keyof NonNullable]; + if (requested !== selected) + throw new Error( + `Unsupported ${task} model choice ${requested}; configured route is ${selected}. No substitution was made.`, + ); + } + const plannedTasks = getPackPlannedModelTasks(brief, assets); + for (const task of Object.keys(brief.model_choices ?? {})) { + if (!plannedTasks.includes(task as (typeof plannedTasks)[number])) + throw new Error( + `Model choice ${task} does not apply to this pack plan; no substitution was made.`, + ); + } + return getPackModelReadiness(plannedTasks); +} diff --git a/lib/content/packs/packTypes.ts b/lib/content/packs/packTypes.ts index 19da137a6..2bb092681 100644 --- a/lib/content/packs/packTypes.ts +++ b/lib/content/packs/packTypes.ts @@ -1,6 +1,10 @@ import type { ReleasePackBrief, ReleasePackDeliverables } from "./releasePackSchema"; import type { buildReleasePackQuote, PackAssetKind } from "./buildReleasePackQuote"; -export type PackQuote = Omit, "cost_accounting"> & { +export type PackQuote = Omit< + ReturnType, + "cost_accounting" | "model_readiness" +> & { + model_readiness?: ReturnType["model_readiness"]; /** Older saved quotes predate these explanatory labels. */ cost_accounting?: ReturnType["cost_accounting"]; id: string; diff --git a/lib/content/packs/preparePackProduction.ts b/lib/content/packs/preparePackProduction.ts index 87ad95ba2..fe3393cda 100644 --- a/lib/content/packs/preparePackProduction.ts +++ b/lib/content/packs/preparePackProduction.ts @@ -1,3 +1,4 @@ +import { assertPackModelRestrictions } from "./assertPackModelRestrictions"; import { packProductionContextSchema as contextSchema } from "./packProductionContextSchema"; import { parsePackAudioAnalysis } from "./parsePackAudioAnalysis"; import { assertPackQuoteConfiguration } from "./assertPackQuoteConfiguration"; @@ -27,6 +28,7 @@ export async function preparePackProduction(actor: PackActor, job: PackJob): Pro throw new Error( "Audio analysis outcome is uncertain; retained context needs reconciliation before production", ); + assertPackModelRestrictions(["analysis"]); const sources = job.quote?.sources; if (!sources) throw new Error("The approved quote has no immutable source snapshot; get a new estimate"); @@ -77,6 +79,7 @@ export async function preparePackProduction(actor: PackActor, job: PackJob): Pro value: { state: "analyzing", audio, references, duration_seconds: duration }, }); assertPackQuoteConfiguration(job.quote); + assertPackModelRestrictions(["analysis"]); const analysis = await callFlamingoGenerate( flamingoGenerateBodySchema.parse({ audio_url: audio.url, diff --git a/lib/content/packs/processPackOperation.ts b/lib/content/packs/processPackOperation.ts index 75e62531d..326065163 100644 --- a/lib/content/packs/processPackOperation.ts +++ b/lib/content/packs/processPackOperation.ts @@ -1,3 +1,7 @@ +import { assertPackModelRestrictions } from "./assertPackModelRestrictions"; +import { getPackModelReadiness } from "./getPackModelReadiness"; +import { getPackPlannedModelTasks } from "./getPackPlannedModelTasks"; +import { packProductionContextSchema } from "./packProductionContextSchema"; import { assertPackQuoteConfiguration } from "./assertPackQuoteConfiguration"; import { importPackSources } from "./importPackSources"; import { randomUUID, createHash } from "node:crypto"; @@ -98,12 +102,20 @@ export async function processPackOperation( const plan = selected ? full.assets.filter(a => a.kind === selected.kind && a.ordinal === selected.ordinal) : full.assets; + const model_readiness = getPackModelReadiness( + getPackPlannedModelTasks( + saved.job.brief, + plan, + !packProductionContextSchema.safeParse(saved.job.production_context).success, + ), + ); const sources = saved.job.quote?.sources ?? (await importPackSources(saved.job.owner_id, resolvePackCreativeContext(saved.job.brief))); const quote: PackQuote = { sources, ...full, + model_readiness, fingerprint: createHash("sha256") .update( JSON.stringify({ @@ -111,6 +123,7 @@ export async function processPackOperation( deliverables: saved.job.deliverables, sources, execution_config_digest: full.execution_config_digest, + model_readiness_digest: model_readiness.digest, }), ) .digest("hex"), @@ -187,10 +200,27 @@ export async function processPackOperation( assertPackQuoteConfiguration(saved.job.quote); if (saved.job.quote.retry_asset_id !== input.asset_id) throw new Error("Request and approve a fresh estimate for this failed asset first"); + assertPackModelRestrictions( + getPackPlannedModelTasks( + saved.job.brief, + [selected], + !packProductionContextSchema.safeParse(saved.job.production_context).success, + ), + ); funded = await runPackCommand(actor, "approve", saved.job.id, input); } } else { assertPackQuoteConfiguration(saved.job.quote); + // A funded running replay asks the durable command for its existing outcome. + // New worker effects remain subject to their own task-specific restriction checks. + if (saved.job.status !== "running") + assertPackModelRestrictions( + getPackPlannedModelTasks( + saved.job.brief, + saved.job.quote.assets, + !packProductionContextSchema.safeParse(saved.job.production_context).success, + ), + ); const credits = saved.job.status === "running" ? { kind: "available" as const } diff --git a/lib/content/packs/readPackSnapshot.ts b/lib/content/packs/readPackSnapshot.ts index f02fd9c21..6f90cc3b1 100644 --- a/lib/content/packs/readPackSnapshot.ts +++ b/lib/content/packs/readPackSnapshot.ts @@ -1,3 +1,6 @@ +import { getPackModelReadiness } from "./getPackModelReadiness"; +import { getPackPlannedModelTasks } from "./getPackPlannedModelTasks"; +import { packProductionContextSchema } from "./packProductionContextSchema"; import { packCostAccounting } from "@/lib/const"; import { getPackProgress } from "./getPackProgress"; import { isPackAssetUsable } from "./isPackAssetUsable"; @@ -33,6 +36,19 @@ export async function readPackSnapshot(snapshot: PackSnapshot) { }); } const readiness = getPackBriefReadiness(snapshot.job.brief); + const modelReadiness = + job.status === "awaiting_approval" && Array.isArray(job.quote?.assets) + ? getPackModelReadiness( + getPackPlannedModelTasks( + snapshot.job.brief, + job.quote!.assets, + !packProductionContextSchema.safeParse(snapshot.job.production_context).success, + ), + ) + : undefined; + const restrictions = Object.entries(modelReadiness?.tasks ?? {}).filter( + ([, entry]) => entry.restriction, + ); return { job: { ...job, @@ -40,11 +56,13 @@ export async function readPackSnapshot(snapshot: PackSnapshot) { quote: job.quote ? { ...job.quote, cost_accounting: { ...packCostAccounting } } : null, }, assets, - readiness, + readiness: { ...readiness, model_readiness: modelReadiness }, progress: getPackProgress(snapshot), next_action: readiness.ready ? snapshot.job.status === "awaiting_approval" - ? "Review this complete quote and approve its budget before generation." + ? restrictions.length + ? `New provider work is blocked: ${restrictions.map(([task, entry]) => `${task}: ${entry.restriction}`).join("; ")}. Review the provider restriction before requesting generation; existing outputs are retained.` + : "Review this complete quote and approve its budget before generation." : snapshot.job.status === "running" ? "The approved pack is running. Poll this saved job; do not submit another pack." : snapshot.job.status === "completed" diff --git a/lib/content/packs/releasePackSchema.ts b/lib/content/packs/releasePackSchema.ts index 87d00388d..41bb08b68 100644 --- a/lib/content/packs/releasePackSchema.ts +++ b/lib/content/packs/releasePackSchema.ts @@ -105,6 +105,18 @@ const creativeContextSchema = z export const releasePackBriefSchema = z .strictObject({ + model_choices: z + .object({ + image: z.string().min(1).max(200).optional(), + image_edit: z.string().min(1).max(200).optional(), + motion: z.string().min(1).max(200).optional(), + caption: z.string().min(1).max(200).optional(), + still_inspection: z.string().min(1).max(200).optional(), + video_inspection: z.string().min(1).max(200).optional(), + analysis: z.string().min(1).max(200).optional(), + }) + .strict() + .optional(), artist_name: text.optional(), artist_account_id: z.uuid().optional(), song_title: text.optional(), diff --git a/lib/contracts/fixtures/published-docs.json b/lib/contracts/fixtures/published-docs.json index b429e82fb..0710b115e 100644 --- a/lib/contracts/fixtures/published-docs.json +++ b/lib/contracts/fixtures/published-docs.json @@ -1231,7 +1231,7 @@ ], "sourceHashes": { "content/docs/source/api-reference/openapi/accounts.json": "1a5a04f9ec358a15b8339b7029c9fed5fe69c6f042c8b03b6b4f03f3aafdeeed", - "content/docs/source/api-reference/openapi/content.json": "877afed1e0fb5c95afab8c5e294bae38fabdf48f50f226e0089b9ff50438870d", + "content/docs/source/api-reference/openapi/content.json": "61b972a3cbd4821e349a4fdbc678f26839c866ae692982fe357b3d54dc8ea288", "content/docs/source/api-reference/openapi/research.json": "f301a714e994e99b1dbcfc63ee18824537cf42813d8df3906ac7c2436a481ae0", "content/docs/source/api-reference/openapi/releases.json": "0e1239e256b2c1f8f189d81e013f942518619a24214b7c2b7f99baad15de7d3c", "content/docs/source/api-reference/openapi/social.json": "5bd167a78cf6a17c46724486e2921882be20969ea32f3c0297f5b7e7ba9a30aa", diff --git a/lib/emails/inbound/__tests__/generateEmailResponse.test.ts b/lib/emails/inbound/__tests__/generateEmailResponse.test.ts index faa3b8e96..a287ed48b 100644 --- a/lib/emails/inbound/__tests__/generateEmailResponse.test.ts +++ b/lib/emails/inbound/__tests__/generateEmailResponse.test.ts @@ -3,14 +3,16 @@ import { generateEmailResponse } from "../generateEmailResponse"; import type { ChatRequestBody } from "@/lib/chat/validateChatRequest"; import getGeneralAgent from "@/lib/agents/generalAgent/getGeneralAgent"; -import { getEmailRoomMessages } from "@/lib/emails/inbound/getEmailRoomMessages"; +import selectMemories from "@/lib/supabase/memories/selectMemories"; +import { selectRoomWithArtist } from "@/lib/supabase/rooms/selectRoomWithArtist"; +import { FatalError } from "workflow"; vi.mock("@/lib/agents/generalAgent/getGeneralAgent", () => ({ default: vi.fn(), })); -vi.mock("@/lib/emails/inbound/getEmailRoomMessages", () => ({ - getEmailRoomMessages: vi.fn(), +vi.mock("@/lib/supabase/memories/selectMemories", () => ({ + default: vi.fn(), })); vi.mock("@/lib/emails/getEmailFooter", () => ({ @@ -43,7 +45,10 @@ describe("generateEmailResponse", () => { }); it("generates response with text and footer", async () => { - vi.mocked(getEmailRoomMessages).mockResolvedValue([{ role: "user", content: "Hi there" }]); + vi.mocked(selectMemories).mockResolvedValue([ + { content: { role: "user", parts: [{ type: "text", text: "Hi there" }] } }, + { content: { role: "assistant", parts: [{ type: "text", text: "Earlier reply" }] } }, + ] as never); const body: ChatRequestBody = { accountId: "acc-1", @@ -55,10 +60,51 @@ describe("generateEmailResponse", () => { const result = await generateEmailResponse(body); expect(mockGenerate).toHaveBeenCalledWith({ - messages: [{ role: "user", content: "Hi there" }], + messages: [ + { role: "user", content: "Hi there" }, + { role: "assistant", content: "Earlier reply" }, + ], }); + expect(selectMemories).toHaveBeenCalledExactlyOnceWith("room-1", { ascending: true }); + expect(getGeneralAgent).toHaveBeenCalledExactlyOnceWith(body); + expect(selectRoomWithArtist).toHaveBeenCalledExactlyOnceWith("room-1"); expect(result.text).toBe("Hello from assistant"); expect(result.html).toContain("Hello from assistant"); expect(result.html).toContain("
footer
"); }); + it.each(["part", "metadata"])( + "denies raw email history with a %s marker before text adaptation", + async location => { + const marker = { type: "data-retained-field-projection", private: "MARKER-SECRET" }; + const content = { + role: "assistant", + parts: [ + ...(location === "part" ? [marker] : []), + { type: "text", text: "PRIVATE-SIBLING-987654321" }, + ], + ...(location === "metadata" + ? { metadata: { contract: "catalog-retained-field-projection/v1" } } + : {}), + }; + const memories = [{ content }]; + const original = structuredClone(memories); + vi.mocked(selectMemories).mockResolvedValue(memories as never); + const body = { + accountId: "authenticated-account", + orgId: "authenticated-org", + roomId: "authorized-room", + messages: [], + } as ChatRequestBody; + const result = generateEmailResponse(body); + await expect(result).rejects.toBeInstanceOf(FatalError); + await expect(result).rejects.toThrow(/^Retained-field model input unavailable$/); + expect(mockGenerate).not.toHaveBeenCalled(); + expect(selectRoomWithArtist).not.toHaveBeenCalled(); + expect(selectMemories).toHaveBeenCalledExactlyOnceWith("authorized-room", { + ascending: true, + }); + expect(getGeneralAgent).toHaveBeenCalledExactlyOnceWith(body); + expect(memories).toEqual(original); + }, + ); }); diff --git a/lib/emails/inbound/getEmailRoomMessages.ts b/lib/emails/inbound/getEmailRoomMessages.ts index daf202756..8adae84e2 100644 --- a/lib/emails/inbound/getEmailRoomMessages.ts +++ b/lib/emails/inbound/getEmailRoomMessages.ts @@ -1,3 +1,4 @@ +import { assertNoRetainedFieldModelInput } from "@/lib/chat/retained-field/assertNoRetainedFieldModelInput"; import type { ModelMessage } from "ai"; import selectMemories from "@/lib/supabase/memories/selectMemories"; import { extractTextFromParts } from "./extractTextFromParts"; @@ -17,6 +18,8 @@ interface MemoryContent { export async function getEmailRoomMessages(roomId: string): Promise { const existingMemories = await selectMemories(roomId, { ascending: true }); if (!existingMemories) return []; + // Inspect stored content before text-only adaptation drops structured markers. + assertNoRetainedFieldModelInput(existingMemories.map(memory => memory.content)); const messages: ModelMessage[] = []; diff --git a/lib/sessions/chats/__tests__/getSessionChatHandler.test.ts b/lib/sessions/chats/__tests__/getSessionChatHandler.test.ts index b6c4c2ee3..cd587409f 100644 --- a/lib/sessions/chats/__tests__/getSessionChatHandler.test.ts +++ b/lib/sessions/chats/__tests__/getSessionChatHandler.test.ts @@ -23,9 +23,10 @@ function makeReq(): NextRequest { } function mockValidated(chatOverrides: Parameters[0] = {}) { - vi.mocked(validateGetSessionChatRequest).mockResolvedValue( - baseChatRow({ id: "chat_1", session_id: "sess_1", ...chatOverrides }), - ); + vi.mocked(validateGetSessionChatRequest).mockResolvedValue({ + chat: baseChatRow({ id: "chat_1", session_id: "sess_1", ...chatOverrides }), + actorAccountId: "actual-actor", + }); } describe("getSessionChatHandler", () => { diff --git a/lib/sessions/chats/__tests__/getSessionChatRoute.readFailure.test.ts b/lib/sessions/chats/__tests__/getSessionChatRoute.readFailure.test.ts index 5178156c9..905c20859 100644 --- a/lib/sessions/chats/__tests__/getSessionChatRoute.readFailure.test.ts +++ b/lib/sessions/chats/__tests__/getSessionChatRoute.readFailure.test.ts @@ -46,7 +46,10 @@ beforeEach(() => { mocks.from.mockImplementation(() => { throw new Error("Unexpected storage access"); }); - mocks.validate.mockResolvedValue(baseChatRow({ id: chatId, session_id: sessionId })); + mocks.validate.mockResolvedValue({ + chat: baseChatRow({ id: chatId, session_id: sessionId }), + actorAccountId: "actual-actor", + }); }); afterEach(() => { expect(fetch).not.toHaveBeenCalled(); @@ -82,9 +85,10 @@ it("preserves the saved UIMessage and streaming state", async () => { role: "assistant", parts: [{ type: "text", text: "Saved answer" }], }; - mocks.validate.mockResolvedValue( - baseChatRow({ id: chatId, session_id: sessionId, active_stream_id: "synthetic-run" }), - ); + mocks.validate.mockResolvedValue({ + chat: baseChatRow({ id: chatId, session_id: sessionId, active_stream_id: "synthetic-run" }), + actorAccountId: "actual-actor", + }); mockRead([{ parts: message }]); const response = await request(); expect(response.status).toBe(200); diff --git a/lib/sessions/chats/__tests__/retainedFieldHistory.test.ts b/lib/sessions/chats/__tests__/retainedFieldHistory.test.ts new file mode 100644 index 000000000..f1b36977a --- /dev/null +++ b/lib/sessions/chats/__tests__/retainedFieldHistory.test.ts @@ -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(); +}); diff --git a/lib/sessions/chats/__tests__/validateGetSessionChatRequest.test.ts b/lib/sessions/chats/__tests__/validateGetSessionChatRequest.test.ts index e3f4bc595..7025beb43 100644 --- a/lib/sessions/chats/__tests__/validateGetSessionChatRequest.test.ts +++ b/lib/sessions/chats/__tests__/validateGetSessionChatRequest.test.ts @@ -143,8 +143,9 @@ describe("validateGetSessionChatRequest", () => { const res = await validateGetSessionChatRequest(makeReq(), "sess_1", "chat_1"); expect(res).not.toBeInstanceOf(NextResponse); if (!(res instanceof NextResponse)) { - expect(res.id).toBe("chat_1"); - expect(res.session_id).toBe("sess_1"); + expect(res.chat.id).toBe("chat_1"); + expect(res.actorAccountId).toBe(accountId); + expect(res.chat.session_id).toBe("sess_1"); } expect(selectSessions).toHaveBeenCalledWith({ id: "sess_1" }); expect(selectChats).toHaveBeenCalledWith({ id: "chat_1" }); diff --git a/lib/sessions/chats/getSessionChatHandler.ts b/lib/sessions/chats/getSessionChatHandler.ts index 23ee6be1c..9a85c854d 100644 --- a/lib/sessions/chats/getSessionChatHandler.ts +++ b/lib/sessions/chats/getSessionChatHandler.ts @@ -4,6 +4,7 @@ import { getCorsHeaders } from "@/lib/networking/getCorsHeaders"; import { toChatResponse } from "@/lib/sessions/toChatResponse"; import { validateGetSessionChatRequest } from "@/lib/sessions/chats/validateGetSessionChatRequest"; import { selectChatMessages } from "@/lib/supabase/chat_messages/selectChatMessages"; +import { filterRetainedFieldHistory } from "./retained-field/filterRetainedFieldHistory"; export interface SessionChatResponse { chat: ReturnType; @@ -17,7 +18,8 @@ export interface SessionChatResponse { * persisted UI message stream — enough for both initial render and * in-tab refresh. * - * `messages` is the raw `parts` JSON for each row (chat messages are + * Retained-field messages are withheld as a whole until current-source disclosure + * is implemented. Other `messages` retain the raw `parts` JSON (chat messages are * stored as the full UIMessage; the caller deserializes back into * its UIMessage type). * @@ -31,10 +33,11 @@ export async function getSessionChatHandler( sessionId: string, chatId: string, ): Promise { - const chat = await validateGetSessionChatRequest(request, sessionId, chatId); - if (chat instanceof NextResponse) { - return chat; + const validated = await validateGetSessionChatRequest(request, sessionId, chatId); + if (validated instanceof NextResponse) { + return validated; } + const { chat, actorAccountId } = validated; const messages = await selectChatMessages({ chatId, orderBy: { createdAt: "asc" } }); if (messages === null) return errorResponse("Failed to load messages", 500); @@ -43,8 +46,11 @@ export async function getSessionChatHandler( { chat: toChatResponse(chat), isStreaming: chat.active_stream_id !== null, - messages: messages.map(row => row.parts), + messages: await filterRetainedFieldHistory( + { actorAccountId, sessionId: chat.session_id, chatId: chat.id }, + messages, + ), } satisfies SessionChatResponse, - { status: 200, headers: getCorsHeaders() }, + { status: 200, headers: { ...getCorsHeaders(), "Cache-Control": "private, no-store" } }, ); } diff --git a/lib/sessions/chats/retained-field/README.md b/lib/sessions/chats/retained-field/README.md new file mode 100644 index 000000000..19cb5a530 --- /dev/null +++ b/lib/sessions/chats/retained-field/README.md @@ -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__`. diff --git a/lib/sessions/chats/retained-field/filterRetainedFieldHistory.ts b/lib/sessions/chats/retained-field/filterRetainedFieldHistory.ts new file mode 100644 index 000000000..26e9df464 --- /dev/null +++ b/lib/sessions/chats/retained-field/filterRetainedFieldHistory.ts @@ -0,0 +1,28 @@ +import type { Tables } from "@/types/database.types"; +import { hasRetainedFieldContent } from "./hasRetainedFieldContent"; +import { + readRetainedFieldDisclosure, + type RetainedFieldDisclosureScope, +} from "./readRetainedFieldDisclosure"; + +/** Withhold the entire stored message, including sibling text, tables and metadata. */ +export async function filterRetainedFieldHistory( + scope: Omit, + rows: Tables<"chat_messages">[], +): Promise { + const messages: unknown[] = []; + for (const row of rows) { + if (!hasRetainedFieldContent(row.parts)) { + messages.push(row.parts); + continue; + } + try { + await readRetainedFieldDisclosure({ ...scope, messageId: row.id }, row.parts); + } catch { + // Operational failure cannot release any projection or existence diagnostics. + } + // No positive transport/publication protocol is adopted. Even a surprising + // cached "ready" result cannot authorize returning stored projections. + } + return messages; +} diff --git a/lib/sessions/chats/retained-field/hasRetainedFieldContent.ts b/lib/sessions/chats/retained-field/hasRetainedFieldContent.ts new file mode 100644 index 000000000..794e262e6 --- /dev/null +++ b/lib/sessions/chats/retained-field/hasRetainedFieldContent.ts @@ -0,0 +1,24 @@ +/** Recognize the reserved family even if malformed; validation must not become a disclosure bypass. */ +export function hasRetainedFieldContent(message: unknown): boolean { + const pending: unknown[] = [message]; + while (pending.length) { + const value = pending.pop(); + if (!value || typeof value !== "object") continue; + if (Array.isArray(value)) { + for (const child of value) pending.push(child); + continue; + } + const record = value as Record; + if ( + (typeof record.type === "string" && record.type.startsWith("data-retained-field-")) || + (typeof record.capability === "string" && + record.capability.startsWith("retained-field-companion/")) || + (typeof record.contract === "string" && + record.contract.startsWith("catalog-retained-field-projection/")) || + record.mode === "retained_field" + ) + return true; + for (const child of Object.values(record)) pending.push(child); + } + return false; +} diff --git a/lib/sessions/chats/retained-field/readRetainedFieldDisclosure.ts b/lib/sessions/chats/retained-field/readRetainedFieldDisclosure.ts new file mode 100644 index 000000000..91a93b562 --- /dev/null +++ b/lib/sessions/chats/retained-field/readRetainedFieldDisclosure.ts @@ -0,0 +1,17 @@ +export type RetainedFieldDisclosureScope = { + actorAccountId: string; + sessionId: string; + chatId: string; + messageId: string; +}; +/** + * No reviewed current-authority transport exists. This boundary cannot grant disclosure. + * The saved message is untrusted source selection/projection, never an authority receipt. + * No network, latest selection, re-key, retain, expansion or cached-ready fallback. + */ +export async function readRetainedFieldDisclosure( + _scope: RetainedFieldDisclosureScope, + _savedMessage: unknown, +): Promise<{ state: "unavailable" | "error" }> { + return { state: "unavailable" }; +} diff --git a/lib/sessions/chats/validateGetSessionChatRequest.ts b/lib/sessions/chats/validateGetSessionChatRequest.ts index 9324cc215..ac2c0be68 100644 --- a/lib/sessions/chats/validateGetSessionChatRequest.ts +++ b/lib/sessions/chats/validateGetSessionChatRequest.ts @@ -14,18 +14,18 @@ import type { Tables } from "@/types/database.types"; * 3. Loads the chat and confirms it belongs to the session * * Returns either a 401/403/404 NextResponse describing the first - * failure, or the resolved chat row for the handler to render. + * failure, or the resolved chat and authenticated account for the handler. * * @param request - The incoming request. * @param sessionId - The id of the parent session. * @param chatId - The id of the chat being fetched. - * @returns A NextResponse on failure, or the chat row on success. + * @returns A NextResponse on failure, or the chat and actual authenticated account on success. */ export async function validateGetSessionChatRequest( request: NextRequest, sessionId: string, chatId: string, -): Promise> { +): Promise; actorAccountId: string }> { const auth = await validateAuthContext(request); if (auth instanceof NextResponse) { return auth; @@ -59,5 +59,5 @@ export async function validateGetSessionChatRequest( ); } - return chat; + return { chat, actorAccountId: auth.accountId }; } diff --git a/lib/slack/chat/handlers/handleSlackChatMessage.ts b/lib/slack/chat/handlers/handleSlackChatMessage.ts index c990578cf..984372ead 100644 --- a/lib/slack/chat/handlers/handleSlackChatMessage.ts +++ b/lib/slack/chat/handlers/handleSlackChatMessage.ts @@ -1,3 +1,4 @@ +import { assertNoRetainedFieldModelInput } from "@/lib/chat/retained-field/assertNoRetainedFieldModelInput"; import type { UIMessage } from "ai"; import type { SlackChatThreadState } from "../types"; import type { ChatRequestBody } from "@/lib/chat/validateChatRequest"; @@ -77,6 +78,7 @@ export async function handleSlackChatMessage( // Load full conversation history from the room (includes the message we just saved) const memories = await selectMemories(roomId, { ascending: true }); + assertNoRetainedFieldModelInput((memories ?? []).map(memory => memory.content)); const historyMessages: UIMessage[] = (memories ?? []) .filter(m => { const content = m.content as unknown as { role?: string; parts?: unknown[] }; diff --git a/scripts/chat-fence-workflow-fixture/run.mjs b/scripts/chat-fence-workflow-fixture/run.mjs index 4960f3592..c2c7fcbd9 100644 --- a/scripts/chat-fence-workflow-fixture/run.mjs +++ b/scripts/chat-fence-workflow-fixture/run.mjs @@ -54,6 +54,8 @@ for (const name of await readdir(join(root, "app/lib/workflows/fence"))) if (name.endsWith(".ts")) await copy(`app/lib/workflows/fence/${name}`, `fence/${name}`); await copy("lib/chat/auto-commit/autoCommitChatTurn.ts", "autoCommitChatTurn.ts"); for (const path of [ + "lib/chat/retained-field/assertNoRetainedFieldModelInput.ts", + "lib/sessions/chats/retained-field/hasRetainedFieldContent.ts", "lib/chat/fence/encodeStrictJson.ts", "lib/chat/fence/persistence/createFencedMessageWriter.ts", "lib/supabase/chat_fence/executeChatFenceCommand.ts", @@ -104,7 +106,7 @@ const seams = [ [ "@/app/lib/workflows/convertMessagesStep", "convertMessagesStep", - 'record("convert"); return [];', + 'record("convert"); return scenario().fail.includes("retained-converted")?[{mode:"retained_field",amount:987654321}]:[];', ], ["@/app/lib/workflows/sendStreamStart", "sendStreamStart", 'record("stream-start",args[1]);'], ["@/app/lib/workflows/sendStreamFinish", "sendStreamFinish", 'record("stream-finish");'], @@ -117,7 +119,7 @@ const seams = [ [ "@/app/lib/workflows/runAgentStep", "runAgentStep", - 'record("provider",{original:args[0].originalMessages}); const {readUIMessageStream}=await import("ai"); const initial=args[0].originalMessages[0]; const stream=new ReadableStream({start(c){c.enqueue({type:"start",messageId:initial.id});c.enqueue({type:"text-start",id:"generated"});c.enqueue({type:"text-delta",id:"generated",delta:"synthetic result"});c.enqueue({type:"text-end",id:"generated"});c.enqueue({type:"finish"});c.close();}}); let responseMessage; for await(const value of readUIMessageStream({message:initial,stream})) responseMessage=value; record("sdk-optional",Object.hasOwn(responseMessage.parts.at(-1),"providerMetadata")); return {finishReason:scenario().fail==="iteration"?"tool-calls":"stop",aborted:false,responseMessages:[],responseMessage};', + 'record("provider",{original:args[0].originalMessages}); if(scenario().fail==="retained-iteration") return {finishReason:"tool-calls",aborted:false,responseMessages:[{role:"tool",content:[{type:"tool-result",output:{type:"json",value:{mode:"retained_field",amount:987654321}}}]}]}; const {readUIMessageStream}=await import("ai"); const initial=args[0].originalMessages[0]; const stream=new ReadableStream({start(c){c.enqueue({type:"start",messageId:initial.id});c.enqueue({type:"text-start",id:"generated"});c.enqueue({type:"text-delta",id:"generated",delta:"synthetic result"});c.enqueue({type:"text-end",id:"generated"});c.enqueue({type:"finish"});c.close();}}); let responseMessage; for await(const value of readUIMessageStream({message:initial,stream})) responseMessage=value; record("sdk-optional",Object.hasOwn(responseMessage.parts.at(-1),"providerMetadata")); return {finishReason:scenario().fail==="iteration"?"tool-calls":"stop",aborted:false,responseMessages:[],responseMessage};', ], [ "@/app/lib/workflows/persistAssistantMessageStep", @@ -245,6 +247,17 @@ try { clearTimeout(timer); } } + // Terminal runs are skipped by the runtime; this does not prove another guard execution. + async function assertTerminalDeliveryNoop(runId, trace) { + const beforeDelivery = deliveries; + await reenqueueRun(world, runId); + await bounded( + (async () => { + while (deliveries === beforeDelivery) await new Promise(r => setTimeout(r, 20)); + })(), + ); + assert.deepEqual(await events(), trace); + } const traces = []; for (const name of [ "success", @@ -260,7 +273,15 @@ try { "new-object", "resume", "legacy", + "retained-input", + "retained-converted", + "retained-iteration", + "legacy-retained-input-interactive", + "legacy-retained-input-headless", + "legacy-retained-converted-interactive", + "legacy-retained-converted-headless", ]) { + const legacy = name === "legacy" || name.startsWith("legacy-retained-"); await writeFile(join(effects, "events.jsonl"), ""); await writeFile(join(effects, "scenario.json"), JSON.stringify({ fail: name })); const invocation = { @@ -283,8 +304,21 @@ try { }); if (name === "resume") invocation.fence.turn = { kind: "same-turn-resume", assistantId: "historical-assistant" }; - const run = await start(runAgentWorkflow, [name === "legacy" ? invocation.input : invocation]); - if ( + if (name.startsWith("legacy-retained-") && name.endsWith("interactive")) + delete invocation.input.agentContext.ephemeralKeyId; + if (name.includes("retained-input")) + invocation.input.messages.push({ + id: "private", + role: "assistant", + parts: [ + { type: "data-retained-field-snapshot", data: { amount: 987654321 } }, + { type: "text", text: "PRIVATE" }, + ], + }); + const run = await start(runAgentWorkflow, [legacy ? invocation.input : invocation]); + if (name.includes("retained-")) + await assert.rejects(bounded(run.returnValue), /Retained-field model input unavailable/); + else if ( [ "bind", "user", @@ -301,7 +335,7 @@ try { const trace = await events(); const actions = trace.map(x => x.action); const commands = trace.filter(x => x.action === "rpc").map(x => x.data); - if (name !== "legacy") { + if (!legacy) { assert.equal(actions[0], "rpc"); assert.equal(commands[0].p_action, "bind"); assert(!actions.includes("legacy-clear")); @@ -313,6 +347,18 @@ try { assert.equal(c.p.epoch, 1); assert.equal(c.p.owner, authority.owner); } + if (name.startsWith("retained-")) { + assert(!actions.includes("commit")); + assert(!commands.some(c => c.p_action === "release")); + assert(actions.includes("stream-close")); + assert.equal( + actions.filter(x => x === "provider").length, + name === "retained-iteration" ? 1 : 0, + ); + if (name === "retained-input") assert(!actions.includes("convert")); + if (name !== "retained-iteration") assert(!actions.includes("routing")); + await assertTerminalDeliveryNoop(run.runId, trace); + } if (name === "bind") assert.deepEqual(actions, ["rpc"]); if (name === "user") { assert(!actions.includes("routing")); @@ -338,15 +384,7 @@ try { trace.findIndex(x => x.action === "rpc" && x.data.p_action === "release") < actions.indexOf("stream-finish"), ); - // Completed journal replay must not repeat any provider/write/commit/cleanup effect. - const beforeReplay = deliveries; - await reenqueueRun(world, run.runId); - await bounded( - (async () => { - while (deliveries === beforeReplay) await new Promise(r => setTimeout(r, 20)); - })(), - ); - assert.deepEqual(await events(), trace); + await assertTerminalDeliveryNoop(run.runId, trace); } if (name === "new-object") { const original = trace.find(x => x.action === "provider").data.original[0]; @@ -360,9 +398,33 @@ try { } } else { assert.equal(commands.length, 0); - assert(actions.includes("legacy-clear")); - assert(actions.includes("key-delete")); - assert(actions.includes("legacy-parts")); + if (name === "legacy") { + assert(actions.includes("legacy-clear")); + assert(actions.includes("key-delete")); + assert(actions.includes("legacy-parts")); + } else { + assert.equal(actions.filter(x => x === "legacy-clear").length, 1); + assert.equal( + actions.filter(x => x === "key-delete").length, + name.endsWith("headless") ? 1 : 0, + ); + assert.equal(actions.filter(x => x === "stream-finish").length, 1); + assert.equal(actions.filter(x => x === "stream-close").length, 1); + assert(actions.indexOf("stream-finish") < actions.indexOf("stream-close")); + assert.equal( + actions.filter(x => x === "convert").length, + name.includes("converted") ? 1 : 0, + ); + for (const forbidden of [ + "provider", + "routing", + "commit", + "legacy-write", + "legacy-parts", + "stream-start", + ]) + assert(!actions.includes(forbidden)); + } } traces.push({ name, runId: run.runId, trace }); console.log(`PASS actual compiled workflow: ${name}`); diff --git a/scripts/proposals/catalog-retained-selection/README.md b/scripts/proposals/catalog-retained-selection/README.md new file mode 100644 index 000000000..b85119232 --- /dev/null +++ b/scripts/proposals/catalog-retained-selection/README.md @@ -0,0 +1,52 @@ +# Retained-field selection and inner consumer — inactive source proposal + +This implements the existing `catalog-field-consumer-2207.md` inner contract only. +It is outside app/lib and routes. Missing authority ports return unavailable. +No SQL wrapper, grant, source write, Context identity creation, environment flag, +UI schema-v1 extension or outer wire version is supplied. Frozen raw-report +API1290/Database142 are a different source path and are unchanged. + +`readRetainedFieldSelection` calls an injected current command reconciler with the +original authenticated actor, workspace, request, catalog and retention key. The +intended backing function is the private, source-only Database proposal +[`stream_binding_proposal.reconcile_retention`](https://github.com/recoupable/database/blob/1b892dd2671ba7e15e0c44b690fbfbe515b59fbd/proposals/stream-retention-writer-2207/writer.sql#L3), +which rechecks `read_binding`. It is not implemented or connected as a live API +transport here; the injected callback alone supplies no authentication proof. +The adapter validates the exact receipt/scope/key and projects its +genuine Context identities. Null, pending, malformed, mismatched or ambiguous +results cannot create a ready selection; operational errors are redacted errors. +No retain/expand/provider call, latest lookup, re-key or raw-report fallback exists. +This projection supplies a selection, not a currentness lease. + +`consumeRetainedField` accepts only the explicit retained_field request and server +actor/session/chat/assistant context. The injected transaction port must authorize +that complete scope, pre-lock the whole batch in a qualified stable order, read +each exact selection using the existing source family, and keep locks through +callback evaluation. No production port is implemented here. Passing mocked data +to that trusted port does not prove source authorization. The actor is never +substituted with owner/workspace. Maximum eight unique subjects; all-or-nothing +refusal, exact registered method/query, current_streams only, 32KiB output cap. +It calls the existing pinned evaluator, then strips raw observations and identifiers +outside the inner projection contract. Unknown has no amount key; zero stays zero. +Caller order is preserved; the port receives a separate subject-sorted lock list. + +The generated `fixtures/fieldContext.json` is the ready inner response without +`state`, as specified by the consumer contract. It is synthetic evidence for UI's +explicitly versioned offline envelope; no outer version is invented here. UI owns +projection/collection/text agreement and capability negotiation. This increment +returns typed values but does not implement a renderer or any disclosure path. + +Pure tests run the actual selection adapter and evaluator with synthetic authority +ports, including withheld values on stale/missing/withdrawn boundary simulations. +The Database companion extends the actual existing retained-field integration +harness with a deferred current reconcile/read consumer. Those PostgreSQL scenarios +are not run under the SQL hold. Simulated refusal labels do not prove actual +withdrawal, memberships, transaction liveness or concurrent batch correctness. + +Remaining runtime gates: real selection-source transport, actor/session/chat scope, +reserved-family writer enforcement, callable held-transaction evaluator transport, +qualified batch lock order, UI-owned envelope, and current authorization on initial +delivery/replay/persistence/history/reconnect/text/export/model reads. A successful +journaled preparation cannot authorize later disclosure; deny both typed values and +numeric/text projections together. No recomputation or new version under the same +saved source revision. The ordinary report stays separate and is never a fallback. diff --git a/scripts/proposals/catalog-retained-selection/classifyRetainedFieldRefusal.ts b/scripts/proposals/catalog-retained-selection/classifyRetainedFieldRefusal.ts new file mode 100644 index 000000000..e9f9bb0c4 --- /dev/null +++ b/scripts/proposals/catalog-retained-selection/classifyRetainedFieldRefusal.ts @@ -0,0 +1,8 @@ +/** Preserve contract failure classes while exposing no source details or values. */ +export function classifyRetainedFieldRefusal(reason: string): { + state: "invalid" | "error" | "unavailable"; +} { + if (reason === "size_limit") return { state: "invalid" }; + if (reason === "helper_changed" || reason === "source_error") return { state: "error" }; + return { state: "unavailable" }; +} diff --git a/scripts/proposals/catalog-retained-selection/consumeRetainedField.ts b/scripts/proposals/catalog-retained-selection/consumeRetainedField.ts new file mode 100644 index 000000000..5a76ea6e8 --- /dev/null +++ b/scripts/proposals/catalog-retained-selection/consumeRetainedField.ts @@ -0,0 +1,93 @@ +import { z } from "zod"; +import { evaluateRetainedStreamField } from "../catalog-stream-fields/evaluate"; +import { actorScopeSchema, requestSchema } from "./schemas"; +import type { Item, Ready, Result, LockedRead } from "./retainedFieldTypes"; +import { classifyRetainedFieldRefusal } from "./classifyRetainedFieldRefusal"; +/** Inactive source consumer; no report fallback, disclosure route, transport or UI schema change. */ +export async function consumeRetainedField( + actorInput: unknown, + input: unknown, + withLockedRead?: LockedRead, +): Promise { + const actor = actorScopeSchema.safeParse(actorInput); + const request = requestSchema.safeParse(input); + if (!actor.success || !request.success) return { state: "invalid" }; + if (!withLockedRead) return { state: "unavailable" }; + const q = request.data; + try { + return await withLockedRead( + { + ...actor.data, + workspaceId: q.workspaceId, + catalogId: q.catalogId, + selections: [...q.selections].sort((a, b) => a.subjectId.localeCompare(b.subjectId)), + }, + async tx => { + if ( + !z.iso.datetime({ offset: true }).safeParse(tx.asOf).success || + !Number.isFinite(Date.parse(tx.asOf)) + ) + return { state: "error" }; + const items: Item[] = []; + for (const selection of q.selections) { + const envelope = await tx.readBinding(selection); + if (envelope === null || envelope === undefined) return { state: "unavailable" }; + const result = evaluateRetainedStreamField({ + operation: "read_field", + recipe: q.method.recipe, + field: q.field, + selection: { ...selection, query: q.query }, + asOf: tx.asOf, + boundary: { state: "qualified", envelope }, + }); + if (result.state !== "ready") return classifyRetainedFieldRefusal(result.reason); + const comparisonState = result.coverage.comparisonState; + if (comparisonState !== "comparable" && comparisonState !== "incomplete") + return { state: "error" }; + const { authority, kind } = result.ref; + if (authority !== "context" || kind !== "recording") return { state: "error" }; + const amount = result.field.amount; + if (amount !== null && (!Number.isSafeInteger(amount) || amount < 0)) + return { state: "unavailable" }; + items.push({ + ref: { ...result.ref, authority, kind }, + binding: selection, + value: amount === null ? { state: "unknown" } : { state: "known", amount }, + periods: result.periods, + coverage: { + comparisonState, + absentDateCount: result.coverage.absentDates.length, + nullDateCount: result.coverage.nullDates.length, + missingDateCount: result.coverage.missingDays.length, + }, + source: { + provider: result.provenance.provider, + metric: result.provenance.metric, + platform: result.provenance.platform, + territory: result.provenance.territory, + timezone: result.provenance.timezone, + retrievalRange: result.provenance.retrievalRange, + }, + }); + } + const ready: Ready = { + state: "ready", + contract: "catalog-retained-field-projection/1", + workspaceId: q.workspaceId, + catalogId: q.catalogId, + field: q.field, + method: q.method, + query: q.query, + asOf: tx.asOf, + semantics: "historical_snapshot", + items, + }; + return Buffer.byteLength(JSON.stringify(ready), "utf8") > 32768 + ? { state: "invalid" } + : ready; + }, + ); + } catch { + return { state: "error" }; + } +} diff --git a/scripts/proposals/catalog-retained-selection/fixtures/fieldContext.json b/scripts/proposals/catalog-retained-selection/fixtures/fieldContext.json new file mode 100644 index 000000000..c53e71597 --- /dev/null +++ b/scripts/proposals/catalog-retained-selection/fixtures/fieldContext.json @@ -0,0 +1,77 @@ +{ + "contract": "catalog-retained-field-projection/1", + "workspaceId": "10000000-0000-4000-8000-000000000001", + "catalogId": "10000000-0000-4000-8000-000000000003", + "field": "current_streams", + "method": { + "recipe": "catalog-retained-stream-fields/1", + "recipeHash": "65a06a785aa496e1232fe3a9f1606595f19c1a152b710ca30c4c06612f01864a", + "evaluatorSha256": "163563e15dd92d473aa51899c54c96300f20d27072b83e39ffb01c84f3adc64e", + "helperSha256": "91dcca66c8dec0851c1f6e110257a63b730e9b7ae33a60bceb0e9ffcf318dd81" + }, + "query": { + "since": "2026-10-24", + "days": 1, + "timezone": "UTC" + }, + "asOf": "2026-10-27T00:00:00Z", + "semantics": "historical_snapshot", + "items": [ + { + "ref": { + "workspaceId": "10000000-0000-4000-8000-000000000001", + "authority": "context", + "kind": "recording", + "id": "10000000-0000-4000-8000-000000000004", + "sourceVersionIds": [ + "10000000-0000-4000-8000-000000000006" + ] + }, + "binding": { + "ownerId": "10000000-0000-4000-8000-000000000001", + "requestId": "10000000-0000-4000-8000-000000000002", + "subjectId": "10000000-0000-4000-8000-000000000004", + "catalogId": "10000000-0000-4000-8000-000000000003", + "resultId": "10000000-0000-4000-8000-000000000005", + "revision": 7, + "topic": "catalog_retained_streams", + "sourceVersionIds": [ + "10000000-0000-4000-8000-000000000006" + ] + }, + "value": { + "state": "known", + "amount": 100 + }, + "periods": { + "previous": { + "start": "2026-10-23", + "end_exclusive": "2026-10-24" + }, + "current": { + "start": "2026-10-24", + "end_exclusive": "2026-10-25" + }, + "days": 1, + "timezone": "UTC" + }, + "coverage": { + "comparisonState": "comparable", + "absentDateCount": 0, + "nullDateCount": 0, + "missingDateCount": 0 + }, + "source": { + "provider": "luminate", + "metric": "daily_streams", + "platform": "all_dsps", + "territory": "worldwide", + "timezone": "UTC", + "retrievalRange": { + "earliest": "2026-10-25T01:00:00Z", + "latest": "2026-10-25T01:00:00Z" + } + } + } + ] +} diff --git a/scripts/proposals/catalog-retained-selection/fixtures/synthetic-authority-input.json b/scripts/proposals/catalog-retained-selection/fixtures/synthetic-authority-input.json new file mode 100644 index 000000000..a9d4b6cf9 --- /dev/null +++ b/scripts/proposals/catalog-retained-selection/fixtures/synthetic-authority-input.json @@ -0,0 +1,99 @@ +{ + "scope": { + "actorId": "10000000-0000-4000-8000-000000000009", + "workspaceId": "10000000-0000-4000-8000-000000000001", + "requestId": "10000000-0000-4000-8000-000000000002", + "catalogId": "10000000-0000-4000-8000-000000000003", + "key": "original" + }, + "actor": { + "actorId": "10000000-0000-4000-8000-000000000009", + "sessionId": "session", + "chatId": "chat", + "messageId": "assistant" + }, + "receipt": { + "ownerId": "10000000-0000-4000-8000-000000000001", + "requestId": "10000000-0000-4000-8000-000000000002", + "subjectId": "10000000-0000-4000-8000-000000000004", + "catalogId": "10000000-0000-4000-8000-000000000003", + "key": "original", + "commandDigest": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "resultId": "10000000-0000-4000-8000-000000000005", + "sourceVersionId": "10000000-0000-4000-8000-000000000006", + "revision": 7 + }, + "envelope": { + "ownerId": "10000000-0000-4000-8000-000000000001", + "requestId": "10000000-0000-4000-8000-000000000002", + "subjectId": "10000000-0000-4000-8000-000000000004", + "catalogId": "10000000-0000-4000-8000-000000000003", + "resultId": "10000000-0000-4000-8000-000000000005", + "revision": 7, + "topic": "catalog_retained_streams", + "sourceVersionIds": [ + "10000000-0000-4000-8000-000000000006" + ], + "evidence": { + "schemaVersion": 1, + "workspaceId": "10000000-0000-4000-8000-000000000001", + "catalogId": "10000000-0000-4000-8000-000000000003", + "subjectId": "10000000-0000-4000-8000-000000000004", + "isrc": "USAAA2600001", + "provider": "luminate", + "metric": "daily_streams", + "platform": "all_dsps", + "territory": "worldwide", + "timezone": "UTC", + "since": "2026-10-23", + "until": "2026-10-25", + "periods": { + "previous": { + "start": "2026-10-23", + "end_exclusive": "2026-10-24" + }, + "current": { + "start": "2026-10-24", + "end_exclusive": "2026-10-25" + }, + "days": 1, + "timezone": "UTC" + }, + "providerRecordingId": "provider-original", + "observations": [ + { + "catalog_id": "10000000-0000-4000-8000-000000000003", + "isrc": "USAAA2600001", + "date": "2026-10-23", + "run_id": "10000000-0000-4000-8000-000000000007", + "provider_recording_id": "provider-original", + "streams": 50, + "source_hash": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "retrieved_at": "2026-10-25T01:00:00Z" + }, + { + "catalog_id": "10000000-0000-4000-8000-000000000003", + "isrc": "USAAA2600001", + "date": "2026-10-24", + "run_id": "10000000-0000-4000-8000-000000000007", + "provider_recording_id": "provider-original", + "streams": 100, + "source_hash": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "retrieved_at": "2026-10-25T01:00:00Z" + } + ], + "absentDates": [], + "nullDates": [], + "retrievalRange": { + "earliest": "2026-10-25T01:00:00Z", + "latest": "2026-10-25T01:00:00Z" + } + } + }, + "query": { + "since": "2026-10-24", + "days": 1, + "timezone": "UTC" + }, + "asOf": "2026-10-27T00:00:00Z" +} diff --git a/scripts/proposals/catalog-retained-selection/readRetainedFieldSelection.ts b/scripts/proposals/catalog-retained-selection/readRetainedFieldSelection.ts new file mode 100644 index 000000000..211469d49 --- /dev/null +++ b/scripts/proposals/catalog-retained-selection/readRetainedFieldSelection.ts @@ -0,0 +1,39 @@ +import { commandScopeSchema, receiptSchema, type CommandScope, type Selection } from "./schemas"; +/** Proposal only. Reconcile is the existing current-authorized command read, not a client receipt. */ +export async function readRetainedFieldSelection( + input: unknown, + reconcile?: (scope: CommandScope) => Promise, +): Promise< + { state: "ready"; selection: Selection } | { state: "invalid" | "unavailable" | "error" } +> { + const scope = commandScopeSchema.safeParse(input); + if (!scope.success) return { state: "invalid" }; + if (!reconcile) return { state: "unavailable" }; + try { + const parsed = receiptSchema.safeParse(await reconcile(scope.data)); + if (!parsed.success) return { state: "unavailable" }; + const r = parsed.data; + if ( + r.ownerId !== scope.data.workspaceId || + r.requestId !== scope.data.requestId || + r.catalogId !== scope.data.catalogId || + r.key !== scope.data.key + ) + return { state: "unavailable" }; + return { + state: "ready", + selection: { + ownerId: r.ownerId, + requestId: r.requestId, + subjectId: r.subjectId, + catalogId: r.catalogId, + resultId: r.resultId, + revision: r.revision, + topic: "catalog_retained_streams", + sourceVersionIds: [r.sourceVersionId], + }, + }; + } catch { + return { state: "error" }; + } +} diff --git a/scripts/proposals/catalog-retained-selection/retainedFieldTypes.ts b/scripts/proposals/catalog-retained-selection/retainedFieldTypes.ts new file mode 100644 index 000000000..27db4775c --- /dev/null +++ b/scripts/proposals/catalog-retained-selection/retainedFieldTypes.ts @@ -0,0 +1,44 @@ +import type { z } from "zod"; +import type { evaluateRetainedStreamField } from "../catalog-stream-fields/evaluate"; +import type { requestSchema, ActorScope, Selection } from "./schemas"; +type Evaluated = Extract, { state: "ready" }>; +export type Item = { + ref: Omit & { + authority: "context"; + kind: "recording"; + }; + binding: Selection; + value: { state: "known"; amount: number } | { state: "unknown" }; + periods: Evaluated["periods"]; + coverage: { + comparisonState: "comparable" | "incomplete"; + absentDateCount: number; + nullDateCount: number; + missingDateCount: number; + }; + source: { + provider: "luminate"; + metric: "daily_streams"; + platform: "all_dsps"; + territory: "worldwide"; + timezone: "UTC"; + retrievalRange: Evaluated["provenance"]["retrievalRange"]; + }; +}; +export type Ready = Omit, "mode" | "selections"> & { + state: "ready"; + contract: "catalog-retained-field-projection/1"; + asOf: string; + semantics: "historical_snapshot"; + items: Item[]; +}; +export type Result = Ready | { state: "unavailable" | "error" | "invalid" }; +/** Adapter MUST authenticate scope and pre-lock the complete sorted batch, keeping locks through callback evaluation. + * No implementation or grant is supplied. Injected test ports prove plumbing only. */ +export type LockedRead = ( + scope: ActorScope & { workspaceId: string; catalogId: string; selections: Selection[] }, + consume: (tx: { + asOf: string; + readBinding: (selection: Selection) => Promise; + }) => Promise, +) => Promise; diff --git a/scripts/proposals/catalog-retained-selection/schemas.ts b/scripts/proposals/catalog-retained-selection/schemas.ts new file mode 100644 index 000000000..ff178d475 --- /dev/null +++ b/scripts/proposals/catalog-retained-selection/schemas.ts @@ -0,0 +1,70 @@ +import { z } from "zod"; +const uuid = z.uuid().refine(value => value === value.toLowerCase()); +const hash = z.string().regex(/^[a-f0-9]{64}$/); +export const method = { + recipe: "catalog-retained-stream-fields/1", + recipeHash: "65a06a785aa496e1232fe3a9f1606595f19c1a152b710ca30c4c06612f01864a", + evaluatorSha256: "163563e15dd92d473aa51899c54c96300f20d27072b83e39ffb01c84f3adc64e", + helperSha256: "91dcca66c8dec0851c1f6e110257a63b730e9b7ae33a60bceb0e9ffcf318dd81", +} as const; +export const selectionSchema = z.strictObject({ + ownerId: uuid, + requestId: uuid, + subjectId: uuid, + catalogId: uuid, + resultId: uuid, + revision: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER), + topic: z.literal("catalog_retained_streams"), + sourceVersionIds: z.tuple([uuid]), +}); +export const commandScopeSchema = z.strictObject({ + actorId: uuid, + workspaceId: uuid, + requestId: uuid, + catalogId: uuid, + key: z.string().regex(/^[A-Za-z0-9_-]{1,100}$/), +}); +export const receiptSchema = z.strictObject({ + ownerId: uuid, + requestId: uuid, + subjectId: uuid, + catalogId: uuid, + resultId: uuid, + sourceVersionId: uuid, + revision: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER), + key: z.string().regex(/^[A-Za-z0-9_-]{1,100}$/), + commandDigest: hash, +}); +export const actorScopeSchema = z.strictObject({ + actorId: uuid, + sessionId: z.string().min(1).max(128), + chatId: z.string().min(1).max(128), + messageId: z.string().min(1).max(128), +}); +export const requestSchema = z + .strictObject({ + mode: z.literal("retained_field"), + workspaceId: uuid, + catalogId: uuid, + field: z.literal("current_streams"), + query: z.strictObject({ + since: z.iso.date(), + days: z.number().int().min(1).max(366), + timezone: z.literal("UTC"), + }), + method: z.strictObject({ + recipe: z.literal(method.recipe), + recipeHash: z.literal(method.recipeHash), + evaluatorSha256: z.literal(method.evaluatorSha256), + helperSha256: z.literal(method.helperSha256), + }), + selections: z.array(selectionSchema).min(1).max(8), + }) + .refine( + q => + q.selections.every(s => s.ownerId === q.workspaceId && s.catalogId === q.catalogId) && + new Set(q.selections.map(s => s.subjectId)).size === q.selections.length, + ); +export type Selection = z.infer; +export type CommandScope = z.infer; +export type ActorScope = z.infer; diff --git a/scripts/proposals/catalog-retained-selection/selection.test.ts b/scripts/proposals/catalog-retained-selection/selection.test.ts new file mode 100644 index 000000000..2ba2f597d --- /dev/null +++ b/scripts/proposals/catalog-retained-selection/selection.test.ts @@ -0,0 +1,273 @@ +import { describe, expect, it, vi } from "vitest"; +import { readFileSync } from "node:fs"; +import { createHash } from "node:crypto"; +import { readRetainedFieldSelection } from "./readRetainedFieldSelection"; +import { consumeRetainedField } from "./consumeRetainedField"; +import { method } from "./schemas"; +import { classifyRetainedFieldRefusal } from "./classifyRetainedFieldRefusal"; +import { evaluateRetainedStreamField } from "../catalog-stream-fields/evaluate"; +const id = (n: number) => `10000000-0000-4000-8000-${String(n).padStart(12, "0")}`; +const scope = { + actorId: id(9), + workspaceId: id(1), + requestId: id(2), + catalogId: id(3), + key: "original", +}; +const receipt = { + ownerId: id(1), + requestId: id(2), + subjectId: id(4), + catalogId: id(3), + key: "original", + commandDigest: "a".repeat(64), + resultId: id(5), + sourceVersionId: id(6), + revision: 7, +}; +const selection = { + ownerId: id(1), + requestId: id(2), + subjectId: id(4), + catalogId: id(3), + resultId: id(5), + revision: 7, + topic: "catalog_retained_streams", + sourceVersionIds: [id(6)], +}; +const query = { since: "2026-10-24", days: 1, timezone: "UTC" }; +const request = { + mode: "retained_field", + workspaceId: id(1), + catalogId: id(3), + field: "current_streams", + query, + method, + selections: [selection], +}; +const actor = { actorId: id(9), sessionId: "session", chatId: "chat", messageId: "assistant" }; +const envelope = { + ...selection, + evidence: { + schemaVersion: 1, + workspaceId: id(1), + catalogId: id(3), + subjectId: id(4), + isrc: "USAAA2600001", + provider: "luminate", + metric: "daily_streams", + platform: "all_dsps", + territory: "worldwide", + timezone: "UTC", + since: "2026-10-23", + until: "2026-10-25", + periods: { + previous: { start: "2026-10-23", end_exclusive: "2026-10-24" }, + current: { start: "2026-10-24", end_exclusive: "2026-10-25" }, + days: 1, + timezone: "UTC", + }, + providerRecordingId: "provider-original", + observations: [23, 24].map((d, i) => ({ + catalog_id: id(3), + isrc: "USAAA2600001", + date: `2026-10-${d}`, + run_id: id(7), + provider_recording_id: "provider-original", + streams: i ? 100 : 50, + source_hash: "a".repeat(64), + retrieved_at: "2026-10-25T01:00:00Z", + })), + absentDates: [], + nullDates: [], + retrievalRange: { earliest: "2026-10-25T01:00:00Z", latest: "2026-10-25T01:00:00Z" }, + }, +}; +const asOf = "2026-10-27T00:00:00Z"; +describe("genuine retained selection proposal", () => { + it("round trips reconciled identities through the real evaluator while transaction is held", async () => { + await expect( + JSON.stringify({ scope, actor, receipt, envelope, query, asOf }, null, 2) + "\n", + ).toMatchFileSnapshot("./fixtures/synthetic-authority-input.json"); + const reconcile = vi.fn(async () => receipt); + const selected = await readRetainedFieldSelection(scope, reconcile); + expect(reconcile).toHaveBeenCalledWith(scope); + expect(selected).toEqual({ state: "ready", selection }); + let held = false; + const result = await consumeRetainedField( + actor, + { ...request, selections: selected.state === "ready" ? [selected.selection] : [] }, + async (context, consume) => { + expect(context.actorId).toBe(id(9)); + expect(context.selections).toEqual([selection]); + held = true; + const value = await consume({ + asOf, + readBinding: async () => { + expect(held).toBe(true); + return envelope; + }, + }); + held = false; + return value; + }, + ); + expect(held).toBe(false); + expect(result.state).toBe("ready"); + if (result.state !== "ready") throw new Error("expected ready"); + const { state: _state, ...fieldContext } = result; + await expect(JSON.stringify(fieldContext, null, 2) + "\n").toMatchFileSnapshot( + "./fixtures/fieldContext.json", + ); + expect(result.items[0].binding).toEqual(selection); + expect(result.items[0].value).toEqual({ state: "known", amount: 100 }); + expect(result.items[0].ref.id).toBe(id(4)); + expect(JSON.stringify(result)).not.toContain("provider-original"); + expect(JSON.stringify(result)).not.toContain("observations"); + }); + it.each([ + null, + undefined, + {}, + { state: "pending" }, + { ...receipt, ownerId: id(9) }, + { ...receipt, requestId: id(8) }, + { ...receipt, catalogId: id(8) }, + { ...receipt, key: "new-key" }, + { ...receipt, extra: true }, + ])("does not manufacture selection from unavailable or mismatched receipt %j", async value => { + expect(await readRetainedFieldSelection(scope, async () => value)).toEqual({ + state: "unavailable", + }); + }); + it("defaults to unavailable and does not retry ambiguous transport errors", async () => { + expect(await readRetainedFieldSelection(scope)).toEqual({ state: "unavailable" }); + const reconcile = vi.fn(async () => { + throw new Error("unknown commit"); + }); + expect(await readRetainedFieldSelection(scope, reconcile)).toEqual({ state: "error" }); + expect(reconcile).toHaveBeenCalledTimes(1); + expect(await consumeRetainedField(actor, request)).toEqual({ state: "unavailable" }); + }); + it("withholds all projections when the authority read returns unavailable", async () => { + const result = await consumeRetainedField(actor, request, async (_, consume) => + consume({ asOf, readBinding: async () => null }), + ); + expect(result).toEqual({ state: "unavailable" }); + }); + it.each([ + { ...envelope, revision: 8 }, + { ...envelope, resultId: id(8) }, + { ...envelope, sourceVersionIds: [id(8)] }, + ])("does not silently upgrade exact binding", async value => { + expect( + await consumeRetainedField(actor, request, async (_, consume) => + consume({ asOf, readBinding: async () => value }), + ), + ).toEqual({ state: "unavailable" }); + }); + it.each([ + { ...request, mode: "report" }, + { ...request, evidence: envelope }, + { ...request, selections: [selection, selection] }, + { ...request, selections: Array(9).fill(selection) }, + { ...request, method: { ...method, evaluatorSha256: "b".repeat(64) } }, + { ...request, selections: [{ ...selection, ownerId: id(9) }] }, + ])("rejects invalid branch before authority call", async input => { + const transaction = vi.fn(); + expect(await consumeRetainedField(actor, input, transaction)).toEqual({ state: "invalid" }); + expect(transaction).not.toHaveBeenCalled(); + }); + it("withholds the whole batch if a later selection is unavailable", async () => { + const second = { ...selection, subjectId: id(8), resultId: id(10) }; + const result = await consumeRetainedField( + actor, + { ...request, selections: [selection, second] }, + async (_, consume) => + consume({ asOf, readBinding: async s => (s.subjectId === id(4) ? envelope : null) }), + ); + expect(result).toEqual({ state: "unavailable" }); + }); + it("preserves observed zero and makes incomplete values unknown", async () => { + const zero = structuredClone(envelope); + zero.evidence.observations[1].streams = 0; + const result = await consumeRetainedField(actor, request, async (_, consume) => + consume({ asOf, readBinding: async () => zero }), + ); + expect(result.state === "ready" && result.items[0].value).toEqual({ + state: "known", + amount: 0, + }); + const absent = structuredClone(envelope); + absent.evidence.observations.pop(); + absent.evidence.absentDates = ["2026-10-24"] as never[]; + const unknown = await consumeRetainedField(actor, request, async (_, consume) => + consume({ asOf, readBinding: async () => absent }), + ); + expect(unknown.state === "ready" && unknown.items[0].value).toEqual({ state: "unknown" }); + }); +}); + +it("pins the actual evaluated code, recipe and helper", () => { + for (const [file, hash] of [ + ["../catalog-stream-fields/evaluate.ts", method.evaluatorSha256], + ["../catalog-stream-fields/recipe.json", method.recipeHash], + ["../../../lib/catalog/compareSpotifyDailyStreams.ts", method.helperSha256], + ]) { + expect( + createHash("sha256") + .update(readFileSync(new URL(file, import.meta.url))) + .digest("hex"), + ).toBe(hash); + } +}); +it("redacts later transport failure after a successful row and never retries", async () => { + const second = { ...selection, subjectId: id(8), resultId: id(10) }; + const readBinding = vi.fn(async s => { + if (s.subjectId === id(4)) return envelope; + throw new Error("transport uncertain"); + }); + expect( + await consumeRetainedField( + actor, + { ...request, selections: [selection, second] }, + async (_, consume) => consume({ asOf, readBinding }), + ), + ).toEqual({ state: "error" }); + expect(readBinding).toHaveBeenCalledTimes(2); +}); + +it.each([270000, 400000])( + "classifies actual oversized UTF-8 authority evidence as invalid (%i characters)", + async characters => { + const oversized = structuredClone(envelope); + oversized.evidence.providerRecordingId = "é".repeat(characters); + const evidenceBytes = Buffer.byteLength(JSON.stringify(oversized.evidence), "utf8"); + const envelopeBytes = Buffer.byteLength(JSON.stringify(oversized), "utf8"); + expect(evidenceBytes).toBeGreaterThan(512 * 1024); + if (characters === 270000) expect(envelopeBytes).toBeLessThan(768 * 1024); + else expect(envelopeBytes).toBeGreaterThan(768 * 1024); + expect( + evaluateRetainedStreamField({ + operation: "read_field", + recipe: method.recipe, + field: "current_streams", + selection: { ...selection, query }, + asOf, + boundary: { state: "qualified", envelope: oversized }, + }), + ).toEqual({ state: "refused", reason: "size_limit" }); + expect( + await consumeRetainedField(actor, request, async (_, consume) => + consume({ asOf, readBinding: async () => oversized }), + ), + ).toEqual({ state: "invalid" }); + }, +); + +it.each(["helper_changed", "source_error"])( + "classifies evaluator operational refusal %s without details", + reason => { + expect(classifyRetainedFieldRefusal(reason)).toEqual({ state: "error" }); + }, +); diff --git a/scripts/proposals/catalog-retained-selection/tsconfig.json b/scripts/proposals/catalog-retained-selection/tsconfig.json new file mode 100644 index 000000000..4899580ae --- /dev/null +++ b/scripts/proposals/catalog-retained-selection/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../../../tsconfig.json", + "compilerOptions": { "incremental": false, "noEmit": true, "strict": true }, + "include": ["./*.ts", "../catalog-stream-fields/evaluate.ts", "../../../lib/catalog/compareSpotifyDailyStreams.ts"], + "exclude": ["node_modules"] +}