#1534·opentui

`Renderable.remove()` uses `instanceof` instead of the package's own `isRenderable()` brand check, breaking across a dual-module-identity split

Author: letrquanCreated Sep 24, 2026Updated Sep 24, 2026

Environment

  • OS: Windows 11 (not platform-specific)
  • Runtime: Bun 1.4.0
  • @opentui/core: 0.5.12 (also latest published as of 2026-09-24)

Summary

@opentui/core's package.json exports map gives . a "bun" condition (index.bun.js) ahead of "node"/"import" (index.node.js) -- two separately compiled bundles, each with its own Renderable/BaseRenderable class objects (confirmed by reading both chunks: each defines its own class BaseRenderable2 extends EventEmitter with its own Symbol.for("@opentui/core/Renderable") brand field). Any consumer whose module graph resolves @opentui/core through both conditions at once (a bundler or test runner's SSR/transform resolver picking one condition for source it processes, while another part of the graph -- e.g. a prebuilt, natively-loaded chunk -- picks the other) ends up with two non-identical class hierarchies for what is nominally "the same" package and version.

The package already anticipates this and has the correct primitive for it: Renderable.ts defines isRenderable(obj) as a brand check via Symbol.for("@opentui/core/Renderable"), a global symbol registry lookup that works correctly across separately-loaded module copies. add() and insertBefore() both correctly route an incoming child through maybeMakeRenderable(), which uses isRenderable():

typescript
// packages/core/src/renderables/composition/vnode.ts
export function maybeMakeRenderable(ctx: RenderContext, node: Renderable | VNode<any, any[]> | unknown): Renderable | null {
  if (isRenderable(node)) return node
  if (isVNode(node)) return instantiate(ctx, node)
  ...
}

insertBefore's anchor check does the same:

typescript
if (!isRenderable(anchor)) {
  throw new Error("Anchor must be a Renderable")
}

But remove() does not use isRenderable() -- it uses instanceof against the module-local class:

typescript
// packages/core/src/Renderable.ts
public remove(child: BaseRenderable): void {
  if (!(child instanceof BaseRenderable)) {
    throw new Error("remove expects a renderable child object")
  }
  ...
}

This is confirmed still present on main (checked directly via gh api repos/anomalyco/opentui/contents/packages/core/src/Renderable.ts), not fixed in an unreleased version.

The practical consequence: a renderable built by a "foreign" copy of the package can be successfully added as a child (accepted by add()/ insertBefore()'s brand check) but can never be removed again through the public API (rejected by remove()'s instanceof check) -- an asymmetry that turns every subsequent teardown of that child (including CliRenderer.destroy()'s own recursive teardown, and React's clearContainer host-config hook, see 02-createroot-render-leaks-effects.md in this same directory) into a thrown, uncaught Error: remove expects a renderable child object.

Minimal repro

Only one renderer/context exists in this repro. The only thing "foreign" about foreignBox is which loaded copy of @opentui/core its class came from -- its render context is genuinely the one live renderer's own, matching the real hazard precisely (one renderer; some renderable classes resolved through a different copy of the package than the renderer itself).

typescript
// repro.ts -- run with `bun repro.ts` from a directory with
// @opentui/[email protected] installed in ./node_modules
import { isRenderable as isRenderableA, BaseRenderable as BaseRenderableA } from "@opentui/core";
import { createTestRenderer as createTestRendererA } from "@opentui/core/testing";

// Force the OTHER bundle by importing its file directly, bypassing
// package.json "exports" entirely -- this stands in for the real trigger (a
// bundler/test-runner resolving "node" for @opentui/core in one part of the
// module graph while another part resolves "bun", e.g. Vitest's SSR resolver
// vs. a prebuilt chunk loaded by Bun's own loader).
import {
  isRenderable as isRenderableB,
  BaseRenderable as BaseRenderableB,
  BoxRenderable as BoxRenderableB,
} from "./node_modules/@opentui/core/index.node.js";

const { renderer } = await createTestRendererA({ width: 20, height: 5 });
console.log("BaseRenderableA === BaseRenderableB:", (BaseRenderableA as any) === (BaseRenderableB as any));

// The renderer's OWN real context, used to construct a bundle-B class --
// no second renderer, no mixing of unrelated contexts.
const ctx = (renderer.root as any)._ctx;
const foreignBox = new (BoxRenderableB as any)(ctx, { id: "foreign-box" });

console.log("isRenderableA(foreignBox):", isRenderableA(foreignBox));
console.log("isRenderableB(foreignBox):", isRenderableB(foreignBox));
console.log("foreignBox instanceof BaseRenderableA:", foreignBox instanceof (BaseRenderableA as any));
console.log("foreignBox instanceof BaseRenderableB:", foreignBox instanceof (BaseRenderableB as any));

console.log("add() index:", (renderer.root as any).add(foreignBox));
try {
  (renderer.root as any).remove(foreignBox);
  console.log("remove() succeeded");
} catch (err) {
  console.log("remove() THREW:", (err as Error).message);
}

// Second failure path: CliRenderer.destroy()'s own recursive teardown hits
// the same foreign child (caught and logged there, not rethrown).
await renderer.destroy();

Steps

  1. bun install @opentui/[email protected] in a scratch directory.
  2. Save the snippet above as repro.ts in that same directory.
  3. bun repro.ts.

Expected vs actual

Expected: either both add() and remove() accept the foreign-bundle object (consistent with isRenderable() saying it's a real renderable), or both reject it consistently.

Actual, every run:

BaseRenderableA === BaseRenderableB: false
isRenderableA(foreignBox): true
isRenderableB(foreignBox): true
foreignBox instanceof BaseRenderableA: false
foreignBox instanceof BaseRenderableB: true

add() index: 0
remove() THREW: remove expects a renderable child object
Error destroying root renderable: Error: remove expects a renderable child object
    at remove (...\node_modules\@opentui\src\Renderable.ts:1330:17)
    at destroy (...\node_modules\@opentui\core\chunk-node-fyb4fckk.js:1426:19)
    at destroyRecursively (...)
    ...

add() accepts the object; remove() throws on the same object immediately after, on the SAME renderer, with no second context involved; and the same error recurs (caught and logged, not rethrown, in this path) when the test renderer's own destroy() recursively tears down its tree and hits the same foreign child.

Notes / possible cause

The fix is a one-line change to match the pattern insertBefore and add already use:

diff
 public remove(child: BaseRenderable): void {
-  if (!(child instanceof BaseRenderable)) {
+  if (!isRenderable(child)) {
     throw new Error("remove expects a renderable child object")
   }

isRenderable is already imported/available in Renderable.ts (it's defined in the same file). This wouldn't fix every consequence of a dual-module- identity split (a foreign object still wouldn't satisfy other instanceof checks scattered through the codebase, and BaseRenderable2 !== BaseRenderable2 remains true across the split), but it would at least make remove() consistent with add()/insertBefore()'s existing brand-check contract, and would fix the specific "can add but can never remove" asymmetry this report demonstrates. The presence of singletonCacheSymbol = Symbol.for("@opentui/core/singleton") elsewhere in the codebase suggests duplicate module copies are already a known, planned-for condition.

Related issues

  • None found with matching keywords (remove expects a renderable, instanceof, "dual package", isRenderable) via gh search issues --repo anomalyco/opentui.
  • Issue #432 ("Bug: conditional sibling causes insertBefore crash in <box>", open) and its follow-up comment thread hit a related but different symptom: LineNumberRenderable's own overridden remove() throwing when React's reconciler tries to unmount a child it manages specially. Same general fragility around host-config lifecycle methods and remove(), not a duplicate of this specific instanceof-vs-brand-check issue.
  • See also 02-createroot-render-leaks-effects.md in this same directory: @opentui/react's clearContainer host-config hook calls container.remove(child) for every existing child on a root's first commit, which is exactly the call site that surfaces this bug as an uncaught crash (rather than the silent effect leak 02 describes) when a consumer's createRoot(...).render() calls straddle a dual-module-identity split.