Skip to content

Commit 2676b35

Browse files
opencodeclaude
andcommitted
fix(mcp): construction-counter test can now go red, plus docstring fixes (TKT-323 chunk 2)
Henry's review: the construction-counter test certified nothing -- the counted node was reachable from only ONE compiled group, so the assertion read 1 regardless of whether MemoMap sharing worked at all. Fixed by putting the same node in BOTH compiled groups (matching TKT-349's own Left/Right shape) and asserting both directions: 1 shared, 2 split. Verified this new version actually fails when the mechanism is broken (a deliberate, reverted sanity check), not just that it passes. Also: deleted the test's own claim that "a single Effect execution memoizes a layer's construction across everywhere it's provided" -- false, and precisely what TKT-349's own fix disproved. Added the construction-counter-test pattern as a standing FORK.md rule. Recorded the Effect.die-on-None rationale explicitly in McpRuntimeLive's docstring (Ethan and Henry's endorsement) so it isn't "fixed" back to a typed failure later. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JJQrqYMv43CrdbjpTT7884
1 parent 3477109 commit 2676b35

3 files changed

Lines changed: 78 additions & 44 deletions

File tree

‎FORK.md‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,12 @@ be the MemoMap-split defect above, moved into a closure where no graph analysis
336336
real, considered, and rejected alternative on this ticket). `Effect.serviceOption` reads the
337337
ambient Context without adding a static requirement, so the layer stays `Layer.succeed` (`R =
338338
never`) and reuses whatever single instance the assembly's own `app`/`AppLayer` tree already
339-
constructed — proven in `packages/core/test/config/mcp-runtime.test.ts`'s construction-counter
340-
test, the same instrument that caught the SessionExecutionLocal split-brain above. **Before
339+
constructed. The underlying mechanism this reuse depends on — a shared MemoMap dedupes a node
340+
reachable from two separate compiles — is proven directly by
341+
`packages/core/test/config/mcp-runtime.test.ts`'s construction-counter test, the same instrument
342+
that caught the SessionExecutionLocal split-brain above; the ambient `Effect.serviceOption` read
343+
itself has no graph edge to duplicate in the first place, which is the point, not something a
344+
counter needs to separately re-prove. **Before
341345
building an ambient-read adapter, probe first**: confirm the target service is actually present in
342346
the ambient context at the real call site (not assumed) — an adapter reading ambient context that
343347
is never populated there is permanently, silently unavailable, which is a design defect the same
@@ -350,6 +354,17 @@ requirement). It is **no defence at all** against a **compile-time** rejection o
350354
in a shared group, because no effect ever runs — compilation rejects the graph first. Two
351355
different sites; both statements are true; conflating them costs real diagnosis time.
352356

357+
**A construction-counter test asserts the deduped count AND the split count, or it is not a
358+
guard.** This ticket's own first draft of the test above put the counted node reachable from only
359+
ONE compiled group and asserted `constructions === 1` — that assertion is true whether or not
360+
memoMap sharing works at all, because there was never a second construction site to duplicate it.
361+
A counter test with no reachable failure mode is not a regression guard, it is a tautology dressed
362+
as one. The correct shape (both this ticket's test and TKT-349's original) puts the SAME node
363+
reachable from two SEPARATE `compile()` calls and asserts both directions: `=== 1` under a shared
364+
MemoMap, `=== 2` under separate ones. Before trusting a construction-counter test, break the
365+
mechanism it claims to guard on purpose and confirm the assertion actually fails — a test that
366+
cannot go red is not evidence.
367+
353368
## Merge-blocking gates
354369

355370
These gates are **merge-blocking conditions**, not aspirations. A PR that trips one does not

‎packages/core/test/config/mcp-runtime.test.ts‎

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { 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"
33
import { McpRuntime } from "@opencode-ai/core/config/mcp-runtime"
44
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
55
import { 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
})

‎packages/opencode/src/mcp/runtime.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ const layer = Layer.succeed(
4949
const mcp = yield* Effect.serviceOption(MCP.Service)
5050
const store = yield* Effect.serviceOption(InstanceStore.Service)
5151
const location = yield* Effect.serviceOption(Location.Service)
52+
// A DEFECT (Effect.die), never McpRuntime.UnavailableError, when this adapter is
53+
// wired -- Ethan and Henry's TKT-323 verdict, recorded here rather than left to be
54+
// "fixed" back to a typed failure later. UnavailableError means "this assembly has no
55+
// live MCP runtime," which is a legitimate, expected state the port's own bound default
56+
// already answers correctly for packages/cli/sdk-next. None reaching HERE means the
57+
// opposite: this assembly DOES have a runtime -- McpRuntimeLive was deliberately wired
58+
// in, replacing the default -- and the adapter still could not reach it. That is a wiring
59+
// regression, not an absence. Answering UnavailableError for it would be indistinguishable
60+
// from the legitimate case to a client, inviting retries against a lie that will never
61+
// resolve; a die surfaces loudly instead, which is what an internal wiring bug should do.
5262
if (Option.isNone(mcp) || Option.isNone(store) || Option.isNone(location))
5363
return yield* Effect.die(
5464
new Error(

0 commit comments

Comments
 (0)