feat(grok): native ACP parity (catalog, effort, usage, plan, multi-agent, compact) #5423
ClosedEnzoTironi(GitHub profile, opens in a new tab)wants to merge 5 commits into
main fromrefs/pull/5423/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/server/src/provider/Layers/GrokProvider.ts lines 284 to 286OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -137,4 +271,16 @@137271 clientInfo: { name: "t3-code-provider-probe", version: "0.0.0" },138272 });139273 const started = yield* acp.start();140- return buildGrokDiscoveredModelsFromSessionModelState(started.sessionSetupResult.models);274+ // Prefer modelState from session setup; fall back to initialize _meta (live wire).275+ const initializeMeta =276+ started.initializeResult._meta &&277+ typeof started.initializeResult._meta === "object" &&278+ !Array.isArray(started.initializeResult._meta)279+ ? (started.initializeResult._meta as Record<string, unknown>)280+ : undefined;281+ const modelsFromSession = buildGrokDiscoveredModelsFromSessionModelState(282+ started.sessionSetupResult.models,283+ );284+ const modelsFromInitialize = buildGrokDiscoveredModelsFromSessionModelState(285+ initializeMeta?.modelState as EffectAcpSchema.SessionModelState | undefined,286+ );apps/server/src/provider/Drivers/GrokDriver.ts lines 126 to 144Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -108 +112,21 @@108112113+ const commandCatalogRef = yield* Ref.make({114+ slashCommands: [] as ServerProvider["slashCommands"],115+ skills: [] as ServerProvider["skills"],116+ });117+ // Live available_commands_update (and initialize meta seed) must push a118+ // snapshot change so clients do not wait for the next health probe.119+ const commandCatalogChanges = yield* Effect.acquireRelease(120+ PubSub.unbounded<void>(),121+ PubSub.shutdown,122+ );123+124+ const mergeCommandCatalog = (snapshot: ServerProvider): Effect.Effect<ServerProvider> =>125+ Ref.get(commandCatalogRef).pipe(126+ Effect.map((catalog) => ({127+ ...snapshot,128+ slashCommands:129+ catalog.slashCommands.length > 0 ? catalog.slashCommands : snapshot.slashCommands,130+ skills: catalog.skills.length > 0 ? catalog.skills : snapshot.skills,131+ })),132+ );apps/server/src/provider/Layers/GrokAdapter.ts lines 284 to 289OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -181,0 +269,21 @@269+/** Live Grok model `_meta.reasoningEffort` after process spawn. */270+function reasoningEffortFromMeta(meta: unknown): string | undefined {271+ if (!isRecord(meta)) return undefined;272+ const raw = meta.reasoningEffort ?? meta.reasoning_effort;273+ return typeof raw === "string" && raw.trim() ? raw.trim() : undefined;274+}275+276+function preferredModelMeta(input: {277+ readonly sessionModels: EffectAcpSchema.SessionModelState | null | undefined;278+ readonly initializeMeta: Record<string, unknown> | undefined;279+ readonly preferredModelId: string | undefined;280+}): unknown {281+ const pick = (282+ models: ReadonlyArray<{ modelId: string; _meta?: unknown }> | undefined,283+ preferred: string | undefined,284+ ): unknown => {285+ if (!models || models.length === 0) return undefined;286+ const preferredMatch = preferred287+ ? models.find((model) => model.modelId === preferred)288+ : undefined;289+ return preferredMatch?._meta ?? models[0]?._meta;
- apps/server/src/provider/Layers/GrokAdapter.ts line 1387OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -913,0 +1367,21 @@1367+ }1368+ const restart = {1369+ cwd: ctx.session.cwd,1370+ runtimeMode: ctx.session.runtimeMode,1371+ resumeCursor: ctx.session.resumeCursor,1372+ };1373+ yield* stopSessionInternal(ctx);1374+ return { _tag: "restart" as const, ...restart };1375+ }1376+ return { _tag: "continue" as const };1377+ }),1378+ );1379+ if (restartDecision._tag === "restart") {1380+ yield* startSession({1381+ threadId: input.threadId,1382+ provider: PROVIDER,1383+ cwd: restartDecision.cwd,1384+ runtimeMode: restartDecision.runtimeMode,1385+ ...(restartDecision.resumeCursor ? { resumeCursor: restartDecision.resumeCursor } : {}),1386+ ...(input.modelSelection ? { modelSelection: input.modelSelection } : {}),1387+ });apps/server/src/provider/Layers/GrokAdapter.ts line 1389OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -913,0 +1369,21 @@1369+ cwd: ctx.session.cwd,1370+ runtimeMode: ctx.session.runtimeMode,1371+ resumeCursor: ctx.session.resumeCursor,1372+ };1373+ yield* stopSessionInternal(ctx);1374+ return { _tag: "restart" as const, ...restart };1375+ }1376+ return { _tag: "continue" as const };1377+ }),1378+ );1379+ if (restartDecision._tag === "restart") {1380+ yield* startSession({1381+ threadId: input.threadId,1382+ provider: PROVIDER,1383+ cwd: restartDecision.cwd,1384+ runtimeMode: restartDecision.runtimeMode,1385+ ...(restartDecision.resumeCursor ? { resumeCursor: restartDecision.resumeCursor } : {}),1386+ ...(input.modelSelection ? { modelSelection: input.modelSelection } : {}),1387+ });1388+ }1389+
- apps/server/src/provider/acp/GrokAcpSupport.ts line 220OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -94,9 +201,20 @@201+ * Apply model and/or reasoning effort via Grok ACP `session/set_model`.202+ * Effort is sent as `_meta.reasoningEffort` (Grok private extension).203+ * Calls set_model when the model changes or when an effort selection is present204+ * (effort can change without a model id change). Mid-thread effort no longer205+ * requires a process restart.206+ */94207 export function applyGrokAcpModelSelection<E>(input: {95208 readonly runtime: Pick<AcpSessionRuntime.AcpSessionRuntime["Service"], "setSessionModel">;96209 readonly currentModelId: string | undefined;97210 readonly requestedModelId: string | undefined;211+ readonly selections?: ReadonlyArray<ProviderOptionSelection> | null;98212 readonly mapError: (cause: EffectAcpErrors.AcpError) => E;99213 }): Effect.Effect<string | undefined, E> {214+ const targetModelId = input.requestedModelId ?? input.currentModelId;215+ const reasoningEffort = resolveGrokReasoningEffortSelection(input.selections);100216 const shouldSwitchModel =101217 input.requestedModelId !== undefined && input.requestedModelId !== input.currentModelId;102- if (!shouldSwitchModel) {218+ const shouldApplyEffort = reasoningEffort !== undefined;219+220+ if (!targetModelId || (!shouldSwitchModel && !shouldApplyEffort)) {apps/server/src/provider/Layers/GrokAdapter.ts line 380OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -181,0 +360,21 @@360+ ...(inputHint ? { inputHint } : {}),361+ },362+ ];363+ });364+}365+366+function buildGrokModelContextWindows(input: {367+ readonly sessionModels: EffectAcpSchema.SessionModelState | null | undefined;368+ readonly initializeMeta: Record<string, unknown> | undefined;369+}): Map<string, number> {370+ const windows = new Map<string, number>();371+ const ingest = (models: ReadonlyArray<{ modelId: string; _meta?: unknown }> | undefined) => {372+ if (!models) return;373+ for (const model of models) {374+ const tokens = totalContextTokensFromMeta(model._meta);375+ if (tokens !== undefined && model.modelId.trim()) {376+ windows.set(model.modelId.trim(), tokens);377+ }378+ }379+ };380+ ingest(input.sessionModels?.availableModels);apps/server/src/provider/acp/XAiAcpExtension.ts line 289OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -198,0 +269,21 @@269+ outcome: "abandoned",270+ feedback:271+ feedback ??272+ "The client captured your proposed plan. Stop here and wait for the user's feedback or implementation request in a later turn.",273+ };274+}275+276+/**277+ * True when a path is Grok's session plan file under `~/.grok/sessions/.../plan.md`.278+ * Deliberately does not match workspace files named `plan.md` (e.g. docs/plan.md).279+ */280+export function isGrokPlanMarkdownPath(path: string | undefined | null): boolean {281+ if (typeof path !== "string") {282+ return false;283+ }284+ const normalized = path.trim().replace(/\\/g, "/");285+ if (normalized.length === 0 || !normalized.endsWith("/plan.md")) {286+ return false;287+ }288+ // Session layout: ~/.grok/sessions/<encoded-cwd>/<session-id>/plan.md289+ return normalized.includes("/.grok/sessions/");apps/server/src/provider/acp/GrokAcpSupport.ts line 103Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -47,0 +82,21 @@82+ getProviderOptionStringSelectionValue(selections, GROK_REASONING_EFFORT_OPTION_ID) ??83+ getProviderOptionStringSelectionValue(selections, "reasoning") ??84+ getProviderOptionStringSelectionValue(selections, "effort")85+ );86+}87+88+/**89+ * Grok has no ACP session modes on live 0.2.x. Plan mode is entered via the90+ * `/plan` slash command (see Grok Build user guide). Map T3 interactionMode91+ * onto that command so Plan/Build in the composer does real work.92+ */93+export function applyGrokPlanModeToPromptText(input: {94+ readonly text: string | undefined;95+ readonly interactionMode: "plan" | "default" | undefined;96+}): string | undefined {97+ const trimmed = input.text?.trim();98+ if (!trimmed) {99+ // Plan mode still needs the slash command so Grok enters plan mode.100+ return input.interactionMode === "plan" ? "/plan" : trimmed;101+ }102+ if (input.interactionMode === "plan") {
EnzoTironi(GitHub profile, opens in a new tab) changed the title
- apps/server/src/provider/acp/GrokAcpSupport.ts line 256Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -99,9 +213,18 @@99213 }): Effect.Effect<string | undefined, E> {214+ const targetModelId = input.requestedModelId ?? input.currentModelId;215+ const reasoningEffort = resolveGrokReasoningEffortSelection(input.selections);100216 const shouldSwitchModel =101217 input.requestedModelId !== undefined && input.requestedModelId !== input.currentModelId;102- if (!shouldSwitchModel) {218+ const shouldApplyEffort = reasoningEffort !== undefined;219+220+ if (!targetModelId || (!shouldSwitchModel && !shouldApplyEffort)) {103221 return Effect.succeed(input.currentModelId);104222 }223+224+ const setOptions = shouldApplyEffort225+ ? { _meta: { reasoningEffort } satisfies { readonly [x: string]: unknown } }226+ : undefined;227+105228 return input.runtime106- .setSessionModel(input.requestedModelId)107- .pipe(Effect.mapError(input.mapError), Effect.as(input.requestedModelId));229+ .setSessionModel(targetModelId, setOptions)230+ .pipe(Effect.mapError(input.mapError), Effect.as(targetModelId));
- apps/server/src/provider/Layers/GrokAdapter.ts line 1693Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -952,2 +1667,20 @@1667+ cause,1668+ ),1669+ });1670+ // Only sticky-update effort when set_model had a model target1671+ // (effort-only with no model id is a no-op and must not claim apply).1672+ if (appliedEffort !== undefined && currentModelId !== undefined) {1673+ ctx.processReasoningEffort = appliedEffort;1674+ }9521675953- const text = input.input?.trim();1676+ const text = applyGrokPlanModeToPromptText({1677+ text: input.input?.trim(),1678+ interactionMode: input.interactionMode,1679+ planModeActive: ctx.planModeActive,1680+ });1681+ // Track local plan-mode state so Build can exit with /default.1682+ if (input.interactionMode === "plan") {1683+ ctx.planModeActive = true;1684+ } else if (input.interactionMode === "default" && ctx.planModeActive) {1685+ ctx.planModeActive = false;1686+ }
- @@ -868,4 +1549,21 @@8681549 rawPayload: event.rawPayload,8691550 }),8701551 );8711552 return;1553+ case "UsageUpdated": {1554+ const maxTokens =1555+ event.usage.size > 01556+ ? event.usage.size1557+ : resolveActiveContextWindowTokens(ctx);1558+ yield* offerRuntimeEvent(1559+ makeAcpTokenUsageEvent({1560+ stamp,1561+ provider: PROVIDER,1562+ threadId: ctx.threadId,1563+ turnId: notificationTurnId,1564+ usedTokens: event.usage.used,1565+ ...(maxTokens !== undefined ? { maxTokens } : {}),1566+ rawPayload: event.rawPayload,1567+ }),1568+ );1569+ return;
EnzoTironi(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.






Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
789a0cda-f325-4c3a-8b00-2084830cae0aYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share