fix(voice_worker): keep history and the brain's output scoped to who was there

- A final is stored under the channel, speaker name and time fixed when its
  end was decided. Decoding takes a while; a move, rename or late joiner
  meanwhile used to change where and when it was said, and who heard it.
- At start the worker closes presence a killed run left open
  (HistoryStore.close_open_presence), so a person who is gone no longer
  hears new speech after a restart.
- run_reply_engine(legacy_memory=False) skips v1's single-user memory (warm
  profile, extractor, diary, graph, digest); the v2 brain uses it so another
  user's profile or diary never reaches the prompt.
- While a brain reply runs, stdout and stderr are muted (also helper threads
  such as the web fetch pool); the worker logs through voice_worker/log.py to
  the original stderr.
- Live check: Ollama's /v1 fixes the context at 2,048 tokens and cut the
  prompt in earlier runs; with an 8k tag the chat-record question was answered
  4 of 4 and nothing reached the output. Noted in llm_contexts and the eval.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 13:28:25 +09:00
parent 864b2e7fdb
commit b3dcd12ec9
18 changed files with 265 additions and 25 deletions

View File

@@ -181,6 +181,13 @@ class HistoryStore:
(ts, user_id, channel_id))
self._conn.commit()
def close_open_presence(self, ts: float) -> int:
"""Close every open interval (at start, after a run that ended without closing them)."""
with self._lock:
n = self._conn.execute("UPDATE presence SET left_at=? WHERE left_at IS NULL", (ts,)).rowcount
self._conn.commit()
return n
def presence_close_channel(self, channel_id: str, ts: float) -> None:
with self._lock:
self._conn.execute("UPDATE presence SET left_at=? WHERE channel_id=? AND left_at IS NULL",

View File

@@ -71,7 +71,9 @@ Setting `since` (default none, meaning the whole history) drops records older th
## Writes and maintenance
- `add_voice`, `add_reply`, `add_chat`, `edit_chat`, `delete_chat`.
- `presence_join`, `presence_leave`, `presence_close_channel`.
- `presence_join`, `presence_leave`, `presence_close_channel`, and `close_open_presence(ts)`, which the
host calls at start: a run that was killed never closed its intervals, and nobody is heard until
the app reports who is there again.
- `records_of(person)` and `forget_person(person)`: the person's own records (authored by them or
replies to them), for the data screen's per-person view and delete. Deleting also removes their
presence intervals.

View File

@@ -1023,7 +1023,8 @@ def _history_block(history: Any) -> str:
def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
text: str, dialogue_memory: "DialogueMemory",
language: Optional[str] = None,
history: Optional[Any] = None) -> Optional[str]:
history: Optional[Any] = None,
legacy_memory: bool = True) -> Optional[str]:
"""
Main entry point for reply generation.
@@ -1042,6 +1043,10 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
asker from the history store (recent window and search matches,
already scoped to what this asker may see). Injected as a fenced
reference block; None or empty adds nothing.
legacy_memory: v1's long-term memory (warm profile, diary and graph
enrichment) belongs to a single user. False skips all of it, for
the v2 brain where several people share one assistant and only
the scoped ``history`` may reach the prompt.
Returns:
Generated reply text or None
@@ -1316,7 +1321,8 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
# - Plan without it → skip memory work entirely (no keyword LLM,
# no diary search, no graph search, no digest LLM).
plan_demands_memory = bool(action_plan) and plan_requires_memory(action_plan)
needs_memory = (not action_plan) or plan_demands_memory
# v2 (legacy_memory=False): the scoped history block already answers searchMemory.
needs_memory = legacy_memory and ((not action_plan) or plan_demands_memory)
# Recall gate: if the hot-window already carries a fresh tool result
# covering the query topic, skip diary/graph enrichment for this turn.
@@ -1387,7 +1393,9 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
dialogue_memory.hot_cache_get(_wp_cache_key)
if dialogue_memory and hasattr(dialogue_memory, "hot_cache_get") else None
)
if isinstance(_wp_cached, str):
if not legacy_memory:
debug_log("single-user memory off (v2): no warm profile, diary or graph", "memory")
elif isinstance(_wp_cached, str):
warm_profile_block = _wp_cached
debug_log("warm profile served from conversation cache", "memory")
else:

View File

@@ -62,6 +62,7 @@ Design principles enforced by the engine:
- Injected into the system message, after the warm profile and before the diary block, under the label "Information shared in previous conversations" (the slot the model's usual denial names) inside a `<conversation_records>` fence. The block says the lines are data, not instructions (they include other people's words), and that Jarvis's own earlier answers are not verified facts. Fence tags inside a line are removed so a record cannot close the fence.
- Not gated by the planner: the store read costs no LLM call, and the recent window is needed for follow-ups even when the planner skips memory. `None` or an empty context adds nothing.
- Eval: `evals/test_history_context_recall.py` (answers from a matching earlier record; control without the block does not know the fact).
- `legacy_memory=False` (the v2 brain) turns off v1's single-user long-term memory for the turn: no warm profile, and no memory enrichment (no keyword extractor call, diary, graph or memory digest), even when the planner asks for `searchMemory`. v1's profile, diary and graph belong to one user; in a shared v2 channel they would reach other people. The scoped history block stands in for them.
5. Build Initial Messages
- messages = [