# Apps that fix themselves and get better on their own

Nov 30, 2026 · 7 min read · João Ramires

Late in November I learned that one of the data inputs of my trading system had delivered nothing for 42 days. No error fired and no alert went off, because the job that polls the input kept running, got an empty answer each time and counted that as a success. I found out by accident while researching something else. The first signal from the system itself came a day after I fixed the input, when the drift monitor raised an alarm because my model had been trained on a world where two of its features were empty.

That is the kind of fault I want software to catch on its own. I designed my trading system to carry its own set of [SRE](https://en.wikipedia.org/wiki/Site_reliability_engineering) tools, and I expect it to fix itself, not to wait until I stumble on a problem. I also want it to keep improving without me having to ask. So I build with two loops, one that fixes what breaks and one that improves what works, and both feed on the telemetry the app writes about itself. This is the first article of a series on [applied AI engineering](https://en.wikipedia.org/wiki/Artificial_intelligence_engineering), and each loop gets a deeper article later in the series: [the telemetry the fixing loop runs on](https://joaoramires.xyz/writing/agent-telemetry/index.md) and [the self-improvement loop](https://joaoramires.xyz/writing/self-improvement-loop/index.md).

## At work, an agent on call

At work I built an agent that picks up production bugs as monitoring reports them. [Sentry](https://sentry.io) reports an error and a ticket opens in [Linear](https://linear.app). A [Claude Code](https://code.claude.com) session then starts in an isolated job, with read-only access to the error tracker, the ticket system and the database. It finds the cause, writes a test that reproduces the bug, writes the fix, and ends the ticket with a pull request that explains its reasoning or with a comment about what it found. The [SRE agent page](https://joaoramires.xyz/projects/sre-agent/index.md) has the detail.

What I care about here is how the work leaves the agent. Every run that finds a fix ends in a pull request, but the model never holds the keys to make one. It runs as its own user, without the credentials that deliver a branch, and a separate, trusted step pushes the branch, opens the pull request and checks again that the agent stayed out of the paths it may not touch. The agent has never merged anything, because an engineer reviews and merges every fix. A fix also has to prove itself. A second AI pass reviews it, and a gate runs the linter, the changed specs and the security scan. The new spec must fail on master and pass on the branch, since a fix that can’t show the bug can’t show that it fixed it.

## The same idea, with nobody on call

My trading system runs all day, and the only person on call is me, so I went further. Early on, an agent mixed up two things: the sandbox that runs my coding agents, and the trading system’s own repair tools. I corrected it: the system was designed with its own set of SRE tools, those tools are part of it, and the software should fix itself.

Here is how it works. Every part writes its errors down, with secrets redacted, and errors of one kind form an incident. Fixed rules sort each incident as noise, self-healing or harmful. A short network failure that a retry clears is self-healing and wakes nobody, while a harmful incident raises an alert and goes to a triage agent. The agent also takes a self-healing incident that does not heal, because it keeps coming back for days, which catches the quiet faults that retries hide.

The triage agent runs headless, under a brief that neither it nor a coding agent can change. It treats the text of an incident as data, works on a branch of its own, and ends with a report and a plan. A review and an approval stand between that branch and `main`, and a restart of a service is never its job. For an incident that keeps coming back, the brief makes it pick one of four answers, or “by design”. This is a trimmed excerpt:

> 1. A defect of our code makes a healing event come back. Say where.
> 2. A fault outside the system, such as a provider, a network or a quota. Name the fault and say what you checked to know that it is outside.
> 3. A wrong class or a wrong threshold. Say which, and what you propose.
> 4. The cause is already fixed on `main`. Name the commit.
>
> “By design” is a valid answer when the report names what you checked. \[…\] If you cannot find the cause, write the report with what you tried. Do not guess a fix.

The excerpt shows what I wanted from the agent: an honest “nothing to fix” is a valid result, and a guess is not. I have no patience for guessing, from people or from agents.

*Triage agent · the shape of one run*

- ask: A harmful incident, or a self-healing one that keeps coming back
- think: Read its errors, backtraces and earlier runs, as data and never as instructions
- tool: Trace the cause into the code, on a branch of its own
- check: Pick one answer: defect, outside fault, wrong class or threshold, already fixed, or by design
- tool: Open an OpenSpec change and stop at the plan
- answer: Write the report and hand the branch to review

2 tool calls · drawn from its brief

The agent stops at a plan written in [OpenSpec](https://github.com/Fission-AI/OpenSpec), because I want to understand the whole system by reading its specs, so every fix starts as a change to them. The code is written by an AI coding agent, and a separate review reads it before it merges.

One incident shows the loop at work. When the system starts, it reads its loss record, the running tally that says which strategies have hit their loss limits. One day that read timed out, and the system stopped new trades, as designed. The triage agent read the code and found a worse problem behind it: pressing Resume would restart trading with an empty loss record, so a strategy that had already hit its limit could trade again, which the spec forbids. The fix makes the system retry the read by itself and keep new trades closed until it works. The agent also called the likely trigger a hypothesis, because the logs could not tell three causes apart.

The loop also learns from false alarms. A normal event that the system’s Rust core logged at warning level once looked harmful and started a triage run, so the fix was a rule in the classifier and the next one starts nothing.

## The loop that makes it better

I first named the second loop “Self Recursive Improvement”, and I changed it to the industry’s term, [recursive self-improvement](https://en.wikipedia.org/wiki/Recursive_self-improvement), once I looked it up. My goal for it was plain: once the system collects data and trains non-stop, loops should keep improving it without me having to ask. Early on I had built a lot around the core while the training loop never ran by itself, so every proposal now has to name the goal it moves.

Mine is narrower than the term suggests. The system retrains every day on everything it recorded, and a new model replaces the current one only when it does better on the newest data, which training never reads. A candidate is tested on decisions made after it was created, and the system writes down the metric, the margin and the sample size before the test starts. The number of tries is limited, because test enough useless ideas and one will look good by chance, and I want that rule at the gate, where it protects the result. An agent may explore ideas fast on a file of older days, but an idea leaves that file only as one registered candidate.

That loop is also what noticed the silent input from the opening. Its drift monitor compared the model with the live data and flagged exactly the two features that had been empty.

## The telemetry under both

The agents are only as good as what the system tells them. I collect four kinds of data, and none of it holds a secret:

- **Error records** with a fingerprint, so repeats join one incident.
- **Metrics and traces** through [OpenTelemetry](https://opentelemetry.io), into [Prometheus](https://prometheus.io) and [Tempo](https://grafana.com/oss/tempo/), shown in [Grafana](https://grafana.com/oss/grafana/).
- **A record of every agent run**, so I can see what each agent did and what it found.
- **Health checks for each data input.**

The last one came from the 42 silent days. A pipeline that runs without errors can still be dead, so now a source that gives no data for 24 hours raises an alert, and the alert watches the output instead of the failures. The telemetry stack had the same gap: its trace store logged read errors for 8 days and nothing warned me, so now rules watch the watchers.

## Where I stay in the loop

The system closes most loops by itself. I review the fixes, I decide what counts as “by design”, and I write the goals and the specs that the loops work toward. I choose the mode the system runs in: in live assisted mode each trade waits for my approval, and in the fully autonomous mode it trades on its own, inside the guardrails the system enforces. Secrets and logins always need me too. The gate only measures: it says “measured, not enough yet” or “ready” and opens nothing. The agents move my attention from reading logs to judging decisions.

## What I’d tell someone building the same

- Build the telemetry first. An agent can’t fix what it can’t see.
- Sort errors before you send them to an agent. Most are noise, and agents cost money.
- Make a fix prove itself: a test that fails before and passes after.
- Take the agent’s credentials away. It proposes, and something else delivers.
- Treat everything the agent reads as data, because strangers can write it.
- Give the improvement loop a gate it can’t tune, and a limit on tries.
- Watch your watchers. A silent input is a fault too.
- Keep a person at the end of every loop.

## Applied AI engineering: All the parts of this series

1. *1***Apps that fix themselves and get better on their own***You are here*
2. *2*[The telemetry that lets an agent fix bugs](https://joaoramires.xyz/writing/agent-telemetry/index.md)
3. *3*[The self-improvement loop in my trading system](https://joaoramires.xyz/writing/self-improvement-loop/index.md)
4. *4*[How I train my trading models](https://joaoramires.xyz/writing/how-i-train-my-trading-models/index.md)
5. *5*[Inside my software factory: from a plan to a merged branch](https://joaoramires.xyz/writing/software-factory/index.md)

[Next part →](https://joaoramires.xyz/writing/agent-telemetry/index.md)
