Skip to content

Commit 41bbb0a

Browse files
author
tsouth89
committed
fix(server): address review on the probe timeout path
Three fixes from bot review: - A timeout left `enrichmentGeneration` untouched, so enrichment still running against the pre-timeout snapshot could publish over it and restore the old `checkedAt` and message. That reintroduced the silent no-op this change is meant to remove. The timeout now retires that generation and interrupts the fiber. - `hasCompletedCheck` was set on any successful check, including one that reported the CLI missing. A later timeout then carried "not installed" forward and ignored what the probe had just established, so a provider installed after a failed check could stay hidden. Only a status the UI can act on now counts. - Grok's ACP timeout runs after `grok --version` has answered, so the error carries that version rather than dropping it. Also switches to `Effect.catchTags` and returns the recovery snapshot from the catch, which removes the error-as-success-value round trip and the private `Schema.is` narrowing. Model: Claude Opus 5; harness: Claude Code.
1 parent 503e227 commit 41bbb0a

4 files changed

Lines changed: 125 additions & 28 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
273273
probe: "ACP model discovery",
274274
timeoutMs: GROK_ACP_MODEL_DISCOVERY_TIMEOUT_MS,
275275
installed: true,
276+
// `grok --version` already answered, so keep what it told us.
277+
...(version ? { version } : {}),
276278
});
277279
}
278280
const discoveredModels = discoveryExit.value.value;

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ const enrichedSnapshot: ServerProvider = {
9494
],
9595
};
9696

97+
const missingCliSnapshot: ServerProvider = {
98+
...refreshedSnapshot,
99+
installed: false,
100+
version: null,
101+
status: "error",
102+
auth: { status: "unknown" },
103+
message: "Codex CLI (`codex`) is not installed or not on PATH.",
104+
};
105+
97106
const probeTimeout = new ProviderProbeTimeoutError({
98107
provider: "Codex",
99108
probe: "app-server status",
@@ -546,6 +555,76 @@ describe("makeManagedServerProvider", () => {
546555
).pipe(Effect.provide(AlwaysRunTestLayer)),
547556
);
548557

558+
it.effect("does not let stale enrichment publish over a probe timeout", () =>
559+
Effect.scoped(
560+
Effect.gen(function* () {
561+
const publishCallbacks: Array<(snapshot: ServerProvider) => Effect.Effect<void>> = [];
562+
const enrichmentReady = yield* Deferred.make<void>();
563+
const checkCalls = yield* Ref.make(0);
564+
const provider = yield* makeManagedServerProvider<TestSettings>({
565+
maintenanceCapabilities,
566+
getSettings: Effect.succeed({ enabled: true }),
567+
streamSettings: Stream.empty,
568+
haveSettingsChanged: (previous, next) => previous.enabled !== next.enabled,
569+
initialSnapshot: () => Effect.succeed(initialSnapshot),
570+
checkProvider: Ref.updateAndGet(checkCalls, (count) => count + 1).pipe(
571+
Effect.flatMap((count) =>
572+
count === 1 ? Effect.succeed(refreshedSnapshot) : Effect.fail(probeTimeout),
573+
),
574+
),
575+
enrichSnapshot: ({ publishSnapshot }) =>
576+
Effect.gen(function* () {
577+
publishCallbacks.push(publishSnapshot);
578+
yield* Deferred.succeed(enrichmentReady, undefined).pipe(Effect.ignore);
579+
}),
580+
refreshInterval: "1 hour",
581+
});
582+
583+
yield* Deferred.await(enrichmentReady);
584+
const afterTimeout = yield* provider.refresh;
585+
586+
// Enrichment started against the pre-timeout snapshot. Publishing now
587+
// would restore the old checkedAt and drop the timeout message, which
588+
// is how a manual refresh became a silent no-op in the first place.
589+
yield* publishCallbacks[0]!(enrichedSnapshot);
590+
591+
assert.deepStrictEqual(yield* provider.getSnapshot, afterTimeout);
592+
assert.match(afterTimeout.message ?? "", /timed out after 10000ms/);
593+
}),
594+
).pipe(Effect.provide(AlwaysRunTestLayer)),
595+
);
596+
597+
it.effect("does not carry a failed check forward as if it were a known good status", () =>
598+
Effect.scoped(
599+
Effect.gen(function* () {
600+
const checkCalls = yield* Ref.make(0);
601+
const provider = yield* makeManagedServerProvider<TestSettings>({
602+
maintenanceCapabilities,
603+
getSettings: Effect.succeed({ enabled: true }),
604+
streamSettings: Stream.empty,
605+
haveSettingsChanged: (previous, next) => previous.enabled !== next.enabled,
606+
initialSnapshot: () => Effect.succeed(initialSnapshot),
607+
checkProvider: Ref.updateAndGet(checkCalls, (count) => count + 1).pipe(
608+
Effect.flatMap((count) =>
609+
count === 1 ? Effect.succeed(missingCliSnapshot) : Effect.fail(probeTimeout),
610+
),
611+
),
612+
refreshInterval: "1 hour",
613+
});
614+
615+
yield* Effect.yieldNow;
616+
// The CLI was missing last time and may since have been installed. The
617+
// timeout knows the probe got far enough to spawn, so keeping the old
618+
// "not installed" verdict would be the more wrong of the two answers.
619+
const afterTimeout = yield* provider.refresh;
620+
621+
assert.strictEqual(afterTimeout.installed, true);
622+
assert.match(afterTimeout.message ?? "", /timed out after 10000ms/);
623+
assert.notMatch(afterTimeout.message ?? "", /last known status/);
624+
}),
625+
).pipe(Effect.provide(AlwaysRunTestLayer)),
626+
);
627+
549628
it.effect("reports the timeout plainly when no check has ever succeeded", () =>
550629
Effect.scoped(
551630
Effect.gen(function* () {

‎apps/server/src/provider/makeManagedServerProvider.ts‎

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import * as Duration from "effect/Duration";
99
import * as Effect from "effect/Effect";
1010
import * as Equal from "effect/Equal";
1111
import * as Fiber from "effect/Fiber";
12+
import * as Option from "effect/Option";
1213
import * as PubSub from "effect/PubSub";
1314
import * as Queue from "effect/Queue";
1415
import * as Ref from "effect/Ref";
15-
import * as Schema from "effect/Schema";
1616
import * as Scope from "effect/Scope";
1717
import * as Stream from "effect/Stream";
1818
import * as Semaphore from "effect/Semaphore";
@@ -22,8 +22,6 @@ import { ServerSettingsService } from "../serverSettings.ts";
2222
import { ProviderProbeTimeoutError } from "./providerSnapshot.ts";
2323
import type { ServerProviderShape } from "./Services/ServerProvider.ts";
2424

25-
const isProviderProbeTimeoutError = Schema.is(ProviderProbeTimeoutError);
26-
2725
interface ProviderSnapshotState {
2826
readonly snapshot: ServerProvider;
2927
readonly enrichmentGeneration: number;
@@ -67,9 +65,9 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
6765
enrichmentGeneration: 0,
6866
});
6967
const settingsRef = yield* Ref.make(initialSettings);
70-
// Gates the "keep what we knew" path: before the first successful check there
71-
// is no known status, only the pending placeholder.
72-
const hasCompletedCheck = yield* Ref.make(false);
68+
// Gates the "keep what we knew" path: set once a check produces a status the
69+
// UI can act on, cleared again if a later check reports the provider broken.
70+
const hasUsableStatus = yield* Ref.make(false);
7371
const enrichmentFiberRef = yield* Ref.make<Fiber.Fiber<void, unknown> | null>(null);
7472
const scope = yield* Effect.scope;
7573

@@ -133,17 +131,18 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
133131
* the cache, a manual refresh visibly does something, and the timeout is
134132
* stated rather than leaving the provider silently frozen.
135133
*
136-
* Before the first successful check there is nothing worth carrying forward.
137-
* The pending snapshot describes a check that has not run, not one that ran
138-
* out of time, so the timeout is reported plainly instead.
134+
* Without a usable status there is nothing worth carrying forward. The
135+
* pending snapshot describes a check that has not run, and a failed one
136+
* describes a provider that was already broken, so the timeout is reported
137+
* plainly along with whatever the probe did establish before it gave up.
139138
*/
140139
const snapshotAfterProbeTimeout = Effect.fn("snapshotAfterProbeTimeout")(function* (
141140
error: ProviderProbeTimeoutError,
142141
) {
143142
const checkedAt = DateTime.formatIso(yield* DateTime.now);
144-
const checkedBefore = yield* Ref.get(hasCompletedCheck);
143+
const carryForward = yield* Ref.get(hasUsableStatus);
145144
const next = yield* Ref.modify(snapshotStateRef, (state) => {
146-
const snapshot: ServerProvider = checkedBefore
145+
const snapshot: ServerProvider = carryForward
147146
? {
148147
...state.snapshot,
149148
checkedAt,
@@ -153,11 +152,21 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
153152
...state.snapshot,
154153
checkedAt,
155154
installed: error.installed,
155+
version: error.version ?? state.snapshot.version,
156156
status: "error",
157157
message: error.message,
158158
};
159-
return [snapshot, { ...state, snapshot }] as const;
159+
// Bumping the generation retires any enrichment still running against the
160+
// superseded snapshot, so it cannot publish over this timeout later.
161+
return [
162+
snapshot,
163+
{ snapshot, enrichmentGeneration: state.enrichmentGeneration + 1 },
164+
] as const;
160165
});
166+
const staleEnrichment = yield* Ref.getAndSet(enrichmentFiberRef, null);
167+
if (staleEnrichment) {
168+
yield* Fiber.interrupt(staleEnrichment).pipe(Effect.ignore);
169+
}
161170
yield* PubSub.publish(changesPubSub, next);
162171
return next;
163172
});
@@ -173,26 +182,32 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
173182
return yield* Ref.get(snapshotStateRef).pipe(Effect.map((state) => state.snapshot));
174183
}
175184

176-
const checkOutcome = yield* input.checkProvider.pipe(
177-
Effect.tap(() => Ref.set(hasCompletedCheck, true)),
178-
Effect.catchTag("ProviderProbeTimeoutError", (error) =>
179-
Effect.logWarning("Provider status probe timed out.").pipe(
180-
Effect.annotateLogs({
181-
"provider.name": error.provider,
182-
"provider.probe": error.probe,
183-
"provider.probe.timeout_ms": error.timeoutMs,
184-
}),
185-
Effect.as(error),
186-
),
187-
),
185+
const checked = yield* input.checkProvider.pipe(
186+
// Only a status the UI can act on is worth protecting from a later
187+
// timeout. A verdict of "broken" is not, or a provider that was missing
188+
// when last checked would stay missing after it is installed.
189+
Effect.tap((snapshot) => Ref.set(hasUsableStatus, snapshot.status !== "error")),
190+
Effect.map(Option.some),
191+
Effect.catchTags({
192+
ProviderProbeTimeoutError: (error) =>
193+
Effect.logWarning("Provider status probe timed out.").pipe(
194+
Effect.annotateLogs({
195+
"provider.name": error.provider,
196+
"provider.probe": error.probe,
197+
"provider.probe.timeout_ms": error.timeoutMs,
198+
}),
199+
Effect.andThen(snapshotAfterProbeTimeout(error)),
200+
Effect.as(Option.none<ServerProvider>()),
201+
),
202+
}),
188203
);
189-
if (isProviderProbeTimeoutError(checkOutcome)) {
204+
if (Option.isNone(checked)) {
190205
// `settingsRef` deliberately stays on the previous value: these settings
191206
// were never applied to a snapshot, so the next emission must still count
192207
// as a change and re-probe rather than short-circuiting above.
193-
return yield* snapshotAfterProbeTimeout(checkOutcome);
208+
return yield* Ref.get(snapshotStateRef).pipe(Effect.map((state) => state.snapshot));
194209
}
195-
const nextSnapshot = checkOutcome;
210+
const nextSnapshot = checked.value;
196211
const nextGeneration = yield* Ref.modify(snapshotStateRef, (state) => {
197212
const generation = input.enrichSnapshot
198213
? state.enrichmentGeneration + 1

‎apps/server/src/provider/providerSnapshot.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ export class ProviderProbeTimeoutError extends Schema.TaggedErrorClass<ProviderP
6161
/**
6262
* What the probe had established about the CLI before it ran out of time.
6363
* Only consulted when there is no earlier status to fall back on, so the
64-
* first-ever check still reports something truthful.
64+
* first-ever check still reports everything it did manage to learn.
6565
*/
6666
installed: Schema.Boolean,
67+
version: Schema.optionalKey(Schema.String),
6768
},
6869
) {
6970
override get message(): string {

0 commit comments

Comments
 (0)