Skip to content

Commit b83aa60

Browse files
committed
feat(grok): add /compact slash command and compaction feedback
Grok 4.5 has a large context window but quality drops well before the limit. Grok Build already supports manual /compact; T3 could send the same prompt text but never advertised the command and ignored auto_compact_completed notifications, so users got no feedback. Advertise /compact on Grok provider slash commands and map _x.ai/session_notification auto_compact_completed to thread.state.changed compacted so the existing "Context compacted" work-log row appears.
1 parent 5a84614 commit b83aa60

7 files changed

Lines changed: 245 additions & 0 deletions

File tree

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

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

71+
function promptTextFromRequest(request: AcpSchema.PromptRequest): string {
72+
const blocks = request.prompt;
73+
if (!Array.isArray(blocks)) {
74+
return "";
75+
}
76+
return blocks
77+
.flatMap((block) =>
78+
typeof block === "object" &&
79+
block !== null &&
80+
"type" in block &&
81+
block.type === "text" &&
82+
"text" in block &&
83+
typeof block.text === "string"
84+
? [block.text]
85+
: [],
86+
)
87+
.join("");
88+
}
89+
7190
function logExit(reason: string): void {
7291
if (!exitLogPath) {
7392
return;
@@ -522,6 +541,21 @@ const program = Effect.gen(function* () {
522541
return yield* Effect.never;
523542
}
524543

544+
// Mirror real Grok: `/compact` is handled as a prompt and emits auto_compact_completed.
545+
const promptText = promptTextFromRequest(request).trim();
546+
if (/^\/compact(?:\s|$)/i.test(promptText)) {
547+
writeJsonRpcNotification("_x.ai/session_notification", {
548+
sessionId: requestedSessionId,
549+
update: {
550+
sessionUpdate: "auto_compact_completed",
551+
tokens_before: 12_000,
552+
tokens_after: 4_000,
553+
summary_preview: null,
554+
},
555+
});
556+
return { stopReason: "end_turn" };
557+
}
558+
525559
if (emitXAiPromptCompleteThenHang) {
526560
writeJsonRpcNotification("session/update", {
527561
sessionId: requestedSessionId,

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,63 @@ it.layer(grokAdapterTestLayer)("GrokAdapterLive", (it) => {
188188
}),
189189
);
190190

191+
it.effect("maps /compact session notifications to thread.state.changed compacted", () =>
192+
Effect.gen(function* () {
193+
const threadId = ThreadId.make("grok-compact-thread");
194+
const wrapperPath = yield* Effect.promise(() => makeMockGrokWrapper());
195+
const adapter = yield* makeTestAdapter(wrapperPath);
196+
197+
const runtimeEvents: ProviderRuntimeEvent[] = [];
198+
const compacted = yield* Deferred.make<void>();
199+
const turnCompleted = yield* Deferred.make<void>();
200+
const runtimeEventsFiber = yield* Stream.runForEach(adapter.streamEvents, (event) =>
201+
Effect.sync(() => {
202+
runtimeEvents.push(event);
203+
}).pipe(
204+
Effect.andThen(
205+
event.type === "thread.state.changed" && event.payload.state === "compacted"
206+
? Deferred.succeed(compacted, undefined)
207+
: event.type === "turn.completed"
208+
? Deferred.succeed(turnCompleted, undefined)
209+
: Effect.void,
210+
),
211+
),
212+
).pipe(Effect.forkChild);
213+
214+
yield* adapter.startSession({
215+
threadId,
216+
provider: ProviderDriverKind.make("grok"),
217+
cwd: process.cwd(),
218+
runtimeMode: "full-access",
219+
});
220+
221+
yield* adapter.sendTurn({
222+
threadId,
223+
input: "/compact keep the auth details",
224+
attachments: [],
225+
});
226+
227+
yield* Deferred.await(compacted).pipe(Effect.timeout("3 seconds"));
228+
yield* Deferred.await(turnCompleted).pipe(Effect.timeout("3 seconds"));
229+
yield* Fiber.interrupt(runtimeEventsFiber);
230+
231+
const compactEvent = runtimeEvents.find(
232+
(event): event is Extract<ProviderRuntimeEvent, { type: "thread.state.changed" }> =>
233+
event.type === "thread.state.changed" && event.payload.state === "compacted",
234+
);
235+
assert.isDefined(compactEvent);
236+
if (compactEvent?.type === "thread.state.changed") {
237+
assert.deepEqual(compactEvent.payload.detail, {
238+
tokensBefore: 12_000,
239+
tokensAfter: 4_000,
240+
});
241+
assert.equal(compactEvent.raw?.method, "_x.ai/session_notification");
242+
}
243+
244+
yield* adapter.stopSession(threadId);
245+
}).pipe(TestClock.withLive),
246+
);
247+
191248
it.effect("closes the ACP child process when a session stops", () =>
192249
Effect.gen(function* () {
193250
const threadId = ThreadId.make("grok-stop-session-close");

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ import {
6161
} from "../acp/GrokAcpSupport.ts";
6262
import {
6363
extractXAiAskUserQuestions,
64+
extractXAiAutoCompactCompleted,
6465
makeXAiAskUserQuestionCancelledResponse,
6566
makeXAiAskUserQuestionResponse,
6667
promptResponseHasMissingXAiStopReason,
6768
XAiAskUserQuestionRequest,
69+
XAiSessionNotification,
6870
} from "../acp/XAiAcpExtension.ts";
6971
import { type GrokAdapterShape } from "../Services/GrokAdapter.ts";
7072
import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
@@ -663,6 +665,54 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte
663665
),
664666
{ discard: true },
665667
);
668+
// Manual `/compact` and auto-compact complete as x.ai session notifications.
669+
// Map them to thread.state.changed → UI work log "Context compacted".
670+
yield* Effect.forEach(
671+
["x.ai/session_notification", "_x.ai/session_notification"] as const,
672+
(method) =>
673+
acp.handleExtNotification(method, XAiSessionNotification, (notification) =>
674+
mapAcpCallbackFailure(
675+
Effect.gen(function* () {
676+
yield* logNative(input.threadId, method, notification);
677+
const compact = extractXAiAutoCompactCompleted(notification);
678+
if (!compact) {
679+
return;
680+
}
681+
const live = sessions.get(input.threadId);
682+
if (!live || live.stopped) {
683+
return;
684+
}
685+
const turnId = resolveSessionCallbackTurnId(sessions, input.threadId);
686+
if (turnId !== undefined && live.interruptedTurnIds.has(turnId)) {
687+
return;
688+
}
689+
yield* offerRuntimeEvent({
690+
type: "thread.state.changed",
691+
...(yield* makeEventStamp()),
692+
provider: PROVIDER,
693+
threadId: input.threadId,
694+
turnId,
695+
payload: {
696+
state: "compacted",
697+
detail: {
698+
tokensBefore: compact.tokensBefore,
699+
tokensAfter: compact.tokensAfter,
700+
...(compact.summaryPreview
701+
? { summaryPreview: compact.summaryPreview }
702+
: {}),
703+
},
704+
},
705+
raw: {
706+
source: "acp.grok.extension",
707+
method,
708+
payload: notification,
709+
},
710+
});
711+
}),
712+
),
713+
),
714+
{ discard: true },
715+
);
666716
yield* acp.handleRequestPermission((params) =>
667717
mapAcpCallbackFailure(
668718
Effect.gen(function* () {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ describe("buildInitialGrokProviderSnapshot", () => {
3232
expect(snapshot.version).toBeNull();
3333
expect(snapshot.message).toContain("Checking Grok");
3434
expect(snapshot.requiresNewThreadForModelChange).toBe(true);
35+
expect(snapshot.slashCommands).toEqual([
36+
{
37+
name: "compact",
38+
description: "Compress conversation history to reclaim context window",
39+
input: { hint: "optional context about what to preserve" },
40+
},
41+
]);
3542
}),
3643
);
3744
});

‎apps/server/src/provider/Layers/GrokProvider.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
type ModelCapabilities,
44
type ServerProvider,
55
type ServerProviderModel,
6+
type ServerProviderSlashCommand,
67
} from "@t3tools/contracts";
78
import type * as EffectAcpSchema from "effect-acp/schema";
89
import { causeErrorTag } from "@t3tools/shared/observability";
@@ -44,6 +45,15 @@ const EMPTY_CAPABILITIES: ModelCapabilities = createModelCapabilities({
4445
const VERSION_PROBE_TIMEOUT_MS = 4_000;
4546
const GROK_ACP_MODEL_DISCOVERY_TIMEOUT_MS = 15_000;
4647

48+
/** Grok ACP handles `/compact` as prompt text; surface it in the composer slash menu. */
49+
const GROK_SLASH_COMMANDS: ReadonlyArray<ServerProviderSlashCommand> = [
50+
{
51+
name: "compact",
52+
description: "Compress conversation history to reclaim context window",
53+
input: { hint: "optional context about what to preserve" },
54+
},
55+
];
56+
4757
const GROK_BUILT_IN_MODELS: ReadonlyArray<ServerProviderModel> = [
4858
{
4959
slug: "grok-build",
@@ -66,6 +76,7 @@ export function buildInitialGrokProviderSnapshot(
6676
enabled: false,
6777
checkedAt,
6878
models,
79+
slashCommands: GROK_SLASH_COMMANDS,
6980
probe: {
7081
installed: false,
7182
version: null,
@@ -81,6 +92,7 @@ export function buildInitialGrokProviderSnapshot(
8192
enabled: true,
8293
checkedAt,
8394
models,
95+
slashCommands: GROK_SLASH_COMMANDS,
8496
probe: {
8597
installed: true,
8698
version: null,
@@ -175,6 +187,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
175187
enabled: false,
176188
checkedAt,
177189
models: fallbackModels,
190+
slashCommands: GROK_SLASH_COMMANDS,
178191
probe: {
179192
installed: false,
180193
version: null,
@@ -200,6 +213,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
200213
enabled: grokSettings.enabled,
201214
checkedAt,
202215
models: fallbackModels,
216+
slashCommands: GROK_SLASH_COMMANDS,
203217
probe: {
204218
installed: !isCommandMissingCause(error),
205219
version: null,
@@ -218,6 +232,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
218232
enabled: grokSettings.enabled,
219233
checkedAt,
220234
models: fallbackModels,
235+
slashCommands: GROK_SLASH_COMMANDS,
221236
probe: {
222237
installed: true,
223238
version: null,
@@ -241,6 +256,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
241256
enabled: grokSettings.enabled,
242257
checkedAt,
243258
models: fallbackModels,
259+
slashCommands: GROK_SLASH_COMMANDS,
244260
probe: {
245261
installed: true,
246262
version,
@@ -264,6 +280,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
264280
enabled: grokSettings.enabled,
265281
checkedAt,
266282
models: fallbackModels,
283+
slashCommands: GROK_SLASH_COMMANDS,
267284
probe: {
268285
installed: true,
269286
version,
@@ -282,6 +299,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
282299
enabled: grokSettings.enabled,
283300
checkedAt,
284301
models: fallbackModels,
302+
slashCommands: GROK_SLASH_COMMANDS,
285303
probe: {
286304
installed: true,
287305
version,
@@ -302,6 +320,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
302320
enabled: grokSettings.enabled,
303321
checkedAt,
304322
models,
323+
slashCommands: GROK_SLASH_COMMANDS,
305324
probe: {
306325
installed: true,
307326
version,

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import { describe, expect } from "vite-plus/test";
1010

1111
import {
1212
extractXAiAskUserQuestions,
13+
extractXAiAutoCompactCompleted,
1314
makeXAiAskUserQuestionCancelledResponse,
1415
makeXAiAskUserQuestionResponse,
1516
makeXAiPromptCompletionRuntime,
1617
XAiAskUserQuestionRequest,
18+
XAiSessionNotification,
1719
} from "./XAiAcpExtension.ts";
1820
import * as AcpSessionRuntime from "./AcpSessionRuntime.ts";
1921

@@ -102,6 +104,34 @@ describe("XAiAcpExtension", () => {
102104
]);
103105
});
104106

107+
it("extracts auto_compact_completed session notifications", () => {
108+
const notification = Schema.decodeUnknownSync(XAiSessionNotification)({
109+
sessionId: "session-1",
110+
update: {
111+
sessionUpdate: "auto_compact_completed",
112+
tokens_before: 12_000,
113+
tokens_after: 4_000,
114+
summary_preview: null,
115+
},
116+
});
117+
const compact = extractXAiAutoCompactCompleted(notification);
118+
expect(compact).toEqual({
119+
sessionId: "session-1",
120+
tokensBefore: 12_000,
121+
tokensAfter: 4_000,
122+
summaryPreview: undefined,
123+
raw: notification,
124+
});
125+
});
126+
127+
it("ignores non-compact session notifications", () => {
128+
const notification = Schema.decodeUnknownSync(XAiSessionNotification)({
129+
sessionId: "session-1",
130+
update: { sessionUpdate: "turn_completed", tokens_before: 1, tokens_after: 1 },
131+
});
132+
expect(extractXAiAutoCompactCompleted(notification)).toBeNull();
133+
});
134+
105135
it("treats nullable multiSelect from Grok as single-select", () => {
106136
const questions = extractXAiAskUserQuestions({
107137
sessionId: "session-1",

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,54 @@ export function makeXAiAskUserQuestionCancelledResponse(): XAiAskUserQuestionCan
196196
return { outcome: "cancelled" };
197197
}
198198

199+
/**
200+
* Grok session lifecycle notifications (`_x.ai/session_notification`).
201+
* Manual `/compact` and auto-compact complete as `auto_compact_completed`.
202+
*/
203+
const XAiSessionNotificationUpdate = Schema.Struct({
204+
sessionUpdate: Schema.String,
205+
tokens_before: Schema.optional(Schema.Number),
206+
tokens_after: Schema.optional(Schema.Number),
207+
summary_preview: Schema.optional(Schema.NullOr(Schema.String)),
208+
});
209+
210+
export const XAiSessionNotification = Schema.Struct({
211+
sessionId: Schema.String,
212+
update: XAiSessionNotificationUpdate,
213+
_meta: Schema.optional(Schema.Unknown),
214+
});
215+
216+
export type XAiSessionNotification = typeof XAiSessionNotification.Type;
217+
218+
export interface XAiAutoCompactCompleted {
219+
readonly sessionId: string;
220+
readonly tokensBefore: number | undefined;
221+
readonly tokensAfter: number | undefined;
222+
readonly summaryPreview: string | undefined;
223+
readonly raw: XAiSessionNotification;
224+
}
225+
226+
function finiteNonNegative(value: number | undefined): number | undefined {
227+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined;
228+
}
229+
230+
/** Returns compact details when the notification is a completed compaction; otherwise null. */
231+
export function extractXAiAutoCompactCompleted(
232+
notification: XAiSessionNotification,
233+
): XAiAutoCompactCompleted | null {
234+
if (notification.update.sessionUpdate !== "auto_compact_completed") {
235+
return null;
236+
}
237+
const summaryPreview = trimmed(notification.update.summary_preview ?? undefined);
238+
return {
239+
sessionId: notification.sessionId,
240+
tokensBefore: finiteNonNegative(notification.update.tokens_before),
241+
tokensAfter: finiteNonNegative(notification.update.tokens_after),
242+
summaryPreview,
243+
raw: notification,
244+
};
245+
}
246+
199247
/**
200248
* Adds Grok's private prompt-completion fallback around a standards-only ACP runtime.
201249
* The underlying runtime remains unaware of xAI methods and metadata.

0 commit comments

Comments
 (0)