// One screen architecture, held by a sweep. // // Before lane I1 the phone had two: the Work lane's scaffold (`ScreenRead`, // `ScreenPage`, `ScreenWriter`) and fifty-four `*Host: View` structs that each // hand-wrote the same `@State` model, the same `init(core:toasts:…)` and the // same `.rereading { model.reread($0) }`, with the core threaded by hand // through 352 `core:` parameters. `Scaffold/Hosted.swift` is that shape written // once, and `EnvironmentValues.cottonCore` is the core travelling down the // tree instead of through every init. This file is what keeps a fifty-fifth // host, or a new `core:` on a View, from appearing. // // FOUR RULES, each its own test: // 1. no `*Host: View` anywhere but the BOOT host — `Hosted` is not a // `*Host`, and the only one allowed is the app's stack container // (`Boot/StackHost.swift`; the Mac's columns host went with the Mac // client, architect 20 Oct 2026), named with the sentence that earns it // and checked to still trip the matcher; // 2. no `core:` parameter on any View's `init` outside `CottonKit`, // `Scaffold/` and `Boot/RootView.swift` (the root builds the ten // app-lifetime models in its own init and is handed the core once); // 3. (a model may take the core; the per-file ledger of how many did was // bookkeeping and is gone — test policy, 11 Oct 2026); // 4. `@Environment(SyncModel.self)` appears nowhere — the sync facts reach // every screen through the ONE optional key, `\.cottonSync`. // // Modelled on `CatalogueGuardTests`: comments are blanked with their offsets // kept so a line number can be reported, the matcher is run once over a // synthetic source so it keeps asserting after the tree is clean. import Foundation import XCTest // MARK: - The sweep enum ScaffoldSweep { struct Site: Equatable { let file: String let line: Int let what: String } /// The files whose `core:` is the root's own or the scaffold's. static func isScaffoldOrRoot(_ file: String) -> Bool { file.hasPrefix("CottonKit/") || file.hasPrefix("CottonUI/Scaffold/") || file == "CottonUI/Boot/RootView.swift" } /// Files the sweep leaves alone for a reason that is somebody else's. Each /// row is checked to still trip the sweep, so a deleted file cannot leave a /// row behind. EMPTY since the sharing lane deleted its screens (the one row /// this held), and the emptiness is checked the same way. static let deferred: [String: String] = [:] /// An app with its own root (the redesigns, 20 Nov 2026: each app is its own object, with its own /// screens and no fan): RootView hands the app's root the PERSONAL core, as it hands its own models, /// and the root hands it to the parts that read the device's own things. `\.cottonCore` is the shown /// database's gated core, `Hosted` the old shared scaffold's model builder. Each row is a type in its /// file, checked to still take the core, so a row cannot outlive its view. static let ownRoots: [String: String] = [ "CottonUI/Story/Ink/InkStory.swift InkStoryRoot": "Story's root (RootView.hosts), builds its InkStory model over the personal core", "CottonUI/Notes/NotesHome.swift NotesRoot": "Words' root (RootView.hosts), builds its model over the personal core", "CottonUI/Days/DaysRoot.swift DaysRoot": "Days' root (RootView.hosts), builds its model over the personal core", "CottonUI/Cinemas/CinemasRoot.swift CinemasRoot": "Cinemas' root (RootView, the streaming app since 26 Nov 2026), builds its CinemasModel and the core's source over the personal core", "CottonUI/Chat/ChatRoot.swift ChatRoot": "Comms' root (RootView.hosts), makes its ChatBook over the personal core", "CottonUI/Post/PostRoot.swift PostRoot": "Post's root (RootView.hosts, 97413f36f9), builds its PostModel over the personal core", "CottonUI/Sites/SitesRoot.swift SitesRoot": "Sites' root (RootView.hosts, 0c979a27b9), builds its SitesModel over the personal core", "CottonUI/DataApp/DataRoot.swift DataRoot": "Data's root (RootView.hosts), builds its DataModel over the personal core", "CottonUI/Albums/AlbumsRoot.swift AlbumsRoot": "Albums' root (RootView.hosts), builds its AlbumsModel over the personal core", "CottonUI/Contacts/ContactsRoot.swift ContactsRoot": "Contacts' root (RootView.hosts), builds its ContactsModel over the personal core", "CottonUI/Chat/ChatYou.swift ChatYouScreen": "Comms' You, handed the core by ChatRoot: the device's account, its chat profile and blocked list", "CottonUI/Chrome/SettingsParts.swift SettingsParts": "each own-look app's Settings: the device's storage, report and About, through the core the app's root holds", "CottonUI/Chrome/SettingsParts.swift YourClaudePart": "each own-look app's Your Claude: this device's assistant scope and Connect Claude, through the core the app's root holds", "CottonUI/Chat/ChatYouPages.swift ChatYouClaudePage": "a page of Comms' You (15f2c8d433), handed the core by ChatYouScreen: this device's assistant scope", "CottonUI/Chat/ChatYouPages.swift ChatYouStoragePage": "a page of Comms' You (15f2c8d433), handed the core by ChatYouScreen: this device's storage", "CottonUI/Chat/ChatYouPages.swift ChatYouHelpPage": "a page of Comms' You (15f2c8d433), handed the core by ChatYouScreen: the report a person sends", "CottonUI/Chrome/DeleteAccountPart.swift DeleteAccountPart": "each own-look app's Delete account (e968c8e6a6), a Settings part handed the core its root holds, as SettingsParts is", "CottonUI/Chat/ChatYouLook.swift ChatYouLookPage": "a page of Comms' You (b4605f4868), handed the core by ChatYouScreen: the look the core answers and keeps", "CottonUI/Chat/ChatExport.swift ChatYouExportPage": "a page of Comms' You (5f8a822718), handed the core by ChatYouScreen: export, made by the core", "CottonUI/Chrome/SettingsGrouped.swift SettingsYouCard": "each own-look app's account card in Settings (dfbfab0e52), handed the core its root holds, as SettingsParts is", "CottonUI/Chrome/SettingsGrouped.swift SettingsSyncPage": "each own-look app's Sync page in Settings (dfbfab0e52), handed the core its root holds, as SettingsParts is", "CottonUI/Chrome/SettingsGrouped.swift SettingsProfileFoot": "each own-look app's Profile foot in Settings (dfbfab0e52), handed the core its root holds, as SettingsParts is", "CottonUI/Chrome/SettingsGrouped.swift SettingsPrivacyPage": "each own-look app's Privacy page in Settings (dfbfab0e52), handed the core its root holds, as SettingsParts is", "CottonUI/Chrome/SettingsGrouped.swift SettingsSharedPage": "each own-look app's shared Settings pages (dfbfab0e52), handed the core its root holds, as SettingsParts is", ] static func isDeferred(_ file: String) -> Bool { deferred.keys.contains { file.hasPrefix($0) } } static func lineNumber(of offset: Int, in chars: [Character]) -> Int { var line = 1 var i = 0 while i < offset, i < chars.count { if chars[i] == "\n" { line += 1 } i += 1 } return line } // Rule 1 ---------------------------------------------------------------- static let hostPattern = #"\bstruct\s+\w+Host\b\s*(?:<[^>]*>)?\s*:\s*[^{]*\bView\b"# static func hosts(in file: String, _ text: String) throws -> [Site] { let chars = ToolCallSweep.blankingComments(text) let code = String(chars) let regex = try NSRegularExpression(pattern: hostPattern) let range = NSRange(code.startIndex..., in: code) return regex.matches(in: code, range: range).map { match in let offset = code.distance(from: code.startIndex, to: Range(match.range, in: code)!.lowerBound) let matched = String(code[Range(match.range, in: code)!]) // The type's name alone: `struct MoneyHost:` and // `struct LegalHost:` both answer their `\w+Host`. let name = matched.range(of: #"\w+Host\b"#, options: .regularExpression).map { String(matched[$0]) } ?? matched return Site(file: file, line: lineNumber(of: offset, in: chars), what: name) } } // Rule 2 ---------------------------------------------------------------- /// A type declaration line: `struct Foo: View {`, `public final class Bar {`, /// `@MainActor @Observable final class Baz {` — attributes on the same /// line are skipped, or a model's init would be read as the View's above it. static let declarationPattern = #"^\s*(?:@\w+(?:\([^)]*\))?\s+)*(?:(?:public|private|internal|fileprivate|package|final|open)\s+)*(struct|class|enum|actor|extension)\s+(\w+)([^{]*)"# /// Every `init(` whose parameter list names `core:`, and every stored /// `let core` / `var core`, whose nearest enclosing type declaration is a /// View. Both shapes: a memberwise init is an init the compiler wrote. static func viewInitsTakingCore(in file: String, _ text: String) throws -> [Site] { let chars = ToolCallSweep.blankingComments(text) let lines = String(chars).split(separator: "\n", omittingEmptySubsequences: false).map(String.init) let declaration = try NSRegularExpression(pattern: declarationPattern) var found: [Site] = [] for (index, line) in lines.enumerated() { let stored = line.range(of: #"^\s*(?:private |fileprivate |public |internal )?(?:let|var) core\s*:"#, options: .regularExpression) != nil guard stored || InitDeclarations.isInitDeclaration(line) else { continue } if stored { // A getter has no memberwise initializer parameter. Its brace // may stand on the next line; an observer still belongs to a // stored property, and an '=' still introduces a stored value. var property = line var next = index + 1 if !property.contains("{") && !property.contains("=") { while next < lines.count, lines[next].trimmingCharacters(in: .whitespaces).isEmpty { next += 1 } if next < lines.count, lines[next].trimmingCharacters(in: .whitespaces).hasPrefix("{") { property += lines[next] next += 1 } } if let brace = property.firstIndex(of: "{"), !property[..= 0 { let candidate = lines[up] let range = NSRange(candidate.startIndex..., in: candidate) if let match = declaration.firstMatch(in: candidate, range: range) { enclosing = ( String(candidate[Range(match.range(at: 1), in: candidate)!]), String(candidate[Range(match.range(at: 2), in: candidate)!]), String(candidate[Range(match.range(at: 3), in: candidate)!]) ) break } up -= 1 } guard let enclosing else { continue } let isView = enclosing.rest.range(of: #"\bView\b"#, options: .regularExpression) != nil || knownViews.contains(enclosing.name) if isView { found.append(Site(file: file, line: index + 1, what: enclosing.name)) } } return found } /// Views whose `init` lives in an `extension` that does not restate the /// conformance — `Hosted` is the only one so far. static let knownViews: Set = ["Hosted"] } // MARK: - The tests final class ScaffoldSweepTests: XCTestCase { /// **The BOOT host, and nothing else may join it.** /// /// The rule is about the fifty-four SCREEN adapters `Hosted` replaced: a /// `*Host: View` that hand-wrote a model, an `init(core:…)` and a /// `.rereading`. This one is a different animal and the design spec /// names it by path (§5.2): it takes no core, builds no model and draws /// no screen — it is the app's ONE stack container. A second would mean a /// second destination, which is the thing worth failing over. private static let bootHosts: [String: String] = [ "CottonUI/Boot/StackHost.swift": "the phone's ONE NavigationStack, rooted on Home — a stack container, never a model adapter", ] func testNoHostAdapterAnywhereButTheBootHost() throws { var offenders: [ScaffoldSweep.Site] = [] for (name, text) in try AppSources.all() where Self.bootHosts[name] == nil { offenders += try ScaffoldSweep.hosts(in: name, text) } XCTAssertEqual( offenders, [], "a `*Host: View`: " + offenders.map { "\($0.file):\($0.line) \($0.what)" }.joined(separator: ", ") + " — route it through `Hosted` instead" ) } func testNoViewInitTakesTheCore() throws { var offenders: [ScaffoldSweep.Site] = [] var deferredTripped: Set = [] var ownTripped: Set = [] for (name, text) in try AppSources.all() where !ScaffoldSweep.isScaffoldOrRoot(name) { let sites = try ScaffoldSweep.viewInitsTakingCore(in: name, text) if ScaffoldSweep.isDeferred(name) { if !sites.isEmpty { deferredTripped.formUnion(ScaffoldSweep.deferred.keys.filter { name.hasPrefix($0) }) } continue } for site in sites { if ScaffoldSweep.ownRoots["\(site.file) \(site.what)"] != nil { ownTripped.insert("\(site.file) \(site.what)") } else { offenders.append(site) } } } for (site, reason) in ScaffoldSweep.ownRoots { XCTAssertTrue(ownTripped.contains(site), "\(site) is an own root (\(reason)) but no longer takes the core — drop the row") } for (prefix, reason) in ScaffoldSweep.deferred { XCTAssertTrue(deferredTripped.contains(prefix), "\(prefix) is deferred (\(reason)) but nothing under it takes the core any more — drop the row") } XCTAssertEqual( offenders, [], "a View taking `core:` in its init: " + offenders.map { "\($0.file):\($0.line) \($0.what)" }.joined(separator: ", ") + " — the core is `\\.cottonCore`, and a model is built by `Hosted`" ) } func testTheSyncModelIsReadThroughTheOneOptionalKey() throws { var offenders: [String] = [] for (name, text) in try AppSources.all() { let code = String(ToolCallSweep.blankingComments(text)) if code.contains("@Environment(SyncModel.self)") { offenders.append(name) } } XCTAssertEqual(offenders, [], "read `\\.cottonSync` (optional) instead") } // MARK: - Anti-vacuity // RULE: The view-init guard rejects stored core parameters, including observed properties, but a computed core getter derived from the environment is not an initializer argument. func testComputedCoreAccessorsAreNotMemberwiseInitializerParameters() throws { let synthetic = """ struct Computed: View { private var core: (any CottonCalling)? { environmentCore } } struct SplitComputed: View { var core: any CottonCalling { get { environmentCore } } } struct Observed: View { var core: any CottonCalling { didSet {} } } struct SplitObserved: View { var core: any CottonCalling { // Comments and blank lines cannot hide the observer. willSet {} } } struct Defaulted: View { var core: any CottonCalling = makeCore() } """ let sites = try ScaffoldSweep.viewInitsTakingCore(in: "synthetic", synthetic) XCTAssertEqual(sites.map(\.what), ["Observed", "SplitObserved", "Defaulted"]) } func testTheMatchersCatchEachShape() throws { let synthetic = """ import SwiftUI // struct CommentedHost: View — a comment is not a host struct MoneyHost: View { init(core: any CottonCalling) {} var body: some View { EmptyView() } } public struct LegalHost: View { var body: some View { EmptyView() } } @MainActor @Observable final class MoneyModel { init(core: any CottonCalling, toasts: ToastCenter) {} } struct Plain: View { init( model: MoneyModel, core: any CottonCalling ) {} } struct Stored: View { let core: any CottonCalling var body: some View { EmptyView() } } """ let hosts = try ScaffoldSweep.hosts(in: "synthetic", synthetic) XCTAssertEqual(hosts.map(\.what), ["MoneyHost", "LegalHost"]) XCTAssertEqual(hosts.map(\.line), [3, 7]) let inits = try ScaffoldSweep.viewInitsTakingCore(in: "synthetic", synthetic) XCTAssertEqual(inits.map(\.what), ["MoneyHost", "Plain", "Stored"], "a model's init is not a View's; a stored core is") } }