Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

test(brain): prove wiki confirm-after-write ordering + wire failure reason

Ruben Lütke19d4a8890497
diff --git a/jarvis/brain/manager.py b/jarvis/brain/manager.py
index 24fefb9d8c..8d4f4aa935 100644
--- a/jarvis/brain/manager.py
+++ b/jarvis/brain/manager.py
@@ -5577,6 +5577,33 @@ class BrainManager:
in_progress=True,
)
+ @staticmethod
+ def _short_wiki_failure_reason(diag: str | None) -> str:
+ """Distil a wiki-ingest failure diagnostic into a short spoken cause.
+
+ Keeps only the first line/sentence, strips a bare ``exit N``-style
+ opaque token (a raw exit code must never be spoken — mirror of
+ ``cu_failure_readback``'s guard), and caps the length so the failure
+ phrase stays one short clause. Returns ``""`` when nothing
+ presentable remains, in which case the caller falls back to the
+ reason-less ``wiki_save_failed`` phrase.
+ """
+ raw = (diag or "").strip()
+ if not raw:
+ return ""
+ # First line, then the first sentence within that line.
+ first_line = raw.splitlines()[0].strip()
+ m = re.match(r"^(.*?[.!?])(?:\s|$)", first_line)
+ reason = (m.group(1) if m else first_line).strip()
+ # Strip a bare "exit N" opaque token (optionally bracketed) — never
+ # speak a raw exit code.
+ reason = re.sub(
+ r"\(?\s*exit\s*\d+\s*\)?", "", reason, flags=re.IGNORECASE
+ ).strip(" .,:;-")
+ if len(reason) > 80:
+ reason = reason[:80].rstrip()
+ return reason
+
async def _run_wiki_ingest_background(
self,
*,
@@ -5627,8 +5654,17 @@ class BrainManager:
latency_budget_ms=2500,
)
else:
- diag = str(getattr(result, "error", "") or "unknown")
- canned_fail = action_phrase("wiki_save_failed", lang)
+ err = str(getattr(result, "error", "") or "").strip()
+ diag = err or "unknown"
+ # Keyless (canned) failure path is honest-with-cause: surface a
+ # short, speakable reason (mirrors wiki_saved_detail on success).
+ # The composer still gets the FULL diag via facts below.
+ short_reason = self._short_wiki_failure_reason(err)
+ canned_fail = (
+ action_phrase("wiki_save_failed_reason", lang, reason=short_reason)
+ if short_reason
+ else action_phrase("wiki_save_failed", lang)
+ )
out = await render_readback(
getattr(self, "_readback_composer", None),
instruction=(
diff --git a/tests/unit/brain/test_wiki_fast_path.py b/tests/unit/brain/test_wiki_fast_path.py
index abfa0fa5c2..58c34f52ff 100644
--- a/tests/unit/brain/test_wiki_fast_path.py
+++ b/tests/unit/brain/test_wiki_fast_path.py
@@ -2,7 +2,14 @@
the write (spec A1-A3). Uses constructor-injected fakes per house style —
build a minimal BrainManager the same way test_routing.py does (copy its
manager fixture) and register a recording fake executor + a fake
-wiki-ingest tool."""
+wiki-ingest tool.
+
+The confirm-after-write contract is proven by a SHARED timeline list that
+BOTH the fake executor and the recording bus append to: the executor writes
+``"execute:done"`` when ``execute()`` returns; the bus writes ``"announce"``
+when it publishes an ``AnnouncementRequested``. A success turn must have
+``execute:done`` strictly before ``announce`` — i.e. the completion is spoken
+only after the write actually happened."""
from __future__ import annotations
import asyncio
@@ -27,33 +34,44 @@ class FakeWikiIngestTool:
class RecordingExecutor:
- def __init__(self) -> None:
- self.order: list[str] = []
+ """Fake ToolExecutor that stamps the shared timeline on completion.
+
+ Appends ``"execute:done"`` to ``timeline`` at the instant ``execute()``
+ returns the tool result — i.e. the moment the write is finished.
+ """
+
+ def __init__(self, timeline: list[str]) -> None:
+ self.timeline = timeline
async def execute(self, tool, args, **kwargs):
- self.order.append("execute:start")
if getattr(tool, "delay_s", 0):
await asyncio.sleep(tool.delay_s)
tool.calls.append(dict(args))
- self.order.append("execute:done")
+ self.timeline.append("execute:done")
return tool.result
class RecordingBus(EventBus):
- """EventBus that also keeps a flat list of everything published.
-
- Mirrors the ``_FakeBus``/``bus.published`` shape used by the sibling
- Computer-Use offload tests (tests/unit/brain/test_computer_use_offload.py)
- while staying a REAL EventBus so BrainManager's constructor/attach path
- behaves exactly as in production.
+ """Real EventBus that also records publishes onto the shared timeline.
+
+ Keeps a flat ``published`` list (mirrors the ``_FakeBus``/``bus.published``
+ shape used by tests/unit/brain/test_computer_use_offload.py) AND appends
+ ``"announce"`` to the shared ``timeline`` whenever an
+ ``AnnouncementRequested`` is published, so the confirm-after-write ordering
+ is directly assertable against the executor's ``execute:done`` stamp.
+ Stays a REAL EventBus so BrainManager's constructor/attach path behaves
+ exactly as in production.
"""
- def __init__(self) -> None:
+ def __init__(self, timeline: list[str]) -> None:
super().__init__()
+ self.timeline = timeline
self.published: list[Any] = []
async def publish(self, event: Any) -> None: # noqa: ANN401
self.published.append(event)
+ if type(event).__name__ == "AnnouncementRequested":
+ self.timeline.append("announce")
await super().publish(event)
@@ -67,6 +85,10 @@ def manager_factory():
``render_readback`` always returns the deterministic canned phrase — no
LLM call, no network).
+ The executor and the bus share ONE ``timeline`` list (exposed as
+ ``bus.timeline`` / ``executor.timeline``, the same object) so a test can
+ prove the completion announcement fires strictly after the write.
+
Seeds one plausible prior user/assistant exchange into ``_history`` so an
ANAPHORIC "write THAT to the wiki" command (spec A1) has real content to
source from — mirroring production, where such a command is never
@@ -74,8 +96,9 @@ def manager_factory():
"""
def _factory(*, tools: dict[str, Any]) -> tuple[BrainManager, RecordingExecutor, RecordingBus]:
- bus = RecordingBus()
- executor = RecordingExecutor()
+ timeline: list[str] = []
+ bus = RecordingBus(timeline)
+ executor = RecordingExecutor(timeline)
mgr = BrainManager(
config=JarvisConfig(),
bus=bus,
@@ -109,7 +132,8 @@ async def test_explicit_command_ingests_and_announces_after_write(manager_factor
ToolResult(
success=True,
output="Wiki ingest done:\n- applied: 1\nPages touched:\n - joy.md",
- )
+ ),
+ delay_s=0.02, # a real gap between "write" and "announce" to order against
)
mgr, executor, bus = manager_factory(tools={"wiki-ingest": tool})
reply = await mgr._run_wiki_ingest_fast_path(
@@ -120,7 +144,14 @@ async def test_explicit_command_ingests_and_announces_after_write(manager_factor
assert tool.calls, "wiki-ingest must be invoked through the executor"
completed = [e for e in bus.published if type(e).__name__ == "AnnouncementRequested"]
assert completed, "outcome must be announced (zero silent drops)"
- assert executor.order.index("execute:done") < len(executor.order)
+ # Confirm-after-write: the completion announcement is stamped on the SHARED
+ # timeline strictly AFTER the executor finished the write.
+ assert "execute:done" in bus.timeline, "the write must be recorded on the timeline"
+ assert "announce" in bus.timeline, "the announcement must be recorded on the timeline"
+ assert bus.timeline.index("execute:done") < bus.timeline.index("announce"), (
+ f"confirm-after-write violated: announce must come after the write; "
+ f"timeline={bus.timeline}"
+ )
async def test_failure_is_announced_honestly_never_as_success(manager_factory):
@@ -138,6 +169,63 @@ async def test_failure_is_announced_honestly_never_as_success(manager_factory):
assert any(k in text for k in ("not work", "nicht geklappt", "no se pudo")) # i18n-allow
+async def test_failure_announcement_carries_the_reason(manager_factory):
+ """The keyless failure path is honest-with-cause: a real tool error is
+ distilled into the spoken failure phrase (wiki_save_failed_reason), and the
+ bare success phrase is NEVER emitted on failure."""
+ tool = FakeWikiIngestTool(
+ ToolResult(success=False, output="", error="wiki integration not bootstrapped")
+ )
+ mgr, executor, bus = manager_factory(tools={"wiki-ingest": tool})
+ reply = await mgr._run_wiki_ingest_fast_path("write that to the wiki")
+ assert reply is not None
+ await asyncio.sleep(0.05)
+ completed = [e for e in bus.published if type(e).__name__ == "AnnouncementRequested"]
+ assert completed
+ text = completed[-1].text
+ # Never a success phrase on failure (any supported language).
+ assert text.strip().lower() not in {
+ "saved to the wiki.",
+ "im wiki gespeichert.", # i18n-allow
+ "guardado en la wiki.", # i18n-allow
+ }
+ assert "not work" in text.lower() # honest failure phrasing (en)
+ assert "wiki integration not bootstrapped" in text # the cause is carried through
+
+
+async def test_bare_exit_code_reason_is_not_spoken(manager_factory):
+ """An opaque 'exit N'-only error leaves no presentable reason, so the
+ failure degrades to the reason-less phrase — a raw exit code is never
+ spoken (mirror of cu_failure_readback's guard)."""
+ tool = FakeWikiIngestTool(ToolResult(success=False, output="", error="exit 5"))
+ mgr, executor, bus = manager_factory(tools={"wiki-ingest": tool})
+ await mgr._run_wiki_ingest_fast_path("write that to the wiki")
+ await asyncio.sleep(0.05)
+ completed = [e for e in bus.published if type(e).__name__ == "AnnouncementRequested"]
+ assert completed
+ text = completed[-1].text
+ assert "exit 5" not in text.lower()
+ assert "exit" not in text.lower()
+ assert text == "Saving to the wiki did not work." # the reason-less phrase
+
+
+async def test_anaphoric_command_without_usable_content_asks_what_to_write(manager_factory):
+ """An anaphoric 'write that to the wiki' with no usable last exchange must
+ ask what to write — the wiki_nothing_to_save ack — with NO executor call
+ and NO completion announcement (spec A1 content gap)."""
+ tool = FakeWikiIngestTool(ToolResult(success=True, output=""))
+ mgr, executor, bus = manager_factory(tools={"wiki-ingest": tool})
+ mgr._history.clear() # no prior exchange to source the anaphor from
+ reply = await mgr._run_wiki_ingest_fast_path("write that to the wiki")
+ assert reply is not None
+ assert "wiki" in reply.lower()
+ await asyncio.sleep(0.05)
+ assert not tool.calls, "the absent-content path must not call the executor"
+ assert not [
+ e for e in bus.published if type(e).__name__ == "AnnouncementRequested"
+ ], "no write happened, so nothing is announced"
+
+
async def test_non_wiki_turn_returns_none(manager_factory):
tool = FakeWikiIngestTool(ToolResult(success=True, output=""))
mgr, executor, bus = manager_factory(tools={"wiki-ingest": tool})