Skip to content

Commit 3929597

Browse files
committed
fix(grok): set_model effort, plan capture, compact, review findings
1 parent 05f5672 commit 3929597

12 files changed

Lines changed: 902 additions & 261 deletions

‎apps/server/scripts/acp-mock-agent.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ function promptIdFromRequestMeta(
6969
return typeof promptId === "string" && promptId.length > 0 ? promptId : undefined;
7070
}
7171

72+
function promptTextFromRequest(request: AcpSchema.PromptRequest): string {
73+
const parts = Array.isArray(request.prompt) ? request.prompt : [];
74+
return parts
75+
.flatMap((part) =>
76+
part && typeof part === "object" && "type" in part && part.type === "text" && "text" in part
77+
? [String(part.text)]
78+
: [],
79+
)
80+
.join("");
81+
}
82+
7283
function logExit(reason: string): void {
7384
if (!exitLogPath) {
7485
return;
@@ -494,6 +505,21 @@ const program = Effect.gen(function* () {
494505
return yield* AcpError.AcpRequestError.internalError("Mock prompt failure");
495506
}
496507

508+
// Mirror real Grok: `/compact` is handled as a prompt and emits auto_compact_completed.
509+
const promptText = promptTextFromRequest(request).trim();
510+
if (/^\/compact(?:\s|$)/i.test(promptText)) {
511+
writeJsonRpcNotification("_x.ai/session_notification", {
512+
sessionId: requestedSessionId,
513+
update: {
514+
sessionUpdate: "auto_compact_completed",
515+
tokens_before: 12_000,
516+
tokens_after: 4_000,
517+
summary_preview: null,
518+
},
519+
});
520+
return { stopReason: "end_turn" };
521+
}
522+
497523
if (emitStaleXAiPromptCompleteBeforeSecondHang && promptCount === 1) {
498524
return {
499525
stopReason: "end_turn",

‎apps/server/src/provider/Drivers/GrokDriver.ts‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
buildInitialGrokProviderSnapshot,
2121
checkGrokProviderStatus,
2222
enrichGrokSnapshot,
23+
ensureGrokStaticSlashCommands,
2324
mapAcpCommandsToCatalog,
2425
} from "../Layers/GrokProvider.ts";
2526
import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
@@ -111,6 +112,7 @@ export const GrokDriver: ProviderDriver<GrokSettings, GrokDriverEnv> = {
111112
});
112113

113114
const commandCatalogRef = yield* Ref.make({
115+
received: false,
114116
slashCommands: [] as ServerProvider["slashCommands"],
115117
skills: [] as ServerProvider["skills"],
116118
});
@@ -123,12 +125,22 @@ export const GrokDriver: ProviderDriver<GrokSettings, GrokDriverEnv> = {
123125

124126
const mergeCommandCatalog = (snapshot: ServerProvider): Effect.Effect<ServerProvider> =>
125127
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-
})),
128+
Effect.map((catalog) => {
129+
// Until a live catalog arrives, keep discovery/probe catalogs on the snapshot.
130+
if (!catalog.received) {
131+
return {
132+
...snapshot,
133+
slashCommands: ensureGrokStaticSlashCommands(snapshot.slashCommands),
134+
};
135+
}
136+
// Once received, apply even when empty so clients clear stale entries,
137+
// but always re-attach static commands (e.g. compact) if missing.
138+
return {
139+
...snapshot,
140+
slashCommands: ensureGrokStaticSlashCommands(catalog.slashCommands),
141+
skills: catalog.skills,
142+
};
143+
}),
132144
);
133145

134146
const adapter = yield* makeGrokAdapter(effectiveConfig, {
@@ -139,7 +151,8 @@ export const GrokDriver: ProviderDriver<GrokSettings, GrokDriverEnv> = {
139151
Effect.gen(function* () {
140152
const catalog = mapAcpCommandsToCatalog(commands);
141153
yield* Ref.set(commandCatalogRef, {
142-
slashCommands: catalog.slashCommands,
154+
received: true,
155+
slashCommands: ensureGrokStaticSlashCommands(catalog.slashCommands),
143156
skills: catalog.skills,
144157
});
145158
yield* PubSub.publish(commandCatalogChanges, undefined);

‎apps/server/src/provider/Layers/GrokAdapter.test.ts‎

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,10 +1335,10 @@ it.layer(grokAdapterTestLayer)("GrokAdapterLive", (it) => {
13351335
);
13361336

13371337
it.effect(
1338-
"restarts the process with --reasoning-effort and resumes the ACP session when effort changes",
1338+
"applies mid-thread effort via session/set_model _meta without process restart",
13391339
() =>
13401340
Effect.gen(function* () {
1341-
const threadId = ThreadId.make("grok-effort-process-restart");
1341+
const threadId = ThreadId.make("grok-effort-set-model-meta");
13421342
const tmpDir = yield* Effect.promise(() =>
13431343
NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "grok-effort-")),
13441344
);
@@ -1348,18 +1348,22 @@ it.layer(grokAdapterTestLayer)("GrokAdapterLive", (it) => {
13481348
makeMockGrokWrapper(
13491349
{
13501350
T3_ACP_REQUEST_LOG_PATH: requestLogPath,
1351-
T3_ACP_EMIT_LOAD_REPLAY: "1",
13521351
},
13531352
{ argvLogPath },
13541353
),
13551354
);
13561355
const adapter = yield* makeTestAdapter(wrapperPath);
13571356
const turnCompleted = yield* Deferred.make<void>();
1358-
const eventsFiber = yield* Stream.runForEach(adapter.streamEvents, (event) =>
1359-
event.type === "turn.completed" && String(event.threadId) === String(threadId)
1360-
? Deferred.succeed(turnCompleted, undefined).pipe(Effect.ignore)
1361-
: Effect.void,
1362-
).pipe(Effect.forkChild);
1357+
const sessionExited = yield* Deferred.make<void>();
1358+
const eventsFiber = yield* Stream.runForEach(adapter.streamEvents, (event) => {
1359+
if (event.type === "turn.completed" && String(event.threadId) === String(threadId)) {
1360+
return Deferred.succeed(turnCompleted, undefined).pipe(Effect.ignore);
1361+
}
1362+
if (event.type === "session.exited" && String(event.threadId) === String(threadId)) {
1363+
return Deferred.succeed(sessionExited, undefined).pipe(Effect.ignore);
1364+
}
1365+
return Effect.void;
1366+
}).pipe(Effect.forkChild);
13631367

13641368
const instanceId = ProviderInstanceId.make("grok");
13651369
yield* adapter.startSession({
@@ -1391,82 +1395,88 @@ it.layer(grokAdapterTestLayer)("GrokAdapterLive", (it) => {
13911395

13921396
yield* Deferred.await(turnCompleted);
13931397

1394-
const argvAfter = yield* waitForFileContent(argvLogPath, 40, "--reasoning-effort low");
1398+
// Process must not restart: still a single argv line with the spawn effort.
1399+
const argvAfter = yield* waitForFileContent(argvLogPath, 10, "--reasoning-effort high");
13951400
const argvLines = argvAfter
13961401
.split("\n")
13971402
.map((line) => line.trim())
13981403
.filter((line) => line.length > 0);
1399-
assert.isAtLeast(argvLines.length, 2);
1404+
assert.equal(argvLines.length, 1);
14001405
assert.include(argvLines[0] ?? "", "--reasoning-effort high");
1401-
assert.include(argvLines[argvLines.length - 1] ?? "", "--reasoning-effort low");
14021406

1403-
const requestLog = yield* waitForFileContent(requestLogPath, 40, "session/load");
1404-
assert.include(requestLog, "session/load");
1407+
const requestLog = yield* waitForFileContent(requestLogPath, 40, "session/set_model");
1408+
assert.include(requestLog, "session/set_model");
1409+
assert.include(requestLog, "reasoningEffort");
1410+
assert.include(requestLog, "low");
1411+
assert.notInclude(requestLog, "session/load");
1412+
1413+
const exitedRace = yield* Deferred.await(sessionExited).pipe(
1414+
Effect.timeoutOption("100 millis"),
1415+
);
1416+
assert.isTrue(exitedRace._tag === "None");
14051417

14061418
yield* Fiber.interrupt(eventsFiber);
14071419
yield* adapter.stopSession(threadId);
14081420
}).pipe(TestClock.withLive),
14091421
);
14101422

1411-
it.effect("rejects effort change while a turn is still running", () =>
1423+
it.effect("maps /compact session notifications to thread.state.changed compacted", () =>
14121424
Effect.gen(function* () {
1413-
const threadId = ThreadId.make("grok-effort-busy");
1414-
const wrapperPath = yield* Effect.promise(() =>
1415-
makeMockGrokWrapper({ T3_ACP_HANG_PROMPT_FOREVER: "1" }),
1416-
);
1425+
const threadId = ThreadId.make("grok-compact-thread");
1426+
const wrapperPath = yield* Effect.promise(() => makeMockGrokWrapper());
14171427
const adapter = yield* makeTestAdapter(wrapperPath);
1418-
const instanceId = ProviderInstanceId.make("grok");
1419-
const turnStarted = yield* Deferred.make<TurnId>();
1420-
const eventsFiber = yield* Stream.runForEach(adapter.streamEvents, (event) =>
1421-
event.type === "turn.started" &&
1422-
event.turnId !== undefined &&
1423-
String(event.threadId) === String(threadId)
1424-
? Deferred.succeed(turnStarted, event.turnId).pipe(Effect.asVoid)
1425-
: Effect.void,
1428+
1429+
const runtimeEvents: Array<{ type: string; payload?: unknown; raw?: unknown }> = [];
1430+
const compacted = yield* Deferred.make<void>();
1431+
const turnCompleted = yield* Deferred.make<void>();
1432+
const runtimeEventsFiber = yield* Stream.runForEach(adapter.streamEvents, (event) =>
1433+
Effect.sync(() => {
1434+
runtimeEvents.push(event);
1435+
}).pipe(
1436+
Effect.andThen(
1437+
event.type === "thread.state.changed" && event.payload.state === "compacted"
1438+
? Deferred.succeed(compacted, undefined)
1439+
: event.type === "turn.completed"
1440+
? Deferred.succeed(turnCompleted, undefined)
1441+
: Effect.void,
1442+
),
1443+
),
14261444
).pipe(Effect.forkChild);
14271445

14281446
yield* adapter.startSession({
14291447
threadId,
14301448
provider: ProviderDriverKind.make("grok"),
14311449
cwd: process.cwd(),
14321450
runtimeMode: "full-access",
1433-
modelSelection: {
1434-
instanceId,
1435-
model: "grok-mock-alt",
1436-
options: [{ id: "reasoningEffort", value: "high" }],
1437-
},
14381451
});
14391452

1440-
const hangingTurn = yield* adapter
1441-
.sendTurn({
1442-
threadId,
1443-
input: "hang please",
1444-
attachments: [],
1445-
})
1446-
.pipe(Effect.forkChild);
1453+
yield* adapter.sendTurn({
1454+
threadId,
1455+
input: "/compact keep the auth details",
1456+
attachments: [],
1457+
});
14471458

1448-
const turnId = yield* Deferred.await(turnStarted).pipe(Effect.timeout("2 seconds"));
1459+
yield* Deferred.await(compacted).pipe(Effect.timeout("3 seconds"));
1460+
yield* Deferred.await(turnCompleted).pipe(Effect.timeout("3 seconds"));
14491461

1450-
const failure = yield* Effect.flip(
1451-
adapter.sendTurn({
1452-
threadId,
1453-
input: "change effort while busy",
1454-
attachments: [],
1455-
modelSelection: {
1456-
instanceId,
1457-
model: "grok-mock-alt",
1458-
options: [{ id: "reasoningEffort", value: "low" }],
1459-
},
1460-
}),
1462+
const compactEvent = runtimeEvents.find(
1463+
(event) =>
1464+
event.type === "thread.state.changed" &&
1465+
(event.payload as { state?: string } | undefined)?.state === "compacted",
14611466
);
1462-
assert.equal(failure._tag, "ProviderAdapterValidationError");
1463-
if (failure._tag === "ProviderAdapterValidationError") {
1464-
assert.include(failure.issue, "reasoning effort");
1467+
assert.isDefined(compactEvent);
1468+
if (compactEvent?.type === "thread.state.changed") {
1469+
assert.deepEqual(compactEvent.payload.detail, {
1470+
tokensBefore: 12_000,
1471+
tokensAfter: 4_000,
1472+
});
1473+
assert.equal(
1474+
(compactEvent as { raw?: { method?: string } }).raw?.method,
1475+
"_x.ai/session_notification",
1476+
);
14651477
}
14661478

1467-
yield* adapter.interruptTurn(threadId, turnId).pipe(Effect.timeout("2 seconds"));
1468-
yield* Fiber.join(hangingTurn).pipe(Effect.timeout("2 seconds"));
1469-
yield* Fiber.interrupt(eventsFiber);
1479+
yield* Fiber.interrupt(runtimeEventsFiber);
14701480
yield* adapter.stopSession(threadId);
14711481
}).pipe(TestClock.withLive),
14721482
);

0 commit comments

Comments
 (0)