pi-memory icon indicating copy to clipboard operation
pi-memory copied to clipboard

MAX_CONTEXT_CHARS is hardcoded and truncation is silent: user lessons are cut first

Open VladimirMonin opened this issue 2 months ago • 0 comments

Environment

  • @samfp/pi-memory 1.5.0, Windows 11, Node 25.8.1
  • Global store shared across projects

Summary

The injected block is capped by a hardcoded constant and truncated silently:

  • var MAX_CONTEXT_CHARS = 8e3; — dist/index.js:414
  • truncation: text.slice(0, MAX_CONTEXT_CHARS - 20) + "\n... (truncated)\n</memory>" — dist/index.js:533-534 and 612-613
  • there is no configuration key for it: the accepted pi-memory settings keys are localPath, lessonInjection, consolidationModel, perTurnInjection — dist/index.js:105

Section order is facts → negative lessons → positives, so the first thing lost when the fact section is large is the user's own lessons.

Evidence

Six sessions measured with the real buildContextBlock() (same store, same day):

every case: chars 8006 | truncated: true

Cases included a plain home-directory cwd with no project scope and a small selection (15 lessons / 4 886 chars), i.e. this is not only a "huge store" problem — the always-present material plus a handful of matched facts already reaches the cap:

cwd C:/Users/<user> (slug "user")   -> chars 8006 | truncated: true
cwd C:\Users\<user>                 -> chars 8006 | truncated: true
cwd …/worktrees/media-vision-settings -> chars 8006 | truncated: true

For a project-scoped cwd the selection alone can exceed the cap several times over (41 lessons / 13 578 chars for one worktree in this store, see also the projectSlug separator issue), so most of that material never reaches the model.

Note also the interaction with prefix co-expansion: siblings are pulled only for keys with 3+ segments (dist/index.js:425-427), up to 20 per hit, which means one matched fact can consume a quarter of the budget on its own.

Suggested fix (any one would help; 1 and 2 are independent)

  1. Make the cap configurable — e.g. "memory": { "maxContextChars": 12000 } — so a user can deliberately trade tokens for recall instead of losing memory silently.
  2. Stop hiding the truncation — emit it in the envelope/log (or as a visible marker the agent can report), so "memory looks incomplete" becomes diagnosable instead of guessed.
  3. Reorder for graceful degradation — always-injected, human-authored lessons before machine-generated facts, so the tail that gets cut is the most replaceable part.

Documentation nit (same area)

The co-expansion rule is not documented anywhere I could find: it pulls siblings only for keys with 3+ segments, and a 2-segment key such as pref.editor is leaf — it never drags anything. I misread this initially (and built an audit on the wrong assumption, concluding a 2-segment family was expensive), which is a good sign that a README sentence would save someone else the same trip. A note on MAX_CONTEXT_CHARS and the selection predicates (project = <slug> OR project IS NULL, newest 50) would help too.

What I did not verify

I did not measure token cost or provider-side effects of a larger cap — only that the current cap is exceeded in practice on this store, on every probed session.

VladimirMonin avatar Sep 26 '26 06:09 VladimirMonin