unforgit 0.11.2 → 0.11.4

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.
@@ -2922,26 +2922,54 @@ var LocalStore = class {
2922
2922
  return stats;
2923
2923
  }
2924
2924
  deprecate(id, reason) {
2925
- const now = (/* @__PURE__ */ new Date()).toISOString();
2926
- const result = this.db.prepare(
2927
- "UPDATE memories SET status = 'deprecated', updated_at = ? WHERE id = ?"
2928
- ).run(now, id);
2929
- if (reason && result.changes > 0) {
2925
+ const transaction = this.db.transaction(() => {
2930
2926
  const mem = this.getById(id);
2931
- if (mem) {
2932
- const refs = mem.sourceRefs ?? {};
2933
- refs.deprecation_reason = reason;
2934
- this.db.prepare("UPDATE memories SET source_refs = ? WHERE id = ?").run(JSON.stringify(refs), id);
2927
+ if (!mem) return false;
2928
+ const now = (/* @__PURE__ */ new Date()).toISOString();
2929
+ const refs = { ...mem.sourceRefs ?? {} };
2930
+ if (reason) refs.deprecation_reason = reason;
2931
+ const result = this.db.prepare(`
2932
+ UPDATE memories
2933
+ SET status = 'deprecated', source_refs = ?, version = version + 1, updated_at = ?
2934
+ WHERE id = ?
2935
+ `).run(Object.keys(refs).length > 0 ? JSON.stringify(refs) : null, now, id);
2936
+ if (result.changes > 0) {
2937
+ this.db.prepare(`
2938
+ UPDATE sync_state
2939
+ SET local_version = (SELECT version FROM memories WHERE id = ?),
2940
+ sync_status = CASE
2941
+ WHEN remote_version IS NULL AND last_pushed_at IS NULL AND last_pulled_at IS NULL
2942
+ THEN 'synced'
2943
+ ELSE 'pending_push'
2944
+ END
2945
+ WHERE memory_id = ?
2946
+ `).run(id, id);
2935
2947
  }
2936
- }
2937
- return result.changes > 0;
2948
+ return result.changes > 0;
2949
+ });
2950
+ return transaction.immediate();
2938
2951
  }
2939
2952
  supersede(oldId, newId) {
2940
- const now = (/* @__PURE__ */ new Date()).toISOString();
2941
- const result = this.db.prepare(
2942
- "UPDATE memories SET status = 'superseded', supersedes_id = ?, updated_at = ? WHERE id = ?"
2943
- ).run(newId, now, oldId);
2944
- return result.changes > 0;
2953
+ const transaction = this.db.transaction(() => {
2954
+ const memory = this.getById(oldId);
2955
+ if (!memory) return false;
2956
+ const now = (/* @__PURE__ */ new Date()).toISOString();
2957
+ const result = this.db.prepare(`
2958
+ UPDATE memories
2959
+ SET status = 'superseded', supersedes_id = ?, version = version + 1, updated_at = ?
2960
+ WHERE id = ?
2961
+ `).run(newId, now, oldId);
2962
+ if (result.changes > 0) {
2963
+ this.db.prepare(`
2964
+ UPDATE sync_state
2965
+ SET local_version = (SELECT version FROM memories WHERE id = ?),
2966
+ sync_status = 'pending_push'
2967
+ WHERE memory_id = ?
2968
+ `).run(oldId, oldId);
2969
+ }
2970
+ return result.changes > 0;
2971
+ });
2972
+ return transaction.immediate();
2945
2973
  }
2946
2974
  updateVisibility(id, visibility) {
2947
2975
  const now = (/* @__PURE__ */ new Date()).toISOString();
@@ -3501,13 +3529,38 @@ var LocalStore = class {
3501
3529
  state.syncStatus
3502
3530
  );
3503
3531
  }
3504
- markAsPushed(memoryId, remoteVersion) {
3505
- const now = (/* @__PURE__ */ new Date()).toISOString();
3506
- this.db.prepare(`
3507
- UPDATE sync_state
3508
- SET sync_status = 'synced', remote_version = ?, last_pushed_at = ?
3509
- WHERE memory_id = ?
3510
- `).run(remoteVersion, now, memoryId);
3532
+ markAsPushed(memoryId, remoteVersion, expectedVersion) {
3533
+ const transaction = this.db.transaction(() => {
3534
+ const now = (/* @__PURE__ */ new Date()).toISOString();
3535
+ if (expectedVersion === void 0) {
3536
+ const result2 = this.db.prepare(`
3537
+ UPDATE sync_state
3538
+ SET sync_status = 'synced', remote_version = ?, last_pushed_at = ?
3539
+ WHERE memory_id = ?
3540
+ `).run(remoteVersion, now, memoryId);
3541
+ return result2.changes > 0;
3542
+ }
3543
+ const result = this.db.prepare(`
3544
+ UPDATE sync_state
3545
+ SET remote_version = MAX(COALESCE(remote_version, 0), ?),
3546
+ last_pushed_at = ?,
3547
+ sync_status = CASE
3548
+ WHEN local_version = ?
3549
+ AND sync_status = 'pending_push'
3550
+ AND EXISTS (
3551
+ SELECT 1 FROM memories WHERE id = ? AND version = ?
3552
+ )
3553
+ THEN 'synced'
3554
+ WHEN sync_status IN ('conflict', 'pending_pull') THEN sync_status
3555
+ ELSE 'pending_push'
3556
+ END
3557
+ WHERE memory_id = ?
3558
+ `).run(remoteVersion, now, expectedVersion, memoryId, expectedVersion, memoryId);
3559
+ if (result.changes === 0) return false;
3560
+ const state = this.getSyncState(memoryId);
3561
+ return state?.syncStatus === "synced" && state.localVersion === expectedVersion;
3562
+ });
3563
+ return transaction.immediate();
3511
3564
  }
3512
3565
  markAsPulled(memoryId, localVersion) {
3513
3566
  const now = (/* @__PURE__ */ new Date()).toISOString();
@@ -3592,6 +3645,25 @@ var LocalStore = class {
3592
3645
  newId: row.supersedes_id
3593
3646
  }));
3594
3647
  }
3648
+ getDeprecatedMemoriesToSync(orgId, repoId) {
3649
+ const rows = this.db.prepare(`
3650
+ SELECT m.*
3651
+ FROM memories m
3652
+ LEFT JOIN sync_state s ON s.memory_id = m.id
3653
+ WHERE m.org_id = ?
3654
+ AND m.repo_id = ?
3655
+ AND m.status = 'deprecated'
3656
+ AND (s.remote_version IS NOT NULL OR s.last_pushed_at IS NOT NULL OR s.last_pulled_at IS NOT NULL)
3657
+ AND (
3658
+ s.sync_status = 'pending_push'
3659
+ OR (
3660
+ s.sync_status = 'synced'
3661
+ AND MAX(COALESCE(s.last_pushed_at, ''), COALESCE(s.last_pulled_at, '')) < m.updated_at
3662
+ )
3663
+ )
3664
+ `).all(orgId, repoId);
3665
+ return rows.map(rowToMemory);
3666
+ }
3595
3667
  getLinksToSync(orgId, repoId) {
3596
3668
  const rows = this.db.prepare(`
3597
3669
  SELECT l.*, ms.id as source_mem_id, mt.id as target_mem_id
@@ -3612,9 +3684,21 @@ var LocalStore = class {
3612
3684
  const now = (/* @__PURE__ */ new Date()).toISOString();
3613
3685
  this.db.prepare(`INSERT OR IGNORE INTO synced_links (link_id, synced_at) VALUES (?, ?)`).run(linkId, now);
3614
3686
  }
3615
- markStatusSynced(memoryId) {
3687
+ markStatusSynced(memoryId, expectedVersion) {
3616
3688
  const now = (/* @__PURE__ */ new Date()).toISOString();
3617
- this.db.prepare(`UPDATE sync_state SET last_pushed_at = ? WHERE memory_id = ?`).run(now, memoryId);
3689
+ const result = this.db.prepare(`
3690
+ UPDATE sync_state
3691
+ SET last_pushed_at = ?,
3692
+ local_version = ?,
3693
+ sync_status = 'synced'
3694
+ WHERE memory_id = ?
3695
+ AND local_version = ?
3696
+ AND sync_status IN ('pending_push', 'synced')
3697
+ AND EXISTS (
3698
+ SELECT 1 FROM memories WHERE id = ? AND version = ?
3699
+ )
3700
+ `).run(now, expectedVersion, memoryId, expectedVersion, memoryId, expectedVersion);
3701
+ return result.changes > 0;
3618
3702
  }
3619
3703
  unconsolidate(consolidationId) {
3620
3704
  const memory = this.getById(consolidationId);
@@ -4152,4 +4236,4 @@ export {
4152
4236
  RemoteClient,
4153
4237
  LocalStore
4154
4238
  };
4155
- //# sourceMappingURL=chunk-AI6MKGCF.js.map
4239
+ //# sourceMappingURL=chunk-IYCICAXZ.js.map