persona-runtime 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. persona_runtime-0.1.0/.gitignore +68 -0
  2. persona_runtime-0.1.0/CHANGELOG.md +47 -0
  3. persona_runtime-0.1.0/LICENSE +77 -0
  4. persona_runtime-0.1.0/PKG-INFO +171 -0
  5. persona_runtime-0.1.0/README.md +140 -0
  6. persona_runtime-0.1.0/pyproject.toml +65 -0
  7. persona_runtime-0.1.0/src/persona_runtime/__init__.py +42 -0
  8. persona_runtime-0.1.0/src/persona_runtime/agentic/__init__.py +46 -0
  9. persona_runtime-0.1.0/src/persona_runtime/agentic/compactor.py +134 -0
  10. persona_runtime-0.1.0/src/persona_runtime/agentic/errors.py +51 -0
  11. persona_runtime-0.1.0/src/persona_runtime/agentic/events.py +214 -0
  12. persona_runtime-0.1.0/src/persona_runtime/agentic/loop.py +691 -0
  13. persona_runtime-0.1.0/src/persona_runtime/agentic/run.py +117 -0
  14. persona_runtime-0.1.0/src/persona_runtime/agentic/step.py +81 -0
  15. persona_runtime-0.1.0/src/persona_runtime/ambiguity.py +422 -0
  16. persona_runtime-0.1.0/src/persona_runtime/errors.py +44 -0
  17. persona_runtime-0.1.0/src/persona_runtime/logging.py +474 -0
  18. persona_runtime-0.1.0/src/persona_runtime/loop.py +1113 -0
  19. persona_runtime-0.1.0/src/persona_runtime/openrouter_subscription.py +197 -0
  20. persona_runtime-0.1.0/src/persona_runtime/prompt.py +510 -0
  21. persona_runtime-0.1.0/src/persona_runtime/py.typed +0 -0
  22. persona_runtime-0.1.0/src/persona_runtime/question_author.py +143 -0
  23. persona_runtime-0.1.0/src/persona_runtime/questions.py +204 -0
  24. persona_runtime-0.1.0/src/persona_runtime/router.py +28 -0
  25. persona_runtime-0.1.0/src/persona_runtime/routing/__init__.py +62 -0
  26. persona_runtime-0.1.0/src/persona_runtime/routing/classifiers.py +107 -0
  27. persona_runtime-0.1.0/src/persona_runtime/routing/heuristic.py +263 -0
  28. persona_runtime-0.1.0/src/persona_runtime/routing/latency.py +116 -0
  29. persona_runtime-0.1.0/src/persona_runtime/routing/layer1.py +126 -0
  30. persona_runtime-0.1.0/src/persona_runtime/routing/nvidia_models.py +113 -0
  31. persona_runtime-0.1.0/src/persona_runtime/routing/protocol.py +112 -0
  32. persona_runtime-0.1.0/src/persona_runtime/routing/scoring.py +199 -0
  33. persona_runtime-0.1.0/src/persona_runtime/routing/types.py +141 -0
  34. persona_runtime-0.1.0/src/persona_runtime/routing/unified.py +235 -0
  35. persona_runtime-0.1.0/src/persona_runtime/task_detector.py +317 -0
  36. persona_runtime-0.1.0/src/persona_runtime/tier.py +616 -0
  37. persona_runtime-0.1.0/tests/_fakes.py +219 -0
  38. persona_runtime-0.1.0/tests/conftest.py +18 -0
  39. persona_runtime-0.1.0/tests/integration/test_agentic_context_window_15_steps.py +122 -0
  40. persona_runtime-0.1.0/tests/integration/test_agentic_end_to_end.py +194 -0
  41. persona_runtime-0.1.0/tests/integration/test_context_window_30_turns.py +87 -0
  42. persona_runtime-0.1.0/tests/integration/test_runtime_end_to_end.py +186 -0
  43. persona_runtime-0.1.0/tests/integration/test_spec20_multimodel_fallback.py +926 -0
  44. persona_runtime-0.1.0/tests/integration/test_spec25_turnlog_sweep.py +164 -0
  45. persona_runtime-0.1.0/tests/unit/test_ambiguity.py +187 -0
  46. persona_runtime-0.1.0/tests/unit/test_compactor.py +174 -0
  47. persona_runtime-0.1.0/tests/unit/test_errors.py +62 -0
  48. persona_runtime-0.1.0/tests/unit/test_errors_agentic.py +57 -0
  49. persona_runtime-0.1.0/tests/unit/test_events.py +300 -0
  50. persona_runtime-0.1.0/tests/unit/test_fallback_rate_window.py +84 -0
  51. persona_runtime-0.1.0/tests/unit/test_first_token_latency_tracker.py +122 -0
  52. persona_runtime-0.1.0/tests/unit/test_heuristic_router.py +275 -0
  53. persona_runtime-0.1.0/tests/unit/test_logging.py +123 -0
  54. persona_runtime-0.1.0/tests/unit/test_loop.py +351 -0
  55. persona_runtime-0.1.0/tests/unit/test_loop_agentic.py +362 -0
  56. persona_runtime-0.1.0/tests/unit/test_loop_agentic_proactive.py +156 -0
  57. persona_runtime-0.1.0/tests/unit/test_loop_proactive_question.py +237 -0
  58. persona_runtime-0.1.0/tests/unit/test_magika_fallback_recovery.py +298 -0
  59. persona_runtime-0.1.0/tests/unit/test_nvidia_price_table.py +63 -0
  60. persona_runtime-0.1.0/tests/unit/test_openrouter_subscription.py +212 -0
  61. persona_runtime-0.1.0/tests/unit/test_prompt.py +302 -0
  62. persona_runtime-0.1.0/tests/unit/test_prompt_documents.py +962 -0
  63. persona_runtime-0.1.0/tests/unit/test_prompt_vision.py +312 -0
  64. persona_runtime-0.1.0/tests/unit/test_questions.py +145 -0
  65. persona_runtime-0.1.0/tests/unit/test_refusal_auto_retry.py +121 -0
  66. persona_runtime-0.1.0/tests/unit/test_router.py +123 -0
  67. persona_runtime-0.1.0/tests/unit/test_router_vision.py +339 -0
  68. persona_runtime-0.1.0/tests/unit/test_routing_classifiers.py +117 -0
  69. persona_runtime-0.1.0/tests/unit/test_routing_eval_harness.py +215 -0
  70. persona_runtime-0.1.0/tests/unit/test_routing_layer1.py +245 -0
  71. persona_runtime-0.1.0/tests/unit/test_routing_layer1_invariant.py +150 -0
  72. persona_runtime-0.1.0/tests/unit/test_routing_nvidia_models.py +234 -0
  73. persona_runtime-0.1.0/tests/unit/test_routing_protocol.py +198 -0
  74. persona_runtime-0.1.0/tests/unit/test_routing_scoring.py +212 -0
  75. persona_runtime-0.1.0/tests/unit/test_routing_types.py +231 -0
  76. persona_runtime-0.1.0/tests/unit/test_run.py +139 -0
  77. persona_runtime-0.1.0/tests/unit/test_step.py +80 -0
  78. persona_runtime-0.1.0/tests/unit/test_task_detector.py +164 -0
  79. persona_runtime-0.1.0/tests/unit/test_tier.py +205 -0
  80. persona_runtime-0.1.0/tests/unit/test_tier_metadata.py +406 -0
  81. persona_runtime-0.1.0/tests/unit/test_tier_registry_multimodel.py +482 -0
  82. persona_runtime-0.1.0/tests/unit/test_tool_refusal_detection.py +188 -0
  83. persona_runtime-0.1.0/tests/unit/test_turnlog_fallback_fields.py +422 -0
  84. persona_runtime-0.1.0/tests/unit/test_turnlog_reasoning_extension.py +112 -0
  85. persona_runtime-0.1.0/tests/unit/test_turnlog_routing_extension.py +154 -0
  86. persona_runtime-0.1.0/tests/unit/test_unified_router.py +290 -0
  87. persona_runtime-0.1.0/tests/unit/test_unified_router_end_to_end.py +182 -0
@@ -0,0 +1,68 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.egg-info/
5
+ dist/
6
+ build/
7
+ .venv/
8
+ *.egg
9
+ .mypy_cache/
10
+ .ruff_cache/
11
+ .pytest_cache/
12
+ htmlcov/
13
+ .coverage
14
+ coverage.xml
15
+
16
+ # Environment
17
+ .env
18
+ .env.local
19
+ .env.*.local
20
+ .secrets/
21
+
22
+ # IDEs
23
+ .vscode/
24
+ .idea/
25
+ .claude/
26
+ *.swp
27
+ *.swo
28
+ *~
29
+
30
+ # OS
31
+ .DS_Store
32
+ Thumbs.db
33
+
34
+ # Node (web app)
35
+ node_modules/
36
+ .next/
37
+ out/
38
+
39
+ # ChromaDB local storage
40
+ .chroma/
41
+
42
+ # Persona working directories
43
+ .persona_work/
44
+ .persona_audit/
45
+
46
+ # UV — uv.lock IS committed (reproducible builds; Dockerfile COPYs it
47
+ # into the image; the CI deploy fails without it).
48
+
49
+ # rsync atomic-write tempfiles
50
+ .*.??????
51
+
52
+ # Docker
53
+ docker-compose.override.yml
54
+
55
+ # Operational caches / temp workspaces
56
+ .playwright-mcp/
57
+ .tmp_smoke_workspace/
58
+
59
+ # Spec 16 D-16-X-3: inspection artifacts (.docx/.pptx/.xlsx/.pdf) are binary;
60
+ # evidence at close-out is the per-row scorecard in state.md, not the binaries.
61
+ docs/specs/phase2/spec_16/inspection/
62
+
63
+ # Spec 17 T09 chart-inspection artifacts (.png) are binary; same discipline —
64
+ # scorecards in state.md are the close-out evidence, not the PNG files.
65
+ docs/specs/phase2/spec_17/inspection/
66
+ packages/web/evidence/
67
+ docs/*
68
+ CLAUDE.md
@@ -0,0 +1,47 @@
1
+ # Changelog — persona-runtime
2
+
3
+ All notable changes to `persona-runtime` are recorded here.
4
+
5
+ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
6
+ The project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ Per-spec entries are added by the close-out phase of each spec. The authoritative
9
+ project-wide changelog lives at [`/CHANGELOG.md`](../../CHANGELOG.md); this file
10
+ mirrors only the `persona-runtime`-touching surface.
11
+
12
+ ---
13
+
14
+ ## [Unreleased]
15
+
16
+ (empty — future post-v0.1 work lands here)
17
+
18
+ ---
19
+
20
+ ## [0.18.0] — 2026-06-07
21
+
22
+ > Spec 18 — Unified Model Router close-out + Spec 19 amendment chain entry 13 (prompt-builder produced-files verification). Strangler-fig discipline preserves Spec 05's `Router` byte-for-byte: existing `test_router.py` 25/25 + `test_router_vision.py` 10/10 pass unchanged. The agentic-loop routing seam is sharpened to honour the unified router's two-layer architecture without disturbing Spec 06's plan-act-reflect cycle.
23
+
24
+ ### Added (Spec 18 — Unified Model Router)
25
+
26
+ - **`Router` + `RouterScorer` Protocols** at [`src/persona_runtime/routing/protocol.py`](src/persona_runtime/routing/protocol.py) — `@runtime_checkable`. `Router.route(context: RoutingContext) -> RoutingDecision`. `RouterScorer` is the v0.2 extras seam for the optional learned-router integration (D-18-1).
27
+ - **`HeuristicRouter`** at [`src/persona_runtime/routing/heuristic.py`](src/persona_runtime/routing/heuristic.py) — Spec 05's rule-based router refactored behind the Protocol. `.choose()` preserved verbatim (byte-for-byte regression guarded). Strangler-fig alias at [`src/persona_runtime/router.py`](src/persona_runtime/router.py) re-exports `HeuristicRouter as Router` per D-18-X-strangler-fig-alias-shape.
28
+ - **`UnifiedRouter`** at [`src/persona_runtime/routing/unified.py`](src/persona_runtime/routing/unified.py) — Layer 1 hard-filter via `apply_constraint_filter` + Layer 2 sweet-spot scorer + bounded fallback (voice 30ms / text 100ms per D-18-4). Four fallback reasons: `"timeout"` / `"scoring_error"` / `"empty_metadata"` / `"partial_metadata:<tier>"` with rate-limited loguru warning per (reason, profile) per 60s (D-18-X-fallback-instrumentation).
29
+ - **`apply_constraint_filter` free function** at [`src/persona_runtime/routing/layer1.py`](src/persona_runtime/routing/layer1.py) — shared by `HeuristicRouter.route()` AND `UnifiedRouter.route()` via module-level import (D-18-X-layer1-extraction).
30
+ - **`RoutingContext` + `RoutingDecision` boundary types** at [`src/persona_runtime/routing/types.py`](src/persona_runtime/routing/types.py) — frozen Pydantic v2 + `extra="forbid"` (D-05-9 precedent).
31
+ - **`TierMetadata` + `TierRegistry.metadata_for()`** at [`src/persona_runtime/tier.py`](src/persona_runtime/tier.py) — additive extension at the runtime layer (NOT on `ChatBackend` Protocol per Phase 1 fold-in d). 6 fields per D-18-3: cost_input/output_per_1k, first_token_latency_ms, throughput_tokens_per_sec, context_window, tool_strength. `tier_metadata_from_env(prefix)` ships the env-var population path.
32
+ - **`FirstTokenLatencyTracker`** at [`src/persona_runtime/routing/latency.py`](src/persona_runtime/routing/latency.py) — per-model EWMA tracker (α=0.2) with simple-average warm-up for samples 1-5 (D-18-X-first-token-measurement-impl). Hooked into `ConversationLoop._stream_round` at the first non-empty `chunk.delta`.
33
+ - **TurnLog routing extension** at [`src/persona_runtime/logging.py`](src/persona_runtime/logging.py) — additive D-18-X-turnlog-extension fields: `routing_decision: RoutingDecision | None`, `routing_latency_ms: float`, `routing_fallback_triggered: bool`, `routing_fallback_reason: str | None`. Pre-Spec-18 callers stay green (all optional with safe defaults). JSON round-trip verified for Postgres JSONB compatibility.
34
+
35
+ ### Added (Spec 19 amendment — chain entry 13)
36
+
37
+ - **L1 (chain 13) D-19-X-prompt-builder-produced-files-verification** — `PromptBuilder` now verifies prior-turn produced-files references against the active workspace before rendering context; resolver mismatches raise the structural domain exception so downstream renderers never reach a stale path. Closed-spec additive extension; no Spec 05 reopen. Coordinates with the Spec F4 `_persist_produced_file` policy fix at the API layer (D-F4-X-bare-ref-resolution).
38
+
39
+ ### Inherited (close-out roll-up from prior versions)
40
+
41
+ The 0.18.0 anchor subsumes the runtime-touching surface of intermediate Phase 2 work folded into prior `[Unreleased]` blocks:
42
+
43
+ - **Spec F4 — Rich-Output UI Surface.** `RunEvent.tool_result` constructor at [`src/persona_runtime/agentic/events.py:96-103`](src/persona_runtime/agentic/events.py) — 4-line additive edit forwards `result.data.produced_files` onto the event payload. One constructor serves BOTH chat SSE AND RunEvent transports. No Pydantic schema change. D-F4-X-event-kind-for-produced-files.
44
+
45
+ ### Inherited (Spec 11 launch and earlier)
46
+
47
+ The 0.18.0 anchor subsumes Spec 11 launch (`_dispatch` recovery on ToolNotAllowedError/ToolExecutionError, assistant-with-tool_calls native-path emission, `StepHistoryCompactor._recent_start` boundary correction), Spec 10 authoring (chat-stream/SSE bridge tightening), Spec 09 web-app pairing (chat-stream delta-by-delta + `tool_calling`/`tool_result` SSE events + real `done.tier`), Spec 06 agentic loop (full `AgenticLoop` + `Run`/`Step`/`RunEvent` + `StepHistoryCompactor`), and Spec 05 conversation loop (`ConversationLoop` + `PromptBuilder` + `Router` + `TierRegistry` + `TurnLog`). Full per-spec rationale lives in [`/CHANGELOG.md`](../../CHANGELOG.md).
@@ -0,0 +1,77 @@
1
+ # PolyForm Noncommercial License 1.0.0
2
+
3
+ <https://polyformproject.org/licenses/noncommercial/1.0.0>
4
+
5
+ ## Acceptance
6
+
7
+ In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
8
+
9
+ ## Copyright License
10
+
11
+ The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose. However, you may only distribute the software according to [Distribution License](#distribution-license) and make changes or new works based on the software according to [Changes and New Works License](#changes-and-new-works-license).
12
+
13
+ ## Distribution License
14
+
15
+ The licensor grants you an additional copyright license to distribute copies of the software. Your license to distribute covers distributing the software with changes and new works permitted by [Changes and New Works License](#changes-and-new-works-license).
16
+
17
+ ## Notices
18
+
19
+ You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms, the copyright notice, and the patent notice.
20
+
21
+ If you modify the software, you must include in any modified copies of the software a prominent notice stating that you have modified the software.
22
+
23
+ ## Changes and New Works License
24
+
25
+ The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
26
+
27
+ ## Patent License
28
+
29
+ The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
30
+
31
+ ## Noncommercial Purposes
32
+
33
+ Any noncommercial purpose is a permitted purpose.
34
+
35
+ ## Personal Uses
36
+
37
+ Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, is use for a permitted purpose.
38
+
39
+ ## Noncommercial Organizations
40
+
41
+ Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution is use for a permitted purpose regardless of the source of funding or obligations resulting from the funding.
42
+
43
+ ## Fair Use
44
+
45
+ You may have "fair use" rights for the software under the law. These terms do not limit them.
46
+
47
+ ## No Other Rights
48
+
49
+ These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
50
+
51
+ ## Patent Defense
52
+
53
+ If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
54
+
55
+ ## Violations
56
+
57
+ The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
58
+
59
+ ## No Liability
60
+
61
+ ***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
62
+
63
+ ## Definitions
64
+
65
+ The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
66
+
67
+ **You** refers to the individual or entity agreeing to these terms.
68
+
69
+ **Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
70
+
71
+ **Your licenses** are all the licenses granted to you for the software under these terms.
72
+
73
+ **Use** means anything you do with the software requiring one of your licenses.
74
+
75
+ ---
76
+
77
+ Copyright 2026 Yasin Hessnawi
@@ -0,0 +1,171 @@
1
+ Metadata-Version: 2.4
2
+ Name: persona-runtime
3
+ Version: 0.1.0
4
+ Summary: Generation loop, router, and agentic engine for persona-core.
5
+ Project-URL: Homepage, https://github.com/yasinhessnawi1/Open-Persona
6
+ Project-URL: Repository, https://github.com/yasinhessnawi1/Open-Persona
7
+ Project-URL: Issues, https://github.com/yasinhessnawi1/Open-Persona/issues
8
+ Project-URL: Changelog, https://github.com/yasinhessnawi1/Open-Persona/blob/main/CHANGELOG.md
9
+ Author-email: Yasin Hessnawi <yasinhessnawi1@gmail.com>
10
+ License-Expression: PolyForm-Noncommercial-1.0.0
11
+ License-File: LICENSE
12
+ Keywords: agent,agentic,ai,llm,persona,router,tool-use
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: Other/Proprietary License
16
+ Classifier: Operating System :: MacOS
17
+ Classifier: Operating System :: POSIX
18
+ Classifier: Operating System :: POSIX :: Linux
19
+ Classifier: Programming Language :: Python
20
+ Classifier: Programming Language :: Python :: 3
21
+ Classifier: Programming Language :: Python :: 3.11
22
+ Classifier: Programming Language :: Python :: 3.12
23
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
24
+ Classifier: Topic :: Software Development :: Libraries
25
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
26
+ Classifier: Typing :: Typed
27
+ Requires-Python: >=3.11
28
+ Requires-Dist: persona-core
29
+ Requires-Dist: tiktoken<1,>=0.7
30
+ Description-Content-Type: text/markdown
31
+
32
+ # persona-runtime
33
+
34
+ > Conversation loop, prompt builder, router, and agentic engine for Persona.
35
+ > Source-available; noncommercial use only.
36
+
37
+ **Status:** PolyForm Noncommercial 1.0.0 · Source Available (Noncommercial Use Only)
38
+
39
+ ## What it is
40
+
41
+ `persona-runtime` is the orchestration layer that turns a `persona-core`
42
+ persona into a running conversational agent. It owns six things and nothing
43
+ else:
44
+
45
+ - **`ConversationLoop`** — the one-turn keystone: retrieve typed-memory
46
+ context, manage history (summarise-and-compact at K=10, keep last 5
47
+ verbatim), build the prompt, route, stream-generate with a tool-call
48
+ sub-loop, write the turn back to the episodic store.
49
+ - **`PromptBuilder`** + `RetrievedContext` — assembles the system prompt
50
+ from identity + constraints + retrieved chunks + skill index, with a
51
+ context-window budget reducer.
52
+ - **Routing** — `Router` (`@runtime_checkable` Protocol) with two concrete
53
+ implementations: `HeuristicRouter` (rule-based, per-turn,
54
+ per-persona-overridable) and `UnifiedRouter` (two-layer: hard-filter via
55
+ `apply_constraint_filter` then sweet-spot scoring, with bounded fallback
56
+ and per-tier metadata).
57
+ - **`TierRegistry`** — lazy-cached backend registry per tier
58
+ (`frontier` / `mid` / `small`); configured via `PERSONA_{TIER}_*`
59
+ env triples; small→mid→frontier fallback; cross-provider multi-model
60
+ per tier (Spec 20).
61
+ - **`AgenticLoop`** — the plan-act-reflect cycle in `persona_runtime.agentic`:
62
+ one model decides at each step whether to call a tool, ask the user, or
63
+ produce a final answer; `[ASK_USER]` / `[FINAL]` markers as the primary
64
+ classification signal; step-history compaction at the tier budget;
65
+ cancel-token boundary; terminal status (`completed` /
66
+ `max_steps_reached` / `cancelled` / `error`) authoritative.
67
+ - **`TurnLog`** + `JSONLTurnLogWriter` / `MemoryTurnLogWriter` — per-turn
68
+ telemetry record (model, tokens, cost, routing decision, latency,
69
+ fallback) durable to JSONL or held in memory for tests.
70
+
71
+ The runtime depends only on `persona-core`; it does not depend on the API
72
+ or web app. The composition root (the API in production, the CLI for
73
+ local use, the tests in CI) owns the `Conversation` object and the
74
+ `TierRegistry` lifecycle — the loop itself is stateless per request.
75
+
76
+ ## Install
77
+
78
+ From PyPI (planned):
79
+
80
+ ```bash
81
+ pip install persona-runtime
82
+ ```
83
+
84
+ Workspace development:
85
+
86
+ ```bash
87
+ git clone https://github.com/yasinhessnawi1/Open-Persona.git
88
+ cd open-persona
89
+ uv sync --all-packages
90
+ ```
91
+
92
+ ## Run
93
+
94
+ `persona-runtime` is a library — no CLI of its own. Compose it on top of
95
+ `persona-core`:
96
+
97
+ ```python
98
+ import asyncio
99
+ from pathlib import Path
100
+
101
+ from persona.schema.persona import Persona
102
+ from persona.schema.conversation import Conversation, ConversationMessage
103
+ from persona.registry import PersonaRegistry
104
+ from persona.stores.chroma import ChromaMemoryStore
105
+ from persona.tools.toolbox import Toolbox
106
+ from persona_runtime import (
107
+ ConversationLoop, PromptBuilder, Router, TurnLog, tier_registry_from_env,
108
+ )
109
+
110
+
111
+ async def main() -> None:
112
+ persona = Persona.from_yaml(Path("examples/astrid_tenancy_law.yaml"))
113
+ registry = PersonaRegistry(store=ChromaMemoryStore.local("./.persona-data"))
114
+ registry.load(persona)
115
+ tiers = tier_registry_from_env()
116
+
117
+ loop = ConversationLoop(
118
+ registry=registry,
119
+ tiers=tiers,
120
+ router=Router(),
121
+ prompt_builder=PromptBuilder(),
122
+ toolbox=Toolbox.empty(),
123
+ )
124
+
125
+ conversation = Conversation.new(persona_id=persona.id)
126
+ user = ConversationMessage(role="user", content="Hva sier husleieloven om mugg?", created_at=None)
127
+ async for chunk in loop.turn(conversation, user):
128
+ print(chunk.delta, end="", flush=True)
129
+ await tiers.aclose()
130
+
131
+
132
+ asyncio.run(main())
133
+ ```
134
+
135
+ Env vars (per tier; see `.env.example` at the repo root):
136
+
137
+ ```
138
+ PERSONA_FRONTIER_PROVIDER=anthropic PERSONA_FRONTIER_MODEL=claude-opus-...
139
+ PERSONA_MID_PROVIDER=deepseek PERSONA_MID_MODEL=deepseek-chat
140
+ PERSONA_SMALL_PROVIDER=groq PERSONA_SMALL_MODEL=llama-...
141
+ ```
142
+
143
+ A single `PERSONA_PROVIDER` + `PERSONA_MODEL` + `PERSONA_API_KEY` pair is
144
+ the fallback when no per-tier vars are set.
145
+
146
+ ## Test
147
+
148
+ ```bash
149
+ uv run pytest packages/runtime # unit (default)
150
+ uv run pytest packages/runtime -m integration # integration
151
+ uv run mypy packages/runtime/src
152
+ uv run ruff check packages/runtime
153
+ ```
154
+
155
+ ## Architecture role
156
+
157
+ `persona-runtime` is layer 3 of the Open Persona stack. It sits directly
158
+ above `persona-core` and below `persona-api`; the API composes the
159
+ runtime, attaches it to HTTP routes, and persists the per-request state
160
+ (conversation, run, turn-log, event bus). The runtime contains zero HTTP,
161
+ zero database client, zero secrets — every collaborator is injected by the
162
+ composition root.
163
+
164
+ ## Contribute
165
+
166
+ Contributions welcome under the same PolyForm Noncommercial 1.0.0 license.
167
+ The package is source-available for noncommercial use; commercial use
168
+ requires a separate license — contact the rights holder. Issues and pull
169
+ requests welcome at
170
+ [github.com/yasinhessnawi1/Open-Persona](https://github.com/yasinhessnawi1/Open-Persona).
171
+ See [CHANGELOG.md](CHANGELOG.md) for the spec-by-spec history.
@@ -0,0 +1,140 @@
1
+ # persona-runtime
2
+
3
+ > Conversation loop, prompt builder, router, and agentic engine for Persona.
4
+ > Source-available; noncommercial use only.
5
+
6
+ **Status:** PolyForm Noncommercial 1.0.0 · Source Available (Noncommercial Use Only)
7
+
8
+ ## What it is
9
+
10
+ `persona-runtime` is the orchestration layer that turns a `persona-core`
11
+ persona into a running conversational agent. It owns six things and nothing
12
+ else:
13
+
14
+ - **`ConversationLoop`** — the one-turn keystone: retrieve typed-memory
15
+ context, manage history (summarise-and-compact at K=10, keep last 5
16
+ verbatim), build the prompt, route, stream-generate with a tool-call
17
+ sub-loop, write the turn back to the episodic store.
18
+ - **`PromptBuilder`** + `RetrievedContext` — assembles the system prompt
19
+ from identity + constraints + retrieved chunks + skill index, with a
20
+ context-window budget reducer.
21
+ - **Routing** — `Router` (`@runtime_checkable` Protocol) with two concrete
22
+ implementations: `HeuristicRouter` (rule-based, per-turn,
23
+ per-persona-overridable) and `UnifiedRouter` (two-layer: hard-filter via
24
+ `apply_constraint_filter` then sweet-spot scoring, with bounded fallback
25
+ and per-tier metadata).
26
+ - **`TierRegistry`** — lazy-cached backend registry per tier
27
+ (`frontier` / `mid` / `small`); configured via `PERSONA_{TIER}_*`
28
+ env triples; small→mid→frontier fallback; cross-provider multi-model
29
+ per tier (Spec 20).
30
+ - **`AgenticLoop`** — the plan-act-reflect cycle in `persona_runtime.agentic`:
31
+ one model decides at each step whether to call a tool, ask the user, or
32
+ produce a final answer; `[ASK_USER]` / `[FINAL]` markers as the primary
33
+ classification signal; step-history compaction at the tier budget;
34
+ cancel-token boundary; terminal status (`completed` /
35
+ `max_steps_reached` / `cancelled` / `error`) authoritative.
36
+ - **`TurnLog`** + `JSONLTurnLogWriter` / `MemoryTurnLogWriter` — per-turn
37
+ telemetry record (model, tokens, cost, routing decision, latency,
38
+ fallback) durable to JSONL or held in memory for tests.
39
+
40
+ The runtime depends only on `persona-core`; it does not depend on the API
41
+ or web app. The composition root (the API in production, the CLI for
42
+ local use, the tests in CI) owns the `Conversation` object and the
43
+ `TierRegistry` lifecycle — the loop itself is stateless per request.
44
+
45
+ ## Install
46
+
47
+ From PyPI (planned):
48
+
49
+ ```bash
50
+ pip install persona-runtime
51
+ ```
52
+
53
+ Workspace development:
54
+
55
+ ```bash
56
+ git clone https://github.com/yasinhessnawi1/Open-Persona.git
57
+ cd open-persona
58
+ uv sync --all-packages
59
+ ```
60
+
61
+ ## Run
62
+
63
+ `persona-runtime` is a library — no CLI of its own. Compose it on top of
64
+ `persona-core`:
65
+
66
+ ```python
67
+ import asyncio
68
+ from pathlib import Path
69
+
70
+ from persona.schema.persona import Persona
71
+ from persona.schema.conversation import Conversation, ConversationMessage
72
+ from persona.registry import PersonaRegistry
73
+ from persona.stores.chroma import ChromaMemoryStore
74
+ from persona.tools.toolbox import Toolbox
75
+ from persona_runtime import (
76
+ ConversationLoop, PromptBuilder, Router, TurnLog, tier_registry_from_env,
77
+ )
78
+
79
+
80
+ async def main() -> None:
81
+ persona = Persona.from_yaml(Path("examples/astrid_tenancy_law.yaml"))
82
+ registry = PersonaRegistry(store=ChromaMemoryStore.local("./.persona-data"))
83
+ registry.load(persona)
84
+ tiers = tier_registry_from_env()
85
+
86
+ loop = ConversationLoop(
87
+ registry=registry,
88
+ tiers=tiers,
89
+ router=Router(),
90
+ prompt_builder=PromptBuilder(),
91
+ toolbox=Toolbox.empty(),
92
+ )
93
+
94
+ conversation = Conversation.new(persona_id=persona.id)
95
+ user = ConversationMessage(role="user", content="Hva sier husleieloven om mugg?", created_at=None)
96
+ async for chunk in loop.turn(conversation, user):
97
+ print(chunk.delta, end="", flush=True)
98
+ await tiers.aclose()
99
+
100
+
101
+ asyncio.run(main())
102
+ ```
103
+
104
+ Env vars (per tier; see `.env.example` at the repo root):
105
+
106
+ ```
107
+ PERSONA_FRONTIER_PROVIDER=anthropic PERSONA_FRONTIER_MODEL=claude-opus-...
108
+ PERSONA_MID_PROVIDER=deepseek PERSONA_MID_MODEL=deepseek-chat
109
+ PERSONA_SMALL_PROVIDER=groq PERSONA_SMALL_MODEL=llama-...
110
+ ```
111
+
112
+ A single `PERSONA_PROVIDER` + `PERSONA_MODEL` + `PERSONA_API_KEY` pair is
113
+ the fallback when no per-tier vars are set.
114
+
115
+ ## Test
116
+
117
+ ```bash
118
+ uv run pytest packages/runtime # unit (default)
119
+ uv run pytest packages/runtime -m integration # integration
120
+ uv run mypy packages/runtime/src
121
+ uv run ruff check packages/runtime
122
+ ```
123
+
124
+ ## Architecture role
125
+
126
+ `persona-runtime` is layer 3 of the Open Persona stack. It sits directly
127
+ above `persona-core` and below `persona-api`; the API composes the
128
+ runtime, attaches it to HTTP routes, and persists the per-request state
129
+ (conversation, run, turn-log, event bus). The runtime contains zero HTTP,
130
+ zero database client, zero secrets — every collaborator is injected by the
131
+ composition root.
132
+
133
+ ## Contribute
134
+
135
+ Contributions welcome under the same PolyForm Noncommercial 1.0.0 license.
136
+ The package is source-available for noncommercial use; commercial use
137
+ requires a separate license — contact the rights holder. Issues and pull
138
+ requests welcome at
139
+ [github.com/yasinhessnawi1/Open-Persona](https://github.com/yasinhessnawi1/Open-Persona).
140
+ See [CHANGELOG.md](CHANGELOG.md) for the spec-by-spec history.
@@ -0,0 +1,65 @@
1
+ [project]
2
+ name = "persona-runtime"
3
+ version = "0.1.0"
4
+ description = "Generation loop, router, and agentic engine for persona-core."
5
+ readme = "README.md"
6
+ requires-python = ">=3.11"
7
+ # Source-available, noncommercial use only. NOT OSI-approved.
8
+ # See LICENSE for full terms. Commercial use requires a separate license
9
+ # negotiated with the rights holder.
10
+ license = "PolyForm-Noncommercial-1.0.0"
11
+ license-files = ["LICENSE"]
12
+ authors = [
13
+ { name = "Yasin Hessnawi", email = "yasinhessnawi1@gmail.com" },
14
+ ]
15
+ keywords = [
16
+ "ai",
17
+ "persona",
18
+ "agent",
19
+ "llm",
20
+ "agentic",
21
+ "router",
22
+ "tool-use",
23
+ ]
24
+ classifiers = [
25
+ "Development Status :: 4 - Beta",
26
+ "Intended Audience :: Developers",
27
+ "License :: Other/Proprietary License",
28
+ "Operating System :: POSIX",
29
+ "Operating System :: MacOS",
30
+ "Operating System :: POSIX :: Linux",
31
+ "Programming Language :: Python",
32
+ "Programming Language :: Python :: 3",
33
+ "Programming Language :: Python :: 3.11",
34
+ "Programming Language :: Python :: 3.12",
35
+ "Topic :: Software Development :: Libraries",
36
+ "Topic :: Software Development :: Libraries :: Python Modules",
37
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
38
+ "Typing :: Typed",
39
+ ]
40
+ dependencies = [
41
+ "persona-core",
42
+ # Direct dep for prompt-token estimation in prompt.py (D-05-8). Already
43
+ # transitive via persona-core (D-01-11); declared directly per
44
+ # ENGINEERING_STANDARDS.md §5 ("declare directs, audit transitives").
45
+ "tiktoken>=0.7,<1",
46
+ ]
47
+
48
+ [project.urls]
49
+ Homepage = "https://github.com/yasinhessnawi1/Open-Persona"
50
+ Repository = "https://github.com/yasinhessnawi1/Open-Persona"
51
+ Issues = "https://github.com/yasinhessnawi1/Open-Persona/issues"
52
+ Changelog = "https://github.com/yasinhessnawi1/Open-Persona/blob/main/CHANGELOG.md"
53
+
54
+ [build-system]
55
+ requires = ["hatchling"]
56
+ build-backend = "hatchling.build"
57
+
58
+ [tool.hatch.build.targets.wheel]
59
+ packages = ["src/persona_runtime"]
60
+ include = [
61
+ "src/persona_runtime/py.typed",
62
+ ]
63
+
64
+ [tool.uv.sources]
65
+ persona-core = { workspace = true }
@@ -0,0 +1,42 @@
1
+ """Persona runtime — conversation loop, router, and agentic engine.
2
+
3
+ The public surface spec 06 (agentic loop) and spec 08 (API) import:
4
+
5
+ - :class:`ConversationLoop` — orchestrates one turn (the keystone).
6
+ - :class:`PromptBuilder` + :class:`RetrievedContext` — prompt assembly.
7
+ - :class:`Router` — rule-based tier selection.
8
+ - :class:`TierConfig` / :class:`TierRegistry` / :func:`tier_registry_from_env`
9
+ — tier configuration and the lazily-cached backend registry.
10
+ - :class:`TurnLog` / :class:`TurnLogWriter` / :class:`JSONLTurnLogWriter` /
11
+ :class:`MemoryTurnLogWriter` — per-turn telemetry.
12
+ - :exc:`TierNotConfiguredError` — the one runtime domain exception (D-05-2).
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from persona_runtime.errors import TierNotConfiguredError
18
+ from persona_runtime.logging import (
19
+ JSONLTurnLogWriter,
20
+ MemoryTurnLogWriter,
21
+ TurnLog,
22
+ TurnLogWriter,
23
+ )
24
+ from persona_runtime.loop import ConversationLoop
25
+ from persona_runtime.prompt import PromptBuilder, RetrievedContext
26
+ from persona_runtime.router import Router
27
+ from persona_runtime.tier import TierConfig, TierRegistry, tier_registry_from_env
28
+
29
+ __all__ = [
30
+ "ConversationLoop",
31
+ "JSONLTurnLogWriter",
32
+ "MemoryTurnLogWriter",
33
+ "PromptBuilder",
34
+ "RetrievedContext",
35
+ "Router",
36
+ "TierConfig",
37
+ "TierNotConfiguredError",
38
+ "TierRegistry",
39
+ "TurnLog",
40
+ "TurnLogWriter",
41
+ "tier_registry_from_env",
42
+ ]
@@ -0,0 +1,46 @@
1
+ """The agentic loop — plan-act-reflect execution for end-to-end tasks (spec 06).
2
+
3
+ When a chat turn is not enough ("draft a complaint about my landlord refusing to
4
+ fix mould"), :class:`AgenticLoop` runs the *simplest possible* agent loop: one
5
+ model decides at each step whether to call a tool, ask the user a question, or
6
+ produce a final answer — no multi-agent orchestration, no graph-of-thought
7
+ (architecture §5.2). The value is in the error-handling and budget management
8
+ around the loop, not the loop itself.
9
+
10
+ The public surface spec 08 (the API, which exposes ``/v1/runs``) imports:
11
+
12
+ - :class:`AgenticLoop` — the plan-act-reflect engine (lands in T06).
13
+ - :class:`Run` / :class:`RunStatus` / :class:`Step` / :class:`StepType` — the
14
+ serialisable run/step data model (T02).
15
+ - :class:`CancelToken` — caller-held cancellation control (T02).
16
+ - :class:`RunEvent` — the SSE event the API serialises for the run viewer (T03).
17
+ - :exc:`MaxStepsReachedError` / :exc:`RunCancelledError` — the two agentic
18
+ terminal exception types (defined, but the loop returns a ``Run`` rather than
19
+ raising; D-06-2).
20
+
21
+ Spec 08 owns what the loop does not (mirrors D-S05-4 / D-05-4): it persists the
22
+ ``Run`` per-step, supplies the ``user_respond`` blocking callback, serialises
23
+ ``RunEvent``\\ s to SSE, and owns the ``TierRegistry`` lifecycle.
24
+ """
25
+
26
+ from __future__ import annotations
27
+
28
+ from persona_runtime.agentic.compactor import StepHistoryCompactor
29
+ from persona_runtime.agentic.errors import MaxStepsReachedError, RunCancelledError
30
+ from persona_runtime.agentic.events import RunEvent
31
+ from persona_runtime.agentic.loop import AgenticLoop
32
+ from persona_runtime.agentic.run import CancelToken, Run, RunStatus
33
+ from persona_runtime.agentic.step import Step, StepType
34
+
35
+ __all__ = [
36
+ "AgenticLoop",
37
+ "CancelToken",
38
+ "MaxStepsReachedError",
39
+ "Run",
40
+ "RunCancelledError",
41
+ "RunEvent",
42
+ "RunStatus",
43
+ "Step",
44
+ "StepHistoryCompactor",
45
+ "StepType",
46
+ ]