Skip to content

fix(platform): keep a governance switch from reverting a newer save - #4057

Merged
yannickmonney merged 5 commits into
mainfrom
fix/governance-toggle-stale-write
Oct 1, 2026
Merged

yannickmonney merged 5 commits into
mainfrom
fix/governance-toggle-stale-write

Conversation

@yannickmonney

@yannickmonney yannickmonney commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

What changed

A governance policy write carries the whole file, built from the config its caller last read. The header switch (useGovernancePolicyToggle) and the section's Save each wrote that file on their own, and the network decided which write landed last. A slow switch write therefore landed after a Save made while it was pending, and silently put the old settings back (#4048).

  • Writes of one policy go out in order (app/lib/backend/policy-write-order.ts, used by the governance/file_actions:saveGovernancePolicy adapter row). One policy's writes from a tab leave one at a time, in the order they were made, each once the one before it has settled. A failed write doesn't hold back the next one, and other policies don't wait. A Save made while the switch's write is in flight now waits for it, and its header Save shows busy meanwhile.
  • The switch waits while its policy is settling. The hook's isToggling becomes isSettling. It is true while any write of the policy from this tab is in flight, and until the policy's read is no longer being fetched after a write. The second part counts a refetch that a live hint replaced mid-flight. So the switch never builds its write from a config a newer write replaced. That closes two more gaps of the same kind, found in the sweep:
    • After a Save: the write resolved before its re-read. A switch flipped in that window wrote the pre-Save config back. A slow network widens this window exactly as it widens the reported race.
    • Rule tables: default models, budgets, feature flags and conversation routing had no guard on their switch while an instant rule save was pending. Only the six form editors had editor.isSaving.
  • All ten consumers pass isSettling to their switch (a mechanical rename). editor.isSaving stays on the form editors' switches, so the reverse-order guard is untouched.
  • Manual layer: GOV-B20 (a throttled-network check) plus its pin and its automation row. Governance goes from 79 to 80 boxes, 1067 in all, counted with tools/lint-manual's parser.

No new strings, no backend change and no API change. expectedHash stays unused. In the reported order the switch's stale write had in effect been applied by the Save, so refusing it would have told the admin something false.

How I verified it

Regression tests, each failing on d6dd53ae0:

  • app/features/settings/governance/hooks/use-governance-policy-toggle.race.test.tsx runs the real hook, react-query and adapter row. fetch stands in for a server that applies each POST as it arrives. The cases:

    • a held toggle, then the editor's save: the final file is the Save's;
    • a pending save holds the switch;
    • the re-read after a save holds the switch, including when a hint replaces the fetch, and the next toggle builds from the fresh config;
    • a failed toggle doesn't hold back the Save.

    On d6dd53ae0 all 4 fail: "expected 2 to be 1" (the Save overtook the held write) and "expected false to be true" (with isToggling read in place of isSettling).

  • app/lib/backend/settings-governance.test.ts adds 5 adapter cases: order, failure, another policy, settling until the read is idle (with a hint replacing the fetch), and no hold for an unread policy.

Local checks on these files:

  • Vitest: hooks 13/13. Adapter file 14/14. The ten editors' suites plus the error-toast and skeleton conventions: 12 files, 278 tests. Guards single-failure-toast, error-message-description and integration-scope: all pass.
  • Type-aware oxlint: clean. oxfmt: clean.
  • Scoped tsc over the changed files: only 3 errors, in conversation-routing-policy-editor.tsx at lines this PR doesn't touch (the HistoryState augmentation in app/router.tsx). They are identical with the same config on d6dd53ae0.
  • lint:manual and check-guide.ts on the governance suite: OK.

Real browser. Local stack only, with its own Postgres and synthetic orgs. Headless Chromium 1208 at 1280×900. Playwright held one request 6 s, then passed it on unchanged. Write order is from app.audit_logs.

Run d6dd53ae0 this branch (86e1f9be9)
R1: session idle timeout, held switch-on, then Save 15 Save 15 lands, then the switch write: 30 at rest, no toast switch write, then Save: 15 at rest; Save busy while it waits
E8: standard agent, held switch-on, then Save new instructions "Saved v4" put back new instructions kept
R2: Save 15, every policy re-read held, then switch off switch enabled; writes {false, 30} switch disabled until the read lands; then {false, 15}
C1: held Save, then the switch (control) switch disabled; Save lands same

CI

  • Exact head 6e9d795b5a8232783df62bb21027372084e98bda: 49 checks pass, 0 fail.
    • Checks 36923926726:
      • Unit executed the platform server suite, 995 files with ytdlp_live included, 25/25 tasks.
      • UI executed test:ui: 564 files, the race test among them, with no act warning.
      • Type check and Lint executed the changed tasks.
      • Browser replayed a cached verdict for identical inputs.
    • E2E 36923926494: all 16 platform shards passed with no retry or diagnostics notice. One test is skipped by the suite in shard 14.
    • Build 36923926636, SAST 36923926561 and Commitlint 36923926466 passed.
    • Skipped by design (13):
      • Backend integration, out of scope because the change is frontend only;
      • the AI gateway, Docs, UI docs and Web container tests;
      • Smoke test and Validate images, which are fork-PR jobs;
      • Trivy;
      • the Candidate gate, five times.
  • Earlier heads failed on CI infrastructure, not on this change.
    • At 790dac2e1 and 2cba748c5, Unit failed only in backend/core/video_links/ytdlp_live.test.ts: its beforeAll timed out at 600 s while the video toolchain was installing from apt.
    • At 2cba748c5, Playwright 6/16 also timed out after 12 min installing Chromium's apt dependencies, before any test ran.
    • In the same window, fix(platform): recheck task access before notification delivery #4056 failed Unit identically (run 36916974313), and main 36881739455 had passed that suite.
    • The token can't re-run jobs, so the empty chore: commits 09541ca7e (green) and 6e9d795b5 (green) re-triggered the checks.
  • 2cba748c5 is test only: two plain waits in the race test now run inside act(). This removes an "update … not wrapped in act" warning that printed only in CI.

Not in this PR

  • While the switch's write is pending, Standard agent can still say "The standard agent is switched off." beside a switch that is on, because its availability read is stale. That is a display issue with no data loss. I left it out to keep this fix to the write order.
  • Writes from two tabs or sessions are still last-writer-wins, for Saves as well as switches. That is the general lost-update case, not this one.

Closes #4048

A governance policy write carries the whole file, built from the config
its caller last read. The header switch and the section's Save each wrote
the file on their own, and the network decided which write landed last.
A slow switch write could arrive after a Save made while it was pending
and silently put the previous settings back (#4048).

- One policy's writes from a tab now go out one at a time, in the order
  they were made, each once the one before it has settled.
- The switch's `isToggling` becomes `isSettling`. It stays true while any
  write of the policy is in flight, and until the policy's read is no
  longer being fetched after a write, even when a live hint replaces that
  fetch. So the switch never builds its write from a config that a newer
  write replaced. This covers a Save, the rule tables' instant saves
  (whose switches had no such guard) and the read right after either.

All ten editors on useGovernancePolicyToggle get this through the shared
adapter and hook.
GOV-B20 throttles the network, saves the session idle timeout beside its
switch's pending write, and checks that the Save lands and that the switch
waits for the read-back after it (#4048). Its pin and its automated half
are recorded, and the governance count goes from 79 to 80 boxes (1067 in
all).
@yannickmonney

Copy link
Copy Markdown
Contributor Author

Independent review: accepted at 6e9d795b5a8232783df62bb21027372084e98bda

  • Reviewer: Tale agent update dashboard ui (#508) #2 3f9fdcee-79e7-430b-a430-ba179d5b5132, run 976bf361-8ed2-4e8f-83cf-3c2d52a59c81 (TALE-359 review-merge occurrence, 2026-10-02 00:45 Europe/Zurich).
  • Implementation: agent add --chat-max-width CSS variable #3 4aec6e61-0328-4213-9719-02d387dee65d, run b56b0af1-939e-4a2b-9c7f-3dabe0e7b765. The reviewer is distinct.
  • Tree: ce38baa3174bc7f81f59b0f669668bcd1f641f5a. The merge base is current main d6dd53ae0, so a squash lands exactly this tree. This PR shares no file with fix(platform): recheck task access before notification delivery #4056; the two merge cleanly together.
  • A read-only review agent covered the write-order module in parallel. I re-ran its proofs before listing them.

Verdict: the fix holds for #4048 and for the two gaps found in the sweep: a Save made while the switch's write is pending now lands last, and the switch waits for the re-read.

Local checks on the exact head

  • Vitest: the governance feature plus the backend settings and adapter tests pass, 47 files and 498 tests, with no act() warning. settings-governance.test.ts passes 14/14.
  • Negative controls: each surgical revert of the fix is caught.
    • Without the write ordering in the adapter, the race test fails 3/4 and the adapter test 2/14.
    • With the switch not waiting for settling, the race test fails 3/4.
    • Without the hold until the re-read, the race test fails 1/4 and the adapter test 1/14.
  • Real Chromium (playwright 1.58.2), with the real hook, adapter row, react-query and @tale/ui Switch:
    • With the switch's write held, a Save stayed queued (1 POST).
    • After release the server applied {true,30} then {true,45}, so the Save landed last.
    • The switch stayed disabled until the re-read landed, then re-enabled.
    • axe (WCAG 2.1 A/AA): 0 violations.
  • Manual tests: lint:manual OK. The recount from the suites gives 1067 boxes across 21 suites, governance 80, with GOV-B20 plus its pin and automation row.

CI at the exact head (merge ref onto d6dd53ae0)

  • All five workflows succeeded: Checks 36923926726, Build 36923926636, E2E 36923926494, SAST 36923926561 and Commitlint 36923926466.
  • 49 checks succeeded and Trivy is neutral. 12 were skipped by design:
    • Backend integration, out of the integration scope for a frontend-only change;
    • the AI gateway, Docs, UI docs and Web container tests (path filter);
    • the 2 fork-PR jobs;
    • 5× Candidate gate.
  • Ran fresh: platform test (995 files, 80,887 passed), test:ui (564 files, 5,084), typecheck and lint.
  • Replayed: the platform browser task, hash ea485ab20. It executed with identical inputs in job 110569028185 at 2cba748c5, and passed (43 files).

Non-blocking (P3), for follow-up

  1. A failed re-read releases the hold. policy-write-order.ts:86-95 ends the hold when the read stops fetching, error included. The cache still holds the pre-write config, so the next switch flip can write it back.
    • Proved with the module and real query-core: after a Save of 15, a failing re-read, then switch off, the server ends at {enabled:false, minutes:30}.
    • main loses the Save in the common path, so this narrows the bug rather than adding one.
    • The same claim also depends on a fresh read. The policy GET serves a 15 s per-process cache for 8 of the 10 policy types, so a re-read on another replica can be stale. That is pre-existing.
    • Holding until a successful post-write read, or sending the server's expectedHash, would close both.
  2. A request that never answers blocks the policy's later writes until reload. backendFetch has no timeout, so the Save waits behind the hung write with Save busy, and the switch stays settling. Proved. On main the Save went out on its own. A timeout on the write, or a bounded wait in the queue, would bound it.
  3. The same class of bug, in an editor this PR doesn't touch. two-factor-policy-editor.tsx:101-112 builds its enforcement switch's write from the last-read config, and disables the switch only during its own write, not the re-read. usePolicySettling would cover it. Pre-existing; reasoned.
  4. Rule tables can briefly drop the newest rule. Budgets, feature flags and default models reset their list from every read. With the queue, the re-read after rule A can return before rule B's write lands. Reasoned; still an improvement on main.
  5. Test hygiene. settings-governance.test.ts:296-299 leaves an org-a/budgets write that never resolves in the module's state. No later test writes that key today.
  6. Keyboard focus is lost on toggle, as on main. Toggling with Space disables the focused switch, so focus drops to the body; this was already the case with isToggling. It is now disabled for longer, through the re-read. aria-disabled with ignored input would keep focus.

Also documented by the author and left open: cross-tab last-writer-wins, and Standard agent's stale "switched off" line while the switch's write is pending.

Not covered: no production contact. I didn't drive the other eight editors in a browser; the shared adapter, the hook tests and the agent's reading cover them.

@yannickmonney
yannickmonney merged commit 0554354 into main Oct 1, 2026
@yannickmonney
yannickmonney deleted the fix/governance-toggle-stale-write branch October 1, 2026 23:13
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.

bug(platform): a slow governance switch write reverts a newer Save of the same policy

1 participant