|
| 1 | +# Storage Transaction Abstraction Explainer |
| 2 | + |
| 3 | +The storage transaction system provides ACID-like guarantees for reading and |
| 4 | +writing data across memory spaces. Think of it as a database transaction but for |
| 5 | +a distributed, content-addressed memory system. |
| 6 | + |
| 7 | +## Core Architecture |
| 8 | + |
| 9 | +The transaction system is built in three layers: |
| 10 | + |
| 11 | +1. **StorageTransaction** |
| 12 | + (`packages/runner/src/storage/transaction.ts:63-107`) - The main API surface |
| 13 | + that manages transaction lifecycle |
| 14 | +2. **Journal** (`packages/runner/src/storage/transaction/journal.ts:52-100`) - |
| 15 | + Tracks all reads/writes and enforces consistency |
| 16 | +3. **Chronicle** |
| 17 | + (`packages/runner/src/storage/transaction/chronicle.ts:51-223`) - Manages |
| 18 | + changes per memory space with conflict detection |
| 19 | + |
| 20 | +## How It Works |
| 21 | + |
| 22 | +### Transaction Lifecycle |
| 23 | + |
| 24 | +A transaction moves through three states: |
| 25 | + |
| 26 | +1. **Ready** - Active and accepting reads/writes |
| 27 | +2. **Pending** - Commit initiated, waiting for storage confirmation |
| 28 | +3. **Done** - Completed (successfully or with error) |
| 29 | + |
| 30 | +```typescript |
| 31 | +// Create a new transaction |
| 32 | +const transaction = create(storageManager); |
| 33 | + |
| 34 | +// Read and write operations |
| 35 | +const readResult = transaction.read({ |
| 36 | + space: "user", |
| 37 | + id: "123", |
| 38 | + type: "application/json", |
| 39 | + path: ["name"], |
| 40 | +}); |
| 41 | +const writeResult = transaction.write({ |
| 42 | + space: "user", |
| 43 | + id: "123", |
| 44 | + type: "application/json", |
| 45 | + path: ["age"], |
| 46 | +}, 25); |
| 47 | + |
| 48 | +// Commit changes |
| 49 | +const commitResult = await transaction.commit(); |
| 50 | +``` |
| 51 | + |
| 52 | +### Key Design Principles |
| 53 | + |
| 54 | +**1. Write Isolation**: A transaction can only write to one memory space. This |
| 55 | +prevents distributed consistency issues: |
| 56 | + |
| 57 | +```typescript |
| 58 | +// First write locks the transaction to "user" space |
| 59 | +transaction.write({ space: "user", ... }, data); |
| 60 | + |
| 61 | +// This will fail with WriteIsolationError |
| 62 | +transaction.write({ space: "project", ... }, data); |
| 63 | +``` |
| 64 | + |
| 65 | +**2. Read Consistency**: All reads capture "invariants" - assumptions about the |
| 66 | +state when read. If any invariant is violated before commit, the transaction |
| 67 | +fails: |
| 68 | + |
| 69 | +```typescript |
| 70 | +// Transaction reads age = 30 |
| 71 | +const age = transaction.read({ ...address, path: ["age"] }); |
| 72 | + |
| 73 | +// Another client changes age to 31 |
| 74 | + |
| 75 | +// This transaction's commit will fail due to inconsistency |
| 76 | +``` |
| 77 | + |
| 78 | +**3. Optimistic Updates**: Writes within a transaction see their own changes |
| 79 | +immediately: |
| 80 | + |
| 81 | +```typescript |
| 82 | +transaction.write({ ...address, path: ["name"] }, "Alice"); |
| 83 | +const name = transaction.read({ ...address, path: ["name"] }); // Returns "Alice" |
| 84 | +``` |
| 85 | + |
| 86 | +## Internal Mechanics |
| 87 | + |
| 88 | +### Journal (`packages/runner/src/storage/transaction/journal.ts`) |
| 89 | + |
| 90 | +The Journal manages transaction state and coordinates between readers/writers: |
| 91 | + |
| 92 | +- Maintains separate Chronicle instances per memory space |
| 93 | +- Tracks all read/write activity for debugging and replay |
| 94 | +- Enforces single-writer constraint |
| 95 | +- Handles transaction abort/close lifecycle |
| 96 | + |
| 97 | +### Chronicle (`packages/runner/src/storage/transaction/chronicle.ts`) |
| 98 | + |
| 99 | +Each Chronicle manages changes for one memory space: |
| 100 | + |
| 101 | +- **History**: Tracks all read invariants to detect conflicts |
| 102 | +- **Novelty**: Stores pending writes not yet committed |
| 103 | +- **Rebase**: Merges overlapping writes intelligently |
| 104 | + |
| 105 | +Key operations: |
| 106 | + |
| 107 | +1. **Read** - Checks novelty first (uncommitted writes), then replica, captures |
| 108 | + invariant |
| 109 | +2. **Write** - Validates against current state, stores in novelty |
| 110 | +3. **Commit** - Verifies all invariants still hold, builds final transaction |
| 111 | + |
| 112 | +### Attestation System |
| 113 | + |
| 114 | +All reads and writes produce "attestations" - immutable records of observed or |
| 115 | +desired state: |
| 116 | + |
| 117 | +```typescript |
| 118 | +interface IAttestation { |
| 119 | + address: IMemoryAddress; // What was read/written |
| 120 | + value?: JSONValue; // The value (undefined = deleted) |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +## Error Handling |
| 125 | + |
| 126 | +The system uses Result types extensively with specific errors: |
| 127 | + |
| 128 | +- **TransactionCompleteError** - Operation on finished transaction |
| 129 | +- **WriteIsolationError** - Attempting to write to second space |
| 130 | +- **StorageTransactionInconsistent** - Read invariant violated (underlying state |
| 131 | + changed) |
| 132 | +- **NotFoundError** - Document or path doesn't exist |
| 133 | +- **TypeMismatchError** - Accessing properties on non-objects (e.g., trying to |
| 134 | + read `string.property`) |
| 135 | +- **ReadOnlyAddressError** - Writing to data: URIs |
| 136 | + |
| 137 | +The distinction between these errors is important: |
| 138 | + |
| 139 | +- **NotFoundError** is transient - the operation would succeed if the |
| 140 | + document/path existed |
| 141 | +- **TypeMismatchError** is persistent - the operation will always fail unless |
| 142 | + the data type changes |
| 143 | +- **StorageTransactionInconsistent** indicates retry is needed - the underlying |
| 144 | + data changed |
| 145 | + |
| 146 | +## Advanced Features |
| 147 | + |
| 148 | +**1. Data URIs**: Read-only inline data using `data:` URLs |
| 149 | + |
| 150 | +**2. Path-based Access**: Read/write nested JSON paths: |
| 151 | + |
| 152 | +```typescript |
| 153 | +transaction.read({ ...address, path: ["user", "profile", "name"] }); |
| 154 | +``` |
| 155 | + |
| 156 | +**3. Activity Tracking**: All operations recorded for debugging: |
| 157 | + |
| 158 | +```typescript |
| 159 | +for (const activity of transaction.journal.activity()) { |
| 160 | + // Log reads and writes |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +**4. Consistency Validation**: Chronicle's commit method |
| 165 | +(`packages/runner/src/storage/transaction/chronicle.ts:176-222`) carefully |
| 166 | +validates that all read invariants still hold before building the final |
| 167 | +transaction. |
| 168 | + |
| 169 | +This architecture ensures strong consistency guarantees while allowing |
| 170 | +optimistic updates within a transaction boundary, making it suitable for |
| 171 | +collaborative editing scenarios where multiple clients may be modifying data |
| 172 | +concurrently. |
0 commit comments