Skip to content

feat(threads): thread_read is bounded and reads on; agent messages are capped per sender - #1222

Merged
bvsr365 merged 1 commit into
masterfrom
thread-read-bounds-and-send-limits
Nov 15, 2026
Merged

bvsr365 merged 1 commit into
masterfrom
thread-read-bounds-and-send-limits

Conversation

@bvsr365

@bvsr365 bvsr365 commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

An agent reading another thread could get a response of about 800 KB, and a message longer than 4,096 characters was cut with no way to read the rest. A thread held 3 waiting agent messages in all, so one chatty sender could shut every other sender out. Nothing stopped a sender from sending the same text twice, and every limit refusal looked about the same to the agent.

Change

  • thread_read, messages view: each message still holds at most 4,096 characters. A message that is cut is marked truncated with totalChars and nextTextOffset. The new textOffset parameter continues the message at afterIndex from that point. A cut never splits a character.
  • An agent's message in the messages view: since feat(chat): a message an agent sent shows as a labelled "Agent" bubble with only its words #1223, an agent's message is stored with its label: the [Message from an agent in thread …] header, the agent's words, and the closing [End of message from an agent] line. origin.body marks where the words sit. thread_read returns that stored text, so text, totalChars, nextTextOffset and textOffset all count characters of the whole stored message. The first part of a long agent message starts with the header, and the last part ends with the closing line. The stored text never changes after it is written, so reading on gives consistent pieces.
  • thread_read, both views: a response holds at most 64 KB of UTF-8 JSON. A page that reaches the cap holds fewer whole items, says capped: true, and its nextAfterIndex or nextAfterSeq continues after the last item it holds. An event too big for a response of its own comes with a preview of its payload. The existing limits stay: events 200 by default and 500 at most, messages 50 by default and 200 at most.
  • A run's result: thread_wait's resultText and run.result events in thread_read are cut to 4,000 characters, with a marker, the full length and a pointer to thread_read. The cut happens in the tool only, so a subagent's result still reaches its parent whole through the delegation notice.
  • thread_send result: a new delivery: "started" | "queued" field sits beside queued.
  • Queue caps: a thread holds at most 3 waiting agent messages from each sending thread and 10 from all agents together. This applies to thread_send admission and to what an agent queues or takes over with queue_enqueue and queue_edit. The refusals are agent_queue_full_per_sender and agent_queue_full. A person's messages never count.
  • Identical repeats: the same text from the same sending thread to the same thread within 2 minutes (AGENT_REPEAT_WINDOW_MS) is refused with duplicate_message and retryAfterMs. It is decided last in the engine's admission, under the thread's lock, so two identical sends at once cannot both pass. A retry with the same clientRequestId never reaches admission: the engine replays its receipt and returns the first result. The record lives in memory, like the per-run send count, so a restart forgets it.
    • Why 2 minutes: the window is longer than an MCP client waits on a call (about a minute) plus the retry an agent makes when a send seemed to fail, which is the repeat this catches. It is short enough that the same words sent after the other thread has answered (such as "continue") still go through.
  • Bots: fix(bots): a bot starts and messages as many of its threads as the work needs #1219 removed a bot's counts per chat turn, and this PR puts none back: the 20-per-run sends_per_run_exceeded count still skips a bot's calls. The queue caps count waiting messages in one target thread. A bot's messages were already held to the old cap of 3 agent messages in all. Now they count like any sender's: at most 3 from the bot's thread waiting in one thread, and 10 from all agents. For a bot that is looser than before. A bot that starts or messages many threads is not limited by this. The 25-start, 21-message chat turn in botThreadsDaemon.test.ts still passes. The repeat check applies to a bot too: the same text to the same thread within 2 minutes is refused with the reason, and the bot can tell its owner.
  • Error codes: each limit has its own data.conflict and wording that says what to do: agent_hops_exceeded, agent_queue_full_per_sender, agent_queue_full, duplicate_message, sends_per_run_exceeded. The sends-per-run limit used to be a plain error and is now a conflict like the others.
  • Tool descriptions, instructions.ts and docs/run-engine/SPEC.md §15.1 describe the new parameters, results and caps.

How to review

  1. src/mms/run/mcp/appTools/thread.ts: the constants at the top, then fitPage, boundedEvent and cutResultText, then the thread_read, thread_send and thread_wait handlers, and SEND_CONFLICTS.
  2. src/mms/orchestrator/OrchestratorService.ts: agentQueueRefusal, its two call sites (send admission and queueCommand), and the repeat check (claimAgentMessage, the new onAdmit send option). This is the riskiest part.

Verification

Tier 5 (run engine). Tested commit: 840af70, rebased on master at ea1b126 (which includes #1197, #1170, #1219, #1223, #1224 and #1220).

  • npx tsc --noEmit -p tsconfig.node.json: clean.
  • npx vitest run with these 16 files: 403 tests passed. The files: tests/run/threadActionsTools.test.ts, threadActionsDaemon.test.ts, queueTools.test.ts, queueEditDaemon.test.ts, mcpToolRegistry.test.ts, agentThreadReach.test.ts, threadCreateTools.test.ts, threadCreateDaemon.test.ts, botThreadTools.test.ts, botThreadsDaemon.test.ts, delegationGuidance.test.ts, mousseMcpServer.test.ts, appToolListing.test.ts, subagentHold.test.ts, subagentHoldIntegration.test.ts, and tests/bots/botThreads.test.ts.
  • Which test covers each part of the change:
    • Reading on with textOffset, including a character outside the BMP on the cut: mcpToolRegistry "a message marked truncated is read on with textOffset".
    • The 64 KB cap with a correct continuation: mcpToolRegistry, one test per view. They use two-byte characters, check every response's byte length, and read every item exactly once by following the continuation. The events test includes an event larger than the cap.
    • An agent's message (the feat(chat): a message an agent sent shows as a labelled "Agent" bubble with only its words #1223 envelope) read on with textOffset: mcpToolRegistry "an agent's long message reads on over its stored text". The pieces put back together equal the stored text, and the agent's own words sit between origin.body.head and tail.
    • The result cut: threadActionsTools "cuts a long result" (it also checks that the parent's child_result row keeps the whole text) and mcpToolRegistry "cuts a run's final text in the events".
    • The cap per sender, with two senders over the real MCP server: threadActionsDaemon. The second sender still gets in after the first is refused.
    • The cap in all, with four senders: queueEditDaemon. A person's message still gets in.
    • A repeat refused while a replay with the same clientRequestId returns the first result, and another thread may send the same words: threadActionsDaemon. With the admission hook removed, this test fails.
    • Each refusal's code and wording: the table in threadActionsTools.
  • Tests whose claim changed: the old "three in all" assertions in queueEditDaemon and threadActionsDaemon now expect the cap per sender. The sends-per-run tests now expect a conflict. The hop-limit chain test now sends different words on each hop, because the same words would now be refused as a repeat before the hop limit is reached.
  • Real data (check-on-real-data, the messages view, copies of 151 threads with 13,961 messages): old and new code were run on the same threads. Every message was rebuilt exactly by following textOffset: 0 mismatches. Before, 100 messages were cut with no way to read on; reading them in full took 147 extra calls. At the largest page size (limit 200), one page was 97 KB before; now the largest is 63.6 KB, 2 pages stopped at the cap and continued, and none went over. At the default limit (50), no page reached the cap; the largest was 42.9 KB.
  • Not checked: the events view on real run events (the check read only the transcripts), and a restart in the middle of the repeat window (the record is in memory by design). The full suite did not run.

Notes

  • This overlaps with PR 4 (thread-settle-notice), which also edits the thread_send description and result. The edits here are one added delivery field and one changed sentence in the description, so the two PRs should rebase cleanly. instructions.ts changed in the thread_send and thread_read lines.
  • queue_enqueue into another thread is held to the queue caps but not to the repeat check.

Refs #1160

🤖 Generated with Claude Code

…e capped per sender

- thread_read cuts a message at 4,096 characters with textOffset to read on, and
  holds a response to 64 KB in both views, saying where to continue.
- thread_wait's resultText and run.result events are cut to 4,000 characters
  with a pointer to thread_read; the delegation notice is unchanged.
- thread_send returns delivery: started | queued.
- A thread holds 3 waiting agent messages per sending thread and 10 in all.
- The same text from the same thread within 2 minutes is refused
  (duplicate_message); a retry with the same clientRequestId still replays.
- Each limit refusal has its own conflict code and wording.

Refs #1160

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bvsr365
bvsr365 force-pushed the thread-read-bounds-and-send-limits branch from f2d4fe6 to 840af70 Compare November 15, 2026 15:38
@bvsr365
bvsr365 merged commit 73cf19a into master Nov 15, 2026
7 checks passed
@bvsr365
bvsr365 deleted the thread-read-bounds-and-send-limits branch November 15, 2026 15:47
bvsr365 added a commit that referenced this pull request Nov 15, 2026
…nd repeat check every agent has

#1222's caps (three waiting per sender, ten in all) and its repeat check (duplicate_message) count
a sender on another computer by its computer and thread (origin.from), so one computer and two
follow one rule and one set of codes. The owner path's own repeat check is gone; its rate limit and
daily budget stay.

Refs #1160

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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