Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AGENTS-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ BitFun 是一个由 Rust workspace 与 React 前端组成的项目。
|---|---|---|
| Core(产品逻辑) | `src/crates/core` | [AGENTS.md](src/crates/core/AGENTS.md) |
| 已拆出的 core 支撑 crate | `src/crates/{core-types,agent-stream,runtime-ports,runtime-services,terminal,tool-runtime}` | (使用 core 指南) |
| Agent runtime owner crate | `src/crates/agent-runtime` | [AGENTS.md](src/crates/agent-runtime/AGENTS.md) |
| Service core owner crate | `src/crates/services-core` | [AGENTS.md](src/crates/services-core/AGENTS.md) |
| Service integrations owner crate | `src/crates/services-integrations` | [AGENTS.md](src/crates/services-integrations/AGENTS.md) |
| Agent tool contracts | `src/crates/agent-tools` | [AGENTS.md](src/crates/agent-tools/AGENTS.md) |
Expand Down
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Repository rule: **keep product logic platform-agnostic, then expose it through
|---|---|---|
| Core (product logic) | `src/crates/core` | [AGENTS.md](src/crates/core/AGENTS.md) |
| Extracted core support | `src/crates/{core-types,agent-stream,runtime-ports,runtime-services,terminal,tool-runtime}` | (use core guide) |
| Agent runtime owner crate | `src/crates/agent-runtime` | [AGENTS.md](src/crates/agent-runtime/AGENTS.md) |
| Service core owner crate | `src/crates/services-core` | [AGENTS.md](src/crates/services-core/AGENTS.md) |
| Service integrations owner crate | `src/crates/services-integrations` | [AGENTS.md](src/crates/services-integrations/AGENTS.md) |
| Agent tool contracts | `src/crates/agent-tools` | [AGENTS.md](src/crates/agent-tools/AGENTS.md) |
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"src/crates/events",
"src/crates/ai-adapters",
"src/crates/agent-stream",
"src/crates/agent-runtime",
"src/crates/runtime-ports",
"src/crates/runtime-services",
"src/crates/services-core",
Expand Down
22 changes: 15 additions & 7 deletions docs/architecture/agent-runtime-services-design.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Agent Runtime SDK 与 Runtime Services 设计

本文是 [`core-decomposition.md`](core-decomposition.md) 的开发设计文档,描述目标模块、
接口、crate 内部结构和迁移保护。`bitfun-runtime-services` 已建立 PR1 基础壳层,
当前只承载 typed service bundle、builder、provider registry、capability availability 和
fake provider;`bitfun-agent-runtime`、`bitfun-harness` 仍是目标 crate,在实际创建前不得把它们当作
已完成事实
接口、crate 内部结构和迁移保护。`bitfun-runtime-services` 已建立 typed service bundle、
builder、provider registry、capability availability 和 fake provider 基础;`bitfun-agent-runtime`
已创建并只承接可独立构建的 scheduler/background delivery 纯决策。`bitfun-harness` 仍是目标 crate
未迁移的 session manager、prompt loop、subagent registry 和 concrete scheduler lifecycle 不得被描述为已完成

## 1. 设计目标与边界

Expand All @@ -24,7 +24,7 @@ bitfun-runtime-ports
bitfun-runtime-services # PR1 基础壳层
bitfun-agent-tools
tool-runtime
bitfun-agent-runtime # 目标
bitfun-agent-runtime # 已创建,当前仅承接 scheduler/background delivery 决策
bitfun-harness # 目标
bitfun-services-core
bitfun-services-integrations
Expand Down Expand Up @@ -91,7 +91,7 @@ bitfun-runtime-services

- 只有当 owner 边界、旧路径兼容、focused tests、依赖收益和 boundary check 都能同时落地时,才创建新的目标 crate。
- `bitfun-runtime-services` 已按该准入建立基础壳层;继续扩展时仍必须保持 typed builder、本地 service、remote service 和 fake provider 三类注入路径可测试。
- `bitfun-agent-runtime` 的创建前提是 session / turn / scheduler / prompt loop 中至少一个 owner 可以脱离 `bitfun-core` 构建,并有旧路径 facade
- `bitfun-agent-runtime` 已通过 scheduler/background delivery 纯决策满足创建准入;继续扩展时仍必须保持旧路径 facade、focused tests 和 boundary check
- `bitfun-harness` 的创建前提是至少两个 workflow 可以通过 provider contract 注册,例如 Deep Review 与 MiniApp / DeepResearch。
- 若目标 crate 只能承接单个 helper 或只能通过 `bitfun-core` 才能测试,继续留在迁移期 facade,不提前拆 crate。

Expand Down Expand Up @@ -235,7 +235,15 @@ Remote ports 的边界:

### 3.1 Agent Runtime SDK

目标 crate:`bitfun-agent-runtime`。
当前 crate:`bitfun-agent-runtime`。

当前已承接范围:

- background delivery 状态决策:Processing 注入当前运行 turn;Missing / Idle / Error 提交 agent-session follow-up turn。

仍留在 `bitfun-core` 的范围:

- concrete scheduler 生命周期、session manager、turn id 生成、injection buffer、submit 执行、prompt loop、subagent registry 和 post-turn hook。

职责:

Expand Down
7 changes: 5 additions & 2 deletions docs/architecture/core-decomposition.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ Computer Use 等能力的组合边界。它负责定义一个产品能力需要
Agent 运行时 SDK(Agent Runtime SDK)是可嵌入的 agent kernel,负责 session、turn、scheduler、prompt loop、subagent、
background task、permission coordination 和 runtime events。它只依赖稳定契约、tool runtime 和注入的
service ports,不感知 Desktop、CLI、Remote、ACP、Tauri 或 Web UI。当前主体仍在 `bitfun-core`,
目标归属是 `bitfun-agent-runtime`。
但 `bitfun-agent-runtime` 已开始承接可独立构建的 scheduler/background delivery 纯决策;concrete
scheduler lifecycle、session manager、prompt loop 和 subagent registry 仍未外移。

### 7.6 工具运行时(Tool Runtime)

Expand All @@ -263,7 +264,7 @@ service ports,不感知 Desktop、CLI、Remote、ACP、Tauri 或 Web UI。当
filesystem、workspace、session store、Git、terminal、network、MCP catalog、remote connection / projection
等端口,不执行产品命令,
不作为无类型 service locator,也不创建平台实现。当前相关 crate 包括 `bitfun-runtime-ports`、
`bitfun-services-core`、`bitfun-services-integrations` 和 `bitfun-core` 中的 service 接线代码。
`bitfun-runtime-services`、`bitfun-services-core`、`bitfun-services-integrations` 和 `bitfun-core` 中的 service 接线代码。

### 7.8 具体实现层(Concrete Integrations)

Expand All @@ -282,6 +283,8 @@ SSH、relay、本地隧道、远端 OS 差异和认证方式属于具体 Remote
稳定契约层提供跨层共享的数据结构和接口语言,包括 DTO、event、permission facts、artifact refs、identity
和 port traits。它只描述事实和能力,不包含 IO、网络、进程、UI、runtime manager 或产品策略。当前相关
crate 包括 `bitfun-core-types`、`bitfun-events` 和 `bitfun-runtime-ports`。
当前 remote workspace facts、remote session metadata、remote workspace file projection DTO 和 remote workspace/projection
host trait 已归入 `bitfun-runtime-ports`,`bitfun-services-integrations::remote_connect` 保留旧路径 re-export。

## 8. 接口与实现关系

Expand Down
43 changes: 43 additions & 0 deletions docs/plans/core-decomposition-plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,49 @@ PR1 是后续高风险迁移的前置门禁,目标是提供可测试的 typed

PR1 不迁移任何 concrete service owner,因此预期不会修改产品行为、默认能力集合、权限语义、工具曝光、事件语义、session 生命周期或构建脚本。

### 4.2 PR2 + PR3 合并实施计划

本次 PR 合并推进 PR2 和 PR3,但仍按两个 owner 主题顺序实施,避免把 remote provider、agent scheduler
和产品 surface 行为混在同一个迁移步骤中。若实现过程中发现必须改变用户可见行为、默认 feature、权限语义或构建形态,
应暂停并在 PR 描述中单独说明设计偏移原因、影响范围和回滚边界。

#### 4.2.1 PR2:Service / Agent Remote Runtime Owner

目标是在不搬动 concrete SSH / relay / terminal / session restore 实现的前提下,把 remote workspace 与 projection
的稳定接口归入 `bitfun-runtime-ports`,并保留 `bitfun-services-integrations::remote_connect` 旧路径 re-export。

1. 在 `bitfun-runtime-ports` 中承接 remote workspace facts、remote session metadata、remote workspace file projection DTO
和 `RemoteWorkspacePort` / `RemoteProjectionPort` owner trait。
2. 在 `bitfun-services-integrations::remote_connect` 中删除重复 owner 定义,改为 re-export 新 owner crate 的类型和 trait,
保持现有调用方 import 路径兼容。
3. 让 core 侧 remote workspace / file adapter 继续作为具体 provider,实现新的 stable port;workspace-root、
persistence、session restore、terminal pre-warm 和 scheduler submit 仍保留在 `bitfun-core`。
4. 补充 focused tests,覆盖 remote workspace / file projection 类型通过旧路径与新 owner 路径保持等价,以及
`RuntimeServicesBuilder` 能注册带方法的 remote workspace / projection provider。
5. 更新 boundary check,防止 remote owner contract 回流到 `bitfun-core` 或 concrete service crate。

#### 4.2.2 PR3:Agent Runtime SDK Owner

目标是创建有真实 owner 的 `bitfun-agent-runtime`,只迁移 scheduler/background delivery 这类可纯函数保护的运行时决策,
不外移 concrete scheduler 生命周期。

1. 新建 `bitfun-agent-runtime` crate,并加入 workspace;该 crate 只依赖 `bitfun-runtime-ports` 等稳定契约,不依赖
`bitfun-core`、Tauri、CLI、ACP、Web UI 或 concrete service crate。
2. 先把 background delivery 的状态决策抽为 `bitfun-agent-runtime` 的纯 contract:Processing 注入当前运行 turn,
Missing / Idle / Error 提交 agent-session follow-up turn。
3. core scheduler 仅调用该决策结果,继续负责 injection buffer、submit、turn id、metadata 和实际生命周期执行。
4. 补充 `bitfun-agent-runtime` focused tests 与 core scheduler 兼容验证,确保 background reply、cancel suppression、
queue/preempt 和 DeepResearch/post-turn 相关语义没有漂移。
5. 更新 `AGENTS.md` / `AGENTS-CN.md` 和设计文档中的 crate 状态,描述 `bitfun-agent-runtime` 已承接的范围以及仍未外移的
scheduler lifecycle、session manager、prompt loop 和 subagent registry。

#### 4.2.3 本次 PR 验收

- 不修改产品命令、UI、默认 feature、release / fast build 脚本或产品能力集合。
- 不新增反向依赖、无类型 service locator、全局 mutable registry 或重复 runtime materialization。
- 必须通过 remote / runtime owner focused tests、boundary check、repo hygiene 和最小 Rust 编译验证。
- 提交前从第三方视角审查功能偏移、性能劣化、跨产品形态遗漏、文档与代码不一致,并修复发现的问题。

## 5. 每类 PR 的保护重点

### 5.1 Service / Agent Remote Runtime Owner
Expand Down
111 changes: 106 additions & 5 deletions scripts/check-core-boundaries.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const noCoreDependencyCrates = [
'events',
'ai-adapters',
'agent-stream',
'agent-runtime',
'runtime-ports',
'runtime-services',
'services-core',
Expand Down Expand Up @@ -113,6 +114,31 @@ const lightweightBoundaryRules = [
'syntect-tui',
],
},
{
crateName: 'agent-runtime',
reason: 'agent-runtime must own portable runtime decisions without concrete service or product implementations',
forbiddenDeps: [
'bitfun-core',
'bitfun-ai-adapters',
'bitfun-services-core',
'bitfun-services-integrations',
'bitfun-tool-packs',
'bitfun-product-domains',
'bitfun-transport',
'terminal-core',
'tauri',
'reqwest',
'git2',
'rmcp',
'image',
'tokio-tungstenite',
'bitfun-cli',
'ratatui',
'crossterm',
'arboard',
'syntect-tui',
],
},
{
crateName: 'agent-tools',
reason: 'agent-tools must not depend on concrete service or product runtime implementations',
Expand Down Expand Up @@ -273,6 +299,32 @@ const dependencyProfileRules = [
'syntect-tui',
],
},
{
crateName: 'agent-runtime',
profileName: 'default agent runtime decision profile',
reason: 'agent-runtime default profile must not compile concrete services or product surfaces',
forbiddenNonOptionalDeps: [
'bitfun-core',
'bitfun-ai-adapters',
'bitfun-services-core',
'bitfun-services-integrations',
'bitfun-tool-packs',
'bitfun-product-domains',
'bitfun-transport',
'terminal-core',
'tauri',
'reqwest',
'git2',
'rmcp',
'image',
'tokio-tungstenite',
'bitfun-cli',
'ratatui',
'crossterm',
'arboard',
'syntect-tui',
],
},
{
crateName: 'agent-tools',
profileName: 'tool contract-only profile',
Expand Down Expand Up @@ -3027,9 +3079,14 @@ const requiredContentRules = [
},
{
regex:
/use bitfun_runtime_ports::\{[\s\S]*DialogSessionStateFact[\s\S]*DialogSubmitQueueAction[\s\S]*DialogSubmitQueueFacts[\s\S]*DialogTurnOutcomeKind[\s\S]*resolve_dialog_submit_queue_action[\s\S]*should_skip_agent_session_reply_contract[\s\S]*should_suppress_agent_session_cancelled_reply_contract[\s\S]*\};/,
/use bitfun_runtime_ports::\{(?=[\s\S]*DialogSessionStateFact)(?=[\s\S]*DialogSubmitQueueAction)(?=[\s\S]*DialogSubmitQueueFacts)(?=[\s\S]*DialogTurnOutcomeKind)(?=[\s\S]*resolve_dialog_submit_queue_action)(?=[\s\S]*should_skip_agent_session_reply_contract)(?=[\s\S]*should_suppress_agent_session_cancelled_reply_contract)[\s\S]*\};/,
message: 'missing dialog scheduler decision contract import',
},
{
regex:
/use bitfun_agent_runtime::scheduler::\{(?=[\s\S]*BackgroundDeliveryAction)(?=[\s\S]*BackgroundDeliveryFacts)(?=[\s\S]*resolve_background_delivery_action)[\s\S]*\};/,
message: 'missing agent-runtime background delivery decision import',
},
],
},
{
Expand Down Expand Up @@ -3276,7 +3333,7 @@ const requiredContentRules = [
{
path: 'src/crates/services-integrations/src/remote_connect.rs',
reason:
'services-integrations must own remote-connect wire, tracker, dialog, file, and image adapter contracts',
'services-integrations must own remote-connect wire/response assembly and preserve remote owner compatibility re-exports',
patterns: [
{
regex: /\bpub struct RemoteSessionStateTracker\b/,
Expand Down Expand Up @@ -3407,7 +3464,7 @@ const requiredContentRules = [
message: 'missing remote workspace file-info reader',
},
{
regex: /\bpub trait RemoteWorkspaceFileRuntimeHost\b/,
regex: /\bRemoteWorkspaceFileRuntimeHost\b/,
message: 'missing remote workspace file runtime host contract',
},
{
Expand Down Expand Up @@ -3443,11 +3500,11 @@ const requiredContentRules = [
message: 'missing remote answer response assembly helper',
},
{
regex: /\bpub struct RemoteWorkspaceFacts\b/,
regex: /\bRemoteWorkspaceFacts\b/,
message: 'missing remote workspace response facts DTO',
},
{
regex: /\bpub struct RemoteSessionMetadata\b/,
regex: /\bRemoteSessionMetadata\b/,
message: 'missing remote session response metadata DTO',
},
{
Expand Down Expand Up @@ -6803,6 +6860,21 @@ function runManifestParserSelfTest() {
if (!runtimeServicesProfile?.forbiddenNonOptionalDeps.includes('tool-runtime')) {
throw new Error('runtime-services dependency profile must forbid tool runtime implementations');
}
const agentRuntimeRule = lightweightBoundaryRules.find(
(rule) => rule.crateName === 'agent-runtime',
);
if (!agentRuntimeRule?.forbiddenDeps.includes('bitfun-core')) {
throw new Error('agent-runtime lightweight boundary must forbid bitfun-core');
}
if (!agentRuntimeRule?.forbiddenDeps.includes('bitfun-services-integrations')) {
throw new Error('agent-runtime lightweight boundary must forbid concrete service integrations');
}
const agentRuntimeProfile = dependencyProfileRules.find(
(rule) => rule.crateName === 'agent-runtime',
);
if (!agentRuntimeProfile?.forbiddenNonOptionalDeps.includes('tauri')) {
throw new Error('agent-runtime dependency profile must forbid product surface dependencies');
}
const agentToolsManifestRule = forbiddenContentUnderRules.find(
(rule) => rule.path === 'src/crates/agent-tools/src',
);
Expand Down Expand Up @@ -6852,6 +6924,14 @@ function runManifestParserSelfTest() {
'AgentTurnCancellationPort',
'RemoteControlStatePort',
'RuntimeEventSink',
'RemoteWorkspaceFacts',
'RemoteWorkspaceRuntimeHost',
'RemoteWorkspacePort',
'RemoteWorkspaceFileRuntimeHost',
'RemoteProjectionPort',
'RemoteInitialSyncRuntimeHost',
'remote_workspace_contracts_preserve_workspace_and_session_facts',
'remote_projection_contract_preserves_file_chunk_identity',
'remote_image',
'DialogTriggerSource',
'dialog_trigger_source_reuses_agent_submission_source_contract',
Expand Down Expand Up @@ -6920,6 +7000,27 @@ function runManifestParserSelfTest() {
'missing_optional_capability_returns_typed_unsupported_error',
'capability_availability_reports_optional_service_status_without_side_effects',
'builder_rejects_port_registered_under_the_wrong_capability',
'registered_remote_ports_expose_owner_contract_methods',
],
},
{
path: 'src/crates/agent-runtime/src/scheduler.rs',
contracts: [
'BackgroundDeliveryFacts',
'BackgroundDeliveryAction',
'resolve_background_delivery_action',
'follow_up_submission_policy',
'SubmitAgentSessionFollowUp',
'InjectIntoRunningTurn',
],
},
{
path: 'src/crates/agent-runtime/tests/scheduler_contracts.rs',
contracts: [
'background_delivery_injects_when_session_is_processing',
'background_delivery_starts_agent_session_follow_up_when_session_is_not_processing',
'background_delivery_follow_up_uses_agent_session_source_semantics',
'background_delivery_injection_does_not_expose_follow_up_policy',
],
},
{
Expand Down
24 changes: 24 additions & 0 deletions src/crates/agent-runtime/AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# agent-runtime Agent Guide

Scope: this guide applies to `src/crates/agent-runtime`.

`bitfun-agent-runtime` owns portable agent runtime decisions that can be built
and tested without `bitfun-core`.

## Guardrails

- Do not depend on `bitfun-core`, app crates, Tauri, ACP protocol, web UI,
concrete service crates, or product-domain implementations.
- Keep concrete scheduler/session lifecycle execution in `bitfun-core` until a
reviewed owner migration proves behavior equivalence.
- Prefer pure facts and decisions first: queue policy, background delivery,
cancellation routing, runtime event facts, and registry visibility.
- Add focused tests before moving any runtime decision into this crate.

## Verification

```bash
cargo test -p bitfun-agent-runtime
node scripts/check-core-boundaries.mjs
cargo check -p bitfun-core --features product-full
```
13 changes: 13 additions & 0 deletions src/crates/agent-runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "bitfun-agent-runtime"
version.workspace = true
authors.workspace = true
edition.workspace = true
description = "Agent runtime contracts and owner decisions for BitFun"

[lib]
name = "bitfun_agent_runtime"
crate-type = ["rlib"]

[dependencies]
bitfun-runtime-ports = { path = "../runtime-ports" }
6 changes: 6 additions & 0 deletions src/crates/agent-runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! Agent runtime owner contracts.
//!
//! This crate owns runtime decisions that can be built and tested without
//! depending on `bitfun-core` concrete session or scheduler lifecycle.

pub mod scheduler;
Loading
Loading