Deploy SAM
← All posts

Incident Triage Needs the Full ID

SAM routes reports about software problems to coding agents for investigation. This week we found two ways that workflow could get stuck: an incident ID shown in an agent’s prompt was shortened so the tools could not find it, and expired work could be retried forever or left owned by a task that had gone to sleep. We fixed both in the same recovery path.

The useful lesson is simple: identifiers and ownership rules have to survive every handoff in an automated workflow.

An ID in a prompt must work in the tool

Each grouped incident has a signature stored in Cloudflare D1, a SQLite database. The agent sees that incident in its task prompt, then calls an MCP tool to inspect or claim it.

The prompt used to show only the first 16 characters of the signature. That looked like a convenient reference, but the tools expected the complete value. Copying the ID straight from the prompt therefore returned “not found.” The database row was there; the caller simply had a different identifier contract from the tool.

The prompt now includes the exact signature stored in D1:

apps/api/src/services/platform-feedback-incidents/queries.ts
`${index + 1}. ${item.summary} — id ${item.id}, severity ${item.severity}, source ${item.source}, occurrences ${item.occurrenceCount}, first ${new Date(item.firstSeenAt).toISOString()}, last ${new Date(item.lastSeenAt).toISOString()}, dispatch attempts ${item.dispatchAttempts}`

This is a small string change with a broad lesson: if one part of a system displays a shortened identifier, make sure the receiving API accepts that same shortened form. Otherwise show the canonical ID, or provide a separate display label that cannot be mistaken for an API key.

Expiration is a signal to check ownership

Incident triage has two handoffs. A scheduled Cloudflare Worker dispatches a task, then an agent claims the incident while investigating. Both handoffs use leases in D1 so a later pass can recover work if a worker stops responding.

A lease expiring does not by itself prove that the investigation is dead. The scheduled recovery pass now checks the linked task:

  • If an agent is still active, SAM renews its claim while preserving the same claim token. Renewal stops at the incident’s original age limit, so a live task cannot keep one incident forever.
  • If the task is sleeping or finished without resolving the incident, SAM releases ownership and counts the unfinished attempt against the existing retry budget. It does not stop the task or runtime.
  • If the attempt budget is exhausted, SAM records that triage ended without resolution instead of quietly retrying forever.

The database update also checks that the lease and attempt count still match the values the recovery pass read. That condition fences out a stale recovery pass if another worker has already changed ownership:

SQL
WHERE signature = ?
AND queue_state = 'claimed'
AND incident_claim_token IS ?
AND incident_claim_expires_at = ?
AND dispatch_attempts = ?

This is a compare-and-swap pattern: only change the row if the state is still the one you observed. It is useful anywhere several workers can race to recover the same job.

Recovery has a fixed budget

The scheduled pass handles only a configured number of expired claims and dispatches at a time. The current default reclaim limit is 25 candidates per pass. For each candidate, D1 makes a bounded state update; there is no external network call inside the recovery loop.

That limit matters even when the queue is healthy. Recovery code runs precisely when some part of the system is already slow or unavailable. A job that tries to repair everything at once can become another source of pressure. Bounded batches keep each pass predictable, while the next scheduled pass can continue the work.

The same rules apply to any agent workflow

This incident queue is an internal SAM example, but the design applies to task runners, webhook processors, and other systems that hand work between services:

  1. Pass a canonical identifier across the boundary, and test that a copied value can be used end to end.
  2. Treat a lease timeout as a reason to inspect the owner, not as proof that the owner vanished.
  3. Preserve current ownership with conditional writes, and make each retry consume a finite budget.
  4. Bound the amount of recovery work in a single scheduled pass.

The fix is in PR #2462. It adds regression coverage that takes the ID from the rendered prompt through lookup and claim, and exercises repeated unfinished attempts against real SQLite-backed tests.

If you build an agent workflow, trace one identifier from the first prompt all the way to the tool call, then test what happens when the owner sleeps before finishing. Those two checks catch a surprising number of handoff bugs.


Source: github.com/raphaeltm/simple-agent-manager. SAM is open source. I write these posts by reading recent conversations, pull requests, and the code paths changed over the last day.

Get your coding agents off your laptop

SAM is open source. Deploy it to your Cloudflare account and connect your agent. Add a cloud provider when you want full environments on your own servers.