Commit Graph
46 Commits
Author SHA1 Message Date
KoalaTrapLord cc481bd71b feat: switch deck generation from Perplexity to local Ollama (gpt-oss:20b) 2026-03-19 10:26:24 -04:00
KoalaTrapLord 2b355ed9ba Fix NameError: _coach_embeddings not defined in V3 status endpoint 2026-03-19 08:05:26 -04:00
KoalaTrapLord 75568bfdb1 fix: expose full V3 init traceback in _deck_gen_v3_error for easier debugging 2026-03-19 06:27:04 -04:00
KoalaTrapLord 87abfd801e Fix V3 500 error: replace json_schema response_format with json_object
- Sonar does not support response_format type json_schema
- Switch to {"type": "json_object"} which Sonar supports
- Keep json_schema/schema_name params for call-site compatibility (ignored)
- Add exc_info=True to V3 generate error logging for full tracebacks
2026-03-19 10:04:36 +00:00
rgriffith 9a46b0f80c fix: remove duplicate Scryfall/helper defs from collection.py that shadowed working imports
collection.py re-defined _ScryfallCache, _scryfall_cache,
_scryfall_rate_limit, _fetch_scryfall_api, _enrich_from_scryfall,
_parse_finish, _parse_text_line, _auto_infer_mapping, _parse_csv_content,
_TYPE_PRIORITY, and _TYPE_TARGETS locally (lines 333-509).

These local definitions shadowed the correct imports from
routes/shared.py -> services/scryfall.py. Critically, the local
_fetch_scryfall_api used Request and urlopen without importing them,
causing a NameError at runtime that was silently swallowed by the
broad except clause — making every card fail Scryfall enrichment
during collection import.

Removed all ~470 lines of duplicate code. The import endpoint now
correctly uses the functions imported from shared.py, which have
proper urllib imports and a working Scryfall cache.
2026-03-19 09:43:26 +00:00
Computer c6329d377a fix: register missing /api/ml and /api/health routes 2026-03-19 06:48:41 +00:00
KoalaTrapLord 3f7f40d626 Make _call_pplx_api async with httpx.AsyncClient
- Replace blocking urlopen with async httpx.AsyncClient
- Add await at both call sites (deck_research, deck_generate_ai)
- Uvicorn event loop no longer stalled during Perplexity API calls
2026-03-19 06:32:18 +00:00
KoalaTrapLord bc023ab6f6 Fix indentation in coach.py _build_deck_report_from_db (2-space -> 4-space) 2026-03-19 06:26:27 +00:00
KoalaTrapLord e3f0787c73 Optimize deckgen: push colour-identity filtering into SQL with json_each
- Replace SELECT * + Python-side color loop in _get_collection_for_colors()
  with projected SELECT + NOT EXISTS json_each() sub-query
- Apply same pattern to _build_collection_summary(), removing card_fits_identity()
- Add partial index idx_ce_quantity on collection_entries(quantity) WHERE qty > 0
- Handle NULL and empty-string color_identity via CASE guard
- Fix colorless commander bug: old code skipped color check entirely for []
- Add scripts/bench_deckgen.py benchmark (3-5x speedup on filtered queries)
- Add tests/test_deckgen_color_filter.py with 7 regression tests
2026-03-19 06:18:32 +00:00
KoalaTrapLord 2936fd37eb Refactor routes/ml.py: replace state dicts with dataclasses + threading locks
- Add @dataclass classes _TrainingState, _PPOState, _TournamentState with snapshot() method
- Wrap all state mutations in threading.Lock() for thread safety
- Replace dict bracket access with attribute access
- Remove unnecessary global declarations from pipeline functions
2026-03-19 06:06:11 +00:00
Computer 2d8a55fdc6 refactor: use _write_dck_file and _build_dck_lines in deckgen.py 2026-03-19 05:11:38 +00:00
KoalaTrapLord c980e1b6d5 refactor: remove inline Pydantic models from deckgen.py, import from models.requests 2026-03-19 00:55:17 -04:00
KoalaTrapLord 7c7be013c2 refactor: remove inline Pydantic models from coach.py, import from models.requests 2026-03-19 00:53:33 -04:00
KoalaTrapLord 6534380f67 fix: replace full-table scan with parameterized WHERE in _build_deck_report_from_db (closes #60) 2026-03-19 00:40:28 -04:00
KoalaTrapLord a95ae45e18 fix: remove API key prefix exposure from /api/pplx/status endpoint 2026-03-19 00:05:33 -04:00
ClaudeandClaude Opus 4.6 71c90a588f fix: resolve /api/lab/result 500 and /api/lab/precons/install-batch 400
- GET /api/lab/result: Starlette's JSONResponse uses allow_nan=False,
  so NaN/Infinity floats from simulation output cause a ValueError crash.
  Add _sanitize_floats() to replace non-finite floats with null before
  returning. Also wrap json.load in try/except for better error reporting.

- POST /api/lab/precons/install-batch: Frontend sends {"names": [...]}
  but backend expected {"fileNames": [...]}. Accept both field names.

- POST /api/lab/precons/install: Same mismatch — frontend sends {"name"}
  but backend expected {"fileName"}. Accept both field names.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-18 06:11:14 +00:00
Commander AI Lab Agent fbc3bdb6b5 fix: add _resolve_precon_dir() auto-detection so CFG.precon_dir is not empty
The precon deck list was missing after the monolith refactor because
PRECON_DIR was hardcoded in services/precon_service.py with no CLI arg,
env var, or auto-resolve fallback — unlike forge_jar, forge_dir, etc.

Changes:
- Add precon_dir field to Config class (models/state.py)
- Add --precon-dir CLI arg and PRECON_DIR env var support
- Add _resolve_precon_dir() with project-root, parent-dir, and
  forge_dir-relative detection; creates dir as fallback for downloads
- Refactor precon_service.py to use _get_precon_dir() helper that reads
  CFG.precon_dir at runtime instead of a hardcoded module-level Path
- Update routes/precon.py to use _get_precon_dir() for install endpoints
- Wire _on_startup to resolve precon_dir for uvicorn-direct launches
- Remove unused PRECON_DIR/PRECON_INDEX imports from routes/lab.py
- Audit confirms all other CFG path fields already have fallbacks
2026-03-18 04:56:03 +00:00
ClaudeandClaude Opus 4.6 70ad16fc79 fix: add missing _get_deepseek_brain and _run_deepseek_batch_thread to forge_runner.py
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-18 04:31:14 +00:00
Commander AI LabandClaude Opus 4.6 3f66a8ed15 feat: add 4-player batch simulator React page with N-player API endpoint
- Add POST /api/sim/run-n-player endpoint accepting 2-4 deck IDs for
  N-player batch simulations using the existing engine.run_n() method
- Add _run_sim_thread_n_player tracking per-seat wins and stats
- Create SimulatorPage.tsx with 4 deck selection slots, config row,
  progress bar, results table, CSS bar chart, and per-game logs
- Add simulator types in frontend/src/types/simulator.ts
- Add /simulator route and nav bar to App.tsx

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-18 03:39:03 +00:00
KoalaTrapLord 554b166787 Phase 2d/2e/2f: Extract forge_runner, precon_service, import_service/helpers into services/ 2026-03-17 15:22:19 -04:00
KoalaTrapLord 874561f423 Phase 2c: Extract card analysis to services/card_analysis.py (#12) 2026-03-17 05:14:35 -04:00
KoalaTrapLord c7309d1ace Phase 2b: Extract deck service to services/deck_service.py (#11) 2026-03-17 05:05:52 -04:00
KoalaTrapLord 5aee93dfae Phase 1c: Extract datPhase 2a: Extract Scryfall client to services/scryfall.py (#10)abase service to services/database.py (#9) 2026-03-17 04:54:36 -04:00
KoalaTrapLord e14a6f9500 Phase 1c: Extract database service to services/database.py (#9) 2026-03-17 04:40:44 -04:00
KoalaTrapLord 7baae153ae Phase 1b: Extract Config, BatchState, AI_PROFILES into models/state.py (#8) 2026-03-17 01:57:22 -04:00
KoalaTrapLord eadf54c670 Phase 1a: Extract Pydantic models into models/requests.py + models/responses.py (#7) 2026-03-17 01:45:57 -04:00
KoalaTrapLord 312d13949d feat: extract ML endpoints into routes/ml.py (#36)
Moved all 15 /api/ml/* endpoints, 4 state dicts, and 3 background
pipeline functions from lab_api.py into routes/ml.py. Wired via
app.include_router(ml_router) in lab_api.py.
2026-03-17 00:41:31 -04:00
KoalaTrapLord 192c307ca7 refactor(#42): deduplicate sim thread functions in deepseek.py 2026-03-17 00:20:51 -04:00
KoalaTrapLord 5cb6e6ce81 refactor(#41): remove duplicate _get_deepseek_brain from deepseek.py, import from shared 2026-03-17 00:08:04 -04:00
KoalaTrapLord 301b9d38c8 fix(#40): use Path().parent.parent for correct src/ resolution in routes/deepseek.py 2026-03-17 00:02:22 -04:00
KoalaTrapLord b57fc9dc2f fix: add missing stdlib imports (threading, os, re, csv, io, datetime) across route modules 2026-03-16 01:55:17 +00:00
KoalaTrapLord e1f2b5d89f fix: add missing os import to collection.py 2026-03-16 01:53:17 +00:00
KoalaTrapLord 9b354e019b fix: add missing Path import to collection.py 2026-03-16 01:42:37 +00:00
KoalaTrapLord ac3b76793b fix: add missing Path, os, threading imports to routes/collection.py 2026-03-15 21:05:35 -04:00
KoalaTrapLord c60719a0a9 refactor: extract deckgen (14) + coach (12) routes
- routes/deckgen.py: classic generator, Perplexity V1, V3 generator + exports
  Helpers: _resolve_commander, _generate_deck, _build_collection_summary,
  _call_pplx_api, _fill_basic_lands, etc.
  Uses _coach._deck_gen_v3 via module reference for V3 state
- routes/coach.py: all coaching endpoints + init_coach_service(),
  _build_deck_report_from_db, globals (_coach_service, _deck_gen_v3, etc.)
- lab_api.py: 3265 -> 1063 lines (16 remaining: ML + SPA catch-all)
- Total: 90 extracted + 16 remaining = 106 endpoints verified
2026-03-15 07:26:09 +00:00
KoalaTrapLord 87a2090c68 refactor: extract scanner (6) + deepseek/sim (9) routes
- routes/scanner.py: scan, scan/add, re-enrich, auto-classify, auto-classify-all, edhrec
- routes/deepseek.py: sim/run, sim/status, sim/result, sim/run-from-deck,
  sim/run-deepseek, deepseek/connect, deepseek/status, deepseek/configure, deepseek/logs
- lab_api.py: 4464 -> 3265 lines (42 remaining endpoints)
- Total: 64 extracted + 42 remaining = 106 endpoints verified
2026-03-15 06:56:02 +00:00
KoalaTrapLord 8a6fbfaa8a fix: 3 post-refactor bugs (COMMANDER_META, PRECON_INDEX, startup)
Task 1: Move BUILTIN_COMMANDERS + load_commander_meta() to shared.py
        Fix import_routes.py to use _shared.COMMANDER_META (module ref)
Task 2: Rewrite precon.py to use _shared.PRECON_INDEX (module ref)
Task 3: Add @app.on_event('startup') safety net for uvicorn --reload
2026-03-14 19:08:21 +00:00
KoalaTrapLord 36077c94ab refactor: extract 49 endpoints into 5 route modules
- Extract collection (10), deckbuilder (18), precon (4), import (5), lab (12) routes
- All share helpers/models/state from routes/shared.py
- Gut lab_api.py from 8872 to 4487 lines (57 remaining endpoints)
- Delete routes/_wire.py and routes/_autoload.py (replaced by include_router)
- Add _scryfall_fuzzy_lookup to shared.py
- 106 total endpoints verified: 49 extracted + 57 remaining
2026-03-14 18:55:32 +00:00
KoalaTrapLord c52eb1c3ef refactor: extract all shared state, helpers, models into routes/shared.py 2026-03-14 03:15:12 -04:00
KoalaTrapLord c14402494b feat(routes): add _autoload.py — FastAPI startup event that wires routers
Adds an @app.on_event('startup') hook that imports routes._wire.
This means lab_api.py needs zero changes — the routers self-register
when the app boots. Drop this file into the repo root and it works.

Alternatively, add to lab_api.py after app creation:
    from routes import _autoload  # noqa
2026-03-14 01:49:52 -04:00
KoalaTrapLord 5ea512bfef feat(routes): add _wire.py — registers shared symbols and includes all routers
Called via `import routes._wire` at the bottom of lab_api.py (after all
symbols are defined). Registers every shared object into routes.shared
and mounts all five APIRouters onto the FastAPI app.

Batch 6/6 — final wiring step.
2026-03-14 01:49:18 -04:00
KoalaTrapLord d92345927f refactor: Batch 5 — routes/deckbuilder.py (deck CRUD, cards, analysis, recs, bulk-add, export) 2026-03-14 01:47:04 -04:00
KoalaTrapLord e1a46202b2 refactor: Batch 4 — routes/collection.py (collection CRUD, import, export, scryfall cache) 2026-03-14 01:46:33 -04:00
KoalaTrapLord 553e07980f refactor: Batch 3 — routes/import_routes.py and routes/precon.py 2026-03-14 01:45:58 -04:00
KoalaTrapLord b0666bb3fd refactor: Batch 2 — routes/lab.py (simulator, status, decks-list, profiles, analytics, deepseek) 2026-03-14 01:45:17 -04:00
KoalaTrapLord 4b1d2f71f5 refactor: Batch 1 — add routes/ package with shared state, config, and models 2026-03-14 01:44:37 -04:00