Skip to content

fix(js): skip re-registering tools already registered in the same registry - #6387

Closed
aslamalkarywk7 wants to merge 2 commits into
genkit-ai:mainfrom
aslamalkarywk7:fix-tool-dynamic-reregister
Closed

aslamalkarywk7 wants to merge 2 commits into
genkit-ai:mainfrom
aslamalkarywk7:fix-tool-dynamic-reregister

Conversation

@aslamalkarywk7

Copy link
Copy Markdown

Fixes #6381

A v2 plugin init() may return tool() actions, which bypass defineTool() and stay flagged dynamic while already registered. Passing them to ai.generate({ tools: [...] }) made maybeRegisterDynamicTools re-register them every time, tripping already-registered errors for what is a no-op overwrite.

This skips re-registration when the action is already registered in the same registry (t.__registry === registry), exports maybeRegisterDynamicTools (matching its middleware sibling), and adds unit tests (registers new, skips same-registry, still registers into a different registry).

Note: I chose the register-path guard over exporting a new plugin-facing tool factory — happy to switch if you prefer the factory.

…istry (WIP, awaiting issue approval)

Do not open PR before maintainer approval on #6381.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request exports and updates maybeRegisterDynamicTools to prevent re-registering dynamic tools that are already registered, addressing a bug where 'already registered' errors are thrown. It also adds corresponding unit tests. The review feedback correctly identifies that since generate() wraps the registry in a child registry, the current strict equality check on the registry will bypass the guard. The reviewer suggests traversing the parent registry chain to resolve this and adding a unit test to cover the parent-child registry scenario.

Comment thread js/ai/src/generate.ts Resolved
Comment thread js/ai/tests/generate/generate_test.ts Resolved
generate() wraps the registry in a child registry, so a strict
equality check against the tool's __registry always misses for
tools registered in the parent. Walk the parent chain instead and
cover it with a parent-registry unit test.

Signed-off-by: aslamalkarywk7 <[email protected]>
@aslamalkarywk7

Copy link
Copy Markdown
Author

Hi maintainers - the bot feedback here is already addressed on the current head: maybeRegisterDynamicTools now walks the parent registry chain instead of a strict equality check (generate() wraps the registry in a child), covered by a new parent-registry unit test.

Verified locally: 33/33 tests pass in js/ai/tests/generate/generate_test.ts. Could a human reviewer please take a look when you get a chance? Thanks!

@aslamalkarywk7 aslamalkarywk7 closed this by deleting the head repository Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tool() leaves metadata.dynamic set, so plugin-registered tools are re-registered on generate

1 participant