fix(mcp): validate anchor in MemoryHistory - #241
Merged
Conversation
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: radimsem <me@radimsemerak.cz>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: radimsem <me@radimsemerak.cz>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: radimsem <me@radimsemerak.cz>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #201.
Summary
HandleHistorywas the only anchor-taking tool inpkg/mcp/tools/without input validation: a mistyped ID fell through toGetDiffsForNodeand rendered as"no history for <anchor>"— a confidently-wrong answer indistinguishable from a real-but-empty trail.This adds the empty-anchor guard + node-existence check that sibling handlers (
forget,pin) use, adapted to the tool's nature:anchor is required(matchesrelated.go's field-named message).GetDiffsForNoderuns first;GetNodeis probed only when the trail is empty, to distinguish a live-but-unchanged node (no history for X) from an unknown ID (node_id not found: X).Why trail-first (the important bit)
A naive copy of the sibling preflight —
GetNodebefore everything — is a functional regression.qSelectDiffsForNodereadsFROM diffs WHERE node_id = ?, keyed independently of thenodestable, so a forgotten node (removed fromnodes,rem/prior diffs retained) still has a trail that history must surface — that's how you roll back a delete by re-writing theoldpayload. An unconditional preflight would returnnode_id not foundfor exactly those nodes. Caught in review (Codex), fixed, and locked down with a regression test.anchor is requirednodes)node_id not found: X— the bug, fixedno history for X— legit empty preservednodes)Tests
TestHandleHistory— happy path now seeds a real node.TestHandleHistory_EmptyAnchor,TestHandleHistory_MissingNode— the two new error paths.TestHandleHistory_DeletedNodeKeepsTrail— seeds aremdiff for a node absent fromnodes, asserts the trail still renders.Verification
make build·gofmt -lclean ·make test(full suite, 212 MCP tests) ·make lint0 issues. Read-tool discipline preserved (noOpMu, no snapshot, no boost change). No public-skill update required (tool name/input/return shape unchanged).🤖 Generated with Claude Code