# Migrate From atty to std::io::IsTerminal in Rust

Yes. An agent replaces atty with the standard library's IsTerminal, checks whether another crate still pulls atty in, runs the tests and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/migrate-atty-to-is-terminal/atty/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent begins with the dependency graph, because the crate named in an advisory is often present twice. In a command-line tool like Miserlou/Loop, `Cargo.toml` lists `atty = "0.2"` directly, and `src/main.rs` uses it once: `if opt.stdin || atty::isnt(atty::Stream::Stdin)`, which decides whether to read items from piped input. Running `cargo tree -i atty` shows a second path. `structopt` 0.2.18 depends on `clap` 2.33, and that `clap` depends on `atty` too. The agent reports both paths up front, since removing the direct dependency alone will not take the crate out of `Cargo.lock`.

It records a baseline before changing anything. `tests/test.rs` holds 11 tests that build the `loop` binary and run it through `assert_cmd`, comparing stdout. The agent runs `cargo test` on the untouched checkout and saves the result per test. It also notes two things that affect how the build is run. The manifest has no `edition` key, so Cargo warns that it defaults to 2015, and `cargo test --locked` refuses to start because the committed `Cargo.lock` no longer matches `Cargo.toml`.

The code change is three edits. The `extern crate atty;` line and the `Cargo.toml` entry go. `IsTerminal` joins the existing `use std::io::{...}` list, and the condition becomes `opt.stdin || !io::stdin().is_terminal()`. The trait has been in the standard library since Rust 1.70, so the agent checks for a declared minimum Rust version in `Cargo.toml`, the CI file and the README. This project declares none and its `.travis.yml` builds stable, beta and nightly. Where a project must support an older compiler, the `is-terminal` crate offers the same method and the agent uses that instead.

Then it verifies behaviour, not only compilation. The tests spawn the binary with stdin that is not a terminal, so every one of them exercises the changed branch. The agent reruns the suite and compares against the baseline test by test: a test that failed before and fails the same way after is not a regression, and it says so explicitly. It also pipes a line into the binary by hand and confirms the piped item is still appended after the `--for` items, and that the same command with stdin from `/dev/null` adds nothing.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "cargo audit reports the atty crate as unsound (RUSTSEC-2021-0145) and unmaintained (RUSTSEC-2024-0375). Replace it with std::io::IsTerminal. First run cargo tree -i atty and report every path by which atty enters the build, direct and transitive. Find every use of atty in the source. Check whether the project declares a minimum supported Rust version in Cargo.toml (rust-version), CI configuration, a rust-toolchain file or the README: IsTerminal needs Rust 1.70; if the project must build on an older compiler, use the is-terminal crate instead and say why. Before editing, run cargo build and cargo test and save the result of each test as a baseline; if --locked fails because Cargo.lock is out of date, say so and continue without it. Replace each atty::is(Stream::X) with the matching std handle's is_terminal() and each atty::isnt with its negation, remove the extern crate line and the Cargo.toml entry, and update Cargo.lock. Run the tests again and compare with the baseline test by test; report any test whose result changed and do not describe pre-existing failures as caused by this change. Exercise the changed branch by hand with piped stdin and with stdin from /dev/null and quote the output. Run cargo tree -i atty again. If atty is still present through another crate, name that crate and the upgrade that would remove it, and leave that upgrade out of this pull request. Do not change the edition or other dependencies. Open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that changes `Cargo.toml`, `Cargo.lock` and one file of source. The description shows `cargo tree -i atty` before and after. In Loop's case the after output still lists the crate, now only under `clap` and `structopt`, and the description says in its first paragraph that `cargo audit` will keep reporting both advisories until `structopt` is replaced. That is a larger migration with its own page of changes, and the agent names it as the follow-up instead of starting it.

The test section is a two-column table, baseline and after, one row per test. A reviewer looks for rows that differ. Identical columns, including identical failures, mean the change did what a like-for-like swap should. The manual checks follow: the exact commands with piped and empty stdin and what each printed.

The reviewer also checks the polarity of each replaced call, which is the one place a swap like this goes wrong. `atty::isnt` means not a terminal, so its replacement carries a `!`. And they check that the committed lock file was regenerated by Cargo and not edited by hand. Here the lock file was already stale, so its diff is larger than the change itself, and the description explains why.

## Run it on every Monday morning

Advisories for unmaintained crates do not arrive with a failing build, so a schedule is the practical way to act on them. An automation in the repository's Project with a `schedule` trigger, a weekly `cron` expression and your `timezone`, starts a run each week. Give it a prompt that runs `cargo audit` first, stops with a one-line note when there is nothing to report, and otherwise handles one advisory per run so each pull request stays reviewable.

For many crates at once, start the runs from your own server with an API key: one request per repository, each with a `request_id` that is a UUID v5 of the repository name and the advisory id, so a retried job returns the run it already started. Runs past the key's `max_concurrent` limit wait as `queued` and start as slots free. Each finished run is reported to the key's webhook, if you set one, with the session URL to read the result from.

## Why run it on Rush?

The swap is quick to write, and the value of the run is in the checks around it. It builds the dependency tree before and after, runs the suite twice and compares, and tries the changed branch by hand, each in its own sandbox with a toolchain installed for the job. The transcript keeps the output of every command. A pull request that says the advisory is only half resolved, and shows the `cargo tree` output that proves it, saves the reviewer from merging it and then wondering why the audit is still red.

It also keeps unrelated state out of the result. The baseline run on this fixture is not all green in every environment, and a person fixing an advisory on a Friday is tempted either to chase those failures or to ignore the suite. The run records them as pre-existing, changes nothing about them, and reports only what moved. With one run per repository, a team can clear the same advisory across all its crates in parallel and review the results in one sitting.

## Frequently asked questions

### What is wrong with atty?

Two advisories. RUSTSEC-2021-0145 reports a potentially unaligned read on Windows and lists no patched version. RUSTSEC-2024-0375 records that the maintainer declared the crate unmaintained and pointed users to the standard library's `IsTerminal` trait.

### Which Rust version do I need?

`std::io::IsTerminal` is stable since Rust 1.70. A project that has to compile on something older can use the `is-terminal` crate, which exposes the same `is_terminal()` method. The agent reads your declared minimum version before choosing between them.

### I removed atty from Cargo.toml and cargo audit still reports it. Why?

Another dependency brings it in. `cargo tree -i atty` prints which one. A common path is `clap` 2, used directly or through `structopt`. The advisory clears only when that dependency is upgraded to a release that no longer uses `atty`, which is a separate and larger change.

### Is the behaviour identical?

For the question both answer, yes: whether a standard stream is attached to a terminal. The call shape differs, a function taking a `Stream` value against a method on the handle, so the agent checks each call's polarity and runs the program with piped and with empty input to confirm the branch still goes the same way.

## More jobs like this: Upgrade dependencies weekly

- [Migrate From AWS SDK for Go v1 to v2](https://getrush.ai/run/migrate-aws-sdk-go-v1-to-v2/aws-sdk-go/)
- [Migrate AWS SDK for JavaScript v2 to v3 in an Express S3 App](https://getrush.ai/run/migrate-aws-sdk-v2-to-v3/aws-sdk/)
- [Replace Axios With Native fetch in a Node.js Library](https://getrush.ai/run/migrate-axios-to-fetch/axios/)
- [Migrate From babel-eslint to @babel/eslint-parser](https://getrush.ai/run/migrate-babel-eslint-to-babel-eslint-parser/babel-eslint/)
- [Migrate a Go Project From Blackfriday to goldmark](https://getrush.ai/run/migrate-blackfriday-to-goldmark/blackfriday/)
- [Migrate From Bluebird to Native Promises in a Node.js Library](https://getrush.ai/run/migrate-bluebird-to-native-promises/bluebird/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
