mirror of
https://github.com/Louis-CFM/coucou.git
synced 2026-12-07 07:32:35 +09:00
The three blocks become one card: Most useful, Most used (week, month, year, all), Never used, Archived. Never used memories can be archived one by one or all at once (after a confirmation); Archived brings them back. brain archive / brain restore: an archived memory leaves the state files (so the hooks, MCP, search index, pages and galaxy no longer see it) and is kept in .state/archived.json. Saving a state leaves archived ids out, so a later ingestion can't bring one back. Nothing is deleted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LJm3SRL9k3gBPaVtfPhwcT
97 lines
5.0 KiB
TypeScript
97 lines
5.0 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { spawnSync } from "node:child_process";
|
|
import type { BrainConfig } from "../src/config.js";
|
|
import { parseHooksLog, readUsage, recordUsage, usagePath, usageReport } from "../src/usage.js";
|
|
|
|
let dir: string;
|
|
let config: BrainConfig;
|
|
const NOW = Date.parse("2026-11-28T12:00:00Z");
|
|
|
|
function el(id: string, kind: string, texte: string, date: string, extra: Record<string, unknown> = {}) {
|
|
return { id, kind, texte, date, sources: [], projets: [], ...extra };
|
|
}
|
|
|
|
function seedProject(id: string, elements: unknown[]) {
|
|
const p = path.join(dir, ".state", "proj", `${id}.json`);
|
|
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
fs.writeFileSync(p, JSON.stringify({ id, title: id, elements }));
|
|
}
|
|
|
|
beforeEach(() => {
|
|
dir = fs.mkdtempSync(path.join(os.tmpdir(), "usage"));
|
|
config = {
|
|
brainPath: dir, claudeProjectsPath: path.join(dir, "none"), repoPaths: [],
|
|
chunkModel: "sonnet", mergeModel: "opus", maxChunkTokens: 100000, language: "fr", repoAliases: {},
|
|
};
|
|
seedProject("proj_coucou", [
|
|
el("dec_a", "décision", "Une commande par bloc", "2026-10-01"),
|
|
el("fait_b", "fait", "Le notch est en SwiftUI", "2026-10-02"),
|
|
el("les_c", "leçon", "Jamais réécrire settings.json", "2026-09-01"),
|
|
el("fait_new", "fait", "Tout neuf", "2026-11-27"),
|
|
el("mes_x", "mesure", "CA 2 000 €", "2026-09-01", { financial: true }),
|
|
]);
|
|
seedProject("louis", [el("pref_d", "préférence", "Pas de rouge", "2026-08-01")]);
|
|
});
|
|
afterEach(() => fs.rmSync(dir, { recursive: true, force: true }));
|
|
|
|
describe("usage", () => {
|
|
it("records, then ranks used, useful and never used memories", () => {
|
|
recordUsage(config, { via: "session", ids: ["dec_a", "pref_d"], session: "s1", t: "2026-11-20T10:00:00Z" });
|
|
recordUsage(config, { via: "prompt", ids: ["dec_a"], session: "s1", t: "2026-11-20T10:05:00Z" });
|
|
recordUsage(config, { via: "session", ids: ["dec_a"], session: "s2", t: "2026-11-25T10:00:00Z" });
|
|
recordUsage(config, { via: "mcp", tool: "brain_search", ids: ["fait_b"], session: "m1", t: "2026-11-26T10:00:00Z" });
|
|
recordUsage(config, { via: "mcp", tool: "brain_rules", ids: ["pref_d"], session: "m1", t: "2026-11-26T10:01:00Z" });
|
|
recordUsage(config, { via: "prompt", ids: ["unknown_id"], session: "s3" }); // gone from memory: ignored
|
|
|
|
const r = usageReport(config, { now: NOW });
|
|
expect(r.memories).toBe(5); // the financial mesure is left out
|
|
expect(r.used).toBe(3);
|
|
expect(r.top.all[0]).toMatchObject({ id: "dec_a", uses: 3, sessions: 2 });
|
|
expect(r.useful.map(u => u.id)).toEqual(["fait_b", "dec_a", "pref_d"]); // a search counts double, rules don't
|
|
expect(r.useful.find(u => u.id === "pref_d")!.picks).toBe(0);
|
|
expect(r.unused.map(u => u.id)).toEqual(["les_c"]); // the memory of yesterday is too young to judge
|
|
expect(r.unusedCount).toBe(1);
|
|
expect(r.uses30).toBe(6);
|
|
expect(r.picks30).toBe(1);
|
|
expect(r.sessions30).toBe(3);
|
|
expect(r.days).toHaveLength(30);
|
|
expect(r.days.find(d => d.date === "2026-11-20")!.n).toBe(3);
|
|
expect(r.projects[0]).toEqual({ name: "coucou", n: 4 });
|
|
});
|
|
|
|
it("never records an empty list and never throws", () => {
|
|
recordUsage(config, { via: "prompt", ids: [] });
|
|
expect(fs.existsSync(usagePath(config))).toBe(false);
|
|
recordUsage({ ...config, brainPath: "/dev/null/nope" }, { via: "prompt", ids: ["x"] });
|
|
});
|
|
|
|
it("reads the prompt hook's old lines from hooks.log, only before the usage log starts", () => {
|
|
const log = [
|
|
`2026-11-27T01:00:00Z [prompt] INFO injected | {"project":"coucou","session":"old","ids":["fait_b"],"tokens":10}`,
|
|
`2026-11-27T01:01:00Z [prompt] INFO no sig tokens | {"prompt":"x"}`,
|
|
`2026-11-27T01:02:00Z [session-start] INFO injected | {"project":"coucou","session":"old","tokens":10}`,
|
|
`2026-11-28T09:00:00Z [prompt] INFO injected | {"project":"coucou","session":"new","ids":["dec_a"]}`,
|
|
].join("\n");
|
|
expect(parseHooksLog(log, null).map(e => e.ids[0])).toEqual(["fait_b", "dec_a"]);
|
|
const logs = path.join(dir, ".state", "logs");
|
|
fs.mkdirSync(logs, { recursive: true });
|
|
fs.writeFileSync(path.join(logs, "hooks.log"), log);
|
|
recordUsage(config, { via: "prompt", ids: ["dec_a"], session: "new", t: "2026-11-28T09:00:00Z" });
|
|
const events = readUsage(config);
|
|
expect(events.map(e => e.ids[0])).toEqual(["fait_b", "dec_a"]); // not counted twice
|
|
});
|
|
|
|
it("the prompt hook writes what it injected", () => {
|
|
const hook = path.join(__dirname, "..", "hooks", "brain-prompt.js");
|
|
expect(fs.readFileSync(hook, "utf8")).toContain("usage.jsonl");
|
|
const start = path.join(__dirname, "..", "hooks", "brain-session-start.js");
|
|
const src = fs.readFileSync(start, "utf8");
|
|
expect(src).toContain("recordUsage(usedIds");
|
|
// syntax check of both hooks
|
|
for (const f of [hook, start]) expect(spawnSync(process.execPath, ["--check", f]).status).toBe(0);
|
|
});
|
|
});
|