@theokit/sdk-memory 0.5.2 → 0.6.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,102 @@
1
1
  # Changelog — @theokit/sdk-memory
2
2
 
3
+ ## 0.6.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#724](https://github.com/usetheokit/theokit-sdk/pull/724) [`bc2f665`](https://github.com/usetheokit/theokit-sdk/commit/bc2f665d4c5e03dda26858004ac60bd1f7d3f3bb) Thanks [@usetheodev](https://github.com/usetheodev)! - Memory can now be given a fact ceiling. Once the store is full, a new fact is refused with a
8
+ typed error. Before this, nothing stopped the store growing, and the one cap that did exist, 500
9
+ facts per dreaming sweep, left the rest out of clustering while the result still said
10
+ `status: "ok"`.
11
+
12
+ Set `memory.maxFacts` on an agent to declare a ceiling. A new fact at or over it is refused with a
13
+ `ConfigurationError` whose code is `memory_ceiling_reached`, and nothing is written. The message
14
+ gives the directory, how many facts it holds, the ceiling and what to consolidate. Recording a fact
15
+ that is already stored counts as a second observation, not a new fact, so it is still admitted. A
16
+ value that is not a positive integer is refused with `invalid_memory_ceiling` by `Agent.create`
17
+ and `Agent.resume`, before the agent exists, and again by the writer before anything touches
18
+ disk. A prompt-driven memory write that hits the ceiling reports it on one diagnostic line and
19
+ stores nothing. With no ceiling declared, nothing changes and no extra read happens.
20
+
21
+ The ceiling holds across processes. Every SDK process that writes one memory directory with a
22
+ ceiling takes a lock (`<directory>.lock`, beside the directory) around the count and the write, so
23
+ two processes one fact below the ceiling no longer both get in. The lock needs the optional
24
+ `proper-lockfile` peer. Without it the ceiling holds within one process only, and the SDK says so
25
+ once on its diagnostics channel. A writer that declares no ceiling, such as the Claude Code CLI,
26
+ takes no lock and can still add facts past it.
27
+
28
+ A resumed agent keeps its ceiling and its `memory.directory`. The directory is now saved with the
29
+ agent, and a `memory` object passed to `Agent.resume` replaces only the fields it sets (a field
30
+ set to `undefined` keeps its persisted value). Before
31
+ this, `Agent.resume(id, { memory: { enabled: true } })` came back with no ceiling, and any resumed
32
+ agent wrote to the default store instead of the directory it was created with.
33
+
34
+ `createInMemoryMarkdownProvider({ maxFacts })` in `@theokit/sdk-memory` applies the same ceiling
35
+ to the adapter's `write` and to the `memory_remember` tool. Each handle counts its own facts.
36
+
37
+ `DreamingSweepResult` gains `factsLeftOut`, the number of deduplicated facts past the 500-fact
38
+ cap that a sweep did not cluster. It is reported on both dispatch paths, the `@theokit/sdk-memory`
39
+ peer and the SDK's own fallback. It is 0 on a skipped sweep and absent on a failed one. It is also
40
+ absent when an older `@theokit/sdk-memory` runs the sweep, because that version cannot count it,
41
+ so read absence as unknown, not as 0. `ClusterResult`, which `@theokit/sdk-memory` exports, now
42
+ requires `factsLeftOut` too. Code that builds its own `ClusterResult`, such as a mock of
43
+ `remPhase`, has to add it.
44
+
45
+ `@theokit/sdk-memory` now needs `@theokit/sdk` `^5.10.0`, the first release that exports the
46
+ ceiling helpers it imports.
47
+
48
+ ## 0.5.3
49
+
50
+ ### Patch Changes
51
+
52
+ - [#570](https://github.com/usetheokit/theokit-sdk/pull/570) [`64ff1f8`](https://github.com/usetheokit/theokit-sdk/commit/64ff1f8e072725dbc4d4dbbb5f82e6f1bca6141e) Thanks [@usetheodev](https://github.com/usetheodev)! - The memory index moves with a relocated store — the note this package's consumers never got ([#562](https://github.com/usetheokit/theokit-sdk/issues/562))
53
+
54
+ This is documentation, not behaviour. The change shipped in `@theokit/sdk@5.0.0` and is already
55
+ live for anyone on that version; what did not ship is any way for a `@theokit/sdk-memory` consumer
56
+ to find out. Its `0.5.2` section is empty — no dependency lines, no prose — so the release that
57
+ carried a privacy-relevant change reached its most directly affected audience with zero signal.
58
+
59
+ **Why this package's readers are the audience.** `@theokit/sdk-memory` owns the markdown store and
60
+ re-exports `memoryIndexRoot` (`src/internal/store/markdown-store.ts`). Someone reading this
61
+ CHANGELOG to decide whether a version is safe to take is exactly the person the note was written
62
+ for.
63
+
64
+ ## What changed in the behaviour
65
+
66
+ `memory.directory` now moves the search index with the store, except into the Claude Code CLI's own
67
+ directory ([#554](https://github.com/usetheokit/theokit-sdk/issues/554)).
68
+
69
+ The index is not a pointer. `chunks.text` holds the fact TEXT — FTS5/BM25 needs it to search — and
70
+ `files.path` holds the store's absolute path. **A copy of the index is a copy of the memory,
71
+ readable with `strings`.**
72
+
73
+ Leaving it at `<cwd>/.theokit/memory/.index/` while the facts moved meant an operator who pointed
74
+ `directory` at one personal store had that store's contents written into *every repository the
75
+ agent ran in*, untracked and un-ignored. Recall worked throughout, so nothing looked wrong; what
76
+ was wrong was where the data landed.
77
+
78
+ ```
79
+ before /tmp/my-store/fact.md facts move
80
+ /any-project/.theokit/memory/.index/memory.sqlite ← and the content follows here
81
+
82
+ after /tmp/my-store/fact.md
83
+ /tmp/my-store/.index/memory.sqlite ← one store, one index
84
+ ```
85
+
86
+ The Claude Code CLI's directory is unchanged, on its own argument: that CLI has no index format, so
87
+ a binary there is an artefact the partner does not understand inside a directory it owns
88
+ (`docs/memory-decisions.md` § 1). The fix narrows the behaviour to match that argument rather than
89
+ reversing the decision.
90
+
91
+ ## What this does not do
92
+
93
+ **The `0.5.2` section is not edited.** A published entry is a record of what shipped, and rewriting
94
+ it would make the record disagree with the tarball on npm. The note arrives in the next version
95
+ instead, which is later than it should have been and is the honest way to be late.
96
+
97
+ If you are on `0.5.2` the behaviour above is already what you have, through the `@theokit/sdk` you
98
+ resolve — this entry only tells you so.
99
+
3
100
  ## 0.5.2
4
101
 
5
102
  ## 0.5.2-next.1
@@ -21,6 +21,26 @@
21
21
  * @public
22
22
  */
23
23
  import type { MemoryProvider } from "@theokit/sdk";
24
+ /**
25
+ * Options for {@link createInMemoryMarkdownProvider}.
26
+ *
27
+ * @public
28
+ */
29
+ export interface InMemoryMarkdownProviderOptions {
30
+ /**
31
+ * The most facts one handle may hold. Default: no ceiling.
32
+ *
33
+ * Each handle counts only its own facts, so two agents on one provider each get the full
34
+ * ceiling.
35
+ *
36
+ * At the ceiling a new fact is refused on both write paths: the adapter's `write` rejects with a
37
+ * `ConfigurationError` whose code is `memory_ceiling_reached`, and the `memory_remember` tool
38
+ * replies `{ ok: false, code, error }`. Nothing is stored either way, and `delete(id)` frees a
39
+ * slot. A value that is not a positive integer throws `invalid_memory_ceiling` when the provider
40
+ * is created.
41
+ */
42
+ maxFacts?: number;
43
+ }
24
44
  /**
25
45
  * Build a fresh `MemoryProvider` whose facts live in-process. The
26
46
  * returned provider is independent — call `createInMemoryMarkdownProvider()`
@@ -34,5 +54,5 @@ import type { MemoryProvider } from "@theokit/sdk";
34
54
  * Future LanceDB-backed impl will replace the substring match with
35
55
  * semantic-similarity ANN over embedding vectors.
36
56
  */
37
- export declare function createInMemoryMarkdownProvider(): MemoryProvider;
57
+ export declare function createInMemoryMarkdownProvider(options?: InMemoryMarkdownProviderOptions): MemoryProvider;
38
58
  //# sourceMappingURL=in-memory-provider.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"in-memory-provider.d.ts","sourceRoot":"","sources":["../src/in-memory-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAIH,OAAO,KAAK,EASV,cAAc,EAMf,MAAM,cAAc,CAAC;AAyRtB;;;;;;;;;;;;GAYG;AACH,wBAAgB,8BAA8B,IAAI,cAAc,CAoE/D"}
1
+ {"version":3,"file":"in-memory-provider.d.ts","sourceRoot":"","sources":["../src/in-memory-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAIH,OAAO,KAAK,EASV,cAAc,EAMf,MAAM,cAAc,CAAC;AAQtB;;;;GAIG;AACH,MAAM,WAAW,+BAA+B;IAC9C;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAqTD;;;;;;;;;;;;GAYG;AACH,wBAAgB,8BAA8B,CAC5C,OAAO,GAAE,+BAAoC,GAC5C,cAAc,CAsEhB"}
package/dist/index.cjs CHANGED
@@ -3,8 +3,8 @@
3
3
  var promises = require('fs/promises');
4
4
  var path = require('path');
5
5
  var sdk = require('@theokit/sdk');
6
- var persistence = require('@theokit/sdk/persistence');
7
6
  var memoryStore = require('@theokit/sdk/internal/memory-store');
7
+ var persistence = require('@theokit/sdk/persistence');
8
8
  var crypto = require('crypto');
9
9
  var memoryAdapters = require('@theokit/sdk/internal/memory-adapters');
10
10
  var errors = require('@theokit/sdk/errors');
@@ -15,6 +15,7 @@ var module$1 = require('module');
15
15
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
16
16
  // src/in-memory-provider.ts
17
17
  var ADAPTER_ID = "in-memory-md";
18
+ var IN_MEMORY_REMEDY = "merge or delete facts with the adapter's delete(id), or raise maxFacts";
18
19
  var INTERNAL_STATE = /* @__PURE__ */ Symbol("sdk-memory.in-memory.state");
19
20
  function buildCapabilities() {
20
21
  return {
@@ -26,6 +27,19 @@ function buildCapabilities() {
26
27
  prefetch: false
27
28
  };
28
29
  }
30
+ function admitNewFact(state) {
31
+ if (state.maxFacts !== void 0 && state.facts.size >= state.maxFacts) {
32
+ throw memoryStore.ceilingReachedError(
33
+ "the in-memory store",
34
+ state.facts.size,
35
+ state.maxFacts,
36
+ IN_MEMORY_REMEDY
37
+ );
38
+ }
39
+ }
40
+ function isCeilingRefusal(cause) {
41
+ return cause instanceof sdk.ConfigurationError && cause.code === "memory_ceiling_reached";
42
+ }
29
43
  function buildAdapter(state) {
30
44
  return {
31
45
  id: ADAPTER_ID,
@@ -36,6 +50,7 @@ function buildAdapter(state) {
36
50
  async write(content, _ctx) {
37
51
  const text = typeof content === "string" ? content : content.map((m) => m.content).join("\n");
38
52
  const safeText = sdk.Security.redact(text);
53
+ admitNewFact(state);
39
54
  const id = `${ADAPTER_ID}:${state.counter.next++}`;
40
55
  state.facts.set(id, {
41
56
  id,
@@ -181,6 +196,12 @@ function buildMemoryRememberTool(state) {
181
196
  return JSON.stringify({ ok: false, error: "empty content" });
182
197
  }
183
198
  const safeContent = sdk.Security.redact(content);
199
+ try {
200
+ admitNewFact(state);
201
+ } catch (cause) {
202
+ if (!isCeilingRefusal(cause)) throw cause;
203
+ return JSON.stringify({ ok: false, code: cause.code, error: cause.message });
204
+ }
184
205
  const id = `${ADAPTER_ID}:${state.counter.next++}`;
185
206
  state.facts.set(id, {
186
207
  id,
@@ -191,11 +212,13 @@ function buildMemoryRememberTool(state) {
191
212
  }
192
213
  };
193
214
  }
194
- function createInMemoryMarkdownProvider() {
215
+ function createInMemoryMarkdownProvider(options = {}) {
216
+ const maxFacts = memoryStore.resolveMaxFacts(options.maxFacts);
195
217
  return {
196
218
  async init(opts) {
197
219
  const state = {
198
220
  facts: /* @__PURE__ */ new Map(),
221
+ maxFacts,
199
222
  counter: { next: 0 },
200
223
  cwd: opts.cwd
201
224
  };
@@ -1097,12 +1120,15 @@ async function lightPhase(facts, embedding, threshold = DEFAULT_DEDUP_THRESHOLD)
1097
1120
  }
1098
1121
  var DEFAULT_MAX_FACTS_PER_SWEEP = 500;
1099
1122
  async function remPhase(facts, embedding, threshold = DEFAULT_CLUSTER_THRESHOLD, maxFactsPerSweep = DEFAULT_MAX_FACTS_PER_SWEEP) {
1100
- if (facts.length === 0) return { clusters: [] };
1123
+ if (facts.length === 0) return { clusters: [], factsLeftOut: 0 };
1101
1124
  const capped = facts.length > maxFactsPerSweep ? facts.slice(0, maxFactsPerSweep) : facts;
1102
1125
  const vectors = await embedding.embed(capped.map((f) => f.text));
1103
1126
  const clusterOfIdx = unionFindByPairs(vectors, threshold);
1104
1127
  const groups = bucketFactsByClusterRoot(capped, clusterOfIdx);
1105
- return { clusters: [...groups.values()].map(buildClusterFromMembers) };
1128
+ return {
1129
+ clusters: [...groups.values()].map(buildClusterFromMembers),
1130
+ factsLeftOut: facts.length - capped.length
1131
+ };
1106
1132
  }
1107
1133
  function unionFindByPairs(vectors, threshold) {
1108
1134
  const clusterOfIdx = vectors.map((_, i) => i);
@@ -1195,7 +1221,8 @@ async function runInner(options) {
1195
1221
  duplicatesRemoved: dedup.duplicatesRemoved,
1196
1222
  clustersCreated: rem.clusters.length,
1197
1223
  notesWritten,
1198
- diaryEntryHash: void 0
1224
+ diaryEntryHash: void 0,
1225
+ factsLeftOut: rem.factsLeftOut
1199
1226
  };
1200
1227
  await memoryStore.appendDiaryEntry(memoryStore.resolveMemoryRoot(options.cwd), {
1201
1228
  timestampMs,
@@ -1224,7 +1251,7 @@ async function writeConsolidatedNotes(cwd, clusters, timestampMs) {
1224
1251
  return 1;
1225
1252
  }
1226
1253
  function emptyResult(status) {
1227
- return {
1254
+ const result = {
1228
1255
  status,
1229
1256
  factsBefore: 0,
1230
1257
  factsAfter: 0,
@@ -1233,6 +1260,7 @@ function emptyResult(status) {
1233
1260
  notesWritten: 0,
1234
1261
  diaryEntryHash: void 0
1235
1262
  };
1263
+ return status === "skipped" ? { ...result, factsLeftOut: 0 } : result;
1236
1264
  }
1237
1265
 
1238
1266
  // src/internal/index/index-schema.ts
@@ -2276,6 +2304,10 @@ Object.defineProperty(exports, "asMemoryRoot", {
2276
2304
  enumerable: true,
2277
2305
  get: function () { return memoryStore.asMemoryRoot; }
2278
2306
  });
2307
+ Object.defineProperty(exports, "ceilingReachedError", {
2308
+ enumerable: true,
2309
+ get: function () { return memoryStore.ceilingReachedError; }
2310
+ });
2279
2311
  Object.defineProperty(exports, "claudeProjectMemoryDir", {
2280
2312
  enumerable: true,
2281
2313
  get: function () { return memoryStore.claudeProjectMemoryDir; }
@@ -2352,6 +2384,10 @@ Object.defineProperty(exports, "renderDiaryEntry", {
2352
2384
  enumerable: true,
2353
2385
  get: function () { return memoryStore.renderDiaryEntry; }
2354
2386
  });
2387
+ Object.defineProperty(exports, "resolveMaxFacts", {
2388
+ enumerable: true,
2389
+ get: function () { return memoryStore.resolveMaxFacts; }
2390
+ });
2355
2391
  Object.defineProperty(exports, "resolveMemoryRoot", {
2356
2392
  enumerable: true,
2357
2393
  get: function () { return memoryStore.resolveMemoryRoot; }