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.

fix(wiki): never let a failing companion topic page block the primary fact

Ruben Lütke30e58560f75c
diff --git a/jarvis/memory/wiki/consolidator.py b/jarvis/memory/wiki/consolidator.py
index 9e03d3cc8e..95f9fc86f6 100644
--- a/jarvis/memory/wiki/consolidator.py
+++ b/jarvis/memory/wiki/consolidator.py
@@ -468,6 +468,11 @@ class Consolidator:
# decision); counted only after the write actually lands.
secondary_invalidations: list[str] = []
secondary_adds: list[str] = []
+ # Companion topic pages (graph-visibility secondary "add"s) are
+ # written in their OWN non-atomic call, never bundled into the
+ # primary's all-or-nothing transaction: a failing bonus page must
+ # not block the durable primary fact.
+ companion_by_candidate: dict[int, list[PageUpdate]] = {}
# Never submit two independently generated full-page bodies for the
# same target in one AtomicWriter call: the later body was based on the
@@ -542,19 +547,22 @@ class Consolidator:
write_plan[cid] = (None, None)
continue
new_body = self._with_source_marker(new_body, by_id[cid])
- updates_by_candidate.setdefault(cid, []).append(
- PageUpdate(
- target_path=Path(target),
- operation="create" if decision == "add" else "update",
- new_body=new_body,
- reason=str(item.get("reason", ""))[:200],
- )
+ update = PageUpdate(
+ target_path=Path(target),
+ operation="create" if decision == "add" else "update",
+ new_body=new_body,
+ reason=str(item.get("reason", ""))[:200],
)
if is_secondary:
+ companion_by_candidate.setdefault(cid, []).append(update)
secondary_adds.append(target)
else:
+ updates_by_candidate.setdefault(cid, []).append(update)
write_plan[cid] = (decision, target)
- required_targets.setdefault(cid, set()).add(target)
+ # The candidate's journal verdict tracks ONLY its primary
+ # (and invalidation) writes — a companion page failure
+ # must not reject or stall the primary fact.
+ required_targets.setdefault(cid, set()).add(target)
else: # invalidate
superseded_by = str(item.get("superseded_by", "") or "").strip()
invalidated = self._build_invalidation(target, superseded_by)
@@ -573,12 +581,48 @@ class Consolidator:
applied_rel: set[str] = set()
rejected_rel: set[str] = set()
recent_rel: set[str] = set()
- for cid in sorted(updates_by_candidate, key=row_order.__getitem__):
+ write_cids = set(updates_by_candidate) | set(companion_by_candidate)
+ for cid in sorted(write_cids, key=row_order.__getitem__):
+ # Companion topic pages land FIRST, in their own non-atomic call:
+ # the primary body's [[link]] to them then resolves from disk,
+ # and a failing companion can never hold the durable primary
+ # fact hostage — it just logs, and the demoted link heals via
+ # promotion once a later batch creates the page.
+ companions = companion_by_candidate.get(cid)
+ if companions:
+ companion_result = await self._curator.apply_external_updates(
+ companions,
+ source_label=f"{label}:candidate:{cid}:companion",
+ verb="merge",
+ all_or_nothing=False,
+ )
+ applied_rel.update(
+ self._rel(p) for p in companion_result.applied
+ )
+ failed_companions = [
+ self._rel(p)
+ for p in (
+ *companion_result.blocked_pii,
+ *companion_result.failed_validation,
+ *companion_result.skipped_due_to_recent_edit,
+ )
+ ]
+ if failed_companions:
+ log.warning(
+ "Consolidator: companion topic page(s) %s for "
+ "candidate %d did not land — the primary decision "
+ "proceeds without them",
+ ", ".join(failed_companions),
+ cid,
+ )
+ updates = updates_by_candidate.get(cid)
+ if not updates:
+ continue
# A contradiction can create/update one page and invalidate a
# second. Those writes are one candidate-level transaction: a
# validation failure or edit lock on either page rolls back both.
result = await self._curator.apply_external_updates(
- updates_by_candidate[cid],
+ updates,
source_label=f"{label}:candidate:{cid}",
verb="merge",
all_or_nothing=True,
diff --git a/tests/unit/memory/wiki/test_consolidator.py b/tests/unit/memory/wiki/test_consolidator.py
index 9d681b4db2..cdc7ba0914 100644
--- a/tests/unit/memory/wiki/test_consolidator.py
+++ b/tests/unit/memory/wiki/test_consolidator.py
@@ -430,12 +430,64 @@ async def test_companion_add_creates_topic_page_beside_profile_update(stack) ->
profile = (vault_root / "entities" / "ruben.md").read_text(encoding="utf-8")
assert "- Enjoys great coffee." in profile # existing fact survived
assert "- Pursuing a high-end espresso machine for the kitchen." in profile
- # The cross-link survives demotion because the target page is created
- # in the SAME batch.
+ # The cross-link survives demotion because the companion page lands in
+ # its own call BEFORE the profile update, so the link resolves from disk.
assert "[[projects/espresso-machine]]" in profile
assert journal.pending() == []
+async def test_failing_companion_add_never_blocks_the_primary_fact(stack) -> None:
+ """The companion topic page is a bonus, not a hostage-taker: when it is
+ refused (here: secret guard), the primary profile update still lands and
+ the candidate is closed out as consolidated."""
+ vault_root, _curator, journal = stack
+ _write_aged(vault_root / "entities" / "ruben.md", RUBEN_FULL_BODY)
+ journal.append(
+ [CandidateFact(
+ fact="The user is pursuing a high-end espresso machine for the kitchen.",
+ kind="preference", subjects=("ruben", "espresso-machine"),
+ )],
+ source_label="realtime-aggressive:3", turn_hash="h-espresso-3",
+ )
+ cid = journal.pending()[0].id
+
+ updated_profile = RUBEN_FULL_BODY.replace(
+ "- Enjoys great coffee.\n",
+ "- Enjoys great coffee.\n"
+ "- Pursuing a high-end espresso machine for the kitchen.\n",
+ )
+ poisoned_companion = _espresso_project_body().replace(
+ "Researching options.",
+ "Researching options via sk-proj-" + "A" * 24 + " lookups.",
+ )
+ brain = FakeBrain([_judge_json([
+ {
+ "candidate_id": cid,
+ "decision": "update",
+ "target": "entities/ruben.md",
+ "new_body": updated_profile,
+ "reason": "profile note",
+ },
+ {
+ "candidate_id": cid,
+ "decision": "add",
+ "target": "projects/espresso-machine.md",
+ "new_body": poisoned_companion,
+ "reason": "companion topic page",
+ },
+ ])])
+ consolidator = _consolidator(stack, brain)
+
+ label = await consolidator.run_once()
+
+ assert label == "journal-batch:1"
+ assert not (vault_root / "projects" / "espresso-machine.md").exists()
+ profile = (vault_root / "entities" / "ruben.md").read_text(encoding="utf-8")
+ assert "- Pursuing a high-end espresso machine for the kitchen." in profile
+ assert journal.pending() == [] # consolidated despite the refused companion
+
+
@pytest.mark.asyncio
async def test_secondary_update_is_still_rejected(stack) -> None:
"""Only "add" and "invalidate" may ride as secondary actions — a