跪拜 Guibai
← All articles
Frontend · JavaScript · AI

Why One Changed Requirement Broke the AI-Written List Page

By kyriewen ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

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.

Summary

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.

Takeaways
— Equating "all current-page rows selected" with `selectedIds.length === rows.length` breaks the moment pagination splits the visible page from the selected set, and even returns true for a 0 === 0 empty list.
— Selection should be stored as a set of record IDs; the current page is a separate range, and batch counts and submit parameters read from the ID set, not from the page's rows.
— Derived values such as selectedCount, allChecked, and canSubmit should be computed from selectedIds and rows instead of stored as individual state that every handler must re-sync.
— Pagination and filter changes affect the selection differently: page turns keep the selection, while a filter change should reset the page to one and clear the selection, done in one event entry.
— Assign each fetch an ignore flag in the effect cleanup, and tag results with a request key, so only the current query's response drives rendering and late responses cannot overwrite newer ones.
— Verify a changed requirement by writing an operation sequence (select on page one, turn, select on page two, switch filter, submit) rather than by listing features.
— A change note to the AI should explicitly state which old behaviors change and which must survive: keep IDs across pages, select-all only the current page, clear on filter change, submit all selected IDs.
— Supporting "select all records matching the filter" requires a different design than an ID set, such as query conditions plus excluded IDs with a server endpoint.
— Ignore flags do not cancel network transfers; AbortSignal or an existing request library's own cancellation and state mechanisms handle that.
— Discovering a bug in one filter control is not a reason to patch a clear there; any condition that changes the result set should follow the same reset rules.
Conclusions

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.

Concepts & terms
Redundant state
Storing the same fact in several state variables, such as selectedIds plus separate selectedCount, allChecked, and canSubmit. Every event handler must manually sync them, so one missed update produces UI values that disagree with the actual data.
Stale response
An earlier request whose response arrives after a newer one and overwrites current data. It is guarded by an ignore flag set in the effect cleanup, or by tagging each result with a key and rendering only the result matching the current query.
Indeterminate checkbox
The mixed state of a parent checkbox when some but not all rows on the current page are selected. It is derived by comparing the set of selected IDs against the current page's rows rather than by comparing array lengths.
From the discussion

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.

— Empty-list select-all is a hidden trap: 0==0 means an empty list is all-selected, and only cross-page behavior exposes it.
— Maintaining selectedCount as separate state invites drift; deriving it from selectedIds.length avoids the mismatch between the button count and the submitted IDs.
— Conventions should be fixed in the repo rules read by the AI from the start, not in a one-off requirement ticket, or the AI will re-guess when someone else changes the list later.
— A remaining question is whether the request out-of-order fix needs AbortSignal in the rules or whether requestKey with ignore is sufficient.
Featured comments
组合搭配函数

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?

kyriewen

Inspired.

shushenghong

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.

kyriewen

Approved.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗