Skip to content

Commit 1c748ed

Browse files
only21milclaude
andauthored
fix(providers): slow Grok, Pi and fx starts no longer show as unavailable (pingdotgg#28400)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent b08d559 commit 1c748ed

6 files changed

Lines changed: 177 additions & 12 deletions

File tree

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ import * as NodeURL from "node:url";
55
import * as NodeServices from "@effect/platform-node/NodeServices";
66
import { describe, expect, it } from "@effect/vitest";
77
import * as Effect from "effect/Effect";
8+
import * as Fiber from "effect/Fiber";
89
import * as FileSystem from "effect/FileSystem";
10+
import * as PlatformError from "effect/PlatformError";
911
import * as Schema from "effect/Schema";
12+
import * as Sink from "effect/Sink";
13+
import * as Stream from "effect/Stream";
1014
import { HttpClient, HttpClientResponse } from "effect/http";
15+
import * as ChildProcess from "effect/process/ChildProcess";
16+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
17+
import * as TestClock from "effect/testing/TestClock";
1118
import { GrokSettings } from "@t3tools/provider-grok/settings";
1219

1320
import {
@@ -221,6 +228,68 @@ it.layer(NodeServices.layer)("checkGrokProviderStatus", (it) => {
221228
}),
222229
);
223230

231+
it.effect.each([
232+
{
233+
label: "answers after 6 s",
234+
versionDelay: "6 seconds",
235+
version: "1.0.52",
236+
message: "Grok CLI is installed but ACP initialize failed. Model options may be incomplete.",
237+
probesAfterVersion: true,
238+
},
239+
{
240+
label: "never answers",
241+
versionDelay: "Infinity",
242+
version: null,
243+
message: "Grok CLI is installed but timed out while running `grok --version`.",
244+
probesAfterVersion: false,
245+
},
246+
] as const)("gives `grok --version` 10 s when it $label", (row) =>
247+
Effect.gen(function* () {
248+
// A fake CLI runs on wall time, so a stub answers `--version` on the test clock.
249+
const spawned: Array<string> = [];
250+
const spawner = ChildProcessSpawner.make((command) => {
251+
const args = ChildProcess.isStandardCommand(command) ? command.args : [];
252+
spawned.push(args.join(" "));
253+
if (!args.includes("--version")) {
254+
return Effect.fail(
255+
PlatformError.systemError({
256+
_tag: "PermissionDenied",
257+
module: "ChildProcess",
258+
method: "spawn",
259+
description: "Only `grok --version` is stubbed.",
260+
}),
261+
);
262+
}
263+
const handle = ChildProcessSpawner.makeHandle({
264+
pid: ChildProcessSpawner.ProcessId(900_000_201),
265+
exitCode: Effect.delay(Effect.succeed(ChildProcessSpawner.ExitCode(0)), row.versionDelay),
266+
isRunning: Effect.succeed(true),
267+
kill: () => Effect.void,
268+
unref: Effect.succeed(Effect.void),
269+
stdin: Sink.drain,
270+
stdout: Stream.encodeText(
271+
Stream.fromEffect(Effect.delay(Effect.succeed("grok 1.0.52\n"), row.versionDelay)),
272+
),
273+
stderr: Stream.empty,
274+
all: Stream.empty,
275+
getInputFd: () => Sink.drain,
276+
getOutputFd: () => Stream.empty,
277+
});
278+
return Effect.succeed(handle);
279+
});
280+
const fiber = yield* checkGrokProviderStatus(decodeGrokSettings({ enabled: true }), {}).pipe(
281+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
282+
Effect.forkChild,
283+
);
284+
yield* TestClock.adjust("10 seconds");
285+
const snapshot = yield* Fiber.join(fiber);
286+
expect(snapshot.installed).toBe(true);
287+
expect(snapshot.version).toBe(row.version);
288+
expect(snapshot.message).toBe(row.message);
289+
expect(spawned.length > 1).toBe(row.probesAfterVersion);
290+
}),
291+
);
292+
224293
// A stand-in for the Grok CLI: `--version` and `models` print canned text,
225294
// and `agent stdio` execs the mock ACP agent so `initialize` returns model metadata.
226295
// A list of `models` outputs is served in call order, repeating the last one.

‎packages/provider-fx/src/server/status.test.ts‎

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { assert, describe, it } from "@effect/vitest";
3+
import type * as Duration from "effect/Duration";
34
import * as Effect from "effect/Effect";
5+
import * as Fiber from "effect/Fiber";
46
import * as Schema from "effect/Schema";
57
import * as Sink from "effect/Sink";
68
import * as Stream from "effect/Stream";
79
import * as ChildProcess from "effect/process/ChildProcess";
810
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11+
import * as TestClock from "effect/testing/TestClock";
912

1013
import { FxSettings } from "../settings.ts";
1114
import { checkFxProviderStatus } from "./status.ts";
1215

1316
const encoder = new TextEncoder();
1417
const settings = Schema.decodeSync(FxSettings)({ enabled: true });
1518

16-
function processHandle(stdout: string, code: number) {
19+
/** Holds stdout and the exit back until `answered` completes, like a CLI slow to answer. */
20+
function processHandle(stdout: string, code: number, answered: Effect.Effect<void> = Effect.void) {
1721
return ChildProcessSpawner.makeHandle({
1822
pid: ChildProcessSpawner.ProcessId(900_000_101),
19-
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(code)),
23+
exitCode: Effect.as(answered, ChildProcessSpawner.ExitCode(code)),
2024
isRunning: Effect.succeed(false),
2125
kill: () => Effect.void,
2226
unref: Effect.succeed(Effect.void),
2327
stdin: Sink.drain,
24-
stdout: stdout.length === 0 ? Stream.empty : Stream.succeed(encoder.encode(stdout)),
28+
stdout: Stream.unwrap(
29+
Effect.as(
30+
answered,
31+
stdout.length === 0 ? Stream.empty : Stream.succeed(encoder.encode(stdout)),
32+
),
33+
),
2534
stderr: Stream.empty,
2635
all: Stream.empty,
2736
getInputFd: () => Sink.drain,
@@ -30,7 +39,7 @@ function processHandle(stdout: string, code: number) {
3039
}
3140

3241
/** Answers `fx` invocations by their arguments and records which ran. */
33-
const check = (statusJson: string) =>
42+
const check = (statusJson: string, versionDelay: Duration.Input = 0) =>
3443
Effect.gen(function* () {
3544
const outputs: Record<string, string> = {
3645
"--version": "fx 0.0.13\n",
@@ -50,7 +59,13 @@ const check = (statusJson: string) =>
5059
const args = ChildProcess.isStandardCommand(command) ? command.args.join(" ") : "";
5160
invoked.push(args);
5261
const stdout = outputs[args];
53-
return Effect.succeed(processHandle(stdout ?? "", stdout === undefined ? 2 : 0));
62+
return Effect.succeed(
63+
processHandle(
64+
stdout ?? "",
65+
stdout === undefined ? 2 : 0,
66+
args === "--version" ? Effect.sleep(versionDelay) : Effect.void,
67+
),
68+
);
5469
});
5570
const draft = yield* checkFxProviderStatus(settings, {}).pipe(
5671
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
@@ -59,6 +74,38 @@ const check = (statusJson: string) =>
5974
}).pipe(Effect.provide(NodeServices.layer));
6075

6176
describe("checkFxProviderStatus", () => {
77+
it.effect.each([
78+
{
79+
label: "answers after 6 s",
80+
versionDelay: "6 seconds",
81+
status: "ready",
82+
version: "0.0.13",
83+
message: undefined,
84+
invoked: ["--version", "status --json", "models --json"],
85+
},
86+
{
87+
label: "never answers",
88+
versionDelay: "Infinity",
89+
status: "error",
90+
version: null,
91+
message: "Failed to run `fx --version`.",
92+
invoked: ["--version"],
93+
},
94+
] as const)("gives `fx --version` 10 s when it $label", (row) =>
95+
Effect.gen(function* () {
96+
const fiber = yield* check(
97+
JSON.stringify({ kind: "status", model: "anthropic/claude-opus-5.5", auth: "fx login" }),
98+
row.versionDelay,
99+
).pipe(Effect.forkChild);
100+
yield* TestClock.adjust("10 seconds");
101+
const { draft, invoked } = yield* Fiber.join(fiber);
102+
assert.equal(draft.status, row.status);
103+
assert.equal(draft.version, row.version);
104+
assert.equal(draft.message, row.message);
105+
assert.deepEqual(invoked, [...row.invoked]);
106+
}),
107+
);
108+
62109
// `fx status --json` from fx 0.0.13, trimmed to the fields T3 reads plus a few it ignores.
63110
it.effect("reports sign-in from `fx status` without opening an ACP session", () =>
64111
Effect.gen(function* () {

‎packages/provider-fx/src/server/status.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
import {
2020
AUTH_PROBE_TIMEOUT_MS,
2121
buildServerProvider,
22-
DEFAULT_TIMEOUT_MS,
2322
isCommandMissingCause,
2423
nonEmptyTrimmed,
2524
parseGenericCliVersion,
@@ -135,6 +134,8 @@ const FxModels = Schema.fromJsonString(
135134
);
136135
const decodeFxModels = Schema.decodeUnknownOption(FxModels);
137136

137+
const VERSION_PROBE_TIMEOUT_MS = 10_000;
138+
138139
function fxAuth(auth: string | undefined): ServerProviderAuth {
139140
const method = nonEmptyTrimmed(auth);
140141
if (method === undefined) return { status: "unknown" };
@@ -163,7 +164,7 @@ export const checkFxProviderStatus = Effect.fn("checkFxProviderStatus")(function
163164
}
164165

165166
const versionResult = yield* runFxCli(settings, ["--version"], environment).pipe(
166-
Effect.timeoutOption(DEFAULT_TIMEOUT_MS),
167+
Effect.timeoutOption(VERSION_PROBE_TIMEOUT_MS),
167168
Effect.result,
168169
);
169170
if (Result.isFailure(versionResult) || Option.isNone(versionResult.success)) {

‎packages/provider-grok/src/server/status.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const EMPTY_CAPABILITIES: ModelCapabilities = createModelCapabilities({
5858
optionDescriptors: [],
5959
});
6060

61-
const VERSION_PROBE_TIMEOUT_MS = 4_000;
61+
const VERSION_PROBE_TIMEOUT_MS = 10_000;
6262
// Grok writes the initialize result only after startup work (settings,
6363
// skill discovery, file watching). From a home directory that work measured
6464
// 9-14s on CLI 1.0.52, so an 8s cap records a healthy CLI as a failed probe.

‎packages/provider-pi/src/server/status.test.ts‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import * as Sink from "effect/Sink";
88
import * as Stream from "effect/Stream";
99
import * as ChildProcess from "effect/process/ChildProcess";
1010
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11+
import * as Fiber from "effect/Fiber";
12+
import * as TestClock from "effect/testing/TestClock";
1113

1214
import { checkPiProviderStatus, MINIMUM_PI_VERSION } from "./status.ts";
1315

@@ -21,19 +23,22 @@ function processHandle(input: {
2123
readonly stdout?: string;
2224
readonly stderr?: string;
2325
readonly exitCode?: number;
26+
/** Holds stdout and the exit back until it completes, like a CLI slow to answer. */
27+
readonly answered?: Effect.Effect<void>;
2428
}) {
29+
const answered = input.answered ?? Effect.void;
2530
const bytes = (value: string | undefined) =>
2631
value === undefined || value.length === 0
2732
? Stream.empty
2833
: Stream.succeed(encoder.encode(value));
2934
return ChildProcessSpawner.makeHandle({
3035
pid: ChildProcessSpawner.ProcessId(900_000_001),
31-
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(input.exitCode ?? 0)),
36+
exitCode: Effect.as(answered, ChildProcessSpawner.ExitCode(input.exitCode ?? 0)),
3237
isRunning: Effect.succeed(false),
3338
kill: () => Effect.void,
3439
unref: Effect.succeed(Effect.void),
3540
stdin: Sink.drain,
36-
stdout: bytes(input.stdout),
41+
stdout: Stream.unwrap(Effect.as(answered, bytes(input.stdout))),
3742
stderr: bytes(input.stderr),
3843
all: Stream.empty,
3944
getInputFd: () => Sink.drain,
@@ -148,6 +153,48 @@ describe("PiProvider", () => {
148153
}).pipe(Effect.provide(NodeServices.layer)),
149154
);
150155

156+
it.effect.each([
157+
{
158+
label: "answers after 6 s",
159+
versionDelay: "6 seconds",
160+
status: "ready",
161+
version: "0.84.3",
162+
message: "could not refresh its models and commands",
163+
probesAfterVersion: true,
164+
},
165+
{
166+
label: "never answers",
167+
versionDelay: "Infinity",
168+
status: "error",
169+
version: null,
170+
message: "Pi CLI is installed but timed out while running `pi --version`.",
171+
probesAfterVersion: false,
172+
},
173+
] as const)("gives `pi --version` 10 s when it $label", (row) =>
174+
Effect.gen(function* () {
175+
const spawned: Array<string> = [];
176+
const spawner = ChildProcessSpawner.make((command) => {
177+
const args = ChildProcess.isStandardCommand(command) ? command.args : [];
178+
spawned.push(args.join(" "));
179+
return Effect.succeed(
180+
args.includes("--version")
181+
? processHandle({ stdout: "pi 0.84.3\n", answered: Effect.sleep(row.versionDelay) })
182+
: processHandle({ stderr: "RPC startup failed", exitCode: 1 }),
183+
);
184+
});
185+
const fiber = yield* checkPiProviderStatus(settings).pipe(
186+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
187+
Effect.forkChild,
188+
);
189+
yield* TestClock.adjust("10 seconds");
190+
const snapshot = yield* Fiber.join(fiber);
191+
assert.equal(snapshot.status, row.status);
192+
assert.equal(snapshot.version, row.version);
193+
assert.include(snapshot.message ?? "", row.message);
194+
assert.equal(spawned.length > 1, row.probesAfterVersion);
195+
}).pipe(Effect.provide(NodeServices.layer)),
196+
);
197+
151198
it.effect("reports a known unauthenticated inventory when Pi returns no usable models", () =>
152199
Effect.gen(function* () {
153200
const spawner = yield* piDiscoverySpawner({ models: [] });

‎packages/provider-pi/src/server/status.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions";
3030
import {
3131
buildServerProvider,
32+
DEFAULT_TIMEOUT_MS,
3233
isCommandMissingCause,
3334
parseGenericCliVersion,
3435
providerModelsFromSettings,
@@ -56,7 +57,7 @@ const PI_PRESENTATION = {
5657
reportsContextWindow: true,
5758
} as const;
5859

59-
const VERSION_PROBE_TIMEOUT_MS = 4_000;
60+
const VERSION_PROBE_TIMEOUT_MS = 10_000;
6061
const PI_RPC_DISCOVERY_TIMEOUT_MS = 30_000;
6162
/**
6263
* get_entries arrived in 0.80.3 and agent_settled landed in source at 0.80.4.
@@ -278,7 +279,7 @@ const describeUnavailablePiModel = Effect.fn("PiProviderStatus.describeUnavailab
278279
}),
279280
),
280281
),
281-
Effect.timeout(VERSION_PROBE_TIMEOUT_MS),
282+
Effect.timeout(DEFAULT_TIMEOUT_MS),
282283
Effect.option,
283284
);
284285
const missingCredentials = Option.exists(

0 commit comments

Comments
 (0)