ctx-sdk 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.
- ctx_sdk-0.1.0/.gitignore +38 -0
- ctx_sdk-0.1.0/PKG-INFO +108 -0
- ctx_sdk-0.1.0/README.md +74 -0
- ctx_sdk-0.1.0/pyproject.toml +69 -0
- ctx_sdk-0.1.0/src/ctx/__init__.py +149 -0
- ctx_sdk-0.1.0/src/ctx/agent/README.md +142 -0
- ctx_sdk-0.1.0/src/ctx/agent/__init__.py +60 -0
- ctx_sdk-0.1.0/src/ctx/agent/agent_runner.py +91 -0
- ctx_sdk-0.1.0/src/ctx/agent/agents/__init__.py +61 -0
- ctx_sdk-0.1.0/src/ctx/agent/agents/activate.py +156 -0
- ctx_sdk-0.1.0/src/ctx/agent/agents/api.py +355 -0
- ctx_sdk-0.1.0/src/ctx/agent/agents/discovery.py +113 -0
- ctx_sdk-0.1.0/src/ctx/agent/agents/loader.py +197 -0
- ctx_sdk-0.1.0/src/ctx/agent/agents/registry.py +190 -0
- ctx_sdk-0.1.0/src/ctx/agent/agents/schema.py +172 -0
- ctx_sdk-0.1.0/src/ctx/agent/async_utils.py +40 -0
- ctx_sdk-0.1.0/src/ctx/agent/background.py +425 -0
- ctx_sdk-0.1.0/src/ctx/agent/context/__init__.py +45 -0
- ctx_sdk-0.1.0/src/ctx/agent/context/_xml.py +8 -0
- ctx_sdk-0.1.0/src/ctx/agent/context/agent_mds.py +134 -0
- ctx_sdk-0.1.0/src/ctx/agent/context/git.py +70 -0
- ctx_sdk-0.1.0/src/ctx/agent/context/loader.py +51 -0
- ctx_sdk-0.1.0/src/ctx/agent/context/skills.py +671 -0
- ctx_sdk-0.1.0/src/ctx/agent/context_governance.py +110 -0
- ctx_sdk-0.1.0/src/ctx/agent/dispatcher.py +94 -0
- ctx_sdk-0.1.0/src/ctx/agent/hooks/__init__.py +45 -0
- ctx_sdk-0.1.0/src/ctx/agent/hooks/agent_hook.py +161 -0
- ctx_sdk-0.1.0/src/ctx/agent/hooks/bridge.py +333 -0
- ctx_sdk-0.1.0/src/ctx/agent/hooks/loader.py +176 -0
- ctx_sdk-0.1.0/src/ctx/agent/hooks/registry.py +140 -0
- ctx_sdk-0.1.0/src/ctx/agent/hooks/runtime.py +157 -0
- ctx_sdk-0.1.0/src/ctx/agent/hooks/types.py +137 -0
- ctx_sdk-0.1.0/src/ctx/agent/loop.py +696 -0
- ctx_sdk-0.1.0/src/ctx/agent/prompts/__init__.py +35 -0
- ctx_sdk-0.1.0/src/ctx/agent/prompts/builder.py +139 -0
- ctx_sdk-0.1.0/src/ctx/agent/prompts/env.py +53 -0
- ctx_sdk-0.1.0/src/ctx/agent/prompts/registry.py +81 -0
- ctx_sdk-0.1.0/src/ctx/agent/prompts/tooling.py +42 -0
- ctx_sdk-0.1.0/src/ctx/agent/revert.py +597 -0
- ctx_sdk-0.1.0/src/ctx/agent/runtime.py +1842 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/__init__.py +142 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/_version.py +9 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/agent.py +514 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/approvals.py +104 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/guardrails/__init__.py +193 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/guardrails/types.py +132 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/handoffs.py +162 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/items.py +145 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/items_base.py +21 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/permissions.py +127 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/results.py +55 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/run_config.py +67 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/runner.py +857 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/sessions.py +260 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/skills.py +55 -0
- ctx_sdk-0.1.0/src/ctx/agent/sdk/tools.py +374 -0
- ctx_sdk-0.1.0/src/ctx/agent/session.py +1204 -0
- ctx_sdk-0.1.0/src/ctx/agent/skills.py +62 -0
- ctx_sdk-0.1.0/src/ctx/agent/skills_refresh.py +168 -0
- ctx_sdk-0.1.0/src/ctx/agent/snapshot.py +539 -0
- ctx_sdk-0.1.0/src/ctx/agent/subagent.py +505 -0
- ctx_sdk-0.1.0/src/ctx/agent/subagents.py +139 -0
- ctx_sdk-0.1.0/src/ctx/agent/tools/__init__.py +285 -0
- ctx_sdk-0.1.0/src/ctx/agent/tools/base.py +83 -0
- ctx_sdk-0.1.0/src/ctx/agent/tools/schema.py +173 -0
- ctx_sdk-0.1.0/src/ctx/agent/tools/schema_tool.py +140 -0
- ctx_sdk-0.1.0/src/ctx/agent/tools_manager.py +224 -0
- ctx_sdk-0.1.0/src/ctx/agent/turn.py +1497 -0
- ctx_sdk-0.1.0/src/ctx/ai/README.md +159 -0
- ctx_sdk-0.1.0/src/ctx/ai/__init__.py +203 -0
- ctx_sdk-0.1.0/src/ctx/ai/_httpcore_patch.py +267 -0
- ctx_sdk-0.1.0/src/ctx/ai/base.py +452 -0
- ctx_sdk-0.1.0/src/ctx/ai/context_length.py +504 -0
- ctx_sdk-0.1.0/src/ctx/ai/dynamic_models.py +1389 -0
- ctx_sdk-0.1.0/src/ctx/ai/model_fetcher.py +506 -0
- ctx_sdk-0.1.0/src/ctx/ai/models.py +122 -0
- ctx_sdk-0.1.0/src/ctx/ai/oauth/__init__.py +157 -0
- ctx_sdk-0.1.0/src/ctx/ai/oauth/cline.py +541 -0
- ctx_sdk-0.1.0/src/ctx/ai/oauth/codex.py +574 -0
- ctx_sdk-0.1.0/src/ctx/ai/oauth/copilot.py +380 -0
- ctx_sdk-0.1.0/src/ctx/ai/oauth/deepai.py +427 -0
- ctx_sdk-0.1.0/src/ctx/ai/oauth/devin/__init__.py +696 -0
- ctx_sdk-0.1.0/src/ctx/ai/oauth/devin/descriptors.pb +0 -0
- ctx_sdk-0.1.0/src/ctx/ai/oauth/devin/proto.py +161 -0
- ctx_sdk-0.1.0/src/ctx/ai/oauth/dynamic.py +407 -0
- ctx_sdk-0.1.0/src/ctx/ai/oauth/freepi.py +118 -0
- ctx_sdk-0.1.0/src/ctx/ai/oauth/junie.py +728 -0
- ctx_sdk-0.1.0/src/ctx/ai/phase_parser.py +270 -0
- ctx_sdk-0.1.0/src/ctx/ai/provider.yaml +783 -0
- ctx_sdk-0.1.0/src/ctx/ai/provider_catalog.py +813 -0
- ctx_sdk-0.1.0/src/ctx/ai/provider_hooks.py +139 -0
- ctx_sdk-0.1.0/src/ctx/ai/providers/__init__.py +57 -0
- ctx_sdk-0.1.0/src/ctx/ai/providers/anthropic_sdk.py +315 -0
- ctx_sdk-0.1.0/src/ctx/ai/providers/mock.py +279 -0
- ctx_sdk-0.1.0/src/ctx/ai/providers/openai_responses_sdk.py +36 -0
- ctx_sdk-0.1.0/src/ctx/ai/providers/openai_sdk.py +349 -0
- ctx_sdk-0.1.0/src/ctx/ai/providers/sanitize.py +14 -0
- ctx_sdk-0.1.0/src/ctx/ai/py.typed +0 -0
- ctx_sdk-0.1.0/src/ctx/ai/rate_limit.py +214 -0
- ctx_sdk-0.1.0/src/ctx/ai/sdk/__init__.py +13 -0
- ctx_sdk-0.1.0/src/ctx/ai/sdk/_sse.py +37 -0
- ctx_sdk-0.1.0/src/ctx/ai/sdk/anthropic.py +428 -0
- ctx_sdk-0.1.0/src/ctx/ai/sdk/base.py +90 -0
- ctx_sdk-0.1.0/src/ctx/ai/sdk/openai.py +545 -0
- ctx_sdk-0.1.0/src/ctx/ai/sdk/openai_responses.py +593 -0
- ctx_sdk-0.1.0/src/ctx/ai/streaming_json.py +588 -0
- ctx_sdk-0.1.0/src/ctx/ai/thinking.py +406 -0
- ctx_sdk-0.1.0/src/ctx/ai/tool_parser.py +161 -0
- ctx_sdk-0.1.0/src/ctx/core/README.md +173 -0
- ctx_sdk-0.1.0/src/ctx/core/__init__.py +135 -0
- ctx_sdk-0.1.0/src/ctx/core/bytes_util.py +32 -0
- ctx_sdk-0.1.0/src/ctx/core/compaction.py +262 -0
- ctx_sdk-0.1.0/src/ctx/core/config.py +1349 -0
- ctx_sdk-0.1.0/src/ctx/core/defaults/__init__.py +0 -0
- ctx_sdk-0.1.0/src/ctx/core/defaults/config.yml +122 -0
- ctx_sdk-0.1.0/src/ctx/core/events.py +335 -0
- ctx_sdk-0.1.0/src/ctx/core/handoff.py +51 -0
- ctx_sdk-0.1.0/src/ctx/core/harness_config.py +58 -0
- ctx_sdk-0.1.0/src/ctx/core/image.py +106 -0
- ctx_sdk-0.1.0/src/ctx/core/notify.py +113 -0
- ctx_sdk-0.1.0/src/ctx/core/paths.py +78 -0
- ctx_sdk-0.1.0/src/ctx/core/permissions.py +238 -0
- ctx_sdk-0.1.0/src/ctx/core/py.typed +0 -0
- ctx_sdk-0.1.0/src/ctx/core/recap.py +169 -0
- ctx_sdk-0.1.0/src/ctx/core/scratchpad.py +54 -0
- ctx_sdk-0.1.0/src/ctx/core/session_retention.py +171 -0
- ctx_sdk-0.1.0/src/ctx/core/sounds/completion.wav +0 -0
- ctx_sdk-0.1.0/src/ctx/core/sounds/error.wav +0 -0
- ctx_sdk-0.1.0/src/ctx/core/sounds/permission.wav +0 -0
- ctx_sdk-0.1.0/src/ctx/core/themes.py +1150 -0
- ctx_sdk-0.1.0/src/ctx/core/token_usage.py +245 -0
- ctx_sdk-0.1.0/src/ctx/core/version.py +47 -0
- ctx_sdk-0.1.0/src/ctx/git/README.md +91 -0
- ctx_sdk-0.1.0/src/ctx/git/__init__.py +1 -0
- ctx_sdk-0.1.0/src/ctx/git/gh_app.py +349 -0
- ctx_sdk-0.1.0/src/ctx/git/gh_cli.py +83 -0
- ctx_sdk-0.1.0/src/ctx/git/git_branch.py +91 -0
- ctx_sdk-0.1.0/src/ctx/plugins/README.md +54 -0
- ctx_sdk-0.1.0/src/ctx/plugins/__init__.py +98 -0
- ctx_sdk-0.1.0/src/ctx/plugins/api.py +697 -0
- ctx_sdk-0.1.0/src/ctx/plugins/bootstrap.py +87 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/__init__.py +102 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/commands.py +59 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/hooks.py +143 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/services/__init__.py +3 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/services/agent_loop.py +50 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/services/agents.py +14 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/services/compaction.py +14 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/services/extensions.py +25 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/services/llm.py +22 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/services/permissions.py +20 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/services/projections.py +46 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/services/prompts.py +28 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/services/sessions.py +14 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/services/skills.py +14 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/services/telemetry.py +28 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/services/tools.py +21 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/tools/__init__.py +10 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/tools/_register.py +18 -0
- ctx_sdk-0.1.0/src/ctx/plugins/builtins/ui.py +63 -0
- ctx_sdk-0.1.0/src/ctx/plugins/bus.py +281 -0
- ctx_sdk-0.1.0/src/ctx/plugins/context.py +264 -0
- ctx_sdk-0.1.0/src/ctx/plugins/errors.py +41 -0
- ctx_sdk-0.1.0/src/ctx/plugins/event_contract.py +499 -0
- ctx_sdk-0.1.0/src/ctx/plugins/events.py +232 -0
- ctx_sdk-0.1.0/src/ctx/plugins/extensions.py +503 -0
- ctx_sdk-0.1.0/src/ctx/plugins/host.py +157 -0
- ctx_sdk-0.1.0/src/ctx/plugins/install.py +307 -0
- ctx_sdk-0.1.0/src/ctx/plugins/loader.py +328 -0
- ctx_sdk-0.1.0/src/ctx/plugins/plugin.py +107 -0
- ctx_sdk-0.1.0/src/ctx/plugins/profile.py +170 -0
- ctx_sdk-0.1.0/src/ctx/plugins/services.py +587 -0
- ctx_sdk-0.1.0/src/ctx/protocol/README.md +170 -0
- ctx_sdk-0.1.0/src/ctx/protocol/__init__.py +63 -0
- ctx_sdk-0.1.0/src/ctx/protocol/abc.py +30 -0
- ctx_sdk-0.1.0/src/ctx/protocol/errors.py +17 -0
- ctx_sdk-0.1.0/src/ctx/protocol/types.py +290 -0
- ctx_sdk-0.1.0/src/ctx/sdk/__init__.py +524 -0
- ctx_sdk-0.1.0/src/ctx/telemetry/README.md +132 -0
- ctx_sdk-0.1.0/src/ctx/telemetry/__init__.py +41 -0
- ctx_sdk-0.1.0/src/ctx/telemetry/exporters/__init__.py +14 -0
- ctx_sdk-0.1.0/src/ctx/telemetry/exporters/console.py +45 -0
- ctx_sdk-0.1.0/src/ctx/telemetry/exporters/jsonl.py +84 -0
- ctx_sdk-0.1.0/src/ctx/telemetry/processor.py +35 -0
- ctx_sdk-0.1.0/src/ctx/telemetry/tracing_impl.py +249 -0
ctx_sdk-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Vendored reference repos
|
|
2
|
+
ref/
|
|
3
|
+
reff/
|
|
4
|
+
|
|
5
|
+
# Python-generated files
|
|
6
|
+
__pycache__/
|
|
7
|
+
*.py[oc]
|
|
8
|
+
build/
|
|
9
|
+
dist/
|
|
10
|
+
wheels/
|
|
11
|
+
*.egg-info
|
|
12
|
+
|
|
13
|
+
# Virtual environments
|
|
14
|
+
.venv
|
|
15
|
+
|
|
16
|
+
# macOS
|
|
17
|
+
.DS_Store
|
|
18
|
+
|
|
19
|
+
# Exported ctx sessions
|
|
20
|
+
ctx-session-*
|
|
21
|
+
|
|
22
|
+
# Local editor settings
|
|
23
|
+
.vscode/
|
|
24
|
+
.env
|
|
25
|
+
.pytest_cache/
|
|
26
|
+
.ruff_cache/
|
|
27
|
+
.coverage
|
|
28
|
+
.coverage.*
|
|
29
|
+
htmlcov/
|
|
30
|
+
Site/
|
|
31
|
+
# One-off screenshots
|
|
32
|
+
image.png
|
|
33
|
+
model-merge/
|
|
34
|
+
benchmark_results.json
|
|
35
|
+
|
|
36
|
+
# Local configuration files
|
|
37
|
+
.ctx/
|
|
38
|
+
.vortexa/
|
ctx_sdk-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: ctx-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Minimalist agent harness: agent loop, sessions, tools, plugin composition, MCP client, 65 LLM providers, and a Python SDK. The coding agent CLI/TUI is a separate package (ctx-code) built on top.
|
|
5
|
+
Author: OEvortex
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Keywords: agent,agent-loop,harness,llm,sdk
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Requires-Dist: aiofiles>=25.1.0
|
|
10
|
+
Requires-Dist: aiohttp>=3.13.3
|
|
11
|
+
Requires-Dist: chardet<6.0.0,>=3.0.2
|
|
12
|
+
Requires-Dist: curl-cffi>=0.15.0
|
|
13
|
+
Requires-Dist: filelock>=3.25.2
|
|
14
|
+
Requires-Dist: hammett>=0.10.0
|
|
15
|
+
Requires-Dist: html-to-markdown<3.4.0,>=3.3.0
|
|
16
|
+
Requires-Dist: json-repair<1.0.0,>=0.57.0
|
|
17
|
+
Requires-Dist: loguru<1.0.0,>=0.7.3
|
|
18
|
+
Requires-Dist: lxml-html-clean>=0.4.3
|
|
19
|
+
Requires-Dist: oauth-cli-kit<1.0.0,>=0.1.3
|
|
20
|
+
Requires-Dist: packaging>=24.0
|
|
21
|
+
Requires-Dist: pillow>=12.1.1
|
|
22
|
+
Requires-Dist: protobuf<7,>=5.29
|
|
23
|
+
Requires-Dist: pydantic-settings<3.0.0,>=2.12.0
|
|
24
|
+
Requires-Dist: pydantic>=2.12.5
|
|
25
|
+
Requires-Dist: pyyaml>=6.0.3
|
|
26
|
+
Requires-Dist: readability-lxml>=0.8.4
|
|
27
|
+
Requires-Dist: rich>=14.3.2
|
|
28
|
+
Requires-Dist: typer<1.0.0,>=0.20.0
|
|
29
|
+
Provides-Extra: gh-app
|
|
30
|
+
Requires-Dist: cryptography>=41.0; extra == 'gh-app'
|
|
31
|
+
Provides-Extra: term
|
|
32
|
+
Requires-Dist: ctx-term>=0.1.0; extra == 'term'
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
|
|
35
|
+
# ctx-sdk
|
|
36
|
+
|
|
37
|
+
**A minimalist agent harness for building LLM-powered agents.**
|
|
38
|
+
|
|
39
|
+
[](https://pypi.org/project/ctx-sdk/)
|
|
40
|
+
[](https://pypi.org/project/ctx-sdk/)
|
|
41
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
42
|
+
|
|
43
|
+
`ctx-sdk` is the core agent harness: agent loop, sessions, tools, plugin composition, MCP client, 65 LLM providers, and a Python SDK. The coding agent CLI/TUI is a separate package ([ctx-code](https://pypi.org/project/ctx-code/)) built on top.
|
|
44
|
+
|
|
45
|
+
## Quick start
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install ctx-sdk
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Python 3.12 or newer.
|
|
52
|
+
|
|
53
|
+
## Usage
|
|
54
|
+
|
|
55
|
+
The SDK is the shortest way in. Build an `Agent`, give it a tool, run it:
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
import asyncio
|
|
59
|
+
|
|
60
|
+
from ctx.agent.sdk import Agent, Runner, tool
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@tool
|
|
64
|
+
def get_weather(city: str) -> str:
|
|
65
|
+
"""Return the current weather for a city."""
|
|
66
|
+
return f"Sunny in {city}"
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
agent = Agent(
|
|
70
|
+
name="Weather bot",
|
|
71
|
+
instructions="Be concise.",
|
|
72
|
+
model="gpt-4o-mini",
|
|
73
|
+
tools=[get_weather],
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
result = asyncio.run(Runner.run(agent, "Weather in Tokyo?"))
|
|
77
|
+
print(result.final_output)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Packages
|
|
81
|
+
|
|
82
|
+
| Package | Description |
|
|
83
|
+
|---------|-------------|
|
|
84
|
+
| **[@ctx.plugins](src/ctx/plugins)** | The plugin kernel: `Context`, `EventHub`, plugin rows, patch layers, profiles, boot composition |
|
|
85
|
+
| **[@ctx.protocol](src/ctx/protocol)** | Leaf. Message, stream, and tool-contract types. Imports nothing from `ctx` |
|
|
86
|
+
| **[@ctx.telemetry](src/ctx/telemetry)** | Leaf. Spans, processors, console and JSONL exporters |
|
|
87
|
+
| **[@ctx.git](src/ctx/git)** | Git and GitHub integration: `gh` CLI wrapper, GitHub App auth |
|
|
88
|
+
| **[@ctx.core](src/ctx/core)** | Agent events, permission gate, compaction, config schema and paths, theme registry |
|
|
89
|
+
| **[@ctx.ai](src/ctx/ai)** | The LLM layer: provider catalog, OAuth, SDK adapters, model catalog, tool parsing |
|
|
90
|
+
| **[@ctx.agent](src/ctx/agent)** | Product-neutral harness: loop, turn engine, sessions, tool registry, prompts, hooks, goals, SDK |
|
|
91
|
+
|
|
92
|
+
## Optional dependencies
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
# Terminal UI building blocks
|
|
96
|
+
pip install ctx-sdk[term]
|
|
97
|
+
|
|
98
|
+
# GitHub App authentication
|
|
99
|
+
pip install ctx-sdk[gh-app]
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Documentation
|
|
103
|
+
|
|
104
|
+
Full reference at [github.com/OEvortex/ctx-coding-agent](https://github.com/OEvortex/ctx-coding-agent).
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
[Apache License 2.0](LICENSE)
|
ctx_sdk-0.1.0/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# ctx-sdk
|
|
2
|
+
|
|
3
|
+
**A minimalist agent harness for building LLM-powered agents.**
|
|
4
|
+
|
|
5
|
+
[](https://pypi.org/project/ctx-sdk/)
|
|
6
|
+
[](https://pypi.org/project/ctx-sdk/)
|
|
7
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
8
|
+
|
|
9
|
+
`ctx-sdk` is the core agent harness: agent loop, sessions, tools, plugin composition, MCP client, 65 LLM providers, and a Python SDK. The coding agent CLI/TUI is a separate package ([ctx-code](https://pypi.org/project/ctx-code/)) built on top.
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install ctx-sdk
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Python 3.12 or newer.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
The SDK is the shortest way in. Build an `Agent`, give it a tool, run it:
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
import asyncio
|
|
25
|
+
|
|
26
|
+
from ctx.agent.sdk import Agent, Runner, tool
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@tool
|
|
30
|
+
def get_weather(city: str) -> str:
|
|
31
|
+
"""Return the current weather for a city."""
|
|
32
|
+
return f"Sunny in {city}"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
agent = Agent(
|
|
36
|
+
name="Weather bot",
|
|
37
|
+
instructions="Be concise.",
|
|
38
|
+
model="gpt-4o-mini",
|
|
39
|
+
tools=[get_weather],
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
result = asyncio.run(Runner.run(agent, "Weather in Tokyo?"))
|
|
43
|
+
print(result.final_output)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Packages
|
|
47
|
+
|
|
48
|
+
| Package | Description |
|
|
49
|
+
|---------|-------------|
|
|
50
|
+
| **[@ctx.plugins](src/ctx/plugins)** | The plugin kernel: `Context`, `EventHub`, plugin rows, patch layers, profiles, boot composition |
|
|
51
|
+
| **[@ctx.protocol](src/ctx/protocol)** | Leaf. Message, stream, and tool-contract types. Imports nothing from `ctx` |
|
|
52
|
+
| **[@ctx.telemetry](src/ctx/telemetry)** | Leaf. Spans, processors, console and JSONL exporters |
|
|
53
|
+
| **[@ctx.git](src/ctx/git)** | Git and GitHub integration: `gh` CLI wrapper, GitHub App auth |
|
|
54
|
+
| **[@ctx.core](src/ctx/core)** | Agent events, permission gate, compaction, config schema and paths, theme registry |
|
|
55
|
+
| **[@ctx.ai](src/ctx/ai)** | The LLM layer: provider catalog, OAuth, SDK adapters, model catalog, tool parsing |
|
|
56
|
+
| **[@ctx.agent](src/ctx/agent)** | Product-neutral harness: loop, turn engine, sessions, tool registry, prompts, hooks, goals, SDK |
|
|
57
|
+
|
|
58
|
+
## Optional dependencies
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
# Terminal UI building blocks
|
|
62
|
+
pip install ctx-sdk[term]
|
|
63
|
+
|
|
64
|
+
# GitHub App authentication
|
|
65
|
+
pip install ctx-sdk[gh-app]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Documentation
|
|
69
|
+
|
|
70
|
+
Full reference at [github.com/OEvortex/ctx-coding-agent](https://github.com/OEvortex/ctx-coding-agent).
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
[Apache License 2.0](LICENSE)
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[tool.hatch.build.targets.wheel]
|
|
6
|
+
packages = ["src/ctx"]
|
|
7
|
+
exclude = [
|
|
8
|
+
"src/ctx/term",
|
|
9
|
+
"src/ctx/code",
|
|
10
|
+
"src/ctx/mcp",
|
|
11
|
+
"src/ctx/codemode",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
[tool.hatch.build.targets.sdist]
|
|
15
|
+
exclude = [
|
|
16
|
+
"src/ctx/term",
|
|
17
|
+
"src/ctx/code",
|
|
18
|
+
"src/ctx/mcp",
|
|
19
|
+
"src/ctx/codemode",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[tool.hatch.build]
|
|
23
|
+
artifacts = []
|
|
24
|
+
|
|
25
|
+
[project]
|
|
26
|
+
name = "ctx-sdk"
|
|
27
|
+
version = "0.1.0"
|
|
28
|
+
description = "Minimalist agent harness: agent loop, sessions, tools, plugin composition, MCP client, 65 LLM providers, and a Python SDK. The coding agent CLI/TUI is a separate package (ctx-code) built on top."
|
|
29
|
+
requires-python = ">=3.12"
|
|
30
|
+
keywords = ["agent", "harness", "llm", "agent-loop", "sdk"]
|
|
31
|
+
license = "Apache-2.0"
|
|
32
|
+
authors = [
|
|
33
|
+
{ name = "OEvortex" },
|
|
34
|
+
]
|
|
35
|
+
readme = "README.md"
|
|
36
|
+
homepage = "https://github.com/OEvortex/ctx-coding-agent"
|
|
37
|
+
repository = "https://github.com/OEvortex/ctx-coding-agent"
|
|
38
|
+
dependencies = [
|
|
39
|
+
"aiofiles>=25.1.0",
|
|
40
|
+
"aiohttp>=3.13.3",
|
|
41
|
+
"chardet>=3.0.2,<6.0.0",
|
|
42
|
+
"curl-cffi>=0.15.0",
|
|
43
|
+
"filelock>=3.25.2",
|
|
44
|
+
"hammett>=0.10.0",
|
|
45
|
+
"html-to-markdown>=3.3.0,<3.4.0",
|
|
46
|
+
"json-repair>=0.57.0,<1.0.0",
|
|
47
|
+
"loguru>=0.7.3,<1.0.0",
|
|
48
|
+
"lxml-html-clean>=0.4.3",
|
|
49
|
+
"oauth-cli-kit>=0.1.3,<1.0.0",
|
|
50
|
+
"packaging>=24.0",
|
|
51
|
+
"pillow>=12.1.1",
|
|
52
|
+
"pydantic-settings>=2.12.0,<3.0.0",
|
|
53
|
+
"pydantic>=2.12.5",
|
|
54
|
+
"pyyaml>=6.0.3",
|
|
55
|
+
"readability-lxml>=0.8.4",
|
|
56
|
+
"rich>=14.3.2",
|
|
57
|
+
"typer>=0.20.0,<1.0.0",
|
|
58
|
+
"protobuf>=5.29,<7",
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
[project.optional-dependencies]
|
|
62
|
+
term = [
|
|
63
|
+
"ctx-term>=0.1.0",
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
gh-app = [
|
|
67
|
+
"cryptography>=41.0",
|
|
68
|
+
]
|
|
69
|
+
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"""Ctx — the minimalist agent harness.
|
|
2
|
+
|
|
3
|
+
Re-exports the whole agent surface. Every name here is resolved lazily through
|
|
4
|
+
:func:`__getattr__`, because importing :mod:`ctx` used to import the entire SDK
|
|
5
|
+
at package-import time.
|
|
6
|
+
|
|
7
|
+
Keep ``_SOURCES`` in this order so a name exported by more than one of them
|
|
8
|
+
still resolves to the same object the eager re-exports produced.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import importlib
|
|
12
|
+
from pkgutil import extend_path
|
|
13
|
+
from typing import TYPE_CHECKING, Any
|
|
14
|
+
|
|
15
|
+
# ``ctx-code`` ships ``ctx.code`` / ``ctx.term`` from its own sources.
|
|
16
|
+
# Editable installs keep the two trees on separate ``sys.path`` entries, so the
|
|
17
|
+
# submodules only resolve if this package's path is extended to cover them.
|
|
18
|
+
__path__ = extend_path(__path__, __name__)
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
from ctx.agent.sdk import (
|
|
22
|
+
Agent,
|
|
23
|
+
AllowlistApprove,
|
|
24
|
+
AutoApprove,
|
|
25
|
+
ConsoleTraceProcessor,
|
|
26
|
+
FunctionTool,
|
|
27
|
+
GuardrailFunctionOutput,
|
|
28
|
+
Handoff,
|
|
29
|
+
HandoffCallItem,
|
|
30
|
+
HandoffInputData,
|
|
31
|
+
HandoffOutputItem,
|
|
32
|
+
InMemorySession,
|
|
33
|
+
InputGuardrail,
|
|
34
|
+
InputGuardrailTripwireTriggered,
|
|
35
|
+
JSONLSession,
|
|
36
|
+
JSONLTraceProcessor,
|
|
37
|
+
MessageOutputItem,
|
|
38
|
+
OutputGuardrail,
|
|
39
|
+
OutputGuardrailTripwireTriggered,
|
|
40
|
+
PermissionDecision,
|
|
41
|
+
PermissionPolicy,
|
|
42
|
+
PromptApprove,
|
|
43
|
+
ReasoningItem,
|
|
44
|
+
RunConfig,
|
|
45
|
+
RunItem,
|
|
46
|
+
Runner,
|
|
47
|
+
RunResult,
|
|
48
|
+
RunStreamed,
|
|
49
|
+
Session,
|
|
50
|
+
SessionSettings,
|
|
51
|
+
Span,
|
|
52
|
+
ToolApprovalItem,
|
|
53
|
+
ToolCallItem,
|
|
54
|
+
ToolCallOutputItem,
|
|
55
|
+
ToolGuardrailFunctionOutput,
|
|
56
|
+
ToolInputGuardrailTripwireTriggered,
|
|
57
|
+
ToolOutputGuardrailTripwireTriggered,
|
|
58
|
+
Trace,
|
|
59
|
+
TraceProcessor,
|
|
60
|
+
Usage,
|
|
61
|
+
_ToolApprovalItem,
|
|
62
|
+
add_trace_processor,
|
|
63
|
+
disable_tracing,
|
|
64
|
+
enable_tracing,
|
|
65
|
+
handoff,
|
|
66
|
+
input_guardrail,
|
|
67
|
+
output_guardrail,
|
|
68
|
+
span,
|
|
69
|
+
tool,
|
|
70
|
+
tool_input_guardrail,
|
|
71
|
+
tool_output_guardrail,
|
|
72
|
+
trace,
|
|
73
|
+
)
|
|
74
|
+
from ctx.protocol import Message
|
|
75
|
+
|
|
76
|
+
_SOURCES = ("ctx.agent.sdk", "ctx.protocol")
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def __getattr__(name: str) -> Any:
|
|
80
|
+
for module_name in _SOURCES:
|
|
81
|
+
module = importlib.import_module(module_name)
|
|
82
|
+
if (value := getattr(module, name, None)) is not None:
|
|
83
|
+
# Cache into ``__dict__``: PEP 562 only fires ``__getattr__`` while
|
|
84
|
+
# the name is absent, and the import system would otherwise rebind
|
|
85
|
+
# any submodule sharing an exported name onto this package.
|
|
86
|
+
globals()[name] = value
|
|
87
|
+
return value
|
|
88
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def __dir__() -> list[str]:
|
|
92
|
+
return sorted(__all__)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
__all__ = [
|
|
96
|
+
# SDK
|
|
97
|
+
"Agent",
|
|
98
|
+
"AllowlistApprove",
|
|
99
|
+
"AutoApprove",
|
|
100
|
+
"ConsoleTraceProcessor",
|
|
101
|
+
"FunctionTool",
|
|
102
|
+
"GuardrailFunctionOutput",
|
|
103
|
+
"Handoff",
|
|
104
|
+
"HandoffCallItem",
|
|
105
|
+
"HandoffInputData",
|
|
106
|
+
"HandoffOutputItem",
|
|
107
|
+
"InMemorySession",
|
|
108
|
+
"InputGuardrail",
|
|
109
|
+
"InputGuardrailTripwireTriggered",
|
|
110
|
+
"JSONLSession",
|
|
111
|
+
"JSONLTraceProcessor",
|
|
112
|
+
"Message",
|
|
113
|
+
"MessageOutputItem",
|
|
114
|
+
"OutputGuardrail",
|
|
115
|
+
"OutputGuardrailTripwireTriggered",
|
|
116
|
+
"PermissionDecision",
|
|
117
|
+
"PermissionPolicy",
|
|
118
|
+
"PromptApprove",
|
|
119
|
+
"ReasoningItem",
|
|
120
|
+
"RunConfig",
|
|
121
|
+
"RunItem",
|
|
122
|
+
"RunResult",
|
|
123
|
+
"RunStreamed",
|
|
124
|
+
"Runner",
|
|
125
|
+
"Session",
|
|
126
|
+
"SessionSettings",
|
|
127
|
+
"Span",
|
|
128
|
+
"ToolApprovalItem",
|
|
129
|
+
"ToolCallItem",
|
|
130
|
+
"ToolCallOutputItem",
|
|
131
|
+
"ToolGuardrailFunctionOutput",
|
|
132
|
+
"ToolInputGuardrailTripwireTriggered",
|
|
133
|
+
"ToolOutputGuardrailTripwireTriggered",
|
|
134
|
+
"Trace",
|
|
135
|
+
"TraceProcessor",
|
|
136
|
+
"Usage",
|
|
137
|
+
"_ToolApprovalItem",
|
|
138
|
+
"add_trace_processor",
|
|
139
|
+
"disable_tracing",
|
|
140
|
+
"enable_tracing",
|
|
141
|
+
"handoff",
|
|
142
|
+
"input_guardrail",
|
|
143
|
+
"output_guardrail",
|
|
144
|
+
"span",
|
|
145
|
+
"tool",
|
|
146
|
+
"tool_input_guardrail",
|
|
147
|
+
"tool_output_guardrail",
|
|
148
|
+
"trace",
|
|
149
|
+
]
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# `ctx.agent`
|
|
2
|
+
|
|
3
|
+
The product-neutral agent harness behind ctx. It owns the turn loop, the composition root, the `BaseTool` contract, prompt and context assembly, sessions, sub-agent dispatch, hooks, and a programmatic SDK. It never imports `ctx.code` or `ctx.term` at module level, so both the TUI and the headless runner drive the same `ConversationRuntime` without the harness depending on either. This package used to be nested at `ctx.ai.agent`, which made `ctx.ai` a 116-module grab bag holding the whole runtime; it is now a top-level sibling of `ctx.ai`, which now contains only provider code.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
The SDK is the shortest way in. Build an `Agent`, give it a tool, run it:
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
import asyncio
|
|
11
|
+
|
|
12
|
+
from ctx.agent.sdk import Agent, Runner, tool
|
|
13
|
+
from ctx.ai.providers.mock import MockProvider
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@tool
|
|
17
|
+
def get_weather(city: str) -> str:
|
|
18
|
+
"""Return the current weather for a city."""
|
|
19
|
+
return f"Sunny in {city}"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
agent = Agent(
|
|
23
|
+
name="Weather bot",
|
|
24
|
+
instructions="Be concise.",
|
|
25
|
+
model="gpt-4o-mini",
|
|
26
|
+
provider=MockProvider(scenario="simple_text"),
|
|
27
|
+
tools=[get_weather],
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
result = asyncio.run(Runner.run(agent, "Weather in Tokyo?"))
|
|
31
|
+
|
|
32
|
+
print(result.stop_reason) # stop
|
|
33
|
+
print(result.final_output) # the assistant's final text
|
|
34
|
+
print(result.usage) # aggregated tokens across every turn
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`MockProvider` (`ctx.ai.providers.mock`) replays scripted streams, so this runs offline. Drop it and set a provider dict for a real call - the recognised keys are `name`, `sdk`, `api_key`, `base_url`, `model`, `max_tokens`, `temperature`, `thinking_level`, `default_headers`:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
agent = Agent(name="Ops", model="gpt-4o", provider={"name": "openai"}, tools=[deploy])
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`Runner.run`, `Runner.run_sync` and `Runner.run_streamed` share one signature: `(starting_agent, input, *, session=None, run_config=None, max_turns=None, cancellation=None, context=None)`.
|
|
44
|
+
|
|
45
|
+
- **`Agent`** is a dataclass: `name`, `instructions` (a string or a callable), `model`, `provider` (a `BaseProvider`, a dict, or `None` for env/config fallback), `tools`, `handoffs`, `output_type`, `input_guardrails`, `output_guardrails`, `tool_use_behavior`, `metadata`, `needs_approval_tools`. `agent.clone(**overrides)` copies it with overrides.
|
|
46
|
+
- **`RunResult`** carries `final_output`, `new_items`, `interruptions`, `state`, `stop_reason`, `usage`, `agent_name`. `to_input_list()` turns the run back into input items you can feed to a later `Runner.run`.
|
|
47
|
+
- **`@tool`** turns a typed Python function into a `BaseTool`. The docstring becomes the model-facing description. Keyword arguments: `name`, `description`, `needs_approval` (default `False`; `True` is meant to pause for approval), `mutating` (default `True`; `False` marks the tool read-only), `input_guardrails`, `output_guardrails`, `tool_icon`. See "Approvals and permissions" below for which of these the runner actually honours today.
|
|
48
|
+
- **`Runner.run_streamed`** returns an async iterable of events; `.result` is only valid after the iterator is exhausted, and raises `RuntimeError` if read earlier.
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
stream = Runner.run_streamed(agent, "Weather in Tokyo?")
|
|
52
|
+
async for event in stream:
|
|
53
|
+
print(type(event).__name__)
|
|
54
|
+
print(stream.result.final_output)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
- **`handoff(agent, *, tool_name_override, tool_description_override, on_handoff, input_filter, input_type)`** turns another `Agent` into a transfer tool on the caller. Give the caller `handoffs=[handoff(triage, tool_name_override="escalate")]` and the model can hand the conversation over; `on_handoff` runs when the handoff tool executes. `input_type` becomes the handoff tool's parameter schema; `input_filter` is accepted but the runner does not invoke it yet.
|
|
58
|
+
- **`JSONLSession(path=None, *, session_id=None)`** persists a run to the same append-only format the TUI and headless runner use, so a session started from the SDK can be resumed in the TUI and the other way round. Without a `path` it is in-memory only.
|
|
59
|
+
- **`InMemorySession(session_id=None)`** keeps items for the life of the process.
|
|
60
|
+
- **`SessionSettings(limit=...)`** caps how much history a session replays into a run.
|
|
61
|
+
- **`RunConfig`** is the per-run knob bag: `max_turns` (default 50), `permission_policy`, `session_settings`, `tracing_disabled`, `trace_include_sensitive_data`, `nest_handoff_history`, `session_input_callback`, `custom`.
|
|
62
|
+
|
|
63
|
+
[docs/sdk.md](../../docs/sdk.md) covers the SDK in depth: the runner, agents and handoffs, tools, approvals, permissions, guardrails, sessions, skills, and tracing.
|
|
64
|
+
|
|
65
|
+
## Approvals and permissions
|
|
66
|
+
|
|
67
|
+
Read this section as the intended design, not the shipped behaviour. The types and the pause branch exist, but nothing feeds them yet:
|
|
68
|
+
|
|
69
|
+
- `PermissionPolicy` / `AutoApprove` / `AllowlistApprove` / `PromptApprove` / `PermissionCallback` are defined in `sdk/permissions.py`, and `RunConfig.permission_policy` accepts one per run -- but `Runner` never reads it.
|
|
70
|
+
- `needs_approval=True` on `@tool` and `Agent.needs_approval_tools` are stored but never consulted.
|
|
71
|
+
- `RunResult.interruptions` / `.state` and the `stop_reason="tool_use"` pause branch in `_run_impl` exist, but `pending_tool_calls` is never populated, so a run never actually pauses for approval.
|
|
72
|
+
- `RunState.approve` / `.reject` / `.decision_for` work as data structures; `decision_for` is never called from the runner, and there is no way to pass a `RunState` back into `Runner.run`.
|
|
73
|
+
|
|
74
|
+
What *does* gate tool calls is the turn engine's `ctx.core.permissions.check_permission` against `~/.ctx/config.yml` (`permissions.mode`, default `"prompt"`). Under `prompt`, a `mutating=True` tool makes the engine emit a `ToolApprovalEvent` whose future the SDK runner never resolves, so the run hangs until `cancellation` fires. In practice: mark SDK tools `mutating=False`, or run with `permissions.mode: auto`.
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from ctx.agent.sdk import Agent, Runner, RunConfig, AllowlistApprove
|
|
78
|
+
|
|
79
|
+
# permission_policy is accepted but currently ignored by Runner.
|
|
80
|
+
result = await Runner.run(
|
|
81
|
+
agent, "Ship v2", run_config=RunConfig(permission_policy=AllowlistApprove(["read", "grep"]))
|
|
82
|
+
)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`ToolApprovalItem.to_input_item()` raises by design - filter approvals out before sending an input list back to the model.
|
|
86
|
+
|
|
87
|
+
## Composition root
|
|
88
|
+
|
|
89
|
+
`ConversationRuntime` (`runtime.py`) is the object both run surfaces drive. It takes a cwd, a model selection, an explicit tool list and optional agent-registry and event-bus wiring, and it owns provider construction, tool activation, model and thinking-level switches, compaction, handoff, sessions, and the background-task manager.
|
|
90
|
+
|
|
91
|
+
It also reads two services off the plugin composition the process booted, and falls back to the built-in path when a run has none mounted: `ctx.prompts` for system-prompt assembly and `ctx.agentLoop` for which driver builds the engine. See `ctx.plugins` and `docs/plugins.md`.
|
|
92
|
+
|
|
93
|
+
`DispatcherContext` (`dispatcher.py`) is the single parent-state slot that dispatching tools read: provider, model, `thinking_level`, `agent_registry`, `cwd`, `system_prompt`, and the product-installed `progress_callback`. `ConversationRuntime` repopulates it on initialise, agent change, model change and thinking-level change. A process with no runtime has no dispatcher context, which is why background sub-agent dispatch is unavailable in a bare headless test harness.
|
|
94
|
+
|
|
95
|
+
## The turn loop
|
|
96
|
+
|
|
97
|
+
Three modules, in order:
|
|
98
|
+
|
|
99
|
+
- `turn.py` (`run_single_turn`) is one request/response cycle: streaming, tool calls, permission checks, retries, cancellation.
|
|
100
|
+
- `agent_runner.py` (`run_agent_turn`) is a provider-agnostic single-turn wrapper around it.
|
|
101
|
+
- `loop.py` (`Agent.run`) is the outer loop: compaction between turns, follow-up and steer queues, background-task draining.
|
|
102
|
+
|
|
103
|
+
`context_governance.py` (`prepare_for_model(messages)`) sits in front of every model call. It is a pure function with no I/O: it drops `ToolResultMessage` entries whose `tool_call_id` no preceding assistant tool call matches (orphans from cancelled turns) and truncates any tool result over the per-result budget, replacing the body with a summary so the model keeps a pointer without carrying all of it.
|
|
104
|
+
|
|
105
|
+
`prompts/builder.py` (`build_system_prompt(cwd, context, tools, *, base_content, include_git_context, include_ponytail, extra_instructions, extra_instructions_mode, skills)`) assembles the prompt from the section modules `identity`, `tooling`, `env` and `ponytail`. `context/` holds the pieces it reads: AGENTS.md discovery, the skills catalog, and the git snapshot.
|
|
106
|
+
|
|
107
|
+
## Tools
|
|
108
|
+
|
|
109
|
+
`tools/base.py` (`BaseTool`) is the contract; `tools/__init__.py` is the global registry (`register_tool`, `register_tools`, `unregister_tool`, `get_tool`, `get_all_tools`, `get_default_tools`, `get_parent_only_tools`, `get_tool_definitions`) plus the JSON-schema slimming that keeps LLM tool definitions small. `tools/schema.py` converts JSON schema to Pydantic for argument validation.
|
|
110
|
+
|
|
111
|
+
**The harness ships no tools.** Every concrete tool — `ask_user`, `delegate_subagent`, `web`, `bash`, `read`, `skill`, `codemode`, `tool_search`, and friends — is implemented by a product package (`ctx/code/tools/` for the coding agent) and mounted through plugin rows, which is why a bare `import ctx.agent.tools` sees an empty registry while a booted composition sees the product's full tool set.
|
|
112
|
+
|
|
113
|
+
Tools marked parent-only at mount time (`ask_user`, `delegate_subagent` in the coding product) are excluded from sub-agent tool surfaces, so a sub-agent cannot recursively spawn one or block on a user prompt.
|
|
114
|
+
|
|
115
|
+
## Sub-agents
|
|
116
|
+
|
|
117
|
+
`delegate_subagent` is the only dispatch path. The harness keeps the dispatch *builder* in `subagent.py` (spec resolution, tool-list composition, system prompt, session creation, the run loop, progress events, scheduler admission); the tool itself — `TaskTool`/`TaskParams` — is implemented by the product (`ctx/code/tools/subagent.py`) on top of that API. It was renamed from `task` to avoid ambiguity with the todo list - a model told to "check the task" could mean the todo list or the dispatcher.
|
|
118
|
+
|
|
119
|
+
There are **no built-in sub-agent presets**. `subagent_type` resolves against user-defined agents in `.ctx/agent/` and `~/.ctx/agent/`; an empty or unknown name runs a default sub-agent with the parent's tool surface minus the parent-only tools, capped at `DEFAULT_MAX_TURNS = 200`. Results are truncated to `MAX_RESULT_CHARS = 32_000`, and transcripts are shown up to `MAX_TRANSCRIPT_LINES = 200` with a "... (n more)" tail.
|
|
120
|
+
|
|
121
|
+
Every dispatch goes through `SubagentScheduler` (`subagents.py`), a FIFO admission queue:
|
|
122
|
+
|
|
123
|
+
- The limit comes from `task.max_concurrent` in `ctx.core.config`, default `4`; `0` means uncapped. `SubagentScheduler.DEFAULT_MAX_CONCURRENT` is the fallback when config cannot be read.
|
|
124
|
+
- **A sub-agent over the cap waits for its slot before it builds a session or a provider.** Waiting happens in `_run_subagent` before `_run_admitted_subagent`, so `running` and `queued` are real counts the UI can render, not estimates.
|
|
125
|
+
- `set_limit()` resizes after a config reload without pre-empting anything in flight; it holds new arrivals only until the running count drops below the new limit. `reset_scheduler()` drops all queue state.
|
|
126
|
+
- The scheduler is deliberately dumb: one asyncio loop, one queue, no priorities, no cross-process state.
|
|
127
|
+
|
|
128
|
+
`subagent_type` resolution, session creation, provider derivation, the run loop and progress events are all internal to `_run_admitted_subagent`. Products override the runner through an explicit hook:
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
from ctx.agent.subagent import set_subagent_runner, resolve_subagent_runner
|
|
132
|
+
|
|
133
|
+
set_subagent_runner(my_runner) # installs a replacement
|
|
134
|
+
resolve_subagent_runner() # returns it, or the built-in _run_subagent
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
That hook replaced three call sites that re-imported `ctx.agent.subagent` looking for a divergent `_run_subagent`. The module re-exports the very same function object, so the lookup could never have differed, and the back-edge to the product layer existed only to keep old monkeypatches alive.
|
|
138
|
+
|
|
139
|
+
`background: true` schedules through `BackgroundTaskManager` instead and returns a `task_id` immediately. Cancellation differs by design: a background task is not cancelled by the parent's `cancel_event` (its call already returned) and stops only explicitly or via `ConversationRuntime.close()`.
|
|
140
|
+
|
|
141
|
+
Sub-agents auto-approve their own approval prompts and answer `ask_user` with an empty response. They have no way to reach the user's terminal.
|
|
142
|
+
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""The product-neutral harness: loop, turns, sessions, tools, extensions, SDK.
|
|
2
|
+
|
|
3
|
+
``ctx.agent`` owns the engine. The product layer (``ctx.code``) injects
|
|
4
|
+
everything engine-side: the system-prompt builder, the context loader, the tool
|
|
5
|
+
registry, and the user's configuration.
|
|
6
|
+
|
|
7
|
+
The names re-exported here are the ones call sites have always imported from
|
|
8
|
+
``ctx.agent``. They now live in :mod:`ctx.plugins`, because the event contract,
|
|
9
|
+
the extension API, and extension discovery are composition concerns; this module
|
|
10
|
+
keeps the historical import path working rather than breaking every caller.
|
|
11
|
+
|
|
12
|
+
The re-exports are lazy: importing this package used to mount the whole plugin
|
|
13
|
+
kernel, so reaching any leaf like ``ctx.agent.context.skills`` through it cost
|
|
14
|
+
~550ms the leaf does not use. Submodule imports (``from ctx.agent import
|
|
15
|
+
revert``) are unaffected — they fall through to the real submodule.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import importlib
|
|
19
|
+
from typing import TYPE_CHECKING, Any
|
|
20
|
+
|
|
21
|
+
if TYPE_CHECKING:
|
|
22
|
+
from ctx.plugins.api import (
|
|
23
|
+
CommandOutcome,
|
|
24
|
+
ExtensionAPI,
|
|
25
|
+
ExtensionCommand,
|
|
26
|
+
ExtensionRecord,
|
|
27
|
+
ExtensionTool,
|
|
28
|
+
)
|
|
29
|
+
from ctx.plugins.event_contract import AGENT_CHANGED, AGENT_START
|
|
30
|
+
from ctx.plugins.extensions import LoadedExtensions, load_for_runtime
|
|
31
|
+
|
|
32
|
+
# Historical import order, so a name exported by more than one source still
|
|
33
|
+
# resolves to the object the eager re-exports produced.
|
|
34
|
+
_SOURCES = ("ctx.plugins.api", "ctx.plugins.event_contract", "ctx.plugins.extensions")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def __getattr__(name: str) -> Any:
|
|
38
|
+
for module_name in _SOURCES:
|
|
39
|
+
module = importlib.import_module(module_name)
|
|
40
|
+
if (value := getattr(module, name, None)) is not None:
|
|
41
|
+
globals()[name] = value
|
|
42
|
+
return value
|
|
43
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def __dir__() -> list[str]:
|
|
47
|
+
return sorted(__all__)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
__all__ = [
|
|
51
|
+
"AGENT_CHANGED",
|
|
52
|
+
"AGENT_START",
|
|
53
|
+
"CommandOutcome",
|
|
54
|
+
"ExtensionAPI",
|
|
55
|
+
"ExtensionCommand",
|
|
56
|
+
"ExtensionRecord",
|
|
57
|
+
"ExtensionTool",
|
|
58
|
+
"LoadedExtensions",
|
|
59
|
+
"load_for_runtime",
|
|
60
|
+
]
|