|
| 1 | +# subagents |
| 2 | + |
| 3 | +An agent that delegates work to subagents, and a client that shows, cancels and closes them. |
| 4 | + |
| 5 | +- Protocol: ACP v1 (the `subagents` capability). |
| 6 | +- Needs: nothing. |
| 7 | +- Package: `com.agentclientprotocol.samples.subagents`. |
| 8 | + |
| 9 | +The subagent protocol is stable in v1, but the SDK's Kotlin API for it (`@UnstableApi`) may still change, so the sample |
| 10 | +opts in explicitly. |
| 11 | + |
| 12 | +## What it shows |
| 13 | + |
| 14 | +A scripted run with no input. The client opens a session and sends two prompts, then closes the session. |
| 15 | + |
| 16 | +- The agent delegates the first prompt to three subagents. The reviewer completes, the user cancels the cleaner while |
| 17 | + its permission request is pending, and the tester fails after it started a grandchild whose feed is lost. |
| 18 | +- The agent relays the second prompt to a native agent, whose workers become nested subagents. |
| 19 | + |
| 20 | +Then the client loads the closed session again. |
| 21 | + |
| 22 | +- The agent tells the session's history again from its own record of each turn, subagents included. Each stream gets |
| 23 | + its history, then `Opened`, then live updates. |
| 24 | +- The replayed subagents start with no confirmed state and no cancel grant. The agent grants cancel again to the |
| 25 | + reviewer, which works on a new task, and the user cancels it. |
| 26 | +- The client then closes the session again. |
| 27 | + |
| 28 | +The app prints what each side sees. |
| 29 | + |
| 30 | +## How to run |
| 31 | + |
| 32 | +From the repository root: |
| 33 | + |
| 34 | +```bash |
| 35 | +./gradlew :samples:kotlin-acp-client-sample:runSubagents |
| 36 | +``` |
| 37 | + |
| 38 | +The streams of different sessions are delivered concurrently, so lines of different sessions may interleave |
| 39 | +differently from run to run. The task logs only warnings, to stderr, through `logback-subagents.xml`. |
| 40 | + |
| 41 | +One warning may appear, depending on timing. When the user cancels the cleaner, the client answers its pending |
| 42 | +permission request `cancelled`, and the agent stops the cleaner's work. If the agent's stop cancels the request before |
| 43 | +the answer arrives, the agent sends `$/cancel_request` for it, and the client logs that it got a cancel for a request |
| 44 | +it has already answered. |
| 45 | + |
| 46 | +## Where to start |
| 47 | + |
| 48 | +Read [`agent/DelegatingAgentSession.kt`](agent/DelegatingAgentSession.kt) first for the agent side and |
| 49 | +[`client/SubagentsClient.kt`](client/SubagentsClient.kt) for the client side. |
| 50 | +[`SubagentsApp.kt`](SubagentsApp.kt) wires them together in one process, and [`Transcript.kt`](Transcript.kt) holds the |
| 51 | +lines both sides write. |
| 52 | + |
| 53 | +## Files |
| 54 | + |
| 55 | +Each file is named after the SDK type it implements or mainly uses. |
| 56 | + |
| 57 | +The agent side, in package `agent`: |
| 58 | + |
| 59 | +- [`DelegatingAgentSupport.kt`](agent/DelegatingAgentSupport.kt): the `AgentSupport`; it opens a |
| 60 | + `DelegatingAgentSession`. |
| 61 | +- [`DelegatingAgentSession.kt`](agent/DelegatingAgentSession.kt): the root `AgentSession`, which delegates or relays |
| 62 | + each prompt and replays the history on `session/load`. |
| 63 | +- [`DelegatedSubagentFactory.kt`](agent/DelegatedSubagentFactory.kt): the `AgentSubagentFactory` that sends a delegated |
| 64 | + child's announcement. |
| 65 | +- [`DelegatedSubagentSession.kt`](agent/DelegatedSubagentSession.kt): the `AgentSubagentSession` of a delegated child; |
| 66 | + it runs a `DelegatedTask`. |
| 67 | +- [`DelegatedTask.kt`](agent/DelegatedTask.kt): a task, and what its work can do through the child's context. |
| 68 | +- [`SampleTasks.kt`](agent/SampleTasks.kt): the reviewer, cleaner and tester tasks, and the reviewer's task after the |
| 69 | + load. |
| 70 | +- [`SessionRecord.kt`](agent/SessionRecord.kt): what the root, each of its turns and each subagent did, which the root |
| 71 | + tells again on `session/load`. |
| 72 | +- [`RemoteProbeSubagentSession.kt`](agent/RemoteProbeSubagentSession.kt): the tester's grandchild. |
| 73 | +- [`NativeRelayedSubagentSession.kt`](agent/NativeRelayedSubagentSession.kt): the `AgentSubagentSession` of one |
| 74 | + relayed worker of a native agent, and the relay loop that builds the tree from the flat event stream. |
| 75 | +- [`FakeNativeAgent.kt`](agent/FakeNativeAgent.kt): the simulated native agent and its flat event stream; it uses no SDK |
| 76 | + type. |
| 77 | +- [`SessionUpdates.kt`](agent/SessionUpdates.kt): small builders for the updates the agent sends, and its |
| 78 | + transcript lines. |
| 79 | + |
| 80 | +The client side, in package `client`: |
| 81 | + |
| 82 | +- [`SubagentsClient.kt`](client/SubagentsClient.kt): runs the `Client`: initialize, the two prompts, close, load, close. |
| 83 | +- [`TreeClientSessionOperations.kt`](client/TreeClientSessionOperations.kt): the `ClientSessionOperations` and |
| 84 | + `SubagentOperations` shared by every session. |
| 85 | +- [`RootClientSessionOperations.kt`](client/RootClientSessionOperations.kt) and |
| 86 | + [`SubagentClientSessionOperations.kt`](client/SubagentClientSessionOperations.kt): the operations of the root and of a |
| 87 | + subagent. |
| 88 | +- [`ScriptedUser.kt`](client/ScriptedUser.kt): answers permission requests, or cancels the subagent that asks. |
| 89 | +- [`ClientTranscript.kt`](client/ClientTranscript.kt): writes the client's lines and renders sessions and updates. |
| 90 | + |
| 91 | +## Where each scenario lives |
| 92 | + |
| 93 | +The numbers are those of the section comments in each side's files. |
| 94 | + |
| 95 | +| Scenario | Agent | Client | |
| 96 | +| --- | --- | --- | |
| 97 | +| The `subagents` capability | `DelegatingAgentSession` (1: no capability, work inline) | `SubagentsClient` (1) | |
| 98 | +| Announcing a child, and the operations that receive it | `DelegatedSubagentFactory` (2) | `TreeClientSessionOperations` (2) | |
| 99 | +| A child's requests, such as a permission request | `DelegatedTask` (3) | `SubagentClientSessionOperations`, `ScriptedUser` (5) | |
| 100 | +| Links from the parent: `session_message`, session tool call content | `DelegatingAgentSession` (4) | `TreeClientSessionOperations`, `SubagentsClient` (3) | |
| 101 | +| Patches to a child | `DelegatedTask` (5) | `TreeClientSessionOperations` (3) | |
| 102 | +| A child's own stream: history, `Opened`, live updates | | `SubagentClientSessionOperations` (4) | |
| 103 | +| Cancel | `DelegatedSubagentSession` (6) | `ScriptedUser` (5, 6) | |
| 104 | +| Failure, and the SDK's `unknown` | `DelegatedSubagentSession`, `RemoteProbeSubagentSession` (7) | | |
| 105 | +| A grandchild | `RemoteProbeSubagentSession` (8) | `TreeClientSessionOperations` (3) | |
| 106 | +| Closing the root | `DelegatingAgentSession` (9) | `SubagentsClient` (7) | |
| 107 | +| Relay of a native agent, `announce(…, parent = …)` | `NativeRelayedSubagentSession` (10) | | |
| 108 | +| `session/load`: the root tells each turn again from its record | `DelegatingAgentSession` (11) | `SubagentsClient` (9), `TreeClientSessionOperations` (8) | |
| 109 | +| A replayed child's own history, its children included | `DelegatingAgentSession` (12) | | |
| 110 | +| The replayed tree, at each parent's `Opened` | | `TreeClientSessionOperations` (11) | |
| 111 | +| A fresh cancel grant after the load | `DelegatingAgentSession` (13) | `SubagentsClient` (10) | |
| 112 | +| The current state of a finished child after the load | `DelegatingAgentSession` (14) | | |
| 113 | + |
| 114 | +## Tests |
| 115 | + |
| 116 | +`SubagentsSampleTest` runs the same script, on one thread and on `Dispatchers.Default`, and checks its key events, so |
| 117 | +`./gradlew check` fails if the sample breaks. `agent/DelegatedWorkTest` checks how a task's end maps to its `idle` stop |
| 118 | +reason. Both are under `src/test`. |
0 commit comments