|
| 1 | +"""Demo 2 — the *after*: log in, and your sessions are yours. |
| 2 | +
|
| 3 | +Same scenarios as demo_01, now correct: |
| 4 | + * login — a password proves identity before any context is resolved; |
| 5 | + * sessions — owned, resumable conversations with human-readable titles; |
| 6 | + * isolation — a caller can only see and resume their own sessions, and state |
| 7 | + is keyed by ``thread_id_for(user, session)`` so nothing leaks. |
| 8 | +
|
| 9 | +Run: uv run python demo_02.py --case all |
| 10 | + uv run python demo_02.py --case login-sessions --trace # attribute in Langfuse |
| 11 | + uv run python demo_02.py --case isolation # ownership; no model call |
| 12 | +""" |
| 13 | + |
| 14 | +import click |
| 15 | +from dotenv import load_dotenv |
| 16 | + |
| 17 | +from realthor import auth, sessions |
| 18 | +from realthor.agent import build_agent, build_checkpointer, build_tracer, ensure_db, run_turn |
| 19 | +from realthor.config import load_config, resolve_paths |
| 20 | + |
| 21 | +load_dotenv() |
| 22 | +CONFIG = "config.yaml" |
| 23 | + |
| 24 | + |
| 25 | +def _rule(title): |
| 26 | + click.echo("\n" + "=" * 60) |
| 27 | + click.echo(title) |
| 28 | + click.echo("=" * 60) |
| 29 | + |
| 30 | + |
| 31 | +def case_login_sessions(cfg, trace): |
| 32 | + _rule("AFTER — login + an owned, resumable session") |
| 33 | + db = cfg["paths"]["app_db"] |
| 34 | + agent = build_agent(cfg, checkpointer=build_checkpointer(cfg)) |
| 35 | + tracer = build_tracer(cfg) if trace else None |
| 36 | + |
| 37 | + # 1) Prove identity with a password (not a bare flag). |
| 38 | + click.echo("\nLogin: analyst_001 / <password>") |
| 39 | + ctx = auth.authenticate(db, "analyst_001", auth.DEV_PASSWORD) |
| 40 | + bad = auth.authenticate(db, "analyst_001", "wrong-password") |
| 41 | + click.echo(f" correct password → {ctx.user_id} ({ctx.role})") |
| 42 | + click.echo(f" wrong password → {bad} (denied)") |
| 43 | + |
| 44 | + # 2) Start a session; its title comes from the first message. |
| 45 | + first = "What's the demand for a 2-bedroom apartment in the west region?" |
| 46 | + title = sessions.generate_title(first) |
| 47 | + sid = sessions.create_session(db, ctx.user_id, title) |
| 48 | + click.echo(f'\nStarted session "{title}"') |
| 49 | + click.echo(f" user> {first}") |
| 50 | + r1 = run_turn(agent, ctx, sid, first, tracer=tracer) |
| 51 | + sessions.touch_session(db, sid) |
| 52 | + click.echo(f" realthor> {r1}") |
| 53 | + |
| 54 | + # 3) List and resume it — same user+session → same thread → it remembers. |
| 55 | + click.echo("\nYour sessions:") |
| 56 | + for s in sessions.list_sessions(db, ctx.user_id): |
| 57 | + click.echo(f" · {s['title']} ({s['message_count']} msgs)") |
| 58 | + resumed = sessions.get_session(db, sid, ctx.user_id) |
| 59 | + click.echo(f'\nResume "{resumed["title"]}":') |
| 60 | + click.echo(" user> Remind me which segment we were looking at.") |
| 61 | + r2 = run_turn(agent, ctx, sid, "Remind me which segment we were looking at.", tracer=tracer) |
| 62 | + sessions.touch_session(db, sid) |
| 63 | + click.echo(f" realthor> {r2}") |
| 64 | + click.echo("\n→ Persisted, because the thread is keyed to (this user, this session).") |
| 65 | + if tracer: |
| 66 | + click.echo("(traced — filter Langfuse by user analyst_001 / this session)") |
| 67 | + |
| 68 | + |
| 69 | +def case_isolation(cfg): |
| 70 | + _rule("AFTER — isolation: sessions belong to their owner") |
| 71 | + db = cfg["paths"]["app_db"] |
| 72 | + analyst = auth.authenticate(db, "analyst_001", auth.DEV_PASSWORD) |
| 73 | + broker = auth.authenticate(db, "broker_north_001", auth.DEV_PASSWORD) |
| 74 | + |
| 75 | + # analyst_001 owns a session; broker_north_001 must not see or resume it. |
| 76 | + sid = sessions.create_session(db, analyst.user_id, "Analyst private notes") |
| 77 | + click.echo(f"\nanalyst_001 created session {sid[:8]}… ('Analyst private notes')") |
| 78 | + |
| 79 | + click.echo("\nbroker_north_001 lists their sessions:") |
| 80 | + theirs = sessions.list_sessions(db, broker.user_id) |
| 81 | + click.echo(f" → {[s['title'] for s in theirs]} (analyst's is not here)") |
| 82 | + |
| 83 | + click.echo("\nbroker_north_001 tries to resume the analyst's session by id:") |
| 84 | + stolen = sessions.get_session(db, sid, broker.user_id) |
| 85 | + click.echo(f" → get_session(...) = {stolen} (ownership check denies it)") |
| 86 | + |
| 87 | + # One user, two sessions → different threads → no cross-talk. |
| 88 | + from realthor.context import thread_id_for |
| 89 | + s_west = sessions.create_session(db, analyst.user_id, "West focus") |
| 90 | + s_north = sessions.create_session(db, analyst.user_id, "North focus") |
| 91 | + click.echo("\nOne analyst, two sessions map to two isolated threads:") |
| 92 | + click.echo(f" West → {thread_id_for(analyst.user_id, s_west)}") |
| 93 | + click.echo(f" North → {thread_id_for(analyst.user_id, s_north)}") |
| 94 | + click.echo("\n→ Isolation is identity-scoped state, not a smarter prompt.") |
| 95 | + |
| 96 | + |
| 97 | +@click.command() |
| 98 | +@click.option("--case", "case", default="all", |
| 99 | + type=click.Choice(["all", "login-sessions", "isolation"])) |
| 100 | +@click.option("--trace/--no-trace", default=False, help="Attribute the run to user/session in Langfuse.") |
| 101 | +def main(case, trace): |
| 102 | + cfg = resolve_paths(load_config(CONFIG), CONFIG) |
| 103 | + ensure_db(cfg) |
| 104 | + if case in ("all", "login-sessions"): |
| 105 | + case_login_sessions(cfg, trace) |
| 106 | + if case in ("all", "isolation"): |
| 107 | + case_isolation(cfg) |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + main() |
0 commit comments