@soulcraft/brainy 5.7.13 → 5.8.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.
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Storage Operations with Rollback Support
3
+ *
4
+ * Provides transactional operations for all storage adapters.
5
+ * Each operation can be executed and rolled back atomically.
6
+ *
7
+ * Supports:
8
+ * - All 8 storage adapters (FileSystem, S3, Memory, OPFS, GCS, Azure, R2, TypeAware)
9
+ * - Nouns (entities) and Verbs (relationships)
10
+ * - Metadata and vector data
11
+ * - COW (Copy-on-Write) storage
12
+ */
13
+ import type { StorageAdapter, HNSWNoun, HNSWVerb, NounMetadata, VerbMetadata } from '../../coreTypes.js';
14
+ import type { Operation, RollbackAction } from '../types.js';
15
+ /**
16
+ * Save noun metadata with rollback support
17
+ *
18
+ * Rollback strategy:
19
+ * - If metadata existed: Restore previous metadata
20
+ * - If metadata was new: Delete metadata
21
+ */
22
+ export declare class SaveNounMetadataOperation implements Operation {
23
+ private readonly storage;
24
+ private readonly id;
25
+ private readonly metadata;
26
+ readonly name = "SaveNounMetadata";
27
+ constructor(storage: StorageAdapter, id: string, metadata: NounMetadata);
28
+ execute(): Promise<RollbackAction>;
29
+ }
30
+ /**
31
+ * Save noun (vector data) with rollback support
32
+ *
33
+ * Rollback strategy:
34
+ * - If noun existed: Restore previous noun
35
+ * - If noun was new: Delete noun (if deleteNoun exists on adapter)
36
+ *
37
+ * Note: Not all adapters implement deleteNoun - this is acceptable
38
+ * because orphaned vector data without metadata is invisible to queries
39
+ */
40
+ export declare class SaveNounOperation implements Operation {
41
+ private readonly storage;
42
+ private readonly noun;
43
+ readonly name = "SaveNoun";
44
+ constructor(storage: StorageAdapter, noun: HNSWNoun);
45
+ execute(): Promise<RollbackAction>;
46
+ }
47
+ /**
48
+ * Delete noun metadata with rollback support
49
+ *
50
+ * Rollback strategy:
51
+ * - Restore deleted metadata
52
+ */
53
+ export declare class DeleteNounMetadataOperation implements Operation {
54
+ private readonly storage;
55
+ private readonly id;
56
+ readonly name = "DeleteNounMetadata";
57
+ constructor(storage: StorageAdapter, id: string);
58
+ execute(): Promise<RollbackAction>;
59
+ }
60
+ /**
61
+ * Save verb metadata with rollback support
62
+ *
63
+ * Rollback strategy:
64
+ * - If metadata existed: Restore previous metadata
65
+ * - If metadata was new: Delete metadata
66
+ */
67
+ export declare class SaveVerbMetadataOperation implements Operation {
68
+ private readonly storage;
69
+ private readonly id;
70
+ private readonly metadata;
71
+ readonly name = "SaveVerbMetadata";
72
+ constructor(storage: StorageAdapter, id: string, metadata: VerbMetadata);
73
+ execute(): Promise<RollbackAction>;
74
+ }
75
+ /**
76
+ * Save verb (vector data) with rollback support
77
+ *
78
+ * Rollback strategy:
79
+ * - If verb existed: Restore previous verb
80
+ * - If verb was new: Delete verb (if deleteVerb exists on adapter)
81
+ */
82
+ export declare class SaveVerbOperation implements Operation {
83
+ private readonly storage;
84
+ private readonly verb;
85
+ readonly name = "SaveVerb";
86
+ constructor(storage: StorageAdapter, verb: HNSWVerb);
87
+ execute(): Promise<RollbackAction>;
88
+ }
89
+ /**
90
+ * Delete verb metadata with rollback support
91
+ *
92
+ * Rollback strategy:
93
+ * - Restore deleted metadata
94
+ */
95
+ export declare class DeleteVerbMetadataOperation implements Operation {
96
+ private readonly storage;
97
+ private readonly id;
98
+ readonly name = "DeleteVerbMetadata";
99
+ constructor(storage: StorageAdapter, id: string);
100
+ execute(): Promise<RollbackAction>;
101
+ }
102
+ /**
103
+ * Update noun metadata with rollback support
104
+ *
105
+ * Rollback strategy:
106
+ * - Restore previous metadata
107
+ *
108
+ * Note: This is a convenience operation that wraps SaveNounMetadataOperation
109
+ * with explicit "update" semantics
110
+ */
111
+ export declare class UpdateNounMetadataOperation implements Operation {
112
+ readonly name = "UpdateNounMetadata";
113
+ private saveOperation;
114
+ constructor(storage: StorageAdapter, id: string, metadata: NounMetadata);
115
+ execute(): Promise<RollbackAction>;
116
+ }
117
+ /**
118
+ * Update verb metadata with rollback support
119
+ *
120
+ * Rollback strategy:
121
+ * - Restore previous metadata
122
+ */
123
+ export declare class UpdateVerbMetadataOperation implements Operation {
124
+ readonly name = "UpdateVerbMetadata";
125
+ private saveOperation;
126
+ constructor(storage: StorageAdapter, id: string, metadata: VerbMetadata);
127
+ execute(): Promise<RollbackAction>;
128
+ }
@@ -0,0 +1,253 @@
1
+ /**
2
+ * Storage Operations with Rollback Support
3
+ *
4
+ * Provides transactional operations for all storage adapters.
5
+ * Each operation can be executed and rolled back atomically.
6
+ *
7
+ * Supports:
8
+ * - All 8 storage adapters (FileSystem, S3, Memory, OPFS, GCS, Azure, R2, TypeAware)
9
+ * - Nouns (entities) and Verbs (relationships)
10
+ * - Metadata and vector data
11
+ * - COW (Copy-on-Write) storage
12
+ */
13
+ /**
14
+ * Save noun metadata with rollback support
15
+ *
16
+ * Rollback strategy:
17
+ * - If metadata existed: Restore previous metadata
18
+ * - If metadata was new: Delete metadata
19
+ */
20
+ export class SaveNounMetadataOperation {
21
+ constructor(storage, id, metadata) {
22
+ this.storage = storage;
23
+ this.id = id;
24
+ this.metadata = metadata;
25
+ this.name = 'SaveNounMetadata';
26
+ }
27
+ async execute() {
28
+ // Get existing metadata (for rollback)
29
+ const previousMetadata = await this.storage.getNounMetadata(this.id);
30
+ // Save new metadata
31
+ await this.storage.saveNounMetadata(this.id, this.metadata);
32
+ // Return rollback action
33
+ return async () => {
34
+ if (previousMetadata) {
35
+ // Restore previous metadata
36
+ await this.storage.saveNounMetadata(this.id, previousMetadata);
37
+ }
38
+ else {
39
+ // Delete newly created metadata
40
+ await this.storage.deleteNounMetadata(this.id);
41
+ }
42
+ };
43
+ }
44
+ }
45
+ /**
46
+ * Save noun (vector data) with rollback support
47
+ *
48
+ * Rollback strategy:
49
+ * - If noun existed: Restore previous noun
50
+ * - If noun was new: Delete noun (if deleteNoun exists on adapter)
51
+ *
52
+ * Note: Not all adapters implement deleteNoun - this is acceptable
53
+ * because orphaned vector data without metadata is invisible to queries
54
+ */
55
+ export class SaveNounOperation {
56
+ constructor(storage, noun) {
57
+ this.storage = storage;
58
+ this.noun = noun;
59
+ this.name = 'SaveNoun';
60
+ }
61
+ async execute() {
62
+ // Get existing noun (for rollback)
63
+ const previousNoun = await this.storage.getNoun(this.noun.id);
64
+ // Save new noun
65
+ await this.storage.saveNoun(this.noun);
66
+ // Return rollback action
67
+ return async () => {
68
+ if (previousNoun) {
69
+ // Restore previous noun (extract just vector data)
70
+ const nounData = {
71
+ id: previousNoun.id,
72
+ vector: previousNoun.vector,
73
+ connections: previousNoun.connections || new Map(),
74
+ level: previousNoun.level || 0
75
+ };
76
+ await this.storage.saveNoun(nounData);
77
+ }
78
+ else {
79
+ // Delete newly created noun (if adapter supports it)
80
+ // Note: Not all adapters implement deleteNoun
81
+ // This is acceptable - metadata deletion makes entity invisible
82
+ if ('deleteNoun' in this.storage && typeof this.storage.deleteNoun === 'function') {
83
+ await this.storage.deleteNoun(this.noun.id);
84
+ }
85
+ }
86
+ };
87
+ }
88
+ }
89
+ /**
90
+ * Delete noun metadata with rollback support
91
+ *
92
+ * Rollback strategy:
93
+ * - Restore deleted metadata
94
+ */
95
+ export class DeleteNounMetadataOperation {
96
+ constructor(storage, id) {
97
+ this.storage = storage;
98
+ this.id = id;
99
+ this.name = 'DeleteNounMetadata';
100
+ }
101
+ async execute() {
102
+ // Get metadata before deletion (for rollback)
103
+ const previousMetadata = await this.storage.getNounMetadata(this.id);
104
+ if (!previousMetadata) {
105
+ // Nothing to delete - no rollback needed
106
+ return async () => { };
107
+ }
108
+ // Delete metadata
109
+ await this.storage.deleteNounMetadata(this.id);
110
+ // Return rollback action
111
+ return async () => {
112
+ // Restore deleted metadata
113
+ await this.storage.saveNounMetadata(this.id, previousMetadata);
114
+ };
115
+ }
116
+ }
117
+ /**
118
+ * Save verb metadata with rollback support
119
+ *
120
+ * Rollback strategy:
121
+ * - If metadata existed: Restore previous metadata
122
+ * - If metadata was new: Delete metadata
123
+ */
124
+ export class SaveVerbMetadataOperation {
125
+ constructor(storage, id, metadata) {
126
+ this.storage = storage;
127
+ this.id = id;
128
+ this.metadata = metadata;
129
+ this.name = 'SaveVerbMetadata';
130
+ }
131
+ async execute() {
132
+ // Get existing metadata (for rollback)
133
+ const previousMetadata = await this.storage.getVerbMetadata(this.id);
134
+ // Save new metadata
135
+ await this.storage.saveVerbMetadata(this.id, this.metadata);
136
+ // Return rollback action
137
+ return async () => {
138
+ if (previousMetadata) {
139
+ // Restore previous metadata
140
+ await this.storage.saveVerbMetadata(this.id, previousMetadata);
141
+ }
142
+ else {
143
+ // Delete newly created verb (metadata + vector)
144
+ // Note: StorageAdapter has deleteVerb but not deleteVerbMetadata
145
+ await this.storage.deleteVerb(this.id);
146
+ }
147
+ };
148
+ }
149
+ }
150
+ /**
151
+ * Save verb (vector data) with rollback support
152
+ *
153
+ * Rollback strategy:
154
+ * - If verb existed: Restore previous verb
155
+ * - If verb was new: Delete verb (if deleteVerb exists on adapter)
156
+ */
157
+ export class SaveVerbOperation {
158
+ constructor(storage, verb) {
159
+ this.storage = storage;
160
+ this.verb = verb;
161
+ this.name = 'SaveVerb';
162
+ }
163
+ async execute() {
164
+ // Get existing verb (for rollback)
165
+ const previousVerb = await this.storage.getVerb(this.verb.id);
166
+ // Save new verb
167
+ await this.storage.saveVerb(this.verb);
168
+ // Return rollback action
169
+ return async () => {
170
+ if (previousVerb) {
171
+ // Restore previous verb (extract just vector data)
172
+ const verbData = {
173
+ id: previousVerb.id,
174
+ sourceId: previousVerb.sourceId,
175
+ targetId: previousVerb.targetId,
176
+ verb: previousVerb.verb,
177
+ vector: previousVerb.vector,
178
+ connections: previousVerb.connections || new Map()
179
+ };
180
+ await this.storage.saveVerb(verbData);
181
+ }
182
+ else {
183
+ // Delete newly created verb (if adapter supports it)
184
+ if ('deleteVerb' in this.storage && typeof this.storage.deleteVerb === 'function') {
185
+ await this.storage.deleteVerb(this.verb.id);
186
+ }
187
+ }
188
+ };
189
+ }
190
+ }
191
+ /**
192
+ * Delete verb metadata with rollback support
193
+ *
194
+ * Rollback strategy:
195
+ * - Restore deleted metadata
196
+ */
197
+ export class DeleteVerbMetadataOperation {
198
+ constructor(storage, id) {
199
+ this.storage = storage;
200
+ this.id = id;
201
+ this.name = 'DeleteVerbMetadata';
202
+ }
203
+ async execute() {
204
+ // Get metadata before deletion (for rollback)
205
+ const previousMetadata = await this.storage.getVerbMetadata(this.id);
206
+ if (!previousMetadata) {
207
+ // Nothing to delete - no rollback needed
208
+ return async () => { };
209
+ }
210
+ // Delete verb (metadata + vector)
211
+ // Note: StorageAdapter has deleteVerb but not deleteVerbMetadata
212
+ await this.storage.deleteVerb(this.id);
213
+ // Return rollback action
214
+ return async () => {
215
+ // Restore deleted metadata
216
+ await this.storage.saveVerbMetadata(this.id, previousMetadata);
217
+ };
218
+ }
219
+ }
220
+ /**
221
+ * Update noun metadata with rollback support
222
+ *
223
+ * Rollback strategy:
224
+ * - Restore previous metadata
225
+ *
226
+ * Note: This is a convenience operation that wraps SaveNounMetadataOperation
227
+ * with explicit "update" semantics
228
+ */
229
+ export class UpdateNounMetadataOperation {
230
+ constructor(storage, id, metadata) {
231
+ this.name = 'UpdateNounMetadata';
232
+ this.saveOperation = new SaveNounMetadataOperation(storage, id, metadata);
233
+ }
234
+ async execute() {
235
+ return await this.saveOperation.execute();
236
+ }
237
+ }
238
+ /**
239
+ * Update verb metadata with rollback support
240
+ *
241
+ * Rollback strategy:
242
+ * - Restore previous metadata
243
+ */
244
+ export class UpdateVerbMetadataOperation {
245
+ constructor(storage, id, metadata) {
246
+ this.name = 'UpdateVerbMetadata';
247
+ this.saveOperation = new SaveVerbMetadataOperation(storage, id, metadata);
248
+ }
249
+ async execute() {
250
+ return await this.saveOperation.execute();
251
+ }
252
+ }
253
+ //# sourceMappingURL=StorageOperations.js.map
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Transaction Operations
3
+ *
4
+ * Re-exports all transactional operations for storage and indexes.
5
+ * These operations provide atomicity with rollback support.
6
+ *
7
+ * @module transaction/operations
8
+ */
9
+ export { SaveNounMetadataOperation, SaveNounOperation, DeleteNounMetadataOperation, SaveVerbMetadataOperation, SaveVerbOperation, DeleteVerbMetadataOperation, UpdateNounMetadataOperation, UpdateVerbMetadataOperation } from './StorageOperations.js';
10
+ export { AddToHNSWOperation, AddToTypeAwareHNSWOperation, RemoveFromHNSWOperation, RemoveFromTypeAwareHNSWOperation, AddToMetadataIndexOperation, RemoveFromMetadataIndexOperation, AddToGraphIndexOperation, RemoveFromGraphIndexOperation, BatchAddToHNSWOperation, BatchAddToMetadataIndexOperation } from './IndexOperations.js';
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Transaction Operations
3
+ *
4
+ * Re-exports all transactional operations for storage and indexes.
5
+ * These operations provide atomicity with rollback support.
6
+ *
7
+ * @module transaction/operations
8
+ */
9
+ // Storage Operations
10
+ export { SaveNounMetadataOperation, SaveNounOperation, DeleteNounMetadataOperation, SaveVerbMetadataOperation, SaveVerbOperation, DeleteVerbMetadataOperation, UpdateNounMetadataOperation, UpdateVerbMetadataOperation } from './StorageOperations.js';
11
+ // Index Operations
12
+ export { AddToHNSWOperation, AddToTypeAwareHNSWOperation, RemoveFromHNSWOperation, RemoveFromTypeAwareHNSWOperation, AddToMetadataIndexOperation, RemoveFromMetadataIndexOperation, AddToGraphIndexOperation, RemoveFromGraphIndexOperation, BatchAddToHNSWOperation, BatchAddToMetadataIndexOperation } from './IndexOperations.js';
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Transaction System Types
3
+ *
4
+ * Provides atomicity for Brainy operations - all succeed or all rollback.
5
+ * Prevents partial failures that leave system in inconsistent state.
6
+ */
7
+ /**
8
+ * Transaction state
9
+ */
10
+ export type TransactionState = 'pending' | 'executing' | 'committed' | 'rolling_back' | 'rolled_back';
11
+ /**
12
+ * Rollback action - undoes an operation
13
+ * Must be idempotent (safe to call multiple times)
14
+ */
15
+ export type RollbackAction = () => Promise<void>;
16
+ /**
17
+ * Operation that can be executed and rolled back
18
+ */
19
+ export interface Operation {
20
+ /**
21
+ * Execute the operation
22
+ * @returns Rollback action to undo this operation (or undefined if no rollback needed)
23
+ */
24
+ execute(): Promise<RollbackAction | undefined>;
25
+ /**
26
+ * Optional: Name of operation for debugging
27
+ */
28
+ readonly name?: string;
29
+ }
30
+ /**
31
+ * Transaction context passed to user functions
32
+ */
33
+ export interface TransactionContext {
34
+ /**
35
+ * Add an operation to the transaction
36
+ */
37
+ addOperation(operation: Operation): void;
38
+ /**
39
+ * Get current transaction state
40
+ */
41
+ getState(): TransactionState;
42
+ /**
43
+ * Get number of operations in transaction
44
+ */
45
+ getOperationCount(): number;
46
+ }
47
+ /**
48
+ * Function that builds a transaction
49
+ */
50
+ export type TransactionFunction<T> = (ctx: TransactionContext) => Promise<T>;
51
+ /**
52
+ * Transaction execution result
53
+ */
54
+ export interface TransactionResult<T> {
55
+ /**
56
+ * Result value from user function
57
+ */
58
+ value: T;
59
+ /**
60
+ * Number of operations executed
61
+ */
62
+ operationCount: number;
63
+ /**
64
+ * Execution time in milliseconds
65
+ */
66
+ executionTimeMs: number;
67
+ }
68
+ /**
69
+ * Transaction execution options
70
+ */
71
+ export interface TransactionOptions {
72
+ /**
73
+ * Timeout in milliseconds (default: 30000 = 30 seconds)
74
+ */
75
+ timeout?: number;
76
+ /**
77
+ * Whether to log transaction execution (default: false)
78
+ */
79
+ logging?: boolean;
80
+ /**
81
+ * Maximum number of rollback retry attempts (default: 3)
82
+ */
83
+ maxRollbackRetries?: number;
84
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Transaction System Types
3
+ *
4
+ * Provides atomicity for Brainy operations - all succeed or all rollback.
5
+ * Prevents partial failures that leave system in inconsistent state.
6
+ */
7
+ export {};
8
+ //# sourceMappingURL=types.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soulcraft/brainy",
3
- "version": "5.7.13",
3
+ "version": "5.8.0",
4
4
  "description": "Universal Knowledge Protocol™ - World's first Triple Intelligence database unifying vector, graph, and document search in one API. Stage 3 CANONICAL: 42 nouns × 127 verbs covering 96-97% of all human knowledge.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",