agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Feature]: Clarify post-offload background tool event delivery semantics

Open breezeFur opened this issue 2 months ago • 1 comments

Summary

Confirm or deny at-least-once semantics for post-offload background tool events, and publish the supported delivery contract for streaming consumers.

Context

Follow-up requested in the approved review of #3299 (review 5333464556; inline comment 4118119631), related to #2681. This is intentionally separate from the Reactor Context propagation fix and is not a blocker for #3299.

Current behavior

AsyncToolMiddleware preserves the caller-owned emitter identity when propagating the Reactor Context into detached tool execution. Completing the foreground acting sink after the offload timeout does not close that external emitter. Direct background emissions may still reach a live consumer; a closed consumer stream may discard them. Separately, returned tool events are buffered and their text is pushed through the inbox for the next reasoning step, with a wakeup enqueued.

These are distinct channels. A consumer observing both may see overlapping content twice; the current code does not establish an end-to-end at-least-once guarantee, exactly-once delivery, or cross-channel deduplication. In particular, emitter delivery and inbox delivery should not be assumed to be interchangeable or identical payloads.

AsyncToolMiddlewareTest.backgroundExecutionRetainsContextAfterOffload asserts assertEquals(1, forwarded.size()), pinning direct emission into a still-open external emitter after foreground completion. Any future change must deliberately revisit that compatibility expectation.

Questions to resolve

  • Which delivery guarantees apply to direct streaming events and inbox results after offload, both separately and together?
  • Who owns emitter lifetime, and what should callers expect on completion, cancellation, disconnection, or delivery failure?
  • Are duplicate observations intentional, and if deduplication is required, which stable identifiers and component own it?
  • Do retry, ordering, or persistence requirements follow from the chosen guarantee?

Acceptance criteria

  • [ ] Confirm or deny at-least-once semantics, distinguishing per-channel guarantees from combined observations.
  • [ ] Document emitter ownership, loss/duplicate possibilities, and consumer responsibilities in user-facing streaming/async-tool documentation.
  • [ ] Decide whether routing, lifecycle, retry, or deduplication changes are needed; track any implementation separately if appropriate.
  • [ ] Add or update deterministic tests for the agreed open/closed-emitter and inbox behavior, explicitly reviewing the existing post-offload assertion.
  • [ ] Link the decision and documentation back to #3299 and #2681.

Scope

This issue tracks a contract decision and its documentation/tests. It does not claim that the current behavior is a confirmed bug or authorize silently changing event routing in #3299.

breezeFur avatar Sep 28 '26 09:09 breezeFur

Issue Evaluation

Category: enhancement | Status: Evaluated — decision needed from maintainers

We verified the described behaviour against the current code, using the pending PR #3299 branch (fix/2681-async-tool-reactor-context) as the reference implementation, since the claims are about the post-offload path introduced there.

Claim-by-claim result

Claim Result Evidence
Reactor Context (incl. caller-owned emitter identity) is propagated into the detached subscription Confirmed AsyncToolMiddleware.java:103-132 (PR #3299) — .contextWrite(ctx -> ctx.putAll(callerContext))
Foreground acting sink completes after offload while the external emitter stays open Confirmed AsyncToolMiddleware.java:258-269 (sink completion) vs :204-210 (detached subscription still active)
Background emissions reach a live consumer, a closed stream may drop them Confirmed as intended behaviour; drop path is documented in code but not covered by a test comment block AsyncToolMiddleware.java:117-130
Returned tool results travel a second, independent channel (buffer → inbox → next reasoning step + wakeup) Confirmed AsyncToolMiddleware.java:133-138, :283-327; drained by InboxMiddleware.java:94-107, :158-179
Test pins exactly one post-offload direct emission Confirmed AsyncToolMiddlewareTest.java:213 — assertEquals(1, forwarded.size())
No user-facing doc states the delivery contract Confirmed only generic ToolEmitter progress docs (docs/v1/en/docs/task/tool.md, "Streaming Tools") and one release-note bullet mentioning background tool results; no statement of guarantees, emitter ownership or dedup

Assessment

This is a legitimate contract-clarification task, not a confirmed code bug: the code comments in PR #3299 already state explicitly that the two channels are "not an exactly-once or deduplicated delivery contract" and link back to this issue. Consumers that observe both the direct emitter stream and inbox-delivered results must not assume de-duplicated delivery today.

Feasibility: feasible, docs + tests only, no behavioural change required for the first increment. Scope: agentscope-harness (AsyncToolMiddleware, AsyncToolMiddlewareTest) plus the v1/v2 tool & streaming docs (en + zh). Compatibility: documenting current behaviour is non-breaking; any change to routing/dedup/lifetime would be a behavioural change and needs its own tracked issue.

Decisions required before a docs/test PR can be written (we deliberately did not open one, because answering these is a maintainer policy call, not a mechanical fix):

  1. Per-channel guarantee to publish: at-least-once for inbox-delivered results, best-effort for post-offload direct emissions, or something else?
  2. Is duplicate observation across the two channels intentional (and therefore documented as such), or should dedup be introduced — and if so, keyed on which stable identifier, owned by which component?
  3. Emitter lifetime ownership: should the framework close/complete anything on offload, or remains it strictly caller-owned?
  4. Should the pinned assertion assertEquals(1, forwarded.size()) be kept as a compatibility contract, and do we want a deterministic test for the closed-consumer drop path (currently untested)?

Once 1-4 are decided we can generate the doc + test PR in one pass and link it back to #3299 and #2681.


Automated evaluation by github-manager-bot

oss-maintainer avatar Sep 28 '26 09:09 oss-maintainer