Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion apps/server/scripts/acp-mock-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const emitOverlappingXAiPromptCompleteOutOfOrder =
const failPrompt = process.env.T3_ACP_FAIL_PROMPT === "1";
const failSetConfigOption = process.env.T3_ACP_FAIL_SET_CONFIG_OPTION === "1";
const exitOnSetConfigOption = process.env.T3_ACP_EXIT_ON_SET_CONFIG_OPTION === "1";
const emitSessionInfoUpdate = process.env.T3_ACP_EMIT_SESSION_INFO === "1";
const promptResponseText = process.env.T3_ACP_PROMPT_RESPONSE_TEXT;
const promptDelayMs = Number(process.env.T3_ACP_PROMPT_DELAY_MS ?? "0");
const permissionOptionIds = {
Expand Down Expand Up @@ -79,6 +80,10 @@ function writeJsonRpcNotification(method: string, params: unknown): void {
process.stdout.write(`${JSON.stringify({ jsonrpc: "2.0", method, params })}\n`);
}

function writeJsonRpcResponse(id: string | number, result: unknown): void {
process.stdout.write(`${JSON.stringify({ jsonrpc: "2.0", id, result })}\n`);
}

process.once("SIGTERM", () => {
logExit("SIGTERM");
process.exit(0);
Expand Down Expand Up @@ -300,9 +305,33 @@ const program = Effect.gen(function* () {
Effect.sync(() => {
parameterizedModelPicker =
request.clientCapabilities?._meta?.parameterizedModelPicker === true;
// #4109-class: unsolicited response with non-numeric id must not crash the client.
if (process.env.T3_ACP_EMIT_SKILLS_RELOAD_ID === "1") {
queueMicrotask(() => {
writeJsonRpcResponse("skills-reload", { ok: true });
});
}
const initMeta =
process.env.T3_ACP_EMIT_INIT_AVAILABLE_COMMANDS === "1"
? {
availableCommands: [
{
name: "compact",
description: "Compress conversation history",
input: { hint: "optional context" },
},
{
name: "session-info",
description: "Show session details",
input: null,
},
],
}
: undefined;
return {
protocolVersion: 1,
agentCapabilities: { loadSession: true },
...(initMeta ? { _meta: initMeta } : {}),
};
}),
);
Expand Down Expand Up @@ -865,6 +894,16 @@ const program = Effect.gen(function* () {
},
});

if (emitSessionInfoUpdate) {
yield* agent.client.sessionUpdate({
sessionId: requestedSessionId,
update: {
sessionUpdate: "session_info_update",
title: "Mock Grok session title",
},
});
}

yield* agent.client.sessionUpdate({
sessionId: requestedSessionId,
update: {
Expand All @@ -873,7 +912,17 @@ const program = Effect.gen(function* () {
},
});

return { stopReason: "end_turn" };
// Live Grok stamps usage on prompt result `_meta` (not only usage_update).
return {
stopReason: "end_turn",
_meta: {
totalTokens: 12_345,
inputTokens: 10_000,
outputTokens: 2_000,
cachedReadTokens: 8_000,
reasoningTokens: 345,
},
};
}),
);

Expand Down
60 changes: 58 additions & 2 deletions apps/server/src/provider/Drivers/GrokDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import * as Crypto from "effect/Crypto";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as PubSub from "effect/PubSub";
import * as Ref from "effect/Ref";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";
import { HttpClient } from "effect/unstable/http";
import { ChildProcessSpawner } from "effect/unstable/process";

Expand All @@ -17,6 +20,7 @@ import {
buildInitialGrokProviderSnapshot,
checkGrokProviderStatus,
enrichGrokSnapshot,
mapAcpCommandsToCatalog,
} from "../Layers/GrokProvider.ts";
import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
Expand Down Expand Up @@ -106,27 +110,63 @@ export const GrokDriver: ProviderDriver<GrokSettings, GrokDriverEnv> = {
env: processEnv,
});

const commandCatalogRef = yield* Ref.make({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium Drivers/GrokDriver.ts:113

mergeCommandCatalog treats an empty live catalog as "not initialized" and falls back to the previous snapshot's arrays. When onAvailableCommands receives an empty available_commands_update (i.e., the provider removed all commands), the catalog ref is correctly cleared to empty arrays, but mergeCommandCatalog sees length === 0 and keeps the stale slashCommands/skills from the last health probe. Removed commands and skills therefore remain advertised to clients indefinitely instead of being cleared.

The length > 0 guard conflates "no live data yet" with "live data says empty." Track whether the live catalog has been initialized with a separate boolean flag (or Option), then use the ref arrays unconditionally once initialized.

🤖 Copy this AI Prompt to have your agent fix this:
In file @apps/server/src/provider/Drivers/GrokDriver.ts around line 113:

`mergeCommandCatalog` treats an empty live catalog as "not initialized" and falls back to the previous snapshot's arrays. When `onAvailableCommands` receives an empty `available_commands_update` (i.e., the provider removed all commands), the catalog ref is correctly cleared to empty arrays, but `mergeCommandCatalog` sees `length === 0` and keeps the stale `slashCommands`/`skills` from the last health probe. Removed commands and skills therefore remain advertised to clients indefinitely instead of being cleared.

The `length > 0` guard conflates "no live data yet" with "live data says empty." Track whether the live catalog has been initialized with a separate boolean flag (or `Option`), then use the ref arrays unconditionally once initialized.

slashCommands: [] as ServerProvider["slashCommands"],
skills: [] as ServerProvider["skills"],
});
// Live available_commands_update (and initialize meta seed) must push a
// snapshot change so clients do not wait for the next health probe.
const commandCatalogChanges = yield* Effect.acquireRelease(
PubSub.unbounded<void>(),
PubSub.shutdown,
);

const mergeCommandCatalog = (snapshot: ServerProvider): Effect.Effect<ServerProvider> =>
Ref.get(commandCatalogRef).pipe(
Effect.map((catalog) => ({
...snapshot,
slashCommands:
catalog.slashCommands.length > 0 ? catalog.slashCommands : snapshot.slashCommands,
skills: catalog.skills.length > 0 ? catalog.skills : snapshot.skills,
})),
);

const adapter = yield* makeGrokAdapter(effectiveConfig, {
environment: processEnv,
...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}),
instanceId,
onAvailableCommands: (commands) =>
Effect.gen(function* () {
const catalog = mapAcpCommandsToCatalog(commands);
yield* Ref.set(commandCatalogRef, {
slashCommands: catalog.slashCommands,
skills: catalog.skills,
});
yield* PubSub.publish(commandCatalogChanges, undefined);
}),
});
const textGeneration = yield* makeGrokTextGeneration(effectiveConfig, processEnv);

const checkProvider = checkGrokProviderStatus(effectiveConfig, processEnv).pipe(
Effect.map(stampIdentity),
Effect.flatMap(mergeCommandCatalog),
Effect.provideService(Crypto.Crypto, crypto),
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
);

const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<GrokSettings>>({
const managedSnapshot = yield* makeManagedServerProvider<
ProviderSnapshotSettings<GrokSettings>
>({
maintenanceCapabilities,
getSettings: snapshotSettings.getSettings,
streamSettings: snapshotSettings.streamSettings,
haveSettingsChanged: haveProviderSnapshotSettingsChanged,
initialSnapshot: (settings) =>
buildInitialGrokProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
buildInitialGrokProviderSnapshot(settings.provider).pipe(
Effect.map(stampIdentity),
Effect.flatMap(mergeCommandCatalog),
),
checkProvider,
enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
enrichGrokSnapshot({
Expand All @@ -148,6 +188,22 @@ export const GrokDriver: ProviderDriver<GrokSettings, GrokDriverEnv> = {
),
);

const snapshot = {
...managedSnapshot,
getSnapshot: managedSnapshot.getSnapshot.pipe(Effect.flatMap(mergeCommandCatalog)),
get streamChanges() {
const managedChanges = Stream.mapEffect(
managedSnapshot.streamChanges,
mergeCommandCatalog,
);
const catalogDrivenChanges = Stream.mapEffect(
Stream.fromPubSub(commandCatalogChanges),
() => managedSnapshot.getSnapshot.pipe(Effect.flatMap(mergeCommandCatalog)),
);
return Stream.merge(managedChanges, catalogDrivenChanges);
},
};

return {
instanceId,
driverKind: DRIVER_KIND,
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/provider/Layers/CursorAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ export function makeCursorAdapter(
turnId: ctx.activeTurnId,
...(event.itemId ? { itemId: event.itemId } : {}),
text: event.text,
streamKind: event.streamKind,
rawPayload: event.rawPayload,
}),
);
Expand Down
Loading
Loading