Skip to content

fix(core): backport invalidated location cleanup - #46074

Open
Hona wants to merge 2 commits into
anomalyco:v2from
Hona:location-cleanup
Open

fix(core): backport invalidated location cleanup#46074
Hona wants to merge 2 commits into
anomalyco:v2from
Hona:location-cleanup

Conversation

@Hona

@Hona Hona commented Aug 29, 2026

Copy link
Copy Markdown
Member

Summary

Temporarily backport Kit Langton's fix from Effect-TS/effect#7516, upstream commit 0e4793c87c18c5e26ad7c4f21e38f84c3a6bb128, onto the pinned Effect 4.0.0-rc.112.

This is a backport of the existing upstream work, not a separate implementation. The patch includes the TypeScript source and matching executable JavaScript. Remove it when upgrading to an Effect release that contains the fix.

Why

OpenCode's Location service map uses infinite idle TTL for existing directories. The server separately invalidates idle Locations after 60 minutes; explicit debug eviction can also invalidate them.

If Location A is invalidated while borrowed, the same key is reacquired as B, and A's last borrower then releases it, the unpatched RcMap checks only whether the key exists. With infinite TTL, it skips A's finalizers. With zero TTL, old cleanup can remove B's mapping. A surviving finite-TTL timer can also remove the replacement.

Kit's fix checks entry identity before removing a mapping, closes detached entries when their last borrower releases, and leaves replacement entries untouched. Ordinary TTL behavior and active borrowers are preserved.

Only the dependency patch and its package.json / bun.lock registration are included. No application API, Location eviction policy, test files, profiler, or changeset changes.

Before / After

Post-GC heap before and after 1000 replacement cycles, comparing unfixed and patched Effect

Version Before cycle 1 After cycle 1,000 + GC Resources finalized
Unfixed rc.112 2.69 MiB 1,004.70 MiB 1,000 / 2,000
Patched rc.112 2.69 MiB 4.56 MiB 2,000 / 2,000

The graph overlays two measured checkpoints per version, using the median of five fresh processes on Windows x64 / Bun 1.4.0. Each cycle acquires A, invalidates it while borrowed, acquires B, releases A, verifies B remains usable, then invalidates and releases B.

This is a synthetic resource workload, not 1,000 full production Location graphs. It uses the real LayerMap, Layer.fresh, and scope-owned timers. Each timer captures a distinct, filled 1-MiB Buffer; the resource finalizer cancels the timer. There are two resources per cycle. Plain unreachable data can be collected even when finalizers are missed, so the timers explicitly model resources whose callbacks require disposal.

Both versions use the same warm-up and eight full-GC rounds with 100-ms event-loop gaps. The map owner remains alive during measurement. Both maps have zero cached keys afterward, and all unrelated weak controls are collected. The patched run collects all payload sentinels; the unfixed run retains 1,000 and still misses those finalizers when the map owner closes.

Metric: bun:jsc.heapStats().heapSize, including external memory. These are not RSS figures or a prediction of production-server savings. The graph and probes are local validation artifacts, not committed files.

Verification

  • Local lifecycle matrix: 15 expected failures on unpatched rc.112; all 41 cases pass with the patch on Windows Bun 1.4.0 and repository-pinned Bun 1.3.14. Includes zero/finite/infinite TTL, multiple borrowers, stale timers, and ordinary cleanup controls. The 40 isolated RcMap/LayerMap cases also pass on Linux x64 / Bun 1.4.0 under WSL.
  • The production LocationServiceMap probe constructs the actual Instance graph with isolated paths and ambient discovery disabled. Before: only B finalizes, even after shutdown. After: A finalizes when released, B remains usable, and shutdown finalizes B. No network requests occur; baseline-only emergency teardown is performed after recording results.
  • Existing Location suites: 24 passed, 0 failed on Windows Bun 1.4.0, Windows Bun 1.3.14, and Linux Bun 1.4.0.
  • The 1,000-cycle patched probe also completes all finalizers and releases all payload sentinels with Windows Bun 1.3.14 and Linux x64 Bun 1.4.0 under WSL.
  • Core and Server bun typecheck passed; the pre-push workspace typecheck also passed all 33 tasks.
  • Repository-pinned Bun 1.3.14 install --frozen-lockfile passed with the patch registered.
  • package.json formatting and git diff --check passed.

The running OpenCode server was not patched or restarted for validation.

Hona added 2 commits August 29, 2026 13:26
Backport Kit Langton's RcMap fix from Effect-TS/effect#7516 (0e4793c87c18c5e26ad7c4f21e38f84c3a6bb128) to Effect 4.0.0-rc.112, including matching runtime JavaScript. Remove this temporary patch when upgrading to a fixed Effect release.
Match the JavaScript postimage hash produced by the patch hunks. Bun patch omitted its prepared file's extra EOF newline from the generated diff; the applied runtime code is unchanged.
Copilot AI lite review requested due to automatic review settings August 29, 2026 03:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

2 participants