Skip to content

fix: add isInvalidAgentIdFormat guard to runMemoryReflection (Issue #686)#687

Open
jlin53882 wants to merge 7 commits intoCortexReach:masterfrom
jlin53882:fix/issue-686-missing-guard
Open

fix: add isInvalidAgentIdFormat guard to runMemoryReflection (Issue #686)#687
jlin53882 wants to merge 7 commits intoCortexReach:masterfrom
jlin53882:fix/issue-686-missing-guard

Conversation

@jlin53882
Copy link
Copy Markdown
Contributor

Fix: Add isInvalidAgentIdFormat guard to runMemoryReflection (Issue #686)

Summary

Adds a missing isInvalidAgentIdFormat() guard to runMemoryReflection, making it consistent with the other 5 hook sites that all have this check.

Changes

index.tsrunMemoryReflection command hook:

  const sourceAgentId = parseAgentIdFromSessionKey(sessionKey) || "main";
+ // Guard: skip if agentId is invalid format (consistent with other hook sites)
+ if (isInvalidAgentIdFormat(sourceAgentId, config.declaredAgents)) {
+   api.logger.debug?.(
+     `memory-reflection: command hook skipped \u2014 invalid agentId '${sourceAgentId}'`,
+   );
+   return;
+ }
  // Exclude agents/sessions listed in memoryReflection.excludeAgents (supports wildcards)

Why this matters

All other hook sites in PR #516 have isInvalidAgentIdFormat() as a 前置 guard:

  • before_prompt_build (auto-recall) — ✅ has guard
  • before_prompt_build (recallWork) — ✅ has guard
  • before_prompt_build (auto-capture) — ✅ has guard
  • before_prompt_build (reflection inheritance) — ✅ has guard
  • before_prompt_build (reflection derived+error) — ✅ has guard
  • before_reset — ✅ has guard
  • runMemoryReflection — ✅ now has guard (this PR)

Currently protected by the || "main" fallback, but the explicit guard is the correct defense-in-depth pattern.

Dependencies

This PR builds on #516. It should be reviewed and merged after #516.

Links

jlin53882 and others added 7 commits April 22, 2026 13:32
…ebase)

This rebases the following fixes from PR CortexReach#516 onto upstream/master (0988a46):

F2 (excludeAgents runtime reading):
- Add isAgentOrSessionExcluded() helper supporting exact/wildcard/temp:* patterns
- Add memoryReflection.excludeAgents to PluginConfig and openclaw.plugin.json schema
- Add excludeAgents check in runMemoryReflection command hook

F3 (wildcard pattern fix):
- Replace config.autoRecallExcludeAgents.includes(agentId) with
  isAgentOrSessionExcluded() in before_prompt_build hook
- Supports pi-, temp:*, and exact match patterns

F5 (serialCooldownMs configurable):
- Add serialCooldownMs?: number to PluginConfig.memoryReflection
- Serial guard now reads cooldown from cfg.memoryReflection.serialCooldownMs
- Default: 120000ms (2 min), set to 0 to disable

Schema additions (openclaw.plugin.json):
- memoryReflection.serialCooldownMs (integer, min: 0)
- memoryReflection.excludeAgents (string array)
- autoRecallExcludeAgents (string array, top-level)

EF1 (backtick fix already present in upstream 0988a46)
…eclaredAgents validation

- Add isChatIdBasedAgentId() helper: pure-digit IDs (e.g. "657229412030480397")
  are almost always chat_id extractions and cause 60s auto-recall timeout
- Add isInvalidAgentIdFormat() with three-layer guard: empty check → numeric
  check → declaredAgents Set lookup (authoritative, from openclaw.json)
- Add declaredAgents Set (IIFE) populated from cfg.agents.list in config return
- Add guard to all 6 hook sites: auto-recall entry, recallWork inner,
  auto-capture (agent_end), reflection inheritance, reflection derived+error,
  before_reset
新增 test/agentid-validation.test.mjs,覆蓋 Issue CortexReach#492 的修復邏輯:

測試內容:

1. Layer 1(空值檢查)
   - undefined / null / "" → invalid

2. Layer 2(純數字 = chat_id)
   - "657229412030480397" → invalid(這就是導致 60s timeout 的元兇)
   - "dc-channel--1476858065914695741" → NOT invalid(有字母前綴,正確)
   - "tg-group--5108601505" → NOT invalid

3. Layer 3(declaredAgents Set)
   - "main" 在清單中 → valid
   - 不在清單中的隨機字串 → invalid
   - declaredAgents 為空時 → 不主動阻擋

4. Regex 迴歸測試
   - 13 個邊界案例全部驗證通過

同時更新 ci-test-manifest.mjs,將新測試加入 core-regression 測試群組。

根因對照:
Issue CortexReach#492 的根本原因是 numeric chat_id(如 657229412030480397)被當成
agentId 傳入 LanceDB,導致 retriever.test() timeout。本測試確保:
- 純數字 ID(Layer 2)被正確攔截
- 有效的 agent ID(dc-channel-- / tg-group--)不受影響
- declaredAgents Set 白名單邏輯正確
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

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: a1b3bd46f2

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread index.ts
})(),
errorReminderMaxEntries: parsePositiveInt(memoryReflectionRaw.errorReminderMaxEntries) ?? DEFAULT_REFLECTION_ERROR_REMINDER_MAX_ENTRIES,
dedupeErrorSignals: memoryReflectionRaw.dedupeErrorSignals !== false,
serialCooldownMs: parsePositiveInt(memoryReflectionRaw.serialCooldownMs) ?? DEFAULT_SERIAL_GUARD_COOLDOWN_MS,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Allow zero serial cooldown to disable reflection throttle

memoryReflection.serialCooldownMs is documented as “set to 0 to disable,” but this parser uses parsePositiveInt, which rejects 0 and falls back to the default 120000 ms. In configs that explicitly set serialCooldownMs: 0, reflection still remains throttled for two minutes, so command-hook reflections are unexpectedly skipped instead of running every trigger.

Useful? React with 👍 / 👎.

@jlin53882
Copy link
Copy Markdown
Contributor Author

CI 失敗分析(3 個 Job 均無關聯 PR #687

失敗摘要

Job 錯誤檔案 根因 是否關聯 PR #687
core-regression test/smart-extractor-branches.mjs:497 測試斷言失敗 ❌ 檔案無變動
storage-and-schema smart-extractor.ts:444 - this.store.bulkStore is not a function mock 缺少 bulkStore ❌ 測試 mock 既有問題
packaging-and-workflow scripts/verify-ci-test-manifest.mjs:60 manifest 未列入 test/import-markdown/ ❌ CI 設定既有問題

關鍵觀察

三個 Job 的 Test step 都失敗在同一個 merge commit (011d344841633aa) 上,這是 PR #687parent commit(merge base),並非 PR 自己引入的變更。PR #687 只修改了 index.ts,與上述三個測試檔案完全無交集。

建議

建議開獨立 issue 追蹤這三個 upstream 既有問題,或在 upstream/master 上驗證這些失敗是否已存在後再合併 PR #687

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