@systemfsoftware/agent-storage-cloudflare 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 (101) hide show
  1. package/LICENSE +21 -0
  2. package/dist/DoMemoryStore-AaGtt8CZ.d.mts +29 -0
  3. package/dist/DoMemoryStore-DVun329p.mjs +67 -0
  4. package/dist/DoMemoryStore-DVun329p.mjs.map +1 -0
  5. package/dist/DoMemoryStore.d.mts +2 -0
  6. package/dist/DoMemoryStore.mjs +1 -0
  7. package/dist/DoMessageDeliveryStore-BvB6OPI1.mjs +34 -0
  8. package/dist/DoMessageDeliveryStore-BvB6OPI1.mjs.map +1 -0
  9. package/dist/DoMessageDeliveryStore-Cb-DNN5s.d.mts +16 -0
  10. package/dist/DoMessageDeliveryStore.d.mts +2 -0
  11. package/dist/DoMessageDeliveryStore.mjs +1 -0
  12. package/dist/DoScheduleStore-BiTgDvX1.d.mts +36 -0
  13. package/dist/DoScheduleStore-C7GkSReF.mjs +167 -0
  14. package/dist/DoScheduleStore-C7GkSReF.mjs.map +1 -0
  15. package/dist/DoScheduleStore.d.mts +2 -0
  16. package/dist/DoScheduleStore.mjs +1 -0
  17. package/dist/DoStorageConfig-BqezsbD6.mjs +43 -0
  18. package/dist/DoStorageConfig-BqezsbD6.mjs.map +1 -0
  19. package/dist/DoStorageConfig-CuB6ZOtX.d.mts +36 -0
  20. package/dist/DoStorageConfig.d.mts +2 -0
  21. package/dist/DoStorageConfig.mjs +1 -0
  22. package/dist/DoStorageError-C-F5Z_FZ.d.mts +82 -0
  23. package/dist/DoStorageError-DSQzuCb0.mjs +138 -0
  24. package/dist/DoStorageError-DSQzuCb0.mjs.map +1 -0
  25. package/dist/DoStorageError.d.mts +2 -0
  26. package/dist/DoStorageError.mjs +1 -0
  27. package/dist/DoStorageFailpoint-C7zLUULu.d.mts +19 -0
  28. package/dist/DoStorageFailpoint-WO3B_DQl.mjs +20 -0
  29. package/dist/DoStorageFailpoint-WO3B_DQl.mjs.map +1 -0
  30. package/dist/DoStorageFailpoint.d.mts +2 -0
  31. package/dist/DoStorageFailpoint.mjs +1 -0
  32. package/dist/DoStorageFailpointTesting.d.mts +21 -0
  33. package/dist/DoStorageFailpointTesting.mjs +18 -0
  34. package/dist/DoStorageFailpointTesting.mjs.map +1 -0
  35. package/dist/DoStorageVersion-BrolfBDS.mjs +7 -0
  36. package/dist/DoStorageVersion-BrolfBDS.mjs.map +1 -0
  37. package/dist/DoStorageVersion-D7CIxFc_.d.mts +8 -0
  38. package/dist/DoStorageVersion.d.mts +3 -0
  39. package/dist/DoStorageVersion.mjs +2 -0
  40. package/dist/DoSubmissionLedger-Oq5OstxL.mjs +1878 -0
  41. package/dist/DoSubmissionLedger-Oq5OstxL.mjs.map +1 -0
  42. package/dist/DoSubmissionLedger-tLjwidUn.d.mts +25 -0
  43. package/dist/DoSubmissionLedger.d.mts +2 -0
  44. package/dist/DoSubmissionLedger.mjs +1 -0
  45. package/dist/DoSubscriptionStore-BMJiFxt3.mjs +185 -0
  46. package/dist/DoSubscriptionStore-BMJiFxt3.mjs.map +1 -0
  47. package/dist/DoSubscriptionStore-zx6nfbbh.d.mts +25 -0
  48. package/dist/DoSubscriptionStore.d.mts +2 -0
  49. package/dist/DoSubscriptionStore.mjs +1 -0
  50. package/dist/DoThreadStore-2zTSfui-.d.mts +81 -0
  51. package/dist/DoThreadStore-CeYHcRR9.mjs +411 -0
  52. package/dist/DoThreadStore-CeYHcRR9.mjs.map +1 -0
  53. package/dist/DoThreadStore.d.mts +2 -0
  54. package/dist/DoThreadStore.mjs +5 -0
  55. package/dist/MemoryProtocol-CtCFYe4s.mjs +221 -0
  56. package/dist/MemoryProtocol-CtCFYe4s.mjs.map +1 -0
  57. package/dist/MemoryProtocol-DXV77CcJ.d.mts +38160 -0
  58. package/dist/MemoryProtocol.d.mts +2 -0
  59. package/dist/MemoryProtocol.mjs +1 -0
  60. package/dist/PortProtocol-4xvMyCgP.mjs +322 -0
  61. package/dist/PortProtocol-4xvMyCgP.mjs.map +1 -0
  62. package/dist/PortProtocol-DXuguAgD.d.mts +966 -0
  63. package/dist/PortProtocol.d.mts +2 -0
  64. package/dist/PortProtocol.mjs +1 -0
  65. package/dist/PortRouting-B9302ouX.mjs +683 -0
  66. package/dist/PortRouting-B9302ouX.mjs.map +1 -0
  67. package/dist/PortRouting-DsoI6soW.d.mts +105 -0
  68. package/dist/PortRouting.d.mts +2 -0
  69. package/dist/PortRouting.mjs +1 -0
  70. package/dist/do-journal-nPNEAjV5.mjs +769 -0
  71. package/dist/do-journal-nPNEAjV5.mjs.map +1 -0
  72. package/dist/index.d.mts +14 -0
  73. package/dist/index.mjs +14 -0
  74. package/dist/migrations-BRTvj8Ue.mjs +233 -0
  75. package/dist/migrations-BRTvj8Ue.mjs.map +1 -0
  76. package/dist/owned-state-DrOD9s0c.mjs +157 -0
  77. package/dist/owned-state-DrOD9s0c.mjs.map +1 -0
  78. package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
  79. package/package.json +106 -3
  80. package/src/DoMemoryStore.ts +71 -0
  81. package/src/DoMessageDeliveryStore.ts +46 -0
  82. package/src/DoScheduleStore.ts +276 -0
  83. package/src/DoStorageConfig.ts +45 -0
  84. package/src/DoStorageError.ts +146 -0
  85. package/src/DoStorageFailpoint.ts +25 -0
  86. package/src/DoStorageFailpointTesting.ts +42 -0
  87. package/src/DoStorageVersion.ts +2 -0
  88. package/src/DoSubmissionLedger.ts +3980 -0
  89. package/src/DoSubscriptionStore.ts +331 -0
  90. package/src/DoThreadStore.ts +602 -0
  91. package/src/MemoryProtocol.ts +357 -0
  92. package/src/PortProtocol.ts +626 -0
  93. package/src/PortRouting.ts +1888 -0
  94. package/src/index.ts +13 -0
  95. package/src/internal/canonical-append.ts +15 -0
  96. package/src/internal/do-journal.ts +1449 -0
  97. package/src/internal/migrations.ts +395 -0
  98. package/src/internal/owned-state.ts +300 -0
  99. package/src/internal/prompt-checkpoint.ts +170 -0
  100. package/src/internal/storage-span.ts +54 -0
  101. package/README.md +0 -3
@@ -0,0 +1,357 @@
1
+ import * as MemoryNamespace from '@systemfsoftware/agent/memory-namespace'
2
+ import { MemoryLookup, MemoryRecallError, MemoryRecallLimits } from '@systemfsoftware/agent/memory-reference'
3
+ import { MemoryAccess, revalidateMemoryLookup } from '@systemfsoftware/agent/memory-revalidation'
4
+ import {
5
+ MemoryConflict,
6
+ MemoryDocument,
7
+ MemoryKey,
8
+ MemoryMutationFailure,
9
+ MemoryOperationConflict,
10
+ MemoryReader,
11
+ MemoryStorageError,
12
+ MemoryWithdrawn,
13
+ MemoryWrite,
14
+ MemoryWriter,
15
+ } from '@systemfsoftware/agent/memory-store'
16
+ import {
17
+ MemoryIndexError,
18
+ MemoryIndexSearch,
19
+ SemanticMemoryProfile,
20
+ } from '@systemfsoftware/agent/semantic-memory-index'
21
+ import {
22
+ revalidateSemanticMemoryCandidates,
23
+ SemanticCandidateLimits,
24
+ SemanticCandidateResult,
25
+ SemanticMemoryError,
26
+ } from '@systemfsoftware/agent/semantic-memory-revalidation'
27
+ import { Principal } from '@systemfsoftware/agent/submission-ledger'
28
+ import * as Clock from 'effect/Clock'
29
+ import * as Context from 'effect/Context'
30
+ import * as Effect from 'effect/Effect'
31
+ import * as Schema from 'effect/Schema'
32
+
33
+ export class MemoryRpcError extends Schema.TaggedError<MemoryRpcError>()('MemoryRpcError', {
34
+ reason: Schema.Literals(['denied', 'protocol', 'budget', 'timeout', 'unavailable']),
35
+ }) {}
36
+
37
+ export class MemoryRpcLimits extends Schema.Class<MemoryRpcLimits>(
38
+ '@effect-agent/storage-cloudflare/MemoryRpcLimits',
39
+ )({
40
+ maxRequestBytes: Schema.Int.check(Schema.isBetween({ minimum: 256, maximum: 4_194_304 })),
41
+ maxResponseBytes: Schema.Int.check(Schema.isBetween({ minimum: 256, maximum: 16_777_216 })),
42
+ maxSourceBytes: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 67_108_864 })),
43
+ maxSources: MemoryRecallLimits.fields.maxSources,
44
+ timeoutMillis: MemoryRecallLimits.fields.timeoutMillis,
45
+ }) {}
46
+
47
+ export const defaultMemoryRpcLimits = MemoryRpcLimits.make({
48
+ maxRequestBytes: 1_048_576,
49
+ maxResponseBytes: 4_194_304,
50
+ maxSourceBytes: 16_777_216,
51
+ maxSources: 16,
52
+ timeoutMillis: 10_000,
53
+ })
54
+
55
+ const RequestFields = {
56
+ version: Schema.Literal(1),
57
+ access: MemoryAccess.Wire,
58
+ /** Host-authenticated identity, never copied from model input. The owner still authorizes it. */
59
+ principal: Principal,
60
+ deadlineMillis: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),
61
+ }
62
+
63
+ const RevalidateRequest = Schema.TaggedStruct('Revalidate', {
64
+ ...RequestFields,
65
+ lookup: MemoryLookup,
66
+ limits: MemoryRecallLimits,
67
+ })
68
+
69
+ const ChangeRequest = Schema.TaggedStruct('Change', { ...RequestFields, write: MemoryWrite.Wire })
70
+
71
+ const GetRequest = Schema.TaggedStruct('Get', { ...RequestFields, key: MemoryKey.Wire })
72
+
73
+ const SemanticRequest: Schema.TaggedStruct<
74
+ 'RevalidateSemantic',
75
+ typeof RequestFields & {
76
+ readonly found: typeof MemoryIndexSearch.Wire
77
+ readonly profile: typeof SemanticMemoryProfile
78
+ readonly limits: typeof SemanticCandidateLimits
79
+ }
80
+ > = Schema.TaggedStruct('RevalidateSemantic', {
81
+ ...RequestFields,
82
+ found: MemoryIndexSearch.Wire,
83
+ profile: SemanticMemoryProfile,
84
+ limits: SemanticCandidateLimits,
85
+ })
86
+
87
+ export const MemoryOwnerRequest: Schema.Union<
88
+ [typeof RevalidateRequest, typeof ChangeRequest, typeof SemanticRequest, typeof GetRequest]
89
+ > = Schema.Union([RevalidateRequest, ChangeRequest, SemanticRequest, GetRequest])
90
+
91
+ export type MemoryOwnerRequest = typeof MemoryOwnerRequest.Type
92
+
93
+ export const MemoryOwnerFailure = Schema.Union([
94
+ MemoryRpcError,
95
+ MemoryStorageError,
96
+ MemoryRecallError,
97
+ MemoryConflict,
98
+ MemoryWithdrawn,
99
+ MemoryOperationConflict,
100
+ MemoryMutationFailure,
101
+ MemoryIndexError,
102
+ SemanticMemoryError,
103
+ ])
104
+
105
+ export type MemoryOwnerFailure = typeof MemoryOwnerFailure.Type
106
+
107
+ const LookupResponse = Schema.TaggedStruct('Lookup', {
108
+ access: MemoryAccess.Wire,
109
+ lookup: MemoryLookup,
110
+ })
111
+
112
+ const ChangedResponse = Schema.TaggedStruct('Changed', {
113
+ access: MemoryAccess.Wire,
114
+ document: MemoryDocument.Wire,
115
+ })
116
+
117
+ const SemanticResponse = Schema.TaggedStruct('Semantic', {
118
+ access: MemoryAccess.Wire,
119
+ result: SemanticCandidateResult,
120
+ })
121
+
122
+ const DocumentResponse = Schema.TaggedStruct('Document', {
123
+ access: MemoryAccess.Wire,
124
+ key: MemoryKey.Wire,
125
+ document: Schema.NullOr(MemoryDocument.Wire),
126
+ })
127
+
128
+ const FailedResponse = Schema.TaggedStruct('Failed', { failure: MemoryOwnerFailure })
129
+
130
+ export const MemoryOwnerResponse: Schema.Union<
131
+ [
132
+ typeof LookupResponse,
133
+ typeof ChangedResponse,
134
+ typeof SemanticResponse,
135
+ typeof DocumentResponse,
136
+ typeof FailedResponse,
137
+ ]
138
+ > = Schema.Union([
139
+ LookupResponse,
140
+ ChangedResponse,
141
+ SemanticResponse,
142
+ DocumentResponse,
143
+ FailedResponse,
144
+ ])
145
+
146
+ export type MemoryOwnerResponse = typeof MemoryOwnerResponse.Type
147
+
148
+ /**
149
+ * Fail-closed application policy. Authorize the namespace, principal, scope, and full command.
150
+ * Get requires exact-key authority, including application source/provenance checks where needed.
151
+ * Possession of a key or scope is not authorization, including for absent or withdrawn documents.
152
+ */
153
+ export class MemoryOwnerAuthorizer extends Context.Service<
154
+ MemoryOwnerAuthorizer,
155
+ {
156
+ readonly authorize: (request: MemoryOwnerRequest) => Effect.Effect<void, MemoryRpcError>
157
+ }
158
+ >()('@effect-agent/storage-cloudflare/MemoryOwnerAuthorizer') {}
159
+
160
+ /** Canonical namespace derived from this object's idFromName identity, never its request. */
161
+ export class MemoryOwnerIdentity extends Context.Service<
162
+ MemoryOwnerIdentity,
163
+ {
164
+ readonly namespace: MemoryNamespace.Any
165
+ }
166
+ >()('@effect-agent/storage-cloudflare/MemoryOwnerIdentity') {}
167
+
168
+ export const memoryWireBytes = (text: string): number => new TextEncoder().encode(text).byteLength
169
+
170
+ export const decodeMemoryWire = Effect.fnUntraced(function*<A, I>(
171
+ schema: Schema.Codec<A, I, never>,
172
+ raw: unknown,
173
+ maxBytes: number,
174
+ ) {
175
+ const text = yield* Schema.decodeUnknownEffect(Schema.String)(raw).pipe(
176
+ Effect.mapError(() => MemoryRpcError.make({ reason: 'protocol' })),
177
+ )
178
+
179
+ if (text.length > maxBytes || memoryWireBytes(text) > maxBytes) {
180
+ return yield* MemoryRpcError.make({ reason: 'budget' })
181
+ }
182
+
183
+ return yield* Schema.decodeEffect(Schema.fromJsonString(schema))(text).pipe(
184
+ Effect.mapError(() => MemoryRpcError.make({ reason: 'protocol' })),
185
+ )
186
+ })
187
+
188
+ export const encodeMemoryWire = Effect.fnUntraced(function*<A, I>(
189
+ schema: Schema.Codec<A, I, never, never>,
190
+ value: A,
191
+ maxBytes: number,
192
+ ) {
193
+ const encoded = yield* Schema.encodeEffect(Schema.fromJsonString(schema))(value).pipe(
194
+ Effect.mapError(() => MemoryRpcError.make({ reason: 'protocol' })),
195
+ )
196
+
197
+ if (encoded.length > maxBytes || memoryWireBytes(encoded) > maxBytes) {
198
+ return yield* MemoryRpcError.make({ reason: 'budget' })
199
+ }
200
+
201
+ return encoded
202
+ })
203
+
204
+ /** One local read for Get, or per distinct candidate source. No discovery or background work. */
205
+ export const handleMemoryOwnerRequest = Effect.fnUntraced(function*(
206
+ raw: unknown,
207
+ limits: MemoryRpcLimits = defaultMemoryRpcLimits,
208
+ ): Effect.fn.Return<
209
+ string,
210
+ never,
211
+ MemoryOwnerIdentity | MemoryOwnerAuthorizer | MemoryReader | MemoryWriter
212
+ > {
213
+ const result = yield* Effect.gen(function*(): Effect.fn.Return<
214
+ MemoryOwnerResponse,
215
+ MemoryOwnerFailure,
216
+ MemoryOwnerIdentity | MemoryOwnerAuthorizer | MemoryReader | MemoryWriter
217
+ > {
218
+ limits = yield* Schema.decodeEffect(MemoryRpcLimits)(limits).pipe(
219
+ Effect.mapError(() => MemoryRpcError.make({ reason: 'protocol' })),
220
+ )
221
+ const request = yield* decodeMemoryWire(MemoryOwnerRequest, raw, limits.maxRequestBytes)
222
+ const { namespace } = yield* MemoryOwnerIdentity
223
+
224
+ if (
225
+ !MemoryNamespace.equals(namespace, request.access.namespace) ||
226
+ (request._tag === 'Get' && !MemoryNamespace.equals(namespace, request.key.namespace)) ||
227
+ (request._tag === 'Change' &&
228
+ !MemoryNamespace.equals(namespace, request.write.key.namespace)) ||
229
+ (request._tag === 'RevalidateSemantic' &&
230
+ request.found.candidates.some(
231
+ (candidate) => !MemoryNamespace.equals(namespace, candidate.key.namespace),
232
+ ))
233
+ ) {
234
+ return yield* MemoryRpcError.make({ reason: 'denied' })
235
+ }
236
+
237
+ const remaining = Math.min(
238
+ limits.timeoutMillis,
239
+ request.deadlineMillis - (yield* Clock.currentTimeMillis),
240
+ )
241
+
242
+ if (remaining <= 0) return yield* MemoryRpcError.make({ reason: 'timeout' })
243
+
244
+ return yield* Effect.gen(function*(): Effect.fn.Return<
245
+ MemoryOwnerResponse,
246
+ MemoryOwnerFailure,
247
+ MemoryOwnerAuthorizer | MemoryReader | MemoryWriter
248
+ > {
249
+ const authorizer = yield* MemoryOwnerAuthorizer
250
+
251
+ yield* authorizer.authorize(request)
252
+ if (request._tag === 'Get') {
253
+ const reader = yield* MemoryReader
254
+ const current = yield* reader.get(request.key)
255
+
256
+ const document = current === null ? null : yield* MemoryDocument.restore(namespace, current)
257
+
258
+ if (document !== null) {
259
+ if (document.key.id !== request.key.id || document.source.id !== request.key.id) {
260
+ return yield* MemoryStorageError.make({
261
+ operation: 'validate memory read identity',
262
+ reason: 'corrupt',
263
+ })
264
+ }
265
+ if (
266
+ document._tag === 'ActiveMemoryDocument' &&
267
+ !document.scopes.includes(request.access.scope)
268
+ ) {
269
+ return yield* MemoryRpcError.make({ reason: 'denied' })
270
+ }
271
+
272
+ yield* encodeMemoryWire(MemoryDocument.Wire, document, limits.maxSourceBytes)
273
+ }
274
+
275
+ return { _tag: 'Document', access: request.access, key: request.key, document }
276
+ }
277
+ if (request._tag === 'Change') {
278
+ const writer = yield* MemoryWriter
279
+
280
+ return {
281
+ _tag: 'Changed',
282
+ access: request.access,
283
+ document: yield* writer.change(request.write),
284
+ }
285
+ }
286
+ if (request._tag === 'RevalidateSemantic') {
287
+ if (
288
+ new Set(request.found.candidates.map((candidate) => candidate.key.id)).size >
289
+ limits.maxSources
290
+ ) {
291
+ return yield* MemoryRpcError.make({ reason: 'budget' })
292
+ }
293
+
294
+ const result = yield* revalidateSemanticMemoryCandidates(
295
+ request.found,
296
+ request.access,
297
+ request.profile,
298
+ {
299
+ ...request.limits,
300
+ maxSourceBytes: Math.min(
301
+ limits.maxSourceBytes,
302
+ request.limits.maxSourceBytes ?? 16_777_216,
303
+ ),
304
+ maxOutputBytes: Math.min(
305
+ limits.maxResponseBytes,
306
+ request.limits.maxOutputBytes ?? 16_777_216,
307
+ ),
308
+ },
309
+ )
310
+
311
+ return { _tag: 'Semantic', access: request.access, result }
312
+ }
313
+
314
+ const count = request.lookup._tag === 'Found'
315
+ ? new Set(request.lookup.passages.map((passage) => passage.source.id)).size
316
+ : 0
317
+
318
+ if (count > Math.min(limits.maxSources, request.limits.maxSources)) {
319
+ return yield* MemoryRpcError.make({ reason: 'budget' })
320
+ }
321
+
322
+ const lookup = yield* revalidateMemoryLookup(request.lookup, request.access, {
323
+ maxSourceBytes: limits.maxSourceBytes,
324
+ maxInputBytes: Math.min(limits.maxSourceBytes, request.limits.maxInputBytes ?? 16_777_216),
325
+ })
326
+
327
+ return { _tag: 'Lookup', access: request.access, lookup }
328
+ }).pipe(
329
+ Effect.scoped,
330
+ Effect.timeoutOrElse({
331
+ duration: remaining,
332
+ orElse: () => Effect.fail(MemoryRpcError.make({ reason: 'timeout' })),
333
+ }),
334
+ )
335
+ }).pipe(
336
+ Effect.flatMap((response) => encodeMemoryWire(MemoryOwnerResponse, response, limits.maxResponseBytes)),
337
+ Effect.result,
338
+ )
339
+
340
+ if (result._tag === 'Success') return result.success
341
+
342
+ return yield* encodeMemoryWire(
343
+ MemoryOwnerResponse,
344
+ {
345
+ _tag: 'Failed',
346
+ failure: result.failure,
347
+ },
348
+ limits.maxResponseBytes,
349
+ ).pipe(
350
+ Effect.catch(() =>
351
+ Schema.encodeEffect(Schema.fromJsonString(MemoryOwnerResponse))({
352
+ _tag: 'Failed',
353
+ failure: MemoryRpcError.make({ reason: 'budget' }),
354
+ }).pipe(Effect.orDie)
355
+ ),
356
+ )
357
+ })