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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions bin/twcore/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,8 +904,8 @@ fn build_store(
bodies: tokio::sync::mpsc::Receiver<tw_gateway::BodyRecord>,
) -> Option<std::sync::Arc<tokio::sync::Mutex<tw_store::Recorder>>> {
let events = bus.subscribe();
let db = match tw_store::Db::open(&dir.join("data.db")) {
Ok(db) => db,
let (db, blobs) = match tw_store::open(dir) {
Ok(opened) => opened,
Err(e) => {
tracing::warn!(
"request recording could not start, so nothing is recorded this run; forwarding is unaffected: {e}"
Expand All @@ -931,7 +931,6 @@ fn build_store(
"could not read the last request id, so this run may overwrite the oldest records: {e}"
),
}
let blobs = tw_store::Blobs::new(dir.join("blobs"));
// 两边的 body 结构在这里对接。**一次移动,不复制** —— `Bytes` 的
// 克隆是引用计数。
let (tx, rx) = tokio::sync::mpsc::channel(tw_gateway::bodies::CHANNEL_CAP);
Expand Down
4 changes: 2 additions & 2 deletions bin/twcore/tests/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,14 @@ fn product_text_is_written_not_spoken() {
}

// 内置扫描规则的说明会原样出现在发现和工具调用告警里
let rules = root.join("crates/tw-scan/data/rules.yaml");
let rules = root.join("crates/tw-config/data/rules.yaml");
for (n, l) in std::fs::read_to_string(&rules).unwrap().lines().enumerate() {
let Some(why) = l.trim().strip_prefix("why:") else {
continue;
};
for w in SPOKEN.iter().filter(|w| why.contains(**w)) {
found.push(format!(
"crates/tw-scan/data/rules.yaml:{} 「{w}」 {why}",
"crates/tw-config/data/rules.yaml:{} 「{w}」 {why}",
n + 1
));
}
Expand Down
14 changes: 3 additions & 11 deletions crates/tw-adopt/src/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ fn originals_from(
.originals
.iter()
.map(|f| {
let path = record_path(f);
let path = f.path.clone();
let was = match f.was.as_str() {
"missing" => Was::Missing,
"secret" => match backed_val.as_ref().and_then(|v| lookup(v, &refs(&path))) {
Expand All @@ -321,14 +321,6 @@ fn originals_from(
.collect())
}

fn record_path(f: &sentinel::SidecarField) -> Vec<String> {
if f.path.is_empty() {
f.field.split('.').map(|s| s.to_string()).collect()
} else {
f.path.clone()
}
}

/// 落盘。**用户确认之后才该调到这里。**
pub fn apply(c: &Client, plan: &Plan, backup_root: &Path) -> Result<Applied, PlanError> {
let fmt = c.format;
Expand Down Expand Up @@ -516,7 +508,7 @@ pub fn plan_restore(c: &Client, home: &Path) -> Result<Plan, PlanError> {
let mut notes = Vec::new();
let mut targets = Vec::new();
for f in &rec.originals {
let p = record_path(f);
let p = f.path.clone();
let r = refs(&p);
let from_backup = backed_val.as_ref().and_then(|v| lookup(v, &r));
match (f.was.as_str(), from_backup, &f.value) {
Expand All @@ -543,7 +535,7 @@ pub fn plan_restore(c: &Client, home: &Path) -> Result<Plan, PlanError> {
// 否则「还原」之后会留下一个用户从来没有过的空段落。
if let Some(bv) = &backed_val {
for f in &rec.originals {
let p = record_path(f);
let p = f.path.clone();
for cut in (1..p.len()).rev() {
let anc = &p[..cut];
if lookup(bv, &refs(anc)).is_none()
Expand Down
9 changes: 4 additions & 5 deletions crates/tw-adopt/src/sentinel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,13 @@ pub fn strip(text: &str, prefix: &str) -> String {
out
}

/// 旁文件里的一个字段。**字段名用中文,因为读它的是人。**
/// 旁文件里的一个字段。
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SidecarField {
pub field: String,
/// 机器用的路径分段。**不能靠切 `field` 里的点号还原它** —— 键名
/// 本身就可能带点(`[projects."/Users/x/a.b"]` 这种),拆错了就会
/// 去改一个不存在的字段,然后「还原成功」地什么都没还原。
#[serde(default)]
pub path: Vec<String>,
/// 机器读的标记:`missing` / `value` / `secret`
pub was: String,
Expand Down Expand Up @@ -175,7 +174,7 @@ pub struct SidecarRecord {
/// 还原时要能把它**整个删掉** —— 否则「还原」之后会留下一个用户
/// 从来没有过的文件。但只在它还是空的时候删:用户可能在这期间往
/// 里加了自己的东西。
#[serde(rename = "file_created_by_us", default)]
#[serde(rename = "file_created_by_us")]
pub created_file: bool,
pub originals: Vec<SidecarField>,
}
Expand Down Expand Up @@ -334,8 +333,8 @@ mod tests {
#[test]
fn a_block_the_user_has_edited_is_left_alone_rather_than_guessed_at() {
// 认不出来就不动。宁可留下几行注释,也不能删掉别的东西。
let src = "# === ThinkWatch 接管开始 ===\n# 用户把结束标记删了\nmodel = \"x\"\n";
assert_eq!(strip(src, "#"), src);
let src = format!("# {BEGIN}\n# 用户把结束标记删了\nmodel = \"x\"\n");
assert_eq!(strip(&src, "#"), src);
assert_eq!(strip("完全没有哨兵\n", "#"), "完全没有哨兵\n");
}

Expand Down
Loading
Loading