Skip to content

fix: drop the driver's test-only observerEntries hook; test pruning through behaviour - #199

Merged
jlongster merged 1 commit into
devfrom
fix/driver-no-observer-hook
Dec 3, 2026
Merged

jlongster merged 1 commit into
devfrom
fix/driver-no-observer-hook

Conversation

@jlongster

Copy link
Copy Markdown
Collaborator

What was retained

#192 added export const observerEntries: { last?: () => number } = {} to src/runtime/driver.ts, and every Driver constructor set observerEntries.last = () => this.reported.size + this.observed.size. That closure captures the driver, so a module global kept the most recent driver (its VM, globals and host context) reachable long after its run finished.

Change

The hook was test-only, so it is removed entirely (export and constructor line). Nothing in src, the index exports or docs referenced it.

How pruning is tested now

  • Per-call pruning (host-observe.test.ts, host-observe-promise.test.ts): a run does 20 observed durable calls and 20 unobserved ones, then parks on a host gate() call. While the run is still alive, the test forces GC and checks that WeakRefs to the ObservedCall objects each before hook received are all collected, so the driver's observed map no longer holds finished calls. Then it releases the gate and checks the result. Commenting out this.observed.delete(id) makes both tests fail.
  • Unobserved calls: they still run in the same test, but nothing from outside shows whether they record anything (the reported set holds only numbers), so there is no separate assertion for them.
  • Real abort during wait: the event assertions stay the same (before, wait, after interrupted once, cancel). Only the entry-count check is gone.
  • Finished run not retained (adapted from fix(ts): don't retain the last Driver in observerEntries #197): a WeakRef to a 1 MiB value that only the run's globals hold is collected after execute returns.

The GC checks force collection with Bun.gc(true) and short sleeps, up to 50 rounds, and stop as soon as the objects are gone. Bun scans the stack conservatively and runs leave timers behind, so a single round was flaky, mainly under roundtrip. Both tests passed with --rerun-each 5, normal and PSYCHOSCRIPT_ROUNDTRIP=1.

Supersedes #197 (thanks @kommander for finding it)

…hrough behaviour

observerEntries was a module global the Driver constructor overwrote with a
closure over itself, so the last driver (its VM, globals and host context)
stayed reachable after its run ended. It existed only for tests, so it is
removed rather than made opt-in.

The per-call pruning tests now check behaviour: WeakRefs to the ObservedCall
each finished durable call's before hook saw are collected while the run is
still alive, and a finished run is not retained after execute returns.

Thanks to #197 / @kommander for finding the retention and for the regression
test this adapts.
@jlongster
jlongster merged commit 00dd6b6 into dev Dec 3, 2026
1 check passed
@jlongster
jlongster deleted the fix/driver-no-observer-hook branch December 3, 2026 12:32
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.

1 participant