How Context Isolation Works Between Parallel Branches in ADHD
ADHD enforces context isolation between parallel branches by spawning independent LLM calls via divergeBranch in engine.ts, each with fresh Idea arrays, frame-specific prompts, and no shared mutable state, ensuring branches explore solutions without cross-contamination until the convergent scoring phase.
The ADHD Tree-of-Thought engine implements a strict diverge-then-converge workflow to explore solution spaces in parallel. Understanding how context isolation between parallel branches works is critical for leveraging the framework's full potential, as it prevents conceptual leakage while maintaining diversity of thought. According to the UditAkhourii/adhd source code, this isolation is achieved through a combination of immutable data structures, independent prompt engineering, and bounded concurrency controls.
The Diverge-Then-Converge Architecture
ADHD's execution flow splits into distinct phases, with context isolation primarily enforced during the diverge phase (Phase 1). During this stage, the engine selects multiple cognitive frames and processes them through independent branches that operate as isolated sandboxes.
Phase 1: Parallel Branch Generation
In src/engine.ts, the run function orchestrates the workflow by first calling selectFrames from frames.ts to choose diverse perspectives. For each selected frame, the engine invokes divergeBranch to spawn an isolated exploration thread. These branches execute concurrently but remain conceptually sealed from one another until the scoring phase begins.
Five Mechanisms Enforcing Context Isolation
The ADHD source code implements five specific technical safeguards to maintain strict boundaries between parallel branches.
No Shared Mutable State
Each branch operates on a fresh Idea[] array created inside divergeBranch. The function at engine.ts:48-83 instantiates a new Branch object containing only the ideas generated for that specific frame, never reading from or writing to other branches' data structures. This immutable approach ensures that generated concepts cannot bleed across branch boundaries.
Independent LLM Prompts
The system prompt DIVERGE_SYSTEM and user prompts embed only the specific frame's description and the original problem statement. As implemented in engine.ts:51-58, the prompt assembly pulls from the divergeProblem variable and the individual frame.prompt, deliberately excluding any information from other active branches. This prompt-level isolation prevents the LLM from anchoring on solutions generated by parallel executions.
Parallel Execution with Bounded Concurrency
The engine uses p-limit to manage concurrency without compromising isolation. At engine.ts:52-62, each divergeBranch call executes within its own promise via limit(async () => { … }), ensuring that while branches run in parallel, they do not share variables or execution contexts. The concurrency parameter controls how many promises run simultaneously without breaking sandbox boundaries.
Problem Reframing Without Anchor Leakage
Before branching begins, reframeProblem processes the original problem once, stripping incidental anchors—such as current implementation stacks—that might bias specific branches. The resulting divergeProblem variable, set at engine.ts:31-38, passes unchanged to every branch, guaranteeing that no branch receives preferential contextual information that could influence its exploration direction.
Frame Selection Guarantees Diversity
The selectFrames function in frames.ts:34-47 ensures conceptual independence by shuffling the frame pool and injecting at least one "wild" frame per run. By returning distinct Frame objects for each execution, the system guarantees that branches start from fundamentally different cognitive perspectives, reinforcing isolation through diversity rather than similarity.
Implementation in the Source Code
The isolation logic centers on src/engine.ts, where the divergeBranch function serves as the primary isolation boundary. This function accepts the reframed problem, optional context, and a specific frame, then returns a self-contained Branch object. The parallel orchestration occurs within the run function's diverge phase, which maps over selected frames and executes divergeBranch calls through the concurrency limiter.
Key files implementing this architecture include:
src/engine.ts: Orchestrates the Tree-of-Thought flow and contains the diverge phase implementationsrc/frames.ts: Defines frames and selection logic ensuring diverse branch starting pointssrc/types.ts: Defines theBranch,Idea, andFrameinterfaces used throughout the isolation logic
Practical Code Examples
To leverage context isolation in your own implementations, use the high-level run API or manually invoke individual branches.
Running a full ADHD execution with multiple isolated branches:
import { run } from "./engine";
await run({
problem: "How can we reduce latency in a distributed cache?",
context: undefined,
framesPerRun: 5, // picks 5 distinct frames
ideasPerFrame: 6,
topK: 3,
concurrency: 4, // up to 4 branches run in parallel
codeMode: true,
stripAnchors: true,
});
Manually invoking a single isolated branch for custom workflows:
import { divergeBranch } from "./engine";
import { selectFrames } from "./frames";
const problem = "How can we reduce latency in a distributed cache?";
const frames = selectFrames(1, true); // pick one frame
const frame = frames[0];
const branch = await divergeBranch(
problem, // same (re-framed) problem for all branches
undefined, // no extra context
frame, // unique frame drives the prompt
6, // number of ideas to generate
undefined // default LLM model
);
console.log(branch);
Both examples demonstrate that each divergeBranch call operates with its own frame and produces a self-contained list of ideas, never accessing the results of other branches.
Summary
- Immutable branch data: Each parallel branch receives a fresh
Idea[]array indivergeBranch, preventing shared mutable state. - Prompt isolation: LLM prompts contain only the frame-specific context and reframed problem, excluding other branch outputs.
- Concurrency boundaries:
p-limitwraps each branch execution in isolated promises without shared variables. - Anchor stripping: The
reframeProblempreprocessing ensures all branches start from the same baseline without incidental biases. - Diverse framing:
selectFramesguarantees conceptual separation by assigning distinct cognitive frames to each branch.
Frequently Asked Questions
Can branches communicate with each other during the diverge phase?
No. According to the ADHD source code in engine.ts:48-83, branches are completely isolated during the diverge phase. Each divergeBranch call operates independently with its own LLM prompt and returns a separate Branch object. Communication only becomes possible during the convergent scoring and clustering phases after all branches complete.
What prevents one branch from influencing another's LLM generation?
The isolation relies on prompt engineering and state management. The prompts assembled at engine.ts:51-58 contain only the original problem and the specific frame's description, deliberately omitting content from other branches. Additionally, since each branch executes as a separate promise via p-limit at engine.ts:52-62, there is no shared memory or context between concurrent executions.
How does the concurrency parameter affect context isolation?
The concurrency setting in the run function controls how many branches execute simultaneously using p-limit, but it does not compromise isolation. Whether running 1 or 10 branches concurrently, each executes within its own sandboxed promise with independent variables and LLM calls. The parameter only affects resource utilization and rate limiting, not the conceptual boundaries between branches.
Why is problem reframing important for context isolation?
The reframeProblem step at engine.ts:31-38 strips incidental anchors—such as specific technology stacks or current implementation details—that might bias particular branches. By creating a single divergeProblem variable passed unchanged to all branches, the system ensures that no branch receives privileged contextual information, maintaining a level playing field for all parallel explorations.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →