Skip to content

The agent that grows with you

fix(onboarding): the due cache never keeps an answer older than its files

alt-glitchf684a2bbe5fd
diff --git a/hermes_cli/onboarding_due.py b/hermes_cli/onboarding_due.py
index 2aa0d6596a9..bd23340f10a 100644
--- a/hermes_cli/onboarding_due.py
+++ b/hermes_cli/onboarding_due.py
@@ -7,8 +7,10 @@ time for a boot computation still in flight. Every writer that can move the answ
:func:`invalidate`: the root ``onboarding.run`` (``onboarding_run.set_run``), the OS user's marker
(``install_state``) and the setup record (``free_tier_bootstrap``). The next reader then computes
live once and caches that. A *stamp* the caller passes (the files the answer reads) catches edits
-no writer in this process saw, such as a key typed into ``.env`` by hand. The computation itself
-lives with its caller (``tui_gateway``), which owns the default-profile scope it runs in.
+no writer in this process saw, such as a key typed into ``.env`` by hand. It is taken before and
+after each computation, and an answer is cached only when the files held still in between: one
+computed while a key landed would otherwise sit under the new stamp. The computation itself lives
+with its caller (``tui_gateway``), which owns the default-profile scope it runs in.
"""
from __future__ import annotations
@@ -18,6 +20,8 @@ from collections.abc import Callable, Hashable
# The desktop's first question waits this long for the boot computation before answering live.
BOOT_WAIT_SECONDS = 8.0
+# The boot's first computation can write a config skeleton itself, so its files move once.
+_BOOT_ATTEMPTS = 2
Due = dict[str, bool]
Stamp = Callable[[], Hashable]
@@ -32,7 +36,7 @@ _boot_done: threading.Event | None = None
def begin_boot(stamp: Stamp = lambda: None) -> Callable[[Callable[[], Due]], None]:
"""Mark a boot computation as in flight (readers wait for it from now on) and return the call
that finishes it: ``finish(compute)`` runs *compute* and caches its answer, unless a writer
- invalidated meanwhile."""
+ invalidated meanwhile or the files never held still."""
global _boot_done
done = threading.Event()
with _lock:
@@ -40,10 +44,13 @@ def begin_boot(stamp: Stamp = lambda: None) -> Callable[[Callable[[], Due]], Non
def finish(compute: Callable[[], Due]) -> None:
try:
- with _lock:
- generation = _generation
- value = compute()
- _store(value, generation, stamp()) # after: computing can itself write a config skeleton
+ for _ in range(_BOOT_ATTEMPTS):
+ with _lock:
+ generation = _generation
+ seen = stamp()
+ value = compute()
+ if _store(value, generation, seen, stamp()):
+ return
finally:
done.set()
@@ -62,7 +69,7 @@ def read(compute: Callable[[], Due], *, stamp: Stamp = lambda: None, wait: float
return dict(_value)
generation = _generation
value = compute()
- _store(value, generation, stamp())
+ _store(value, generation, seen, stamp())
return dict(value)
@@ -80,8 +87,13 @@ def reset_for_tests() -> None:
_boot_done = None
-def _store(value: Due, generation: int, seen: Hashable) -> None:
+def _store(value: Due, generation: int, before: Hashable, after: Hashable) -> bool:
+ """Cache *value*, computed between the stamps *before* and *after*; True when it was cached."""
global _value, _value_stamp
+ if before != after: # a file moved mid-computation: the answer may predate it
+ return False
with _lock:
- if generation == _generation: # a writer that landed meanwhile wins over this computation
- _value, _value_stamp = dict(value), seen
+ if generation != _generation: # a writer that landed meanwhile wins over this computation
+ return False
+ _value, _value_stamp = dict(value), after
+ return True
diff --git a/tests/hermes_cli/test_onboarding_due.py b/tests/hermes_cli/test_onboarding_due.py
index cd3f219510c..a7e51a8c352 100644
--- a/tests/hermes_cli/test_onboarding_due.py
+++ b/tests/hermes_cli/test_onboarding_due.py
@@ -37,3 +37,23 @@ def test_a_write_during_the_boot_computation_is_not_lost():
finish(compute)
assert onboarding_due.read(lambda: {"run": False, "eligible": True}) == {"run": False, "eligible": True}
+
+
[email protected]("path", ["boot", "read"])
+def test_a_key_saved_during_the_computation_is_not_hidden_by_the_cache(path):
+ env = {"key": False}
+
+ def compute():
+ answer = {"run": True, "eligible": env["key"]} # the provider check has read the files
+ env["key"] = True # then another process saves a key
+ return answer
+
+ def stamp():
+ return env["key"]
+
+ if path == "boot":
+ onboarding_due.begin_boot(stamp)(compute)
+ else:
+ onboarding_due.read(compute, stamp=stamp)
+
+ assert onboarding_due.read(lambda: {"run": True, "eligible": env["key"]}, stamp=stamp)["eligible"] is True