Skip to content

Commit 4dcc34e

Browse files
Merge upstream/main (3ee042d) into vps2-f2a
upstream advanced a4f774b -> 3ee042d (#2271 prompt-cache TTL, #2269 checkpoint restore when the saved commit is missing, #2300 condense), which made this PR dirty. One content conflict in src/core/webview/ClineProvider.ts: kept this branch's ProfileActivationInconsistentError / ProfileActivationSnapshot / ProfileActivationLanded declarations and kept upstream's new removeTaskEventListeners helper - both sides retained, nothing selected away. Local verification: tsc --noEmit clean in src (junction node_modules -> pr1311-wt, @roo-code/types -> this branch's own packages/types); eslint-suppressions.json had no per-file count increase. Committed with --no-verify because the husky/lint-staged hook cannot run in this worktree: the junction'd install predates @roo-code/config-eslint's ./i18n-keys export (ERR_PACKAGE_PATH_NOT_EXPORTED) - a local tooling gap, not a code finding.
2 parents bcc03b2 + 3ee042d commit 4dcc34e

97 files changed

Lines changed: 4342 additions & 570 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// npx vitest run src/__tests__/cache-write-ttl-pricing.test.ts
2+
3+
import { describe, it, expect } from "vitest"
4+
5+
import { anthropicModels } from "../providers/anthropic.js"
6+
import { bedrockModels } from "../providers/bedrock.js"
7+
import { openAiNativeModels } from "../providers/openai.js"
8+
import {
9+
CACHE_WRITE_MULTIPLIER_12H,
10+
CACHE_WRITE_MULTIPLIER_1H,
11+
deriveCacheWritesPrice12h,
12+
deriveCacheWritesPrice1h,
13+
type ModelInfo,
14+
} from "../model.js"
15+
16+
/**
17+
* Catalog invariant for the TTL accounting program (ADR-0006 phase 1).
18+
*
19+
* A model that advertises prompt caching on a provider which can report a
20+
* long-lived cache write must carry the matching per-TTL write rate. Without it
21+
* the cost layer can only fall back to the aggregate write rate, which
22+
* under-reports every long-lived write (37.5% for Anthropic 1h, 50% for OpenAI 12h).
23+
*/
24+
describe("cache-write TTL pricing catalog invariant", () => {
25+
const anthropicFamilyEntries = Object.entries(anthropicModels as Record<string, ModelInfo>)
26+
27+
const bedrockAnthropicEntries = Object.entries(bedrockModels as Record<string, ModelInfo>).filter(([id]) =>
28+
id.startsWith("anthropic."),
29+
)
30+
31+
const openAiLongLivedEntries = Object.entries(openAiNativeModels as Record<string, ModelInfo>).filter(
32+
([id]) => id.startsWith("gpt-5.6") || id.startsWith("gpt-6"),
33+
)
34+
35+
it("every Anthropic catalog entry with prompt caching defines cacheWritesPrice1h", () => {
36+
const missing = anthropicFamilyEntries
37+
.filter(([, info]) => info.supportsPromptCache && info.cacheWritesPrice1h === undefined)
38+
.map(([id]) => id)
39+
40+
expect(missing).toEqual([])
41+
})
42+
43+
it("every Bedrock Anthropic-family entry with prompt caching defines cacheWritesPrice1h", () => {
44+
const missing = bedrockAnthropicEntries
45+
.filter(([, info]) => info.supportsPromptCache && info.cacheWritesPrice1h === undefined)
46+
.map(([id]) => id)
47+
48+
expect(missing).toEqual([])
49+
})
50+
51+
it("every OpenAI 5.6/6-class entry defines cacheWritesPrice12h", () => {
52+
expect(openAiLongLivedEntries.length).toBeGreaterThan(0)
53+
54+
const missing = openAiLongLivedEntries
55+
.filter(([, info]) => info.supportsPromptCache && info.cacheWritesPrice12h === undefined)
56+
.map(([id]) => id)
57+
58+
expect(missing).toEqual([])
59+
})
60+
61+
it("Anthropic 1h rates match the documented 2x input-price relationship", () => {
62+
for (const [id, info] of anthropicFamilyEntries) {
63+
if (info.cacheWritesPrice1h === undefined || info.inputPrice === undefined) continue
64+
expect(info.cacheWritesPrice1h, id).toBeCloseTo(info.inputPrice * CACHE_WRITE_MULTIPLIER_1H, 9)
65+
}
66+
})
67+
68+
it("OpenAI 12h rates match the documented 2.5x input-price relationship", () => {
69+
for (const [id, info] of openAiLongLivedEntries) {
70+
if (info.cacheWritesPrice12h === undefined || info.inputPrice === undefined) continue
71+
expect(info.cacheWritesPrice12h, id).toBeCloseTo(info.inputPrice * CACHE_WRITE_MULTIPLIER_12H, 9)
72+
}
73+
})
74+
75+
it("deriveCacheWritesPrice1h scales inputPrice by 2x and stays undefined without one", () => {
76+
expect(deriveCacheWritesPrice1h(3.0)).toBe(6.0)
77+
expect(deriveCacheWritesPrice1h(undefined)).toBeUndefined()
78+
})
79+
80+
it("deriveCacheWritesPrice12h scales inputPrice by 2.5x and stays undefined without one", () => {
81+
expect(deriveCacheWritesPrice12h(4.0)).toBe(10.0)
82+
expect(deriveCacheWritesPrice12h(undefined)).toBeUndefined()
83+
})
84+
})

‎packages/types/src/__tests__/openai-models.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ describe("OpenAI native models", () => {
122122
inputPrice: 2,
123123
outputPrice: 10,
124124
cacheWritesPrice: 2.5,
125+
cacheWritesPrice12h: 5,
125126
cacheReadsPrice: 0.2,
126127
longContextPricing: {
127128
thresholdTokens: 272_000,
@@ -166,6 +167,7 @@ describe("OpenAI native models", () => {
166167
inputPrice: 0.1,
167168
outputPrice: 0.5,
168169
cacheWritesPrice: 0.125,
170+
cacheWritesPrice12h: 0.25,
169171
cacheReadsPrice: 0.01,
170172
longContextPricing: {
171173
thresholdTokens: 272_000,
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// npx vitest run src/__tests__/prompt-cache-ttl.test.ts
2+
3+
import { describe, it, expect } from "vitest"
4+
5+
import { anthropicModels, anthropicProviderDefinition } from "../providers/anthropic.js"
6+
import { vertexModels } from "../providers/vertex.js"
7+
import { openRouterDefaultModelInfo } from "../providers/openrouter.js"
8+
import {
9+
vercelAiGatewayDefaultModelId,
10+
VERCEL_AI_GATEWAY_PROMPT_CACHING_MODELS,
11+
} from "../providers/vercel-ai-gateway.js"
12+
import {
13+
DEFAULT_PROMPT_CACHE_TTL,
14+
modelInfoSchema,
15+
promptCacheTtlSchema,
16+
promptCacheTtlValues,
17+
type ModelInfo,
18+
} from "../model.js"
19+
import { baseProviderSettingsShape } from "../providers/common.js"
20+
import { providerIdentifiers } from "../provider-identifiers.js"
21+
import { globalSettingsSchema } from "../global-settings.js"
22+
23+
/**
24+
* Catalog invariant for the request-side TTL knob (ADR-0006 phase 2, PR-C5b).
25+
*
26+
* Emission is gated by `ModelInfo.promptCacheTtlSupport`, so a model that can
27+
* report a 1h cache write but carries no capability array would silently never
28+
* emit the TTL the user asked for. These tests pin both directions: capable
29+
* catalogs advertise it, and catalogs that cannot know stay absent (fail closed).
30+
*/
31+
describe("prompt cache TTL schema", () => {
32+
it("accepts exactly the 5m and 1h classes", () => {
33+
expect(promptCacheTtlValues).toEqual(["5m", "1h"])
34+
expect(promptCacheTtlSchema.parse("5m")).toBe("5m")
35+
expect(promptCacheTtlSchema.parse("1h")).toBe("1h")
36+
expect(promptCacheTtlSchema.safeParse("30m").success).toBe(false)
37+
expect(promptCacheTtlSchema.safeParse("12h").success).toBe(false)
38+
})
39+
40+
it("defaults to undefined so an unset knob emits no ttl key", () => {
41+
expect(DEFAULT_PROMPT_CACHE_TTL).toBeUndefined()
42+
})
43+
44+
it("is optional on ModelInfo", () => {
45+
const parsed = modelInfoSchema.parse({ contextWindow: 200_000, supportsPromptCache: true })
46+
expect(parsed.promptCacheTtlSupport).toBeUndefined()
47+
48+
const withCapability = modelInfoSchema.parse({
49+
contextWindow: 200_000,
50+
supportsPromptCache: true,
51+
promptCacheTtlSupport: ["5m", "1h"],
52+
})
53+
expect(withCapability.promptCacheTtlSupport).toEqual(["5m", "1h"])
54+
})
55+
})
56+
57+
describe("prompt cache TTL knob location", () => {
58+
it("lives in provider settings so handlers can see it", () => {
59+
// ApiHandlerOptions = Omit<ProviderSettings, "apiProvider">, so only a
60+
// provider-settings key reaches a handler (ADR-0006 decision item 2).
61+
expect(baseProviderSettingsShape.promptCacheTtl).toBeDefined()
62+
63+
const parsed = anthropicProviderDefinition.schema.safeParse({
64+
apiProvider: providerIdentifiers.anthropic,
65+
promptCacheTtl: "1h",
66+
})
67+
expect(parsed.success).toBe(true)
68+
expect(parsed.success && parsed.data.promptCacheTtl).toBe("1h")
69+
})
70+
71+
it("is not a global setting", () => {
72+
const shape = globalSettingsSchema.shape as Record<string, unknown>
73+
expect(shape.promptCacheTtl).toBeUndefined()
74+
})
75+
})
76+
77+
describe("prompt cache TTL capability catalog", () => {
78+
it("every Anthropic entry with prompt caching advertises both TTL classes", () => {
79+
const missing = Object.entries(anthropicModels as Record<string, ModelInfo>)
80+
.filter(([, info]) => info.supportsPromptCache && !info.promptCacheTtlSupport?.includes("1h"))
81+
.map(([id]) => id)
82+
83+
expect(missing).toEqual([])
84+
})
85+
86+
it("leaves the Vertex catalog without capability so it fails closed", () => {
87+
// PR-C5b scopes the catalog capability to the Anthropic entries; the Vertex
88+
// catalog is populated separately, so its models stay 5m-only for now.
89+
const withCapability = Object.entries(vertexModels as Record<string, ModelInfo>)
90+
.filter(([, info]) => info.promptCacheTtlSupport !== undefined)
91+
.map(([id]) => id)
92+
93+
expect(withCapability).toEqual([])
94+
})
95+
96+
it("leaves dynamic-catalog defaults without capability so they fail closed", () => {
97+
// OpenRouter and the gateway catalogs are fetched at runtime and carry no TTL
98+
// capability data, which ADR-0006 maps to "5m only" (invariant I2).
99+
expect(openRouterDefaultModelInfo.promptCacheTtlSupport).toBeUndefined()
100+
expect(VERCEL_AI_GATEWAY_PROMPT_CACHING_MODELS.has(vercelAiGatewayDefaultModelId)).toBe(true)
101+
})
102+
})

‎packages/types/src/model.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ export type ModelParameter = z.infer<typeof modelParametersSchema>
9595
export const isModelParameter = (value: string): value is ModelParameter =>
9696
modelParameters.includes(value as ModelParameter)
9797

98+
/**
99+
* Prompt cache TTL classes a request-side cache marker can carry. `"5m"` is the
100+
* vendor default and is never emitted explicitly, so only `"1h"` changes the wire
101+
* shape and an unset knob stays byte-identical to a pre-knob build (ADR-0006 I3).
102+
*/
103+
export const promptCacheTtlValues = ["5m", "1h"] as const
104+
105+
export const promptCacheTtlSchema = z.enum(promptCacheTtlValues)
106+
107+
export type PromptCacheTtl = z.infer<typeof promptCacheTtlSchema>
108+
109+
/** Deliberately `undefined`, not `"5m"`: emitting `ttl: "5m"` would change every request's bytes. */
110+
export const DEFAULT_PROMPT_CACHE_TTL: PromptCacheTtl | undefined = undefined
111+
98112
/**
99113
* ModelInfo
100114
*/
@@ -112,6 +126,8 @@ export const modelInfoSchema = z.object({
112126
// When set to "24h", extended prompt caching will be requested; when omitted
113127
// or set to "in_memory", the default in‑memory cache is used.
114128
promptCacheRetention: z.enum(["in_memory", "24h"]).optional(),
129+
// Cache TTLs this model accepts. Absent means "5m only" (ADR-0006 I2, fail closed).
130+
promptCacheTtlSupport: z.array(promptCacheTtlSchema).optional(),
115131
// Capability flag to indicate whether the model supports an output verbosity parameter
116132
supportsVerbosity: z.boolean().optional(),
117133
// Capability flag to indicate whether the model exposes a user-configurable max output
@@ -136,6 +152,13 @@ export const modelInfoSchema = z.object({
136152
inputPrice: z.number().optional(),
137153
outputPrice: z.number().optional(),
138154
cacheWritesPrice: z.number().optional(),
155+
// Per-TTL cache-write rates. Vendors bill a longer cache lifetime at a higher
156+
// write rate than the default 5m/30m class, so a single `cacheWritesPrice`
157+
// under-reports whenever the response reports a long-lived write. Omitted
158+
// means "no long-lived writes are expected on this model"; the cost layer
159+
// derives a fail-closed rate from `inputPrice` if one still arrives.
160+
cacheWritesPrice1h: z.number().optional(),
161+
cacheWritesPrice12h: z.number().optional(),
139162
cacheReadsPrice: z.number().optional(),
140163
longContextPricing: z
141164
.object({
@@ -180,6 +203,11 @@ export const modelInfoSchema = z.object({
180203
inputPrice: z.number().optional(),
181204
outputPrice: z.number().optional(),
182205
cacheWritesPrice: z.number().optional(),
206+
// Per-TTL write rates for this tier. A tier that overrides inputPrice
207+
// must also override these, otherwise a long-context or flex/priority
208+
// write bills at the base tier's TTL rate.
209+
cacheWritesPrice1h: z.number().optional(),
210+
cacheWritesPrice12h: z.number().optional(),
183211
cacheReadsPrice: z.number().optional(),
184212
}),
185213
)
@@ -188,6 +216,34 @@ export const modelInfoSchema = z.object({
188216

189217
export type ModelInfo = z.infer<typeof modelInfoSchema>
190218

219+
/**
220+
* Multipliers vendors apply to the base input price for a cache write, by TTL class.
221+
* Anthropic: 5m = 1.25x, 1h = 2x. OpenAI: 30m = 1.25x, 12h = 2.5x.
222+
*/
223+
export const CACHE_WRITE_MULTIPLIER_5M = 1.25
224+
export const CACHE_WRITE_MULTIPLIER_1H = 2.0
225+
export const CACHE_WRITE_MULTIPLIER_12H = 2.5
226+
227+
/**
228+
* Derive the 1h cache-write rate from the base input price.
229+
*
230+
* Stated once here so every Anthropic-family catalog entry and every runtime
231+
* fallback agrees on the 2x relationship. Returns `undefined` when the model
232+
* has no input price to scale, which keeps the cost layer fail-closed: it will
233+
* fall back to `cacheWritesPrice` rather than invent a rate.
234+
*/
235+
export function deriveCacheWritesPrice1h(inputPrice: number | undefined): number | undefined {
236+
return inputPrice === undefined ? undefined : inputPrice * CACHE_WRITE_MULTIPLIER_1H
237+
}
238+
239+
/**
240+
* Derive the 12h cache-write rate from the base input price (OpenAI 12h class).
241+
* Same fail-closed contract as {@link deriveCacheWritesPrice1h}.
242+
*/
243+
export function deriveCacheWritesPrice12h(inputPrice: number | undefined): number | undefined {
244+
return inputPrice === undefined ? undefined : inputPrice * CACHE_WRITE_MULTIPLIER_12H
245+
}
246+
191247
export type ModelRecord = Record<string, ModelInfo>
192248

193249
export type RouterModels = Record<DynamicProvider | LocalProvider, ModelRecord>

0 commit comments

Comments
 (0)