Skip to content

Agent Zero AI framework

Refresh context usage during generation

Alessandro6a6cecff8527
diff --git a/plugins/_context_window/AGENTS.md b/plugins/_context_window/AGENTS.md
index c7dee5a52..b1aa0c1a2 100644
--- a/plugins/_context_window/AGENTS.md
+++ b/plugins/_context_window/AGENTS.md
@@ -41,6 +41,8 @@
restores the accepted response after the accounting tail is drained.
- Responses API turns keep their native result and callback behavior unchanged.
- Older chats without a stored breakdown show the explanatory empty state.
+- The indicator refreshes once per new Agent 0 generation and again when the
+ run completes; streamed updates to the same generation do not refetch it.
- `_model_config` supplies the effective model limit and the
`model-context-strip-end` WebUI slot; it does not own this feature's state.
- The `contextWindowUsage` Interface setting defaults to visible on mobile and
diff --git a/plugins/_context_window/extensions/webui/apply_snapshot_before/refresh-context-window.js b/plugins/_context_window/extensions/webui/apply_snapshot_before/refresh-context-window.js
index 8bba8bb83..5a21adbd5 100644
--- a/plugins/_context_window/extensions/webui/apply_snapshot_before/refresh-context-window.js
+++ b/plugins/_context_window/extensions/webui/apply_snapshot_before/refresh-context-window.js
@@ -3,6 +3,17 @@ import { store as contextWindowStore } from "/plugins/_context_window/webui/cont
const OVERRIDE_REVISION_KEY = "_model_config_override_revision";
let lastContextId = "";
let lastRevision = null;
+let lastGenerationKey = "";
+
+function latestGenerationKey(logs) {
+ if (!Array.isArray(logs)) return "";
+ for (let index = logs.length - 1; index >= 0; index--) {
+ const item = logs[index];
+ if (item?.type !== "agent" || Number(item.agentno || 0) !== 0) continue;
+ return `${item.no ?? ""}:${item.id ?? ""}`;
+ }
+ return "";
+}
export default async function refreshContextWindow(ctx) {
const snapshot = ctx?.snapshot;
@@ -10,15 +21,22 @@ export default async function refreshContextWindow(ctx) {
if (!contextId) {
lastContextId = "";
lastRevision = null;
+ lastGenerationKey = "";
return;
}
const contexts = Array.isArray(snapshot?.contexts) ? snapshot.contexts : [];
const active = contexts.find(item => item?.id === contextId) || null;
const revision = active?.[OVERRIDE_REVISION_KEY] || null;
- if (contextId === lastContextId && revision === lastRevision) return;
+ const generationKey = latestGenerationKey(snapshot?.logs);
+ if (
+ contextId === lastContextId
+ && revision === lastRevision
+ && (!generationKey || generationKey === lastGenerationKey)
+ ) return;
lastContextId = contextId;
lastRevision = revision;
+ if (generationKey) lastGenerationKey = generationKey;
await contextWindowStore.refresh(contextId);
}
diff --git a/plugins/_context_window/tests/test_context_window.py b/plugins/_context_window/tests/test_context_window.py
index f629f3881..4ad144caf 100644
--- a/plugins/_context_window/tests/test_context_window.py
+++ b/plugins/_context_window/tests/test_context_window.py
@@ -145,6 +145,10 @@ def test_webui_and_accounting_are_plugin_owned():
helper = (ROOT / "plugins/_context_window/helpers/usage.py").read_text(
encoding="utf-8"
)
+ refresh_hook = (
+ ROOT
+ / "plugins/_context_window/extensions/webui/apply_snapshot_before/refresh-context-window.js"
+ ).read_text(encoding="utf-8")
assert 'id="model-context-strip-end"' in model_switcher
assert "contextWindowUsage" not in model_switcher
@@ -186,6 +190,9 @@ def test_webui_and_accounting_are_plugin_owned():
assert "Breakdown available after the next message." in component
assert "startswith(" not in helper
assert "rpartition(" not in helper
+ assert 'item?.type !== "agent"' in refresh_hook
+ assert "Number(item.agentno || 0) !== 0" in refresh_hook
+ assert "generationKey === lastGenerationKey" in refresh_hook
def test_source_prompt_extensions_are_registered():