Compare commits

...
3 changed files with 97 additions and 4 deletions

No files matched your search

@@ -179,6 +179,11 @@ class DocsManager:
self._docs[key] = doc
self._log(f"Loaded doc: {key}")
@staticmethod
def _effective_priority(priority: int, scope: str) -> int:
"""Apply the same global-document offset on creation and reload."""
return priority - 1000 if scope == "global" else priority
def _load_all_docs(self):
"""Load all docs from global and workspace directories."""
self._docs.clear()
@@ -188,7 +193,7 @@ class DocsManager:
self._load_docs_from_dir(self.global_docs_path, "global")
for key, doc in self._docs.items():
if key.startswith("global:"):
doc.priority = doc.priority - 1000 # Lower priority for global
doc.priority = self._effective_priority(doc.priority, "global")
# 2. Load workspace docs
workspace_docs_dir = self.workspace_path / self.DOCS_DIR_NAME.replace("/", os.sep)
@@ -355,8 +360,8 @@ class DocsManager:
frontmatter_lines = ["---"]
if description:
frontmatter_lines.append(f'description: "{description}"')
if priority != 0:
frontmatter_lines.append(f"priority: {priority}")
# Persist explicit zero instead of reloading the workspace default (100).
frontmatter_lines.append(f"priority: {priority}")
if tags:
tags_str = ", ".join(f'"{t}"' for t in tags)
frontmatter_lines.append(f"tags: [{tags_str}]")
@@ -372,7 +377,7 @@ class DocsManager:
name=name,
content=content,
description=description,
priority=priority,
priority=self._effective_priority(priority, scope),
tags=tags or [],
file_path=str(file_path)
)
@@ -0,0 +1,52 @@
"""Opt-in real LLM regression for documentation priority across reload.
Run with RUN_REAL_KEY_TESTS=1 and a configured provider key.
"""
import os
import pytest
@pytest.mark.skipif(
os.environ.get("RUN_REAL_KEY_TESTS") != "1",
reason="requires an explicitly enabled real provider",
)
def test_real_agent_uses_same_document_context_after_reload(tmp_path):
"""Effective priority must select the same source for real agent answers."""
from praisonaiagents import Agent
from praisonaiagents.memory.docs_manager import DocsManager
options = dict(
workspace_path=str(tmp_path),
global_docs_path=str(tmp_path / "global"),
)
manager = DocsManager(**options)
manager.create_doc("local", "The project codename is NEBULA-7.", priority=200)
manager.create_doc(
"shared", "The project codename is ORBIT-9.", priority=300, scope="global"
)
manager.create_doc("zero", "Supplementary reference.", priority=0)
original_context = manager.format_docs_for_prompt()
def verify(current):
"""Check effective priorities and the real answer for one manager lifecycle."""
assert current.get_doc("zero").priority == 0
assert current.get_doc("shared").priority == -700
context = current.format_docs_for_prompt()
assert context == original_context
assert "NEBULA-7" in context
assert "ORBIT-9" not in context
reader = Agent(
name="documentation-reader",
instructions="Answer only from this documentation:\n" + context,
)
result = reader.start("What is the project codename? Return only the codename.")
print(result)
assert "NEBULA-7" in str(result)
assert "ORBIT-9" not in str(result)
verify(manager)
manager.reload()
verify(manager)
verify(DocsManager(**options))
@@ -0,0 +1,36 @@
"""Created documentation must keep the same effective priority after reload."""
import pytest
from praisonaiagents.memory.docs_manager import DocsManager
@pytest.mark.parametrize("scope", ["workspace", "global"])
@pytest.mark.parametrize("priority", [0, -5, 200, 1500])
def test_created_doc_priority_and_selection_survive_reload(tmp_path, scope, priority):
"""Persist explicit priorities and preserve selection across manager lifecycles."""
options = dict(workspace_path=str(tmp_path), global_docs_path=str(tmp_path / "global"))
manager = DocsManager(**options)
manager.create_doc("reference", "Stable documentation content", priority=priority, scope=scope)
before_priority = manager.get_doc("reference").priority
before_selection = [doc.name for doc in manager.get_docs_for_context()]
manager.reload()
reopened = DocsManager(**options)
for current in (manager, reopened):
assert current.get_doc("reference").priority == before_priority
assert [doc.name for doc in current.get_docs_for_context()] == before_selection
expected_priority = priority - 1000 if scope == "global" else priority
assert before_priority == expected_priority
def test_new_global_doc_does_not_temporarily_outrank_workspace_doc(tmp_path):
"""Apply the global priority offset immediately and keep selection across reload."""
options = dict(workspace_path=str(tmp_path), global_docs_path=str(tmp_path / "global"))
manager = DocsManager(**options)
manager.create_doc("local", "local content", priority=200)
manager.create_doc("shared", "global content", priority=300, scope="global")
manager.reload()
reopened = DocsManager(**options)
for current in (manager, reopened):
assert [doc.name for doc in current.get_all_docs()] == ["local", "shared"]
assert [doc.name for doc in current.get_docs_for_context()] == ["local"]