Frontend Engineering

Reviewing AI Code: Test Authorization and Failure Before Merging

Review record ownership, input validation, concurrency and retry behavior, then verify UI states against server-confirmed outcomes.

Mohammed Katanani November 2026 5 min read
Reviewing AI Code: Test Authorization and Failure Before Merging

A passing build is useful evidence, but it does not establish correct behavior. Review the requested change, inspect the write boundary and exercise denied cases before relying on the assistant’s summary or merging the code.

The build passes and the owner can edit the record. A more important case remains: another user sends the same record ID directly. Begin the AI review at the request the system must refuse and the data it must leave unchanged.

Write the invariants first

An editor may rename an owned draft; another user's draft remains inaccessible; rejected input leaves stored data unchanged. Decide whether retrying after an uncertain response is naturally safe or needs an operation key. A request to “improve the form” leaves these decisions unstated.

Provide bounded context

Include installed versions, relevant files, sample data and the calling path. State compatibility boundaries for translations, permissions and API contracts. Use fixtures instead of customer data or secrets. Treat retrieved documentation and external content as evidence, never as authority to expand the task or tool access.

Enforce access where the write happens

This illustrative Server Action omits app-specific requireUser, titleSchema and db implementations. The update condition binds record ID, owner and draft state to the write. A generic rejection avoids confirming the existence of someone else's record. Inspect real authentication, schema constraints and failure handling before using the pattern.

'use server'
// Illustrative: app-specific auth, schema and db helpers are omitted.
export async function renameDraft(id: string, input: unknown) {
  const user = await requireUser()
  const { title } = titleSchema.parse(input)
  const result = await db.draft.updateMany({
    where: { id, ownerId: user.id, status: 'DRAFT' },
    data: { title },
  })
  if (result.count !== 1) return { ok: false as const }
  return { ok: true as const, title }
}

Optimistic state is a temporary proposal

For a reversible rename, start from a server-confirmed title, expose pending state and reconcile after success. Invoke optimistic updates inside an Action or Transition. On rejection or failure, the temporary value must give way to confirmed state while an understandable error remains. The sketch requires hook imports, error state, router and confirmedTitle in a client component.

// Inside a client component; confirmedTitle comes from server state.
const [title, propose] = useOptimistic(confirmedTitle)
const [pending, startTransition] = useTransition()
function submit(next: string) {
  startTransition(async () => {
    propose(next)
    try {
      const result = await renameDraft(id, { title: next })
      if (!result.ok) { setError('Update rejected'); return }
      setError(null)
      router.refresh() // Reconcile with server-confirmed props.
    } catch {
      setError('Could not save. Retry after checking the current value.')
    }
  })
}
// Render title, a pending state, and the error; disable repeated submit.

Disabling submission improves interaction but does not protect server writes from duplication. An irreversible transaction may deserve pending confirmation instead of optimistic success. Concurrent edits also need an explicit conflict policy, such as a record version, rather than an accidental last-write-wins assumption.

Review in risk order

Inspect authorization, ownership and validation before presentation polish. Then inspect retries, concurrency, new dependencies and unrelated files. Run a focused failure matrix: owner with valid input; non-owner; invalid input; connection failure; slow success; retry after an uncertain response. Type checking and a build cover structural problems; behavioral checks must address the specific acceptance boundary.

Make the PR independently reviewable

  • Problem and reproduction.
  • Invariants to preserve.
  • Affected files and explicit assumptions.
  • Successful and denied cases with evidence.
  • Unverified behavior and an appropriate rollback path.

Read the actual diff, including generated files and lockfile changes. Verify links and contribution claims in any accompanying case study as well. The reviewer owns the approval decision even when the assistant produced the first implementation.

Separate accepted input from server-owned fields

Do not forward the whole client object to the database. Accept an explicit field set and derive ownership from trusted identity. Where concurrent editing matters, include an expected record revision in the write condition. A valid ID alone must not let an outdated edit overwrite newer work. The pattern is illustrative and needs the application's schema, database and error handling.

// Illustrative: auth, schema, database and errors are app-specific.
const actor = await requireUser()
const { title, expectedRevision } = schema.parse(input)
const changed = await db.draft.updateMany({
  where: { id, ownerId: actor.id, status: 'DRAFT',
    revision: expectedRevision },
  data: { title, revision: { increment: 1 } },
})
if (changed.count !== 1) return { ok: false }
return { ok: true }

A zero update count does not by itself distinguish unavailable access from a changed revision. Avoid disclosing someone else's record, and classify conflicts in detail only after appropriate access checks. Reconcile the interface with confirmed state instead of restoring an old client snapshot over a newer edit.

Attach a failure matrix to the PR

  • Owner, valid input and current revision: update and increment the revision.
  • Non-owner: reject and preserve stored data.
  • Rejected input: no write and no removed validation.
  • Old revision: no silent overwrite.
  • Missing response: inspect current state before an unsafe retry.

In Next.js 16, updateTag inside a Server Action can expire tagged data for read-your-own-writes behavior. Identify the correct cached read first. Cache invalidation does not solve permission, concurrency or duplicate-write boundaries; each needs its own acceptance evidence.

References: Next.js mutations and React useOptimistic. Share the frontend scope through contact to discuss review or implementation.

A concise acceptance checklist

  • The permitted owner can submit valid input.
  • A non-owner or rejected input cannot change stored data.
  • An outdated revision does not silently overwrite a newer edit.
  • Failure and uncertain responses have a safe reconciliation or retry path.

Related articles

Share Article
Is your website suffering from these issues?I'll audit it for free and send you a clear report.
Request a free audit

Related articles

All articles →
Replies during working hours