Skip to content

fix: preserve context tracking when retained subtrees become memoized - #1977

Merged
trueadm merged 3 commits into
octanejs:mainfrom
openai-oss-forks:codex/memo-context-promotion
Oct 10, 2026
Merged

trueadm merged 3 commits into
octanejs:mainfrom
openai-oss-forks:codex/memo-context-promotion

Conversation

@domgan-oai

@domgan-oai domgan-oai commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Keep retained context consumers reactive when an existing ancestor gains a memo or compiler-cached child boundary.

Why

The descendants were created before that boundary existed. Marking only the ancestor left their memoInChain flags false, so later context reads skipped dependency recording. Changing the provider then left stale text.

The fix promotes existing descendants when the boundary is armed. It covers a lazy body becoming memoized, an ordinary render function becoming cached compiled children, and a memo wrapper taking over a retained render slot.

Changes

  • Propagate the existing memo-ancestry flag through retained blocks and lightweight scopes before rendering the newly armed boundary.
  • Keep promotion conservative across rollback and guard direct calls to memo wrappers that do not own the current scope.
  • Add public regressions for context updates, edited input identity, direct wrapper calls and suspended-update rollback.

Validation

  • On main 5d6d02c8, the four new context scenarios fail in both development and production: 8 expected failures, with 4 neighboring controls passing.
  • The isolated fix passes 12/12 focused checks and 450/450 neighboring development/production executions across context, memo, lazy rendering and hydration suites.
  • Official scoped runtime, fixture and test typecheck passes. Scoped formatting and full pnpm sync pass with no generated changes.
  • perf-review: node scripts/perf-review-scan.mjs --base 2075c0e7 --head HEAD --json reports 0 candidates. memoWrapper adds a per-render guard; promoteMemoAncestry visits retained scopes only when a boundary is newly armed. It writes an existing boolean field and adds no collection, closure, DOM operation or scheduling hop. Compiler sources and ancestry loops are unchanged. The current-head benchmark report passes with all 64 work counters unchanged; local latency is unmeasured.
  • concise-code: source/runtime +29 −7; no new source file or export. The new changeset records the correctness fix. Reusing the ancestry flag and subtree visitor preserves state; remounting would lose it, while recording every context read would tax ordinary trees without memo boundaries. The report measures +34 B gzip for the complete root-static fixture, +62 B for weather production JS and +84 B for bindings production JS; these are correctness-fix costs, not size savings.
  • Signed main integration f991f5329 against 2075c0e7: 12/12 focused development/production checks pass; full pnpm sync passes with no generated changes. The promotion patch remains +29 −7; the compiler and existing context walks match main.
  • All current-head checks for f991f5329 pass, including full CI and the benchmark report. Maintainer review is still required.

Risk / follow-ups

Promotion walks a retained subtree only when its boundary is newly armed; the memo wrapper also gains a small guard. Existing context ancestry walks are unchanged, and no block field, cache or compiler transform is added. Local latency has not been measured, and no speedup or Ripple parity is claimed. Timing the stale-output baseline would not compare equivalent behavior.

This is the context correctness change separated from #1967. The descriptor optimization, shorter context walks and teardown optimization are excluded.

Provenance

  • An agent produced this diff (agent-authored)

Codex task · implementation host com-79386.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Medium Risk
Touches core runtime context dependency tracking in memo, lazy, and implicit-bailout paths; behavior is narrowly scoped to newly armed boundaries but affects re-render correctness.

Overview
Fixes stale context when a retained subtree’s ancestor later becomes a memo() boundary or compiler-cached child slot: descendants created earlier kept memoInChain false, so useContext skipped dependency recording and provider updates did not re-render them.

Adds promoteMemoAncestry, which walks the retained block/scope subtree and sets memoInChain when lazy resolution, implicit bailout arming, or the owning memo wrapper first arms the boundary—instead of only flipping the ancestor block. memo() now promotes only when the current render scope actually owns that wrapper, so direct calls outside a boundary stay safe.

Regression coverage spans lazy→memo promotion, render-function→compiled-children, memo→element, suspension rollback, and preserved DOM/input identity; changeset records an octane patch.

Reviewed by Cursor Bugbot for commit f991f53. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 10, 2026
@domgan-oai
domgan-oai marked this pull request as ready for review October 10, 2026 12:01
@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🔴 bundle-size: 27 value(s) increased
  • 🔴 bundle-reachability: 20 value(s) increased

Compares 2075c0e72, the merge commit's first parent with c5014806a on the same runner. Any increase in a js-framework work counter fails this check; byte changes are a report. Wall time is a report: base and head samples alternate in one browser, and an operation is called slower or faster only when the 95% confidence interval of its ratio excludes ±3%. Workflow run

js-framework: 1k/10k row operations

All 20 timed operations (20 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 2.333 2.317 −3.7% −4.3% … −0.0% ⚪ within noise
octane-tsrx replace 4.967 4.983 −0.0% −3.6% … +3.1% ⚪ within noise
octane-tsrx add 2.167 2.225 ±0.0% ±0.0% … +0.0% ⚪ within noise
octane-tsrx update 0.118 0.117 +0.2% −1.5% … +1.8% ⚪ within noise
octane-tsrx select 0.005 0.005 +0.3% −2.1% … +3.7% ⚪ within noise
octane-tsrx swap 0.015 0.016 +3.9% +2.8% … +5.9% ⚪ within noise
octane-tsrx remove 0.012 0.012 −1.0% −3.9% … +1.6% ⚪ within noise
octane-tsrx runlots 22.217 22.550 −3.1% −3.8% … −0.7% ⚪ within noise
octane-tsrx select_lots 0.006 0.006 +0.9% +0.0% … +3.2% ⚪ within noise
octane-tsrx clear 39.642 39.525 +1.1% −2.5% … +2.2% ⚪ within noise
octane-jsx run 3.092 3.033 ±0.0% −0.0% … +3.2% ⚪ within noise
octane-jsx replace 5.758 5.500 −0.0% −4.5% … +4.2% ⚪ within noise
octane-jsx add 2.950 2.917 −0.0% −3.3% … ±0.0% ⚪ within noise
octane-jsx update 0.224 0.229 +1.4% ±0.0% … +2.9% ⚪ within noise
octane-jsx select 0.115 0.114 −1.0% −1.5% … −0.8% ⚪ within noise
octane-jsx swap 0.162 0.158 −1.9% −4.1% … −0.5% ⚪ within noise
octane-jsx remove 0.156 0.154 +0.0% −2.0% … +1.5% ⚪ within noise
octane-jsx runlots 25.417 26.108 +1.9% −0.4% … +4.7% ⚪ within noise
octane-jsx select_lots 1.376 1.347 −2.1% −3.8% … −1.5% ⚪ within noise
octane-jsx clear 41.658 41.700 −0.5% −2.2% … +1.2% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

target metric base head Δ
octane-tsrx weather_js_raw 140,049 140,191 +142 (+0.1%) 🔴
octane-tsrx weather_js_gzip 46,297 46,359 +62 (+0.1%) 🔴
octane-tsrx weather_js_brotli 40,761 40,814 +53 (+0.1%) 🔴
octane-tsrx weather_app_brotli 6,650 6,654 +4 (+0.1%) 🔴
octane-tsrx weather_fw_raw 117,875 118,017 +142 (+0.1%) 🔴
octane-tsrx weather_fw_gzip 38,440 38,502 +62 (+0.2%) 🔴
octane-tsrx weather_fw_brotli 34,111 34,160 +49 (+0.1%) 🔴
octane-tsrx bindings_js_raw 678,716 678,868 +152 (+0.0%) 🔴
octane-tsrx bindings_js_gzip 223,483 223,567 +84 (+0.0%) 🔴
octane-tsrx bindings_js_brotli 190,143 190,271 +128 (+0.1%) 🔴
octane-tsrx bindings_app_gzip 115,670 115,672 +2 (+0.0%) 🔴
octane-tsrx bindings_app_brotli 96,179 96,213 +34 (+0.0%) 🔴
octane-tsrx bindings_fw_raw 330,974 331,126 +152 (+0.0%) 🔴
octane-tsrx bindings_fw_gzip 107,813 107,895 +82 (+0.1%) 🔴
octane-tsrx bindings_fw_brotli 93,964 94,058 +94 (+0.1%) 🔴
octane-tsrx full_fw_raw 573,435 573,585 +150 (+0.0%) 🔴
octane-tsrx full_fw_gzip 173,260 173,277 +17 (+0.0%) 🔴
octane-tsrx full_fw_brotli 145,807 145,733 −74 (−0.1%) 🟢
octane-jsx js_raw 168,668 168,757 +89 (+0.1%) 🔴
octane-jsx js_gzip 54,615 54,665 +50 (+0.1%) 🔴
octane-jsx js_brotli 47,904 48,025 +121 (+0.3%) 🔴
octane-jsx app_gzip 2,860 2,861 +1 (+0.0%) 🔴
octane-jsx app_brotli 2,411 2,413 +2 (+0.1%) 🔴
octane-jsx fw_raw 160,389 160,478 +89 (+0.1%) 🔴
octane-jsx fw_gzip 51,755 51,804 +49 (+0.1%) 🔴
octane-jsx fw_brotli 45,493 45,612 +119 (+0.3%) 🔴
octane-jsx full_fw_raw 573,435 573,585 +150 (+0.0%) 🔴
octane-jsx full_fw_gzip 173,260 173,277 +17 (+0.0%) 🔴
octane-jsx full_fw_brotli 145,807 145,733 −74 (−0.1%) 🟢

31 other measured values are unchanged.

Public-import reachability bundles (bytes)

target metric base head Δ
root-static raw 148,414 148,503 +89 (+0.1%) 🔴
root-static gzip 48,068 48,102 +34 (+0.1%) 🔴
root-static brotli 42,384 42,364 −20 (−0.0%) 🟢
hydrate-frame raw 350,551 350,640 +89 (+0.0%) 🔴
hydrate-frame gzip 110,455 110,499 +44 (+0.0%) 🔴
hydrate-frame brotli 94,750 94,857 +107 (+0.1%) 🔴
binding-base-ui-vite raw 187,433 187,522 +89 (+0.0%) 🔴
binding-base-ui-vite gzip 60,219 60,268 +49 (+0.1%) 🔴
binding-base-ui-vite brotli 52,730 52,754 +24 (+0.0%) 🔴
binding-base-ui-esbuild raw 214,755 214,844 +89 (+0.0%) 🔴
binding-base-ui-esbuild gzip 69,843 69,871 +28 (+0.0%) 🔴
binding-base-ui-esbuild brotli 60,731 60,819 +88 (+0.1%) 🔴
binding-radix-vite raw 187,795 187,884 +89 (+0.0%) 🔴
binding-radix-vite gzip 60,140 60,180 +40 (+0.1%) 🔴
binding-radix-vite brotli 52,625 52,663 +38 (+0.1%) 🔴
binding-radix-esbuild raw 214,901 214,990 +89 (+0.0%) 🔴
binding-radix-esbuild gzip 69,756 69,792 +36 (+0.1%) 🔴
binding-apollo-client-vite raw 329,588 329,677 +89 (+0.0%) 🔴
binding-apollo-client-vite gzip 102,485 102,483 −2 (−0.0%) 🟢
binding-apollo-client-vite brotli 89,868 89,841 −27 (−0.0%) 🟢
binding-apollo-client-esbuild raw 364,926 365,015 +89 (+0.0%) 🔴
binding-apollo-client-esbuild gzip 116,241 116,274 +33 (+0.0%) 🔴
binding-apollo-client-esbuild brotli 101,512 101,573 +61 (+0.1%) 🔴

88 other measured values are unchanged.

@trueadm

trueadm commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator

Good to go, with two small nits. The root cause is fixed. memoInChain was only set when a block was constructed, so a retained subtree under a boundary that became memo-armed later never recorded its context reads. The next equal-props bail then left those consumers on stale values. Promoting the subtree before the boundary renders fixes that. The flag only ever goes to true, so each Block is walked at most once.

Suggestion (not blocking): the memoWrapper guard runs on every memo render. createBlock already arms __memo bodies (L12260), so the guard can only fire after an in-place body swap. The only swap that is followed by a bail is childSlot's bare-function path. Folding the check into the existing arming branch removes the per-render check and makes the direct-call test unnecessary:

if (taggedChildren && !wasImplicitlyArmed) state.block.$$implicitBail = true;
if ((taggedChildren || (comp as any).__memo === true) && !state.block.memoInChain)
	promoteMemoAncestry(state.block);

Nits:

  • Changeset L6–L7: "Preserve DOM identity… across suspended updates" reads like a second fix, but DOM identity was already preserved.
  • The comment at L19068 still says the flag "is precomputed at block creation".

Verified:

  • I checked every memoInChain writer. The remaining direct writes (L35966, L40223, L40374) arm newly created Blocks with no retained descendants.
  • forEachSubtreeChild covers everything the refreshContextConsumers walk covers, plus hidden try content.
  • Traced on main: the lazy, tagged-children and memo-element cases strand at two, and the rollback case strands at after. All three arming sites are covered.
  • perf-review-scan finds 0 candidates, adds no Block fields, and work counters are unchanged. root-static grows +89 B raw.

@trueadm
trueadm merged commit 773144a into octanejs:main Oct 10, 2026
33 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants