Skip to content

docs: Add comprehensive core loop architecture guide#12263

Closed
omer-bhutta wants to merge 1 commit into
anomalyco:devfrom
omer-bhutta:claude/document-repo-overview-N3kPz
Closed

docs: Add comprehensive core loop architecture guide#12263
omer-bhutta wants to merge 1 commit into
anomalyco:devfrom
omer-bhutta:claude/document-repo-overview-N3kPz

Conversation

@omer-bhutta

Copy link
Copy Markdown

What does this PR do?

This PR adds a detailed architectural documentation file (CORE_LOOP_ARCHITECTURE.md) that explains how OpenCode orchestrates multi-turn LLM interactions, manages tool execution, assembles context, and handles state transitions.

The document is written at a level of detail sufficient for a developer to reimplement the core loop from scratch. It covers:

  • Data Model (Section 1): Session, message, and part schemas; the tool state machine
  • Outer Loop (Section 2): The main SessionPrompt.loop() orchestrator, concurrency guards, exit conditions, and step limits
  • Processor (Section 3): Single-turn LLM streaming, tool lifecycle, retry logic, and doom loop detection
  • LLM Call (Section 4): System prompt assembly, tool filtering, model parameters, and the final streamText() invocation
  • Context Assembly (Section 5): Message array structure, system prompt components, conversation conversion, mid-loop reminders, and compaction filtering
  • Tool System (Section 6): Registry, resolution, context, and permission flow
  • Agent System (Section 7): Agent definitions, built-in agents, and permission rulesets
  • Context Window Management (Section 8): Overflow detection, compaction flow, and pruning
  • Subtask Execution (Section 9): How subtasks spawn child sessions
  • Cancellation (Section 10): Abort signal propagation
  • Complete Flow Diagram (Section 11): Visual representation of the entire flow
  • Key Design Decisions (Section 12): Rationale for architectural choices

This documentation serves as a reference for understanding the codebase and will help new contributors and maintainers understand the system's design without having to reverse-engineer it from code.

How did you verify your code works?

This is documentation only. The content was derived from careful analysis of the actual source code in packages/opencode/src/session/ and related modules. The document references specific file locations, function names, and line numbers that can be cross-referenced with the actual implementation. The architectural descriptions, data structures, and flow diagrams accurately reflect the current codebase structure and behavior.

https://claude.ai/code/session_013Gf8B4tpe3d39cb6t9ELAf

Detailed technical reference covering the multi-turn loop, processor,
context assembly, tool system, permission model, compaction, and state
management. Written at implementation-level detail for developer handoff.

https://claude.ai/code/session_013Gf8B4tpe3d39cb6t9ELAf
@github-actions

github-actions Bot commented Feb 5, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

@omer-bhutta omer-bhutta closed this Feb 5, 2026
@omer-bhutta
omer-bhutta deleted the claude/document-repo-overview-N3kPz branch February 5, 2026 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants