From 1c9140c19ef0b7472ac6c4b076c0d4f85f951690 Mon Sep 17 00:00:00 2001 From: Sidney Swift <158200036+sidneyswift@users.noreply.github.com> Date: Sat, 10 Oct 2026 16:08:14 -0400 Subject: [PATCH 1/2] Attribute evidence freshness in compiled Context briefs read_context_documents already returns each source version's retrievedAt, but the brief compiler dropped it, so a creative-direction or playlist-pitch brief could not show how current its evidence was. Summarize retrieval dates per selected document into an additive input_manifest.freshness object and end each document section of the brief text with a Retrieved line; source locations stay out of both. Keep compilerVersion at context-brief-v1 and the manifest document entries unchanged because the deployed save_context_brief function pins that version and compares the manifest exactly. Add a two-song same-artist checkpoint fixture that drives the shared operation for brief, save_brief and read_brief. Refs recoupable/app#2130 Co-Authored-By: Claude Fable 5.1 --- lib/context/README.md | 7 +- .../__tests__/compileContextBrief.test.ts | 31 ++ .../__tests__/twoSongBriefCheckpoint.test.ts | 264 ++++++++++++++++++ lib/context/compileContextBrief.ts | 23 +- lib/context/selectContextDocuments.ts | 2 + lib/context/summarizeContextBriefFreshness.ts | 25 ++ 6 files changed, 344 insertions(+), 8 deletions(-) create mode 100644 lib/context/__tests__/twoSongBriefCheckpoint.test.ts create mode 100644 lib/context/summarizeContextBriefFreshness.ts diff --git a/lib/context/README.md b/lib/context/README.md index 3771d8eca..7a182b757 100644 --- a/lib/context/README.md +++ b/lib/context/README.md @@ -81,7 +81,12 @@ and `readiness`, and adds bounded Markdown `text`, `text_characters`, `request_i versions. `max_characters` independently bounds evidence JSON and rendered text; it is not a cap on the entire response including metadata. Documents are omitted whole to fit the limit; evidence is never cut mid-claim. A missing topic may mean -absent, ineligible or over-budget evidence. +absent, ineligible or over-budget evidence. `input_manifest.freshness` reports the +oldest and newest source retrieval timestamps, the documents without a retrieval +date, and each selected document's newest `retrievedAt`; every document section in +`text` ends with a `Retrieved:` line (timestamp or `unknown`) and never a source +location. `compilerVersion` remains `context-brief-v1` because `save_context_brief` +pins that value and compares the document manifest exactly; freshness is additive. These are attributed evidence briefs for downstream work, not generated creative proposals or finished outreach. `brief` is read-only and recompilation can reflect diff --git a/lib/context/__tests__/compileContextBrief.test.ts b/lib/context/__tests__/compileContextBrief.test.ts index 2391e9508..546196d8c 100644 --- a/lib/context/__tests__/compileContextBrief.test.ts +++ b/lib/context/__tests__/compileContextBrief.test.ts @@ -98,3 +98,34 @@ it("excludes unrelated workspace evidence and creative proposals", () => { expect(brief.text).not.toMatch(/PRIVATE|PROPOSAL/); expect(brief.documents).toEqual([]); }); + +it("reports unknown freshness without failing when source rows are absent or undated", () => { + const brief = compileContextBrief({ + ...input, + purpose: "playlist_pitch", + documents: [ + document("song-a", "song_summary"), + { ...document("song-a", "catalog_metadata"), sources: [] }, + { + ...document("artist", "artist_research"), + sources: [{ versionId: "artist:artist_research:source", retrievedAt: "yesterday" }], + }, + ], + }); + expect(brief.input_manifest.compilerVersion).toBe("context-brief-v1"); + expect(brief.input_manifest.freshness).toEqual({ + oldestRetrievedAt: null, + newestRetrievedAt: null, + documentsWithoutRetrievalDate: [ + "song-a:catalog_metadata", + "song-a:song_summary", + "artist:artist_research", + ], + documents: [ + { documentId: "song-a:catalog_metadata", retrievedAt: null }, + { documentId: "song-a:song_summary", retrievedAt: null }, + { documentId: "artist:artist_research", retrievedAt: null }, + ], + }); + expect(brief.text.match(/Retrieved: unknown/g)).toHaveLength(3); +}); diff --git a/lib/context/__tests__/twoSongBriefCheckpoint.test.ts b/lib/context/__tests__/twoSongBriefCheckpoint.test.ts new file mode 100644 index 000000000..b94c0550e --- /dev/null +++ b/lib/context/__tests__/twoSongBriefCheckpoint.test.ts @@ -0,0 +1,264 @@ +import { expect, it, vi } from "vitest"; +import { processContextOperation } from "../processContextOperation"; +import type { ContextBriefDocument } from "../selectContextDocuments"; +import type { compileContextBrief } from "../compileContextBrief"; + +type Brief = ReturnType; + +vi.mock("../authorizeContextOwner", () => ({ authorizeContextOwner: vi.fn() })); + +// Fixture identifiers only; no production, customer or provider IDs. +const owner = "11111111-1111-4111-8111-111111111111"; +const otherWorkspace = "99999999-9999-4999-8999-999999999999"; +const requestA = "22222222-2222-4222-8222-22222222222a"; +const requestB = "22222222-2222-4222-8222-22222222222b"; +const requestMaterial = "22222222-2222-4222-8222-22222222222c"; +const songA = "33333333-3333-4333-8333-33333333333a"; +const songB = "33333333-3333-4333-8333-33333333333b"; +const artist = "44444444-4444-4444-8444-444444444444"; +const material = "55555555-5555-4555-8555-555555555555"; +const retrieved = { + artistResearch: "2026-09-20T09:00:00.000Z", + songA: "2026-09-21T10:00:00.000Z", + songBOlder: "2026-09-19T08:00:00.000Z", + songB: "2026-09-22T11:30:00.000Z", +}; +const requestSubjects: Record = { + [requestA]: [songA, artist], + [requestB]: [songB, artist], + [requestMaterial]: [material], +}; + +const document = ( + subjectId: string, + topic: string, + retrievedAt: (string | null)[], + overrides: Partial = {}, +): ContextBriefDocument => { + const id = `${subjectId.slice(-4)}:${topic}`; + return { + id, + resultId: `${id}:result`, + ownerId: owner, + subjectId, + topic, + version: 2, + status: "accepted", + evidenceKind: "observation", + text: `${id} evidence`, + sourceVersionIds: retrievedAt.map((_, index) => `${id}:source-${index}`), + coverage: "full", + // Source locations are private; the brief text and manifest must never repeat them. + sources: retrievedAt.map((value, index) => ({ + versionId: `${id}:source-${index}`, + url: `https://private-source.example.test/${id}/${index}`, + retrievedAt: value, + })), + ...overrides, + }; +}; + +const workspace: ContextBriefDocument[] = [ + document(songA, "song_summary", [retrieved.songA]), + document(songA, "lyrics", [retrieved.songA], { + coverage: "partial", + text: "LYRICS-A preview transcript", + }), + document(songA, "artwork_branding", [retrieved.songA], { text: "ARTWORK-A observed palette" }), + document(songA, "recording_metadata", [retrieved.songA]), + document(songA, "release_metadata", [retrieved.songA]), + document(songA, "catalog_metadata", [retrieved.songA], { text: "CATALOG-A audio features" }), + document(songB, "song_summary", [retrieved.songB]), + document(songB, "artwork_branding", [retrieved.songB], { text: "ARTWORK-B observed palette" }), + document(songB, "recording_metadata", [retrieved.songB]), + document(songB, "release_metadata", [retrieved.songBOlder, retrieved.songB]), + document(songB, "catalog_metadata", [retrieved.songB], { text: "CATALOG-B audio features" }), + document(artist, "artist_research", [retrieved.artistResearch]), + document(artist, "artist_metadata", [null]), + // Newer withdrawn revision must not displace or leak into the accepted artwork document. + document(songA, "artwork_branding", [retrieved.songA], { + id: "withdrawn:artwork", + resultId: "withdrawn:artwork:result", + version: 3, + status: "withdrawn", + text: "WITHDRAWN artwork observation", + }), + document(material, "material_input", [retrieved.songA], { + evidenceKind: "customer_assertion", + coverage: "partial", + text: "PRIVATE supporting text with internal campaign notes", + }), + document(songA, "lyrics", [retrieved.songA], { + id: "other:lyrics", + ownerId: otherWorkspace, + text: "OTHER-WORKSPACE lyrics", + }), +]; + +function createDeps() { + const snapshots = new Map(); + const rpc = vi.fn(async (name: string, params: Record) => { + expect(params.p_owner).toBe(owner); + const requestId = String(params.p_request); + if (name === "read_context_request") + return { + id: requestId, + owner_id: owner, + status: requestId === requestA ? "partial" : "completed", + output: { subjectIds: requestSubjects[requestId] }, + }; + // The RPC filters by workspace and subject; the compiler re-checks status and owner. + if (name === "read_context_documents") + return workspace.filter(doc => requestSubjects[requestId].includes(doc.subjectId)); + if (name === "save_context_brief") { + const id = `66666666-6666-4666-8666-${String(params.p_key).length.toString().padStart(12, "0")}`; + if (!snapshots.has(id)) + snapshots.set(id, { + id, + state: "saved", + superseded: false, + purpose: (params.p_snapshot as { purpose: string }).purpose, + brief: params.p_snapshot, + }); + return snapshots.get(id); + } + if (name === "read_context_brief") return snapshots.get(String(params.p_brief)) ?? null; + throw new Error(`Unexpected RPC ${name}`); + }); + return { + rpc, + dispatch: vi.fn(), + authorize: vi.fn(async () => ({ ownerId: owner, accountId: owner, organizationId: null })), + }; +} + +const compile = (purpose: "creative_direction" | "playlist_pitch", deps = createDeps()) => + processContextOperation( + owner, + { + action: "brief", + request_id: requestA, + additional_request_ids: [requestB, requestMaterial, requestA], + purpose, + }, + deps, + ) as Promise; + +it("reuses shared artist evidence once while keeping per-song documents separate", async () => { + const deps = createDeps(); + const creative = await compile("creative_direction", deps); + expect(deps.dispatch).not.toHaveBeenCalled(); + expect(deps.rpc.mock.calls.filter(([name]) => name === "read_context_request")).toHaveLength(3); + expect(creative.request_ids).toEqual([requestA, requestB, requestMaterial]); + expect(creative.documents.filter(doc => doc.subjectId === artist)).toEqual([ + expect.objectContaining({ topic: "artist_research" }), + expect.objectContaining({ topic: "artist_metadata" }), + ]); + for (const topic of ["recording_metadata", "release_metadata", "artwork_branding"]) + expect(creative.documents.filter(doc => doc.topic === topic).map(doc => doc.subjectId)).toEqual( + [songA, songB], + ); + expect(creative.text).toContain("ARTWORK-A observed palette"); + expect(creative.text).toContain("ARTWORK-B observed palette"); + expect(creative.text).toContain("LYRICS-A preview transcript"); +}); + +it("keeps each brief task-specific and honest about lyric coverage", async () => { + const creative = await compile("creative_direction"); + const pitch = await compile("playlist_pitch"); + expect(pitch.text).toContain("Playlist-pitch brief"); + expect(pitch.text).not.toMatch(/LYRICS-A|ARTWORK-A|ARTWORK-B/); + expect(pitch.text).toContain("CATALOG-A audio features"); + expect(pitch.text).toContain("CATALOG-B audio features"); + expect(creative.text).not.toMatch(/CATALOG-A|CATALOG-B/); + expect(creative.input_manifest.excludedTopics).toEqual(["catalog_metadata", "material_input"]); + expect(pitch.input_manifest.excludedTopics).toEqual([ + "artwork_branding", + "lyrics", + "material_input", + ]); + expect(creative.readiness).toBe("partial"); + expect(pitch.readiness).toBe("partial"); + expect(creative.gaps).toContainEqual({ + requestId: requestA, + topic: "lyrics", + subjectId: songA, + status: "partial", + reason: expect.any(String), + }); + expect(creative.gaps).toContainEqual({ + requestId: requestB, + topic: "lyrics", + subjectId: null, + status: "unavailable", + reason: expect.any(String), + }); + expect(pitch.gaps.some(gap => gap.topic === "lyrics")).toBe(false); +}); + +it("keeps private assertions, withdrawn evidence, other workspaces and source locations out", async () => { + for (const purpose of ["creative_direction", "playlist_pitch"] as const) { + const brief = await compile(purpose); + expect(brief.text).not.toMatch(/PRIVATE|WITHDRAWN|OTHER-WORKSPACE/); + expect(brief.documents.map(doc => doc.id)).not.toContain("withdrawn:artwork"); + expect(brief.documents.some(doc => doc.evidenceKind === "customer_assertion")).toBe(false); + expect(brief.text).not.toContain("example.test"); + expect(JSON.stringify(brief.input_manifest)).not.toContain("example.test"); + } +}); + +it("attributes evidence freshness without changing the snapshot-compatible manifest shape", async () => { + const brief = await compile("creative_direction"); + expect(brief.input_manifest.compilerVersion).toBe("context-brief-v1"); + for (const entry of brief.input_manifest.documents) + expect(Object.keys(entry).sort()).toEqual([ + "documentId", + "resultId", + "sourceVersionIds", + "subjectId", + "topic", + "version", + ]); + expect(brief.input_manifest.freshness).toMatchObject({ + oldestRetrievedAt: retrieved.artistResearch, + newestRetrievedAt: retrieved.songB, + documentsWithoutRetrievalDate: ["4444:artist_metadata"], + }); + expect(brief.input_manifest.freshness.documents).toContainEqual({ + documentId: "333b:release_metadata", + retrievedAt: retrieved.songB, + }); + expect(brief.input_manifest.freshness.documents).toContainEqual({ + documentId: "4444:artist_metadata", + retrievedAt: null, + }); + expect(brief.input_manifest.freshness.documents).toHaveLength(brief.documents.length); + expect(brief.text).toContain(`Retrieved: ${retrieved.songA}`); + expect(brief.text).toContain("Retrieved: unknown"); +}); + +it("saves and reopens identical snapshots for both consumers without dispatch", async () => { + const deps = createDeps(); + for (const purpose of ["creative_direction", "playlist_pitch"] as const) { + const compiled = await compile(purpose, deps); + const saved = (await processContextOperation( + owner, + { + action: "save_brief", + request_id: requestA, + additional_request_ids: [requestB, requestMaterial], + purpose, + idempotency_key: `${purpose}-v1`, + }, + deps, + )) as { snapshot: { id: string; brief: unknown } }; + expect(saved.snapshot.brief).toEqual(compiled); + const read = await processContextOperation( + owner, + { action: "read_brief", brief_id: saved.snapshot.id }, + deps, + ); + expect(read).toEqual(saved); + } + expect(deps.dispatch).not.toHaveBeenCalled(); +}); diff --git a/lib/context/compileContextBrief.ts b/lib/context/compileContextBrief.ts index 27c51d489..a1c5b0e13 100644 --- a/lib/context/compileContextBrief.ts +++ b/lib/context/compileContextBrief.ts @@ -1,4 +1,5 @@ import { selectContextDocuments, type ContextBriefDocument } from "./selectContextDocuments"; +import { summarizeContextBriefFreshness } from "./summarizeContextBriefFreshness"; const recipes = { company_onboarding: { @@ -75,16 +76,22 @@ export function compileContextBrief(input: { withdrawnSourceVersionIds: [], }); const documents = [...selection.documents]; + const retrievedAt = new Map( + summarizeContextBriefFreshness(documents).documents.map(entry => [ + entry.documentId, + entry.retrievedAt ?? "unknown", + ]), + ); const render = () => header + documents - .map( - doc => - `\n## ${doc.topic.replaceAll("_", " ")} — ${doc.subjectId}\nCoverage: ${doc.coverage}; evidence: ${doc.evidenceKind}\n\n${doc.text - .split("\n") - .map(line => `> ${line}`) - .join("\n")}\n\nSources: ${doc.sourceVersionIds.join(", ")}\n`, - ) + .map(doc => { + const quoted = doc.text + .split("\n") + .map(line => `> ${line}`) + .join("\n"); + return `\n## ${doc.topic.replaceAll("_", " ")} — ${doc.subjectId}\nCoverage: ${doc.coverage}; evidence: ${doc.evidenceKind}\n\n${quoted}\n\nSources: ${doc.sourceVersionIds.join(", ")}\nRetrieved: ${retrievedAt.get(doc.id)}\n`; + }) .join(""); // Keep complete evidence and attribution together; never cut a document mid-claim. let text = render(); @@ -155,6 +162,7 @@ export function compileContextBrief(input: { guidance, request_coverage: requestCoverage, input_manifest: { + // save_context_brief pins this version and compares `documents` exactly; keep both stable. compilerVersion: "context-brief-v1", method: "saved_evidence_selection", requestIds: input.requests.map(request => request.id), @@ -166,6 +174,7 @@ export function compileContextBrief(input: { version: doc.version, sourceVersionIds: doc.sourceVersionIds, })), + freshness: summarizeContextBriefFreshness(documents), excludedTopics: [ ...new Set( input.documents diff --git a/lib/context/selectContextDocuments.ts b/lib/context/selectContextDocuments.ts index 7bf1e456e..db9875a62 100644 --- a/lib/context/selectContextDocuments.ts +++ b/lib/context/selectContextDocuments.ts @@ -15,6 +15,8 @@ export interface ContextBriefDocument { text: string; sourceVersionIds: string[]; coverage: "full" | "partial" | "unknown" | "unavailable"; + /** Source rows as read_context_documents returns them; locations stay out of brief text and manifests. */ + sources?: { versionId: string; url?: string | null; retrievedAt?: string | null }[]; } export interface ContextBriefSelection { diff --git a/lib/context/summarizeContextBriefFreshness.ts b/lib/context/summarizeContextBriefFreshness.ts new file mode 100644 index 000000000..6c5efda01 --- /dev/null +++ b/lib/context/summarizeContextBriefFreshness.ts @@ -0,0 +1,25 @@ +import type { ContextBriefDocument } from "./selectContextDocuments"; + +function latestRetrievedAt(doc: ContextBriefDocument) { + const times = (doc.sources ?? []) + .map(source => (source.retrievedAt ? Date.parse(source.retrievedAt) : Number.NaN)) + .filter(time => Number.isFinite(time)); + return times.length ? new Date(Math.max(...times)).toISOString() : null; +} + +/** Summarize when selected evidence was retrieved; source locations never enter the summary. */ +export function summarizeContextBriefFreshness(documents: ContextBriefDocument[]) { + const perDocument = documents.map(doc => ({ + documentId: doc.id, + retrievedAt: latestRetrievedAt(doc), + })); + const dated = perDocument.flatMap(entry => (entry.retrievedAt ? [entry.retrievedAt] : [])); + return { + oldestRetrievedAt: dated.length ? dated.reduce((a, b) => (a < b ? a : b)) : null, + newestRetrievedAt: dated.length ? dated.reduce((a, b) => (a > b ? a : b)) : null, + documentsWithoutRetrievalDate: perDocument + .filter(entry => !entry.retrievedAt) + .map(entry => entry.documentId), + documents: perDocument, + }; +} From 393dcb91737f442e746285fe588901e932494d61 Mon Sep 17 00:00:00 2001 From: Sidney Swift <158200036+sidneyswift@users.noreply.github.com> Date: Sat, 10 Oct 2026 17:35:39 -0400 Subject: [PATCH 2/2] Bound brief freshness by every source; rotate Sites brief key Compiled briefs now carry freshness lines and a manifest freshness object, so a Sites save_brief replay of a pre-deploy brief-v3 key would hit save_context_brief's "Brief key already used for different output" conflict on the concepts -> generate path and on every retry. Rotate the suffix to brief-v4 and lock the exact keys in the Sites key test. Freshness previously took each document's newest source, which masked an older source on multi-source evidence. Overall and per-document bounds now span every dated source, and the Retrieved line shows a range when a document's sources differ. The checkpoint test that only proves topic scoping is renamed so it no longer claims a private-input rule. Co-Authored-By: Claude Fable 5.1 --- lib/context/README.md | 10 +++--- .../__tests__/compileContextBrief.test.ts | 6 ++-- .../__tests__/twoSongBriefCheckpoint.test.ts | 16 ++++++--- lib/context/compileContextBrief.ts | 4 ++- lib/context/summarizeContextBriefFreshness.ts | 34 ++++++++++++------- lib/sites/__tests__/albumContextKeys.test.ts | 5 +++ lib/sites/production/saveSiteContextBrief.ts | 4 ++- 7 files changed, 52 insertions(+), 27 deletions(-) diff --git a/lib/context/README.md b/lib/context/README.md index 7a182b757..a3413071e 100644 --- a/lib/context/README.md +++ b/lib/context/README.md @@ -82,10 +82,12 @@ versions. `max_characters` independently bounds evidence JSON and rendered text; it is not a cap on the entire response including metadata. Documents are omitted whole to fit the limit; evidence is never cut mid-claim. A missing topic may mean absent, ineligible or over-budget evidence. `input_manifest.freshness` reports the -oldest and newest source retrieval timestamps, the documents without a retrieval -date, and each selected document's newest `retrievedAt`; every document section in -`text` ends with a `Retrieved:` line (timestamp or `unknown`) and never a source -location. `compilerVersion` remains `context-brief-v1` because `save_context_brief` +oldest and newest retrieval timestamps across every dated source of the selected +documents, the documents with no dated source, and each selected document's own +`oldestRetrievedAt`/`newestRetrievedAt`; every document section in `text` ends with a +`Retrieved:` line (one timestamp, ` to ` when its sources differ, or +`unknown`) and never a source location. Freshness is reported, not enforced: no age +limit drops evidence yet. `compilerVersion` remains `context-brief-v1` because `save_context_brief` pins that value and compares the document manifest exactly; freshness is additive. These are attributed evidence briefs for downstream work, not generated creative diff --git a/lib/context/__tests__/compileContextBrief.test.ts b/lib/context/__tests__/compileContextBrief.test.ts index 546196d8c..bc9b998b8 100644 --- a/lib/context/__tests__/compileContextBrief.test.ts +++ b/lib/context/__tests__/compileContextBrief.test.ts @@ -122,9 +122,9 @@ it("reports unknown freshness without failing when source rows are absent or und "artist:artist_research", ], documents: [ - { documentId: "song-a:catalog_metadata", retrievedAt: null }, - { documentId: "song-a:song_summary", retrievedAt: null }, - { documentId: "artist:artist_research", retrievedAt: null }, + { documentId: "song-a:catalog_metadata", oldestRetrievedAt: null, newestRetrievedAt: null }, + { documentId: "song-a:song_summary", oldestRetrievedAt: null, newestRetrievedAt: null }, + { documentId: "artist:artist_research", oldestRetrievedAt: null, newestRetrievedAt: null }, ], }); expect(brief.text.match(/Retrieved: unknown/g)).toHaveLength(3); diff --git a/lib/context/__tests__/twoSongBriefCheckpoint.test.ts b/lib/context/__tests__/twoSongBriefCheckpoint.test.ts index b94c0550e..37b9edd5a 100644 --- a/lib/context/__tests__/twoSongBriefCheckpoint.test.ts +++ b/lib/context/__tests__/twoSongBriefCheckpoint.test.ts @@ -196,7 +196,9 @@ it("keeps each brief task-specific and honest about lyric coverage", async () => expect(pitch.gaps.some(gap => gap.topic === "lyrics")).toBe(false); }); -it("keeps private assertions, withdrawn evidence, other workspaces and source locations out", async () => { +// material_input is outside both recipes, so this proves topic scoping, not an evidence-kind +// private-input rule; a customer_assertion on a recipe topic would still be selected. +it("keeps out-of-recipe material, withdrawn evidence, other workspaces and source locations out", async () => { for (const purpose of ["creative_direction", "playlist_pitch"] as const) { const brief = await compile(purpose); expect(brief.text).not.toMatch(/PRIVATE|WITHDRAWN|OTHER-WORKSPACE/); @@ -219,21 +221,25 @@ it("attributes evidence freshness without changing the snapshot-compatible manif "topic", "version", ]); + // The oldest bound comes from any selected source, not from each document's newest one. expect(brief.input_manifest.freshness).toMatchObject({ - oldestRetrievedAt: retrieved.artistResearch, + oldestRetrievedAt: retrieved.songBOlder, newestRetrievedAt: retrieved.songB, documentsWithoutRetrievalDate: ["4444:artist_metadata"], }); expect(brief.input_manifest.freshness.documents).toContainEqual({ documentId: "333b:release_metadata", - retrievedAt: retrieved.songB, + oldestRetrievedAt: retrieved.songBOlder, + newestRetrievedAt: retrieved.songB, }); expect(brief.input_manifest.freshness.documents).toContainEqual({ documentId: "4444:artist_metadata", - retrievedAt: null, + oldestRetrievedAt: null, + newestRetrievedAt: null, }); expect(brief.input_manifest.freshness.documents).toHaveLength(brief.documents.length); - expect(brief.text).toContain(`Retrieved: ${retrieved.songA}`); + expect(brief.text).toContain(`Retrieved: ${retrieved.songA}\n`); + expect(brief.text).toContain(`Retrieved: ${retrieved.songBOlder} to ${retrieved.songB}\n`); expect(brief.text).toContain("Retrieved: unknown"); }); diff --git a/lib/context/compileContextBrief.ts b/lib/context/compileContextBrief.ts index a1c5b0e13..7e51d078f 100644 --- a/lib/context/compileContextBrief.ts +++ b/lib/context/compileContextBrief.ts @@ -79,7 +79,9 @@ export function compileContextBrief(input: { const retrievedAt = new Map( summarizeContextBriefFreshness(documents).documents.map(entry => [ entry.documentId, - entry.retrievedAt ?? "unknown", + entry.oldestRetrievedAt === entry.newestRetrievedAt + ? (entry.newestRetrievedAt ?? "unknown") + : `${entry.oldestRetrievedAt} to ${entry.newestRetrievedAt}`, ]), ); const render = () => diff --git a/lib/context/summarizeContextBriefFreshness.ts b/lib/context/summarizeContextBriefFreshness.ts index 6c5efda01..7d249b75a 100644 --- a/lib/context/summarizeContextBriefFreshness.ts +++ b/lib/context/summarizeContextBriefFreshness.ts @@ -1,24 +1,32 @@ import type { ContextBriefDocument } from "./selectContextDocuments"; -function latestRetrievedAt(doc: ContextBriefDocument) { - const times = (doc.sources ?? []) - .map(source => (source.retrievedAt ? Date.parse(source.retrievedAt) : Number.NaN)) - .filter(time => Number.isFinite(time)); - return times.length ? new Date(Math.max(...times)).toISOString() : null; +function retrievalTimes(doc: ContextBriefDocument) { + return (doc.sources ?? []).flatMap(source => { + const time = source.retrievedAt ? Date.parse(source.retrievedAt) : Number.NaN; + return Number.isFinite(time) ? [time] : []; + }); } +const bound = (times: number[], pick: (...values: number[]) => number) => + times.length ? new Date(pick(...times)).toISOString() : null; + /** Summarize when selected evidence was retrieved; source locations never enter the summary. */ export function summarizeContextBriefFreshness(documents: ContextBriefDocument[]) { - const perDocument = documents.map(doc => ({ - documentId: doc.id, - retrievedAt: latestRetrievedAt(doc), - })); - const dated = perDocument.flatMap(entry => (entry.retrievedAt ? [entry.retrievedAt] : [])); + const perDocument = documents.map(doc => { + const times = retrievalTimes(doc); + return { + documentId: doc.id, + oldestRetrievedAt: bound(times, Math.min), + newestRetrievedAt: bound(times, Math.max), + }; + }); + // Bound by every dated source so an older source on a multi-source document is not masked. + const allTimes = documents.flatMap(retrievalTimes); return { - oldestRetrievedAt: dated.length ? dated.reduce((a, b) => (a < b ? a : b)) : null, - newestRetrievedAt: dated.length ? dated.reduce((a, b) => (a > b ? a : b)) : null, + oldestRetrievedAt: bound(allTimes, Math.min), + newestRetrievedAt: bound(allTimes, Math.max), documentsWithoutRetrievalDate: perDocument - .filter(entry => !entry.retrievedAt) + .filter(entry => !entry.newestRetrievedAt) .map(entry => entry.documentId), documents: perDocument, }; diff --git a/lib/sites/__tests__/albumContextKeys.test.ts b/lib/sites/__tests__/albumContextKeys.test.ts index 4cb5b1d2e..9f75ec7bc 100644 --- a/lib/sites/__tests__/albumContextKeys.test.ts +++ b/lib/sites/__tests__/albumContextKeys.test.ts @@ -55,4 +55,9 @@ it("saves distinct briefs for different tracks in the same site revision", async >); const keys = m.operation.mock.calls.slice(-2).map(call => call[1].idempotency_key); expect(keys[0]).not.toBe(keys[1]); + // v4: compiled briefs gained freshness lines, so pre-deploy brief-v3 keys must not be replayed. + expect(keys).toEqual([ + `sites:site:1:${requestId}:brief-v4`, + `sites:site:1:${otherRequest}:brief-v4`, + ]); }); diff --git a/lib/sites/production/saveSiteContextBrief.ts b/lib/sites/production/saveSiteContextBrief.ts index 46664718b..67819b6f7 100644 --- a/lib/sites/production/saveSiteContextBrief.ts +++ b/lib/sites/production/saveSiteContextBrief.ts @@ -19,7 +19,9 @@ export async function saveSiteContextBrief( organization_id: site.owner_id, purpose: "creative_direction", max_characters: 32000, - idempotency_key: `sites:${site.id}:${site.revision}:${context.requestId}:brief-v3`, + // save_context_brief rejects a reused key with different output: bump the suffix + // whenever compiled brief output changes (v4: evidence freshness lines and manifest). + idempotency_key: `sites:${site.id}:${site.revision}:${context.requestId}:brief-v4`, }, { rpc: callContextRpc, dispatch: runStoredContextRequest }, );