Skip to content

Fix #1543: fix: MEMOS不支持JSON5格式的openclaw.json - #1990

Merged
syzsunshine219 merged 2 commits into
dev-v2.0.22from
bugfix/autodev-1543
Jul 2, 2026
Merged

syzsunshine219 merged 2 commits into
dev-v2.0.22from
bugfix/autodev-1543

Conversation

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

Description

Fix issue #1543: memos-local-openclaw plugin now reads ~/.openclaw/openclaw.json as JSON5 instead of strict JSON.

Root cause: OpenClaw's host config is officially JSON5 (allows // and /* */ comments, single-quoted strings, unquoted identifier keys, trailing commas). The plugin previously called strict JSON.parse on the file in six places, including the tools.allow patcher that runs on every plugin startup — which is what produced the reported warning memos-local: could not patch tools.allow: SyntaxError: Expected double-quoted property name in JSON at position 2222 (line 90 column 7) whenever a user kept comments in their config.

Fix:

  1. New zero-dependency src/shared/json5.ts provides parseJsonOrJson5 — a strict-JSON fast path with a JSON5-tolerant fallback that strips comments via a string-respecting state machine, rewrites single quotes, quotes unquoted identifier keys, and tolerates trailing commas and UTF-8 BOM.
  2. New src/shared/openclaw-config.ts extracts the tools.allow patch logic into a unit-testable helper that anchors on both double- and single-quoted last entries and tolerates trailing commas. It never throws.
  3. All six JSON.parse(fs.readFileSync(<openclaw.json>...)) call sites are routed through the new helper: index.ts, src/shared/llm-call.ts, src/ingest/providers/index.ts, and six endpoints inside src/viewer/server.ts. Plain-JSON configs are unaffected thanks to the fast path.

Tests added (21 new, all green): tests/json5-config.test.ts covers the issue case, block comments, trailing commas, single/mixed quotes, unquoted keys, BOM, the classic "// inside a URL string literal" bug, malformed input, and an end-to-end exercise of loadOpenClawFallbackConfig. tests/openclaw-config-patch.test.ts covers the patcher across comments, single quotes, trailing commas, and the no-op paths (*, already-present, empty, malformed). Regression sweep across openclaw-fallback, config, viewer-config, storage, policy, capture, recall, chunker, migration-status, postinstall-native-binding — 105 tests pass; tsc --noEmit clean.

Related Issue (Required): Fixes #1543

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (does not change functionality, e.g. code style improvements, linting)
  • Documentation update

How Has This Been Tested?

Automated tests are pending.

  • Unit Test
  • Test Script Or Test Steps (please provide)
  • Pipeline Automated API Test (please provide)

Checklist

  • I have performed a self-review of my own code
  • I have commented my code in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • I have created related documentation issue/PR in MemOS-Docs (if applicable)
  • I have linked the issue to this PR (if applicable)
  • I have mentioned the person who will review this PR

@MatthewZhuang, @CarltonXiang, @syzsunshine219, @World-controller please review this PR.

Reviewer Checklist

OpenClaw stores its host config as JSON5 (// comments, single quotes,
trailing commas, unquoted identifier keys). The plugin previously read
the file via strict JSON.parse in 6 places, including the tools.allow
patcher that runs on every plugin startup. Users hitting any JSON5
syntax saw:

  warn plugins memos-local: could not patch tools.allow: SyntaxError:
  Expected double-quoted property name in JSON at position 2222

This change introduces:

* src/shared/json5.ts — zero-dependency JSON5-tolerant parser
  (parseJsonOrJson5 + parseJson5 + normalizeJson5). Tries strict
  JSON.parse first, falls back to a state-machine normalizer that
  strips // and /* */ comments (respecting string literals), converts
  single quotes, quotes unquoted identifier keys, and tolerates trailing
  commas and BOM.
* src/shared/openclaw-config.ts — extracts the tools.allow patcher into
  a testable helper, anchors on both double- and single-quoted last
  entries, tolerates trailing commas, never throws.

Replaces JSON.parse(fs.readFileSync(<openclaw.json>...)) at all 6 read
sites (index.ts, src/shared/llm-call.ts, src/ingest/providers/index.ts,
src/viewer/server.ts × 6). Plain JSON config files are unaffected by
the strict-JSON fast path.

Test coverage:
* tests/json5-config.test.ts — 14 cases (issue case, block comments,
  trailing commas, single/mixed quotes, unquoted keys, BOM, strings
  containing // and /* */, malformed, end-to-end via
  loadOpenClawFallbackConfig).
* tests/openclaw-config-patch.test.ts — 7 cases (patch w/ comments /
  single quotes / trailing commas; no-op when already permissive /
  empty; no throw on malformed).

vitest: 21 new tests + 84 regression tests pass; tsc --noEmit clean.
@Memtensor-AI Memtensor-AI added ai:generated Generated or modified by AI | 由 AI 生成或修改 types:bug Something isn't working | 功能异常 labels Jun 30, 2026
@Memtensor-AI

Copy link
Copy Markdown
Collaborator Author

❌ Automated Test Results: FAILED

Auto-fix retry 1/2 triggered.

Failed tests:

  • test_out_of_range_rejected_or_clamped_to_valid[negative_1]
  • test_out_of_range_rejected_or_clamped_to_valid[negative_60s]
  • test_out_of_range_rejected_or_clamped_to_valid[negative_one_day]
  • test_out_of_range_rejected_or_clamped_to_valid[max_plus_1]
  • test_out_of_range_rejected_or_clamped_to_valid[max_plus_one_day]
  • test_out_of_range_rejected_or_clamped_to_valid[hundred_x_max]
  • test_invalid_type_does_not_crash_or_corrupt[string_number]
  • test_invalid_type_does_not_crash_or_corrupt[string_text]
  • test_invalid_type_does_not_crash_or_corrupt[none_value]
  • test_invalid_type_does_not_crash_or_corrupt[dict_value]
Error details
The vector_scan_max_age config field accepts and persists invalid values (negative numbers, out-of-range integers, strings, null, dicts) without validation, clamping, or rejection. After a dirty PATCH, GET returns the raw invalid value instead of rejecting the write or clamping to [0, 31536000000]. AI-generated tests: 80/80 passed.

Branch: bugfix/autodev-1543

@Memtensor-AI
Memtensor-AI changed the base branch from dev-20260624-v2.0.22 to dev-v2.0.22 June 30, 2026 12:24
@Memtensor-AI

Copy link
Copy Markdown
Collaborator Author

Cloud verification update (test-engine v2): FAILED.

Run tr-7ba163e1-0e2 executed focused memos_local_openclaw/unit: 24/25 passed, 1 failed.

Failure: tests/update-install.test.ts expected process.kill(process.pid, "SIGUSR1") after successful postinstall, but no call was observed. This looks like a real regression against the update-install restart baseline, not an infra/no-scope issue. Needs code fix, then rerun.

@CarltonXiang CarltonXiang added the area:plugin OpenClaw & Hermes label Jul 2, 2026
@Memtensor-AI

Copy link
Copy Markdown
Collaborator Author

Automated Test Results: PASSED

Cloud test-engine rerun against dev-v2.0.22 completed successfully.

  • Run: tr-bbe5ce28-583 on cloud test-engine 10011
  • Scope: memos_local_openclaw/unit
  • Result: 31 passed, 0 failed, 0 skipped

This replaces the earlier failed signal for this PR. Manual code review is still required before merge.

@syzsunshine219
syzsunshine219 merged commit b50246b into dev-v2.0.22 Jul 2, 2026
16 checks passed
@syzsunshine219
syzsunshine219 deleted the bugfix/autodev-1543 branch July 2, 2026 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:generated Generated or modified by AI | 由 AI 生成或修改 area:plugin OpenClaw & Hermes types:bug Something isn't working | 功能异常

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants