@effect-agent/storage-cloudflare 0.1.0-beta.6 → 0.1.0-beta.61

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 (72) 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-UrxrvbnF.d.mts +98 -0
  14. package/dist/DoStorageError.d.mts +2 -0
  15. package/dist/DoStorageError.mjs +162 -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-Pt3jEHdW.d.mts +10 -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 +1826 -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 +432 -0
  35. package/dist/DoThreadStore.mjs.map +1 -0
  36. package/dist/MemoryProtocol.d.mts +19796 -0
  37. package/dist/MemoryProtocol.mjs +182 -0
  38. package/dist/MemoryProtocol.mjs.map +1 -0
  39. package/dist/PortProtocol.d.mts +3099 -0
  40. package/dist/PortProtocol.mjs +176 -0
  41. package/dist/PortProtocol.mjs.map +1 -0
  42. package/dist/PortRouting.d.mts +82 -0
  43. package/dist/PortRouting.mjs +405 -0
  44. package/dist/PortRouting.mjs.map +1 -0
  45. package/dist/do-journal-Brv7xSH5.mjs +869 -0
  46. package/dist/do-journal-Brv7xSH5.mjs.map +1 -0
  47. package/dist/index.d.mts +14 -1505
  48. package/dist/index.mjs +14 -3695
  49. package/dist/migrations-soC86CQA.mjs +267 -0
  50. package/dist/migrations-soC86CQA.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 +59 -0
  54. package/src/DoMessageDeliveryStore.ts +53 -0
  55. package/src/DoScheduleStore.ts +622 -0
  56. package/src/{errors.ts → DoStorageError.ts} +8 -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} +757 -123
  61. package/src/DoSubscriptionStore.ts +329 -0
  62. package/src/DoThreadStore.ts +924 -0
  63. package/src/MemoryProtocol.ts +291 -0
  64. package/src/{port-protocol.ts → PortProtocol.ts} +41 -34
  65. package/src/{routing.ts → PortRouting.ts} +266 -259
  66. package/src/index.ts +13 -32
  67. package/src/{do-journal.ts → internal/do-journal.ts} +495 -214
  68. package/src/internal/message-delivery-schema.ts +24 -0
  69. package/src/{migrations.ts → internal/migrations.ts} +40 -35
  70. package/dist/index.mjs.map +0 -1
  71. package/src/do-conversation-store.ts +0 -850
  72. /package/src/{do-storage-config.ts → DoStorageConfig.ts} +0 -0
@@ -0,0 +1,182 @@
1
+ import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
+ import { Clock, Context, Effect, Schema } from "effect";
3
+ import { MemoryConflict, MemoryDocument, MemoryMutationFailure, MemoryOperationConflict, MemoryStorageError, MemoryWithdrawn, MemoryWrite, MemoryWriter } from "@effect-agent/core/MemoryStore";
4
+ import { Principal } from "@effect-agent/thread/SubmissionLedger";
5
+ import * as MemoryNamespace from "@effect-agent/core/MemoryNamespace";
6
+ import { MemoryLookup, MemoryRecallError, MemoryRecallLimits } from "@effect-agent/core/MemoryReference";
7
+ import { MemoryAccess, revalidateMemoryLookup } from "@effect-agent/core/MemoryRevalidation";
8
+ import { MemoryIndexError, MemoryIndexSearch, SemanticMemoryProfile } from "@effect-agent/core/SemanticMemoryIndex";
9
+ import { SemanticCandidateLimits, SemanticCandidateResult, SemanticMemoryError, revalidateSemanticMemoryCandidates } from "@effect-agent/core/SemanticMemoryRevalidation";
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 SemanticRequest = Schema.TaggedStruct("RevalidateSemantic", {
72
+ ...RequestFields,
73
+ found: MemoryIndexSearch.Wire,
74
+ profile: SemanticMemoryProfile,
75
+ limits: SemanticCandidateLimits
76
+ });
77
+ const MemoryOwnerRequest = Schema.Union([
78
+ RevalidateRequest,
79
+ ChangeRequest,
80
+ SemanticRequest
81
+ ]);
82
+ const MemoryOwnerFailure = Schema.Union([
83
+ MemoryRpcError,
84
+ MemoryStorageError,
85
+ MemoryRecallError,
86
+ MemoryConflict,
87
+ MemoryWithdrawn,
88
+ MemoryOperationConflict,
89
+ MemoryMutationFailure,
90
+ MemoryIndexError,
91
+ SemanticMemoryError
92
+ ]);
93
+ const MemoryOwnerResponse = Schema.Union([
94
+ Schema.TaggedStruct("Lookup", {
95
+ access: MemoryAccess.Wire,
96
+ lookup: MemoryLookup
97
+ }),
98
+ Schema.TaggedStruct("Changed", {
99
+ access: MemoryAccess.Wire,
100
+ document: MemoryDocument.Wire
101
+ }),
102
+ Schema.TaggedStruct("Semantic", {
103
+ access: MemoryAccess.Wire,
104
+ result: SemanticCandidateResult
105
+ }),
106
+ Schema.TaggedStruct("Failed", { failure: MemoryOwnerFailure })
107
+ ]);
108
+ /** Fail-closed application policy. Authorize the namespace, principal, scope, and full command. */
109
+ var MemoryOwnerAuthorizer = class extends Context.Service()("@effect-agent/storage-cloudflare/MemoryOwnerAuthorizer") {};
110
+ /** Canonical namespace derived from this object's idFromName identity, never its request. */
111
+ var MemoryOwnerIdentity = class extends Context.Service()("@effect-agent/storage-cloudflare/MemoryOwnerIdentity") {};
112
+ const memoryWireBytes = (text) => new TextEncoder().encode(text).byteLength;
113
+ const decodeMemoryWire = Effect.fn("decodeMemoryWire")(function* (schema, raw, maxBytes) {
114
+ const text = yield* Schema.decodeUnknownEffect(Schema.String)(raw).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
115
+ if (text.length > maxBytes || memoryWireBytes(text) > maxBytes) return yield* MemoryRpcError.make({ reason: "budget" });
116
+ return yield* Schema.decodeEffect(Schema.fromJsonString(schema))(text).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
117
+ });
118
+ const encodeMemoryWire = Effect.fn("encodeMemoryWire")(function* (schema, value, maxBytes) {
119
+ const encoded = yield* Schema.encodeEffect(Schema.fromJsonString(schema))(value).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
120
+ if (encoded.length > maxBytes || memoryWireBytes(encoded) > maxBytes) return yield* MemoryRpcError.make({ reason: "budget" });
121
+ return encoded;
122
+ });
123
+ /** One local read per distinct candidate source, with no per-document network calls. */
124
+ const handleMemoryOwnerRequest = Effect.fn("MemoryOwner.handleRequest")(function* (raw, limits = defaultMemoryRpcLimits) {
125
+ const result = yield* Effect.gen(function* () {
126
+ limits = yield* Schema.decodeUnknownEffect(MemoryRpcLimits)(limits).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
127
+ const request = yield* decodeMemoryWire(MemoryOwnerRequest, raw, limits.maxRequestBytes);
128
+ const { namespace } = yield* MemoryOwnerIdentity;
129
+ if (!MemoryNamespace.equals(namespace, request.access.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" });
130
+ const remaining = Math.min(limits.timeoutMillis, request.deadlineMillis - (yield* Clock.currentTimeMillis));
131
+ if (remaining <= 0) return yield* MemoryRpcError.make({ reason: "timeout" });
132
+ return yield* Effect.gen(function* () {
133
+ yield* (yield* MemoryOwnerAuthorizer).authorize(request);
134
+ if (request._tag === "Change") {
135
+ const writer = yield* MemoryWriter;
136
+ return {
137
+ _tag: "Changed",
138
+ access: request.access,
139
+ document: yield* writer.change(request.write)
140
+ };
141
+ }
142
+ if (request._tag === "RevalidateSemantic") {
143
+ if (new Set(request.found.candidates.map((candidate) => candidate.key.id)).size > limits.maxSources) return yield* MemoryRpcError.make({ reason: "budget" });
144
+ const result = yield* revalidateSemanticMemoryCandidates(request.found, request.access, request.profile, {
145
+ ...request.limits,
146
+ maxSourceBytes: Math.min(limits.maxSourceBytes, request.limits.maxSourceBytes ?? 16777216),
147
+ maxOutputBytes: Math.min(limits.maxResponseBytes, request.limits.maxOutputBytes ?? 16777216)
148
+ });
149
+ return {
150
+ _tag: "Semantic",
151
+ access: request.access,
152
+ result
153
+ };
154
+ }
155
+ 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" });
156
+ const lookup = yield* revalidateMemoryLookup(request.lookup, request.access, {
157
+ maxSourceBytes: limits.maxSourceBytes,
158
+ maxInputBytes: Math.min(limits.maxSourceBytes, request.limits.maxInputBytes ?? 16777216)
159
+ });
160
+ return {
161
+ _tag: "Lookup",
162
+ access: request.access,
163
+ lookup
164
+ };
165
+ }).pipe(Effect.scoped, Effect.timeoutOrElse({
166
+ duration: remaining,
167
+ orElse: () => Effect.fail(MemoryRpcError.make({ reason: "timeout" }))
168
+ }));
169
+ }).pipe(Effect.flatMap((response) => encodeMemoryWire(MemoryOwnerResponse, response, limits.maxResponseBytes)), Effect.result);
170
+ if (result._tag === "Success") return result.success;
171
+ return yield* encodeMemoryWire(MemoryOwnerResponse, {
172
+ _tag: "Failed",
173
+ failure: result.failure
174
+ }, limits.maxResponseBytes).pipe(Effect.catch(() => Schema.encodeEffect(Schema.fromJsonString(MemoryOwnerResponse))({
175
+ _tag: "Failed",
176
+ failure: MemoryRpcError.make({ reason: "budget" })
177
+ }).pipe(Effect.orDie)));
178
+ });
179
+ //#endregion
180
+ export { MemoryOwnerAuthorizer, MemoryOwnerFailure, MemoryOwnerIdentity, MemoryOwnerRequest, MemoryOwnerResponse, MemoryRpcError, MemoryRpcLimits, decodeMemoryWire, defaultMemoryRpcLimits, encodeMemoryWire, handleMemoryOwnerRequest, memoryWireBytes, MemoryProtocol_exports as t };
181
+
182
+ //# sourceMappingURL=MemoryProtocol.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MemoryProtocol.mjs","names":[],"sources":["../src/MemoryProtocol.ts"],"sourcesContent":["import * as MemoryNamespace from \"@effect-agent/core/MemoryNamespace\";\nimport {\n MemoryLookup,\n MemoryRecallError,\n MemoryRecallLimits,\n} from \"@effect-agent/core/MemoryReference\";\nimport { MemoryAccess, revalidateMemoryLookup } from \"@effect-agent/core/MemoryRevalidation\";\nimport { type MemoryReader } from \"@effect-agent/core/MemoryStore\";\nimport {\n MemoryConflict,\n MemoryDocument,\n MemoryMutationFailure,\n MemoryOperationConflict,\n MemoryStorageError,\n MemoryWithdrawn,\n MemoryWrite,\n MemoryWriter,\n} from \"@effect-agent/core/MemoryStore\";\nimport {\n MemoryIndexSearch,\n MemoryIndexError,\n SemanticMemoryProfile,\n} from \"@effect-agent/core/SemanticMemoryIndex\";\nimport {\n SemanticMemoryError,\n SemanticCandidateLimits,\n SemanticCandidateResult,\n revalidateSemanticMemoryCandidates,\n} from \"@effect-agent/core/SemanticMemoryRevalidation\";\nimport { Principal } from \"@effect-agent/thread/SubmissionLedger\";\nimport { Clock, Context, Effect, Schema } from \"effect\";\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 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]\n> = Schema.Union([RevalidateRequest, ChangeRequest, SemanticRequest]);\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\nexport const MemoryOwnerResponse = Schema.Union([\n Schema.TaggedStruct(\"Lookup\", { access: MemoryAccess.Wire, lookup: MemoryLookup }),\n Schema.TaggedStruct(\"Changed\", { access: MemoryAccess.Wire, document: MemoryDocument.Wire }),\n Schema.TaggedStruct(\"Semantic\", { access: MemoryAccess.Wire, result: SemanticCandidateResult }),\n Schema.TaggedStruct(\"Failed\", { failure: MemoryOwnerFailure }),\n]);\n\nexport type MemoryOwnerResponse = typeof MemoryOwnerResponse.Type;\n\n/** Fail-closed application policy. Authorize the namespace, principal, scope, and full command. */\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 per distinct candidate source, with no per-document network calls. */\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.decodeUnknownEffect(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 === \"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 === \"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":";;;;;;;;;;;;;;;;;;;;;;;;AAgCA,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,k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