Skip to content

cli: attach to running pi sessions by name or UUID#59

Merged
benvinegar merged 3 commits into
mainfrom
bentlegen/pi-attach-cli
Feb 19, 2026
Merged

cli: attach to running pi sessions by name or UUID#59
benvinegar merged 3 commits into
mainfrom
bentlegen/pi-attach-cli

Conversation

@benvinegar

Copy link
Copy Markdown
Member

Summary

Make baudbot attach useful in systemd + pi-session-control mode (where tmux may be empty).

What changes

  • baudbot attach now supports pi session attach by name or UUID:
    • sudo baudbot attach (auto: tmux first, then pi fallback)
    • sudo baudbot attach --pi control-agent
    • sudo baudbot attach --pi <uuid>
    • sudo baudbot attach control-agent (auto mode, resolves to pi when no tmux)
  • baudbot sessions alias mapping improved to show name → id correctly for current alias formats.

Details

  • Added pi session resolution helpers in bin/baudbot:
    • resolve by alias (*.alias), legacy symlink alias (*.sock), exact UUID, and unique UUID prefix
  • Added attach --help usage examples
  • Preserved existing tmux attach behavior (--tmux to force)

Why

With systemd-managed runtime, users often have live pi sessions but no tmux sessions. This makes one-command attach work in both modes without forcing users to manually run pi --session <uuid>.

Quick test plan

  1. sudo baudbot sessions → verify named pi sessions show with UUIDs.
  2. sudo baudbot attach --pi control-agent → enters control-agent pi session.
  3. Detach with Ctrl+C twice.
  4. sudo baudbot attach in an environment with no tmux sessions → should fall back to pi attach.

@greptile-apps

greptile-apps Bot commented Feb 18, 2026

Copy link
Copy Markdown

Greptile Summary

Extends baudbot attach to support pi sessions in addition to tmux, making it functional in systemd-managed environments where tmux sessions may not exist. Adds session resolution by name (alias), UUID, or unique UUID prefix, with --pi / --tmux flags to force a specific backend and auto-fallback when neither is specified. The sessions subcommand is also updated to handle both the new .alias symlink format and the legacy .sock symlink format for alias resolution.

  • Pi session resolution: New helpers (pi_control_dir, pi_alias_to_uuid, resolve_pi_session_id) handle alias lookup (.alias and legacy .sock symlinks), exact UUID match, and unique UUID prefix match.
  • Attach refactor: attach now has proper argument parsing (--pi, --tmux, --help), mode dispatch, and structured fallback (tmux → pi → error).
  • Sessions display: Alias collection updated to support both .alias and .sock symlink formats, and socket liveness check simplified to python3 only (socat fallback removed).
  • Minor issue: Ambiguous UUID prefix matches can show contradictory error messages (ambiguity warning followed by "not found").

Confidence Score: 4/5

  • This PR is safe to merge with minor improvements recommended for error messaging clarity.
  • The changes are well-structured with clear separation of concerns (resolution helpers, attach helpers, mode dispatch). The core logic for session resolution is thorough, covering alias formats, exact UUID, and prefix matching. Two minor issues were identified: (1) a defensive quoting improvement for the nested shell command in attach_pi_session, and (2) contradictory error messages when an ambiguous UUID prefix is provided. Neither issue would cause data loss or security problems in normal usage.
  • bin/baudbot — specifically the attach_pi_session shell quoting (line 344) and the error path for ambiguous pi session matches (lines 386-392)

Important Files Changed

Filename Overview
bin/baudbot Adds pi session resolution helpers and extends attach with --pi/--tmux mode flags and auto-fallback. Well-structured refactor with good backward compatibility. Minor issues: nested shell quoting could be hardened, and ambiguous prefix matches produce a contradictory error message.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["baudbot attach [--pi|--tmux] [target]"] --> B{ATTACH_MODE?}
    B -->|"--tmux"| C["choose_tmux_target(TARGET)"]
    C -->|found| D["exec tmux attach-session"]
    C -->|not found| E["❌ tmux session not found"]
    B -->|"--pi"| F["choose_pi_target(TARGET)"]
    F -->|found| G["exec pi --session UUID"]
    F -->|not found| H["❌ pi session not found"]
    B -->|auto| I["choose_tmux_target(TARGET)"]
    I -->|found| D
    I -->|not found| J["choose_pi_target(TARGET)"]
    J -->|found| G
    J -->|not found| K["❌ No matching session found"]

    F --> L["resolve_pi_session_id()"]
    J --> L
    L --> M{query provided?}
    M -->|no| N["control-agent.alias → UUID"]
    M -->|no| O["first *.sock file"]
    M -->|yes| P["exact UUID match"]
    M -->|yes| Q[".alias symlink"]
    M -->|yes| R[".sock symlink (legacy)"]
    M -->|yes| S["unique UUID prefix"]
Loading

Last reviewed commit: af247d1

@greptile-apps greptile-apps Bot 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.

1 file reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Comment thread bin/baudbot
echo "Attaching to pi session: $pi_target"
echo "Detach with: Ctrl+C twice"
echo ""
exec sudo -u "$AGENT_USER" bash -lc "export PATH='$AGENT_HOME/.varlock/bin:$AGENT_HOME/opt/node-v22.14.0-linux-x64/bin':\$PATH; cd ~; pi --session '$pi_target'"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unquoted interpolation in nested shell string

$pi_target is interpolated inside single quotes within a double-quoted bash -lc string. If the resolved UUID/alias somehow contains a single quote, it would break out of the quoting and allow command injection as baudbot_agent. While the current resolution logic (filesystem-derived basenames) makes this unlikely, the same pattern for $sock on line 272 was flagged in the pre-existing code — defensive quoting is a good practice here, especially since this runs via sudo.

Consider using printf '%q' to safely escape the value:

Suggested change
exec sudo -u "$AGENT_USER" bash -lc "export PATH='$AGENT_HOME/.varlock/bin:$AGENT_HOME/opt/node-v22.14.0-linux-x64/bin':\$PATH; cd ~; pi --session '$pi_target'"
exec sudo -u "$AGENT_USER" bash -lc "export PATH='$AGENT_HOME/.varlock/bin:$AGENT_HOME/opt/node-v22.14.0-linux-x64/bin':\$PATH; cd ~; pi --session $(printf '%q' "$pi_target")"
Prompt To Fix With AI
This is a comment left during a code review.
Path: bin/baudbot
Line: 344

Comment:
**Unquoted interpolation in nested shell string**

`$pi_target` is interpolated inside single quotes within a double-quoted `bash -lc` string. If the resolved UUID/alias somehow contains a single quote, it would break out of the quoting and allow command injection as `baudbot_agent`. While the current resolution logic (filesystem-derived basenames) makes this unlikely, the same pattern for `$sock` on line 272 was flagged in the pre-existing code — defensive quoting is a good practice here, especially since this runs via `sudo`.

Consider using `printf '%q'` to safely escape the value:

```suggestion
      exec sudo -u "$AGENT_USER" bash -lc "export PATH='$AGENT_HOME/.varlock/bin:$AGENT_HOME/opt/node-v22.14.0-linux-x64/bin':\$PATH; cd ~; pi --session $(printf '%q' "$pi_target")"
```

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Great callout. I agree this is worth hardening with explicit shell escaping (printf %q) before interpolation into the nested bash -lc command.\n\nI can’t safely land this directly from this environment because bin/* is admin-managed and guarded by the pre-commit security hook, but I’ve queued it for the admin-managed patch on this PR.\n\nResponded by pi-coding-agent using openai/gpt-5.

Comment thread bin/baudbot
Comment on lines +386 to +392
if [ "$ATTACH_MODE" = "pi" ]; then
if pi_target=$(choose_pi_target "$TARGET"); then
attach_pi_session "$pi_target"
fi
echo "❌ pi session not found. See: sudo baudbot sessions"
exit 1
fi

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Misleading error after ambiguous prefix match

When resolve_pi_session_id finds multiple sessions matching a prefix, it writes "❌ Multiple pi sessions match..." to stderr and returns exit code 2. choose_pi_target then returns 1, and the user also sees "❌ pi session not found" on stdout — which contradicts the first message (the session wasn't "not found", multiple were found).

Consider distinguishing the exit codes so you can suppress the generic error when an ambiguity message was already shown:

    if [ "$ATTACH_MODE" = "pi" ]; then
      if pi_target=$(choose_pi_target "$TARGET"); then
        attach_pi_session "$pi_target"
      fi
      # resolve_pi_session_id already printed a specific error for ambiguous matches
      if [ -z "$TARGET" ]; then
        echo "❌ No pi sessions found. Start the agent first: sudo baudbot start"
      else
        echo "❌ pi session '$TARGET' not found. See: sudo baudbot sessions"
      fi
      exit 1
    fi
Prompt To Fix With AI
This is a comment left during a code review.
Path: bin/baudbot
Line: 386-392

Comment:
**Misleading error after ambiguous prefix match**

When `resolve_pi_session_id` finds multiple sessions matching a prefix, it writes `"❌ Multiple pi sessions match..."` to stderr and returns exit code 2. `choose_pi_target` then returns 1, and the user also sees `"❌ pi session not found"` on stdout — which contradicts the first message (the session wasn't "not found", multiple were found).

Consider distinguishing the exit codes so you can suppress the generic error when an ambiguity message was already shown:

```
    if [ "$ATTACH_MODE" = "pi" ]; then
      if pi_target=$(choose_pi_target "$TARGET"); then
        attach_pi_session "$pi_target"
      fi
      # resolve_pi_session_id already printed a specific error for ambiguous matches
      if [ -z "$TARGET" ]; then
        echo "❌ No pi sessions found. Start the agent first: sudo baudbot start"
      else
        echo "❌ pi session '$TARGET' not found. See: sudo baudbot sessions"
      fi
      exit 1
    fi
```

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed — that generic not found message is misleading after an ambiguity failure.\n\nPlan is to propagate and branch on the resolver exit code (e.g. preserve code 2 for ambiguous match) so we suppress the generic not found message when a specific ambiguity message was already emitted.\n\nI can’t land that edit directly from this environment because bin/* is admin-managed and blocked by the security pre-commit hook, but this is queued for the admin-managed patch on this PR.\n\nResponded by pi-coding-agent using openai/gpt-5.

@benvinegar benvinegar force-pushed the bentlegen/pi-attach-cli branch from 0c88bb9 to 14b988a Compare February 19, 2026 05:15
@benvinegar benvinegar merged commit d287a08 into main Feb 19, 2026
9 checks passed
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.

1 participant