Skip to content

feat(phase2): Singleton State Management (PR #598, 4 commits)#637

Open
jlin53882 wants to merge 5 commits intoCortexReach:masterfrom
jlin53882:feat/phase2-singleton-v3
Open

feat(phase2): Singleton State Management (PR #598, 4 commits)#637
jlin53882 wants to merge 5 commits intoCortexReach:masterfrom
jlin53882:feat/phase2-singleton-v3

Conversation

@jlin53882
Copy link
Copy Markdown
Contributor

Phase 2: Singleton State Management

Split into 4 clean commits for reviewer convenience.

Commits

# Commit Description
1 0a74a95 Add PluginSingletonState interface + _singletonState variable + empty _initPluginState() skeleton
2 dc02799 Replace register() init block with singleton guard + 18-property destructuring
3 2bbab76 Fill _initPluginState() body with all resources (store, embedder, tierManager, retriever, 8 Maps, etc.)
4 9c466ab Remove 8 old duplicate Map declarations from helpers section

Motivation

The old register() function ran a ~140-line init block on every call. In multi-agent processes with 9 scope registrations per lifecycle, this created duplicate heavy resources (stores, embedders, tier managers) and accumulated session Maps across re-registrations.

Solution

  • Extract all resource creation into _initPluginState(api) factory function
  • Guard with if (!_singletonState) — first call initializes, subsequent calls reuse
  • 18 singleton state properties: 10 resources + 8 session Maps
  • Helpers access via destructuring (Phase 1 dedup hooks unchanged)

Testing

  • CLI smoke: openclaw memory-pro stats
  • Phase 1 dedup hooks: _hookEventDedup / _dedupHookEvent remain intact
  • Session Maps appear exactly once in _initPluginState return

Files Changed

  • index.ts: single file refactor, no new tests required for infrastructure change

Notes

  • Intermediate commits (C2) intentionally leave factory body as return null! — breaks TypeScript compilation until C3 fills it. This is expected for the commit-split strategy.
  • Final state (after all 4 commits): 4246 lines, 1013/1013 balanced braces

…variable (PR CortexReach#598)

- Add PluginSingletonState interface: defines all 18 properties returned by the
  singleton factory (8 heavy resources + 8 session Maps with their types)
- Add module-level _singletonState: null until first register() call
- Add _initPluginState(api) factory skeleton: returns PluginSingletonState

This commit only adds the interface and skeleton.
The factory body is empty (returns null!) and will be filled in subsequent commits.
…ll (PR CortexReach#598)

- Replace the old init block (config, store, embedder, tierManager, retriever,
  scopeManager, migrator, smartExtractor, extractionRateLimiter) with:
  - if (!_singletonState) guard that calls _initPluginState()
  - destructuring of all 18 properties from _singletonState
- The factory _initPluginState() body is still empty (returns null!) —
  filled in the next commit
- Helpers section is unchanged but will reference undefined values
  until the factory body is added in the next commit (expected state)
…ers (PR CortexReach#598)

The helpers section had 8 Map declarations that are now created once in
_singletonState and accessed via destructuring. Remove them to prevent
duplicate declarations that would cause TypeScript errors.

Maps removed from helpers:
- reflectionErrorStateBySession
- reflectionDerivedBySession
- reflectionByAgentCache
- recallHistory
- turnCounter
- autoCaptureSeenTextCount
- autoCapturePendingIngressTexts
- autoCaptureRecentTexts
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

migrator was returned by _initPluginState() but missing from the
singleton destructuring, causing ReferenceError at createMemoryCLI()
call site (index.ts:2286).

This was the root cause of CI failures in PR CortexReach#598.
@jlin53882
Copy link
Copy Markdown
Contributor Author

CI 狀態說明

migrator is not defined (已修補 ✅)

  • 根因migrator 有在 _initPluginState() return,但漏在了 singleton destructuring(commit c7ecd3a 已修補)
  • 影響:8 個測試全摔在 createMemoryCLI() call site

剩餘 CI 失敗(與本 PR 無關)

以下 job 失敗屬於 upstream 既有问题,不是 singleton refactor 引進:

  • core-regression / storage-and-schemarecall-text-cleanup.test.mjs 4 subtests AssertionError
    • upstream master branch 最近一次 push CI 也同樣失敗(fix: stripEnvelopeMetadata multiline wrapper regression commit)
    • cli-smoke / llm-clients-and-auth / version-sync 皆 ✅ PASS

建議:单独开 issue 追踪 recall-text-cleanup.test.mjs regression,暂不影响本 PR merge。

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.

1 participant