# Session Patterns

Use this reference when interactive shell handling is the hard part of the task.

## Shell Compatibility

- Do not assume features like `/dev/tcp`, brace expansion, or shell redirection syntax work the same across shells.
- If the current shell is uncertain, verify it from recent output or use an explicit interpreter only when that interpreter is clearly present.
- Prefer observable checks over hidden assumptions. A short probe is better than a long command chain built on the wrong shell family.

## Output-Driven Sequencing

- If the next step depends on readiness text, prompts, or listener state, wait for that output instead of polling blindly.
- Use ANSI-stripped terminal reads when you need to parse text such as prompts, bind errors, or callback success lines.
- Keep read and wait loops tight so you do not act on stale terminal state.

## Session Control

- Use real control-key input when a process must be interrupted, resumed, or detached.
- Treat interactive sessions as stateful. Re-check the prompt or output after interrupts before sending the next command.

## PTY and Shell Stabilization

- If the task is to stabilize a raw shell, first confirm what runtime is available before picking a PTY upgrade path.
- Prefer the simplest upgrade path that is already supported by the observed environment instead of assuming extra tools.
