fix(opencode): handle child approvals, stops, and model catalogs (#8480)
This commit is contained in:
1 parent
0009aacdf1
commit
cb49e5d720
42 files changed
+7889
-581
No files matched your search
@@ -1,9 +1,5 @@
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Exit from "effect/Exit";
|
||||
import * as Fiber from "effect/Fiber";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as Scope from "effect/Scope";
|
||||
import * as Semaphore from "effect/Semaphore";
|
||||
|
||||
import {
|
||||
NonNegativeInt,
|
||||
@@ -31,8 +27,7 @@ import {
|
||||
sanitizeThreadTitle,
|
||||
} from "./TextGenerationUtils.ts";
|
||||
import * as OpenCodeRuntime from "../provider/opencodeRuntime.ts";
|
||||
|
||||
const OPENCODE_TEXT_GENERATION_IDLE_TTL = "30 seconds";
|
||||
import * as OpenCodeServerOwner from "../provider/OpenCodeServerOwner.ts";
|
||||
|
||||
const OpenCodeTextGenerationOperation = Schema.Literals([
|
||||
"generateCommitMessage",
|
||||
@@ -175,188 +170,12 @@ function getOpenCodeTextResponse(parts: ReadonlyArray<unknown> | undefined): str
|
||||
.trim();
|
||||
}
|
||||
|
||||
interface SharedOpenCodeTextGenerationServerState {
|
||||
server: OpenCodeRuntime.OpenCodeServerProcess | null;
|
||||
/**
|
||||
* The scope that owns the shared server's lifetime. Closing this scope
|
||||
* terminates the OpenCode child process and interrupts any fibers the
|
||||
* runtime forked during startup. We don't hold a `close()` function on
|
||||
* the server handle anymore — the scope is the only lifecycle handle.
|
||||
*/
|
||||
serverScope: Scope.Closeable | null;
|
||||
binaryPath: string | null;
|
||||
activeRequests: number;
|
||||
idleCloseFiber: Fiber.Fiber<void, never> | null;
|
||||
}
|
||||
|
||||
export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration")(function* (
|
||||
openCodeSettings: OpenCodeSettings,
|
||||
environment?: NodeJS.ProcessEnv,
|
||||
) {
|
||||
const serverConfig = yield* ServerConfig.ServerConfig;
|
||||
const openCodeRuntime = yield* OpenCodeRuntime.OpenCodeRuntime;
|
||||
const resolvedEnvironment = environment ?? process.env;
|
||||
const idleFiberScope = yield* Effect.acquireRelease(Scope.make(), (scope) =>
|
||||
Scope.close(scope, Exit.void),
|
||||
);
|
||||
const sharedServerMutex = yield* Semaphore.make(1);
|
||||
const sharedServerState: SharedOpenCodeTextGenerationServerState = {
|
||||
server: null,
|
||||
serverScope: null,
|
||||
binaryPath: null,
|
||||
activeRequests: 0,
|
||||
idleCloseFiber: null,
|
||||
};
|
||||
|
||||
const closeSharedServer = Effect.fn("closeSharedServer")(function* () {
|
||||
const scope = sharedServerState.serverScope;
|
||||
sharedServerState.server = null;
|
||||
sharedServerState.serverScope = null;
|
||||
sharedServerState.binaryPath = null;
|
||||
if (scope !== null) {
|
||||
yield* Scope.close(scope, Exit.void).pipe(Effect.ignore);
|
||||
}
|
||||
});
|
||||
|
||||
const cancelIdleCloseFiber = Effect.fn("cancelIdleCloseFiber")(function* () {
|
||||
const idleCloseFiber = sharedServerState.idleCloseFiber;
|
||||
sharedServerState.idleCloseFiber = null;
|
||||
if (idleCloseFiber !== null) {
|
||||
yield* Fiber.interrupt(idleCloseFiber).pipe(Effect.ignore);
|
||||
}
|
||||
});
|
||||
|
||||
const scheduleIdleClose = Effect.fn("scheduleIdleClose")(function* (
|
||||
server: OpenCodeRuntime.OpenCodeServerProcess,
|
||||
) {
|
||||
yield* cancelIdleCloseFiber();
|
||||
const fiber = yield* Effect.sleep(OPENCODE_TEXT_GENERATION_IDLE_TTL).pipe(
|
||||
Effect.andThen(
|
||||
sharedServerMutex.withPermit(
|
||||
Effect.gen(function* () {
|
||||
if (sharedServerState.server !== server || sharedServerState.activeRequests > 0) {
|
||||
return;
|
||||
}
|
||||
sharedServerState.idleCloseFiber = null;
|
||||
yield* closeSharedServer();
|
||||
}),
|
||||
),
|
||||
),
|
||||
Effect.forkIn(idleFiberScope),
|
||||
);
|
||||
sharedServerState.idleCloseFiber = fiber;
|
||||
});
|
||||
|
||||
const acquireSharedServer = (input: {
|
||||
readonly binaryPath: string;
|
||||
readonly operation:
|
||||
| "generateCommitMessage"
|
||||
| "generatePrContent"
|
||||
| "generateBranchName"
|
||||
| "generateThreadTitle";
|
||||
}) =>
|
||||
sharedServerMutex.withPermit(
|
||||
Effect.gen(function* () {
|
||||
yield* cancelIdleCloseFiber();
|
||||
|
||||
const existingServer = sharedServerState.server;
|
||||
if (existingServer !== null) {
|
||||
if (
|
||||
sharedServerState.binaryPath !== input.binaryPath &&
|
||||
sharedServerState.activeRequests === 0
|
||||
) {
|
||||
yield* closeSharedServer();
|
||||
} else {
|
||||
if (sharedServerState.binaryPath !== input.binaryPath) {
|
||||
yield* Effect.logWarning(
|
||||
"OpenCode shared server binary path mismatch: requested " +
|
||||
input.binaryPath +
|
||||
" but active server uses " +
|
||||
sharedServerState.binaryPath +
|
||||
"; reusing existing server because there are active requests",
|
||||
);
|
||||
}
|
||||
sharedServerState.activeRequests += 1;
|
||||
return existingServer;
|
||||
}
|
||||
}
|
||||
|
||||
// Create a fresh scope that owns this shared server. The runtime
|
||||
// will attach its child-process and fiber finalizers to this scope;
|
||||
// closing it kills the server and interrupts those fibers.
|
||||
//
|
||||
// The `Scope.make` / spawn / record-or-close transitions run inside
|
||||
// `uninterruptibleMask` so an interrupt arriving between any two
|
||||
// steps can't orphan the scope (and the child process attached to
|
||||
// it) before we either close it on failure or hand ownership to
|
||||
// `sharedServerState`. `restore` keeps the actual spawn
|
||||
// interruptible; an interrupt during the spawn is captured by
|
||||
// `Effect.exit` and drives us through the failure branch that
|
||||
// closes the fresh scope.
|
||||
return yield* Effect.uninterruptibleMask((restore) =>
|
||||
Effect.gen(function* () {
|
||||
const serverScope = yield* Scope.make();
|
||||
const startedExit = yield* Effect.exit(
|
||||
restore(
|
||||
openCodeRuntime
|
||||
.startOpenCodeServerProcess({
|
||||
binaryPath: input.binaryPath,
|
||||
environment: resolvedEnvironment,
|
||||
})
|
||||
.pipe(
|
||||
Effect.provideService(Scope.Scope, serverScope),
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new TextGenerationError({
|
||||
operation: input.operation,
|
||||
detail: OpenCodeRuntime.openCodeRuntimeErrorDetail(cause),
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
if (startedExit._tag === "Failure") {
|
||||
yield* Scope.close(serverScope, Exit.void).pipe(Effect.ignore);
|
||||
return yield* Effect.failCause(startedExit.cause);
|
||||
}
|
||||
|
||||
const server = startedExit.value;
|
||||
sharedServerState.server = server;
|
||||
sharedServerState.serverScope = serverScope;
|
||||
sharedServerState.binaryPath = input.binaryPath;
|
||||
sharedServerState.activeRequests = 1;
|
||||
return server;
|
||||
}),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
const releaseSharedServer = (server: OpenCodeRuntime.OpenCodeServerProcess) =>
|
||||
sharedServerMutex.withPermit(
|
||||
Effect.gen(function* () {
|
||||
if (sharedServerState.server !== server) {
|
||||
return;
|
||||
}
|
||||
sharedServerState.activeRequests = Math.max(0, sharedServerState.activeRequests - 1);
|
||||
if (sharedServerState.activeRequests === 0) {
|
||||
yield* scheduleIdleClose(server);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
// Module-level finalizer: on layer shutdown, cancel the idle close fiber
|
||||
// and close the shared server scope. Consumers therefore cannot leak
|
||||
// the shared OpenCode server by forgetting to call anything.
|
||||
yield* Effect.addFinalizer(() =>
|
||||
sharedServerMutex.withPermit(
|
||||
Effect.gen(function* () {
|
||||
yield* cancelIdleCloseFiber();
|
||||
sharedServerState.activeRequests = 0;
|
||||
yield* closeSharedServer();
|
||||
}),
|
||||
),
|
||||
);
|
||||
const serverOwner = yield* OpenCodeServerOwner.OpenCodeServerOwner;
|
||||
|
||||
const runOpenCodeJson = Effect.fn("runOpenCodeJson")(function* <S extends Schema.Top>(input: {
|
||||
readonly operation: OpenCodeTextGenerationOperation;
|
||||
@@ -381,13 +200,16 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration"
|
||||
});
|
||||
|
||||
const runAgainstServer = Effect.fn("runOpenCodeJson.runAgainstServer")(
|
||||
function* (server: Pick<OpenCodeRuntime.OpenCodeServerConnection, "url">) {
|
||||
function* (
|
||||
server: Pick<
|
||||
OpenCodeRuntime.OpenCodeServerConnection,
|
||||
"url" | "serverPassword" | "version"
|
||||
>,
|
||||
) {
|
||||
const client = openCodeRuntime.createOpenCodeSdkClient({
|
||||
baseUrl: server.url,
|
||||
directory: input.cwd,
|
||||
...(openCodeSettings.serverUrl.length > 0 && openCodeSettings.serverPassword
|
||||
? { serverPassword: openCodeSettings.serverPassword }
|
||||
: {}),
|
||||
...(server.serverPassword !== undefined ? { serverPassword: server.serverPassword } : {}),
|
||||
});
|
||||
const session = yield* Effect.tryPromise({
|
||||
try: () =>
|
||||
@@ -496,17 +318,31 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration"
|
||||
}),
|
||||
);
|
||||
|
||||
const rawOutput =
|
||||
const serverOutput =
|
||||
openCodeSettings.serverUrl.length > 0
|
||||
? yield* runAgainstServer({ url: openCodeSettings.serverUrl })
|
||||
: yield* Effect.acquireUseRelease(
|
||||
acquireSharedServer({
|
||||
? openCodeRuntime
|
||||
.connectToOpenCodeServer({
|
||||
binaryPath: openCodeSettings.binaryPath,
|
||||
directory: input.cwd,
|
||||
serverUrl: openCodeSettings.serverUrl,
|
||||
...(openCodeSettings.serverPassword
|
||||
? { serverPassword: openCodeSettings.serverPassword }
|
||||
: {}),
|
||||
})
|
||||
.pipe(Effect.flatMap(runAgainstServer), Effect.scoped)
|
||||
: serverOwner.withServer(runAgainstServer);
|
||||
const rawOutput = yield* serverOutput.pipe(
|
||||
Effect.catchTags({
|
||||
OpenCodeRuntimeError: (cause) =>
|
||||
Effect.fail(
|
||||
new TextGenerationError({
|
||||
operation: input.operation,
|
||||
detail: OpenCodeRuntime.openCodeRuntimeErrorDetail(cause),
|
||||
cause,
|
||||
}),
|
||||
runAgainstServer,
|
||||
releaseSharedServer,
|
||||
);
|
||||
),
|
||||
}),
|
||||
);
|
||||
|
||||
const decodeOutput = Schema.decodeEffect(Schema.fromJsonString(input.outputSchemaJson));
|
||||
return yield* decodeOutput(extractJsonObject(rawOutput)).pipe(
|
||||
|
||||
Reference in new issue
Block a user