Skip to content
Merged
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
4 changes: 3 additions & 1 deletion packages/person-memory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"devDependencies": {
"@koishijs/plugin-database-memory": "^3.7.0",
"@koishijs/plugin-mock": "^2.6.6",
"koishi": "^4.18.7"
"@minatojs/driver-sqlite": "^4.7.0",
"koishi": "^4.18.7",
"minato": "^3.7.0"
},
"peerDependencies": {
"koishi": "^4.18.7",
Expand Down
6 changes: 5 additions & 1 deletion packages/person-memory/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,11 @@ export class PersonStore {
try {
const old = await this.db.get(SOURCE, { scope }, { sort: { timestamp: "desc", key: "desc" }, offset: 200, limit: 2000 });
const expired = old.filter((row) => row.timestamp < Date.now() - 7 * 86400000);
if (expired.length) await this.db.remove(SOURCE, { key: { $in: expired.map((r) => r.key) } });
// Bound SQL text: the WASM SQLite driver copies prepared SQL onto a small stack.
for (let offset = 0; offset < expired.length; offset += 128) {
if (!active() || this.options.active?.() === false) break;
await this.db.remove(SOURCE, { scope, key: { $in: expired.slice(offset, offset + 128).map((row) => row.key) } });
}
} catch {
/* Retry cleanup on the next captured source. */
}
Expand Down
65 changes: 65 additions & 0 deletions packages/person-memory/tests/source-retention-batches.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { createHash } from "node:crypto";

import { expect, it, vi } from "vitest";

import { PersonStore, registerModels } from "../src/store";
import { fixture } from "./helpers";

const table = "person_memory.sources";
const key = (id: string) => createHash("sha256").update(id).digest("hex");
const oldTime = Date.now() - 8 * 86400000;
async function seed(t, count = 2200) {
const ctx = await fixture(t, registerModels);
await ctx.database.upsert(
table,
Array.from({ length: count }, (_, i) => ({
key: key(String(i)),
scope: "mind:a",
userId: "u",
timestamp: oldTime + i,
source: { id: String(i), userId: "u", name: "u", text: "old", timestamp: oldTime + i },
})),
);
await ctx.database.create(table, {
key: key("other"),
scope: "mind:b",
userId: "u",
timestamp: oldTime,
source: { id: "other", userId: "u", name: "u", text: "other", timestamp: oldTime },
});
return ctx;
}
const fresh = { id: "fresh", userId: "u", name: "u", text: "new", timestamp: Date.now() };

it("cleans a full expired page with bounded queries while preserving the newest sources and other domains", async (t) => {
const ctx = await seed(t);
const remove = vi.spyOn(ctx.database, "remove");
await new PersonStore(ctx.database, "review").capture("mind:a", fresh);
const calls = remove.mock.calls.filter(([name]) => name === table);
expect(calls.length).toBeGreaterThan(1);
for (const [, query] of calls) {
expect(query.scope).toBe("mind:a");
expect(query.key.$in.length).toBeLessThanOrEqual(128);
}
const rows = await ctx.database.get(table, { scope: "mind:a" });
expect(rows).toHaveLength(201);
expect(rows.some((row) => row.source.id === "fresh")).toBe(true);
for (let i = 2001; i < 2200; i++) expect(rows.some((row) => row.source.id === String(i))).toBe(true);
expect(await ctx.database.get(table, { scope: "mind:b" })).toHaveLength(1);
});

it("stops deleting between cleanup batches after the capture is cancelled", async (t) => {
const ctx = await seed(t, 500);
let active = true;
const original = ctx.database.remove.bind(ctx.database);
const remove = vi.spyOn(ctx.database, "remove").mockImplementation(async (...args) => {
const result = await original(...args);
active = false;
return result;
});
await new PersonStore(ctx.database, "review").capture("mind:a", fresh, () => active);
expect(remove).toHaveBeenCalledTimes(1);
const rows = await ctx.database.get(table, { scope: "mind:a" });
expect(rows.length).toBeGreaterThan(200);
expect(rows.some((row) => row.source.id === "fresh")).toBe(true);
});
27 changes: 27 additions & 0 deletions packages/person-memory/tests/sqlite-source-retention.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createHash } from "node:crypto";

import { SQLiteDriver } from "@minatojs/driver-sqlite";
import { Context } from "koishi";
import { expect, it } from "vitest";

import { PersonStore, registerModels } from "../src/store";
it("retention leaves the actual WASM SQLite driver usable after 2000 expired sources", async (t) => {
const ctx = new Context();
ctx.plugin(SQLiteDriver, { path: ":memory:" });
registerModels(ctx);
await ctx.start();
await ctx.database.prepared();
t.onTestFinished(() => ctx.stop());
const timestamp = Date.now() - 8 * 86400000;
for (let i = 0; i < 2200; i++)
await ctx.database.create("person_memory.sources", {
key: createHash("sha256").update(String(i)).digest("hex"),
scope: "mind:a",
userId: "u",
timestamp: timestamp + i,
source: { id: String(i), userId: "u", name: "u", text: "old", timestamp: timestamp + i },
});
await new PersonStore(ctx.database, "review").capture("mind:a", { id: "fresh", userId: "u", name: "u", text: "new", timestamp: Date.now() });
expect(await ctx.database.get("person_memory.sources", { scope: "mind:a" })).toHaveLength(201);
expect((await ctx.database.get("person_memory.sources", { scope: "mind:a" })).some((row) => row.source.id === "fresh")).toBe(true);
});
33 changes: 33 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,37 @@ __metadata:
languageName: node
linkType: hard

"@minatojs/driver-sqlite@npm:^4.7.0":
version: 4.7.0
resolution: "@minatojs/driver-sqlite@npm:4.7.0"
dependencies:
"@minatojs/sql-utils": "npm:^5.6.0"
"@minatojs/sql.js": "npm:^3.1.0"
cosmokit: "npm:^1.6.3"
peerDependencies:
minato: ^3.7.0
checksum: 10c0/ef1059ba9a921e1c5739027f74f2e4b661755f11e9649138c14cfe55fc25de884e26fb204a9fac1dafa5b64a4a6f6f2606c680601dc41fc5e6f2b3b59f459b2a
languageName: node
linkType: hard

"@minatojs/sql-utils@npm:^5.6.0":
version: 5.6.0
resolution: "@minatojs/sql-utils@npm:5.6.0"
dependencies:
cosmokit: "npm:^1.6.3"
peerDependencies:
minato: ^3.7.0
checksum: 10c0/7b97a6ce110e74d2b1d906bf34b79c5fcd784de49da7fb7172595ae695a694db2b7d6e54d224dd17256cc30cf58a669d041bfe37ea365148d54a50b68a247962
languageName: node
linkType: hard

"@minatojs/sql.js@npm:^3.1.0":
version: 3.1.0
resolution: "@minatojs/sql.js@npm:3.1.0"
checksum: 10c0/3a61cd868d848a008d89ccdf27411a392d690831b6c5508dd04d84bf002bbfb2e1934e8979b0f6c14ab16523b708bff2fdb7004e564dbe186d7812f1c383f066
languageName: node
linkType: hard

"@modelcontextprotocol/sdk@npm:^1.13.0":
version: 1.32.1
resolution: "@modelcontextprotocol/sdk@npm:1.32.1"
Expand Down Expand Up @@ -6157,7 +6188,9 @@ __metadata:
dependencies:
"@koishijs/plugin-database-memory": "npm:^3.7.0"
"@koishijs/plugin-mock": "npm:^2.6.6"
"@minatojs/driver-sqlite": "npm:^4.7.0"
koishi: "npm:^4.18.7"
minato: "npm:^3.7.0"
peerDependencies:
koishi: ^4.18.7
koishi-plugin-yesimbot: ^3.0.4
Expand Down
Loading