Repository navigation
Conversation
Declared subagents (workspace subagents/*.md and programmatic SubagentDeclaration) are built through buildDeclaredFactory, which forwarded the parent's middlewares but not its hooks. The general-purpose subagent factory already propagates b.hooks, so the two paths were inconsistent: hooks configured on the parent for tool governance, human-in-the-loop approval, audit, or budget were silently dropped for declared subagents. Capture b.hooks in buildDeclaredFactory and apply them to the child builder alongside the middlewares, mirroring buildGeneralPurposeFactory. Fixes agentscope-ai#1578
ningmao-hlyz
left a comment
There was a problem hiding this comment.
[P1] Inherited hooks re-enable the parent's denied tools in the declared child. Hook tools() are registered after the toolkit is narrowed, so they bypass both the declaration allowlist and the parent's ToolsConfig deny list. Probe on this branch: parent with deny=["execute"] and a hook whose tools() returns an execute tool, declaration allowing only read_file → child toolkit is [execute, read_file]. The same probe on the base commit gives [read_file], so the bypass is introduced here. That is the boundary childCannotReenableParentDeniedTool protects. #2996 filters hook-contributed tools against the declaration allowlist and the effective child workspace policy; the same filter, or skipping tools() for declared children, looks necessary.
[P2] The new test asserts only that the hook object is present in the child's list. One assertion on the tool boundary above would have caught this.
[P2] Hooks are shared by reference (List.copyOf copies the list, not the hooks). One line in the javadoc about reentrancy and agent-scoped state would spare readers the analysis #2996 spells out.
Hook.tools() contributions are installed while the agent is built, after callers have assembled the toolkit, so there is no way to keep a specific contribution out before registration. Add ReActAgent.Builder.hookToolFilter(Predicate<String>). When set, hook contributions are resolved into a throwaway toolkit first and only the accepted names are registered on the agent toolkit. A rejected contribution never registers, so it cannot displace an existing tool with the same name. HookToolsRegistrationTest covers AgentTool and annotated @tool contributions, rejected names, and same-name preservation.
Declared subagents inherit the parent's hooks, but their Hook.tools() contributions were installed after ToolFilter.apply had narrowed the toolkit, so an inherited hook could re-enable a tool the parent denied or the declaration excluded. Set a hook tool filter on declared children that requires both the declaration allowlist and the effective child ToolsConfig. Add regression coverage for the denied-tool probe, allowlist boundaries, a parent deny without a declaration allowlist, a declared-but-denied tool, the no-policy case, annotated @tool contributions, multiple hooks, and the unchanged general-purpose path.
- Hook: document that automatically constructed subagents share the same instances, the resulting reentrancy and agent-scoped state requirements, and that tools() may be resolved more than once. - harness subagent pages (EN/ZH): note hook inheritance and that hook-contributed tools remain bounded by the declaration allowlist and the child's effective ToolsConfig.
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
|
All three points were valid. Follow-up pushed as three commits: P1 — fixed: inherited hooks can no longer re-enable removed tools. Confirmed the mechanism: The fix adds The two conditions are ANDed, so being declared does not override a parent deny. This is the same P2 — test coverage.
|
ningmao-hlyz
left a comment
There was a problem hiding this comment.
Re-reviewed at a485a147. The declared path is fixed and both P2s are addressed: the pre-registration filter sits in the right place, and resolving contributions in a throwaway toolkit is the necessary shape — registering first and removing later would throw IllegalStateException on a same-name contribution instead of leaving the existing tool untouched.
Two things remain.
[P2] general-purpose still re-enables a parent-denied hook tool. Same probe as round 1, only the factory changes: parent deny=["hook_extra"] with a hook contributing hook_extra → the gp child toolkit still contains hook_extra; the declared child is now clean. The root cause is that inheritedHookToolAllowlist is a per-call-site opt-in and buildGeneralPurposeFactory never passes it (decl.getTools() is never null, so the null branch is exactly "not a declared child"). This is pre-existing — gp inherited hooks before this PR — but it is the same mechanism and the same code, and #1578 uses gp as the reference behaviour. Please close it here rather than in a follow-up.
[Suggestion] the allowlist condition in the predicate is redundant, and removing it lets the fix cover all three paths. childToolsConfig already builds the child config as strictAllow=true / defaultToolsEnabled=false / allow = decl.tools ∩ parent policy / deny = parent.deny, so ToolFilter.isAllowed(name, childCfg) already means "declared and allowed by the parent". Dropping the extra condition, the builder field, the package-private setter and the factory call leaves all 15 harness cases and 5 core cases green with no test edits, and applying the filter unconditionally in buildInstance fixes declared + general-purpose + the root agent in one place (+4 / −31):
ToolsConfig hookToolPolicy = resolvedToolsConfig;
inner.hookToolFilter(toolName -> ToolFilter.isAllowed(toolName, hookToolPolicy));Without a tools.json, isAllowed(name, null) is true, so configs with no tool policy are unaffected. I ran the full core + harness suite with that patch: 0 failures, spotless clean. If you would rather keep the blast radius on subagents only, the fallback is one line in buildGeneralPurposeFactory (sub.inheritedHookToolAllowlist(List.of())) plus dropping the redundant &&.
Nits: the new HookToolsRegistrationTest cases use fully-qualified Mockito/Assertions while the file uses static imports; the package-private setter javadoc reads Internal: used by ... where asLeafSubagent() uses the "Package-private because only ... should ..." phrasing; and a debug/warn when a hook contribution is dropped would match ToolFilter.warnUnknown and save someone an afternoon.
ningmao-hlyz
left a comment
There was a problem hiding this comment.
Formalising the review above as a change request, since the general-purpose gap is the same defect this PR is meant to close.
Required
general-purposemust not re-enable a parent-denied hook tool. Probe and root cause are in the comment above; the fix is one line inbuildGeneralPurposeFactory(sub.inheritedHookToolAllowlist(List.of())) or, preferably, item 2.- Prefer the unified filter in
HarnessAgent.buildInstance—inner.hookToolFilter(toolName -> ToolFilter.isAllowed(toolName, resolvedToolsConfig))— withinheritedHookToolAllowlist(field, package-private setter, factory call) removed. The declaration allowlist is already encoded in the child'sToolsConfigbychildToolsConfig, so the extra&&is redundant, and one predicate fixes declared + general-purpose + the root agent: +4 / −31, full core + harness suite green,spotless:checkclean, and no behaviour change when notools.jsonis present.
The nits from the previous comment (static imports, setter javadoc phrasing, log on dropped contributions) can be handled inline while you are there.
When hookToolFilter rejects a contribution, the tool is silently absent from the built agent. Log the dropped name at WARN, matching the diagnostics ToolFilter.warnUnknown provides for unmatched tools.json entries.
Match the existing static imports in HookToolsRegistrationTest instead of fully-qualified Mockito and Assertions references.
Hook.tools() contributions were filtered only for declared children, so
the general-purpose subagent and the root agent could still install a tool
the effective ToolsConfig denied. They register after ToolFilter.apply, so
the deny list never saw them.
childToolsConfig already folds the declaration allowlist into the child's
strict ToolsConfig, so the extra allowlist condition was redundant. Apply
one predicate in HarnessAgent.buildInstance for every build:
toolName -> ToolFilter.isAllowed(toolName, resolvedToolsConfig)
A null config allows every contribution, preserving configs that define no
tool policy. Remove the per-call-site inheritedHookToolAllowlist field,
setter, and factory call.
Regression coverage: general-purpose and root agents now drop a denied hook
tool; the no-policy general-purpose case still installs it.
Hook-contributed tools are now filtered for the root agent and general-purpose as well, not only declared children. Describe the behavior by the effective ToolsConfig and note that declared children also encode the declaration allowlist in it.
|
Both required items are addressed, along with the nits. Unified filter. HarnessAgent.buildInstance now installs one predicate for every build: inheritedHookToolAllowlist (field, package-private setter, and the buildDeclaredFactory call) general-purpose and root. With the filter applied unconditionally, the gp child and the root
Nits.
Validation.
Docs now describe the behavior by the effective ToolsConfig across root, general-purpose, and |
ningmao-hlyz
left a comment
There was a problem hiding this comment.
Follow-up at 58f141a7. Both required items are in, the filter placement is right, and generalPurposeSubagentStillInstallsHookTools is the control I would have asked for. Core 605, harness 2,363 (52 skipped), Spotless clean, CI green.
[P1] A deny list that lives in tools.json never reaches a declared child. childToolsConfig reads toolsConfigOverride, which is null when the parent's policy comes from the workspace file, and the child never loads tools.json itself. Probe: tools.json {"deny":["hook_extra"]}, hook contributing hook_extra, declaration listing it → root rejects, general-purpose rejects, the declared child installs it. With .toolsConfig(cfg) all three reject. A declaration without a tools: list leaks too, and there it is not hook-specific: write_file denied in tools.json still lands in the child.
Pre-existing, those two code paths are untouched here. But the sentence you added to subagent.md states it without qualification.
I tried the small fix first and it does not stay small: hoisting the policy into a builder field fixes the declaration that lists tools:, not the one without a list, because the factories can be created before any build runs (buildSubagentEntries(Path) is public and documented for mounting factories on a session router) and buildStaticSubagentEntries has no workspace manager to load tools.json from. So narrow the sentence here to the declaration allowlist and programmatic config, and take workspace-policy propagation as its own change.
One nit: stray blank line after ToolsConfig toolsConfigOverride;, and the warning says hookToolFilter, an internal name where "tool policy" reads better.
AgentScope-Java Version
2.1.0-SNAPSHOT
Description
Fixes #1578 — declared subagents do not inherit the parent agent's hooks.
buildDeclaredFactory(used for both workspacesubagents/*.mdand programmaticSubagentDeclarationsubagents) captured and forwarded the parent's middlewaresbut never its hooks. The sibling
buildGeneralPurposeFactoryalready propagatedb.hooks, so the two paths were inconsistent: hooks registered on the parent fortool governance, human-in-the-loop approval, audit, or budget were silently
dropped for declared subagents.
This change captures
b.hooksinbuildDeclaredFactoryand applies them to thechild builder alongside the middlewares, mirroring
buildGeneralPurposeFactory.Changes
HarnessAgentBuilderSupport.buildDeclaredFactory: captureb.hooksand forwardvia
sub.hooks(capturedHooks)before building the child.ManagedSubagentBoundaryTest: add a regression test asserting a declaredsubagent inherits the parent's hooks.
How to test
mvn -T1C -pl agentscope-harness -am test -Dtest=ManagedSubagentBoundaryTestmvn -T1C -pl agentscope-harness -am test(1919 tests, 0 failures)Checklist
mvn spotless:applymvn test)