-
Notifications
You must be signed in to change notification settings - Fork 167
[api][plan][runtime] Introduce AGENT resource type and sub-agent invocation API #938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0b75c92
[runtime] Consolidate per-task execution contexts into a single trans…
pltbkd 966ec52
[python] Add optional durable id keying for durable execution
pltbkd e59e522
[runtime] Record the input sequence number on action tasks
pltbkd afa13af
[runtime] Broadcast record/action lifecycle events to registered list…
pltbkd 6cb1aa0
[runtime] Materialize resources of a type through their owning runtime
pltbkd 45341b9
[api] Add sub-agent resource definitions
pltbkd 0ec451b
[plan] Register sub-agent setups as AGENT resources in the planner
pltbkd 64a022c
[runtime][python] Add sub-agent setup base with deterministic id assi…
pltbkd 3024260
[runtime][python] Add deferred execution mode for external sub-agent …
pltbkd d1b2490
[runtime][python] Add async external sub-agent base running in durabl…
pltbkd 9e14200
[runtime][e2e] Add sub-agent integration tests
pltbkd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
api/src/main/java/org/apache/flink/agents/api/subagent/SubagentFuture.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.flink.agents.api.subagent; | ||
|
|
||
| /** | ||
| * Handle for one sub-agent invocation, identified by the {@code (sessionId, callId)} pair that keys | ||
| * the invocation. | ||
| */ | ||
| public abstract class SubagentFuture { | ||
|
|
||
| private final String sessionId; | ||
| private final String callId; | ||
|
|
||
| protected SubagentFuture(String sessionId, String callId) { | ||
| this.sessionId = sessionId; | ||
| this.callId = callId; | ||
| } | ||
|
|
||
| public String getSessionId() { | ||
| return sessionId; | ||
| } | ||
|
|
||
| public String getCallId() { | ||
| return callId; | ||
| } | ||
|
|
||
| /** Whether the invocation has reached a terminal state. */ | ||
| public abstract boolean isDone(); | ||
|
|
||
| /** | ||
| * Resolves the invocation, waiting until it reaches a terminal state. Failures converge into a | ||
| * failed {@link SubagentResult} rather than a separately reported exceptional completion. | ||
| */ | ||
| public abstract SubagentResult await() throws Exception; | ||
|
|
||
| /** Requests cancellation of the invocation. */ | ||
| public void cancel() {} | ||
|
|
||
| /** Groups this handle with others to be resolved together through {@link SubagentFutures}. */ | ||
| public abstract SubagentFutures combine(SubagentFuture... others); | ||
| } |
44 changes: 44 additions & 0 deletions
44
api/src/main/java/org/apache/flink/agents/api/subagent/SubagentFutures.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.flink.agents.api.subagent; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A group of sub-agent handles to be resolved together. A group is not itself an invocation and | ||
| * carries no {@code (sessionId, callId)} identity. | ||
| */ | ||
| public abstract class SubagentFutures { | ||
|
|
||
| /** Whether every handle in the group has reached a terminal state. */ | ||
| public abstract boolean isDone(); | ||
|
|
||
| /** | ||
| * Waits for every handle in the group and returns their outcomes in the order the handles were | ||
| * added. Like {@link SubagentFuture#await()}, failures surface through failed {@link | ||
| * SubagentResult}s. | ||
| */ | ||
| public abstract List<SubagentResult> awaitAll() throws Exception; | ||
|
|
||
| /** Requests cancellation of every handle in the group. */ | ||
| public void cancel() {} | ||
|
|
||
| /** Adds more handles to the group. */ | ||
| public abstract SubagentFutures combine(SubagentFuture... others); | ||
| } |
119 changes: 119 additions & 0 deletions
119
api/src/main/java/org/apache/flink/agents/api/subagent/SubagentResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.flink.agents.api.subagent; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonIgnore; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.Serializable; | ||
|
|
||
| /** | ||
| * Outcome of a sub-agent call issued through {@link SubagentSetup}. A successful outcome carries a | ||
| * JSON-serializable payload, and a failed one carries a serializable error message. | ||
| * | ||
| * <p>Implementations capture their internal failures into a result through {@link #error} instead | ||
| * of throwing, so callers inspect {@link #isSuccess()} rather than catching. Because the failure is | ||
| * carried as a message rather than a live exception, the whole result can be persisted through | ||
| * durable execution and survive a failover. | ||
| */ | ||
| public class SubagentResult implements Serializable { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(SubagentResult.class); | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
|
|
||
| private final boolean success; | ||
| private final Object result; | ||
| private final String errorMessage; | ||
|
|
||
| @JsonCreator | ||
| public SubagentResult( | ||
| @JsonProperty("success") boolean success, | ||
| @JsonProperty("result") Object result, | ||
| @JsonProperty("errorMessage") String errorMessage) { | ||
| this.success = success; | ||
| this.result = result; | ||
| this.errorMessage = errorMessage; | ||
| } | ||
|
|
||
| /** Creates a successful result carrying the given value. */ | ||
| public static SubagentResult ok(Object result) { | ||
| return new SubagentResult(true, result, null); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a failed result carrying the exception's type and message. The full stack trace is | ||
| * logged here rather than persisted, keeping the durable payload bounded. | ||
| */ | ||
| public static SubagentResult error(Exception exception) { | ||
| if (exception == null) { | ||
| return new SubagentResult(false, null, null); | ||
| } | ||
| LOG.warn("Sub-agent call failed; persisting the exception summary.", exception); | ||
| return new SubagentResult(false, null, summaryOf(exception)); | ||
| } | ||
|
|
||
| /** Creates a failed result carrying the given message. */ | ||
| public static SubagentResult error(String errorMessage) { | ||
| return new SubagentResult(false, null, errorMessage); | ||
| } | ||
|
|
||
| private static String summaryOf(Exception exception) { | ||
| return exception.getClass().getName() + ": " + exception.getMessage(); | ||
| } | ||
|
|
||
| public boolean isSuccess() { | ||
| return success; | ||
| } | ||
|
|
||
| public Object getResult() { | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the payload converted to {@code resultClass}. | ||
| * | ||
| * <p>Durable recovery re-binds the persisted payload through a plain {@link ObjectMapper} | ||
| * without polymorphic typing, so after a failover replay {@link #getResult()} hands back a | ||
| * {@code LinkedHashMap} instead of the caller's type. This accessor converts the payload to the | ||
| * expected class uniformly on both the first execution and a replay. | ||
| */ | ||
| public <T> T getResult(Class<T> resultClass) { | ||
| return OBJECT_MAPPER.convertValue(result, resultClass); | ||
| } | ||
|
|
||
| public String getErrorMessage() { | ||
| return errorMessage; | ||
| } | ||
|
|
||
| /** | ||
| * Reconstructs an exception carrying the stored summary as its message, or null if this result | ||
| * is successful. | ||
| */ | ||
| @JsonIgnore | ||
| public Exception getException() { | ||
| return success ? null : new RuntimeException(errorMessage); | ||
| } | ||
| } | ||
58 changes: 58 additions & 0 deletions
58
api/src/main/java/org/apache/flink/agents/api/subagent/SubagentSetup.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.flink.agents.api.subagent; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnore; | ||
| import org.apache.flink.agents.api.context.RunnerContext; | ||
| import org.apache.flink.agents.api.resource.ResourceType; | ||
| import org.apache.flink.agents.api.resource.SerializableResource; | ||
|
|
||
| /** | ||
| * Caller-facing definition of a sub-agent, registered in the agent plan as an {@code AGENT} | ||
| * resource. | ||
| */ | ||
| public abstract class SubagentSetup extends SerializableResource { | ||
|
|
||
| @Override | ||
| @JsonIgnore | ||
| public ResourceType getResourceType() { | ||
| return ResourceType.AGENT; | ||
| } | ||
|
|
||
| /** | ||
| * Issues a new invocation with an implementation-assigned identity. This is the preferred form. | ||
| */ | ||
| public abstract SubagentFuture submit(RunnerContext ctx, Object prompt) throws Exception; | ||
|
|
||
| /** | ||
| * Issues an invocation that continues the conversation of an earlier invocation. Pass the | ||
| * {@code sessionId} of the earlier invocation to continue it. The session id is available on | ||
| * the handle returned by that invocation. Whether a conversation can be continued across | ||
| * actions is up to the concrete implementation. | ||
| */ | ||
| public abstract SubagentFuture submit(RunnerContext ctx, Object prompt, String sessionId) | ||
| throws Exception; | ||
|
|
||
| /** | ||
| * Issues an invocation under the given {@code (sessionId, callId)} identity. This form is | ||
| * reserved for implementation use. | ||
| */ | ||
| public abstract SubagentFuture submit( | ||
| RunnerContext ctx, Object prompt, String sessionId, String callId) throws Exception; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resultis typedObject,BaseSubagentCallable.getResultClass()pins the durable result class toResult.class(BaseSubagentCallable.java:47-48), and recovery re-binds through the plainOBJECT_MAPPERatRunnerContextImpl.java:516, which is constructed with no polymorphic typing (:67-68).I round-tripped a small POJO payload through this
ResultwithwriteValueAsString/readValue(s, Result.class):So
getResult()hands back the author's type on the first execution and aLinkedHashMapafter a failover replay. The shipped example casts atExternalSubagentAgent.java:52(((List<?>) result.getResult()).get(0)) and survives only because JSON arrays bind toArrayList. Every payload in the suite is aStringor aList<String>(MockExternalSubagentSetup.java:92,SubagentIdentityRecoveryTest.java:108), so nothing currently exercises the shape that breaks.Python does not diverge here. Its durable payload goes through
cloudpickle(flink_runner_context.py:430,473), which preserves the type, so this is also a Java/Python semantic gap on new public API thatAGENTS.mdasks to keep aligned.What should
getResult()return after a replay when the sub-agent returned a record or a POJO? A couple of routes, in case they help: makingResultgeneric and threading the payload class throughgetResultClass(), or keeping the field opaque and addinggetResult(Class<T>)backed byOBJECT_MAPPER.convertValue. Either way a test with a non-String, non-collection payload would pin the behavior.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this — it's a real oversight. I'm currently working on the cross-language sub-agent invocation design and ran into the same issue there.
The plan is to add
getResult(Class<T>)backed by OBJECT_MAPPER.convertValue. This unifies all three paths where result can appear as a LinkedHashMap: durable recovery (Jackson JSON deserialization), cross-language sub-agent calls (pemja conversion), and first execution (direct cast, no conversion needed). convertValue handles the Map→POJO conversion uniformly regardless of the source.One known limitation: for generic collection payloads like List, both recovery and cross-language paths leave result as ArrayList — getResult(List.class) only returns List, losing element types. A
getResult(TypeReference<T>)overload can be added later if type-safe collections are needed.