@agentmemory/agentmemory 0.7.0
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.
- package/.claude-plugin/marketplace.json +14 -0
- package/.github/workflows/ci.yml +22 -0
- package/.github/workflows/publish.yml +28 -0
- package/AGENTS.md +113 -0
- package/LICENSE +190 -0
- package/README.md +828 -0
- package/assets/banner.png +0 -0
- package/assets/demo.gif +0 -0
- package/assets/demo.mp4 +0 -0
- package/benchmark/QUALITY.md +73 -0
- package/benchmark/REAL-EMBEDDINGS.md +67 -0
- package/benchmark/SCALE.md +110 -0
- package/benchmark/dataset.ts +293 -0
- package/benchmark/quality-eval.ts +643 -0
- package/benchmark/real-embeddings-eval.ts +405 -0
- package/benchmark/scale-eval.ts +398 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +137 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/docker-compose.yml +14 -0
- package/dist/hooks/notification.d.mts +1 -0
- package/dist/hooks/notification.mjs +45 -0
- package/dist/hooks/notification.mjs.map +1 -0
- package/dist/hooks/post-tool-failure.d.mts +1 -0
- package/dist/hooks/post-tool-failure.mjs +45 -0
- package/dist/hooks/post-tool-failure.mjs.map +1 -0
- package/dist/hooks/post-tool-use.d.mts +1 -0
- package/dist/hooks/post-tool-use.mjs +53 -0
- package/dist/hooks/post-tool-use.mjs.map +1 -0
- package/dist/hooks/pre-compact.d.mts +1 -0
- package/dist/hooks/pre-compact.mjs +50 -0
- package/dist/hooks/pre-compact.mjs.map +1 -0
- package/dist/hooks/pre-tool-use.d.mts +1 -0
- package/dist/hooks/pre-tool-use.mjs +69 -0
- package/dist/hooks/pre-tool-use.mjs.map +1 -0
- package/dist/hooks/prompt-submit.d.mts +1 -0
- package/dist/hooks/prompt-submit.mjs +40 -0
- package/dist/hooks/prompt-submit.mjs.map +1 -0
- package/dist/hooks/session-end.d.mts +1 -0
- package/dist/hooks/session-end.mjs +61 -0
- package/dist/hooks/session-end.mjs.map +1 -0
- package/dist/hooks/session-start.d.mts +1 -0
- package/dist/hooks/session-start.mjs +42 -0
- package/dist/hooks/session-start.mjs.map +1 -0
- package/dist/hooks/stop.d.mts +1 -0
- package/dist/hooks/stop.mjs +33 -0
- package/dist/hooks/stop.mjs.map +1 -0
- package/dist/hooks/subagent-start.d.mts +1 -0
- package/dist/hooks/subagent-start.mjs +43 -0
- package/dist/hooks/subagent-start.mjs.map +1 -0
- package/dist/hooks/subagent-stop.d.mts +1 -0
- package/dist/hooks/subagent-stop.mjs +45 -0
- package/dist/hooks/subagent-stop.mjs.map +1 -0
- package/dist/hooks/task-completed.d.mts +1 -0
- package/dist/hooks/task-completed.mjs +46 -0
- package/dist/hooks/task-completed.mjs.map +1 -0
- package/dist/iii-config.yaml +51 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +13776 -0
- package/dist/index.mjs.map +1 -0
- package/dist/src-QxitMPfJ.mjs +13775 -0
- package/dist/src-QxitMPfJ.mjs.map +1 -0
- package/dist/standalone.d.mts +1 -0
- package/dist/standalone.mjs +1155 -0
- package/dist/standalone.mjs.map +1 -0
- package/dist/transformers-BX_tgxdO.mjs +38684 -0
- package/dist/transformers-BX_tgxdO.mjs.map +1 -0
- package/dist/transformers-KMm1i9no.mjs +38683 -0
- package/dist/transformers-KMm1i9no.mjs.map +1 -0
- package/docker-compose.yml +14 -0
- package/iii-config.yaml +51 -0
- package/package.json +59 -0
- package/plugin/.claude-plugin/plugin.json +10 -0
- package/plugin/hooks/hooks.json +77 -0
- package/plugin/scripts/diagnostics.mjs +551 -0
- package/plugin/scripts/notification.mjs +45 -0
- package/plugin/scripts/post-tool-failure.mjs +45 -0
- package/plugin/scripts/post-tool-use.mjs +53 -0
- package/plugin/scripts/pre-compact.mjs +50 -0
- package/plugin/scripts/pre-tool-use.mjs +69 -0
- package/plugin/scripts/prompt-submit.mjs +40 -0
- package/plugin/scripts/session-end.mjs +61 -0
- package/plugin/scripts/session-start.mjs +42 -0
- package/plugin/scripts/stop.mjs +33 -0
- package/plugin/scripts/subagent-start.mjs +43 -0
- package/plugin/scripts/subagent-stop.mjs +45 -0
- package/plugin/scripts/task-completed.mjs +46 -0
- package/plugin/skills/forget/SKILL.md +32 -0
- package/plugin/skills/recall/SKILL.md +18 -0
- package/plugin/skills/remember/SKILL.md +25 -0
- package/plugin/skills/session-history/SKILL.md +17 -0
- package/src/auth.ts +12 -0
- package/src/cli.ts +159 -0
- package/src/config.ts +221 -0
- package/src/eval/metrics-store.ts +65 -0
- package/src/eval/quality.ts +51 -0
- package/src/eval/schemas.ts +124 -0
- package/src/eval/self-correct.ts +28 -0
- package/src/eval/validator.ts +31 -0
- package/src/functions/actions.ts +288 -0
- package/src/functions/audit.ts +61 -0
- package/src/functions/auto-forget.ts +169 -0
- package/src/functions/branch-aware.ts +169 -0
- package/src/functions/cascade.ts +80 -0
- package/src/functions/checkpoints.ts +209 -0
- package/src/functions/claude-bridge.ts +161 -0
- package/src/functions/compress.ts +194 -0
- package/src/functions/consolidate.ts +212 -0
- package/src/functions/consolidation-pipeline.ts +258 -0
- package/src/functions/context.ts +169 -0
- package/src/functions/crystallize.ts +293 -0
- package/src/functions/dedup.ts +57 -0
- package/src/functions/diagnostics.ts +785 -0
- package/src/functions/enrich.ts +132 -0
- package/src/functions/evict.ts +163 -0
- package/src/functions/export-import.ts +508 -0
- package/src/functions/facets.ts +248 -0
- package/src/functions/file-index.ts +106 -0
- package/src/functions/flow-compress.ts +214 -0
- package/src/functions/frontier.ts +196 -0
- package/src/functions/governance.ts +131 -0
- package/src/functions/graph-retrieval.ts +277 -0
- package/src/functions/graph.ts +275 -0
- package/src/functions/leases.ts +216 -0
- package/src/functions/lessons.ts +253 -0
- package/src/functions/mesh.ts +434 -0
- package/src/functions/migrate.ts +165 -0
- package/src/functions/observe.ts +144 -0
- package/src/functions/obsidian-export.ts +310 -0
- package/src/functions/patterns.ts +138 -0
- package/src/functions/privacy.ts +39 -0
- package/src/functions/profile.ts +155 -0
- package/src/functions/query-expansion.ts +186 -0
- package/src/functions/relations.ts +237 -0
- package/src/functions/remember.ts +162 -0
- package/src/functions/retention.ts +235 -0
- package/src/functions/routines.ts +289 -0
- package/src/functions/search.ts +80 -0
- package/src/functions/sentinels.ts +417 -0
- package/src/functions/signals.ts +186 -0
- package/src/functions/sketches.ts +274 -0
- package/src/functions/sliding-window.ts +257 -0
- package/src/functions/smart-search.ts +115 -0
- package/src/functions/snapshot.ts +219 -0
- package/src/functions/summarize.ts +155 -0
- package/src/functions/team.ts +147 -0
- package/src/functions/temporal-graph.ts +476 -0
- package/src/functions/timeline.ts +138 -0
- package/src/functions/verify.ts +117 -0
- package/src/health/monitor.ts +110 -0
- package/src/health/thresholds.ts +73 -0
- package/src/hooks/notification.ts +52 -0
- package/src/hooks/post-tool-failure.ts +58 -0
- package/src/hooks/post-tool-use.ts +62 -0
- package/src/hooks/pre-compact.ts +60 -0
- package/src/hooks/pre-tool-use.ts +72 -0
- package/src/hooks/prompt-submit.ts +46 -0
- package/src/hooks/session-end.ts +71 -0
- package/src/hooks/session-start.ts +48 -0
- package/src/hooks/stop.ts +39 -0
- package/src/hooks/subagent-start.ts +49 -0
- package/src/hooks/subagent-stop.ts +54 -0
- package/src/hooks/task-completed.ts +54 -0
- package/src/index.ts +342 -0
- package/src/mcp/in-memory-kv.ts +61 -0
- package/src/mcp/server.ts +1455 -0
- package/src/mcp/standalone.ts +177 -0
- package/src/mcp/tools-registry.ts +769 -0
- package/src/mcp/transport.ts +91 -0
- package/src/prompts/compression.ts +67 -0
- package/src/prompts/consolidation.ts +48 -0
- package/src/prompts/graph-extraction.ts +35 -0
- package/src/prompts/summary.ts +38 -0
- package/src/prompts/xml.ts +26 -0
- package/src/providers/agent-sdk.ts +34 -0
- package/src/providers/anthropic.ts +35 -0
- package/src/providers/circuit-breaker.ts +82 -0
- package/src/providers/embedding/cohere.ts +46 -0
- package/src/providers/embedding/gemini.ts +54 -0
- package/src/providers/embedding/index.ts +39 -0
- package/src/providers/embedding/local.ts +52 -0
- package/src/providers/embedding/openai.ts +45 -0
- package/src/providers/embedding/openrouter.ts +51 -0
- package/src/providers/embedding/voyage.ts +46 -0
- package/src/providers/fallback-chain.ts +31 -0
- package/src/providers/index.ts +84 -0
- package/src/providers/openrouter.ts +71 -0
- package/src/providers/resilient.ts +37 -0
- package/src/state/hybrid-search.ts +295 -0
- package/src/state/index-persistence.ts +63 -0
- package/src/state/keyed-mutex.ts +18 -0
- package/src/state/kv.ts +33 -0
- package/src/state/schema.ts +71 -0
- package/src/state/search-index.ts +245 -0
- package/src/state/stemmer.ts +104 -0
- package/src/state/synonyms.ts +63 -0
- package/src/state/vector-index.ts +130 -0
- package/src/telemetry/setup.ts +116 -0
- package/src/triggers/api.ts +1904 -0
- package/src/triggers/events.ts +71 -0
- package/src/types.ts +769 -0
- package/src/version.ts +1 -0
- package/src/viewer/index.html +2497 -0
- package/src/viewer/server.ts +207 -0
- package/src/xenova.d.ts +3 -0
- package/test/actions.test.ts +490 -0
- package/test/audit.test.ts +108 -0
- package/test/auto-forget.test.ts +188 -0
- package/test/cascade.test.ts +277 -0
- package/test/checkpoints.test.ts +493 -0
- package/test/circuit-breaker.test.ts +107 -0
- package/test/claude-bridge.test.ts +178 -0
- package/test/confidence.test.ts +247 -0
- package/test/consistency.test.ts +61 -0
- package/test/consolidation-pipeline.test.ts +251 -0
- package/test/crystallize.test.ts +521 -0
- package/test/diagnostics.test.ts +638 -0
- package/test/embedding-provider.test.ts +49 -0
- package/test/enrich.test.ts +209 -0
- package/test/eval.test.ts +300 -0
- package/test/export-import.test.ts +251 -0
- package/test/facets.test.ts +448 -0
- package/test/fallback-chain.test.ts +93 -0
- package/test/frontier.test.ts +485 -0
- package/test/governance.test.ts +147 -0
- package/test/graph-retrieval.test.ts +186 -0
- package/test/graph.test.ts +160 -0
- package/test/helpers/mocks.ts +40 -0
- package/test/hybrid-search.test.ts +145 -0
- package/test/index-persistence.test.ts +124 -0
- package/test/integration.test.ts +265 -0
- package/test/leases.test.ts +399 -0
- package/test/mcp-prompts.test.ts +218 -0
- package/test/mcp-resources.test.ts +286 -0
- package/test/mcp-standalone.test.ts +113 -0
- package/test/mesh.test.ts +700 -0
- package/test/privacy.test.ts +87 -0
- package/test/profile.test.ts +161 -0
- package/test/query-expansion.test.ts +154 -0
- package/test/relations.test.ts +198 -0
- package/test/retention.test.ts +245 -0
- package/test/routines.test.ts +497 -0
- package/test/schema-fingerprint.test.ts +81 -0
- package/test/schema.test.ts +42 -0
- package/test/search-index.test.ts +128 -0
- package/test/sentinels.test.ts +626 -0
- package/test/signals.test.ts +410 -0
- package/test/sketches.test.ts +549 -0
- package/test/sliding-window.test.ts +199 -0
- package/test/smart-search.test.ts +169 -0
- package/test/snapshot.test.ts +165 -0
- package/test/team.test.ts +156 -0
- package/test/temporal-graph.test.ts +378 -0
- package/test/timeline.test.ts +148 -0
- package/test/vector-index.test.ts +79 -0
- package/test/verify.test.ts +209 -0
- package/test/xml.test.ts +65 -0
- package/tsconfig.json +22 -0
- package/tsdown.config.ts +62 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { ISdk } from "iii-sdk";
|
|
2
|
+
import { getContext } from "iii-sdk";
|
|
3
|
+
import type { Memory } from "../types.js";
|
|
4
|
+
import { KV } from "../state/schema.js";
|
|
5
|
+
import { StateKV } from "../state/kv.js";
|
|
6
|
+
|
|
7
|
+
const MAX_CONTEXT_LENGTH = 4000;
|
|
8
|
+
|
|
9
|
+
function escapeXml(s: string): string {
|
|
10
|
+
return s
|
|
11
|
+
.replace(/&/g, "&")
|
|
12
|
+
.replace(/</g, "<")
|
|
13
|
+
.replace(/>/g, ">")
|
|
14
|
+
.replace(/"/g, """)
|
|
15
|
+
.replace(/'/g, "'");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function registerEnrichFunction(sdk: ISdk, kv: StateKV): void {
|
|
19
|
+
sdk.registerFunction(
|
|
20
|
+
{
|
|
21
|
+
id: "mem::enrich",
|
|
22
|
+
description:
|
|
23
|
+
"Aggregate file context, relevant observations, and bug memories for pre-tool enrichment",
|
|
24
|
+
},
|
|
25
|
+
async (data: {
|
|
26
|
+
sessionId: string;
|
|
27
|
+
files: string[];
|
|
28
|
+
terms?: string[];
|
|
29
|
+
toolName?: string;
|
|
30
|
+
}) => {
|
|
31
|
+
const ctx = getContext();
|
|
32
|
+
const parts: string[] = [];
|
|
33
|
+
|
|
34
|
+
const fileContextPromise = sdk
|
|
35
|
+
.trigger<{ sessionId: string; files: string[] }, { context: string }>(
|
|
36
|
+
"mem::file-context",
|
|
37
|
+
{
|
|
38
|
+
sessionId: data.sessionId,
|
|
39
|
+
files: data.files,
|
|
40
|
+
},
|
|
41
|
+
)
|
|
42
|
+
.catch(() => ({ context: "" }));
|
|
43
|
+
|
|
44
|
+
const searchQueries: string[] = [
|
|
45
|
+
...data.files.map((f) => f.split("/").pop() || f),
|
|
46
|
+
...(data.terms || []),
|
|
47
|
+
].filter((q) => q.length > 0);
|
|
48
|
+
|
|
49
|
+
const searchPromise =
|
|
50
|
+
searchQueries.length > 0
|
|
51
|
+
? sdk
|
|
52
|
+
.trigger<
|
|
53
|
+
{ query: string; limit: number },
|
|
54
|
+
{ results: Array<{ observation: { narrative: string } }> }
|
|
55
|
+
>("mem::search", {
|
|
56
|
+
query: searchQueries.join(" "),
|
|
57
|
+
limit: 5,
|
|
58
|
+
})
|
|
59
|
+
.catch(() => ({ results: [] }))
|
|
60
|
+
: Promise.resolve({ results: [] });
|
|
61
|
+
|
|
62
|
+
const bugMemoriesPromise = kv
|
|
63
|
+
.list<Memory>(KV.memories)
|
|
64
|
+
.then((memories) =>
|
|
65
|
+
memories
|
|
66
|
+
.filter(
|
|
67
|
+
(m) =>
|
|
68
|
+
m.type === "bug" &&
|
|
69
|
+
m.isLatest &&
|
|
70
|
+
m.files.some((f) =>
|
|
71
|
+
data.files.some((df) => f.includes(df) || df.includes(f)),
|
|
72
|
+
),
|
|
73
|
+
)
|
|
74
|
+
.sort(
|
|
75
|
+
(a, b) =>
|
|
76
|
+
new Date(b.updatedAt || b.createdAt).getTime() -
|
|
77
|
+
new Date(a.updatedAt || a.createdAt).getTime(),
|
|
78
|
+
),
|
|
79
|
+
)
|
|
80
|
+
.catch(() => []);
|
|
81
|
+
|
|
82
|
+
const [fileContext, searchResult, bugMemories] = await Promise.all([
|
|
83
|
+
fileContextPromise,
|
|
84
|
+
searchPromise,
|
|
85
|
+
bugMemoriesPromise,
|
|
86
|
+
]);
|
|
87
|
+
|
|
88
|
+
if (fileContext.context) {
|
|
89
|
+
parts.push(fileContext.context);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (searchResult.results.length > 0) {
|
|
93
|
+
const observations = searchResult.results
|
|
94
|
+
.map((r) => r.observation?.narrative)
|
|
95
|
+
.filter(Boolean)
|
|
96
|
+
.map((n) => escapeXml(n as string))
|
|
97
|
+
.join("\n");
|
|
98
|
+
if (observations) {
|
|
99
|
+
parts.push(
|
|
100
|
+
`<agentmemory-relevant-context>\n${observations}\n</agentmemory-relevant-context>`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (bugMemories.length > 0) {
|
|
106
|
+
const bugs = bugMemories
|
|
107
|
+
.slice(0, 3)
|
|
108
|
+
.map((m) => `- ${escapeXml(m.title)}: ${escapeXml(m.content)}`)
|
|
109
|
+
.join("\n");
|
|
110
|
+
parts.push(
|
|
111
|
+
`<agentmemory-past-errors>\n${bugs}\n</agentmemory-past-errors>`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let context = parts.join("\n\n");
|
|
116
|
+
let truncated = false;
|
|
117
|
+
if (context.length > MAX_CONTEXT_LENGTH) {
|
|
118
|
+
context = context.slice(0, MAX_CONTEXT_LENGTH);
|
|
119
|
+
truncated = true;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
ctx.logger.info("Enrichment completed", {
|
|
123
|
+
sessionId: data.sessionId,
|
|
124
|
+
fileCount: data.files.length,
|
|
125
|
+
contextLength: context.length,
|
|
126
|
+
truncated,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
return { context, truncated };
|
|
130
|
+
},
|
|
131
|
+
);
|
|
132
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type { ISdk } from "iii-sdk";
|
|
2
|
+
import { getContext } from "iii-sdk";
|
|
3
|
+
import type {
|
|
4
|
+
Session,
|
|
5
|
+
CompressedObservation,
|
|
6
|
+
SessionSummary,
|
|
7
|
+
Memory,
|
|
8
|
+
} from "../types.js";
|
|
9
|
+
import { KV } from "../state/schema.js";
|
|
10
|
+
import { StateKV } from "../state/kv.js";
|
|
11
|
+
|
|
12
|
+
interface EvictionConfig {
|
|
13
|
+
staleSessionDays: number;
|
|
14
|
+
lowImportanceMaxDays: number;
|
|
15
|
+
lowImportanceThreshold: number;
|
|
16
|
+
maxObservationsPerProject: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const MS_PER_DAY = 24 * 60 * 60 * 1000;
|
|
20
|
+
|
|
21
|
+
const DEFAULTS: EvictionConfig = {
|
|
22
|
+
staleSessionDays: 30,
|
|
23
|
+
lowImportanceMaxDays: 90,
|
|
24
|
+
lowImportanceThreshold: 3,
|
|
25
|
+
maxObservationsPerProject: 10_000,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
interface EvictionStats {
|
|
29
|
+
staleSessions: number;
|
|
30
|
+
lowImportanceObs: number;
|
|
31
|
+
capEvictions: number;
|
|
32
|
+
expiredMemories: number;
|
|
33
|
+
nonLatestMemories: number;
|
|
34
|
+
dryRun: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function registerEvictFunction(sdk: ISdk, kv: StateKV): void {
|
|
38
|
+
sdk.registerFunction(
|
|
39
|
+
{
|
|
40
|
+
id: "mem::evict",
|
|
41
|
+
description: "Evict stale memories based on age and importance",
|
|
42
|
+
},
|
|
43
|
+
async (data: { dryRun?: boolean }): Promise<EvictionStats> => {
|
|
44
|
+
const ctx = getContext();
|
|
45
|
+
const dryRun = data?.dryRun ?? false;
|
|
46
|
+
|
|
47
|
+
const configOverride = await kv
|
|
48
|
+
.get<Partial<EvictionConfig>>(KV.config, "eviction")
|
|
49
|
+
.catch(() => null);
|
|
50
|
+
const cfg = { ...DEFAULTS, ...configOverride };
|
|
51
|
+
|
|
52
|
+
const now = Date.now();
|
|
53
|
+
const stats: EvictionStats = {
|
|
54
|
+
staleSessions: 0,
|
|
55
|
+
lowImportanceObs: 0,
|
|
56
|
+
capEvictions: 0,
|
|
57
|
+
expiredMemories: 0,
|
|
58
|
+
nonLatestMemories: 0,
|
|
59
|
+
dryRun,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const sessions = await kv.list<Session>(KV.sessions).catch(() => []);
|
|
63
|
+
const summaries = await kv
|
|
64
|
+
.list<SessionSummary>(KV.summaries)
|
|
65
|
+
.catch(() => []);
|
|
66
|
+
const summaryIds = new Set(summaries.map((s) => s.sessionId));
|
|
67
|
+
|
|
68
|
+
for (const session of sessions) {
|
|
69
|
+
if (!session.startedAt) continue;
|
|
70
|
+
const age = now - new Date(session.startedAt).getTime();
|
|
71
|
+
const staleDays = cfg.staleSessionDays * MS_PER_DAY;
|
|
72
|
+
if (age > staleDays && !summaryIds.has(session.id)) {
|
|
73
|
+
stats.staleSessions++;
|
|
74
|
+
if (!dryRun) {
|
|
75
|
+
await kv.delete(KV.sessions, session.id).catch(() => {});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const projectObs = new Map<string, CompressedObservation[]>();
|
|
81
|
+
for (const session of sessions) {
|
|
82
|
+
const obs = await kv
|
|
83
|
+
.list<CompressedObservation>(KV.observations(session.id))
|
|
84
|
+
.catch(() => []);
|
|
85
|
+
const compressed = obs.filter((o) => o.title);
|
|
86
|
+
|
|
87
|
+
for (const o of compressed) {
|
|
88
|
+
if (!o.timestamp) continue;
|
|
89
|
+
const age = now - new Date(o.timestamp).getTime();
|
|
90
|
+
const maxAge = cfg.lowImportanceMaxDays * MS_PER_DAY;
|
|
91
|
+
if (
|
|
92
|
+
age > maxAge &&
|
|
93
|
+
(o.importance ?? 5) < cfg.lowImportanceThreshold
|
|
94
|
+
) {
|
|
95
|
+
stats.lowImportanceObs++;
|
|
96
|
+
if (!dryRun) {
|
|
97
|
+
await kv
|
|
98
|
+
.delete(KV.observations(session.id), o.id)
|
|
99
|
+
.catch(() => {});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const project = session.project || "unknown";
|
|
105
|
+
const existing = projectObs.get(project) || [];
|
|
106
|
+
existing.push(...compressed);
|
|
107
|
+
projectObs.set(project, existing);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
for (const [, obs] of projectObs) {
|
|
111
|
+
if (obs.length > cfg.maxObservationsPerProject) {
|
|
112
|
+
const sorted = obs.sort(
|
|
113
|
+
(a, b) => (a.importance ?? 5) - (b.importance ?? 5),
|
|
114
|
+
);
|
|
115
|
+
const toEvict = sorted.slice(
|
|
116
|
+
0,
|
|
117
|
+
obs.length - cfg.maxObservationsPerProject,
|
|
118
|
+
);
|
|
119
|
+
stats.capEvictions += toEvict.length;
|
|
120
|
+
if (!dryRun) {
|
|
121
|
+
for (const o of toEvict) {
|
|
122
|
+
await kv
|
|
123
|
+
.delete(KV.observations(o.sessionId), o.id)
|
|
124
|
+
.catch(() => {});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const memories = await kv.list<Memory>(KV.memories).catch(() => []);
|
|
131
|
+
const evictedMemIds = new Set<string>();
|
|
132
|
+
for (const mem of memories) {
|
|
133
|
+
if (mem.forgetAfter) {
|
|
134
|
+
const expiry = new Date(mem.forgetAfter).getTime();
|
|
135
|
+
if (now > expiry) {
|
|
136
|
+
stats.expiredMemories++;
|
|
137
|
+
evictedMemIds.add(mem.id);
|
|
138
|
+
if (!dryRun) {
|
|
139
|
+
await kv.delete(KV.memories, mem.id).catch(() => {});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (
|
|
145
|
+
!evictedMemIds.has(mem.id) &&
|
|
146
|
+
mem.isLatest === false &&
|
|
147
|
+
mem.createdAt
|
|
148
|
+
) {
|
|
149
|
+
const age = now - new Date(mem.createdAt).getTime();
|
|
150
|
+
if (age > cfg.lowImportanceMaxDays * MS_PER_DAY) {
|
|
151
|
+
stats.nonLatestMemories++;
|
|
152
|
+
if (!dryRun) {
|
|
153
|
+
await kv.delete(KV.memories, mem.id).catch(() => {});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
ctx.logger.info("Eviction complete", { stats });
|
|
160
|
+
return stats;
|
|
161
|
+
},
|
|
162
|
+
);
|
|
163
|
+
}
|