@yielded/agent-storage-memory 0.1.0-beta.166

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.
@@ -0,0 +1,223 @@
1
+ import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
+ import { Clock, Effect, Layer, Ref, Schema } from "effect";
3
+ import { MemoryKey } from "@yielded/agent/memory-store";
4
+ import { MemoryIndexCandidate, MemoryIndexError, MemoryIndexQuery, MemoryIndexReplacement, MemoryIndexSearch, MemoryIndexSource, SemanticMemoryChunk, SemanticMemoryIndex, SemanticMemoryProfile } from "@yielded/agent/semantic-memory-index";
5
+ import { Hex } from "effect/encoding";
6
+ //#region src/MemorySemanticIndex.ts
7
+ var MemorySemanticIndex_exports = /* @__PURE__ */ __exportAll({
8
+ InMemorySemanticIndexCapacity: () => InMemorySemanticIndexCapacity,
9
+ inMemorySemanticIndexLayer: () => inMemorySemanticIndexLayer
10
+ });
11
+ const PositiveCapacity = Schema.Int.check(Schema.isBetween({
12
+ minimum: 1,
13
+ maximum: 65536
14
+ }));
15
+ const MaxStoredVectorComponents = 16777216;
16
+ /**
17
+ * Hard per-Layer bounds for disposable semantic index state. maxChunks times profile dimensions
18
+ * must not exceed 16,777,216 vector components. maxSourceBytes bounds the aggregate UTF-8 JSON
19
+ * of retained source identities and defaults to 16 MiB; it is not a general heap limit.
20
+ */
21
+ var InMemorySemanticIndexCapacity = class extends Schema.Class("@effect-agent/storage-memory/InMemorySemanticIndexCapacity")({
22
+ maxSources: PositiveCapacity,
23
+ maxChunks: PositiveCapacity,
24
+ maxSourceBytes: Schema.optionalKey(Schema.Int.check(Schema.isBetween({
25
+ minimum: 1,
26
+ maximum: 67108864
27
+ })))
28
+ }) {};
29
+ const sameProfile = Schema.toEquivalence(SemanticMemoryProfile);
30
+ const sameSource = Schema.toEquivalence(MemoryIndexSource.Wire);
31
+ const error = (operation, reason) => MemoryIndexError.make({
32
+ operation,
33
+ reason
34
+ });
35
+ const keyString = (key) => JSON.stringify([key.namespace.address, key.id]);
36
+ const sourceIdentityBytes = (source) => Hex.encode(JSON.stringify(source)).length / 2;
37
+ const decodeBoundary = Effect.fn("InMemorySemanticIndex.decodeBoundary")(function* (schema, value, operation) {
38
+ return yield* Schema.decodeUnknownEffect(schema)(value).pipe(Effect.flatMap((decoded) => Schema.encodeEffect(schema)(decoded).pipe(Effect.as(decoded))), Effect.mapError(() => error(operation, "invalid-input")));
39
+ });
40
+ const freezeSource = (source) => Object.freeze(MemoryIndexSource.make({
41
+ key: Object.freeze(MemoryKey.make({
42
+ ...source.key,
43
+ namespace: Object.freeze({ address: source.key.namespace.address })
44
+ })),
45
+ source: Object.freeze({ ...source.source }),
46
+ sourceGeneration: source.sourceGeneration
47
+ }));
48
+ const freezeChunk = (chunk) => Object.freeze(SemanticMemoryChunk.make({
49
+ ...chunk,
50
+ vector: Object.freeze([...chunk.vector])
51
+ }));
52
+ const sourceIsFenced = (source, existing) => source.sourceGeneration < existing.source.sourceGeneration || source.sourceGeneration === existing.source.sourceGeneration && !sameSource(source, existing.source);
53
+ const squaredNorm = (vector) => {
54
+ let sum = 0;
55
+ for (const value of vector) {
56
+ sum += value * value;
57
+ if (!Number.isFinite(sum)) return null;
58
+ }
59
+ return sum > 0 ? sum : null;
60
+ };
61
+ const validVector = (vector, profile) => vector.length === profile.dimensions && squaredNorm(vector) !== null;
62
+ const validateChunks = Effect.fn("InMemorySemanticIndex.validateChunks")(function* (chunks, profile, operation) {
63
+ let nextByte = 0;
64
+ const passageIds = /* @__PURE__ */ new Set();
65
+ for (let index = 0; index < chunks.length; index++) {
66
+ const chunk = chunks[index];
67
+ const byteLength = Hex.encode(chunk.text).length / 2;
68
+ if (chunk.ordinal !== index || chunk.startByte !== nextByte || chunk.endByte <= chunk.startByte || chunk.endByte - chunk.startByte !== byteLength || byteLength > profile.maxChunkBytes || passageIds.has(chunk.passageId) || !validVector(chunk.vector, profile)) return yield* error(operation, "invalid-input");
69
+ passageIds.add(chunk.passageId);
70
+ nextByte = chunk.endByte;
71
+ }
72
+ });
73
+ const cosine = (left, right) => {
74
+ const leftNorm = Math.sqrt(squaredNorm(left) ?? 1);
75
+ const rightNorm = Math.sqrt(squaredNorm(right) ?? 1);
76
+ let score = 0;
77
+ for (let index = 0; index < left.length; index++) score += left[index] / leftNorm * (right[index] / rightNorm);
78
+ const bounded = Math.max(-1, Math.min(1, score));
79
+ return bounded === 0 ? 0 : bounded;
80
+ };
81
+ const compareText = (left, right) => left < right ? -1 : left > right ? 1 : 0;
82
+ const makeIndex = Effect.fn("InMemorySemanticIndex.make")(function* (rawProfile, rawCapacity) {
83
+ const profile = Object.freeze(SemanticMemoryProfile.make({ ...yield* decodeBoundary(SemanticMemoryProfile, rawProfile, "configure semantic memory index") }));
84
+ const capacity = yield* decodeBoundary(InMemorySemanticIndexCapacity, rawCapacity, "configure semantic memory index");
85
+ if (capacity.maxChunks * profile.dimensions > MaxStoredVectorComponents) return yield* error("configure semantic memory index", "invalid-input");
86
+ const maxSourceBytes = capacity.maxSourceBytes ?? 16777216;
87
+ const data = yield* Ref.make({
88
+ closed: false,
89
+ entries: /* @__PURE__ */ new Map(),
90
+ sourceBytes: 0
91
+ });
92
+ yield* Effect.addFinalizer(() => Ref.set(data, {
93
+ closed: true,
94
+ entries: /* @__PURE__ */ new Map(),
95
+ sourceBytes: 0
96
+ }));
97
+ const ensureOpen = Effect.fn("InMemorySemanticIndex.ensureOpen")(function* (operation) {
98
+ if ((yield* Ref.get(data)).closed) return yield* error(operation, "unavailable");
99
+ });
100
+ const replace = Effect.fn("InMemorySemanticIndex.replace")(function* (rawRequest) {
101
+ const operation = "replace semantic memory source";
102
+ yield* ensureOpen(operation);
103
+ const request = yield* decodeBoundary(MemoryIndexReplacement.Wire, rawRequest, operation);
104
+ const source = freezeSource(request.source);
105
+ const sourceBytes = sourceIdentityBytes(source);
106
+ const chunks = Object.freeze(request.chunks.map(freezeChunk));
107
+ if (source.source.id !== source.key.id) return yield* error(operation, "invalid-input");
108
+ if (!sameProfile(request.profile, profile)) return yield* error(operation, "incompatible");
109
+ yield* validateChunks(chunks, profile, operation);
110
+ const indexedAt = yield* Clock.currentTimeMillis;
111
+ const failure = yield* Ref.modify(data, (current) => {
112
+ if (current.closed) return [error(operation, "unavailable"), current];
113
+ const id = keyString(source.key);
114
+ const existing = current.entries.get(id);
115
+ if (existing !== void 0 && (existing._tag === "Withdrawn" || sourceIsFenced(source, existing))) return [error(operation, "fenced"), current];
116
+ if (existing === void 0 && current.entries.size >= capacity.maxSources) return [error(operation, "budget"), current];
117
+ const nextSourceBytes = current.sourceBytes - (existing?.sourceBytes ?? 0) + sourceBytes;
118
+ if (nextSourceBytes > maxSourceBytes) return [error(operation, "budget"), current];
119
+ let count = chunks.length;
120
+ for (const [entryId, entry] of current.entries) if (entryId !== id && entry._tag === "Indexed") count += entry.chunks.length;
121
+ if (count > capacity.maxChunks) return [error(operation, "budget"), current];
122
+ const entries = new Map(current.entries);
123
+ entries.set(id, {
124
+ _tag: "Indexed",
125
+ source,
126
+ sourceBytes,
127
+ chunks,
128
+ indexedAt
129
+ });
130
+ return [void 0, {
131
+ ...current,
132
+ entries,
133
+ sourceBytes: nextSourceBytes
134
+ }];
135
+ });
136
+ if (failure !== void 0) return yield* failure;
137
+ });
138
+ const withdraw = Effect.fn("InMemorySemanticIndex.withdraw")(function* (rawSource) {
139
+ const operation = "withdraw semantic memory source";
140
+ yield* ensureOpen(operation);
141
+ const source = freezeSource(yield* decodeBoundary(MemoryIndexSource.Wire, rawSource, operation));
142
+ const sourceBytes = sourceIdentityBytes(source);
143
+ if (source.source.id !== source.key.id) return yield* error(operation, "invalid-input");
144
+ const failure = yield* Ref.modify(data, (current) => {
145
+ if (current.closed) return [error(operation, "unavailable"), current];
146
+ const id = keyString(source.key);
147
+ const existing = current.entries.get(id);
148
+ if (existing !== void 0) {
149
+ if (existing._tag === "Withdrawn") return [sameSource(source, existing.source) ? void 0 : error(operation, "fenced"), current];
150
+ if (sourceIsFenced(source, existing)) return [error(operation, "fenced"), current];
151
+ } else if (current.entries.size >= capacity.maxSources) return [error(operation, "budget"), current];
152
+ const nextSourceBytes = current.sourceBytes - (existing?.sourceBytes ?? 0) + sourceBytes;
153
+ if (nextSourceBytes > maxSourceBytes) return [error(operation, "budget"), current];
154
+ const entries = new Map(current.entries);
155
+ entries.set(id, {
156
+ _tag: "Withdrawn",
157
+ source,
158
+ sourceBytes
159
+ });
160
+ return [void 0, {
161
+ ...current,
162
+ entries,
163
+ sourceBytes: nextSourceBytes
164
+ }];
165
+ });
166
+ if (failure !== void 0) return yield* failure;
167
+ });
168
+ const search = Effect.fn("InMemorySemanticIndex.search")(function* (rawQuery) {
169
+ const operation = "search semantic memory index";
170
+ yield* ensureOpen(operation);
171
+ const query = yield* decodeBoundary(MemoryIndexQuery.Wire, rawQuery, operation);
172
+ const vector = Object.freeze([...query.vector]);
173
+ if (!validVector(vector, profile)) return yield* error(operation, "invalid-input");
174
+ const current = yield* Ref.get(data);
175
+ if (current.closed) return yield* error(operation, "unavailable");
176
+ let scannedChunks = 0;
177
+ let inspectedSources = 0;
178
+ const candidates = [];
179
+ for (const entry of current.entries.values()) {
180
+ inspectedSources += 1;
181
+ if (inspectedSources % 128 === 0) yield* Effect.yieldNow;
182
+ if (entry.source.key.namespace.address !== query.namespace.address || entry._tag !== "Indexed") continue;
183
+ scannedChunks += entry.chunks.length;
184
+ if (scannedChunks > query.maxScannedChunks) return yield* error(operation, "budget");
185
+ }
186
+ for (const entry of current.entries.values()) {
187
+ if (entry.source.key.namespace.address !== query.namespace.address || entry._tag !== "Indexed") continue;
188
+ yield* Effect.yieldNow;
189
+ for (const chunk of entry.chunks) {
190
+ const score = cosine(vector, chunk.vector);
191
+ if (score < query.minScore) continue;
192
+ candidates.push(MemoryIndexCandidate.make({
193
+ ...entry.source,
194
+ passageId: chunk.passageId,
195
+ ordinal: chunk.ordinal,
196
+ startByte: chunk.startByte,
197
+ endByte: chunk.endByte,
198
+ text: chunk.text,
199
+ score,
200
+ indexedAt: entry.indexedAt
201
+ }));
202
+ }
203
+ }
204
+ candidates.sort((left, right) => right.score - left.score || compareText(left.key.id, right.key.id) || compareText(left.source.revision, right.source.revision) || left.ordinal - right.ordinal);
205
+ yield* ensureOpen(operation);
206
+ return MemoryIndexSearch.make({
207
+ candidates: candidates.slice(0, query.limit),
208
+ scannedChunks
209
+ });
210
+ });
211
+ return SemanticMemoryIndex.fromAdapter({
212
+ profile,
213
+ replace,
214
+ withdraw,
215
+ search
216
+ });
217
+ });
218
+ /** Scoped disposable semantic index. No persistent build or recovery state is retained. */
219
+ const inMemorySemanticIndexLayer = (profile, capacity) => Layer.effect(SemanticMemoryIndex, makeIndex(profile, capacity));
220
+ //#endregion
221
+ export { InMemorySemanticIndexCapacity, inMemorySemanticIndexLayer, MemorySemanticIndex_exports as t };
222
+
223
+ //# sourceMappingURL=MemorySemanticIndex.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MemorySemanticIndex.mjs","names":[],"sources":["../src/MemorySemanticIndex.ts"],"sourcesContent":["import { MemoryKey } from \"@yielded/agent/memory-store\";\nimport {\n MemoryIndexCandidate,\n MemoryIndexError,\n MemoryIndexQuery,\n MemoryIndexReplacement,\n MemoryIndexSearch,\n MemoryIndexSource,\n SemanticMemoryChunk,\n SemanticMemoryIndex,\n SemanticMemoryProfile,\n} from \"@yielded/agent/semantic-memory-index\";\nimport { Clock, Effect, Layer, Ref, Schema } from \"effect\";\nimport { Hex } from \"effect/encoding\";\n\nconst PositiveCapacity = Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 65_536 }));\nconst MaxStoredVectorComponents = 16_777_216;\n\n/**\n * Hard per-Layer bounds for disposable semantic index state. maxChunks times profile dimensions\n * must not exceed 16,777,216 vector components. maxSourceBytes bounds the aggregate UTF-8 JSON\n * of retained source identities and defaults to 16 MiB; it is not a general heap limit.\n */\nexport class InMemorySemanticIndexCapacity extends Schema.Class<InMemorySemanticIndexCapacity>(\n \"@effect-agent/storage-memory/InMemorySemanticIndexCapacity\",\n)({\n maxSources: PositiveCapacity,\n maxChunks: PositiveCapacity,\n maxSourceBytes: Schema.optionalKey(\n Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 67_108_864 })),\n ),\n}) {}\n\ntype StoredEntry = {\n readonly source: MemoryIndexSource;\n readonly sourceBytes: number;\n} & (\n | {\n readonly _tag: \"Indexed\";\n readonly chunks: ReadonlyArray<SemanticMemoryChunk>;\n readonly indexedAt: number;\n }\n | { readonly _tag: \"Withdrawn\" }\n);\n\ninterface IndexData {\n readonly closed: boolean;\n readonly entries: ReadonlyMap<string, StoredEntry>;\n readonly sourceBytes: number;\n}\n\nconst sameProfile = Schema.toEquivalence(SemanticMemoryProfile);\nconst sameSource = Schema.toEquivalence(MemoryIndexSource.Wire);\n\nconst error = (operation: string, reason: MemoryIndexError[\"reason\"]): MemoryIndexError =>\n MemoryIndexError.make({ operation, reason });\n\nconst keyString = (key: MemoryKey): string => JSON.stringify([key.namespace.address, key.id]);\n\nconst sourceIdentityBytes = (source: MemoryIndexSource): number =>\n Hex.encode(JSON.stringify(source)).length / 2;\n\nconst decodeBoundary = Effect.fn(\"InMemorySemanticIndex.decodeBoundary\")(function* <A, I>(\n schema: Schema.Codec<A, I, never>,\n value: unknown,\n operation: string,\n): Effect.fn.Return<A, MemoryIndexError> {\n return yield* Schema.decodeUnknownEffect(schema)(value).pipe(\n Effect.flatMap((decoded) => Schema.encodeEffect(schema)(decoded).pipe(Effect.as(decoded))),\n Effect.mapError(() => error(operation, \"invalid-input\")),\n );\n});\n\nconst freezeSource = (source: MemoryIndexSource): MemoryIndexSource =>\n Object.freeze(\n MemoryIndexSource.make({\n key: Object.freeze(\n MemoryKey.make({\n ...source.key,\n namespace: Object.freeze({ address: source.key.namespace.address }),\n }),\n ),\n source: Object.freeze({ ...source.source }),\n sourceGeneration: source.sourceGeneration,\n }),\n );\n\nconst freezeChunk = (chunk: SemanticMemoryChunk): SemanticMemoryChunk =>\n Object.freeze(SemanticMemoryChunk.make({ ...chunk, vector: Object.freeze([...chunk.vector]) }));\n\nconst sourceIsFenced = (source: MemoryIndexSource, existing: StoredEntry): boolean =>\n source.sourceGeneration < existing.source.sourceGeneration ||\n (source.sourceGeneration === existing.source.sourceGeneration &&\n !sameSource(source, existing.source));\n\nconst squaredNorm = (vector: ReadonlyArray<number>): number | null => {\n let sum = 0;\n\n for (const value of vector) {\n sum += value * value;\n if (!Number.isFinite(sum)) return null;\n }\n\n return sum > 0 ? sum : null;\n};\n\nconst validVector = (vector: ReadonlyArray<number>, profile: SemanticMemoryProfile): boolean =>\n vector.length === profile.dimensions && squaredNorm(vector) !== null;\n\nconst validateChunks = Effect.fn(\"InMemorySemanticIndex.validateChunks\")(function* (\n chunks: ReadonlyArray<SemanticMemoryChunk>,\n profile: SemanticMemoryProfile,\n operation: string,\n): Effect.fn.Return<void, MemoryIndexError> {\n let nextByte = 0;\n const passageIds = new Set<string>();\n\n for (let index = 0; index < chunks.length; index++) {\n const chunk = chunks[index];\n const byteLength = Hex.encode(chunk.text).length / 2;\n\n if (\n chunk.ordinal !== index ||\n chunk.startByte !== nextByte ||\n chunk.endByte <= chunk.startByte ||\n chunk.endByte - chunk.startByte !== byteLength ||\n byteLength > profile.maxChunkBytes ||\n passageIds.has(chunk.passageId) ||\n !validVector(chunk.vector, profile)\n ) {\n return yield* error(operation, \"invalid-input\");\n }\n passageIds.add(chunk.passageId);\n nextByte = chunk.endByte;\n }\n});\n\nconst cosine = (left: ReadonlyArray<number>, right: ReadonlyArray<number>): number => {\n const leftNorm = Math.sqrt(squaredNorm(left) ?? 1);\n const rightNorm = Math.sqrt(squaredNorm(right) ?? 1);\n let score = 0;\n\n for (let index = 0; index < left.length; index++) {\n score += (left[index] / leftNorm) * (right[index] / rightNorm);\n }\n const bounded = Math.max(-1, Math.min(1, score));\n\n return bounded === 0 ? 0 : bounded;\n};\n\nconst compareText = (left: string, right: string): number =>\n left < right ? -1 : left > right ? 1 : 0;\n\nconst makeIndex = Effect.fn(\"InMemorySemanticIndex.make\")(function* (\n rawProfile: SemanticMemoryProfile,\n rawCapacity: InMemorySemanticIndexCapacity,\n) {\n const profile = Object.freeze(\n SemanticMemoryProfile.make({\n ...(yield* decodeBoundary(\n SemanticMemoryProfile,\n rawProfile,\n \"configure semantic memory index\",\n )),\n }),\n );\n\n const capacity = yield* decodeBoundary(\n InMemorySemanticIndexCapacity,\n rawCapacity,\n \"configure semantic memory index\",\n );\n\n if (capacity.maxChunks * profile.dimensions > MaxStoredVectorComponents) {\n return yield* error(\"configure semantic memory index\", \"invalid-input\");\n }\n const maxSourceBytes = capacity.maxSourceBytes ?? 16_777_216;\n const data = yield* Ref.make<IndexData>({ closed: false, entries: new Map(), sourceBytes: 0 });\n\n yield* Effect.addFinalizer(() =>\n Ref.set(data, { closed: true, entries: new Map(), sourceBytes: 0 }),\n );\n\n const ensureOpen = Effect.fn(\"InMemorySemanticIndex.ensureOpen\")(function* (operation: string) {\n if ((yield* Ref.get(data)).closed) return yield* error(operation, \"unavailable\");\n });\n\n const replace: SemanticMemoryIndex[\"Service\"][\"replace\"] = Effect.fn(\n \"InMemorySemanticIndex.replace\",\n )(function* (rawRequest) {\n const operation = \"replace semantic memory source\";\n\n yield* ensureOpen(operation);\n const request = yield* decodeBoundary(MemoryIndexReplacement.Wire, rawRequest, operation);\n const source = freezeSource(request.source);\n const sourceBytes = sourceIdentityBytes(source);\n const chunks = Object.freeze(request.chunks.map(freezeChunk));\n\n if (source.source.id !== source.key.id) return yield* error(operation, \"invalid-input\");\n if (!sameProfile(request.profile, profile)) return yield* error(operation, \"incompatible\");\n yield* validateChunks(chunks, profile, operation);\n const indexedAt = yield* Clock.currentTimeMillis;\n\n const failure = yield* Ref.modify(\n data,\n (current): readonly [MemoryIndexError | undefined, IndexData] => {\n if (current.closed) return [error(operation, \"unavailable\"), current];\n const id = keyString(source.key);\n const existing = current.entries.get(id);\n\n if (\n existing !== undefined &&\n (existing._tag === \"Withdrawn\" || sourceIsFenced(source, existing))\n ) {\n return [error(operation, \"fenced\"), current];\n }\n if (existing === undefined && current.entries.size >= capacity.maxSources) {\n return [error(operation, \"budget\"), current];\n }\n const nextSourceBytes = current.sourceBytes - (existing?.sourceBytes ?? 0) + sourceBytes;\n\n if (nextSourceBytes > maxSourceBytes) return [error(operation, \"budget\"), current];\n let count = chunks.length;\n\n for (const [entryId, entry] of current.entries) {\n if (entryId !== id && entry._tag === \"Indexed\") count += entry.chunks.length;\n }\n if (count > capacity.maxChunks) return [error(operation, \"budget\"), current];\n const entries = new Map(current.entries);\n\n entries.set(id, { _tag: \"Indexed\", source, sourceBytes, chunks, indexedAt });\n\n return [undefined, { ...current, entries, sourceBytes: nextSourceBytes }];\n },\n );\n\n if (failure !== undefined) return yield* failure;\n });\n\n const withdraw: SemanticMemoryIndex[\"Service\"][\"withdraw\"] = Effect.fn(\n \"InMemorySemanticIndex.withdraw\",\n )(function* (rawSource) {\n const operation = \"withdraw semantic memory source\";\n\n yield* ensureOpen(operation);\n\n const source = freezeSource(\n yield* decodeBoundary(MemoryIndexSource.Wire, rawSource, operation),\n );\n\n const sourceBytes = sourceIdentityBytes(source);\n\n if (source.source.id !== source.key.id) return yield* error(operation, \"invalid-input\");\n\n const failure = yield* Ref.modify(\n data,\n (current): readonly [MemoryIndexError | undefined, IndexData] => {\n if (current.closed) return [error(operation, \"unavailable\"), current];\n const id = keyString(source.key);\n const existing = current.entries.get(id);\n\n if (existing !== undefined) {\n if (existing._tag === \"Withdrawn\") {\n return [\n sameSource(source, existing.source) ? undefined : error(operation, \"fenced\"),\n current,\n ];\n }\n if (sourceIsFenced(source, existing)) return [error(operation, \"fenced\"), current];\n } else if (current.entries.size >= capacity.maxSources) {\n return [error(operation, \"budget\"), current];\n }\n const nextSourceBytes = current.sourceBytes - (existing?.sourceBytes ?? 0) + sourceBytes;\n\n if (nextSourceBytes > maxSourceBytes) return [error(operation, \"budget\"), current];\n const entries = new Map(current.entries);\n\n entries.set(id, { _tag: \"Withdrawn\", source, sourceBytes });\n\n return [undefined, { ...current, entries, sourceBytes: nextSourceBytes }];\n },\n );\n\n if (failure !== undefined) return yield* failure;\n });\n\n const search = Effect.fn(\"InMemorySemanticIndex.search\")(function* (rawQuery: MemoryIndexQuery) {\n const operation = \"search semantic memory index\";\n\n yield* ensureOpen(operation);\n const query = yield* decodeBoundary(MemoryIndexQuery.Wire, rawQuery, operation);\n const vector = Object.freeze([...query.vector]);\n\n if (!validVector(vector, profile)) return yield* error(operation, \"invalid-input\");\n const current = yield* Ref.get(data);\n\n if (current.closed) return yield* error(operation, \"unavailable\");\n let scannedChunks = 0;\n let inspectedSources = 0;\n const candidates: Array<MemoryIndexCandidate> = [];\n\n for (const entry of current.entries.values()) {\n inspectedSources += 1;\n if (inspectedSources % 128 === 0) yield* Effect.yieldNow;\n if (\n entry.source.key.namespace.address !== query.namespace.address ||\n entry._tag !== \"Indexed\"\n )\n continue;\n scannedChunks += entry.chunks.length;\n if (scannedChunks > query.maxScannedChunks) return yield* error(operation, \"budget\");\n }\n for (const entry of current.entries.values()) {\n if (\n entry.source.key.namespace.address !== query.namespace.address ||\n entry._tag !== \"Indexed\"\n )\n continue;\n yield* Effect.yieldNow;\n for (const chunk of entry.chunks) {\n const score = cosine(vector, chunk.vector);\n\n if (score < query.minScore) continue;\n candidates.push(\n MemoryIndexCandidate.make({\n ...entry.source,\n passageId: chunk.passageId,\n ordinal: chunk.ordinal,\n startByte: chunk.startByte,\n endByte: chunk.endByte,\n text: chunk.text,\n score,\n indexedAt: entry.indexedAt,\n }),\n );\n }\n }\n candidates.sort(\n (left, right) =>\n right.score - left.score ||\n compareText(left.key.id, right.key.id) ||\n compareText(left.source.revision, right.source.revision) ||\n left.ordinal - right.ordinal,\n );\n yield* ensureOpen(operation);\n\n return MemoryIndexSearch.make({ candidates: candidates.slice(0, query.limit), scannedChunks });\n });\n\n return SemanticMemoryIndex.fromAdapter({ profile, replace, withdraw, search });\n});\n\n/** Scoped disposable semantic index. No persistent build or recovery state is retained. */\nexport const inMemorySemanticIndexLayer = (\n profile: SemanticMemoryProfile,\n capacity: InMemorySemanticIndexCapacity,\n): Layer.Layer<SemanticMemoryIndex, MemoryIndexError> =>\n Layer.effect(SemanticMemoryIndex, makeIndex(profile, capacity));\n"],"mappings":";;;;;;;;;;AAeA,MAAM,mBAAmB,OAAO,IAAI,MAAM,OAAO,UAAU;CAAE,SAAS;CAAG,SAAS;AAAO,CAAC,CAAC;AAC3F,MAAM,4BAA4B;;;;;;AAOlC,IAAa,gCAAb,cAAmD,OAAO,MACxD,4DACF,CAAC,CAAC;CACA,YAAY;CACZ,WAAW;CACX,gBAAgB,OAAO,YACrB,OAAO,IAAI,MAAM,OAAO,UAAU;EAAE,SAAS;EAAG,SAAS;CAAW,CAAC,CAAC,CACxE;AACF,CAAC,CAAC,CAAC,CAAC;AAoBJ,MAAM,cAAc,OAAO,cAAc,qBAAqB;AAC9D,MAAM,aAAa,OAAO,cAAc,kBAAkB,IAAI;AAE9D,MAAM,SAAS,WAAmB,WAChC,iBAAiB,KAAK;CAAE;CAAW;AAAO,CAAC;AAE7C,MAAM,aAAa,QAA2B,KAAK,UAAU,CAAC,IAAI,UAAU,SAAS,IAAI,EAAE,CAAC;AAE5F,MAAM,uBAAuB,WAC3B,IAAI,OAAO,KAAK,UAAU,MAAM,CAAC,CAAC,CAAC,SAAS;AAE9C,MAAM,iBAAiB,OAAO,GAAG,sCAAsC,CAAC,CAAC,WACvE,QACA,OACA,WACuC;CACvC,OAAO,OAAO,OAAO,oBAAoB,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,KACtD,OAAO,SAAS,YAAY,OAAO,aAAa,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,OAAO,GAAG,OAAO,CAAC,CAAC,GACzF,OAAO,eAAe,MAAM,WAAW,eAAe,CAAC,CACzD;AACF,CAAC;AAED,MAAM,gBAAgB,WACpB,OAAO,OACL,kBAAkB,KAAK;CACrB,KAAK,OAAO,OAC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M,UAAU;IAC5B,WAAW,KACT,qBAAqB,KAAK;KACxB,GAAG,MAAM;KACT,WAAW,MAAM;KACjB,SAAS,MAAM;KACf,WAAW,MAAM;KACjB,SAAS,MAAM;KACf,MAAM,MAAM;KACZ;KACA,WAAW,MAAM;IACnB,CAAC,CACH;GACF;EACF;EACA,WAAW,MACR,MAAM,UACL,MAAM,QAAQ,KAAK,SACnB,YAAY,KAAK,IAAI,IAAI,MAAM,IAAI,EAAE,KACrC,YAAY,KAAK,OAAO,UAAU,MAAM,OAAO,QAAQ,KACvD,KAAK,UAAU,MAAM,OACzB;EACA,OAAO,WAAW,SAAS;EAE3B,OAAO,kBAAkB,KAAK;GAAE,YAAY,WAAW,MAAM,GAAG,MAAM,KAAK;GAAG;EAAc,CAAC;CAC/F,CAAC;CAED,OAAO,oBAAoB,YAAY;EAAE;EAAS;EAAS;EAAU;CAAO,CAAC;AAC/E,CAAC;;AAGD,MAAa,8BACX,SACA,aAEA,MAAM,OAAO,qBAAqB,UAAU,SAAS,QAAQ,CAAC"}
@@ -0,0 +1,24 @@
1
+ import { Effect, Layer, Option } from "effect";
2
+ import { SubmissionLedger } from "@yielded/agent/submission-ledger";
3
+ declare namespace MemorySubmissionLedger_d_exports {
4
+ export { MemorySubmissionLedgerLive, MemorySubmissionLedgerOptions, memorySubmissionLedgerLayer };
5
+ }
6
+ /** Construction options for the in-memory reference SubmissionLedger. */
7
+ export interface MemorySubmissionLedgerOptions {
8
+ /**
9
+ * Test-only fault seam for `resolveAdmission` (SUB-031): when the effect yields a reason,
10
+ * the resolution answers `Indeterminate` with it instead of consulting the store — modelling
11
+ * an authoritative child owner that is temporarily unreachable. `Option.none()` restores the
12
+ * store-derived answer. Ledger state is never mutated by the fault.
13
+ */
14
+ readonly resolveAdmissionFault?: Effect.Effect<Option.Option<string>>;
15
+ }
16
+ /**
17
+ * In-memory reference SubmissionLedger Layer (durability `non-durable`). All state lives in one
18
+ * `Ref` owned by the Layer's Scope; no daemon fibers are spawned and no wall clock is consulted.
19
+ */
20
+ export declare const memorySubmissionLedgerLayer: (options?: MemorySubmissionLedgerOptions) => Layer.Layer<SubmissionLedger>;
21
+ export declare const MemorySubmissionLedgerLive: Layer.Layer<SubmissionLedger>;
22
+ //#endregion
23
+ export { MemorySubmissionLedger_d_exports as t };
24
+ //# sourceMappingURL=MemorySubmissionLedger.d.mts.map