@nicia-ai/typegraph 0.31.0 → 0.33.0

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.
Files changed (188) hide show
  1. package/README.md +31 -0
  2. package/dist/backend/postgres/index.cjs +31 -18
  3. package/dist/backend/postgres/index.d.cts +6 -6
  4. package/dist/backend/postgres/index.d.ts +6 -6
  5. package/dist/backend/postgres/index.js +9 -8
  6. package/dist/backend/postgres/pglite.cjs +13 -12
  7. package/dist/backend/postgres/pglite.cjs.map +1 -1
  8. package/dist/backend/postgres/pglite.d.cts +3 -3
  9. package/dist/backend/postgres/pglite.d.ts +3 -3
  10. package/dist/backend/postgres/pglite.js +9 -8
  11. package/dist/backend/postgres/pglite.js.map +1 -1
  12. package/dist/backend/sqlite/index.cjs +29 -16
  13. package/dist/backend/sqlite/index.d.cts +7 -7
  14. package/dist/backend/sqlite/index.d.ts +7 -7
  15. package/dist/backend/sqlite/index.js +8 -7
  16. package/dist/backend/sqlite/libsql.cjs +13 -12
  17. package/dist/backend/sqlite/libsql.cjs.map +1 -1
  18. package/dist/backend/sqlite/libsql.d.cts +3 -3
  19. package/dist/backend/sqlite/libsql.d.ts +3 -3
  20. package/dist/backend/sqlite/libsql.js +9 -8
  21. package/dist/backend/sqlite/libsql.js.map +1 -1
  22. package/dist/backend/sqlite/local.cjs +14 -13
  23. package/dist/backend/sqlite/local.cjs.map +1 -1
  24. package/dist/backend/sqlite/local.d.cts +3 -3
  25. package/dist/backend/sqlite/local.d.ts +3 -3
  26. package/dist/backend/sqlite/local.js +9 -8
  27. package/dist/backend/sqlite/local.js.map +1 -1
  28. package/dist/{chunk-SFFJ2IKN.cjs → chunk-3642VBVC.cjs} +70 -68
  29. package/dist/chunk-3642VBVC.cjs.map +1 -0
  30. package/dist/chunk-3E7SNSIA.cjs +55 -0
  31. package/dist/chunk-3E7SNSIA.cjs.map +1 -0
  32. package/dist/{chunk-6RMLO5O4.cjs → chunk-3IR2GEMJ.cjs} +8 -7
  33. package/dist/chunk-3IR2GEMJ.cjs.map +1 -0
  34. package/dist/{chunk-6SA7UIQJ.cjs → chunk-3QRBXYT5.cjs} +6 -6
  35. package/dist/{chunk-6SA7UIQJ.cjs.map → chunk-3QRBXYT5.cjs.map} +1 -1
  36. package/dist/{chunk-54JVNOAQ.cjs → chunk-4RBAJVEC.cjs} +128 -6
  37. package/dist/chunk-4RBAJVEC.cjs.map +1 -0
  38. package/dist/{chunk-IJORSAC3.js → chunk-6NRFUHMS.js} +4 -3
  39. package/dist/chunk-6NRFUHMS.js.map +1 -0
  40. package/dist/{chunk-FLOI7QOT.cjs → chunk-BF3ZDEUE.cjs} +104 -72
  41. package/dist/chunk-BF3ZDEUE.cjs.map +1 -0
  42. package/dist/{chunk-2HZGWCB4.js → chunk-C4C4Q7A6.js} +3 -3
  43. package/dist/chunk-C4C4Q7A6.js.map +1 -0
  44. package/dist/{chunk-O4VUR6SI.js → chunk-CEF2QOTO.js} +3 -3
  45. package/dist/{chunk-O4VUR6SI.js.map → chunk-CEF2QOTO.js.map} +1 -1
  46. package/dist/{chunk-V4IQYOQ4.cjs → chunk-CFNT3ITG.cjs} +3 -3
  47. package/dist/chunk-CFNT3ITG.cjs.map +1 -0
  48. package/dist/{chunk-LJLMDDCW.js → chunk-CWFPJ3RC.js} +3 -3
  49. package/dist/{chunk-LJLMDDCW.js.map → chunk-CWFPJ3RC.js.map} +1 -1
  50. package/dist/{chunk-RZ4DH4E7.cjs → chunk-D3EWZ67S.cjs} +3348 -2030
  51. package/dist/chunk-D3EWZ67S.cjs.map +1 -0
  52. package/dist/{chunk-GKFWM3DS.js → chunk-DJYRRDHA.js} +124 -5
  53. package/dist/chunk-DJYRRDHA.js.map +1 -0
  54. package/dist/{chunk-YQGLVWB2.cjs → chunk-EF3Z645U.cjs} +2 -52
  55. package/dist/chunk-EF3Z645U.cjs.map +1 -0
  56. package/dist/{chunk-ASYQDPPU.cjs → chunk-FDN43KXV.cjs} +19 -19
  57. package/dist/{chunk-ASYQDPPU.cjs.map → chunk-FDN43KXV.cjs.map} +1 -1
  58. package/dist/chunk-GYVBEZAO.js +120 -0
  59. package/dist/chunk-GYVBEZAO.js.map +1 -0
  60. package/dist/chunk-I4W7MKVG.cjs +1526 -0
  61. package/dist/chunk-I4W7MKVG.cjs.map +1 -0
  62. package/dist/{chunk-MXE2U3EX.js → chunk-IZ4PCL47.js} +3 -52
  63. package/dist/chunk-IZ4PCL47.js.map +1 -0
  64. package/dist/{chunk-4A4GUZTQ.js → chunk-JHOBXD7O.js} +10 -8
  65. package/dist/chunk-JHOBXD7O.js.map +1 -0
  66. package/dist/{chunk-CIINW6PS.cjs → chunk-KY5SDVA7.cjs} +676 -635
  67. package/dist/chunk-KY5SDVA7.cjs.map +1 -0
  68. package/dist/{chunk-N2MBPTAF.cjs → chunk-LM2IC4ZJ.cjs} +14 -14
  69. package/dist/{chunk-N2MBPTAF.cjs.map → chunk-LM2IC4ZJ.cjs.map} +1 -1
  70. package/dist/{chunk-7JGLPAFV.cjs → chunk-MBLXCV2Y.cjs} +16 -16
  71. package/dist/chunk-MBLXCV2Y.cjs.map +1 -0
  72. package/dist/{chunk-5DTPBFX5.cjs → chunk-NG5N7R45.cjs} +28 -28
  73. package/dist/{chunk-5DTPBFX5.cjs.map → chunk-NG5N7R45.cjs.map} +1 -1
  74. package/dist/chunk-OIE2UPVG.cjs +126 -0
  75. package/dist/chunk-OIE2UPVG.cjs.map +1 -0
  76. package/dist/{chunk-STP7QVUB.js → chunk-OS6KS3HV.js} +43 -11
  77. package/dist/chunk-OS6KS3HV.js.map +1 -0
  78. package/dist/{chunk-2ZY4K43M.js → chunk-PIHXFF73.js} +3 -3
  79. package/dist/{chunk-2ZY4K43M.js.map → chunk-PIHXFF73.js.map} +1 -1
  80. package/dist/{chunk-ATTXBMAX.js → chunk-PRMKPFN5.js} +3 -3
  81. package/dist/{chunk-ATTXBMAX.js.map → chunk-PRMKPFN5.js.map} +1 -1
  82. package/dist/chunk-PYD3UCDU.js +1480 -0
  83. package/dist/chunk-PYD3UCDU.js.map +1 -0
  84. package/dist/{chunk-MAQJYNJR.js → chunk-QBYTJSAY.js} +14 -16
  85. package/dist/chunk-QBYTJSAY.js.map +1 -0
  86. package/dist/{chunk-THX25XW6.js → chunk-SPT5JMME.js} +27 -7
  87. package/dist/chunk-SPT5JMME.js.map +1 -0
  88. package/dist/{chunk-3JBJEYWE.cjs → chunk-SST7XDY6.cjs} +32 -12
  89. package/dist/chunk-SST7XDY6.cjs.map +1 -0
  90. package/dist/{chunk-VZX2KSY3.js → chunk-UPDMJEW6.js} +608 -563
  91. package/dist/chunk-UPDMJEW6.js.map +1 -0
  92. package/dist/{chunk-UIFEEPME.js → chunk-WLP6TKFB.js} +3141 -1823
  93. package/dist/chunk-WLP6TKFB.js.map +1 -0
  94. package/dist/{chunk-VMEWZU2N.cjs → chunk-WWVV44DY.cjs} +11 -11
  95. package/dist/{chunk-VMEWZU2N.cjs.map → chunk-WWVV44DY.cjs.map} +1 -1
  96. package/dist/{chunk-UJ4V3YUQ.js → chunk-XKNAK27R.js} +3 -3
  97. package/dist/{chunk-UJ4V3YUQ.js.map → chunk-XKNAK27R.js.map} +1 -1
  98. package/dist/chunk-XLUNB2TZ.js +53 -0
  99. package/dist/chunk-XLUNB2TZ.js.map +1 -0
  100. package/dist/{chunk-XKJQQQVR.js → chunk-XVUJY72M.js} +6 -6
  101. package/dist/{chunk-XKJQQQVR.js.map → chunk-XVUJY72M.js.map} +1 -1
  102. package/dist/{chunk-5CN7B4CZ.cjs → chunk-YW7GHMF4.cjs} +17 -17
  103. package/dist/{chunk-5CN7B4CZ.cjs.map → chunk-YW7GHMF4.cjs.map} +1 -1
  104. package/dist/{ddl-BJcPrB4L.d.ts → ddl-CVnHJcar.d.ts} +3 -3
  105. package/dist/{ddl-CPZ7mB7X.d.cts → ddl-Dxt_fBHr.d.cts} +3 -3
  106. package/dist/{extension-types-D1k0inWU.d.cts → extension-types-YGFJPrzK.d.cts} +1 -1
  107. package/dist/{extension-types-D1k0inWU.d.ts → extension-types-YGFJPrzK.d.ts} +1 -1
  108. package/dist/graph-extension/index.d.cts +4 -4
  109. package/dist/graph-extension/index.d.ts +4 -4
  110. package/dist/graph-merge/index.cjs +200 -55
  111. package/dist/graph-merge/index.cjs.map +1 -1
  112. package/dist/graph-merge/index.d.cts +6 -6
  113. package/dist/graph-merge/index.d.ts +6 -6
  114. package/dist/graph-merge/index.js +185 -40
  115. package/dist/graph-merge/index.js.map +1 -1
  116. package/dist/{index-BEK0VtNB.d.ts → index-DGX6D9j1.d.ts} +2 -2
  117. package/dist/{index-C8sSTErx.d.cts → index-SEvUHDA6.d.cts} +2 -2
  118. package/dist/index.cjs +145 -116
  119. package/dist/index.cjs.map +1 -1
  120. package/dist/index.d.cts +25 -55
  121. package/dist/index.d.ts +25 -55
  122. package/dist/index.js +19 -17
  123. package/dist/index.js.map +1 -1
  124. package/dist/indexes/index.cjs +15 -14
  125. package/dist/indexes/index.d.cts +5 -5
  126. package/dist/indexes/index.d.ts +5 -5
  127. package/dist/indexes/index.js +6 -5
  128. package/dist/interchange/index.cjs +15 -15
  129. package/dist/interchange/index.d.cts +9 -9
  130. package/dist/interchange/index.d.ts +9 -9
  131. package/dist/interchange/index.js +2 -2
  132. package/dist/{manager-Np7VVSlE.d.cts → manager-BYl_zyZR.d.cts} +2 -2
  133. package/dist/{manager-By-WEd8M.d.ts → manager-C0_r0ALf.d.ts} +2 -2
  134. package/dist/{materialize-indexes-CAGhqPHQ.d.ts → materialize-indexes-CAkIYFV-.d.ts} +1 -1
  135. package/dist/{materialize-indexes-DEYkU-wr.d.cts → materialize-indexes-D20xNluI.d.cts} +1 -1
  136. package/dist/{postgres-4J2tmauh.d.cts → postgres-MzBPkYzq.d.ts} +2651 -596
  137. package/dist/{postgres-BdIb7tS_.d.ts → postgres-_9psmVGm.d.cts} +2651 -596
  138. package/dist/profiler/index.cjs +4 -3
  139. package/dist/profiler/index.cjs.map +1 -1
  140. package/dist/profiler/index.d.cts +7 -7
  141. package/dist/profiler/index.d.ts +7 -7
  142. package/dist/profiler/index.js +2 -1
  143. package/dist/profiler/index.js.map +1 -1
  144. package/dist/schema/index.cjs +30 -29
  145. package/dist/schema/index.cjs.map +1 -1
  146. package/dist/schema/index.d.cts +7 -7
  147. package/dist/schema/index.d.ts +7 -7
  148. package/dist/schema/index.js +7 -6
  149. package/dist/schema/index.js.map +1 -1
  150. package/dist/{sqlite-aM_PAz-O.d.ts → sqlite-BfksSb45.d.cts} +2726 -455
  151. package/dist/{sqlite-Gatk_iw_.d.cts → sqlite-gm44dShb.d.ts} +2726 -455
  152. package/dist/{store-DuCbdqsh.d.cts → store-view-9jQ2rPum.d.cts} +1101 -465
  153. package/dist/{store-MloRPwb9.d.ts → store-view-FfE5_7eo.d.ts} +1101 -465
  154. package/dist/{types-DoByHGyU.d.cts → types-B6mhrcLv.d.cts} +2 -2
  155. package/dist/{types-C1CYxWrL.d.cts → types-C_UnQOQp.d.cts} +154 -23
  156. package/dist/{types-K3vpaJAL.d.ts → types-CdWxK-nD.d.ts} +2 -2
  157. package/dist/{types-BKQljXom.d.ts → types-DGylBhWo.d.ts} +154 -23
  158. package/dist/{where-DydZjGOb.d.cts → where-Bpp2ul_z.d.cts} +2 -2
  159. package/dist/{where-Dt49bvo6.d.ts → where-BsEXraKW.d.ts} +2 -2
  160. package/package.json +2 -2
  161. package/dist/chunk-2HZGWCB4.js.map +0 -1
  162. package/dist/chunk-3JBJEYWE.cjs.map +0 -1
  163. package/dist/chunk-4A4GUZTQ.js.map +0 -1
  164. package/dist/chunk-54JVNOAQ.cjs.map +0 -1
  165. package/dist/chunk-6G35I3VW.cjs +0 -40
  166. package/dist/chunk-6G35I3VW.cjs.map +0 -1
  167. package/dist/chunk-6RMLO5O4.cjs.map +0 -1
  168. package/dist/chunk-7JGLPAFV.cjs.map +0 -1
  169. package/dist/chunk-CIINW6PS.cjs.map +0 -1
  170. package/dist/chunk-FLOI7QOT.cjs.map +0 -1
  171. package/dist/chunk-GKFWM3DS.js.map +0 -1
  172. package/dist/chunk-IJORSAC3.js.map +0 -1
  173. package/dist/chunk-M6OC455F.js +0 -65
  174. package/dist/chunk-M6OC455F.js.map +0 -1
  175. package/dist/chunk-MAQJYNJR.js.map +0 -1
  176. package/dist/chunk-MXE2U3EX.js.map +0 -1
  177. package/dist/chunk-RP7YITHX.cjs +0 -70
  178. package/dist/chunk-RP7YITHX.cjs.map +0 -1
  179. package/dist/chunk-RZ4DH4E7.cjs.map +0 -1
  180. package/dist/chunk-SAJSMR2B.js +0 -35
  181. package/dist/chunk-SAJSMR2B.js.map +0 -1
  182. package/dist/chunk-SFFJ2IKN.cjs.map +0 -1
  183. package/dist/chunk-STP7QVUB.js.map +0 -1
  184. package/dist/chunk-THX25XW6.js.map +0 -1
  185. package/dist/chunk-UIFEEPME.js.map +0 -1
  186. package/dist/chunk-V4IQYOQ4.cjs.map +0 -1
  187. package/dist/chunk-VZX2KSY3.js.map +0 -1
  188. package/dist/chunk-YQGLVWB2.cjs.map +0 -1
@@ -1,13 +1,13 @@
1
- import { G as GraphDef, t as NodeKinds, u as GetNodeType } from '../manager-Np7VVSlE.cjs';
2
- import { S as Store, N as Node, U as UniqueIntrospection } from '../store-DuCbdqsh.cjs';
3
- import { d as NodeId, N as NodeType, e as EdgeId, J as JsonValue, f as KindAnnotations, g as EdgeType, D as DeleteBehavior, T as TemporalMode, h as GraphExtensionVersion, i as ExtensionNodeDef, j as ExtensionEdgeDef, k as ExtensionOntologyRelation, l as ExtensionIndex } from '../extension-types-D1k0inWU.cjs';
4
- import { T as TypeGraphError } from '../index-C8sSTErx.cjs';
1
+ import { G as GraphDef, t as NodeKinds, u as GetNodeType } from '../manager-BYl_zyZR.cjs';
2
+ import { S as Store, N as Node, U as UniqueIntrospection } from '../store-view-9jQ2rPum.cjs';
3
+ import { d as NodeId, N as NodeType, e as EdgeId, J as JsonValue, f as KindAnnotations, g as EdgeType, D as DeleteBehavior, T as TemporalMode, h as GraphExtensionVersion, i as ExtensionNodeDef, j as ExtensionEdgeDef, k as ExtensionOntologyRelation, l as ExtensionIndex } from '../extension-types-YGFJPrzK.cjs';
4
+ import { T as TypeGraphError } from '../index-SEvUHDA6.cjs';
5
5
  import { R as Result } from '../result-DWo6mUVz.cjs';
6
6
  export { i as isErr, a as isOk, u as unwrap } from '../result-DWo6mUVz.cjs';
7
- import { G as GraphBackend, M as MetaEdge, I as IndexDeclaration } from '../types-C1CYxWrL.cjs';
7
+ import { G as GraphBackend, M as MetaEdge, I as IndexDeclaration } from '../types-C_UnQOQp.cjs';
8
8
  import { z } from 'zod';
9
+ import '../materialize-indexes-D20xNluI.cjs';
9
10
  import 'drizzle-orm';
10
- import '../materialize-indexes-DEYkU-wr.cjs';
11
11
  import 'drizzle-orm/pg-core';
12
12
  import 'drizzle-orm/sqlite-core';
13
13
 
@@ -1,13 +1,13 @@
1
- import { G as GraphDef, t as NodeKinds, u as GetNodeType } from '../manager-By-WEd8M.js';
2
- import { S as Store, N as Node, U as UniqueIntrospection } from '../store-MloRPwb9.js';
3
- import { d as NodeId, N as NodeType, e as EdgeId, J as JsonValue, f as KindAnnotations, g as EdgeType, D as DeleteBehavior, T as TemporalMode, h as GraphExtensionVersion, i as ExtensionNodeDef, j as ExtensionEdgeDef, k as ExtensionOntologyRelation, l as ExtensionIndex } from '../extension-types-D1k0inWU.js';
4
- import { T as TypeGraphError } from '../index-BEK0VtNB.js';
1
+ import { G as GraphDef, t as NodeKinds, u as GetNodeType } from '../manager-C0_r0ALf.js';
2
+ import { S as Store, N as Node, U as UniqueIntrospection } from '../store-view-FfE5_7eo.js';
3
+ import { d as NodeId, N as NodeType, e as EdgeId, J as JsonValue, f as KindAnnotations, g as EdgeType, D as DeleteBehavior, T as TemporalMode, h as GraphExtensionVersion, i as ExtensionNodeDef, j as ExtensionEdgeDef, k as ExtensionOntologyRelation, l as ExtensionIndex } from '../extension-types-YGFJPrzK.js';
4
+ import { T as TypeGraphError } from '../index-DGX6D9j1.js';
5
5
  import { R as Result } from '../result-DWo6mUVz.js';
6
6
  export { i as isErr, a as isOk, u as unwrap } from '../result-DWo6mUVz.js';
7
- import { G as GraphBackend, M as MetaEdge, I as IndexDeclaration } from '../types-BKQljXom.js';
7
+ import { G as GraphBackend, M as MetaEdge, I as IndexDeclaration } from '../types-DGylBhWo.js';
8
8
  import { z } from 'zod';
9
+ import '../materialize-indexes-CAkIYFV-.js';
9
10
  import 'drizzle-orm';
10
- import '../materialize-indexes-CAGhqPHQ.js';
11
11
  import 'drizzle-orm/pg-core';
12
12
  import 'drizzle-orm/sqlite-core';
13
13
 
@@ -1,18 +1,19 @@
1
- import { createStoreWithSchema, generateId, computeUniqueKey } from '../chunk-UIFEEPME.js';
1
+ import { createStoreWithSchema, generateId, computeUniqueKey } from '../chunk-WLP6TKFB.js';
2
2
  import '../chunk-3EUKJEQ3.js';
3
- import { exportGraph, importGraph } from '../chunk-THX25XW6.js';
4
- import { defineNode, computeSchemaHash, serializeSchema } from '../chunk-XKJQQQVR.js';
5
- import { getNodeKinds, getEdgeKinds, defineGraph } from '../chunk-MAQJYNJR.js';
6
- import '../chunk-2ZY4K43M.js';
7
- import '../chunk-IJORSAC3.js';
8
- import '../chunk-MXE2U3EX.js';
3
+ import { exportGraph, importGraph } from '../chunk-SPT5JMME.js';
4
+ import { defineNode, computeSchemaHash, serializeSchema } from '../chunk-XVUJY72M.js';
5
+ import { getNodeKinds, getEdgeKinds, defineGraph } from '../chunk-QBYTJSAY.js';
6
+ import '../chunk-PIHXFF73.js';
7
+ import '../chunk-6NRFUHMS.js';
8
+ import '../chunk-IZ4PCL47.js';
9
9
  import '../chunk-454I6GDF.js';
10
10
  import { ok, err, isErr, computeTransitiveClosure, isReachable } from '../chunk-YCV72E6L.js';
11
11
  export { isErr, isOk, unwrap } from '../chunk-YCV72E6L.js';
12
- import '../chunk-SAJSMR2B.js';
13
- import { sortedReplacer } from '../chunk-M6OC455F.js';
14
- import '../chunk-2HZGWCB4.js';
12
+ import '../chunk-PYD3UCDU.js';
13
+ import { sortedReplacer } from '../chunk-GYVBEZAO.js';
14
+ import '../chunk-C4C4Q7A6.js';
15
15
  import { sha256Hex } from '../chunk-2YMGNYXY.js';
16
+ import '../chunk-XLUNB2TZ.js';
16
17
  import { TypeGraphError } from '../chunk-TQOYM4LN.js';
17
18
  import '../chunk-K5P5OZXS.js';
18
19
  import { z } from 'zod';
@@ -317,10 +318,7 @@ function byDigestEntry(left, right) {
317
318
  return byKind === 0 ? compareStrings(left.id, right.id) : byKind;
318
319
  }
319
320
  var CONTENT_FINGERPRINT_BYTES = 16;
320
- async function computeContentFingerprint(store) {
321
- const backend = store.backend;
322
- const graphId = store.graphId;
323
- const graph = store.graph;
321
+ async function computeContentComponent(backend, graphId, graph) {
324
322
  const nodeKinds = getNodeKinds(graph);
325
323
  const edgeKinds = getEdgeKinds(graph);
326
324
  const nodeDigest = [];
@@ -368,12 +366,16 @@ async function computeContentFingerprint(store) {
368
366
  async function computeBaseVersion(store) {
369
367
  const [schemaComponent, contentComponent] = await Promise.all([
370
368
  computeSchemaComponent(store),
371
- computeContentFingerprint(store)
369
+ computeContentComponent(store.backend, store.graphId, store.graph)
372
370
  ]);
373
371
  return asBaseVersion(
374
372
  `${schemaComponent}${TOKEN_SEPARATOR}${contentComponent}`
375
373
  );
376
374
  }
375
+ function contentComponentOf(version) {
376
+ const separatorIndex = version.indexOf(TOKEN_SEPARATOR);
377
+ return separatorIndex === -1 ? version : version.slice(separatorIndex + 1);
378
+ }
377
379
 
378
380
  // src/graph-merge/errors.ts
379
381
  var MERGE_ERROR_CODES = {
@@ -2506,6 +2508,48 @@ async function stageBranches(baseStore, branches) {
2506
2508
  };
2507
2509
  }
2508
2510
 
2511
+ // src/graph-merge/tx-retry.ts
2512
+ var RETRYABLE_SQLSTATES = /* @__PURE__ */ new Set(["40001", "40P01"]);
2513
+ var RETRYABLE_MESSAGE_PATTERN = /could not serialize access|deadlock detected/i;
2514
+ var MAX_COMMIT_ATTEMPTS = 3;
2515
+ function isRetryableTxConflict(error) {
2516
+ const seen = /* @__PURE__ */ new Set();
2517
+ let current = error;
2518
+ while (current !== void 0 && current !== null && typeof current === "object" && !seen.has(current)) {
2519
+ seen.add(current);
2520
+ const candidate = current;
2521
+ if (typeof candidate.code === "string" && RETRYABLE_SQLSTATES.has(candidate.code)) {
2522
+ return true;
2523
+ }
2524
+ if (typeof candidate.message === "string" && RETRYABLE_MESSAGE_PATTERN.test(candidate.message)) {
2525
+ return true;
2526
+ }
2527
+ current = candidate.cause;
2528
+ }
2529
+ return false;
2530
+ }
2531
+ async function withTxConflictRetry(commit) {
2532
+ let lastConflict;
2533
+ for (let attempt = 1; attempt <= MAX_COMMIT_ATTEMPTS; attempt += 1) {
2534
+ try {
2535
+ return await commit();
2536
+ } catch (error) {
2537
+ if (!isRetryableTxConflict(error)) {
2538
+ throw error;
2539
+ }
2540
+ lastConflict = error;
2541
+ }
2542
+ }
2543
+ throw new MergeError(
2544
+ `Merge commit aborted by transaction conflicts (serialization failure or deadlock) on ${MAX_COMMIT_ATTEMPTS} consecutive attempts; giving up.`,
2545
+ {
2546
+ cause: lastConflict,
2547
+ details: { attempts: MAX_COMMIT_ATTEMPTS },
2548
+ suggestion: "Reduce concurrent writes to the merge target, or serialize merges against it."
2549
+ }
2550
+ );
2551
+ }
2552
+
2509
2553
  // src/graph-merge/type-reconcile.ts
2510
2554
  var INCOMPATIBLE_TYPES_FLAG_REASON = "type-reconcile:incompatible-kinds";
2511
2555
  function distinctKinds(kinds) {
@@ -3225,20 +3269,48 @@ async function applyMergePlan(plan, nodesApi, edgesApi) {
3225
3269
  }
3226
3270
  return { nodes: committedNodeIds.size, edges: committedEdges };
3227
3271
  }
3228
- async function commitPlan(target, plan) {
3272
+ async function commitPlan(target, plan, expectedBaseVersion) {
3229
3273
  if (!target.backend.capabilities.transactions) {
3230
3274
  throw new MergeError(
3231
3275
  "merge() requires a transaction-capable target backend. The merged plan (canonical upserts, soft-deletes, edge upserts) must commit atomically; a non-transactional fallback would leave a partially-merged graph on a mid-commit failure. (mergeIncremental() enforces the same requirement.)",
3232
3276
  { details: { capability: "transactions" } }
3233
3277
  );
3234
3278
  }
3235
- return target.transaction(
3236
- (tx) => applyMergePlan(
3237
- plan,
3238
- tx.nodes,
3239
- tx.edges
3240
- )
3279
+ return withTxConflictRetry(
3280
+ () => target.transaction(async (tx) => {
3281
+ if (expectedBaseVersion !== void 0) {
3282
+ await assertTargetUnchanged(tx.backend, target, expectedBaseVersion);
3283
+ }
3284
+ return applyMergePlan(
3285
+ plan,
3286
+ tx.nodes,
3287
+ tx.edges
3288
+ );
3289
+ }, MERGE_COMMIT_TX_OPTIONS)
3290
+ );
3291
+ }
3292
+ var MERGE_COMMIT_TX_OPTIONS = {
3293
+ isolationLevel: "serializable"
3294
+ };
3295
+ async function assertTargetUnchanged(txBackend, target, expectedBaseVersion) {
3296
+ const liveContent = await computeContentComponent(
3297
+ txBackend,
3298
+ target.graphId,
3299
+ target.graph
3241
3300
  );
3301
+ const expectedContent = contentComponentOf(expectedBaseVersion);
3302
+ if (liveContent !== expectedContent) {
3303
+ throw new BaseVersionMismatchError(
3304
+ "The merge target was modified between the base@V check and the commit transaction; the resolved plan no longer describes the live target and was not applied.",
3305
+ {
3306
+ details: {
3307
+ expectedContentFingerprint: expectedContent,
3308
+ liveContentFingerprint: liveContent
3309
+ },
3310
+ suggestion: "Re-run the merge (and re-branch if the divergence is real), or serialize writers against merges on this target."
3311
+ }
3312
+ );
3313
+ }
3242
3314
  }
3243
3315
  function nodeCollection(nodes, kind) {
3244
3316
  const collection = nodes[kind];
@@ -3276,7 +3348,7 @@ async function validateBaseVersions(target, branches) {
3276
3348
  );
3277
3349
  }
3278
3350
  }
3279
- return ok();
3351
+ return ok(targetVersion);
3280
3352
  }
3281
3353
  function tryNormalize(optionsInput) {
3282
3354
  try {
@@ -3285,7 +3357,7 @@ function tryNormalize(optionsInput) {
3285
3357
  return err(new MergeError("Invalid merge options.", { cause: error }));
3286
3358
  }
3287
3359
  }
3288
- async function resolveMerge(store, target, branches, options, useBaseSources, incremental) {
3360
+ async function resolveMerge(store, target, branches, options, useBaseSources, incremental, expectedBaseVersion) {
3289
3361
  let targetBranchSeen = false;
3290
3362
  for (const branch2 of branches) {
3291
3363
  if (branch2.id === BASE_PROVENANCE_BRANCH) {
@@ -3356,7 +3428,16 @@ async function resolveMerge(store, target, branches, options, useBaseSources, in
3356
3428
  subClassClosure,
3357
3429
  preferredBranchId
3358
3430
  );
3359
- const merged = incremental === void 0 ? await commitPlan(target, plan) : await commitIncrementalPlan(target, plan);
3431
+ const merged = incremental === void 0 ? await commitPlan(target, plan, expectedBaseVersion) : await commitIncrementalPlan(target, plan, {
3432
+ stagedNewByKind: newNodesByKind(staging),
3433
+ options,
3434
+ introspectionKinds,
3435
+ // The committed (kind, id) keys the base sources matched at PLAN time;
3436
+ // anything NEW the in-tx re-probe surfaces is a window write.
3437
+ plannedBaseMatchKeys: new Set(
3438
+ candidates.data.baseMembers.map((member) => mergeKeyOf(member))
3439
+ )
3440
+ });
3360
3441
  const provenance = options.provenance ? buildProvenanceIndex(plan.provenanceRecords) : { byBranch: () => ({ nodeIds: [], edgeIds: [] }) };
3361
3442
  const warnings = [...plan.warnings];
3362
3443
  let provenancePersisted;
@@ -3417,7 +3498,15 @@ async function merge(store, branches, optionsInput = {}) {
3417
3498
  if (isErr(precondition)) {
3418
3499
  return err(precondition.error);
3419
3500
  }
3420
- return resolveMerge(store, target, branches, options, false);
3501
+ return resolveMerge(
3502
+ store,
3503
+ target,
3504
+ branches,
3505
+ options,
3506
+ false,
3507
+ void 0,
3508
+ precondition.data
3509
+ );
3421
3510
  }
3422
3511
  async function validateForkPointVersions(forkPoint, branches) {
3423
3512
  const forkVersion = await computeBaseVersion(forkPoint);
@@ -3650,13 +3739,12 @@ async function validateIncrementalEdgeWrites(target, edgesApi, plan) {
3650
3739
  }
3651
3740
  const notFound = [...plannedKindById.keys()].filter((id) => !existingById.has(id)).sort((left, right) => compareStrings(left, right));
3652
3741
  if (notFound.length > 0) {
3653
- const perKind = await Promise.all(
3654
- Object.keys(target.graph.edges).map(
3655
- (kind) => edgeCollection(edgesApi, kind).getByIds(notFound, INCLUDE_TOMBSTONES)
3656
- )
3657
- );
3658
3742
  const crossKindById = /* @__PURE__ */ new Map();
3659
- for (const rows of perKind) {
3743
+ for (const kind of Object.keys(target.graph.edges)) {
3744
+ const rows = await edgeCollection(edgesApi, kind).getByIds(
3745
+ notFound,
3746
+ INCLUDE_TOMBSTONES
3747
+ );
3660
3748
  for (const [index, id] of notFound.entries()) {
3661
3749
  const row = rows[index];
3662
3750
  if (row !== void 0 && !crossKindById.has(id)) {
@@ -3724,20 +3812,77 @@ async function validateIncrementalEdgeWrites(target, edgesApi, plan) {
3724
3812
  }
3725
3813
  }
3726
3814
  }
3727
- async function commitIncrementalPlan(target, plan) {
3815
+ async function collectBaseMatchKeys(lookupStore, guard) {
3816
+ const matched = /* @__PURE__ */ new Set();
3817
+ for (const [kind, stagedNodes] of guard.stagedNewByKind) {
3818
+ const resolveConfig = guard.options.resolve[kind];
3819
+ if (resolveConfig === void 0) {
3820
+ continue;
3821
+ }
3822
+ const collection = lookupStore.nodes[kind];
3823
+ if (collection === void 0 || stagedNodes.length === 0) {
3824
+ continue;
3825
+ }
3826
+ const items = stagedNodes.map((staged) => ({ props: staged.node.props }));
3827
+ for (const constraint of uniqueConstraintsFor(
3828
+ guard.introspectionKinds,
3829
+ kind
3830
+ )) {
3831
+ const matches = await collection.bulkFindByConstraint(
3832
+ constraint.name,
3833
+ items
3834
+ );
3835
+ for (const base of matches) {
3836
+ if (base !== void 0) {
3837
+ matched.add(mergeKeyOf(base));
3838
+ }
3839
+ }
3840
+ }
3841
+ if (resolveConfig.blockIndex !== void 0) {
3842
+ const matchesByItem = await collection.bulkFindByIndex(
3843
+ resolveConfig.blockIndex,
3844
+ items
3845
+ );
3846
+ for (const perItem of matchesByItem) {
3847
+ for (const base of perItem) {
3848
+ matched.add(mergeKeyOf(base));
3849
+ }
3850
+ }
3851
+ }
3852
+ }
3853
+ return matched;
3854
+ }
3855
+ async function assertBaseResolutionStable(lookupStore, guard) {
3856
+ const liveKeys = await collectBaseMatchKeys(lookupStore, guard);
3857
+ const appeared = [...liveKeys].filter((key) => !guard.plannedBaseMatchKeys.has(key)).sort((left, right) => compareMergeKeys(left, right));
3858
+ if (appeared.length === 0) {
3859
+ return;
3860
+ }
3861
+ throw new BaseVersionMismatchError(
3862
+ `mergeIncremental() resolved its branch additions against the target as of planning, but ${appeared.length} committed row(s) matching a branch addition's identity key (a unique constraint or block index) were inserted before the commit transaction. Committing the plan would create duplicate entities the base-source resolution would otherwise have collapsed.`,
3863
+ {
3864
+ details: { appeared },
3865
+ suggestion: "Re-run mergeIncremental(); the re-planned merge resolves the branch additions against the now-committed rows."
3866
+ }
3867
+ );
3868
+ }
3869
+ async function commitIncrementalPlan(target, plan, guard) {
3728
3870
  if (!target.backend.capabilities.transactions) {
3729
3871
  throw new MergeError(
3730
3872
  "mergeIncremental() requires a transaction-capable target backend. Incremental writes must preflight existing rows and commit atomically; non-transactional fallback would allow partial graph writes.",
3731
3873
  { details: { capability: "transactions" } }
3732
3874
  );
3733
3875
  }
3734
- return target.transaction(async (tx) => {
3735
- const nodesApi = tx.nodes;
3736
- const edgesApi = tx.edges;
3737
- await validateIncrementalNodeWrites(target, nodesApi, plan);
3738
- await validateIncrementalEdgeWrites(target, edgesApi, plan);
3739
- return applyMergePlan(plan, nodesApi, edgesApi);
3740
- });
3876
+ return withTxConflictRetry(
3877
+ () => target.transaction(async (tx) => {
3878
+ const nodesApi = tx.nodes;
3879
+ const edgesApi = tx.edges;
3880
+ await assertBaseResolutionStable(tx, guard);
3881
+ await validateIncrementalNodeWrites(target, nodesApi, plan);
3882
+ await validateIncrementalEdgeWrites(target, edgesApi, plan);
3883
+ return applyMergePlan(plan, nodesApi, edgesApi);
3884
+ }, MERGE_COMMIT_TX_OPTIONS)
3885
+ );
3741
3886
  }
3742
3887
  async function mergeIncremental(args) {
3743
3888
  const { forkPoint, target, branches } = args;