Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

fix(people): time the contact keeper's next run from its last run, not a restart (#650)

Rufus787d070a6e50
diff --git a/packages/core/openexecutive/people/contact_keeper.py b/packages/core/openexecutive/people/contact_keeper.py
index 270edbfc..3e95c900 100644
--- a/packages/core/openexecutive/people/contact_keeper.py
+++ b/packages/core/openexecutive/people/contact_keeper.py
@@ -972,9 +972,12 @@ def maybe_run(now: datetime) -> bool:
_last_start = now
return False
last, _status = changes.last_run()
- _last_start = now
if last is not None and now - last < RUN_EVERY:
+ # Due RUN_EVERY after the last run, not after this process
+ # started: a restart (every deploy) must not push it back.
+ _last_start = last
return False
+ _last_start = now
from openexecutive.audit.context import unscoped_audit_rows
with unscoped_audit_rows():
diff --git a/packages/core/tests/unit/test_contact_keeper.py b/packages/core/tests/unit/test_contact_keeper.py
index fa8269c9..840bf4b0 100644
--- a/packages/core/tests/unit/test_contact_keeper.py
+++ b/packages/core/tests/unit/test_contact_keeper.py
@@ -964,3 +964,28 @@ def test_undo_never_changes_a_team_members_name(owner: int) -> None:
)
changes.undo(change_id)
assert people_store.get_person(pid).full_name == "Ben Teammate"
+
+
+def test_a_restart_does_not_push_the_next_run_back(owner: int, monkeypatch: pytest.MonkeyPatch) -> None:
+ started: list[datetime] = []
+
+ async def run(*, now: datetime | None = None, **_kw: Any) -> keeper.RunResult:
+ started.append(now or NOW)
+ return keeper.RunResult("ok")
+
+ async def tick(moment: datetime) -> bool:
+ ran = keeper.maybe_run(moment)
+ if keeper._task is not None:
+ await keeper._task
+ return ran
+
+ monkeypatch.setattr(keeper, "run_once", run)
+ monkeypatch.setattr(keeper, "_task", None)
+ # A fresh process, two hours after the last run.
+ monkeypatch.setattr(keeper, "_last_start", None)
+ changes.note_run("ok", NOW)
+ restart = NOW + timedelta(hours=2)
+ assert asyncio.run(tick(restart)) is False
+ # Due six hours after the last run, not six after the restart.
+ assert asyncio.run(tick(NOW + keeper.RUN_EVERY + timedelta(minutes=1))) is True
+ assert started == [NOW + keeper.RUN_EVERY + timedelta(minutes=1)]