GeezerVison is showing github.com — fix(grok): harden advertised reasoning metadata · pingdotgg/t3code@e0fc12b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/Layers/GrokAdapter.test.ts +54Lines changed: 54 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -237,6 +237,60 @@ it.layer(grokAdapterTestLayer)("GrokAdapterLive", (it) => { | 237 | 237 | }), | 238 | 238 | ); | 239 | 239 |
| | 240 | + it.effect("clears and reapplies reasoning effort on the same model", () => | | 241 | + Effect.gen(function* () { | | 242 | + const threadId = ThreadId.make("grok-same-model-effort-clear"); | | 243 | + const tempDir = yield* Effect.promise(() => | | 244 | + NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "grok-same-model-effort-clear-")), | | 245 | + ); | | 246 | + const requestLogPath = NodePath.join(tempDir, "requests.ndjson"); | | 247 | + const wrapperPath = yield* Effect.promise(() => | | 248 | + makeMockGrokWrapper({ T3_ACP_REQUEST_LOG_PATH: requestLogPath }), | | 249 | + ); | | 250 | + const adapter = yield* makeTestAdapter(wrapperPath); | | 251 | + const selection = (effort?: string) => ({ | | 252 | + instanceId: ProviderInstanceId.make("grok"), | | 253 | + model: "grok-build", | | 254 | + ...(effort ? { options: [{ id: "reasoningEffort", value: effort }] } : {}), | | 255 | + }); | | 256 | + | | 257 | + yield* adapter.startSession({ | | 258 | + threadId, | | 259 | + provider: ProviderDriverKind.make("grok"), | | 260 | + cwd: process.cwd(), | | 261 | + runtimeMode: "full-access", | | 262 | + modelSelection: selection(), | | 263 | + }); | | 264 | + yield* adapter.sendTurn({ | | 265 | + threadId, | | 266 | + input: "use high effort", | | 267 | + attachments: [], | | 268 | + modelSelection: selection("high"), | | 269 | + }); | | 270 | + yield* adapter.sendTurn({ | | 271 | + threadId, | | 272 | + input: "use the model default", | | 273 | + attachments: [], | | 274 | + modelSelection: selection(), | | 275 | + }); | | 276 | + yield* adapter.sendTurn({ | | 277 | + threadId, | | 278 | + input: "use high effort again", | | 279 | + attachments: [], | | 280 | + modelSelection: selection("high"), | | 281 | + }); | | 282 | + | | 283 | + const requests = yield* Effect.promise(() => readJsonLines(requestLogPath)); | | 284 | + const setModelRequests = requests.filter((entry) => entry.method === "session/set_model"); | | 285 | + assert.lengthOf(setModelRequests, 3); | | 286 | + assert.nestedPropertyVal(setModelRequests[0], "params._meta.reasoningEffort", "high"); | | 287 | + assert.notNestedProperty(setModelRequests[1], "params._meta"); | | 288 | + assert.nestedPropertyVal(setModelRequests[2], "params._meta.reasoningEffort", "high"); | | 289 | + | | 290 | + yield* adapter.stopSession(threadId); | | 291 | + }), | | 292 | + ); | | 293 | + | 240 | 294 | it.effect("closes the ACP child process when a session stops", () => | 241 | 295 | Effect.gen(function* () { | 242 | 296 | const threadId = ThreadId.make("grok-stop-session-close"); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/Layers/GrokAdapter.ts +8-11Lines changed: 8 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,6 +59,7 @@ import { | 59 | 59 | currentGrokModelIdFromSessionSetup, | 60 | 60 | currentGrokReasoningEffortFromSessionSetup, | 61 | 61 | makeGrokAcpRuntime, | | 62 | + normalizeGrokReasoningEffort, | 62 | 63 | resolveGrokAcpBaseModelId, | 63 | 64 | } from "../acp/GrokAcpSupport.ts"; | 64 | 65 | import { | @@ -751,8 +752,6 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte | 751 | 752 | grokModelSelection, | 752 | 753 | "reasoningEffort", | 753 | 754 | ); | 754 | | - const startModelChanged = | 755 | | - requestedStartModelId !== undefined && requestedStartModelId !== currentStartModelId; | 756 | 755 | const boundModelId = yield* applyGrokAcpModelSelection({ | 757 | 756 | runtime: acp, | 758 | 757 | currentModelId: currentStartModelId, | @@ -796,8 +795,9 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte | 796 | 795 | promptsInFlight: 0, | 797 | 796 | currentModelId: boundModelId, | 798 | 797 | currentReasoningEffort: | 799 | | - requestedStartReasoningEffort ?? | 800 | | - (startModelChanged ? undefined : currentStartReasoningEffort), | | 798 | + requestedStartModelId !== undefined | | 799 | + ? normalizeGrokReasoningEffort(requestedStartReasoningEffort) | | 800 | + : currentStartReasoningEffort, | 801 | 801 | stopped: false, | 802 | 802 | }; | 803 | 803 |
| @@ -966,9 +966,6 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte | 966 | 966 | turnModelSelection, | 967 | 967 | "reasoningEffort", | 968 | 968 | ); | 969 | | - const modelChanged = | 970 | | - requestedTurnModelId !== undefined && requestedTurnModelId !== ctx.currentModelId; | 971 | | - | 972 | 969 | const text = input.input?.trim(); | 973 | 970 | const imagePromptParts = yield* Effect.forEach( | 974 | 971 | input.attachments ?? [], | @@ -1026,10 +1023,10 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte | 1026 | 1023 | mapAcpToAdapterError(PROVIDER, input.threadId, "session/set_model", cause), | 1027 | 1024 | }); | 1028 | 1025 | ctx.currentModelId = currentModelId; | 1029 | | - if (requestedTurnReasoningEffort !== undefined) { | 1030 | | - ctx.currentReasoningEffort = requestedTurnReasoningEffort; | 1031 | | - } else if (modelChanged) { | 1032 | | - ctx.currentReasoningEffort = undefined; | | 1026 | + if (requestedTurnModelId !== undefined) { | | 1027 | + ctx.currentReasoningEffort = normalizeGrokReasoningEffort( | | 1028 | + requestedTurnReasoningEffort, | | 1029 | + ); | 1033 | 1030 | } | 1034 | 1031 | const displayModel = currentModelId | 1035 | 1032 | ? resolveGrokAcpBaseModelId(currentModelId) | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/Layers/GrokProvider.test.ts +95-1Lines changed: 95 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -65,13 +65,107 @@ describe("buildGrokModelCapabilities", () => { | 65 | 65 | type: "select", | 66 | 66 | currentValue: "xhigh", | 67 | 67 | options: [ | 68 | | - { id: "xhigh", label: "xhigh", isDefault: true }, | | 68 | + { id: "xhigh", label: "xhigh" }, | 69 | 69 | { id: "medium", label: "medium" }, | 70 | 70 | ], | 71 | 71 | }, | 72 | 72 | ]); | 73 | 73 | }); | 74 | 74 |
| | 75 | + it("keeps ACP current effort separate from its collapsed advertised default", () => { | | 76 | + const capabilities = buildGrokModelCapabilities({ | | 77 | + modelId: "grok-4.6", | | 78 | + name: "Grok 4.6", | | 79 | + _meta: { | | 80 | + supportsReasoningEffort: true, | | 81 | + reasoningEffort: "medium", | | 82 | + reasoningEfforts: [ | | 83 | + { value: "xhigh", label: "Extra High Effort", default: true }, | | 84 | + { value: "high", label: "High Effort", default: true }, | | 85 | + { value: "medium", label: "Medium Effort" }, | | 86 | + ], | | 87 | + }, | | 88 | + }); | | 89 | + | | 90 | + expect(capabilities.optionDescriptors).toEqual([ | | 91 | + { | | 92 | + id: "reasoningEffort", | | 93 | + label: "Reasoning", | | 94 | + type: "select", | | 95 | + currentValue: "medium", | | 96 | + options: [ | | 97 | + { id: "xhigh", label: "Extra High Effort", isDefault: true }, | | 98 | + { id: "high", label: "High Effort" }, | | 99 | + { id: "medium", label: "Medium Effort" }, | | 100 | + ], | | 101 | + }, | | 102 | + ]); | | 103 | + }); | | 104 | + | | 105 | + it("preserves ACP descriptions and falls back from invalid values to valid ids", () => { | | 106 | + const capabilities = buildGrokModelCapabilities({ | | 107 | + modelId: "grok-4.6", | | 108 | + name: "Grok 4.6", | | 109 | + _meta: { | | 110 | + supportsReasoningEffort: true, | | 111 | + reasoningEffort: "high", | | 112 | + reasoningEfforts: [ | | 113 | + { | | 114 | + id: "high", | | 115 | + value: "not a token", | | 116 | + label: "High Effort", | | 117 | + description: "Higher implementation quality", | | 118 | + default: true, | | 119 | + }, | | 120 | + { id: "bad id", value: "also invalid", label: "Invalid" }, | | 121 | + ], | | 122 | + }, | | 123 | + }); | | 124 | + | | 125 | + expect(capabilities.optionDescriptors).toEqual([ | | 126 | + { | | 127 | + id: "reasoningEffort", | | 128 | + label: "Reasoning", | | 129 | + type: "select", | | 130 | + currentValue: "high", | | 131 | + options: [ | | 132 | + { | | 133 | + id: "high", | | 134 | + label: "High Effort", | | 135 | + description: "Higher implementation quality", | | 136 | + isDefault: true, | | 137 | + }, | | 138 | + ], | | 139 | + }, | | 140 | + ]); | | 141 | + }); | | 142 | + | | 143 | + it("accepts an advertised ACP menu when the support flag is omitted", () => { | | 144 | + const capabilities = buildGrokModelCapabilities({ | | 145 | + modelId: "grok-4.6", | | 146 | + name: "Grok 4.6", | | 147 | + _meta: { | | 148 | + reasoningEffort: "high", | | 149 | + reasoningEfforts: [{ value: "high", label: "High Effort", default: true }], | | 150 | + }, | | 151 | + }); | | 152 | + | | 153 | + expect(capabilities.optionDescriptors).toHaveLength(1); | | 154 | + }); | | 155 | + | | 156 | + it("honors an explicit ACP opt-out even when a menu is present", () => { | | 157 | + const capabilities = buildGrokModelCapabilities({ | | 158 | + modelId: "grok-4.6", | | 159 | + name: "Grok 4.6", | | 160 | + _meta: { | | 161 | + supportsReasoningEffort: false, | | 162 | + reasoningEfforts: [{ value: "high", label: "High Effort", default: true }], | | 163 | + }, | | 164 | + }); | | 165 | + | | 166 | + expect(capabilities.optionDescriptors).toEqual([]); | | 167 | + }); | | 168 | + | 75 | 169 | it("does not synthesize a reasoning menu when ACP omits it", () => { | 76 | 170 | expect( | 77 | 171 | buildGrokModelCapabilities({ | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/Layers/GrokProvider.ts +67-24Lines changed: 67 additions & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,7 +18,6 @@ import { createModelCapabilities } from "@t3tools/shared/model"; | 18 | 18 | import { resolveSpawnCommand } from "@t3tools/shared/shell"; | 19 | 19 |
| 20 | 20 | import { | 21 | | - buildSelectOptionDescriptor, | 22 | 21 | buildServerProvider, | 23 | 22 | isCommandMissingCause, | 24 | 23 | parseGenericCliVersion, | @@ -30,7 +29,11 @@ import { | 30 | 29 | enrichProviderSnapshotWithVersionAdvisory, | 31 | 30 | type ProviderMaintenanceCapabilities, | 32 | 31 | } from "../providerMaintenance.ts"; | 33 | | -import { makeGrokAcpRuntime, resolveGrokAcpBaseModelId } from "../acp/GrokAcpSupport.ts"; | | 32 | +import { | | 33 | + isValidGrokReasoningEffortToken, | | 34 | + makeGrokAcpRuntime, | | 35 | + resolveGrokAcpBaseModelId, | | 36 | +} from "../acp/GrokAcpSupport.ts"; | 34 | 37 |
| 35 | 38 | const GROK_PRESENTATION = { | 36 | 39 | displayName: "Grok", | @@ -108,51 +111,91 @@ function isRecord(value: unknown): value is Record<string, unknown> { | 108 | 111 | return typeof value === "object" && value !== null && !Array.isArray(value); | 109 | 112 | } | 110 | 113 |
| 111 | | -function grokReasoningOptionsFromModel( | 112 | | - model: EffectAcpSchema.ModelInfo, | 113 | | -): ReadonlyArray<{ value: string; label: string; isDefault?: boolean }> { | | 114 | +function grokReasoningOptionsFromModel(model: EffectAcpSchema.ModelInfo): { | | 115 | + readonly options: ReadonlyArray<{ | | 116 | + value: string; | | 117 | + label: string; | | 118 | + description?: string; | | 119 | + isDefault?: boolean; | | 120 | + }>; | | 121 | + readonly currentValue: string | undefined; | | 122 | +} { | 114 | 123 | const meta = model._meta; | 115 | | - if (!meta || meta.supportsReasoningEffort !== true) { | 116 | | - return []; | | 124 | + if (!meta || meta.supportsReasoningEffort === false) { | | 125 | + return { options: [], currentValue: undefined }; | 117 | 126 | } | 118 | 127 |
| 119 | | - const defaultEffort = nonEmptyString(meta.reasoningEffort); | | 128 | + const currentEffort = nonEmptyString(meta.reasoningEffort); | 120 | 129 | const advertisedOptions = Array.isArray(meta.reasoningEfforts) ? meta.reasoningEfforts : []; | 121 | 130 | const seen = new Set<string>(); | 122 | | - const options: Array<{ value: string; label: string; advertisedDefault: boolean }> = []; | | 131 | + const options: Array<{ | | 132 | + value: string; | | 133 | + label: string; | | 134 | + description?: string; | | 135 | + advertisedDefault: boolean; | | 136 | + }> = []; | 123 | 137 |
| 124 | 138 | for (const entry of advertisedOptions) { | 125 | | - const value = nonEmptyString(isRecord(entry) ? (entry.value ?? entry.id) : entry); | 126 | | - if (!value || seen.has(value)) { | | 139 | + if (!isRecord(entry)) { | | 140 | + continue; | | 141 | + } | | 142 | + const rawValue = nonEmptyString(entry.value); | | 143 | + const rawId = nonEmptyString(entry.id); | | 144 | + const value = | | 145 | + rawValue && isValidGrokReasoningEffortToken(rawValue) | | 146 | + ? rawValue | | 147 | + : rawId && isValidGrokReasoningEffortToken(rawId) | | 148 | + ? rawId | | 149 | + : undefined; | | 150 | + if (value === undefined || seen.has(value)) { | 127 | 151 | continue; | 128 | 152 | } | 129 | 153 | seen.add(value); | | 154 | + const description = nonEmptyString(entry.description); | 130 | 155 | options.push({ | 131 | 156 | value, | 132 | | - label: nonEmptyString(isRecord(entry) ? entry.label : undefined) ?? value, | 133 | | - advertisedDefault: isRecord(entry) && entry.default === true, | | 157 | + label: nonEmptyString(entry.label) ?? value, | | 158 | + ...(description ? { description } : {}), | | 159 | + advertisedDefault: entry.default === true || entry.isDefault === true, | 134 | 160 | }); | 135 | 161 | } | 136 | 162 |
| | 163 | + const currentValue = | | 164 | + currentEffort && options.some((option) => option.value === currentEffort) | | 165 | + ? currentEffort | | 166 | + : undefined; | | 167 | + const advertisedDefaults = options.filter((option) => option.advertisedDefault); | 137 | 168 | const selectedDefault = | 138 | | - (defaultEffort && options.some((option) => option.value === defaultEffort) | 139 | | - ? defaultEffort | 140 | | - : undefined) ?? options.find((option) => option.advertisedDefault)?.value; | 141 | | - return options.map(({ value, label }) => | 142 | | - value === selectedDefault ? { value, label, isDefault: true } : { value, label }, | 143 | | - ); | | 169 | + advertisedDefaults.find((option) => option.value === currentValue)?.value ?? | | 170 | + advertisedDefaults[0]?.value; | | 171 | + return { | | 172 | + options: options.map(({ value, label, description }) => ({ | | 173 | + value, | | 174 | + label, | | 175 | + ...(description ? { description } : {}), | | 176 | + ...(value === selectedDefault ? { isDefault: true } : {}), | | 177 | + })), | | 178 | + currentValue: currentValue ?? selectedDefault, | | 179 | + }; | 144 | 180 | } | 145 | 181 |
| 146 | 182 | export function buildGrokModelCapabilities(model: EffectAcpSchema.ModelInfo): ModelCapabilities { | 147 | | - const reasoningOptions = grokReasoningOptionsFromModel(model); | 148 | | - return reasoningOptions.length > 0 | | 183 | + const reasoning = grokReasoningOptionsFromModel(model); | | 184 | + return reasoning.options.length > 0 | 149 | 185 | ? createModelCapabilities({ | 150 | 186 | optionDescriptors: [ | 151 | | - buildSelectOptionDescriptor({ | | 187 | + { | 152 | 188 | id: "reasoningEffort", | 153 | 189 | label: "Reasoning", | 154 | | - options: reasoningOptions, | 155 | | - }), | | 190 | + type: "select", | | 191 | + options: reasoning.options.map((option) => ({ | | 192 | + id: option.value, | | 193 | + label: option.label, | | 194 | + ...(option.description ? { description: option.description } : {}), | | 195 | + ...(option.isDefault ? { isDefault: true } : {}), | | 196 | + })), | | 197 | + ...(reasoning.currentValue ? { currentValue: reasoning.currentValue } : {}), | | 198 | + }, | 156 | 199 | ], | 157 | 200 | }) | 158 | 201 | : EMPTY_CAPABILITIES; | |
|