Skip to content

Commit 843f4e3

Browse files
fix(grok): invoke leading picked skills through native commands (#24605)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent c4fa462 commit 843f4e3

5 files changed

Lines changed: 149 additions & 4 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import * as EffectAcpErrors from "effect-acp/errors";
5353
import type * as EffectAcpProtocol from "effect-acp/protocol";
5454
import type * as EffectAcpSchema from "effect-acp/compat";
5555

56+
import { writeFakeCli } from "@t3tools/provider-testing/fakeCli";
5657
import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
5758
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
5859
import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
@@ -3169,6 +3170,89 @@ describe("AcpAdapterV2", () => {
31693170
}).pipe(Effect.provide(layerTest), Effect.scoped),
31703171
);
31713172

3173+
// Oracle: #13457. Grok expands only a leading `/name`, so a picked leading `$name`
3174+
// must reach it in that form; anything else keeps the user's text.
3175+
it.live.each([
3176+
{ text: "$deploy ship it", sent: "/deploy ship it", native: true },
3177+
{ text: "ship $deploy", sent: "ship $deploy", native: false },
3178+
{ text: "$other ship it", sent: "$other ship it", native: false },
3179+
])("Grok sends $text with a picked skill as $sent", ({ text, sent, native }) =>
3180+
Effect.gen(function* () {
3181+
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
3182+
const fileSystem = yield* FileSystem.FileSystem;
3183+
const path = yield* Path.Path;
3184+
const mockAgentPath = yield* path.fromFileUrl(
3185+
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
3186+
);
3187+
const hostPlatform = yield* HostProcess.Platform;
3188+
// Stands in for `grok inspect --json`, the catalog behind the `$` picker.
3189+
const catalog = { skills: [{ name: "deploy", source: { path: "/s/deploy/SKILL.md" } }] };
3190+
const grokPath = writeFakeCli({
3191+
directory: yield* fileSystem.makeTempDirectoryScoped(),
3192+
name: "grok",
3193+
source: `process.stdout.write(${JSON.stringify(`${JSON.stringify(catalog)}\n`)});\n`,
3194+
platform: hostPlatform,
3195+
});
3196+
const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
3197+
const instanceId = ProviderInstanceId.make("grok-skill-mention");
3198+
const adapter = yield* makeGrokAdapterV2({
3199+
instanceId,
3200+
settings: { ...DEFAULT_GROK_SETTINGS, binaryPath: grokPath },
3201+
// The fake CLI needs `node` on its PATH.
3202+
environment: { ...process.env },
3203+
hostPlatform,
3204+
selfInvocation: yield* resolveSelfInvocation(),
3205+
makeRuntime: (input) =>
3206+
makeMockRuntime({ childProcessSpawner, mockAgentPath, protocolEvents })(input).pipe(
3207+
Effect.flatMap(makeXAiPromptCompletionRuntime),
3208+
),
3209+
});
3210+
const threadId = ThreadId.make("grok-skill-mention");
3211+
const modelSelection = { instanceId, model: "default" } as const;
3212+
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3213+
runtimeMode: "full-access",
3214+
interactionMode: "default",
3215+
cwd: process.cwd(),
3216+
});
3217+
const runtime = yield* adapter.openSession({
3218+
threadId,
3219+
providerSessionId: ProviderSessionId.make("grok-skill-mention"),
3220+
modelSelection,
3221+
runtimePolicy,
3222+
});
3223+
const providerThread = yield* runtime.ensureThread({
3224+
threadId,
3225+
modelSelection,
3226+
runtimePolicy,
3227+
});
3228+
yield* runtime.startTurn(
3229+
makeTurnInput({
3230+
threadId,
3231+
providerThread,
3232+
instanceId,
3233+
runtimePolicy,
3234+
now: yield* DateTime.now,
3235+
messageText: text,
3236+
}),
3237+
);
3238+
const prompt = yield* Stream.fromQueue(protocolEvents).pipe(
3239+
Stream.filter(
3240+
(event) =>
3241+
event.direction === "outgoing" && rawProtocolMethod(event) === "session/prompt",
3242+
),
3243+
Stream.runHead,
3244+
);
3245+
const blocks = rawProtocolPromptTextBlocks(Option.getOrThrow(prompt));
3246+
if (native) {
3247+
// Same shape as a typed native command: one bare text block, no T3 instructions.
3248+
assert.deepEqual(blocks, [{ type: "text", text: sent }]);
3249+
} else {
3250+
assert.isTrue(blocks.some((block) => block.text.includes(sent)));
3251+
assert.isFalse(blocks.some((block) => block.text.trimStart().startsWith("/")));
3252+
}
3253+
}).pipe(Effect.provide(layerTest), Effect.scoped),
3254+
);
3255+
31723256
// Oracle: #15888 KillTask reports. Command completion must release the turn even
31733257
// when the CLI keeps the prompt RPC and process alive. Kill calls stay visible.
31743258
it.live.each([

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ class AcpModelSelectionError extends Schema.TaggedError<AcpModelSelectionError>(
225225

226226
export interface AcpAdapterV2Flavor {
227227
readonly promptFailure?: (cause: unknown) => OrchestrationV2ProviderFailure;
228+
/**
229+
* Runs before attachment paths, T3 instructions and native slash-command detection,
230+
* so a rewrite can turn the user's text into a native command.
231+
*/
232+
readonly rewriteUserText?: (
233+
text: string,
234+
turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
235+
) => Effect.Effect<string, never, ChildProcessSpawner.ChildProcessSpawner>;
228236
readonly driver: ProviderDriverKind;
229237
readonly capabilities: OrchestrationV2ProviderCapabilities;
230238
readonly clientCapabilitiesMeta?: NonNullable<
@@ -7162,12 +7170,23 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
71627170
} satisfies T3AcpInstructionState;
71637171
const previousInstructionState = (yield* Ref.get(promptInstructionStates)).get(sessionId);
71647172
const compactionMarker = (yield* Ref.get(compactionMarkers)).get(sessionId);
7173+
const userText =
7174+
flavor.rewriteUserText === undefined
7175+
? turnInput.message.text
7176+
: yield* flavor
7177+
.rewriteUserText(turnInput.message.text, turnInput)
7178+
.pipe(
7179+
Effect.provideService(
7180+
ChildProcessSpawner.ChildProcessSpawner,
7181+
childProcessSpawner,
7182+
),
7183+
);
71657184
const messageText = providerMessageTextWithAttachmentPaths({
7166-
text: turnInput.message.text,
7185+
text: userText,
71677186
attachments: turnInput.message.attachments,
71687187
resolveAttachmentPath: host.resolveAttachmentPath,
71697188
});
7170-
const isNativeSlashCommand = turnInput.message.text.trimStart().startsWith("/");
7189+
const isNativeSlashCommand = userText.trimStart().startsWith("/");
71717190
const text = t3AcpPromptWithInstructions({
71727191
prompt: messageText,
71737192
state: instructionState,

‎packages/provider-grok/src/server/adapter.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
isXAiTaskCompletedWakeNotification,
55
xAiRateLimitedErrorCode,
66
} from "./xaiAcpExtension.ts";
7+
import { discoverGrokSkills, leadingGrokSkillMention } from "./skills.ts";
78
import * as HostProcess from "@t3tools/shared/HostProcess";
89
import { resolveSelfInvocation, type SelfInvocation } from "@t3tools/shared/nodeRuntime";
910
import {
@@ -251,6 +252,24 @@ export function makeGrokAcpAdapterFlavor(
251252
// still accepts image content blocks (verified with real screenshots).
252253
supportsImagePrompts: true,
253254
supportsCompaction: true,
255+
// Grok expands only a leading `/name`, while the `$` picker inserts `$name` from the
256+
// `grok inspect` catalog. Ask that catalog again so other `$` text is left alone.
257+
rewriteUserText: (text, turnInput) => {
258+
const mention = leadingGrokSkillMention(text);
259+
if (mention === undefined) return Effect.succeed(text);
260+
return discoverGrokSkills(
261+
options.settings,
262+
options.environment,
263+
turnInput.runtimePolicy.cwd ?? undefined,
264+
).pipe(
265+
Effect.map((skills) =>
266+
skills.some((skill) => skill.enabled && skill.name === mention.name)
267+
? `/${mention.name}${mention.rest}`
268+
: text,
269+
),
270+
Effect.orElseSucceed(() => text),
271+
);
272+
},
254273
resolveModelId: (selection) => resolveGrokAcpBaseModelId(selection.model),
255274
getModelContextWindow: (selection, session) => {
256275
if (selection.instanceId !== options.instanceId) return undefined;

‎packages/provider-grok/src/server/skills.test.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { describe, expect, it } from "@effect/vitest";
1+
import { assert, describe, expect, it } from "@effect/vitest";
22
import * as Effect from "effect/Effect";
33
import * as Sink from "effect/Sink";
44
import * as Stream from "effect/Stream";
55
import type * as ChildProcess from "effect/process/ChildProcess";
66
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
77

8-
import { discoverGrokSkills } from "./skills.ts";
8+
import { discoverGrokSkills, leadingGrokSkillMention } from "./skills.ts";
99

1010
const inspectPayload = (skills: ReadonlyArray<unknown>) => JSON.stringify({ skills });
1111

@@ -199,3 +199,14 @@ describe("discoverGrokSkills", () => {
199199
});
200200
});
201201
});
202+
203+
describe("leadingGrokSkillMention", () => {
204+
it.each([
205+
{ text: " €deploy\nship it", mention: { name: "deploy", rest: "\nship it" } },
206+
{ text: "$deploy", mention: { name: "deploy", rest: "" } },
207+
{ text: "$25 off", mention: undefined },
208+
{ text: "$deploy, then ship", mention: undefined },
209+
])("reads $text", ({ text, mention }) => {
210+
assert.deepEqual(leadingGrokSkillMention(text), mention);
211+
});
212+
});

‎packages/provider-grok/src/server/skills.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@ import * as Effect from "effect/Effect";
99
import * as Option from "effect/Option";
1010
import * as Schema from "effect/Schema";
1111
import * as ChildProcess from "effect/process/ChildProcess";
12+
import { SKILL_MENTION_PATTERN } from "@t3tools/shared/composerInlineTokens";
1213
import { resolveSpawnCommand } from "@t3tools/shared/shell";
1314

1415
import { spawnAndCollect } from "@t3tools/provider-core/server/snapshotProbe";
1516
import { grokProbeEnvironment } from "./probeEnvironment.ts";
1617

1718
const GROK_SKILLS_PROBE_TIMEOUT_MS = 4_000;
19+
// The composer's chip rule, anchored to the message start, so `$25` never names a skill.
20+
const LEADING_SKILL_MENTION = new RegExp(`^\\s*${SKILL_MENTION_PATTERN.source}`, "u");
21+
22+
/** A message opening with a mention, such as `deploy` and ` ship it` for `$deploy ship it`. */
23+
export function leadingGrokSkillMention(
24+
text: string,
25+
): { readonly name: string; readonly rest: string } | undefined {
26+
const match = LEADING_SKILL_MENTION.exec(text);
27+
if (match?.[2] === undefined) return undefined;
28+
return { name: match[2], rest: text.slice(match[0].length) };
29+
}
1830

1931
class GrokSkillsProbeError extends Schema.TaggedError<GrokSkillsProbeError>()(
2032
"GrokSkillsProbeError",

0 commit comments

Comments
 (0)