Why One Changed Requirement Broke the AI-Written List Page
A page that passes its first AI-written demo can still ship a select-all that lies, batch buttons holding stale IDs, and responses overwriting newer results, because vague requirement text forces the code to guess the convention. The rework lands on whoever maintains the list unless the behavioral rules are written down before the next requirement change.
A list page with search, pagination, checkboxes, and batch actions looks fine when clicked through with static data. Add the requirement "keep selection across page turns, clear it when the filter changes" and the page starts lying: the select-all box on page two glows with nothing selected, the batch button carries records from an old query, and a slow response overwrites newer results. The first version proved each feature works; the requirement change is what proves whether they work together.
The failure comes from three habits. The check `selectedIds.length === rows.length` treats equal array lengths as "all rows on this page are selected", which silently breaks once the page and the selection are different scopes. Derivable values like selectedCount, allChecked, and canSubmit are stored as separate state, so every event handler must manually keep them in sync, and one missed call produces a mismatch between what the button shows and what gets submitted. Filter changes update only the keyword, leaving page and selection frozen in the previous query's world.
The fix is a protocol. Keep only selectedIds as state; compute everything else from it and the current page's rows. Group query conditions into one object and reset page plus selection at a single event entry. Guard the fetch with an ignore flag and a request key so only the current query's response can drive the list. Write out the operation sequences as acceptance cases, and hand the AI a change note that states which old behaviors change and which must stay.
The `selectedIds.length === rows.length` check encodes a hidden assumption that the displayed set equals the selected set; the moment pagination splits those two scopes, the bug is the symptom, not the cause.
Manually re-syncing duplicated state at every event handler produces silent mismatches between the UI label and the submitted payload, a failure mode that survives most demos because demos rarely click the exact long sequence.
Model-generated code reproduces the ambiguity of the prompt: where the requirement is vague, the model silently picks one interpretation, and the next requirement change then fights the previous guess.
The drift between "the page can display" and "the requirement can keep evolving" is the real cost driver, and the article's whole argument is that a written behavioral protocol, not better code generation, closes that gap.
The root problem is duplicated state and implicit assumptions. An empty list passes the select-all check because 0==0, and a separately stored selectedCount can drift out of sync with the actual IDs. The solution is to derive from a single source and to pin conventions like 'keep selection on pagination, clear on filter change' in the repo rules for AI. There is a split on whether the rules should also mandate AbortSignal or whether requestKey plus ignore already covers request out-of-order.
The 0==0 example for the select-all checkbox is painfully real: an empty list counts as all-selected, so a single-page demo never catches it, and the flaw shows the moment you switch pages. On the section about storing the same fact in multiple places, keeping selectedCount in its own state and syncing it in every event — miss one place and the button shows 3 items while submission sends only 2 IDs. Deriving it from selectedIds.length is indeed the least invasive fix. I agree with the comment above about writing the convention into the repo rules. Along that line, I'd like to ask: for the request out-of-order section that uses requestKey comparison, would you also require hooking up AbortSignal in the rules, or is ignore plus key already enough?
Inspired.
The phrase 'where the requirement is unclear, the code will still give an answer' is spot on. Our habit is to write conventions like 'keep selection on pagination, clear it when filters change' into the repo rules that the AI reads from the start, not just into this one requirement ticket. Otherwise, two months later when someone else has the AI modify this list, it will guess again based on its own understanding.
Approved.