Skip to content

feat(harness): let operators configure subagent timeout, overriding the LLM - #2749

Open
haosong384 wants to merge 3 commits into
agentscope-ai:mainfrom
haosong384:feat/subagent-declaration-timeout
Open

haosong384 wants to merge 3 commits into
agentscope-ai:mainfrom
haosong384:feat/subagent-declaration-timeout

Conversation

@haosong384

@haosong384 haosong384 commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT, synchronized with main at 48f7257a.

Description

Long-running subagent work can exceed the wait chosen by the model. Applications can now configure that wait on SubagentDeclaration.timeoutSeconds(...) or, for an individual call, AgentSpawnTool.CTX_TIMEOUT_SECONDS in RuntimeContext. For example, a declaration timeout of 300 seconds overrides an LLM request for immediate background execution (timeout_seconds=0), so the parent waits for a result for up to 300 seconds.

The configuration bounds the parent's synchronous wait. In ordinary mode, an unfinished local execution is promoted to a background task and continues running; remote execution remains on its task server and returns its task ID. Force-sync keeps the existing cancellation/no-promotion behavior when the wait expires.

Resolution precedence

Mode Highest to lowest priority
Ordinary Positive CTX_TIMEOUT_SECONDS → positive declaration timeout → LLM timeout_seconds
Force-sync CTX_FORCE_SYNC_TIMEOUT_SECONDS → positive CTX_TIMEOUT_SECONDS → positive declaration timeout → LLM timeout_seconds

General context/declaration values of zero or less are ignored. Positive values are capped at 600 seconds. The existing force-sync override retains its behavior: non-positive values use the 30-second default, and values above 600 are capped. Without an application override, the existing LLM sync/background behavior is preserved; force-sync still coerces an LLM value of zero to the default wait.

All three dispatch paths use the same resolution: fresh agent_spawn, persisted-instance reuse, and agent_send. The two-argument resolver remains available and delegates with an empty declaration.

SubagentDeclaration.builder()
        .name("deep-etl")
        .description("Runs a long ETL pipeline")
        .timeoutSeconds(300)
        .build();

RuntimeContext ctx = RuntimeContext.builder()
        .put(AgentSpawnTool.CTX_TIMEOUT_SECONDS, 120)
        .build();

Conflict and review fixes

  • Merged current main, resolving declaration conflicts while retaining enablePendingToolRecovery and current call-scoped manager/permission handling.
  • Clarified getter, builder and force-sync Javadoc to distinguish synchronous waiting from execution lifetime and document positive-value semantics.
  • Updated the English and Chinese subagent guides with configuration examples, precedence, background continuation and force-sync behavior.

Validation

  • Latest head e0ead076: all six CI checks passed (Linux, Windows, license, module sync, documentation validation and patch coverage). CLA status passed too.
  • Full clean verify in Linux CI and Windows CI: 10,495 tests per platform, 0 failures, 0 errors. Linux reports 187 skips; Windows reports 218 skips.
  • Patch coverage: 95.23%, above the 60% target.
  • Focused Java regression: 54 tests, 0 failures, 0 errors, 0 skips, including 20 timeout cases. Coverage includes invalid-value fallback, clamping, force-sync precedence, fresh spawn, persisted reuse, send, call-scoped overrides, background execution continuing after timeout, and execution cancellation under force-sync.
  • Complete Harness regression: 1,172 tests, 0 failures, 0 errors, 18 skips; Spotless checks passed in the reactor build.
  • Local mvn -B -T1 clean verify ran 2,590 Core tests (0 failures, 2 errors, 8 skips) and stopped on two untouched DangerousPathBypassTest cases because this Windows host lacks symbolic-link creation privileges. No test assertions or unrelated Core code were changed.
  • Documentation: 13 tests passed; npm run validate passed (430 pages / 584 redirects and strict build validation).
  • npm run broken-links reports the existing 2,206 broken links across 326 files; this change adds no links or anchors, and the repository's navigation/local-link source check passes.

Run the new timeout scenarios or the complete Java check from the repository root:

mvn -B -T1 -pl agentscope-harness -am \
    -Dtest=AgentSpawnToolDeclarationTimeoutTest \
    -Dsurefire.failIfNoSpecifiedTests=false test
mvn -B -T1 clean verify

Checklist

  • Code formatted with mvn spotless:apply; reactor Spotless checks passed
  • All tests passing in Linux and Windows full CI (skip counts and local host limitations documented above)
  • Javadoc describes wait duration, precedence and execution lifetime
  • Related English and Chinese documentation updated
  • Code ready for review

Closes #2103

Copilot AI lite review requested due to automatic review settings August 17, 2026 08:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds operator-controlled subagent timeout configuration so application code can bound the synchronous wait for subagent runs, overriding the LLM-provided timeout_seconds (and doing so without requiring force-sync mode).

Changes:

  • Introduces SubagentDeclaration.timeoutSeconds to configure a per-subagent synchronous wait timeout.
  • Adds AgentSpawnTool.CTX_TIMEOUT_SECONDS and updates timeout resolution to apply per-call context and per-subagent declaration precedence over the LLM value.
  • Adds a focused test suite covering the new precedence and edge cases.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
agentscope-harness/src/main/java/io/agentscope/harness/agent/tool/AgentSpawnTool.java Adds a new per-call context override key and updates effective timeout resolution to incorporate operator-configured precedence.
agentscope-harness/src/main/java/io/agentscope/harness/agent/subagent/SubagentDeclaration.java Adds a new per-subagent timeout configuration field + builder setter/getter.
agentscope-harness/src/test/java/io/agentscope/harness/agent/tool/AgentSpawnToolDeclarationTimeoutTest.java Adds unit tests validating precedence (context → declaration → LLM) and force-sync interaction.
Suppressed comments (1)

agentscope-harness/src/main/java/io/agentscope/harness/agent/subagent/SubagentDeclaration.java:600

  • This builder Javadoc says a non-null timeoutSeconds "forces synchronous execution"; in practice it forces a synchronous wait window (the run may continue in the background if the wait times out and force-sync is not enabled). Rewording avoids implying the subagent is stopped/interrupted at the timeout.
         * <p>Use this when the subagent runs deep, long-running work whose duration the model
         * cannot estimate reliably. A non-null value forces synchronous execution for up to this
         * many seconds, even if the model requests background execution. {@code null} (default)
         * defers to the {@code RuntimeContext} override and then the LLM's argument.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +308 to +319
* <p>When set (non-null), this value takes precedence over the {@code timeout_seconds} argument
* the LLM supplies on {@code agent_spawn} / {@code agent_send}, giving the application operator
* control over how long a deep, long-running subagent is allowed to run before it is
* interrupted. This is useful when the model cannot reliably estimate the wait and keeps
* timing out.
*
* <p>Semantics when set:
*
* <ul>
* <li>The subagent runs synchronously for up to this many seconds, overriding an LLM request
* for background execution ({@code timeout_seconds=0}).
* <li>Values are clamped to the tool's maximum; {@code <= 0} is treated as unset.
@AgentScopeJavaBot AgentScopeJavaBot added enhancement New feature or request area/harness agentscope-harness (test/runtime support) labels Aug 17, 2026
…he LLM

Subagent timeouts could only be set by the model through the agent_spawn /
agent_send timeout_seconds argument. For deep, long-running work the model
cannot estimate the duration reliably, so calls kept timing out even after the
model retried with a larger value. The only existing override,
CTX_FORCE_SYNC_TIMEOUT_SECONDS, applies solely when CTX_FORCE_SYNC is enabled,
which also strips the model of the ability to choose background execution.

This adds an operator-controlled timeout that does not depend on force-sync:

- SubagentDeclaration: new Integer timeoutSeconds (builder + getter), so a
  timeout can be configured per subagent type alongside steps/model/etc.
- AgentSpawnTool: new CTX_TIMEOUT_SECONDS RuntimeContext key for a per-call
  override, plus resolveOperatorTimeoutSeconds() and a 3-arg
  resolveEffectiveTimeoutMs(Integer, RuntimeContext, Optional<SubagentDeclaration>).

Resolution precedence:
- force-sync: CTX_FORCE_SYNC_TIMEOUT_SECONDS > CTX_TIMEOUT_SECONDS >
  declaration > LLM timeout_seconds
- otherwise:  CTX_TIMEOUT_SECONDS > declaration > LLM timeout_seconds

An operator timeout expresses explicit intent to wait synchronously, so a
positive value overrides an LLM request for background execution
(timeout_seconds=0). Non-positive operator values are treated as unset and fall
through to the next source; all sources are clamped to MAX_TIMEOUT_SECONDS.

All three call sites (agentSpawn, agentSend, execSpawnTask) now pass the
declaration, so local and remote subagents share the same resolution. The 2-arg
resolveEffectiveTimeoutMs overload is preserved and delegates with an empty
declaration, keeping existing behaviour unchanged.

Closes agentscope-ai#2103
@haosong384
haosong384 force-pushed the feat/subagent-declaration-timeout branch from 7fcc874 to 9682918 Compare August 17, 2026 09:11
@codecov

codecov Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.23810% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../agentscope/harness/agent/tool/AgentSpawnTool.java 94.11% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Review

This PR adds operator-configurable subagent timeouts via a new CTX_TIMEOUT_SECONDS runtime context key and a timeoutSeconds field on SubagentDeclaration. The implementation is well-structured with clear precedence rules and backwards compatibility. The resolution logic is properly tested with comprehensive unit tests.

@oss-maintainer

Copy link
Copy Markdown
Collaborator

⚠️ Merge conflict detected

This PR currently conflicts with main and cannot be merged. Please rebase (or merge main in) and resolve the conflicts:

git fetch upstream
git checkout feat/subagent-declaration-timeout
git rebase upstream/main
# resolve conflicts, then:
git push --force-with-lease

This is a one-time reminder. Once the conflicts are resolved, comment @oss-maintainer please re-review and I will pick it up again.


Automated notification by github-manager-bot

Buktal added a commit that referenced this pull request Oct 31, 2026
#3879)

## AgentScope-Java Version

2.1.0-SNAPSHOT; based on upstream `main` at `e190a8d1b`.

## Description

A synchronous `agent_spawn` or `agent_send` can be cancelled by the
ordinary-tool timeout before its own wait finishes. Raising the
application override alone also leaves it silently capped at 600
seconds.

Fixes #3878.

### Summary

```diff
- Ordinary-tool timeout wraps the entire subagent invocation
+ Delegation uses AgentSpawnTool's existing wait timer
+ Positive application force-sync overrides can exceed 600 seconds
```

Add an opt-in `timeoutManaged` tool flag, defaulting to false, and
enable it on `agent_spawn` / `agent_send`. Ordinary tools, including
tools called inside the child, retain their configured timeout. The
LLM-facing 600-second cap and parent-cancellation propagation are
preserved. Update the existing English and Chinese subagent guides.

Related: #3166 and #1451. This PR focuses on spawn/send and does not
change `task_output` or add a separate application-ceiling policy. It
overlaps #2749 in `AgentSpawnTool` and the subagent guides; it does not
depend on that PR.

### Evidence

- **Before:** `Tool execution failed: Tool execution timeout after
PT10M` interrupts the whole delegation; an application override of 1800
is also clamped to 600.
- **After:** regression coverage uses a 50 ms ordinary-tool timeout and
a 200 ms child invocation. Both spawn and send finish successfully
through direct and batch Toolkit calls. Ordinary-tool timeout, parent
cancellation, and the child's own force-sync timeout remain covered.

Validation completed on Windows with Java 21:

```text
mvn -B -T1C clean verify                                         PASS
mvn -B -T1C -pl agentscope-harness -am \
  -Dtest=AgentSpawnTool*Test,ToolExecutorTest,ReflectiveFunctionToolTest test
                                                               66 tests passed
npm test                                                       13 tests passed
npm run validate                                               PASS
```

`npm run broken-links` reports 2,222 broken links across 328 files. This
patch changes no links or anchors; the repository's documentation source
check and strict build validation pass.

### Merge Danger

**Door:** Two-way; revert the commit to restore the previous timeout
behavior.

**Blast Radius:** Tools. The new flag defaults to false; only the two
delegation tools opt in. Applications own the positive force-sync wait
budget.

## Checklist

- [x] Code formatted with `mvn spotless:apply`
- [x] All tests pass in the full `clean verify`
- [x] Javadoc comments updated
- [x] Related English and Chinese documentation updated
- [x] Code ready for review
@jujn jujn added the P2 重要不紧急 label Nov 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/harness agentscope-harness (test/runtime support) enhancement New feature or request P2 重要不紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

子智能体无法控制超时时间,目前都是AI通过启动子智能体传递过去的

5 participants