Skip to content

#1 Persistent memory for AI coding agents based on real-world benchmarks

fix(viewer): live-stream updates instead of re-fetching, keep data on failed refresh, correct dashboard numbers (#1407)

Rohit Ghumareb146382797c7
diff --git a/AGENTS.md b/AGENTS.md
index 58f7e87e..45b0ce96 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -119,7 +119,7 @@ Hook scripts in `src/hooks/` are standalone Node.js scripts (no iii-sdk import).
## Current Stats (v0.9.29)
- 54 MCP tools (8 visible by default, `AGENTMEMORY_TOOLS=all` for all)
-- 131 REST endpoints
+- 132 REST endpoints
- 6 MCP resources, 3 MCP prompts
- 12 hooks, 17 skills
- 260+ iii functions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 38fc2ba6..1e5b3bf3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
### Fixed
+- **The viewer updates from the live stream instead of re-fetching, and stops blinking.** The dashboard used to wipe itself to "Loading dashboard..." and re-fetch everything on a 30 s timer and on every stream event; on an idle store that was 73 rebuilds and 20 requests a minute, and 21 live events cost 200 requests. The worker now pushes `session.updated`, `session.deleted`, `memory.updated` and `memory.deleted` on the `mem-live` viewer group next to the existing observation and `session.activity` events (which the viewer used to drop because it read the wrong field), and the viewer applies them to what is on screen: sessions, observation counts, session status and the memory total update with zero requests, redraws are batched to at most one every 250 ms and skipped when nothing changed. The 30 s timer is gone, no refresh path blanks a loaded view, and polling remains only as the fallback when the stream cannot connect (#921, #1340). The viewer now connects with the stream mode engine 0.22 serves first instead of failing twice on the other one. Live buffers on the Timeline and Activity tabs are capped at a few hundred entries so a large stream backlog cannot freeze the tab (#609). When the stream reconnects, the loaded views re-fetch once so changes made while it was down show up, and a list that was loading when live events arrived replays them, so a deleted session does not come back. Session counts no longer reload or blank the open session detail, bursts of memory events coalesce into one list reload, and with an isolated agent scope (`AGENT_ID` plus `AGENTMEMORY_AGENT_SCOPE=isolated`) the worker only pushes that agent's sessions and memories.
+- **A failed dashboard refresh keeps the last data.** Panels that fail to refresh keep their previous values behind a "could not refresh" note instead of flipping to the new-install screen; a first load that cannot reach the API shows a retry button (#1340).
+- **Dashboard numbers match the API.** The Memories card uses the server total instead of counting a capped list (#1279, #951), token savings only counts sessions that produced observations (#944), and the Graph Nodes card shows when its snapshot was taken.
+- **Session summaries stored as objects render as text instead of `[object Object]`** (#1229), and tags stored as a comma-separated string no longer break the Actions, Lessons or Sessions views (#906).
+- **`GET /agentmemory/memories` returns newest first**, so a `limit` page shows the latest memories rather than the oldest (#990).
+- **Rebuild Graph asks before replaying the corpus**, ignores repeat clicks while it runs, and reports how many sessions, nodes and edges it produced (#1383).
+- **The dashboard explains consolidation instead of showing three bare counters.** Semantic Memory, Procedural Memory and Consolidation Status are one "Memory layers" panel: each layer says what it is built from, how many items it holds, and whether it is off (with the exact setting to change), waiting (for example "3 of 5 session summaries needed"), ready, active with the last run's output, or failed with the error. The consolidation pipeline now stores each run's per-tier results, served at `GET /agentmemory/consolidation/status`.
+- **Procedures are extracted from finished sessions.** Procedural memory only came from a cross-session pass that needs two pattern memories each seen in two or more sessions, so it stayed empty on almost every install, and `mem::skill-extract` (which turns one session with a summary and three or more observations into a procedure) had no caller. It now runs for the stopping session inside the existing consolidation cooldown, only when consolidation is on and an LLM provider is configured, so it adds at most one LLM call per cooldown window and none on keyless installs.
+- **The viewer reconnects its live stream when the tab becomes visible again** after a sleep or network drop, instead of staying disconnected until a manual reload (#1370).
- **The search index snapshot is written continuously, not only on clean shutdown.** Every observation, compression result and memory save now schedules the debounced snapshot write, and bulk indexing (import, replay, rebuild) does the same. Before, the BM25/vector snapshot was written only by a graceful shutdown; after a crash, `kill -9` or power loss the worker booted the stale snapshot, skipped the rebuild because the snapshot was not empty, and every observation captured since the last clean stop stayed invisible to search for good. On boot the worker now also compares the loaded snapshot against each session's observation count and re-indexes whatever is missing. Re-adding an observation under an id that is already indexed (compression replacing the synthetic entry) now replaces the index document instead of stacking postings on top of the old ones.
- **Worker shutdown no longer hangs when the engine is already gone.** The SIGTERM handler waited on a state write that can never land while the SDK is reconnecting, so `agentmemory stop` had to SIGKILL an orphaned worker after its grace period. The final index flush and the SDK shutdown are now time-boxed at 4 s each, with a hard exit at 8 s, and open viewer connections are closed instead of awaited. `agentmemory stop` gives an orphaned worker that full budget before escalating to SIGKILL.
diff --git a/README.md b/README.md
index 8fc01484..1a84a013 100644
--- a/README.md
+++ b/README.md
@@ -1608,7 +1608,7 @@ Create `~/.agentmemory/.env`:
<h2 id="api"><picture><source media="(prefers-color-scheme: dark)" srcset="assets/tags/light/section-api.svg"><img src="assets/tags/section-api.svg" alt="API" height="32" /></picture></h2>
-131 endpoints on port `3111`. The REST API binds to `127.0.0.1` by default. Protected endpoints require `Authorization: Bearer <secret>` when `AGENTMEMORY_SECRET` is set, and mesh sync endpoints require `AGENTMEMORY_SECRET` on both peers.
+132 endpoints on port `3111`. The REST API binds to `127.0.0.1` by default. Protected endpoints require `Authorization: Bearer <secret>` when `AGENTMEMORY_SECRET` is set, and mesh sync endpoints require `AGENTMEMORY_SECRET` on both peers.
<details>
<summary>Key endpoints</summary>
diff --git a/plugin/skills/agentmemory-rest-api/REFERENCE.md b/plugin/skills/agentmemory-rest-api/REFERENCE.md
index bacd540f..e98b8adf 100644
--- a/plugin/skills/agentmemory-rest-api/REFERENCE.md
+++ b/plugin/skills/agentmemory-rest-api/REFERENCE.md
@@ -5,7 +5,7 @@ Generated from `src/triggers/api.ts`. Do not edit the block below by hand; run `
<!-- AUTOGEN:rest START - generated by scripts/skills/generate.ts, do not edit by hand -->
The REST API is the primary surface. All paths are under `http://localhost:3111` (override with `--port`). When `AGENTMEMORY_SECRET` is set, send `Authorization: Bearer $AGENTMEMORY_SECRET`; localhost is otherwise open.
-131 registered endpoints:
+132 registered endpoints:
| Method | Path |
| --- | --- |
@@ -30,6 +30,7 @@ The REST API is the primary surface. All paths are under `http://localhost:3111`
| GET | `/agentmemory/config/flags` |
| POST | `/agentmemory/consolidate` |
| POST | `/agentmemory/consolidate-pipeline` |
+| GET | `/agentmemory/consolidation/status` |
| POST | `/agentmemory/context` |
| GET | `/agentmemory/crystals` |
| POST | `/agentmemory/crystals/auto` |
diff --git a/src/functions/consolidation-pipeline.ts b/src/functions/consolidation-pipeline.ts
index 75791148..65a23767 100644
--- a/src/functions/consolidation-pipeline.ts
+++ b/src/functions/consolidation-pipeline.ts
@@ -15,6 +15,7 @@ import {
buildProceduralExtractionPrompt,
} from "../prompts/consolidation.js";
import { recordAudit } from "./audit.js";
+import { CONSOLIDATION_LAST_RUN_KEY } from "./consolidation-status.js";
import { getConsolidationDecayDays, isConsolidationEnabled } from "../config.js";
import { logger } from "../logger.js";
@@ -263,6 +264,10 @@ export function registerConsolidationPipelineFunction(
results,
});
+ await kv
+ .set(KV.config, CONSOLIDATION_LAST_RUN_KEY, { at: new Date().toISOString(), tier, results })
+ .catch(() => {});
+
logger.info("Consolidation pipeline complete", { tier, results });
return { success: true, results };
},
diff --git a/src/functions/consolidation-status.ts b/src/functions/consolidation-status.ts
new file mode 100644
index 00000000..48ee02f7
--- /dev/null
+++ b/src/functions/consolidation-status.ts
@@ -0,0 +1,131 @@
+export const CONSOLIDATION_LAST_RUN_KEY = "consolidation:lastPipelineRun";
+export const SEMANTIC_MIN_SUMMARIES = 5;
+export const PROCEDURAL_MIN_PATTERNS = 2;
+export const PROCEDURAL_MIN_SESSIONS_PER_PATTERN = 2;
+export const CONSOLIDATION_COUNTS_REUSE_MS = 30_000;
+
+export type TierState = "off" | "waiting" | "ready" | "ran" | "error";
+
+export interface ConsolidationRunRecord {
+ at: string;
+ tier: string;
+ results: Record<string, { skipped?: boolean; reason?: string; error?: string; [key: string]: unknown } | undefined>;
+}
+
+export interface ConsolidationStatusInput {
+ now: Date;
+ enabled: boolean;
+ llmConfigured: boolean;
+ summaries: number;
+ recurringPatterns: number;
+ semanticFacts: number;
+ procedures: number;
+ relations: number;
+ lastRun: ConsolidationRunRecord | null;
+}
+
+export interface ConsolidationTier {
+ id: "semantic" | "procedural" | "relations";
+ label: string;
+ from: string;
+ count: number;
+ state: TierState;
+ detail: string;
+}
+
+export interface ConsolidationStatus {
+ enabled: boolean;
+ llmConfigured: boolean;
+ lastRunAt: string | null;
+ tiers: ConsolidationTier[];
+}
+
+function ago(now: Date, iso: string): string {
+ const seconds = Math.max(0, Math.round((now.getTime() - Date.parse(iso)) / 1000));
+ if (Number.isNaN(seconds)) return "at an unknown time";
+ if (seconds < 90) return "just now";
+ const minutes = Math.round(seconds / 60);
+ if (minutes < 90) return `${minutes} min ago`;
+ const hours = Math.round(minutes / 60);
+ if (hours < 48) return `${hours} h ago`;
+ return `${Math.round(hours / 24)} days ago`;
+}
+
+function fromLastRun(
+ input: ConsolidationStatusInput,
+ tier: "semantic" | "procedural",
+): { state: TierState; detail: string } | null {
+ const result = input.lastRun?.results?.[tier];
+ if (!input.lastRun || !result) return null;
+ const when = ago(input.now, input.lastRun.at);
+ if (result.error) return { state: "error", detail: `Last run ${when} failed: ${result.error}` };
+ if (result.skipped) return null;
+ const made =
+ tier === "semantic"
+ ? `${Number(result["newFacts"] ?? 0)} new facts`
+ : `${Number(result["newProcedures"] ?? 0)} new procedures`;
+ return { state: "ran", detail: `Last run ${when}: ${made}.` };
+}
+
+export function describeConsolidation(input: ConsolidationStatusInput): ConsolidationStatus {
+ const off = !input.enabled || !input.llmConfigured;
+ const offDetail = !input.llmConfigured
+ ? "Off: add an LLM provider key to ~/.agentmemory/.env and restart; consolidation turns on with it."
+ : "Off: CONSOLIDATION_ENABLED is set to false; remove it or set it to true and restart.";
+
+ const semanticWaiting = input.summaries < SEMANTIC_MIN_SUMMARIES;
+ const semantic: ConsolidationTier = {
+ id: "semantic",
+ label: "Semantic facts",
+ from: "Durable facts distilled from session summaries",
+ count: input.semanticFacts,
+ ...(off
+ ? { state: "off", detail: offDetail }
+ : fromLastRun(input, "semantic") ??
+ (semanticWaiting
+ ? {
+ state: "waiting",
+ detail: `Waiting: ${input.summaries} of ${SEMANTIC_MIN_SUMMARIES} session summaries needed.`,
+ }
+ : { state: "ready", detail: "Ready: runs when the next session ends." }))
+ };
+
+ const proceduralWaiting = input.recurringPatterns < PROCEDURAL_MIN_PATTERNS;
+ const procedural: ConsolidationTier = {
+ id: "procedural",
+ label: "Procedures",
+ from: "Repeatable workflows, from finished sessions and from patterns that recur across sessions",
+ count: input.procedures,
+ ...(off
+ ? { state: "off", detail: offDetail }
+ : fromLastRun(input, "procedural") ??
+ (proceduralWaiting
+ ? {
+ state: "waiting",
+ detail:
+ `Extracted from each finished session with a summary and 3+ observations. ` +
+ `The cross-session pass also needs ${PROCEDURAL_MIN_PATTERNS} pattern memories seen in ` +
+ `${PROCEDURAL_MIN_SESSIONS_PER_PATTERN}+ sessions (you have ${input.recurringPatterns}).`,
+ }
+ : { state: "ready", detail: "Ready: runs when the next session ends." }))
+ };
+
+ const relations: ConsolidationTier = {
+ id: "relations",
+ label: "Relations",
+ from: "Links between memories (supersedes, extends, contradicts)",
+ count: input.relations,
+ state: input.relations > 0 ? "ran" : "waiting",
+ detail:
+ input.relations > 0
+ ? `${input.relations} links saved.`
+ : "None yet: links are saved when a memory updates or relates to another.",
+ };
+
+ return {
+ enabled: input.enabled,
+ llmConfigured: input.llmConfigured,
+ lastRunAt: input.lastRun?.at ?? null,
+ tiers: [semantic, procedural, relations],
+ };
+}
diff --git a/src/index.ts b/src/index.ts
index 60138963..ff465afe 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -549,7 +549,7 @@ async function main() {
`Ready. ${embeddingProvider ? "Triple-stream (BM25+Vector+Graph)" : "BM25+Graph"} search active.`,
);
bootLog(
- `REST API: 131 endpoints at http://localhost:${config.restPort}/agentmemory/*`,
+ `REST API: 132 endpoints at http://localhost:${config.restPort}/agentmemory/*`,
);
bootLog(
`MCP surface (opt-in via \`npx @agentmemory/mcp\`): ${getAllTools().length} tools · 6 resources · 3 prompts`,
diff --git a/src/triggers/api.ts b/src/triggers/api.ts
index 78426e80..d8a447f2 100644
--- a/src/triggers/api.ts
+++ b/src/triggers/api.ts
@@ -10,6 +10,7 @@ import { getLatestHealth } from "../health/monitor.js";
import type { MetricsStore } from "../eval/metrics-store.js";
import type { ResilientProvider } from "../providers/resilient.js";
import { III_PINNED_VERSION, VERSION } from "../version.js";
+import { CONSOLIDATION_COUNTS_REUSE_MS, CONSOLIDATION_LAST_RUN_KEY, PROCEDURAL_MIN_SESSIONS_PER_PATTERN, describeConsolidation, type ConsolidationRunRecord } from "../functions/consolidation-status.js";
import { UNINDEXED_SCAN_REUSE_MS, evaluateStatus, prefersHtml, renderStatusHtml, singleFlight, type GraphStatsInput } from "../functions/status.js";
import { findUnindexedObservations, getSearchIndex, getVectorIndex } from "../functions/search.js";
import { timingSafeCompare } from "../auth.js";
@@ -2081,6 +2082,11 @@ export function registerApiTriggers(
: undefined;
const offset =
Number.isInteger(parsedOffset) && parsedOffset >= 0 ? parsedOffset : 0;
+ filtered.sort((a, b) =>
+ (b.updatedAt || b.createdAt || "").localeCompare(
+ a.updatedAt || a.createdAt || "",
+ ),
+ );
const sliced =
limit !== undefined ? filtered.slice(offset, offset + limit) : filtered;
@@ -2150,6 +2156,54 @@ export function registerApiTriggers(
config: { api_path: "/agentmemory/procedural", http_method: "GET" },
});
+ const sharedConsolidationCounts = singleFlight(async () => {
+ const [summaries, memories, semantic, procedural, relations] = await Promise.all([
+ kv.list(KV.summaries).catch(() => []),
+ kv.list<import("../types.js").Memory>(KV.memories).catch(() => []),
+ kv.list(KV.semantic).catch(() => []),
+ kv.list(KV.procedural).catch(() => []),
+ kv.list(KV.relations).catch(() => []),
+ ]);
+ return {
+ summaries: summaries.length,
+ recurringPatterns: memories.filter(
+ (m) =>
+ m.isLatest &&
+ m.type === "pattern" &&
+ (m.sessionIds?.length ?? 0) >= PROCEDURAL_MIN_SESSIONS_PER_PATTERN,
+ ).length,
+ semanticFacts: semantic.length,
+ procedures: procedural.length,
+ relations: relations.length,
+ };
+ }, CONSOLIDATION_COUNTS_REUSE_MS);
+
+ sdk.registerFunction("api::consolidation-status",
+ async (req: HttpRequest): Promise<Response> => {
+ const authErr = checkAuth(req, secret);
+ if (authErr) return authErr;
+ const [counts, lastRun] = await Promise.all([
+ sharedConsolidationCounts(),
+ kv.get<ConsolidationRunRecord>(KV.config, CONSOLIDATION_LAST_RUN_KEY).catch(() => null),
+ ]);
+ return {
+ status_code: 200,
+ body: describeConsolidation({
+ now: new Date(),
+ enabled: isConsolidationEnabled(),
+ llmConfigured: detectLlmProviderKind() === "llm",
+ ...counts,
+ lastRun,
+ }),
+ };
+ },
+ );
+ sdk.registerTrigger({
+ type: "http",
+ function_id: "api::consolidation-status",
+ config: { api_path: "/agentmemory/consolidation/status", http_method: "GET" },
+ });
+
sdk.registerFunction("api::relations-list",
async (req: HttpRequest): Promise<Response> => {
const authErr = checkAuth(req, secret);
diff --git a/src/triggers/events.ts b/src/triggers/events.ts
index 3ce2d4be..1e248236 100644
--- a/src/triggers/events.ts
+++ b/src/triggers/events.ts
@@ -1,11 +1,13 @@
import { TriggerAction, type IIIClient } from "iii-sdk";
-import type { CompressedObservation, HookPayload, Session } from "../types.js";
+import type { CompressedObservation, HookPayload, Memory, Session } from "../types.js";
import { KV, STREAM } from "../state/schema.js";
import { StateKV } from "../state/kv.js";
import { isReflectEnabled } from "../functions/slots.js";
import {
+ detectLlmProviderKind,
getAgentId,
getConsolidationCooldownMs,
+ isAgentScopeIsolated,
isConsolidationEnabled,
} from "../config.js";
import { logger } from "../logger.js";
@@ -142,6 +144,9 @@ export function registerEventTriggers(sdk: IIIClient, kv: StateKV): void {
if (await consolidationDue(kv)) {
fireVoid("mem::consolidate-pipeline", { tier: "all", force: true });
fireVoid("mem::auto-crystallize", { olderThanDays: 0 });
+ if (detectLlmProviderKind() === "llm") {
+ fireVoid("mem::skill-extract", { sessionId: data.sessionId });
+ }
}
}
return summary;
@@ -177,26 +182,29 @@ export function registerEventTriggers(sdk: IIIClient, kv: StateKV): void {
old_value?: Session;
new_value?: Session;
}) => {
- if (payload.event_type === "delete") return { skipped: true };
+ if (isOutOfAgentScope(payload.new_value ?? payload.old_value)) {
+ return { emitted: false };
+ }
+ if (isStateDelete(payload)) {
+ await sendViewerEvent(sdk, `session-deleted-${payload.key}-${Date.now()}`, "session.deleted", {
+ sessionId: payload.key,
+ });
+ return { emitted: true };
+ }
+ if (payload.new_value) {
+ await sendViewerEvent(sdk, `session-updated-${payload.key}-${Date.now()}`, "session.updated", {
+ session: payload.new_value,
+ });
+ }
const oldCount = payload.old_value?.observationCount ?? 0;
const newCount = payload.new_value?.observationCount ?? 0;
- if (newCount <= oldCount) return { skipped: true };
+ if (newCount <= oldCount) return { emitted: Boolean(payload.new_value) };
- await sdk.trigger({
- function_id: "stream::send",
- payload: {
- stream_name: STREAM.name,
- group_id: STREAM.viewerGroup,
- id: `session-activity-${payload.key}-${Date.now()}`,
- type: "session.activity",
- data: {
- sessionId: payload.key,
- observationCount: newCount,
- delta: newCount - oldCount,
- updatedAt: payload.new_value?.updatedAt ?? new Date().toISOString(),
- },
- },
- action: TriggerAction.Void(),
+ await sendViewerEvent(sdk, `session-activity-${payload.key}-${Date.now()}`, "session.activity", {
+ sessionId: payload.key,
+ observationCount: newCount,
+ delta: newCount - oldCount,
+ updatedAt: payload.new_value?.updatedAt ?? new Date().toISOString(),
});
return { emitted: true };
@@ -207,4 +215,61 @@ export function registerEventTriggers(sdk: IIIClient, kv: StateKV): void {
function_id: "event::session::observation-count-changed",
config: { scope: KV.sessions },
});
+
+ sdk.registerFunction(
+ "event::memory::changed",
+ async (payload: {
+ key: string;
+ event_type: string;
+ old_value?: Memory;
+ new_value?: Memory;
+ }) => {
+ if (isOutOfAgentScope(payload.new_value ?? payload.old_value)) {
+ return { emitted: false };
+ }
+ const deleted = isStateDelete(payload);
+ const memory = payload.new_value;
+ await sendViewerEvent(
+ sdk,
+ `memory-${deleted ? "deleted" : "updated"}-${payload.key}-${Date.now()}`,
+ deleted ? "memory.deleted" : "memory.updated",
+ deleted
+ ? { memoryId: payload.key }
+ : {
+ memoryId: payload.key,
+ type: memory?.type,
+ title: memory?.title,
+ isLatest: memory?.isLatest,
+ updatedAt: memory?.updatedAt,
+ },
+ );
+ return { emitted: true };
+ },
+ );
+ sdk.registerTrigger({
+ type: "state",
+ function_id: "event::memory::changed",
+ config: { scope: KV.memories },
+ });
+}
+
+function isStateDelete(payload: { event_type: string; new_value?: unknown }): boolean {
+ return payload.event_type === "state:deleted" || !payload.new_value;
+}
+
+function isOutOfAgentScope(record: { agentId?: string } | undefined): boolean {
+ return isAgentScopeIsolated() && record?.agentId !== getAgentId();
+}
+
+async function sendViewerEvent(
+ sdk: IIIClient,
+ id: string,
+ type: string,
+ data: Record<string, unknown>,
+): Promise<void> {
+ await sdk.trigger({
+ function_id: "stream::send",
+ payload: { stream_name: STREAM.name, group_id: STREAM.viewerGroup, id, type, data },
+ action: TriggerAction.Void(),
+ });
}
diff --git a/src/viewer/index.html b/src/viewer/index.html
index 8fc60ade..38e25649 100644
--- a/src/viewer/index.html
+++ b/src/viewer/index.html
@@ -221,6 +221,15 @@
.ws-status.connected::before { animation: none; }
}
+ .layer-row { padding: 12px 0; border-bottom: 1px solid var(--border-light); }
+ .layer-row:last-child { border-bottom: none; }
+ .layer-head { display: flex; align-items: baseline; gap: 10px; }
+ .layer-name { font-family: var(--font-display); font-weight: 700; font-size: 14px; color: var(--ink); }
+ .layer-count { font-family: var(--font-mono); font-size: 13px; color: var(--ink); margin-left: auto; }
+ .layer-from { font-size: 12px; color: var(--ink-muted); margin-top: 2px; }
+ .layer-detail { font-size: 12px; color: var(--ink-faint); margin-top: 4px; line-height: 1.5; }
+ .layer-preview { margin: 6px 0 0; padding-left: 16px; font-size: 12px; color: var(--ink-muted); }
+ .layer-preview li { margin-bottom: 2px; }
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
@@ -1014,9 +1023,6 @@
.procedure-item:last-child { border-bottom: none; }
.procedure-steps { margin: 6px 0 0 16px; font-size: 12px; color: var(--ink-muted); }
.procedure-steps li { margin-bottom: 2px; }
- .consolidation-row { display: flex; justify-content: space-between; padding: 4px 0; font-size: 12px; font-family: var(--font-ui); }
- .consolidation-row .cl { color: var(--ink-muted); }
- .consolidation-row .cv { color: var(--ink); font-weight: 600; font-family: var(--font-mono); }
.activity-feed-item { display: flex; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border-light); font-size: 13px; }
.activity-feed-item:last-child { border-bottom: none; }
@@ -1236,6 +1242,26 @@
return s;
}
+ function summaryText(v) {
+ if (!v) return '';
+ if (typeof v === 'string') return v;
+ if (typeof v === 'object') return v.title || v.narrative || (Array.isArray(v.keyDecisions) ? v.keyDecisions.join('; ') : '');
+ return String(v);
+ }
+
+ function asTags(v) {
+ if (Array.isArray(v)) return v.map(String);
+ if (typeof v === 'string') return v.split(',').map(function(t) { return t.trim(); }).filter(Boolean);
+ return [];
+ }
+
+ function setViewHtml(el, html) {
+ if (el.__renderedHtml === html && el.firstElementChild && el.firstElementChild === el.__renderedFirst) return;
+ el.innerHTML = html;
+ el.__renderedHtml = html;
+ el.__renderedFirst = el.firstElementChild;
+ }
+
function truncate(s, n) {
if (!s) return '';
return s.length > n ? s.slice(0, n) + '...' : s;
@@ -1471,33 +1497,83 @@
}
}
+ var dashboardLoading = false;
+ var dashboardReloadPending = false;
+ var dashboardReloadTimer = null;
+ var DASHBOARD_RELOAD_COALESCE_MS = 2000;
+
+ function scheduleDashboardReload() {
+ if (dashboardReloadTimer) return;
+ dashboardReloadTimer = setTimeout(function() {
+ dashboardReloadTimer = null;
+ if (state.activeTab === 'dashboard') loadDashboard();
+ }, DASHBOARD_RELOAD_COALESCE_MS);
+ }
+
+ var STALE_RETRY_MS = 15000;
+ var staleRetryTimer = null;
+ function scheduleStaleRetry() {
+ if (staleRetryTimer) return;
+ staleRetryTimer = setTimeout(function() {
+ staleRetryTimer = null;
+ if (state.activeTab === 'dashboard' && state.dashboard.stale) loadDashboard();
+ }, STALE_RETRY_MS);
+ }
+
+ function listOr(previous, result, key, fallbackKey) {
+ if (result === null || result === undefined) return previous || [];
+ return result[key] || (fallbackKey && result[fallbackKey]) || [];
+ }
+
async function loadDashboard() {
+ if (dashboardLoading) {
+ dashboardReloadPending = true;
+ return;
+ }
+ dashboardLoading = true;
var el = document.getElementById('view-dashboard');
- if (!state.dashboard.loaded) el.innerHTML = '<div class="loading">Loading dashboard...</div>';
+ var d = state.dashboard;
+ if (!d.loaded) el.innerHTML = '<div class="loading">Loading dashboard...</div>';
+ var seqAtStart = liveSessionSeq;
+ var countToken = ++memoryCountRequest;
try {
var results = await Promise.all([
api('health', { readErrorBody: true }),
apiGet('sessions'),
- apiGet('memories?latest=true&limit=500'),
+ apiGet('memories?latest=true&count=true'),
apiGet('graph/stats'),
apiGet('audit?limit=5'),
apiGet('semantic'),
apiGet('procedural'),
apiGet('relations'),
apiGet('lessons'),
- apiGet('crystals')
+ apiGet('crystals'),
+ apiGet('consolidation/status')
]);
- state.dashboard.health = results[0];
- state.dashboard.sessions = (results[1] && results[1].sessions) || [];
- state.dashboard.memories = (results[2] && results[2].memories) || [];
- state.dashboard.graphStats = results[3];
- state.dashboard.recentAudit = (results[4] && results[4].entries) || [];
- state.dashboard.semantic = (results[5] && results[5].facts) || (results[5] && results[5].semantic) || [];
- state.dashboard.procedural = (results[6] && results[6].procedures) || (results[6] && results[6].procedural) || [];
- state.dashboard.lessons = (results[8] && results[8].lessons) || [];
- state.dashboard.crystals = (results[9] && results[9].crystals) || [];
- state.dashboard.relations = (results[7] && results[7].relations) || [];
- state.dashboard.loaded = true;
+ var sessionsOk = !!(results[1] && Array.isArray(results[1].sessions));
+ if (!sessionsOk && !d.loaded) {
+ el.innerHTML =
+ '<div class="loading" style="color:var(--accent);">Could not reach the agentmemory API. ' +
+ 'The server may be restarting.<br><br>' +
+ '<button class="btn btn-primary" data-action="refresh-dashboard">Retry</button></div>';
+ return;
+ }
+ d.stale = results.some(function(r) { return r === null; });
+ if (d.stale) scheduleStaleRetry();
+ if (results[0]) d.health = results[0];
+ if (sessionsOk) d.sessions = replaySessionEvents(results[1].sessions, seqAtStart);
+ if (countToken === memoryCountRequest && results[2] && typeof results[2].total === 'number') {
+ d.memoryTotal = results[2].total;
+ }
+ if (results[3]) d.graphStats = results[3];
+ d.recentAudit = listOr(d.recentAudit, results[4], 'entries');
+ d.semantic = listOr(d.semantic, results[5], 'facts', 'semantic');
+ d.procedural = listOr(d.procedural, results[6], 'procedures', 'procedural');
+ d.relations = listOr(d.relations, results[7], 'relations');
+ d.lessons = listOr(d.lessons, results[8], 'lessons');
+ d.crystals = listOr(d.crystals, results[9], 'crystals');
+ if (results[10] && results[10].tiers) d.consolidation = results[10];
+ d.loaded = true;
renderDashboard();
} catch (err) {
// Without this catch, any uncaught error in the await Promise.all
@@ -1517,9 +1593,71 @@
'violations, please open an issue with the agentmemory version ' +
'(top-right of the viewer) and the violation text.' +
'</span></div>';
+ } finally {
+ dashboardLoading = false;
+ if (dashboardReloadPending) {
+ dashboardReloadPending = false;
+ scheduleDashboardReload();
+ }
}
}
+ function estimateTokenSavings(sessions, tokenBudget) {
+ var full = 0;
+ var injected = 0;
+ sessions.forEach(function(s) {
+ var sessionFull = (s.observationCount || 0) * 80;
+ if (sessionFull <= 0) return;
+ full += sessionFull;
+ injected += Math.min(tokenBudget, sessionFull);
+ });
+ var saved = Math.max(0, full - injected);
+ return { percent: full > 0 ? Math.round((saved / full) * 100) : 0, saved: saved };
+ }
+
+ var LAYER_STATE_BADGES = { off: 'badge-muted', waiting: 'badge-yellow', ready: 'badge-blue', ran: 'badge-green', error: 'badge-red' };
+ var LAYER_STATE_LABELS = { off: 'off', waiting: 'waiting', ready: 'ready', ran: 'active', error: 'failed' };
+
+ function layerPreview(tierId, semFacts, procItems) {
+ var items = [];
+ if (tierId === 'semantic') {
+ items = semFacts.slice(0, 3).map(function(f) { return f.fact || f.content || f.title || ''; });
+ } else if (tierId === 'procedural') {
+ items = procItems.slice(0, 3).map(function(p) {
+ var steps = p.steps && p.steps.length ? ' (' + p.steps.length + ' steps)' : '';
+ return (p.name || p.title || 'Procedure') + steps;
+ });
+ }
+ items = items.filter(Boolean);
+ if (!items.length) return '';
+ return '<ul class="layer-preview">' + items.map(function(t) { return '<li>' + esc(truncate(t, 140)) + '</li>'; }).join('') + '</ul>';
+ }
+
+ function renderMemoryLayers(status, semFacts, procItems) {
+ var html = '<div class="card" style="margin-top:16px;"><div class="card-title">Memory layers';
+ if (status && status.lastRunAt) {
+ html += '<span style="float:right;font-size:10px;color:var(--ink-faint);font-family:var(--font-mono);">consolidated ' + esc(formatTime(status.lastRunAt)) + '</span>';
+ }
+ html += '</div>';
+ if (!status || !status.tiers) {
+ html += '<div style="font-size:13px;color:var(--ink-faint);">Consolidation status did not load. Semantic facts: ' + semFacts.length + ', procedures: ' + procItems.length + '.</div></div>';
+ return html;
+ }
+ html += '<div style="font-size:12px;color:var(--ink-muted);margin-bottom:8px;line-height:1.5;">Raw observations stay searchable as they are. Consolidation turns them into these longer-lived layers.</div>';
+ status.tiers.forEach(function(t) {
+ html += '<div class="layer-row">' +
+ '<div class="layer-head"><span class="layer-name">' + esc(t.label) + '</span>' +
+ '<span class="layer-count">' + esc(String(t.count)) + '</span>' +
+ '<span class="badge ' + (LAYER_STATE_BADGES[t.state] || 'badge-muted') + '">' + esc(LAYER_STATE_LABELS[t.state] || t.state) + '</span></div>' +
+ '<div class="layer-from">' + esc(t.from) + '</div>' +
+ '<div class="layer-detail">' + esc(t.detail) + '</div>' +
+ layerPreview(t.id, semFacts, procItems) +
+ '</div>';
+ });
+ html += '</div>';
+ return html;
+ }
+
function renderDashboard() {
var el = document.getElementById('view-dashboard');
var d = state.dashboard;
@@ -1546,14 +1684,18 @@
'<div style="margin-top:10px;"><a class="empty-link" href="https://github.com/rohitg00/agentmemory#quick-start" target="_blank" rel="noopener" style="font-size:12px;">Or: wire up your real agent &rarr;</a></div>' +
'</div>';
}
+ if (d.stale) {
+ html += '<div class="card" style="margin-bottom:14px;padding:10px 16px;font-size:12px;color:var(--ink-muted);border-left:3px solid var(--yellow);">' +
+ 'Some panels could not refresh; showing the last data that loaded. Retrying automatically.</div>';
+ }
html += '<div class="stats-grid">';
html += '<div class="stat-card" data-action="goto-tab" data-tab="sessions" role="link" tabindex="0"><div class="label">Sessions</div><div class="value">' + d.sessions.length + '</div><div class="sub">' + activeSessions + ' active</div></div>';
- html += '<div class="stat-card" data-action="goto-tab" data-tab="memories" role="link" tabindex="0"><div class="label">Memories</div><div class="value">' + d.memories.length + '</div><div class="sub">latest versions</div></div>';
+ html += '<div class="stat-card" data-action="goto-tab" data-tab="memories" role="link" tabindex="0"><div class="label">Memories</div><div class="value">' + (d.memoryTotal || 0) + '</div><div class="sub">latest versions</div></div>';
var lessonCount = (d.lessons || []).length;
var crystalCount = (d.crystals || []).length;
html += '<div class="stat-card" data-action="goto-tab" data-tab="lessons" role="link" tabindex="0"><div class="label">Lessons</div><div class="value">' + lessonCount + '</div><div class="sub">confidence-scored</div></div>';
html += '<div class="stat-card" data-action="goto-tab" data-tab="crystals" role="link" tabindex="0"><div class="label">Crystals</div><div class="value">' + crystalCount + '</div><div class="sub">action digests</div></div>';
- html += '<div class="stat-card" data-action="goto-tab" data-tab="graph" role="link" tabindex="0"><div class="label">Graph Nodes</div><div class="value">' + nodeCount + '</div><div class="sub">' + edgeCount + ' edges</div></div>';
+ html += '<div class="stat-card" data-action="goto-tab" data-tab="graph" role="link" tabindex="0"><div class="label">Graph Nodes</div><div class="value">' + nodeCount + '</div><div class="sub">' + edgeCount + ' edges' + (gs.fromSnapshot && gs.updatedAt ? ' · snapshot ' + esc(shortTime(gs.updatedAt)) : '') + '</div></div>';
html += '<div class="stat-card"><div class="label">Health</div><div class="value"><div class="health-bar"><span class="health-dot ' + dotClass + '"></span> ' + esc(healthStatus) + '</div></div>';
html += '<div class="sub">' + esc(snap.connectionState || 'unknown') + '</div></div>';
var totalCalls = fMetrics.reduce(function(a, m) { return a + (m.totalCalls || 0); }, 0);
@@ -1563,13 +1705,10 @@
html += '<div class="stat-card"><div class="label">Circuit Breaker</div><div class="value"><span class="cb-indicator ' + cbClass + '">' + esc(cb.state) + '</span></div>';
html += '<div class="sub">' + (cb.failures || 0) + ' failures</div></div>';
}
- var totalObs = d.sessions.reduce(function(a, s) { return a + (s.observationCount || 0); }, 0);
var tokenBudget = parseInt(new URLSearchParams(window.location.search).get('tokenBudget') || '2000', 10) || 2000;
- var estFull = totalObs * 80;
- var estInjected = d.sessions.length * tokenBudget;
- var savings = estFull > 0 ? Math.round((1 - estInjected / Math.max(estFull, 1)) * 100) : 0;
- if (savings < 0) savings = 0;
- var tokensSaved = Math.max(0, estFull - estInjected);
+ var tokenEstimate = estimateTokenSavings(d.sessions, tokenBudget);
+ var savings = tokenEstimate.percent;
+ var tokensSaved = tokenEstimate.saved;
// Rate: $0.30 per 1K tokens (mid-tier model baseline)
var costDollars = tokensSaved / 1000 * 0.3;
var costCents = Math.round(costDollars * 100);
@@ -1712,56 +1851,7 @@
var relItems = d.relations || [];
html += '<hr class="section-rule">';
- html += '<div class="two-col">';
-
- html += '<div class="card"><div class="card-title">Semantic Memory</div>';
- if (semFacts.length === 0) {
- html += '<div style="font-size:13px;color:var(--ink-faint);font-style:italic;">No semantic facts yet. Observations will be consolidated into semantic memories over time.</div>';
- } else {
- semFacts.slice(0, 5).forEach(function(f) {
- var conf = typeof f.confidence === 'number' ? Math.round(f.confidence * 100) : null;
- var str = typeof f.strength === 'number' ? Math.round(f.strength * 100) : null;
- var barColor = (str || 0) > 70 ? 'var(--green)' : (str || 0) > 40 ? 'var(--yellow)' : 'var(--red)';
- html += '<div class="memory-fact">';
- html += '<span style="color:var(--ink);">' + esc(f.fact || f.content || f.title || 'Fact') + '</span>';
- html += '<span style="display:flex;align-items:center;gap:6px;">';
- if (str !== null) html += '<span class="strength-bar" style="width:40px;"><span class="fill" style="width:' + str + '%;background:' + barColor + '"></span></span>';
- if (conf !== null) html += '<span style="font-size:10px;font-family:var(--font-mono);color:var(--ink-faint);">' + conf + '%</span>';
- html += '</span></div>';
- });
- }
- html += '</div>';
-
- html += '<div class="card"><div class="card-title">Procedural Memory</div>';
- if (procItems.length === 0) {
- html += '<div style="font-size:13px;color:var(--ink-faint);font-style:italic;">No procedures yet. Repeated patterns will be extracted as procedures.</div>';
- } else {
- procItems.slice(0, 5).forEach(function(p) {
- html += '<div class="procedure-item">';
- html += '<div style="font-weight:600;color:var(--ink);font-family:var(--font-display);font-size:13px;">' + esc(p.name || p.title || 'Procedure') + '</div>';
- if (p.trigger || p.triggerCondition) html += '<div style="font-size:11px;color:var(--ink-faint);font-family:var(--font-mono);margin-top:2px;">Trigger: ' + esc(p.trigger || p.triggerCondition) + '</div>';
- if (p.frequency) html += '<div style="font-size:11px;color:var(--ink-faint);margin-top:2px;">Freq: ' + p.frequency + '</div>';
- if (p.steps && p.steps.length > 0) {
- html += '<ol class="procedure-steps">';
- p.steps.slice(0, 4).forEach(function(s) { html += '<li>' + esc(typeof s === 'string' ? s : s.description || s.action || JSON.stringify(s)) + '</li>'; });
- if (p.steps.length > 4) html += '<li style="color:var(--ink-faint);font-style:italic;">+ ' + (p.steps.length - 4) + ' more...</li>';
- html += '</ol>';
- }
- html += '</div>';
- });
- }
- html += '</div>';
-
- html += '</div>';
-
- html += '<div class="card" style="margin-top:16px;"><div class="card-title">Consolidation Status</div>';
- html += '<div class="consolidation-row"><span class="cl">Semantic facts</span><span class="cv">' + semFacts.length + '</span></div>';
- html += '<div class="consolidation-row"><span class="cl">Procedures</span><span class="cv">' + procItems.length + '</span></div>';
- html += '<div class="consolidation-row"><span class="cl">Relations</span><span class="cv">' + relItems.length + '</span></div>';
- if (semFacts.length === 0 && procItems.length === 0 && relItems.length === 0) {
- html += '<div style="font-size:12px;color:var(--ink-faint);margin-top:8px;line-height:1.5;">Consolidation distills session observations into durable facts and repeatable procedures. It runs on a schedule when <code>CONSOLIDATION_ENABLED=true</code> and an LLM provider key are set, or on demand via <code>memory_consolidate</code>.</div>';
- }
- html += '</div>';
+ html += renderMemoryLayers(d.consolidation, semFacts, procItems);
if (relItems.length > 0) {
html += '<div class="card" style="margin-top:16px;"><div class="card-title">Memory Relations</div>';
@@ -1778,23 +1868,14 @@
}
html += '<div style="text-align:center;margin-top:20px;"><button class="btn btn-primary" data-action="refresh-dashboard">Refresh</button>';
- html += '<span style="font-size:10px;color:var(--ink-faint);margin-left:10px;font-family:var(--font-mono);text-transform:uppercase;letter-spacing:0.08em;">Auto-refresh 30s</span></div>';
+ html += '</div>';
- el.innerHTML = html;
+ setViewHtml(el, html);
}
- var dashboardTimer = null;
function refreshDashboard() {
- state.dashboard.loaded = false;
loadDashboard();
}
- function startDashboardAutoRefresh() {
- if (dashboardTimer) clearInterval(dashboardTimer);
- dashboardTimer = setInterval(function() {
- if (pollTimer) return;
- if (state.activeTab === 'dashboard') refreshDashboard();
- }, 30000);
- }
var graphSim = { nodes: [], edges: [], running: false, canvas: null, ctx: null, raf: null, panX: 0, panY: 0, zoom: 1, dragNode: null, mouseX: 0, mouseY: 0, tickCount: 0, quietTicks: 0 };
function wakeGraphSim() {
@@ -2314,12 +2395,39 @@
graphSim.raf = requestAnimationFrame(runSimulation);
}
+ var graphRebuilding = false;
+
async function rebuildGraph() {
+ if (graphRebuilding) return;
+ var ok = window.confirm(
+ 'Rebuild the knowledge graph from every stored observation?\n\n' +
+ 'This replays the whole corpus and can take several minutes on a large store. ' +
+ 'With an LLM provider configured it also spends provider tokens.'
+ );
+ if (!ok) return;
+ graphRebuilding = true;
var sb = document.getElementById('graph-sidebar');
if (sb) sb.innerHTML = '<h3>Graph</h3><p style="font-size:12px;color:var(--ink-faint);font-style:italic;">Rebuilding graph from observations...</p>';
- await apiPost('graph/build', {});
- state.graph.loaded = false;
- loadGraph();
+ try {
+ var result = await apiPost('graph/build', {});
+ state.graph.rebuildResult = result && result.success
+ ? 'Rebuilt from ' + (result.sessions || 0) + ' sessions: ' + (result.nodes || 0) + ' nodes, ' + (result.edges || 0) + ' edges.'
+ : 'Graph rebuild failed or graph extraction is disabled. Check the server log.';
+ } finally {
+ graphRebuilding = false;
+ state.graph.loaded = false;
+ await loadGraph();
+ if (state.graph.queryError && state.graph.rebuildResult) {
+ state.graph.rebuildResult = 'The rebuild finished, but the graph could not be refreshed. Reload the Graph tab.';
+ }
+ var sidebar = document.getElementById('graph-sidebar');
+ if (sidebar && state.graph.rebuildResult) {
+ var note = document.createElement('p');
+ note.style.cssText = 'font-size:12px;color:var(--ink-muted);margin:8px 0;';
+ note.textContent = state.graph.rebuildResult;
+ sidebar.insertBefore(note, sidebar.firstChild);
+ }
+ }
}
function drawNodeShape(ctx, x, y, r, type) {
@@ -2745,7 +2853,7 @@
}
var __focus = captureSearchFocus(['mem-search']);
- el.innerHTML = html;
+ setViewHtml(el, html);
var searchInput = document.getElementById('mem-search');
if (searchInput) {
@@ -2771,7 +2879,6 @@
async function confirmDeleteMemory(id) {
closeModal();
await apiDelete('governance/memories', { memoryIds: [id], reason: 'Deleted via viewer' });
- state.memories.loaded = false;
loadMemories();
}
@@ -3145,14 +3252,20 @@
html += '</div>';
- el.innerHTML = html;
+ setViewHtml(el, html);
}
async function loadSessions() {
var el = document.getElementById('view-sessions');
if (!state.sessions.loaded) el.innerHTML = '<div class="loading">Loading sessions...</div>';
+ var seqAtStart = liveSessionSeq;
var result = await apiGet('sessions');
- state.sessions.items = (result && result.sessions) || [];
+ if (result && Array.isArray(result.sessions)) {
+ state.sessions.items = replaySessionEvents(result.sessions, seqAtStart);
+ } else if (!state.sessions.loaded) {
+ state.sessions.items = [];
+ }
+ state.sessions.detailStale = true;
state.sessions.loaded = true;
renderSessions();
}
@@ -3174,7 +3287,7 @@
html += '<div class="session-item' + (selected ? ' selected' : '') + '"' + (id ? ' data-action="select-session" data-session-id="' + esc(id) + '" role="button" tabindex="0" aria-expanded="' + (selected ? 'true' : 'false') + '"' : '') + '>';
html += '<div class="session-top"><span class="session-project">' + esc(sessionDisplayName(s)) + '</span>';
html += '<span class="badge ' + statusBadge + '">' + esc(s.status) + '</span></div>';
- var preview = s.firstPrompt || s.summary || '';
+ var preview = s.firstPrompt || summaryText(s.summary) || '';
if (preview) {
html += '<div class="session-preview" style="font-size:13px;color:var(--ink);margin:4px 0;line-height:1.4;">' + esc(truncate(preview, 140)) + '</div>';
}
@@ -3186,9 +3299,16 @@
}
html += '</div>';
html += '<div id="session-detail"></div></div>';
- el.innerHTML = html;
+ setViewHtml(el, html);
- if (state.sessions.selectedId) renderSessionDetail();
+ if (!state.sessions.selectedId) return;
+ var cached = state.sessions.detailCache;
+ if (cached && cached.id === state.sessions.selectedId && !state.sessions.detailStale) {
+ var panel = document.getElementById('session-detail');
+ if (panel && panel.innerHTML !== cached.html) panel.innerHTML = cached.html;
+ return;
+ }
+ renderSessionDetail();
}
function selectSession(id) {
@@ -3208,6 +3328,7 @@
}
}
+ var sessionDetailRequest = 0;
async function renderSessionDetail() {
var panel = document.getElementById('session-detail');
if (!panel) return;
@@ -3215,9 +3336,15 @@
var id = sessionId(s);
if (!s || !id) { panel.innerHTML = ''; return; }
- panel.innerHTML = '<div class="detail-panel"><h3>Loading session details…</h3></div>';
+ state.sessions.detailStale = false;
+ var request = ++sessionDetailRequest;
+ var cached = state.sessions.detailCache;
+ if (!cached || cached.id !== id) {
+ panel.innerHTML = '<div class="detail-panel"><h3>Loading session details…</h3></div>';
+ }
var obsRes = await apiGet('observations?sessionId=' + encodeURIComponent(id));
+ if (request !== sessionDetailRequest || state.sessions.selectedId !== id) return;
var obs = (obsRes && obsRes.observations) || [];
var typeCounts = {};
@@ -3238,7 +3365,7 @@
var durationMs = s.endedAt ? new Date(s.endedAt).getTime() - new Date(s.startedAt).getTime() : 0;
var durationLabel = durationMs > 0 ? (durationMs < 60000 ? (durationMs / 1000).toFixed(1) + 's' : (durationMs / 60000).toFixed(1) + 'm') : '-';
- var preview = s.firstPrompt || s.summary || firstPromptFromObs || '';
+ var preview = s.firstPrompt || summaryText(s.summary) || firstPromptFromObs || '';
var html = '<div class="detail-panel">';
html += '<div style="display:flex;justify-content:space-between;align-items:flex-start;margin-bottom:12px;">';
@@ -3296,7 +3423,7 @@
html += '<div><span style="color:var(--ink-muted);">started:</span> ' + esc(formatTime(s.startedAt)) + '</div>';
if (s.endedAt) html += '<div><span style="color:var(--ink-muted);">ended:</span> ' + esc(formatTime(s.endedAt)) + '</div>';
if (s.model) html += '<div><span style="color:var(--ink-muted);">model:</span> ' + esc(s.model) + '</div>';
- if (s.tags && s.tags.length) html += '<div><span style="color:var(--ink-muted);">tags:</span> ' + s.tags.map(esc).join(', ') + '</div>';
+ if (asTags(s.tags).length) html += '<div><span style="color:var(--ink-muted);">tags:</span> ' + asTags(s.tags).map(esc).join(', ') + '</div>';
html += '</div></div>';
html += '<div style="display:flex;gap:8px;">';
@@ -3309,12 +3436,13 @@
html += '<button class="btn btn-primary" disabled>Summarize unavailable</button>';
}
html += '</div></div>';
- panel.innerHTML = html;
+ state.sessions.detailCache = { id: id, html: html };
+ panel = document.getElementById('session-detail');
+ if (panel) panel.innerHTML = html;
}
async function endSession(id) {
await apiPost('session/end', { sessionId: id });
- state.sessions.loaded = false;
loadSessions();
}
@@ -3343,7 +3471,7 @@
if (search) {
items = items.filter(function(l) {
- return (l.content + ' ' + l.context + ' ' + (l.tags || []).join(' ')).toLowerCase().indexOf(search) >= 0;
+ return (l.content + ' ' + l.context + ' ' + asTags(l.tags).join(' ')).toLowerCase().indexOf(search) >= 0;
});
}
@@ -3385,7 +3513,7 @@
if (l.context) html += '<div style="font-size:12px;color:var(--ink-muted);margin-bottom:8px;"><span style="font-family:var(--font-ui);font-size:10px;text-transform:uppercase;letter-spacing:0.1em;">Why learned</span><br>' + esc(l.context) + '</div>';
html += '<div style="display:flex;gap:16px;flex-wrap:wrap;font-size:11px;color:var(--ink-muted);font-family:var(--font-mono);margin-bottom:8px;">';
html += '<span>id: ' + esc(l.id) + '</span>';
- if (l.tags && l.tags.length) html += '<span>tags: ' + esc(l.tags.join(', ')) + '</span>';
+ if (asTags(l.tags).length) html += '<span>tags: ' + esc(asTags(l.tags).join(', ')) + '</span>';
if (l.createdAt) html += '<span>learned: ' + esc(formatTime(l.createdAt)) + '</span>';
if (l.lastReinforcedAt) html += '<span>last confirmed: ' + esc(formatTime(l.lastReinforcedAt)) + '</span>';
if (l.sourceIds && l.sourceIds.length) html += '<span>from ' + l.sourceIds.length + ' session(s)</span>';
@@ -3428,7 +3556,7 @@
if (search) {
items = items.filter(function(a) {
- return (a.title + ' ' + (a.description || '') + ' ' + (a.tags || []).join(' ')).toLowerCase().indexOf(search) >= 0;
+ return (a.title + ' ' + (a.description || '') + ' ' + asTags(a.tags).join(' ')).toLowerCase().indexOf(search) >= 0;
});
}
if (statusFilter) {
@@ -3468,7 +3596,7 @@
html += '</td>';
html += '<td><span class="badge ' + statusClass + '">' + esc(a.status) + '</span></td>';
html += '<td style="text-align:center;font-weight:600;">' + (a.priority || '-') + '</td>';
- html += '<td style="font-size:11px;color:var(--ink-muted);">' + (a.tags || []).map(esc).join(', ') + '</td>';
+ html += '<td style="font-size:11px;color:var(--ink-muted);">' + asTags(a.tags).map(esc).join(', ') + '</td>';
html += '<td style="text-align:center;">' + (isFrontier ? '&#9889;' : '') + '</td>';
html += '<td style="font-size:12px;color:var(--ink-muted);">' + shortTime(a.updatedAt) + '</td>';
html += '</tr>';
@@ -3870,7 +3998,7 @@
if (p.summary) {
html += '<div class="card" style="margin-top:16px;"><div class="card-title">Project Summary</div>';
- html += '<p style="font-size:13px;color:var(--ink-muted);line-height:1.7;">' + esc(p.summary) + '</p></div>';
+ html += '<p style="font-size:13px;color:var(--ink-muted);line-height:1.7;">' + esc(summaryText(p.summary)) + '</p></div>';
}
var stats = '<div class="card" style="margin-top:16px;"><div class="card-title">Project Stats</div>';
@@ -3885,8 +4013,9 @@
var wsReconnectTimer = null;
var wsRetries = 0;
var WS_MAX_RETRIES = 4;
- var directFailed = false;
+ var directFailed = true;
var directFailures = 0;
+ var wsHasConnected = false;
var DIRECT_FAILURE_THRESHOLD = 2;
var pollTimer = null;
var POLL_INTERVAL_MS = 10000;
@@ -3907,16 +4036,12 @@
pollTimer = setInterval(function() {
tick++;
if (state.activeTab === 'dashboard') {
- state.dashboard.loaded = false;
loadDashboard();
} else if (state.activeTab === 'memories') {
- state.memories.loaded = false;
loadMemories();
} else if (state.activeTab === 'sessions') {
- state.sessions.loaded = false;
loadSessions();
} else if (state.activeTab === 'activity') {
- state.activity.loaded = false;
loadActivity();
}
if (tick % WS_REPROBE_EVERY_TICKS === 0) {
@@ -3924,7 +4049,7 @@
if (!ws || ws.readyState !== WebSocket.OPEN) {
wsRetries = 0;
directFailures = 0;
- directFailed = false;
+ directFailed = true;
connectWs();
}
}
@@ -3964,10 +4089,8 @@
if (state.ws !== ws) return;
wsRetries = 0;
stopPolling();
- if (ws.__direct) {
- directFailures = 0;
- directFailed = false;
- }
+ if (wsHasConnected) resyncLiveViews();
+ wsHasConnected = true;
if (!ws.__direct) {
ws.send(JSON.stringify({
type: 'join',
@@ -3982,6 +4105,10 @@
};
ws.onmessage = function(e) {
if (state.ws !== ws) return;
+ if (!ws.__usable) {
+ ws.__usable = true;
+ directFailures = 0;
+ }
try {
var msg = JSON.parse(e.data);
if (msg.type === 'stream' && msg.event) {
@@ -3994,11 +4121,10 @@
ws.onclose = function() {
clearTimeout(connectTimer);
if (state.ws !== ws) return;
- if (ws.__direct) {
- directFailures += 1;
- if (directFailures >= DIRECT_FAILURE_THRESHOLD) {
- directFailed = true;
- }
+ directFailures += 1;
+ if (directFailures >= DIRECT_FAILURE_THRESHOLD) {
+ directFailed = ws.__direct;
+ directFailures = 0;
}
wsRetries++;
if (wsRetries < WS_MAX_RETRIES) {
@@ -4023,14 +4149,144 @@
}
}
+ var LIVE_BUFFER_MAX = 200;
+
function looksLikeObservation(obj) {
return !!(obj && typeof obj === 'object' && obj.id && obj.timestamp);
}
+ function sessionKey(s) {
+ return s && (s.id || s.sessionId);
+ }
+
+ function upsertSessionIn(list, session) {
+ var key = sessionKey(session);
+ if (!key) return list;
+ var index = list.findIndex(function(s) { return sessionKey(s) === key; });
+ if (index >= 0) list[index] = Object.assign({}, list[index], session);
+ else list.unshift(session);
+ return list;
+ }
+
+ function removeSessionFrom(list, key) {
+ return list.filter(function(s) { return sessionKey(s) !== key; });
+ }
+
+ function patchSessionCount(list, key, count) {
+ list.forEach(function(s) {
+ if (sessionKey(s) === key) s.observationCount = count;
+ });
+ }
+
+ var LIVE_SESSION_LOG_MAX = 500;
+ var liveSessionLog = [];
+ var liveSessionSeq = 0;
+
+ function applySessionEvent(list, entry) {
+ if (entry.type === 'session.updated') return upsertSessionIn(list, entry.data.session);
+ if (entry.type === 'session.deleted') return removeSessionFrom(list, entry.data.sessionId);
+ patchSessionCount(list, entry.data.sessionId, entry.data.observationCount);
+ return list;
+ }
+
+ function replaySessionEvents(list, sinceSeq) {
+ liveSessionLog.forEach(function(entry) {
+ if (entry.seq > sinceSeq) list = applySessionEvent(list, entry);
+ });
+ return list;
+ }
+
+ var memoryCountTimer = null;
+ var memoryCountRequest = 0;
+ function refreshMemoryCount() {
+ if (memoryCountTimer) return;
+ memoryCountTimer = setTimeout(async function() {
+ memoryCountTimer = null;
+ var request = ++memoryCountRequest;
+ var result = await apiGet('memories?latest=true&count=true');
+ if (request !== memoryCountRequest) return;
+ if (result && typeof result.total === 'number') {
+ state.dashboard.memoryTotal = result.total;
+ if (state.activeTab === 'dashboard' && state.dashboard.loaded) renderDashboard();
+ }
+ }, 500);
+ }
+
+ var memoriesReloadTimer = null;
+ var memoriesReloading = false;
+ var memoriesReloadPending = false;
+ function scheduleMemoriesReload() {
+ if (memoriesReloading) {
+ memoriesReloadPending = true;
+ return;
+ }
+ if (memoriesReloadTimer) return;
+ memoriesReloadTimer = setTimeout(async function() {
+ memoriesReloadTimer = null;
+ if (state.activeTab !== 'memories') return;
+ memoriesReloading = true;
+ try {
+ await loadMemories();
+ } finally {
+ memoriesReloading = false;
+ if (memoriesReloadPending) {
+ memoriesReloadPending = false;
+ scheduleMemoriesReload();
+ }
+ }
+ }, 500);
+ }
+
+ function resyncLiveViews() {
+ if (state.dashboard.loaded) loadDashboard();
+ if (state.sessions.loaded) loadSessions();
+ tabFetchedAt.memories = 0;
+ if (state.memories.loaded) scheduleMemoriesReload();
+ refreshMemoryCount();
+ }
+
+ var LIVE_RENDER_THROTTLE_MS = 250;
+ var pendingLiveRender = null;
+ function scheduleLiveRender() {
+ if (pendingLiveRender) return;
+ pendingLiveRender = setTimeout(function() {
+ pendingLiveRender = null;
+ if (state.activeTab === 'dashboard' && state.dashboard.loaded) renderDashboard();
+ if (state.activeTab === 'sessions' && state.sessions.loaded) renderSessions();
+ }, LIVE_RENDER_THROTTLE_MS);
+ }
+
+ function handleLiveEvent(type, data) {
+ if (type === 'memory.updated' || type === 'memory.deleted') {
+ refreshMemoryCount();
+ tabFetchedAt.memories = 0;
+ scheduleMemoriesReload();
+ return;
+ }
+ var isSessionEvent =
+ (type === 'session.updated' && data.session && sessionKey(data.session)) ||
+ ((type === 'session.activity' || type === 'session.deleted') && data.sessionId);
+ if (!isSessionEvent) return;
+ var entry = { seq: ++liveSessionSeq, type: type, data: data };
+ liveSessionLog.push(entry);
+ if (liveSessionLog.length > LIVE_SESSION_LOG_MAX) liveSessionLog.shift();
+ state.dashboard.sessions = applySessionEvent(state.dashboard.sessions, entry);
+ state.sessions.items = applySessionEvent(state.sessions.items, entry);
+ var touched = type === 'session.updated' ? sessionKey(data.session) : data.sessionId;
+ if (type !== 'session.activity' && touched === state.sessions.selectedId) {
+ state.sessions.detailStale = true;
+ }
+ scheduleLiveRender();
+ }
+
function handleStreamEvent(msg) {
var evt = msg.event;
var observation;
if (!evt) return;
+ if (evt.type === 'event' && evt.event && evt.event.type) {
+ handleLiveEvent(evt.event.type, evt.event.data || {});
+ return;
+ }
if (evt.event_type && evt.event_type !== 'observation' && evt.event_type !== 'create' && evt.event_type !== 'update') {
return;
}
@@ -4048,7 +4304,7 @@
routeWsMessage({ observation: observation });
}
} else if (evt.type === 'sync') {
- var items = Array.isArray(evt.data) ? evt.data : [];
+ var items = Array.isArray(evt.data) ? evt.data.slice(-LIVE_BUFFER_MAX) : [];
items.forEach(function(item) {
var payload = item.data || item;
observation = payload.observation || payload;
@@ -4067,16 +4323,18 @@
state.timeline.observations[existing] = msg.observation;
} else {
state.timeline.observations.unshift(msg.observation);
+ if (state.timeline.observations.length > LIVE_BUFFER_MAX * 4) {
+ state.timeline.observations.length = LIVE_BUFFER_MAX * 4;
+ }
}
renderObservations();
}
}
- if (state.activeTab === 'dashboard') {
- state.dashboard.loaded = false;
- loadDashboard();
- }
if (state.activeTab === 'activity' && msg.observation) {
state.activity.observations.unshift(msg.observation);
+ if (state.activity.observations.length > LIVE_BUFFER_MAX) {
+ state.activity.observations.length = LIVE_BUFFER_MAX;
+ }
renderActivity();
}
}
@@ -4617,7 +4875,18 @@
} catch (_) {}
connectWs();
})();
- startDashboardAutoRefresh();
+
+ document.addEventListener('visibilitychange', function() {
+ if (document.visibilityState !== 'visible') return;
+ var ws = state.ws;
+ if (ws && (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING)) return;
+ if (wsReconnectTimer) clearTimeout(wsReconnectTimer);
+ wsRetries = 0;
+ directFailures = 0;
+ directFailed = true;
+ connectWs();
+ if (state.activeTab === 'dashboard') scheduleDashboardReload();
+ });
// Ambient background: an ordered-dither dot field that drifts very
// slowly — the print-halftone cousin of the old static dot grid.
diff --git a/test/consolidation-status.test.ts b/test/consolidation-status.test.ts
new file mode 100644
index 00000000..305d0ee9
--- /dev/null
+++ b/test/consolidation-status.test.ts
@@ -0,0 +1,198 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
+import { readFileSync } from "node:fs";
+import {
+ describeConsolidation,
+ type ConsolidationStatusInput,
+} from "../src/functions/consolidation-status.js";
+
+function input(overrides: Partial<ConsolidationStatusInput> = {}): ConsolidationStatusInput {
+ return {
+ now: new Date("2026-09-25T12:00:00.000Z"),
+ enabled: true,
+ llmConfigured: true,
+ summaries: 0,
+ recurringPatterns: 0,
+ semanticFacts: 0,
+ procedures: 0,
+ relations: 0,
+ lastRun: null,
+ ...overrides,
+ };
+}
+
+function tier(status: ReturnType<typeof describeConsolidation>, id: string) {
+ const found = status.tiers.find((t) => t.id === id);
+ if (!found) throw new Error(`tier ${id} missing`);
+ return found;
+}
+
+describe("describeConsolidation", () => {
+ it("says what to set when no LLM key is configured", () => {
+ const status = describeConsolidation(input({ enabled: false, llmConfigured: false }));
+ expect(tier(status, "semantic").state).toBe("off");
+ expect(tier(status, "semantic").detail).toMatch(/add an LLM provider key/);
+ expect(tier(status, "procedural").state).toBe("off");
+ });
+
+ it("says consolidation was switched off on purpose when a key exists", () => {
+ const status = describeConsolidation(input({ enabled: false, llmConfigured: true }));
+ expect(tier(status, "semantic").detail).toMatch(/CONSOLIDATION_ENABLED is set to false/);
+ });
+
+ it("shows progress toward the semantic threshold and the procedural requirements", () => {
+ const status = describeConsolidation(input({ summaries: 3, recurringPatterns: 1 }));
+ expect(tier(status, "semantic")).toMatchObject({
+ state: "waiting",
+ detail: "Waiting: 3 of 5 session summaries needed.",
+ });
+ const procedural = tier(status, "procedural");
+ expect(procedural.state).toBe("waiting");
+ expect(procedural.detail).toMatch(/each finished session with a summary and 3\+ observations/);
+ expect(procedural.detail).toMatch(/you have 1/);
+ });
+
+ it("reports ready once the inputs are there and nothing has run yet", () => {
+ const status = describeConsolidation(input({ summaries: 8, recurringPatterns: 2 }));
+ expect(tier(status, "semantic").state).toBe("ready");
+ expect(tier(status, "procedural").state).toBe("ready");
+ });
+
+ it("reports the last run's output and errors per tier", () => {
+ const status = describeConsolidation(
+ input({
+ summaries: 8,
+ semanticFacts: 12,
+ lastRun: {
+ at: "2026-09-25T10:00:00.000Z",
+ tier: "all",
+ results: {
+ semantic: { newFacts: 4, totalSummaries: 8 },
+ procedural: { error: "provider timeout" },
+ },
+ },
+ }),
+ );
+ expect(status.lastRunAt).toBe("2026-09-25T10:00:00.000Z");
+ expect(tier(status, "semantic")).toMatchObject({ state: "ran", count: 12, detail: "Last run 2 h ago: 4 new facts." });
+ expect(tier(status, "procedural")).toMatchObject({ state: "error", detail: "Last run 2 h ago failed: provider timeout" });
+ });
+
+ it("falls back to the input-based state when the last run skipped a tier", () => {
+ const status = describeConsolidation(
+ input({
+ summaries: 2,
+ lastRun: {
+ at: "2026-09-25T11:59:30.000Z",
+ tier: "all",
+ results: { semantic: { skipped: true, reason: "fewer than 5 summaries" } },
+ },
+ }),
+ );
+ expect(tier(status, "semantic").state).toBe("waiting");
+ });
+
+ it("counts relations without depending on consolidation", () => {
+ expect(tier(describeConsolidation(input({ enabled: false, relations: 3 })), "relations")).toMatchObject({
+ state: "ran",
+ detail: "3 links saved.",
+ });
+ expect(tier(describeConsolidation(input()), "relations").state).toBe("waiting");
+ });
+});
+
+describe("skill extraction runs inside the consolidation cooldown", () => {
+ const saved = { ...process.env };
+
+ beforeEach(() => {
+ vi.resetModules();
+ });
+
+ afterEach(() => {
+ process.env = { ...saved };
+ });
+
+ async function stop(env: Record<string, string | undefined>) {
+ for (const key of [
+ "ANTHROPIC_API_KEY",
+ "OPENAI_API_KEY",
+ "GEMINI_API_KEY",
+ "GOOGLE_API_KEY",
+ "OPENROUTER_API_KEY",
+ "MINIMAX_API_KEY",
+ "CONSOLIDATION_ENABLED",
+ "AGENTMEMORY_CONSOLIDATION_COOLDOWN_MS",
+ ]) {
+ delete process.env[key];
+ }
+ Object.assign(process.env, env);
+ const { registerEventTriggers } = await import("../src/triggers/events.js");
+ const handlers = new Map<string, (p: unknown) => Promise<unknown>>();
+ const fired: string[] = [];
+ const sdk = {
+ registerFunction: (id: string, fn: (p: unknown) => Promise<unknown>) => handlers.set(id, fn),
+ registerTrigger: () => undefined,
+ trigger: vi.fn(async (req: { function_id: string }) => {
+ fired.push(req.function_id);
+ return {};
+ }),
+ };
+ const kv = {
+ get: vi.fn(async () => null),
+ set: vi.fn(async () => undefined),
+ list: vi.fn(async () => []),
+ };
+ registerEventTriggers(sdk as never, kv as never);
+ await handlers.get("event::session::stopped")!({ sessionId: "s1" });
+ return fired;
+ }
+
+ it("extracts a procedure from the session when consolidation is due and an LLM key is set", async () => {
+ const fired = await stop({ ANTHROPIC_API_KEY: "sk-test", CONSOLIDATION_ENABLED: "true" });
+ expect(fired).toContain("mem::consolidate-pipeline");
+ expect(fired).toContain("mem::skill-extract");
+ });
+
+ it("never calls skill extraction without an LLM key, even with consolidation forced on", async () => {
+ const fired = await stop({ CONSOLIDATION_ENABLED: "true" });
+ expect(fired).not.toContain("mem::skill-extract");
+ });
+
+ it("never calls skill extraction when consolidation is off", async () => {
+ const fired = await stop({ ANTHROPIC_API_KEY: "sk-test", CONSOLIDATION_ENABLED: "false" });
+ expect(fired).not.toContain("mem::skill-extract");
+ expect(fired).not.toContain("mem::consolidate-pipeline");
+ });
+});
+
+describe("consolidation status wiring", () => {
+ const api = readFileSync("src/triggers/api.ts", "utf-8");
+ const pipeline = readFileSync("src/functions/consolidation-pipeline.ts", "utf-8");
+ const viewer = readFileSync("src/viewer/index.html", "utf-8");
+
+ it("the pipeline stores each run's per-tier results", () => {
+ expect(pipeline).toMatch(/\.set\(KV\.config, CONSOLIDATION_LAST_RUN_KEY, \{ at: new Date\(\)\.toISOString\(\), tier, results \}\)/);
+ });
+
+ it("GET /agentmemory/consolidation/status is registered behind auth", () => {
+ expect(api).toMatch(/registerFunction\("api::consolidation-status",\s*async \(req: HttpRequest\): Promise<Response> => \{\s*const authErr = checkAuth\(req, secret\);/);
+ expect(api).toMatch(/api_path: "\/agentmemory\/consolidation\/status", http_method: "GET"/);
+ });
+
+ it("status requests share one set of store scans instead of listing every scope per request", () => {
+ expect(api).toMatch(/const sharedConsolidationCounts = singleFlight\(async \(\) => \{/);
+ expect(api).toMatch(/\}, CONSOLIDATION_COUNTS_REUSE_MS\);/);
+ const handler = api.slice(
+ api.indexOf('registerFunction("api::consolidation-status"'),
+ api.indexOf('function_id: "api::consolidation-status"'),
+ );
+ expect(handler).toMatch(/sharedConsolidationCounts\(\)/);
+ expect(handler).not.toMatch(/kv\.list\(/);
+ });
+
+ it("the dashboard shows one Memory layers panel instead of the three separate cards", () => {
+ expect(viewer).toMatch(/apiGet\('consolidation\/status'\)/);
+ expect(viewer).toMatch(/html \+= renderMemoryLayers\(d\.consolidation, semFacts, procItems\);/);
+ expect(viewer).not.toMatch(/card-title">Semantic Memory|card-title">Procedural Memory|card-title">Consolidation Status/);
+ expect(viewer).not.toMatch(/consolidation-row/);
+ });
+});
diff --git a/test/events-consolidation.test.ts b/test/events-consolidation.test.ts
index 038566a8..0c082ae2 100644
--- a/test/events-consolidation.test.ts
+++ b/test/events-consolidation.test.ts
@@ -10,6 +10,7 @@ vi.mock("../src/config.js", () => ({
isConsolidationEnabled: vi.fn(() => true),
isGraphExtractionEnabled: vi.fn(() => false),
getConsolidationCooldownMs: vi.fn(() => 300000),
+ detectLlmProviderKind: vi.fn(() => "noop"),
}));
vi.mock("../src/functions/slots.js", () => ({
diff --git a/test/memories-pagination.test.ts b/test/memories-pagination.test.ts
index c3275df6..4259b088 100644
--- a/test/memories-pagination.test.ts
+++ b/test/memories-pagination.test.ts
@@ -35,9 +35,19 @@ describe("memories + export pagination (#544)", () => {
);
});
- it("viewer dashboard caps memories?latest fetch with limit", () => {
+ it("viewer dashboard counts memories via count=true and the tab caps its fetch", () => {
const viewer = readFileSync("src/viewer/index.html", "utf-8");
- expect(viewer).toMatch(/memories\?latest=true&limit=500/);
+ expect(viewer).toMatch(/memories\?latest=true&count=true/);
+ expect(viewer).not.toMatch(/memories\?latest=true&limit=500/);
expect(viewer).toMatch(/memories\?latest=true&limit=2000/);
});
+
+ it("api::memories orders the list newest first before slicing a page", () => {
+ expect(api).toMatch(
+ /filtered\.sort\(\(a, b\) =>\s*\(b\.updatedAt \|\| b\.createdAt \|\| ""\)\.localeCompare\(/,
+ );
+ expect(api.indexOf("filtered.sort(")).toBeLessThan(
+ api.indexOf("filtered.slice(offset"),
+ );
+ });
});
diff --git a/test/viewer-dashboard-reliability.test.ts b/test/viewer-dashboard-reliability.test.ts
new file mode 100644
index 00000000..16fe12bd
--- /dev/null
+++ b/test/viewer-dashboard-reliability.test.ts
@@ -0,0 +1,237 @@
+import { describe, it, expect } from "vitest";
+import { readFileSync } from "node:fs";
+
+const viewer = readFileSync("src/viewer/index.html", "utf-8");
+
+function extractFunction(name: string): string {
+ const start = viewer.indexOf(`function ${name}(`);
+ if (start < 0) throw new Error(`function ${name} not found in viewer`);
+ let depth = 0;
+ for (let i = viewer.indexOf("{", start); i < viewer.length; i++) {
+ if (viewer[i] === "{") depth++;
+ if (viewer[i] === "}") {
+ depth--;
+ if (depth === 0) return viewer.slice(start, i + 1);
+ }
+ }
+ throw new Error(`function ${name} is not balanced`);
+}
+
+function load<T>(name: string): T {
+ return new Function(`${extractFunction(name)}\nreturn ${name};`)() as T;
+}
+
+describe("viewer dashboard reliability", () => {
+ it("observations never reload the dashboard; pushed session and memory events update it in place", () => {
+ const router = extractFunction("routeWsMessage");
+ expect(router).not.toMatch(/loadDashboard\(\)|scheduleDashboardReload\(\)/);
+ const live = extractFunction("handleLiveEvent");
+ expect(live).not.toMatch(/loadDashboard\(\)/);
+ expect(live).toMatch(/type === 'session\.updated'/);
+ expect(live).toMatch(/type === 'session\.activity'/);
+ expect(live).toMatch(/type === 'session\.deleted'/);
+ expect(live).toMatch(/type === 'memory\.updated' \|\| type === 'memory\.deleted'/);
+ expect(live).toMatch(/scheduleLiveRender\(\);\s*\}$/);
+ expect(viewer).toMatch(/var LIVE_RENDER_THROTTLE_MS = 250;/);
+ expect(extractFunction("handleStreamEvent")).toMatch(
+ /evt\.type === 'event' && evt\.event && evt\.event\.type\) \{\s*handleLiveEvent\(evt\.event\.type, evt\.event\.data \|\| \{\}\);/,
+ );
+ });
+
+ it("no timer re-fetches the dashboard and no refresh path blanks a loaded view", () => {
+ expect(viewer).not.toMatch(/startDashboardAutoRefresh|dashboardTimer|Auto-refresh 30s/);
+ expect(extractFunction("refreshDashboard")).not.toMatch(/loaded = false/);
+ expect(extractFunction("startPolling")).not.toMatch(/loaded = false/);
+ expect(extractFunction("endSession")).not.toMatch(/loaded = false/);
+ expect(extractFunction("confirmDeleteMemory")).not.toMatch(/loaded = false/);
+ });
+
+ it("session events upsert, patch and remove sessions by id", () => {
+ const deps = ["sessionKey", "upsertSessionIn", "removeSessionFrom", "patchSessionCount"]
+ .map(extractFunction)
+ .join("\n");
+ const api = new Function(`${deps}\nreturn { upsertSessionIn, removeSessionFrom, patchSessionCount };`)() as {
+ upsertSessionIn: (l: Array<Record<string, unknown>>, s: Record<string, unknown>) => Array<Record<string, unknown>>;
+ removeSessionFrom: (l: Array<Record<string, unknown>>, k: string) => Array<Record<string, unknown>>;
+ patchSessionCount: (l: Array<Record<string, unknown>>, k: string, c: number) => void;
+ };
+ const list: Array<Record<string, unknown>> = [{ id: "a", status: "active", observationCount: 1 }];
+ api.upsertSessionIn(list, { id: "a", status: "completed" });
+ expect(list).toEqual([{ id: "a", status: "completed", observationCount: 1 }]);
+ api.upsertSessionIn(list, { id: "b", status: "active" });
+ expect(list.map((s) => s.id)).toEqual(["b", "a"]);
+ api.patchSessionCount(list, "a", 7);
+ expect(list[1].observationCount).toBe(7);
+ expect(api.removeSessionFrom(list, "b").map((s) => s.id)).toEqual(["a"]);
+ });
+
+ it("re-rendering identical HTML leaves the DOM alone, but a placeholder in between forces a render", () => {
+ const setViewHtml = load<(el: Record<string, unknown>, html: string) => void>("setViewHtml");
+ let writes = 0;
+ const el: Record<string, unknown> & { firstElementChild?: object } = {};
+ Object.defineProperty(el, "innerHTML", {
+ set() {
+ writes++;
+ el.firstElementChild = {};
+ },
+ });
+ setViewHtml(el, "<div>a</div>");
+ setViewHtml(el, "<div>a</div>");
+ expect(writes).toBe(1);
+ setViewHtml(el, "<div>b</div>");
+ expect(writes).toBe(2);
+ el.firstElementChild = {};
+ setViewHtml(el, "<div>b</div>");
+ expect(writes).toBe(3);
+ });
+
+ it("connects with the join mode first and flips mode after two failures of either", () => {
+ expect(viewer).toMatch(/var directFailed = true;/);
+ expect(viewer).toMatch(/if \(directFailures >= DIRECT_FAILURE_THRESHOLD\) \{\s*directFailed = ws\.__direct;/);
+ });
+
+ it("loadDashboard never runs twice at once and replays a request that arrived mid-flight", () => {
+ const loader = extractFunction("loadDashboard");
+ expect(loader).toMatch(/if \(dashboardLoading\) \{\s*dashboardReloadPending = true;\s*return;/);
+ expect(loader).toMatch(/finally \{\s*dashboardLoading = false;/);
+ });
+
+ it("a failed refresh keeps the last data instead of showing the new-install hero", () => {
+ const loader = extractFunction("loadDashboard");
+ expect(loader).toMatch(/if \(sessionsOk\) d\.sessions = replaySessionEvents\(results\[1\]\.sessions, seqAtStart\);/);
+ expect(loader).toMatch(/if \(!sessionsOk && !d\.loaded\)/);
+ const listOr = load<(p: unknown[], r: unknown, k: string, f?: string) => unknown[]>("listOr");
+ expect(listOr([1, 2], null, "items")).toEqual([1, 2]);
+ expect(listOr([1, 2], { items: [3] }, "items")).toEqual([3]);
+ expect(listOr([1], { other: [4] }, "items", "other")).toEqual([4]);
+ expect(listOr([1], {}, "items")).toEqual([]);
+ });
+
+ it("token savings only counts sessions that produced observations", () => {
+ const estimate = load<(s: Array<{ observationCount?: number }>, b: number) => { percent: number; saved: number }>(
+ "estimateTokenSavings",
+ );
+ const busy = { observationCount: 100 };
+ const empties = Array.from({ length: 50 }, () => ({ observationCount: 0 }));
+ expect(estimate([busy, ...empties], 2000)).toEqual({ percent: 75, saved: 6000 });
+ expect(estimate([{ observationCount: 10 }], 2000)).toEqual({ percent: 0, saved: 0 });
+ expect(estimate([], 2000)).toEqual({ percent: 0, saved: 0 });
+ });
+
+ it("session summaries stored as objects render as text (#1229)", () => {
+ const summaryText = load<(v: unknown) => string>("summaryText");
+ expect(summaryText({ title: "Fix auth", narrative: "long" })).toBe("Fix auth");
+ expect(summaryText({ narrative: "Only narrative" })).toBe("Only narrative");
+ expect(summaryText({ keyDecisions: ["a", "b"] })).toBe("a; b");
+ expect(summaryText("plain")).toBe("plain");
+ expect(summaryText(undefined)).toBe("");
+ expect(viewer).not.toMatch(/s\.firstPrompt \|\| s\.summary \|\|/);
+ });
+
+ it("tags stored as a CSV string no longer break a tab (#906)", () => {
+ const asTags = load<(v: unknown) => string[]>("asTags");
+ expect(asTags("analysis, run,streamed")).toEqual(["analysis", "run", "streamed"]);
+ expect(asTags(["feat", "prompts"])).toEqual(["feat", "prompts"]);
+ expect(asTags(undefined)).toEqual([]);
+ expect(viewer).not.toMatch(/\(a\.tags \|\| \[\]\)/);
+ expect(viewer).not.toMatch(/\(l\.tags \|\| \[\]\)/);
+ });
+
+ it("live buffers are capped so a large sync backlog cannot freeze the tab (#609)", () => {
+ expect(viewer).toMatch(/var LIVE_BUFFER_MAX = 200;/);
+ expect(extractFunction("handleStreamEvent")).toMatch(/evt\.data\.slice\(-LIVE_BUFFER_MAX\)/);
+ expect(extractFunction("routeWsMessage")).toMatch(/observations\.length > LIVE_BUFFER_MAX/);
+ });
+
+ it("Rebuild Graph asks first, ignores repeat clicks, and reports the result (#1383)", () => {
+ const rebuild = extractFunction("rebuildGraph");
+ expect(rebuild).toMatch(/if \(graphRebuilding\) return;/);
+ expect(rebuild).toMatch(/window\.confirm\(/);
+ expect(rebuild).toMatch(/result && result\.success/);
+ expect(rebuild).toMatch(/finally \{\s*graphRebuilding = false;/);
+ });
+
+ it("a session list fetched while live events arrive replays them so deletes and updates stick", () => {
+ const deps = ["sessionKey", "upsertSessionIn", "removeSessionFrom", "patchSessionCount", "applySessionEvent", "replaySessionEvents"]
+ .map(extractFunction)
+ .join("\n");
+ const replay = new Function(
+ "liveSessionLog",
+ `${deps}\nreturn replaySessionEvents;`,
+ )([
+ { seq: 1, type: "session.deleted", data: { sessionId: "old" } },
+ { seq: 2, type: "session.deleted", data: { sessionId: "a" } },
+ { seq: 3, type: "session.updated", data: { session: { id: "c", status: "active" } } },
+ { seq: 4, type: "session.activity", data: { sessionId: "b", observationCount: 9 } },
+ ]) as (l: Array<Record<string, unknown>>, since: number) => Array<Record<string, unknown>>;
+ const fetched = [
+ { id: "a", observationCount: 1 },
+ { id: "b", observationCount: 2 },
+ { id: "old", observationCount: 0 },
+ ];
+ expect(replay(fetched, 1)).toEqual([
+ { id: "c", status: "active" },
+ { id: "b", observationCount: 9 },
+ { id: "old", observationCount: 0 },
+ ]);
+ expect(extractFunction("loadSessions")).toMatch(/replaySessionEvents\(result\.sessions, seqAtStart\)/);
+ });
+
+ it("count updates rerender the session list without refetching or blanking the open detail", () => {
+ const render = extractFunction("renderSessions");
+ expect(render).toMatch(/cached\.id === state\.sessions\.selectedId && !state\.sessions\.detailStale/);
+ const detail = extractFunction("renderSessionDetail");
+ expect(detail).toMatch(/if \(!cached \|\| cached\.id !== id\) \{\s*panel\.innerHTML = '<div class="detail-panel"><h3>Loading/);
+ expect(detail).toMatch(/if \(request !== sessionDetailRequest \|\| state\.sessions\.selectedId !== id\) return;/);
+ expect(extractFunction("handleLiveEvent")).toMatch(
+ /if \(type !== 'session\.activity' && touched === state\.sessions\.selectedId\) \{\s*state\.sessions\.detailStale = true;/,
+ );
+ });
+
+ it("memory events coalesce list reloads and only the newest count response wins", () => {
+ const reload = extractFunction("scheduleMemoriesReload");
+ expect(reload).toMatch(/if \(memoriesReloading\) \{\s*memoriesReloadPending = true;\s*return;/);
+ expect(reload).toMatch(/finally \{\s*memoriesReloading = false;/);
+ expect(extractFunction("handleLiveEvent")).not.toMatch(/loadMemories\(\)/);
+ expect(extractFunction("refreshMemoryCount")).toMatch(
+ /var request = \+\+memoryCountRequest;[\s\S]*if \(request !== memoryCountRequest\) return;/,
+ );
+ });
+
+ it("a reconnect resyncs loaded views, and only a delivered message clears the failure count", () => {
+ expect(viewer).toMatch(/if \(wsHasConnected\) resyncLiveViews\(\);\s*wsHasConnected = true;/);
+ const resync = extractFunction("resyncLiveViews");
+ expect(resync).toMatch(/if \(state\.dashboard\.loaded\) loadDashboard\(\);/);
+ expect(resync).toMatch(/if \(state\.sessions\.loaded\) loadSessions\(\);/);
+ expect(viewer).toMatch(/if \(!ws\.__usable\) \{\s*ws\.__usable = true;\s*directFailures = 0;/);
+ const onopen = viewer.slice(viewer.indexOf("ws.onopen = function"), viewer.indexOf("ws.onmessage = function"));
+ expect(onopen).toMatch(/wsRetries = 0;/);
+ expect(onopen).not.toMatch(/directFailures = 0;/);
+ });
+
+ it("a stale dashboard retries on its own slower timer, once at a time", () => {
+ expect(viewer).toMatch(/var STALE_RETRY_MS = 15000;/);
+ const retry = extractFunction("scheduleStaleRetry");
+ expect(retry).toMatch(/if \(staleRetryTimer\) return;/);
+ expect(retry).toMatch(/state\.dashboard\.stale\) loadDashboard\(\);/);
+ expect(extractFunction("loadDashboard")).toMatch(/if \(d\.stale\) scheduleStaleRetry\(\);/);
+ });
+
+ it("the dashboard applies its memory total only when no newer count request started", () => {
+ const loader = extractFunction("loadDashboard");
+ expect(loader).toMatch(/var countToken = \+\+memoryCountRequest;/);
+ expect(loader).toMatch(/if \(countToken === memoryCountRequest && results\[2\]/);
+ });
+
+ it("a rebuild whose graph refresh fails says so instead of reporting success", () => {
+ expect(extractFunction("rebuildGraph")).toMatch(
+ /await loadGraph\(\);\s*if \(state\.graph\.queryError && state\.graph\.rebuildResult\) \{\s*state\.graph\.rebuildResult = 'The rebuild finished, but the graph could not be refreshed/,
+ );
+ expect(viewer).toMatch(/consolidated ' \+ esc\(formatTime\(status\.lastRunAt\)\)/);
+ });
+
+ it("returning to the tab reconnects a dropped live stream (#1370)", () => {
+ expect(viewer).toMatch(/addEventListener\('visibilitychange'/);
+ expect(viewer).toMatch(/visibilityState !== 'visible'[\s\S]{0,400}connectWs\(\);/);
+ });
+});
diff --git a/test/viewer-live-events.test.ts b/test/viewer-live-events.test.ts
new file mode 100644
index 00000000..058df7a2
--- /dev/null
+++ b/test/viewer-live-events.test.ts
@@ -0,0 +1,105 @@
+import { describe, it, expect, vi } from "vitest";
+import { registerEventTriggers } from "../src/triggers/events.js";
+import type { StateKV } from "../src/state/kv.js";
+
+type Handler = (payload: unknown) => Promise<unknown>;
+
+function setup() {
+ const handlers = new Map<string, Handler>();
+ const triggers: Array<{ type: string; function_id: string; config: Record<string, unknown> }> = [];
+ const sent: Array<{ type: string; group_id: string; data: Record<string, unknown> }> = [];
+ const sdk = {
+ registerFunction: (id: string, fn: Handler) => handlers.set(id, fn),
+ registerTrigger: (t: { type: string; function_id: string; config: Record<string, unknown> }) => triggers.push(t),
+ trigger: vi.fn(async (req: { function_id: string; payload: Record<string, unknown> }) => {
+ if (req.function_id === "stream::send") {
+ sent.push(req.payload as (typeof sent)[number]);
+ }
+ return {};
+ }),
+ };
+ registerEventTriggers(sdk as never, {} as StateKV);
+ return { handlers, triggers, sent };
+}
+
+describe("viewer live events", () => {
+ it("pushes session.updated for every session write and keeps session.activity for count growth", async () => {
+ const { handlers, sent } = setup();
+ const handler = handlers.get("event::session::observation-count-changed")!;
+ await handler({
+ key: "s1",
+ event_type: "state:updated",
+ old_value: { id: "s1", status: "active", observationCount: 2 },
+ new_value: { id: "s1", status: "active", observationCount: 3 },
+ });
+ expect(sent.map((e) => e.type)).toEqual(["session.updated", "session.activity"]);
+ expect(sent[0].group_id).toBe("viewer");
+ expect(sent[0].data.session).toMatchObject({ id: "s1", observationCount: 3 });
+ expect(sent[1].data).toMatchObject({ sessionId: "s1", observationCount: 3, delta: 1 });
+ });
+
+ it("pushes session.updated without session.activity when only the status changes", async () => {
+ const { handlers, sent } = setup();
+ await handlers.get("event::session::observation-count-changed")!({
+ key: "s1",
+ event_type: "state:updated",
+ old_value: { id: "s1", status: "active", observationCount: 3 },
+ new_value: { id: "s1", status: "completed", observationCount: 3 },
+ });
+ expect(sent.map((e) => e.type)).toEqual(["session.updated"]);
+ expect(sent[0].data.session).toMatchObject({ status: "completed" });
+ });
+
+ it("pushes session.deleted when a session is removed", async () => {
+ const { handlers, sent } = setup();
+ await handlers.get("event::session::observation-count-changed")!({
+ key: "s9",
+ event_type: "state:deleted",
+ old_value: { id: "s9", status: "completed", observationCount: 4 },
+ new_value: null,
+ });
+ expect(sent).toEqual([expect.objectContaining({ type: "session.deleted", data: { sessionId: "s9" } })]);
+ });
+
+ it("drops session and memory events from other agents when agent scope is isolated", async () => {
+ vi.stubEnv("AGENT_ID", "agent-a");
+ vi.stubEnv("AGENTMEMORY_AGENT_SCOPE", "isolated");
+ try {
+ const { handlers, sent } = setup();
+ const sessions = handlers.get("event::session::observation-count-changed")!;
+ const memories = handlers.get("event::memory::changed")!;
+ await sessions({ key: "s1", event_type: "state:created", new_value: { id: "s1", agentId: "agent-b", observationCount: 1 } });
+ await sessions({ key: "s2", event_type: "state:deleted", old_value: { id: "s2", agentId: "agent-b" }, new_value: null });
+ await memories({ key: "m1", event_type: "state:created", new_value: { id: "m1", agentId: "agent-b" } });
+ expect(sent).toEqual([]);
+ await sessions({ key: "s3", event_type: "state:created", new_value: { id: "s3", agentId: "agent-a", observationCount: 0 } });
+ await memories({ key: "m2", event_type: "state:deleted", old_value: { id: "m2", agentId: "agent-a" }, new_value: null });
+ expect(sent.map((e) => e.type)).toEqual(["session.updated", "memory.deleted"]);
+ } finally {
+ vi.unstubAllEnvs();
+ }
+ });
+
+ it("pushes a slim memory.updated or memory.deleted on memory writes", async () => {
+ const { handlers, triggers, sent } = setup();
+ expect(triggers).toContainEqual(
+ expect.objectContaining({ type: "state", function_id: "event::memory::changed", config: { scope: "mem:memories" } }),
+ );
+ const handler = handlers.get("event::memory::changed")!;
+ await handler({
+ key: "m1",
+ event_type: "state:created",
+ new_value: { id: "m1", type: "fact", title: "t", content: "long body", isLatest: true, updatedAt: "2026-09-24T00:00:00Z" },
+ });
+ await handler({ key: "m1", event_type: "state:deleted", new_value: null });
+ expect(sent.map((e) => e.type)).toEqual(["memory.updated", "memory.deleted"]);
+ expect(sent[0].data).toEqual({
+ memoryId: "m1",
+ type: "fact",
+ title: "t",
+ isLatest: true,
+ updatedAt: "2026-09-24T00:00:00Z",
+ });
+ expect(sent[1].data).toEqual({ memoryId: "m1" });
+ });
+});