dsh-mnemon-source-runtime 0.5.11 → 0.5.12

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 (2) hide show
  1. package/lib/index.js +23 -14
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -267,12 +267,14 @@ function prepareMutation(before, request, now) {
267
267
  }
268
268
  function compactionCandidates(compacted, existing, target, now) {
269
269
  const seen = /* @__PURE__ */ new Set();
270
+ const existingByContent = /* @__PURE__ */ new Map();
271
+ for (const entry of existing) if (!existingByContent.has(entry.content)) existingByContent.set(entry.content, entry);
270
272
  return compacted.map((entry) => {
271
273
  const content = normalizeContent(entry.content, "compacted content");
272
274
  if (!isImportance(entry.importance)) throw new Error("compacted importance must be critical, normal, or low");
273
275
  if (seen.has(content)) throw new Error("compacted runtime memory contains duplicate entries");
274
276
  seen.add(content);
275
- const unchanged = existing.find((current) => current.content === content);
277
+ const unchanged = existingByContent.get(content);
276
278
  const inheritedBranches = entry.branches ?? unchanged?.branches;
277
279
  return {
278
280
  content,
@@ -285,21 +287,28 @@ function compactionCandidates(compacted, existing, target, now) {
285
287
  });
286
288
  }
287
289
  function packCompactionCandidates(replacements, target, maxBytes) {
288
- const priority = {
289
- critical: 0,
290
- normal: 1,
291
- low: 2
290
+ const ranked = {
291
+ critical: [],
292
+ normal: [],
293
+ low: []
292
294
  };
293
- const ranked = replacements.map((entry, index) => ({
294
- entry,
295
- index
296
- })).sort((left, right) => priority[left.entry.importance] - priority[right.entry.importance] || left.index - right.index);
295
+ replacements.forEach((entry, index) => ranked[entry.importance].push(index));
297
296
  const selected = /* @__PURE__ */ new Set();
298
- const packed = [];
299
- for (const candidate of ranked) {
300
- if (byteCount([...packed, candidate.entry], target) > maxBytes) continue;
301
- packed.push(candidate.entry);
302
- selected.add(candidate.index);
297
+ const delimiterBytes = Buffer.byteLength(RUNTIME_ENTRY_DELIMITER, "utf8");
298
+ let used = 0;
299
+ let count = 0;
300
+ for (const importance of [
301
+ "critical",
302
+ "normal",
303
+ "low"
304
+ ]) for (const index of ranked[importance]) {
305
+ const entry = replacements[index];
306
+ const matchesTarget = entry.target === target;
307
+ const added = matchesTarget ? Buffer.byteLength(entry.content, "utf8") + (count === 0 ? 0 : delimiterBytes) : 0;
308
+ if (used + added > maxBytes) continue;
309
+ used += added;
310
+ if (matchesTarget) count += 1;
311
+ selected.add(index);
303
312
  }
304
313
  return replacements.filter((_, index) => selected.has(index));
305
314
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mnemon-source-runtime",
3
- "version": "0.5.11",
3
+ "version": "0.5.12",
4
4
  "description": "Self-contained runtime Source for Composable View Memory.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -60,7 +60,7 @@
60
60
  "@types/node": "^22.0.0",
61
61
  "@types/react": "^18.3.0",
62
62
  "@types/react-dom": "^18.3.0",
63
- "dsh-mnemon": "^0.5.19",
63
+ "dsh-mnemon": "^0.5.23",
64
64
  "jsdom": "^26.0.0",
65
65
  "react": "^18.2.0",
66
66
  "react-dom": "^18.2.0",