sofos 0.1.11

An interactive AI coding agent for your terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# Sofos Code

![](https://github.com/alexylon/sofos-code/actions/workflows/rust.yml/badge.svg)   [![Crates.io](https://img.shields.io/crates/v/sofos.svg?color=blue)](https://crates.io/crates/sofos)

A blazingly fast, interactive AI coding assistant powered by Claude or GPT, implemented in pure Rust, that can generate code, edit files, and search the web - all from your terminal.

<div align="center"><img src="/assets/sofos_code.png" style="width: 800px;" alt="Sofos Code"></div>

## Table of Contents

- [Features]#features
- [Installation]#installation
  - [Requirements]#requirements
  - [Optional (but strongly recommended)]#optional-but-strongly-recommended
  - [Important: Gitignore Setup]#important-gitignore-setup
- [Usage]#usage
  - [Quick Start]#quick-start
  - [Commands]#commands
  - [Image Vision]#image-vision
- [Cost Tracking]#cost-tracking
  - [Options]#options
- [Extended Thinking]#extended-thinking
- [Custom Instructions]#custom-instructions
- [Session History]#session-history
- [Available Tools]#available-tools
- [Security]#security
  - [Bash Command Permissions (3-Tier System)]#bash-command-permissions-3-tier-system
  - [Config File]#config-file
- [Development]#development
- [Troubleshooting]#troubleshooting
- [Morph Integration]#morph-integration
- [License]#license
- [Acknowledgments]#acknowledgments
- [Links & Resources]#links--resources

## Features

- **Interactive REPL** - Multi-turn conversations with Claude or GPT
- **Image Vision** - Analyze local or web images by including paths/URLs in your message
- **Session History** - Automatic session saving and resume previous conversations
- **Custom Instructions** - Project and personal instruction files for context-aware assistance
- **File Operations** - Read, write, list, and create files/directories (sandboxed to current directory)
- **Ultra-Fast Editing** - Optional Morph Apply integration (10,500+ tokens/sec, 96-98% accuracy)
- **Code Search** - Fast regex-based code search using `ripgrep` (optional)
- **Web Search** - Real-time web information via Claude's and OpenAI's native search tools
- **Bash Execution** - Run tests and build commands safely (read-only, sandboxed)
- **Visual Diff Display** - See exactly what changed with colored diffs (red for deletions, blue for additions)
- **Iterative Tool Execution** - Sofos can use up to 200 tools per request for complex multi-file operations
- **Session Usage** – After exiting Sofos, a session usage is displayed, including the input and output tokens used and the estimated cost.
- **Secure** - All operations restricted to workspace, prevents directory traversal
- **Safe Mode** - Start or switch to a write-protected mode that limits tools to listing/reading files and web search; 
prompt changes from **`λ>`** to **`λ:`** as a visual cue

## Installation

### Requirements

- At least one LLM API key:
  - Anthropic API key ([get one]https://console.anthropic.com/) for Claude models
  - OpenAI API key ([get one]https://platform.openai.com/) for OpenAI models

### Optional (but strongly recommended)

- `ripgrep` for code search ([install guide]https://github.com/BurntSushi/ripgrep#installation)
- Morph API key for ultra-fast editing ([get one]https://morphllm.com/)

**Install with Homebrew**

```bash
brew tap alexylon/tap
brew install sofos
```

**Install from crates.io:**

*Requires Rust 1.70+ ([install guide](https://rust-lang.org/tools/install/))*

```bash
cargo install sofos
```

**Or build from source:**

```bash
git clone https://github.com/alexylon/sofos-code.git
cd sofos-code
cargo install --path .
```

### Important: Gitignore Setup

**Add `.sofos/` to your `.gitignore`** to avoid committing session history and personal settings:

```bash
# Add to your .gitignore
.sofos/
```

This directory contains sensitive data like conversation transcripts and personal instructions that shouldn't be shared.

**Note:** The `.sofosrc` file *should* be committed, as it contains team-wide project instructions.

## Usage

### Quick Start

```bash
# Set your API key (choose one)
export ANTHROPIC_API_KEY='your-anthropic-key'
# or
export OPENAI_API_KEY='your-openai-key'

# Optional: Enable ultra-fast editing
export MORPH_API_KEY='your-morph-key'

# Start Sofos
sofos
```

### Commands

- `/resume`   - Resume the previous session
- `/clear`    - Clear the conversation history
- `/think on` - Enable extended thinking
- `/think off`- Disable extended thinking
- `/think`    - Show the current thinking status
- `/s`        - Switch to safe mode (disables write/edit/delete tools). Prompt changes to **`λ:`**
- `/n`        - Switch back to normal mode (re-enables all tools). Prompt changes to **`λ>`**
- `/exit`, `/quit`, `/q`, or `Ctrl+D` - Exit and display a session cost summary
- `ESC` (while AI is responding) - Interrupt the response and provide additional guidance; the assistant will remember what was done before the interruption

**Command shortcuts:**

- Press **Tab** to show available commands (including partial matches on incomplete input) and navigate the completion menu; **Shift+Tab** moves to the previous item.
- Hit **Enter** to expand the currently highlighted completion.

### Image Vision

Include image paths or URLs directly in your message to have the AI analyze them:

```bash
# Local images (relative to workspace)
What's in this screenshot.png?
Describe ./images/diagram.jpg and explain the architecture

# Web images
Analyze this https://example.com/chart.png
What do you see in https://i.imgur.com/abc123.jpg?
```

**Supported formats:** JPEG, PNG, GIF, WebP (max 20MB for local files)

**Permission rules apply:** Local images outside workspace require explicit allow in config.

**Visual feedback:** When an image is detected, you'll see:
- `🔍 Detected 1 image reference(s)`
- `📷 Image loaded: local: path/to/image.png` (on success)
- `⚠️  Failed to load image: <error>` (on failure)

## Cost Tracking

Sofos automatically tracks token usage and calculates session costs. When you exit with `quit`, `exit`, or `Ctrl+D`, you'll see a summary:

```
──────────────────────────────────────────────────
Session Summary
──────────────────────────────────────────────────
Input tokens:      12,345
Output tokens:      5,678
Total tokens:      18,023

Estimated cost:     $0.1304
──────────────────────────────────────────────────
```

**Cost Calculation:**
- Costs are calculated based on official model pricing
- Claude models use official Anthropic pricing (e.g., Sonnet 4.5: $3/$15 per million input/output tokens)
- OpenAI models use official OpenAI pricing ($1.25/$10 per million tokens for gpt-5.1-codex and gpt-5.1-codex-max models)
- Accurate for standard API usage

### Options

```
-p, --prompt <TEXT>          One-shot mode
-s, --safe-mode              Start in read-only mode (only read/list/web-search tools; no writes or bash commands)
-r, --resume                 Resume a previous session
    --api-key <KEY>          Anthropic API key (overrides env var)
    --openai-api-key <KEY>   OpenAI API key (overrides env var)
    --morph-api-key <KEY>    Morph API key (overrides env var)
    --model <MODEL>          Model to use (default: claude-sonnet-4-5)
    --morph-model <MODEL>    Morph model (default: morph-v3-fast)
    --max-tokens <N>         Max response tokens (default: 8192)
-t, --enable-thinking        Enable extended thinking (default: false)
    --thinking-budget <N>    Token budget for thinking (default: 5120, must be < max-tokens)
-v, --verbose                Verbose logging
```

## Extended Thinking

Enable extended thinking for complex reasoning tasks that benefit from deeper analysis (disabled by default):

```bash
# Enable with default 5120 token budget
sofos --enable-thinking
# or use short flag
sofos -t

# Customize thinking budget (must be less than max-tokens)
sofos -t --thinking-budget 10000 --max-tokens 16000
```

**Note:** Thinking tokens count toward your API usage. Only enable for tasks that benefit from deeper reasoning.

## Custom Instructions

Provide project context to Sofos using instruction files:

**`.sofosrc`** (project root, version controlled)
- Shared with entire team
- Contains project conventions, architecture, coding standards
- See `.sofosrc` of this project's root for example

**`.sofos/instructions.md`** (gitignored, personal)
- Private to your workspace
- Your personal preferences and notes

Both files are loaded automatically at startup and appended to the system prompt.

## Session History

All conversations are automatically saved to `.sofos/sessions/` with both API messages (for continuing conversations) and display messages (for showing the original UI). 
Resume with `sofos --resume` or type `resume` in the REPL.

## Available Tools

Sofos can automatically use these tools:

**File Operations:**
- `read_file` - Read file contents
- `write_file` - Create or overwrite files
- `morph_edit_file` - Ultra-fast code editing (requires MORPH_API_KEY)
- `list_directory` - List directory contents
- `create_directory` - Create directories
- `delete_file` / `delete_directory` - Delete files/directories (with confirmation)
- `move_file` / `copy_file` - Move or copy files

**Code & Search:**
- `search_code` - Fast regex-based code search (requires `ripgrep`)
- `web_search` - Search the web for up-to-date information using Claude’s or OpenAI’s native web search
- `execute_bash` - Run tests and build commands (read-only, sandboxed)

When safe mode is enabled (via `--safe-mode` or `/s`), only `list_directory`, `read_file`, and `web_search` are available until you switch back with `/n`.

## Security

All file operations are sandboxed to your current working directory:

- ✅ Can access files in current directory and subdirectories
- ❌ Cannot access parent directories (`../`)
- ❌ Cannot access absolute paths (`/etc/passwd`)
- ❌ Cannot follow symlinks outside workspace

Bash execution is restricted to read-only operations:

- ✅ Can run tests and build commands (`cargo test`, `npm test`, etc.)
- ✅ Can read files and list directories (`cat`, `ls`, `grep`)
- ❌ Cannot use `sudo` or privilege escalation
- ❌ Cannot modify files (`rm`, `mv`, `cp`, `chmod`, `mkdir`, `touch`)
- ❌ Cannot change directories or use output redirection

### Bash Command Permissions (3-Tier System)

Sofos uses a 3-tier permission system for bash commands:

**Tier 1: Allowed (Predefined Safe Commands)**
These commands are automatically allowed without prompting:
- Build tools: `cargo`, `npm`, `go`, `make`, `python`, `pip`, etc.
- Read-only file operations: `ls`, `cat`, `grep`, `find`, `wc`, etc.
- System info: `pwd`, `whoami`, `date`, `env`, etc.
- Text processing: `sed`, `awk`, `sort`, `cut`, etc.
- Safe git commands (read-only)

**Tier 2: Forbidden (Predefined Dangerous Commands)**
These commands are always blocked:
- File deletion/modification: `rm`, `mv`, `cp`, `touch`, `mkdir`
- Permissions: `chmod`, `chown`, `chgrp`
- System control: `shutdown`, `reboot`, `systemctl`
- User management: `useradd`, `userdel`, `passwd`
- Process control: `kill`, `killall`
- Directory navigation: `cd`, `pushd`, `popd` (breaks sandbox)

**Tier 3: Unknown Commands (User Confirmation)**
Commands not in the predefined lists will prompt you for permission. You can:
- Allow once (temporary permission for this session)
- Remember decision (saved to `.sofos/config.local.toml` for future sessions)
- Deny once or permanently

### Config File

Your permission decisions are stored in configuration files:

**`~/.sofos/config.toml`** (global, optional)
- Applies to all Sofos workspaces on your machine
- Useful for personal preferences (e.g., always allow reading `~/.zshrc`)
- Gitignored by default (in your home directory)

**`.sofos/config.local.toml`** (workspace-specific, gitignored)
- Applies only to the current workspace
- Local settings override global settings when they conflict
- Same rule → local takes precedence
- Different rules → both apply

Example global config (`~/.sofos/config.toml`):
```toml
[permissions]
allow = [
  "Bash(custom_tool)",
  "Read(~/.zshrc)",
  "Read(~/.config/**)",
]
deny = []
ask = []
```

Example local config (`.sofos/config.local.toml`):
```toml
[permissions]
allow = [
  "Bash(custom_command_1)", 
  "Bash(custom_command_2:*)",
  "Read(/etc/hosts)",
]
deny = [
  "Bash(dangerous_command)",
  "Read(./.env)",
  "Read(./.env.*)",
  "Read(./secrets/**)",
]
ask = ["Bash(unknown_tool)"]
```

**Read Permission Rules:**
- Files inside workspace: allowed by default, denied if matched by `deny` rule
- Files outside workspace: denied by default, allowed only if matched by `allow` rule
- Supports glob patterns (`*` for single level, `**` for recursive)
- Supports tilde expansion (`~` expands to home directory)
- Paths are canonicalized before checking (symlinks resolved, `..` normalized)
- For outside workspace files, use absolute paths or tilde paths in config
- `ask` list only applies to Bash commands, not Read operations

**Bash Command Rules:**
- Always sandboxed to workspace (cannot access outside files even if Read allow rule exists)
- Commands in `allow` execute without prompts
- Commands in `deny` are always blocked
- Commands in `ask` prompt for permission each time
- Path arguments in commands are checked against Read deny rules

**Interactive Decisions:**
- When you approve/deny an unknown bash command with "remember", it's saved to `.sofos/config.local.toml`
- Global config is never modified automatically - edit it manually as needed

Both files are gitignored and local to your system.

Git commands are restricted to read-only operations:

- ✅ Can view history and status (`git status`, `git log`, `git diff`, `git show`)
- ✅ Can list branches and remotes (`git branch`, `git remote -v`)
- ✅ Can search and blame (`git grep`, `git blame`)
- ❌ Cannot push, pull, fetch, or clone (network operations)
- ❌ Cannot commit, add, or modify files (`git commit`, `git add`, `git reset --hard`)
- ❌ Cannot change branches or stash (`git checkout -b`, `git stash`, `git switch`)
- ❌ Cannot configure remotes (`git remote add`, `git remote set-url`)

**Best Practice:** Run `sofos` from your project directory and use git to track changes.

## Development

```bash
# Run tests
cargo test

# Build release
cargo build --release

# Debug logging
RUST_LOG=debug sofos
```

**Project Structure:**
- `src/` - Core Rust code
  - `api/` - Anthropic/OpenAI/Morph clients, shared API types/utilities
  - `tools/` - Sandboxed tools (filesystem, bash exec, web/code search, permissions, utils, tests)
  - `commands/` - Built-in REPL commands (e.g. `/resume`, `/clear`, safe mode)
  - `repl.rs` / `ui.rs` - Interactive REPL + terminal UI
  - `request_builder.rs` / `response_handler.rs` - LLM request/response plumbing + tool loop
  - `conversation.rs` / `history.rs` / `session_state.rs` / `session_selector.rs` - Conversation state, persistence, resume UI
  - `prompt.rs` / `model_config.rs` / `config.rs` / `cli.rs` - Prompt building, model selection, config + CLI flags
  - `diff.rs` / `syntax.rs` / `error.rs` / `error_ext.rs` - Diff rendering, syntax highlighting, error types/helpers
  - `main.rs` - Binary entry point
- `tests/` - Integration tests
- `assets/` - README images

See `.sofosrc` for detailed project conventions.

## Troubleshooting

- **API errors:** Check internet connection and API key
- **Path errors:** Use relative paths only, no `..` or absolute paths
- **Build errors:** Run `rustup update && cargo clean && cargo build`

## Morph Integration

Optional integration with Morph's Apply API for ultra-fast code editing:

- **10,500+ tokens/sec** - Lightning-fast edits
- **96-98% accuracy** - Reliable code modifications
- **Direct REST API** - No additional dependencies
- **Optional** - Enable with `MORPH_API_KEY`

## License

MIT License

## Acknowledgments

Built with Rust and powered by Anthropic's Claude or OpenAI's GPT. Morph Apply integration for fast edits. Inspired by Aider and similar tools.

## Links & Resources

- [GitHub]https://github.com/alexylon/sofos-code
- [Crates.io]https://crates.io/crates/sofos

---

**Disclaimer:** Sofos Code may make mistakes. Always review generated code before use.

[![forthebadge](https://forthebadge.com/images/badges/made-with-rust.svg)](https://forthebadge.com)