Skip to content

Comprehensive System Bug Fixes & Windows Cross-Platform Hardening - #57

Open
tienoho wants to merge 2 commits into
crisng95:mainfrom
tienoho:main
Open

tienoho wants to merge 2 commits into
crisng95:mainfrom
tienoho:main

Conversation

@tienoho

@tienoho tienoho commented Sep 22, 2026

Copy link
Copy Markdown

Overview

This PR addresses 14 distinct bugs and edge-case defects across the Flow Kit platform, spanning the FastAPI REST layer, Background Worker queue logic, Chrome MV3 Extension callback mechanism, FFmpeg media processing pipeline, cross-platform Windows compatibility (UTF-8 encoding and path normalization), and restores the complete YouTube integration module (youtube/).

All unit and integration tests pass at 100% (373 passed, 0 failed, 0 errors).


Key Fixes & Improvements

1. REST API & FastAPI Layer

  • Prevent Server Crash on Non-Integer HTTP Status Codes (agent/api/flow.py):

    • Issue: When Google Flow extension returned error payloads like {"status": "failed", "error": "..."}, raising HTTPException(result.get("status", 502), ...) triggered TypeError: '<' not supported between instances of 'str' and 'int' inside Starlette's response handler.
    • Fix: Implemented _safe_status_code() helper to strictly sanitize and validate status codes into the HTTP error range (400 <= status <= 599), defaulting to 502 Bad Gateway for non-integer or 2xx error responses across all 9 generation/upscale endpoints.
  • Multi-Orientation Request Deduplication (agent/api/requests.py):

    • Issue: Both create and create_batch checked existing active requests by scene_id and type only, completely ignoring orientation. Submitting a HORIZONTAL video generation while a VERTICAL request was in progress caused the second request to be erroneously rejected as a duplicate (409 Conflict or silently skipped).
    • Fix: Added and (not orient or r.get("orientation") == orient) to deduplication filters, allowing concurrent or subsequent generation for distinct orientations.
  • Active Project State Corruption Fix on Windows (agent/api/active_project.py):

    • Issue: _read_state() opened active_project.json using system default encoding (cp1252 on Windows). If a project title or description contained Unicode/Vietnamese characters, json.load() threw UnicodeDecodeError. The catch block treated this as a corrupted file and executed _clear_state(), wiping out the active project state.
    • Fix: Enforced encoding="utf-8" in open(_STATE_FILE) and added ensure_ascii=False / utf-8 encoding to _write_state().

2. Background Worker & Queue Logic

  • Fix Missing Character Reference Image URL (agent/worker/processor.py):

    • Issue: When a character image generation request was skipped as already completed, code queried char.get("image_url"). In the SQLite schema, the column is named reference_image_url, resulting in output_url being set to None.
    • Fix: Updated to char.get("reference_image_url") or char.get("image_url").
  • Character Generation Completion Check (agent/worker/processor.py):

    • Issue: _is_already_completed unconditionally returned False for GENERATE_CHARACTER_IMAGE, violating Rule 8 by re-generating character reference images even when the entity already possessed a valid media_id.
    • Fix: Added entity lookup bool(char and char.get("media_id")) for GENERATE_CHARACTER_IMAGE while preserving unconditional execution for REGENERATE_* and EDIT_* requests.

3. Chrome MV3 Extension

  • Attach X-Callback-Secret Header (extension/background.js):
    • Issue: In sendToAgent(msg), HTTP POST callbacks to /api/ext/callback did not transmit the X-Callback-Secret received during the initial WebSocket handshake, leaving HTTP responses unauthenticated.
    • Fix: Injected headers['X-Callback-Secret'] = callbackSecret when callbackSecret is present.

4. Media Services & Windows Cross-Platform Support

  • FFmpeg Concat Demuxer Path Escaping (agent/services/post_process.py):

    • Issue: Concatenation file .concat.txt wrote Windows backslashes (D:\path\clip.mp4), which FFmpeg concat demuxer interprets as escape sequences (causing No such file or directory or corrupt parsing).
    • Fix: Converted all paths to POSIX forward slashes via Path(p).as_posix() and added encoding="utf-8" to file creation.
  • FFmpeg Bundled Binary Fallback (agent/services/post_process.py, agent/services/video_reviewer.py):

    • Issue: On Windows developer machines without system ffmpeg on PATH, subprocess calls failed with FileNotFoundError.
    • Fix: Added _ffmpeg_bin() helper with automatic fallback to the bundled executable provided by imageio_ffmpeg.
  • Windows Symlink Privilege Fallback (agent/services/video_reviewer.py):

    • Issue: os.symlink failed with WinError 1314 (A required privilege is not held by the client) when unprivileged on Windows.
    • Fix: Added graceful fallback to shutil.copy2() when symlinking contact sheet frames fails.
  • Cross-Platform Python Binary in TTS (agent/services/tts.py):

    • Issue: Hardcoded python3.10 executable caused FileNotFoundError on standard Windows installations.
    • Fix: Added automatic fallback to sys.executable on win32.
  • Standard File URI Format (agent/sdk/services/operations.py):

    • Issue: Manual string formatting f"file://{_os.path.abspath(local)}" produced non-standard Windows URIs with backslashes (file://D:\...).
    • Fix: Switched to standard Path(local).resolve().as_uri().

5. Universal UTF-8 Normalization

  • Explicitly specified encoding="utf-8" across all configuration, prompt, and JSON file I/O to eliminate UnicodeDecodeError / UnicodeEncodeError under Windows default ANSI code page (cp1252):
    • agent/config.py (models.json, providers.json, channel_rules.json)
    • agent/api/projects.py (meta.json)
    • agent/api/tts.py (templates_meta.json)
    • agent/api/models.py (models.json)
    • agent/api/providers.py (providers.json)
    • agent/services/cli_providers.py (~/.codex/models_cache.json)
    • agent/services/video_reviewer.py (Codex CLI output)
    • tools/review_server.py (feedback.json)
    • tests/unit/test_setup.py (fixtures with em-dashes —)
    • tests/unit/test_cli_providers.py (cross-platform path assertions)

6. Restored YouTube Integration Module

  • .gitignore Refinement: Corrected .gitignore line 9 from youtube (which blocked the entire module) to youtube/channels/ (safeguarding OAuth secrets, credentials, and tokens while tracking source code).
  • youtube/auth.py: Created OAuth2 credential manager using InstalledAppFlow and automatic token refresh via google-auth-oauthlib.
  • youtube/upload.py: Created full YouTube upload pipeline:
    • detect_video_type(): Detects Shorts (<61s, vertical 9:16) vs Long-form.
    • load_channel_rules(): Parses scheduling and quota constraints with default fallbacks.
    • validate_upload(): Enforces daily quotas, min gap hours, and avoids posting during dead hours.
    • auto_schedule(): Generates optimized release slots.
    • upload_video(): Resumable chunked upload (10MB) via YouTube Data API v3 with SEO hashtags.
  • tests/unit/test_youtube.py: Added comprehensive test suite covering rules parsing, validation, auto-scheduling, and video type detection.

Verification & Test Results

============================= test session starts =============================
platform win32 -- Python 3.11.9, pytest-9.0.2, pluggy-1.6.0
rootdir: D:\Project\flowkit
configfile: pytest.ini
testpaths: tests
plugins: anyio-4.14.2, asyncio-1.4.0, mock-3.15.1
collected 373 items

tests\unit\test_cli_providers.py ....................................... [ 10%]
......................................                                   [ 20%]
tests\unit\test_flow_batch.py .......................................... [ 31%]
...........................                                              [ 39%]
tests\unit\test_flow_batch_golden.py ....                                [ 40%]
tests\unit\test_flow_client_batch.py ................................... [ 49%]
.........                                                                [ 52%]
tests\unit\test_models.py .........................                      [ 58%]
tests\unit\test_omni_flash.py ............................               [ 66%]
tests\unit\test_operations.py ...................                        [ 71%]
tests\unit\test_parsing.py ...............................               [ 79%]
tests\unit\test_processor.py .............                               [ 83%]
tests\unit\test_result_handler.py ..............                         [ 86%]
tests\unit\test_setup.py .............                                   [ 90%]
tests\unit\test_video_reviewer.py .............................          [ 98%]
tests\unit\test_youtube.py .......                                       [100%]

============================= 373 passed in 4.60s =============================
  • Previous State: 337 passed, 6 failed, 22 errors.
  • Current State: 373 passed, 0 failed, 0 errors (100% pass rate).

Modified Files

  • .gitignore
  • agent/api/active_project.py
  • agent/api/flow.py
  • agent/api/models.py
  • agent/api/projects.py
  • agent/api/providers.py
  • agent/api/requests.py
  • agent/api/tts.py
  • agent/config.py
  • agent/sdk/services/operations.py
  • agent/services/cli_providers.py
  • agent/services/post_process.py
  • agent/services/tts.py
  • agent/services/video_reviewer.py
  • agent/worker/processor.py
  • extension/background.js
  • tests/unit/test_cli_providers.py
  • tests/unit/test_processor.py
  • tests/unit/test_setup.py
  • tests/unit/test_video_reviewer.py
  • tests/unit/test_youtube.py (New)
  • tools/review_server.py
  • youtube/__init__.py (New)
  • youtube/auth.py (New)
  • youtube/upload.py (New)

@crisng95

Copy link
Copy Markdown
Owner

@tienoho jobs run 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.

2 participants