@devries-cameron20 / t3code Mirror · read-only / commits / a68bd772a0
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 21, 23:30 UTC

fix(grok): apply the selected reasoning effort to ACP sessions (#19573)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 11, 2026 at 17:47 UTC a68bd772a04ddac6c9aff26c2cd722fbb11d09bc
4 files changed +483 -20
packages/provider-grok/src/server/adapter.test.ts new
+265
@@ -0,0 +1,265 @@
1 +// @effect-diagnostics nodeBuiltinImport:off
2 +import * as NodeFS from "node:fs";
3 +
4 +import * as NodeServices from "@effect/platform-node/NodeServices";
5 +import { assert, describe, it } from "@effect/vitest";
6 +import {
7 + MessageId,
8 + type ModelSelection,
9 + NodeId,
10 + ProjectId,
11 + ProviderInstanceId,
12 + ProviderSessionId,
13 + RunAttemptId,
14 + RunId,
15 + ThreadId,
16 +} from "@t3tools/contracts";
17 +import * as HostProcess from "@t3tools/shared/HostProcess";
18 +import { resolveSelfInvocation } from "@t3tools/shared/nodeRuntime";
19 +import * as SecureRandom from "effect/crypto/SecureRandom";
20 +import * as DateTime from "effect/DateTime";
21 +import * as Effect from "effect/Effect";
22 +import * as FileSystem from "effect/FileSystem";
23 +import * as Layer from "effect/Layer";
24 +import * as Path from "effect/Path";
25 +import * as Schema from "effect/Schema";
26 +import * as Stream from "effect/Stream";
27 +
28 +import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
29 +import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
30 +import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
31 +import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
32 +import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
33 +import { GrokSettings } from "../settings.ts";
34 +import { makeGrokAdapterV2 } from "./adapter.ts";
35 +
36 +const layerTest = Layer.mergeAll(
37 + NodeServices.layer,
38 + IdAllocator.layer,
39 + McpProviderSessions.layer,
40 + SecureRandom.layerWebCrypto,
41 + TestProviderHost.layer().pipe(Layer.provide(NodeServices.layer)),
42 +);
43 +
44 +const DEFAULT_GROK_SETTINGS = Schema.decodeSync(GrokSettings)({});
45 +const instanceId = ProviderInstanceId.make("grok");
46 +const select = (model: string, reasoningEffort?: string): ModelSelection => ({
47 + instanceId,
48 + model,
49 + ...(reasoningEffort === undefined
50 + ? {}
51 + : { options: [{ id: "reasoningEffort", value: reasoningEffort }] }),
52 +});
53 +
54 +const ModelRequest = Schema.Struct({
55 + method: Schema.String,
56 + params: Schema.Struct({
57 + modelId: Schema.optional(Schema.String),
58 + configId: Schema.optional(Schema.String),
59 + value: Schema.optional(Schema.String),
60 + _meta: Schema.optional(Schema.Struct({ reasoningEffort: Schema.optional(Schema.String) })),
61 + }),
62 +});
63 +const decodeModelRequest = Schema.decodeUnknownSync(Schema.fromJsonString(ModelRequest));
64 +
65 +/** The model and effort writes Grok received, in wire order. */
66 +function modelWrites(requestLogPath: string): ReadonlyArray<string> {
67 + return NodeFS.readFileSync(requestLogPath, "utf8")
68 + .split("\n")
69 + .filter(
70 + (line) =>
71 + line.includes('"session/set_model"') || line.includes('"session/set_config_option"'),
72 + )
73 + .map((line) => {
74 + const { method, params } = decodeModelRequest(line);
75 + return method === "session/set_model"
76 + ? `set_model ${params.modelId}${params._meta?.reasoningEffort ? ` effort=${params._meta.reasoningEffort}` : ""}`
77 + : `set_config_option ${params.configId}=${params.value}`;
78 + });
79 +}
80 +
81 +/**
82 + * Opens a Grok session on the first selection, then runs one turn per later
83 + * selection on the same live session. Returns the model and effort writes each
84 + * step sent.
85 + */
86 +const writesPerSelection = (
87 + selections: ReadonlyArray<ModelSelection>,
88 + agentEnvironment: Readonly<Record<string, string>>,
89 +) =>
90 + Effect.gen(function* () {
91 + const fileSystem = yield* FileSystem.FileSystem;
92 + const path = yield* Path.Path;
93 + const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-grok-effort-" });
94 + const requestLogPath = path.join(directory, "requests.ndjson");
95 + yield* fileSystem.writeFileString(requestLogPath, "");
96 + const agentPath = yield* path.fromFileUrl(
97 + new URL("./testFixtures/grok-model-selection-agent.mjs", import.meta.url),
98 + );
99 + const adapter = yield* makeGrokAdapterV2({
100 + instanceId,
101 + settings: DEFAULT_GROK_SETTINGS,
102 + environment: {},
103 + hostPlatform: yield* HostProcess.Platform,
104 + selfInvocation: yield* resolveSelfInvocation(),
105 + makeRuntime: (runtimeInput) =>
106 + Effect.gen(function* () {
107 + const context = yield* Layer.build(
108 + AcpSessionRuntime.layer({
109 + ...runtimeInput,
110 + spawn: {
111 + command: process.execPath,
112 + args: [agentPath],
113 + cwd: runtimeInput.cwd,
114 + env: { ...agentEnvironment, T3_ACP_REQUEST_LOG_PATH: requestLogPath },
115 + },
116 + authMethodId: "cached_token",
117 + }),
118 + );
119 + return yield* Effect.service(AcpSessionRuntime.AcpSessionRuntime).pipe(
120 + Effect.provide(context),
121 + );
122 + }),
123 + });
124 + const threadId = ThreadId.make("grok-effort-thread");
125 + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
126 + runtimeMode: "full-access",
127 + interactionMode: "default",
128 + cwd: directory,
129 + });
130 + const [first, ...rest] = selections;
131 + if (first === undefined) return [];
132 + const writes: Array<ReadonlyArray<string>> = [];
133 + const step = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
134 + Effect.gen(function* () {
135 + const before = modelWrites(requestLogPath).length;
136 + const result = yield* effect;
137 + writes.push(modelWrites(requestLogPath).slice(before));
138 + return result;
139 + });
140 + const runtime = yield* step(
141 + adapter.openSession({
142 + threadId,
143 + providerSessionId: ProviderSessionId.make("grok-effort-session"),
144 + modelSelection: first,
145 + runtimePolicy,
146 + }),
147 + );
148 + const providerThread = yield* runtime.ensureThread({
149 + threadId,
150 + modelSelection: first,
151 + runtimePolicy,
152 + });
153 + const now = yield* DateTime.now;
154 + for (const [index, modelSelection] of rest.entries()) {
155 + const ordinal = index + 1;
156 + const suffix = `${threadId}:${ordinal}`;
157 + yield* step(
158 + runtime.startTurn({
159 + appThread: {
160 + createdBy: "user",
161 + creationSource: "web",
162 + id: threadId,
163 + projectId: ProjectId.make("grok-effort-project"),
164 + title: "Grok effort",
165 + providerInstanceId: instanceId,
166 + modelSelection,
167 + runtimeMode: "full-access",
168 + interactionMode: "default",
169 + branch: null,
170 + worktreePath: null,
171 + activeProviderThreadId: providerThread.id,
172 + lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
173 + forkedFrom: null,
174 + createdAt: now,
175 + updatedAt: now,
176 + archivedAt: null,
177 + settledOverride: null,
178 + settledAt: null,
179 + lastVisitedAt: null,
180 + deletedAt: null,
181 + },
182 + threadId,
183 + runId: RunId.make(`run:${suffix}`),
184 + runOrdinal: ordinal,
185 + providerTurnOrdinal: ordinal,
186 + attemptId: RunAttemptId.make(`attempt:${suffix}`),
187 + rootNodeId: NodeId.make(`node:${suffix}`),
188 + providerThread,
189 + message: {
190 + createdBy: "user",
191 + creationSource: "web",
192 + messageId: MessageId.make(`message:${suffix}`),
193 + text: "hello",
194 + attachments: [],
195 + },
196 + modelSelection,
197 + runtimePolicy,
198 + }),
199 + );
200 + yield* runtime.events.pipe(
201 + Stream.filter((event) => event.type === "turn.terminal"),
202 + Stream.runHead,
203 + );
204 + }
205 + return writes;
206 + }).pipe(Effect.scoped, Effect.provide(layerTest));
207 +
208 +describe("Grok reasoning effort", () => {
209 + // Grok's session starts at xhigh on grok-4.6. grok-4.6-fast offers only
210 + // high and low, and resets to high when a switch carries no effort.
211 + const selections = [
212 + select("grok-4.6", "low"),
213 + select("grok-4.6", "high"),
214 + select("grok-4.6"),
215 + select("grok-4.6", "high"),
216 + select("grok-4.6-fast", "xhigh"),
217 + select("grok-4.6", "low"),
218 + ];
219 +
220 + it.live("protocol 1 carries the selected effort on session/set_model", () =>
221 + Effect.gen(function* () {
222 + assert.deepEqual(yield* writesPerSelection(selections, { T3_GROK_MOCK_PROTOCOL: "1" }), [
223 + ["set_model grok-4.6 effort=low"],
224 + ["set_model grok-4.6 effort=high"],
225 + [],
226 + ["set_model grok-4.6 effort=high"],
227 + ["set_model grok-4.6-fast"],
228 + ["set_model grok-4.6 effort=low"],
229 + ]);
230 + }),
231 + );
232 +
233 + it.live("newer protocols write the session's own reasoning_effort option", () =>
234 + Effect.gen(function* () {
235 + assert.deepEqual(yield* writesPerSelection(selections, { T3_GROK_MOCK_PROTOCOL: "2" }), [
236 + ["set_config_option reasoning_effort=low"],
237 + ["set_config_option reasoning_effort=high"],
238 + [],
239 + [],
240 + ["set_config_option model=grok-4.6-fast"],
241 + ["set_config_option model=grok-4.6", "set_config_option reasoning_effort=low"],
242 + ]);
243 + }),
244 + );
245 +
246 + it.live("an effort Grok refuses keeps the model switch and opens the session", () =>
247 + Effect.gen(function* () {
248 + const refused = [select("grok-4.6-fast", "low")];
249 + assert.deepEqual(
250 + yield* writesPerSelection(refused, {
251 + T3_GROK_MOCK_PROTOCOL: "1",
252 + T3_GROK_MOCK_REJECT_EFFORT: "low",
253 + }),
254 + [["set_model grok-4.6-fast effort=low", "set_model grok-4.6-fast"]],
255 + );
256 + assert.deepEqual(
257 + yield* writesPerSelection(refused, {
258 + T3_GROK_MOCK_PROTOCOL: "2",
259 + T3_GROK_MOCK_REJECT_EFFORT: "low",
260 + }),
261 + [["set_config_option model=grok-4.6-fast", "set_config_option reasoning_effort=low"]],
262 + );
263 + }),
264 + );
265 +});
packages/provider-grok/src/server/adapter.ts
+92 -19
@@ -22,6 +22,7 @@ import * as Schema from "effect/Schema";
22 import type * as Scope from "effect/Scope";
23 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
24 import * as EffectAcpErrors from "effect-acp/errors";
25 +import { getModelSelectionStringOptionValue } from "@t3tools/shared/model";
26
27 import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
28 import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
@@ -29,10 +30,12 @@ import { makeAcpNativeLoggerFactory } from "@t3tools/provider-acp/server/nativeL
30 import {
31 applyGrokAcpModelSelection,
32 currentGrokModelIdFromSessionSetup,
33 + GROK_DEFAULT_MODEL_SLUG,
34 grokApprovalOptions,
35 makeGrokAcpRuntime,
36 resolveGrokAcpBaseModelId,
37 } from "./acpSupport.ts";
38 +import { grokReasoningOptionsFromModel } from "./status.ts";
39 import {
40 extractGrokPlanMarkdownFromToolCallData,
41 extractXAiAcpBackgroundToolMutation,
@@ -221,6 +224,93 @@ export function grokLaunchRuntimeMode(
224 : "approval-required";
225 }
226
227 +/** The capability id the composer stores Grok's reasoning effort under. */
228 +const GROK_REASONING_EFFORT_OPTION_ID = "reasoningEffort";
229 +
230 +// Protocol 1 sends the effort as `session/set_model` metadata on every call: that response
231 +// carries no config options, so there is no live effort to compare against. Newer protocols
232 +// write the `thought_level` config option after the model switch. An effort the model does
233 +// not offer, or that Grok refuses, is skipped with a warning and Grok's default applies.
234 +const applyGrokSessionModelSelection: NonNullable<AcpAdapterV2Flavor["applyModelSelection"]> =
235 + Effect.fn("GrokAdapter.applyModelSelection")(function* ({
236 + runtime,
237 + startResult,
238 + modelSelection,
239 + }) {
240 + const requestedModelId = resolveGrokAcpBaseModelId(modelSelection.model);
241 + const requestedEffort = getModelSelectionStringOptionValue(
242 + modelSelection,
243 + GROK_REASONING_EFFORT_OPTION_ID,
244 + );
245 + const skipEffort = (detail: string) =>
246 + Effect.logWarning("Grok session skipped the requested reasoning effort", {
247 + sessionId: startResult.sessionId,
248 + reasoningEffort: requestedEffort,
249 + detail,
250 + });
251 +
252 + if (startResult.initializeResult.protocolVersion === 1) {
253 + const setup = startResult.sessionSetupResult;
254 + const currentModelId = currentGrokModelIdFromSessionSetup(setup);
255 + const targetModelId =
256 + requestedModelId === GROK_DEFAULT_MODEL_SLUG ? currentModelId : requestedModelId;
257 + const targetModel = setup.models?.availableModels.find(
258 + (model) => model.modelId.trim() === targetModelId,
259 + );
260 + const offeredEfforts =
261 + targetModel === undefined
262 + ? []
263 + : grokReasoningOptionsFromModel(targetModel).options.map((option) => option.value);
264 + const effort =
265 + requestedEffort !== undefined &&
266 + (offeredEfforts.length === 0 || offeredEfforts.includes(requestedEffort))
267 + ? requestedEffort
268 + : undefined;
269 + if (requestedEffort !== undefined && effort === undefined) {
270 + yield* skipEffort(`${targetModelId} does not offer it`);
271 + }
272 + const apply = (requestedReasoningEffort: string | undefined) =>
273 + applyGrokAcpModelSelection({
274 + runtime,
275 + currentModelId,
276 + requestedModelId,
277 + requestedReasoningEffort,
278 + mapError: (cause) => cause,
279 + });
280 + return yield* effort === undefined
281 + ? apply(undefined)
282 + : apply(effort).pipe(
283 + Effect.catchTags({
284 + AcpRequestError: (error) =>
285 + skipEffort(error.message).pipe(Effect.andThen(apply(undefined))),
286 + }),
287 + );
288 + }
289 +
290 + const appliedModelId = yield* applyGrokAcpModelSelection({
291 + runtime: { setSessionModel: (model) => runtime.setModel(model).pipe(Effect.as({})) },
292 + currentModelId: findModelConfigOption(yield* runtime.getConfigOptions)?.currentValue,
293 + requestedModelId,
294 + mapError: (cause) => cause,
295 + });
296 + if (requestedEffort === undefined) {
297 + return appliedModelId;
298 + }
299 + // Read after the model switch: the effort choices belong to the new model.
300 + const effortOption = (yield* runtime.getConfigOptions).find(
301 + (option) => option.type === "select" && option.category === "thought_level",
302 + );
303 + if (effortOption === undefined) {
304 + yield* skipEffort("the session has no reasoning effort option");
305 + return appliedModelId;
306 + }
307 + // setConfigOption rejects a value the option does not offer and skips the current one.
308 + yield* runtime
309 + .setConfigOption(effortOption.id, requestedEffort)
310 + .pipe(Effect.catchTags({ AcpRequestError: (error) => skipEffort(error.message) }));
311 + return appliedModelId;
312 + });
313 +
314 export function makeGrokAcpAdapterFlavor(
315 options: GrokAdapterV2Options,
316 homeDirectory: string,
@@ -270,25 +360,8 @@ export function makeGrokAcpAdapterFlavor(
360 ? window
361 : undefined;
362 },
273 - applyModelSelection: Effect.fn("GrokAdapter.applyModelSelection")(function* ({
274 - runtime,
275 - startResult,
276 - modelSelection,
277 - }) {
278 - const legacy = startResult.initializeResult.protocolVersion === 1;
279 - const options = legacy ? [] : yield* runtime.getConfigOptions;
280 - const configuredModel = findModelConfigOption(options)?.currentValue;
281 - return yield* applyGrokAcpModelSelection({
282 - runtime: legacy
283 - ? runtime
284 - : { setSessionModel: (model) => runtime.setModel(model).pipe(Effect.as({})) },
285 - currentModelId: legacy
286 - ? currentGrokModelIdFromSessionSetup(startResult.sessionSetupResult)
287 - : configuredModel,
288 - requestedModelId: resolveGrokAcpBaseModelId(modelSelection.model),
289 - mapError: (cause) => cause,
290 - });
291 - }),
363 + applyModelSelection: applyGrokSessionModelSelection,
364 + ownedModelOptionIds: [GROK_REASONING_EFFORT_OPTION_ID],
365 makeRuntime:
366 options.makeRuntime ??
367 (({ runtimePolicy, ...input }) =>
packages/provider-grok/src/server/status.ts
+1 -1
@@ -128,7 +128,7 @@ function isRecord(value: unknown): value is Record<string, unknown> {
128
129 const GROK_EFFORT_RANK = ["none", "minimal", "low", "medium", "high", "xhigh", "max"];
130
131 -function grokReasoningOptionsFromModel(model: EffectAcpSchema.ModelInfo): {
131 +export function grokReasoningOptionsFromModel(model: EffectAcpSchema.ModelInfo): {
132 readonly options: ReadonlyArray<{
133 value: string;
134 label: string;
packages/provider-grok/src/server/testFixtures/grok-model-selection-agent.mjs new
+125
@@ -0,0 +1,125 @@
1 +// Grok's model and effort wire contract (grok 1.0.41): per-model efforts on `models`, a
2 +// `thought_level` option `reasoning_effort`, and `config_option_update` after set_model.
3 +// T3_GROK_MOCK_PROTOCOL picks the protocol (default 1); T3_GROK_MOCK_REJECT_EFFORT names
4 +// an advertised effort Grok refuses.
5 +import * as NodeFS from "node:fs";
6 +import * as NodeReadline from "node:readline";
7 +
8 +const sessionId = "grok-model-selection-session";
9 +const protocolVersion = Number(process.env.T3_GROK_MOCK_PROTOCOL ?? "1");
10 +const rejectedEffort = process.env.T3_GROK_MOCK_REJECT_EFFORT;
11 +const models = {
12 + "grok-4.6": { efforts: ["xhigh", "high", "medium", "low"], defaultEffort: "xhigh" },
13 + "grok-4.6-fast": { efforts: ["high", "low"], defaultEffort: "high" },
14 +};
15 +let currentModelId = "grok-4.6";
16 +let currentEffort = models[currentModelId].defaultEffort;
17 +const write = (message) => process.stdout.write(`${JSON.stringify(message)}\n`);
18 +
19 +const configOptions = () => [
20 + {
21 + id: "model",
22 + name: "Model",
23 + category: "model",
24 + type: "select",
25 + currentValue: currentModelId,
26 + options: Object.keys(models).map((value) => ({ value, name: value })),
27 + },
28 + {
29 + id: "reasoning_effort",
30 + name: "Reasoning Effort",
31 + category: "thought_level",
32 + type: "select",
33 + currentValue: currentEffort,
34 + options: models[currentModelId].efforts.map((value) => ({ value, name: value })),
35 + },
36 +];
37 +
38 +// Returns an error message when Grok would refuse the switch.
39 +const switchTo = (modelId, effort) => {
40 + const model = models[modelId];
41 + if (model === undefined) return "Unknown Grok model";
42 + if (effort !== undefined && (effort === rejectedEffort || !model.efforts.includes(effort))) {
43 + return `Unsupported reasoning effort ${effort}`;
44 + }
45 + currentEffort = effort ?? (modelId === currentModelId ? currentEffort : model.defaultEffort);
46 + currentModelId = modelId;
47 + return undefined;
48 +};
49 +
50 +for await (const line of NodeReadline.createInterface({ input: process.stdin })) {
51 + const request = JSON.parse(line);
52 + if (process.env.T3_ACP_REQUEST_LOG_PATH) {
53 + NodeFS.appendFileSync(process.env.T3_ACP_REQUEST_LOG_PATH, `${line}\n`);
54 + }
55 + if (request.id === undefined) continue;
56 + const fail = (message) =>
57 + write({ jsonrpc: "2.0", id: request.id, error: { code: -32602, message } });
58 + let result;
59 + switch (request.method) {
60 + case "initialize":
61 + result = { protocolVersion, agentInfo: { name: "grok", version: "mock" } };
62 + break;
63 + case "session/new":
64 + result = {
65 + sessionId,
66 + models: {
67 + currentModelId,
68 + availableModels: Object.entries(models).map(([modelId, model]) => ({
69 + modelId,
70 + name: modelId,
71 + _meta: {
72 + supportsReasoningEffort: true,
73 + reasoningEffort: modelId === currentModelId ? currentEffort : model.defaultEffort,
74 + reasoningEfforts: model.efforts.map((value) => ({ id: value, value, label: value })),
75 + },
76 + })),
77 + },
78 + configOptions: configOptions(),
79 + };
80 + break;
81 + case "session/set_model": {
82 + const error = switchTo(request.params.modelId, request.params._meta?.reasoningEffort);
83 + if (error !== undefined) {
84 + fail(error);
85 + continue;
86 + }
87 + write({ jsonrpc: "2.0", id: request.id, result: {} });
88 + write({
89 + jsonrpc: "2.0",
90 + method: "session/update",
91 + params: {
92 + sessionId,
93 + update: { sessionUpdate: "config_option_update", configOptions: configOptions() },
94 + },
95 + });
96 + continue;
97 + }
98 + case "session/set_config_option": {
99 + const { configId, value } = request.params;
100 + const error =
101 + configId === "model"
102 + ? switchTo(value, undefined)
103 + : configId === "reasoning_effort"
104 + ? switchTo(currentModelId, value)
105 + : `Unknown config option ${configId}`;
106 + if (error !== undefined) {
107 + fail(error);
108 + continue;
109 + }
110 + result = { configOptions: configOptions() };
111 + break;
112 + }
113 + case "session/prompt":
114 + result = { stopReason: "end_turn" };
115 + break;
116 + default:
117 + write({
118 + jsonrpc: "2.0",
119 + id: request.id,
120 + error: { code: -32601, message: "Method not found" },
121 + });
122 + continue;
123 + }
124 + write({ jsonrpc: "2.0", id: request.id, result });
125 +}