@effect-agent/storage-cloudflare 0.1.0-beta.9 → 0.1.0-beta.91

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 (73) hide show
  1. package/dist/DoMemoryStore.d.mts +29 -0
  2. package/dist/DoMemoryStore.mjs +55 -0
  3. package/dist/DoMemoryStore.mjs.map +1 -0
  4. package/dist/DoMessageDeliveryStore.d.mts +17 -0
  5. package/dist/DoMessageDeliveryStore.mjs +32 -0
  6. package/dist/DoMessageDeliveryStore.mjs.map +1 -0
  7. package/dist/DoScheduleStore.d.mts +36 -0
  8. package/dist/DoScheduleStore.mjs +388 -0
  9. package/dist/DoScheduleStore.mjs.map +1 -0
  10. package/dist/DoStorageConfig.d.mts +45 -0
  11. package/dist/DoStorageConfig.mjs +52 -0
  12. package/dist/DoStorageConfig.mjs.map +1 -0
  13. package/dist/DoStorageError-Cmhvl4TQ.d.mts +98 -0
  14. package/dist/DoStorageError.d.mts +2 -0
  15. package/dist/DoStorageError.mjs +164 -0
  16. package/dist/DoStorageError.mjs.map +1 -0
  17. package/dist/DoStorageFailpoint.d.mts +17 -0
  18. package/dist/DoStorageFailpoint.mjs +14 -0
  19. package/dist/DoStorageFailpoint.mjs.map +1 -0
  20. package/dist/DoStorageFailpointTesting.d.mts +34 -0
  21. package/dist/DoStorageFailpointTesting.mjs +35 -0
  22. package/dist/DoStorageFailpointTesting.mjs.map +1 -0
  23. package/dist/DoStorageVersion-x5OOurvZ.d.mts +12 -0
  24. package/dist/DoStorageVersion.d.mts +2 -0
  25. package/dist/DoStorageVersion.mjs +8 -0
  26. package/dist/DoStorageVersion.mjs.map +1 -0
  27. package/dist/DoSubmissionLedger.d.mts +23 -0
  28. package/dist/DoSubmissionLedger.mjs +1849 -0
  29. package/dist/DoSubmissionLedger.mjs.map +1 -0
  30. package/dist/DoSubscriptionStore.d.mts +25 -0
  31. package/dist/DoSubscriptionStore.mjs +183 -0
  32. package/dist/DoSubscriptionStore.mjs.map +1 -0
  33. package/dist/DoThreadStore.d.mts +59 -0
  34. package/dist/DoThreadStore.mjs +465 -0
  35. package/dist/DoThreadStore.mjs.map +1 -0
  36. package/dist/MemoryProtocol.d.mts +38160 -0
  37. package/dist/MemoryProtocol.mjs +219 -0
  38. package/dist/MemoryProtocol.mjs.map +1 -0
  39. package/dist/PortProtocol.d.mts +3804 -0
  40. package/dist/PortProtocol.mjs +176 -0
  41. package/dist/PortProtocol.mjs.map +1 -0
  42. package/dist/PortRouting.d.mts +83 -0
  43. package/dist/PortRouting.mjs +410 -0
  44. package/dist/PortRouting.mjs.map +1 -0
  45. package/dist/do-journal-dJ01KSKf.mjs +991 -0
  46. package/dist/do-journal-dJ01KSKf.mjs.map +1 -0
  47. package/dist/index.d.mts +14 -1525
  48. package/dist/index.mjs +14 -3695
  49. package/dist/migrations-Ba_kOrSx.mjs +287 -0
  50. package/dist/migrations-Ba_kOrSx.mjs.map +1 -0
  51. package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
  52. package/package.json +1 -45
  53. package/src/DoMemoryStore.ts +56 -0
  54. package/src/DoMessageDeliveryStore.ts +53 -0
  55. package/src/DoScheduleStore.ts +622 -0
  56. package/src/{errors.ts → DoStorageError.ts} +10 -3
  57. package/src/DoStorageFailpoint.ts +20 -0
  58. package/src/{do-storage-failpoint.ts → DoStorageFailpointTesting.ts} +5 -18
  59. package/src/DoStorageVersion.ts +2 -0
  60. package/src/{do-ledger.ts → DoSubmissionLedger.ts} +924 -225
  61. package/src/DoSubscriptionStore.ts +329 -0
  62. package/src/DoThreadStore.ts +1012 -0
  63. package/src/MemoryProtocol.ts +351 -0
  64. package/src/{port-protocol.ts → PortProtocol.ts} +45 -38
  65. package/src/{routing.ts → PortRouting.ts} +292 -264
  66. package/src/index.ts +13 -32
  67. package/src/{do-journal.ts → internal/do-journal.ts} +718 -234
  68. package/src/internal/message-delivery-schema.ts +24 -0
  69. package/src/{migrations.ts → internal/migrations.ts} +51 -35
  70. package/src/internal/recovery-checkpoint-schema.ts +17 -0
  71. package/dist/index.mjs.map +0 -1
  72. package/src/do-conversation-store.ts +0 -850
  73. /package/src/{do-storage-config.ts → DoStorageConfig.ts} +0 -0
@@ -0,0 +1,219 @@
1
+ import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
+ import { Clock, Context, Effect, Schema } from "effect";
3
+ import { MemoryConflict, MemoryDocument, MemoryKey, MemoryMutationFailure, MemoryOperationConflict, MemoryReader, MemoryStorageError, MemoryWithdrawn, MemoryWrite, MemoryWriter } from "effect-agent/memory-store";
4
+ import { Principal } from "effect-agent/submission-ledger";
5
+ import * as MemoryNamespace from "effect-agent/memory-namespace";
6
+ import { MemoryLookup, MemoryRecallError, MemoryRecallLimits } from "effect-agent/memory-reference";
7
+ import { MemoryAccess, revalidateMemoryLookup } from "effect-agent/memory-revalidation";
8
+ import { MemoryIndexError, MemoryIndexSearch, SemanticMemoryProfile } from "effect-agent/semantic-memory-index";
9
+ import { SemanticCandidateLimits, SemanticCandidateResult, SemanticMemoryError, revalidateSemanticMemoryCandidates } from "effect-agent/semantic-memory-revalidation";
10
+ //#region src/MemoryProtocol.ts
11
+ var MemoryProtocol_exports = /* @__PURE__ */ __exportAll({
12
+ MemoryOwnerAuthorizer: () => MemoryOwnerAuthorizer,
13
+ MemoryOwnerFailure: () => MemoryOwnerFailure,
14
+ MemoryOwnerIdentity: () => MemoryOwnerIdentity,
15
+ MemoryOwnerRequest: () => MemoryOwnerRequest,
16
+ MemoryOwnerResponse: () => MemoryOwnerResponse,
17
+ MemoryRpcError: () => MemoryRpcError,
18
+ MemoryRpcLimits: () => MemoryRpcLimits,
19
+ decodeMemoryWire: () => decodeMemoryWire,
20
+ defaultMemoryRpcLimits: () => defaultMemoryRpcLimits,
21
+ encodeMemoryWire: () => encodeMemoryWire,
22
+ handleMemoryOwnerRequest: () => handleMemoryOwnerRequest,
23
+ memoryWireBytes: () => memoryWireBytes
24
+ });
25
+ var MemoryRpcError = class extends Schema.TaggedError()("MemoryRpcError", { reason: Schema.Literals([
26
+ "denied",
27
+ "protocol",
28
+ "budget",
29
+ "timeout",
30
+ "unavailable"
31
+ ]) }) {};
32
+ var MemoryRpcLimits = class extends Schema.Class("@effect-agent/storage-cloudflare/MemoryRpcLimits")({
33
+ maxRequestBytes: Schema.Int.check(Schema.isBetween({
34
+ minimum: 256,
35
+ maximum: 4194304
36
+ })),
37
+ maxResponseBytes: Schema.Int.check(Schema.isBetween({
38
+ minimum: 256,
39
+ maximum: 16777216
40
+ })),
41
+ maxSourceBytes: Schema.Int.check(Schema.isBetween({
42
+ minimum: 1,
43
+ maximum: 67108864
44
+ })),
45
+ maxSources: MemoryRecallLimits.fields.maxSources,
46
+ timeoutMillis: MemoryRecallLimits.fields.timeoutMillis
47
+ }) {};
48
+ const defaultMemoryRpcLimits = MemoryRpcLimits.make({
49
+ maxRequestBytes: 1048576,
50
+ maxResponseBytes: 4194304,
51
+ maxSourceBytes: 16777216,
52
+ maxSources: 16,
53
+ timeoutMillis: 1e4
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
+ const RevalidateRequest = Schema.TaggedStruct("Revalidate", {
63
+ ...RequestFields,
64
+ lookup: MemoryLookup,
65
+ limits: MemoryRecallLimits
66
+ });
67
+ const ChangeRequest = Schema.TaggedStruct("Change", {
68
+ ...RequestFields,
69
+ write: MemoryWrite.Wire
70
+ });
71
+ const GetRequest = Schema.TaggedStruct("Get", {
72
+ ...RequestFields,
73
+ key: MemoryKey.Wire
74
+ });
75
+ const SemanticRequest = Schema.TaggedStruct("RevalidateSemantic", {
76
+ ...RequestFields,
77
+ found: MemoryIndexSearch.Wire,
78
+ profile: SemanticMemoryProfile,
79
+ limits: SemanticCandidateLimits
80
+ });
81
+ const MemoryOwnerRequest = Schema.Union([
82
+ RevalidateRequest,
83
+ ChangeRequest,
84
+ SemanticRequest,
85
+ GetRequest
86
+ ]);
87
+ const MemoryOwnerFailure = Schema.Union([
88
+ MemoryRpcError,
89
+ MemoryStorageError,
90
+ MemoryRecallError,
91
+ MemoryConflict,
92
+ MemoryWithdrawn,
93
+ MemoryOperationConflict,
94
+ MemoryMutationFailure,
95
+ MemoryIndexError,
96
+ SemanticMemoryError
97
+ ]);
98
+ const LookupResponse = Schema.TaggedStruct("Lookup", {
99
+ access: MemoryAccess.Wire,
100
+ lookup: MemoryLookup
101
+ });
102
+ const ChangedResponse = Schema.TaggedStruct("Changed", {
103
+ access: MemoryAccess.Wire,
104
+ document: MemoryDocument.Wire
105
+ });
106
+ const SemanticResponse = Schema.TaggedStruct("Semantic", {
107
+ access: MemoryAccess.Wire,
108
+ result: SemanticCandidateResult
109
+ });
110
+ const DocumentResponse = Schema.TaggedStruct("Document", {
111
+ access: MemoryAccess.Wire,
112
+ key: MemoryKey.Wire,
113
+ document: Schema.NullOr(MemoryDocument.Wire)
114
+ });
115
+ const FailedResponse = Schema.TaggedStruct("Failed", { failure: MemoryOwnerFailure });
116
+ const MemoryOwnerResponse = Schema.Union([
117
+ LookupResponse,
118
+ ChangedResponse,
119
+ SemanticResponse,
120
+ DocumentResponse,
121
+ FailedResponse
122
+ ]);
123
+ /**
124
+ * Fail-closed application policy. Authorize the namespace, principal, scope, and full command.
125
+ * Get requires exact-key authority, including application source/provenance checks where needed.
126
+ * Possession of a key or scope is not authorization, including for absent or withdrawn documents.
127
+ */
128
+ var MemoryOwnerAuthorizer = class extends Context.Service()("@effect-agent/storage-cloudflare/MemoryOwnerAuthorizer") {};
129
+ /** Canonical namespace derived from this object's idFromName identity, never its request. */
130
+ var MemoryOwnerIdentity = class extends Context.Service()("@effect-agent/storage-cloudflare/MemoryOwnerIdentity") {};
131
+ const memoryWireBytes = (text) => new TextEncoder().encode(text).byteLength;
132
+ const decodeMemoryWire = Effect.fn("decodeMemoryWire")(function* (schema, raw, maxBytes) {
133
+ const text = yield* Schema.decodeUnknownEffect(Schema.String)(raw).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
134
+ if (text.length > maxBytes || memoryWireBytes(text) > maxBytes) return yield* MemoryRpcError.make({ reason: "budget" });
135
+ return yield* Schema.decodeEffect(Schema.fromJsonString(schema))(text).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
136
+ });
137
+ const encodeMemoryWire = Effect.fn("encodeMemoryWire")(function* (schema, value, maxBytes) {
138
+ const encoded = yield* Schema.encodeEffect(Schema.fromJsonString(schema))(value).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
139
+ if (encoded.length > maxBytes || memoryWireBytes(encoded) > maxBytes) return yield* MemoryRpcError.make({ reason: "budget" });
140
+ return encoded;
141
+ });
142
+ /** One local read for Get, or per distinct candidate source. No discovery or background work. */
143
+ const handleMemoryOwnerRequest = Effect.fn("MemoryOwner.handleRequest")(function* (raw, limits = defaultMemoryRpcLimits) {
144
+ const result = yield* Effect.gen(function* () {
145
+ limits = yield* Schema.decodeEffect(MemoryRpcLimits)(limits).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
146
+ const request = yield* decodeMemoryWire(MemoryOwnerRequest, raw, limits.maxRequestBytes);
147
+ const { namespace } = yield* MemoryOwnerIdentity;
148
+ if (!MemoryNamespace.equals(namespace, request.access.namespace) || request._tag === "Get" && !MemoryNamespace.equals(namespace, request.key.namespace) || request._tag === "Change" && !MemoryNamespace.equals(namespace, request.write.key.namespace) || request._tag === "RevalidateSemantic" && request.found.candidates.some((candidate) => !MemoryNamespace.equals(namespace, candidate.key.namespace))) return yield* MemoryRpcError.make({ reason: "denied" });
149
+ const remaining = Math.min(limits.timeoutMillis, request.deadlineMillis - (yield* Clock.currentTimeMillis));
150
+ if (remaining <= 0) return yield* MemoryRpcError.make({ reason: "timeout" });
151
+ return yield* Effect.gen(function* () {
152
+ yield* (yield* MemoryOwnerAuthorizer).authorize(request);
153
+ if (request._tag === "Get") {
154
+ const current = yield* (yield* MemoryReader).get(request.key);
155
+ const document = current === null ? null : yield* MemoryDocument.restore(namespace, current);
156
+ if (document !== null) {
157
+ if (document.key.id !== request.key.id || document.source.id !== request.key.id) return yield* MemoryStorageError.make({
158
+ operation: "validate memory read identity",
159
+ reason: "corrupt"
160
+ });
161
+ if (document._tag === "ActiveMemoryDocument" && !document.scopes.includes(request.access.scope)) return yield* MemoryRpcError.make({ reason: "denied" });
162
+ yield* encodeMemoryWire(MemoryDocument.Wire, document, limits.maxSourceBytes);
163
+ }
164
+ return {
165
+ _tag: "Document",
166
+ access: request.access,
167
+ key: request.key,
168
+ document
169
+ };
170
+ }
171
+ if (request._tag === "Change") {
172
+ const writer = yield* MemoryWriter;
173
+ return {
174
+ _tag: "Changed",
175
+ access: request.access,
176
+ document: yield* writer.change(request.write)
177
+ };
178
+ }
179
+ if (request._tag === "RevalidateSemantic") {
180
+ if (new Set(request.found.candidates.map((candidate) => candidate.key.id)).size > limits.maxSources) return yield* MemoryRpcError.make({ reason: "budget" });
181
+ const result = yield* revalidateSemanticMemoryCandidates(request.found, request.access, request.profile, {
182
+ ...request.limits,
183
+ maxSourceBytes: Math.min(limits.maxSourceBytes, request.limits.maxSourceBytes ?? 16777216),
184
+ maxOutputBytes: Math.min(limits.maxResponseBytes, request.limits.maxOutputBytes ?? 16777216)
185
+ });
186
+ return {
187
+ _tag: "Semantic",
188
+ access: request.access,
189
+ result
190
+ };
191
+ }
192
+ if ((request.lookup._tag === "Found" ? new Set(request.lookup.passages.map((passage) => passage.source.id)).size : 0) > Math.min(limits.maxSources, request.limits.maxSources)) return yield* MemoryRpcError.make({ reason: "budget" });
193
+ const lookup = yield* revalidateMemoryLookup(request.lookup, request.access, {
194
+ maxSourceBytes: limits.maxSourceBytes,
195
+ maxInputBytes: Math.min(limits.maxSourceBytes, request.limits.maxInputBytes ?? 16777216)
196
+ });
197
+ return {
198
+ _tag: "Lookup",
199
+ access: request.access,
200
+ lookup
201
+ };
202
+ }).pipe(Effect.scoped, Effect.timeoutOrElse({
203
+ duration: remaining,
204
+ orElse: () => Effect.fail(MemoryRpcError.make({ reason: "timeout" }))
205
+ }));
206
+ }).pipe(Effect.flatMap((response) => encodeMemoryWire(MemoryOwnerResponse, response, limits.maxResponseBytes)), Effect.result);
207
+ if (result._tag === "Success") return result.success;
208
+ return yield* encodeMemoryWire(MemoryOwnerResponse, {
209
+ _tag: "Failed",
210
+ failure: result.failure
211
+ }, limits.maxResponseBytes).pipe(Effect.catch(() => Schema.encodeEffect(Schema.fromJsonString(MemoryOwnerResponse))({
212
+ _tag: "Failed",
213
+ failure: MemoryRpcError.make({ reason: "budget" })
214
+ }).pipe(Effect.orDie)));
215
+ });
216
+ //#endregion
217
+ export { MemoryOwnerAuthorizer, MemoryOwnerFailure, MemoryOwnerIdentity, MemoryOwnerRequest, MemoryOwnerResponse, MemoryRpcError, MemoryRpcLimits, decodeMemoryWire, defaultMemoryRpcLimits, encodeMemoryWire, handleMemoryOwnerRequest, memoryWireBytes, MemoryProtocol_exports as t };
218
+
219
+ //# sourceMappingURL=MemoryProtocol.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MemoryProtocol.mjs","names":[],"sources":["../src/MemoryProtocol.ts"],"sourcesContent":["import { Clock, Context, Effect, Schema } from \"effect\";\nimport * as MemoryNamespace from \"effect-agent/memory-namespace\";\nimport { MemoryLookup, MemoryRecallError, MemoryRecallLimits } from \"effect-agent/memory-reference\";\nimport { MemoryAccess, revalidateMemoryLookup } from \"effect-agent/memory-revalidation\";\nimport {\n MemoryKey,\n MemoryReader,\n MemoryConflict,\n MemoryDocument,\n MemoryMutationFailure,\n MemoryOperationConflict,\n MemoryStorageError,\n MemoryWithdrawn,\n MemoryWrite,\n MemoryWriter,\n} from \"effect-agent/memory-store\";\nimport {\n MemoryIndexSearch,\n MemoryIndexError,\n SemanticMemoryProfile,\n} from \"effect-agent/semantic-memory-index\";\nimport {\n SemanticMemoryError,\n SemanticCandidateLimits,\n SemanticCandidateResult,\n revalidateSemanticMemoryCandidates,\n} from \"effect-agent/semantic-memory-revalidation\";\nimport { Principal } from \"effect-agent/submission-ledger\";\n\nexport class MemoryRpcError extends Schema.TaggedError<MemoryRpcError>()(\"MemoryRpcError\", {\n reason: Schema.Literals([\"denied\", \"protocol\", \"budget\", \"timeout\", \"unavailable\"]),\n}) {}\n\nexport class MemoryRpcLimits extends Schema.Class<MemoryRpcLimits>(\n \"@effect-agent/storage-cloudflare/MemoryRpcLimits\",\n)({\n maxRequestBytes: Schema.Int.check(Schema.isBetween({ minimum: 256, maximum: 4_194_304 })),\n maxResponseBytes: Schema.Int.check(Schema.isBetween({ minimum: 256, maximum: 16_777_216 })),\n maxSourceBytes: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 67_108_864 })),\n maxSources: MemoryRecallLimits.fields.maxSources,\n timeoutMillis: MemoryRecallLimits.fields.timeoutMillis,\n}) {}\n\nexport const defaultMemoryRpcLimits = MemoryRpcLimits.make({\n maxRequestBytes: 1_048_576,\n maxResponseBytes: 4_194_304,\n maxSourceBytes: 16_777_216,\n maxSources: 16,\n timeoutMillis: 10_000,\n});\n\nconst RequestFields = {\n version: Schema.Literal(1),\n access: MemoryAccess.Wire,\n /** Host-authenticated identity, never copied from model input. The owner still authorizes it. */\n principal: Principal,\n deadlineMillis: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),\n};\n\nconst RevalidateRequest = Schema.TaggedStruct(\"Revalidate\", {\n ...RequestFields,\n lookup: MemoryLookup,\n limits: MemoryRecallLimits,\n});\n\nconst ChangeRequest = Schema.TaggedStruct(\"Change\", { ...RequestFields, write: MemoryWrite.Wire });\n\nconst GetRequest = Schema.TaggedStruct(\"Get\", { ...RequestFields, key: MemoryKey.Wire });\n\nconst SemanticRequest: Schema.TaggedStruct<\n \"RevalidateSemantic\",\n typeof RequestFields & {\n readonly found: typeof MemoryIndexSearch.Wire;\n readonly profile: typeof SemanticMemoryProfile;\n readonly limits: typeof SemanticCandidateLimits;\n }\n> = Schema.TaggedStruct(\"RevalidateSemantic\", {\n ...RequestFields,\n found: MemoryIndexSearch.Wire,\n profile: SemanticMemoryProfile,\n limits: SemanticCandidateLimits,\n});\n\nexport const MemoryOwnerRequest: Schema.Union<\n [typeof RevalidateRequest, typeof ChangeRequest, typeof SemanticRequest, typeof GetRequest]\n> = Schema.Union([RevalidateRequest, ChangeRequest, SemanticRequest, GetRequest]);\n\nexport type MemoryOwnerRequest = typeof MemoryOwnerRequest.Type;\n\nexport const MemoryOwnerFailure = Schema.Union([\n MemoryRpcError,\n MemoryStorageError,\n MemoryRecallError,\n MemoryConflict,\n MemoryWithdrawn,\n MemoryOperationConflict,\n MemoryMutationFailure,\n MemoryIndexError,\n SemanticMemoryError,\n]);\n\nexport type MemoryOwnerFailure = typeof MemoryOwnerFailure.Type;\n\nconst LookupResponse = Schema.TaggedStruct(\"Lookup\", {\n access: MemoryAccess.Wire,\n lookup: MemoryLookup,\n});\n\nconst ChangedResponse = Schema.TaggedStruct(\"Changed\", {\n access: MemoryAccess.Wire,\n document: MemoryDocument.Wire,\n});\n\nconst SemanticResponse = Schema.TaggedStruct(\"Semantic\", {\n access: MemoryAccess.Wire,\n result: SemanticCandidateResult,\n});\n\nconst DocumentResponse = Schema.TaggedStruct(\"Document\", {\n access: MemoryAccess.Wire,\n key: MemoryKey.Wire,\n document: Schema.NullOr(MemoryDocument.Wire),\n});\n\nconst FailedResponse = Schema.TaggedStruct(\"Failed\", { failure: MemoryOwnerFailure });\n\nexport const MemoryOwnerResponse: Schema.Union<\n [\n typeof LookupResponse,\n typeof ChangedResponse,\n typeof SemanticResponse,\n typeof DocumentResponse,\n typeof FailedResponse,\n ]\n> = Schema.Union([\n LookupResponse,\n ChangedResponse,\n SemanticResponse,\n DocumentResponse,\n FailedResponse,\n]);\n\nexport type MemoryOwnerResponse = typeof MemoryOwnerResponse.Type;\n\n/**\n * Fail-closed application policy. Authorize the namespace, principal, scope, and full command.\n * Get requires exact-key authority, including application source/provenance checks where needed.\n * Possession of a key or scope is not authorization, including for absent or withdrawn documents.\n */\nexport class MemoryOwnerAuthorizer extends Context.Service<\n MemoryOwnerAuthorizer,\n {\n readonly authorize: (request: MemoryOwnerRequest) => Effect.Effect<void, MemoryRpcError>;\n }\n>()(\"@effect-agent/storage-cloudflare/MemoryOwnerAuthorizer\") {}\n\n/** Canonical namespace derived from this object's idFromName identity, never its request. */\nexport class MemoryOwnerIdentity extends Context.Service<\n MemoryOwnerIdentity,\n {\n readonly namespace: MemoryNamespace.Any;\n }\n>()(\"@effect-agent/storage-cloudflare/MemoryOwnerIdentity\") {}\n\nexport const memoryWireBytes = (text: string): number => new TextEncoder().encode(text).byteLength;\n\nexport const decodeMemoryWire = Effect.fn(\"decodeMemoryWire\")(function* <A, I>(\n schema: Schema.Codec<A, I, never>,\n raw: unknown,\n maxBytes: number,\n) {\n const text = yield* Schema.decodeUnknownEffect(Schema.String)(raw).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: \"protocol\" })),\n );\n\n if (text.length > maxBytes || memoryWireBytes(text) > maxBytes)\n return yield* MemoryRpcError.make({ reason: \"budget\" });\n\n return yield* Schema.decodeEffect(Schema.fromJsonString(schema))(text).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: \"protocol\" })),\n );\n});\n\nexport const encodeMemoryWire = Effect.fn(\"encodeMemoryWire\")(function* <A, I>(\n schema: Schema.Codec<A, I, never, never>,\n value: A,\n maxBytes: number,\n) {\n const encoded = yield* Schema.encodeEffect(Schema.fromJsonString(schema))(value).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: \"protocol\" })),\n );\n\n if (encoded.length > maxBytes || memoryWireBytes(encoded) > maxBytes)\n return yield* MemoryRpcError.make({ reason: \"budget\" });\n\n return encoded;\n});\n\n/** One local read for Get, or per distinct candidate source. No discovery or background work. */\nexport const handleMemoryOwnerRequest = Effect.fn(\"MemoryOwner.handleRequest\")(function* (\n raw: unknown,\n limits: MemoryRpcLimits = defaultMemoryRpcLimits,\n): Effect.fn.Return<\n string,\n never,\n MemoryOwnerIdentity | MemoryOwnerAuthorizer | MemoryReader | MemoryWriter\n> {\n const result = yield* Effect.gen(function* (): Effect.fn.Return<\n MemoryOwnerResponse,\n MemoryOwnerFailure,\n MemoryOwnerIdentity | MemoryOwnerAuthorizer | MemoryReader | MemoryWriter\n > {\n limits = yield* Schema.decodeEffect(MemoryRpcLimits)(limits).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: \"protocol\" })),\n );\n const request = yield* decodeMemoryWire(MemoryOwnerRequest, raw, limits.maxRequestBytes);\n const { namespace } = yield* MemoryOwnerIdentity;\n\n if (\n !MemoryNamespace.equals(namespace, request.access.namespace) ||\n (request._tag === \"Get\" && !MemoryNamespace.equals(namespace, request.key.namespace)) ||\n (request._tag === \"Change\" &&\n !MemoryNamespace.equals(namespace, request.write.key.namespace)) ||\n (request._tag === \"RevalidateSemantic\" &&\n request.found.candidates.some(\n (candidate) => !MemoryNamespace.equals(namespace, candidate.key.namespace),\n ))\n )\n return yield* MemoryRpcError.make({ reason: \"denied\" });\n\n const remaining = Math.min(\n limits.timeoutMillis,\n request.deadlineMillis - (yield* Clock.currentTimeMillis),\n );\n\n if (remaining <= 0) return yield* MemoryRpcError.make({ reason: \"timeout\" });\n\n return yield* Effect.gen(function* (): Effect.fn.Return<\n MemoryOwnerResponse,\n MemoryOwnerFailure,\n MemoryOwnerAuthorizer | MemoryReader | MemoryWriter\n > {\n const authorizer = yield* MemoryOwnerAuthorizer;\n\n yield* authorizer.authorize(request);\n if (request._tag === \"Get\") {\n const reader = yield* MemoryReader;\n const current = yield* reader.get(request.key);\n\n const document =\n current === null ? null : yield* MemoryDocument.restore(namespace, current);\n\n if (document !== null) {\n if (document.key.id !== request.key.id || document.source.id !== request.key.id)\n return yield* MemoryStorageError.make({\n operation: \"validate memory read identity\",\n reason: \"corrupt\",\n });\n if (\n document._tag === \"ActiveMemoryDocument\" &&\n !document.scopes.includes(request.access.scope)\n )\n return yield* MemoryRpcError.make({ reason: \"denied\" });\n\n yield* encodeMemoryWire(MemoryDocument.Wire, document, limits.maxSourceBytes);\n }\n\n return { _tag: \"Document\", access: request.access, key: request.key, document };\n }\n if (request._tag === \"Change\") {\n const writer = yield* MemoryWriter;\n\n return {\n _tag: \"Changed\",\n access: request.access,\n document: yield* writer.change(request.write),\n };\n }\n if (request._tag === \"RevalidateSemantic\") {\n if (\n new Set(request.found.candidates.map((candidate) => candidate.key.id)).size >\n limits.maxSources\n )\n return yield* MemoryRpcError.make({ reason: \"budget\" });\n\n const result = yield* revalidateSemanticMemoryCandidates(\n request.found,\n request.access,\n request.profile,\n {\n ...request.limits,\n maxSourceBytes: Math.min(\n limits.maxSourceBytes,\n request.limits.maxSourceBytes ?? 16_777_216,\n ),\n maxOutputBytes: Math.min(\n limits.maxResponseBytes,\n request.limits.maxOutputBytes ?? 16_777_216,\n ),\n },\n );\n\n return { _tag: \"Semantic\", access: request.access, result };\n }\n\n const count =\n request.lookup._tag === \"Found\"\n ? new Set(request.lookup.passages.map((passage) => passage.source.id)).size\n : 0;\n\n if (count > Math.min(limits.maxSources, request.limits.maxSources))\n return yield* MemoryRpcError.make({ reason: \"budget\" });\n\n const lookup = yield* revalidateMemoryLookup(request.lookup, request.access, {\n maxSourceBytes: limits.maxSourceBytes,\n maxInputBytes: Math.min(limits.maxSourceBytes, request.limits.maxInputBytes ?? 16_777_216),\n });\n\n return { _tag: \"Lookup\", access: request.access, lookup };\n }).pipe(\n Effect.scoped,\n Effect.timeoutOrElse({\n duration: remaining,\n orElse: () => Effect.fail(MemoryRpcError.make({ reason: \"timeout\" })),\n }),\n );\n }).pipe(\n Effect.flatMap((response) =>\n encodeMemoryWire(MemoryOwnerResponse, response, limits.maxResponseBytes),\n ),\n Effect.result,\n );\n\n if (result._tag === \"Success\") return result.success;\n\n return yield* encodeMemoryWire(\n MemoryOwnerResponse,\n {\n _tag: \"Failed\",\n failure: result.failure,\n },\n limits.maxResponseBytes,\n ).pipe(\n Effect.catch(() =>\n Schema.encodeEffect(Schema.fromJsonString(MemoryOwnerResponse))({\n _tag: \"Failed\",\n failure: MemoryRpcError.make({ reason: \"budget\" }),\n }).pipe(Effect.orDie),\n ),\n );\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA6BA,IAAa,iBAAb,cAAoC,OAAO,YAA4B,CAAC,CAAC,kBAAkB,EACzF,QAAQ,OAAO,SAAS;CAAC;CAAU;CAAY;CAAU;CAAW;AAAa,CAAC,EACpF,CAAC,CAAC,CAAC,CAAC;AAEJ,IAAa,kBAAb,cAAqC,OAAO,MAC1C,kDACF,CAAC,CAAC;CACA,iBAAiB,OAAO,IAAI,MAAM,OAAO,UAAU;EAAE,SAAS;EAAK,SAAS;CAAU,CAAC,CAAC;CACxF,kBAAkB,OAAO,IAAI,MAAM,OAAO,UAAU;EAAE,SAAS;EAAK,SAAS;CAAW,CAAC,CAAC;CAC1F,gBAAgB,OAAO,IAAI,MAAM,OAAO,UAAU;EAAE,SAAS;EAAG,SAAS;CAAW,CAAC,CAAC;CACtF,YAAY,mBAAmB,OAAO;CACtC,eAAe,mBAAmB,OAAO;AAC3C,CAAC,CAAC,CAAC,CAAC;AAEJ,MAAa,yBAAyB,gBAAgB,KAAK;CACzD,iBAAiB;CACjB,kBAAkB;CAClB,gBAAgB;CAChB,YAAY;CACZ,eAAe;AACjB,CAAC;AAED,MAAM,gBAAgB;CACpB,SAAS,OAAO,QAAQ,CAAC;CACzB,QAAQ,aAAa;;CAErB,WAAW;CACX,gBAAgB,OAAO,OAAO,MAAM,OAAO,uBAAuB,CAAC,CAAC;AACtE;AAEA,MAAM,oBAAoB,OAAO,aAAa,cAAc;CAC1D,GAAG;CACH,QAAQ;CACR,QAAQ;AACV,CAAC;AAED,MAAM,gBAAgB,OAAO,aAAa,UAAU;CAAE,GAAG;CAAe,OAAO,YAAY;AAAK,CAAC;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