KEEP: Upgrading the Canister, Keeping the Heap
What the viewer will understand
How can a canister install different Wasm and still read its native Rust values? Follow one KEEP upgrade through old-code teardown, startup checks, and a new App binding. The viewer should be able to explain why the new business code can use the old buffer. This film uses the Nyxus pages from lesson 3 and the service bindings from lesson 2. Aim for about three minutes of narration and holds. Lesson 8 changes the saved memory contract.
The worked example
Use the nested rrt-arena-probe fixture and its PocketIC lifecycle runner. The runner installs V1 with seed 7, appends 128 values starting at 100 and 17 starting at 500, then installs the independently built business-two Wasm with KEEP. The last pre-upgrade snapshot has schema = 1, business = 1, value = 152 and len = 147 at the root. Its items[7] leaf reports value = 162 and len = 2048 bytes. The code changes business revision to 2 outside the saved memory contract. The runner compares the rest of the snapshot for equality, including the two buffer addresses, checksums and the leaf's strong count after rebinding. A later incompatible-layout build adds a field to the V1 root and fails admission. The runner checks the same installed snapshot again. Fixture snapshot · Business revision · V1 layout variant · Lifecycle assertions
The lifecycle drawings follow the source and assertions. Show buffer identity with the same address tick and, when supplied by a capture, the actual address. Teardown and recovery are explanatory views of the SDK. The tested application alias and the provider's general reclamation of leftover Runtime runs are separate events. Transient registrations · Runtime recovery
The story in one pass
Start with a populated tree and a query that reports business revision one. Follow the code swap through old-code teardown, the startup guard and the Catalog comparison. Recover old Runtime capacity, rebuild the transient App and repeat the query. A second candidate fails admission. The IC rolls back the whole attempt, including the old teardown, and revision two continues over the same V1 memory.
Components and views
Use the shared visual language. Keep the canister, MemoryModule cells and page strip fixed as new Wasm enters. Draw the root with items[7], the branch slot and its leaf, plus the empty scratch slot. On the shared main-memory strip, distinguish Nyxus and Runtime claims and the static/control range. The root owns a Vec<u64> backing buffer. The direct leaf and the separate HELD clone both point to one Rc control block, which owns the leaf's Vec<u8>. Put address ticks on the two backing buffers. Draw App's actor binding with a closed ring. Reuse CatalogGate and CatalogFingerprint and show the clean-marker check before the Catalog comparison.
| View | Camera and visible objects |
|---|---|
arena_canister |
Fit the tree, the App and HELD above it, the Wasm card and the shared main-memory strip. |
arena_buffers |
Center on the root vector and direct leaf byte buffer, with their address ticks and owner paths readable. |
arena_startup |
Frame the incoming Wasm, platform bootstrap and clean marker, the static/control range, and the saved and expected Catalog cards. Keep the Nyxus allocations in the strip visible at the bottom. |
arena_status |
Widen from the tree to the state() result and the current Wasm card. Anchor on the two buffer address ticks. |
Scene populated_v1 — Read the running canister
Enters with The installed V1 canister has already completed the two append calls in the runner.
Beat inspect_first_state — Read the two revisions
View and focus In arena_status, start on the old Wasm card and its state() query, then follow the query into the tree.
Narration
This canister is already running. Its code reports business revision one, and its Store uses memory schema one. The root owns a vector of integers. Its numbered child owns a separate buffer of bytes.
Cue and animation At “This canister is already running,” carry state() to the old Wasm and reveal business 1 on the return. On “Its numbered child,” bring items[7] and the leaf's own buffer into focus. Keep schema 1 on the memory side.
Leaves with Both values are visible in arena_buffers with their original address ticks.
Beat request_keep — Keep the main-memory image
View and focus Pull back to arena_canister. Frame the position where a new Wasm card will enter over the retained memory.
Narration
We can check where both buffers live before asking the Internet Computer to replace the Wasm. The request selects KEEP, which preserves the canister's Wasm main memory for the upgrade.
Cue and animation At “asking the Internet Computer,” hold both address ticks and move the upgrade · KEEP pill toward the canister boundary. Bring the replacement card to the boundary and leave it there while the installed code runs its shutdown.
Leaves with The upgrade request is at the boundary. The installed code still owns the shutdown phase.
Scene old_teardown — The old executable gets ready
Enters with The upgrade · KEEP request has reached the canister and the V1 App is still bound.
Beat clear_old_roots — Release the transient objects
View and focus In arena_canister, center on the old Wasm and the transient App and HELD above the tree.
Narration
Application work drains before the upgrade begins. The old code checks that callbacks and executor tasks have drained and that no Store frame is open. Shutdown then drops App and a separately held reference to the leaf's vector. The leaf keeps its own reference.
Cue and animation At “callbacks and executor tasks have drained,” highlight the SDK check, then show the fixture's Store frame check. At “Shutdown then drops App,” remove App and its actor handle. Then remove the HELD reference from the Rc block. Keep the leaf's owning edge and both backing buffers pinned.
Leaves with The registered transient objects have gone. The retained tree and buffers stay in view.
Beat publish_clean_marker — Record that teardown passed
View and focus Move into arena_startup and point at the clean marker's slot after the final checks.
Narration
After SDK cleanup and the final readiness checks, the old code records a clean marker. The incoming Wasm checks this marker before it initializes its statics.
Cue and animation At “records a clean marker,” fill the marker slot.
Hold or transition Use PauseAndExplain through the next sentence, with the leader ending on the marker. Resume at “The startup guard” in the next scene.
On-screen text or code Balloon Old-code teardown passed.
Leaves with The complete machine is held with the clean marker visible and the incoming Wasm at the boundary.
Scene startup_admission — Check the saved image
Enters with Old-code teardown has recorded the marker and the incoming Wasm is about to start.
Beat guard_and_initialize — Start the incoming code
View and focus Hold arena_startup with the bootstrap, marker, static/control range, and retained band visible.
Narration
The startup guard checks the saved platform layout and consumes the clean marker. It resets the SDK's control area and initializes the new program's statics. The retained allocations stay in their pages.
Cue and animation At “checks the saved platform layout,” slide in the new Wasm card, then trace the bootstrap and marker reads. As the marker is consumed, fade its token. On “initializes the new program's statics,” replace the indicated initial values in the bounded static/control strip and keep the retained pages pinned.
Leaves with Fresh statics are visible and both Catalog fingerprints are ready to compare.
Beat match_v1_catalog — Compare the saved contract
View and focus Keep arena_startup and move emphasis to the two CatalogFingerprint cards at CatalogGate.
Narration
The incoming code compares the saved fingerprint with its compiled Catalog. The Catalog covers the data's layout and how the code interprets it. This fixture's business revision can change outside that contract. The fingerprints match.
Cue and animation At “compares the saved fingerprint,” bring the saved V1 and expected V1 cards together. At “business revision can change,” keep the separate business code indicator on the Wasm card. On “The fingerprints match,” show admission accepted. The first Store read belongs to the restore beat.
Leaves with The expected V1 layout has been admitted. The provider can recover before new allocation.
Beat recover_runtime_runs — Give free capacity back
View and focus Follow the accepted path to the shared main-memory strip. Focus on old Runtime runs and keep both Nyxus buffer ticks in frame.
Narration
Before the first new allocation, the provider recovers any leftover Runtime pages for reuse. It reads their ownership records. The Nyxus pages are still owned, so both retained buffers stay at their original addresses.
Cue and animation At “recovers any leftover Runtime pages,” move ownership marks from an illustrative old Runtime run to the free pool. Keep its addresses physically stationary and leave the retained Nyxus ownership marks intact.
Leaves with Runtime capacity is ready for new allocations. The retained root and buffers are ready to reopen.
Scene return_to_service — Query the same data again
Enters with The incoming code has admitted the V1 Catalog and recovered Runtime capacity.
Beat restore_app — Bind a new App
View and focus Return to arena_canister with the App position above the retained actor tree.
Narration
The restore hook opens the retained root. It takes a fresh reference to the leaf's vector and saves a new App bound to the actor. Then it writes the V1 fingerprint back to the saved Catalog.
Cue and animation At “opens the retained root,” follow the admitted read into Store. On “takes a fresh reference,” connect the separate HELD edge to the leaf's Rc block. At “saves a new App,” bring in App and connect its actor binding to the root. On “writes the V1 fingerprint,” seat it in the saved Catalog position.
Leaves with The new code has finished restoring the working application.
Beat show_business_two — Change code over the same values
View and focus Move to arena_status and focus on the new state() response, with both retained buffer positions still visible.
Narration
Ask for state again. The response now says business revision two. Schema one and the saved values match the first response. Both buffers are still at the same addresses.
Cue and animation At “Ask for state again,” replay the query through the new App. On “The response now says business revision two,” change the result field from 1 to 2 and pulse the other compared fields in place. On “the same addresses,” trace each retained tick once.
Leaves with The successful query is visible as the starting state for the runner's rejection check.
Beat reject_other_layout — Reject a different layout
View and focus Reuse arena_startup for the separately built incompatible candidate. End back in arena_status.
Narration
The test then tries a build with an extra field in the V1 root. The Catalog comparison fails before the candidate opens a retained pointer. The IC rolls back the attempt, including the old code's teardown. Business revision two is still running on schema one. To change the schema on purpose, the next build will name the V1 contract it can convert.
Cue and animation At “an extra field in the V1 root,” show the incompatible-layout candidate and replay teardown, clean-marker consumption and static replay in order. On “Catalog comparison fails,” stop the candidate at admission. At “rolls back the attempt,” restore the prior code, App, HELD, static state and running marker state together from the pre-attempt frame. The old code resumes through rollback. At “Business revision two is still running,” re-show business 2, schema 1 and the two matching address ticks for the cut to lesson eight.
Leaves with The original accepted V1 memory contract is active, ready for the exact predecessor example in lesson 8.
Review notes
- Start the film with the populated snapshot after
append(17, 500). The preceding 4,096-value trap in the runner rolls back and contributes no values to that snapshot. The equalities compare addresses within the same canister. Runner sequence state()reports bothschemaandbusinessfrom code. Place the reported schema next to its tree and the business revision next to its Wasm card to show what each field describes. Snapshot fields- Check that
HELDand the leaf connect to the sameRcblock while the address tick points to the backing byte buffer. The native teardown test asserts a strong count of one after shutdown. The PocketIC snapshots compare the running App and its held alias, with a strong count of two after rebind. Native teardown test · Payload assertions - Keep pre-upgrade readiness, raw guard and Catalog admission in sequence. The fixture enters with no pending async work. Its explicit check tests Store quiescence after the SDK's callback and executor check. The raw guard consumes the clean marker before replaying statics and admitting the Catalog. The runner checks that bypassing pre-upgrade rejects and that the incompatible candidate rolls back. Readiness hooks · KEEP ordering · Rejection assertions
- Verify the accepted sequence against the time-ordered outline with narration muted. Publish the Catalog after the runtime install call succeeds, then return control to the host and query. The film condenses repeated KEEP checks to this one code change and one mismatched candidate. Restore hook