@spine-event-engine/storage 2.0.0-snapshot.21 → 2.0.0-snapshot.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 (95) hide show
  1. package/README.md +7 -0
  2. package/REFERENCE.md +106 -1
  3. package/dist/entity/agent-execution-record-spec.d.ts +87 -0
  4. package/dist/entity/agent-execution-record-spec.d.ts.map +1 -0
  5. package/dist/entity/agent-execution-record-spec.js +230 -0
  6. package/dist/entity/agent-execution-record-spec.js.map +1 -0
  7. package/dist/entity/agent-execution-sizes.d.ts +30 -0
  8. package/dist/entity/agent-execution-sizes.d.ts.map +1 -0
  9. package/dist/entity/agent-execution-sizes.js +51 -0
  10. package/dist/entity/agent-execution-sizes.js.map +1 -0
  11. package/dist/entity/agent-execution-transitions.d.ts +71 -0
  12. package/dist/entity/agent-execution-transitions.d.ts.map +1 -0
  13. package/dist/entity/agent-execution-transitions.js +182 -0
  14. package/dist/entity/agent-execution-transitions.js.map +1 -0
  15. package/dist/entity/agent-execution-values.d.ts +142 -0
  16. package/dist/entity/agent-execution-values.d.ts.map +1 -0
  17. package/dist/entity/agent-execution-values.js +250 -0
  18. package/dist/entity/agent-execution-values.js.map +1 -0
  19. package/dist/entity/agent-execution.d.ts +234 -0
  20. package/dist/entity/agent-execution.d.ts.map +1 -0
  21. package/dist/entity/agent-execution.js +15 -0
  22. package/dist/entity/agent-execution.js.map +1 -0
  23. package/dist/entity/agent-history-conformance.d.ts +78 -0
  24. package/dist/entity/agent-history-conformance.d.ts.map +1 -0
  25. package/dist/entity/agent-history-conformance.js +493 -0
  26. package/dist/entity/agent-history-conformance.js.map +1 -0
  27. package/dist/entity/agent-history-provider-page.d.ts +39 -0
  28. package/dist/entity/agent-history-provider-page.d.ts.map +1 -0
  29. package/dist/entity/agent-history-provider-page.js +82 -0
  30. package/dist/entity/agent-history-provider-page.js.map +1 -0
  31. package/dist/entity/agent-history-record-spec.d.ts +79 -0
  32. package/dist/entity/agent-history-record-spec.d.ts.map +1 -0
  33. package/dist/entity/agent-history-record-spec.js +147 -0
  34. package/dist/entity/agent-history-record-spec.js.map +1 -0
  35. package/dist/entity/agent-history.d.ts +196 -0
  36. package/dist/entity/agent-history.d.ts.map +1 -0
  37. package/dist/entity/agent-history.js +136 -0
  38. package/dist/entity/agent-history.js.map +1 -0
  39. package/dist/event/event-store.d.ts +52 -4
  40. package/dist/event/event-store.d.ts.map +1 -1
  41. package/dist/event/event-store.js +153 -4
  42. package/dist/event/event-store.js.map +1 -1
  43. package/dist/internal/agent-execution.d.ts +51 -0
  44. package/dist/internal/agent-execution.d.ts.map +1 -0
  45. package/dist/internal/agent-execution.js +53 -0
  46. package/dist/internal/agent-execution.js.map +1 -0
  47. package/dist/internal/agent-history.d.ts +48 -0
  48. package/dist/internal/agent-history.d.ts.map +1 -0
  49. package/dist/internal/agent-history.js +52 -0
  50. package/dist/internal/agent-history.js.map +1 -0
  51. package/dist/internal/entity-history.d.ts +11 -0
  52. package/dist/internal/entity-history.d.ts.map +1 -1
  53. package/dist/internal/entity-history.js +10 -0
  54. package/dist/internal/entity-history.js.map +1 -1
  55. package/dist/internal/tenancy.d.ts +87 -1
  56. package/dist/internal/tenancy.d.ts.map +1 -1
  57. package/dist/internal/tenancy.js +39 -0
  58. package/dist/internal/tenancy.js.map +1 -1
  59. package/dist/memory/agent-execution-head-index.d.ts +52 -0
  60. package/dist/memory/agent-execution-head-index.d.ts.map +1 -0
  61. package/dist/memory/agent-execution-head-index.js +91 -0
  62. package/dist/memory/agent-execution-head-index.js.map +1 -0
  63. package/dist/memory/agent-history-index.d.ts +49 -0
  64. package/dist/memory/agent-history-index.d.ts.map +1 -0
  65. package/dist/memory/agent-history-index.js +239 -0
  66. package/dist/memory/agent-history-index.js.map +1 -0
  67. package/dist/memory/agent-history-mutation-queue.d.ts +17 -0
  68. package/dist/memory/agent-history-mutation-queue.d.ts.map +1 -0
  69. package/dist/memory/agent-history-mutation-queue.js +31 -0
  70. package/dist/memory/agent-history-mutation-queue.js.map +1 -0
  71. package/dist/memory/in-memory-agent-execution.d.ts +21 -0
  72. package/dist/memory/in-memory-agent-execution.d.ts.map +1 -0
  73. package/dist/memory/in-memory-agent-execution.js +587 -0
  74. package/dist/memory/in-memory-agent-execution.js.map +1 -0
  75. package/dist/memory/in-memory-agent-history.d.ts +21 -0
  76. package/dist/memory/in-memory-agent-history.d.ts.map +1 -0
  77. package/dist/memory/in-memory-agent-history.js +133 -0
  78. package/dist/memory/in-memory-agent-history.js.map +1 -0
  79. package/dist/memory/in-memory-entity-commit.d.ts +17 -0
  80. package/dist/memory/in-memory-entity-commit.d.ts.map +1 -1
  81. package/dist/memory/in-memory-entity-commit.js +47 -3
  82. package/dist/memory/in-memory-entity-commit.js.map +1 -1
  83. package/dist/memory/in-memory-entity-history.d.ts +1 -1
  84. package/dist/memory/in-memory-entity-history.d.ts.map +1 -1
  85. package/dist/memory/in-memory-entity-history.js.map +1 -1
  86. package/dist/memory/in-memory-storage-backend.d.ts +18 -0
  87. package/dist/memory/in-memory-storage-backend.d.ts.map +1 -1
  88. package/dist/memory/in-memory-storage-backend.js +71 -0
  89. package/dist/memory/in-memory-storage-backend.js.map +1 -1
  90. package/dist/memory/in-memory-storage-factory.d.ts +37 -2
  91. package/dist/memory/in-memory-storage-factory.d.ts.map +1 -1
  92. package/dist/memory/in-memory-storage-factory.js +144 -3
  93. package/dist/memory/in-memory-storage-factory.js.map +1 -1
  94. package/dist/tsconfig.tsbuildinfo +1 -1
  95. package/package.json +3 -2
@@ -33,7 +33,7 @@ interface TenantBoundaryFactory {
33
33
  */
34
34
  from(tenantId: TenantId): MultitenantTenantBoundary;
35
35
  /**
36
- * Selects the boundary declared by a storage context.
36
+ * Validates a storage context and returns its complete tenant boundary.
37
37
  *
38
38
  * @param context The storage context.
39
39
  * @returns The validated tenant boundary.
@@ -46,6 +46,85 @@ interface TenantBoundaryFactory {
46
46
  * @internal
47
47
  */
48
48
  export declare const TenantBoundary: TenantBoundaryFactory;
49
+ /**
50
+ * Opaque continuation issued by one provider catalog instance.
51
+ */
52
+ export interface TenantCatalogCursor {
53
+ /**
54
+ * Identifies an opaque in-process catalog continuation.
55
+ */
56
+ readonly [Symbol.toStringTag]: "TenantCatalogCursor";
57
+ }
58
+ /**
59
+ * Structural cancellation accepted from a platform AbortSignal.
60
+ */
61
+ export interface TenantCatalogSignal {
62
+ /**
63
+ * Whether cancellation has already been requested.
64
+ */
65
+ readonly aborted: boolean;
66
+ /**
67
+ * Throws when cancellation has been requested.
68
+ */
69
+ throwIfAborted(): void;
70
+ /**
71
+ * Observes one cancellation notification.
72
+ *
73
+ * @param type Abort event name.
74
+ * @param onAbort Callback notified on cancellation.
75
+ * @param options One-shot listener selection.
76
+ */
77
+ addEventListener(type: "abort", onAbort: () => void, options?: {
78
+ once?: boolean;
79
+ }): void;
80
+ /**
81
+ * Removes a previously registered cancellation listener.
82
+ *
83
+ * @param type Abort event name.
84
+ * @param onAbort Callback to remove.
85
+ */
86
+ removeEventListener(type: "abort", onAbort: () => void): void;
87
+ }
88
+ /**
89
+ * Bounded provider tenant-page request.
90
+ */
91
+ export interface TenantCatalogRead {
92
+ /**
93
+ * Positive safe page size bounding native candidates.
94
+ */
95
+ readonly count: number;
96
+ /**
97
+ * Cancels discovery before native work and before results become visible.
98
+ */
99
+ readonly signal: TenantCatalogSignal;
100
+ /**
101
+ * Continuation issued by this same catalog instance.
102
+ */
103
+ readonly after?: TenantCatalogCursor;
104
+ }
105
+ /**
106
+ * One finite tenant-catalog page.
107
+ */
108
+ export interface TenantCatalogPage {
109
+ /**
110
+ * Complete boundaries selected from this page's native candidates.
111
+ */
112
+ readonly boundaries: readonly TenantBoundary[];
113
+ /**
114
+ * Continuation when more candidates remain.
115
+ */
116
+ readonly after?: TenantCatalogCursor;
117
+ /**
118
+ * Whether another page belongs to this finite sweep.
119
+ */
120
+ readonly hasMore: boolean;
121
+ }
122
+ /**
123
+ * Validates common finite-page limits for provider catalogs.
124
+ */
125
+ export declare const TenantCatalogReads: Readonly<{
126
+ require(request: TenantCatalogRead): void;
127
+ }>;
49
128
  /**
50
129
  * Provider-owned enumeration of storage tenant boundaries.
51
130
  *
@@ -58,6 +137,13 @@ export interface TenantCatalog {
58
137
  * @returns The available tenant boundaries.
59
138
  */
60
139
  all(): Promise<readonly TenantBoundary[]>;
140
+ /**
141
+ * Reads at most the requested number of native candidates per page.
142
+ *
143
+ * @param request Finite page request and opaque continuation.
144
+ * @returns A possibly empty continuing page or terminal page.
145
+ */
146
+ page(request: TenantCatalogRead): Promise<TenantCatalogPage>;
61
147
  /**
62
148
  * Closes resources owned by this catalog.
63
149
  *
@@ -1 +1 @@
1
- {"version":3,"file":"tenancy.d.ts","sourceRoot":"","sources":["../../src/internal/tenancy.ts"],"names":[],"mappings":"AAeA,OAAO,EAAkB,KAAK,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAC1E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAI5D;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAG7B;;OAEG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;IAE9B;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IAEzB;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,QAAQ,GAAG,SAAS,CAAC;CACzC;AAED,UAAU,yBAA0B,SAAQ,cAAc;IACxD,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC;IACvB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;CAC7B;AAED,UAAU,qBAAqB;IAC7B,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC;IAEhC;;;;;OAKG;IACH,IAAI,CAAC,QAAQ,EAAE,QAAQ,GAAG,yBAAyB,CAAC;IAEpD;;;;;OAKG;IACH,EAAE,CAAC,OAAO,EAAE,cAAc,GAAG,cAAc,CAAC;CAC7C;AAQD;;;;GAIG;AACH,eAAO,MAAM,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Ac,EAAE,CAAC,CAAC;IAE1C;;;;;OAKG;IACH,IAAI,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;IAE7D;;;;OAIG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEvB;;;;;OAKG;IACH,IAAI,CAAC,QAAQ,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/C;AAED;;;;GAIG;AACH,MAAM,WAAW,qBAAqB;IAGpC;;;;OAIG;IACH,aAAa,IAAI,aAAa,CAAC;CAChC"}
@@ -60,21 +60,39 @@ export const TenantBoundary = {
60
60
  },
61
61
  };
62
62
  Object.freeze(TenantBoundary);
63
+ /**
64
+ * Clones and validates a complete multitenant identifier.
65
+ */
63
66
  class MultitenantBoundary {
64
67
  #tenantId;
65
68
  key;
66
69
  single = false;
70
+ /**
71
+ * Stores one validated tenant identity.
72
+ *
73
+ * @param tenantId Complete generated tenant identifier.
74
+ */
67
75
  constructor(tenantId) {
68
76
  TenantIds.require(tenantId);
69
77
  this.#tenantId = clone(TenantIdSchema, tenantId);
70
78
  this.key = TenantIds.key(this.#tenantId);
71
79
  Object.freeze(this);
72
80
  }
81
+ /**
82
+ * Returns a detached complete tenant identifier.
83
+ *
84
+ * @returns The cloned generated tenant identifier.
85
+ */
73
86
  get tenantId() {
74
87
  return clone(TenantIdSchema, this.#tenantId);
75
88
  }
76
89
  }
77
90
  const TenantIds = Object.freeze({
91
+ /**
92
+ * Rejects an empty or unspecified generated tenant identifier.
93
+ *
94
+ * @param tenantId Identifier to validate.
95
+ */
78
96
  require(tenantId) {
79
97
  const kind = tenantId.kind;
80
98
  const value = kind.case === "value"
@@ -86,6 +104,12 @@ const TenantIds = Object.freeze({
86
104
  throw new Error("Multitenant storage requires a non-empty TenantId.");
87
105
  }
88
106
  },
107
+ /**
108
+ * Encodes the complete generated tenant identifier as a map key.
109
+ *
110
+ * @param tenantId Identifier to encode.
111
+ * @returns Hexadecimal Protobuf bytes without unknown fields.
112
+ */
89
113
  key(tenantId) {
90
114
  const bytes = toBinary(TenantIdSchema, tenantId, { writeUnknownFields: false });
91
115
  let encoded = "";
@@ -94,4 +118,19 @@ const TenantIds = Object.freeze({
94
118
  return encoded;
95
119
  },
96
120
  });
121
+ /**
122
+ * Validates common finite-page limits for provider catalogs.
123
+ */
124
+ export const TenantCatalogReads = Object.freeze({
125
+ /**
126
+ * Rejects invalid page bounds or cancellation.
127
+ *
128
+ * @param request Requested finite page.
129
+ */
130
+ require(request) {
131
+ if (!Number.isSafeInteger(request.count) || request.count < 1)
132
+ throw new RangeError("Tenant catalog count must be a positive safe integer.");
133
+ request.signal.throwIfAborted();
134
+ },
135
+ });
97
136
  //# sourceMappingURL=tenancy.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"tenancy.js","sourceRoot":"","sources":["../../src/internal/tenancy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,cAAc,EAAiB,MAAM,2BAA2B,CAAC;AAG1E,MAAM,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;AAmDhD,MAAM,oBAAoB,GAAmB,MAAM,CAAC,MAAM,CAAC;IACzD,GAAG,EAAE,eAAe;IACpB,MAAM,EAAE,IAAI;IACZ,QAAQ,EAAE,SAAS;CACpB,CAAC,CAAC;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAA0B;IACnD,kBAAkB;IAElB;;OAEG;IACH,MAAM,EAAE,oBAAoB;IAE5B;;;;;OAKG;IACH,IAAI,CAAC,QAAkB;QACrB,OAAO,IAAI,mBAAmB,CAAC,QAAQ,CAAC,CAAC;IAC3C,CAAC;IAED;;;;;OAKG;IACH,EAAE,CAAC,OAAuB;QACxB,MAAM,QAAQ,GAAI,OAA4C,CAAC,QAAQ,CAAC;QACxE,IAAI,CAAC,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} from "@spine-event-engine/proto/generated/spine/server/agent/execution_record_pb.js";
3
+ /**
4
+ * One complete observed head-index entry.
5
+ */
6
+ export interface MemoryPendingHead {
7
+ /**
8
+ * Complete native-equivalent order key.
9
+ */
10
+ readonly orderKey: string;
11
+ /**
12
+ * Independent head snapshot at the page read.
13
+ */
14
+ readonly head: AgentExecutionHead;
15
+ }
16
+ /**
17
+ * Keeps one ordered discovery entry per Agent instance.
18
+ */
19
+ export declare class MemoryPendingHeadIndex {
20
+ #private;
21
+ /**
22
+ * Replaces the visible entry for one complete Agent scope.
23
+ *
24
+ * @param head Per-instance pending and lease record.
25
+ * @param scope Generated state type and Agent key.
26
+ */
27
+ set(scope: string, head: AgentExecutionHead): void;
28
+ /**
29
+ * Reads no more than count plus one candidate before the fixed cutoff.
30
+ *
31
+ * @param after Observed pending-page cursor.
32
+ * @param asOf Fixed eligibility cutoff for the page.
33
+ * @param count Maximum number of records in the page.
34
+ * @returns Ordered bounded per-instance head entries.
35
+ */
36
+ page(after: string | undefined, asOf: Timestamp, count: number): readonly MemoryPendingHead[];
37
+ /**
38
+ * Finds the first index entry whose key is not smaller than value.
39
+ *
40
+ * @param value Value read from a provider boundary.
41
+ * @returns First matching index position.
42
+ */
43
+ private lower;
44
+ /**
45
+ * Finds the first index entry strictly after the cursor.
46
+ *
47
+ * @param value Value read from a provider boundary.
48
+ * @returns Position after the last matching index entry.
49
+ */
50
+ private upper;
51
+ }
52
+ //# sourceMappingURL=agent-execution-head-index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-execution-head-index.d.ts","sourceRoot":"","sources":["../../src/memory/agent-execution-head-index.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAEL,KAAK,kBAAkB,EACxB,MAAM,+EAA+E,CAAC;AAIvF;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE1B;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,kBAAkB,CAAC;CACnC;AAED;;GAEG;AACH,qBAAa,sBAAsB;;IAKjC;;;;;OAKG;IACH,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,kBAAkB,GAAG,IAAI;IAelD;;;;;;;OAOG;IACH,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,SAAS,iBAAiB,EAAE;IAM7F;;;;;OAKG;IACH,OAAO,CAAC,KAAK;IAab;;;;;OAKG;IACH,OAAO,CAAC,KAAK;CAKd"}
@@ -0,0 +1,91 @@
1
+ /*
2
+ * Copyright 2026, CodeMatters. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
+ * in compliance with the License. You may obtain a copy of the License at
6
+ *
7
+ * https://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
10
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
+ * or implied. See the License for the specific language governing permissions and limitations under
12
+ * the License.
13
+ */
14
+ import { fromBinary, toBinary } from "@bufbuild/protobuf";
15
+ import { AgentExecutionHeadSchema, } from "@spine-event-engine/proto/generated/spine/server/agent/execution_record_pb.js";
16
+ import { AgentExecutionRecords } from "../entity/agent-execution-record-spec.js";
17
+ /**
18
+ * Keeps one ordered discovery entry per Agent instance.
19
+ */
20
+ export class MemoryPendingHeadIndex {
21
+ #byScope = new Map();
22
+ #ordered = [];
23
+ /**
24
+ * Replaces the visible entry for one complete Agent scope.
25
+ *
26
+ * @param head Per-instance pending and lease record.
27
+ * @param scope Generated state type and Agent key.
28
+ */
29
+ set(scope, head) {
30
+ const previous = this.#byScope.get(scope);
31
+ if (previous !== undefined)
32
+ this.#ordered.splice(this.lower(previous.orderKey), 1);
33
+ if (head.pending === undefined || head.eligibleAt === undefined || head.scope === undefined) {
34
+ this.#byScope.delete(scope);
35
+ return;
36
+ }
37
+ const entry = {
38
+ orderKey: AgentExecutionRecords.pendingKey(head.eligibleAt, head.scope),
39
+ head: fromBinary(AgentExecutionHeadSchema, toBinary(AgentExecutionHeadSchema, head)),
40
+ };
41
+ this.#ordered.splice(this.lower(entry.orderKey), 0, entry);
42
+ this.#byScope.set(scope, entry);
43
+ }
44
+ /**
45
+ * Reads no more than count plus one candidate before the fixed cutoff.
46
+ *
47
+ * @param after Observed pending-page cursor.
48
+ * @param asOf Fixed eligibility cutoff for the page.
49
+ * @param count Maximum number of records in the page.
50
+ * @returns Ordered bounded per-instance head entries.
51
+ */
52
+ page(after, asOf, count) {
53
+ const start = after === undefined ? 0 : this.upper(after);
54
+ const end = this.lower(AgentExecutionRecords.pendingLower(asOf));
55
+ return this.#ordered.slice(start, Math.min(end, start + count + 1));
56
+ }
57
+ /**
58
+ * Finds the first index entry whose key is not smaller than value.
59
+ *
60
+ * @param value Value read from a provider boundary.
61
+ * @returns First matching index position.
62
+ */
63
+ lower(value) {
64
+ let left = 0;
65
+ let right = this.#ordered.length;
66
+ while (left < right) {
67
+ const middle = (left + right) >>> 1;
68
+ const entry = this.#ordered[middle];
69
+ if (entry === undefined)
70
+ throw new Error("Agent head index position is missing.");
71
+ if (entry.orderKey < value)
72
+ left = middle + 1;
73
+ else
74
+ right = middle;
75
+ }
76
+ return left;
77
+ }
78
+ /**
79
+ * Finds the first index entry strictly after the cursor.
80
+ *
81
+ * @param value Value read from a provider boundary.
82
+ * @returns Position after the last matching index entry.
83
+ */
84
+ upper(value) {
85
+ let left = this.lower(value);
86
+ while (this.#ordered[left]?.orderKey === value)
87
+ left += 1;
88
+ return left;
89
+ }
90
+ }
91
+ //# sourceMappingURL=agent-execution-head-index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-execution-head-index.js","sourceRoot":"","sources":["../../src/memory/agent-execution-head-index.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E,IAAI,IAAI,CAAC,CAAC;QAC1D,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
@@ -0,0 +1,49 @@
1
+ import { type AgentHistoryEntry } from "@spine-event-engine/proto/agent";
2
+ import { type AgentHistoryOrderKey, type AgentHistoryPage, type AgentHistoryView } from "../entity/agent-history.js";
3
+ /**
4
+ * A validated batch applied and restored within a fenced mutation.
5
+ */
6
+ export interface AgentHistoryUpdate {
7
+ /**
8
+ * The retained index receiving the new rows.
9
+ */
10
+ readonly index: AgentHistoryIndex;
11
+ /**
12
+ * Adds the validated rows once.
13
+ */
14
+ apply(): void;
15
+ /**
16
+ * Removes only rows added by this batch.
17
+ */
18
+ restore(): void;
19
+ }
20
+ /**
21
+ * Maintains ordered category and conversation indexes for one Agent ID.
22
+ */
23
+ export declare class AgentHistoryIndex {
24
+ #private;
25
+ /**
26
+ * Validates and serializes only the new entries before a fenced mutation.
27
+ *
28
+ * @param entries Entries to append after the containing write is accepted.
29
+ * @returns A reversible batch without copying retained rows.
30
+ */
31
+ withEntries(entries: readonly AgentHistoryEntry[]): AgentHistoryUpdate;
32
+ /**
33
+ * Adds an immutable entry to its indexed views.
34
+ *
35
+ * @param entry Complete Proto history record.
36
+ */
37
+ append(entry: AgentHistoryEntry): void;
38
+ /**
39
+ * Reads the selected index after a complete ordering key.
40
+ *
41
+ * @param view Full, category, or conversation selection.
42
+ * @param after Last returned complete key, when continuing.
43
+ * @param count Maximum returned records.
44
+ * @param maxBytes Maximum serialized wrapper bytes.
45
+ * @returns Bounded independent entries and continuation flag.
46
+ */
47
+ read(view: AgentHistoryView, after: AgentHistoryOrderKey | undefined, count: number, maxBytes: number | undefined): AgentHistoryPage;
48
+ }
49
+ //# sourceMappingURL=agent-history-index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-history-index.d.ts","sourceRoot":"","sources":["../../src/memory/agent-history-index.ts"],"names":[],"mappings":"AAeA,OAAO,EAA2B,KAAK,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AAElG,OAAO,EAEL,KAAK,oBAAoB,EACzB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACtB,MAAM,4BAA4B,CAAC;AAUpC;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,iBAAiB,CAAC;IAElC;;OAEG;IACH,KAAK,IAAI,IAAI,CAAC;IAEd;;OAEG;IACH,OAAO,IAAI,IAAI,CAAC;CACjB;AAED;;GAEG;AACH,qBAAa,iBAAiB;;IAW5B;;;;;OAKG;IACH,WAAW,CAAC,OAAO,EAAE,SAAS,iBAAiB,EAAE,GAAG,kBAAkB;IAmCtE;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,iBAAiB,GAAG,IAAI;IA8DtC;;;;;;;;OAQG;IACH,IAAI,CACF,IAAI,EAAE,gBAAgB,EACtB,KAAK,EAAE,oBAAoB,GAAG,SAAS,EACvC,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,GAAG,SAAS,GAC3B,gBAAgB;CAwFpB"}
@@ -0,0 +1,239 @@
1
+ /*
2
+ * Copyright 2026, CodeMatters. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
+ * in compliance with the License. You may obtain a copy of the License at
6
+ *
7
+ * https://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
10
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
+ * or implied. See the License for the specific language governing permissions and limitations under
12
+ * the License.
13
+ */
14
+ import { fromBinary, toBinary } from "@bufbuild/protobuf";
15
+ import { AgentHistoryEntrySchema } from "@spine-event-engine/proto/agent";
16
+ import { AgentHistoryKeys, } from "../entity/agent-history.js";
17
+ /**
18
+ * Maintains ordered category and conversation indexes for one Agent ID.
19
+ */
20
+ export class AgentHistoryIndex {
21
+ #all = [];
22
+ #system = [];
23
+ #domain = [];
24
+ #conversations = new Map();
25
+ #byIdentity = new Map();
26
+ /**
27
+ * Validates and serializes only the new entries before a fenced mutation.
28
+ *
29
+ * @param entries Entries to append after the containing write is accepted.
30
+ * @returns A reversible batch without copying retained rows.
31
+ */
32
+ withEntries(entries) {
33
+ const additions = [];
34
+ const pending = new Map();
35
+ for (const entry of entries) {
36
+ const indexed = this.#indexed(entry);
37
+ const existing = pending.get(indexed.identity) ?? this.#byIdentity.get(indexed.identity);
38
+ if (existing !== undefined) {
39
+ if (!this.#sameBytes(existing.bytes, indexed.bytes))
40
+ throw new Error("Agent history record ID conflicts with immutable content.");
41
+ continue;
42
+ }
43
+ pending.set(indexed.identity, indexed);
44
+ additions.push(indexed);
45
+ }
46
+ let applied = false;
47
+ return {
48
+ index: this,
49
+ apply: () => {
50
+ if (applied)
51
+ return;
52
+ try {
53
+ for (const indexed of additions)
54
+ this.#appendIndexed(indexed);
55
+ applied = true;
56
+ }
57
+ catch (error) {
58
+ for (const indexed of additions)
59
+ this.#removeIndexed(indexed);
60
+ throw error;
61
+ }
62
+ },
63
+ restore: () => {
64
+ if (!applied)
65
+ return;
66
+ for (const indexed of additions)
67
+ this.#removeIndexed(indexed);
68
+ applied = false;
69
+ },
70
+ };
71
+ }
72
+ /**
73
+ * Adds an immutable entry to its indexed views.
74
+ *
75
+ * @param entry Complete Proto history record.
76
+ */
77
+ append(entry) {
78
+ this.withEntries([entry]).apply();
79
+ }
80
+ /**
81
+ * Serializes one entry after checking its complete history identity.
82
+ *
83
+ * @param entry Complete history entry.
84
+ * @returns Immutable indexed row.
85
+ */
86
+ #indexed(entry) {
87
+ const key = AgentHistoryKeys.fromEntry(entry);
88
+ const indexKey = AgentHistoryKeys.indexValue(key);
89
+ const bytes = toBinary(AgentHistoryEntrySchema, entry);
90
+ const identity = `${key.category}:${key.recordId}`;
91
+ const conversation = entry.item.case === "conversationRecord" ? entry.item.value.conversation?.value : undefined;
92
+ if (key.category === "conversation" && conversation === undefined)
93
+ throw new TypeError("Conversation history requires a ConversationId.");
94
+ return {
95
+ indexKey,
96
+ identity,
97
+ category: key.category,
98
+ ...(conversation === undefined ? {} : { conversation }),
99
+ bytes,
100
+ };
101
+ }
102
+ /**
103
+ * Adds one already validated immutable entry to its indexed views.
104
+ *
105
+ * @param indexed Serialized row to append.
106
+ */
107
+ #appendIndexed(indexed) {
108
+ this.#byIdentity.set(indexed.identity, indexed);
109
+ this.#insert(this.#all, indexed);
110
+ if (indexed.category === "system")
111
+ this.#insert(this.#system, indexed);
112
+ if (indexed.category === "domain")
113
+ this.#insert(this.#domain, indexed);
114
+ if (indexed.conversation !== undefined)
115
+ this.#insert(this.#conversation(indexed.conversation), indexed);
116
+ }
117
+ /**
118
+ * Reverses only rows appended by a failed containing mutation.
119
+ *
120
+ * @param indexed Serialized row to remove.
121
+ */
122
+ #removeIndexed(indexed) {
123
+ if (this.#byIdentity.get(indexed.identity) !== indexed)
124
+ return;
125
+ this.#byIdentity.delete(indexed.identity);
126
+ this.#remove(this.#all, indexed);
127
+ if (indexed.category === "system")
128
+ this.#remove(this.#system, indexed);
129
+ if (indexed.category === "domain")
130
+ this.#remove(this.#domain, indexed);
131
+ if (indexed.conversation !== undefined) {
132
+ const records = this.#conversations.get(indexed.conversation);
133
+ if (records !== undefined) {
134
+ this.#remove(records, indexed);
135
+ if (records.length === 0)
136
+ this.#conversations.delete(indexed.conversation);
137
+ }
138
+ }
139
+ }
140
+ /**
141
+ * Reads the selected index after a complete ordering key.
142
+ *
143
+ * @param view Full, category, or conversation selection.
144
+ * @param after Last returned complete key, when continuing.
145
+ * @param count Maximum returned records.
146
+ * @param maxBytes Maximum serialized wrapper bytes.
147
+ * @returns Bounded independent entries and continuation flag.
148
+ */
149
+ read(view, after, count, maxBytes) {
150
+ const records = this.#view(view);
151
+ const start = after === undefined
152
+ ? records.length - 1
153
+ : this.#firstNotOlder(records, AgentHistoryKeys.indexValue(after)) - 1;
154
+ const entries = [];
155
+ let bytes = 0;
156
+ let index = start;
157
+ while (index >= 0 && entries.length < count) {
158
+ const record = records[index];
159
+ if (record === undefined)
160
+ throw new Error("Agent history index changed during read.");
161
+ if (maxBytes !== undefined && record.bytes.length > maxBytes - bytes) {
162
+ if (entries.length === 0)
163
+ throw new RangeError("Agent history first entry exceeds maxBytes.");
164
+ break;
165
+ }
166
+ entries.push(fromBinary(AgentHistoryEntrySchema, record.bytes));
167
+ bytes += record.bytes.length;
168
+ index -= 1;
169
+ }
170
+ return { entries: Object.freeze(entries), hasMore: index >= 0 };
171
+ }
172
+ /**
173
+ * Selects one already ordered index without scanning unrelated categories.
174
+ */
175
+ #view(view) {
176
+ if (view.kind === "full")
177
+ return this.#all;
178
+ if (view.kind === "system")
179
+ return this.#system;
180
+ if (view.kind === "domain")
181
+ return this.#domain;
182
+ if (view.conversation.value.length === 0)
183
+ throw new TypeError("Conversation history requires a ConversationId.");
184
+ return this.#conversations.get(view.conversation.value) ?? [];
185
+ }
186
+ /**
187
+ * Finds or creates the ordered index for one conversation.
188
+ */
189
+ #conversation(id) {
190
+ let records = this.#conversations.get(id);
191
+ if (records === undefined) {
192
+ records = [];
193
+ this.#conversations.set(id, records);
194
+ }
195
+ return records;
196
+ }
197
+ /**
198
+ * Inserts one immutable record at its sorted position.
199
+ */
200
+ #insert(records, entry) {
201
+ records.splice(this.#firstNotOlder(records, entry.indexKey), 0, entry);
202
+ }
203
+ /**
204
+ * Removes one exact indexed row when its containing mutation restores.
205
+ *
206
+ * @param records Ordered view to update.
207
+ * @param entry Exact appended row.
208
+ */
209
+ #remove(records, entry) {
210
+ const index = records.indexOf(entry);
211
+ if (index >= 0)
212
+ records.splice(index, 1);
213
+ }
214
+ /**
215
+ * Finds the first record strictly after the complete key.
216
+ */
217
+ #firstNotOlder(records, key) {
218
+ let low = 0;
219
+ let high = records.length;
220
+ while (low < high) {
221
+ const middle = Math.floor((low + high) / 2);
222
+ const candidate = records[middle];
223
+ if (candidate === undefined)
224
+ throw new Error("Agent history index changed during search.");
225
+ if (candidate.indexKey > key)
226
+ low = middle + 1;
227
+ else
228
+ high = middle;
229
+ }
230
+ return low;
231
+ }
232
+ /**
233
+ * Compares immutable serialized Proto bytes.
234
+ */
235
+ #sameBytes(first, second) {
236
+ return first.length === second.length && first.every((byte, index) => byte === second[index]);
237
+ }
238
+ }
239
+ //# sourceMappingURL=agent-history-index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-history-index.js","sourceRoot":"","sources":["../../src/memory/agent-history-index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,uBAAuB,EAA0B,MAAM,iCAAiC,CAAC;AAElG,OAAO,EACL,gBAAgB,GAIjB,MAAM,4BAA4B,CAAC;AA8BpC;;GAEG;AACH,MAAM,OAAO,iBAAiB;IACnB,IAAI,GAAmB,EAAE,CAAC;IAE1B,OAAO,GAAmB,EAAE,CAAC;IAE7B,OAAO,GAAmB,EAAE,CAAC;IAE7B,cAAc,GAAG,IAAI,GAAG,EAA0B,CAAC;IAEnD,WAAW,GAAG,IAAI,GAAG,EAAwB,CAAC;IAEvD;;;;;OAKG;IACH,WAAW,CAAC,OAAqC;QAC/C,MAAM,SAAS,GAAmB,EAAE,CAAC;QACrC,MAAM,OAAO,GAAG,IAAI,GAAG,EAAwB,CAAC;QAChD,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;YAC5B,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;YACrC,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;YACzF,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;gBAC3B,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ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