Agent ReliabilityAI AgentsMCPSQLitePython

Stopping AI Agents from Double-Posting When Tools Time Out

Putting a local SQLite write-ahead journal, deterministic preflight rules, and read-only crash reconciliation in front of mutating MCP and CLI calls.

By Solomon Wakhungu7 min read
Chaos suite6/6 pass0 duplicate writes, 100% receipt accuracy
Read passthrough+0.02 msNo SQLite writes on read-only MCP calls
Write-ahead cost~8.0 msTwo full-fsync SQLite WAL commits on macOS

Timeouts turn helpful agents into duplicate-write machines

Your coding agent creates a GitHub issue or fires a webhook, the call times out right after the server accepts it, and the agent immediately retries. Now you have two identical issues, a duplicate comment, or a double webhook delivery.

Then, at the end of a long session, the agent summarizes its external changes from compressed chat memory and either misses a git push or claims a timed-out write landed.

I built sidefx to put a durable local SQLite write-ahead journal in front of mutating MCP (Model Context Protocol, the standard way agents talk to external tool servers) and shell calls, so retries reconcile instead of double-posting and session receipts come straight from database rows.

Why a local SQLite WAL instead of prompt rules or a hosted proxy

Telling an agent in a system prompt not to retry blindly does not work when a timeout leaves the agent with no way to know whether the write landed. Standing up a separate proxy server or external database for local CLI workflows is overkill nobody will actually maintain.

SQLite with a WAL (write-ahead log, SQLite's crash-safe commit log) and full synchronous disk flushes is already built into Python's standard library. It handles concurrent local worker processes cleanly, survives crashes, needs no third-party dependencies, and lives inside an owner-only local directory that automatically gitignores itself.

I also made a deliberate privacy trade-off early: the journal stores SHA-256 hashes of tool arguments and command arrays rather than raw payloads, and redacts URL credentials, webhook path tokens, auth headers, and API keys before saving targets or error snippets.

How the MCP proxy, CLI wrapper, and crash reconcilers fit together

sidefx sits in front of external writes in two places: a drop-in stdio MCP proxy for Claude Code, Cursor, or Codex, and a CLI wrapper for shell commands like git push, GitHub CLI writes, and curl webhooks.

Write-ahead durability
How sidefx prevents duplicate external writes after timeouts and crashes
1. Intercept
MCP Proxy or CLI Wrap
Read-only tools pass straight through; mutating calls enter the journal.
2. Preflight
Deterministic Rules
Checks numeric caps, protected branches, required args, and allowed domains.
3. Write-Ahead Log
SQLite WAL (Pending)
Atomically claims actor, target, and action ID with a SHA-256 payload hash.
Clean Reply
Confirmed & Cached
Marks confirmed. Identical retries replay from SQLite without calling upstream.
Timeout or Crash
Unknown (Fail Closed)
Blocks blind retries. Reconciles via read-only GitHub API or git ls-remote.
Session End
Ground-Truth Receipt
Stop hook and report command read SQLite in read-only mode to list real writes.
Every mutating call claims an atomic pending row in local SQLite WAL before touching the network. Timeouts move the row to unknown and block blind retries until read-only reconciliation confirms what landed.

In the MCP proxy, read-only tools and protocol handshakes pass straight through with roughly 0.02 ms of median overhead. Mutating tool calls first hit deterministic preflight rules in your project config, where you can enforce required arguments, numeric caps like maximum refund amounts, protected-branch denylists, and allowed email domains. A violation fails closed before anything reaches the upstream server.

Calls that pass preflight take two durable WAL commits: first writing the action as pending (keyed by actor, target, and action ID) before calling upstream, and second marking it confirmed once the reply arrives. On macOS with full fsync (flushing data all the way to disk) enabled, those two commits add about 8.0 ms of median overhead.

Once confirmed, identical MCP retries in the same session replay directly from disk in 0.044 ms without touching upstream when deterministic IDs are enabled or when an explicit action ID is supplied. Reusing the same action ID with modified arguments is rejected immediately as a payload mismatch.

When a call times out or crashes mid-flight, sidefx marks the row unknown and blocks blind retries with an explicit outcome-unknown error containing exact recovery commands. For git push, the built-in reconciler checks the remote read-only with git ls-remote. For GitHub issues, pull requests, and comments, sidefx injects a tiny hidden HTML comment marker into outgoing bodies and checks both open and closed items through the GitHub REST API to recover the exact URL instead of creating a duplicate.

The stderr redaction bug that could have stranded pending writes

Hardening the CLI wrapper before launch turned up a failure mode I almost missed. When a wrapped command fails, sidefx scrubs secrets out of stderr before storing a short error line in SQLite.

If a failing child command echoed a malformed URL, like an invalid port number or broken IPv6 brackets, Python's built-in URL parser raised a ValueError right inside the redaction step. Because that happened right before finalizing the journal row, an uncaught redaction error could crash the wrapper and leave the action stranded in pending.

The fix was twofold: isolate URL redaction behind a safe fallback so malformed strings can never raise, and tighten the URL scanner with a negative lookbehind so long noisy error lines always scan in linear time instead of backtracking. A logging helper should never be able to wedge the durability layer it serves.

Ground-truth receipts and a real chaos suite

Because every write lives in SQLite, session summaries no longer rely on chat memory. Running the report command opens the journal in strict read-only mode and prints confirmed, reconciled, failed, and unresolved actions in Markdown, plain text, or JSON. A bundled Claude Code Stop hook stays completely silent when no external writes occurred and warns loudly if anything is still pending or unknown.

To test crash behavior for real instead of mocking every function call, sidefx ships with a self-contained chaos test suite. It spins up a local mock GitHub HTTP server, a real bare git repository with fault-injecting receive hooks, a subprocess stdio MCP server with an fsync-backed ledger, and four concurrent OS worker processes racing on the same action ID.

Across all six chaos scenarios (pre-commit crashes, post-commit response loss, concurrent duplicate dispatch, payload mutation on reused IDs, preflight policy denials, and mixed session receipts), it passes 6 of 6 with 0 duplicate remote writes and 100% receipt accuracy (17 of 17 checks) in about 13 seconds.

Manual resolution and wedged MCP servers

Not every external API has a built-in reconciler yet. Right now automatic read-only reconciliation covers git push and GitHub issues, pull requests, and comments. When an arbitrary MCP server or custom webhook times out, sidefx refuses to guess and leaves the action unknown. You or the agent inspect the remote system read-only and record the verdict with the resolve command (either landed with a reference URL, or not landed to re-open the retry).

Wedged single-threaded servers

Some local stdio MCP servers are single-threaded and hang permanently after a timeout. sidefx provides an opt-in restart-on-timeout flag that probes the server on the next request, terminates the process group if unresponsive, and replays the MCP initialize handshake transparently. It stays off by default so slow-but-healthy serial servers can finish in-flight work and reconcile cleanly.

Run the chaos suite or wrap your next push

You can watch the six fault-injection scenarios run locally without installing anything:

Run the built-in chaos suite
uvx sidefx eval --verbose

To start journaling shell writes and configuring preflight guardrails in a repository:

Initialize and wrap a mutating command
uv tool install sidefx
sidefx init
sidefx wrap -- git push origin HEAD
sidefx report

Drop-in MCP proxy snippets for Claude Code, Cursor, and Codex, plus the Claude Code plugin and agent skill, live in the GitHub repository.

View all posts
Agent Verification · 6 min read

Your Coding Agent Cited Line 48 Without Ever Opening the File

Why I built a deterministic transcript verifier that checks file citations, quoted code, and 'no other callers' claims in under a millisecond without calling another model.

Read post
Agent Memory · 6 min read

Context Compaction Keeps Erasing Your Rules Right When Precision Matters

How compact-ledger captures verbatim user constraints, mid-session overrides, and failing test trajectories before compaction and re-injects them in about 50 milliseconds.

Read post
CLI & Guardrails · 6 min read

Giving AI Agents Safe Access to Discord Without MCP Schema Bloat

Why I built a REST-first Discord CLI with 89 commands, operator policy ceilings, offline dry-run confirmation tokens, and random-nonce prompt-injection boundaries.

Read post