Counterfactual

From Kevin-Liu-01/Agent-Machines · View interactive version

Counterfactual is an AI agent skill from the GitHub repository Kevin-Liu-01/Agent-Machines. Compare the current implementation against the minimal correct algorithm. Use when debugging regressions, reviewing complex fixes, or when the user asks what should happen versus what is happening now.

How to use this skill

Skill file

Counterfactual

Use this to avoid patching symptoms. The output must separate the

minimal correct algorithm from the current algorithm, then name

the smallest intervention that moves current toward correct.

Output shape

Use exactly these sections:

  1. Goal: one sentence stating the invariant we need.
  2. Minimal algorithm: pseudocode or numbered steps for the ideal

implementation. No library constraints yet.

  1. Current algorithm: what the code actually does today, including

ownership, blocking points, and cleanup boundaries.

  1. Gap: the first point where current diverges from minimal.
  2. Smallest fix: one intervention. Preserve working invariants.
  3. Falsifier: the test or measurement that proves the fix wrong.

Rules

functions, fields, and blocking calls.

a deadlock, do not revert it to fix a leak.

after the fact.

impossible states.

Checklist

Example

Goal: Dropping a per-machine vhost-user server must not block the

registry actor and must not leak fds when no VM ever connects.

Minimal algorithm: bind listener; spawn accept loop cancellable by a

shutdown token; Drop fires token, wakes listener, signals workers, joins

threads with a bound, and closes all owned fds.

Current algorithm: start() moves VhostUserDaemon into an accept

thread and calls daemon.start(&mut listener). Drop removes the socket

path, then tries to take the daemon from Arc<Mutex<Option<_>>>. If the

thread is blocked in accept(), the option is None, so cleanup is

skipped.

Gap: unlinking a Unix socket path does not wake a blocked accept(),

so the daemon owner never returns to Drop.

Smallest fix: keep Drop nonblocking, but spawn a bounded reaper that

self-connects to wake accept(), waits for daemon handoff, signals

workers, and joins the protocol thread off the actor thread.

Falsifier: create and drop N servers without clients, then assert the

fd delta over a control arm stays below the regression threshold. If fd

count grows linearly with N, the fix is wrong.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.