diff --git a/apps/swift-ios/App/NativeFeatureClient.swift b/apps/swift-ios/App/NativeFeatureClient.swift index 845a423689..f957f5d6bc 100644 --- a/apps/swift-ios/App/NativeFeatureClient.swift +++ b/apps/swift-ios/App/NativeFeatureClient.swift @@ -25,7 +25,11 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { 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? @@ -39,8 +43,8 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { private var attachmentHydrationTasks: [ String: (id: UUID, task: Task) ] = [:] - private var approvalThreadIDs: [String: String] = [:] - private var inputThreadIDs: [String: String] = [:] + private var approvalRoutes: [String: PendingRequestRoute] = [:] + private var inputRoutes: [String: PendingRequestRoute] = [:] private var terminalIDs: [String: String] = [:] private var terminalSnapshots: [String: FeatureTerminalSnapshot] = [:] private var terminalContinuations: [ @@ -52,6 +56,7 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { 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 @@ -75,6 +80,7 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { aggregateRefreshTask?.cancel() archivedRefreshTask?.cancel() detailRefreshTask?.cancel() + passiveDetailPollingTask?.cancel() attachmentHydrationTasks.values.forEach { $0.task.cancel() } continuation.finish() } @@ -167,11 +173,13 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { 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 @@ -189,11 +197,13 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { 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 @@ -216,7 +226,11 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { environmentClients.removeAll() shellsByEnvironmentID.removeAll() archivedThreadsByEnvironmentID.removeAll() + projectEnvironmentIDs.removeAll() + projectWireIDs.removeAll() threadEnvironmentIDs.removeAll() + threadWireIDs.removeAll() + provisionalThreadRoutes.removeAll() environmentConnectionStates.removeAll() environmentConnectionDetails.removeAll() } @@ -227,8 +241,8 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { attachmentURLs.removeAll() pendingBootstrapSubmission = nil pendingTurnSubmissions.removeAll() - approvalThreadIDs.removeAll() - inputThreadIDs.removeAll() + approvalRoutes.removeAll() + inputRoutes.removeAll() finishTerminalStreams() terminalIDs.removeAll() terminalSnapshots.removeAll() @@ -270,39 +284,52 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { title: String?, selection: FeatureSelection? ) async throws -> FeatureThread { - let client = try requireClient() + 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: projectID) + 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: projectID, + projectID: route.wireID, title: threadTitle, model: model, runtimeMode: .fullAccess ) - let shell = try await client.shellSnapshot() - guard isCurrentSession(client: client, generation: generation) else { + guard isKnownClient(client, environmentID: environment.id, generation: generation) else { throw CancellationError() } - guard let environment = activeEnvironment else { - throw NativeFeatureClientError.notConnected + 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) + } } - latestShell = shell - shellsByEnvironmentID[environment.id] = shell - if let created = shell.threads.first(where: { $0.id == threadID }) { - let mapped = mapThread(created, environment: environment) - await emitSnapshot(shell) - return mapped - } - await emitSnapshot(shell, environment: environment) return FeatureThread( - id: threadID, - projectID: projectID, - environmentID: activeEnvironment?.id, - environmentName: activeEnvironment?.label, + 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), @@ -318,9 +345,15 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { interactionMode: FeatureInteractionMode, attachments: [FeatureUploadAttachment] ) async throws -> FeatureThread { - let client = try requireClient() + let route = try projectRoute(for: projectID) + let client = route.client + let environment = client.environment let generation = environmentGeneration - let model = modelSelection(selection, projectID: projectID) + 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) @@ -349,7 +382,7 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { do { _ = try await client.createThreadAndSend( threadID: pending.threadID, - projectID: projectID, + projectID: route.wireID, title: title, text: prompt, model: model, @@ -368,7 +401,7 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { guard try await recoverBootstrap( client: client, pending: pending, - projectID: projectID, + projectID: route.wireID, text: prompt, model: model, runtimeMode: runtime, @@ -379,7 +412,8 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { } } - guard isCurrentSession(client: client, generation: generation) else { + registerProvisionalThread(wireID: pending.threadID, environmentID: environment.id) + guard isKnownClient(client, environmentID: environment.id, generation: generation) else { throw CancellationError() } if pendingBootstrapSubmission?.identity == pending.identity { @@ -388,24 +422,31 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { // 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 isCurrentSession(client: client, generation: generation) else { + guard isKnownClient(client, environmentID: environment.id, generation: generation) else { throw CancellationError() } - guard let environment = activeEnvironment else { - throw NativeFeatureClientError.notConnected - } - latestShell = shell shellsByEnvironmentID[environment.id] = shell - await emitSnapshot(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: pending.threadID, - projectID: projectID, - environmentID: activeEnvironment?.id, - environmentName: activeEnvironment?.label, + 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), @@ -465,81 +506,119 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { } func renameThread(id: String, title: String) async throws { - let client = try requireClient(forThreadID: id) - _ = try await client.rename(threadID: id, title: title) - try await refresh(client: client) + 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 client = try requireClient(forThreadID: id) - _ = try await client.archive(threadID: id, archived: archived) - try await refresh(client: client, includeArchived: true) + 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 client = try requireClient(forThreadID: id) - _ = try await client.settle(threadID: id, settled: settled) - try await refresh(client: client) + 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 client = try requireClient(forThreadID: id) - _ = try await client.snooze(threadID: id, until: until) - try await refresh(client: client) + 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 client = try requireClient(forThreadID: id) - _ = try await client.setRuntimeMode(threadID: id, mode: coreRuntimeMode(mode)) - try await refresh(client: client) - if activeThreadID == id { - try await refreshThread(id: id, client: client) + 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 client = try requireClient(forThreadID: id) - _ = try await client.setInteractionMode(threadID: id, mode: coreInteractionMode(mode)) - try await refresh(client: client) - if activeThreadID == id { - try await refreshThread(id: id, client: client) + 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 client = try requireClient(forThreadID: id) - _ = try await client.delete(threadID: id) - if activeThreadID == id { + 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[id] = nil - try await refresh(client: client, includeArchived: true) + 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 client = try requireClient(forThreadID: id) + let route = try threadRoute(for: id) + let client = route.client let environment = client.environment let generation = environmentGeneration resetDetailRefresh() - activeThreadID = id + passiveDetailPollingTask?.cancel() + passiveDetailPollingTask = nil + activeThreadID = route.uiID activeThreadEnvironmentID = environment.id - let snapshot = try await client.threadSnapshot(id: 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[id] = detail + latestDetails[route.uiID] = detail scheduleAttachmentHydration( in: detail, - threadID: id, + 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, @@ -559,11 +638,12 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { selection: FeatureSelection?, attachments: [FeatureUploadAttachment] ) async throws { - let client = try requireClient(forThreadID: threadID) - let environmentID = client.environment.id + 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 == threadID }) else { + .first(where: { $0.id == route.wireID }) else { throw NativeFeatureClientError.threadNotFound } let model = selection.map(coreModelSelection) @@ -576,7 +656,7 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { attachments: attachments ) let pending: PendingTurnSubmission - if let existing = pendingTurnSubmissions[threadID], + if let existing = pendingTurnSubmissions[route.uiID], existing.signature == signature { pending = existing } else { @@ -584,12 +664,12 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { signature: signature, identity: CommandIdentity() ) - pendingTurnSubmissions[threadID] = pending + pendingTurnSubmissions[route.uiID] = pending } do { _ = try await client.sendTurn( - threadID: threadID, + threadID: route.wireID, text: text, runtimeMode: shellThread.runtimeMode, interactionMode: shellThread.interactionMode, @@ -605,7 +685,7 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { } guard await messageWasCommitted( client: client, - threadID: threadID, + threadID: route.wireID, messageID: pending.identity.messageID ) else { // Keep the stable identity. Retrying the same restored draft @@ -616,13 +696,13 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { guard isKnownClient(client, environmentID: environmentID, generation: generation) else { throw CancellationError() } - if pendingTurnSubmissions[threadID]?.identity == pending.identity { - pendingTurnSubmissions[threadID] = nil + 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: threadID, client: client) + try? await refreshThread(id: route.uiID, client: client) try? await refresh(client: client) } @@ -638,44 +718,48 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { } func cancelTurn(threadID: String) async throws { - let client = try requireClient(forThreadID: threadID) - let turnID = shellsByEnvironmentID[client.environment.id]?.threads - .first(where: { $0.id == threadID })? + let route = try threadRoute(for: threadID) + let turnID = shellsByEnvironmentID[route.environmentID]?.threads + .first(where: { $0.id == route.wireID })? .latestTurn? .turnId - _ = try await client.interrupt(threadID: threadID, turnID: turnID) - try await refresh(client: client) + _ = 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 threadID = approvalThreadIDs[id] else { + guard let request = approvalRoutes[id] else { throw NativeFeatureClientError.approvalNotFound } - let client = try requireClient(forThreadID: threadID) + let route = try threadRoute(for: request.threadID) let wireDecision = switch decision { case .allowOnce: "accept" case .allowForSession: "acceptForSession" case .deny: "decline" } - _ = try await client.respondToApproval( - threadID: threadID, - requestID: id, + _ = try await route.client.respondToApproval( + threadID: route.wireID, + requestID: request.wireID, decision: wireDecision ) - try await refreshThread(id: threadID, client: client) + 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 threadID = inputThreadIDs[id] else { + guard let request = inputRoutes[id] else { throw NativeFeatureClientError.inputRequestNotFound } - let client = try requireClient(forThreadID: threadID) - _ = try await client.respondToUserInput( - threadID: threadID, - requestID: id, + let route = try threadRoute(for: request.threadID) + _ = try await route.client.respondToUserInput( + threadID: route.wireID, + requestID: request.wireID, answers: answers.mapValues(\.jsonValue) ) - try await refreshThread(id: threadID, client: client) + inputRoutes[id] = nil + removeCachedInput(id: id, threadID: route.uiID) + try? await refreshThread(id: route.uiID, client: route.client) } func saveSettings(_ settings: FeatureSettings) async throws { @@ -718,16 +802,16 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { } func listFiles(threadID: String, path: String?) async throws -> [FeatureFileEntry] { - let client = try requireClient(forThreadID: threadID) - let context = try workspaceContext(threadID: threadID) - let result = try await client.listProjectEntries(cwd: context.cwd) + 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 client = try requireClient(forThreadID: threadID) - let context = try workspaceContext(threadID: threadID) - let result = try await client.readProjectFile( + let route = try threadRoute(for: threadID) + let context = try workspaceContext(route: route) + let result = try await route.client.readProjectFile( cwd: context.cwd, relativePath: path ) @@ -741,17 +825,17 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { } func loadReview(threadID: String) async throws -> FeatureReview { - let client = try requireClient(forThreadID: threadID) - let context = try workspaceContext(threadID: threadID) - let preview = try await client.reviewDiffPreview(cwd: context.cwd) + 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 client = try requireClient(forThreadID: threadID) - let context = try workspaceContext(threadID: threadID) + let route = try threadRoute(for: threadID) + let context = try workspaceContext(route: route) return NativeWorkspaceMapper.sourceControl( - try await client.refreshVCSStatus(cwd: context.cwd) + try await route.client.refreshVCSStatus(cwd: context.cwd) ) } @@ -760,8 +844,9 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { action: FeatureSourceControlAction, message: String? ) async throws -> FeatureSourceControlStatus { - let client = try requireClient(forThreadID: threadID) - let context = try workspaceContext(threadID: threadID) + 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) @@ -784,26 +869,29 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { } func terminalSnapshot(threadID: String) async throws -> FeatureTerminalSnapshot { - if let snapshot = terminalSnapshots[threadID] { + let route = try threadRoute(for: threadID) + if let snapshot = terminalSnapshots[route.uiID] { return snapshot } - let context = try workspaceContext(threadID: threadID) + let context = try workspaceContext(route: route) return FeatureTerminalSnapshot( - threadID: threadID, + threadID: route.uiID, workingDirectory: context.cwd ) } func terminalEvents(threadID: String) -> AsyncStream { - guard let environmentID = threadEnvironmentIDs[threadID], - let client = environmentClients[environmentID] else { + 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[threadID, default: [:]][subscriptionID] = continuation - if let snapshot = terminalSnapshots[threadID] { + terminalContinuations[uiThreadID, default: [:]][subscriptionID] = continuation + if let snapshot = terminalSnapshots[uiThreadID] { continuation.yield(snapshot) } let task = Task { [weak self] in @@ -818,14 +906,14 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { ) else { break } - guard event.threadId == threadID else { continue } - if let terminalID = self.terminalIDs[threadID], + 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: threadID) - self.publishTerminal(snapshot, threadID: threadID) + let snapshot = self.consumeTerminalEvent(event, threadID: uiThreadID) + self.publishTerminal(snapshot, threadID: uiThreadID) } continuation.finish() } catch is CancellationError { @@ -843,21 +931,21 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { continuation.finish() return } - var snapshot = self.terminalSnapshots[threadID] - ?? FeatureTerminalSnapshot(threadID: threadID) + var snapshot = self.terminalSnapshots[uiThreadID] + ?? FeatureTerminalSnapshot(threadID: uiThreadID) snapshot.state = .failed snapshot.error = error.localizedDescription - self.terminalSnapshots[threadID] = snapshot - self.publishTerminal(snapshot, threadID: threadID) + self.terminalSnapshots[uiThreadID] = snapshot + self.publishTerminal(snapshot, threadID: uiThreadID) continuation.finish() } } continuation.onTermination = { @Sendable [weak self] _ in task.cancel() Task { @MainActor in - self?.terminalContinuations[threadID]?[subscriptionID] = nil - if self?.terminalContinuations[threadID]?.isEmpty == true { - self?.terminalContinuations[threadID] = nil + self?.terminalContinuations[uiThreadID]?[subscriptionID] = nil + if self?.terminalContinuations[uiThreadID]?.isEmpty == true { + self?.terminalContinuations[uiThreadID] = nil } } } @@ -865,14 +953,15 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { } func openTerminal(threadID: String, columns: Int, rows: Int) async throws { - let client = try requireClient(forThreadID: threadID) - let environmentID = client.environment.id + let route = try threadRoute(for: threadID) + let client = route.client + let environmentID = route.environmentID let generation = environmentGeneration - let context = try workspaceContext(threadID: threadID) - let terminalID = terminalIDs[threadID] ?? UUID().uuidString - terminalIDs[threadID] = terminalID + let context = try workspaceContext(route: route) + let terminalID = terminalIDs[route.uiID] ?? UUID().uuidString + terminalIDs[route.uiID] = terminalID let snapshot = try await client.openTerminal( - threadID: threadID, + threadID: route.wireID, terminalID: terminalID, cwd: context.cwd, worktreePath: context.worktreePath, @@ -883,25 +972,27 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { throw CancellationError() } let mapped = NativeWorkspaceMapper.terminal(snapshot) - terminalSnapshots[threadID] = mapped - publishTerminal(mapped, threadID: threadID) + 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 client = try requireClient(forThreadID: threadID) - let terminalID = try requireTerminalID(threadID: threadID) - try await client.writeTerminal( - threadID: threadID, + 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 client = try requireClient(forThreadID: threadID) - let terminalID = try requireTerminalID(threadID: threadID) - try await client.resizeTerminal( - threadID: threadID, + 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 @@ -909,18 +1000,19 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { } func closeTerminal(threadID: String) async throws { - let client = try requireClient(forThreadID: threadID) - let environmentID = client.environment.id + let route = try threadRoute(for: threadID) + let client = route.client + let environmentID = route.environmentID let generation = environmentGeneration - let terminalID = try requireTerminalID(threadID: threadID) - try await client.closeTerminal(threadID: threadID, terminalID: terminalID) + 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[threadID] = nil - let snapshot = FeatureTerminalSnapshot(threadID: threadID) - terminalSnapshots[threadID] = snapshot - publishTerminal(snapshot, threadID: threadID) + terminalIDs[route.uiID] = nil + let snapshot = FeatureTerminalSnapshot(threadID: route.uiID) + terminalSnapshots[route.uiID] = snapshot + publishTerminal(snapshot, threadID: route.uiID) } private func requireClient() throws -> T3Client { @@ -928,12 +1020,118 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { return client } - private func requireClient(forThreadID threadID: String) throws -> T3Client { + 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 client + 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 { @@ -943,13 +1141,12 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { return terminalID } - private func workspaceContext(threadID: String) throws -> ( + private func workspaceContext(route: NativeThreadRoute) throws -> ( cwd: String, worktreePath: String? ) { - guard let environmentID = threadEnvironmentIDs[threadID], - let shell = shellsByEnvironmentID[environmentID], - let thread = shell.threads.first(where: { $0.id == threadID }), + 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 } @@ -964,7 +1161,8 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { threadID: String ) -> FeatureTerminalSnapshot { if let coreSnapshot = event.snapshot { - let snapshot = NativeWorkspaceMapper.terminal(coreSnapshot) + var snapshot = NativeWorkspaceMapper.terminal(coreSnapshot) + snapshot.threadID = threadID terminalIDs[threadID] = coreSnapshot.terminalId terminalSnapshots[threadID] = snapshot return snapshot @@ -1245,9 +1443,13 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { case let .projectRemoved(_, projectID): projects.removeAll { $0.id == projectID } case let .threadUpserted(_, thread): - changedThreadID = thread.id + changedThreadID = activeEnvironment.map { + FeatureScopedID.thread(environmentID: $0.id, wireID: thread.id) + } if let environmentID = activeEnvironment?.id { - archivedThreadsByEnvironmentID[environmentID]?.removeAll { $0.id == thread.id } + archivedThreadsByEnvironmentID[environmentID]?.removeAll { + ($0.wireID ?? $0.id) == thread.id + } } if let index = threads.firstIndex(where: { $0.id == thread.id }) { threads[index] = thread @@ -1255,11 +1457,16 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { threads.append(thread) } case let .threadRemoved(_, threadID): - changedThreadID = threadID + let uiThreadID = activeEnvironment.map { + FeatureScopedID.thread(environmentID: $0.id, wireID: threadID) + } + changedThreadID = uiThreadID shouldRefreshArchived = true threads.removeAll { $0.id == threadID } - latestDetails[threadID] = nil - if activeThreadID == threadID { + if let uiThreadID { + latestDetails[uiThreadID] = nil + } + if activeThreadID == uiThreadID { resetDetailRefresh() activeThreadID = nil activeThreadEnvironmentID = nil @@ -1312,6 +1519,34 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { } } + /// 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 @@ -1390,25 +1625,94 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { "That server is currently unreachable." } } - rebuildThreadEnvironmentIndex(savedEnvironments) + rebuildEntityIndexes(savedEnvironments) } - private func rebuildThreadEnvironmentIndex(_ environments: [Environment]) { - var owners: [String: String] = [:] - // Prefer the active environment only in the vanishingly unlikely case - // that two independent servers generated the same thread UUID. - let activeID = activeEnvironment?.id - let ordered = environments.filter { $0.id != activeID } - + environments.filter { $0.id == activeID } - for environment in ordered { - for thread in shellsByEnvironmentID[environment.id]?.threads ?? [] { - owners[thread.id] = environment.id + 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 archivedThreadsByEnvironmentID[environment.id] ?? [] { - owners[thread.id] = environment.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) + ) } } - threadEnvironmentIDs = owners + + 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 { @@ -1422,11 +1726,16 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { if activeEnvironment?.id == environment.id { latestShell = shell } - rebuildThreadEnvironmentIndex( + rebuildEntityIndexes( (try? await runtime.environments()) ?? [environment] ) - if includeArchived, activeEnvironment?.id == environment.id { - scheduleArchivedRefresh(client: client, environment: 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) } @@ -1444,7 +1753,7 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { self.archivedThreadsByEnvironmentID[environment.id] = archivedShell.threads.map { self.mapThread($0, environment: environment) } - self.rebuildThreadEnvironmentIndex( + self.rebuildEntityIndexes( (try? await self.runtime.environments()) ?? [environment] ) if let shell = self.latestShell { @@ -1454,18 +1763,22 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { } private func refreshThread(id: String, client: T3Client) async throws { - let environment = client.environment + 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: 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) - if latestDetails[id] != detail { - latestDetails[id] = detail + if latestDetails[route.uiID] != detail { + latestDetails[route.uiID] = detail continuation.yield(.detail(detail)) } - let hydrationBase = latestDetails[id] ?? detail + let hydrationBase = latestDetails[route.uiID] ?? detail let hydrated = await hydratedAttachmentURLs( in: hydrationBase, client: client, @@ -1473,11 +1786,11 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { generation: generation ) guard isKnownClient(client, environmentID: environment.id, generation: generation), - latestDetails[id] == hydrationBase, + latestDetails[route.uiID] == hydrationBase, hydrated != hydrationBase else { return } - latestDetails[id] = hydrated + latestDetails[route.uiID] = hydrated continuation.yield(.detail(hydrated)) } @@ -1499,7 +1812,7 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { if sourceEnvironment.id == environment.id { latestShell = shell } - rebuildThreadEnvironmentIndex(environments) + rebuildEntityIndexes(environments) let connectionState: FeatureConnection.State let connectionDetail: String? if sourceEnvironment.id == environment.id { @@ -1569,15 +1882,24 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { let live = shellsByEnvironmentID[environment.id]?.threads.map { mapThread($0, environment: environment) } ?? [] - return live + (archivedThreadsByEnvironmentID[environment.id] ?? []) + let liveIDs = Set(live.map(\.id)) + let cached = (archivedThreadsByEnvironmentID[environment.id] ?? []).filter { + !liveIDs.contains($0.id) + } + return live + cached } let projects = environments.flatMap { environment in (shellsByEnvironmentID[environment.id]?.projects ?? []).map { project in + let uiID = FeatureScopedID.project( + environmentID: environment.id, + wireID: project.id + ) let count = threads.lazy.filter { - $0.environmentID == environment.id && $0.projectID == project.id + $0.projectID == uiID }.count return FeatureProject( - id: project.id, + id: uiID, + wireID: project.id, environmentID: environment.id, name: project.title, path: project.workspaceRoot, @@ -1620,8 +1942,12 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { environment: Environment ) -> FeatureThread { FeatureThread( - id: thread.id, - projectID: thread.projectId, + 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, @@ -1671,8 +1997,12 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { environment: Environment ) -> FeatureThread { FeatureThread( - id: thread.id, - projectID: thread.projectId, + 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, @@ -1722,8 +2052,8 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { _ thread: OrchestrationThread, environment: Environment ) -> FeatureThreadDetail { - let approvals = pendingApprovals(thread) - let userInputs = pendingUserInputs(thread) + let approvals = pendingApprovals(thread, environment: environment) + let userInputs = pendingUserInputs(thread, environment: environment) let messages = thread.messages.map { message in FeatureMessage( id: message.id, @@ -1761,8 +2091,12 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { let mergedMessages = (messages + errors + collapsedWorkLogs(thread.activities)) .sorted { $0.createdAt < $1.createdAt } let mappedThread = FeatureThread( - id: thread.id, - projectID: thread.projectId, + 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, @@ -1854,12 +2188,22 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { } } - private func pendingApprovals(_ thread: OrchestrationThread) -> [FeatureApproval] { + 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 { @@ -1869,52 +2213,78 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { default: .other } let detail = activity.payload["detail"]?.stringValue ?? activity.summary - open[requestID] = FeatureApproval( - id: requestID, - threadID: thread.id, + 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 ) - approvalThreadIDs[requestID] = thread.id - } else if activity.kind == "approval.resolved", let requestID { - open[requestID] = nil - approvalThreadIDs[requestID] = nil - } else if activity.kind == "provider.approval.respond.failed", let requestID { + 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[requestID] = nil - approvalThreadIDs[requestID] = nil + open[uiRequestID] = nil + approvalRoutes[uiRequestID] = nil } } } return open.values.sorted { $0.id < $1.id } } - private func pendingUserInputs(_ thread: OrchestrationThread) -> [FeatureUserInput] { + 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 { - open[requestID] = FeatureUserInput( - id: requestID, - threadID: thread.id, + let uiRequestID = FeatureScopedID.input( + environmentID: environment.id, + wireID: requestID + ) + open[uiRequestID] = FeatureUserInput( + id: uiRequestID, + wireID: requestID, + threadID: threadID, questions: questions ) - inputThreadIDs[requestID] = thread.id - } else if activity.kind == "user-input.resolved", let requestID { - open[requestID] = nil - inputThreadIDs[requestID] = nil - } else if activity.kind == "provider.user-input.respond.failed", let requestID { + 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[requestID] = nil - inputThreadIDs[requestID] = nil + open[uiRequestID] = nil + inputRoutes[uiRequestID] = nil } } } @@ -2074,12 +2444,13 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging { private func modelSelection( _ selection: FeatureSelection?, - projectID: String + projectID: String, + shell: OrchestrationShellSnapshot? ) -> ModelSelection { if let selection { return coreModelSelection(selection) } - if let projectDefault = latestShell?.projects + if let projectDefault = shell?.projects .first(where: { $0.id == projectID })? .defaultModelSelection { return projectDefault @@ -2470,6 +2841,35 @@ private struct EnvironmentShellLoad: Sendable { 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 @@ -2516,6 +2916,7 @@ private struct PendingTurnSubmission { private enum NativeFeatureClientError: LocalizedError { case notConnected + case projectNotFound case threadNotFound case workspaceNotFound case approvalNotFound @@ -2529,6 +2930,7 @@ private enum NativeFeatureClientError: LocalizedError { 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." diff --git a/apps/swift-ios/Features/Chat/ThreadDetailView.swift b/apps/swift-ios/Features/Chat/ThreadDetailView.swift index 66a97c7a52..e28269cef5 100644 --- a/apps/swift-ios/Features/Chat/ThreadDetailView.swift +++ b/apps/swift-ios/Features/Chat/ThreadDetailView.swift @@ -52,6 +52,9 @@ public struct ThreadDetailView: View { selection = currentSelection isLoading = false } + .onDisappear { + model.releaseThread(thread.id) + } .sheet(item: $toolSurface) { surface in NavigationStack { switch surface { diff --git a/apps/swift-ios/Features/Root/FeatureRootModel.swift b/apps/swift-ios/Features/Root/FeatureRootModel.swift index 17a9cc4aab..1e739a04af 100644 --- a/apps/swift-ios/Features/Root/FeatureRootModel.swift +++ b/apps/swift-ios/Features/Root/FeatureRootModel.swift @@ -231,6 +231,11 @@ public final class FeatureRootModel { } } + /// Ends any selected-thread transport work when its detail view closes. + public func releaseThread(_ id: String) { + client.releaseThread(id: id) + } + public func sendMessage(threadID: String, text: String, selection: FeatureSelection?) async -> Bool { await sendMessage( FeatureMessageSubmission( diff --git a/apps/swift-ios/Features/Shared/FeatureClient.swift b/apps/swift-ios/Features/Shared/FeatureClient.swift index dab0bc90f3..773dbba44b 100644 --- a/apps/swift-ios/Features/Shared/FeatureClient.swift +++ b/apps/swift-ios/Features/Shared/FeatureClient.swift @@ -31,6 +31,7 @@ public protocol FeatureClient: AnyObject { func deleteThread(id: String) async throws func loadThread(id: String) async throws -> FeatureThreadDetail + func releaseThread(id: String) func sendMessage(threadID: String, text: String, selection: FeatureSelection?) async throws func sendMessage( threadID: String, @@ -72,6 +73,7 @@ public extension FeatureClient { func removeEnvironment(id: String) async throws {} func disconnect() async {} func addProject(path: String) async throws {} + func releaseThread(id: String) {} func resolveUserInput(id: String, answers: [String: FeatureInputAnswer]) async throws {} /// Keeps simple text-only callers source-compatible while the typed API diff --git a/apps/swift-ios/Features/Shared/FeatureModels.swift b/apps/swift-ios/Features/Shared/FeatureModels.swift index 153efe61a1..02f6b21f01 100644 --- a/apps/swift-ios/Features/Shared/FeatureModels.swift +++ b/apps/swift-ios/Features/Shared/FeatureModels.swift @@ -55,6 +55,9 @@ public struct FeatureEnvironment: Identifiable, Sendable, Equatable, Hashable, C public struct FeatureProject: Identifiable, Sendable, Equatable, Hashable, Codable { public let id: String + /// The environment-local identifier sent over the wire. Native aggregate + /// snapshots scope `id` by environment so cloned databases remain distinct. + public var wireID: String? public var environmentID: String public var name: String public var path: String @@ -63,6 +66,7 @@ public struct FeatureProject: Identifiable, Sendable, Equatable, Hashable, Codab public init( id: String, + wireID: String? = nil, environmentID: String, name: String, path: String, @@ -70,6 +74,7 @@ public struct FeatureProject: Identifiable, Sendable, Equatable, Hashable, Codab defaultSelection: FeatureSelection? = nil ) { self.id = id + self.wireID = wireID self.environmentID = environmentID self.name = name self.path = path @@ -102,6 +107,8 @@ public enum FeatureInteractionMode: String, CaseIterable, Sendable, Codable { public struct FeatureThread: Identifiable, Sendable, Equatable, Hashable, Codable { public let id: String + /// The environment-local identifier sent over the wire. + public var wireID: String? public var projectID: String public var environmentID: String? public var environmentName: String? @@ -131,6 +138,7 @@ public struct FeatureThread: Identifiable, Sendable, Equatable, Hashable, Codabl public init( id: String, + wireID: String? = nil, projectID: String, environmentID: String? = nil, environmentName: String? = nil, @@ -159,6 +167,7 @@ public struct FeatureThread: Identifiable, Sendable, Equatable, Hashable, Codabl interactionMode: FeatureInteractionMode = .standard ) { self.id = id + self.wireID = wireID self.projectID = projectID self.environmentID = environmentID self.environmentName = environmentName @@ -274,13 +283,23 @@ public enum FeatureApprovalKind: String, Sendable, Codable { public struct FeatureApproval: Identifiable, Sendable, Equatable, Hashable, Codable { public let id: String + /// The provider request identifier sent over the wire. + public var wireID: String? public var threadID: String public var kind: FeatureApprovalKind public var title: String public var detail: String - public init(id: String, threadID: String, kind: FeatureApprovalKind, title: String, detail: String) { + public init( + id: String, + wireID: String? = nil, + threadID: String, + kind: FeatureApprovalKind, + title: String, + detail: String + ) { self.id = id + self.wireID = wireID self.threadID = threadID self.kind = kind self.title = title @@ -383,16 +402,48 @@ public struct FeatureInputQuestion: Identifiable, Sendable, Equatable, Hashable, public struct FeatureUserInput: Identifiable, Sendable, Equatable, Hashable, Codable { public let id: String + /// The provider request identifier sent over the wire. + public var wireID: String? public var threadID: String public var questions: [FeatureInputQuestion] - public init(id: String, threadID: String, questions: [FeatureInputQuestion]) { + public init( + id: String, + wireID: String? = nil, + threadID: String, + questions: [FeatureInputQuestion] + ) { self.id = id + self.wireID = wireID self.threadID = threadID self.questions = questions } } +/// Stable UI identity for entities merged from independent environments. +/// Length-prefixing avoids separator collisions without requiring IDs to be parsed. +enum FeatureScopedID { + static func project(environmentID: String, wireID: String) -> String { + make(kind: "project", environmentID: environmentID, wireID: wireID) + } + + static func thread(environmentID: String, wireID: String) -> String { + make(kind: "thread", environmentID: environmentID, wireID: wireID) + } + + static func approval(environmentID: String, wireID: String) -> String { + make(kind: "approval", environmentID: environmentID, wireID: wireID) + } + + static func input(environmentID: String, wireID: String) -> String { + make(kind: "input", environmentID: environmentID, wireID: wireID) + } + + private static func make(kind: String, environmentID: String, wireID: String) -> String { + "\(kind):\(environmentID.utf8.count):\(environmentID)\(wireID)" + } +} + public struct FeatureThreadDetail: Sendable, Equatable, Codable { public var thread: FeatureThread public var messages: [FeatureMessage] diff --git a/apps/swift-ios/Features/Workspace/DailyUXModels.swift b/apps/swift-ios/Features/Workspace/DailyUXModels.swift index 958b42fa44..94f8cb9e4b 100644 --- a/apps/swift-ios/Features/Workspace/DailyUXModels.swift +++ b/apps/swift-ios/Features/Workspace/DailyUXModels.swift @@ -74,6 +74,63 @@ public struct FeatureMessageSubmission: Sendable, Equatable { } } +enum DailyUXCreationContext { + static func projects(in snapshot: FeatureSnapshot) -> [FeatureProject] { + guard !snapshot.environments.isEmpty else { return snapshot.projects } + let availableEnvironmentIDs = Set( + snapshot.environments.compactMap { environment in + let state = environment.isActive + ? snapshot.connection.state + : environment.connectionState + return state == .disconnected ? nil : environment.id + } + ) + return snapshot.projects.filter { + availableEnvironmentIDs.contains($0.environmentID) + } + } + + static func providers( + for project: FeatureProject?, + in snapshot: FeatureSnapshot + ) -> [FeatureProvider] { + guard let project, + let activeID = snapshot.environments.first(where: \.isActive)?.id, + project.environmentID != activeID else { + return snapshot.providers + } + guard let selection = project.defaultSelection else { return [] } + return [ + FeatureProvider( + id: selection.providerID, + name: selection.providerID, + driver: selection.providerID, + models: [ + FeatureModel( + id: selection.modelID, + name: selection.modelID, + isDefault: true + ), + ] + ), + ] + } + + static func initialSelection( + for project: FeatureProject?, + in snapshot: FeatureSnapshot + ) -> FeatureSelection? { + if let projectDefault = project?.defaultSelection { + return projectDefault + } + return DailyUXModelOptions.initialSelection( + projectDefault: nil, + appDefault: snapshot.settings.defaultSelection, + providers: providers(for: project, in: snapshot) + ) + } +} + struct DailyUXSidebarIndex { let active: [FeatureThread] let snoozed: [FeatureThread] @@ -145,7 +202,12 @@ struct DailyUXSidebarIndex { ) -> [FeatureThread] { let normalizedQuery = query.trimmingCharacters(in: .whitespacesAndNewlines) guard !normalizedQuery.isEmpty else { return [] } - let projectByID = Dictionary(uniqueKeysWithValues: snapshot.projects.map { ($0.id, $0) }) + // Aggregate snapshots can include legacy fixtures with duplicate raw IDs. + // Native projects are environment-scoped, while this defensive reduce + // keeps search non-crashing for older callers during migration. + let projectByID = snapshot.projects.reduce(into: [String: FeatureProject]()) { + $0[$1.id] = $1 + } return candidates.filter { thread in let project = projectByID[thread.projectID] return [ diff --git a/apps/swift-ios/Features/Workspace/NewThreadView.swift b/apps/swift-ios/Features/Workspace/NewThreadView.swift index fe51c7d510..d3b18f873b 100644 --- a/apps/swift-ios/Features/Workspace/NewThreadView.swift +++ b/apps/swift-ios/Features/Workspace/NewThreadView.swift @@ -42,8 +42,8 @@ public struct NewThreadView: View { text: $prompt, selection: $selection, attachments: $attachments, - providers: model.snapshot.providers, - threadSelection: initialSelection, + providers: creationProviders, + threadSelection: nil, isSending: isSubmitting, isWorking: false, focused: $promptFocused, @@ -147,10 +147,7 @@ public struct NewThreadView: View { } private var creationProjects: [FeatureProject] { - guard let activeID = model.snapshot.environments.first(where: \.isActive)?.id else { - return model.snapshot.projects - } - return model.snapshot.projects.filter { $0.environmentID == activeID } + DailyUXCreationContext.projects(in: model.snapshot) } private var environmentName: String { @@ -162,13 +159,18 @@ public struct NewThreadView: View { } private var initialSelection: FeatureSelection? { - DailyUXModelOptions.initialSelection( - projectDefault: selectedProject?.defaultSelection, - appDefault: model.snapshot.settings.defaultSelection, - providers: model.snapshot.providers + DailyUXCreationContext.initialSelection( + for: selectedProject, + in: model.snapshot ) } + /// Provider catalogues are active-device-only. For a passive project, keep + /// its own default selectable without leaking models from another machine. + private var creationProviders: [FeatureProvider] { + DailyUXCreationContext.providers(for: selectedProject, in: model.snapshot) + } + private var canSubmit: Bool { !isSubmitting && !projectID.isEmpty @@ -183,7 +185,7 @@ public struct NewThreadView: View { private var imagesAllowed: Bool { DailyUXModelOptions.supportsImages( selection: selection, - providers: model.snapshot.providers + providers: creationProviders ) } diff --git a/apps/swift-ios/Tests/FeatureTests/DailyUXNewTaskTests.swift b/apps/swift-ios/Tests/FeatureTests/DailyUXNewTaskTests.swift index 6ef3fb2be8..28aa27f342 100644 --- a/apps/swift-ios/Tests/FeatureTests/DailyUXNewTaskTests.swift +++ b/apps/swift-ios/Tests/FeatureTests/DailyUXNewTaskTests.swift @@ -26,6 +26,83 @@ struct DailyUXNewTaskTests { #expect(request.attachments.first?.byteCount == 3) } + @Test + func passiveProjectsKeepTheirOwnModelCatalogAndDefault() throws { + let passiveDefault = FeatureSelection( + providerID: "claudeAgent", + modelID: "claude-opus-4-1" + ) + let activeProject = FeatureProject( + id: "active-project", + environmentID: "active", + name: "Active", + path: "/active" + ) + let passiveProject = FeatureProject( + id: "passive-project", + environmentID: "passive", + name: "Passive", + path: "/passive", + defaultSelection: passiveDefault + ) + let snapshot = FeatureSnapshot( + connection: .init(state: .connected), + environments: [ + .init( + id: "active", + name: "Active", + endpoint: "https://active.example", + isActive: true, + connectionState: .connected + ), + .init( + id: "passive", + name: "Passive", + endpoint: "https://passive.example", + connectionState: .connected + ), + .init( + id: "offline", + name: "Offline", + endpoint: "https://offline.example", + connectionState: .disconnected + ), + ], + projects: [ + activeProject, + passiveProject, + .init( + id: "offline-project", + environmentID: "offline", + name: "Offline", + path: "/offline" + ), + ], + providers: [ + .init( + id: "codex", + name: "Codex", + models: [.init(id: "gpt-5.6-sol", name: "GPT-5.6")] + ), + ] + ) + + #expect( + DailyUXCreationContext.projects(in: snapshot).map(\.id) + == ["active-project", "passive-project"] + ) + let passiveProviders = DailyUXCreationContext.providers( + for: passiveProject, + in: snapshot + ) + #expect(passiveProviders.map(\.id) == ["claudeAgent"]) + #expect(passiveProviders.first?.models.map(\.id) == ["claude-opus-4-1"]) + #expect( + DailyUXCreationContext.initialSelection(for: passiveProject, in: snapshot) + == passiveDefault + ) + } + @Test @MainActor func imageProcessorDownsamplesUploadAndBuildsSmallThumbnail() throws { let source = UIGraphicsImageRenderer(size: CGSize(width: 2_400, height: 1_200)) diff --git a/apps/swift-ios/Tests/FeatureTests/DailyUXSidebarTests.swift b/apps/swift-ios/Tests/FeatureTests/DailyUXSidebarTests.swift index a8b74de062..4984a189ea 100644 --- a/apps/swift-ios/Tests/FeatureTests/DailyUXSidebarTests.swift +++ b/apps/swift-ios/Tests/FeatureTests/DailyUXSidebarTests.swift @@ -167,6 +167,78 @@ struct DailyUXSidebarTests { #expect(searched.searchResults.map(\.id) == ["server"]) } + @Test + func searchHandlesScopedClonesAndLegacyDuplicateProjectIDs() { + let localProjectID = FeatureScopedID.project( + environmentID: "local", + wireID: "project-shared" + ) + let remoteProjectID = FeatureScopedID.project( + environmentID: "remote", + wireID: "project-shared" + ) + let projects = [ + FeatureProject( + id: localProjectID, + wireID: "project-shared", + environmentID: "local", + name: "Mobile", + path: "/work/mobile" + ), + FeatureProject( + id: remoteProjectID, + wireID: "project-shared", + environmentID: "remote", + name: "Server", + path: "/work/server" + ), + ] + let local = thread( + id: "local-thread", + projectID: localProjectID, + title: "Polish", + created: -10, + updated: -5 + ) + let remote = thread( + id: "remote-thread", + projectID: remoteProjectID, + title: "Compression", + created: -20, + updated: -5 + ) + let scoped = FeatureSnapshot(projects: projects, threads: [local, remote]) + + #expect( + DailyUXSidebarIndex(snapshot: scoped, query: "server", now: now) + .searchResults.map(\.id) == ["remote-thread"] + ) + + let legacyDuplicates = FeatureSnapshot( + projects: projects.map { + FeatureProject( + id: "project-shared", + environmentID: $0.environmentID, + name: $0.name, + path: $0.path + ) + }, + threads: [ + thread( + id: "legacy", + projectID: "project-shared", + title: "Legacy", + created: -10, + updated: -5 + ), + ] + ) + #expect( + DailyUXSidebarIndex(snapshot: legacyDuplicates, query: "server", now: now) + .searchResults.map(\.id) == ["legacy"] + ) + } + @Test func attentionScopesRemainFocusedSubsetsOfActive() { let approval = thread( diff --git a/apps/swift-ios/Tests/FeatureTests/NativeMultiEnvironmentTests.swift b/apps/swift-ios/Tests/FeatureTests/NativeMultiEnvironmentTests.swift index 07fc208830..e92846c8f4 100644 --- a/apps/swift-ios/Tests/FeatureTests/NativeMultiEnvironmentTests.swift +++ b/apps/swift-ios/Tests/FeatureTests/NativeMultiEnvironmentTests.swift @@ -11,9 +11,12 @@ final class NativeMultiEnvironmentTests: XCTestCase { let snapshot = try await fixture.client.initialSnapshot() XCTAssertEqual(Set(snapshot.projects.map(\.environmentID)), ["one", "two"]) - XCTAssertEqual(Set(snapshot.threads.map(\.id)), ["thread-one", "thread-two"]) + XCTAssertEqual(Set(snapshot.threads.compactMap(\.wireID)), ["thread-one", "thread-two"]) + let remoteThread = try XCTUnwrap( + snapshot.threads.first(where: { $0.environmentID == "two" }) + ) XCTAssertEqual( - snapshot.threads.first(where: { $0.id == "thread-two" })?.environmentName, + remoteThread.environmentName, "Steam Box" ) XCTAssertEqual( @@ -21,13 +24,13 @@ final class NativeMultiEnvironmentTests: XCTestCase { .connected ) - let detail = try await fixture.client.loadThread(id: "thread-two") + let detail = try await fixture.client.loadThread(id: remoteThread.id) XCTAssertEqual(detail.thread.environmentID, "two") XCTAssertEqual(detail.thread.environmentName, "Steam Box") - try await fixture.client.renameThread(id: "thread-two", title: "Remote rename") + try await fixture.client.renameThread(id: remoteThread.id, title: "Remote rename") try await fixture.client.sendMessage( - threadID: "thread-two", + threadID: remoteThread.id, text: "Run this on Steam Box", selection: nil ) @@ -45,7 +48,10 @@ final class NativeMultiEnvironmentTests: XCTestCase { await fixture.transport.setReachable(false, host: "two.example") let passiveFailure = try await fixture.client.initialSnapshot() - XCTAssertEqual(Set(passiveFailure.threads.map(\.id)), ["thread-one", "thread-two"]) + XCTAssertEqual( + Set(passiveFailure.threads.compactMap(\.wireID)), + ["thread-one", "thread-two"] + ) XCTAssertEqual(passiveFailure.connection.state, .connected) XCTAssertEqual( passiveFailure.environments.first(where: { $0.id == "two" })?.connectionState, @@ -56,7 +62,10 @@ final class NativeMultiEnvironmentTests: XCTestCase { await fixture.transport.setReachable(true, host: "two.example") let activeFailure = try await fixture.client.initialSnapshot() - XCTAssertEqual(Set(activeFailure.threads.map(\.id)), ["thread-one", "thread-two"]) + XCTAssertEqual( + Set(activeFailure.threads.compactMap(\.wireID)), + ["thread-one", "thread-two"] + ) XCTAssertEqual(activeFailure.connection.state, .disconnected) XCTAssertEqual(activeFailure.connection.environmentName, "Left Book") XCTAssertEqual( @@ -66,7 +75,64 @@ final class NativeMultiEnvironmentTests: XCTestCase { await fixture.client.disconnect() } - private func makeFixture() async throws -> MultiEnvironmentFixture { + func testDuplicateWireIDsRemainDistinctAndRouteByEnvironment() async throws { + let fixture = try await makeFixture(duplicateIDs: true) + defer { try? FileManager.default.removeItem(at: fixture.directory) } + + let snapshot = try await fixture.client.initialSnapshot() + XCTAssertEqual(snapshot.projects.count, 2) + XCTAssertEqual(snapshot.threads.count, 2) + XCTAssertEqual(Set(snapshot.projects.map(\.id)).count, 2) + XCTAssertEqual(Set(snapshot.threads.map(\.id)).count, 2) + XCTAssertEqual(Set(snapshot.projects.compactMap(\.wireID)), ["project-shared"]) + XCTAssertEqual(Set(snapshot.threads.compactMap(\.wireID)), ["thread-shared"]) + + let remote = try XCTUnwrap( + snapshot.threads.first(where: { $0.environmentID == "two" }) + ) + _ = try await fixture.client.loadThread(id: remote.id) + try await fixture.client.renameThread(id: remote.id, title: "Remote only") + + let hosts = await fixture.transport.dispatchHosts() + XCTAssertEqual(hosts, ["two.example"]) + await fixture.client.disconnect() + } + + func testPassiveCreateUsesOwningProjectDefaultAndFallbackRemainsRoutable() async throws { + let fixture = try await makeFixture() + defer { try? FileManager.default.removeItem(at: fixture.directory) } + + let snapshot = try await fixture.client.initialSnapshot() + let remoteProject = try XCTUnwrap( + snapshot.projects.first(where: { $0.environmentID == "two" }) + ) + let created = try await fixture.client.createThread( + projectID: remoteProject.id, + title: "Passive task", + selection: nil + ) + + XCTAssertEqual(created.environmentID, "two") + XCTAssertEqual(created.projectID, remoteProject.id) + XCTAssertNotNil(created.wireID) + try await fixture.client.renameThread(id: created.id, title: "Fallback routed") + + let records = await fixture.transport.dispatchRecords() + XCTAssertEqual(records.map(\.host), ["two.example", "two.example"]) + XCTAssertEqual(records[0].command["type"]?.stringValue, "thread.create") + XCTAssertEqual(records[0].command["projectId"]?.stringValue, "project-two") + XCTAssertEqual( + records[0].command["modelSelection"]?["instanceId"]?.stringValue, + "claudeAgent" + ) + XCTAssertEqual( + records[1].command["threadId"]?.stringValue, + created.wireID + ) + await fixture.client.disconnect() + } + + private func makeFixture(duplicateIDs: Bool = false) async throws -> MultiEnvironmentFixture { let directory = FileManager.default.temporaryDirectory .appendingPathComponent("t3-native-multi-\(UUID().uuidString)", isDirectory: true) let environments = [ @@ -91,14 +157,16 @@ final class NativeMultiEnvironmentTests: XCTestCase { let transport = MultiEnvironmentHTTPTransport( shells: [ "one.example": multiEnvironmentShell( - projectID: "project-one", - threadID: "thread-one", + projectID: duplicateIDs ? "project-shared" : "project-one", + threadID: duplicateIDs ? "thread-shared" : "thread-one", title: "Local work" ), "two.example": multiEnvironmentShell( - projectID: "project-two", - threadID: "thread-two", - title: "Remote work" + projectID: duplicateIDs ? "project-shared" : "project-two", + threadID: duplicateIDs ? "thread-shared" : "thread-two", + title: "Remote work", + providerID: "claudeAgent", + modelID: "claude-opus-4-1" ), ] ) @@ -131,11 +199,13 @@ private struct MultiEnvironmentFixture { } private actor MultiEnvironmentHTTPTransport: HTTPTransport { + private let shells: [String: OrchestrationShellSnapshot] private let shellData: [String: Data] private var reachableHosts: Set - private var dispatchedToHosts: [String] = [] + private var dispatched: [MultiEnvironmentDispatchRecord] = [] init(shells: [String: OrchestrationShellSnapshot]) { + self.shells = shells shellData = shells.mapValues { try! JSONEncoder.t3.encode($0) } reachableHosts = Set(shells.keys) } @@ -149,7 +219,11 @@ private actor MultiEnvironmentHTTPTransport: HTTPTransport { } func dispatchHosts() -> [String] { - dispatchedToHosts + dispatched.map(\.host) + } + + func dispatchRecords() -> [MultiEnvironmentDispatchRecord] { + dispatched } func data(for request: URLRequest) throws -> (Data, HTTPURLResponse) { @@ -163,7 +237,9 @@ private actor MultiEnvironmentHTTPTransport: HTTPTransport { } if path.hasPrefix("/api/orchestration/threads/") { let threadID = request.url?.lastPathComponent.removingPercentEncoding ?? "thread" - let projectID = threadID == "thread-two" ? "project-two" : "project-one" + let projectID = shells[host]?.threads + .first(where: { $0.id == threadID })? + .projectId ?? shells[host]?.projects.first?.id ?? "project" return ( try JSONEncoder.t3.encode( multiEnvironmentDetail(projectID: projectID, threadID: threadID) @@ -172,7 +248,13 @@ private actor MultiEnvironmentHTTPTransport: HTTPTransport { ) } if path == "/api/orchestration/dispatch" { - dispatchedToHosts.append(host) + guard let body = request.httpBody else { throw URLError(.badServerResponse) } + dispatched.append( + MultiEnvironmentDispatchRecord( + host: host, + command: try JSONDecoder.t3.decode(JSONValue.self, from: body) + ) + ) return ( try JSONEncoder.t3.encode(DispatchResult(sequence: 2)), multiEnvironmentResponse(request) @@ -192,6 +274,11 @@ private actor MultiEnvironmentHTTPTransport: HTTPTransport { } } +private struct MultiEnvironmentDispatchRecord: Sendable { + let host: String + let command: JSONValue +} + private struct UnavailableMultiEnvironmentWebSocketConnector: WebSocketConnecting { func connect(to _: URL) async throws -> any WebSocketConnection { throw URLError(.cannotConnectToHost) @@ -201,10 +288,12 @@ private struct UnavailableMultiEnvironmentWebSocketConnector: WebSocketConnectin private func multiEnvironmentShell( projectID: String, threadID: String, - title: String + title: String, + providerID: String = "codex", + modelID: String = "gpt-5.6-sol" ) -> OrchestrationShellSnapshot { let timestamp = "2026-07-31T12:00:00.000Z" - let model = ModelSelection(instanceId: "codex", model: "gpt-5.6-sol") + let model = ModelSelection(instanceId: providerID, model: modelID) return OrchestrationShellSnapshot( snapshotSequence: 1, projects: [ diff --git a/apps/swift-ios/Tests/FeatureTests/NativeRetryIdentityTests.swift b/apps/swift-ios/Tests/FeatureTests/NativeRetryIdentityTests.swift index 9b93336007..e4dd2420b5 100644 --- a/apps/swift-ios/Tests/FeatureTests/NativeRetryIdentityTests.swift +++ b/apps/swift-ios/Tests/FeatureTests/NativeRetryIdentityTests.swift @@ -120,7 +120,12 @@ final class NativeRetryIdentityTests: XCTestCase { XCTAssertNotNil(commands[0]["bootstrap"]) XCTAssertNil(commands[1]["bootstrap"]) assertStableIdentity(commands[0], commands[1], includesThreadID: true) - XCTAssertEqual(created.id, commands[0]["threadId"]?.stringValue) + let wireID = try XCTUnwrap(commands[0]["threadId"]?.stringValue) + XCTAssertEqual(created.wireID, wireID) + XCTAssertEqual( + created.id, + FeatureScopedID.thread(environmentID: environment.id, wireID: wireID) + ) await client.disconnect() }