diff --git a/apps/swift-ios/App/NativeFeatureClient.swift b/apps/swift-ios/App/NativeFeatureClient.swift index 3f944cfb4a..57df649733 100644 --- a/apps/swift-ios/App/NativeFeatureClient.swift +++ b/apps/swift-ios/App/NativeFeatureClient.swift @@ -3527,7 +3527,17 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging, name: project.title, path: project.workspaceRoot, threadCount: threadCountByProjectID[uiID, default: 0], - defaultSelection: project.defaultModelSelection.map(mapSelection) + defaultSelection: project.defaultModelSelection.map(mapSelection), + repositoryIdentity: project.repositoryIdentity.map { + FeatureRepositoryIdentity( + canonicalKey: $0.canonicalKey, + rootPath: $0.rootPath, + displayName: $0.displayName, + name: $0.name + ) + }, + createdAt: project.createdAt, + updatedAt: project.updatedAt ) } } @@ -3552,9 +3562,25 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging, case .local: .local case .worktree: .worktree } + let groupingMode: FeatureEnvironmentPreferences.ProjectGroupingMode = + switch serverSettings.sidebarProjectGroupingMode { + case .repositoryPath: .repositoryPath + case .separate: .separate + case .repository, nil: .repository + } + let groupingOverrides = serverSettings.sidebarProjectGroupingOverrides? + .mapValues { mode -> FeatureEnvironmentPreferences.ProjectGroupingMode in + switch mode { + case .repository: return .repository + case .repositoryPath: return .repositoryPath + case .separate: return .separate + } + } ?? [:] preferences[environment.id] = FeatureEnvironmentPreferences( defaultWorkspaceMode: defaultWorkspaceMode, - newWorktreesStartFromOrigin: serverSettings.newWorktreesStartFromOrigin + newWorktreesStartFromOrigin: serverSettings.newWorktreesStartFromOrigin, + projectGroupingMode: groupingMode, + projectGroupingOverrides: groupingOverrides ) } return FeatureSnapshot( diff --git a/apps/swift-ios/Core/ServerConfigModels.swift b/apps/swift-ios/Core/ServerConfigModels.swift index cc61212a4b..b24693014a 100644 --- a/apps/swift-ios/Core/ServerConfigModels.swift +++ b/apps/swift-ios/Core/ServerConfigModels.swift @@ -132,18 +132,30 @@ public enum ServerThreadEnvironmentMode: String, Codable, Equatable, Sendable { case worktree } +public enum ServerProjectGroupingMode: String, Codable, Equatable, Sendable { + case repository + case repositoryPath = "repository_path" + case separate +} + /// New-thread preferences are server-authoritative, so every saved environment /// can resolve these differently even though they share one mobile client. public struct ServerSettingsSnapshot: Codable, Equatable, Sendable { public let defaultThreadEnvMode: ServerThreadEnvironmentMode public let newWorktreesStartFromOrigin: Bool + public let sidebarProjectGroupingMode: ServerProjectGroupingMode? + public let sidebarProjectGroupingOverrides: [String: ServerProjectGroupingMode]? public init( defaultThreadEnvMode: ServerThreadEnvironmentMode = .local, - newWorktreesStartFromOrigin: Bool = true + newWorktreesStartFromOrigin: Bool = true, + sidebarProjectGroupingMode: ServerProjectGroupingMode? = nil, + sidebarProjectGroupingOverrides: [String: ServerProjectGroupingMode]? = nil ) { self.defaultThreadEnvMode = defaultThreadEnvMode self.newWorktreesStartFromOrigin = newWorktreesStartFromOrigin + self.sidebarProjectGroupingMode = sidebarProjectGroupingMode + self.sidebarProjectGroupingOverrides = sidebarProjectGroupingOverrides } } diff --git a/apps/swift-ios/Features/Shared/FeatureComposerDraftStore.swift b/apps/swift-ios/Features/Shared/FeatureComposerDraftStore.swift index 760a85b02d..5319a8770f 100644 --- a/apps/swift-ios/Features/Shared/FeatureComposerDraftStore.swift +++ b/apps/swift-ios/Features/Shared/FeatureComposerDraftStore.swift @@ -254,6 +254,10 @@ public actor FeatureComposerDraftStore { return "environment:\(project.environmentID):new-task:\(projectID)" } + public static func newTaskKey(logicalProjectID: String) -> String { + "logical-project:\(logicalProjectID):new-task" + } + private func loadIfNeeded() throws -> [String: PersistedDraft] { if let loadedDrafts { return loadedDrafts } guard FileManager.default.fileExists(atPath: fileURL.path) else { diff --git a/apps/swift-ios/Features/Shared/FeatureModels.swift b/apps/swift-ios/Features/Shared/FeatureModels.swift index 4277612fd4..27d6063add 100644 --- a/apps/swift-ios/Features/Shared/FeatureModels.swift +++ b/apps/swift-ios/Features/Shared/FeatureModels.swift @@ -53,6 +53,25 @@ public struct FeatureEnvironment: Identifiable, Sendable, Equatable, Hashable, C } } +public struct FeatureRepositoryIdentity: Sendable, Equatable, Hashable, Codable { + public var canonicalKey: String + public var rootPath: String? + public var displayName: String? + public var name: String? + + public init( + canonicalKey: String, + rootPath: String? = nil, + displayName: String? = nil, + name: String? = nil + ) { + self.canonicalKey = canonicalKey + self.rootPath = rootPath + self.displayName = displayName + self.name = name + } +} + public struct FeatureProject: Identifiable, Sendable, Equatable, Hashable, Codable { public let id: String /// The environment-local identifier sent over the wire. Native aggregate @@ -63,6 +82,9 @@ public struct FeatureProject: Identifiable, Sendable, Equatable, Hashable, Codab public var path: String public var threadCount: Int public var defaultSelection: FeatureSelection? + public var repositoryIdentity: FeatureRepositoryIdentity? + public var createdAt: String? + public var updatedAt: String? public init( id: String, @@ -71,7 +93,10 @@ public struct FeatureProject: Identifiable, Sendable, Equatable, Hashable, Codab name: String, path: String, threadCount: Int = 0, - defaultSelection: FeatureSelection? = nil + defaultSelection: FeatureSelection? = nil, + repositoryIdentity: FeatureRepositoryIdentity? = nil, + createdAt: String? = nil, + updatedAt: String? = nil ) { self.id = id self.wireID = wireID @@ -80,6 +105,9 @@ public struct FeatureProject: Identifiable, Sendable, Equatable, Hashable, Codab self.path = path self.threadCount = threadCount self.defaultSelection = defaultSelection + self.repositoryIdentity = repositoryIdentity + self.createdAt = createdAt + self.updatedAt = updatedAt } } @@ -786,15 +814,62 @@ public struct FeatureSettings: Sendable, Equatable, Codable { } public struct FeatureEnvironmentPreferences: Sendable, Equatable, Codable { + public enum ProjectGroupingMode: String, Sendable, Equatable, Codable { + case repository + case repositoryPath = "repository_path" + case separate + } + public var defaultWorkspaceMode: FeatureWorkspaceMode public var newWorktreesStartFromOrigin: Bool + public var projectGroupingMode: ProjectGroupingMode + public var projectGroupingOverrides: [String: ProjectGroupingMode] public init( defaultWorkspaceMode: FeatureWorkspaceMode = .local, - newWorktreesStartFromOrigin: Bool = true + newWorktreesStartFromOrigin: Bool = true, + projectGroupingMode: ProjectGroupingMode = .repository, + projectGroupingOverrides: [String: ProjectGroupingMode] = [:] ) { self.defaultWorkspaceMode = defaultWorkspaceMode self.newWorktreesStartFromOrigin = newWorktreesStartFromOrigin + self.projectGroupingMode = projectGroupingMode + self.projectGroupingOverrides = projectGroupingOverrides + } + + private enum CodingKeys: String, CodingKey { + case defaultWorkspaceMode + case newWorktreesStartFromOrigin + case projectGroupingMode + case projectGroupingOverrides + } + + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + defaultWorkspaceMode = try container.decodeIfPresent( + FeatureWorkspaceMode.self, + forKey: .defaultWorkspaceMode + ) ?? .local + newWorktreesStartFromOrigin = try container.decodeIfPresent( + Bool.self, + forKey: .newWorktreesStartFromOrigin + ) ?? true + projectGroupingMode = try container.decodeIfPresent( + ProjectGroupingMode.self, + forKey: .projectGroupingMode + ) ?? .repository + projectGroupingOverrides = try container.decodeIfPresent( + [String: ProjectGroupingMode].self, + forKey: .projectGroupingOverrides + ) ?? [:] + } + + public func encode(to encoder: any Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(defaultWorkspaceMode, forKey: .defaultWorkspaceMode) + try container.encode(newWorktreesStartFromOrigin, forKey: .newWorktreesStartFromOrigin) + try container.encode(projectGroupingMode, forKey: .projectGroupingMode) + try container.encode(projectGroupingOverrides, forKey: .projectGroupingOverrides) } } diff --git a/apps/swift-ios/Features/Workspace/DailyUXModels.swift b/apps/swift-ios/Features/Workspace/DailyUXModels.swift index 48e1ee68e8..660064a9bc 100644 --- a/apps/swift-ios/Features/Workspace/DailyUXModels.swift +++ b/apps/swift-ios/Features/Workspace/DailyUXModels.swift @@ -110,6 +110,42 @@ enum DailyUXCreationContext { } } + static func projectGroups(in snapshot: FeatureSnapshot) -> [DailyUXProjectGroup] { + let preferences = projectGroupingPreferences(in: snapshot) + return DailyUXProjectGrouping.groups( + projects: projects(in: snapshot), + mode: preferences.projectGroupingMode, + overrides: preferences.projectGroupingOverrides + ) + } + + static func logicalProjectID( + for project: FeatureProject, + in snapshot: FeatureSnapshot + ) -> String { + let preferences = projectGroupingPreferences(in: snapshot) + let groups = DailyUXProjectGrouping.groups( + projects: snapshot.projects, + mode: preferences.projectGroupingMode, + overrides: preferences.projectGroupingOverrides + ) + return DailyUXProjectGrouping.group(containing: project.id, in: groups)?.id + ?? DailyUXProjectGrouping.logicalProjectID( + for: project, + mode: preferences.projectGroupingMode, + overrides: preferences.projectGroupingOverrides + ) + } + + private static func projectGroupingPreferences( + in snapshot: FeatureSnapshot + ) -> FeatureEnvironmentPreferences { + let activeEnvironmentID = snapshot.environments.first(where: \.isActive)?.id + return activeEnvironmentID.flatMap { + snapshot.preferencesByEnvironment?[$0] + } ?? FeatureEnvironmentPreferences() + } + static func providers( for project: FeatureProject?, in snapshot: FeatureSnapshot @@ -173,6 +209,174 @@ enum DailyUXCreationContext { } } +struct DailyUXProjectGroup: Identifiable, Equatable { + let id: String + let name: String + let projects: [FeatureProject] + let memberProjectIDs: Set + + func project(in environmentID: String) -> FeatureProject? { + projects.first { $0.environmentID == environmentID } + } + + func preferredProject(environmentID: String?) -> FeatureProject? { + environmentID.flatMap(project(in:)) ?? projects.first + } +} + +enum DailyUXProjectGrouping { + static func logicalProjectID( + for project: FeatureProject, + mode: FeatureEnvironmentPreferences.ProjectGroupingMode = .repository, + overrides: [String: FeatureEnvironmentPreferences.ProjectGroupingMode] = [:] + ) -> String { + logicalKey(project, mode: resolvedMode(project, mode: mode, overrides: overrides)) + } + + static func groups( + projects: [FeatureProject], + mode: FeatureEnvironmentPreferences.ProjectGroupingMode = .repository, + overrides: [String: FeatureEnvironmentPreferences.ProjectGroupingMode] = [:] + ) -> [DailyUXProjectGroup] { + var projectsByLogicalKey: [String: [FeatureProject]] = [:] + var memberIDsByLogicalKey: [String: Set] = [:] + for physicalProjects in Dictionary(grouping: projects, by: physicalKey).values { + guard let winner = physicalWinner(physicalProjects) else { continue } + let identitySource = identitySource(projects: physicalProjects, winner: winner) + let groupingMode = resolvedMode(winner, mode: mode, overrides: overrides) + let key = logicalKey(identitySource, mode: groupingMode) + projectsByLogicalKey[key, default: []].append(winner) + memberIDsByLogicalKey[key, default: []].formUnion(physicalProjects.map(\.id)) + } + + return projectsByLogicalKey + .map { key, members in + let sorted = members.sorted(by: projectOrder) + return DailyUXProjectGroup( + id: key, + name: groupName(projects: sorted), + projects: sorted, + memberProjectIDs: memberIDsByLogicalKey[key] ?? [] + ) + } + .sorted { lhs, rhs in + let comparison = lhs.name.localizedCaseInsensitiveCompare(rhs.name) + return comparison == .orderedSame ? lhs.id < rhs.id : comparison == .orderedAscending + } + } + + static func group(containing projectID: String, in groups: [DailyUXProjectGroup]) + -> DailyUXProjectGroup? + { + groups.first { $0.memberProjectIDs.contains(projectID) } + } + + private static func physicalKey(_ project: FeatureProject) -> String { + "\(project.environmentID):\(normalizedPath(project.path))" + } + + private static func logicalKey( + _ project: FeatureProject, + mode: FeatureEnvironmentPreferences.ProjectGroupingMode + ) -> String { + if mode == .separate { return physicalKey(project) } + guard let key = project.repositoryIdentity?.canonicalKey.trimmingCharacters( + in: .whitespacesAndNewlines + ), !key.isEmpty else { + return physicalKey(project) + } + if mode == .repositoryPath, + let relativePath = repositoryRelativePath(project), + !relativePath.isEmpty { + return "\(key)::\(relativePath)" + } + return key + } + + private static func resolvedMode( + _ project: FeatureProject, + mode: FeatureEnvironmentPreferences.ProjectGroupingMode, + overrides: [String: FeatureEnvironmentPreferences.ProjectGroupingMode] + ) -> FeatureEnvironmentPreferences.ProjectGroupingMode { + overrides[physicalKey(project)] ?? mode + } + + private static func repositoryRelativePath(_ project: FeatureProject) -> String? { + guard let rootPath = project.repositoryIdentity?.rootPath else { return nil } + let projectPath = normalizedPath(project.path) + let repositoryPath = normalizedPath(rootPath) + guard !projectPath.isEmpty, !repositoryPath.isEmpty else { return nil } + if projectPath == repositoryPath { return "" } + let separator = repositoryPath.contains("\\") ? "\\" : "/" + let prefix = repositoryPath + separator + guard projectPath.hasPrefix(prefix) else { return nil } + return String(projectPath.dropFirst(prefix.count)).replacingOccurrences(of: "\\", with: "/") + } + + private static func physicalWinner(_ projects: [FeatureProject]) -> FeatureProject? { + projects.max { lhs, rhs in + let lhsFreshness = freshness(lhs) + let rhsFreshness = freshness(rhs) + if lhsFreshness != rhsFreshness { return lhsFreshness < rhsFreshness } + return lhs.id < rhs.id + } + } + + private static func identitySource( + projects: [FeatureProject], + winner: FeatureProject + ) -> FeatureProject { + guard winner.repositoryIdentity == nil else { return winner } + return physicalWinner(projects.filter { $0.repositoryIdentity != nil }) ?? winner + } + + private static func freshness(_ project: FeatureProject) -> String { + project.updatedAt ?? project.createdAt ?? "" + } + + private static func groupName(projects: [FeatureProject]) -> String { + let displayNames = uniqueNonEmpty(projects.compactMap(\.repositoryIdentity?.displayName)) + if displayNames.count == 1, let name = displayNames.first { return name } + let repositoryNames = uniqueNonEmpty(projects.compactMap(\.repositoryIdentity?.name)) + if repositoryNames.count == 1, let name = repositoryNames.first { return name } + return projects.first?.name ?? "Project" + } + + private static func uniqueNonEmpty(_ values: [String]) -> [String] { + var seen = Set() + return values.compactMap { value in + let trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines) + guard !trimmed.isEmpty, seen.insert(trimmed).inserted else { return nil } + return trimmed + } + } + + private static func normalizedPath(_ path: String) -> String { + var normalized = path.trimmingCharacters(in: .whitespacesAndNewlines) + let isWindowsPath = normalized.range( + of: #"^[a-zA-Z]:([/\\]|$)"#, + options: .regularExpression + ) != nil || normalized.hasPrefix("\\\\") + let separators = isWindowsPath + ? CharacterSet(charactersIn: "/\\") + : CharacterSet(charactersIn: "/") + while normalized.count > 1, + let scalar = normalized.unicodeScalars.last, + separators.contains(scalar) { + normalized.removeLast() + } + if isWindowsPath { + return normalized.replacingOccurrences(of: "/", with: "\\").lowercased() + } + return normalized + } + + private static func projectOrder(_ lhs: FeatureProject, _ rhs: FeatureProject) -> Bool { + if lhs.environmentID != rhs.environmentID { return lhs.environmentID < rhs.environmentID } + return lhs.id < rhs.id + } +} + struct DailyUXSidebarIndex { let pinned: [FeatureThread] let active: [FeatureThread] diff --git a/apps/swift-ios/Features/Workspace/NewThreadView.swift b/apps/swift-ios/Features/Workspace/NewThreadView.swift index c4b14d69ab..9db369b1e8 100644 --- a/apps/swift-ios/Features/Workspace/NewThreadView.swift +++ b/apps/swift-ios/Features/Workspace/NewThreadView.swift @@ -14,7 +14,6 @@ public struct NewThreadView: View { @State private var selection: FeatureSelection? @State private var selectionIsExplicit = false @State private var preferredSelection: FeatureSelection? - @State private var recentProjectByEnvironment: [String: String] = [:] @State private var attachments: [FeatureDraftAttachment] = [] @State private var workspaceMode: FeatureWorkspaceMode = .local @State private var workspaceSelectionIsExplicit = false @@ -90,9 +89,16 @@ public struct NewThreadView: View { } .onAppear { if projectID.isEmpty { - let initialID = creationProjects.first(where: { $0.id == initialProjectID })?.id - ?? creationProjects.first?.id - ?? "" + let initialProject = creationProjects.first { $0.id == initialProjectID } + let initialGroup = initialProject.flatMap { + DailyUXProjectGrouping.group( + containing: $0.id, + in: creationProjectGroups + ) + } + let initialID = initialGroup?.preferredProject( + environmentID: initialProject?.environmentID + )?.id ?? creationProjectGroups.first?.projects.first?.id ?? "" selectInitialProject(initialID) } } @@ -100,7 +106,14 @@ public struct NewThreadView: View { .onChange(of: creationProjectIDs) { _, ids in guard !ids.contains(projectID) else { return } persistCurrentDraftImmediately() - selectInitialProject(ids.first ?? "") + let previousProject = model.snapshot.projects.first { $0.id == projectID } + let previousGroupID = previousProject.map { + DailyUXCreationContext.logicalProjectID(for: $0, in: model.snapshot) + } + let replacement = creationProjectGroups.first { $0.id == previousGroupID }? + .preferredProject(environmentID: previousProject?.environmentID) + ?? creationProjectGroups.first?.projects.first + selectInitialProject(replacement?.id ?? "") } .onChange(of: prompt) { scheduleDraftSave() } .onChange(of: selection) { scheduleDraftSave() } @@ -155,19 +168,19 @@ public struct NewThreadView: View { HStack(spacing: 0) { Text("in") Menu { - ForEach(creationProjects) { project in + ForEach(creationProjectGroups) { group in Button { - selectProject(project.id) + selectProjectGroup(group) } label: { - if project.id == projectID { - Label(project.name, systemImage: "checkmark") + if group.id == selectedProjectGroup?.id { + Label(group.name, systemImage: "checkmark") } else { - Text(project.name) + Text(group.name) } } } } label: { - Text(selectedProject?.name ?? "a project") + Text(selectedProjectGroup?.name ?? selectedProject?.name ?? "a project") .foregroundStyle(T3Colors.textPrimary) .overlay(alignment: .bottom) { DottedUnderline() @@ -258,6 +271,14 @@ public struct NewThreadView: View { creationProjects.first { $0.id == projectID } } + private var creationProjectGroups: [DailyUXProjectGroup] { + DailyUXCreationContext.projectGroups(in: model.snapshot) + } + + private var selectedProjectGroup: DailyUXProjectGroup? { + DailyUXProjectGrouping.group(containing: projectID, in: creationProjectGroups) + } + private var workspaceControls: some View { HStack(spacing: 14) { Menu { @@ -359,11 +380,11 @@ public struct NewThreadView: View { } private var creationProjectIDs: [String] { - creationProjects.map(\.id) + creationProjectGroups.flatMap(\.projects).map(\.id) } private var creationEnvironments: [FeatureEnvironment] { - let environmentIDs = Set(creationProjects.map(\.environmentID)) + let environmentIDs = Set(selectedProjectGroup?.projects.map(\.environmentID) ?? []) return model.snapshot.environments.filter { environmentIDs.contains($0.id) } } @@ -536,11 +557,17 @@ public struct NewThreadView: View { prepareProjectIfNeeded(id) } + private func selectProjectGroup(_ group: DailyUXProjectGroup) { + guard group.id != selectedProjectGroup?.id else { return } + let target = group.preferredProject(environmentID: selectedProject?.environmentID) + ?? group.projects.first + guard let target else { return } + selectProject(target.id) + } + private func selectEnvironment(_ id: String) { guard selectedProject?.environmentID != id else { return } - let project = recentProjectByEnvironment[id].flatMap { recentID in - creationProjects.first { $0.id == recentID && $0.environmentID == id } - } ?? creationProjects.first { $0.environmentID == id } + let project = selectedProjectGroup?.project(in: id) guard let project else { return } selectProject(project.id) } @@ -577,8 +604,6 @@ public struct NewThreadView: View { return } - recentProjectByEnvironment[project.environmentID] = project.id - let providers = ProviderModelCatalogNormalizer.normalized( DailyUXCreationContext.providers(for: project, in: model.snapshot) ) @@ -656,7 +681,7 @@ public struct NewThreadView: View { !requestedProjectID.isEmpty else { return } - let key = FeatureComposerDraftStore.newTaskKey(project: project) + let key = draftKey(for: project) let pendingImmediateSave = immediateDraftSaveTasks[key] await NewTaskDraftWriteFence.wait(pendingImmediateSave) guard !Task.isCancelled, @@ -714,7 +739,18 @@ public struct NewThreadView: View { private var currentDraftKey: String? { guard let project = selectedProject else { return nil } - return FeatureComposerDraftStore.newTaskKey(project: project) + return draftKey(for: project) + } + + private func draftKey(for project: FeatureProject) -> String { + guard project.repositoryIdentity != nil, + let group = DailyUXProjectGrouping.group( + containing: project.id, + in: creationProjectGroups + ) else { + return FeatureComposerDraftStore.newTaskKey(project: project) + } + return FeatureComposerDraftStore.newTaskKey(logicalProjectID: group.id) } private var composerDraft: FeatureComposerDraft { diff --git a/apps/swift-ios/Tests/FeatureTests/DailyUXNewTaskTests.swift b/apps/swift-ios/Tests/FeatureTests/DailyUXNewTaskTests.swift index 902344efbc..4dec580197 100644 --- a/apps/swift-ios/Tests/FeatureTests/DailyUXNewTaskTests.swift +++ b/apps/swift-ios/Tests/FeatureTests/DailyUXNewTaskTests.swift @@ -226,6 +226,149 @@ struct DailyUXNewTaskTests { ) } + @Test + func projectGroupsOnlyOfferComputersThatContainTheSelectedRepository() throws { + let identity = FeatureRepositoryIdentity( + canonicalKey: "github.com/t3/example", + displayName: "Example" + ) + let studio = FeatureProject( + id: "example-studio", + environmentID: "studio", + name: "example", + path: "/code/example", + repositoryIdentity: identity + ) + let laptop = FeatureProject( + id: "example-laptop", + environmentID: "laptop", + name: "example-copy", + path: "/Users/test/example", + repositoryIdentity: identity + ) + let unrelated = FeatureProject( + id: "other-laptop", + environmentID: "laptop", + name: "other", + path: "/Users/test/other", + repositoryIdentity: .init(canonicalKey: "github.com/t3/other") + ) + + let groups = DailyUXProjectGrouping.groups(projects: [studio, unrelated, laptop]) + let group = try #require( + DailyUXProjectGrouping.group(containing: studio.id, in: groups) + ) + + #expect(group.name == "Example") + #expect(Set(group.projects.map(\.environmentID)) == ["studio", "laptop"]) + #expect(group.project(in: "laptop")?.id == laptop.id) + #expect(!group.memberProjectIDs.contains(unrelated.id)) + #expect(DailyUXProjectGrouping.logicalProjectID(for: studio) == group.id) + } + + @Test + func projectsWithoutRepositoryIdentityNeverGroupAcrossComputers() { + let studio = FeatureProject( + id: "studio", + environmentID: "studio", + name: "Same name", + path: "/code/project" + ) + let laptop = FeatureProject( + id: "laptop", + environmentID: "laptop", + name: "Same name", + path: "/code/project" + ) + + let groups = DailyUXProjectGrouping.groups(projects: [studio, laptop]) + + #expect(groups.count == 2) + #expect(groups.allSatisfy { $0.projects.count == 1 }) + } + + @Test + func projectGroupingUsesFreshestPhysicalRowAndNormalizesTrailingSlash() throws { + let identity = FeatureRepositoryIdentity(canonicalKey: "github.com/t3/example") + let stale = FeatureProject( + id: "stale", + environmentID: "studio", + name: "stale", + path: "/code/example/", + repositoryIdentity: nil, + updatedAt: "2026-01-01T00:00:00.000Z" + ) + let current = FeatureProject( + id: "current", + environmentID: "studio", + name: "current", + path: "/code/example", + repositoryIdentity: identity, + updatedAt: "2026-01-02T00:00:00.000Z" + ) + let remote = FeatureProject( + id: "remote", + environmentID: "remote", + name: "remote", + path: "/srv/example", + repositoryIdentity: identity + ) + + let groups = DailyUXProjectGrouping.groups(projects: [stale, current, remote]) + let group = try #require( + DailyUXProjectGrouping.group(containing: stale.id, in: groups) + ) + + #expect(group.projects.map(\.id) == ["remote", "current"]) + #expect(group.memberProjectIDs == ["stale", "current", "remote"]) + } + + @Test + func logicalProjectDraftKeyDoesNotChangeWithComputer() { + let projectKey = "github.com/t3/example" + + #expect( + FeatureComposerDraftStore.newTaskKey(logicalProjectID: projectKey) + == "logical-project:github.com/t3/example:new-task" + ) + } + + @Test + func projectGroupingHonorsRepositoryPathAndSeparateModes() { + let identity = FeatureRepositoryIdentity( + canonicalKey: "github.com/t3/mono", + rootPath: "/code/mono" + ) + let app = FeatureProject( + id: "app", + environmentID: "studio", + name: "app", + path: "/code/mono/apps/app", + repositoryIdentity: identity + ) + let docs = FeatureProject( + id: "docs", + environmentID: "studio", + name: "docs", + path: "/code/mono/apps/docs", + repositoryIdentity: identity + ) + + #expect(DailyUXProjectGrouping.groups(projects: [app, docs]).count == 1) + #expect( + DailyUXProjectGrouping.groups( + projects: [app, docs], + mode: .repositoryPath + ).count == 2 + ) + #expect( + DailyUXProjectGrouping.groups( + projects: [app, docs], + mode: .separate + ).count == 2 + ) + } + @Test func appDefaultWinsAndExplicitModelCarriesAcrossCompatibleProjects() throws { let appDefault = FeatureSelection(providerID: "codex", modelID: "gpt-5.6-sol") diff --git a/apps/swift-ios/Tests/FeatureTests/NativeMultiEnvironmentTests.swift b/apps/swift-ios/Tests/FeatureTests/NativeMultiEnvironmentTests.swift index 827f28ca73..d9747120e0 100644 --- a/apps/swift-ios/Tests/FeatureTests/NativeMultiEnvironmentTests.swift +++ b/apps/swift-ios/Tests/FeatureTests/NativeMultiEnvironmentTests.swift @@ -112,6 +112,34 @@ final class NativeMultiEnvironmentTests: XCTestCase { await fixture.client.disconnect() } + func testSnapshotKeepsRepositoryIdentityForCrossComputerProjectGrouping() async throws { + let identity = RepositoryIdentity( + canonicalKey: "github.com/t3/example", + locator: .init( + source: "git-remote", + remoteName: "origin", + remoteUrl: "https://github.com/t3/example.git" + ), + rootPath: "/work/example", + displayName: "Example", + provider: "github", + owner: "t3", + name: "example" + ) + let fixture = try await makeFixture(repositoryIdentity: identity) + defer { try? FileManager.default.removeItem(at: fixture.directory) } + + let snapshot = try await fixture.client.initialSnapshot() + let groups = DailyUXCreationContext.projectGroups(in: snapshot) + + XCTAssertEqual(Set(snapshot.projects.compactMap(\.repositoryIdentity?.canonicalKey)), [ + identity.canonicalKey, + ]) + XCTAssertEqual(groups.count, 1) + XCTAssertEqual(Set(groups[0].projects.map(\.environmentID)), ["one", "two"]) + await fixture.client.disconnect() + } + func testFailedEnvironmentKeepsItsLastKnownRowsWithoutHidingHealthyDevices() async throws { let fixture = try await makeFixture() defer { try? FileManager.default.removeItem(at: fixture.directory) } @@ -351,6 +379,7 @@ final class NativeMultiEnvironmentTests: XCTestCase { private func makeFixture( duplicateIDs: Bool = false, + repositoryIdentity: RepositoryIdentity? = nil, fallbackPollingInitialDelay: Duration = .seconds(3), fallbackPollingInterval: Duration = .seconds(2), aggregateRefreshInterval: Duration = .seconds(20), @@ -384,14 +413,16 @@ final class NativeMultiEnvironmentTests: XCTestCase { "one.example": multiEnvironmentShell( projectID: duplicateIDs ? "project-shared" : "project-one", threadID: duplicateIDs ? "thread-shared" : "thread-one", - title: "Local work" + title: "Local work", + repositoryIdentity: repositoryIdentity ), "two.example": multiEnvironmentShell( projectID: duplicateIDs ? "project-shared" : "project-two", threadID: duplicateIDs ? "thread-shared" : "thread-two", title: "Remote work", providerID: "claudeAgent", - modelID: "claude-opus-4-1" + modelID: "claude-opus-4-1", + repositoryIdentity: repositoryIdentity ), ] ) @@ -660,7 +691,8 @@ private func multiEnvironmentShell( threadID: String, title: String, providerID: String = "codex", - modelID: String = "gpt-5.6-sol" + modelID: String = "gpt-5.6-sol", + repositoryIdentity: RepositoryIdentity? = nil ) -> OrchestrationShellSnapshot { let timestamp = "2026-07-31T12:00:00.000Z" let model = ModelSelection(instanceId: providerID, model: modelID) @@ -671,7 +703,7 @@ private func multiEnvironmentShell( id: projectID, title: title, workspaceRoot: "/work/\(projectID)", - repositoryIdentity: nil, + repositoryIdentity: repositoryIdentity, defaultModelSelection: model, scripts: [], createdAt: timestamp,