Fix yt-long global channel daily max being reset to 5 by reconcile.

Operator-saved defaults and apply-all now persist; API-limited mode only clamps channels above the effective cap.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
eric 2026-09-06 10:59:05 -05:00
parent 89e5ecc98b
commit 2e64ae4094
3 changed files with 42 additions and 30 deletions

View File

@ -197,10 +197,17 @@ def mark_yt_long_api_limited(*, reason: str = "") -> None:
def effective_yt_long_channel_daily_max(cfg: dict[str, Any] | None = None) -> int: def effective_yt_long_channel_daily_max(cfg: dict[str, Any] | None = None) -> int:
"""Per-channel Beijing-day upload target: 5 normally, 4 when API-limited.""" """Per-channel Beijing-day upload target from operator setting.
When shared YouTube API quota is limited, soft-cap at YT_LONG_CHANNEL_LIMITED
(never raise above the saved default).
"""
base = yt_long_channel_daily_max(cfg)
if base <= 0:
return 0
if youtube_api_upload_limited(cfg): if youtube_api_upload_limited(cfg):
return YT_LONG_CHANNEL_LIMITED return min(base, YT_LONG_CHANNEL_LIMITED)
return YT_LONG_CHANNEL_DEFAULT return base
def count_oauth_yt_long_channels(*, enabled_only: bool = True) -> int: def count_oauth_yt_long_channels(*, enabled_only: bool = True) -> int:
@ -215,26 +222,19 @@ def count_oauth_yt_long_channels(*, enabled_only: bool = True) -> int:
def reconcile_yt_long_upload_caps(cfg: dict[str, Any] | None = None) -> dict[str, Any]: def reconcile_yt_long_upload_caps(cfg: dict[str, Any] | None = None) -> dict[str, Any]:
"""Keep global + per-channel caps aligned: 5/ch normal, 4/ch when API limited.""" """Align global upload cap with per-channel defaults.
Respects operator-saved ``yt_long_channel_daily_max`` (and per-channel rows).
Does **not** reset the global default back to 5. When API-limited, only clamps
channel rows that exceed the effective per-channel cap.
"""
from . import db from . import db
settings = cfg if cfg is not None else db.get_settings_map() settings = cfg if cfg is not None else db.get_settings_map()
limited = youtube_api_upload_limited(settings) limited = youtube_api_upload_limited(settings)
# Persist the *normal* target at 5 so UI/settings match policy per = effective_yt_long_channel_daily_max(settings)
try:
stored_ch = int(settings.get("yt_long_channel_daily_max") or 0)
except Exception:
stored_ch = 0
if stored_ch != YT_LONG_CHANNEL_DEFAULT:
try:
db.update_settings_map({"yt_long_channel_daily_max": YT_LONG_CHANNEL_DEFAULT})
settings = db.get_settings_map()
except Exception:
pass
per = YT_LONG_CHANNEL_LIMITED if limited else YT_LONG_CHANNEL_DEFAULT
n = count_oauth_yt_long_channels(enabled_only=True) n = count_oauth_yt_long_channels(enabled_only=True)
global_max = max(per, n * per) if n else YT_LONG_DAILY_DEFAULT global_max = max(per, n * per) if n else max(per, YT_LONG_DAILY_DEFAULT)
try: try:
stored_global = int(settings.get("yt_long_daily_max") or 0) stored_global = int(settings.get("yt_long_daily_max") or 0)
except Exception: except Exception:
@ -247,6 +247,8 @@ def reconcile_yt_long_upload_caps(cfg: dict[str, Any] | None = None) -> dict[str
pass pass
touched = 0 touched = 0
# Only clamp when API-limited — never force every channel back to a hardcoded 5.
if limited and per >= 0:
for ch in list_yt_long_channels(enabled=None): for ch in list_yt_long_channels(enabled=None):
if not str(ch.get("refresh_token") or "").strip(): if not str(ch.get("refresh_token") or "").strip():
continue continue
@ -254,7 +256,7 @@ def reconcile_yt_long_upload_caps(cfg: dict[str, Any] | None = None) -> dict[str
cur = int(ch.get("daily_max") or 0) cur = int(ch.get("daily_max") or 0)
except Exception: except Exception:
cur = 0 cur = 0
if cur != per: if cur > per:
try: try:
update_yt_long_channel(str(ch.get("id") or ""), daily_max=per) update_yt_long_channel(str(ch.get("id") or ""), daily_max=per)
touched += 1 touched += 1

View File

@ -419,8 +419,9 @@ class YtLongPipeline:
"source_daily_max": yt_long_db.yt_long_source_daily_max(cfg), "source_daily_max": yt_long_db.yt_long_source_daily_max(cfg),
"upload_gap": yt_long_db.yt_long_upload_gap(day, cfg), "upload_gap": yt_long_db.yt_long_upload_gap(day, cfg),
"upload_used": yt_long_db.yt_long_upload_used(day), "upload_used": yt_long_db.yt_long_upload_used(day),
"channel_daily_max": effective_ch, "channel_daily_max": yt_long_db.yt_long_channel_daily_max(cfg),
"channel_daily_max_normal": yt_long_db.YT_LONG_CHANNEL_DEFAULT, "channel_daily_max_normal": yt_long_db.YT_LONG_CHANNEL_DEFAULT,
"channel_daily_max_effective": effective_ch,
"api_quota_limited": limited, "api_quota_limited": limited,
"last_error": self.last_error, "last_error": self.last_error,
"stats": yt_long_db.yt_long_stats(day), "stats": yt_long_db.yt_long_stats(day),

View File

@ -29,3 +29,12 @@ def test_tiktok_quota_defaults_clamp(temp_db):
out = db.set_tiktok_quota_defaults(daily_min=40, daily_max=1, apply_all=False) out = db.set_tiktok_quota_defaults(daily_min=40, daily_max=1, apply_all=False)
assert out["daily_min"] == 30 assert out["daily_min"] == 30
assert out["daily_max"] == 30 assert out["daily_max"] == 30
def test_yt_long_reconcile_respects_saved_channel_daily_max(temp_db):
from app import yt_long_db
db.update_settings_map({"yt_long_channel_daily_max": 9, "yt_long_quota_limited_day": ""})
yt_long_db.reconcile_yt_long_upload_caps()
assert yt_long_db.yt_long_channel_daily_max() == 9
assert yt_long_db.effective_yt_long_channel_daily_max() == 9