Repository navigation
Description
Bug description
Successfully admitted read-only tools execute concurrently, but their final tool.result events are retained until every sibling in the read batch has completed. A fast result can therefore remain invisible behind an unrelated slow sibling even after its PostToolUse processing and inbound contextual-guardrail review have completed.
This became operator-visible with concurrent foreground Subagent calls: subagent.end arrives immediately for the fast child, while its outer tool.result remains behind the batch barrier. Ordinary read-only tools have no separate terminal projection, so clients cannot show even that much progress.
The result-timing regression was introduced by 409ea74e8 when concurrent execution moved from immediate per-goroutine finalization to private execution records followed by wg.Wait() and an ordered release drain in engine/agent/dispatch.go.
Regression history
Read-only batching and concurrent foreground Subagents are older behavior. Before 409ea74e8, each batch goroutine executed and finalized its own tool result, so a fast call emitted EvToolResult before the batch-level wait returned. The LLM still waited for the complete dispatch, but clients could observe each safe result promptly.
409ea74e8 moved successful calls to executePrivate. Each goroutine now retains an execution record without emitting the final result; dispatch waits for every sibling, then resolves inbound release decisions and finalizes results sequentially in original call order. This intentionally supports ordered contextual-guardrail release decisions, but it also means one slow sibling delays every fast sibling's client-visible result.
The missing Subagent checkmark was a pre-existing Mecatui state split, not a typed-scrollback regression: subagent.end controlled the terminal summary while only the outer tool.result controlled header chrome. The split existed from the original inline Subagent UI and was preserved by typed-scrollback extraction commit e7e51c90b. It became a sustained, operator-visible problem only after 409ea74e8 inserted the batch-wide delay between those events. PR #1977 fixes the specialized Subagent/Team/Parallel presentation path; this issue tracks the underlying generic read-only result timing.
Steps to reproduce
- In one model turn, dispatch two read-only tools concurrently.
- Make the first finish quickly and the second block for approximately 30 seconds.
- Observe the event stream while the second remains blocked.
A concrete UI reproduction is two foreground Subagent calls where one returns immediately and one sleeps. The fast child emits subagent.end, but no matching outer tool.result arrives until the slow child finishes.
Expected behavior
Once an individual read-only result has completed all applicable PostToolUse processing, repair, inbound guardrail review, and release authorization, the safe result should become observable without waiting for unrelated siblings.
The design must preserve:
- no result exposure before its effective inbound release decision;
- model-facing tool-call/result pairing and original-call ordering;
- deny-dominant permissions and configured guardrail asks;
- held-result destruction and cancellation semantics;
- serialized, comprehensible operator approval prompts.
The acceptance plan must decide whether a later clean result may bypass an earlier result awaiting operator release, and distinguish client event order from model-history order.
Actual behavior
runReadBatch executes every admitted call into a private record, waits for the complete batch with wg.Wait(), then resolves and emits results sequentially in original call order. A slow sibling delays every successful fast sibling. If an earlier result requires operator approval, later results also remain behind it.
Pre-execution denials and preparation failures may still emit early because they do not enter the admitted execution batch.
Planning direction: separate presentation release from canonical result order
The LLM is not called again until the complete dispatch returns. dispatch reassembles the full result slice, the session records those results, and only then can the next model turn begin. UI notification order therefore does not need to match model-facing tool-result order.
However, the existing EvToolResult is not merely a UI notification:
- it is persisted and folded to reconstruct the session conversation;
- reconstruction appends tool messages in event order;
- tool-call and consecutive-failure counters are order-sensitive;
- the live session separately records the complete result slice in original model-call order.
Emitting canonical EvToolResult events in completion order while recording the live aggregate in call order could make durable reconstruction disagree with the live session. The plan should not solve the UI latency problem by casually reordering canonical events.
The recommended design direction is to separate two lifecycle facts:
- Presentation/release event: correlated by call ID and emitted as soon as that individual result has completed PostToolUse processing, repair, inbound review, and its effective release decision. It may arrive in completion order and should carry the exact guardrail-approved effective payload needed by clients to render the result immediately.
- Canonical
tool.result: retained until the batch can be recorded and emitted in original model-call order, preserving provider pairing, aggregate counters, event-source reconstruction, and model history. When it arrives, clients treat it as an idempotent authoritative confirmation of the earlier presentation event.
This parallels the Subagent/Parallel UI lifecycle: an early correlated terminal projection updates presentation while a later canonical tool.result preserves the conversation contract.
The plan must pin these details:
- The early payload and later canonical result remain byte-/part-equivalent after all effective-payload rewriting; no pre-guardrail content may leak.
- A later clean result should be allowed to notify the UI while an earlier sibling awaits operator release, provided the later result's own decision is final and independent.
- Operator release prompts remain serialized and comprehensible even when independently clean results notify around them.
- Cancellation destroys or replaces every still-held result without allowing an early event for a result whose release was not final.
- Reconnect/replay semantics define whether the presentation event is durable, replayable, or safely superseded by the canonical result.
- Clients deduplicate the presentation event and canonical result rather than rendering the body twice.
- Recorder, audit, telemetry, event-log reconstruction, and model-facing history retain their declared ordering and payload contracts.
Environment
- mecatl commit lineage: current main after
409ea74e8 - Deployment shape: engine event stream; visible in mecatui
Additional context
This is Bounded work because it changes event timing, contextual-guardrail release ordering, cancellation behavior, and potentially recorder/event-log ordering. Create and approve an acceptance plan before implementation.
The owning architecture guide also needs correction: docs/architecture/agent-loop.md still describes concurrent results as being merged under a mutex rather than private execution records followed by an ordered release drain.
Activity