react-agent-harness 0.10.3__tar.gz → 0.11.1__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.
- {react_agent_harness-0.10.3/react_agent_harness.egg-info → react_agent_harness-0.11.1}/PKG-INFO +1 -1
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/README.md +39 -3
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/agents/base.py +114 -108
- react_agent_harness-0.11.1/harness/agent_tree.py +52 -0
- react_agent_harness-0.11.1/harness/background_tasks.py +217 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/console.py +284 -1
- react_agent_harness-0.11.1/harness/events.py +454 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/hitl.py +5 -1
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/llm/anthropic.py +44 -5
- react_agent_harness-0.11.1/harness/model_switching.py +124 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/persistent.py +241 -885
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/persistent_completion.py +54 -2
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/persistent_controls.py +102 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/persistent_prompt.py +4 -1
- react_agent_harness-0.11.1/harness/plan_mode.py +280 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/runtime.py +5 -17
- react_agent_harness-0.11.1/harness/session_memory.py +479 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/orchestrator/planner.py +19 -38
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/pyproject.toml +18 -1
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1/react_agent_harness.egg-info}/PKG-INFO +1 -1
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/react_agent_harness.egg-info/SOURCES.txt +12 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_agents_base.py +81 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_anthropic_llm.py +81 -0
- react_agent_harness-0.11.1/tests/test_background_tasks.py +146 -0
- react_agent_harness-0.11.1/tests/test_console_renderer.py +321 -0
- react_agent_harness-0.11.1/tests/test_event_payloads.py +199 -0
- react_agent_harness-0.11.1/tests/test_hitl_banner.py +34 -0
- react_agent_harness-0.11.1/tests/test_model_switching.py +147 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_persistent_agent.py +256 -9
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_persistent_completion.py +74 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_persistent_controls.py +80 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_plan_mode.py +3 -2
- react_agent_harness-0.11.1/tests/test_plan_mode_controller.py +187 -0
- react_agent_harness-0.11.1/tests/test_session_memory.py +216 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_subagent_tool.py +123 -0
- react_agent_harness-0.11.1/tools/builtin/background.py +123 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tools/builtin/subagent.py +19 -1
- react_agent_harness-0.10.3/harness/events.py +0 -74
- react_agent_harness-0.10.3/tests/test_console_renderer.py +0 -145
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/LICENSE +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/agents/__init__.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/__init__.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/annotation.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/cancellation.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/checkpoint.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/cli.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/executor_bridge.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/llm/__init__.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/llm/_streaming.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/llm/auth.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/llm/claude_code.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/llm/fallback.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/llm/openai.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/llm/openai_codex.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/llm/routing.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/oauth_browser.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/otel.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/skills.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/steering.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/streaming.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/tool_policy.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/trace.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/trace_viewer.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/harness/utils.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/memory/__init__.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/memory/episodic_lance.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/memory/manager.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/memory/redis_store.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/memory/stores.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/memory/working.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/orchestrator/__init__.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/react_agent_harness.egg-info/dependency_links.txt +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/react_agent_harness.egg-info/entry_points.txt +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/react_agent_harness.egg-info/requires.txt +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/react_agent_harness.egg-info/top_level.txt +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/setup.cfg +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_annotation.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_budget_guard.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_cancellation.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_checkpoint_resume.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_claude_code_llm.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_cli.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_executor_bridge.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_fallback_llm.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_fan_in.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_http_fetch.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_llm_auth.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_mcp_adapter.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_mcp_auth.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_memory.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_memory_reconciler.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_memory_touchpoints.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_oauth_browser.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_openai_codex_llm.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_openai_llm.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_orchestrator.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_otel.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_parse_action_json.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_per_call_site_llm.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_persistent_prompt.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_plan_approval.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_redis_store.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_resume_hint_banner.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_routing_llm.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_skills.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_steering.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_streaming.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_system_prompt_routing.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_tool_policy.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_trace.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_trailing_assistant_state.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_utils.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_vision.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tests/test_working_memory.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tools/__init__.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tools/builtin/__init__.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tools/builtin/fetch_image.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tools/builtin/http_fetch.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tools/mcp/__init__.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tools/mcp/adapter.py +0 -0
- {react_agent_harness-0.10.3 → react_agent_harness-0.11.1}/tools/mcp/auth.py +0 -0
{react_agent_harness-0.10.3/react_agent_harness.egg-info → react_agent_harness-0.11.1}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: react-agent-harness
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.11.1
|
|
4
4
|
Summary: Multi-agent LLM orchestration: hybrid DAG planning, two-tier memory, streaming, cost/token budgets with per-call-site breakdown
|
|
5
5
|
Requires-Python: >=3.10
|
|
6
6
|
License-File: LICENSE
|
|
@@ -455,6 +455,18 @@ agents.register(AgentConfig(
|
|
|
455
455
|
))
|
|
456
456
|
```
|
|
457
457
|
|
|
458
|
+
### Tool observation budget
|
|
459
|
+
|
|
460
|
+
Tool results emitted on the event stream stay intact, but the observation
|
|
461
|
+
text appended back into the agent's working memory is capped by
|
|
462
|
+
`AgentConfig.max_observation_chars` (default `20_000`). This prevents a
|
|
463
|
+
single browser snapshot, MCP response, or shell output from dominating the
|
|
464
|
+
next LLM call. When capped, the model sees a structured JSON envelope with
|
|
465
|
+
`truncated`, `original_chars`, `shown_chars`, `omitted_chars`, and `content`.
|
|
466
|
+
|
|
467
|
+
Set `max_observation_chars=0` to disable this cap for agents that must feed
|
|
468
|
+
full raw tool output back into the next step.
|
|
469
|
+
|
|
458
470
|
Defaults are in-memory (`InMemorySemanticStore`, `InMemoryEpisodicStore`).
|
|
459
471
|
For durable storage:
|
|
460
472
|
|
|
@@ -623,8 +635,12 @@ main_agent = BaseAgent(
|
|
|
623
635
|
)
|
|
624
636
|
|
|
625
637
|
async for event in main_agent.run_stream(goal):
|
|
626
|
-
# Sub-agent
|
|
627
|
-
#
|
|
638
|
+
# Sub-agent events bubble up tagged with event.parent_agent_id so
|
|
639
|
+
# renderers can indent or group them.
|
|
640
|
+
# SUBAGENT_START fires before the sub's first event (payload:
|
|
641
|
+
# task, invocation_id).
|
|
642
|
+
# SUBAGENT_DONE fires after the sub's TASK_DONE (payload: success,
|
|
643
|
+
# steps, confidence, answer, error, invocation_id).
|
|
628
644
|
...
|
|
629
645
|
```
|
|
630
646
|
|
|
@@ -757,7 +773,7 @@ from harness.persistent_completion import SlashCommandCompleter
|
|
|
757
773
|
|
|
758
774
|
session = PromptSession(
|
|
759
775
|
history=FileHistory("~/.agent-harness/demo_history"),
|
|
760
|
-
completer=SlashCommandCompleter(app),
|
|
776
|
+
completer=SlashCommandCompleter(app, session_id_provider=lambda: session_id),
|
|
761
777
|
complete_while_typing=False, # Tab-triggered; doesn't query the store every keystroke
|
|
762
778
|
)
|
|
763
779
|
message = (await session.prompt_async("> ")).strip()
|
|
@@ -776,6 +792,8 @@ The demo uses that utility for:
|
|
|
776
792
|
- `/usage` shows persisted total and last-run provider-reported token usage
|
|
777
793
|
- `/models` lists switchable model names when an LLM registry is configured
|
|
778
794
|
- `/model [agent_id] [model|default]` shows or changes a session-scoped model override
|
|
795
|
+
- `/background <agent_id> <task>` launches a sub-agent task in the background for the current process
|
|
796
|
+
- `/tasks [collect|cancel] [id|all]` lists background tasks, appends completed results to the transcript, or cancels running work
|
|
779
797
|
- `/sessions [query]` lists known session ids, optionally filtered by id text
|
|
780
798
|
- `/memory` shows the cached per-session memory context
|
|
781
799
|
- `/save` flushes turns after the last reconcile checkpoint into long-term memory **without** evicting the cached prior (foreground prefix stays warm)
|
|
@@ -788,6 +806,22 @@ The demo uses that utility for:
|
|
|
788
806
|
- `/delete [id] confirm` deletes a transcript row; long-term memory is retained
|
|
789
807
|
- `/end` exits the demo — no auto-flush; call `/save` first if you want pending facts persisted
|
|
790
808
|
|
|
809
|
+
Background sub-agent tasks are process-local. They keep running while the
|
|
810
|
+
REPL/server process is alive; completed results become durable only when
|
|
811
|
+
collected into the session transcript with `/tasks collect <id|all>`. Avoid
|
|
812
|
+
launching overlapping work against the same sub-agent instance unless that
|
|
813
|
+
agent's tools and LLM adapter are safe for concurrent use.
|
|
814
|
+
|
|
815
|
+
When a `PersistentAgent` coordinator is wired with `SubAgentTool`s, it also
|
|
816
|
+
gets LLM-visible background tools automatically:
|
|
817
|
+
|
|
818
|
+
- `background_delegate_<agent_id>(task)` starts that sub-agent and returns a `task_id`
|
|
819
|
+
- `check_background_task(task_id)` reports status and any available result preview
|
|
820
|
+
- `collect_background_task(task_id)` returns the completed answer to the coordinator and appends it to the transcript
|
|
821
|
+
|
|
822
|
+
The regular `delegate_<agent_id>` tools remain blocking; background delegation
|
|
823
|
+
uses separate tool names so the model's intent is explicit in traces.
|
|
824
|
+
|
|
791
825
|
**Plan mode** (`/plan on`, or **Shift-Tab** at the prompt) gates each turn behind an approval step. The
|
|
792
826
|
coordinator's LLM produces a structured plan (`{summary, steps[]}`) before
|
|
793
827
|
any tools run; `PersistentAgent.chat` yields a `PLAN_PROPOSED` event so
|
|
@@ -935,6 +969,7 @@ Event types by path:
|
|
|
935
969
|
| `DISPATCH` | ✓ | — | — | — | — |
|
|
936
970
|
| `ROUTE` | ✓ (simple) | ✓ | — | — | — |
|
|
937
971
|
| `THOUGHT` / `TOKEN` / `ACTION` / `OBSERVATION` | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
972
|
+
| `SUBAGENT_START` / `SUBAGENT_DONE` | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
938
973
|
| `TASK_DONE` | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
939
974
|
| `PLAN` / `REPLAN` / `SYNTHESIS` / `DONE` | ✓ (complex) | — | — | ✓ | ✓ |
|
|
940
975
|
| `ERROR` | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
@@ -1781,6 +1816,7 @@ agents alongside HITL on the shell tool.
|
|
|
1781
1816
|
| `memory_context_enabled` | `True` | Prepend relevant long-term memory to the system prompt |
|
|
1782
1817
|
| `confidence_from_llm` | `True` | Use the `confidence` field from the LLM response; set `False` to always return `1.0` |
|
|
1783
1818
|
| `working_memory_max_tokens` | `None` (auto-derive from `llm.input_token_budget × 0.8`; pass an int to hard-cap) | Token budget for in-context working memory before rolling summarisation kicks in |
|
|
1819
|
+
| `max_observation_chars` | `20_000` | Character cap for model-facing tool observations appended to working memory; event payloads keep the original result. Set `0` to disable |
|
|
1784
1820
|
| `hitl_tools` | `[]` | Tool names that require human approval before execution |
|
|
1785
1821
|
| `checkpoint_every` | `0` | Write a crash-resumable checkpoint every N steps; `0` disables periodic checkpoints |
|
|
1786
1822
|
| `stream_tokens` | `False` | Emit `TOKEN` events as the LLM streams. Disabled by default — enable if you want to render partial output in real time: `AgentConfig(..., stream_tokens=True)` |
|
|
@@ -81,6 +81,11 @@ class AgentConfig:
|
|
|
81
81
|
# int to hard-cap the WorkingMemory eviction threshold — useful for
|
|
82
82
|
# cost-sensitive workloads or when feeding very small models.
|
|
83
83
|
working_memory_max_tokens: int | None = None
|
|
84
|
+
# Cap model-facing text appended after tool observations. Event payloads
|
|
85
|
+
# and traces still carry the original tool result; this only protects the
|
|
86
|
+
# next LLM prompt from oversized browser/MCP/shell observations. Set to
|
|
87
|
+
# 0 or a negative value to disable the cap.
|
|
88
|
+
max_observation_chars: int = 20_000
|
|
84
89
|
hitl_tools: list[str] = None # tools requiring human approval; None = no HITL
|
|
85
90
|
checkpoint_every: int = 0 # write a resumable checkpoint every N steps; 0 = disabled
|
|
86
91
|
# Cache tool results within a single run, keyed by (tool_name, args).
|
|
@@ -183,6 +188,8 @@ class BaseAgent:
|
|
|
183
188
|
self._guard = guard
|
|
184
189
|
self._llm = llm
|
|
185
190
|
self._checkpoint_store = checkpoint_store
|
|
191
|
+
self._checkpoint_resume_enabled: bool = True
|
|
192
|
+
self._hitl_resume_hint: str | None = None
|
|
186
193
|
self._working_memory: WorkingMemory | None = None
|
|
187
194
|
self._task: str = ""
|
|
188
195
|
self._last_think_error: str | None = None
|
|
@@ -248,11 +255,7 @@ class BaseAgent:
|
|
|
248
255
|
self.config.agent_id,
|
|
249
256
|
{"step": step, "text": text},
|
|
250
257
|
)
|
|
251
|
-
yield BusEvent(
|
|
252
|
-
type=EventType.HUMAN_GUIDANCE,
|
|
253
|
-
agent_id=self.config.agent_id,
|
|
254
|
-
payload={"step": step, "text": text},
|
|
255
|
-
)
|
|
258
|
+
yield BusEvent.human_guidance(self.config.agent_id, step=step, text=text)
|
|
256
259
|
|
|
257
260
|
# ── Streaming entry point (canonical) ─────────────────────────────────────
|
|
258
261
|
|
|
@@ -396,11 +399,7 @@ class BaseAgent:
|
|
|
396
399
|
yield event
|
|
397
400
|
except Exception as e:
|
|
398
401
|
logger.exception("Agent %s stream crashed", self.config.agent_id)
|
|
399
|
-
yield BusEvent(
|
|
400
|
-
type=EventType.ERROR,
|
|
401
|
-
agent_id=self.config.agent_id,
|
|
402
|
-
error=str(e),
|
|
403
|
-
)
|
|
402
|
+
yield BusEvent.error_event(self.config.agent_id, error=str(e))
|
|
404
403
|
finally:
|
|
405
404
|
if self._working_memory is not None:
|
|
406
405
|
self._tracer.log(
|
|
@@ -479,7 +478,7 @@ class BaseAgent:
|
|
|
479
478
|
# ── ReAct Loop (stream) ───────────────────────────────────────────────────
|
|
480
479
|
|
|
481
480
|
async def _write_step_checkpoint(self, run_id: str, step: int) -> None:
|
|
482
|
-
if self._checkpoint_store is None:
|
|
481
|
+
if self._checkpoint_store is None or not self._checkpoint_resume_enabled:
|
|
483
482
|
return
|
|
484
483
|
await self._checkpoint_store.write(
|
|
485
484
|
self._ckp_id,
|
|
@@ -526,11 +525,7 @@ class BaseAgent:
|
|
|
526
525
|
self.config.agent_id,
|
|
527
526
|
{"answer": "", "confidence": 0.0, "steps": step, "error": reason},
|
|
528
527
|
)
|
|
529
|
-
yield BusEvent(
|
|
530
|
-
type=EventType.ERROR,
|
|
531
|
-
agent_id=self.config.agent_id,
|
|
532
|
-
error=reason,
|
|
533
|
-
)
|
|
528
|
+
yield BusEvent.error_event(self.config.agent_id, error=reason)
|
|
534
529
|
return
|
|
535
530
|
|
|
536
531
|
self._tracer.log(
|
|
@@ -580,11 +575,7 @@ class BaseAgent:
|
|
|
580
575
|
"error": "",
|
|
581
576
|
},
|
|
582
577
|
)
|
|
583
|
-
yield BusEvent(
|
|
584
|
-
type=EventType.TASK_DONE,
|
|
585
|
-
agent_id=self.config.agent_id,
|
|
586
|
-
payload=result,
|
|
587
|
-
)
|
|
578
|
+
yield BusEvent.task_done_agent(self.config.agent_id, result=result)
|
|
588
579
|
return
|
|
589
580
|
|
|
590
581
|
# Act — parallel or single
|
|
@@ -615,14 +606,11 @@ class BaseAgent:
|
|
|
615
606
|
|
|
616
607
|
# Emit ACTION events first so callers see what's being launched.
|
|
617
608
|
for act in parallel_actions:
|
|
618
|
-
yield BusEvent(
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
"tool": act.get("tool", ""),
|
|
624
|
-
"args": act.get("args", {}),
|
|
625
|
-
},
|
|
609
|
+
yield BusEvent.action(
|
|
610
|
+
self.config.agent_id,
|
|
611
|
+
step=step,
|
|
612
|
+
tool=act.get("tool", ""),
|
|
613
|
+
args=act.get("args", {}),
|
|
626
614
|
)
|
|
627
615
|
|
|
628
616
|
# Fan out all approved tool calls concurrently. Mixed
|
|
@@ -702,7 +690,9 @@ class BaseAgent:
|
|
|
702
690
|
for i, (act, obs) in enumerate(zip(parallel_actions, observations, strict=False)):
|
|
703
691
|
tool_name = act.get("tool", "")
|
|
704
692
|
tool_args = act.get("args", {})
|
|
705
|
-
|
|
693
|
+
is_img = _is_image_block(obs)
|
|
694
|
+
obs_raw = "[image]" if is_img else str(obs)
|
|
695
|
+
obs_display = obs_raw if is_img else obs_raw[:500]
|
|
706
696
|
self._tracer.log(
|
|
707
697
|
"action",
|
|
708
698
|
self.config.agent_id,
|
|
@@ -714,13 +704,14 @@ class BaseAgent:
|
|
|
714
704
|
},
|
|
715
705
|
)
|
|
716
706
|
observation_events.append(
|
|
717
|
-
BusEvent(
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
707
|
+
BusEvent.observation(
|
|
708
|
+
self.config.agent_id,
|
|
709
|
+
step=step,
|
|
710
|
+
tool=tool_name,
|
|
711
|
+
observation=obs_raw,
|
|
721
712
|
)
|
|
722
713
|
)
|
|
723
|
-
combined.append({"tool": tool_name, "result":
|
|
714
|
+
combined.append({"tool": tool_name, "result": obs_raw})
|
|
724
715
|
if obs and not isinstance(obs, str) and not _is_image_block(obs):
|
|
725
716
|
fire(
|
|
726
717
|
self._memory.write_working_fact(
|
|
@@ -744,10 +735,8 @@ class BaseAgent:
|
|
|
744
735
|
# Single action path.
|
|
745
736
|
tool_name = response.get("action", "")
|
|
746
737
|
tool_args = response.get("args", {})
|
|
747
|
-
yield BusEvent(
|
|
748
|
-
|
|
749
|
-
agent_id=self.config.agent_id,
|
|
750
|
-
payload={"step": step, "tool": tool_name, "args": tool_args},
|
|
738
|
+
yield BusEvent.action(
|
|
739
|
+
self.config.agent_id, step=step, tool=tool_name, args=tool_args
|
|
751
740
|
)
|
|
752
741
|
|
|
753
742
|
tool_obj = self._tools.get(tool_name)
|
|
@@ -776,7 +765,9 @@ class BaseAgent:
|
|
|
776
765
|
continue
|
|
777
766
|
|
|
778
767
|
await self._record_tool_observation(response, tool_name, observation)
|
|
779
|
-
|
|
768
|
+
is_img = _is_image_block(observation)
|
|
769
|
+
obs_raw = "[image]" if is_img else str(observation)
|
|
770
|
+
obs_display = obs_raw if is_img else obs_raw[:500]
|
|
780
771
|
self._tracer.log(
|
|
781
772
|
"action",
|
|
782
773
|
self.config.agent_id,
|
|
@@ -787,14 +778,8 @@ class BaseAgent:
|
|
|
787
778
|
"observation": obs_display,
|
|
788
779
|
},
|
|
789
780
|
)
|
|
790
|
-
yield BusEvent(
|
|
791
|
-
|
|
792
|
-
agent_id=self.config.agent_id,
|
|
793
|
-
payload={
|
|
794
|
-
"step": step,
|
|
795
|
-
"tool": tool_name,
|
|
796
|
-
"observation": obs_display,
|
|
797
|
-
},
|
|
781
|
+
yield BusEvent.observation(
|
|
782
|
+
self.config.agent_id, step=step, tool=tool_name, observation=obs_raw
|
|
798
783
|
)
|
|
799
784
|
|
|
800
785
|
if (
|
|
@@ -822,11 +807,10 @@ class BaseAgent:
|
|
|
822
807
|
"error": f"Max steps ({self.config.max_steps}) reached",
|
|
823
808
|
},
|
|
824
809
|
)
|
|
825
|
-
yield BusEvent(
|
|
826
|
-
|
|
827
|
-
agent_id=self.config.agent_id,
|
|
810
|
+
yield BusEvent.error_event(
|
|
811
|
+
self.config.agent_id,
|
|
828
812
|
error=f"Max steps ({self.config.max_steps}) reached",
|
|
829
|
-
|
|
813
|
+
steps=self.config.max_steps,
|
|
830
814
|
)
|
|
831
815
|
|
|
832
816
|
# ── Think ─────────────────────────────────────────────────────────────────
|
|
@@ -889,11 +873,7 @@ class BaseAgent:
|
|
|
889
873
|
):
|
|
890
874
|
accumulated += token
|
|
891
875
|
if self.config.stream_tokens:
|
|
892
|
-
yield BusEvent(
|
|
893
|
-
type=EventType.TOKEN,
|
|
894
|
-
agent_id=self.config.agent_id,
|
|
895
|
-
token=token,
|
|
896
|
-
)
|
|
876
|
+
yield BusEvent.token_event(self.config.agent_id, token=token)
|
|
897
877
|
response = _normalize_response(accumulated)
|
|
898
878
|
if response is None:
|
|
899
879
|
response = await self._retry_complete_after_bad_stream(
|
|
@@ -927,39 +907,24 @@ class BaseAgent:
|
|
|
927
907
|
|
|
928
908
|
after_usage = self._working_memory.context_usage()
|
|
929
909
|
if self._working_memory.summarization_count > before_summarizations:
|
|
930
|
-
yield BusEvent(
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
"before": before_usage,
|
|
936
|
-
"after": after_usage,
|
|
937
|
-
"summarizations": self._working_memory.summarization_count,
|
|
938
|
-
},
|
|
910
|
+
yield BusEvent.memory(
|
|
911
|
+
self.config.agent_id,
|
|
912
|
+
before=before_usage,
|
|
913
|
+
after=after_usage,
|
|
914
|
+
summarizations=self._working_memory.summarization_count,
|
|
939
915
|
)
|
|
940
916
|
llm_usage = getattr(self._llm, "last_usage", None) or {}
|
|
941
917
|
if llm_usage or after_usage != before_usage:
|
|
942
|
-
yield BusEvent(
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
"cache_read_tokens": llm_usage.get("cache_read_tokens"),
|
|
950
|
-
"cache_creation_tokens": llm_usage.get("cache_creation_tokens"),
|
|
951
|
-
},
|
|
918
|
+
yield BusEvent.context_usage(
|
|
919
|
+
self.config.agent_id,
|
|
920
|
+
usage=after_usage,
|
|
921
|
+
tokens_in=llm_usage.get("tokens_in"),
|
|
922
|
+
tokens_out=llm_usage.get("tokens_out"),
|
|
923
|
+
cache_read_tokens=llm_usage.get("cache_read_tokens"),
|
|
924
|
+
cache_creation_tokens=llm_usage.get("cache_creation_tokens"),
|
|
952
925
|
)
|
|
953
926
|
|
|
954
|
-
yield BusEvent(
|
|
955
|
-
type=EventType.THOUGHT,
|
|
956
|
-
agent_id=self.config.agent_id,
|
|
957
|
-
payload={
|
|
958
|
-
"response": response,
|
|
959
|
-
"thought": response.get("thought", "") if response else "",
|
|
960
|
-
"action": response.get("action") if response else None,
|
|
961
|
-
},
|
|
962
|
-
)
|
|
927
|
+
yield BusEvent.thought(self.config.agent_id, response=response)
|
|
963
928
|
|
|
964
929
|
async def _retry_complete_after_bad_stream(
|
|
965
930
|
self,
|
|
@@ -1024,6 +989,10 @@ class BaseAgent:
|
|
|
1024
989
|
if not isinstance(observation, str)
|
|
1025
990
|
else observation
|
|
1026
991
|
)
|
|
992
|
+
obs_text = _format_model_observation(
|
|
993
|
+
obs_text,
|
|
994
|
+
max_chars=self.config.max_observation_chars,
|
|
995
|
+
)
|
|
1027
996
|
await self._working_memory.append("user", f"Observation: {obs_text}")
|
|
1028
997
|
|
|
1029
998
|
async def _record_parallel_observations(
|
|
@@ -1038,7 +1007,11 @@ class BaseAgent:
|
|
|
1038
1007
|
content: list = [
|
|
1039
1008
|
{
|
|
1040
1009
|
"type": "text",
|
|
1041
|
-
"text":
|
|
1010
|
+
"text": "Observations:\n"
|
|
1011
|
+
+ _format_model_observation(
|
|
1012
|
+
json.dumps(combined, default=str),
|
|
1013
|
+
max_chars=self.config.max_observation_chars,
|
|
1014
|
+
),
|
|
1042
1015
|
}
|
|
1043
1016
|
]
|
|
1044
1017
|
for tool_name_img, img_block in image_blocks:
|
|
@@ -1048,7 +1021,11 @@ class BaseAgent:
|
|
|
1048
1021
|
return
|
|
1049
1022
|
await self._working_memory.append(
|
|
1050
1023
|
"user",
|
|
1051
|
-
|
|
1024
|
+
"Observations:\n"
|
|
1025
|
+
+ _format_model_observation(
|
|
1026
|
+
json.dumps(combined, default=str),
|
|
1027
|
+
max_chars=self.config.max_observation_chars,
|
|
1028
|
+
),
|
|
1052
1029
|
)
|
|
1053
1030
|
|
|
1054
1031
|
# ── Tool Execution ────────────────────────────────────────────────────────
|
|
@@ -1108,9 +1085,12 @@ class BaseAgent:
|
|
|
1108
1085
|
Run the HITL approval gate for one tool.
|
|
1109
1086
|
|
|
1110
1087
|
Returns ApprovalResponse if the tool is gated, None if not.
|
|
1111
|
-
Writes a crash-resumable checkpoint
|
|
1088
|
+
Writes a crash-resumable checkpoint before blocking when checkpoint
|
|
1089
|
+
resume is enabled.
|
|
1112
1090
|
"""
|
|
1113
|
-
if not
|
|
1091
|
+
if tool_name not in self.config.hitl_tools:
|
|
1092
|
+
return None
|
|
1093
|
+
if self._checkpoint_store is None and self._checkpoint_resume_enabled:
|
|
1114
1094
|
return None
|
|
1115
1095
|
|
|
1116
1096
|
from harness.hitl import ApprovalRequest, is_allowed, request_approval
|
|
@@ -1119,23 +1099,24 @@ class BaseAgent:
|
|
|
1119
1099
|
return None # fast-path: human already allowed this tool/prefix
|
|
1120
1100
|
|
|
1121
1101
|
approval_id = str(uuid.uuid4())
|
|
1122
|
-
|
|
1123
|
-
self.
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
"memory": self._working_memory.to_dict(),
|
|
1130
|
-
"pending": {
|
|
1131
|
-
"approval_id": approval_id,
|
|
1132
|
-
"tool": tool_name,
|
|
1133
|
-
"args": tool_args,
|
|
1102
|
+
if self._checkpoint_store is not None and self._checkpoint_resume_enabled:
|
|
1103
|
+
await self._checkpoint_store.write(
|
|
1104
|
+
self._ckp_id,
|
|
1105
|
+
{
|
|
1106
|
+
"run_id": run_id,
|
|
1107
|
+
"agent_id": self.config.agent_id,
|
|
1108
|
+
"task": self._task,
|
|
1134
1109
|
"step": step,
|
|
1135
|
-
"
|
|
1110
|
+
"memory": self._working_memory.to_dict(),
|
|
1111
|
+
"pending": {
|
|
1112
|
+
"approval_id": approval_id,
|
|
1113
|
+
"tool": tool_name,
|
|
1114
|
+
"args": tool_args,
|
|
1115
|
+
"step": step,
|
|
1116
|
+
"llm_response": llm_response,
|
|
1117
|
+
},
|
|
1136
1118
|
},
|
|
1137
|
-
|
|
1138
|
-
)
|
|
1119
|
+
)
|
|
1139
1120
|
return await request_approval(
|
|
1140
1121
|
ApprovalRequest(
|
|
1141
1122
|
approval_id=approval_id,
|
|
@@ -1145,6 +1126,7 @@ class BaseAgent:
|
|
|
1145
1126
|
args=tool_args,
|
|
1146
1127
|
step=step,
|
|
1147
1128
|
timestamp=datetime.now(timezone.utc).isoformat(),
|
|
1129
|
+
resume_hint=self._hitl_resume_hint if not self._checkpoint_resume_enabled else None,
|
|
1148
1130
|
),
|
|
1149
1131
|
self._guard,
|
|
1150
1132
|
)
|
|
@@ -1335,10 +1317,8 @@ class BaseAgent:
|
|
|
1335
1317
|
)
|
|
1336
1318
|
await self._record_tool_observation(llm_response, tool_name, observation)
|
|
1337
1319
|
obs_display = "[image]" if _is_image_block(observation) else str(observation)[:500]
|
|
1338
|
-
yield BusEvent(
|
|
1339
|
-
|
|
1340
|
-
agent_id=self.config.agent_id,
|
|
1341
|
-
payload={"step": step, "tool": tool_name, "observation": obs_display},
|
|
1320
|
+
yield BusEvent.observation(
|
|
1321
|
+
self.config.agent_id, step=step, tool=tool_name, observation=obs_display
|
|
1342
1322
|
)
|
|
1343
1323
|
await self._commit_checkpoint(run_id, step)
|
|
1344
1324
|
|
|
@@ -1388,6 +1368,32 @@ def _split_system(messages: list[dict]) -> tuple[str | None, list[dict]]:
|
|
|
1388
1368
|
return system_text, rest
|
|
1389
1369
|
|
|
1390
1370
|
|
|
1371
|
+
# ── Tool observation shaping ─────────────────────────────────────────────────
|
|
1372
|
+
|
|
1373
|
+
|
|
1374
|
+
def _format_model_observation(text: str, *, max_chars: int) -> str:
|
|
1375
|
+
"""Return the observation text appended to WorkingMemory.
|
|
1376
|
+
|
|
1377
|
+
The raw tool result is still emitted on the event stream before this
|
|
1378
|
+
method is involved. This cap only protects the *next* LLM call from
|
|
1379
|
+
oversized observations.
|
|
1380
|
+
"""
|
|
1381
|
+
if max_chars <= 0 or len(text) <= max_chars:
|
|
1382
|
+
return text
|
|
1383
|
+
omitted = len(text) - max_chars
|
|
1384
|
+
return json.dumps(
|
|
1385
|
+
{
|
|
1386
|
+
"truncated": True,
|
|
1387
|
+
"original_chars": len(text),
|
|
1388
|
+
"shown_chars": max_chars,
|
|
1389
|
+
"omitted_chars": omitted,
|
|
1390
|
+
"content": text[:max_chars],
|
|
1391
|
+
"note": "Tool observation was capped before the next LLM call.",
|
|
1392
|
+
},
|
|
1393
|
+
ensure_ascii=False,
|
|
1394
|
+
)
|
|
1395
|
+
|
|
1396
|
+
|
|
1391
1397
|
# ── Response normalization (module-level for testability) ────────────────────
|
|
1392
1398
|
|
|
1393
1399
|
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""Agent-tree traversal shared across the persistent-agent concerns.
|
|
2
|
+
|
|
3
|
+
A coordinator ``BaseAgent`` may have ``SubAgentTool``s wired in as tools, each
|
|
4
|
+
wrapping another ``BaseAgent`` (which may itself delegate further). Model
|
|
5
|
+
switching, background tasks, and guard assignment all need to walk this tree.
|
|
6
|
+
These pure helpers are the single place that knows its shape.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import TYPE_CHECKING, Any
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from agents.base import BaseAgent
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def subagent_tool_agent(tool: Any) -> BaseAgent | None:
|
|
18
|
+
"""Return the ``BaseAgent`` wrapped by a ``SubAgentTool``, else ``None``."""
|
|
19
|
+
from agents.base import BaseAgent # noqa: PLC0415 — avoid import cycle at module load
|
|
20
|
+
|
|
21
|
+
if tool.__class__.__name__ != "SubAgentTool":
|
|
22
|
+
return None
|
|
23
|
+
agent = getattr(tool, "_agent", None)
|
|
24
|
+
return agent if isinstance(agent, BaseAgent) else None
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def iter_agents(coordinator: BaseAgent) -> list[BaseAgent]:
|
|
28
|
+
"""Return the coordinator and every sub-agent reachable through its tools,
|
|
29
|
+
depth-first, each visited once."""
|
|
30
|
+
agents: list[BaseAgent] = []
|
|
31
|
+
seen: set[int] = set()
|
|
32
|
+
|
|
33
|
+
def visit(agent: BaseAgent) -> None:
|
|
34
|
+
if id(agent) in seen:
|
|
35
|
+
return
|
|
36
|
+
seen.add(id(agent))
|
|
37
|
+
agents.append(agent)
|
|
38
|
+
for tool in getattr(agent, "_tools", {}).values():
|
|
39
|
+
sub = subagent_tool_agent(tool)
|
|
40
|
+
if sub is not None:
|
|
41
|
+
visit(sub)
|
|
42
|
+
|
|
43
|
+
visit(coordinator)
|
|
44
|
+
return agents
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def find_agent(coordinator: BaseAgent, agent_id: str) -> BaseAgent | None:
|
|
48
|
+
"""Return the reachable agent whose ``config.agent_id`` matches, else ``None``."""
|
|
49
|
+
for agent in iter_agents(coordinator):
|
|
50
|
+
if agent.config.agent_id == agent_id:
|
|
51
|
+
return agent
|
|
52
|
+
return None
|