@x12i/memorix-service 3.4.0 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +1 -1
  2. package/dist/app.d.ts.map +1 -1
  3. package/dist/app.js +6 -0
  4. package/dist/app.js.map +1 -1
  5. package/dist/connector-ops/checkpoint-store.d.ts +7 -0
  6. package/dist/connector-ops/checkpoint-store.d.ts.map +1 -0
  7. package/dist/connector-ops/checkpoint-store.js +135 -0
  8. package/dist/connector-ops/checkpoint-store.js.map +1 -0
  9. package/dist/connector-ops/checkpoint-types.d.ts +46 -0
  10. package/dist/connector-ops/checkpoint-types.d.ts.map +1 -0
  11. package/dist/connector-ops/checkpoint-types.js +14 -0
  12. package/dist/connector-ops/checkpoint-types.js.map +1 -0
  13. package/dist/connector-ops/continuous-types.d.ts +141 -0
  14. package/dist/connector-ops/continuous-types.d.ts.map +1 -0
  15. package/dist/connector-ops/continuous-types.js +13 -0
  16. package/dist/connector-ops/continuous-types.js.map +1 -0
  17. package/dist/connector-ops/coordinator.d.ts +88 -0
  18. package/dist/connector-ops/coordinator.d.ts.map +1 -0
  19. package/dist/connector-ops/coordinator.js +174 -0
  20. package/dist/connector-ops/coordinator.js.map +1 -0
  21. package/dist/connector-ops/index.d.ts +6 -0
  22. package/dist/connector-ops/index.d.ts.map +1 -1
  23. package/dist/connector-ops/index.js +6 -0
  24. package/dist/connector-ops/index.js.map +1 -1
  25. package/dist/connector-ops/run-store.d.ts +16 -0
  26. package/dist/connector-ops/run-store.d.ts.map +1 -0
  27. package/dist/connector-ops/run-store.js +249 -0
  28. package/dist/connector-ops/run-store.js.map +1 -0
  29. package/dist/connector-ops/schedule-lease-store.d.ts +8 -0
  30. package/dist/connector-ops/schedule-lease-store.d.ts.map +1 -0
  31. package/dist/connector-ops/schedule-lease-store.js +318 -0
  32. package/dist/connector-ops/schedule-lease-store.js.map +1 -0
  33. package/dist/incremental-state/index.d.ts +3 -0
  34. package/dist/incremental-state/index.d.ts.map +1 -0
  35. package/dist/incremental-state/index.js +3 -0
  36. package/dist/incremental-state/index.js.map +1 -0
  37. package/dist/incremental-state/state-store.d.ts +7 -0
  38. package/dist/incremental-state/state-store.d.ts.map +1 -0
  39. package/dist/incremental-state/state-store.js +183 -0
  40. package/dist/incremental-state/state-store.js.map +1 -0
  41. package/dist/incremental-state/types.d.ts +104 -0
  42. package/dist/incremental-state/types.d.ts.map +1 -0
  43. package/dist/incremental-state/types.js +8 -0
  44. package/dist/incremental-state/types.js.map +1 -0
  45. package/dist/platform.d.ts +26 -1
  46. package/dist/platform.d.ts.map +1 -1
  47. package/dist/platform.js +111 -1
  48. package/dist/platform.js.map +1 -1
  49. package/dist/routes/continuous-acquisition.d.ts +5 -0
  50. package/dist/routes/continuous-acquisition.d.ts.map +1 -0
  51. package/dist/routes/continuous-acquisition.js +259 -0
  52. package/dist/routes/continuous-acquisition.js.map +1 -0
  53. package/dist/routes/incremental-state.d.ts +5 -0
  54. package/dist/routes/incremental-state.d.ts.map +1 -0
  55. package/dist/routes/incremental-state.js +173 -0
  56. package/dist/routes/incremental-state.js.map +1 -0
  57. package/package.json +1 -1
@@ -0,0 +1,259 @@
1
+ import { ServiceError } from "../errors.js";
2
+ import { hasSecretPayload, ACQUISITION_RUNS_DEFAULT_LIMIT, ACQUISITION_RUNS_MAX_LIMIT, computeStreamFreshness, validatePutCheckpoint, validatePutSchedule, } from "../connector-ops/index.js";
3
+ import { resolveConnectorOpsOrg } from "./connector-ops.js";
4
+ function requireStreamIds(params) {
5
+ const connectorId = typeof params.connectorId === "string" ? params.connectorId.trim() : "";
6
+ const instanceId = typeof params.instanceId === "string" ? params.instanceId.trim() : "";
7
+ const streamId = typeof params.streamId === "string" ? params.streamId.trim() : "";
8
+ if (!connectorId || !instanceId || !streamId) {
9
+ throw new ServiceError("VALIDATION_ERROR", "connectorId, instanceId, and streamId required", 400);
10
+ }
11
+ return { connectorId, instanceId, streamId };
12
+ }
13
+ function parseRunLimit(raw) {
14
+ const n = Number(raw ?? ACQUISITION_RUNS_DEFAULT_LIMIT);
15
+ if (!Number.isFinite(n) || n < 1)
16
+ return ACQUISITION_RUNS_DEFAULT_LIMIT;
17
+ return Math.min(Math.floor(n), ACQUISITION_RUNS_MAX_LIMIT);
18
+ }
19
+ function mapStoreErr(err) {
20
+ if (err instanceof ServiceError)
21
+ throw err;
22
+ throw new ServiceError("VALIDATION_ERROR", err instanceof Error ? err.message : String(err), 400);
23
+ }
24
+ /** INC-01 checkpoints + MX-CONT-01 schedule/lease/runs/freshness/tick. */
25
+ export async function registerContinuousAcquisitionRoutes(app, platform) {
26
+ const prefix = "/api/connector-ops/v1";
27
+ const streamBase = `${prefix}/orgs/:orgId/connectors/:connectorId/instances/:instanceId/streams/:streamId`;
28
+ app.get(`${streamBase}/checkpoint`, async (req) => {
29
+ const orgId = resolveConnectorOpsOrg(req);
30
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
31
+ const doc = await platform
32
+ .streamCheckpointsFor(orgId)
33
+ .get(connectorId, instanceId, streamId);
34
+ if (!doc) {
35
+ throw new ServiceError("NOT_FOUND", "no checkpoint for this stream", 404);
36
+ }
37
+ return doc;
38
+ });
39
+ app.put(`${streamBase}/checkpoint`, async (req) => {
40
+ const orgId = resolveConnectorOpsOrg(req);
41
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
42
+ const body = (req.body ?? {});
43
+ if (hasSecretPayload(body)) {
44
+ throw new ServiceError("PAYLOAD_REJECTED", "checkpoint payload must not contain secrets or response bodies", 400);
45
+ }
46
+ try {
47
+ validatePutCheckpoint(body);
48
+ return await platform
49
+ .streamCheckpointsFor(orgId)
50
+ .put(connectorId, instanceId, streamId, body);
51
+ }
52
+ catch (err) {
53
+ mapStoreErr(err);
54
+ }
55
+ });
56
+ app.delete(`${streamBase}/checkpoint`, async (req, reply) => {
57
+ const orgId = resolveConnectorOpsOrg(req);
58
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
59
+ await platform
60
+ .streamCheckpointsFor(orgId)
61
+ .clear(connectorId, instanceId, streamId);
62
+ return reply.code(204).send();
63
+ });
64
+ app.get(`${streamBase}/schedule`, async (req) => {
65
+ const orgId = resolveConnectorOpsOrg(req);
66
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
67
+ const doc = await platform
68
+ .streamSchedulesFor(orgId)
69
+ .get(connectorId, instanceId, streamId);
70
+ if (!doc) {
71
+ throw new ServiceError("NOT_FOUND", "no schedule for this stream", 404);
72
+ }
73
+ return doc;
74
+ });
75
+ app.put(`${streamBase}/schedule`, async (req) => {
76
+ const orgId = resolveConnectorOpsOrg(req);
77
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
78
+ const body = (req.body ?? {});
79
+ try {
80
+ validatePutSchedule(body);
81
+ return await platform
82
+ .streamSchedulesFor(orgId)
83
+ .put(connectorId, instanceId, streamId, body);
84
+ }
85
+ catch (err) {
86
+ mapStoreErr(err);
87
+ }
88
+ });
89
+ app.delete(`${streamBase}/schedule`, async (req, reply) => {
90
+ const orgId = resolveConnectorOpsOrg(req);
91
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
92
+ await platform
93
+ .streamSchedulesFor(orgId)
94
+ .clear(connectorId, instanceId, streamId);
95
+ return reply.code(204).send();
96
+ });
97
+ app.get(`${streamBase}/lease`, async (req) => {
98
+ const orgId = resolveConnectorOpsOrg(req);
99
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
100
+ const doc = await platform
101
+ .streamLeasesFor(orgId)
102
+ .get(connectorId, instanceId, streamId);
103
+ if (!doc) {
104
+ throw new ServiceError("NOT_FOUND", "no active lease", 404);
105
+ }
106
+ return doc;
107
+ });
108
+ app.post(`${streamBase}/lease`, async (req, reply) => {
109
+ const orgId = resolveConnectorOpsOrg(req);
110
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
111
+ const body = (req.body ?? {});
112
+ if (!body.ownerId?.trim()) {
113
+ throw new ServiceError("VALIDATION_ERROR", "ownerId required", 400);
114
+ }
115
+ try {
116
+ const lease = await platform
117
+ .streamLeasesFor(orgId)
118
+ .acquire(connectorId, instanceId, streamId, body);
119
+ return reply.code(201).send(lease);
120
+ }
121
+ catch (err) {
122
+ mapStoreErr(err);
123
+ }
124
+ });
125
+ app.post(`${streamBase}/lease/renew`, async (req) => {
126
+ const orgId = resolveConnectorOpsOrg(req);
127
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
128
+ const body = (req.body ?? {});
129
+ if (!body.ownerId?.trim() || !body.leaseId?.trim()) {
130
+ throw new ServiceError("VALIDATION_ERROR", "ownerId and leaseId required", 400);
131
+ }
132
+ try {
133
+ return await platform.streamLeasesFor(orgId).renew(connectorId, instanceId, streamId, {
134
+ ownerId: body.ownerId,
135
+ leaseId: body.leaseId,
136
+ ttlMs: body.ttlMs,
137
+ });
138
+ }
139
+ catch (err) {
140
+ mapStoreErr(err);
141
+ }
142
+ });
143
+ app.delete(`${streamBase}/lease`, async (req, reply) => {
144
+ const orgId = resolveConnectorOpsOrg(req);
145
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
146
+ const body = (req.body ?? {});
147
+ if (!body.ownerId?.trim() || !body.leaseId?.trim()) {
148
+ throw new ServiceError("VALIDATION_ERROR", "ownerId and leaseId required in body", 400);
149
+ }
150
+ try {
151
+ await platform.streamLeasesFor(orgId).release(connectorId, instanceId, streamId, { ownerId: body.ownerId, leaseId: body.leaseId });
152
+ return reply.code(204).send();
153
+ }
154
+ catch (err) {
155
+ mapStoreErr(err);
156
+ }
157
+ });
158
+ app.post(`${streamBase}/runs`, async (req, reply) => {
159
+ const orgId = resolveConnectorOpsOrg(req);
160
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
161
+ const body = (req.body ?? {});
162
+ try {
163
+ const run = await platform
164
+ .acquisitionRunsFor(orgId)
165
+ .start(connectorId, instanceId, streamId, body);
166
+ return reply.code(201).send(run);
167
+ }
168
+ catch (err) {
169
+ mapStoreErr(err);
170
+ }
171
+ });
172
+ app.post(`${streamBase}/runs/:runId/end`, async (req) => {
173
+ const orgId = resolveConnectorOpsOrg(req);
174
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
175
+ const runId = String(req.params.runId ?? "").trim();
176
+ if (!runId) {
177
+ throw new ServiceError("VALIDATION_ERROR", "runId required", 400);
178
+ }
179
+ const body = (req.body ?? {});
180
+ if (!body.outcome) {
181
+ throw new ServiceError("VALIDATION_ERROR", "outcome required", 400);
182
+ }
183
+ try {
184
+ return await platform
185
+ .acquisitionRunsFor(orgId)
186
+ .end(connectorId, instanceId, streamId, runId, body);
187
+ }
188
+ catch (err) {
189
+ mapStoreErr(err);
190
+ }
191
+ });
192
+ app.post(`${streamBase}/runs/:runId/cancel`, async (req) => {
193
+ const orgId = resolveConnectorOpsOrg(req);
194
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
195
+ const runId = String(req.params.runId ?? "").trim();
196
+ const run = await platform
197
+ .acquisitionRunsFor(orgId)
198
+ .requestCancel(connectorId, instanceId, streamId, runId);
199
+ if (!run) {
200
+ throw new ServiceError("NOT_FOUND", `run ${runId} not found`, 404);
201
+ }
202
+ const lease = await platform
203
+ .streamLeasesFor(orgId)
204
+ .get(connectorId, instanceId, streamId);
205
+ if (lease) {
206
+ try {
207
+ await platform.streamLeasesFor(orgId).release(connectorId, instanceId, streamId, { ownerId: lease.ownerId, leaseId: lease.leaseId });
208
+ }
209
+ catch {
210
+ /* ignore */
211
+ }
212
+ }
213
+ return run;
214
+ });
215
+ app.get(`${streamBase}/runs`, async (req) => {
216
+ const orgId = resolveConnectorOpsOrg(req);
217
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
218
+ const q = (req.query ?? {});
219
+ const limit = parseRunLimit(q.limit);
220
+ const runs = await platform
221
+ .acquisitionRunsFor(orgId)
222
+ .list(connectorId, instanceId, streamId, { limit });
223
+ return { orgId, connectorId, instanceId, streamId, runs, limit };
224
+ });
225
+ app.get(`${streamBase}/freshness`, async (req) => {
226
+ const orgId = resolveConnectorOpsOrg(req);
227
+ const { connectorId, instanceId, streamId } = requireStreamIds(req.params);
228
+ const schedule = await platform
229
+ .streamSchedulesFor(orgId)
230
+ .get(connectorId, instanceId, streamId);
231
+ const runs = await platform
232
+ .acquisitionRunsFor(orgId)
233
+ .list(connectorId, instanceId, streamId, { limit: 1 });
234
+ const lastSuccess = await platform
235
+ .acquisitionRunsFor(orgId)
236
+ .latestSuccess(connectorId, instanceId, streamId);
237
+ const lease = await platform
238
+ .streamLeasesFor(orgId)
239
+ .get(connectorId, instanceId, streamId);
240
+ return computeStreamFreshness({
241
+ orgId,
242
+ connectorId,
243
+ instanceId,
244
+ streamId,
245
+ schedule,
246
+ lastSuccess,
247
+ lastRun: runs[0] ?? null,
248
+ lease,
249
+ });
250
+ });
251
+ app.post(`${prefix}/orgs/:orgId/continuous-acquisition/tick`, async (req) => {
252
+ const orgId = resolveConnectorOpsOrg(req);
253
+ const body = (req.body ?? {});
254
+ return platform.tickContinuousAcquisitionForOrg(orgId, {
255
+ ownerId: body.ownerId,
256
+ });
257
+ });
258
+ }
259
+ //# sourceMappingURL=continuous-acquisition.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"continuous-acquisition.js","sourceRoot":"","sources":["../../src/routes/continuous-acquisition.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EACL,gBAAgB,EAChB,8BAA8B,EAC9B,0BAA0B,EAM1B,sBAAsB,EACtB,qBAAqB,EACrB,mBAAmB,GACpB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAE5D,SAAS,gBAAgB,CAAC,MAA+B;IAKvD,MAAM,WAAW,GACf,OAAO,MAAM,CAAC,WAAW,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,CAAC,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AI,CAAC,CAAC,CAAC,IAAI,IAAI;YACxB,KAAK;SACN,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,GAAG,CAAC,IAAI,CACN,GAAG,MAAM,0CAA0C,EACnD,KAAK,EAAE,GAAG,EAAE,EAAE;QACZ,MAAM,KAAK,GAAG,sBAAsB,CAAC,GAAG,CAAC,CAAC;QAC1C,MAAM,IAAI,GAAG,CAAC,GAAG,CAAC,IAAI,IAAI,EAAE,CAAyB,CAAC;QACtD,OAAO,QAAQ,CAAC,+BAA+B,CAAC,KAAK,EAAE;YACrD,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,CAAC,CAAC;IACL,CAAC,CACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,5 @@
1
+ import type { FastifyInstance } from "fastify";
2
+ import type { MemorixPlatform } from "../platform.js";
3
+ /** MX-INC-01 versioned state refs + CAS + apply-commit. */
4
+ export declare function registerIncrementalStateRoutes(app: FastifyInstance, platform: MemorixPlatform): Promise<void>;
5
+ //# sourceMappingURL=incremental-state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"incremental-state.d.ts","sourceRoot":"","sources":["../../src/routes/incremental-state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAoCtD,2DAA2D;AAC3D,wBAAsB,8BAA8B,CAClD,GAAG,EAAE,eAAe,EACpB,QAAQ,EAAE,eAAe,GACxB,OAAO,CAAC,IAAI,CAAC,CAwMf"}
@@ -0,0 +1,173 @@
1
+ import { ServiceError } from "../errors.js";
2
+ import { resolveConnectorOpsOrg } from "./connector-ops.js";
3
+ import { hasSecretPayload } from "../connector-ops/sanitize.js";
4
+ function requireScopeIds(params) {
5
+ const instanceId = typeof params.instanceId === "string" ? params.instanceId.trim() : "";
6
+ const scopeKey = typeof params.scopeKey === "string" ? params.scopeKey.trim() : "";
7
+ if (!instanceId || !scopeKey) {
8
+ throw new ServiceError("VALIDATION_ERROR", "instanceId and scopeKey required", 400);
9
+ }
10
+ return { instanceId, scopeKey };
11
+ }
12
+ function mapErr(err) {
13
+ if (err instanceof ServiceError)
14
+ throw err;
15
+ throw new ServiceError("VALIDATION_ERROR", err instanceof Error ? err.message : String(err), 400);
16
+ }
17
+ /** MX-INC-01 versioned state refs + CAS + apply-commit. */
18
+ export async function registerIncrementalStateRoutes(app, platform) {
19
+ const prefix = "/api/incremental-state/v1";
20
+ const scopeBase = `${prefix}/orgs/:orgId/instances/:instanceId/scopes/:scopeKey`;
21
+ app.get(`${scopeBase}`, async (req) => {
22
+ const orgId = resolveConnectorOpsOrg(req);
23
+ const { instanceId, scopeKey } = requireScopeIds(req.params);
24
+ const doc = await platform.incrementalStateFor(orgId).get(instanceId, scopeKey);
25
+ if (!doc) {
26
+ throw new ServiceError("NOT_FOUND", "no incremental state for scope", 404);
27
+ }
28
+ return doc;
29
+ });
30
+ app.get(`${scopeBase}/content`, async (req) => {
31
+ const orgId = resolveConnectorOpsOrg(req);
32
+ const { instanceId, scopeKey } = requireScopeIds(req.params);
33
+ const got = await platform
34
+ .incrementalStateFor(orgId)
35
+ .getContent(instanceId, scopeKey);
36
+ if (!got) {
37
+ throw new ServiceError("NOT_FOUND", "no incremental state content", 404);
38
+ }
39
+ return {
40
+ ...got.meta,
41
+ bodyBase64: got.body.toString("base64"),
42
+ };
43
+ });
44
+ app.post(`${scopeBase}/cas-publish`, async (req) => {
45
+ const orgId = resolveConnectorOpsOrg(req);
46
+ const { instanceId, scopeKey } = requireScopeIds(req.params);
47
+ const body = (req.body ?? {});
48
+ if (typeof body.expectedBaseVersion !== "number") {
49
+ throw new ServiceError("VALIDATION_ERROR", "expectedBaseVersion required", 400);
50
+ }
51
+ try {
52
+ return await platform
53
+ .incrementalStateFor(orgId)
54
+ .casPublish(instanceId, scopeKey, body);
55
+ }
56
+ catch (err) {
57
+ mapErr(err);
58
+ }
59
+ });
60
+ app.get(`${scopeBase}/apply-ledger`, async (req) => {
61
+ const orgId = resolveConnectorOpsOrg(req);
62
+ const { instanceId, scopeKey } = requireScopeIds(req.params);
63
+ const q = (req.query ?? {});
64
+ const limit = Math.min(200, Math.max(1, Number(q.limit ?? 50) || 50));
65
+ const items = await platform
66
+ .incrementalStateFor(orgId)
67
+ .listApplyLedger(instanceId, scopeKey, { limit });
68
+ return { orgId, instanceId, scopeKey, items, limit };
69
+ });
70
+ /**
71
+ * Atomic apply handoff: CAS-publish snapshot then commit stream checkpoint.
72
+ * Failure at either step → no checkpoint advance (and no silent state bump
73
+ * beyond what CAS already rejected).
74
+ */
75
+ app.post(`${scopeBase}/apply-commit`, async (req) => {
76
+ const orgId = resolveConnectorOpsOrg(req);
77
+ const { instanceId, scopeKey } = requireScopeIds(req.params);
78
+ const body = (req.body ?? {});
79
+ if (!body.runId?.trim()) {
80
+ throw new ServiceError("VALIDATION_ERROR", "runId required", 400);
81
+ }
82
+ if (typeof body.expectedBaseVersion !== "number") {
83
+ throw new ServiceError("VALIDATION_ERROR", "expectedBaseVersion required", 400);
84
+ }
85
+ if (!body.checkpoint?.connectorId || !body.checkpoint?.streamId) {
86
+ throw new ServiceError("VALIDATION_ERROR", "checkpoint.connectorId and checkpoint.streamId required", 400);
87
+ }
88
+ if (hasSecretPayload({ ...body, bodyBase64: undefined })) {
89
+ throw new ServiceError("PAYLOAD_REJECTED", "apply-commit must not contain secret field keys", 400);
90
+ }
91
+ const store = platform.incrementalStateFor(orgId);
92
+ const existing = await store.getApplyByRunId(instanceId, scopeKey, body.runId);
93
+ if (existing?.outcome === "ok") {
94
+ const state = await store.get(instanceId, scopeKey);
95
+ const checkpoint = await platform
96
+ .streamCheckpointsFor(orgId)
97
+ .get(body.checkpoint.connectorId, body.checkpoint.instanceId || instanceId, body.checkpoint.streamId);
98
+ if (state && checkpoint) {
99
+ return { state, checkpoint, applyLedger: existing, replayed: true };
100
+ }
101
+ }
102
+ const before = await store.get(instanceId, scopeKey);
103
+ const baseBefore = before?.baseVersion ?? 0;
104
+ let state;
105
+ try {
106
+ state = await store.casPublish(instanceId, scopeKey, {
107
+ expectedBaseVersion: body.expectedBaseVersion,
108
+ bodyBase64: body.bodyBase64,
109
+ contentType: body.contentType,
110
+ contentHash: body.contentHash,
111
+ producerRunId: body.runId,
112
+ incomplete: body.incomplete,
113
+ shrinkGuardFailed: body.shrinkGuardFailed,
114
+ tombstones: body.tombstones,
115
+ });
116
+ }
117
+ catch (err) {
118
+ await store.recordApply({
119
+ orgId,
120
+ instanceId,
121
+ scopeKey,
122
+ runId: body.runId,
123
+ at: new Date().toISOString(),
124
+ outcome: "conflict",
125
+ baseVersionBefore: baseBefore,
126
+ errorClass: err instanceof ServiceError ? err.code : "cas_failed",
127
+ errorMessage: err instanceof Error ? err.message : String(err),
128
+ });
129
+ mapErr(err);
130
+ }
131
+ try {
132
+ const checkpoint = await platform.streamCheckpointsFor(orgId).put(body.checkpoint.connectorId, body.checkpoint.instanceId || instanceId, body.checkpoint.streamId, {
133
+ method: body.checkpoint.method,
134
+ watermark: body.checkpoint.watermark,
135
+ revision: body.checkpoint.revision,
136
+ fullScanHash: body.checkpoint.fullScanHash,
137
+ cursor: body.checkpoint.cursor,
138
+ contentHash: body.checkpoint.contentHash,
139
+ producerRunId: body.runId,
140
+ expectedCheckpointRevision: body.checkpoint.expectedCheckpointRevision,
141
+ });
142
+ const applyLedger = await store.recordApply({
143
+ orgId,
144
+ instanceId,
145
+ scopeKey,
146
+ runId: body.runId,
147
+ at: new Date().toISOString(),
148
+ outcome: "ok",
149
+ baseVersionBefore: baseBefore,
150
+ baseVersionAfter: state.baseVersion,
151
+ });
152
+ return { state, checkpoint, applyLedger, replayed: false };
153
+ }
154
+ catch (err) {
155
+ await store.recordApply({
156
+ orgId,
157
+ instanceId,
158
+ scopeKey,
159
+ runId: body.runId,
160
+ at: new Date().toISOString(),
161
+ outcome: "failed",
162
+ baseVersionBefore: baseBefore,
163
+ baseVersionAfter: state.baseVersion,
164
+ errorClass: "checkpoint_commit_failed",
165
+ errorMessage: err instanceof Error ? err.message : String(err),
166
+ });
167
+ // State already published; checkpoint not advanced — resume uses new
168
+ // baseVersion; host must not invent a further checkpoint skip-ahead.
169
+ mapErr(err);
170
+ }
171
+ });
172
+ }
173
+ //# sourceMappingURL=incremental-state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"incremental-state.js","sourceRoot":"","sources":["../../src/routes/incremental-state.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAK5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAEhE,SAAS,eAAe,CAAC,MAA+B;IAItD,MAAM,UAAU,GACd,OAAO,MAAM,CAAC,UAAU,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IACxE,MAAM,QAAQ,GACZ,OAAO,MAAM,CAAC,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IACpE,IAAI,CAAC,UAAU,IAAI,CAAC,QAAQ,EAAE,CAAC;QAC7B,MAAM,IAAI,YAAY,CACpB,kBAAkB,EAClB,kCAAkC,EAClC,GAAG,CACJ,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC;AAClC,CAAC;AAED,SAAS,MAAM,CAAC,GAAY;IAC1B,IAAI,GAAG,YAAY,YAAY;QAAE,MAAM,GAAG,CAAC;IAC3C,MAAM,IAAI,YAAY,CACpB,kBAAkB,EAClB,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAChD,GA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C,aAAa,EAAE,IAAI,CAAC,KAAK;gBACzB,0BAA0B,EAAE,IAAI,CAAC,UAAU,CAAC,0BAA0B;aACvE,CACF,CAAC;YACF,MAAM,WAAW,GAAG,MAAM,KAAK,CAAC,WAAW,CAAC;gBAC1C,KAAK;gBACL,UAAU;gBACV,QAAQ;gBACR,KAAK,EAAE,IAAI,CAAC,KAAK;gBACjB,EAAE,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;gBAC5B,OAAO,EAAE,IAAI;gBACb,iBAAiB,EAAE,UAAU;gBAC7B,gBAAgB,EAAE,KAAK,CAAC,WAAW;aACpC,CAAC,CAAC;YACH,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;QAC7D,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,KAAK,CAAC,WAAW,CAAC;gBACtB,KAAK;gBACL,UAAU;gBACV,QAAQ;gBACR,KAAK,EAAE,IAAI,CAAC,KAAK;gBACjB,EAAE,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;gBAC5B,OAAO,EAAE,QAAQ;gBACjB,iBAAiB,EAAE,UAAU;gBAC7B,gBAAgB,EAAE,KAAK,CAAC,WAAW;gBACnC,UAAU,EAAE,0BAA0B;gBACtC,YAAY,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC;aAC/D,CAAC,CAAC;YACH,qEAAqE;YACrE,qEAAqE;YACrE,MAAM,CAAC,GAAG,CAAC,CAAC;QACd,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x12i/memorix-service",
3
- "version": "3.4.0",
3
+ "version": "3.6.0",
4
4
  "description": "Memorix production service composition root — one versioned API over accepted packages.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",