Skip to main content

Interactive Mode

cosh-shell runs bash/zsh on top of a native PTY and marks command boundaries using OSC escape sequences, enabling seamless integration of AI analysis and tool approval.

PTY Host

cosh-shell creates a pseudo-terminal pair via openpty() and starts a bash or zsh subprocess on the slave end:

┌──────────────────────┐
│ cosh-shell │
│ ┌────────────────┐ │ ┌──────────────┐
│ │ PTY master │──────────│ bash/zsh │
│ └────────────────┘ │ │ (PTY slave) │
│ ┌────────────────┐ │ └──────────────┘
│ │ OSC Parser │ │
│ └────────────────┘ │
└──────────────────────┘

Shell Selection

cosh-shell # Default: auto (auto-detect)
cosh-shell --shell zsh # Use zsh
cosh-shell raw qwen --shell zsh

Shell selection priority:

  1. --shell argument
  2. COSH_SHELL_RAW_SHELL environment variable
  3. Configuration file shell.default
  4. Auto-detect (default bash)

Run Modes

ModeDescription
Default (no subcommand)Start interactive shell with configured adapter
raw [adapter]Explicitly specify adapter
-c <command>Execute command then exit (pass-through to underlying shell)
-- <command>Execute command directly then exit
--isolatedIsolated mode, do not load user rcfile
--login / -lStart as login shell

OSC Marking System

cosh-shell injects a custom rcfile (bashrc/zshrc) into the child shell, marking command lifecycle via OSC 1337 escape sequences:

ESC]1337;COSH;<payload>BEL

Marked events include:

  • Shell ready (prompt displayed)
  • Command execution start (preexec)
  • Command execution end (precmd, carries exit code)
  • Working directory change

These markers enable cosh-shell to precisely identify:

  1. When the user enters a command
  2. When a command starts/ends
  3. The command's exit code
  4. Current working directory

Input Classification

User input in the shell is classified into the following types:

TypeDescriptionExample
Shell CommandNormal shell commandls -la, git status
Slash CommandBuilt-in control command starting with //help, /mode
Natural LanguageNatural language questionHandled by AI adapter
Agent MarkerAgent execution markerInternal use

Slash Commands

CommandDescription
/helpDisplay help information
/mode [approval|analysis] [value]View or switch mode
/config [key] [value]View or modify runtime configuration
/hooks [list|enable|disable] [name]Manage hooks
/extensions [list|enable|disable] [name]Manage extensions
/skills [list|enable|disable] [name]Manage skills
/debug [state|events|adapter]Debug information
/authTrigger authentication flow

Startup Flow

  1. Parse command-line arguments (shell type, adapter, mode)
  2. Install terminal restore handler (restore termios on SIGTERM/SIGHUP/panic)
  3. Load configuration (~/.copilot-shell/config.toml)
  4. Initialize logging (file output to ~/.copilot-shell/logs/)
  5. Create PTY session, start bash/zsh subprocess
  6. Inject OSC marking script
  7. Start AI adapter connection
  8. Render startup banner (COSH logo + adapter/shell/approval mode info)
  9. Enter main event loop

Terminal Restore

cosh-shell automatically restores terminal state (termios) in the following scenarios:

  • Process receives SIGTERM / SIGHUP / SIGQUIT
  • Panic triggered
  • Normal exit

Ensures terminal is not left in raw mode after abnormal exit.

Native Mode vs Isolated Mode

FeatureNative Mode (default)Isolated Mode (--isolated)
User rcfileLoaded (~/.bashrc etc.)Skipped
PS1 promptPreserves user settingsUses cosh-osc$
HistoryLoads $HISTFILENot loaded
Environment variablesInheritedMinimized

Session Working Directory

cosh-shell creates a working directory for each session under ~/.copilot-shell/, storing:

  • OSC marking scripts
  • Command output references (auto-cleaned after 24 hours)
  • Terminal restore request files
  • Shell handoff request files