Repository navigation
fix(agent-mode): keep the typed buffer when running the setup command - #1011
Conversation
Pressing Enter on "flyline can set it up for you" replaced the buffer with the `flyline set-agent-mode ...` command and submitted it, discarding the question the user had just typed. Stash the buffer in `settings.initial_buffer` first so the next prompt comes back pre-filled with it, ready to submit now that agent mode is configured. Also collapse the two duplicate `AgentError` match arms into one. Additionally, `set-agent-mode` now prints the shell-quoted equivalent line to add to `.bashrc` (or `programs.bash.interactiveShellInit` on Nix), since the setting only lives for the current shell. The hint is skipped when no app session has run yet, so it stays quiet when invoked from a startup file.
96dbc3c to
0731a71
Compare
| .into_iter() | ||
| .find(|(cmd_name, _)| shell::backend().command_info(cmd_name).is_known()) | ||
| .map(|(_, flyline_cmd)| flyline_cmd); | ||
| .map(|(_, flyline_cmd)| format!("{}\n\n# Make sure you put the above command into your ~/.bashrc!", flyline_cmd)); |
There was a problem hiding this comment.
Thanks for the fix!
I've changed this PR a bit. The code to print the warning about adding the command to your bashrc would misfire when the user sources their bashrc mid session if they did have a flyline set-agent-mode command in their bashrc. I think it is much simpler and more correct to just add the reminder here.
All settings that I mention in the readme need to be added the users bashrc for permanence. So it seemed strange to have a warning in the readme about adding the setting to the bashrc just for agent mode. I think reminding the user in this code snippet is useful.
I got rid of the nixos bash setup wording. I believe that "add X to your .bashrc" will be interpreted by a beginner as literally add X to your ~/.bashrc file but all advanced users (and hence all nixos users) interpret it as "however you configure your shell startup, add X to your shell startup". So I prefer to just write "add X to your .bashrc" than try to enumerate all ways users might configure flyline.
|
let me know if you're happy with my changes and Ill merge |
Lgtm |
## Summary A [thefuck](https://github.com/nvbn/thefuck)-style fix built on agent mode: one key sends the failed command and its exit status to the configured agent. The suggestions appear in the existing agent-output picker. ```diff Alt+Enter - always -> runAgentMode + empty buffer -> fixLastCommand # works on legacy terminals + otherwise -> runAgentMode +Alt+Shift+Enter / Meta+Shift+Enter + always -> fixLastCommand # needs extended key codes ``` ```text fixLastCommand agent not configured empty buffer -> put last command in buffer # the setup flow from #1011 keeps it show setup dialog empty buffer -> "This Bash command exited with status $?. Fix it:\n<last command>" otherwise -> "Fix this Bash command:\n<buffer, trigger prefix stripped>" start_agent_mode(prompt) ``` The command comes from bash's own history, gated by what this shell just submitted: ```text on submit (not a PS2 continuation) last_command = None remember bash's last history entry (pointer) next prompt keep bash's last history line only if bash added a new entry # our command, already !!-expanded and multi-line joined or it equals what we submitted # ignoredups otherwise last_command stays None # ignorespace / HISTIGNORE / set +o history ``` So anything bash keeps out of history is never sent to the agent. Reading "the last history entry" directly wasn't enough: on the default Bash backend, entries have no session, so it could be yesterday's line from another terminal. ## Evidence - `cargo test --lib`: 873 passed. `cargo clippy --workspace --all-features -D warnings` and `cargo fmt --check` are clean. - Live bash run through tmux with `HISTCONTROL=ignoreboth`, using a stub agent that records the prompt it receives: | Typed | Agent receives | |---|---| | `false` | `This Bash command exited with status 1. Fix it:\nfalse` | | ` secret-cmd --token=abc` | *(not called)* | | `ls /nope` twice (ignoredups) | `…status 2. Fix it:\nls /nope` | | `for i in 1; do` ⏎ `false; done` | `…status 1. Fix it:\nfor i in 1; do false; done` | | `true`, then ` secret2` | *(not called)* | | `echo hi`, then `!!` (a dup under ignoredups) | *(not called)*: safe direction, no fix offered | - There's no automated test for the handler. `App` can't be built in lib tests, and the docker harness has no agent-mode coverage. ## Merge Danger **Door:** two-way **Blast Radius:** keybindings - **Alt+Enter on an empty buffer changes meaning.** Before, it ran the agent with an empty prompt. Now it fixes the previous command. `start_agent_mode` has a TODO about empty-buffer UX (maybe opening agent-prompt history search); this change takes that slot. @HalFrgrd, is that OK? - **Privacy:** only commands bash keeps in history are ever sent. `check_add_history` couldn't be the filter, because it also adds the line to history. - **After the setup dialog, the fix context is lost.** The command comes back in the buffer, but its exit status doesn't, and Alt+Enter on a non-empty buffer sends it as a plain question.
Problem
When agent mode isn't configured yet, flyline offers to set it up:
Pressing Enter replaces the buffer with the setup command and submits it, so the question you just typed is gone. You configure agent mode and then have to retype the thing you wanted to ask.
Second issue:
set-agent-modeonly configures the current shell, and nothing tells you that. Next shell, same dialog.Changes
AgentModeRunHelpCommandstashes the current buffer intosettings.initial_bufferbefore replacing it, so the next prompt comes back pre-filled with the original text, ready to submit, since agent mode is configured. Also collapses the two duplicateContentMode::AgentErrormatch arms into one.flyline set-agent-modeprints the shell-quoted equivalent line to copy into.bashrc(orprograms.bash.interactiveShellIniton Nix):The hint is gated on
settings.last_app_closed_at.is_some(), i.e. at least one prompt session has run, so it stays quiet when the same command is sourced from a startup file. Re-quoting goes throughshlex::try_join, so the printed line is paste-safe.Notes
.bashrcand the Nix option. Sniffing the loadedlibflyline.sopath would need/proc/self/mapsand has no macOS equivalent, so it didn't seem worth it.Testing
cargo test --lib— 872 passed, 0 failed (includes a new round-trip test for the quoted hint line).cargo fmt --all -- --check,cargo doc --workspace --no-deps --document-private-items(no new warnings in touched files),cargo build --release.cargo clippynot run — no clippy component available in my local toolchain.