feat: multi-user/multi-persona support with two-level home directory layout
Restructures persona storage from a flat personas/{name}/ layout to
home/{username}/persona/{name}/, mirroring Linux home directories.
Changes:
- persona.py: two ContextVars (user + persona), Linux-style name validation,
set_context(), get_user(), get_persona(), validate(), list_users(),
list_user_personas(); persona_path() takes (username, name)
- config.py: replaces personas_dir with home_dir + home_root()
- git mv personas/inara → home/scott/persona/inara (history preserved)
- home/holly/persona/tina/: Holly's persona stub added
- cron_runner.py: all storage functions take (username, persona) params
- tools/cron.py: stamps user + persona on jobs; APScheduler IDs are
{user}:{persona}:{job_id} to prevent collisions across users
- memory_distiller.py: distill_short/mid/long take (username, persona);
added missing Path + settings imports
- scheduler.py: _load_user_crons() iterates home/*/persona/* (two-level)
- routers/chat.py, orchestrator.py: user field added; set_context() called
- tests/conftest.py: home_root fixture with two-level structure;
patches home_dir instead of personas_dir
- tests/test_persona.py: fully rewritten for two-level API
- tests/test_api_files.py: updated fixture name and path
- .env.default: documents HOME_DIR setting; scrubs stale API key
- CLAUDE.md, README.md: directory maps updated for new layout
All 80 tests pass.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,10 +2,19 @@
|
||||
Shared fixtures for Cortex test suite.
|
||||
|
||||
Key design choices:
|
||||
- All file I/O goes to a tmp_path, never touching personas/inara/ or real sessions.
|
||||
- All file I/O goes to a tmp_path, never touching home/ or real sessions.
|
||||
- LLM calls are mocked by default — tests are fast and deterministic.
|
||||
- The 'app' fixture patches settings before importing main, so all modules
|
||||
- The 'client' fixture patches settings before importing main, so all modules
|
||||
see the temp directory.
|
||||
|
||||
Home layout mirrors the two-level structure:
|
||||
tmp/
|
||||
scott/
|
||||
persona/
|
||||
inara/ ← the default test persona
|
||||
holly/
|
||||
persona/
|
||||
tina/
|
||||
"""
|
||||
|
||||
import json
|
||||
@@ -19,19 +28,21 @@ from httpx import ASGITransport
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Temp persona directory
|
||||
# Temp home directory
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def personas_root(tmp_path_factory) -> Path:
|
||||
"""A temp personas/ dir with a minimal 'inara' persona for testing."""
|
||||
root = tmp_path_factory.mktemp("personas")
|
||||
_make_persona(root, "inara", "Inara", "Scott")
|
||||
def home_root(tmp_path_factory) -> Path:
|
||||
"""A temp home/ dir with minimal user/persona stubs for testing."""
|
||||
root = tmp_path_factory.mktemp("home")
|
||||
_make_persona(root, "scott", "inara", "Inara", "Scott")
|
||||
_make_persona(root, "holly", "tina", "Tina", "Holly")
|
||||
return root
|
||||
|
||||
|
||||
def _make_persona(root: Path, name: str, agent: str, user: str) -> Path:
|
||||
p = root / name
|
||||
def _make_persona(root: Path, username: str, persona: str,
|
||||
agent: str, user: str) -> Path:
|
||||
p = root / username / "persona" / persona
|
||||
p.mkdir(parents=True, exist_ok=True)
|
||||
(p / "IDENTITY.md").write_text(f"# {agent}\nTest identity for {agent}.")
|
||||
(p / "SOUL.md").write_text(f"# Soul\nTest soul for {agent}.")
|
||||
@@ -54,7 +65,7 @@ def _make_persona(root: Path, name: str, agent: str, user: str) -> Path:
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def client(personas_root, tmp_path):
|
||||
async def client(home_root, tmp_path):
|
||||
"""HTTPX async test client against the live ASGI app with patched paths."""
|
||||
import config
|
||||
import persona as persona_mod
|
||||
@@ -63,13 +74,12 @@ async def client(personas_root, tmp_path):
|
||||
sessions_dir.mkdir()
|
||||
|
||||
with (
|
||||
patch.object(config.settings, "personas_dir", personas_root),
|
||||
patch.object(config.settings, "home_dir", home_root),
|
||||
patch.object(config.settings, "sessions_dir", sessions_dir),
|
||||
patch("scheduler.start"), # don't run APScheduler in tests
|
||||
patch("scheduler.stop"),
|
||||
):
|
||||
# Force persona module to re-read patched settings
|
||||
persona_mod._current.set("inara")
|
||||
persona_mod.set_context("scott", "inara")
|
||||
|
||||
from main import app
|
||||
async with httpx.AsyncClient(
|
||||
|
||||
@@ -50,10 +50,10 @@ async def test_files_put_not_allowed(client):
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_files_get_missing_but_allowed(client, personas_root):
|
||||
async def test_files_get_missing_but_allowed(client, home_root):
|
||||
"""An allowed file that doesn't exist yet returns 404."""
|
||||
# Temporarily remove MEMORY_MID.md
|
||||
f = personas_root / "inara" / "MEMORY_MID.md"
|
||||
f = home_root / "scott" / "persona" / "inara" / "MEMORY_MID.md"
|
||||
existed = f.exists()
|
||||
if existed:
|
||||
backup = f.read_text()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""
|
||||
Unit tests for persona.py — validation, routing, path traversal.
|
||||
Tests the two-level home/{username}/persona/{name}/ structure.
|
||||
No HTTP involved.
|
||||
"""
|
||||
import pytest
|
||||
@@ -7,88 +8,118 @@ from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
def _make_temp_personas(tmp_path: Path) -> Path:
|
||||
root = tmp_path / "personas"
|
||||
for name in ("alice", "bob"):
|
||||
p = root / name
|
||||
def _make_home(tmp_path: Path) -> Path:
|
||||
"""Create a minimal home/ tree with two users and some personas."""
|
||||
root = tmp_path / "home"
|
||||
for username, persona in [("scott", "inara"), ("holly", "tina"), ("scott", "alt")]:
|
||||
p = root / username / "persona" / persona
|
||||
p.mkdir(parents=True)
|
||||
(p / "IDENTITY.md").write_text(f"# {name}")
|
||||
# A directory WITHOUT IDENTITY.md — should not appear in list_personas()
|
||||
(root / "incomplete").mkdir()
|
||||
(p / "IDENTITY.md").write_text(f"# {persona}")
|
||||
# A persona dir WITHOUT IDENTITY.md — should be invisible to list_user_personas()
|
||||
incomplete = root / "scott" / "persona" / "broken"
|
||||
incomplete.mkdir(parents=True)
|
||||
return root
|
||||
|
||||
|
||||
def test_validate_good(tmp_path):
|
||||
root = _make_temp_personas(tmp_path)
|
||||
root = _make_home(tmp_path)
|
||||
import config, persona
|
||||
with patch.object(config.settings, "personas_dir", root):
|
||||
assert persona.validate("alice") == "alice"
|
||||
assert persona.validate("bob") == "bob"
|
||||
with patch.object(config.settings, "home_dir", root):
|
||||
assert persona.validate("scott", "inara") == ("scott", "inara")
|
||||
assert persona.validate("holly", "tina") == ("holly", "tina")
|
||||
|
||||
|
||||
def test_validate_unknown(tmp_path):
|
||||
root = _make_temp_personas(tmp_path)
|
||||
def test_validate_unknown_user(tmp_path):
|
||||
root = _make_home(tmp_path)
|
||||
import config, persona
|
||||
with patch.object(config.settings, "personas_dir", root):
|
||||
with patch.object(config.settings, "home_dir", root):
|
||||
with pytest.raises(ValueError, match="Unknown user"):
|
||||
persona.validate("charlie", "inara")
|
||||
|
||||
|
||||
def test_validate_unknown_persona(tmp_path):
|
||||
root = _make_home(tmp_path)
|
||||
import config, persona
|
||||
with patch.object(config.settings, "home_dir", root):
|
||||
with pytest.raises(ValueError, match="Unknown persona"):
|
||||
persona.validate("charlie")
|
||||
persona.validate("scott", "ghost")
|
||||
|
||||
|
||||
def test_validate_path_traversal(tmp_path):
|
||||
root = _make_temp_personas(tmp_path)
|
||||
root = _make_home(tmp_path)
|
||||
import config, persona
|
||||
with patch.object(config.settings, "personas_dir", root):
|
||||
with pytest.raises(ValueError, match="Invalid persona name"):
|
||||
persona.validate("../../etc/passwd")
|
||||
with pytest.raises(ValueError, match="Invalid persona name"):
|
||||
persona.validate("../alice")
|
||||
with pytest.raises(ValueError, match="Invalid persona name"):
|
||||
persona.validate("alice/../../etc")
|
||||
with patch.object(config.settings, "home_dir", root):
|
||||
for bad in ("../../etc/passwd", "../scott", "scott/../../etc"):
|
||||
with pytest.raises(ValueError, match="Invalid"):
|
||||
persona.validate(bad, "inara")
|
||||
with pytest.raises(ValueError, match="Invalid"):
|
||||
persona.validate("scott", "../../etc/passwd")
|
||||
|
||||
|
||||
def test_validate_special_chars(tmp_path):
|
||||
root = _make_temp_personas(tmp_path)
|
||||
root = _make_home(tmp_path)
|
||||
import config, persona
|
||||
with patch.object(config.settings, "personas_dir", root):
|
||||
with patch.object(config.settings, "home_dir", root):
|
||||
for bad in ("alice bob", "alice;bob", "alice\x00bob", "A" * 33, ""):
|
||||
with pytest.raises(ValueError):
|
||||
persona.validate(bad)
|
||||
persona.validate(bad, "inara")
|
||||
|
||||
|
||||
def test_validate_allows_hyphen_underscore(tmp_path):
|
||||
root = _make_temp_personas(tmp_path)
|
||||
# Create a persona with hyphen and underscore in name
|
||||
p = root / "my_ai-agent"
|
||||
root = _make_home(tmp_path)
|
||||
p = root / "my_user" / "persona" / "my-agent"
|
||||
p.mkdir(parents=True)
|
||||
(p / "IDENTITY.md").write_text("# My Agent")
|
||||
import config, persona
|
||||
with patch.object(config.settings, "personas_dir", root):
|
||||
assert persona.validate("my_ai-agent") == "my_ai-agent"
|
||||
with patch.object(config.settings, "home_dir", root):
|
||||
assert persona.validate("my_user", "my-agent") == ("my_user", "my-agent")
|
||||
|
||||
|
||||
def test_list_personas(tmp_path):
|
||||
root = _make_temp_personas(tmp_path)
|
||||
def test_list_users(tmp_path):
|
||||
root = _make_home(tmp_path)
|
||||
import config, persona
|
||||
with patch.object(config.settings, "personas_dir", root):
|
||||
names = persona.list_personas()
|
||||
assert "alice" in names
|
||||
assert "bob" in names
|
||||
assert "incomplete" not in names # no IDENTITY.md
|
||||
with patch.object(config.settings, "home_dir", root):
|
||||
users = persona.list_users()
|
||||
assert "scott" in users
|
||||
assert "holly" in users
|
||||
|
||||
|
||||
def test_persona_path_uses_contextvar(tmp_path):
|
||||
root = _make_temp_personas(tmp_path)
|
||||
def test_list_user_personas(tmp_path):
|
||||
root = _make_home(tmp_path)
|
||||
import config, persona
|
||||
with patch.object(config.settings, "personas_dir", root):
|
||||
persona.set_persona("alice")
|
||||
assert persona.persona_path() == root / "alice"
|
||||
persona.set_persona("bob")
|
||||
assert persona.persona_path() == root / "bob"
|
||||
with patch.object(config.settings, "home_dir", root):
|
||||
names = persona.list_user_personas("scott")
|
||||
assert "inara" in names
|
||||
assert "alt" in names
|
||||
assert "broken" not in names # no IDENTITY.md
|
||||
|
||||
|
||||
def test_persona_path_explicit_name(tmp_path):
|
||||
root = _make_temp_personas(tmp_path)
|
||||
def test_persona_path_uses_contextvars(tmp_path):
|
||||
root = _make_home(tmp_path)
|
||||
import config, persona
|
||||
with patch.object(config.settings, "personas_dir", root):
|
||||
persona.set_persona("alice")
|
||||
assert persona.persona_path("bob") == root / "bob"
|
||||
with patch.object(config.settings, "home_dir", root):
|
||||
persona.set_context("scott", "inara")
|
||||
assert persona.persona_path() == root / "scott" / "persona" / "inara"
|
||||
persona.set_context("holly", "tina")
|
||||
assert persona.persona_path() == root / "holly" / "persona" / "tina"
|
||||
|
||||
|
||||
def test_persona_path_explicit_args(tmp_path):
|
||||
root = _make_home(tmp_path)
|
||||
import config, persona
|
||||
with patch.object(config.settings, "home_dir", root):
|
||||
persona.set_context("scott", "inara")
|
||||
# Explicit args override the ContextVar
|
||||
assert persona.persona_path("holly", "tina") == root / "holly" / "persona" / "tina"
|
||||
# ContextVar unchanged
|
||||
assert persona.persona_path() == root / "scott" / "persona" / "inara"
|
||||
|
||||
|
||||
def test_get_user_and_persona(tmp_path):
|
||||
import persona
|
||||
persona.set_context("scott", "inara")
|
||||
assert persona.get_user() == "scott"
|
||||
assert persona.get_persona() == "inara"
|
||||
persona.set_context("holly", "tina")
|
||||
assert persona.get_user() == "holly"
|
||||
assert persona.get_persona() == "tina"
|
||||
|
||||
Reference in New Issue
Block a user