概念 | 说明 |
Worker | 运行中的 CLI 进程,通过 PID 文件注册在 ~/.codebuddy/sessions/ |
Daemon | 后台常驻的 HTTP 服务进程( --serve 模式),通过 daemon start 管理 |
后台会话 (bg) | 通过 --bg 启动的非交互式任务,自动输出日志到文件 |
# 启动 daemon(后台运行,自动分配端口)codebuddy daemon start# 指定端口codebuddy daemon start --port 8080# 指定绑定地址(允许远程访问)codebuddy daemon start --host 0.0.0.0# 指定权限模式(默认为 delegate 委托模式)codebuddy daemon start --permission-mode default# 传递其他标准参数(model、mcp-config 等会自动继承)codebuddy daemon start --model claude-sonnet-4-20250514 --mcp-config ./.mcp.json
--permission-mode 切换为其他模式。daemon start 时指定的标准 CLI 参数(--model、--permission-mode、--mcp-config、--tools、--agent、--settings 等)会自动继承到 daemon 子进程中。daemon start 不会创建多个 daemon。如果已有 daemon 在运行,会直接返回现有 daemon 的信息。codebuddy daemon status# {"status":"running","pid":12345,"endpoint":"http://127.0.0.1:51862","startedAt":1775498920401}
codebuddy daemon stopcodebuddy daemon restart
# 后台执行任务codebuddy --bg "实现登录页面"# 指定名称(便于查找)codebuddy --bg --name feature-login "实现登录页面"
--print -y 模式运行(无 TUI + 跳过权限确认),stdout/stderr 重定向到 ~/.codebuddy/logs/{name}.log。# 列出所有活跃 Workercodebuddy ps# 查看后台会话日志codebuddy logs feature-login# 持续跟踪日志(类似 tail -f)codebuddy logs feature-login -f# 附加到后台会话codebuddy attach feature-login# 终止后台会话codebuddy kill feature-login
# 列出所有 Workercurl http://127.0.0.1:8080/api/v1/workers# 启动 Daemoncurl -X POST http://127.0.0.1:8080/api/v1/daemon/start \\-H "Content-Type: application/json" \\-d '{"port": 9090}'# 查看 Worker 日志(遥测日志)curl "http://127.0.0.1:8080/api/v1/workers/12345/logs?type=telemetry&tail=100"# 终止 Workercurl -X DELETE http://127.0.0.1:8080/api/v1/workers/12345
--serve 模式启动后,Web UI 提供三个管理页面:类型 | 路径 | 内容 | 触发条件 |
telemetry | ~/.codebuddy/logs/{date}/{workspace}.log | 所有模块的 Info/Warn/Error | 始终(默认优先) |
process | ~/.codebuddy/logs/{name}.log | 进程 stdout/stderr | 仅 bg/daemon |
debug | ~/.codebuddy/debug/{sessionId}.txt | 详细调试信息 | 需 --debug |
transcript | ~/.codebuddy/projects/{id}/{sessionId}.jsonl | 对话历史 | 始终 |
~/.codebuddy/sessions/ 注册 PID 文件:~/.codebuddy/sessions/├── 12345.json # 本地进程(PID 作为文件名)├── 67890.json # 另一个本地进程└── manual-abc123.json # 手动添加的远程 Worker
{"pid": 12345,"sessionId": "interactive-12345","cwd": "/home/user/project","startedAt": 1775498920401,"kind": "interactive","url": "http://127.0.0.1:8080","mode": "local","version": "2.78.1","hostname": "my-machine"}
kill -0 检测,手动远程 Worker 通过心跳超时检测(2 分钟)。变量 | 说明 |
CODEBUDDY_SESSION_KIND | Worker 类型(interactive / bg / daemon) |
CODEBUDDY_SESSION_NAME | 后台会话显示名称 |
CODEBUDDY_SESSION_LOG | 后台会话日志路径 |
CODEBUDDY_GATEWAY_AUTH | 认证模式(none / password) |
# 注册为系统服务并立即启动 daemoncodebuddy daemon install# 指定端口和权限模式codebuddy daemon install --port 8080 --permission-mode bypassPermissions# 移除系统服务注册codebuddy daemon uninstall
codebuddy daemon status 会显示系统服务状态:{"status": "running","pid": 42567,"endpoint": "http://127.0.0.1:9527","systemService": {"installed": true,"backend": "launchd","configPath": "/Users/xxx/Library/LaunchAgents/com.codebuddy.daemon.plist"}}
平台 | 后端 | 服务类型 | 配置路径 |
macOS | launchd | Launch Agent(用户级) | ~/Library/LaunchAgents/com.codebuddy.daemon.plist |
Linux | systemd | User Unit | ~/.config/systemd/user/codebuddy-daemon.service |
Windows | Task Scheduler | 计划任务(用户级) | schtasks /tn "CodeBuddy Daemon" |
KeepAlive / Restart=on-failure)# macOS 原生管理launchctl list | grep codebuddylaunchctl stop com.codebuddy.daemonlaunchctl start com.codebuddy.daemon# Linux 原生管理systemctl --user status codebuddy-daemonsystemctl --user stop codebuddy-daemonsystemctl --user start codebuddy-daemon# Windows 原生管理schtasks /query /tn "CodeBuddy Daemon"
codebuddy daemon start --port 8080# 浏览器打开 http://127.0.0.1:8080 即可使用# 关闭终端后服务仍在运行
# CI 启动codebuddy daemon start --port 9090# 其他 CI 步骤调用(body 为 Gateway Protocol 格式,须含 id/type)curl -X POST http://127.0.0.1:9090/api/v1/runs \\-H "Content-Type: application/json" \\-H "X-CodeBuddy-Request: 1" \\-d '{"id": "run-1", "type": "message", "payload": {"text": "审查这个 PR 的代码变更"}}'# 响应: {"data": {"runId": "uuid-xxx", "status": "accepted"}}
/api/v1/runs 请求必须携带 X-CodeBuddy-Request 头,body 使用 Gateway Protocol 格式(id、type 为必填,prompt 文本放在 payload.text),字段详情见 HTTP API 文档。codebuddy daemon start --host 0.0.0.0 --port 8080# 同事访问 http://192.168.1.100:8080
--bg 同时启动多个后台会话处理不同任务,通过 ps/logs/kill 管理。codebuddy --bg --name "refactor-auth" "重构认证模块"codebuddy --bg --name "add-tests" "给 utils 目录补充单元测试"codebuddy --bg --name "fix-types" "修复所有 TypeScript 类型错误"# 查看进度codebuddy pscodebuddy logs refactor-auth
codebuddy daemon start# 配合远程控制功能连接微信/企业微信渠道
特性 | --serve | daemon start |
生命周期 | 跟随终端,关闭终端则退出 | 后台常驻,独立于终端 |
启动方式 | 前台运行 | fork detached 子进程 |
管理方式 | Ctrl+C 停止 | daemon stop/status/restart |
多次启动 | 每次创建新进程 | 幂等,只维护一个 daemon |
典型用途 | 临时开发调试 | 长期运行的服务 |
文档反馈