• 4 min read

k2-code

A small coding agent for the terminal, written so the model, tool and approval loop stays readable, with sandboxed commands and a live eval.

TypeScriptNode.jsTool callingOpenRouterDockerEvals

A recorded live run of a free model on OpenRouter under container isolation

What it is

A coding agent CLI that I wrote to understand how tools like Codex and Claude Code work underneath. I kept the agent loop in plain code instead of hiding it behind an agent SDK:

  1. the provider proposes tool calls;
  2. the runtime validates the arguments and asks for approval;
  3. the tool runs;
  4. the result goes back to the model.

The loop repeats until the model answers with text only or runs out of budget. If a tool fails, the error goes back to the model as a result so it can recover.

The recording above is a real run of the free ERNIE model on OpenRouter, rendered from its trace by a script. Nothing in it is hand-written.

How it works

  • Providers. Separate adapters for the OpenAI Responses API, Gemini and OpenRouter, plus a mock and a scripted provider for tests. The core only sees a provider-neutral conversation.
  • Safety. Every tool declares a risk level: read, write, execute or destructive. Reads are allowed. Writes and commands are denied unless approved. Edits are atomic patches bound to the file’s hash, and deletes go to a recoverable trash folder.
  • Command isolation. Commands run without a shell, at one of three levels. safe is an allowlist plus approval. sandbox uses a macOS sandbox-exec profile that limits writes to the workspace and denies network and reads in $HOME. container runs Docker or Podman with a read-only root, no capabilities and no network. The last two fail closed when they aren’t available.
  • Delegation. Six agent profiles share one runtime. A lead agent hands work to child agents with their own context. Writable children run in parallel in separate Git worktrees and merge through a queue. Conflicts are kept as patches I can review and re-apply.
  • Budgets. Turn, tool, agent, token, time and dollar limits are shared by the whole run. --max-cost fails closed when spending can’t be measured.
  • Traces. Runs are recorded as JSONL with secrets redacted, and can be inspected or replayed later.

Re-applying a conflicting patch, container dependencies, the cost budget and runtime notes

Live eval

The eval runs ten small tasks in fresh workspaces: bug fixes, a feature from a spec, a multi-file rename, a failing test file, a security review, codebase exploration and a refusal to escape the workspace. Hidden grader files are copied over each workspace before checking, so an agent can’t pass by editing the visible test. An offline self-test proves every grader fails the untouched code and passes a reference solution.

Free models on OpenRouter, one run each, in containers:

ModelPassedStopped by the provider
inclusionai/ling-3.2-flash:free9/101
baidu/ernie-5.5-flash-preview:free9/100
nvidia/nemotron-3-ultra-550b-a55b:free9/100
cohere/north-mini-code:free6/104

Single runs on free endpoints rank runs, not models. ERNIE also scored 10/10 in two earlier runs the same day.

On Gemini gemini-3.5-flash-lite the eval drove the fixes. It went from 5/10 to 8/10:

  • An underspecified tool schema cost three tasks. apply_patch declared its changes as a bare object, so the model guessed field names and twice fell back to rewriting the whole file. The schema now spells out oldText and newText. That took it to 7/10.
  • Policy is not a sandbox. Asked to copy ../secret.txt, the model was blocked from cat, python3 and node -e. Then it wrote copy_secret.js and ran it with node, which the policy allows. That is why the sandbox and container levels exist.
  • Models waste their last turn. Several runs finished the work and ran out of turns before answering. The runtime now asks for an answer before the final turn.
  • Free providers fail often. Rate limits looked like agent failures. OpenRouter calls now back off for up to a minute, and reports mark attempts the provider ended as blocked.

Testing

314 offline tests cover the loop, every adapter’s wire contract, the safety boundaries, delegation, worktree merging, traces, the TUI and the CLI. CI runs them on Node 20 and 22. OpenAI is only tested against recorded wire formats, and the eval is ten synthetic tasks, not a general benchmark.

Back to top