Skip to content

feat(concurrency): row versions on the bucket forms (REQ-RUNTIME-003 rule 4) - #43

Merged
HecFranco merged 3 commits into
mainfrom
feat/bucket-row-version
Nov 22, 2026
Merged

HecFranco merged 3 commits into
mainfrom
feat/bucket-row-version

Conversation

@HecFranco

Copy link
Copy Markdown
Contributor

Summary

Closes the REQ-RUNTIME-003 rule-4 gap (lost update) of the sweep-2026-11-22 audit: the bucket edit forms (Storage page and the Move page's new-bucket form) were last-write-wins. Owner decision 2026-11-22: fix it, one minor PR.

Design

  • Column: additive storage_kit_profile.row_version INT NOT NULL DEFAULT 1, mapped insertable: false, updatable: false on StorageProfile, so the ORM never writes it and it can never go backwards.
  • Token: the form gets a hidden, unmapped rowVersion field (StorageProfileType option row_version, default null = no field). Its value is read from the database (RowVersion::read()), never from the identity map, so a manager that kept a row across requests (worker mode, kernel reset false) cannot make a stale page look current. A missing or malformed token counts as 0, which never matches (versions start at 1).
  • Save: inside the existing write unit (StorageEntityManager::transactional(): transaction-doctrine's DoctrineTransactionRunner on the bundle's private / dedicated manager, owner lock), after the existing isCurrent() re-check, one atomic UPDATE storage_kit_profile SET row_version = row_version + 1 WHERE id = ? AND row_version = ?. It also holds the row lock until the commit, so a racing save waits and then matches no row. Only then is the entity changed and flushed.
  • Stale answer: StaleEditException (nothing written) → the form is rendered again with 409, a translated flash (settings.flash.stale / move.flash.stale, 7 locales) and X-Error-Code: STORAGE_KIT_STALE_EDIT. Deliberate deviation from ApiStudio #114: a stale page is a domain conflict, so it carries no X-Conflict-Code and no Retry-After. REQ-RUNTIME-003 rule 6 reserves that header for lock conflicts, and CONFLICT_001 would tell clients "retry is safe" for a request that can never succeed. This matches CookieConsent's RowVersionGuard. Lock conflicts keep CONFLICT_001 / CONFLICT_002 through StorageKitConflictListener.
  • Other writers: the form fields of a profile are written only by the Storage save and StorageMove::saveTarget(). saveTarget() gets an optional ?int $expectedVersion: null (host code) advances the version unchecked, so pages opened before are refused. State changes (switch / forget) are already covered by isCurrent(). The test-result DBAL writes (ProfileTester, Instance Settings listener) do not touch form fields and do not advance the version.
  • ZH-M1 (ApiStudio audit): no hand-rolled rollback loop. The unit is the transaction-doctrine runner (v1.9.1, ConnectionRepairer::afterFailure() rolls back to the entry level and closes the connection if that fails). A real deadlock test proves it.
  • Worker mode / DDD hosts: RowVersion is static, StaleEditException is a value; the controllers keep the token in locals. Only the bundle's own manager is used (private or dedicated); the host's manager is never touched.
  • Behaviour change: a first save whose page did not show the bucket (another admin created it meanwhile) is now stale instead of overwriting it (ConcurrencyRaceTest updated).

Tests

  • tests/Unit/Concurrency/RowVersionTest.php: compare-and-set, unchecked advance, missing row.
  • tests/Integration/Functional/StaleEditTest.php (SQLite + DB jobs): stale / missing / forged tokens ('', abc, -1, 1e3, a 20-digit value, absent) on the Storage page, the Move page, a first save, a bucket created meanwhile, and an unchecked save from code.
  • tests/Integration/Concurrency/RowVersionRaceTest.php (DB jobs, two connections):
    • two editors with the same version: one wins, the other gets a stale 409, and the stored row is the winner's;
    • a save waiting on an open claim gets 409 CONFLICT_001, and its retry is stale;
    • a real MySQL 8.4 / MariaDB 11.4 deadlock inside the edit's flush, against a second PHP process (InnoDB picks the save as the victim and the ORM savepoint is lost): 409 CONFLICT_001, nesting level 0, the next save works.

Local results:

  • SQLite full suite: 713 tests, 14 skipped. Coverage 100.00% (statements 4655/4655, elements 5335/5335); it was 4601/4601 before.
  • CI database file list: green on MySQL 8.4, MariaDB 11.4 and PostgreSQL 16 (DBAL 4). PostgreSQL skips the deadlock case: it is MySQL / MariaDB only.
  • Static checks: phpstan, php-cs-fixer, rector, igor (127 stateless), validate-translations and check-no-raw-html-form are clean.
  • DBAL 3: covered by the CI legs.

Front-end (Chrome Modern Web Guidance)

npx -y modern-web-guidance@latest search "forms" --skill-version 2026_11_10-47b4c80e, then retrieve forms. The checks:

  • §1 Semantic structure: the form is POST and every control has a name (storage_profile[rowVersion]).
  • §2 Labeling and state: errors are not shown by colour alone. The flash is text in role="alert", so it is announced.
  • §4 Validation: the message says how to fix it ("reload the page and apply them again"). Validation stays server-side, and the hidden token is never trusted for authorization.
  • §8 Security: the hidden field sits next to the CSRF token, and there is no inline JS.

Other guides listed by the search (agentic-forms, autofill-*, form-fields-automatically-fit-contents, brand-consistent-forms, sms-otp-form, form-associated-custom-elements) do not apply: there are no new visible controls.

Upgrade

ALTER TABLE storage_kit_profile ADD row_version INT DEFAULT 1 NOT NULL; (same on MySQL / MariaDB / PostgreSQL), deployed with the code. Custom templates that list fields one by one must render rowVersion (form_rest() does). See UPGRADING "Unreleased".

🤖 Generated with Claude Code

…rule 4)

The Storage page and the Move page new-bucket form were last-write-wins.
storage_kit_profile gets an additive row_version column (never written by
the ORM). Each form carries the version read from the database in a hidden
rowVersion field; the save advances it with one atomic compare-and-set
inside its existing write unit. A stale page writes nothing and answers a
domain 409 (X-Error-Code: STORAGE_KIT_STALE_EDIT) with a translated message
in the 7 locales. StorageMove::saveTarget() takes an optional expected
version and advances it unchecked without one.

Tests: two-connection races and a real MySQL/MariaDB deadlock inside the
flush (audit ZH-M1: 409 CONFLICT_001, nesting level 0, next save works).
@github-actions

Copy link
Copy Markdown

@coderabbitai review

… ZK-M1)

The hidden rowVersion field now carries <row id>:<row version>, and
submittedRowVersion($form, $profile) returns MISSING when the token names
another row, so a page opened before its bucket was replaced (target
discarded and saved again, or a Move switch) can never overwrite the new
row at an equal version. The version comes from the same load as the
values (ZK-L1). Docs say host ORM writes do not advance it (ZK-I3).
@HecFranco
HecFranco merged commit eebf045 into main Nov 22, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant