fix(server): prevent probeClaudeCapabilities from wasting API requests (#2192)
Co-authored-by: Claude Opus 4.6 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 file changed
+21
-5
@@ -14,6 +14,7 @@ import { decodeJsonResult } from "@t3tools/shared/schemaJson";
|
||||
import {
|
||||
query as claudeQuery,
|
||||
type SlashCommand as ClaudeSlashCommand,
|
||||
type SDKUserMessage,
|
||||
} from "@anthropic-ai/claude-agent-sdk";
|
||||
|
||||
import {
|
||||
@@ -480,13 +481,24 @@ function dedupeSlashCommands(
|
||||
return [...commandsByName.values()];
|
||||
}
|
||||
|
||||
function waitForAbortSignal(signal: AbortSignal): Promise<void> {
|
||||
if (signal.aborted) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
signal.addEventListener("abort", () => resolve(), { once: true });
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Probe account information by spawning a lightweight Claude Agent SDK
|
||||
* session and reading the initialization result.
|
||||
*
|
||||
* The prompt is never sent to the Anthropic API — we abort immediately
|
||||
* after the local initialization phase completes. This gives us the
|
||||
* user's subscription type without incurring any token cost.
|
||||
* We pass a never-yielding AsyncIterable as the prompt so that no user
|
||||
* message is ever written to the subprocess stdin. This means the Claude
|
||||
* Code subprocess completes its local initialization IPC (returning
|
||||
* account info and slash commands) but never starts an API request to
|
||||
* Anthropic. We read the init data and then abort the subprocess.
|
||||
*
|
||||
* This is used as a fallback when `claude auth status` does not include
|
||||
* subscription type information.
|
||||
@@ -495,12 +507,16 @@ const probeClaudeCapabilities = (binaryPath: string) => {
|
||||
const abort = new AbortController();
|
||||
return Effect.tryPromise(async () => {
|
||||
const q = claudeQuery({
|
||||
prompt: ".",
|
||||
// Never yield — we only need initialization data, not a conversation.
|
||||
// This prevents any prompt from reaching the Anthropic API.
|
||||
// oxlint-disable-next-line require-yield
|
||||
prompt: (async function* (): AsyncGenerator<SDKUserMessage> {
|
||||
await waitForAbortSignal(abort.signal);
|
||||
})(),
|
||||
options: {
|
||||
persistSession: false,
|
||||
pathToClaudeCodeExecutable: binaryPath,
|
||||
abortController: abort,
|
||||
maxTurns: 0,
|
||||
settingSources: ["user", "project", "local"],
|
||||
allowedTools: [],
|
||||
stderr: () => {},
|
||||
|
||||
Reference in new issue
Block a user