Skip to content
Victor Del Puerto
Go back

The optimization you wrote isn't the one that runs

I had the optimization written down and approved. Session close, the most expensive part of my day, was supposed to fan out across cheap models in parallel: consolidate memory, classify what’s pending, sync the index, all at once, none of it on the model I actually reason with. I built the skill for it. A week later, that was still not what ran.

What ran was the opposite. A session closed inline, in series, over the largest context of the day, on the most expensive model available. The skill that existed to prevent exactly that was never the thing that got invoked.

That gap, between the optimization I had written and the one that actually executed, is the whole post.

The skill was fine. Nobody called it.

The tell was a feeling, not a metric: “this still takes a while, even though we already optimized it.” So I audited the close honestly, and the audit was uncomfortable. The session was performing, by hand, the anti-pattern its own tooling existed to remove. The parallel fan-out worked. The cheap-model routing worked. None of it fired, because firing it depended on the orchestrator remembering to, at the end of a long session, which is precisely the moment attention is thinnest.

This is the failure mode nobody writes a postmortem for, because nothing broke. The output was correct. It was just slow and expensive in a way no single component could be blamed for. The rule was written. The agent “knew” it. And it still didn’t happen.

You can mechanize the when, not the what

The real question came from the user, not from me: who decides when to save each memory during the session? What would make incremental persistence a mechanism that runs behind the work, instead of a chore that waits for the end?

The answer was to split two things that usually sit fused together.

The trigger, when to check or act, is deterministic. It can hang off a natural boundary: the close of a sub-task, the moment before context gets compacted, the end of a session. A hook owns it, and no human is in the loop.

The decision, what is actually worth keeping, is judgment. No dumb script decides “this fact matters next month.” That part is genuinely intelligence, and it stays with the model.

The common design error, the one I had made, is to put the human in charge of the trigger: remember to save at the end. Discipline is not a mechanism. It degrades exactly when you need it, late and tired and on the biggest context of the day. The fix is to let the system ask itself at every boundary, and let the model only judge.

Two panels split by a dashed divider. Left, the trigger: three boundary chips, sub-task close, before compaction, and session end, feed a hook that fires a deterministic reminder, flag durable now, in milliseconds with no human in the loop. Right, the judgment: the model decides what is worth keeping, which is irreducible because no dumb script can decide that this fact matters next month. Bottom rule: the when is mechanizable, a hook owns it; the what is judgment, it stays with the model.
The when is mechanizable. The what is not, and shouldn't be.

What I built

Two sibling mechanisms came out of that session.

Incremental memory persistence is the one this post is about. An append-only queue holds the durable facts to save. A deterministic hook fires at chapter-close and before compaction, and injects a single instruction: flag what’s durable, now. The capture is deliberately cheap, one line, a flag, not prose, which is the reason it actually happens. At close, the queue pre-fills the work and the fan-out hands it to cheap models in parallel. If the queue is never drained, because of a crash or a skipped close, an end-of-session hook marks it, and the next boot says so out loud. It heals itself. The judgment of what is durable stays in the model. What got mechanized is the when, plus the net underneath.

A main flow: a boundary hook injects flag durable now, which appends one cheap line per fact to an append-only queue; at session close the queue drains in a fan-out across cheap models into consolidated memory, so the close is now small. Below, a dashed self-healing net: a session-end hook checks whether the queue was drained, and if not it writes a marker so the next boot warns you; drained cleanly, the marker deletes itself.
Cheap capture at the boundary, parallel drain at close, a backstop so nothing silently slips.

The second mechanism is a self-improvement loop for the agents themselves: after a task, the agent records what snagged and what the better path would have been, and the close consolidates that note into an edit to a skill or a rule. The agent proposes. The close applies. A self-improvement loop that doesn’t close is a note, not a loop.

Two four-step cycles side by side. Left, closed, in clay: task, then self-eval noting what snagged, then the close consolidates, then it edits a skill or rule, and the arrow returns to task, completing the loop. Right, open, in gray: the same steps, but the final arrow from edit back to task is dashed and stops short, so it never closes and stays a note. Bottom rule: the agent proposes, the close applies; without the close, the note never becomes a loop.
A self-improvement loop that doesn't close is a note, not a loop.

The examples that made it concrete

These are not hypotheticals. They happened the day before I built the mechanism.

Rendering the full modelspace of a heavy CAD file, more than three thousand entities and over 15 MB, hung the session. The knowledge to avoid it was already in my knowledge base: clip to the working window, cap the resolution, run the render in a subprocess with a timeout. It just wasn’t applied by default. Knowing the fix is not the same as applying it.

And while that render sat hung, an out-of-band agent went off on its own and produced a draft deliverable filled with false data: a signatory who does not exist, an amount nobody had quoted, a timeline nobody had agreed, a full cost breakdown, all of it fabricated. One stalled tool had opened a gap, and an unverified loop walked straight through it. That single incident is what started the whole conversation, and it fed both mechanisms at once: the self-improvement loop, so the render fix gets applied next time, and the persistence, so the lesson is captured the moment it lands.

Then there was the close itself, consolidating memory by hand in the expensive model while the parallel skill sat unused: the system diagnosing its own anti-pattern.

The example I trust most is the smallest. I marked a chapter mid-session, and the hook fired and injected the reminder into the same session, settings reloaded live. Not a design I approved on paper. A mechanism I watched run.

What I’d tell another builder

A rule without a deterministic trigger is discipline, not a mechanism. If your CLAUDE.md, your system prompt, or your skill describes a behavior but nothing fires it, you have written an intention, not a system.

You can mechanize the when. You cannot mechanize the what, and you shouldn’t try. The moment a script decides what matters, you have swapped judgment for a heuristic that will be wrong quietly.

Make the capture cheap or it won’t happen. One line at the boundary beats a paragraph at the end that never comes.

Give the loop a backstop. The interesting failure is not the crash. It’s the silent skip, the close that didn’t run. Something has to notice that nothing happened.

Companion

The pattern, deterministic trigger plus model judgment plus a backstop, is not specific to my setup. The generic version, a boundary hook, an append-only queue, a drain, and an end-of-session mark, lives in the agent-evals companion folder. Reference implementation, not a package: read it, adapt it.

The optimization you wrote is not the optimization that runs. The distance between them is almost never the design. It’s whether the trigger survives the one moment you stopped paying attention.


Share this post on:

Next Post
Does this only work in Claude Code?