fix(voice_worker): one person's voice and chat answers are written one at a time

The voice and chat lanes let a person's voice reply start while their chat
reply was still running. The chat delete consent counts "the very next
message" by turn number, so a message that started before the delete was
asked could pass as the confirmation, and the person's dialogue memory
was updated out of order.

- A reply takes its asker's lock, then its lane's: one person's replies
  stay one at a time in the order asked, different people's voice and
  chat answers still overlap, and a reply waiting for its asker holds no
  lane. Test: a person's voice question starts only after their chat
  answer.
- Specs and roadmap: the lanes are per person; the roadmap no longer says
  voice answers wait behind chat answers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-12 01:27:13 +09:00
parent 2f69557e1b
commit 19f984ae73
6 changed files with 50 additions and 10 deletions

View File

@@ -135,7 +135,8 @@ never takes the voice turn and never goes through the response policy.
person), pinging nobody. A failed or empty answer posts nothing. An answer that arrives 5 minutes or
more after it was asked is not posted.
- The worker writes voice and chat answers in separate lanes (`voice_worker.spec.md`), so a voice
answer never waits behind a chat answer. For the two to run at the same time the LLM must take two
answer never waits behind another person's chat answer; one person's answers are written one at a
time, in the order they asked. For the two to run at the same time the LLM must take two
requests at once (`docs/v2/conversation.md`, "LLM 동시 요청 수"): until the app starts Strata itself
(M5), Strata is started with `--parallel 2` when some channel answers everything; with
`--parallel 1` the two lanes' LLM calls take turns at Strata.

File diff suppressed because one or more lines are too long

View File

@@ -15,7 +15,8 @@ from jarvis.memory.history import HistoryContext, Scope
class Brain(Protocol):
def reply(self, user: str, text: str, history: HistoryContext, chat=None) -> Optional[str]:
"""Blocking; called on a worker thread, one call at a time. ``chat``: the chat backend for
"""Blocking; called on a worker thread, one call per person at a time (a voice and a chat
reply for different people may overlap). ``chat``: the chat backend for
the chatChannels tool (voice_worker/chat_bridge.py), or None without registered channels."""
def addressed(self, user: str, text: str, names: list[str]) -> Optional[bool]:
@@ -74,8 +75,9 @@ class Muted:
The v1 reply engine and its tools print the question, the answer, tool arguments, URLs and
memory snippets (``print`` and ``debug_log``, also from helper threads such as the web fetch
pool); the worker's logs must never carry such text. Replies run one at a time; the worker's
own log (``voice_worker.log``) writes to the original stderr and is not muted.
pool); the worker's logs must never carry such text. Replies may overlap (a voice and a chat
lane); output stays muted while any of them runs. The worker's own log (``voice_worker.log``)
writes to the original stderr and is not muted.
"""
_lock = threading.Lock()

View File

@@ -52,9 +52,12 @@ class VoiceWorker:
self.tick_s = tick_s
self.history = history # jarvis.memory.history.HistoryStore or None
self.brain = brain
# One reply at a time per lane: voice answers never wait behind a chat answer (conversation.md:
# with an "answer everything" chat channel the LLM takes two or more requests at once).
# One reply at a time per lane: voice answers never wait behind another person's chat answer
# (conversation.md: with an "answer everything" chat channel the LLM takes two or more
# requests at once). One person's replies stay one at a time across lanes, so their dialogue
# memory and delete confirmations ("the very next message") follow the order they asked.
self.brain_lanes = {"voice": asyncio.Lock(), "chat": asyncio.Lock()}
self.brain_people: dict[str, asyncio.Lock] = {}
self.chat_timeout_s = chat_timeout_s
# chat_action ids are unique across connections and worker runs, so a late answer from an
# earlier connection (or an earlier worker) never completes a newer action.
@@ -547,7 +550,8 @@ class Session:
async def _reply_inner(self, rid: str, user: str, text: str, via: str, speak: bool) -> None:
w = self.worker
channel = self.voice_channel
async with w.brain_lanes[via]:
# Person first, then lane: a reply waiting for its asker's other answer holds no lane.
async with w.brain_people.setdefault(user, asyncio.Lock()), w.brain_lanes[via]:
if self.closed:
return
now = time.time()

View File

@@ -219,8 +219,10 @@ which carry Discord's own time.
- Chat messages are stored only as given; the app sends only registered channels.
- `reply` asks the brain to answer `user`'s `text`. The worker builds the history context for that
user (their scope only) and runs the reply engine on a worker thread, one voice reply and one chat
reply at a time: the two kinds have separate lanes, so a voice answer never waits behind a chat
answer (`app/src/conversation.spec.md`, chat calls).
reply at a time: the two kinds have separate lanes, so a voice answer never waits behind another
person's chat answer (`app/src/conversation.spec.md`, chat calls). One person's replies stay one at
a time across both lanes, in the order asked, so their dialogue memory and delete confirmations
("the very next message") follow that order.
`reply_text` carries the answer (`failed: true` with empty text when the brain gave none). A voice
answer (`via: "voice"`) is stored as a `reply` record to the user in the current channel; a chat
answer is stored when the app reports the sent message as `chat_message` with `to`. With

View File

@@ -703,3 +703,34 @@ def test_answers_of_one_kind_are_written_one_at_a_time(store, via):
asyncio.run(with_worker(body, store=store, brain=FakeBrain(answer)))
assert peak[0] == 1
@pytest.mark.unit
def test_one_persons_voice_and_chat_answers_are_written_one_at_a_time(store):
# Their dialogue memory and delete confirmations ("the very next message") follow the order
# they asked, so a person's second question starts only after their first is answered.
import threading
release = threading.Event()
running = []
def answer(user, text):
running.append(text)
if text == "chat question":
release.wait(10)
return f"answer for {text}"
async def body(url):
async with connect(url) as ws:
await hello(ws)
await send(ws, type="reply", id="c1", user="100", text="chat question", via="chat", speak=False)
await asyncio.sleep(0.1)
await send(ws, type="reply", id="v1", user="100", text="voice question", via="voice", speak=False)
await asyncio.sleep(0.3)
started = list(running)
release.set()
seen = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "v1")
return started, [m["id"] for m in seen if isinstance(m, dict) and m.get("type") == "reply_text"]
started, order = asyncio.run(with_worker(body, store=store, brain=FakeBrain(answer)))
assert started == ["chat question"], "the voice question started before the same person's chat answer"
assert order == ["c1", "v1"]