Files
t3code/apps/server/src/provider/OpenCodeServerOwner.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

189 lines
5.9 KiB
TypeScript

import * as Context from "effect/Context";
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 Scope from "effect/Scope";
import * as Semaphore from "effect/Semaphore";
import * as OpenCodeRuntime from "./opencodeRuntime.ts";
const OPENCODE_SERVER_IDLE_TTL = "30 seconds";
interface OpenCodeServerOwnerState {
server: OpenCodeRuntime.OpenCodeServerProcess | null;
serverScope: Scope.Closeable | null;
borrowers: number;
idleCloseFiber: Fiber.Fiber<void, never> | null;
}
export class OpenCodeServerOwner extends Context.Service<
OpenCodeServerOwner,
{
readonly withServer: <A, E, R>(
use: (server: OpenCodeRuntime.OpenCodeServerProcess) => Effect.Effect<A, E, R>,
) => Effect.Effect<A, E | OpenCodeRuntime.OpenCodeRuntimeError, R>;
}
>()("t3/provider/OpenCodeServerOwner") {}
/** Owns the lazy local OpenCode server shared by one provider instance. */
export const make = Effect.fn("OpenCodeServerOwner.make")(function* (input: {
readonly binaryPath: string;
readonly directory: string;
readonly serverPassword?: string;
readonly environment?: NodeJS.ProcessEnv;
readonly verify?: (url: string) => Effect.Effect<string, OpenCodeRuntime.OpenCodeRuntimeError>;
}) {
const runtime = yield* OpenCodeRuntime.OpenCodeRuntime;
const ownerScope = yield* Effect.acquireRelease(Scope.make(), (scope) =>
Scope.close(scope, Exit.void),
);
const mutex = yield* Semaphore.make(1);
const state: OpenCodeServerOwnerState = {
server: null,
serverScope: null,
borrowers: 0,
idleCloseFiber: null,
};
const cancelIdleClose = Effect.fn("OpenCodeServerOwner.cancelIdleClose")(function* () {
const fiber = state.idleCloseFiber;
state.idleCloseFiber = null;
if (fiber !== null) {
yield* Fiber.interrupt(fiber).pipe(Effect.ignore);
}
});
const closeServer = Effect.fn("OpenCodeServerOwner.closeServer")(function* (
expected?: OpenCodeRuntime.OpenCodeServerProcess,
) {
if (expected !== undefined && state.server !== expected) {
return;
}
const scope = state.serverScope;
state.server = null;
state.serverScope = null;
if (scope !== null) {
yield* Scope.close(scope, Exit.void).pipe(Effect.ignore);
}
});
const watchServerExit = Effect.fn("OpenCodeServerOwner.watchServerExit")(function* (
server: OpenCodeRuntime.OpenCodeServerProcess,
) {
yield* server.exitCode;
yield* mutex.withPermit(
Effect.gen(function* () {
if (state.server !== server) {
return;
}
yield* cancelIdleClose();
yield* closeServer(server);
}),
);
});
const acquireServer = mutex.withPermit(
Effect.gen(function* () {
yield* cancelIdleClose();
if (state.server !== null) {
if (yield* state.server.isRunning) {
state.borrowers += 1;
return state.server;
}
yield* closeServer(state.server);
}
return yield* Effect.uninterruptibleMask((restore) =>
Effect.gen(function* () {
const serverScope = yield* Scope.make();
const started = yield* Effect.exit(
restore(
runtime
.startOpenCodeServerProcess({
binaryPath: input.binaryPath,
directory: input.directory,
...(input.serverPassword !== undefined
? { serverPassword: input.serverPassword }
: {}),
...(input.environment ? { environment: input.environment } : {}),
...(input.verify ? { verify: input.verify } : {}),
})
.pipe(Effect.provideService(Scope.Scope, serverScope)),
),
);
if (Exit.isFailure(started)) {
yield* Scope.close(serverScope, Exit.void).pipe(Effect.ignore);
return yield* Effect.failCause(started.cause);
}
const server = started.value;
state.server = server;
state.serverScope = serverScope;
state.borrowers = 1;
yield* watchServerExit(server).pipe(Effect.forkIn(ownerScope));
return server;
}),
);
}),
);
const releaseServer = (server: OpenCodeRuntime.OpenCodeServerProcess) =>
mutex.withPermit(
Effect.gen(function* () {
if (state.server !== server) {
return;
}
state.borrowers = Math.max(0, state.borrowers - 1);
if (state.borrowers > 0) {
return;
}
yield* cancelIdleClose();
state.idleCloseFiber = yield* Effect.sleep(OPENCODE_SERVER_IDLE_TTL).pipe(
Effect.andThen(
mutex.withPermit(
Effect.gen(function* () {
if (state.server !== server || state.borrowers > 0) {
return;
}
state.idleCloseFiber = null;
yield* closeServer(server);
}),
),
),
Effect.forkIn(ownerScope),
);
}),
);
yield* Effect.addFinalizer(() =>
mutex.withPermit(
Effect.gen(function* () {
yield* cancelIdleClose();
state.borrowers = 0;
yield* closeServer();
}),
),
);
return OpenCodeServerOwner.of({
withServer: (use) =>
Effect.uninterruptibleMask((restore) =>
restore(acquireServer).pipe(
Effect.flatMap((server) =>
restore(use(server)).pipe(Effect.ensuring(releaseServer(server))),
),
),
),
});
});
/** @public Service construction is part of the canonical Effect module API. */
export const layer = (input: {
readonly binaryPath: string;
readonly directory: string;
readonly serverPassword?: string;
readonly environment?: NodeJS.ProcessEnv;
readonly verify?: (url: string) => Effect.Effect<string, OpenCodeRuntime.OpenCodeRuntimeError>;
}) => Layer.effect(OpenCodeServerOwner, make(input));