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.

15.9 KB
Raw Blame
"""Explicitly saved realtime turns reach real Markdown pages without a manual flush.

Since 2026-09-30 only turns the brain acknowledged saving ("Noted.") are
reviewed; there is no automatic per-turn review and no end-of-call sweep.
"""
from __future__ import annotations

import asyncio
import json
import time
from collections.abc import AsyncIterator
from pathlib import Path
from typing import Any

import pytest

from dodaios.core.bus import EventBus
from dodaios.core.config import (
    BrainConfig,
    BrainProviderConfig,
    JarvisConfig,
    MemoryConfig,
    SchedulerConfig,
    WikiMemoryConfig,
)
from dodaios.core.events import VoiceSessionEnded, VoiceTurnCompleted
from dodaios.core.protocols import BrainDelta, BrainRequest
from dodaios.memory.wiki.atomic_writer import AtomicWriter
from dodaios.memory.wiki.consolidator import Consolidator
from dodaios.memory.wiki.curator import WikiCurator
from dodaios.memory.wiki.curator_llm import WikiCuratorLLM
from dodaios.memory.wiki.extractor import ConversationFactExtractor
from dodaios.memory.wiki.journal import CandidateJournal
from dodaios.memory.wiki.lock import VaultLock
from dodaios.memory.wiki.log_writer import LogWriter
from dodaios.memory.wiki.page import MarkdownPageRepository
from dodaios.memory.wiki.scheduler import CuratorScheduler
from dodaios.memory.wiki.vault_index import VaultIndex
from dodaios.memory.wiki.voice_bridge import VoiceFactBridge


class _ReviewBrain:
    """Script both review stages while keeping the production pipeline real.

    Responses are routed by REQUEST KIND, not by call order: a turn's
    extraction and the judge round its candidates trigger run as
    background tasks, and the write-time alias lookups interleave with
    them, so a strict call-order script would race.
    """

    name = "review-brain"
    context_window = 100_000
    supports_tools = False
    supports_vision = False

    def __init__(self, extractions: list[str], judgements: list[str]) -> None:
        self._extractions = list(extractions)
        self._judgements = list(judgements)
        self.calls = 0
        self.requests: list[BrainRequest] = []
        self.judge_requests: list[BrainRequest] = []
        self.alias_requests: list[BrainRequest] = []

    async def complete(self, request: BrainRequest) -> AsyncIterator[BrainDelta]:
        self.calls += 1
        self.requests.append(request)
        text = "\n".join(
            (
                str(getattr(request, "system", "") or ""),
                *(
                    str(getattr(message, "content", "") or "")
                    for message in request.messages
                ),
            )
        )
        if "You generate SEARCH ALIASES" in text:
            self.alias_requests.append(request)
            payload = "[]"
        elif "Evidence user turn [" in text:
            self.judge_requests.append(request)
            payload = self._judgements.pop(0)
        else:
            payload = self._extractions.pop(0)
        yield BrainDelta(content=payload)
        yield BrainDelta(finish_reason="stop")

    def estimate_cost(self, request: BrainRequest) -> float:  # pragma: no cover
        return 0.0


class _Registry:
    def __init__(self, brain: _ReviewBrain) -> None:
        self._brain = brain

    def available(self) -> set[str]:
        return {"test-provider"}

    def instantiate(self, name: str, **kwargs: Any) -> _ReviewBrain:
        return self._brain


def _entity_page(slug: str, fact: str) -> str:
    title = slug.replace("-", " ").title()
    return (
        "---\n"
        "type: entity\n"
        "entity_kind: person\n"
        f"slug: {slug}\n"
        f"aliases: [{title}]\n"
        "created: 2026-07-12\n"
        "updated: 2026-07-12\n"
        "---\n\n"
        f"# {title}\n\n"
        f"## Summary\n\n{fact}\n\n"
        f"## Facts\n\n- {fact}\n\n"
        "## Relationships\n\n"
        "## Sources\n\n- realtime conversation\n"
    )


def _asset_page(slug: str, fact: str, owner_slug: str) -> str:
    title = slug.replace("-", " ").title()
    owner = owner_slug.replace("-", " ").title()
    return (
        "---\n"
        "type: entity\n"
        "entity_kind: vehicle\n"
        f"slug: {slug}\n"
        f"aliases: [{title}]\n"
        "created: 2026-07-15\n"
        "updated: 2026-07-15\n"
        "---\n\n"
        f"# {title}\n\n"
        f"## Summary\n\n{fact}\n\n"
        f"## Facts\n\n- {fact}\n\n"
        f"## Relationships\n\n- Owned by [[entities/{owner_slug}|{owner}]]\n\n"
        "## Sources\n\n- realtime conversation\n"
    )


def _turn(
    *,
    turn_id: str,
    provider: str,
    text: str,
    assistant_text: str = "Noted, thanks for telling me.",
    session_id: str = "realtime-session",
) -> VoiceTurnCompleted:
    return VoiceTurnCompleted(
        session_id=session_id,
        turn_id=turn_id,
        user_text=text,
        jarvis_text=assistant_text,
        tier="realtime",
        provider=provider,
        model="realtime-test-model",
    )


# Hang guard, not a latency budget: each wait ends as soon as its condition
# holds (about half a second on a desktop), but a loaded Windows CI runner
# took longer than the former 3 s to carry one turn through the extractor,
# judge and writer into the vault.
_HANG_GUARD_S = 60.0


async def _wait_for_page(path: Path, timeout_s: float = _HANG_GUARD_S) -> None:
    deadline = time.monotonic() + timeout_s
    while time.monotonic() < deadline:  # noqa: ASYNC110 -- bounded test polling
        if path.is_file():  # noqa: ASYNC240 -- bounded integration-test probe
            return
        await asyncio.sleep(0.02)
    raise AssertionError(f"reviewed realtime fact never reached {path}")


async def _wait_for_idle(
    brain: _ReviewBrain,
    journal: CandidateJournal,
    *,
    calls: int,
    timeout_s: float = _HANG_GUARD_S,
) -> None:
    deadline = time.monotonic() + timeout_s
    while time.monotonic() < deadline:  # noqa: ASYNC110 -- bounded test polling
        if brain.calls >= calls and journal.backlog_count() == 0:
            return
        await asyncio.sleep(0.02)
    raise AssertionError("realtime wiki review did not become idle")


@pytest.mark.asyncio
async def test_consecutive_realtime_reviews_write_to_selected_vault(
    tmp_path: Path,
) -> None:
    vault_root = tmp_path / "selected-vault" / "Jarvis"
    for directory in ("entities", "concepts", "projects", "sessions", "_archive"):
        (vault_root / directory).mkdir(parents=True)
    (vault_root / "schema.md").write_text("# Schema\n", encoding="utf-8")
    (vault_root / "index.md").write_text("# Index\n", encoding="utf-8")
    (vault_root / "log.md").write_text("# Wiki Log\n", encoding="utf-8")
    (vault_root / "entities" / "example-user.md").write_text(
        _entity_page("example-user", "Example User is the test user."),
        encoding="utf-8",
    )

    config = JarvisConfig(
        brain=BrainConfig(
            primary="test-provider",
            providers={"test-provider": BrainProviderConfig(model="test-model")},
        ),
        memory=MemoryConfig(wiki=WikiMemoryConfig(user_entity_slug="example-user")),
        # This E2E drives one fact per short call and expects each to land
        # in the vault immediately, so it pins per-candidate consolidation
        # explicitly (the SHIPPED default batches 3 per judge run since the
        # 2026-07-28 cost audit).
        wiki_scheduler=SchedulerConfig(consolidate_after_candidates=1),
    )
    assert config.wiki_scheduler.consolidate_after_candidates == 1

    facts = [
        "Example Friend moved to Example City last month.",
        "Example Colleague works at the sample library.",
        "Example User owns a vessel named Sample One.",
        "Example User needs a short break right now.",
        "Example User owns a demo glider.",
    ]
    extractions = [
        json.dumps(
            [
                {
                    "fact": facts[0],
                    "kind": "person",
                    "subjects": ["example-friend"],
                    "evidence_turn_id": "openai-turn",
                }
            ]
        ),
        json.dumps(
            [
                {
                    "fact": facts[1],
                    "kind": "person",
                    "subjects": ["example-colleague"],
                    "evidence_turn_id": "gemini-turn",
                }
            ]
        ),
        json.dumps(
            [
                {
                    "fact": facts[2],
                    "kind": "asset",
                    "subjects": ["sample-one"],
                    "evidence_turn_id": "short-asset-turn",
                }
            ]
        ),
        # Stage 1 may over-capture; Stage 2 remains the binding slop filter.
        json.dumps(
            [
                {
                    "fact": facts[3],
                    "kind": "other",
                    "subjects": ["example-user"],
                    "evidence_turn_id": "transient-turn",
                }
            ]
        ),
        # Hostile Stage 1 copies an assistant guess while citing the valid user
        # turn. Stage 2 sees the user-only evidence and rejects the claim.
        json.dumps(
            [
                {
                    "fact": facts[4],
                    "kind": "asset",
                    "subjects": ["example-user", "demo-glider"],
                    "evidence_turn_id": "assistant-guess-turn",
                }
            ]
        ),
    ]
    judgements = [
        json.dumps(
            [
                {
                    "candidate_id": 1,
                    "decision": "add",
                    "target": "entities/example-friend.md",
                    "new_body": _entity_page("example-friend", facts[0]),
                    "reason": "new durable person fact",
                }
            ]
        ),
        json.dumps(
            [
                {
                    "candidate_id": 2,
                    "decision": "add",
                    "target": "entities/example-colleague.md",
                    "new_body": _entity_page("example-colleague", facts[1]),
                    "reason": "new durable person fact",
                }
            ]
        ),
        json.dumps(
            [
                {
                    "candidate_id": 3,
                    "decision": "add",
                    "target": "entities/sample-one.md",
                    "new_body": _asset_page("sample-one", facts[2], "example-user"),
                    "reason": "durable owned asset and relationship",
                }
            ]
        ),
        json.dumps(
            [
                {
                    "candidate_id": 4,
                    "decision": "noop",
                    "reason": "transient bodily need has no durable value",
                }
            ]
        ),
        json.dumps(
            [
                {
                    "candidate_id": 5,
                    "decision": "noop",
                    "reason": "the user asked a question and did not assert ownership",
                }
            ]
        ),
    ]
    brain = _ReviewBrain(extractions, judgements)
    registry = _Registry(brain)
    repository = MarkdownPageRepository()
    vault = VaultIndex(repo=repository)
    await vault.scan(vault_root)
    writer = AtomicWriter(vault_root=vault_root, backup_dir=tmp_path / "backups")
    curator = WikiCurator(
        repo=repository,
        vault=vault,
        writer=writer,
        llm=WikiCuratorLLM.__new__(WikiCuratorLLM),
        log_writer=LogWriter(log_path=vault_root / "log.md"),
        vault_root=vault_root,
    )
    journal = CandidateJournal(tmp_path / "data" / "jarvis.db")
    extractor = ConversationFactExtractor(
        config=config,
        journal=journal,
        registry=registry,
    )
    consolidator = Consolidator(
        config=config,
        journal=journal,
        curator=curator,
        search=None,
        vault_root=vault_root,
        registry=registry,
    )
    scheduler = CuratorScheduler(
        curator=curator,
        lock=VaultLock(tmp_path / "curator.lock"),
        config=config.wiki_scheduler,
        consolidator=consolidator,
    )
    extractor.attach_scheduler(
        scheduler,
        consolidate_after=config.wiki_scheduler.consolidate_after_candidates,
    )
    bus = EventBus()
    bridge = VoiceFactBridge(bus=bus, extractor=extractor)
    bridge.start()

    async def _speak_and_hang_up(turn: VoiceTurnCompleted) -> None:
        """One short call per fact; hanging up runs nothing extra."""
        await bus.publish(turn)
        await bus.publish(
            VoiceSessionEnded(
                session_id=turn.session_id, hangup_reason="hotkey"
            )
        )

    try:
        await _speak_and_hang_up(
            _turn(
                turn_id="openai-turn",
                provider="openai-realtime",
                text=(
                    "My friend Example Friend moved to Example City last month "
                    "and lives there now."
                ),
                session_id="call-1",
            )
        )
        await _wait_for_page(vault_root / "entities" / "example-friend.md")
        await _speak_and_hang_up(
            _turn(
                turn_id="gemini-turn",
                provider="gemini-live",
                text=(
                    "My colleague Example Colleague works at the sample library "
                    "during the week."
                ),
                session_id="call-2",
            )
        )
        await _wait_for_page(vault_root / "entities" / "example-colleague.md")
        await _speak_and_hang_up(
            _turn(
                turn_id="short-asset-turn",
                provider="openai-realtime",
                text="I own a vessel named Sample One.",
                session_id="call-3",
            )
        )
        await _wait_for_page(vault_root / "entities" / "sample-one.md")
        await _speak_and_hang_up(
            _turn(
                turn_id="transient-turn",
                provider="gemini-live",
                text="I need a short break right now.",
                session_id="call-4",
            )
        )
        await _wait_for_idle(brain, journal, calls=11)
        await _speak_and_hang_up(
            _turn(
                turn_id="assistant-guess-turn",
                provider="openai-realtime",
                text="What do you think I own?",
                assistant_text="Perhaps you own a demo glider. Noted.",
                session_id="call-5",
            )
        )
        await _wait_for_idle(brain, journal, calls=13)
    finally:
        bridge.stop()
        journal.close()

    assert facts[0] in (vault_root / "entities" / "example-friend.md").read_text(
        encoding="utf-8"
    )
    assert facts[1] in (vault_root / "entities" / "example-colleague.md").read_text(
        encoding="utf-8"
    )
    asset_body = (vault_root / "entities" / "sample-one.md").read_text(encoding="utf-8")
    assert facts[2] in asset_body
    assert "[[entities/example-user|Example User]]" in asset_body
    fresh_index = VaultIndex(repo=MarkdownPageRepository())
    await fresh_index.scan(vault_root)
    assert [page.slug for page in fresh_index.backlinks_to("example-user")] == [
        "sample-one"
    ]
    assert all(
        "short break" not in path.read_text(encoding="utf-8").lower()
        for path in vault_root.rglob("*.md")
    )
    assert all(
        "demo glider" not in path.read_text(encoding="utf-8").lower()
        for path in vault_root.rglob("*.md")
    )
    final_judge_prompt = brain.judge_requests[-1].messages[0].content
    assert "user_evidence_excerpt=" in final_judge_prompt
    assert "Evidence user turn [assistant-guess-turn]" in final_judge_prompt
    assert "What do you think I own?" in final_judge_prompt
    assert "Perhaps you own a demo glider." not in final_judge_prompt
    # 5 turn extractions + 5 judge rounds, no session sweeps, plus one
    # separate alias lookup for each page that was written.
    assert len(brain.alias_requests) == 3
    assert brain.calls == 10 + len(brain.alias_requests)
    assert journal.backlog_count() == 0