Skip to content

fix(plugin): add fallback for runtime.state.resolveStateDir on OpenClaw >=2026.5.x#79

Closed
leoge007 wants to merge 1 commit into
Tencent:mainfrom
leoge007:fix/runtime-state-undefined
Closed

fix(plugin): add fallback for runtime.state.resolveStateDir on OpenClaw >=2026.5.x#79
leoge007 wants to merge 1 commit into
Tencent:mainfrom
leoge007:fix/runtime-state-undefined

Conversation

@leoge007
Copy link
Copy Markdown

Summary

Fixes #78

Problem

TDB 0.3.5 fails to register on OpenClaw 2026.5.20 because api.runtime.state is undefined during plugin registration:

TypeError: Cannot read properties of undefined (reading 'resolveStateDir')

The TypeScript types declare runtime.state exists, but at registration time on 2026.5.x the object isn't yet populated.

Fix

Add optional chaining with os.homedir() fallback at the two call sites.

resolveStateDir() returns ~/.openclaw (or $OPENCLAW_STATE_DIR), so the fallback is well-defined and has no side effects.

Testing

  • ✅ Tested on OpenClaw 2026.5.20 with macOS 15.5 (Apple Silicon)
  • ✅ Plugin registers successfully
  • tdai_memory_search and tdai_conversation_search work correctly
  • ✅ CLI commands register (openclaw memory-tdai --help)

Changes

2 lines changed in index.ts:

  • L220: const pluginDataDir = path.join((api.runtime.state?.resolveStateDir ? ... : require('os').homedir() + '/.openclaw'), "memory-tdai");
  • L826: stateDir: (api.runtime.state?.resolveStateDir ? ... : require('os').homedir() + '/.openclaw'),

…aw >=2026.5.x

On OpenClaw 2026.5.20, api.runtime.state is undefined during plugin
registration, causing memory-tencentdb to fail register with
'Cannot read properties of undefined (reading resolveStateDir)'.

Add optional chaining with os.homedir() fallback so the plugin can
resolve its data directory even when the runtime.state API isn't
yet populated at registration time.

Fixes Tencent#78
@Maxwell-Code07
Copy link
Copy Markdown
Collaborator

Thanks for the fix and the thorough testing! The root cause analysis is clear and the change is well-scoped. We'll review internally and get back to you with feedback.

@YOMXXX
Copy link
Copy Markdown
Contributor

YOMXXX commented May 24, 2026

Reviewer triage notes from a local pass:

Verification I ran locally on this branch:

Functional risk to address before merge:

  • The fallback branch uses bare require('os') inside index.ts, which is an ESM source file and is also shipped as source today. This fallback is exactly the path that runs when api.runtime.state is missing, so source-mode execution can turn the original error into ReferenceError: require is not defined.

Suggested safer shape:

import { homedir } from "node:os";

function resolveOpenClawStateDir(api: OpenClawPluginApi): string {
  return api.runtime.state?.resolveStateDir?.() ?? path.join(homedir(), ".openclaw");
}

Then reuse that helper for both pluginDataDir and CLI stateDir. This keeps the fix small, avoids duplicated expressions, and is safe in ESM/source runtime.

@YOMXXX
Copy link
Copy Markdown
Contributor

YOMXXX commented May 24, 2026

I opened #85 as an ESM-safe alternative for the same #78 failure mode. It keeps the fallback small, adds a focused unit test, and CI is green including Size Guard. Reviewers can compare against this PR when deciding which path to merge.

@Xuruida
Copy link
Copy Markdown
Collaborator

Xuruida commented May 25, 2026

Thanks a lot for the fix and the detailed investigation! 🙏

We’ve reviewed both approaches (#79 vs #85) internally, and we’re going to move forward with #85 as the canonical fix.

The main reasons are:

  • Test coverage: fix(plugin): add OpenClaw state dir fallback #85 includes solid unit tests covering the require() / ESM edge cases, which gives us higher confidence for long-term stability.
  • Architecture alignment: It provides a cleaner ESM-safe implementation and avoids mixing CommonJS semantics in our source ESM context.

That said, the diagnosis here was spot-on and really helped us confirm the root cause quickly.
Closing this PR in favor of #85.

Thanks again @leoge007 and @YOMXXX for driving this forward — really appreciate the collaboration! 🚀

@Xuruida Xuruida closed this May 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: runtime.state.resolveStateDir undefined on OpenClaw >=2026.5.x

4 participants