Skip to content

feat(cli): add noninteractive controls and interrupt-safe progress - #5

Merged
thebrandonlucas merged 1 commit into
masterfrom
feat/cli-interaction-progress
Oct 7, 2026
Merged

thebrandonlucas merged 1 commit into
masterfrom
feat/cli-interaction-progress

Conversation

@thebrandonlucas

@thebrandonlucas thebrandonlucas commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add --no-input and --quiet while preserving mandatory confirmations, errors, results and recovery IDs. --yes approves consequential actions but never supplies a secret.
  • Show cancellable status only on interactive stderr for API/auth/price requests, waits, streams and credential lock contention. Redirected stderr and quiet mode get no status lines or control codes.
  • Ctrl-C exits 130 and wins at the confirmation and hidden API-key prompts, whether it arrives as a signal or is typed at the hidden prompt: blocking terminal reads run on a detached thread, the interrupt handler is installed before the command runs, and a cancelled hidden prompt restores terminal echo.
  • Record every write immediately before it is sent. Ctrl-C before that reports that no resource change was submitted; after it, the outcome is reported as unknown with the original ID and reconciliation detail (payment-specific wording for payments). A submitted write is never retried or described as cancelled.
  • Record in-flight login changes too: an interrupted token refresh, device login, or logout revoke names what may have happened on the server and how to recover (voltage login again, or logout with --local).
  • Use a fresh saved login without the credential lock; the lock is taken only to refresh, so parallel commands no longer run one at a time. Logout keeps the lock across revoke so a concurrent refresh cannot leave the new session live.
  • Pass one Terminal built from the global flags instead of threading quiet/no_input booleans through auth, config and startup. Terminal::during ties a status line to the one future it describes.
  • Cover PTY vs redirected progress, no-input/quiet behavior, Ctrl-C (signalled and typed) at both prompts with echo restored, a fresh login under a held lock, and interruption before and after a payment or other write and during refresh, login, and logout.

Targets master directly (not stacked) and merges cleanly with it, including #9 and #10. Local docs/plans/ files are gitignored and intentionally not included.

Feature-by-feature, before and after

Real output from master and feat/cli-interaction-progress, both run against a slow local mock API. Long JSON is trimmed, and the temp directory is shown as <tmp>.

1. Progress status on a terminal (new)

voltage wallets list --org $ORG      # API takes ~2s

Before: the terminal sits silent for 2 seconds, then:

retrieved
ID  NAME
11111111-1111-1111-1111-111111111111  main

After: raw bytes written to stderr:

Waiting for API response...\r\x1b[2Kretrieved
ID  NAME
11111111-1111-1111-1111-111111111111  main

You see Waiting for API response... while the request is in flight. Then \r\x1b[2K (carriage return, then "erase line") removes it, so the final screen looks the same as before. Other status labels are "Submitting request...", "Waiting for auth service...", "Waiting for device approval...", "Waiting for credential lock...", "Waiting for price service..." and "Waiting for checkout event...".

When redirected (2>file or a pipe), both versions write 0 bytes to stderr. The status appears only when stderr is a real terminal, so log files never get control codes.

2. -q / --quiet (new)

voltage -q wallets list --org $ORG
retrieved
ID  NAME
11111111-1111-1111-1111-111111111111  main

No status line. With --quiet, the necessary output still appears:

echo "$BODY" | voltage -q --yes --no-input payments create --org $ORG --env $ENV --data - --json
{ "action": "payments create", ... "resource_id": "4444..." }                 ← confirmation summary (kept)
Network/provider fee limits exclude additional processing fees. ...           ← warning (kept)
Resource ID: 4444.... Recovery record: <tmp>/cfg/requests/4444....json       ← recovery ID (kept)
{"http_status":202,"data":null,"resource_id":"4444...","outcome":"accepted"}  ← result on stdout
exit=0

Hidden: status lines, "Invoice copied to the clipboard.", "Invoice is ready; continuing to poll...", and repeated "Payment X status: pending; waiting..." updates.

Before: error: unexpected argument '-q' found, exit 2.

3. --no-input (new)

On a terminal, without --yes:

voltage --no-input payments create --org $ORG --env $ENV --data @req.json
{ ...confirmation summary... }
error: This operation requires --yes when input is disabled or noninteractive
exit: 2

Without the flag, it would show Proceed? [y/N] and wait. With it, nothing is sent and no recovery record is written.

Secrets are never taken from --yes:

voltage --no-input --yes auth import-key --account other --org $ORG --env $ENV
{"error":{"detail":null,"exit_code":2,"message":"Use --stdin to import an API key when input is disabled or noninteractive (--yes does not supply a key)"}}
exit=2

The fix is ... auth import-key --stdin <<< "$KEY".

Before: error: unexpected argument '--no-input' found, exit 2.

Refusing earlier: when a confirmation can't be answered, the branch now refuses before resolving credentials or making a wallet preflight request, so nothing touches the network. In my run the timing was the same (0.01s) because an API key from the environment needs no refresh. With a login session it saves a token refresh.

4. Ctrl-C at the Proceed? [y/N] prompt (fix)

voltage payments create --org $ORG --env $ENV --data @req.json
# press Ctrl-C at the prompt

Before:

Proceed? [y/N]
exit: -2   (killed by SIGINT, no message)

The blocking read_line stalled the async task, so the CLI's own Ctrl-C handler never ran. The operating system killed the process outright: no error report, no --json error, and the cursor left on the prompt line.

After:

Proceed? [y/N] error: Interrupted before any resource change was submitted
exit: 130

The read runs on a detached thread (read_detached), so the handler runs. It reports an error that says what happened and exits with the standard code 130.

5. Ctrl-C messages that say what actually happened (fix)

During a read (wallets list):

stderr
Before error: Interrupted; any submitted payment continues independently. Use its original ID to check status.
After error: Interrupted before any resource change was submitted

The old message mentioned a payment during a read-only list, which was misleading.

While a payment POST is in flight (--yes payments create):

Before:

error: Interrupted; any submitted payment continues independently. Use its original ID to check status.

After:

error: Interrupted after payment 44444444-... may have been submitted; query its original ID before resubmitting. The payment was not cancelled.
details: {"action":"Query the original resource ID before deciding whether to resubmit","environment_ids":["3333..."],"organization_id":"2222...","outcome":"unknown","resource_id":"44444444-..."}
exit: 130

The new message names the ID and machine-readable details say "outcome":"unknown", so a script can reconcile. A wallets delete interrupted the same way gets a generic "request may have been submitted" message that doesn't mention a payment.

SubmissionState in src/api.rs makes this work. It records the write just before the request leaves the process, and the Ctrl-C handler checks it to pick the right message. A second Ctrl-C now calls exit(130) immediately, even if cleanup is still running.

6. Unchanged except for wording

Piped stdin without --yes behaves the same on both versions (exit 2, summary printed first). Only the message changed, from "requires --yes in noninteractive use" to "requires --yes when input is disabled or noninteractive".

Review exceptions

  • Detached std::thread for terminal reads: a blocked read cannot be cancelled, and the runtime waits for spawn_blocking work at shutdown. A oneshot channel reports completion, and process exit ends an unfinished read.
  • tokio sync feature: provides oneshot for those reads and Notify in tests; no new crate.
  • unsafe libc::tcgetattr/tcsetattr in TerminalModes (production): std cannot save or restore terminal attributes, and rpassword restores echo only when its read returns, which Ctrl-C no longer waits for. One call per block with a SAFETY: comment; recorded in the style guide's approved exceptions.
  • Test-only unsafe libc::openpty, setsid/ioctl(TIOCSCTTY) in pre_exec, and tcgetattr: std cannot open a PTY, make it a child's controlling terminal, or read its modes, and terminal-only progress and echo restoration need a real terminal to test. Each block has a SAFETY: comment.
  • pub(crate) LOCK_TIMEOUT: read by a const assertion in auth that keeps it at least twice AUTH_TIMEOUT.

Verification

  • cargo fmt --check
  • cargo clippy --locked --all-targets -- -D warnings, plus the test file linted with its cfg(unix) items disabled to simulate the Windows job
  • cargo test --locked
  • python3 scripts/check-coverage.py
  • cargo machete --with-metadata

Base automatically changed from feat/cli-help-errors to master September 24, 2026 21:08
@thebrandonlucas
thebrandonlucas force-pushed the feat/cli-interaction-progress branch 3 times, most recently from 51e2179 to 0e6078b Compare September 25, 2026 19:08
@thebrandonlucas
thebrandonlucas marked this pull request as ready for review September 29, 2026 15:11

@tee8z tee8z left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, just some small stuff around locking/remove the need for "drop" calls. Overall looks good.

Comment thread src/startup.rs Resolved
Comment thread src/auth.rs Resolved
Comment thread src/auth.rs Resolved

@tee8z tee8z left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Follow-up from a broader pass over the PR: two issues with the new Ctrl-C handling, each with a suggested fix. The first one can leave the user's terminal in a bad state, so I think it should be fixed before merge.

Comment thread src/terminal.rs Resolved
Comment thread src/api.rs Resolved
@thebrandonlucas
thebrandonlucas force-pushed the feat/cli-interaction-progress branch 2 times, most recently from ff3e105 to 1d73676 Compare October 7, 2026 12:53
@thebrandonlucas

Copy link
Copy Markdown
Contributor Author

Follow-up to the review: beyond the interrupt-message fix @tee8z suggested for token refresh, 1d73676 extends the same tracking to the other auth requests that can change server state.

SubmissionState now records one in-flight SessionChange, held until its local side is saved:

  • Refresh: "Interrupted while refreshing the saved login for {account}", with a hint to run voltage login if the next command reports an expired session.
  • Login: marked only while a device-token poll is in flight and from token receipt until the login is saved, so interrupting during the approval wait still reports no change. Message: "Interrupted after sign-in may have created a session that was not saved", with a hint to run voltage login again or end the unsaved session with account global signout.
  • Logout: marked from just before the revoke until the saved login is removed. Message: "Interrupted while logging out {account}; its session may already be revoked", with a hint to rerun the logout with --local.

A sent write still outranks a session change. Unit tests cover each message, and integration tests interrupt a slow refresh, a login before it is saved, and a logout revoke.

The same push also makes Ctrl-C exit 130 reliably: it installs the interrupt handler before the command runs (biased select), and treats a Ctrl-C typed at the hidden API-key prompt (which rpassword raises as SIGINT itself) as an interruption instead of an I/O error.

Comment thread src/auth.rs Resolved
@thebrandonlucas
thebrandonlucas requested a review from tee8z October 7, 2026 17:42

@LightningK0ala LightningK0ala left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The SIGINT payment recovery behavior looks correct against local mocks. Before merging, please address:

  • src/startup.rs:32: synchronous explicit stdin reads prevent both first and second Ctrl-C from being handled; move them behind cancellable orchestration.
  • tests/cli_integration.rs:1330: make the PTY descriptors close-on-exec so concurrent children cannot keep the drain open.

tee8z
tee8z previously approved these changes Oct 7, 2026

@tee8z tee8z left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm good once @LightningK0ala comments are addressed

Add global --no-input and -q/--quiet, route stderr diagnostics through a
Terminal helper that separates optional progress and notices from
confirmation and recovery information, and show cancellable status on
interactive stderr only. Terminal::during ties a status line to the one
future it describes, so it clears on completion, error, or cancellation
and cannot leak into a result.

Ctrl-C exits 130 and wins at prompts. The interrupt handler is installed
before the command runs, and terminal prompts, stdin, and --data or
credential file reads run on a detached thread so a blocked read cannot
hold the process. A hidden prompt saves the terminal
attributes and restores them when cancelled, and a Ctrl-C typed at it,
which rpassword raises as SIGINT itself, is reported as an interruption
rather than an I/O failure.

Every write is recorded immediately before it is sent, and an in-flight
login change (token refresh, device login, or logout revoke) is recorded
until its local side is saved. An interruption reports that no resource
change was submitted, how to recover a login change, or an unknown
outcome with the original ID; it never claims a write was cancelled.

A fresh saved login is used without the credential lock, which is now
taken only to refresh, so parallel commands no longer run one at a time.
A const assertion keeps LOCK_TIMEOUT at least twice AUTH_TIMEOUT. Logout
keeps the lock across revoke so a concurrent refresh cannot leave the new
session live. TerminalModes' unsafe calls are an approved exception in
the style guide.
@thebrandonlucas

Copy link
Copy Markdown
Contributor Author

I'm good once @LightningK0ala comments are addressed

done

@tee8z
tee8z self-requested a review October 7, 2026 19:26
@thebrandonlucas
thebrandonlucas merged commit dfbd7a3 into master Oct 7, 2026
7 checks passed
@thebrandonlucas
thebrandonlucas deleted the feat/cli-interaction-progress branch October 7, 2026 20:51
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.

3 participants