agentscope-ai / agentscope-ai/agentscope

[Bug]: Deleting a team leader session leaves worker workspace and MCP state behind

Open
#2,288 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
31.5k
Forks
3.5k
Avg merge
1d 23h
Merged PRs (30d)
95

Description

### Prerequisites

- [x] I have searched the existing [issues](https://github.com/agentscope-ai/agentscope/issues) and [discussions](https://github.com/agentscope-ai/agentscope/discussions), and this is not a duplicate.
- [x] This is a bug, not a usage question. (For questions, please use [Discussions](https://github.com/agentscope-ai/agentscope/discussions/new?category=general) instead.)

### Background / Description

Deleting a team leader session cascades through storage and removes the team's
worker sessions, but `SessionService` only purges the leader's workspace scope.

Before storage mutation, the service collects worker session IDs for run
cancellation and message-bus cleanup:

```python
worker_sids = await self._team_worker_session_ids(...)
all_sids = [session_id, *worker_sids]
```

This is sufficient for cancellation and bus cleanup because those operations
only require a session ID. However, workspace cleanup additionally requires the
session owner, agent ID, and persisted workspace ID.

After `storage.delete_session()` dissolves the team, the worker records no
longer exist. The final workspace cleanup therefore only calls:

```python
await workspace.purge_session(
agent_id=agent_id,
session_id=session_id,
)
```

where `agent_id` and `session_id` still refer to the leader.

This leaves worker-scoped state behind:

- stateful MCP instances and subprocesses;
- persisted `.mcp` declarations;
- session offload files;
- `_mcp_last_used` capacity records.

This also affects shared workspaces. `WorkspaceBase.purge_session()` scopes MCP
state by `(agent_id, session_id)`, so purging the leader does not implicitly
purge worker scopes.

Expected behavior:

- every session removed by the storage cascade has its workspace scope purged;
- created workers are purged along with the leader;
- invited agents only lose their borrowed team session scope;
- an invited agent's ordinary sessions and agent-level state survive;
- one failed workspace purge does not prevent cleanup of later targets.

### Error Messages

```shell
Observed deterministic cleanup calls:

{
"bus_purged": [
"leader-session",
"worker-session"
],
"workspace_purged": [
["leader-agent", "leader-session"]
]
}

Expected `workspace_purged` to also contain:

["worker-agent", "worker-session"]
```

### Steps to Reproduce

1. Create a leader agent and session.
2. Create a team from that session.
3. Add a created worker. The worker receives its own agent/session scope while
sharing the leader workspace.
4. Register worker-scoped MCP state or offload session data.
5. Delete the leader session through
`DELETE /sessions/{leader_session_id}`.
6. Observe that storage removes the team and worker records.
7. Observe that message-bus state is purged for both leader and worker sessions.
8. Inspect calls to `Workspace.purge_session()` or inspect the remaining
workspace state.

A minimal service-level regression assertion is:

```python
assert workspace.purge_session.await_args_list == [
call(
agent_id="leader-agent",
session_id="leader-session",
),
call(
agent_id="worker-agent",
session_id="worker-session",
),
]
```

On the affected implementation, only the first call is made.

### Environment

- AgentScope Version: 2.0.6
- Python Version: 3.11.7
- OS: Windows 10 (10.0.26200)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.