Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONSTANT Variant = "BasDropsDefault"
SPECIFICATION Spec
INVARIANTS TypeOK OmitWhenUnset ExplicitZeroKept CustomKept ReasoningBudgetNeutral DeclaredDefaultKept
CHECK_DEADLOCK FALSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONSTANT Variant = "BedrockRetainedDefault"
SPECIFICATION Spec
INVARIANTS TypeOK OmitWhenUnset ExplicitZeroKept CustomKept ReasoningBudgetNeutral DeclaredDefaultKept
CHECK_DEADLOCK FALSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONSTANT Variant = "DeepSeekRetainedDefault"
SPECIFICATION Spec
INVARIANTS TypeOK OmitWhenUnset ExplicitZeroKept CustomKept ReasoningBudgetNeutral DeclaredDefaultKept
CHECK_DEADLOCK FALSE
4 changes: 4 additions & 0 deletions docs/architecture/tla/TemperatureOmission.LegacyDefault.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONSTANT Variant = "LegacyDefault"
SPECIFICATION Spec
INVARIANTS TypeOK OmitWhenUnset ExplicitZeroKept CustomKept ReasoningBudgetNeutral DeclaredDefaultKept
CHECK_DEADLOCK FALSE
4 changes: 4 additions & 0 deletions docs/architecture/tla/TemperatureOmission.LegacyTruthy.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONSTANT Variant = "LegacyTruthy"
SPECIFICATION Spec
INVARIANTS TypeOK OmitWhenUnset ExplicitZeroKept CustomKept ReasoningBudgetNeutral DeclaredDefaultKept
CHECK_DEADLOCK FALSE
4 changes: 4 additions & 0 deletions docs/architecture/tla/TemperatureOmission.NullPasses.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONSTANT Variant = "NullPasses"
SPECIFICATION Spec
INVARIANTS TypeOK OmitWhenUnset ExplicitZeroKept CustomKept ReasoningBudgetNeutral DeclaredDefaultKept
CHECK_DEADLOCK FALSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONSTANT Variant = "OpenAiDropsCustom"
SPECIFICATION Spec
INVARIANTS TypeOK OmitWhenUnset ExplicitZeroKept CustomKept ReasoningBudgetNeutral DeclaredDefaultKept
CHECK_DEADLOCK FALSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONSTANT Variant = "OpenAiDropsZero"
SPECIFICATION Spec
INVARIANTS TypeOK OmitWhenUnset ExplicitZeroKept CustomKept ReasoningBudgetNeutral DeclaredDefaultKept
CHECK_DEADLOCK FALSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONSTANT Variant = "RawUnderThinking"
SPECIFICATION Spec
INVARIANTS TypeOK OmitWhenUnset ExplicitZeroKept CustomKept ReasoningBudgetNeutral DeclaredDefaultKept
CHECK_DEADLOCK FALSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONSTANT Variant = "XaiRetainedDefault"
SPECIFICATION Spec
INVARIANTS TypeOK OmitWhenUnset ExplicitZeroKept CustomKept ReasoningBudgetNeutral DeclaredDefaultKept
CHECK_DEADLOCK FALSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONSTANT Variant = "ZaiThinkingOmits"
SPECIFICATION Spec
INVARIANTS TypeOK OmitWhenUnset ExplicitZeroKept CustomKept ReasoningBudgetNeutral DeclaredDefaultKept
CHECK_DEADLOCK FALSE
4 changes: 4 additions & 0 deletions docs/architecture/tla/TemperatureOmission.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONSTANT Variant = "Fixed"
SPECIFICATION Spec
INVARIANTS TypeOK OmitWhenUnset ExplicitZeroKept CustomKept ReasoningBudgetNeutral DeclaredDefaultKept
CHECK_DEADLOCK FALSE
197 changes: 197 additions & 0 deletions docs/architecture/tla/TemperatureOmission.tla
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
---- MODULE TemperatureOmission ----
\* Source: TemperatureControl.onChange stores null when "use custom temperature" is
\* off (modelTemperature is nullish). A number, zero included, is a custom value.
\* Sinks: createStream in BaseOpenAiCompatibleProvider; createMessage and
\* completePrompt in LiteLlmHandler, NativeOllamaHandler, OpenAiNativeHandler,
\* MistralHandler, LmStudioHandler, VercelAiGatewayHandler, ZooGatewayHandler,
\* KenariHandler, NanoGptHandler, FriendliHandler, ZaiHandler, GeminiHandler, and
\* the two non-adaptive paths of AwsBedrockHandler. Each writes (or omits) one
\* temperature field in the request body.
\* Exception: a reasoning format that requires temperature (DeepSeek reasoner in
\* openai.ts PR 247, ollama useR1Format) sends its default when no custom value is set.
\* Review ratchet: PR 2650 round 1 (comment 4658152524) removed Bedrock from the
\* sink list as not covered, because the constructor retained a default. Round 2
\* (comment 4658222935) restored Bedrock as an omission sink after the fix: the
\* constructor keeps a nullish setting nullish and both non-adaptive request
\* paths omit the key. The adaptive-thinking paths omit unconditionally and stay
\* outside this spec as a capability gate.
\* Round 3 (inline 4658434316, codecov 7080405454, mutation report on
\* f29c88b4e): three test-layer findings. The vertex completePrompt matchers
\* accepted a config that still carried temperature. Nine changed lines in six
\* sinks lacked focused completePrompt coverage. Fifteen mutants survived on the
\* changed guards. Covered: the invariant above already models every sink, so
\* the fixes land in the property-test file. The vertex exact-assertion fix and
\* the positive and capability-gate completePrompt arms are those tests. The
\* supportsTemperature gates stay a recorded gap (Not modeled below), pinned by
\* direct unit tests.
\* Round 4 (edelauna review, thread 4688025467 on bedrock.ts and the review body
\* "also address this in Openai, XAI, and deepseek"): two findings, two added.
\* (a) The Bedrock omission line sent the raw setting while a budget_tokens
\* thinking request was on. Before PR 2650 that path sent the getModelParams
\* result, which forces 1.0 for a reasoning-budget model (model-params.ts; the
\* in-tree note says Anthropic rejects a temperature change under extended
\* thinking). Added: the thinking dimension and invariant I4. I2 and I3 now hold
\* for a non-thinking request only. Decision: the thinking case omits the key
\* (same shape as the adaptive branch and the DeepSeek thinking gate); it does
\* not send 1.0, because the service fixes that value and an omitted key cannot
\* be rejected. OpenAiHandler keeps the getModelParams route: a custom value is
\* its getModel() result, which is the neutral value for a hybrid model.
\* (b) OpenAiHandler, XAIHandler and DeepSeekHandler were not omission sinks.
\* XAI and DeepSeek wrote the default (0) when the setting was nullish, and the
\* non-streaming branch of OpenAiHandler.createMessage never wrote a custom
\* value. Added: the provider dimension; I1, I2 and I3 range over each sink.
\* Covered: 0. Added: 2. Gap: 1 (the supportsTemperature gates below).
\* Round 5 (merge CI on 4ad50bf93: main's new fixture specs pin provider
\* defaults the merge tree dropped): baseten.spec.ts pins 0.5 for a plain
\* request and zai.spec.ts pins the reasoning default for GLM thinking. The
\* merge had stripped the declared defaults, so the request carried no
\* temperature at all. Added: the baseten sink with its declared default and
\* the zai thinking path that keeps the reasoning default; invariant
\* DeclaredDefaultKept. OmitWhenUnset now scopes to a non-thinking request
\* without a declared default; a custom value still goes out under thinking
\* for zai (GLM accepts it). Covered: 0. Added: 1. Gap: 1 (the
\* supportsTemperature gates below).
\* Round 6 (CR 6191828039, inline 4801992024 on openai.spec.ts): the direct
\* unit test pinning the openai supportsTemperature gate set no custom value,
\* so the omission held without the guard and guard removal was invisible.
\* Covered: 0. Added: 0. Gap: 1 (unchanged: the gate stays outside the model
\* by design; its pin is the strengthened unit test, which now sends a custom
\* 0.7 on both stream branches).
\* Failure: the user turns custom temperature off and a provider still writes a
\* temperature field (often 0), or overwrites an explicit 0, or writes null; or
\* a budget-thinking request carries a non-neutral temperature; or a provider
\* that declares a default for the path loses it and the request omits the
\* field.
\* Invariant: no temperature field when the value is nullish and the model is not a
\* reasoner format, on a non-thinking request without a declared default. An
\* explicit 0 and any custom number are sent unchanged on a non-thinking
\* request. A thinking request sends no temperature (Bedrock, DeepSeek) or the
\* neutral value that getModelParams forces (OpenAiHandler) or the reasoning
\* default (zai GLM thinking), never a raw wrong value. A declared default
\* (baseten) is sent when the value is nullish.
\* Expect: LegacyDefault, LegacyTruthy, NullPasses, BedrockRetainedDefault,
\* XaiRetainedDefault and DeepSeekRetainedDefault fail on unset or zero input;
\* OpenAiDropsZero fails I2, OpenAiDropsCustom fails I3, RawUnderThinking fails
\* I4, BasDropsDefault and ZaiThinkingOmits fail I5. Fixed holds. The provider
\* constants are oracles.
\* BedrockRetainedDefault reproduces the pre-fix Bedrock shape (constructor
\* clobber plus a non-adaptive send of the default) that round 2 removed.
\* XaiRetainedDefault and DeepSeekRetainedDefault reproduce the round-4 pre-fix
\* shapes: an unconditional send of the getModelParams default. OpenAiDropsZero
\* and OpenAiDropsCustom reproduce the non-streaming branch that never wrote the
\* field. RawUnderThinking reproduces the bedrock.ts:520 shape of round 2.
\* Cost: each TLC run takes less than 1 second. Agent work takes about 3 hours
\* per round.
\* Result: Fixed holds in 72 states (36 reachable inputs). Every sabotage cfg
\* fails the invariant named above. No false alarm was retained.
\* Gate: Fixed must pass. LegacyDefault, LegacyTruthy, NullPasses,
\* BedrockRetainedDefault, XaiRetainedDefault, DeepSeekRetainedDefault,
\* OpenAiDropsZero, OpenAiDropsCustom, RawUnderThinking, BasDropsDefault and
\* ZaiThinkingOmits must fail.
\* Property test: src/api/providers/__tests__/temperature-omission.spec.ts.
\* Not modeled: supportsTemperature capability gates, Bedrock adaptive-thinking
\* paths, maxTokens and message fields. The thinking dimension models
\* AwsBedrockHandler.createMessage only: its completePrompt sends no thinking
\* field, so a custom value stays valid there. OpenAiHandler.completePrompt sends
\* no temperature at all (I1 holds; I2 and I3 are a recorded gap for it).

EXTENDS TLC
CONSTANT Variant
VARIABLES input, phase, sent

Providers == {"bedrock", "openai", "xai", "deepseek", "zai", "baseten"}
Settings == {"nullish", "zero", "custom"}

\* Reachable inputs. An R1 reasoner format is an OpenAI-compatible or DeepSeek
\* model id, never a Bedrock, xAI, zai or baseten model. A reasoning budget is
\* a Bedrock budget_tokens request, an OpenAI-compatible hybrid model, or a
\* DeepSeek V4 thinking request; xAI has no such model. An R1 reasoner is not a
\* budget model. Baseten sends plain chat requests, never thinking ones.
Reachable(i) == (i.provider \in {"bedrock", "xai", "zai", "baseten"} => ~i.reasoner)
/\ (i.provider = "xai" => ~i.thinking)
/\ (i.provider = "baseten" => ~i.thinking)
/\ (i.reasoner => ~i.thinking)

Inputs == {i \in [setting : Settings, reasoner : BOOLEAN, thinking : BOOLEAN, provider : Providers] : Reachable(i)}

\* The field written into one request body. "neutral" is the 1.0 that
\* getModelParams forces for a reasoning-budget model.
SentValues == {"absent", "null", "default", "zero", "custom", "neutral"}

\* Fixed is the rule of round 5. A reasoner format keeps its default when the
\* value is nullish. A declared provider default (baseten) is sent when the
\* value is nullish. A zai thinking request sends the reasoning default, any
\* other thinking request omits the field (or sends the neutral OpenAI value).
\* Otherwise the setting is sent as is.
FixedValue(s, r, t, p) ==
IF r /\ s = "nullish" THEN "default"
ELSE IF s # "nullish" /\ t /\ p = "zai" THEN s
ELSE IF s = "nullish" /\ p = "baseten" THEN "default"
ELSE IF s = "nullish" /\ p = "zai" /\ t THEN "neutral"
ELSE IF s = "nullish" THEN "absent"
ELSE IF t /\ p = "openai" THEN "neutral"
ELSE IF t THEN "absent"
ELSE s

\* Each Variant models one unfixed assembly shape seen in the provider files.
\* The legacy shapes and the retained-default shapes are scoped to a
\* non-thinking request, so each fails only the invariant the header names.
AssembleValue(s, r, t, p) ==
IF Variant = "LegacyDefault" /\ s = "nullish" /\ ~t THEN "default"
ELSE IF Variant = "LegacyTruthy" /\ s \in {"nullish", "zero"} /\ ~t THEN "default"
ELSE IF Variant = "NullPasses" /\ s = "nullish" /\ ~t THEN "null"
ELSE IF Variant = "BedrockRetainedDefault" /\ s = "nullish" /\ p = "bedrock"
THEN (IF t THEN "neutral" ELSE "default")
ELSE IF Variant = "XaiRetainedDefault" /\ s = "nullish" /\ p = "xai" THEN "default"
ELSE IF Variant = "DeepSeekRetainedDefault" /\ s = "nullish" /\ p = "deepseek" /\ ~t /\ ~r
THEN "default"
ELSE IF Variant = "OpenAiDropsZero" /\ s = "zero" /\ p = "openai" /\ ~t THEN "absent"
ELSE IF Variant = "OpenAiDropsCustom" /\ s = "custom" /\ p = "openai" /\ ~t THEN "absent"
ELSE IF Variant = "RawUnderThinking" /\ t /\ s \in {"zero", "custom"} THEN s
ELSE IF Variant = "BasDropsDefault" /\ s = "nullish" /\ p = "baseten" THEN "absent"
ELSE IF Variant = "ZaiThinkingOmits" /\ s = "nullish" /\ p = "zai" /\ t THEN "absent"
ELSE FixedValue(s, r, t, p)

Init == /\ input \in Inputs
/\ phase = "source"
/\ sent = "absent"

Assemble == /\ phase = "source"
/\ sent' = AssembleValue(input.setting, input.reasoner, input.thinking, input.provider)
/\ phase' = "assembled"
/\ UNCHANGED input

Next == Assemble
Spec == Init /\ [][Next]_<<input, phase, sent>>

TypeOK == /\ input \in Inputs
/\ phase \in {"source", "assembled"}
/\ sent \in SentValues

\* A provider that declares a default for the modeled path: baseten on every
\* request. zai's reasoning default is thinking-only, so it stays out of this
\* predicate and I1 keeps covering the zai plain path.
DeclaresDefault(p) == p = "baseten"

\* I1: unset custom temperature sends no temperature field (F1, F3) in every
\* sink, on a non-thinking request without a declared default.
OmitWhenUnset == phase = "assembled" /\ input.setting = "nullish" /\ ~input.reasoner
/\ ~input.thinking /\ ~DeclaresDefault(input.provider) => sent = "absent"
\* I2: an explicit 0 is sent as 0 on a non-thinking request (F2).
ExplicitZeroKept == phase = "assembled" /\ input.setting = "zero" /\ ~input.thinking => sent = "zero"
\* I3: a custom number is sent unchanged on a non-thinking request.
CustomKept == phase = "assembled" /\ input.setting = "custom" /\ ~input.thinking => sent = "custom"
\* I4: a budget-thinking request never carries the setting; it omits the field
\* or sends the neutral value (round 4, thread 4688025467). zai GLM thinking
\* accepts a custom temperature (main pins a default there), so its custom
\* value goes out and only its default is pinned by I5.
ReasoningBudgetNeutral == phase = "assembled" /\ input.thinking /\ input.provider # "zai"
=> sent \in {"absent", "neutral"}
\* I5: a provider that declares a default for the path sends it when the value
\* is nullish (round 5: main's fixture specs pin baseten 0.5 and the zai GLM
\* reasoning default; the merge tree dropped both).
DeclaredDefaultKept ==
(phase = "assembled" /\ input.setting = "nullish") =>
/\ (input.provider = "baseten" => sent = "default")
/\ ((input.provider = "zai" /\ input.thinking) => sent = "neutral")
====
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,14 @@ describe("BaseOpenAiCompatibleProvider", () => {
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
model: "test-model",
temperature: 0,
messages: expect.arrayContaining([{ role: "system", content: systemPrompt }]),
stream: true,
stream_options: { include_usage: true },
}),
undefined,
)
// No custom temperature: the request must omit the field (#242).
expect(mockCreate.mock.calls[0][0]).not.toHaveProperty("temperature")
})

it("should yield usage data from stream", async () => {
Expand Down
30 changes: 20 additions & 10 deletions src/api/providers/__tests__/bedrock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2039,14 +2039,18 @@ describe("AwsBedrockHandler", () => {
expect(commandArg.inferenceConfig?.temperature).toBeUndefined()
})

it("should still send temperature and budget_tokens thinking for older Claude Opus 4.6", async () => {
it("should send budget_tokens thinking and omit temperature for older Claude Opus 4.6", async () => {
// Regression guard: the adaptive-thinking branch must NOT activate for 4.6 or earlier.
// PR 2650 round 4 (thread 4688025467): budget_tokens thinking rejects a
// temperature change, so the request omits the key even when a custom
// value is set; before PR 2650 this path sent the getModelParams 1.0.
const opus46Handler = new AwsBedrockHandler({
apiModelId: "anthropic.claude-opus-4-6-v1",
awsAccessKey: "test-access-key",
awsSecretKey: "test-secret-key",
awsRegion: "us-east-1",
enableReasoningEffort: true,
modelTemperature: 0.5,
})

const generator = opus46Handler.createMessage("System prompt", messages)
Expand All @@ -2058,8 +2062,8 @@ describe("AwsBedrockHandler", () => {
// 4.6 still uses the budget_tokens-based thinking format.
expect(commandArg.additionalModelRequestFields?.thinking?.type).toBe("enabled")
expect(commandArg.additionalModelRequestFields?.thinking?.budget_tokens).toBeGreaterThan(0)
// 4.6 still accepts temperature.
expect(commandArg.inferenceConfig?.temperature).toBeDefined()
// Thinking on: no temperature key at all (not the raw 0.5, not a forced 1.0).
expect("temperature" in (commandArg.inferenceConfig ?? {})).toBe(false)
})

it("should detect adaptive-thinking models via cross-region inference prefix (us.anthropic.claude-opus-4-8)", async () => {
Expand Down Expand Up @@ -2136,23 +2140,25 @@ describe("AwsBedrockHandler", () => {

it("completePrompt should still send temperature for older Claude Opus 4.6 (non-stream path)", async () => {
// 4.6 and earlier still accept sampling parameters, so completePrompt must
// continue to send temperature for them.
// keep sending an explicit custom temperature for them. #242: an unset
// value omits the key (covered in temperature-omission.spec.ts).
const mockConverseCommand = vi.mocked(ConverseCommand)

const opus46Handler = new AwsBedrockHandler({
apiModelId: "anthropic.claude-opus-4-6-v1",
awsAccessKey: "test-access-key",
awsSecretKey: "test-secret-key",
awsRegion: "us-east-1",
modelTemperature: 0.5,
})

await opus46Handler.completePrompt("Test prompt")

expect(mockConverseCommand).toHaveBeenCalled()
const commandArg = mockConverseCommand.mock.calls[0][0] as any

// 4.6 must still receive temperature.
expect(commandArg.inferenceConfig?.temperature).toBeDefined()
// 4.6 must still receive the explicit custom temperature.
expect(commandArg.inferenceConfig?.temperature).toBe(0.5)
})

describe("custom ARN base model", () => {
Expand Down Expand Up @@ -2210,16 +2216,18 @@ describe("AwsBedrockHandler", () => {
})
})

it("keeps budget thinking and temperature for a Sonnet 4.5 base model", async () => {
it("keeps budget thinking and omits temperature for a Sonnet 4.5 base model", async () => {
const input = await sendFirstRequest(
createCustomArnHandler({
awsCustomArnBaseModelId: "anthropic.claude-sonnet-4-5-20250929-v1:0",
enableReasoningEffort: true,
// PR 2650 round 4: a custom value is omitted while thinking is on.
modelTemperature: 0.5,
}),
)

expect(input).toMatchObject({ additionalModelRequestFields: { thinking: { type: "enabled" } } })
expect(input.inferenceConfig?.temperature).toBeDefined()
expect("temperature" in (input.inferenceConfig ?? {})).toBe(false)
})

it("sends no Anthropic fields for a non-Anthropic base model", async () => {
Expand All @@ -2234,7 +2242,8 @@ describe("AwsBedrockHandler", () => {
const input = await sendFirstRequest(handler)
expect(input.modelId).toBe(appProfileArn)
expect(input.additionalModelRequestFields).toBeUndefined()
expect(input.inferenceConfig?.temperature).toBeDefined()
// #242: no custom temperature set, so the key is omitted.
expect(input.inferenceConfig?.temperature).toBeUndefined()
})

it("uses the generic fallback with user limits when the model is not listed", () => {
Expand Down Expand Up @@ -2304,7 +2313,8 @@ describe("AwsBedrockHandler", () => {
const input = await sendFirstRequest(handler)
expect(input.modelId).toBe(profileArn)
expect(input.additionalModelRequestFields).toBeUndefined()
expect(input.inferenceConfig?.temperature).toBeDefined()
// #242: no custom temperature set, so the key is omitted.
expect(input.inferenceConfig?.temperature).toBeUndefined()
})

it("keeps the named model for a foundation-model ARN even with an explicit Other choice", () => {
Expand Down
Loading
Loading