Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(context-gateway): repeat the hard window reminder and ask for exact notes (#5707)
t0saki469e1837b0b1
diff --git a/context_gateway/windows.py b/context_gateway/windows.pyindex ab72c021e..b384d906a 100644--- a/context_gateway/windows.py+++ b/context_gateway/windows.py@@ -28,10 +28,11 @@ ALONE = (STAY = "A new context window cannot start at this point; continue in the current one."SEARCHABLE = " Earlier windows stay searchable with openviking_grep and openviking_read."REMINDERS = {- "soft": "[context-reminder] This context window is {} full. At the next natural boundary, "- "finish or checkpoint the current step, then call openviking_new_context with complete notes.",- "hard": "[context-reminder] This context window is {} full. Call openviking_new_context now "- "with complete notes, before the window runs out.",+ "soft": "[context-reminder] This context window is {} full. Start a new window soon: once "+ "the current step is done, call openviking_new_context with notes that record your findings "+ "so far, with the exact paths, line numbers and values you will need, and what remains.",+ "hard": "[context-reminder] This context window is {} full. Call openviking_new_context now, "+ "on its own, with complete notes. This reminder repeats on every step until you do.",}@@ -100,13 +101,13 @@ def since(request):def due(request, policy, index):- """The reminder this request is due; each kind is sent at most once per window."""+ """The reminder this request is due: soft once per window, hard on every step past its ratio."""ratio = request.context_tokens / request.context_windowsent = {request.records.get((K.INJECTION, anchor), {}).get("reminder")for anchor in request.chain[index + 1 :]}- if ratio >= policy.window_hard_ratio and "hard" not in sent:+ if ratio >= policy.window_hard_ratio:return "hard"if ratio >= policy.window_soft_ratio and not sent & {"soft", "hard"}:return "soft"@@ -237,11 +238,11 @@ async def context_remaining(request, credential, args):if gap:lines.append(f"Time since the user's previous message: {gap}.")if ratio >= policy.window_hard_ratio:- advice = "call openviking_new_context now with complete notes."+ advice = "call openviking_new_context now, on its own, with complete notes."elif ratio >= policy.window_soft_ratio:advice = (- "plan a new window: at the next natural boundary, finish or checkpoint the current "- "step, then call openviking_new_context."+ "start a new window soon: once the current step is done, call "+ "openviking_new_context with notes that record your findings so far and what remains.")else:advice = "no action needed; keep working."@@ -269,7 +270,8 @@ NATIVE_TOOLS = {"notes": {**TEXT,"description": "Everything the next window needs: goal, decisions, current "- "state, files and identifiers, open problems.",+ "state, findings so far with the exact paths, line numbers and values they "+ "rest on, open problems.",},"next_steps": {**TEXT, "description": "What to do first in the new window."},},diff --git a/docs/en/guides/22-context-gateway-operations.md b/docs/en/guides/22-context-gateway-operations.mdindex a762a1262..8c46f9b17 100644--- a/docs/en/guides/22-context-gateway-operations.md+++ b/docs/en/guides/22-context-gateway-operations.md@@ -577,7 +577,7 @@ What the model sees:- `openviking_context_remaining()` reports the window number, the estimated tokens used and left, the user turns in this window, the time since the user's previous message, and a one-line recommendation.- A line in the opening note saying that the model manages its own context windows with these tools.- A status line after each new user message, at the end of the memory block: `[context-status] window wN · ~X/Y tokens (P%) · T since your previous message`.-- At most one soft and one hard reminder per window, when the window reaches **Soft reminder at** (0.7) and **Hard reminder at** (0.85). The soft reminder suggests starting a new window at a natural break; the hard one asks for it now. A reminder is attached to the new user message or, during a tool step, to the tool result.+- Reminders when the window reaches **Soft reminder at** (0.7) and **Hard reminder at** (0.85). The soft reminder, sent once per window, asks the model to start a new window once the current step is done, with notes that keep the exact paths, line numbers and values it will need. The hard one asks for a new window now and repeats on every step until the model starts one or compaction takes over. A reminder is attached to the new user message or, during a tool step, to the tool result.When the model calls `openviking_new_context`, the gateway replaces the conversation so far with a window header, and the model carries on in the same reply. The header says that the model started window N and that the user did not write it, then gives the reason, the notes and next steps, the user's latest message word for word, directions for searching earlier windows (under the same conditions as for compaction), and the conversation's opening note. Later requests rebuild the same window from the client's history. The part before the reset is saved to OpenViking right away and stays searchable. The reason and notes appear only in the header; they are not saved to OpenViking. A reset costs one provider cache miss but no summary request.diff --git a/docs/zh/guides/22-context-gateway-operations.md b/docs/zh/guides/22-context-gateway-operations.mdindex 24570bd9f..fbc509e81 100644--- a/docs/zh/guides/22-context-gateway-operations.md+++ b/docs/zh/guides/22-context-gateway-operations.md@@ -577,7 +577,7 @@ Token 预算用于模型使用 OpenViking 工具时新增的调用、结果和- `openviking_context_remaining()`:返回窗口序号、已用和剩余 token 的估算值、本窗口内的用户轮数、距用户上一条消息的时间,以及一句建议。- 开头的网关说明里多一行,告诉模型它用这两个工具自己管理上下文窗口。- 每条新的用户消息之后,记忆块末尾有一行状态:`[context-status] window wN · ~X/Y tokens (P%) · T since your previous message`。-- 每个窗口最多一次软提醒、一次硬提醒,分别在窗口用到**软提醒时机**(0.7)和**硬提醒时机**(0.85)时发出。软提醒建议模型找个合适的节点开启新窗口,硬提醒要求它立即开启。提醒附在新的用户消息上,工具步骤里则附在工具结果上。+- 窗口用到**软提醒时机**(0.7)和**硬提醒时机**(0.85)时,网关分别发出软提醒和硬提醒。软提醒每个窗口只发一次,要求模型做完当前这一步就开启新窗口,并在交接笔记里记下之后要用到的确切路径、行号和取值。硬提醒要求模型立即开启新窗口,此后每一步都会重复,直到模型开启新窗口或压缩接手。提醒附在新的用户消息上,工具步骤里则附在工具结果上。模型调用 `openviking_new_context` 后,网关把目前为止的对话换成一个窗口头,模型在同一条回复里接着工作。窗口头先说明这是模型开启的第 N 个窗口、内容不是用户写的,然后依次是 reason、notes 和 next_steps、用户最新一条消息的原文、检索之前窗口的方法(条件和压缩时相同),以及这段对话开头的网关说明。之后的请求会根据客户端的历史重建同一个窗口。重置之前的内容会立即保存到 OpenViking,之后仍可检索。reason 和 notes 只出现在窗口头里,不会保存到 OpenViking。每次重置会让服务商缓存失效一次,但不需要摘要请求。diff --git a/tests/context_gateway/test_windows.py b/tests/context_gateway/test_windows.pyindex 34ff98a38..923e531e6 100644--- a/tests/context_gateway/test_windows.py+++ b/tests/context_gateway/test_windows.py@@ -292,7 +292,7 @@ def test_context_appends_to_tool_results(protocol, message, expected):assert message == expected-def test_reminders_fire_once_per_window_and_count_cuts():+def test_soft_reminder_fires_once_per_window_hard_repeats_and_cuts_count():policy = Policy()records = {(K.INJECTION, "b"): {"text": "x", "reminder": "soft"}}request = SimpleNamespace(@@ -307,7 +307,7 @@ def test_reminders_fire_once_per_window_and_count_cuts():request.context_tokens = 90assert due(request, policy, -1) == "hard"records[K.INJECTION, "d"] = {"text": "x", "reminder": "hard"}- assert due(request, policy, -1) == ""+ assert due(request, policy, -1) == "hard"records[K.REPLACEMENT, "b"] = {"source": "compaction", "text": "", "tokens": 0}assert active_cut(request) == 1 and window_number(request) == 1records[K.REPLACEMENT, "c"] = {"source": "window", "text": "", "tokens": 0}@@ -408,9 +408,9 @@ async def test_context_remaining_reports_the_window(setup_kernel, credential, poprepared.context_window = 100_000advice = {10_000: "no action needed; keep working.",- 80_000: "plan a new window: at the next natural boundary, finish or checkpoint the "- "current step, then call openviking_new_context.",- 90_000: "call openviking_new_context now with complete notes.",+ 80_000: "start a new window soon: once the current step is done, call "+ "openviking_new_context with notes that record your findings so far and what remains.",+ 90_000: "call openviking_new_context now, on its own, with complete notes.",}for used, expected in advice.items():prepared.context_tokens = used@@ -545,7 +545,7 @@ def tool_round(protocol, number, output):@pytest.mark.parametrize("protocol", ["chat", "anthropic", "responses"])-async def test_continuation_reminders_append_to_tool_results_once(+async def test_continuation_reminders_append_to_tool_results(setup_kernel, credential, policy, protocol):kernel, _, _, _ = setup_kernel@@ -553,7 +553,8 @@ async def test_continuation_reminders_append_to_tool_results_once(upstream = {"id": "u", "context_windows": {"model": 20_000}}messages = [{"role": "user", "content": "Read the files"}]seen = []- for number, (words, expected) in enumerate([(7200, "soft"), (1, ""), (4800, "hard")]):+ steps = [(7200, "soft"), (1, ""), (4800, "hard"), (1, "hard")]+ for number, (words, expected) in enumerate(steps):messages += tool_round(protocol, number, "data " * words)prepared = await windows_request(kernel, credential, policy, messages, upstream, protocol)assert prepared.kind == "continuation"@@ -575,6 +576,7 @@ async def test_continuation_reminders_append_to_tool_results_once(assert [(r["reminder"], r["tokens"], r["uris"]) for r in seen] == [("soft", 0, []),("hard", 0, []),+ ("hard", 0, []),]diff --git a/web-studio/src/i18n/locales/en/context-gateway/profiles.ts b/web-studio/src/i18n/locales/en/context-gateway/profiles.tsindex 6812cc61a..e22b02aaf 100644--- a/web-studio/src/i18n/locales/en/context-gateway/profiles.ts+++ b/web-studio/src/i18n/locales/en/context-gateway/profiles.ts@@ -176,12 +176,12 @@ const profiles = {softRatio: {label: 'Soft reminder at',description:- 'The share of the context window at which the model is reminded to start a new window at a natural break. Sent once per window.',+ 'The share of the context window at which the model is reminded to start a new window once the current step is done. Sent once per window.',},hardRatio: {label: 'Hard reminder at',description:- 'The share of the context window at which the model is told to start a new window now. Sent once per window; must be above the soft reminder.',+ 'The share of the context window at which the model is told to start a new window now. Repeats on every step until it does; must be above the soft reminder.',},},tools: {diff --git a/web-studio/src/i18n/locales/zh-CN/context-gateway/profiles.ts b/web-studio/src/i18n/locales/zh-CN/context-gateway/profiles.tsindex 234d52f48..6fd610b17 100644--- a/web-studio/src/i18n/locales/zh-CN/context-gateway/profiles.ts+++ b/web-studio/src/i18n/locales/zh-CN/context-gateway/profiles.ts@@ -167,12 +167,12 @@ const profiles = {softRatio: {label: '软提醒时机',description:- '上下文窗口用到这个比例时,提醒模型找个合适的节点开启新窗口。每个窗口只提醒一次。',+ '上下文窗口用到这个比例时,提醒模型做完当前这一步就开启新窗口。每个窗口只提醒一次。',},hardRatio: {label: '硬提醒时机',description:- '上下文窗口用到这个比例时,要求模型立即开启新窗口。每个窗口只提醒一次,须高于软提醒。',+ '上下文窗口用到这个比例时,要求模型立即开启新窗口。模型开启新窗口之前每一步都会提醒,须高于软提醒。',},},tools: {