@systemfsoftware/agent-storage-sqlite 0.0.0-bootstrap.0 → 0.1.0-beta.192
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.
- package/LICENSE +21 -0
- package/dist/SqliteActivityStore-CSvw-3OJ.mjs +25 -0
- package/dist/SqliteActivityStore-CSvw-3OJ.mjs.map +1 -0
- package/dist/SqliteActivityStore-DM-gMSMv.d.mts +15 -0
- package/dist/SqliteActivityStore.d.mts +2 -0
- package/dist/SqliteActivityStore.mjs +1 -0
- package/dist/SqliteMessageDeliveryStore-C0D-jYWc.mjs +18 -0
- package/dist/SqliteMessageDeliveryStore-C0D-jYWc.mjs.map +1 -0
- package/dist/SqliteMessageDeliveryStore-D1pwnufl.d.mts +11 -0
- package/dist/SqliteMessageDeliveryStore.d.mts +2 -0
- package/dist/SqliteMessageDeliveryStore.mjs +1 -0
- package/dist/SqliteScheduleStore-CrH-tLQB.d.mts +12 -0
- package/dist/SqliteScheduleStore-Nph_W_Lg.mjs +19 -0
- package/dist/SqliteScheduleStore-Nph_W_Lg.mjs.map +1 -0
- package/dist/SqliteScheduleStore.d.mts +2 -0
- package/dist/SqliteScheduleStore.mjs +1 -0
- package/dist/SqliteStorageConfig-6HRtBHf8.d.mts +27 -0
- package/dist/SqliteStorageConfig-DN-WwQrw.mjs +32 -0
- package/dist/SqliteStorageConfig-DN-WwQrw.mjs.map +1 -0
- package/dist/SqliteStorageConfig.d.mts +2 -0
- package/dist/SqliteStorageConfig.mjs +1 -0
- package/dist/SqliteStorageError-Bc-iVib1.d.mts +71 -0
- package/dist/SqliteStorageError-d_tygf_W.mjs +79 -0
- package/dist/SqliteStorageError-d_tygf_W.mjs.map +1 -0
- package/dist/SqliteStorageError.d.mts +2 -0
- package/dist/SqliteStorageError.mjs +1 -0
- package/dist/SqliteStorageFailpoint-BqxmxFBb.mjs +15 -0
- package/dist/SqliteStorageFailpoint-BqxmxFBb.mjs.map +1 -0
- package/dist/SqliteStorageFailpoint-CJRyxmsW.d.mts +17 -0
- package/dist/SqliteStorageFailpoint.d.mts +2 -0
- package/dist/SqliteStorageFailpoint.mjs +1 -0
- package/dist/SqliteStorageVersion-Bayz1P9w.d.mts +8 -0
- package/dist/SqliteStorageVersion-BxAVd_1t.mjs +7 -0
- package/dist/SqliteStorageVersion-BxAVd_1t.mjs.map +1 -0
- package/dist/SqliteStorageVersion.d.mts +3 -0
- package/dist/SqliteStorageVersion.mjs +2 -0
- package/dist/SqliteSubmissionLedger-2lHitXAA.d.mts +25 -0
- package/dist/SqliteSubmissionLedger-DTWaRPjV.mjs +46 -0
- package/dist/SqliteSubmissionLedger-DTWaRPjV.mjs.map +1 -0
- package/dist/SqliteSubmissionLedger.d.mts +2 -0
- package/dist/SqliteSubmissionLedger.mjs +1 -0
- package/dist/SqliteSubscriptionStore-CFmi_ULJ.mjs +21 -0
- package/dist/SqliteSubscriptionStore-CFmi_ULJ.mjs.map +1 -0
- package/dist/SqliteSubscriptionStore-CRkOEBaa.d.mts +11 -0
- package/dist/SqliteSubscriptionStore.d.mts +2 -0
- package/dist/SqliteSubscriptionStore.mjs +1 -0
- package/dist/SqliteThreadStore-CUkk9A0Y.mjs +235 -0
- package/dist/SqliteThreadStore-CUkk9A0Y.mjs.map +1 -0
- package/dist/SqliteThreadStore-DKac999S.d.mts +89 -0
- package/dist/SqliteThreadStore.d.mts +2 -0
- package/dist/SqliteThreadStore.mjs +1 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.mjs +11 -0
- package/dist/migrations-_uIpJR5O.mjs +240 -0
- package/dist/migrations-_uIpJR5O.mjs.map +1 -0
- package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
- package/dist/sqlite-journal-3Zguf9OC.mjs +112 -0
- package/dist/sqlite-journal-3Zguf9OC.mjs.map +1 -0
- package/package.json +85 -5
- package/src/SqliteActivityStore.ts +35 -0
- package/src/SqliteMessageDeliveryStore.ts +23 -0
- package/src/SqliteScheduleStore.ts +38 -0
- package/src/SqliteStorageConfig.ts +31 -0
- package/src/SqliteStorageError.ts +96 -0
- package/src/SqliteStorageFailpoint.ts +22 -0
- package/src/SqliteStorageVersion.ts +2 -0
- package/src/SqliteSubmissionLedger.ts +80 -0
- package/src/SqliteSubscriptionStore.ts +31 -0
- package/src/SqliteThreadStore.ts +395 -0
- package/src/index.ts +10 -0
- package/src/internal/migrations.ts +396 -0
- package/src/internal/sqlite-client-layer.ts +19 -0
- package/src/internal/sqlite-journal.ts +205 -0
- package/src/internal/sqlite-ledger-errors.ts +25 -0
- package/README.md +0 -3
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import * as NodeCrypto from '@effect/platform-node/NodeCrypto'
|
|
2
|
+
import { sqlStorageOwner } from '@systemfsoftware/agent-storage-sql/sql-journal'
|
|
3
|
+
import { makeSqlRunStorage } from '@systemfsoftware/agent-storage-sql/sql-run-storage'
|
|
4
|
+
import { makeSqlTransaction, SqlInteger } from '@systemfsoftware/agent-storage-sql/sql-storage'
|
|
5
|
+
import { makeSqlThreadImport } from '@systemfsoftware/agent-storage-sql/sql-thread-import'
|
|
6
|
+
import { makeSqlThreadStoreKernel } from '@systemfsoftware/agent-storage-sql/sql-thread-store'
|
|
7
|
+
import { RunStorage, StorageOwner } from '@systemfsoftware/agent/run-storage'
|
|
8
|
+
import { SettlementPublisher } from '@systemfsoftware/agent/settlement-publisher'
|
|
9
|
+
import { SubmissionLedger } from '@systemfsoftware/agent/submission-ledger'
|
|
10
|
+
import { ThreadImport } from '@systemfsoftware/agent/thread-import'
|
|
11
|
+
import type { ThreadExportRequest } from '@systemfsoftware/agent/thread-store'
|
|
12
|
+
import { ThreadExportSource, ThreadReader, ThreadStore, ThreadStoreError } from '@systemfsoftware/agent/thread-store'
|
|
13
|
+
import { checkExportSource } from '@systemfsoftware/agent/thread-transfer'
|
|
14
|
+
import * as Context from 'effect/Context'
|
|
15
|
+
import * as CryptoDigest from 'effect/crypto/Digest'
|
|
16
|
+
import * as SecureRandom from 'effect/crypto/SecureRandom'
|
|
17
|
+
import * as Effect from 'effect/Effect'
|
|
18
|
+
import * as Layer from 'effect/Layer'
|
|
19
|
+
import * as Schema from 'effect/Schema'
|
|
20
|
+
import * as Scope from 'effect/Scope'
|
|
21
|
+
import * as SqlClientService from 'effect/sql/SqlClient'
|
|
22
|
+
import * as Statement from 'effect/sql/Statement'
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
CurrentSqliteStorageVersion,
|
|
26
|
+
inspectManagedSqliteStorage,
|
|
27
|
+
readSqliteStorageHeader,
|
|
28
|
+
} from './internal/migrations'
|
|
29
|
+
import { sqliteClientLayer } from './internal/sqlite-client-layer'
|
|
30
|
+
import {
|
|
31
|
+
configureSqliteSynchronous,
|
|
32
|
+
initializeSqliteJournal,
|
|
33
|
+
initializeSqliteJournalKernel,
|
|
34
|
+
sqliteErrors,
|
|
35
|
+
} from './internal/sqlite-journal'
|
|
36
|
+
import { sqlFailure } from './internal/sqlite-ledger-errors'
|
|
37
|
+
import { SqliteStorageConfig, SqliteStorageConfigValue } from './SqliteStorageConfig'
|
|
38
|
+
import type { SqliteStorageCorruptionError } from './SqliteStorageError'
|
|
39
|
+
import { SqliteStorageCompatibilityError, SqliteStorageError } from './SqliteStorageError'
|
|
40
|
+
import { SqliteStorageFailpoint, type SqliteStorageFailpointHandler } from './SqliteStorageFailpoint'
|
|
41
|
+
|
|
42
|
+
export interface SqliteStorageOptions {
|
|
43
|
+
readonly filename: string
|
|
44
|
+
readonly observationPollInterval?: number | undefined
|
|
45
|
+
/** Bounded SQLITE_BUSY retry window for write-lock acquisition, in milliseconds. */
|
|
46
|
+
readonly busyTimeout?: number | undefined
|
|
47
|
+
/**
|
|
48
|
+
* SQLite WAL synchronization for this store's connection. Defaults to FULL. NORMAL can lose
|
|
49
|
+
* acknowledged commits after power loss or an OS crash, potentially repeating external effects.
|
|
50
|
+
* All stores sharing a client must select the same mode when constructed.
|
|
51
|
+
*/
|
|
52
|
+
readonly synchronous?: 'FULL' | 'NORMAL' | undefined
|
|
53
|
+
/**
|
|
54
|
+
* Audit stored payloads, identities, and batch coverage on open. Defaults to false. Full reads validate
|
|
55
|
+
* canonical records; readPrompt trusts write-time validation of the omitted fields.
|
|
56
|
+
*/
|
|
57
|
+
readonly verifyOnOpen?: boolean | undefined
|
|
58
|
+
readonly failpoint?: SqliteStorageFailpointHandler | undefined
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export type SqliteStorageInitializationError =
|
|
62
|
+
| SqliteStorageCompatibilityError
|
|
63
|
+
| SqliteStorageCorruptionError
|
|
64
|
+
| SqliteStorageError
|
|
65
|
+
|
|
66
|
+
/** Only the exclusive client acquisition can construct managed mutation authority. */
|
|
67
|
+
class ExclusiveSqliteHost extends Context.Service<
|
|
68
|
+
ExclusiveSqliteHost,
|
|
69
|
+
{ readonly sql: SqlClientService.SqlClient }
|
|
70
|
+
>()('@effect-agent/storage-sqlite/internal/ExclusiveSqliteHost') {}
|
|
71
|
+
|
|
72
|
+
const ExclusiveMode = Schema.Tuple([Schema.Struct({ locking_mode: Schema.Literal('exclusive') })])
|
|
73
|
+
const JournalMode = Schema.Tuple([Schema.Struct({ journal_mode: Schema.String })])
|
|
74
|
+
|
|
75
|
+
const DatabaseHeader = Schema.Tuple([
|
|
76
|
+
Schema.Struct({ user_version: SqlInteger, schema_object_count: SqlInteger }),
|
|
77
|
+
])
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Acquire process-lifetime authority over a dedicated SQLite client before exposing it to a
|
|
81
|
+
* managed host. Open the client with `disableWAL: true`: WAL setup and lock contention must
|
|
82
|
+
* fail through this Layer's typed initialization error. The client Scope owns the lock;
|
|
83
|
+
* never change its locking mode or close it while host work is alive.
|
|
84
|
+
*
|
|
85
|
+
* This excludes ALL other database connections, including readers. After compatibility checks,
|
|
86
|
+
* initialize fresh storage or atomically upgrade supported predecessor layouts, then recover from durable facts. Scope closure
|
|
87
|
+
* retires local capabilities; no ownership is restored from the database. Only the exact
|
|
88
|
+
* declared triggers on Thread storage tables are permitted before any initialization;
|
|
89
|
+
* application and optional memory tables retain their own schema contracts.
|
|
90
|
+
*/
|
|
91
|
+
export const exclusiveHostClientLayer: Layer.Layer<
|
|
92
|
+
SqlClientService.SqlClient | ExclusiveSqliteHost,
|
|
93
|
+
SqliteStorageInitializationError,
|
|
94
|
+
SqliteStorageConfig | SqliteStorageFailpoint | SqlClientService.SqlClient | CryptoDigest.Digest
|
|
95
|
+
> = Layer.effectContext(
|
|
96
|
+
Effect.gen(function*() {
|
|
97
|
+
const sql = (yield* SqlClientService.SqlClient).withoutTransforms()
|
|
98
|
+
|
|
99
|
+
const acquireError = (cause: unknown) =>
|
|
100
|
+
SqliteStorageError.make({
|
|
101
|
+
operation: 'acquire exclusive host',
|
|
102
|
+
message: 'Cannot acquire exclusive SQLite host authority; close other database connections.',
|
|
103
|
+
cause,
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
yield* Effect.gen(function*() {
|
|
107
|
+
yield* sql`PRAGMA busy_timeout = 0`
|
|
108
|
+
const mode = yield* sql`PRAGMA main.locking_mode = EXCLUSIVE`
|
|
109
|
+
|
|
110
|
+
yield* Schema.decodeUnknownEffect(ExclusiveMode)(mode)
|
|
111
|
+
|
|
112
|
+
const [journal] = yield* Schema.decodeUnknownEffect(JournalMode)(
|
|
113
|
+
yield* sql`PRAGMA journal_mode`,
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
if (journal.journal_mode !== 'wal') {
|
|
117
|
+
const [header] = yield* Schema.decodeUnknownEffect(DatabaseHeader)(
|
|
118
|
+
yield* sql`
|
|
119
|
+
SELECT (SELECT user_version FROM pragma_user_version) AS user_version,
|
|
120
|
+
(SELECT COUNT(*) FROM sqlite_master) AS schema_object_count
|
|
121
|
+
`,
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
// Only a version-zero file with no schema objects may change journal mode. Existing
|
|
125
|
+
// stores must already satisfy the adapter's WAL contract, including predecessors.
|
|
126
|
+
if (header.user_version !== 0 || header.schema_object_count !== 0) {
|
|
127
|
+
return yield* SqliteStorageCompatibilityError.make({
|
|
128
|
+
actualVersion: header.user_version,
|
|
129
|
+
supportedVersion: CurrentSqliteStorageVersion,
|
|
130
|
+
message: 'An existing managed-host database must use SQLite WAL mode; no data was changed.',
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
yield* sql`PRAGMA journal_mode = WAL`
|
|
134
|
+
}
|
|
135
|
+
yield* configureSqliteSynchronous()
|
|
136
|
+
// A mode setting alone is not authority. Acquire the write lock now; EXCLUSIVE mode
|
|
137
|
+
// retains it after commit/rollback until this client's enclosing Scope closes.
|
|
138
|
+
yield* makeSqlTransaction(sql, { begin: 'BEGIN IMMEDIATE' })(Effect.void)
|
|
139
|
+
|
|
140
|
+
yield* inspectManagedSqliteStorage()
|
|
141
|
+
}).pipe(
|
|
142
|
+
Effect.mapError((error) => Schema.is(SqliteStorageCompatibilityError)(error) ? error : acquireError(error)),
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
yield* initializeSqliteJournal.pipe(Effect.provideService(SqlClientService.SqlClient, sql))
|
|
146
|
+
|
|
147
|
+
return Context.make(SqlClientService.SqlClient, sql).pipe(
|
|
148
|
+
Context.add(ExclusiveSqliteHost, { sql }),
|
|
149
|
+
)
|
|
150
|
+
}),
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
const makeServices = Effect.fnUntraced(function*() {
|
|
154
|
+
const config = yield* SqliteStorageConfig
|
|
155
|
+
const failpoint = yield* SqliteStorageFailpoint
|
|
156
|
+
const journal = yield* initializeSqliteJournal
|
|
157
|
+
|
|
158
|
+
const services = yield* makeSqlThreadStoreKernel(journal, {
|
|
159
|
+
...config,
|
|
160
|
+
errors: sqliteErrors,
|
|
161
|
+
hitFailpoint: failpoint.hit,
|
|
162
|
+
offsetPrefix: 'effect-agent-sqlite@1:',
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
return { ...services, owner: journal.owner }
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* SQLite Thread Store implementation with configuration, failpoint, SQL, and Digest
|
|
170
|
+
* authority kept visible in its input channel.
|
|
171
|
+
* Inject a client opened with `disableWAL: true`; this adapter enables WAL only for a
|
|
172
|
+
* fresh file so unsupported files reject before persistent driver configuration changes.
|
|
173
|
+
*/
|
|
174
|
+
export const threadStoreLayer: Layer.Layer<
|
|
175
|
+
ThreadStore | ThreadReader | ThreadImport | StorageOwner,
|
|
176
|
+
SqliteStorageInitializationError,
|
|
177
|
+
SqliteStorageConfig | SqliteStorageFailpoint | SqlClientService.SqlClient | CryptoDigest.Digest
|
|
178
|
+
> = Layer.effect(
|
|
179
|
+
ThreadReader,
|
|
180
|
+
Effect.map(ThreadStore, (store) => ThreadReader.fromStore(store)),
|
|
181
|
+
).pipe(
|
|
182
|
+
Layer.provideMerge(
|
|
183
|
+
Layer.effectContext(
|
|
184
|
+
Effect.map(makeServices(), ({ store, importer, owner }) =>
|
|
185
|
+
Context.make(ThreadStore, store).pipe(
|
|
186
|
+
Context.add(ThreadImport, importer),
|
|
187
|
+
Context.add(StorageOwner, owner),
|
|
188
|
+
)),
|
|
189
|
+
),
|
|
190
|
+
),
|
|
191
|
+
Layer.provide(exclusiveHostClientLayer),
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Managed-host storage for an already acquired exclusiveHostClientLayer. The composition
|
|
196
|
+
* root consumes the mutation ports privately and exposes only ThreadReader to application
|
|
197
|
+
* bindings. Its private construction witness binds the exact exclusive client; supplying a
|
|
198
|
+
* generic SqlClient cannot bypass the required exclusive acquisition.
|
|
199
|
+
*/
|
|
200
|
+
export const exclusiveRunStorageLayer = Layer.effectContext(
|
|
201
|
+
Effect.gen(function*() {
|
|
202
|
+
const exclusive = yield* ExclusiveSqliteHost
|
|
203
|
+
|
|
204
|
+
const live = yield* Effect.context<
|
|
205
|
+
| CryptoDigest.Digest
|
|
206
|
+
| SecureRandom.SecureRandom
|
|
207
|
+
| SqliteStorageConfig
|
|
208
|
+
| SqliteStorageFailpoint
|
|
209
|
+
| Scope.Scope
|
|
210
|
+
>()
|
|
211
|
+
|
|
212
|
+
const context = Context.add(
|
|
213
|
+
Context.pick(
|
|
214
|
+
CryptoDigest.Digest,
|
|
215
|
+
SecureRandom.SecureRandom,
|
|
216
|
+
SqliteStorageConfig,
|
|
217
|
+
SqliteStorageFailpoint,
|
|
218
|
+
Scope.Scope,
|
|
219
|
+
)(live),
|
|
220
|
+
SqlClientService.SqlClient,
|
|
221
|
+
exclusive.sql,
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
return yield* Effect.setContext(
|
|
225
|
+
Effect.gen(function*() {
|
|
226
|
+
const config = yield* SqliteStorageConfig
|
|
227
|
+
const failpoint = yield* SqliteStorageFailpoint
|
|
228
|
+
const journal = yield* initializeSqliteJournalKernel()
|
|
229
|
+
|
|
230
|
+
const services = yield* makeSqlRunStorage(
|
|
231
|
+
journal,
|
|
232
|
+
{
|
|
233
|
+
...config,
|
|
234
|
+
errors: sqliteErrors,
|
|
235
|
+
hitFailpoint: failpoint.hit,
|
|
236
|
+
offsetPrefix: 'effect-agent-sqlite@1:',
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
errors: sqliteErrors,
|
|
240
|
+
hitFailpoint: failpoint.hit,
|
|
241
|
+
offsetPrefix: 'effect-agent-sqlite@1:',
|
|
242
|
+
sqlFailure,
|
|
243
|
+
},
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
return Context.make(ThreadStore, services.store).pipe(
|
|
247
|
+
Context.add(ThreadReader, services.reader),
|
|
248
|
+
Context.add(ThreadImport, services.importer),
|
|
249
|
+
Context.add(SubmissionLedger, services.ledger),
|
|
250
|
+
Context.add(SettlementPublisher, services.publisher),
|
|
251
|
+
Context.add(RunStorage, services.runStorage),
|
|
252
|
+
Context.add(StorageOwner, services.owner),
|
|
253
|
+
)
|
|
254
|
+
}),
|
|
255
|
+
Context.merge(
|
|
256
|
+
Context.omit(Statement.CurrentTransformer, exclusive.sql.transactionService)(live),
|
|
257
|
+
context,
|
|
258
|
+
),
|
|
259
|
+
)
|
|
260
|
+
}),
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Validated SQLite storage configuration Layer with the documented defaults applied. Shared
|
|
265
|
+
* by the ThreadStore and SubmissionLedger convenience layers so their defaults cannot
|
|
266
|
+
* drift.
|
|
267
|
+
*/
|
|
268
|
+
export const storageConfigLayer = (
|
|
269
|
+
options: SqliteStorageOptions,
|
|
270
|
+
): Layer.Layer<SqliteStorageConfig, SqliteStorageError> =>
|
|
271
|
+
Layer.effect(SqliteStorageConfig)(
|
|
272
|
+
Schema.decodeEffect(SqliteStorageConfigValue)({
|
|
273
|
+
observationPollInterval: options.observationPollInterval ?? 25,
|
|
274
|
+
busyTimeout: options.busyTimeout ?? 5_000,
|
|
275
|
+
synchronous: options.synchronous ?? 'FULL',
|
|
276
|
+
verifyOnOpen: options.verifyOnOpen ?? false,
|
|
277
|
+
}).pipe(
|
|
278
|
+
Effect.mapError((error) =>
|
|
279
|
+
SqliteStorageError.make({
|
|
280
|
+
cause: error,
|
|
281
|
+
operation: 'configure SQLite storage',
|
|
282
|
+
message: error.message,
|
|
283
|
+
})
|
|
284
|
+
),
|
|
285
|
+
),
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
/** The failpoint Layer selected by convenience options: explicit handler or the no-op default. */
|
|
289
|
+
export const storageFailpointLayer = (
|
|
290
|
+
options: SqliteStorageOptions,
|
|
291
|
+
): Layer.Layer<SqliteStorageFailpoint> =>
|
|
292
|
+
options.failpoint === undefined
|
|
293
|
+
? SqliteStorageFailpoint.layer
|
|
294
|
+
: Layer.succeed(SqliteStorageFailpoint)({ hit: options.failpoint })
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* A composition-root convenience Layer for canonical Threads. Durable accepted work is
|
|
298
|
+
* served by the separate SubmissionLedger port.
|
|
299
|
+
*/
|
|
300
|
+
export const layer = (
|
|
301
|
+
options: SqliteStorageOptions,
|
|
302
|
+
): Layer.Layer<
|
|
303
|
+
ThreadStore | ThreadReader | ThreadImport | StorageOwner,
|
|
304
|
+
SqliteStorageInitializationError
|
|
305
|
+
> =>
|
|
306
|
+
Layer.unwrap(
|
|
307
|
+
Effect.map(SqliteStorageConfig, (config) =>
|
|
308
|
+
threadStoreLayer.pipe(
|
|
309
|
+
Layer.provide(
|
|
310
|
+
Layer.mergeAll(
|
|
311
|
+
Layer.succeed(SqliteStorageConfig)(config),
|
|
312
|
+
storageFailpointLayer(options),
|
|
313
|
+
// The journal enables WAL only after proving the file is fresh.
|
|
314
|
+
sqliteClientLayer({ filename: options.filename, disableWAL: true }, 'open SQLite'),
|
|
315
|
+
NodeCrypto.layer,
|
|
316
|
+
),
|
|
317
|
+
),
|
|
318
|
+
)),
|
|
319
|
+
).pipe(Layer.provide(storageConfigLayer(options)))
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Export a quiesced current-layout database without initializing layout or retiring
|
|
323
|
+
* ownership. Its read-only connection closes before returning; the source remains untouched.
|
|
324
|
+
*/
|
|
325
|
+
export const exportThread = Effect.fn('SqliteThreadStore.exportThread')(function*(
|
|
326
|
+
options: Pick<SqliteStorageOptions, 'filename' | 'busyTimeout'>,
|
|
327
|
+
request: ThreadExportRequest,
|
|
328
|
+
) {
|
|
329
|
+
yield* checkExportSource(request.threadId)
|
|
330
|
+
|
|
331
|
+
return yield* Effect.gen(function*() {
|
|
332
|
+
const sql = yield* SqlClientService.SqlClient
|
|
333
|
+
|
|
334
|
+
const owner = yield* sqlStorageOwner(sql)
|
|
335
|
+
|
|
336
|
+
return yield* owner.withMutation(
|
|
337
|
+
makeSqlTransaction(sql, { begin: 'BEGIN' })(
|
|
338
|
+
Effect.gen(function*() {
|
|
339
|
+
yield* readSqliteStorageHeader()
|
|
340
|
+
|
|
341
|
+
const transfer = yield* makeSqlThreadImport({
|
|
342
|
+
owner,
|
|
343
|
+
offsetPrefix: 'effect-agent-sqlite@1:',
|
|
344
|
+
read: (body) => body,
|
|
345
|
+
write: () => Effect.die('A read-only exporter cannot import'),
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
return yield* transfer.export(request)
|
|
349
|
+
}),
|
|
350
|
+
).pipe(
|
|
351
|
+
Effect.catchTag('SqlError', (cause) =>
|
|
352
|
+
SqliteStorageError.make({
|
|
353
|
+
operation: 'export Thread snapshot',
|
|
354
|
+
message: cause.message,
|
|
355
|
+
cause,
|
|
356
|
+
})),
|
|
357
|
+
),
|
|
358
|
+
)
|
|
359
|
+
}).pipe(
|
|
360
|
+
Effect.scoped,
|
|
361
|
+
Effect.provide(
|
|
362
|
+
Layer.merge(
|
|
363
|
+
sqliteClientLayer(
|
|
364
|
+
{
|
|
365
|
+
filename: options.filename,
|
|
366
|
+
readonly: true,
|
|
367
|
+
disableWAL: true,
|
|
368
|
+
busyTimeout: options.busyTimeout ?? 5_000,
|
|
369
|
+
},
|
|
370
|
+
'open SQLite for export',
|
|
371
|
+
),
|
|
372
|
+
NodeCrypto.layer,
|
|
373
|
+
),
|
|
374
|
+
),
|
|
375
|
+
)
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
/** Read-only page exports retain native diagnostics in the transfer port's typed storage failure. */
|
|
379
|
+
export const exportSourceLayer = (
|
|
380
|
+
options: Pick<SqliteStorageOptions, 'filename' | 'busyTimeout'>,
|
|
381
|
+
): Layer.Layer<ThreadExportSource> => {
|
|
382
|
+
const failure = (cause: SqliteStorageInitializationError) =>
|
|
383
|
+
ThreadStoreError.make({ operation: 'export Thread snapshot', message: cause.message, cause })
|
|
384
|
+
|
|
385
|
+
return Layer.succeed(ThreadExportSource, {
|
|
386
|
+
export: (request) =>
|
|
387
|
+
exportThread(options, request).pipe(
|
|
388
|
+
Effect.catchTags({
|
|
389
|
+
SqliteStorageCompatibilityError: failure,
|
|
390
|
+
SqliteStorageCorruptionError: failure,
|
|
391
|
+
SqliteStorageError: failure,
|
|
392
|
+
}),
|
|
393
|
+
),
|
|
394
|
+
})
|
|
395
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * as SqliteActivityStore from './SqliteActivityStore'
|
|
2
|
+
export * as SqliteMessageDeliveryStore from './SqliteMessageDeliveryStore'
|
|
3
|
+
export * as SqliteScheduleStore from './SqliteScheduleStore'
|
|
4
|
+
export * as SqliteStorageConfig from './SqliteStorageConfig'
|
|
5
|
+
export * as SqliteStorageError from './SqliteStorageError'
|
|
6
|
+
export * as SqliteStorageFailpoint from './SqliteStorageFailpoint'
|
|
7
|
+
export * as SqliteStorageVersion from './SqliteStorageVersion'
|
|
8
|
+
export * as SqliteSubmissionLedger from './SqliteSubmissionLedger'
|
|
9
|
+
export * as SqliteSubscriptionStore from './SqliteSubscriptionStore'
|
|
10
|
+
export * as SqliteThreadStore from './SqliteThreadStore'
|