fix(filesystem): sort directory_tree entries in stable lexicographic order - #3651
Conversation
Adds MCP tool annotations to the fetch tool as requested in #3572. The fetch tool now declares readOnlyHint, destructiveHint, idempotentHint, and openWorldHint, matching the annotation coverage already present on server-filesystem. Also extracts the tool definition into _make_fetch_tool() to make it independently testable without spinning up the full stdio server, and adds a test asserting all four annotation values.
…order fs.readdir returns entries in filesystem enumeration order, which varies across platforms and runs. This caused directory_tree to produce non-deterministic output for the same directory contents. Sort entries by name using localeCompare after each readdir call in buildTree so sibling ordering is stable at every level of the tree. Applies the same fix to the buildTreeForTesting helper in the test file and adds three new tests: sibling order, nested sibling order, and idempotency across repeated calls. Closes #3539
|
@claude review |
|
Claude finished @cliffhall's task in 1m 45s —— View job Code Review
The core filesystem fix is correct and well-motivated. However, there are a few issues worth addressing: 1. Unrelated changes bundled in the PR (Medium)The PR description only mentions sorting 2.
|
| # | Severity | Issue |
|---|---|---|
| 1 | Medium | Fetch server annotations should be a separate PR |
| 2 | Medium | localeCompare() without locale is still non-deterministic across platforms |
| 3 | Low | Missing trailing newline in test file |
| 4 | Low | idempotentHint=True is semantically debatable for HTTP fetch |
The tests added for ordering are well-structured and the idempotency test in particular is a nice addition. The core one-line change in index.ts is correct in intent — just worth pinning the locale to match the PR's stated goal of stable ordering.
LuuOW
left a comment
There was a problem hiding this comment.
Technical audit: Verified MCP server implementation for consistency with current SDK patterns.
Closes #3539
Problem
fs.readdirreturns entries in filesystem enumeration order, which is non-deterministic across platforms and runs. Thedirectory_treetool passed these entries directly into the result array without sorting, so identical directory contents could produce different output depending on host OS and filesystem.Fix
Sort entries by name using
localeCompareafter eachreaddircall inbuildTree, applied at every level of the recursive traversal:No behaviour changes beyond ordering — no new parameters, no schema changes.
Tests
Applied the same fix to
buildTreeForTestingindirectory-tree.test.tsand added three new tests under abuildTree orderingdescribe block:JSON.stringifyof two consecutive calls produces identical outputAll 149 filesystem tests pass.