@@ -80,6 +80,11 @@ final class QueryViewModel {
8080 /// The stream of the most recent `loadSchema` call, used to drop a stale
8181 /// in-flight response when the user rapidly switches streams.
8282 private(set) var latestSchemaStream: String?
83+ /// The stream of the most recent `executeQuery` call. Defense-in-depth
84+ /// alongside `queryGeneration`/task cancellation: drops a stale in-flight
85+ /// query response if a newer query for a different stream supersedes it
86+ /// without going through the normal cancel path.
87+ private var latestQueryStream: String?
8388
8489 // Query task for cancellation
8590 private var queryTask: Task<Void, Never>?
@@ -415,6 +420,7 @@ final class QueryViewModel {
415420 // the loading indicator during this query).
416421 queryGeneration &+= 1
417422 let generation = queryGeneration
423+ latestQueryStream = stream
418424
419425 isLoading = true
420426 errorMessage = nil
@@ -442,6 +448,9 @@ final class QueryViewModel {
442448 // cancellation landing just after the check above would otherwise
443449 // let a stale query clobber the newer one's results/columns.
444450 guard !Task.isCancelled else { return }
451+ // Drop the response if a newer query (possibly for a different
452+ // stream) has since been launched without cancelling this task.
453+ guard latestQueryStream == stream else { return }
445454
446455 results = queryResults
447456 // Drop any prior row selection: the new result set reassigns row
@@ -484,6 +493,9 @@ final class QueryViewModel {
484493 return
485494 } catch {
486495 guard !Task.isCancelled else { return }
496+ // Same supersession guard as the success path: a stale failed
497+ // query must not clobber a newer query's state.
498+ guard latestQueryStream == stream else { return }
487499 errorMessage = ParseableError.userFriendlyMessage(for: error)
488500 if let serverError = error as? ParseableError,
489501 case .serverError(_, let msg) = serverError,
@@ -682,6 +694,10 @@ final class QueryViewModel {
682694 errorRange = nil
683695 resultsTruncated = false
684696 schemaFields = []
697+ // Keep the stream trackers consistent with the cleared schema/results so
698+ // a later reload guard never sees a non-nil stream paired with empty data.
699+ latestSchemaStream = nil
700+ latestQueryStream = nil
685701 }
686702
687703 /// Modifies `sqlQuery` to add a column-value filter condition.