Skip to content

Release v2.4.0

Choose a tag to compare

@newfpv newfpv released this 19 Oct 22:39

AI Twin Memory Safety & Extended History Update 🧠

This update prevents large media from being encoded entirely in container memory, expands AI Twin history with an explicit token budget, and handles free-tier model quotas more accurately. It includes all changes since v2.3.3.

🛠️ Large Media & Container Stability

  • Streaming Files API Uploads: Files larger than 4 MiB are streamed in bounded chunks and referenced by URI instead of being loaded and base64-encoded as a single payload. Small files retain inline handling.
  • Reusable Uploads: A bounded, per-key content-hash cache reuses the uploaded file across fallback models, prompt changes, and different local paths containing the same media.
  • Bounded Concurrency: Uploads and in-flight media payloads are limited independently of model requests. Cancelled operations close their streaming resources and release their slots.
  • Target Download Reuse: The current photo or video downloaded during context recognition can be reused for the outgoing AI request. Failed and cancelled context downloads clean up partial files.
  • Separate Preparation Deadlines: Large-file upload and processing no longer consume the model-generation timeout. Videos beyond the existing ten-minute context rule are not reattached as raw target videos.

🧩 Extended & Bounded Conversation History

  • Up to 1,000 Messages: AI Twin can consider a larger recent-history window while preserving the existing standalone .ai context behavior.
  • 65,536-Token History Budget: The serialized conversation is checked with the API token counter. Oldest whole messages are dropped when needed; a conservative local bound applies if the counter is unavailable. System instructions and original target media are separate from this history budget.
  • Prune Before Recognition: Cached descriptions and existing text are considered before downloading older attachments. New attachments are processed newest first, with periodic budget checks to avoid recognizing discarded history.
  • Historical Time Anchors: Compact UTC anchors distinguish earlier conversations from the current runtime clock without repeating a full timestamp on every row.
  • Recent Relevant Context First: System rules prioritize the response target, discourage reviving old topics, and prevent earlier plans or status from automatically being treated as current facts.

⏳ Free-Tier Quotas & Model Fallbacks

  • Actual Retry Delays: Quota cooldowns follow API RetryInfo. Daily-quota errors without a retry delay wait for the next Pacific-time reset instead of retrying every minute.
  • Model-Specific Exhaustion: An exhausted model does not temporarily disable other usable models on the same key. Canonical model names from API responses and quota details help identify aliases sharing a quota.
  • Invalid-Key Handling: API_KEY_INVALID responses receive an authentication cooldown instead of being retried for every request.
  • Flash Lite for Recognition: Media descriptions and transcription prefer Flash Lite models already present in the configured fallback list, preserving scarce full-model requests for conversation replies.
  • Usage Visibility: Successful requests log requested and resolved models plus input/output token counts, without logging API keys or conversation content.

✅ Verification & Changed Files

  • 110 Automated Tests Passed: Covers streaming, cancellation, upload reuse, cache bounds, payload concurrency, partial-file cleanup, history pruning, timestamps, and quota cooldown behavior alongside existing AI Twin regressions.

  • 503 MB Stress Test: Streamed a 503,430,097-byte file in a container limited to 512 MiB, with approximately 235 MiB peak process RSS.

  • Live Gemini Validation: Processed the original 503 MB video through Gemini in a separate container limited to 384 MiB, with approximately 231 MiB peak process RSS. Verified upload reuse; no Telegram messages were sent.

  • core/gemini_media.py

    • Added bounded-memory Files API transport, upload caching, request slots, and text-token counting.
  • core/services.py

    • Integrated safe media preparation, budgeted history, cached-context pruning, partial-file cleanup, and quota-aware fallback handling.
  • modules/ai_twin.py

    • Enabled the extended history window, retained target-media reuse, and historical-context rules.
  • tests/test_memory_transport.py

    • Added memory, transport, context, concurrency, and quota regression coverage.

Full Changelog: v2.3.3...v2.4.0