From 4351890d26f78beb9ce994311ff44b76fc5b21ac Mon Sep 17 00:00:00 2001 From: Choti Wongbussakorn <126886556+Chewji9875@users.noreply.github.com> Date: Thu, 3 Sep 2026 02:40:23 +0700 Subject: [PATCH] fix(viewer): dashboard shows 0 sessions when unbounded KV scopes overflow iii invocation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lib/state/api::list has no pagination — state::list returns the whole scope as one WebSocket frame. The dashboard fired unbounded GET /agentmemory/semantic over a 15K-record (17MB) scope, which exceeded the iii-engine invocation timeout → HTTP 500 'Invocation stopped'. Because loadDashboard() uses Promise.all, ANY single 500 cascaded into state.dashboard.sessions = [] → viewer rendered 'Sessions 0' + first-run hero. - api::sessions: replace 233 sequential chunk-10 kv.get fan-out with one kv.list(KV.summaries) + Map join (same data, 1 invocation) - api::semantic/procedural/relations: add ?limit= (default 100) + total - viewer loadDashboard: bound the 5 unbounded endpoints to ?limit=50 --- src/triggers/api.ts | 48 +++++++++++++++++++++---------------------- src/viewer/index.html | 10 ++++----- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/triggers/api.ts b/src/triggers/api.ts index 56fad4f0de..9948e64f0e 100644 --- a/src/triggers/api.ts +++ b/src/triggers/api.ts @@ -865,24 +865,15 @@ export function registerApiTriggers( const filtered = filterAgentId ? sessions.filter((s) => s.agentId === filterAgentId) : sessions; - // Bounded fan-out: each kv.get is a full engine invocation, so - // Promise.all over hundreds of sessions saturates the invocation - // pool. Batch in chunks of 10 (parallel within a chunk, sequential - // across chunks); the summaries array stays index-aligned with - // `filtered`. - const summaries: Array = []; - for (let batch = 0; batch < filtered.length; batch += 10) { - const chunk = filtered.slice(batch, batch + 10); - const results = await Promise.all( - chunk.map((s) => - kv.get(KV.summaries, s.id).catch(() => null), - ), - ); - summaries.push(...results); - } - const withSummary = filtered.map((s, i) => - summaries[i] ? { ...s, summary: summaries[i] } : s, - ); + const summariesList = await kv.list(KV.summaries).catch(() => []); + const summaryBySessionId = new Map(); + for (const sm of summariesList) { + if (sm?.sessionId) summaryBySessionId.set(sm.sessionId, sm); + } + const withSummary = filtered.map((s) => { + const sum = summaryBySessionId.get(s.id); + return sum ? { ...s, summary: sum } : s; + }); return { status_code: 200, body: { sessions: withSummary } }; }, ); @@ -2027,8 +2018,11 @@ export function registerApiTriggers( async (req: ApiRequest): Promise => { const authErr = checkAuth(req, secret); if (authErr) return authErr; - const semantic = await kv.list(KV.semantic); - return { status_code: 200, body: { semantic } }; + const limitParam = parseOptionalPositiveInt(req.query_params?.["limit"]); + const limit = limitParam ?? 100; + const allSemantic = await kv.list(KV.semantic); + const semantic = allSemantic.slice(0, limit); + return { status_code: 200, body: { semantic, total: allSemantic.length } }; }, ); sdk.registerTrigger({ @@ -2041,8 +2035,11 @@ export function registerApiTriggers( async (req: ApiRequest): Promise => { const authErr = checkAuth(req, secret); if (authErr) return authErr; - const procedural = await kv.list(KV.procedural); - return { status_code: 200, body: { procedural } }; + const limitParam = parseOptionalPositiveInt(req.query_params?.["limit"]); + const limit = limitParam ?? 100; + const allProcedural = await kv.list(KV.procedural); + const procedural = allProcedural.slice(0, limit); + return { status_code: 200, body: { procedural, total: allProcedural.length } }; }, ); sdk.registerTrigger({ @@ -2055,8 +2052,11 @@ export function registerApiTriggers( async (req: ApiRequest): Promise => { const authErr = checkAuth(req, secret); if (authErr) return authErr; - const relations = await kv.list(KV.relations); - return { status_code: 200, body: { relations } }; + const limitParam = parseOptionalPositiveInt(req.query_params?.["limit"]); + const limit = limitParam ?? 100; + const allRelations = await kv.list(KV.relations); + const relations = allRelations.slice(0, limit); + return { status_code: 200, body: { relations, total: allRelations.length } }; }, ); sdk.registerTrigger({ diff --git a/src/viewer/index.html b/src/viewer/index.html index de47f4f5f9..b1cad74631 100644 --- a/src/viewer/index.html +++ b/src/viewer/index.html @@ -1477,11 +1477,11 @@

agentmemory

apiGet('memories?latest=true&limit=500'), apiGet('graph/stats'), apiGet('audit?limit=5'), - apiGet('semantic'), - apiGet('procedural'), - apiGet('relations'), - apiGet('lessons'), - apiGet('crystals') + apiGet('semantic?limit=50'), + apiGet('procedural?limit=50'), + apiGet('relations?limit=50'), + apiGet('lessons?limit=50'), + apiGet('crystals?limit=50') ]); state.dashboard.health = results[0]; state.dashboard.sessions = (results[1] && results[1].sessions) || [];