Skip to content

fix(memory): extend the frontmatter read to the closing fence when the byte cap cuts it off - #3617

Merged
qbc2016 merged 2 commits into
agentscope-ai:mainfrom
konlue:fix/memory-frontmatter-cap
Nov 3, 2026
Merged

qbc2016 merged 2 commits into
agentscope-ai:mainfrom
konlue:fix/memory-frontmatter-cap

Conversation

@konlue

@konlue konlue commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

AgentScope Version

2.0.12dev (main @ 4acacb9)

Description

The agentic-memory indexer reads only a byte-capped prefix of each memory file before parsing frontmatter (retrieval_max_tokens_per_frontmatter defaults to 256 tokens = 1024 bytes). The parsing regex requires a closing fence, so when the frontmatter block itself outgrows the cap the fence sits outside the prefix, the regex cannot match, and the file's description / type metadata are all silently dropped from the memory index (#3616 has the repro). #3345 already fixed the sibling case where only the trailing newline falls outside the prefix.

Changes — extract the prefix computation into _frontmatter_snippet and extend the read window to the closing fence when one exists beyond the cap:

closing = raw.find(b"\n---", 1)
if closing != -1:
    newline = raw.find(b"\n", closing + 1)
    fence_end = len(raw) if newline == -1 else newline + 1
    snippet_end = max(snippet_end, min(len(raw), fence_end))

The fence sits near the top of the file by definition, so the extension is bounded; raw is already fully in memory (read_file), so nothing extra is read. Behavior matrix:

file before after
frontmatter longer than the cap, fence beyond it all metadata lost fully recovered
closing fence at end of file, no trailing newline kept (#3345) kept
no closing fence (malformed) capped prefix, {} unchanged
frontmatter inside the cap unchanged unchanged

How to test

Three regression cases added to the existing FrontmatterParseTest in tests/middleware_filesystem_memory_test.py: the over-cap file now yields description + type through the extended window; the unclosed and within-cap shapes are pinned to their current behavior.

python -m pytest tests/middleware_filesystem_memory_test.py
# 13 passed.

Fixes #3616

Checklist

  • An issue has been created for this PR
  • I have read the CONTRIBUTING.md
  • Docstrings are in Google style
  • Related documentation has been updated (e.g. links, examples, etc.) in documentation repository
  • Code is ready for review

@github-actions github-actions Bot added the pr/awaiting-review Waiting for maintainers to review this PR label Oct 31, 2026

@qbc2016 qbc2016 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@github-actions github-actions Bot removed the pr/awaiting-review Waiting for maintainers to review this PR label Nov 3, 2026

@qbc2016 qbc2016 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@qbc2016
qbc2016 merged commit be0c7e8 into agentscope-ai:main Nov 3, 2026
10 checks passed
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.

[Bug]: agentic memory drops all frontmatter metadata when the frontmatter exceeds the byte-capped read

2 participants