Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(contacts): preserve confirmed sync conflicts

Chris7bda0d1f9336
diff --git a/packages/maker-core/src/contacts/__tests__/contactsSync.test.ts b/packages/maker-core/src/contacts/__tests__/contactsSync.test.ts
index 660d122675..9fae1b1a2c 100644
--- a/packages/maker-core/src/contacts/__tests__/contactsSync.test.ts
+++ b/packages/maker-core/src/contacts/__tests__/contactsSync.test.ts
@@ -36,7 +36,9 @@ describe("contacts device sync", () => {
for (const db of databases.splice(0)) db.close();
});
- function createStore(config?: { maxIdentityValueLen: number }): MakerContactsStore {
+ function createStore(config?: {
+ maxIdentityValueLen: number;
+ }): MakerContactsStore {
const db = new DatabaseCtor(":memory:");
databases.push(db);
const store = new MakerContactsStore({ db, logger: noopLogger(), config });
@@ -246,6 +248,39 @@ describe("contacts device sync", () => {
expect(b.listContacts({ status: "pending" })).toHaveLength(2);
});
+ it("用户确认冲突联系人后,reconcile 和设备往返不会重新变成待确认", () => {
+ const a = createStore();
+ const b = createStore();
+ a.activateDeviceSync();
+ b.activateDeviceSync();
+ const aContact = a.createContact({
+ kind: "person",
+ displayName: "甲",
+ identities: [{ platform: "email", value: "[email protected]" }],
+ });
+ b.createContact({
+ kind: "person",
+ displayName: "乙",
+ identities: [{ platform: "email", value: "[email protected]" }],
+ });
+
+ exchange(a, b);
+ expect(a.listContacts({ status: "pending" })).toHaveLength(2);
+
+ a.updateContact(aContact.id, { status: "confirmed" });
+ expect(a.stats().pending).toBe(1);
+ // 这次读取会捕获用户确认,并重建同步投影;旧实现会在这里再次打回 pending。
+ expect(a.readDeviceSyncState()).not.toBeNull();
+ expect(a.stats().pending).toBe(1);
+
+ exchange(b, a);
+ exchange(a, b);
+ expect(a.getContact(aContact.id).status).toBe("confirmed");
+ expect(b.getContact(aContact.id).status).toBe("confirmed");
+ expect(a.stats().pending).toBe(1);
+ expect(b.stats().pending).toBe(1);
+ });
+
it("FTS 重建失败后重复接收相同状态仍会重试", () => {
const a = createStore();
const b = createStore();
diff --git a/packages/maker-core/src/contacts/sync/materialize.ts b/packages/maker-core/src/contacts/sync/materialize.ts
index 8b58e97e79..4fe3d6fae9 100644
--- a/packages/maker-core/src/contacts/sync/materialize.ts
+++ b/packages/maker-core/src/contacts/sync/materialize.ts
@@ -16,6 +16,8 @@ import {
type ContactsSnapshotMembership,
type ContactsSnapshotRelation,
type ContactsSyncEntity,
+ type ContactsSyncIdentityValue,
+ type ContactsSyncStamp,
type ContactsSyncState,
} from "./types.js";
@@ -70,6 +72,9 @@ function uniqueBy<T>(
export function materializeContactsSyncState(
state: ContactsSyncState,
): ContactsDataSnapshot {
+ const contactsById = new Map(
+ state.contacts.map((record) => [record.id, record]),
+ );
const contacts = state.contacts
.filter((record) => !record.deleted)
.map<ContactsSnapshotContact>((record) => ({
@@ -102,17 +107,35 @@ export function materializeContactsSyncState(
contactIds.has(record.value.value.contactId),
);
const identityOwners = new Map<string, Set<string>>();
+ const identityRecordsByKey = new Map<
+ string,
+ Array<ContactsSyncEntity<ContactsSyncIdentityValue>>
+ >();
+ const latestConflictStampByContact = new Map<string, ContactsSyncStamp>();
for (const record of identityCandidates) {
const value = record.value.value;
const key = `${value.platform}\u0000${value.normalizedValue}`;
const owners = identityOwners.get(key) ?? new Set<string>();
owners.add(value.contactId);
identityOwners.set(key, owners);
+ const records = identityRecordsByKey.get(key) ?? [];
+ records.push(record);
+ identityRecordsByKey.set(key, records);
}
const conflictedContactIds = new Set<string>();
- for (const owners of identityOwners.values()) {
+ for (const [key, owners] of identityOwners.entries()) {
if (owners.size <= 1) continue;
- for (const contactId of owners) conflictedContactIds.add(contactId);
+ for (const record of identityRecordsByKey.get(key) ?? []) {
+ const value = record.value.value;
+ conflictedContactIds.add(value.contactId);
+ const previous = latestConflictStampByContact.get(value.contactId);
+ if (
+ !previous ||
+ compareContactsSyncStamp(previous, record.value.stamp) < 0
+ ) {
+ latestConflictStampByContact.set(value.contactId, record.value.stamp);
+ }
+ }
}
const identities = uniqueBy(
@@ -127,8 +150,20 @@ export function materializeContactsSyncState(
// 同一身份被并发分给不同联系人时,SQLite 只能物化一个确定性赢家;把所有
// 相关档案标成待确认,避免另一边被隐藏后用户完全不知道发生过冲突。
+ // 用户确认后会产生一个晚于冲突身份 stamp 的 status stamp;此时尊重用户
+ // 的裁决,不要在下一次 reconcile / 往返同步时把它重新打回 pending。
for (const contact of contacts) {
- if (conflictedContactIds.has(contact.id)) contact.status = "pending";
+ if (!conflictedContactIds.has(contact.id)) continue;
+ const status = contactsById.get(contact.id)?.status;
+ const conflictStamp = latestConflictStampByContact.get(contact.id);
+ if (
+ status &&
+ conflictStamp &&
+ compareContactsSyncStamp(status.stamp, conflictStamp) > 0
+ ) {
+ continue;
+ }
+ contact.status = "pending";
}
const events = liveEntities(state.events)