aihi-agent 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 (162) hide show
  1. aihi_agent-0.1.0/.gitignore +25 -0
  2. aihi_agent-0.1.0/PKG-INFO +161 -0
  3. aihi_agent-0.1.0/README.md +148 -0
  4. aihi_agent-0.1.0/README.zh-CN.md +71 -0
  5. aihi_agent-0.1.0/pyproject.toml +28 -0
  6. aihi_agent-0.1.0/src/aihi/agent/__init__.py +323 -0
  7. aihi_agent-0.1.0/src/aihi/agent/_core/__init__.py +5 -0
  8. aihi_agent-0.1.0/src/aihi/agent/_core/awaits.py +44 -0
  9. aihi_agent-0.1.0/src/aihi/agent/_core/errors.py +87 -0
  10. aihi_agent-0.1.0/src/aihi/agent/_core/events.py +67 -0
  11. aihi_agent-0.1.0/src/aihi/agent/_core/ids.py +29 -0
  12. aihi_agent-0.1.0/src/aihi/agent/_core/schema.py +135 -0
  13. aihi_agent-0.1.0/src/aihi/agent/agents/__init__.py +46 -0
  14. aihi_agent-0.1.0/src/aihi/agent/agents/errors.py +37 -0
  15. aihi_agent-0.1.0/src/aihi/agent/agents/graph.py +427 -0
  16. aihi_agent-0.1.0/src/aihi/agent/agents/subagent.py +567 -0
  17. aihi_agent-0.1.0/src/aihi/agent/agents/types.py +498 -0
  18. aihi_agent-0.1.0/src/aihi/agent/artifacts/__init__.py +19 -0
  19. aihi_agent-0.1.0/src/aihi/agent/artifacts/store.py +411 -0
  20. aihi_agent-0.1.0/src/aihi/agent/builder.py +334 -0
  21. aihi_agent-0.1.0/src/aihi/agent/context/__init__.py +37 -0
  22. aihi_agent-0.1.0/src/aihi/agent/context/compiler.py +452 -0
  23. aihi_agent-0.1.0/src/aihi/agent/context/model_summary.py +170 -0
  24. aihi_agent-0.1.0/src/aihi/agent/context/summary.py +118 -0
  25. aihi_agent-0.1.0/src/aihi/agent/evals/__init__.py +53 -0
  26. aihi_agent-0.1.0/src/aihi/agent/evals/dataset.py +100 -0
  27. aihi_agent-0.1.0/src/aihi/agent/evals/errors.py +19 -0
  28. aihi_agent-0.1.0/src/aihi/agent/evals/golden.py +90 -0
  29. aihi_agent-0.1.0/src/aihi/agent/evals/graders.py +114 -0
  30. aihi_agent-0.1.0/src/aihi/agent/evals/replay.py +387 -0
  31. aihi_agent-0.1.0/src/aihi/agent/evals/runner.py +53 -0
  32. aihi_agent-0.1.0/src/aihi/agent/evals/trace_graph.py +227 -0
  33. aihi_agent-0.1.0/src/aihi/agent/hooks/__init__.py +35 -0
  34. aihi_agent-0.1.0/src/aihi/agent/hooks/bus.py +310 -0
  35. aihi_agent-0.1.0/src/aihi/agent/hooks/errors.py +29 -0
  36. aihi_agent-0.1.0/src/aihi/agent/mcp/__init__.py +45 -0
  37. aihi_agent-0.1.0/src/aihi/agent/mcp/client.py +283 -0
  38. aihi_agent-0.1.0/src/aihi/agent/mcp/errors.py +45 -0
  39. aihi_agent-0.1.0/src/aihi/agent/mcp/protocol.py +253 -0
  40. aihi_agent-0.1.0/src/aihi/agent/mcp/registration.py +39 -0
  41. aihi_agent-0.1.0/src/aihi/agent/mcp/server.py +138 -0
  42. aihi_agent-0.1.0/src/aihi/agent/mcp/stdio.py +222 -0
  43. aihi_agent-0.1.0/src/aihi/agent/mcp/transport.py +90 -0
  44. aihi_agent-0.1.0/src/aihi/agent/memory/__init__.py +43 -0
  45. aihi_agent-0.1.0/src/aihi/agent/memory/context.py +108 -0
  46. aihi_agent-0.1.0/src/aihi/agent/memory/errors.py +34 -0
  47. aihi_agent-0.1.0/src/aihi/agent/memory/extraction.py +87 -0
  48. aihi_agent-0.1.0/src/aihi/agent/memory/redaction.py +138 -0
  49. aihi_agent-0.1.0/src/aihi/agent/memory/service.py +273 -0
  50. aihi_agent-0.1.0/src/aihi/agent/memory/store.py +135 -0
  51. aihi_agent-0.1.0/src/aihi/agent/memory/types.py +375 -0
  52. aihi_agent-0.1.0/src/aihi/agent/observability/__init__.py +38 -0
  53. aihi_agent-0.1.0/src/aihi/agent/observability/exporters.py +94 -0
  54. aihi_agent-0.1.0/src/aihi/agent/observability/telemetry.py +470 -0
  55. aihi_agent-0.1.0/src/aihi/agent/plugins/__init__.py +59 -0
  56. aihi_agent-0.1.0/src/aihi/agent/plugins/discovery.py +246 -0
  57. aihi_agent-0.1.0/src/aihi/agent/plugins/errors.py +64 -0
  58. aihi_agent-0.1.0/src/aihi/agent/plugins/host.py +512 -0
  59. aihi_agent-0.1.0/src/aihi/agent/plugins/host_protocol.py +123 -0
  60. aihi_agent-0.1.0/src/aihi/agent/plugins/host_worker.py +241 -0
  61. aihi_agent-0.1.0/src/aihi/agent/plugins/manifest.py +249 -0
  62. aihi_agent-0.1.0/src/aihi/agent/plugins/registration.py +38 -0
  63. aihi_agent-0.1.0/src/aihi/agent/plugins/trust.py +272 -0
  64. aihi_agent-0.1.0/src/aihi/agent/policy/__init__.py +39 -0
  65. aihi_agent-0.1.0/src/aihi/agent/policy/approvals.py +152 -0
  66. aihi_agent-0.1.0/src/aihi/agent/policy/engine.py +219 -0
  67. aihi_agent-0.1.0/src/aihi/agent/policy/leases.py +271 -0
  68. aihi_agent-0.1.0/src/aihi/agent/py.typed +0 -0
  69. aihi_agent-0.1.0/src/aihi/agent/runtime/__init__.py +24 -0
  70. aihi_agent-0.1.0/src/aihi/agent/runtime/coordinator.py +1381 -0
  71. aihi_agent-0.1.0/src/aihi/agent/runtime/extensions.py +84 -0
  72. aihi_agent-0.1.0/src/aihi/agent/runtime/state.py +78 -0
  73. aihi_agent-0.1.0/src/aihi/agent/sandbox/__init__.py +16 -0
  74. aihi_agent-0.1.0/src/aihi/agent/sandbox/base.py +75 -0
  75. aihi_agent-0.1.0/src/aihi/agent/sandbox/docker.py +459 -0
  76. aihi_agent-0.1.0/src/aihi/agent/sandbox/host.py +269 -0
  77. aihi_agent-0.1.0/src/aihi/agent/sandbox/local.py +456 -0
  78. aihi_agent-0.1.0/src/aihi/agent/sandbox/scoped.py +135 -0
  79. aihi_agent-0.1.0/src/aihi/agent/sandbox/walk.py +88 -0
  80. aihi_agent-0.1.0/src/aihi/agent/sessions/__init__.py +20 -0
  81. aihi_agent-0.1.0/src/aihi/agent/sessions/session.py +505 -0
  82. aihi_agent-0.1.0/src/aihi/agent/sessions/snapshots.py +53 -0
  83. aihi_agent-0.1.0/src/aihi/agent/sessions/store.py +379 -0
  84. aihi_agent-0.1.0/src/aihi/agent/skills/__init__.py +49 -0
  85. aihi_agent-0.1.0/src/aihi/agent/skills/context.py +67 -0
  86. aihi_agent-0.1.0/src/aihi/agent/skills/discovery.py +232 -0
  87. aihi_agent-0.1.0/src/aihi/agent/skills/errors.py +44 -0
  88. aihi_agent-0.1.0/src/aihi/agent/skills/loader.py +83 -0
  89. aihi_agent-0.1.0/src/aihi/agent/skills/manifest.py +267 -0
  90. aihi_agent-0.1.0/src/aihi/agent/skills/trust.py +314 -0
  91. aihi_agent-0.1.0/src/aihi/agent/tools/__init__.py +33 -0
  92. aihi_agent-0.1.0/src/aihi/agent/tools/base.py +71 -0
  93. aihi_agent-0.1.0/src/aihi/agent/tools/builtin/__init__.py +22 -0
  94. aihi_agent-0.1.0/src/aihi/agent/tools/builtin/bash.py +92 -0
  95. aihi_agent-0.1.0/src/aihi/agent/tools/builtin/command.py +38 -0
  96. aihi_agent-0.1.0/src/aihi/agent/tools/builtin/edit_file.py +95 -0
  97. aihi_agent-0.1.0/src/aihi/agent/tools/builtin/ledger.py +50 -0
  98. aihi_agent-0.1.0/src/aihi/agent/tools/builtin/read_file.py +66 -0
  99. aihi_agent-0.1.0/src/aihi/agent/tools/builtin/search.py +162 -0
  100. aihi_agent-0.1.0/src/aihi/agent/tools/builtin/write_file.py +78 -0
  101. aihi_agent-0.1.0/src/aihi/agent/tools/dispatcher.py +207 -0
  102. aihi_agent-0.1.0/src/aihi/agent/tools/registry.py +30 -0
  103. aihi_agent-0.1.0/src/aihi/agent/tools/spec.py +76 -0
  104. aihi_agent-0.1.0/tests/contract/test_docker_sandbox.py +71 -0
  105. aihi_agent-0.1.0/tests/contract/test_eval_replay.py +226 -0
  106. aihi_agent-0.1.0/tests/contract/test_event_compatibility.py +293 -0
  107. aihi_agent-0.1.0/tests/contract/test_event_stores.py +174 -0
  108. aihi_agent-0.1.0/tests/contract/test_golden_tasks.py +44 -0
  109. aihi_agent-0.1.0/tests/contract/test_layering.py +143 -0
  110. aihi_agent-0.1.0/tests/contract/test_local_sandbox.py +73 -0
  111. aihi_agent-0.1.0/tests/contract/test_observability_exporters.py +72 -0
  112. aihi_agent-0.1.0/tests/contract/test_public_api.py +157 -0
  113. aihi_agent-0.1.0/tests/contract/test_remote_tool_registration.py +186 -0
  114. aihi_agent-0.1.0/tests/integration/test_approval_flow.py +413 -0
  115. aihi_agent-0.1.0/tests/integration/test_event_write_amplification.py +131 -0
  116. aihi_agent-0.1.0/tests/integration/test_model_compaction.py +178 -0
  117. aihi_agent-0.1.0/tests/integration/test_one_shot_approval.py +153 -0
  118. aihi_agent-0.1.0/tests/integration/test_parallel_tools.py +179 -0
  119. aihi_agent-0.1.0/tests/integration/test_run_termination.py +136 -0
  120. aihi_agent-0.1.0/tests/integration/test_runtime_builder.py +260 -0
  121. aihi_agent-0.1.0/tests/integration/test_runtime_coordinator.py +531 -0
  122. aihi_agent-0.1.0/tests/integration/test_runtime_extensions.py +202 -0
  123. aihi_agent-0.1.0/tests/integration/test_runtime_observability_lifecycle.py +76 -0
  124. aihi_agent-0.1.0/tests/integration/test_session_fork.py +155 -0
  125. aihi_agent-0.1.0/tests/integration/test_subagent_runs.py +349 -0
  126. aihi_agent-0.1.0/tests/integration/test_trace_graph.py +183 -0
  127. aihi_agent-0.1.0/tests/security/test_accept_edits_scope.py +120 -0
  128. aihi_agent-0.1.0/tests/security/test_agents_security.py +91 -0
  129. aihi_agent-0.1.0/tests/security/test_authorization_events.py +199 -0
  130. aihi_agent-0.1.0/tests/security/test_bash_tool.py +92 -0
  131. aihi_agent-0.1.0/tests/security/test_docker_sandbox_security.py +38 -0
  132. aihi_agent-0.1.0/tests/security/test_eval_replay_security.py +65 -0
  133. aihi_agent-0.1.0/tests/security/test_hooks_governance.py +28 -0
  134. aihi_agent-0.1.0/tests/security/test_local_sandbox_policy.py +80 -0
  135. aihi_agent-0.1.0/tests/security/test_local_sandbox_security.py +50 -0
  136. aihi_agent-0.1.0/tests/security/test_mcp_policy.py +50 -0
  137. aihi_agent-0.1.0/tests/security/test_memory_security.py +158 -0
  138. aihi_agent-0.1.0/tests/security/test_mutating_tools.py +244 -0
  139. aihi_agent-0.1.0/tests/security/test_observability_security.py +41 -0
  140. aihi_agent-0.1.0/tests/security/test_plugin_host_security.py +70 -0
  141. aihi_agent-0.1.0/tests/security/test_runtime_security.py +133 -0
  142. aihi_agent-0.1.0/tests/security/test_scoped_sandbox.py +54 -0
  143. aihi_agent-0.1.0/tests/security/test_search_tools.py +138 -0
  144. aihi_agent-0.1.0/tests/security/test_session_invariants.py +34 -0
  145. aihi_agent-0.1.0/tests/security/test_skill_security.py +39 -0
  146. aihi_agent-0.1.0/tests/unit/test_agents.py +87 -0
  147. aihi_agent-0.1.0/tests/unit/test_artifact_store.py +87 -0
  148. aihi_agent-0.1.0/tests/unit/test_context_compiler.py +140 -0
  149. aihi_agent-0.1.0/tests/unit/test_core.py +49 -0
  150. aihi_agent-0.1.0/tests/unit/test_hooks.py +160 -0
  151. aihi_agent-0.1.0/tests/unit/test_mcp.py +278 -0
  152. aihi_agent-0.1.0/tests/unit/test_memory.py +166 -0
  153. aihi_agent-0.1.0/tests/unit/test_observability.py +148 -0
  154. aihi_agent-0.1.0/tests/unit/test_plugin_host.py +180 -0
  155. aihi_agent-0.1.0/tests/unit/test_plugins.py +182 -0
  156. aihi_agent-0.1.0/tests/unit/test_read_before_write.py +111 -0
  157. aihi_agent-0.1.0/tests/unit/test_runtime_state.py +13 -0
  158. aihi_agent-0.1.0/tests/unit/test_skills.py +243 -0
  159. aihi_agent-0.1.0/tests/unit/test_subagent_named_runners.py +154 -0
  160. aihi_agent-0.1.0/tests/unit/test_token_estimation.py +74 -0
  161. aihi_agent-0.1.0/tests/unit/test_tool_validation.py +39 -0
  162. aihi_agent-0.1.0/tests/unit/test_usage_events.py +40 -0
@@ -0,0 +1,25 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ node_modules/
4
+ .pnpm-store/
5
+ .venv/
6
+ venv/
7
+ *.egg-info/
8
+ dist/
9
+ build/
10
+ .pytest_cache/
11
+ .ruff_cache/
12
+ .mypy_cache/
13
+ .aihi/settings.local.json
14
+ .aihi/audit.jsonl
15
+ .DS_Store
16
+
17
+ # Local legacy SQLite compatibility fixture and SQLite WAL sidecars.
18
+ /tests/fixtures/session_schema_v1.sqlite3*
19
+
20
+ # Superpowers working documents: local process artefacts, not project docs.
21
+ /docs/superpowers/
22
+
23
+ # Architecture decision records and RFC drafts are local working notes.
24
+ /docs/adr/
25
+ /docs/rfcs/
@@ -0,0 +1,161 @@
1
+ Metadata-Version: 2.5
2
+ Name: aihi-agent
3
+ Version: 0.1.0
4
+ Summary: Recoverable provider-neutral Agent Runtime for AIHI
5
+ License: MIT
6
+ Classifier: Programming Language :: Python :: 3.11
7
+ Classifier: Programming Language :: Python :: 3.12
8
+ Classifier: Programming Language :: Python :: 3.13
9
+ Classifier: Typing :: Typed
10
+ Requires-Python: >=3.11
11
+ Requires-Dist: aihi-models<0.2,>=0.1
12
+ Description-Content-Type: text/markdown
13
+
14
+ # aihi-agent
15
+
16
+ [English] | [简体中文](README.zh-CN.md)
17
+
18
+ Provider-neutral, recoverable agent runtime for AIHI.
19
+
20
+ `aihi-agent` turns model contracts into a durable execution system. It provides the loop, sessions, tools, policy, approvals, sandbox boundary, context management, integrations, and observability that an application can compose for a specific product.
21
+
22
+ ## Responsibilities
23
+
24
+ - Run bounded model/tool turns with explicit runtime composition.
25
+ - Persist an append-only event log and recover sessions after interruption.
26
+ - Compile context and compact it into derived summaries without rewriting history.
27
+ - Register and execute tools through policy, approvals, hooks, and a sandbox backend.
28
+ - Integrate Skills, MCP servers, subagents, memory, artifacts, telemetry, replay, and evaluations.
29
+
30
+ The package does **not** select a provider, implement a UI, provide a model router/gateway, or hide tool defaults. Applications pass those choices to `RuntimeBuilder`.
31
+
32
+ ## Architecture
33
+
34
+ ```text
35
+ Model Provider (aihi-models)
36
+ │
37
+ ▼
38
+ RuntimeBuilder ──► Runtime / RunCoordinator ──► EventStore
39
+ │ │
40
+ │ ├── ContextCompiler / Compaction
41
+ │ ├── ToolRegistry ──► Policy ──► Approval
42
+ │ │ │
43
+ │ │ ▼
44
+ │ └── Hooks ──► SandboxBackend ──► Tool
45
+ │
46
+ └── Skills / MCP / Subagents / Memory / Artifacts / Telemetry
47
+ ```
48
+
49
+ The event store is the source of truth. Tool calls are recorded before execution and have exactly one result. An approval decision of `ASK` suspends the run so it can be resumed later.
50
+
51
+ ## Installation
52
+
53
+ From the workspace:
54
+
55
+ ```bash
56
+ uv sync
57
+ ```
58
+
59
+ For a local editable install:
60
+
61
+ ```bash
62
+ uv pip install -e packages/aihi/agent
63
+ ```
64
+
65
+ `aihi-agent` requires Python 3.11+ and depends on `aihi-models` 0.1.x.
66
+
67
+ ## Minimal runtime
68
+
69
+ ```python
70
+ from pathlib import Path
71
+
72
+ from aihi.agent import HostBackend, InMemoryEventStore, ReadFileTool, RuntimeBuilder, Session
73
+ from aihi.models import FakeProvider, FakeStep, Message
74
+
75
+ provider = FakeProvider([FakeStep(text="I inspected the workspace.")])
76
+ runtime = (
77
+ RuntimeBuilder(
78
+ provider=provider,
79
+ model="fake-model",
80
+ sandbox=HostBackend(Path.cwd(), unsafe=True),
81
+ tools=[ReadFileTool()],
82
+ )
83
+ .with_max_turns(20)
84
+ .build()
85
+ )
86
+
87
+ session = Session.create(
88
+ InMemoryEventStore(),
89
+ cwd=Path.cwd(),
90
+ provider="fake",
91
+ model="fake-model",
92
+ )
93
+
94
+ result = await runtime.coordinator.run(
95
+ session,
96
+ model=runtime.model,
97
+ user_message=Message.text("user", "Inspect this project."),
98
+ )
99
+ print(result.state)
100
+ ```
101
+
102
+ For real applications, prefer an isolated backend when available. `HostBackend` is a controlled local execution backend, not a security isolation boundary, and requires an explicit `unsafe=True` acknowledgement.
103
+
104
+ ## Runtime composition
105
+
106
+ `RuntimeBuilder` requires the important dependencies up front:
107
+
108
+ - `provider` and `model`;
109
+ - a `sandbox` backend;
110
+ - the application-approved `tools` collection.
111
+
112
+ Optional extensions are added explicitly with methods such as:
113
+
114
+ - `.with_max_turns(...)` and `.with_context_window(...)`;
115
+ - `.with_policy(...)`, `.with_approvals(...)`, and `.with_hooks(...)`;
116
+ - `.with_skills(...)`, `.with_memory(...)`, `.with_compaction(...)`;
117
+ - `.with_subagents(...)`, `.with_artifacts(...)`, and `.with_telemetry(...)`.
118
+
119
+ The default coordinator turn budget is finite (`100`) and can be lowered for a product-specific safety envelope.
120
+
121
+ ## Core modules
122
+
123
+ | Area | Main API |
124
+ | --- | --- |
125
+ | Runtime and runs | `Runtime`, `RuntimeBuilder`, `RunCoordinator`, `RunResult`, `RunState` |
126
+ | Sessions and storage | `Session`, `EventStore`, `InMemoryEventStore`, `SQLiteEventStore`, `Event` |
127
+ | Context | `ContextCompiler`, summaries, compaction generators |
128
+ | Tools | `Tool`, `ToolSpec`, `ToolContext`, `ToolRegistry`, built-in file/shell tools |
129
+ | Policy and approval | `PermissionMode`, `DefaultPolicyEngine`, `Approval`, approval resolvers |
130
+ | Sandbox | `HostBackend`, `LocalIsolatedBackend`, `DockerBackend` |
131
+ | Integrations | Skills, MCP, plugins, subagents, memory, artifacts |
132
+ | Observability | `Telemetry`, `JsonlTelemetrySink`, `InMemoryTelemetrySink` |
133
+ | Verification | replay, golden tasks, evals, and contract helpers |
134
+
135
+ ## Tool and approval model
136
+
137
+ Tools are registered with explicit `ToolSpec` metadata. The policy engine decides whether an invocation is allowed, denied, or must ask for approval. Approval leases can scope a decision to a request, a tool, or a run according to the application policy.
138
+
139
+ Use the built-in tools only with a sandbox and policy appropriate for the workspace. File reads, glob/grep, edits, writes, and shell execution should not be treated as interchangeable capabilities.
140
+
141
+ ## Observability
142
+
143
+ Telemetry is an observation stream, not the event log. `JsonlTelemetrySink` emits redacted, bounded records and creates owner-only files by default. Use the event store for recovery and audit the telemetry stream for operational diagnosis; do not use UI output as a source of truth.
144
+
145
+ ## Development
146
+
147
+ ```bash
148
+ uv run pytest packages/aihi/agent/tests
149
+ uv run ruff check packages/aihi/agent
150
+ uv run mypy
151
+ uv run python -m build --wheel --no-isolation packages/aihi/agent
152
+ ```
153
+
154
+ See the repository [architecture guide](../../../docs/ARCHITECTURE.md) and the [code-agent README](../code-agent/README.md) for an application-level composition.
155
+
156
+ ## Security model
157
+
158
+ - Keep credentials in the application/provider boundary, never in prompts or event payloads.
159
+ - Treat model output, tool arguments, Skills, MCP responses, and subagent output as untrusted.
160
+ - Do not claim that `HostBackend` isolates a process; use `LocalIsolatedBackend` or `DockerBackend` when isolation is required.
161
+ - Set a finite turn limit and review approval/policy defaults before exposing tools to a model.
@@ -0,0 +1,148 @@
1
+ # aihi-agent
2
+
3
+ [English] | [简体中文](README.zh-CN.md)
4
+
5
+ Provider-neutral, recoverable agent runtime for AIHI.
6
+
7
+ `aihi-agent` turns model contracts into a durable execution system. It provides the loop, sessions, tools, policy, approvals, sandbox boundary, context management, integrations, and observability that an application can compose for a specific product.
8
+
9
+ ## Responsibilities
10
+
11
+ - Run bounded model/tool turns with explicit runtime composition.
12
+ - Persist an append-only event log and recover sessions after interruption.
13
+ - Compile context and compact it into derived summaries without rewriting history.
14
+ - Register and execute tools through policy, approvals, hooks, and a sandbox backend.
15
+ - Integrate Skills, MCP servers, subagents, memory, artifacts, telemetry, replay, and evaluations.
16
+
17
+ The package does **not** select a provider, implement a UI, provide a model router/gateway, or hide tool defaults. Applications pass those choices to `RuntimeBuilder`.
18
+
19
+ ## Architecture
20
+
21
+ ```text
22
+ Model Provider (aihi-models)
23
+ │
24
+ ▼
25
+ RuntimeBuilder ──► Runtime / RunCoordinator ──► EventStore
26
+ │ │
27
+ │ ├── ContextCompiler / Compaction
28
+ │ ├── ToolRegistry ──► Policy ──► Approval
29
+ │ │ │
30
+ │ │ ▼
31
+ │ └── Hooks ──► SandboxBackend ──► Tool
32
+ │
33
+ └── Skills / MCP / Subagents / Memory / Artifacts / Telemetry
34
+ ```
35
+
36
+ The event store is the source of truth. Tool calls are recorded before execution and have exactly one result. An approval decision of `ASK` suspends the run so it can be resumed later.
37
+
38
+ ## Installation
39
+
40
+ From the workspace:
41
+
42
+ ```bash
43
+ uv sync
44
+ ```
45
+
46
+ For a local editable install:
47
+
48
+ ```bash
49
+ uv pip install -e packages/aihi/agent
50
+ ```
51
+
52
+ `aihi-agent` requires Python 3.11+ and depends on `aihi-models` 0.1.x.
53
+
54
+ ## Minimal runtime
55
+
56
+ ```python
57
+ from pathlib import Path
58
+
59
+ from aihi.agent import HostBackend, InMemoryEventStore, ReadFileTool, RuntimeBuilder, Session
60
+ from aihi.models import FakeProvider, FakeStep, Message
61
+
62
+ provider = FakeProvider([FakeStep(text="I inspected the workspace.")])
63
+ runtime = (
64
+ RuntimeBuilder(
65
+ provider=provider,
66
+ model="fake-model",
67
+ sandbox=HostBackend(Path.cwd(), unsafe=True),
68
+ tools=[ReadFileTool()],
69
+ )
70
+ .with_max_turns(20)
71
+ .build()
72
+ )
73
+
74
+ session = Session.create(
75
+ InMemoryEventStore(),
76
+ cwd=Path.cwd(),
77
+ provider="fake",
78
+ model="fake-model",
79
+ )
80
+
81
+ result = await runtime.coordinator.run(
82
+ session,
83
+ model=runtime.model,
84
+ user_message=Message.text("user", "Inspect this project."),
85
+ )
86
+ print(result.state)
87
+ ```
88
+
89
+ For real applications, prefer an isolated backend when available. `HostBackend` is a controlled local execution backend, not a security isolation boundary, and requires an explicit `unsafe=True` acknowledgement.
90
+
91
+ ## Runtime composition
92
+
93
+ `RuntimeBuilder` requires the important dependencies up front:
94
+
95
+ - `provider` and `model`;
96
+ - a `sandbox` backend;
97
+ - the application-approved `tools` collection.
98
+
99
+ Optional extensions are added explicitly with methods such as:
100
+
101
+ - `.with_max_turns(...)` and `.with_context_window(...)`;
102
+ - `.with_policy(...)`, `.with_approvals(...)`, and `.with_hooks(...)`;
103
+ - `.with_skills(...)`, `.with_memory(...)`, `.with_compaction(...)`;
104
+ - `.with_subagents(...)`, `.with_artifacts(...)`, and `.with_telemetry(...)`.
105
+
106
+ The default coordinator turn budget is finite (`100`) and can be lowered for a product-specific safety envelope.
107
+
108
+ ## Core modules
109
+
110
+ | Area | Main API |
111
+ | --- | --- |
112
+ | Runtime and runs | `Runtime`, `RuntimeBuilder`, `RunCoordinator`, `RunResult`, `RunState` |
113
+ | Sessions and storage | `Session`, `EventStore`, `InMemoryEventStore`, `SQLiteEventStore`, `Event` |
114
+ | Context | `ContextCompiler`, summaries, compaction generators |
115
+ | Tools | `Tool`, `ToolSpec`, `ToolContext`, `ToolRegistry`, built-in file/shell tools |
116
+ | Policy and approval | `PermissionMode`, `DefaultPolicyEngine`, `Approval`, approval resolvers |
117
+ | Sandbox | `HostBackend`, `LocalIsolatedBackend`, `DockerBackend` |
118
+ | Integrations | Skills, MCP, plugins, subagents, memory, artifacts |
119
+ | Observability | `Telemetry`, `JsonlTelemetrySink`, `InMemoryTelemetrySink` |
120
+ | Verification | replay, golden tasks, evals, and contract helpers |
121
+
122
+ ## Tool and approval model
123
+
124
+ Tools are registered with explicit `ToolSpec` metadata. The policy engine decides whether an invocation is allowed, denied, or must ask for approval. Approval leases can scope a decision to a request, a tool, or a run according to the application policy.
125
+
126
+ Use the built-in tools only with a sandbox and policy appropriate for the workspace. File reads, glob/grep, edits, writes, and shell execution should not be treated as interchangeable capabilities.
127
+
128
+ ## Observability
129
+
130
+ Telemetry is an observation stream, not the event log. `JsonlTelemetrySink` emits redacted, bounded records and creates owner-only files by default. Use the event store for recovery and audit the telemetry stream for operational diagnosis; do not use UI output as a source of truth.
131
+
132
+ ## Development
133
+
134
+ ```bash
135
+ uv run pytest packages/aihi/agent/tests
136
+ uv run ruff check packages/aihi/agent
137
+ uv run mypy
138
+ uv run python -m build --wheel --no-isolation packages/aihi/agent
139
+ ```
140
+
141
+ See the repository [architecture guide](../../../docs/ARCHITECTURE.md) and the [code-agent README](../code-agent/README.md) for an application-level composition.
142
+
143
+ ## Security model
144
+
145
+ - Keep credentials in the application/provider boundary, never in prompts or event payloads.
146
+ - Treat model output, tool arguments, Skills, MCP responses, and subagent output as untrusted.
147
+ - Do not claim that `HostBackend` isolates a process; use `LocalIsolatedBackend` or `DockerBackend` when isolation is required.
148
+ - Set a finite turn limit and review approval/policy defaults before exposing tools to a model.
@@ -0,0 +1,71 @@
1
+ # aihi-agent
2
+
3
+ [English](README.md) | **简体中文**
4
+
5
+ AIHI 的 Provider-neutral、可恢复 Agent Runtime。它把模型契约转换成带持久化、策略和安全边界的
6
+ 执行系统,供具体应用组合。
7
+
8
+ ## 职责
9
+
10
+ - 通过显式 `RuntimeBuilder` 运行有界的 model/tool turns。
11
+ - 追加事件日志并在中断后恢复 Session。
12
+ - 编译上下文、预算保护和派生摘要压缩。
13
+ - 通过 Policy、Approval、Hooks 和 Sandbox 执行工具。
14
+ - 提供 Skill、MCP、Plugin、Subagent、Memory、Artifact、Telemetry、Replay 和 Eval 接入点。
15
+
16
+ 本包不选择 Provider、不实现 UI、不提供 Router/Gateway,也不隐藏工具默认值。
17
+
18
+ ## 架构
19
+
20
+ ```text
21
+ aihi.models Provider
22
+ │
23
+ RuntimeBuilder → RunCoordinator → EventStore
24
+ │ ├─ ContextCompiler / Compaction
25
+ │ ├─ ToolRegistry → Policy → Approval
26
+ │ └─ Hooks → SandboxBackend → Tool
27
+ └─ Skills / MCP / Subagents / Memory / Artifacts
28
+ ```
29
+
30
+ 事件存储是事实源。工具调用先落盘再执行,并且每个调用恰好有一个结果;Policy 返回 `ASK` 时
31
+ Run 会挂起,等待应用层解决后恢复。
32
+
33
+ ## 安装与最小运行
34
+
35
+ ```bash
36
+ uv sync
37
+ uv pip install -e packages/aihi/agent
38
+ ```
39
+
40
+ ```python
41
+ from aihi.agent import RuntimeBuilder
42
+
43
+ runtime = (
44
+ RuntimeBuilder()
45
+ .with_provider(provider, model="my-model")
46
+ .with_sandbox(sandbox)
47
+ .with_tools(tool_registry)
48
+ .build()
49
+ )
50
+ ```
51
+
52
+ Provider、Sandbox 和 tools 必须由应用显式注入;不存在无条件选择这些依赖的
53
+ `default_runtime()`。
54
+
55
+ ## 核心约束
56
+
57
+ - 默认 loop 有最大 turns,防止无界消耗 token。
58
+ - 读文件、Glob、Grep 等声明为并发安全的只读工具可并行执行;修改工具保持顺序。
59
+ - Host backend 必须显式 `unsafe=true`。
60
+ - Resume 使用首次 `run.started` 固化的 Provider、Model、Workspace、权限和预算。
61
+ - 子 Agent 使用独立 Session,并只能获得父级权限、预算和 workspace 的更严格子集。
62
+
63
+ ## 开发
64
+
65
+ ```bash
66
+ pytest packages/aihi/agent/tests
67
+ ruff check packages/aihi/agent
68
+ mypy packages/aihi/agent/src
69
+ ```
70
+
71
+ 参见 [架构文档](../../../docs/ARCHITECTURE.zh-CN.md) 和 [Coding Agent 文档](../code-agent/README.zh-CN.md)。
@@ -0,0 +1,28 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "aihi-agent"
7
+ version = "0.1.0"
8
+ description = "Recoverable provider-neutral Agent Runtime for AIHI"
9
+ readme = "README.md"
10
+ requires-python = ">=3.11"
11
+ license = { text = "MIT" }
12
+ dependencies = [
13
+ "aihi-models>=0.1,<0.2",
14
+ ]
15
+
16
+ classifiers = [
17
+ "Programming Language :: Python :: 3.11",
18
+ "Programming Language :: Python :: 3.12",
19
+ "Programming Language :: Python :: 3.13",
20
+ "Typing :: Typed",
21
+ ]
22
+
23
+ [tool.uv.sources]
24
+ aihi-models = { workspace = true }
25
+
26
+ [tool.hatch.build.targets.wheel]
27
+ packages = ["src/aihi"]
28
+ artifacts = ["src/aihi/agent/py.typed"]