Skip to content

Commit 4a6c1ba

Browse files
fix(server): ACP mode states with null descriptions are no longer dropped
Grok advertises its session modes in `initialize._meta.modeState`, which reaches us untyped. A hand-written guard checked it and rejected a mode whose `description` was `null`. The ACP schema allows that value, so one such entry silently threw away the whole mode list. Both initialize guards (mode state and model state) are now `Schema.is` over the effect-acp schemas: the v1 `SessionModeState` (modes are gone from ACP v2) and the compat `SessionModelState` the session RPCs already decode with. Valid states that passed before still pass. Malformed entries are still rejected, and so is a non-object `_meta` on the state, which the old guards never checked. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 5eb8773 commit 4a6c1ba

2 files changed

Lines changed: 59 additions & 34 deletions

File tree

‎apps/server/src/provider/acp/AcpRuntimeModel.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
parsePermissionRequest,
1414
parseSessionModeState,
1515
parseSessionUpdateEvent,
16+
sessionModelStateFromInitialize,
1617
sessionUpdateCountsAsLoadReplayActivity,
1718
sessionUpdateIsReplay,
1819
syntheticLoadSessionResponseFromInitialize,
@@ -216,6 +217,57 @@ describe("AcpRuntimeModel", () => {
216217
expect(response.modes?.availableModes).toHaveLength(2);
217218
});
218219

220+
it("keeps initialize mode state whose modes have null descriptions", () => {
221+
const response = syntheticLoadSessionResponseFromInitialize({
222+
protocolVersion: 1,
223+
_meta: {
224+
modeState: {
225+
currentModeId: "code",
226+
availableModes: [
227+
{ id: "ask", name: "Ask", description: null },
228+
{ id: "code", name: "Code", description: "Edit files", _meta: null },
229+
],
230+
_meta: null,
231+
},
232+
},
233+
} satisfies EffectAcpSchema.InitializeResponse);
234+
235+
expect(parseSessionModeState(response)).toEqual({
236+
currentModeId: "code",
237+
availableModes: [
238+
{ id: "ask", name: "Ask" },
239+
{ id: "code", name: "Code", description: "Edit files" },
240+
],
241+
});
242+
});
243+
244+
it("reads initialize model state and ignores malformed model state", () => {
245+
const modelState = {
246+
currentModelId: "grok-4.7",
247+
availableModels: [
248+
{
249+
modelId: "grok-4.7",
250+
name: "Grok 4.7",
251+
description: null,
252+
_meta: { reasoningEfforts: [{ id: "high", default: true }] },
253+
},
254+
{ modelId: "grok-4.6", name: "Grok 4.6" },
255+
],
256+
};
257+
258+
expect(sessionModelStateFromInitialize({ protocolVersion: 1, _meta: { modelState } })).toEqual(
259+
modelState,
260+
);
261+
expect(
262+
sessionModelStateFromInitialize({
263+
protocolVersion: 1,
264+
_meta: {
265+
modelState: { currentModelId: "grok-4.7", availableModels: [{ modelId: "grok-4.7" }] },
266+
},
267+
}),
268+
).toBeUndefined();
269+
});
270+
219271
it("projects typed ACP tool call updates into runtime events", () => {
220272
const created = parseSessionUpdateEvent({
221273
sessionId: "session-1",

‎apps/server/src/provider/acp/AcpRuntimeModel.ts‎

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import * as Duration from "effect/Duration";
33
import * as Effect from "effect/Effect";
44
import * as Option from "effect/Option";
55
import * as Ref from "effect/Ref";
6-
import type * as EffectAcpSchema from "effect-acp/compat";
6+
import * as Schema from "effect/Schema";
7+
import * as EffectAcpSchema from "effect-acp/compat";
8+
import * as EffectAcpSchemaV1 from "effect-acp/schema-v1";
79
import {
810
deriveToolActivityPresentation,
911
mergeToolActivityData,
@@ -19,39 +21,10 @@ function isRecord(value: unknown): value is Record<string, unknown> {
1921
return typeof value === "object" && value !== null && !Array.isArray(value);
2022
}
2123

22-
function isSessionModelState(value: unknown): value is EffectAcpSchema.SessionModelState {
23-
if (!isRecord(value) || typeof value.currentModelId !== "string") {
24-
return false;
25-
}
26-
if (!Array.isArray(value.availableModels)) {
27-
return false;
28-
}
29-
return value.availableModels.every(
30-
(model) =>
31-
isRecord(model) &&
32-
typeof model.modelId === "string" &&
33-
typeof model.name === "string" &&
34-
(model.description === undefined ||
35-
model.description === null ||
36-
typeof model.description === "string"),
37-
);
38-
}
39-
40-
function isSessionModeState(value: unknown): value is EffectAcpSchema.SessionModeState {
41-
if (!isRecord(value) || typeof value.currentModeId !== "string") {
42-
return false;
43-
}
44-
if (!Array.isArray(value.availableModes)) {
45-
return false;
46-
}
47-
return value.availableModes.every(
48-
(mode) =>
49-
isRecord(mode) &&
50-
typeof mode.id === "string" &&
51-
typeof mode.name === "string" &&
52-
(mode.description === undefined || typeof mode.description === "string"),
53-
);
54-
}
24+
// Guards for the untyped `initialize._meta` states some agents (Grok) advertise.
25+
// Modes were removed from ACP v2, so the v1 wire schema is the source of truth.
26+
const isSessionModelState = Schema.is(EffectAcpSchema.SessionModelState);
27+
const isSessionModeState = Schema.is(EffectAcpSchemaV1.SessionModeState);
5528

5629
export interface AcpSessionMode {
5730
readonly id: string;

0 commit comments

Comments
 (0)