Skip to content

Resume agent plans without duplicate work

Idempotent multi-step plans via Plan-Key: cache each step server-side for 24 hours so retries replay completed steps instantly without duplicate writes or wasted rows.

import { PerSQL } from "@persql/sdk";
const persql = new PerSQL({ token: process.env.PERSQL_TOKEN! });
const db = persql.database("acme/orders");
const planKey = "migrate-2026-01-15";
// Step 1 is cached after the first successful run.
await db.query(
"ALTER TABLE orders ADD COLUMN priority INTEGER DEFAULT 0",
[],
{ planKey, planStep: "1-add-priority" }
);
// Step 2 is cached after the first successful run.
await db.query(
"ALTER TABLE orders ADD COLUMN due_at INTEGER",
[],
{ planKey, planStep: "2-add-due-at" }
);
// Step 3 may fail on first invocation; retry replays steps 1-2 from cache.
await db.query(
"CREATE INDEX idx_orders_priority ON orders(priority)",
[],
{ planKey, planStep: "3-index-priority" }
);

Pass the same planKey on every agent run. The server stores step results keyed by planKey + planStep for 24 hours, so partial failures resume safely without client-side checkpoints.

Guard agent writes with propose