How ADHD's Two-Phase Diverge-and-Focus Loop Works: A Deep Dive into the Creative Engine

ADHD implements a "Steve-Jobs-style" creative loop that strictly alternates between wide-angle idea generation (Diverge) and deep, focused refinement (Focus), implemented in src/engine.ts with separate system prompts for each phase.

The ADHD repository from UditAkhourii/adhd is an open-source creative problem-solving engine that mimics how breakthrough thinkers work: first expanding broadly across multiple cognitive angles, then selectively deepening the most promising paths. This two-phase Diverge-and-Focus loop is the core architectural pattern driving all creative output.

Understanding the Diverge Phase

The Diverge phase generates N parallel idea branches, each operating under a distinct cognitive frame defined in src/frames.ts. The system enters "ADHD-mode"—no criticism, no cross-talk, pure lateral exploration.

The phase executes through four sequential steps implemented in src/engine.ts【/cache/repos/github.com/UditAkhourii/adhd/main/src/engine.ts#L2-L15】:

  • divergeBranch — generates raw ideas per frame using the DIVERGE_SYSTEM prompt
  • scoreIdeas — evaluates every leaf on novelty, viability, and fit via SCORE_SYSTEM
  • clusterIdeas — groups scored ideas by underlying conceptual angle using CLUSTER_SYSTEM
  • Prune — selects top-K candidates for the Focus phase

Key Implementation Detail

The DIVERGE_SYSTEM prompt explicitly forbids convergence. Ideas are generated in isolation across frames, ensuring maximum coverage of the problem space before any judgment occurs.

Understanding the Focus Phase

The Focus phase transforms a selected idea into concrete actionable depth. Implemented by DEEPEN_SYSTEM in src/engine.ts【/cache/repos/github.com/UditAkhourii/adhd/main/src/engine.ts#L15-L21】, this phase "connects the dots" on a single promising leaf.

Each Focus pass produces four outputs:

  1. Sketch — a concrete mini-design showing how the idea works
  2. Load-bearing risk — the single most critical failure point
  3. First step — the immediate actionable implementation move
  4. Sub-ideas — 3-5 variations or downstream explorations

Critical Architectural Constraint

The two phases never intermix. Convergence happens only after divergence completes. This strict separation prevents premature optimization from killing nascent ideas.

Loop Continuation and Recursion

After Focus completes, the generated sub-ideas feed back into a new Diverge phase. This creates recursive expansion until termination:

  • Depth limit — configurable recursion ceiling (default: maxDepth)
  • Quality threshold — satisfaction-based early stopping
  • Budget exhaustion — token or API call limits

The loop structure:


[DIVERGE] → generate wide ideas
    ↓
[SCORE] → evaluate leaves
    ↓
[CLUSTER] → group by angle
    ↓
[PRUNE] → keep top-K
    ↓
[FOCUS] → deepen selected idea
    ↺ (restart on sub-ideas)

Running the Full ADHD Loop

The run function in src/index.ts orchestrates the complete two-phase loop with minimal configuration:

import { run } from "adhd";

const problem = "Create a lightweight note-taking app that works offline.";
const options = {
  ideasPerFrame: 4,      // ideas generated per cognitive frame
  maxDepth: 2,           // recursion depth for focus passes
  model: "gpt-4o-mini",  // LLM for all phases
};

run(problem, options)
  .then((result) => {
    console.log("Best idea:", result.bestIdea.text);
    console.log("Full tree:", JSON.stringify(result.tree, null, 2));
  })
  .catch(console.error);

Manual Phase Control

For custom pipelines, import individual phase functions from src/engine.ts:

import {
  reframeProblem,
  divergeBranch,
  scoreIdeas,
  clusterIdeas,
  deepenIdea,
} from "adhd/src/engine";

// Optional: reframe the problem for new angles
const { reframed } = await reframeProblem(problem, undefined, undefined);

// 1. DIVERGE — generate under each frame
const frames = await selectFrames(reframed, undefined);
const branches = await Promise.all(
  frames.map((f) => divergeBranch(reframed, undefined, f, 5, undefined))
);

// 2. SCORE — evaluate all ideas
const allIdeas = branches.flatMap((b) => b.ideas);
const scores = await scoreIdeas(reframed, allIdeas, undefined);

// 3. CLUSTER & PRUNE — group and filter
const clusters = clusterIdeas(scores);
const topIdeas = pruneClusters(clusters, 10);

// 4. FOCUS — deepen the winner
const focusResult = await deepenIdea(topIdeas[0], undefined);
console.log(focusResult);

Key Source Files

File Role
src/engine.ts Core loop implementation, system prompts (DIVERGE_SYSTEM, DEEPEN_SYSTEM, SCORE_SYSTEM, CLUSTER_SYSTEM)
src/frames.ts Cognitive frame definitions for Diverge phase diversity
src/render.ts Human-readable output formatting ("Focus — deepened branches")
src/types.ts Idea, Score, Branch type definitions
src/index.ts Public API exposing run()

Summary

  • Strict phase separation — Diverge and Focus never overlap; this protects idea diversity
  • Frame-based expansion — Multiple cognitive angles in src/frames.ts prevent local maxima
  • Scoring before clustering — Quantitative evaluation (novelty, viability, fit) enables data-driven pruning
  • Recursive depth — Sub-ideas from Focus automatically re-enter Diverge for compound exploration
  • Engine location — All orchestration lives in src/engine.ts with clear system prompt boundaries【/cache/repos/github.com/UditAkhourii/adhd/main/src/engine.ts#L2-L21】

Frequently Asked Questions

How does ADHD prevent premature convergence during the Diverge phase?

The DIVERGE_SYSTEM prompt explicitly instructs the LLM to generate ideas without criticism or cross-referencing. Each frame operates in isolation, and the SCORE_SYSTEM evaluation only occurs after all ideas exist. This "no judgment until divergence ends" rule is hardcoded in the phase sequence within src/engine.ts.

What cognitive frames does ADHD use and where are they defined?

Frames are defined in src/frames.ts and selected dynamically based on problem characteristics. Common frames include constraint inversion, first-principles decomposition, analogy transfer, and extreme user perspectives. The frame selection itself can be customized via the selectFrames function in the engine.

Can I run only the Diverge or Focus phase independently?

Yes. The engine exports divergeBranch, scoreIdeas, clusterIdeas, and deepenIdea as standalone async functions. Import them directly from adhd/src/engine to build custom pipelines, though you must manually handle the output-to-input plumbing that run() automates.

What termination conditions stop the recursive loop?

Three built-in conditions: (1) maxDepth recursion limit in options, (2) quality threshold satisfaction when scores plateau, and (3) implicit budget limits via token or API constraints. The loop always completes at least one full Diverge-Focus cycle before checking termination.

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:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →