@devries-cameron20 / t3code Mirror · read-only / commits / 5cb2a7d088
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 02:00 UTC

fix(pi): preserve explicit thinking when switching models (#32389)

Co-authored-by: GPT 6.1 Sol <codex@openai.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Wout Stiens committed Nov 7, 2026 at 11:49 UTC 5cb2a7d08802f3f046dcd10b0e802c1e63e570c8
2 files changed +174 -48
packages/provider-pi/src/server/adapter.test.ts
+169 -46
@@ -165,11 +165,14 @@ interface FakePi {
165 readonly deferNextState: () => void;
166 readonly resolveDeferredState: (data: unknown) => Effect.Effect<void>;
167 readonly failNextState: () => void;
168 + readonly failNextThinking: () => void;
169 readonly deferNextLifecycle: (type: "switch_session" | "new_session" | "fork" | "abort") => void;
170 readonly deferNextModelSelection: () => void;
171 readonly queueModels: (models: ReadonlyArray<unknown>) => void;
172 readonly vetoNextNewSession: () => void;
173 readonly allRequests: () => ReadonlyArray<PiRpcRecord>;
174 + readonly promptThinkingLevels: ReadonlyArray<string>;
175 + readonly promptModels: ReadonlyArray<string>;
176 readonly queueStats: (data: unknown) => void;
177 /** Pi before 0.84.4 answers clear_queue as an unknown command. */
178 readonly setClearQueue: (
@@ -213,6 +216,7 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
216 let deferState = false;
217 let deferredStateRequest: PiRpcRecord | undefined;
218 let failState = false;
219 + let failThinking = false;
220 let vetoSwitch = false;
221 let vetoNewSession = false;
222 let deferredLifecycle: string | undefined;
@@ -223,6 +227,10 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
227 let isCompacting = false;
228 let clearQueue: Parameters<FakePi["setClearQueue"]>[0] = { steering: [], followUp: [] };
229 let stdinBuffer = "";
230 + let thinkingLevel = "high";
231 + const promptThinkingLevels: Array<string> = [];
232 + const promptModels: Array<string> = [];
233 + let currentModel = "default";
234
235 const emit = (record: PiRpcRecord, chunkBytes?: number) =>
236 Effect.gen(function* () {
@@ -259,6 +267,18 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
267 ...stateQueue.shift(),
268 },
269 };
270 + case "set_model":
271 + // Pi reapplies the target model's configured thinking default.
272 + thinkingLevel = "low";
273 + currentModel = `${String(record["provider"])}/${String(record["modelId"])}`;
274 + return base;
275 + case "set_thinking_level":
276 + if (failThinking) {
277 + failThinking = false;
278 + return { ...base, success: false, error: "thinking unavailable" };
279 + }
280 + thinkingLevel = String(record["level"]);
281 + return base;
282 case "prompt":
283 if (isCompacting) {
284 return { ...base, success: false, error: PI_COMPACTING_PROMPT_ERROR };
@@ -312,6 +332,10 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
332 stdinBuffer = stdinBuffer.slice(newline + 1);
333 if (line.length === 0) continue;
334 const record = decodeJsonLine(line) as PiRpcRecord;
335 + if (record["type"] === "prompt") {
336 + promptThinkingLevels.push(thinkingLevel);
337 + promptModels.push(currentModel);
338 + }
339 allRequests.push(record);
340 yield* Queue.offer(requests, record);
341 if (record["type"] === "get_state" && deferState) {
@@ -366,6 +390,11 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
390
391 return {
392 spawner,
393 + failNextThinking: () => {
394 + failThinking = true;
395 + },
396 + promptThinkingLevels,
397 + promptModels,
398 emit,
399 takeRequest,
400 queueEntries: (data) => entriesQueue.push(data),
@@ -2214,55 +2243,149 @@ describe("PiAdapterV2", () => {
2243 }).pipe(Effect.scoped, Effect.provide(layerTest)),
2244 );
2245
2217 - it.effect("resets applied thinking when returning to Pi default", () =>
2218 - Effect.gen(function* () {
2219 - const fake = yield* makeFakePi;
2220 - fake.queueState({
2221 - model: { provider: "xai", id: "grok-4.6" },
2222 - thinkingLevel: "medium",
2223 - sessionFile: FAKE_SESSION_FILE,
2224 - sessionId: "abc",
2225 - });
2226 - const { runtime, takeEvent } = yield* openRuntime(fake);
2227 - const providerThread = yield* runtime.ensureThread({
2228 - threadId: THREAD_ID,
2229 - modelSelection: modelSelection("default"),
2230 - runtimePolicy,
2231 - });
2246 + it.effect.each(["anthropic/model-b", "default"])(
2247 + "preserves explicit thinking when switching models to %s in the same runtime",
2248 + (targetModel) =>
2249 + Effect.gen(function* () {
2250 + const fake = yield* makeFakePi;
2251 + fake.queueState({
2252 + model: { provider: "anthropic", id: "model-b" },
2253 + thinkingLevel: "high",
2254 + sessionFile: FAKE_SESSION_FILE,
2255 + });
2256 + const { runtime, takeEvent } = yield* openRuntime(fake);
2257 + const providerThread = yield* runtime.ensureThread({
2258 + threadId: THREAD_ID,
2259 + modelSelection: modelSelection("default"),
2260 + runtimePolicy,
2261 + });
2262 + const selection = (model: string): ModelSelection => ({
2263 + instanceId: PI_INSTANCE_ID,
2264 + model,
2265 + options: [{ id: "thinking", value: "high" }],
2266 + });
2267
2233 - yield* startTurn(runtime, providerThread, "default", [], "Hello pi", {
2234 - instanceId: PI_INSTANCE_ID,
2235 - model: "xai/grok-4.6",
2236 - options: [{ id: "thinking", value: "high" }],
2237 - });
2238 - const modelRequest = yield* fake.takeRequest("set_model");
2239 - assert.equal(modelRequest["provider"], "xai");
2240 - assert.equal(modelRequest["modelId"], "grok-4.6");
2241 - const levelRequest = yield* fake.takeRequest("set_thinking_level");
2242 - assert.equal(levelRequest["level"], "high");
2243 - yield* fake.emit({ type: "agent_start" });
2244 - yield* fake.emit({ type: "agent_end", messages: [], willRetry: false });
2245 - yield* fake.emit({ type: "agent_settled" });
2246 - yield* takeEvent((event) => event.type === "turn.terminal");
2268 + yield* startTurn(
2269 + runtime,
2270 + providerThread,
2271 + "default",
2272 + [],
2273 + "First",
2274 + selection("anthropic/model-a"),
2275 + );
2276 + yield* fake.takeRequest("prompt");
2277 + yield* fake.emit({ type: "agent_start" });
2278 + yield* fake.emit({ type: "agent_end", messages: [], willRetry: false });
2279 + yield* fake.emit({ type: "agent_settled" });
2280 + yield* takeEvent((event) => event.type === "turn.terminal");
2281
2248 - yield* startTurn(
2249 - runtime,
2250 - providerThread,
2251 - "default",
2252 - [],
2253 - "Hello pi",
2254 - {
2282 + yield* startTurn(
2283 + runtime,
2284 + providerThread,
2285 + "default",
2286 + [],
2287 + "Second",
2288 + selection(targetModel),
2289 + 2,
2290 + );
2291 + yield* fake.takeRequest("prompt");
2292 + assert.deepEqual(fake.promptThinkingLevels, ["high", "high"]);
2293 + }).pipe(Effect.scoped, Effect.provide(layerTest)),
2294 + );
2295 +
2296 + it.effect.each([false, true, "previous"] as const)(
2297 + "resets applied thinking when returning to Pi default (failed first restore: %s)",
2298 + (failFirstRestore) =>
2299 + Effect.gen(function* () {
2300 + const fake = yield* makeFakePi;
2301 + fake.queueState({
2302 + model: { provider: "xai", id: "grok-4.6" },
2303 + thinkingLevel: "medium",
2304 + sessionFile: FAKE_SESSION_FILE,
2305 + sessionId: "abc",
2306 + });
2307 + const { runtime, takeEvent } = yield* openRuntime(fake);
2308 + const providerThread = yield* runtime.ensureThread({
2309 + threadId: THREAD_ID,
2310 + modelSelection: modelSelection("default"),
2311 + runtimePolicy,
2312 + });
2313 +
2314 + yield* startTurn(runtime, providerThread, "default", [], "Hello pi", {
2315 instanceId: PI_INSTANCE_ID,
2256 - model: "default",
2257 - },
2258 - 2,
2259 - );
2260 - const replayModel = yield* fake.takeRequest("set_model");
2261 - assert.equal(replayModel["provider"], "xai");
2262 - assert.equal(replayModel["modelId"], "grok-4.6");
2263 - const resetLevel = yield* fake.takeRequest("set_thinking_level");
2264 - assert.equal(resetLevel["level"], "medium");
2265 - }).pipe(Effect.scoped, Effect.provide(layerTest)),
2316 + model: "anthropic/model-a",
2317 + options: [{ id: "thinking", value: "high" }],
2318 + });
2319 + const modelRequest = yield* fake.takeRequest("set_model");
2320 + assert.equal(modelRequest["provider"], "anthropic");
2321 + assert.equal(modelRequest["modelId"], "model-a");
2322 + const levelRequest = yield* fake.takeRequest("set_thinking_level");
2323 + assert.equal(levelRequest["level"], "high");
2324 + yield* fake.takeRequest("prompt");
2325 + yield* fake.emit({ type: "agent_start" });
2326 + yield* fake.emit({ type: "agent_end", messages: [], willRetry: false });
2327 + yield* fake.emit({ type: "agent_settled" });
2328 + yield* takeEvent((event) => event.type === "turn.terminal");
2329 +
2330 + if (failFirstRestore) {
2331 + fake.failNextThinking();
2332 + const result = yield* startTurn(
2333 + runtime,
2334 + providerThread,
2335 + "default",
2336 + [],
2337 + "Hello pi",
2338 + {
2339 + instanceId: PI_INSTANCE_ID,
2340 + model: "default",
2341 + },
2342 + 2,
2343 + ).pipe(Effect.exit);
2344 + assert.equal(result._tag, "Failure");
2345 + yield* fake.takeRequest("set_model");
2346 + yield* fake.takeRequest("set_thinking_level");
2347 + }
2348 +
2349 + yield* startTurn(
2350 + runtime,
2351 + providerThread,
2352 + "default",
2353 + [],
2354 + "Hello pi",
2355 + {
2356 + instanceId: PI_INSTANCE_ID,
2357 + model: failFirstRestore === "previous" ? "anthropic/model-a" : "default",
2358 + ...(failFirstRestore === "previous"
2359 + ? { options: [{ id: "thinking", value: "high" }] }
2360 + : {}),
2361 + },
2362 + 2,
2363 + );
2364 + yield* fake.takeRequest("prompt");
2365 + assert.deepEqual(fake.promptThinkingLevels, [
2366 + "high",
2367 + failFirstRestore === "previous" ? "high" : "medium",
2368 + ]);
2369 + assert.deepEqual(fake.promptModels, [
2370 + "anthropic/model-a",
2371 + failFirstRestore === "previous" ? "anthropic/model-a" : "xai/grok-4.6",
2372 + ]);
2373 + const replayModel = fake
2374 + .allRequests()
2375 + .findLast((request) => request["type"] === "set_model")!;
2376 + assert.equal(
2377 + replayModel["provider"],
2378 + failFirstRestore === "previous" ? "anthropic" : "xai",
2379 + );
2380 + assert.equal(
2381 + replayModel["modelId"],
2382 + failFirstRestore === "previous" ? "model-a" : "grok-4.6",
2383 + );
2384 + const resetLevel = fake
2385 + .allRequests()
2386 + .findLast((request) => request["type"] === "set_thinking_level")!;
2387 + assert.equal(resetLevel["level"], failFirstRestore === "previous" ? "high" : "medium");
2388 + }).pipe(Effect.scoped, Effect.provide(layerTest)),
2389 );
2390
2391 it.effect.each([
packages/provider-pi/src/server/adapter.ts
+5 -2
@@ -2484,6 +2484,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
2484 }
2485 if (baselineThinking === null && appliedThinking === null) {
2486 baselineThinking = recordString(stateData, "thinkingLevel") ?? null;
2487 + appliedThinking = baselineThinking;
2488 }
2489 // switch_session accepts a file path, not Pi's display session UUID.
2490 const nativeId = recordString(stateData, "sessionFile");
@@ -2546,6 +2547,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
2547 if (modelSelection.model === PI_INHERIT_MODEL_SLUG) {
2548 if (appliedModel !== null && baselineModel !== null) {
2549 const restoredModel = yield* request({ type: "set_model", ...baselineModel });
2550 + appliedThinking = null;
2551 contextWindow = rememberModelContextWindow(restoredModel);
2552 appliedModel = null;
2553 const updatedAt = yield* DateTime.now;
@@ -2558,12 +2560,11 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
2560 }
2561 if (
2562 thinking === undefined &&
2561 - appliedThinking !== null &&
2563 baselineThinking !== null &&
2564 appliedThinking !== baselineThinking
2565 ) {
2566 yield* request({ type: "set_thinking_level", level: baselineThinking });
2566 - appliedThinking = null;
2567 + appliedThinking = baselineThinking;
2568 }
2569 } else if (modelSelection.model !== appliedModel) {
2570 const parsed = parsePiModelSlug(modelSelection.model);
@@ -2577,6 +2578,8 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
2578 provider: parsed.provider,
2579 modelId: parsed.modelId,
2580 });
2581 + // Pi resets thinking to the target model's configured default on set_model.
2582 + appliedThinking = null;
2583 contextWindow = rememberModelContextWindow(selectedModel);
2584 appliedModel = modelSelection.model;
2585 const updatedAt = yield* DateTime.now;