You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "yunfengzhou-hub (via GitHub)" <gi...@apache.org> on 2026/10/16 05:48:50 UTC

[PR] [docs] Improve the Sub-Agents guide [flink-agents]

yunfengzhou-hub opened a new pull request, #1268:
URL: https://github.com/apache/flink-agents/pull/1268

   Linked issue: Closes #1258
   
   ### Purpose of change
   
   The Sub-Agents page covered registration and the two ways a call can start, and stopped there. A reader could not work out from it how a call behaves once it is running. This rewrites the page around what a user has to know to run a sub-agent in a job, and keeps every statement to public API and observable behavior.
   
   - Declare: both registration forms of an internal child, with the criterion that chooses between them, and a workflow agent and a ReActAgent as children. An external sub-agent is a setup class, and the two execution modes are separated by what the reader can observe about their remote: whether one request returns the result, or the remote hands back a run to query later.
   - Invoke: model-driven and caller-driven delegation, both ending in the same `submit` call, so the handle is defined once where the reader first meets it. A new section covers what an open handle can do: batching, continuing a session, and cancelling.
   - Outcome: what a call returns, how a failure reaches a caller versus a model, what a replay reuses rather than repeats, and what is reported under the `subagent` scope. Restrictions collects limits that were scattered or absent.
   - Navigation: the page now follows the ReAct Agent introduction, since it uses `ReActAgent` as one of its examples.
   
   ### Tests
   
   - Every Java snippet was assembled into one source file and compiled with `javac --release 11` against freshly built `api`, `plan`, and `runtime`. Clean.
   - All 17 Python snippets parse, and the registration snippets were executed: both YAML forms load through the loader (agent-level and shared document level), `AgentPlan.from_agent` builds with both external descriptor registrations, descriptor arguments bind to the setup's fields, `status_poll_interval_millis` defaults to 500, and a deferred `prepare` returns a working `(id, call, reconciler)` triple.
   - 103 Python sub-agent tests passed across `api`, `plan`, and `runtime`.
   - Hugo extended 0.110.0, the version `docs/setup_hugo.sh` pins, builds the site with no errors or warnings. Every `{{< ref >}}` and in-page anchor in the changed pages was resolved against the target pages' real headings.
   - Statements no test covers were checked against the implementation directly, among them the reserved-prefix rule for MCP-advertised tools, the absence of sub-agent reporting on the caller-driven path, and the two YAML registration forms.
   - `git diff --check` passed.
   - No Flink or cross-language end-to-end jobs were run; this PR changes documentation only.
   
   ### API
   
   No public API or runtime behavior changes.
   
   ### Documentation
   
   - [ ] `doc-needed`
   - [ ] `doc-not-needed`
   - [x] `doc-included`
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   - [x] Yes
   - [ ] No
   
   Generated-by: Qoder (Qwen 3.8-Max)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "yunfengzhou-hub (via GitHub)" <gi...@apache.org>.
yunfengzhou-hub commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4300857903


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -24,30 +24,170 @@ specific language governing permissions and limitations
 under the License.
 -->
 
-A sub-agent handles a task delegated by a parent agent. An internal sub-agent runs
-an agent workflow in the same job. An external sub-agent calls an agent service
-through a `SubagentSetup` implementation.
+## Overview
 
-## Declare an internal sub-agent
+A sub-agent is an agent that another agent delegates a task to. The parent hands it
+input, the sub-agent does the work, and the parent receives the outcome as a
+`SubagentResult`. Use one when the delegated work is multi-step or long-running and
+should run isolated from the parent.
 
-Use `InternalSubagentResourceDescriptorBuilder` to attach a description and input
-schema to a child agent. The builder returns a normal resource descriptor for `addResource`
-or `add_resource`.
+The work runs either inside the job or outside it. An *internal* sub-agent is a
+child `Agent`, including a `ReActAgent`, compiled into the parent's plan. The
+child's own actions and events run in the same job. An *external* sub-agent has no
+plan of its own. The work happens in your own code or in a remote agent service,
+outside the parent's event loop. It takes one of two forms, *deferred* or *async
+(pub/sub)*.
 
+Either way, the call is initiated by the model or by you. In *model-driven
+delegation* you list the sub-agent on a chat model setup and the model chooses when
+to delegate. In *caller-driven delegation* one of your actions submits the task
+itself.
+
+Every sub-agent call runs through
+[durable execution]({{< ref "docs/development/create-agent/actions#durable-execution" >}}).
+There is no durability toggle and no sub-agent timeout option.
+
+## Declare a sub-agent
+
+A sub-agent enters the parent's plan as a resource registered under
+`ResourceType.AGENT`. The name you register it under is the name that invocations and
+metrics use. What you register depends on the form. An internal sub-agent registers
+a child `Agent`. An external sub-agent registers a descriptor that names your setup
+class, and a YAML file can declare it.
+
+### Internal: compile a child agent
+
+Nothing in a child's own code marks it as a sub-agent. The child is an ordinary
+agent with its own resources, actions, and event loop, and it runs standalone
+exactly as written. A parent registers it as an `AGENT` resource, and that
+registration is the whole of the difference. It gives the child a name in the
+parent's plan. It can also give the child an input schema, so a chat model can call
+it. And it supplies the caller that submits input and awaits the child's outputs.
+
+Any `Agent` can serve as the child. The two common kinds are a workflow agent and a
+`ReActAgent`. Either way, the child's plan is compiled into the parent's when the
+parent's plan is built.
+
+#### A workflow agent as the child
+
+The example child owns a chat model and answers each review request with its

Review Comment:
   Fixed in the rebase. The restriction is gone, the Declare section links to Declaring sub-agents, and the internal section states the agent field. Loaded all three YAML forms against the new loaders to confirm.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "yunfengzhou-hub (via GitHub)" <gi...@apache.org>.
yunfengzhou-hub commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4309929964


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -24,35 +24,136 @@ specific language governing permissions and limitations
 under the License.
 -->
 
-A sub-agent handles a task delegated by a parent agent. An internal sub-agent runs
-an agent workflow in the same job. An external sub-agent calls an agent service
-through a `SubagentSetup` implementation.
+## Overview
 
-To declare parent and child workflows in YAML, see
-[Declaring sub-agents]({{< ref "docs/development/create-agent/yaml#declaring-sub-agents" >}}).
-YAML supports references to internal child Agents in the same file and descriptors
-for external sub-agents.
+A sub-agent is an agent that another agent delegates a task to. The parent hands it
+input, the sub-agent does the work, and the parent receives the outcome as a
+`SubagentResult`. Use one when the delegated work is multi-step or long-running and
+should run isolated from the parent.
 
-## Declare an internal sub-agent
+The work runs either inside the job or outside it. An *internal* sub-agent is a
+child `Agent`, including a `ReActAgent`, compiled into the parent's plan. The
+child's own actions and events run in the same job. An *external* sub-agent has no
+plan of its own. The work happens in your own code or in a remote agent service,
+outside the parent's event loop. It takes one of two forms, *deferred* or *async
+(pub/sub)*.
 
-Use `InternalSubagentResourceDescriptorBuilder` to attach a description and input
-schema to a child agent. The builder returns a normal resource descriptor for `addResource`
-or `add_resource`.
+Either way, the call is initiated by the model or by you. In *model-driven
+delegation* you list the sub-agent on a chat model setup and the model chooses when
+to delegate. In *caller-driven delegation* one of your actions submits the task
+itself.
 
+Every sub-agent call runs through
+[durable execution]({{< ref "docs/development/create-agent/actions#durable-execution" >}}).
+
+## Declare a sub-agent
+
+A sub-agent enters the parent's plan as a resource registered under
+`ResourceType.AGENT`. The name you register it under is the name that invocations and
+metrics use. What you register depends on the form. An internal sub-agent registers
+a child `Agent`. An external sub-agent registers a descriptor that names your setup
+class. A YAML file declares either form, as
+[Declaring sub-agents]({{< ref "docs/development/create-agent/yaml#declaring-sub-agents" >}})
+describes.
+
+### Internal: compile a child agent {#declare-an-internal-sub-agent}
+
+Nothing in a child's own code marks it as a sub-agent. The child is an ordinary
+agent with its own resources, actions, and event loop, and it runs standalone
+exactly as written. A parent registers it as an `AGENT` resource, and that
+registration is the whole of the difference. It gives the child a name in the
+parent's plan. It can also give the child an input schema, so a chat model can call
+it. And it supplies the caller that submits input and awaits the child's outputs.
+
+Any `Agent` can serve as the child. The two common kinds are a workflow agent and a
+`ReActAgent`. Either way, the child's plan is compiled into the parent's when the
+parent's plan is built.
+
+#### A workflow agent as the child
+
+Any workflow agent can serve as the child. The one used through this page is built
+the way
+[Request and Response Example]({{< ref "docs/development/calling-llms/chat#usage-example" >}})
+builds an agent that owns a chat model, and this page calls it `ReviewAgent`. It has
+a connection resource, a model setup, an action that turns an `InputEvent` into a
+`ChatRequestEvent`, and an action that turns the `ChatResponseEvent` into
+`OutputEvent`s.
+
+The one part a sub-agent changes is how that first action reads its input. A
+caller-driven call passes the object the caller submitted. A model-driven call
+passes the arguments map built from the input schema.
+
+{{< tabs "Read the Input in a Child Action" >}}
+
+{{< tab "Python" >}}
+```python
+# Inside ReviewAgent.
+
+
+@action(EventType.InputEvent)
+@staticmethod
+def ask(event: Event, ctx: RunnerContext) -> None:
+    task = InputEvent.from_event(event).input
+    # A model-driven call passes the arguments map; a caller-driven call
+    # passes the submitted object.
+    diff = task["code"] if isinstance(task, dict) else str(task)
+    ctx.send_event(
+        ChatRequestEvent(
+            model="review_model",
+            messages=[ChatMessage.user(f"Review this diff:\n{diff}")],
+        )
+    )
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
 ```java
-import org.apache.flink.agents.api.resource.ResourceType;
-import org.apache.flink.agents.api.subagent.InternalSubagentResourceDescriptorBuilder;
+// Inside ReviewAgent.
 
-parent.addResource(
-    "reviewer",
-    ResourceType.AGENT,
-    new InternalSubagentResourceDescriptorBuilder()
-        .agent(new ReviewAgent())
-        .description("Review code for correctness and potential defects")
-        .inputType(ReviewRequest.class)
-        .build());
+@Action(EventType.InputEvent)
+public static void ask(Event event, RunnerContext ctx) throws Exception {
+    Object task = InputEvent.fromEvent(event).getInput();
+    // A model-driven call passes the arguments map; a caller-driven call
+    // passes the submitted object.
+    String diff =
+            task instanceof Map
+                    ? String.valueOf(((Map<?, ?>) task).get("code"))
+                    : String.valueOf(task);
+    ctx.sendEvent(
+            new ChatRequestEvent(
+                    "reviewModel",
+                    List.of(new ChatMessage(MessageRole.USER, "Review this diff:\n" + diff))));
+}
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
 
+While a call runs, the child behaves like a scoped agent of its own:
+
+- Its `OutputEvent`s are accumulated, in order, into the call's result instead of
+  landing on the parent's context. See
+  [the result](#the-result) for the shape.
+- It resolves resources against its own plan. Register the resources a child uses,
+  such as its chat model connection, on the child itself. In Python a resource
+  registered solely on the parent is not visible to the child.
+- Its sensory and short-term memory start empty for each call, including calls
+  continuing a session, and are shared by the child's actions within that call.
+  Long-term memory, when configured, is shared with the caller.
+- It may register and await sub-agents of its own, so nesting is unbounded in
+  depth.
+
+Registering the child takes one of two forms. You register a descriptor, or you
+register the `Agent` itself. Which one you need follows from who calls it. A chat
+model can delegate only to a sub-agent it can describe and build arguments for. So a
+child exposed to a model needs the descriptor form, and that descriptor carries a

Review Comment:
   Done, with one deviation: the two calling modes are named in a single sentence rather than redefined, because Overview already defines them and a second definition would give two places to keep in step. The table follows, then the descriptor form, the direct form, and the YAML pointer. The description row also caught a contradiction, since the section required a description while the delegate section said a missing one falls back to a generic one. build() requires only agent, so the table is right and that sentence is gone.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "sangkyoonnam (via GitHub)" <gi...@apache.org>.
sangkyoonnam commented on PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#issuecomment-6228889880

   Checked `6c26529f`: all five of my comments are addressed, and the Java Ollama connection in the `ReActAgent` example now sets `endpoint`. Nothing more from my side.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "yunfengzhou-hub (via GitHub)" <gi...@apache.org>.
yunfengzhou-hub commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4300860093


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -307,30 +443,854 @@ parent.addResource(
                                 + "\"required\":[\"diff\"]}")
                 .build());
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The framework does not check the schema against the prompt. A field the prompt does
+not use is ignored, and a placeholder no field fills is left in the prompt as literal
+text. So keep the two in step when you change either.
+
+### External: write a setup class
+
+An external sub-agent has no plan of its own, and it is not compiled into the
+parent's. The work happens in your own code or in a remote agent service. The parent
+only submits to it and waits for the outcome. The framework cannot perform that work
+for you, because it does not know your protocol. So you write the part that performs
+one invocation, and the framework supplies everything around it. It runs your code
+under durable execution, and after a failover it replays the recorded result instead
+of calling your code again.
+
+What you write is a setup class. Its only job is to carry out one invocation, so it
+declares no actions and no events of its own. It extends a framework base class and
+implements the methods that class declares.
+
+Registration names the setup class in a descriptor, and both languages construct the
+setup from the descriptor at runtime. Registering a setup instance instead is
+rejected.
+
+The invocation itself is carried out in one of two modes, and which one you write
+follows from your remote interface. When one request returns the result, write a
+*deferred* setup. You own the whole interaction, and the framework only holds it back
+until the caller asks for the result. When the remote instead hands back a run that
+you later query for status and result, write an *async (pub/sub)* setup. The base
+class owns that composition of publishing, polling, and fetching, and you supply the
+transport calls it is built from.
+
+#### Deferred execution
+
+A deferred setup sends nothing when the caller submits. It only registers the
+invocation. The request goes out when the caller first awaits the result. So a call
+that is cancelled instead never issues its request.
+
+What you write is the interaction itself, in the `prepare` method of your setup class.
+`prepare` runs once per invocation and returns the durable call that carries it. The
+example is one blocking request, and whatever else your call does is yours to decide,
+because the framework does not know your protocol.
 
+The framework assigns `session_id` and `call_id` before it calls `prepare`, and they
+are the only values that a replay reproduces. The durable id you build from them is
+the key that recovery matches a recorded result against. When the key matches, your
+call is not run again. So build the key from those two values alone. Anything else in
+it, a timestamp for instance, makes a replay look up a different key and run the work
+a second time. Passing the same values to your own remote lets it recognize a
+re-issued request as the same one.
+
+The reconciler is optional. Omitting it leaves one crash window uncovered, and
+[Durability and failover](#durability-and-failover) describes that window. Supply one
+when the work cannot be repeated.
+
+{{< tabs "Supply a Reconciler" >}}
+
+{{< tab "Python" >}}
 ```python
-reviewer = ReActAgent(
-    chat_model=chat_model_descriptor,
-    prompt=Prompt.from_text(
-        "Review the following code diff and report any problems:\n\n{diff}"
+def prepare(
+    self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+) -> PreparedTriple:
+    key = f"{session_id}#{call_id}"
+
+    def call() -> SubagentResult:
+        return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+
+    # Consulted only during recovery, when this call is revisited and no result was
+    # recorded for it. Return the recovered outcome, or raise the recovered failure.
+    # It takes no arguments.
+    def reconcile() -> SubagentResult:
+        # fetch_review_by_key is your own lookup; not part of Flink Agents.
+        found = fetch_review_by_key(self.endpoint, key)
+        if found is None:
+            raise RuntimeError("the review service has no record of this call")
+        return SubagentResult.ok(found)
+
+    return (key, call, reconcile)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+// Inside the DurableCallable that prepare returns, alongside getId() and call().
+
+// Consulted only during recovery, when this call is revisited and no result was
+// recorded for it. Return the recovered outcome, or throw the recovered failure.
+@Override
+public Callable<SubagentResult> reconciler() {
+    return () -> {
+        // fetchReviewByKey is your own lookup; not part of Flink Agents.
+        String found = fetchReviewByKey(endpoint, key);
+        if (found == null) {
+            throw new IllegalStateException("the review service has no record of this call");
+        }
+        return SubagentResult.ok(found);
+    };
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+{{< tabs "Deferred Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.runner_context import RunnerContext
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.deferred_subagent import (
+    DeferredSubagentSetup,
+    PreparedTriple,
+)
+
+
+def post_review(endpoint: str, prompt: Any, key: str) -> str:
+    """Your own call to the review service; not part of Flink Agents.
+
+    Keying the work lets the service treat a re-issued request as the same one.
+    """
+    ...
+
+
+class RemoteReviewSetup(DeferredSubagentSetup):
+    endpoint: str = ""
+
+    def prepare(
+        self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+    ) -> PreparedTriple:
+        key = f"{session_id}#{call_id}"
+
+        def call() -> SubagentResult:
+            try:
+                return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+            except Exception as e:
+                # An expected failure is a value; only a system fault should escape.
+                return SubagentResult.error(e)
+
+        # The first value is the key recovery matches a recorded result against, so
+        # it is built from the two ids alone. The third is the reconciler.
+        return (key, call, None)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.context.DurableCallable;
+import org.apache.flink.agents.api.context.RunnerContext;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseDeferredSubagentSetup;
+
+/**
+ * Your own call to the review service; not part of Flink Agents. Keying the work lets
+ * the service treat a re-issued request as the same one.
+ */
+static String postReview(String endpoint, Object prompt, String key) {
+    ...
+}
+
+public class RemoteReviewSetup extends BaseDeferredSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteReviewSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected DurableCallable<SubagentResult> prepare(
+            RunnerContext ctx, Object prompt, String sessionId, String callId) {
+        // Built from the two ids alone, so a replay reproduces it.
+        String key = sessionId + "#" + callId;
+        return new DurableCallable<SubagentResult>() {
+            @Override
+            public String getId() {
+                // The key recovery matches a recorded result against.
+                return key;
+            }
+
+            @Override
+            public Class<SubagentResult> getResultClass() {
+                return SubagentResult.class;
+            }
+
+            @Override
+            public SubagentResult call() {
+                try {
+                    return SubagentResult.ok(postReview(endpoint, prompt, key));
+                } catch (Exception e) {
+                    // An expected failure is a value; only a system fault should escape.
+                    return SubagentResult.error(e);
+                }
+            }
+        };
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup under the name invocations will use. Every descriptor argument
+beyond the built-in ones is configuration for the setup to read back. In Python it
+reads them as model fields, and in Java through the public
+`(ResourceDescriptor, ResourceContext)` constructor. The `endpoint` above is such
+configuration, not a built-in transport.
+
+{{< tabs "Register an External Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
+parent.add_resource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteReviewSetup",
+        endpoint="https://example.com/review",
+        description="Review code using the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
     ),
 )
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteReviewSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/review")
+        .addInitialArgument("description", "Review code using the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_reviewer
+        clazz: my_app.agents.RemoteReviewSetup
+        endpoint: https://example.com/review
+        description: Review code using the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The YAML form of that registration is the `subagents` list of an agent entry, as in
+the YAML tab above. That list can also sit at the shared document level, outside
+`agents:`, so that several agents in one file reuse it. A chat model setup declared in
+the same file also takes a `subagents` argument, but it is a list of registered names
+rather than a list of entries. See
+[delegate from a chat model](#delegate-from-a-chat-model).
+
+Two constraints apply to the entry:
+
+- No class alias is defined for `AGENT` resources, so `clazz` is always a
+  fully-qualified class path.
+- The entry's language must match the loader. A Python YAML file names a Python
+  class and may not set `type: java`. A Java YAML file must set `type: java`.
+
+#### Async (pub/sub) execution
+
+An async sub-agent talks to a service that runs the task for you. The service accepts
+the work and answers at once, and you query it later for the run's status and
+result.
+
+An async setup publishes the run when the caller submits, through one durable POST.
+Waiting for the result then polls the run's status until it reaches a terminal state,
+and fetches it.
+
+That is the difference from the deferred mode, which sends nothing when the caller

Review Comment:
   Done, and the async execution ref link went with the type argument. Invoke now opens on where the result goes. The prepare, durable id, and reconciler contracts are unchanged.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "yunfengzhou-hub (via GitHub)" <gi...@apache.org>.
yunfengzhou-hub commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4300861527


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -24,30 +24,170 @@ specific language governing permissions and limitations
 under the License.
 -->
 
-A sub-agent handles a task delegated by a parent agent. An internal sub-agent runs
-an agent workflow in the same job. An external sub-agent calls an agent service
-through a `SubagentSetup` implementation.
+## Overview
 
-## Declare an internal sub-agent
+A sub-agent is an agent that another agent delegates a task to. The parent hands it
+input, the sub-agent does the work, and the parent receives the outcome as a
+`SubagentResult`. Use one when the delegated work is multi-step or long-running and
+should run isolated from the parent.
 
-Use `InternalSubagentResourceDescriptorBuilder` to attach a description and input
-schema to a child agent. The builder returns a normal resource descriptor for `addResource`
-or `add_resource`.
+The work runs either inside the job or outside it. An *internal* sub-agent is a
+child `Agent`, including a `ReActAgent`, compiled into the parent's plan. The
+child's own actions and events run in the same job. An *external* sub-agent has no
+plan of its own. The work happens in your own code or in a remote agent service,
+outside the parent's event loop. It takes one of two forms, *deferred* or *async
+(pub/sub)*.
 
+Either way, the call is initiated by the model or by you. In *model-driven
+delegation* you list the sub-agent on a chat model setup and the model chooses when
+to delegate. In *caller-driven delegation* one of your actions submits the task
+itself.
+
+Every sub-agent call runs through
+[durable execution]({{< ref "docs/development/create-agent/actions#durable-execution" >}}).
+There is no durability toggle and no sub-agent timeout option.
+
+## Declare a sub-agent
+
+A sub-agent enters the parent's plan as a resource registered under
+`ResourceType.AGENT`. The name you register it under is the name that invocations and
+metrics use. What you register depends on the form. An internal sub-agent registers
+a child `Agent`. An external sub-agent registers a descriptor that names your setup
+class, and a YAML file can declare it.
+
+### Internal: compile a child agent

Review Comment:
   Fixed with explicit heading ids, so yaml.md needs no change. Added a check that resolves every inbound ref to this page against its real headings; it reports 5 links, 0 broken.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "yunfengzhou-hub (via GitHub)" <gi...@apache.org>.
yunfengzhou-hub commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4300856301


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -307,30 +443,854 @@ parent.addResource(
                                 + "\"required\":[\"diff\"]}")
                 .build());
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The framework does not check the schema against the prompt. A field the prompt does
+not use is ignored, and a placeholder no field fills is left in the prompt as literal
+text. So keep the two in step when you change either.
+
+### External: write a setup class
+
+An external sub-agent has no plan of its own, and it is not compiled into the
+parent's. The work happens in your own code or in a remote agent service. The parent
+only submits to it and waits for the outcome. The framework cannot perform that work
+for you, because it does not know your protocol. So you write the part that performs
+one invocation, and the framework supplies everything around it. It runs your code
+under durable execution, and after a failover it replays the recorded result instead
+of calling your code again.
+
+What you write is a setup class. Its only job is to carry out one invocation, so it
+declares no actions and no events of its own. It extends a framework base class and
+implements the methods that class declares.
+
+Registration names the setup class in a descriptor, and both languages construct the
+setup from the descriptor at runtime. Registering a setup instance instead is
+rejected.
+
+The invocation itself is carried out in one of two modes, and which one you write
+follows from your remote interface. When one request returns the result, write a
+*deferred* setup. You own the whole interaction, and the framework only holds it back
+until the caller asks for the result. When the remote instead hands back a run that
+you later query for status and result, write an *async (pub/sub)* setup. The base
+class owns that composition of publishing, polling, and fetching, and you supply the
+transport calls it is built from.
+
+#### Deferred execution
+
+A deferred setup sends nothing when the caller submits. It only registers the
+invocation. The request goes out when the caller first awaits the result. So a call
+that is cancelled instead never issues its request.
+
+What you write is the interaction itself, in the `prepare` method of your setup class.
+`prepare` runs once per invocation and returns the durable call that carries it. The
+example is one blocking request, and whatever else your call does is yours to decide,
+because the framework does not know your protocol.
 
+The framework assigns `session_id` and `call_id` before it calls `prepare`, and they
+are the only values that a replay reproduces. The durable id you build from them is
+the key that recovery matches a recorded result against. When the key matches, your
+call is not run again. So build the key from those two values alone. Anything else in
+it, a timestamp for instance, makes a replay look up a different key and run the work
+a second time. Passing the same values to your own remote lets it recognize a
+re-issued request as the same one.
+
+The reconciler is optional. Omitting it leaves one crash window uncovered, and
+[Durability and failover](#durability-and-failover) describes that window. Supply one
+when the work cannot be repeated.
+
+{{< tabs "Supply a Reconciler" >}}
+
+{{< tab "Python" >}}
 ```python
-reviewer = ReActAgent(
-    chat_model=chat_model_descriptor,
-    prompt=Prompt.from_text(
-        "Review the following code diff and report any problems:\n\n{diff}"
+def prepare(
+    self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+) -> PreparedTriple:
+    key = f"{session_id}#{call_id}"
+
+    def call() -> SubagentResult:
+        return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+
+    # Consulted only during recovery, when this call is revisited and no result was
+    # recorded for it. Return the recovered outcome, or raise the recovered failure.
+    # It takes no arguments.
+    def reconcile() -> SubagentResult:
+        # fetch_review_by_key is your own lookup; not part of Flink Agents.
+        found = fetch_review_by_key(self.endpoint, key)
+        if found is None:
+            raise RuntimeError("the review service has no record of this call")
+        return SubagentResult.ok(found)
+
+    return (key, call, reconcile)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+// Inside the DurableCallable that prepare returns, alongside getId() and call().
+
+// Consulted only during recovery, when this call is revisited and no result was
+// recorded for it. Return the recovered outcome, or throw the recovered failure.
+@Override
+public Callable<SubagentResult> reconciler() {
+    return () -> {
+        // fetchReviewByKey is your own lookup; not part of Flink Agents.
+        String found = fetchReviewByKey(endpoint, key);
+        if (found == null) {
+            throw new IllegalStateException("the review service has no record of this call");
+        }
+        return SubagentResult.ok(found);
+    };
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+{{< tabs "Deferred Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.runner_context import RunnerContext
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.deferred_subagent import (
+    DeferredSubagentSetup,
+    PreparedTriple,
+)
+
+
+def post_review(endpoint: str, prompt: Any, key: str) -> str:
+    """Your own call to the review service; not part of Flink Agents.
+
+    Keying the work lets the service treat a re-issued request as the same one.
+    """
+    ...
+
+
+class RemoteReviewSetup(DeferredSubagentSetup):
+    endpoint: str = ""
+
+    def prepare(
+        self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+    ) -> PreparedTriple:
+        key = f"{session_id}#{call_id}"
+
+        def call() -> SubagentResult:
+            try:
+                return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+            except Exception as e:
+                # An expected failure is a value; only a system fault should escape.
+                return SubagentResult.error(e)
+
+        # The first value is the key recovery matches a recorded result against, so
+        # it is built from the two ids alone. The third is the reconciler.
+        return (key, call, None)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.context.DurableCallable;
+import org.apache.flink.agents.api.context.RunnerContext;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseDeferredSubagentSetup;
+
+/**
+ * Your own call to the review service; not part of Flink Agents. Keying the work lets
+ * the service treat a re-issued request as the same one.
+ */
+static String postReview(String endpoint, Object prompt, String key) {
+    ...
+}
+
+public class RemoteReviewSetup extends BaseDeferredSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteReviewSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected DurableCallable<SubagentResult> prepare(
+            RunnerContext ctx, Object prompt, String sessionId, String callId) {
+        // Built from the two ids alone, so a replay reproduces it.
+        String key = sessionId + "#" + callId;
+        return new DurableCallable<SubagentResult>() {
+            @Override
+            public String getId() {
+                // The key recovery matches a recorded result against.
+                return key;
+            }
+
+            @Override
+            public Class<SubagentResult> getResultClass() {
+                return SubagentResult.class;
+            }
+
+            @Override
+            public SubagentResult call() {
+                try {
+                    return SubagentResult.ok(postReview(endpoint, prompt, key));
+                } catch (Exception e) {
+                    // An expected failure is a value; only a system fault should escape.
+                    return SubagentResult.error(e);
+                }
+            }
+        };
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup under the name invocations will use. Every descriptor argument
+beyond the built-in ones is configuration for the setup to read back. In Python it
+reads them as model fields, and in Java through the public
+`(ResourceDescriptor, ResourceContext)` constructor. The `endpoint` above is such
+configuration, not a built-in transport.
+
+{{< tabs "Register an External Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
+parent.add_resource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteReviewSetup",
+        endpoint="https://example.com/review",
+        description="Review code using the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
     ),
 )
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteReviewSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/review")
+        .addInitialArgument("description", "Review code using the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_reviewer
+        clazz: my_app.agents.RemoteReviewSetup
+        endpoint: https://example.com/review
+        description: Review code using the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The YAML form of that registration is the `subagents` list of an agent entry, as in
+the YAML tab above. That list can also sit at the shared document level, outside
+`agents:`, so that several agents in one file reuse it. A chat model setup declared in
+the same file also takes a `subagents` argument, but it is a list of registered names
+rather than a list of entries. See
+[delegate from a chat model](#delegate-from-a-chat-model).
+
+Two constraints apply to the entry:
+
+- No class alias is defined for `AGENT` resources, so `clazz` is always a
+  fully-qualified class path.
+- The entry's language must match the loader. A Python YAML file names a Python
+  class and may not set `type: java`. A Java YAML file must set `type: java`.
+
+#### Async (pub/sub) execution
+
+An async sub-agent talks to a service that runs the task for you. The service accepts
+the work and answers at once, and you query it later for the run's status and
+result.
+
+An async setup publishes the run when the caller submits, through one durable POST.
+Waiting for the result then polls the run's status until it reaches a terminal state,
+and fetches it.
+
+That is the difference from the deferred mode, which sends nothing when the caller
+submits. The mode's name is the framework's own. The POST is the publish, and the
+polling is the subscribe.
+
+The submit-poll-fetch composition is the same for every such remote, so the base
+class owns it, along with the durable execution and the crash-window recovery. You
+supply the three transport calls it is built from:
+
+- `callSubmitRequest` / `call_submit_request` starts the run.
+- `callQueryStatus` / `call_query_status` returns a `RunStatus`, one of
+  `notStarted()`, `running()`, `completed()`, or `failed(error)`. It must be a
+  read-only probe with no side effects. Report an expected failure as a `FAILED`
+  status.
+- `callFetchResult` / `call_fetch_result` returns the `SubagentResult`. It must be
+  an idempotent read, because a replay may call it again. The status snapshot never
+  carries the payload, and a failed run is reported without fetching at all.
+
+{{< tabs "Async Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.async_subagent import BaseAsyncSubagentSetup, RunStatus
+
+# client(...) and task_id stand for your own transport client and run-id
+# scheme; neither is part of Flink Agents.
+
+
+class RemoteRunSetup(BaseAsyncSubagentSetup):
+    endpoint: str = ""
+
+    def call_submit_request(self, session_id: str, call_id: str, prompt: Any) -> None:
+        client(self.endpoint).submit(prompt, task_id=f"{session_id}#{call_id}")
+
+    def call_query_status(self, session_id: str, call_id: str) -> RunStatus:
+        probe = client(self.endpoint).status(task_id=f"{session_id}#{call_id}")
+        if probe is None:
+            return RunStatus.not_started()
+        if probe.succeeded:
+            return RunStatus.completed()
+        if probe.failed:
+            return RunStatus.failed(probe.error)
+        return RunStatus.running()
+
+    def call_fetch_result(self, session_id: str, call_id: str) -> SubagentResult:
+        return SubagentResult.ok(
+            client(self.endpoint).fetch(task_id=f"{session_id}#{call_id}")
+        )
+
+    # Optional, and a no-op by default, so a cancelled call leaves the remote run
+    # going. A replay may propagate the same cancellation again, so your remote has
+    # to accept it twice.
+    def call_cancel_request(self, session_id: str, call_id: str) -> None:
+        client(self.endpoint).cancel(task_id=f"{session_id}#{call_id}")
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseAsyncSubagentSetup;
+
+// client() and taskId(...) stand for your own transport client and run-id
+// scheme; neither is part of Flink Agents.
+public class RemoteRunSetup extends BaseAsyncSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteRunSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
 
+    @Override
+    protected void callSubmitRequest(String sessionId, String callId, Object prompt)
+            throws Exception {
+        client().submit(String.valueOf(prompt), taskId(sessionId, callId));
+    }
+
+    @Override
+    protected RunStatus callQueryStatus(String sessionId, String callId) {
+        // TaskStatus is your own transport type, not a framework type.
+        TaskStatus probe = client().status(taskId(sessionId, callId));
+        if (probe == null) {
+            return RunStatus.notStarted();
+        }
+        if (probe.succeeded()) {
+            return RunStatus.completed();
+        }
+        if (probe.failed()) {
+            return RunStatus.failed(probe.error());
+        }
+        return RunStatus.running();
+    }
+
+    @Override
+    protected SubagentResult callFetchResult(String sessionId, String callId) throws Exception {
+        return client().fetchResult(taskId(sessionId, callId));
+    }
+
+    // Optional, and a no-op by default, so a cancelled call leaves the remote run
+    // going. A replay may propagate the same cancellation again, so your remote has
+    // to accept it twice.
+    @Override
+    protected void callCancelRequest(String sessionId, String callId) {
+        client().cancel(taskId(sessionId, callId));
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup like any external sub-agent. The async form adds one descriptor
+argument, `status_poll_interval_millis`, and it defaults to 500.
+
+{{< tabs "Register an Async Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
 parent.add_resource(
-    "reviewer",
+    "remote_runner",
     ResourceType.AGENT,
-    InternalSubagentResourceDescriptorBuilder()
-    .agent(reviewer)
-    .description("Reviews a code diff and reports problems.")
-    # "diff" matches the {diff} placeholder of the child's prompt.
-    .input_schema(
-        '{"type":"object","properties":{"diff":{"type":"string"}},'
-        '"required":["diff"]}'
-    )
-    .build(),
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteRunSetup",
+        endpoint="https://example.com/runs",
+        description="Run a review task on the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
+        status_poll_interval_millis=500,
+    ),
 )
 ```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_runner",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteRunSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/runs")
+        .addInitialArgument("description", "Run a review task on the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .addInitialArgument("status_poll_interval_millis", 500L)
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_runner
+        clazz: my_app.agents.RemoteRunSetup
+        endpoint: https://example.com/runs
+        description: Run a review task on the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+        status_poll_interval_millis: 500
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The base class also recovers the crash window between "POST sent" and "POST durably
+recorded". On recovery it probes the run's status first, and it re-posts only when the
+probe reports that the run never started. Override `reconcileSubmitRequest` /
+`reconcile_submit_request` only if your service needs a different repair.
+
+## Invoke a sub-agent
+
+A sub-agent is invoked in one of two ways, model-driven or caller-driven, and the two
+reuse different existing abstractions.
+
+Model-driven delegation rides the tool abstraction. The sub-agent is offered to the
+model as a tool, so your action sends a `ChatRequestEvent` and the model answers with
+a call to the sub-agent. The result then returns to the model as the tool
+observation, until a final `ChatResponseEvent` reaches your action. This is the same
+cycle any tool call makes.
+
+Caller-driven delegation cannot ride that cycle, because a tool's result goes back
+into the chat loop rather than to the action that asked for it. An action that needs
+the delegated outcome therefore calls the sub-agent itself, with no model in the loop.
+
+Both ways end in the same call. Your action, or the framework on the model's behalf,
+runs `submit(ctx, prompt)` on the registered name. That call returns a
+`SubagentFuture`, which is the handle for that one call. Awaiting the handle yields
+the call's `SubagentResult`.
+
+### Delegate from a chat model
+
+List the registered name under `subagents` of the parent's chat model:
+
+{{< tabs "Declare Sub-agents on a Chat Model" >}}
+
+{{< tab "Python" >}}
+```python
+ResourceDescriptor(
+    clazz=ResourceName.ChatModel.OLLAMA_SETUP,
+    connection="ollama_connection",
+    model="qwen3:8b",
+    subagents=["reviewer"],
+)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_SETUP)
+    .addInitialArgument("connection", "ollamaConnection")
+    .addInitialArgument("model", "qwen3:8b")
+    .addInitialArgument("subagents", List.of("reviewer"))
+    .build();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The model sees `reviewer` as a tool, described by the registration's description and
+input schema. A schema is required for model-selected calls. A missing description
+falls back to a generic delegation description.
+
+The arguments the model sends become the sub-agent's input, and its outputs return to
+the model as the tool observation. The model can then keep reasoning over them. For
+an internal sub-agent, the observation is the JSON encoding of the full output list
+described in [the result](#the-result). That holds even when the child emits a single
+output, so the shape does not depend on how many outputs the child emits.
+
+An external setup class can state the shape of that observation. It declares
+`result_type()` in Python or `getResultType()` in Java, and the default reports the
+result as it arrived. Declare one to narrow a wider payload, or to convert a payload
+that is not already JSON-compatible. An internal sub-agent has no such declaration, so
+its observation is always the JSON encoding of its output list. This shapes the
+observation only. A caller that awaits the handle gets the result the sub-agent
+produced, as [the result](#the-result) describes.
+
+The external sub-agents `remote_reviewer` and `remote_runner` are offered the same
+way. Only their registration differs.
+
+### Call from an action
+
+An action can submit input to a registered sub-agent directly and await its
+outcome. This path needs no description and no input declaration, so it also works
+for a child registered as-is.
+
+{{< tabs "Call a Sub-agent From an Action" >}}
+
+{{< tab "Python" >}}
+```python
+class DelegatingAgent(Agent):
+    @action(EventType.InputEvent)
+    @staticmethod
+    async def delegate(event: Event, ctx: RunnerContext) -> None:
+        reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+        future = await reviewer.submit(ctx, "review the diff")
+        result = await future
+        if result.success:
+            ctx.send_event(OutputEvent(output=result.result[0]))
+        else:
+            ctx.send_event(OutputEvent(output=f"failed: {result.error_message}"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+public class DelegatingAgent extends Agent {
+
+    @Action(EventType.InputEvent)
+    public static void delegate(Event event, RunnerContext ctx) throws Exception {
+        SubagentSetup reviewer =
+                (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+        SubagentResult result = reviewer.submit(ctx, "review the diff").await();
+        if (result.isSuccess()) {
+            String answer = String.valueOf(((List<?>) result.getResult()).get(0));
+            ctx.sendEvent(new OutputEvent(answer));
+        } else {
+            ctx.sendEvent(new OutputEvent("failed: " + result.getErrorMessage()));
+        }
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+What you pass to `submit` is what the sub-agent receives. An internal child gets it
+as its `InputEvent` payload, and a model-driven call passes the arguments map
+instead. What comes back is a `SubagentResult`. For the internal sub-agent used here,
+its `result` is the list of `OutputEvent` payloads the child emitted. That is why the
+example reads `result.result[0]`. An external sub-agent returns whatever its setup
+produces.
+
+In Python `submit` is a coroutine, so the call is awaited in two phases and the
+action must be `async`. In Java `submit` returns the handle directly and `await()`
+yields the result. The same two calls drive `remote_reviewer` and `remote_runner`
+unchanged. Only the registration differs.
+
+## Work with open handles
+
+A `SubagentFuture` is live from `submit` until it is awaited or cancelled.
+
+It works like the `AsyncFuture` that
+[async execution]({{< ref "docs/development/create-agent/actions#async-execution" >}})
+returns. You get it from one call, and you await it to get the outcome. It is its own
+type rather than an `AsyncFuture`, because it always resolves to a `SubagentResult`,
+it carries the call's `(session_id, call_id)`, and it owns its durable execution
+inside the setup.
+
+### Fan out to several sub-agents
+
+`combine` batches handles into a `SubagentFutures`, and it works the way `gather`
+works for ordinary async futures. `gather` does not accept a `SubagentFuture`, so
+handles batch this way instead. Every handle in the batch is prepared before any is
+executed, so deferred requests all go out up front and remote runs overlap. Results

Review Comment:
   Fixed in both places. combine now claims only grouping and result order. I left the one-by-one execution unstated as well, since #926 will change it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "yunfengzhou-hub (via GitHub)" <gi...@apache.org>.
yunfengzhou-hub commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4300864107


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -307,30 +443,854 @@ parent.addResource(
                                 + "\"required\":[\"diff\"]}")
                 .build());
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The framework does not check the schema against the prompt. A field the prompt does
+not use is ignored, and a placeholder no field fills is left in the prompt as literal
+text. So keep the two in step when you change either.
+
+### External: write a setup class
+
+An external sub-agent has no plan of its own, and it is not compiled into the
+parent's. The work happens in your own code or in a remote agent service. The parent
+only submits to it and waits for the outcome. The framework cannot perform that work
+for you, because it does not know your protocol. So you write the part that performs
+one invocation, and the framework supplies everything around it. It runs your code
+under durable execution, and after a failover it replays the recorded result instead
+of calling your code again.
+
+What you write is a setup class. Its only job is to carry out one invocation, so it
+declares no actions and no events of its own. It extends a framework base class and
+implements the methods that class declares.
+
+Registration names the setup class in a descriptor, and both languages construct the
+setup from the descriptor at runtime. Registering a setup instance instead is
+rejected.
+
+The invocation itself is carried out in one of two modes, and which one you write
+follows from your remote interface. When one request returns the result, write a
+*deferred* setup. You own the whole interaction, and the framework only holds it back
+until the caller asks for the result. When the remote instead hands back a run that
+you later query for status and result, write an *async (pub/sub)* setup. The base
+class owns that composition of publishing, polling, and fetching, and you supply the
+transport calls it is built from.
+
+#### Deferred execution
+
+A deferred setup sends nothing when the caller submits. It only registers the
+invocation. The request goes out when the caller first awaits the result. So a call
+that is cancelled instead never issues its request.
+
+What you write is the interaction itself, in the `prepare` method of your setup class.
+`prepare` runs once per invocation and returns the durable call that carries it. The
+example is one blocking request, and whatever else your call does is yours to decide,
+because the framework does not know your protocol.
 
+The framework assigns `session_id` and `call_id` before it calls `prepare`, and they
+are the only values that a replay reproduces. The durable id you build from them is
+the key that recovery matches a recorded result against. When the key matches, your
+call is not run again. So build the key from those two values alone. Anything else in
+it, a timestamp for instance, makes a replay look up a different key and run the work
+a second time. Passing the same values to your own remote lets it recognize a
+re-issued request as the same one.
+
+The reconciler is optional. Omitting it leaves one crash window uncovered, and
+[Durability and failover](#durability-and-failover) describes that window. Supply one
+when the work cannot be repeated.
+
+{{< tabs "Supply a Reconciler" >}}
+
+{{< tab "Python" >}}
 ```python
-reviewer = ReActAgent(
-    chat_model=chat_model_descriptor,
-    prompt=Prompt.from_text(
-        "Review the following code diff and report any problems:\n\n{diff}"
+def prepare(
+    self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+) -> PreparedTriple:
+    key = f"{session_id}#{call_id}"
+
+    def call() -> SubagentResult:
+        return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+
+    # Consulted only during recovery, when this call is revisited and no result was
+    # recorded for it. Return the recovered outcome, or raise the recovered failure.
+    # It takes no arguments.
+    def reconcile() -> SubagentResult:
+        # fetch_review_by_key is your own lookup; not part of Flink Agents.
+        found = fetch_review_by_key(self.endpoint, key)
+        if found is None:
+            raise RuntimeError("the review service has no record of this call")

Review Comment:
   Fixed in these tabs. The comments now say raising here fails the action, which is what How failures surface already claimed, so the example and that section no longer disagree.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "wenjin272 (via GitHub)" <gi...@apache.org>.
wenjin272 commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4293163914


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -307,30 +443,854 @@ parent.addResource(
                                 + "\"required\":[\"diff\"]}")
                 .build());
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The framework does not check the schema against the prompt. A field the prompt does
+not use is ignored, and a placeholder no field fills is left in the prompt as literal
+text. So keep the two in step when you change either.
+
+### External: write a setup class
+
+An external sub-agent has no plan of its own, and it is not compiled into the
+parent's. The work happens in your own code or in a remote agent service. The parent
+only submits to it and waits for the outcome. The framework cannot perform that work
+for you, because it does not know your protocol. So you write the part that performs
+one invocation, and the framework supplies everything around it. It runs your code
+under durable execution, and after a failover it replays the recorded result instead
+of calling your code again.
+
+What you write is a setup class. Its only job is to carry out one invocation, so it
+declares no actions and no events of its own. It extends a framework base class and
+implements the methods that class declares.
+
+Registration names the setup class in a descriptor, and both languages construct the
+setup from the descriptor at runtime. Registering a setup instance instead is
+rejected.
+
+The invocation itself is carried out in one of two modes, and which one you write
+follows from your remote interface. When one request returns the result, write a
+*deferred* setup. You own the whole interaction, and the framework only holds it back
+until the caller asks for the result. When the remote instead hands back a run that
+you later query for status and result, write an *async (pub/sub)* setup. The base
+class owns that composition of publishing, polling, and fetching, and you supply the
+transport calls it is built from.
+
+#### Deferred execution
+
+A deferred setup sends nothing when the caller submits. It only registers the
+invocation. The request goes out when the caller first awaits the result. So a call
+that is cancelled instead never issues its request.
+
+What you write is the interaction itself, in the `prepare` method of your setup class.
+`prepare` runs once per invocation and returns the durable call that carries it. The
+example is one blocking request, and whatever else your call does is yours to decide,
+because the framework does not know your protocol.
 
+The framework assigns `session_id` and `call_id` before it calls `prepare`, and they
+are the only values that a replay reproduces. The durable id you build from them is
+the key that recovery matches a recorded result against. When the key matches, your
+call is not run again. So build the key from those two values alone. Anything else in
+it, a timestamp for instance, makes a replay look up a different key and run the work
+a second time. Passing the same values to your own remote lets it recognize a
+re-issued request as the same one.
+
+The reconciler is optional. Omitting it leaves one crash window uncovered, and
+[Durability and failover](#durability-and-failover) describes that window. Supply one
+when the work cannot be repeated.
+
+{{< tabs "Supply a Reconciler" >}}
+
+{{< tab "Python" >}}
 ```python
-reviewer = ReActAgent(
-    chat_model=chat_model_descriptor,
-    prompt=Prompt.from_text(
-        "Review the following code diff and report any problems:\n\n{diff}"
+def prepare(
+    self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+) -> PreparedTriple:
+    key = f"{session_id}#{call_id}"
+
+    def call() -> SubagentResult:
+        return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+
+    # Consulted only during recovery, when this call is revisited and no result was
+    # recorded for it. Return the recovered outcome, or raise the recovered failure.
+    # It takes no arguments.
+    def reconcile() -> SubagentResult:
+        # fetch_review_by_key is your own lookup; not part of Flink Agents.
+        found = fetch_review_by_key(self.endpoint, key)
+        if found is None:
+            raise RuntimeError("the review service has no record of this call")
+        return SubagentResult.ok(found)
+
+    return (key, call, reconcile)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+// Inside the DurableCallable that prepare returns, alongside getId() and call().
+
+// Consulted only during recovery, when this call is revisited and no result was
+// recorded for it. Return the recovered outcome, or throw the recovered failure.
+@Override
+public Callable<SubagentResult> reconciler() {
+    return () -> {
+        // fetchReviewByKey is your own lookup; not part of Flink Agents.
+        String found = fetchReviewByKey(endpoint, key);
+        if (found == null) {
+            throw new IllegalStateException("the review service has no record of this call");
+        }
+        return SubagentResult.ok(found);
+    };
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+{{< tabs "Deferred Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.runner_context import RunnerContext
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.deferred_subagent import (
+    DeferredSubagentSetup,
+    PreparedTriple,
+)
+
+
+def post_review(endpoint: str, prompt: Any, key: str) -> str:
+    """Your own call to the review service; not part of Flink Agents.
+
+    Keying the work lets the service treat a re-issued request as the same one.
+    """
+    ...
+
+
+class RemoteReviewSetup(DeferredSubagentSetup):
+    endpoint: str = ""
+
+    def prepare(
+        self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+    ) -> PreparedTriple:
+        key = f"{session_id}#{call_id}"
+
+        def call() -> SubagentResult:
+            try:
+                return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+            except Exception as e:
+                # An expected failure is a value; only a system fault should escape.
+                return SubagentResult.error(e)
+
+        # The first value is the key recovery matches a recorded result against, so
+        # it is built from the two ids alone. The third is the reconciler.
+        return (key, call, None)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.context.DurableCallable;
+import org.apache.flink.agents.api.context.RunnerContext;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseDeferredSubagentSetup;
+
+/**
+ * Your own call to the review service; not part of Flink Agents. Keying the work lets
+ * the service treat a re-issued request as the same one.
+ */
+static String postReview(String endpoint, Object prompt, String key) {
+    ...
+}
+
+public class RemoteReviewSetup extends BaseDeferredSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteReviewSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected DurableCallable<SubagentResult> prepare(
+            RunnerContext ctx, Object prompt, String sessionId, String callId) {
+        // Built from the two ids alone, so a replay reproduces it.
+        String key = sessionId + "#" + callId;
+        return new DurableCallable<SubagentResult>() {
+            @Override
+            public String getId() {
+                // The key recovery matches a recorded result against.
+                return key;
+            }
+
+            @Override
+            public Class<SubagentResult> getResultClass() {
+                return SubagentResult.class;
+            }
+
+            @Override
+            public SubagentResult call() {
+                try {
+                    return SubagentResult.ok(postReview(endpoint, prompt, key));
+                } catch (Exception e) {
+                    // An expected failure is a value; only a system fault should escape.
+                    return SubagentResult.error(e);
+                }
+            }
+        };
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup under the name invocations will use. Every descriptor argument
+beyond the built-in ones is configuration for the setup to read back. In Python it
+reads them as model fields, and in Java through the public
+`(ResourceDescriptor, ResourceContext)` constructor. The `endpoint` above is such
+configuration, not a built-in transport.
+
+{{< tabs "Register an External Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
+parent.add_resource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteReviewSetup",
+        endpoint="https://example.com/review",
+        description="Review code using the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
     ),
 )
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteReviewSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/review")
+        .addInitialArgument("description", "Review code using the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_reviewer
+        clazz: my_app.agents.RemoteReviewSetup
+        endpoint: https://example.com/review
+        description: Review code using the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The YAML form of that registration is the `subagents` list of an agent entry, as in
+the YAML tab above. That list can also sit at the shared document level, outside
+`agents:`, so that several agents in one file reuse it. A chat model setup declared in
+the same file also takes a `subagents` argument, but it is a list of registered names
+rather than a list of entries. See
+[delegate from a chat model](#delegate-from-a-chat-model).
+
+Two constraints apply to the entry:
+
+- No class alias is defined for `AGENT` resources, so `clazz` is always a
+  fully-qualified class path.
+- The entry's language must match the loader. A Python YAML file names a Python
+  class and may not set `type: java`. A Java YAML file must set `type: java`.
+
+#### Async (pub/sub) execution
+
+An async sub-agent talks to a service that runs the task for you. The service accepts
+the work and answers at once, and you query it later for the run's status and
+result.
+
+An async setup publishes the run when the caller submits, through one durable POST.
+Waiting for the result then polls the run's status until it reaches a terminal state,
+and fetches it.
+
+That is the difference from the deferred mode, which sends nothing when the caller
+submits. The mode's name is the framework's own. The POST is the publish, and the
+polling is the subscribe.
+
+The submit-poll-fetch composition is the same for every such remote, so the base
+class owns it, along with the durable execution and the crash-window recovery. You
+supply the three transport calls it is built from:
+
+- `callSubmitRequest` / `call_submit_request` starts the run.
+- `callQueryStatus` / `call_query_status` returns a `RunStatus`, one of
+  `notStarted()`, `running()`, `completed()`, or `failed(error)`. It must be a
+  read-only probe with no side effects. Report an expected failure as a `FAILED`
+  status.
+- `callFetchResult` / `call_fetch_result` returns the `SubagentResult`. It must be
+  an idempotent read, because a replay may call it again. The status snapshot never
+  carries the payload, and a failed run is reported without fetching at all.
+
+{{< tabs "Async Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.async_subagent import BaseAsyncSubagentSetup, RunStatus
+
+# client(...) and task_id stand for your own transport client and run-id
+# scheme; neither is part of Flink Agents.
+
+
+class RemoteRunSetup(BaseAsyncSubagentSetup):
+    endpoint: str = ""
+
+    def call_submit_request(self, session_id: str, call_id: str, prompt: Any) -> None:
+        client(self.endpoint).submit(prompt, task_id=f"{session_id}#{call_id}")
+
+    def call_query_status(self, session_id: str, call_id: str) -> RunStatus:
+        probe = client(self.endpoint).status(task_id=f"{session_id}#{call_id}")
+        if probe is None:
+            return RunStatus.not_started()
+        if probe.succeeded:
+            return RunStatus.completed()
+        if probe.failed:
+            return RunStatus.failed(probe.error)
+        return RunStatus.running()
+
+    def call_fetch_result(self, session_id: str, call_id: str) -> SubagentResult:
+        return SubagentResult.ok(
+            client(self.endpoint).fetch(task_id=f"{session_id}#{call_id}")
+        )
+
+    # Optional, and a no-op by default, so a cancelled call leaves the remote run
+    # going. A replay may propagate the same cancellation again, so your remote has
+    # to accept it twice.
+    def call_cancel_request(self, session_id: str, call_id: str) -> None:
+        client(self.endpoint).cancel(task_id=f"{session_id}#{call_id}")
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseAsyncSubagentSetup;
+
+// client() and taskId(...) stand for your own transport client and run-id
+// scheme; neither is part of Flink Agents.
+public class RemoteRunSetup extends BaseAsyncSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteRunSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
 
+    @Override
+    protected void callSubmitRequest(String sessionId, String callId, Object prompt)
+            throws Exception {
+        client().submit(String.valueOf(prompt), taskId(sessionId, callId));
+    }
+
+    @Override
+    protected RunStatus callQueryStatus(String sessionId, String callId) {
+        // TaskStatus is your own transport type, not a framework type.
+        TaskStatus probe = client().status(taskId(sessionId, callId));
+        if (probe == null) {
+            return RunStatus.notStarted();
+        }
+        if (probe.succeeded()) {
+            return RunStatus.completed();
+        }
+        if (probe.failed()) {
+            return RunStatus.failed(probe.error());
+        }
+        return RunStatus.running();
+    }
+
+    @Override
+    protected SubagentResult callFetchResult(String sessionId, String callId) throws Exception {
+        return client().fetchResult(taskId(sessionId, callId));
+    }
+
+    // Optional, and a no-op by default, so a cancelled call leaves the remote run
+    // going. A replay may propagate the same cancellation again, so your remote has
+    // to accept it twice.
+    @Override
+    protected void callCancelRequest(String sessionId, String callId) {
+        client().cancel(taskId(sessionId, callId));
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup like any external sub-agent. The async form adds one descriptor
+argument, `status_poll_interval_millis`, and it defaults to 500.
+
+{{< tabs "Register an Async Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
 parent.add_resource(
-    "reviewer",
+    "remote_runner",
     ResourceType.AGENT,
-    InternalSubagentResourceDescriptorBuilder()
-    .agent(reviewer)
-    .description("Reviews a code diff and reports problems.")
-    # "diff" matches the {diff} placeholder of the child's prompt.
-    .input_schema(
-        '{"type":"object","properties":{"diff":{"type":"string"}},'
-        '"required":["diff"]}'
-    )
-    .build(),
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteRunSetup",
+        endpoint="https://example.com/runs",
+        description="Run a review task on the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
+        status_poll_interval_millis=500,
+    ),
 )
 ```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_runner",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteRunSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/runs")
+        .addInitialArgument("description", "Run a review task on the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .addInitialArgument("status_poll_interval_millis", 500L)
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_runner
+        clazz: my_app.agents.RemoteRunSetup
+        endpoint: https://example.com/runs
+        description: Run a review task on the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+        status_poll_interval_millis: 500
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The base class also recovers the crash window between "POST sent" and "POST durably
+recorded". On recovery it probes the run's status first, and it re-posts only when the
+probe reports that the run never started. Override `reconcileSubmitRequest` /
+`reconcile_submit_request` only if your service needs a different repair.
+
+## Invoke a sub-agent
+
+A sub-agent is invoked in one of two ways, model-driven or caller-driven, and the two
+reuse different existing abstractions.
+
+Model-driven delegation rides the tool abstraction. The sub-agent is offered to the
+model as a tool, so your action sends a `ChatRequestEvent` and the model answers with
+a call to the sub-agent. The result then returns to the model as the tool
+observation, until a final `ChatResponseEvent` reaches your action. This is the same
+cycle any tool call makes.
+
+Caller-driven delegation cannot ride that cycle, because a tool's result goes back
+into the chat loop rather than to the action that asked for it. An action that needs
+the delegated outcome therefore calls the sub-agent itself, with no model in the loop.
+
+Both ways end in the same call. Your action, or the framework on the model's behalf,
+runs `submit(ctx, prompt)` on the registered name. That call returns a
+`SubagentFuture`, which is the handle for that one call. Awaiting the handle yields
+the call's `SubagentResult`.
+
+### Delegate from a chat model
+
+List the registered name under `subagents` of the parent's chat model:
+
+{{< tabs "Declare Sub-agents on a Chat Model" >}}
+
+{{< tab "Python" >}}
+```python
+ResourceDescriptor(
+    clazz=ResourceName.ChatModel.OLLAMA_SETUP,
+    connection="ollama_connection",
+    model="qwen3:8b",
+    subagents=["reviewer"],
+)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_SETUP)
+    .addInitialArgument("connection", "ollamaConnection")
+    .addInitialArgument("model", "qwen3:8b")
+    .addInitialArgument("subagents", List.of("reviewer"))
+    .build();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The model sees `reviewer` as a tool, described by the registration's description and
+input schema. A schema is required for model-selected calls. A missing description
+falls back to a generic delegation description.
+
+The arguments the model sends become the sub-agent's input, and its outputs return to
+the model as the tool observation. The model can then keep reasoning over them. For
+an internal sub-agent, the observation is the JSON encoding of the full output list
+described in [the result](#the-result). That holds even when the child emits a single
+output, so the shape does not depend on how many outputs the child emits.
+
+An external setup class can state the shape of that observation. It declares
+`result_type()` in Python or `getResultType()` in Java, and the default reports the
+result as it arrived. Declare one to narrow a wider payload, or to convert a payload
+that is not already JSON-compatible. An internal sub-agent has no such declaration, so
+its observation is always the JSON encoding of its output list. This shapes the
+observation only. A caller that awaits the handle gets the result the sub-agent
+produced, as [the result](#the-result) describes.
+
+The external sub-agents `remote_reviewer` and `remote_runner` are offered the same
+way. Only their registration differs.
+
+### Call from an action
+
+An action can submit input to a registered sub-agent directly and await its
+outcome. This path needs no description and no input declaration, so it also works
+for a child registered as-is.
+
+{{< tabs "Call a Sub-agent From an Action" >}}
+
+{{< tab "Python" >}}
+```python
+class DelegatingAgent(Agent):
+    @action(EventType.InputEvent)
+    @staticmethod
+    async def delegate(event: Event, ctx: RunnerContext) -> None:
+        reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+        future = await reviewer.submit(ctx, "review the diff")
+        result = await future
+        if result.success:
+            ctx.send_event(OutputEvent(output=result.result[0]))
+        else:
+            ctx.send_event(OutputEvent(output=f"failed: {result.error_message}"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+public class DelegatingAgent extends Agent {
+
+    @Action(EventType.InputEvent)
+    public static void delegate(Event event, RunnerContext ctx) throws Exception {
+        SubagentSetup reviewer =
+                (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+        SubagentResult result = reviewer.submit(ctx, "review the diff").await();
+        if (result.isSuccess()) {
+            String answer = String.valueOf(((List<?>) result.getResult()).get(0));
+            ctx.sendEvent(new OutputEvent(answer));
+        } else {
+            ctx.sendEvent(new OutputEvent("failed: " + result.getErrorMessage()));
+        }
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+What you pass to `submit` is what the sub-agent receives. An internal child gets it
+as its `InputEvent` payload, and a model-driven call passes the arguments map
+instead. What comes back is a `SubagentResult`. For the internal sub-agent used here,
+its `result` is the list of `OutputEvent` payloads the child emitted. That is why the
+example reads `result.result[0]`. An external sub-agent returns whatever its setup
+produces.
+
+In Python `submit` is a coroutine, so the call is awaited in two phases and the
+action must be `async`. In Java `submit` returns the handle directly and `await()`
+yields the result. The same two calls drive `remote_reviewer` and `remote_runner`
+unchanged. Only the registration differs.
+
+## Work with open handles
+
+A `SubagentFuture` is live from `submit` until it is awaited or cancelled.
+
+It works like the `AsyncFuture` that
+[async execution]({{< ref "docs/development/create-agent/actions#async-execution" >}})
+returns. You get it from one call, and you await it to get the outcome. It is its own
+type rather than an `AsyncFuture`, because it always resolves to a `SubagentResult`,
+it carries the call's `(session_id, call_id)`, and it owns its durable execution
+inside the setup.
+
+### Fan out to several sub-agents
+
+`combine` batches handles into a `SubagentFutures`, and it works the way `gather`
+works for ordinary async futures. `gather` does not accept a `SubagentFuture`, so
+handles batch this way instead. Every handle in the batch is prepared before any is
+executed, so deferred requests all go out up front and remote runs overlap. Results

Review Comment:
   Could we avoid promising concurrent execution for deferred calls here? In both Java and Python, `SubagentFutureGroup` prepares all handles, then executes and waits for each durable call one by one (the implementation explicitly leaves batching to TODO #926). In the `RemoteReviewSetup` example above, the POST is inside the callable, so the second request starts only after the first completes. A minimal reproduction produces `POST a -> complete a -> POST b -> complete b`.
   
   Please describe `combine` as grouping handles and preserving result order, without implying that it makes deferred requests overlap. The similar overlap claim at the end of Restrictions should also distinguish async setups, which start remote work during `submit`, from deferred/internal calls.



##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -307,30 +443,854 @@ parent.addResource(
                                 + "\"required\":[\"diff\"]}")
                 .build());
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The framework does not check the schema against the prompt. A field the prompt does
+not use is ignored, and a placeholder no field fills is left in the prompt as literal
+text. So keep the two in step when you change either.
+
+### External: write a setup class
+
+An external sub-agent has no plan of its own, and it is not compiled into the
+parent's. The work happens in your own code or in a remote agent service. The parent
+only submits to it and waits for the outcome. The framework cannot perform that work
+for you, because it does not know your protocol. So you write the part that performs
+one invocation, and the framework supplies everything around it. It runs your code
+under durable execution, and after a failover it replays the recorded result instead
+of calling your code again.
+
+What you write is a setup class. Its only job is to carry out one invocation, so it
+declares no actions and no events of its own. It extends a framework base class and
+implements the methods that class declares.
+
+Registration names the setup class in a descriptor, and both languages construct the
+setup from the descriptor at runtime. Registering a setup instance instead is
+rejected.
+
+The invocation itself is carried out in one of two modes, and which one you write
+follows from your remote interface. When one request returns the result, write a
+*deferred* setup. You own the whole interaction, and the framework only holds it back
+until the caller asks for the result. When the remote instead hands back a run that
+you later query for status and result, write an *async (pub/sub)* setup. The base
+class owns that composition of publishing, polling, and fetching, and you supply the
+transport calls it is built from.
+
+#### Deferred execution
+
+A deferred setup sends nothing when the caller submits. It only registers the
+invocation. The request goes out when the caller first awaits the result. So a call
+that is cancelled instead never issues its request.
+
+What you write is the interaction itself, in the `prepare` method of your setup class.
+`prepare` runs once per invocation and returns the durable call that carries it. The
+example is one blocking request, and whatever else your call does is yours to decide,
+because the framework does not know your protocol.
 
+The framework assigns `session_id` and `call_id` before it calls `prepare`, and they
+are the only values that a replay reproduces. The durable id you build from them is
+the key that recovery matches a recorded result against. When the key matches, your
+call is not run again. So build the key from those two values alone. Anything else in
+it, a timestamp for instance, makes a replay look up a different key and run the work
+a second time. Passing the same values to your own remote lets it recognize a
+re-issued request as the same one.
+
+The reconciler is optional. Omitting it leaves one crash window uncovered, and
+[Durability and failover](#durability-and-failover) describes that window. Supply one
+when the work cannot be repeated.
+
+{{< tabs "Supply a Reconciler" >}}
+
+{{< tab "Python" >}}
 ```python
-reviewer = ReActAgent(
-    chat_model=chat_model_descriptor,
-    prompt=Prompt.from_text(
-        "Review the following code diff and report any problems:\n\n{diff}"
+def prepare(
+    self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+) -> PreparedTriple:
+    key = f"{session_id}#{call_id}"
+
+    def call() -> SubagentResult:
+        return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+
+    # Consulted only during recovery, when this call is revisited and no result was
+    # recorded for it. Return the recovered outcome, or raise the recovered failure.
+    # It takes no arguments.
+    def reconcile() -> SubagentResult:
+        # fetch_review_by_key is your own lookup; not part of Flink Agents.
+        found = fetch_review_by_key(self.endpoint, key)
+        if found is None:
+            raise RuntimeError("the review service has no record of this call")
+        return SubagentResult.ok(found)
+
+    return (key, call, reconcile)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+// Inside the DurableCallable that prepare returns, alongside getId() and call().
+
+// Consulted only during recovery, when this call is revisited and no result was
+// recorded for it. Return the recovered outcome, or throw the recovered failure.
+@Override
+public Callable<SubagentResult> reconciler() {
+    return () -> {
+        // fetchReviewByKey is your own lookup; not part of Flink Agents.
+        String found = fetchReviewByKey(endpoint, key);
+        if (found == null) {
+            throw new IllegalStateException("the review service has no record of this call");
+        }
+        return SubagentResult.ok(found);
+    };
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+{{< tabs "Deferred Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.runner_context import RunnerContext
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.deferred_subagent import (
+    DeferredSubagentSetup,
+    PreparedTriple,
+)
+
+
+def post_review(endpoint: str, prompt: Any, key: str) -> str:
+    """Your own call to the review service; not part of Flink Agents.
+
+    Keying the work lets the service treat a re-issued request as the same one.
+    """
+    ...
+
+
+class RemoteReviewSetup(DeferredSubagentSetup):
+    endpoint: str = ""
+
+    def prepare(
+        self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+    ) -> PreparedTriple:
+        key = f"{session_id}#{call_id}"
+
+        def call() -> SubagentResult:
+            try:
+                return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+            except Exception as e:
+                # An expected failure is a value; only a system fault should escape.
+                return SubagentResult.error(e)
+
+        # The first value is the key recovery matches a recorded result against, so
+        # it is built from the two ids alone. The third is the reconciler.
+        return (key, call, None)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.context.DurableCallable;
+import org.apache.flink.agents.api.context.RunnerContext;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseDeferredSubagentSetup;
+
+/**
+ * Your own call to the review service; not part of Flink Agents. Keying the work lets
+ * the service treat a re-issued request as the same one.
+ */
+static String postReview(String endpoint, Object prompt, String key) {
+    ...
+}
+
+public class RemoteReviewSetup extends BaseDeferredSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteReviewSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected DurableCallable<SubagentResult> prepare(
+            RunnerContext ctx, Object prompt, String sessionId, String callId) {
+        // Built from the two ids alone, so a replay reproduces it.
+        String key = sessionId + "#" + callId;
+        return new DurableCallable<SubagentResult>() {
+            @Override
+            public String getId() {
+                // The key recovery matches a recorded result against.
+                return key;
+            }
+
+            @Override
+            public Class<SubagentResult> getResultClass() {
+                return SubagentResult.class;
+            }
+
+            @Override
+            public SubagentResult call() {
+                try {
+                    return SubagentResult.ok(postReview(endpoint, prompt, key));
+                } catch (Exception e) {
+                    // An expected failure is a value; only a system fault should escape.
+                    return SubagentResult.error(e);
+                }
+            }
+        };
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup under the name invocations will use. Every descriptor argument
+beyond the built-in ones is configuration for the setup to read back. In Python it
+reads them as model fields, and in Java through the public
+`(ResourceDescriptor, ResourceContext)` constructor. The `endpoint` above is such
+configuration, not a built-in transport.
+
+{{< tabs "Register an External Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
+parent.add_resource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteReviewSetup",
+        endpoint="https://example.com/review",
+        description="Review code using the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
     ),
 )
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteReviewSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/review")
+        .addInitialArgument("description", "Review code using the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_reviewer
+        clazz: my_app.agents.RemoteReviewSetup
+        endpoint: https://example.com/review
+        description: Review code using the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The YAML form of that registration is the `subagents` list of an agent entry, as in
+the YAML tab above. That list can also sit at the shared document level, outside
+`agents:`, so that several agents in one file reuse it. A chat model setup declared in
+the same file also takes a `subagents` argument, but it is a list of registered names
+rather than a list of entries. See
+[delegate from a chat model](#delegate-from-a-chat-model).
+
+Two constraints apply to the entry:
+
+- No class alias is defined for `AGENT` resources, so `clazz` is always a
+  fully-qualified class path.
+- The entry's language must match the loader. A Python YAML file names a Python
+  class and may not set `type: java`. A Java YAML file must set `type: java`.
+
+#### Async (pub/sub) execution
+
+An async sub-agent talks to a service that runs the task for you. The service accepts
+the work and answers at once, and you query it later for the run's status and
+result.
+
+An async setup publishes the run when the caller submits, through one durable POST.
+Waiting for the result then polls the run's status until it reaches a terminal state,
+and fetches it.
+
+That is the difference from the deferred mode, which sends nothing when the caller

Review Comment:
   Could we focus these explanations more on how users work with sub-agents and less on the framework’s design rationale? For example:
   
   - Instead of explaining “the POST is the publish, and the polling is the subscribe,” describe the observable behavior: submitting starts a remote run, and awaiting polls its status and retrieves the result.
   - In “Work with open handles,” focus on awaiting, combining, cancelling, and accessing session/call IDs rather than why `SubagentFuture` is a separate type from `AsyncFuture` or where it owns durable execution.
   - In “Invoke a sub-agent,” replace “rides the tool abstraction” with the practical distinction: model-driven results return to the model, while caller-driven results return to the action.
   
   The `prepare`, durable ID, and reconciler contracts should stay, since users implementing external setups need those details.



##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -24,30 +24,170 @@ specific language governing permissions and limitations
 under the License.
 -->
 
-A sub-agent handles a task delegated by a parent agent. An internal sub-agent runs
-an agent workflow in the same job. An external sub-agent calls an agent service
-through a `SubagentSetup` implementation.
+## Overview
 
-## Declare an internal sub-agent
+A sub-agent is an agent that another agent delegates a task to. The parent hands it
+input, the sub-agent does the work, and the parent receives the outcome as a
+`SubagentResult`. Use one when the delegated work is multi-step or long-running and
+should run isolated from the parent.
 
-Use `InternalSubagentResourceDescriptorBuilder` to attach a description and input
-schema to a child agent. The builder returns a normal resource descriptor for `addResource`
-or `add_resource`.
+The work runs either inside the job or outside it. An *internal* sub-agent is a
+child `Agent`, including a `ReActAgent`, compiled into the parent's plan. The
+child's own actions and events run in the same job. An *external* sub-agent has no
+plan of its own. The work happens in your own code or in a remote agent service,
+outside the parent's event loop. It takes one of two forms, *deferred* or *async
+(pub/sub)*.
 
+Either way, the call is initiated by the model or by you. In *model-driven
+delegation* you list the sub-agent on a chat model setup and the model chooses when
+to delegate. In *caller-driven delegation* one of your actions submits the task
+itself.
+
+Every sub-agent call runs through
+[durable execution]({{< ref "docs/development/create-agent/actions#durable-execution" >}}).
+There is no durability toggle and no sub-agent timeout option.
+
+## Declare a sub-agent
+
+A sub-agent enters the parent's plan as a resource registered under
+`ResourceType.AGENT`. The name you register it under is the name that invocations and
+metrics use. What you register depends on the form. An internal sub-agent registers
+a child `Agent`. An external sub-agent registers a descriptor that names your setup
+class, and a YAML file can declare it.
+
+### Internal: compile a child agent
+
+Nothing in a child's own code marks it as a sub-agent. The child is an ordinary
+agent with its own resources, actions, and event loop, and it runs standalone
+exactly as written. A parent registers it as an `AGENT` resource, and that
+registration is the whole of the difference. It gives the child a name in the
+parent's plan. It can also give the child an input schema, so a chat model can call
+it. And it supplies the caller that submits input and awaits the child's outputs.
+
+Any `Agent` can serve as the child. The two common kinds are a workflow agent and a
+`ReActAgent`. Either way, the child's plan is compiled into the parent's when the
+parent's plan is built.
+
+#### A workflow agent as the child
+
+The example child owns a chat model and answers each review request with its

Review Comment:
   Could we streamline the guide and make it easier to scan? A few concrete suggestions:
   
   - **Reuse existing examples.** “A workflow agent as the child” repeats the connection/model setup and request/response actions already shown in the Chat with Models guide. Could we link to that example and keep only the child registration and input adaptation here—for example, reading `task["code"]` for model-driven calls versus the submitted string for caller-driven calls?
   - **Consolidate repeated explanations.** The introduction to internal sub-agents already explains that a child is an ordinary Agent made into a sub-agent through registration; the workflow example immediately repeats this. Likewise, “always durable, no sub-agent timeout option” appears in both Overview and Durability and failover. A brief introduction with a link to the detailed section should be enough.
   - **Use tables for comparisons.** Before the external setup examples, a deferred-vs-async table could show when work starts (first await vs. submit), what users implement (`prepare` vs. submit/status/fetch hooks), and when to choose each (a request returning the result vs. a remote run queried later). This would help readers choose an approach without extracting these differences from several long paragraphs.



##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -67,156 +207,147 @@ parent.add_resource(
         .build(),
 )
 ```
+{{< /tab >}}
 
-`ReviewAgent` is your child workflow; the Java `ReviewRequest` is a data class
-with fields describing its input. Alternatively, pass a JSON schema string to
-`inputSchema(...)` in Java or `input_schema(...)` in Python. Choose either an
-input type or an explicit schema. The schema describes the submitted arguments,
-which the child receives as its input event payload; it does not convert that
-payload into an instance of the input type.
-
-When a chat model is configured with `subagents=["reviewer"]`, the description
-helps it choose when to delegate, and the schema tells it which arguments to
-send. A schema is required for model-selected calls. A missing description uses
-a generic delegation description.
-
-For calls made explicitly by an action, the existing shorthand remains available:
-
+{{< tab "Java" >}}
 ```java
-parent.addResource("reviewer", ResourceType.AGENT, new ReviewAgent());
-```
+import org.apache.flink.agents.api.resource.ResourceType;
+import org.apache.flink.agents.api.subagent.InternalSubagentResourceDescriptorBuilder;
 
-```python
-parent.add_resource("reviewer", ResourceType.AGENT, ReviewAgent())
+parent.addResource(
+    "reviewer",
+    ResourceType.AGENT,
+    new InternalSubagentResourceDescriptorBuilder()
+        .agent(new ReviewAgent())
+        .description("Review code for correctness and potential defects")
+        .inputType(ReviewRequest.class)
+        .build());
 ```
+{{< /tab >}}
 
-This shorthand does not declare an input schema. Use the builder when exposing
-the child to a model. Recursive registration, such as a child registering its
-parent, is rejected when the plan is built.
+{{< /tabs >}}
 
-Register the resources a child uses, such as its chat model connection, on the
-child itself. In Python, a child resolves resources against its own plan only,
-so a resource registered solely on the parent is not visible to the child.
+`ReviewRequest` describes the arguments the child accepts, and the Java form is a bean.
+Where no type fits, a JSON schema string declares the same arguments:
 
-## Declare an external sub-agent
+{{< tabs "Declare the Input as a Schema" >}}
 
-Implement `SubagentSetup` (or the appropriate runtime base class) and register a
-resource descriptor naming that implementation. Do not register a setup instance.
+{{< tab "Python" >}}
+```python
+parent.add_resource(
+    "reviewer",
+    ResourceType.AGENT,
+    InternalSubagentResourceDescriptorBuilder()
+        .agent(ReviewAgent())
+        .description("Review code for correctness and potential defects")
+        .input_schema(
+            '{"type":"object","properties":{"code":{"type":"string"}},'
+            '"required":["code"]}'
+        )
+        .build(),
+)
+```
+{{< /tab >}}
 
+{{< tab "Java" >}}
 ```java
 parent.addResource(
-    "remote_reviewer",
+    "reviewer",
     ResourceType.AGENT,
-    ResourceDescriptor.Builder.newBuilder(RemoteReviewSetup.class.getName())
-        .addInitialArgument("endpoint", "https://example.com/review")
-        .addInitialArgument("description", "Review code using the remote service")
-        .addInitialArgument("input_schema", inputSchema)
+    new InternalSubagentResourceDescriptorBuilder()
+        .agent(new ReviewAgent())
+        .description("Review code for correctness and potential defects")
+        .inputSchema(
+                "{\"type\":\"object\",\"properties\":{\"code\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"code\"]}")
         .build());
 ```
+{{< /tab >}}
 
-The Java setup exposes a public `(ResourceDescriptor, ResourceContext)`
-constructor and implements the sub-agent invocation contract. `endpoint` above
-is configuration read by the example implementation, not a built-in transport.
+{{< /tabs >}}
 
-```python
-parent.add_resource(
-    "remote_reviewer",
-    ResourceType.AGENT,
-    ResourceDescriptor(
-        clazz="my_app.agents.RemoteReviewSetup",
-        endpoint="https://example.com/review",
-        description="Review code using the remote service",
-        input_schema=input_schema,
-    ),
-)
-```
+Declare either an input type or an input schema, not both. A declaration that carries
+both is rejected when the descriptor is built. In Python the type must be a pydantic
+model, and a type that is not is rejected there too.
 
-The Python setup declares these configuration fields and implements the
-invocation contract. Both languages construct the setup at runtime from the
-descriptor. Existing code that passes a setup instance to `addResource` or
-`add_resource` must instead pass a descriptor with the same configuration.
+An input type is a shorthand for a schema. The framework derives the schema from the
+type and registers that, so use a type when you already have one and a schema when
+you do not. Nothing is ever converted into an instance of the type. The sub-agent
+receives the arguments as a map keyed by the schema's field names.
 
-## Delegate from a chat model
+For calls an action makes itself, the child registers as-is:
 
-List the child's registered name under `subagents` of the parent's chat model:
+{{< tabs "Register the Child Agent Directly" >}}
 
-```java
-ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_SETUP)
-    .addInitialArgument("connection", "ollamaConnection")
-    .addInitialArgument("model", "qwen3:8b")
-    .addInitialArgument("subagents", List.of("reviewer"))
-    .build();
+{{< tab "Python" >}}
+```python
+parent.add_resource("reviewer", ResourceType.AGENT, ReviewAgent())
 ```
+{{< /tab >}}
 
-```python
-ResourceDescriptor(
-    clazz=ResourceName.ChatModel.OLLAMA_SETUP,
-    connection="ollama_connection",
-    model="qwen3:8b",
-    subagents=["reviewer"],
-)
+{{< tab "Java" >}}
+```java
+parent.addResource("reviewer", ResourceType.AGENT, new ReviewAgent());
 ```
+{{< /tab >}}
 
-The model sees the child as a tool named `_subagent_reviewer`, described by the
-registration's description and input schema. The arguments the model sends
-become the child's input, and the child's outputs return to the model as the
-tool observation, so it can continue reasoning over them. For an internal
-sub-agent, the observation is the JSON encoding of the full output list
-described in [Call from an action](#call-from-an-action), even when the child
-emits a single output, so its shape does not depend on how many outputs the
-child emits.
+{{< /tabs >}}
 
-## Call from an action
+This form carries no description and no input declaration, so a chat model rejects
+the sub-agent at setup time rather than calling it with no arguments. Only the
+descriptor form exposes a child to a model.
 
-An action can submit input to a child directly and await its outcome. This path
-does not need an input schema.
+There is no third form. An internal sub-agent cannot be declared in YAML, because

Review Comment:
   This statement is now out of date with the target branch: main has added internal sub-agent references in YAML in commit `762c2c4ae`. For example, a parent can declare `subagents: [{name: reviewer, agent: child}]`, where `child` names another Agent in the YAML document. Both Java and Python loaders support this.
   
   When rebasing, could we replace this restriction with a reference to the YAML guide's “Declaring sub-agents” section and preserve the navigation link recently added on main?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "wenjin272 (via GitHub)" <gi...@apache.org>.
wenjin272 commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4309767153


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -24,35 +24,136 @@ specific language governing permissions and limitations
 under the License.
 -->
 
-A sub-agent handles a task delegated by a parent agent. An internal sub-agent runs
-an agent workflow in the same job. An external sub-agent calls an agent service
-through a `SubagentSetup` implementation.
+## Overview
 
-To declare parent and child workflows in YAML, see
-[Declaring sub-agents]({{< ref "docs/development/create-agent/yaml#declaring-sub-agents" >}}).
-YAML supports references to internal child Agents in the same file and descriptors
-for external sub-agents.
+A sub-agent is an agent that another agent delegates a task to. The parent hands it
+input, the sub-agent does the work, and the parent receives the outcome as a
+`SubagentResult`. Use one when the delegated work is multi-step or long-running and
+should run isolated from the parent.
 
-## Declare an internal sub-agent
+The work runs either inside the job or outside it. An *internal* sub-agent is a
+child `Agent`, including a `ReActAgent`, compiled into the parent's plan. The
+child's own actions and events run in the same job. An *external* sub-agent has no
+plan of its own. The work happens in your own code or in a remote agent service,
+outside the parent's event loop. It takes one of two forms, *deferred* or *async
+(pub/sub)*.
 
-Use `InternalSubagentResourceDescriptorBuilder` to attach a description and input
-schema to a child agent. The builder returns a normal resource descriptor for `addResource`
-or `add_resource`.
+Either way, the call is initiated by the model or by you. In *model-driven
+delegation* you list the sub-agent on a chat model setup and the model chooses when
+to delegate. In *caller-driven delegation* one of your actions submits the task
+itself.
 
+Every sub-agent call runs through
+[durable execution]({{< ref "docs/development/create-agent/actions#durable-execution" >}}).
+
+## Declare a sub-agent
+
+A sub-agent enters the parent's plan as a resource registered under
+`ResourceType.AGENT`. The name you register it under is the name that invocations and
+metrics use. What you register depends on the form. An internal sub-agent registers
+a child `Agent`. An external sub-agent registers a descriptor that names your setup
+class. A YAML file declares either form, as
+[Declaring sub-agents]({{< ref "docs/development/create-agent/yaml#declaring-sub-agents" >}})
+describes.
+
+### Internal: compile a child agent {#declare-an-internal-sub-agent}
+
+Nothing in a child's own code marks it as a sub-agent. The child is an ordinary
+agent with its own resources, actions, and event loop, and it runs standalone
+exactly as written. A parent registers it as an `AGENT` resource, and that
+registration is the whole of the difference. It gives the child a name in the
+parent's plan. It can also give the child an input schema, so a chat model can call
+it. And it supplies the caller that submits input and awaits the child's outputs.
+
+Any `Agent` can serve as the child. The two common kinds are a workflow agent and a
+`ReActAgent`. Either way, the child's plan is compiled into the parent's when the
+parent's plan is built.
+
+#### A workflow agent as the child
+
+Any workflow agent can serve as the child. The one used through this page is built
+the way
+[Request and Response Example]({{< ref "docs/development/calling-llms/chat#usage-example" >}})
+builds an agent that owns a chat model, and this page calls it `ReviewAgent`. It has
+a connection resource, a model setup, an action that turns an `InputEvent` into a
+`ChatRequestEvent`, and an action that turns the `ChatResponseEvent` into
+`OutputEvent`s.
+
+The one part a sub-agent changes is how that first action reads its input. A
+caller-driven call passes the object the caller submitted. A model-driven call
+passes the arguments map built from the input schema.
+
+{{< tabs "Read the Input in a Child Action" >}}
+
+{{< tab "Python" >}}
+```python
+# Inside ReviewAgent.
+
+
+@action(EventType.InputEvent)
+@staticmethod
+def ask(event: Event, ctx: RunnerContext) -> None:
+    task = InputEvent.from_event(event).input
+    # A model-driven call passes the arguments map; a caller-driven call
+    # passes the submitted object.
+    diff = task["code"] if isinstance(task, dict) else str(task)
+    ctx.send_event(
+        ChatRequestEvent(
+            model="review_model",

Review Comment:
   The referenced Chat with Models example registers `ollama_chat_model` in Python and `ollamaChatModel` in Java, but these replacement input actions request `review_model` / `reviewModel`. Since this revision removes the declarations of those resources, following the linked example and replacing its input action will fail to resolve the model.
   
   Could we use the resource names from the referenced example in both snippets?



##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -24,35 +24,136 @@ specific language governing permissions and limitations
 under the License.
 -->
 
-A sub-agent handles a task delegated by a parent agent. An internal sub-agent runs
-an agent workflow in the same job. An external sub-agent calls an agent service
-through a `SubagentSetup` implementation.
+## Overview
 
-To declare parent and child workflows in YAML, see
-[Declaring sub-agents]({{< ref "docs/development/create-agent/yaml#declaring-sub-agents" >}}).
-YAML supports references to internal child Agents in the same file and descriptors
-for external sub-agents.
+A sub-agent is an agent that another agent delegates a task to. The parent hands it
+input, the sub-agent does the work, and the parent receives the outcome as a
+`SubagentResult`. Use one when the delegated work is multi-step or long-running and
+should run isolated from the parent.
 
-## Declare an internal sub-agent
+The work runs either inside the job or outside it. An *internal* sub-agent is a
+child `Agent`, including a `ReActAgent`, compiled into the parent's plan. The
+child's own actions and events run in the same job. An *external* sub-agent has no
+plan of its own. The work happens in your own code or in a remote agent service,
+outside the parent's event loop. It takes one of two forms, *deferred* or *async
+(pub/sub)*.
 
-Use `InternalSubagentResourceDescriptorBuilder` to attach a description and input
-schema to a child agent. The builder returns a normal resource descriptor for `addResource`
-or `add_resource`.
+Either way, the call is initiated by the model or by you. In *model-driven
+delegation* you list the sub-agent on a chat model setup and the model chooses when
+to delegate. In *caller-driven delegation* one of your actions submits the task
+itself.
 
+Every sub-agent call runs through
+[durable execution]({{< ref "docs/development/create-agent/actions#durable-execution" >}}).
+
+## Declare a sub-agent
+
+A sub-agent enters the parent's plan as a resource registered under
+`ResourceType.AGENT`. The name you register it under is the name that invocations and
+metrics use. What you register depends on the form. An internal sub-agent registers
+a child `Agent`. An external sub-agent registers a descriptor that names your setup
+class. A YAML file declares either form, as
+[Declaring sub-agents]({{< ref "docs/development/create-agent/yaml#declaring-sub-agents" >}})
+describes.
+
+### Internal: compile a child agent {#declare-an-internal-sub-agent}
+
+Nothing in a child's own code marks it as a sub-agent. The child is an ordinary
+agent with its own resources, actions, and event loop, and it runs standalone
+exactly as written. A parent registers it as an `AGENT` resource, and that
+registration is the whole of the difference. It gives the child a name in the
+parent's plan. It can also give the child an input schema, so a chat model can call
+it. And it supplies the caller that submits input and awaits the child's outputs.
+
+Any `Agent` can serve as the child. The two common kinds are a workflow agent and a
+`ReActAgent`. Either way, the child's plan is compiled into the parent's when the
+parent's plan is built.
+
+#### A workflow agent as the child
+
+Any workflow agent can serve as the child. The one used through this page is built
+the way
+[Request and Response Example]({{< ref "docs/development/calling-llms/chat#usage-example" >}})
+builds an agent that owns a chat model, and this page calls it `ReviewAgent`. It has
+a connection resource, a model setup, an action that turns an `InputEvent` into a
+`ChatRequestEvent`, and an action that turns the `ChatResponseEvent` into
+`OutputEvent`s.
+
+The one part a sub-agent changes is how that first action reads its input. A
+caller-driven call passes the object the caller submitted. A model-driven call
+passes the arguments map built from the input schema.
+
+{{< tabs "Read the Input in a Child Action" >}}
+
+{{< tab "Python" >}}
+```python
+# Inside ReviewAgent.
+
+
+@action(EventType.InputEvent)
+@staticmethod
+def ask(event: Event, ctx: RunnerContext) -> None:
+    task = InputEvent.from_event(event).input
+    # A model-driven call passes the arguments map; a caller-driven call
+    # passes the submitted object.
+    diff = task["code"] if isinstance(task, dict) else str(task)
+    ctx.send_event(
+        ChatRequestEvent(
+            model="review_model",
+            messages=[ChatMessage.user(f"Review this diff:\n{diff}")],
+        )
+    )
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
 ```java
-import org.apache.flink.agents.api.resource.ResourceType;
-import org.apache.flink.agents.api.subagent.InternalSubagentResourceDescriptorBuilder;
+// Inside ReviewAgent.
 
-parent.addResource(
-    "reviewer",
-    ResourceType.AGENT,
-    new InternalSubagentResourceDescriptorBuilder()
-        .agent(new ReviewAgent())
-        .description("Review code for correctness and potential defects")
-        .inputType(ReviewRequest.class)
-        .build());
+@Action(EventType.InputEvent)
+public static void ask(Event event, RunnerContext ctx) throws Exception {
+    Object task = InputEvent.fromEvent(event).getInput();
+    // A model-driven call passes the arguments map; a caller-driven call
+    // passes the submitted object.
+    String diff =
+            task instanceof Map
+                    ? String.valueOf(((Map<?, ?>) task).get("code"))
+                    : String.valueOf(task);
+    ctx.sendEvent(
+            new ChatRequestEvent(
+                    "reviewModel",
+                    List.of(new ChatMessage(MessageRole.USER, "Review this diff:\n" + diff))));
+}
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
 
+While a call runs, the child behaves like a scoped agent of its own:
+
+- Its `OutputEvent`s are accumulated, in order, into the call's result instead of
+  landing on the parent's context. See
+  [the result](#the-result) for the shape.
+- It resolves resources against its own plan. Register the resources a child uses,
+  such as its chat model connection, on the child itself. In Python a resource
+  registered solely on the parent is not visible to the child.
+- Its sensory and short-term memory start empty for each call, including calls
+  continuing a session, and are shared by the child's actions within that call.
+  Long-term memory, when configured, is shared with the caller.
+- It may register and await sub-agents of its own, so nesting is unbounded in
+  depth.
+
+Registering the child takes one of two forms. You register a descriptor, or you
+register the `Agent` itself. Which one you need follows from who calls it. A chat
+model can delegate only to a sub-agent it can describe and build arguments for. So a
+child exposed to a model needs the descriptor form, and that descriptor carries a

Review Comment:
   Could we organize the internal sub-agent registration section in this order?
   
   First, briefly define the two calling modes: **model-driven calls** let the model choose when to delegate and construct the arguments, with results returned to the model; **caller-driven calls** let an action explicitly submit input and receive the result. The invocation examples can stay in the later Invoke section.
   
   Then summarize the registration requirements in a table:
   
   | Parameter | Model-driven calls | Caller-driven calls |
   |---|---|---|
   | `agent` | Required | Required |
   | `description` | Optional, recommended | Optional |
   | `input_type` / `input_schema` | Exactly one required | Optional; do not set both |
   
   Follow the table with descriptor-based registration, then the shorter direct registration form (`parent.add_resource(..., ReviewAgent())` / `parent.addResource(..., new ReviewAgent())`) for action-only use, where neither description nor input declaration is needed. The YAML reference could follow these examples.
   
   This would explain the terminology before using it in the table, help readers choose a registration form, and avoid implying that `description` is mandatory.



##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -312,30 +412,854 @@ parent.addResource(
                                 + "\"required\":[\"diff\"]}")
                 .build());
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The framework does not check the schema against the prompt. A field the prompt does
+not use is ignored, and a placeholder no field fills is left in the prompt as literal
+text. So keep the two in step when you change either.
 
+### External: write a setup class {#declare-an-external-sub-agent}
+
+An external sub-agent has no plan of its own, and it is not compiled into the
+parent's. The work happens in your own code or in a remote agent service. The parent
+only submits to it and waits for the outcome. The framework cannot perform that work
+for you, because it does not know your protocol. So you write the part that performs
+one invocation, and the framework supplies everything around it. It runs your code
+under durable execution, and after a failover it replays the recorded result instead
+of calling your code again.
+
+What you write is a setup class. Its only job is to carry out one invocation, so it
+declares no actions and no events of its own. It extends a framework base class and
+implements the methods that class declares.
+
+Registration names the setup class in a descriptor, and both languages construct the
+setup from the descriptor at runtime. Registering a setup instance instead is
+rejected.
+
+The invocation itself is carried out in one of two modes, *deferred* and *async
+(pub/sub)*. Which one you write follows from your remote interface.
+
+| | Deferred | Async (pub/sub) |
+|---|---|---|
+| Work starts | When the caller first awaits the result | When the caller submits |
+| You implement | `prepare` | `call_submit_request`, `call_query_status`, `call_fetch_result` |
+| Choose it when | One request returns the result | The remote hands back a run you query later |
+
+#### Deferred execution
+
+A deferred setup sends nothing when the caller submits. It only registers the
+invocation. The request goes out when the caller first awaits the result. So a call
+that is cancelled instead never issues its request.
+
+What you write is the interaction itself, in the `prepare` method of your setup class.
+`prepare` runs once per invocation and returns the durable call that carries it. The
+example is one blocking request, and whatever else your call does is yours to decide,
+because the framework does not know your protocol.
+
+The framework assigns `session_id` and `call_id` before it calls `prepare`, and they
+are the only values that a replay reproduces. The durable id you build from them is
+the key that recovery matches a recorded result against. When the key matches, your
+call is not run again. So build the key from those two values alone. Anything else in
+it, a timestamp for instance, makes a replay look up a different key and run the work
+a second time. Passing the same values to your own remote lets it recognize a
+re-issued request as the same one.
+
+The reconciler is optional. Omitting it leaves one crash window uncovered, and
+[Durability and failover](#durability-and-failover) describes that window. Supply one
+when the work cannot be repeated.
+
+{{< tabs "Supply a Reconciler" >}}
+
+{{< tab "Python" >}}
 ```python
-reviewer = ReActAgent(
-    chat_model=chat_model_descriptor,
-    prompt=Prompt.from_text(
-        "Review the following code diff and report any problems:\n\n{diff}"
+def prepare(
+    self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+) -> PreparedTriple:
+    key = f"{session_id}#{call_id}"
+
+    def call() -> SubagentResult:
+        return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+
+    # Consulted only during recovery, when this call is revisited and no result was
+    # recorded for it. It takes no arguments. Return the recovered outcome, because
+    # an exception here is a system-level failure that fails the action instead of
+    # reaching the caller.
+    def reconcile() -> SubagentResult:
+        # fetch_review_by_key is your own lookup; not part of Flink Agents.
+        found = fetch_review_by_key(self.endpoint, key)
+        if found is None:
+            return SubagentResult.error("the review service has no record of this call")
+        return SubagentResult.ok(found)
+
+    return (key, call, reconcile)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+// Inside the DurableCallable that prepare returns, alongside getId() and call().
+
+// Consulted only during recovery, when this call is revisited and no result was
+// recorded for it. Return the recovered outcome, because an exception here is a
+// system-level failure that fails the action instead of reaching the caller.
+@Override
+public Callable<SubagentResult> reconciler() {
+    return () -> {
+        // fetchReviewByKey is your own lookup; not part of Flink Agents.
+        String found = fetchReviewByKey(endpoint, key);
+        if (found == null) {
+            return SubagentResult.error("the review service has no record of this call");
+        }
+        return SubagentResult.ok(found);
+    };
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+{{< tabs "Deferred Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.runner_context import RunnerContext
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.deferred_subagent import (
+    DeferredSubagentSetup,
+    PreparedTriple,
+)
+
+
+def post_review(endpoint: str, prompt: Any, key: str) -> str:
+    """Your own call to the review service; not part of Flink Agents.
+
+    Keying the work lets the service treat a re-issued request as the same one.
+    """
+    ...
+
+
+class RemoteReviewSetup(DeferredSubagentSetup):
+    endpoint: str = ""
+
+    def prepare(
+        self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+    ) -> PreparedTriple:
+        key = f"{session_id}#{call_id}"
+
+        def call() -> SubagentResult:
+            try:
+                return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+            except Exception as e:
+                # An expected failure is a value; only a system fault should escape.
+                return SubagentResult.error(e)
+
+        # The first value is the key recovery matches a recorded result against, so
+        # it is built from the two ids alone. The third is the reconciler.
+        return (key, call, None)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.context.DurableCallable;
+import org.apache.flink.agents.api.context.RunnerContext;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseDeferredSubagentSetup;
+
+/**
+ * Your own call to the review service; not part of Flink Agents. Keying the work lets
+ * the service treat a re-issued request as the same one.
+ */
+static String postReview(String endpoint, Object prompt, String key) {
+    ...
+}
+
+public class RemoteReviewSetup extends BaseDeferredSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteReviewSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected DurableCallable<SubagentResult> prepare(
+            RunnerContext ctx, Object prompt, String sessionId, String callId) {
+        // Built from the two ids alone, so a replay reproduces it.
+        String key = sessionId + "#" + callId;
+        return new DurableCallable<SubagentResult>() {
+            @Override
+            public String getId() {
+                // The key recovery matches a recorded result against.
+                return key;
+            }
+
+            @Override
+            public Class<SubagentResult> getResultClass() {
+                return SubagentResult.class;
+            }
+
+            @Override
+            public SubagentResult call() {
+                try {
+                    return SubagentResult.ok(postReview(endpoint, prompt, key));
+                } catch (Exception e) {
+                    // An expected failure is a value; only a system fault should escape.
+                    return SubagentResult.error(e);
+                }
+            }
+        };
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup under the name invocations will use. Every descriptor argument
+beyond the built-in ones is configuration for the setup to read back. In Python it
+reads them as model fields, and in Java through the public
+`(ResourceDescriptor, ResourceContext)` constructor. The `endpoint` above is such
+configuration, not a built-in transport.
+
+{{< tabs "Register an External Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
+parent.add_resource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteReviewSetup",
+        endpoint="https://example.com/review",
+        description="Review code using the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
     ),
 )
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteReviewSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/review")
+        .addInitialArgument("description", "Review code using the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_reviewer
+        clazz: my_app.agents.RemoteReviewSetup
+        endpoint: https://example.com/review
+        description: Review code using the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The YAML form of that registration is the `subagents` list of an agent entry, as in
+the YAML tab above. That list can also sit at the shared document level, outside
+`agents:`, so that several agents in one file reuse it. A chat model setup declared in
+the same file also takes a `subagents` argument, but it is a list of registered names
+rather than a list of entries. See
+[delegate from a chat model](#delegate-from-a-chat-model).
+
+Two constraints apply to the entry:
 
+- No class alias is defined for `AGENT` resources, so `clazz` is always a
+  fully-qualified class path.
+- The entry's language must match the loader. A Python YAML file names a Python
+  class and may not set `type: java`. A Java YAML file must set `type: java`.
+
+#### Async (pub/sub) execution
+
+An async sub-agent talks to a service that runs the task for you. The service accepts
+the work and answers at once, and you query it later for the run's status and
+result.
+
+An async setup starts the remote run when the caller submits, through one durable
+POST. Waiting for the result then polls the run's status until it reaches a terminal
+state, and fetches it.
+
+The base class owns that submit-poll-fetch composition, along with the durable
+execution and the crash-window recovery. You supply the three transport calls it is
+built from:
+
+- `callSubmitRequest` / `call_submit_request` starts the run.
+- `callQueryStatus` / `call_query_status` returns a `RunStatus`, one of
+  `notStarted()`, `running()`, `completed()`, or `failed(error)`. It must be a
+  read-only probe with no side effects. Report an expected failure as a `FAILED`
+  status.
+- `callFetchResult` / `call_fetch_result` returns the `SubagentResult`. It must be
+  an idempotent read, because a replay may call it again. The status snapshot never
+  carries the payload, and a failed run is reported without fetching at all.
+
+{{< tabs "Async Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.async_subagent import BaseAsyncSubagentSetup, RunStatus
+
+# client(...) and task_id stand for your own transport client and run-id
+# scheme; neither is part of Flink Agents.
+
+
+class RemoteRunSetup(BaseAsyncSubagentSetup):
+    endpoint: str = ""
+
+    def call_submit_request(self, session_id: str, call_id: str, prompt: Any) -> None:
+        client(self.endpoint).submit(prompt, task_id=f"{session_id}#{call_id}")
+
+    def call_query_status(self, session_id: str, call_id: str) -> RunStatus:
+        probe = client(self.endpoint).status(task_id=f"{session_id}#{call_id}")
+        if probe is None:
+            return RunStatus.not_started()
+        if probe.succeeded:
+            return RunStatus.completed()
+        if probe.failed:
+            return RunStatus.failed(probe.error)
+        return RunStatus.running()
+
+    def call_fetch_result(self, session_id: str, call_id: str) -> SubagentResult:
+        return SubagentResult.ok(
+            client(self.endpoint).fetch(task_id=f"{session_id}#{call_id}")
+        )
+
+    # Optional, and a no-op by default, so a cancelled call leaves the remote run
+    # going. A replay may propagate the same cancellation again, so your remote has
+    # to accept it twice.
+    def call_cancel_request(self, session_id: str, call_id: str) -> None:
+        client(self.endpoint).cancel(task_id=f"{session_id}#{call_id}")
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseAsyncSubagentSetup;
+
+// client() and taskId(...) stand for your own transport client and run-id
+// scheme; neither is part of Flink Agents.
+public class RemoteRunSetup extends BaseAsyncSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteRunSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected void callSubmitRequest(String sessionId, String callId, Object prompt)
+            throws Exception {
+        client().submit(String.valueOf(prompt), taskId(sessionId, callId));
+    }
+
+    @Override
+    protected RunStatus callQueryStatus(String sessionId, String callId) {
+        // TaskStatus is your own transport type, not a framework type.
+        TaskStatus probe = client().status(taskId(sessionId, callId));
+        if (probe == null) {
+            return RunStatus.notStarted();
+        }
+        if (probe.succeeded()) {
+            return RunStatus.completed();
+        }
+        if (probe.failed()) {
+            return RunStatus.failed(probe.error());
+        }
+        return RunStatus.running();
+    }
+
+    @Override
+    protected SubagentResult callFetchResult(String sessionId, String callId) throws Exception {
+        return client().fetchResult(taskId(sessionId, callId));
+    }
+
+    // Optional, and a no-op by default, so a cancelled call leaves the remote run
+    // going. A replay may propagate the same cancellation again, so your remote has
+    // to accept it twice.
+    @Override
+    protected void callCancelRequest(String sessionId, String callId) {
+        client().cancel(taskId(sessionId, callId));
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup like any external sub-agent. The async form adds one descriptor
+argument, `status_poll_interval_millis`, and it defaults to 500.
+
+{{< tabs "Register an Async Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
 parent.add_resource(
-    "reviewer",
+    "remote_runner",
     ResourceType.AGENT,
-    InternalSubagentResourceDescriptorBuilder()
-    .agent(reviewer)
-    .description("Reviews a code diff and reports problems.")
-    # "diff" matches the {diff} placeholder of the child's prompt.
-    .input_schema(
-        '{"type":"object","properties":{"diff":{"type":"string"}},'
-        '"required":["diff"]}'
-    )
-    .build(),
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteRunSetup",
+        endpoint="https://example.com/runs",
+        description="Run a review task on the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
+        status_poll_interval_millis=500,
+    ),
 )
 ```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_runner",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteRunSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/runs")
+        .addInitialArgument("description", "Run a review task on the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .addInitialArgument("status_poll_interval_millis", 500L)
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_runner
+        clazz: my_app.agents.RemoteRunSetup
+        endpoint: https://example.com/runs
+        description: Run a review task on the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+        status_poll_interval_millis: 500
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The base class also recovers the crash window between "POST sent" and "POST durably
+recorded". On recovery it probes the run's status first, and it re-posts only when the
+probe reports that the run never started. Override `reconcileSubmitRequest` /
+`reconcile_submit_request` only if your service needs a different repair.
+
+## Invoke a sub-agent
+
+A sub-agent is invoked in one of two ways, model-driven or caller-driven. The
+difference you see is where the result goes.
+
+In model-driven delegation the result returns to the model. The sub-agent is offered
+to the model as a tool, so your action sends a `ChatRequestEvent` and the model
+answers with a call to the sub-agent. The result goes back to the model as the tool
+observation, and the model reasons over it until a final `ChatResponseEvent` reaches
+your action.
+
+In caller-driven delegation the result returns to your action. An action that needs
+the delegated outcome calls the sub-agent itself, with no model in the loop.
+
+Both ways end in the same call. Your action, or the framework on the model's behalf,
+runs `submit(ctx, prompt)` on the registered name. That call returns a
+`SubagentFuture`, which is the handle for that one call. Awaiting the handle yields
+the call's `SubagentResult`.
+
+### Delegate from a chat model
+
+List the registered name under `subagents` of the parent's chat model:
+
+{{< tabs "Declare Sub-agents on a Chat Model" >}}
+
+{{< tab "Python" >}}
+```python
+ResourceDescriptor(
+    clazz=ResourceName.ChatModel.OLLAMA_SETUP,
+    connection="ollama_connection",
+    model="qwen3:8b",
+    subagents=["reviewer"],
+)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_SETUP)
+    .addInitialArgument("connection", "ollamaConnection")
+    .addInitialArgument("model", "qwen3:8b")
+    .addInitialArgument("subagents", List.of("reviewer"))
+    .build();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The model sees `reviewer` as a tool, described by the registration's description and
+input schema. A schema is required for model-selected calls. A missing description
+falls back to a generic delegation description.
+
+The arguments the model sends become the sub-agent's input, and its outputs return to
+the model as the tool observation. The model can then keep reasoning over them. For
+an internal sub-agent, the observation is the JSON encoding of the full output list
+described in [the result](#the-result). That holds even when the child emits a single
+output, so the shape does not depend on how many outputs the child emits.
+
+An external setup class can state the shape of that observation. It declares
+`result_type()` in Python or `getResultType()` in Java, and the default reports the
+result as it arrived. Declare one to narrow a wider payload, or to convert a payload
+that is not already JSON-compatible. An internal sub-agent has no such declaration, so
+its observation is always the JSON encoding of its output list. This shapes the
+observation only. A caller that awaits the handle gets the result the sub-agent
+produced, as [the result](#the-result) describes.
+
+The external sub-agents `remote_reviewer` and `remote_runner` are offered the same
+way. Only their registration differs.
+
+### Call from an action
+
+An action can submit input to a registered sub-agent directly and await its
+outcome. This path needs no description and no input declaration, so it also works
+for a child registered as-is.
+
+{{< tabs "Call a Sub-agent From an Action" >}}
+
+{{< tab "Python" >}}
+```python
+class DelegatingAgent(Agent):
+    @action(EventType.InputEvent)
+    @staticmethod
+    async def delegate(event: Event, ctx: RunnerContext) -> None:
+        reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+        future = await reviewer.submit(ctx, "review the diff")
+        result = await future
+        if result.success:
+            ctx.send_event(OutputEvent(output=result.result[0]))
+        else:
+            ctx.send_event(OutputEvent(output=f"failed: {result.error_message}"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+public class DelegatingAgent extends Agent {
+
+    @Action(EventType.InputEvent)
+    public static void delegate(Event event, RunnerContext ctx) throws Exception {
+        SubagentSetup reviewer =
+                (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+        SubagentResult result = reviewer.submit(ctx, "review the diff").await();
+        if (result.isSuccess()) {
+            String answer = String.valueOf(((List<?>) result.getResult()).get(0));
+            ctx.sendEvent(new OutputEvent(answer));
+        } else {
+            ctx.sendEvent(new OutputEvent("failed: " + result.getErrorMessage()));
+        }
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+What you pass to `submit` is what the sub-agent receives. An internal child gets it
+as its `InputEvent` payload, and a model-driven call passes the arguments map
+instead. What comes back is a `SubagentResult`. For the internal sub-agent used here,
+its `result` is the list of `OutputEvent` payloads the child emitted. That is why the
+example reads `result.result[0]`. An external sub-agent returns whatever its setup
+produces.
+
+In Python `submit` is a coroutine, so the call is awaited in two phases and the
+action must be `async`. In Java `submit` returns the handle directly and `await()`
+yields the result. The same two calls drive `remote_reviewer` and `remote_runner`
+unchanged. Only the registration differs.
+
+## Work with open handles

Review Comment:
   Could we make these section titles more explicit?
   
   - Rename **“Work with open handles”** to **“Manage sub-agent calls”**. “Open handles” is abstract and can suggest file or resource handles; the section actually covers grouping calls, continuing sessions, and cancellation.
   - Rename **“The outcome of a call”** to **“Results and failures”**, and move durability/recovery and monitoring into separate sections. This makes it clearer where to find return values and error handling, while giving recovery and observability their own headings.



##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -312,30 +412,854 @@ parent.addResource(
                                 + "\"required\":[\"diff\"]}")
                 .build());
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The framework does not check the schema against the prompt. A field the prompt does
+not use is ignored, and a placeholder no field fills is left in the prompt as literal
+text. So keep the two in step when you change either.
 
+### External: write a setup class {#declare-an-external-sub-agent}
+
+An external sub-agent has no plan of its own, and it is not compiled into the
+parent's. The work happens in your own code or in a remote agent service. The parent
+only submits to it and waits for the outcome. The framework cannot perform that work
+for you, because it does not know your protocol. So you write the part that performs
+one invocation, and the framework supplies everything around it. It runs your code
+under durable execution, and after a failover it replays the recorded result instead
+of calling your code again.
+
+What you write is a setup class. Its only job is to carry out one invocation, so it
+declares no actions and no events of its own. It extends a framework base class and
+implements the methods that class declares.
+
+Registration names the setup class in a descriptor, and both languages construct the
+setup from the descriptor at runtime. Registering a setup instance instead is
+rejected.
+
+The invocation itself is carried out in one of two modes, *deferred* and *async
+(pub/sub)*. Which one you write follows from your remote interface.
+
+| | Deferred | Async (pub/sub) |
+|---|---|---|
+| Work starts | When the caller first awaits the result | When the caller submits |
+| You implement | `prepare` | `call_submit_request`, `call_query_status`, `call_fetch_result` |
+| Choose it when | One request returns the result | The remote hands back a run you query later |
+
+#### Deferred execution
+
+A deferred setup sends nothing when the caller submits. It only registers the
+invocation. The request goes out when the caller first awaits the result. So a call
+that is cancelled instead never issues its request.
+
+What you write is the interaction itself, in the `prepare` method of your setup class.
+`prepare` runs once per invocation and returns the durable call that carries it. The
+example is one blocking request, and whatever else your call does is yours to decide,
+because the framework does not know your protocol.
+
+The framework assigns `session_id` and `call_id` before it calls `prepare`, and they
+are the only values that a replay reproduces. The durable id you build from them is
+the key that recovery matches a recorded result against. When the key matches, your
+call is not run again. So build the key from those two values alone. Anything else in
+it, a timestamp for instance, makes a replay look up a different key and run the work
+a second time. Passing the same values to your own remote lets it recognize a
+re-issued request as the same one.
+
+The reconciler is optional. Omitting it leaves one crash window uncovered, and
+[Durability and failover](#durability-and-failover) describes that window. Supply one
+when the work cannot be repeated.
+
+{{< tabs "Supply a Reconciler" >}}
+
+{{< tab "Python" >}}
 ```python
-reviewer = ReActAgent(
-    chat_model=chat_model_descriptor,
-    prompt=Prompt.from_text(
-        "Review the following code diff and report any problems:\n\n{diff}"
+def prepare(
+    self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+) -> PreparedTriple:
+    key = f"{session_id}#{call_id}"
+
+    def call() -> SubagentResult:
+        return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+
+    # Consulted only during recovery, when this call is revisited and no result was
+    # recorded for it. It takes no arguments. Return the recovered outcome, because
+    # an exception here is a system-level failure that fails the action instead of
+    # reaching the caller.
+    def reconcile() -> SubagentResult:
+        # fetch_review_by_key is your own lookup; not part of Flink Agents.
+        found = fetch_review_by_key(self.endpoint, key)
+        if found is None:
+            return SubagentResult.error("the review service has no record of this call")
+        return SubagentResult.ok(found)
+
+    return (key, call, reconcile)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+// Inside the DurableCallable that prepare returns, alongside getId() and call().
+
+// Consulted only during recovery, when this call is revisited and no result was
+// recorded for it. Return the recovered outcome, because an exception here is a
+// system-level failure that fails the action instead of reaching the caller.
+@Override
+public Callable<SubagentResult> reconciler() {
+    return () -> {
+        // fetchReviewByKey is your own lookup; not part of Flink Agents.
+        String found = fetchReviewByKey(endpoint, key);
+        if (found == null) {
+            return SubagentResult.error("the review service has no record of this call");
+        }
+        return SubagentResult.ok(found);
+    };
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+{{< tabs "Deferred Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.runner_context import RunnerContext
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.deferred_subagent import (
+    DeferredSubagentSetup,
+    PreparedTriple,
+)
+
+
+def post_review(endpoint: str, prompt: Any, key: str) -> str:
+    """Your own call to the review service; not part of Flink Agents.
+
+    Keying the work lets the service treat a re-issued request as the same one.
+    """
+    ...
+
+
+class RemoteReviewSetup(DeferredSubagentSetup):
+    endpoint: str = ""
+
+    def prepare(
+        self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+    ) -> PreparedTriple:
+        key = f"{session_id}#{call_id}"
+
+        def call() -> SubagentResult:
+            try:
+                return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+            except Exception as e:
+                # An expected failure is a value; only a system fault should escape.
+                return SubagentResult.error(e)
+
+        # The first value is the key recovery matches a recorded result against, so
+        # it is built from the two ids alone. The third is the reconciler.
+        return (key, call, None)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.context.DurableCallable;
+import org.apache.flink.agents.api.context.RunnerContext;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseDeferredSubagentSetup;
+
+/**
+ * Your own call to the review service; not part of Flink Agents. Keying the work lets
+ * the service treat a re-issued request as the same one.
+ */
+static String postReview(String endpoint, Object prompt, String key) {
+    ...
+}
+
+public class RemoteReviewSetup extends BaseDeferredSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteReviewSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected DurableCallable<SubagentResult> prepare(
+            RunnerContext ctx, Object prompt, String sessionId, String callId) {
+        // Built from the two ids alone, so a replay reproduces it.
+        String key = sessionId + "#" + callId;
+        return new DurableCallable<SubagentResult>() {
+            @Override
+            public String getId() {
+                // The key recovery matches a recorded result against.
+                return key;
+            }
+
+            @Override
+            public Class<SubagentResult> getResultClass() {
+                return SubagentResult.class;
+            }
+
+            @Override
+            public SubagentResult call() {
+                try {
+                    return SubagentResult.ok(postReview(endpoint, prompt, key));
+                } catch (Exception e) {
+                    // An expected failure is a value; only a system fault should escape.
+                    return SubagentResult.error(e);
+                }
+            }
+        };
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup under the name invocations will use. Every descriptor argument
+beyond the built-in ones is configuration for the setup to read back. In Python it
+reads them as model fields, and in Java through the public
+`(ResourceDescriptor, ResourceContext)` constructor. The `endpoint` above is such
+configuration, not a built-in transport.
+
+{{< tabs "Register an External Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
+parent.add_resource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteReviewSetup",
+        endpoint="https://example.com/review",
+        description="Review code using the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
     ),
 )
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteReviewSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/review")
+        .addInitialArgument("description", "Review code using the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_reviewer
+        clazz: my_app.agents.RemoteReviewSetup
+        endpoint: https://example.com/review
+        description: Review code using the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The YAML form of that registration is the `subagents` list of an agent entry, as in
+the YAML tab above. That list can also sit at the shared document level, outside
+`agents:`, so that several agents in one file reuse it. A chat model setup declared in
+the same file also takes a `subagents` argument, but it is a list of registered names
+rather than a list of entries. See
+[delegate from a chat model](#delegate-from-a-chat-model).
+
+Two constraints apply to the entry:
 
+- No class alias is defined for `AGENT` resources, so `clazz` is always a
+  fully-qualified class path.
+- The entry's language must match the loader. A Python YAML file names a Python
+  class and may not set `type: java`. A Java YAML file must set `type: java`.
+
+#### Async (pub/sub) execution
+
+An async sub-agent talks to a service that runs the task for you. The service accepts
+the work and answers at once, and you query it later for the run's status and
+result.
+
+An async setup starts the remote run when the caller submits, through one durable
+POST. Waiting for the result then polls the run's status until it reaches a terminal
+state, and fetches it.
+
+The base class owns that submit-poll-fetch composition, along with the durable
+execution and the crash-window recovery. You supply the three transport calls it is
+built from:
+
+- `callSubmitRequest` / `call_submit_request` starts the run.
+- `callQueryStatus` / `call_query_status` returns a `RunStatus`, one of
+  `notStarted()`, `running()`, `completed()`, or `failed(error)`. It must be a
+  read-only probe with no side effects. Report an expected failure as a `FAILED`
+  status.
+- `callFetchResult` / `call_fetch_result` returns the `SubagentResult`. It must be
+  an idempotent read, because a replay may call it again. The status snapshot never
+  carries the payload, and a failed run is reported without fetching at all.
+
+{{< tabs "Async Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.async_subagent import BaseAsyncSubagentSetup, RunStatus
+
+# client(...) and task_id stand for your own transport client and run-id
+# scheme; neither is part of Flink Agents.
+
+
+class RemoteRunSetup(BaseAsyncSubagentSetup):
+    endpoint: str = ""
+
+    def call_submit_request(self, session_id: str, call_id: str, prompt: Any) -> None:
+        client(self.endpoint).submit(prompt, task_id=f"{session_id}#{call_id}")
+
+    def call_query_status(self, session_id: str, call_id: str) -> RunStatus:
+        probe = client(self.endpoint).status(task_id=f"{session_id}#{call_id}")
+        if probe is None:
+            return RunStatus.not_started()
+        if probe.succeeded:
+            return RunStatus.completed()
+        if probe.failed:
+            return RunStatus.failed(probe.error)
+        return RunStatus.running()
+
+    def call_fetch_result(self, session_id: str, call_id: str) -> SubagentResult:
+        return SubagentResult.ok(
+            client(self.endpoint).fetch(task_id=f"{session_id}#{call_id}")
+        )
+
+    # Optional, and a no-op by default, so a cancelled call leaves the remote run
+    # going. A replay may propagate the same cancellation again, so your remote has
+    # to accept it twice.
+    def call_cancel_request(self, session_id: str, call_id: str) -> None:
+        client(self.endpoint).cancel(task_id=f"{session_id}#{call_id}")
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseAsyncSubagentSetup;
+
+// client() and taskId(...) stand for your own transport client and run-id
+// scheme; neither is part of Flink Agents.
+public class RemoteRunSetup extends BaseAsyncSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteRunSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected void callSubmitRequest(String sessionId, String callId, Object prompt)
+            throws Exception {
+        client().submit(String.valueOf(prompt), taskId(sessionId, callId));
+    }
+
+    @Override
+    protected RunStatus callQueryStatus(String sessionId, String callId) {
+        // TaskStatus is your own transport type, not a framework type.
+        TaskStatus probe = client().status(taskId(sessionId, callId));
+        if (probe == null) {
+            return RunStatus.notStarted();
+        }
+        if (probe.succeeded()) {
+            return RunStatus.completed();
+        }
+        if (probe.failed()) {
+            return RunStatus.failed(probe.error());
+        }
+        return RunStatus.running();
+    }
+
+    @Override
+    protected SubagentResult callFetchResult(String sessionId, String callId) throws Exception {
+        return client().fetchResult(taskId(sessionId, callId));
+    }
+
+    // Optional, and a no-op by default, so a cancelled call leaves the remote run
+    // going. A replay may propagate the same cancellation again, so your remote has
+    // to accept it twice.
+    @Override
+    protected void callCancelRequest(String sessionId, String callId) {
+        client().cancel(taskId(sessionId, callId));
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup like any external sub-agent. The async form adds one descriptor
+argument, `status_poll_interval_millis`, and it defaults to 500.
+
+{{< tabs "Register an Async Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
 parent.add_resource(
-    "reviewer",
+    "remote_runner",
     ResourceType.AGENT,
-    InternalSubagentResourceDescriptorBuilder()
-    .agent(reviewer)
-    .description("Reviews a code diff and reports problems.")
-    # "diff" matches the {diff} placeholder of the child's prompt.
-    .input_schema(
-        '{"type":"object","properties":{"diff":{"type":"string"}},'
-        '"required":["diff"]}'
-    )
-    .build(),
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteRunSetup",
+        endpoint="https://example.com/runs",
+        description="Run a review task on the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
+        status_poll_interval_millis=500,
+    ),
 )
 ```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_runner",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteRunSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/runs")
+        .addInitialArgument("description", "Run a review task on the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .addInitialArgument("status_poll_interval_millis", 500L)
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_runner
+        clazz: my_app.agents.RemoteRunSetup
+        endpoint: https://example.com/runs
+        description: Run a review task on the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+        status_poll_interval_millis: 500
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The base class also recovers the crash window between "POST sent" and "POST durably
+recorded". On recovery it probes the run's status first, and it re-posts only when the
+probe reports that the run never started. Override `reconcileSubmitRequest` /
+`reconcile_submit_request` only if your service needs a different repair.
+
+## Invoke a sub-agent
+
+A sub-agent is invoked in one of two ways, model-driven or caller-driven. The
+difference you see is where the result goes.
+
+In model-driven delegation the result returns to the model. The sub-agent is offered
+to the model as a tool, so your action sends a `ChatRequestEvent` and the model
+answers with a call to the sub-agent. The result goes back to the model as the tool
+observation, and the model reasons over it until a final `ChatResponseEvent` reaches
+your action.
+
+In caller-driven delegation the result returns to your action. An action that needs
+the delegated outcome calls the sub-agent itself, with no model in the loop.
+
+Both ways end in the same call. Your action, or the framework on the model's behalf,
+runs `submit(ctx, prompt)` on the registered name. That call returns a
+`SubagentFuture`, which is the handle for that one call. Awaiting the handle yields
+the call's `SubagentResult`.
+
+### Delegate from a chat model
+
+List the registered name under `subagents` of the parent's chat model:
+
+{{< tabs "Declare Sub-agents on a Chat Model" >}}
+
+{{< tab "Python" >}}
+```python
+ResourceDescriptor(
+    clazz=ResourceName.ChatModel.OLLAMA_SETUP,
+    connection="ollama_connection",
+    model="qwen3:8b",
+    subagents=["reviewer"],
+)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_SETUP)
+    .addInitialArgument("connection", "ollamaConnection")
+    .addInitialArgument("model", "qwen3:8b")
+    .addInitialArgument("subagents", List.of("reviewer"))
+    .build();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The model sees `reviewer` as a tool, described by the registration's description and
+input schema. A schema is required for model-selected calls. A missing description
+falls back to a generic delegation description.
+
+The arguments the model sends become the sub-agent's input, and its outputs return to
+the model as the tool observation. The model can then keep reasoning over them. For
+an internal sub-agent, the observation is the JSON encoding of the full output list
+described in [the result](#the-result). That holds even when the child emits a single
+output, so the shape does not depend on how many outputs the child emits.
+
+An external setup class can state the shape of that observation. It declares
+`result_type()` in Python or `getResultType()` in Java, and the default reports the
+result as it arrived. Declare one to narrow a wider payload, or to convert a payload
+that is not already JSON-compatible. An internal sub-agent has no such declaration, so
+its observation is always the JSON encoding of its output list. This shapes the
+observation only. A caller that awaits the handle gets the result the sub-agent
+produced, as [the result](#the-result) describes.
+
+The external sub-agents `remote_reviewer` and `remote_runner` are offered the same
+way. Only their registration differs.
+
+### Call from an action
+
+An action can submit input to a registered sub-agent directly and await its
+outcome. This path needs no description and no input declaration, so it also works
+for a child registered as-is.
+
+{{< tabs "Call a Sub-agent From an Action" >}}
+
+{{< tab "Python" >}}
+```python
+class DelegatingAgent(Agent):
+    @action(EventType.InputEvent)
+    @staticmethod
+    async def delegate(event: Event, ctx: RunnerContext) -> None:
+        reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+        future = await reviewer.submit(ctx, "review the diff")
+        result = await future
+        if result.success:
+            ctx.send_event(OutputEvent(output=result.result[0]))
+        else:
+            ctx.send_event(OutputEvent(output=f"failed: {result.error_message}"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+public class DelegatingAgent extends Agent {
+
+    @Action(EventType.InputEvent)
+    public static void delegate(Event event, RunnerContext ctx) throws Exception {
+        SubagentSetup reviewer =
+                (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+        SubagentResult result = reviewer.submit(ctx, "review the diff").await();
+        if (result.isSuccess()) {
+            String answer = String.valueOf(((List<?>) result.getResult()).get(0));
+            ctx.sendEvent(new OutputEvent(answer));
+        } else {
+            ctx.sendEvent(new OutputEvent("failed: " + result.getErrorMessage()));
+        }
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+What you pass to `submit` is what the sub-agent receives. An internal child gets it
+as its `InputEvent` payload, and a model-driven call passes the arguments map
+instead. What comes back is a `SubagentResult`. For the internal sub-agent used here,
+its `result` is the list of `OutputEvent` payloads the child emitted. That is why the
+example reads `result.result[0]`. An external sub-agent returns whatever its setup
+produces.
+
+In Python `submit` is a coroutine, so the call is awaited in two phases and the
+action must be `async`. In Java `submit` returns the handle directly and `await()`
+yields the result. The same two calls drive `remote_reviewer` and `remote_runner`
+unchanged. Only the registration differs.
+
+## Work with open handles
+
+A `SubagentFuture` is live from `submit` until it is awaited or cancelled. Awaiting
+it yields the call's `SubagentResult`. It also carries the call's `session_id` and
+`call_id`, so you can read them to continue the session or to match the call against
+your own records.
+
+### Fan out to several sub-agents
+
+`combine` groups handles into a `SubagentFutures`, and resolving the batch resolves
+every handle in it. `gather` does not accept a `SubagentFuture`, so handles batch this
+way instead. The results come back in the order the handles were added.
+
+{{< tabs "Fan Out to Several Sub-agents" >}}
+
+{{< tab "Python" >}}
+```python
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+first = await reviewer.submit(ctx, "review the diff")
+second = await reviewer.submit(ctx, "review the tests")
+
+outcomes = await first.combine(second)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture first = reviewer.submit(ctx, "review the diff");
+SubagentFuture second = reviewer.submit(ctx, "review the tests");
+
+List<SubagentResult> outcomes = first.combine(second).awaitAll();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+### Continue a session
+
+Pass the `session_id` of an earlier handle to continue that conversation. The new
+call gets a fresh `call_id` in the same session. Whether a conversation can be
+continued across actions is up to the implementation.
+
+{{< tabs "Continue a Session" >}}
+
+{{< tab "Python" >}}
+```python
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+first = await reviewer.submit(ctx, "review the diff")
+first_result = await first
+
+second = await reviewer.submit(ctx, "now the tests", first.session_id)
+second_result = await second
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture first = reviewer.submit(ctx, "review the diff");
+SubagentResult firstResult = first.await();
+
+SubagentFuture second = reviewer.submit(ctx, "now the tests", first.getSessionId());
+SubagentResult secondResult = second.await();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+### Cancel and resolve
+
+Resolve every handle you submit. An action that finishes with a handle it neither
+awaited nor cancelled fails with `Action <name> finished without resolving the
+sub-agent calls it submitted: <ids>`. Cancelling counts as resolving, and awaiting a
+cancelled handle throws `CancellationException` in Java and raises `CancelledError` in
+Python.
+
+{{< tabs "Cancel a Sub-agent Call" >}}
+
+{{< tab "Python" >}}
+```python
+from concurrent.futures import CancelledError
+
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+future = await reviewer.submit(ctx, "review the diff")
+future.cancel()
+
+try:
+    await future
+except CancelledError:
+    ctx.send_event(OutputEvent(output="cancelled"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture future = reviewer.submit(ctx, "review the diff");
+future.cancel();
+
+try {
+    future.await();
+} catch (CancellationException e) {
+    ctx.sendEvent(new OutputEvent("cancelled"));
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+`cancel()` on a deferred handle takes effect only before the request is issued.
+
+## The outcome of a call
+
+### The result
+
+The awaited value of every call is a `SubagentResult`, which carries the outcome in
+three fields:
+
+| Field | Java | Carries |
+|---|---|---|
+| `success` | `isSuccess()` | Whether the call succeeded |
+| `result` | `getResult()` | The answer on success. For an internal sub-agent, the list of `OutputEvent` payloads in emission order. For an external one, whatever the setup produces, such as a string or an object |
+| `error_message` | `getErrorMessage()` | The reason on failure |
+
+A result a model observes must be JSON-compatible. That means string map keys,
+finite numbers, and no cycles. The framework normalizes the result on its way into
+the tool observation, and one it cannot normalize fails the delegation.
+
+A failover restores the recorded result, and the two languages restore it
+differently. Python persists the payload with cloudpickle, so a replay hands it back
+with the type the sub-agent produced. Java persists it as JSON, so `getResult()`
+hands back a map after a replay, because recovery re-binds the recorded payload
+without your type. `getResult(Class<T>)` converts on the first run and after a
+replay alike, so prefer it when you want your own type back.
+
+{{< tabs "Read a SubagentResult" >}}
+
+{{< tab "Python" >}}
+```python
+if result.success:
+    # The payload keeps the type the sub-agent produced, after a replay too.
+    ctx.send_event(OutputEvent(output=result.result["verdict"]))
+else:
+    ctx.send_event(OutputEvent(output=f"failed: {result.error_message}"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+if (result.isSuccess()) {
+    // Review is your own type, the shape this sub-agent produces.
+    Review review = result.getResult(Review.class);
+    ctx.sendEvent(new OutputEvent(review.verdict));
+} else {
+    ctx.sendEvent(new OutputEvent("failed: " + result.getErrorMessage()));
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+### How failures surface
+
+An expected failure is a value, and an unexpected one is an exception. Return
+`SubagentResult.error(...)` for anything the caller or the model should react to. An
+exception escaping the durable call is a system-level failure. It fails the action and
+triggers recovery.
+
+- A failed result reaches a caller as `success == false` with the reason in
+  `error_message`.
+- A failed result reaches a model as a tool observation that names the sub-agent and
+  the reason, so the model can decide what to do next. An exception while submitting
+  or awaiting is reported the same way.
+- A failing child action does not fail the job. It surfaces through the caller's
+  result. The text in `error_message` is not a contract, so test for the failure
+  rather than for the message.
+- A result that is not JSON-compatible fails the delegation rather than the job.
+
+### Durability and failover
+
+A sub-agent call always runs through
+[durable execution]({{< ref "docs/development/create-agent/actions#durable-execution" >}}).
+Its result is persisted and replayed on recovery, so the work is not repeated after
+a failover. There is no durability toggle and no sub-agent timeout option.
+
+- Identities are replay-stable. Without explicit ids, the framework assigns
+  `session_id` and `call_id` from the executing action and the sub-agent's resource
+  name, so a replay of the same call gets the same ids. The ids are opaque, so do not
+  parse them. Two sub-agents called from one action never collide, because the
+  resource name is part of the namespace.
+- A deferred call runs under the durable id you build in `prepare`, so a replay
+  re-issues the same logical invocation. A remote keyed on the same values then reuses
+  the original work instead of starting a second one. The reconciler is optional.
+  Without it, a crash between the call landing and its result being persisted re-runs
+  the sub-agent and duplicates its side effects, so supply one whenever the work
+  cannot be repeated.
+- An async call reuses one identity for both its POST and its poll, so your service
+  sees at most one run per call.
+- An internal sub-agent's child actions have their own persisted state, so a replay
+  reuses the recorded child output instead of running the child again.
+
+### Metrics and Trace Log
+
+Only the delegations a model requests are reported. A sub-agent you drive yourself
+from an action emits no sub-agent metrics and no sub-agent trace records. Add
+[custom metrics]({{< ref "docs/operations/monitoring#how-to-add-custom-metrics" >}})
+if you need to observe that path.
+
+A model-driven delegation is counted under a `subagent` scope keyed by the registered
+sub-agent name. It is reported there only and never also as a tool, so the scope's
+cardinality is bounded by your plan.
+
+| Metric | Counts |
+|---|---|
+| `numOfSubagentCallsSucceeded` | Delegations that returned a successful result |
+| `numOfSubagentCallsFailed` | Delegations that failed, either while submitting or awaiting, or by returning a failed result |
+| `subagentCallLatencyMs` | Time from handing the prompt to the sub-agent until its result is observed |
+
+Full metric paths and types are in
+[Execution Metrics]({{< ref "docs/operations/monitoring#execution-metrics" >}}).
+
+The same delegations appear in
+[Trace Log]({{< ref "docs/operations/monitoring#trace-log" >}}) as call records with
+`entityType: "subagent"`. The default target records events only, so select the
+`subagent` scope, or `ALL`, to get them.
+
+## Restrictions
 
-The framework does not check the schema against the prompt. A field the prompt
-does not use is ignored, and a placeholder no field fills is left in the prompt
-as literal text, so keep the two in step when changing either.
+- Recursive registration is rejected when the plan is built, whether the cycle runs
+  through a child, through the root agent, or directly back to itself.
+- Registering the same child `Agent` instance under two names compiles it once and
+  shares the plan.
+- `_subagent_` is a reserved prefix. A sub-agent reaches a model as a tool whose name
+  carries it, and no tool may be registered under the prefix, including a tool an MCP
+  server advertises. A tool and a sub-agent may therefore share a name, because the
+  prefix keeps the two namespaces apart. See
+  [Tool Use]({{< ref "docs/development/calling-llms/tool_use" >}}).
+- A sub-agent declared in one language cannot be driven from the other. Delegation
+  resolves the `AGENT` resource in the executing language only.
+- Resolving an internal sub-agent call in Java requires JDK 21+ with the Continuation
+  API. On an older runtime the call fails fast rather than hanging. Python callers
+  and external sub-agents have no JDK requirement.
+- When a model emits several sub-agent calls in one turn, and `tool-call.async` is
+  enabled with `async.batch.parallelism` above 1, the framework submits every call
+  before awaiting any of them. An async sub-agent starts its remote run during that
+  submit, so those runs overlap. A deferred or internal call does its work while it
+  is awaited, so it does not overlap this way, and batching handles yourself with
+  [combine](#fan-out-to-several-sub-agents) does not change that. Results still land

Review Comment:
   The statement that `combine` does not change this behavior is too broad for internal sub-agents. `SubagentFutureGroup` prepares all handles before waiting, and `InternalSubagentSetup.prepare` sends the child's bootstrap event. Combining two internal calls therefore bootstraps both children before awaiting either, allowing their work to overlap.
   
   Could we distinguish this from external deferred setups whose actual request runs inside the callable? The model-driven submit-then-await path and explicitly combining internal handles do not have the same behavior.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "sangkyoonnam (via GitHub)" <gi...@apache.org>.
sangkyoonnam commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4293522819


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -24,30 +24,170 @@ specific language governing permissions and limitations
 under the License.
 -->
 
-A sub-agent handles a task delegated by a parent agent. An internal sub-agent runs
-an agent workflow in the same job. An external sub-agent calls an agent service
-through a `SubagentSetup` implementation.
+## Overview
 
-## Declare an internal sub-agent
+A sub-agent is an agent that another agent delegates a task to. The parent hands it
+input, the sub-agent does the work, and the parent receives the outcome as a
+`SubagentResult`. Use one when the delegated work is multi-step or long-running and
+should run isolated from the parent.
 
-Use `InternalSubagentResourceDescriptorBuilder` to attach a description and input
-schema to a child agent. The builder returns a normal resource descriptor for `addResource`
-or `add_resource`.
+The work runs either inside the job or outside it. An *internal* sub-agent is a
+child `Agent`, including a `ReActAgent`, compiled into the parent's plan. The
+child's own actions and events run in the same job. An *external* sub-agent has no
+plan of its own. The work happens in your own code or in a remote agent service,
+outside the parent's event loop. It takes one of two forms, *deferred* or *async
+(pub/sub)*.
 
+Either way, the call is initiated by the model or by you. In *model-driven
+delegation* you list the sub-agent on a chat model setup and the model chooses when
+to delegate. In *caller-driven delegation* one of your actions submits the task
+itself.
+
+Every sub-agent call runs through
+[durable execution]({{< ref "docs/development/create-agent/actions#durable-execution" >}}).
+There is no durability toggle and no sub-agent timeout option.
+
+## Declare a sub-agent
+
+A sub-agent enters the parent's plan as a resource registered under
+`ResourceType.AGENT`. The name you register it under is the name that invocations and
+metrics use. What you register depends on the form. An internal sub-agent registers
+a child `Agent`. An external sub-agent registers a descriptor that names your setup
+class, and a YAML file can declare it.
+
+### Internal: compile a child agent

Review Comment:
   `yaml.md` on main links to `subagents#declare-an-internal-sub-agent` (line 512) and `#declare-an-external-sub-agent` (line 537). Both IDs go away in this rewrite, here and at line 454, so neither link reaches its section. Preserving these IDs or updating the incoming links would fit into the same rebase as the YAML change @wenjin272 asked for.



##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -24,30 +24,170 @@ specific language governing permissions and limitations
 under the License.
 -->
 
-A sub-agent handles a task delegated by a parent agent. An internal sub-agent runs
-an agent workflow in the same job. An external sub-agent calls an agent service
-through a `SubagentSetup` implementation.
+## Overview
 
-## Declare an internal sub-agent
+A sub-agent is an agent that another agent delegates a task to. The parent hands it
+input, the sub-agent does the work, and the parent receives the outcome as a
+`SubagentResult`. Use one when the delegated work is multi-step or long-running and
+should run isolated from the parent.
 
-Use `InternalSubagentResourceDescriptorBuilder` to attach a description and input
-schema to a child agent. The builder returns a normal resource descriptor for `addResource`
-or `add_resource`.
+The work runs either inside the job or outside it. An *internal* sub-agent is a
+child `Agent`, including a `ReActAgent`, compiled into the parent's plan. The
+child's own actions and events run in the same job. An *external* sub-agent has no
+plan of its own. The work happens in your own code or in a remote agent service,
+outside the parent's event loop. It takes one of two forms, *deferred* or *async
+(pub/sub)*.
 
+Either way, the call is initiated by the model or by you. In *model-driven
+delegation* you list the sub-agent on a chat model setup and the model chooses when
+to delegate. In *caller-driven delegation* one of your actions submits the task
+itself.
+
+Every sub-agent call runs through
+[durable execution]({{< ref "docs/development/create-agent/actions#durable-execution" >}}).
+There is no durability toggle and no sub-agent timeout option.
+
+## Declare a sub-agent
+
+A sub-agent enters the parent's plan as a resource registered under
+`ResourceType.AGENT`. The name you register it under is the name that invocations and
+metrics use. What you register depends on the form. An internal sub-agent registers
+a child `Agent`. An external sub-agent registers a descriptor that names your setup
+class, and a YAML file can declare it.
+
+### Internal: compile a child agent
+
+Nothing in a child's own code marks it as a sub-agent. The child is an ordinary
+agent with its own resources, actions, and event loop, and it runs standalone
+exactly as written. A parent registers it as an `AGENT` resource, and that
+registration is the whole of the difference. It gives the child a name in the
+parent's plan. It can also give the child an input schema, so a chat model can call
+it. And it supplies the caller that submits input and awaits the child's outputs.
+
+Any `Agent` can serve as the child. The two common kinds are a workflow agent and a
+`ReActAgent`. Either way, the child's plan is compiled into the parent's when the
+parent's plan is built.
+
+#### A workflow agent as the child
+
+The example child owns a chat model and answers each review request with its
+verdict. Written this way it is an ordinary agent. The registration that follows is
+what makes it a sub-agent. Its action receives the submitted input as an
+`InputEvent` and sends its answer as `OutputEvent`s.
+
+{{< tabs "Write a Child Agent" >}}
+
+{{< tab "Python" >}}
+```python
+class ReviewAgent(Agent):
+    @chat_model_connection
+    @staticmethod
+    def review_connection() -> ResourceDescriptor:
+        return ResourceDescriptor(
+            clazz=ResourceName.ChatModel.OLLAMA_CONNECTION,
+            base_url="http://localhost:11434",
+        )
+
+    @chat_model_setup
+    @staticmethod
+    def review_model() -> ResourceDescriptor:
+        return ResourceDescriptor(
+            clazz=ResourceName.ChatModel.OLLAMA_SETUP,
+            connection="review_connection",
+            model="qwen3:8b",
+        )
+
+    @action(EventType.InputEvent)
+    @staticmethod
+    def ask(event: Event, ctx: RunnerContext) -> None:
+        task = InputEvent.from_event(event).input
+        # A model-driven call passes the arguments map; a caller-driven call
+        # passes the submitted object.
+        diff = task["code"] if isinstance(task, dict) else str(task)
+        ctx.send_event(
+            ChatRequestEvent(
+                model="review_model",
+                messages=[ChatMessage.user(f"Review this diff:\n{diff}")],
+            )
+        )
+
+    @action(EventType.ChatResponseEvent)
+    @staticmethod
+    def answer(event: Event, ctx: RunnerContext) -> None:
+        response = ChatResponseEvent.from_event(event).response
+        ctx.send_event(OutputEvent(output=response.text))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
 ```java
-import org.apache.flink.agents.api.resource.ResourceType;
-import org.apache.flink.agents.api.subagent.InternalSubagentResourceDescriptorBuilder;
+public class ReviewAgent extends Agent {
 
-parent.addResource(
-    "reviewer",
-    ResourceType.AGENT,
-    new InternalSubagentResourceDescriptorBuilder()
-        .agent(new ReviewAgent())
-        .description("Review code for correctness and potential defects")
-        .inputType(ReviewRequest.class)
-        .build());
+    @ChatModelConnection
+    public static ResourceDescriptor reviewConnection() {
+        return ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_CONNECTION)
+                .addInitialArgument("endpoint", "http://localhost:11434")
+                .build();
+    }
+
+    @ChatModelSetup
+    public static ResourceDescriptor reviewModel() {
+        return ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_SETUP)
+                .addInitialArgument("connection", "reviewConnection")
+                .addInitialArgument("model", "qwen3:8b")
+                .build();
+    }
+
+    @Action(EventType.InputEvent)
+    public static void ask(Event event, RunnerContext ctx) throws Exception {
+        Object task = InputEvent.fromEvent(event).getInput();
+        // A model-driven call passes the arguments map; a caller-driven call
+        // passes the submitted object.
+        String diff =
+                task instanceof Map
+                        ? String.valueOf(((Map<?, ?>) task).get("code"))
+                        : String.valueOf(task);
+        ctx.sendEvent(
+                new ChatRequestEvent(
+                        "reviewModel",
+                        List.of(new ChatMessage(MessageRole.USER, "Review this diff:\n" + diff))));
+    }
+
+    @Action(EventType.ChatResponseEvent)
+    public static void answer(Event event, RunnerContext ctx) throws Exception {
+        ctx.sendEvent(new OutputEvent(ChatResponseEvent.fromEvent(event).getResponse().getText()));
+    }
+}
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+While a call runs, the child behaves like a scoped agent of its own:
+
+- Its `OutputEvent`s are accumulated, in order, into the call's result instead of
+  landing on the parent's context. See
+  [the result](#the-result) for the shape.
+- It resolves resources against its own plan. Register the resources a child uses,
+  such as its chat model connection, on the child itself. In Python a resource
+  registered solely on the parent is not visible to the child.
+- It runs against memory isolated from the caller's, so it never reads what the
+  caller persisted. The child's own actions share that memory within one call.
+  Isolation is per call. Continuing a session does not carry memory across calls.

Review Comment:
   Only sensory and short-term memory are per call (`MemoryContext.createChildContext()`, `RunnerContextImpl.java:142-145`). Long-term memory is the operator's one instance in both languages (`RunnerContextImpl.java:776-778`, `flink_runner_context.py:958`), so it's outside this per-call isolation.
   ```suggestion
   - Its sensory and short-term memory start empty for each call, including calls
     continuing a session, and are shared by the child's actions within that call.
     Long-term memory, when configured, is shared with the caller.
   ```



##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -307,30 +443,854 @@ parent.addResource(
                                 + "\"required\":[\"diff\"]}")
                 .build());
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The framework does not check the schema against the prompt. A field the prompt does
+not use is ignored, and a placeholder no field fills is left in the prompt as literal
+text. So keep the two in step when you change either.
+
+### External: write a setup class
+
+An external sub-agent has no plan of its own, and it is not compiled into the
+parent's. The work happens in your own code or in a remote agent service. The parent
+only submits to it and waits for the outcome. The framework cannot perform that work
+for you, because it does not know your protocol. So you write the part that performs
+one invocation, and the framework supplies everything around it. It runs your code
+under durable execution, and after a failover it replays the recorded result instead
+of calling your code again.
+
+What you write is a setup class. Its only job is to carry out one invocation, so it
+declares no actions and no events of its own. It extends a framework base class and
+implements the methods that class declares.
+
+Registration names the setup class in a descriptor, and both languages construct the
+setup from the descriptor at runtime. Registering a setup instance instead is
+rejected.
+
+The invocation itself is carried out in one of two modes, and which one you write
+follows from your remote interface. When one request returns the result, write a
+*deferred* setup. You own the whole interaction, and the framework only holds it back
+until the caller asks for the result. When the remote instead hands back a run that
+you later query for status and result, write an *async (pub/sub)* setup. The base
+class owns that composition of publishing, polling, and fetching, and you supply the
+transport calls it is built from.
+
+#### Deferred execution
+
+A deferred setup sends nothing when the caller submits. It only registers the
+invocation. The request goes out when the caller first awaits the result. So a call
+that is cancelled instead never issues its request.
+
+What you write is the interaction itself, in the `prepare` method of your setup class.
+`prepare` runs once per invocation and returns the durable call that carries it. The
+example is one blocking request, and whatever else your call does is yours to decide,
+because the framework does not know your protocol.
 
+The framework assigns `session_id` and `call_id` before it calls `prepare`, and they
+are the only values that a replay reproduces. The durable id you build from them is
+the key that recovery matches a recorded result against. When the key matches, your
+call is not run again. So build the key from those two values alone. Anything else in
+it, a timestamp for instance, makes a replay look up a different key and run the work
+a second time. Passing the same values to your own remote lets it recognize a
+re-issued request as the same one.
+
+The reconciler is optional. Omitting it leaves one crash window uncovered, and
+[Durability and failover](#durability-and-failover) describes that window. Supply one
+when the work cannot be repeated.
+
+{{< tabs "Supply a Reconciler" >}}
+
+{{< tab "Python" >}}
 ```python
-reviewer = ReActAgent(
-    chat_model=chat_model_descriptor,
-    prompt=Prompt.from_text(
-        "Review the following code diff and report any problems:\n\n{diff}"
+def prepare(
+    self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+) -> PreparedTriple:
+    key = f"{session_id}#{call_id}"
+
+    def call() -> SubagentResult:
+        return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+
+    # Consulted only during recovery, when this call is revisited and no result was
+    # recorded for it. Return the recovered outcome, or raise the recovered failure.
+    # It takes no arguments.
+    def reconcile() -> SubagentResult:
+        # fetch_review_by_key is your own lookup; not part of Flink Agents.
+        found = fetch_review_by_key(self.endpoint, key)
+        if found is None:
+            raise RuntimeError("the review service has no record of this call")

Review Comment:
   A failure raised here isn't folded into a result: `DeferredSubagentFuture.execute` lets it propagate, so for a call made from an action, recovery fails the action instead of handing the caller `success == false`. If "no record" is meant as a caller-visible failure, returning an error matches How failures surface; the same applies to the Java tab at line 541 and the comments at lines 516 and 534. If failing the action is the intent, a sentence saying so would help.
   ```suggestion
           if found is None:
               return SubagentResult.error("the review service has no record of this call")
   ```



##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -307,30 +443,854 @@ parent.addResource(
                                 + "\"required\":[\"diff\"]}")
                 .build());
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The framework does not check the schema against the prompt. A field the prompt does
+not use is ignored, and a placeholder no field fills is left in the prompt as literal
+text. So keep the two in step when you change either.
+
+### External: write a setup class
+
+An external sub-agent has no plan of its own, and it is not compiled into the
+parent's. The work happens in your own code or in a remote agent service. The parent
+only submits to it and waits for the outcome. The framework cannot perform that work
+for you, because it does not know your protocol. So you write the part that performs
+one invocation, and the framework supplies everything around it. It runs your code
+under durable execution, and after a failover it replays the recorded result instead
+of calling your code again.
+
+What you write is a setup class. Its only job is to carry out one invocation, so it
+declares no actions and no events of its own. It extends a framework base class and
+implements the methods that class declares.
+
+Registration names the setup class in a descriptor, and both languages construct the
+setup from the descriptor at runtime. Registering a setup instance instead is
+rejected.
+
+The invocation itself is carried out in one of two modes, and which one you write
+follows from your remote interface. When one request returns the result, write a
+*deferred* setup. You own the whole interaction, and the framework only holds it back
+until the caller asks for the result. When the remote instead hands back a run that
+you later query for status and result, write an *async (pub/sub)* setup. The base
+class owns that composition of publishing, polling, and fetching, and you supply the
+transport calls it is built from.
+
+#### Deferred execution
+
+A deferred setup sends nothing when the caller submits. It only registers the
+invocation. The request goes out when the caller first awaits the result. So a call
+that is cancelled instead never issues its request.
+
+What you write is the interaction itself, in the `prepare` method of your setup class.
+`prepare` runs once per invocation and returns the durable call that carries it. The
+example is one blocking request, and whatever else your call does is yours to decide,
+because the framework does not know your protocol.
 
+The framework assigns `session_id` and `call_id` before it calls `prepare`, and they
+are the only values that a replay reproduces. The durable id you build from them is
+the key that recovery matches a recorded result against. When the key matches, your
+call is not run again. So build the key from those two values alone. Anything else in
+it, a timestamp for instance, makes a replay look up a different key and run the work
+a second time. Passing the same values to your own remote lets it recognize a
+re-issued request as the same one.
+
+The reconciler is optional. Omitting it leaves one crash window uncovered, and
+[Durability and failover](#durability-and-failover) describes that window. Supply one
+when the work cannot be repeated.
+
+{{< tabs "Supply a Reconciler" >}}
+
+{{< tab "Python" >}}
 ```python
-reviewer = ReActAgent(
-    chat_model=chat_model_descriptor,
-    prompt=Prompt.from_text(
-        "Review the following code diff and report any problems:\n\n{diff}"
+def prepare(
+    self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+) -> PreparedTriple:
+    key = f"{session_id}#{call_id}"
+
+    def call() -> SubagentResult:
+        return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+
+    # Consulted only during recovery, when this call is revisited and no result was
+    # recorded for it. Return the recovered outcome, or raise the recovered failure.
+    # It takes no arguments.
+    def reconcile() -> SubagentResult:
+        # fetch_review_by_key is your own lookup; not part of Flink Agents.
+        found = fetch_review_by_key(self.endpoint, key)
+        if found is None:
+            raise RuntimeError("the review service has no record of this call")
+        return SubagentResult.ok(found)
+
+    return (key, call, reconcile)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+// Inside the DurableCallable that prepare returns, alongside getId() and call().
+
+// Consulted only during recovery, when this call is revisited and no result was
+// recorded for it. Return the recovered outcome, or throw the recovered failure.
+@Override
+public Callable<SubagentResult> reconciler() {
+    return () -> {
+        // fetchReviewByKey is your own lookup; not part of Flink Agents.
+        String found = fetchReviewByKey(endpoint, key);
+        if (found == null) {
+            throw new IllegalStateException("the review service has no record of this call");
+        }
+        return SubagentResult.ok(found);
+    };
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+{{< tabs "Deferred Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.runner_context import RunnerContext
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.deferred_subagent import (
+    DeferredSubagentSetup,
+    PreparedTriple,
+)
+
+
+def post_review(endpoint: str, prompt: Any, key: str) -> str:
+    """Your own call to the review service; not part of Flink Agents.
+
+    Keying the work lets the service treat a re-issued request as the same one.
+    """
+    ...
+
+
+class RemoteReviewSetup(DeferredSubagentSetup):
+    endpoint: str = ""
+
+    def prepare(
+        self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+    ) -> PreparedTriple:
+        key = f"{session_id}#{call_id}"
+
+        def call() -> SubagentResult:
+            try:
+                return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+            except Exception as e:
+                # An expected failure is a value; only a system fault should escape.
+                return SubagentResult.error(e)
+
+        # The first value is the key recovery matches a recorded result against, so
+        # it is built from the two ids alone. The third is the reconciler.
+        return (key, call, None)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.context.DurableCallable;
+import org.apache.flink.agents.api.context.RunnerContext;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseDeferredSubagentSetup;
+
+/**
+ * Your own call to the review service; not part of Flink Agents. Keying the work lets
+ * the service treat a re-issued request as the same one.
+ */
+static String postReview(String endpoint, Object prompt, String key) {
+    ...
+}
+
+public class RemoteReviewSetup extends BaseDeferredSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteReviewSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected DurableCallable<SubagentResult> prepare(
+            RunnerContext ctx, Object prompt, String sessionId, String callId) {
+        // Built from the two ids alone, so a replay reproduces it.
+        String key = sessionId + "#" + callId;
+        return new DurableCallable<SubagentResult>() {
+            @Override
+            public String getId() {
+                // The key recovery matches a recorded result against.
+                return key;
+            }
+
+            @Override
+            public Class<SubagentResult> getResultClass() {
+                return SubagentResult.class;
+            }
+
+            @Override
+            public SubagentResult call() {
+                try {
+                    return SubagentResult.ok(postReview(endpoint, prompt, key));
+                } catch (Exception e) {
+                    // An expected failure is a value; only a system fault should escape.
+                    return SubagentResult.error(e);
+                }
+            }
+        };
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup under the name invocations will use. Every descriptor argument
+beyond the built-in ones is configuration for the setup to read back. In Python it
+reads them as model fields, and in Java through the public
+`(ResourceDescriptor, ResourceContext)` constructor. The `endpoint` above is such
+configuration, not a built-in transport.
+
+{{< tabs "Register an External Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
+parent.add_resource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteReviewSetup",
+        endpoint="https://example.com/review",
+        description="Review code using the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
     ),
 )
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteReviewSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/review")
+        .addInitialArgument("description", "Review code using the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_reviewer
+        clazz: my_app.agents.RemoteReviewSetup
+        endpoint: https://example.com/review
+        description: Review code using the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The YAML form of that registration is the `subagents` list of an agent entry, as in
+the YAML tab above. That list can also sit at the shared document level, outside
+`agents:`, so that several agents in one file reuse it. A chat model setup declared in
+the same file also takes a `subagents` argument, but it is a list of registered names
+rather than a list of entries. See
+[delegate from a chat model](#delegate-from-a-chat-model).
+
+Two constraints apply to the entry:
+
+- No class alias is defined for `AGENT` resources, so `clazz` is always a
+  fully-qualified class path.
+- The entry's language must match the loader. A Python YAML file names a Python
+  class and may not set `type: java`. A Java YAML file must set `type: java`.
+
+#### Async (pub/sub) execution
+
+An async sub-agent talks to a service that runs the task for you. The service accepts
+the work and answers at once, and you query it later for the run's status and
+result.
+
+An async setup publishes the run when the caller submits, through one durable POST.
+Waiting for the result then polls the run's status until it reaches a terminal state,
+and fetches it.
+
+That is the difference from the deferred mode, which sends nothing when the caller
+submits. The mode's name is the framework's own. The POST is the publish, and the
+polling is the subscribe.
+
+The submit-poll-fetch composition is the same for every such remote, so the base
+class owns it, along with the durable execution and the crash-window recovery. You
+supply the three transport calls it is built from:
+
+- `callSubmitRequest` / `call_submit_request` starts the run.
+- `callQueryStatus` / `call_query_status` returns a `RunStatus`, one of
+  `notStarted()`, `running()`, `completed()`, or `failed(error)`. It must be a
+  read-only probe with no side effects. Report an expected failure as a `FAILED`
+  status.
+- `callFetchResult` / `call_fetch_result` returns the `SubagentResult`. It must be
+  an idempotent read, because a replay may call it again. The status snapshot never
+  carries the payload, and a failed run is reported without fetching at all.
+
+{{< tabs "Async Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.async_subagent import BaseAsyncSubagentSetup, RunStatus
+
+# client(...) and task_id stand for your own transport client and run-id
+# scheme; neither is part of Flink Agents.
+
+
+class RemoteRunSetup(BaseAsyncSubagentSetup):
+    endpoint: str = ""
+
+    def call_submit_request(self, session_id: str, call_id: str, prompt: Any) -> None:
+        client(self.endpoint).submit(prompt, task_id=f"{session_id}#{call_id}")
+
+    def call_query_status(self, session_id: str, call_id: str) -> RunStatus:
+        probe = client(self.endpoint).status(task_id=f"{session_id}#{call_id}")
+        if probe is None:
+            return RunStatus.not_started()
+        if probe.succeeded:
+            return RunStatus.completed()
+        if probe.failed:
+            return RunStatus.failed(probe.error)
+        return RunStatus.running()
+
+    def call_fetch_result(self, session_id: str, call_id: str) -> SubagentResult:
+        return SubagentResult.ok(
+            client(self.endpoint).fetch(task_id=f"{session_id}#{call_id}")
+        )
+
+    # Optional, and a no-op by default, so a cancelled call leaves the remote run
+    # going. A replay may propagate the same cancellation again, so your remote has
+    # to accept it twice.
+    def call_cancel_request(self, session_id: str, call_id: str) -> None:
+        client(self.endpoint).cancel(task_id=f"{session_id}#{call_id}")
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseAsyncSubagentSetup;
+
+// client() and taskId(...) stand for your own transport client and run-id
+// scheme; neither is part of Flink Agents.
+public class RemoteRunSetup extends BaseAsyncSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteRunSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
 
+    @Override
+    protected void callSubmitRequest(String sessionId, String callId, Object prompt)
+            throws Exception {
+        client().submit(String.valueOf(prompt), taskId(sessionId, callId));
+    }
+
+    @Override
+    protected RunStatus callQueryStatus(String sessionId, String callId) {
+        // TaskStatus is your own transport type, not a framework type.
+        TaskStatus probe = client().status(taskId(sessionId, callId));
+        if (probe == null) {
+            return RunStatus.notStarted();
+        }
+        if (probe.succeeded()) {
+            return RunStatus.completed();
+        }
+        if (probe.failed()) {
+            return RunStatus.failed(probe.error());
+        }
+        return RunStatus.running();
+    }
+
+    @Override
+    protected SubagentResult callFetchResult(String sessionId, String callId) throws Exception {
+        return client().fetchResult(taskId(sessionId, callId));
+    }
+
+    // Optional, and a no-op by default, so a cancelled call leaves the remote run
+    // going. A replay may propagate the same cancellation again, so your remote has
+    // to accept it twice.
+    @Override
+    protected void callCancelRequest(String sessionId, String callId) {
+        client().cancel(taskId(sessionId, callId));
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup like any external sub-agent. The async form adds one descriptor
+argument, `status_poll_interval_millis`, and it defaults to 500.
+
+{{< tabs "Register an Async Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
 parent.add_resource(
-    "reviewer",
+    "remote_runner",
     ResourceType.AGENT,
-    InternalSubagentResourceDescriptorBuilder()
-    .agent(reviewer)
-    .description("Reviews a code diff and reports problems.")
-    # "diff" matches the {diff} placeholder of the child's prompt.
-    .input_schema(
-        '{"type":"object","properties":{"diff":{"type":"string"}},'
-        '"required":["diff"]}'
-    )
-    .build(),
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteRunSetup",
+        endpoint="https://example.com/runs",
+        description="Run a review task on the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
+        status_poll_interval_millis=500,
+    ),
 )
 ```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_runner",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteRunSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/runs")
+        .addInitialArgument("description", "Run a review task on the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .addInitialArgument("status_poll_interval_millis", 500L)
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_runner
+        clazz: my_app.agents.RemoteRunSetup
+        endpoint: https://example.com/runs
+        description: Run a review task on the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+        status_poll_interval_millis: 500
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The base class also recovers the crash window between "POST sent" and "POST durably
+recorded". On recovery it probes the run's status first, and it re-posts only when the
+probe reports that the run never started. Override `reconcileSubmitRequest` /
+`reconcile_submit_request` only if your service needs a different repair.
+
+## Invoke a sub-agent
+
+A sub-agent is invoked in one of two ways, model-driven or caller-driven, and the two
+reuse different existing abstractions.
+
+Model-driven delegation rides the tool abstraction. The sub-agent is offered to the
+model as a tool, so your action sends a `ChatRequestEvent` and the model answers with
+a call to the sub-agent. The result then returns to the model as the tool
+observation, until a final `ChatResponseEvent` reaches your action. This is the same
+cycle any tool call makes.
+
+Caller-driven delegation cannot ride that cycle, because a tool's result goes back
+into the chat loop rather than to the action that asked for it. An action that needs
+the delegated outcome therefore calls the sub-agent itself, with no model in the loop.
+
+Both ways end in the same call. Your action, or the framework on the model's behalf,
+runs `submit(ctx, prompt)` on the registered name. That call returns a
+`SubagentFuture`, which is the handle for that one call. Awaiting the handle yields
+the call's `SubagentResult`.
+
+### Delegate from a chat model
+
+List the registered name under `subagents` of the parent's chat model:
+
+{{< tabs "Declare Sub-agents on a Chat Model" >}}
+
+{{< tab "Python" >}}
+```python
+ResourceDescriptor(
+    clazz=ResourceName.ChatModel.OLLAMA_SETUP,
+    connection="ollama_connection",
+    model="qwen3:8b",
+    subagents=["reviewer"],
+)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_SETUP)
+    .addInitialArgument("connection", "ollamaConnection")
+    .addInitialArgument("model", "qwen3:8b")
+    .addInitialArgument("subagents", List.of("reviewer"))
+    .build();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The model sees `reviewer` as a tool, described by the registration's description and
+input schema. A schema is required for model-selected calls. A missing description
+falls back to a generic delegation description.
+
+The arguments the model sends become the sub-agent's input, and its outputs return to
+the model as the tool observation. The model can then keep reasoning over them. For
+an internal sub-agent, the observation is the JSON encoding of the full output list
+described in [the result](#the-result). That holds even when the child emits a single
+output, so the shape does not depend on how many outputs the child emits.
+
+An external setup class can state the shape of that observation. It declares
+`result_type()` in Python or `getResultType()` in Java, and the default reports the
+result as it arrived. Declare one to narrow a wider payload, or to convert a payload
+that is not already JSON-compatible. An internal sub-agent has no such declaration, so
+its observation is always the JSON encoding of its output list. This shapes the
+observation only. A caller that awaits the handle gets the result the sub-agent
+produced, as [the result](#the-result) describes.
+
+The external sub-agents `remote_reviewer` and `remote_runner` are offered the same
+way. Only their registration differs.
+
+### Call from an action
+
+An action can submit input to a registered sub-agent directly and await its
+outcome. This path needs no description and no input declaration, so it also works
+for a child registered as-is.
+
+{{< tabs "Call a Sub-agent From an Action" >}}
+
+{{< tab "Python" >}}
+```python
+class DelegatingAgent(Agent):
+    @action(EventType.InputEvent)
+    @staticmethod
+    async def delegate(event: Event, ctx: RunnerContext) -> None:
+        reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+        future = await reviewer.submit(ctx, "review the diff")
+        result = await future
+        if result.success:
+            ctx.send_event(OutputEvent(output=result.result[0]))
+        else:
+            ctx.send_event(OutputEvent(output=f"failed: {result.error_message}"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+public class DelegatingAgent extends Agent {
+
+    @Action(EventType.InputEvent)
+    public static void delegate(Event event, RunnerContext ctx) throws Exception {
+        SubagentSetup reviewer =
+                (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+        SubagentResult result = reviewer.submit(ctx, "review the diff").await();
+        if (result.isSuccess()) {
+            String answer = String.valueOf(((List<?>) result.getResult()).get(0));
+            ctx.sendEvent(new OutputEvent(answer));
+        } else {
+            ctx.sendEvent(new OutputEvent("failed: " + result.getErrorMessage()));
+        }
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+What you pass to `submit` is what the sub-agent receives. An internal child gets it
+as its `InputEvent` payload, and a model-driven call passes the arguments map
+instead. What comes back is a `SubagentResult`. For the internal sub-agent used here,
+its `result` is the list of `OutputEvent` payloads the child emitted. That is why the
+example reads `result.result[0]`. An external sub-agent returns whatever its setup
+produces.
+
+In Python `submit` is a coroutine, so the call is awaited in two phases and the
+action must be `async`. In Java `submit` returns the handle directly and `await()`
+yields the result. The same two calls drive `remote_reviewer` and `remote_runner`
+unchanged. Only the registration differs.
+
+## Work with open handles
+
+A `SubagentFuture` is live from `submit` until it is awaited or cancelled.
+
+It works like the `AsyncFuture` that
+[async execution]({{< ref "docs/development/create-agent/actions#async-execution" >}})
+returns. You get it from one call, and you await it to get the outcome. It is its own
+type rather than an `AsyncFuture`, because it always resolves to a `SubagentResult`,
+it carries the call's `(session_id, call_id)`, and it owns its durable execution
+inside the setup.
+
+### Fan out to several sub-agents
+
+`combine` batches handles into a `SubagentFutures`, and it works the way `gather`
+works for ordinary async futures. `gather` does not accept a `SubagentFuture`, so
+handles batch this way instead. Every handle in the batch is prepared before any is
+executed, so deferred requests all go out up front and remote runs overlap. Results
+come back in the order the handles were added.
+
+{{< tabs "Fan Out to Several Sub-agents" >}}
+
+{{< tab "Python" >}}
+```python
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+first = await reviewer.submit(ctx, "review the diff")
+second = await reviewer.submit(ctx, "review the tests")
+
+outcomes = await first.combine(second)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture first = reviewer.submit(ctx, "review the diff");
+SubagentFuture second = reviewer.submit(ctx, "review the tests");
+
+List<SubagentResult> outcomes = first.combine(second).awaitAll();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+### Continue a session
+
+Pass the `session_id` of an earlier handle to continue that conversation. The new
+call gets a fresh `call_id` in the same session. Whether a conversation can be
+continued across actions is up to the implementation.
+
+{{< tabs "Continue a Session" >}}
+
+{{< tab "Python" >}}
+```python
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+first = await reviewer.submit(ctx, "review the diff")
+first_result = await first
+
+second = await reviewer.submit(ctx, "now the tests", first.session_id)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture first = reviewer.submit(ctx, "review the diff");
+SubagentResult firstResult = first.await();
+
+SubagentFuture second = reviewer.submit(ctx, "now the tests", first.getSessionId());

Review Comment:
   `second` is never resolved, so by Cancel and resolve this action fails with "finished without resolving". Adding `SubagentResult secondResult = second.await();` here and `second_result = await second` in the Python tab resolves both calls before the action returns.



##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -307,30 +443,854 @@ parent.addResource(
                                 + "\"required\":[\"diff\"]}")
                 .build());
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The framework does not check the schema against the prompt. A field the prompt does
+not use is ignored, and a placeholder no field fills is left in the prompt as literal
+text. So keep the two in step when you change either.
+
+### External: write a setup class
+
+An external sub-agent has no plan of its own, and it is not compiled into the
+parent's. The work happens in your own code or in a remote agent service. The parent
+only submits to it and waits for the outcome. The framework cannot perform that work
+for you, because it does not know your protocol. So you write the part that performs
+one invocation, and the framework supplies everything around it. It runs your code
+under durable execution, and after a failover it replays the recorded result instead
+of calling your code again.
+
+What you write is a setup class. Its only job is to carry out one invocation, so it
+declares no actions and no events of its own. It extends a framework base class and
+implements the methods that class declares.
+
+Registration names the setup class in a descriptor, and both languages construct the
+setup from the descriptor at runtime. Registering a setup instance instead is
+rejected.
+
+The invocation itself is carried out in one of two modes, and which one you write
+follows from your remote interface. When one request returns the result, write a
+*deferred* setup. You own the whole interaction, and the framework only holds it back
+until the caller asks for the result. When the remote instead hands back a run that
+you later query for status and result, write an *async (pub/sub)* setup. The base
+class owns that composition of publishing, polling, and fetching, and you supply the
+transport calls it is built from.
+
+#### Deferred execution
+
+A deferred setup sends nothing when the caller submits. It only registers the
+invocation. The request goes out when the caller first awaits the result. So a call
+that is cancelled instead never issues its request.
+
+What you write is the interaction itself, in the `prepare` method of your setup class.
+`prepare` runs once per invocation and returns the durable call that carries it. The
+example is one blocking request, and whatever else your call does is yours to decide,
+because the framework does not know your protocol.
 
+The framework assigns `session_id` and `call_id` before it calls `prepare`, and they
+are the only values that a replay reproduces. The durable id you build from them is
+the key that recovery matches a recorded result against. When the key matches, your
+call is not run again. So build the key from those two values alone. Anything else in
+it, a timestamp for instance, makes a replay look up a different key and run the work
+a second time. Passing the same values to your own remote lets it recognize a
+re-issued request as the same one.
+
+The reconciler is optional. Omitting it leaves one crash window uncovered, and
+[Durability and failover](#durability-and-failover) describes that window. Supply one
+when the work cannot be repeated.
+
+{{< tabs "Supply a Reconciler" >}}
+
+{{< tab "Python" >}}
 ```python
-reviewer = ReActAgent(
-    chat_model=chat_model_descriptor,
-    prompt=Prompt.from_text(
-        "Review the following code diff and report any problems:\n\n{diff}"
+def prepare(
+    self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+) -> PreparedTriple:
+    key = f"{session_id}#{call_id}"
+
+    def call() -> SubagentResult:
+        return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+
+    # Consulted only during recovery, when this call is revisited and no result was
+    # recorded for it. Return the recovered outcome, or raise the recovered failure.
+    # It takes no arguments.
+    def reconcile() -> SubagentResult:
+        # fetch_review_by_key is your own lookup; not part of Flink Agents.
+        found = fetch_review_by_key(self.endpoint, key)
+        if found is None:
+            raise RuntimeError("the review service has no record of this call")
+        return SubagentResult.ok(found)
+
+    return (key, call, reconcile)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+// Inside the DurableCallable that prepare returns, alongside getId() and call().
+
+// Consulted only during recovery, when this call is revisited and no result was
+// recorded for it. Return the recovered outcome, or throw the recovered failure.
+@Override
+public Callable<SubagentResult> reconciler() {
+    return () -> {
+        // fetchReviewByKey is your own lookup; not part of Flink Agents.
+        String found = fetchReviewByKey(endpoint, key);
+        if (found == null) {
+            throw new IllegalStateException("the review service has no record of this call");
+        }
+        return SubagentResult.ok(found);
+    };
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+{{< tabs "Deferred Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.runner_context import RunnerContext
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.deferred_subagent import (
+    DeferredSubagentSetup,
+    PreparedTriple,
+)
+
+
+def post_review(endpoint: str, prompt: Any, key: str) -> str:
+    """Your own call to the review service; not part of Flink Agents.
+
+    Keying the work lets the service treat a re-issued request as the same one.
+    """
+    ...
+
+
+class RemoteReviewSetup(DeferredSubagentSetup):
+    endpoint: str = ""
+
+    def prepare(
+        self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+    ) -> PreparedTriple:
+        key = f"{session_id}#{call_id}"
+
+        def call() -> SubagentResult:
+            try:
+                return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+            except Exception as e:
+                # An expected failure is a value; only a system fault should escape.
+                return SubagentResult.error(e)
+
+        # The first value is the key recovery matches a recorded result against, so
+        # it is built from the two ids alone. The third is the reconciler.
+        return (key, call, None)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.context.DurableCallable;
+import org.apache.flink.agents.api.context.RunnerContext;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseDeferredSubagentSetup;
+
+/**
+ * Your own call to the review service; not part of Flink Agents. Keying the work lets
+ * the service treat a re-issued request as the same one.
+ */
+static String postReview(String endpoint, Object prompt, String key) {
+    ...
+}
+
+public class RemoteReviewSetup extends BaseDeferredSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteReviewSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected DurableCallable<SubagentResult> prepare(
+            RunnerContext ctx, Object prompt, String sessionId, String callId) {
+        // Built from the two ids alone, so a replay reproduces it.
+        String key = sessionId + "#" + callId;
+        return new DurableCallable<SubagentResult>() {
+            @Override
+            public String getId() {
+                // The key recovery matches a recorded result against.
+                return key;
+            }
+
+            @Override
+            public Class<SubagentResult> getResultClass() {
+                return SubagentResult.class;
+            }
+
+            @Override
+            public SubagentResult call() {
+                try {
+                    return SubagentResult.ok(postReview(endpoint, prompt, key));
+                } catch (Exception e) {
+                    // An expected failure is a value; only a system fault should escape.
+                    return SubagentResult.error(e);
+                }
+            }
+        };
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup under the name invocations will use. Every descriptor argument
+beyond the built-in ones is configuration for the setup to read back. In Python it
+reads them as model fields, and in Java through the public
+`(ResourceDescriptor, ResourceContext)` constructor. The `endpoint` above is such
+configuration, not a built-in transport.
+
+{{< tabs "Register an External Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
+parent.add_resource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteReviewSetup",
+        endpoint="https://example.com/review",
+        description="Review code using the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
     ),
 )
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteReviewSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/review")
+        .addInitialArgument("description", "Review code using the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_reviewer
+        clazz: my_app.agents.RemoteReviewSetup
+        endpoint: https://example.com/review
+        description: Review code using the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The YAML form of that registration is the `subagents` list of an agent entry, as in
+the YAML tab above. That list can also sit at the shared document level, outside
+`agents:`, so that several agents in one file reuse it. A chat model setup declared in
+the same file also takes a `subagents` argument, but it is a list of registered names
+rather than a list of entries. See
+[delegate from a chat model](#delegate-from-a-chat-model).
+
+Two constraints apply to the entry:
+
+- No class alias is defined for `AGENT` resources, so `clazz` is always a
+  fully-qualified class path.
+- The entry's language must match the loader. A Python YAML file names a Python
+  class and may not set `type: java`. A Java YAML file must set `type: java`.
+
+#### Async (pub/sub) execution
+
+An async sub-agent talks to a service that runs the task for you. The service accepts
+the work and answers at once, and you query it later for the run's status and
+result.
+
+An async setup publishes the run when the caller submits, through one durable POST.
+Waiting for the result then polls the run's status until it reaches a terminal state,
+and fetches it.
+
+That is the difference from the deferred mode, which sends nothing when the caller
+submits. The mode's name is the framework's own. The POST is the publish, and the
+polling is the subscribe.
+
+The submit-poll-fetch composition is the same for every such remote, so the base
+class owns it, along with the durable execution and the crash-window recovery. You
+supply the three transport calls it is built from:
+
+- `callSubmitRequest` / `call_submit_request` starts the run.
+- `callQueryStatus` / `call_query_status` returns a `RunStatus`, one of
+  `notStarted()`, `running()`, `completed()`, or `failed(error)`. It must be a
+  read-only probe with no side effects. Report an expected failure as a `FAILED`
+  status.
+- `callFetchResult` / `call_fetch_result` returns the `SubagentResult`. It must be
+  an idempotent read, because a replay may call it again. The status snapshot never
+  carries the payload, and a failed run is reported without fetching at all.
+
+{{< tabs "Async Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.async_subagent import BaseAsyncSubagentSetup, RunStatus
+
+# client(...) and task_id stand for your own transport client and run-id
+# scheme; neither is part of Flink Agents.
+
+
+class RemoteRunSetup(BaseAsyncSubagentSetup):
+    endpoint: str = ""
+
+    def call_submit_request(self, session_id: str, call_id: str, prompt: Any) -> None:
+        client(self.endpoint).submit(prompt, task_id=f"{session_id}#{call_id}")
+
+    def call_query_status(self, session_id: str, call_id: str) -> RunStatus:
+        probe = client(self.endpoint).status(task_id=f"{session_id}#{call_id}")
+        if probe is None:
+            return RunStatus.not_started()
+        if probe.succeeded:
+            return RunStatus.completed()
+        if probe.failed:
+            return RunStatus.failed(probe.error)
+        return RunStatus.running()
+
+    def call_fetch_result(self, session_id: str, call_id: str) -> SubagentResult:
+        return SubagentResult.ok(
+            client(self.endpoint).fetch(task_id=f"{session_id}#{call_id}")
+        )
+
+    # Optional, and a no-op by default, so a cancelled call leaves the remote run
+    # going. A replay may propagate the same cancellation again, so your remote has
+    # to accept it twice.
+    def call_cancel_request(self, session_id: str, call_id: str) -> None:
+        client(self.endpoint).cancel(task_id=f"{session_id}#{call_id}")
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseAsyncSubagentSetup;
+
+// client() and taskId(...) stand for your own transport client and run-id
+// scheme; neither is part of Flink Agents.
+public class RemoteRunSetup extends BaseAsyncSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteRunSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
 
+    @Override
+    protected void callSubmitRequest(String sessionId, String callId, Object prompt)
+            throws Exception {
+        client().submit(String.valueOf(prompt), taskId(sessionId, callId));
+    }
+
+    @Override
+    protected RunStatus callQueryStatus(String sessionId, String callId) {
+        // TaskStatus is your own transport type, not a framework type.
+        TaskStatus probe = client().status(taskId(sessionId, callId));
+        if (probe == null) {
+            return RunStatus.notStarted();
+        }
+        if (probe.succeeded()) {
+            return RunStatus.completed();
+        }
+        if (probe.failed()) {
+            return RunStatus.failed(probe.error());
+        }
+        return RunStatus.running();
+    }
+
+    @Override
+    protected SubagentResult callFetchResult(String sessionId, String callId) throws Exception {
+        return client().fetchResult(taskId(sessionId, callId));
+    }
+
+    // Optional, and a no-op by default, so a cancelled call leaves the remote run
+    // going. A replay may propagate the same cancellation again, so your remote has
+    // to accept it twice.
+    @Override
+    protected void callCancelRequest(String sessionId, String callId) {
+        client().cancel(taskId(sessionId, callId));
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup like any external sub-agent. The async form adds one descriptor
+argument, `status_poll_interval_millis`, and it defaults to 500.
+
+{{< tabs "Register an Async Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
 parent.add_resource(
-    "reviewer",
+    "remote_runner",
     ResourceType.AGENT,
-    InternalSubagentResourceDescriptorBuilder()
-    .agent(reviewer)
-    .description("Reviews a code diff and reports problems.")
-    # "diff" matches the {diff} placeholder of the child's prompt.
-    .input_schema(
-        '{"type":"object","properties":{"diff":{"type":"string"}},'
-        '"required":["diff"]}'
-    )
-    .build(),
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteRunSetup",
+        endpoint="https://example.com/runs",
+        description="Run a review task on the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
+        status_poll_interval_millis=500,
+    ),
 )
 ```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_runner",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteRunSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/runs")
+        .addInitialArgument("description", "Run a review task on the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .addInitialArgument("status_poll_interval_millis", 500L)
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_runner
+        clazz: my_app.agents.RemoteRunSetup
+        endpoint: https://example.com/runs
+        description: Run a review task on the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+        status_poll_interval_millis: 500
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The base class also recovers the crash window between "POST sent" and "POST durably
+recorded". On recovery it probes the run's status first, and it re-posts only when the
+probe reports that the run never started. Override `reconcileSubmitRequest` /
+`reconcile_submit_request` only if your service needs a different repair.
+
+## Invoke a sub-agent
+
+A sub-agent is invoked in one of two ways, model-driven or caller-driven, and the two
+reuse different existing abstractions.
+
+Model-driven delegation rides the tool abstraction. The sub-agent is offered to the
+model as a tool, so your action sends a `ChatRequestEvent` and the model answers with
+a call to the sub-agent. The result then returns to the model as the tool
+observation, until a final `ChatResponseEvent` reaches your action. This is the same
+cycle any tool call makes.
+
+Caller-driven delegation cannot ride that cycle, because a tool's result goes back
+into the chat loop rather than to the action that asked for it. An action that needs
+the delegated outcome therefore calls the sub-agent itself, with no model in the loop.
+
+Both ways end in the same call. Your action, or the framework on the model's behalf,
+runs `submit(ctx, prompt)` on the registered name. That call returns a
+`SubagentFuture`, which is the handle for that one call. Awaiting the handle yields
+the call's `SubagentResult`.
+
+### Delegate from a chat model
+
+List the registered name under `subagents` of the parent's chat model:
+
+{{< tabs "Declare Sub-agents on a Chat Model" >}}
+
+{{< tab "Python" >}}
+```python
+ResourceDescriptor(
+    clazz=ResourceName.ChatModel.OLLAMA_SETUP,
+    connection="ollama_connection",
+    model="qwen3:8b",
+    subagents=["reviewer"],
+)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_SETUP)
+    .addInitialArgument("connection", "ollamaConnection")
+    .addInitialArgument("model", "qwen3:8b")
+    .addInitialArgument("subagents", List.of("reviewer"))
+    .build();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The model sees `reviewer` as a tool, described by the registration's description and
+input schema. A schema is required for model-selected calls. A missing description
+falls back to a generic delegation description.
+
+The arguments the model sends become the sub-agent's input, and its outputs return to
+the model as the tool observation. The model can then keep reasoning over them. For
+an internal sub-agent, the observation is the JSON encoding of the full output list
+described in [the result](#the-result). That holds even when the child emits a single
+output, so the shape does not depend on how many outputs the child emits.
+
+An external setup class can state the shape of that observation. It declares
+`result_type()` in Python or `getResultType()` in Java, and the default reports the
+result as it arrived. Declare one to narrow a wider payload, or to convert a payload
+that is not already JSON-compatible. An internal sub-agent has no such declaration, so
+its observation is always the JSON encoding of its output list. This shapes the
+observation only. A caller that awaits the handle gets the result the sub-agent
+produced, as [the result](#the-result) describes.
+
+The external sub-agents `remote_reviewer` and `remote_runner` are offered the same
+way. Only their registration differs.
+
+### Call from an action
+
+An action can submit input to a registered sub-agent directly and await its
+outcome. This path needs no description and no input declaration, so it also works
+for a child registered as-is.
+
+{{< tabs "Call a Sub-agent From an Action" >}}
+
+{{< tab "Python" >}}
+```python
+class DelegatingAgent(Agent):
+    @action(EventType.InputEvent)
+    @staticmethod
+    async def delegate(event: Event, ctx: RunnerContext) -> None:
+        reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+        future = await reviewer.submit(ctx, "review the diff")
+        result = await future
+        if result.success:
+            ctx.send_event(OutputEvent(output=result.result[0]))
+        else:
+            ctx.send_event(OutputEvent(output=f"failed: {result.error_message}"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+public class DelegatingAgent extends Agent {
+
+    @Action(EventType.InputEvent)
+    public static void delegate(Event event, RunnerContext ctx) throws Exception {
+        SubagentSetup reviewer =
+                (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+        SubagentResult result = reviewer.submit(ctx, "review the diff").await();
+        if (result.isSuccess()) {
+            String answer = String.valueOf(((List<?>) result.getResult()).get(0));
+            ctx.sendEvent(new OutputEvent(answer));
+        } else {
+            ctx.sendEvent(new OutputEvent("failed: " + result.getErrorMessage()));
+        }
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+What you pass to `submit` is what the sub-agent receives. An internal child gets it
+as its `InputEvent` payload, and a model-driven call passes the arguments map
+instead. What comes back is a `SubagentResult`. For the internal sub-agent used here,
+its `result` is the list of `OutputEvent` payloads the child emitted. That is why the
+example reads `result.result[0]`. An external sub-agent returns whatever its setup
+produces.
+
+In Python `submit` is a coroutine, so the call is awaited in two phases and the
+action must be `async`. In Java `submit` returns the handle directly and `await()`
+yields the result. The same two calls drive `remote_reviewer` and `remote_runner`
+unchanged. Only the registration differs.
+
+## Work with open handles
+
+A `SubagentFuture` is live from `submit` until it is awaited or cancelled.
+
+It works like the `AsyncFuture` that
+[async execution]({{< ref "docs/development/create-agent/actions#async-execution" >}})
+returns. You get it from one call, and you await it to get the outcome. It is its own
+type rather than an `AsyncFuture`, because it always resolves to a `SubagentResult`,
+it carries the call's `(session_id, call_id)`, and it owns its durable execution
+inside the setup.
+
+### Fan out to several sub-agents
+
+`combine` batches handles into a `SubagentFutures`, and it works the way `gather`
+works for ordinary async futures. `gather` does not accept a `SubagentFuture`, so
+handles batch this way instead. Every handle in the batch is prepared before any is
+executed, so deferred requests all go out up front and remote runs overlap. Results
+come back in the order the handles were added.
+
+{{< tabs "Fan Out to Several Sub-agents" >}}
+
+{{< tab "Python" >}}
+```python
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+first = await reviewer.submit(ctx, "review the diff")
+second = await reviewer.submit(ctx, "review the tests")
+
+outcomes = await first.combine(second)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture first = reviewer.submit(ctx, "review the diff");
+SubagentFuture second = reviewer.submit(ctx, "review the tests");
+
+List<SubagentResult> outcomes = first.combine(second).awaitAll();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+### Continue a session
+
+Pass the `session_id` of an earlier handle to continue that conversation. The new
+call gets a fresh `call_id` in the same session. Whether a conversation can be
+continued across actions is up to the implementation.
+
+{{< tabs "Continue a Session" >}}
+
+{{< tab "Python" >}}
+```python
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+first = await reviewer.submit(ctx, "review the diff")
+first_result = await first
+
+second = await reviewer.submit(ctx, "now the tests", first.session_id)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture first = reviewer.submit(ctx, "review the diff");
+SubagentResult firstResult = first.await();
+
+SubagentFuture second = reviewer.submit(ctx, "now the tests", first.getSessionId());
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+### Cancel and resolve
+
+Resolve every handle you submit. An action that finishes with a handle it neither
+awaited nor cancelled fails with `Action <name> finished without resolving the
+sub-agent calls it submitted: <ids>`. Cancelling counts as resolving, and awaiting a
+cancelled handle throws `CancellationException` in Java and raises `CancelledError` in
+Python.
+
+{{< tabs "Cancel a Sub-agent Call" >}}
+
+{{< tab "Python" >}}
+```python
+from concurrent.futures import CancelledError
+
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+future = await reviewer.submit(ctx, "review the diff")
+future.cancel()
+
+try:
+    await future
+except CancelledError:
+    ctx.send_event(OutputEvent(output="cancelled"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture future = reviewer.submit(ctx, "review the diff");
+future.cancel();
+
+try {
+    future.await();
+} catch (CancellationException e) {
+    ctx.sendEvent(new OutputEvent("cancelled"));
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+`cancel()` on a deferred handle takes effect only before the request is issued.
+
+## The outcome of a call
+
+### The result
+
+The awaited value of every call is a `SubagentResult`, which carries the outcome in
+three fields:
+
+| Field | Java | Carries |
+|---|---|---|
+| `success` | `isSuccess()` | Whether the call succeeded |
+| `result` | `getResult()` | The answer on success. For an internal sub-agent, the list of `OutputEvent` payloads in emission order. For an external one, whatever the setup produces, such as a string or an object |
+| `error_message` | `getErrorMessage()` | The reason on failure |
+
+A result is persisted and re-bound after a failover, so it must be JSON-compatible.

Review Comment:
   Python persists durable results with cloudpickle (`flink_runner_context.py:1209`, `1461`), so a Python replay restores the payload's type rather than turning it into a dict; Java does persist through Jackson. The string-key and finite-number checks run on model-facing results (`normalize_agent_result`, `ToolResultUtils.normalizeAgentResult`), not at persistence. Splitting the Java and Python replay behavior, removing the Python dict comment at line 1190, and scoping the JSON rule to model-facing results would cover it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "yunfengzhou-hub (via GitHub)" <gi...@apache.org>.
yunfengzhou-hub commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4300862261


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -24,30 +24,170 @@ specific language governing permissions and limitations
 under the License.
 -->
 
-A sub-agent handles a task delegated by a parent agent. An internal sub-agent runs
-an agent workflow in the same job. An external sub-agent calls an agent service
-through a `SubagentSetup` implementation.
+## Overview
 
-## Declare an internal sub-agent
+A sub-agent is an agent that another agent delegates a task to. The parent hands it
+input, the sub-agent does the work, and the parent receives the outcome as a
+`SubagentResult`. Use one when the delegated work is multi-step or long-running and
+should run isolated from the parent.
 
-Use `InternalSubagentResourceDescriptorBuilder` to attach a description and input
-schema to a child agent. The builder returns a normal resource descriptor for `addResource`
-or `add_resource`.
+The work runs either inside the job or outside it. An *internal* sub-agent is a
+child `Agent`, including a `ReActAgent`, compiled into the parent's plan. The
+child's own actions and events run in the same job. An *external* sub-agent has no
+plan of its own. The work happens in your own code or in a remote agent service,
+outside the parent's event loop. It takes one of two forms, *deferred* or *async
+(pub/sub)*.
 
+Either way, the call is initiated by the model or by you. In *model-driven
+delegation* you list the sub-agent on a chat model setup and the model chooses when
+to delegate. In *caller-driven delegation* one of your actions submits the task
+itself.
+
+Every sub-agent call runs through
+[durable execution]({{< ref "docs/development/create-agent/actions#durable-execution" >}}).
+There is no durability toggle and no sub-agent timeout option.
+
+## Declare a sub-agent
+
+A sub-agent enters the parent's plan as a resource registered under
+`ResourceType.AGENT`. The name you register it under is the name that invocations and
+metrics use. What you register depends on the form. An internal sub-agent registers
+a child `Agent`. An external sub-agent registers a descriptor that names your setup
+class, and a YAML file can declare it.
+
+### Internal: compile a child agent
+
+Nothing in a child's own code marks it as a sub-agent. The child is an ordinary
+agent with its own resources, actions, and event loop, and it runs standalone
+exactly as written. A parent registers it as an `AGENT` resource, and that
+registration is the whole of the difference. It gives the child a name in the
+parent's plan. It can also give the child an input schema, so a chat model can call
+it. And it supplies the caller that submits input and awaits the child's outputs.
+
+Any `Agent` can serve as the child. The two common kinds are a workflow agent and a
+`ReActAgent`. Either way, the child's plan is compiled into the parent's when the
+parent's plan is built.
+
+#### A workflow agent as the child
+
+The example child owns a chat model and answers each review request with its
+verdict. Written this way it is an ordinary agent. The registration that follows is
+what makes it a sub-agent. Its action receives the submitted input as an
+`InputEvent` and sends its answer as `OutputEvent`s.
+
+{{< tabs "Write a Child Agent" >}}
+
+{{< tab "Python" >}}
+```python
+class ReviewAgent(Agent):
+    @chat_model_connection
+    @staticmethod
+    def review_connection() -> ResourceDescriptor:
+        return ResourceDescriptor(
+            clazz=ResourceName.ChatModel.OLLAMA_CONNECTION,
+            base_url="http://localhost:11434",
+        )
+
+    @chat_model_setup
+    @staticmethod
+    def review_model() -> ResourceDescriptor:
+        return ResourceDescriptor(
+            clazz=ResourceName.ChatModel.OLLAMA_SETUP,
+            connection="review_connection",
+            model="qwen3:8b",
+        )
+
+    @action(EventType.InputEvent)
+    @staticmethod
+    def ask(event: Event, ctx: RunnerContext) -> None:
+        task = InputEvent.from_event(event).input
+        # A model-driven call passes the arguments map; a caller-driven call
+        # passes the submitted object.
+        diff = task["code"] if isinstance(task, dict) else str(task)
+        ctx.send_event(
+            ChatRequestEvent(
+                model="review_model",
+                messages=[ChatMessage.user(f"Review this diff:\n{diff}")],
+            )
+        )
+
+    @action(EventType.ChatResponseEvent)
+    @staticmethod
+    def answer(event: Event, ctx: RunnerContext) -> None:
+        response = ChatResponseEvent.from_event(event).response
+        ctx.send_event(OutputEvent(output=response.text))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
 ```java
-import org.apache.flink.agents.api.resource.ResourceType;
-import org.apache.flink.agents.api.subagent.InternalSubagentResourceDescriptorBuilder;
+public class ReviewAgent extends Agent {
 
-parent.addResource(
-    "reviewer",
-    ResourceType.AGENT,
-    new InternalSubagentResourceDescriptorBuilder()
-        .agent(new ReviewAgent())
-        .description("Review code for correctness and potential defects")
-        .inputType(ReviewRequest.class)
-        .build());
+    @ChatModelConnection
+    public static ResourceDescriptor reviewConnection() {
+        return ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_CONNECTION)
+                .addInitialArgument("endpoint", "http://localhost:11434")
+                .build();
+    }
+
+    @ChatModelSetup
+    public static ResourceDescriptor reviewModel() {
+        return ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_SETUP)
+                .addInitialArgument("connection", "reviewConnection")
+                .addInitialArgument("model", "qwen3:8b")
+                .build();
+    }
+
+    @Action(EventType.InputEvent)
+    public static void ask(Event event, RunnerContext ctx) throws Exception {
+        Object task = InputEvent.fromEvent(event).getInput();
+        // A model-driven call passes the arguments map; a caller-driven call
+        // passes the submitted object.
+        String diff =
+                task instanceof Map
+                        ? String.valueOf(((Map<?, ?>) task).get("code"))
+                        : String.valueOf(task);
+        ctx.sendEvent(
+                new ChatRequestEvent(
+                        "reviewModel",
+                        List.of(new ChatMessage(MessageRole.USER, "Review this diff:\n" + diff))));
+    }
+
+    @Action(EventType.ChatResponseEvent)
+    public static void answer(Event event, RunnerContext ctx) throws Exception {
+        ctx.sendEvent(new OutputEvent(ChatResponseEvent.fromEvent(event).getResponse().getText()));
+    }
+}
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+While a call runs, the child behaves like a scoped agent of its own:
+
+- Its `OutputEvent`s are accumulated, in order, into the call's result instead of
+  landing on the parent's context. See
+  [the result](#the-result) for the shape.
+- It resolves resources against its own plan. Register the resources a child uses,
+  such as its chat model connection, on the child itself. In Python a resource
+  registered solely on the parent is not visible to the child.
+- It runs against memory isolated from the caller's, so it never reads what the
+  caller persisted. The child's own actions share that memory within one call.
+  Isolation is per call. Continuing a session does not carry memory across calls.

Review Comment:
   Fixed with your wording.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "yunfengzhou-hub (via GitHub)" <gi...@apache.org>.
yunfengzhou-hub merged PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "yunfengzhou-hub (via GitHub)" <gi...@apache.org>.
yunfengzhou-hub commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4309927448


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -312,30 +412,854 @@ parent.addResource(
                                 + "\"required\":[\"diff\"]}")
                 .build());
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The framework does not check the schema against the prompt. A field the prompt does
+not use is ignored, and a placeholder no field fills is left in the prompt as literal
+text. So keep the two in step when you change either.
 
+### External: write a setup class {#declare-an-external-sub-agent}
+
+An external sub-agent has no plan of its own, and it is not compiled into the
+parent's. The work happens in your own code or in a remote agent service. The parent
+only submits to it and waits for the outcome. The framework cannot perform that work
+for you, because it does not know your protocol. So you write the part that performs
+one invocation, and the framework supplies everything around it. It runs your code
+under durable execution, and after a failover it replays the recorded result instead
+of calling your code again.
+
+What you write is a setup class. Its only job is to carry out one invocation, so it
+declares no actions and no events of its own. It extends a framework base class and
+implements the methods that class declares.
+
+Registration names the setup class in a descriptor, and both languages construct the
+setup from the descriptor at runtime. Registering a setup instance instead is
+rejected.
+
+The invocation itself is carried out in one of two modes, *deferred* and *async
+(pub/sub)*. Which one you write follows from your remote interface.
+
+| | Deferred | Async (pub/sub) |
+|---|---|---|
+| Work starts | When the caller first awaits the result | When the caller submits |
+| You implement | `prepare` | `call_submit_request`, `call_query_status`, `call_fetch_result` |
+| Choose it when | One request returns the result | The remote hands back a run you query later |
+
+#### Deferred execution
+
+A deferred setup sends nothing when the caller submits. It only registers the
+invocation. The request goes out when the caller first awaits the result. So a call
+that is cancelled instead never issues its request.
+
+What you write is the interaction itself, in the `prepare` method of your setup class.
+`prepare` runs once per invocation and returns the durable call that carries it. The
+example is one blocking request, and whatever else your call does is yours to decide,
+because the framework does not know your protocol.
+
+The framework assigns `session_id` and `call_id` before it calls `prepare`, and they
+are the only values that a replay reproduces. The durable id you build from them is
+the key that recovery matches a recorded result against. When the key matches, your
+call is not run again. So build the key from those two values alone. Anything else in
+it, a timestamp for instance, makes a replay look up a different key and run the work
+a second time. Passing the same values to your own remote lets it recognize a
+re-issued request as the same one.
+
+The reconciler is optional. Omitting it leaves one crash window uncovered, and
+[Durability and failover](#durability-and-failover) describes that window. Supply one
+when the work cannot be repeated.
+
+{{< tabs "Supply a Reconciler" >}}
+
+{{< tab "Python" >}}
 ```python
-reviewer = ReActAgent(
-    chat_model=chat_model_descriptor,
-    prompt=Prompt.from_text(
-        "Review the following code diff and report any problems:\n\n{diff}"
+def prepare(
+    self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+) -> PreparedTriple:
+    key = f"{session_id}#{call_id}"
+
+    def call() -> SubagentResult:
+        return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+
+    # Consulted only during recovery, when this call is revisited and no result was
+    # recorded for it. It takes no arguments. Return the recovered outcome, because
+    # an exception here is a system-level failure that fails the action instead of
+    # reaching the caller.
+    def reconcile() -> SubagentResult:
+        # fetch_review_by_key is your own lookup; not part of Flink Agents.
+        found = fetch_review_by_key(self.endpoint, key)
+        if found is None:
+            return SubagentResult.error("the review service has no record of this call")
+        return SubagentResult.ok(found)
+
+    return (key, call, reconcile)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+// Inside the DurableCallable that prepare returns, alongside getId() and call().
+
+// Consulted only during recovery, when this call is revisited and no result was
+// recorded for it. Return the recovered outcome, because an exception here is a
+// system-level failure that fails the action instead of reaching the caller.
+@Override
+public Callable<SubagentResult> reconciler() {
+    return () -> {
+        // fetchReviewByKey is your own lookup; not part of Flink Agents.
+        String found = fetchReviewByKey(endpoint, key);
+        if (found == null) {
+            return SubagentResult.error("the review service has no record of this call");
+        }
+        return SubagentResult.ok(found);
+    };
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+{{< tabs "Deferred Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.runner_context import RunnerContext
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.deferred_subagent import (
+    DeferredSubagentSetup,
+    PreparedTriple,
+)
+
+
+def post_review(endpoint: str, prompt: Any, key: str) -> str:
+    """Your own call to the review service; not part of Flink Agents.
+
+    Keying the work lets the service treat a re-issued request as the same one.
+    """
+    ...
+
+
+class RemoteReviewSetup(DeferredSubagentSetup):
+    endpoint: str = ""
+
+    def prepare(
+        self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+    ) -> PreparedTriple:
+        key = f"{session_id}#{call_id}"
+
+        def call() -> SubagentResult:
+            try:
+                return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+            except Exception as e:
+                # An expected failure is a value; only a system fault should escape.
+                return SubagentResult.error(e)
+
+        # The first value is the key recovery matches a recorded result against, so
+        # it is built from the two ids alone. The third is the reconciler.
+        return (key, call, None)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.context.DurableCallable;
+import org.apache.flink.agents.api.context.RunnerContext;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseDeferredSubagentSetup;
+
+/**
+ * Your own call to the review service; not part of Flink Agents. Keying the work lets
+ * the service treat a re-issued request as the same one.
+ */
+static String postReview(String endpoint, Object prompt, String key) {
+    ...
+}
+
+public class RemoteReviewSetup extends BaseDeferredSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteReviewSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected DurableCallable<SubagentResult> prepare(
+            RunnerContext ctx, Object prompt, String sessionId, String callId) {
+        // Built from the two ids alone, so a replay reproduces it.
+        String key = sessionId + "#" + callId;
+        return new DurableCallable<SubagentResult>() {
+            @Override
+            public String getId() {
+                // The key recovery matches a recorded result against.
+                return key;
+            }
+
+            @Override
+            public Class<SubagentResult> getResultClass() {
+                return SubagentResult.class;
+            }
+
+            @Override
+            public SubagentResult call() {
+                try {
+                    return SubagentResult.ok(postReview(endpoint, prompt, key));
+                } catch (Exception e) {
+                    // An expected failure is a value; only a system fault should escape.
+                    return SubagentResult.error(e);
+                }
+            }
+        };
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup under the name invocations will use. Every descriptor argument
+beyond the built-in ones is configuration for the setup to read back. In Python it
+reads them as model fields, and in Java through the public
+`(ResourceDescriptor, ResourceContext)` constructor. The `endpoint` above is such
+configuration, not a built-in transport.
+
+{{< tabs "Register an External Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
+parent.add_resource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteReviewSetup",
+        endpoint="https://example.com/review",
+        description="Review code using the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
     ),
 )
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteReviewSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/review")
+        .addInitialArgument("description", "Review code using the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_reviewer
+        clazz: my_app.agents.RemoteReviewSetup
+        endpoint: https://example.com/review
+        description: Review code using the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The YAML form of that registration is the `subagents` list of an agent entry, as in
+the YAML tab above. That list can also sit at the shared document level, outside
+`agents:`, so that several agents in one file reuse it. A chat model setup declared in
+the same file also takes a `subagents` argument, but it is a list of registered names
+rather than a list of entries. See
+[delegate from a chat model](#delegate-from-a-chat-model).
+
+Two constraints apply to the entry:
 
+- No class alias is defined for `AGENT` resources, so `clazz` is always a
+  fully-qualified class path.
+- The entry's language must match the loader. A Python YAML file names a Python
+  class and may not set `type: java`. A Java YAML file must set `type: java`.
+
+#### Async (pub/sub) execution
+
+An async sub-agent talks to a service that runs the task for you. The service accepts
+the work and answers at once, and you query it later for the run's status and
+result.
+
+An async setup starts the remote run when the caller submits, through one durable
+POST. Waiting for the result then polls the run's status until it reaches a terminal
+state, and fetches it.
+
+The base class owns that submit-poll-fetch composition, along with the durable
+execution and the crash-window recovery. You supply the three transport calls it is
+built from:
+
+- `callSubmitRequest` / `call_submit_request` starts the run.
+- `callQueryStatus` / `call_query_status` returns a `RunStatus`, one of
+  `notStarted()`, `running()`, `completed()`, or `failed(error)`. It must be a
+  read-only probe with no side effects. Report an expected failure as a `FAILED`
+  status.
+- `callFetchResult` / `call_fetch_result` returns the `SubagentResult`. It must be
+  an idempotent read, because a replay may call it again. The status snapshot never
+  carries the payload, and a failed run is reported without fetching at all.
+
+{{< tabs "Async Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.async_subagent import BaseAsyncSubagentSetup, RunStatus
+
+# client(...) and task_id stand for your own transport client and run-id
+# scheme; neither is part of Flink Agents.
+
+
+class RemoteRunSetup(BaseAsyncSubagentSetup):
+    endpoint: str = ""
+
+    def call_submit_request(self, session_id: str, call_id: str, prompt: Any) -> None:
+        client(self.endpoint).submit(prompt, task_id=f"{session_id}#{call_id}")
+
+    def call_query_status(self, session_id: str, call_id: str) -> RunStatus:
+        probe = client(self.endpoint).status(task_id=f"{session_id}#{call_id}")
+        if probe is None:
+            return RunStatus.not_started()
+        if probe.succeeded:
+            return RunStatus.completed()
+        if probe.failed:
+            return RunStatus.failed(probe.error)
+        return RunStatus.running()
+
+    def call_fetch_result(self, session_id: str, call_id: str) -> SubagentResult:
+        return SubagentResult.ok(
+            client(self.endpoint).fetch(task_id=f"{session_id}#{call_id}")
+        )
+
+    # Optional, and a no-op by default, so a cancelled call leaves the remote run
+    # going. A replay may propagate the same cancellation again, so your remote has
+    # to accept it twice.
+    def call_cancel_request(self, session_id: str, call_id: str) -> None:
+        client(self.endpoint).cancel(task_id=f"{session_id}#{call_id}")
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseAsyncSubagentSetup;
+
+// client() and taskId(...) stand for your own transport client and run-id
+// scheme; neither is part of Flink Agents.
+public class RemoteRunSetup extends BaseAsyncSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteRunSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected void callSubmitRequest(String sessionId, String callId, Object prompt)
+            throws Exception {
+        client().submit(String.valueOf(prompt), taskId(sessionId, callId));
+    }
+
+    @Override
+    protected RunStatus callQueryStatus(String sessionId, String callId) {
+        // TaskStatus is your own transport type, not a framework type.
+        TaskStatus probe = client().status(taskId(sessionId, callId));
+        if (probe == null) {
+            return RunStatus.notStarted();
+        }
+        if (probe.succeeded()) {
+            return RunStatus.completed();
+        }
+        if (probe.failed()) {
+            return RunStatus.failed(probe.error());
+        }
+        return RunStatus.running();
+    }
+
+    @Override
+    protected SubagentResult callFetchResult(String sessionId, String callId) throws Exception {
+        return client().fetchResult(taskId(sessionId, callId));
+    }
+
+    // Optional, and a no-op by default, so a cancelled call leaves the remote run
+    // going. A replay may propagate the same cancellation again, so your remote has
+    // to accept it twice.
+    @Override
+    protected void callCancelRequest(String sessionId, String callId) {
+        client().cancel(taskId(sessionId, callId));
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup like any external sub-agent. The async form adds one descriptor
+argument, `status_poll_interval_millis`, and it defaults to 500.
+
+{{< tabs "Register an Async Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
 parent.add_resource(
-    "reviewer",
+    "remote_runner",
     ResourceType.AGENT,
-    InternalSubagentResourceDescriptorBuilder()
-    .agent(reviewer)
-    .description("Reviews a code diff and reports problems.")
-    # "diff" matches the {diff} placeholder of the child's prompt.
-    .input_schema(
-        '{"type":"object","properties":{"diff":{"type":"string"}},'
-        '"required":["diff"]}'
-    )
-    .build(),
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteRunSetup",
+        endpoint="https://example.com/runs",
+        description="Run a review task on the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
+        status_poll_interval_millis=500,
+    ),
 )
 ```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_runner",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteRunSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/runs")
+        .addInitialArgument("description", "Run a review task on the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .addInitialArgument("status_poll_interval_millis", 500L)
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_runner
+        clazz: my_app.agents.RemoteRunSetup
+        endpoint: https://example.com/runs
+        description: Run a review task on the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+        status_poll_interval_millis: 500
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The base class also recovers the crash window between "POST sent" and "POST durably
+recorded". On recovery it probes the run's status first, and it re-posts only when the
+probe reports that the run never started. Override `reconcileSubmitRequest` /
+`reconcile_submit_request` only if your service needs a different repair.
+
+## Invoke a sub-agent
+
+A sub-agent is invoked in one of two ways, model-driven or caller-driven. The
+difference you see is where the result goes.
+
+In model-driven delegation the result returns to the model. The sub-agent is offered
+to the model as a tool, so your action sends a `ChatRequestEvent` and the model
+answers with a call to the sub-agent. The result goes back to the model as the tool
+observation, and the model reasons over it until a final `ChatResponseEvent` reaches
+your action.
+
+In caller-driven delegation the result returns to your action. An action that needs
+the delegated outcome calls the sub-agent itself, with no model in the loop.
+
+Both ways end in the same call. Your action, or the framework on the model's behalf,
+runs `submit(ctx, prompt)` on the registered name. That call returns a
+`SubagentFuture`, which is the handle for that one call. Awaiting the handle yields
+the call's `SubagentResult`.
+
+### Delegate from a chat model
+
+List the registered name under `subagents` of the parent's chat model:
+
+{{< tabs "Declare Sub-agents on a Chat Model" >}}
+
+{{< tab "Python" >}}
+```python
+ResourceDescriptor(
+    clazz=ResourceName.ChatModel.OLLAMA_SETUP,
+    connection="ollama_connection",
+    model="qwen3:8b",
+    subagents=["reviewer"],
+)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_SETUP)
+    .addInitialArgument("connection", "ollamaConnection")
+    .addInitialArgument("model", "qwen3:8b")
+    .addInitialArgument("subagents", List.of("reviewer"))
+    .build();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The model sees `reviewer` as a tool, described by the registration's description and
+input schema. A schema is required for model-selected calls. A missing description
+falls back to a generic delegation description.
+
+The arguments the model sends become the sub-agent's input, and its outputs return to
+the model as the tool observation. The model can then keep reasoning over them. For
+an internal sub-agent, the observation is the JSON encoding of the full output list
+described in [the result](#the-result). That holds even when the child emits a single
+output, so the shape does not depend on how many outputs the child emits.
+
+An external setup class can state the shape of that observation. It declares
+`result_type()` in Python or `getResultType()` in Java, and the default reports the
+result as it arrived. Declare one to narrow a wider payload, or to convert a payload
+that is not already JSON-compatible. An internal sub-agent has no such declaration, so
+its observation is always the JSON encoding of its output list. This shapes the
+observation only. A caller that awaits the handle gets the result the sub-agent
+produced, as [the result](#the-result) describes.
+
+The external sub-agents `remote_reviewer` and `remote_runner` are offered the same
+way. Only their registration differs.
+
+### Call from an action
+
+An action can submit input to a registered sub-agent directly and await its
+outcome. This path needs no description and no input declaration, so it also works
+for a child registered as-is.
+
+{{< tabs "Call a Sub-agent From an Action" >}}
+
+{{< tab "Python" >}}
+```python
+class DelegatingAgent(Agent):
+    @action(EventType.InputEvent)
+    @staticmethod
+    async def delegate(event: Event, ctx: RunnerContext) -> None:
+        reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+        future = await reviewer.submit(ctx, "review the diff")
+        result = await future
+        if result.success:
+            ctx.send_event(OutputEvent(output=result.result[0]))
+        else:
+            ctx.send_event(OutputEvent(output=f"failed: {result.error_message}"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+public class DelegatingAgent extends Agent {
+
+    @Action(EventType.InputEvent)
+    public static void delegate(Event event, RunnerContext ctx) throws Exception {
+        SubagentSetup reviewer =
+                (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+        SubagentResult result = reviewer.submit(ctx, "review the diff").await();
+        if (result.isSuccess()) {
+            String answer = String.valueOf(((List<?>) result.getResult()).get(0));
+            ctx.sendEvent(new OutputEvent(answer));
+        } else {
+            ctx.sendEvent(new OutputEvent("failed: " + result.getErrorMessage()));
+        }
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+What you pass to `submit` is what the sub-agent receives. An internal child gets it
+as its `InputEvent` payload, and a model-driven call passes the arguments map
+instead. What comes back is a `SubagentResult`. For the internal sub-agent used here,
+its `result` is the list of `OutputEvent` payloads the child emitted. That is why the
+example reads `result.result[0]`. An external sub-agent returns whatever its setup
+produces.
+
+In Python `submit` is a coroutine, so the call is awaited in two phases and the
+action must be `async`. In Java `submit` returns the handle directly and `await()`
+yields the result. The same two calls drive `remote_reviewer` and `remote_runner`
+unchanged. Only the registration differs.
+
+## Work with open handles
+
+A `SubagentFuture` is live from `submit` until it is awaited or cancelled. Awaiting
+it yields the call's `SubagentResult`. It also carries the call's `session_id` and
+`call_id`, so you can read them to continue the session or to match the call against
+your own records.
+
+### Fan out to several sub-agents
+
+`combine` groups handles into a `SubagentFutures`, and resolving the batch resolves
+every handle in it. `gather` does not accept a `SubagentFuture`, so handles batch this
+way instead. The results come back in the order the handles were added.
+
+{{< tabs "Fan Out to Several Sub-agents" >}}
+
+{{< tab "Python" >}}
+```python
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+first = await reviewer.submit(ctx, "review the diff")
+second = await reviewer.submit(ctx, "review the tests")
+
+outcomes = await first.combine(second)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture first = reviewer.submit(ctx, "review the diff");
+SubagentFuture second = reviewer.submit(ctx, "review the tests");
+
+List<SubagentResult> outcomes = first.combine(second).awaitAll();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+### Continue a session
+
+Pass the `session_id` of an earlier handle to continue that conversation. The new
+call gets a fresh `call_id` in the same session. Whether a conversation can be
+continued across actions is up to the implementation.
+
+{{< tabs "Continue a Session" >}}
+
+{{< tab "Python" >}}
+```python
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+first = await reviewer.submit(ctx, "review the diff")
+first_result = await first
+
+second = await reviewer.submit(ctx, "now the tests", first.session_id)
+second_result = await second
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture first = reviewer.submit(ctx, "review the diff");
+SubagentResult firstResult = first.await();
+
+SubagentFuture second = reviewer.submit(ctx, "now the tests", first.getSessionId());
+SubagentResult secondResult = second.await();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+### Cancel and resolve
+
+Resolve every handle you submit. An action that finishes with a handle it neither
+awaited nor cancelled fails with `Action <name> finished without resolving the
+sub-agent calls it submitted: <ids>`. Cancelling counts as resolving, and awaiting a
+cancelled handle throws `CancellationException` in Java and raises `CancelledError` in
+Python.
+
+{{< tabs "Cancel a Sub-agent Call" >}}
+
+{{< tab "Python" >}}
+```python
+from concurrent.futures import CancelledError
+
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+future = await reviewer.submit(ctx, "review the diff")
+future.cancel()
+
+try:
+    await future
+except CancelledError:
+    ctx.send_event(OutputEvent(output="cancelled"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture future = reviewer.submit(ctx, "review the diff");
+future.cancel();
+
+try {
+    future.await();
+} catch (CancellationException e) {
+    ctx.sendEvent(new OutputEvent("cancelled"));
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+`cancel()` on a deferred handle takes effect only before the request is issued.
+
+## The outcome of a call
+
+### The result
+
+The awaited value of every call is a `SubagentResult`, which carries the outcome in
+three fields:
+
+| Field | Java | Carries |
+|---|---|---|
+| `success` | `isSuccess()` | Whether the call succeeded |
+| `result` | `getResult()` | The answer on success. For an internal sub-agent, the list of `OutputEvent` payloads in emission order. For an external one, whatever the setup produces, such as a string or an object |
+| `error_message` | `getErrorMessage()` | The reason on failure |
+
+A result a model observes must be JSON-compatible. That means string map keys,
+finite numbers, and no cycles. The framework normalizes the result on its way into
+the tool observation, and one it cannot normalize fails the delegation.
+
+A failover restores the recorded result, and the two languages restore it
+differently. Python persists the payload with cloudpickle, so a replay hands it back
+with the type the sub-agent produced. Java persists it as JSON, so `getResult()`
+hands back a map after a replay, because recovery re-binds the recorded payload
+without your type. `getResult(Class<T>)` converts on the first run and after a
+replay alike, so prefer it when you want your own type back.
+
+{{< tabs "Read a SubagentResult" >}}
+
+{{< tab "Python" >}}
+```python
+if result.success:
+    # The payload keeps the type the sub-agent produced, after a replay too.
+    ctx.send_event(OutputEvent(output=result.result["verdict"]))
+else:
+    ctx.send_event(OutputEvent(output=f"failed: {result.error_message}"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+if (result.isSuccess()) {
+    // Review is your own type, the shape this sub-agent produces.
+    Review review = result.getResult(Review.class);
+    ctx.sendEvent(new OutputEvent(review.verdict));
+} else {
+    ctx.sendEvent(new OutputEvent("failed: " + result.getErrorMessage()));
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+### How failures surface
+
+An expected failure is a value, and an unexpected one is an exception. Return
+`SubagentResult.error(...)` for anything the caller or the model should react to. An
+exception escaping the durable call is a system-level failure. It fails the action and
+triggers recovery.
+
+- A failed result reaches a caller as `success == false` with the reason in
+  `error_message`.
+- A failed result reaches a model as a tool observation that names the sub-agent and
+  the reason, so the model can decide what to do next. An exception while submitting
+  or awaiting is reported the same way.
+- A failing child action does not fail the job. It surfaces through the caller's
+  result. The text in `error_message` is not a contract, so test for the failure
+  rather than for the message.
+- A result that is not JSON-compatible fails the delegation rather than the job.
+
+### Durability and failover
+
+A sub-agent call always runs through
+[durable execution]({{< ref "docs/development/create-agent/actions#durable-execution" >}}).
+Its result is persisted and replayed on recovery, so the work is not repeated after
+a failover. There is no durability toggle and no sub-agent timeout option.
+
+- Identities are replay-stable. Without explicit ids, the framework assigns
+  `session_id` and `call_id` from the executing action and the sub-agent's resource
+  name, so a replay of the same call gets the same ids. The ids are opaque, so do not
+  parse them. Two sub-agents called from one action never collide, because the
+  resource name is part of the namespace.
+- A deferred call runs under the durable id you build in `prepare`, so a replay
+  re-issues the same logical invocation. A remote keyed on the same values then reuses
+  the original work instead of starting a second one. The reconciler is optional.
+  Without it, a crash between the call landing and its result being persisted re-runs
+  the sub-agent and duplicates its side effects, so supply one whenever the work
+  cannot be repeated.
+- An async call reuses one identity for both its POST and its poll, so your service
+  sees at most one run per call.
+- An internal sub-agent's child actions have their own persisted state, so a replay
+  reuses the recorded child output instead of running the child again.
+
+### Metrics and Trace Log
+
+Only the delegations a model requests are reported. A sub-agent you drive yourself
+from an action emits no sub-agent metrics and no sub-agent trace records. Add
+[custom metrics]({{< ref "docs/operations/monitoring#how-to-add-custom-metrics" >}})
+if you need to observe that path.
+
+A model-driven delegation is counted under a `subagent` scope keyed by the registered
+sub-agent name. It is reported there only and never also as a tool, so the scope's
+cardinality is bounded by your plan.
+
+| Metric | Counts |
+|---|---|
+| `numOfSubagentCallsSucceeded` | Delegations that returned a successful result |
+| `numOfSubagentCallsFailed` | Delegations that failed, either while submitting or awaiting, or by returning a failed result |
+| `subagentCallLatencyMs` | Time from handing the prompt to the sub-agent until its result is observed |
+
+Full metric paths and types are in
+[Execution Metrics]({{< ref "docs/operations/monitoring#execution-metrics" >}}).
+
+The same delegations appear in
+[Trace Log]({{< ref "docs/operations/monitoring#trace-log" >}}) as call records with
+`entityType: "subagent"`. The default target records events only, so select the
+`subagent` scope, or `ALL`, to get them.
+
+## Restrictions
 
-The framework does not check the schema against the prompt. A field the prompt
-does not use is ignored, and a placeholder no field fills is left in the prompt
-as literal text, so keep the two in step when changing either.
+- Recursive registration is rejected when the plan is built, whether the cycle runs
+  through a child, through the root agent, or directly back to itself.
+- Registering the same child `Agent` instance under two names compiles it once and
+  shares the plan.
+- `_subagent_` is a reserved prefix. A sub-agent reaches a model as a tool whose name
+  carries it, and no tool may be registered under the prefix, including a tool an MCP
+  server advertises. A tool and a sub-agent may therefore share a name, because the
+  prefix keeps the two namespaces apart. See
+  [Tool Use]({{< ref "docs/development/calling-llms/tool_use" >}}).
+- A sub-agent declared in one language cannot be driven from the other. Delegation
+  resolves the `AGENT` resource in the executing language only.
+- Resolving an internal sub-agent call in Java requires JDK 21+ with the Continuation
+  API. On an older runtime the call fails fast rather than hanging. Python callers
+  and external sub-agents have no JDK requirement.
+- When a model emits several sub-agent calls in one turn, and `tool-call.async` is
+  enabled with `async.batch.parallelism` above 1, the framework submits every call
+  before awaiting any of them. An async sub-agent starts its remote run during that
+  submit, so those runs overlap. A deferred or internal call does its work while it
+  is awaited, so it does not overlap this way, and batching handles yourself with
+  [combine](#fan-out-to-several-sub-agents) does not change that. Results still land

Review Comment:
   Fixed. The distinction now lives in the combine section, and the restriction points there instead of asserting that combine changes nothing.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "yunfengzhou-hub (via GitHub)" <gi...@apache.org>.
yunfengzhou-hub commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4309922519


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -24,35 +24,136 @@ specific language governing permissions and limitations
 under the License.
 -->
 
-A sub-agent handles a task delegated by a parent agent. An internal sub-agent runs
-an agent workflow in the same job. An external sub-agent calls an agent service
-through a `SubagentSetup` implementation.
+## Overview
 
-To declare parent and child workflows in YAML, see
-[Declaring sub-agents]({{< ref "docs/development/create-agent/yaml#declaring-sub-agents" >}}).
-YAML supports references to internal child Agents in the same file and descriptors
-for external sub-agents.
+A sub-agent is an agent that another agent delegates a task to. The parent hands it
+input, the sub-agent does the work, and the parent receives the outcome as a
+`SubagentResult`. Use one when the delegated work is multi-step or long-running and
+should run isolated from the parent.
 
-## Declare an internal sub-agent
+The work runs either inside the job or outside it. An *internal* sub-agent is a
+child `Agent`, including a `ReActAgent`, compiled into the parent's plan. The
+child's own actions and events run in the same job. An *external* sub-agent has no
+plan of its own. The work happens in your own code or in a remote agent service,
+outside the parent's event loop. It takes one of two forms, *deferred* or *async
+(pub/sub)*.
 
-Use `InternalSubagentResourceDescriptorBuilder` to attach a description and input
-schema to a child agent. The builder returns a normal resource descriptor for `addResource`
-or `add_resource`.
+Either way, the call is initiated by the model or by you. In *model-driven
+delegation* you list the sub-agent on a chat model setup and the model chooses when
+to delegate. In *caller-driven delegation* one of your actions submits the task
+itself.
 
+Every sub-agent call runs through
+[durable execution]({{< ref "docs/development/create-agent/actions#durable-execution" >}}).
+
+## Declare a sub-agent
+
+A sub-agent enters the parent's plan as a resource registered under
+`ResourceType.AGENT`. The name you register it under is the name that invocations and
+metrics use. What you register depends on the form. An internal sub-agent registers
+a child `Agent`. An external sub-agent registers a descriptor that names your setup
+class. A YAML file declares either form, as
+[Declaring sub-agents]({{< ref "docs/development/create-agent/yaml#declaring-sub-agents" >}})
+describes.
+
+### Internal: compile a child agent {#declare-an-internal-sub-agent}
+
+Nothing in a child's own code marks it as a sub-agent. The child is an ordinary
+agent with its own resources, actions, and event loop, and it runs standalone
+exactly as written. A parent registers it as an `AGENT` resource, and that
+registration is the whole of the difference. It gives the child a name in the
+parent's plan. It can also give the child an input schema, so a chat model can call
+it. And it supplies the caller that submits input and awaits the child's outputs.
+
+Any `Agent` can serve as the child. The two common kinds are a workflow agent and a
+`ReActAgent`. Either way, the child's plan is compiled into the parent's when the
+parent's plan is built.
+
+#### A workflow agent as the child
+
+Any workflow agent can serve as the child. The one used through this page is built
+the way
+[Request and Response Example]({{< ref "docs/development/calling-llms/chat#usage-example" >}})
+builds an agent that owns a chat model, and this page calls it `ReviewAgent`. It has
+a connection resource, a model setup, an action that turns an `InputEvent` into a
+`ChatRequestEvent`, and an action that turns the `ChatResponseEvent` into
+`OutputEvent`s.
+
+The one part a sub-agent changes is how that first action reads its input. A
+caller-driven call passes the object the caller submitted. A model-driven call
+passes the arguments map built from the input schema.
+
+{{< tabs "Read the Input in a Child Action" >}}
+
+{{< tab "Python" >}}
+```python
+# Inside ReviewAgent.
+
+
+@action(EventType.InputEvent)
+@staticmethod
+def ask(event: Event, ctx: RunnerContext) -> None:
+    task = InputEvent.from_event(event).input
+    # A model-driven call passes the arguments map; a caller-driven call
+    # passes the submitted object.
+    diff = task["code"] if isinstance(task, dict) else str(task)
+    ctx.send_event(
+        ChatRequestEvent(
+            model="review_model",

Review Comment:
   Fixed. Both snippets now use ollama_chat_model and ollamaChatModel, matching the example they link to.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "yunfengzhou-hub (via GitHub)" <gi...@apache.org>.
yunfengzhou-hub commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4300859145


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -67,156 +207,147 @@ parent.add_resource(
         .build(),
 )
 ```
+{{< /tab >}}
 
-`ReviewAgent` is your child workflow; the Java `ReviewRequest` is a data class
-with fields describing its input. Alternatively, pass a JSON schema string to
-`inputSchema(...)` in Java or `input_schema(...)` in Python. Choose either an
-input type or an explicit schema. The schema describes the submitted arguments,
-which the child receives as its input event payload; it does not convert that
-payload into an instance of the input type.
-
-When a chat model is configured with `subagents=["reviewer"]`, the description
-helps it choose when to delegate, and the schema tells it which arguments to
-send. A schema is required for model-selected calls. A missing description uses
-a generic delegation description.
-
-For calls made explicitly by an action, the existing shorthand remains available:
-
+{{< tab "Java" >}}
 ```java
-parent.addResource("reviewer", ResourceType.AGENT, new ReviewAgent());
-```
+import org.apache.flink.agents.api.resource.ResourceType;
+import org.apache.flink.agents.api.subagent.InternalSubagentResourceDescriptorBuilder;
 
-```python
-parent.add_resource("reviewer", ResourceType.AGENT, ReviewAgent())
+parent.addResource(
+    "reviewer",
+    ResourceType.AGENT,
+    new InternalSubagentResourceDescriptorBuilder()
+        .agent(new ReviewAgent())
+        .description("Review code for correctness and potential defects")
+        .inputType(ReviewRequest.class)
+        .build());
 ```
+{{< /tab >}}
 
-This shorthand does not declare an input schema. Use the builder when exposing
-the child to a model. Recursive registration, such as a child registering its
-parent, is rejected when the plan is built.
+{{< /tabs >}}
 
-Register the resources a child uses, such as its chat model connection, on the
-child itself. In Python, a child resolves resources against its own plan only,
-so a resource registered solely on the parent is not visible to the child.
+`ReviewRequest` describes the arguments the child accepts, and the Java form is a bean.
+Where no type fits, a JSON schema string declares the same arguments:
 
-## Declare an external sub-agent
+{{< tabs "Declare the Input as a Schema" >}}
 
-Implement `SubagentSetup` (or the appropriate runtime base class) and register a
-resource descriptor naming that implementation. Do not register a setup instance.
+{{< tab "Python" >}}
+```python
+parent.add_resource(
+    "reviewer",
+    ResourceType.AGENT,
+    InternalSubagentResourceDescriptorBuilder()
+        .agent(ReviewAgent())
+        .description("Review code for correctness and potential defects")
+        .input_schema(
+            '{"type":"object","properties":{"code":{"type":"string"}},'
+            '"required":["code"]}'
+        )
+        .build(),
+)
+```
+{{< /tab >}}
 
+{{< tab "Java" >}}
 ```java
 parent.addResource(
-    "remote_reviewer",
+    "reviewer",
     ResourceType.AGENT,
-    ResourceDescriptor.Builder.newBuilder(RemoteReviewSetup.class.getName())
-        .addInitialArgument("endpoint", "https://example.com/review")
-        .addInitialArgument("description", "Review code using the remote service")
-        .addInitialArgument("input_schema", inputSchema)
+    new InternalSubagentResourceDescriptorBuilder()
+        .agent(new ReviewAgent())
+        .description("Review code for correctness and potential defects")
+        .inputSchema(
+                "{\"type\":\"object\",\"properties\":{\"code\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"code\"]}")
         .build());
 ```
+{{< /tab >}}
 
-The Java setup exposes a public `(ResourceDescriptor, ResourceContext)`
-constructor and implements the sub-agent invocation contract. `endpoint` above
-is configuration read by the example implementation, not a built-in transport.
+{{< /tabs >}}
 
-```python
-parent.add_resource(
-    "remote_reviewer",
-    ResourceType.AGENT,
-    ResourceDescriptor(
-        clazz="my_app.agents.RemoteReviewSetup",
-        endpoint="https://example.com/review",
-        description="Review code using the remote service",
-        input_schema=input_schema,
-    ),
-)
-```
+Declare either an input type or an input schema, not both. A declaration that carries
+both is rejected when the descriptor is built. In Python the type must be a pydantic
+model, and a type that is not is rejected there too.
 
-The Python setup declares these configuration fields and implements the
-invocation contract. Both languages construct the setup at runtime from the
-descriptor. Existing code that passes a setup instance to `addResource` or
-`add_resource` must instead pass a descriptor with the same configuration.
+An input type is a shorthand for a schema. The framework derives the schema from the
+type and registers that, so use a type when you already have one and a schema when
+you do not. Nothing is ever converted into an instance of the type. The sub-agent
+receives the arguments as a map keyed by the schema's field names.
 
-## Delegate from a chat model
+For calls an action makes itself, the child registers as-is:
 
-List the child's registered name under `subagents` of the parent's chat model:
+{{< tabs "Register the Child Agent Directly" >}}
 
-```java
-ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_SETUP)
-    .addInitialArgument("connection", "ollamaConnection")
-    .addInitialArgument("model", "qwen3:8b")
-    .addInitialArgument("subagents", List.of("reviewer"))
-    .build();
+{{< tab "Python" >}}
+```python
+parent.add_resource("reviewer", ResourceType.AGENT, ReviewAgent())
 ```
+{{< /tab >}}
 
-```python
-ResourceDescriptor(
-    clazz=ResourceName.ChatModel.OLLAMA_SETUP,
-    connection="ollama_connection",
-    model="qwen3:8b",
-    subagents=["reviewer"],
-)
+{{< tab "Java" >}}
+```java
+parent.addResource("reviewer", ResourceType.AGENT, new ReviewAgent());
 ```
+{{< /tab >}}
 
-The model sees the child as a tool named `_subagent_reviewer`, described by the
-registration's description and input schema. The arguments the model sends
-become the child's input, and the child's outputs return to the model as the
-tool observation, so it can continue reasoning over them. For an internal
-sub-agent, the observation is the JSON encoding of the full output list
-described in [Call from an action](#call-from-an-action), even when the child
-emits a single output, so its shape does not depend on how many outputs the
-child emits.
+{{< /tabs >}}
 
-## Call from an action
+This form carries no description and no input declaration, so a chat model rejects
+the sub-agent at setup time rather than calling it with no arguments. Only the
+descriptor form exposes a child to a model.
 
-An action can submit input to a child directly and await its outcome. This path
-does not need an input schema.
+There is no third form. An internal sub-agent cannot be declared in YAML, because

Review Comment:
   Fixed in the rebase. The restriction is gone, the Declare section links to Declaring sub-agents, and the internal section states the agent field. Loaded all three YAML forms against the new loaders to confirm.



##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -24,30 +24,170 @@ specific language governing permissions and limitations
 under the License.
 -->
 
-A sub-agent handles a task delegated by a parent agent. An internal sub-agent runs
-an agent workflow in the same job. An external sub-agent calls an agent service
-through a `SubagentSetup` implementation.
+## Overview
 
-## Declare an internal sub-agent
+A sub-agent is an agent that another agent delegates a task to. The parent hands it
+input, the sub-agent does the work, and the parent receives the outcome as a
+`SubagentResult`. Use one when the delegated work is multi-step or long-running and
+should run isolated from the parent.
 
-Use `InternalSubagentResourceDescriptorBuilder` to attach a description and input
-schema to a child agent. The builder returns a normal resource descriptor for `addResource`
-or `add_resource`.
+The work runs either inside the job or outside it. An *internal* sub-agent is a
+child `Agent`, including a `ReActAgent`, compiled into the parent's plan. The
+child's own actions and events run in the same job. An *external* sub-agent has no
+plan of its own. The work happens in your own code or in a remote agent service,
+outside the parent's event loop. It takes one of two forms, *deferred* or *async
+(pub/sub)*.
 
+Either way, the call is initiated by the model or by you. In *model-driven
+delegation* you list the sub-agent on a chat model setup and the model chooses when
+to delegate. In *caller-driven delegation* one of your actions submits the task
+itself.
+
+Every sub-agent call runs through
+[durable execution]({{< ref "docs/development/create-agent/actions#durable-execution" >}}).
+There is no durability toggle and no sub-agent timeout option.
+
+## Declare a sub-agent
+
+A sub-agent enters the parent's plan as a resource registered under
+`ResourceType.AGENT`. The name you register it under is the name that invocations and
+metrics use. What you register depends on the form. An internal sub-agent registers
+a child `Agent`. An external sub-agent registers a descriptor that names your setup
+class, and a YAML file can declare it.
+
+### Internal: compile a child agent
+
+Nothing in a child's own code marks it as a sub-agent. The child is an ordinary
+agent with its own resources, actions, and event loop, and it runs standalone
+exactly as written. A parent registers it as an `AGENT` resource, and that
+registration is the whole of the difference. It gives the child a name in the
+parent's plan. It can also give the child an input schema, so a chat model can call
+it. And it supplies the caller that submits input and awaits the child's outputs.
+
+Any `Agent` can serve as the child. The two common kinds are a workflow agent and a
+`ReActAgent`. Either way, the child's plan is compiled into the parent's when the
+parent's plan is built.
+
+#### A workflow agent as the child
+
+The example child owns a chat model and answers each review request with its

Review Comment:
   All three done. The child example links to chat#usage-example and keeps only the input adaptation. The toggle and timeout detail now survives only in Durability and failover. The table uses your three rows.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "yunfengzhou-hub (via GitHub)" <gi...@apache.org>.
yunfengzhou-hub commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4300865107


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -307,30 +443,854 @@ parent.addResource(
                                 + "\"required\":[\"diff\"]}")
                 .build());
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The framework does not check the schema against the prompt. A field the prompt does
+not use is ignored, and a placeholder no field fills is left in the prompt as literal
+text. So keep the two in step when you change either.
+
+### External: write a setup class
+
+An external sub-agent has no plan of its own, and it is not compiled into the
+parent's. The work happens in your own code or in a remote agent service. The parent
+only submits to it and waits for the outcome. The framework cannot perform that work
+for you, because it does not know your protocol. So you write the part that performs
+one invocation, and the framework supplies everything around it. It runs your code
+under durable execution, and after a failover it replays the recorded result instead
+of calling your code again.
+
+What you write is a setup class. Its only job is to carry out one invocation, so it
+declares no actions and no events of its own. It extends a framework base class and
+implements the methods that class declares.
+
+Registration names the setup class in a descriptor, and both languages construct the
+setup from the descriptor at runtime. Registering a setup instance instead is
+rejected.
+
+The invocation itself is carried out in one of two modes, and which one you write
+follows from your remote interface. When one request returns the result, write a
+*deferred* setup. You own the whole interaction, and the framework only holds it back
+until the caller asks for the result. When the remote instead hands back a run that
+you later query for status and result, write an *async (pub/sub)* setup. The base
+class owns that composition of publishing, polling, and fetching, and you supply the
+transport calls it is built from.
+
+#### Deferred execution
+
+A deferred setup sends nothing when the caller submits. It only registers the
+invocation. The request goes out when the caller first awaits the result. So a call
+that is cancelled instead never issues its request.
+
+What you write is the interaction itself, in the `prepare` method of your setup class.
+`prepare` runs once per invocation and returns the durable call that carries it. The
+example is one blocking request, and whatever else your call does is yours to decide,
+because the framework does not know your protocol.
 
+The framework assigns `session_id` and `call_id` before it calls `prepare`, and they
+are the only values that a replay reproduces. The durable id you build from them is
+the key that recovery matches a recorded result against. When the key matches, your
+call is not run again. So build the key from those two values alone. Anything else in
+it, a timestamp for instance, makes a replay look up a different key and run the work
+a second time. Passing the same values to your own remote lets it recognize a
+re-issued request as the same one.
+
+The reconciler is optional. Omitting it leaves one crash window uncovered, and
+[Durability and failover](#durability-and-failover) describes that window. Supply one
+when the work cannot be repeated.
+
+{{< tabs "Supply a Reconciler" >}}
+
+{{< tab "Python" >}}
 ```python
-reviewer = ReActAgent(
-    chat_model=chat_model_descriptor,
-    prompt=Prompt.from_text(
-        "Review the following code diff and report any problems:\n\n{diff}"
+def prepare(
+    self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+) -> PreparedTriple:
+    key = f"{session_id}#{call_id}"
+
+    def call() -> SubagentResult:
+        return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+
+    # Consulted only during recovery, when this call is revisited and no result was
+    # recorded for it. Return the recovered outcome, or raise the recovered failure.
+    # It takes no arguments.
+    def reconcile() -> SubagentResult:
+        # fetch_review_by_key is your own lookup; not part of Flink Agents.
+        found = fetch_review_by_key(self.endpoint, key)
+        if found is None:
+            raise RuntimeError("the review service has no record of this call")
+        return SubagentResult.ok(found)
+
+    return (key, call, reconcile)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+// Inside the DurableCallable that prepare returns, alongside getId() and call().
+
+// Consulted only during recovery, when this call is revisited and no result was
+// recorded for it. Return the recovered outcome, or throw the recovered failure.
+@Override
+public Callable<SubagentResult> reconciler() {
+    return () -> {
+        // fetchReviewByKey is your own lookup; not part of Flink Agents.
+        String found = fetchReviewByKey(endpoint, key);
+        if (found == null) {
+            throw new IllegalStateException("the review service has no record of this call");
+        }
+        return SubagentResult.ok(found);
+    };
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+{{< tabs "Deferred Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.runner_context import RunnerContext
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.deferred_subagent import (
+    DeferredSubagentSetup,
+    PreparedTriple,
+)
+
+
+def post_review(endpoint: str, prompt: Any, key: str) -> str:
+    """Your own call to the review service; not part of Flink Agents.
+
+    Keying the work lets the service treat a re-issued request as the same one.
+    """
+    ...
+
+
+class RemoteReviewSetup(DeferredSubagentSetup):
+    endpoint: str = ""
+
+    def prepare(
+        self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+    ) -> PreparedTriple:
+        key = f"{session_id}#{call_id}"
+
+        def call() -> SubagentResult:
+            try:
+                return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+            except Exception as e:
+                # An expected failure is a value; only a system fault should escape.
+                return SubagentResult.error(e)
+
+        # The first value is the key recovery matches a recorded result against, so
+        # it is built from the two ids alone. The third is the reconciler.
+        return (key, call, None)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.context.DurableCallable;
+import org.apache.flink.agents.api.context.RunnerContext;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseDeferredSubagentSetup;
+
+/**
+ * Your own call to the review service; not part of Flink Agents. Keying the work lets
+ * the service treat a re-issued request as the same one.
+ */
+static String postReview(String endpoint, Object prompt, String key) {
+    ...
+}
+
+public class RemoteReviewSetup extends BaseDeferredSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteReviewSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected DurableCallable<SubagentResult> prepare(
+            RunnerContext ctx, Object prompt, String sessionId, String callId) {
+        // Built from the two ids alone, so a replay reproduces it.
+        String key = sessionId + "#" + callId;
+        return new DurableCallable<SubagentResult>() {
+            @Override
+            public String getId() {
+                // The key recovery matches a recorded result against.
+                return key;
+            }
+
+            @Override
+            public Class<SubagentResult> getResultClass() {
+                return SubagentResult.class;
+            }
+
+            @Override
+            public SubagentResult call() {
+                try {
+                    return SubagentResult.ok(postReview(endpoint, prompt, key));
+                } catch (Exception e) {
+                    // An expected failure is a value; only a system fault should escape.
+                    return SubagentResult.error(e);
+                }
+            }
+        };
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup under the name invocations will use. Every descriptor argument
+beyond the built-in ones is configuration for the setup to read back. In Python it
+reads them as model fields, and in Java through the public
+`(ResourceDescriptor, ResourceContext)` constructor. The `endpoint` above is such
+configuration, not a built-in transport.
+
+{{< tabs "Register an External Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
+parent.add_resource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteReviewSetup",
+        endpoint="https://example.com/review",
+        description="Review code using the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
     ),
 )
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteReviewSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/review")
+        .addInitialArgument("description", "Review code using the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_reviewer
+        clazz: my_app.agents.RemoteReviewSetup
+        endpoint: https://example.com/review
+        description: Review code using the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The YAML form of that registration is the `subagents` list of an agent entry, as in
+the YAML tab above. That list can also sit at the shared document level, outside
+`agents:`, so that several agents in one file reuse it. A chat model setup declared in
+the same file also takes a `subagents` argument, but it is a list of registered names
+rather than a list of entries. See
+[delegate from a chat model](#delegate-from-a-chat-model).
+
+Two constraints apply to the entry:
+
+- No class alias is defined for `AGENT` resources, so `clazz` is always a
+  fully-qualified class path.
+- The entry's language must match the loader. A Python YAML file names a Python
+  class and may not set `type: java`. A Java YAML file must set `type: java`.
+
+#### Async (pub/sub) execution
+
+An async sub-agent talks to a service that runs the task for you. The service accepts
+the work and answers at once, and you query it later for the run's status and
+result.
+
+An async setup publishes the run when the caller submits, through one durable POST.
+Waiting for the result then polls the run's status until it reaches a terminal state,
+and fetches it.
+
+That is the difference from the deferred mode, which sends nothing when the caller
+submits. The mode's name is the framework's own. The POST is the publish, and the
+polling is the subscribe.
+
+The submit-poll-fetch composition is the same for every such remote, so the base
+class owns it, along with the durable execution and the crash-window recovery. You
+supply the three transport calls it is built from:
+
+- `callSubmitRequest` / `call_submit_request` starts the run.
+- `callQueryStatus` / `call_query_status` returns a `RunStatus`, one of
+  `notStarted()`, `running()`, `completed()`, or `failed(error)`. It must be a
+  read-only probe with no side effects. Report an expected failure as a `FAILED`
+  status.
+- `callFetchResult` / `call_fetch_result` returns the `SubagentResult`. It must be
+  an idempotent read, because a replay may call it again. The status snapshot never
+  carries the payload, and a failed run is reported without fetching at all.
+
+{{< tabs "Async Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.async_subagent import BaseAsyncSubagentSetup, RunStatus
+
+# client(...) and task_id stand for your own transport client and run-id
+# scheme; neither is part of Flink Agents.
+
+
+class RemoteRunSetup(BaseAsyncSubagentSetup):
+    endpoint: str = ""
+
+    def call_submit_request(self, session_id: str, call_id: str, prompt: Any) -> None:
+        client(self.endpoint).submit(prompt, task_id=f"{session_id}#{call_id}")
+
+    def call_query_status(self, session_id: str, call_id: str) -> RunStatus:
+        probe = client(self.endpoint).status(task_id=f"{session_id}#{call_id}")
+        if probe is None:
+            return RunStatus.not_started()
+        if probe.succeeded:
+            return RunStatus.completed()
+        if probe.failed:
+            return RunStatus.failed(probe.error)
+        return RunStatus.running()
+
+    def call_fetch_result(self, session_id: str, call_id: str) -> SubagentResult:
+        return SubagentResult.ok(
+            client(self.endpoint).fetch(task_id=f"{session_id}#{call_id}")
+        )
+
+    # Optional, and a no-op by default, so a cancelled call leaves the remote run
+    # going. A replay may propagate the same cancellation again, so your remote has
+    # to accept it twice.
+    def call_cancel_request(self, session_id: str, call_id: str) -> None:
+        client(self.endpoint).cancel(task_id=f"{session_id}#{call_id}")
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseAsyncSubagentSetup;
+
+// client() and taskId(...) stand for your own transport client and run-id
+// scheme; neither is part of Flink Agents.
+public class RemoteRunSetup extends BaseAsyncSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteRunSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
 
+    @Override
+    protected void callSubmitRequest(String sessionId, String callId, Object prompt)
+            throws Exception {
+        client().submit(String.valueOf(prompt), taskId(sessionId, callId));
+    }
+
+    @Override
+    protected RunStatus callQueryStatus(String sessionId, String callId) {
+        // TaskStatus is your own transport type, not a framework type.
+        TaskStatus probe = client().status(taskId(sessionId, callId));
+        if (probe == null) {
+            return RunStatus.notStarted();
+        }
+        if (probe.succeeded()) {
+            return RunStatus.completed();
+        }
+        if (probe.failed()) {
+            return RunStatus.failed(probe.error());
+        }
+        return RunStatus.running();
+    }
+
+    @Override
+    protected SubagentResult callFetchResult(String sessionId, String callId) throws Exception {
+        return client().fetchResult(taskId(sessionId, callId));
+    }
+
+    // Optional, and a no-op by default, so a cancelled call leaves the remote run
+    // going. A replay may propagate the same cancellation again, so your remote has
+    // to accept it twice.
+    @Override
+    protected void callCancelRequest(String sessionId, String callId) {
+        client().cancel(taskId(sessionId, callId));
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup like any external sub-agent. The async form adds one descriptor
+argument, `status_poll_interval_millis`, and it defaults to 500.
+
+{{< tabs "Register an Async Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
 parent.add_resource(
-    "reviewer",
+    "remote_runner",
     ResourceType.AGENT,
-    InternalSubagentResourceDescriptorBuilder()
-    .agent(reviewer)
-    .description("Reviews a code diff and reports problems.")
-    # "diff" matches the {diff} placeholder of the child's prompt.
-    .input_schema(
-        '{"type":"object","properties":{"diff":{"type":"string"}},'
-        '"required":["diff"]}'
-    )
-    .build(),
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteRunSetup",
+        endpoint="https://example.com/runs",
+        description="Run a review task on the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
+        status_poll_interval_millis=500,
+    ),
 )
 ```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_runner",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteRunSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/runs")
+        .addInitialArgument("description", "Run a review task on the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .addInitialArgument("status_poll_interval_millis", 500L)
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_runner
+        clazz: my_app.agents.RemoteRunSetup
+        endpoint: https://example.com/runs
+        description: Run a review task on the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+        status_poll_interval_millis: 500
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The base class also recovers the crash window between "POST sent" and "POST durably
+recorded". On recovery it probes the run's status first, and it re-posts only when the
+probe reports that the run never started. Override `reconcileSubmitRequest` /
+`reconcile_submit_request` only if your service needs a different repair.
+
+## Invoke a sub-agent
+
+A sub-agent is invoked in one of two ways, model-driven or caller-driven, and the two
+reuse different existing abstractions.
+
+Model-driven delegation rides the tool abstraction. The sub-agent is offered to the
+model as a tool, so your action sends a `ChatRequestEvent` and the model answers with
+a call to the sub-agent. The result then returns to the model as the tool
+observation, until a final `ChatResponseEvent` reaches your action. This is the same
+cycle any tool call makes.
+
+Caller-driven delegation cannot ride that cycle, because a tool's result goes back
+into the chat loop rather than to the action that asked for it. An action that needs
+the delegated outcome therefore calls the sub-agent itself, with no model in the loop.
+
+Both ways end in the same call. Your action, or the framework on the model's behalf,
+runs `submit(ctx, prompt)` on the registered name. That call returns a
+`SubagentFuture`, which is the handle for that one call. Awaiting the handle yields
+the call's `SubagentResult`.
+
+### Delegate from a chat model
+
+List the registered name under `subagents` of the parent's chat model:
+
+{{< tabs "Declare Sub-agents on a Chat Model" >}}
+
+{{< tab "Python" >}}
+```python
+ResourceDescriptor(
+    clazz=ResourceName.ChatModel.OLLAMA_SETUP,
+    connection="ollama_connection",
+    model="qwen3:8b",
+    subagents=["reviewer"],
+)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_SETUP)
+    .addInitialArgument("connection", "ollamaConnection")
+    .addInitialArgument("model", "qwen3:8b")
+    .addInitialArgument("subagents", List.of("reviewer"))
+    .build();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The model sees `reviewer` as a tool, described by the registration's description and
+input schema. A schema is required for model-selected calls. A missing description
+falls back to a generic delegation description.
+
+The arguments the model sends become the sub-agent's input, and its outputs return to
+the model as the tool observation. The model can then keep reasoning over them. For
+an internal sub-agent, the observation is the JSON encoding of the full output list
+described in [the result](#the-result). That holds even when the child emits a single
+output, so the shape does not depend on how many outputs the child emits.
+
+An external setup class can state the shape of that observation. It declares
+`result_type()` in Python or `getResultType()` in Java, and the default reports the
+result as it arrived. Declare one to narrow a wider payload, or to convert a payload
+that is not already JSON-compatible. An internal sub-agent has no such declaration, so
+its observation is always the JSON encoding of its output list. This shapes the
+observation only. A caller that awaits the handle gets the result the sub-agent
+produced, as [the result](#the-result) describes.
+
+The external sub-agents `remote_reviewer` and `remote_runner` are offered the same
+way. Only their registration differs.
+
+### Call from an action
+
+An action can submit input to a registered sub-agent directly and await its
+outcome. This path needs no description and no input declaration, so it also works
+for a child registered as-is.
+
+{{< tabs "Call a Sub-agent From an Action" >}}
+
+{{< tab "Python" >}}
+```python
+class DelegatingAgent(Agent):
+    @action(EventType.InputEvent)
+    @staticmethod
+    async def delegate(event: Event, ctx: RunnerContext) -> None:
+        reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+        future = await reviewer.submit(ctx, "review the diff")
+        result = await future
+        if result.success:
+            ctx.send_event(OutputEvent(output=result.result[0]))
+        else:
+            ctx.send_event(OutputEvent(output=f"failed: {result.error_message}"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+public class DelegatingAgent extends Agent {
+
+    @Action(EventType.InputEvent)
+    public static void delegate(Event event, RunnerContext ctx) throws Exception {
+        SubagentSetup reviewer =
+                (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+        SubagentResult result = reviewer.submit(ctx, "review the diff").await();
+        if (result.isSuccess()) {
+            String answer = String.valueOf(((List<?>) result.getResult()).get(0));
+            ctx.sendEvent(new OutputEvent(answer));
+        } else {
+            ctx.sendEvent(new OutputEvent("failed: " + result.getErrorMessage()));
+        }
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+What you pass to `submit` is what the sub-agent receives. An internal child gets it
+as its `InputEvent` payload, and a model-driven call passes the arguments map
+instead. What comes back is a `SubagentResult`. For the internal sub-agent used here,
+its `result` is the list of `OutputEvent` payloads the child emitted. That is why the
+example reads `result.result[0]`. An external sub-agent returns whatever its setup
+produces.
+
+In Python `submit` is a coroutine, so the call is awaited in two phases and the
+action must be `async`. In Java `submit` returns the handle directly and `await()`
+yields the result. The same two calls drive `remote_reviewer` and `remote_runner`
+unchanged. Only the registration differs.
+
+## Work with open handles
+
+A `SubagentFuture` is live from `submit` until it is awaited or cancelled.
+
+It works like the `AsyncFuture` that
+[async execution]({{< ref "docs/development/create-agent/actions#async-execution" >}})
+returns. You get it from one call, and you await it to get the outcome. It is its own
+type rather than an `AsyncFuture`, because it always resolves to a `SubagentResult`,
+it carries the call's `(session_id, call_id)`, and it owns its durable execution
+inside the setup.
+
+### Fan out to several sub-agents
+
+`combine` batches handles into a `SubagentFutures`, and it works the way `gather`
+works for ordinary async futures. `gather` does not accept a `SubagentFuture`, so
+handles batch this way instead. Every handle in the batch is prepared before any is
+executed, so deferred requests all go out up front and remote runs overlap. Results
+come back in the order the handles were added.
+
+{{< tabs "Fan Out to Several Sub-agents" >}}
+
+{{< tab "Python" >}}
+```python
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+first = await reviewer.submit(ctx, "review the diff")
+second = await reviewer.submit(ctx, "review the tests")
+
+outcomes = await first.combine(second)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture first = reviewer.submit(ctx, "review the diff");
+SubagentFuture second = reviewer.submit(ctx, "review the tests");
+
+List<SubagentResult> outcomes = first.combine(second).awaitAll();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+### Continue a session
+
+Pass the `session_id` of an earlier handle to continue that conversation. The new
+call gets a fresh `call_id` in the same session. Whether a conversation can be
+continued across actions is up to the implementation.
+
+{{< tabs "Continue a Session" >}}
+
+{{< tab "Python" >}}
+```python
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+first = await reviewer.submit(ctx, "review the diff")
+first_result = await first
+
+second = await reviewer.submit(ctx, "now the tests", first.session_id)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture first = reviewer.submit(ctx, "review the diff");
+SubagentResult firstResult = first.await();
+
+SubagentFuture second = reviewer.submit(ctx, "now the tests", first.getSessionId());
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+### Cancel and resolve
+
+Resolve every handle you submit. An action that finishes with a handle it neither
+awaited nor cancelled fails with `Action <name> finished without resolving the
+sub-agent calls it submitted: <ids>`. Cancelling counts as resolving, and awaiting a
+cancelled handle throws `CancellationException` in Java and raises `CancelledError` in
+Python.
+
+{{< tabs "Cancel a Sub-agent Call" >}}
+
+{{< tab "Python" >}}
+```python
+from concurrent.futures import CancelledError
+
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+future = await reviewer.submit(ctx, "review the diff")
+future.cancel()
+
+try:
+    await future
+except CancelledError:
+    ctx.send_event(OutputEvent(output="cancelled"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture future = reviewer.submit(ctx, "review the diff");
+future.cancel();
+
+try {
+    future.await();
+} catch (CancellationException e) {
+    ctx.sendEvent(new OutputEvent("cancelled"));
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+`cancel()` on a deferred handle takes effect only before the request is issued.
+
+## The outcome of a call
+
+### The result
+
+The awaited value of every call is a `SubagentResult`, which carries the outcome in
+three fields:
+
+| Field | Java | Carries |
+|---|---|---|
+| `success` | `isSuccess()` | Whether the call succeeded |
+| `result` | `getResult()` | The answer on success. For an internal sub-agent, the list of `OutputEvent` payloads in emission order. For an external one, whatever the setup produces, such as a string or an object |
+| `error_message` | `getErrorMessage()` | The reason on failure |
+
+A result is persisted and re-bound after a failover, so it must be JSON-compatible.

Review Comment:
   Fixed. Replay is split per language, and the JSON rule is scoped to model-facing results and tied to normalization rather than to persistence.



##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -307,30 +443,854 @@ parent.addResource(
                                 + "\"required\":[\"diff\"]}")
                 .build());
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The framework does not check the schema against the prompt. A field the prompt does
+not use is ignored, and a placeholder no field fills is left in the prompt as literal
+text. So keep the two in step when you change either.
+
+### External: write a setup class
+
+An external sub-agent has no plan of its own, and it is not compiled into the
+parent's. The work happens in your own code or in a remote agent service. The parent
+only submits to it and waits for the outcome. The framework cannot perform that work
+for you, because it does not know your protocol. So you write the part that performs
+one invocation, and the framework supplies everything around it. It runs your code
+under durable execution, and after a failover it replays the recorded result instead
+of calling your code again.
+
+What you write is a setup class. Its only job is to carry out one invocation, so it
+declares no actions and no events of its own. It extends a framework base class and
+implements the methods that class declares.
+
+Registration names the setup class in a descriptor, and both languages construct the
+setup from the descriptor at runtime. Registering a setup instance instead is
+rejected.
+
+The invocation itself is carried out in one of two modes, and which one you write
+follows from your remote interface. When one request returns the result, write a
+*deferred* setup. You own the whole interaction, and the framework only holds it back
+until the caller asks for the result. When the remote instead hands back a run that
+you later query for status and result, write an *async (pub/sub)* setup. The base
+class owns that composition of publishing, polling, and fetching, and you supply the
+transport calls it is built from.
+
+#### Deferred execution
+
+A deferred setup sends nothing when the caller submits. It only registers the
+invocation. The request goes out when the caller first awaits the result. So a call
+that is cancelled instead never issues its request.
+
+What you write is the interaction itself, in the `prepare` method of your setup class.
+`prepare` runs once per invocation and returns the durable call that carries it. The
+example is one blocking request, and whatever else your call does is yours to decide,
+because the framework does not know your protocol.
 
+The framework assigns `session_id` and `call_id` before it calls `prepare`, and they
+are the only values that a replay reproduces. The durable id you build from them is
+the key that recovery matches a recorded result against. When the key matches, your
+call is not run again. So build the key from those two values alone. Anything else in
+it, a timestamp for instance, makes a replay look up a different key and run the work
+a second time. Passing the same values to your own remote lets it recognize a
+re-issued request as the same one.
+
+The reconciler is optional. Omitting it leaves one crash window uncovered, and
+[Durability and failover](#durability-and-failover) describes that window. Supply one
+when the work cannot be repeated.
+
+{{< tabs "Supply a Reconciler" >}}
+
+{{< tab "Python" >}}
 ```python
-reviewer = ReActAgent(
-    chat_model=chat_model_descriptor,
-    prompt=Prompt.from_text(
-        "Review the following code diff and report any problems:\n\n{diff}"
+def prepare(
+    self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+) -> PreparedTriple:
+    key = f"{session_id}#{call_id}"
+
+    def call() -> SubagentResult:
+        return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+
+    # Consulted only during recovery, when this call is revisited and no result was
+    # recorded for it. Return the recovered outcome, or raise the recovered failure.
+    # It takes no arguments.
+    def reconcile() -> SubagentResult:
+        # fetch_review_by_key is your own lookup; not part of Flink Agents.
+        found = fetch_review_by_key(self.endpoint, key)
+        if found is None:
+            raise RuntimeError("the review service has no record of this call")
+        return SubagentResult.ok(found)
+
+    return (key, call, reconcile)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+// Inside the DurableCallable that prepare returns, alongside getId() and call().
+
+// Consulted only during recovery, when this call is revisited and no result was
+// recorded for it. Return the recovered outcome, or throw the recovered failure.
+@Override
+public Callable<SubagentResult> reconciler() {
+    return () -> {
+        // fetchReviewByKey is your own lookup; not part of Flink Agents.
+        String found = fetchReviewByKey(endpoint, key);
+        if (found == null) {
+            throw new IllegalStateException("the review service has no record of this call");
+        }
+        return SubagentResult.ok(found);
+    };
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+{{< tabs "Deferred Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.runner_context import RunnerContext
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.deferred_subagent import (
+    DeferredSubagentSetup,
+    PreparedTriple,
+)
+
+
+def post_review(endpoint: str, prompt: Any, key: str) -> str:
+    """Your own call to the review service; not part of Flink Agents.
+
+    Keying the work lets the service treat a re-issued request as the same one.
+    """
+    ...
+
+
+class RemoteReviewSetup(DeferredSubagentSetup):
+    endpoint: str = ""
+
+    def prepare(
+        self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+    ) -> PreparedTriple:
+        key = f"{session_id}#{call_id}"
+
+        def call() -> SubagentResult:
+            try:
+                return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+            except Exception as e:
+                # An expected failure is a value; only a system fault should escape.
+                return SubagentResult.error(e)
+
+        # The first value is the key recovery matches a recorded result against, so
+        # it is built from the two ids alone. The third is the reconciler.
+        return (key, call, None)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.context.DurableCallable;
+import org.apache.flink.agents.api.context.RunnerContext;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseDeferredSubagentSetup;
+
+/**
+ * Your own call to the review service; not part of Flink Agents. Keying the work lets
+ * the service treat a re-issued request as the same one.
+ */
+static String postReview(String endpoint, Object prompt, String key) {
+    ...
+}
+
+public class RemoteReviewSetup extends BaseDeferredSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteReviewSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected DurableCallable<SubagentResult> prepare(
+            RunnerContext ctx, Object prompt, String sessionId, String callId) {
+        // Built from the two ids alone, so a replay reproduces it.
+        String key = sessionId + "#" + callId;
+        return new DurableCallable<SubagentResult>() {
+            @Override
+            public String getId() {
+                // The key recovery matches a recorded result against.
+                return key;
+            }
+
+            @Override
+            public Class<SubagentResult> getResultClass() {
+                return SubagentResult.class;
+            }
+
+            @Override
+            public SubagentResult call() {
+                try {
+                    return SubagentResult.ok(postReview(endpoint, prompt, key));
+                } catch (Exception e) {
+                    // An expected failure is a value; only a system fault should escape.
+                    return SubagentResult.error(e);
+                }
+            }
+        };
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup under the name invocations will use. Every descriptor argument
+beyond the built-in ones is configuration for the setup to read back. In Python it
+reads them as model fields, and in Java through the public
+`(ResourceDescriptor, ResourceContext)` constructor. The `endpoint` above is such
+configuration, not a built-in transport.
+
+{{< tabs "Register an External Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
+parent.add_resource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteReviewSetup",
+        endpoint="https://example.com/review",
+        description="Review code using the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
     ),
 )
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteReviewSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/review")
+        .addInitialArgument("description", "Review code using the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_reviewer
+        clazz: my_app.agents.RemoteReviewSetup
+        endpoint: https://example.com/review
+        description: Review code using the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The YAML form of that registration is the `subagents` list of an agent entry, as in
+the YAML tab above. That list can also sit at the shared document level, outside
+`agents:`, so that several agents in one file reuse it. A chat model setup declared in
+the same file also takes a `subagents` argument, but it is a list of registered names
+rather than a list of entries. See
+[delegate from a chat model](#delegate-from-a-chat-model).
+
+Two constraints apply to the entry:
+
+- No class alias is defined for `AGENT` resources, so `clazz` is always a
+  fully-qualified class path.
+- The entry's language must match the loader. A Python YAML file names a Python
+  class and may not set `type: java`. A Java YAML file must set `type: java`.
+
+#### Async (pub/sub) execution
+
+An async sub-agent talks to a service that runs the task for you. The service accepts
+the work and answers at once, and you query it later for the run's status and
+result.
+
+An async setup publishes the run when the caller submits, through one durable POST.
+Waiting for the result then polls the run's status until it reaches a terminal state,
+and fetches it.
+
+That is the difference from the deferred mode, which sends nothing when the caller
+submits. The mode's name is the framework's own. The POST is the publish, and the
+polling is the subscribe.
+
+The submit-poll-fetch composition is the same for every such remote, so the base
+class owns it, along with the durable execution and the crash-window recovery. You
+supply the three transport calls it is built from:
+
+- `callSubmitRequest` / `call_submit_request` starts the run.
+- `callQueryStatus` / `call_query_status` returns a `RunStatus`, one of
+  `notStarted()`, `running()`, `completed()`, or `failed(error)`. It must be a
+  read-only probe with no side effects. Report an expected failure as a `FAILED`
+  status.
+- `callFetchResult` / `call_fetch_result` returns the `SubagentResult`. It must be
+  an idempotent read, because a replay may call it again. The status snapshot never
+  carries the payload, and a failed run is reported without fetching at all.
+
+{{< tabs "Async Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.async_subagent import BaseAsyncSubagentSetup, RunStatus
+
+# client(...) and task_id stand for your own transport client and run-id
+# scheme; neither is part of Flink Agents.
+
+
+class RemoteRunSetup(BaseAsyncSubagentSetup):
+    endpoint: str = ""
+
+    def call_submit_request(self, session_id: str, call_id: str, prompt: Any) -> None:
+        client(self.endpoint).submit(prompt, task_id=f"{session_id}#{call_id}")
+
+    def call_query_status(self, session_id: str, call_id: str) -> RunStatus:
+        probe = client(self.endpoint).status(task_id=f"{session_id}#{call_id}")
+        if probe is None:
+            return RunStatus.not_started()
+        if probe.succeeded:
+            return RunStatus.completed()
+        if probe.failed:
+            return RunStatus.failed(probe.error)
+        return RunStatus.running()
+
+    def call_fetch_result(self, session_id: str, call_id: str) -> SubagentResult:
+        return SubagentResult.ok(
+            client(self.endpoint).fetch(task_id=f"{session_id}#{call_id}")
+        )
+
+    # Optional, and a no-op by default, so a cancelled call leaves the remote run
+    # going. A replay may propagate the same cancellation again, so your remote has
+    # to accept it twice.
+    def call_cancel_request(self, session_id: str, call_id: str) -> None:
+        client(self.endpoint).cancel(task_id=f"{session_id}#{call_id}")
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseAsyncSubagentSetup;
+
+// client() and taskId(...) stand for your own transport client and run-id
+// scheme; neither is part of Flink Agents.
+public class RemoteRunSetup extends BaseAsyncSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteRunSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
 
+    @Override
+    protected void callSubmitRequest(String sessionId, String callId, Object prompt)
+            throws Exception {
+        client().submit(String.valueOf(prompt), taskId(sessionId, callId));
+    }
+
+    @Override
+    protected RunStatus callQueryStatus(String sessionId, String callId) {
+        // TaskStatus is your own transport type, not a framework type.
+        TaskStatus probe = client().status(taskId(sessionId, callId));
+        if (probe == null) {
+            return RunStatus.notStarted();
+        }
+        if (probe.succeeded()) {
+            return RunStatus.completed();
+        }
+        if (probe.failed()) {
+            return RunStatus.failed(probe.error());
+        }
+        return RunStatus.running();
+    }
+
+    @Override
+    protected SubagentResult callFetchResult(String sessionId, String callId) throws Exception {
+        return client().fetchResult(taskId(sessionId, callId));
+    }
+
+    // Optional, and a no-op by default, so a cancelled call leaves the remote run
+    // going. A replay may propagate the same cancellation again, so your remote has
+    // to accept it twice.
+    @Override
+    protected void callCancelRequest(String sessionId, String callId) {
+        client().cancel(taskId(sessionId, callId));
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup like any external sub-agent. The async form adds one descriptor
+argument, `status_poll_interval_millis`, and it defaults to 500.
+
+{{< tabs "Register an Async Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
 parent.add_resource(
-    "reviewer",
+    "remote_runner",
     ResourceType.AGENT,
-    InternalSubagentResourceDescriptorBuilder()
-    .agent(reviewer)
-    .description("Reviews a code diff and reports problems.")
-    # "diff" matches the {diff} placeholder of the child's prompt.
-    .input_schema(
-        '{"type":"object","properties":{"diff":{"type":"string"}},'
-        '"required":["diff"]}'
-    )
-    .build(),
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteRunSetup",
+        endpoint="https://example.com/runs",
+        description="Run a review task on the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
+        status_poll_interval_millis=500,
+    ),
 )
 ```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_runner",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteRunSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/runs")
+        .addInitialArgument("description", "Run a review task on the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .addInitialArgument("status_poll_interval_millis", 500L)
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_runner
+        clazz: my_app.agents.RemoteRunSetup
+        endpoint: https://example.com/runs
+        description: Run a review task on the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+        status_poll_interval_millis: 500
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The base class also recovers the crash window between "POST sent" and "POST durably
+recorded". On recovery it probes the run's status first, and it re-posts only when the
+probe reports that the run never started. Override `reconcileSubmitRequest` /
+`reconcile_submit_request` only if your service needs a different repair.
+
+## Invoke a sub-agent
+
+A sub-agent is invoked in one of two ways, model-driven or caller-driven, and the two
+reuse different existing abstractions.
+
+Model-driven delegation rides the tool abstraction. The sub-agent is offered to the
+model as a tool, so your action sends a `ChatRequestEvent` and the model answers with
+a call to the sub-agent. The result then returns to the model as the tool
+observation, until a final `ChatResponseEvent` reaches your action. This is the same
+cycle any tool call makes.
+
+Caller-driven delegation cannot ride that cycle, because a tool's result goes back
+into the chat loop rather than to the action that asked for it. An action that needs
+the delegated outcome therefore calls the sub-agent itself, with no model in the loop.
+
+Both ways end in the same call. Your action, or the framework on the model's behalf,
+runs `submit(ctx, prompt)` on the registered name. That call returns a
+`SubagentFuture`, which is the handle for that one call. Awaiting the handle yields
+the call's `SubagentResult`.
+
+### Delegate from a chat model
+
+List the registered name under `subagents` of the parent's chat model:
+
+{{< tabs "Declare Sub-agents on a Chat Model" >}}
+
+{{< tab "Python" >}}
+```python
+ResourceDescriptor(
+    clazz=ResourceName.ChatModel.OLLAMA_SETUP,
+    connection="ollama_connection",
+    model="qwen3:8b",
+    subagents=["reviewer"],
+)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_SETUP)
+    .addInitialArgument("connection", "ollamaConnection")
+    .addInitialArgument("model", "qwen3:8b")
+    .addInitialArgument("subagents", List.of("reviewer"))
+    .build();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The model sees `reviewer` as a tool, described by the registration's description and
+input schema. A schema is required for model-selected calls. A missing description
+falls back to a generic delegation description.
+
+The arguments the model sends become the sub-agent's input, and its outputs return to
+the model as the tool observation. The model can then keep reasoning over them. For
+an internal sub-agent, the observation is the JSON encoding of the full output list
+described in [the result](#the-result). That holds even when the child emits a single
+output, so the shape does not depend on how many outputs the child emits.
+
+An external setup class can state the shape of that observation. It declares
+`result_type()` in Python or `getResultType()` in Java, and the default reports the
+result as it arrived. Declare one to narrow a wider payload, or to convert a payload
+that is not already JSON-compatible. An internal sub-agent has no such declaration, so
+its observation is always the JSON encoding of its output list. This shapes the
+observation only. A caller that awaits the handle gets the result the sub-agent
+produced, as [the result](#the-result) describes.
+
+The external sub-agents `remote_reviewer` and `remote_runner` are offered the same
+way. Only their registration differs.
+
+### Call from an action
+
+An action can submit input to a registered sub-agent directly and await its
+outcome. This path needs no description and no input declaration, so it also works
+for a child registered as-is.
+
+{{< tabs "Call a Sub-agent From an Action" >}}
+
+{{< tab "Python" >}}
+```python
+class DelegatingAgent(Agent):
+    @action(EventType.InputEvent)
+    @staticmethod
+    async def delegate(event: Event, ctx: RunnerContext) -> None:
+        reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+        future = await reviewer.submit(ctx, "review the diff")
+        result = await future
+        if result.success:
+            ctx.send_event(OutputEvent(output=result.result[0]))
+        else:
+            ctx.send_event(OutputEvent(output=f"failed: {result.error_message}"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+public class DelegatingAgent extends Agent {
+
+    @Action(EventType.InputEvent)
+    public static void delegate(Event event, RunnerContext ctx) throws Exception {
+        SubagentSetup reviewer =
+                (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+        SubagentResult result = reviewer.submit(ctx, "review the diff").await();
+        if (result.isSuccess()) {
+            String answer = String.valueOf(((List<?>) result.getResult()).get(0));
+            ctx.sendEvent(new OutputEvent(answer));
+        } else {
+            ctx.sendEvent(new OutputEvent("failed: " + result.getErrorMessage()));
+        }
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+What you pass to `submit` is what the sub-agent receives. An internal child gets it
+as its `InputEvent` payload, and a model-driven call passes the arguments map
+instead. What comes back is a `SubagentResult`. For the internal sub-agent used here,
+its `result` is the list of `OutputEvent` payloads the child emitted. That is why the
+example reads `result.result[0]`. An external sub-agent returns whatever its setup
+produces.
+
+In Python `submit` is a coroutine, so the call is awaited in two phases and the
+action must be `async`. In Java `submit` returns the handle directly and `await()`
+yields the result. The same two calls drive `remote_reviewer` and `remote_runner`
+unchanged. Only the registration differs.
+
+## Work with open handles
+
+A `SubagentFuture` is live from `submit` until it is awaited or cancelled.
+
+It works like the `AsyncFuture` that
+[async execution]({{< ref "docs/development/create-agent/actions#async-execution" >}})
+returns. You get it from one call, and you await it to get the outcome. It is its own
+type rather than an `AsyncFuture`, because it always resolves to a `SubagentResult`,
+it carries the call's `(session_id, call_id)`, and it owns its durable execution
+inside the setup.
+
+### Fan out to several sub-agents
+
+`combine` batches handles into a `SubagentFutures`, and it works the way `gather`
+works for ordinary async futures. `gather` does not accept a `SubagentFuture`, so
+handles batch this way instead. Every handle in the batch is prepared before any is
+executed, so deferred requests all go out up front and remote runs overlap. Results
+come back in the order the handles were added.
+
+{{< tabs "Fan Out to Several Sub-agents" >}}
+
+{{< tab "Python" >}}
+```python
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+first = await reviewer.submit(ctx, "review the diff")
+second = await reviewer.submit(ctx, "review the tests")
+
+outcomes = await first.combine(second)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture first = reviewer.submit(ctx, "review the diff");
+SubagentFuture second = reviewer.submit(ctx, "review the tests");
+
+List<SubagentResult> outcomes = first.combine(second).awaitAll();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+### Continue a session
+
+Pass the `session_id` of an earlier handle to continue that conversation. The new
+call gets a fresh `call_id` in the same session. Whether a conversation can be
+continued across actions is up to the implementation.
+
+{{< tabs "Continue a Session" >}}
+
+{{< tab "Python" >}}
+```python
+reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+
+first = await reviewer.submit(ctx, "review the diff")
+first_result = await first
+
+second = await reviewer.submit(ctx, "now the tests", first.session_id)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+SubagentSetup reviewer = (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+
+SubagentFuture first = reviewer.submit(ctx, "review the diff");
+SubagentResult firstResult = first.await();
+
+SubagentFuture second = reviewer.submit(ctx, "now the tests", first.getSessionId());

Review Comment:
   Fixed in both tabs. Swept the other examples: fan-out resolves through combine, cancel through cancel().



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [docs] Improve the Sub-Agents guide [flink-agents]

Posted by "yunfengzhou-hub (via GitHub)" <gi...@apache.org>.
yunfengzhou-hub commented on code in PR #1268:
URL: https://github.com/apache/flink-agents/pull/1268#discussion_r4309928701


##########
docs/content/docs/development/multi-agent/subagents.md:
##########
@@ -312,30 +412,854 @@ parent.addResource(
                                 + "\"required\":[\"diff\"]}")
                 .build());
 ```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The framework does not check the schema against the prompt. A field the prompt does
+not use is ignored, and a placeholder no field fills is left in the prompt as literal
+text. So keep the two in step when you change either.
 
+### External: write a setup class {#declare-an-external-sub-agent}
+
+An external sub-agent has no plan of its own, and it is not compiled into the
+parent's. The work happens in your own code or in a remote agent service. The parent
+only submits to it and waits for the outcome. The framework cannot perform that work
+for you, because it does not know your protocol. So you write the part that performs
+one invocation, and the framework supplies everything around it. It runs your code
+under durable execution, and after a failover it replays the recorded result instead
+of calling your code again.
+
+What you write is a setup class. Its only job is to carry out one invocation, so it
+declares no actions and no events of its own. It extends a framework base class and
+implements the methods that class declares.
+
+Registration names the setup class in a descriptor, and both languages construct the
+setup from the descriptor at runtime. Registering a setup instance instead is
+rejected.
+
+The invocation itself is carried out in one of two modes, *deferred* and *async
+(pub/sub)*. Which one you write follows from your remote interface.
+
+| | Deferred | Async (pub/sub) |
+|---|---|---|
+| Work starts | When the caller first awaits the result | When the caller submits |
+| You implement | `prepare` | `call_submit_request`, `call_query_status`, `call_fetch_result` |
+| Choose it when | One request returns the result | The remote hands back a run you query later |
+
+#### Deferred execution
+
+A deferred setup sends nothing when the caller submits. It only registers the
+invocation. The request goes out when the caller first awaits the result. So a call
+that is cancelled instead never issues its request.
+
+What you write is the interaction itself, in the `prepare` method of your setup class.
+`prepare` runs once per invocation and returns the durable call that carries it. The
+example is one blocking request, and whatever else your call does is yours to decide,
+because the framework does not know your protocol.
+
+The framework assigns `session_id` and `call_id` before it calls `prepare`, and they
+are the only values that a replay reproduces. The durable id you build from them is
+the key that recovery matches a recorded result against. When the key matches, your
+call is not run again. So build the key from those two values alone. Anything else in
+it, a timestamp for instance, makes a replay look up a different key and run the work
+a second time. Passing the same values to your own remote lets it recognize a
+re-issued request as the same one.
+
+The reconciler is optional. Omitting it leaves one crash window uncovered, and
+[Durability and failover](#durability-and-failover) describes that window. Supply one
+when the work cannot be repeated.
+
+{{< tabs "Supply a Reconciler" >}}
+
+{{< tab "Python" >}}
 ```python
-reviewer = ReActAgent(
-    chat_model=chat_model_descriptor,
-    prompt=Prompt.from_text(
-        "Review the following code diff and report any problems:\n\n{diff}"
+def prepare(
+    self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+) -> PreparedTriple:
+    key = f"{session_id}#{call_id}"
+
+    def call() -> SubagentResult:
+        return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+
+    # Consulted only during recovery, when this call is revisited and no result was
+    # recorded for it. It takes no arguments. Return the recovered outcome, because
+    # an exception here is a system-level failure that fails the action instead of
+    # reaching the caller.
+    def reconcile() -> SubagentResult:
+        # fetch_review_by_key is your own lookup; not part of Flink Agents.
+        found = fetch_review_by_key(self.endpoint, key)
+        if found is None:
+            return SubagentResult.error("the review service has no record of this call")
+        return SubagentResult.ok(found)
+
+    return (key, call, reconcile)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+// Inside the DurableCallable that prepare returns, alongside getId() and call().
+
+// Consulted only during recovery, when this call is revisited and no result was
+// recorded for it. Return the recovered outcome, because an exception here is a
+// system-level failure that fails the action instead of reaching the caller.
+@Override
+public Callable<SubagentResult> reconciler() {
+    return () -> {
+        // fetchReviewByKey is your own lookup; not part of Flink Agents.
+        String found = fetchReviewByKey(endpoint, key);
+        if (found == null) {
+            return SubagentResult.error("the review service has no record of this call");
+        }
+        return SubagentResult.ok(found);
+    };
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+{{< tabs "Deferred Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.runner_context import RunnerContext
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.deferred_subagent import (
+    DeferredSubagentSetup,
+    PreparedTriple,
+)
+
+
+def post_review(endpoint: str, prompt: Any, key: str) -> str:
+    """Your own call to the review service; not part of Flink Agents.
+
+    Keying the work lets the service treat a re-issued request as the same one.
+    """
+    ...
+
+
+class RemoteReviewSetup(DeferredSubagentSetup):
+    endpoint: str = ""
+
+    def prepare(
+        self, ctx: RunnerContext, prompt: Any, session_id: str, call_id: str
+    ) -> PreparedTriple:
+        key = f"{session_id}#{call_id}"
+
+        def call() -> SubagentResult:
+            try:
+                return SubagentResult.ok(post_review(self.endpoint, prompt, key))
+            except Exception as e:
+                # An expected failure is a value; only a system fault should escape.
+                return SubagentResult.error(e)
+
+        # The first value is the key recovery matches a recorded result against, so
+        # it is built from the two ids alone. The third is the reconciler.
+        return (key, call, None)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.context.DurableCallable;
+import org.apache.flink.agents.api.context.RunnerContext;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseDeferredSubagentSetup;
+
+/**
+ * Your own call to the review service; not part of Flink Agents. Keying the work lets
+ * the service treat a re-issued request as the same one.
+ */
+static String postReview(String endpoint, Object prompt, String key) {
+    ...
+}
+
+public class RemoteReviewSetup extends BaseDeferredSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteReviewSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected DurableCallable<SubagentResult> prepare(
+            RunnerContext ctx, Object prompt, String sessionId, String callId) {
+        // Built from the two ids alone, so a replay reproduces it.
+        String key = sessionId + "#" + callId;
+        return new DurableCallable<SubagentResult>() {
+            @Override
+            public String getId() {
+                // The key recovery matches a recorded result against.
+                return key;
+            }
+
+            @Override
+            public Class<SubagentResult> getResultClass() {
+                return SubagentResult.class;
+            }
+
+            @Override
+            public SubagentResult call() {
+                try {
+                    return SubagentResult.ok(postReview(endpoint, prompt, key));
+                } catch (Exception e) {
+                    // An expected failure is a value; only a system fault should escape.
+                    return SubagentResult.error(e);
+                }
+            }
+        };
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup under the name invocations will use. Every descriptor argument
+beyond the built-in ones is configuration for the setup to read back. In Python it
+reads them as model fields, and in Java through the public
+`(ResourceDescriptor, ResourceContext)` constructor. The `endpoint` above is such
+configuration, not a built-in transport.
+
+{{< tabs "Register an External Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
+parent.add_resource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteReviewSetup",
+        endpoint="https://example.com/review",
+        description="Review code using the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
     ),
 )
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_reviewer",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteReviewSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/review")
+        .addInitialArgument("description", "Review code using the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_reviewer
+        clazz: my_app.agents.RemoteReviewSetup
+        endpoint: https://example.com/review
+        description: Review code using the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The YAML form of that registration is the `subagents` list of an agent entry, as in
+the YAML tab above. That list can also sit at the shared document level, outside
+`agents:`, so that several agents in one file reuse it. A chat model setup declared in
+the same file also takes a `subagents` argument, but it is a list of registered names
+rather than a list of entries. See
+[delegate from a chat model](#delegate-from-a-chat-model).
+
+Two constraints apply to the entry:
 
+- No class alias is defined for `AGENT` resources, so `clazz` is always a
+  fully-qualified class path.
+- The entry's language must match the loader. A Python YAML file names a Python
+  class and may not set `type: java`. A Java YAML file must set `type: java`.
+
+#### Async (pub/sub) execution
+
+An async sub-agent talks to a service that runs the task for you. The service accepts
+the work and answers at once, and you query it later for the run's status and
+result.
+
+An async setup starts the remote run when the caller submits, through one durable
+POST. Waiting for the result then polls the run's status until it reaches a terminal
+state, and fetches it.
+
+The base class owns that submit-poll-fetch composition, along with the durable
+execution and the crash-window recovery. You supply the three transport calls it is
+built from:
+
+- `callSubmitRequest` / `call_submit_request` starts the run.
+- `callQueryStatus` / `call_query_status` returns a `RunStatus`, one of
+  `notStarted()`, `running()`, `completed()`, or `failed(error)`. It must be a
+  read-only probe with no side effects. Report an expected failure as a `FAILED`
+  status.
+- `callFetchResult` / `call_fetch_result` returns the `SubagentResult`. It must be
+  an idempotent read, because a replay may call it again. The status snapshot never
+  carries the payload, and a failed run is reported without fetching at all.
+
+{{< tabs "Async Sub-agent Setup" >}}
+
+{{< tab "Python" >}}
+```python
+from typing import Any
+
+from flink_agents.api.subagent import SubagentResult
+from flink_agents.runtime.async_subagent import BaseAsyncSubagentSetup, RunStatus
+
+# client(...) and task_id stand for your own transport client and run-id
+# scheme; neither is part of Flink Agents.
+
+
+class RemoteRunSetup(BaseAsyncSubagentSetup):
+    endpoint: str = ""
+
+    def call_submit_request(self, session_id: str, call_id: str, prompt: Any) -> None:
+        client(self.endpoint).submit(prompt, task_id=f"{session_id}#{call_id}")
+
+    def call_query_status(self, session_id: str, call_id: str) -> RunStatus:
+        probe = client(self.endpoint).status(task_id=f"{session_id}#{call_id}")
+        if probe is None:
+            return RunStatus.not_started()
+        if probe.succeeded:
+            return RunStatus.completed()
+        if probe.failed:
+            return RunStatus.failed(probe.error)
+        return RunStatus.running()
+
+    def call_fetch_result(self, session_id: str, call_id: str) -> SubagentResult:
+        return SubagentResult.ok(
+            client(self.endpoint).fetch(task_id=f"{session_id}#{call_id}")
+        )
+
+    # Optional, and a no-op by default, so a cancelled call leaves the remote run
+    # going. A replay may propagate the same cancellation again, so your remote has
+    # to accept it twice.
+    def call_cancel_request(self, session_id: str, call_id: str) -> None:
+        client(self.endpoint).cancel(task_id=f"{session_id}#{call_id}")
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.subagent.SubagentResult;
+import org.apache.flink.agents.runtime.subagent.BaseAsyncSubagentSetup;
+
+// client() and taskId(...) stand for your own transport client and run-id
+// scheme; neither is part of Flink Agents.
+public class RemoteRunSetup extends BaseAsyncSubagentSetup {
+
+    private final String endpoint;
+
+    public RemoteRunSetup(ResourceDescriptor descriptor, ResourceContext resourceContext) {
+        super(descriptor, resourceContext);
+        this.endpoint = descriptor.getArgument("endpoint");
+    }
+
+    @Override
+    protected void callSubmitRequest(String sessionId, String callId, Object prompt)
+            throws Exception {
+        client().submit(String.valueOf(prompt), taskId(sessionId, callId));
+    }
+
+    @Override
+    protected RunStatus callQueryStatus(String sessionId, String callId) {
+        // TaskStatus is your own transport type, not a framework type.
+        TaskStatus probe = client().status(taskId(sessionId, callId));
+        if (probe == null) {
+            return RunStatus.notStarted();
+        }
+        if (probe.succeeded()) {
+            return RunStatus.completed();
+        }
+        if (probe.failed()) {
+            return RunStatus.failed(probe.error());
+        }
+        return RunStatus.running();
+    }
+
+    @Override
+    protected SubagentResult callFetchResult(String sessionId, String callId) throws Exception {
+        return client().fetchResult(taskId(sessionId, callId));
+    }
+
+    // Optional, and a no-op by default, so a cancelled call leaves the remote run
+    // going. A replay may propagate the same cancellation again, so your remote has
+    // to accept it twice.
+    @Override
+    protected void callCancelRequest(String sessionId, String callId) {
+        client().cancel(taskId(sessionId, callId));
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+Register the setup like any external sub-agent. The async form adds one descriptor
+argument, `status_poll_interval_millis`, and it defaults to 500.
+
+{{< tabs "Register an Async Sub-agent" >}}
+
+{{< tab "Python" >}}
+```python
 parent.add_resource(
-    "reviewer",
+    "remote_runner",
     ResourceType.AGENT,
-    InternalSubagentResourceDescriptorBuilder()
-    .agent(reviewer)
-    .description("Reviews a code diff and reports problems.")
-    # "diff" matches the {diff} placeholder of the child's prompt.
-    .input_schema(
-        '{"type":"object","properties":{"diff":{"type":"string"}},'
-        '"required":["diff"]}'
-    )
-    .build(),
+    ResourceDescriptor(
+        clazz="my_app.agents.RemoteRunSetup",
+        endpoint="https://example.com/runs",
+        description="Run a review task on the remote service",
+        input_schema=(
+            '{"type":"object","properties":{"diff":{"type":"string"}},'
+            '"required":["diff"]}'
+        ),
+        status_poll_interval_millis=500,
+    ),
 )
 ```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+parent.addResource(
+    "remote_runner",
+    ResourceType.AGENT,
+    ResourceDescriptor.Builder.newBuilder(RemoteRunSetup.class.getName())
+        .addInitialArgument("endpoint", "https://example.com/runs")
+        .addInitialArgument("description", "Run a review task on the remote service")
+        .addInitialArgument(
+                "input_schema",
+                "{\"type\":\"object\",\"properties\":{\"diff\":{\"type\":\"string\"}},"
+                        + "\"required\":[\"diff\"]}")
+        .addInitialArgument("status_poll_interval_millis", 500L)
+        .build());
+```
+{{< /tab >}}
+
+{{< tab "YAML" >}}
+```yaml
+agents:
+  - name: orchestrator
+    subagents:
+      - name: remote_runner
+        clazz: my_app.agents.RemoteRunSetup
+        endpoint: https://example.com/runs
+        description: Run a review task on the remote service
+        input_schema: '{"type":"object","properties":{"diff":{"type":"string"}},"required":["diff"]}'
+        status_poll_interval_millis: 500
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The base class also recovers the crash window between "POST sent" and "POST durably
+recorded". On recovery it probes the run's status first, and it re-posts only when the
+probe reports that the run never started. Override `reconcileSubmitRequest` /
+`reconcile_submit_request` only if your service needs a different repair.
+
+## Invoke a sub-agent
+
+A sub-agent is invoked in one of two ways, model-driven or caller-driven. The
+difference you see is where the result goes.
+
+In model-driven delegation the result returns to the model. The sub-agent is offered
+to the model as a tool, so your action sends a `ChatRequestEvent` and the model
+answers with a call to the sub-agent. The result goes back to the model as the tool
+observation, and the model reasons over it until a final `ChatResponseEvent` reaches
+your action.
+
+In caller-driven delegation the result returns to your action. An action that needs
+the delegated outcome calls the sub-agent itself, with no model in the loop.
+
+Both ways end in the same call. Your action, or the framework on the model's behalf,
+runs `submit(ctx, prompt)` on the registered name. That call returns a
+`SubagentFuture`, which is the handle for that one call. Awaiting the handle yields
+the call's `SubagentResult`.
+
+### Delegate from a chat model
+
+List the registered name under `subagents` of the parent's chat model:
+
+{{< tabs "Declare Sub-agents on a Chat Model" >}}
+
+{{< tab "Python" >}}
+```python
+ResourceDescriptor(
+    clazz=ResourceName.ChatModel.OLLAMA_SETUP,
+    connection="ollama_connection",
+    model="qwen3:8b",
+    subagents=["reviewer"],
+)
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+ResourceDescriptor.Builder.newBuilder(ResourceName.ChatModel.OLLAMA_SETUP)
+    .addInitialArgument("connection", "ollamaConnection")
+    .addInitialArgument("model", "qwen3:8b")
+    .addInitialArgument("subagents", List.of("reviewer"))
+    .build();
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+The model sees `reviewer` as a tool, described by the registration's description and
+input schema. A schema is required for model-selected calls. A missing description
+falls back to a generic delegation description.
+
+The arguments the model sends become the sub-agent's input, and its outputs return to
+the model as the tool observation. The model can then keep reasoning over them. For
+an internal sub-agent, the observation is the JSON encoding of the full output list
+described in [the result](#the-result). That holds even when the child emits a single
+output, so the shape does not depend on how many outputs the child emits.
+
+An external setup class can state the shape of that observation. It declares
+`result_type()` in Python or `getResultType()` in Java, and the default reports the
+result as it arrived. Declare one to narrow a wider payload, or to convert a payload
+that is not already JSON-compatible. An internal sub-agent has no such declaration, so
+its observation is always the JSON encoding of its output list. This shapes the
+observation only. A caller that awaits the handle gets the result the sub-agent
+produced, as [the result](#the-result) describes.
+
+The external sub-agents `remote_reviewer` and `remote_runner` are offered the same
+way. Only their registration differs.
+
+### Call from an action
+
+An action can submit input to a registered sub-agent directly and await its
+outcome. This path needs no description and no input declaration, so it also works
+for a child registered as-is.
+
+{{< tabs "Call a Sub-agent From an Action" >}}
+
+{{< tab "Python" >}}
+```python
+class DelegatingAgent(Agent):
+    @action(EventType.InputEvent)
+    @staticmethod
+    async def delegate(event: Event, ctx: RunnerContext) -> None:
+        reviewer = ctx.get_resource("reviewer", ResourceType.AGENT)
+        future = await reviewer.submit(ctx, "review the diff")
+        result = await future
+        if result.success:
+            ctx.send_event(OutputEvent(output=result.result[0]))
+        else:
+            ctx.send_event(OutputEvent(output=f"failed: {result.error_message}"))
+```
+{{< /tab >}}
+
+{{< tab "Java" >}}
+```java
+public class DelegatingAgent extends Agent {
+
+    @Action(EventType.InputEvent)
+    public static void delegate(Event event, RunnerContext ctx) throws Exception {
+        SubagentSetup reviewer =
+                (SubagentSetup) ctx.getResource("reviewer", ResourceType.AGENT);
+        SubagentResult result = reviewer.submit(ctx, "review the diff").await();
+        if (result.isSuccess()) {
+            String answer = String.valueOf(((List<?>) result.getResult()).get(0));
+            ctx.sendEvent(new OutputEvent(answer));
+        } else {
+            ctx.sendEvent(new OutputEvent("failed: " + result.getErrorMessage()));
+        }
+    }
+}
+```
+{{< /tab >}}
+
+{{< /tabs >}}
+
+What you pass to `submit` is what the sub-agent receives. An internal child gets it
+as its `InputEvent` payload, and a model-driven call passes the arguments map
+instead. What comes back is a `SubagentResult`. For the internal sub-agent used here,
+its `result` is the list of `OutputEvent` payloads the child emitted. That is why the
+example reads `result.result[0]`. An external sub-agent returns whatever its setup
+produces.
+
+In Python `submit` is a coroutine, so the call is awaited in two phases and the
+action must be `async`. In Java `submit` returns the handle directly and `await()`
+yields the result. The same two calls drive `remote_reviewer` and `remote_runner`
+unchanged. Only the registration differs.
+
+## Work with open handles

Review Comment:
   Done. Manage sub-agent calls, and Results and failures with Durability and failover and Metrics and Trace Log promoted to their own sections.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org