feat: multi-persona support (single Cortex, multiple users)
- Add cortex/persona.py: ContextVar-based per-request routing with path traversal protection and persona validation - Migrate inara/ → personas/inara/ (git history preserved via git mv) - config.py: add personas_root(), inara_path() delegates to personas/inara - All 14 settings.inara_path() call sites replaced with persona_path() - ChatRequest + OrchestrateRequest: add persona field (default: "inara") with validation at request entry before any processing - memory_distiller: add optional persona param for future per-persona distill - cron_runner/tools/cron: stamp persona on jobs, prefix APScheduler IDs (persona:job_id) to prevent collisions across personas - scheduler: _load_user_crons() iterates all personas at startup Adding a new persona: create personas/<name>/ with IDENTITY.md + SOUL.md. Auth: handled at nginx level (inject X-Cortex-Persona header per subdomain). Future: persona maps to Aether account_id_random for full integration. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -26,7 +26,7 @@ import logging
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
from config import settings
|
||||
from persona import persona_path as _persona_path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -45,12 +45,12 @@ _DOW = {
|
||||
# Storage
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def crons_path() -> Path:
|
||||
return settings.inara_path() / "CRONS.json"
|
||||
def crons_path(persona: str | None = None) -> Path:
|
||||
return _persona_path(persona) / "CRONS.json"
|
||||
|
||||
|
||||
def load_crons() -> list[dict]:
|
||||
p = crons_path()
|
||||
def load_crons(persona: str | None = None) -> list[dict]:
|
||||
p = crons_path(persona)
|
||||
if not p.exists():
|
||||
return []
|
||||
try:
|
||||
@@ -60,9 +60,9 @@ def load_crons() -> list[dict]:
|
||||
return []
|
||||
|
||||
|
||||
def save_crons(crons: list[dict]) -> None:
|
||||
def save_crons(crons: list[dict], persona: str | None = None) -> None:
|
||||
import json
|
||||
crons_path().write_text(json.dumps(crons, indent=2) + "\n")
|
||||
crons_path(persona).write_text(json.dumps(crons, indent=2) + "\n")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -134,16 +134,16 @@ async def run_job(job: dict) -> None:
|
||||
label = job.get("label", job.get("id", "cron"))
|
||||
section = f"\n## {label} — {_now_label()}\n\n{payload}\n"
|
||||
|
||||
inara_dir = settings.inara_path()
|
||||
p_root = _persona_path(job.get("persona"))
|
||||
|
||||
if job_type == "remind":
|
||||
p = inara_dir / "REMINDERS.md"
|
||||
p = p_root / "REMINDERS.md"
|
||||
existing = p.read_text() if p.exists() else ""
|
||||
p.write_text(existing.rstrip() + "\n" + section)
|
||||
logger.info("cron [remind] fired: %s", label)
|
||||
|
||||
elif job_type == "note":
|
||||
p = inara_dir / "SCRATCH.md"
|
||||
p = p_root / "SCRATCH.md"
|
||||
existing = p.read_text() if p.exists() else ""
|
||||
p.write_text(existing.rstrip() + "\n" + section)
|
||||
logger.info("cron [note] fired: %s", label)
|
||||
@@ -152,10 +152,11 @@ async def run_job(job: dict) -> None:
|
||||
logger.warning("cron: unknown type %r (job %s)", job_type, job.get("id"))
|
||||
return
|
||||
|
||||
# Record last_run
|
||||
crons = load_crons()
|
||||
# Record last_run in the right persona's CRONS.json
|
||||
persona = job.get("persona")
|
||||
crons = load_crons(persona)
|
||||
for c in crons:
|
||||
if c["id"] == job["id"]:
|
||||
c["last_run"] = datetime.now(timezone.utc).isoformat()
|
||||
break
|
||||
save_crons(crons)
|
||||
save_crons(crons, persona)
|
||||
|
||||
Reference in New Issue
Block a user