From aradotso-trending-skills-37
Explores the decompiled TypeScript source of Claude Code v2.1.88 and its Python reimplementation (claw-code) to study the agent loop, tool system, slash commands, and memory architecture.
How this skill is triggered — by the user, by Claude, or both
Slash command
/aradotso-trending-skills-37:claude-code-source-analysisThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
```markdown
---
name: claude-code-source-analysis
description: Expertise in exploring, understanding, and extending the Claude Code decompiled source archive and its Python reimplementation (claw-code)
triggers:
- explore claude code source code
- understand claude code architecture
- analyze claude code internals
- work with claude code tools system
- study claude code agent loop
- implement claude code patterns
- extend claude code slash commands
- understand claude code memory system
---
# Claude Code Source Code Collection
> Skill by [ara.so](https://ara.so) — Daily 2026 Skills collection
A research repository containing the decompiled TypeScript source of Claude Code v2.1.88 (~163,318 lines across 1,884 files) and a clean-room Python reimplementation (`claw-code`). Use this repository to study, extend, or reimplement Anthropic's CLI coding agent.
---
## Repository Structure
collection-claude-code-source-code/ ├── claude-code-source-code/ # Decompiled TypeScript (v2.1.88) │ └── src/ │ ├── main.tsx # CLI entry + REPL bootstrap │ ├── query.ts # Core agent loop (785KB) │ ├── QueryEngine.ts # SDK/Headless lifecycle engine │ ├── Tool.ts # Tool interface + buildTool factory │ ├── commands.ts # Slash command definitions │ ├── tools/ # 40+ tool implementations │ ├── commands/ # ~87 slash command handlers │ ├── components/ # React/Ink terminal UI │ ├── services/ # Business logic layer │ ├── coordinator/ # Multi-agent coordination │ ├── memdir/ # Long-term memory management │ └── plugins/ # Plugin system ├── claw-code/ # Python clean-room rewrite (66 files) └── docs/ # Bilingual analysis (en/ + zh/)
---
## Installation & Setup
```bash
# Clone the repository
git clone https://github.com/chauncygu/collection-claude-code-source-code.git
cd collection-claude-code-source-code
# Explore the TypeScript source
cd claude-code-source-code
npm install # if package.json dependencies are needed for tooling
# Work with the Python rewrite
cd ../claw-code
pip install -r requirements.txt
The central execution model lives in src/query.ts. Understanding it is key to understanding the whole system.
// Simplified agent loop pattern from query.ts
async function* query(
userMessage: string,
context: ConversationContext,
tools: Tool[],
): AsyncGenerator<SDKMessage> {
// 1. Assemble system prompt from parts
const systemPromptParts = await fetchSystemPromptParts(context);
// 2. Run streaming tool executor with auto-compaction
const executor = new StreamingToolExecutor(tools);
while (true) {
const response = await callClaude({ systemPromptParts, userMessage, tools });
// 3. Yield streamed messages back to consumer
for await (const chunk of response) {
yield chunk;
}
// 4. Execute tool calls in parallel
const toolResults = await executor.runTools(response.toolCalls);
// 5. Auto-compact context if approaching token limit
if (shouldCompact(context)) {
await autoCompact(context);
}
if (!hasMoreToolCalls(response)) break;
}
}
main.tsx)// CLI bootstrap pattern
import { render } from 'ink';
import { App } from './components/App';
async function main() {
const args = parseArgs(process.argv.slice(2));
if (args.headless) {
// SDK/headless mode via QueryEngine
const engine = new QueryEngine(args);
await engine.run();
} else {
// Interactive REPL mode via React/Ink
render(<App initialArgs={args} />);
}
}
main().catch(console.error);
src/Tool.ts)interface Tool {
name: string;
description: string;
inputSchema: ZodSchema;
execute(input: unknown, context: ToolContext): Promise<ToolResult>;
}
// buildTool factory pattern
const MyTool = buildTool({
name: 'my_tool',
description: 'Does something useful',
inputSchema: z.object({
path: z.string().describe('File path to operate on'),
content: z.string().optional(),
}),
async execute({ path, content }, ctx) {
// tool implementation
return { type: 'text', text: `Processed ${path}` };
},
});
// File operations
import { FileReadTool } from './tools/FileReadTool';
import { FileEditTool } from './tools/FileEditTool';
import { FileWriteTool } from './tools/FileWriteTool';
// Code search
import { GlobTool } from './tools/GlobTool';
import { GrepTool } from './tools/GrepTool';
// Execution
import { BashTool } from './tools/BashTool';
// Web
import { WebFetchTool } from './tools/WebFetchTool';
import { WebSearchTool } from './tools/WebSearchTool';
// Sub-agents
import { AgentTool } from './tools/AgentTool';
// Memory
import { TodoWriteTool } from './tools/TodoWriteTool';
src/tools.ts)// Tool registration pattern
export function getTools(config: Config): Tool[] {
const baseTools = [
FileReadTool,
FileEditTool,
FileWriteTool,
GlobTool,
GrepTool,
BashTool,
];
if (config.enableWebTools) {
baseTools.push(WebFetchTool, WebSearchTool);
}
if (config.enableAgentTools) {
baseTools.push(AgentTool);
}
return baseTools;
}
src/commands/)// Pattern for defining a slash command
export const reviewCommand = {
name: 'review',
description: 'Review code changes',
aliases: ['/review'],
async execute(args: string[], context: CommandContext) {
const diff = await context.tools.bash.execute('git diff HEAD');
return context.query(`Please review these changes:\n${diff}`);
},
};
// Registration in commands.ts
export const SLASH_COMMANDS = [
reviewCommand,
commitCommand,
sessionCommand,
memoryCommand,
configCommand,
// ... ~87 total
];
| Command | Purpose |
|---|---|
/commit | Stage and commit changes |
/commit-push-pr | Commit, push, and open PR |
/review | Review current diff |
/resume | Resume last session |
/memory | Manage long-term memory |
/config | Edit configuration |
/skills | List available skills |
/permissions | Manage tool permissions |
/mcp | MCP server management |
/vim | Toggle vim keybindings |
// Three permission modes
type PermissionMode = 'default' | 'bypass' | 'strict';
// default → ask user before executing sensitive tools
// bypass → auto-allow all tools (headless/CI use)
// strict → auto-deny all unwhitelisted tools
// Permission rule structure
interface PermissionRule {
tool: string; // e.g. 'bash', 'file_write'
pattern?: string; // glob pattern for path-based rules
mode: PermissionMode;
}
// Checking permissions at runtime
async function checkPermission(
tool: Tool,
input: unknown,
rules: PermissionRule[],
): Promise<'allow' | 'deny' | 'ask'> {
const matchingRule = rules.find(r => matchesRule(r, tool, input));
if (matchingRule) return matchingRule.mode === 'bypass' ? 'allow' : 'deny';
return 'ask'; // default: prompt user
}
// Auto-compact strategies from query.ts
type CompactionStrategy =
| 'reactive' // compress when near token limit
| 'micro' // compress small incremental chunks
| 'trimmed'; // trim oldest turns first
async function autoCompact(
context: ConversationContext,
strategy: CompactionStrategy = 'reactive',
): Promise<void> {
const tokenCount = await estimateTokens(context.messages);
if (tokenCount > CONTEXT_COLLAPSE_THRESHOLD) {
const summary = await summarizeHistory(context.messages);
context.messages = [
{ role: 'system', content: summary },
...context.messages.slice(-KEEP_LAST_N_MESSAGES),
];
}
}
// Token estimation utility
function estimateTokens(messages: Message[]): number {
return messages.reduce((sum, m) => sum + Math.ceil(m.content.length / 4), 0);
}
src/memdir/)Claude Code implements a 7-layer memory architecture:
// Memory layer types
type MemoryLayer =
| 'working' // current session context window
| 'episodic' // compressed past session summaries
| 'semantic' // extracted facts and preferences
| 'procedural' // learned workflows and patterns
| 'external' // files, docs, codebases
| 'meta' // memory about memory (indexing)
| 'dream'; // background consolidation process
// Writing to long-term memory
import { TodoWriteTool } from './tools/TodoWriteTool';
// Memory entries are stored as structured markdown in ~/.claude/memory/
interface MemoryEntry {
id: string;
layer: MemoryLayer;
content: string;
embedding?: number[]; // for semantic search
createdAt: Date;
tags: string[];
}
src/coordinator/)// Sub-agent spawning via AgentTool
const result = await AgentTool.execute({
task: 'Analyze the test failures in src/utils/',
tools: ['file_read', 'grep', 'bash'],
context: {
maxTokens: 8192,
systemPrompt: 'You are a debugging specialist.',
},
});
// Coordinator pattern for parallel agents
class AgentCoordinator {
async runParallel(tasks: AgentTask[]): Promise<AgentResult[]> {
return Promise.all(tasks.map(task => this.spawnAgent(task)));
}
async runSequential(tasks: AgentTask[]): Promise<AgentResult[]> {
const results = [];
for (const task of tasks) {
results.push(await this.spawnAgent(task));
}
return results;
}
}
// Using QueryEngine for programmatic access
import { QueryEngine } from './QueryEngine';
const engine = new QueryEngine({
apiKey: process.env.ANTHROPIC_API_KEY,
model: 'claude-opus-4-5',
permissionMode: 'bypass', // for CI environments
tools: getTools({ enableWebTools: true }),
});
// Single query
const result = await engine.query('Refactor the auth module to use JWT');
// Streaming
for await (const chunk of engine.queryStream('Write tests for UserService')) {
process.stdout.write(chunk.text ?? '');
}
# claw-code mirrors the TypeScript architecture in Python
# Core agent loop
from claw_code.agent import ClawAgent
from claw_code.tools import FileReadTool, BashTool, GrepTool
agent = ClawAgent(
api_key=os.environ["ANTHROPIC_API_KEY"],
model="claude-opus-4-5",
tools=[FileReadTool(), BashTool(), GrepTool()],
)
# Run a task
result = agent.run("Find all TODO comments and create a summary report")
print(result)
# Async streaming
async for chunk in agent.stream("Refactor the database layer"):
print(chunk, end="", flush=True)
# Implementing a custom tool in claw-code
from claw_code.tools.base import BaseTool
from pydantic import BaseModel
class MyToolInput(BaseModel):
path: str
pattern: str
class MyCustomTool(BaseTool):
name = "my_custom_tool"
description = "Search files matching a pattern"
input_schema = MyToolInput
def execute(self, input: MyToolInput, context) -> str:
import subprocess
result = subprocess.run(
["grep", "-r", input.pattern, input.path],
capture_output=True, text=True
)
return result.stdout
// Model Context Protocol tool registration
import { MCPTool } from './tools/MCPTool';
// Connect to an MCP server
const mcpTool = new MCPTool({
serverUrl: process.env.MCP_SERVER_URL,
capabilities: ['resources', 'tools', 'prompts'],
});
// Tools exposed by MCP servers are auto-discovered
const mcpTools = await mcpTool.discoverTools();
// Returns standard Tool[] that integrate seamlessly
// Component pattern from src/components/
import React, { useState } from 'react';
import { Box, Text, useInput } from 'ink';
const AgentOutput: React.FC<{ messages: Message[] }> = ({ messages }) => {
return (
<Box flexDirection="column">
{messages.map((msg, i) => (
<Box key={i} marginBottom={1}>
<Text color={msg.role === 'assistant' ? 'cyan' : 'white'}>
{msg.role === 'assistant' ? '🤖 ' : '👤 '}
{msg.content}
</Text>
</Box>
))}
</Box>
);
};
// Custom hook for agent state
function useAgentState() {
const [messages, setMessages] = useState<Message[]>([]);
const [isThinking, setIsThinking] = useState(false);
async function sendMessage(text: string) {
setIsThinking(true);
for await (const chunk of queryStream(text)) {
setMessages(prev => appendChunk(prev, chunk));
}
setIsThinking(false);
}
return { messages, isThinking, sendMessage };
}
// Config structure (src/setup.ts)
interface ClaudeCodeConfig {
model: string; // Default: 'claude-opus-4-5'
permissionMode: PermissionMode; // Default: 'default'
maxTokens: number; // Default: 8192
enableWebTools: boolean;
enableVoice: boolean;
enableVim: boolean;
mcpServers: MCPServerConfig[];
memoryDir: string; // Default: ~/.claude/memory
apiKey: string; // From ANTHROPIC_API_KEY env var
}
// Config is stored at ~/.claude/config.json
// Editable via /config slash command or directly
# Environment variables used by the system
export ANTHROPIC_API_KEY="..." # Required
export CLAUDE_MODEL="claude-opus-4-5" # Optional override
export CLAUDE_PERMISSION_MODE="bypass" # For CI: bypass | strict | default
export CLAUDE_MCP_SERVER_URL="..." # Optional MCP server
export CLAUDE_MEMORY_DIR="~/.claude/memory"
// 1. Create src/tools/MyNewTool.ts
import { buildTool } from '../Tool';
import { z } from 'zod';
export const MyNewTool = buildTool({
name: 'my_new_tool',
description: 'What this tool does',
inputSchema: z.object({
input: z.string(),
}),
async execute({ input }, ctx) {
const result = await doSomething(input);
return { type: 'text', text: result };
},
});
// 2. Register in src/tools.ts
import { MyNewTool } from './tools/MyNewTool';
export function getTools(config) {
return [...existingTools, MyNewTool];
}
// src/commands/myCommand.ts
export const myCommand = {
name: 'my-command',
description: 'Does something useful',
usage: '/my-command [args]',
async execute(args: string[], ctx: CommandContext) {
const [target] = args;
return ctx.query(`Please do something with: ${target}`);
},
};
// Register in src/commands.ts
export const SLASH_COMMANDS = [...existingCommands, myCommand];
# English architecture analysis
cat docs/en/architecture-overview.md
# Deep dive PDF
open docs/claude-code-deep-dive-xelatex.pdf
# The source is decompiled — some types may need stubs
cd claude-code-source-code
ls stubs/ # Check existing stubs
# Add missing module stubs in stubs/ directory
cd claw-code
pip install -e . # Install in editable mode
python -c "from claw_code.agent import ClawAgent; print('OK')"
# Use grep to trace a concept through the codebase
grep -r "autoCompact" claude-code-source-code/src/ --include="*.ts" -l
grep -r "StreamingToolExecutor" claude-code-source-code/src/ --include="*.ts"
// Adjust compaction threshold in query.ts
const CONTEXT_COLLAPSE_THRESHOLD = 150_000; // tokens
const KEEP_LAST_N_MESSAGES = 20;
grep -r "name: 'commit'" claude-code-source-code/src/commands/ --include="*.ts"
npx claudepluginhub aradotso/trending-skillsAnalyzes decompiled Claude Code source code (v2.1.88) and a Python rewrite to understand the architecture, agent loop, tool system, and slash commands of a production AI coding agent.
Maximizes productivity with Anthropic's Claude Code CLI: shortcuts, hooks, MCPs, advanced configuration, workflows, CLAUDE.md, memory, sub-agents, permissions, and ecosystem integrations. Activate for configuring Claude Code, creating hooks, optimizing CLAUDE.md, using MCPs, resolving CLI errors, or advanced workflows.
Deep expertise in Claude Code CLI configuration, hooks, MCPs, CLAUDE.md, sub-agents, permissions, and workflows. Activate for setup, optimization, or troubleshooting of Claude Code features.