zen-fs-config 0.9.1 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -474,7 +474,9 @@ interface GroupDescriptor {
474
474
  * 两个能力位互不蕴含。
475
475
  */
476
476
  /** 归属冲突的细分原因。 */
477
- type BackendConflictReason = 'kind-mismatch' | 'id-mismatch' | 'instance-mismatch' | 'unreadable' | 'claim-failed';
477
+ type BackendConflictReason = 'kind-mismatch' | 'id-mismatch' | 'instance-mismatch' | 'unreadable' | 'claim-failed'
478
+ /** 本组实例身份尚未确定(没有读到任何候选),本轮不判定、也不认领。 */
479
+ | 'local-unset';
478
480
  /**
479
481
  * 一个后端与远端根归属冲突的完整信息。
480
482
  *
@@ -1284,6 +1286,14 @@ declare class ConfigRepo implements IConfigRepo {
1284
1286
  private backendConflicts;
1285
1287
  /** Last known backend descriptors, for the synchronous unified handle API. */
1286
1288
  private backendDescriptorCache;
1289
+ /**
1290
+ * 本会话内被用户**显式**清空后端的数据组 id。
1291
+ *
1292
+ * 只有这类组允许把「0 个后端」的引导副本推给远端;启动时读到的空副本
1293
+ * (历史脏数据、或「清空本页配置」后本地新建的空组)绝不覆盖远端既有拓扑,
1294
+ * 见 `healEmptyBootstrapDescriptors()`。
1295
+ */
1296
+ private explicitEmptyDescriptors;
1287
1297
  /**
1288
1298
  * Tracks the background initial sync started by createConfigRepo().
1289
1299
  * `flush()` and `dispose()` will await this if it hasn't completed yet,
@@ -1759,6 +1769,18 @@ declare class ConfigRepo implements IConfigRepo {
1759
1769
  */
1760
1770
  private syncBootstrapDescriptor;
1761
1771
  listAppDataGroups(): Promise<AppDataGroupDescriptor[]>;
1772
+ /**
1773
+ * 「引导副本只升不降」:本地 `.config/app-data-groups/{appId}/{id}.json` 若声明
1774
+ * **0 个后端**,而任何副本上同一路径还带着非空后端列表,就采用远端那份。
1775
+ *
1776
+ * 为什么必须做:本页「清除同步组配置」后本地库被重建,应用会先建一个只有隐式
1777
+ * 本地存储的空 default 组并写下 `backends: []`;配置组后端一接上,首轮双向同步
1778
+ * 以本地为准,这份空副本就会把远端登记的真实拓扑抹平(2026-11-24 线上实际发生)。
1779
+ *
1780
+ * 例外:本会话内用户显式清空后端(`updateAppDataGroupDescriptor(id, [])`)是
1781
+ * 真实意图,允许推送空副本,不参与自愈。
1782
+ */
1783
+ private healEmptyBootstrapDescriptors;
1762
1784
  removeAppDataGroup(id: string): Promise<void>;
1763
1785
  listAccountBackends(): Promise<BackendDescriptor[]>;
1764
1786
  private tryParse;
@@ -1976,7 +1998,9 @@ type IdentityConflictReason =
1976
1998
  /** 远端描述符读不到(网络 / 权限),无法判断归属。 */
1977
1999
  | 'unreadable'
1978
2000
  /** 认领写入失败(远端只读 / 无权限)。 */
1979
- | 'claim-failed';
2001
+ | 'claim-failed'
2002
+ /** 本组实例身份尚未确定(没有读到任何候选),本轮不判定、也不认领。 */
2003
+ | 'local-unset';
1980
2004
  /** 本组身份:kind + 显示用 id + 不可变实例身份。 */
1981
2005
  interface GroupIdentity {
1982
2006
  kind: SyncGroupKind;
package/dist/index.d.ts CHANGED
@@ -474,7 +474,9 @@ interface GroupDescriptor {
474
474
  * 两个能力位互不蕴含。
475
475
  */
476
476
  /** 归属冲突的细分原因。 */
477
- type BackendConflictReason = 'kind-mismatch' | 'id-mismatch' | 'instance-mismatch' | 'unreadable' | 'claim-failed';
477
+ type BackendConflictReason = 'kind-mismatch' | 'id-mismatch' | 'instance-mismatch' | 'unreadable' | 'claim-failed'
478
+ /** 本组实例身份尚未确定(没有读到任何候选),本轮不判定、也不认领。 */
479
+ | 'local-unset';
478
480
  /**
479
481
  * 一个后端与远端根归属冲突的完整信息。
480
482
  *
@@ -1284,6 +1286,14 @@ declare class ConfigRepo implements IConfigRepo {
1284
1286
  private backendConflicts;
1285
1287
  /** Last known backend descriptors, for the synchronous unified handle API. */
1286
1288
  private backendDescriptorCache;
1289
+ /**
1290
+ * 本会话内被用户**显式**清空后端的数据组 id。
1291
+ *
1292
+ * 只有这类组允许把「0 个后端」的引导副本推给远端;启动时读到的空副本
1293
+ * (历史脏数据、或「清空本页配置」后本地新建的空组)绝不覆盖远端既有拓扑,
1294
+ * 见 `healEmptyBootstrapDescriptors()`。
1295
+ */
1296
+ private explicitEmptyDescriptors;
1287
1297
  /**
1288
1298
  * Tracks the background initial sync started by createConfigRepo().
1289
1299
  * `flush()` and `dispose()` will await this if it hasn't completed yet,
@@ -1759,6 +1769,18 @@ declare class ConfigRepo implements IConfigRepo {
1759
1769
  */
1760
1770
  private syncBootstrapDescriptor;
1761
1771
  listAppDataGroups(): Promise<AppDataGroupDescriptor[]>;
1772
+ /**
1773
+ * 「引导副本只升不降」:本地 `.config/app-data-groups/{appId}/{id}.json` 若声明
1774
+ * **0 个后端**,而任何副本上同一路径还带着非空后端列表,就采用远端那份。
1775
+ *
1776
+ * 为什么必须做:本页「清除同步组配置」后本地库被重建,应用会先建一个只有隐式
1777
+ * 本地存储的空 default 组并写下 `backends: []`;配置组后端一接上,首轮双向同步
1778
+ * 以本地为准,这份空副本就会把远端登记的真实拓扑抹平(2026-11-24 线上实际发生)。
1779
+ *
1780
+ * 例外:本会话内用户显式清空后端(`updateAppDataGroupDescriptor(id, [])`)是
1781
+ * 真实意图,允许推送空副本,不参与自愈。
1782
+ */
1783
+ private healEmptyBootstrapDescriptors;
1762
1784
  removeAppDataGroup(id: string): Promise<void>;
1763
1785
  listAccountBackends(): Promise<BackendDescriptor[]>;
1764
1786
  private tryParse;
@@ -1976,7 +1998,9 @@ type IdentityConflictReason =
1976
1998
  /** 远端描述符读不到(网络 / 权限),无法判断归属。 */
1977
1999
  | 'unreadable'
1978
2000
  /** 认领写入失败(远端只读 / 无权限)。 */
1979
- | 'claim-failed';
2001
+ | 'claim-failed'
2002
+ /** 本组实例身份尚未确定(没有读到任何候选),本轮不判定、也不认领。 */
2003
+ | 'local-unset';
1980
2004
  /** 本组身份:kind + 显示用 id + 不可变实例身份。 */
1981
2005
  interface GroupIdentity {
1982
2006
  kind: SyncGroupKind;
package/dist/index.js CHANGED
@@ -2546,8 +2546,12 @@ async function bindRemoteGroupIdentityDetailed(remoteFS, identity, prefetched) {
2546
2546
  );
2547
2547
  return { state: "conflict", remote: snapshot, local: identity, conflict: "kind-mismatch" };
2548
2548
  }
2549
+ if (!isValidGroupInstanceId(identity.instanceId)) {
2550
+ return { state: "conflict", remote: snapshot, local: identity, conflict: "local-unset" };
2551
+ }
2552
+ const localInstanceId = identity.instanceId;
2549
2553
  if (isValidGroupInstanceId(current)) {
2550
- if (current === identity.instanceId) {
2554
+ if (current === localInstanceId) {
2551
2555
  return { state: "match", remote: snapshot, local: identity };
2552
2556
  }
2553
2557
  const reason = snapshot?.id && snapshot.id !== identity.id ? "id-mismatch" : "instance-mismatch";
@@ -2560,7 +2564,7 @@ async function bindRemoteGroupIdentityDetailed(remoteFS, identity, prefetched) {
2560
2564
  schemaVersion: 2,
2561
2565
  kind: remote?.kind ?? identity.kind,
2562
2566
  id: remote?.id ?? identity.id,
2563
- instanceId: identity.instanceId,
2567
+ instanceId: localInstanceId,
2564
2568
  ...remote?.name ?? identity.name ? { name: remote?.name ?? identity.name } : {},
2565
2569
  ...remote?.features ?? identity.features ? { features: remote?.features ?? identity.features } : {}
2566
2570
  };
@@ -2577,7 +2581,7 @@ async function bindRemoteGroupIdentityDetailed(remoteFS, identity, prefetched) {
2577
2581
  };
2578
2582
  }
2579
2583
  log4.log(
2580
- `[GroupIdentity] claimed remote root for ${identity.kind}/"${identity.id}" (instanceId=${identity.instanceId})`
2584
+ `[GroupIdentity] claimed remote root for ${identity.kind}/"${identity.id}" (instanceId=${localInstanceId})`
2581
2585
  );
2582
2586
  return { state: "claimed", remote: snapshot, local: identity };
2583
2587
  }
@@ -2901,6 +2905,8 @@ function describeConflict(result) {
2901
2905
  return `\u8FDC\u7AEF group.json \u8BFB\u4E0D\u5230\uFF08${result.error ?? "\u672A\u77E5\u9519\u8BEF"}\uFF09\uFF0C\u65E0\u6CD5\u5224\u65AD\u5F52\u5C5E\uFF0C\u5DF2\u8DF3\u8FC7\u8BE5\u540E\u7AEF`;
2902
2906
  case "claim-failed":
2903
2907
  return `\u8BA4\u9886\u8FDC\u7AEF\u6839\u5931\u8D25\uFF08${result.error ?? "\u672A\u77E5\u9519\u8BEF"}\uFF09\uFF0C\u5DF2\u8DF3\u8FC7\u8BE5\u540E\u7AEF`;
2908
+ case "local-unset":
2909
+ return "\u672C\u7EC4\u5B9E\u4F8B\u8EAB\u4EFD\u5C1A\u672A\u786E\u5B9A\uFF08\u5DF2\u6CE8\u518C\u7684\u8FDC\u7AEF\u6839\u90FD\u8BFB\u4E0D\u5230 group.json\uFF09\uFF0C\u672C\u8F6E\u65E2\u4E0D\u5224\u5B9A\u5F52\u5C5E\u4E5F\u4E0D\u5199\u5165\u4EFB\u4F55\u8FDC\u7AEF\uFF0C\u5DF2\u8DF3\u8FC7\u8BE5\u540E\u7AEF";
2904
2910
  default:
2905
2911
  return `\u8FDC\u7AEF\u6839\u5F52\u5C5E\u51B2\u7A81\uFF1A\u8FDC\u7AEF ${remote}\uFF0C\u672C\u7EC4 ${local}`;
2906
2912
  }
@@ -3134,6 +3140,14 @@ var ConfigRepo = class _ConfigRepo {
3134
3140
  backendConflicts = /* @__PURE__ */ new Map();
3135
3141
  /** Last known backend descriptors, for the synchronous unified handle API. */
3136
3142
  backendDescriptorCache = [];
3143
+ /**
3144
+ * 本会话内被用户**显式**清空后端的数据组 id。
3145
+ *
3146
+ * 只有这类组允许把「0 个后端」的引导副本推给远端;启动时读到的空副本
3147
+ * (历史脏数据、或「清空本页配置」后本地新建的空组)绝不覆盖远端既有拓扑,
3148
+ * 见 `healEmptyBootstrapDescriptors()`。
3149
+ */
3150
+ explicitEmptyDescriptors = /* @__PURE__ */ new Set();
3137
3151
  /**
3138
3152
  * Tracks the background initial sync started by createConfigRepo().
3139
3153
  * `flush()` and `dispose()` will await this if it hasn't completed yet,
@@ -3313,6 +3327,7 @@ var ConfigRepo = class _ConfigRepo {
3313
3327
  this.initialSyncPromise = null;
3314
3328
  }
3315
3329
  await this.processTombstones();
3330
+ await this.healEmptyBootstrapDescriptors();
3316
3331
  const resultsMap = await this.syncEngine.syncAll();
3317
3332
  this.invalidateTombstoneCache();
3318
3333
  await this.readAllBackendDescriptors();
@@ -3820,6 +3835,7 @@ var ConfigRepo = class _ConfigRepo {
3820
3835
  */
3821
3836
  async initialSyncAndDedup() {
3822
3837
  this.syncEngine.unwatchAll();
3838
+ await this.healEmptyBootstrapDescriptors();
3823
3839
  await this.syncEngine.syncAll();
3824
3840
  await this.readAllBackendDescriptors();
3825
3841
  await this.processTombstones();
@@ -4954,7 +4970,23 @@ var ConfigRepo = class _ConfigRepo {
4954
4970
  const collected = await this.collectConfigIdentityCandidates(kind, id);
4955
4971
  this.identityCandidates = collected.candidates;
4956
4972
  const distinct = collected.distinct;
4957
- this.instanceId = distinct[0] ?? newGroupInstanceId();
4973
+ if (!distinct.length && collected.backendCount > 0 && collected.readableCount === 0) {
4974
+ this.instanceId = void 0;
4975
+ this.configIdentityUnsettled = true;
4976
+ log5.warn(
4977
+ `[ConfigRepo] ${collected.backendCount} registered backend(s) but none readable \u2014 group instanceId NOT assigned (nothing written, no remote touched)`
4978
+ );
4979
+ existingFeatures = existingFeatures ?? CONFIG_GROUP_FEATURES;
4980
+ return {
4981
+ kind,
4982
+ id,
4983
+ instanceId: "",
4984
+ ...this.name ? { name: this.name } : {},
4985
+ features: existingFeatures
4986
+ };
4987
+ }
4988
+ const assigned = distinct[0] ?? newGroupInstanceId();
4989
+ this.instanceId = assigned;
4958
4990
  this.configIdentityUnsettled = distinct.length > 1;
4959
4991
  if (this.configIdentityUnsettled) {
4960
4992
  log5.warn(
@@ -4962,18 +4994,18 @@ var ConfigRepo = class _ConfigRepo {
4962
4994
  );
4963
4995
  }
4964
4996
  if (!distinct.length) {
4965
- log5.log(`[ConfigRepo] no identity anywhere (local/remote all empty) \u2014 generated ${this.instanceId}`);
4997
+ log5.log(`[ConfigRepo] no identity anywhere (local/remote all empty) \u2014 generated ${assigned}`);
4966
4998
  }
4967
4999
  try {
4968
5000
  await writeGroupDescriptor(this.cachedFS, {
4969
5001
  schemaVersion: 2,
4970
5002
  kind: existing?.kind ?? kind,
4971
5003
  id: existing?.id ?? id,
4972
- instanceId: this.instanceId,
5004
+ instanceId: assigned,
4973
5005
  ...existing?.name ?? this.name ? { name: existing?.name ?? this.name } : {},
4974
5006
  features: existing?.features ?? CONFIG_GROUP_FEATURES
4975
5007
  });
4976
- log5.log(`[ConfigRepo] group instanceId assigned: ${this.instanceId} (kind=${existing?.kind ?? kind})`);
5008
+ log5.log(`[ConfigRepo] group instanceId assigned: ${assigned} (kind=${existing?.kind ?? kind})`);
4977
5009
  } catch (err) {
4978
5010
  log5.warn("[ConfigRepo] failed to persist group instanceId:", err);
4979
5011
  }
@@ -4987,7 +5019,9 @@ var ConfigRepo = class _ConfigRepo {
4987
5019
  return {
4988
5020
  kind,
4989
5021
  id,
4990
- instanceId: this.instanceId,
5022
+ // 身份可能「尚未分配」(已注册的根都读不到时有意不定)——用空串占位,
5023
+ // 由 `isValidGroupInstanceId` 判定为无效,调用方据此跳过而不误比对。
5024
+ instanceId: this.instanceId ?? "",
4991
5025
  ...this.name ? { name: this.name } : {},
4992
5026
  ...existingFeatures ? { features: existingFeatures } : {}
4993
5027
  };
@@ -5006,15 +5040,31 @@ var ConfigRepo = class _ConfigRepo {
5006
5040
  if (local) {
5007
5041
  candidates.push({ source: "local", instanceId: local, rootCount: 1, kind, id });
5008
5042
  }
5009
- for (const desc of this.backendDescriptorCache ?? []) {
5043
+ let descriptors = [];
5044
+ try {
5045
+ descriptors = await this.readAllBackendDescriptors();
5046
+ } catch (err) {
5047
+ log5.warn("[ConfigRepo] collectConfigIdentityCandidates: failed to read .meta/backends:", err);
5048
+ }
5049
+ let readableCount = 0;
5050
+ for (const desc of descriptors) {
5010
5051
  let inst;
5011
5052
  try {
5012
- inst = await createBackend({ type: desc.type, options: desc.options });
5053
+ inst = await withTimeout(
5054
+ createBackend({ type: desc.type, options: desc.options }),
5055
+ REPLICA_SETUP_TIMEOUT_MS,
5056
+ `\u914D\u7F6E\u7EC4\u540E\u7AEF ${desc.id} \u521B\u5EFA\u5B9E\u4F8B`
5057
+ );
5013
5058
  } catch {
5014
5059
  continue;
5015
5060
  }
5016
5061
  try {
5017
- const remote = await readGroupDescriptorStrict(inst);
5062
+ const remote = await withTimeout(
5063
+ readGroupDescriptorStrict(inst),
5064
+ REPLICA_SETUP_TIMEOUT_MS,
5065
+ `\u914D\u7F6E\u7EC4\u540E\u7AEF ${desc.id} \u8BFB group.json`
5066
+ );
5067
+ readableCount += 1;
5018
5068
  if (!remote || remote.kind !== kind || remote.id !== id) continue;
5019
5069
  const value = pickInstanceId(remote);
5020
5070
  if (!value) continue;
@@ -5031,7 +5081,7 @@ var ConfigRepo = class _ConfigRepo {
5031
5081
  for (const c of candidates) {
5032
5082
  c.rootCount = candidates.filter((x) => x.instanceId === c.instanceId).length;
5033
5083
  }
5034
- return { candidates, distinct };
5084
+ return { candidates, distinct, backendCount: descriptors.length, readableCount };
5035
5085
  }
5036
5086
  /** 组实例身份(未初始化时先兜底生成并落盘)。 */
5037
5087
  async ensureGroupInstanceId() {
@@ -5156,7 +5206,7 @@ var ConfigRepo = class _ConfigRepo {
5156
5206
  reason: describeConflict(result),
5157
5207
  ...result.conflict ? { conflict: result.conflict } : {},
5158
5208
  remote: result.remote,
5159
- local: { kind: identity.kind, id: identity.id, instanceId: identity.instanceId },
5209
+ local: { kind: identity.kind, id: identity.id, instanceId: this.instanceId },
5160
5210
  canAdoptRemoteInstance: !!result.remote?.instanceId && result.remote.kind === identity.kind && result.remote.id === identity.id,
5161
5211
  canRewriteRemoteIdentity: result.remote ? result.remote.kind === identity.kind : false,
5162
5212
  ...result.unreadable ? { unreadable: true } : {},
@@ -5446,6 +5496,8 @@ var ConfigRepo = class _ConfigRepo {
5446
5496
  ...features ? { features } : {},
5447
5497
  backends
5448
5498
  };
5499
+ if (backends.length === 0) this.explicitEmptyDescriptors.add(id);
5500
+ else this.explicitEmptyDescriptors.delete(id);
5449
5501
  await this.writeAppDataGroupDescriptorFile(id, descriptor);
5450
5502
  this.scheduleDescriptorPush();
5451
5503
  }
@@ -5694,6 +5746,59 @@ var ConfigRepo = class _ConfigRepo {
5694
5746
  }
5695
5747
  return Array.from(byId.values());
5696
5748
  }
5749
+ /**
5750
+ * 「引导副本只升不降」:本地 `.config/app-data-groups/{appId}/{id}.json` 若声明
5751
+ * **0 个后端**,而任何副本上同一路径还带着非空后端列表,就采用远端那份。
5752
+ *
5753
+ * 为什么必须做:本页「清除同步组配置」后本地库被重建,应用会先建一个只有隐式
5754
+ * 本地存储的空 default 组并写下 `backends: []`;配置组后端一接上,首轮双向同步
5755
+ * 以本地为准,这份空副本就会把远端登记的真实拓扑抹平(2026-11-24 线上实际发生)。
5756
+ *
5757
+ * 例外:本会话内用户显式清空后端(`updateAppDataGroupDescriptor(id, [])`)是
5758
+ * 真实意图,允许推送空副本,不参与自愈。
5759
+ */
5760
+ async healEmptyBootstrapDescriptors() {
5761
+ if (this.replicaBackends.size === 0) return;
5762
+ let localDescriptors;
5763
+ try {
5764
+ localDescriptors = await this.listAppDataGroups();
5765
+ } catch {
5766
+ return;
5767
+ }
5768
+ for (const descriptor of localDescriptors) {
5769
+ if ((descriptor.backends?.length ?? 0) > 0) continue;
5770
+ if (this.explicitEmptyDescriptors.has(descriptor.id)) continue;
5771
+ const descPath = this.appDataGroupFilePath(descriptor.id);
5772
+ for (const [replicaId, replica] of this.replicaBackends) {
5773
+ let remote;
5774
+ try {
5775
+ const raw = await withTimeout(
5776
+ replica.instance.readFile(descPath),
5777
+ REPLICA_SETUP_TIMEOUT_MS,
5778
+ `${replicaId} \u8BFB\u53D6\u5F15\u5BFC\u526F\u672C`
5779
+ );
5780
+ remote = JSON.parse(new TextDecoder().decode(toUint8Array2(raw)));
5781
+ } catch {
5782
+ continue;
5783
+ }
5784
+ if (!remote || remote.id !== descriptor.id || remote.groupType !== "data-sync") continue;
5785
+ const remoteBackends = Array.isArray(remote.backends) ? remote.backends : [];
5786
+ if (remoteBackends.length === 0) continue;
5787
+ await this.writeAppDataGroupDescriptorFile(descriptor.id, {
5788
+ id: descriptor.id,
5789
+ groupType: "data-sync",
5790
+ ...remote.instanceId ? { instanceId: remote.instanceId } : {},
5791
+ ...remote.name ? { name: remote.name } : {},
5792
+ ...remote.features ? { features: remote.features } : {},
5793
+ backends: remoteBackends
5794
+ });
5795
+ log5.warn(
5796
+ `[ConfigRepo] ${descriptor.id}: \u672C\u5730\u5F15\u5BFC\u526F\u672C\u4E3A\u7A7A\uFF0C\u91C7\u7528\u8FDC\u7AEF\u62D3\u6251\uFF08${remoteBackends.length} \u4E2A\u540E\u7AEF\uFF09\uFF0C\u907F\u514D\u8986\u76D6\u8FDC\u7AEF\u8BBE\u7F6E`
5797
+ );
5798
+ break;
5799
+ }
5800
+ }
5801
+ }
5697
5802
  async removeAppDataGroup(id) {
5698
5803
  this.assertNotDisposed();
5699
5804
  const group = this.appDataGroups.get(id);
package/dist/index.mjs CHANGED
@@ -1781,8 +1781,12 @@ async function bindRemoteGroupIdentityDetailed(remoteFS, identity, prefetched) {
1781
1781
  );
1782
1782
  return { state: "conflict", remote: snapshot, local: identity, conflict: "kind-mismatch" };
1783
1783
  }
1784
+ if (!isValidGroupInstanceId(identity.instanceId)) {
1785
+ return { state: "conflict", remote: snapshot, local: identity, conflict: "local-unset" };
1786
+ }
1787
+ const localInstanceId = identity.instanceId;
1784
1788
  if (isValidGroupInstanceId(current)) {
1785
- if (current === identity.instanceId) {
1789
+ if (current === localInstanceId) {
1786
1790
  return { state: "match", remote: snapshot, local: identity };
1787
1791
  }
1788
1792
  const reason = snapshot?.id && snapshot.id !== identity.id ? "id-mismatch" : "instance-mismatch";
@@ -1795,7 +1799,7 @@ async function bindRemoteGroupIdentityDetailed(remoteFS, identity, prefetched) {
1795
1799
  schemaVersion: 2,
1796
1800
  kind: remote?.kind ?? identity.kind,
1797
1801
  id: remote?.id ?? identity.id,
1798
- instanceId: identity.instanceId,
1802
+ instanceId: localInstanceId,
1799
1803
  ...remote?.name ?? identity.name ? { name: remote?.name ?? identity.name } : {},
1800
1804
  ...remote?.features ?? identity.features ? { features: remote?.features ?? identity.features } : {}
1801
1805
  };
@@ -1812,7 +1816,7 @@ async function bindRemoteGroupIdentityDetailed(remoteFS, identity, prefetched) {
1812
1816
  };
1813
1817
  }
1814
1818
  log4.log(
1815
- `[GroupIdentity] claimed remote root for ${identity.kind}/"${identity.id}" (instanceId=${identity.instanceId})`
1819
+ `[GroupIdentity] claimed remote root for ${identity.kind}/"${identity.id}" (instanceId=${localInstanceId})`
1816
1820
  );
1817
1821
  return { state: "claimed", remote: snapshot, local: identity };
1818
1822
  }
@@ -2136,6 +2140,8 @@ function describeConflict(result) {
2136
2140
  return `\u8FDC\u7AEF group.json \u8BFB\u4E0D\u5230\uFF08${result.error ?? "\u672A\u77E5\u9519\u8BEF"}\uFF09\uFF0C\u65E0\u6CD5\u5224\u65AD\u5F52\u5C5E\uFF0C\u5DF2\u8DF3\u8FC7\u8BE5\u540E\u7AEF`;
2137
2141
  case "claim-failed":
2138
2142
  return `\u8BA4\u9886\u8FDC\u7AEF\u6839\u5931\u8D25\uFF08${result.error ?? "\u672A\u77E5\u9519\u8BEF"}\uFF09\uFF0C\u5DF2\u8DF3\u8FC7\u8BE5\u540E\u7AEF`;
2143
+ case "local-unset":
2144
+ return "\u672C\u7EC4\u5B9E\u4F8B\u8EAB\u4EFD\u5C1A\u672A\u786E\u5B9A\uFF08\u5DF2\u6CE8\u518C\u7684\u8FDC\u7AEF\u6839\u90FD\u8BFB\u4E0D\u5230 group.json\uFF09\uFF0C\u672C\u8F6E\u65E2\u4E0D\u5224\u5B9A\u5F52\u5C5E\u4E5F\u4E0D\u5199\u5165\u4EFB\u4F55\u8FDC\u7AEF\uFF0C\u5DF2\u8DF3\u8FC7\u8BE5\u540E\u7AEF";
2139
2145
  default:
2140
2146
  return `\u8FDC\u7AEF\u6839\u5F52\u5C5E\u51B2\u7A81\uFF1A\u8FDC\u7AEF ${remote}\uFF0C\u672C\u7EC4 ${local}`;
2141
2147
  }
@@ -2369,6 +2375,14 @@ var ConfigRepo = class _ConfigRepo {
2369
2375
  backendConflicts = /* @__PURE__ */ new Map();
2370
2376
  /** Last known backend descriptors, for the synchronous unified handle API. */
2371
2377
  backendDescriptorCache = [];
2378
+ /**
2379
+ * 本会话内被用户**显式**清空后端的数据组 id。
2380
+ *
2381
+ * 只有这类组允许把「0 个后端」的引导副本推给远端;启动时读到的空副本
2382
+ * (历史脏数据、或「清空本页配置」后本地新建的空组)绝不覆盖远端既有拓扑,
2383
+ * 见 `healEmptyBootstrapDescriptors()`。
2384
+ */
2385
+ explicitEmptyDescriptors = /* @__PURE__ */ new Set();
2372
2386
  /**
2373
2387
  * Tracks the background initial sync started by createConfigRepo().
2374
2388
  * `flush()` and `dispose()` will await this if it hasn't completed yet,
@@ -2548,6 +2562,7 @@ var ConfigRepo = class _ConfigRepo {
2548
2562
  this.initialSyncPromise = null;
2549
2563
  }
2550
2564
  await this.processTombstones();
2565
+ await this.healEmptyBootstrapDescriptors();
2551
2566
  const resultsMap = await this.syncEngine.syncAll();
2552
2567
  this.invalidateTombstoneCache();
2553
2568
  await this.readAllBackendDescriptors();
@@ -3055,6 +3070,7 @@ var ConfigRepo = class _ConfigRepo {
3055
3070
  */
3056
3071
  async initialSyncAndDedup() {
3057
3072
  this.syncEngine.unwatchAll();
3073
+ await this.healEmptyBootstrapDescriptors();
3058
3074
  await this.syncEngine.syncAll();
3059
3075
  await this.readAllBackendDescriptors();
3060
3076
  await this.processTombstones();
@@ -4189,7 +4205,23 @@ var ConfigRepo = class _ConfigRepo {
4189
4205
  const collected = await this.collectConfigIdentityCandidates(kind, id);
4190
4206
  this.identityCandidates = collected.candidates;
4191
4207
  const distinct = collected.distinct;
4192
- this.instanceId = distinct[0] ?? newGroupInstanceId();
4208
+ if (!distinct.length && collected.backendCount > 0 && collected.readableCount === 0) {
4209
+ this.instanceId = void 0;
4210
+ this.configIdentityUnsettled = true;
4211
+ log5.warn(
4212
+ `[ConfigRepo] ${collected.backendCount} registered backend(s) but none readable \u2014 group instanceId NOT assigned (nothing written, no remote touched)`
4213
+ );
4214
+ existingFeatures = existingFeatures ?? CONFIG_GROUP_FEATURES;
4215
+ return {
4216
+ kind,
4217
+ id,
4218
+ instanceId: "",
4219
+ ...this.name ? { name: this.name } : {},
4220
+ features: existingFeatures
4221
+ };
4222
+ }
4223
+ const assigned = distinct[0] ?? newGroupInstanceId();
4224
+ this.instanceId = assigned;
4193
4225
  this.configIdentityUnsettled = distinct.length > 1;
4194
4226
  if (this.configIdentityUnsettled) {
4195
4227
  log5.warn(
@@ -4197,18 +4229,18 @@ var ConfigRepo = class _ConfigRepo {
4197
4229
  );
4198
4230
  }
4199
4231
  if (!distinct.length) {
4200
- log5.log(`[ConfigRepo] no identity anywhere (local/remote all empty) \u2014 generated ${this.instanceId}`);
4232
+ log5.log(`[ConfigRepo] no identity anywhere (local/remote all empty) \u2014 generated ${assigned}`);
4201
4233
  }
4202
4234
  try {
4203
4235
  await writeGroupDescriptor(this.cachedFS, {
4204
4236
  schemaVersion: 2,
4205
4237
  kind: existing?.kind ?? kind,
4206
4238
  id: existing?.id ?? id,
4207
- instanceId: this.instanceId,
4239
+ instanceId: assigned,
4208
4240
  ...existing?.name ?? this.name ? { name: existing?.name ?? this.name } : {},
4209
4241
  features: existing?.features ?? CONFIG_GROUP_FEATURES
4210
4242
  });
4211
- log5.log(`[ConfigRepo] group instanceId assigned: ${this.instanceId} (kind=${existing?.kind ?? kind})`);
4243
+ log5.log(`[ConfigRepo] group instanceId assigned: ${assigned} (kind=${existing?.kind ?? kind})`);
4212
4244
  } catch (err) {
4213
4245
  log5.warn("[ConfigRepo] failed to persist group instanceId:", err);
4214
4246
  }
@@ -4222,7 +4254,9 @@ var ConfigRepo = class _ConfigRepo {
4222
4254
  return {
4223
4255
  kind,
4224
4256
  id,
4225
- instanceId: this.instanceId,
4257
+ // 身份可能「尚未分配」(已注册的根都读不到时有意不定)——用空串占位,
4258
+ // 由 `isValidGroupInstanceId` 判定为无效,调用方据此跳过而不误比对。
4259
+ instanceId: this.instanceId ?? "",
4226
4260
  ...this.name ? { name: this.name } : {},
4227
4261
  ...existingFeatures ? { features: existingFeatures } : {}
4228
4262
  };
@@ -4241,15 +4275,31 @@ var ConfigRepo = class _ConfigRepo {
4241
4275
  if (local) {
4242
4276
  candidates.push({ source: "local", instanceId: local, rootCount: 1, kind, id });
4243
4277
  }
4244
- for (const desc of this.backendDescriptorCache ?? []) {
4278
+ let descriptors = [];
4279
+ try {
4280
+ descriptors = await this.readAllBackendDescriptors();
4281
+ } catch (err) {
4282
+ log5.warn("[ConfigRepo] collectConfigIdentityCandidates: failed to read .meta/backends:", err);
4283
+ }
4284
+ let readableCount = 0;
4285
+ for (const desc of descriptors) {
4245
4286
  let inst;
4246
4287
  try {
4247
- inst = await createBackend({ type: desc.type, options: desc.options });
4288
+ inst = await withTimeout(
4289
+ createBackend({ type: desc.type, options: desc.options }),
4290
+ REPLICA_SETUP_TIMEOUT_MS,
4291
+ `\u914D\u7F6E\u7EC4\u540E\u7AEF ${desc.id} \u521B\u5EFA\u5B9E\u4F8B`
4292
+ );
4248
4293
  } catch {
4249
4294
  continue;
4250
4295
  }
4251
4296
  try {
4252
- const remote = await readGroupDescriptorStrict(inst);
4297
+ const remote = await withTimeout(
4298
+ readGroupDescriptorStrict(inst),
4299
+ REPLICA_SETUP_TIMEOUT_MS,
4300
+ `\u914D\u7F6E\u7EC4\u540E\u7AEF ${desc.id} \u8BFB group.json`
4301
+ );
4302
+ readableCount += 1;
4253
4303
  if (!remote || remote.kind !== kind || remote.id !== id) continue;
4254
4304
  const value = pickInstanceId(remote);
4255
4305
  if (!value) continue;
@@ -4266,7 +4316,7 @@ var ConfigRepo = class _ConfigRepo {
4266
4316
  for (const c of candidates) {
4267
4317
  c.rootCount = candidates.filter((x) => x.instanceId === c.instanceId).length;
4268
4318
  }
4269
- return { candidates, distinct };
4319
+ return { candidates, distinct, backendCount: descriptors.length, readableCount };
4270
4320
  }
4271
4321
  /** 组实例身份(未初始化时先兜底生成并落盘)。 */
4272
4322
  async ensureGroupInstanceId() {
@@ -4391,7 +4441,7 @@ var ConfigRepo = class _ConfigRepo {
4391
4441
  reason: describeConflict(result),
4392
4442
  ...result.conflict ? { conflict: result.conflict } : {},
4393
4443
  remote: result.remote,
4394
- local: { kind: identity.kind, id: identity.id, instanceId: identity.instanceId },
4444
+ local: { kind: identity.kind, id: identity.id, instanceId: this.instanceId },
4395
4445
  canAdoptRemoteInstance: !!result.remote?.instanceId && result.remote.kind === identity.kind && result.remote.id === identity.id,
4396
4446
  canRewriteRemoteIdentity: result.remote ? result.remote.kind === identity.kind : false,
4397
4447
  ...result.unreadable ? { unreadable: true } : {},
@@ -4681,6 +4731,8 @@ var ConfigRepo = class _ConfigRepo {
4681
4731
  ...features ? { features } : {},
4682
4732
  backends
4683
4733
  };
4734
+ if (backends.length === 0) this.explicitEmptyDescriptors.add(id);
4735
+ else this.explicitEmptyDescriptors.delete(id);
4684
4736
  await this.writeAppDataGroupDescriptorFile(id, descriptor);
4685
4737
  this.scheduleDescriptorPush();
4686
4738
  }
@@ -4929,6 +4981,59 @@ var ConfigRepo = class _ConfigRepo {
4929
4981
  }
4930
4982
  return Array.from(byId.values());
4931
4983
  }
4984
+ /**
4985
+ * 「引导副本只升不降」:本地 `.config/app-data-groups/{appId}/{id}.json` 若声明
4986
+ * **0 个后端**,而任何副本上同一路径还带着非空后端列表,就采用远端那份。
4987
+ *
4988
+ * 为什么必须做:本页「清除同步组配置」后本地库被重建,应用会先建一个只有隐式
4989
+ * 本地存储的空 default 组并写下 `backends: []`;配置组后端一接上,首轮双向同步
4990
+ * 以本地为准,这份空副本就会把远端登记的真实拓扑抹平(2026-11-24 线上实际发生)。
4991
+ *
4992
+ * 例外:本会话内用户显式清空后端(`updateAppDataGroupDescriptor(id, [])`)是
4993
+ * 真实意图,允许推送空副本,不参与自愈。
4994
+ */
4995
+ async healEmptyBootstrapDescriptors() {
4996
+ if (this.replicaBackends.size === 0) return;
4997
+ let localDescriptors;
4998
+ try {
4999
+ localDescriptors = await this.listAppDataGroups();
5000
+ } catch {
5001
+ return;
5002
+ }
5003
+ for (const descriptor of localDescriptors) {
5004
+ if ((descriptor.backends?.length ?? 0) > 0) continue;
5005
+ if (this.explicitEmptyDescriptors.has(descriptor.id)) continue;
5006
+ const descPath = this.appDataGroupFilePath(descriptor.id);
5007
+ for (const [replicaId, replica] of this.replicaBackends) {
5008
+ let remote;
5009
+ try {
5010
+ const raw = await withTimeout(
5011
+ replica.instance.readFile(descPath),
5012
+ REPLICA_SETUP_TIMEOUT_MS,
5013
+ `${replicaId} \u8BFB\u53D6\u5F15\u5BFC\u526F\u672C`
5014
+ );
5015
+ remote = JSON.parse(new TextDecoder().decode(toUint8Array2(raw)));
5016
+ } catch {
5017
+ continue;
5018
+ }
5019
+ if (!remote || remote.id !== descriptor.id || remote.groupType !== "data-sync") continue;
5020
+ const remoteBackends = Array.isArray(remote.backends) ? remote.backends : [];
5021
+ if (remoteBackends.length === 0) continue;
5022
+ await this.writeAppDataGroupDescriptorFile(descriptor.id, {
5023
+ id: descriptor.id,
5024
+ groupType: "data-sync",
5025
+ ...remote.instanceId ? { instanceId: remote.instanceId } : {},
5026
+ ...remote.name ? { name: remote.name } : {},
5027
+ ...remote.features ? { features: remote.features } : {},
5028
+ backends: remoteBackends
5029
+ });
5030
+ log5.warn(
5031
+ `[ConfigRepo] ${descriptor.id}: \u672C\u5730\u5F15\u5BFC\u526F\u672C\u4E3A\u7A7A\uFF0C\u91C7\u7528\u8FDC\u7AEF\u62D3\u6251\uFF08${remoteBackends.length} \u4E2A\u540E\u7AEF\uFF09\uFF0C\u907F\u514D\u8986\u76D6\u8FDC\u7AEF\u8BBE\u7F6E`
5032
+ );
5033
+ break;
5034
+ }
5035
+ }
5036
+ }
4932
5037
  async removeAppDataGroup(id) {
4933
5038
  this.assertNotDisposed();
4934
5039
  const group = this.appDataGroups.get(id);
@@ -28503,8 +28503,12 @@ var ZenFSConfig = (() => {
28503
28503
  );
28504
28504
  return { state: "conflict", remote: snapshot, local: identity, conflict: "kind-mismatch" };
28505
28505
  }
28506
+ if (!isValidGroupInstanceId(identity.instanceId)) {
28507
+ return { state: "conflict", remote: snapshot, local: identity, conflict: "local-unset" };
28508
+ }
28509
+ const localInstanceId = identity.instanceId;
28506
28510
  if (isValidGroupInstanceId(current)) {
28507
- if (current === identity.instanceId) {
28511
+ if (current === localInstanceId) {
28508
28512
  return { state: "match", remote: snapshot, local: identity };
28509
28513
  }
28510
28514
  const reason = snapshot?.id && snapshot.id !== identity.id ? "id-mismatch" : "instance-mismatch";
@@ -28517,7 +28521,7 @@ var ZenFSConfig = (() => {
28517
28521
  schemaVersion: 2,
28518
28522
  kind: remote?.kind ?? identity.kind,
28519
28523
  id: remote?.id ?? identity.id,
28520
- instanceId: identity.instanceId,
28524
+ instanceId: localInstanceId,
28521
28525
  ...remote?.name ?? identity.name ? { name: remote?.name ?? identity.name } : {},
28522
28526
  ...remote?.features ?? identity.features ? { features: remote?.features ?? identity.features } : {}
28523
28527
  };
@@ -28534,7 +28538,7 @@ var ZenFSConfig = (() => {
28534
28538
  };
28535
28539
  }
28536
28540
  log8.log(
28537
- `[GroupIdentity] claimed remote root for ${identity.kind}/"${identity.id}" (instanceId=${identity.instanceId})`
28541
+ `[GroupIdentity] claimed remote root for ${identity.kind}/"${identity.id}" (instanceId=${localInstanceId})`
28538
28542
  );
28539
28543
  return { state: "claimed", remote: snapshot, local: identity };
28540
28544
  }
@@ -28858,6 +28862,8 @@ var ZenFSConfig = (() => {
28858
28862
  return `\u8FDC\u7AEF group.json \u8BFB\u4E0D\u5230\uFF08${result.error ?? "\u672A\u77E5\u9519\u8BEF"}\uFF09\uFF0C\u65E0\u6CD5\u5224\u65AD\u5F52\u5C5E\uFF0C\u5DF2\u8DF3\u8FC7\u8BE5\u540E\u7AEF`;
28859
28863
  case "claim-failed":
28860
28864
  return `\u8BA4\u9886\u8FDC\u7AEF\u6839\u5931\u8D25\uFF08${result.error ?? "\u672A\u77E5\u9519\u8BEF"}\uFF09\uFF0C\u5DF2\u8DF3\u8FC7\u8BE5\u540E\u7AEF`;
28865
+ case "local-unset":
28866
+ return "\u672C\u7EC4\u5B9E\u4F8B\u8EAB\u4EFD\u5C1A\u672A\u786E\u5B9A\uFF08\u5DF2\u6CE8\u518C\u7684\u8FDC\u7AEF\u6839\u90FD\u8BFB\u4E0D\u5230 group.json\uFF09\uFF0C\u672C\u8F6E\u65E2\u4E0D\u5224\u5B9A\u5F52\u5C5E\u4E5F\u4E0D\u5199\u5165\u4EFB\u4F55\u8FDC\u7AEF\uFF0C\u5DF2\u8DF3\u8FC7\u8BE5\u540E\u7AEF";
28861
28867
  default:
28862
28868
  return `\u8FDC\u7AEF\u6839\u5F52\u5C5E\u51B2\u7A81\uFF1A\u8FDC\u7AEF ${remote}\uFF0C\u672C\u7EC4 ${local}`;
28863
28869
  }
@@ -29074,6 +29080,14 @@ var ZenFSConfig = (() => {
29074
29080
  __publicField(this, "backendConflicts", /* @__PURE__ */ new Map());
29075
29081
  /** Last known backend descriptors, for the synchronous unified handle API. */
29076
29082
  __publicField(this, "backendDescriptorCache", []);
29083
+ /**
29084
+ * 本会话内被用户**显式**清空后端的数据组 id。
29085
+ *
29086
+ * 只有这类组允许把「0 个后端」的引导副本推给远端;启动时读到的空副本
29087
+ * (历史脏数据、或「清空本页配置」后本地新建的空组)绝不覆盖远端既有拓扑,
29088
+ * 见 `healEmptyBootstrapDescriptors()`。
29089
+ */
29090
+ __publicField(this, "explicitEmptyDescriptors", /* @__PURE__ */ new Set());
29077
29091
  /**
29078
29092
  * Tracks the background initial sync started by createConfigRepo().
29079
29093
  * `flush()` and `dispose()` will await this if it hasn't completed yet,
@@ -29262,6 +29276,7 @@ var ZenFSConfig = (() => {
29262
29276
  this.initialSyncPromise = null;
29263
29277
  }
29264
29278
  await this.processTombstones();
29279
+ await this.healEmptyBootstrapDescriptors();
29265
29280
  const resultsMap = await this.syncEngine.syncAll();
29266
29281
  this.invalidateTombstoneCache();
29267
29282
  await this.readAllBackendDescriptors();
@@ -29763,6 +29778,7 @@ var ZenFSConfig = (() => {
29763
29778
  */
29764
29779
  async initialSyncAndDedup() {
29765
29780
  this.syncEngine.unwatchAll();
29781
+ await this.healEmptyBootstrapDescriptors();
29766
29782
  await this.syncEngine.syncAll();
29767
29783
  await this.readAllBackendDescriptors();
29768
29784
  await this.processTombstones();
@@ -30893,7 +30909,23 @@ var ZenFSConfig = (() => {
30893
30909
  const collected = await this.collectConfigIdentityCandidates(kind, id);
30894
30910
  this.identityCandidates = collected.candidates;
30895
30911
  const distinct = collected.distinct;
30896
- this.instanceId = distinct[0] ?? newGroupInstanceId();
30912
+ if (!distinct.length && collected.backendCount > 0 && collected.readableCount === 0) {
30913
+ this.instanceId = void 0;
30914
+ this.configIdentityUnsettled = true;
30915
+ log9.warn(
30916
+ `[ConfigRepo] ${collected.backendCount} registered backend(s) but none readable \u2014 group instanceId NOT assigned (nothing written, no remote touched)`
30917
+ );
30918
+ existingFeatures = existingFeatures ?? CONFIG_GROUP_FEATURES;
30919
+ return {
30920
+ kind,
30921
+ id,
30922
+ instanceId: "",
30923
+ ...this.name ? { name: this.name } : {},
30924
+ features: existingFeatures
30925
+ };
30926
+ }
30927
+ const assigned = distinct[0] ?? newGroupInstanceId();
30928
+ this.instanceId = assigned;
30897
30929
  this.configIdentityUnsettled = distinct.length > 1;
30898
30930
  if (this.configIdentityUnsettled) {
30899
30931
  log9.warn(
@@ -30901,18 +30933,18 @@ var ZenFSConfig = (() => {
30901
30933
  );
30902
30934
  }
30903
30935
  if (!distinct.length) {
30904
- log9.log(`[ConfigRepo] no identity anywhere (local/remote all empty) \u2014 generated ${this.instanceId}`);
30936
+ log9.log(`[ConfigRepo] no identity anywhere (local/remote all empty) \u2014 generated ${assigned}`);
30905
30937
  }
30906
30938
  try {
30907
30939
  await writeGroupDescriptor(this.cachedFS, {
30908
30940
  schemaVersion: 2,
30909
30941
  kind: existing?.kind ?? kind,
30910
30942
  id: existing?.id ?? id,
30911
- instanceId: this.instanceId,
30943
+ instanceId: assigned,
30912
30944
  ...existing?.name ?? this.name ? { name: existing?.name ?? this.name } : {},
30913
30945
  features: existing?.features ?? CONFIG_GROUP_FEATURES
30914
30946
  });
30915
- log9.log(`[ConfigRepo] group instanceId assigned: ${this.instanceId} (kind=${existing?.kind ?? kind})`);
30947
+ log9.log(`[ConfigRepo] group instanceId assigned: ${assigned} (kind=${existing?.kind ?? kind})`);
30916
30948
  } catch (err2) {
30917
30949
  log9.warn("[ConfigRepo] failed to persist group instanceId:", err2);
30918
30950
  }
@@ -30926,7 +30958,9 @@ var ZenFSConfig = (() => {
30926
30958
  return {
30927
30959
  kind,
30928
30960
  id,
30929
- instanceId: this.instanceId,
30961
+ // 身份可能「尚未分配」(已注册的根都读不到时有意不定)——用空串占位,
30962
+ // 由 `isValidGroupInstanceId` 判定为无效,调用方据此跳过而不误比对。
30963
+ instanceId: this.instanceId ?? "",
30930
30964
  ...this.name ? { name: this.name } : {},
30931
30965
  ...existingFeatures ? { features: existingFeatures } : {}
30932
30966
  };
@@ -30945,15 +30979,31 @@ var ZenFSConfig = (() => {
30945
30979
  if (local) {
30946
30980
  candidates.push({ source: "local", instanceId: local, rootCount: 1, kind, id });
30947
30981
  }
30948
- for (const desc of this.backendDescriptorCache ?? []) {
30982
+ let descriptors = [];
30983
+ try {
30984
+ descriptors = await this.readAllBackendDescriptors();
30985
+ } catch (err2) {
30986
+ log9.warn("[ConfigRepo] collectConfigIdentityCandidates: failed to read .meta/backends:", err2);
30987
+ }
30988
+ let readableCount = 0;
30989
+ for (const desc of descriptors) {
30949
30990
  let inst;
30950
30991
  try {
30951
- inst = await createBackend({ type: desc.type, options: desc.options });
30992
+ inst = await withTimeout(
30993
+ createBackend({ type: desc.type, options: desc.options }),
30994
+ REPLICA_SETUP_TIMEOUT_MS,
30995
+ `\u914D\u7F6E\u7EC4\u540E\u7AEF ${desc.id} \u521B\u5EFA\u5B9E\u4F8B`
30996
+ );
30952
30997
  } catch {
30953
30998
  continue;
30954
30999
  }
30955
31000
  try {
30956
- const remote = await readGroupDescriptorStrict(inst);
31001
+ const remote = await withTimeout(
31002
+ readGroupDescriptorStrict(inst),
31003
+ REPLICA_SETUP_TIMEOUT_MS,
31004
+ `\u914D\u7F6E\u7EC4\u540E\u7AEF ${desc.id} \u8BFB group.json`
31005
+ );
31006
+ readableCount += 1;
30957
31007
  if (!remote || remote.kind !== kind || remote.id !== id) continue;
30958
31008
  const value = pickInstanceId(remote);
30959
31009
  if (!value) continue;
@@ -30970,7 +31020,7 @@ var ZenFSConfig = (() => {
30970
31020
  for (const c of candidates) {
30971
31021
  c.rootCount = candidates.filter((x) => x.instanceId === c.instanceId).length;
30972
31022
  }
30973
- return { candidates, distinct };
31023
+ return { candidates, distinct, backendCount: descriptors.length, readableCount };
30974
31024
  }
30975
31025
  /** 组实例身份(未初始化时先兜底生成并落盘)。 */
30976
31026
  async ensureGroupInstanceId() {
@@ -31095,7 +31145,7 @@ var ZenFSConfig = (() => {
31095
31145
  reason: describeConflict(result),
31096
31146
  ...result.conflict ? { conflict: result.conflict } : {},
31097
31147
  remote: result.remote,
31098
- local: { kind: identity.kind, id: identity.id, instanceId: identity.instanceId },
31148
+ local: { kind: identity.kind, id: identity.id, instanceId: this.instanceId },
31099
31149
  canAdoptRemoteInstance: !!result.remote?.instanceId && result.remote.kind === identity.kind && result.remote.id === identity.id,
31100
31150
  canRewriteRemoteIdentity: result.remote ? result.remote.kind === identity.kind : false,
31101
31151
  ...result.unreadable ? { unreadable: true } : {},
@@ -31385,6 +31435,8 @@ var ZenFSConfig = (() => {
31385
31435
  ...features ? { features } : {},
31386
31436
  backends
31387
31437
  };
31438
+ if (backends.length === 0) this.explicitEmptyDescriptors.add(id);
31439
+ else this.explicitEmptyDescriptors.delete(id);
31388
31440
  await this.writeAppDataGroupDescriptorFile(id, descriptor);
31389
31441
  this.scheduleDescriptorPush();
31390
31442
  }
@@ -31633,6 +31685,59 @@ var ZenFSConfig = (() => {
31633
31685
  }
31634
31686
  return Array.from(byId.values());
31635
31687
  }
31688
+ /**
31689
+ * 「引导副本只升不降」:本地 `.config/app-data-groups/{appId}/{id}.json` 若声明
31690
+ * **0 个后端**,而任何副本上同一路径还带着非空后端列表,就采用远端那份。
31691
+ *
31692
+ * 为什么必须做:本页「清除同步组配置」后本地库被重建,应用会先建一个只有隐式
31693
+ * 本地存储的空 default 组并写下 `backends: []`;配置组后端一接上,首轮双向同步
31694
+ * 以本地为准,这份空副本就会把远端登记的真实拓扑抹平(2026-11-24 线上实际发生)。
31695
+ *
31696
+ * 例外:本会话内用户显式清空后端(`updateAppDataGroupDescriptor(id, [])`)是
31697
+ * 真实意图,允许推送空副本,不参与自愈。
31698
+ */
31699
+ async healEmptyBootstrapDescriptors() {
31700
+ if (this.replicaBackends.size === 0) return;
31701
+ let localDescriptors;
31702
+ try {
31703
+ localDescriptors = await this.listAppDataGroups();
31704
+ } catch {
31705
+ return;
31706
+ }
31707
+ for (const descriptor of localDescriptors) {
31708
+ if ((descriptor.backends?.length ?? 0) > 0) continue;
31709
+ if (this.explicitEmptyDescriptors.has(descriptor.id)) continue;
31710
+ const descPath = this.appDataGroupFilePath(descriptor.id);
31711
+ for (const [replicaId, replica] of this.replicaBackends) {
31712
+ let remote;
31713
+ try {
31714
+ const raw = await withTimeout(
31715
+ replica.instance.readFile(descPath),
31716
+ REPLICA_SETUP_TIMEOUT_MS,
31717
+ `${replicaId} \u8BFB\u53D6\u5F15\u5BFC\u526F\u672C`
31718
+ );
31719
+ remote = JSON.parse(new TextDecoder().decode(toUint8Array2(raw)));
31720
+ } catch {
31721
+ continue;
31722
+ }
31723
+ if (!remote || remote.id !== descriptor.id || remote.groupType !== "data-sync") continue;
31724
+ const remoteBackends = Array.isArray(remote.backends) ? remote.backends : [];
31725
+ if (remoteBackends.length === 0) continue;
31726
+ await this.writeAppDataGroupDescriptorFile(descriptor.id, {
31727
+ id: descriptor.id,
31728
+ groupType: "data-sync",
31729
+ ...remote.instanceId ? { instanceId: remote.instanceId } : {},
31730
+ ...remote.name ? { name: remote.name } : {},
31731
+ ...remote.features ? { features: remote.features } : {},
31732
+ backends: remoteBackends
31733
+ });
31734
+ log9.warn(
31735
+ `[ConfigRepo] ${descriptor.id}: \u672C\u5730\u5F15\u5BFC\u526F\u672C\u4E3A\u7A7A\uFF0C\u91C7\u7528\u8FDC\u7AEF\u62D3\u6251\uFF08${remoteBackends.length} \u4E2A\u540E\u7AEF\uFF09\uFF0C\u907F\u514D\u8986\u76D6\u8FDC\u7AEF\u8BBE\u7F6E`
31736
+ );
31737
+ break;
31738
+ }
31739
+ }
31740
+ }
31636
31741
  async removeAppDataGroup(id) {
31637
31742
  this.assertNotDisposed();
31638
31743
  const group = this.appDataGroups.get(id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zen-fs-config",
3
- "version": "0.9.1",
3
+ "version": "0.9.3",
4
4
  "description": "Distributed config management library built on ZenFS, zen-fs-cache, and zen-fs-sync",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",