@ibgib/core-gib 0.1.20 → 0.1.22

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 (165) hide show
  1. package/dist/sync/graft-info/graft-info-constants.d.mts +5 -0
  2. package/dist/sync/graft-info/graft-info-constants.d.mts.map +1 -0
  3. package/dist/sync/graft-info/graft-info-constants.mjs +5 -0
  4. package/dist/sync/graft-info/graft-info-constants.mjs.map +1 -0
  5. package/dist/sync/graft-info/graft-info-helpers.d.mts +49 -0
  6. package/dist/sync/graft-info/graft-info-helpers.d.mts.map +1 -0
  7. package/dist/sync/graft-info/graft-info-helpers.mjs +236 -0
  8. package/dist/sync/graft-info/graft-info-helpers.mjs.map +1 -0
  9. package/dist/sync/graft-info/graft-info-helpers.respec.d.mts +2 -0
  10. package/dist/sync/graft-info/graft-info-helpers.respec.d.mts.map +1 -0
  11. package/dist/sync/graft-info/graft-info-helpers.respec.mjs +70 -0
  12. package/dist/sync/graft-info/graft-info-helpers.respec.mjs.map +1 -0
  13. package/dist/sync/graft-info/graft-info-types.d.mts +31 -0
  14. package/dist/sync/{merge-info/merge-info-types.d.mts.map → graft-info/graft-info-types.d.mts.map} +1 -1
  15. package/dist/sync/graft-info/graft-info-types.mjs +2 -0
  16. package/dist/sync/graft-info/graft-info-types.mjs.map +1 -0
  17. package/dist/sync/strategies/conflict-optimistic.d.mts +1 -1
  18. package/dist/sync/strategies/conflict-optimistic.d.mts.map +1 -1
  19. package/dist/sync/strategies/conflict-optimistic.mjs +10 -60
  20. package/dist/sync/strategies/conflict-optimistic.mjs.map +1 -1
  21. package/dist/sync/sync-conflict.respec.mjs +155 -34
  22. package/dist/sync/sync-conflict.respec.mjs.map +1 -1
  23. package/dist/sync/sync-constants.d.mts +9 -11
  24. package/dist/sync/sync-constants.d.mts.map +1 -1
  25. package/dist/sync/sync-constants.mjs +3 -5
  26. package/dist/sync/sync-constants.mjs.map +1 -1
  27. package/dist/sync/sync-helpers.d.mts +2 -0
  28. package/dist/sync/sync-helpers.d.mts.map +1 -1
  29. package/dist/sync/sync-helpers.mjs +11 -9
  30. package/dist/sync/sync-helpers.mjs.map +1 -1
  31. package/dist/sync/sync-innerspace-constants.respec.mjs +5 -1
  32. package/dist/sync/sync-innerspace-constants.respec.mjs.map +1 -1
  33. package/dist/sync/sync-innerspace-deep-updates.respec.mjs +5 -1
  34. package/dist/sync/sync-innerspace-deep-updates.respec.mjs.map +1 -1
  35. package/dist/sync/sync-innerspace-dest-ahead.respec.mjs +8 -4
  36. package/dist/sync/sync-innerspace-dest-ahead.respec.mjs.map +1 -1
  37. package/dist/sync/sync-innerspace-multiple-timelines.respec.mjs +5 -1
  38. package/dist/sync/sync-innerspace-multiple-timelines.respec.mjs.map +1 -1
  39. package/dist/sync/sync-innerspace-partial-update.respec.mjs +5 -1
  40. package/dist/sync/sync-innerspace-partial-update.respec.mjs.map +1 -1
  41. package/dist/sync/sync-innerspace.respec.mjs +5 -1
  42. package/dist/sync/sync-innerspace.respec.mjs.map +1 -1
  43. package/dist/sync/sync-peer/sync-peer-constants.d.mts +2 -0
  44. package/dist/sync/sync-peer/sync-peer-constants.d.mts.map +1 -0
  45. package/dist/sync/sync-peer/sync-peer-constants.mjs +2 -0
  46. package/dist/sync/sync-peer/sync-peer-constants.mjs.map +1 -0
  47. package/dist/sync/sync-peer/sync-peer-helpers.d.mts +2 -0
  48. package/dist/sync/sync-peer/sync-peer-helpers.d.mts.map +1 -0
  49. package/dist/sync/sync-peer/sync-peer-helpers.mjs +2 -0
  50. package/dist/sync/sync-peer/sync-peer-helpers.mjs.map +1 -0
  51. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.d.mts +8 -0
  52. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.d.mts.map +1 -0
  53. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.mjs +8 -0
  54. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.mjs.map +1 -0
  55. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.d.mts +18 -0
  56. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.d.mts.map +1 -0
  57. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.mjs +54 -0
  58. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.mjs.map +1 -0
  59. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.d.mts +80 -0
  60. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.d.mts.map +1 -0
  61. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.mjs +5 -0
  62. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.mjs.map +1 -0
  63. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.d.mts +43 -0
  64. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.d.mts.map +1 -0
  65. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mjs +229 -0
  66. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mjs.map +1 -0
  67. package/dist/sync/sync-peer/sync-peer-types.d.mts +12 -0
  68. package/dist/sync/sync-peer/sync-peer-types.d.mts.map +1 -1
  69. package/dist/sync/sync-peer/sync-peer-v1.d.mts +15 -7
  70. package/dist/sync/sync-peer/sync-peer-v1.d.mts.map +1 -1
  71. package/dist/sync/sync-peer/sync-peer-v1.mjs +105 -24
  72. package/dist/sync/sync-peer/sync-peer-v1.mjs.map +1 -1
  73. package/dist/sync/sync-saga-context/sync-saga-context-helpers.d.mts +5 -8
  74. package/dist/sync/sync-saga-context/sync-saga-context-helpers.d.mts.map +1 -1
  75. package/dist/sync/sync-saga-context/sync-saga-context-helpers.mjs +34 -18
  76. package/dist/sync/sync-saga-context/sync-saga-context-helpers.mjs.map +1 -1
  77. package/dist/sync/sync-saga-context/sync-saga-context-types.d.mts +26 -22
  78. package/dist/sync/sync-saga-context/sync-saga-context-types.d.mts.map +1 -1
  79. package/dist/sync/sync-saga-context/sync-saga-context-types.mjs +1 -9
  80. package/dist/sync/sync-saga-context/sync-saga-context-types.mjs.map +1 -1
  81. package/dist/sync/sync-saga-coordinator.d.mts +48 -51
  82. package/dist/sync/sync-saga-coordinator.d.mts.map +1 -1
  83. package/dist/sync/sync-saga-coordinator.mjs +720 -420
  84. package/dist/sync/sync-saga-coordinator.mjs.map +1 -1
  85. package/dist/sync/sync-saga-message/sync-saga-message-helpers.d.mts.map +1 -1
  86. package/dist/sync/sync-saga-message/sync-saga-message-helpers.mjs +1 -0
  87. package/dist/sync/sync-saga-message/sync-saga-message-helpers.mjs.map +1 -1
  88. package/dist/sync/sync-saga-message/sync-saga-message-types.d.mts +1 -12
  89. package/dist/sync/sync-saga-message/sync-saga-message-types.d.mts.map +1 -1
  90. package/dist/sync/sync-types.d.mts +15 -3
  91. package/dist/sync/sync-types.d.mts.map +1 -1
  92. package/dist/witness/light-witness-base-v1.d.mts.map +1 -1
  93. package/dist/witness/light-witness-base-v1.mjs +2 -0
  94. package/dist/witness/light-witness-base-v1.mjs.map +1 -1
  95. package/dist/witness/space/inner-space/inner-space-v1.mjs +1 -1
  96. package/dist/witness/space/inner-space/inner-space-v1.mjs.map +1 -1
  97. package/ibgib-foundations.md +20 -2
  98. package/package.json +1 -1
  99. package/src/sync/README.md +31 -22
  100. package/src/sync/graft-info/graft-info-constants.mts +4 -0
  101. package/src/sync/graft-info/graft-info-helpers.mts +308 -0
  102. package/src/sync/graft-info/graft-info-helpers.respec.mts +83 -0
  103. package/src/sync/graft-info/graft-info-types.mts +33 -0
  104. package/src/sync/strategies/conflict-optimistic.mts +11 -70
  105. package/src/sync/sync-conflict.respec.mts +177 -37
  106. package/src/sync/sync-constants.mts +5 -9
  107. package/src/sync/sync-helpers.mts +11 -7
  108. package/src/sync/sync-innerspace-constants.respec.mts +5 -1
  109. package/src/sync/sync-innerspace-deep-updates.respec.mts +5 -1
  110. package/src/sync/sync-innerspace-dest-ahead.respec.mts +7 -3
  111. package/src/sync/sync-innerspace-multiple-timelines.respec.mts +5 -1
  112. package/src/sync/sync-innerspace-partial-update.respec.mts +8 -2
  113. package/src/sync/sync-innerspace.respec.mts +5 -1
  114. package/src/sync/sync-peer/sync-peer-constants.mts +0 -0
  115. package/src/sync/sync-peer/sync-peer-helpers.mts +0 -0
  116. package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.mts +8 -0
  117. package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.mts +72 -0
  118. package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.mts +87 -0
  119. package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mts +242 -0
  120. package/src/sync/sync-peer/sync-peer-types.mts +13 -1
  121. package/src/sync/sync-peer/sync-peer-v1.mts +93 -27
  122. package/src/sync/sync-saga-context/sync-saga-context-helpers.mts +47 -29
  123. package/src/sync/sync-saga-context/sync-saga-context-types.mts +29 -30
  124. package/src/sync/sync-saga-coordinator.mts +782 -441
  125. package/src/sync/sync-saga-message/sync-saga-message-helpers.mts +2 -0
  126. package/src/sync/sync-saga-message/sync-saga-message-types.mts +0 -11
  127. package/src/sync/sync-types.mts +17 -3
  128. package/src/witness/light-witness-base-v1.mts +2 -1
  129. package/src/witness/space/inner-space/inner-space-v1.mts +1 -1
  130. package/test_output.log +489 -0
  131. package/tmp.md +61 -2
  132. package/dist/sync/merge-info/merge-info-constants.d.mts +0 -2
  133. package/dist/sync/merge-info/merge-info-constants.d.mts.map +0 -1
  134. package/dist/sync/merge-info/merge-info-constants.mjs +0 -2
  135. package/dist/sync/merge-info/merge-info-constants.mjs.map +0 -1
  136. package/dist/sync/merge-info/merge-info-helpers.d.mts +0 -51
  137. package/dist/sync/merge-info/merge-info-helpers.d.mts.map +0 -1
  138. package/dist/sync/merge-info/merge-info-helpers.mjs +0 -92
  139. package/dist/sync/merge-info/merge-info-helpers.mjs.map +0 -1
  140. package/dist/sync/merge-info/merge-info-helpers.respec.d.mts +0 -2
  141. package/dist/sync/merge-info/merge-info-helpers.respec.d.mts.map +0 -1
  142. package/dist/sync/merge-info/merge-info-helpers.respec.mjs +0 -32
  143. package/dist/sync/merge-info/merge-info-helpers.respec.mjs.map +0 -1
  144. package/dist/sync/merge-info/merge-info-types.d.mts +0 -26
  145. package/dist/sync/merge-info/merge-info-types.mjs +0 -2
  146. package/dist/sync/merge-info/merge-info-types.mjs.map +0 -1
  147. package/dist/sync/sync-local-spaces.respec.d.mts +0 -2
  148. package/dist/sync/sync-local-spaces.respec.d.mts.map +0 -1
  149. package/dist/sync/sync-local-spaces.respec.mjs +0 -159
  150. package/dist/sync/sync-local-spaces.respec.mjs.map +0 -1
  151. package/dist/sync/sync-peer/sync-peer-innerspace-v1.d.mts +0 -39
  152. package/dist/sync/sync-peer/sync-peer-innerspace-v1.d.mts.map +0 -1
  153. package/dist/sync/sync-peer/sync-peer-innerspace-v1.mjs +0 -131
  154. package/dist/sync/sync-peer/sync-peer-innerspace-v1.mjs.map +0 -1
  155. package/dist/sync/sync-saga-coordinator.respec.d.mts +0 -2
  156. package/dist/sync/sync-saga-coordinator.respec.d.mts.map +0 -1
  157. package/dist/sync/sync-saga-coordinator.respec.mjs +0 -40
  158. package/dist/sync/sync-saga-coordinator.respec.mjs.map +0 -1
  159. package/src/sync/merge-info/merge-info-constants.mts +0 -1
  160. package/src/sync/merge-info/merge-info-helpers.mts +0 -134
  161. package/src/sync/merge-info/merge-info-helpers.respec.mts +0 -41
  162. package/src/sync/merge-info/merge-info-types.mts +0 -28
  163. package/src/sync/sync-local-spaces.respec.mts +0 -200
  164. package/src/sync/sync-peer/sync-peer-innerspace-v1.mts +0 -167
  165. package/src/sync/sync-saga-coordinator.respec.mts +0 -52
@@ -8,27 +8,36 @@ The synchronization process is modeled as a "Saga" — a linear but distributed
8
8
 
9
9
  * **[SyncSagaIbGib_V1](file:./sync-types.mts)**: The timeline that encapsulates the entire sync process. This acts as the **shared audit trail**. By the end of a successful sync, both the Local and Remote nodes will have an identical copy of this saga timeline, proving exactly what was exchanged and when.
10
10
  * **[SyncSagaCoordinator](file:./sync-saga-coordinator.mts)**: The "Driver" (State Machine) that manages the saga. It is symmetric: both Alice (Source) and Bob (Destination) use the exact same coordinator logic to evolve the saga frame.
11
- * **[SyncPeerWitness](file:./sync-peer/sync-peer-types.mts)**: The interface for the "Transport Layer". It abstracts the communication channel (Method Call, HTTP, WebSocket) into a standard Witness pattern.
11
+ * **[SyncPeerWitness](file:./sync-peer/sync-peer-types.mts)**: The interface for the "Transport Layer". It abstracts the communication channel (Method Call, HTTP, WebSocket) into a simple Witness pattern: `(SyncSagaContextIbGib_V1) => Promise<SyncSagaContextIbGib_V1>` ("ping-pong" exchange).
12
12
  * **[SyncPeer_V1](file:./sync-peer/sync-peer-v1.mts)** provides the base plumbing.
13
13
  * Concrete implementations (e.g., in-memory) handle the actual byte transfer.
14
- * **[SyncSagaContextIbGib_V1](file:./sync-saga-context/sync-saga-context-types.mts)**: The "Transport Envelope". This is the payload passed to the Witness. It contains the current Saga Frame, the Message Stones (Data), and any cryptographic proofs required for the step.## Conflict Resolution & Protocol Refinements
15
-
16
- ### Unified Ack Protocol
17
- To optimize the handshake, the `Ack` frame now carries both synchronization data (requests/offers) AND conflict information.
18
- * **Conflicts**: If the Receiver detects divergence (multiple timeline tips), it includes a `conflicts` array in the `Ack`.
19
- * **Structure**: Each conflict includes the `tjpAddr`, `localAddr` (Receiver's Tip), `remoteAddr` (Sender's Tip), and crucially `timelineAddrs` (History).
20
-
21
- ### Iterative Delta Protocol (Ping-Pong)
22
- The `Delta` phase is no longer a single push. It is an iterative negotiation:
23
- 1. **Symmetrical**: Both Sender and Receiver can send `Delta` frames.
24
- 2. **Payloads**: Frames carry `payloadAddrs` (manifest) and the actual data is persisted to the peer's space.
25
- 3. **Requests**: A Delta can include `requests` (e.g., asking for missing conflict history).
26
- 4. **Merge**: If a peer resolves a conflict locally (using `optimistic` strategies), the resulting **Merge Frame** is added to the outgoing payload.
27
- 5. **Completion**: A `Delta` frame includes a `proposeCommit` flag. If one peer sets this and the other has nothing to add, the session transitions to `Commit`.
28
-
29
- ### Commit Phase
30
- * **Persistence**: The `Commit` frame signals success.
31
- * **Cleanup**: The `SyncPeer` is responsible for moving verified `ibGibs` from the temporary saga space to the persistent domain space.
14
+ * **[SyncSagaContextIbGib_V1](file:./sync-saga-context/sync-saga-context-types.mts)**: The "Transport Envelope" passed to the `SyncPeer_V1`.
15
+ * Contains the current Saga Frame, the Message Stone(s) (Data), and any cryptographic proofs required for the step.
16
+
17
+ ## Workflow
18
+
19
+ Init -> Ack -> Delta(s) -> Conflict/Commit via "ping pong" exchange.
20
+
21
+ 1. **Init**: Sender initiates sync.
22
+ * Creates the `SyncSagaIbGib_V1` with an `Init` msg frame containing knowledge about domain ibgibs to sync.
23
+ * Optionally creates session keystone based off of Sender's primary keystone.
24
+ * Wraps the saga ibgib in a `SyncSagaContextIbGib_V1` and passes to the `SyncPeer_V1`.
25
+ 2. **Ack**: Receiver gets incoming `SyncSagaContextIbGib_V1` and passes to its `SyncSagaCoordinator`.
26
+ * Handles the `Init` frame, comparing knowledge with Receiver's own status of each timeline/stone in domain ibgibs.
27
+ * Creates an `Ack` frame, evolves the `SyncSagaIbGib_V1` with this, wraps in `SyncSagaContextIbGib_V1` and returns to Sender.
28
+ * The `Ack` frame carries both synchronization data (requests/offers) AND conflict information.
29
+ * If the Receiver detects divergence (timeline tips differ and receiver doesn't recognize Sender's tip), it includes a `conflicts` array.
30
+ * Each conflict includes the `tjpAddr`, `localAddr` (Receiver's Tip), `remoteAddr` (Sender's Tip), and crucially `timelineAddrs` (History).
31
+ 3. **Delta**: Sender now has enough information to send `Delta` frame if needed.
32
+ * Both Sender and Receiver can send `Delta` frames.
33
+ * `Delta` frames soft link to `payloadAddrsControl` and `payloadAddrsDomain` and act as a manifest, while the actual data transmitted happens according to the `SyncPeer_V1` implementation.
34
+ * `SyncPeer_V1.payloadIbGibsDomainReceived$` is an observable for the actual domain payload ibgibs transmitted.
35
+ * So the a node sends domain payloads out in bulk to the `SyncPeer_V1`, receives the return saga ibgib with a manifest, but may have to wait for the observable to complete to actually process the ibgibs.
36
+ * `Delta` frame can include `requests` (e.g., asking for missing conflict history).
37
+ * Once no deltas required, `Delta` frame sets `proposeCommit` flag. If the other has nothing to add, the session transitions to `Commit`.
38
+ 4. **Commit**: When receive `Delta` frame with `proposeCommit`, should trigger the receiver of this to start its commit. If that is successful, it should return a `Commit` frame, which signals the commit was successful on their end. When this `Commit` frame is received, this end should not perform its own commit.
39
+ * For local-only syncs, the `SyncPeer_V1` implementation is responsible for moving verified `ibGibs` from the temporary saga space to the durable/persistent domain space.
40
+ * From each endpoint's POV, the cleanup should remove the temp spaces used, in addition to any other required transaction cleanup (like closing any connections).
32
41
 
33
42
  ### Ephemeral Identity (`useSessionIdentity`)
34
43
  The sync protocol can optionally generate an ephemeral Keystone Identity for the duration of the saga.
@@ -42,9 +51,9 @@ The sync protocol can optionally generate an ephemeral Keystone Identity for the
42
51
 
43
52
  ## Features
44
53
  * **Symmetric Design**: No distinct "Client" or "Server" logic.
45
- * **Smart Diff**: Efficient "Knowledge Vector" exchange to transfer only missing deltas.
46
- * **Constants & Timelines**: Support for syncing both mutable histories and immutable stones.
47
- * **Optimistic Concurrency**: (In Progress) Branch detection and merge strategies.
54
+ * **Smart Diff**: Efficient "Knowledge Vector" exchange to transfer only missing deltas (though some inefficiencies may exist early on).
55
+ * **Timelines && Constants**: Support for syncing both mutable histories and immutable constant ibgibs.
56
+ * **Optimistic Conflict Resolution**: (In Progress) Branch detection and merge strategies.
48
57
 
49
58
  ## Verification
50
59
  We rely on rigorous In-Memory Simulation (`InnerSpace`) to verify complex graph scenarios. See **[Verification](./docs/verification.md)** for the full status.
@@ -0,0 +1,4 @@
1
+ export const GRAFT_INFO_ATOM = 'graft_info';
2
+ export const GRAFT_INFO_REL8N_NAME = 'graftinfo';
3
+ export const GRAFT_BASE_REL8N_NAME = 'graftbase';
4
+ export const GRAFT_ORPHAN_REL8N_NAME = 'graftorphan';
@@ -0,0 +1,308 @@
1
+ import { Factory_V1 } from "@ibgib/ts-gib/dist/V1/factory.mjs";
2
+ import { IbGib_V1 } from "@ibgib/ts-gib/dist/V1/types.mjs";
3
+ import { getIbGibAddr } from "@ibgib/ts-gib/dist/helper.mjs";
4
+ import { extractErrorMsg } from "@ibgib/helper-gib/dist/helpers/utils-helper.mjs";
5
+ import { rel8 } from "@ibgib/ts-gib/dist/V1/transforms/rel8.mjs";
6
+
7
+ import { IbGibSpaceAny } from "../../witness/space/space-base-v1.mjs";
8
+ import { getFromSpace, putInSpace } from "../../witness/space/space-helper.mjs";
9
+ import { applyTransforms } from "../../witness/space/reconciliation-space/reconciliation-space-helper.mjs";
10
+ import {
11
+ GRAFT_INFO_ATOM,
12
+ GRAFT_BASE_REL8N_NAME,
13
+ GRAFT_ORPHAN_REL8N_NAME,
14
+ GRAFT_INFO_REL8N_NAME
15
+ } from "./graft-info-constants.mjs";
16
+ import { GraftInfoData_V1, GraftInfoIbGib_V1, GraftInfoIb_V1 } from "./graft-info-types.mjs";
17
+
18
+ const lc = `[graft-info-helpers]`;
19
+
20
+ /**
21
+ * Validates and constructs the `ib` object for a Graft Info ibGib.
22
+ */
23
+ export async function getGraftInfoIb({
24
+ data
25
+ }: {
26
+ data: GraftInfoData_V1
27
+ }): Promise<string> {
28
+ if (!data.algo) { throw new Error(`(UNEXPECTED) data.algo required for GraftInfoIb (E: 8a9b0c1d2e3f4g5h)`); }
29
+
30
+ // graft_info algo
31
+ return [GRAFT_INFO_ATOM, data.algo].join(' ');
32
+ }
33
+
34
+ /**
35
+ * Parses a standard Graft Info 'ib' string.
36
+ */
37
+ export async function parseGraftInfoIb({
38
+ ib
39
+ }: {
40
+ ib: string
41
+ }): Promise<GraftInfoIb_V1> {
42
+ try {
43
+ const parts = ib.split(' ');
44
+ if (parts[0] !== GRAFT_INFO_ATOM) { throw new Error(`Atom mismatch. Expected ${GRAFT_INFO_ATOM} (E: 8f03c92a95144b618821915632599266)`); }
45
+ if (parts.length < 2) { throw new Error(`Invalid graft info ib. Expected at least 2 parts. (E: 9c2b4c8a6d34469f8263544710183355)`); }
46
+
47
+ const algo = parts.slice(1).join(' '); // allow spaces in algo? usually single word but safe to join.
48
+
49
+ return {
50
+ atom: GRAFT_INFO_ATOM,
51
+ algo,
52
+ };
53
+ } catch (error) {
54
+ console.error(`${lc} ${extractErrorMsg(error)}`);
55
+ throw error;
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Creates a new GraftInfo ibGib.
61
+ */
62
+ export async function createGraftInfo({
63
+ algo,
64
+ details,
65
+ }: {
66
+ algo: string,
67
+ details?: string,
68
+ }): Promise<GraftInfoIbGib_V1> {
69
+ const data: GraftInfoData_V1 = {
70
+ algo,
71
+ details,
72
+ };
73
+
74
+ const ib = await getGraftInfoIb({ data });
75
+
76
+ const res = await Factory_V1.stone({
77
+ ib,
78
+ data,
79
+ rel8ns: {}, // No default rel8ns (no 'ancestors' anymore)
80
+ parentPrimitiveIb: GRAFT_INFO_ATOM,
81
+ uuid: true,
82
+ });
83
+
84
+ return res as GraftInfoIbGib_V1;
85
+ }
86
+
87
+ /**
88
+ * Naive line-by-line text merge using LCS.
89
+ * Renamed to mergeTextLCS for clarity, but logic is generic LCS.
90
+ */
91
+ export async function mergeTextLCS({
92
+ textA,
93
+ textB,
94
+ }: {
95
+ textA: string,
96
+ textB: string,
97
+ }): Promise<string> {
98
+ if (textA === textB) { return textA; }
99
+
100
+ const linesA = textA.split('\n');
101
+ const linesB = textB.split('\n');
102
+
103
+ const common = longestCommonSubsequence(linesA, linesB); // Array of { line, indexA, indexB }
104
+
105
+ let resultLines: string[] = [];
106
+ let currentA = 0;
107
+ let currentB = 0;
108
+
109
+ for (const match of common) {
110
+ // Add unique lines from A (before this match)
111
+ while (currentA < match.indexA) {
112
+ resultLines.push(linesA[currentA]);
113
+ currentA++;
114
+ }
115
+ // Add unique lines from B (before this match)
116
+ while (currentB < match.indexB) {
117
+ resultLines.push(linesB[currentB]);
118
+ currentB++;
119
+ }
120
+ // Add the common line
121
+ resultLines.push(match.line);
122
+ currentA++;
123
+ currentB++;
124
+ }
125
+
126
+ // Add remaining unique lines from A
127
+ while (currentA < linesA.length) {
128
+ resultLines.push(linesA[currentA]);
129
+ currentA++;
130
+ }
131
+ // Add remaining unique lines from B
132
+ while (currentB < linesB.length) {
133
+ resultLines.push(linesB[currentB]);
134
+ currentB++;
135
+ }
136
+
137
+ return resultLines.join('\n');
138
+ }
139
+
140
+ interface LcsMatch {
141
+ line: string;
142
+ indexA: number;
143
+ indexB: number;
144
+ }
145
+
146
+ function longestCommonSubsequence(a: string[], b: string[]): LcsMatch[] {
147
+ const m = a.length;
148
+ const n = b.length;
149
+ const dp: number[][] = Array(m + 1).fill(0).map(() => Array(n + 1).fill(0));
150
+
151
+ // Fill DP table
152
+ for (let i = 1; i <= m; i++) {
153
+ for (let j = 1; j <= n; j++) {
154
+ if (a[i - 1] === b[j - 1]) {
155
+ dp[i][j] = dp[i - 1][j - 1] + 1;
156
+ } else {
157
+ dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
158
+ }
159
+ }
160
+ }
161
+
162
+ // Backtrack to find LCS
163
+ let i = m, j = n;
164
+ const lcs: LcsMatch[] = [];
165
+ while (i > 0 && j > 0) {
166
+ if (a[i - 1] === b[j - 1]) {
167
+ lcs.unshift({ line: a[i - 1], indexA: i - 1, indexB: j - 1 });
168
+ i--;
169
+ j--;
170
+ } else if (dp[i - 1][j] > dp[i][j - 1]) {
171
+ i--;
172
+ } else {
173
+ j--;
174
+ }
175
+ }
176
+ return lcs;
177
+ }
178
+
179
+ /**
180
+ * Grafts two divergent timelines by splicing missing DNA from one branch (Orphan) onto another (Base).
181
+ *
182
+ * Logic:
183
+ * 1. Checks timestamps of tips. Earlier = Base, Later = Orphan.
184
+ * 2. Fetches DNA from Orphan's branch (since LCA).
185
+ * 3. Replays Orphan's DNA onto Base.
186
+ * 4. Creates GraftInfo linking Base and Orphan.
187
+ * 5. Rel8s the Result to GraftInfo.
188
+ */
189
+ export async function graftTimelines({
190
+ tipA,
191
+ branchA,
192
+ tipB,
193
+ branchB,
194
+ space,
195
+ metaspace,
196
+ }: {
197
+ tipA: IbGib_V1,
198
+ branchA: IbGib_V1[],
199
+ tipB: IbGib_V1,
200
+ branchB: IbGib_V1[],
201
+ space: IbGibSpaceAny,
202
+ metaspace: any,
203
+ }): Promise<IbGib_V1> {
204
+ const lc = `[${graftTimelines.name}]`;
205
+ console.log(`${lc} [TEST DEBUG] starting... Metaspace present: ${!!metaspace}`);
206
+
207
+ // 1. Determine Base and Orphan based on timestamp
208
+ let baseTip: IbGib_V1;
209
+ let orphanTip: IbGib_V1; // The one providing the DNA to be replayed
210
+ let orphanBranch: IbGib_V1[];
211
+
212
+ const getTs = (ibGib: IbGib_V1) => {
213
+ const ms = ibGib.data?.timestampMs ?? 0;
214
+ if (!ms && ibGib.data?.timestamp) { return new Date(ibGib.data.timestamp).getTime(); }
215
+ return ms;
216
+ }
217
+
218
+ const tsA = getTs(tipA);
219
+ const tsB = getTs(tipB);
220
+
221
+ if (tsA <= tsB) {
222
+ // A is earlier (or equal), use A as base
223
+ baseTip = tipA;
224
+ orphanTip = tipB;
225
+ orphanBranch = branchB;
226
+ } else {
227
+ // B is earlier, use B as base
228
+ baseTip = tipB;
229
+ orphanTip = tipA;
230
+ orphanBranch = branchA;
231
+ }
232
+
233
+ const baseAddr = getIbGibAddr({ ibGib: baseTip });
234
+ const orphanAddr = getIbGibAddr({ ibGib: orphanTip });
235
+
236
+ // 2. Gather DNA addresses from the Orphan branch
237
+ const dnaAddrsToApply: string[] = [];
238
+ for (const ibGib of orphanBranch) {
239
+ const dnas = ibGib.rel8ns?.dna || [];
240
+ dnaAddrsToApply.push(...dnas);
241
+ }
242
+
243
+ if (dnaAddrsToApply.length === 0) {
244
+ return baseTip;
245
+ }
246
+
247
+ // 3. Fetch the DNA stones
248
+ const dnaRes = await getFromSpace({ space, addrs: dnaAddrsToApply });
249
+ if (!dnaRes.success || !dnaRes.ibGibs) {
250
+ throw new Error(`Failed to fetch DNA stones for replay. (E: 3a2b1c4d5e6f7g8h)`);
251
+ }
252
+ const allDnaIbGibs = dnaRes.ibGibs;
253
+
254
+ // 4. Replay Transforms onto Base
255
+ const createdIbGibs_Running: IbGib_V1[] = [];
256
+
257
+ const mergedTip = await applyTransforms({
258
+ src: baseTip,
259
+ dnaAddrsToApplyToStoreVersion: [...dnaAddrsToApply],
260
+ allLocalIbGibs: allDnaIbGibs,
261
+ createdIbGibs_Running,
262
+ });
263
+
264
+ // 5. Persist the new stones
265
+ await putInSpace({ space, ibGibs: createdIbGibs_Running });
266
+
267
+ // 6. Create Graft Info
268
+ const graftInfo = await createGraftInfo({
269
+ algo: 'optimistic_dna_replay',
270
+ details: `Replayed ${dnaAddrsToApply.length} transforms from orphan (${orphanAddr}) onto base (${baseAddr}).`,
271
+ });
272
+
273
+ // Add relations via rel8
274
+ const graftInfoRel8nsToAdd = {
275
+ [GRAFT_BASE_REL8N_NAME]: [baseAddr],
276
+ [GRAFT_ORPHAN_REL8N_NAME]: [orphanAddr],
277
+ };
278
+
279
+ const { newIbGib: graftInfoWithRel8ns } = await rel8({
280
+ src: graftInfo,
281
+ rel8nsToAddByAddr: graftInfoRel8nsToAdd,
282
+ dna: true,
283
+ });
284
+ // Persist this update
285
+ await putInSpace({ space, ibGibs: [graftInfoWithRel8ns] });
286
+
287
+ // 7. Link GraftInfo to Result via 'graftinfo' rel8n
288
+ const graftInfoAddr = getIbGibAddr({ ibGib: graftInfoWithRel8ns });
289
+ const { newIbGib: finalIbGib, dnas: rel8Dnas, intermediateIbGibs } = await rel8({
290
+ src: mergedTip,
291
+ rel8nsToAddByAddr: {
292
+ [GRAFT_INFO_REL8N_NAME]: [graftInfoAddr],
293
+ },
294
+ dna: true, // Auto-link 'dna'
295
+ nCounter: true,
296
+ });
297
+
298
+ // Persist final result
299
+ const finalStones = [finalIbGib, ...(rel8Dnas || []), ...(intermediateIbGibs || [])];
300
+ await putInSpace({ space, ibGibs: finalStones });
301
+
302
+ // 8. Register New Tip in Metaspace (CRITICAL for updating Timeline Index)
303
+ if (metaspace && metaspace.registerNewIbGib) {
304
+ await metaspace.registerNewIbGib({ ibGib: finalIbGib, space });
305
+ }
306
+
307
+ return finalIbGib;
308
+ }
@@ -0,0 +1,83 @@
1
+ import { iReckon, respecfully, ifWe, ifWeMight } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs';
2
+ import { mergeTextLCS } from './graft-info-helpers.mjs';
3
+
4
+ const maam = `[${import.meta.url}]`;
5
+ const sir = maam;
6
+
7
+ await respecfully(sir, 'Graft Info Helpers', async () => {
8
+
9
+ await respecfully(sir, 'mergeTextLCS', async () => {
10
+
11
+ await ifWe(sir, 'mergeTextLCS should interleave unique lines (A then B)', async () => {
12
+ const textA = "Line 1\nLine A\nLine 3";
13
+ const textB = "Line 1\nLine B\nLine 3";
14
+ const res = await mergeTextLCS({ textA, textB });
15
+ const expected = "Line 1\nLine A\nLine B\nLine 3";
16
+ iReckon(sir, res).asTo('result').isGonnaBe(expected);
17
+ });
18
+
19
+ await ifWe(sir, 'mergeTextLCS should handle insertions', async () => {
20
+ const textA = "Line 1\nLine 3";
21
+ const textB = "Line 1\nLine 2\nLine 3";
22
+ const res = await mergeTextLCS({ textA, textB });
23
+ const expected = "Line 1\nLine 2\nLine 3";
24
+ iReckon(sir, res).asTo('result').isGonnaBe(expected);
25
+ });
26
+
27
+ await ifWe(sir, 'mergeTextLCS should handle appends', async () => {
28
+ const textA = "Line 1";
29
+ const textB = "Line 1\nLine 2";
30
+ const res = await mergeTextLCS({ textA, textB });
31
+ const expected = "Line 1\nLine 2";
32
+ iReckon(sir, res).asTo('result').isGonnaBe(expected);
33
+ });
34
+
35
+ await ifWe(sir, 'mergeTextLCS should handle simultaneous list additions', async () => {
36
+ const textA = "- Item 1\n- Item 2\n- Item A";
37
+ const textB = "- Item 1\n- Item 2\n- Item B";
38
+ const res = await mergeTextLCS({ textA, textB });
39
+
40
+ // Expected: Common (1, 2) then A then B.
41
+ const expected = "- Item 1\n- Item 2\n- Item A\n- Item B";
42
+ iReckon(sir, res).asTo('result').isGonnaBe(expected);
43
+ });
44
+
45
+ await ifWe(sir, 'mergeTextLCS should handle distinct modifications in code block', async () => {
46
+ const textA = `function foo() {
47
+ console.log("start");
48
+ doA();
49
+ console.log("end");
50
+ }`;
51
+ const textB = `function foo() {
52
+ console.log("start");
53
+ doB();
54
+ console.log("end");
55
+ }`;
56
+ const res = await mergeTextLCS({ textA, textB });
57
+
58
+ const expected = `function foo() {
59
+ console.log("start");
60
+ doA();
61
+ doB();
62
+ console.log("end");
63
+ }`;
64
+ iReckon(sir, res).asTo('result').isGonnaBe(expected);
65
+ });
66
+
67
+ await ifWe(sir, 'mergeTextLCS should return same string if identical', async () => {
68
+ const textA = "Hello";
69
+ const res = await mergeTextLCS({ textA, textB: textA });
70
+ iReckon(sir, res).asTo('result').isGonnaBe(textA);
71
+ });
72
+
73
+ });
74
+
75
+ await respecfully(sir, 'graftTimelines', async () => {
76
+ // We need a more complex setup to test this properly (mock space, etc.)
77
+ // For now, this placeholder ensures the test file runs.
78
+ await ifWe(sir, 'graftTimelines placeholders', async () => {
79
+ iReckon(sir, true).isGonnaBeTrue();
80
+ });
81
+ });
82
+
83
+ });
@@ -0,0 +1,33 @@
1
+ import { IbGib_V1, IbGibData_V1, IbGibRel8ns_V1 } from "@ibgib/ts-gib/dist/V1/types.mjs";
2
+ import { GRAFT_INFO_ATOM } from "./graft-info-constants.mjs";
3
+
4
+ export interface GraftInfoIb_V1 {
5
+ atom: typeof GRAFT_INFO_ATOM;
6
+ algo: string; // e.g. "optimistic_dna_replay", "text_lcs", "manual"
7
+ }
8
+
9
+ export interface GraftInfoData_V1 extends IbGibData_V1 {
10
+ /**
11
+ * The algorithm or strategy used to perform the graft.
12
+ */
13
+ algo: string;
14
+ /**
15
+ * Optional details about the graft, e.g. validation results or specifics about conflicts resolved.
16
+ */
17
+ details?: string;
18
+ }
19
+
20
+ export interface GraftInfoRel8ns_V1 extends IbGibRel8ns_V1 {
21
+ /**
22
+ * The 'base' of the graft (the earlier tip, usually).
23
+ * This is the timeline we are grafting ONTO.
24
+ */
25
+ graftbase?: string[];
26
+ /**
27
+ * The 'orphan' tip (the later tip, usually) effectively grafted into the base.
28
+ * This is the branch that ends here, its DNA spliced into the base.
29
+ */
30
+ graftorphan?: string[];
31
+ }
32
+
33
+ export interface GraftInfoIbGib_V1 extends IbGib_V1<GraftInfoData_V1, GraftInfoRel8ns_V1> { }
@@ -113,8 +113,10 @@ export async function findLCA({
113
113
  return { lcaAddr, branchA, branchB };
114
114
  }
115
115
 
116
+ import { graftTimelines, mergeTextLCS } from '../graft-info/graft-info-helpers.mjs';
117
+
116
118
  /**
117
- * Optimistically merges two divergent timelines by inspecting DNA.
119
+ * Optimistically grafts two divergent timelines by inspecting DNA and replaying transforms.
118
120
  */
119
121
  export async function mergeDivergentTimelines({
120
122
  tipA,
@@ -127,82 +129,21 @@ export async function mergeDivergentTimelines({
127
129
  space: IbGibSpaceAny,
128
130
  metaspace: any, // MetaspaceService type
129
131
  }): Promise<IbGib_V1> {
132
+ const lc = `[${mergeDivergentTimelines.name}]`;
130
133
 
131
134
  // 1. Find LCA & Branches
132
135
  const { lcaAddr, branchA, branchB } = await findLCA({ tipA, tipB, space });
133
136
 
134
- // 2. DNA Analysis & Reducing
135
- // We need to accumulate the "changes" from A and "changes" from B.
136
- // For V1 simple objects, this means extracting the `dataToAddOrPatch` from each DNA transform.
137
-
138
- const extractChanges = async (branch: IbGib_V1[]): Promise<any> => {
139
- let accumulatedData = {};
140
- for (const ibGib of branch) {
141
- // Check DNA
142
- const dnaRel = ibGib.rel8ns?.dna;
143
- if (!dnaRel || dnaRel.length === 0) continue;
144
-
145
- // Load DNA (usually optimized to be in-memory if we just loaded the timeline?
146
- // no, DNA is a separate stone. we might need to fetch it.)
147
- const dnaRes = await getFromSpace({ space, addrs: dnaRel });
148
- const dnaIbGibs = dnaRes.ibGibs || [];
149
-
150
- for (const dna of dnaIbGibs) {
151
- // Inspect DNA data.
152
- // We expect 'mut8' transform DNA.
153
- // The DNA *data* contains the args used in the transform.
154
- if (dna.data && (dna.data as any).dataToAddOrPatch) {
155
- Object.assign(accumulatedData, (dna.data as any).dataToAddOrPatch);
156
- }
157
- }
158
- }
159
- return accumulatedData;
160
- };
161
-
162
- const changesA = await extractChanges(branchA);
163
- const changesB = await extractChanges(branchB);
164
-
165
- // 3. Conflict Resolution Strategy (Optimistic: Merge All)
166
- // Conflict collision: If A and B changed the SAME field.
167
- // Strategy: Last Write Wins? Or deterministic tie-break?
168
- // Let's default to: Apply A, then Apply B. B wins collisions.
169
- // (Ideally we flag this for user review, but "Optimistic" means we presume it's fine).
170
-
171
- const mergedChanges = { ...changesA, ...changesB };
172
-
173
- // 4. Create Merge Transform
174
- // We create a NEW mut8 on top of tipA (or tipB).
175
- // Let's pick tipA as the "base" to apply the merge to.
176
- // But we must also link tipB as an ancestor?
177
- // Standard mut8Timeline only supports linear ancestry.
178
- // We need a "Merge" transform that rel8s both.
179
-
180
- // Use mut8Timeline on tipA, applying the *missing* changes from B,
181
- // AND explicitly adding tipB to the 'ancestor' (or 'merge_ancestor'?) rel8n.
182
-
183
- // Actually, simply applying 'mergedChanges' to tipA might be redundant for the parts A already did.
184
- // We only need to apply 'changesB' to tipA?
185
- // Wait, if B changed field-X to 'foo' and A touched field-Y.
186
- // We apply changesB (foo) to tipA. Result has both.
187
- // What if B changed field-X to 'foo' and A changed field-X to 'bar'.
188
- // changesB overwrites. Result is 'foo'.
189
-
190
- // So: Apply changesB to tipA.
191
-
192
- const result = await mut8Timeline({
193
- timeline: tipA,
194
- mut8Opts: {
195
- dataToAddOrPatch: changesB, // Apply B's changes onto A
196
- // We want to record that B is also a parent.
197
- // mut8Timeline might not expose 'otherAncestors'.
198
- // required to verify this API capabilities.
199
- },
137
+ // 2. Delegate to Graft Engine
138
+ // The graft engine handles timestamp verification, DNA fetching, replay, and Stone creation.
139
+ const result = await graftTimelines({
140
+ tipA,
141
+ branchA,
142
+ tipB,
143
+ branchB,
200
144
  space,
201
145
  metaspace,
202
146
  });
203
147
 
204
- // Ideally we'd modify the result to add 'past' link to tipB.
205
- // But for now, let's just achieve data convergence.
206
-
207
148
  return result;
208
149
  }