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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

## [Unreleased]

### Fixed

- **`mem::reflect` no longer enumerates the graph node scope.** `kv.list(KV.graphNodes)` stopped completing once `mem:graph:nodes` grew past ~100 MB, and the failure was swallowed by a silent `.catch(() => [])`. Graph clustering then returned nothing, reflect fell back to lexical clustering, and a cluster matching thousands of facts produced a prompt large enough to exceed the 180s invocation timeout — every run, silently disabling insight generation. Reflect now reads the top-degree graph snapshot (the same fast path graph extraction already used), cluster inputs are capped at 50 items per category, and the remaining `kv.list` calls log a warning instead of degrading silently.
- **Health no longer pins itself to `critical` on a healthy process.** Memory severity divided `heapUsed` by `heapTotal`, but `heapTotal` is the heap V8 has committed so far, which it deliberately keeps near-full and grows on demand. Any process past the RSS floor therefore reported ~100% and `/agentmemory/health` returned 503 indefinitely, making it useless as a liveness signal. The snapshot now carries `heapLimit` from `v8.getHeapStatistics()` and the ratio is measured against that ceiling; snapshots persisted without it still evaluate as before.

## [0.9.27] — 2026-06-07

Wave release closing several breaking regressions reported against v0.9.26, plus an agent-scope isolation security fix, an iii version-pin audit fix, and a benchmark scorecard correction. No breaking changes; drop-in upgrade.
Expand Down
12 changes: 3 additions & 9 deletions plugin/scripts/stop.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,16 @@ async function main() {
}
if (isSdkChildContext(data)) return;
const sessionId = data.session_id || data.sessionId || "unknown";
fetch(`${REST_URL}/agentmemory/summarize`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({ sessionId }),
signal: AbortSignal.timeout(12e4)
}).catch(() => {});
fetch(`${REST_URL}/agentmemory/session/end`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({ sessionId }),
signal: AbortSignal.timeout(5e3)
}).catch(() => {});
setTimeout(() => process.exit(0), 1500).unref();
setTimeout(() => process.exit(0), 500).unref();
}
Comment on lines 26 to 33
main();

//#endregion
export { };
export {};

//# sourceMappingURL=stop.mjs.map
4 changes: 3 additions & 1 deletion src/functions/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ function emptySnapshot(): GraphSnapshot {
};
}

async function readSnapshot(kv: StateKV): Promise<GraphSnapshot | null> {
export async function readSnapshot(
kv: StateKV,
): Promise<GraphSnapshot | null> {
try {
const snap = await kv.get<GraphSnapshot>(KV.graphSnapshot, SNAPSHOT_KEY);
if (snap && typeof snap === "object" && snap.version === 1) {
Expand Down
14 changes: 14 additions & 0 deletions src/functions/input-fingerprint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createHash } from "node:crypto";
import type { CompressedObservation } from "../types.js";

// Stable fingerprint of an exact observation set, used to skip LLM re-runs
// (summarize, graph extraction) when the input is unchanged. Count alone is
// not enough: evict/auto-forget can delete observations, so the set can
// change while the count returns to a previous value.
export function computeInputFingerprint(
observations: CompressedObservation[],
): string {
const h = createHash("sha256");
for (const o of observations) h.update(`${o.id} ${o.timestamp} `);
return h.digest("hex").slice(0, 32);
}
67 changes: 55 additions & 12 deletions src/functions/reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type {
MemoryProvider,
} from "../types.js";
import { recordAudit } from "./audit.js";
import { readSnapshot } from "./graph.js";
import { logger } from "../logger.js";
import { REFLECT_SYSTEM, buildReflectPrompt } from "../prompts/reflect.js";

interface ConceptCluster {
Expand Down Expand Up @@ -170,16 +172,46 @@ export function registerReflectFunctions(
const maxClusters = Math.min(data?.maxClusters ?? 10, 20);
const maxInsightsPerCluster = 5;
const maxTotal = 50;
const maxItemsPerCluster = 50;

const [graphNodes, graphEdges, semanticMemories, lessons, crystals] =
// 讀不到就退化成 fallback 分群,但不能靜默:payload 一旦超過引擎上限
// 就會在這裡失敗,而 fallback 的 prompt 成本高出兩個數量級。
const listOrEmpty = async <T>(scope: string): Promise<T[]> => {
try {
return await kv.list<T>(scope);
} catch (error) {
logger.warn("reflect: kv.list failed, degrading to empty set", {
scope,
error: error instanceof Error ? error.message : String(error),
});
return [];
}
};

// #814 v2: graph nodes come from the precomputed top-degree
// snapshot, never from `kv.list<GraphNode>(KV.graphNodes)`. At 26K
// nodes that list burns the entire invocation budget, so reflect
// timed out before it could emit anything. The snapshot is capped
// at the top-degree subgraph, which is far more than the at-most-20
// clusters this function builds. No snapshot => empty node set =>
// the existing lexical fallback below.
const [snapshot, graphEdges, semanticMemories, lessons, crystals] =
await Promise.all([
kv.list<GraphNode>(KV.graphNodes).catch(() => []),
kv.list<GraphEdge>(KV.graphEdges).catch(() => []),
kv.list<SemanticMemory>(KV.semantic).catch(() => []),
kv.list<Lesson>(KV.lessons).catch(() => []),
kv.list<Crystal>(KV.crystals).catch(() => []),
readSnapshot(kv),
listOrEmpty<GraphEdge>(KV.graphEdges),
listOrEmpty<SemanticMemory>(KV.semantic),
listOrEmpty<Lesson>(KV.lessons),
listOrEmpty<Crystal>(KV.crystals),
]);
Comment on lines +198 to 205

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance & Scalability | 🟠 Major | ⚡ Quick win

Use snapshot edges for reflection.

Line 201 still enumerates every graph edge. A large KV.graphEdges scope can consume the invocation budget before listOrEmpty catches a failure. Reflection then has snapshot nodes but no edges, so it unnecessarily uses lexical clustering.

Use snapshot?.topEdges ?? []. GraphSnapshot.topEdges is the bounded edge set that corresponds to the snapshot node set. Extend test/reflect.test.ts to assert that mem:graph:edges is not enumerated.

Proposed fix
-      const [snapshot, graphEdges, semanticMemories, lessons, crystals] =
+      const [snapshot, semanticMemories, lessons, crystals] =
         await Promise.all([
           readSnapshot(kv),
-          listOrEmpty<GraphEdge>(KV.graphEdges),
           listOrEmpty<SemanticMemory>(KV.semantic),
           listOrEmpty<Lesson>(KV.lessons),
           listOrEmpty<Crystal>(KV.crystals),
         ]);
+      const graphEdges = snapshot?.topEdges ?? [];
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const [snapshot, graphEdges, semanticMemories, lessons, crystals] =
await Promise.all([
kv.list<GraphNode>(KV.graphNodes).catch(() => []),
kv.list<GraphEdge>(KV.graphEdges).catch(() => []),
kv.list<SemanticMemory>(KV.semantic).catch(() => []),
kv.list<Lesson>(KV.lessons).catch(() => []),
kv.list<Crystal>(KV.crystals).catch(() => []),
readSnapshot(kv),
listOrEmpty<GraphEdge>(KV.graphEdges),
listOrEmpty<SemanticMemory>(KV.semantic),
listOrEmpty<Lesson>(KV.lessons),
listOrEmpty<Crystal>(KV.crystals),
]);
const [snapshot, semanticMemories, lessons, crystals] =
await Promise.all([
readSnapshot(kv),
listOrEmpty<SemanticMemory>(KV.semantic),
listOrEmpty<Lesson>(KV.lessons),
listOrEmpty<Crystal>(KV.crystals),
]);
const graphEdges = snapshot?.topEdges ?? [];
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/functions/reflect.ts` around lines 198 - 205, Update the reflection
data-loading flow around Promise.all to derive graphEdges from
snapshot?.topEdges ?? [] instead of calling listOrEmpty on KV.graphEdges, while
preserving the existing snapshot node and memory loading behavior. Extend the
relevant reflect test to verify that mem:graph:edges is not enumerated.


if (!snapshot) {
logger.warn(
"reflect: no graph snapshot, degrading to lexical clustering",
{ scope: KV.graphSnapshot },
);
}
const graphNodes: GraphNode[] = snapshot?.topNodes ?? [];

let activeLessons = lessons.filter((l) => !l.deleted);
if (data?.project) {
activeLessons = activeLessons.filter((l) => l.project === data.project);
Expand Down Expand Up @@ -237,20 +269,31 @@ export function registerReflectFunctions(
continue;
}

// 詞彙 fallback 的 cluster 可以匹配到數千筆 facts,未截斷的 prompt 會
// 撐到數十萬 token 並超過引擎的 invocation timeout。取信心最高的前段即可。
const topByConfidence = <T extends { confidence: number }>(items: T[]) =>
[...items]
.sort((a, b) => b.confidence - a.confidence)
.slice(0, maxItemsPerCluster);

const selectedFacts = topByConfidence(clusterFacts);
const selectedLessons = topByConfidence(clusterLessons);
const selectedCrystals = clusterCrystals.slice(0, maxItemsPerCluster);

const cluster: ConceptCluster = {
concepts: conceptNames,
facts: clusterFacts.map((f) => ({
facts: selectedFacts.map((f) => ({
fact: f.fact,
confidence: f.confidence,
})),
lessons: clusterLessons.map((l) => ({
lessons: selectedLessons.map((l) => ({
content: l.content,
confidence: l.confidence,
})),
crystalNarratives: clusterCrystals.map((c) => c.narrative),
factIds: clusterFacts.map((f) => f.id),
lessonIds: clusterLessons.map((l) => l.id),
crystalIds: clusterCrystals.map((c) => c.id),
crystalNarratives: selectedCrystals.map((c) => c.narrative),
factIds: selectedFacts.map((f) => f.id),
lessonIds: selectedLessons.map((l) => l.id),
crystalIds: selectedCrystals.map((c) => c.id),
};

try {
Expand Down
Loading