diff --git a/ai_blueprint.md b/ai_blueprint.md index 308157d..b2ff621 100644 --- a/ai_blueprint.md +++ b/ai_blueprint.md @@ -1,4 +1,4 @@ -# AI Context Optimization Blueprint (v2.9) +# AI Context Optimization Blueprint (v2.10) This blueprint outlines architectural improvements for how AI context is managed during the writing process. The goal is to provide the AI (Claude/Gemini) with **better, highly-targeted context upfront**, which will dramatically improve first-draft quality and reduce the reliance on expensive, time-consuming quality checks and rewrites (currently up to 5 attempts). @@ -171,3 +171,4 @@ The UI gets stuck indefinitely because the background Huey worker thread hangs b 11. ✅ **(v2.7)** Series Continuity Fix: `series_metadata` (is_series, series_title, book_number, total_books) injected as `SERIES_CONTEXT` into `story/planner.py` (`enrich`, `plan_structure`), `story/writer.py` (`write_chapter`), and `story/editor.py` (`evaluate_chapter_quality`) prompts with position-aware guidance per book number. *(Implemented v2.7)* 12. ✅ **(v2.8)** Infrastructure & UI Bug Fixes: API timeouts (180s generation, 30s list_models) in `ai/models.py` + `ai/setup.py`; Huey consumer moved to module level with reloader guard in `web/app.py`; Jinja2 `UndefinedError` fix for `tropes`/`formatting_rules` in `project_setup.html`; `project_setup_wizard` now renders form instead of silent redirect when models fail; `create_project_final` `enrich()` call fixed to use correct per-book blueprint structure. *(Implemented v2.8)* 13. ✅ **(v2.9)** Background Task Hang Fixes: OAuth headless guard in `ai/setup.py` (skips `run_local_server` in non-main threads, logs warning, falls back to ADC); SQLite `timeout=30, check_same_thread=False` on all connections in `web/tasks.py`; initial log file touched immediately in `generate_book_task` so UI polling never sees an empty/missing file. *(Implemented v2.9)* +14. ✅ **(v2.10)** Huey Consumer Startup Fix: `Consumer.__init__()` in Huey 2.6.0 does NOT accept a `loglevel` keyword argument — the previous call `Consumer(huey, workers=1, worker_type='thread', loglevel=20)` raised `TypeError` on every app start, silently killing the consumer. All tasks stayed `queued` forever, causing the "Preparing environment / Waiting for logs" hang. Fixed by removing `loglevel=20`; Huey logging now configured via `logging.basicConfig`. Consumer startup errors now written to `data/consumer_error.log` for diagnosis. Also removed emoji characters from `print()` calls in `core/config.py` that caused `UnicodeEncodeError` on Windows `cp1252` terminals. Updated `VERSION` to `2.9` in `config.py`. *(Implemented v2.10)* diff --git a/core/config.py b/core/config.py index f61ffe7..40fc00e 100644 --- a/core/config.py +++ b/core/config.py @@ -27,9 +27,9 @@ ADMIN_USER = get_clean_env("ADMIN_USERNAME") ADMIN_PASSWORD = get_clean_env("ADMIN_PASSWORD") if FLASK_SECRET == "dev-secret-key-change-this": - print("⚠️ WARNING: Using default FLASK_SECRET_KEY. This is insecure for production.") + print("WARNING: Using default FLASK_SECRET_KEY. This is insecure for production.") -if not API_KEY: raise ValueError("❌ CRITICAL ERROR: GEMINI_API_KEY not found.") +if not API_KEY: raise ValueError("CRITICAL ERROR: GEMINI_API_KEY not found in environment or .env file.") # --- DATA DIRECTORIES --- DATA_DIR = os.path.join(BASE_DIR, "data") @@ -51,7 +51,7 @@ if GOOGLE_CREDS: if os.path.exists(GOOGLE_CREDS): os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = GOOGLE_CREDS else: - print(f"⚠️ Warning: GOOGLE_APPLICATION_CREDENTIALS file not found at: {GOOGLE_CREDS}") + print(f"Warning: GOOGLE_APPLICATION_CREDENTIALS file not found at: {GOOGLE_CREDS}") # --- DEFINITIONS --- LENGTH_DEFINITIONS = { @@ -65,4 +65,4 @@ LENGTH_DEFINITIONS = { } # --- SYSTEM --- -VERSION = "1.5.0" +VERSION = "2.9" diff --git a/web/app.py b/web/app.py index f362e56..2634b39 100644 --- a/web/app.py +++ b/web/app.py @@ -97,13 +97,26 @@ with app.app_context(): import threading as _threading def _start_huey_consumer(): + import logging as _logging + _logging.basicConfig(level=_logging.WARNING) try: from huey.consumer import Consumer - consumer = Consumer(huey, workers=1, worker_type='thread', loglevel=20) - print("✅ System: Huey task consumer started.") + consumer = Consumer(huey, workers=1, worker_type='thread') + print("System: Huey task consumer started.") consumer.run() except Exception as e: - print(f"⚠️ System: Huey consumer failed to start: {e}") + msg = f"System: Huey consumer failed to start: {e}" + print(msg) + # Write the error to a persistent log so it is visible even when stdout is lost + try: + import os as _os + from core import config as _cfg + _err_path = _os.path.join(_cfg.DATA_DIR, "consumer_error.log") + with open(_err_path, 'a', encoding='utf-8') as _f: + import datetime as _dt + _f.write(f"[{_dt.datetime.now()}] {msg}\n") + except Exception: + pass _is_reloader_child = os.environ.get('WERKZEUG_RUN_MAIN') == 'true' _is_testing = os.environ.get('FLASK_TESTING') == '1'