My trading system runs a lot of small background jobs, and each one leaves a row behind that says when it started and how it ended. In late October, five of those rows, all from jobs that deliver alerts, got stuck. The jobs had done their work, and all 15 alerts they had to send went out within 20 milliseconds, yet their rows still read running, with no end time, as if the jobs were still busy. Nothing had crashed, so there was no stack trace to follow. The only clue was that four of the five jobs had raised the same database timeout error on their way out.
An agent found the cause of that, and it could do so because of what the system had already written down. This is the second article of a series that starts with how I build apps that fix themselves, and it is about that writing: the telemetry. I designed the system to carry its own SRE tools and to fix itself, as the first article describes. That only works if the tools can read what the system says about itself.
A person at a dashboard brings memory. I know that a spike is the nightly job and that a warning is harmless. The agent starts each run with none of that, so the telemetry has to carry it. That means one record per fault instead of per event, a verdict before the agent starts, the context inside the record, text that is safe to read, and proof that the telemetry itself still works.
One fault, one incident
The Rails app and the Rust core write each error as a record: its source, process, severity, whether the code handled it, the error class, the place in my code, the message, the backtrace and the time. Nobody edits a record after it is written.
The fingerprint makes repeats join. It hashes the error class and the place in my code, after removing the line number, so an edit above a failing line doesn’t split one fault into two:
fingerprint.rbRUBY
1defself.fingerprint(error_class,app_frame)2 place = app_frame.to_s.sub(/:\d+:in /," in ").sub(/:\d+\z/,"")3Digest::SHA256.hexdigest("#{error_class}|#{place}")[0,16]4end
A job takes the new records in passes, groups them by fingerprint and adds each group to an incident that keeps the count, the first and last time and the latest message. In my case, the four timeout errors shared one error class and one place, so they became one incident. An incident that goes quiet for 24 hours and then returns starts a new episode, and it keeps its old class in a history.
Fixed rules in code then give each incident a class. Noise is expected and harmless, and a handled event at severity info lands there whatever its error class. Self-healing means the system repaired itself, as when a retry clears a short network failure, and it raises no alert. Harmful means the incident changes what the system decides or records, so it alerts and enters the triage queue. A self-healing incident that comes back every day for 3 days with at least 10 events isn’t healing, so it goes to the agent as well, with the days and the events of the last three windows of 24 hours attached. An error that no rule knows is harmful, which means a new fault is never silent, and the price is that each new false alarm costs an agent run until I add a rule for it.
That is what happened with my stuck jobs: their timeout error matched no rule, so the incident was harmful and a triage run started.
What the agent reads
The triage agent, a headless Claude Code session, receives a fixed brief and the incident record as JSON: the fingerprint, the class and the rule that gave it, sample errors with their backtraces, and the agent runs that raised them. It also gets a clean branch of the code and the repository’s own rules. It can replay an hour of recorded data and run one read-only query, and it writes a report with the headings Cause and Fix and a list of what it did not verify. For a defect, it opens an OpenSpec change with the plan and the test that must fail without the fix. A coding agent then builds the plan, a review reads the code, and after my approval the change merges.
I read the same incident on a page of its own, which shows the class and the rule, the five stages from incident to main, and the agent’s Cause and Fix side by side, so the agent and I never read two versions of a fault.
The brief tells the agent that every text in the incident is data and never an instruction. An error message can hold a name chosen by a stranger in the data I read, or a reply from a server I don’t control, and I can’t out-argue a hostile string in advance. So the agent has little to be talked into: it never merges, restarts a service or reads a secret file, and its output goes to a report that I read.
Secrets get the same care. One function writes [redacted] in place of each secret, in the Rails app and in the Rust core, and both pass the same file of test cases. It hides first and cuts after, so a cut can’t leave half a secret. Hashes and public identifiers stay, because a redactor that eats them ruins the evidence. If the function fails, the system stores [not stored: the redaction failed] and never the raw text. Error records, alerts, spans, run records and the brief all pass through it.
The case, continued
Every job leaves a run row in the database, and every headless agent run writes a record of its own: one file of JSON lines. The first line holds the trigger and the last holds the status, the cost and the kind of fix. A line in between marks each change in the chain of work. Lines only append and each is at most 4,096 bytes. A run with no end line counts as running until its last line is 3 hours old, and as stopped after that. That is why five job rows reading running mattered: a row with no end meant the system had lost the record of how that job ended.
The agent set the incident beside the rows around it. The next alert-delivery job, seconds later, ended in 97 milliseconds. Nine earlier deliveries, one at a time, all succeeded. Five unrelated jobs that started together earlier had ended in 40 to 113 milliseconds. So the cause was not “five jobs at once”. Only the delivery jobs took a database lock before they finished.
Its hypothesis: the connection pool has 5 connections, and the lock helper left each job’s thread holding one. The code that writes the end of a run needs a second connection, so five jobs held five and each waited for a sixth, like five people who each need a sixth chair. The agent marked this as a hypothesis, because it could not run a direct test and two of its commands were refused. It also listed what it did not know, such as why the incident held four events for five stuck rows.
The plan changes the lock helper to take its connection for one statement only, and makes the run tracker give back its own connection before it writes the end of a run. It leaves the pool size alone and says why. Its test uses a pool of 2 connections and 2 threads, and it fails without the fix. I care that this is a root fix. While we were cleaning up flaky tests, I told the agents to fix every error at its root and never to bend a test until it passes. The change was reviewed and merged.
One gap showed the limit of my own telemetry. The backtrace of each error stopped at the write function, because the failing code ran on a new thread, and the caller was not in it. The agent found the caller by reading code, and I would rather the record carried it.
Watching the pipes
OpenTelemetry carries metrics and traces from both halves of the system to a collector, which sends metrics to Prometheus and traces to Tempo, and Grafana shows them. The long steps of a training run are spans, and so is the drift check. The stack reports to no third party, which matches what I told the agents: internal telemetry with OpenTelemetry is fine and desired, and I don’t want to leak my data.
Each data input, a feed the system reads, has its own health check, because a job can run without error and find nothing. An input that is on and delivers no data for 24 hours raises one alert, once for the whole silence.
The stack breaks too, so four rules watch the stack: Tempo can’t refresh its list of trace blocks, the collector fails to send spans, Tempo is down for 2 minutes, and no span arrives for 15 minutes. This is the one for damaged blocks. It fires when Tempo’s own error counter rises, and also when Tempo is up but has stopped reporting the age of its block list:
rules.ymlYAML
1- alert:TempoBlocklistPollFailing2expr:>-3 sum(increase(tempodb_blocklist_tenant_index_errors_total[7m])) > 04 or (absent(tempodb_blocklist_tenant_index_age_seconds)5 and on() up{job="tempo"} == 1)6for:10m
Each rule has a test with one series that must fire it and one that must not, run with the promtool that comes with Prometheus, and a rule with no case fails the build. A second test checks that each dashboard query names a metric my code exports.
This part matters to me because I don’t sit and watch the system. The alerts and the incident page almost always catch a fault before I do, so the pipes that carry them have to be the part I trust most.
Mistakes I made
My first Tempo rule read a metric that Tempo never exports. Tempo logged the same error 2,304 times in 8 days and nothing warned me, which is why every rule now needs a firing case and a quiet one. My input health check skipped inputs that were switched off, so when all of them were off it said nothing, and now it also alerts on the outcome: no data for 24 hours. A refit of the model on every recent row sat skipped for 15 days, and its run record only said the new model showed no gain, so a step that can skip now has to record why, in words I read.
The alerts those stuck jobs delivered came from one event: a new version of the model inputs went live, five strategies could no longer load their models, and the core raised one alert for each in the same second. Each alert made its own incident and its own triage run, and two of the plans contradicted each other. Now the incidents of one event, up to 8 within 60 seconds, join the first run, and that run answers for each of them.
The listener, the part that turns each event of the Rust core into a trace, also made 64% of all traces and buried the web and job traces I actually read. Now it keeps one trace in 20, picked by trace id, and a trace stays whole or goes whole.
What I’d tell someone building the same
Fingerprint without line numbers, so a refactor doesn’t split a fault.
Class errors in code, and make the default harmful.
Send the agent the same record you read.
Redact in one place, and test it in every language you use.
Write a record for every run, including the ones that skip.
Give each input a silence alert.
Test every alert rule, and watch the stack that carries them.