# THE FACTORY SWIFT & XCODE DIRECTIVES (SIDECAR) ### 1. Swift Architecture Rules - **SWIFTDATA ISOLATION:** Never use `default.store` or generic `ModelContainer` setups. Always resolve isolated SQLite URLs via `FactoryPaths.databaseURL(for:storeName:)`. - **STRICT CONCURRENCY:** Adhere strictly to Swift 6.4 actor boundaries and `@MainActor` isolation. - **WORKSPACE RELATIVE PATHS & NORMALIZATION:** Standard app source files reside at `[ProjectName]/[FileName].swift` relative to workspace root. * **PROMPT PATH NORMALIZATION:** If a prompt contains duplicated project folder prefixes (e.g. `CrossTalk/CrossTalk/File.swift`), normalize to `[ProjectName]/[FileName].swift` before calling file tools. * **SUBFOLDER PATH DEDUPLICATION:** If any path develops multiplied project segments (e.g. `.../CrossTalk/CrossTalk/...`), collapse immediately to the canonical single-subfolder path. - **PROJECT BUNDLES OFF-LIMITS:** Never read, search, or edit files inside `*.xcodeproj` or `*.xcworkspace` (especially `project.pbxproj`). Xcode picks up new files on disk automatically and links binaries dynamically from `$SHARED_BINARIES`. - **DYNAMIC AGENT RESOLUTION:** Never hardcode versioned agent names (e.g. `"Frank 2.0"`, `"Mike 13.0"`). Always resolve dynamically via `FactoryRegistryManager.shared.resolveActiveAgent(baseName:)`. - **STANDARDIZED IPC (`FactoryKit`):** Never use raw `DistributedNotificationCenter.default()`. Always import `FactoryKit` and use `NotificationCenter.postDistributed(...)` and `NotificationCenter.addDistributedObserver(...)`. - **FRAMEWORK BOUNDARIES & STUB TRUTH:** * **STUBS ARE THE API SOURCE OF TRUTH:** To inspect Factory framework signatures (`FactoryKit`, `StreamlineFoundation`, `WarehouseKit`, `AIKit`, `BrowserKit`, etc.), ALWAYS read `[ProjectName]/Framework_Stubs/.api.swift` (or `apps/Framework Manager/Framework_Stubs/` in monorepos) inside your active workspace—NEVER run `find` or `grep` across `$FACTORY_HOME/xcode` or read external/legacy directories outside the workspace root (especially obsolete trees like `workspace_v9` or `Project Reference Files`). * Files in `Framework_Stubs/*.api.swift` are read-only headers. Do NOT edit stubs or import frameworks absent from the target's `Framework_Stubs/` directory. ### 2. Apple Developer Documentation Search (MCP) - Use `DocumentationSearch` to verify modern Xcode 27.2 / Swift 6.4 APIs and SwiftUI modifiers before guessing syntax. ### 3. File Tool Preference, Xcode MCP Sequence & The Yellow Triangle Rule - **Prefer Native File Tools for Source Edits:** Use `read_file`, `write_to_file`, and `apply_diff` when reading or modifying existing Swift files—they return complete files in one call without 600-line pagination, `workspaceIdentifier` overhead, or integer parameter mismatches. Reserve Xcode MCP tools for `XcodeOpenWorkspace`, `XcodeSwitchScheme`, `BuildProject`, `GetBuildLog`, `DocumentationSearch`, and project-tree changes (`XcodeWrite`, `XcodeRM`, `XcodeMV`). 1. **Session First (Use Pre-Opened Context When Provided):** If Sidecar already provides an active `workspaceIdentifier` and `activeScheme` in your session context, use that `workspaceIdentifier` directly and do NOT call `XcodeListWorkspaces`, `XcodeOpenWorkspace`, or `XcodeListSchemes` (unless switching to a sibling scheme in a multi-target monorepo task). Otherwise, call `XcodeOpenWorkspace` (or `XcodeListWorkspaces`) first to acquire a `workspaceIdentifier`, and always pass unquoted integers (never strings) for numeric parameters such as `limit` and `offset`. 2. **Switch Scheme (When Needed):** `BuildProject` compiles the *currently active scheme* in Xcode. If the target scheme does not already match the active scheme provided by Sidecar (or if targeting a sibling scheme in a monorepo), call `XcodeSwitchScheme` passing `schemeName: "[TargetName]"` and confirm it succeeded before calling `BuildProject`. 3. **Compile & Inspect (MCP Primary — No Redundant `xcodebuild`):** Run `BuildProject`, then inspect `GetBuildLog` (defaulting to errors, and checking warnings for files you modified). When Xcode MCP `BuildProject` succeeds, verification is complete—do **not** also run terminal `xcodebuild` (especially under Codex's `workspace-write` sandbox, which blocks `xcodebuild` from writing outside the workspace). Use `xcodebuild` only if the Xcode MCP server is offline. 4. **THE YELLOW TRIANGLE RULE:** - Ignore errors or warnings inside third-party packages (`.build/`, `Swifter`, `SwiftSoup`, `Alamofire`) or unmodified files. - Fix all compiler errors and warnings in every file you explicitly modified. - Note any out-of-scope pre-existing warnings in your closing summary. ### 4. On-Demand Skills (`read_skill`) - Before writing complex Swift concurrency, SwiftUI layouts, or Swift Testing suites, call `read_skill(skill_name:)` to load the official dossier (`swift-concurrency`, `swiftui-specialist`, `swiftui-whats-new-27`, `app-resizability`, `modernize-tests`). ### 5. Process Execution Boundaries - Never instantiate raw Foundation `Process()` for shell commands or scripts; use `StreamlineFoundation.runCommand`, `runSwift`, or `runPython`. Raw `Process()` is reserved strictly for continuous bidirectional stdio JSON-RPC transports. ### 6. 2026 Swift 6.4 & SwiftUI Polyfill - **Strict Sendable & Approachable Concurrency:** Never use `@unchecked Sendable` as a shortcut. Annotate cross-isolation closures with `@Sendable` and use typed throws (`throws(DomainError)`) where appropriate. - **Async Cleanup in `defer` (SE-0493):** Place async teardown operations inside `defer { await ... }` rather than duplicating cleanup calls across `return` and `catch` paths. - **Natural Optionals (SE-0521):** Write unparenthesized optional existential/opaque types (`some View?`, `any Tool?`). - **Swift Testing:** Never import `XCTest`. Use `import Testing`, `@Suite`, `@Test`, `#expect`, and `#require`. - **Modern State Architecture:** Never use `ObservableObject`, `@ObservedObject`, `@StateObject`, `@Published`, or `@EnvironmentObject`. Use `@Observable`, `@State`, `@Bindable`, and `@Environment(ModelType.self)`. - **Navigation & Windows:** Never use `NavigationView` or `NavigationLink(destination:isActive:)`. Use `NavigationStack(path:)` or `NavigationSplitView` with `Hashable` routes, and declarative SwiftUI `Window` scenes rather than manual `NSWindow` frame manipulation.