Skip to content
Visual explainer 3h 30m to learn 5 steps

Find subtle risks in AI-generated changes

Use repository evidence to find idempotency, concurrency, backward-compatibility and operational problems in generated code.

By Invicly Tech, , in Prompt Engineering and Vibe Coding. About 8 minutes to read.

Topic 26 of 29

In short

AI-generated code often passes the happy path and hides problems that only appear when a request is repeated, two users collide, or an older client calls the new code. You find these by asking sharp questions and backing each finding with evidence from the repository.

Think of it like this: It is like proofreading a contract: the sentences read fine, so you hunt for the clauses that fail when something unusual happens.

Explained simply

Generated code often works when you try it once. The trouble starts later, when a user double-clicks, two people act at the same moment, or an older app talks to the new server. These are subtle risks because a quick test does not show them.

You can find them by asking a few plain questions of any change. What happens if this runs twice? What happens if two of these run together? Can old callers still use it? Will I know if it fails? Each question has a clear way to check.

Checking means evidence, not feelings. Write a small test, read the exact lines, or run a query. A review note that says 'file X line 40 updates stock without a lock; test Y shows it oversells' is useful. 'Looks risky' is not.

You can ask the assistant to help, but treat its answer as a lead to verify. It can suggest where to look. You decide what is true by running the code.

Press Play on a diagram to walk through it one step at a time.

MapFour risk areas to review
Generated change
  • Idempotency
    • Retried POST creates duplicates?
    • Is there a unique key?
  • Concurrency
    • Read then write races?
    • Transaction or lock used?
  • Compatibility
    • Old clients still work?
    • Response fields removed?
  • Operations
    • Errors logged with context?
    • Secrets kept out of logs?
SequenceA lost update caused by a race
User A
User B
Database
  1. 1Read stock = 1
    User A to Database: Read stock = 1
  2. 2Read stock = 1
    User B to Database: Read stock = 1
  3. 3Write stock = 0
    User A to Database: Write stock = 0
  4. 4Write stock = 0 (oversold)
    User B to Database: Write stock = 0 (oversold)
CompareA weak finding and a strong finding
Weak
  • This might be a race
  • No file or line
  • No test
  • Cannot be checked
Strong
  • orders.ts line 40 reads then writes
  • Two parallel requests oversell
  • Test output attached
  • Fix and rerun shown

In depth

Idempotency of writes

An operation is idempotent if repeating it leaves the same final state. In HTTP, PUT and DELETE are defined as idempotent, and POST is not. Networks retry requests, and users click twice, so a non-idempotent write can create duplicates or charge twice. Common fixes: a client-sent idempotency key stored with a unique constraint, an upsert such as INSERT with ON CONFLICT in PostgreSQL, or a check for an existing record before creating. Ask the assistant: what happens when this endpoint is called twice with the same body?

  • Look for INSERT without a unique key.
  • Look for counters changed by +1 on retry.
  • Test by sending the same request twice.

Concurrency assumptions

A race happens when code reads a value, decides, then writes, while another request does the same between those steps. The result is a lost update. Fix it by moving the decision into the database: one UPDATE with a condition, a unique constraint, a row lock such as SELECT ... FOR UPDATE, or a stricter transaction isolation level. PostgreSQL's default, Read Committed, does not stop every race, so do not assume a transaction alone is enough. Test with two parallel requests and count the results.

FlowFrom suspicion to evidence on a race
  1. 1
    Spot read then write
  2. 2
    Write a parallel test
  3. 3
    Observe wrong count
  4. 4
    Make it atomic
  5. 5
    Rerun test and attach output

Backward compatibility

Old mobile apps, cached pages and other services keep calling your API after you release. Removing a field, renaming a parameter or tightening validation can break them. Keep fields you remove for a deprecation period, add new fields as optional, and run old test fixtures or recorded requests against the new code. Contract tests are a good fit. Ask: who calls this today, and which version of them?

  • Adding an optional field is usually safe.
  • Removing or renaming a field is a breaking change.
  • Run old fixtures against new code.

Logging, errors and evidence

Operational problems hide in empty catch blocks, vague messages and logs that leak secrets. Check that errors are logged once with context such as request id and the failing input type, that failures return the right status code, and that tokens and personal data never reach logs. Then write every finding in the same shape: what, where, how you proved it, suggested fix. This keeps the review fair and lets the next person check it.

Learn it in 5 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Check idempotency of writes~40m
  2. 2
    Question concurrency assumptions~40m
  3. 3
    Test backward compatibility~40m
  4. 4
    Review logging and error handling~40m
  5. 5
    Back each finding with evidence~40m

Key points

  • An idempotent write gives the same result when repeated. PUT is defined as idempotent in HTTP, and POST is not, so a retried POST needs a unique key or a check.
  • Look for read-then-write code. Fix it with a single atomic statement, a unique constraint, or a transaction at the right isolation level.
  • Test backward compatibility by running the old client or old test fixtures against the new code.
  • Check that failures are logged with enough context, that errors are not swallowed, and that no secrets or personal data reach the logs.
  • Every review finding needs evidence: a file and line, a failing test, a log line or a query result.

Key terms

Tap a card to flip it. 0/5 known

Examples

Make a retried order safe

A generated endpoint inserts an order on every call. A mobile client retries after a timeout and the customer gets two orders. You add a unique key sent by the client and let the database reject the copy.

sql
ALTER TABLE orders ADD COLUMN idempotency_key text;
CREATE UNIQUE INDEX orders_idem ON orders (idempotency_key);

INSERT INTO orders (idempotency_key, total)
VALUES ('key-123', 40)
ON CONFLICT (idempotency_key) DO NOTHING;

Fix a lost update on stock

The code reads stock, checks it is above zero, then writes stock minus one. Two buyers both pass the check. You replace it with one conditional update and check how many rows changed.

sql
UPDATE products
SET stock = stock - 1
WHERE id = 7 AND stock > 0;
-- affected rows = 1: sold
-- affected rows = 0: sold out

Quick reference

  • Ask: what happens if this runs twice, together, for an old client, or fails?
  • PUT and DELETE are idempotent in HTTP. POST is not by default.
  • Use a unique constraint plus ON CONFLICT for safe retries in PostgreSQL.
  • Make check-and-change one atomic statement where you can.
  • Removing or renaming a response field is a breaking change.
  • Never log tokens, passwords or full personal data.
  • Every finding: what, where, proof, fix.

Common mistakes

  • Writing findings such as 'this looks risky' with no evidence. Reproduce it or point to the line.
  • Assuming a transaction alone prevents lost updates. The default isolation level may still allow them.
  • Reviewing only the new files and missing a changed function signature that old callers use.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Which HTTP method is not idempotent by default?

Question 2

2What is the best fix for a read-then-write race on stock?

Question 3

3Which change is usually backward compatible for API clients?

Question 4

4What makes a review finding useful?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Send the same create request twice to a local endpoint and note how many rows exist afterwards.

  2. Exercise 2medium

    Write a parallel test that exposes a lost update on a counter, then fix it with one atomic statement.

  3. Exercise 3hard

    Review a generated diff for compatibility and operations. Produce three findings, each with evidence.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Is a database transaction enough to stop race conditions?
Not always. PostgreSQL defaults to Read Committed, which still allows some races. Use atomic statements, constraints, locks or a stricter isolation level.
Can I ask the AI to find these risks for me?
Yes, as a source of leads. Always verify each one with a test or a line reference before you trust it.
How do I know who still calls my old API?
Check access logs, client versions and contract tests, and keep deprecated fields for a stated period.

Go deeper

ShareXLinkedInWhatsApp