Skip to content

feat(civitai): complete integration overhaul with caching and pagination#2

Merged
fewtarius merged 1 commit intoSyntheticAutonomicMind:mainfrom
fewtarius:main
Dec 25, 2025
Merged

feat(civitai): complete integration overhaul with caching and pagination#2
fewtarius merged 1 commit intoSyntheticAutonomicMind:mainfrom
fewtarius:main

Conversation

@fewtarius
Copy link
Copy Markdown
Member

CivitAI API Fixes

  • Fixed authentication to use query parameters (token=apikey) instead of Authorization header
  • Added warning logging when no API key is configured
  • Updated both searchModels() and getModelDetails() methods

Multi-Page Loading

  • Implemented cursor-based pagination for reliable browsing beyond page 20
  • Checkpoint loading now fetches up to 10 pages (1000 models) automatically
  • LoRA loading updated to use nextCursor for pagination detection
  • Added per-page progress logging for debugging

Smart Caching System

  • Created CivitAIModelCache actor with disk-based storage
  • Separate caches for checkpoints and LoRAs with 24-hour expiry
  • Cache files stored in ~/Library/Caches/sam/civitai-cache/
  • Instant loading when cache is valid, automatic API fallback when expired
  • Refresh button clears cache and forces reload from API

Concurrency Safety

  • Added Sendable conformance to all CivitAI types for Swift 6
  • Actor-isolated cache operations prevent data races
  • Thread-safe data access throughout the integration

UI Improvements

  • Updated Refresh button with clear cache behavior
  • Better loading indicators and status messages
  • Updated What's New for release 20251224.1

Benefits

  • Respectful API usage: 1 request per 24h instead of every preference open
  • Better user experience: instant loads from cache
  • Full catalog access: 1000+ models instead of just 100
  • Reliable pagination: cursor-based system handles large catalogs

Testing
✅ Build: PASS
✅ API Authentication: Working with query parameters ✅ Multi-page loading: Fetches up to 1000 models
✅ Caching: Disk storage with 24h expiry
✅ Concurrency: All types Sendable-compliant

**CivitAI API Fixes**
- Fixed authentication to use query parameters (token=apikey) instead of Authorization header
- Added warning logging when no API key is configured
- Updated both searchModels() and getModelDetails() methods

**Multi-Page Loading**
- Implemented cursor-based pagination for reliable browsing beyond page 20
- Checkpoint loading now fetches up to 10 pages (1000 models) automatically
- LoRA loading updated to use nextCursor for pagination detection
- Added per-page progress logging for debugging

**Smart Caching System**
- Created CivitAIModelCache actor with disk-based storage
- Separate caches for checkpoints and LoRAs with 24-hour expiry
- Cache files stored in ~/Library/Caches/sam/civitai-cache/
- Instant loading when cache is valid, automatic API fallback when expired
- Refresh button clears cache and forces reload from API

**Concurrency Safety**
- Added Sendable conformance to all CivitAI types for Swift 6
- Actor-isolated cache operations prevent data races
- Thread-safe data access throughout the integration

**UI Improvements**
- Updated Refresh button with clear cache behavior
- Better loading indicators and status messages
- Updated What's New for release 20251224.1

**Benefits**
- Respectful API usage: 1 request per 24h instead of every preference open
- Better user experience: instant loads from cache
- Full catalog access: 1000+ models instead of just 100
- Reliable pagination: cursor-based system handles large catalogs

**Testing**
✅ Build: PASS
✅ API Authentication: Working with query parameters
✅ Multi-page loading: Fetches up to 1000 models
✅ Caching: Disk storage with 24h expiry
✅ Concurrency: All types Sendable-compliant
@fewtarius fewtarius merged commit 3022f20 into SyntheticAutonomicMind:main Dec 25, 2025
1 check passed
fewtarius added a commit that referenced this pull request Dec 30, 2025
**Problem:**
1. Sparkle showed updates available when already on current version
2. Release notes displayed as embedded GitHub iframe (poor UX)

**Root Cause:**
- set-build-version.sh generated CFBundleVersion as date only (20251230)
- CFBundleShortVersionString had full version (20251230.1)
- Sparkle compared: 20251230 < 20251230.1 → offered "update"

**Solution:**

**Issue #1 - Version Comparison:**
- Modified set-build-version.sh to sync both version fields
- Removed obsolete date-based build number generation
- Both CFBundleVersion and CFBundleShortVersionString now use YYYYMMDD.RELEASE
- Removed .last_version tracking (no longer needed)

**Issue #2 - Release Notes:**
- Created generate_release_notes.sh to generate styled HTML from whats-new.json
- Apple-style typography, responsive design, inline CSS
- Comprehensive RELEASE_NOTES.md documentation
- Updated VERSIONING.md with new release process
- Added release-notes/ to .gitignore

**Testing:**
✅ Build: PASS (make build-debug successful)
✅ Version sync: Both fields show 20251230.1 in built app
✅ HTML generation: Successfully created release-notes/20251230.1.html (8375 bytes)
✅ User validation: Confirmed version fix working correctly

**Files Changed:**
- scripts/set-build-version.sh - Sync CFBundleVersion = CFBundleShortVersionString
- scripts/generate_release_notes.sh - NEW: HTML release notes generator
- RELEASE_NOTES.md - NEW: Complete documentation for release notes system
- VERSIONING.md - Updated release process with HTML generation step
- .gitignore - Added release-notes/ directory

**Next Steps:**
- Optionally integrate HTML generator into .github/workflows/release.yml
- Consider hosting release notes on GitHub Pages or raw
- Future: Create markdown changelog generator for GitHub releases
fewtarius added a commit that referenced this pull request Jan 4, 2026
…ixes

Complete redesign of agent orchestrator workflow engine to fix critical bugs
in todo tracking, message alternation, and continuation guidance. Evolved from
rigid "force tools" approach to intelligent context-aware orchestration that
follows the orchestrator.txt flow diagram correctly.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
SUMMARY OF FIXES
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

**Fix #1: Todo Workflow Infinite Loops & Update-Before-Create**
Agent tried to update todos before creating list, causing crashes and loops.
Graduated interventions gave contradictory instructions.

Solution:
- Added CRITICAL ERROR TO AVOID section in todo_operations tool
- Visual step-by-step workflow added to tool description
- Rewrote all 3 graduated intervention levels with consistent guidance
- Implemented proper Continue Flag pattern matching orchestrator.txt
- Added shouldContinueAfterChecks flag for correct workflow flow

Result: No more update-before-create crashes, workflow matches design exactly

**Fix #2: Response Loops (Agent Repeating Same Text)**
Agent stuck repeating same response infinitely when todos incomplete.
pendingAutoContinueMessage was set but never injected.

Solution:
- Inject pendingAutoContinueMessage at iteration start
- Call injectAutoContinueIfTodosIncomplete() when no tools + active todos
- Graduated intervention (Level 1 → 2 → 3) now works as designed
- Remove last assistant message to prevent loops

Result: No more infinite response loops, graduated intervention working

**Fix #3: Tool Result Infinite Loop (read_tool_result Stuck)**
Agent stuck seeing SAME tool result chunk repeatedly for 15+ iterations.
TOOL_RESULT_CHUNK messages preserved across iterations incorrectly.

Solution:
- Removed preservation logic for TOOL_RESULT_CHUNK messages
- Chunks now appear once when tool executes
- Agent must call read_tool_result to get more chunks
- Proper pagination flow restored

Result: No more chunk re-injection loops, clean pagination behavior

**Fix #4: Message Alternation Violations**
Multiple consecutive assistant messages broke Claude API compatibility.
Evolved through 3 iterations: Rigid → Binary → Flexible → Context-Aware

Final Solution - Todo-Aware Continuation Guidance:
- 4 guidance variants: (has todos YES/NO) × (tools used YES/NO)
- With todos + tools: "MANDATORY TODO WORKFLOW: mark → work → complete"
- With todos + no tools: "You have incomplete todos - MUST follow workflow"
- Without todos + tools: "Need more data? → tools. Have enough? → respond"
- Without todos + no tools: "Already answered? Use tools for follow-up"

Result: Fixes consecutive messages, allows flexibility, enforces discipline

**Fix #5: Planning Loop False Positives**
Planning loop detector flagged normal workflow (mark todo → work → complete)
as infinite loop because it saw consecutive todo_operations calls.

Solution:
- Added isTodoCompletionCall() helper function
- Marking todos complete now counts as progress
- Only flag as loop if NO work tools AND NO todo completions

Result: Normal workflow allowed, actual loops still detected

**Fix #6: Stale Todo List (Workflow Stopped Early)**
Workflow stopped when todos incomplete because orchestrator saw stale "all
complete" state. currentTodoList only updated after tool execution.

Solution:
- Read fresh todo list from MCP BEFORE every workflow check
- Only if currentTodoList.count > 0 (known active list exists)
- TodoReminderInjector: Clarified workflow guidance wording
- Makes explicit: mark in-progress → DO THE WORK → mark completed

Result: Workflows continue correctly, no premature stops, fresh state accurate

**Fix #7: Duplicate Tool Cards in Streaming Mode**
Tool cards appeared twice in UI - once from streaming, once from main loop.

Solution:
- Skip tool message creation in main loop when streaming active
- Check streamContinuation - if present, streaming created messages
- Non-streaming unchanged

Result: Clean UI, no duplicate cards

**Fix #8: Web Research Error Card Clutter**
Red error cards for expected situations (empty pages, no results).
VectorRAGError helpful messages wrapped with confusing text.

Solution:
- Pass through VectorRAGError messages without wrapping
- Handle partial failures gracefully (some sources succeed = green card)
- Only show error if ALL sources fail

Result: Clean UI, helpful guidance preserved

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
TESTING RESULTS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✅ Simple workflow (3 stories): All tracked, completed, brief summary
✅ Complex workflow (3 research tasks): All tracked, completed, brief summary
✅ Fresh todo reads detect incomplete todos correctly
✅ Agent doesn't repeat work in final summary
✅ No more response loops or chunk re-injection
✅ Planning loop detector allows normal workflow
✅ Context-aware continuation guidance works
✅ Streaming mode: no duplicate tool cards
✅ Web research: clean error handling
✅ Build: PASS (all commits)

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
DOCUMENTATION
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Added comprehensive documentation:
- project-docs/AGENT_ORCHESTRATOR.md (complete architecture)
  * Complete workflow flow diagram (Mermaid)
  * Detailed 8-step decision tree
  * Fresh todo state read documentation
  * Continuation priority table
  * All fixes documented with root causes
  * Known Issues updated with resolutions

- ai-assisted/2026-01-04/workflow-alternation-fix/
  * CONTINUATION_PROMPT.md (session handoff)
  * AGENT_PLAN.md (remaining work breakdown)

- .github/copilot-instructions.md
  * Added isBackground=false requirement (CRITICAL)

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
FILES MODIFIED
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Primary:
- Sources/APIFramework/AgentOrchestrator.swift
  * Fresh todo reads before workflow checks
  * Context-aware continuation guidance system
  * Graduated intervention injection fixed
  * Planning loop detection improvements
  * Duplicate tool card fix for streaming

- Sources/MCPFramework/TodoReminderInjector.swift
  * Todo-aware workflow guidance (4 variants)
  * Clear final message guidance when all tasks complete

- Sources/MCPFramework/Tools/TodoOperationsTool.swift
  * CREATE FIRST requirement documentation
  * Visual step-by-step workflow added

- Sources/ConfigurationSystem/SimpleSystemPromptManager.swift
  * Todo workflow discipline documentation

Supporting:
- Sources/MCPFramework/Tools/WebResearchTool.swift
  * VectorRAGError pass-through without wrapping

- Sources/ConversationEngine/WebResearchService.swift
  * Partial failure handling (some sources succeed)

Documentation:
- project-docs/AGENT_ORCHESTRATOR.md (new + updated)
- ai-assisted/2026-01-04/workflow-alternation-fix/* (new)
- .github/copilot-instructions.md (updated)

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ARCHITECTURAL IMPROVEMENTS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Context-Aware Guidance System:
- 4 guidance variants adapt to workflow state
- Based on: (has incomplete todos?) × (tools called?)
- Each variant provides specific, actionable instructions
- Prevents workflow violations through clear communication

Fresh State Reads:
- Todo list read fresh from MCP before every workflow check
- Prevents stale cache bugs that caused premature stops
- Only reads when active todo list exists (performance optimization)

Graduated Intervention Pressure:
- Level 1: Polite reminder about incomplete todos
- Level 2: Warning about loop behavior
- Level 3: Final warning before failure
- Escalates pressure if agent keeps ignoring todos

Proper Continue Flag Pattern:
- Matches orchestrator.txt flow diagram exactly
- Can be set by: tool execution, incomplete todos, workflow mode
- Priority ordering ensures correct continuation behavior

Unified Todo Workflow Discipline:
- All guidance sources enforce same workflow
- CREATE list first, THEN mark in-progress
- Mark in-progress → DO THE WORK → mark completed
- Never skip status updates

Code Quality:
- Eliminated ~100 lines of redundant code
- Cleaner separation: streaming vs non-streaming paths
- Better error handling and logging

Result: Intelligent orchestrator that adapts guidance based on workflow
context while enforcing todo discipline and preventing loops.
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