Skip to content

fix(feishu): prevent duplicate tool registration on cache miss - #56520

Closed
bin448482 wants to merge 3 commits into
openclaw:mainfrom
bin448482:main
Closed

bin448482 wants to merge 3 commits into
openclaw:mainfrom
bin448482:main

Conversation

@bin448482

Copy link
Copy Markdown

Summary

  • Fix feishu plugin tools (doc, chat, wiki, drive, bitable) being repeatedly registered every second when loadOpenClawPlugins cache misses

Symptom

00:18:05 feishu_doc: Registered feishu_doc, feishu_app_scopes
00:18:06 feishu_doc: Registered feishu_doc, feishu_app_scopes  ← repeated every second
00:18:07 feishu_doc: Registered feishu_doc, feishu_app_scopes
...

Root Cause

When loadOpenClawPlugins (in src/plugins/loader.ts) misses its cache, it recreates the registry and calls registerFull(api) again. The feishu plugin's registerFull had no idempotency guard, so tools get re-registered on every cache miss.

Fix

Add a module-level guard in extensions/feishu/index.ts to ensure tools are only registered once.

Note: This is a workaround. The real fix should be in loadOpenClawPlugins cache logic to prevent cache misses in the first place, or ensure registerFull is only called once per plugin lifetime.

Cherry-pick from upstream d22279d:
- Add early return in ensureRuntimePluginsLoaded() if active plugin registry already exists
- Prevents redundant plugin re-registration on every agent message run

Fixes: feishu plugin duplicate registration bug
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling channel: feishu Channel integration: feishu size: XS labels Mar 28, 2026

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 92dbcc39d1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +58 to +61
if (feishuToolsRegistered) {
return;
}
feishuToolsRegistered = true;

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.

P1 Badge Remove global Feishu registration latch on reload

The module-level feishuToolsRegistered flag persists across plugin-registry rebuilds, so after any cache miss/config change loadOpenClawPlugins will clear all plugin commands (clearPluginCommands() in src/plugins/loader.ts) and then registerFull returns early here without re-registering Feishu tools. This leaves Feishu tools missing for the rest of the process lifetime after the first real reload.

Useful? React with 👍 / 👎.

Comment on lines +11 to +13
if (getActivePluginRegistryKey()) {
return;
}

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.

P1 Badge Keep runtime plugin load keyed by workspace/options

This early return makes ensureRuntimePluginsLoaded effectively one-shot: once any registry key is set, later calls with a different workspaceDir or allowGatewaySubagentBinding never invoke loadOpenClawPlugins. That bypasses the loader’s cache-key logic (which explicitly includes workspace and runtime subagent mode), so long-lived processes can run with a stale plugin registry for subsequent sessions/workspaces.

Useful? React with 👍 / 👎.

@greptile-apps

greptile-apps Bot commented Mar 28, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds two layered guards to prevent feishu plugin tools from being re-registered every second when loadOpenClawPlugins experiences a cache miss. The upstream fix in runtime-plugins.ts (using the globally-shared registry key) is clean and robust; the secondary module-level boolean guard in extensions/feishu/index.ts is an acknowledged workaround with a reliability edge case.

  • src/agents/runtime-plugins.ts: The getActivePluginRegistryKey() guard is a solid idempotency check — it leverages the Symbol.for-based global state and will correctly skip re-loading once the registry is active, regardless of module boundaries.
  • extensions/feishu/index.ts: The feishuToolsRegistered boolean is a module-scoped variable. If a fresh plugin registry is ever built (distinct api object) while the feishu module remains in jiti's cache, registerFull will silently no-op and the new registry will be missing all feishu tools. The PR description explicitly calls this out as a workaround; a WeakSet<object> keyed on api would make the guard registry-aware rather than process-lifetime-scoped.
  • package.json: discord-api-types is added to root devDependencies; this appears unrelated to the feishu fix and discord-api-types is already declared in extensions/discord/package.json.

Confidence Score: 4/5

Safe to merge for the immediate regression, but the feishu guard can silently drop tools if the registry is rebuilt while the module is cached.

The upstream guard in runtime-plugins.ts is clean and addresses the root symptom. The feishuToolsRegistered module-level boolean is a known workaround (acknowledged in the PR) that could silently produce a registry with no feishu tools if registerFull is called with a new api after the flag is already set. This is a present correctness risk on the changed path, warranting a 4 rather than 5.

extensions/feishu/index.ts — the module-level boolean guard; consider a WeakSet keyed on the api object to be registry-aware.

Important Files Changed

Filename Overview
extensions/feishu/index.ts Adds a module-level boolean guard to prevent registerFull from re-registering feishu tools on repeated calls. Effective when the same jiti module instance is reused, but silently drops tool registration if the registry is rebuilt with a new api object while the module is already loaded.
src/agents/runtime-plugins.ts Adds an idempotency guard using getActivePluginRegistryKey() — the global Symbol-based state — to short-circuit ensureRuntimePluginsLoaded once the registry is active. This is the cleaner upstream fix.
package.json Adds discord-api-types to root devDependencies; unrelated to the feishu fix and already declared in extensions/discord/package.json.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: extensions/feishu/index.ts
Line: 49-61

Comment:
**Module-level guard prevents re-registration in a fresh registry**

`feishuToolsRegistered` is a plain module-level variable scoped to a single jiti module instance. If the plugin registry is legitimately rebuilt (e.g., after a state reset that sets `state.key = null`) while the same jiti context is reused, `registerFull` will return early and the new registry will silently contain **no feishu tools**.

In contrast, the `Symbol.for("openclaw.pluginRegistryState")` global in `src/plugins/runtime.ts` is explicitly designed to survive across module instances. A more robust guard here would tie the registration to the registry identity rather than a static boolean:

```ts
const registeredApis = new WeakSet<object>();

registerFull(api) {
  if (registeredApis.has(api)) {
    return;
  }
  registeredApis.add(api);
  // ...
}
```

This way the guard is scoped to the specific `api` object, so a fresh registry (different `api`) will always get its tools registered, while duplicate calls on the same registry are still no-ops. The PR description already acknowledges this is a workaround — the above pattern would make the workaround less likely to silently misbehave under a registry rebuild.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: package.json
Line: 653

Comment:
**Unrelated dependency added**

`discord-api-types` is already declared as a direct dependency in `extensions/discord/package.json`. Adding it here to the root workspace `devDependencies` is unrelated to the feishu duplicate-registration fix and should probably be a separate PR unless there's a specific root-level test or tooling reason for it.

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix(feishu): prevent duplicate tool regi..." | Re-trigger Greptile

Comment on lines +49 to +61
let feishuToolsRegistered = false;

export default defineChannelPluginEntry({
id: "feishu",
name: "Feishu",
description: "Feishu/Lark channel plugin",
plugin: feishuPlugin,
setRuntime: setFeishuRuntime,
registerFull(api) {
if (feishuToolsRegistered) {
return;
}
feishuToolsRegistered = true;

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.

P1 Module-level guard prevents re-registration in a fresh registry

feishuToolsRegistered is a plain module-level variable scoped to a single jiti module instance. If the plugin registry is legitimately rebuilt (e.g., after a state reset that sets state.key = null) while the same jiti context is reused, registerFull will return early and the new registry will silently contain no feishu tools.

In contrast, the Symbol.for("openclaw.pluginRegistryState") global in src/plugins/runtime.ts is explicitly designed to survive across module instances. A more robust guard here would tie the registration to the registry identity rather than a static boolean:

const registeredApis = new WeakSet<object>();

registerFull(api) {
  if (registeredApis.has(api)) {
    return;
  }
  registeredApis.add(api);
  // ...
}

This way the guard is scoped to the specific api object, so a fresh registry (different api) will always get its tools registered, while duplicate calls on the same registry are still no-ops. The PR description already acknowledges this is a workaround — the above pattern would make the workaround less likely to silently misbehave under a registry rebuild.

Prompt To Fix With AI
This is a comment left during a code review.
Path: extensions/feishu/index.ts
Line: 49-61

Comment:
**Module-level guard prevents re-registration in a fresh registry**

`feishuToolsRegistered` is a plain module-level variable scoped to a single jiti module instance. If the plugin registry is legitimately rebuilt (e.g., after a state reset that sets `state.key = null`) while the same jiti context is reused, `registerFull` will return early and the new registry will silently contain **no feishu tools**.

In contrast, the `Symbol.for("openclaw.pluginRegistryState")` global in `src/plugins/runtime.ts` is explicitly designed to survive across module instances. A more robust guard here would tie the registration to the registry identity rather than a static boolean:

```ts
const registeredApis = new WeakSet<object>();

registerFull(api) {
  if (registeredApis.has(api)) {
    return;
  }
  registeredApis.add(api);
  // ...
}
```

This way the guard is scoped to the specific `api` object, so a fresh registry (different `api`) will always get its tools registered, while duplicate calls on the same registry are still no-ops. The PR description already acknowledges this is a workaround — the above pattern would make the workaround less likely to silently misbehave under a registry rebuild.

How can I resolve this? If you propose a fix, please make it concise.

Comment thread package.json
"@types/ws": "^8.18.1",
"@typescript/native-preview": "7.0.0-dev.20260317.1",
"@vitest/coverage-v8": "^4.1.0",
"discord-api-types": "^0.38.42",

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.

P2 Unrelated dependency added

discord-api-types is already declared as a direct dependency in extensions/discord/package.json. Adding it here to the root workspace devDependencies is unrelated to the feishu duplicate-registration fix and should probably be a separate PR unless there's a specific root-level test or tooling reason for it.

Prompt To Fix With AI
This is a comment left during a code review.
Path: package.json
Line: 653

Comment:
**Unrelated dependency added**

`discord-api-types` is already declared as a direct dependency in `extensions/discord/package.json`. Adding it here to the root workspace `devDependencies` is unrelated to the feishu duplicate-registration fix and should probably be a separate PR unless there's a specific root-level test or tooling reason for it.

How can I resolve this? If you propose a fix, please make it concise.

@bin448482

Copy link
Copy Markdown
Author

Superseded by #65398.

I opened a new PR from a dedicated branch so the follow-up Feishu fix could land without rewriting my fork's main branch. The new PR includes both the duplicate registration guard and the named-account groupPolicy fix that restored live Feishu group replies.

Closing this PR in favor of #65398.

@bin448482 bin448482 closed this Apr 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling channel: feishu Channel integration: feishu size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant