@systemfsoftware/agent-storage-sqlite 0.0.0-stage → 0.1.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 (75) hide show
  1. package/LICENSE +21 -0
  2. package/dist/SqliteActivityStore-CSvw-3OJ.mjs +25 -0
  3. package/dist/SqliteActivityStore-CSvw-3OJ.mjs.map +1 -0
  4. package/dist/SqliteActivityStore-DM-gMSMv.d.mts +15 -0
  5. package/dist/SqliteActivityStore.d.mts +2 -0
  6. package/dist/SqliteActivityStore.mjs +1 -0
  7. package/dist/SqliteMessageDeliveryStore-C0D-jYWc.mjs +18 -0
  8. package/dist/SqliteMessageDeliveryStore-C0D-jYWc.mjs.map +1 -0
  9. package/dist/SqliteMessageDeliveryStore-D1pwnufl.d.mts +11 -0
  10. package/dist/SqliteMessageDeliveryStore.d.mts +2 -0
  11. package/dist/SqliteMessageDeliveryStore.mjs +1 -0
  12. package/dist/SqliteScheduleStore-CrH-tLQB.d.mts +12 -0
  13. package/dist/SqliteScheduleStore-Nph_W_Lg.mjs +19 -0
  14. package/dist/SqliteScheduleStore-Nph_W_Lg.mjs.map +1 -0
  15. package/dist/SqliteScheduleStore.d.mts +2 -0
  16. package/dist/SqliteScheduleStore.mjs +1 -0
  17. package/dist/SqliteStorageConfig-6HRtBHf8.d.mts +27 -0
  18. package/dist/SqliteStorageConfig-DN-WwQrw.mjs +32 -0
  19. package/dist/SqliteStorageConfig-DN-WwQrw.mjs.map +1 -0
  20. package/dist/SqliteStorageConfig.d.mts +2 -0
  21. package/dist/SqliteStorageConfig.mjs +1 -0
  22. package/dist/SqliteStorageError-Bc-iVib1.d.mts +71 -0
  23. package/dist/SqliteStorageError-d_tygf_W.mjs +79 -0
  24. package/dist/SqliteStorageError-d_tygf_W.mjs.map +1 -0
  25. package/dist/SqliteStorageError.d.mts +2 -0
  26. package/dist/SqliteStorageError.mjs +1 -0
  27. package/dist/SqliteStorageFailpoint-BqxmxFBb.mjs +15 -0
  28. package/dist/SqliteStorageFailpoint-BqxmxFBb.mjs.map +1 -0
  29. package/dist/SqliteStorageFailpoint-CJRyxmsW.d.mts +17 -0
  30. package/dist/SqliteStorageFailpoint.d.mts +2 -0
  31. package/dist/SqliteStorageFailpoint.mjs +1 -0
  32. package/dist/SqliteStorageVersion-Bayz1P9w.d.mts +8 -0
  33. package/dist/SqliteStorageVersion-BxAVd_1t.mjs +7 -0
  34. package/dist/SqliteStorageVersion-BxAVd_1t.mjs.map +1 -0
  35. package/dist/SqliteStorageVersion.d.mts +3 -0
  36. package/dist/SqliteStorageVersion.mjs +2 -0
  37. package/dist/SqliteSubmissionLedger-2lHitXAA.d.mts +25 -0
  38. package/dist/SqliteSubmissionLedger-DTWaRPjV.mjs +46 -0
  39. package/dist/SqliteSubmissionLedger-DTWaRPjV.mjs.map +1 -0
  40. package/dist/SqliteSubmissionLedger.d.mts +2 -0
  41. package/dist/SqliteSubmissionLedger.mjs +1 -0
  42. package/dist/SqliteSubscriptionStore-CFmi_ULJ.mjs +21 -0
  43. package/dist/SqliteSubscriptionStore-CFmi_ULJ.mjs.map +1 -0
  44. package/dist/SqliteSubscriptionStore-CRkOEBaa.d.mts +11 -0
  45. package/dist/SqliteSubscriptionStore.d.mts +2 -0
  46. package/dist/SqliteSubscriptionStore.mjs +1 -0
  47. package/dist/SqliteThreadStore-CUkk9A0Y.mjs +235 -0
  48. package/dist/SqliteThreadStore-CUkk9A0Y.mjs.map +1 -0
  49. package/dist/SqliteThreadStore-DKac999S.d.mts +89 -0
  50. package/dist/SqliteThreadStore.d.mts +2 -0
  51. package/dist/SqliteThreadStore.mjs +1 -0
  52. package/dist/index.d.mts +11 -0
  53. package/dist/index.mjs +11 -0
  54. package/dist/migrations-_uIpJR5O.mjs +240 -0
  55. package/dist/migrations-_uIpJR5O.mjs.map +1 -0
  56. package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
  57. package/dist/sqlite-journal-3Zguf9OC.mjs +112 -0
  58. package/dist/sqlite-journal-3Zguf9OC.mjs.map +1 -0
  59. package/package.json +87 -3
  60. package/src/SqliteActivityStore.ts +35 -0
  61. package/src/SqliteMessageDeliveryStore.ts +23 -0
  62. package/src/SqliteScheduleStore.ts +38 -0
  63. package/src/SqliteStorageConfig.ts +31 -0
  64. package/src/SqliteStorageError.ts +96 -0
  65. package/src/SqliteStorageFailpoint.ts +22 -0
  66. package/src/SqliteStorageVersion.ts +2 -0
  67. package/src/SqliteSubmissionLedger.ts +80 -0
  68. package/src/SqliteSubscriptionStore.ts +31 -0
  69. package/src/SqliteThreadStore.ts +395 -0
  70. package/src/index.ts +10 -0
  71. package/src/internal/migrations.ts +396 -0
  72. package/src/internal/sqlite-client-layer.ts +19 -0
  73. package/src/internal/sqlite-journal.ts +205 -0
  74. package/src/internal/sqlite-ledger-errors.ts +25 -0
  75. package/README.md +0 -3
@@ -0,0 +1,35 @@
1
+ import { makeSqlActivityStore } from '@systemfsoftware/agent-storage-sql/sql-activity-store'
2
+ import {
3
+ ActivityMutationFailpoint,
4
+ type ActivityMutationFailure,
5
+ ActivityProcessorStore,
6
+ type ActivityStoreError,
7
+ } from '@systemfsoftware/agent/activity-store'
8
+ import * as Effect from 'effect/Effect'
9
+ import * as Layer from 'effect/Layer'
10
+ import * as SqlClientService from 'effect/sql/SqlClient'
11
+
12
+ export type SqliteActivityInitializationError = ActivityStoreError | ActivityMutationFailure
13
+
14
+ const makeActivityStore = Effect.fnUntraced(function*() {
15
+ const sql = yield* SqlClientService.SqlClient
16
+
17
+ return yield* makeSqlActivityStore(sql.withTransaction)
18
+ })
19
+
20
+ /** SQLite activity progress with mutation failpoints kept injectable for recovery tests. */
21
+ export const activityProcessorStoreLayerWithFailpoints: Layer.Layer<
22
+ ActivityProcessorStore,
23
+ SqliteActivityInitializationError,
24
+ SqlClientService.SqlClient | ActivityMutationFailpoint
25
+ > = Layer.effect(ActivityProcessorStore, makeActivityStore())
26
+
27
+ /**
28
+ * Optional activity progress with scoped local sessions. Share the same SqlClient across
29
+ * live Layers for one physical database; separate processes cannot share an activity key.
30
+ */
31
+ export const activityProcessorStoreLayer: Layer.Layer<
32
+ ActivityProcessorStore,
33
+ SqliteActivityInitializationError,
34
+ SqlClientService.SqlClient
35
+ > = activityProcessorStoreLayerWithFailpoints.pipe(Layer.provide(ActivityMutationFailpoint.layer))
@@ -0,0 +1,23 @@
1
+ import {
2
+ makeSqlMessageDeliveryStore,
3
+ registerCanonicalMessageWriter,
4
+ } from '@systemfsoftware/agent-storage-sql/sql-message-delivery-store'
5
+ import { MessageDeliveryStore, type MessageDeliveryStoreLimits } from '@systemfsoftware/agent/message-delivery'
6
+ import * as Effect from 'effect/Effect'
7
+ import * as Layer from 'effect/Layer'
8
+
9
+ import { initializeSqliteJournal } from './internal/sqlite-journal'
10
+
11
+ /** Source-owned message obligations in the host's existing SQLite database. */
12
+ export const messageDeliveryStoreLayer = (limits?: MessageDeliveryStoreLimits) =>
13
+ Layer.effect(
14
+ MessageDeliveryStore,
15
+ Effect.gen(function*() {
16
+ const journal = yield* initializeSqliteJournal
17
+ const store = yield* makeSqlMessageDeliveryStore(journal.owner, limits)
18
+
19
+ yield* registerCanonicalMessageWriter(journal.owner, store)
20
+
21
+ return store
22
+ }),
23
+ )
@@ -0,0 +1,38 @@
1
+ import {
2
+ makeSqlScheduleStore,
3
+ SqlScheduleWriteTransaction,
4
+ } from '@systemfsoftware/agent-storage-sql/sql-schedule-store'
5
+ import { ScheduleStore } from '@systemfsoftware/agent/schedule'
6
+ import * as Effect from 'effect/Effect'
7
+ import * as Layer from 'effect/Layer'
8
+ import * as SqlClient from 'effect/sql/SqlClient'
9
+
10
+ import { initializeSqliteStorage } from './internal/sqlite-journal'
11
+ import type { SqliteStorageConfig } from './SqliteStorageConfig'
12
+ import type { SqliteStorageFailpoint } from './SqliteStorageFailpoint'
13
+ import type { SqliteStorageInitializationError } from './SqliteThreadStore'
14
+
15
+ /** SQLite implementation of the atomic ScheduleStore port. */
16
+ export const scheduleStoreLayer: Layer.Layer<
17
+ ScheduleStore,
18
+ SqliteStorageInitializationError,
19
+ SqliteStorageConfig | SqliteStorageFailpoint | SqlClient.SqlClient
20
+ > = Layer.effect(
21
+ ScheduleStore,
22
+ Effect.gen(function*() {
23
+ yield* initializeSqliteStorage()
24
+
25
+ return yield* makeSqlScheduleStore()
26
+ }),
27
+ ).pipe(
28
+ Layer.provide(
29
+ Layer.effect(
30
+ SqlScheduleWriteTransaction,
31
+ Effect.gen(function*() {
32
+ const sql = yield* SqlClient.SqlClient
33
+
34
+ return { run: sql.withTransaction }
35
+ }),
36
+ ),
37
+ ),
38
+ )
@@ -0,0 +1,31 @@
1
+ import * as Context from 'effect/Context'
2
+ import * as Schema from 'effect/Schema'
3
+
4
+ const ObservationPollInterval = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))
5
+ const BusyTimeoutMillis = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))
6
+
7
+ /**
8
+ * Validated construction configuration consumed by the SQLite storage Layer. The database
9
+ * identity itself belongs to the SqlClient Layer; duplicating it here could silently diverge
10
+ * from the connection actually in use.
11
+ */
12
+ export class SqliteStorageConfigValue extends Schema.Class<SqliteStorageConfigValue>(
13
+ '@effect-agent/storage-sqlite/SqliteStorageConfigValue',
14
+ )({
15
+ observationPollInterval: ObservationPollInterval,
16
+ /** Bounded SQLITE_BUSY retry window for write-lock acquisition, in milliseconds. */
17
+ busyTimeout: BusyTimeoutMillis,
18
+ /** Connection synchronization selected at construction; FULL is the convenience default. */
19
+ synchronous: Schema.Literals(['FULL', 'NORMAL']),
20
+ /**
21
+ * Audit stored payload schemas, identities, and batch coverage while opening the store.
22
+ * Per-operation Schema decoding remains enabled; this complete scan is an explicit opt-in.
23
+ */
24
+ verifyOnOpen: Schema.Boolean,
25
+ }) {}
26
+
27
+ /** Explicit SQLite storage configuration authority. */
28
+ export class SqliteStorageConfig extends Context.Service<
29
+ SqliteStorageConfig,
30
+ SqliteStorageConfigValue
31
+ >()('@effect-agent/storage-sqlite/SqliteStorageConfig') {}
@@ -0,0 +1,96 @@
1
+ import { SqlStorageFailpointLocation as SqliteStorageFailpointLocation } from '@systemfsoftware/agent-storage-sql/sql-storage-failpoint'
2
+ import { CanonicalSequence } from '@systemfsoftware/agent/records'
3
+ import * as Schema from 'effect/Schema'
4
+
5
+ /** The SQLite file uses a private-development format this adapter cannot read. */
6
+ export class SqliteStorageCompatibilityError extends Schema.TaggedError<SqliteStorageCompatibilityError>()(
7
+ 'SqliteStorageCompatibilityError',
8
+ {
9
+ actualVersion: Schema.Int,
10
+ message: Schema.String,
11
+ supportedVersion: Schema.Int,
12
+ },
13
+ ) {}
14
+
15
+ /** Stored bytes failed the current Schema and cannot be used as recovery truth. */
16
+ export class SqliteStorageCorruptionError extends Schema.TaggedError<SqliteStorageCorruptionError>()(
17
+ 'SqliteStorageCorruptionError',
18
+ {
19
+ message: Schema.String,
20
+ rowKey: Schema.String,
21
+ table: Schema.String,
22
+ },
23
+ ) {}
24
+
25
+ /** SQLite infrastructure failed while opening or operating the store. */
26
+ export class SqliteStorageError extends Schema.TaggedError<SqliteStorageError>()(
27
+ 'SqliteStorageError',
28
+ {
29
+ cause: Schema.optionalKey(Schema.Defect()),
30
+ message: Schema.String,
31
+ operation: Schema.String,
32
+ },
33
+ ) {}
34
+
35
+ /**
36
+ * SQLite infrastructure failed while operating the Submission Ledger. Surfaces at the
37
+ * SubmissionLedger port as the typed `LedgerError` with this error preserved as its cause,
38
+ * so the adapter-level tag is never erased.
39
+ */
40
+ export class SqliteLedgerError extends Schema.TaggedError<SqliteLedgerError>()(
41
+ 'SqliteLedgerError',
42
+ {
43
+ cause: Schema.optionalKey(Schema.Defect()),
44
+ message: Schema.String,
45
+ operation: Schema.String,
46
+ },
47
+ ) {}
48
+
49
+ /**
50
+ * A canonical batch retry conflicts with existing append state. Tail conflicts carry the
51
+ * actual committed tail as a diagnostic resume hint.
52
+ */
53
+ export class SqliteAppendConflict extends Schema.TaggedError<SqliteAppendConflict>()(
54
+ 'SqliteAppendConflict',
55
+ {
56
+ message: Schema.String,
57
+ reason: Schema.Literals(['batch-content', 'record-identity', 'tail', 'stale-command']),
58
+ actualTailSequence: Schema.optionalKey(CanonicalSequence),
59
+ },
60
+ ) {}
61
+
62
+ /**
63
+ * A write transaction could not acquire the SQLite write lock within the configured busy
64
+ * timeout (SQLITE_BUSY / SQLITE_LOCKED). Another transiently coexisting owner is writing;
65
+ * the operation did not mutate canonical state and is safe to retry.
66
+ */
67
+ export class SqliteWriteContention extends Schema.TaggedError<SqliteWriteContention>()(
68
+ 'SqliteWriteContention',
69
+ {
70
+ cause: Schema.optionalKey(Schema.Defect()),
71
+ message: Schema.String,
72
+ operation: Schema.String,
73
+ },
74
+ ) {}
75
+
76
+ /** A checkpoint conflicts with a previously stored checkpoint at the same offset. */
77
+ export class SqliteCheckpointConflict extends Schema.TaggedError<SqliteCheckpointConflict>()(
78
+ 'SqliteCheckpointConflict',
79
+ {
80
+ message: Schema.String,
81
+ },
82
+ ) {}
83
+
84
+ export { SqlStorageFailpointLocation as SqliteStorageFailpointLocation } from '@systemfsoftware/agent-storage-sql/sql-storage-failpoint'
85
+
86
+ /** Deterministic test-only fault or pause injected at a SQLite operation boundary. */
87
+ export class SqliteStorageFailpointError extends Schema.TaggedError<SqliteStorageFailpointError>()(
88
+ 'SqliteStorageFailpointError',
89
+ {
90
+ location: SqliteStorageFailpointLocation,
91
+ },
92
+ ) {
93
+ override get message() {
94
+ return `Injected SQLite storage failure at ${this.location}.`
95
+ }
96
+ }
@@ -0,0 +1,22 @@
1
+ import * as Context from 'effect/Context'
2
+ import * as Effect from 'effect/Effect'
3
+ import * as Layer from 'effect/Layer'
4
+
5
+ import type { SqliteStorageFailpointError, SqliteStorageFailpointLocation } from './SqliteStorageError'
6
+
7
+ export type SqliteStorageFailpointHandler = (
8
+ location: SqliteStorageFailpointLocation,
9
+ ) => Effect.Effect<void, SqliteStorageFailpointError>
10
+
11
+ const noFailpoint: SqliteStorageFailpointHandler = () => Effect.void
12
+
13
+ /** Explicit fault-injection authority used at SQLite operation boundaries. */
14
+ export class SqliteStorageFailpoint extends Context.Service<
15
+ SqliteStorageFailpoint,
16
+ {
17
+ readonly hit: SqliteStorageFailpointHandler
18
+ }
19
+ >()('@effect-agent/storage-sqlite/SqliteStorageFailpoint') {
20
+ /** Production default: no fault injection. */
21
+ static readonly layer = Layer.succeed(this)({ hit: noFailpoint })
22
+ }
@@ -0,0 +1,2 @@
1
+ /** Current adapter layout version; record encoding is versioned independently. */
2
+ export { CurrentSqliteStorageVersion } from './internal/migrations'
@@ -0,0 +1,80 @@
1
+ import * as NodeCrypto from '@effect/platform-node/NodeCrypto'
2
+ import { makeSqlSubmissionLedgerKernel } from '@systemfsoftware/agent-storage-sql/sql-submission-ledger'
3
+ import { StorageOwner } from '@systemfsoftware/agent/run-storage'
4
+ import { SettlementPublisher } from '@systemfsoftware/agent/settlement-publisher'
5
+ import { SubmissionLedger } from '@systemfsoftware/agent/submission-ledger'
6
+ import * as Context from 'effect/Context'
7
+ import type * as CryptoDigest from 'effect/crypto/Digest'
8
+ import type * as SecureRandom from 'effect/crypto/SecureRandom'
9
+ import * as Effect from 'effect/Effect'
10
+ import * as Layer from 'effect/Layer'
11
+ import type * as SqlClientService from 'effect/sql/SqlClient'
12
+
13
+ import { sqliteClientLayer } from './internal/sqlite-client-layer'
14
+ import { initializeSqliteJournal, sqliteErrors } from './internal/sqlite-journal'
15
+ import { sqlFailure } from './internal/sqlite-ledger-errors'
16
+ import { SqliteStorageConfig } from './SqliteStorageConfig'
17
+ import { SqliteStorageFailpoint } from './SqliteStorageFailpoint'
18
+ import {
19
+ exclusiveHostClientLayer,
20
+ type SqliteStorageInitializationError,
21
+ type SqliteStorageOptions,
22
+ storageConfigLayer,
23
+ storageFailpointLayer,
24
+ } from './SqliteThreadStore'
25
+
26
+ const makeServices = Effect.fnUntraced(function*() {
27
+ const failpoint = yield* SqliteStorageFailpoint
28
+ const journal = yield* initializeSqliteJournal
29
+
30
+ const services = yield* makeSqlSubmissionLedgerKernel(journal, {
31
+ errors: sqliteErrors,
32
+ hitFailpoint: failpoint.hit,
33
+ offsetPrefix: 'effect-agent-sqlite@1:',
34
+ sqlFailure,
35
+ })
36
+
37
+ return Context.make(SubmissionLedger, services.ledger).pipe(
38
+ Context.add(SettlementPublisher, services.publisher),
39
+ Context.add(StorageOwner, journal.owner),
40
+ )
41
+ })
42
+
43
+ /**
44
+ * SQLite submission transitions and canonical settlement publication share the journal's
45
+ * database file, scoped owner, and mutation gate. Configuration, failpoint, SQL, Digest, and SecureRandom
46
+ * authority stay visible in the input channel.
47
+ */
48
+ export const submissionLedgerLayer: Layer.Layer<
49
+ SubmissionLedger | SettlementPublisher | StorageOwner,
50
+ SqliteStorageInitializationError,
51
+ | SqliteStorageConfig
52
+ | SqliteStorageFailpoint
53
+ | SqlClientService.SqlClient
54
+ | CryptoDigest.Digest
55
+ | SecureRandom.SecureRandom
56
+ > = Layer.effectContext(makeServices()).pipe(Layer.provide(exclusiveHostClientLayer))
57
+
58
+ /**
59
+ * A composition-root Layer for submission transitions and settlement publication. Point it
60
+ * at the same exclusive client as ThreadStore so every mutation shares its local owner.
61
+ */
62
+ export const ledgerLayer = (
63
+ options: SqliteStorageOptions,
64
+ ): Layer.Layer<
65
+ SubmissionLedger | SettlementPublisher | StorageOwner,
66
+ SqliteStorageInitializationError
67
+ > =>
68
+ Layer.unwrap(
69
+ Effect.map(SqliteStorageConfig, (config) =>
70
+ submissionLedgerLayer.pipe(
71
+ Layer.provide(
72
+ Layer.mergeAll(
73
+ Layer.succeed(SqliteStorageConfig)(config),
74
+ storageFailpointLayer(options),
75
+ sqliteClientLayer({ filename: options.filename, disableWAL: true }, 'open SQLite'),
76
+ NodeCrypto.layer,
77
+ ),
78
+ ),
79
+ )),
80
+ ).pipe(Layer.provide(storageConfigLayer(options)))
@@ -0,0 +1,31 @@
1
+ import { makeSqlSubscriptionStore } from '@systemfsoftware/agent-storage-sql/sql-subscription-store'
2
+ import { SourcePartition, SubscriptionError, SubscriptionStore } from '@systemfsoftware/agent/subscription'
3
+ import * as Effect from 'effect/Effect'
4
+ import * as Layer from 'effect/Layer'
5
+ import * as Schema from 'effect/Schema'
6
+ import type * as SqlClientService from 'effect/sql/SqlClient'
7
+
8
+ import { initializeSqliteStorage } from './internal/sqlite-journal'
9
+ import type { SqliteStorageConfig } from './SqliteStorageConfig'
10
+ import type { SqliteStorageFailpoint } from './SqliteStorageFailpoint'
11
+ import type { SqliteStorageInitializationError } from './SqliteThreadStore'
12
+
13
+ const makeSubscriptionStore = Effect.fnUntraced(function*(owned: SourcePartition) {
14
+ const partition = yield* Schema.decodeEffect(SourcePartition)(owned).pipe(
15
+ Effect.mapError(() => SubscriptionError.make({ reason: 'validation', code: 'partition' })),
16
+ )
17
+
18
+ yield* initializeSqliteStorage()
19
+
20
+ return yield* makeSqlSubscriptionStore(partition, {
21
+ maxStoredJsonLength: 16 * 1024 * 1024,
22
+ })
23
+ })
24
+
25
+ export const subscriptionStoreLayer = (
26
+ partition: SourcePartition,
27
+ ): Layer.Layer<
28
+ SubscriptionStore,
29
+ SqliteStorageInitializationError | SubscriptionError,
30
+ SqliteStorageConfig | SqliteStorageFailpoint | SqlClientService.SqlClient
31
+ > => Layer.effect(SubscriptionStore, makeSubscriptionStore(partition))