Skip to content

fix(harness): inherit parent hooks in declared subagents - #2996

Open
ordinary-s wants to merge 10 commits into
agentscope-ai:mainfrom
ordinary-s:fix/1578-declared-subagent-hooks
Open

ordinary-s wants to merge 10 commits into
agentscope-ai:mainfrom
ordinary-s:fix/1578-declared-subagent-hooks

Conversation

@ordinary-s

@ordinary-s ordinary-s commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.0.3-SNAPSHOT

Description

Fixes #1578

Automatically constructed local declared subagents now inherit their parent's explicitly configured Hook instances, matching general-purpose subagents. Programmatic declarations, static workspace Markdown and dynamic Markdown discovery use the same factory. Hook priority and deduplication remain unchanged. Hook Javadoc explains shared-instance reentrancy, agent/session state isolation and parent-only event handling.

Filter Hook-contributed tools against the declaration allowlist and effective child workspace policy before installation, including when runtime tool deletion is disabled. Rejected contributions preserve existing same-name tools; retained tools preserve execution configuration, callbacks and reflective metadata.

The core API is ReActAgent.Builder.hookToolFilter(Predicate<String>).build(). The predicate deliberately receives only resolved public tool/schema names after duplicate resolution; it does not receive tool objects or runtime arguments. Null predicates are rejected, and omitting the setter preserves normal registration. Accepted tools are copied in sorted name order. Hook contributions must have no registration-time resource ownership or one-shot toolkit binding; this contract is documented both on Hook.tools() and beside the temporary toolkit.

Declared-child MCP behavior

When there is no explicit parent ToolsConfig override, resolve the child's workspace/tools.json before deriving its declaration policy. This intentionally loads that file's mcpServers, including when the declaration contains a nonempty tools: list. Previously the synthetic strict override hid the entire workspace configuration and prevented these servers from being registered. Existing declarations with workspace MCP servers can therefore establish connections that were previously suppressed. The declaration filters exposed tools, not server connections. Explicit parent configuration still takes precedence; an explicit configuration with no MCP servers suppresses workspace MCP registration.

A deterministic regression builds the real declared child and exercises workspace loading, MCP registration, tool restrictions and client closure with mocked transport clients. It pins workspace-server, explicit-server and empty-override behavior without external connections.

General-purpose behavior, custom factories, remote subagents and middleware inheritance are unchanged.

Validation

Windows, Maven 3.9.11, Temurin Java 17.0.20.1. Follow-up based on PR head 8d5687bababf1589a0f2bfb6779fff4d382f0d5a (main c6af781836b23b1325968585104445856c57bba3).

  • Focused Hook/MCP regressions: mvn -pl agentscope-harness -am "-Dtest=HookToolsRegistrationTest,DeclaredSubagentMcpConfigTest,HarnessAgentTest#subagentHooks_*" -Dsurefire.failIfNoSpecifiedTests=false -DforkCount=0 test — 41 cases passed.

  • Core full suite: mvn -pl agentscope-core -am test — 2341 cases, 0 assertion failures, 2 errors, 8 skipped. Both errors are Windows symlink privilege failures in DangerousPathBypassTest; the same two errors reproduce on unchanged PR head 8d5687ba in a separate clean worktree.

  • Harness full suite: mvn -pl agentscope-harness -am "-Dtest=io.agentscope.harness.**" -Dsurefire.failIfNoSpecifiedTests=false -DforkCount=0 test — 1062 cases, 0 failures/errors, 15 skipped (1047 passed).

  • Whole-repository mvn spotless:check and diff whitespace checks against the PR base passed.

  • Consumer compatibility: mvn -pl agentscope-distribution/agentscope-all -am -DskipTests package — all 63 modules built successfully, including test compilation and Javadoc packaging. This is a compilation/package check, not an extension test run.

Harness retains the existing local forkCount=0 workaround. Core uses the default forked JVM: attempting the whole core suite without a fork additionally breaks existing working-directory and nested-jar classloading tests. No build/test configuration is changed, and full-reactor green status is not claimed. Skips and environment errors are not counted as passes.

Checklist

  • Public filter API and Hook lifecycle contracts documented.
  • Regression tests cover actual declared-subagent tool policies and MCP configuration selection.
  • Formatting and compilation checked; local test limitations described above.

@codecov

codecov Bot commented Sep 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@xiaoyuuuuuupeng xiaoyuuuuuupeng 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.

LGTM

* @return a new ReActAgent instance
* @throws IllegalArgumentException if required parameters are missing or invalid
*/
public ReActAgent build(Consumer<Toolkit> hookToolFilter) {

ghost Sep 8, 2026

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.

Non-blocking suggestion: since this callback is intended to decide which hook-contributed tools to retain, could the public API accept a Predicate<String> instead? Consumer<Toolkit> also allows callers to add or replace tools in the temporary toolkit, which is broader than the filtering contract described by the Javadoc. A predicate would express the intended keep/drop behavior more directly.

ghost Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. Updated the overload to build(Predicate). The predicate receives the resolved registered tool name (the same name exposed in the tool schema), with true retaining the Hook-contributed tool and false excluding it.

Filtering is applied after Hook tool resolution and only to Hook contributions. Existing tool registration, ordering/deduplication, toolkit configuration, callbacks, and reflective metadata remain unchanged. I also added regression coverage for these boundaries.

@xiaoyuuuuuupeng

ghost commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Thanks for the update. I reviewed the follow-up changes. The Predicate API now matches the intended filtering contract, and the added tests cover the important boundaries: resolved tool names, Hook ordering/deduplication, existing and built-in tools, toolkit configuration/metadata, and effective child tool restrictions.

The Hook inheritance Javadoc and parent-only filtering guidance also address my documentation concern. No further changes requested from my side.

ghost 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.

Summary

Declared subagents now inherit the parent's explicit Hook collection, and hook-contributed tools are filtered by the declaration allowlist plus the child's workspace policy before installation — implemented by a new ReActAgent.Builder.build(Predicate<String>) hook and a package-private HarnessAgent.Builder.build(List<String>) variant. Fixes #1578.

Verdict: COMMENT — the approach is sound and the test coverage is unusually thorough, but I want two points resolved before this is treated as ready:

  1. The capturedToolsConfig != null guard in buildDeclaredFactory quietly changes MCP server materialization for declared subagents that have a tools: declaration and their own workspace/tools.json (details inline). If that is intended, it belongs in the PR description / release note, because it widens what a declared child can reach externally.
  2. The new build(Predicate<String>) overload is core API that cascades to harness, distribution and every extension; the name-only, null-means-no-op contract should be a deliberate decision (inline).

Strengths

  • Filtering before installation is the right layer: it keeps working when the toolkit forbids runtime deletion (allowToolDeletion(false)), which a post-registration ToolFilter.apply could not do. The test that asserts a rejected contribution leaves the child's same-name Harness tool intact pins exactly that.
  • filterRunsAfterToolNameResolutionWithoutChangingHookOrderOrDeduplication is the correct regression to write here: it proves the throwaway-toolkit rewrite preserves hook priority ordering, one-shot tools() invocation, and duplicate-name resolution instead of just asserting the happy path.
  • Preserving ToolBase metadata (readOnly / concurrencySafe), the caller's executor and the chunk callback through the copy is verified by executing the kept tool, not by inspecting fields.
  • Making childToolsConfig package-private rather than duplicating its strict-allow synthesis keeps one definition of the policy.
  • The empty-list case is handled deliberately: an empty declaration still applies the child's workspace policy instead of being read as "allow everything".

Caveat

Read-only review: nothing was compiled or executed here beyond the CI evidence below, and the 727-line HarnessAgentTest addition was reviewed for what it pins, not run.

CI on head 8d5687ba: ubuntu + windows build, Check License, Check Module Sync and codecov/patch all pass; CLA signed. MERGEABLE/BLOCKED with one human APPROVED already on record.


Automated review by github-manager-bot

ghost 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.

Summary

Re-review after the follow-up push (8d5687ba → a6ad007e). The previous round's blocking concerns are addressed: contributed tools are copied in sorted name order, the new hookToolFilter setter replaces the build(Predicate) shape, the temporary-toolkit contract is now documented on both Hook.tools() and ReActAgent.Builder, and the workspace-MCP / declaration-allowlist interaction is pinned by DeclaredSubagentMcpConfigTest plus the subagentHooks_* matrix. Hook inheritance for declared subagents now matches the general-purpose path, which is the right way to close #1578.

Findings

No correctness bug found in the new commits. The remaining items are about an undocumented parent/child asymmetry and the silence of policy-driven drops:

  • [Warning] HarnessAgent.java:2969 — inherited hook tools are policy-gated in declared children but never in the parent (ToolFilter.apply runs before inner.build()); the asymmetry should be documented.
  • [Warning] HarnessAgent.java:2973 — a dropped hook tool leaves no log evidence of which policy rejected it.
  • [Warning] ReActAgent.java:5376 — the throwaway toolkit makes Hook.tools() a second-pass call and binds against ToolkitConfig.defaultConfig(); tighten the contract wording.
  • [Info] HarnessAgent.java:2765, HarnessAgentBuilderSupport.java:453, ReActAgent.java:5379 — precedence / snapshot / same-name-replacement clarity.

Verdict

Comment only, not an approval: build (windows-latest) is still pending on the new head and codecov/patch reports a failure, so coverage on the added filter branches should be confirmed green before this is approved. None of the findings above block a merge once CI is green — they are documentation and observability follow-ups.


Automated review by github-manager-bot

ghost 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.

Summary

Re-review at 65af113f. The only change since the previous pass (a6ad007e) is a merge of upstream main: Hook.java, HookToolsRegistrationTest and DeclaredSubagentMcpConfigTest are untouched by the merge, so the earlier analysis still holds.

CI on this head is green — build (ubuntu-latest), build (windows-latest), Check License, Check Module Sync, codecov/patch, license/cla — which was the only reason the previous round stopped short of an approval.

Findings

No new issues in the merge. Open items from the previous round stay non-blocking follow-ups:

  • [Warning] HarnessAgent.java — inherited hook tools are policy-gated in declared children but not in the parent (ToolFilter.apply runs before inner.build()); the asymmetry deserves a doc note.
  • [Warning] HarnessAgent.java — a policy-dropped hook tool leaves no log evidence of which policy rejected it; a debug log would help operators.
  • [Info] ReActAgent.java — the throwaway toolkit makes Hook.tools() a second-pass call bound to ToolkitConfig.defaultConfig(); tighten the contract wording.

Verdict

Approving: hook inheritance for declared subagents is correct, pinned by tests, and CI + CLA are green on the current head. The items above are documentation/observability follow-ups, not merge blockers.

---Automated review by github-manager-bot

@oss-maintainer

ghost commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Merge conflict detected

This PR currently conflicts with main and cannot be merged (mergeable: CONFLICTING). Please rebase or merge main into fix/1578-declared-subagent-hooks and resolve the conflicts:

git fetch origin
git checkout fix/1578-declared-subagent-hooks
git rebase origin/main
# resolve conflicts, then:
git push --force-with-lease

This is a one-time reminder. Feel free to @mention me for a re-review after the conflicts are resolved.


Automated notification by github-manager-bot

ghost 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.

Summary

Re-review of the new head c2398d02, which merges upstream main (857e3e6cf1) into the previously approved 65af113f and resolves the hook-tool-registration conflicts. Scope of this pass is the merge resolution, not a re-litigation of the design.

What I verified in the resolved files:

  • ReActAgent.Builder.hookToolFilter survived the merge intact: the field, the null-checked setter, the build() branch that resolves hook contributions into a throwaway Toolkit and copies the retained ones, and the Hook.tools() javadoc that states the "plain value objects, no registration-time resource ownership" precondition this design depends on.
  • HarnessAgent.Builder.build(inheritedHookToolAllowlist) still composes the inherited allowlist with ToolFilter.isAllowed(...) before the child is built, and HarnessAgentBuilderSupport.childToolsConfig is package-private for that call site — the null/empty distinction is documented on the overload.
  • The conflict resolution strengthened rather than weakened the tests: HookToolsRegistrationTest now asserts assertNull(builderToolkit.getTool("hook_ping")) and adds a second agent registering a same-named tool, pinning both that the caller's toolkit is never mutated and that two agents keep distinct instances.

CI on this head is green — build (ubuntu-latest), build (windows-latest), Check License, Check Module Sync, codecov/patch, license/cla — and mergeStateStatus is CLEAN.

Approving. The two non-blocking items from the previous round are still open (no evidence trail for a policy-dropped hook tool, parent/child gating asymmetry deserves a doc note); one is inline above.


Automated review by github-manager-bot

Preserve the default 30-second chat wait and upstream 60-second waits for slow durable session scenarios.

Validation: 115 focused tests passed across Hook tools, declared subagents, builder reuse, custom factories, and chat sessions. Relevant Spotless checks and git diff whitespace checks passed. Tests use a workspace temp directory to avoid Windows system-temp access errors.
@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

P2 重要不紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:声明的子 Agent 不会继承父 Agent hooks

4 participants