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(retrieve): stop served directories from hiding new files in recall (#5982)
t0saki5680dac89662
diff --git a/docs/en/api/06-retrieval.md b/docs/en/api/06-retrieval.mdindex 26d02521b..83cdb1630 100644--- a/docs/en/api/06-retrieval.md+++ b/docs/en/api/06-retrieval.md@@ -690,7 +690,7 @@ Injecting context every turn used to mean searching per type, reading each hit b| `purpose` | `chat` \| `coding` | None | Enables six-domain bucket sampling with the preset quotas below. Applies only when `quotas` is not given || `detail` | `abstract` \| `overview` \| `full` \| object | None | Requests one starting/maximum tier for every entry. Entries whose requested tier is unavailable or does not fit step down instead of being truncated. Omitted, each category takes its default tier (below). Also accepts a per-category object such as `{"events":"overview","preferences":"abstract"}`; categories left out keep their default. `"auto"` is a deprecated spelling and behaves as if omitted || `dedup_turns` | int | 0 | Cooldown window in turns; needs `session_id`. Ledger lives at `{session_uri}/.recall_log.json` |-| `exclude_uris` | string[] | [] | Stateless dedup fallback, up to 200 entries, unioned with `dedup_turns` |+| `exclude_uris` | string[] | [] | Stateless dedup fallback, up to 200 entries, unioned with `dedup_turns`. A directory URI excludes its whole subtree, so echo a served directory as its `.abstract.md` and `.overview.md` URIs to exclude just that node; `dedup_turns` cools served URIs exactly || `peer_scope` | `actor` \| `all` | `all` | `actor` excludes other peers while keeping global, self-owned and current-actor content || `other_peer_penalty` | number \| object | per-category defaults | Score penalty applied to other-peer hits |diff --git a/docs/zh/api/06-retrieval.md b/docs/zh/api/06-retrieval.mdindex 185c0756d..a1f999ce5 100644--- a/docs/zh/api/06-retrieval.md+++ b/docs/zh/api/06-retrieval.md@@ -690,7 +690,7 @@ Agent 插件每轮注入上下文时,过去需要按类型逐个检索、再| `purpose` | `chat` \| `coding` | None | 按下表的分类配额启用六域分桶采样;仅在未显式传 `quotas` 时生效 || `detail` | `abstract` \| `overview` \| `full` \| object | None | 为每条结果请求同一个起始档和最高档;请求档不可用或装不进预算时仍逐档退档而不截断。省略时按类别取默认档(见下)。也可传按类别的对象,如 `{"events":"overview","preferences":"abstract"}`,未列出的类别仍取默认档。`"auto"` 是已废弃的写法,等价于省略 || `dedup_turns` | int | 0 | 跨轮冷却轮数,需要 `session_id`;账本存在 `{session_uri}/.recall_log.json` |-| `exclude_uris` | string[] | [] | 无状态去重兜底,最多 200 条,与 `dedup_turns` 取并集 |+| `exclude_uris` | string[] | [] | 无状态去重兜底,最多 200 条,与 `dedup_turns` 取并集。目录 URI 会排除整棵子树;只想排除已返回的目录节点本身时,回传它的 `.abstract.md` 和 `.overview.md` URI;`dedup_turns` 只冷却原样返回过的 URI || `peer_scope` | `actor` \| `all` | `all` | `actor` 排除其他 peer,但仍保留全局、User 自有和当前 Actor Peer 内容 || `other_peer_penalty` | number \| object | 按类型默认值 | 对其他 peer 结果施加的分数折损 |diff --git a/openviking/retrieve/context_assembler/gather.py b/openviking/retrieve/context_assembler/gather.pyindex 3fca3fe05..f21ee374d 100644--- a/openviking/retrieve/context_assembler/gather.py+++ b/openviking/retrieve/context_assembler/gather.py@@ -217,6 +217,7 @@ async def gather_candidates(peer_scope: str = "all",penalties: Optional[Mapping[str, float]] = None,excluded: Optional[Set[str]] = None,+ cooled: Optional[Set[str]] = None,) -> Tuple[List[Candidate], Dict[str, Any]]:"""Retrieve and rank candidates, returning ``(candidates, stats)``."""peer_scope = "actor" if peer_scope == "actor" else "all"@@ -224,6 +225,7 @@ async def gather_candidates(open_ctx = replace(ctx, actor_peer_id=None)penalties = penalties or {}excluded = excluded or set()+ cooled = cooled or set()planned = [q for q in queries if q] or [""]preset_dirs = preset_directory_uris(ctx)@@ -255,7 +257,13 @@ async def gather_candidates(abstract = ""uri = base_uri = f"{root}/SKILL.md"is_directory = False- if any(_is_under(u, x) for u in (uri, base_uri) for x in excluded):+ # Caller exclusions cover a subtree; ledger-cooled URIs are served+ # entries, so a cooled directory must not hide files added under it.+ if (+ uri in cooled+ or base_uri in cooled+ or any(_is_under(u, x) for u in (uri, base_uri) for x in excluded)+ ):excluded_count += 1continueorigin = origin_for_uri(base_uri, ctx.actor_peer_id, user_root)@@ -284,7 +292,7 @@ async def gather_candidates(without this a bucket whose whole top page is cooled comes back emptyinstead of falling through to the next-best hits."""- return want + min(len(excluded), want * 2)+ return want + min(len(excluded) + len(cooled), want * 2)def _find(*,diff --git a/openviking/retrieve/context_assembler/pipeline.py b/openviking/retrieve/context_assembler/pipeline.pyindex 2a71a06e0..b091c4dee 100644--- a/openviking/retrieve/context_assembler/pipeline.py+++ b/openviking/retrieve/context_assembler/pipeline.py@@ -106,7 +106,7 @@ async def assemble_context(dedup_turns=params.dedup_turns,)cooled = ledger.cooled_uris() if ledger else set()- excluded = normalize_exclude_uris(params.exclude_uris) | cooled+ excluded = normalize_exclude_uris(params.exclude_uris)# The fan-out below embeds each planned query once per (query, target)# find, so wrap it in the request-scoped query embedding cache: sibling@@ -126,6 +126,7 @@ async def assemble_context(peer_scope=params.peer_scope,penalties=penalties,excluded=excluded,+ cooled=cooled,)# Read only the candidates whose planned tier actually needs a body: withdiff --git a/openviking_gateway/kernel.py b/openviking_gateway/kernel.pyindex 88fef8619..3ca695eda 100644--- a/openviking_gateway/kernel.py+++ b/openviking_gateway/kernel.py@@ -463,7 +463,24 @@ class MemoryKernel:cut = active_cut(request)current = [value for index, value in existing if index > cut]used = sum(v.get("tokens", 0) for v in current)- exclude = list(dict.fromkeys(u for v in current for u in v.get("uris", [])))+ # ponytail: exclude_uris matches a whole subtree since #5627, so echoing a+ # served directory would hide every file written under it later. A URI+ # without a dot in its last segment is treated as a directory and echoed+ # as its own level files, which excludes the node but not its children;+ # an extensionless file may be served again and a dotted directory still+ # hides its subtree. Upgrade path: a server-reported is_directory flag.+ exclude = list(+ dict.fromkeys(+ x+ for v in current+ for u in v.get("uris", [])+ for x in (+ [u]+ if "." in u.rsplit("/", 1)[-1]+ else [f"{u}/.abstract.md", f"{u}/.overview.md"]+ )+ )+ )budget = min(policy.max_tokens, policy.session_max_tokens - used)query = clean_text(unwrap_client(text_content(request.messages[request.anchor])))[: policy.query_max_charsdiff --git a/tests/gateway/test_kernel.py b/tests/gateway/test_kernel.pyindex c0d5e7274..ae18922ca 100644--- a/tests/gateway/test_kernel.py+++ b/tests/gateway/test_kernel.py@@ -301,6 +301,25 @@ async def test_recall_uses_server_rendered_and_uri_only_entries(assert uri in viking.recalls[-1][2]+async def test_served_directory_is_echoed_as_its_level_files(setup_kernel, credential, policy):+ """exclude_uris matches a subtree, so a served directory is echoed as its own node only."""+ kernel, _, viking, _ = setup_kernel+ folder = "viking://user/alice/memories/preferences"+ viking.entries[:] = [{"uri": f"{folder}/kestrel-theme.md"}, {"uri": folder}]+ body = {"messages": [{"role": "user", "content": "Which theme do I like?"}]}+ await prepare(kernel, body, credential, policy)+ body["messages"] += [+ {"role": "assistant", "content": "Kestrel"},+ {"role": "user", "content": "And my region?"},+ ]+ await prepare(kernel, body, credential, policy)+ assert viking.recalls[-1][2] == [+ f"{folder}/kestrel-theme.md",+ f"{folder}/.abstract.md",+ f"{folder}/.overview.md",+ ]++async def test_empty_server_rendered_does_not_fall_back_to_entries(setup_kernel, credential, policy):diff --git a/tests/retrieve/test_context_assembler_pipeline.py b/tests/retrieve/test_context_assembler_pipeline.pyindex b0798b9f9..7f740a54c 100644--- a/tests/retrieve/test_context_assembler_pipeline.py+++ b/tests/retrieve/test_context_assembler_pipeline.py@@ -785,6 +785,39 @@ async def test_exclude_uris_directory_prefix_excludes_subtree():assert f"{USER_ROOT}/memories/preferences/lang.md" in uris+async def test_a_cooled_directory_does_not_hide_files_added_under_it(monkeypatch):+ """The ledger cools served entries only; caller exclude_uris keep subtree semantics."""+ folder = f"{USER_ROOT}/memories/preferences"+ hits = [+ {"uri": f"{folder}/.overview.md", "score": 0.9, "abstract": "prefs", "level": 1},+ {"uri": f"{folder}/region.md", "score": 0.8, "abstract": "region pref"},+ ]++ class _Ledger:+ status = "on"+ turn = 2++ def cooled_uris(self):+ return {folder}++ async def record(self, entries):+ del entries++ async def fake_load(**kwargs):+ del kwargs+ return _Ledger()++ monkeypatch.setattr(pipeline_module.RecallLedger, "load", staticmethod(fake_load))+ result = await assemble_context(+ service=_service(hits=hits, bodies={}),+ ctx=_ctx(),+ params=AssembleParams(query="region", session_id="s", dedup_turns=5),+ )++ assert [e.uri for e in result.entries] == [f"{folder}/region.md"]+ assert result.stats["excluded"] == 1++async def test_a_pinned_detail_reads_the_package_skill_md():"""A package entry is a file, not a directory: `detail` reaches its SKILL.md."""service = _skill_service(