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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 每次改动都要过的那几关。
#
# **价目表的比对在这里**(DESIGN.md §4.3.0):内置快照和
# **价目表的比对在这里**:内置快照和
# `crates/tw-pricing/data/verified.yaml` 对不上就构建失败 —— 那是挡住
# 「打包了错数据」的唯一手段。
name: CI
Expand Down Expand Up @@ -40,7 +40,7 @@ jobs:
run: cargo build --release -p twcore

# **接缝上的失败模式单元测试看不见**:文件权限、socket、某个端点
# 在真二进制上根本没注册。这个项目的前四个真 bug 都是这么被逮到的
# (DESIGN.md §9.7)。脚本自己会 build,这一步只是跑它。
# 在真二进制上根本没注册。这个项目的前四个真 bug 都是这么被逮到的。
# 脚本自己会 build,这一步只是跑它。
- name: Smoke (real binary, real socket, real data plane)
run: ./scripts/smoke.sh
4 changes: 2 additions & 2 deletions .github/workflows/pricing.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 价目表会不会已经过期了。
#
# **这是个提醒,不是一道关卡**(DESIGN.md §4.3.0)。真正的关卡是
# **这是个提醒,不是一道关卡**。真正的关卡是
# `tw-pricing` 里那条比对测试 —— 它每次 CI 都跑,对不上就红。
#
# 这个 workflow 只回答另一个问题:**内置快照放了多久了。**任何第三方
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:
echo "pinned: $PINNED"
echo "upstream: $LATEST"
if [ "$PINNED" != "$LATEST" ]; then
# **不自动更新。**§4.3.0:pin 到具体的 commit,不要用 main ——
# **不自动更新。**pin 到具体的 commit,不要用 main ——
# 自动跟随意味着两次构建可能拿到不同的价格,而「昨天算出来的
# 钱和今天不一样」是没法解释的。
echo "::warning::Upstream has a newer revision. To take it, follow PROVENANCE.md and re-verify verified.yaml against the vendors' own pricing pages"
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ clean the diff is:
- **Never present an estimate as exact.** Cost is three states —
measured, estimated, and no price at all. Treating the third as 0
makes a total quietly wrong with nothing to signal it.
- **Observation must never block forwarding** (§4.7). Storage, pricing,
- **Observation must never block forwarding**. Storage, pricing,
and scanning run off bounded channels; a full channel drops the
observation rather than delaying the request.
- **Report, never auto-delete** (§5.3). The scanner has no write path,
- **Report, never auto-delete**. The scanner has no write path,
and there's a test that reads the product code to prove it.
- **Anything that bypasses the main pipeline re-applies its
protections.** Replay came close to being a legitimate way around
Expand Down
18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ license = "MIT"
repository = "https://github.com/ThinkWatchProject/ThinkWatch-Core"

# 二进制走 CalVer,crate 走 SemVer —— 两者是两回事,卖给不同的人。
# 见 DESIGN.md §9.6。这里是 crate 的版本。
# 这里是 crate 的版本。
version = "0.1.0"

[workspace.dependencies]
Expand Down Expand Up @@ -74,30 +74,30 @@ bytes = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_yaml_ng = "0.10"
# 文件监听。盯配置文件那一份(§3.8)
# 文件监听。盯配置文件那一份
notify = "8"
# 请求历史。bundled 是刻意的:零外部依赖(§8),用户不必装 sqlite
# 请求历史。bundled 是刻意的:零外部依赖,用户不必装 sqlite
rusqlite = { version = "0.40", features = ["bundled"] }
libc = "0.2"
# 内置价目表压缩着放(2.3 MB → 110 KB,§4.3.0)
# 内置价目表压缩着放(2.3 MB → 110 KB)
flate2 = "1"
# regex-automata / regex-syntax 已经在依赖树里(tracing 的 EnvFilter),
# 所以加它的增量很小
regex = "1"
# 保格式地改 TOML。接管 Codex 要写它的 config.toml,而重新序列化
# 会把用户的注释和排版一起抹掉(§7.11)
# 会把用户的注释和排版一起抹掉
toml_edit = "0.25"
# 只有它给节点的 span —— 而 span 是「只改一段字节」的全部前提(§3.8)。
# 只有它给节点的 span —— 而 span 是「只改一段字节」的全部前提。
# serde_yaml 已经归档,且本来就不保留格式。
saphyr-parser = "0.0.12"

reqwest = { version = "0.13", default-features = false, features = ["stream", "json", "rustls", "socks", "http2", "form"] } # form: OAuth 换 token 是表单 POST(§3.6),密钥的百分号编码不自己写
reqwest = { version = "0.13", default-features = false, features = ["stream", "json", "rustls", "socks", "http2", "form"] } # form: OAuth 换 token 是表单 POST,密钥的百分号编码不自己写
# L1 分段测速要自己走握手 —— reqwest 把建连整个包起来,只给一个总时间,
# 而分段正是这一层的全部价值。版本跟着 reqwest 的传递依赖走,不引新的树。
rustls = "0.23"
rustls-platform-verifier = "0.7"
tokio-rustls = "0.26"
# WS 升级代理(§3.6)。**不带自己的 TLS**:连接用我们已经配好的 rustls
# WS 升级代理。**不带自己的 TLS**:连接用我们已经配好的 rustls
# 栈,否则会出现「数据面信任的根证书」和「WS 信任的」是两套
tokio-tungstenite = { version = "0.28", default-features = false, features = ["handshake"] }
base64 = "0.22"
Expand Down Expand Up @@ -136,6 +136,6 @@ urlencoding = "2"
lto = "thin"
codegen-units = 1
strip = "symbols"
# 保持 unwind:§4.7 的 catch_unwind 要靠它兜住单个请求的 panic。
# 保持 unwind:catch_unwind 要靠它兜住单个请求的 panic。
# abort 会让那层防护失效,整个进程跟着死 —— 而 core 死了所有 AI 客户端一起瞎。
panic = "unwind"
2 changes: 1 addition & 1 deletion bin/twcore/src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! 网关端口是固定的(客户端配置里写死了它),所以「上次没退干净」这件事
//! 会表现为启动失败,而错误信息必须能说清是这个原因 —— 否则用户会以为
//! 自己电脑坏了(DESIGN.md §2.2、§2.4)。
//! 自己电脑坏了。

use std::io::Write;
use std::path::{Path, PathBuf};
Expand Down
37 changes: 18 additions & 19 deletions bin/twcore/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! twcore —— 本地 AI 网关引擎。
//!
//! 它和 UI 在用户眼里是同一个程序(DESIGN.md §2.2.1):UI 起它、UI 关它、
//! 它和 UI 在用户眼里是同一个程序:UI 起它、UI 关它、
//! 它自己不注册开机自启。这里做的是引擎侧那一半 —— 单实例、父进程守望、
//! 干净退出。

Expand Down Expand Up @@ -141,9 +141,9 @@ fn main() -> Result<()> {
}
}

/// 静态扫描(§5.3)。**只报告,不删任何东西。**
/// 静态扫描。**只报告,不删任何东西。**
fn cmd_scan(config: &Path, projects: Vec<PathBuf>, inventory: bool) -> Result<()> {
// 规则住在 config.yaml 的 `security.scan_rules` 里(§3.1:只有一份
// 规则住在 config.yaml 的 `security.scan_rules` 里(只有一份
// 配置文件)。读不出配置时用内置那套 —— 扫描不该因为配置坏了就停摆
let user = tw_config::load(config)
.map(|c| c.security.scan_rules.clone())
Expand Down Expand Up @@ -197,7 +197,7 @@ fn cmd_scan(config: &Path, projects: Vec<PathBuf>, inventory: bool) -> Result<()
println!("⚠ 读不动:{u}");
}
if r.findings.is_empty() {
// §0.6:没风险的时候要说「安全」,而不是什么都不显示
// 没风险的时候要说「安全」,而不是什么都不显示
println!("\n✓ 没发现问题。");
return Ok(());
}
Expand Down Expand Up @@ -409,7 +409,7 @@ fn fmt_time(ms: u64) -> String {
}
}

/// L1 测速(§4.6)。**逐个测,不并发** —— 六条线一起抢带宽测出来的
/// L1 测速。**逐个测,不并发** —— 六条线一起抢带宽测出来的
/// 握手时间不是任何一条线的真实值,而这一层存在的全部意义就是那几个
/// 数字准不准。
fn cmd_speed(path: &Path, provider: Option<String>, proxy: Option<String>) -> Result<()> {
Expand Down Expand Up @@ -556,13 +556,13 @@ fn cmd_check(path: &Path) -> Result<()> {
);
// OAuth 不在这里换 token:那是一次网络往返,而 check
// 是个用户期望立刻返回的命令。但**能离线查的都要查** ——
// 这几样写错了,症状全是网关起来之后一片 401(§3.6)。
// 这几样写错了,症状全是网关起来之后一片 401。
if let Some(o) = p.key.oauth() {
if o.refresh.trim().is_empty() {
println!(" ⚠ refresh token 是空的 —— 这家换不出 token");
}
// **本机的 http 不算明文过网。**报它是个假警报,而
// 假警报的代价是用户学会忽略这一栏的所有话(§0.6:
// 假警报的代价是用户学会忽略这一栏的所有话(
// 没有风险的时候要说「安全」,不是把话说满)。
// tw-redact 的内网规则出于同一个理由排除回环。
let loopback = o.endpoint.starts_with("http://127.0.0.1")
Expand Down Expand Up @@ -616,7 +616,7 @@ fn cmd_serve(path: &Path, port: Option<u16>, safe: bool, parent: Option<u32>) ->
}
};

// **配置不存在就地生成**(§7.6 第 1 步)。
// **配置不存在就地生成**(第 1 步)。
//
// 这里曾经是「加载失败,去跑一次 twcore init」——而那是错的:真正的
// 首次运行里根本没有人会去跑 init。UI 直接 spawn 的是 serve,于是
Expand All @@ -633,7 +633,7 @@ fn cmd_serve(path: &Path, port: Option<u16>, safe: bool, parent: Option<u32>) ->
}
let cfg = tw_config::load(path).with_context(|| format!("加载 {} 失败", path.display()))?;
// `--port` 是一个**显式的覆盖**,配置文件不该推翻它。所以给了它
// 之后就不再跟着配置里的监听地址走(§3.8 的「温」那一级)。
// 之后就不再跟着配置里的监听地址走(「温」那一级)。
let overridden = port.is_some();
let mut listen = cfg.listen.gateway.clone();
if let Some(p) = port {
Expand All @@ -653,7 +653,7 @@ fn cmd_serve(path: &Path, port: Option<u16>, safe: bool, parent: Option<u32>) ->
let state = tw_gateway::AppState::new(cfg.clone())
.map_err(|e| anyhow::anyhow!("{}", e.message))?;
// **和观测那一层用同一份价目表。**两处各拿一份的话,「成本栏
// 显示的」和「按最便宜选的」会对不上(§4.3.0 的用户覆盖层就是
// 显示的」和「按最便宜选的」会对不上(用户覆盖层就是
// `cheapest` 唯一的判据来源)。读不了就用内置那份,转发照常。
match tw_pricing::Prices::builtin()
.and_then(|p| p.with_overrides(&dir.join("pricing.yaml")))
Expand All @@ -663,18 +663,17 @@ fn cmd_serve(path: &Path, port: Option<u16>, safe: bool, parent: Option<u32>) ->
}

// 观测这一层。**起不来不是致命的** —— 历史记录看不见,而网关
// 照常转发(§4.7)。所以这里所有的失败都只记一行日志。
// 照常转发。所以这里所有的失败都只记一行日志。
//
// body 的通道在这里建:**它是唯一同时看得见网关和存储的地方**,
// 而两边各有各的同形结构,是为了不让「观测」挂到「转发」下面
// (§9.0.1)。
// 而两边各有各的同形结构,是为了不让「观测」挂到「转发」下面。
let (body_tx, body_rx) = tokio::sync::mpsc::channel(tw_gateway::bodies::CHANNEL_CAP);
let store = build_store(&dir, state.bus.subscribe(), body_rx);
if store.is_some() {
state.set_body_sink(body_tx);
}

// 配置的唯一入口。UI、CLI、文件监听都从这里进(§3.8)。
// 配置的唯一入口。UI、CLI、文件监听都从这里进。
let manager = std::sync::Arc::new(tw_control::ConfigManager::new(
config_path,
state.clone(),
Expand All @@ -692,7 +691,7 @@ fn cmd_serve(path: &Path, port: Option<u16>, safe: bool, parent: Option<u32>) ->
};

// 控制面无论如何都要起来 —— **网关挂了的时候,用户最需要的恰恰
// 是能改配置**(§2.2.1)。安全模式就是「只有这一半」。
// 是能改配置**。安全模式就是「只有这一半」。
let control = tw_control::ControlState {
home: tw_control::home_dir(),
started: std::time::Instant::now(),
Expand All @@ -701,10 +700,10 @@ fn cmd_serve(path: &Path, port: Option<u16>, safe: bool, parent: Option<u32>) ->
gateway_addr: if safe { None } else { Some(addr.to_string()) },
store,
};
// 盯着客户端配置面(§5.3)。**只报告** —— 这条路径上没有任何
// 盯着客户端配置面。**只报告** —— 这条路径上没有任何
// 一处会改用户的文件。盯不住就只是少了「变更时告警」,页面上
// 那份「打开时扫一次」照常可用,所以说一句就继续。
// 凭据轮换要写回 config.yaml(§3.6)。**这是这个程序里唯一一次
// 凭据轮换要写回 config.yaml。**这是这个程序里唯一一次
// 不是人发起的配置写入** —— 理由是服务器换发新 refresh token 的
// 那一刻旧的就作废了,不写回等于让配置文件从那一秒起就是坏的。
tw_control::rotation::spawn(control.clone());
Expand Down Expand Up @@ -763,7 +762,7 @@ fn cmd_serve(path: &Path, port: Option<u16>, safe: bool, parent: Option<u32>) ->
return Ok(());
}

// 模型目录:后台去问每个上游有哪些模型(§3.9)。**不挡启动** ——
// 模型目录:后台去问每个上游有哪些模型。**不挡启动** ——
// 探测要打网络,而网关不该因为一次探测慢而起不来。
tw_gateway::spawn_catalog_refresh(state.clone());

Expand Down Expand Up @@ -793,7 +792,7 @@ fn cmd_serve(path: &Path, port: Option<u16>, safe: bool, parent: Option<u32>) ->
///
/// 这条边界值得写死在代码形状里:这个函数返回 `Option`,而不是
/// `Result` —— 调用方连处理错误的机会都不该有,因为没有任何一种
/// 处理方式是「不转发了」(§4.7)。
/// 处理方式是「不转发了」。
fn build_store(
dir: &Path,
events: tokio::sync::broadcast::Receiver<tw_api::Event>,
Expand Down
Loading
Loading