Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions src/functions/reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createGraphIndexReader } from "../state/graph-retrieval-index.js";
import { lookupObservationSession } from "../state/obs-index.js";
import type { IIIClient } from "iii-sdk";
import type { StateKV } from "../state/kv.js";
import { collectAll } from "../state/kv.js";
import { collectAll, compareInsertionOrder, insertionOrderKey } from "../state/kv.js";
import { KV, fingerprintId } from "../state/schema.js";
import type {
Insight,
Expand Down Expand Up @@ -570,19 +570,40 @@ export function registerReflectFunctions(
if (limit === null) return invalidOption("limit must be a positive safe integer");
if (!validConfidence(data?.minConfidence)) return invalidOption("minConfidence must be a finite number between 0 and 1");
const minConfidence = data?.minConfidence ?? 0;
let items = await collectAll<Insight>(kv, KV.insights);

items = items.filter(
(i) => !i.deleted && i.confidence >= minConfidence,
);

if (data?.project) {
items = items.filter((i) => i.project === data.project);
const paged = typeof kv.iterate === "function";
const redisOrder = paged && kv.backend === "redis";
type Entry = { insight: Insight; order: ReturnType<typeof insertionOrderKey> };
const items: Entry[] = [];
const compare = (a: Entry, b: Entry) =>
b.insight.confidence - a.insight.confidence ||
(redisOrder ? compareInsertionOrder(a.order, b.order) : 0);
const consider = (insight: Insight) => {
if (insight.deleted || !(insight.confidence >= minConfidence)) return;
if (data?.project && insight.project !== data.project) return;
const entry: Entry = {
insight,
order: redisOrder ? insertionOrderKey(insight) : { at: 0, id: "" },
};
if (items.length === limit && compare(entry, items[items.length - 1]!) >= 0) return;
let low = 0;
let high = items.length;
while (low < high) {
const middle = (low + high) >>> 1;
if (compare(entry, items[middle]!) < 0) high = middle;
else low = middle + 1;
}
items.splice(low, 0, entry);
if (items.length > limit) items.pop();
};
if (paged) {
for await (const page of kv.iterate<Insight>(KV.insights)) {
for (const insight of page) consider(insight);
}
} else {
for (const insight of await collectAll<Insight>(kv, KV.insights)) consider(insight);
}

items.sort((a, b) => b.confidence - a.confidence);

const result = { success: true, insights: items.slice(0, limit) };
const result = { success: true, insights: items.map((entry) => entry.insight) };
return checkPayloadFrameSize(result, "reduce limit or filter by project") ?? result;
},
);
Expand Down
21 changes: 15 additions & 6 deletions src/state/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,23 @@ function idKey(value: unknown): string {
return typeof id === 'string' ? id : typeof id === 'number' ? String(id) : ''
}

export function insertionOrderKey(value: unknown): { at: number; id: string } {
return { at: orderKey(value), id: idKey(value) }
}

export function compareInsertionOrder(
a: ReturnType<typeof insertionOrderKey>,
b: ReturnType<typeof insertionOrderKey>,
): number {
if (a.at !== b.at) return a.at < b.at ? -1 : 1
if (a.id === b.id) return 0
return a.id < b.id ? -1 : 1
}

export function orderLikeInsertion<T>(values: T[]): T[] {
return values
.map((value) => ({ value, at: orderKey(value), id: idKey(value) }))
.sort((a, b) => {
if (a.at !== b.at) return a.at < b.at ? -1 : 1
if (a.id === b.id) return 0
return a.id < b.id ? -1 : 1
})
.map((value) => ({ value, ...insertionOrderKey(value) }))
.sort(compareInsertionOrder)
.map((entry) => entry.value)
}

Expand Down
94 changes: 94 additions & 0 deletions test/fixtures/insight-list-memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { strict as assert } from "node:assert";
import { createHash } from "node:crypto";
import { registerReflectFunctions } from "../../src/functions/reflect.js";
import { StateKV } from "../../src/state/kv.js";
import { KV } from "../../src/state/schema.js";

const backend = process.argv[2];
assert(backend === "file" || backend === "redis");
assert.equal(typeof global.gc, "function", "Run this probe with --expose-gc");

const count = 4096;
const functions = new Map<string, (data: unknown) => unknown>();
let reads = 0;
let peakHeapBytes = 0;
let modelRequests = 0;

function sampleHeap() {
global.gc!();
peakHeapBytes = Math.max(peakHeapBytes, process.memoryUsage().heapUsed);
}

function record(index: number) {
const bytes = Buffer.alloc(12 * 1024, index % 251);
bytes.writeUInt32LE(index);
const concept = bytes.toString("base64");
return JSON.parse(JSON.stringify({
id: `historical-${index.toString().padStart(5, "0")}`,
title: `Historical insight ${index}`,
content: `Complete insight ${index}; Unicode Ж and é remain intact.`,
confidence: (index + 1) / count,
reinforcements: 3,
sourceConceptCluster: [concept],
sourceMemoryIds: [`memory-${index}`],
sourceLessonIds: [],
sourceCrystalIds: [],
tags: [concept],
createdAt: "2026-01-01T00:00:00.000Z",
updatedAt: "2026-01-01T00:00:00.000Z",
decayRate: 0.05,
project: "large-store",
legacy_extra: { index, values: [0, false, null] },
}));
}

const sdk = {
registerFunction: (id: string | { id: string }, handler: (data: unknown) => unknown) => {
functions.set(typeof id === "string" ? id : id.id, handler);
},
trigger: async ({ function_id, payload }: {
function_id: string;
payload: { scope: string; key?: string };
}) => {
assert.equal(payload.scope, KV.insights);
if (function_id === "state::list_keys") {
return Array.from({ length: count }, (_, index) => String(index));
}
assert.equal(function_id, "state::get");
const index = Number(payload.key);
assert(Number.isInteger(index) && index >= 0 && index < count);
// Sample while the handler still owns earlier pages, before the next read.
if (reads % 200 === 0) sampleHeap();
reads++;
return record(index);
},
};

const unexpectedModel = async () => {
modelRequests++;
throw new Error("Listing insights must not request a model");
};
const kv = new StateKV(sdk as never, { backend });
registerReflectFunctions(sdk as never, kv, {
name: "no-model",
compress: unexpectedModel,
summarize: unexpectedModel,
} as never);

sampleHeap();
const baselineHeapBytes = peakHeapBytes;
const result = await functions.get("mem::insight-list")!({ limit: 1, project: "large-store" });
sampleHeap();
assert.deepEqual(result, { success: true, insights: [record(count - 1)] });
assert.equal(reads, count);
assert.equal(modelRequests, 0);
console.log(JSON.stringify({
backend,
records: count,
reads,
modelRequests,
baselineHeapBytes,
peakHeapBytes,
heapGrowthBytes: peakHeapBytes - baselineHeapBytes,
completeResultSha256: createHash("sha256").update(JSON.stringify(result)).digest("hex"),
}));
Loading