Skip to content

fix(a2a): interrupt the session of a stopped task in BaseReActAgentRunner - #3991

Merged
jujn merged 2 commits into
agentscope-ai:mainfrom
guslegend0510:fix/a2a-runner-stop-session
Nov 6, 2026
Merged

jujn merged 2 commits into
agentscope-ai:mainfrom
guslegend0510:fix/a2a-runner-stop-session

Conversation

@guslegend0510

@guslegend0510 guslegend0510 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT (main)

Description

问题

A2A 服务端取消任务时会调 AgentRunner.stop(taskId)。BaseReActAgentRunner.stop() 调的是无参的 ReActAgent.interrupt(),它只打断默认会话(__anon__/<defaultSessionId>)。#3938 以后,任务按请求的 userId 和 contextId 在各自的会话里运行(streamEvents(msgs, RuntimeContext)),所以:

  • 被取消的任务停不下来,会一直跑到结束;
  • 如果 runner 每次返回同一个 agent 实例(Spring Boot Starter 的 ReActAgent 是单例 bean),同一个 agent 上走默认会话的其他调用会被误打断,比如应用自己调的 agent.call(msgs)。

在当前 main 上用真实 ReActAgent 复现过:取消后任务那条流 5 秒内没有结束,而同一 agent 上的默认会话调用返回了 "I noticed that you have interrupted me. What can I do for you?"。

改动

  • BaseReActAgentRunner 缓存 agent 时,连同启动这个任务用的 RuntimeContext 一起存,stop() 改调 agent.interrupt(context),只打断这个任务所在的会话。
  • 缓存仍按实例比较,旧流收尾不会删掉同一 taskId 重启的任务,原有保护不变。
  • 不改公开 API:AgentRunner 接口、ReActAgentWithBuilderRunner、Starter 的 runner 都没动。

不在本 PR 范围:执行器取消后 blockLast() 不返回(另提)。

测试

  • AgentRunnerTest 新增 testStopInterruptsTaskSessionOnly:用真实 ReActAgent 和持续吐分片的假模型,同一个 agent 上同时跑一个 A2A 任务(userId=alice,sessionId=context-a)和一个默认会话的调用;stop(taskId) 后任务结束、默认会话的调用继续运行。在 main 上先确认失败(任务那条流超时),修复后通过。
  • 原来断言无参 interrupt() 的 mock 测试改为断言 interrupt(RuntimeContext),其中一条核对传入的就是启动任务时的那个 context。
  • 本地(JDK 17,macOS):mvn -B -T1C clean verify 99 个模块全部通过(12,085 个测试,0 失败,225 跳过)。AgentRunnerTest 连跑 3 次通过。补丁覆盖率:改动的可执行行 9/9。

Related to #3938
Part of #3990

…nner

stop() called the no-arg ReActAgent.interrupt(), which targets the
default session. Since agentscope-ai#3938 a task runs in the session of its userId
and contextId, so a cancelled task kept running, and on a shared agent
(the starter's singleton bean) another caller's default-session call was
interrupted instead.

Keep the RuntimeContext a task was started with next to its agent and
interrupt that session on stop().
@jujn
jujn merged commit 6b699f3 into agentscope-ai:main Nov 6, 2026
4 of 5 checks passed
xzxiaoshan added a commit that referenced this pull request Nov 12, 2026
#4149)

## Summary

Scanned the last 5 pages of PRs (#3941–#4147, 125 PRs): 14 had red CI,
and 9 of those failed on exactly one OS while the other OS stayed green
on the same commit — the classic `-T1C` parallel-build starvation
signature (the both-OS failures were compile/spotless issues and are out
of scope). This PR fixes every distinct flaky test class found that way,
plus two narrowly-scoped hardenings the fixes depend on.

| Test / class | Failed OS | Victim PRs | Evidence | Fix |
|---|---|---|---|---|
| `AguiMvcControllerTest#eventStreamSignals…` | ubuntu ×2 | #4112, #4143
| 5s latch deadline hit while sibling tests in the class ran 5–9s under
the same load | shared 30s await constant (all waits in the class share
the same executor starvation exposure) |
| `SubagentRunIdChainTest` (both methods) | windows ×3 | #4091, #4025,
#4142 | blocking read starved just past 10s (10.04–10.84s elapsed); same
test 0.414s on ubuntu | block budget 10s → 30s |
| `InboxMiddlewareTest#notificationReaches…` | ubuntu ×2 | #3991, #4093
| assertion matched the memory-consolidation prompt instead of the
reasoning request | `MockModel` recordings made thread-safe (see below)
|
| `GatewayEventsWebSocketTest#sameOrigin…` | windows ×2 | #4072, #4094 |
~0.1s clean close with no frames; job log shows the preceding 403'd
upstream upgrade left a dying pooled socket (`Connection prematurely
closed BEFORE opening handshake is complete`) that the next proxied
socket reused | stand-in control plane now answers rejected upgrades
with `Connection: close` so the gateway pool discards the socket |
| `TurnLeaseServiceTest#heartbeatErrors…` | windows | #4075 |
`await(70ms) isFalse` anchored to when the starved test thread resumes,
not to lease acquisition | bounds re-anchored to
elapsed-time-since-acquisition (fires within 2s, not before 120ms;
deadline 150ms, ticks 50ms) |
| `SandboxConnectorRedirectTest#commandWaits…` | windows | #4058 |
capability probe inherited the 200ms command-transport budget and died
at 199ms before any command was submitted | probe budget floor (see
CommandExecutor below) |
| `GracefulShutdownManager` singleton leak | local | found during
verification | `resetForTesting()` reset every field except `config`, so
a `PT0.1S` config leaked across tests and failed `defaultConfig`
whenever filesystem class order placed it after `shutdownTimeoutSignal`
| reset now also restores `GracefulShutdownConfig.DEFAULT` |

## Shared test-util fix

`MockModel` recorded calls from whichever thread drives the agent — the
turn thread and background memory maintenance — into a plain `ArrayList`
plus unsynchronized `int`/`last*` fields. Two concurrent adds race the
`elementData[size] / size++` update and can silently drop an entry; that
is exactly the #4093/#3991 signature (the reasoning request vanished,
only the post-call consolidation request survived). Recordings are now a
`CopyOnWriteArrayList` with `AtomicInteger`/`volatile` siblings —
order-preserving, atomic per add, stable to iterate while a call is in
flight. Complements the iteration fix proposed in #4133.

## Non-test changes (called out explicitly)

- **`CommandExecutor`** — the `/capabilities` pre-flight probe now uses
`max(configured per-attempt, 2s)` for its budgets. Strictly a widening:
identical for default (60s) and ≥2s configurations; only sub-2s configs
change, and only for the probe — command exchanges keep the configured
budget. Side benefit: the old code passed the same 200ms as the shared
exchange deadline, which made the intended `CAPABILITY_MAX_ATTEMPTS = 3`
retry dead code; the floor lets the retry actually run. The test-only
alternative (raising the test's 200ms config and the server delays to
match) would add ~1.5–2s of real test wall time, so the 2-line
production fix is the smaller change.
- **`GracefulShutdownManager.resetForTesting`** — one added line
restoring `config` to default, inside a method documented "Intended for
testing and demo purposes only" (no production callers; production code
never calls `setConfig`).

## Constraint compliance

- **No happy-path slowdown:** every widened number is a failure-path
deadline only; all awaits/latches return the moment the signal arrives,
and no sleeps were added (`TurnLeaseServiceTest` actually drops a forced
70ms wait).
- **Minimal widenings, anchored to measurements:** 5s→30s (deadline hit
twice; repo precedent from #3726 for CI-starvation waits), 10s→30s (3×
the observed 10.04–10.84s), probe floor 2s (10× the 199ms miss),
TurnLease window [120ms, 2s] (bug population ~50ms vs legitimate
≥150ms).

## Test plan

- [x] Full reactor `mvn -B -T2C clean verify` green locally (4m38s wall
clock; all seven fixed areas' suites green)
- [x] `JsonFileAgentStateStoreTest` (8 symlink cases) excluded locally
only — pre-existing Windows-symlink-privilege limitation, reproduced
identically on a pristine `main` baseline, unrelated to this diff
- [ ] CI on both OSes

Follow-up noted during review: the production control-plane hub behind
`/api/v1/events` lives outside this repo; if it also answers rejected
upgrades with a keep-alive 403 and closes, the gateway can still poison
a subsequent proxied socket in production — worth checking against the
real hub separately.
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.

3 participants