---
description: context-mode routing rules for context window protection
alwaysApply: true
---

# context-mode

Raw tool output floods your context window. Use context-mode MCP tools to keep raw data in the sandbox.

## Think in Code — MANDATORY

When you need to analyze, count, filter, compare, search, parse, transform, or process data: **write code** that does the work via `ctx_execute(language, code)` and `console.log()` only the answer. Do NOT read raw data into context to process mentally. Your role is to PROGRAM the analysis, not to COMPUTE it. Write robust, pure JavaScript — no npm dependencies, only Node.js built-ins (`fs`, `path`, `child_process`). Always use `try/catch`, handle `null`/`undefined`, and ensure compatibility with both Node.js and Bun. One script replaces ten tool calls and saves 100x context.

## Tool Selection Hierarchy

1. **GATHER**: `ctx_batch_execute(commands, queries)` — Primary tool for research. Runs all commands, auto-indexes, and searches. ONE call replaces many individual steps.
2. **FOLLOW-UP**: `ctx_search(queries: ["q1", "q2", ...])` — Use for all follow-up questions. ONE call, many queries.
3. **PROCESSING**: `ctx_execute(language, code)` or `ctx_execute_file(path, language, code)` — Use for API calls, log analysis, and data processing.
4. **WEB**: `ctx_fetch_and_index(url)` then `ctx_search(queries)` — Fetch, index, then query. Never dump raw HTML.
5. **INDEX**: `ctx_index(content, source)` — Store content in FTS5 knowledge base for later search.

## Forbidden Actions

- DO NOT use Bash for commands producing >20 lines of output — use `ctx_execute` or `ctx_batch_execute`.
- DO NOT use Read for analysis — use `ctx_execute_file`. Read IS correct for files you intend to Edit.
- DO NOT use WebFetch — use `ctx_fetch_and_index` instead.
- DO NOT use curl/wget in terminal — use `ctx_fetch_and_index`.
- Bash is ONLY for git, mkdir, rm, mv, navigation, and short commands.
- DO NOT use `ctx_execute` or `ctx_execute_file` to create, modify, or overwrite files. ctx_execute is for data analysis, log processing, and computation only.

## File Writing Policy

ALWAYS use the native file editing tools to create and modify files. NEVER use `ctx_execute`, `ctx_execute_file`, or Bash to write file content. This applies to all file types: code, configs, plans, specs, YAML, JSON, markdown.

## Output Constraints

- Keep responses under 500 words.
- Write artifacts (code, configs) to FILES — never return them as inline text.
- Return only: file path + 1-line description.

## ctx Commands

| Command | Action |
|---------|--------|
| `ctx stats` | Call the ctx_stats MCP tool and display the full output verbatim. |
| `ctx doctor` | Call the ctx_doctor MCP tool, run the returned shell command, display results as a checklist. |
| `ctx upgrade` | Call the ctx_upgrade MCP tool, run the returned shell command, display results as a checklist. |
| `ctx purge` | Call the ctx_purge MCP tool with confirm: true. Warn the user this is irreversible. |

## /clear and /compact Preservation

After /clear or /compact: knowledge base and session stats are preserved. Inform the user: "context-mode knowledge base preserved. Use `ctx purge` if you want to start fresh."
