跟踪 core runtime 架构拆解闭环
1. 核心目标
将 bitfun-core 从承载大量 concrete runtime / product logic 的中心模块,逐步收敛为兼容层与产品组装入口。重构完成后,核心能力应按稳定接口、runtime owner、product capability 和产品入口分层组合,避免不同交付形态都被迫依赖全量底层实现。
目标依赖方向:
Product Surfaces -> Product Assembly / Capabilities -> Harness / Tool Runtime / Agent Runtime SDK -> Runtime Services -> Stable Contracts / External Providers
本重构不以新增 crate 数量为目标。每次 owner 迁移都必须同时减少、迁移或显著简化既有 core 路径,并保持旧路径兼容和产品行为等价。
2. 重构方案
- 接口与实现分离:下层只暴露 typed ports、DTO、event facts、capability availability 和 hook contracts;具体 filesystem、terminal、Git、AI、MCP、remote、Tauri 等实现由上层组装注册。
- Product Assembly 负责组装:不同平台、产品形态和交付形态通过组装器选择 provider 和 capability,不把平台 handle 或 app state 下沉到 runtime owner。
- Owner crate 承接真实逻辑:Agent Runtime、Runtime Services、Tool Runtime、Harness、Product Domains 只迁移可被测试证明等价的事实、策略、状态机或 runtime 决策。
- Core 保留兼容层:在迁移期保留旧 import path、adapter、具体 IO 接线和产品入口兼容,避免一次性大改导致功能漂移。
- Feature / 构建收益后置:只有 owner 边界稳定后,才评估 default feature、no-default build、交付形态裁剪和构建收益。
3. 关键阶段
| 阶段 | 目标 | 准出要求 |
|---|---|---|
| 分层与 owner 基线 | 明确 core、runtime services、agent runtime、tool runtime、harness、product domains 的边界 | 文档、AGENTS、boundary check 与实际代码一致 |
| 低风险 owner 收敛 | 迁移纯策略、DTO、manifest、catalog、facts、状态派生和无副作用决策 | focused tests 证明旧行为等价,旧路径继续可用 |
| 高风险 runtime 迁移 | 逐步迁移 scheduler、event、permission、tool execution、remote、MiniApp、function-agent 等 concrete runtime owner | 先补等价保护,再移动实现;不得混入用户可见行为变更 |
| Harness / capability 组装 | 将多步骤 workflow、capability pack 和 hook/event contract 纳入可扩展组装体系 | hook 顺序、错误策略、timeout、权限和旧路径兼容均可测 |
| Feature / 构建收益评估 | 基于稳定 owner 边界评估 feature matrix、依赖裁剪和构建收益 | 使用 cargo metadata / cargo tree / build check 数据证明收益与风险 |
4. 质量保护
- 每个 owner 迁移必须有旧路径兼容、focused regression、boundary check 和最小必要 build/check 证据。
- 涉及权限、工具曝光、session 生命周期、scheduler 时序、remote workspace、MiniApp worker / host、Git / AI provider 的变更,必须先补可观测等价测试。
- Runtime / contract crate 不允许吸收 Tauri、app state、process execution、network client、Git provider、AI provider 等 concrete dependency,除非该 crate 明确就是 concrete integration owner。
- 文档只维护稳定设计和活跃计划;已完成事实归档,避免计划文档继续膨胀为流水账。
- 如果发现需要改变功能逻辑、权限边界、事件语义、工具 schema 或交付形态,必须单独评审,不能伪装成结构重构。
5. 主要风险
| 风险 | 控制方式 |
|---|---|
| 只新增抽象,旧逻辑仍留在 core,导致代码膨胀 | owner PR 必须迁移或简化现有路径;纯 facade / guard 不算完成 |
| 边界模糊,runtime owner 反向依赖 core 或平台实现 | boundary check、cargo tree、AGENTS 和 crate feature 持续约束 |
| 高风险路径迁移导致功能降级或时序漂移 | 先迁移 facts / decisions,再迁移 lifecycle;无法证明等价则暂停 |
| 为追求构建收益提前裁剪 feature | feature / build-benefit 只在 owner 稳定后处理 |
| 不同产品形态遗漏 provider 或 capability | Product Assembly 必须显式注册能力,并验证桌面、CLI、server、remote 等入口的能力范围 |
| 文档与代码漂移 | 每次阶段性变更只更新 plan / completed / issue 状态;设计文档仅在目标设计变化时更新 |
6. 完成标准
bitfun-core不再是 concrete runtime 和产品能力的默认聚合点,而是兼容 facade 与组装入口。- Agent Runtime、Runtime Services、Tool Runtime、Harness、Product Domains 均可通过稳定接口独立承接对应 owner 逻辑。
- 不同平台和交付形态可以通过 feature 或 Product Assembly 最小化依赖,而不影响既有功能范围。
- 高风险路径均具备等价保护、旧路径兼容和明确回滚边界。
- 构建收益、依赖裁剪和 feature matrix 有数据证明,且没有功能降级、权限漂移或性能劣化。
7. Issue 生命周期
本 Issue 在完整架构迁移达到完成标准前保持打开。阶段性进展、风险变化和剩余工作可以在评论中更新;正文只维护整体目标、方案、阶段、保护措施、风险和完成标准。
PR1 已提交:#975
本次状态更新:
- 已完成 Product Assembly / Runtime Services Foundation 的基础实现:新增
bitfun-runtime-services、typed builder、provider registry、capability availability、fake provider 和 focused tests。 - 已把后续迁移计划收敛为 PR1-PR7,并在
docs/plans/core-decomposition-plan.md中细化 PR1 的实施门禁。 - 当前没有迁移 concrete runtime owner,不改变产品行为、默认能力集合、权限语义、工具曝光、事件语义、session 生命周期或构建脚本。
- 设计说明:
bitfun-runtime-services当前保持最小依赖,仅依赖bitfun-runtime-ports、async-trait、thiserror;bitfun-core-types/bitfun-events保留为后续 service DTO 或 event contract 需要时才引入的允许依赖,避免 PR1 提前扩大依赖面。 - 对抗性审查中发现并修复了 provider capability mismatch 风险;builder 现在会拒绝字段类型正确但 port 自报 capability 错误的注入。
已验证:
cargo test -p bitfun-runtime-servicescargo test -p bitfun-runtime-portsnode scripts/check-core-boundaries.mjspnpm run check:repo-hygienegit diff --check gcwing/main..HEADcargo check -p bitfun-core --features product-fullcargo check --workspace
下一阶段仍按计划进入 PR2:Service / Agent Remote Runtime Owner。该 Issue 继续保持打开,后续根据关联 PR 更新迁移状态和计划调整。
PR2 + PR3 合并实现已提交:#978
本次进展:
- PR2:remote workspace/session/file-projection 的稳定 owner contract 已迁入
bitfun-runtime-ports,bitfun-services-integrations::remote_connect保留旧路径 re-export;具体 SSH / relay / terminal pre-warm / session restore / scheduler submit 仍留在 core。 - PR3:新增
bitfun-agent-runtime,当前只承接 scheduler/background delivery 的纯决策;concrete scheduler lifecycle、session manager、prompt loop、subagent registry 和 post-turn hook 未外移。 - 已同步更新 plan、架构文档、AGENTS 边界说明和
scripts/check-core-boundaries.mjs,避免把未完成迁移误描述为已完成。
风险与保护:
- 未修改产品命令、UI、默认 feature、release / fast build 脚本或产品能力集合。
- 提交前已按第三方对抗视角检查依赖方向、旧路径兼容、行为等价、性能额外开销和文档一致性;发现并修复了 background delivery action 暴露无意义 submission policy 的 API 风险。
- GitHub Actions CI run #1859 已通过:Frontend Build、Windows / Ubuntu / macOS Rust checks 全部成功。
- 本 issue 保持 open,后续会继续按关联 PR 更新迁移状态,不在 #978 合并时关闭。
验证命令已在 PR 描述中列出。
PR4 已提交:#989。
本次完成范围:
- 新增
bitfun-harness,作为 Harness Layer 的 provider-neutral contract crate,承接 workflow descriptor、route plan、outcome/error 和 registry wiring。 - 在
bitfun-core::agentic::harness注册 Deep Review、DeepResearch、MiniApp 三个 legacy-facade provider,满足至少两个 workflow 可通过 provider contract 表达的 PR4 准入。 - 将
bitfun-harness纳入 boundary check,禁止依赖bitfun-core、具体 service crate、product-domain implementation、AI adapter、transport、Tauri、Git/MCP/image/WebSocket runtime deps、CLI/TUI 或 concrete tool pack。 - 更新架构、详细设计、执行计划、已完成归档和 AGENTS 文档,明确 PR4 只完成 contract / registry boundary。
明确未完成边界:
- Deep Review、DeepResearch、MiniApp 的 concrete workflow execution 未迁移。
legacy_facadeprovider 只生成旧路径 route plan,execute仍返回 unsupported;这用于防止误判为执行语义已外移。- Product command registry、capability pack、Harness 对 Tool Runtime / Runtime Services 的实际 orchestration 仍属于后续迁移。
验证:
cargo test -p bitfun-harnesscargo test -p bitfun-core --features product-full product_harnessnode scripts/check-core-boundaries.mjspnpm run check:repo-hygienecargo check --workspacegit diff --check gcwing/main...HEAD
该 issue 继续保持打开,等待后续 Product-Domain Runtime、Tool Runtime、Feature / Build-Benefit Evaluation,以及单独保护后的 Harness execution / Product Capability pack 迁移。
PR5 已提交:#991
本次完成内容:
- 在
bitfun-product-domains中补齐 MiniApp / function-agent typed runtime service view。 - MiniApp dependency install 已通过 runtime port 路由,保留 worker process、
pool.stop、state persistence、事件发送等旧路径语义。 - function-agent Git/AI facade 改为通过 service view 组装,concrete Git command、AI provider acquisition、AI transport error mapping 仍在 core adapter。
- 更新了 boundary check、架构文档、执行计划和 product-domains AGENTS 文档,明确 PR5 的完成边界。
仍未关闭的后续边界:
- MiniApp filesystem IO、worker process、host dispatch、builtin asset seed / marker IO 仍在 core / app adapter。
- function-agent Git/AI concrete adapter 与 provider acquisition 仍在 core。
- 后续重点仍是 Tool Runtime owner、Feature / Build-Benefit Evaluation,以及需要单独保护的更深 Product-Domain concrete provider 或 Harness execution / Product Capability pack 迁移。
本 issue 继续保持打开,等完整架构迁移闭环后再关闭。
更新 #991:本次 PR 已重做为文档纠偏,不再提交 Product-Domain runtime service-view 代码。
关键变化:
- 移除本轮“只新增 service view / facade / boundary 锚点”的代码尝试,不将其计入 PR5 完成。
- 明确新增抽象的准入标准:必须同时删除、迁移或显著简化既有代码路径,否则只能作为计划说明,不能算 runtime owner 迁移。
- 主计划收敛为剩余 PR5-PR7:Product-Domain Runtime 实迁移复核、Tool Runtime 真实 owner 迁移、Feature / Build-Benefit Closure。
- Product-Domain Runtime 需要先证明可迁移的完整 owner 候选;若无法减少旧路径复杂度,应明确跳过代码迁移。
该 issue 保持打开,后续会继续按关联 PR 更新迁移状态、风险和计划变化。
## 迁移进展更新:PR #1030
本次 PR 完成 Agent Runtime Kernel Closure 的一块实际 owner 迁移:
- 将 prompt-loop 中不依赖 workspace / remote / config IO 的纯事实迁入
bitfun-agent-runtime:UserContextPolicy、UserContextSection、ToolListingSections、PrependedPromptReminders。 bitfun-core仅保留旧路径 re-export 和具体PromptBuilder组装逻辑,避免调用方 import 路径漂移。- 增加
bitfun-agent-runtimecontract tests 与 boundary check,防止这些 prompt facts 回流 core。 - 同步更新架构、计划、完成归档和模块 AGENTS 文档,明确“prompt-loop 纯事实已迁移,concrete prompt assembly / workspace context IO / prompt cache 协调仍未迁移”。
保护与验证:
cargo test -p bitfun-agent-runtimecargo test -p bitfun-core --features product-full prompt -- --nocapturecargo check -p bitfun-core --features product-fullcargo check -p bitfun-core --no-default-featuresnode scripts/check-core-boundaries.mjspnpm run check:repo-hygienegit diff --check
后续仍保持 issue 打开,继续按全局里程碑推进 concrete scheduler lifecycle、prompt module / prompt cache contract、runtime events、permission coordination、Product-Domain Runtime 和 Tool Runtime 剩余主体迁移。
## 文档维护口径更新
本次对 PR #1030 做了补充调整:architecture / design 文档不再承载每个 PR 的完成状态或过程日志。
后续维护规则:
docs/architecture/core-decomposition.md和docs/architecture/agent-runtime-services-design.md只描述稳定分层、接口归属、行为边界、风险与目标态判定口径。- PR 是否完成、已迁移项、待迁移项和阶段范围通过
docs/plans/core-decomposition-plan.md、docs/plans/core-decomposition-completed.md与本 issue 维护。 - 只有当目标分层、接口归属、行为边界或关键风险判断需要修正时,才修改 architecture / design 文档。
这个口径用于防止设计文档随着每次 PR 膨胀,也避免把阶段性完成状态误写成长期架构事实。
阶段更新:PR #1033 已提交。
本次范围:
bitfun-agent-runtime承接 runtime event / execution facts:FinishReasonwire label、session-state event label、turn-outcome event fact。bitfun-core仅保留旧路径 re-export、concreteSessionStateadapter 和 scheduler 调用方,不再拥有这些纯 runtime facts。- 边界脚本已补充回流保护,plan / completed 文档和模块 AGENTS 已同步;稳定架构/详设文档未修改,因为目标设计没有变化。
保护与验证:
- 未修改产品命令、UI、默认 feature、构建脚本、scheduler 生命周期、event delivery 或 post-turn hook。
- 已验证
bitfun-agent-runtime事件契约、core scheduler cancel-suppression 回归、core boundary check 和 repo hygiene。
后续仍未完成:
- event delivery / post-turn hook、concrete scheduler lifecycle、agent definition loading、permission coordination、prompt cache contract 等仍属于后续受保护迁移范围。
PR-A 已提交:#1036。
本次范围:
bitfun-agent-runtime承接 prompt cache policy / identity / DTO / scope key / in-memory store。bitfun-agent-runtime承接 shared mode profile/context policy、mode presentation rank、SubAgentSource与 source rank 等 registry 纯事实。bitfun-core继续保留 session persistence / cold restore、agent definition loading、custom subagent IO、scheduler lifecycle、event delivery、permission handler 和 post-turn hook。- 已补 focused tests 与
check-core-boundaries防回流规则。
下一阶段按计划进入 PR-B:Product-Domain + Tool Runtime owner closure。该 issue 继续保持打开,等完整架构迁移完成后再关闭。
PR-B 已提交:#1041
本次完成范围:
- ToolUseContext runtime handles 收敛到
ToolRuntimeHandles,并通过 desktop/workspace check 覆盖外部调用。 - Product tool materialization、collapsed unlock lifecycle state 迁入 product runtime owner 模块。
- 内置 MiniApp bundle identity / version / embedded assets 迁入
bitfun-product-domains,core 保留 seed 写盘、marker IO、storage 保留与 recompile。 - 更新 AGENTS、MiniApp skill、执行计划和 boundary check,移除旧 core asset owner 文档漂移。
质量保护:
- focused Rust tests 覆盖 runtime handles、ToolUseContext、product runtime、unlock state、builtin reseed。
bitfun-product-domains --no-default-features与--features product-full均通过,确认 feature 边界未扩大。cargo check -p bitfun-desktop与cargo check --workspace均通过,覆盖 API 调用遗漏风险。
未关闭项:#970 继续跟踪完整 core runtime 架构迁移,后续仍需处理 concrete IO tools、MiniApp worker/host、function-agent concrete Git/AI,以及更深层 runtime owner 迁移。
阶段更新:Product Capability / Harness / Tool provider 组装闭环已提交 draft PR #1046。
本阶段完成:
- 新增 product capability owner,集中描述 service capability、tool provider group、harness provider descriptor。
- core 的 product harness registry 与 product tool provider group assembly 改为消费 capability owner。
- 增加未知 tool provider group 防静默降级测试、boundary check、no-default / dependency profile 证据。
未在本阶段迁移:MiniApp worker/host、function-agent Git/AI、具体 IO tools、scheduler/event/permission/post-turn hook。原因是这些路径仍需要更强端到端等价保护,后续应作为独立完整 owner 迁移评估。
阶段更新:PR-C 已更新到 draft PR #1048,当前 head 为单 commit refactor(runtime): migrate product capability registry owners。
本阶段已继续扩大实际 owner 迁移:bitfun-product-capabilities 承接 service requirement、tool provider group id selection 和 harness provider selection;bitfun-harness 承接 provider-neutral harness descriptor 与 descriptor registry builder;bitfun-tool-packs 承接 tool provider group plan、按 id 选择 plan 和未知 provider group 校验;bitfun-agent-tools 承接 provider-neutral static provider materialization 与 plan-to-registry assembly;core 只保留 concrete tool factory、product plan adapter、snapshot decorator 注入和旧路径兼容入口。已删除 core 内 provider 中间态、provider group 构建、未知 tool 错误处理和 registry 安装主体算法,并删除 product-capabilities 内本地 harness descriptor / registry builder / provider plan 扫描、缺失 group 校验逻辑以及默认 tool provider plan 快捷出口。
风险边界保持不变:MiniApp worker/host、function-agent Git/AI、具体 IO tools、scheduler/event/permission/post-turn hook 等 concrete runtime 行为未在本 PR 强迁移,后续必须先补等价保护后再作为完整 owner PR 评估。
阶段更新:Scheduler owner 迁移已提交为 #1060。
本阶段范围:
bitfun-agent-runtime承接 dialog queue、active-turn facts、cancelled-reply suppression state 和 goal-continuation abort flags。bitfun-core保留 concrete submit/start-turn/coordinator/session-manager wiring、scheduler lifecycle、event delivery、permissionToolhandler 和 post-turn hook。- 新增 scheduler owner contract 与 boundary check,保护 queue ordering、overflow error、cancel suppression、goal continuation abort/retry 语义不回流。
风险状态:未改变 UI、Tauri/API wire shape、tool exposure、permission 策略、默认产品能力或 session lifecycle;后续若迁移 concrete scheduler lifecycle/event delivery,需要先补端到端等价保护。
阶段更新:#1060 已扩展为 scheduler owner decisions 迁移,不再只覆盖 queue owner。当前迁移范围包括 dialog queue、active-turn facts、cancelled-reply suppression、goal-continuation abort flags、SessionMessage reply plan、steering action plan 和 background running-turn injection construction。
仍保留在 core 的边界:concrete scheduler lifecycle、coordinator/session-manager wiring、event delivery、permission Tool handler、post-turn hook、SessionManager persistence、persisted thread-goal metadata store 和具体 submit/cancel 行为。后续若继续移动这些高风险路径,需要先补端到端等价保护。
阶段更新:已提交 PR #1070。
本次范围:
- 将 function-agent Git snapshot concrete service 外移到
bitfun-services-integrations::function_agents,core 只保留旧入口 adapter 与 AI provider acquisition。 - 为 staged/unstaged diff、no-HEAD、非 Git workspace fallback、last-commit timestamp 增加 focused regression。
- 拆分
scripts/check-core-boundaries.mjs到scripts/core-boundaries/*,并补 checker/self-test 行为保护。
剩余风险/边界:
- function-agent AI provider acquisition 暂留 core;后续如继续外移,需先形成稳定 AI runtime owner / AI provider port,避免 integration crate 依赖回 core。
- MiniApp worker/host/seed IO、session persistence/cold restore、remote FS/terminal/image context、terminal/indexed search/remote shell 等高风险 concrete runtime 仍需单独按等价保护推进。
M1 已通过 PR #1071 提交并完成 CI。
本次闭合范围:
- Product Assembly 增加
DeliveryProfile、CapabilitySet、provider plan 与 service availability report。 - core product tool runtime / harness registry 改为消费显式 Product Assembly plan,保持旧
product-full输出等价。 - core Product Assembly provider 将现有 session-store、remote workspace/projection adapter 与当前 product-full 能力 marker 接入 RuntimeServicesBuilder 组合。
风险边界:
- 不改变工具 schema、权限、UI、发布 feature、session 生命周期或具体执行语义。
- Terminal/Git/Network/MCP 当前只作为能力可用性边界,具体执行仍保留在既有 core 路径。
- feature/dependency 裁剪、search concrete、AI provider acquisition、Tool/Agent Runtime 深迁移仍是后续工作。
后续节奏按更新后的计划从 M2 开始推进:Tool Runtime concrete execution 闭环 -> Agent Runtime concrete lifecycle -> Harness/Product Domain workflow -> feature matrix 与目录组织收尾。
M2 状态更新:
- 已提交 PR #1072:将远程 Delete / Read / LS / Glob / Grep 的命令规划、stdout/stderr 规整、marker 解析、match count 和 offset/limit windowing 等低层 execution helper 收敛到
tool-runtime。 - core 对应路径已删除重复拼接/解析逻辑,继续保留
Tooladapter、path resolution、workspace shell 执行、checkpoint / UI side effect 和ToolResult包装。 - 本阶段未改变工具 schema、权限默认值、checkpoint、remote fallback、shell cwd、terminal prewarm 或 PTY lifecycle;无 Cargo 依赖变更。
- 已更新
docs/plans/core-decomposition-plan.md与docs/plans/core-decomposition-completed.md,明确 M2 的实际闭环边界和剩余 owner 边界。 - 后续仍需按设计继续推进 Agent Runtime lifecycle、Harness / Product Domain concrete workflow、feature / dependency matrix 与构建收益验证。
M4 状态更新:已提交 PR #1075。
本次迁移范围:
product-domains接管 MiniAppfs.*/shell.exechost call planning、参数默认值、错误契约和权限检查计划。core保留权限 policy resolution、路径规范化、文件 IO、进程执行、net.fetch/os.inforuntime 行为。- boundary check 已同步保护新的 owner API,避免后续回退到 core 内部解析。
保持不变的边界:
- 不改变 tool schema、权限语义、storage layout、worker 生命周期或发布形态。
- worker execution、seed/marker IO、function-agent AI provider acquisition 仍留在 core,后续如迁移需单独评审风险。
验证已完成:product-domain product-full tests、harness tests、core MiniApp host focused tests、boundary/self-test、repo hygiene、workspace check、diff check。
状态更新(2026-06-05):
- 已提交 PR #1076,用于完成当前阶段的 crate 物理分层、Product Assembly/Core Facade 收口与边界保护。
- 当前已闭合:
src/crates分层目录、workspace/path 同步、bitfun-core::product_assembly兼容 facade 收口、core-specific runtime service provider 移入bitfun-core::product_runtime、profile-scoped harness registry 入口归入bitfun-product-capabilities。 - 当前未闭合:Runtime Services concrete provider 深迁移、Tool Runtime 完整 execution pipeline、Agent Runtime/Harness concrete workflow 迁移、按交付形态的 feature/dependency trimming。
- 质量保护:本轮验证覆盖 boundary check、repo hygiene、focused Rust/Web tests、workspace check、core no-default/product-full check、cargo metadata/tree 基线与 diff check。
该 issue 仍保持打开,后续继续按设计目标跟踪剩余 owner 迁移与风险边界。
关联 PR:#1097
本次更新范围:
- remote-connect command routing / response assembly 迁入
services-integrations,core 保留 host adapter 和入口 glue。 - Bash shell 可复用 execution helper 迁入
tool-runtime::shell,包括命令包装、非交互环境、AppleScript/IM guard、local/remote result rendering 和 background result 文本。 - 新增 focused owner tests 和 boundary 回流保护。
风险边界:
- 不改变工具 schema、权限语义、remote wire shape、terminal lifecycle 或产品能力曝光。
- indexed workspace search service、permission wait、checkpoint orchestration 和完整 tool pipeline 仍是后续高风险迁移项。