Skip to content

fix(bots): a bot's Chat can tell the DM a background job came from, until anyone else writes - #50146

Closed
brennanb2025 wants to merge 3 commits into
mainfrom
brennanb2025/bot-dm-saved-reply
Closed

brennanb2025 wants to merge 3 commits into
mainfrom
brennanb2025/bot-dm-saved-reply

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

All of a bot's Slack DMs share one Orca chat. When a job started from a DM finishes in the background, the bot wakes on its own, and that turn's answer stays in Orca. Now the bot is told which DM started the job and can post there itself. Orca lets it post only into the DM that started that job, and only until anyone else's words reach the chat; after that the answer stays in Orca. It can miss a post, but it can't send to a DM that didn't start the job. Orca still posts nothing on its own; the bot decides what to say.

Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​264 $\color{#cf222e}{\Huge{\mathbf{−}}}$​14 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​250
Prod 16 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​396 $\color{#cf222e}{\Huge{\mathbf{−}}}$​78 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​318

What Changed

  • Before: in a bot's Chat, worker-start printed no note, and bot reply was always refused there. A DM asker never heard about a job that finished later.
  • After:
    • When the turn that runs worker-start was opened by a Slack DM, Orca saves that DM and the note names it: bot reply --to <that DM, with a token for this start> --text "...". Each start gets its own token, so a note that was voided never works again.
    • A later turn, including one that started on its own, may post there with --to.
  • When a saved DM stops working (fail closed):
    • Anything from another conversation reaches the chat after the save. That covers a DM queued behind the turn, one that joins or relinks it mid-turn, an automation run adopted into it or sent straight to it, and a returned message sent again. It is checked when a delivery is sent (before the chat takes it) and again for every message the chat admits that carries someone else's words, so it doesn't matter which turn it lands in or whether that turn has a message of its own. The owner's typing and worker reports don't void it.
    • --to checks again just before posting and before each retry.
    • The chat is no longer linked to that DM. A chat holds one link, and only a delivery moves it.
    • A watch gap, sign-out, account switch, Orca restart or bot deletion.
  • When a DM is never saved:
    • Something from another conversation reached the chat after the message that opened the saving turn, even if the same DM wrote again later.
    • Orca never saw the opener arrive: an earlier Orca, a reset since, or more than 32 messages ago.
    • Any of stage 1's checks fail: a delivery mark, the opener stamp, or the origin record not naming the same channel.
  • How: ChatDeliveries (the existing per-chat delivery record) now keeps a short arrival log for each bot's Chat (only those, so thread chats add nothing) and the saved DMs. It clears both whenever it clears its own state. --to uses its own RPC (connector.replySavedDm), so an older host refuses it instead of posting in the chat's current DM.
  • Local and paired-server starts: both behave the same. The note is read from the bot's own chat before the start is routed, so --on <server> doesn't change it. How a remote worker reports back is handled separately.

What this stops working for

Surface Changes?
Thread chats (the earlier stage) Nothing. Same note, same bot reply.
Bot's Chat (DMs) Nothing stops. bot reply without --to is still refused; its message now points to --to.
Orca-only chats, automations, terminals Nothing. A chat with no Slack link never saves a DM.
Older host + newer CLI --to is refused with "update Orca"; nothing posts.
Restart Saved DMs are kept in memory and lost on restart, so the answer stays in Orca. Making them survive a restart is a separate item.

Known limits

  • A thread inside a DM is still not covered.
  • The chat's earlier turns, and the owner's own typing, are still in the agent's context. That is true of every answer the shared chat gives today.

Tests

connector-wake-reply.test.ts drives the real ChatDeliveries delivery path and the real connector.wakeReplyPlace and connector.replySavedDm handlers. I removed each rule in turn and confirmed a test fails without it:

  • another DM voids the saved DM;
  • a save is refused when another DM is already in the turn;
  • a save is refused when the opener is unknown;
  • a delivery is recorded;
  • an adopted run is recorded;
  • a clear drops saved DMs;
  • the chat must still be linked to the DM;
  • the caller must be the chat itself;
  • a deleted bot's DMs are dropped.
Screenshots: the note and replies as they appear in the bot's chat (light and dark)

Worker start from a DM:

start note

A later turn posts with --to:

posted

After another DM reached the chat:

voided

Plain bot reply in the Chat:

plain reply

…ntil anyone else writes

A turn that starts on its own (a background job finished) stays in Orca. For a
job started from a Slack DM, worker-start now saves that DM and names it, and
bot reply --to posts there. The saved DM is void once anything from another
conversation reaches the Chat, whichever turn it lands in, and on a watch gap,
sign-out, account switch or restart. A save needs the turn's opener to be the
last thing any other conversation sent before it.
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Perf scan: PASS (head c73ab73)

No findings: no P0/P1. Nothing new runs on a timer, poll, subscription or IPC at idle; there are no SQLite or filesystem writes; nothing new runs in the renderer; and no per-item subprocesses are added.

Work by trigger (main process; the new state lives only in memory)

Trigger Work per event Population / bound
Each connector delivery (ChatDeliveries.deliver → savedDms.arrived) conversationId + 2 Map lookups. Only for a bot's Chat: push onto its arrival log (shift at 32) and a sweep of its saved DMs Log ≤ 32 entries per bot Chat. Sweep ≤ 64 DMs, and only for a chat that has a saved DM. Thread chats never get a log. Constant per delivery, whatever the number of worktrees, chats or history size
An automation run adopted into a chat (adopt → arrived) Same as above, without a delivery id Same bound
worker-start (connector.wakeReplyPlace) A DM turn in a bot's Chat now reaches readBotTurnOrigin (before, it returned early). With widen=false that reads only the newest turn, by index (latestTurns: 1), plus a bot-request lookup by id. Then save scans the ≤ 32-entry log once One call per worker start, and only for a delivered DM turn. It is the same bounded read thread chats already pay. It does not grow with journal size: an unindexed window throws rather than reading the whole chat. The CLI caps it at 5 s
bot reply --to (connector.replySavedDm) O(1) saved-DM lookup, the existing replyTarget and botTurnFor, and one cloud reply via the shared sendReply One call per command, with a single post. Retries are the cloud's existing ones
Bot deletion (forgetBot) Walks both maps (saved, arrivals) Entries ≤ bot Chats seen since the last reset. Runs once per deletion
Account reset / sign-out / watch gap / dispose (ChatDeliveries.clear) Clears both maps O(entries), once per event

Regression classes touched

  • Per-entity state outlives the entity: matched and disproved. The maps are keyed by bot Chat session and are emptied in the same clear() as their sibling ChatDeliveries maps, which also have no per-session close delete. They are also emptied on bot deletion. A bot Chat that is closed and recreated leaves one stale entry until the next reset or deletion, at most about 13 KB (32 arrivals plus 64 DM keys). That memory grows only with manual bot Chat recreations, never with traffic.
  • Hot-path rescans: matched and disproved. log.shift() and the saved-DM sweep in arrived, and the log scan in save, are capped at 32 or 64 entries.
  • Expensive code newly reachable past a gate: matched and disproved. Bot-Chat DM turns now reach readBotTurnOrigin, which is limited to the newest turn by index, once per worker start.
  • Unbounded fan-out or queues: no matching code. sendReply is a single awaited call.

Six questions

  1. Work paid before the skip gate: reviewed. conversationId is computed per delivery even for chats that aren't a bot's Chat. That is one string per delivery, constant.
  2. Ownership multiplying work: reviewed. There is one BotChatSavedDms per seam.
  3. Which thread and storage layer pays: reviewed. Main process, in memory, no sync I/O added. The newly reached journal read is the existing async, bounded one.
  4. Growth and progress: reviewed. Every structure has a fixed cap, and the log keeps only the last 32 arrivals.
  5. Execution host: not applicable. There are no new host probes or SSH calls.
  6. Teardown: reviewed. The state is cleared on reset, watch gap, dispose and bot deletion. It holds no timers or listeners.

Coverage: a static trace at this head of the CLI and main files in the diff, plus their callers (ChatDeliveries.deliver/adopt, ExternalConversationSeam.resetAccount/dispose, bot removal in orca-runtime-bots, readBotTurnOrigin/readNewestBotActingTurn). This was a static review only, so it makes no timing claims.

…and can't come back

From the audit: each save now has its own --to, so a voided note never works
again after a later save. Any message the Chat admits carrying someone else's
words (an event run sent straight to the Chat, a returned card sent again) voids
saved DMs and joins the arrival log. --to checks again after its async read and
before each cloud retry. A bot's new Chat drops what its old one kept.
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Perf scan, head 0f7e36a: pass, 0 findings

Scope: git diff origin/main...HEAD (17 files) plus the callers and consumers it reaches. Every new piece of work runs on Electron main. None of it has a timer, a poll, a subprocess or a disk write.

Trigger (rate) Work per trigger Population and bound
Each connector delivery (ChatDeliveries.deliver) and each adopted automation run (adopt), at the delivery rate savedDms.arrived: one map lookup, then a push and possibly a shift on a log capped at 32 entries, then a walk over that chat's saved DMs (≤64) Only a bot's Chat (bot-chat kind) gets a log, so thread chats add nothing. The first arrival in a new Chat calls forget, which walks two maps holding at most one entry per bot. That runs once per new Chat, not on every message.
Every message a chat admits (messageAdmitted), once per sent message, not per streaming delta and not for queued or continuation sends admitted: builds an array of at most a few named conversations, does a find over the 4 connector sources, then returns early unless outside words are present. If they are, it calls arrived as above. Constant per message. For a chat that isn't a bot's Chat, arrived ends after two map misses.
orca orchestration worker-start in a bot's Chat (connector.wakeReplyPlace), once per CLI call Bot Chats with a bare DM target now reach readBotTurnOrigin, which main used to skip with an early return. That read is unwidened (latestTurns: 1, anchorsOnly, indexedOnly), plus save: a scan over ≤32 entries, a slice and 4 random bytes One indexed read of the newest turn per worker-start. It doesn't grow with chat history. This path was already taken for thread chats.
orca bot reply --to (connector.replySavedDm), once per CLI call stillSaved() is checked up to 2 times plus once before each retry (REPLY_ATTEMPTS = 3). Each check does replyTarget (an existing link-index lookup plus the turn opener) and two map gets. Then one botTurnFor and one cloud reply. At most about 4 checks per call, each the same cost as the connector.reply path that already exists
Bot deletion forgetBot: walks the two maps At most one entry per bot. Runs on delete only.
Account reset, sign-out or watch gap (ChatDeliveries.clear) Two Map.clear() calls O(1)

Retained memory: at most one log entry per bot (32 arrivals plus 64 DMs saved for --to, each a short string). A new Chat for the same bot frees the old one's entry, and bot deletion or a reset frees it too. If a bot's Chat is closed while the bot lives on, that one bounded entry stays until the next Chat, the bot's deletion or a reset. It doesn't grow with history, number of chats or uptime.

Regression classes touched:

  • Per-entity state outliving its entity: matched and ruled out. The state is keyed per bot and released by a new Chat, forgetBot and clear, as shown above.
  • Hot-path rescans: matched and ruled out. Every loop is capped at 32 or 64 entries, and the forget walk is capped at one entry per bot.
  • Expensive code past a loosened gate (wakeReplyPlace now lets bot Chats through): matched and ruled out. The newly reachable read covers one indexed turn per CLI call.
  • New steady-idle wakeups: none.
  • Subprocess per item: none.
  • Sync SQLite on main: only the existing bounded read above.

Six questions:

  1. Work before the skip gate: reviewed. admitted returns early before any map work for messages without outside words.
  2. Ownership multiplying work: reviewed. There is one BotChatSavedDms per seam and one entry per bot.
  3. Which thread pays: reviewed. Main, in-memory only, with one bounded journal read per worker-start.
  4. Growth and progress: reviewed. Logs are capped and the retry count is fixed.
  5. Execution host: not applicable. There are no host probes or per-item SSH round trips.
  6. Teardown: reviewed. clear, forgetBot and Chat replacement release the state. proceed stops retries once the DM is no longer saved.

Runtime timing wasn't measured. The costs above are counted from the source at the reference fleet (100+ agents, chat messages arriving several times a second) and stay constant at 10× that.

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Closing this. Slack DMs now each get a chat of their own, the same as a thread does, so a bot's background answer to a DM no longer shares a chat with anyone else's DM. The saved-DM mechanism here was built for that shared chat and isn't needed anymore. A smaller change will let the existing worker-start note name a DM's own chat, so a bot that wakes on its own there is told it can post with its reply command.

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.

1 participant