@x12i/countex 1.2.0 → 1.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 (112) hide show
  1. package/README.md +80 -18
  2. package/dist/backup/manifest.d.ts +70 -0
  3. package/dist/backup/manifest.d.ts.map +1 -0
  4. package/dist/backup/manifest.js +131 -0
  5. package/dist/backup/manifest.js.map +1 -0
  6. package/dist/backup/snapshot-encoding.d.ts +8 -0
  7. package/dist/backup/snapshot-encoding.d.ts.map +1 -0
  8. package/dist/backup/snapshot-encoding.js +27 -0
  9. package/dist/backup/snapshot-encoding.js.map +1 -0
  10. package/dist/cli.js +16 -0
  11. package/dist/cli.js.map +1 -1
  12. package/dist/core/countex.d.ts +79 -1
  13. package/dist/core/countex.d.ts.map +1 -1
  14. package/dist/core/countex.js +254 -49
  15. package/dist/core/countex.js.map +1 -1
  16. package/dist/core/extraction.d.ts +1 -5
  17. package/dist/core/extraction.d.ts.map +1 -1
  18. package/dist/core/extraction.js.map +1 -1
  19. package/dist/core/mapping.d.ts +5 -0
  20. package/dist/core/mapping.d.ts.map +1 -1
  21. package/dist/core/mapping.js +27 -0
  22. package/dist/core/mapping.js.map +1 -1
  23. package/dist/index.d.ts +5 -2
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +15 -1
  26. package/dist/index.js.map +1 -1
  27. package/dist/query/formatter.d.ts +2 -2
  28. package/dist/query/formatter.d.ts.map +1 -1
  29. package/dist/query/formatter.js +9 -6
  30. package/dist/query/formatter.js.map +1 -1
  31. package/dist/query/planner.d.ts +15 -1
  32. package/dist/query/planner.d.ts.map +1 -1
  33. package/dist/query/planner.js +152 -3
  34. package/dist/query/planner.js.map +1 -1
  35. package/dist/simulation/context.d.ts +5 -0
  36. package/dist/simulation/context.d.ts.map +1 -0
  37. package/dist/simulation/context.js +56 -0
  38. package/dist/simulation/context.js.map +1 -0
  39. package/dist/simulation/events.d.ts +3 -0
  40. package/dist/simulation/events.d.ts.map +1 -0
  41. package/dist/simulation/events.js +67 -0
  42. package/dist/simulation/events.js.map +1 -0
  43. package/dist/simulation/index.d.ts +7 -0
  44. package/dist/simulation/index.d.ts.map +1 -0
  45. package/dist/simulation/index.js +16 -0
  46. package/dist/simulation/index.js.map +1 -0
  47. package/dist/simulation/prng.d.ts +4 -0
  48. package/dist/simulation/prng.d.ts.map +1 -0
  49. package/dist/simulation/prng.js +18 -0
  50. package/dist/simulation/prng.js.map +1 -0
  51. package/dist/simulation/resource-estimate.d.ts +6 -0
  52. package/dist/simulation/resource-estimate.d.ts.map +1 -0
  53. package/dist/simulation/resource-estimate.js +11 -0
  54. package/dist/simulation/resource-estimate.js.map +1 -0
  55. package/dist/simulation/simulator.d.ts +15 -0
  56. package/dist/simulation/simulator.d.ts.map +1 -0
  57. package/dist/simulation/simulator.js +67 -0
  58. package/dist/simulation/simulator.js.map +1 -0
  59. package/dist/simulation/tracker.d.ts +8 -0
  60. package/dist/simulation/tracker.d.ts.map +1 -0
  61. package/dist/simulation/tracker.js +18 -0
  62. package/dist/simulation/tracker.js.map +1 -0
  63. package/dist/simulation/types.d.ts +49 -0
  64. package/dist/simulation/types.d.ts.map +1 -0
  65. package/dist/simulation/types.js +21 -0
  66. package/dist/simulation/types.js.map +1 -0
  67. package/dist/storage/coverage.d.ts +13 -0
  68. package/dist/storage/coverage.d.ts.map +1 -0
  69. package/dist/storage/coverage.js +37 -0
  70. package/dist/storage/coverage.js.map +1 -0
  71. package/dist/storage/memory.d.ts +23 -5
  72. package/dist/storage/memory.d.ts.map +1 -1
  73. package/dist/storage/memory.js +162 -3
  74. package/dist/storage/memory.js.map +1 -1
  75. package/dist/storage/mongo.d.ts +5 -0
  76. package/dist/storage/mongo.d.ts.map +1 -1
  77. package/dist/storage/mongo.js +68 -1
  78. package/dist/storage/mongo.js.map +1 -1
  79. package/dist/storage/redis.d.ts +40 -11
  80. package/dist/storage/redis.d.ts.map +1 -1
  81. package/dist/storage/redis.js +202 -44
  82. package/dist/storage/redis.js.map +1 -1
  83. package/dist/types/index.d.ts +165 -7
  84. package/dist/types/index.d.ts.map +1 -1
  85. package/docs/DEPLOYMENT.md +125 -3
  86. package/docs/countex_gap_analysis_complete_checklist.md +1340 -0
  87. package/docs/guides/README.md +5 -0
  88. package/docs/guides/adapter-mongo.md +12 -0
  89. package/docs/guides/adapter-redis.md +46 -4
  90. package/docs/guides/backup-export.md +106 -10
  91. package/docs/guides/cli.md +8 -0
  92. package/docs/guides/counter-maps.md +14 -0
  93. package/docs/guides/insights-overview.md +6 -0
  94. package/docs/guides/measures.md +4 -0
  95. package/docs/guides/preview.md +92 -0
  96. package/docs/guides/querying.md +70 -0
  97. package/docs/guides/simulation-profiles.md +33 -0
  98. package/docs/guides/simulation-resource-estimation.md +30 -0
  99. package/docs/guides/simulation.md +36 -0
  100. package/docs/internals/README.md +1 -0
  101. package/docs/internals/internal-dimension-hash.md +34 -0
  102. package/docs/internals/internal-storage-mongo.md +24 -4
  103. package/docs/internals/internal-storage-redis.md +41 -9
  104. package/docs/use-cases/README.md +56 -0
  105. package/docs/use-cases/audit-security-log-counts.md +550 -0
  106. package/docs/use-cases/llm-cost-and-token-metering.md +255 -0
  107. package/docs/use-cases/multi-tenant-isolation.md +203 -0
  108. package/docs/use-cases/ops-dashboard-daily-rollups.md +255 -0
  109. package/docs/use-cases/processing-workflow-activity.md +165 -0
  110. package/docs/use-cases/product-feature-usage.md +240 -0
  111. package/docs/use-cases/webhook-idempotent-ingestion.md +188 -0
  112. package/package.json +18 -2
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prng.d.ts","sourceRoot":"","sources":["../../src/simulation/prng.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,MAAM,CAQ1D;AAED,wBAAgB,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,MAAM,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,CAAC,CAEtD"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSeededRng = createSeededRng;
4
+ exports.pick = pick;
5
+ /** Deterministic PRNG (mulberry32). */
6
+ function createSeededRng(seed) {
7
+ let t = seed >>> 0;
8
+ return () => {
9
+ t += 0x6d2b79f5;
10
+ let r = Math.imul(t ^ (t >>> 15), 1 | t);
11
+ r ^= r + Math.imul(r ^ (r >>> 7), 61 | r);
12
+ return ((r ^ (r >>> 14)) >>> 0) / 4294967296;
13
+ };
14
+ }
15
+ function pick(rng, arr) {
16
+ return arr[Math.floor(rng() * arr.length)];
17
+ }
18
+ //# sourceMappingURL=prng.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prng.js","sourceRoot":"","sources":["../../src/simulation/prng.ts"],"names":[],"mappings":";;AACA,0CAQC;AAED,oBAEC;AAbD,uCAAuC;AACvC,SAAgB,eAAe,CAAC,IAAY;IAC1C,IAAI,CAAC,GAAG,IAAI,KAAK,CAAC,CAAC;IACnB,OAAO,GAAG,EAAE;QACV,CAAC,IAAI,UAAU,CAAC;QAChB,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QACzC,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC;QAC1C,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,UAAU,CAAC;IAC/C,CAAC,CAAC;AACJ,CAAC;AAED,SAAgB,IAAI,CAAI,GAAiB,EAAE,GAAQ;IACjD,OAAO,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,CAAE,CAAC;AAC9C,CAAC"}
@@ -0,0 +1,6 @@
1
+ import type { SimulationReport } from "./types.js";
2
+ /** Rough cell cardinality upper bound from simulated raw increments (no dedupe across buckets). */
3
+ export declare function estimateCellsFromReport(report: SimulationReport, timeframesPerMap: number): {
4
+ cellsUpperBound: number;
5
+ };
6
+ //# sourceMappingURL=resource-estimate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resource-estimate.d.ts","sourceRoot":"","sources":["../../src/simulation/resource-estimate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD,mGAAmG;AACnG,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,gBAAgB,EACxB,gBAAgB,EAAE,MAAM,GACvB;IAAE,eAAe,EAAE,MAAM,CAAA;CAAE,CAK7B"}
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.estimateCellsFromReport = estimateCellsFromReport;
4
+ /** Rough cell cardinality upper bound from simulated raw increments (no dedupe across buckets). */
5
+ function estimateCellsFromReport(report, timeframesPerMap) {
6
+ const tf = Math.max(1, timeframesPerMap);
7
+ return {
8
+ cellsUpperBound: report.rawIncrementsEstimated * tf,
9
+ };
10
+ }
11
+ //# sourceMappingURL=resource-estimate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resource-estimate.js","sourceRoot":"","sources":["../../src/simulation/resource-estimate.ts"],"names":[],"mappings":";;AAGA,0DAQC;AATD,mGAAmG;AACnG,SAAgB,uBAAuB,CACrC,MAAwB,EACxB,gBAAwB;IAExB,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC;IACzC,OAAO;QACL,eAAe,EAAE,MAAM,CAAC,sBAAsB,GAAG,EAAE;KACpD,CAAC;AACJ,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { Countex } from "../core/countex.js";
2
+ import type { CounterMap } from "../types/index.js";
3
+ import type { CountexSimulatorOptions, SimulationReport } from "./types.js";
4
+ export declare class CountexSimulator {
5
+ private readonly opts;
6
+ constructor(opts: CountexSimulatorOptions);
7
+ /** Yield synthetic JSON events for custom pipelines or tests. */
8
+ generateEvents(count: number): Generator<unknown, void, unknown>;
9
+ /** Dry-run extraction fan-out against a map (no storage). */
10
+ estimateFanout(map: CounterMap, count: number): SimulationReport;
11
+ /** Ingest synthetic stream into a Countex instance. */
12
+ runIngest(countex: Countex, count: number): Promise<SimulationReport>;
13
+ }
14
+ export declare function createCountexSimulator(opts: CountexSimulatorOptions): CountexSimulator;
15
+ //# sourceMappingURL=simulator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"simulator.d.ts","sourceRoot":"","sources":["../../src/simulation/simulator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAElD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAGpD,OAAO,KAAK,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE5E,qBAAa,gBAAgB;IACf,OAAO,CAAC,QAAQ,CAAC,IAAI;gBAAJ,IAAI,EAAE,uBAAuB;IAE1D,iEAAiE;IAChE,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC;IASjE,6DAA6D;IAC7D,cAAc,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,GAAG,gBAAgB;IAqBhE,uDAAuD;IACjD,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;CAgB5E;AAED,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,uBAAuB,GAAG,gBAAgB,CAEtF"}
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CountexSimulator = void 0;
4
+ exports.createCountexSimulator = createCountexSimulator;
5
+ const extraction_js_1 = require("../core/extraction.js");
6
+ const context_js_1 = require("./context.js");
7
+ const events_js_1 = require("./events.js");
8
+ class CountexSimulator {
9
+ opts;
10
+ constructor(opts) {
11
+ this.opts = opts;
12
+ }
13
+ /** Yield synthetic JSON events for custom pipelines or tests. */
14
+ *generateEvents(count) {
15
+ const seed = this.opts.seed ?? 1;
16
+ const scopeId = this.opts.scopeId ?? "sim_scope";
17
+ const ctx = (0, context_js_1.buildDrawContext)(seed, this.opts.profile, scopeId);
18
+ for (let i = 0; i < count; i++) {
19
+ yield (0, events_js_1.emitSyntheticEvent)(this.opts.profile, ctx, i);
20
+ }
21
+ }
22
+ /** Dry-run extraction fan-out against a map (no storage). */
23
+ estimateFanout(map, count) {
24
+ const start = Date.now();
25
+ let raw = 0;
26
+ let skipped = 0;
27
+ let emitted = 0;
28
+ for (const ev of this.generateEvents(count)) {
29
+ emitted++;
30
+ const r = (0, extraction_js_1.extractIncrements)(map, ev);
31
+ if (r.skipped)
32
+ skipped++;
33
+ else
34
+ raw += r.increments.length;
35
+ }
36
+ return {
37
+ profileId: this.opts.profile.id,
38
+ seed: this.opts.seed ?? 1,
39
+ eventsEmitted: emitted,
40
+ rawIncrementsEstimated: raw,
41
+ skippedEvents: skipped,
42
+ durationMs: Date.now() - start,
43
+ };
44
+ }
45
+ /** Ingest synthetic stream into a Countex instance. */
46
+ async runIngest(countex, count) {
47
+ const start = Date.now();
48
+ let emitted = 0;
49
+ for (const ev of this.generateEvents(count)) {
50
+ await countex.ingest(ev);
51
+ emitted++;
52
+ }
53
+ return {
54
+ profileId: this.opts.profile.id,
55
+ seed: this.opts.seed ?? 1,
56
+ eventsEmitted: emitted,
57
+ rawIncrementsEstimated: 0,
58
+ skippedEvents: 0,
59
+ durationMs: Date.now() - start,
60
+ };
61
+ }
62
+ }
63
+ exports.CountexSimulator = CountexSimulator;
64
+ function createCountexSimulator(opts) {
65
+ return new CountexSimulator(opts);
66
+ }
67
+ //# sourceMappingURL=simulator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"simulator.js","sourceRoot":"","sources":["../../src/simulation/simulator.ts"],"names":[],"mappings":";;;AA6DA,wDAEC;AA9DD,yDAA0D;AAE1D,6CAAgD;AAChD,2CAAiD;AAGjD,MAAa,gBAAgB;IACE;IAA7B,YAA6B,IAA6B;QAA7B,SAAI,GAAJ,IAAI,CAAyB;IAAG,CAAC;IAE9D,iEAAiE;IACjE,CAAC,cAAc,CAAC,KAAa;QAC3B,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC;QACjC,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,IAAI,WAAW,CAAC;QACjD,MAAM,GAAG,GAAG,IAAA,6BAAgB,EAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAC/D,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;YAC/B,MAAM,IAAA,8BAAkB,EAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;QACtD,CAAC;IACH,CAAC;IAED,6DAA6D;IAC7D,cAAc,CAAC,GAAe,EAAE,KAAa;QAC3C,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACzB,IAAI,GAAG,GAAG,CAAC,CAAC;QACZ,IAAI,OAAO,GAAG,CAAC,CAAC;QAChB,IAAI,OAAO,GAAG,CAAC,CAAC;QAChB,KAAK,MAAM,EAAE,IAAI,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5C,OAAO,EAAE,CAAC;YACV,MAAM,CAAC,GAAG,IAAA,iCAAiB,EAAC,GAAG,EAAE,EAAE,CAAC,CAAC;YACrC,IAAI,CAAC,CAAC,OAAO;gBAAE,OAAO,EAAE,CAAC;;gBACpB,GAAG,IAAI,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC;QAClC,CAAC;QACD,OAAO;YACL,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE;YAC/B,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC;YACzB,aAAa,EAAE,OAAO;YACtB,sBAAsB,EAAE,GAAG;YAC3B,aAAa,EAAE,OAAO;YACtB,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK;SAC/B,CAAC;IACJ,CAAC;IAED,uDAAuD;IACvD,KAAK,CAAC,SAAS,CAAC,OAAgB,EAAE,KAAa;QAC7C,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACzB,IAAI,OAAO,GAAG,CAAC,CAAC;QAChB,KAAK,MAAM,EAAE,IAAI,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5C,MAAM,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;YACzB,OAAO,EAAE,CAAC;QACZ,CAAC;QACD,OAAO;YACL,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE;YAC/B,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC;YACzB,aAAa,EAAE,OAAO;YACtB,sBAAsB,EAAE,CAAC;YACzB,aAAa,EAAE,CAAC;YAChB,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK;SAC/B,CAAC;IACJ,CAAC;CACF;AApDD,4CAoDC;AAED,SAAgB,sBAAsB,CAAC,IAA6B;IAClE,OAAO,IAAI,gBAAgB,CAAC,IAAI,CAAC,CAAC;AACpC,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { CounterMap } from "../types/index.js";
2
+ /** Running estimate of logical increments for acceptance tests / CI. */
3
+ export declare class ExpectedAggregateTracker {
4
+ totalRawIncrements: number;
5
+ skippedEvents: number;
6
+ observe(map: CounterMap, event: unknown): void;
7
+ }
8
+ //# sourceMappingURL=tracker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracker.d.ts","sourceRoot":"","sources":["../../src/simulation/tracker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAGpD,wEAAwE;AACxE,qBAAa,wBAAwB;IACnC,kBAAkB,SAAK;IACvB,aAAa,SAAK;IAElB,OAAO,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI;CAK/C"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExpectedAggregateTracker = void 0;
4
+ const extraction_js_1 = require("../core/extraction.js");
5
+ /** Running estimate of logical increments for acceptance tests / CI. */
6
+ class ExpectedAggregateTracker {
7
+ totalRawIncrements = 0;
8
+ skippedEvents = 0;
9
+ observe(map, event) {
10
+ const r = (0, extraction_js_1.extractIncrements)(map, event);
11
+ if (r.skipped)
12
+ this.skippedEvents++;
13
+ else
14
+ this.totalRawIncrements += r.increments.length;
15
+ }
16
+ }
17
+ exports.ExpectedAggregateTracker = ExpectedAggregateTracker;
18
+ //# sourceMappingURL=tracker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracker.js","sourceRoot":"","sources":["../../src/simulation/tracker.ts"],"names":[],"mappings":";;;AACA,yDAA0D;AAE1D,wEAAwE;AACxE,MAAa,wBAAwB;IACnC,kBAAkB,GAAG,CAAC,CAAC;IACvB,aAAa,GAAG,CAAC,CAAC;IAElB,OAAO,CAAC,GAAe,EAAE,KAAc;QACrC,MAAM,CAAC,GAAG,IAAA,iCAAiB,EAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACxC,IAAI,CAAC,CAAC,OAAO;YAAE,IAAI,CAAC,aAAa,EAAE,CAAC;;YAC/B,IAAI,CAAC,kBAAkB,IAAI,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC;IACtD,CAAC;CACF;AATD,4DASC"}
@@ -0,0 +1,49 @@
1
+ import type { CounterMap } from "../types/index.js";
2
+ export type SimulationProfileId = "audit-log" | "product-activity" | "processing-activity" | "llm-usage" | "custom";
3
+ export interface SimulationPoolsConfig {
4
+ tenants?: number;
5
+ actors?: number;
6
+ resources?: number;
7
+ metrics?: number;
8
+ models?: number;
9
+ jobs?: number;
10
+ tasks?: number;
11
+ statuses?: number;
12
+ }
13
+ export interface SimulationProfile {
14
+ id: SimulationProfileId;
15
+ /** Caps for synthetic key cardinality (defaults per profile). */
16
+ pools?: SimulationPoolsConfig;
17
+ /** Required when `id` is `custom`. */
18
+ eventFactory?: (ctx: SimulationDrawContext, tick: number) => unknown;
19
+ }
20
+ export interface SimulationDrawContext {
21
+ /** Deterministic [0, 1) */
22
+ rng: () => number;
23
+ scopeId: string;
24
+ tenantIds: string[];
25
+ actorIds: string[];
26
+ resourceIds: string[];
27
+ metricIds: string[];
28
+ modelIds: string[];
29
+ jobIds: string[];
30
+ taskIds: string[];
31
+ statuses: string[];
32
+ }
33
+ export interface SimulationReport {
34
+ profileId: SimulationProfileId;
35
+ seed: number;
36
+ eventsEmitted: number;
37
+ /** Sum of `extractIncrements(...).increments.length` across emitted events (dry-run). */
38
+ rawIncrementsEstimated: number;
39
+ skippedEvents: number;
40
+ durationMs: number;
41
+ }
42
+ export interface CountexSimulatorOptions {
43
+ profile: SimulationProfile;
44
+ seed?: number;
45
+ scopeId?: string;
46
+ }
47
+ /** Minimal demo map usable with bundled profiles (audit-style labels). */
48
+ export declare const DEMO_SIMULATION_MAP: CounterMap;
49
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/simulation/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAEpD,MAAM,MA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ACtB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,uBAAuB;IACtC,OAAO,EAAE,iBAAiB,CAAC;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,0EAA0E;AAC1E,eAAO,MAAM,mBAAmB,EAAE,UAejC,CAAC"}
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEMO_SIMULATION_MAP = void 0;
4
+ /** Minimal demo map usable with bundled profiles (audit-style labels). */
5
+ exports.DEMO_SIMULATION_MAP = {
6
+ counterId: "sim_demo",
7
+ scope: { path: "$.tenant.id" },
8
+ eventTime: { path: "$.event.createdAt" },
9
+ subjects: [
10
+ { type: "actor", path: "$.actor.id", required: true },
11
+ { type: "resource", path: "$.resource.id", required: false },
12
+ ],
13
+ metrics: [{ path: "$.labels[*]", required: true }],
14
+ dimensions: [
15
+ { name: "operation", path: "$.event.operation" },
16
+ { name: "situation", path: "$.event.situation" },
17
+ ],
18
+ delta: { default: 1 },
19
+ timeframes: ["day"],
20
+ };
21
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/simulation/types.ts"],"names":[],"mappings":";;;AA0DA,0EAA0E;AAC7D,QAAA,mBAAmB,GAAe;IAC7C,SAAS,EAAE,UAAU;IACrB,KAAK,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE;IAC9B,SAAS,EAAE,EAAE,IAAI,EAAE,mBAAmB,EAAE;IACxC,QAAQ,EAAE;QACR,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,YAAY,EAAE,QAAQ,EAAE,IAAI,EAAE;QACrD,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,eAAe,EAAE,QAAQ,EAAE,KAAK,EAAE;KAC7D;IACD,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;IAClD,UAAU,EAAE;QACV,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,mBAAmB,EAAE;QAChD,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,mBAAmB,EAAE;KACjD;IACD,KAAK,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE;IACrB,UAAU,EAAE,CAAC,KAAK,CAAC;CACpB,CAAC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Shared helpers for cache adapters that track per-bucket coverage metadata.
3
+ */
4
+ import type { BucketCoverageMeta } from "../types/index.js";
5
+ /**
6
+ * Reduce per-bucket coverage to a single planner-facing verdict.
7
+ *
8
+ * full iff every requested bucket reports `complete`
9
+ * partial if at least one bucket reports `complete | partial | rebuilding`
10
+ * miss when no bucket has any coverage
11
+ */
12
+ export declare function aggregateCoverage(buckets: string[], bucketCoverage: Record<string, BucketCoverageMeta>): "full" | "partial" | "miss";
13
+ //# sourceMappingURL=coverage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"coverage.d.ts","sourceRoot":"","sources":["../../src/storage/coverage.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAE5D;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,MAAM,EAAE,EACjB,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,GACjD,MAAM,GAAG,SAAS,GAAG,MAAM,CAiB7B"}
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ /**
3
+ * Shared helpers for cache adapters that track per-bucket coverage metadata.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.aggregateCoverage = aggregateCoverage;
7
+ /**
8
+ * Reduce per-bucket coverage to a single planner-facing verdict.
9
+ *
10
+ * full iff every requested bucket reports `complete`
11
+ * partial if at least one bucket reports `complete | partial | rebuilding`
12
+ * miss when no bucket has any coverage
13
+ */
14
+ function aggregateCoverage(buckets, bucketCoverage) {
15
+ if (buckets.length === 0)
16
+ return "miss";
17
+ let complete = 0;
18
+ let any = 0;
19
+ for (const b of buckets) {
20
+ const m = bucketCoverage[b];
21
+ if (!m)
22
+ continue;
23
+ if (m.status === "complete") {
24
+ complete++;
25
+ any++;
26
+ }
27
+ else if (m.status === "partial" || m.status === "rebuilding") {
28
+ any++;
29
+ }
30
+ }
31
+ if (complete === buckets.length)
32
+ return "full";
33
+ if (any > 0)
34
+ return "partial";
35
+ return "miss";
36
+ }
37
+ //# sourceMappingURL=coverage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"coverage.js","sourceRoot":"","sources":["../../src/storage/coverage.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAWH,8CAoBC;AA3BD;;;;;;GAMG;AACH,SAAgB,iBAAiB,CAC/B,OAAiB,EACjB,cAAkD;IAElD,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,MAAM,CAAC;IACxC,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE,CAAC;QACxB,MAAM,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC;QAC5B,IAAI,CAAC,CAAC;YAAE,SAAS;QACjB,IAAI,CAAC,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;YAC5B,QAAQ,EAAE,CAAC;YACX,GAAG,EAAE,CAAC;QACR,CAAC;aAAM,IAAI,CAAC,CAAC,MAAM,KAAK,SAAS,IAAI,CAAC,CAAC,MAAM,KAAK,YAAY,EAAE,CAAC;YAC/D,GAAG,EAAE,CAAC;QACR,CAAC;IACH,CAAC;IACD,IAAI,QAAQ,KAAK,OAAO,CAAC,MAAM;QAAE,OAAO,MAAM,CAAC;IAC/C,IAAI,GAAG,GAAG,CAAC;QAAE,OAAO,SAAS,CAAC;IAC9B,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -6,31 +6,49 @@
6
6
  * reference of what the contracts mean. Not for production: state is lost when
7
7
  * the process exits.
8
8
  */
9
- import type { CacheStorage, CounterCell, DictionaryLookup, DurableStorage, PendingDelta, QuerySpec } from "../types/index.js";
9
+ import type { BucketCoverageMeta, CacheReadOptions, CacheReadResult, CacheStorage, CounterCell, DictionaryLookup, DurableStorage, PendingDelta, QuerySpec, TimeframeType } from "../types/index.js";
10
10
  export declare class MemoryDurable implements DurableStorage {
11
11
  private state;
12
12
  private dict;
13
13
  private nextId;
14
14
  applyDeltas(deltas: PendingDelta[]): Promise<void>;
15
+ importSnapshotCells(cells: CounterCell[], opts?: {
16
+ signal?: AbortSignal;
17
+ }): Promise<void>;
15
18
  readCells(spec: QuerySpec): Promise<CounterCell[]>;
16
19
  resolveIds(lookups: DictionaryLookup[]): Promise<Map<string, number>>;
17
20
  streamCells(filter: Pick<QuerySpec, "scope" | "counterId" | "timeframe">): AsyncIterable<CounterCell>;
18
21
  /** Test helper. */
19
22
  _getCells(): CounterCell[];
20
23
  }
24
+ /** Cache layout schema version for the in-process adapter; aligned with Redis. */
25
+ export declare const MEMORY_CACHE_SCHEMA_VERSION = 1;
26
+ interface MemoryCacheOptions {
27
+ /** Map version stamped onto coverage meta during warm(). */
28
+ mapVersion?: number;
29
+ /** Per-timeframe TTLs (seconds). */
30
+ ttlSecondsByTimeframe?: Partial<Record<TimeframeType, number>>;
31
+ }
21
32
  export declare class MemoryCache implements CacheStorage {
22
33
  private state;
23
34
  private seen;
35
+ private bucketCoverage;
36
+ private ttlByTf;
37
+ private readonly mapVersion?;
38
+ constructor(opts?: MemoryCacheOptions);
39
+ setRetentionPolicy(policy: Partial<Record<TimeframeType, number>>): void;
40
+ private ttlFor;
41
+ private freshCoverage;
24
42
  applyDeltas(deltas: PendingDelta[]): Promise<void>;
25
- readCells(spec: QuerySpec): Promise<{
26
- cells: CounterCell[];
27
- coverage: "full" | "partial" | "miss";
28
- }>;
43
+ readCells(spec: QuerySpec, opts?: CacheReadOptions): Promise<CacheReadResult>;
29
44
  warm(cells: CounterCell[]): Promise<void>;
45
+ probeBucketCoverage(scope: string, counterId: string, timeframeType: TimeframeType, timeframeKeys: string[]): Promise<Record<string, BucketCoverageMeta>>;
46
+ markBucketRebuilding(scope: string, counterId: string, timeframeType: TimeframeType, timeframeKey: string): Promise<void>;
30
47
  markEventSeen(scopeId: string, eventId: string, ttlSeconds: number): Promise<boolean>;
31
48
  }
32
49
  export declare function createMemoryStorage(): {
33
50
  durable: MemoryDurable;
34
51
  cache: MemoryCache;
35
52
  };
53
+ export {};
36
54
  //# sourceMappingURL=memory.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/storage/memory.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,YAAY,EACZ,WAAW,EAEX,gBAAgB,EAChB,cAAc,EACd,YAAY,EAEZ,SAAS,EACV,MAAM,mBAAmB,CAAC;AAqI3B,qBAAa,aAAc,YAAW,cAAc;IAClD,OAAO,CAAC,KAAK,CAAqC;IAClD,OAAO,CAAC,IAAI,CAAkC;IAC9C,OAAO,CAAC,MAAM,CAAK;IAEb,WAAW,CAAC,MAAM,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlD,SAAS,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAIlD,UAAU,CAAC,OAAO,EAAE,gBAAgB,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAcpE,WAAW,CAChB,MAAM,EAAE,IAAI,CAAC,SAAS,EAAE,OAAO,GAAG,WAAW,GAAG,WAAW,CAAC,GAC3D,aAAa,CAAC,WAAW,CAAC;IAa7B,mBAAmB;IACnB,SAAS,IAAI,WAAW,EAAE;CAG3B;AAED,qBAAa,WAAY,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AE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,aAAa,EAAE,aAAa,EAC5B,aAAa,EAAE,MAAM,EAAE,GACtB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IAQxC,oBAAoB,CACxB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,aAAa,EAAE,aAAa,EAC5B,YAAY,EAAE,MAAM,GACnB,OAAO,CAAC,IAAI,CAAC;IAcV,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CAQ5F;AAkCD,wBAAgB,mBAAmB,IAAI;IAAE,OAAO,EAAE,aAAa,CAAC;IAAC,KAAK,EAAE,WAAW,CAAA;CAAE,CAEpF"}
@@ -8,9 +8,12 @@
8
8
  * the process exits.
9
9
  */
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.MemoryCache = exports.MemoryDurable = void 0;
11
+ exports.MemoryCache = exports.MEMORY_CACHE_SCHEMA_VERSION = exports.MemoryDurable = void 0;
12
12
  exports.createMemoryStorage = createMemoryStorage;
13
+ const coverage_js_1 = require("./coverage.js");
14
+ const snapshot_encoding_js_1 = require("../backup/snapshot-encoding.js");
13
15
  const hash_js_1 = require("../utils/hash.js");
16
+ const errors_js_1 = require("../utils/errors.js");
14
17
  const timeframe_js_1 = require("../core/timeframe.js");
15
18
  /** Divide stored fixed-point sums/min/max by `measure.scale` (Mongo-compatible semantics). */
16
19
  function cellForPresentation(c) {
@@ -152,6 +155,39 @@ class MemoryDurable {
152
155
  async applyDeltas(deltas) {
153
156
  applyDeltasInternal(this.state, deltas);
154
157
  }
158
+ async importSnapshotCells(cells, opts) {
159
+ const now = new Date();
160
+ for (const c of cells) {
161
+ if (opts?.signal?.aborted)
162
+ throw new errors_js_1.StorageError("importSnapshotCells aborted", {});
163
+ const s = (0, snapshot_encoding_js_1.presentationToStorageCell)(c);
164
+ const key = (0, hash_js_1.cellKey)({
165
+ scopeId: s.scopeId,
166
+ counterId: s.counterId,
167
+ timeframeType: s.timeframeType,
168
+ timeframeKey: s.timeframeKey,
169
+ subjectType: s.subjectType,
170
+ subjectId: s.subjectId,
171
+ metricId: s.metricId,
172
+ dimensionsHash: s.dimensionsHash,
173
+ });
174
+ this.state.cells.set(key, {
175
+ scopeId: s.scopeId,
176
+ counterId: s.counterId,
177
+ timeframeType: s.timeframeType,
178
+ timeframeKey: s.timeframeKey,
179
+ subjectType: s.subjectType,
180
+ subjectId: s.subjectId,
181
+ metricId: s.metricId,
182
+ dimensionsHash: s.dimensionsHash,
183
+ dimensions: { ...s.dimensions },
184
+ count: s.count,
185
+ measures: s.measures,
186
+ createdAt: c.createdAt ?? now,
187
+ updatedAt: now,
188
+ });
189
+ }
190
+ }
155
191
  async readCells(spec) {
156
192
  return matchingCells(spec, this.state).map(cellForPresentation);
157
193
  }
@@ -188,22 +224,130 @@ class MemoryDurable {
188
224
  }
189
225
  }
190
226
  exports.MemoryDurable = MemoryDurable;
227
+ /** Cache layout schema version for the in-process adapter; aligned with Redis. */
228
+ exports.MEMORY_CACHE_SCHEMA_VERSION = 1;
229
+ function bucketIdKey(scope, counterId, tf, key) {
230
+ return `${scope}|${counterId}|${tf}|${key}`;
231
+ }
191
232
  class MemoryCache {
192
233
  state = { cells: new Map() };
193
234
  seen = new Map();
235
+ bucketCoverage = new Map();
236
+ ttlByTf;
237
+ mapVersion;
238
+ constructor(opts = {}) {
239
+ this.mapVersion = opts.mapVersion;
240
+ this.ttlByTf = { ...(opts.ttlSecondsByTimeframe ?? {}) };
241
+ }
242
+ setRetentionPolicy(policy) {
243
+ this.ttlByTf = { ...this.ttlByTf, ...policy };
244
+ }
245
+ ttlFor(tf) {
246
+ const v = this.ttlByTf[tf];
247
+ return v !== undefined && v > 0 ? v : undefined;
248
+ }
249
+ freshCoverage(id) {
250
+ const m = this.bucketCoverage.get(id);
251
+ if (!m)
252
+ return { status: "missing" };
253
+ if (m.expiresAt !== undefined && m.expiresAt <= Date.now()) {
254
+ this.bucketCoverage.delete(id);
255
+ return { status: "missing" };
256
+ }
257
+ const { expiresAt: _e, ...rest } = m;
258
+ return rest;
259
+ }
194
260
  async applyDeltas(deltas) {
195
261
  applyDeltasInternal(this.state, deltas);
262
+ const now = Date.now();
263
+ const touched = new Set();
264
+ for (const d of deltas) {
265
+ const id = d.identity;
266
+ const k = bucketIdKey(id.scopeId, id.counterId, id.timeframeType, id.timeframeKey);
267
+ if (touched.has(k))
268
+ continue;
269
+ touched.add(k);
270
+ const existing = this.freshCoverage(k);
271
+ const ttl = this.ttlFor(id.timeframeType);
272
+ const expiresAt = ttl !== undefined ? now + ttl * 1000 : undefined;
273
+ // Never downgrade `complete` or `rebuilding`. Only set `partial` when missing.
274
+ const status = existing.status === "missing" ? "partial" : existing.status;
275
+ this.bucketCoverage.set(k, {
276
+ ...existing,
277
+ status,
278
+ lastWarmAt: now,
279
+ expiresAt,
280
+ });
281
+ }
196
282
  }
197
- async readCells(spec) {
283
+ async readCells(spec, opts) {
198
284
  const cells = matchingCells(spec, this.state).map(cellForPresentation);
199
- return { cells, coverage: cells.length > 0 ? "full" : "miss" };
285
+ const buckets = Array.from(new Set(cells.map((c) => c.timeframeKey)));
286
+ // Use the timeframe range to know which buckets were requested.
287
+ // We re-derive via bucketRange so `coverage: "miss"` correctly reflects empty windows.
288
+ const requested = (0, timeframe_js_1.bucketRange)(spec.timeframe.type, spec.timeframe.from, spec.timeframe.to);
289
+ const coverage = {};
290
+ for (const b of requested) {
291
+ const id = bucketIdKey(spec.scope, spec.counterId, spec.timeframe.type, b);
292
+ const meta = this.freshCoverage(id);
293
+ coverage[b] = applyVersionExpectations(meta, opts);
294
+ }
295
+ // If the bucket has cells but missing meta (legacy), promote to partial.
296
+ for (const b of buckets) {
297
+ if (coverage[b]?.status === "missing") {
298
+ coverage[b] = { ...coverage[b], status: "partial" };
299
+ }
300
+ }
301
+ return {
302
+ cells,
303
+ coverage: (0, coverage_js_1.aggregateCoverage)(requested, coverage),
304
+ bucketCoverage: coverage,
305
+ };
200
306
  }
201
307
  async warm(cells) {
202
308
  const now = new Date();
309
+ const nowMs = now.getTime();
310
+ const byBucket = new Map();
203
311
  for (const c of cells) {
204
312
  this.state.cells.set((0, hash_js_1.cellKey)(c), { ...stripMeasureScalesForWarm(c), updatedAt: now });
313
+ const id = bucketIdKey(c.scopeId, c.counterId, c.timeframeType, c.timeframeKey);
314
+ const existing = byBucket.get(id) ?? { tf: c.timeframeType, cells: [] };
315
+ existing.cells.push(c);
316
+ byBucket.set(id, existing);
317
+ }
318
+ for (const [id, { tf, cells: bcells }] of byBucket) {
319
+ const ttl = this.ttlFor(tf);
320
+ const expiresAt = ttl !== undefined ? nowMs + ttl * 1000 : undefined;
321
+ this.bucketCoverage.set(id, {
322
+ status: "complete",
323
+ lastWarmAt: nowMs,
324
+ cellCount: bcells.length,
325
+ schemaVersion: exports.MEMORY_CACHE_SCHEMA_VERSION,
326
+ ...(this.mapVersion !== undefined ? { mapVersion: this.mapVersion } : {}),
327
+ expiresAt,
328
+ });
205
329
  }
206
330
  }
331
+ async probeBucketCoverage(scope, counterId, timeframeType, timeframeKeys) {
332
+ const out = {};
333
+ for (const k of timeframeKeys) {
334
+ out[k] = this.freshCoverage(bucketIdKey(scope, counterId, timeframeType, k));
335
+ }
336
+ return out;
337
+ }
338
+ async markBucketRebuilding(scope, counterId, timeframeType, timeframeKey) {
339
+ const id = bucketIdKey(scope, counterId, timeframeType, timeframeKey);
340
+ const ttl = this.ttlFor(timeframeType);
341
+ const nowMs = Date.now();
342
+ const expiresAt = ttl !== undefined ? nowMs + ttl * 1000 : undefined;
343
+ const prev = this.freshCoverage(id);
344
+ this.bucketCoverage.set(id, {
345
+ ...prev,
346
+ status: "rebuilding",
347
+ lastWarmAt: nowMs,
348
+ expiresAt,
349
+ });
350
+ }
207
351
  async markEventSeen(scopeId, eventId, ttlSeconds) {
208
352
  const k = `${scopeId}|${eventId}`;
209
353
  const now = Date.now();
@@ -215,6 +359,21 @@ class MemoryCache {
215
359
  }
216
360
  }
217
361
  exports.MemoryCache = MemoryCache;
362
+ function applyVersionExpectations(meta, opts) {
363
+ if (meta.status === "complete" &&
364
+ opts?.expectedMapVersion !== undefined &&
365
+ meta.mapVersion !== undefined &&
366
+ meta.mapVersion !== opts.expectedMapVersion) {
367
+ return { ...meta, status: "partial" };
368
+ }
369
+ if (meta.status === "complete" &&
370
+ opts?.expectedSchemaVersion !== undefined &&
371
+ meta.schemaVersion !== undefined &&
372
+ meta.schemaVersion !== opts.expectedSchemaVersion) {
373
+ return { ...meta, status: "partial" };
374
+ }
375
+ return meta;
376
+ }
218
377
  function dictKey(scopeId, kind, qualifier, sourceKey) {
219
378
  return `${scopeId}|${kind}|${qualifier ?? ""}|${sourceKey}`;
220
379
  }