Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
Preserve Codex probe results across scoped teardown (#2638)
Julius Marminge8fc317939f5c
diff --git a/apps/server/src/provider/Layers/CodexProvider.ts b/apps/server/src/provider/Layers/CodexProvider.tsindex 6841591d0..178450fb7 100644--- a/apps/server/src/provider/Layers/CodexProvider.ts+++ b/apps/server/src/provider/Layers/CodexProvider.ts@@ -5,6 +5,7 @@ import * as Layer from "effect/Layer";import * as Option from "effect/Option";import * as Result from "effect/Result";import * as Schema from "effect/Schema";+import * as Scope from "effect/Scope";import * as Types from "effect/Types";import { ChildProcessSpawner } from "effect/unstable/process";import * as CodexClient from "effect-codex-app-server/client";@@ -28,7 +29,6 @@ import {type ServerProviderDraft,} from "../providerSnapshot.ts";import { expandHomePath } from "../../pathExpansion.ts";-import { scopedSafeTeardown } from "./scopedSafeTeardown.ts";import packageJson from "../../../package.json" with { type: "json" };const isCodexAppServerSpawnError = Schema.is(CodexErrors.CodexAppServerSpawnError);@@ -248,12 +248,6 @@ export function buildCodexInitializeParams(): CodexSchema.V1InitializeParams {};}-// Wrapped with `scopedSafeTeardown("codex-probe")` rather than the usual-// `Effect.scoped` so that a defect from the `Layer.build` finalizer (e.g.-// `ChildProcess.kill` throwing because the `codex app-server` child exited-// early) cannot override a successful probe body. Without this guard the-// defect bubbles past `Effect.result` in `checkCodexProviderStatus`, dies-// `refreshOneSource`, and `providersRef` never receives the snapshot.const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(function* (input: {readonly binaryPath: string;readonly homePath?: string;@@ -323,7 +317,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(funmodels: appendCustomCodexModels(models, input.customModels ?? []),skills: parseCodexSkillsListResponse(skillsResponse, input.cwd),} satisfies CodexAppServerProviderSnapshot;-}, scopedSafeTeardown("codex-probe"));+});const emptyCodexModelsFromSettings = (codexSettings: CodexSettings): ServerProvider["models"] =>codexSettings.customModels@@ -416,7 +410,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu}) => Effect.Effect<CodexAppServerProviderSnapshot,CodexErrors.CodexAppServerError,- ChildProcessSpawner.ChildProcessSpawner+ ChildProcessSpawner.ChildProcessSpawner | Scope.Scope> = probeCodexAppServerProvider,environment: NodeJS.ProcessEnv = process.env,): Effect.fn.Return<@@ -450,7 +444,11 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fucwd: process.cwd(),customModels: codexSettings.customModels,environment,- }).pipe(Effect.timeoutOption(Duration.millis(AUTH_PROBE_TIMEOUT_MS)), Effect.result);+ }).pipe(+ Effect.scoped,+ Effect.timeoutOption(Duration.millis(AUTH_PROBE_TIMEOUT_MS)),+ Effect.result,+ );if (Result.isFailure(probeResult)) {const error = probeResult.failure;diff --git a/apps/server/src/provider/Layers/CodexSessionRuntime.ts b/apps/server/src/provider/Layers/CodexSessionRuntime.tsindex a2b54fac2..7f71ef46b 100644--- a/apps/server/src/provider/Layers/CodexSessionRuntime.ts+++ b/apps/server/src/provider/Layers/CodexSessionRuntime.ts@@ -53,6 +53,7 @@ const BENIGN_ERROR_LOG_SNIPPETS = ["state db missing rollout path for thread","state db record_discrepancy: find_thread_path_by_id_str_in_subdir, falling_back",];+const CODEX_APP_SERVER_FORCE_KILL_AFTER = "2 seconds" as const;const RECOVERABLE_THREAD_RESUME_ERROR_SNIPPETS = ["not found","missing thread",@@ -721,6 +722,7 @@ export const makeCodexSessionRuntime = (ChildProcess.make(options.binaryPath, ["app-server"], {cwd: options.cwd,env,+ forceKillAfter: CODEX_APP_SERVER_FORCE_KILL_AFTER,shell: process.platform === "win32",}),)diff --git a/apps/server/src/provider/Layers/ProviderRegistry.test.ts b/apps/server/src/provider/Layers/ProviderRegistry.test.tsindex 455cac1eb..fb6eb3b44 100644--- a/apps/server/src/provider/Layers/ProviderRegistry.test.ts+++ b/apps/server/src/provider/Layers/ProviderRegistry.test.ts@@ -2,6 +2,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";import { describe, it, assert } from "@effect/vitest";import * as Effect from "effect/Effect";import * as Exit from "effect/Exit";+import * as Fiber from "effect/Fiber";import * as Layer from "effect/Layer";import * as PubSub from "effect/PubSub";import * as Ref from "effect/Ref";@@ -209,6 +210,31 @@ function failingSpawnerLayer(description: string) {);}+function hangingScopedSpawnerLayer(killCalls: Ref.Ref<number>) {+ return Layer.succeed(+ ChildProcessSpawner.ChildProcessSpawner,+ ChildProcessSpawner.make(() =>+ Effect.gen(function* () {+ const handle = ChildProcessSpawner.makeHandle({+ pid: ChildProcessSpawner.ProcessId(1),+ exitCode: Effect.never,+ isRunning: Effect.succeed(true),+ kill: () => Ref.update(killCalls, (current) => current + 1),+ unref: Effect.succeed(Effect.void),+ stdin: Sink.drain,+ stdout: Stream.never,+ stderr: Stream.never,+ all: Stream.never,+ getInputFd: () => Sink.drain,+ getOutputFd: () => Stream.empty,+ });+ yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));+ return handle;+ }),+ ),+ );+}+const codexModelCapabilities = createModelCapabilities({optionDescriptors: [selectDescriptor("reasoningEffort", "Reasoning", [@@ -421,6 +447,28 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsService.layerTest(), T);}),);++ it.effect("closes the app-server probe scope when provider status times out", () =>+ Effect.gen(function* () {+ const killCalls = yield* Ref.make(0);+ const statusFiber = yield* checkCodexProviderStatus(defaultCodexSettings).pipe(+ Effect.provide(hangingScopedSpawnerLayer(killCalls)),+ Effect.forkChild,+ );++ yield* Effect.yieldNow;+ yield* TestClock.adjust("11 seconds");+ yield* Effect.yieldNow;++ const status = yield* Fiber.join(statusFiber);+ assert.strictEqual(status.status, "error");+ assert.strictEqual(+ status.message,+ "Timed out while checking Codex app-server provider status.",+ );+ assert.strictEqual(yield* Ref.get(killCalls), 1);+ }),+ );});describe("ProviderRegistryLive", () => {diff --git a/apps/server/src/provider/Layers/scopedSafeTeardown.test.ts b/apps/server/src/provider/Layers/scopedSafeTeardown.test.tsdeleted file mode 100644index 5bd932505..000000000--- a/apps/server/src/provider/Layers/scopedSafeTeardown.test.ts+++ /dev/null@@ -1,96 +0,0 @@-import { it } from "@effect/vitest";-import * as Cause from "effect/Cause";-import * as Effect from "effect/Effect";-import * as Exit from "effect/Exit";-import { describe, expect } from "vitest";--import { scopedSafeTeardown } from "./scopedSafeTeardown.ts";--describe("scopedSafeTeardown", () => {- it.effect("returns the body's value when teardown is clean", () =>- Effect.gen(function* () {- const finalizers: string[] = [];- const wrapped = Effect.gen(function* () {- yield* Effect.addFinalizer(() =>- Effect.sync(() => {- finalizers.push("clean");- }),- );- return "body-ok";- }).pipe(scopedSafeTeardown("test"));-- const value = yield* wrapped;- expect(value).toBe("body-ok");- expect(finalizers).toEqual(["clean"]);- }),- );-- it.effect("preserves body success when a finalizer dies", () =>- // The production failure mode: `Layer.build(...)` registers a finalizer- // that kills a subprocess; if the kill fails, the defect would otherwise- // override a successful probe body.- Effect.gen(function* () {- const finalizers: string[] = [];- const wrapped = Effect.gen(function* () {- yield* Effect.addFinalizer(() =>- Effect.sync(() => {- finalizers.push("ran-before-die");- }),- );- yield* Effect.addFinalizer(() =>- Effect.die(new Error("simulated subprocess kill failure")),- );- return "body-ok";- }).pipe(scopedSafeTeardown("test"));-- const value = yield* wrapped;- expect(value).toBe("body-ok");- // The clean finalizer still ran; teardown defect was logged + swallowed.- expect(finalizers).toEqual(["ran-before-die"]);- }),- );-- it.effect("preserves typed body failures even when teardown is clean", () =>- Effect.gen(function* () {- class BodyError {- readonly _tag = "BodyError" as const;- }- const wrapped = Effect.gen(function* () {- yield* Effect.addFinalizer(() => Effect.void);- return yield* Effect.fail(new BodyError());- }).pipe(scopedSafeTeardown("test"));-- const exit = yield* Effect.exit(wrapped);- expect(Exit.isFailure(exit)).toBe(true);- if (Exit.isFailure(exit)) {- // Body's typed failure should surface, not a defect.- const squashed = Cause.squash(exit.cause);- expect(squashed).toBeInstanceOf(BodyError);- }- }),- );-- it.effect("prefers the body's typed failure over a teardown defect", () =>- // Even when both the body fails AND teardown defects, the body's typed- // failure is what callers see. This matters because `Effect.result` /- // `.pipe(Effect.exit)` in callers expects a typed Failure, not a Die.- Effect.gen(function* () {- class BodyError {- readonly _tag = "BodyError" as const;- }- const wrapped = Effect.gen(function* () {- yield* Effect.addFinalizer(() =>- Effect.die(new Error("simulated subprocess kill failure")),- );- return yield* Effect.fail(new BodyError());- }).pipe(scopedSafeTeardown("test"));-- const exit = yield* Effect.exit(wrapped);- expect(Exit.isFailure(exit)).toBe(true);- if (Exit.isFailure(exit)) {- const squashed = Cause.squash(exit.cause);- expect(squashed).toBeInstanceOf(BodyError);- }- }),- );-});diff --git a/apps/server/src/provider/Layers/scopedSafeTeardown.ts b/apps/server/src/provider/Layers/scopedSafeTeardown.tsdeleted file mode 100644index 8a1bc829c..000000000--- a/apps/server/src/provider/Layers/scopedSafeTeardown.ts+++ /dev/null@@ -1,63 +0,0 @@-/**- * scopedSafeTeardown — run a scope-requiring effect so that finalizer- * failures during scope close cannot override the body's Exit.- *- * Motivation- * ----------- * The obvious pattern is `body.pipe(Effect.scoped)`: provide a fresh- * Scope, run the body, close the scope with the body's Exit. If a- * finalizer (e.g. `ChildProcess.kill` from an effect-codex-app-server- * spawn) dies during that close, the combined Exit becomes the- * finalizer's defect — even when the body already succeeded.- *- * Concretely this bit us in the Codex provider probe: a successful- * `initialize` → `account/read` → `skills/list` → `model/list`- * round-trip produced a `CodexAppServerProviderSnapshot`, but the- * `Layer.build(CodexClient.layerCommand(...))` finalizer then failed to- * kill the `codex app-server` subprocess with a `PlatformError`. The- * defect bubbled past `Effect.result` in `checkCodexProviderStatus`,- * died `refreshOneSource`, and `providersRef` never saw the snapshot.- *- * Strategy- * --------- * 1. Make a fresh scope manually.- * 2. Run the body against that scope, capturing its Exit via- * `Effect.exit`.- * 3. Close the scope, catching any cause (typed failure *or* defect)- * with a log.- * 4. Replay the captured Exit so typed body failures still surface and- * successes still return their value.- *- * The helper deliberately logs teardown causes at `Warning` level —- * silently swallowing them is dangerous because they usually indicate a- * real bug in a downstream Layer's finalizer.- *- * @module provider/Layers/scopedSafeTeardown- */-import * as Effect from "effect/Effect";-import * as Exit from "effect/Exit";-import * as Scope from "effect/Scope";--/**- * Run `effect` with a freshly made `Scope.Scope`, guaranteeing that- * teardown failures cannot override the body's Exit.- *- * Shape matches `Effect.scoped`: takes an effect whose env includes- * `Scope.Scope`, returns one whose env excludes it.- *- * @param label Short label for the warning log emitted when teardown- * fails. Use something like `"codex-probe"`.- */-export const scopedSafeTeardown =- (label: string) =>- <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, Exclude<R, Scope.Scope>> =>- Effect.gen(function* () {- const scope = yield* Scope.make();- const bodyExit = yield* effect.pipe(Effect.provideService(Scope.Scope, scope), Effect.exit);- yield* Scope.close(scope, Exit.void).pipe(- Effect.catchCause((cause) =>- Effect.logWarning(`${label} teardown errored; preserving body result`, cause),- ),- );- return yield* bodyExit;- }) as Effect.Effect<A, E, Exclude<R, Scope.Scope>>;