- Add local trace file output and OTLP export hooks - Instrument RPC, git, orchestration, provider, and DB paths - Document observability configuration
476 lines
19 KiB
TypeScript
476 lines
19 KiB
TypeScript
import { Effect, Layer, Option, Queue, Ref, Schema, Stream } from "effect";
|
|
import {
|
|
type GitActionProgressEvent,
|
|
type GitManagerServiceError,
|
|
OrchestrationDispatchCommandError,
|
|
type OrchestrationEvent,
|
|
OrchestrationGetFullThreadDiffError,
|
|
OrchestrationGetSnapshotError,
|
|
OrchestrationGetTurnDiffError,
|
|
ORCHESTRATION_WS_METHODS,
|
|
ProjectSearchEntriesError,
|
|
ProjectWriteFileError,
|
|
OrchestrationReplayEventsError,
|
|
type TerminalEvent,
|
|
WS_METHODS,
|
|
WsRpcGroup,
|
|
} from "@t3tools/contracts";
|
|
import { clamp } from "effect/Number";
|
|
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/unstable/http";
|
|
import { RpcSerialization, RpcServer } from "effect/unstable/rpc";
|
|
|
|
import { CheckpointDiffQuery } from "./checkpointing/Services/CheckpointDiffQuery";
|
|
import { ServerConfig } from "./config";
|
|
import { GitCore } from "./git/Services/GitCore";
|
|
import { GitManager } from "./git/Services/GitManager";
|
|
import { Keybindings } from "./keybindings";
|
|
import { Open, resolveAvailableEditors } from "./open";
|
|
import { normalizeDispatchCommand } from "./orchestration/Normalizer";
|
|
import { OrchestrationEngineService } from "./orchestration/Services/OrchestrationEngine";
|
|
import { ProjectionSnapshotQuery } from "./orchestration/Services/ProjectionSnapshotQuery";
|
|
import {
|
|
observeRpcEffect,
|
|
observeRpcStream,
|
|
observeRpcStreamEffect,
|
|
} from "./observability/RpcInstrumentation";
|
|
import { ProviderRegistry } from "./provider/Services/ProviderRegistry";
|
|
import { ServerLifecycleEvents } from "./serverLifecycleEvents";
|
|
import { ServerRuntimeStartup } from "./serverRuntimeStartup";
|
|
import { ServerSettingsService } from "./serverSettings";
|
|
import { TerminalManager } from "./terminal/Services/Manager";
|
|
import { WorkspaceEntries } from "./workspace/Services/WorkspaceEntries";
|
|
import { WorkspaceFileSystem } from "./workspace/Services/WorkspaceFileSystem";
|
|
import { WorkspacePathOutsideRootError } from "./workspace/Services/WorkspacePaths";
|
|
|
|
const WsRpcLayer = WsRpcGroup.toLayer(
|
|
Effect.gen(function* () {
|
|
const projectionSnapshotQuery = yield* ProjectionSnapshotQuery;
|
|
const orchestrationEngine = yield* OrchestrationEngineService;
|
|
const checkpointDiffQuery = yield* CheckpointDiffQuery;
|
|
const keybindings = yield* Keybindings;
|
|
const open = yield* Open;
|
|
const gitManager = yield* GitManager;
|
|
const git = yield* GitCore;
|
|
const terminalManager = yield* TerminalManager;
|
|
const providerRegistry = yield* ProviderRegistry;
|
|
const config = yield* ServerConfig;
|
|
const lifecycleEvents = yield* ServerLifecycleEvents;
|
|
const serverSettings = yield* ServerSettingsService;
|
|
const startup = yield* ServerRuntimeStartup;
|
|
const workspaceEntries = yield* WorkspaceEntries;
|
|
const workspaceFileSystem = yield* WorkspaceFileSystem;
|
|
|
|
const loadServerConfig = Effect.gen(function* () {
|
|
const keybindingsConfig = yield* keybindings.loadConfigState;
|
|
const providers = yield* providerRegistry.getProviders;
|
|
const settings = yield* serverSettings.getSettings;
|
|
|
|
return {
|
|
cwd: config.cwd,
|
|
keybindingsConfigPath: config.keybindingsConfigPath,
|
|
keybindings: keybindingsConfig.keybindings,
|
|
issues: keybindingsConfig.issues,
|
|
providers,
|
|
availableEditors: resolveAvailableEditors(),
|
|
settings,
|
|
};
|
|
});
|
|
|
|
const rpcEffect = <A, E, R>(
|
|
method: string,
|
|
effect: Effect.Effect<A, E, R>,
|
|
traceAttributes?: Readonly<Record<string, unknown>>,
|
|
) => observeRpcEffect(method, effect, traceAttributes);
|
|
|
|
const rpcStream = <A, E, R>(
|
|
method: string,
|
|
stream: Stream.Stream<A, E, R>,
|
|
traceAttributes?: Readonly<Record<string, unknown>>,
|
|
) => observeRpcStream(method, stream, traceAttributes);
|
|
|
|
const rpcStreamEffect = <A, StreamError, StreamContext, EffectError, EffectContext>(
|
|
method: string,
|
|
effect: Effect.Effect<
|
|
Stream.Stream<A, StreamError, StreamContext>,
|
|
EffectError,
|
|
EffectContext
|
|
>,
|
|
traceAttributes?: Readonly<Record<string, unknown>>,
|
|
) => observeRpcStreamEffect(method, effect, traceAttributes);
|
|
|
|
return WsRpcGroup.of({
|
|
[ORCHESTRATION_WS_METHODS.getSnapshot]: (_input) =>
|
|
rpcEffect(
|
|
ORCHESTRATION_WS_METHODS.getSnapshot,
|
|
projectionSnapshotQuery.getSnapshot().pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new OrchestrationGetSnapshotError({
|
|
message: "Failed to load orchestration snapshot",
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "orchestration" },
|
|
),
|
|
[ORCHESTRATION_WS_METHODS.dispatchCommand]: (command) =>
|
|
rpcEffect(
|
|
ORCHESTRATION_WS_METHODS.dispatchCommand,
|
|
Effect.gen(function* () {
|
|
const normalizedCommand = yield* normalizeDispatchCommand(command);
|
|
return yield* startup.enqueueCommand(orchestrationEngine.dispatch(normalizedCommand));
|
|
}).pipe(
|
|
Effect.mapError((cause) =>
|
|
Schema.is(OrchestrationDispatchCommandError)(cause)
|
|
? cause
|
|
: new OrchestrationDispatchCommandError({
|
|
message: "Failed to dispatch orchestration command",
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "orchestration" },
|
|
),
|
|
[ORCHESTRATION_WS_METHODS.getTurnDiff]: (input) =>
|
|
rpcEffect(
|
|
ORCHESTRATION_WS_METHODS.getTurnDiff,
|
|
checkpointDiffQuery.getTurnDiff(input).pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new OrchestrationGetTurnDiffError({
|
|
message: "Failed to load turn diff",
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "orchestration" },
|
|
),
|
|
[ORCHESTRATION_WS_METHODS.getFullThreadDiff]: (input) =>
|
|
rpcEffect(
|
|
ORCHESTRATION_WS_METHODS.getFullThreadDiff,
|
|
checkpointDiffQuery.getFullThreadDiff(input).pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new OrchestrationGetFullThreadDiffError({
|
|
message: "Failed to load full thread diff",
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "orchestration" },
|
|
),
|
|
[ORCHESTRATION_WS_METHODS.replayEvents]: (input) =>
|
|
rpcEffect(
|
|
ORCHESTRATION_WS_METHODS.replayEvents,
|
|
Stream.runCollect(
|
|
orchestrationEngine.readEvents(
|
|
clamp(input.fromSequenceExclusive, { maximum: Number.MAX_SAFE_INTEGER, minimum: 0 }),
|
|
),
|
|
).pipe(
|
|
Effect.map((events) => Array.from(events)),
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new OrchestrationReplayEventsError({
|
|
message: "Failed to replay orchestration events",
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "orchestration" },
|
|
),
|
|
[WS_METHODS.subscribeOrchestrationDomainEvents]: (_input) =>
|
|
rpcStreamEffect(
|
|
WS_METHODS.subscribeOrchestrationDomainEvents,
|
|
Effect.gen(function* () {
|
|
const snapshot = yield* orchestrationEngine.getReadModel();
|
|
const fromSequenceExclusive = snapshot.snapshotSequence;
|
|
const replayEvents: Array<OrchestrationEvent> = yield* Stream.runCollect(
|
|
orchestrationEngine.readEvents(fromSequenceExclusive),
|
|
).pipe(
|
|
Effect.map((events) => Array.from(events)),
|
|
Effect.catch(() => Effect.succeed([] as Array<OrchestrationEvent>)),
|
|
);
|
|
const replayStream = Stream.fromIterable(replayEvents);
|
|
const source = Stream.merge(replayStream, orchestrationEngine.streamDomainEvents);
|
|
type SequenceState = {
|
|
readonly nextSequence: number;
|
|
readonly pendingBySequence: Map<number, OrchestrationEvent>;
|
|
};
|
|
const state = yield* Ref.make<SequenceState>({
|
|
nextSequence: fromSequenceExclusive + 1,
|
|
pendingBySequence: new Map<number, OrchestrationEvent>(),
|
|
});
|
|
|
|
return source.pipe(
|
|
Stream.mapEffect((event) =>
|
|
Ref.modify(
|
|
state,
|
|
({
|
|
nextSequence,
|
|
pendingBySequence,
|
|
}): [Array<OrchestrationEvent>, SequenceState] => {
|
|
if (event.sequence < nextSequence || pendingBySequence.has(event.sequence)) {
|
|
return [[], { nextSequence, pendingBySequence }];
|
|
}
|
|
|
|
const updatedPending = new Map(pendingBySequence);
|
|
updatedPending.set(event.sequence, event);
|
|
|
|
const emit: Array<OrchestrationEvent> = [];
|
|
let expected = nextSequence;
|
|
for (;;) {
|
|
const expectedEvent = updatedPending.get(expected);
|
|
if (!expectedEvent) {
|
|
break;
|
|
}
|
|
emit.push(expectedEvent);
|
|
updatedPending.delete(expected);
|
|
expected += 1;
|
|
}
|
|
|
|
return [emit, { nextSequence: expected, pendingBySequence: updatedPending }];
|
|
},
|
|
),
|
|
),
|
|
Stream.flatMap((events) => Stream.fromIterable(events)),
|
|
);
|
|
}),
|
|
{ "rpc.aggregate": "orchestration" },
|
|
),
|
|
[WS_METHODS.serverGetConfig]: (_input) =>
|
|
rpcEffect(WS_METHODS.serverGetConfig, loadServerConfig, { "rpc.aggregate": "server" }),
|
|
[WS_METHODS.serverRefreshProviders]: (_input) =>
|
|
rpcEffect(
|
|
WS_METHODS.serverRefreshProviders,
|
|
providerRegistry.refresh().pipe(Effect.map((providers) => ({ providers }))),
|
|
{ "rpc.aggregate": "server" },
|
|
),
|
|
[WS_METHODS.serverUpsertKeybinding]: (rule) =>
|
|
rpcEffect(
|
|
WS_METHODS.serverUpsertKeybinding,
|
|
Effect.gen(function* () {
|
|
const keybindingsConfig = yield* keybindings.upsertKeybindingRule(rule);
|
|
return { keybindings: keybindingsConfig, issues: [] };
|
|
}),
|
|
{ "rpc.aggregate": "server" },
|
|
),
|
|
[WS_METHODS.serverGetSettings]: (_input) =>
|
|
rpcEffect(WS_METHODS.serverGetSettings, serverSettings.getSettings, {
|
|
"rpc.aggregate": "server",
|
|
}),
|
|
[WS_METHODS.serverUpdateSettings]: ({ patch }) =>
|
|
rpcEffect(WS_METHODS.serverUpdateSettings, serverSettings.updateSettings(patch), {
|
|
"rpc.aggregate": "server",
|
|
}),
|
|
[WS_METHODS.projectsSearchEntries]: (input) =>
|
|
rpcEffect(
|
|
WS_METHODS.projectsSearchEntries,
|
|
workspaceEntries.search(input).pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new ProjectSearchEntriesError({
|
|
message: `Failed to search workspace entries: ${cause.detail}`,
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "workspace" },
|
|
),
|
|
[WS_METHODS.projectsWriteFile]: (input) =>
|
|
rpcEffect(
|
|
WS_METHODS.projectsWriteFile,
|
|
workspaceFileSystem.writeFile(input).pipe(
|
|
Effect.mapError((cause) => {
|
|
const message = Schema.is(WorkspacePathOutsideRootError)(cause)
|
|
? "Workspace file path must stay within the project root."
|
|
: "Failed to write workspace file";
|
|
return new ProjectWriteFileError({
|
|
message,
|
|
cause,
|
|
});
|
|
}),
|
|
),
|
|
{ "rpc.aggregate": "workspace" },
|
|
),
|
|
[WS_METHODS.shellOpenInEditor]: (input) =>
|
|
rpcEffect(WS_METHODS.shellOpenInEditor, open.openInEditor(input), {
|
|
"rpc.aggregate": "workspace",
|
|
}),
|
|
[WS_METHODS.gitStatus]: (input) =>
|
|
rpcEffect(WS_METHODS.gitStatus, gitManager.status(input), { "rpc.aggregate": "git" }),
|
|
[WS_METHODS.gitPull]: (input) =>
|
|
rpcEffect(WS_METHODS.gitPull, git.pullCurrentBranch(input.cwd), { "rpc.aggregate": "git" }),
|
|
[WS_METHODS.gitRunStackedAction]: (input) =>
|
|
rpcStream(
|
|
WS_METHODS.gitRunStackedAction,
|
|
Stream.callback<GitActionProgressEvent, GitManagerServiceError>((queue) =>
|
|
gitManager
|
|
.runStackedAction(input, {
|
|
actionId: input.actionId,
|
|
progressReporter: {
|
|
publish: (event) => Queue.offer(queue, event).pipe(Effect.asVoid),
|
|
},
|
|
})
|
|
.pipe(
|
|
Effect.matchCauseEffect({
|
|
onFailure: (cause) => Queue.failCause(queue, cause),
|
|
onSuccess: () => Queue.end(queue).pipe(Effect.asVoid),
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "git" },
|
|
),
|
|
[WS_METHODS.gitResolvePullRequest]: (input) =>
|
|
rpcEffect(WS_METHODS.gitResolvePullRequest, gitManager.resolvePullRequest(input), {
|
|
"rpc.aggregate": "git",
|
|
}),
|
|
[WS_METHODS.gitPreparePullRequestThread]: (input) =>
|
|
rpcEffect(
|
|
WS_METHODS.gitPreparePullRequestThread,
|
|
gitManager.preparePullRequestThread(input),
|
|
{ "rpc.aggregate": "git" },
|
|
),
|
|
[WS_METHODS.gitListBranches]: (input) =>
|
|
rpcEffect(WS_METHODS.gitListBranches, git.listBranches(input), { "rpc.aggregate": "git" }),
|
|
[WS_METHODS.gitCreateWorktree]: (input) =>
|
|
rpcEffect(WS_METHODS.gitCreateWorktree, git.createWorktree(input), {
|
|
"rpc.aggregate": "git",
|
|
}),
|
|
[WS_METHODS.gitRemoveWorktree]: (input) =>
|
|
rpcEffect(WS_METHODS.gitRemoveWorktree, git.removeWorktree(input), {
|
|
"rpc.aggregate": "git",
|
|
}),
|
|
[WS_METHODS.gitCreateBranch]: (input) =>
|
|
rpcEffect(WS_METHODS.gitCreateBranch, git.createBranch(input), {
|
|
"rpc.aggregate": "git",
|
|
}),
|
|
[WS_METHODS.gitCheckout]: (input) =>
|
|
rpcEffect(WS_METHODS.gitCheckout, Effect.scoped(git.checkoutBranch(input)), {
|
|
"rpc.aggregate": "git",
|
|
}),
|
|
[WS_METHODS.gitInit]: (input) =>
|
|
rpcEffect(WS_METHODS.gitInit, git.initRepo(input), { "rpc.aggregate": "git" }),
|
|
[WS_METHODS.terminalOpen]: (input) =>
|
|
rpcEffect(WS_METHODS.terminalOpen, terminalManager.open(input), {
|
|
"rpc.aggregate": "terminal",
|
|
}),
|
|
[WS_METHODS.terminalWrite]: (input) =>
|
|
rpcEffect(WS_METHODS.terminalWrite, terminalManager.write(input), {
|
|
"rpc.aggregate": "terminal",
|
|
}),
|
|
[WS_METHODS.terminalResize]: (input) =>
|
|
rpcEffect(WS_METHODS.terminalResize, terminalManager.resize(input), {
|
|
"rpc.aggregate": "terminal",
|
|
}),
|
|
[WS_METHODS.terminalClear]: (input) =>
|
|
rpcEffect(WS_METHODS.terminalClear, terminalManager.clear(input), {
|
|
"rpc.aggregate": "terminal",
|
|
}),
|
|
[WS_METHODS.terminalRestart]: (input) =>
|
|
rpcEffect(WS_METHODS.terminalRestart, terminalManager.restart(input), {
|
|
"rpc.aggregate": "terminal",
|
|
}),
|
|
[WS_METHODS.terminalClose]: (input) =>
|
|
rpcEffect(WS_METHODS.terminalClose, terminalManager.close(input), {
|
|
"rpc.aggregate": "terminal",
|
|
}),
|
|
[WS_METHODS.subscribeTerminalEvents]: (_input) =>
|
|
rpcStream(
|
|
WS_METHODS.subscribeTerminalEvents,
|
|
Stream.callback<TerminalEvent>((queue) =>
|
|
Effect.acquireRelease(
|
|
terminalManager.subscribe((event) => Queue.offer(queue, event)),
|
|
(unsubscribe) => Effect.sync(unsubscribe),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "terminal" },
|
|
),
|
|
[WS_METHODS.subscribeServerConfig]: (_input) =>
|
|
rpcStreamEffect(
|
|
WS_METHODS.subscribeServerConfig,
|
|
Effect.gen(function* () {
|
|
const keybindingsUpdates = keybindings.streamChanges.pipe(
|
|
Stream.map((event) => ({
|
|
version: 1 as const,
|
|
type: "keybindingsUpdated" as const,
|
|
payload: {
|
|
issues: event.issues,
|
|
},
|
|
})),
|
|
);
|
|
const providerStatuses = providerRegistry.streamChanges.pipe(
|
|
Stream.map((providers) => ({
|
|
version: 1 as const,
|
|
type: "providerStatuses" as const,
|
|
payload: { providers },
|
|
})),
|
|
);
|
|
const settingsUpdates = serverSettings.streamChanges.pipe(
|
|
Stream.map((settings) => ({
|
|
version: 1 as const,
|
|
type: "settingsUpdated" as const,
|
|
payload: { settings },
|
|
})),
|
|
);
|
|
|
|
return Stream.concat(
|
|
Stream.make({
|
|
version: 1 as const,
|
|
type: "snapshot" as const,
|
|
config: yield* loadServerConfig,
|
|
}),
|
|
Stream.merge(keybindingsUpdates, Stream.merge(providerStatuses, settingsUpdates)),
|
|
);
|
|
}),
|
|
{ "rpc.aggregate": "server" },
|
|
),
|
|
[WS_METHODS.subscribeServerLifecycle]: (_input) =>
|
|
rpcStreamEffect(
|
|
WS_METHODS.subscribeServerLifecycle,
|
|
Effect.gen(function* () {
|
|
const snapshot = yield* lifecycleEvents.snapshot;
|
|
const snapshotEvents = Array.from(snapshot.events).toSorted(
|
|
(left, right) => left.sequence - right.sequence,
|
|
);
|
|
const liveEvents = lifecycleEvents.stream.pipe(
|
|
Stream.filter((event) => event.sequence > snapshot.sequence),
|
|
);
|
|
return Stream.concat(Stream.fromIterable(snapshotEvents), liveEvents);
|
|
}),
|
|
{ "rpc.aggregate": "server" },
|
|
),
|
|
});
|
|
}),
|
|
);
|
|
|
|
export const websocketRpcRouteLayer = Layer.unwrap(
|
|
Effect.gen(function* () {
|
|
const rpcWebSocketHttpEffect = yield* RpcServer.toHttpEffectWebsocket(WsRpcGroup, {
|
|
spanPrefix: "ws.rpc",
|
|
spanAttributes: {
|
|
"rpc.transport": "websocket",
|
|
"rpc.system": "effect-rpc",
|
|
},
|
|
}).pipe(Effect.provide(Layer.mergeAll(WsRpcLayer, RpcSerialization.layerJson)));
|
|
return HttpRouter.add(
|
|
"GET",
|
|
"/ws",
|
|
Effect.gen(function* () {
|
|
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
const config = yield* ServerConfig;
|
|
if (config.authToken) {
|
|
const url = HttpServerRequest.toURL(request);
|
|
if (Option.isNone(url)) {
|
|
return HttpServerResponse.text("Invalid WebSocket URL", { status: 400 });
|
|
}
|
|
const token = url.value.searchParams.get("token");
|
|
if (token !== config.authToken) {
|
|
return HttpServerResponse.text("Unauthorized WebSocket connection", { status: 401 });
|
|
}
|
|
}
|
|
return yield* rpcWebSocketHttpEffect;
|
|
}),
|
|
);
|
|
}),
|
|
);
|