diff --git a/lib/const.ts b/lib/const.ts index cba8f5bcd..31b87c82a 100644 --- a/lib/const.ts +++ b/lib/const.ts @@ -162,3 +162,14 @@ export const packCandidateAdapterSettings = { inspectionTokens: 600, }, } as const; + +/** Quote amounts are customer charges; provider expenses are not measured by pack receipts. */ +export const packCostAccounting = { + amount_basis: "customer_charge", + included_services: "no_additional_customer_charge", + provider_cost_usd: null, + provider_cost_status: "not_measured", + concepts: "not_budgeted", + contingency: "not_budgeted", + refinements: "not_budgeted", +} as const; diff --git a/lib/content/packs/__tests__/packCostAccounting.test.ts b/lib/content/packs/__tests__/packCostAccounting.test.ts new file mode 100644 index 000000000..3a5c566d7 --- /dev/null +++ b/lib/content/packs/__tests__/packCostAccounting.test.ts @@ -0,0 +1,100 @@ +import { createHash } from "node:crypto"; +import { describe, expect, it, vi } from "vitest"; +import { buildReleasePackQuote } from "../buildReleasePackQuote"; +import { readPackSnapshot } from "../readPackSnapshot"; +import { assertPackQuoteConfiguration } from "../assertPackQuoteConfiguration"; +import { releasePackBriefSchema, releasePackDeliverablesSchema } from "../releasePackSchema"; +import type { PackSnapshot } from "../packTypes"; +vi.mock("@/lib/supabase/storage/createSignedFileUrlByKey", () => ({ + createSignedFileUrlByKey: vi.fn(), +})); +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({ + videos: 0, + artwork: 1, + quote_cards: 0, + carousels: 0, + visualizers: 0, +}); +const accounting = { + amount_basis: "customer_charge", + included_services: "no_additional_customer_charge", + provider_cost_usd: null, + provider_cost_status: "not_measured", + concepts: "not_budgeted", + contingency: "not_budgeted", + refinements: "not_budgeted", +}; +describe("pack quote cost accounting", () => { + it("labels house charges without representing included provider expense as zero", () => { + const quote = buildReleasePackQuote(brief, deliverables); + expect(quote).toHaveProperty("cost_accounting", accounting); + expect(quote.maximum_usd).toBe(0.01); + expect(quote.line_items.filter(item => item.included).every(item => item.usd === 0)).toBe(true); + expect(quote.assets[0].maximum_usd).toBe(0.01); + }); + it("preserves the production fingerprint and configuration for unchanged prices and scope", () => { + const quote = buildReleasePackQuote(brief, deliverables); + expect(quote.fingerprint).toBe( + createHash("sha256") + .update( + JSON.stringify({ + brief, + deliverables, + execution_config_digest: quote.execution_config_digest, + }), + ) + .digest("hex"), + ); + const legacy = JSON.parse(JSON.stringify(quote)); + delete legacy.cost_accounting; + expect(() => assertPackQuoteConfiguration(legacy)).not.toThrow(); + }); + it.each([false, true])( + "clarifies saved quote readback without rewriting approval (metadata present: %s)", + async present => { + const quote = { + ...buildReleasePackQuote(brief, deliverables), + id: "quote", + revision: 1, + expires_at: "2026-10-26T12:00:00Z", + }; + const saved = JSON.parse(JSON.stringify(quote)); + if (!present) delete saved.cost_accounting; + const snapshot: PackSnapshot = { + job: { + id: "job", + owner_id: "owner", + created_by: "owner", + revision: 1, + status: "awaiting_approval", + brief, + deliverables, + quote: saved, + held_credits: 0, + consumed_credits: 0, + workflow_run_id: null, + error: null, + }, + assets: [], + }; + const before = structuredClone(snapshot); + const result = await readPackSnapshot(snapshot); + expect(result.job.quote).toHaveProperty("cost_accounting", accounting); + expect(result.job.quote).toMatchObject({ + id: saved.id, + fingerprint: saved.fingerprint, + execution_config_digest: saved.execution_config_digest, + maximum_usd: saved.maximum_usd, + credits: saved.credits, + }); + expect(snapshot).toEqual(before); + }, + ); +}); diff --git a/lib/content/packs/__tests__/packFirstUse.test.ts b/lib/content/packs/__tests__/packFirstUse.test.ts index ec53fa717..32689c7d0 100644 --- a/lib/content/packs/__tests__/packFirstUse.test.ts +++ b/lib/content/packs/__tests__/packFirstUse.test.ts @@ -381,6 +381,30 @@ describe("new-buyer pack acceptance fixture", () => { expect(result.job.brief.reference_image_urls).toEqual([]); }, ); + it("labels a targeted retry quote without funding additional refinements", async () => { + const prepared = await prepareWorld({ + ...proposedWorld, + selection: { direction_id: "paper", selected_by: "customer", decision_note: "Selected" }, + }); + await processPackOperation("owner", "estimate", { job_id: prepared.job.id }); + const asset = fixture.saved.assets[0]; + asset.id = crypto.randomUUID(); + asset.status = "failed"; + const retried = (await processPackOperation("owner", "estimate", { + job_id: prepared.job.id, + asset_id: asset.id, + })) as any; + expect(retried.job.quote.retry_asset_id).toBe(asset.id); + expect(retried.job.quote.cost_accounting).toMatchObject({ + amount_basis: "customer_charge", + provider_cost_usd: null, + concepts: "not_budgeted", + contingency: "not_budgeted", + refinements: "not_budgeted", + }); + expect(retried.job.quote.maximum_usd).toBe(asset.maximum_usd); + expect(start).not.toHaveBeenCalled(); + }); it("prepares, quotes, runs and reads a reviewed song-backed video without an artist or repository", async () => { const prepared = (await processPackOperation("owner", "prepare", { idempotency_key: "first-use", @@ -405,6 +429,13 @@ describe("new-buyer pack acceptance fixture", () => { const estimated = (await processPackOperation("owner", "estimate", { job_id: prepared.job.id, })) as any; + expect(estimated.job.quote.cost_accounting).toMatchObject({ + amount_basis: "customer_charge", + provider_cost_usd: null, + refinements: "not_budgeted", + }); + // Existing compatible approvals remain usable even when stored before labels existed. + delete fixture.saved.job.quote.cost_accounting; await processPackOperation("owner", "generate", { job_id: prepared.job.id, quote_id: estimated.job.quote.id, @@ -413,6 +444,11 @@ describe("new-buyer pack acceptance fixture", () => { }); const read = (await processPackOperation("owner", "get", { job_id: prepared.job.id })) as any; expect(read.job.status).toBe("completed"); + expect(read.job.quote.cost_accounting.provider_cost_usd).toBeNull(); + const exported = (await processPackOperation("owner", "export", { + job_id: prepared.job.id, + })) as any; + expect(exported.job.quote.cost_accounting).toEqual(estimated.job.quote.cost_accounting); expect(read.assets[0].usable).toBe(true); expect(read.assets[0].caption).toContain("First Song"); expect(read.assets[0].files[0].url).toContain("signed=fixture"); diff --git a/lib/content/packs/buildReleasePackQuote.ts b/lib/content/packs/buildReleasePackQuote.ts index fee16078b..e9a17f33b 100644 --- a/lib/content/packs/buildReleasePackQuote.ts +++ b/lib/content/packs/buildReleasePackQuote.ts @@ -1,3 +1,4 @@ +import { packCostAccounting } from "@/lib/const"; import { getPackExecutionConfiguration } from "./getPackExecutionConfiguration"; import { getPackConfigurationDigest } from "./getPackConfigurationDigest"; import { createHash } from "node:crypto"; @@ -45,6 +46,7 @@ export function buildReleasePackQuote( 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 { + cost_accounting: { ...packCostAccounting }, pricing_version: execution_config.pricing_version, execution_config, execution_config_digest, diff --git a/lib/content/packs/packTypes.ts b/lib/content/packs/packTypes.ts index dd5fafb00..19da137a6 100644 --- a/lib/content/packs/packTypes.ts +++ b/lib/content/packs/packTypes.ts @@ -1,6 +1,8 @@ import type { ReleasePackBrief, ReleasePackDeliverables } from "./releasePackSchema"; import type { buildReleasePackQuote, PackAssetKind } from "./buildReleasePackQuote"; -export type PackQuote = ReturnType & { +export type PackQuote = Omit, "cost_accounting"> & { + /** Older saved quotes predate these explanatory labels. */ + cost_accounting?: ReturnType["cost_accounting"]; id: string; revision: number; expires_at: string; diff --git a/lib/content/packs/readPackSnapshot.ts b/lib/content/packs/readPackSnapshot.ts index 8dfd979fd..f02fd9c21 100644 --- a/lib/content/packs/readPackSnapshot.ts +++ b/lib/content/packs/readPackSnapshot.ts @@ -1,3 +1,4 @@ +import { packCostAccounting } from "@/lib/const"; import { getPackProgress } from "./getPackProgress"; import { isPackAssetUsable } from "./isPackAssetUsable"; import { createSignedFileUrlByKey } from "@/lib/supabase/storage/createSignedFileUrlByKey"; @@ -33,7 +34,11 @@ export async function readPackSnapshot(snapshot: PackSnapshot) { } const readiness = getPackBriefReadiness(snapshot.job.brief); return { - job, + job: { + ...job, + // Explain legacy amounts on read without rewriting stored approval or receipts. + quote: job.quote ? { ...job.quote, cost_accounting: { ...packCostAccounting } } : null, + }, assets, readiness, progress: getPackProgress(snapshot), diff --git a/lib/contracts/fixtures/published-docs.json b/lib/contracts/fixtures/published-docs.json index 080aa90cf..b429e82fb 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": "f790ee44d1b881709e393a65500bec23493e21a391c7b66555e0c78750f95239", + "content/docs/source/api-reference/openapi/content.json": "877afed1e0fb5c95afab8c5e294bae38fabdf48f50f226e0089b9ff50438870d", "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/mcp/tools/packs/index.ts b/lib/mcp/tools/packs/index.ts index cb5ecfa23..d91d47afc 100644 --- a/lib/mcp/tools/packs/index.ts +++ b/lib/mcp/tools/packs/index.ts @@ -20,7 +20,7 @@ export const releasePackToolManifest: Record = ], estimate: [ "estimate_release_pack", - "Quote every creative piece, captions, inspection and composition. Pass asset_id only for a confirmed failed asset's fresh retry quote. Does not spend.", + "Quote the customer charge ceiling for the selected pieces; captions, inspection and composition have no added customer fee. Provider expense is unmeasured, not zero. Concepts, contingency and refinements are not budgeted. Pass asset_id only for a confirmed failed asset's fresh retry quote. Does not spend.", ], generate: [ "generate_release_pack",