Skip to content

fix(platform): arbitrate concurrent knowledge entry writes - #4225

Merged
yannickmonney merged 2 commits into
mainfrom
fix/knowledge-entry-write-races
Oct 4, 2026
Merged

yannickmonney merged 2 commits into
mainfrom
fix/knowledge-entry-write-races

Conversation

@yannickmonney

@yannickmonney yannickmonney commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

What changed

  • Serialize creation and rename destinations by tenant-scoped normalized-topic PostgreSQL transaction advisory locks, before the transactional duplicate lookup.
  • Lock the addressed revision with FOR UPDATE OF ke, so stale concurrent corrections/renames re-read its committed superseded state and receive the normal 409 rather than creating another current successor.
  • Preserve uploads outside the transaction, sequential idempotent updates, document identity/rotation, topic renames and history.
  • Add controlled real-transaction regressions and register them in Backend integration CI; add unit assertions for lock scope/order and record the automated manual-layer coverage.

Verification

Source head: 513e1faf5150847fc78ec50ce5da47a56d29e8eb (base 9b7463b707c5e44349f0a8c51e764a39e3e5c4fb).

Owned throwaway PostgreSQL 15.18: cluster tale-147-pg, database tale147_races, loopback port 55473. Original service reproduced all four races (four expected failures, two committing writers). Fixed service passes normalized-topic create, stale corrections, stale renames to distinct destinations and rename/create destination contention. Both actual prechecks complete before the controlled transaction interleaving; PostgreSQL lock waiting is observed through pg_blocking_pids.

Scoped server command, from services/platform:

TALE_KNOWLEDGE_RACE_DATABASE_URL=postgres://[email protected]:55473/tale147_races \
bunx vitest run --project server --maxWorkers 1 \
  backend/domains/knowledge_entries/write-races.test.ts \
  backend/domains/knowledge_entries/service.test.ts \
  backend/domains/knowledge_entries/service.delete.test.ts \
  backend/domains/knowledge_entries/routes.test.ts \
  backend/rest/v1-core.test.ts backend/rest/error-codes.test.ts \
  tests/guards/integration-scope.guard.test.ts \
  backend/domains/chat/shim.knowledge-scope.test.ts \
  backend/domains/retention/purge-document.test.ts \
  backend/domains/webdav/handlers.holder-scope.test.ts \
  backend/core/knowledge_entries/helpers.test.ts
  • Targeted server suite: 11 files / 156 tests passed, including all four controlled real-transaction regressions.
  • Scoped TypeScript: ten changed/importing sources, zero diagnostics, 3072 MiB heap cap; no whole-workspace typecheck.
  • Type-aware oxlint and oxfmt on changed TypeScript files: pass.
  • bun run lint:manual and git diff --check: pass.
  • Scoped pinned Opengrep: zero findings; its normal exclusions/large-file/partial-analysis limits apply (the large integration harness is not fully scanned).

Local storage PUTs capture bytes in memory; indexing/enqueue are mocked, while all PostgreSQL transactions and mutations are real. CI's new Backend integration lane uses the migrated database and real object store. No stack, browser, E2E, container or full platform suite ran locally. Proof/logs are retained in the TALE-147 delivery box.

Data / rollout / review

No migration is needed for this arbitration fix, so no existing duplicate is silently deleted and no live schema constraint/backfill is introduced. Historical duplicates require deliberate reconciliation; mixed-version instances do not all participate in the new advisory protocol. The guarantee applies to writes through the updated entry service, not arbitrary direct SQL writers.

Independent exact-head review is requested through TALE-359. No author acceptance, merge or task move.

Closes #3602

@yannickmonney

Copy link
Copy Markdown
Contributor Author

ACCEPT — independent review at exact head 513e1faf5150847fc78ec50ce5da47a56d29e8eb

Evidence: PR description/implementation and TALE-147 author receipt (comment 44494144-21e1-4fce-aa52-0f5548815ce8, referenced as 44494144). The destination-keyed tenant advisory lock is acquired inside the transaction before the duplicate lookup; updates then lock and re-read the addressed row with FOR UPDATE OF ke. Creates and renames therefore arbitrate one active destination and stale writers receive the existing 409 paths. Uploads remain before sql.begin; the examined create/rename order has no reverse lock acquisition. The PR explicitly states the historical-duplicate and mixed-version/direct-SQL rollout limits.

Unrun proof: I did not rerun CI, and the local focused Vitest command could not start because this checkout has no installed workspace dependencies (@storybook/addon-vitest/vitest-plugin, @tale/ui/vite/yaml, and vitest/config unresolved). I did not create a local PostgreSQL fixture. The author-reported scoped proof is 11 files / 156 tests passed, including the four controlled real-transaction races; CI status was not rerun or altered.

GitHub would not accept a native approval because the configured token identity is the PR author (Review Can not approve your own pull request); this comment is the accept record requested by the independent review task.

…4225

# Conflicts:
#	services/platform/tests/manual/reference/automation.md
@yannickmonney

Copy link
Copy Markdown
Contributor Author

Merged origin/main into the accepted PR branch without rebasing or force-pushing.

  • New head: f3163f54fdab61fe810d0b651f6b89ceb011aaab
  • Only conflicted file: services/platform/tests/manual/reference/automation.md
  • Resolution: retained every register row from both sides below the table delimiter; the interdiff is only the register merge.
  • Verification: bun run lint:manual passed (5 manual trees, 1447 boxes).
  • CI was not rerun or cancelled because the hosted queue is saturated, as requested.

@yannickmonney yannickmonney left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent review of PR #4225 at f3163f54fdab61fe810d0b651f6b89ceb011aaab: accepted (head recheck after the main merge)

  • Reviewer: agent #4 5f5307c9, run 9c98372b (TALE-596).
  • Implementation: Codex #3 4c7208dc. Run aa016970 wrote 513e1faf; run b8aa37aa made the merge commit f3163f54.
  • Earlier ACCEPT: 5976228314 at 513e1faf, by agent c0c99cd0. It ran no tests. I did not inherit it. Everything below was run or derived again at this head.

Scope: nothing changed beyond the merge

  • f3163f54 merges 513e1faf (the accepted head) with main at bd2275c3. The PR's own diff (bd2275c3..f3163f54) is byte-identical to the accepted diff (9b7463b7..513e1faf): the same 6 files, +509/−0. Only the hunk offsets and the register's context rows differ.
  • git show --remerge-diff f3163f54 shows one hand resolution, in services/platform/tests/manual/reference/automation.md. Both sides were kept: the PR's row, then main's #3619 Price row. Every other file is git's clean auto-merge, including integration-check.ts.
  • The head's register is the three-way union of both sides: 0 rows lost, 0 extra rows, 0 conflict markers.

Targeted tests at this head (run locally)

  • Setup: a PostgreSQL 16.14 cluster of my own, migrated by the platform's own runBootMigrations (148 app migrations).
  • The PR's 11-file command (bunx vitest run --project server --maxWorkers 1 …, with TALE_KNOWLEDGE_RACE_DATABASE_URL set): 11 files / 156 tests passed. The four write-races.test.ts tests executed against PostgreSQL and none were skipped.
  • Mutants: I mutated service.ts at head, ran write-races.test.ts and service.test.ts against each mutant, then restored the file (git status clean). Every part of the fix is load-bearing:
Mutant Race tests that fail (real PostgreSQL) Unit tests that fail
main's unfixed service.ts all 4 (exactly one concurrent writer commits) both lock tests
FOR UPDATE OF ke removed stale renames to distinct destinations rename / FOR UPDATE
create lock removed normalized same-topic creates; rename vs create create lock
update lock removed rename vs create rename lock
  • bun run lint:manual passed (5 trees, 1447 boxes), and git diff --check is clean.

Composition with current main

  • main at 56dd8691 (re-read just before posting): merge-tree is clean (tree 25ab0a91). The local composition commit fe04f674 was never pushed. Its diff against main is exactly the PR's diff. The same 11-file command passed 11 / 156, with the race tests executed, and lint:manual passed.
  • main at d29a3c8f (composition b1323dc9): 11 / 156 passed. An isolated tsc 6.0.2 run over the PR's TypeScript files plus integration-check.ts and the platform .d.ts files gave 0 diagnostics. d29a3c8f..56dd8691 changes no platform file.

CI (read only; nothing rerun, cancelled or waited on)

  • At f3163f54: these jobs are green: Unit (job 111377314357), Type check, Lint, Format, Knip, Integration scope, UI, Browser, Opengrep, and Lint commits. Unit skips write-races.test.ts (4 tests | 4 skipped) because it has no database URL.
  • Backend integration (job 111385828240) is still queued. It is the only CI job that runs the new lane. Builds, Playwright (platform) and Storybook are also queued.
  • At 513e1faf: Backend integration (job 111362974136) logged PASS knowledge entries: concurrent creates, corrections and renames and 1523/1523 checks passed across 225/225 lanes. The lane's code is byte-identical at this head.

Review notes (non-blocking)

  • No new deadlock cycle. The PR adds one advisory lock per create or update, taken as the transaction's first statement, and no other path in these transactions takes an advisory lock. An update then takes the row lock. The update's entry-row-then-document order matches deleteKnowledgeEntry.
  • Pre-existing inversion, not this PR's. Retention's expiry sweep locks the document and then the entry rows, the reverse of the update path's order. The update path has used that order since before this PR, and the PR only takes the entry row earlier, in the same order.
  • Precheck lock. The precheck outside the transaction runs in autocommit, so its FOR UPDATE is released at the end of the statement. It is harmless.
  • Rollout limits. The limits in the PR body still apply: historical duplicates, mixed-version instances and direct SQL writers.

Not run

  • Backend integration at this exact head is still queued. I did not run the full harness lane against a real object store locally; the local race tests capture blob bytes in memory and mock indexing.
  • Full platform suites, a whole-workspace typecheck, E2E, browser and container tests.
  • Local oxlint, oxfmt, knip and Opengrep (the CI jobs are green at this head).

Verdict: ACCEPT at f3163f54. Merge readiness still depends on this head's queued Backend integration and the other required checks. No push, merge, CI action or card move.

@yannickmonney
yannickmonney merged commit e6f7727 into main Oct 4, 2026
@yannickmonney
yannickmonney deleted the fix/knowledge-entry-write-races branch October 4, 2026 11:00
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: Concurrent knowledge-entry writes leave multiple current facts for one topic

1 participant