fix(server): stop tying codex text-generation temp files to the caller's scope (#5406)

Co-authored-by: Claude Fable 5 <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Fable 5 committed 2026-08-14 11:23:32 +02:00
1 parent 1c31861521
commit 8efd4d2253
2 files changed
+57 -9

No files matched your search

@@ -1,11 +1,13 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as Result from "effect/Result";
import * as Schema from "effect/Schema";
import * as Scope from "effect/Scope";
import { createModelSelection } from "@t3tools/shared/model";
import { expect } from "vite-plus/test";
@@ -400,6 +402,34 @@ it.layer(CodexTextGenerationTestLayer)("CodexTextGeneration", (it) => {
),
);
it.effect("generates branch names even when the ambient scope is already closed", () =>
withFakeCodexEnv(
{
output: JSON.stringify({
branch: "feat/background-generation",
}),
},
(textGeneration) =>
Effect.gen(function* () {
// Background fibers (e.g. the worktree branch rename fork) can run
// after their launching request's scope has closed; temp files must
// not be tied to that ambient scope or they are reaped on creation.
const closedScope = yield* Scope.make();
yield* Scope.close(closedScope, Exit.void);
const generated = yield* textGeneration
.generateBranchName({
cwd: process.cwd(),
message: "Please update session handling.",
modelSelection: DEFAULT_TEST_MODEL_SELECTION,
})
.pipe(Effect.provideService(Scope.Scope, closedScope));
expect(generated.branch).toBe("feat/background-generation");
}),
),
);
it.effect("generates thread titles and trims them for sidebar use", () =>
withFakeCodexEnv(
{
@@ -3,7 +3,6 @@ import * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import * as Scope from "effect/Scope";
import * as Stream from "effect/Stream";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
@@ -72,17 +71,33 @@ export const makeCodexTextGeneration = Effect.fn("makeCodexTextGeneration")(func
),
);
const safeUnlink = (filePath: string): Effect.Effect<void, never> =>
fileSystem.remove(filePath).pipe(Effect.catch(() => Effect.void));
const removeTempFileDir = (filePath: string): Effect.Effect<void, never> =>
fileSystem
.remove(path.dirname(filePath), { recursive: true })
.pipe(Effect.catch(() => Effect.void));
// Deliberately unscoped: text generation runs from background fibers whose
// ambient scope may already be closed (a closed scope reaps the temp
// directory the moment it is created). Each allocation removes its own
// directory on failure; success-path cleanup is explicit in runCodexJson.
const writeTempFile = (
operation: string,
prefix: string,
content: string,
): Effect.Effect<string, TextGenerationError, Scope.Scope> =>
): Effect.Effect<string, TextGenerationError> =>
fileSystem
.makeTempFileScoped({
.makeTempFile({
prefix: `t3code-${prefix}-${process.pid}-`,
})
.pipe(
Effect.tap((filePath) => fileSystem.writeFileString(filePath, content)),
Effect.tap((filePath) =>
fileSystem
.writeFileString(filePath, content)
.pipe(Effect.onError(() => removeTempFileDir(filePath))),
),
Effect.mapError(
(cause) =>
new TextGenerationError({
@@ -93,9 +108,6 @@ export const makeCodexTextGeneration = Effect.fn("makeCodexTextGeneration")(func
),
);
const safeUnlink = (filePath: string): Effect.Effect<void, never> =>
fileSystem.remove(filePath).pipe(Effect.catch(() => Effect.void));
const encodeJsonForOperation = (
operation:
| "generateCommitMessage"
@@ -175,7 +187,9 @@ export const makeCodexTextGeneration = Effect.fn("makeCodexTextGeneration")(func
toJsonSchemaObject(outputSchemaJson),
);
const schemaPath = yield* writeTempFile(operation, "codex-schema", schemaJson);
const outputPath = yield* writeTempFile(operation, "codex-output", "");
const outputPath = yield* writeTempFile(operation, "codex-output", "").pipe(
Effect.onError(() => removeTempFileDir(schemaPath)),
);
const runCodexCommand = Effect.fn("runCodexJson.runCodexCommand")(function* () {
const launchArgs = resolveCodexLaunchArgs(codexConfig.launchArgs, resolvedEnvironment);
@@ -254,7 +268,11 @@ export const makeCodexTextGeneration = Effect.fn("makeCodexTextGeneration")(func
});
const cleanup = Effect.all(
[schemaPath, outputPath, ...cleanupPaths].map((filePath) => safeUnlink(filePath)),
[
removeTempFileDir(schemaPath),
removeTempFileDir(outputPath),
...cleanupPaths.map((filePath) => safeUnlink(filePath)),
],
{
concurrency: "unbounded",
},