import { OrchestrationDispatchCommandError } from "@t3tools/contracts";
import * as Context from "effect/Context";
import * as SecureRandom from "effect/crypto/SecureRandom";
import * as Orchestrator from "./orchestration-v2/Orchestrator.ts";
import * as PeerLinkRequests from "./peer/PeerLinkRequests.ts";
import * as PeerLinks from "./peer/PeerLinks.ts";
import * as DateTime from "effect/DateTime";
import * as Duration from "effect/Duration";
import * as Base64 from "effect/encoding/Base64";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Queue from "effect/Queue";
import * as Ref from "effect/Ref";
import * as Schema from "effect/Schema";
import * as Result from "effect/Result";
import * as Stream from "effect/Stream";
import { rpcInitialItems } from "./rpcInitialItems.ts";
import { subscribeChatGptHandoff } from "./provider/CodexChatGptHandoff.ts";
import {
AcpRegistryOperationError,
CommandId,
authScopeResponse,
AuthAccessStreamError,
type AuthAccessStreamEvent,
AuthOrchestrationOperateScope,
type AuthEnvironmentScope,
type ScheduledTaskListResult,
AuthSessionId,
ClientConnectionMethod,
ClientDeviceType,
ClientOs,
ClientSurface,
ClientWebDeployment,
type DiscoveredLocalServerList,
type EditorId,
type FileManagerRevealKind,
type OrchestrationV2Command,
type GitActionProgressEvent,
type GitManagerServiceError,
type MessageId,
type AcpRegistryImportSessionInput,
type AcpRegistryDeleteSessionInput,
type AcpRegistryDisableProviderInput,
type AcpRegistryListProvidersInput,
type AcpRegistryListSessionsInput,
type AcpRegistrySetProviderInput,
OrchestrationGetFullThreadDiffError,
OrchestrationSearchThreadsError,
OrchestrationV2SearchThreadError,
OrchestrationGetTurnDiffError,
ORCHESTRATION_V2_WS_METHODS,
ORCHESTRATION_PROTOCOL_QUERY_PARAM,
CLIENT_CAPABILITIES_QUERY_PARAM,
SETTINGS_SECRET_REFERENCES_CAPABILITY,
ORCHESTRATION_PROTOCOL_VERSION,
OrchestrationV2DispatchCommandError,
OrchestrationV2GetShellSnapshotError,
OrchestrationV2GetThreadProjectionError,
OrchestrationV2ThreadLaunchError,
type OrchestrationProjectShell,
type OrchestrationV2ShellSnapshot,
type OrchestrationV2ThreadStreamItem,
type ApplicationStoredEvent,
type ProjectEntriesFailure,
type ProjectFileFailure,
type ProjectFileOperation,
type ProjectMutation,
ProjectListEntriesError,
ProjectReadFileError,
ProjectSearchContentsError,
ProjectSearchEntriesError,
ProjectWriteFileError,
ProjectMutationError,
ProviderUploadFeedbackError,
ProviderSetupError,
RelayClientInstallFailedError,
type RelayClientInstallProgressEvent,
type ServerConfig as ClientServerConfig,
type ServerConfigStreamEvent,
type ServerLifecycleStreamEvent,
type ServerProvider,
type FilesystemBrowseFailure,
FilesystemBrowseError,
AssetWorkspaceContextNotFoundError,
AssetWorkspaceContextResolutionError,
SOURCE_CONTROL_MEDIA_ASSET,
ChatAttachmentId,
PersistChatAttachmentsError,
RpcClientId,
EnvironmentAuthorizationError,
type ProjectId,
type ProviderDriverKind,
type ProviderInstanceId,
ThreadId,
type TerminalMetadataStreamEvent,
type PullRequestRef,
WS_METHODS,
WsRpcGroup,
} from "@t3tools/contracts";
import {
HttpRouter,
HttpServerRequest,
HttpServerRespondable,
HttpServerResponse,
} from "effect/http";
import { RpcGroup, RpcSerialization, RpcServer } from "effect/rpc";
import * as CheckpointDiffQuery from "./checkpointing/CheckpointDiffQuery.ts";
import * as NativeChildWorkQuery from "./orchestration-v2/NativeChildWorkQuery.ts";
import * as ServerConfig from "./config.ts";
import * as EnvironmentTheme from "./environmentTheme.ts";
import * as Keybindings from "./keybindings.ts";
import * as ExternalLauncher from "./process/externalLauncher.ts";
import * as ThreadManagementService from "./orchestration-v2/ThreadManagementService.ts";
import * as CommandOutputHub from "./orchestration-v2/CommandOutputHub.ts";
import * as McpAppRequests from "./mcpApps/McpAppRequests.ts";
import * as ProviderSessionManager from "./orchestration-v2/ProviderSessionManager.ts";
import * as ThreadLaunchService from "./orchestration-v2/ThreadLaunchService.ts";
import * as ThreadMessageIntake from "./orchestration-v2/ThreadMessageIntake.ts";
import * as DocumentText from "./provider/DocumentText.ts";
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
import * as ScheduledTasks from "./scheduledTasks/ScheduledTaskService.ts";
import * as SecretRequests from "./secrets/SecretRequests.ts";
import * as GuestShareService from "./guest/GuestShareService.ts";
import {
bufferShellLiveStream,
coalesceShellApplicationEvents,
composeShellStreamWithEnrichment,
dedupeShellEnrichment,
loadOlderSettledThreadsPage,
loadShellSnapshotParts,
shellStreamItemFromEnrichmentRefresh,
shellStreamItemFromThreadShell,
shellStreamItemsFromInitialSnapshot,
loadSubscriberShellSnapshot,
shellStreamItemsFromResumeSnapshot,
shouldPublishShellApplicationEvent,
skipUnchangedThreadShells,
toShellApplicationEvent,
type ShellApplicationEvent,
} from "./orchestration-v2/ShellStream.ts";
import { ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION } from "./orchestration-v2/ProjectionStore.ts";
import {
coalesceLiveUpdatedEvents,
coalesceThreadLiveStream,
} from "./orchestration-v2/ThreadLiveEventCoalescer.ts";
import {
buildBoundedThreadStreamSnapshot,
decideThreadResume,
isThreadReplayRawPayloadSafe,
threadReplayEncodedBytes,
THREAD_RESUME_MAX_REPLAY_EVENTS,
} from "./orchestration-v2/ThreadStream.ts";
import {
buildBoundedThreadProjection,
THREAD_HISTORY_PAGE_POLICY,
THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
} from "./orchestration-v2/threadHistoryPaging.ts";
import {
makeLiveBodyDeduper,
projectDomainEventForWire,
projectThreadProjectionForWire,
threadStreamEventItem,
} from "./orchestration-v2/WireProjection.ts";
import { dedupeSnapshotBodies } from "@t3tools/shared/orchestrationV2DedupedBodies";
import * as ProjectStore from "./orchestration-v2/ProjectStore.ts";
import * as ThreadSearch from "./orchestration-v2/ThreadSearch.ts";
import * as OrchestrationEventStore from "./persistence/OrchestrationEventStore.ts";
import { userFacingDispatchErrorMessage } from "./orchestration-v2/UserFacingErrors.ts";
import * as ProviderRegistry from "./provider/ProviderRegistry.ts";
import * as ProviderInstanceRegistry from "./provider/ProviderInstanceRegistry.ts";
import * as AcpRegistrySupport from "@t3tools/provider-acp-registry/server/AcpRegistrySupport";
import * as AcpRegistryRuntimeCoordinator from "@t3tools/provider-acp-registry/server/AcpRegistryRuntimeCoordinator";
import * as ProviderMaintenanceRunner from "./provider/providerMaintenanceRunner.ts";
import * as ProviderAuthService from "./provider/ProviderAuthService.ts";
import { makeProviderInstallation } from "./provider/providerInstallation.ts";
import * as ServerSelfUpdate from "./cloud/selfUpdate.ts";
import * as NpmRelease from "./cloud/npmRelease.ts";
import * as ServerLifecycleEvents from "./serverLifecycleEvents.ts";
import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts";
import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
import * as ServerSettings from "./serverSettings.ts";
import { invalidateSettingsSecrets } from "./sourceControl/sourceControlHostSecrets.ts";
import * as DatabaseStorage from "./persistence/DatabaseStorage.ts";
import * as StorageCleanup from "./storageCleanup.ts";
import * as TerminalManager from "./terminal/Manager.ts";
import { terminalOutputStream, withOutputWindow } from "./terminal/OutputProtocol.ts";
import * as ServerBrowser from "./preview/ServerBrowser.ts";
import * as DeviceService from "./device/DeviceService.ts";
import { remoteSshDeviceHosts } from "./device/localSshDeviceHost.ts";
import * as PreviewManager from "./preview/Manager.ts";
import { issueAssetUrl } from "./assets/AssetAccess.ts";
import {
attachmentRelativePath,
chatAttachmentStableKey,
createDeterministicAttachmentId,
} from "./attachmentStore.ts";
import { parseBase64DataUrl } from "./imageMime.ts";
import { deletePendingAttachment, issueAttachmentUploadUrl } from "./assets/AttachmentUpload.ts";
import * as PortScanner from "./preview/PortScanner.ts";
import * as WorkspaceEntries from "./workspace/WorkspaceEntries.ts";
import * as KnownWorkspaces from "./workspace/KnownWorkspaces.ts";
import * as WorkspaceFileSystem from "./workspace/WorkspaceFileSystem.ts";
import { readWorkflowScript } from "./orchestration-v2/workflowScriptQuery.ts";
import * as WorkspacePaths from "./workspace/WorkspacePaths.ts";
import * as VcsStatusBroadcaster from "./vcs/VcsStatusBroadcaster.ts";
import * as VcsProvisioningService from "./vcs/VcsProvisioningService.ts";
import * as GitWorkflowService from "./git/GitWorkflowService.ts";
import { refreshPushedPullRequests } from "./git/refreshPushedPullRequests.ts";
import { linkCreatedPullRequest } from "./git/linkCreatedPullRequest.ts";
import * as ReviewService from "./review/ReviewService.ts";
import * as ProjectEnrichmentService from "./project/ProjectEnrichmentService.ts";
import * as ProjectService from "./project/ProjectService.ts";
import { threadWorkspaceRoot } from "./project/threadWorkspaceRoot.ts";
import * as ManagedProjectFolders from "./project/ManagedProjectFolders.ts";
import * as ScratchThreadPromotion from "./project/ScratchThreadPromotion.ts";
import { mutateProjectWithClone } from "./project/ProjectMutation.ts";
import * as ProjectCloneTracker from "./project/ProjectCloneTracker.ts";
import * as RepositoryIdentityResolver from "./project/RepositoryIdentityResolver.ts";
import * as WorktreeSetupTracker from "./project/WorktreeSetupTracker.ts";
import * as ServerEnvironment from "./environment/ServerEnvironment.ts";
import * as DirectEndpoints from "./environment/DirectEndpoints.ts";
import * as RemoteOpenTargets from "./environment/RemoteOpenTargets.ts";
import * as DefectReporter from "./observability/DefectReporter.ts";
import * as EventLoopMonitor from "./observability/EventLoopMonitor.ts";
import * as BackgroundPolicy from "./background/BackgroundPolicy.ts";
import * as EnvironmentAuth from "./auth/EnvironmentAuth.ts";
import { requiredScopeForDeviceList, rpcAuthorizationError } from "./auth/RpcAuthorization.ts";
import * as RpcAuthorization from "./auth/RpcAuthorization.ts";
import { RpcInstrumentation, rpcInstrumentationLayer } from "./observability/RpcInstrumentation.ts";
import * as ProcessDiagnostics from "./diagnostics/ProcessDiagnostics.ts";
import * as ProcessResourceMonitor from "./diagnostics/ProcessResourceMonitor.ts";
import * as ResourceTelemetry from "./resourceTelemetry/ResourceTelemetry.ts";
import * as HostResources from "./resourceTelemetry/HostResources.ts";
import * as AnalyticsService from "./telemetry/AnalyticsService.ts";
import { commandFeatureUsage, featureUsage } from "./telemetry/FeatureUsage.ts";
import type { TelemetryOwner } from "./telemetry/Identify.ts";
import * as UsageService from "./usage/UsageService.ts";
import * as TraceDiagnostics from "./diagnostics/TraceDiagnostics.ts";
import * as TriageService from "./triage/TriageService.ts";
import * as PullRequestService from "./pullRequest/PullRequestService.ts";
import * as PullRequestReviewGuideService from "./pullRequest/PullRequestReviewGuideService.ts";
import { listLinkedPullRequestThreads } from "./pullRequest/linkedThreads.ts";
import { pullRequestSyncKey } from "./pullRequest/pullRequestSyncKey.ts";
import * as SqlClient from "effect/sql/SqlClient";
import * as PullRequestSyncReactor from "./orchestration-v2/PullRequestSyncReactor.ts";
import * as SourceControlDiscovery from "./sourceControl/SourceControlDiscovery.ts";
import * as OtlpEndpointCheck from "./observability/OtlpEndpointCheck.ts";
import * as SourceControlRepositoryService from "./sourceControl/SourceControlRepositoryService.ts";
import * as SourceControlSignIn from "./sourceControl/SourceControlSignIn.ts";
import * as SourceControlBuiltInDrivers from "./sourceControl/builtInDrivers.ts";
import * as SourceControlProviderRegistry from "./sourceControl/SourceControlProviderRegistry.ts";
import * as GitVcsDriver from "./vcs/GitVcsDriver.ts";
import * as VcsDriverRegistry from "./vcs/VcsDriverRegistry.ts";
import * as VcsProjectConfig from "./vcs/VcsProjectConfig.ts";
import * as PairingGrantStore from "./auth/PairingGrantStore.ts";
import * as SessionStore from "./auth/SessionStore.ts";
import { failEnvironmentAuthInvalid, failEnvironmentInternal } from "./auth/http.ts";
import { isNetworkPath } from "@t3tools/shared/path";
import * as RelayClient from "@t3tools/shared/relayClient";
import {
SOCKET_FRAME_PARTS_QUERY_PARAM,
splitLargeSocketFrames,
} from "@t3tools/shared/socketFrameParts";
import { isTelemetryIdHash } from "@t3tools/shared/telemetryId";
import {
sameUsageLimitCommandCoverage,
withUsageLimitsCommands,
} from "@t3tools/shared/usageLimits";
import * as AgentSessionScanner from "./project/AgentSessionScanner.ts";
import * as AgentSessionImporter from "./project/AgentSessionImporter.ts";
import * as UsageLimitSources from "./usage/UsageLimitSources.ts";
import { requestUrl } from "./requestUrl.ts";
const CONFIG_DISCOVERY_TIMEOUT = Duration.seconds(5);
const isProviderUploadFeedbackError = Schema.is(ProviderUploadFeedbackError);
const resolveDiscoveryForConfig = <A, E, R>(
discovery: Effect.Effect<A, E, R>,
onTimeout: () => A,
) =>
discovery.pipe(
Effect.timeoutOption(CONFIG_DISCOVERY_TIMEOUT),
Effect.map(Option.getOrElse(onTimeout)),
);
const resolveAvailableEditorsForConfig = <A, E, R>(
discovery: Effect.Effect<ReadonlyArray<A>, E, R>,
) => resolveDiscoveryForConfig(discovery, () => []);
const resolveFileManagerRevealKindForConfig = <E, R>(
discovery: Effect.Effect<FileManagerRevealKind | undefined, E, R>,
) => resolveDiscoveryForConfig(discovery, () => undefined);
type EditorDiscovery = Pick<
ExternalLauncher.ExternalLauncher["Service"],
"resolveAvailableEditors" | "resolveFileManagerRevealKind"
>;
const resolveEditorConfig = Effect.fn("ws.resolveEditorConfig")(function* <E, R>(
availableEditors: ReadonlyArray<EditorId>,
revealKind: Effect.Effect<FileManagerRevealKind | undefined, E, R>,
) {
const fileManagerRevealKind = availableEditors.includes("file-manager")
? yield* revealKind
: undefined;
return {
availableEditors,
...(fileManagerRevealKind === undefined
? {}
: {
shellRevealInFileManager: true,
shellRevealInFileManagerKind: fileManagerRevealKind,
}),
};
});
export const withLateConfigDiscovery = <E, R>(
config: ClientServerConfig,
liveUpdates: Stream.Stream<ServerConfigStreamEvent, E, R>,
launcher: EditorDiscovery,
remoteOpenTargets: RemoteOpenTargets.RemoteOpenTargets["Service"],
directEndpoints: Pick<DirectEndpoints.DirectEndpoints["Service"], "resolve">,
) => {
const lateEditorConfig = Stream.fromEffect(launcher.resolveAvailableEditors()).pipe(
Stream.filter(
(editors) =>
editors.join() !== config.availableEditors.join() ||
(editors.includes("file-manager") && config.shellRevealInFileManagerKind === undefined),
),
// Unbounded, unlike the snapshot: this exists to deliver a probe that
// outlasts the snapshot's timeout.
Stream.mapEffect((editors) =>
resolveEditorConfig(editors, launcher.resolveFileManagerRevealKind()),
),
Stream.filter(
(editorConfig) =>
editorConfig.availableEditors.join() !== config.availableEditors.join() ||
editorConfig.shellRevealInFileManagerKind !== config.shellRevealInFileManagerKind,
),
Stream.map((editorConfig) => ({ type: "editorsResolved" as const, editorConfig })),
);
const lateRemoteOpenTargets = Stream.fromEffect(remoteOpenTargets.resolveTargets()).pipe(
Stream.filter(
(targets) => JSON.stringify(targets) !== JSON.stringify(config.remoteOpenTargets ?? []),
),
Stream.map((targets) => ({ type: "remoteOpenTargetsResolved" as const, targets })),
);
const lateDirectEndpoints = Stream.fromEffect(directEndpoints.resolve()).pipe(
Stream.catchCause(() => Stream.empty),
Stream.map((endpoints) => ({ type: "directEndpointsResolved" as const, endpoints })),
);
return Stream.merge(
liveUpdates,
Stream.merge(lateEditorConfig, Stream.merge(lateRemoteOpenTargets, lateDirectEndpoints)),
).pipe(
Stream.mapAccum(
(): ClientServerConfig => config,
(current, event): readonly [ClientServerConfig, ReadonlyArray<ServerConfigStreamEvent>] => {
switch (event.type) {
case "editorsResolved": {
const {
availableEditors: _editors,
shellRevealInFileManager: _reveal,
shellRevealInFileManagerKind: _revealKind,
...rest
} = current;
const next = { ...rest, ...event.editorConfig };
return [next, [{ version: 1, type: "snapshot", config: next }]];
}
case "remoteOpenTargetsResolved": {
const next = { ...current, remoteOpenTargets: event.targets };
return [next, [{ version: 1, type: "snapshot", config: next }]];
}
case "directEndpointsResolved": {
if (JSON.stringify(current.directEndpoints ?? []) === JSON.stringify(event.endpoints)) {
return [current, []];
}
const next = { ...current, directEndpoints: event.endpoints };
return [next, [{ version: 1, type: "snapshot", config: next }]];
}
case "keybindingsUpdated":
return [{ ...current, ...event.payload }, [event]];
case "providerStatuses":
return [{ ...current, providers: event.payload.providers }, [event]];
case "settingsUpdated":
return [{ ...current, settings: event.payload.settings }, [event]];
// Themes and usage-limit sources survive snapshots because snapshots never include them.
default:
return [current, [event]];
}
},
),
);
};
// Discoveries run side by side so the slowest one, not their sum, bounds the
// server config snapshot. Each degrades on timeout rather than failing it.
export const resolveOpenDiscoveryForConfig = <T, D, E1, R1, E2, R2, E3, R3, E4, R4>(discovery: {
readonly editors: Effect.Effect<ReadonlyArray<EditorId>, E1, R1>;
readonly fileManagerRevealKind: Effect.Effect<FileManagerRevealKind | undefined, E2, R2>;
readonly remoteOpenTargets: Effect.Effect<ReadonlyArray<T>, E3, R3>;
readonly directEndpoints: Effect.Effect<ReadonlyArray<D>, E4, R4>;
}) =>
Effect.all(
{
editorConfig: resolveAvailableEditorsForConfig(discovery.editors).pipe(
Effect.flatMap((availableEditors) =>
resolveEditorConfig(
availableEditors,
resolveFileManagerRevealKindForConfig(discovery.fileManagerRevealKind),
),
),
),
// Same discovery-with-timeout treatment as editors: a slow probe must
// not stall server.getConfig, so it degrades to an empty list.
remoteOpenTargets: resolveAvailableEditorsForConfig(discovery.remoteOpenTargets),
directEndpoints: resolveAvailableEditorsForConfig(discovery.directEndpoints),
},
{ concurrency: "unbounded" },
).pipe(Effect.map(({ editorConfig, ...endpoints }) => ({ ...editorConfig, ...endpoints })));
// A failed scan leaves no cwd snapshot, so a client without the machine catalog gets it
// as a pending snapshot, which it shows while it retries the scan.
function workspaceSnapshotOrCatalog(
provider: ServerProvider,
cwd: string,
catalogOnDemand: boolean,
) {
const snapshots = (provider.workspaceSnapshots ?? []).filter((snapshot) => snapshot.cwd === cwd);
if (snapshots.length > 0 || !catalogOnDemand) return snapshots;
return [
{
cwd,
checkedAt: provider.checkedAt,
slashCommands: provider.slashCommands,
slashCommandsPending: true,
skills: provider.skills,
},
];
}
// On-demand catalog clients read full entries from the cwd snapshot they fetch. Names still
// change the config when the machine catalog does, so the client refetches, and skill names
// and labels keep skill links working where no snapshot is fetched.
function withoutWorkspaceSnapshots(
providers: ReadonlyArray<ServerProvider>,
catalogsPerCwd: ReadonlySet<string> = new Set(),
): ServerProvider[] {
return providers.map(({ workspaceSnapshots: _workspaceSnapshots, ...provider }) =>
catalogsPerCwd.has(provider.instanceId)
? {
...provider,
slashCommands: provider.slashCommands.map(({ name }) => ({ name })),
skills: provider.skills.map(
({ description: _d, shortDescription: _s, ...skill }) => skill,
),
}
: provider,
);
}
function unexpectedCompatibilityError(error: never): never {
throw new Error(`Unhandled compatibility error: ${String(error)}`);
}
const persistChatAttachments = Effect.fn("ws.assets.persistChatAttachments")(function* (input: {
readonly threadId: ThreadId;
readonly messageId: MessageId;
readonly attachments: ReadonlyArray<{
readonly type: "image";
readonly name: string;
readonly mimeType: string;
readonly sizeBytes: number;
readonly dataUrl: string;
}>;
}) {
const config = yield* ServerConfig.ServerConfig;
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
return yield* Effect.forEach(
input.attachments.map((attachment, index) => ({ attachment, index })),
Effect.fn("ws.assets.persistChatAttachment")(function* ({ attachment, index }) {
const parsed = parseBase64DataUrl(attachment.dataUrl);
if (parsed === null || parsed.mimeType !== attachment.mimeType.toLowerCase()) {
return yield* new PersistChatAttachmentsError({
message: `Attachment ${attachment.name} has an invalid image payload.`,
});
}
const bytes = yield* Effect.fromResult(Base64.decode(parsed.base64)).pipe(
Effect.mapError(
(cause) =>
new PersistChatAttachmentsError({
message: `Attachment ${attachment.name} is not valid base64.`,
cause,
}),
),
);
if (bytes.byteLength !== attachment.sizeBytes) {
return yield* new PersistChatAttachmentsError({
message: `Attachment ${attachment.name} size does not match its payload.`,
});
}
const rawId = createDeterministicAttachmentId(
input.threadId,
chatAttachmentStableKey(input.messageId, index, bytes),
);
if (rawId === null) {
return yield* new PersistChatAttachmentsError({
message: "Could not allocate an attachment identifier.",
});
}
const persisted = {
type: "image" as const,
id: ChatAttachmentId.make(rawId),
name: attachment.name,
mimeType: attachment.mimeType,
sizeBytes: attachment.sizeBytes,
};
yield* fileSystem
.writeFile(path.join(config.attachmentsDir, attachmentRelativePath(persisted)!), bytes)
.pipe(
Effect.mapError(
(cause) =>
new PersistChatAttachmentsError({
message: `Could not persist attachment ${attachment.name}.`,
cause,
}),
),
);
return persisted;
}),
{ concurrency: 2 },
);
});
function projectEntriesFailureContext(error: WorkspaceEntries.WorkspaceEntriesError): {
readonly failure: ProjectEntriesFailure;
readonly normalizedCwd?: string;
readonly timeout?: string;
readonly detail?: string;
} {
switch (error._tag) {
case "WorkspaceRootNotExistsError":
return {
failure: "workspace_root_not_found",
normalizedCwd: error.normalizedWorkspaceRoot,
};
case "WorkspaceRootCreateFailedError":
return {
failure: "workspace_root_create_failed",
normalizedCwd: error.normalizedWorkspaceRoot,
};
case "WorkspaceRootStatFailedError":
return {
failure: "workspace_root_stat_failed",
normalizedCwd: error.normalizedWorkspaceRoot,
detail: error.phase,
};
case "WorkspaceRootNotDirectoryError":
return {
failure: "workspace_root_not_directory",
normalizedCwd: error.normalizedWorkspaceRoot,
};
case "WorkspaceEntriesReadDirectoryError":
return {
failure: "directory_list_failed",
...(error.cwd !== undefined ? { normalizedCwd: error.cwd } : {}),
detail: error.message,
};
case "WorkspaceSearchIndexCreateFailed":
return {
failure: "search_index_create_failed",
normalizedCwd: error.cwd,
detail: error.reason,
};
case "WorkspaceSearchIndexScanTimedOut":
return {
failure: "search_index_scan_timed_out",
normalizedCwd: error.cwd,
timeout: error.timeout,
};
case "WorkspaceSearchIndexSearchFailed":
return {
failure: "search_index_search_failed",
normalizedCwd: error.cwd,
detail: error.reason,
};
default:
return unexpectedCompatibilityError(error);
}
}
function filesystemBrowseFailureContext(error: WorkspaceEntries.WorkspaceEntriesBrowseError): {
readonly failure: FilesystemBrowseFailure;
readonly parentPath?: string;
readonly platform?: string;
readonly pathMissing?: boolean;
} {
switch (error._tag) {
case "WorkspaceEntriesWindowsPathUnsupportedError":
return { failure: "windows_path_unsupported", platform: error.platform };
case "WorkspaceEntriesCurrentProjectRequiredError":
return { failure: "current_project_required" };
case "WorkspaceEntriesReadDirectoryError":
return {
failure: "read_directory_failed",
parentPath: error.parentPath,
...(error.pathMissing ? { pathMissing: true } : {}),
};
default:
return unexpectedCompatibilityError(error);
}
}
function projectFileFailureContext(
error:
| WorkspaceFileSystem.WorkspaceFileSystemError
| WorkspacePaths.WorkspacePathOutsideRootError
| KnownWorkspaces.UnknownWorkspaceError,
): {
readonly failure: ProjectFileFailure;
readonly resolvedPath?: string;
readonly resolvedWorkspaceRoot?: string;
readonly operation?: ProjectFileOperation;
readonly operationPath?: string;
} {
switch (error._tag) {
case "WorkspacePathOutsideRootError":
case "UnknownWorkspaceError":
return { failure: "workspace_path_outside_root" };
case "WorkspaceFileSystemOperationError":
return {
failure: "operation_failed",
resolvedPath: error.resolvedPath,
operation: error.operation,
operationPath: error.operationPath,
};
case "WorkspaceFilePathEscapeError":
return {
failure: "resolved_path_outside_root",
resolvedPath: error.resolvedPath,
resolvedWorkspaceRoot: error.resolvedWorkspaceRoot,
};
case "WorkspacePathNotFileError":
return { failure: "path_not_file", resolvedPath: error.resolvedPath };
case "WorkspaceBinaryFileError":
return { failure: "binary_file", resolvedPath: error.resolvedPath };
default:
return unexpectedCompatibilityError(error);
}
}
const PROVIDER_STATUS_DEBOUNCE_MS = 200;
// Middleware added later wraps middleware added earlier, so instrumentation wraps authorization.
const ServerWsRpcGroup = WsRpcGroup.middleware(RpcInstrumentation);
// RpcGroup.toLayer derives each handler's requirements by scanning every RPC in
// the group, so its type cost grows with handlers × RPCs. Near 200 methods that
// crosses TypeScript's instantiation limit and the layer's requirements degrade to
// `any`, so the handlers are registered as two disjoint, smaller groups.
type ServerWsRpc = RpcGroup.Rpcs<typeof ServerWsRpcGroup>;
const SPLIT_WS_RPC_PREFIXES = ["server", "pullRequests", "provider", "orchestration"] as const;
type SplitWsRpc = Extract<
ServerWsRpc,
{ readonly _tag: `${(typeof SPLIT_WS_RPC_PREFIXES)[number]}.${string}` }
>;
const isSplitWsRpc = (rpc: ServerWsRpc): rpc is SplitWsRpc =>
SPLIT_WS_RPC_PREFIXES.some((prefix) => rpc._tag.startsWith(`${prefix}.`));
const isOtherWsRpc = (rpc: ServerWsRpc): rpc is Exclude<ServerWsRpc, SplitWsRpc> =>
!isSplitWsRpc(rpc);
const serverWsRpcs = Array.from(ServerWsRpcGroup.requests.values());
const SplitWsRpcGroup = RpcGroup.make(...serverWsRpcs.filter(isSplitWsRpc));
const OtherWsRpcGroup = RpcGroup.make(...serverWsRpcs.filter(isOtherWsRpc));
// Replaying each intervening event costs a shell refetch; past this gap a
// single O(active-threads) snapshot is cheaper and bounded.
const SHELL_RESUME_MAX_GAP = 1_000;
// Row counts do not bound large tool payloads; sum stored JSON bytes in SQL and snapshot above budget.
const ORCHESTRATION_REPLAY_PAYLOAD_BUDGET_BYTES = 8 * 1024 * 1024;
function toAuthAccessStreamEvent(
change: PairingGrantStore.BootstrapCredentialChange | SessionStore.SessionCredentialChange,
revision: number,
currentSessionId: AuthSessionId,
): AuthAccessStreamEvent {
switch (change.type) {
case "pairingLinkUpserted":
return {
version: 1,
revision,
type: "pairingLinkUpserted",
payload: { ...change.pairingLink, ...authScopeResponse(change.pairingLink.scopes) },
};
case "pairingLinkRemoved":
return {
version: 1,
revision,
type: "pairingLinkRemoved",
payload: { id: change.id },
};
case "clientUpserted":
return {
version: 1,
revision,
type: "clientUpserted",
payload: {
...change.clientSession,
...authScopeResponse(change.clientSession.scopes),
current: change.clientSession.sessionId === currentSessionId,
},
};
case "clientRemoved":
return {
version: 1,
revision,
type: "clientRemoved",
payload: { sessionId: change.sessionId },
};
}
}
const isClientSurface = Schema.is(ClientSurface);
const isClientConnectionMethod = Schema.is(ClientConnectionMethod);
const isClientDeviceType = Schema.is(ClientDeviceType);
const isClientOs = Schema.is(ClientOs);
const isClientWebDeployment = Schema.is(ClientWebDeployment);
const MAX_CLIENT_APP_VERSION_LENGTH = 64;
const MAX_CLIENT_BROWSER_LENGTH = 64;
const MAX_CLIENT_DEVICE_MODEL_LENGTH = 80;
function hasCompatibleOrchestrationProtocol(url: URL): boolean {
return (
url.searchParams.get(ORCHESTRATION_PROTOCOL_QUERY_PARAM) ===
String(ORCHESTRATION_PROTOCOL_VERSION)
);
}
function readSettingsClient(url: URL) {
const capabilities = url.searchParams.get(CLIENT_CAPABILITIES_QUERY_PARAM)?.split(",") ?? [];
return { secretReferences: capabilities.includes(SETTINGS_SECRET_REFERENCES_CAPABILITY) };
}
function shouldUseBoundedThreadSnapshot(input: {
readonly acceptBoundedSnapshot?: boolean;
readonly acceptDeferredWork?: boolean;
}): boolean {
return input.acceptBoundedSnapshot === true;
}
// Keep telemetry per socket; several client types can connect simultaneously.
function readClientAnalyticsProps(request: HttpServerRequest.HttpServerRequest) {
const url = requestUrl(request);
if (Option.isNone(url)) {
return {};
}
const surface = url.value.searchParams.get("clientSurface");
const appVersion = url.value.searchParams.get("clientAppVersion")?.trim() ?? "";
const deviceType = url.value.searchParams.get("clientDeviceType");
const os = url.value.searchParams.get("clientOs");
const webDeployment = url.value.searchParams.get("clientWebDeployment");
const browser = url.value.searchParams.get("clientBrowser")?.trim() ?? "";
const connectionMethod = url.value.searchParams.get("connectionMethod");
const rawOsMajorVersion = url.value.searchParams.get("clientOsMajorVersion") ?? "";
const osMajorVersion = Number(rawOsMajorVersion);
const deviceModel = url.value.searchParams.get("clientDeviceModel")?.trim() ?? "";
const isMobile = surface === "mobile";
const hasOsMajorVersion =
isMobile && rawOsMajorVersion !== "" && Number.isInteger(osMajorVersion) && osMajorVersion > 0;
const hasDeviceModel =
isMobile && deviceModel !== "" && deviceModel.length <= MAX_CLIENT_DEVICE_MODEL_LENGTH;
return {
...(isClientSurface(surface) ? { surface } : {}),
...(appVersion !== "" && appVersion.length <= MAX_CLIENT_APP_VERSION_LENGTH
? { appVersion, clientAppVersion: appVersion }
: {}),
...(isClientOs(os)
? {
clientOs: os,
...(isMobile && (os === "iOS" || os === "Android") ? { os } : {}),
}
: {}),
...(isClientDeviceType(deviceType) ? { clientDeviceType: deviceType } : {}),
...(surface === "web" && isClientWebDeployment(webDeployment) ? { webDeployment } : {}),
...(surface === "web" && browser !== "" && browser.length <= MAX_CLIENT_BROWSER_LENGTH
? { clientBrowser: browser }
: {}),
...(hasOsMajorVersion ? { osMajorVersion, clientOsMajorVersion: osMajorVersion } : {}),
...(hasDeviceModel ? { deviceModel, clientDeviceModel: deviceModel } : {}),
...(isClientConnectionMethod(connectionMethod) ? { connectionMethod } : {}),
};
}
/**
* Owners a connecting client reports: its hashed T3 Connect account and its
* hashed device id. Only hashes are accepted, so a raw id never reaches analytics.
*/
function readClientOwners(
request: HttpServerRequest.HttpServerRequest,
): ReadonlyArray<TelemetryOwner> {
const url = requestUrl(request);
if (Option.isNone(url)) {
return [];
}
const params = url.value.searchParams;
return (
[
["connect", params.get("clientOwner")],
["device", params.get("clientDevice")],
] as const
).flatMap(([source, ownerId]) =>
ownerId !== null && isTelemetryIdHash(ownerId) ? [{ source, ownerId }] : [],
);
}
const canReplayPersistedRange = Effect.fnUntraced(function* (
afterSequence: number,
headSequence: number,
maxGap: number,
) {
const applicationEvents = yield* OrchestrationEventStore.OrchestrationEventStore;
const replayGap = headSequence - afterSequence;
if (replayGap < 0 || replayGap > maxGap) {
return false;
}
if (afterSequence < (yield* applicationEvents.prunedThroughSequence)) return false;
const stats = yield* applicationEvents.getReplayStats({
afterSequence,
throughSequence: headSequence,
});
if (stats.rawPayloadBytes > ORCHESTRATION_REPLAY_PAYLOAD_BUDGET_BYTES) {
yield* Effect.logDebug("orchestration replay replaced by snapshot", {
afterSequence,
headSequence,
replayGap,
eventCount: stats.eventCount,
payloadBytes: stats.rawPayloadBytes,
payloadBudgetBytes: ORCHESTRATION_REPLAY_PAYLOAD_BUDGET_BYTES,
});
return false;
}
return true;
});
const enrichProjectShells = Effect.fn("ws.orchestrationV2.enrichProjectShells")(
(projects: ReadonlyArray<OrchestrationProjectShell>) =>
Effect.flatMap(ProjectEnrichmentService.ProjectEnrichmentService, (projectEnrichment) =>
Effect.forEach(
projects,
(project) =>
projectEnrichment.getAvailable(project.workspaceRoot).pipe(
Effect.map((enrichment) => ({
project: {
...project,
repositoryIdentity: enrichment.repositoryIdentity,
},
repositoryIdentityResolved: enrichment.repositoryIdentityResolved,
})),
),
{ concurrency: 16 },
).pipe(
Effect.map((enriched) => ({
projects: enriched.map((entry) => entry.project),
resolvedRepositoryIdentityRoots: enriched
.filter((entry) => entry.repositoryIdentityResolved)
.map((entry) => entry.project.workspaceRoot),
})),
),
),
);
export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subscribeThread")(
function* (input: {
readonly threadId: ThreadId;
readonly afterSequence?: number;
readonly requestCompletionMarker?: boolean;
readonly acceptBoundedSnapshot?: boolean;
readonly acceptDeferredWork?: boolean;
readonly acceptCompactTurnItems?: boolean;
readonly acceptDedupedBodies?: boolean;
readonly acceptCompactIds?: boolean;
}) {
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
const dedupeBodies = input.acceptDedupedBodies === true;
// Replay, snapshot, and live tail share one deduper: it must see every delivered record.
const withLiveDedupe = <E, R>(stream: Stream.Stream<OrchestrationV2ThreadStreamItem, E, R>) =>
dedupeBodies ? Stream.suspend(() => Stream.map(stream, makeLiveBodyDeduper())) : stream;
const applicationEvents = yield* OrchestrationEventStore.OrchestrationEventStore;
yield* Effect.annotateCurrentSpan({
"orchestration_v2.thread_id": input.threadId,
});
yield* threadManagement.ensureLegacyTranscript(input.threadId).pipe(
Effect.mapError(
(cause) =>
new OrchestrationV2GetThreadProjectionError({
threadId: input.threadId,
message: `Failed to hydrate migrated thread ${input.threadId}`,
cause,
}),
),
);
const eventStreamFrom = (afterSequence: number) =>
threadManagement
.streamStoredEventsFrom({
threadId: input.threadId,
afterSequence,
})
.pipe(
Stream.map(threadStreamEventItem),
coalesceThreadLiveStream,
Stream.mapError(
(cause) =>
new OrchestrationV2GetThreadProjectionError({
threadId: input.threadId,
message: `Failed while streaming orchestration V2 thread ${input.threadId}`,
cause,
}),
),
);
const loadReplayThrough = (afterSequence: number, throughSequence: number) =>
applicationEvents
.readAgentEvents({
threadId: input.threadId,
afterSequence,
throughSequence,
limit: THREAD_RESUME_MAX_REPLAY_EVENTS + 1,
// Downgrades can leave unknown rows; skip them and resume live from highWater so later events survive.
skipUnknownEventTypes: true,
})
.pipe(
Stream.map((stored) => ({
kind: "event" as const,
sequence: stored.sequence,
event: projectDomainEventForWire(stored.event),
})),
Stream.runCollect,
Effect.map((items) => Array.from(items)),
Effect.mapError(
(cause) =>
new OrchestrationV2GetThreadProjectionError({
threadId: input.threadId,
message: `Failed while replaying orchestration V2 thread ${input.threadId}`,
cause,
}),
),
);
const completionMarker =
input.requestCompletionMarker === true
? Stream.make({ kind: "synchronized" as const })
: Stream.empty;
const snapshotThenLive = Effect.fn("ws.orchestrationV2.threadSnapshotThenLive")(function* () {
const useBoundedSnapshot = shouldUseBoundedThreadSnapshot(input);
const snapshot = yield* (
useBoundedSnapshot
? threadManagement.getThreadSnapshotWindow(input.threadId, {
rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
deferWork: input.acceptDeferredWork === true,
userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns,
})
: threadManagement.getThreadSnapshot(input.threadId)
).pipe(
Effect.mapError(
(cause) =>
new OrchestrationV2GetThreadProjectionError({
threadId: input.threadId,
message: `Failed to load orchestration V2 thread ${input.threadId}`,
cause,
}),
),
);
const { snapshotSequence } = snapshot;
const snapshotItem = useBoundedSnapshot
? buildBoundedThreadStreamSnapshot({
...snapshot,
compactTurnItems: input.acceptCompactTurnItems === true,
dedupeBodies,
compactIds: input.acceptCompactIds === true,
})
: (() => {
const projection = projectThreadProjectionForWire(snapshot.projection);
return {
kind: "snapshot" as const,
snapshotSequence,
projection: dedupeBodies ? dedupeSnapshotBodies(projection) : projection,
};
})();
return rpcInitialItems([snapshotItem]).pipe(
Stream.concat(completionMarker),
Stream.concat(eventStreamFrom(snapshotSequence)),
withLiveDedupe,
);
});
// Subscribe before reading persisted events to retain concurrent publishes; clients dedupe overlap by sequence.
if (input.afterSequence !== undefined) {
const highWater = yield* applicationEvents.latestAgentSequence(input.threadId).pipe(
Effect.mapError(
(cause) =>
new OrchestrationV2GetThreadProjectionError({
threadId: input.threadId,
message: `Failed to prepare orchestration V2 thread ${input.threadId} replay`,
cause,
}),
),
);
const prunedThrough = yield* applicationEvents.prunedThroughSequence.pipe(
Effect.mapError(
(cause) =>
new OrchestrationV2GetThreadProjectionError({
threadId: input.threadId,
message: `Failed to prepare orchestration V2 thread ${input.threadId} replay`,
cause,
}),
),
);
// An idle thread's cursor sits at its own high-water: nothing was missed, so replay nothing.
if (
input.afterSequence > highWater ||
(input.afterSequence < highWater && input.afterSequence < prunedThrough)
) {
return yield* snapshotThenLive();
}
const stats = yield* applicationEvents
.getAgentReplayStats({
threadId: input.threadId,
afterSequence: input.afterSequence,
throughSequence: highWater,
maxEvents: THREAD_RESUME_MAX_REPLAY_EVENTS,
})
.pipe(
Effect.mapError(
(cause) =>
new OrchestrationV2GetThreadProjectionError({
threadId: input.threadId,
message: `Failed to measure orchestration V2 thread ${input.threadId} replay`,
cause,
}),
),
);
// Bound stored and projected payload bytes separately; neither bounds process memory.
if (
stats.eventCount > THREAD_RESUME_MAX_REPLAY_EVENTS ||
!isThreadReplayRawPayloadSafe(stats.rawPayloadBytes)
) {
return yield* snapshotThenLive();
}
if (stats.hasCreateEvent) {
const shell = yield* threadManagement.getThreadShell(input.threadId).pipe(
Effect.mapError(
(cause) =>
new OrchestrationV2GetThreadProjectionError({
threadId: input.threadId,
message: `Failed to locate recreated orchestration V2 thread ${input.threadId}`,
cause,
}),
),
);
// Retained creation events may describe deleted threads; replace replay only if a snapshot exists.
if (shell !== null) return yield* snapshotThenLive();
}
const replayed = yield* loadReplayThrough(input.afterSequence, highWater);
// Pruning publishes its boundary before deleting, so a pass during the read shows up here.
if (
input.afterSequence < highWater &&
input.afterSequence <
(yield* applicationEvents.prunedThroughSequence.pipe(
Effect.mapError(
(cause) =>
new OrchestrationV2GetThreadProjectionError({
threadId: input.threadId,
message: `Failed to prepare orchestration V2 thread ${input.threadId} replay`,
cause,
}),
),
))
) {
return yield* snapshotThenLive();
}
const replay = coalesceLiveUpdatedEvents(replayed);
const plan = decideThreadResume({
afterSequence: input.afterSequence,
highWater,
replayEventCount: replay.length,
replayEncodedBytes: threadReplayEncodedBytes(replay),
});
if (plan.mode === "snapshot") {
return yield* snapshotThenLive();
}
return rpcInitialItems(replay).pipe(
Stream.concat(completionMarker),
Stream.concat(eventStreamFrom(highWater)),
withLiveDedupe,
);
}
return yield* snapshotThenLive();
},
);
export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subscribeShell")(
function* (input: {
readonly afterSequence?: number;
readonly requestCompletionMarker?: boolean;
readonly omitIdleSubagents?: boolean;
readonly omitOlderSettled?: boolean;
}) {
const sql = yield* SqlClient.SqlClient;
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
const applicationEvents = yield* OrchestrationEventStore.OrchestrationEventStore;
const projects = yield* ProjectStore.ProjectStoreV2;
const projectService = yield* ProjectService.ProjectService;
const projectEnrichment = yield* ProjectEnrichmentService.ProjectEnrichmentService;
let subagentThreadIds = new Set<string>();
const filterSubagentTranscript = (stored: ApplicationStoredEvent) =>
shouldPublishShellApplicationEvent(stored, subagentThreadIds);
const enrichmentChanges = yield* projectEnrichment.subscribeChanges;
const loadProjectMetadataSnapshot = Effect.fn("ws.orchestrationV2.loadProjectMetadataSnapshot")(
function* (snapshotSequence: number) {
const enriched = yield* enrichProjectShells(yield* projects.listShells());
return {
snapshot: {
schemaVersion: ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION,
snapshotSequence,
projects: enriched.projects,
threads: [],
archivedThreads: [],
} as OrchestrationV2ShellSnapshot,
resolvedRepositoryIdentityRoots: enriched.resolvedRepositoryIdentityRoots,
};
},
);
const loadSnapshot = Effect.fn("ws.orchestrationV2.loadShellSnapshot")(function* () {
const forSubscriber = yield* loadSubscriberShellSnapshot({
sql,
listShellIndex: threadManagement.listActiveShellIndex(),
readThreads: (threadIds) =>
threadManagement.readShellSnapshot({
location: "active",
...(threadIds === undefined ? {} : { threadIds }),
}),
listProjects: projects.listShells(),
latestSequence: applicationEvents.latestApplicationSequence,
options: {
omitIdleSubagents: input.omitIdleSubagents === true,
omitOlderSettled: input.omitOlderSettled === true,
},
});
subagentThreadIds = forSubscriber.subagentThreadIds;
const enriched = yield* enrichProjectShells(forSubscriber.snapshot.projects);
return {
snapshot: {
...forSubscriber.snapshot,
projects: enriched.projects,
} as OrchestrationV2ShellSnapshot,
resolvedRepositoryIdentityRoots: enriched.resolvedRepositoryIdentityRoots,
};
});
const projectItem = Effect.fn("ws.orchestrationV2.projectShellItem")(function* (
stored: Extract<ShellApplicationEvent, { readonly aggregateKind: "project" }>,
) {
if (stored.type === "project.deleted") {
return {
kind: "project.removed" as const,
sequence: stored.sequence,
projectId: stored.aggregateId,
};
}
const project = yield* projectService.getShell(stored.aggregateId);
return Option.match(project, {
onNone: () => ({
kind: "project.removed" as const,
sequence: stored.sequence,
projectId: stored.aggregateId,
}),
onSome: (value) => ({
kind: "project.updated" as const,
sequence: stored.sequence,
project: value,
}),
});
});
const projectShellItems = Effect.fn("ws.orchestrationV2.projectShellItems")(function* (
events: ReadonlyArray<ShellApplicationEvent>,
) {
const coalesced = coalesceShellApplicationEvents(events);
const threadIds = coalesced.flatMap((stored) =>
"aggregateKind" in stored ? [] : [stored.event.threadId],
);
// A failed batch can't name its thread, so the scalar reads below report which one is bad.
const batch =
threadIds.length > 1
? yield* threadManagement
.getShellSnapshot({ threadIds })
.pipe(Effect.orElseSucceed(() => null))
: null;
const batchedShells =
batch === null
? null
: new Map(
[...batch.threads, ...batch.archivedThreads].map(
(shell) => [shell.id, shell] as const,
),
);
return yield* Effect.forEach(
coalesced,
(stored) =>
Effect.gen(function* () {
if ("aggregateKind" in stored) {
return yield* projectItem(stored);
}
const threadId = stored.event.threadId;
const shell =
batchedShells === null
? yield* threadManagement.getThreadShell(threadId)
: (batchedShells.get(threadId) ?? null);
return shellStreamItemFromThreadShell({ stored, shell });
}),
{ concurrency: 8 },
);
});
const toShellStream = <E, R>(stream: Stream.Stream<ShellApplicationEvent, E, R>) =>
stream.pipe(
Stream.groupedWithin(512, Duration.millis(50)),
Stream.mapEffect((events) => projectShellItems(Array.from(events))),
Stream.flatMap(Stream.fromIterable),
skipUnchangedThreadShells,
);
const liveFrom = (afterSequence: number) =>
bufferShellLiveStream(
toShellStream(
applicationEvents.streamProjectedApplicationEvents({
afterSequence,
project: toShellApplicationEvent,
filter: filterSubagentTranscript,
}),
),
);
const enrichmentRefreshes = Stream.fromSubscription(enrichmentChanges).pipe(
Stream.filter((change) => change.repositoryIdentityResolved),
Stream.groupedWithin(64, Duration.millis(25)),
Stream.mapEffect((changes) =>
Effect.gen(function* () {
const identities = new Map(
Array.from(changes, (change) => [
change.workspaceRoot,
change.enrichment.repositoryIdentity,
]),
);
const snapshotSequence = yield* applicationEvents.latestApplicationSequence;
const changedProjects = (yield* projects.listShells()).flatMap((project) =>
identities.has(project.workspaceRoot)
? [{ ...project, repositoryIdentity: identities.get(project.workspaceRoot) ?? null }]
: [],
);
return shellStreamItemFromEnrichmentRefresh({
snapshot: {
schemaVersion: ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION,
snapshotSequence,
projects: changedProjects,
threads: [],
archivedThreads: [],
} as OrchestrationV2ShellSnapshot,
changes: Array.from(changes),
});
}),
),
);
// Subscribe before loading for racing enrichments; initial refresh covers pre-subscription completions and cold HTTP snapshots.
const completionMarker =
input.requestCompletionMarker === true
? Stream.make({ kind: "synchronized" as const })
: Stream.empty;
const initialSnapshotItems = (loaded: {
readonly snapshot: OrchestrationV2ShellSnapshot;
readonly resolvedRepositoryIdentityRoots: ReadonlyArray<string>;
}) =>
rpcInitialItems(
shellStreamItemsFromInitialSnapshot({
snapshot: loaded.snapshot,
resolvedRepositoryIdentityRoots: loaded.resolvedRepositoryIdentityRoots,
}),
);
const initialEnrichmentItems = (loaded: {
readonly snapshot: OrchestrationV2ShellSnapshot;
readonly resolvedRepositoryIdentityRoots: ReadonlyArray<string>;
}) =>
rpcInitialItems(
shellStreamItemsFromResumeSnapshot({
snapshot: loaded.snapshot,
resolvedRepositoryIdentityRoots: loaded.resolvedRepositoryIdentityRoots,
}),
);
// Drain initial frames before merging enrichment with live events so marked refreshes cannot precede the initial frame.
const completionThenLive = (afterSequence: number) =>
Stream.concat(completionMarker, liveFrom(afterSequence));
const stream = yield* Effect.gen(function* () {
if (input.afterSequence === undefined) {
const loaded = yield* loadSnapshot();
return composeShellStreamWithEnrichment({
initial: initialSnapshotItems(loaded),
tail: completionThenLive(loaded.snapshot.snapshotSequence),
enrichment: enrichmentRefreshes,
});
}
const fromSnapshot = Effect.gen(function* () {
const loaded = yield* loadSnapshot();
return composeShellStreamWithEnrichment({
initial: initialSnapshotItems(loaded),
tail: completionThenLive(loaded.snapshot.snapshotSequence),
enrichment: enrichmentRefreshes,
});
});
const highWater = yield* applicationEvents.latestApplicationSequence;
if (!(yield* canReplayPersistedRange(input.afterSequence, highWater, SHELL_RESUME_MAX_GAP))) {
return yield* fromSnapshot;
}
subagentThreadIds = new Set(yield* threadManagement.listActiveSubagentThreadIds());
const loaded = yield* loadProjectMetadataSnapshot(highWater);
// Read before replying: the range is budgeted above, and a prune pass during the read
// publishes its boundary first, so the recheck below catches deleted rows.
const replayed = yield* applicationEvents
.readApplicationEvents({
afterSequence: input.afterSequence,
throughSequence: highWater,
// A row a newer build wrote (e.g. after a downgrade) must not fail
// every shell resubscribe. The live tail below resumes from
// highWater, so a dropped row never hides later events.
skipUnknownEventTypes: true,
})
.pipe(Stream.filter(filterSubagentTranscript), Stream.runCollect);
if (input.afterSequence < (yield* applicationEvents.prunedThroughSequence)) {
return yield* fromSnapshot;
}
const replay = toShellStream(Stream.fromIterable(replayed));
return composeShellStreamWithEnrichment({
initial: initialEnrichmentItems(loaded),
tail: Stream.concat(Stream.concat(replay, completionMarker), liveFrom(highWater)),
enrichment: enrichmentRefreshes,
});
}).pipe(
Effect.mapError(
(cause) =>
new OrchestrationV2GetShellSnapshotError({
message: "Failed to prepare the application shell stream",
cause,
}),
),
);
return stream.pipe(
dedupeShellEnrichment,
Stream.mapError(
(cause) =>
new OrchestrationV2GetShellSnapshotError({
message: "Failed while streaming the application shell",
cause,
}),
),
);
},
);
export const layerWsRpc = (
currentSession: EnvironmentAuth.AuthenticatedSession,
clientAnalyticsProps: Readonly<Record<string, unknown>>,
serverBrowser: ServerBrowser.ServerBrowser["Service"],
reportProfiles: ServerBrowser.ProfileReporter,
settingsClient: { readonly secretReferences: boolean },
) =>
Layer.effectContext(
Effect.gen(function* () {
const currentSessionId = currentSession.sessionId;
const sql = yield* SqlClient.SqlClient;
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
const intakeContext = yield* Effect.context<
| ThreadManagementService.ThreadManagementService
| ThreadLaunchService.ThreadLaunchService
| FileSystem.FileSystem
| ServerConfig.ServerConfig
| ServerSettings.ServerSettingsService
| DocumentText.DocumentText
>();
const applicationEvents = yield* OrchestrationEventStore.OrchestrationEventStore;
const projectStore = yield* ProjectStore.ProjectStoreV2;
const projectService = yield* ProjectService.ProjectService;
const managedFolders = yield* ManagedProjectFolders.ManagedProjectFolders;
const scratchThreadPromotion = yield* ScratchThreadPromotion.ScratchThreadPromotion;
const eventLoopMonitor = yield* EventLoopMonitor.EventLoopMonitor;
const threadSearch = yield* ThreadSearch.ThreadSearch;
const providerSessionsV2 = yield* ProviderSessionManager.ProviderSessionManagerV2;
const mcpAppRequests = yield* McpAppRequests.McpAppRequests;
const analytics = yield* AnalyticsService.AnalyticsService;
const originProps = clientAnalyticsProps;
const recordClientCommandAnalytics = (command: OrchestrationV2Command) => {
const usage = commandFeatureUsage(command);
return Effect.all(
[
command.type === "message.dispatch"
? analytics.record("client.turn.requested", originProps)
: Effect.void,
usage === undefined
? Effect.void
: analytics.record("feature.used", {
...usage,
...(originProps.surface === undefined ? {} : { surface: originProps.surface }),
}),
],
{ discard: true },
).pipe(Effect.ignore);
};
const threadLaunch = yield* ThreadLaunchService.ThreadLaunchService;
const commandOutputHub = yield* CommandOutputHub.CommandOutputHub;
const providerSessionManager = yield* ProviderSessionManager.ProviderSessionManagerV2;
const scheduledTasks = yield* ScheduledTasks.ScheduledTaskService;
const secretRequests = yield* SecretRequests.SecretRequests;
const guestShares = yield* GuestShareService.GuestShareService;
const pullRequests = yield* PullRequestService.PullRequestService;
const reviewGuides = yield* PullRequestReviewGuideService.PullRequestReviewGuideService;
const pullRequestSync = yield* PullRequestSyncReactor.PullRequestSyncReactor;
const deviceService = yield* DeviceService.DeviceService;
const deviceHostContext =
yield* Effect.context<Effect.Services<ReturnType<typeof remoteSshDeviceHosts>>>();
const orchestrationEngine = yield* Orchestrator.OrchestratorV2;
const random = yield* SecureRandom.SecureRandom;
// RPC client ids are only unique within one socket; terminals share viewers across sockets.
const terminalViewerPrefix = yield* random.uuidV4();
const terminalViewerId = (metadata: { readonly client: { readonly id: number } }) =>
`${terminalViewerPrefix}:${metadata.client.id}`;
const serverCommandId = (tag: string) =>
random.uuidV4().pipe(
Effect.orDie,
Effect.map((id) => CommandId.make(`server:${tag}:${id}`)),
);
const resolvePullRequestSyncKey = (reference: PullRequestRef) =>
reference.host !== undefined && reference.repository.includes("/")
? Effect.succeed(pullRequestSyncKey(reference))
: projectService.getShell(reference.projectId).pipe(
Effect.map((project) =>
pullRequestSyncKey(reference, Option.getOrUndefined(project)?.repositoryIdentity),
),
Effect.orElseSucceed(() => null),
);
const usage = yield* UsageService.UsageService;
const usageLimitSources = yield* UsageLimitSources.UsageLimitSources;
const worktreeSetupTracker = yield* WorktreeSetupTracker.WorktreeSetupTracker;
const projectCloneTracker = yield* ProjectCloneTracker.ProjectCloneTracker;
const repositoryIdentityResolver =
yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
const projectEnrichment = yield* ProjectEnrichmentService.ProjectEnrichmentService;
const agentSessionScanner = yield* AgentSessionScanner.AgentSessionScanner;
const agentSessionImporter = yield* AgentSessionImporter.AgentSessionImporter;
const checkpointDiffQuery = yield* CheckpointDiffQuery.CheckpointDiffQuery;
const nativeChildWork = yield* NativeChildWorkQuery.NativeChildWorkQuery;
const keybindings = yield* Keybindings.Keybindings;
const environmentTheme = yield* EnvironmentTheme.EnvironmentThemeService;
const externalLauncher = yield* ExternalLauncher.ExternalLauncher;
const remoteOpenTargets = yield* RemoteOpenTargets.RemoteOpenTargets;
const directEndpoints = yield* DirectEndpoints.DirectEndpoints;
const gitWorkflow = yield* GitWorkflowService.GitWorkflowService;
const review = yield* ReviewService.ReviewService;
const vcsProvisioning = yield* VcsProvisioningService.VcsProvisioningService;
const vcsStatusBroadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
const terminalManager = yield* TerminalManager.TerminalManager;
const previewManager = yield* PreviewManager.PreviewManager;
const portDiscovery = yield* PortScanner.PortDiscovery;
const providerRegistry = yield* ProviderRegistry.ProviderRegistry;
const providerInstances = yield* ProviderInstanceRegistry.ProviderInstanceRegistry;
const acpRegistryCatalog = yield* AcpRegistrySupport.AcpRegistryCatalog;
const acpRegistryRuntimeCoordinator =
yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator;
const providerMaintenanceRunner = yield* ProviderMaintenanceRunner.ProviderMaintenanceRunner;
const providerAuth = yield* ProviderAuthService.ProviderAuthService;
const providerInstallation = yield* makeProviderInstallation();
const serverSelfUpdate = yield* ServerSelfUpdate.ServerSelfUpdate;
const npmRelease = yield* NpmRelease.NpmRelease;
const config = yield* ServerConfig.ServerConfig;
const lifecycleEvents = yield* ServerLifecycleEvents.ServerLifecycleEvents;
const storageCleanup = yield* StorageCleanup.StorageCleanup;
const databaseStorage = yield* DatabaseStorage.DatabaseStorage;
const serverSettings = yield* ServerSettings.ServerSettingsService;
const secretResolver = yield* SecretResolver.SecretResolver;
const peerLinks = yield* PeerLinks.PeerLinks;
const peerLinkRequests = yield* PeerLinkRequests.PeerLinkRequests;
const startup = yield* ServerRuntimeStartup.ServerRuntimeStartup;
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;
const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
const rpcClientIds = yield* Ref.make(new Set<RpcClientId>());
yield* Effect.addFinalizer(() =>
Ref.get(rpcClientIds).pipe(
Effect.flatMap((clientIds) =>
Effect.forEach(
clientIds,
(clientId) => backgroundPolicy.removeRpcClient(currentSessionId, clientId),
{
discard: true,
},
),
),
Effect.ignore,
),
);
const serverAuth = yield* EnvironmentAuth.EnvironmentAuth;
const sourceControlDiscovery = yield* SourceControlDiscovery.SourceControlDiscovery;
const otlpEndpointCheck = yield* OtlpEndpointCheck.OtlpEndpointCheck;
const sourceControlRepositories =
yield* SourceControlRepositoryService.SourceControlRepositoryService;
const sourceControlSignIn = yield* SourceControlSignIn.SourceControlSignIn;
const withPullRequestViewer = pullRequests.withRoutingCredential;
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
const sessions = yield* SessionStore.SessionStore;
const processDiagnostics = yield* ProcessDiagnostics.ProcessDiagnostics;
const triage = yield* TriageService.TriageService;
const hostResources = yield* HostResources.HostResources;
const processResourceMonitor = yield* ProcessResourceMonitor.ProcessResourceMonitor;
const resourceTelemetry = yield* ResourceTelemetry.ResourceTelemetry;
const relayClient = yield* RelayClient.RelayClient;
// Webhook URLs start agents; expose them only to operate-scoped sessions while keeping tasks readable.
const withVisibleWebhookUrls = (result: ScheduledTaskListResult): ScheduledTaskListResult =>
currentSession.scopes.includes(AuthOrchestrationOperateScope)
? result
: {