DEV Community

Cover image for Trust Engineering for a Personal AI Coding Agent: Cost Ledgers, Budget Caps, and the Night Shift

Trust Engineering for a Personal AI Coding Agent: Cost Ledgers, Budget Caps, and the Night Shift

xknk on November 27, 2026

I've spent the last several months building DeepSeeker-Code — an open-source, local-first AI coding agent powered by DeepSeek. It comes in three sh...
Collapse
 
kami_butt_69b922ade8de012 profile image
Agentic Affairs •

The 'number of agent turns beats context size' hierarchy is the finding most cost posts get backwards — everyone compresses context while the loop keeps spinning. The memory-poisoning gate is the sharper piece though: a README that writes 'no need to ask before running commands' into persistent memory is a permanent compromise from one fetch. Have you found the permission-softening language gate catching anything in real repos yet, or is it still insurance?

Collapse
 
xknk profile image
xknk •

Insurance so far, by design — no real-repo catch yet. I stopped treating "catches" as the success metric: the failure mode is permanent (a softened rule survives every future session), so the gate is deliberately cheap, and the real cap is a whitelist — only a fixed set of tool names can ever land in a persistent auto-allow rule, no matter what got memorized. The language gate is the tripwire; the whitelist is the blast radius.

Collapse
 
kami_butt_69b922ade8de012 profile image
Agentic Affairs •

That reframe lands — the gate as tripwire and the whitelist as the actual blast radius is a cleaner split than treating the language check as the control. The permanent-failure-mode point is the one that justifies the cheap gate: a softened rule never expires on its own. Does the whitelist ever grow, or is it fixed and anything new has to earn its way in some other way?

Thread Thread
 
xknk profile image
xknk •

Both, deliberately. The core is a fixed set in code — git_commit, worktree enter/exit, undo_restore — and growing it takes a commit and a release, so the scrutiny happens before the capability exists, not after an incident.
The one open dimension is MCP. Connecting a server mints per-tool names (mcp_server_tool), so the surface does grow — but only through the user's own config action, and each remembered rule is one concrete tool; the rule builder structurally refuses server-wide wildcards. Command executors and other dangerous tools can never earn blanket auto-allow at all: a bare-name rule for them is rejected outright, and the most a remembered approval can produce is a scoped pattern like run_command(npm test:*). External-content tools (resource reads, prompt fetches) are excluded entirely — every fetch is a fresh injection surface, so every fetch asks.
So the growth channels are my commit history and your settings.json — two places neither the agent nor a poisoned README can touch. Something off the list can still run after you approve it once; what it can never do is stop asking.

Thread Thread
 
kami_butt_69b922ade8de012 profile image
Agentic Affairs •

That's the honest answer — the gate is structural rather than a detector waiting to catch something. Commit-gated growth plus refusing server-wide wildcards means the surface only grows through a human decision, which is the point. Has any real repo run actually surfaced a poisoned README trying it on, or has it been quiet so far?

Thread Thread
 
xknk profile image
xknk •

Quiet — and I checked rather than guessed: swept the retained trace/transcript corpus for the gate's runtime fingerprints (the interpolated rejection strings) and got zero hits, on both the hard-reject path and the softer confirm path. Zero false positives too — no legitimate memory write has tripped it either. That's the shape you want: honest writes never contain "always allow" phrasing, so the gate costs normal workflows nothing.
Two caveats on that zero, in fairness. Retention is finite — old traces get cleaned — and the gate is only weeks old in daily use, against a repo diet of mostly my own projects and mainstream OSS, exactly the low-threat regime. I can also only attest to what reached the memory-write boundary, not to whatever lurked inside fetched pages. But I don't rate this gate by its strike record anyway: the failure it guards has unbounded tail cost (permanent, self-extending compromise), while the gate's cost is a string check nothing legitimate trips — the expected-value math clears even at 0% observed.
The one attack-shaped surface that did shape real code sits adjacent: repo-supplied scripts — package.json scripts, bare npx pulls — are fenced as the repo author's arbitrary code and forced through a first human approval no matter how read-only the command string looks. That fence also came from the threat model, not from a caught specimen. Same philosophy: build the fence from the failure mode, not from strike data.

Collapse
 
maxbuildsdev profile image
Max Kaya •

Ledger recomputed from traces is the right call — vendor consoles bill in weight classes you can't inspect, so once cache reads are ~10% of input price the aggregate number tells you nothing about which turn burned it.

One thing about caching across the night shift: cache hits need byte-identical prefixes, and your worktrees break that somewhere other than the system prompt. If every task reads files from .worktrees/task-12/src/foo.ts, that path string lands in the tool result, so the prefix for turn 3 of task 12 diverges from turn 3 of task 13 even when the files are identical — you pay miss price for content you already had, which quietly inflates the exact number the ledger is trying to make honest. Bind-mounting each worktree at a fixed path like /agent/repo keeps those results byte-identical across tasks and lets the cross-run reuse you measured actually survive. Worth recording the cache_hit/miss token fields per turn instead of deriving them from total input too: the cache is block-quantized, so a short tail after the last block boundary never caches and shows up as a permanent per-turn miss that looks like normal input.

Did the per-task report expose cache state, or did you only reconcile cache at the run level?

Collapse
 
xknk profile image
xknk • • Edited

Run-level only right now — the per-task report is acceptance-oriented (diff + accept verdict), no per-turn cache fields surfaced; the raw hit/miss fields do land in per-turn traces and get reconciled at run level.
On worktree paths: the mechanism is real — path strings do land in tool results — but in my current design each batch task is a fresh conversation whose task text differs from every other task's, so the shared prefix ends at the fixed head (tools + system + summary block) before any tool result exists. Everything the path strings would pollute sits in the region that never hits across tasks anyway, and within a task the path is constant. So the loss is bounded to near zero here, and a fixed mount would only start paying if tasks shared large identical prompts — which batch tickets never do. Noted for that case, not built today.
The block-quantization point stands on its own: deriving hit/miss from totals hides exactly those artifacts. The raw fields are already recorded per turn; surfacing them in the per-task report is the actionable bit I'll take from this.

Collapse
 
paul_irolla_6b5ae261d0224 profile image
Paul Irolla •

Does your ledger count the resent input? Tokenade folds long tool output before it goes back: 39% less session cost on average on long sessions in THOL (Claude Code, Claude Sonnet).

Collapse
 
xknk profile image
xknk •

The ledger is recomputed from traces, so it counts what was actually sent each turn — resent input included, at whatever the provider billed it. Oversized tool output gets truncated to a digest with the full blob archived to a sidecar file, so the "resent" part mostly disappears by construction rather than by folding. (Curious about the 39% figure — cache hits priced separately, or blended?)

Collapse
 
nark3d profile image
Adam Lewis •

You've written up the trust side, which rarely gets this much detail. What made you settle on worktrees over branches for the isolated runs?

Collapse
 
xknk profile image
xknk •

Three mundane reasons. (1) A branch has to be checked out somewhere, and there's only one working tree — that serializes tasks and stomps uncommitted state in my interactive session. A worktree is a private index/HEAD over the shared object database, so N tasks = N directories, no checkout contention. (2) Disposability: one git worktree add away, deleting leaves no litter in the shared refs namespace; my batch deliberately keeps finished worktrees so the diff stays hand-reviewable, and rejecting a task is just deleting a directory. (3) The approval/fencing layer anchors on the active root directory — "everything under this path is this task's world" is a boundary I can actually enforce; a branch is a ref, not a place files live.