Repository navigation
fix(memory): read published text the way it is written, and count what is dropped - #592
XiaoBinGan wants to merge 4 commits into
Conversation
d2eb8f6 to
725b681
Compare
There was a problem hiding this comment.
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?\nand looks for a delimiter line that is exactly---. - The new code uses
indexOf("\n---")andslice(3), so it also accepts a line such as----or---fooas 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 islimitcharacters long, ellipsis included.flashback-card-unicode.test.tsasserts 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 theMAX_SOURCE_TEXTsemantics 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. IfmaxTextis 1 and the text starts with an astral character, the result is""plus the note, with no crash. The only caller isclip(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:(.*)$withmstops before\rbecause.excludes line terminators in JS. The new test asserts that the CRLF cardtoEqualthe 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:
yaml2.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_readypath. 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.
725b681 to
6e8a057
Compare
There was a problem hiding this comment.
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-129now readsaliasesthroughparseOkfFrontmatter, the same parserapps/node-services/src/llm-wiki/wiki-entity-registry.ts:36uses; 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 tobuildWikiGraph(pages, wikiRevision: string)(apps/node-services/src/memory/graph/build.ts:35-38).tests/**/*.tsis in the typechecked project (apps/node-services/tsconfig.json), sopnpm --filter @linger/node-services typecheckfails:Vitest still passes because it doesn't typecheck, which is probably why the PR body says typecheck was clean.tests/memory/graph-build.test.ts(113,7): error TS2345: Argument of type 'never[]' is not assignable to parameter of type 'string'.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 howwiki-entity-registry.ts:36reads the same pages;@linger/knowledge-formatis 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.clipsends 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?\nand a closing---at end of input. Downstream,^description:(.*)$withmtreats\ras a line terminator in JS, so no stray\ris 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.
6e8a057 to
eafd79f
Compare
There was a problem hiding this comment.
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.tspassed[]asbuildWikiGraph's revision: Resolved. Both new tests now pass"wiki-r1"(apps/node-services/tests/memory/graph-build.test.ts:90,:114). I rantsc -p apps/node-services/tsconfig.json --noEmitlocally and it is clean.
Findings
None.
Verified as fine
- Graph aliases:
frontmatterAliasesnow reads aliases withparseOkfFrontmatter(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-formatis 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
clipdelegates to it, so the history-JSON behaviour is otherwise unchanged.
- Flashback clip:
clipinapps/node-services/src/memory/flashback/card.ts:195-203now counts and slices by code points. The tests cover bothdailyReport(200) andexperienceCard(360) callers. - Daily card: both frontmatter regexes accept
\r?\nand a closing---at end of input (apps/web/src/features/wiki/wiki-daily-card.tsx:72,:85-88). Thedescription: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
changespasses.repository-gate,web-staticandweb tests (1/2, 2/2)were still pending at review time. Every othercheck (*)job was skipped, and none listed covers node-services.- I ran these locally:
tsc --noEmitfor node-services: clean.vitest runongraph-build,flashback-card-unicodeandagent-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.
eafd79f to
fedc4fd
Compare
There was a problem hiding this comment.
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 returnsfieldsonly 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-formatis 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 - 1code points plus…, so the output is exactlylimitcharacters 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.
- It keeps
- 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. clipkeeps its signature, and bothnativeRecordcall 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\ras a line terminator). sectionuses\s*$, andplaincollapses the trailing\rthatitemsleaves after splitting on\n, so CRLF input gives the same card as LF. The new test assertstoEqualbetween the two.
- The frontmatter regex accepts
- 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, andmainrequires 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 runontests/agent/agent-memory-source-clip.test.ts,tests/memory/flashback-card-unicode.test.tsandtests/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.
fedc4fd to
0287b83
Compare
Problem and what changes
Four readers in the memory path disagreed with the text they read, in ways a user can see:
---block shownEach 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
mainat2196b922; the branch is built from it. Node.js 26.5.0, pnpm 11.7.0. Part of #446, #447, #448 and #453.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).[]for the YAML page (fix(memory): read the YAML aliases a Wiki page writes #447); thelength-based clip ends inside a character (fix(memory): cut a flashback line between characters #448); the unit-basedclipSourceTextreports 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 typecheckand@linger/web typecheck: no errors. A previous revision of this branch passed[]asbuildWikiGraph's revision argument in the new test, which Vitest accepts buttscrejects; the test now passes"wiki-r1"like its neighbours, andtsc -p apps/node-services/tsconfig.json --noEmitis clean. Suites:tests/memory+ the clip case — 477 passed, 36 skipped;wiki-daily-card.test.ts— 4 passed.Boundary
apps/node-services/src/memory/README.mdis unchanged because the behaviour the operators read is the same: the same files are read, now correctly.Not verified here
Author readiness
contracts/openapi/changed (not applicable)Human owner: @XiaoBinGan