|
| 1 | +# Investigation + Plan: Simulator Selector Normalization, CLI Determinism, and Platform Inference |
| 2 | + |
| 3 | +## Purpose |
| 4 | + |
| 5 | +This document separates three related but distinct problem domains and defines a concrete implementation plan for each. |
| 6 | + |
| 7 | +## Problem Domains |
| 8 | + |
| 9 | +1. Inconsistent handling of `simulatorId` and `simulatorName` across logic paths. |
| 10 | +2. CLI hydrates session defaults, which makes CLI behavior non-deterministic. |
| 11 | +3. Non-iOS simulator targets (watchOS/tvOS/visionOS) can use incorrect platform names and fail builds. |
| 12 | + |
| 13 | +## Scope |
| 14 | + |
| 15 | +1. Keep existing CLI argument UX and command surface. |
| 16 | +2. Keep existing runtime validation behavior (`oneOf`/`allOf`/XOR checks). |
| 17 | +3. Change only session-default hydration behavior, selector normalization behavior, and platform inference behavior. |
| 18 | +4. Stateful CLI behavior (daemon, logs, debug sessions) remains by design and is out of scope. |
| 19 | + |
| 20 | +## Decision Snapshot |
| 21 | + |
| 22 | +1. Platform inference must support non-iOS simulators using simulator runtime metadata, with build-settings only as fallback. |
| 23 | +2. Session defaults must be MCP-only runtime behavior (no CLI/daemon hydration into `sessionStore`). |
| 24 | +3. Store both `simulatorId` and `simulatorName` in session defaults/config and disambiguate at tool boundary via shared helper logic. |
| 25 | +4. Persist only `simulatorPlatform` as platform cache; do not persist `simulatorRuntime` or timestamp fields. |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## Domain 1: `simulatorId` / `simulatorName` Normalization |
| 30 | + |
| 31 | +## Current State (verified) |
| 32 | + |
| 33 | +1. `session_set_defaults` can keep both `simulatorId` and `simulatorName`. |
| 34 | +2. Config normalization drops `simulatorName` when both are present. |
| 35 | +3. Session-aware factory prunes exclusive pairs at merge-time and prefers first key when both come from defaults. |
| 36 | +4. Some tools still enforce schema-level XOR, creating layered/duplicated enforcement. |
| 37 | + |
| 38 | +Net effect: behavior is usually correct, but inconsistent and hard to reason about. |
| 39 | + |
| 40 | +## Decision |
| 41 | + |
| 42 | +Use a single normalized model everywhere: |
| 43 | + |
| 44 | +1. Store both values in session defaults/config. |
| 45 | +2. `simulatorId` is authoritative for tools that require UUID. |
| 46 | +3. `simulatorName` is preserved for portability and tools that can use name. |
| 47 | +4. Explicit user args that provide both remain invalid. |
| 48 | +5. Each tool receives exactly one effective selector value at execution boundary. |
| 49 | + |
| 50 | +## Why this model |
| 51 | + |
| 52 | +1. `simulatorName` survives simulator resets better than UUIDs. |
| 53 | +2. Some operations fundamentally require UUID; others can run with name. |
| 54 | +3. Keeping both in storage avoids repeated lossy conversion and supports both execution modes. |
| 55 | + |
| 56 | +## Required Invariants |
| 57 | + |
| 58 | +1. Storage layer may contain both selector fields. |
| 59 | +2. Explicit invocation args may not contain both selector fields. |
| 60 | +3. Tool execution input must be disambiguated to one selector by a shared helper. |
| 61 | +4. Disambiguation precedence must be deterministic and test-covered. |
| 62 | + |
| 63 | +## Implementation Plan |
| 64 | + |
| 65 | +1. Add `inferSimulatorSelectorForTool(...)` helper (or equivalent) used by simulator tools. |
| 66 | +2. Normalize config/session behavior to stop dropping `simulatorName` when both exist. |
| 67 | +3. Keep factory-level explicit XOR validation. |
| 68 | +4. Keep per-tool requirement checks (`oneOf`/`allOf`) unchanged. |
| 69 | +5. Reduce duplicated tool-local selector branching by centralizing selector choice. |
| 70 | + |
| 71 | +## Validation Plan |
| 72 | + |
| 73 | +1. Unit tests for helper precedence across explicit args, stored defaults, and missing values. |
| 74 | +2. Regression tests for config-load behavior preserving both fields. |
| 75 | +3. Integration tests for tools that require UUID vs tools that accept name. |
| 76 | +4. Real-world MCP run validating both selector paths. |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## Domain 2: CLI Determinism and Session Defaults |
| 81 | + |
| 82 | +## Current State (verified) |
| 83 | + |
| 84 | +1. `session-management` workflow is not exposed in CLI. |
| 85 | +2. CLI runtime still bootstraps config and hydrates `config.sessionDefaults` into `sessionStore`. |
| 86 | +3. Result: CLI can pick up hidden persisted defaults that the user cannot inspect/mutate via CLI commands. |
| 87 | + |
| 88 | +Net effect: CLI behavior can vary based on hidden config state. |
| 89 | + |
| 90 | +## Decision |
| 91 | + |
| 92 | +Make session-default hydration MCP-only. |
| 93 | + |
| 94 | +1. MCP runtime hydrates `config.sessionDefaults` into `sessionStore`. |
| 95 | +2. CLI and daemon runtimes do not hydrate `sessionDefaults` into `sessionStore`. |
| 96 | +3. CLI/daemon still read non-session config (workflow filters, debug flags, timeouts, etc.). |
| 97 | +4. No CLI command/flag redesign is required. |
| 98 | + |
| 99 | +## Required Invariants |
| 100 | + |
| 101 | +1. CLI tool behavior must not depend on persisted `sessionDefaults`. |
| 102 | +2. CLI behavior remains explicit-argument driven. |
| 103 | +3. Existing runtime validation for required parameters remains intact. |
| 104 | +4. `disableSessionDefaults=true` behavior for MCP tools remains consistent with current expectations. |
| 105 | + |
| 106 | +## Implementation Plan |
| 107 | + |
| 108 | +1. Gate session-default hydration by runtime in `bootstrapRuntime`. |
| 109 | +2. Ensure daemon startup path also does not hydrate selector defaults. |
| 110 | +3. Add tests that verify: |
| 111 | + - MCP hydrates session defaults. |
| 112 | + - CLI and daemon do not. |
| 113 | +4. Keep all existing CLI validation paths and error messages unless a bug is found. |
| 114 | + |
| 115 | +## Validation Plan |
| 116 | + |
| 117 | +1. Unit/integration tests for runtime hydration boundaries. |
| 118 | +2. CLI real-world test with persisted `sessionDefaults` present in config: |
| 119 | + - missing required args should still fail. |
| 120 | + - explicit args should succeed. |
| 121 | +3. MCP real-world test confirming session-default convenience still works. |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +## Domain 3: Non-iOS Simulator Platform Inference |
| 126 | + |
| 127 | +## Current State (verified) |
| 128 | + |
| 129 | +1. iOS paths are generally reliable. |
| 130 | +2. Non-iOS simulator targets can infer wrong platform and fail destination matching. |
| 131 | +3. Build settings lookups are slower and should not be the first-line source for simulator platform inference. |
| 132 | + |
| 133 | +## Decision |
| 134 | + |
| 135 | +Platform inference for simulator tools must use simulator metadata first, then fallback. |
| 136 | + |
| 137 | +1. Primary source: simulator runtime metadata (via `simctl` resolution). |
| 138 | +2. Derived output: correct simulator platform string (`iOS/watchOS/tvOS/visionOS Simulator`). |
| 139 | +3. Secondary source: build settings only when simulator metadata cannot resolve. |
| 140 | +4. Final fallback: explicit warning + `iOS Simulator` only when no better signal exists. |
| 141 | + |
| 142 | +Cache policy: |
| 143 | + |
| 144 | +1. Persist `simulatorPlatform` as the cached output. |
| 145 | +2. Recompute `simulatorPlatform` during MCP startup hydration. |
| 146 | +3. Recompute `simulatorPlatform` whenever simulator selector (`simulatorId`/`simulatorName`) changes. |
| 147 | +4. Do not persist `simulatorRuntime` or timestamp fields. |
| 148 | + |
| 149 | +## Required Invariants |
| 150 | + |
| 151 | +1. If runtime indicates non-iOS simulator, platform must not default to iOS. |
| 152 | +2. Platform inference source should be logged for observability. |
| 153 | +3. Selector normalization and platform inference should be reusable utilities, not tool-local variants. |
| 154 | + |
| 155 | +## Implementation Plan |
| 156 | + |
| 157 | +1. Introduce/standardize `inferPlatform(...)` utility contract around selector + runtime metadata. |
| 158 | +2. Ensure simulator-name and simulator-id paths both resolve runtime/platform deterministically. |
| 159 | +3. Add normalized mapping from CoreSimulator runtime to xcodebuild destination platform. |
| 160 | +4. Use build-settings only as fallback path. |
| 161 | + |
| 162 | +## Validation Plan |
| 163 | + |
| 164 | +1. Unit tests for runtime-to-platform mapping. |
| 165 | +2. Integration tests for iOS + non-iOS simulator selectors. |
| 166 | +3. Real-world CLI/MCP checks for watchOS/tvOS/visionOS flows where available. |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +## Cross-Cutting Architecture |
| 171 | + |
| 172 | +## Shared Helpers |
| 173 | + |
| 174 | +1. `inferSimulatorSelectorForTool(...)` |
| 175 | + - Input: explicit params + stored defaults + tool capability (`requiresId` vs `acceptsNameOrId`). |
| 176 | + - Output: exactly one effective selector (or deterministic validation error). |
| 177 | +2. `inferPlatform(...)` |
| 178 | + - Input: resolved selector + scheme/path context. |
| 179 | + - Output: `{ platform, source, runtime? }`. |
| 180 | + |
| 181 | +## Data Model |
| 182 | + |
| 183 | +Keep/add simulator metadata fields in session/config: |
| 184 | + |
| 185 | +1. `simulatorId` |
| 186 | +2. `simulatorName` |
| 187 | +3. `simulatorPlatform` (optional cache) |
| 188 | + |
| 189 | +`simulatorRuntime` can still be used transiently inside resolver/helper logic, but is not persisted. |
| 190 | + |
| 191 | +## Runtime Boundary Rules |
| 192 | + |
| 193 | +1. MCP: may hydrate/use session defaults. |
| 194 | +2. CLI: no session-default hydration; explicit invocation only. |
| 195 | +3. Daemon (CLI backend): same as CLI for hydration semantics. |
| 196 | + |
| 197 | +--- |
| 198 | + |
| 199 | +## Delivery Plan |
| 200 | + |
| 201 | +## Phase 0: Lock Decisions |
| 202 | + |
| 203 | +1. Approve this document’s three-domain decisions. |
| 204 | +2. Confirm no CLI UX changes are in scope. |
| 205 | + |
| 206 | +## Phase 1: Runtime Boundary Fix (Domain 2) |
| 207 | + |
| 208 | +1. Implement MCP-only session-default hydration. |
| 209 | +2. Add runtime boundary tests. |
| 210 | + |
| 211 | +## Phase 2: Selector Normalization (Domain 1) |
| 212 | + |
| 213 | +1. Implement shared selector helper. |
| 214 | +2. Align config/session behavior to preserve both selector fields. |
| 215 | +3. Remove path-specific inconsistencies. |
| 216 | + |
| 217 | +## Phase 3: Platform Inference Hardening (Domain 3) |
| 218 | + |
| 219 | +1. Consolidate non-iOS platform inference on simulator metadata. |
| 220 | +2. Add mapping tests and fallback tests. |
| 221 | + |
| 222 | +## Phase 4: Regression + Real-World Validation |
| 223 | + |
| 224 | +1. Run project test/lint/typecheck/build pipeline. |
| 225 | +2. Run real-world MCP and CLI smoke tests for selector + platform behavior. |
| 226 | +3. Document outcomes in PR notes. |
| 227 | + |
| 228 | +--- |
| 229 | + |
| 230 | +## Risks and Mitigations |
| 231 | + |
| 232 | +1. Risk: duplicate validation behavior drifts across tools. |
| 233 | + - Mitigation: central helper + contract tests. |
| 234 | +2. Risk: config backward-compat surprises. |
| 235 | + - Mitigation: additive fields and migration-safe parsing. |
| 236 | +3. Risk: non-iOS paths regress silently. |
| 237 | + - Mitigation: explicit non-iOS test coverage and runtime-source logging. |
| 238 | + |
| 239 | +## Out of Scope |
| 240 | + |
| 241 | +1. Redesigning CLI command structure or argument names. |
| 242 | +2. Changing general daemon stateful behavior. |
| 243 | +3. Introducing auto-retry heuristics on command failure. |
0 commit comments