11import { describe , expect } from "bun:test"
2- import { Cause , Context , Effect , Exit , Layer , Option } from "effect"
2+ import { Cause , Context , Effect , Exit , Layer , Option , Scope } from "effect"
33import { McpRuntime } from "@opencode-ai/core/config/mcp-runtime"
44import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
55import { LayerNode } from "@opencode-ai/core/effect/layer-node"
@@ -51,18 +51,19 @@ describe("McpRuntime", () => {
5151 expect ( status ) . toEqual ( { example : { status : "connected" } } )
5252 } ) )
5353
54- // TKT-323 chunk 2, Ethan's ruling: the same instrument that caught the SessionExecutionLocal
55- // split-brain (TKT-349), applied to this ticket's shape. McpRuntimeLive reads MCP.Service and
56- // InstanceStore.Service via `Effect.serviceOption` INSIDE its status() method body, never via a
57- // `deps:` graph edge -- the whole point being that it reuses whatever single instance the
58- // httpapi assembly's `app` build already constructed, rather than building its own. This proves
59- // that reuse actually holds: a global-analog service built once via one compile (standing in
60- // for `app`), and a second, SEPARATE compile (standing in for `locationServices`) whose only
61- // node reads that same service ambiently, share a memoMap and construct the underlying service
62- // exactly once when merged -- not twice, which would be a live, silently duplicated MCP.Service
63- // or InstanceStore.Service, invisible to every functional assertion (see FORK.md's
64- // "process singleton is a singleton per MemoMap" entry, TKT-349).
65- it . effect ( "an ambient serviceOption read reuses the sibling compile's instance, not a second one" , ( ) =>
54+ // TKT-323 chunk 2, Ethan's ruling, corrected per Henry's review: the same instrument that
55+ // caught the SessionExecutionLocal split-brain (TKT-349), applied to this ticket's shape.
56+ //
57+ // The FIRST version of this test asserted constructions === 1 with `globalNode` reachable from
58+ // only ONE compiled group (appTree) and `locationTree` reading it purely via
59+ // `Effect.serviceOption`, never as a graph member. That is not a guard: with no second
60+ // construction site anywhere in the test, the assertion reads 1 whether or not memoMap sharing
61+ // works at all -- it cannot go red. A construction-counter test asserts the DEDUPED count AND
62+ // the SPLIT count, or it is not a guard (see FORK.md's canonical pattern line). Fixed by putting
63+ // the SAME global node in BOTH compiled groups -- matching TKT-349's own Left/Right shape
64+ // exactly -- and asserting both sides: constructions === 1 under a shared MemoMap, === 2 under
65+ // separate ones.
66+ it . effect ( "a node reachable from two separate compiles constructs once shared, twice split" , ( ) =>
6667 Effect . gen ( function * ( ) {
6768 let constructions = 0
6869 class Global extends Context . Service < Global , { value : string } > ( ) ( "test/McpRuntime/Global" ) { }
@@ -73,41 +74,49 @@ describe("McpRuntime", () => {
7374 return Global . of ( { value : "shared" } )
7475 } ) ,
7576 )
76-
7777 const globalNode = LayerNode . make ( { service : Global , layer : globalLayer , deps : [ ] } )
78- // Stands in for `app`'s LayerNode.group([..., MCP.node, ..., InstanceStore.node, ...]):
79- // a direct, tracked consumer of the global service.
80- const appTree = LayerNode . compile ( LayerNode . group ( [ globalNode ] ) ) as Layer . Layer < Global >
8178
82- // Stands in for locationServices' McpRuntime.node: reads the SAME tag via serviceOption,
83- // deps: [] -- no graph edge to globalNode at all, exactly McpRuntimeLive's own shape.
84- const ambientLayer = Layer . succeed (
85- McpRuntime . Service ,
86- McpRuntime . Service . of ( {
87- status : ( ) =>
88- Effect . gen ( function * ( ) {
89- const global = yield * Effect . serviceOption ( Global )
90- return { seen : { status : Option . isSome ( global ) ? "connected" : "disabled" } }
91- } ) ,
79+ // Stands in for `app`'s LayerNode.group([..., MCP.node, ..., InstanceStore.node, ...]): a
80+ // direct, tracked consumer of the global service.
81+ const appNode = LayerNode . make ( {
82+ service : McpRuntime . Service ,
83+ layer : Layer . succeed ( McpRuntime . Service , McpRuntime . Service . of ( { status : ( ) => Effect . succeed ( { } ) } ) ) ,
84+ deps : [ globalNode ] ,
85+ } )
86+ const appLayer = LayerNode . compile ( LayerNode . group ( [ appNode ] ) ) as Layer . Layer < McpRuntime . Service >
87+
88+ // Stands in for `locationServices`' McpRuntime.node reaching the SAME global service via a
89+ // second, independent compile -- the shape this test actually needs to distinguish deduped
90+ // from split, not the ambient-serviceOption shape McpRuntimeLive itself uses (that shape has
91+ // no graph edge to duplicate in the first place; this test is the standing guard for the
92+ // mechanism the ambient read relies on, not a re-test of McpRuntimeLive's own code).
93+ const locationNode = LayerNode . make ( {
94+ service : McpRuntime . Service ,
95+ layer : Layer . succeed ( McpRuntime . Service , McpRuntime . Service . of ( { status : ( ) => Effect . succeed ( { } ) } ) ) ,
96+ deps : [ globalNode ] ,
97+ } )
98+ const locationLayer = LayerNode . compile ( LayerNode . group ( [ locationNode ] ) ) as Layer . Layer < McpRuntime . Service >
99+
100+ constructions = 0
101+ yield * Effect . scoped (
102+ Effect . gen ( function * ( ) {
103+ const scope = yield * Scope . Scope
104+ // Each build gets its OWN fresh MemoMap -- the unshared case.
105+ yield * Layer . buildWithMemoMap ( appLayer , Layer . makeMemoMapUnsafe ( ) , scope )
106+ yield * Layer . buildWithMemoMap ( locationLayer , Layer . makeMemoMapUnsafe ( ) , scope )
92107 } ) ,
93108 )
94- const locationTree = LayerNode . compile (
95- LayerNode . group ( [ makeLocationNode ( { service : McpRuntime . Service , layer : ambientLayer , deps : [ ] } ) ] ) ,
96- ) as Layer . Layer < McpRuntime . Service >
109+ expect ( constructions ) . toBe ( 2 )
97110
98- // Both "sides" of the real composition consume the global service in one execution --
99- // app's own direct dependent, and McpRuntime's status() via serviceOption -- exactly how
100- // createRoutes()'s own pipe merges locationServiceMapV2 and AppNodeBuilderV1.build(app)
101- // into ONE overall Layer, built once at server startup. No manual memoMap threading is
102- // needed here: a single Effect execution memoizes a layer's construction across everywhere
103- // it's provided within that one run, which is what this asserts.
104111 constructions = 0
105- yield * Effect . gen ( function * ( ) {
106- yield * Global
107- const runtime = yield * McpRuntime . Service
108- yield * runtime . status ( )
109- } ) . pipe ( Effect . provide ( locationTree ) , Effect . provide ( appTree ) )
110-
112+ yield * Effect . scoped (
113+ Effect . gen ( function * ( ) {
114+ const scope = yield * Scope . Scope
115+ const sharedMemoMap = Layer . makeMemoMapUnsafe ( )
116+ yield * Layer . buildWithMemoMap ( appLayer , sharedMemoMap , scope )
117+ yield * Layer . buildWithMemoMap ( locationLayer , sharedMemoMap , scope )
118+ } ) ,
119+ )
111120 expect ( constructions ) . toBe ( 1 )
112121 } ) )
113122} )
0 commit comments