nextloop 0.4.0rc1__tar.gz → 0.5.0rc1__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.
- {nextloop-0.4.0rc1/src/nextloop.egg-info → nextloop-0.5.0rc1}/PKG-INFO +29 -8
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/README.md +28 -7
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/design/harness.md +41 -0
- nextloop-0.5.0rc1/docs/research/2026-11-10-nextloop-optimization-review.md +197 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/pyproject.toml +1 -1
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/__init__.py +14 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/adapters/anthropic.py +51 -7
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/adapters/memory.py +19 -14
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/adapters/observing.py +29 -2
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/adapters/openai.py +36 -4
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/adapters/sqlite.py +2 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/contracts.py +45 -1
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/driver.py +193 -10
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/events/outcome.py +97 -2
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/events/repair.py +9 -4
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/execute/batch.py +13 -1
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/execute/composite.py +9 -3
- nextloop-0.5.0rc1/src/nextloop/execute/mcp.py +196 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/execute/model.py +61 -10
- nextloop-0.5.0rc1/src/nextloop/execute/preview.py +207 -0
- nextloop-0.5.0rc1/src/nextloop/execute/result_read.py +95 -0
- nextloop-0.5.0rc1/src/nextloop/execute/stdio.py +240 -0
- nextloop-0.4.0rc1/src/nextloop/execute/stdio.py → nextloop-0.5.0rc1/src/nextloop/execute/stdio_transport.py +88 -217
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/execute/tools.py +4 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/harness.py +115 -51
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/loop.py +21 -1
- nextloop-0.5.0rc1/src/nextloop/testing.py +67 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1/src/nextloop.egg-info}/PKG-INFO +29 -8
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop.egg-info/SOURCES.txt +15 -0
- nextloop-0.5.0rc1/tests/test_budgets.py +228 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_cli.py +1 -1
- nextloop-0.5.0rc1/tests/test_correctness_regressions.py +174 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_harness.py +1 -1
- nextloop-0.5.0rc1/tests/test_harness_injection.py +87 -0
- nextloop-0.5.0rc1/tests/test_mcp.py +169 -0
- nextloop-0.5.0rc1/tests/test_preview.py +177 -0
- nextloop-0.5.0rc1/tests/test_result_policies.py +159 -0
- nextloop-0.5.0rc1/tests/test_review_regressions.py +205 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_skills.py +1 -1
- nextloop-0.5.0rc1/tests/test_store_contract.py +52 -0
- nextloop-0.5.0rc1/tests/test_surface_snapshot.py +70 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/LICENSE +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/MANIFEST.in +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/adr/0001-embeddable-agent-loop.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/adr/0002-dsh-aligned-agent-loop.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/adr/0003-python-and-centralized-contracts.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/architecture/agent-loop-capability-review.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/architecture/development-plan.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/architecture/discussion-roadmap.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/architecture/feature-worklist.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/architecture/requirements.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/design/agent-loop.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/design/context-builder.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/design/loop-interface.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/design/react-protocol.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/design/skills.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/design/verification-matrix.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/research/2026-11-02-dsh-primary-sources.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/research/2026-11-02-loop-evidence-notes.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/research/2026-11-03-hermes-turn-control.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/research/2026-11-03-tool-batch-failures.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/research/2026-11-03-tool-call-validation.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/research/2026-11-03-yomi-turn-control.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/research/2026-11-06-open-source-agent-loops.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/docs/research/2026-11-06-react-tool-event-recovery.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/examples/basic.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/examples/harness-skills/echo/SKILL.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/examples/harness.toml +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/examples/live_smoke.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/examples/python_tools.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/examples/skills/coding/SKILL.md +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/examples/skills_demo.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/examples/workers/cpp_worker.cpp +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/examples/workers/python_worker.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/examples/workers/rust_worker.rs +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/setup.cfg +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/adapters/__init__.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/adapters/_sse.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/cli.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/events/__init__.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/events/project.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/events/response.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/events/trace.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/execute/__init__.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/execute/cleanup.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/py.typed +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop/skills.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop.egg-info/dependency_links.txt +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop.egg-info/entry_points.txt +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop.egg-info/requires.txt +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/src/nextloop.egg-info/top_level.txt +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_aggregate_close.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_events.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_failures.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_loop.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_observability.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_providers.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_stdio.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_stdio_cleanup.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_stdio_frames.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_stdio_safety.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_stores.py +0 -0
- {nextloop-0.4.0rc1 → nextloop-0.5.0rc1}/tests/test_worker_conformance.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: nextloop
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0rc1
|
|
4
4
|
Summary: An embeddable AgentLoop with explicit event, tool, recovery, and delivery semantics
|
|
5
5
|
Author-email: 12bitsD <bis12@163.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -34,12 +34,12 @@ nextloop is an embeddable Python AgentLoop for developers who want to build thei
|
|
|
34
34
|
|
|
35
35
|
**Your agent, your rules. No magic, no black box.**
|
|
36
36
|
|
|
37
|
-
Current version: **0.
|
|
37
|
+
Current version: **0.5.0rc1**.
|
|
38
38
|
|
|
39
39
|
## Install
|
|
40
40
|
|
|
41
41
|
```bash
|
|
42
|
-
python -m pip install nextloop==0.
|
|
42
|
+
python -m pip install nextloop==0.5.0rc1
|
|
43
43
|
```
|
|
44
44
|
|
|
45
45
|
For local development:
|
|
@@ -53,9 +53,11 @@ uv pip install --python .venv/bin/python -e '.[dev]'
|
|
|
53
53
|
|
|
54
54
|
- **Execution you can reason about.** DSH-aligned Session/Turn/Step/attempt coordinates, writer fencing, and append-only Trajectory instead of implicit runner state.
|
|
55
55
|
- **Crash recovery without blind replay.** Resource locks, uncertain outcomes, crash closers, and delivery-only recovery tell you what is known, what is unknown, and what must not run twice.
|
|
56
|
-
- **Tools without framework lock-in.** Register Python functions directly, or run persistent Rust and C++ workers over stdio JSON-RPC. Schema validation, parallel calls, and exclusive barriers are built in.
|
|
56
|
+
- **Tools without framework lock-in.** Register Python functions directly, connect MCP stdio servers, or run persistent Rust and C++ workers over stdio JSON-RPC. Schema validation, parallel calls, and exclusive barriers are built in.
|
|
57
57
|
- **Observability from committed facts.** Structured Model/Tool/Turn traces, asynchronous Exporters, and Session history. No second execution path and no telemetry-driven control flow.
|
|
58
|
-
- **Small, explicit integration.** Assemble Providers, Stores, Tools, and Skills with `harness.toml`, or
|
|
58
|
+
- **Small, explicit integration.** Assemble Providers, Stores, Tools, and Skills with `harness.toml`, or inject your own adapters in Python. OpenAI-compatible and Anthropic Providers are included.
|
|
59
|
+
- **Bounded execution and context.** Optional Step, Provider-attempt, Tool, token, and deadline budgets. Large Tool Results use explicit preview/page/full policies with durable read-back.
|
|
60
|
+
- **Transient previews, authoritative results.** Optional OpenAI/Anthropic deltas with bounded queues and snapshot recovery. Previews never enter the EventStore, execute partial Tool Calls, or replace committed Delivery.
|
|
59
61
|
|
|
60
62
|
It is a library, not an HTTP service, product bot, Router, EventBus, or multi-agent framework. **The Loop stays in your application; the decisions stay with you.**
|
|
61
63
|
|
|
@@ -70,9 +72,13 @@ It is a library, not an HTTP service, product bot, Router, EventBus, or multi-ag
|
|
|
70
72
|
- SQLite and in-memory EventStores, crash closers, and delivery-only recovery.
|
|
71
73
|
- OpenAI-compatible and Anthropic streaming Provider adapters.
|
|
72
74
|
- Directory-based `SkillRegistry` for progressive, on-demand `SKILL.md` loading with dependency and version validation.
|
|
73
|
-
- Strict `harness.toml` assembly, Python `module:register`, and persistent Python/Rust/C++ stdio workers.
|
|
75
|
+
- Strict `harness.toml` assembly, Python adapter injection, `module:register`, and persistent Python/Rust/C++ stdio workers.
|
|
76
|
+
- MCP stdio lifecycle, paginated catalogs, request-scoped cancellation, and no replay after crashes.
|
|
77
|
+
- Optional Step, model-call, Tool-call, token, and Turn-deadline budgets with structured terminal reasons.
|
|
78
|
+
- Turn-private surface snapshots and `PREVIEW` / `PAGED` / `FULL` Tool Result policies with `read_tool_result`.
|
|
79
|
+
- Optional transient `PreviewSink`, legacy-Provider snapshots, bounded queues, and RESET recovery.
|
|
74
80
|
- Offline `nextloop` CLI for configuration, worker, Tool, and Skill diagnostics.
|
|
75
|
-
- Committed-event observability with Model/Tool/Turn DTOs, asynchronous Exporters, and Session history.
|
|
81
|
+
- Committed-event observability with Model/Tool/Turn DTOs, asynchronous Exporters, optional Exporter lifecycle, and Session history.
|
|
76
82
|
|
|
77
83
|
## Public API
|
|
78
84
|
|
|
@@ -87,6 +93,20 @@ The only four Adapter protocols are `EventStore`, `ModelProvider`, `ToolRuntime`
|
|
|
87
93
|
|
|
88
94
|
`Harness.load("harness.toml")` assembles those adapters without changing the Loop. Use `await harness.check()` for offline validation and `await harness.run("input")` for a model-backed Turn. See [Harness and extension interface](docs/design/harness.md) for the strict TOML schema, stdio protocol, lifecycle rules, and CLI.
|
|
89
95
|
|
|
96
|
+
Adapters can also be injected directly; caller-owned dependencies are not closed by Harness:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
harness = Harness.load(
|
|
100
|
+
"harness.toml",
|
|
101
|
+
provider=my_provider,
|
|
102
|
+
store=my_store,
|
|
103
|
+
delivery=my_delivery,
|
|
104
|
+
tools=my_runtime,
|
|
105
|
+
)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Use `ToolSpec(result_mode=ToolResultMode.PREVIEW | PAGED | FULL)` to control model-visible Tool output. `read_tool_result` retrieves committed full results by `event_id` and UTF-8 byte range. Use `TurnContext(preview=my_sink)` for transient Provider previews; committed Turn results still come only from `AgentLoop.run()` and the EventStore.
|
|
109
|
+
|
|
90
110
|
```bash
|
|
91
111
|
nextloop check examples/harness.toml
|
|
92
112
|
nextloop tools call python_worker__echo '{"text":"hello"}' examples/harness.toml
|
|
@@ -184,7 +204,8 @@ The release-candidate checks cover:
|
|
|
184
204
|
- real subprocess exits during Tool execution and final delivery, followed by SQLite recovery;
|
|
185
205
|
- recorded OpenAI and Anthropic SSE streams;
|
|
186
206
|
- 12 live Kimi scenarios across both protocols, real Tool usage, parallel execution, recovery, steer, cancellation, Compaction, and WorkingContext;
|
|
187
|
-
-
|
|
207
|
+
- live DeepSeek text + Preview + usage and real Tool Call + Delivery smoke;
|
|
208
|
+
- 185 offline tests, `ruff`, `mypy`, wheel/sdist builds, `twine check`, and clean wheel installation.
|
|
188
209
|
|
|
189
210
|
Current limits:
|
|
190
211
|
|
|
@@ -6,12 +6,12 @@ nextloop is an embeddable Python AgentLoop for developers who want to build thei
|
|
|
6
6
|
|
|
7
7
|
**Your agent, your rules. No magic, no black box.**
|
|
8
8
|
|
|
9
|
-
Current version: **0.
|
|
9
|
+
Current version: **0.5.0rc1**.
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
13
13
|
```bash
|
|
14
|
-
python -m pip install nextloop==0.
|
|
14
|
+
python -m pip install nextloop==0.5.0rc1
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
For local development:
|
|
@@ -25,9 +25,11 @@ uv pip install --python .venv/bin/python -e '.[dev]'
|
|
|
25
25
|
|
|
26
26
|
- **Execution you can reason about.** DSH-aligned Session/Turn/Step/attempt coordinates, writer fencing, and append-only Trajectory instead of implicit runner state.
|
|
27
27
|
- **Crash recovery without blind replay.** Resource locks, uncertain outcomes, crash closers, and delivery-only recovery tell you what is known, what is unknown, and what must not run twice.
|
|
28
|
-
- **Tools without framework lock-in.** Register Python functions directly, or run persistent Rust and C++ workers over stdio JSON-RPC. Schema validation, parallel calls, and exclusive barriers are built in.
|
|
28
|
+
- **Tools without framework lock-in.** Register Python functions directly, connect MCP stdio servers, or run persistent Rust and C++ workers over stdio JSON-RPC. Schema validation, parallel calls, and exclusive barriers are built in.
|
|
29
29
|
- **Observability from committed facts.** Structured Model/Tool/Turn traces, asynchronous Exporters, and Session history. No second execution path and no telemetry-driven control flow.
|
|
30
|
-
- **Small, explicit integration.** Assemble Providers, Stores, Tools, and Skills with `harness.toml`, or
|
|
30
|
+
- **Small, explicit integration.** Assemble Providers, Stores, Tools, and Skills with `harness.toml`, or inject your own adapters in Python. OpenAI-compatible and Anthropic Providers are included.
|
|
31
|
+
- **Bounded execution and context.** Optional Step, Provider-attempt, Tool, token, and deadline budgets. Large Tool Results use explicit preview/page/full policies with durable read-back.
|
|
32
|
+
- **Transient previews, authoritative results.** Optional OpenAI/Anthropic deltas with bounded queues and snapshot recovery. Previews never enter the EventStore, execute partial Tool Calls, or replace committed Delivery.
|
|
31
33
|
|
|
32
34
|
It is a library, not an HTTP service, product bot, Router, EventBus, or multi-agent framework. **The Loop stays in your application; the decisions stay with you.**
|
|
33
35
|
|
|
@@ -42,9 +44,13 @@ It is a library, not an HTTP service, product bot, Router, EventBus, or multi-ag
|
|
|
42
44
|
- SQLite and in-memory EventStores, crash closers, and delivery-only recovery.
|
|
43
45
|
- OpenAI-compatible and Anthropic streaming Provider adapters.
|
|
44
46
|
- Directory-based `SkillRegistry` for progressive, on-demand `SKILL.md` loading with dependency and version validation.
|
|
45
|
-
- Strict `harness.toml` assembly, Python `module:register`, and persistent Python/Rust/C++ stdio workers.
|
|
47
|
+
- Strict `harness.toml` assembly, Python adapter injection, `module:register`, and persistent Python/Rust/C++ stdio workers.
|
|
48
|
+
- MCP stdio lifecycle, paginated catalogs, request-scoped cancellation, and no replay after crashes.
|
|
49
|
+
- Optional Step, model-call, Tool-call, token, and Turn-deadline budgets with structured terminal reasons.
|
|
50
|
+
- Turn-private surface snapshots and `PREVIEW` / `PAGED` / `FULL` Tool Result policies with `read_tool_result`.
|
|
51
|
+
- Optional transient `PreviewSink`, legacy-Provider snapshots, bounded queues, and RESET recovery.
|
|
46
52
|
- Offline `nextloop` CLI for configuration, worker, Tool, and Skill diagnostics.
|
|
47
|
-
- Committed-event observability with Model/Tool/Turn DTOs, asynchronous Exporters, and Session history.
|
|
53
|
+
- Committed-event observability with Model/Tool/Turn DTOs, asynchronous Exporters, optional Exporter lifecycle, and Session history.
|
|
48
54
|
|
|
49
55
|
## Public API
|
|
50
56
|
|
|
@@ -59,6 +65,20 @@ The only four Adapter protocols are `EventStore`, `ModelProvider`, `ToolRuntime`
|
|
|
59
65
|
|
|
60
66
|
`Harness.load("harness.toml")` assembles those adapters without changing the Loop. Use `await harness.check()` for offline validation and `await harness.run("input")` for a model-backed Turn. See [Harness and extension interface](docs/design/harness.md) for the strict TOML schema, stdio protocol, lifecycle rules, and CLI.
|
|
61
67
|
|
|
68
|
+
Adapters can also be injected directly; caller-owned dependencies are not closed by Harness:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
harness = Harness.load(
|
|
72
|
+
"harness.toml",
|
|
73
|
+
provider=my_provider,
|
|
74
|
+
store=my_store,
|
|
75
|
+
delivery=my_delivery,
|
|
76
|
+
tools=my_runtime,
|
|
77
|
+
)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Use `ToolSpec(result_mode=ToolResultMode.PREVIEW | PAGED | FULL)` to control model-visible Tool output. `read_tool_result` retrieves committed full results by `event_id` and UTF-8 byte range. Use `TurnContext(preview=my_sink)` for transient Provider previews; committed Turn results still come only from `AgentLoop.run()` and the EventStore.
|
|
81
|
+
|
|
62
82
|
```bash
|
|
63
83
|
nextloop check examples/harness.toml
|
|
64
84
|
nextloop tools call python_worker__echo '{"text":"hello"}' examples/harness.toml
|
|
@@ -156,7 +176,8 @@ The release-candidate checks cover:
|
|
|
156
176
|
- real subprocess exits during Tool execution and final delivery, followed by SQLite recovery;
|
|
157
177
|
- recorded OpenAI and Anthropic SSE streams;
|
|
158
178
|
- 12 live Kimi scenarios across both protocols, real Tool usage, parallel execution, recovery, steer, cancellation, Compaction, and WorkingContext;
|
|
159
|
-
-
|
|
179
|
+
- live DeepSeek text + Preview + usage and real Tool Call + Delivery smoke;
|
|
180
|
+
- 185 offline tests, `ruff`, `mypy`, wheel/sdist builds, `twine check`, and clean wheel installation.
|
|
160
181
|
|
|
161
182
|
Current limits:
|
|
162
183
|
|
|
@@ -27,6 +27,15 @@ max_protocol_corrections = 2
|
|
|
27
27
|
max_provider_attempts = 3
|
|
28
28
|
retry_backoff_seconds = 0.1
|
|
29
29
|
pacing_step_interval = 15
|
|
30
|
+
max_steps = 50
|
|
31
|
+
max_model_calls = 20
|
|
32
|
+
max_tool_calls = 100
|
|
33
|
+
max_input_tokens = 100000
|
|
34
|
+
max_output_tokens = 10000
|
|
35
|
+
max_total_tokens = 110000
|
|
36
|
+
turn_deadline_seconds = 300
|
|
37
|
+
# tool_result_bytes = 50000
|
|
38
|
+
# tool_result_full_max_bytes = 1048576
|
|
30
39
|
|
|
31
40
|
[[tools]]
|
|
32
41
|
driver = "python"
|
|
@@ -41,6 +50,14 @@ resources = ["filesystem"]
|
|
|
41
50
|
timeout_seconds = 30
|
|
42
51
|
cancel_grace_seconds = 0.25
|
|
43
52
|
|
|
53
|
+
[[tools]]
|
|
54
|
+
driver = "mcp"
|
|
55
|
+
command = ["python3", "workers/mcp_server.py"]
|
|
56
|
+
namespace = "public"
|
|
57
|
+
start = "lazy"
|
|
58
|
+
resources = ["remote_read"]
|
|
59
|
+
timeout_seconds = 30
|
|
60
|
+
|
|
44
61
|
[skills]
|
|
45
62
|
paths = ["skills"]
|
|
46
63
|
```
|
|
@@ -59,6 +76,22 @@ await harness.close()
|
|
|
59
76
|
|
|
60
77
|
Skills remain instructions only. Their metadata is added to the system prompt, and the read-only `skill_view` Tool loads a body on demand. Loading a Skill never runs adjacent scripts or expands Tool permissions.
|
|
61
78
|
|
|
79
|
+
## Adapter injection and ownership
|
|
80
|
+
|
|
81
|
+
`Harness.load()` and the `Harness` constructor accept optional `provider`, `store`, `delivery`, and `tools` objects. These are the existing four Adapter protocols; no plugin registry is involved. Harness-created dependencies are closed by Harness. Caller-injected dependencies remain caller-owned and are not closed. An ObservingEventStore created around an external Store flushes and closes its own worker without closing that Store.
|
|
82
|
+
|
|
83
|
+
Exporters may optionally implement `flush()` and `close()`, synchronously or asynchronously. The queue is flushed before Exporter lifecycle calls. Harness closes Exporters created from `[trace]`; runtime-injected Exporters are flushed but not closed. `nextloop.testing.check_event_store(store)` exposes the same basic atomicity, idempotency, fencing, surface, and recovery-read contract used by the built-in Stores.
|
|
84
|
+
|
|
85
|
+
## Budgets and Tool Results
|
|
86
|
+
|
|
87
|
+
All budget fields default to `None`. Step, model-attempt, and validated Tool-batch limits stop the next side effect. Model attempts include retries and compaction. Token limits use Provider-reported usage and fail closed when required usage is missing or invalid. `turn_deadline_seconds` reuses cancellation and uncertain Tool semantics. Budgets are Turn-local; recovery closes an interrupted Turn and a later Turn gets a new budget.
|
|
88
|
+
|
|
89
|
+
`ToolSpec.result_mode` supports `PREVIEW`, `PAGED`, and `FULL`, with `PREVIEW` as the default. Small results are unchanged. Large previews retain UTF-8-safe head/tail content and an `event_id`; pages are continuous. The durable outcome retains full content while the model Message contains the projection. `read_tool_result` reads committed content from the recovery log by `event_id`, UTF-8 byte offset, and bounded limit. FULL and terminal results are not generically truncated; exceeding the hard limit commits the full result and ends the Turn as failed without final delivery.
|
|
90
|
+
|
|
91
|
+
## Transient Preview
|
|
92
|
+
|
|
93
|
+
`TurnContext.preview` accepts one optional synchronous `PreviewSink`. Providers may implement `complete_with_preview`; Providers that only implement `complete` remain compatible and produce a response snapshot rather than synthetic token deltas. Preview events are transient, never enter the EventStore, never execute partial Tool Calls, and never trigger Delivery. The bounded worker queue replaces overflow with a RESET containing the latest transient snapshot. Slow or failing sinks do not change the Turn result, and closing a subscription does not cancel the Turn.
|
|
94
|
+
|
|
62
95
|
## Observability
|
|
63
96
|
|
|
64
97
|
Observability is disabled by default. Enable the built-in JSON Lines Exporter with:
|
|
@@ -88,6 +121,14 @@ history = await harness.history(from_turn=1, to_turn=10)
|
|
|
88
121
|
|
|
89
122
|
History reads the complete EventStore log and uses the same TraceProjector as live export. Only new-format Events are supported; missing measured Model/Tool fields produce `timing_source="unknown"`, not estimated durations.
|
|
90
123
|
|
|
124
|
+
## MCP stdio
|
|
125
|
+
|
|
126
|
+
`driver = "mcp"` starts a standard MCP `2025-06-18` stdio server. The adapter performs initialize, the initialized notification, and paginated `tools/list` until the cursor is exhausted, then keeps a static catalog for the process lifetime. Remote annotations do not grant parallel execution, resources, or approval privileges.
|
|
127
|
+
|
|
128
|
+
MCP calls use ordinary ToolRuntime routing and the existing JSON Schema validation. Text and `structuredContent` are supported; `isError` maps to a normal Tool error. Cancellation uses the original JSON-RPC requestId. Timeout, transport failure, and process crash become uncertain outcomes with local resource locks, never automatic restart or replay. Closing uses stdin EOF followed by bounded process termination, not the private stdio `shutdown` method.
|
|
129
|
+
|
|
130
|
+
HTTP transports, OAuth, `listChanged`, dynamic catalog revisions, prompts/resources/sampling, and non-text content mapping are outside this version.
|
|
131
|
+
|
|
91
132
|
## Stdio protocol
|
|
92
133
|
|
|
93
134
|
The protocol version is `nextloop.stdio/v1`. Messages are one JSON-RPC 2.0 object per line on stdout; logs belong on stderr. Supported methods are `initialize`, `tools/list`, `tools/call`, `notifications/cancelled`, `ping`, and `shutdown`.
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# nextloop 优化研究:Feature、扩展性、代码质量与性能
|
|
2
|
+
|
|
3
|
+
日期:2026-11-10
|
|
4
|
+
基线:`e298afeeae782eb06bdad94bfe4afabb5be97c07`(`0.4.0rc1`)
|
|
5
|
+
范围:只读源码审查、现有测试与静态检查、临时微基准、官方文档与第一手资料。本文不包含产品代码改动。
|
|
6
|
+
|
|
7
|
+
## 结论
|
|
8
|
+
|
|
9
|
+
nextloop 不需要更换核心架构。Session 串行、EventStore fencing、append-only facts、ToolOutcome 分类、先提交 final 再 Delivery 的边界应保留。
|
|
10
|
+
|
|
11
|
+
后续顺序应是:
|
|
12
|
+
|
|
13
|
+
1. 修复已复现的正确性缺陷,补齐失败路径测试。
|
|
14
|
+
2. 用构造注入打开 Harness 扩展,并增加硬执行预算。
|
|
15
|
+
3. 在 parity 测试约束下实现增量 Context Projection,消除逐 Step 全量重建。
|
|
16
|
+
4. 按真实使用门槛选择 Tool Result spill、流式 preview、MCP、Provider fallback 等能力。
|
|
17
|
+
|
|
18
|
+
不建议引入通用插件管理器、第二套 EventBus、Router/多 Agent 框架、分布式任务队列或自动副作用重放。
|
|
19
|
+
|
|
20
|
+
## 1. P0:先修正确性,不做新 Feature
|
|
21
|
+
|
|
22
|
+
现有基线验证:`140 passed`,ruff 通过,strict mypy 对 27 个源码文件无问题。以下缺陷均由独立最小脚本复现,说明当前测试主要遗漏失败与竞态路径。
|
|
23
|
+
|
|
24
|
+
| 缺陷 | 证据与影响 | 最小修复与验收 |
|
|
25
|
+
|---|---|---|
|
|
26
|
+
| 并行 Tool 跨 chunk 截断重复结算 | `src/nextloop/execute/batch.py:102-124`。首块出现 Cancelled/Uncertain 后,剩余调用被追加 Skipped,但循环未退出,后续 chunk 再追加一次。6 个调用、`max_parallel=2` 可产生 10 个结果。 | 统一只结算一次剩余调用;覆盖多 chunk 的 Cancelled、UncertainSettled、UncertainHanging,断言 outcome 与持久化结果的数量、ID、顺序均与输入一一对应。 |
|
|
27
|
+
| Recovery 的 call_id 配对作用域错误 | `src/nextloop/events/repair.py:57-92` 按整个 Turn 的 `call_id` 配对,而 `src/nextloop/execute/tools.py:40-47` 只保证单批唯一。跨 Step 重用 ID 时,旧结果会覆盖新调用的未决状态。 | 以 `(step, call_id)` 配对 proposal、TOOL_CALL、TOOL_RESULT;覆盖第二 Step 未 dispatch 与已 dispatch 无结果两种恢复。 |
|
|
28
|
+
| final 前取消被记录为内部 FAILED | `src/nextloop/driver.py:329-336,393-394`。`_CancelFinalization` 在内层 try 之前抛出,专门捕获不可达,最终进入通用 Exception。 | 在正确的状态机边界处理取消;用 gated Store 精确安排 assistant append 后、final commit 前取消,断言 CANCELLED、无 FINAL_MESSAGE、无内部异常文本。 |
|
|
29
|
+
| 已拒绝批次在恢复时生成虚假 ToolResult | `src/nextloop/events/repair.py:43-92` 未识别 `TOOL_BATCH_REJECTED(executed=False)`,会把协议拒绝且未执行的 proposal 当成待恢复调用。 | 先收集 rejected step,recovery 只关闭 Step/Turn,不制造 ToolResult;补 crash recovery 回归。 |
|
|
30
|
+
| 同步 Python Tool 的 timeout 不生效 | `src/nextloop/execute/tools.py:107-129` 先同步调用 handler,仅对 awaitable 使用 timeout/cancel 包装。同步阻塞还会卡住事件循环。 | 首版建议明确“同步 handler 不支持 timeout”,注册时 fail-fast;保留无 timeout 的简单同步 Tool。不要静默忽略配置,也不要为首版引入线程副作用语义。若未来支持线程执行,需另行定义 timeout 后外部效果未知、取消与资源锁规则。 |
|
|
31
|
+
|
|
32
|
+
同阶段可做的低价值噪音清理仅限:`Harness` 对未知 protocol 显式报错,`_loop_config` 用 `dataclasses.replace` 避免新增字段漏拷。不要借修 bug 重写 Driver。
|
|
33
|
+
|
|
34
|
+
## 2. Feature:按内核价值排序
|
|
35
|
+
|
|
36
|
+
### P0:硬执行预算
|
|
37
|
+
|
|
38
|
+
`TurnDriver.run` 当前没有硬 Step 上限(`src/nextloop/driver.py:94-100`);`pacing_step_interval` 只是提示模型收敛,不阻止下一次调用(`src/nextloop/contracts.py:408-419`)。
|
|
39
|
+
|
|
40
|
+
建议为 LoopConfig 增加可选限制:
|
|
41
|
+
|
|
42
|
+
- `max_steps`、`max_model_calls`、`max_tool_calls`。
|
|
43
|
+
- input/output/total token 限额,usage 以 Provider 实报为准。
|
|
44
|
+
- `turn_deadline_seconds`,使用 monotonic clock。
|
|
45
|
+
|
|
46
|
+
语义要求:Step/Tool 在下一次副作用前检查;token 只能在响应返回后阻止下一次调用,不能承诺本次绝不超支;deadline 复用取消与 uncertain Tool 语义;恢复不能重置预算。超限必须有明确事件与 Turn 终态。
|
|
47
|
+
|
|
48
|
+
参考:[OpenAI Agents SDK max_turns](https://openai.github.io/openai-agents-python/running_agents/)、[PydanticAI UsageLimits](https://ai.pydantic.dev/api/usage/)。
|
|
49
|
+
|
|
50
|
+
### P1:文本 Tool Result spill
|
|
51
|
+
|
|
52
|
+
当前完整 Tool 结果同时进入 durable outcome 与模型 Message(`src/nextloop/events/outcome.py:91-128`),之后每个 Step 都可能重复进入上下文。大日志会同时放大 token、投影与 compaction 成本。
|
|
53
|
+
|
|
54
|
+
首版建议复用 append-only log 保存全文,模型只接收有界 head/tail preview、总字节数和 `(session_id, tool_result_seq)`;通过只读 Tool 按 offset/limit 分段读取。只有 durable result 已提交,preview 才能对模型可见;preview 不得冒充完整结果。
|
|
55
|
+
|
|
56
|
+
参考固定源码版本:[DSH Spill Storage](https://github.com/deepseek-ai/deepseek-harness/blob/ce7073d51254c26cd63b22a60835b8f948deb447/docs/subsystems/spill.md)。
|
|
57
|
+
|
|
58
|
+
### P1(有交互需求时):非权威流式 preview
|
|
59
|
+
|
|
60
|
+
Provider 已消费 SSE delta,但公共模型接口只返回完整响应(`src/nextloop/contracts.py:511-515`)。可增加 provider-neutral preview delta 与 committed semantic events,而不是把权威 Event 改成 token 流。
|
|
61
|
+
|
|
62
|
+
Preview 不写入权威 Trajectory、不触发 Tool、不作为 final delivery;slow consumer 不得阻塞执行;最后一个 delta 不等于 committed final。断线后仍从 EventStore seq 重同步。
|
|
63
|
+
|
|
64
|
+
参考:[OpenAI Agents SDK Streaming](https://openai.github.io/openai-agents-python/streaming/)。
|
|
65
|
+
|
|
66
|
+
### P1(有生态接入需求时):MCP ToolRuntime adapter
|
|
67
|
+
|
|
68
|
+
MCP 应只实现为 Composite ToolRuntime 的 adapter,不进入 Loop 控制流。首版可只做 stdio,覆盖分页 catalog、命名空间、schema/content 映射、取消、超时和传输中断分类。`isError` 是普通 Tool execution error;传输中断、超时和崩溃必须映射 uncertain,禁止自动重放。
|
|
69
|
+
|
|
70
|
+
参考:[MCP Tools specification](https://modelcontextprotocol.io/specification/2026-07-28/server/tools)。
|
|
71
|
+
|
|
72
|
+
### P2:达到明确门槛后再做
|
|
73
|
+
|
|
74
|
+
- **Provider fallback**:静态有序 chain,只在无 partial output 且错误类别允许时切换;不做 Router。参考 [PydanticAI FallbackModel](https://ai.pydantic.dev/models/overview/)。
|
|
75
|
+
- **渐进工具发现**:工具 schema 已显著消耗上下文或影响选择质量时,再做 catalog/search/inspect;需同时测 token、任务成功率和 cache 命中。参考 [MCP Client Best Practices](https://modelcontextprotocol.io/docs/develop/clients/client-best-practices)。
|
|
76
|
+
- **Typed final output**:确有程序化消费需求时增加 schema-constrained output;校验失败不得 final commit/delivery。
|
|
77
|
+
- **持久化工具审批与显式 resume**:只有向不可信调用方开放危险副作用时才进入核心。它需要审批事件、参数/策略指纹、等待终态、writer fencing、显式 resume 与并发故障矩阵,是独立里程碑,不应和轻量 Tool 权限混做。UI、reviewer 认证仍在 Bot。参考 [OpenAI Agents SDK Human-in-the-loop](https://openai.github.io/openai-agents-python/human_in_the_loop/)。
|
|
78
|
+
|
|
79
|
+
## 3. 架构扩展性:保留核心,打开 Harness
|
|
80
|
+
|
|
81
|
+
现有四个适配缝已经足够小:EventStore、ModelProvider、ToolRuntime、DeliveryOwner(`src/nextloop/contracts.py:502-547`)。问题主要是 Harness 没有提供同等注入能力:
|
|
82
|
+
|
|
83
|
+
- Provider 工厂固定 OpenAI/Anthropic,且强制 `api_key_env`(`src/nextloop/harness.py:186-208,284-293`)。
|
|
84
|
+
- Delivery 无条件使用 MemoryDeliveryOwner(`src/nextloop/harness.py:83-107`)。
|
|
85
|
+
- Composite 只完整管理 StdioDriver 的 start/close,其他 runtime 没有统一可选生命周期(`src/nextloop/execute/composite.py:74-93,124-131`)。
|
|
86
|
+
- Exporter 缺少可选 flush/close,持有外部连接的 exporter 无法统一收尾(`src/nextloop/adapters/observing.py:152-197`)。
|
|
87
|
+
|
|
88
|
+
建议只为 Harness 增加可选 `provider`、`store`、`delivery`、`tools/runtime` 构造注入;缺省路径不变。依赖所有权遵循“谁创建谁关闭”,调用方注入对象默认由调用方关闭。需要生命周期时检测可选 `start/flush/close` 能力,不扩大所有 Protocol。
|
|
89
|
+
|
|
90
|
+
Store 扩展必须有 contract suite,而不只是类型签名:原子批次 append、expected_seq/event_id 幂等、writer_epoch fencing、有序读取、surface/recovery 语义、至少一次 Delivery。读取未知 schema_version 时应 fail-fast 或显式迁移。
|
|
91
|
+
|
|
92
|
+
暂缓项:
|
|
93
|
+
|
|
94
|
+
- SQLite 使用同步 sqlite3,确实可能阻塞 async 宿主(`src/nextloop/adapters/sqlite.py:71-189`),但当前基准未证明它是吞吐瓶颈。只有明确 heartbeat/P99 延迟目标后,才把完整事务/查询移入受控线程;不能把单条 SQL 拆到不同线程,也不能降低 WAL/synchronous 级别。
|
|
95
|
+
- Model attempt 当前批量提交(`src/nextloop/execute/model.py:48-124`),实时 Trace 需改为逐 attempt 提交才有真实时性;应与流式 preview 一起决定,避免两套实时通道。
|
|
96
|
+
- `_actors` 与 `_resource_locks` 按 Session 保留(`src/nextloop/loop.py:18-22`)。长生命周期大量 Session 时才设计休眠清理,且不能清除未决资源锁。
|
|
97
|
+
|
|
98
|
+
## 4. 性能:实测热点与优化门槛
|
|
99
|
+
|
|
100
|
+
环境:macOS arm64,Python 3.11.18,SQLite 3.53.1,FakeProvider、短消息、no-op Tool、本地临时文件。每项多次运行取中位数;不代表真实模型网络、生产 payload 或 P95/P99。
|
|
101
|
+
|
|
102
|
+
### 4.1 主热点:逐 Step 全量 Context 重建
|
|
103
|
+
|
|
104
|
+
每个 Step 都执行 `load_surface + project_events`(`src/nextloop/driver.py:94-109`)。Memory 会筛选并 deepcopy 全部事件(`src/nextloop/adapters/memory.py:63-78`);SQLite 会读取并 JSON decode surface(`src/nextloop/adapters/sqlite.py:166-181,235-250`);随后重新投影 Message 并 canonicalize 请求。
|
|
105
|
+
|
|
106
|
+
| Steps | Memory 中位耗时 | SQLite 中位耗时 |
|
|
107
|
+
|---:|---:|---:|
|
|
108
|
+
| 1 | 0.664 ms | 2.022 ms |
|
|
109
|
+
| 10 | 4.726 ms | 11.644 ms |
|
|
110
|
+
| 50 | 41.891 ms | 60.344 ms |
|
|
111
|
+
| 100 | 141.194 ms | 150.111 ms |
|
|
112
|
+
| 200 | 495.751 ms | 408.817 ms |
|
|
113
|
+
| 400 | 1883.033 ms | 1302.332 ms |
|
|
114
|
+
|
|
115
|
+
100-step cProfile 中,Memory 的主要热点是 `deepcopy`;SQLite 则包括 append、surface 读取、JSON decode 和 projection。不同 Store 的相对常数成本不能外推为普遍优劣,但规模增长明确超线性。
|
|
116
|
+
|
|
117
|
+
建议实现 append 驱动的增量投影:surface append 时增量构造消息和请求状态,compaction epoch 时显式重建。优化前必须建立旧/新实现 parity:
|
|
118
|
+
|
|
119
|
+
- 每步 CandidateRequest、canonical bytes、compaction reason 与 WorkingContext 全等。
|
|
120
|
+
- 覆盖 steer、protocol correction、rejected batch、provider_data、Tool schema 顺序、compaction 后继续执行及 recovery 后新 Turn。
|
|
121
|
+
- 防止调用方 mutation 污染缓存。
|
|
122
|
+
- 同一基准 100/400 Step 中位耗时均降低至少 50%;完整 pytest、mypy、ruff 通过。
|
|
123
|
+
|
|
124
|
+
若 parity 无法证明,不落地复杂缓存;先只做 Memory 不可变快照等小改并单独测量。
|
|
125
|
+
|
|
126
|
+
### 4.2 次热点:历史查询全量回放
|
|
127
|
+
|
|
128
|
+
`_query_history` 先 `load_recovery` 全量事件,再投影,最后按 Turn 过滤(`src/nextloop/adapters/observing.py:224-236`)。Memory 中 1000 个已完成 Turn(2000 events)只查询最后一个 Turn,中位仍为 11.570ms。
|
|
129
|
+
|
|
130
|
+
有历史 API 延迟要求时,先按 Turn 范围读取/过滤,再投影;目标为该场景降低至少 80%,全范围 history 与旧实现完全一致。不能简单 SQL LIMIT 破坏同 Turn 顺序或 recovery 边界。
|
|
131
|
+
|
|
132
|
+
### 4.3 明确不优化
|
|
133
|
+
|
|
134
|
+
- 64 个 async Tool、每个 10ms、max_parallel=4,总耗时 184.127ms,接近理论 16 批,未发现调度热点。
|
|
135
|
+
- SQLite 单事务 append:1 event 0.096ms、10 events 0.133ms、100 events 0.668ms;不为微基准合并 Tool 事件或降低 durability。
|
|
136
|
+
- FakeProvider 下 ModelCaller 约 0.095ms/次;真实 Provider 网络未知,不优化等待框架。
|
|
137
|
+
- 正常 recovery 仅一个开放 Turn 时约 0.222ms;不按病态多开放 Turn 重写。
|
|
138
|
+
- Exporter 已是单 worker 有界队列;不增加 worker、不隐藏 drop、不无界扩队。
|
|
139
|
+
|
|
140
|
+
## 5. 推荐里程碑
|
|
141
|
+
|
|
142
|
+
### M1:Correctness patch
|
|
143
|
+
|
|
144
|
+
只修第 1 节五个缺陷及对应回归测试。关闭条件:所有复现脚本转为测试,全量测试与静态检查通过,真实取消/恢复场景通过。
|
|
145
|
+
|
|
146
|
+
### M2:Embedding and guardrails
|
|
147
|
+
|
|
148
|
+
完成 Harness 构造注入、依赖所有权、未知配置 fail-fast、硬执行预算。关闭条件:第三方 Provider/Store/Delivery/ToolRuntime 不 monkeypatch 即可运行;预算的副作用前保证与恢复计数经过故障测试。
|
|
149
|
+
|
|
150
|
+
### M3:Context scaling
|
|
151
|
+
|
|
152
|
+
先建立 parity 与固定 benchmark,再实现增量 Context Projection;Tool Result spill 可独立分阶段落地。关闭条件:语义全等,性能达到上述阈值,长 Tool 输出请求大小有界。
|
|
153
|
+
|
|
154
|
+
### M4:按产品触发选择
|
|
155
|
+
|
|
156
|
+
流式 preview、MCP、Provider fallback、typed output、审批 resume 各自独立立项,不捆绑成“大版本全家桶”。
|
|
157
|
+
|
|
158
|
+
## 6. 不做清单
|
|
159
|
+
|
|
160
|
+
- 不把 Bot 渠道、权限、用户/群组、定时任务或长期记忆塞入 Loop。
|
|
161
|
+
- 不新增通用插件发现、Service Locator、第二 EventBus 或通用 Hook 控制面。
|
|
162
|
+
- 不做 Router、通用多 Agent、DAG/工作流引擎、分布式队列。
|
|
163
|
+
- 不自动 replay 已 dispatch 的未知副作用。
|
|
164
|
+
- 不为假想需求预建大一统多模态、流式 Provider 抽象或二进制 Asset GC 平台。
|
|
165
|
+
|
|
166
|
+
## 限制与未知
|
|
167
|
+
|
|
168
|
+
- 性能实验未覆盖真实模型网络、长输出、大 Tool payload、多进程 writer、stdio worker、生产 exporter 和磁盘争用。
|
|
169
|
+
- Feature 成本未作工时承诺;优先级依据当前代码机制与通用 Loop 定位,不代表所有宿主都必须实现。
|
|
170
|
+
- 同步 Tool timeout 推荐 fail-fast 是轻量首版决策;若真实用户需要阻塞式 SDK 的硬超时,应另立线程执行契约。
|
|
171
|
+
- 本研究未修改产品代码;后续实施需独立 review 与真实场景验收。
|
|
172
|
+
|
|
173
|
+
## 7. 后续 Feature 设计决策(2026-11-10)
|
|
174
|
+
|
|
175
|
+
### Tool Result 消费策略
|
|
176
|
+
|
|
177
|
+
不一刀切 spill。ToolSpec 使用可信的 `PREVIEW | PAGED | FULL` 声明,默认 PREVIEW:小结果完整内联,超过可配置阈值才保留 UTF-8 安全的 head/tail、精确省略字节数与 result_ref;PAGED 返回连续窗口和 next_offset;FULL 禁止通用裁剪,超独立硬上限必须显式失败。`concludes_turn=True` 按 FULL 处理。初始实验参数为 50,000 bytes、40% head / 60% tail,需用真实任务评估。
|
|
178
|
+
|
|
179
|
+
权威 Event 的 outcome 保留全文,模型 Message 保存对应投影。第一阶段只承诺减少模型 token;只有 surface 不再读取全文后,才承诺降低 Store I/O、JSON decode 或 deepcopy 成本。
|
|
180
|
+
|
|
181
|
+
### Streaming Preview
|
|
182
|
+
|
|
183
|
+
采用可选 PreviewSink 与 Provider 可选 `complete_with_preview()`;旧 `complete()` 和非流式第三方 Provider 保持兼容,非流式路径只给 response snapshot,不伪造 token delta。
|
|
184
|
+
|
|
185
|
+
PreviewEvent 使用 run-local stream_id/seq,包含 text/refusal/tool-call delta、attempt started/ended/resolved 与 reset。preview 不进入 EventStore、不执行部分 Tool arguments、不触发 Delivery;resolved 只在对应 assistant decision 提交后发出。EventStore 仍是唯一事实源。
|
|
186
|
+
|
|
187
|
+
核心提供有界非阻塞队列;相邻 delta 可合并,溢出通过带最新瞬态 snapshot 的 RESET 恢复,控制事件尽量不丢。关闭 preview 只退订,不取消 Turn;停止执行仍显式调用 `AgentLoop.cancel()`。首版不做多订阅、原始 SSE 暴露、持久 replay 或 Last-Event-ID。
|
|
188
|
+
|
|
189
|
+
### MCP 与工具发现
|
|
190
|
+
|
|
191
|
+
MCP 实现为 ToolRuntime adapter,Loop 不感知 MCP。工具发现默认仍使用完整列表;达到实测 schema token 或选择质量门槛后,提供确定性分页 `list_tools` 和精确名称 `inspect_tools`。BM25 `search_tools` 只是可选候选加速,必须证明相对 exact/substring/regex 的收益,不能成为唯一入口。
|
|
192
|
+
|
|
193
|
+
不增加 `execute`/`call_tool` 元工具。模型知道 Schema 时可直接发起普通 Tool Call;需要时先 list/search/inspect,随后仍走现有 BatchExecutor 的名称、参数 JSON 和 JSON Schema 校验,错误交给现有 correction。静态目录首版不增加 revision 检查;只有实现动态 MCP catalog/listChanged 时才评估 revision 冲突。授权由宿主的 authorized catalog 与执行路径保证,不在核心虚构租户模型。
|
|
194
|
+
|
|
195
|
+
### Typed output
|
|
196
|
+
|
|
197
|
+
维持 P2。现有 `Completed(concludes_turn=True)` 是 nextloop 的 terminal Tool 机制:Tool content 直接成为 final,不再调用模型生成答复,可由 Tool 自己完成结构化校验。确有大量程序化 final 消费需求时,再实现 Provider 原生 Schema 约束和 Loop 校验后的 structured final。
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "nextloop"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.5.0rc1"
|
|
8
8
|
description = "An embeddable AgentLoop with explicit event, tool, recovery, and delivery semantics"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.11"
|
|
@@ -32,6 +32,9 @@ from .contracts import (
|
|
|
32
32
|
ModelUsage,
|
|
33
33
|
NewEvent,
|
|
34
34
|
NextloopError,
|
|
35
|
+
PreviewEvent,
|
|
36
|
+
PreviewKind,
|
|
37
|
+
PreviewSink,
|
|
35
38
|
ProtocolExhausted,
|
|
36
39
|
ProviderConfig,
|
|
37
40
|
ProviderError,
|
|
@@ -51,6 +54,7 @@ from .contracts import (
|
|
|
51
54
|
ToolExecutionTiming,
|
|
52
55
|
ToolOutcome,
|
|
53
56
|
ToolOutcomeKind,
|
|
57
|
+
ToolResultMode,
|
|
54
58
|
ToolRuntime,
|
|
55
59
|
ToolSpec,
|
|
56
60
|
TraceConfig,
|
|
@@ -71,11 +75,14 @@ from .contracts import (
|
|
|
71
75
|
)
|
|
72
76
|
from .events.trace import TraceProjector, project_traces
|
|
73
77
|
from .execute.composite import CompositeToolRuntime
|
|
78
|
+
from .execute.mcp import McpStdioRuntime
|
|
79
|
+
from .execute.result_read import with_result_reads
|
|
74
80
|
from .execute.stdio import StdioDriver, StdioProtocolError
|
|
75
81
|
from .execute.tools import ToolContext, ToolRegistry
|
|
76
82
|
from .harness import Harness, MemoryDeliveryOwner, load_harness_spec
|
|
77
83
|
from .loop import AgentLoop
|
|
78
84
|
from .skills import SkillRegistry, skill_view
|
|
85
|
+
from .testing import check_event_store
|
|
79
86
|
|
|
80
87
|
__all__ = [
|
|
81
88
|
"API_VERSION",
|
|
@@ -89,6 +96,7 @@ __all__ = [
|
|
|
89
96
|
"Cancelled",
|
|
90
97
|
"CapacityError",
|
|
91
98
|
"Completed",
|
|
99
|
+
"check_event_store",
|
|
92
100
|
"CompositeToolRuntime",
|
|
93
101
|
"DeliveryOwner",
|
|
94
102
|
"Event",
|
|
@@ -102,6 +110,7 @@ __all__ = [
|
|
|
102
110
|
"InMemoryEventStore",
|
|
103
111
|
"InvariantViolation",
|
|
104
112
|
"LoopConfig",
|
|
113
|
+
"McpStdioRuntime",
|
|
105
114
|
"MemoryDeliveryOwner",
|
|
106
115
|
"Message",
|
|
107
116
|
"ModelCallPurpose",
|
|
@@ -114,6 +123,9 @@ __all__ = [
|
|
|
114
123
|
"NextloopError",
|
|
115
124
|
"ObservingEventStore",
|
|
116
125
|
"OpenAIProvider",
|
|
126
|
+
"PreviewEvent",
|
|
127
|
+
"PreviewKind",
|
|
128
|
+
"PreviewSink",
|
|
117
129
|
"ProtocolExhausted",
|
|
118
130
|
"ProviderConfig",
|
|
119
131
|
"ProviderError",
|
|
@@ -140,6 +152,7 @@ __all__ = [
|
|
|
140
152
|
"ToolOutcome",
|
|
141
153
|
"ToolOutcomeKind",
|
|
142
154
|
"ToolRegistry",
|
|
155
|
+
"ToolResultMode",
|
|
143
156
|
"ToolRuntime",
|
|
144
157
|
"ToolSpec",
|
|
145
158
|
"TraceConfig",
|
|
@@ -161,4 +174,5 @@ __all__ = [
|
|
|
161
174
|
"project_traces",
|
|
162
175
|
"skill_view",
|
|
163
176
|
"utc_now",
|
|
177
|
+
"with_result_reads",
|
|
164
178
|
]
|
|
@@ -2,7 +2,7 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
import asyncio
|
|
4
4
|
import json
|
|
5
|
-
from collections.abc import Mapping
|
|
5
|
+
from collections.abc import Callable, Mapping
|
|
6
6
|
from typing import Any
|
|
7
7
|
|
|
8
8
|
import httpx
|
|
@@ -43,6 +43,26 @@ class AnthropicProvider:
|
|
|
43
43
|
*,
|
|
44
44
|
attempt: int,
|
|
45
45
|
cancel: asyncio.Event,
|
|
46
|
+
) -> ModelResponse:
|
|
47
|
+
return await self._complete(request, attempt=attempt, cancel=cancel)
|
|
48
|
+
|
|
49
|
+
async def complete_with_preview(
|
|
50
|
+
self,
|
|
51
|
+
request: CandidateRequest,
|
|
52
|
+
*,
|
|
53
|
+
attempt: int,
|
|
54
|
+
cancel: asyncio.Event,
|
|
55
|
+
emit: Callable[[Mapping[str, Any]], None],
|
|
56
|
+
) -> ModelResponse:
|
|
57
|
+
return await self._complete(request, attempt=attempt, cancel=cancel, emit=emit)
|
|
58
|
+
|
|
59
|
+
async def _complete(
|
|
60
|
+
self,
|
|
61
|
+
request: CandidateRequest,
|
|
62
|
+
*,
|
|
63
|
+
attempt: int,
|
|
64
|
+
cancel: asyncio.Event,
|
|
65
|
+
emit: Callable[[Mapping[str, Any]], None] | None = None,
|
|
46
66
|
) -> ModelResponse:
|
|
47
67
|
system = "\n\n".join(
|
|
48
68
|
message.content for message in request.messages if message.role == "system"
|
|
@@ -102,24 +122,48 @@ class AnthropicProvider:
|
|
|
102
122
|
index = int(payload["index"])
|
|
103
123
|
block = payload["content_block"]
|
|
104
124
|
if block["type"] == "text":
|
|
105
|
-
|
|
125
|
+
text = str(block.get("text", ""))
|
|
126
|
+
text_blocks[index] = [text]
|
|
127
|
+
if emit is not None and text:
|
|
128
|
+
emit({"channel": "content", "delta": text})
|
|
106
129
|
elif block["type"] == "tool_use":
|
|
130
|
+
arguments = (
|
|
131
|
+
json.dumps(block["input"], separators=(",", ":"))
|
|
132
|
+
if block.get("input")
|
|
133
|
+
else ""
|
|
134
|
+
)
|
|
107
135
|
tool_blocks[index] = {
|
|
108
136
|
"id": block["id"],
|
|
109
137
|
"name": block["name"],
|
|
110
|
-
"arguments":
|
|
111
|
-
json.dumps(block["input"], separators=(",", ":"))
|
|
112
|
-
if block.get("input")
|
|
113
|
-
else ""
|
|
114
|
-
),
|
|
138
|
+
"arguments": arguments,
|
|
115
139
|
}
|
|
140
|
+
if emit is not None:
|
|
141
|
+
emit(
|
|
142
|
+
{
|
|
143
|
+
"channel": "tool_call",
|
|
144
|
+
"index": index,
|
|
145
|
+
"call_id": str(block["id"]),
|
|
146
|
+
"name_delta": str(block["name"]),
|
|
147
|
+
"arguments_delta": arguments,
|
|
148
|
+
}
|
|
149
|
+
)
|
|
116
150
|
elif event == "content_block_delta":
|
|
117
151
|
index = int(payload["index"])
|
|
118
152
|
delta = payload["delta"]
|
|
119
153
|
if delta["type"] == "text_delta":
|
|
120
154
|
text_blocks.setdefault(index, []).append(delta["text"])
|
|
155
|
+
if emit is not None:
|
|
156
|
+
emit({"channel": "content", "delta": str(delta["text"])})
|
|
121
157
|
elif delta["type"] == "input_json_delta":
|
|
122
158
|
tool_blocks[index]["arguments"] += delta["partial_json"]
|
|
159
|
+
if emit is not None:
|
|
160
|
+
emit(
|
|
161
|
+
{
|
|
162
|
+
"channel": "tool_call",
|
|
163
|
+
"index": index,
|
|
164
|
+
"arguments_delta": str(delta["partial_json"]),
|
|
165
|
+
}
|
|
166
|
+
)
|
|
123
167
|
elif event == "message_delta":
|
|
124
168
|
stop_reason = payload.get("delta", {}).get("stop_reason")
|
|
125
169
|
finish_reason = self._finish_reason(stop_reason) or finish_reason
|
|
@@ -41,23 +41,28 @@ class InMemoryEventStore:
|
|
|
41
41
|
existing = self._events[session_id]
|
|
42
42
|
if expected_seq != len(existing) + 1:
|
|
43
43
|
return self._idempotent(existing, events)
|
|
44
|
+
known_ids = {event.event_id for event in existing}
|
|
44
45
|
committed = []
|
|
45
46
|
for offset, draft in enumerate(events):
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
47
|
+
if draft.event_id in known_ids:
|
|
48
|
+
raise InvariantViolation("Event ID reused with different content")
|
|
49
|
+
known_ids.add(draft.event_id)
|
|
50
|
+
committed.append(
|
|
51
|
+
Event(
|
|
52
|
+
session_id=session_id,
|
|
53
|
+
seq=expected_seq + offset,
|
|
54
|
+
turn=draft.turn,
|
|
55
|
+
step=draft.step,
|
|
56
|
+
attempt=draft.attempt,
|
|
57
|
+
writer_epoch=writer_epoch,
|
|
58
|
+
event_id=draft.event_id,
|
|
59
|
+
event_type=draft.event_type,
|
|
60
|
+
surface_op=draft.surface_op,
|
|
61
|
+
payload=copy.deepcopy(dict(draft.payload)),
|
|
62
|
+
occurred_at=utc_now(),
|
|
63
|
+
)
|
|
58
64
|
)
|
|
59
|
-
|
|
60
|
-
committed.append(event)
|
|
65
|
+
existing.extend(committed)
|
|
61
66
|
return committed
|
|
62
67
|
|
|
63
68
|
async def load_surface(self, session_id: str) -> Sequence[Event]:
|