import Foundation extension FeatureInputAnswer { var jsonValue: JSONValue { switch self { case let .text(value): .string(value) case let .selections(values): .array(values.map(JSONValue.string)) } } } /// Composes the transport-focused Core layer with the UI-focused Features layer. @MainActor final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { private let runtime: EnvironmentRuntime private let settingsStore: UserDefaults private let stream: AsyncStream private let continuation: AsyncStream.Continuation private var activeEnvironment: Environment? private var client: T3Client? private var latestShell: OrchestrationShellSnapshot? private var environmentClients: [String: T3Client] = [:] private var shellsByEnvironmentID: [String: OrchestrationShellSnapshot] = [:] private var archivedThreadsByEnvironmentID: [String: [FeatureThread]] = [:] private var projectEnvironmentIDs: [String: String] = [:] private var projectWireIDs: [String: String] = [:] private var threadEnvironmentIDs: [String: String] = [:] private var threadWireIDs: [String: String] = [:] private var provisionalThreadRoutes: [String: ProvisionalThreadRoute] = [:] private var environmentConnectionStates: [String: FeatureConnection.State] = [:] private var environmentConnectionDetails: [String: String] = [:] private var latestServerConfig: ServerConfigSnapshot? private var latestSnapshot: FeatureSnapshot? private var activeThreadID: String? private var activeThreadEnvironmentID: String? private var latestDetails: [String: FeatureThreadDetail] = [:] private var attachmentURLs: [AttachmentCacheKey: CachedAttachmentURL] = [:] private var pendingBootstrapSubmission: PendingBootstrapSubmission? private var pendingTurnSubmissions: [String: PendingTurnSubmission] = [:] private var attachmentHydrationTasks: [ String: (id: UUID, task: Task) ] = [:] private var approvalRoutes: [String: PendingRequestRoute] = [:] private var inputRoutes: [String: PendingRequestRoute] = [:] private var terminalIDs: [String: String] = [:] private var terminalSnapshots: [String: FeatureTerminalSnapshot] = [:] private var terminalContinuations: [ String: [UUID: AsyncStream.Continuation] ] = [:] private var pollingTask: Task? private var fallbackPollingTask: Task? private var configurationTask: Task? private var aggregateRefreshTask: Task? private var archivedRefreshTask: Task? private var detailRefreshTask: Task? private var passiveDetailPollingTask: Task? private var detailRefreshPending = false private var detailRefreshGeneration = 0 private var environmentGeneration = 0 private var lastShellEventAt: Date? init( runtime: EnvironmentRuntime = EnvironmentRuntime(), settingsStore: UserDefaults = .standard ) { self.runtime = runtime self.settingsStore = settingsStore let pair = AsyncStream.makeStream() stream = pair.stream continuation = pair.continuation } deinit { pollingTask?.cancel() fallbackPollingTask?.cancel() configurationTask?.cancel() aggregateRefreshTask?.cancel() archivedRefreshTask?.cancel() detailRefreshTask?.cancel() passiveDetailPollingTask?.cancel() attachmentHydrationTasks.values.forEach { $0.task.cancel() } continuation.finish() } func initialSnapshot() async throws -> FeatureSnapshot { let environments = try await runtime.environments() guard let environment = try await runtime.activeEnvironment(), let activeClient = try await runtime.activeClient() else { await clearActiveEnvironment() let snapshot = disconnectedSnapshot(environments: environments) latestSnapshot = snapshot return snapshot } await adoptEnvironment(environment, client: activeClient) let generation = environmentGeneration let loads = await loadEnvironmentShells(environments) guard isCurrentSession(client: activeClient, generation: generation) else { throw CancellationError() } reconcileEnvironmentLoads(loads, savedEnvironments: environments) latestShell = shellsByEnvironmentID[environment.id] startPolling(activeClient) let activeIsReachable = loads.contains { $0.environment.id == environment.id && $0.shell != nil } if activeIsReachable { scheduleArchivedRefresh(client: activeClient, environment: environment) } let snapshot = makeSnapshot( environments: environments, activeEnvironment: environment, connectionState: activeIsReachable ? .connected : .disconnected, connectionDetail: activeIsReachable ? nil : "That server is currently unreachable." ) latestSnapshot = snapshot startAggregateRefresh(activeClient) return snapshot } func events() -> AsyncStream { stream } func pair(endpoint: String, token: String?) async throws { let pairedClient: T3Client if let token, !token.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { pairedClient = try await runtime.pair( host: endpoint, code: token, clientLabel: "T3 Code Swift" ) } else { pairedClient = try await runtime.pair(url: endpoint, clientLabel: "T3 Code Swift") } await adoptEnvironment(pairedClient.environment, client: pairedClient) startPolling(pairedClient) } func activateEnvironment(id: String) async throws { let activated = try await runtime.activate(id: id) await adoptEnvironment(activated.environment, client: activated) startPolling(activated) } func removeEnvironment(id: String) async throws { if activeEnvironment?.id == id { await clearActiveEnvironment(disconnectClient: false) } try await runtime.remove(id: id) } func disconnect() async { await clearActiveEnvironment() } private func adoptEnvironment( _ environment: Environment, client newClient: T3Client ) async { if activeEnvironment?.id == environment.id, client === newClient { activeEnvironment = environment environmentClients[environment.id] = newClient latestShell = shellsByEnvironmentID[environment.id] return } let previousClient = client pollingTask?.cancel() fallbackPollingTask?.cancel() configurationTask?.cancel() aggregateRefreshTask?.cancel() archivedRefreshTask?.cancel() passiveDetailPollingTask?.cancel() pollingTask = nil fallbackPollingTask = nil configurationTask = nil aggregateRefreshTask = nil archivedRefreshTask = nil passiveDetailPollingTask = nil clearEnvironmentState(preserveEnvironmentSnapshots: true) activeEnvironment = environment client = newClient environmentClients[environment.id] = newClient latestShell = shellsByEnvironmentID[environment.id] if let previousClient, previousClient !== newClient { await previousClient.disconnect() } } private func clearActiveEnvironment(disconnectClient: Bool = true) async { let previousClient = client pollingTask?.cancel() fallbackPollingTask?.cancel() configurationTask?.cancel() aggregateRefreshTask?.cancel() archivedRefreshTask?.cancel() passiveDetailPollingTask?.cancel() pollingTask = nil fallbackPollingTask = nil configurationTask = nil aggregateRefreshTask = nil archivedRefreshTask = nil passiveDetailPollingTask = nil clearEnvironmentState() client = nil activeEnvironment = nil if disconnectClient, let previousClient { await previousClient.disconnect() } } private func clearEnvironmentState(preserveEnvironmentSnapshots: Bool = false) { environmentGeneration &+= 1 resetDetailRefresh() attachmentHydrationTasks.values.forEach { $0.task.cancel() } attachmentHydrationTasks.removeAll() archivedRefreshTask?.cancel() archivedRefreshTask = nil latestShell = nil lastShellEventAt = nil latestServerConfig = nil if !preserveEnvironmentSnapshots { environmentClients.removeAll() shellsByEnvironmentID.removeAll() archivedThreadsByEnvironmentID.removeAll() projectEnvironmentIDs.removeAll() projectWireIDs.removeAll() threadEnvironmentIDs.removeAll() threadWireIDs.removeAll() provisionalThreadRoutes.removeAll() environmentConnectionStates.removeAll() environmentConnectionDetails.removeAll() } latestSnapshot = nil activeThreadID = nil activeThreadEnvironmentID = nil latestDetails.removeAll() attachmentURLs.removeAll() pendingBootstrapSubmission = nil pendingTurnSubmissions.removeAll() approvalRoutes.removeAll() inputRoutes.removeAll() finishTerminalStreams() terminalIDs.removeAll() terminalSnapshots.removeAll() } private func isCurrentSession(client: T3Client, generation: Int) -> Bool { guard generation == environmentGeneration, let currentClient = self.client else { return false } return currentClient === client } private func isKnownClient( _ client: T3Client, environmentID: String, generation: Int ) -> Bool { generation == environmentGeneration && environmentClients[environmentID] === client } func addProject(path: String) async throws { let client = try requireClient() let trimmed = path.trimmingCharacters(in: .whitespacesAndNewlines) guard !trimmed.isEmpty else { throw NativeFeatureClientError.invalidProjectPath } let title = URL(fileURLWithPath: trimmed).lastPathComponent _ = try await client.createProject( title: title.isEmpty ? "Project" : title, workspaceRoot: trimmed, defaultModel: defaultModelSelection() ) try await refresh(client: client) } func createThread( projectID: String, title: String?, selection: FeatureSelection? ) async throws -> FeatureThread { let route = try projectRoute(for: projectID) let client = route.client let environment = client.environment let generation = environmentGeneration let threadID = UUID().uuidString let model = modelSelection( selection, projectID: route.wireID, shell: shellsByEnvironmentID[environment.id] ) let resolvedTitle = title?.trimmingCharacters(in: .whitespacesAndNewlines) let threadTitle = resolvedTitle?.isEmpty == false ? resolvedTitle! : "New thread" _ = try await client.createThread( threadID: threadID, projectID: route.wireID, title: threadTitle, model: model, runtimeMode: .fullAccess ) guard isKnownClient(client, environmentID: environment.id, generation: generation) else { throw CancellationError() } registerProvisionalThread(wireID: threadID, environmentID: environment.id) if let shell = try? await client.shellSnapshot() { guard isKnownClient(client, environmentID: environment.id, generation: generation) else { throw CancellationError() } shellsByEnvironmentID[environment.id] = shell if activeEnvironment?.id == environment.id { latestShell = shell } await emitSnapshot(shell, environment: environment) if let created = shell.threads.first(where: { $0.id == threadID }) { provisionalThreadRoutes[FeatureScopedID.thread( environmentID: environment.id, wireID: threadID )] = nil return mapThread(created, environment: environment) } } return FeatureThread( id: FeatureScopedID.thread(environmentID: environment.id, wireID: threadID), wireID: threadID, projectID: route.uiID, environmentID: environment.id, environmentName: environment.label, title: threadTitle, providerID: model.instanceId, providerName: providerDisplayName(model.instanceId), modelID: model.model ) } func createThreadAndSend( projectID: String, prompt: String, selection: FeatureSelection?, runtimeMode: FeatureRuntimeMode, interactionMode: FeatureInteractionMode, attachments: [FeatureUploadAttachment] ) async throws -> FeatureThread { let route = try projectRoute(for: projectID) let client = route.client let environment = client.environment let generation = environmentGeneration let model = modelSelection( selection, projectID: route.wireID, shell: shellsByEnvironmentID[environment.id] ) let title = Self.title(from: prompt, hasAttachments: !attachments.isEmpty) let uploads = try makeUploadAttachments(attachments) let runtime = coreRuntimeMode(runtimeMode) let interaction = coreInteractionMode(interactionMode) let signature = BootstrapSubmissionSignature( projectID: projectID, prompt: prompt, model: model, runtimeMode: runtime, interactionMode: interaction, attachments: attachments ) let pending: PendingBootstrapSubmission if let existing = pendingBootstrapSubmission, existing.signature == signature { pending = existing } else { pending = PendingBootstrapSubmission( signature: signature, threadID: UUID().uuidString, identity: CommandIdentity() ) pendingBootstrapSubmission = pending } do { _ = try await client.createThreadAndSend( threadID: pending.threadID, projectID: route.wireID, title: title, text: prompt, model: model, runtimeMode: runtime, interactionMode: interaction, attachments: uploads, commandID: pending.identity.commandID, messageID: pending.identity.messageID, createdAt: pending.identity.createdAt ) } catch { // A connection can disappear after the server accepted the command // but before its reply reaches us. Bootstrap expansion creates the // thread before dispatching the stable final turn, so recover an // interrupted empty thread by sending only that original turn. guard try await recoverBootstrap( client: client, pending: pending, projectID: route.wireID, text: prompt, model: model, runtimeMode: runtime, interactionMode: interaction, attachments: uploads ) else { throw error } } registerProvisionalThread(wireID: pending.threadID, environmentID: environment.id) guard isKnownClient(client, environmentID: environment.id, generation: generation) else { throw CancellationError() } if pendingBootstrapSubmission?.identity == pending.identity { pendingBootstrapSubmission = nil } // Dispatch acceptance is the commit point. A dropped refresh must not // turn a successful first turn into a retry that creates a duplicate. if let shell = try? await client.shellSnapshot() { guard isKnownClient(client, environmentID: environment.id, generation: generation) else { throw CancellationError() } shellsByEnvironmentID[environment.id] = shell if activeEnvironment?.id == environment.id { latestShell = shell } await emitSnapshot(shell, environment: environment) if let created = shell.threads.first(where: { $0.id == pending.threadID }) { provisionalThreadRoutes[FeatureScopedID.thread( environmentID: environment.id, wireID: pending.threadID )] = nil return mapThread(created, environment: environment) } } return FeatureThread( id: FeatureScopedID.thread( environmentID: environment.id, wireID: pending.threadID ), wireID: pending.threadID, projectID: route.uiID, environmentID: environment.id, environmentName: environment.label, title: title, providerID: model.instanceId, providerName: providerDisplayName(model.instanceId), modelID: model.model, modelOptions: mapOptionSelections(model.options), runtimeMode: runtimeMode, interactionMode: interactionMode ) } private func recoverBootstrap( client: T3Client, pending: PendingBootstrapSubmission, projectID: String, text: String, model: ModelSelection, runtimeMode: RuntimeMode, interactionMode: InteractionMode, attachments: [UploadChatImageAttachment] ) async throws -> Bool { guard let snapshot = try? await client.threadSnapshot(id: pending.threadID) else { return false } if snapshot.thread.messages.contains(where: { $0.id == pending.identity.messageID }) { return true } guard snapshot.thread.projectId == projectID, snapshot.thread.deletedAt == nil, snapshot.thread.messages.isEmpty else { return false } do { _ = try await client.sendTurn( threadID: pending.threadID, text: text, runtimeMode: runtimeMode, interactionMode: interactionMode, model: model, attachments: attachments, commandID: pending.identity.commandID, messageID: pending.identity.messageID, createdAt: pending.identity.createdAt ) } catch { guard await messageWasCommitted( client: client, threadID: pending.threadID, messageID: pending.identity.messageID ) else { throw error } } return true } func renameThread(id: String, title: String) async throws { let route = try threadRoute(for: id) _ = try await route.client.rename(threadID: route.wireID, title: title) updateCachedArchivedThread(id: route.uiID) { $0.title = title } try? await refresh(client: route.client) } func setThreadArchived(id: String, archived: Bool) async throws { let route = try threadRoute(for: id) let cached = cachedThread(id: route.uiID) _ = try await route.client.archive(threadID: route.wireID, archived: archived) reconcileArchivedCache(thread: cached, route: route, archived: archived) await emitCachedSnapshot(for: route.environmentID) try? await refresh(client: route.client, includeArchived: true) } func setThreadSettled(id: String, settled: Bool) async throws { let route = try threadRoute(for: id) _ = try await route.client.settle(threadID: route.wireID, settled: settled) try? await refresh(client: route.client) } func setThreadSnoozed(id: String, until: Date?) async throws { let route = try threadRoute(for: id) _ = try await route.client.snooze(threadID: route.wireID, until: until) try? await refresh(client: route.client) } func setRuntimeMode(id: String, mode: FeatureRuntimeMode) async throws { let route = try threadRoute(for: id) _ = try await route.client.setRuntimeMode( threadID: route.wireID, mode: coreRuntimeMode(mode) ) try? await refresh(client: route.client) if activeThreadID == route.uiID { try? await refreshThread(id: route.uiID, client: route.client) } } func setInteractionMode(id: String, mode: FeatureInteractionMode) async throws { let route = try threadRoute(for: id) _ = try await route.client.setInteractionMode( threadID: route.wireID, mode: coreInteractionMode(mode) ) try? await refresh(client: route.client) if activeThreadID == route.uiID { try? await refreshThread(id: route.uiID, client: route.client) } } func deleteThread(id: String) async throws { let route = try threadRoute(for: id) _ = try await route.client.delete(threadID: route.wireID) archivedThreadsByEnvironmentID[route.environmentID]?.removeAll { $0.id == route.uiID } if let shell = shellsByEnvironmentID[route.environmentID] { shellsByEnvironmentID[route.environmentID] = OrchestrationShellSnapshot( snapshotSequence: shell.snapshotSequence, projects: shell.projects, threads: shell.threads.filter { $0.id != route.wireID }, updatedAt: shell.updatedAt ) } provisionalThreadRoutes[route.uiID] = nil if activeThreadID == route.uiID { resetDetailRefresh() passiveDetailPollingTask?.cancel() passiveDetailPollingTask = nil activeThreadID = nil activeThreadEnvironmentID = nil } latestDetails[route.uiID] = nil await emitCachedSnapshot(for: route.environmentID) try? await refresh(client: route.client, includeArchived: true) } func loadThread(id: String) async throws -> FeatureThreadDetail { let route = try threadRoute(for: id) let client = route.client let environment = client.environment let generation = environmentGeneration resetDetailRefresh() passiveDetailPollingTask?.cancel() passiveDetailPollingTask = nil activeThreadID = route.uiID activeThreadEnvironmentID = environment.id let snapshot = try await client.threadSnapshot(id: route.wireID) guard isKnownClient(client, environmentID: environment.id, generation: generation) else { throw CancellationError() } let detail = mapDetail(snapshot.thread, environment: environment) latestDetails[route.uiID] = detail scheduleAttachmentHydration( in: detail, threadID: route.uiID, client: client, environmentID: environment.id ) startPassiveDetailPolling(route) return detail } func releaseThread(id: String) { guard activeThreadID == id else { return } resetDetailRefresh() passiveDetailPollingTask?.cancel() passiveDetailPollingTask = nil activeThreadID = nil activeThreadEnvironmentID = nil } func sendMessage( threadID: String, text: String, selection: FeatureSelection? ) async throws { try await sendMessage( threadID: threadID, text: text, selection: selection, attachments: [] ) } func sendMessage( threadID: String, text: String, selection: FeatureSelection?, attachments: [FeatureUploadAttachment] ) async throws { let route = try threadRoute(for: threadID) let client = route.client let environmentID = route.environmentID let generation = environmentGeneration guard let shellThread = shellsByEnvironmentID[environmentID]?.threads .first(where: { $0.id == route.wireID }) else { throw NativeFeatureClientError.threadNotFound } let model = selection.map(coreModelSelection) let uploads = try makeUploadAttachments(attachments) let signature = TurnSubmissionSignature( text: text, model: model, runtimeMode: shellThread.runtimeMode, interactionMode: shellThread.interactionMode, attachments: attachments ) let pending: PendingTurnSubmission if let existing = pendingTurnSubmissions[route.uiID], existing.signature == signature { pending = existing } else { pending = PendingTurnSubmission( signature: signature, identity: CommandIdentity() ) pendingTurnSubmissions[route.uiID] = pending } do { _ = try await client.sendTurn( threadID: route.wireID, text: text, runtimeMode: shellThread.runtimeMode, interactionMode: shellThread.interactionMode, model: model, attachments: uploads, commandID: pending.identity.commandID, messageID: pending.identity.messageID, createdAt: pending.identity.createdAt ) } catch { guard isKnownClient(client, environmentID: environmentID, generation: generation) else { throw CancellationError() } guard await messageWasCommitted( client: client, threadID: route.wireID, messageID: pending.identity.messageID ) else { // Keep the stable identity. Retrying the same restored draft // cannot enqueue a duplicate turn after an ambiguous failure. throw error } } guard isKnownClient(client, environmentID: environmentID, generation: generation) else { throw CancellationError() } if pendingTurnSubmissions[route.uiID]?.identity == pending.identity { pendingTurnSubmissions[route.uiID] = nil } // Live sync reconciles these snapshots. Refreshes are opportunistic // after the accepted command so transient reads cannot invite a // duplicate user turn. try? await refreshThread(id: route.uiID, client: client) try? await refresh(client: client) } private func messageWasCommitted( client: T3Client, threadID: String, messageID: String ) async -> Bool { guard let snapshot = try? await client.threadSnapshot(id: threadID) else { return false } return snapshot.thread.messages.contains { $0.id == messageID } } func cancelTurn(threadID: String) async throws { let route = try threadRoute(for: threadID) let turnID = shellsByEnvironmentID[route.environmentID]?.threads .first(where: { $0.id == route.wireID })? .latestTurn? .turnId _ = try await route.client.interrupt(threadID: route.wireID, turnID: turnID) try? await refresh(client: route.client) } func resolveApproval(id: String, decision: FeatureApprovalDecision) async throws { guard let request = approvalRoutes[id] else { throw NativeFeatureClientError.approvalNotFound } let route = try threadRoute(for: request.threadID) let wireDecision = switch decision { case .allowOnce: "accept" case .allowForSession: "acceptForSession" case .deny: "decline" } _ = try await route.client.respondToApproval( threadID: route.wireID, requestID: request.wireID, decision: wireDecision ) approvalRoutes[id] = nil removeCachedApproval(id: id, threadID: route.uiID) try? await refreshThread(id: route.uiID, client: route.client) } func resolveUserInput(id: String, answers: [String: FeatureInputAnswer]) async throws { guard let request = inputRoutes[id] else { throw NativeFeatureClientError.inputRequestNotFound } let route = try threadRoute(for: request.threadID) _ = try await route.client.respondToUserInput( threadID: route.wireID, requestID: request.wireID, answers: answers.mapValues(\.jsonValue) ) inputRoutes[id] = nil removeCachedInput(id: id, threadID: route.uiID) try? await refreshThread(id: route.uiID, client: route.client) } func saveSettings(_ settings: FeatureSettings) async throws { let data = try JSONEncoder().encode(settings) settingsStore.set(data, forKey: Self.settingsKey) } func loadDeviceSessions() async throws -> [FeatureDeviceSession] { let client = try requireClient() try await requireScope("access:read", client: client) return try await client.clientSessions().map { session in FeatureDeviceSession( sessionID: session.sessionId, label: session.client.label, deviceType: FeatureDeviceType(rawValue: session.client.deviceType) ?? .unknown, operatingSystem: session.client.os, browser: session.client.browser, ipAddress: session.client.ipAddress, issuedAt: parseDate(session.issuedAt), expiresAt: parseDate(session.expiresAt), lastConnectedAt: session.lastConnectedAt.map(parseDate), isConnected: session.connected, isCurrent: session.current ) } } func revokeDeviceSession(id: String) async throws { let client = try requireClient() try await requireScope("access:write", client: client) guard try await client.revokeClientSession(id: id) else { throw NativeFeatureClientError.deviceSessionNotFound } } func revokeOtherDeviceSessions() async throws { let client = try requireClient() try await requireScope("access:write", client: client) _ = try await client.revokeOtherClientSessions() } func listFiles(threadID: String, path: String?) async throws -> [FeatureFileEntry] { let route = try threadRoute(for: threadID) let context = try workspaceContext(route: route) let result = try await route.client.listProjectEntries(cwd: context.cwd) return NativeWorkspaceMapper.files(result.entries, directory: path) } func readFile(threadID: String, path: String) async throws -> FeatureFileContent { let route = try threadRoute(for: threadID) let context = try workspaceContext(route: route) let result = try await route.client.readProjectFile( cwd: context.cwd, relativePath: path ) return FeatureFileContent( path: result.relativePath, text: result.contents, language: NativeWorkspaceMapper.language(for: result.relativePath), isTruncated: result.truncated, totalBytes: result.byteLength ) } func loadReview(threadID: String) async throws -> FeatureReview { let route = try threadRoute(for: threadID) let context = try workspaceContext(route: route) let preview = try await route.client.reviewDiffPreview(cwd: context.cwd) return NativeWorkspaceMapper.review(preview) } func sourceControlStatus(threadID: String) async throws -> FeatureSourceControlStatus { let route = try threadRoute(for: threadID) let context = try workspaceContext(route: route) return NativeWorkspaceMapper.sourceControl( try await route.client.refreshVCSStatus(cwd: context.cwd) ) } func performSourceControlAction( threadID: String, action: FeatureSourceControlAction, message: String? ) async throws -> FeatureSourceControlStatus { let route = try threadRoute(for: threadID) let client = route.client let context = try workspaceContext(route: route) if action == .pull { _ = try await client.pull(cwd: context.cwd) } else { let progress = try await client.runGitAction( cwd: context.cwd, action: NativeWorkspaceMapper.gitAction(action), commitMessage: message ) for try await event in progress { if event.kind == "action_failed" { throw RPCError.remote(event.message ?? "The source-control action failed.") } } } return NativeWorkspaceMapper.sourceControl( try await client.refreshVCSStatus(cwd: context.cwd) ) } func terminalSnapshot(threadID: String) async throws -> FeatureTerminalSnapshot { let route = try threadRoute(for: threadID) if let snapshot = terminalSnapshots[route.uiID] { return snapshot } let context = try workspaceContext(route: route) return FeatureTerminalSnapshot( threadID: route.uiID, workingDirectory: context.cwd ) } func terminalEvents(threadID: String) -> AsyncStream { guard let route = try? threadRoute(for: threadID) else { return AsyncStream { continuation in continuation.finish() } } let environmentID = route.environmentID let client = route.client let uiThreadID = route.uiID let generation = environmentGeneration return AsyncStream { continuation in let subscriptionID = UUID() terminalContinuations[uiThreadID, default: [:]][subscriptionID] = continuation if let snapshot = terminalSnapshots[uiThreadID] { continuation.yield(snapshot) } let task = Task { [weak self] in do { for try await event in await client.terminalEvents() { guard !Task.isCancelled else { break } guard let self else { break } guard self.isKnownClient( client, environmentID: environmentID, generation: generation ) else { break } guard event.threadId == route.wireID else { continue } if let terminalID = self.terminalIDs[uiThreadID], event.terminalId != nil, event.terminalId != terminalID { continue } let snapshot = self.consumeTerminalEvent(event, threadID: uiThreadID) self.publishTerminal(snapshot, threadID: uiThreadID) } continuation.finish() } catch is CancellationError { continuation.finish() } catch { guard let self else { continuation.finish() return } guard self.isKnownClient( client, environmentID: environmentID, generation: generation ) else { continuation.finish() return } var snapshot = self.terminalSnapshots[uiThreadID] ?? FeatureTerminalSnapshot(threadID: uiThreadID) snapshot.state = .failed snapshot.error = error.localizedDescription self.terminalSnapshots[uiThreadID] = snapshot self.publishTerminal(snapshot, threadID: uiThreadID) continuation.finish() } } continuation.onTermination = { @Sendable [weak self] _ in task.cancel() Task { @MainActor in self?.terminalContinuations[uiThreadID]?[subscriptionID] = nil if self?.terminalContinuations[uiThreadID]?.isEmpty == true { self?.terminalContinuations[uiThreadID] = nil } } } } } func openTerminal(threadID: String, columns: Int, rows: Int) async throws { let route = try threadRoute(for: threadID) let client = route.client let environmentID = route.environmentID let generation = environmentGeneration let context = try workspaceContext(route: route) let terminalID = terminalIDs[route.uiID] ?? UUID().uuidString terminalIDs[route.uiID] = terminalID let snapshot = try await client.openTerminal( threadID: route.wireID, terminalID: terminalID, cwd: context.cwd, worktreePath: context.worktreePath, columns: columns, rows: rows ) guard isKnownClient(client, environmentID: environmentID, generation: generation) else { throw CancellationError() } let mapped = NativeWorkspaceMapper.terminal(snapshot) var scoped = mapped scoped.threadID = route.uiID terminalSnapshots[route.uiID] = scoped publishTerminal(scoped, threadID: route.uiID) } func writeTerminal(threadID: String, data: String) async throws { let route = try threadRoute(for: threadID) let terminalID = try requireTerminalID(threadID: route.uiID) try await route.client.writeTerminal( threadID: route.wireID, terminalID: terminalID, data: data ) } func resizeTerminal(threadID: String, columns: Int, rows: Int) async throws { let route = try threadRoute(for: threadID) let terminalID = try requireTerminalID(threadID: route.uiID) try await route.client.resizeTerminal( threadID: route.wireID, terminalID: terminalID, columns: columns, rows: rows ) } func closeTerminal(threadID: String) async throws { let route = try threadRoute(for: threadID) let client = route.client let environmentID = route.environmentID let generation = environmentGeneration let terminalID = try requireTerminalID(threadID: route.uiID) try await client.closeTerminal(threadID: route.wireID, terminalID: terminalID) guard isKnownClient(client, environmentID: environmentID, generation: generation) else { throw CancellationError() } terminalIDs[route.uiID] = nil let snapshot = FeatureTerminalSnapshot(threadID: route.uiID) terminalSnapshots[route.uiID] = snapshot publishTerminal(snapshot, threadID: route.uiID) } private func requireClient() throws -> T3Client { guard let client else { throw NativeFeatureClientError.notConnected } return client } private func projectRoute(for projectID: String) throws -> NativeProjectRoute { guard let environmentID = projectEnvironmentIDs[projectID], let wireID = projectWireIDs[projectID], let client = environmentClients[environmentID] else { throw NativeFeatureClientError.projectNotFound } return NativeProjectRoute( uiID: FeatureScopedID.project(environmentID: environmentID, wireID: wireID), wireID: wireID, environmentID: environmentID, client: client ) } private func threadRoute(for threadID: String) throws -> NativeThreadRoute { guard let environmentID = threadEnvironmentIDs[threadID], let wireID = threadWireIDs[threadID], let client = environmentClients[environmentID] else { throw NativeFeatureClientError.threadNotFound } return NativeThreadRoute( uiID: FeatureScopedID.thread(environmentID: environmentID, wireID: wireID), wireID: wireID, environmentID: environmentID, client: client ) } private func registerProvisionalThread(wireID: String, environmentID: String) { let uiID = FeatureScopedID.thread(environmentID: environmentID, wireID: wireID) provisionalThreadRoutes[uiID] = ProvisionalThreadRoute( environmentID: environmentID, wireID: wireID ) threadEnvironmentIDs[uiID] = environmentID threadWireIDs[uiID] = wireID } private func cachedThread(id: String) -> FeatureThread? { latestSnapshot?.threads.first(where: { $0.id == id }) ?? archivedThreadsByEnvironmentID.values.lazy .flatMap { $0 } .first(where: { $0.id == id }) } private func updateCachedArchivedThread( id: String, update: (inout FeatureThread) -> Void ) { for environmentID in Array(archivedThreadsByEnvironmentID.keys) { guard var threads = archivedThreadsByEnvironmentID[environmentID], let index = threads.firstIndex(where: { $0.id == id }) else { continue } update(&threads[index]) archivedThreadsByEnvironmentID[environmentID] = threads return } } private func reconcileArchivedCache( thread: FeatureThread?, route: NativeThreadRoute, archived: Bool ) { archivedThreadsByEnvironmentID[route.environmentID, default: []] .removeAll { $0.id == route.uiID } if var thread { // Keep the accepted lifecycle transition visible until both live // and archived follow-up reads converge, including when the // owning passive device drops immediately after the command. thread.isArchived = archived archivedThreadsByEnvironmentID[route.environmentID, default: []].append(thread) } if archived { if let shell = shellsByEnvironmentID[route.environmentID] { shellsByEnvironmentID[route.environmentID] = OrchestrationShellSnapshot( snapshotSequence: shell.snapshotSequence, projects: shell.projects, threads: shell.threads.filter { $0.id != route.wireID }, updatedAt: shell.updatedAt ) } } } private func emitCachedSnapshot(for environmentID: String) async { guard let environment = environmentClients[environmentID]?.environment, let shell = shellsByEnvironmentID[environmentID] else { return } await emitSnapshot(shell, environment: environment) } private func removeCachedApproval(id: String, threadID: String) { guard var detail = latestDetails[threadID] else { return } detail.approvals.removeAll { $0.id == id } if detail.approvals.isEmpty, detail.thread.state == .waitingForApproval { detail.thread.state = detail.userInputs.isEmpty ? .idle : .waitingForInput } latestDetails[threadID] = detail continuation.yield(.detail(detail)) } private func removeCachedInput(id: String, threadID: String) { guard var detail = latestDetails[threadID] else { return } detail.userInputs.removeAll { $0.id == id } if detail.userInputs.isEmpty, detail.thread.state == .waitingForInput { detail.thread.state = detail.approvals.isEmpty ? .idle : .waitingForApproval } latestDetails[threadID] = detail continuation.yield(.detail(detail)) } private func requireTerminalID(threadID: String) throws -> String { guard let terminalID = terminalIDs[threadID] else { throw NativeFeatureClientError.terminalNotOpen } return terminalID } private func workspaceContext(route: NativeThreadRoute) throws -> ( cwd: String, worktreePath: String? ) { guard let shell = shellsByEnvironmentID[route.environmentID], let thread = shell.threads.first(where: { $0.id == route.wireID }), let project = shell.projects.first(where: { $0.id == thread.projectId }) else { throw NativeFeatureClientError.workspaceNotFound } return ( cwd: thread.worktreePath ?? project.workspaceRoot, worktreePath: thread.worktreePath ) } private func consumeTerminalEvent( _ event: TerminalEvent, threadID: String ) -> FeatureTerminalSnapshot { if let coreSnapshot = event.snapshot { var snapshot = NativeWorkspaceMapper.terminal(coreSnapshot) snapshot.threadID = threadID terminalIDs[threadID] = coreSnapshot.terminalId terminalSnapshots[threadID] = snapshot return snapshot } var snapshot = terminalSnapshots[threadID] ?? FeatureTerminalSnapshot(threadID: threadID) switch event.type { case "output": snapshot.buffer.append(NativeWorkspaceMapper.terminalText(event.data ?? "")) case "exited": snapshot.state = .exited snapshot.exitCode = event.exitCode case "closed": snapshot.state = .stopped case "error": snapshot.state = .failed snapshot.error = event.message case "cleared": snapshot.buffer = "" case "activity": snapshot.title = event.label ?? snapshot.title default: break } terminalSnapshots[threadID] = snapshot return snapshot } private func publishTerminal( _ snapshot: FeatureTerminalSnapshot, threadID: String ) { for continuation in terminalContinuations[threadID]?.values ?? [:].values { continuation.yield(snapshot) } } private func finishTerminalStreams() { for continuations in terminalContinuations.values { for continuation in continuations.values { continuation.finish() } } terminalContinuations.removeAll() } private func startPolling(_ activeClient: T3Client) { pollingTask?.cancel() fallbackPollingTask?.cancel() configurationTask?.cancel() let generation = environmentGeneration pollingTask = Task { [weak self] in do { await activeClient.connect() guard let self, self.isCurrentSession(client: activeClient, generation: generation) else { return } let sequence = self.latestShell?.snapshotSequence for try await item in await activeClient.shellEvents(after: sequence) { guard !Task.isCancelled, self.isCurrentSession( client: activeClient, generation: generation ) else { break } self.lastShellEventAt = .now self.emitConnection(.connected) switch item { case let .snapshot(shell): await self.consume( shell: shell, client: activeClient, refreshActiveThread: true ) case .projectUpserted, .projectRemoved, .threadUpserted, .threadRemoved: await self.consume(delta: item, client: activeClient) case .synchronized: break } } } catch is CancellationError { return } catch { // The independent HTTP fallback below keeps the workspace // fresh while the socket reconnects. } guard !Task.isCancelled, let self, self.isCurrentSession(client: activeClient, generation: generation) else { return } self.emitConnection( .reconnecting, detail: "Live updates paused. Refreshing over HTTP." ) } fallbackPollingTask = Task { [weak self] in try? await Task.sleep(for: .seconds(3)) while !Task.isCancelled { guard let self, self.isCurrentSession( client: activeClient, generation: generation ) else { return } let socketIsSynchronized = await activeClient.liveConnectionActive() && self.lastShellEventAt != nil if !socketIsSynchronized { self.emitConnection( .reconnecting, detail: "Live updates reconnecting. Refreshing over HTTP." ) do { let shell = try await activeClient.shellSnapshot() guard !Task.isCancelled else { return } await self.consume( shell: shell, client: activeClient, refreshActiveThread: true ) } catch is CancellationError { return } catch { self.emitConnection( .reconnecting, detail: "Server unreachable. Retrying automatically." ) } } try? await Task.sleep(for: .seconds(2)) } } configurationTask = Task { [weak self] in do { for try await event in await activeClient.serverConfigEvents() { guard !Task.isCancelled, let self, self.isCurrentSession( client: activeClient, generation: generation ) else { break } switch event { case let .snapshot(config): self.latestServerConfig = config case let .providerStatuses(providers): self.latestServerConfig = ServerConfigSnapshot(providers: providers) case .unrelated: continue } if let shell = self.latestShell { await self.emitSnapshot(shell) } } } catch is CancellationError { return } catch { // The shell and thread streams remain useful on older servers // that do not expose the provider catalogue subscription. } } } /// Non-active environments do not hold WebSocket subscriptions. A quiet /// HTTP refresh keeps their home rows and reachability useful without /// multiplying live streams or creating a high-frequency battery cost. private func startAggregateRefresh(_ activeClient: T3Client) { aggregateRefreshTask?.cancel() let generation = environmentGeneration aggregateRefreshTask = Task { [weak self] in while !Task.isCancelled { do { try await Task.sleep(for: .seconds(20)) } catch { return } guard let self, self.isCurrentSession( client: activeClient, generation: generation ), let activeEnvironment = self.activeEnvironment, let environments = try? await self.runtime.environments() else { return } let passiveEnvironments = environments.filter { $0.id != activeEnvironment.id } guard !passiveEnvironments.isEmpty else { continue } let loads = await self.loadEnvironmentShells(passiveEnvironments) guard !Task.isCancelled, self.isCurrentSession( client: activeClient, generation: generation ) else { return } self.reconcileEnvironmentLoads(loads, savedEnvironments: environments) let currentConnection = self.latestSnapshot?.connection ?? FeatureConnection( state: .disconnected, environmentName: activeEnvironment.label, endpoint: activeEnvironment.httpBaseURL.absoluteString ) let snapshot = self.makeSnapshot( environments: environments, activeEnvironment: activeEnvironment, connectionState: currentConnection.state, connectionDetail: currentConnection.detail ) guard self.latestSnapshot != snapshot else { continue } self.latestSnapshot = snapshot self.continuation.yield(.snapshot(snapshot)) } } } private func consume( shell: OrchestrationShellSnapshot, client: T3Client, refreshActiveThread: Bool ) async { guard let currentClient = self.client, currentClient === client else { return } latestShell = shell await emitSnapshot(shell) if refreshActiveThread, let threadID = activeThreadID { scheduleDetailRefresh(threadID: threadID, client: client) } } private func consume(delta: ShellStreamItem, client: T3Client) async { guard let currentClient = self.client, currentClient === client else { return } guard let current = latestShell else { if let shell = try? await client.shellSnapshot() { await consume(shell: shell, client: client, refreshActiveThread: true) } return } let sequence: Int switch delta { case let .projectUpserted(nextSequence, _): sequence = nextSequence case let .projectRemoved(nextSequence, _): sequence = nextSequence case let .threadUpserted(nextSequence, _): sequence = nextSequence case let .threadRemoved(nextSequence, _): sequence = nextSequence case .snapshot, .synchronized: return } // Replayed deltas are expected after reconnect. They must be entirely // side-effect free, including for cached detail and selection state. guard sequence > current.snapshotSequence else { return } var projects = current.projects var threads = current.threads var changedThreadID: String? var shouldRefreshArchived = false switch delta { case let .projectUpserted(_, project): if let index = projects.firstIndex(where: { $0.id == project.id }) { projects[index] = project } else { projects.append(project) } case let .projectRemoved(_, projectID): projects.removeAll { $0.id == projectID } case let .threadUpserted(_, thread): changedThreadID = activeEnvironment.map { FeatureScopedID.thread(environmentID: $0.id, wireID: thread.id) } if let environmentID = activeEnvironment?.id { archivedThreadsByEnvironmentID[environmentID]?.removeAll { ($0.wireID ?? $0.id) == thread.id } } if let index = threads.firstIndex(where: { $0.id == thread.id }) { threads[index] = thread } else { threads.append(thread) } case let .threadRemoved(_, threadID): let uiThreadID = activeEnvironment.map { FeatureScopedID.thread(environmentID: $0.id, wireID: threadID) } changedThreadID = uiThreadID shouldRefreshArchived = true threads.removeAll { $0.id == threadID } if let uiThreadID { latestDetails[uiThreadID] = nil } if activeThreadID == uiThreadID { resetDetailRefresh() activeThreadID = nil activeThreadEnvironmentID = nil } case .snapshot, .synchronized: return } let shell = OrchestrationShellSnapshot( snapshotSequence: sequence, projects: projects, threads: threads, updatedAt: current.updatedAt ) latestShell = shell await emitSnapshot(shell) if shouldRefreshArchived, let environment = activeEnvironment { scheduleArchivedRefresh(client: client, environment: environment) } if let changedThreadID, activeThreadID == changedThreadID { scheduleDetailRefresh(threadID: changedThreadID, client: client) } } private func scheduleDetailRefresh(threadID: String, client: T3Client) { guard activeThreadID == threadID, activeThreadEnvironmentID == activeEnvironment?.id else { return } guard detailRefreshTask == nil else { detailRefreshPending = true return } detailRefreshPending = false detailRefreshGeneration &+= 1 let generation = detailRefreshGeneration let sessionGeneration = environmentGeneration detailRefreshTask = Task { [weak self] in do { try await Task.sleep(for: .milliseconds(160)) } catch { self?.finishDetailRefresh(generation: generation, client: client) return } guard let self else { return } if !Task.isCancelled, self.activeThreadID == threadID, self.isCurrentSession(client: client, generation: sessionGeneration) { try? await self.refreshThread(id: threadID, client: client) } self.finishDetailRefresh(generation: generation, client: client) } } /// Passive environments intentionally avoid full shell WebSocket streams. /// A selected passive thread is still live enough to drive remotely. private func startPassiveDetailPolling(_ route: NativeThreadRoute) { passiveDetailPollingTask?.cancel() passiveDetailPollingTask = nil guard route.environmentID != activeEnvironment?.id else { return } let generation = environmentGeneration passiveDetailPollingTask = Task { [weak self] in while !Task.isCancelled { do { try await Task.sleep(for: .seconds(2)) } catch { return } guard let self, self.activeThreadID == route.uiID, self.isKnownClient( route.client, environmentID: route.environmentID, generation: generation ) else { return } try? await self.refreshThread(id: route.uiID, client: route.client) } } } private func finishDetailRefresh(generation: Int, client: T3Client) { guard detailRefreshGeneration == generation else { return } detailRefreshTask = nil let needsTrailingRefresh = detailRefreshPending detailRefreshPending = false if needsTrailingRefresh, let threadID = activeThreadID { scheduleDetailRefresh(threadID: threadID, client: client) } } private func resetDetailRefresh() { detailRefreshGeneration &+= 1 detailRefreshTask?.cancel() detailRefreshTask = nil detailRefreshPending = false } private func loadEnvironmentShells( _ environments: [Environment] ) async -> [EnvironmentShellLoad] { var clients: [(environment: Environment, client: T3Client)] = [] clients.reserveCapacity(environments.count) for environment in environments { clients.append( (environment, await runtime.client(for: environment)) ) } return await withTaskGroup(of: EnvironmentShellLoad.self) { group in for pair in clients { group.addTask { EnvironmentShellLoad( environment: pair.environment, client: pair.client, shell: try? await pair.client.shellSnapshot() ) } } var loads: [EnvironmentShellLoad] = [] loads.reserveCapacity(environments.count) for await load in group { loads.append(load) } return loads } } /// Successful reads replace that environment's cache. Failed reads leave /// its last-known rows intact, so one offline machine cannot empty home. private func reconcileEnvironmentLoads( _ loads: [EnvironmentShellLoad], savedEnvironments: [Environment] ) { let savedIDs = Set(savedEnvironments.map(\.id)) environmentClients = environmentClients.filter { savedIDs.contains($0.key) } shellsByEnvironmentID = shellsByEnvironmentID.filter { savedIDs.contains($0.key) } archivedThreadsByEnvironmentID = archivedThreadsByEnvironmentID.filter { savedIDs.contains($0.key) } environmentConnectionStates = environmentConnectionStates.filter { savedIDs.contains($0.key) } environmentConnectionDetails = environmentConnectionDetails.filter { savedIDs.contains($0.key) } for load in loads { environmentClients[load.environment.id] = load.client if let shell = load.shell { shellsByEnvironmentID[load.environment.id] = shell environmentConnectionStates[load.environment.id] = .connected environmentConnectionDetails[load.environment.id] = nil } else { environmentConnectionStates[load.environment.id] = .disconnected environmentConnectionDetails[load.environment.id] = "That server is currently unreachable." } } rebuildEntityIndexes(savedEnvironments) } private func rebuildEntityIndexes(_ environments: [Environment]) { let savedIDs = Set(environments.map(\.id)) provisionalThreadRoutes = provisionalThreadRoutes.filter { savedIDs.contains($0.value.environmentID) } var nextProjectEnvironments: [String: String] = [:] var nextProjectWireIDs: [String: String] = [:] var nextThreadEnvironments: [String: String] = [:] var nextThreadWireIDs: [String: String] = [:] var projectCandidates: [String: Set] = [:] var threadCandidates: [String: Set] = [:] var materializedThreadIDs: Set = [] for environment in environments { let environmentID = environment.id for project in shellsByEnvironmentID[environmentID]?.projects ?? [] { let uiID = FeatureScopedID.project( environmentID: environmentID, wireID: project.id ) nextProjectEnvironments[uiID] = environmentID nextProjectWireIDs[uiID] = project.id projectCandidates[project.id, default: []].insert( EntityWireOwner(environmentID: environmentID, wireID: project.id) ) } for thread in shellsByEnvironmentID[environmentID]?.threads ?? [] { let uiID = FeatureScopedID.thread( environmentID: environmentID, wireID: thread.id ) nextThreadEnvironments[uiID] = environmentID nextThreadWireIDs[uiID] = thread.id materializedThreadIDs.insert(uiID) threadCandidates[thread.id, default: []].insert( EntityWireOwner(environmentID: environmentID, wireID: thread.id) ) } for thread in archivedThreadsByEnvironmentID[environmentID] ?? [] { let wireID = thread.wireID ?? thread.id let uiID = FeatureScopedID.thread( environmentID: environmentID, wireID: wireID ) nextThreadEnvironments[uiID] = environmentID nextThreadWireIDs[uiID] = wireID materializedThreadIDs.insert(uiID) threadCandidates[wireID, default: []].insert( EntityWireOwner(environmentID: environmentID, wireID: wireID) ) } } provisionalThreadRoutes = provisionalThreadRoutes.filter { !materializedThreadIDs.contains($0.key) } for (uiID, provisional) in provisionalThreadRoutes { nextThreadEnvironments[uiID] = provisional.environmentID nextThreadWireIDs[uiID] = provisional.wireID threadCandidates[provisional.wireID, default: []].insert( EntityWireOwner( environmentID: provisional.environmentID, wireID: provisional.wireID ) ) } // Raw IDs remain accepted for source-compatible fixtures only when // their owner is unambiguous. Native snapshots always use scoped IDs. for (rawID, candidates) in projectCandidates where candidates.count == 1 { guard let owner = candidates.first else { continue } nextProjectEnvironments[rawID] = owner.environmentID nextProjectWireIDs[rawID] = owner.wireID } for (rawID, candidates) in threadCandidates where candidates.count == 1 { guard let owner = candidates.first else { continue } nextThreadEnvironments[rawID] = owner.environmentID nextThreadWireIDs[rawID] = owner.wireID } projectEnvironmentIDs = nextProjectEnvironments projectWireIDs = nextProjectWireIDs threadEnvironmentIDs = nextThreadEnvironments threadWireIDs = nextThreadWireIDs } private func refresh(client: T3Client, includeArchived: Bool = false) async throws { let environment = client.environment let generation = environmentGeneration let shell = try await client.shellSnapshot() guard isKnownClient(client, environmentID: environment.id, generation: generation) else { throw CancellationError() } shellsByEnvironmentID[environment.id] = shell if activeEnvironment?.id == environment.id { latestShell = shell } rebuildEntityIndexes( (try? await runtime.environments()) ?? [environment] ) if includeArchived, let archivedShell = try? await client.archivedShellSnapshot(), isKnownClient(client, environmentID: environment.id, generation: generation) { archivedThreadsByEnvironmentID[environment.id] = archivedShell.threads.map { mapThread($0, environment: environment) } rebuildEntityIndexes((try? await runtime.environments()) ?? [environment]) } await emitSnapshot(shell, environment: environment) } private func scheduleArchivedRefresh(client: T3Client, environment: Environment) { archivedRefreshTask?.cancel() let generation = environmentGeneration archivedRefreshTask = Task { [weak self] in guard let self, let archivedShell = try? await client.archivedShellSnapshot(), !Task.isCancelled, self.isCurrentSession(client: client, generation: generation) else { return } self.archivedThreadsByEnvironmentID[environment.id] = archivedShell.threads.map { self.mapThread($0, environment: environment) } self.rebuildEntityIndexes( (try? await self.runtime.environments()) ?? [environment] ) if let shell = self.latestShell { await self.emitSnapshot(shell) } } } private func refreshThread(id: String, client: T3Client) async throws { let route = try threadRoute(for: id) guard route.client === client else { throw NativeFeatureClientError.threadNotFound } let environment = route.client.environment let generation = environmentGeneration let snapshot = try await client.threadSnapshot(id: route.wireID) guard isKnownClient(client, environmentID: environment.id, generation: generation) else { throw CancellationError() } let detail = mapDetail(snapshot.thread, environment: environment) if latestDetails[route.uiID] != detail { latestDetails[route.uiID] = detail continuation.yield(.detail(detail)) } let hydrationBase = latestDetails[route.uiID] ?? detail let hydrated = await hydratedAttachmentURLs( in: hydrationBase, client: client, environmentID: environment.id, generation: generation ) guard isKnownClient(client, environmentID: environment.id, generation: generation), latestDetails[route.uiID] == hydrationBase, hydrated != hydrationBase else { return } latestDetails[route.uiID] = hydrated continuation.yield(.detail(hydrated)) } private func emitSnapshot( _ shell: OrchestrationShellSnapshot, environment sourceEnvironment: Environment? = nil ) async { guard let environment = activeEnvironment else { return } let sourceEnvironment = sourceEnvironment ?? environment let generation = environmentGeneration let environments = (try? await runtime.environments()) ?? [environment] guard generation == environmentGeneration, activeEnvironment?.id == environment.id else { return } shellsByEnvironmentID[sourceEnvironment.id] = shell environmentConnectionStates[sourceEnvironment.id] = .connected environmentConnectionDetails[sourceEnvironment.id] = nil if sourceEnvironment.id == environment.id { latestShell = shell } rebuildEntityIndexes(environments) let connectionState: FeatureConnection.State let connectionDetail: String? if sourceEnvironment.id == environment.id { connectionState = .connected connectionDetail = nil } else { connectionState = latestSnapshot?.connection.state ?? environmentConnectionStates[environment.id] ?? .disconnected connectionDetail = latestSnapshot?.connection.detail } let snapshot = makeSnapshot( environments: environments, activeEnvironment: environment, connectionState: connectionState, connectionDetail: connectionDetail ) guard latestSnapshot != snapshot else { return } latestSnapshot = snapshot continuation.yield(.snapshot(snapshot)) } private func disconnectedSnapshot( environments: [Environment], detail: String? = nil ) -> FeatureSnapshot { FeatureSnapshot( connection: .init(state: .disconnected, detail: detail), environments: environments.map { mapEnvironment($0, activeID: nil) }, settings: loadSettings() ) } private func emitConnection( _ state: FeatureConnection.State, detail: String? = nil ) { guard let environment = activeEnvironment else { return } environmentConnectionStates[environment.id] = state environmentConnectionDetails[environment.id] = detail let connection = FeatureConnection( state: state, environmentName: environment.label, endpoint: environment.httpBaseURL.absoluteString, detail: detail ) if var snapshot = latestSnapshot { snapshot.connection = connection if let index = snapshot.environments.firstIndex(where: { $0.id == environment.id }) { snapshot.environments[index].connectionState = state snapshot.environments[index].connectionDetail = detail } latestSnapshot = snapshot } continuation.yield( .connection(connection) ) } private func makeSnapshot( environments: [Environment], activeEnvironment: Environment, connectionState: FeatureConnection.State, connectionDetail: String? = nil ) -> FeatureSnapshot { let threads = environments.flatMap { environment in let live = shellsByEnvironmentID[environment.id]?.threads.map { mapThread($0, environment: environment) } ?? [] let liveIDs = Set(live.map(\.id)) let cached = (archivedThreadsByEnvironmentID[environment.id] ?? []).filter { !liveIDs.contains($0.id) } return live + cached } let threadCountByProjectID = threads.reduce(into: [String: Int]()) { $0[$1.projectID, default: 0] += 1 } let projects = environments.flatMap { environment in (shellsByEnvironmentID[environment.id]?.projects ?? []).map { project in let uiID = FeatureScopedID.project( environmentID: environment.id, wireID: project.id ) return FeatureProject( id: uiID, wireID: project.id, environmentID: environment.id, name: project.title, path: project.workspaceRoot, threadCount: threadCountByProjectID[uiID, default: 0], defaultSelection: project.defaultModelSelection.map(mapSelection) ) } } let activeShell = shellsByEnvironmentID[activeEnvironment.id] return FeatureSnapshot( connection: FeatureConnection( state: connectionState, environmentName: activeEnvironment.label, endpoint: activeEnvironment.httpBaseURL.absoluteString, detail: connectionDetail ), environments: environments.map { mapEnvironment($0, activeID: activeEnvironment.id) }, projects: projects, threads: threads, providers: activeShell.map(mapProviders) ?? [], settings: loadSettings() ) } private func mapEnvironment(_ environment: Environment, activeID: String?) -> FeatureEnvironment { FeatureEnvironment( id: environment.id, name: environment.label, endpoint: environment.httpBaseURL.absoluteString, isActive: environment.id == activeID, connectionState: environmentConnectionStates[environment.id], connectionDetail: environmentConnectionDetails[environment.id] ) } private func mapThread( _ thread: OrchestrationThreadShell, environment: Environment ) -> FeatureThread { FeatureThread( id: FeatureScopedID.thread(environmentID: environment.id, wireID: thread.id), wireID: thread.id, projectID: FeatureScopedID.project( environmentID: environment.id, wireID: thread.projectId ), environmentID: environment.id, environmentName: environment.label, title: thread.title, branch: thread.branch, worktreePath: thread.worktreePath, createdAt: parseDate(thread.createdAt), updatedAt: parseDate(thread.updatedAt), state: mapThreadState( latestTurn: thread.latestTurn, session: thread.session, hasApprovals: thread.hasPendingApprovals, hasUserInput: thread.hasPendingUserInput ), providerID: thread.modelSelection.instanceId, providerName: threadProviderName( session: thread.session, modelSelection: thread.modelSelection ), modelID: thread.modelSelection.model, modelOptions: mapOptionSelections(thread.modelSelection.options), isArchived: thread.archivedAt != nil, isSettled: isSettled(thread.settledOverride, settledAt: thread.settledAt), keepsActive: thread.settledOverride == "active", settledAt: thread.settledAt.map(parseDate), lastActivityAt: lastActivityDate( latestUserMessageAt: thread.latestUserMessageAt, latestTurn: thread.latestTurn ), snoozedUntil: thread.snoozedUntil.map(parseDate), snoozedAt: thread.snoozedAt.map(parseDate), attentionAt: failureDate( latestTurn: thread.latestTurn, session: thread.session ), workingStartedAt: workingStartedAt( latestTurn: thread.latestTurn, session: thread.session ), latestTurnCompletedAt: thread.latestTurn?.completedAt.map(parseDate), runtimeMode: mapRuntimeMode(thread.runtimeMode), interactionMode: mapInteractionMode(thread.interactionMode) ) } private func mapThread( _ thread: OrchestrationThread, environment: Environment ) -> FeatureThread { FeatureThread( id: FeatureScopedID.thread(environmentID: environment.id, wireID: thread.id), wireID: thread.id, projectID: FeatureScopedID.project( environmentID: environment.id, wireID: thread.projectId ), environmentID: environment.id, environmentName: environment.label, title: thread.title, preview: previewText(thread.messages.last?.text), branch: thread.branch, worktreePath: thread.worktreePath, createdAt: parseDate(thread.createdAt), updatedAt: parseDate(thread.updatedAt), state: mapThreadState( latestTurn: thread.latestTurn, session: thread.session, hasApprovals: false, hasUserInput: false ), providerID: thread.modelSelection.instanceId, providerName: threadProviderName( session: thread.session, modelSelection: thread.modelSelection ), modelID: thread.modelSelection.model, modelOptions: mapOptionSelections(thread.modelSelection.options), isArchived: thread.archivedAt != nil, isSettled: isSettled(thread.settledOverride, settledAt: thread.settledAt), keepsActive: thread.settledOverride == "active", settledAt: thread.settledAt.map(parseDate), lastActivityAt: lastActivityDate( latestUserMessageAt: thread.messages.last(where: { $0.role == "user" })?.createdAt, latestTurn: thread.latestTurn ), snoozedUntil: thread.snoozedUntil.map(parseDate), snoozedAt: thread.snoozedAt.map(parseDate), attentionAt: failureDate( latestTurn: thread.latestTurn, session: thread.session ), workingStartedAt: workingStartedAt( latestTurn: thread.latestTurn, session: thread.session ), latestTurnCompletedAt: thread.latestTurn?.completedAt.map(parseDate), runtimeMode: mapRuntimeMode(thread.runtimeMode), interactionMode: mapInteractionMode(thread.interactionMode) ) } private func mapDetail( _ thread: OrchestrationThread, environment: Environment ) -> FeatureThreadDetail { let approvals = pendingApprovals(thread, environment: environment) let userInputs = pendingUserInputs(thread, environment: environment) let messages = thread.messages.map { message in FeatureMessage( id: message.id, role: mapRole(message.role), text: message.text, createdAt: parseDate(message.createdAt), state: message.streaming ? .streaming : .complete, attachments: (message.attachments ?? []).map { FeatureMessageAttachment( id: $0.id, name: $0.name, mimeType: $0.mimeType, sizeBytes: $0.sizeBytes, url: cachedAttachmentURL( for: $0.id, environmentID: environment.id ) ) } ) } let errors = thread.activities.compactMap { activity -> FeatureMessage? in guard activity.tone == "error" else { return nil } let detail = activity.payload["detail"]?.stringValue let text = detail.map { "\(activity.summary)\n\($0)" } ?? activity.summary return FeatureMessage( id: "activity-\(activity.id)", role: activity.tone == "error" ? .system : .tool, text: text, createdAt: parseDate(activity.createdAt), state: .complete, toolName: activity.kind ) } let mergedMessages = (messages + errors + collapsedWorkLogs(thread.activities)) .sorted { $0.createdAt < $1.createdAt } let mappedThread = FeatureThread( id: FeatureScopedID.thread(environmentID: environment.id, wireID: thread.id), wireID: thread.id, projectID: FeatureScopedID.project( environmentID: environment.id, wireID: thread.projectId ), environmentID: environment.id, environmentName: environment.label, title: thread.title, preview: previewText(thread.messages.last?.text), branch: thread.branch, worktreePath: thread.worktreePath, createdAt: parseDate(thread.createdAt), updatedAt: parseDate(thread.updatedAt), state: mapThreadState( latestTurn: thread.latestTurn, session: thread.session, hasApprovals: !approvals.isEmpty, hasUserInput: !userInputs.isEmpty ), providerID: thread.modelSelection.instanceId, providerName: threadProviderName( session: thread.session, modelSelection: thread.modelSelection ), modelID: thread.modelSelection.model, modelOptions: mapOptionSelections(thread.modelSelection.options), isArchived: thread.archivedAt != nil, isSettled: isSettled(thread.settledOverride, settledAt: thread.settledAt), keepsActive: thread.settledOverride == "active", settledAt: thread.settledAt.map(parseDate), lastActivityAt: lastActivityDate( latestUserMessageAt: thread.messages.last(where: { $0.role == "user" })?.createdAt, latestTurn: thread.latestTurn ), snoozedUntil: thread.snoozedUntil.map(parseDate), snoozedAt: thread.snoozedAt.map(parseDate), attentionAt: failureDate( latestTurn: thread.latestTurn, session: thread.session ), workingStartedAt: workingStartedAt( latestTurn: thread.latestTurn, session: thread.session ), latestTurnCompletedAt: thread.latestTurn?.completedAt.map(parseDate), runtimeMode: mapRuntimeMode(thread.runtimeMode), interactionMode: mapInteractionMode(thread.interactionMode) ) return FeatureThreadDetail( thread: mappedThread, messages: mergedMessages, approvals: approvals, userInputs: userInputs ) } /// Lifecycle updates can number in the thousands on a long turn. Keep the /// primary transcript message-sized while preserving a bounded, expandable /// summary for each turn. private func collapsedWorkLogs( _ activities: [OrchestrationActivity] ) -> [FeatureMessage] { let terminalKinds = Set(["tool.completed", "task.completed", "turn.plan.updated"]) let completed = activities.filter { $0.tone != "error" && terminalKinds.contains($0.kind) } let groups = Dictionary(grouping: completed) { activity in activity.turnId ?? "unscoped" } return groups.values.compactMap { unsorted in let group = unsorted.sorted { $0.createdAt < $1.createdAt } guard let last = group.last else { return nil } let visible = group.suffix(40) var lines: [String] = [] let hiddenCount = group.count - visible.count if hiddenCount > 0 { lines.append("\(hiddenCount) earlier updates hidden") } lines.append( contentsOf: visible.map { activity in let detail = previewText(activity.payload["detail"]?.stringValue) return "• \(detail ?? activity.summary)" } ) return FeatureMessage( id: "work-log-\(last.turnId ?? last.id)", role: .tool, text: lines.joined(separator: "\n"), createdAt: parseDate(last.createdAt), state: .complete, toolName: "Work log · \(group.count)" ) } } private func pendingApprovals( _ thread: OrchestrationThread, environment: Environment ) -> [FeatureApproval] { var open: [String: FeatureApproval] = [:] let threadID = FeatureScopedID.thread( environmentID: environment.id, wireID: thread.id ) for activity in thread.activities.sorted(by: { parseDate($0.createdAt) < parseDate($1.createdAt) }) { let requestID = activity.payload["requestId"]?.stringValue let uiRequestID = requestID.map { FeatureScopedID.approval(environmentID: environment.id, wireID: $0) } if activity.kind == "approval.requested", let requestID { let requestKind = activity.payload["requestKind"]?.stringValue let kind: FeatureApprovalKind = switch requestKind { case "command": .command case "file-read": .fileRead case "file-change": .fileChange default: .other } let detail = activity.payload["detail"]?.stringValue ?? activity.summary let uiRequestID = FeatureScopedID.approval( environmentID: environment.id, wireID: requestID ) open[uiRequestID] = FeatureApproval( id: uiRequestID, wireID: requestID, threadID: threadID, kind: kind, title: activity.summary, detail: detail ) approvalRoutes[uiRequestID] = PendingRequestRoute( threadID: threadID, wireID: requestID ) } else if activity.kind == "approval.resolved", let uiRequestID { open[uiRequestID] = nil approvalRoutes[uiRequestID] = nil } else if activity.kind == "provider.approval.respond.failed", let uiRequestID { let detail = activity.payload["detail"]?.stringValue?.lowercased() ?? "" if detail.contains("stale") || detail.contains("unknown") { open[uiRequestID] = nil approvalRoutes[uiRequestID] = nil } } } return open.values.sorted { $0.id < $1.id } } private func pendingUserInputs( _ thread: OrchestrationThread, environment: Environment ) -> [FeatureUserInput] { var open: [String: FeatureUserInput] = [:] let threadID = FeatureScopedID.thread( environmentID: environment.id, wireID: thread.id ) for activity in thread.activities.sorted(by: { parseDate($0.createdAt) < parseDate($1.createdAt) }) { let requestID = activity.payload["requestId"]?.stringValue let uiRequestID = requestID.map { FeatureScopedID.input(environmentID: environment.id, wireID: $0) } if activity.kind == "user-input.requested", let requestID, let questions = parseInputQuestions(activity.payload), !questions.isEmpty { let uiRequestID = FeatureScopedID.input( environmentID: environment.id, wireID: requestID ) open[uiRequestID] = FeatureUserInput( id: uiRequestID, wireID: requestID, threadID: threadID, questions: questions ) inputRoutes[uiRequestID] = PendingRequestRoute( threadID: threadID, wireID: requestID ) } else if activity.kind == "user-input.resolved", let uiRequestID { open[uiRequestID] = nil inputRoutes[uiRequestID] = nil } else if activity.kind == "provider.user-input.respond.failed", let uiRequestID { let detail = activity.payload["detail"]?.stringValue?.lowercased() ?? "" if detail.contains("stale") || detail.contains("unknown") { open[uiRequestID] = nil inputRoutes[uiRequestID] = nil } } } return open.values.sorted { $0.id < $1.id } } private func parseInputQuestions(_ payload: JSONValue) -> [FeatureInputQuestion]? { guard case let .array(rawQuestions)? = payload["questions"] else { return nil } return rawQuestions.compactMap { rawQuestion in guard case let .object(question) = rawQuestion, let id = question["id"]?.stringValue, let header = question["header"]?.stringValue, let text = question["question"]?.stringValue else { return nil } let options: [FeatureInputOption] if case let .array(rawOptions)? = question["options"] { options = rawOptions.compactMap { rawOption in guard case let .object(option) = rawOption, let label = option["label"]?.stringValue else { return nil } return FeatureInputOption( label: label, detail: option["description"]?.stringValue ?? "" ) } } else { options = [] } let allowsMultiple: Bool if case let .bool(value)? = question["multiSelect"] { allowsMultiple = value } else { allowsMultiple = false } return FeatureInputQuestion( id: id, header: header, question: text, options: options, allowsMultiple: allowsMultiple ) } } private func mapThreadState( latestTurn: OrchestrationLatestTurn?, session: OrchestrationSession?, hasApprovals: Bool, hasUserInput: Bool ) -> FeatureThreadState { if hasApprovals { return .waitingForApproval } if hasUserInput { return .waitingForInput } if session?.status == "starting" { return .queued } if session?.status == "running" || latestTurn?.state == "running" { return .working } if session?.status == "error" || latestTurn?.state == "error" { return .failed } if latestTurn?.state == "completed" { return .completed } return .idle } private func mapRole(_ role: String) -> FeatureMessageRole { switch role { case "user": .user case "assistant": .assistant case "system": .system default: .tool } } private func isSettled(_ override: String?, settledAt: String?) -> Bool { if override == "active" { return false } return override == "settled" || settledAt != nil } private func mapRuntimeMode(_ mode: RuntimeMode) -> FeatureRuntimeMode { switch mode { case .approvalRequired: .approvalRequired case .autoAcceptEdits: .autoAcceptEdits case .auto: .automatic case .fullAccess: .fullAccess } } private func coreRuntimeMode(_ mode: FeatureRuntimeMode) -> RuntimeMode { switch mode { case .approvalRequired: .approvalRequired case .autoAcceptEdits: .autoAcceptEdits case .automatic: .auto case .fullAccess: .fullAccess } } private func mapInteractionMode(_ mode: InteractionMode) -> FeatureInteractionMode { mode == .plan ? .plan : .standard } private func coreInteractionMode(_ mode: FeatureInteractionMode) -> InteractionMode { mode == .plan ? .plan : .default } private func mapProviders(_ shell: OrchestrationShellSnapshot) -> [FeatureProvider] { if let providers = latestServerConfig?.providers, !providers.isEmpty { return providers.map { provider in FeatureProvider( id: provider.instanceId, name: provider.displayName ?? providerDisplayName(provider.driver), isAvailable: provider.enabled && provider.installed && provider.status != "disabled" && provider.status != "error" && provider.auth.status != "unauthenticated" && provider.availability != "unavailable", driver: provider.driver, requiresNewThreadForModelChange: provider.requiresNewThreadForModelChange ?? false, models: provider.models.map { model in let options = (model.capabilities?.optionDescriptors ?? []) .map(mapOptionDescriptor) return FeatureModel( id: model.slug, name: model.name, detail: model.subProvider ?? model.shortName, supportsReasoning: options.contains { descriptor in let searchable = "\(descriptor.id) \(descriptor.label)".lowercased() return searchable.contains("reason") || searchable.contains("effort") || searchable.contains("thinking") }, isDefault: model.isDefault ?? false, options: options ) } ) } } var modelsByProvider: [String: Set] = [:] for selection in shell.projects.compactMap(\.defaultModelSelection) + shell.threads.map(\.modelSelection) { modelsByProvider[selection.instanceId, default: []].insert(selection.model) } if modelsByProvider.isEmpty { modelsByProvider["codex"] = ["gpt-5.6-sol"] } return modelsByProvider.keys.sorted().map { providerID in FeatureProvider( id: providerID, name: providerDisplayName(providerID), driver: providerID, models: (modelsByProvider[providerID] ?? []).sorted().map { FeatureModel(id: $0, name: $0) } ) } } private func modelSelection( _ selection: FeatureSelection?, projectID: String, shell: OrchestrationShellSnapshot? ) -> ModelSelection { if let selection { return coreModelSelection(selection) } if let projectDefault = shell?.projects .first(where: { $0.id == projectID })? .defaultModelSelection { return projectDefault } return defaultModelSelection() } private func defaultModelSelection() -> ModelSelection { if let selection = loadSettings().defaultSelection { return coreModelSelection(selection) } return ModelSelection(instanceId: "codex", model: "gpt-5.6-sol") } private func coreModelSelection(_ selection: FeatureSelection) -> ModelSelection { let options = selection.options.map { option in ModelSelection.OptionSelection( id: option.id, value: coreOptionValue(option.value) ) } return ModelSelection( instanceId: selection.providerID, model: selection.modelID, options: options.isEmpty ? nil : options ) } private func mapSelection(_ selection: ModelSelection) -> FeatureSelection { FeatureSelection( providerID: selection.instanceId, modelID: selection.model, options: mapOptionSelections(selection.options) ) } private func coreOptionValue(_ value: FeatureModelOptionValue) -> JSONValue { switch value { case let .string(rawValue): return .string(rawValue) case let .boolean(rawValue): return .bool(rawValue) } } private func mapOptionSelections( _ selections: [ModelSelection.OptionSelection]? ) -> [FeatureModelOptionSelection] { (selections ?? []).compactMap { selection in let value: FeatureModelOptionValue switch selection.value { case let .string(rawValue): value = .string(rawValue) case let .bool(rawValue): value = .boolean(rawValue) default: return nil } return FeatureModelOptionSelection(id: selection.id, value: value) } } private func mapOptionDescriptor( _ descriptor: ServerProviderOptionDescriptor ) -> FeatureModelOptionDescriptor { switch descriptor { case let .select(value): let defaultValue = value.currentValue ?? value.options.first(where: { $0.isDefault == true })?.id return FeatureModelOptionDescriptor( id: value.id, label: value.label, detail: value.description, kind: .select, choices: value.options.map { FeatureModelOptionChoice( id: $0.id, label: $0.label, detail: $0.description, isDefault: $0.isDefault ?? false ) }, defaultValue: defaultValue.map(FeatureModelOptionValue.string) ) case let .boolean(value): return FeatureModelOptionDescriptor( id: value.id, label: value.label, detail: value.description, kind: .boolean, defaultValue: value.currentValue.map(FeatureModelOptionValue.boolean) ) } } private func providerDisplayName(_ id: String) -> String { switch id { case "codex": "Codex" case "claudeAgent", "claude": "Claude" case "cursor": "Cursor" case "grok": "Grok" case "opencode": "OpenCode" default: id } } private func threadProviderName( session: OrchestrationSession?, modelSelection: ModelSelection ) -> String { if let name = session?.providerName?.trimmingCharacters(in: .whitespacesAndNewlines), !name.isEmpty { return name } let providerID = session?.providerInstanceId ?? modelSelection.instanceId if let provider = latestServerConfig?.providers.first(where: { $0.instanceId == providerID }) { return provider.displayName ?? providerDisplayName(provider.driver) } return providerDisplayName(providerID) } private func cachedAttachmentURL( for id: String, environmentID: String? = nil ) -> URL? { guard let environmentID = environmentID ?? activeEnvironment?.id else { return nil } let key = AttachmentCacheKey(environmentID: environmentID, attachmentID: id) guard let cached = attachmentURLs[key] else { return nil } guard cached.expiresAt > Date().addingTimeInterval(30) else { attachmentURLs[key] = nil return nil } return cached.url } private func scheduleAttachmentHydration( in detail: FeatureThreadDetail, threadID: String, client: T3Client, environmentID: String ) { attachmentHydrationTasks[threadID]?.task.cancel() let generation = environmentGeneration let workID = UUID() let task = Task { [weak self] in guard let self else { return } let hydrated = await self.hydratedAttachmentURLs( in: detail, client: client, environmentID: environmentID, generation: generation ) guard self.isKnownClient( client, environmentID: environmentID, generation: generation ), self.latestDetails[threadID] == detail, hydrated != detail else { self.finishAttachmentHydration(threadID: threadID, workID: workID) return } self.latestDetails[threadID] = hydrated self.continuation.yield(.detail(hydrated)) self.finishAttachmentHydration(threadID: threadID, workID: workID) } attachmentHydrationTasks[threadID] = (workID, task) } private func finishAttachmentHydration(threadID: String, workID: UUID) { guard attachmentHydrationTasks[threadID]?.id == workID else { return } attachmentHydrationTasks[threadID] = nil } private func hydratedAttachmentURLs( in detail: FeatureThreadDetail, client: T3Client, environmentID: String, generation: Int ) async -> FeatureThreadDetail { guard isKnownClient(client, environmentID: environmentID, generation: generation) else { return detail } let imageIDs = Set( detail.messages.flatMap(\.attachments) .filter { $0.mimeType.hasPrefix("image/") } .map(\.id) ) let missingIDs = Array(imageIDs.filter { cachedAttachmentURL(for: $0, environmentID: environmentID) == nil }) await withTaskGroup(of: (String, ResolvedAssetURL?).self) { group in var iterator = missingIDs.makeIterator() for _ in 0.. Date? { [ latestUserMessageAt, latestTurn?.requestedAt, latestTurn?.startedAt, latestTurn?.completedAt, ] .compactMap { $0.flatMap(parseValidDate) } .max() } private func failureDate( latestTurn: OrchestrationLatestTurn?, session: OrchestrationSession? ) -> Date? { guard session?.status == "error" || latestTurn?.state == "error" else { return nil } return [ session?.updatedAt, latestTurn?.completedAt, latestTurn?.startedAt, latestTurn?.requestedAt, ] .compactMap { $0.flatMap(parseValidDate) } .max() } private func workingStartedAt( latestTurn: OrchestrationLatestTurn?, session: OrchestrationSession? ) -> Date? { guard session?.status == "starting" || session?.status == "running" || latestTurn?.state == "running" else { return nil } let candidates: [String?] if let latestTurn, latestTurn.completedAt == nil { candidates = [ latestTurn.startedAt, latestTurn.requestedAt, session?.updatedAt, ] } else { candidates = [session?.updatedAt] } return candidates.lazy.compactMap { $0.flatMap(self.parseValidDate) }.first } private func makeUploadAttachments( _ attachments: [FeatureUploadAttachment] ) throws -> [UploadChatImageAttachment] { guard attachments.count <= 8 else { throw NativeFeatureClientError.tooManyAttachments } return try attachments.map { try UploadChatImageAttachment( data: $0.data, name: $0.name, mimeType: $0.mimeType ) } } private func requireScope(_ scope: String, client: T3Client) async throws { let session = try await client.authSession() guard session.scopes?.contains(scope) == true else { throw NativeFeatureClientError.missingScope(scope) } } private static func title(from prompt: String, hasAttachments: Bool) -> String { let compact = prompt .split(whereSeparator: \.isWhitespace) .joined(separator: " ") guard !compact.isEmpty else { return hasAttachments ? "Image task" : "New thread" } guard compact.count > 72 else { return compact } return "\(compact.prefix(69).trimmingCharacters(in: .whitespacesAndNewlines))..." } private func previewText(_ text: String?) -> String? { guard let text else { return nil } let compact = text.split(whereSeparator: \.isWhitespace).joined(separator: " ") guard !compact.isEmpty else { return nil } return compact.count > 160 ? "\(compact.prefix(157))..." : compact } private func loadSettings() -> FeatureSettings { guard let data = settingsStore.data(forKey: Self.settingsKey), let settings = try? JSONDecoder().decode(FeatureSettings.self, from: data) else { return FeatureSettings() } return settings } private func parseDate(_ value: String) -> Date { parseValidDate(value) ?? .distantPast } private func parseValidDate(_ value: String) -> Date? { Self.fractionalDateFormatter.date(from: value) ?? Self.dateFormatter.date(from: value) } private static let settingsKey = "swift-ios.feature-settings.v1" private static let fractionalDateFormatter: ISO8601DateFormatter = { let formatter = ISO8601DateFormatter() formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds] return formatter }() private static let dateFormatter = ISO8601DateFormatter() } private struct AttachmentCacheKey: Hashable { let environmentID: String let attachmentID: String } private struct CachedAttachmentURL { let url: URL let expiresAt: Date } private struct EnvironmentShellLoad: Sendable { let environment: Environment let client: T3Client let shell: OrchestrationShellSnapshot? } private struct EntityWireOwner: Hashable { let environmentID: String let wireID: String } private struct NativeProjectRoute { let uiID: String let wireID: String let environmentID: String let client: T3Client } private struct NativeThreadRoute { let uiID: String let wireID: String let environmentID: String let client: T3Client } private struct ProvisionalThreadRoute { let environmentID: String let wireID: String } private struct PendingRequestRoute { let threadID: String let wireID: String } private struct CommandIdentity: Equatable { let commandID: String let messageID: String let createdAt: String init( commandID: String = UUID().uuidString, messageID: String = UUID().uuidString, createdAt: String = OrchestrationCommands.now() ) { self.commandID = commandID self.messageID = messageID self.createdAt = createdAt } } private struct BootstrapSubmissionSignature: Equatable { let projectID: String let prompt: String let model: ModelSelection let runtimeMode: RuntimeMode let interactionMode: InteractionMode let attachments: [FeatureUploadAttachment] } private struct PendingBootstrapSubmission { let signature: BootstrapSubmissionSignature let threadID: String let identity: CommandIdentity } private struct TurnSubmissionSignature: Equatable { let text: String let model: ModelSelection? let runtimeMode: RuntimeMode let interactionMode: InteractionMode let attachments: [FeatureUploadAttachment] } private struct PendingTurnSubmission { let signature: TurnSubmissionSignature let identity: CommandIdentity } private enum NativeFeatureClientError: LocalizedError { case notConnected case projectNotFound case threadNotFound case workspaceNotFound case approvalNotFound case inputRequestNotFound case invalidProjectPath case terminalNotOpen case deviceSessionNotFound case missingScope(String) case tooManyAttachments var errorDescription: String? { switch self { case .notConnected: "Connect to a T3 environment first." case .projectNotFound: "The selected project is no longer available." case .threadNotFound: "The selected thread is no longer available." case .workspaceNotFound: "The thread workspace is no longer available." case .approvalNotFound: "The approval request is no longer active." case .inputRequestNotFound: "The input request is no longer active." case .invalidProjectPath: "Enter a workspace path on the connected environment." case .terminalNotOpen: "Open the terminal before sending input." case .deviceSessionNotFound: "That device session is no longer active." case .missingScope: "This connection does not have permission to manage devices." case .tooManyAttachments: "You can attach up to 8 images per message." } } }