diff --git a/apple/TablePro/Core/CloudSQL/CloudSQLProxyManager.swift b/apple/TablePro/Core/CloudSQL/CloudSQLProxyManager.swift index 8938447e4e..aced8fc7d8 100644 --- a/apple/TablePro/Core/CloudSQL/CloudSQLProxyManager.swift +++ b/apple/TablePro/Core/CloudSQL/CloudSQLProxyManager.swift @@ -28,6 +28,7 @@ actor CloudSQLProxyManager: TunnelManaging { } private var tunnels: [UUID: TunnelState] = [:] + private var creations = TunnelCreationGenerations() private var pidRecords: [UUID: CloudSQLProxyPidRecord] = [:] private let runnerFactory: () -> any SupervisedProcessRunner private let managedBinary: @Sendable () async throws -> String @@ -65,16 +66,18 @@ actor CloudSQLProxyManager: TunnelManaging { deadline: ConnectionDeadline = ConnectionDeadline(configuredSeconds: nil) ) async throws -> Int { guard config.isValid else { throw CloudSQLProxyError.invalidInstanceConnectionName } + let generation = creations.begin(connectionId) + defer { creations.finish(generation, for: connectionId) } let timeoutEndpoint = ConnectionTimeoutEndpoint.proxy(config.instanceConnectionName) try deadline.check(endpoint: timeoutEndpoint) - /// A `cloud-sql-proxy` a crashed session left behind still owns the port this is about to - /// ask for, and a configuration with a fixed `localPort` gets exactly one attempt at it. + // A `cloud-sql-proxy` a crashed session left behind still owns the port this is about to + // ask for, and a configuration with a fixed `localPort` gets exactly one attempt at it. await sweepStalePidsIfNeeded() try deadline.check(endpoint: timeoutEndpoint) if tunnels[connectionId] != nil { - try await closeTunnel(connectionId: connectionId) + try await discardTunnel(connectionId: connectionId) } let binaryPath = try await resolveBinaryPath(config: config) @@ -154,6 +157,12 @@ actor CloudSQLProxyManager: TunnelManaging { throw error } + // A newer createTunnel or a close for this connection came while this one dialed, so it must not adopt. + guard creations.isNewest(generation, for: connectionId) else { + runner.stop() + deleteCredentialsFile(at: credentialsFilePath) + throw CancellationError() + } register( connectionId: connectionId, runner: runner, @@ -169,7 +178,13 @@ actor CloudSQLProxyManager: TunnelManaging { throw lastError } + // Also ends a creation still dialing, so nothing it builds is adopted after the close returned. func closeTunnel(connectionId: UUID) async throws { + creations.invalidate(connectionId) + try await discardTunnel(connectionId: connectionId) + } + + private func discardTunnel(connectionId: UUID) async throws { guard let state = tunnels.removeValue(forKey: connectionId) else { return } Self.runnerRegistry.withLock { $0[connectionId] = nil } pidRecords.removeValue(forKey: connectionId) @@ -180,6 +195,7 @@ actor CloudSQLProxyManager: TunnelManaging { } func closeAllTunnels() async { + creations.invalidateAll() let current = tunnels tunnels.removeAll() pidRecords.removeAll() @@ -368,7 +384,8 @@ actor CloudSQLProxyManager: TunnelManaging { throw CloudSQLProxyError.credentialsWriteFailed } let url = FileManager.default.temporaryDirectory - .appendingPathComponent("\(Self.credentialsFilePrefix)\(connectionId.uuidString).json") + // One file per creation: two creations for one connection would otherwise share and delete one path. + .appendingPathComponent("\(Self.credentialsFilePrefix)\(connectionId.uuidString)-\(UUID().uuidString).json") do { try data.write(to: url, options: .atomic) try FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: url.path) diff --git a/apple/TablePro/Core/Cloudflare/CloudflareTunnelManager.swift b/apple/TablePro/Core/Cloudflare/CloudflareTunnelManager.swift index 4dbed3d4a4..9037c2d32f 100644 --- a/apple/TablePro/Core/Cloudflare/CloudflareTunnelManager.swift +++ b/apple/TablePro/Core/Cloudflare/CloudflareTunnelManager.swift @@ -24,6 +24,7 @@ actor CloudflareTunnelManager: TunnelManaging { } private var tunnels: [UUID: TunnelState] = [:] + private var creations = TunnelCreationGenerations() private var pidRecords: [UUID: CloudflaredPidRecord] = [:] private let runnerFactory: () -> any SupervisedProcessRunner private var staleSweep: Task? @@ -43,8 +44,6 @@ actor CloudflareTunnelManager: TunnelManaging { self.reaperTimings = reaperTimings } - /// Create a Cloudflare Access TCP tunnel for a database connection. - /// Returns the local loopback port the database driver should connect to. func createTunnel( connectionId: UUID, config: CloudflareConfiguration, @@ -52,15 +51,17 @@ actor CloudflareTunnelManager: TunnelManaging { tokenSecret: String? = nil, deadline: ConnectionDeadline = ConnectionDeadline(configuredSeconds: nil) ) async throws -> Int { + let generation = creations.begin(connectionId) + defer { creations.finish(generation, for: connectionId) } let timeoutEndpoint = ConnectionTimeoutEndpoint.tunnel(config.accessHostname) try deadline.check(endpoint: timeoutEndpoint) - /// A `cloudflared` a crashed session left behind still owns the port this is about to ask - /// for, and a configuration with a fixed `localPort` gets exactly one attempt at it. + // A `cloudflared` a crashed session left behind still owns the port this is about to ask + // for, and a configuration with a fixed `localPort` gets exactly one attempt at it. await sweepStalePidsIfNeeded() try deadline.check(endpoint: timeoutEndpoint) if tunnels[connectionId] != nil { - try await closeTunnel(connectionId: connectionId) + try await discardTunnel(connectionId: connectionId) } let binaryPath = try resolveBinaryPath(config: config) @@ -114,6 +115,11 @@ actor CloudflareTunnelManager: TunnelManaging { throw error } + // A newer createTunnel or a close for this connection came while this one dialed, so it must not adopt. + guard creations.isNewest(generation, for: connectionId) else { + runner.stop() + throw CancellationError() + } register(connectionId: connectionId, runner: runner, port: port, binaryPath: binaryPath) Self.logger.info("Cloudflare tunnel ready for \(connectionId.uuidString, privacy: .public) on 127.0.0.1:\(port)") return port @@ -122,7 +128,13 @@ actor CloudflareTunnelManager: TunnelManaging { throw lastError } + // Also ends a creation still dialing, so nothing it builds is adopted after the close returned. func closeTunnel(connectionId: UUID) async throws { + creations.invalidate(connectionId) + try await discardTunnel(connectionId: connectionId) + } + + private func discardTunnel(connectionId: UUID) async throws { guard let state = tunnels.removeValue(forKey: connectionId) else { return } Self.runnerRegistry.withLock { $0[connectionId] = nil } pidRecords.removeValue(forKey: connectionId) @@ -132,6 +144,7 @@ actor CloudflareTunnelManager: TunnelManaging { } func closeAllTunnels() async { + creations.invalidateAll() let current = tunnels tunnels.removeAll() pidRecords.removeAll() diff --git a/apple/TablePro/Core/DataWrite/Rewind/RewindAvailability.swift b/apple/TablePro/Core/DataWrite/Rewind/RewindAvailability.swift new file mode 100644 index 0000000000..83b786233f --- /dev/null +++ b/apple/TablePro/Core/DataWrite/Rewind/RewindAvailability.swift @@ -0,0 +1,53 @@ +// +// RewindAvailability.swift +// TablePro +// + +import Combine +import Foundation + +struct RewindAvailability: Equatable, Sendable { + struct Target: Hashable, Sendable { + let connectionId: UUID + let database: String + let schema: String? + let table: String + } + + let revision: Int + let targets: Set + let historyIds: Set + + static let empty = RewindAvailability(revision: -1, targets: [], historyIds: []) +} + +// Menu and toolbar validation is synchronous, so the main actor keeps the history actor's last answer. +@MainActor +final class RewindAvailabilityCache: ObservableObject { + @Published private(set) var availability = RewindAvailability.empty + + nonisolated init(storage: QueryHistoryStorage) { + // Weak, so the subscription never keeps the store alive: its deinit finishes the stream. + Task { @MainActor [weak self, weak storage] in + guard let updates = await storage?.rewindAvailabilityUpdates() else { return } + for await next in updates { + guard let self else { return } + adopt(next) + } + } + } + + func hasSnapshot(for target: RewindAvailability.Target) -> Bool { + availability.targets.contains(target) + } + + func hasSnapshot(forHistoryId historyId: UUID) -> Bool { + availability.historyIds.contains(historyId) + } + + // Readings can reach the main actor out of order, so only a newer revision replaces the current one. + func adopt(_ next: RewindAvailability) { + guard next.revision > availability.revision else { return } + availability = next + } +} diff --git a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift index 7658309309..ec818feb5c 100644 --- a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift +++ b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift @@ -88,10 +88,27 @@ internal actor DatabaseAccessBridge { internal func disconnect(connectionId: UUID) async throws { let manager = await databaseManager - let sessionExists = await MainActor.run { - manager.activeSessions[connectionId] != nil + try await Self.disconnectUnlessWorkIsLost(connectionId, manager: manager) + } + + // TablePro asks the user before a disconnect that loses work; a caller that is not a person gets a refusal. + @MainActor + private static func disconnectUnlessWorkIsLost(_ connectionId: UUID, manager: DatabaseManager) async throws { + guard manager.activeSessions[connectionId] != nil else { throw DatabaseAccessError.notConnected(connectionId) } + let turnRevisionBefore = manager.sessionTransactions.turnRevision(for: connectionId) + let probed = await manager.transactionSafety(for: connectionId) + // Read after the probe and with no suspension before the disconnect, so work that started meanwhile counts. + guard manager.activeSessions[connectionId] != nil else { throw DatabaseAccessError.notConnected(connectionId) } + let transactions = manager.settledTransactionSafety( + probed, for: connectionId, turnRevisionBefore: turnRevisionBefore + ) + if let refusal = ExternalDisconnectRefusal.message( + work: RunningWorkRegistry.shared.items(in: .connection(connectionId)), + transactions: transactions, + hasUnsavedWork: MainContentCoordinator.hasUnsavedWork(forConnection: connectionId) + ) { + throw DatabaseAccessError.forbidden(refusal) } - guard sessionExists else { throw DatabaseAccessError.notConnected(connectionId) } await manager.disconnectSession(connectionId) } @@ -694,3 +711,62 @@ internal actor DatabaseAccessBridge { internal protocol StatementCancellationSignal: Sendable { func onCancelRequested(_ handler: @escaping @Sendable () async -> Void) async } + +// Kinds only: a line naming the work could name a tab, a file or another connection the caller cannot see. +@MainActor +internal enum ExternalDisconnectRefusal { + internal enum Loss: Hashable, CaseIterable { + case query + case transfer + case agentReply + case comparison + case transaction + case unsavedChanges + + internal var noun: String { + switch self { + case .query: return String(localized: "a running query") + case .transfer: return String(localized: "an import, export, backup, restore or copy") + case .agentReply: return String(localized: "an agent reply") + case .comparison: return String(localized: "a comparison or sync") + case .transaction: return String(localized: "an open transaction") + case .unsavedChanges: return String(localized: "unsaved changes") + } + } + } + + internal static func message( + work: [RunningWork], + transactions: SessionTransactionSafety, + hasUnsavedWork: Bool + ) -> String? { + var losses = Set(work.map { loss(for: $0.kind) }) + if transactions == .holding { losses.insert(.transaction) } + if hasUnsavedWork { losses.insert(.unsavedChanges) } + if let message = message(for: losses) { return message } + guard transactions == .unknown else { return nil } + return String( + localized: "TablePro cannot tell whether this connection holds an open transaction, so it stays connected. Try again once its work is done, or disconnect in TablePro." + ) + } + + internal static func message(for losses: Set) -> String? { + let nouns = Loss.allCases.filter { losses.contains($0) }.map { $0.noun } + guard !nouns.isEmpty else { return nil } + return String( + format: String( + localized: "This connection has %@ in TablePro, which disconnecting would stop or discard. Finish it first, or disconnect in TablePro, which asks the user." + ), + ListFormatter.localizedString(byJoining: nouns) + ) + } + + private static func loss(for kind: RunningWorkKind) -> Loss { + switch kind { + case .query: return .query + case .restore, .dataImport, .dataExport, .backup, .tableTransfer, .objectCopy: return .transfer + case .agentReply: return .agentReply + case .compareRun, .compareApply: return .comparison + } + } +} diff --git a/apple/TablePro/Core/Database/DatabaseManager+OpenTransactions.swift b/apple/TablePro/Core/Database/DatabaseManager+OpenTransactions.swift index a9c8af58a9..cdd8268df7 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+OpenTransactions.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+OpenTransactions.swift @@ -47,6 +47,57 @@ extension DatabaseManager { return holding } + // Unlike the probe above, a busy session, a timeout or an engine that cannot say is never read as safe. + // The probe suspends, so a lane can take a turn meanwhile; what the tracker learned since, or a turn still + // running, outranks a stale idle answer. + internal func settledTransactionSafety( + _ probed: SessionTransactionSafety, + for connectionId: UUID, + turnRevisionBefore: Int + ) -> SessionTransactionSafety { + guard activeSessions[connectionId]?.driver?.supportsTransactions ?? true else { return probed } + if probed == .holding || !sessionTransactions.holds(for: connectionId).isEmpty { return .holding } + guard probed == .noTransaction else { return probed } + let laneKeys = [browsedGateKey(for: connectionId)] + sessionLanes.parkedDatabases(for: connectionId).map { + SessionDriverGate.Key(connectionId: connectionId, database: $0) + } + if laneKeys.contains(where: { sessionDriverGate.isHeld($0) }) { return .unknown } + return sessionTransactions.turnRevision(for: connectionId) == turnRevisionBefore ? .noTransaction : .unknown + } + + internal func transactionSafety( + for connectionId: UUID, + clock: any Clock = ContinuousClock() + ) async -> SessionTransactionSafety { + guard let session = activeSessions[connectionId] else { return .noTransaction } + guard session.driver?.supportsTransactions ?? true else { return .noTransaction } + var drivers: [DatabaseDriver] = [] + var isBusy = false + if let driver = session.driver { + if sessionDriverGate.isHeld(browsedGateKey(for: connectionId)) { isBusy = true } else { drivers.append(driver) } + } + for database in sessionLanes.parkedDatabases(for: connectionId).sorted() { + guard let driver = sessionLanes.parkedDriver(for: connectionId, database: database) else { continue } + let key = SessionDriverGate.Key(connectionId: connectionId, database: database) + if sessionDriverGate.isHeld(key) { isBusy = true } else { drivers.append(driver) } + } + let reads = drivers.map { driver in Task { await Self.boundedTransactionState(of: driver, clock: clock) } } + var safety: SessionTransactionSafety = isBusy ? .unknown : .noTransaction + for read in reads { + switch await read.value { + case .inTransaction, .abortedTransaction: + safety = .holding + case .idle, .holdsSessionLocks: + break + case .unknown: + if safety == .noTransaction { safety = .unknown } + @unknown default: + if safety == .noTransaction { safety = .unknown } + } + } + return safety + } + /// Ends the transaction a hold shows, on the connection that holds it. Refused when that /// connection was replaced, or when other work ran there after the hold was read: the user /// confirmed what they saw, not a transaction someone opened since. @@ -122,3 +173,10 @@ internal enum SessionTransactionActionError: Error, LocalizedError, Equatable { } } } + +internal enum SessionTransactionSafety: Equatable, Sendable { + case noTransaction + case holding + // Busy, timed out, or an engine that cannot report its transaction state. + case unknown +} diff --git a/apple/TablePro/Core/Database/SessionTransactionTracker.swift b/apple/TablePro/Core/Database/SessionTransactionTracker.swift index f0490ec38b..1cd91819ea 100644 --- a/apple/TablePro/Core/Database/SessionTransactionTracker.swift +++ b/apple/TablePro/Core/Database/SessionTransactionTracker.swift @@ -73,6 +73,13 @@ internal final class SessionTransactionTracker: ObservableObject { return states.isEmpty ? nil : .open } + // Moves with every turn recorded on any of the connection's lanes. + internal func turnRevision(for connectionId: UUID) -> Int { + revisions.reduce(0) { total, entry in + entry.key.connectionId == connectionId ? total &+ entry.value : total + } + } + internal func driver(for hold: Hold) -> DatabaseDriver? { guard let entry = drivers[hold.key], let driver = entry.driver as? DatabaseDriver else { return nil } return driver diff --git a/apple/TablePro/Core/Database/TunnelCreationGenerations.swift b/apple/TablePro/Core/Database/TunnelCreationGenerations.swift new file mode 100644 index 0000000000..662b59c5d7 --- /dev/null +++ b/apple/TablePro/Core/Database/TunnelCreationGenerations.swift @@ -0,0 +1,35 @@ +// +// TunnelCreationGenerations.swift +// TablePro +// + +import Foundation + +// A manager's createTunnel suspends while it dials, so two calls for one connection interleave; only the newest adopts. +internal struct TunnelCreationGenerations: Sendable { + private var lastIssued: UInt64 = 0 + private var newest: [UUID: UInt64] = [:] + + internal mutating func begin(_ connectionId: UUID) -> UInt64 { + lastIssued &+= 1 + newest[connectionId] = lastIssued + return lastIssued + } + + internal func isNewest(_ generation: UInt64, for connectionId: UUID) -> Bool { + newest[connectionId] == generation + } + + internal mutating func finish(_ generation: UInt64, for connectionId: UUID) { + guard newest[connectionId] == generation else { return } + newest[connectionId] = nil + } + + internal mutating func invalidate(_ connectionId: UUID) { + newest[connectionId] = nil + } + + internal mutating func invalidateAll() { + newest.removeAll() + } +} diff --git a/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteTransportManager.swift b/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteTransportManager.swift index c254abf4fe..d44e922406 100644 --- a/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteTransportManager.swift +++ b/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteTransportManager.swift @@ -24,6 +24,7 @@ actor RemoteSQLiteTransportManager: TunnelManaging { private static let portRangeEnd = 65_000 private var tunnels: [UUID: RemoteSQLiteTunnel] = [:] + private var creations = TunnelCreationGenerations() private static let tunnelRegistry = OSAllocatedUnfairLock(initialState: [UUID: RemoteSQLiteTunnel]()) private var appNapActivity: NSObjectProtocol? @@ -35,17 +36,19 @@ actor RemoteSQLiteTransportManager: TunnelManaging { credentials: SSHTunnelCredentials, deadline: ConnectionDeadline = ConnectionDeadline(configuredSeconds: nil) ) async throws -> RemoteSQLiteEndpoint { + let generation = creations.begin(connectionId) + defer { creations.finish(generation, for: connectionId) } if tunnels[connectionId] != nil { - try await closeTunnel(connectionId: connectionId) + try await discardTunnel(connectionId: connectionId) } let token = RemoteSQLiteAdmission.newToken() let command = RemoteSQLiteAgentSource.launcherCommand() let candidatePorts = localPortCandidates() - /// The tunnel is built inside the detached task and only it, not the authenticated chain of - /// raw libssh2 handles it holds, crosses back to the actor. That is what `LibSSH2Tunnel`'s - /// factory does too, and it is what keeps the non-Sendable session out of an actor hop. + // The tunnel is built inside the detached task and only it, not the authenticated chain of + // raw libssh2 handles it holds, crosses back to the actor. That is what `LibSSH2Tunnel`'s + // factory does too, and it is what keeps the non-Sendable session out of an actor hop. let creationTask = Task.detached { let endpoint = ConnectionTimeoutEndpoint.tunnel("\(config.host):\(config.port ?? 22)") let attempt = SSHConnectionAttempt(deadline: deadline, endpoint: endpoint) @@ -101,6 +104,11 @@ actor RemoteSQLiteTransportManager: TunnelManaging { tunnel.close() throw error } + // A newer createTunnel or a close for this connection came while this one dialed, so it must not adopt. + guard creations.isNewest(generation, for: connectionId) else { + tunnel.close() + throw CancellationError() + } tunnel.onDeath = { [weak self] id in Task { [weak self] in await self?.handleTunnelDeath(connectionId: id) } @@ -116,7 +124,13 @@ actor RemoteSQLiteTransportManager: TunnelManaging { return RemoteSQLiteEndpoint(port: tunnel.localPort, token: token) } + // Also ends a creation still dialing, so nothing it builds is adopted after the close returned. func closeTunnel(connectionId: UUID) async throws { + creations.invalidate(connectionId) + try await discardTunnel(connectionId: connectionId) + } + + private func discardTunnel(connectionId: UUID) async throws { guard let tunnel = tunnels.removeValue(forKey: connectionId) else { return } Self.tunnelRegistry.withLock { $0[connectionId] = nil } updateAppNapState() diff --git a/apple/TablePro/Core/SOCKS/SOCKSProxyManager.swift b/apple/TablePro/Core/SOCKS/SOCKSProxyManager.swift index 4c2ed607ef..fcbe61021c 100644 --- a/apple/TablePro/Core/SOCKS/SOCKSProxyManager.swift +++ b/apple/TablePro/Core/SOCKS/SOCKSProxyManager.swift @@ -71,6 +71,7 @@ actor SOCKSProxyManager: TunnelManaging { } private var tunnels: [UUID: TunnelState] = [:] + private var creations = TunnelCreationGenerations() private var appNapActivity: NSObjectProtocol? private let connectTimeout: TimeInterval private let now: @Sendable () -> ContinuousClock.Instant @@ -112,15 +113,17 @@ actor SOCKSProxyManager: TunnelManaging { guard config.isValid, Self.nwPort(config.port) != nil, Self.nwPort(targetPort) != nil else { throw SOCKSProxyError.invalidConfiguration } + let generation = creations.begin(connectionId) + defer { creations.finish(generation, for: connectionId) } let timeoutEndpoint = ConnectionTimeoutEndpoint.proxy("\(config.host):\(config.port)") try deadline.check(endpoint: timeoutEndpoint, at: now()) if tunnels[connectionId] != nil { - try await closeTunnel(connectionId: connectionId) + try await discardTunnel(connectionId: connectionId) } - /// `ProxyConfiguration` is macOS 14. Network.framework offers no SOCKS5 path before - /// it, so the connection is refused with a reason rather than failing obscurely. + // `ProxyConfiguration` is macOS 14. Network.framework offers no SOCKS5 path before + // it, so the connection is refused with a reason rather than failing obscurely. guard #available(macOS 14.0, *) else { throw SOCKSProxyError.unsupportedOnThisSystem } @@ -170,6 +173,12 @@ actor SOCKSProxyManager: TunnelManaging { listener.cancel() throw error } + // A newer createTunnel or a close for this connection came while this one dialed, so it must not adopt. + guard creations.isNewest(generation, for: connectionId) else { + listener.stateUpdateHandler = nil + listener.cancel() + throw CancellationError() + } let state = TunnelState( listener: listener, localPort: localPort, @@ -183,7 +192,13 @@ actor SOCKSProxyManager: TunnelManaging { return localPort } + // Also ends a creation still dialing, so nothing it builds is adopted after the close returned. func closeTunnel(connectionId: UUID) async throws { + creations.invalidate(connectionId) + try await discardTunnel(connectionId: connectionId) + } + + private func discardTunnel(connectionId: UUID) async throws { guard let state = tunnels.removeValue(forKey: connectionId) else { return } updateAppNapState() state.listener.stateUpdateHandler = nil @@ -195,6 +210,7 @@ actor SOCKSProxyManager: TunnelManaging { } func closeAllTunnels() async { + creations.invalidateAll() let connectionIds = Array(tunnels.keys) for connectionId in connectionIds { try? await closeTunnel(connectionId: connectionId) diff --git a/apple/TablePro/Core/SSH/SSHTunnelManager.swift b/apple/TablePro/Core/SSH/SSHTunnelManager.swift index 4d09111e4f..553ea82277 100644 --- a/apple/TablePro/Core/SSH/SSHTunnelManager.swift +++ b/apple/TablePro/Core/SSH/SSHTunnelManager.swift @@ -17,6 +17,7 @@ actor SSHTunnelManager: TunnelManaging { private static let logger = Logger(subsystem: "com.TablePro", category: "SSHTunnelManager") private var tunnels: [UUID: LibSSH2Tunnel] = [:] + private var creations = TunnelCreationGenerations() private let portRangeStart = 60_000 private let portRangeEnd = 65_000 @@ -29,7 +30,6 @@ actor SSHTunnelManager: TunnelManaging { private init() {} - /// Create an SSH tunnel for a database connection. func createTunnel( connectionId: UUID, sshHost: String, @@ -49,10 +49,12 @@ actor SSHTunnelManager: TunnelManaging { totpPeriod: Int = 30, deadline: ConnectionDeadline = ConnectionDeadline(configuredSeconds: nil) ) async throws -> Int { + let generation = creations.begin(connectionId) + defer { creations.finish(generation, for: connectionId) } let timeoutEndpoint = ConnectionTimeoutEndpoint.tunnel("\(sshHost):\(sshPort ?? 22)") try deadline.check(endpoint: timeoutEndpoint) if tunnels[connectionId] != nil { - try await closeTunnel(connectionId: connectionId) + try await discardTunnel(connectionId: connectionId) } let config = SSHConfiguration( @@ -103,6 +105,11 @@ actor SSHTunnelManager: TunnelManaging { tunnel.close() throw error } + // A newer createTunnel or a close for this connection came while this one dialed, so it must not adopt. + guard creations.isNewest(generation, for: connectionId) else { + tunnel.close() + throw CancellationError() + } tunnel.onDeath = { [weak self] id in Task { [weak self] in @@ -132,16 +139,21 @@ actor SSHTunnelManager: TunnelManaging { throw SSHTunnelError.noAvailablePort } - /// Close an SSH tunnel + // Also ends a creation still dialing, so nothing it builds is adopted after the close returned. func closeTunnel(connectionId: UUID) async throws { + creations.invalidate(connectionId) + try await discardTunnel(connectionId: connectionId) + } + + private func discardTunnel(connectionId: UUID) async throws { guard let tunnel = tunnels.removeValue(forKey: connectionId) else { return } Self.tunnelRegistry.withLock { $0[connectionId] = nil } updateAppNapState() tunnel.close() } - /// Close all SSH tunnels func closeAllTunnels() async { + creations.invalidateAll() let currentTunnels = tunnels tunnels.removeAll() Self.tunnelRegistry.withLock { $0.removeAll(); return } diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift index 73c2f1b806..867911de50 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift @@ -858,17 +858,22 @@ internal final class MainSplitViewController: NSSplitViewController { if let selected = workspaces.selected { bindSidebarChrome(to: selected) } } - /// Parents the sidebar and detail trees the selected connection's mode draws, and does nothing - /// else, so a mode toggle is the same view swap a workspace switch is. - /// - /// Parenting happens here and in no builder, so a connection put into Agent mode while another - /// is on screen has its panes built for the mode at once and parented only when it is selected: - /// `applySelectedWorkspace` calls through here after its sync, which is the same repair a - /// background connect relies on (#2545). + // The only place panes are parented, so a mode toggle is the same view swap a workspace switch is (#2545). func showSelectedContentPanes() { let selected = workspaces.selected navigationSidebar?.objectBrowser.show(selected.map { $0.panes.sidebarPane(for: $0.resolvedContentMode) }) detailPaneHost?.show(selected.map { $0.panes.detailPane(for: $0.detailMode) }) + if let selected { clearShownTabMark(of: selected) } + } + + // A result that finished behind Agent mode or another connection marked the selected tab; it is in view now. + private func clearShownTabMark(of workspace: ConnectionWorkspace) { + guard workspace.resolvedPane == .content, + workspace.detailMode == .browse, + let tabManager = workspace.sessionState?.tabManager, + let tabId = tabManager.selectedTabId + else { return } + OperationUnseenMarker.clear(tabId: tabId, in: tabManager) } /// The scope control and the filter field live above the object list and belong to the window, diff --git a/apple/TablePro/Core/Services/Infrastructure/MainWindowToolbar.swift b/apple/TablePro/Core/Services/Infrastructure/MainWindowToolbar.swift index be3598b496..ba1335db96 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainWindowToolbar.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainWindowToolbar.swift @@ -11,6 +11,7 @@ import os internal final class MainWindowToolbar: NSObject, NSToolbarDelegate { private var itemStateObservation: AnyCancellable? private var tabStateObservation: AnyCancellable? + private var rewindAvailabilityObservation: AnyCancellable? nonisolated internal static let lifecycleLogger = Logger(subsystem: "com.TablePro", category: "NativeTabLifecycle") /// The autosave name, and deliberately not bumped when the default set changes. @@ -204,6 +205,7 @@ internal final class MainWindowToolbar: NSObject, NSToolbarDelegate { itemStateObservationGeneration += 1 itemStateObservation = nil tabStateObservation = nil + rewindAvailabilityObservation = nil coordinator = nil } @@ -217,6 +219,7 @@ internal final class MainWindowToolbar: NSObject, NSToolbarDelegate { private func observeItemState() { itemStateObservation = nil tabStateObservation = nil + rewindAvailabilityObservation = nil guard let coordinator else { return } let generation = itemStateObservationGeneration let coordinatorIdentifier = ObjectIdentifier(coordinator) @@ -232,6 +235,11 @@ internal final class MainWindowToolbar: NSObject, NSToolbarDelegate { guard let self, self.isObserving(generation, coordinatorIdentifier) else { return } self.refreshContext() } + rewindAvailabilityObservation = coordinator.services.queryHistoryManager.rewindAvailability + .onMainActorChange { [weak self] in + guard let self, self.isObserving(generation, coordinatorIdentifier) else { return } + self.managedToolbar.validateVisibleItems() + } } /// A callback queued before a repoint must not act on the connection that replaced its own. diff --git a/apple/TablePro/Core/Services/Operations/ResultVisibilityResolver.swift b/apple/TablePro/Core/Services/Operations/ResultVisibilityResolver.swift index ccc96009a5..67e91c4e9e 100644 --- a/apple/TablePro/Core/Services/Operations/ResultVisibilityResolver.swift +++ b/apple/TablePro/Core/Services/Operations/ResultVisibilityResolver.swift @@ -53,8 +53,12 @@ internal enum ResultVisibilityResolver { return controller.workspaces.selected?.connectionId } + // Agent mode draws the conversation over the tabs; `showSelectedContentPanes` clears the mark when Browse returns. private static func selectedTabId(in window: NSWindow) -> UUID? { - guard let controller = window.contentViewController as? MainSplitViewController else { return nil } - return controller.workspaces.selected?.sessionState?.tabManager.selectedTab?.id + guard let controller = window.contentViewController as? MainSplitViewController, + let workspace = controller.workspaces.selected, + workspace.detailMode == .browse + else { return nil } + return workspace.sessionState?.tabManager.selectedTab?.id } } diff --git a/apple/TablePro/Core/Services/Operations/RunningWork.swift b/apple/TablePro/Core/Services/Operations/RunningWork.swift index 0052f15686..14515f3dbc 100644 --- a/apple/TablePro/Core/Services/Operations/RunningWork.swift +++ b/apple/TablePro/Core/Services/Operations/RunningWork.swift @@ -24,7 +24,7 @@ internal struct RunningWork: Identifiable { internal let subject: String internal let connectionName: String internal let connectionIds: Set - // Only a query sets these: it belongs to one tab in one window. + // A query sets both; a sheet's job sets the window, so closing another window on its connection leaves it. internal let windowId: UUID? internal let tabId: UUID? internal let cancel: @MainActor () -> Void @@ -92,6 +92,7 @@ internal final class RunningWorkRegistry { subject: String, connectionName: String, connectionIds: Set, + windowId: UUID? = nil, cancel: @escaping @MainActor () -> Void ) -> UUID { let work = RunningWork( @@ -99,6 +100,7 @@ internal final class RunningWorkRegistry { subject: subject, connectionName: connectionName, connectionIds: connectionIds, + windowId: windowId, cancel: cancel ) registered[work.id] = work diff --git a/apple/TablePro/Core/Services/Operations/SheetWork.swift b/apple/TablePro/Core/Services/Operations/SheetWork.swift new file mode 100644 index 0000000000..6ba4d58a58 --- /dev/null +++ b/apple/TablePro/Core/Services/Operations/SheetWork.swift @@ -0,0 +1,78 @@ +// +// SheetWork.swift +// TablePro +// + +import Foundation + +// What a sheet's owner stops when it goes away for good. A pane leaving the window on a connection +// switch or an Agent mode toggle is not that, which is why this is not `onDisappear`. +@MainActor +internal final class SheetWorkScope { + private var stops: [UUID: @MainActor () -> Void] = [:] + private let windowIdProvider: @MainActor () -> UUID? + + internal init(windowId: @escaping @MainActor () -> UUID? = { nil }) { + windowIdProvider = windowId + } + + internal var windowId: UUID? { windowIdProvider() } + + internal var isEmpty: Bool { stops.isEmpty } + + internal func begin(stop: @escaping @MainActor () -> Void) -> UUID { + let id = UUID() + stops[id] = stop + return id + } + + internal func end(_ id: UUID?) { + guard let id else { return } + stops[id] = nil + } + + internal func stopAll() { + let pending = Array(stops.values) + stops.removeAll() + for stop in pending { + stop() + } + } +} + +// One job a sheet started: listed for the closers' running-work question and stopped with the sheet's owner. +@MainActor +internal final class SheetJob { + private let registry: RunningWorkRegistry + private weak var scope: SheetWorkScope? + private let workId: UUID + private let scopeId: UUID? + + internal init( + _ kind: RunningWorkKind, + subject: String, + connectionName: String, + connectionIds: Set, + registry: RunningWorkRegistry = .shared, + scope: SheetWorkScope?, + listedOnlyForItsWindow: Bool = true, + stop: @escaping @MainActor () -> Void + ) { + self.registry = registry + self.scope = scope + workId = registry.begin( + kind, + subject: subject, + connectionName: connectionName, + connectionIds: connectionIds, + windowId: listedOnlyForItsWindow ? scope?.windowId : nil, + cancel: stop + ) + scopeId = scope?.begin(stop: stop) + } + + internal func end() { + registry.end(workId) + scope?.end(scopeId) + } +} diff --git a/apple/TablePro/Core/Storage/QueryHistoryManager.swift b/apple/TablePro/Core/Storage/QueryHistoryManager.swift index 712fdef2dc..1842118b1d 100644 --- a/apple/TablePro/Core/Storage/QueryHistoryManager.swift +++ b/apple/TablePro/Core/Storage/QueryHistoryManager.swift @@ -5,9 +5,9 @@ import TableProPluginKit final class QueryHistoryManager: QueryHistoryRecording, QueryHistoryReading, QueryPlanSnapshotReading, Sendable { static let shared = QueryHistoryManager() - /// Not private: the rewind snapshot API lives in its own extension file rather than growing - /// this one, and an extension in another file cannot reach a private stored property. + // Not private: QueryHistoryManager+Rewind reaches it from another file. internal let storage: QueryHistoryStorage + let rewindAvailability: RewindAvailabilityCache private let isCapturePaused: @Sendable () -> Bool init( @@ -15,6 +15,7 @@ final class QueryHistoryManager: QueryHistoryRecording, QueryHistoryReading, Que isCapturePaused: @escaping @Sendable () -> Bool = { QueryHistoryCaptureStore.isPaused } ) { self.storage = storage + self.rewindAvailability = RewindAvailabilityCache(storage: storage) self.isCapturePaused = isCapturePaused } diff --git a/apple/TablePro/Core/Storage/QueryHistoryStorage+RewindSnapshots.swift b/apple/TablePro/Core/Storage/QueryHistoryStorage+RewindSnapshots.swift index 0a6b924945..32021a571a 100644 --- a/apple/TablePro/Core/Storage/QueryHistoryStorage+RewindSnapshots.swift +++ b/apple/TablePro/Core/Storage/QueryHistoryStorage+RewindSnapshots.swift @@ -111,6 +111,7 @@ extension QueryHistoryStorage { logSqliteError(context: "rewind snapshot insert") return false } + rewindSnapshotsDidChange() return true } @@ -209,24 +210,23 @@ extension QueryHistoryStorage { logSqliteError(context: "rewind snapshot delete") return false } - return sqlite3_changes(db) > 0 + let deleted = sqlite3_changes(db) > 0 + if deleted { rewindSnapshotsDidChange() } + return deleted } @discardableResult func clearRewindSnapshots() -> Bool { guard db != nil else { return false } execute("DELETE FROM rewind_snapshots;") + rewindSnapshotsDidChange() return true } // MARK: - Retention - /// Age, then per-table depth, then total bytes. Runs on query history's own cleanup cadence so - /// a save never pays for a full scan of the table. - /// - /// The byte budget measures what is already kept, excluding the row being judged, so a single - /// save larger than the whole budget is still the one save that survives rather than the one - /// that deletes itself the moment it is written. + // Runs on history's cleanup cadence, so a save never pays for a full scan. The byte budget + // excludes the row being judged, so one save larger than the budget survives its own write. @discardableResult func pruneRewindSnapshots( now: Date = Date(), @@ -294,9 +294,82 @@ extension QueryHistoryStorage { } sqlite3_finalize(byteStatement) + if changed { rewindSnapshotsDidChange() } return changed } + // MARK: - Availability + + // Plain columns only, so knowing a save is kept costs no decrypt. + func rewindAvailability() -> RewindAvailability { + var targets = Set() + var historyIds = Set() + guard let db else { + return RewindAvailability(revision: rewindRevision, targets: targets, historyIds: historyIds) + } + var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } + let sql = """ + SELECT DISTINCT connection_id, database_name, schema_name, table_name, history_id + FROM rewind_snapshots; + """ + guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK else { + logSqliteError(context: "prepare rewind availability") + return RewindAvailability(revision: rewindRevision, targets: targets, historyIds: historyIds) + } + while sqlite3_step(statement) == SQLITE_ROW { + guard let connectionId = Self.rewindUUID(statement, column: 0), + let database = Self.rewindText(statement, column: 1), + let table = Self.rewindText(statement, column: 3) + else { continue } + targets.insert(RewindAvailability.Target( + connectionId: connectionId, + database: database, + schema: Self.rewindText(statement, column: 2), + table: table + )) + if let historyId = Self.rewindUUID(statement, column: 4) { + historyIds.insert(historyId) + } + } + return RewindAvailability(revision: rewindRevision, targets: targets, historyIds: historyIds) + } + + // Yields what is kept now, then again after every change that commits. + func rewindAvailabilityUpdates() -> AsyncStream { + let (stream, continuation) = AsyncStream.makeStream( + of: RewindAvailability.self, bufferingPolicy: .bufferingNewest(1) + ) + let id = UUID() + rewindSubscribers[id] = continuation + continuation.onTermination = { [weak self] _ in + Task { await self?.removeRewindSubscriber(id) } + } + continuation.yield(rewindAvailability()) + return stream + } + + func rewindSnapshotsDidChange() { + rewindRevision += 1 + guard !rewindSubscribers.isEmpty else { return } + let current = rewindAvailability() + for subscriber in rewindSubscribers.values { + subscriber.yield(current) + } + } + + private func removeRewindSubscriber(_ id: UUID) { + rewindSubscribers[id] = nil + } + + private static func rewindText(_ statement: OpaquePointer?, column: Int32) -> String? { + sqlite3_column_text(statement, column).map { String(cString: $0) } + } + + private static func rewindUUID(_ statement: OpaquePointer?, column: Int32) -> UUID? { + rewindText(statement, column: column).flatMap(UUID.init(uuidString:)) + } + // MARK: - Helpers private func decodeRewindPayload( diff --git a/apple/TablePro/Core/Storage/QueryHistoryStorage.swift b/apple/TablePro/Core/Storage/QueryHistoryStorage.swift index 22f9b07b59..14a8966b21 100644 --- a/apple/TablePro/Core/Storage/QueryHistoryStorage.swift +++ b/apple/TablePro/Core/Storage/QueryHistoryStorage.swift @@ -32,6 +32,9 @@ actor QueryHistoryStorage { private var insertsSinceCleanup: Int = 0 private var didBackfillFingerprints = false + var rewindRevision = 0 + var rewindSubscribers: [UUID: AsyncStream.Continuation] = [:] + private let databaseURL: URL private let removeDatabaseOnDeinit: Bool @@ -50,6 +53,9 @@ actor QueryHistoryStorage { } deinit { + for subscriber in rewindSubscribers.values { + subscriber.finish() + } if let pointer = dbHandle.pointer { sqlite3_close_v2(pointer) } @@ -1098,13 +1104,8 @@ actor QueryHistoryStorage { return true } - /// Everything this connection left in the database, across all three tables. - /// - /// `clear(matching:)` cannot serve, because it deletes from `history` alone and both snapshot - /// tables hold their own copy of what the statement said: `plan_snapshots.subject_sql` and - /// `raw_plan`, and `rewind_snapshots.payload`, which is captured row data. Their `history_id` - /// is `ON DELETE SET NULL`, so deleting the history rows unlinks them and leaves them on disk, - /// out of reach of the UI and, for a pinned plan, out of reach of retention pruning too. + // Not clear(matching:): the snapshot tables keep their own copy of the statement and rows, and + // deleting history only unlinks them (ON DELETE SET NULL), out of reach of the UI and retention. @discardableResult func deleteEverything(forConnection connectionId: UUID) -> Bool { guard let db else { return false } @@ -1126,6 +1127,7 @@ actor QueryHistoryStorage { } sqlite3_finalize(statement) } + rewindSnapshotsDidChange() return succeeded } diff --git a/apple/TablePro/Core/TunnelCommand/TunnelCommandManager.swift b/apple/TablePro/Core/TunnelCommand/TunnelCommandManager.swift index a3516b97db..55d9162a9d 100644 --- a/apple/TablePro/Core/TunnelCommand/TunnelCommandManager.swift +++ b/apple/TablePro/Core/TunnelCommand/TunnelCommandManager.swift @@ -28,6 +28,7 @@ actor TunnelCommandManager: TunnelManaging { } private var tunnels: [UUID: TunnelState] = [:] + private var creations = TunnelCreationGenerations() private var pidRecords: [UUID: TunnelCommandPidRecord] = [:] private let runnerFactory: () -> any SupervisedProcessRunner private let executableLookup: (String) -> String? @@ -55,13 +56,15 @@ actor TunnelCommandManager: TunnelManaging { remotePort: Int, deadline: ConnectionDeadline = ConnectionDeadline(configuredSeconds: nil) ) async throws -> Int { + let generation = creations.begin(connectionId) + defer { creations.finish(generation, for: connectionId) } let timeoutEndpoint = ConnectionTimeoutEndpoint.tunnel("\(remoteHost):\(remotePort)") try deadline.check(endpoint: timeoutEndpoint) await sweepStalePidsIfNeeded() try deadline.check(endpoint: timeoutEndpoint) if tunnels[connectionId] != nil { - try await closeTunnel(connectionId: connectionId) + try await discardTunnel(connectionId: connectionId) } let environment = ProcessEnvironment.addingToolSearchPaths(to: ProcessEnvironment.inherited()) @@ -94,9 +97,9 @@ actor TunnelCommandManager: TunnelManaging { throw TunnelCommandError.executableNotFound(invocation.executable) } - /// Every exit from here stops the process, cancellation included. A connect the user - /// cancels while the port is still being waited on would otherwise leave the forward - /// running until the next launch swept it, still holding its port. + // Every exit from here stops the process, cancellation included. A connect the user + // cancels while the port is still being waited on would otherwise leave the forward + // running until the next launch swept it, still holding its port. do { try await awaitReadiness( runner: runner, @@ -117,6 +120,11 @@ actor TunnelCommandManager: TunnelManaging { throw error } + // A newer createTunnel or a close for this connection came while this one dialed, so it must not adopt. + guard creations.isNewest(generation, for: connectionId) else { + runner.stop() + throw CancellationError() + } register( connectionId: connectionId, runner: runner, @@ -133,7 +141,13 @@ actor TunnelCommandManager: TunnelManaging { throw lastError } + // Also ends a creation still dialing, so nothing it builds is adopted after the close returned. func closeTunnel(connectionId: UUID) async throws { + creations.invalidate(connectionId) + try await discardTunnel(connectionId: connectionId) + } + + private func discardTunnel(connectionId: UUID) async throws { guard let state = tunnels.removeValue(forKey: connectionId) else { return } Self.runnerRegistry.withLock { $0[connectionId] = nil } pidRecords.removeValue(forKey: connectionId) @@ -143,6 +157,7 @@ actor TunnelCommandManager: TunnelManaging { } func closeAllTunnels() async { + creations.invalidateAll() let current = tunnels tunnels.removeAll() pidRecords.removeAll() diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 77aec8a3a9..13217f7a93 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -15401,6 +15401,9 @@ } } } + }, + "a comparison or sync" : { + }, "A composite's fields in declaration order" : { "localizations" : { @@ -16732,6 +16735,9 @@ } } } + }, + "a running query" : { + }, "A save is already running in this window." : { "localizations" : { @@ -24350,6 +24356,9 @@ } } } + }, + "an agent reply" : { + }, "An array has no \"items\"." : { @@ -24699,6 +24708,9 @@ } } } + }, + "an import, export, backup, restore or copy" : { + }, "An index name is 3 to 255 characters of letters, digits, underscore, hyphen and period." : { "extractionState" : "manual", @@ -24808,6 +24820,9 @@ "an object with latitude and longitude" : { "extractionState" : "manual", "generatesSymbol" : false + }, + "an open transaction" : { + }, "An operation is already running." : { "localizations" : { @@ -180151,6 +180166,9 @@ } } } + }, + "TablePro cannot tell whether this connection holds an open transaction, so it stays connected. Try again once its work is done, or disconnect in TablePro." : { + }, "TablePro cannot tell which Redshift cluster or workgroup \"%@\" is. Set Host to the endpoint AWS issued, such as my-cluster.abc123.us-east-1.redshift.amazonaws.com, or enter that endpoint as the Redshift Endpoint when you connect through a port forward." : { @@ -195945,6 +195963,9 @@ } } } + }, + "This connection has %@ in TablePro, which disconnecting would stop or discard. Finish it first, or disconnect in TablePro, which asks the user." : { + }, "This connection has a transaction open. Commit or roll it back in TablePro, then try again." : { "extractionState" : "manual", @@ -207720,6 +207741,9 @@ }, "Unpin Tab" : { + }, + "unsaved changes" : { + }, "Unsaved changes" : { diff --git a/apple/TablePro/Views/Connection/ConnectionStatusMark.swift b/apple/TablePro/Views/Connection/ConnectionStatusMark.swift index d4769cf4c3..08d3d551c1 100644 --- a/apple/TablePro/Views/Connection/ConnectionStatusMark.swift +++ b/apple/TablePro/Views/Connection/ConnectionStatusMark.swift @@ -117,6 +117,10 @@ internal final class ConnectionStatusMarkView: NSView { presentation.isBusy } + internal var displayedTint: NSColor? { + displayedSymbolName == nil ? nil : presentation.tint.nsColor + } + internal func configure(_ presentation: ConnectionStatusPresentation, isEmphasized: Bool) { self.presentation = presentation self.isEmphasized = isEmphasized @@ -135,15 +139,25 @@ internal final class ConnectionStatusMarkView: NSView { private func apply() { let name = presentation.symbol?.systemName - symbol.image = name.flatMap { NSImage(systemSymbolName: $0, accessibilityDescription: nil) } + symbol.image = name.flatMap { Self.tintedSymbol(named: $0, color: presentation.tint.nsColor) } symbol.isHidden = name == nil - symbol.contentTintColor = presentation.tint.nsColor isHidden = presentation.symbol == nil // The colour AppKit draws content in on a selection, as the old identity dot's rim did. backing.fillColor = isEmphasized ? .alternateSelectedControlTextColor : .windowBackgroundColor applyAnimation() } + // A template follows the selected row's emphasized style into white, the colour of the backing under it. + nonisolated private static func tintedSymbol(named name: String, color: NSColor) -> NSImage? { + guard let template = NSImage(systemSymbolName: name, accessibilityDescription: nil) else { return nil } + return NSImage(size: template.size, flipped: false) { rect in + template.draw(in: rect) + color.set() + rect.fill(using: .sourceAtop) + return true + } + } + private func applyAnimation() { if presentation.isBusy, window != nil { spinner.startAnimation(nil) diff --git a/apple/TablePro/Views/Editor/History/HistoryPanelView.swift b/apple/TablePro/Views/Editor/History/HistoryPanelView.swift index b25ca84e0c..39e3761a60 100644 --- a/apple/TablePro/Views/Editor/History/HistoryPanelView.swift +++ b/apple/TablePro/Views/Editor/History/HistoryPanelView.swift @@ -75,7 +75,7 @@ struct HistoryPanelView: View { onRunInNewTab: { runInNewTab($0) }, onCopy: { copy($0) }, onSaveAsFavorite: { favoriteDialogQuery = FavoriteDialogQuery(query: $0.query) }, - canRestorePreviousValues: { canRun($0) && coordinator.canRewindSelectedTab }, + canRestorePreviousValues: { canRun($0) && coordinator.canRestoreSave(historyId: $0.id) }, onRestorePreviousValues: { coordinator.rewindSave(historyId: $0.id) } ) } secondary: { diff --git a/apple/TablePro/Views/Export/ExportDialog.swift b/apple/TablePro/Views/Export/ExportDialog.swift index 43f129a36c..df3c2c556e 100644 --- a/apple/TablePro/Views/Export/ExportDialog.swift +++ b/apple/TablePro/Views/Export/ExportDialog.swift @@ -13,6 +13,7 @@ struct ExportDialog: View { let mode: ExportMode var sidebarTables: [TableInfo] = [] var initialFormatId: String? + var sheetWork: SheetWorkScope? // MARK: - State @@ -976,18 +977,19 @@ struct ExportDialog: View { run.reset() let source = connection?.name ?? dataSourceRequest?.title ?? "" let stopper = run - run.workId = RunningWorkRegistry.shared.begin( + run.job = SheetJob( .dataExport, subject: url.lastPathComponent, connectionName: source, - connectionIds: Set([connection?.id].compactMap { $0 }) + connectionIds: Set([connection?.id].compactMap { $0 }), + scope: sheetWork ) { stopper.stop() } showProgressDialog = true } private func endRunningWork() { - RunningWorkRegistry.shared.end(run.workId) - run.workId = nil + run.job?.end() + run.job = nil } private func adopt(_ service: ExportService) { @@ -1113,7 +1115,7 @@ struct ExportDialog: View { private final class ExportRun { var service: ExportService? var task: Task? - var workId: UUID? + var job: SheetJob? private(set) var isStopRequested = false func reset() { diff --git a/apple/TablePro/Views/Export/ExportSheetPresenter.swift b/apple/TablePro/Views/Export/ExportSheetPresenter.swift index 73f4a39ecd..ed64022b84 100644 --- a/apple/TablePro/Views/Export/ExportSheetPresenter.swift +++ b/apple/TablePro/Views/Export/ExportSheetPresenter.swift @@ -10,6 +10,7 @@ import SwiftUI internal final class ExportSheetPresenter { private var sheetWindow: NSWindow? private weak var parentWindow: NSWindow? + internal let sheetWork = SheetWorkScope() internal init() {} @@ -25,7 +26,9 @@ internal final class ExportSheetPresenter { @discardableResult internal func present(_ mode: ExportMode, on parent: NSWindow) -> Bool { guard sheetWindow == nil, parent.attachedSheet == nil else { return false } - let hosting = NSHostingController(rootView: ExportDialog(isPresented: presentationBinding, mode: mode)) + let hosting = NSHostingController( + rootView: ExportDialog(isPresented: presentationBinding, mode: mode, sheetWork: sheetWork) + ) let sheet = NSWindow(contentViewController: hosting) sheet.isReleasedWhenClosed = false sheetWindow = sheet @@ -34,7 +37,13 @@ internal final class ExportSheetPresenter { return true } + // The window closing; the dialog's own close comes through the binding, with nothing running. internal func dismiss() { + sheetWork.stopAll() + endSheet() + } + + private func endSheet() { guard let sheet = sheetWindow else { return } sheetWindow = nil let parent = parentWindow @@ -52,7 +61,7 @@ internal final class ExportSheetPresenter { get: { [weak self] in self?.sheetWindow != nil }, set: { [weak self] isPresented in guard !isPresented else { return } - self?.dismiss() + self?.endSheet() } ) } diff --git a/apple/TablePro/Views/Export/TableTransferSheet.swift b/apple/TablePro/Views/Export/TableTransferSheet.swift index bc26e4ae0c..becf16b958 100644 --- a/apple/TablePro/Views/Export/TableTransferSheet.swift +++ b/apple/TablePro/Views/Export/TableTransferSheet.swift @@ -20,9 +20,9 @@ struct TableTransferSheet: View { @Binding var isPresented: Bool let sourceConnection: DatabaseConnection let preselectedTables: Set - /// The schema the preselected rows came from. Without it the sheet listed whichever schema the - /// session happened to be browsing and matched the bare names against that one instead. + // Without it the sheet listed whichever schema the session was browsing and matched the bare names there. var preselectedSchema: String? + var sheetWork: SheetWorkScope? @StateObject private var service = TableTransferService() @State private var destinationConnectionId: UUID? @@ -444,17 +444,20 @@ struct TableTransferSheet: View { service.prepareForRun() isRunning = true let transferService = service - let workId = RunningWorkRegistry.shared.begin( + let job = SheetJob( .tableTransfer, subject: destinationConnection.name, connectionName: sourceConnection.name, - connectionIds: [sourceConnection.id, destinationConnection.id] + connectionIds: [sourceConnection.id, destinationConnection.id], + scope: sheetWork, + // Closing the destination's last window disconnects it, so that close has to list the transfer too. + listedOnlyForItsWindow: false ) { transferService.cancel() } defer { isRunning = false - RunningWorkRegistry.shared.end(workId) + job.end() } await loadColumnsForSelection() diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Rewind.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Rewind.swift index f26f211ab5..40d7e43014 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Rewind.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Rewind.swift @@ -14,44 +14,60 @@ extension MainContentCoordinator { Logger(subsystem: "com.TablePro", category: "Rewind") } - /// Whether the selected tab is one a save could be restored on. Says nothing about whether - /// there is a save to restore, which costs a read to find out, and nothing about the licence. - /// - /// Deliberately not gated on the licence. Someone who has just saved the wrong thing has to be - /// able to find this, and a dimmed control they never notice tells them nothing about what the - /// licence is for. The gate is at the point of use instead, where it can explain itself. + // Not gated on the license: someone who just saved the wrong thing must find this. The gate + // is at the point of use, where it can say what the license is for. var canRewindSelectedTab: Bool { + guard let target = selectedRewindTarget else { return false } + return services.queryHistoryManager.rewindAvailability.hasSnapshot(for: target) + } + + // The drawer's save names its own target, so the selected table need not have one kept. + func canRestoreSave(historyId: UUID) -> Bool { + isSelectedTabRewindable && services.queryHistoryManager.rewindAvailability.hasSnapshot(forHistoryId: historyId) + } + + private var isSelectedTabRewindable: Bool { guard let tab = tabManager.selectedTab, tab.tabType == .table else { return false } return tab.tableContext.tableName != nil && !safeModeLevel.blocksAllWrites } + private var selectedRewindTarget: RewindAvailability.Target? { + guard isSelectedTabRewindable, + let tableName = tabManager.selectedTab?.tableContext.tableName, + let tabScope = selectedTabScope + else { return nil } + return RewindAvailability.Target( + connectionId: connection.id, + database: tabScope.database, + schema: tabScope.schema, + table: tableName + ) + } + private var isDataRewindLicensed: Bool { services.licenseManager.isFeatureAvailable(.dataRewind) } func rewindLastSave() { guard presentUpsellUnlessLicensed() else { return } - guard canRewindSelectedTab, - let tab = tabManager.selectedTab, - let tableName = tab.tableContext.tableName, - let tabScope = selectedTabScope - else { return } + guard canRewindSelectedTab, let target = selectedRewindTarget else { return } Task { [weak self] in guard let self else { return } let records = await services.queryHistoryManager.rewindSnapshots( - connectionId: connection.id, - database: tabScope.database, - schema: tabScope.schema, - table: tableName, + connectionId: target.connectionId, + database: target.database, + schema: target.schema, + table: target.table, limit: 1 ) + // Enabled from the last publish; the save can be pruned or unreadable by the time this reads it. guard let record = records.first else { AlertHelper.showInfoSheet( title: String(localized: "Nothing to Restore"), message: String( format: String(localized: "No saved changes to '%@' are being kept."), - tableName + target.table ), window: contentWindow ) @@ -61,16 +77,14 @@ extension MainContentCoordinator { } } - /// Reached from the history drawer, where the row the user clicked is the save itself. - /// - /// The drawer can be scoped to every connection, so the record may belong to another one. A - /// rewind planned against the window's current scope would then read and write a same-named - /// table in the wrong database, which is why the record's own target decides where it runs. + // The record's own target decides where it runs: a rewind planned against the window's scope + // would write a same-named table in the wrong database. func rewindSave(historyId: UUID) { - guard presentUpsellUnlessLicensed(), canRewindSelectedTab else { return } + guard presentUpsellUnlessLicensed(), canRestoreSave(historyId: historyId) else { return } Task { [weak self] in guard let self else { return } let manager = services.queryHistoryManager + // Enabled from the last publish; the save can be pruned or unreadable by the time this reads it. guard let recordId = await manager.rewindSnapshotId(forHistoryId: historyId), let record = await manager.rewindSnapshot(id: recordId) else { @@ -81,14 +95,9 @@ extension MainContentCoordinator { ) return } + // The drawer disables another connection's saves; this keeps a stray call off the wrong database. guard record.connectionId == connection.id else { - AlertHelper.showInfoSheet( - title: String(localized: "Another Connection"), - message: String( - localized: "This save was made on a different connection. Open that connection to restore it." - ), - window: contentWindow - ) + Self.rewindLogger.error("Refused a rewind of another connection's save") return } await presentRewind(for: record, scope: scope(for: record)) @@ -102,7 +111,7 @@ extension MainContentCoordinator { return false } - /// Where the record's own rows live, which is not necessarily where the window is pointing. + // Where the record's own rows live, not necessarily where the window points. private func scope(for record: RewindRecord) -> DatabaseScope { DatabaseScope( connectionId: connection.id, @@ -164,9 +173,7 @@ extension MainContentCoordinator { } } - /// A factory shaped by the record rather than by the live tab: the columns, keys and computed - /// columns that matter are the ones the save ran against, and the tab may be on another table - /// by now. + // Shaped by the record, not the live tab: the save's columns and keys matter, and the tab may have moved on. private func rewindStatementFactory(for record: RewindRecord) -> RowChangeStatementFactory { let operation = record.operations.first return RowChangeStatementFactory( diff --git a/apple/TablePro/Views/Main/Extensions/MainContentView+TransferSheets.swift b/apple/TablePro/Views/Main/Extensions/MainContentView+TransferSheets.swift index cd2aabf777..0d46f85082 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentView+TransferSheets.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentView+TransferSheets.swift @@ -5,9 +5,7 @@ import SwiftUI -/// The sheets that move data in or out of a connection: export, import, transfer, backup and -/// restore. They live apart from the rest because together they were more than half of what -/// `sheetContent(for:)` had to hold. +// Apart from `sheetContent(for:)`, which they made more than twice as long. extension MainContentView { @ViewBuilder func transferSheetContent(for sheet: ActiveSheet, dismiss dismissBinding: Binding) -> some View { @@ -25,7 +23,8 @@ extension MainContentView { ) ), sidebarTables: tables, - initialFormatId: coordinator.exportFormatId + initialFormatId: coordinator.exportFormatId, + sheetWork: coordinator.sheetWork ) case .exportQueryResults: if let tab = coordinator.tabManager.selectedTab { @@ -37,7 +36,8 @@ extension MainContentView { connection: connectionWithCurrentDatabase, query: baseQuery, suggestedFileName: fileName - ) + ), + sheetWork: coordinator.sheetWork ) } else { ExportDialog( @@ -47,7 +47,8 @@ extension MainContentView { tableRows: coordinator.tabSessionRegistry.tableRows(for: tab.id), suggestedFileName: fileName, fillUnloaded: coordinator.unloadedValueFill(for: tab) - ) + ), + sheetWork: coordinator.sheetWork ) } } diff --git a/apple/TablePro/Views/Main/MainContentCoordinator.swift b/apple/TablePro/Views/Main/MainContentCoordinator.swift index d0411e4eed..4520cb00ca 100644 --- a/apple/TablePro/Views/Main/MainContentCoordinator.swift +++ b/apple/TablePro/Views/Main/MainContentCoordinator.swift @@ -260,6 +260,8 @@ final class MainContentCoordinator: ObservableObject { @Published var activeSheet: ActiveSheet? { didSet { releaseImportFileUnlessImporting() } } + // Export and transfer started from these sheets stop with `teardown()`, never with the pane. + private(set) lazy var sheetWork = SheetWorkScope { [weak self] in self?.windowId } // Commands present through this, not a flag: the toolbar view is gone when its item is in overflow or removed. var switcherPresenter: ToolbarSwitcherPresenter? { splitViewController?.switcherPresenter @@ -833,6 +835,7 @@ final class MainContentCoordinator: ObservableObject { ) _didTeardown.withLock { $0 = true } + sheetWork.stopAll() unregisterFromPersistence() dropMovesAwaitingRestore(reason: "coordinator torn down") SessionRecoveryTracker.sync() diff --git a/apple/TablePro/Views/Main/MainContentView.swift b/apple/TablePro/Views/Main/MainContentView.swift index 3954b0d3e5..366d35fafd 100644 --- a/apple/TablePro/Views/Main/MainContentView.swift +++ b/apple/TablePro/Views/Main/MainContentView.swift @@ -147,7 +147,8 @@ struct MainContentView: View { isPresented: dismiss, sourceConnection: connectionWithCurrentDatabase, preselectedTables: tables, - preselectedSchema: schema + preselectedSchema: schema, + sheetWork: coordinator.sheetWork ) } diff --git a/apple/TableProMobile/TableProMobile/Views/RowDetailView.swift b/apple/TableProMobile/TableProMobile/Views/RowDetailView.swift index 2329d6c960..e0572c3948 100644 --- a/apple/TableProMobile/TableProMobile/Views/RowDetailView.swift +++ b/apple/TableProMobile/TableProMobile/Views/RowDetailView.swift @@ -350,6 +350,7 @@ struct RowDetailView: View { .font(.body) .foregroundStyle(.secondary) .italic() + .frame(maxWidth: .infinity, alignment: .leading) } else if viewModel.isJSON(at: index) { if let value, !value.isEmpty { JSONFieldPreview(text: value) diff --git a/apple/TableProTests/CloudSQL/CloudSQLProxyManagerTests.swift b/apple/TableProTests/CloudSQL/CloudSQLProxyManagerTests.swift index d62939c966..0da01e08f6 100644 --- a/apple/TableProTests/CloudSQL/CloudSQLProxyManagerTests.swift +++ b/apple/TableProTests/CloudSQL/CloudSQLProxyManagerTests.swift @@ -369,8 +369,10 @@ struct CloudSQLProxyManagerTests { serviceAccountKeyJSON: "{\"type\":\"service_account\"}" ) - let path = FileManager.default.temporaryDirectory - .appendingPathComponent("com.TablePro.cloudsqlproxy.\(id.uuidString).json") + let arguments = fake.startedArguments + let flag = try #require(arguments.firstIndex(of: "--credentials-file")) + let path = URL(fileURLWithPath: arguments[flag + 1]) + #expect(path.lastPathComponent.hasPrefix("com.TablePro.cloudsqlproxy.\(id.uuidString)-")) #expect(FileManager.default.fileExists(atPath: path.path)) let perms = try FileManager.default.attributesOfItem(atPath: path.path)[.posixPermissions] as? Int #expect(perms == 0o600) diff --git a/apple/TableProTests/Core/Database/TunnelCreationRaceTests.swift b/apple/TableProTests/Core/Database/TunnelCreationRaceTests.swift new file mode 100644 index 0000000000..3b796b9848 --- /dev/null +++ b/apple/TableProTests/Core/Database/TunnelCreationRaceTests.swift @@ -0,0 +1,402 @@ +// +// TunnelCreationRaceTests.swift +// TableProTests +// + +import Darwin +import Foundation +import Testing + +@testable import TablePro +import TableProProcess +import TableProTestSupport + +// A forwarding process that listens only once the test releases it, so one creation can be held mid-readiness. +final class HeldReadinessRunner: SupervisedProcessRunner, @unchecked Sendable { + private let portInArguments: @Sendable ([String]) -> Int? + private let lock = NSLock() + private var isHeld: Bool + private var startedPort: Int? + private var listenerFd: Int32? + private var starts = 0 + private var stops = 0 + private var arguments: [String] = [] + private var requested = false + private var terminationResult: SubprocessTermination? + private var terminationContinuation: CheckedContinuation? + + let stderrLines: AsyncStream + private let stderrContinuation: AsyncStream.Continuation + + init(held: Bool, portInArguments: @escaping @Sendable ([String]) -> Int?) { + isHeld = held + self.portInArguments = portInArguments + var continuation: AsyncStream.Continuation! + stderrLines = AsyncStream { continuation = $0 } + stderrContinuation = continuation + } + + var processIdentifier: Int32? { nil } + + var hasStarted: Bool { lock.withLock { starts > 0 } } + + var stopCount: Int { lock.withLock { stops } } + + func argument(after flag: String) -> String? { + lock.withLock { + guard let index = arguments.firstIndex(of: flag), index + 1 < arguments.count else { return nil } + return arguments[index + 1] + } + } + + func start(_ command: ProcessCommand) throws { + let port = portInArguments(command.arguments) + lock.withLock { + starts += 1 + startedPort = port + arguments = command.arguments + if !isHeld, let port { listenerFd = FakeCloudSQLProxyRunner.openListener(port: port) } + } + } + + func release() { + lock.withLock { + isHeld = false + guard listenerFd == nil, let port = startedPort else { return } + listenerFd = FakeCloudSQLProxyRunner.openListener(port: port) + } + } + + func stop() { + lock.lock() + requested = true + stops += 1 + if let fd = listenerFd { + close(fd) + listenerFd = nil + } + lock.unlock() + finish() + } + + var termination: SubprocessTermination { + get async { + await withCheckedContinuation { continuation in + lock.lock() + if let cached = terminationResult { + lock.unlock() + continuation.resume(returning: cached) + return + } + terminationContinuation = continuation + lock.unlock() + } + } + } + + private func finish() { + lock.lock() + guard terminationResult == nil else { + lock.unlock() + return + } + let result = SubprocessTermination(exitCode: 0, wasRequested: requested) + terminationResult = result + let pending = terminationContinuation + terminationContinuation = nil + lock.unlock() + stderrContinuation.finish() + pending?.resume(returning: result) + } + + static func port(after flag: String) -> @Sendable ([String]) -> Int? { + { arguments in + guard let index = arguments.firstIndex(of: flag), index + 1 < arguments.count else { return nil } + let value = arguments[index + 1] + return Int(value.split(separator: ":").last.map(String.init) ?? value) + } + } +} + +private final class RunnerQueue: @unchecked Sendable { + private let lock = NSLock() + private var runners: [HeldReadinessRunner] + + init(_ runners: [HeldReadinessRunner]) { + self.runners = runners + } + + func next() -> HeldReadinessRunner { + lock.withLock { runners.removeFirst() } + } +} + +private func expectSuperseded(_ result: Result) { + guard case .failure(let error) = result else { + Issue.record("The older creation adopted its tunnel over the newer one") + return + } + #expect(error is CancellationError) +} + +// Two creations for one connection: the older one is held until the newer one has its tunnel, then let go. +struct TunnelCreationRaceTests { + @Test("The newest creation is the only one that may adopt") + func generationsKeepTheNewest() { + var generations = TunnelCreationGenerations() + let id = UUID() + let older = generations.begin(id) + let newer = generations.begin(id) + + let elsewhere = generations.begin(UUID()) + + #expect(!generations.isNewest(older, for: id)) + #expect(generations.isNewest(newer, for: id)) + #expect(!generations.isNewest(elsewhere, for: id)) + + generations.finish(older, for: id) + #expect(generations.isNewest(newer, for: id)) + generations.finish(newer, for: id) + #expect(!generations.isNewest(older, for: id)) + #expect(!generations.isNewest(newer, for: id)) + } + + @Test("A tunnel command creation that lost the race stops its process and keeps the newer tunnel") + func tunnelCommandRace() async throws { + let older = HeldReadinessRunner(held: true, portInArguments: HeldReadinessRunner.port(after: "--listen")) + let newer = HeldReadinessRunner(held: false, portInArguments: HeldReadinessRunner.port(after: "--listen")) + let runners = RunnerQueue([older, newer]) + let manager = TunnelCommandManager(runnerFactory: { runners.next() }) + let config = TunnelCommandConfiguration(method: .custom, command: "/bin/echo --listen {port}") + let id = UUID() + + let olderCreation = Task { + try await manager.createTunnel(connectionId: id, config: config, remoteHost: "db.internal", remotePort: 5_432) + } + try await waitUntil { older.hasStarted } + let newerPort = try await manager.createTunnel( + connectionId: id, config: config, remoteHost: "db.internal", remotePort: 5_432 + ) + older.release() + + expectSuperseded(await olderCreation.result) + #expect(await manager.getLocalPort(connectionId: id) == newerPort) + #expect(older.stopCount == 1) + #expect(newer.stopCount == 0) + await manager.closeAllTunnels() + } + + @Test("A Cloudflare creation that lost the race stops its process and keeps the newer tunnel") + func cloudflareRace() async throws { + let older = HeldReadinessRunner(held: true, portInArguments: HeldReadinessRunner.port(after: "--url")) + let newer = HeldReadinessRunner(held: false, portInArguments: HeldReadinessRunner.port(after: "--url")) + let runners = RunnerQueue([older, newer]) + let manager = CloudflareTunnelManager(runnerFactory: { runners.next() }) + let config = CloudflareConfiguration(accessHostname: "db.example.com", binaryPath: "/bin/echo") + let id = UUID() + + let olderCreation = Task { try await manager.createTunnel(connectionId: id, config: config) } + try await waitUntil { older.hasStarted } + let newerPort = try await manager.createTunnel(connectionId: id, config: config) + older.release() + + expectSuperseded(await olderCreation.result) + #expect(await manager.getLocalPort(connectionId: id) == newerPort) + #expect(older.stopCount == 1) + #expect(newer.stopCount == 0) + await manager.closeAllTunnels() + } + + @Test("A Cloud SQL creation that lost the race stops its proxy and leaves the newer one its key file") + func cloudSQLRace() async throws { + let older = HeldReadinessRunner(held: true, portInArguments: HeldReadinessRunner.port(after: "--port")) + let newer = HeldReadinessRunner(held: false, portInArguments: HeldReadinessRunner.port(after: "--port")) + let runners = RunnerQueue([older, newer]) + let manager = CloudSQLProxyManager(runnerFactory: { runners.next() }, ownsListener: { _, _ in true }) + let config = CloudSQLProxyConfiguration( + instanceConnectionName: "proj:region:inst", + authMode: .serviceAccountKey, + binaryPath: "/bin/echo" + ) + let key = "{\"type\":\"service_account\"}" + let id = UUID() + + let olderCreation = Task { + try await manager.createTunnel(connectionId: id, config: config, serviceAccountKeyJSON: key) + } + try await waitUntil { older.hasStarted } + let newerPort = try await manager.createTunnel(connectionId: id, config: config, serviceAccountKeyJSON: key) + older.release() + + expectSuperseded(await olderCreation.result) + #expect(await manager.getLocalPort(connectionId: id) == newerPort) + #expect(older.stopCount == 1) + #expect(newer.stopCount == 0) + let olderKey = try #require(older.argument(after: "--credentials-file")) + let newerKey = try #require(newer.argument(after: "--credentials-file")) + #expect(olderKey != newerKey) + #expect(!FileManager.default.fileExists(atPath: olderKey)) + #expect(FileManager.default.fileExists(atPath: newerKey)) + await manager.closeAllTunnels() + #expect(!FileManager.default.fileExists(atPath: newerKey)) + } + + @Test("A SOCKS creation that lost the race closes its listener and keeps the newer tunnel") + func socksRace() async throws { + let gate = AsyncGate() + let held = FakeSOCKS5Server(behavior: .echo, connectReplyGate: gate) + let prompt = FakeSOCKS5Server(behavior: .echo) + try await held.start() + try await prompt.start() + defer { + held.stop() + prompt.stop() + } + let manager = SOCKSProxyManager(connectTimeout: 10) + let id = UUID() + + let olderCreation = Task { + try await manager.createTunnel( + connectionId: id, + config: SOCKSProxyConfiguration(host: "127.0.0.1", port: held.port), + password: nil, + targetHost: "db.internal.example", + targetPort: 5_432 + ) + } + try await waitUntil { held.capturedConnectRequest != nil } + let newerPort = try await manager.createTunnel( + connectionId: id, + config: SOCKSProxyConfiguration(host: "127.0.0.1", port: prompt.port), + password: nil, + targetHost: "db.internal.example", + targetPort: 5_432 + ) + await gate.open() + + expectSuperseded(await olderCreation.result) + #expect(await manager.getLocalPort(connectionId: id) == newerPort) + await manager.closeAllTunnels() + } +} + +// A close while a creation still waits on readiness: nothing may be adopted after the close returned. +struct TunnelCloseDuringCreationTests { + @Test("Closing a tunnel command that is still dialing stops its process and adopts nothing") + func tunnelCommandClose() async throws { + let held = HeldReadinessRunner(held: true, portInArguments: HeldReadinessRunner.port(after: "--listen")) + let manager = TunnelCommandManager(runnerFactory: { held }) + let config = TunnelCommandConfiguration(method: .custom, command: "/bin/echo --listen {port}") + let id = UUID() + + let creation = Task { + try await manager.createTunnel(connectionId: id, config: config, remoteHost: "db.internal", remotePort: 5_432) + } + try await waitUntil { held.hasStarted } + try await manager.closeTunnel(connectionId: id) + held.release() + + expectSuperseded(await creation.result) + #expect(await !manager.hasTunnel(connectionId: id)) + #expect(held.stopCount == 1) + } + + @Test("Closing every Cloudflare tunnel also ends one that is still dialing") + func cloudflareCloseAll() async throws { + let held = HeldReadinessRunner(held: true, portInArguments: HeldReadinessRunner.port(after: "--url")) + let manager = CloudflareTunnelManager(runnerFactory: { held }) + let config = CloudflareConfiguration(accessHostname: "db.example.com", binaryPath: "/bin/echo") + let id = UUID() + + let creation = Task { try await manager.createTunnel(connectionId: id, config: config) } + try await waitUntil { held.hasStarted } + await manager.closeAllTunnels() + held.release() + + expectSuperseded(await creation.result) + #expect(await !manager.hasTunnel(connectionId: id)) + #expect(held.stopCount == 1) + } + + @Test("Closing a Cloud SQL proxy that is still dialing stops it and removes its key file") + func cloudSQLClose() async throws { + let held = HeldReadinessRunner(held: true, portInArguments: HeldReadinessRunner.port(after: "--port")) + let manager = CloudSQLProxyManager(runnerFactory: { held }, ownsListener: { _, _ in true }) + let config = CloudSQLProxyConfiguration( + instanceConnectionName: "proj:region:inst", + authMode: .serviceAccountKey, + binaryPath: "/bin/echo" + ) + let id = UUID() + + let creation = Task { + try await manager.createTunnel(connectionId: id, config: config, serviceAccountKeyJSON: "{}") + } + try await waitUntil { held.hasStarted } + try await manager.closeTunnel(connectionId: id) + held.release() + + expectSuperseded(await creation.result) + #expect(await !manager.hasTunnel(connectionId: id)) + #expect(held.stopCount == 1) + let key = try #require(held.argument(after: "--credentials-file")) + #expect(!FileManager.default.fileExists(atPath: key)) + } + + @Test("A newer Cloud SQL attempt that fails before writing its key leaves no key file behind") + func cloudSQLNewerFailureLeavesNoKey() async throws { + let held = HeldReadinessRunner(held: true, portInArguments: HeldReadinessRunner.port(after: "--port")) + let manager = CloudSQLProxyManager(runnerFactory: { held }, ownsListener: { _, _ in true }) + let config = CloudSQLProxyConfiguration( + instanceConnectionName: "proj:region:inst", + authMode: .serviceAccountKey, + binaryPath: "/bin/echo" + ) + let missingBinary = CloudSQLProxyConfiguration( + instanceConnectionName: "proj:region:inst", + authMode: .serviceAccountKey, + binaryPath: "/nonexistent/cloud-sql-proxy" + ) + let id = UUID() + + let older = Task { + try await manager.createTunnel(connectionId: id, config: config, serviceAccountKeyJSON: "{}") + } + try await waitUntil { held.hasStarted } + await #expect(throws: (any Error).self) { + _ = try await manager.createTunnel(connectionId: id, config: missingBinary, serviceAccountKeyJSON: "{}") + } + held.release() + + expectSuperseded(await older.result) + let key = try #require(held.argument(after: "--credentials-file")) + #expect(!FileManager.default.fileExists(atPath: key)) + #expect(await !manager.hasTunnel(connectionId: id)) + } + + @Test("Closing a SOCKS tunnel that is still probing closes its listener and adopts nothing") + func socksClose() async throws { + let gate = AsyncGate() + let server = FakeSOCKS5Server(behavior: .echo, connectReplyGate: gate) + try await server.start() + defer { server.stop() } + let manager = SOCKSProxyManager(connectTimeout: 10) + let id = UUID() + + let creation = Task { + try await manager.createTunnel( + connectionId: id, + config: SOCKSProxyConfiguration(host: "127.0.0.1", port: server.port), + password: nil, + targetHost: "db.internal.example", + targetPort: 5_432 + ) + } + try await waitUntil { server.capturedConnectRequest != nil } + try await manager.closeTunnel(connectionId: id) + await gate.open() + + expectSuperseded(await creation.result) + #expect(await !manager.hasTunnel(connectionId: id)) + } +} diff --git a/apple/TableProTests/Core/MCP/Protocol/Tools/DisconnectToolTests.swift b/apple/TableProTests/Core/MCP/Protocol/Tools/DisconnectToolTests.swift index 99cd07d451..0082b6a248 100644 --- a/apple/TableProTests/Core/MCP/Protocol/Tools/DisconnectToolTests.swift +++ b/apple/TableProTests/Core/MCP/Protocol/Tools/DisconnectToolTests.swift @@ -6,6 +6,7 @@ import Foundation @testable import TablePro import TableProMCP +import TableProPluginKit import Testing struct DisconnectToolTests { @@ -48,6 +49,168 @@ struct DisconnectToolTests { #expect(MCPToolTestHarness.errorText(result)?.hasPrefix("not_connected:") == true) } + @Test("Disconnecting while TablePro has work on the connection is refused and the session stays") + @MainActor + func runningWorkRefusesDisconnect() async throws { + let connection = TestFixtures.makeConnection(name: "Busy", type: .mysql) + var session = ConnectionSession( + connection: connection, + driver: PluginDriverAdapter(connection: connection, pluginDriver: IdleDriver()) + ) + session.status = .connected + DatabaseManager.shared.injectSession(session, for: connection.id) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let work = RunningWorkRegistry.shared.begin( + .dataImport, + subject: "private-file.csv", + connectionName: connection.name, + connectionIds: [connection.id] + ) {} + defer { RunningWorkRegistry.shared.end(work) } + + let result = try await call(.object(["connection_id": .string(connection.id.uuidString)])) + + #expect(result.isError) + let text = try #require(MCPToolTestHarness.errorText(result)) + #expect(text.hasPrefix("denied:")) + #expect(text.contains("an import, export, backup, restore or copy")) + #expect(!text.contains("private-file.csv")) + #expect(DatabaseManager.shared.activeSessions[connection.id] != nil) + } + + @Test("Once the work ends the same call disconnects") + @MainActor + func disconnectsOnceTheWorkEnds() async throws { + let connection = TestFixtures.makeConnection(name: "Idle again", type: .mysql) + var session = ConnectionSession( + connection: connection, + driver: PluginDriverAdapter(connection: connection, pluginDriver: IdleDriver()) + ) + session.status = .connected + DatabaseManager.shared.injectSession(session, for: connection.id) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let work = RunningWorkRegistry.shared.begin( + .backup, subject: "", connectionName: connection.name, connectionIds: [connection.id] + ) {} + let arguments: JsonValue = .object(["connection_id": .string(connection.id.uuidString)]) + + #expect(try await call(arguments).isError) + RunningWorkRegistry.shared.end(work) + let result = try await call(arguments) + + #expect(!result.isError) + #expect(DatabaseManager.shared.activeSessions[connection.id] == nil) + } + + @Test("Work that starts while the transaction probe runs is seen, and the disconnect is refused") + @MainActor + func workStartingDuringTheProbeRefuses() async throws { + let connection = TestFixtures.makeConnection(name: "Starts work", type: .mysql) + let driver = WorkStartingDriver(connectionId: connection.id) + try await withLiveSession(connection, driver: driver) { + let result = try await call(.object(["connection_id": .string(connection.id.uuidString)])) + + #expect(result.isError) + #expect(MCPToolTestHarness.errorText(result)?.contains("an import, export, backup, restore or copy") == true) + #expect(DatabaseManager.shared.activeSessions[connection.id] != nil) + } + RunningWorkRegistry.shared.end(driver.startedWork) + } + + @Test("A transaction another lane opened while the probe ran is seen, and the disconnect is refused") + @MainActor + func transactionOpenedDuringTheProbeRefuses() async throws { + let connection = TestFixtures.makeConnection(name: "Opens a transaction", type: .mysql) + let driver = TransactionOpeningDriver(connectionId: connection.id) + try await withLiveSession(connection, driver: driver) { + let result = try await call(.object(["connection_id": .string(connection.id.uuidString)])) + + #expect(result.isError) + #expect(MCPToolTestHarness.errorText(result)?.contains("an open transaction") == true) + #expect(DatabaseManager.shared.activeSessions[connection.id] != nil) + } + } + + @Test("A turn recorded while the probe ran makes an idle answer unknown") + @MainActor + func turnDuringTheProbeIsUnknown() { + let manager = DatabaseManager.shared + let connectionId = UUID() + let before = manager.sessionTransactions.turnRevision(for: connectionId) + + #expect(manager.settledTransactionSafety(.noTransaction, for: connectionId, turnRevisionBefore: before) == .noTransaction) + #expect(manager.settledTransactionSafety(.noTransaction, for: connectionId, turnRevisionBefore: before - 1) == .unknown) + #expect(manager.settledTransactionSafety(.holding, for: connectionId, turnRevisionBefore: before) == .holding) + } + + @Test("A transactional engine that cannot report its state is refused, naming nothing") + @MainActor + func unknownTransactionStateRefuses() async throws { + let connection = TestFixtures.makeConnection(name: "Cannot tell", type: .mysql) + try await withLiveSession(connection, driver: UnknownStateDriver()) { + let result = try await call(.object(["connection_id": .string(connection.id.uuidString)])) + + #expect(result.isError) + let text = try #require(MCPToolTestHarness.errorText(result)) + #expect(text.hasPrefix("denied:")) + #expect(text.contains("cannot tell whether this connection holds an open transaction")) + #expect(!text.contains("Cannot tell")) + #expect(DatabaseManager.shared.activeSessions[connection.id] != nil) + } + } + + @Test("An engine without transactions disconnects without a probe answer") + @MainActor + func engineWithoutTransactionsDisconnects() async throws { + let connection = TestFixtures.makeConnection(name: "No transactions", type: .mysql) + try await withLiveSession(connection, driver: TransactionlessDriver()) { + let result = try await call(.object(["connection_id": .string(connection.id.uuidString)])) + + #expect(!result.isError) + #expect(DatabaseManager.shared.activeSessions[connection.id] == nil) + } + } + + @Test("An open transaction is named as a loss, never the database") + @MainActor + func openTransactionRefuses() { + let message = ExternalDisconnectRefusal.message(work: [], transactions: .holding, hasUnsavedWork: false) + #expect(message?.contains("an open transaction") == true) + #expect(ExternalDisconnectRefusal.message(work: [], transactions: .noTransaction, hasUnsavedWork: false) == nil) + } + + @MainActor + private func withLiveSession( + _ connection: DatabaseConnection, + driver: any PluginDatabaseDriver, + _ body: @MainActor () async throws -> Void + ) async throws { + var session = ConnectionSession( + connection: connection, + driver: PluginDriverAdapter(connection: connection, pluginDriver: driver) + ) + session.status = .connected + DatabaseManager.shared.injectSession(session, for: connection.id) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await body() + } + + @Test("Each loss is named by its kind, in one fixed order") + @MainActor + func refusalNamesKinds() { + #expect(ExternalDisconnectRefusal.message(for: []) == nil) + #expect( + ExternalDisconnectRefusal.message(for: [.transaction]) + == "This connection has an open transaction in TablePro, which disconnecting would stop or discard. " + + "Finish it first, or disconnect in TablePro, which asks the user." + ) + let several = ExternalDisconnectRefusal.message(for: [.unsavedChanges, .query]) ?? "" + let query = several.range(of: "a running query") + let unsaved = several.range(of: "unsaved changes") + #expect(query != nil && unsaved != nil) + if let query, let unsaved { #expect(query.lowerBound < unsaved.lowerBound) } + } + @Test("An unknown parameter is rejected") func unknownParameterIsRejected() async throws { await #expect(throws: MCPProtocolError.self) { @@ -58,3 +221,62 @@ struct DisconnectToolTests { } } } + +private final class IdleDriver: PluginDriverStub, @unchecked Sendable { + func sessionTransactionState() async -> PluginSessionTransactionState { .idle } +} + +private final class UnknownStateDriver: PluginDriverStub, @unchecked Sendable { + func sessionTransactionState() async -> PluginSessionTransactionState { .unknown } +} + +private final class TransactionlessDriver: PluginDriverStub, @unchecked Sendable { + var supportsTransactions: Bool { false } +} + +// Another lane's turn finishes with a transaction open while the disconnect still waits on this probe. +private final class TransactionOpeningDriver: PluginDriverStub, @unchecked Sendable { + private let connectionId: UUID + + init(connectionId: UUID) { + self.connectionId = connectionId + } + + func sessionTransactionState() async -> PluginSessionTransactionState { + let connectionId = connectionId + await MainActor.run { + let manager = DatabaseManager.shared + guard let driver = manager.activeSessions[connectionId]?.driver else { return } + manager.sessionTransactions.record( + .inTransaction, + on: driver, + for: SessionTransactionTracker.Key(connectionId: connectionId, lane: nil) + ) + } + return .idle + } +} + +// Starts an import on the connection while the disconnect is still asking about transactions. +private final class WorkStartingDriver: PluginDriverStub, @unchecked Sendable { + private let connectionId: UUID + private let lock = NSLock() + private var work: UUID? + + init(connectionId: UUID) { + self.connectionId = connectionId + } + + var startedWork: UUID? { lock.withLock { work } } + + func sessionTransactionState() async -> PluginSessionTransactionState { + let connectionId = connectionId + let id = await MainActor.run { + RunningWorkRegistry.shared.begin( + .dataImport, subject: "late.csv", connectionName: "", connectionIds: [connectionId] + ) {} + } + lock.withLock { work = id } + return .idle + } +} diff --git a/apple/TableProTests/Core/Operations/SheetWorkTests.swift b/apple/TableProTests/Core/Operations/SheetWorkTests.swift new file mode 100644 index 0000000000..59483c757e --- /dev/null +++ b/apple/TableProTests/Core/Operations/SheetWorkTests.swift @@ -0,0 +1,155 @@ +import Foundation +@testable import TablePro +import Testing + +@MainActor +struct SheetWorkTests { + @Test("Stopping the scope stops each job still in it, once") + func stopAllStopsEachJobOnce() { + let scope = SheetWorkScope() + var stopped: [String] = [] + _ = scope.begin { stopped.append("export") } + _ = scope.begin { stopped.append("transfer") } + + scope.stopAll() + scope.stopAll() + + #expect(stopped.sorted() == ["export", "transfer"]) + #expect(scope.isEmpty) + } + + @Test("A job that ended is not stopped") + func endedJobIsNotStopped() { + let scope = SheetWorkScope() + var stopped = false + let id = scope.begin { stopped = true } + + scope.end(id) + scope.end(id) + scope.end(nil) + scope.stopAll() + + #expect(!stopped) + } + + @Test("A sheet job is listed for the closers and leaves both lists when it ends") + func sheetJobJoinsAndLeavesBothLists() { + let registry = RunningWorkRegistry(providers: []) + let scope = SheetWorkScope() + let connectionId = UUID() + var stopped = false + let job = SheetJob( + .dataExport, + subject: "orders.csv", + connectionName: "Production", + connectionIds: [connectionId], + registry: registry, + scope: scope + ) { stopped = true } + + #expect(registry.items(in: .connection(connectionId)).map(\.kind) == [.dataExport]) + #expect(!scope.isEmpty) + + job.end() + scope.stopAll() + + #expect(registry.items(in: .app).isEmpty) + #expect(scope.isEmpty) + #expect(!stopped) + } + + @Test("Closing another window on the same connection does not list a sheet's job") + func sheetJobBelongsToItsWindow() { + let registry = RunningWorkRegistry(providers: []) + let ownWindow = UUID() + let scope = SheetWorkScope { ownWindow } + let connectionId = UUID() + let job = SheetJob( + .dataExport, + subject: "orders.csv", + connectionName: "Production", + connectionIds: [connectionId], + registry: registry, + scope: scope + ) {} + + #expect(registry.items(in: .window(ids: [UUID()], connectionIds: [connectionId])).isEmpty) + #expect(registry.items(in: .window(ids: [ownWindow], connectionIds: [connectionId])).count == 1) + #expect(registry.items(in: .connection(connectionId)).count == 1) + job.end() + } + + @Test("A transfer is listed for the window closing its destination's last window") + func transferIsListedForItsDestinationWindow() { + let registry = RunningWorkRegistry(providers: []) + let scope = SheetWorkScope { UUID() } + let source = UUID() + let destination = UUID() + let job = SheetJob( + .tableTransfer, + subject: "Staging", + connectionName: "Production", + connectionIds: [source, destination], + registry: registry, + scope: scope, + listedOnlyForItsWindow: false + ) {} + + #expect(registry.items(in: .window(ids: [UUID()], connectionIds: [destination])).count == 1) + job.end() + } + + @Test("The closers' question and the owner going away reach the same stop") + func bothPathsReachTheStop() { + let registry = RunningWorkRegistry(providers: []) + let scope = SheetWorkScope() + var stops = 0 + _ = SheetJob( + .tableTransfer, + subject: "Staging", + connectionName: "Production", + connectionIds: [UUID(), UUID()], + registry: registry, + scope: scope + ) { stops += 1 } + + registry.cancel(registry.items(in: .app)) + #expect(stops == 1) + scope.stopAll() + #expect(stops == 2) + } + + @Test("Tearing the coordinator down stops what its sheets started") + func coordinatorTeardownStopsSheetWork() { + let fixture = CoordinatorFixture() + var stopped = false + _ = fixture.coordinator.sheetWork.begin { stopped = true } + + fixture.coordinator.teardown() + + #expect(stopped) + } + + @Test("Closing the window stops what the coordinator's sheets started") + func windowCloseStopsSheetWork() { + let fixture = CoordinatorFixture() + var stopped = false + _ = fixture.coordinator.sheetWork.begin { stopped = true } + + fixture.coordinator.handleWindowWillClose() + + #expect(stopped) + } + + @Test("A data file window closing stops the export its sheet started") + func presenterDismissStopsItsExport() { + let presenter = ExportSheetPresenter() + var stopped = false + _ = presenter.sheetWork.begin { stopped = true } + #expect(!stopped) + + presenter.dismiss() + + #expect(stopped) + } +} diff --git a/apple/TableProTests/Core/Services/Infrastructure/AgentModeResultVisibilityTests.swift b/apple/TableProTests/Core/Services/Infrastructure/AgentModeResultVisibilityTests.swift new file mode 100644 index 0000000000..a0aea5ba6a --- /dev/null +++ b/apple/TableProTests/Core/Services/Infrastructure/AgentModeResultVisibilityTests.swift @@ -0,0 +1,98 @@ +import AppKit +import Foundation +@testable import TablePro +import Testing + +// A selected tab's result is off screen while the window draws Agent mode, so it is marked; the +// mark has to go when the tab is on screen again, or it shows on the tab once the user moves off it. +@Suite("Agent mode result visibility", .serialized) +@MainActor +struct AgentModeResultVisibilityTests { + @Test("A selected tab counts as selected while browsing, and not in Agent mode") + func agentModeHidesTheSelectedTab() throws { + let harness = TwoConnectionWindow() + defer { harness.tearDown() } + let coordinator = try harness.coordinator(of: harness.foreground) + let tab = try harness.addSelectedTab(to: harness.foreground) + let windowId = harness.registerWindow(for: coordinator) + defer { WindowLifecycleMonitor.shared.unregisterWindow(for: windowId) } + let owner = OperationOwner.tab(windowId: windowId, tabId: tab.id) + + let browsing = ResultVisibilityResolver.resolve(owner: owner, connectionId: coordinator.connectionId) + #expect(browsing.ownerIsSelectedInWindow) + + harness.controller.setContentMode(.agent) + let inAgentMode = ResultVisibilityResolver.resolve(owner: owner, connectionId: coordinator.connectionId) + + #expect(!inAgentMode.ownerIsSelectedInWindow) + #expect(!inAgentMode.resultIsOnScreen) + } + + @Test("A result that finishes in Agent mode marks the selected tab, and Browse clears it") + func browseClearsTheMarkAgentModeLeft() throws { + let harness = TwoConnectionWindow() + defer { harness.tearDown() } + let coordinator = try harness.coordinator(of: harness.foreground) + let tab = try harness.addSelectedTab(to: harness.foreground) + let windowId = harness.registerWindow(for: coordinator) + defer { WindowLifecycleMonitor.shared.unregisterWindow(for: windowId) } + harness.controller.setContentMode(.agent) + + let visibility = ResultVisibilityResolver.resolve( + owner: .tab(windowId: windowId, tabId: tab.id), + connectionId: coordinator.connectionId + ) + let decision = OperationCompletionPolicy.decide( + completion: OperationCompletion( + kind: .query, + owner: .tab(windowId: windowId, tabId: tab.id), + connectionId: coordinator.connectionId, + connectionName: coordinator.connection.name, + databaseName: nil, + elapsed: .seconds(60), + outcome: .succeeded(OperationSummary(rowsAffected: 3)) + ), + visibility: visibility, + settings: NotificationSettings() + ) + #expect(decision.marksOwnerUnseen) + OperationUnseenMarker.mark(.tab(windowId: windowId, tabId: tab.id), outcome: .succeeded) + #expect(unseenOutcome(of: tab.id, in: coordinator) == .succeeded) + + harness.controller.setContentMode(.browse) + + #expect(unseenOutcome(of: tab.id, in: coordinator) == nil) + } + + @Test("Selecting a connection clears the mark a result left on its selected tab") + func selectingTheConnectionClearsItsMark() throws { + let harness = TwoConnectionWindow() + defer { harness.tearDown() } + let coordinator = try harness.coordinator(of: harness.background) + let tab = try harness.addSelectedTab(to: harness.background) + coordinator.tabManager.mutate(tabId: tab.id) { $0.execution.unseenOutcome = .failed } + + harness.controller.workspaces.select(harness.background.connectionId) + + #expect(unseenOutcome(of: tab.id, in: coordinator) == nil) + } + + @Test("A mark on a tab that is not selected stays when the pane shows again") + func unselectedTabKeepsItsMark() throws { + let harness = TwoConnectionWindow() + defer { harness.tearDown() } + let coordinator = try harness.coordinator(of: harness.foreground) + let marked = try harness.addSelectedTab(to: harness.foreground) + try harness.addSelectedTab(to: harness.foreground) + coordinator.tabManager.mutate(tabId: marked.id) { $0.execution.unseenOutcome = .succeeded } + + harness.controller.setContentMode(.agent) + harness.controller.setContentMode(.browse) + + #expect(unseenOutcome(of: marked.id, in: coordinator) == .succeeded) + } + + private func unseenOutcome(of tabId: UUID, in coordinator: MainContentCoordinator) -> TabUnseenOutcome? { + coordinator.tabManager.tabs.first { $0.id == tabId }?.execution.unseenOutcome + } +} diff --git a/apple/TableProTests/Core/Services/Infrastructure/SheetWorkLifetimeTests.swift b/apple/TableProTests/Core/Services/Infrastructure/SheetWorkLifetimeTests.swift new file mode 100644 index 0000000000..e7f8253acf --- /dev/null +++ b/apple/TableProTests/Core/Services/Infrastructure/SheetWorkLifetimeTests.swift @@ -0,0 +1,69 @@ +import AppKit +import Foundation +@testable import TablePro +import Testing + +// A connection switch and an Agent mode toggle unparent the pane that presents the export and +// transfer sheets, so an `onDisappear` stop would end work nobody cancelled. +@Suite("Sheet work lifetime", .serialized) +@MainActor +struct SheetWorkLifetimeTests { + @Test("Switching connection and back keeps what a sheet started running") + func connectionSwitchKeepsSheetWork() throws { + let harness = TwoConnectionWindow() + defer { harness.tearDown() } + let coordinator = try harness.coordinator(of: harness.foreground) + var stopped = false + _ = coordinator.sheetWork.begin { stopped = true } + + harness.controller.workspaces.select(harness.background.connectionId) + #expect(harness.controller.detailPaneHost.shown === harness.background.panes.detail) + harness.controller.workspaces.select(harness.foreground.connectionId) + + #expect(!stopped) + #expect(!coordinator.sheetWork.isEmpty) + } + + @Test("Agent mode and back keeps what a sheet started running") + func agentModeToggleKeepsSheetWork() throws { + let harness = TwoConnectionWindow() + defer { harness.tearDown() } + let coordinator = try harness.coordinator(of: harness.foreground) + var stopped = false + _ = coordinator.sheetWork.begin { stopped = true } + + harness.controller.setContentMode(.agent) + #expect(harness.controller.detailPaneHost.shown === harness.foreground.panes.agentConversation) + harness.controller.setContentMode(.browse) + + #expect(!stopped) + } + + @Test("Closing the connection's workspace stops what its sheets started") + func workspaceTeardownStopsSheetWork() throws { + let harness = TwoConnectionWindow() + defer { harness.tearDown() } + let coordinator = try harness.coordinator(of: harness.background) + var stopped = false + _ = coordinator.sheetWork.begin { stopped = true } + + harness.background.teardown() + + #expect(stopped) + } + + @Test("A session that ends tears its coordinator down and stops what its sheets started") + func sessionEndStopsSheetWork() throws { + let harness = TwoConnectionWindow() + defer { harness.tearDown() } + let coordinator = try harness.coordinator(of: harness.background) + var stopped = false + _ = coordinator.sheetWork.begin { stopped = true } + + DatabaseManager.shared.removeSession(for: harness.background.connectionId) + harness.controller.refreshFromActiveSessions() + + #expect(harness.background.sessionState == nil) + #expect(stopped) + } +} diff --git a/apple/TableProTests/Core/Services/WorkspaceRailStoreTests.swift b/apple/TableProTests/Core/Services/WorkspaceRailStoreTests.swift index b2acb6fd16..1c3992834b 100644 --- a/apple/TableProTests/Core/Services/WorkspaceRailStoreTests.swift +++ b/apple/TableProTests/Core/Services/WorkspaceRailStoreTests.swift @@ -682,6 +682,56 @@ struct WorkspaceRailCellTextTests { #expect(failed.renderedTint == .systemRed) } + // The selected row's backing is white, so a symbol drawn in the selection's white vanished into it. + @Test("A selected row's lost or failed mark still draws its symbol in its tint", arguments: [ + (ConnectionDisplayStatus.lost(cause: "closed"), NSAppearance.Name.aqua), + (.lost(cause: "closed"), .darkAqua), + (.failed(cause: "refused"), .aqua), + (.failed(cause: "refused"), .darkAqua), + ]) + func selectedRowMarkKeepsItsSymbol(_ status: ConnectionDisplayStatus, _ appearanceName: NSAppearance.Name) throws { + let cell = configuredCell(for: makeEntry(status: status)) + cell.appearance = NSAppearance(named: appearanceName) + let mark = try #require(cell.renderedStatusMark) + + cell.backgroundStyle = .normal + let normal = try tintedPixelCount(of: mark) + cell.backgroundStyle = .emphasized + let selected = try tintedPixelCount(of: mark) + + #expect(normal > 0, "the unselected mark draws its symbol") + #expect(selected > 0, "the selected mark draws its symbol, not a plain disc") + } + + // Pixels whose channels spread like orange or red do; the white or grey backing never does. + private func tintedPixelCount(of view: NSView) throws -> Int { + let scale: CGFloat = 2 + let rep = try #require(NSBitmapImageRep( + bitmapDataPlanes: nil, + pixelsWide: Int(view.bounds.width * scale), + pixelsHigh: Int(view.bounds.height * scale), + bitsPerSample: 8, + samplesPerPixel: 4, + hasAlpha: true, + isPlanar: false, + colorSpaceName: .deviceRGB, + bytesPerRow: 0, + bitsPerPixel: 0 + )) + rep.size = view.bounds.size + view.layoutSubtreeIfNeeded() + view.cacheDisplay(in: view.bounds, to: rep) + var count = 0 + for x in 0 ..< rep.pixelsWide { + for y in 0 ..< rep.pixelsHigh { + guard let color = rep.colorAt(x: x, y: y), color.alphaComponent > 0.5 else { continue } + let channels = [color.redComponent, color.greenComponent, color.blueComponent] + if (channels.max() ?? 0) - (channels.min() ?? 0) > 0.35 { count += 1 } + } + } + return count + } + @Test("Connected and not connected carry no mark", arguments: [ ConnectionDisplayStatus.connected, .notConnected, diff --git a/apple/TableProTests/Core/Storage/RewindAvailabilityTests.swift b/apple/TableProTests/Core/Storage/RewindAvailabilityTests.swift new file mode 100644 index 0000000000..fd24e5f35c --- /dev/null +++ b/apple/TableProTests/Core/Storage/RewindAvailabilityTests.swift @@ -0,0 +1,150 @@ +// +// RewindAvailabilityTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import TableProTestSupport +import Testing + +struct RewindAvailabilityTests { + private let connectionId = UUID() + private let cipher = RewindCipher(keychain: InMemoryKeychain()) + + private func makeStorage() -> QueryHistoryStorage { + let url = FileManager.default.temporaryDirectory + .appendingPathComponent("rewind-availability-\(UUID().uuidString).db") + return QueryHistoryStorage(databaseURL: url, removeDatabaseOnDeinit: true) + } + + private func makeRecord( + table: String = "users", + historyId: UUID? = nil, + capturedAt: Date = Date() + ) -> RewindRecord { + let target = DataWriteTarget(database: "shop", schema: nil, table: table) + return RewindRecord( + id: UUID(), + historyId: historyId, + connectionId: connectionId, + databaseType: .sqlite, + target: target, + capturedAt: capturedAt, + generatedColumns: [], + identityColumns: [], + operations: [ + RowWriteOperation( + kind: .update, + target: target, + columns: ["id", "name"], + primaryKeyColumns: ["id"], + preImage: ["7", "Ada"], + postImage: ["7", "Grace"], + writtenColumns: ["name"], + refusal: nil + ), + ] + ) + } + + private func target(_ table: String = "users") -> RewindAvailability.Target { + RewindAvailability.Target(connectionId: connectionId, database: "shop", schema: nil, table: table) + } + + @Test("A kept save is known by its table and its history row, without reading the payload") + func availabilityListsTargetAndHistoryRow() async { + let storage = makeStorage() + let historyId = UUID() + #expect(await storage.record(TestFixtures.makeHistoryEntry( + id: historyId, connectionId: connectionId, databaseName: "shop", source: .rowEdit + ))) + #expect(await storage.recordRewindSnapshot(makeRecord(historyId: historyId), cipher: cipher)) + + let availability = await storage.rewindAvailability() + #expect(availability.targets == [target()]) + #expect(availability.historyIds == [historyId]) + } + + @Test("A subscriber hears the current state, then every write, delete and clear after it commits") + func updatesFollowEveryChange() async throws { + let storage = makeStorage() + var updates = await storage.rewindAvailabilityUpdates().makeAsyncIterator() + let initial = try #require(await updates.next()) + #expect(initial.targets.isEmpty) + + let record = makeRecord() + await storage.recordRewindSnapshot(record, cipher: cipher) + let written = try #require(await updates.next()) + #expect(written.targets == [target()]) + #expect(written.revision > initial.revision) + + await storage.recordRewindSnapshot(makeRecord(table: "orders"), cipher: cipher) + _ = await updates.next() + await storage.deleteRewindSnapshot(id: record.id) + let deleted = try #require(await updates.next()) + #expect(deleted.targets == [target("orders")]) + + await storage.clearRewindSnapshots() + let cleared = try #require(await updates.next()) + #expect(cleared.targets.isEmpty) + } + + @Test("Retention pruning a save publishes that it is gone") + func pruningPublishes() async throws { + let storage = makeStorage() + let now = Date() + await storage.recordRewindSnapshot( + makeRecord(capturedAt: now.addingTimeInterval(-30 * 24 * 60 * 60)), cipher: cipher + ) + var updates = await storage.rewindAvailabilityUpdates().makeAsyncIterator() + let kept = try #require(await updates.next()) + #expect(kept.targets == [target()]) + + await storage.pruneRewindSnapshots(now: now) + let pruned = try #require(await updates.next()) + #expect(pruned.targets.isEmpty) + } + + @Test("Deleting a connection's history publishes that its saves are gone") + func deletingTheConnectionPublishes() async throws { + let storage = makeStorage() + await storage.recordRewindSnapshot(makeRecord(), cipher: cipher) + var updates = await storage.rewindAvailabilityUpdates().makeAsyncIterator() + let kept = try #require(await updates.next()) + #expect(kept.targets == [target()]) + + await storage.deleteEverything(forConnection: connectionId) + let deleted = try #require(await updates.next()) + #expect(deleted.targets.isEmpty) + } + + @Test("A save written after its history row is picked up without the drawer reloading") + @MainActor + func snapshotWrittenAfterHistoryRowIsPickedUp() async throws { + let storage = makeStorage() + let manager = QueryHistoryManager(storage: storage, isCapturePaused: { false }) + let cache = manager.rewindAvailability + let historyId = UUID() + + #expect(await manager.record(TestFixtures.makeHistoryEntry( + id: historyId, connectionId: connectionId, databaseName: "shop", source: .rowEdit + ))) + try await waitUntil { cache.availability.revision >= 0 } + #expect(!cache.hasSnapshot(forHistoryId: historyId)) + + await storage.recordRewindSnapshot(makeRecord(historyId: historyId), cipher: cipher) + try await waitUntil { cache.hasSnapshot(forHistoryId: historyId) } + #expect(cache.hasSnapshot(for: target())) + } + + @Test("An older reading never replaces a newer one") + @MainActor + func olderReadingIsIgnored() { + let cache = RewindAvailabilityCache(storage: makeStorage()) + cache.adopt(RewindAvailability(revision: 5, targets: [target()], historyIds: [])) + cache.adopt(RewindAvailability(revision: 4, targets: [], historyIds: [])) + #expect(cache.hasSnapshot(for: target())) + } +} diff --git a/apple/TableProTests/Core/WritePolicy/WritePolicyFixture.swift b/apple/TableProTests/Core/WritePolicy/WritePolicyFixture.swift index f8d40e9200..0b684aefa2 100644 --- a/apple/TableProTests/Core/WritePolicy/WritePolicyFixture.swift +++ b/apple/TableProTests/Core/WritePolicy/WritePolicyFixture.swift @@ -121,6 +121,9 @@ final class WritePolicyDriver: PluginDriverStub, @unchecked Sendable { record(query) } + // Idle, so the disconnect cells reach the session: an MCP disconnect refuses a session that cannot say. + func sessionTransactionState() async -> PluginSessionTransactionState { .idle } + func executeParameterized(query: String, parameters: [PluginCellValue]) async throws -> PluginQueryResult { lock.withLock { bound.append(parameters) } return record(query) diff --git a/apple/TableProTests/Helpers/TwoConnectionWindow.swift b/apple/TableProTests/Helpers/TwoConnectionWindow.swift new file mode 100644 index 0000000000..74f2328621 --- /dev/null +++ b/apple/TableProTests/Helpers/TwoConnectionWindow.swift @@ -0,0 +1,114 @@ +// +// TwoConnectionWindow.swift +// TableProTests +// + +import AppKit +import Foundation +@testable import TablePro + +// One window hosting two connected connections, each with its coordinator. Agent sessions live in a +// registry of its own, since stopping a shared one writes to the app's real conversation store. +@MainActor +internal struct TwoConnectionWindow { + let controller: MainSplitViewController + let foreground: ConnectionWorkspace + let background: ConnectionWorkspace + let agentSessions: AgentSessionRegistry + let window: NSWindow + private let registryDirectory: URL + + init() { + registryDirectory = FileManager.default.temporaryDirectory + .appendingPathComponent("TwoConnectionWindow-\(UUID().uuidString)", isDirectory: true) + agentSessions = AgentSessionRegistry(store: AgentSessionStore(directory: registryDirectory)) + let foregroundConnection = TestFixtures.makeConnection(name: "Foreground") + let backgroundConnection = TestFixtures.makeConnection(name: "Background") + foreground = Self.makeWorkspace(connection: foregroundConnection, agentSessions: agentSessions) + background = Self.makeWorkspace(connection: backgroundConnection, agentSessions: agentSessions) + Self.injectSession(for: foregroundConnection) + Self.injectSession(for: backgroundConnection) + + controller = MainSplitViewController(payload: nil, sessionState: nil, adopting: foreground) + controller.workspaces.insert(background, select: false) + + window = NSWindow( + contentRect: NSRect(x: 0, y: 0, width: 1_200, height: 700), + styleMask: [.titled], + backing: .buffered, + defer: false + ) + window.isReleasedWhenClosed = false + window.contentViewController = controller + window.orderFront(nil) + controller.refreshFromActiveSessions() + } + + func coordinator(of workspace: ConnectionWorkspace) throws -> MainContentCoordinator { + guard let coordinator = workspace.sessionState?.coordinator else { + throw TwoConnectionWindowError.noCoordinator(workspace.connectionId) + } + return coordinator + } + + @discardableResult + func addSelectedTab(to workspace: ConnectionWorkspace) throws -> QueryTab { + let coordinator = try coordinator(of: workspace) + let tab = QueryTab(title: "Query", tabType: .query) + coordinator.tabManager.tabs.append(tab) + coordinator.tabManager.selectedTabId = tab.id + return tab + } + + func registerWindow(for coordinator: MainContentCoordinator) -> UUID { + let windowId = UUID() + coordinator.windowId = windowId + WindowLifecycleMonitor.shared.register(window: window, connectionId: coordinator.connectionId, windowId: windowId) + return windowId + } + + func tearDown() { + for workspace in [foreground, background] where workspace.contentMode == .agent { + controller.setContentMode(.browse, for: workspace.connectionId) + } + if controller.isSidebarCollapsed { controller.toggleSidebar(nil) } + if controller.isTrailingPaneOpen { controller.hideTrailingPane() } + for session in agentSessions.sessions { + agentSessions.removeSession(id: session.id) + } + window.orderOut(nil) + window.contentViewController = nil + background.teardown() + foreground.teardown() + DatabaseManager.shared.removeSession(for: foreground.connectionId) + DatabaseManager.shared.removeSession(for: background.connectionId) + try? FileManager.default.removeItem(at: registryDirectory) + } + + private static func injectSession(for connection: DatabaseConnection) { + var session = ConnectionSession(connection: connection, driver: MockDatabaseDriver(connection: connection)) + session.status = .connected + DatabaseManager.shared.injectSession(session, for: connection.id) + } + + private static func makeWorkspace( + connection: DatabaseConnection, + agentSessions: AgentSessionRegistry + ) -> ConnectionWorkspace { + ConnectionWorkspace( + connectionId: connection.id, + payload: nil, + autoConnect: false, + payloadConnection: connection, + session: nil, + sessionState: nil, + trailingPaneState: nil, + phase: .connecting, + agentSessions: agentSessions + ) + } +} + +internal enum TwoConnectionWindowError: Error { + case noCoordinator(UUID) +} diff --git a/apple/TableProTests/Helpers/WorkspaceRailCellView+Label.swift b/apple/TableProTests/Helpers/WorkspaceRailCellView+Label.swift index 4da4fa658b..4d0bc56a15 100644 --- a/apple/TableProTests/Helpers/WorkspaceRailCellView+Label.swift +++ b/apple/TableProTests/Helpers/WorkspaceRailCellView+Label.swift @@ -30,6 +30,6 @@ extension WorkspaceRailCellView { extension ConnectionStatusMarkView { var renderedTint: NSColor? { - subviews.lazy.compactMap { $0 as? NSImageView }.first?.contentTintColor + displayedTint } } diff --git a/apple/TableProTests/SOCKS/FakeSOCKS5Server.swift b/apple/TableProTests/SOCKS/FakeSOCKS5Server.swift index bf1d53682a..0bec467e58 100644 --- a/apple/TableProTests/SOCKS/FakeSOCKS5Server.swift +++ b/apple/TableProTests/SOCKS/FakeSOCKS5Server.swift @@ -6,6 +6,7 @@ import Foundation import Network import os +import TableProTestSupport final class FakeSOCKS5Server: @unchecked Sendable { enum Behavior: Sendable { @@ -30,6 +31,8 @@ final class FakeSOCKS5Server: @unchecked Sendable { private let behavior: Behavior private let requiredUsername: String? private let requiredPassword: String? + // Holds every CONNECT reply until opened, so a tunnel creation can be kept mid-probe. + private let connectReplyGate: AsyncGate? private let captured = OSAllocatedUnfairLock(initialState: CapturedState()) private var listener: NWListener? @@ -47,10 +50,16 @@ final class FakeSOCKS5Server: @unchecked Sendable { captured.withLock { $0.sawAuthNegotiation } } - init(behavior: Behavior = .echo, username: String? = nil, password: String? = nil) { + init( + behavior: Behavior = .echo, + username: String? = nil, + password: String? = nil, + connectReplyGate: AsyncGate? = nil + ) { self.behavior = behavior self.requiredUsername = username self.requiredPassword = password + self.connectReplyGate = connectReplyGate } func start() async throws { @@ -117,6 +126,7 @@ final class FakeSOCKS5Server: @unchecked Sendable { let request = try await readConnectRequest(connection) captured.withLock { $0.connectRequests.append(request) } + await connectReplyGate?.wait() try await send(connection, Data([0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0])) echoLoop(connection) } diff --git a/apple/TableProTests/Views/Main/CoordinatorRewindAvailabilityTests.swift b/apple/TableProTests/Views/Main/CoordinatorRewindAvailabilityTests.swift new file mode 100644 index 0000000000..ab35ec5911 --- /dev/null +++ b/apple/TableProTests/Views/Main/CoordinatorRewindAvailabilityTests.swift @@ -0,0 +1,127 @@ +// +// CoordinatorRewindAvailabilityTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import TableProTestSupport +import Testing + +@MainActor +struct CoordinatorRewindAvailabilityTests { + private let storage: QueryHistoryStorage + private let fixture: CoordinatorFixture + private let cipher = RewindCipher(keychain: InMemoryKeychain()) + + init() { + let url = FileManager.default.temporaryDirectory + .appendingPathComponent("coordinator-rewind-\(UUID().uuidString).db") + storage = QueryHistoryStorage(databaseURL: url, removeDatabaseOnDeinit: true) + let manager = QueryHistoryManager(storage: storage, isCapturePaused: { false }) + fixture = CoordinatorFixture(services: AppServices.live.replacingQueryHistory(manager)) + fixture.addTab(QueryTab(title: "users", query: "SELECT * FROM users", tabType: .table, tableName: "users")) + } + + private var coordinator: MainContentCoordinator { fixture.coordinator } + + private func makeRecord(table: String, historyId: UUID? = nil) throws -> RewindRecord { + let scope = try #require(coordinator.selectedTabScope) + let target = DataWriteTarget(database: scope.database, schema: scope.schema, table: table) + return RewindRecord( + id: UUID(), + historyId: historyId, + connectionId: fixture.connection.id, + databaseType: fixture.connection.type, + target: target, + capturedAt: Date(), + generatedColumns: [], + identityColumns: [], + operations: [ + RowWriteOperation( + kind: .update, + target: target, + columns: ["id", "name"], + primaryKeyColumns: ["id"], + preImage: ["7", "Ada"], + postImage: ["7", "Grace"], + writtenColumns: ["name"], + refusal: nil + ), + ] + ) + } + + @Test("Restore Previous Values stays off until the selected table has a kept save") + func offUntilASaveIsKept() async throws { + let cache = coordinator.services.queryHistoryManager.rewindAvailability + try await waitUntil { cache.availability.revision >= 0 } + #expect(!coordinator.canRewindSelectedTab) + + await storage.recordRewindSnapshot(try makeRecord(table: "orders"), cipher: cipher) + try await waitUntil { cache.availability.targets.count == 1 } + #expect(!coordinator.canRewindSelectedTab) + + let kept = try makeRecord(table: "users") + await storage.recordRewindSnapshot(kept, cipher: cipher) + try await waitUntil { coordinator.canRewindSelectedTab } + + await storage.deleteRewindSnapshot(id: kept.id) + try await waitUntil { !coordinator.canRewindSelectedTab } + } + + @Test("The drawer's Restore Previous Values follows its own save, not the selected table") + func drawerFollowsItsOwnSave() async throws { + let historyId = UUID() + let cache = coordinator.services.queryHistoryManager.rewindAvailability + try await waitUntil { cache.availability.revision >= 0 } + #expect(await storage.record(TestFixtures.makeHistoryEntry( + id: historyId, connectionId: fixture.connection.id, source: .rowEdit + ))) + #expect(!coordinator.canRestoreSave(historyId: historyId)) + + #expect(await storage.recordRewindSnapshot(try makeRecord(table: "orders", historyId: historyId), cipher: cipher)) + try await waitUntil { coordinator.canRestoreSave(historyId: historyId) } + #expect(!coordinator.canRewindSelectedTab) + } +} + +@MainActor +private extension AppServices { + func replacingQueryHistory(_ manager: QueryHistoryManager) -> AppServices { + AppServices( + appEvents: appEvents, + appSettings: appSettings, + appSettingsStorage: appSettingsStorage, + connectionStorage: connectionStorage, + databaseManager: databaseManager, + pluginManager: pluginManager, + schemaService: schemaService, + schemaRefreshService: schemaRefreshService, + schemaProviderRegistry: schemaProviderRegistry, + catalogChangeService: catalogChangeService, + sqlFavoriteManager: sqlFavoriteManager, + favoriteTablesStorage: favoriteTablesStorage, + favoriteDatabasesStorage: favoriteDatabasesStorage, + tableFolderStorage: tableFolderStorage, + aiChatStorage: aiChatStorage, + aiKeyStorage: aiKeyStorage, + aiAccessApprovals: aiAccessApprovals, + groupStorage: groupStorage, + tagStorage: tagStorage, + sshProfileStorage: sshProfileStorage, + credentialProfileStorage: credentialProfileStorage, + licenseManager: licenseManager, + syncMetadataStorage: syncMetadataStorage, + favoritesExpansionState: favoritesExpansionState, + linkedFolderWatcher: linkedFolderWatcher, + queryHistoryManager: manager, + dateFormattingService: dateFormattingService, + copilotService: copilotService, + mcpServerManager: mcpServerManager, + syncTracker: syncTracker, + welcomeRouter: welcomeRouter + ) + } +} diff --git a/apple/changelog.d/4018.changed.md b/apple/changelog.d/4018.changed.md new file mode 100644 index 0000000000..92d1c8a116 --- /dev/null +++ b/apple/changelog.d/4018.changed.md @@ -0,0 +1 @@ +The MCP disconnect tool refuses while the connection has running work, unsaved changes or an open transaction. diff --git a/apple/changelog.d/4018.fixed.2.md b/apple/changelog.d/4018.fixed.2.md new file mode 100644 index 0000000000..8bab38fc07 --- /dev/null +++ b/apple/changelog.d/4018.fixed.2.md @@ -0,0 +1 @@ +An export or table transfer kept running with no window after a disconnect that did not ask first. diff --git a/apple/changelog.d/4018.fixed.3.md b/apple/changelog.d/4018.fixed.3.md new file mode 100644 index 0000000000..9f5f688ffe --- /dev/null +++ b/apple/changelog.d/4018.fixed.3.md @@ -0,0 +1 @@ +The selected connection in the sidebar showed a blank white badge when it was lost or failed. diff --git a/apple/changelog.d/4018.fixed.4.md b/apple/changelog.d/4018.fixed.4.md new file mode 100644 index 0000000000..bc55f15a66 --- /dev/null +++ b/apple/changelog.d/4018.fixed.4.md @@ -0,0 +1 @@ +On iOS, the NULL button moved across the field when turned on while editing a row. diff --git a/apple/changelog.d/4018.fixed.md b/apple/changelog.d/4018.fixed.md new file mode 100644 index 0000000000..f1f541e6f4 --- /dev/null +++ b/apple/changelog.d/4018.fixed.md @@ -0,0 +1 @@ +Restore Previous Values stayed enabled with nothing to restore, then only showed an alert.