Skip to content

The agent that grows with you

refactor(gateway): hardcode the deferred scan deadline at 1800 s

unsupportedpastels8f0367c8ec6c
diff --git a/gateway/lifecycle_ledger.py b/gateway/lifecycle_ledger.py
index 6f4c4a45174..32dbaabc94e 100644
--- a/gateway/lifecycle_ledger.py
+++ b/gateway/lifecycle_ledger.py
@@ -188,8 +188,8 @@ def detect_unclean_exit(home: Optional[Path] = None) -> Optional[dict[str, Any]]
# and the restart loop never reaches the cron ticker. The progress handler below renews
# a phase-owned lease for as long as SQLite is making progress; without a deadline it
# always returns 0 so it never aborts the check, and the ok/absent/first-complaint
-# verdicts stay fail-closed. When an explicit ``deadline_at`` is supplied (t_8a73f016)
-# the handler returns non-zero once it passes, so SQLite aborts the scan and the caller
+# verdicts stay fail-closed. When an explicit ``deadline_at`` is supplied the
+# handler returns non-zero once it passes, so SQLite aborts the scan and the caller
# can bound a pathological store instead of pinning the boot path.
_INTEGRITY_CHECK_LEASE_PHASE = "state_db_unclean_integrity_check"
_INTEGRITY_CHECK_LEASE_S = 900.0
@@ -197,6 +197,8 @@ _INTEGRITY_CHECK_LEASE_S = 900.0
# monotonic-clock compare, renewing at most every _INTEGRITY_CHECK_LEASE_RENEW_S.
_INTEGRITY_CHECK_PROGRESS_OPS = 100_000
_INTEGRITY_CHECK_LEASE_RENEW_S = 60.0
+# Bound on the deferred post-connect scan: a pathological store cannot pin its thread forever.
+STATE_DB_INTEGRITY_DEADLINE_S = 1800.0
def _install_integrity_check_lease(conn: sqlite3.Connection, *, deadline_at: Optional[float] = None) -> None:
@@ -239,7 +241,7 @@ def check_state_db_integrity(home: Optional[Path] = None, *, deadline_s: Optiona
(renewed from a SQLite progress handler, #115542) so a long healthy check
on a huge store is not mistaken for a parked deadlock.
- ``deadline_s`` (optional, t_8a73f016): abort the scan once that many seconds elapse and
+ ``deadline_s`` (optional): abort the scan once that many seconds elapse and
return ``"check-incomplete: exceeded <N>s"``. ``deadline_s=None`` (default) is the
historic unbounded behaviour -- the progress handler never aborts the PRAGMA.
"""
@@ -346,7 +348,8 @@ def record_startup(home: Optional[Path] = None, *, defer_integrity_check: bool =
def run_deferred_integrity_check(
- evidence: Optional[dict[str, Any]], *, home: Optional[Path] = None, deadline_s: float = 1800.0
+ evidence: Optional[dict[str, Any]], *, home: Optional[Path] = None,
+ deadline_s: float = STATE_DB_INTEGRITY_DEADLINE_S,
) -> str:
"""Run the deferred post-connect state.db scan and append a second diag record.
diff --git a/gateway/run_bootstrap.py b/gateway/run_bootstrap.py
index 8625fc07df2..85a7becfa6e 100644
--- a/gateway/run_bootstrap.py
+++ b/gateway/run_bootstrap.py
@@ -2,34 +2,12 @@
from __future__ import annotations
import logging
-import math
import threading
from pathlib import Path
from typing import Any, Optional
from gateway.config import GatewayConfig
-_STATE_DB_INTEGRITY_DEADLINE_DEFAULT_S = 1800.0
-
-
-def _state_db_integrity_deadline_s() -> float:
- """``agent.gateway_state_db_integrity_deadline_s`` (default 1800 s) for the deferred scan.
-
- The deadline is the only bound on a pathological store, so anything but a finite positive
- number (unset, unparsable, ``0``, ``.inf``) keeps the default instead of removing it.
- """
- from gateway.run import _load_gateway_config
- # The gateway's own loader: managed overlay and ``${VAR}`` expansion apply, ``{}`` on error.
- agent_cfg = _load_gateway_config().get("agent")
- raw = agent_cfg.get("gateway_state_db_integrity_deadline_s") if isinstance(agent_cfg, dict) else None
- try:
- value = float(raw) if raw is not None and not isinstance(raw, bool) else None
- except (TypeError, ValueError):
- value = None
- if value is None or not math.isfinite(value) or value <= 0:
- return _STATE_DB_INTEGRITY_DEADLINE_DEFAULT_S
- return value
-
def _start_deferred_state_db_integrity_check(evidence: dict) -> None:
"""Run the store scan an unclean-exit report deferred, once the runner is live.
@@ -39,11 +17,11 @@ def _start_deferred_state_db_integrity_check(evidence: dict) -> None:
refused. A daemon thread nothing waits on; it only appends the forensic exit-diag record.
"""
from agent.memory_provider import spawn_context_thread
- from gateway.lifecycle_ledger import run_deferred_integrity_check
+ from gateway.lifecycle_ledger import STATE_DB_INTEGRITY_DEADLINE_S, run_deferred_integrity_check
# Under the caller's profile scope: the scan resolves this gateway's state.db through it.
spawn_context_thread(
run_deferred_integrity_check, args=(evidence,),
- kwargs={"deadline_s": _state_db_integrity_deadline_s()}, name="state-db-integrity-check").start()
+ kwargs={"deadline_s": STATE_DB_INTEGRITY_DEADLINE_S}, name="state-db-integrity-check").start()
# Log-record parity with the origin module.
logger = logging.getLogger("gateway.run")
diff --git a/tests/gateway/test_bootstrap_deferred_integrity_scan.py b/tests/gateway/test_bootstrap_deferred_integrity_scan.py
index b2d951f63af..6e8e3d585d7 100644
--- a/tests/gateway/test_bootstrap_deferred_integrity_scan.py
+++ b/tests/gateway/test_bootstrap_deferred_integrity_scan.py
@@ -7,44 +7,8 @@ import pytest
from gateway import run_bootstrap
-def _config(monkeypatch, agent):
- from gateway import run as gateway_run
- monkeypatch.setattr(gateway_run, "_load_gateway_config", lambda config_path=None: {"agent": agent})
-
-
[email protected]("raw, expected", [
- (None, 1800.0), (600, 600.0), ("90", 90.0), (0.5, 0.5),
- (0, 1800.0), (-5, 1800.0), (float("inf"), 1800.0), ("1e999", 1800.0), ("nan", 1800.0),
- ("soon", 1800.0), (True, 1800.0), ([], 1800.0),
-])
-def test_deadline_is_finite_and_positive(monkeypatch, raw, expected):
- _config(monkeypatch, {} if raw is None else {"gateway_state_db_integrity_deadline_s": raw})
- assert run_bootstrap._state_db_integrity_deadline_s() == expected
-
-
-def _gateway_home(monkeypatch, tmp_path, config_text):
- from gateway import run as gateway_run
- (tmp_path / "config.yaml").write_text(config_text, encoding="utf-8")
- monkeypatch.setenv("HERMES_HOME", str(tmp_path))
- monkeypatch.setattr(gateway_run, "_hermes_home", tmp_path)
-
-
-def test_deadline_honours_an_env_expanded_value(monkeypatch, tmp_path):
- """The gateway's config loader expands ``${VAR}``; the deadline must read through it."""
- monkeypatch.setenv("SCAN_DEADLINE_UNDER_TEST", "42")
- _gateway_home(monkeypatch, tmp_path,
- "agent:\n gateway_state_db_integrity_deadline_s: ${SCAN_DEADLINE_UNDER_TEST}\n")
- assert run_bootstrap._state_db_integrity_deadline_s() == 42.0
-
-
-def test_deadline_survives_unreadable_config(monkeypatch, tmp_path):
- _gateway_home(monkeypatch, tmp_path, "agent: [unclosed\n")
- assert run_bootstrap._state_db_integrity_deadline_s() == 1800.0
-
-
-def test_scan_runs_on_a_daemon_thread_with_the_configured_deadline(monkeypatch):
+def test_scan_runs_on_a_daemon_thread_with_the_1800s_deadline(monkeypatch):
from gateway import lifecycle_ledger as ledger
- _config(monkeypatch, {"gateway_state_db_integrity_deadline_s": 42})
seen, release, done = {}, threading.Event(), threading.Event()
def scan(evidence, *, deadline_s):
@@ -58,7 +22,7 @@ def test_scan_runs_on_a_daemon_thread_with_the_configured_deadline(monkeypatch):
assert not done.is_set()
release.set()
assert done.wait(5)
- assert seen["evidence"] is evidence and seen["deadline_s"] == 42.0
+ assert seen["evidence"] is evidence and seen["deadline_s"] == 1800.0
assert seen["thread"].daemon and seen["thread"].name == "state-db-integrity-check"
diff --git a/tests/gateway/test_lifecycle_state_db_check.py b/tests/gateway/test_lifecycle_state_db_check.py
index 1efd5445965..55e44057f36 100644
--- a/tests/gateway/test_lifecycle_state_db_check.py
+++ b/tests/gateway/test_lifecycle_state_db_check.py
@@ -11,7 +11,7 @@ VM death" — it just never looked at the database that death may have torn.
The check is gated on the unclean exit precisely because it costs ~2s on a
500MB store; a clean boot must not pay it.
-Since t_8a73f016 the scan is DEFERRED by default: the boot entry point writes
+The scan is now DEFERRED by default: the boot entry point writes
the exit-diag record and claims the sentinel immediately, and the forensic scan
runs post-connect (``run_deferred_integrity_check``) so a large store can never
gate platform startup (the ``:8644`` fleet peer-DM transport). The tests below
@@ -150,7 +150,7 @@ def test_clean_exit_does_not_pay_for_the_check(tmp_path: Path, monkeypatch) -> N
assert not called, "integrity check ran on a clean boot"
-# ── deferred startup (t_8a73f016): the scan must not gate platform startup ───
+# ── deferred startup: the scan must not gate platform startup ───
def test_deferred_startup_never_runs_the_check_inline(tmp_path: Path, monkeypatch) -> None: