@joshuaswarren/openclaw-engram 9.1.24 → 9.1.25

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.
@@ -3,10 +3,10 @@ import {
3
3
  EngramAccessService,
4
4
  Orchestrator,
5
5
  parseConfig
6
- } from "./chunk-XKT3IT22.js";
7
- import "./chunk-PIQWOQEX.js";
6
+ } from "./chunk-JUQ752QV.js";
7
+ import "./chunk-MNBCF5B2.js";
8
8
  import "./chunk-IMMYYNXG.js";
9
- import "./chunk-Y6DMH5LN.js";
9
+ import "./chunk-IADQJS2J.js";
10
10
  import "./chunk-6KX4XLQJ.js";
11
11
  import "./chunk-4722S3A6.js";
12
12
  import "./chunk-MKM2BCQH.js";
@@ -2227,7 +2227,13 @@ ${sanitized.text}
2227
2227
  invalidateAllMemoriesCache() {
2228
2228
  _StorageManager.allMemoriesInFlight.delete(this.baseDir);
2229
2229
  }
2230
- async _readAllMemoriesFromDisk() {
2230
+ normalizeMemoryReadBatchSize(batchSize) {
2231
+ if (typeof batchSize !== "number" || !Number.isFinite(batchSize)) {
2232
+ return 50;
2233
+ }
2234
+ return Math.max(1, Math.floor(batchSize));
2235
+ }
2236
+ async collectActiveMemoryPaths() {
2231
2237
  const filePaths = [];
2232
2238
  const collectPaths = async (dir) => {
2233
2239
  try {
@@ -2249,11 +2255,14 @@ ${sanitized.text}
2249
2255
  };
2250
2256
  await collectPaths(this.factsDir);
2251
2257
  await collectPaths(this.correctionsDir);
2258
+ return filePaths;
2259
+ }
2260
+ async readParsedMemoriesFromPaths(filePaths, batchSize) {
2252
2261
  if (filePaths.length === 0) return [];
2253
- const BATCH_SIZE = 50;
2262
+ const normalizedBatchSize = this.normalizeMemoryReadBatchSize(batchSize);
2254
2263
  const memories = [];
2255
- for (let i = 0; i < filePaths.length; i += BATCH_SIZE) {
2256
- const batch = filePaths.slice(i, i + BATCH_SIZE);
2264
+ for (let i = 0; i < filePaths.length; i += normalizedBatchSize) {
2265
+ const batch = filePaths.slice(i, i + normalizedBatchSize);
2257
2266
  const results = await Promise.all(
2258
2267
  batch.map(async (fullPath) => {
2259
2268
  try {
@@ -2273,12 +2282,126 @@ ${sanitized.text}
2273
2282
  }
2274
2283
  })
2275
2284
  );
2276
- for (const m of results) {
2277
- if (m !== null) memories.push(m);
2285
+ for (const memory of results) {
2286
+ if (memory !== null) memories.push(memory);
2278
2287
  }
2279
2288
  }
2280
2289
  return memories;
2281
2290
  }
2291
+ async readWindowUpdatedMs(filePath) {
2292
+ try {
2293
+ const raw = await readFile2(filePath, "utf-8");
2294
+ const match = raw.match(/^---\n([\s\S]*?)\n---\n?/);
2295
+ if (!match) return null;
2296
+ const frontmatterBlock = match[1];
2297
+ const rawUpdated = frontmatterBlock.match(/^updated:\s*"?([^"\n]*)"?/m)?.[1] ?? frontmatterBlock.match(/^created:\s*"?([^"\n]*)"?/m)?.[1] ?? null;
2298
+ const updatedMs = rawUpdated ? Date.parse(rawUpdated) : Number.NaN;
2299
+ return Number.isFinite(updatedMs) ? updatedMs : null;
2300
+ } catch {
2301
+ return null;
2302
+ }
2303
+ }
2304
+ async filterWindowPathsByUpdatedAfter(filePaths, updatedAfterMs) {
2305
+ const results = await Promise.all(filePaths.map(async (filePath) => {
2306
+ const updatedMs = await this.readWindowUpdatedMs(filePath);
2307
+ if (updatedMs !== null) {
2308
+ return updatedMs >= updatedAfterMs ? filePath : null;
2309
+ }
2310
+ try {
2311
+ const fileStat = await stat2(filePath);
2312
+ return fileStat.mtimeMs >= updatedAfterMs ? filePath : null;
2313
+ } catch {
2314
+ return filePath;
2315
+ }
2316
+ }));
2317
+ return results.filter((filePath) => filePath !== null);
2318
+ }
2319
+ orderWindowPaths(filePaths) {
2320
+ const correctionPaths = [];
2321
+ const factPaths = [];
2322
+ for (const filePath of filePaths) {
2323
+ if (filePath === this.correctionsDir || filePath.startsWith(`${this.correctionsDir}${path4.sep}`)) {
2324
+ correctionPaths.push(filePath);
2325
+ } else {
2326
+ factPaths.push(filePath);
2327
+ }
2328
+ }
2329
+ correctionPaths.sort((left, right) => right.localeCompare(left));
2330
+ factPaths.sort((left, right) => right.localeCompare(left));
2331
+ if (correctionPaths.length === 0) return factPaths;
2332
+ if (factPaths.length === 0) return correctionPaths;
2333
+ const ordered = [];
2334
+ const maxLength = Math.max(correctionPaths.length, factPaths.length);
2335
+ for (let i = 0; i < maxLength; i += 1) {
2336
+ const correctionPath = correctionPaths[i];
2337
+ if (correctionPath) ordered.push(correctionPath);
2338
+ const factPath = factPaths[i];
2339
+ if (factPath) ordered.push(factPath);
2340
+ }
2341
+ return ordered;
2342
+ }
2343
+ async readWindowBoundedBatch(candidateBatchPaths, remainingSlots, remainingInspectionBudget, readBatchSize) {
2344
+ const memories = [];
2345
+ const filePaths = [];
2346
+ const normalizedReadBatchSize = this.normalizeMemoryReadBatchSize(readBatchSize);
2347
+ for (let index = 0; index < candidateBatchPaths.length; ) {
2348
+ if (memories.length >= remainingSlots || filePaths.length >= remainingInspectionBudget) break;
2349
+ const availableSlots = remainingSlots - memories.length;
2350
+ const availableInspectionBudget = remainingInspectionBudget - filePaths.length;
2351
+ const parallelWindow = availableSlots >= 4 && availableInspectionBudget >= 4 ? Math.min(normalizedReadBatchSize, 4) : 1;
2352
+ const candidatePaths = candidateBatchPaths.slice(
2353
+ index,
2354
+ index + Math.min(parallelWindow, availableInspectionBudget)
2355
+ );
2356
+ index += candidatePaths.length;
2357
+ if (candidatePaths.length === 0) break;
2358
+ filePaths.push(...candidatePaths);
2359
+ const parsedMemories = await this.readParsedMemoriesFromPaths(candidatePaths, candidatePaths.length);
2360
+ if (parsedMemories.length === 0) continue;
2361
+ memories.push(...parsedMemories.slice(0, availableSlots));
2362
+ }
2363
+ return { memories, filePaths };
2364
+ }
2365
+ async readMemoriesWindow(options = {}) {
2366
+ const allPaths = await this.collectActiveMemoryPaths();
2367
+ const sortedPaths = this.orderWindowPaths(allPaths);
2368
+ const maxMemories = typeof options.maxMemories === "number" && Number.isFinite(options.maxMemories) ? Math.max(1, Math.floor(options.maxMemories)) : void 0;
2369
+ const maxCandidatePaths = maxMemories === void 0 ? void 0 : maxMemories * 2;
2370
+ const updatedAfterMs = options.updatedAfter?.getTime();
2371
+ const normalizedBatchSize = this.normalizeMemoryReadBatchSize(options.batchSize);
2372
+ const memories = [];
2373
+ const selectedPaths = [];
2374
+ for (let i = 0; i < sortedPaths.length; i += normalizedBatchSize) {
2375
+ if (maxMemories !== void 0 && (memories.length >= maxMemories || maxCandidatePaths !== void 0 && selectedPaths.length >= maxCandidatePaths)) {
2376
+ return { memories, filePaths: selectedPaths };
2377
+ }
2378
+ const batchPaths = sortedPaths.slice(i, i + normalizedBatchSize);
2379
+ const candidateBatchPaths = updatedAfterMs === void 0 ? batchPaths : await this.filterWindowPathsByUpdatedAfter(batchPaths, updatedAfterMs);
2380
+ const remainingSlots = maxMemories === void 0 ? void 0 : Math.max(0, maxMemories - memories.length);
2381
+ const remainingInspectionBudget = maxCandidatePaths === void 0 ? void 0 : Math.max(0, maxCandidatePaths - selectedPaths.length);
2382
+ const { memories: batchMemories, filePaths: parsedCandidatePaths } = remainingSlots === void 0 ? {
2383
+ memories: await this.readParsedMemoriesFromPaths(candidateBatchPaths, normalizedBatchSize),
2384
+ filePaths: candidateBatchPaths
2385
+ } : await this.readWindowBoundedBatch(
2386
+ candidateBatchPaths,
2387
+ remainingSlots,
2388
+ remainingInspectionBudget ?? remainingSlots,
2389
+ normalizedBatchSize
2390
+ );
2391
+ selectedPaths.push(...parsedCandidatePaths);
2392
+ for (const memory of batchMemories) {
2393
+ memories.push(memory);
2394
+ if (maxMemories !== void 0 && memories.length >= maxMemories) {
2395
+ return { memories, filePaths: selectedPaths };
2396
+ }
2397
+ }
2398
+ }
2399
+ return { memories, filePaths: selectedPaths };
2400
+ }
2401
+ async _readAllMemoriesFromDisk() {
2402
+ const filePaths = await this.collectActiveMemoryPaths();
2403
+ return this.readParsedMemoriesFromPaths(filePaths, 50);
2404
+ }
2282
2405
  /**
2283
2406
  * Read archived memory markdown files under archive/.
2284
2407
  * Used by long-term recall fallback when hot recall has no hits.
@@ -4088,4 +4211,4 @@ export {
4088
4211
  serializeEntityFile,
4089
4212
  StorageManager
4090
4213
  };
4091
- //# sourceMappingURL=chunk-Y6DMH5LN.js.map
4214
+ //# sourceMappingURL=chunk-IADQJS2J.js.map