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.

chore: auto-save WIP via Claude stop-hook safety net (2026-06-30 17:03)

Ruben Lütkebdbdd7ab8bb7
diff --git a/jarvis/brain/contact_intent.py b/jarvis/brain/contact_intent.py
new file mode 100644
index 0000000000..ed8dc492cf
--- /dev/null
+++ b/jarvis/brain/contact_intent.py
@@ -0,0 +1,115 @@
+"""Contact write-intent detection — input side of the say-do honesty guard.
+
+Why this exists
+---------------
+Live voice session (2026-06-30): Jarvis offered "Soll ich die anlegen?", the
+user confirmed "ja, legt die mal an … die Mailadresse von Harald ist …", and
+Jarvis answered "Okay, sehr gut" — but never called the ``contact-upsert`` tool,
+so the address book stayed empty. The capability was fully wired; the gap was at
+the brain decision layer (it acknowledged in words instead of acting).
+
+This module mirrors the read-side evidence gate (``evidence_gate.py``): it spots,
+deterministically, that a turn wants to SAVE a person, so the manager can mandate
+``contact-upsert`` for the turn and the existing unverified-answer backstop can
+replace a fake "okay" with an honest line. Pure regex + a string normalizer —
+NO LLM, NO IO (AP-9/AP-11).
+
+Precision over recall by mandate
+--------------------------------
+Falsely "correcting" the user is a hard anti-pattern here (the maintainer turned
+the generic clarify question OFF for exactly that reason). So the detector fires
+only on high-confidence signals — a genitive contact detail ("Christophs Nummer
+ist …") alone, or a save verb corroborated by a contact noun / dictated detail.
+A bare "die Nummer ist falsch" or "schick Harald eine Mail" must NOT fire.
+"""
+from __future__ import annotations
+
+import re
+
+from jarvis.core.capabilities import _normalize
+
+# A genitive contact detail ("Christophs Nummer ist …", "Haralds Mail ist …").
+# High precision on its own: a possessive name + a contact field + "ist/lautet"
+# is almost always "save this person's detail". The non-genitive "<field> von X
+# ist" form is intentionally NOT here (it also matches "die Adresse von Berlin
+# ist zentral") — it only counts as a corroborating ``_DETAIL_RE`` signal.
+_GENITIVE_DETAIL_RE = re.compile(
+ r"\b\w+s\s+(mail|e-?mail|mailadresse|emailadresse|nummer|telefonnummer|"
+ r"handynummer|telefon|adresse|anschrift|number|phone|address)\s+"
+ r"(ist|lautet|is)\b"
+)
+
+# Any dictated contact field "<field> [von X] ist/lautet …" — corroborating only.
+_DETAIL_RE = re.compile(
+ r"\b(mail|e-?mail|mailadresse|emailadresse|nummer|telefonnummer|"
+ r"handynummer|telefon|adresse|anschrift|number|phone|address)\b"
+ r"(\s+von\s+\w+)?\s+(ist|lautet|is)\b"
+)
+
+# Imperative/intent to save, create, add, remember, enter a contact.
+_SAVE_VERB_RE = re.compile(
+ r"\b("
+ r"merk(e|t)?\s+dir" # merk dir / merke dir
+ r"|notier\w*" # notiere / notier dir
+ r"|speicher\w*" # speichere / speichern / speicher
+ r"|anleg\w*" # anlegen
+ r"|leg\w*\b[^?]{0,40}\ban\b" # leg/legt … an (separable)
+ r"|eintrag\w*" # eintragen
+ r"|trag\w*\b[^?]{0,40}\bein\b" # trag … ein (separable)
+ r"|hinzufueg\w*" # hinzufuegen
+ r"|fueg\w*\b[^?]{0,40}\bhinzu\b" # fueg … hinzu (separable)
+ r"|nimm\b[^?]{0,40}\bauf\b" # nimm … auf
+ r"|save|remember|store\b|note\s+down|add\b|create\b"
+ r"|guard\w*|anota\w*|agrega\w*|anade\w*" # ES guardar/anotar/agregar/anadir
+ r")"
+)
+
+# A contact container noun.
+_CONTACT_NOUN_RE = re.compile(
+ r"\b(kontakt|kontakte|kontakten|kontakts|adressbuch|adress-?buch"
+ r"|contact|contacts|contacto|contactos)\b"
+)
+
+# Per-turn directive injected into the system prompt when a write intent is
+# detected (English — LLM-facing, mirrors the read evidence gate's directive).
+CONTACT_WRITE_DIRECTIVE = (
+ "MANDATORY THIS TURN: the user wants to save or update a person in their "
+ "contact book. You MUST call the `contact-upsert` tool to actually store "
+ "it — one call per person — not just acknowledge it in words. Pass the "
+ "name plus every detail the user gave (email, phone, relationship, "
+ "address, note) as the tool arguments. If a required detail is clearly "
+ "incomplete or malformed (for example an email address with no '@'), do "
+ "NOT save the broken value: ask the user one short question to repeat that "
+ "detail before saving. Never tell the user a contact was saved unless the "
+ "`contact-upsert` tool actually ran this turn."
+)
+
+
+def detect_contact_write_intent(utterance: str) -> bool:
+ """True when the turn is a high-confidence request to save/update a contact.
+
+ Deterministic, conservative (precision over recall): a false positive would
+ wrongly tell the user "I haven't saved that yet", which the anti-correction
+ mandate forbids. The detector is the INPUT trigger; the actual honesty is
+ enforced by the manager's unverified-answer backstop only if the mandated
+ tool then does not run.
+ """
+ t = _normalize(utterance or "")
+ if not t.strip():
+ return False
+ # High-precision genitive detail may fire on its own.
+ if _GENITIVE_DETAIL_RE.search(t):
+ return True
+ save = bool(_SAVE_VERB_RE.search(t))
+ noun = bool(_CONTACT_NOUN_RE.search(t))
+ detail = bool(_DETAIL_RE.search(t))
+ # A save verb needs one corroborating contact signal; a contact noun plus a
+ # dictated detail is also enough. A lone signal never fires.
+ if save and (noun or detail):
+ return True
+ if noun and detail:
+ return True
+ return False
+
+
+__all__ = ["detect_contact_write_intent", "CONTACT_WRITE_DIRECTIVE"]
diff --git a/jarvis/brain/manager.py b/jarvis/brain/manager.py
index 72a1e58ac4..ecce41c0ce 100644
--- a/jarvis/brain/manager.py
+++ b/jarvis/brain/manager.py
@@ -1138,6 +1138,72 @@ def _evidence_unfulfilled_answer(*, lang: str) -> str:
)
+# Honest spoken fallback for a mandated WRITE (e.g. contact-upsert) that never
+# ran — the say-do gap (live voice session 2026-06-30: "Okay, sehr gut" spoken
+# while no contact was saved). Distinct from the READ fallback above: it speaks
+# to a write ("not saved yet"), not a failed lookup. Static, no LLM (AP-11);
+# localized for every supported language, unknown code → default locale.
+_ACTION_UNFULFILLED_PHRASES: dict[str, dict[str, str]] = {
+ "contact-upsert": {
+ "de": (
+ "Ich hab den Kontakt noch nicht gespeichert — sag mir die Angaben " # i18n-allow: German TTS
+ "noch mal, dann lege ich ihn an." # i18n-allow: German TTS
+ ),
+ "en": (
+ "I haven't actually saved that contact yet — give me the details "
+ "once more and I'll add it."
+ ),
+ "es": (
+ "Todavía no he guardado ese contacto — dime los datos otra vez y lo "
+ "añado."
+ ),
+ },
+}
+# A write tool with no bespoke table reuses the contact wording's shape.
+_ACTION_UNFULFILLED_DEFAULT = _ACTION_UNFULFILLED_PHRASES["contact-upsert"]
+
+
+def _action_unfulfilled_answer(required_tool: str, *, lang: str) -> str:
+ """Honest spoken fallback for a mandated WRITE tool that never ran.
+
+ Mirrors :func:`_evidence_unfulfilled_answer` but for a write/create action:
+ it must never claim the save happened. Unknown tool or language degrades to
+ the contact wording / default locale so the spoken turn never crashes.
+ """
+ table = _ACTION_UNFULFILLED_PHRASES.get(required_tool, _ACTION_UNFULFILLED_DEFAULT)
+ return table.get(lang, table.get(DEFAULT_LOCALE, table["de"]))
+
+
+def _unfulfilled_replacement(
+ *,
+ required_tool: str,
+ executed: "set[str]",
+ response_text: str,
+ suppressed: bool,
+ is_write: bool,
+ lang: str,
+) -> "str | None":
+ """Decide whether a mandated-tool turn's answer must be replaced for honesty.
+
+ Returns the honest replacement text, or ``None`` to keep the answer as-is.
+ Shared by the read evidence gate and the write (contact) say-do guard.
+
+ A WRITE mandate additionally LEAVES A CLARIFYING QUESTION intact: asking the
+ user to repeat a missing/broken field (e.g. the '@'-less email) is the
+ desired behavior, not a fake claim — only a flat confirmation with no
+ question is corrected.
+ """
+ if not _evidence_answer_is_unverified(
+ required_tool, executed, response_text, suppressed=suppressed
+ ):
+ return None
+ if is_write:
+ if "?" in (response_text or ""):
+ return None # honest clarifying question — keep it
+ return _action_unfulfilled_answer(required_tool, lang=lang)
+ return _evidence_unfulfilled_answer(lang=lang)
+
+
def _render_recovered_tool_output(output: Any) -> str:
"""Speakable plain-text rendering of a recovered tool's output.
diff --git a/tests/unit/brain/test_action_say_do_guard.py b/tests/unit/brain/test_action_say_do_guard.py
new file mode 100644
index 0000000000..1956f7dccc
--- /dev/null
+++ b/tests/unit/brain/test_action_say_do_guard.py
@@ -0,0 +1,113 @@
+"""Say-do honesty backstop for WRITE mandates (contacts).
+
+Sibling of ``test_evidence_enforcement.py`` (which covers READ mandates). When a
+contact-write turn mandated ``contact-upsert`` but the tool never ran, a flat
+confirmation ("Okay, sehr gut") is a dishonest say-do gap and must be replaced
+with an honest line. A clarifying QUESTION ("Wie lautet die volle E-Mail?") is
+the DESIRED behavior for a missing/broken field and must be left intact.
+"""
+from jarvis.brain.manager import (
+ _action_unfulfilled_answer,
+ _unfulfilled_replacement,
+)
+
+
+# --- honest write fallback wording -------------------------------------------
+
+
+def test_action_unfulfilled_answer_is_honest_and_localized():
+ de = _action_unfulfilled_answer("contact-upsert", lang="de")
+ en = _action_unfulfilled_answer("contact-upsert", lang="en")
+ es = _action_unfulfilled_answer("contact-upsert", lang="es")
+ # Never claims the save happened.
+ for txt in (de, en, es):
+ assert txt.strip()
+ assert "gespeichert habe ich" not in txt.lower()
+ assert "noch nicht" in de.lower() # "not yet saved"
+ assert "haven't" in en.lower() or "not" in en.lower()
+ assert "todav" in es.lower() or "aún" in es.lower() or "no he" in es.lower()
+
+
+def test_action_unfulfilled_answer_unknown_language_is_safe():
+ assert isinstance(_action_unfulfilled_answer("contact-upsert", lang="fr"), str)
+ assert _action_unfulfilled_answer("contact-upsert", lang="fr").strip()
+
+
+# --- the replacement decision (pure) -----------------------------------------
+
+
+def test_write_mandate_flat_confirmation_is_replaced():
+ # Mandated contact-upsert, tool NOT run, no question → dishonest → replace.
+ out = _unfulfilled_replacement(
+ required_tool="contact-upsert",
+ executed=set(),
+ response_text="Okay, sehr gut.",
+ suppressed=False,
+ is_write=True,
+ lang="de",
+ )
+ assert out is not None
+ assert "okay" not in out.lower() # the fake confirmation is gone
+
+
+def test_write_mandate_clarifying_question_is_kept():
+ # The brain honestly asked for the broken email → leave it alone.
+ out = _unfulfilled_replacement(
+ required_tool="contact-upsert",
+ executed=set(),
+ response_text="Wie lautet Haralds vollständige E-Mail-Adresse?",
+ suppressed=False,
+ is_write=True,
+ lang="de",
+ )
+ assert out is None
+
+
+def test_write_mandate_with_executed_tool_stands():
+ out = _unfulfilled_replacement(
+ required_tool="contact-upsert",
+ executed={"contact-upsert"},
+ response_text="Hab ich gespeichert.",
+ suppressed=False,
+ is_write=True,
+ lang="de",
+ )
+ assert out is None
+
+
+def test_read_mandate_still_uses_read_wording():
+ # is_write=False must behave exactly like the legacy evidence enforcement.
+ out = _unfulfilled_replacement(
+ required_tool="cli_gcloud",
+ executed=set(),
+ response_text="the gcloud tool blocked execution",
+ suppressed=False,
+ is_write=False,
+ lang="de",
+ )
+ assert out is not None
+ assert "kontakt" not in out.lower() # read fallback, not the contact line
+
+
+def test_no_mandate_never_replaces():
+ out = _unfulfilled_replacement(
+ required_tool="",
+ executed=set(),
+ response_text="any free answer",
+ suppressed=False,
+ is_write=False,
+ lang="de",
+ )
+ assert out is None
+
+
+def test_suppressed_turn_never_replaces():
+ out = _unfulfilled_replacement(
+ required_tool="contact-upsert",
+ executed=set(),
+ response_text="",
+ suppressed=True,
+ is_write=True,
+ lang="de",
+ )
+ assert out is None
diff --git a/tests/unit/brain/test_contact_intent.py b/tests/unit/brain/test_contact_intent.py
new file mode 100644
index 0000000000..71b8a299e8
--- /dev/null
+++ b/tests/unit/brain/test_contact_intent.py
@@ -0,0 +1,81 @@
+"""Contact write-intent detection — the input side of the say-do honesty guard.
+
+The recurring failure (voice session 2026-06-30): Jarvis offered "Soll ich die
+anlegen?", the user confirmed "ja, legt die mal an … die Mailadresse von Harald
+ist …", and Jarvis replied "Okay, sehr gut" WITHOUT ever calling the
+``contact-upsert`` tool — the address book stayed empty. The detector below
+mandates the real tool on a contact-write turn so the read-style evidence gate's
+backstop can catch a fake confirmation. Pure regex, no LLM (AP-9/AP-11).
+"""
+from jarvis.brain.contact_intent import (
+ CONTACT_WRITE_DIRECTIVE,
+ detect_contact_write_intent,
+)
+
+# --- positive: the turn really asks to save a person ------------------------
+
+
+def test_reported_transcript_turn_fires():
+ # The exact utterance from the live session (save verb + dictated detail).
+ assert detect_contact_write_intent(
+ "Ähm, ja, legt die mal an. Also die Mailadresse von Harald ist harald.10.de."
+ ) is True
+
+
+def test_genitive_detail_alone_fires():
+ # "Christoph's number is …" is a self-contained save intent.
+ assert detect_contact_write_intent("Merk dir, Christophs Nummer ist 0171 1234567.") is True
+ assert detect_contact_write_intent("Haralds E-Mail ist [email protected]") is True
+
+
+def test_save_verb_plus_contact_noun_fires():
+ assert detect_contact_write_intent("Speichere Laura als Kontakt.") is True
+ assert detect_contact_write_intent("Leg Tom als Kontakt an.") is True
+ assert detect_contact_write_intent("Füge Anna zu meinen Kontakten hinzu.") is True
+
+
+def test_english_and_spanish_fire():
+ assert detect_contact_write_intent("Save Tom as a contact.") is True
+ assert detect_contact_write_intent("Remember, Laura's number is 0151 22334.") is True
+ assert detect_contact_write_intent("Guarda a Laura como contacto.") is True
+
+
+# --- negative: must NOT fire (anti-false-correction is a hard user mandate) ---
+
+
+def test_lookup_questions_do_not_fire():
+ assert detect_contact_write_intent("Was steht heute in meinem Kalender?") is False
+ assert detect_contact_write_intent("Habe ich irgendwelche Kontakte?") is False
+ assert detect_contact_write_intent("Was geht ab?") is False
+ assert detect_contact_write_intent("Wie ist das Wetter?") is False
+
+
+def test_send_or_call_actions_do_not_fire():
+ # Messaging / dialing a person is not saving them.
+ assert detect_contact_write_intent("Schick Harald eine Mail.") is False
+ assert detect_contact_write_intent("Ruf Harald an.") is False
+
+
+def test_bare_detail_statement_does_not_fire():
+ # A non-possessive "the number is wrong" must not be mistaken for a save.
+ assert detect_contact_write_intent("Die Nummer ist falsch.") is False
+ assert detect_contact_write_intent("Harald ist 1976 geboren.") is False
+ assert detect_contact_write_intent("Die Adresse von Berlin ist zentral.") is False
+
+
+def test_empty_input_is_safe():
+ assert detect_contact_write_intent("") is False
+ assert detect_contact_write_intent(" ") is False
+
+
+# --- the per-turn directive ---------------------------------------------------
+
+
+def test_directive_forces_the_real_tool_and_clarifies_bad_fields():
+ d = CONTACT_WRITE_DIRECTIVE
+ assert "contact-upsert" in d
+ assert "MANDATORY" in d
+ # Must instruct asking on a malformed required field (the '@'-less email).
+ assert "@" in d
+ # Must forbid claiming a save that did not run.
+ assert "never" in d.lower() or "not" in d.lower()