~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(news): validate saved subscription intervals before dispatch (#6919)
LearningCircuit3e649213ca96
diff --git a/changelog.d/+validate-saved-subscription-intervals.bugfix.md b/changelog.d/+validate-saved-subscription-intervals.bugfix.mdnew file mode 100644index 0000000000..b9713ce00f--- /dev/null+++ b/changelog.d/+validate-saved-subscription-intervals.bugfix.md@@ -0,0 +1 @@+Reject invalid refresh intervals in the news service API and skip saved subscriptions whose interval cannot be scheduled, including when a direct start-research request links one. Subscriptions saved by earlier releases with an interval above the new one-week cap, up to 365 days, keep running; saving an edit to one requires an interval of at most one week. Run Now on the subscriptions dashboard now uses the guarded subscription endpoint, so it runs with the subscription's saved provider, model and strategy instead of the user's current defaults (a provider or model that was never saved still comes from the user's settings, and the strategy is news aggregation when none is saved), and saving a subscription from the compact editor reschedules it.diff --git a/docs/news-subscriptions.md b/docs/news-subscriptions.mdindex 236be7340f..beac8ed5b9 100644--- a/docs/news-subscriptions.md+++ b/docs/news-subscriptions.md@@ -118,6 +118,23 @@ Organize subscriptions into folders:## Update Frequency+An interval you save, on a new subscription or an edit, must be a whole+number of minutes from 1 to 10080 (one week).++Releases through 1.10.7 did not cap the interval and documented anything from+1 hour to 30 days, so a subscription they saved can be longer than a week.+Those subscriptions keep running with the interval they have, up to 525600+minutes (365 days). The one-week limit applies when you edit one: saving a+subscription sends its interval along with the other fields, so lower the+interval to 10080 or less in the same save. Pausing and resuming from the+subscriptions page does not send the interval and leaves it unchanged.++A saved interval that is not a whole number of minutes from 1 to 525600, such+as 0, a negative number, a fraction or an empty value left by an import, is+skipped by scheduled and overdue checks, and **Run Now** asks you to correct+it. Edit the subscription and save a valid frequency to make it runnable+again. The saved value is not silently changed.+### Preset Intervals| Frequency | Interval |@@ -131,7 +148,8 @@ Organize subscriptions into folders:### Custom Intervals-Set any interval between 1 hour and 30 days (in minutes).+Set any interval from 1 to 10080 whole minutes (one week) when you create+or edit a subscription.### Jitter@@ -225,6 +243,7 @@ See [NOTIFICATIONS.md](NOTIFICATIONS.md) for supported services.| `/news/api/subscriptions/<id>` | GET | Get subscription details || `/news/api/subscriptions/<id>` | PUT | Update subscription || `/news/api/subscriptions/<id>` | DELETE | Delete subscription |+| `/news/api/subscriptions/<id>/run` | POST | Run with saved settings; reject an invalid saved interval || `/news/api/subscriptions/<id>/history` | GET | Get subscription history |### Folder Endpointsdiff --git a/src/local_deep_research/news/api.py b/src/local_deep_research/news/api.pyindex 13083256ca..c90780f7be 100644--- a/src/local_deep_research/news/api.py+++ b/src/local_deep_research/news/api.py@@ -10,6 +10,7 @@ from sqlalchemy.orm import Sessionimport jsonfrom ..constants import ResearchStatus+from .constants import validate_refresh_intervalfrom ..llm.providers.base import normalize_providerfrom ..utilities.sql_utils import escape_likefrom ..security.log_sanitizer import redact_and_bound_for_log@@ -898,6 +899,9 @@ def update_subscription(Returns:Dictionary with updated subscription data"""+ if "refresh_interval_minutes" in data:+ validate_refresh_interval(data["refresh_interval_minutes"])+try:from ..database.session_context import get_user_db_sessionfrom ..database.models.news import NewsSubscription@@ -1170,6 +1174,8 @@ def create_subscription("Settings manager not available, using default refresh_minutes")refresh_minutes = 240 # Default to 4 hours+ validate_refresh_interval(refresh_minutes)+# Create new subscriptionsubscription = NewsSubscription(id=str(uuid.uuid4()),@@ -1209,9 +1215,8 @@ def create_subscription("refresh_minutes": refresh_minutes,}- except SubscriptionCreationException:- # Already a structured creation error (e.g. the N14 policy- # rejection) — propagate as-is instead of re-wrapping.+ except NewsAPIException:+ # Preserve structured policy and input-validation errors.raiseexcept Exception:logger.exception("Error creating subscription")diff --git a/src/local_deep_research/news/constants.py b/src/local_deep_research/news/constants.pyindex 16d5d8aca8..217e5cc001 100644--- a/src/local_deep_research/news/constants.py+++ b/src/local_deep_research/news/constants.py@@ -6,7 +6,83 @@ from ..database.models.news import SubscriptionFolder# update routes (``web/routers/news_flask_api.py`` imports these back) so# their limits cannot drift apart.NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES = 1+# Cap on a value being written now: create, and both update routes.NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES = 10_080+# Ceiling on a value that is already saved. v1.10.7 stored whatever interval+# it was sent -- its form set only min="1" and its docs allowed up to 30 days+# -- so rows above the write cap exist and must keep running after an+# upgrade. One year is far past that documented range and keeps every+# ``timedelta``/``datetime``/APScheduler date computed from the value+# comfortably in range; anything beyond it is not a usable schedule.+NEWS_SUBSCRIPTION_MAX_SAVED_REFRESH_MINUTES = 525_600++# Shown when a saved row cannot be run until the user fixes its interval.+# Shared by the manual run, the overdue sweep and the research-start guard.+SUBSCRIPTION_INTERVAL_REPAIR_MESSAGE = (+ "Edit this subscription's refresh interval before running it"+)+++def _is_whole_minutes_up_to(value, maximum: int) -> bool:+ # ``type(...) is int`` rather than ``isinstance`` so bool, float (NaN and+ # infinity included), str and None are all rejected.+ return (+ type(value) is int+ and NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES <= value <= maximum+ )+++def is_valid_refresh_interval(value) -> bool:+ """Whether ``value`` may be written as a refresh interval now.++ Applies to a new subscription and to an interval sent with an edit. A+ row saved before the cap existed is judged by+ :func:`is_runnable_refresh_interval` instead.+ """+ return _is_whole_minutes_up_to(value, NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES)+++def is_runnable_refresh_interval(value) -> bool:+ """Whether an already-saved interval is safe to schedule and run.++ Accepts every whole number of minutes the released form could store, up+ to :data:`NEWS_SUBSCRIPTION_MAX_SAVED_REFRESH_MINUTES`. Rejects what+ cannot be scheduled: non-integers (NaN, fractions, text, null), zero and+ negatives (APScheduler turns a zero interval into a one-second trigger)+ and anything beyond the ceiling.+ """+ return _is_whole_minutes_up_to(+ value, NEWS_SUBSCRIPTION_MAX_SAVED_REFRESH_MINUTES+ )+++def validate_refresh_interval(value) -> None:+ """Reject an interval that may not be written (create or edit)."""+ if not is_valid_refresh_interval(value):+ from .exceptions import NewsAPIException++ raise NewsAPIException(+ "Refresh interval must be an integer between "+ f"{NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES} and "+ f"{NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES} minutes",+ status_code=400,+ error_code="INVALID_REFRESH_INTERVAL",+ )+++def validate_runnable_refresh_interval(value) -> None:+ """Reject a saved interval before a run's schedule is advanced from it."""+ if not is_runnable_refresh_interval(value):+ from .exceptions import NewsAPIException++ raise NewsAPIException(+ "Saved refresh interval must be an integer between "+ f"{NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES} and "+ f"{NEWS_SUBSCRIPTION_MAX_SAVED_REFRESH_MINUTES} minutes",+ status_code=400,+ error_code="INVALID_REFRESH_INTERVAL",+ )+# ``NewsSubscription.name`` is ``Column(String(255))`` (database/models/# news.py:63) -- this mirrors that column's length, it is not an@@ -98,11 +174,8 @@ def compact_subscription_update_error(data):):return "status must be 'active' or 'paused'"- if "refresh_interval_minutes" in data and (- type(data["refresh_interval_minutes"]) is not int- or not NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES- <= data["refresh_interval_minutes"]- <= NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES+ if "refresh_interval_minutes" in data and not is_valid_refresh_interval(+ data["refresh_interval_minutes"]):return ("refresh_interval_minutes must be an integer between "diff --git a/src/local_deep_research/news/subscription_runner.py b/src/local_deep_research/news/subscription_runner.pyindex 5bcaf0ab29..fa58dbd9c0 100644--- a/src/local_deep_research/news/subscription_runner.py+++ b/src/local_deep_research/news/subscription_runner.py@@ -91,7 +91,16 @@ def advance_refresh_schedule(``refresh_interval_minutes`` later. Shared by the manual run, the overduesweep, and the scheduler so the arithmetic -- and remembering to advancethe schedule at all -- lives in one place.++ Raises ``NewsAPIException`` (``INVALID_REFRESH_INTERVAL``) before+ touching either timestamp when the saved interval is not runnable (see+ ``is_runnable_refresh_interval``). That is the saved-row bound, not the+ narrower cap on newly written values, so a long interval saved by an+ older version still advances."""+ from .constants import validate_runnable_refresh_interval++ validate_runnable_refresh_interval(subscription.refresh_interval_minutes)if now is None:now = datetime.now(timezone.utc)subscription.last_refresh = nowdiff --git a/src/local_deep_research/scheduler/background.py b/src/local_deep_research/scheduler/background.pyindex 7a40ca10eb..4e23662eac 100644--- a/src/local_deep_research/scheduler/background.py+++ b/src/local_deep_research/scheduler/background.py@@ -45,6 +45,29 @@ _LIBRARY_SWEEP_BATCH = 50_document_pass_locks: Dict[str, threading.Lock] = {}_document_pass_locks_guard = threading.Lock()+# Subscription rows already reported for an unusable saved refresh interval.+# Such a row stays unusable until the user edits it, but every reschedule+# pass and overdue sweep meets it again, so it is reported once per process.+_reported_invalid_intervals: set[tuple[str, str]] = set()+_reported_invalid_intervals_guard = threading.Lock()+++def _log_invalid_refresh_interval(+ username: str, subscription_id: Any, message: str+) -> None:+ """Warn the first time a row's interval is found unusable, then only debug.++ ``message`` takes the subscription id as its one ``{}`` placeholder.+ """+ key = (username, str(subscription_id))+ with _reported_invalid_intervals_guard:+ first_report = key not in _reported_invalid_intervals+ _reported_invalid_intervals.add(key)+ if first_report:+ logger.warning(message, subscription_id)+ else:+ logger.debug(message, subscription_id)+def _one_document_pass_per_user(method: Callable) -> Callable:"""Skip a document pass while another one for the same user is running.@@ -616,6 +639,10 @@ class BackgroundJobScheduler:from ..database.session_context import get_user_db_sessionfrom ..database.models.news import NewsSubscription+ # news.__init__ re-exports this scheduler, so defer news imports+ # until the scheduler module has finished initializing.+ from ..news.constants import is_runnable_refresh_interval+with get_user_db_session(username, password) as db:subscriptions = (db.query(NewsSubscription)@@ -646,6 +673,15 @@ class BackgroundJobScheduler:# Schedule each subscription with jitterfor sub in subscriptions:+ refresh_minutes = sub.refresh_interval_minutes+ if not is_runnable_refresh_interval(refresh_minutes):+ _log_invalid_refresh_interval(+ username,+ sub.id,+ "Subscription {} has an invalid refresh interval; "+ "edit its interval before scheduling it",+ )+ continuejob_id = f"{username}_{sub.id}"# Calculate jitter@@ -655,8 +691,6 @@ class BackgroundJobScheduler:jitter = random.randint(0, max_jitter)# Determine trigger based on frequency- refresh_minutes = sub.refresh_interval_minutes-if refresh_minutes <= 60: # 60 minutes or less# For hourly or more frequent, use interval triggertrigger = "interval"@@ -1973,6 +2007,7 @@ class BackgroundJobScheduler:# Get user's overdue subscriptionsfrom ..database.session_context import get_user_db_sessionfrom ..database.models.news import NewsSubscription+ from ..news.constants import is_runnable_refresh_intervalfrom datetime import timezonewith get_user_db_session(username, password) as db:@@ -1989,6 +2024,16 @@ class BackgroundJobScheduler:)for sub in overdue_subs:+ if not is_runnable_refresh_interval(+ sub.refresh_interval_minutes+ ):+ _log_invalid_refresh_interval(+ username,+ sub.id,+ "Skipping overdue subscription {} with an "+ "invalid refresh interval",+ )+ continue# Run immediately with small random delay# Security: random delay to stagger overdue jobs, not security-sensitive# nosemgrep: semgrep.rules.weak-random-generation, reason: Non-security scheduling or rate-limit jitter@@ -2056,6 +2101,7 @@ class BackgroundJobScheduler:SubscriptionStatus,)from ..news.subscription_runner import advance_refresh_schedule+ from ..news.constants import is_runnable_refresh_intervalwith get_user_db_session(username, password) as db:sub = db.get(NewsSubscription, subscription_id)@@ -2065,6 +2111,25 @@ class BackgroundJobScheduler:)return+ if not is_runnable_refresh_interval(+ sub.refresh_interval_minutes+ ):+ # An old interval job can outlive the stored value it+ # was created from. Remove it before it can dispatch+ # research or repeatedly retry an invalid schedule.+ job_id = "{}_{}".format(username, subscription_id)+ try:+ self.scheduler.remove_job(job_id)+ except JobLookupError:+ pass+ session_info["scheduled_jobs"].discard(job_id)+ logger.warning(+ "Stopped subscription {} with an invalid refresh "+ "interval; edit its interval before scheduling it",+ subscription_id,+ )+ return+# Prepare query with date replacement using user's timezonequery = sub.query_or_topicif "YYYY-MM-DD" in query:diff --git a/src/local_deep_research/web/routers/news_flask_api.py b/src/local_deep_research/web/routers/news_flask_api.pyindex 01510cca47..d979b45c59 100644--- a/src/local_deep_research/web/routers/news_flask_api.py+++ b/src/local_deep_research/web/routers/news_flask_api.py@@ -27,6 +27,8 @@ from ...news import apifrom ...news.constants import (NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES,NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES,+ SUBSCRIPTION_INTERVAL_REPAIR_MESSAGE,+ is_runnable_refresh_interval,compact_subscription_update_error,folder_update_error,normalize_compact_subscription_update,@@ -553,11 +555,17 @@ async def create_subscription(status_code=400,)except NewsAPIException:- # api.create_subscription() always raises SubscriptionCreationException- # on failure (e.g. the N14 egress-policy rejection, or a wrapped DB- # error) — let the NewsAPIException handler in fastapi_app.py render- # exc.to_dict() (error_code, details) instead of downgrading it to a- # generic {"error": ...} below.+ # api.create_subscription() raises one of two NewsAPIException+ # shapes on failure: a SubscriptionCreationException (e.g. the N14+ # egress-policy rejection, or a wrapped DB error), or a plain+ # NewsAPIException with error_code INVALID_REFRESH_INTERVAL and+ # status 400 when the interval it resolved is outside the write cap.+ # Through this route only the saved news.subscription.refresh_minutes+ # default can cause the latter, because an explicit refresh_minutes+ # was already range-checked by _validate_subscription_payload above.+ # Let the NewsAPIException handler in fastapi_app.py render+ # exc.to_dict() (error_code, details) instead of downgrading it to+ # a generic {"error": ...} below.raiseexcept Exception as e:return JSONResponse(@@ -997,6 +1005,11 @@ def run_subscription_now({"error": "Subscription not found"}, status_code=404)+ if not is_runnable_refresh_interval(sub.refresh_interval_minutes):+ return _subscription_validation_error(+ SUBSCRIPTION_INTERVAL_REPAIR_MESSAGE+ )+subscription_pk = sub.id# Snapshot next_refresh for the post-POST compare-and-set (below):# a fast-failing run's failure handler may reset next_refresh on the@@ -1599,6 +1612,17 @@ def check_overdue_subscriptions(# sweep. Snapshotting here keeps the error branches session-free.sub_id = str(sub.id)sub_label = sub.name or sub.query_or_topic[:50]+ if not is_runnable_refresh_interval(+ sub.refresh_interval_minutes+ ):+ results.append(+ {+ "id": sub_id,+ "name": sub_label,+ "error": SUBSCRIPTION_INTERVAL_REPAIR_MESSAGE,+ }+ )+ continuetry:# Run the subscription using the same pattern as run_subscription_nowlogger.info(@@ -2009,7 +2033,14 @@ def _update_subscription_folder_sync(data, subscription_id, username):logger.info(f"Updated subscription result: refresh_interval_minutes={result.get('refresh_interval_minutes')}, next_refresh={result.get('next_refresh')}")- return result++ # Reschedule like the full update path (api.update_subscription)+ # does; without this, a row skipped for an invalid interval stays+ # unscheduled after the interval is repaired here, until the next+ # login. It runs after the commit and after the reply is built, and+ # the helper swallows its own failures, so it cannot change the reply.+ api._notify_scheduler_about_subscription_change("updated", username)+ return resultexcept Exception as e:return JSONResponse(diff --git a/src/local_deep_research/web/routers/research.py b/src/local_deep_research/web/routers/research.pyindex 6cf921ece5..f1431ab09f 100644--- a/src/local_deep_research/web/routers/research.py+++ b/src/local_deep_research/web/routers/research.py@@ -957,6 +957,45 @@ async def start_research()+def _unrunnable_subscription_response(db_session, metadata):+ """400 response for a run linked to a saved subscription that cannot run.++ ``metadata.subscription_id`` makes the finished run advance that+ subscription's schedule, which needs a runnable saved interval (see+ ``is_runnable_refresh_interval``). The manual-run endpoint and the+ overdue sweep check that before they call in; checking it in this shared+ entry point as well covers a direct ``POST /api/start_research``. An id+ that matches no saved subscription is left alone, as before.++ Any truthy id is looked up as ``str(id)``, the way the completion advance+ (``advance_refresh_schedule_by_id``) resolves it, so a JSON number cannot+ pass this check and still advance a numeric-string row at completion.+ """+ subscription_id = metadata.get("subscription_id")+ if not subscription_id:+ return None+ subscription_id = str(subscription_id)+ from ...database.models.news import NewsSubscription+ from ...news.constants import (+ SUBSCRIPTION_INTERVAL_REPAIR_MESSAGE,+ is_runnable_refresh_interval,+ )++ subscription = (+ db_session.query(NewsSubscription)+ .filter(NewsSubscription.id == subscription_id)+ .first()+ )+ if subscription is None or is_runnable_refresh_interval(+ subscription.refresh_interval_minutes+ ):+ return None+ return JSONResponse(+ {"status": "error", "message": SUBSCRIPTION_INTERVAL_REPAIR_MESSAGE},+ status_code=400,+ )++def _start_research_sync(data: dict, username: str, base_url: str, session_id: str | None):@@ -1017,6 +1056,10 @@ def _start_research_sync(from ...settings import SettingsManagerwith get_user_db_session(username) as db_session:+ unrunnable = _unrunnable_subscription_response(db_session, metadata)+ if unrunnable is not None:+ return unrunnable+settings_manager = SettingsManager(db_session=db_session)try:params = _extract_research_params(data, settings_manager)diff --git a/src/local_deep_research/web/static/js/components/subscription-manager.js b/src/local_deep_research/web/static/js/components/subscription-manager.jsindex bd0efc1a69..e3bed183f1 100644--- a/src/local_deep_research/web/static/js/components/subscription-manager.js+++ b/src/local_deep_research/web/static/js/components/subscription-manager.js@@ -258,6 +258,26 @@ class SubscriptionManager {if (!subscription) return;+ // The frequency list must offer the interval the row has now. When the+ // presets do not, the browser selects the first one and saving would+ // silently rewrite the interval.+ const currentInterval = subscription.refresh_interval_minutes;+ let currentIntervalOption = '';+ if (![60, 180, 360, 720, 1440, 10080].includes(currentInterval)) {+ if (Number.isInteger(currentInterval) && currentInterval >= 1 && currentInterval <= 10080) {+ // A valid custom interval stays selectable, so saving keeps it.+ currentIntervalOption = `<option value="${currentInterval}" selected>Current: ${currentInterval} minutes</option>`;+ } else {+ // An interval that cannot be saved again (above the one-week+ // limit, or not a whole number of minutes) is a disabled+ // placeholder; saving needs an explicit new choice.+ const placeholderLabel = Number.isInteger(currentInterval) && currentInterval > 10080+ ? `Current: ${currentInterval} minutes (above the 1-week limit)`+ : 'Current interval is not valid';+ currentIntervalOption = `<option value="" selected disabled>${placeholderLabel}</option>`;+ }+ }+// Create edit modalconst modalHtml = `<div class="modal fade" id="editSubscriptionModal" tabindex="-1">@@ -271,6 +291,7 @@ class SubscriptionManager {<div class="mb-3"><label class="form-label">Update Frequency</label><select class="form-select" id="edit-frequency">+ ${currentIntervalOption}<option value="60" ${subscription.refresh_interval_minutes === 60 ? 'selected' : ''}>Every hour</option><option value="180" ${subscription.refresh_interval_minutes === 180 ? 'selected' : ''}>Every 3 hours</option><option value="360" ${subscription.refresh_interval_minutes === 360 ? 'selected' : ''}>Every 6 hours</option>@@ -316,14 +337,27 @@ class SubscriptionManager {document.getElementById('edit-notes').value = subscription.notes || '';const modal = new bootstrap.Modal(modalElement);const saveButton = document.getElementById('save-subscription-edit');+ const frequencySelect = document.getElementById('edit-frequency');++ // The placeholder for an interval that cannot be saved again has an+ // empty value; the first change can only pick a real frequency.+ if (frequencySelect.value === '') {+ saveButton.disabled = true;+ frequencySelect.addEventListener('change', () => {+ saveButton.disabled = false;+ }, { once: true });+ }// Handle savesaveButton.addEventListener('click', async () => {if (saveButton.disabled) return;++ const refreshInterval = parseInt(frequencySelect.value, 10);+ if (!Number.isInteger(refreshInterval)) return;saveButton.disabled = true;const updates = {- refresh_interval_minutes: parseInt(document.getElementById('edit-frequency').value, 10),+ refresh_interval_minutes: refreshInterval,folder: document.getElementById('edit-folder').value,notes: document.getElementById('edit-notes').value};diff --git a/src/local_deep_research/web/static/js/pages/subscriptions.js b/src/local_deep_research/web/static/js/pages/subscriptions.jsindex a1bf8b08db..4ae6428e8d 100644--- a/src/local_deep_research/web/static/js/pages/subscriptions.js+++ b/src/local_deep_research/web/static/js/pages/subscriptions.js@@ -368,7 +368,7 @@ function filterSubscriptions() {// New subscription creation now handled by dedicated form page-// Run subscription now - uses the same research system as news page+// Run the saved subscription through its interval and configuration checks.async function runSubscriptionNow(subscriptionId) {const subscription = subscriptions.find(s => s.id === subscriptionId);if (!subscription || runningSubscriptionIds.has(subscriptionId)) return;@@ -382,28 +382,15 @@ async function runSubscriptionNow(subscriptionId) {const safeQuery = escapeHtml(query);showAlert('Starting research for: ' + safeQuery, 'info');- const requestData = {- query,- mode: 'quick',- metadata: {- is_news_search: true,- search_type: 'news_analysis',- display_in: 'news_feed',- subscription_id: subscriptionId,- triggered_by: 'manual_run'- }- };- SafeLogger.log('Sending research request:', requestData);-- // Use the same research endpoint as the news page- const response = await fetch('/api/start_research', {+ // The server validates the saved interval and uses the subscription's+ // stored provider and model before it starts research.+ const response = await fetch(`/news/api/subscriptions/${encodeURIComponent(subscriptionId)}/run`, {...fetchOptions,method: 'POST',headers: {'Content-Type': 'application/json','X-CSRFToken': getCSRFToken()- },- body: JSON.stringify(requestData)+ }});if (response.ok) {diff --git a/tests/js/components/subscription-manager-actions-runtime.test.js b/tests/js/components/subscription-manager-actions-runtime.test.jsindex 032fb77b7e..ad662941aa 100644--- a/tests/js/components/subscription-manager-actions-runtime.test.js+++ b/tests/js/components/subscription-manager-actions-runtime.test.js@@ -198,3 +198,146 @@ it('does not let an older save completion hide a newly opened edit modal', asyncexpect(document.getElementById('editSubscriptionModal')).toBe(newerModal);expect(document.getElementById('edit-notes').value).toBe('New modal notes');});++async function openIntervalEditModal(interval, fetchImplementation) {+ setSubscriptions(+ subscription('interval-edit', { refresh_interval_minutes: interval }),+ );+ installBootstrapModal();+ const fetchMock = vi.fn(+ fetchImplementation ||+ (() => Promise.resolve({ ok: true, status: 200 })),+ );+ vi.stubGlobal('fetch', fetchMock);+ vi.spyOn(manager, 'getCSRFToken').mockReturnValue('csrf-edit');+ vi.spyOn(manager, 'loadSubscriptionData').mockResolvedValue();+ vi.spyOn(manager, 'showSuccess').mockImplementation(() => {});+ vi.spyOn(manager, 'showError').mockImplementation(() => {});++ await manager.editSubscription('interval-edit');+ const select = document.getElementById('edit-frequency');+ return {+ fetchMock,+ select,+ // Read from the markup: happy-dom resolves `selected` on options after+ // the second one to the wrong option and reports selectedIndex -1 for+ // a first option that carries it, so only a first-position selection+ // is checked through select.value below.+ selectedOptions: Array.from(select.querySelectorAll('option[selected]')),+ saveButton: document.getElementById('save-subscription-edit'),+ };+}++function sentIntervals(fetchMock) {+ return fetchMock.mock.calls.map(+ ([, options]) => JSON.parse(options.body).refresh_interval_minutes,+ );+}++function choose(select, value) {+ select.value = value;+ select.dispatchEvent(new Event('change', { bubbles: true }));+}++it('requires an explicit choice before replacing a saved interval above the one-week limit', async () => {+ const { fetchMock, select, selectedOptions, saveButton } =+ await openIntervalEditModal(43200);++ // Saving without a choice must not send the first preset, which is what+ // the browser selects when the list holds no option for the saved value.+ saveButton.click();+ expect(sentIntervals(fetchMock)).toEqual([]);++ expect(selectedOptions).toHaveLength(1);+ expect(selectedOptions[0].disabled).toBe(true);+ expect(selectedOptions[0].textContent).toBe(+ 'Current: 43200 minutes (above the 1-week limit)',+ );+ expect(select.value).toBe('');+ expect(saveButton.disabled).toBe(true);++ choose(select, '10080');+ expect(saveButton.disabled).toBe(false);+ saveButton.click();+ await vi.waitFor(() => {+ expect(sentIntervals(fetchMock)).toEqual([10080]);+ });+});++it('does not re-enable a pending save when the frequency changes again', async () => {+ const pendingUpdate = deferred();+ const { fetchMock, select, saveButton } = await openIntervalEditModal(+ 43200,+ () => pendingUpdate.promise,+ );++ choose(select, '1440');+ saveButton.click();+ expect(sentIntervals(fetchMock)).toEqual([1440]);+ expect(saveButton.disabled).toBe(true);++ choose(select, '720');+ saveButton.click();++ expect(saveButton.disabled).toBe(true);+ expect(sentIntervals(fetchMock)).toEqual([1440]);+ pendingUpdate.resolve({ ok: true, status: 200 });+});++it.each([0, -5, 90.5, null, 'soon'])(+ 'requires an explicit choice when the saved interval %s is not a whole number of minutes in range',+ async (interval) => {+ const { fetchMock, select, selectedOptions, saveButton } =+ await openIntervalEditModal(interval);++ saveButton.click();+ expect(sentIntervals(fetchMock)).toEqual([]);++ expect(selectedOptions).toHaveLength(1);+ expect(selectedOptions[0].disabled).toBe(true);+ expect(selectedOptions[0].textContent).toBe(+ 'Current interval is not valid',+ );+ expect(select.value).toBe('');+ expect(saveButton.disabled).toBe(true);++ choose(select, '60');+ saveButton.click();+ await vi.waitFor(() => {+ expect(sentIntervals(fetchMock)).toEqual([60]);+ });+ },+);++it.each([90, 2880])(+ 'keeps a valid custom interval of %i minutes selected and saves it unchanged',+ async (minutes) => {+ const { fetchMock, select, selectedOptions, saveButton } =+ await openIntervalEditModal(minutes);++ expect(selectedOptions).toHaveLength(1);+ expect(selectedOptions[0].disabled).toBe(false);+ expect(selectedOptions[0].textContent).toBe(`Current: ${minutes} minutes`);+ expect(select.value).toBe(String(minutes));+ expect(saveButton.disabled).toBe(false);++ saveButton.click();+ await vi.waitFor(() => {+ expect(sentIntervals(fetchMock)).toEqual([minutes]);+ });+ },+);++it.each([60, 180, 360, 720, 1440, 10080])(+ 'selects the %i-minute preset without adding a current-interval option',+ async (minutes) => {+ const { select, selectedOptions, saveButton } =+ await openIntervalEditModal(minutes);++ expect(select.options).toHaveLength(6);+ expect(selectedOptions.map((option) => option.value)).toEqual([+ String(minutes),+ ]);+ expect(saveButton.disabled).toBe(false);+ },+);diff --git a/tests/js/pages/subscriptions-card-actions-runtime.test.js b/tests/js/pages/subscriptions-card-actions-runtime.test.jsindex c142b24982..cb09a20b04 100644--- a/tests/js/pages/subscriptions-card-actions-runtime.test.js+++ b/tests/js/pages/subscriptions-card-actions-runtime.test.js@@ -195,7 +195,7 @@ it('keeps every inline handler off the card', () => {it.each([['the card body', '.ldr-query-text', [[`${ACTION_URL}/history`, 'GET']]],- ['Run now', 'button[data-subscription-action="run"] i', [['/api/start_research', 'POST']]],+ ['Run now', 'button[data-subscription-action="run"] i', [[`${ACTION_URL}/run`, 'POST']]],['Pause', 'button[data-subscription-action="toggle"] i', [[ACTION_URL, 'PUT']]],['View history', 'button[data-subscription-action="history"] i', [[`${ACTION_URL}/history`, 'GET']]],['Delete', 'button[data-subscription-action="delete"] i', [[ACTION_URL, 'DELETE']]],@@ -268,7 +268,7 @@ it('dispatches each card action with the exact subscription ID', async () => {));await vi.waitFor(() => {expect(actionRequests()).toEqual([- ['/api/start_research', 'POST'],+ [`${ACTION_URL}/run`, 'POST'],[ACTION_URL, 'PUT'],[`${ACTION_URL}/history`, 'GET'],[`${ACTION_URL}/history`, 'GET'],@@ -276,8 +276,43 @@ it('dispatches each card action with the exact subscription ID', async () => {]);});const [, runOptions] = fetchMock.mock.calls- .find(([url]) => url === '/api/start_research');- expect(JSON.parse(runOptions.body).metadata.subscription_id)- .toBe(HOSTILE_ID);+ .find(([url]) => url === `${ACTION_URL}/run`);+ expect(runOptions.headers['X-CSRFToken']).toBe('csrf-card-actions');+ expect(runOptions.credentials).toBe('same-origin');+ expect(runOptions.body).toBeUndefined();expect(confirm).toHaveBeenCalledOnce();});++it('shows the saved-interval refusal from Run now and permits retry after repair', async () => {+ const start = fetchMock.mock.calls.length;+ const message = "Edit this subscription's refresh interval before running it";+ fetchMock.mockResolvedValueOnce(jsonResponse({ error: message }, 400));++ cardButtonIcon('run').click();++ expect(requestsSince(start)).toEqual([[`${ACTION_URL}/run`, 'POST']]);+ await vi.waitFor(() => {+ expect(document.querySelector('.alert-danger')?.textContent)+ .toContain('refresh interval before running it');+ });+ expect(document.querySelector('.alert-danger').textContent)+ .toContain('Edit this subscription');+ expect(localStorage.getItem('active_news_research')).toBeNull();++ fetchMock.mockResolvedValueOnce(jsonResponse({+ status: 'success',+ research_id: '12345678-1234-1234-1234-123456789abc',+ }));+ cardButtonIcon('run').click();++ await vi.waitFor(() => {+ expect(document.querySelector('.alert-success')?.textContent)+ .toContain('Research started!');+ });+ expect(requestsSince(start)).toEqual([+ [`${ACTION_URL}/run`, 'POST'],+ [`${ACTION_URL}/run`, 'POST'],+ ]);+ expect(JSON.parse(localStorage.getItem('active_news_research')).researchId)+ .toBe('12345678-1234-1234-1234-123456789abc');+});diff --git a/tests/js/pages/subscriptions-direct-runtime.test.js b/tests/js/pages/subscriptions-direct-runtime.test.jsindex de8582ff15..92e711c6da 100644--- a/tests/js/pages/subscriptions-direct-runtime.test.js+++ b/tests/js/pages/subscriptions-direct-runtime.test.js@@ -50,7 +50,6 @@ function installDom() {let serverSubscriptions;let serverFolders;let fetchMock;-let startResearchPayload;let modalShow;let registeredDocumentListeners;@@ -165,8 +164,7 @@ beforeEach(async () => {scheduled_jobs: 3,});}- if (url === '/api/start_research') {- startResearchPayload = JSON.parse(options.body);+ if (url === '/news/api/subscriptions/sub-active/run') {return jsonResponse({status: 'queued',research_id: 'research-3299<script>',@@ -313,7 +311,9 @@ it('runs research through the real page handler with CSRF and a safe progress liawait window.runSubscriptionNow('sub-active');- const [, options] = fetchMock.mock.calls.find(([url]) => url === '/api/start_research');+ const [, options] = fetchMock.mock.calls.find(([url]) => (+ url === '/news/api/subscriptions/sub-active/run'+ ));expect(options).toMatchObject({method: 'POST',credentials: 'same-origin',@@ -322,17 +322,9 @@ it('runs research through the real page handler with CSRF and a safe progress li'X-CSRFToken': 'csrf-subscriptions-direct',},});- expect(startResearchPayload).toEqual({- query: 'FastAPI migration updates',- mode: 'quick',- metadata: {- is_news_search: true,- search_type: 'news_analysis',- display_in: 'news_feed',- subscription_id: 'sub-active',- triggered_by: 'manual_run',- },- });+ expect(options.body).toBeUndefined();+ expect(fetchMock.mock.calls.some(([url]) => url === '/api/start_research'))+ .toBe(false);expect(JSON.parse(localStorage.getItem('active_news_research'))).toMatchObject({researchId: 'research-3299<script>',diff --git a/tests/js/pages/subscriptions-migration-runtime.test.js b/tests/js/pages/subscriptions-migration-runtime.test.jsindex 9429307edf..fe4c1f6616 100644--- a/tests/js/pages/subscriptions-migration-runtime.test.js+++ b/tests/js/pages/subscriptions-migration-runtime.test.js@@ -209,7 +209,7 @@ it('renders the migrated scheduler status envelope', async () => {.toBe('2 active users, 5 scheduled jobs');});-it('starts a queued subscription research with the migrated payload and CSRF', async () => {+it('starts queued subscription research through the saved-subscription route with CSRF', async () => {vi.useFakeTimers();const runSource = sourceBlock(/(async function runSubscriptionNow\(subscriptionId\)\s*\{[\s\S]*?\n\})\n\n\/\/ Toggle subscription/,@@ -249,24 +249,14 @@ it('starts a queued subscription research with the migrated payload and CSRF', aexpect(fetchMock).toHaveBeenCalledOnce();const [url, options] = fetchMock.mock.calls[0];- expect(url).toBe('/api/start_research');+ expect(url).toBe('/news/api/subscriptions/sub-1/run');expect(options.method).toBe('POST');expect(options.credentials).toBe('same-origin');expect(options.headers).toEqual({'Content-Type': 'application/json','X-CSRFToken': 'csrf-subscriptions',});- expect(JSON.parse(options.body)).toEqual({- query: 'FastAPI migration news',- mode: 'quick',- metadata: {- is_news_search: true,- search_type: 'news_analysis',- display_in: 'news_feed',- subscription_id: 'sub-1',- triggered_by: 'manual_run',- },- });+ expect(options.body).toBeUndefined();expect(calls.showAlert).toHaveBeenLastCalledWith(expect.stringContaining('/progress/research-1script'),'success',diff --git a/tests/js/pages/subscriptions-xss.test.js b/tests/js/pages/subscriptions-xss.test.jsindex a63a13b193..9ffddd466d 100644--- a/tests/js/pages/subscriptions-xss.test.js+++ b/tests/js/pages/subscriptions-xss.test.js@@ -291,7 +291,7 @@ describe('subscriptions adversarial rendering', () => {// Action buttons route through delegation using data-subscription-id.card.querySelector('[data-subscription-action="run"]').click();await vi.waitFor(() => {- expect(globalThis.fetch).toHaveBeenCalledWith('/api/start_research', expect.anything());+ expect(globalThis.fetch).toHaveBeenCalledWith('/news/api/subscriptions/subscription-1/run', expect.anything());});});@@ -508,13 +508,13 @@ describe('subscription alerts', () => {['a rejected Run',(grid) => grid.querySelector('[data-subscription-action="run"]'),- (url) => url === '/api/start_research',+ (url) => url === '/news/api/subscriptions/subscription-1/run',stubResponse({ detail: HOSTILE_MARKUP }, false, 503),],['a Run the server did not start',(grid) => grid.querySelector('[data-subscription-action="run"]'),- (url) => url === '/api/start_research',+ (url) => url === '/news/api/subscriptions/subscription-1/run',stubResponse({ status: 'error', message: HOSTILE_MARKUP }),],[diff --git a/tests/news/subscription_manager/test_scheduler.py b/tests/news/subscription_manager/test_scheduler.pyindex 44a4e4951c..e451c3dc95 100644--- a/tests/news/subscription_manager/test_scheduler.py+++ b/tests/news/subscription_manager/test_scheduler.py@@ -654,6 +654,7 @@ class TestCheckUserOverdueSubscriptions:mock_subscription.id = 1mock_subscription.name = "Overdue"mock_subscription.query_or_topic = "test"+ mock_subscription.refresh_interval_minutes = 60with patch("local_deep_research.database.session_context.get_user_db_session"diff --git a/tests/news/test_news_api_contract_restored.py b/tests/news/test_news_api_contract_restored.pyindex 6eab8ac574..d19db8054e 100644--- a/tests/news/test_news_api_contract_restored.py+++ b/tests/news/test_news_api_contract_restored.py@@ -154,6 +154,7 @@ def _subscription(sub_id, name, next_refresh="2026-08-25T00:00:00+00:00"):sub.name = namesub.query_or_topic = f"topic for {sub_id}"sub.next_refresh = next_refresh+ sub.refresh_interval_minutes = 60sub.model_provider = "ollama"sub.model = "llama3"sub.search_strategy = "source-based"diff --git a/tests/news/test_news_run_subscription_ported.py b/tests/news/test_news_run_subscription_ported.pyindex ae2862526a..66402e4473 100644--- a/tests/news/test_news_run_subscription_ported.py+++ b/tests/news/test_news_run_subscription_ported.py@@ -165,6 +165,7 @@ def _subscription_row(sub_id=SUB_ID, next_refresh=NEXT_REFRESH):sub.id = sub_idsub.name = "Fusion digest"sub.query_or_topic = "fusion energy"+ sub.refresh_interval_minutes = 60sub.next_refresh = next_refreshsub.model_provider = "ollama"sub.model = "llama3"diff --git a/tests/news/test_news_service_contracts.py b/tests/news/test_news_service_contracts.pyindex 5ab4676efa..c8f1312dd7 100644--- a/tests/news/test_news_service_contracts.py+++ b/tests/news/test_news_service_contracts.py@@ -41,9 +41,8 @@ DEFECTS PINNED AS CHARACTERIZATION (each named in the test's docstring):D1 ``news/core/search_integration.py:69`` imports``news.preference_manager.search_tracker``, which does not exist.-D2 ``refresh_minutes`` is unvalidated end to end. ``0`` persists and- makes the subscription permanently due; APScheduler coerces the- resulting zero interval to ONE SECOND.+D2 Resolved: the service rejects invalid refresh intervals before saving+ them, and the scheduler rejects invalid legacy rows before dispatch.D3 ``delete_subscription`` / ``update_subscription`` do not pass theirknown ``username`` as the scheduler-notify fallback, though``create_subscription`` does -- so with no request context the@@ -76,8 +75,6 @@ mutations, and the single test that caught each:``test_card_storage_writes_an_unreadable_card_type``* ``SQLCardStorage.create`` assigning ``subscription_id`` ->``test_card_storage_drops_the_subscription_link``-* ``create_subscription`` clamping ``refresh_minutes`` to ``max(1, ...)``- -> ``test_nonpositive_refresh_interval_is_accepted_and_never_settles``* ``delete_subscription`` forwarding ``username`` to the notify hook ->``test_delete_and_update_lose_the_notify_when_context_is_absent``* the source-links fetch wrapped in its own ``try/except`` ->@@ -117,6 +114,7 @@ from local_deep_research.news.core.card_storage import SQLCardStoragefrom local_deep_research.news.exceptions import (DatabaseAccessException,InvalidLimitException,+ NewsAPIException,SubscriptionDeletionException,SubscriptionNotFoundException,)@@ -664,87 +662,43 @@ class TestOverdueDetection:assert sub.last_refresh is not None@pytest.mark.parametrize("refresh_minutes", [0, -1440])- def test_nonpositive_refresh_interval_is_accepted_and_never_settles(+ def test_nonpositive_refresh_interval_is_rejected_before_persisting(self, userdbs, refresh_minutes):- """D2 (DEFECT). ``refresh_minutes`` is unvalidated end to end.-- Neither the router (which reads ``data.get("refresh_minutes")``- raw) nor ``create_subscription`` bounds it. A zero or negative- interval persists, lands ``next_refresh`` at-or-before ``now``,- and -- crucially -- running the subscription does NOT clear the- condition: ``advance_refresh_schedule`` adds the same- non-positive interval, so it is due again immediately. That is an- unbounded run loop driven by a request-body field.- """- before_create = datetime.now(timezone.utc)- result = news_api.create_subscription(- user_id="alice",- query="widgets",- refresh_minutes=refresh_minutes,- )- after_create = datetime.now(timezone.utc)- assert result["status"] == "success"- assert result["refresh_minutes"] == refresh_minutes-- with userdbs.session("alice") as db:- sub = (- db.query(NewsSubscription)- .filter_by(id=result["subscription_id"])- .one()- )- assert sub.refresh_interval_minutes == refresh_minutes- offset = timedelta(minutes=refresh_minutes)- assert (- before_create + offset- <= sub.next_refresh- <= after_create + offset- )-- # Due right away...- due = (- db.query(NewsSubscription)- .filter(NewsSubscription.due_filter(datetime.now(timezone.utc)))- .all()+ """Service callers receive the same interval protection as HTTP callers."""+ with pytest.raises(NewsAPIException) as rejected:+ news_api.create_subscription(+ user_id="alice",+ query="widgets",+ refresh_minutes=refresh_minutes,)- assert [s.id for s in due] == [result["subscription_id"]]-- # ...and still due after a "successful" run advances it.- run_at = datetime.now(timezone.utc)- advance_refresh_schedule(sub, run_at)- assert sub.next_refresh <= run_at+ assert rejected.value.status_code == 400+ with userdbs.session("alice") as db:+ assert db.query(NewsSubscription).count() == 0def test_zero_interval_becomes_a_one_second_apscheduler_trigger(self):- """D2 consequence, asserted against APScheduler itself.-- ``_schedule_user_subscriptions`` routes any interval <= 60 minutes- to ``trigger="interval", minutes=refresh_minutes``. APScheduler- does not reject a zero interval -- it silently coerces it to one- second, turning D2's unvalidated ``0`` into a research run every- second for as long as the user is logged in.- """+ """APScheduler itself does not reject zero; our boundary must do so."""from apscheduler.triggers.interval import IntervalTriggerassert IntervalTrigger(minutes=0).interval == timedelta(seconds=1)- def test_update_can_also_install_a_nonpositive_interval(self, userdbs):- """D2 via the update path, which recomputes ``next_refresh`` from- ``now`` whenever the interval changes."""+ def test_update_rejects_a_nonpositive_interval(self, userdbs):+ """A rejected update preserves the usable saved schedule."""created = news_api.create_subscription(user_id="alice", query="widgets", refresh_minutes=240)sub_id = created["subscription_id"]- news_api.update_subscription(- sub_id, {"refresh_interval_minutes": 0}, username="alice"- )+ with pytest.raises(NewsAPIException) as rejected:+ news_api.update_subscription(+ sub_id, {"refresh_interval_minutes": 0}, username="alice"+ )+ assert rejected.value.status_code == 400with userdbs.session("alice") as db:sub = db.query(NewsSubscription).filter_by(id=sub_id).one()- assert sub.refresh_interval_minutes == 0- assert sub.next_refresh <= datetime.now(timezone.utc) + timedelta(- seconds=1- )+ assert sub.refresh_interval_minutes == 240+ assert sub.next_refresh > datetime.now(timezone.utc)class TestSchedulingForALoggedOutUser:diff --git a/tests/news/test_persisted_subscription_intervals.py b/tests/news/test_persisted_subscription_intervals.pynew file mode 100644index 0000000000..4930bc21d5--- /dev/null+++ b/tests/news/test_persisted_subscription_intervals.py@@ -0,0 +1,586 @@+"""Saved subscription intervals.++An interval that cannot be scheduled never dispatches research or becomes an+APScheduler job. Every interval the released version accepted does keep+running: v1.10.7 had no server-side cap and documented up to 30 days, so the+one-week cap on newly written values must not stop the rows it already saved.+"""++import json+from contextlib import contextmanager+from datetime import datetime, timedelta, UTC+from types import SimpleNamespace+from unittest.mock import Mock++import pytest+from sqlalchemy import create_engine, update+from sqlalchemy.orm import sessionmaker+from starlette.requests import Request++from local_deep_research.database import session_context+from local_deep_research.database.models.news import NewsSubscription+from local_deep_research.news import api, constants+from local_deep_research.news.exceptions import NewsAPIException+from local_deep_research.news.subscription_runner import (+ advance_refresh_schedule,+ advance_refresh_schedule_by_id,+)+from local_deep_research.scheduler import background+from local_deep_research.scheduler.background import BackgroundJobScheduler+from local_deep_research.web.routers import news_flask_api as routes++# Intervals above the write cap that v1.10.7 could save: one past the cap,+# biweekly, the documented 30-day maximum, and the saved-row ceiling.+LEGACY_INTERVALS = [10_081, 20_160, 43_200, 525_600]++REPAIR_MESSAGE = "Edit this subscription's refresh interval before running it"+++def world(monkeypatch):+ engine = create_engine("sqlite:///:memory:")+ NewsSubscription.__table__.create(engine)+ make_session = sessionmaker(bind=engine, expire_on_commit=False)++ @contextmanager+ def session(*_args, **_kwargs):+ with make_session() as db:+ yield db++ def seed(sub_id, interval):+ with session() as db:+ db.add(+ NewsSubscription(+ id=sub_id,+ name=sub_id,+ query_or_topic="A saved topic",+ subscription_type="topic",+ status="active",+ refresh_interval_minutes=60,+ next_refresh=datetime.now(UTC) - timedelta(hours=1),+ )+ )+ db.flush()+ # A legacy/imported value, bypassing today's service validation.+ db.execute(+ update(NewsSubscription)+ .where(NewsSubscription.id == sub_id)+ .values(refresh_interval_minutes=interval)+ )+ db.commit()++ notify = Mock()+ monkeypatch.setattr(session_context, "get_user_db_session", session)+ monkeypatch.setattr(+ api, "_validate_subscription_policy", Mock(return_value=None)+ )+ monkeypatch.setattr(+ api, "_notify_scheduler_about_subscription_change", notify+ )+ # Which rows were already reported is process state; every test starts+ # with none reported.+ monkeypatch.setattr(+ background, "_reported_invalid_intervals", set(), raising=False+ )+ scheduler = BackgroundJobScheduler()+ scheduler._credential_store = Mock()+ scheduler._credential_store.retrieve.return_value = "test-password"+ scheduler.user_sessions["user"] = {"scheduled_jobs": set()}+ scheduler.config["max_jitter_seconds"] = 0+ monkeypatch.setattr(scheduler, "_schedule_document_processing", Mock())+ trigger = Mock()+ monkeypatch.setattr(+ scheduler, "_trigger_subscription_research_sync", trigger+ )+ yield SimpleNamespace(+ session=session,+ seed=seed,+ scheduler=scheduler,+ trigger=trigger,+ notify=notify,+ )+ scheduler.scheduler.remove_all_jobs()+ engine.dispose()+++def web_run_harness(monkeypatch):+ """Patch the research start and settings seams the web run paths use."""+ start = Mock(return_value={"status": "success", "research_id": "research"})+ monkeypatch.setattr(routes, "_start_research_in_process", start)+ monkeypatch.setattr(+ "local_deep_research.settings.manager.SettingsManager", Mock()+ )+ monkeypatch.setattr(+ "local_deep_research.news.core.utils.get_local_date_string",+ Mock(return_value="2026-09-27"),+ )+ request = Request(+ {"type": "http", "method": "POST", "path": "/", "headers": []}+ )+ return start, request+++ ("value", "writable", "runnable"),+ [+ (1, True, True),+ (10_080, True, True),+ (10_081, False, True),+ (43_200, False, True),+ (525_600, False, True),+ (525_601, False, False),+ (10**12, False, False),+ (0, False, False),+ (-1, False, False),+ (1.5, False, False),+ (60.0, False, False),+ (float("nan"), False, False),+ (float("inf"), False, False),+ (True, False, False),+ ("60", False, False),+ (None, False, False),+ ],+)+def test_interval_bounds_separate_new_values_from_saved_rows(+ value, writable, runnable+):+ assert constants.is_valid_refresh_interval(value) is writable+ assert constants.is_runnable_refresh_interval(value) is runnable+++def test_saved_row_ceiling_covers_the_released_documented_range():+ assert constants.NEWS_SUBSCRIPTION_MAX_SAVED_REFRESH_MINUTES >= 30 * 24 * 60+ assert (+ constants.NEWS_SUBSCRIPTION_MAX_SAVED_REFRESH_MINUTES+ > constants.NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES+ )+++ "interval", [0, -100, 525_601, 10**12, None, "bad", 1.5]+)+def test_invalid_saved_interval_is_not_scheduled_but_valid_rows_are(+ world, interval+):+ world.seed("bad", interval)+ valid = (1, 60, 240, 10_080, 43_200)+ for minutes in valid:+ world.seed(str(minutes), minutes)+ world.scheduler._schedule_user_subscriptions("user")+ jobs = {job.id: job for job in world.scheduler.scheduler.get_jobs()}+ assert set(jobs) == {f"user_{minutes}" for minutes in valid}+ assert jobs["user_1"].trigger.interval == timedelta(minutes=1)+ assert jobs["user_60"].trigger.interval == timedelta(minutes=60)+ for minutes in (240, 10_080, 43_200):+ assert type(jobs[f"user_{minutes}"].trigger).__name__ == "DateTrigger"+ world.trigger.assert_not_called()+++def test_invalid_overdue_row_does_not_prevent_valid_overdue_job(world):+ world.seed("bad", 0)+ world.seed("good", 60)+ world.scheduler._check_user_overdue_subscriptions("user")+ jobs = world.scheduler.scheduler.get_jobs()+ assert len(jobs) == 1+ assert jobs[0].args == ("user", "good")++[email protected]("interval", [0, -100, None, "bad", 525_601])+def test_preexisting_job_is_removed_before_dispatching_invalid_row(+ world, interval+):+ world.seed("bad", interval)+ scheduler = world.scheduler+ scheduler.scheduler.add_job(+ lambda: None, "interval", minutes=1, id="user_bad"+ )+ scheduler.user_sessions["user"]["scheduled_jobs"].add("user_bad")+ with world.session() as db:+ before = db.get(NewsSubscription, "bad").next_refresh+ scheduler._check_subscription("user", "bad")+ world.trigger.assert_not_called()+ assert scheduler.scheduler.get_job("user_bad") is None+ assert scheduler.user_sessions["user"]["scheduled_jobs"] == set()+ with world.session() as db:+ assert db.get(NewsSubscription, "bad").next_refresh == before+++def test_valid_scheduled_job_still_dispatches_and_advances(world):+ world.seed("good", 60)+ world.scheduler._check_subscription("user", "good")+ world.trigger.assert_called_once()+ with world.session() as db:+ sub = db.get(NewsSubscription, "good")+ assert sub.next_refresh - sub.last_refresh == timedelta(minutes=60)++[email protected]("interval", LEGACY_INTERVALS)+def test_saved_interval_above_the_write_cap_keeps_running(world, interval):+ """The cap limits what is written now; rows v1.10.7 saved still run."""+ world.seed("legacy", interval)+ scheduler = world.scheduler++ scheduler._schedule_user_subscriptions("user")+ job = scheduler.scheduler.get_job("user_legacy")+ assert type(job.trigger).__name__ == "DateTrigger"++ scheduler._check_user_overdue_subscriptions("user")+ overdue = [+ job.args+ for job in scheduler.scheduler.get_jobs()+ if job.id.startswith("overdue_")+ ]+ assert overdue == [("user", "legacy")]++ scheduler._check_subscription("user", "legacy")+ world.trigger.assert_called_once()+ with world.session() as db:+ sub = db.get(NewsSubscription, "legacy")+ assert sub.next_refresh - sub.last_refresh == timedelta(+ minutes=interval+ )++[email protected]("interval", LEGACY_INTERVALS)+def test_advance_accepts_a_saved_interval_above_the_write_cap(interval):+ now = datetime(2026, 10, 16, tzinfo=UTC)+ row = SimpleNamespace(+ refresh_interval_minutes=interval, last_refresh=None, next_refresh=None+ )+ advance_refresh_schedule(row, now)+ assert row.last_refresh == now+ assert row.next_refresh == now + timedelta(minutes=interval)+++ "interval", [0, -100, 525_601, True, "60", 1.5, float("nan")]+)+def test_advance_rejects_an_unrunnable_interval_without_moving_the_schedule(+ world, interval+):+ world.seed("good", 60)+ with world.session() as db:+ row = db.get(NewsSubscription, "good")+ before = (row.last_refresh, row.next_refresh)+ row.refresh_interval_minutes = interval+ with pytest.raises(NewsAPIException) as rejected:+ advance_refresh_schedule(row)+ assert rejected.value.status_code == 400+ assert (row.last_refresh, row.next_refresh) == before+ with world.session() as db:+ assert db.get(NewsSubscription, "good").refresh_interval_minutes == 60++[email protected]("operation", ["create", "update"])+ "interval", [0, -100, 10_081, 43_200, True, "60", 1.5, float("nan")]+)+def test_new_intervals_above_the_cap_or_malformed_are_rejected(+ world, operation, interval+):+ world.seed("good", 60)+ with pytest.raises(NewsAPIException) as rejected:+ if operation == "create":+ api.create_subscription("user", "Topic", refresh_minutes=interval)+ else:+ api.update_subscription(+ "good",+ {"name": "changed", "refresh_interval_minutes": interval},+ username="user",+ )+ assert rejected.value.status_code == 400+ with world.session() as db:+ assert db.query(NewsSubscription).count() == 1+ unchanged = db.get(NewsSubscription, "good")+ assert unchanged.name == "good"+ assert unchanged.refresh_interval_minutes == 60+++def test_editing_a_row_above_the_write_cap_must_lower_the_interval(world):+ """A legacy row keeps running, but an edit that sends its interval is a write.++ Both editors send the interval with every save, so saving one needs 10080+ or less (the docs say so). Pausing sends no interval and leaves it as saved.+ """+ world.seed("legacy", 43_200)++ with pytest.raises(NewsAPIException) as rejected:+ api.update_subscription(+ "legacy",+ {"name": "renamed", "refresh_interval_minutes": 43_200},+ username="user",+ )+ assert rejected.value.status_code == 400+ assert rejected.value.error_code == "INVALID_REFRESH_INTERVAL"+ full_put = routes._validate_subscription_payload(+ {"refresh_minutes": 43_200}+ )+ assert full_put.status_code == 400+ assert (+ constants.compact_subscription_update_error(+ {"refresh_interval_minutes": 43_200}+ )+ == "refresh_interval_minutes must be an integer between 1 and 10080"+ )+ with world.session() as db:+ row = db.get(NewsSubscription, "legacy")+ assert (row.name, row.refresh_interval_minutes) == ("legacy", 43_200)++ api.update_subscription("legacy", {"is_active": False}, username="user")+ with world.session() as db:+ row = db.get(NewsSubscription, "legacy")+ assert (row.status, row.refresh_interval_minutes) == ("paused", 43_200)++ api.update_subscription(+ "legacy",+ {"name": "renamed", "refresh_interval_minutes": 10_080},+ username="user",+ )+ with world.session() as db:+ row = db.get(NewsSubscription, "legacy")+ assert (row.name, row.refresh_interval_minutes) == ("renamed", 10_080)++[email protected]("default_interval", [0, None, "bad", 10_081])+def test_invalid_saved_default_cannot_create_a_subscription(+ world, monkeypatch, default_interval+):+ settings = Mock()+ settings.get_setting.return_value = default_interval+ monkeypatch.setattr(+ "local_deep_research.utilities.db_utils.get_settings_manager",+ Mock(return_value=settings),+ )+ with pytest.raises(NewsAPIException) as rejected:+ api.create_subscription("user", "Topic")+ assert rejected.value.status_code == 400+ with world.session() as db:+ assert db.query(NewsSubscription).count() == 0++[email protected]("operation", ["manual", "overdue"])+def test_web_run_paths_skip_invalid_rows_before_starting_research(+ world, monkeypatch, operation+):+ world.seed("bad", 0)+ world.seed("good", 60)+ start, request = web_run_harness(monkeypatch)+ if operation == "manual":+ response = routes.run_subscription_now("bad", request, "user")+ assert response.status_code == 400+ assert json.loads(response.body) == {"error": REPAIR_MESSAGE}+ start.assert_not_called()+ else:+ response = routes.check_overdue_subscriptions(request, "user")+ assert response["started"] == 1+ assert len(response["results"]) == 2+ assert any(+ row["id"] == "bad" and row["error"] == REPAIR_MESSAGE+ for row in response["results"]+ )+ start.assert_called_once()+ assert start.call_args.args[1]["metadata"]["subscription_id"] == "good"++[email protected]("interval", LEGACY_INTERVALS)[email protected]("operation", ["manual", "overdue"])+def test_web_run_paths_run_a_saved_interval_above_the_write_cap(+ world, monkeypatch, operation, interval+):+ world.seed("legacy", interval)+ start, request = web_run_harness(monkeypatch)+ if operation == "manual":+ response = routes.run_subscription_now("legacy", request, "user")+ assert response["status"] == "success"+ else:+ response = routes.check_overdue_subscriptions(request, "user")+ assert response["started"] == 1+ start.assert_called_once()+ assert start.call_args.args[1]["metadata"]["subscription_id"] == "legacy"+ with world.session() as db:+ sub = db.get(NewsSubscription, "legacy")+ assert sub.next_refresh - sub.last_refresh == timedelta(+ minutes=interval+ )+++def test_compact_edit_that_repairs_an_interval_reschedules_the_row(+ world, monkeypatch+):+ """The compact PUT must reach the scheduler like the full update does."""+ monkeypatch.setattr(routes, "get_user_db_session", world.session)+ # What the real notification does for a running scheduler.+ world.notify.side_effect = lambda *_args: (+ world.scheduler._schedule_user_subscriptions("user")+ )+ world.seed("bad", 0)+ world.scheduler._schedule_user_subscriptions("user")+ assert world.scheduler.scheduler.get_job("user_bad") is None++ result = routes._update_subscription_folder_sync(+ {"refresh_interval_minutes": 120}, "bad", "user"+ )++ assert result["refresh_interval_minutes"] == 120+ world.notify.assert_called_once_with("updated", "user")+ assert world.scheduler.scheduler.get_job("user_bad") is not None+++def test_compact_edit_that_pauses_a_row_notifies_the_scheduler(+ world, monkeypatch+):+ monkeypatch.setattr(routes, "get_user_db_session", world.session)+ world.seed("running", 60)++ result = routes._update_subscription_folder_sync(+ {"is_active": False}, "running", "user"+ )++ assert result["status"] == "paused"+ world.notify.assert_called_once_with("updated", "user")+++ ("sub_id", "payload", "status_code"),+ [+ ("bad", {"refresh_interval_minutes": 10_081}, 400),+ ("bad", {"refresh_interval_minutes": 0}, 400),+ ("missing", {"name": "renamed"}, 404),+ ],+)+def test_compact_edit_that_saves_nothing_does_not_notify_the_scheduler(+ world, monkeypatch, sub_id, payload, status_code+):+ monkeypatch.setattr(routes, "get_user_db_session", world.session)+ world.seed("bad", 0)++ response = routes._update_subscription_folder_sync(payload, sub_id, "user")++ assert response.status_code == status_code+ world.notify.assert_not_called()+++def test_direct_start_research_refuses_a_link_to_an_unrunnable_subscription(+ world, monkeypatch+):+ """POST /api/start_research must not run research for a row /run refuses."""+ from local_deep_research.web.routers import research++ world.seed("bad", 0)+ world.seed("empty", None)+ world.seed("good", 60)+ world.seed("legacy", 43_200)+ monkeypatch.setattr(research, "get_user_db_session", world.session)+ monkeypatch.setattr("local_deep_research.settings.SettingsManager", Mock())+ # Parameter extraction is the next step after the guard; stopping it with+ # a validation error tells "passed the guard" from "refused by it".+ extract = Mock(side_effect=ValueError("iterations"))+ monkeypatch.setattr(research, "_extract_research_params", extract)++ def start(metadata):+ response = research._start_research_sync(+ {"query": "A saved topic", "metadata": metadata},+ "user",+ "http://testserver",+ None,+ )+ return response.status_code, json.loads(response.body)++ for sub_id in ("bad", "empty"):+ assert start({"subscription_id": sub_id}) == (+ 400,+ {"status": "error", "message": REPAIR_MESSAGE},+ )+ extract.assert_not_called()++ passed = (+ {"subscription_id": "good"},+ {"subscription_id": "legacy"},+ {"subscription_id": "no-such-row"},+ {"subscription_id": 7},+ {},+ )+ for metadata in passed:+ status_code, body = start(metadata)+ assert status_code == 400+ assert body["message"].startswith("iterations must be")+ assert extract.call_count == len(passed)+++def test_direct_start_research_guards_a_number_id_like_the_completion_advance(+ world, monkeypatch+):+ """A JSON-number id reaches the numeric-string row at completion.++ The completion advance looks the row up as ``str(id)``, so the guard must+ not treat ``{"subscription_id": 7}`` as "not an id" and let the run start.+ """+ from local_deep_research.web.routers import research++ world.seed("7", 0)+ world.seed("8", 60)+ # The premise: a number finds the numeric-string row in the completion+ # advance (the unrunnable row refuses to move, the runnable one moves).+ with world.session() as db:+ assert advance_refresh_schedule_by_id(db, 8) is True+ with pytest.raises(NewsAPIException):+ advance_refresh_schedule_by_id(db, 7)++ monkeypatch.setattr(research, "get_user_db_session", world.session)+ monkeypatch.setattr("local_deep_research.settings.SettingsManager", Mock())+ # As above: stopping parameter extraction tells "passed the guard" from+ # "refused by it".+ extract = Mock(side_effect=ValueError("iterations"))+ monkeypatch.setattr(research, "_extract_research_params", extract)++ def start(subscription_id):+ response = research._start_research_sync(+ {+ "query": "A saved topic",+ "metadata": {"subscription_id": subscription_id},+ },+ "user",+ "http://testserver",+ None,+ )+ return response.status_code, json.loads(response.body)++ assert start(7) == (+ 400,+ {"status": "error", "message": REPAIR_MESSAGE},+ )+ extract.assert_not_called()++ # A runnable row, an id that matches no row and falsy ids all pass.+ passed = (8, 9, 0, "")+ for subscription_id in passed:+ status_code, body = start(subscription_id)+ assert status_code == 400+ assert body["message"].startswith("iterations must be")+ assert extract.call_count == len(passed)+++def test_unusable_interval_is_reported_once_per_row_not_on_every_pass(+ world, monkeypatch+):+ logged = Mock()+ monkeypatch.setattr(background, "logger", logged)+ world.seed("bad", 0)+ world.seed("also-bad", None)+ world.seed("good", 60)++ for _ in range(3):+ world.scheduler._schedule_user_subscriptions("user")+ for _ in range(2):+ world.scheduler._check_user_overdue_subscriptions("user")++ def reported_ids(level):+ return [+ call.args[1]+ for call in getattr(logged, level).call_args_list+ if "invalid refresh interval" in call.args[0]+ ]++ # Each bad row is met five times: one warning, then four debug lines.+ assert sorted(reported_ids("warning")) == ["also-bad", "bad"]+ assert sorted(reported_ids("debug")) == ["also-bad"] * 4 + ["bad"] * 4diff --git a/tests/news/test_scheduler.py b/tests/news/test_scheduler.pyindex ba2223a7d6..28ec1babd6 100644--- a/tests/news/test_scheduler.py+++ b/tests/news/test_scheduler.py@@ -2601,6 +2601,7 @@ class TestCheckUserOverdueSubscriptions:mock_sub.id = 1mock_sub.name = "Overdue Sub"mock_sub.query_or_topic = "overdue query"+ mock_sub.refresh_interval_minutes = 60mock_query = MagicMock()mock_db.query.return_value = mock_querydiff --git a/tests/web/test_route_table_parity.py b/tests/web/test_route_table_parity.pyindex 6e4f4558cf..b8c46486a8 100644--- a/tests/web/test_route_table_parity.py+++ b/tests/web/test_route_table_parity.py@@ -1689,6 +1689,10 @@ EXPECTED_STATUS_CODES_GAINED = {# Intentional: an unimplemented endpoint answers 501 rather than letting# a bare `except Exception` turn NotImplementedException into a 500.("GET", "/news/api/categories"): [501],+ # Legacy subscriptions can contain intervals outside today's scheduling+ # bounds. Manual Run Now rejects them before dispatch and asks the user+ # to edit the interval, rather than starting research with invalid state.+ ("POST", "/news/api/subscriptions/{}/run"): [400],# Non-UUID collection ids used to be rendered into the page template# unvalidated (stored-XSS vector); they now 404 before rendering.# web/routers/rag.py:_validated_collection_id.