# Constant — Portable Agent Cognition (Complete Technical Reference for Coding Agents) URL: https://constant.sh Repo: https://github.com/kennykankush/constant License: MIT Version: 0.12.0 ## Summary Constant is local-first **portable agent cognition** — flight recorder, trajectory rewind, anti-compaction projection, and `Model × Harness` verification for coding agents. It seals a live conversation (turns, redacted tool calls, failing tool outputs, receipt, source SHA-256, and runtime/git subconscious stamp) into one unedited **Dry Master** record under `~/.constant/snapshots/` and lets you rewind (`constant rewind`), triage past compaction cliffs (`--render triage`), and verify across independent coding agent harnesses with objective `--verify-cmd` scoring: - Built-in native codecs: Codex (`0.167.x`), Claude Code (`2.1.x`), OpenCode (`2.x` via `--standalone` and `1.18.x`), Antigravity (`agy` `1.5.x`: native transcript read, `stream-json` headless `ask`, `--mode plan --sandbox` readonly, `agy acp`), plus legacy Gemini CLI (`0.50.x`, read-only source). - ACP Execution Bus: any Agent Client Protocol (JSON-RPC 2.0 over stdio) agent configured in `~/.constant/runtimes.json` or `$CONSTANT_RUNTIMES`. - External plugins: any executable named `constant-adapter-` in `~/.constant/adapters/` or on `$PATH` implementing Constant's 4-verb JSON-over-stdio contract (reference Ollama/vLLM/DeepSeek/OpenAI-compatible adapter in `examples/constant-adapter-openai-compat.py`). ## When an Agent Should Call Constant 1. Stuck after 2+ failed attempts, high-risk refactor, or pre-PR audit -> call `constant_ask` with multiple `harness:model` targets (`targets: ["claude:sonnet", "claude:opus", "codex:o3", "antigravity"]`, `with_tools: true`, `render: "triage"`, `isolate: "worktree"`, `verify_cmd: "cargo test"`) to get independent full-context diagnoses, isolated patch diffs, objective verification results, and deterministic divergence. 2. Long thread hitting the compaction cliff / context rot -> use `render: "triage"` (`--render triage`) on `carry`, `ask`, `replay`, or `rewind` to pin Turn 1 + constraint turns verbatim, rescue earlier failing tool outputs, and collapse bulky passing tool outputs (`> 400` chars) into exact `constant recall` pointers without thinning the Dry Master. 3. Agent hallucinated or went off the rails at Turn K -> run `constant rewind --turn K [--to ] [--question ...]` (or pass `until_turn: K` to `constant_carry` / `constant_ask`) to fork cleanly from Turn K while sealing the full Dry Master intact. 4. Context compacted and earlier constraints or error outputs disappeared -> call `constant_search` (with `tools: true` for stack traces / exit codes) followed by `constant_recall` to retrieve exact verbatim turns and redacted tool traffic. 5. Passive capture before compaction or session end -> run `constant record --session --quiet --json` (idempotent by `source_sha256`, writes only under `~/.constant/`). 6. Hand off a thread for native resumption in another agent CLI -> call `constant_carry` and report the receipt and exact native resume command. 7. Evaluate models × harnesses on real history -> run `constant replay --corpus --targets claude:sonnet,claude:opus,codex:o3 --pin-commit --verify-cmd "cargo test" --out report.json`. 8. Compliance expungement of Constant-owned records -> run `constant purge --json` (strictly confined to `~/.constant/snapshots/` and `~/.constant/trail.jsonl`). ## Installation macOS (Homebrew): ```bash brew tap kennykankush/constant https://github.com/kennykankush/constant brew install kennykankush/constant/constant constant doctor --json ``` Linux x86_64 / macOS (standalone installer): ```bash curl -fsSL https://raw.githubusercontent.com/kennykankush/constant/main/scripts/install.sh | sh ``` Source (Rust 1.94+): ```bash cargo install --git https://github.com/kennykankush/constant --locked ``` ## Native Stdio MCP Server (`constant mcp`) Start command: `constant mcp` (JSON-RPC 2.0 newline-delimited stdio; zero external runtime dependencies; diagnostics strictly on stderr). Host configuration: - Claude Code: `claude mcp add constant -- constant mcp` - Codex: `codex mcp add constant -- constant mcp` - Generic `.mcp.json` / `.cursor/mcp.json`: ```json { "mcpServers": { "constant": { "command": "constant", "args": ["mcp"] } } } ``` - OpenCode (`opencode.json`): ```json { "mcp": { "constant": { "type": "local", "command": ["constant", "mcp"] } } } ``` ### MCP Tool Inventory (7 In-Process Tools) 1. `constant_runtimes` - Args: `{ "probe_plugins"?: boolean }` (default `true`) - Returns: `{ "config": string | null, "runtimes": [{ "name", "kind": "builtin"|"acp"|"plugin", "version", "capabilities", "detail" }] }` 2. `constant_sessions` - Args: `{ "from"?: "codex"|"claude"|"opencode"|"antigravity"|"gemini", "cwd"?: string, "all"?: boolean, "query"?: string, "titles"?: boolean, "limit"?: integer }` - Returns: `{ "total", "shown", "sessions": [{ "runtime", "id", "path", "cwd", "mtime", "has_conversation", "title", "handle", "name" }] }` 3. `constant_record` - Args: `{ "session"?: string, "from"?: string, "force"?: boolean }` (provide `session` OR `from`) - Returns: `{ "handle", "name", "conversation", "chapter", "from", "snapshot", "source_sha256", "unchanged", "receipt", "subconscious" }` 4. `constant_search` - Args: `{ "handle": string, "query": string, "chapter"?: integer, "tools"?: boolean, "role"?: "user"|"assistant", "limit"?: integer }` - Returns: `{ "handle", "name", "chapter", "snapshot", "query", "include_tools", "total_hits", "shown", "hits": [{ "kind": "turn"|"tool_call"|"tool_result", "turn", "tool_seq", "label", "is_error", "snippet" }] }` 5. `constant_recall` - Args: `{ "handle": string, "chapter"?: integer, "range"?: string, "tools"?: boolean }` (`range` is `"14"` or `"12-18"`) - Returns: `{ "handle", "name", "chapter", "snapshot", "total_turns", "range": [lo, hi], "meta", "turns": [{ "turn", "role", "text", "tools"?: [...] }] }` 6. `constant_carry` - Args: `{ "to": string, "session"?: string, "from"?: string, "new"?: boolean, "with_tools"?: boolean, "render"?: "full"|"paged"|"triage", "tail_budget"?: integer, "until_turn"?: integer }` - Returns: `{ "id", "from", "to", "adapter_kind", "render", "until_turn", "cwd", "path", "resume", "trail", "handle", "name", "receipt", "snapshot" }` 7. `constant_ask` - Args: `{ "targets"?: string[], "to"?: string, "session"?: string, "from"?: string, "question": string, "with_tools"?: boolean, "render"?: "full"|"paged"|"triage", "tail_budget"?: integer, "until_turn"?: integer, "isolate"?: "auto"|"worktree"|"readonly"|"none", "verify_cmd"?: string, "verify_timeout_secs"?: integer, "timeout_secs"?: integer }` - Targets accept bare runtime names or `:` pairs (e.g. `"claude:sonnet"`, `"codex:o3"`, `"agy:gemini-3-pro"`). - Single-target return: `{ "ok", "to", "adapter_kind", "from", "source_session", "session", "path", "cwd", "resume", "handle", "name", "trail", "snapshot", "receipt", "fork", "question_chars", "answer", "elapsed_ms", "model"?, "isolation"?, "files_touched"?, "patch_diff"?, "verify"? }` - Multi-target return: `{ "ok", "all_ok", "multi": true, "from", "source_session", "handle", "name", "trail", "snapshot", "cwd", "isolation", "question_chars", "elapsed_ms", "targets": [...], "results": [...], "divergence": { "targets_succeeded", "targets_failed", "answers_identical", "shared_references", "unique_references_by_target", "files_touched_by_target", "patches_produced_by", "patches_differ", "verify_passed_by", "verify_failed_by", "verify_differs", "summary" } }` ## CLI Command Reference - `constant ask --to RT[:MODEL][,RT[:MODEL]...] (--from RT | --session PATH_OR_ID) (--question TEXT | TEXT... | --question -) [--with-tools] [--render full|paged|triage] [--tail CHARS] [--until-turn K] [--isolate auto|worktree|readonly|none] [--verify-cmd CMD] [--verify-timeout SECS] [--timeout SECS] [--json] [--dry-run]` - `constant rewind --turn K [--to RT[:MODEL][,...]] [--question TEXT|-] [--with-tools] [--render full|paged|triage] [--tail CHARS] [--isolate MODE] [--pin-commit] [--verify-cmd CMD] [--verify-timeout SECS] [--timeout SECS] [--json] [--dry-run]` - `constant mcp` - `constant replay --corpus HANDLE|PATH|DIR[,...] --targets RT[:MODEL][,...] [--turn auto|K] [--question TEXT|-] [--with-tools] [--render full|paged|triage] [--tail CHARS] [--isolate MODE] [--pin-commit] [--verify-cmd CMD] [--verify-timeout SECS] [--timeout SECS] [--out FILE] [--json] [--dry-run]` - `constant runtimes [--json]` (`constant harnesses` alias) - `constant record (--from RT | --session PATH_OR_ID) [--force] [--quiet] [--json]` - `constant search HANDLE QUERY [--ch N | chNN] [--tools] [--role user|assistant] [--limit N] [--json]` - `constant recall HANDLE [chNN] [TURN | A-B] [--tools] [--json]` - `constant carry --to codex|claude|opencode| [--from RT | --session PATH_OR_ID] [--new] [--with-tools] [--render full|paged|triage] [--tail CHARS] [--until-turn K] [--dry-run] [--debug] [--json]` - `constant purge (HANDLE | --match-sha PREFIX | --older-than DAYS) [--dry-run] [--json]` - `constant sessions [QUERY] [--from RT] [--all] [--titles] [--plain] [--json]` - `constant integration [status|install|uninstall|mcp-config] [claude|codex|opencode|cursor|all] [--json]` - `constant snapshots [--all] [--full]` - `constant audit [HANDLE] [--all] [--json] [--tail CHARS]` - `constant restore [--to RT] [--json]` - `constant pack HANDLE [--out FILE]` / `constant unpack FILE` - `constant route [--all] [--json] [--session PATH_OR_ID]` - `constant trail [--all] [--plain] [--full] [--events]` - `constant ps [--deep] [--json]` - `constant doctor [--json]` / `constant status [--all]` - `constant export (--from RT | --session PATH_OR_ID) [--out FILE]` - `constant rename [--of HANDLE] NEW NAME...` - `constant host [codex|claude|opencode]` / `constant resume [QUERY]` (optional interactive terminal modes; agents should not run interactive TTY commands) ## Dry Master Record Schema (`constant/v2`) Snapshots live at `~/.constant/snapshots//chNN-from-.json` (owner-only `0o700`, atomic temp + fsync + rename). Every Dry Master stores full redacted turns plus redacted tool calls/outputs (up to 64,000 chars per output) and embeds under `metadata.extra.constant`: ```json { "schema": "constant/v2", "source_sha256": "<64-hex sha256 of source file>", "receipt": { "turns": 14, "tools": 28, "dropped_tools": 0, "dropped_reasoning": 9, "dropped_scaffold": 4, "redactions": 2, "indexed": 0 }, "subconscious": { "runtime": "codex", "model": "gpt-5.4-codex", "platform_version": "0.167.0", "git_branch": "main", "git_commit": "44aaed7..." } } ``` ## ACP Runtime Configuration (`~/.constant/runtimes.json`) Override path via `CONSTANT_RUNTIMES`. Built-in names (`codex`, `claude`, `opencode`, `antigravity`, `gemini`) can never be shadowed. ```json { "runtimes": [ { "name": "opencode-acp", "kind": "acp", "command": "opencode", "args": ["acp"], "env": {} } ] } ``` Headless ACP requests run `initialize` -> `session/new` -> `session/prompt`, stream `session/update` chunks, and automatically reply `outcome: "cancelled"` to any `session/request_permission` prompt. ## External Plugin Protocol (`constant-adapter-`) Place an executable named `constant-adapter-` in `~/.constant/adapters/` or on `$PATH`: 1. `manifest` -> stdout JSON `{ "name", "version"?, "capabilities": { "read", "write_native", "ask_direct" } }` 2. `read ` -> stdout `UniversalSession` JSON (`transession/v1`) 3. `write` -> stdin `{ "session", "title"? }` -> stdout `{ "id", "path"?, "resume"? }` 4. `ask` -> stdin `{ "session", "question", "cwd", "with_tools", "readonly" }` -> stdout `{ "answer", "model"?, "session"?, "resume"? }` ## Isolation & Trust Invariants 1. Originals are seeds, never targets (source sessions are never modified). 2. All snapshot, projection, and ledger rewrites are atomic (temp sibling + fsync + rename); ledger mutations hold an exclusive `flock` (`~/.constant/.trail.lock`). 3. Recursive secret redaction applies to every string including nested tool payloads; reasoning traces never cross runtimes. 4. Lossiness is always declared in structured receipts; never silent. 5. Multi-target `ask` and `replay` isolate workspaces via RAII-guarded ephemeral detached git worktrees (mirroring uncommitted tracked changes or pinning a recorded `git_commit` and deleting worktrees unconditionally on `Drop`) or verified read-only flags (`codex exec -s read-only`, `claude --permission-mode plan`). 6. OpenCode is written strictly via `opencode [session] import` (never direct SQLite writes); `~/.gemini` is strictly read-only. 7. `constant purge` deletes strictly inside canonicalized `~/.constant/snapshots/` and never touches native runtime stores.