Skip to content

Code Mode

Since v0.16.0; modules since v0.17.0.

ctx-optimize code runs a small JavaScript-subset program in one process. The six read-only verbs are its operations: ctx.query, ctx.card, ctx.change_plan, ctx.affected, ctx.path, ctx.hubs. Each call resolves to the verb’s parsed JSON, so the program filters and only the answer reaches the agent.

No model, no network, deterministic. It cannot gather, sync or write the store: none of those is an operation (tested).

Measured on this repo, query, then card on the top 5, then change-plan, best of 5, on a loaded Mac:

time bytes back
seven processes 63 ms 22,538
one code run 29 ms 825

2.2x faster and 27x less output (about 5,600 tokens to 200). The win is the output: the program returns only what you will cite. Cost: +1.45 MB of binary.

Four modules ship inside the binary. Import one instead of composing raw operations.

Terminal window
ctx-optimize code modules
ctx:edit Before editing: one compact plan per symbol (signature, callers, blast radius, tests to run).
ctx:find Find symbols by words and inspect them in one step: query, then a card for each hit.
ctx:impact Blast radius across several symbols: which files break, merged and deduplicated.
ctx:orient Orient in an unfamiliar repo: the hubs worth reading first, and how two symbols connect.
ctx:safe Helpers shared by the ctx modules: run one lookup per name without letting a single miss reject the whole fan-out.
module exports
ctx:edit planEdit(symbol), planEdits(symbols): signature, callers, impacted files, tests, co-changed files; a misspelled name is reported under missing
ctx:find findCards(terms, limit)
ctx:impact blastRadius(symbols, depth)
ctx:orient orient(top), connect(a, b)

ctx-optimize code describe ctx:edit shows every export with its doc and source.

A four-file Go repo gathered into a temporary store. The program:

import { planEdit } from "ctx:edit"
const p = await planEdit(input.symbol);
({symbol: p.symbol, signature: p.signature, file: p.file, callers: p.callers, impactedFiles: p.impactedFiles})

Check it first (nothing runs), then run it with the parameter in --input:

Terminal window
ctx-optimize code check prog.js
ctx-optimize code run prog.js --input '{"symbol":"Store.Merge"}' --json
ok: prog.js (1 operation(s), 2 import(s))
{
"ok": true,
"value": {
"callers": ["main.go::Sync"],
"file": "store.go",
"impactedFiles": ["main.go", "store.go"],
"signature": "func (s *Store) Merge(other *Store)",
"symbol": "Store.Merge"
},
"ms": 1
}

The program’s last expression is its result. - reads the program from stdin.

command does
code run <file|-> [--input JSON] [--json] run a program; --input becomes the global input
code check <file|-> validate syntax, operation names (with did-you-mean) and literal argument types; runs nothing
code modules list the shipped modules
code describe <module|operation> exports, contract and source
code operations list the operations
code docs language and API reference for writing programs

Exit codes: 0 ok, 1 ran and failed, 2 nothing ran (diagnostics or bad --input). --json prints one object, {ok, value, error?, diagnostics?, ms}.

  • Read-only: add, sync, search and boundaries are not operations; run them normally.
  • Names must be exact labels. A near miss throws and names the nearest match instead of answering for it; pass "--fuzzy" in args to accept one. In a fan-out, catch per name (ctx:safe does this) so one miss does not reject the rest.
  • A mistyped flag throws.
  • callers and blastRadius are floors for methods: unattributed callers are not counted.
  • Operation names have no hyphen: change_plan. check flags change-plan.
  • For one question, plain query or card is simpler. Use Code Mode for three or more dependent calls, or a fan-out over many symbols.
  • The interpreter bounds strings (64 MiB) and arrays (10M elements); an import cannot read outside the module root (fixed in v0.16.1).
  • The 27x figure is bytes on one repo and one query shape, not whole-session cost.

All code verbs in the CLI reference