Gate agent writes with approval rules
Attach a require_approval rule to a table so an autonomous agent cannot UPDATE or DELETE without human review. When the agent hits the rule, the SDK throws ApprovalRequiredError with an approvalUrl and token; poll for approval, then redeem to apply the write.
import { PerSQL, ApprovalRequiredError } from "@persql/sdk";
const persql = new PerSQL({ token: process.env.PERSQL_TOKEN! });const db = persql.database("acme/orders");
// Gate all writes to orders behind human approvalawait db.approvalRules.create({ tableGlob: "orders", action: "require_approval", note: "Agent must get human approval before modifying orders",});
const tryWrite = async (sql: string, params: unknown[]) => { try { return await db.query(sql, params); } catch (err) { if (err instanceof ApprovalRequiredError) { const status = await db.approvals.poll(err.approvalToken); if (status.status === "approved") { return db.approvals.redeem(err.approvalToken); } throw new Error("Write denied or expired"); } throw err; }};
await tryWrite("UPDATE orders SET status = ? WHERE id = ?", ["shipped", 1]);Requires an admin-role token to create rules. The approvalUrl can be surfaced to a human reviewer in Slack or email. Poll intervals default to 2 seconds with a 10-minute timeout.