refactor(lifecycle): bootstrap as pure orchestration#25510
Merged
kitlangton merged 3 commits intodevfrom May 3, 2026
Merged
Conversation
f0da764 to
380aeff
Compare
kitlangton
commented
May 3, 2026
| @@ -1,7 +1,12 @@ | |||
| import { Context, Effect } from "effect" | |||
| import { Context, Effect, Scope } from "effect" | |||
kitlangton
commented
May 3, 2026
| import { Context, Effect } from "effect" | ||
| import { Context, Effect, Scope } from "effect" | ||
|
|
||
| export interface Interface { |
Wrap the synchronous subscribe(...) calls in Effect.forkScoped so they run in the background of FileWatcher's per-instance state scope, matching every other service (share-next, vcs, snapshot, file). State materialization no longer blocks on ParcelWatcher.subscribe; the subscribe still completes before its first event is delivered, but init returns immediately. This is the missing piece that lets InstanceBootstrap stop forking init() calls itself — every service now self-manages its own slow work against its own per-instance scope.
Move the Command.Event.Executed → setInitialized wiring out of InstanceBootstrap.run and into Project.init(). The subscription was added in #3677 directly to bootstrap when it was still a Promise function. With Project as a proper Effect Service it belongs there — the project is listening to its own /init event. Implementation matches the established InstanceState.make pattern (same shape as ShareNext, MCP, Plugin, etc.): the make body holds the subscription via Effect.forkScoped against the per-instance state scope, and init() just materializes the state. The public Interface.init returns Effect<void> — no Scope leak. Project.layer now requires Bus.Service in R; defaultLayer adds Layer.provide(Bus.defaultLayer) so consumers don't need to know.
Replace Effect.forkDetach with an awaited Effect.forEach. Each service's init() materializes its per-instance state, and the slow background work is forked inside the service's InstanceState.make body against the per-instance state scope (the established pattern across share-next, vcs, snapshot, and now file/watcher). Bootstrap is now pure orchestration: load config, await plugin.init, await each service.init in parallel. Returns when materialization completes. Failures are logged per-service and never propagate. Interface.run stays Effect<void> with R = never — no Scope leak in the public API. The fix to the original forkDetach leak lives in each service: their state-scoped forks die when the per-instance state scope is invalidated on dispose.
380aeff to
2a601a7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three small, focused refactors that fix the original
forkDetachleak by aligning every service to the establishedInstanceState.makepattern. No new lifecycle concepts, no new scopes, just consistency.The problem:
bootstrap.runusedEffect.forkDetachfor per-service init fibers. Those forks deliberately escaped any scope and ran forever — cleanup happened incidentally because each service also usedInstanceState.maketo register a per-directory disposer that fired on dispose. The asymmetry was the leak: fork lifetime was independent of instance lifetime, papered over by the registry walking.The fix: Move the slow work into each service's
InstanceState.makebody, whereEffect.forkScopedagainst the per-instance state scope handles cleanup natively. After this,bootstrap.runis pure orchestration: load config, await plugin, await each service's fastinit()materialization, return. Every service self-manages its own background work.Commits
refactor(file/watcher): self-fork ParcelWatcher subscribes— wrap the synchronoussubscribe(...)calls inEffect.forkScoped. FileWatcher was the only service that didn't already self-fork; this brings it in line withshare-next,vcs,snapshot,file. Subscribe still completes before its first event, butinit()no longer blocks on it.refactor(project): own /init command subscription via Project.init()— move theCommand.Event.Executed → setInitializedwiring out of bootstrap and into aProject.init()that uses theInstanceState.makepattern. Same shape asShareNext.init. Publicinit()returnsEffect<void>. Project.layer now requires Bus.refactor(bootstrap): pure orchestration, no fork— replaceforkDetachwith awaitedEffect.forEachover every service'sinit()(concurrent, discard, per-initcatchCause).Interface.runstaysEffect<void>withR = never.Why this shape
Each service already owns a per-instance scope via
InstanceState.make'sScopedCacheper-key scope. That scope dies on dispose (via the existinginstance-registrywalk). Bootstrap doesn't need to introduce a parallel scope mechanism — the existing one is sufficient if every service uses it consistently. This PR just makes that consistent.Test plan
bun run typecheckcleanbun run test test/project/ test/agent/plugin-agent-regression.test.ts test/file/watcher.test.ts— 77 pass / 1 skip / 0 failOut of scope
instance-registry.ts— the registry works and isn't load-bearing in a fragile way; aesthetic only. If we ever do retire it, the replacement is "letInstanceState.makeinstall its cleanup as a finalizer on a per-instance scope discoverable fromInstanceContext," but there's no current need.Effect.forkDetachinconfig/config.ts:577andcontrol-plane/workspace.ts:810(separate ownership; not bootstrap fibers).