Framework RecipesImplementation, failure path, check

Persist a Hono notification record before post-response dispatch

waitUntil extends execution after a response; it does not wait for an unrelated database transaction to commit. Persist order and outbox together, await the transaction, and only then schedule a delivery attempt. A durable worker must still discover undelivered rows if the request process dies.

Save this requirement in Perch and check it after each agent edit. The rule gives your agent a named check to run, a finding to repair, and the same command to recheck.

Implement the Hono boundary

Save this illustrative integration module as confirm.ts.

import type { Context } from "hono";

type Transaction = { confirmOrder: (actorId:string,id:string)=>Promise<void>; insertOutbox:(key:string,id:string)=>Promise<void> };
type Database = { transaction:<T>(run:(tx:Transaction)=>Promise<T>)=>Promise<T> };

export async function confirm(c: Context, db: Database, actorId: string, orderId: string,
  dispatchCommittedOutbox: () => Promise<void>) {
  await db.transaction(async tx => {
    await tx.confirmOrder(actorId, orderId);
    await tx.insertOutbox(`confirmed:${orderId}`, orderId);
  });
  c.executionCtx.waitUntil(dispatchCommittedOutbox());
  return c.json({status:"confirmed"});
}

The order, inventory, or resource-lifetime scenario is adapted from Perch’s public order-service source at commit 4af67af. This Hono implementation is an illustrative adaptation.

Integrate with your application

This Cloudflare-runtime service uses an explicit Database adapter contract: transaction resolves only after commit and rejects on rollback; tx.confirmOrder checks ownership and state; tx.insertOutbox inserts a unique key in the same transaction. actorId must come from verified middleware, never the body. The request-triggered dispatcher is an optimization; an independent retrying outbox worker supplies durable delivery.

Exercise the boundary and failure paths

Scenario Expected outcome
Transaction completes Dispatch starts after commit
Transaction rejects No dispatch scheduled, no success response
Request process dies after commit Worker finds persisted outbox row
Dispatcher rejects Delivery failure recorded for retry

Add regression tests for these cases in your existing Hono test project. Use separate connections or deliberately reordered completion where the case involves concurrency; a sequential happy-path test does not exercise that failure. Run the test command after creating the matching test file or suite:

npx vitest run tests/confirm-order.test.ts

A focused test to adapt to the paths and test dependencies in your project:

import { expect, test } from "vitest";
import { confirm } from "../confirm";
test("dispatch waits for commit", async () => {
  let release!: () => void;
  const gate = new Promise<void>(resolve => { release = resolve; });
  let dispatched = false;
  let persisted = false;
  const db = { transaction: async (run: any) => { await run({confirmOrder:async()=>{},insertOutbox:async()=>{persisted=true;}}); await gate; } };
  const pending: Promise<unknown>[] = [];
  const c = { executionCtx: {waitUntil:(promise:Promise<unknown>)=>pending.push(promise)}, json:()=>({status:200}) };
  const result = confirm(c as any, db as any, "owner", "order", async()=>{expect(persisted).toBe(true);dispatched=true;});
  await new Promise(resolve=>setTimeout(resolve,0));
  expect(dispatched).toBe(false);
  release(); await result; await Promise.all(pending);
  expect(dispatched).toBe(true);
  expect(persisted).toBe(true);
});

Introduce the specific agent regression

In a throwaway copy, replace this source span:

await tx.insertOutbox(`confirmed:${orderId}`, orderId);

with:

// The notification record was accidentally removed.

Run the relevant negative test and the Perch command on that broken copy. Restore the corrected source before committing the application. The supplied mutation reproduces the exact regression for the rule check.

Preserve the requirement as a Perch rule

Save this in .perch/rules/perch-guide-hono-waituntil-after-persistence.yaml at the repository root, keeping your existing perch.yaml:

- name: persist-notification-before-dispatch
  where: "confirm.ts"
  ensure: >-
    The confirm function persists a durable notification record by calling tx.insertOutbox inside the same db.transaction callback as tx.confirmOrder, before scheduling dispatchCommittedOutbox with waitUntil. Confirming the order and scheduling dispatch without inserting an outbox record violates this requirement.

Install and authenticate the CLI once, then check the current working file:

npm install -g @lakeday/perch
perch login
perch check confirm.ts --rules persist-notification-before-dispatch --json

Use Node 22 or newer. The framework can use another language; Perch’s CLI remains a Node command. A gated finding exits 3. Exits 1 or 2 mean the check did not complete and must be reported as a blocker.

Inspect the captured rule check

On 2026-10-14, Perch using jev-latest checked the exact fixed and broken source provided here. The capture records SHA-256 hashes of both sources and the requirement.

Source control Exit Outcome
Broken 3 Rule finding
Corrected 0 No rule finding

The rule distinguished these controls in this run. Raw Perch capture · Exact sources and mutation.

Install the rule pack and configure your agent

Download this rule pack ZIP · Download the YAML rules · Agent task. The pack contains the rule, its example paths and the repair/recheck instructions. Review the where selector before using it in an existing project. Keep your root perch.yaml; install this rule as a separate file under .perch/rules/.

  1. Install Perch using Node 22 or newer and sign in once:
npm install -g @lakeday/perch
perch login
  1. Download the pack, inspect it, and extract it from your repository root. The extraction preserves any existing files:
curl -fL 'https://frameworkrecipes.dev/hono/waituntil-after-persistence/rule-pack.zip' -o 'perch-guide-hono-waituntil-after-persistence.zip'
unzip -n 'perch-guide-hono-waituntil-after-persistence.zip'

The installed rule is .perch/rules/perch-guide-hono-waituntil-after-persistence.yaml. Its selector matches confirm.ts in this example; adapt the selector to your application file before checking an existing project.

  1. Configure the coding agent you use. Run one of these commands:

Claude Code:

perch setup claude-code

Codex:

perch setup codex

Cursor:

perch setup cursor

Pi:

perch setup pi
  1. Ask the agent to edit the implementation and check the saved requirement:

Use the installed Perch skill while editing confirm.ts. Preserve persist-notification-before-dispatch. Run perch check confirm.ts --rules persist-notification-before-dispatch --json after the edit. Read each finding and the affected code, fix the implementation, and run the identical check again after the fix. Keep the rule and regression tests intact. Run the application's relevant regression tests and report the commands and actual results. Exit 3 means a rule finding; exit 1 or 2 means the check did not complete. Report an unavailable or incomplete check as blocked. Do not claim the application tests ran if only the Perch check ran.

  1. After repairing a finding, run the same check again and inspect the result:
perch check confirm.ts --rules persist-notification-before-dispatch --json

Commit the downloaded rule and the installed agent configuration with your application. The rule file adds no guarantee of correctness; keep the regression tests from this guide.

With the base reference fetched, use perch scan --since origin/main --filter type=lint in CI.

Framework references and verification scope

The dated capture records the source check shown above. Implement the adapters, schema and regression tests described in this guide in your application. Unchanged requests reuse cached answers; fresh model judgments can vary. Model analysis sends relevant source to the configured service.