# Codjent Sample App

Codjent (`github.com/ijaidev/gojent/codjent`) is an ACP-first local coding agent built on the Gojent runtime to exercise the loop, typed tools, MCP client, policies, managed subagents, and persistence end to end. **Codjent is a sample application, not a supported product**—it omits authentication, multi-tenant isolation, and hosted deployment features, and exists as a concrete reference you can inspect or copy from.

## Install and run

Codjent lives in its own Go module (`codjent/`) and requires Go 1.25 or newer:

```bash
go install github.com/ijaidev/gojent/codjent/cmd/codjent@latest
codjent          # open the Bubble Tea provider & model manager TUI
codjent acp      # serve ACP v2 over stdio for external clients such as T3 Code
```

Or run directly from a repository checkout:

```bash
just --justfile codjent/justfile run   # interactive provider manager TUI
just --justfile codjent/justfile acp   # ACP v2 stdio server
```

On first run, press `a` in the TUI to configure a provider preset (or `c` for a custom OpenAI/Anthropic-compatible endpoint), paste an API key, review pre-selected models, and press `Ctrl+S` to save to SQLite. For backwards compatibility, setting `GEMINI_API_KEY` or `GOOGLE_API_KEY` seeds a Google provider automatically.

## Key capabilities

- **Bubble Tea provider & model manager (`codjent`):** Embedded preset and model catalog from `models.dev` and OpenRouter (OpenAI Responses/Chat Completions, Anthropic, Google Gemini, OpenRouter, OpenCode, DeepSeek, Groq, xAI, Mistral, Ollama, LM Studio), live `/models` discovery (`Ctrl+F`), manual model entry (`Ctrl+A`), masked write-only keys, static/dynamic custom headers, and cross-process SQLite catalog sync (`PRAGMA data_version`).
- **ACP v2 stdio server (`codjent acp`):** Newline-delimited JSON-RPC 2.0 over stdin/stdout (diagnostics on stderr) supporting `initialize`, `session/new`, `session/set_config_option`, `session/prompt` (text, PNG/JPEG/WebP images, resources), `session/cancel`, `session/list`, `session/resume`, `session/fork`, `session/delete`, and `session/close`.
- **Workspace tools & approvals:** Sandboxed filesystem read/write/edit/list/glob/grep tools, foreground-to-background `execute_command` with live output streaming (`terminal_update` / `terminal_output_chunk`), client-supplied stdio/HTTP MCP servers, workspace skills (`<root>/.agents/skills/*/SKILL.md`), `/compact` summarization, and `session/request_permission` approval gates with inline diffs.

## Subagents, thread lineage, and ACP v2 sessions

Every root session in `codjent acp` wires a session-scoped `tools/subagent` host backed by SQLite thread persistence:

- **Persisted thread lineage:** An additive, idempotent SQLite migration adds `parent_thread_id`, `subagent_profile`, `subagent_description`, and `subagent_branch` (indexed by `idx_threads_parent`) to `threads`. Child threads are omitted by default from top-level listings (`session/list`, `ListThreads`), and `session/delete` (`DeleteThread`) recursively deletes all descendant threads and messages in SQLite.
- **Built-in profiles and dynamic definition:** Codjent defines two built-in Go profiles—`explorer` (read-only `read_file`, `list_files`, `glob_files`, `grep_files`, `AllowNested: false`) and `worker` (filesystem, search, and managed `exec` tools, `AllowNested: true`)—plus capability-attenuated runtime `subagent_define` capped at `worker` tools and 16 dynamic profiles per session. Workspace or home-directory agent files are **not** loaded.
- **Git worktree isolation:** Subagents using `workspace: "worktree"` run inside isolated git worktrees and temporary branches via `subagent.GitWorktreeProvider` (keeping `additionalDirectories` isolated), with per-worktree `exec` managers closed on session shutdown and `subagent_cleanup` gated in `ask` mode.
- **Native ACP v2 subagent sessions:**
  - Child sessions are announced to their immediate parent via `subagent_update` before child updates or tool links, receive prompts via `session_message`, and are linked from `subagent_spawn`/`subagent_send`/`subagent_wait`/`subagent_status`/`subagent_cancel` tool results via `type: "session"` content items (both live and during `session/resume` `replayFrom: "start"`).
  - Clients can cancel an active child session directly with `session/cancel`, while client-initiated `session/prompt`, `session/resume`, `session/fork`, `session/delete`, `session/close`, and `session/set_config_option` targeting a child session are rejected with JSON-RPC `-32602`.
  - Under `ask` mode, mutating tool calls inside child and nested grandchild runs trigger `session/request_permission` on the child's `sessionId` while inheriting parent/ancestor `plan` mode blocks and session-memory rejections.

## Internal layout and configuration

Codjent follows the same one-way dependency rules as the rest of Gojent:

- `codjent/internal/app`: Thread/run lifecycle, approval gate, subagent host (`NewSessionSubagents`), event log, and transcript persistence contract.
- `codjent/internal/store/sqlite`: `app.Store` and provider catalog persistence (using `modernc.org/sqlite`).
- `codjent/internal/provider`: Provider seam, SQLite-backed dynamic catalog, and embedded/cached presets.
- `codjent/internal/acp`: Stdlib-only ACP v2 stdio server calling `app`, never imported by `app` or `tui`.
- `codjent/internal/tui`: Bubble Tea UI, never imported by `app` or `acp`.

Environment variables such as `CODJENT_DATA_DIR`, `CODJENT_WORKSPACE`, `CODJENT_PROVIDER`, `CODJENT_MODEL`, `CODJENT_ACCESS_MODE`, and `CODJENT_MAX_STEPS` configure defaults; see [`codjent/README.md`](https://github.com/ijaidev/gojent/blob/main/codjent/README.md) for the full variable table and ACP command flags.
