Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import * as ProviderReplayHarness from "../testkit/ProviderReplayHarness.ts";
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
import type * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
import * as ModelManifest from "../../provider/ModelManifest.ts";
import * as PortScanner from "../../preview/PortScanner.ts";
import { resolveProviderCompatibility } from "../../provider/providerCompatibility.ts";
import * as CodexAdapterV2 from "./CodexAdapterV2.ts";
import { withCodexReplayChildMetadata } from "./CodexAdapterV2.testkit.ts";
Expand Down Expand Up @@ -879,6 +880,50 @@ describe("CodexAdapterV2 process exit", () => {
),
),
);

it.effect("counts the app-server as a running agent until its session closes", () => {
let runningAgents = 0;
const factoryLayer = CodexAdapterV2.layerAppServerClientFactory.pipe(
Layer.provide(
Layer.mock(PortScanner.PortDiscovery)({
trackAgent: () => {
runningAgents += 1;
return () => {
runningAgents -= 1;
};
},
}),
),
Layer.provide(
Layer.succeed(ProviderEventLoggers.ProviderEventLoggers, {
native: undefined,
canonical: undefined,
}),
),
Layer.provide(NodeServices.layer),
);
return Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-cli-" });
const binaryPath = `${dir}/codex`;
yield* fs.writeFileString(binaryPath, "#!/bin/sh\ncat > /dev/null\n");
yield* fs.chmod(binaryPath, 0o755);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const factory = yield* CodexAdapterV2.CodexAppServerClientFactory;
yield* Effect.scoped(
factory
.open({
instanceId: CodexAdapterV2.CODEX_DEFAULT_INSTANCE_ID,
threadId: ThreadId.make("thread-codex-agent"),
providerSessionId: ProviderSessionId.make("session-codex-agent"),
runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
settings: { ...DEFAULT_CODEX_SETTINGS, binaryPath },
environment: {},
})
.pipe(Effect.tap(() => Effect.sync(() => assert.equal(runningAgents, 1)))),
);
assert.equal(runningAgents, 0);
}).pipe(Effect.scoped, Effect.provide(Layer.merge(factoryLayer, NodeServices.layer)));
});
});

describe("CodexAdapterV2 session initialize", () => {
Expand Down
9 changes: 8 additions & 1 deletion apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1516,13 +1516,16 @@ function isSensitiveCodexProtocolKey(key: string): boolean {
export const layerAppServerClientFactory: Layer.Layer<
CodexAppServerClientFactory,
never,
ChildProcessSpawner.ChildProcessSpawner | ProviderEventLoggers.ProviderEventLoggers
| ChildProcessSpawner.ChildProcessSpawner
| PortScanner.PortDiscovery
| ProviderEventLoggers.ProviderEventLoggers
> = Layer.effect(
CodexAppServerClientFactory,
Effect.gen(function* () {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const { native: nativeEventLogger } = yield* ProviderEventLoggers.ProviderEventLoggers;
const agentScope = yield* AgentScope;
const portDiscovery = yield* PortScanner.PortDiscovery;

return CodexAppServerClientFactory.of({
open: Effect.fn("CodexAdapterV2.open")(function* (input) {
Expand Down Expand Up @@ -1550,6 +1553,10 @@ export const layerAppServerClientFactory: Layer.Layer<
}),
),
);
// Its shells carry each thread's T3CODE_THREAD_ID, so port discovery reads listeners' threads.
yield* Effect.acquireRelease(Effect.sync(portDiscovery.trackAgent), (release) =>
Effect.sync(release),
);
const protocolLogger = makeCodexAppServerProtocolLogger({
nativeEventLogger,
threadId: input.threadId,
Expand Down
Loading