Skip to content

fix: move assignMessageRefs after prune to prevent orphan alias leak#565

Open
zhexulong wants to merge 2 commits into
Opencode-DCP:devfrom
zhexulong:fix/message-id-alias-leak
Open

fix: move assignMessageRefs after prune to prevent orphan alias leak#565
zhexulong wants to merge 2 commits into
Opencode-DCP:devfrom
zhexulong:fix/message-id-alias-leak

Conversation

@zhexulong
Copy link
Copy Markdown

This patch fixes the "Message ID alias capacity exceeded" crash (closes #549) by reordering the createChatMessageTransformHandler pipeline.

Root Cause

In lib/hooks.ts, assignMessageRefs ran before prune. Every message in the conversation received an mXXXX alias via assignMessageRefs, but then prune (via filterCompressedRanges) removed some messages from the array — those now-orm messages never got their aliases cleaned up from state.messageIds. Over a long session, these orphan aliases accumulated until the 9999-ref limit was hit, crashing the session.

Fix

lib/hooks.ts: Moved assignMessageRefs(state, output.messages) from before prune() to after it. Now only survivors of the prune pass get aliases. No alias leak, no capacity exhaustion.

- assignMessageRefs(state, output.messages)
  syncCompressionBlocks(state, logger, output.messages)
  syncToolCache(state, config, logger, output.messages)
  buildToolIdList(state, output.messages)
  prune(state, logger, config, output.messages)
+ assignMessageRefs(state, output.messages)

Verification

  • tests/message-ids.test.ts: Added a RED→GREEN integration test that goes through createChatMessageTransformHandler with real prune state. On the old pipeline order the test FAILs (proving the leak); on the new order it PASSes.
  • npm test: 87/87 pass
  • npm run typecheck: clean

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.

2 participants