Back to Blog
Durable Agent Workflows: State, Retries and Merge Gates
AI & Machine Learning
9 min read
56 views

Durable Agent Workflows: State, Retries and Merge Gates

Abhijith N M
Abhijith N M

November 5, 2026

Table of Contents (9)Expand ↓

The demo works. The second run is where it breaks.

A coding agent that opens a pull request is a weekend project. A coding agent that opens pull requests every day, survives a container dying mid-edit, doesn't retry a failing test forever, and never merges its own work is an operations problem wearing a code-change costume.

Inkitt built a one-shot PR creator that automates small, low-risk changes from task intake to a draft PR. Per that report, it runs on an event-driven orchestrator that persists workflow state, coordinates short-lived agent containers, bounds retries, and keeps human approval before merge. The report also notes the system was shipping code daily, according to Inkitt's own technical writeup.

The interesting part isn't the agent. It's the scaffolding around it. Below is how I'd think about building the same shape, with the parts the report doesn't specify marked as my inference.

What "durable" actually buys you

A workflow is durable when it can stop and start again without losing where it was. The naive version is a script: read task, call model, write files, open PR. If the process dies after writing files but before opening the PR, you have a half-applied change and no record of intent.

The Inkitt design, as described, persists each run in a database so execution can resume from the recorded state. That means the unit of progress is a transition: a record that says "run 123 moved from implementing to verifying at time T." If the worker dies, a new worker reads the last transition and continues from there.

Two things follow, and both are costs:

  • You now own a schema. Every state you add is a migration. Every field you rename is a backfill over in-flight runs.
  • You now own idempotency. Resuming means some side effect may already have happened. Opening the same PR twice, posting the same Slack message twice, or pushing the same commit twice all need to be safe.

That second point is where most homegrown implementations quietly fail. The report says the orchestrator separates decision-making from side effects: a core transition function maps current state plus incoming event to the next action without touching a database, network, or model. That's the right instinct, and it's worth spelling out why.

Pure decisions, impure effects

If the function that decides "what next" also performs the action, you can't test it, replay it, or retry it without side effects. Split them:

🟨JavaScript
// transition.js β€” pure. No I/O. Easy to unit test.
export function nextAction(state, event) {
  switch (state.stage) {
    case 'intake':
      return { type: 'START_AGENT', stage: 'specifying' };
    case 'specifying':
      if (event.type === 'AGENT_DONE') return { type: 'START_AGENT', stage: 'implementing' };
      if (event.type === 'AGENT_FAILED') return retryOrFail(state, event);
      return { type: 'WAIT' };
    case 'implementing':
      if (event.type === 'AGENT_DONE') return { type: 'RUN_CHECKS', stage: 'verifying' };
      if (event.type === 'AGENT_FAILED') return retryOrFail(state, event);
      return { type: 'WAIT' };
    case 'verifying':
      if (event.type === 'CHECKS_PASSED') return { type: 'OPEN_DRAFT_PR', stage: 'awaiting_review' };
      if (event.type === 'CHECKS_FAILED') return retryOrFail(state, event);
      return { type: 'WAIT' };
    case 'awaiting_review':
      return { type: 'WAIT' };
    default:
      return { type: 'FAIL', reason: `unknown stage ${state.stage}` };
  }
}

function retryOrFail(state, event) {
  const attempts = (state.attempts ?? 0) + 1;
  if (attempts > state.maxAttempts) {
    return { type: 'FAIL', reason: event.reason ?? 'retry budget exhausted' };
  }
  return { type: 'START_AGENT', stage: state.stage, attempts };
}

A separate executor takes that action, performs the I/O, and writes the resulting transition. The decision function stays a switch statement you can read in a code review. That's the whole trick.

Short-lived containers and why they matter

The report describes agent containers as short-lived, receiving short-lived repository tokens and model credentials, and unable to reach Slack or Linear credentials, cloud identities, or arbitrary network connections. The orchestrator holds the long-lived credentials.

This is a blast-radius argument, not a performance one. A container that lives for the duration of one stage and holds a token scoped to one repository can do limited damage if the model is talked into something strange. A long-lived container with ambient cloud credentials can do a lot.

If you're building this, the practical checklist is:

  • Scope tokens per run, not per service. A token minted for run 123 that expires in minutes is easier to reason about than a shared bot token.
  • Deny egress by default. If the agent needs a package registry, allow that host. Don't assume the model won't try to fetch something.
  • Don't pass the orchestrator's environment into the container. It's the easiest mistake to make and the hardest to notice.

There's a related failure mode worth naming: the agent's instructions are not a security boundary. The report says a separate check inspects the actual Git diff to ensure changes don't touch restricted paths. That's the correct pattern β€” inspect the artifact, not the intent. Prompt-level instructions are advisory; a diff check is enforcement. If you want more on why that distinction matters, prompt injection is an architecture problem covers the same reasoning from a different angle.

Bounding retries without hiding failures

"Bounds retries" sounds like a for loop with a counter. The hard part is deciding what counts as retryable.

A model call that timed out is probably worth retrying. A model call that returned a patch touching a restricted path is not β€” retrying it just spends money to get the same refusal. A CI failure caused by a flaky test is retryable; a CI failure caused by a compile error the agent introduced is a signal that the implementation stage needs another pass, which is a different transition, not a retry.

The report mentions individual stages handling specification, implementation, verification, CI failure repair, and merge conflict resolution. That's a useful decomposition because it gives you distinct retry budgets per stage rather than one global counter.

Approach β€” What it protects β€” What it costs Β· Single global retry counter β€” Total cost β€” One flaky stage starves the rest Β· Per-stage counters β€” Stage-level progress β€” More state to persist and migrate Β· Retry with backoff, no cap β€” Transient errors β€” Unbounded spend on a deterministic failure Β· Classify-then-retry β€” Correctness and cost β€” You need an error taxonomy you'll maintain

My take: classify-then-retry with per-stage counters is the version that survives contact with production. It's also the version that requires the most upfront design, which is why people skip it and then spend a quarter debugging loops.

One more thing the report doesn't specify: what happens to a run that exhausts its budget. A reasonable default is to park it in a terminal failed state, keep the transition history, and notify the requester with the last error. Don't silently drop it β€” a failed run with a record is debuggable; a vanished run is not.

The merge gate is a permission, not a policy

The report is explicit that human review and approval are retained before changes can be merged. The design detail that matters is where that gate lives. If it's a line in the agent's system prompt saying "do not merge," it's a suggestion. If it's the absence of merge permission on the token the container holds, it's a boundary.

Concretely, for a GitHub-style setup, the agent's token should be able to push a branch and open a draft PR, and nothing else. Merging requires a human identity. The orchestrator can hold a token that opens PRs; it should not hold one that merges them.

The report also says automation is limited to changes within predefined boundaries. That's a scope decision as much as a safety one: small, low-risk changes are the ones where a draft PR plus a human reviewer is cheaper than writing the change by hand. The moment the change touches something load-bearing, the economics flip.

Where does durable state become a liability here? When the persisted run becomes the source of truth for something it shouldn't. If your orchestrator's database says a PR is "awaiting review" but the PR was merged manually an hour ago, you have two truths. The fix is to treat external systems as authoritative and the run state as a cache of intent β€” reconcile on read, don't assume.

Where the complexity isn't worth it

An event-driven orchestrator with persisted transitions is more machinery than most teams need for their first agent. If you're shipping one change a week and a human is watching, a script with a try/catch is fine. The orchestration earns its keep when runs are frequent, long, or expensive enough that a lost run is a real cost.

The tradeoff table:

Simple loop β€” Durable orchestrator Β· No schema to migrate β€” State schema is now an API Β· Crash loses the run β€” Crash resumes from last transition Β· Retries are ad hoc β€” Retry budgets are explicit and auditable Β· Hard to answer "what happened to run 123" β€” Transition log is the answer

If you're not yet at the right-hand column, don't build it. But know that the migration from left to right is mostly about extracting the decision function from the effects β€” and if you write the switch statement first, that extraction is a refactor, not a rewrite.

For teams thinking about the surrounding measurement questions β€” how often runs fail, how long stages take, what the retry distribution looks like β€” six numbers to check before you ship an AI feature is a reasonable companion. And if you're earlier in the journey, concept bites covers the vocabulary.

Further reading

Sources

Share

About the Author

Abhijith N M
Abhijith N M

Founder, writer and editor

I'm a software developer based in India. I write about AI engineering, system design and the parts of software that tutorials skip. I started nexGenCoders to put the explanations, diagrams and working examples I wanted while learning in one place.

Comments (0)

Loading comments...