Skip to content

fix(memory): read published text the way it is written, and count what is dropped - #592

Open
XiaoBinGan wants to merge 4 commits into
simplexai-labs:mainfrom
XiaoBinGan:fix/memory-read-and-count-edges
Open

XiaoBinGan wants to merge 4 commits into
simplexai-labs:mainfrom
XiaoBinGan:fix/memory-read-and-count-edges

Conversation

@XiaoBinGan

@XiaoBinGan XiaoBinGan commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem and what changes

Four readers in the memory path disagreed with the text they read, in ways a user can see:

Reader Text Before Now
Wiki graph aliases published frontmatter (YAML) every alias dropped, parse error swallowed read by the same OKF parser the entity registry uses, so the two cannot disagree on one page
Flashback line clip a line of astral characters cut inside a character, ellipsis replaced half of it cut between characters
Clipped memory source a 4 000-character source "N characters omitted" counted UTF-16 units, and the cut could split a surrogate pair counts characters, never splits one
Daily Memory card a report saved with CRLF frontmatter match failed, raw --- block shown read like the LF copy

Each was its own pull request (#447, #448, #453, #446); they are one here because they are the same defect family — code that assumed a unit or a line ending — and the review for each was the same question.

Evidence and validation

Base main at 2196b922; the branch is built from it. Node.js 26.5.0, pnpm 11.7.0. Part of #446, #447, #448 and #453.

  • New cases: graph-build.test.ts (aliases from YAML, JSON still parsed, and an unterminated block yields none), flashback-card-unicode.test.ts (a clip across astral characters), agent-memory-source-clip.test.ts (the omitted count is characters, no half surrogate), wiki-daily-card.test.ts (CRLF and LF produce the same card).
  • Negative control: each old implementation was restored in turn and its new case went red, then green again: the JSON-only parse returns [] for the YAML page (fix(memory): read the YAML aliases a Wiki page writes #447); the length-based clip ends inside a character (fix(memory): cut a flashback line between characters #448); the unit-based clipSourceText reports the wrong omitted count and keeps half a surrogate (fix(agent): count the characters a memory source drops #453); the LF-only frontmatter leaves the --- block in the body (fix(web): read a CRLF Daily Memory into the same card #446).
  • pnpm --filter @linger/node-services typecheck and @linger/web typecheck: no errors. A previous revision of this branch passed [] as buildWikiGraph's revision argument in the new test, which Vitest accepts but tsc rejects; the test now passes "wiki-r1" like its neighbours, and tsc -p apps/node-services/tsconfig.json --noEmit is clean. Suites: tests/memory + the clip case — 477 passed, 36 skipped; wiki-daily-card.test.ts — 4 passed.

Boundary

  • No contract, storage or schedule change; the four readers keep their signatures.
  • apps/node-services/src/memory/README.md is unchanged because the behaviour the operators read is the same: the same files are read, now correctly.
  • Nothing adds a network round trip or a file walk.

Not verified here

  • The graph change was checked against fixture Markdown, not against a live published Wiki page.
  • The CRLF case is a string fixture, not a file written by Windows Notepad.
  • Locale rendering of the clipped text was not re-checked; only its length and boundaries.

Author readiness

  • Focused tests for the changed behavior
  • The fast checks of the touched apps (typecheck plus the suites above)
  • Contracts regenerated if contracts/openapi/ changed (not applicable)

Human owner: @XiaoBinGan

@XiaoBinGan
XiaoBinGan force-pushed the fix/memory-read-and-count-edges branch from d2eb8f6 to 725b681 Compare November 23, 2026 17:26

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approve with nits (1 P2)

Scope: base main, merge base 61abac3f; 4 commits, 8 files reviewed at 725b6810. Helper skills loaded: coderbook (sections 2, 3, 5). ponytail-review and karpathy-guidelines were not loaded; I applied their checklists from the review instructions. This review ran in a separate session.

Findings

P2

apps/node-services/src/memory/graph/build.ts:122: frontmatterAliases adds a second YAML frontmatter reader instead of reusing the shared one

The shared parser already exists: parseOkfFrontmatter in @linger/knowledge-format (packages/knowledge-format/src/index.ts:47), and node-services already depends on it (apps/node-services/package.json:37). It already reads aliases from the same projects/, clients/ and contacts/ entity pages (apps/node-services/src/llm-wiki/wiki-entity-registry.ts:36-47). After this PR, two YAML readers handle one field on the same files, and they treat edge cases differently:

  • The OKF parser splits on \r?\n and looks for a delimiter line that is exactly ---.
  • The new code uses indexOf("\n---") and slice(3), so it also accepts a line such as ---- or ---foo as the closing delimiter.

The memory browser uses yet another reader (memory/markdown.ts), which repairs unquoted colon scalars. I checked that yaml.parse("description: Session: notes\naliases:\n - A") throws Nested mappings are not allowed in compact mappings. The new reader then returns [], so Flashback drops every alias for that page. Whether the registry keeps those aliases depends on how OKF handles that input. Either way, the two readers can disagree on one page.

Fix: replace the hand-written block with

const aliases = parseOkfFrontmatter(raw).fields?.aliases;
return Array.isArray(aliases) ? aliases.filter((a): a is string => typeof a === "string") : [];

This deletes the delimiter slicing, the try/catch and the direct yaml import from this file. Graph aliases and the entity registry then share one definition of "frontmatter".

Verified as fine

  • Flashback clip (memory/flashback/card.ts:195): every caller passes a character limit (card.ts:67,102,126,179). Spreading the text into code points means the cut never splits a surrogate pair, and the result is limit characters long, ellipsis included. flashback-card-unicode.test.ts asserts both the length and that no lone surrogate remains.
  • clipSourceText (agent-memory/coordinator.ts:140): the cut stays in UTF-16 units, so the byte budget and the MAX_SOURCE_TEXT semantics are unchanged. The code steps back by one when the last kept unit is a high surrogate. The omitted count is code points: total minus kept. If maxText is 1 and the text starts with an astral character, the result is "" plus the note, with no crash. The only caller is clip (coordinator.ts:158), and the new export is used only by the test.
  • Web CRLF frontmatter (wiki-daily-card.tsx:72,85): both regexes accept \r?\n. description:(.*)$ with m stops before \r because . excludes line terminators in JS. The new test asserts that the CRLF card toEqual the LF card, so the section and bullet parsing downstream behaves the same. Allowing $ after the closing --- also covers a file that ends there, which is harmless.
  • Dependencies: yaml 2.9.0 was already a node-services dependency (apps/node-services/package.json:55), and no manifest or lockfile changed.
  • Repository rules: no CJK in added lines or commit messages; no contract, env or migration change; no cross-app imports; no work added on the user message → provider_ready path. The PR body's claims match the diff.
  • Commits: four atomic fix(...) commits, one per reader, each with its own test. No merge commits and no fixups.

Process notes

None. The branch has no merge commits. mergeStateStatus is BLOCKED, which only means it is waiting for required review.

CI

All checks that ran passed: repository-gate, web-static, both web tests shards and check (web); the other app checks were skipped by path. CI does not run the node-services suites (.github/workflows/ci.yml:14, where check-node-services is on the heavy lane). I ran them locally: pnpm --filter @linger/node-services exec vitest run tests/agent/agent-memory-source-clip.test.ts tests/memory/flashback-card-unicode.test.ts tests/memory/graph-build.test.ts → 3 files, 9 tests passed.

Automated review by Claude Code


Automated review of 725b681 by Claude Code.

@XiaoBinGan
XiaoBinGan force-pushed the fix/memory-read-and-count-edges branch from 725b681 to 6e8a057 Compare November 23, 2026 19:27

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Request changes (1 P0)

Scope: follow-up on 725b681, reviewing 6e8a057 against main (merge base d6a5ccf5), 4 commits, 8 files. Helper skills not loaded this run (coderbook / ponytail-review / karpathy-guidelines checklists applied from the review procedure). Ran in a separate session.

Previous findings

  • Graph aliases used a second frontmatter parser (P2): Resolved. apps/node-services/src/memory/graph/build.ts:121-129 now reads aliases through parseOkfFrontmatter, the same parser apps/node-services/src/llm-wiki/wiki-entity-registry.ts:36 uses; the JSON-only parser is gone.

Note: the previous head was based on an older main; git diff 725b681..HEAD includes unrelated main commits, so this review re-read the whole merge-base diff.

Findings

P0

  • apps/node-services/tests/memory/graph-build.test.ts:113 — the new test "reads aliases through the same parser the entity registry uses" passes [] as the second argument to buildWikiGraph(pages, wikiRevision: string) (apps/node-services/src/memory/graph/build.ts:35-38). tests/**/*.ts is in the typechecked project (apps/node-services/tsconfig.json), so pnpm --filter @linger/node-services typecheck fails:
    tests/memory/graph-build.test.ts(113,7): error TS2345: Argument of type 'never[]' is not assignable to parameter of type 'string'.
    
    Vitest still passes because it doesn't typecheck, which is probably why the PR body says typecheck was clean. check (node-services) can't pass as is. Fix: pass a revision string such as "wiki-r1", like the other cases in this file.

Verified as fine

  • Graph aliases: parseOkfFrontmatter(raw).fields ?? {} matches how wiki-entity-registry.ts:36 reads the same pages; @linger/knowledge-format is already a dependency of @linger/node-services (apps/node-services/package.json:37). The new YAML case and the unterminated-block case pass.
  • Flashback clip (apps/node-services/src/memory/flashback/card.ts:195-202): it splits by code points, so it never cuts inside a surrogate pair. All four call sites (card.ts:67,102,126,179) pass display limits, where counting characters is what's wanted.
  • Memory source clip (apps/node-services/src/agent/agent-features/agent-memory/coordinator.ts:139-152): the threshold stays in code units (so the size budget doesn't change), it backs off one unit when the cut would land after a high surrogate, and the omitted count is in code points. clip sends strings through it at every nesting level.
  • Daily card (apps/web/src/features/wiki/wiki-daily-card.tsx:72,85-88): the frontmatter regexes accept \r?\n and a closing --- at end of input. Downstream, ^description:(.*)$ with m treats \r as a line terminator in JS, so no stray \r is captured. The new test asserts that the CRLF and LF output are equal.
  • No CJK in the added lines or in any commit message. No contract, env or migration changes.

Process notes

  • None that block. There are no merge or fixup commits; the 4 commits each map to one of the four fixes.

CI

Still running when this review was written (changes, repository-gate and the web tests were pending; unrelated app checks skipped). Run locally: tsc -p apps/node-services/tsconfig.json --noEmit fails with the error above. The focused Vitest files (graph-build, flashback-card-unicode, agent-memory-source-clip) gave 10 passed, and wiki-daily-card.test.ts gave 4 passed.

Automated review by Claude Code


Automated review of 6e8a057 by Claude Code.

@XiaoBinGan
XiaoBinGan force-pushed the fix/memory-read-and-count-edges branch from 6e8a057 to eafd79f Compare November 23, 2026 19:37

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approve (0 findings)

Scope: follow-up on 6e8a057. Base main, merge base d6a5ccf5 (branch is up to date with origin/main); 4 commits, 8 files reviewed. Helper skills: coderbook loaded; ponytail-review and karpathy-guidelines were not loaded as separate skills, so I applied their checklists from the review instructions. Ran in a separate session.

Previous findings

  • P0 graph-build.test.ts passed [] as buildWikiGraph's revision: Resolved. Both new tests now pass "wiki-r1" (apps/node-services/tests/memory/graph-build.test.ts:90, :114). I ran tsc -p apps/node-services/tsconfig.json --noEmit locally and it is clean.

Findings

None.

Verified as fine

  • Graph aliases: frontmatterAliases now reads aliases with parseOkfFrontmatter (apps/node-services/src/memory/graph/build.ts:121-130). This is the parser the entity registry uses (apps/node-services/src/llm-wiki/wiki-entity-registry.ts:36). The parser splits on \r?\n, needs a closing --- line and parses the block as YAML (packages/knowledge-format/src/index.ts:47-87), so:
    • YAML frontmatter is now read correctly.
    • The existing JSON-frontmatter test still passes, because JSON is a YAML flow mapping (graph-build.test.ts:60-75).
    • An unterminated block yields no aliases.
    • @linger/knowledge-format is already a dependency of node-services (apps/node-services/package.json:37), so no new dependency is added.
  • Source clip: clipSourceText (coordinator.ts:140-152):
    • It still applies the budget in UTF-16 units, as before.
    • It backs off one unit when the cut lands right after a high surrogate, so the kept text never ends with half a character.
    • It counts the omitted part in code points. The tests cover a pair at the boundary, a pair cut in the middle, and plain ASCII.
    • The recursive clip delegates to it, so the history-JSON behaviour is otherwise unchanged.
  • Flashback clip: clip in apps/node-services/src/memory/flashback/card.ts:195-203 now counts and slices by code points. The tests cover both dailyReport (200) and experienceCard (360) callers.
  • Daily card: both frontmatter regexes accept \r?\n and a closing --- at end of input (apps/web/src/features/wiki/wiki-daily-card.tsx:72, :85-88). The description: match is safe under CRLF: in JS a multiline $ matches before \r, and . doesn't match it. The new test asserts the CRLF card deep-equals the LF one.
  • Repo rules:
    • No CJK in added lines or commit messages.
    • No contract, env or storage change.
    • No cross-app imports.
    • Each commit is one atomic fix with a body explaining it.
  • Claims in the PR description: "no contract, storage or schedule change" and "Nothing adds a network round trip or a file walk" are both backed by the diff.

Process notes

  • No merge commits and no fixup commits. The branch is not behind main.

CI

  • changes passes. repository-gate, web-static and web tests (1/2, 2/2) were still pending at review time. Every other check (*) job was skipped, and none listed covers node-services.
  • I ran these locally:
    • tsc --noEmit for node-services: clean.
    • vitest run on graph-build, flashback-card-unicode and agent-memory-source-clip: 10 passed.
    • The Web wiki-daily-card.test.ts: 4 passed.

Automated review by Claude Code


Automated review of eafd79f by Claude Code.

@XiaoBinGan
XiaoBinGan force-pushed the fix/memory-read-and-count-edges branch from eafd79f to fedc4fd Compare November 24, 2026 15:09

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approve (0 findings)

Scope: base main, merge-base 54c7a379; 4 commits, 8 files reviewed (the whole merge-base diff, not only the delta). Helper skills followed: coderbook (sections 2, 3, 5), ponytail-review (sections 1–3), karpathy-guidelines (sections 2–3). Ran in a separate session.

Previous findings

Follow-up on eafd79f. The previous review approved with no findings, so there is nothing to track. git range-diff shows the branch was rebased onto a newer main. Commits 1, 2 and 4 are identical. Commit 3 (f7c90dc5) differs from the earlier one only by where a blank line sits in coordinator.ts.

Findings

None.

Verified as fine

  • Wiki graph aliases (apps/node-services/src/memory/graph/build.ts:121-130):
    • parseOkfFrontmatter (packages/knowledge-format/src/index.ts:47-87) splits on \r?\n, needs an exact --- opening and closing line, and returns fields only when the YAML parses. So a page with no closing delimiter yields no aliases (covered by the new test).
    • JSON frontmatter is still read, since YAML flow mappings accept it; the existing JSON test still passes.
    • The entity registry uses the same parser (apps/node-services/src/llm-wiki/wiki-entity-registry.ts:36), and @linger/knowledge-format is already a dependency of node-services (apps/node-services/package.json:37). This removes a second parser rather than adding one.
  • Flashback clip (apps/node-services/src/memory/flashback/card.ts:195-203):
    • It keeps limit - 1 code points plus …, so the output is exactly limit characters and never ends on a lone surrogate.
    • Every caller (card.ts:67,102,126,179) passes a character limit, so the new unit matches what the callers expect.
  • Clipped memory source (apps/node-services/src/agent/agent-features/agent-memory/coordinator.ts:138-158):
    • The threshold is still in code units, so the size budget is unchanged.
    • The cut steps back one unit when the last kept unit is a high surrogate, so it never keeps half a pair.
    • The omitted count is characters(value) - characters(kept), which matches the note's wording.
    • clip keeps its signature, and both nativeRecord call sites (:286, :829) are unchanged.
  • Daily Memory card on CRLF (apps/web/src/features/wiki/wiki-daily-card.tsx:71-88):
    • The frontmatter regex accepts \r?\n, and also a closing --- at the end of the string.
    • In multiline mode, description:(.*)$ stops before \r (JS treats \r as a line terminator).
    • section uses \s*$, and plain collapses the trailing \r that items leaves after splitting on \n, so CRLF input gives the same card as LF. The new test asserts toEqual between the two.
  • Repository rules:
    • No contract, env or storage changes.
    • No new helpers that duplicate @linger/service-kit.
    • No CJK in added lines or in commit messages.
    • Doc comments explain the invariants.
    • No network round trip or file walk added.

Process notes

  • The branch is 7 commits behind main, and main requires up-to-date branches, so it needs a rebase before merge. None of those 7 commits touch the changed files, so the rebase should be clean.
  • No merge commits and no fixup commits.

CI

  • The checks that ran all pass: repository-gate, changes, web-static, check (web), web tests (1/2) and (2/2). The other app checks were skipped as out of scope.
  • CI does not run the node-services suite, so I ran it locally on fedc4fd:
    • vitest run on tests/agent/agent-memory-source-clip.test.ts, tests/memory/flashback-card-unicode.test.ts and tests/memory/graph-build.test.ts: 3 files, 10 tests passed.
    • tsc -p apps/node-services/tsconfig.json --noEmit: clean.

Automated review by Claude Code


Automated review of fedc4fd by Claude Code.

A Wiki page writes YAML, and the graph reader parsed it as JSON, so every
published alias was dropped with a silent parse failure. Part of simplexai-labs#447.
The clip counted UTF-16 units, so a line of astral characters was cut inside
one and the ellipsis replaced half a character. Part of simplexai-labs#448.
The note said how many UTF-16 units were removed while calling them
characters, and the cut could split a surrogate pair. Part of simplexai-labs#453.
The Daily card's frontmatter match required LF, so a report saved with CRLF on
Windows fell through to the raw text. Part of simplexai-labs#446.
@XiaoBinGan
XiaoBinGan force-pushed the fix/memory-read-and-count-edges branch from fedc4fd to 0287b83 Compare November 25, 2026 19:09
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