Skip to content

Commit efad29d

Browse files
committed
fix: register retry with Map + resetRegistration clear + test (#448)
1 parent cb32130 commit efad29d

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,11 +1618,16 @@ const pluginVersion = getPluginVersion();
16181618
// WeakSet keyed by API instance — each distinct API object tracks its own initialized state.
16191619
// Using WeakSet instead of a module-level boolean avoids the "second register() call skips
16201620
// hook/tool registration for the new API instance" regression that rwmjhb identified.
1621-
const _registeredApis = new WeakSet<OpenClawPluginApi>();
1621+
const _registeredApis = new Map<OpenClawPluginApi, boolean>();
16221622

16231623
// Tracks whether register() has ever completed successfully (for retry after failure)
16241624
let _initialized = false;
16251625

1626+
// Helper for tests to check registered APIs
1627+
export function _getRegisteredApisForTest(): Map<OpenClawPluginApi, boolean> {
1628+
return _registeredApis;
1629+
}
1630+
16261631
const memoryLanceDBProPlugin = {
16271632
id: "memory-lancedb-pro",
16281633
name: "Memory (LanceDB Pro)",
@@ -1636,7 +1641,7 @@ const memoryLanceDBProPlugin = {
16361641
api.logger.debug?.("memory-lancedb-pro: register() called again — skipping re-init (idempotent)");
16371642
return;
16381643
}
1639-
_registeredApis.add(api);
1644+
// Note: Map.set is called AFTER successful init in try block
16401645

16411646
try {
16421647
// Parse and validate configuration
@@ -3852,10 +3857,12 @@ const memoryLanceDBProPlugin = {
38523857
});
38533858

38543859
// All initialization completed successfully: mark success.
3860+
// 只有初始化完全成功才標記為已註冊
3861+
_registeredApis.set(api, true);
38553862
_initialized = true;
38563863
} catch (err) {
3857-
// init 失敗:_initialized 仍為 false,下次不同 instance 可重試
3858-
// WeakSet 沒加入,該 instance 不會被錯誤 block
3864+
// init 失敗:_registeredApis 沒加入,該 api instance 可重試
3865+
// _initialized 仍為 false,下次不同 instance 可重試
38593866
throw err;
38603867
}
38613868
},
@@ -4148,10 +4155,8 @@ export function parsePluginConfig(value: unknown): PluginConfig {
41484155
* @public
41494156
*/
41504157
export function resetRegistration() {
4151-
// Note: WeakSets cannot be cleared by design. In test scenarios where the
4152-
// same process reloads the module, a fresh module state means a new WeakSet.
4153-
// For hot-reload scenarios, the module is re-imported fresh.
4154-
// (WeakSet.clear() does not exist, so we do nothing here.)
4158+
// Clear the Map to allow re-registration after failure or hot-reload
4159+
_registeredApis.clear();
41554160
_initialized = false;
41564161
}
41574162

0 commit comments

Comments
 (0)