Skip to content

feat(hostname): add hostname - #277

Open
ferdingler wants to merge 3 commits into
strands-agents:mainfrom
ferdingler:feat/hostname
Open

ferdingler wants to merge 3 commits into
strands-agents:mainfrom
ferdingler:feat/hostname

Conversation

@ferdingler

Copy link
Copy Markdown
Collaborator

Description

There's no hostname, so a script that prints or logs the machine name gets command not found.

This adds hostname. It prints the shell's own host name: $HOSTNAME if the embedder or script sets it, otherwise localhost. It never looks at the real host's name. It only prints; it doesn't set the name and doesn't take the formatting flags, so any argument is an error. I also added it to the WASM command list and bumped the command count in the README.

Related Issues

Part of #207

Documentation PR

Type of Change

New feature

Testing

I checked the output and the help flag against the hostname tool on Linux. The new tests cover the default name, a name set in the shell or by the embedder, an empty variable, help, and rejecting an attempt to set the name. The full Rust suite passes and clippy has the same warnings as main. I didn't run the Python or Node suites because the bindings are unchanged. This is one of four small PRs adding whoami, hostname, uname, and id; each stands on its own, but they touch the same command lists, so whichever lands later will need a trivial rebase. uname -n in its own PR uses the same rule for the name.

  • 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 requested a review from a team as a code owner November 13, 2026 05:14
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@ferdingler ferdingler mentioned this pull request Nov 13, 2026
10 tasks done

@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. I found no defects. hostname matches the other commands, the localhost default is what users actually get, and the tests check real behaviour.

🟡 Watch the merge order: #273 and #286 make the same 33 → 34 edit at README.md:118 and README.md:252. Identical edits merge without a conflict. I tried it: merging this PR and then #273 conflicts only in src/vfs.rs, and the README stays at 34 while mod.rs has 35 commands. Whichever of these PRs lands second needs to bump the count by hand.

@ferdingler, two non-blocking questions:

  • hostname -s and hostname -f show up a lot in scripts, and right now they fail with invalid option. In a sandbox that has no domain, both could just print the name. Is rejecting them a deliberate choice?
  • The help text doesn't mention $HOSTNAME or the localhost fallback, so an agent reading --help can't tell how the name is set. Could you add a line?
What I verified (head 5cd7643)
  • cargo test --test shell_integration hostname: 7/7 pass. cargo fmt --check is clean, and cargo clippy --all-targets has no warnings in the changed lines.
  • Built CLI: hostname → localhost. HOSTNAME=box hostname → box. hostname newname → hostname: unexpected argument "newname" with exit 1. hostname -s and -f → invalid option, exit 1. -h prints the usage. /bin/hostname exists.
  • VfsKernel::new_process (src/vfs_kernel.rs:286-291) sets only HOME, PWD, PATH and USER, and nothing sets HOSTNAME. So the default is reachable, and the code never looks at the real host name.
  • hostname.rs follows pwd.rs line for line: same lexopt parsing, -h/--help, and arg.unexpected().
  • Native /bin links and the command lists come from the registry. Only the WASM list in src/vfs.rs is maintained by hand, and the PR updates it.
  • #286 uname -n uses the same $HOSTNAME-or-localhost rule. It copies DEFAULT_HOSTNAME rather than sharing it, so whichever PR lands second could import the other's constant to keep them from drifting.
  • #285 (only exported variables reach commands) keeps builder env and VAR=x cmd prefixes exported, so the tests still hold after it merges.

Passes run: correctness/tests (fresh reviewer). I skipped the API bar-raiser and adversarial passes: there's no library API change and the parsing is trivial.

@ferdingler ferdingler mentioned this pull request Nov 13, 2026
10 tasks done
@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks. Pushed 0ca9f3a: --help now says the name comes from $HOSTNAME and falls back to localhost, and the error tests check the actual messages.

-s and -f are left out on purpose. The scope for this one was "just print it", and I would rather add flags in a separate small change than guess at what -f should mean in a sandbox with no domain. It is an easy follow-up if we want it.

On merge order: agreed. Whichever of #273, #277, #286, #288 lands later will get rebased, with the README count bumped to match and the WASM list conflict in src/vfs.rs resolved.

# Conflicts:
#	README.md
#	src/commands/mod.rs
#	src/vfs.rs
#	tests/shell_integration.rs
@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 at d2324a8, with one 🟡 that the merge with main brought in.

Points from my last review:

  • README count: fixed. The README says 38 and src/commands/mod.rs has 38 commands.
  • Help text: fixed in 0ca9f3a.
  • -s / -f: left out on purpose, which is fine as a follow-up.
  • Duplicated DEFAULT_HOSTNAME: replaced by the inline finding below.

🟡 src/commands/hostname.rs:22: since #331, uname -n falls back to the kernel's node name, but hostname still falls back to the constant localhost. Under a kernel that overrides Kernel::uname, uname -n; hostname prints studio then localhost. The standalone shell is unaffected. The fix is two lines and is inline; neither command is released yet, so it's cheap to line them up now.

CI on this head was still running when I posted (several Rust and Node jobs queued).

What I verified (head d2324a8, base c28e1fb)

Linux aarch64, cargo 1.100.0.

  • cargo test --workspace --all-targets: 2697 passed, 0 failed, 0 ignored across 20 test binaries. cargo fmt --check is clean. cargo clippy --workspace --all-targets exits 0 with no warnings in hostname.rs.
  • Merge resolution: README.md:118 and :252 say 38, matching the 38 arms in lookup. The WASM list in src/vfs.rs has hostname next to id, uname and whoami; it lacks only curl and sleep, as on main.
  • Built CLI:
    • hostname → localhost; HOSTNAME=box hostname → box; HOSTNAME= hostname → localhost.
    • export HOSTNAME=web1; hostname; sh -c hostname; uname -n → web1 three times.
    • hostname newname, hostname -- newname, hostname '' and hostname -s each exit 1 with the message the tests now check.
    • --help and -h print the new $HOSTNAME line.
    • /bin/hostname links to lash, and x=$(hostname) and hostname > file both capture the name.
  • The inline finding: reproduced with the tests/custom_kernel.rs kernel, then re-run with the suggested fix applied (studio from both commands; the 9 hostname-named tests in shell_integration still pass).

Not run: pytest tests/python, npm test, a WASM build, anything on macOS or Windows.

Appendix (1)

  • hostname "" prints the name and exits 0 because the shell drops a literal "" word before the command sees it. That is #299, not this PR.

Comment thread src/commands/hostname.rs
Comment on lines +22 to +25
let host = io::with_process(|p| match p.get_env("HOSTNAME") {
Some(host) if !host.is_empty() => host.to_string(),
_ => DEFAULT_HOSTNAME.to_string(),
});

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.

hostname and uname -n print different names when the kernel reports its own node name

Since #331 (merged into this branch), uname -n falls back to Kernel::uname().node_name. hostname still falls back to the constant localhost. An embedder that overrides Kernel::uname and leaves $HOSTNAME unset (Box plans to, per #331) gets studio from uname -n and localhost from hostname. The standalone shell is unaffected, because its kernel's node name is localhost.

Suggestion: use the same fallback as uname. This also needs _os renamed to os on line 10, DEFAULT_HOSTNAME removed, and the help line reworded, since it names localhost.

Suggested change
let host = io::with_process(|p| match p.get_env("HOSTNAME") {
Some(host) if !host.is_empty() => host.to_string(),
_ => DEFAULT_HOSTNAME.to_string(),
});
let host = io::with_process(|p| match p.get_env("HOSTNAME") {
Some(host) if !host.is_empty() => host.to_string(),
_ => os.uname().node_name,
});
Reproduction details

Reviewed and tested at d2324a8 (base c28e1fb), Linux aarch64, cargo 1.100.0.

Setup: a copy of tests/custom_kernel.rs saved as a scratch test file, with this test appended. Its MemKernel already overrides uname() with node_name: "studio" and does not set HOSTNAME.

#[test]
fn rev277_hostname_vs_uname_n_under_custom_kernel() {
    let kernel = MemKernel::new();
    let mut shell = shell_over(&kernel);
    block_on(async {
        let out = shell.run("echo \"[$HOSTNAME]\"; uname -n; hostname").await;
        eprintln!("REV277 unset: {:?} status={}", out.stdout, out.status);
        let out = shell.run("HOSTNAME=box uname -n; HOSTNAME=box hostname").await;
        eprintln!("REV277 set:   {:?} status={}", out.stdout, out.status);
    });
}

Command: cargo test --test zz_rev277_hostname rev277 -- --nocapture

$HOSTNAME unset HOSTNAME=box
Expected [], studio, studio box, box
Observed at d2324a8 [], studio, localhost box, box
Observed with the suggestion applied [], studio, studio box, box

Exit status was 0 in every run. With the suggestion applied, cargo test --test shell_integration hostname passed 9 of 9. I removed the scratch file and reverted the edit afterwards.

A regression test could add hostname to uname_describes_the_kernel_system in tests/custom_kernel.rs and assert studio. I did not run it in that form.

This branch has not been deployed

No deployments
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