Flush native logs on adapter shutdown
- Close the managed native event logger when the adapter layer tears down - Make session runtime close idempotent with an atomic closed flag - Add coverage for flushing thread native logs on shutdown
This commit is contained in:
1 parent
7949333a18
commit
540407ce07
3 files changed
+71
-9
No files matched your search
@@ -1,8 +1,7 @@
|
||||
// @effect-diagnostics nodeBuiltinImport:off
|
||||
import * as NodeAssert from "node:assert/strict";
|
||||
import * as NodeFS from "node:fs";
|
||||
import * as NodeOS from "node:os";
|
||||
import * as NodePath from "node:path";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import {
|
||||
ApprovalRequestId,
|
||||
CodexSettings,
|
||||
@@ -22,7 +21,7 @@ import { createModelSelection } from "@t3tools/shared/model";
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { it, vi } from "@effect/vitest";
|
||||
|
||||
import { Effect, Fiber, Layer, Option, Queue, Scope, Stream } from "effect";
|
||||
import { Effect, Exit, Fiber, Layer, Option, Queue, Scope, Stream } from "effect";
|
||||
import * as CodexErrors from "effect-codex-app-server/errors";
|
||||
|
||||
import { ServerConfig } from "../../config.ts";
|
||||
@@ -1144,3 +1143,61 @@ scopedFailureLayer("CodexAdapterLive scoped startup failure", (it) => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("flushes managed native logs when the adapter layer shuts down", () =>
|
||||
Effect.gen(function* () {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "t3-codex-adapter-native-log-"));
|
||||
const basePath = path.join(tempDir, "provider-native.ndjson");
|
||||
const runtimeFactory = makeRuntimeFactory();
|
||||
const scope = yield* Scope.make("sequential");
|
||||
let scopeClosed = false;
|
||||
|
||||
try {
|
||||
const layer = makeCodexAdapterLive({
|
||||
makeRuntime: runtimeFactory.factory,
|
||||
nativeEventLogPath: basePath,
|
||||
}).pipe(
|
||||
Layer.provideMerge(ServerConfig.layerTest(process.cwd(), process.cwd())),
|
||||
Layer.provideMerge(ServerSettingsService.layerTest()),
|
||||
Layer.provideMerge(providerSessionDirectoryTestLayer),
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
);
|
||||
const context = yield* Layer.buildWithScope(layer, scope);
|
||||
const adapter = yield* Effect.service(CodexAdapter).pipe(Effect.provide(context));
|
||||
|
||||
yield* adapter.startSession({
|
||||
provider: "codex",
|
||||
threadId: asThreadId("thread-logger"),
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
|
||||
const runtime = runtimeFactory.lastRuntime;
|
||||
assert.ok(runtime);
|
||||
|
||||
const firstEventFiber = yield* Stream.runHead(adapter.streamEvents).pipe(Effect.forkChild);
|
||||
yield* runtime.emit({
|
||||
id: asEventId("evt-native-log"),
|
||||
kind: "notification",
|
||||
provider: "codex",
|
||||
threadId: asThreadId("thread-logger"),
|
||||
createdAt: new Date().toISOString(),
|
||||
method: "process/stderr",
|
||||
message: "native flush test",
|
||||
} satisfies ProviderEvent);
|
||||
yield* Fiber.join(firstEventFiber);
|
||||
|
||||
yield* Scope.close(scope, Exit.void);
|
||||
scopeClosed = true;
|
||||
|
||||
const threadLogPath = path.join(tempDir, "thread-logger.log");
|
||||
assert.equal(fs.existsSync(threadLogPath), true);
|
||||
const contents = fs.readFileSync(threadLogPath, "utf8");
|
||||
assert.match(contents, /NTIVE: .*"message":"native flush test"/);
|
||||
} finally {
|
||||
if (!scopeClosed) {
|
||||
yield* Scope.close(scope, Exit.void);
|
||||
}
|
||||
fs.rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
}),
|
||||
);
|
||||
@@ -1350,6 +1350,8 @@ export const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (
|
||||
stream: "native",
|
||||
})
|
||||
: undefined);
|
||||
const managedNativeEventLogger =
|
||||
options?.nativeEventLogger === undefined ? nativeEventLogger : undefined;
|
||||
const serverSettingsService = yield* ServerSettingsService;
|
||||
const runtimeEventQueue = yield* Queue.unbounded<ProviderRuntimeEvent>();
|
||||
const sessions = new Map<ThreadId, CodexAdapterSessionContext>();
|
||||
@@ -1656,7 +1658,11 @@ export const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (
|
||||
}).pipe(Effect.asVoid);
|
||||
|
||||
yield* Effect.acquireRelease(Effect.void, () =>
|
||||
stopAll().pipe(Effect.andThen(Queue.shutdown(runtimeEventQueue)), Effect.ignore),
|
||||
stopAll().pipe(
|
||||
Effect.andThen(Queue.shutdown(runtimeEventQueue)),
|
||||
Effect.andThen(managedNativeEventLogger?.close() ?? Effect.void),
|
||||
Effect.ignore,
|
||||
),
|
||||
);
|
||||
|
||||
return {
|
||||
|
||||
@@ -1135,11 +1135,10 @@ export const makeCodexSessionRuntime = (
|
||||
});
|
||||
|
||||
const close = Effect.gen(function* () {
|
||||
const alreadyClosed = yield* Ref.get(closedRef);
|
||||
const alreadyClosed = yield* Ref.getAndSet(closedRef, true);
|
||||
if (alreadyClosed) {
|
||||
return;
|
||||
}
|
||||
yield* Ref.set(closedRef, true);
|
||||
yield* settlePendingApprovals("cancel");
|
||||
yield* settlePendingUserInputs({});
|
||||
yield* updateSession(sessionRef, {
|
||||
|
||||
Reference in new issue
Block a user