Tool Library: Who Owns and Who Calls
Design problem and decision
- Design problem Lending a tool touches four things: the loan policy, the member, the branch shelf and the loan book. They must all agree after every call.
- Viewer's decision Which module owns each piece of state, which module coordinates them, and in what order does a coordinated call check and write?
- Outcome The viewer can draw an app twice, once as an owning tree and once as a call graph, and say what each drawing decides. They can specify a multi-module operation as checks first, writes last. They can say what must not be carried across a call to another canister.
- Starting point Lesson 1 left one sealed Shelf capsule on screen.
- Handoff Lesson 3 asks why each branch has its own Shelf.
- Pacing Four minutes.
Worked design
Tool Library is illustrative. State at the start of this film, on day 100:
| Module | Saved state |
|---|---|
| Policy | loan days 14, loan limit 3 |
| Members | 501: 2 active, 502: 2 active |
| Shelf of branch 3 | 17 drill: owned 4, available 2. 22 ladder: owned 1, available 0. 30 saw: owned 2, available 1. |
| Loans | (103, 3) and (108, 4): member 502, drill. (105, 5): member 501, ladder. (110, 6): member 501, saw. Next loan 7. |
Desk saves nothing. It offers lend and give_back and calls the other four.
| Call | Outcome | State after |
|---|---|---|
lend(501, 3, 17) |
Lent { loan 7, due 114 } |
drill available 1, member 501 active 3, loan (114, 7) added |
lend(501, 3, 17) again |
Refused(LimitReached) |
nothing changes |
charge_late_fee(5) on day 120 |
Charged { cents 150 } |
loan 5 marked charged |
The late fee is 15 days at 10 cents, charged through a separate payments canister.
The story in one pass
Add four capsules to the one from lesson 1 and hang them from a root: this is the owning tree. Place Desk beside them, owning nothing. Draw the call lines Desk needs: this is the call graph. Send one lend through it, watching every check complete before any value moves. Send a second lend that is refused. Then charge a late fee, and watch the frame close before the call leaves the canister.
Views and visual objects
Use the shared visual language. This film introduces OwnTree, CallLines and Frame.
| View | Framing and anchor |
|---|---|
tree |
The canister boundary. Root capsule at top. Policy, Members, Branches and Loans in a row beneath. The Shelf capsule for branch 3 hangs under Branches. This layout is fixed for the rest of the course. |
calls |
The same layout with Desk at upper right, outside the tree, and CallLines drawn over it. A narrow check row sits above Desk. |
await |
The canister at left, a small Payments canister at right, a timeline strip beneath with two frame brackets. |
Scene two_drawings — Owning is not calling
Already on screen The Shelf capsule from lesson 1, values 17: 4/2, 22: 1/0, 30: 2/1, tab v1.
Beat owning_tree — Where memory lives
View and focus tree.
Narration
The shelf is one module of several. Policy holds the rules. Members holds who is borrowing. Loans holds the loan book. Branches holds the shelves. Each has its own sealed memory, and all of it hangs from one root. This is the owning tree. It says where memory lives.
Cue and animation The camera pulls back from Shelf. On each module name, its capsule draws in place with its vault table. On “hangs from one root”, solid edges draw from the root vault down to the four capsules, and from Branches to Shelf with the key 3 written on the edge. On “owning tree”, the edges pulse once.
Leaves with Six capsules joined by solid edges. Every vault has a v1 tab.
Beat desk_owns_nothing — A module without memory
View and focus calls. Focus on Desk.
Narration
Lending needs all of them at once. So we add Desk. Desk has public functions and code, and no memory of its own. It is not in the tree at all.
Cue and animation On “we add Desk”, a capsule with a rim reading lend, give_back and a service band draws at upper right. On “no memory”, the place where its vault would be stays empty with a faint outline. On “not in the tree”, no edge draws to it, and the tree edges dim slightly.
Leaves with Desk beside the tree, unattached.
Beat call_lines — Who talks to whom
View and focus calls.
Show The AppTree appears small at lower left with desk.rs and root/mod.rs highlighted.
Narration
Desk calls the public functions of Policy, Members, Branches and Loans. This is the second drawing, the call graph. It is wired once at startup, in the root. The two drawings are separate. Owning decides where memory lives and when it is removed. Calling decides who talks to whom.
Cue and animation On each module name, a thin call line draws from Desk's band to that module's rim. On “wired once at startup”, root/mod.rs highlights in the tree. On “Owning decides”, the solid edges brighten and the call lines dim. On “Calling decides”, they swap.
Leaves with Both drawings visible: solid owning edges and thin call lines.
Scene one_lend — Check everything, then write
Already on screen The calls view with both drawings.
Beat enter_frame — A public method opens one frame
View and focus calls. Focus on the canister edge and Desk.
Show One snippet, tabbed lib.rs — illustrative, modelled on Shop's checkout endpoint:
#[cdk_rsx::update]
fn lend(member: u64, branch: u64, tool: u64) -> Outcome {
root::with(|app| app.desk().lend(member, branch, tool, today()))
}
Narration
A member borrows a drill. The public canister method checks who's allowed to make the request and supplies today's date. Then it opens a write frame on the root and calls Desk. Desk and the modules it calls share that frame.
Cue and animation On “borrows a drill”, the pill lend(501, 3, 17) arrives at the canister edge. On “opens a write frame”, the line root::with highlights and a thin outline labeled write draws around the root, Desk and the modules. On “calls Desk”, the pill docks at Desk's lend.
Leaves with The pill at Desk inside the write frame. The snippet fades.
Beat checks — Every rule first
View and focus calls. Focus on the check row above Desk.
Narration
Desk asks before it changes anything. Policy allows three loans per member. Members says five-oh-one holds two. Branches has a drill on the shelf. Loans has room for another entry. Every check passes before the first write.
Cue and animation On “Policy”, a small pill travels the call line to Policy and returns limit 3, days 14 into the check row. On “Members”, a pill returns active 2 and a tick appears beside 2 < 3. On “Branches”, a pill travels to Branches, continues down the owning edge to the Shelf of branch 3, and returns available 2 with a tick. On “has room”, a tick appears on Loans' capacity check. On “before the first write”, every vault pulses once, closed.
Leaves with The four checks complete. All values unchanged.
Beat writes — Then each owner writes its own
View and focus calls. Focus moves across Shelf, Members, Loans.
Narration
Now the writes. Each one is a call to a public function, and each module changes only its own memory. The shelf gives up a drill. The member's count goes up. The loan book opens loan seven, due on day one-fourteen.
Cue and animation On “The shelf gives up a drill”, the Shelf key badge turns and available goes 2 to 1 in red with -1. On “count goes up”, Members shows 501: 2 to 3 in green with +1. On “opens loan seven”, a new row (114, 7) 501 · 3 · 17 descends into the Loans table. The OutcomeCard Lent { loan 7, due 114 } leaves through the canister edge. The write frame closes.
Leaves with Drill 4/1. Member 501 active 3. Loans has five rows.
Beat refused — The same call, one loan later
View and focus calls.
Narration
The same member asks again. Desk asks Members first. Three is the limit. Desk stops there and answers with the reason. No write was made, so there is nothing to undo.
Cue and animation On “asks again”, a second pill lend(501, 3, 17) docks at Desk in a new write frame. On “Three is the limit”, the check row shows active 3 and a cross: 3 < 3. On “answers with the reason”, a neutral OutcomeCard Refused(LimitReached) leaves. On “nothing to undo”, all vaults pulse once, closed. The frame closes.
Leaves with State unchanged from the previous beat.
Scene across_await — A call that leaves the canister
Already on screen The calls view, no frame open.
Beat late_fee — Close the frame before you wait
View and focus await.
Narration
One feature leaves the canister. On day one-twenty, loan five is fifteen days late, and the fee is charged by a payments canister. First frame: Loans marks loan five as charging and hands back plain values. The frame closes. Then the call leaves, and we wait. When the answer returns, a new frame opens and Loans records the result. Nothing borrowed from the first frame is carried across the wait.
Cue and animation On “First frame”, a write frame draws and the Loans row (105, 5) gains the mark charging. On “plain values”, a small card member 501, cents 150 lifts out of the frame. On “The frame closes”, the outline disappears and the first bracket on the timeline strip ends. On “the call leaves”, a dashed arrow with a clock travels to Payments carrying the card. On “a new frame opens”, a second bracket starts, a fresh write frame draws, and the mark changes to charged. The OutcomeCard Charged { cents 150 } leaves.
Hold PauseAndExplain pointing at the gap between the two brackets. The balloon reads:
Between the frames
other calls can run
loan 5 says "charging" so they know
if the answer never arrives, "charging" is what remains
Leaves with Loan 5 marked charged. Two closed brackets on the strip. Lesson 6 returns to the charging mark as work that blocks an upgrade.
Brief to hand over
Illustrative.
Module
desk. Saves nothing.lend(member, branch, tool, today)checks Policy, Members, the branch's Shelf and Loans' ability to open the new entry before any write. On success it calls the shelf'stake, Members'add_loan, and Loans'open. It returns the loan number and due day. The endpoint verifies the caller's authority for the member and getstodayfrom the canister clock. Tests pass a fixed day to Desk. Desk uses each dependency's public functions.Late fee. Mark the loan
chargingand close the frame before calling Payments. Record the answer in a new frame. A loan leftchargingis a visible state, not an error.
Acceptance test, expected and not run
| Given | Command | Expect | Then |
|---|---|---|---|
| state above, day 100 | lend(501, 3, 17) |
Lent { loan 7, due 114 } |
drill 4/1, 501 active 3, loan (114, 7) |
| after that | lend(501, 3, 17) |
Refused(LimitReached) |
no value changed |
| state above | lend(502, 3, 22) |
Refused(NoneAvailable) |
no value changed |
| state above | lend(502, 8, 17) |
Refused(NoSuchBranch) |
no value changed |
| state above | lend(777, 3, 17) |
Refused(NoSuchMember) |
no value changed |
| valid member and available tool, loan book at its cap | lend(501, 3, 17) |
Refused(LoansFull) |
no value changed |
After every row: each member's active count equals that member's rows in Loans, and is at most the limit.
Production and source notes
- Tool Library, Desk and the snippet are illustrative. The excerpt shows the scoped service call. Stage the app's caller check before the excerpted lines. The endpoint shape follows Shop's
checkout. Shop endpoints, lines 30 to 33. - Desk is modelled on Shop's stateless
Checkout, which declares its dependencies as service fields and has no memory. Itsbuyquotes and computes before it sells. Shop checkout, lines 12 to 34. - Two separate graphs: "Persistent ownership and transient service composition are separate graphs." Store architecture, lines 17 to 20. Shop wires its call graph in one
bindfunction. Shop root wiring, lines 37 to 59. - One frame per call, dependencies lent inside it, no storage arguments on business methods: Conventions, lines 191 to 226.
- A frame is not a transaction and an error undoes nothing: Store architecture, lines 148 to 152.
- Nothing borrowed crosses an await: Store architecture, lines 143 to 146. The async scope probe is the checkout's example of two frames around one call. Async scope probe
- The balloon's last line reflects the status page: a caller must keep the identity of the effect and treat the outcome as uncertain. Status, lines 66 to 70.
- The path from Desk to a branch's Shelf goes through Branches. Draw the pill travelling the call line to Branches and then down the owning edge. Lesson 3 explains why.
- Keep the
Frameoutline thin. It is a boundary of one call, not a container.