# Orch writes its own bug reports

2026-11-30 · https://gabrielkoerich.com/posts/orch-writes-its-own-bug-reports/ · tags: ai, agents, rust, orch

> Two scheduled orch jobs file issues against orch itself. 290 issues later, all closed, I checked which fixes held and which never reached the service.

On October 4 at 21:08 UTC, issue [#3641](https://github.com/gabrielkoerich/orch/issues/3641) opened against [orch](https://github.com/gabrielkoerich/orch). It said a MiniMax run had failed two days earlier with `API Error: 402 insufficient balance (1008)`, and that orch had filed it as a generic failure instead of an empty account. It had the task run id, the cooldown rows from the database, the file and line where the check was missing, and a suggested fix. At 21:12 a PR opened. At 21:20 [PR #3642](https://github.com/gabrielkoerich/orch/pull/3642) merged and closed the issue. I did not write the issue or the fix.

The failed run in that issue was a run of the job that filed it. Orch has two scheduled jobs that look at orch itself: one reads the last day of agent failures, the other wrote a daily review, weekly since October. Both file issues against the repo, and the issues go through the same pipeline as any other task. [Orchestrating agents for humans](https://gabrielkoerich.com/posts/orchestrating-agents-for-humans.md) covers what orch is and how a task gets from an issue to a merged PR.

## Two jobs, one pipeline

A job in orch is a markdown file in `prompts/jobs/` with a cron schedule in its frontmatter. `src/engine/jobs.rs` loads them, the schedule runs in UTC, and each run becomes an internal task that gets routed to an agent like any issue would. Two of these jobs file issues against orch:

| Job | Schedule (UTC) | First run | Runs | What it writes |
|---|---|---|---|---|
| `agent-debugger` | 21:00 daily | 2026-03-24 | 189 | GitHub issues for the root causes of agent failures |
| `daily-review` | 23:00 daily | 2026-06-04 | 110 | A post in `docs/content/posts/`, plus at most 2 or 3 issues |

The run counts are rows in the `tasks` table of my local orch database, up to October 6.

The daily review prompt was short. It told the agent to read the last 24 hours of `git log`, the previous review post, open and recently closed issues, `orch task list`, the last 200 lines of the service log, and the repo's own operations skill. Then it queried the database directly:

```sql
SELECT agent, model, outcome, COUNT(*) FROM task_runs
WHERE datetime(started_at) > datetime('now', '-24 hours')
GROUP BY agent, model, outcome ORDER BY COUNT(*) DESC;
```

A second query does the same for `task_activity`, which logs every status change, dispatch, push, review and cooldown. `task_runs` holds one row per agent run with the command, the full output, the outcome and the error. Between them, the agent can see what failed and what the engine did about it.

The prompt also set rules for filing. Before it opens an issue, the review must check open issues, the last 50 closed ones, and the last 7 days of commits, so it does not file something already fixed. Before it files about routing or config, it must search `src/` for a mechanism that already handles it. It files root causes only, never features, and no more than two or three a day. The output is a post, and the prompt says plainly: "do NOT modify code."

The review posts are public in the repo. There are 106 of them so far, each with what shipped, what failed, a table of runs by agent and outcome, stuck tasks, and priorities for the next day.

On 13 October I moved the review to Sundays at 23:00 UTC and renamed it `weekly-review` ([4f61908a](https://github.com/gabrielkoerich/orch/commit/4f61908a)). It now reads 7 days of commits and runs, the last 100 closed issues and 1,000 lines of the log, and writes `weekly-review-YYYY-MM-DD.md`. The filing rules did not change.

On 29 October I moved the debugger to 03:00 UTC and turned on a third job that files issues, `code-review-orch`, at 04:00 UTC with up to five a day ([a1dc6e5c](https://github.com/gabrielkoerich/orch/commit/a1dc6e5c)). The numbers below cover only the first two.

## What the jobs filed

GitHub does not know which job opened an issue, because both use my `gh` login. So I matched them myself: for every run of either job whose output contains `gh issue create`, I took the issue URLs in that output and kept the ones created while the run was going on.

| Job | Issues opened | Closed | Closed by a linked PR | Median time to close |
|---|---|---|---|---|
| `daily-review` | 27 | 27 | 27 | 25 min |
| `agent-debugger` | 263 | 263 | 229 | 25 min |

Since the daily review started on June 4, the repo got 144 issues. These two jobs opened 131 of them. On October 7 the repo had two open issues, and neither came from these jobs.

All 290 are closed and GitHub marks every one as completed. "Closed by a linked PR" is closer to the truth, and it still does not tell you whether the fix reached the running service.

## Loop one: an empty account

On October 2, the `agent-debugger` run itself went to MiniMax, and MiniMax answered with a 402. The engine failed over to Claude and the task finished. Orch put MiniMax on a cooldown and moved on.

The next night's daily review saw that failure in its `task_runs` table and wrote: "Generic cooldown path as designed." It filed nothing.

The night after, the debugger read the same row more closely. Orch has a separate path for credit exhaustion, with a longer cooldown and its own counter, and the 402 never reached it. The issue showed why with two lines from the `kv` table:

```text
cooldown:minimax          | {"reason":"agent_error","until":1791157614}
credit_failure_count:minimax | 0        <- never incremented
```

The error string did not match any auth pattern, so it was classified as `AgentFailed`, and that arm of the error handler never checked for credit exhaustion. Only the rate limit and auth arms did. The issue also pointed out that this was the same kind of bug as [#3256](https://github.com/gabrielkoerich/orch/issues/3256), where 429s had been recorded as `failed` back in June, and that it came back because that fix went into the classifier patterns instead of a check every arm passes through.

The fix in [#3642](https://github.com/gabrielkoerich/orch/pull/3642) adds `insufficient balance` to the patterns and puts the check in the `AgentFailed` arm of `src/engine/runner/fallback.rs`:

```rust
// Credit-shaped errors that escaped the Auth/RateLimit classifiers still
// need the credit cooldown and counter, not the generic 5 min backoff.
if let Some(reason) = crate::engine::cooldown::detect_credit_exhaustion(message) {
    match model_name {
        Some(model)
            if reason
                == crate::engine::cooldown::CreditExhaustionReason::BillingCycleExhausted =>
        {
            crate::engine::cooldown::record_persistent_model_failure(agent_name, model)
                .await;
        }
        _ => crate::engine::cooldown::record_credit_exhaustion(agent_name, reason).await,
    }
}
// ...
```

The next day another MiniMax review run hit the same 402. The October 5 review caught it: "The fix is merged but the service runs the previous release, so credit-shaped failures still land on the old cooldown path."

That was correct. The fix shipped in v0.81.62 on October 4. On October 7 the running service was still `orch 0.81.59`, started on October 2. The rebase conflict fix from the night before, [#3638](https://github.com/gabrielkoerich/orch/issues/3638) and [PR #3639](https://github.com/gabrielkoerich/orch/pull/3639), shipped in v0.81.60 and was not running either.

## Loop two: the review's own clock

On August 23 the daily review filed [#3548](https://github.com/gabrielkoerich/orch/issues/3548) about the queries it was running itself.

`task_runs.started_at` and `task_activity.timestamp` are stored as RFC 3339 strings, with a `T` between date and time. SQLite's `datetime('now', '-24 hours')` returns a space there. The original prompt compared them as plain strings, and since `T` sorts after a space, every row from the cutoff's calendar day passed the filter, whatever its time. I reproduced it in an in-memory database:

```text
$ sqlite3 -header :memory: "select datetime('2026-08-23 23:01:00','-24 hours') as cutoff,
    '2026-08-22T05:00:00Z' > datetime('2026-08-23 23:01:00','-24 hours') as raw_string,
    datetime('2026-08-22T05:00:00Z') > datetime('2026-08-23 23:01:00','-24 hours') as wrapped;"
cutoff|raw_string|wrapped
2026-08-22 23:01:00|1|0
```

A run 18 hours before the cutoff counts as inside the window. Against the live database at 23:01 UTC the issue counted 90 runs in the "last 24 hours" with the old query and 44 with the correct one. A review that runs at 23:00 sees almost two days of history and calls it one. The issue said so itself: earlier review tables "likely counted closer to 48h of history."

The bug was not only in the prompt. The same comparison was in 14 places in `src/store/metrics.rs`, which feeds `orch stats`. [PR #3550](https://github.com/gabrielkoerich/orch/pull/3550) wrapped every column in `datetime(...)`, fixed the same query in the review prompt, the debugger prompt and the operations skill, and added a regression test for a row on the cutoff's calendar day. Issue to merge took 25 minutes. The `datetime(started_at)` in the query above is that fix.

## Loop three: fixed in the repo, not on the machine

On June 27 a fix for a log line that repeated on every sync tick shipped as v0.80.32. The daily review that night saw the warning still in the log, ran `orch -V`, got `0.80.31`, and filed [#3358](https://github.com/gabrielkoerich/orch/issues/3358): the fix was released but the service had not upgraded.

The agent that picked it up wrote code, [PR #3360](https://github.com/gabrielkoerich/orch/pull/3360) turned on auto-upgrade by default and made the first upgrade check run at startup. It merged 18 minutes after the issue opened, and the issue closed. That code only runs after an upgrade, and the service did not upgrade.

The next review still saw `0.80.31`. On June 29 the debugger filed [#3364](https://github.com/gabrielkoerich/orch/issues/3364), and it closed four minutes later with no PR. On July 1 the debugger filed [#3368](https://github.com/gabrielkoerich/orch/issues/3368), which described the cycle from the `task_runs` table: the agent cannot run `brew` in its sandbox, so attempt one fails with "no code changes", the engine reroutes, and attempt two closes the GitHub issue, which the engine then syncs as done. The agent on #3368 posted that the upgrade had to be run by hand and wrote "This issue must NOT be closed until the upgrade is confirmed." The issue closed four minutes after it opened.

The July 4 review found the service on v0.80.38, "verified via lsof", a week after the first issue. On August 4 a commit removed auto-upgrade entirely ([#3466](https://github.com/gabrielkoerich/orch/pull/3466)) because it "mutated shared system state without operator consent". Upgrading is now an operator decision, and the operations skill that both jobs read says so:

```text
- Do not report CLI, service or `main` version drift as an issue, finding or action item.
```

So the October 5 review followed its own rules. It noted the lag and filed nothing, and the fix merged on October 4 was still not running three days later.

One thing is allowing agents to fix code autonomously, the other thing is running those new versions automatically. I still need to review what happened and what changed before I upgrade something on my machine. The idea of an auto-upgrade was totally in one agent's mind, and the reviewer approved, but that was not something I could just allow, so I had to remove the auto-upgrade code and update the [prompts](https://github.com/gabrielkoerich/orch/commit/a818009e0f208d4988375e84491aabd5ecd19a20).

I also had to write a section of Settled architecture, with an explicit 'DO NOT TOUCH', because some agents always tried to implement something in a different way that was already done, or create something that was out of scope. The prompt changes help, but some agents still need more control.

## Fixes that did not hold

The review also runs again the next day, so it sees whether a fix worked. Seven of the 27 issues it filed refer to an earlier issue or PR in the title, and several of those are a fix that did not work:

- [#3526](https://github.com/gabrielkoerich/orch/issues/3526): a stuck-task reclaim race "recurred post-#3525", 16 hours after the fix merged.
- [#3580](https://github.com/gabrielkoerich/orch/issues/3580): the failover path bypassed a usage window parser, "undoing #3572/#3574 fix".
- [#3587](https://github.com/gabrielkoerich/orch/issues/3587): CI failure recovery "still not landing PR #3536" after 16 cycles, six days after the fix for [#3561](https://github.com/gabrielkoerich/orch/issues/3561) merged.
- [#3609](https://github.com/gabrielkoerich/orch/issues/3609): the CI pending timeout from #3603 "still never fires".

In each case the earlier fix had merged, and a later review found the problem still in the database and the logs.

## Where the loop leaks

**Closed is only a GitHub state.** GitHub marks all 290 issues as completed, the ones a PR fixed and the ones like #3364 and #3368 that had nothing to fix.

**Releases wait for me.** A fix can sit in a release for days before I upgrade, and the review does not flag that, because I told it not to.

**An agent judges agents.** The October 3 review called the 402 "as designed". The October 4 debugger called it a bug, and the debugger was right. Which job runs, and on which model, changes what gets filed.

**Some models cannot do the job.** Of 5 daily review runs routed to MiniMax, none succeeded. Claude finished 100 of 102. Each failed run costs a failover to another agent, and 109 of the 110 review tasks still finished.

**Fixes ship without tests.** PR #3642 changed two files and added no test for the 402 string. The test came four days later in [#3668](https://github.com/gabrielkoerich/orch/pull/3668). Nothing stops the next error string with a new shape from taking the same path.

**It costs runs.** A successful daily review took about 3 minutes of agent time. A debugger run takes 3 to 15 minutes on average, depending on the agent. Every issue they file then costs a code run and a review run.

## Where the loop stops

The loop stops at the merge, and that is on purpose. The jobs find a bug, file it and get a fix merged, the median issue closed in 25 minutes, and a fix runs on my machine when I upgrade. #3358, #3364 and #3368 should never have been opened: the bug was already fixed, and the agents were reading logs from a version I had not installed yet. Since the operations skill says version drift is not a bug, the reviews file what is broken in the code, and the next review checks whether the fix held.

If you're interested, the job prompts are in [`prompts/jobs/`](https://github.com/gabrielkoerich/orch/tree/main/prompts/jobs), the operations skill is [`SKILL.md`](https://github.com/gabrielkoerich/orch/blob/main/SKILL.md). You can also check the full history of orch review updates [here](https://orch.gabrielkoerich.com/posts/).
