Skip to content

feat(ln): add -f/--force and link into an existing directory - #234

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:feat/ln-force
Nov 13, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:feat/ln-force

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

The shell rejected ln -sf target link, so scripts could not replace an existing symbolic link. Retrying link creation also left unused inodes behind when the destination already existed, eventually exhausting long-lived sessions.

This adds force replacement and creates the link inside an existing directory, following GNU behavior while refusing same-file replacement and directory overwrites. It keeps direct-bind files intact when link creation is unsupported, and failed VFS link creation no longer consumes an inode. Hard links, multiple source operands, and -n/-T remain outside this change.

I compared 12 cases with GNU and ran 10,000 successful replacements followed by creating another file. The inode regressions failed before the fix, and the direct-bind test now checks that force was accepted. The full Rust suite, formatting, lint and documentation checks pass locally.

Related Issues

Part of #207

Documentation PR

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 23:20
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 23:20
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: comment. Close to mergeable; there's one 🟡 worth fixing first. The ln behaviour matches GNU coreutils 9.1 in every case we tried. The problem is underneath it: each successful ln -sf over an existing file leaks one VFS inode. After about 9,950 replacements in a session, the VFS hits its inode limit and every command that creates a file fails.

  • 🟡 ln.rs:64: the create-then-remove order relies on a failed Vfs::symlink, and that call leaks an inode (src/vfs.rs:371-372). The fix is a two-line swap in vfs.rs (inline).
  • 🟡 tests/shell_integration.rs:7330: the bind_direct test also passes on main, because -f is rejected there as an invalid option (inline).

The adversarial pass found no data loss and no case where this succeeds when GNU fails.

✅ What I verified, plus the appendix

Verified at 552e7a2

  • cargo test --test shell_integration ln_: 15/15 pass. Full cargo test --test shell_integration: 1789/1789 pass.
  • About 30 reviewer cases and 17 adversarial cases compared side by side with GNU ln 9.1. Covered: replacing a file, replacing a symlink (not followed), a symlink to a directory, a real directory, linking into a directory, . and .., /, a dangling destination, trailing slashes, the same-file check, and -f without -s (returns before anything is removed). In every case the files end up the same as with GNU and the command succeeds or fails the same way.
  • Self-loop parity: ln -sf l l and ln -sf a d/a (with d/a a file) create self-loop symlinks, and ln -sf a a is refused as the same file. GNU does exactly the same, so these aren't findings.
  • Direct bind: on a mode="direct" mount, ln -sf leaves the host files untouched, read-only or writable, so the claim in the PR description holds.
  • Leak repro (fresh VFS for each -c):
    $ strands-shell -c 'cd /tmp; ln -sf a l; i=0; while [ $i -lt 10000 ]; do ln -sf a l || { echo "ln failed at $i"; break; }; i=$((i+1)); done; touch newfile; echo touch=$?'
    strands-shell: ln: filesystem inode limit exceeded
    ln failed at 9947
    strands-shell: touch: filesystem inode limit exceeded
    touch=1
    
    The control loop rm -f l; ln -s a l runs 10,000 iterations and then prints touch=0.

Reading order: read ln.rs:35-58 (directory handling and the same-file check), then ln.rs:62-73 (create, then remove on AlreadyExists), then vfs.rs:361-374.

Appendix: non-blocking (3)

  • ⚪ Pre-existing: ln.rs:35-36 (let target = &paths[0]; let mut link = paths[1].clone();) silently drops extra operands. ln -sf a b extra returns 0 and creates b -> a, while GNU says ln: target 'extra': No such file or directory and returns 1. Now that a directory can be the link name, ln -s a b DIR is an easy thing to type. A one-line guard would fix it: if paths.len() > 2 { return Err("ln: extra operand".into()); }. Fine as a follow-up under #207.
  • ⚪ Some error messages differ from GNU's wording. Both tools fail and nothing changes in each case. mkdir d; ln -sf / d prints d//: cannot overwrite directory because file_name("/") falls back to "/" at ln.rs:47-50. ln -sf .. . reports "same file" where GNU reports "cannot overwrite directory", because the check at :55 runs before the one at :66.
  • ⚪ vfs.rs create_file (around :301-302) uses the same insert-before-dir_insert order as symlink. It's pre-existing and not on this PR's path. It would be worth fixing in the same pass if you touch symlink.
  • Question (non-blocking): agents often use ln -sfn new cur to repoint a symlink at a directory. Without -n, ln -sf new cur now creates cur/new inside the old target, which matches GNU but may surprise. Is -n worth tracking under #207?

Repro script and output: strands-agents/shell/pr/234/repro.sh and repro-output.txt in the artifacts bucket.

Comment thread src/commands/ln.rs Resolved
Comment thread tests/shell_integration.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

I kept the remaining appendix items separate from these fixes. Multiple-source operands and -n/-T remain follow-up work under #207; adding those forms would expand the command interface beyond this PR. I agree -n matters for repointing directory symlinks, and the description now makes that omission explicit. The root/parent-directory diagnostic differences still refuse the operation without modifying files, so I left their wording alone. I also left the pre-existing regular-file creation path unchanged: it needs its own failure-path coverage and is not used by the force-link retry.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve: 3776f7d fixes both findings and I found no new issues.

  • ✅ Inode leak (ln.rs:64 → vfs.rs:372-373): fixed. The 10,000-replacement CLI probe now ends with touch=0. It previously failed at iteration 9947.
  • ✅ The bind_direct test now fails if -f is rejected as an invalid option.

I'm fine leaving the appendix items (multiple sources, -n/-T, diagnostic wording, create_file) as follow-ups under #207.

What I verified
  • The fix itself: dir_insert only touches the parent directory and doesn't read the child inode, and nothing that can fail runs between it and inodes.insert, so a failed call can't leave a dangling directory entry. A plain ln -s x b over an existing b still errors with b: already exists, leaves b -> a, and later file creation works.
  • The new tests catch the bug: I reverted the two-line swap locally, and failed_symlinks_preserve_inode_budget and ln_force_replacements_preserve_inode_budget both fail. With the fix restored they pass.
  • Test runs at 3776f7d: cargo test --test shell_integration --test vfs_unit passes 1790 + 97, including 16 ln_ tests. I couldn't run fmt/clippy locally because those components aren't installed. CI's Rust jobs on ubuntu, macOS and Windows are green. The red rollup is a cancelled duplicate validate-pr-title run; its rerun passed.

@ferdingler
ferdingler merged commit c769595 into strands-agents:main Nov 13, 2026
31 of 32 checks passed
@ferdingler
ferdingler deleted the feat/ln-force branch November 13, 2026 00:49
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