Skip to content

fix(agent-mode): keep the typed buffer when running the setup command - #1011

Merged
HalFrgrd merged 3 commits into
HalFrgrd:masterfrom
jakob1379:fix/agent-mode-setup-keeps-buffer
Sep 28, 2026
Merged

HalFrgrd merged 3 commits into
HalFrgrd:masterfrom
jakob1379:fix/agent-mode-setup-keeps-buffer

Conversation

@jakob1379

@jakob1379 jakob1379 commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Problem

When agent mode isn't configured yet, flyline offers to set it up:

❯ why does it do this?
Agent mode is not configured. However, flyline can set it up for you:
Press Enter to run `flyline set-agent-mode --system-prompt "..." --trigger-prefix ': ' --command 'claude --effort low --print'`.

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-mode only configures the current shell, and nothing tells you that. Next shell, same dialog.

Changes

  1. AgentModeRunHelpCommand stashes the current buffer into settings.initial_buffer before 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 duplicate ContentMode::AgentError match arms into one.

  2. flyline set-agent-mode prints the shell-quoted equivalent line to copy into .bashrc (or programs.bash.interactiveShellInit on Nix):

Agent mode is set for this shell only. To persist, add this to ~/.bashrc (Nix: programs.bash.interactiveShellInit):
  flyline set-agent-mode --system-prompt 'Be concise. ...' --trigger-prefix ': ' --command 'claude --effort low --print'

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 through shlex::try_join, so the printed line is paste-safe.

Notes

  • Installation method isn't detected; the one hint line names both .bashrc and the Nix option. Sniffing the loaded libflyline.so path would need /proc/self/maps and has no macOS equivalent, so it didn't seem worth it.
  • README agent-mode section updated for both behaviours.

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 clippy not run — no clippy component available in my local toolchain.

@jakob1379
jakob1379 marked this pull request as ready for review September 24, 2026 04:54
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.
@jakob1379
jakob1379 force-pushed the fix/agent-mode-setup-keeps-buffer branch from 96dbc3c to 0731a71 Compare September 25, 2026 06:41
Comment thread src/app/mod.rs Outdated
.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));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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.

@HalFrgrd

Copy link
Copy Markdown
Owner

let me know if you're happy with my changes and Ill merge

@jakob1379

Copy link
Copy Markdown
Contributor Author

let me know if you're happy with my changes and Ill merge

Lgtm

@HalFrgrd
HalFrgrd merged commit 2fec687 into HalFrgrd:master Sep 28, 2026
7 checks passed
HalFrgrd pushed a commit that referenced this pull request Oct 10, 2026
## 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.
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