Skip to content

feat(participation): decide a message's wake once for each account that heard it - #2489

Open
zhangfand wants to merge 3 commits into
claude/reply-rule-in-wake-gatefrom
claude/wake-per-account
Open

zhangfand wants to merge 3 commits into
claude/reply-rule-in-wake-gatefrom
claude/wake-per-account

Conversation

@zhangfand

@zhangfand zhangfand commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Requested by zhangfan · project thread

Behaviour agreed with zhangfan in the project thread (2026-11-20): this closes ZHA-229. In a chat where both of Rome's accounts wake on every message, both agents wake on a message addressed to neither.

Stacked on #2481. Merge it before #2481 reaches main, or together with it.

Why

When two of Rome's accounts are in one chat, a message is recorded once, and until now only the account that recorded it first decided its wake. #2481 moved the reply rule, the bot rule and mentions of others into the wake gate, so the first account's settings could keep a message quiet that the other account would wake on. Example: account A wakes only on a mention, B on every message, and someone writes "anyone around?". If A records it first, B never wakes. On main, A dropped the message in the connection, so B woke. #2481 covered only messages addressed to another account, and only on Discord.

Sketch

  • inbound-recording.ts still records each message once. It now raises MessageSaid once for each account that hears it, with the same record, so each account's hook reads how the message addressed that account and decides on that account's settings.
  • A redelivery to an account that heard the message already raises nothing. Accounts are counted by platform and id, so an account connected again on a new channel is still the same account.
  • Each account's raise carries who said the message as that account heard them, since a platform such as Feishu names one person by a different id to each app.
  • Which accounts heard a message is kept in the process for ten minutes. A second account hears a group message moments after the first, so this covers it without a schema change. An account that first hears a message after a restart raises nothing for it, as today.
  • The holds that keep a message out of catch-up count one per raise (TurnLinks.wakeUndecided). If the first account decides before the second account's delivery arrives, the hold is already released, and a turn in the second account's session can read the message once as context and then wake on it. Accepted, since two of Rome's accounts in one chat is not supported for now.
  • The Discord skip from feat(participation): record every Discord and Feishu message, and decide the reply, bot and mention rules in the wake gate #2481 (addressesAnotherRomeAccount, romeAccountIds) is removed.

Behaviour changes

  • An account no longer misses a message because another of Rome's accounts recorded it first.
  • In a chat where both accounts wake on every message, both agents wake on a message addressed to neither, and on a reply or thread addressed to the other account without a mention. Two of Rome's accounts in one chat is not supported for now. This answers ZHA-229's first open point, as agreed.
  • Feishu, which had no stand-in, gets the same fix.

Invariants

  • A message is on record once, whatever number of Rome's accounts heard it.
  • No account decides another account's wake.
  • A redelivery wakes nothing.

Merge danger

Low to medium. Only chats with two of Rome's accounts change. An app hook that listens on both accounts' channels would now hear the message once per account; the Inbox hook decides each on its own account, which is the intent.

Evidence

  • pnpm preflight passed.
  • messages-said.test.ts: a message two accounts hear is recorded once and heard once for each, through each account's session, with each account's own sender; the second account's redelivery raises nothing, and neither does a redelivery to an account connected again on a new channel; the message is held out of catch-up until both decided; an account hearing it only after a restart raises nothing.
  • Adapter conformance on Discord: a mention of the second account, a stop said to it, a reply to it, and a message addressed to neither each raise for both accounts, with ambient for the first account and mention/reply for the second where it is addressed.

🤖 Generated with Claude Code

…at heard it

A message two of Rome's accounts hear is still recorded once, and now raises
MessageSaid for each of them, so each account's settings decide its own wake.
The Discord skip that stood in for this goes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zhangfand zhangfand self-assigned this Nov 20, 2026

@zoolsher zoolsher left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔁 This review has been superseded. See the latest review.

@zoolsher zoolsher left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Design & Alternatives review: ✅ APPROVE

Problem: When two of Rome's accounts share a chat and someone writes a message, only the account whose adapter records it first decides the wake. The message is recorded once and messages.record() returns null for the second account, so no MessageSaid is raised for it. After #2481 moved the reply/bot/mention rules into the wake gate, account A's settings could silence a message that account B would have woken on, such as an ambient "anyone around?". #2481's Discord stand-in covered only messages addressed to the other account, and Feishu had no stand-in.

Goals: each account decides its own wake on its own settings and its own addressing; the message stays on record once; a redelivery wakes nothing; the message stays out of catch-up until every account has decided.
Non-goals: deciding which agent should answer an ambient message when both chats wake on every message (ZHA-229's first open point, which the author flags for a decision); surviving a restart between the two accounts hearing the message.

What the PR does: inbound-recording.ts keeps a 10-minute in-process map, lately, keyed by chat id and message key. It holds the RecordedMessage and the set of channels that heard it. When record() returns null, a channel not yet in the set gets its own MessageSaid on the same record, and a channel already in the set raises nothing. The hold works per raise: messagesSaid keys owed by event object, and TurnLinks.wakeUndecided keeps a count per message id. Each hook gets its own heard(channel, id) and its own session through ensureChannelChat, so A and B see their own addressing. The Discord-only addressesAnotherRomeAccount skip and its romeAccountIds wiring are removed.

Alternatives checked against the code:

  1. Do nothing, or keep #2481's stand-in and port it to Feishu. It still lets A's settings silence ambient messages for B. Each adapter would need its own "addressed to another Rome account" check, including a reply-reference fetch per message on Discord. This is the root cause the PR removes.
  2. A copy of the message per account, as already happens where messageHolder() (connections/chat-adapter.ts) numbers messages per member account, with Copies in turn-links.ts. It breaks the "recorded once" invariant for platforms whose message ids are global (Discord, Feishu), and every reader would need copy handling. Not a fit.
  3. Persist which accounts heard a message: a small table keyed on message id and channel, with INSERT OR IGNORE deciding "first hearing for this account". The record could be re-read through the existing MessageRepository.idOf(chat, key) and byIds. This works after a restart and needs no time window. It costs a migration, an additive schema step that is hard to undo, plus a write per hearing. The existing agent_turn_messages (message, session) links can't stand in for it, because a "no wake" decision writes no link.
  4. The PR's in-memory map. It reuses the per-account hold that already exists and needs no schema change.
Approach Added complexity Correctness / completeness Blast radius Reversibility Maintenance Effort
Do nothing / stand-in per adapter Low per adapter, repeated N times Partial: ambient still silenced; one heuristic per platform Adapters Easy High (per-platform heuristics) Low–Med
Copy per account High (copy handling in every reader) Breaks the "recorded once" invariant Chats storage and all readers Hard (data shape) High High
Persisted hearing table Medium (migration + repo method) Exact, works after a restart Storage + recording Additive schema, hard to undo Low Medium
PR: in-process lately map Low (~40 lines in one module) Exact within 10 min; after a restart the second account misses the message (no worse than today) Only chats where two of Rome's accounts share message keys Fully reversible, no schema or wire change Low Low

Recommendation: the PR's approach is the best fit now. It moves the wake decision to the right place: one raise per account, decided by the hook, with no account deciding another's wake. It deletes a heuristic in one adapter instead of adding more, and it commits to nothing that is hard to undo. If missed wakes after a restart, or the catch-up race described in the finding, turn out to matter, alternative 3 is the next step. It would only replace the lately lookup, and the raise path would stay the same.

The double wake on ambient messages is a product decision, not a flaw in this design. Raising per account is what any later rule needs (say, ambient messages wake only one agent), and that rule would go in the wake gate, not in recording. A stale comment in recordOne ("a group message a second of Rome's accounts hears" raises nothing) now contradicts the new behaviour and should be updated.

Verdict: APPROVE — Raising MessageSaid once per account on top of the single record is the best fit. It reuses the per-account hold that already exists in TurnLinks and makes no schema or wire commitment, and its known gaps are documented and can be closed later without undoing it.

1 finding(s) — 0 posted as inline comments below; 1 listed in this table only (no matching diff line).

Severity Category File Title
P3 design packages/core/src/connections/inbound-recording.ts Second account's hold starts only when it hears, so catch-up can read the message in between

Automated review by RomeOS Code Review · commit 2ef0859 · Full review history

@zhangfand zhangfand left a comment •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔁 This review has been superseded. See the latest review.

zhangfand and others added 2 commits November 20, 2026 10:24
… who said a shared message as each account knows them

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@zoolsher zoolsher left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review: ✅ APPROVE

Goal: when two of Rome's accounts hear one group message, keep it on record once but raise MessageSaid once for each account. Each account's wake gate then decides on that account's own settings and addressing. This replaces #2481's Discord-only skip (addressesAnotherRomeAccount) and also covers Feishu.

Assessment: this is the right fix at the right layer. The change deletes an adapter heuristic and its wiring through index.ts, the integrations and the Discord adapter. In its place is a small in-process map, lately, with a 10-minute TTL, so there is no schema change. Restart behaviour is documented and tested.

What I traced:

  • inbound-recording.ts, read in full.
    • tails serializes recordings per place across accounts, so the second account's record() always runs after the first's raise.
    • lately keys are unique, because they are set only on a fresh record. forget() can stop at the first unexpired entry because Map insertion order matches at.
    • hearing/heard() are keyed by channel, so each raise hands over that account's own platform event and addressing.
  • messages-said.ts.
    • Each raise is a distinct event object. That gives a separate owed hold and a separate wakeUndecided count, and senders resolves the sender per raise.
    • speakerOf opens the session of the account that heard the message, and handedKey is per session, so the two decisions don't collide.
  • Inbox hook (channel-message/index.ts). A /stop whose addressing is ambient is ignored, so a stop said to B can't stop A. The wake gate reads addressedBy(message) from each account's own event, and inbox:message_handler takes channelUserId from that account's delivery.
  • Discord messageCreate. discordInboundText doesn't depend on which account hears the message, so sharing one record body is safe. The per-account mode === "ignore" check still runs before recording.
  • Other subscribers. Only messages-said.ts listens to MessageSaid in core, and only the Inbox hook uses messagesSaid.

Earlier threads:

  • P1, sender identity: fixed in 56f9077. The second raise now carries saidBy from its own delivery, and a test gives each account a different sender id.
  • P2, dedupe by account: fixed. hearer is now keyed by platform and account id, with a reconnect regression test.
  • The two P2s from @zoolsher (hold gap; replies and threads addressed to B waking A): both are answered convincingly. Two accounts in one chat is unsupported for now, and the PR description records the agreed behaviour. I'm not raising them again.

Not verified: I didn't run the test suite. An earlier review reported that the rstest dependencies are missing from the clone.

Option Complexity Covers Feishu Survives restart Per-account decision
#2481 Discord skip (removed) Adapter heuristic + cross-module wiring No n/a Only for messages addressed to the other account
This PR: in-process lately map, one raise per hearer ~40 lines in one module Yes No (documented, tested) Yes
Persist hearers in the schema Migration + repository changes Yes Yes Yes

The in-process map is the right trade-off for a configuration that is unsupported for now.

Verdict: APPROVE — The per-account MessageSaid raise is a correct, simple fix at the right layer, and the earlier P1 and P2 findings are fixed in 56f9077. Only an optional comment fix is left.

1 finding(s) — 0 posted as inline comments below; 1 listed in this table only (no matching diff line).

Severity Category File Title
P3 code-quality packages/core/src/connections/inbound-recording.ts Two comments still say an on-record message raises nothing

Automated review by RomeOS Code Review · commit 56f9077 · Full review history

@zhangfand zhangfand left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review: ✅ APPROVE

The change preserves single-message recording while allowing each receiving account to decide its own wake. Reviewed the recording, event, session, wake-hold, and Inbox paths alongside the added regression tests.

Tests could not start because node_modules and rstest are missing. The required submit_output tool was unavailable.

Verdict: APPROVE — No actionable regressions found in the per-account wake delivery changes.

No issues found. Code looks good!


Automated review by RomeOS Code Review · commit 56f9077

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants