Skip to content

[Beta]Refactor: Decompose Models.swift + WebUI Local Fallback fix#6

Merged
johnwatso merged 11 commits intomainfrom
Code-Refactor-Phase2
Mar 14, 2026
Merged

[Beta]Refactor: Decompose Models.swift + WebUI Local Fallback fix#6
johnwatso merged 11 commits intomainfrom
Code-Refactor-Phase2

Conversation

@johnwatso
Copy link
Copy Markdown
Owner

Refactor: Decompose Models.swift (4,263 → 16 lines) + WebUI fixes

MODELS.SWIFT DECOMPOSITION (Phase 3):
Extract 99.6% of Models.swift into domain-specific files:

  • Models/AIModels.swift (~380 lines): ConversationStore, WikiContextCache, AI types
  • Models/BotSettings.swift (~987 lines): GuildSettings, AdminWebUISettings, OAuth
  • Models/BotStateModels.swift (~184 lines): BotStatus, BugEntry, voice/guild types
  • Models/ClusterModels.swift (~403 lines): ClusterMode, Mesh protocols, SwiftMesh
  • Models/DiscordCache.swift (~251 lines): DiscordCache actor, snapshot types
  • Models/EventBus.swift (~120 lines): Event system, voice events
  • Models/GatewayModels.swift (~118 lines): GatewayPayload, DiscordJSON, VoiceRuleEvent
  • Models/KeychainHelper.swift (~72 lines): Keychain utility
  • Models/RuleEngineModels.swift (~1,603 lines): Rule, Trigger, Condition, Action, Plugin

Models.swift: 4,263 → 16 lines (99.6% reduction)

WEBUI LOCAL FALLBACK FIX:

  • Add devFeaturesEnabled to AdminWebServer.Configuration
  • Update handleAuthOptions() to return localEnabled && devFeaturesEnabled
  • Local Fallback now only shows when Developer Mode is enabled
  • Update index.html auth prompt text

BUILD FIXES:

  • Add Models/ files to Xcode project.pbxproj (fixes "Cannot find type" errors)
  • Replace async let with TaskGroup in DiscordService (fixes unused value warnings)

PREVIOUS SESSION WORK (Phase 2):

  • AI engine racing (TaskGroup parallel execution, 30s+ → >5s latency)
  • Bounded avatar caches (1000-entry FIFO eviction)
  • URLSession consolidation (default + ephemeral for token security)
  • Parallel gateway seed operations
  • RuleExecutionService actor conversion (data race fix)
  • RuleEngine @mainactor removal
  • Universal ActionDispatcher gate (cluster safety)
  • Long-running task safety (weak-self)
  • Structured logging (OSLog in Security/ files)
  • Instant failover fix (setOutputAllowed flip, no reconnect)

Files: 9 new Models/*.swift files, AdminWebServer.swift, AppModel.swift,
DiscordService.swift, Resources/admin/index.html, project.pbxproj

Performance: AI engine racing + bounded avatar caches

AI Engine Racing (DiscordAIService.swift):
- Replace sequential AI fallback with TaskGroup parallel execution
- Race Apple Intelligence, OpenAI, and Ollama simultaneously
- Take first successful response, cancel remaining tasks
- Expected latency improvement: 30s+ → <5s worst case
- Add comment documenting preference order is starting order only

Bounded Avatar Caches (AppModel.swift):
- Add maxAvatarCacheCount limit (1000 entries per cache)
- Add cacheUserAvatar() and cacheGuildAvatar() helpers with FIFO eviction
- Prevent unbounded memory growth during extended operation
- Update syncVoicePresenceFromGuildSnapshot() and cacheGuildMembers()

QA/Test Updates:
- stripLeadingSpeakerPrefix made nonisolated for TaskGroup closure
- DiscordAIServiceTests updated for racing semantics
- Test asserts set membership not order (deterministic winner)

Files Changed:
- Services/DiscordAIService.swift (AI racing implementation)
- AppModel.swift (cache eviction + helpers)
- Services/DiscordAIServiceTests.swift (test updates)

Build: ✅ GREEN
Tests: ✅ ALL PASSED
@johnwatso johnwatso merged commit 94ed13a into main Mar 14, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant