@ibgib/core-gib 0.1.21 → 0.1.23

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 (127) hide show
  1. package/dist/common/other/graph-helper.d.mts +8 -0
  2. package/dist/common/other/graph-helper.d.mts.map +1 -1
  3. package/dist/common/other/graph-helper.mjs +31 -1
  4. package/dist/common/other/graph-helper.mjs.map +1 -1
  5. package/dist/sync/sync-conflict.respec.mjs +31 -28
  6. package/dist/sync/sync-conflict.respec.mjs.map +1 -1
  7. package/dist/sync/sync-constants.d.mts +21 -11
  8. package/dist/sync/sync-constants.d.mts.map +1 -1
  9. package/dist/sync/sync-constants.mjs +18 -6
  10. package/dist/sync/sync-constants.mjs.map +1 -1
  11. package/dist/sync/sync-helpers.d.mts +24 -15
  12. package/dist/sync/sync-helpers.d.mts.map +1 -1
  13. package/dist/sync/sync-helpers.mjs +163 -92
  14. package/dist/sync/sync-helpers.mjs.map +1 -1
  15. package/dist/sync/sync-innerspace-constants.respec.mjs +12 -7
  16. package/dist/sync/sync-innerspace-constants.respec.mjs.map +1 -1
  17. package/dist/sync/sync-innerspace-deep-updates.respec.mjs +12 -7
  18. package/dist/sync/sync-innerspace-deep-updates.respec.mjs.map +1 -1
  19. package/dist/sync/sync-innerspace-dest-ahead.respec.mjs +15 -10
  20. package/dist/sync/sync-innerspace-dest-ahead.respec.mjs.map +1 -1
  21. package/dist/sync/sync-innerspace-multiple-timelines.respec.mjs +13 -7
  22. package/dist/sync/sync-innerspace-multiple-timelines.respec.mjs.map +1 -1
  23. package/dist/sync/sync-innerspace-partial-update.respec.mjs +15 -7
  24. package/dist/sync/sync-innerspace-partial-update.respec.mjs.map +1 -1
  25. package/dist/sync/sync-innerspace.respec.mjs +13 -7
  26. package/dist/sync/sync-innerspace.respec.mjs.map +1 -1
  27. package/dist/sync/sync-peer/sync-peer-constants.d.mts +2 -0
  28. package/dist/sync/sync-peer/sync-peer-constants.d.mts.map +1 -0
  29. package/dist/sync/sync-peer/sync-peer-constants.mjs +2 -0
  30. package/dist/sync/sync-peer/sync-peer-constants.mjs.map +1 -0
  31. package/dist/sync/sync-peer/sync-peer-helpers.d.mts +2 -0
  32. package/dist/sync/sync-peer/sync-peer-helpers.d.mts.map +1 -0
  33. package/dist/sync/sync-peer/sync-peer-helpers.mjs +2 -0
  34. package/dist/sync/sync-peer/sync-peer-helpers.mjs.map +1 -0
  35. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.d.mts +8 -0
  36. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.d.mts.map +1 -0
  37. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.mjs +8 -0
  38. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.mjs.map +1 -0
  39. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.d.mts +18 -0
  40. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.d.mts.map +1 -0
  41. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.mjs +54 -0
  42. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.mjs.map +1 -0
  43. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.d.mts +65 -0
  44. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.d.mts.map +1 -0
  45. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.mjs +5 -0
  46. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.mjs.map +1 -0
  47. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.d.mts +44 -0
  48. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.d.mts.map +1 -0
  49. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mjs +183 -0
  50. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mjs.map +1 -0
  51. package/dist/sync/sync-peer/sync-peer-types.d.mts +28 -0
  52. package/dist/sync/sync-peer/sync-peer-types.d.mts.map +1 -1
  53. package/dist/sync/sync-peer/sync-peer-v1.d.mts +51 -9
  54. package/dist/sync/sync-peer/sync-peer-v1.d.mts.map +1 -1
  55. package/dist/sync/sync-peer/sync-peer-v1.mjs +244 -26
  56. package/dist/sync/sync-peer/sync-peer-v1.mjs.map +1 -1
  57. package/dist/sync/sync-saga-context/sync-saga-context-helpers.d.mts +26 -10
  58. package/dist/sync/sync-saga-context/sync-saga-context-helpers.d.mts.map +1 -1
  59. package/dist/sync/sync-saga-context/sync-saga-context-helpers.mjs +119 -30
  60. package/dist/sync/sync-saga-context/sync-saga-context-helpers.mjs.map +1 -1
  61. package/dist/sync/sync-saga-context/sync-saga-context-types.d.mts +31 -22
  62. package/dist/sync/sync-saga-context/sync-saga-context-types.d.mts.map +1 -1
  63. package/dist/sync/sync-saga-context/sync-saga-context-types.mjs +1 -9
  64. package/dist/sync/sync-saga-context/sync-saga-context-types.mjs.map +1 -1
  65. package/dist/sync/sync-saga-coordinator.d.mts +81 -87
  66. package/dist/sync/sync-saga-coordinator.d.mts.map +1 -1
  67. package/dist/sync/sync-saga-coordinator.mjs +627 -571
  68. package/dist/sync/sync-saga-coordinator.mjs.map +1 -1
  69. package/dist/sync/sync-saga-message/sync-saga-message-types.d.mts +105 -22
  70. package/dist/sync/sync-saga-message/sync-saga-message-types.d.mts.map +1 -1
  71. package/dist/sync/sync-types.d.mts +56 -76
  72. package/dist/sync/sync-types.d.mts.map +1 -1
  73. package/dist/sync/sync-types.mjs +5 -0
  74. package/dist/sync/sync-types.mjs.map +1 -1
  75. package/dist/timeline/timeline-api.d.mts.map +1 -1
  76. package/dist/timeline/timeline-api.mjs +15 -8
  77. package/dist/timeline/timeline-api.mjs.map +1 -1
  78. package/dist/witness/light-witness-base-v1.d.mts.map +1 -1
  79. package/dist/witness/light-witness-base-v1.mjs +2 -0
  80. package/dist/witness/light-witness-base-v1.mjs.map +1 -1
  81. package/dist/witness/space/inner-space/inner-space-v1.mjs +1 -1
  82. package/dist/witness/space/inner-space/inner-space-v1.mjs.map +1 -1
  83. package/package.json +1 -1
  84. package/src/common/other/graph-helper.mts +26 -1
  85. package/src/sync/README.md +31 -22
  86. package/src/sync/sync-conflict.respec.mts +31 -26
  87. package/src/sync/sync-constants.mts +19 -9
  88. package/src/sync/sync-helpers.mts +173 -97
  89. package/src/sync/sync-innerspace-constants.respec.mts +12 -7
  90. package/src/sync/sync-innerspace-deep-updates.respec.mts +12 -7
  91. package/src/sync/sync-innerspace-dest-ahead.respec.mts +14 -9
  92. package/src/sync/sync-innerspace-multiple-timelines.respec.mts +13 -7
  93. package/src/sync/sync-innerspace-partial-update.respec.mts +15 -7
  94. package/src/sync/sync-innerspace.respec.mts +13 -7
  95. package/src/sync/sync-peer/sync-peer-constants.mts +0 -0
  96. package/src/sync/sync-peer/sync-peer-helpers.mts +0 -0
  97. package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.mts +8 -0
  98. package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.mts +72 -0
  99. package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.mts +72 -0
  100. package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mts +193 -0
  101. package/src/sync/sync-peer/sync-peer-types.mts +30 -1
  102. package/src/sync/sync-peer/sync-peer-v1.mts +229 -30
  103. package/src/sync/sync-saga-context/sync-saga-context-helpers.mts +140 -43
  104. package/src/sync/sync-saga-context/sync-saga-context-types.mts +34 -30
  105. package/src/sync/sync-saga-coordinator.mts +678 -660
  106. package/src/sync/sync-saga-message/sync-saga-message-types.mts +106 -22
  107. package/src/sync/sync-types.mts +59 -87
  108. package/src/timeline/timeline-api.mts +17 -10
  109. package/src/witness/light-witness-base-v1.mts +2 -1
  110. package/src/witness/space/inner-space/inner-space-v1.mts +1 -1
  111. package/test_output.log +0 -0
  112. package/tmp.md +62 -44
  113. package/dist/sync/sync-local-spaces.respec.d.mts +0 -2
  114. package/dist/sync/sync-local-spaces.respec.d.mts.map +0 -1
  115. package/dist/sync/sync-local-spaces.respec.mjs +0 -159
  116. package/dist/sync/sync-local-spaces.respec.mjs.map +0 -1
  117. package/dist/sync/sync-peer/sync-peer-innerspace-v1.d.mts +0 -42
  118. package/dist/sync/sync-peer/sync-peer-innerspace-v1.d.mts.map +0 -1
  119. package/dist/sync/sync-peer/sync-peer-innerspace-v1.mjs +0 -194
  120. package/dist/sync/sync-peer/sync-peer-innerspace-v1.mjs.map +0 -1
  121. package/dist/sync/sync-saga-coordinator.respec.d.mts +0 -2
  122. package/dist/sync/sync-saga-coordinator.respec.d.mts.map +0 -1
  123. package/dist/sync/sync-saga-coordinator.respec.mjs +0 -40
  124. package/dist/sync/sync-saga-coordinator.respec.mjs.map +0 -1
  125. package/src/sync/sync-local-spaces.respec.mts +0 -200
  126. package/src/sync/sync-peer/sync-peer-innerspace-v1.mts +0 -240
  127. package/src/sync/sync-saga-coordinator.respec.mts +0 -52
@@ -1,18 +1,39 @@
1
1
  /**
2
2
  * @module SyncPeer_V1 witness class
3
+ *
4
+ *
5
+ * NOTE: All push/pulls in this were done to get to compile. These should mostly
6
+ * not be single push/pull calls but should be batched with multiple addresses
7
+ * NOTE: All push/pulls in this were done to get to compile. These should mostly
8
+ * not be single push/pull calls but should be batched with multiple addresses
9
+ * NOTE: All push/pulls in this were done to get to compile. These should mostly
10
+ * not be single push/pull calls but should be batched with multiple addresses
11
+ * NOTE: All push/pulls in this were done to get to compile. These should mostly
12
+ * not be single push/pull calls but should be batched with multiple addresses
13
+ * NOTE: All push/pulls in this were done to get to compile. These should mostly
14
+ * not be single push/pull calls but should be batched with multiple addresses
3
15
  */
4
16
 
5
17
  import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
6
18
  import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
7
19
  import { IbGib_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs';
20
+ import { IbGibAddr } from '@ibgib/ts-gib/dist/types.mjs';
21
+ import { validateIbGibIntrinsically } from '@ibgib/ts-gib/dist/V1/validate-helper.mjs';
8
22
 
9
23
  import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs';
10
- import { SYNC_MSG_REL8N_NAME, SYNC_SAGA_PAYLOAD_ADDRS_REL8N_NAME } from '../sync-constants.mjs';
24
+ import { SYNC_MSG_REL8N_NAME, SYNC_SAGA_PAYLOAD_ADDRS_DOMAIN } from '../sync-constants.mjs';
25
+ import { SubjectWitness } from '../../common/pubsub/subject/subject-types.mjs';
11
26
  import { SyncSagaContextIbGib_V1 } from '../sync-saga-context/sync-saga-context-types.mjs';
12
27
  import { SyncPeerData_V1, SyncPeerRel8ns_V1, SyncPeerWitness } from './sync-peer-types.mjs';
13
28
  import { LightWitnessBase_V1 } from '../../witness/light-witness-base-v1.mjs';
29
+ import { IbGibSpaceAny } from '../../witness/space/space-base-v1.mjs';
30
+ import { newupSubject } from '../../common/pubsub/subject/subject-helper.mjs';
31
+ import { validateContext } from '../sync-saga-context/sync-saga-context-helpers.mjs';
32
+ import { getFromSpace } from '../../witness/space/space-helper.mjs';
14
33
 
15
34
  const logalot = GLOBAL_LOG_A_LOT || true;
35
+ const logalotControlDomain = true;
36
+ const lcControlDomain = '[ControlDomain]';
16
37
 
17
38
  /**
18
39
  * Abstract witness for talking to a Sync Peer (e.g. Remote Node or Local Simulator).
@@ -29,28 +50,94 @@ export abstract class SyncPeer_V1 extends LightWitnessBase_V1<SyncPeerData_V1, S
29
50
 
30
51
  protected lc: string = `[${SyncPeer_V1.name}]`;
31
52
 
53
+ public senderSpace: IbGibSpaceAny;
54
+ public senderTempSpace: IbGibSpaceAny;
55
+ public payloadIbGibsDomainReceived$!: SubjectWitness<IbGib_V1>;
56
+
57
+ get classname(): string {
58
+ if (!this.data) { throw new Error(`(UNEXPECTED) this.data falsy? (E: 1ab1841e9338b54f3aa615fa37024826)`); }
59
+ if (!this.data.classname) { throw new Error(`invalid peer. this.data.classname is falsy (E: b0ee28a0abb84a06588d9de7afcef826)`); }
60
+ return this.data.classname;
61
+ }
62
+
63
+ constructor(senderSpace: IbGibSpaceAny, senderTempSpace: IbGibSpaceAny, initialData: SyncPeerData_V1, initialRel8ns?: SyncPeerRel8ns_V1) {
64
+ super(initialData, initialRel8ns);
65
+ this.senderSpace = senderSpace;
66
+ this.senderTempSpace = senderTempSpace;
67
+ }
68
+
69
+ async initialize(): Promise<void> {
70
+ this.payloadIbGibsDomainReceived$ = await newupSubject<IbGib_V1>();
71
+ }
72
+
73
+ protected abstract ensureReceiverTempSpace(): Promise<IbGibSpaceAny>;
74
+
32
75
  /**
33
76
  * Witness the synchronization context (Send/Receive).
34
77
  *
78
+ * At this point in code, we are sending the context OUTGOING. The {@link context} should
79
+ * have a fully populated {@link SyncSagaContextIbGib_V1.sagaFrame}
80
+ *
35
81
  * @param arg The OUTGOING context (Request).
36
82
  * @returns The INCOMING context (Response), or undefined if failed/empty.
37
83
  */
38
- async witness(arg: SyncSagaContextIbGib_V1): Promise<SyncSagaContextIbGib_V1 | undefined> {
84
+ override async witness(context: SyncSagaContextIbGib_V1): Promise<SyncSagaContextIbGib_V1 | undefined> {
39
85
  const lc = `${this.lc}[${this.witness.name}]`;
40
86
  try {
41
87
  if (logalot) { console.log(`${lc} starting...`); }
42
88
 
89
+ if (logalot) { console.log(`${lc} starting...`); }
90
+
91
+ // validate, authenticate, and authorize the context, sagaFrame, and identity(s)
92
+ const validationErrors = await validateContext({ context });
93
+ if (validationErrors.length > 0) {
94
+ throw new Error(`invalid context received. validationErrors: ${validationErrors} (E: 8b34c875c968af29bc433138e57a7826)`);
95
+ }
96
+ const authenticationErrors = await this.authenticateContext({ context });
97
+ if (authenticationErrors.length > 0) {
98
+ throw new Error(`invalid context authentication. authenticationErrors: ${authenticationErrors} (E: da89da5ee1269aeb78952d475d607526)`);
99
+ }
100
+ const authorizationErrors = await this.authorizeContext({ context });
101
+ if (authorizationErrors.length > 0) {
102
+ throw new Error(`invalid context authorization. authorizationErrors: ${authorizationErrors} (E: 8ddc284a758cf10ba829334c1babb826)`);
103
+ }
104
+
105
+ // at this point, we have a valid, authenticated, authorized context
106
+
107
+ // persist the context, sagaframe, identity(s) ONLY in the receiver's durable space
108
+ // if sending domain ibgibs...
109
+ // verify the domain ibgibs intrinsically.
110
+ // persist the domain ibgibs in the receiver's temp space (push all addrs?)
111
+ // await until all domain ibgibs are in receiver's temp space
112
+ // process the context with the receiver's coordinator to get a new response context
113
+ // analyze the response context
114
+ // validate context intrinsically
115
+ // validate return identity if present
116
+ // persist the response context's dependency graph (context, saga frame and msg(s), identities)
117
+ // if response has payload domain ibgibs,
118
+ // spin off...
119
+ // pull domain ibgibs to local temp space
120
+ // publish domain ibgibs to observable
121
+ // when all domain ibgibs are pulled, publish complete to the observable
122
+ // return resulting context ibgib (which the caller should get BEFORE the domain ibgibs observable completes)
123
+
43
124
  // 1. PUSH: Ensure Request Data is available on Receiver
44
- await this.pushContextGraph({ context: arg });
125
+ await this.pushContextGraph({ context }); // ???
45
126
 
46
127
  // 2. EXECUTE: Trigger Remote Processing
47
- const response = await this.sendRequest(arg);
128
+ const response = await this.sendContextRequest(context);
48
129
 
49
130
  if (!response) {
50
131
  if (logalot) { console.log(`${lc} No response from peer.`); }
51
132
  return undefined;
52
133
  }
53
134
 
135
+ // at this point, we have received the response context, sync saga
136
+ // frame/msg stones, and any keystones should be evolved. Depending
137
+ // on the concrete implementation, if there are domain ibgibs to
138
+ // receive, they may already be coming.
139
+
140
+ // todo: this pull is a
54
141
  // 3. PULL: Ensure Response Data is available on Sender (Local)
55
142
  await this.pullContextGraph({ context: response });
56
143
 
@@ -65,45 +152,98 @@ export abstract class SyncPeer_V1 extends LightWitnessBase_V1<SyncPeerData_V1, S
65
152
  }
66
153
 
67
154
  /**
68
- * Primitive: Transmits the Context IbGib itself to the Peer.
155
+ * Transmits the Context IbGib itself to the Peer.
69
156
  */
70
- protected abstract sendRequest(context: SyncSagaContextIbGib_V1): Promise<SyncSagaContextIbGib_V1 | undefined>;
157
+ protected abstract sendContextRequest(context: SyncSagaContextIbGib_V1): Promise<SyncSagaContextIbGib_V1 | undefined>;
71
158
 
72
159
  /**
73
- * Primitive: Pushes a specific IbGib (by address) from Local to Remote.
74
- * Should handle identifying if the remote already has it (optimization) or just put it.
160
+ * Pushes specific IbGib(s) (by address) from Local to Remote.
161
+ *
162
+ * Should NOT have to handle identifying if the remote already has it
163
+ * (optimization), because the sync coordinator should be doing this in
164
+ * its diffing algorithm.
75
165
  */
76
- protected abstract push(addr: string): Promise<void>;
166
+ protected abstract push(addrs: IbGibAddr[]): Promise<void>;
167
+
168
+ /**
169
+ * Primitive: Pulls specific IbGib(s) (by address) from Remote to Local.
170
+ */
171
+ protected abstract pull(addrs: IbGibAddr[]): Promise<void>;
172
+
173
+
174
+
175
+ /**
176
+ * move to sync-peer-helpers.mts as a pure function?
177
+ */
178
+ protected async authenticateContext({ context }: { context: SyncSagaContextIbGib_V1 }): Promise<string[]> {
179
+ const lc = `${this.lc}[${this.authenticateContext.name}]`;
180
+ try {
181
+ if (logalot) { console.log(`${lc} starting... (I: 2677a482dfa873dcd1aa04a3031ff826)`); }
182
+
183
+ console.error(`${lc} NAG ERROR (NOT THROWN): not implemented. // todo: authenticate (v1 must have this after we get merge logic workflow going) (E: bc3a78f2dab18ab64c36d055a4b50526)`);
184
+
185
+ return [];
186
+ } catch (error) {
187
+ console.error(`${lc} ${extractErrorMsg(error)}`);
188
+ throw error;
189
+ } finally {
190
+ if (logalot) { console.log(`${lc} complete.`); }
191
+ }
192
+ }
77
193
 
78
194
  /**
79
- * Primitive: Pulls a specific IbGib (by address) from Remote to Local.
195
+ * move to sync-peer-helpers.mts as a pure function?
80
196
  */
81
- protected abstract pull(addr: string): Promise<void>;
197
+ protected async authorizeContext({ context }: { context: SyncSagaContextIbGib_V1 }): Promise<string[]> {
198
+ const lc = `${this.lc}[${this.authorizeContext.name}]`;
199
+ try {
200
+ if (logalot) { console.log(`${lc} starting... (I: 48c918b41ceec0cd489ca3b8819e6826)`); }
201
+
202
+ console.error(`${lc} NAG ERROR (NOT THROWN): not implemented. authorize business logic (v1 must have this, but later when we are working on admin vs. student)(E: bc3a78f2dab18ab64c36d055a4b50526)`);
203
+
204
+ return [];
205
+ } catch (error) {
206
+ console.error(`${lc} ${extractErrorMsg(error)}`);
207
+ throw error;
208
+ } finally {
209
+ if (logalot) { console.log(`${lc} complete.`); }
210
+ }
211
+ }
82
212
 
83
213
  /**
84
214
  * Helper to orchestrate the PUSH of a Context's dependency graph.
85
215
  */
86
216
  protected async pushContextGraph({ context }: { context: SyncSagaContextIbGib_V1 }): Promise<void> {
217
+ const lc = `[${SyncPeer_V1.name}][pushContextGraph]`;
218
+
87
219
  // A. Push Context Envelope
88
220
  const contextAddr = getIbGibAddr({ ibGib: context });
89
- await this.push(contextAddr);
221
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH Context Envelope -> ${contextAddr} (I: 3f4e5a6b7c8d9e0f)`); }
222
+ await this.push([contextAddr]);
90
223
 
91
224
  // B. Push Saga Frame & Immediate Deps (Msg, Identity)
92
225
  const frameAddr = context.rel8ns?.sagaFrame?.[0];
93
226
  if (frameAddr) {
94
- await this.push(frameAddr);
227
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH CONTROL: Saga Frame -> ${frameAddr} (I: 4a5b6c7d8e9f0a1b)`); }
228
+ await this.push([frameAddr]);
95
229
  // We inspect the local frame to find its immediate deps
96
230
  const frame = await this.getLocalIbGib(frameAddr);
97
231
  if (frame) {
98
232
  // Msg Stone
99
233
  const msgAddrs = frame.rel8ns?.[SYNC_MSG_REL8N_NAME];
100
234
  if (msgAddrs) {
101
- for (const addr of msgAddrs) await this.push(addr);
235
+ for (const addr of msgAddrs) {
236
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH CONTROL: Msg Stone -> ${addr} (I: 5b6c7d8e9f0a1b2c)`); }
237
+ await this.push([addr]);
238
+ }
102
239
  }
103
240
  // Identity
104
241
  const idAddrs = frame.rel8ns?.identity;
105
242
  if (idAddrs) {
106
- for (const addr of idAddrs) await this.push(addr);
243
+ for (const addr of idAddrs) {
244
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH CONTROL: Identity -> ${addr} (I: 6c7d8e9f0a1b2c3d)`); }
245
+ await this.push([addr]);
246
+ }
107
247
  }
108
248
  }
109
249
  }
@@ -111,7 +251,8 @@ export abstract class SyncPeer_V1 extends LightWitnessBase_V1<SyncPeerData_V1, S
111
251
  // C. Push Session Identity
112
252
  const sessionKeystoneAddr = context.rel8ns?.sessionKeystone?.[0];
113
253
  if (sessionKeystoneAddr) {
114
- await this.push(sessionKeystoneAddr);
254
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH CONTROL: Session Keystone -> ${sessionKeystoneAddr} (I: 7d8e9f0a1b2c3d4e)`); }
255
+ await this.push([sessionKeystoneAddr]);
115
256
  }
116
257
 
117
258
  // D. Push Payloads (Deep/Soft)
@@ -121,36 +262,60 @@ export abstract class SyncPeer_V1 extends LightWitnessBase_V1<SyncPeerData_V1, S
121
262
  // Or do we need to be explicit?
122
263
  // User said: "payloads should be flat... sync coordinator is the one doing the heavy lifting".
123
264
  // So we just push the addrs listed.
124
- const payloadAddrs = context.data?.[SYNC_SAGA_PAYLOAD_ADDRS_REL8N_NAME];
125
- if (payloadAddrs) {
126
- for (const addr of payloadAddrs) {
127
- await this.push(addr); // Assumed shallow/flat per user instruction
265
+
266
+ // 1. Control Payloads
267
+ // const controlAddrs = context.data?.[SYNC_SAGA_PAYLOAD_ADDRS_CONTROL];
268
+ // if (controlAddrs && controlAddrs.length > 0) {
269
+ // if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH CONTROL PAYLOADS (${controlAddrs.length}): ${controlAddrs.join(', ')} (I: 8e9f0a1b2c3d4e5f)`); }
270
+ // for (const addr of controlAddrs) {
271
+ // await this.push(addr);
272
+ // }
273
+ // }
274
+
275
+ // 2. Domain Payloads
276
+ const domainAddrs = context.data?.[SYNC_SAGA_PAYLOAD_ADDRS_DOMAIN];
277
+ if (domainAddrs && domainAddrs.length > 0) {
278
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH DOMAIN PAYLOADS (${domainAddrs.length}): ${domainAddrs.join(', ')} (I: 9f0a1b2c3d4e5f6a)`); }
279
+ for (const addr of domainAddrs) {
280
+ await this.push([addr]);
128
281
  }
129
282
  }
283
+
284
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH COMPLETE for context ${contextAddr} (I: a0b1c2d3e4f5a6b7)`); }
130
285
  }
131
286
 
132
287
  /**
133
288
  * Helper to orchestrate the PULL of a Context's dependency graph.
134
289
  */
135
290
  protected async pullContextGraph({ context }: { context: SyncSagaContextIbGib_V1 }): Promise<void> {
291
+ const lc = `[${SyncPeer_V1.name}][pullContextGraph]`;
292
+
136
293
  // A. Pull Context Envelope
137
294
  const contextAddr = getIbGibAddr({ ibGib: context });
138
- await this.pull(contextAddr);
295
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL Context Envelope <- ${contextAddr} (I: b1c2d3e4f5a6b7c8)`); }
296
+ await this.pull([contextAddr]);
139
297
 
140
298
  // B. Pull Saga Frame & Immediate Deps
141
299
  const frameAddr = context.rel8ns?.sagaFrame?.[0];
142
300
  if (frameAddr) {
143
- await this.pull(frameAddr);
301
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL CONTROL: Saga Frame <- ${frameAddr} (I: c2d3e4f5a6b7c8d9)`); }
302
+ await this.pull([frameAddr]);
144
303
  // Inspect REMOTE frame? We need to have pulled it first.
145
304
  const frame = await this.getLocalIbGib(frameAddr); // Should be local now
146
305
  if (frame) {
147
306
  const msgAddrs = frame.rel8ns?.[SYNC_MSG_REL8N_NAME];
148
307
  if (msgAddrs) {
149
- for (const addr of msgAddrs) await this.pull(addr);
308
+ for (const addr of msgAddrs) {
309
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL CONTROL: Msg Stone <- ${addr} (I: d3e4f5a6b7c8d9e0)`); }
310
+ await this.pull([addr]);
311
+ }
150
312
  }
151
313
  const idAddrs = frame.rel8ns?.identity;
152
314
  if (idAddrs) {
153
- for (const addr of idAddrs) await this.pull(addr);
315
+ for (const addr of idAddrs) {
316
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL CONTROL: Identity <- ${addr} (I: e4f5a6b7c8d9e0f1)`); }
317
+ await this.pull([addr]);
318
+ }
154
319
  }
155
320
  }
156
321
  }
@@ -158,21 +323,55 @@ export abstract class SyncPeer_V1 extends LightWitnessBase_V1<SyncPeerData_V1, S
158
323
  // C. Pull Session Identity
159
324
  const sessionKeystoneAddr = context.rel8ns?.sessionKeystone?.[0];
160
325
  if (sessionKeystoneAddr) {
161
- await this.pull(sessionKeystoneAddr);
326
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL CONTROL: Session Keystone <- ${sessionKeystoneAddr} (I: f5a6b7c8d9e0f1a2)`); }
327
+ await this.pull([sessionKeystoneAddr]);
162
328
  }
163
329
 
164
330
  // D. Pull Payloads
165
- const payloadAddrs = context.data?.[SYNC_SAGA_PAYLOAD_ADDRS_REL8N_NAME];
166
- if (payloadAddrs) {
167
- for (const addr of payloadAddrs) {
168
- await this.pull(addr);
331
+ // const controlAddrs = context.data?.[SYNC_SAGA_PAYLOAD_ADDRS_CONTROL];
332
+ // if (controlAddrs && controlAddrs.length > 0) {
333
+ // if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL CONTROL PAYLOADS (${controlAddrs.length}): ${controlAddrs.join(', ')} (I: a6b7c8d9e0f1a2b3)`); }
334
+ // for (const addr of controlAddrs) {
335
+ // await this.pull(addr);
336
+ // }
337
+ // }
338
+ const domainAddrs = context.data?.[SYNC_SAGA_PAYLOAD_ADDRS_DOMAIN];
339
+ if (domainAddrs && domainAddrs.length > 0) {
340
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL DOMAIN PAYLOADS (${domainAddrs.length}): ${domainAddrs.join(', ')} (I: b7c8d9e0f1a2b3c4)`); }
341
+ for (const addr of domainAddrs) {
342
+ await this.pull([addr]);
169
343
  }
170
344
  }
345
+
346
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL COMPLETE for context ${contextAddr} (I: c8d9e0f1a2b3c4d5)`); }
171
347
  }
172
348
 
173
349
  /**
174
350
  * Abstract accessor to get an IbGib from the Local space/store.
175
351
  * Needed for inspecting rela8ns during the graph walk.
352
+ *
353
+ * Default implementation simply looks in senderSpace
176
354
  */
177
- protected abstract getLocalIbGib(addr: string): Promise<IbGib_V1 | undefined>;
355
+ protected async getLocalIbGib(addr: string): Promise<IbGib_V1 | undefined> {
356
+ const lc = `${this.lc}[${this.getLocalIbGib.name}]`;
357
+ try {
358
+ if (logalot) { console.log(`${lc} starting... (I: 27b248cb9801eeb2386b71485389a826)`); }
359
+
360
+ console.warn(`${lc} possibly a bottleneck here, getLocalIbGib only gets a single ibgib... (W: 2fd448a435480e6b128f6b8bcbef4826)`);
361
+
362
+ const resGet = await getFromSpace({ space: this.senderSpace, addr });
363
+
364
+ if (resGet.success && resGet.ibGibs && resGet.ibGibs.length === 1) {
365
+ const ibGib = resGet.ibGibs[0];
366
+ return ibGib;
367
+ } else {
368
+ throw new Error(`couldn't get addr (${addr}) from local space ${this.senderSpace}. reason: ${resGet.errorMsg ?? 'unknown error (E: 926ef8bf4fcc299ab89dba34ea691a26)'} (E: d8a89807e471d3f8b938ab21df44cb26)`);
369
+ }
370
+ } catch (error) {
371
+ console.error(`${lc} ${extractErrorMsg(error)}`);
372
+ throw error;
373
+ } finally {
374
+ if (logalot) { console.log(`${lc} complete.`); }
375
+ }
376
+ }
178
377
  }
@@ -2,39 +2,48 @@
2
2
  * @module sync saga context helpers
3
3
  */
4
4
 
5
- import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
5
+ import { extractErrorMsg, getTimestamp } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
6
6
  import { Factory_V1 } from '@ibgib/ts-gib/dist/V1/factory.mjs';
7
7
  import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
8
8
  import { IbGib_V1, } from '@ibgib/ts-gib/dist/V1/types.mjs';
9
9
  import { Ib } from '@ibgib/ts-gib/dist/types.mjs';
10
+ import { validateIbGibIntrinsically } from '@ibgib/ts-gib/dist/V1/validate-helper.mjs';
10
11
 
12
+ import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs';
11
13
  import { SYNC_SAGA_CONTEXT_ATOM } from './sync-saga-context-constants.mjs';
12
- import { SYNC_SAGA_PAYLOAD_ADDRS_REL8N_NAME } from '../sync-constants.mjs';
14
+ import { SYNC_SAGA_PAYLOAD_ADDRS_DOMAIN } from '../sync-constants.mjs';
13
15
  import {
14
- SyncSagaContextCmd, SyncSagaContextData_V1, SyncSagaContextIbGib_V1,
15
- SyncSagaContextIb_V1, SyncSagaContextRel8ns_V1
16
+ SyncSagaContextData_V1, SyncSagaContextIbGib_V1, SyncSagaContextIb_V1,
17
+ SyncSagaContextRel8ns_V1,
16
18
  } from './sync-saga-context-types.mjs';
19
+ import { IbGibSpaceAny } from '../../witness/space/space-base-v1.mjs';
20
+ import { putInSpace, registerNewIbGib } from '../../witness/space/space-helper.mjs';
21
+ import { SyncIbGib_V1 } from '../sync-types.mjs';
22
+
23
+ const logalot = GLOBAL_LOG_A_LOT;
17
24
 
18
25
  /**
19
26
  * Options for creating a SyncSagaContext ibgib.
20
27
  */
21
28
  export interface CreateSyncSagaContextOptions {
22
- /**
23
- * Optional command string (e.g. 'process').
24
- */
25
- cmd?: SyncSagaContextCmd | string;
26
29
  /**
27
30
  * The main saga frame (Init, Ack, etc.).
28
31
  */
29
- sagaFrame: IbGib_V1;
32
+ sagaFrame: SyncIbGib_V1;
30
33
  /**
31
34
  * Session identity keystones.
32
35
  */
33
36
  sessionKeystones?: IbGib_V1[];
34
37
  /**
35
- * Payload addresses (strings) for bulk data.
38
+ * Domain payload ibgibs when the sync saga frame includes actual domain
39
+ * payloads to send, e.g., in a Delta frame.
36
40
  */
37
- payloadAddrs?: string[];
41
+ payloadIbGibsDomain?: IbGib_V1[];
42
+ /**
43
+ * we persist the context in the local/sender space (relative to our
44
+ * execution POV) right when we create it.
45
+ */
46
+ localSpace: IbGibSpaceAny;
38
47
  }
39
48
 
40
49
  /**
@@ -48,11 +57,12 @@ export async function getSyncSagaContextIb({
48
57
  const lc = `[${getSyncSagaContextIb.name}]`;
49
58
  try {
50
59
  if (!data) { throw new Error(`data required (E: 839d82138c234b3e89582855594191fe)`); }
51
- const cmd = data.cmd || 'default';
60
+
61
+ const payloadCount = data['@payloadAddrsDomain'] ? data['@payloadAddrsDomain'].length : 0;
52
62
  // sync_sagacontext cmd
53
63
  const ib = [
54
64
  SYNC_SAGA_CONTEXT_ATOM,
55
- cmd
65
+ payloadCount,
56
66
  ].join(' ');
57
67
 
58
68
  return ib;
@@ -73,17 +83,25 @@ export async function parseSyncSagaContextIb({
73
83
  const lc = `[${parseSyncSagaContextIb.name}]`;
74
84
  try {
75
85
  const parts = ib.split(' ');
76
- if (parts.length < 2) {
77
- throw new Error(`Invalid sync saga context ib. Expected at least 2 parts [atom cmd]. Got ${parts.length}. (E: 8b06c88820c74f5fb9f21f148459422a)`);
86
+ if (parts.length !== 2) {
87
+ throw new Error(`Invalid sync saga context ib. Expected at least 2 parts [atom, payloadCount]. Got ${parts.length}. (E: 8b06c88820c74f5fb9f21f148459422a)`);
78
88
  }
79
- const [atom, ...rest] = parts;
80
- const cmd = rest.join(' '); // cmd might have spaces if it's not a strict enum? but usually single word key.
89
+ const [
90
+ atom,
91
+ payloadCountStr,
92
+ ] = parts;
81
93
 
82
94
  if (atom !== SYNC_SAGA_CONTEXT_ATOM) {
83
95
  throw new Error(`Atom mismatch. Expected ${SYNC_SAGA_CONTEXT_ATOM}. Got ${atom}. (E: 67d71fc0f8d04840b178652309f471f5)`);
84
96
  }
85
97
 
86
- return { atom, cmd };
98
+ if (!payloadCountStr) { throw new Error(`invalid ib. second piece should be payloadCount but is falsy (E: 0bb7580793d800ca02227828a7294826)`); }
99
+ const payloadCount = parseInt(payloadCountStr);
100
+ if (isNaN(payloadCount)) {
101
+ throw new Error(`payloadCountStr is not a number. (E: 49457814282a44069871630987588722)`);
102
+ }
103
+
104
+ return { atom, payloadCount };
87
105
  } catch (error) {
88
106
  console.error(`${lc} ${extractErrorMsg(error)}`);
89
107
  throw error;
@@ -91,42 +109,121 @@ export async function parseSyncSagaContextIb({
91
109
  }
92
110
 
93
111
  /**
94
- * Creates a new SyncSagaContext ibGib stone.
112
+ * Creates new SyncSagaContext stone. Puts/registers in {@link localSpace}
113
+ * immediately after creation.
95
114
  *
96
115
  * @returns The context ibGib.
116
+ *
117
+ * ## notes
118
+ *
119
+ * the other ibgibs that are related to this context stone should already be
120
+ * put/registered in {@link localSpace}.
97
121
  */
98
122
  export async function createSyncSagaContext({
99
- cmd,
100
123
  sagaFrame,
101
124
  sessionKeystones,
102
- payloadAddrs,
125
+ payloadIbGibsDomain,
126
+ localSpace,
103
127
  }: CreateSyncSagaContextOptions): Promise<SyncSagaContextIbGib_V1> {
128
+ const lc = `[${createSyncSagaContext.name}]`;
129
+ try {
130
+ if (logalot) { console.log(`${lc} starting... (I: 6b87bee313e811d1d2fc90e87fbec826)`); }
104
131
 
105
- const data: SyncSagaContextData_V1 = {
106
- cmd,
107
- };
108
- if (payloadAddrs && payloadAddrs.length > 0) {
109
- data[SYNC_SAGA_PAYLOAD_ADDRS_REL8N_NAME] = payloadAddrs;
110
- }
132
+ const date = new Date();
133
+ const timestamp = getTimestamp(date);
134
+ const timestampMs = date.getMilliseconds();
111
135
 
112
- const rel8ns: SyncSagaContextRel8ns_V1 = {};
136
+ const data: SyncSagaContextData_V1 = {
137
+ timestamp,
138
+ timestampMs,
139
+ };
113
140
 
114
- if (sagaFrame) {
115
- rel8ns.sagaFrame = [getIbGibAddr(sagaFrame)];
116
- }
117
- if (sessionKeystones && sessionKeystones.length > 0) {
118
- rel8ns.sessionKeystone = sessionKeystones.map(getIbGibAddr);
119
- }
141
+ // Domain Payloads
142
+ const payloadAddrsDomain = payloadIbGibsDomain ?
143
+ payloadIbGibsDomain?.map(x => getIbGibAddr({ ibGib: x })) :
144
+ undefined;
145
+ if (payloadAddrsDomain && payloadAddrsDomain.length > 0) {
146
+ data[SYNC_SAGA_PAYLOAD_ADDRS_DOMAIN] = payloadAddrsDomain;
147
+ }
148
+
149
+ // rel8ns should always have saga frame, sometimes have keystone
150
+ const rel8ns: SyncSagaContextRel8ns_V1 = {
151
+ sagaFrame: [getIbGibAddr({ ibGib: sagaFrame })],
152
+ };
153
+ if (sessionKeystones && sessionKeystones.length > 0) {
154
+ rel8ns.sessionKeystone = sessionKeystones.map(x => getIbGibAddr({ ibGib: x }));
155
+ }
120
156
 
121
- // Generate standard ib
122
- const ib = await getSyncSagaContextIb({ data });
157
+ // Generate standard ib
158
+ const ib = await getSyncSagaContextIb({ data });
123
159
 
124
- const contextIbGib = await Factory_V1.stone<SyncSagaContextData_V1, SyncSagaContextRel8ns_V1>({
125
- parentPrimitiveIb: SYNC_SAGA_CONTEXT_ATOM,
126
- ib,
127
- data,
128
- rel8ns,
129
- });
160
+ const contextIbGib = await Factory_V1.stone<SyncSagaContextData_V1, SyncSagaContextRel8ns_V1>({
161
+ parentPrimitiveIb: SYNC_SAGA_CONTEXT_ATOM,
162
+ ib,
163
+ data,
164
+ rel8ns,
165
+ }) as SyncSagaContextIbGib_V1;
130
166
 
131
- return contextIbGib as SyncSagaContextIbGib_V1;
167
+ // put/register immediately. Note that contextIbGib at this point is
168
+ // pure DTO, i.e., only ib, gib, data, rel8ns props.
169
+ await putInSpace({ ibGib: contextIbGib, space: localSpace, });
170
+ await registerNewIbGib({
171
+ ibGib: contextIbGib,
172
+ space: localSpace,
173
+ fnBroadcast: undefined,
174
+ });
175
+
176
+ // Attach actual ibgibs for transport (not pure DTO now)
177
+ contextIbGib.sagaFrame = sagaFrame;
178
+ if (payloadIbGibsDomain && payloadIbGibsDomain.length > 0) {
179
+ contextIbGib.payloadIbGibsDomain = payloadIbGibsDomain;
180
+ }
181
+
182
+ return contextIbGib;
183
+ } catch (error) {
184
+ console.error(`${lc} ${extractErrorMsg(error)}`);
185
+ throw error;
186
+ } finally {
187
+ if (logalot) { console.log(`${lc} complete.`); }
188
+ }
132
189
  }
190
+
191
+ /**
192
+ * move to sync-peer-helpers.mts as a pure function?
193
+ */
194
+ export async function validateContext({ context }: { context: SyncSagaContextIbGib_V1 }): Promise<string[]> {
195
+ const lc = `[${validateContext.name}]`;
196
+ try {
197
+ if (logalot) { console.log(`${lc} starting... (I: 7797f8294bd8f7e5089cb722ad468226)`); }
198
+
199
+ const errors: string[] = [];
200
+
201
+ // validate just the context first. if this fails we return early.
202
+ const intrinsicErrors = await validateIbGibIntrinsically({ ibGib: context });
203
+ if (intrinsicErrors && intrinsicErrors.length > 0) {
204
+ intrinsicErrors.forEach(x => errors.push(x));
205
+ return errors; /* <<<< returns early */
206
+ }
207
+
208
+ if (context.sagaFrame) {
209
+
210
+ } else {
211
+ errors.push(`context.sagaFrame is falsy. (E: b4edd88f4963f493789f83b29ba2df26)`);
212
+ }
213
+
214
+ // validate the entire context graph intrinsically, including saga frame, stones, identity(s) (if applicable but not authentication, that will be done in a separate step)
215
+
216
+
217
+
218
+ // ensure the non-dto properties on the context object (i.e. props
219
+ // that are not ib, gib, data, or rel8ns) match the data in the
220
+ // context object
221
+
222
+ throw new Error(`not implemented (E: cb639658e1183fa6c8a4ebca023ba926)`);
223
+ } catch (error) {
224
+ console.error(`${lc} ${extractErrorMsg(error)}`);
225
+ throw error;
226
+ } finally {
227
+ if (logalot) { console.log(`${lc} complete.`); }
228
+ }
229
+ }