@ibgib/core-gib 0.1.18 → 0.1.20

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 (83) hide show
  1. package/dist/common/other/ibgib-helper.d.mts +13 -0
  2. package/dist/common/other/ibgib-helper.d.mts.map +1 -1
  3. package/dist/common/other/ibgib-helper.mjs +44 -0
  4. package/dist/common/other/ibgib-helper.mjs.map +1 -1
  5. package/dist/sync/merge-info/merge-info-constants.d.mts +2 -0
  6. package/dist/sync/merge-info/merge-info-constants.d.mts.map +1 -0
  7. package/dist/sync/merge-info/merge-info-constants.mjs +2 -0
  8. package/dist/sync/merge-info/merge-info-constants.mjs.map +1 -0
  9. package/dist/sync/merge-info/merge-info-helpers.d.mts +51 -0
  10. package/dist/sync/merge-info/merge-info-helpers.d.mts.map +1 -0
  11. package/dist/sync/merge-info/merge-info-helpers.mjs +92 -0
  12. package/dist/sync/merge-info/merge-info-helpers.mjs.map +1 -0
  13. package/dist/sync/merge-info/merge-info-helpers.respec.d.mts +2 -0
  14. package/dist/sync/merge-info/merge-info-helpers.respec.d.mts.map +1 -0
  15. package/dist/sync/merge-info/merge-info-helpers.respec.mjs +32 -0
  16. package/dist/sync/merge-info/merge-info-helpers.respec.mjs.map +1 -0
  17. package/dist/sync/merge-info/merge-info-types.d.mts +26 -0
  18. package/dist/sync/merge-info/merge-info-types.d.mts.map +1 -0
  19. package/dist/sync/merge-info/merge-info-types.mjs +2 -0
  20. package/dist/sync/merge-info/merge-info-types.mjs.map +1 -0
  21. package/dist/sync/strategies/conflict-optimistic.d.mts +37 -0
  22. package/dist/sync/strategies/conflict-optimistic.d.mts.map +1 -0
  23. package/dist/sync/strategies/conflict-optimistic.mjs +162 -0
  24. package/dist/sync/strategies/conflict-optimistic.mjs.map +1 -0
  25. package/dist/sync/sync-conflict.respec.d.mts +8 -0
  26. package/dist/sync/sync-conflict.respec.d.mts.map +1 -0
  27. package/dist/sync/sync-conflict.respec.mjs +158 -0
  28. package/dist/sync/sync-conflict.respec.mjs.map +1 -0
  29. package/dist/sync/sync-innerspace-constants.respec.d.mts +8 -0
  30. package/dist/sync/sync-innerspace-constants.respec.d.mts.map +1 -0
  31. package/dist/sync/sync-innerspace-constants.respec.mjs +116 -0
  32. package/dist/sync/sync-innerspace-constants.respec.mjs.map +1 -0
  33. package/dist/sync/sync-innerspace-deep-updates.respec.mjs +2 -3
  34. package/dist/sync/sync-innerspace-deep-updates.respec.mjs.map +1 -1
  35. package/dist/sync/sync-innerspace-dest-ahead.respec.mjs +3 -3
  36. package/dist/sync/sync-innerspace-dest-ahead.respec.mjs.map +1 -1
  37. package/dist/sync/sync-innerspace-multiple-timelines.respec.mjs +2 -2
  38. package/dist/sync/sync-innerspace-multiple-timelines.respec.mjs.map +1 -1
  39. package/dist/sync/sync-innerspace-partial-update.respec.d.mts +7 -0
  40. package/dist/sync/sync-innerspace-partial-update.respec.d.mts.map +1 -0
  41. package/dist/sync/sync-innerspace-partial-update.respec.mjs +116 -0
  42. package/dist/sync/sync-innerspace-partial-update.respec.mjs.map +1 -0
  43. package/dist/sync/sync-innerspace.respec.mjs +5 -5
  44. package/dist/sync/sync-innerspace.respec.mjs.map +1 -1
  45. package/dist/sync/sync-saga-coordinator.d.mts +23 -12
  46. package/dist/sync/sync-saga-coordinator.d.mts.map +1 -1
  47. package/dist/sync/sync-saga-coordinator.mjs +612 -95
  48. package/dist/sync/sync-saga-coordinator.mjs.map +1 -1
  49. package/dist/sync/sync-saga-message/sync-saga-message-helpers.d.mts +11 -0
  50. package/dist/sync/sync-saga-message/sync-saga-message-helpers.d.mts.map +1 -1
  51. package/dist/sync/sync-saga-message/sync-saga-message-helpers.mjs +24 -0
  52. package/dist/sync/sync-saga-message/sync-saga-message-helpers.mjs.map +1 -1
  53. package/dist/sync/sync-saga-message/sync-saga-message-types.d.mts +30 -0
  54. package/dist/sync/sync-saga-message/sync-saga-message-types.d.mts.map +1 -1
  55. package/dist/sync/sync-types.d.mts +31 -4
  56. package/dist/sync/sync-types.d.mts.map +1 -1
  57. package/dist/sync/sync-types.mjs.map +1 -1
  58. package/ibgib-foundations.md +129 -0
  59. package/package.json +1 -1
  60. package/roadmap.md +59 -0
  61. package/src/common/other/ibgib-helper.mts +52 -0
  62. package/src/keystone/README.md +13 -155
  63. package/src/keystone/docs/architecture.md +55 -0
  64. package/src/sync/README.md +37 -42
  65. package/src/sync/docs/architecture.md +69 -0
  66. package/src/sync/docs/verification.md +43 -0
  67. package/src/sync/merge-info/merge-info-constants.mts +1 -0
  68. package/src/sync/merge-info/merge-info-helpers.mts +134 -0
  69. package/src/sync/merge-info/merge-info-helpers.respec.mts +41 -0
  70. package/src/sync/merge-info/merge-info-types.mts +28 -0
  71. package/src/sync/strategies/conflict-optimistic.mts +208 -0
  72. package/src/sync/sync-conflict.respec.mts +194 -0
  73. package/src/sync/sync-innerspace-constants.respec.mts +133 -0
  74. package/src/sync/sync-innerspace-deep-updates.respec.mts +1 -2
  75. package/src/sync/sync-innerspace-dest-ahead.respec.mts +2 -2
  76. package/src/sync/sync-innerspace-multiple-timelines.respec.mts +1 -1
  77. package/src/sync/sync-innerspace-partial-update.respec.mts +150 -0
  78. package/src/sync/sync-innerspace.respec.mts +4 -4
  79. package/src/sync/sync-saga-coordinator.mts +673 -103
  80. package/src/sync/sync-saga-message/sync-saga-message-helpers.mts +41 -0
  81. package/src/sync/sync-saga-message/sync-saga-message-types.mts +28 -0
  82. package/src/sync/sync-types.mts +33 -4
  83. package/tmp.md +2 -374
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @module merge-info-helpers.respec
3
+ */
4
+ import { iReckon, respecfully, ifWe, ifWeMight } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs';
5
+ import { mergeText } from './merge-info-helpers.mjs';
6
+
7
+ const maam = `[${import.meta.url}]`;
8
+ const sir = maam;
9
+
10
+ await respecfully(sir, 'Merge Info Helpers', async () => {
11
+
12
+ await respecfully(sir, 'mergeText', async () => {
13
+
14
+ await ifWeMight(sir, 'mergeText should concat different strings', async () => {
15
+ const textA = "Hello";
16
+ const textB = "World";
17
+ const res = await mergeText({ textA, textB });
18
+ iReckon(sir, res).asTo('result').isGonnaBeTruthy();
19
+ iReckon(sir, res.includes(textA)).asTo('includes A').isGonnaBeTruthy();
20
+ iReckon(sir, res.includes(textB)).asTo('includes B').isGonnaBeTruthy();
21
+ iReckon(sir, res.includes('MERGE SEPARATOR')).asTo('includes separator').isGonnaBeTruthy();
22
+ });
23
+
24
+ await ifWeMight(sir, 'mergeText should return same string if identical', async () => {
25
+ const textA = "Hello";
26
+ const res = await mergeText({ textA, textB: textA });
27
+ iReckon(sir, res).asTo('result').isGonnaBe(textA);
28
+ });
29
+
30
+ });
31
+
32
+ await respecfully(sir, 'combineDivergentTimelines', async () => {
33
+
34
+ await ifWeMight(sir, 'combineDivergentTimelines should work (Stub)', async () => {
35
+ // Validation of stub for now, just to ensure scaffolding works.
36
+ iReckon(sir, true).asTo('stub executed').isGonnaBeTruthy();
37
+ });
38
+
39
+ });
40
+
41
+ });
@@ -0,0 +1,28 @@
1
+ import { IbGib_V1, IbGibData_V1, IbGibRel8ns_V1 } from "@ibgib/ts-gib/dist/V1/types.mjs";
2
+ import { MERGE_INFO_ATOM } from "./merge-info-constants.mjs";
3
+
4
+ export interface MergeInfoIb_V1 {
5
+ atom: typeof MERGE_INFO_ATOM;
6
+ algo: string; // e.g. "optimistic", "text", "manual"
7
+ }
8
+
9
+ export interface MergeInfoData_V1 extends IbGibData_V1 {
10
+ /**
11
+ * The algorithm or strategy used to perform the merge.
12
+ */
13
+ algo: string;
14
+ /**
15
+ * Optional details about the merge, e.g. validation results or specifics about conflicts resolved.
16
+ */
17
+ details?: string;
18
+ }
19
+
20
+ export interface MergeInfoRel8ns_V1 extends IbGibRel8ns_V1 {
21
+ /**
22
+ * The ibGibs that were merged together.
23
+ * For a timeline merge, these are the tips of the divergent branches.
24
+ */
25
+ ancestors?: string[];
26
+ }
27
+
28
+ export interface MergeInfoIbGib_V1 extends IbGib_V1<MergeInfoData_V1, MergeInfoRel8ns_V1> { }
@@ -0,0 +1,208 @@
1
+ /**
2
+ * @module conflict-optimistic
3
+ *
4
+ * Implements the Optimistic Conflict Resolution Strategy.
5
+ *
6
+ * Logic:
7
+ * 1. findLCA: Traverses `past` to find common ancestor.
8
+ * 2. mergeDivergentTimelines: Replays DNA to create a merged tip.
9
+ */
10
+
11
+ import { IbGib_V1, IbGibRel8ns_V1, IbGibData_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs';
12
+ import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
13
+ import { IbGibSpaceAny } from '../../witness/space/space-base-v1.mjs';
14
+ import { getFromSpace, putInSpace } from '../../witness/space/space-helper.mjs';
15
+ import { mut8Timeline } from '../../timeline/timeline-api.mjs';
16
+
17
+ const lc = `[conflict-optimistic]`;
18
+
19
+ export interface LCAResult {
20
+ lcaAddr: string;
21
+ branchA: IbGib_V1[]; // Path from LCA to Tip A (exclusive of LCA)
22
+ branchB: IbGib_V1[]; // Path from LCA to Tip B (exclusive of LCA)
23
+ }
24
+
25
+ /**
26
+ * Finds the Last Common Ancestor (LCA) between two divergent timelines.
27
+ *
28
+ * Note: simplistic implementation assuming simple linear divergence.
29
+ * Complex DAGs might require more robust graph traversal.
30
+ */
31
+ export async function findLCA({
32
+ tipA,
33
+ tipB,
34
+ space,
35
+ }: {
36
+ tipA: IbGib_V1,
37
+ tipB: IbGib_V1,
38
+ space: IbGibSpaceAny,
39
+ }): Promise<LCAResult> {
40
+ // 1. Load history for both
41
+ // Optimization: Just load 'past' array if available.
42
+ // Ideally we assume 'past' is full history for V1.
43
+
44
+ // Check if one is ancestor of another first?
45
+ const pastA = tipA.rel8ns?.past || [];
46
+ const pastB = tipB.rel8ns?.past || [];
47
+ const addrA = getIbGibAddr({ ibGib: tipA });
48
+ const addrB = getIbGibAddr({ ibGib: tipB });
49
+
50
+ // Is B in A's past?
51
+ if (pastA.includes(addrB)) { return { lcaAddr: addrB, branchA: [], branchB: [] }; } // Not a divergence, just an update.
52
+ // Is A in B's past?
53
+ if (pastB.includes(addrA)) { return { lcaAddr: addrA, branchA: [], branchB: [] }; }
54
+
55
+ // Find intersection
56
+ let lcaAddr: string | undefined;
57
+
58
+ // Iterate backwards through A's past
59
+ // The *latest* entry in A's past that is also in B's past (or is B itself, but we checked that)
60
+ // Actually, 'past' is usually ordered? V1 convention is accretive, so order matches evolution.
61
+ // But 'past' array order isn't strictly guaranteed by schema to be chronological, but robust implementations do so.
62
+ // Let's assume Set intersection.
63
+
64
+ const setPastB = new Set(pastB);
65
+ // Find the *latest* item in pastA that is in setPastB.
66
+ // We assume pastA is ordered [older -> newer].
67
+ for (let i = pastA.length - 1; i >= 0; i--) {
68
+ const candidate = pastA[i];
69
+ if (setPastB.has(candidate)) {
70
+ lcaAddr = candidate;
71
+ break;
72
+ }
73
+ }
74
+
75
+ if (!lcaAddr) {
76
+ // Fallback: check if they share the SAME TJP (they should if they are same timeline).
77
+ // If they share TJP but no common past, the TJP itself is the LCA.
78
+ const tjpA = tipA.rel8ns?.tjp?.[0];
79
+ const tjpB = tipB.rel8ns?.tjp?.[0];
80
+ if (tjpA && tjpA === tjpB) {
81
+ lcaAddr = tjpA;
82
+ } else {
83
+ throw new Error(`No LCA found for divergences. Are they the same timeline? (E: 81aa17f0e34b4138ad1743a60a76f27b)`);
84
+ }
85
+ }
86
+
87
+ // 2. Reconstruct Branches
88
+ // We need the actual ibGib objects from LCA to Tips to inspect DNA.
89
+ // This requires fetching from Space.
90
+
91
+ // Helper to get path from LCA to Tip
92
+ const getPath = async (tip: IbGib_V1, past: string[], lca: string): Promise<IbGib_V1[]> => {
93
+ const pathAddrs: string[] = [];
94
+ let foundLca = false;
95
+ for (const addr of past) {
96
+ if (foundLca) {
97
+ pathAddrs.push(addr);
98
+ } else if (addr === lca) {
99
+ foundLca = true;
100
+ }
101
+ }
102
+ pathAddrs.push(getIbGibAddr({ ibGib: tip }));
103
+
104
+ // Fetch all
105
+ const res = await getFromSpace({ space, addrs: pathAddrs });
106
+ if (!res.success || !res.ibGibs) { throw new Error(`Failed to fetch history branch. (E: 7904097405be4a27b871c5031631553c)`); }
107
+ return res.ibGibs;
108
+ };
109
+
110
+ const branchA = await getPath(tipA, pastA, lcaAddr);
111
+ const branchB = await getPath(tipB, pastB, lcaAddr);
112
+
113
+ return { lcaAddr, branchA, branchB };
114
+ }
115
+
116
+ /**
117
+ * Optimistically merges two divergent timelines by inspecting DNA.
118
+ */
119
+ export async function mergeDivergentTimelines({
120
+ tipA,
121
+ tipB,
122
+ space,
123
+ metaspace,
124
+ }: {
125
+ tipA: IbGib_V1,
126
+ tipB: IbGib_V1,
127
+ space: IbGibSpaceAny,
128
+ metaspace: any, // MetaspaceService type
129
+ }): Promise<IbGib_V1> {
130
+
131
+ // 1. Find LCA & Branches
132
+ const { lcaAddr, branchA, branchB } = await findLCA({ tipA, tipB, space });
133
+
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
+ },
200
+ space,
201
+ metaspace,
202
+ });
203
+
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
+ return result;
208
+ }
@@ -0,0 +1,194 @@
1
+ /**
2
+ * @module sync-conflict.respec
3
+ *
4
+ * Verifies Conflict Resolution strategies in SyncSagaCoordinator.
5
+ * Reproduces divergence scenarios and asserts resolution behavior.
6
+ */
7
+
8
+ import {
9
+ respecfully, lastOfAll, ifWe, iReckon,
10
+ ifWeMight
11
+ } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs';
12
+ const maam = `[${import.meta.url}]`, sir = maam;
13
+ import { delay, extractErrorMsg, pretty } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
14
+ import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
15
+
16
+ import { SyncSagaCoordinator } from './sync-saga-coordinator.mjs';
17
+ import { getFromSpace } from '../witness/space/space-helper.mjs';
18
+ import { Metaspace_Innerspace } from '../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs';
19
+ import { InnerSpace_V1 } from '../witness/space/inner-space/inner-space-v1.mjs';
20
+ import { createTimelineRootHelper, getTestKeystoneServiceHelper } from '../agent-helpers.mjs';
21
+ import { mut8Timeline } from '../timeline/timeline-api.mjs';
22
+ import { SyncPeerInnerspace_V1 } from './sync-peer/sync-peer-innerspace-v1.mjs';
23
+ import { DEFAULT_INNER_SPACE_DATA_V1 } from '../witness/space/inner-space/inner-space-types.mjs';
24
+ import { toDto } from '../common/other/ibgib-helper.mjs';
25
+ import { SyncConflictStrategy, SyncSagaInfo } from './sync-types.mjs';
26
+ import { IbGibData_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs';
27
+
28
+ interface TestData extends IbGibData_V1 {
29
+ text?: string;
30
+ fieldA?: string;
31
+ fieldB?: string;
32
+ }
33
+
34
+ const logalot = true;
35
+ const lc = `[sync-conflict.respec]`;
36
+
37
+ await respecfully(sir, `Sync Conflict Resolution`, async () => {
38
+
39
+ let metaspace: Metaspace_Innerspace;
40
+ let sourceSpace: InnerSpace_V1;
41
+ let destSpace: InnerSpace_V1;
42
+
43
+ await respecfully(sir, `Scenario: Simple Divergence (Field A vs Field B)`, async () => {
44
+ // 1. Setup Spaces
45
+ metaspace = new Metaspace_Innerspace(undefined);
46
+ await metaspace.initialize({
47
+ getFnAlert: () => async ({ title, msg }) => { console.log(`[Alert] ${title}: ${msg}`); },
48
+ getFnPrompt: () => async ({ title, msg }) => { console.log(`[Prompt] ${title}: ${msg}`); return ''; },
49
+ getFnPromptPassword: () => async (title, msg) => { console.log(`[PromptPwd] ${title}: ${msg}`); return null; },
50
+ });
51
+ while (!metaspace.initialized) { await delay(10); }
52
+
53
+ const defaultLocalUserSpace = await metaspace.getLocalUserSpace({ lock: false });
54
+ await defaultLocalUserSpace!.initialized;
55
+
56
+ sourceSpace = new InnerSpace_V1({
57
+ ...DEFAULT_INNER_SPACE_DATA_V1,
58
+ name: 'source',
59
+ uuid: 'source_uuid',
60
+ description: 'source test space',
61
+ });
62
+ await sourceSpace.initialized;
63
+
64
+ destSpace = new InnerSpace_V1({
65
+ ...DEFAULT_INNER_SPACE_DATA_V1,
66
+ name: 'dest',
67
+ uuid: 'dest_uuid',
68
+ description: 'dest test space',
69
+ });
70
+ await destSpace.initialized;
71
+
72
+ // 2. Seed Common History (V0 -> V1)
73
+ console.log(`${lc} Creating Common History...`);
74
+ const root = await createTimelineRootHelper<TestData>({
75
+ ib: 'timeline_root',
76
+ data: { type: 'root', text: 'v0' },
77
+ space: sourceSpace,
78
+ });
79
+ // Sync root to dest immediately so they start synced
80
+ await metaspace.put({ ibGib: root, space: destSpace });
81
+ await metaspace.registerNewIbGib({ ibGib: root, space: destSpace });
82
+
83
+
84
+ // Create V1 (Common)
85
+ const v1_Source = await mut8Timeline<TestData>({
86
+ timeline: root,
87
+ mut8Opts: { dataToAddOrPatch: { text: 'v1_common' } },
88
+ metaspace,
89
+ space: sourceSpace,
90
+ });
91
+ // Sync V1 to dest
92
+ await metaspace.put({ ibGib: v1_Source, space: destSpace });
93
+ await metaspace.registerNewIbGib({ ibGib: v1_Source, space: destSpace });
94
+
95
+ const resGetDest = await getFromSpace({ space: destSpace, addr: getIbGibAddr({ ibGib: v1_Source }) });
96
+ if (!resGetDest.success || !resGetDest.ibGibs || resGetDest.ibGibs.length === 0) {
97
+ throw new Error(`Failed to retrieve v1_Dest from destSpace. (E: a1b2c3d4e5f6g7h8i9j0)`);
98
+ }
99
+ const v1_Dest = resGetDest.ibGibs[0];
100
+
101
+
102
+ // 3. Create Divergence (V2a vs V2b)
103
+ console.log(`${lc} Creating Divergence...`);
104
+
105
+ // Source: V1 -> V2a (Edit Field A)
106
+ const v2a = await mut8Timeline<TestData>({
107
+ timeline: v1_Source,
108
+ mut8Opts: { dataToAddOrPatch: { fieldA: 'source_edit' } },
109
+ metaspace,
110
+ space: sourceSpace,
111
+ });
112
+
113
+ // Dest: V1 -> V2b (Edit Field B)
114
+ const v2b = await mut8Timeline<TestData>({
115
+ timeline: v1_Dest,
116
+ mut8Opts: { dataToAddOrPatch: { fieldB: 'dest_edit' } },
117
+ metaspace,
118
+ space: destSpace,
119
+ });
120
+
121
+ // Verify Divergence
122
+ const tjpAddr = root.rel8ns!.tjp![0]; // wait, root IS tjp, so no tjp rel8n...
123
+ // Actually root frame has isTjp: true.
124
+ // v1 has tjp: [v0_gib].
125
+ console.log(`${lc} Divergence created.`);
126
+
127
+
128
+ // 4. Run Sync (Optimistic)
129
+ console.log(`${lc} Setting up Coordinators...`);
130
+ const mockKeystone = await getTestKeystoneServiceHelper();
131
+ const senderCoordinator = new SyncSagaCoordinator(mockKeystone);
132
+ const receiverCoordinator = new SyncSagaCoordinator(mockKeystone);
133
+
134
+ const peer = new SyncPeerInnerspace_V1({
135
+ senderSpace: sourceSpace,
136
+ receiverSpace: destSpace,
137
+ receiverCoordinator,
138
+ receiverMetaspace: metaspace,
139
+ });
140
+
141
+ // Verify Receiver has correct KV (Pre-Sync Check)
142
+ // This ensures the conflict precondition exists.
143
+ await ifWe(sir, 'verify receiver KV pre-sync', async () => {
144
+ const destKV = await receiverCoordinator.getKnowledgeVector({
145
+ space: destSpace,
146
+ metaspace,
147
+ domainIbGibs: [v1_Dest]
148
+ });
149
+ const v1TjpAddr = v1_Dest.rel8ns?.tjp?.[0] || getIbGibAddr({ ibGib: v1_Dest }); // v1 might be tjp if it was root, but here v1 is child of root
150
+ // Actually v1 has tjp rel8n.
151
+ const destTip = destKV[v1TjpAddr];
152
+ iReckon(sir, !!destTip).asTo(`Dest KV has timeline tip for ${v1TjpAddr}`).isGonnaBeTruthy();
153
+ if (!destTip) {
154
+ throw new Error(`Test Setup Fail: Dest Space does not have index for timeline ${v1TjpAddr}. Seeding failed. (E: 8a9b0c1d)`);
155
+ }
156
+ });
157
+
158
+ console.log(`${lc} Running Sync (ConflictStrategy: optimistic)...`);
159
+
160
+ let resSync: SyncSagaInfo | undefined;
161
+ try {
162
+ resSync = await senderCoordinator.sync({
163
+ peer,
164
+ source: sourceSpace,
165
+ dest: destSpace,
166
+ metaspace,
167
+ domainIbGibs: [v2a],
168
+ conflictStrategy: SyncConflictStrategy.optimistic,
169
+ // identity: undefined as any, // Not needed? If removed, it uses session identity.
170
+ // identitySecret: undefined as any,
171
+ // explicit useSessionIdentity: true is default
172
+ });
173
+
174
+ await resSync.done;
175
+ console.log(`${lc} Sync Complete.`);
176
+ } catch (e) {
177
+ console.error(`${lc} Sync Failed with Error:`, e);
178
+ iReckon(sir, false).asTo(`Sync failed with error: ${e}`).isGonnaBeTruthy();
179
+ return; // Exit test early
180
+ }
181
+
182
+ // 5. Verification
183
+ // Expectation: Both Spaces should now have a NEW tip (V3) that merges V2a and V2b.
184
+
185
+ const resSourceTip = await getFromSpace({ space: sourceSpace, addr: getIbGibAddr({ ibGib: v2a }) });
186
+
187
+ await ifWe(sir, `verify merge happened`, async () => {
188
+ // We expect a new tip that is NOT v2a or v2b
189
+ // For now, valid failure is that this code doesn't start or completes without merge.
190
+ iReckon(sir, true).asTo('Merge Verification Not Yet Implemented').isGonnaBeFalse();
191
+ });
192
+ });
193
+
194
+ });
@@ -0,0 +1,133 @@
1
+ /**
2
+ * @module sync-innerspace-constants.respec
3
+ *
4
+ * Verifies syncing "Constants" (ibGibs without timelines/TJPs).
5
+ * Also verifies Smart Diff behavior (Constants shouldn't be re-sent).
6
+ */
7
+
8
+ import {
9
+ respecfully, ifWeMight, iReckon, ifWe
10
+ } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs';
11
+ const maam = `[${import.meta.url}]`, sir = maam;
12
+ import { delay } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
13
+ import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
14
+ import { Factory_V1 } from '@ibgib/ts-gib/dist/V1/index.mjs';
15
+
16
+ import { SyncSagaCoordinator } from './sync-saga-coordinator.mjs';
17
+ import { putInSpace, getFromSpace } from '../witness/space/space-helper.mjs';
18
+ import { Metaspace_Innerspace } from '../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs';
19
+ import { SyncPeerInnerspace_V1 } from './sync-peer/sync-peer-innerspace-v1.mjs';
20
+ import { InnerSpace_V1 } from '../witness/space/inner-space/inner-space-v1.mjs';
21
+ import { DEFAULT_INNER_SPACE_DATA_V1 } from '../witness/space/inner-space/inner-space-types.mjs';
22
+ import { getTestKeystoneServiceHelper } from '../agent-helpers.mjs';
23
+
24
+ const lc = `[sync-innerspace-constants.respec]`;
25
+
26
+ await respecfully(sir, `Sync Constants (No TJP)`, async () => {
27
+
28
+ await ifWe(sir, `Verify Constants Sync`, async () => {
29
+ // 1. Setup Spaces
30
+ const metaspace = new Metaspace_Innerspace(undefined);
31
+ await metaspace.initialize({
32
+ getFnAlert: () => async ({ title, msg }) => { },
33
+ getFnPrompt: () => async ({ title, msg }) => { return ''; },
34
+ getFnPromptPassword: () => async (title, msg) => { return null; },
35
+ });
36
+ while (!metaspace.initialized) { await delay(10); }
37
+
38
+ const sourceSpace = new InnerSpace_V1({
39
+ ...DEFAULT_INNER_SPACE_DATA_V1,
40
+ name: 'source',
41
+ uuid: 'source_uuid',
42
+ description: 'source test space',
43
+ });
44
+ await sourceSpace.initialized;
45
+
46
+ const destSpace = new InnerSpace_V1({
47
+ ...DEFAULT_INNER_SPACE_DATA_V1,
48
+ name: 'dest',
49
+ uuid: 'dest_uuid',
50
+ description: 'dest test space',
51
+ });
52
+ await destSpace.initialized;
53
+
54
+ // 2. Create Constant (C1) in Source
55
+ // Factory_V1.constant returns Promise<IbGib_V1>, not { newIbGib: ... }
56
+ const c1 = await Factory_V1.constant({
57
+ ib: 'constant_c1',
58
+ parentPrimitiveIb: 'root',
59
+ data: { some: 'data' },
60
+ });
61
+ const addrC1 = getIbGibAddr({ ibGib: c1 });
62
+ await putInSpace({ space: sourceSpace, ibGibs: [c1] });
63
+
64
+ const checkDest1 = await getFromSpace({ space: destSpace, addr: addrC1 });
65
+ iReckon(sir, checkDest1.success).asTo('Dest initially does not have C1').isGonnaBeFalse();
66
+
67
+ // 3. Setup Coordinator
68
+ const mockKeystone = await getTestKeystoneServiceHelper();
69
+ const senderCoordinator = new SyncSagaCoordinator(mockKeystone);
70
+ const receiverCoordinator = new SyncSagaCoordinator(mockKeystone);
71
+
72
+ const peer = new SyncPeerInnerspace_V1({
73
+ senderSpace: sourceSpace,
74
+ receiverSpace: destSpace,
75
+ receiverCoordinator: receiverCoordinator,
76
+ receiverMetaspace: metaspace,
77
+ });
78
+
79
+ // 4. Sync C1 (First Pass)
80
+ let resSync = await senderCoordinator.sync({
81
+ peer: peer,
82
+ localSpace: sourceSpace,
83
+ metaspace: metaspace,
84
+ domainIbGibs: [c1],
85
+ useSessionIdentity: false,
86
+ });
87
+ await resSync.done;
88
+ iReckon(sir, true).asTo('Sync execution 1 completes').isGonnaBeTrue();
89
+
90
+ // Verify C1 in Dest
91
+ const getC1Dest = await getFromSpace({ space: destSpace, addr: addrC1 });
92
+ iReckon(sir, getC1Dest.success && !!getC1Dest.ibGibs![0]).asTo('Dest has C1 after sync').isGonnaBeTrue();
93
+
94
+ // 5. Sync C1 (Second Pass - Idempotency / Smart Diff)
95
+ // Should NOT send payload again.
96
+ resSync = await senderCoordinator.sync({
97
+ peer: peer,
98
+ localSpace: sourceSpace,
99
+ metaspace: metaspace,
100
+ domainIbGibs: [c1],
101
+ useSessionIdentity: false,
102
+ });
103
+ await resSync.done;
104
+ iReckon(sir, true).asTo('Sync execution 2 (idempotent) completes').isGonnaBeTrue();
105
+
106
+ // 6. Create "Constant with Dependency" (C2 -> C1)
107
+ const c2 = await Factory_V1.constant({
108
+ ib: 'constant_c2',
109
+ parentPrimitiveIb: 'root',
110
+ rel8ns: {
111
+ link: [addrC1], // C2 links to C1
112
+ },
113
+ });
114
+ const addrC2 = getIbGibAddr({ ibGib: c2 });
115
+ await putInSpace({ space: sourceSpace, ibGibs: [c2] });
116
+
117
+ // Sync C2
118
+ resSync = await senderCoordinator.sync({
119
+ peer: peer,
120
+ localSpace: sourceSpace,
121
+ metaspace: metaspace,
122
+ domainIbGibs: [c2],
123
+ useSessionIdentity: false,
124
+ });
125
+ await resSync.done;
126
+ iReckon(sir, true).asTo('Sync C2 completes').isGonnaBeTrue();
127
+
128
+ // Verify C2 in Dest
129
+ const getC2Dest = await getFromSpace({ space: destSpace, addr: addrC2 });
130
+ iReckon(sir, getC2Dest.success).asTo('Dest has C2').isGonnaBeTrue();
131
+
132
+ });
133
+ });
@@ -118,7 +118,6 @@ await respecfully(sir, `Sync InnerSpaces (Deep Updates)`, async () => {
118
118
  localSpace: sourceSpace,
119
119
  metaspace: metaspace,
120
120
  domainIbGibs: [commentV2], // Sync the TIP
121
- useSessionIdentity: false,
122
121
  });
123
122
 
124
123
  await resSync.done;
@@ -126,7 +125,7 @@ await respecfully(sir, `Sync InnerSpaces (Deep Updates)`, async () => {
126
125
  // 5. Verify Dest
127
126
  console.log(`${lc} Verifying Destination...`);
128
127
 
129
- await ifWeMight(sir, `verify deep timeline present`, async () => {
128
+ await ifWe(sir, `verify deep timeline present`, async () => {
130
129
  // Verify Tip (V2)
131
130
  const getV2 = await getFromSpace({ space: destSpace, addr: addrV2 });
132
131
  iReckon(sir, getV2.success).asTo('Tip V2 present').isGonnaBeTrue();
@@ -98,7 +98,7 @@ await respecfully(sir, `Sync InnerSpaces (Dest Ahead)`, async () => {
98
98
  });
99
99
  const addrV2 = getIbGibAddr({ ibGib: v2 });
100
100
 
101
- await ifWeMight(sir, 'verify setup', async () => {
101
+ await ifWe(sir, 'verify setup', async () => {
102
102
  // Ensure V2 is ONLY in Dest (it is, per `space: destSpace`)
103
103
  // Ensure Source does NOT have V2
104
104
  const checkV2InSource = await getFromSpace({ space: sourceSpace, addr: addrV2 });
@@ -132,7 +132,7 @@ await respecfully(sir, `Sync InnerSpaces (Dest Ahead)`, async () => {
132
132
  // 5. Verify Dest (Should still have V2 as latest)
133
133
  console.log(`${lc} Verifying Destination...`);
134
134
 
135
- await ifWeMight(sir, `verify dest stays ahead`, async () => {
135
+ await ifWe(sir, `verify dest stays ahead`, async () => {
136
136
  // Verify Tip (V2)
137
137
  const getV2 = await getFromSpace({ space: destSpace, addr: addrV2 });
138
138
  iReckon(sir, getV2.success).asTo('V2 still present in Dest').isGonnaBeTrue();
@@ -127,7 +127,7 @@ await respecfully(sir, `Sync InnerSpaces (Multiple Timelines)`, async () => {
127
127
  // 5. Verify Dest
128
128
  console.log(`${lc} Verifying Destination...`);
129
129
 
130
- await ifWeMight(sir, `verify timelines present`, async () => {
130
+ await ifWe(sir, `verify timelines present`, async () => {
131
131
  // Verify A
132
132
  const getA = await getFromSpace({ space: destSpace, addr: addrA });
133
133
  iReckon(sir, getA.success).asTo('Timeline A present').isGonnaBeTrue();