GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: limita concorrência e preserva resultados dos probes Claude · pingdotgg/t3code@ec95dc6

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src/provider/Layers
    • ClaudeProvider.test.ts
    • ClaudeProvider.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboard
+98Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
2+
import { assert, it } from "@effect/vitest";
3+
import { ClaudeSettings } from "@t3tools/contracts";
4+
import * as Effect from "effect/Effect";
5+
import * as Fiber from "effect/Fiber";
6+
import * as Schema from "effect/Schema";
7+
import * as TestClock from "effect/testing/TestClock";
8+
import { beforeEach, vi } from "vite-plus/test";
9+
10+
import { probeClaudeCapabilities } from "./ClaudeProvider.ts";
11+
12+
type ClaudeQuery = typeof import("@anthropic-ai/claude-agent-sdk").query;
13+
type ClaudeInitialization = Awaited<ReturnType<ReturnType<ClaudeQuery>["initializationResult"]>>;
14+
15+
const claudeQueryMock = vi.hoisted(() => vi.fn());
16+
const decodeClaudeSettings = Schema.decodeEffect(ClaudeSettings);
17+
18+
vi.mock("@anthropic-ai/claude-agent-sdk", () => ({
19+
query: claudeQueryMock,
20+
}));
21+
22+
beforeEach(() => {
23+
claudeQueryMock.mockReset();
24+
});
25+
26+
it.layer(NodeServices.layer)("probeClaudeCapabilities", (it) => {
27+
it.effect("keeps completed probes when another custom model times out", () =>
28+
Effect.gen(function* () {
29+
let activeProbes = 0;
30+
let peakActiveProbes = 0;
31+
const closedModels: Array<string> = [];
32+
33+
claudeQueryMock.mockImplementation((input: Parameters<ClaudeQuery>[0]) => {
34+
assert.ok(input.options);
35+
assert.ok(input.options.abortController);
36+
const model = input.options.model ?? "default";
37+
const abort = input.options.abortController;
38+
activeProbes += 1;
39+
peakActiveProbes = Math.max(peakActiveProbes, activeProbes);
40+
let closed = false;
41+
const close = () => {
42+
if (!closed) {
43+
closed = true;
44+
activeProbes -= 1;
45+
closedModels.push(model);
46+
}
47+
};
48+
49+
const initializationResult = () => {
50+
if (model === "slow") {
51+
return new Promise<ClaudeInitialization>((_resolve, reject) => {
52+
abort.signal.addEventListener("abort", () => reject(new Error("aborted")), {
53+
once: true,
54+
});
55+
});
56+
}
57+
return Promise.resolve({
58+
account: { email: `${model}@example.com` },
59+
commands: [],
60+
models: [
61+
{
62+
value: model,
63+
displayName: model,
64+
description: "Custom model",
65+
supportsEffort: true,
66+
supportedEffortLevels: ["low", "high"],
67+
},
68+
],
69+
} as unknown as ClaudeInitialization);
70+
};
71+
72+
return { close, initializationResult } as ReturnType<ClaudeQuery>;
73+
});
74+
75+
const settings = yield* decodeClaudeSettings({
76+
customModels: ["slow", "fast", "later"],
77+
});
78+
const probe = yield* probeClaudeCapabilities(settings).pipe(
79+
Effect.forkChild({ startImmediately: true }),
80+
);
81+
82+
while (claudeQueryMock.mock.calls.length < 3) {
83+
yield* Effect.yieldNow;
84+
}
85+
yield* TestClock.adjust("25 seconds");
86+
87+
const result = yield* Fiber.join(probe);
88+
assert.deepStrictEqual(result?.models.map((model) => model.value).toSorted(), [
89+
"fast",
90+
"later",
91+
]);
92+
assert.strictEqual(result?.email, "[email protected]");
93+
assert.strictEqual(peakActiveProbes, 2);
94+
assert.strictEqual(activeProbes, 0);
95+
assert.deepStrictEqual(closedModels.toSorted(), ["fast", "later", "slow"]);
96+
}),
97+
);
98+
});
Collapse file

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

Copy file name to clipboardExpand all lines: apps/server/src/provider/Layers/ClaudeProvider.ts
+55-51Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ function apiProviderAuthMetadata(
571571
// account info. The previous 8s budget expired mid-init, so the probe returned
572572
// `undefined` and left the provider unverified and unselectable in the picker.
573573
const CAPABILITIES_PROBE_TIMEOUT_MS = 25_000;
574+
const CAPABILITIES_PROBE_CONCURRENCY = 2;
574575

575576
function nonEmptyProbeString(value: string): string | undefined {
576577
const candidate = value.trim();
@@ -683,18 +684,17 @@ const probeClaudeCapabilities = (
683684
environment?: NodeJS.ProcessEnv,
684685
cwd?: string,
685686
) => {
686-
const abortControllers: AbortController[] = [];
687687
return Effect.gen(function* () {
688688
const claudeEnvironment = yield* makeClaudeEnvironment(claudeSettings, environment);
689689
const executablePath = yield* resolveClaudeSdkExecutablePath(
690690
claudeSettings.binaryPath,
691691
claudeEnvironment,
692692
);
693-
return yield* Effect.tryPromise(async () => {
694-
const initialize = (model?: string) => {
695-
const abort = new AbortController();
696-
abortControllers.push(abort);
697-
const q = claudeQuery({
693+
const initialize = (model?: string) => {
694+
const abort = new AbortController();
695+
let query: ReturnType<typeof claudeQuery> | undefined;
696+
return Effect.tryPromise(() => {
697+
query = claudeQuery({
698698
// Never yield — we only need initialization data, not a conversation.
699699
// This prevents any prompt from reaching the Anthropic API.
700700
// oxlint-disable-next-line require-yield
@@ -713,56 +713,60 @@ const probeClaudeCapabilities = (
713713
stderr: () => {},
714714
},
715715
});
716-
return q.initializationResult();
717-
};
718-
const customModels = [
719-
...new Set(claudeSettings.customModels.map((model) => model.trim())),
720-
].filter((model) => model.length > 0);
721-
const initializations = [];
722-
for (const model of customModels.length > 0 ? customModels : [undefined]) {
723-
try {
724-
initializations.push(await initialize(model));
725-
} catch {
726-
// One invalid custom model must not prevent the remaining probes.
727-
}
728-
}
729-
if (initializations.length === 0 && customModels.length > 0) {
730-
initializations.push(await initialize());
731-
}
732-
const init = initializations[0]!;
733-
const modelsByValue = indexClaudeModels(
734-
initializations.flatMap((initialization) => initialization.models),
716+
return query.initializationResult();
717+
}).pipe(
718+
Effect.ensuring(
719+
Effect.sync(() => {
720+
if (!abort.signal.aborted) abort.abort();
721+
query?.close();
722+
}),
723+
),
724+
Effect.timeoutOption(CAPABILITIES_PROBE_TIMEOUT_MS),
725+
Effect.result,
726+
Effect.map((result) =>
727+
Result.isFailure(result) ? undefined : Option.getOrUndefined(result.success),
728+
),
735729
);
736-
const account = init.account as
737-
| {
738-
readonly email?: string;
739-
readonly subscriptionType?: string;
740-
readonly tokenSource?: string;
741-
readonly apiProvider?: string;
742-
}
743-
| undefined;
744-
return {
745-
email: account?.email,
746-
subscriptionType: account?.subscriptionType,
747-
tokenSource: account?.tokenSource,
748-
apiProvider: account?.apiProvider,
749-
slashCommands: parseClaudeInitializationCommands(init.commands),
750-
models: [...modelsByValue.values()],
751-
} satisfies ClaudeCapabilitiesProbe;
752-
});
753-
}).pipe(
754-
Effect.ensuring(
755-
Effect.sync(() => {
756-
for (const abort of abortControllers) {
757-
if (!abort.signal.aborted) abort.abort();
730+
};
731+
const customModels = [
732+
...new Set(claudeSettings.customModels.map((model) => model.trim())),
733+
].filter((model) => model.length > 0);
734+
const initializations = (yield* Effect.forEach(
735+
customModels.length > 0 ? customModels : [undefined],
736+
initialize,
737+
{ concurrency: CAPABILITIES_PROBE_CONCURRENCY },
738+
)).filter((initialization) => initialization !== undefined);
739+
if (initializations.length === 0 && customModels.length > 0) {
740+
const fallback = yield* initialize();
741+
if (fallback) initializations.push(fallback);
742+
}
743+
const init = initializations[0];
744+
if (!init) return undefined;
745+
746+
const modelsByValue = indexClaudeModels(
747+
initializations.flatMap((initialization) => initialization.models),
748+
);
749+
const account = init.account as
750+
| {
751+
readonly email?: string;
752+
readonly subscriptionType?: string;
753+
readonly tokenSource?: string;
754+
readonly apiProvider?: string;
758755
}
759-
}),
760-
),
761-
Effect.timeoutOption(CAPABILITIES_PROBE_TIMEOUT_MS),
756+
| undefined;
757+
return {
758+
email: account?.email,
759+
subscriptionType: account?.subscriptionType,
760+
tokenSource: account?.tokenSource,
761+
apiProvider: account?.apiProvider,
762+
slashCommands: parseClaudeInitializationCommands(init.commands),
763+
models: [...modelsByValue.values()],
764+
} satisfies ClaudeCapabilitiesProbe;
765+
}).pipe(
762766
Effect.result,
763767
Effect.map((result) => {
764768
if (Result.isFailure(result)) return undefined;
765-
return Option.isSome(result.success) ? result.success.value : undefined;
769+
return result.success;
766770
}),
767771
);
768772
};

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.