Build a Custom Agent Harness¶
| Category | AI Solutions Engineering |
| Status | ✅ Completed |
| Repo | nextwork-build-a-custom-agent-harness |
| Original NextWork guide | nextwork.ai |
Learning project
Practice work following a NextWork guide, separate from my production projects.
📚 NextWork learning project. This is a practice project I built while working through NextWork's Build a Custom Agent Harness guide. It is part of my Learning portfolio and is fully separate from my production projects.
Category: Learning › AI Solutions Engineering
Status: ✅ Completed
Overview¶
An agent is a loop: the model sees the conversation and the tool definitions, asks for tool calls, the harness runs them and feeds the results back, and this repeats until the model answers without tools. The harness, not the model, decides what's safe. Read-only tools run freely. Anything that changes the workspace (write, edit, delete, run a command) waits for a y/N approval, can't escape the workspace folder, and can't touch .git.
Summary¶
The full TypeScript harness is built: contracts, an OpenAI-compatible model adapter, the agent loop, read-only workspace tools, approval-gated write/edit/delete/command tools, a terminal UI, and the persistent session store (secret mission). 6 deterministic tests pass, and a live demo runs against a real model (local Ollama through the same OpenAI-compatible adapter the guide points at OpenRouter).
The checks behind this are in docs/verification.txt.
Skills & tools¶
TypeScript, Agent loop, Tool calling, Human-in-the-loop approvals, OpenAI-compatible APIs, Ollama
What I built¶
src/types.ts: contracts.src/model.ts: OpenAI-compatible adapter plus a scripted test model.src/agent.ts: the loop with approval gating and error containment.src/tools.ts: list/read/search (read-only); write/edit (exactly one match)/delete/run_command (approval-gated, no shell).src/session.ts: the persistent session store (secret mission).src/cli.ts: the terminal UI.tests/harness.test.ts: 6 tests.scripts/live-demo.ts: fixes a bug with a real local model.
How to run¶
# Node >= 23.6 runs .ts directly (type stripping); no build step or dependencies
npm test
ollama pull qwen2.5:1.5b && npm run demo
npm start -- --session mywork # interactive
MODEL_BASE_URL=https://openrouter.ai/api/v1 OPENROUTER_API_KEY=... MODEL=<model> npm start
Verification¶
I ran the verification command on 2026-12-02. The full output is in docs/verification.txt.
node --test tests/*.test.ts && node scripts/live-demo.ts
Notes¶
Model/provider substitution: the guide points the adapter at OpenRouter. The adapter is OpenAI-compatible, so the verification points it at a local Ollama model; set MODEL_BASE_URL=https://openrouter.ai/api/v1 and OPENROUTER_API_KEY to match the guide exactly. The terminal UI is my own minimal readline UI rather than the guide's starter TUI.
Original project: https://nextwork.ai/projects/fbd360ae-3944-42e5-8cc2-2a8a8d6a55b8 · Part of my NextWork Learning portfolio (hub).
Verification output¶
From docs/verification.txt:
$ node --test tests/*.test.ts && node scripts/live-demo.ts
✔ read-only tools run without approval and results reach the model (6.969367ms)
✔ consequential tools are gated: denial leaves the file untouched (2.157647ms)
✔ approved edit requires exactly one match (1.447081ms)
✔ workspace boundary and protected paths (1.051904ms)
✔ tool errors are returned to the model instead of crashing (1.075792ms)
✔ sessions persist across runs (2.111307ms)
ℹ tests 6
ℹ suites 0
ℹ pass 6
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 132.650069
tool read_file({"path":"app.py"}) -> def add(a, b): | return a - b # BUG |
tool edit_file({"path":"app.py","old_text":"return a - b","new_text":"return a + b"}) -> edited app.py
assistant: The file app.py has been edited successfully. The 'add' function now correctly adds instead of subtracting.
FINAL: The file app.py has been edited successfully. The 'add' function now correctly adds instead of subtracting.
approval requests: [ 'edit_file' ]
app.py now:
def add(a, b):
return a + b # BUG
[exit code 0]