原汁原昧 Claude Code 可运行,可构建, 可调试版; 生产级工程化, 企业级可靠性; 安全无毒, 内存泄露修复
Go to file
unraid f2e9af4927 feat: harden autonomy lifecycle, OOM bounds, and provider-boundary finalization
This PR consolidates a coordinated batch of fixes around autonomy run/flow lifecycle, scheduled task deduplication, provider-boundary state finalization, and matching memory-bound treatments for adjacent long-running subsystems (REPL fullscreen scrollback, skill-search/skill-learning runtime activation). All changes were developed and reviewed together because they touched the same lifecycle invariants and were uncovered by the same long-running session reproductions.

## Lifecycle correctness

- Queued autonomy prompts are not injected unless the persisted run was successfully claimed; queued run claiming is now terminal-safe so a once-consumed/cancelled/failed run can not slip back into `queued`.
- Autonomy run/flow finalization happens on completion, provider error, generator close, and cancellation — not just the happy path. New `src/__tests__/queryAutonomyProviderBoundary.test.ts` covers these provider-boundary transitions.
- `requestManagedAutonomyFlowCancel` and `resumeManagedAutonomyFlowPrompt` carry `rootDir` and `currentDir` explicitly across detached async boundaries (proactive-tick, cron, daemon restart) instead of inferring from process state.
- Active runs/flows are protected from janitor pruning so a running step can not be garbage-collected mid-flight (`src/utils/autonomyAuthority.ts`).
- Heartbeat parser now ignores fenced code blocks; the two-phase commit window for autonomy state transitions is documented in `docs/internals/autonomy-jira.md`.

## Ownership and dedup

- `src/utils/autonomyRuns.ts`: ownership stamping (run id + rootDir carried end-to-end), source-based dedup against active runs.
- `src/hooks/useScheduledTasks.ts`: scheduled ticks deduplicate against runs already active on the same source label.
- `src/utils/processUserInput/processSlashCommand.tsx`: forked slash commands now thread the autonomy `runId` so completion finalizers can find the originating run for deferred completion.
- New `src/utils/autonomyQueueLifecycle.ts` and tests collect the queue-side lifecycle invariants in one place.

## Memory bounds (related, same review pass)

- `src/screens/REPL.tsx`: caps fullscreen scrollback after the compact boundary and updates trailing progress rows in place. Long-running fullscreen sessions could otherwise retain thousands of post-compaction messages and duplicate progress rows, keeping Ink trees alive long after their useful context had moved on.
- `src/services/skillSearch/*` and `src/services/skillLearning/*`: runtime activation is strictly opt-in via existing env toggles; session caches are capped so long-running processes can not grow them forever. Build presence is preserved so operators can still discover and opt into the slash commands.

## CI / test contract

- `tests/integration/dependency-overrides.test.ts`: smoke test no longer drives Mermaid's browser renderer; it validates the package-resolution contract directly so CI does not regress on unrelated browser timing.
- New `tests/integration/autonomy-lifecycle-user-flow.test.ts`: end-to-end CLI subprocess flow exercising `status --deep`, `flows`, `flow <id>`, `flow resume`, `flow cancel` against persisted state.
- `src/entrypoints/cli.tsx`: `claude autonomy …` routes through an entrypoint fast path that reuses the slash-command formatter without booting the full interactive CLI. Stdout is flushed before forced exit so coverage subprocesses do not terminate with empty stdout.
- `packages/builtin-tools/src/tools/RemoteTriggerTool/__tests__/RemoteTriggerTool.test.ts`: stabilized to prevent audit flake under coverage.

## Tests added

- `src/__tests__/queryAutonomyProviderBoundary.test.ts`
- `src/hooks/__tests__/useScheduledTasks.test.ts`
- `src/utils/__tests__/autonomyAuthority.test.ts`
- `src/utils/__tests__/autonomyFlows.test.ts` (extended)
- `src/utils/__tests__/autonomyPersistence.test.ts` (extended)
- `src/utils/__tests__/autonomyQueueLifecycle.test.ts`
- `src/utils/__tests__/autonomyRuns.test.ts` (extended)
- `src/utils/processUserInput/__tests__/processSlashCommand.test.ts`
- `tests/integration/autonomy-lifecycle-user-flow.test.ts`

## Docs

- `docs/agent/sur-loop-scheduled-oom.md`: System Understanding Report covering the scheduled/loop OOM problem, the call graphs investigated, and the lifecycle invariants this PR establishes.
- `docs/agent/sur-skill-overflow-bugs.md`: SUR for the related skill-overflow context.
- `docs/internals/autonomy-jira.md`: documents the two-phase commit window and ownership stamping invariants.
- `docs/memory-leak-audit.md`: audit notes covering the REPL/scrollback and skill-search bounds.

## Invariants this PR establishes

1. Queued autonomy prompts are not injected unless the persisted run was successfully claimed.
2. Terminal run/flow states are terminal — completion, failure, and cancellation all finalize state regardless of which provider/error path triggered them.
3. Autonomy run/flow `rootDir` is carried explicitly across detached async boundaries instead of inferred from a shared singleton.
4. State-only CLI subcommands (`autonomy status|runs|flows|flow …`) bypass full interactive bootstrap so they do not hold unrelated handles open.
5. REPL fullscreen scrollback and skill-search/skill-learning session caches are explicitly bounded.

## Validation

```bash
bun run typecheck
CI=true GITHUB_ACTIONS=true bun test            # 3996 pass / 0 fail across 305 files
bun test src/__tests__/queryAutonomyProviderBoundary.test.ts \
         src/hooks/__tests__/useScheduledTasks.test.ts \
         src/utils/__tests__/autonomy{Runs,Flows,Authority,QueueLifecycle,Persistence}.test.ts \
         src/utils/processUserInput/__tests__/processSlashCommand.test.ts \
         tests/integration/autonomy-lifecycle-user-flow.test.ts
```

## Origin

This PR is the consolidated, upstream-targeted version of two fork-side review PRs (fix/loop-scheduled-autonomy-oom and fix/autonomy-lifecycle). The fork-side review history is preserved at https://github.com/amDosion/claude-code-bast/pull/7 . The fork's own internal `chore: keep fork current with upstream` sync commits and the `docs: update contributors` automation are intentionally not included in this PR.

The autonomy CLI handler `rootDir` threading that the fork added (78f64d8a, 98d04ddb) is intentionally omitted here because upstream `a2cfaf91` (fix: 修复 RemoteTriggerTool 和 autonomy 测试的全量运行失败) already performed the equivalent change with an additional `currentDir` option. Keeping the upstream version avoids regressing that improvement.
2026-04-29 14:04:27 +08:00
.claude fix: 修复 anthropic 煞笔的四个 bug (#352) 2026-04-24 20:07:18 +08:00
.github/workflows fix: resolve dependency audit findings precisely (#361) 2026-04-26 19:49:54 +08:00
.vscode fix: 删除 debug 限制 2026-04-07 16:13:15 +08:00
docs feat: harden autonomy lifecycle, OOM bounds, and provider-boundary finalization 2026-04-29 14:04:27 +08:00
learn Fix mintlify validate errors (#367) 2026-04-26 11:07:20 +08:00
packages feat: harden autonomy lifecycle, OOM bounds, and provider-boundary finalization 2026-04-29 14:04:27 +08:00
scripts feat: harden autonomy lifecycle, OOM bounds, and provider-boundary finalization 2026-04-29 14:04:27 +08:00
src feat: harden autonomy lifecycle, OOM bounds, and provider-boundary finalization 2026-04-29 14:04:27 +08:00
teach-me Fix mintlify validate errors (#367) 2026-04-26 11:07:20 +08:00
tests feat: harden autonomy lifecycle, OOM bounds, and provider-boundary finalization 2026-04-29 14:04:27 +08:00
vendor feat: enable /voice mode with native audio binaries 2026-04-03 21:14:44 +08:00
.dockerignore feat: 支持自托管的 remote-control-server (#214) 2026-04-09 17:40:50 +08:00
.editorconfig feat: 配置 Biome 代码格式化与校验工具 2026-04-01 01:40:27 +08:00
.gitignore feat: 集成豆包 ASR 语音识别后端,支持 /voice doubao 切换 (#357) 2026-04-25 13:57:30 +08:00
.impeccable.md feat: ACP 协议版本 remote control (#293) 2026-04-18 21:54:22 +08:00
.mintignore fix: 修复 mintlify ignore, 修复侧边栏 2026-04-10 18:14:07 +08:00
.npmrc build: 更改构建逻辑 2026-04-19 08:45:06 +08:00
AGENTS.md chore: 清理仓库审计问题——修正 CLAUDE.md、删除冗余 yoga-layout、清除 621 个未使用的类型 stub (#354) 2026-04-25 08:54:18 +08:00
biome.json chore: 调整配置 2026-04-01 09:52:43 +08:00
build.ts fix: 修复构建后 vendor 二进制路径解析错误(ripgrep/audio-capture) 2026-04-25 14:46:02 +08:00
bun.lock fix: resolve dependency audit findings precisely (#361) 2026-04-26 19:49:54 +08:00
bunfig.toml feat: 搭建单元测试基础设施 — Bun test runner + 示例测试 2026-04-01 02:08:38 +08:00
CLAUDE.md fix: 修复构建后 vendor 二进制路径解析错误(ripgrep/audio-capture) 2026-04-25 14:46:02 +08:00
contributors.svg docs: update contributors 2026-04-27 07:57:43 +00:00
DEV-LOG.md feat: 远程群控 (#243) 2026-04-11 23:22:55 +08:00
docs.json docs: 尝试 docs 文档更新 2026-04-10 10:15:19 +08:00
Friends.md docs: 新增留影文档 2026-04-06 11:04:13 +08:00
knip.json feat: 添加 knip 冗余代码检查工具 2026-04-01 04:57:34 +08:00
mint.json feat: 恢复 --channels 能力 (#297) 2026-04-19 10:24:34 +08:00
package.json chore: 1.10.10 2026-04-28 21:27:47 +08:00
README_EN.md Fix mintlify validate errors (#367) 2026-04-26 11:07:20 +08:00
README.md feature: langfuse thinking 及 文本edit的问题修复( #371); 省略 diff 以减少内存峰值 (#376) 2026-04-27 17:06:33 +08:00
SECURITY.md Create SECURITY.md 2026-04-01 08:54:08 +08:00
tsconfig.base.json feat: 重构供应商层次 (#286) 2026-04-17 09:33:14 +08:00
tsconfig.json feat: 接入内建 weixin channel(同 #301 重构版本) (#303) 2026-04-19 21:33:27 +08:00
V6.md Revert "docs: 更新 README,新增 Run.ps1/TODO.md,删除 V6.md" 2026-04-04 23:31:21 +08:00
vite.config.ts fix: resolve dependency audit findings precisely (#361) 2026-04-26 19:49:54 +08:00

Claude Code Best V5 (CCB)

GitHub Stars GitHub Contributors GitHub Issues GitHub License Last Commit Bun

Which Claude do you like? The open source one is the best.

A reverse-engineered / decompiled source restoration of Anthropic's official Claude Code CLI tool. The goal is to reproduce most of Claude Code's functionality and engineering capabilities. It's abbreviated as CCB.

Documentation (Chinese) — PR contributions welcome.

Sponsor placeholder.

  • v1: Basic runability and type checking pass
  • V2: Complete engineering infrastructure
    • Biome formatting may not be implemented first to avoid code conflicts
    • Build pipeline complete, output runnable on both Node.js and Bun
  • V3: Extensive documentation and documentation site improvements
  • V4: Large-scale test suite for improved stability
    • Buddy pet feature restored Docs
    • Auto Mode restored Docs
    • All features now configurable via environment variables instead of bun --feature
  • V5: Enterprise-grade monitoring/reporting, missing tools补全, restrictions removed
    • Removed anti-distillation code
    • Web search capability (using Bing) Docs
    • Debug mode support Docs
    • Disabled auto-updates
    • Custom Sentry error reporting support Docs
    • Custom GrowthBook support (GB is open source — configure your own feature flag platform) Docs
    • Custom login mode — configure Claude models your way
  • V6: Large-scale refactoring, full modular packaging
    • V6 will be a new branch; main branch will be archived as a historical version

I don't know how long this project will survive. Star + Fork + git clone + .zip is the safest bet.

This project updates rapidly — Opus continuously optimizes in the background, with new changes almost every few hours.

Claude has burned over $1000, out of budget, switching to GLM to continue; @zai-org GLM 5.1 is quite capable.

Quick Start

Prerequisites

Make sure you're on the latest version of Bun, otherwise you'll run into all sorts of weird bugs. Run bun upgrade!

  • Bun >= 1.3.11

Install Bun:

# Linux and macOS
curl -fsSL https://bun.sh/install | bash

# Windows (PowerShell)
powershell -c "irm bun.sh/install.ps1 | iex"

Post-installation steps:

  1. Make bun available in the current terminal

    The installer adds ~/.bun/bin to the matching shell configuration file. On macOS with the default zsh shell, you may see:

    Added "~/.bun/bin" to $PATH in "~/.zshrc"
    

    Restart the current shell as the installer suggests:

    exec /bin/zsh
    

    If you use bash, reload the bash configuration:

    source ~/.bashrc
    

    Windows PowerShell users can close and reopen PowerShell.

  2. Verify that Bun is available:

    bun --help
    bun --version
    
  3. Update to latest version (if already installed):

    bun upgrade
    
  • Standard Claude Code configuration — each provider has its own setup method

Command Execution Location

  • Bun installation and checking commands can be run from any directory: curl -fsSL https://bun.sh/install | bash, bun --help, bun --version, bun upgrade
  • Project dependency installation, development mode, and builds must be run from this repository root, the directory containing package.json.

Install

cd /path/to/claude-code
bun install

Run

# Dev mode — if you see version 888, it's working
bun run dev

# Build
bun run build

The build uses code splitting (build.ts), outputting to dist/ (entry dist/cli.js + ~450 chunk files).

The build output runs on both Bun and Node.js — you can publish to a private registry and run directly.

If you encounter a bug, please open an issue — we'll prioritize it.

First-time Setup /login

After the first run, enter /login in the REPL to access the login configuration screen. Select Anthropic Compatible to connect to third-party API-compatible services (no Anthropic account required).

Fields to fill in:

Field Description Example
Base URL API service URL https://api.example.com/v1
API Key Authentication key sk-xxx
Haiku Model Fast model ID claude-haiku-4-5-20251001
Sonnet Model Balanced model ID claude-sonnet-4-6
Opus Model High-performance model ID claude-opus-4-6
  • Tab / Shift+Tab to switch fields, Enter to confirm and move to the next, press Enter on the last field to save
  • Model fields auto-fill from current environment variables
  • Configuration saves to ~/.claude/settings.json under the env key, effective immediately

You can also edit ~/.claude/settings.json directly:

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.example.com/v1",
    "ANTHROPIC_AUTH_TOKEN": "sk-xxx",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude-haiku-4-5-20251001",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-4-6",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "claude-opus-4-6"
  }
}

Supports all Anthropic API-compatible services (e.g., OpenRouter, AWS Bedrock proxies, etc.) as long as the interface is compatible with the Messages API.

Feature Flags

All feature toggles are enabled via FEATURE_<FLAG_NAME>=1 environment variables, for example:

FEATURE_BUDDY=1 FEATURE_FORK_SUBAGENT=1 bun run dev

See docs/features/ for detailed descriptions of each feature. Contributions welcome.

VS Code Debugging

The TUI (REPL) mode requires a real terminal and cannot be launched directly via VS Code's launch config. Use attach mode:

Steps

  1. Start inspect server in terminal:

    bun run dev:inspect
    

    This outputs an address like ws://localhost:8888/xxxxxxxx.

  2. Attach debugger from VS Code:

    • Set breakpoints in src/ files
    • Press F5 → select "Attach to Bun (TUI debug)"

Contributors

Star History

Star History Chart

License

This project is for educational and research purposes only. All rights to Claude Code belong to Anthropic.