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
12 changes: 6 additions & 6 deletions Cargo.lock

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

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "3"
members = ["crates/kc-wire", "crates/kc-proto", "crates/kc-store", "crates/kc-relay", "crates/kc-cli", "crates/kc-sync"]

[workspace.package]
version = "0.3.1"
version = "0.3.2"
edition = "2024"
rust-version = "1.88"
license = "MIT"
Expand All @@ -15,10 +15,10 @@ authors = ["Dcode <support@1pass.dev>"]
# `dep = { workspace = true, features = [...] }`. A lane that needs a crate that
# isn't listed here may add it to its own Cargo.toml directly.
[workspace.dependencies]
kc-wire = { path = "crates/kc-wire", version = "0.3.1" }
kc-proto = { path = "crates/kc-proto", version = "0.3.1" }
kc-store = { path = "crates/kc-store", version = "0.3.1" }
kc-sync = { path = "crates/kc-sync", version = "0.3.1" }
kc-wire = { path = "crates/kc-wire", version = "0.3.2" }
kc-proto = { path = "crates/kc-proto", version = "0.3.2" }
kc-store = { path = "crates/kc-store", version = "0.3.2" }
kc-sync = { path = "crates/kc-sync", version = "0.3.2" }

anyhow = "1"
thiserror = "2"
Expand Down
1 change: 1 addition & 0 deletions crates/kc-cli/locales/de.toml
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@
"Sync item held; run `kc sync review`." = "Synchronisierungselement zurückgehalten; `kc sync review` ausführen."
"{name} is in conflict; settle it with `kc sync resolve {name}` first." = "{name} steht im Konflikt; zuerst mit `kc sync resolve {name}` auflösen."
"{name} changed on this device meanwhile and is now a conflict; settle it with `kc sync resolve {name}`." = "{name} wurde inzwischen auf diesem Gerät geändert und steht jetzt im Konflikt; mit `kc sync resolve {name}` auflösen."
"{name} from {peer} was replaced by a newer change; run `kc sync review` again." = "{name} von {peer} wurde durch eine neuere Änderung ersetzt; `kc sync review` erneut ausführen."
"removed from sync" = "aus der Synchronisierung entfernt"
"hasn't answered the sync handshake yet" = "hat auf den Sync-Handshake noch nicht geantwortet"
"paused after too many changes" = "nach zu vielen Änderungen pausiert"
Expand Down
1 change: 1 addition & 0 deletions crates/kc-cli/locales/es.toml
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@
"Sync item held; run `kc sync review`." = "Elemento de sincronización retenido; ejecuta `kc sync review`."
"{name} is in conflict; settle it with `kc sync resolve {name}` first." = "{name} está en conflicto; resuélvelo primero con `kc sync resolve {name}`."
"{name} changed on this device meanwhile and is now a conflict; settle it with `kc sync resolve {name}`." = "{name} cambió en este dispositivo mientras tanto y ahora está en conflicto; resuélvelo con `kc sync resolve {name}`."
"{name} from {peer} was replaced by a newer change; run `kc sync review` again." = "{name} de {peer} fue reemplazado por un cambio más reciente; ejecuta `kc sync review` de nuevo."
"removed from sync" = "quitado de la sincronización"
"hasn't answered the sync handshake yet" = "aún no ha respondido al saludo de sincronización"
"paused after too many changes" = "en pausa tras demasiados cambios"
Expand Down
1 change: 1 addition & 0 deletions crates/kc-cli/locales/fr.toml
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@
"Sync item held; run `kc sync review`." = "Élément de synchronisation en attente ; exécutez `kc sync review`."
"{name} is in conflict; settle it with `kc sync resolve {name}` first." = "{name} est en conflit ; résolvez-le d’abord avec `kc sync resolve {name}`."
"{name} changed on this device meanwhile and is now a conflict; settle it with `kc sync resolve {name}`." = "{name} a été modifié entre-temps sur cet appareil et est maintenant en conflit ; résolvez-le avec `kc sync resolve {name}`."
"{name} from {peer} was replaced by a newer change; run `kc sync review` again." = "{name} depuis {peer} a été remplacé par une modification plus récente ; exécutez à nouveau `kc sync review`."
"removed from sync" = "retiré de la synchronisation"
"hasn't answered the sync handshake yet" = "n’a pas encore répondu à la négociation de synchronisation"
"paused after too many changes" = "en pause après trop de modifications"
Expand Down
1 change: 1 addition & 0 deletions crates/kc-cli/locales/ja.toml
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@
"Sync item held; run `kc sync review`." = "同期項目を保留しました。`kc sync review`を実行してください。"
"{name} is in conflict; settle it with `kc sync resolve {name}` first." = "{name} は競合中です。先に `kc sync resolve {name}` で解決してください。"
"{name} changed on this device meanwhile and is now a conflict; settle it with `kc sync resolve {name}`." = "その間にこのデバイスで {name} が変更されたため、競合になりました。`kc sync resolve {name}` で解決してください。"
"{name} from {peer} was replaced by a newer change; run `kc sync review` again." = "{peer} からの {name} の変更は、より新しい変更に置き換えられました。`kc sync review` をもう一度実行してください。"
"removed from sync" = "同期から外した"
"hasn't answered the sync handshake yet" = "同期ハンドシェイクの応答待ち"
"paused after too many changes" = "変更が多すぎるため一時停止中"
Expand Down
1 change: 1 addition & 0 deletions crates/kc-cli/locales/ko.toml
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@
"Sync item held; run `kc sync review`." = "동기화 항목을 보류했습니다. `kc sync review`를 실행하세요."
"{name} is in conflict; settle it with `kc sync resolve {name}` first." = "{name} 항목이 충돌 중입니다. 먼저 `kc sync resolve {name}`로 해결하세요."
"{name} changed on this device meanwhile and is now a conflict; settle it with `kc sync resolve {name}`." = "그사이 이 기기에서 {name} 항목이 바뀌어 충돌 상태가 되었습니다. `kc sync resolve {name}`로 해결하세요."
"{name} from {peer} was replaced by a newer change; run `kc sync review` again." = "{peer}에서 온 {name} 변경은 더 최근 변경으로 대체되었습니다. `kc sync review`를 다시 실행하세요."
"removed from sync" = "동기화에서 뺌"
"hasn't answered the sync handshake yet" = "아직 동기화 연결 응답 없음"
"paused after too many changes" = "변경이 너무 많아 일시 중지됨"
Expand Down
1 change: 1 addition & 0 deletions crates/kc-cli/locales/zh-Hans.toml
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@
"Sync item held; run `kc sync review`." = "同步项目已保留;请运行 `kc sync review`。"
"{name} is in conflict; settle it with `kc sync resolve {name}` first." = "{name} 存在冲突,请先用 `kc sync resolve {name}` 解决。"
"{name} changed on this device meanwhile and is now a conflict; settle it with `kc sync resolve {name}`." = "{name} 在此期间已在本设备上更改,现已成为冲突,请用 `kc sync resolve {name}` 解决。"
"{name} from {peer} was replaced by a newer change; run `kc sync review` again." = "来自 {peer} 的 {name} 更改已被更新的更改取代,请再次运行 `kc sync review`。"
"removed from sync" = "已移出同步"
"hasn't answered the sync handshake yet" = "尚未回应同步握手"
"paused after too many changes" = "因更改过多已暂停"
Expand Down
1 change: 1 addition & 0 deletions crates/kc-cli/locales/zh-Hant.toml
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@
"Sync item held; run `kc sync review`." = "同步項目已保留;請執行 `kc sync review`。"
"{name} is in conflict; settle it with `kc sync resolve {name}` first." = "{name} 有衝突,請先用 `kc sync resolve {name}` 解決。"
"{name} changed on this device meanwhile and is now a conflict; settle it with `kc sync resolve {name}`." = "{name} 在此期間已在本裝置上變更,現已成為衝突,請用 `kc sync resolve {name}` 解決。"
"{name} from {peer} was replaced by a newer change; run `kc sync review` again." = "來自 {peer} 的 {name} 變更已被更新的變更取代,請再次執行 `kc sync review`。"
"removed from sync" = "已移出同步"
"hasn't answered the sync handshake yet" = "尚未回應同步交握"
"paused after too many changes" = "因變更過多已暫停"
Expand Down
37 changes: 30 additions & 7 deletions crates/kc-cli/src/sync/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ fn offer<H: Host>(ctx: &mut Ctx<'_, H>, s: &mut State, name: &str, peers: &[Stri
}
pub(crate) fn remove<H: Host>(ctx: &Ctx<'_, H>, s: &mut State, name: &str) -> Result<()> {
let (registration, mut peers) = s.generations.rm(name).map_err(|_| state_error())?;
// The known vids' sync/fp records stay: after `sync add` of the same name, a peer could
// otherwise reuse an old vid with another value under the new generation unquarantined.
if let Some(item) = s.items.remove(name) {
peers.extend(item.scope.into_iter().map(PeerId));
}
Expand Down Expand Up @@ -423,6 +425,7 @@ pub(crate) fn review<H: Host>(ctx: &mut Ctx<'_, H>) -> Result<i32> {
reject_offer(&mut s, delivery, ctx.host.now());
forget_binding(&mut s, &c.version.name, c.version.vid);
} else {
drop_binding(&mut s, &c.version.name, c.version.vid);
s.receipts.insert(
delivery,
(
Expand All @@ -447,19 +450,39 @@ pub(crate) fn review<H: Host>(ctx: &mut Ctx<'_, H>) -> Result<i32> {
}
}
}
// Accepting one candidate clears the item's other candidates.
let mut applied = std::collections::BTreeSet::new();
for c in accepts {
if s.frozen {
break;
}
if let Some(current) = s.candidates.get(&c.version.slot).cloned()
&& current.category != CandidateKind::Conflict
{
match transaction::accept(ctx, &mut s, &current)? {
Accepted::Applied => done.push(tr!(
"Accepted {name} from {peer}.",
// The candidate may have changed since it was listed: retired as superseded by a
// newer change from the same peer, or turned into a conflict.
let Some(current) = s.candidates.get(&c.version.slot).cloned() else {
if !applied.contains(&c.version.name) {
done.push(tr!(
"{name} from {peer} was replaced by a newer change; run `kc sync review` again.",
name = c.version.name,
peer = label(ctx, &c.peer)?
)),
));
}
continue;
};
if current.category == CandidateKind::Conflict {
done.push(tr!(
"{name} is in conflict; settle it with `kc sync resolve {name}` first.",
name = c.version.name
));
} else {
match transaction::accept(ctx, &mut s, &current)? {
Accepted::Applied => {
done.push(tr!(
"Accepted {name} from {peer}.",
name = c.version.name,
peer = label(ctx, &c.peer)?
));
applied.insert(c.version.name.clone());
}
Accepted::Conflict => done.push(tr!(
"{name} changed on this device meanwhile and is now a conflict; settle it with `kc sync resolve {name}`.",
name = c.version.name
Expand Down
79 changes: 69 additions & 10 deletions crates/kc-cli/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,13 @@ fn reserve_keeping<H: Host>(
s.candidates.retain(|_, c| c.version.slot != victim);
for c in evicted {
s.gc.insert(c.base_record);
// An invalid binding's record belongs to the legitimate vid.
if !c.invalid_binding {
// Only a proposal's binding goes with it: held and conflict bindings stay
// for the immutable-vid check while known, as in retire_superseded. An
// invalid binding's record belongs to the legitimate vid.
if c.category == CandidateKind::Proposal && !c.invalid_binding {
forget_binding(s, &c.version.name, c.version.vid);
} else {
drop_binding(s, &c.version.name, c.version.vid);
}
}
s.slots.remove(&victim);
Expand All @@ -252,23 +256,68 @@ fn reserve_keeping<H: Host>(
}
}
}
/// Whether the head, lineage, a version or a candidate still refers to this vid binding.
/// A candidate's parents and lineage count: accepting it makes them the item's lineage.
fn binding_referenced(s: &State, name: &str, vid: Vid) -> bool {
s.items
.get(name)
.is_some_and(|i| i.head == vid || i.lineage.contains(&vid))
|| s.versions.values().any(|v| v.name == name && v.vid == vid)
|| s.candidates.values().any(|other| {
other.version.name == name
&& (other.version.vid == vid
|| other.version.parents.contains(&vid)
|| other.version.lineage.contains(&vid))
})
}
fn in_known(s: &State, name: &str, vid: Vid) -> bool {
s.items.get(name).is_some_and(|i| i.known.contains(&vid))
}
/// The `name:vid` key of `unbound` and `unbind_watch`.
fn binding_key(name: &str, vid: Vid) -> String {
format!("{}:{}", name, vid_key(vid))
}
/// Drops a dropped proposal's vid binding (`sync/fp/...` record, unbound entry, known vid)
/// unless the head, a version or another candidate still refers to it.
/// unless something else still refers to it; a referenced binding outside known is
/// watched until that reference goes away.
fn forget_binding(s: &mut State, name: &str, vid: Vid) {
let referenced = s.items.get(name).is_some_and(|i| i.head == vid)
|| s.versions.values().any(|v| v.name == name && v.vid == vid)
|| s.candidates
.values()
.any(|other| other.version.name == name && other.version.vid == vid);
if referenced {
let key = binding_key(name, vid);
if binding_referenced(s, name, vid) {
if !in_known(s, name, vid) {
s.unbind_watch.insert(key);
}
return;
}
s.unbound.remove(&format!("{}:{}", name, vid_key(vid)));
s.unbound.remove(&key);
s.unbind_watch.remove(&key);
if let Some(item) = s.items.get_mut(name) {
item.known.retain(|known| *known != vid);
}
s.gc.insert(record_slot(name, vid));
}
/// Drops a vid binding once it has left the item's known cache and nothing else refers to
/// it, so neither the known cap nor removed candidates leave records behind.
fn drop_binding(s: &mut State, name: &str, vid: Vid) {
if !in_known(s, name, vid) {
forget_binding(s, name, vid);
}
}
/// Caps the item's known cache, keeping the head, and drops the bindings that fall out.
fn trim_known(s: &mut State, name: &str) {
let Some(item) = s.items.get_mut(name) else {
return;
};
let mut dropped = Vec::new();
while item.known.len() > kc_sync::MAX_KNOWN {
let Some(index) = item.known.iter().position(|vid| *vid != item.head) else {
break;
};
dropped.push(item.known.remove(index));
}
for vid in dropped {
drop_binding(s, name, vid);
}
}

/// A rejected offer stays blocked while its sender keeps retrying.
const REJECTED_OFFER_TTL: i64 = kc_wire::limits::INBOX_IDLE_DELETE_DAYS as i64 * 86400;
Expand Down Expand Up @@ -326,6 +375,16 @@ fn prune(s: &mut State, now: i64) {
forget_binding(s, &name, vid);
}
}
// A watched binding back in known is trim_known's again; the rest is retried here.
for entry in std::mem::take(&mut s.unbind_watch) {
let Some((name, vid)) = entry
.rsplit_once(':')
.and_then(|(name, hex)| Some((name.to_owned(), parse_vid(hex)?)))
else {
continue;
};
drop_binding(s, &name, vid);
}
}
fn parse_vid(hex: &str) -> Option<Vid> {
let mut vid = [0; 16];
Expand Down
29 changes: 20 additions & 9 deletions crates/kc-cli/src/sync/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ fn change_received<H: Host>(
if !item.known.contains(&c.new_vid) {
item.known.push(c.new_vid);
}
trim_known(s, name);
}
Transition::Unchanged => {}
// No verified approval is ever constructed in Phase 1.
Expand Down Expand Up @@ -678,15 +679,11 @@ pub(super) fn store_candidate<H: Host>(
if !retire
&& !invalid_binding
&& let Some(item) = s.items.get_mut(&v.name)
&& !item.known.contains(&v.vid)
{
if !item.known.contains(&v.vid) {
item.known.push(v.vid);
}
while item.known.len() > 256 {
let index = item.known.iter().position(|vid| *vid != item.head).unwrap();
item.known.remove(index);
}
item.known.push(v.vid);
}
trim_known(s, &v.name);
let key = v.slot.clone();
s.candidates.insert(
key,
Expand Down Expand Up @@ -775,9 +772,23 @@ pub(crate) fn legacy<H: Host>(
return Err(error);
}
let c = s.candidates[&slot].clone();
if transaction::accept(ctx, &mut s, &c)? == transaction::Accepted::Applied {
return Ok(Some(kc_proto::ItemStatus::Stored));
let line = match transaction::accept(ctx, &mut s, &c)? {
transaction::Accepted::Applied => return Ok(Some(kc_proto::ItemStatus::Stored)),
transaction::Accepted::Conflict => Some(tr!(
"{name} changed on this device meanwhile and is now a conflict; settle it with `kc sync resolve {name}`.",
name = name
)),
transaction::Accepted::Full => Some(tr!(
"Sync storage is full; review pending items before retrying."
)),
// accept() already said which conflict must be settled first.
transaction::Accepted::Unsettled => None,
};
s.cleanup(ctx)?;
if let Some(line) = line {
crate::commands::say!(ctx, "{line}");
}
return Ok(Some(kc_proto::ItemStatus::SkippedConflict));
}
s.cleanup(ctx)?;
crate::commands::say!(ctx, "{}", tr!("Sync item held; run `kc sync review`."));
Expand Down
Loading
Loading