Skip to content

Wake Switch's own reader only for command rows, and read only those (CHOO-3667) - #1236

Closed
abeldantas wants to merge 3 commits into
admin-mention-checksfrom
admin-reads-commands
Closed

abeldantas wants to merge 3 commits into
admin-mention-checksfrom
admin-reads-commands

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

The process that holds a workspace's admin client read every row of every room to find the !command rows its consumer answers. Since #1222 the warnings for tags nobody can answer are posted by the process that writes the message, so commands are the only rows that consumer acts on.

In this PR we:

  • add the row's type to the announcement a new row sends (a migration replaces the notify function)
  • let a listener subscriber ask for some types only (MessageListener.subscribe); an announcement with no type, from a database before the migration, still wakes everyone
  • give CommandConsumer its row types (Consumer.ROW_TYPES), so it is woken for command rows only and reads only those, through a partial index on them, however far behind its cursor is

Stack (merge order)

  1. Post the tagged-agent heads-up from the writing process, and skip it when only room agents are tagged (CHOO-3667) #1222
  2. This PR ← you are here

…CHOO-3667)

The admin client's CommandConsumer acts on `!command` rows only, but the
process holding it was woken for every row of every room and read each
one to find them, so one process did a read per message for the whole
workspace on top of its share of everything else.

The message announcement now carries the row's type, a listener
subscriber can ask for some types only (MessageListener.subscribe), and a
consumer that names its types (Consumer.ROW_TYPES) is woken for those and
reads only those rows, through a partial index on the command rows. A
database or process from before this sends or reads announcements
without a type, which still wake every reader.
Copilot AI lite review requested due to automatic review settings November 1, 2026 17:05

Copilot AI 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.

🟡 Changes recommended

The main blocker is a moderate migration issue: the IF NOT EXISTS guard would accept an existing INVALID index with the same name, so the upgrade reports success while command reads silently fall back to scans.

3 open issues
What changed in this PR

This PR stops Switch's own admin reader from waking for and reading every row in every room. It only needs the !command rows, which the CommandConsumer acts on. A new row's notification now includes its type, and a listener can subscribe for certain types only. The consumer declares its types, and a partial index covers command rows so reads stay fast however far behind the consumer is. This is the second PR in a stack that follows #1222, which already moved the tagged-agent warnings to the process that writes the message.

Changes:

  • A migration replaces the notify function so the announcement includes the row's type, and adds a partial index on command rows.
  • MessageListener.subscribe accepts an optional set of types. Announcements without a type still wake every subscriber.
  • CommandConsumer sets ROW_TYPES to command rows, so its transport reads and wake-ups are limited to those rows.
File Description
core/​tests/​switch_core/​transport/​test_postgres_transport.py Adds a test that a typed reader is handed only command rows.
core/​tests/​switch_core/​transport/​test_membership_signals.py Updates the fake listener to accept the new types argument.
core/​tests/​switch_core/​messages/​test_notify.py Tests the announcement type field and typed wake-ups, including the no-type fallback.
core/​tests/​switch_core/​gateway/​conftest.py Updates the fake listener signature for the types argument.
core/​tests/​db_buffer.py Updates the no-op listener signature for the types argument.
core/​switch_core/​transport/​postgres.py Passes row types through receive, watch, and read paths, and skips the shared room cache for typed readers.
core/​switch_core/​transport/​port.py Adds the optional row_types parameter to the transport interface.
core/​switch_core/​migrations/​versions/​f2a9c7d41e08_command_rows_for_the_admin_reader.py Replaces the notify function to include the type and adds the partial command index.
core/​switch_core/​messages/​notify.py Parses the optional type and wakes subscribers only for their types.
core/​switch_core/​events.py Defines the shared COMMAND_EVENT_TYPE constant.
core/​switch_core/​db/​stores/​message_store.py Adds an optional type filter to head and list queries.
core/​switch_core/​db/​notify_ddl.py Documents the type field in the notification trigger.
core/​switch_core/​db/​models.py Declares the partial index on command rows in the model.
core/​switch_core/​clients/​consumer.py Adds the ROW_TYPES class attribute and passes it to the transport.
core/​switch_core/​clients/​command_consumer.py Sets ROW_TYPES so the command consumer reads only command rows.
CHANGELOG.md Notes that the reader is now woken only by commands.

Review effort: Lite

Comment on lines +38 to +42
# `IF NOT EXISTS` so an index built by hand ahead of the upgrade is kept.
op.execute(
f"CREATE INDEX IF NOT EXISTS {_INDEX} ON messages (room_id, seq) "
"WHERE event_type = 'com.switch.command'"
)
Comment on lines 6 to +9
**The notification is a hint, never the payload.** It carries a room, a
position and a row id, and a consumer reads the row itself. That is what makes
position, a row id and the row's type, and a consumer reads the row itself.
The type lets a reader that acts on some kinds of row only be woken for those
(`MessageListener.subscribe`). That is what makes
Comment on lines +265 to +266
if types is not None:
query = query.where(Message.event_type.in_(list(types)))
@abeldantas

Copy link
Copy Markdown
Collaborator Author

Combined into #1307 with the other scaling PRs.

@abeldantas abeldantas closed this Nov 2, 2026
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