Skip to content

fix(tools): align claude-memory insert with memory_20250818 line semantics#1309

Open
rohitsux wants to merge 1 commit into
supermemoryai:mainfrom
rohitsux:fix/claude-memory-insert-spec
Open

fix(tools): align claude-memory insert with memory_20250818 line semantics#1309
rohitsux wants to merge 1 commit into
supermemoryai:mainfrom
rohitsux:fix/claude-memory-insert-spec

Conversation

@rohitsux

Copy link
Copy Markdown

Problem

Anthropic's memory_20250818 tool spec defines the insert command as: insert_text is inserted after line insert_line, 0 inserts at the beginning of the file, and the valid range is [0, n_lines].

ClaudeMemoryTool.insert treated insert_line as a 1-based insert-before index with range [1, n_lines + 1]:

if (insertLine < 1 || insertLine > lines.length + 1) { ... }
lines.splice(insertLine - 1, 0, insertText)

The caller of this tool is Claude itself, which is trained on the spec semantics, so in practice:

  • every model-driven insert landed one line earlier than intended,
  • insert_line: 0 (insert at top of file) was rejected as invalid,
  • insert_line: n_lines (append) inserted before the last line instead of after it.

Same class of issue as #1285 (str_replace spec literalness), one command over.

Fix

Validate against [0, lines.length], splice at insert_line directly (0-based insert-after), and update the error and success messages to the spec's wording. One existing tool-operations test encoded the old insert-before behavior; its insert_line is adjusted from 2 to 1 so its expected output stays byte-identical under spec semantics.

Tests

New insert line semantics describe block in claude-memory.test.ts using the existing mock harness: top-of-file (insert_line: 0), middle insert-after, append at n_lines, and both out-of-range directions (negative and n_lines + 1, asserting no write occurs). All four fail on main and pass with the fix.

Verification

  • bun run test claude-memory: 14 passed (10 existing + 4 new).
  • Full packages/tools suite: 87 passed, zero new failures (the two pre-existing file-level failures reproduce identically on unmodified main).
  • bun run build clean.

Scope

Only the insert command's line-index semantics change (validation range, splice index, and its two messages). str_replace, view, create, delete, and rename are untouched.

…ntics

Anthropic's memory_20250818 spec defines insert as: insert_text is inserted
AFTER line insert_line, 0 inserts at the beginning of the file, and the valid
range is [0, n_lines]. The implementation treated insert_line as a 1-based
insert-BEFORE index with range [1, n_lines + 1].

Since the caller of this tool is Claude itself, which is trained on the spec
semantics, every model-driven insert landed one line earlier than intended,
insert_line: 0 (insert at top of file) was rejected as invalid, and
insert_line: n_lines (append) inserted before the last line instead of after
it.

Fix the validation range to [0, n_lines], splice at insert_line directly
(0-based insert-after), and update the error and success messages to match.
One existing tool-operations test encoded the old insert-before behavior; its
insert_line is adjusted so its expected output is unchanged under spec
semantics. Adds four regression tests covering top-of-file, middle,
append, and both out-of-range directions.
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.

1 participant