The reviewer gets tired before the agent does

the-reviewer-gets-tired-before-the-agent-does

The diff is green. The tests pass. Somewhere in the middle, a line now updates a row that used to be write-once. You scroll past it. You approve.

Review is a person reading, and people get tired. An agent can produce the next diff as quickly as the first. Which rules can you make the database check on every write?

Give retries a durable key

Try this in a disposable Postgres database:

CREATE TABLE sends (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  idempotency_key text NOT NULL UNIQUE,
  recipient text NOT NULL,
  status text NOT NULL CHECK (status IN ('queued', 'sent', 'failed'))
);

INSERT INTO sends (idempotency_key, recipient, status)
VALUES ('order-42-receipt', 'a@example.com', 'queued');

INSERT INTO sends (idempotency_key, recipient, status)
VALUES ('order-42-receipt', 'a@example.com', 'queued')
ON CONFLICT (idempotency_key) DO NOTHING;

SELECT count(*) FROM sends;

The count is one. Without the conflict clause, the second insert raises a unique-constraint violation. An insert with status ‘delivered’ raises a check-constraint violation. Both rules apply even if another caller skips the application validator.

For a real API, scope the key to the caller and operation. When a key already exists, check that the request matches the original before returning its result. A database constraint covers the local record. If a provider call times out, use provider idempotency or reconcile the ambiguous outcome before retrying.

Give history a separate write path

A send changes status. Its history can record each transition as a new row:

CREATE TABLE send_events (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  send_id bigint NOT NULL REFERENCES sends(id),
  event text NOT NULL,
  recorded_at timestamptz NOT NULL DEFAULT now()
);

CREATE FUNCTION forbid_event_change() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
  RAISE EXCEPTION 'send_events is append-only';
END;
$$;

CREATE TRIGGER send_events_append_only
BEFORE UPDATE OR DELETE ON send_events
FOR EACH ROW EXECUTE FUNCTION forbid_event_change();

INSERT INTO send_events (send_id, event)
SELECT id, 'queued' FROM sends WHERE idempotency_key = 'order-42-receipt';

Try updating or deleting that event. The trigger rejects the change. In production, keep the application role separate from the owner, grant only the needed permissions, and withhold TRUNCATE on the history table. Row-level update and delete triggers do not fire on TRUNCATE. Review migrations that can change constraints, triggers, or grants.

A test worth adding

Run two attempts with the same operation key and verify how many records survive. Then try mutating the history using the same role your app uses. You’ll learn which parts of the design are enforced today and which still depend on the next reviewer noticing.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
how-to-use-claude-code-with-deepseek,-kimi,-glm,-qwen,-and-minimax

How to use Claude Code with DeepSeek, Kimi, GLM, Qwen, and MiniMax

Next Post
8-levers-of-change:-how-to-make-change-stick-on-your-project

8 Levers of change: How to make change stick on your project

Related Posts