Repository navigation
Recompute flat entity maps before skipping a stored formula backfill - #32449
Conversation
|
🚀 Preview Environment Ready! Your preview environment is available at: https://precision-steel-jackson-possible.trycloudflare.com This environment will automatically shut down after 5 hours. |
🤖 PR Review
🛡️ Security Review✅ No high-severity vulnerabilities detected. 🚦 Auto-approve🙋 Manual review recommended for the following reason(s):
Automated pre-review — human approval still required. |
✅ Quality review · no findings
High-level — The outcome rename to Reviewed against the |
| }: DeferredWorkspaceOperationExecutionArgs< | ||
| DeferredWorkspaceOperationPayload<'compute_storedFormulaFields'> | ||
| >): Promise<DeferredWorkspaceOperationOutcome> { | ||
| const hasEveryFormulaField = |
There was a problem hiding this comment.
🟡 Nit · High-level · extend existing seams
The payloadEntityNotFound check reads the ORM workspace context instead of the runner-provided allFlatEntityMaps, so the runner's refresh-and-retry never covers it
The sibling handlers (e.g. rebuild-object-deferred-operation-handler.service.ts:31) look up the entity in allFlatEntityMaps for keys declared in metadataNamesToLoad, and DeferredWorkspaceOperationRunnerService (deferred-workspace-operation-runner.service.ts:427-441) invalidates and reloads only those keys before the second attempt. Here the field lookup goes through getWorkspaceContext() while metadataNamesToLoad lists only objectMetadata, so if the field map is stale the retry hits the same stale map, logs "metadata no longer exists" and drops the backfill. Fix: add 'fieldMetadata' to metadataNamesToLoad and check allFlatEntityMaps.flatFieldMetadataMaps in the handler, then delete hasEveryFormulaField.
There was a problem hiding this comment.
Not changed. The runner expands metadataNamesToLoad through getMetadataNamesToLoadForWorkspaceMigration, so objectMetadata already loads fieldMetadata, and invalidateFlatEntityMaps goes through withDerivedFieldMetadataMaps, so the retry refreshes flatFieldMetadataMapsOrm too. The check reads the ORM context on purpose: it is the snapshot the computation itself uses, and its freshness is tracked separately from allFlatEntityMaps.
CoreTeamTwenty
left a comment
There was a problem hiding this comment.
✅ Auto-approved — cleared automated quality and security review and is low-complexity.
- Quality: passed — 1 nit(s)
- Security: passed
- Auto-approve: passed — complexity low
Automated approval — a human still merges, and this is re-evaluated on every push.
Confidence Score: 5/5
[Medium impact] The PR appears safe to merge. SummaryThe handler now returns
Known limitation acknowledged by Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Check requested formula fields] --> B{All fields found?}
B -->|Yes| C[Backfill records]
B -->|No| D{Maps already refreshed?}
D -->|No| E[Refresh maps including ORM field maps]
E --> A
D -->|Yes| F[Skip deleted field operation]
Reviews (1) · Last reviewed commit: "Recompute flat entity maps before skippi..." · Reviewed by Greptile |
There was a problem hiding this comment.
🟡 Changes recommended
The cache consistency check can accept a new formula missing from its cached object's fieldIds, silently leaving it uncomputed instead of triggering recomputation.
1 open issue
What changed in this PR
Extends the deferred-operation retry mechanism from #32290 to stored-formula backfills, addressing silent skips caused by stale metadata.
Changes:
- Adds a formula-field existence check using the ORM workspace context.
- Returns
payloadEntityNotFoundwhen fields are missing, triggering the runner’s existing refresh-and-retry flow.
| File | Description |
|---|---|
| packages/twenty-server/src/engine/core-modules/stored-formula-field/services/stored-formula-field-computation.service.ts | Adds the formula-field existence check. |
| packages/twenty-server/src/engine/core-modules/stored-formula-field/deferred-operation-handlers/compute-stored-formula-fields-deferred-operation-handler.service.ts | Signals missing metadata before starting backfill. |
Review effort: Balanced
Superseded — re-approving the latest commit.
CoreTeamTwenty
left a comment
There was a problem hiding this comment.
✅ Auto-approved — cleared automated quality and security review and is low-complexity.
- Quality: passed
- Security: passed
- Auto-approve: passed — complexity low
Automated approval — a human still merges, and this is re-evaluated on every push.
The worker memoizes workspace cache reads for 10 seconds, so a job starting right after a metadata change read maps without the new entity. Marking the local cache stale forces a hash check against Redis without invalidating it for other pods. Rename the payloadEntityNotFound outcome to retryWithFreshMetadata.
Auto-approval withdrawn — this PR no longer meets the auto-approve criteria.
Backport to `v2.68` for a v2.68.1 patch. ## Why `twenty/v2.68.0` was tagged at `7feaaa1` (#32441) and is on staging, not prod. Fixes merged on main since then need to ship with 2.68, but main is already on 2.69.0 (#32446) and release tags are immutable, so they go out as v2.68.1 from this branch. ## Changes Clean cherry-picks (`-x`), no conflicts: - #32450 Keep SDK client type declarations in the server image (AI component preview fails in Docker images) - #32449 Recompute flat entity maps before skipping a stored formula backfill (formula fields left empty, found in 2.68 QA) - #32452 Answer not found for a deleted chat channel while its cleanup holds the lock (integration shard 14 failure) - #32372 Build the RLS workspace member predicates once per request (prod p99 latency) `.github/workflows` is unchanged on main since the tag, so the tag workflow should not 403. No UI change, no screenshots. <!-- This is an auto-generated description by cubic. --> <a href="https://cubic.dev/pr/twentyhq/twenty/pull/32471?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a> <!-- End of auto-generated description by cubic. -->

Why
A stored formula field created within about 10 seconds of another metadata change ends up with no values. Its
compute_storedFormulaFieldsoperation completes in a few milliseconds, the row is deleted, and nothing is backfilled. There is no error and no FAILED row.Seen during 2.68 QA on twenty-main: three formula fields created back to back had their operation complete in 6 to 9 ms with every record left empty.
Two things combine:
WorkspaceCacheServicememoizes each read for 10 seconds per process. An invalidation only clears that memo in the process that invalidates. The worker keeps serving the maps it read for the previous job, without checking the Redis hash.completed.The row is also committed before the migration invalidates the cache, but that window is a few milliseconds. The memo is what makes this happen in practice.
Reproduced locally on main with a server and a worker: 6 of 8 isolated creations, and 15 of 30 parallel ones, were left with no value. A later backfill on the same object hides it, because the dirty pass recomputes every formula of the records it touches.
This is the same failure as #32290 (
build_index,validate_foreignKey,build_recordPermissionIndexes,rebuild_object). The formula handler was not converted then.Fix
markWorkspaceLocalCacheStale, as the access group jobs do since Mark the workspace cache stale instead of evicting it in access-group jobs #31703). The next read compares hashes with Redis and refetches only what changed. Nothing is written to Redis and other pods are not affected. This covers every deferred operation, not only formulas.retryWithFreshMetadatawhen a formula field of its payload is not in the workspace context. The runner then recomputes the maps once from the database and runs the operation again. If the field is still missing it was really deleted and the operation is skipped, as before.payloadEntityNotFoundis renamedretryWithFreshMetadata, which says what the runner does with it.The recompute stays as a fallback for a row committed before its migration has invalidated the cache, and for entities that were really deleted.
Not covered
A formula switched from computed on read to stored inside the commit window is in the cache with its old evaluation, so it is still skipped. That needs a different signal than "not in the maps".
Screenshots
No UI change.
Test
Typecheck and lint. QA on a local server and worker, scratch database, final code:
Redis cache traffic for the same 48 creations, server and worker together:
The stale mark costs at most a few hash reads per job run and no write. Each recompute it avoids is about 33 MB written on this workspace.