|
| 1 | +# TTY-less Environment Support |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The Continue CLI supports running in TTY-less environments (environments without a terminal/TTY), which is essential for: |
| 6 | + |
| 7 | +- VSCode and IntelliJ extensions using the `run_terminal_command` tool |
| 8 | +- Docker containers without TTY allocation |
| 9 | +- CI/CD pipelines |
| 10 | +- Automated scripts and tools |
| 11 | +- Background processes |
| 12 | + |
| 13 | +## Architecture |
| 14 | + |
| 15 | +### Mode Separation |
| 16 | + |
| 17 | +The CLI has two distinct execution modes with complete separation: |
| 18 | + |
| 19 | +1. **Interactive Mode (TUI)**: Requires a TTY, uses Ink for rendering |
| 20 | +2. **Headless Mode**: Works in TTY-less environments, outputs to stdout/stderr |
| 21 | + |
| 22 | +``` |
| 23 | +┌─────────────────────────────────────────────────────────────┐ |
| 24 | +│ CLI Entry Point │ |
| 25 | +│ (src/index.ts) │ |
| 26 | +└────────────────────────┬────────────────────────────────────┘ |
| 27 | + │ |
| 28 | + ┌────────────┴────────────┐ |
| 29 | + │ │ |
| 30 | + ┌───────▼────────┐ ┌───────▼─────────┐ |
| 31 | + │ Interactive │ │ Headless │ |
| 32 | + │ Mode (TUI) │ │ Mode (-p) │ |
| 33 | + │ │ │ │ |
| 34 | + │ • Requires TTY │ │ • No TTY needed │ |
| 35 | + │ • Uses Ink │ │ • Stdin/stdout │ |
| 36 | + │ • Keyboard UI │ │ • One-shot exec │ |
| 37 | + └────────────────┘ └─────────────────┘ |
| 38 | +``` |
| 39 | + |
| 40 | +### Safeguards Implemented |
| 41 | + |
| 42 | +#### 1. **TTY Detection Utilities** (`src/util/cli.ts`) |
| 43 | + |
| 44 | +```typescript |
| 45 | +// Check if running in TTY-less environment |
| 46 | +export function isTTYless(): boolean; |
| 47 | + |
| 48 | +// Check if environment supports interactive features |
| 49 | +export function supportsInteractive(): boolean; |
| 50 | + |
| 51 | +// Check if prompt was supplied via CLI arguments |
| 52 | +export function hasSuppliedPrompt(): boolean; |
| 53 | +``` |
| 54 | + |
| 55 | +#### 2. **Stdin Reading Protection** (`src/util/stdin.ts`) |
| 56 | + |
| 57 | +Prevents stdin reading when: |
| 58 | + |
| 59 | +- In headless mode with supplied prompt |
| 60 | +- `FORCE_NO_TTY` environment variable is set |
| 61 | +- In test environments |
| 62 | + |
| 63 | +This avoids blocking/hanging in TTY-less environments where stdin is not available or not readable. |
| 64 | + |
| 65 | +#### 3. **TUI Initialization Guards** (`src/ui/index.ts`) |
| 66 | + |
| 67 | +The `startTUIChat()` function now includes multiple safeguards: |
| 68 | + |
| 69 | +- **Headless mode check**: Throws error if called in headless mode |
| 70 | +- **TTY-less check**: Throws error if no TTY is available |
| 71 | +- **Raw mode test**: Validates stdin supports raw mode (required by Ink) |
| 72 | +- **Explicit stdin/stdout**: Passes streams explicitly to Ink |
| 73 | + |
| 74 | +```typescript |
| 75 | +// Critical safeguard: Prevent TUI in headless mode |
| 76 | +if (isHeadlessMode()) { |
| 77 | + throw new Error("Cannot start TUI in headless mode"); |
| 78 | +} |
| 79 | + |
| 80 | +// Critical safeguard: Prevent TUI in TTY-less environment |
| 81 | +if (isTTYless() && !customStdin) { |
| 82 | + throw new Error("Cannot start TUI in TTY-less environment"); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +#### 4. **Headless Mode Validation** (`src/commands/chat.ts`) |
| 87 | + |
| 88 | +Ensures headless mode has all required inputs: |
| 89 | + |
| 90 | +```typescript |
| 91 | +if (!prompt) { |
| 92 | + throw new Error("Headless mode requires a prompt"); |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +#### 5. **Logger Configuration** (`src/util/logger.ts`) |
| 97 | + |
| 98 | +Configures output handling for TTY-less environments: |
| 99 | + |
| 100 | +- Sets UTF-8 encoding |
| 101 | +- Leaves stdout/stderr buffering unchanged in headless mode. |
| 102 | +- Disables progress indicators |
| 103 | + |
| 104 | +## Usage Examples |
| 105 | + |
| 106 | +### From VSCode/IntelliJ Extension |
| 107 | + |
| 108 | +```typescript |
| 109 | +// Using the run_terminal_command tool |
| 110 | +const command = 'cn -p "Analyze the current git diff"'; |
| 111 | +const result = await runTerminalCommand(command); |
| 112 | +``` |
| 113 | + |
| 114 | +### From Docker Container |
| 115 | + |
| 116 | +```bash |
| 117 | +# Without TTY allocation (-t flag) |
| 118 | +docker run --rm my-image cn -p "Generate a README" |
| 119 | +``` |
| 120 | + |
| 121 | +### From CI/CD Pipeline |
| 122 | + |
| 123 | +```yaml |
| 124 | +- name: Run Continue CLI |
| 125 | + run: | |
| 126 | + cn -p "Review code changes" --format json |
| 127 | +``` |
| 128 | +
|
| 129 | +### From Automated Script |
| 130 | +
|
| 131 | +```bash |
| 132 | +#!/bin/bash |
| 133 | +# Non-interactive script |
| 134 | +cn -p "Generate commit message for current changes" --silent |
| 135 | +``` |
| 136 | + |
| 137 | +## Environment Variables |
| 138 | + |
| 139 | +- `FORCE_NO_TTY`: Forces TTY-less mode, prevents stdin reading |
| 140 | +- `CONTINUE_CLI_TEST`: Marks test environment, prevents stdin reading |
| 141 | + |
| 142 | +## Testing |
| 143 | + |
| 144 | +### TTY-less Test |
| 145 | + |
| 146 | +```typescript |
| 147 | +const result = await runCLI(context, { |
| 148 | + args: ["-p", "Hello, world!"], |
| 149 | + env: { |
| 150 | + FORCE_NO_TTY: "true", |
| 151 | + }, |
| 152 | +}); |
| 153 | +``` |
| 154 | + |
| 155 | +### Expected Behavior |
| 156 | + |
| 157 | +- ✅ Should not hang on stdin |
| 158 | +- ✅ Should not attempt to initialize Ink |
| 159 | +- ✅ Should output results to stdout |
| 160 | +- ✅ Should exit cleanly |
| 161 | + |
| 162 | +## Error Messages |
| 163 | + |
| 164 | +### Attempting TUI in TTY-less Environment |
| 165 | + |
| 166 | +``` |
| 167 | +Error: Cannot start TUI in TTY-less environment. No TTY available for interactive mode. |
| 168 | +For non-interactive use, run with -p flag: |
| 169 | + cn -p "your prompt here" |
| 170 | +``` |
| 171 | + |
| 172 | +### Missing Prompt in Headless Mode |
| 173 | + |
| 174 | +``` |
| 175 | +Error: A prompt is required when using the -p/--print flag, unless --prompt or --agent is provided. |
| 176 | +
|
| 177 | +Usage examples: |
| 178 | + cn -p "please review my current git diff" |
| 179 | + echo "hello" | cn -p |
| 180 | + cn -p "analyze the code in src/" |
| 181 | + cn -p --agent my-org/my-agent |
| 182 | +``` |
| 183 | + |
| 184 | +## Troubleshooting |
| 185 | + |
| 186 | +### CLI Hangs in Docker/CI |
| 187 | + |
| 188 | +**Cause**: CLI attempting to read stdin in TTY-less environment |
| 189 | + |
| 190 | +**Solution**: Ensure using `-p` flag with a prompt: |
| 191 | + |
| 192 | +```bash |
| 193 | +cn -p "your prompt" --config config.yaml |
| 194 | +``` |
| 195 | + |
| 196 | +### "Cannot start TUI" Error |
| 197 | + |
| 198 | +**Cause**: Attempting interactive mode in TTY-less environment |
| 199 | + |
| 200 | +**Solution**: Use headless mode: |
| 201 | + |
| 202 | +```bash |
| 203 | +cn -p "your prompt" |
| 204 | +``` |
| 205 | + |
| 206 | +### Raw Mode Error |
| 207 | + |
| 208 | +**Cause**: Terminal doesn't support raw mode (required by Ink) |
| 209 | + |
| 210 | +**Solution**: Use headless mode instead of interactive mode |
| 211 | + |
| 212 | +## Design Principles |
| 213 | + |
| 214 | +1. **Fail Fast**: Detect environment early and fail with clear messages |
| 215 | +2. **Explicit Separation**: No code path should allow Ink to load in headless mode |
| 216 | +3. **No Blocking**: Never block on stdin in TTY-less environments |
| 217 | +4. **Clear Errors**: Provide actionable error messages with examples |
| 218 | +5. **Testing**: Comprehensive tests for TTY-less scenarios |
| 219 | + |
| 220 | +## Implementation Checklist |
| 221 | + |
| 222 | +- [x] Add TTY detection utilities |
| 223 | +- [x] Protect stdin reading in headless mode |
| 224 | +- [x] Guard TUI initialization |
| 225 | +- [x] Validate headless mode inputs |
| 226 | +- [x] Configure logger for TTY-less output |
| 227 | +- [x] Update test helpers |
| 228 | +- [x] Add TTY-less tests |
| 229 | +- [x] Document TTY-less support |
| 230 | + |
| 231 | +## Related Files |
| 232 | + |
| 233 | +- `src/util/cli.ts` - TTY detection utilities |
| 234 | +- `src/util/stdin.ts` - Stdin reading protection |
| 235 | +- `src/ui/index.ts` - TUI initialization guards |
| 236 | +- `src/commands/chat.ts` - Mode routing and validation |
| 237 | +- `src/util/logger.ts` - Output configuration |
| 238 | +- `src/test-helpers/cli-helpers.ts` - Test support |
| 239 | +- `src/e2e/headless-minimal.test.ts` - TTY-less tests |
0 commit comments