8000 One agent abstraction: environments, Direct in Layer 1, ReAct hooks, supports and needs by haizhongzheng · Pull Request #111 · Infini-AI-Lab/hermit · GitHub
[go: up one dir, main page]

Skip to content

One agent abstraction: environments, Direct in Layer 1, ReAct hooks, supports and needs - #111

Merged
haizhongzheng merged 3 commits into
mainfrom
agent-environment-refactor
Nov 19, 2026
Merged

haizhongzheng merged 3 commits into
mainfrom
agent-environment-refactor

Conversation

@haizhongzheng
@haizhongzheng haizhongzheng commented Nov 19, 2026 •
Copy link
Copy Markdown
Member

Change

One agent abstraction, the first step of the refactor in the design doc (Hermit agent and benchmark refactor). Every
agent stays inside the three layers, with three things made explicit:

  • Where an agent works is an environment, never a setting. New hermit/agent/environment/: Environment
    (workspace, limits, isolation; exec, upload, download, path) and its implementations Local (isolation
    none), Sandboxed (the run's bubblewrap sandbox) and Container (a Docker container; the old
    hermit/agent/base/container.py, now environment/docker.py). BaseAgent.run(prompt, environment=..., tools=, session=, roles=) replaces run(prompt, workspace=, limits=); a path is a Local. The CLI Base Agents take their
    isolation from the environment and refuse isolation, sandbox or container given as settings (a session that
    took them for settings would have run without its isolation). The isolation code paths (bubblewrap wrap, container
    run) are unchanged; only where they read their choice from changed. placed(path, sandbox) is an Evolver's
    sessions' environment (Sandboxed when the run's sandbox is on). The interface follows Harbor's environments, for
    the follow-up that runs any agent in a Harbor environment.
  • A single model call is Layer 1, not an agent. Direct moves to hermit/agent/model/direct.py and returns a
    Reply; make("direct") is refused with the new place. ctx.agent, the local endpoint and the benchmark loops use
    it as before.
  • One tool loop, extended by hooks. react (hermit/agent/base/react.py) gains hooks (call_model,
    before_tool, after_tool, stopped) and step-by-step driving (start, step, external tools that wait in
    loop.pending for agent.give(loop, ...), in the order the model asked). The acting ReactAgent is the class a benchmark subclasses in its example folder:
    tools as methods marked @tool, an environment_for, and task hooks (system_prompt, user_message, tools,
    answer) besides the loop's. @tool alone reads the description from the docstring and the schema from the
    signature.
  • What an agent takes, what a method needs. An acting agent declares supports (the adapter exports it gives
    its session: context, context_tool, tools, skills, agents, hooks); a method lists what it needs of them
    in its evolver.yaml needs (MWM tools, ASPIRE and ReHarness context_tool, AHE skills, agents, hooks). A run
    whose acting agent does not take one is refused at hermit check and before training (adapter_needs_errors).
    Built-in agents and host pipelines are checked; a task's class that runs only in its sandbox is not imported to
    check it. The check compares declared needs, not template exports: MWM exports roles that CL-Bench's host never
    used, and an export-based check would refuse its shipped configs.

Core changes: hermit/agent/environment/ (new), hermit/agent/base/{interface,cli,react,endpoint}.py,
hermit/agent/model/direct.py (moved), hermit/agent/acting/{agent,react,tools,workspace}.py, the Evolver sessions
that set isolation (agent.py, AHE debugger, EvoSafe roles, Harness² channel, MWM, RRSI), hermit/trainer/trainer.py
(adapter_needs_errors), hermit/agent/evolver/loader.py (the adapter needs), hermit/harbor/{runner,tools}.py.
No sandbox change.

Not in this PR (follow-ups in the design doc): one Harbor bridge for every agent; a bwrap executor for Harbor-format
tasks; benchmarks moved to Harbor's task format, with benchmark.yaml's default agent; the robotics changes; the
LIBERO-90 to LIBERO-Pro Long experiment. The benchmark loops that come from a benchmark or a paper (CL-Bench,
ServeLearnBench, OfficeQA's SkillOpt rollout, AgentDyn) stay as they are, conforming to the interfaces: replacing
them with the core ReAct would change what reaches the model, and each needs its own parity check.

Docs and checklists: docs/framework/agent-layers.md (Layer 1's Direct, Environments, the ReAct hooks,
supports and needs, rules 7 and 8, a before/now table), docs/framework/custom-evolver.md, AGENTS.md, the
general PR checklist (an Agent layers item) and the PR template.

No config compatibility is kept, by decision: code that passed workspace=/limits= or isolation settings is
changed here (every caller in hermit/, examples/ and tests/).

Validation

Run on Linux (RHEL 9, x86_64) with bubblewrap, as a non-root user, against main at a6f39ea1.

Requests unchanged. A script records what each kind of agent sends, with a stand-in at the edge: the ReAct
agent's chat requests (a task tool and an adapter), a single Direct call, and the Codex and Claude Code Base Agents
(argv, environment, stdin, the home's and workspace's files; Codex also inside the run's bubblewrap sandbox). Recorded
on main and on this branch after each step: byte for byte identical (paths under the scenario's root normalised).

Real runs, GLM 5.3 Flash on Fireworks, main against this branch (same configs, same data):

Run Version Root adapter Trained adapter Training tokens (rollout / learner)
E1: ReAct acting, ACE learning, 9 sentiment tasks (examples/claude_subscription's), test 3 × 3 repeats main 0/9 0/9 977 / 11,334
this branch 0/9 0/9 962 / 10,916
E2: Codex acting, the built-in Codex Evolver in the run's sandbox, same tasks main 0/9 9/9 28,170 / 99,052
this branch 0/9 9/9 28,218 / 104,405
E3: CL-Bench database, MWM, the first 10 questions of order 0 main 8/10 learner USD 0.30
this branch 8/10 learner USD 0.33

Trajectories: in E1 both versions classify correctly, but ACE's playbook makes the model append bullet_ids after
the JSON, which the strict judge scores 0 (the same on main). In E2 both learners wrote the same rule (exactly
{"label": ...}, with what each label means). E2's and E3's learner sessions ran in the run's sandbox on this branch
(sandbox/learner-1.json: enforced, network isolated, probe passed, the data and judge absent). In E3 every MWM
review was adopted in both versions.

Tests. python -m pytest --changed --base origin/main (shared code: every group, plus the examples' own tests),
in a venv with the optional groups CI installs (.[test,mini-swe-agent], gepa, skillopt, harness2, rrsi, she): 1983
passed, 187 skipped, 0 failed
on b6932693. main (a6f39ea1) in the same venv: 1882 passed, 165 skipped, 0 failed;
the 123 more collected here are the examples' tests (94) and the new ones. The skips are optional CLIs and opt-ins
(the pi CLI, --integration, official AgentDojo, metaworld, clbench, Docker). git merge-tree against main: clean.
The parity script was rerun after every review round: still byte for byte identical to main.

New tests: tests/core/test_environment.py (28: environments and their limits, the CLI's refusal of isolation
settings, Direct in Layer 1, the ReAct hooks and external calls, a ReactAgent subclass with @tool methods,
@tool's schema, the adapter needs on every shipped config, Harbor's supports, an endpoint entered after it started),
and one in test_ahe_debugger.py (each attempt in the run's sandbox). Each review fix's test fails without its fix.

Not verified: macOS; Docker-isolated CLI sessions (no officeqa-codex image here: the container path's code is
unchanged, only where it reads its settings from); the Pi CLI (not installed); Harbor trials.

Review

  • Tests and documentation cover the affected behavior, where applicable.
  • Commands, defaults, and public interfaces are consistent.
  • Agents follow the agent layers.
  • Shared logs and examples contain no credentials or private data.
  • Documentation-only: N/A.
  • Core changes: listed in Change.
  • Implementation correctness and failures: a CLI given isolation as settings is refused, never run
    unisolated; an unknown pending call is refused; a method's need an agent lacks stops the run before it starts.
  • Real execution paths and tests: see Validation (stub-model parity, real GLM 5.3 Flash runs).
  • Independent review: dual review, 5 rounds (this session and a separate Codex gpt-6.1-sol review at high
    effort, each round a fresh session); see Review rounds below.

Review rounds

Round Codex Self Confirmed and fixed Left
1 14 2 15 (one merged) 0
2 9 2 9 + 2 0
3 5 0 5 0
4 3 + 1 minor 0 3 1 (not worth it)
5 0 0
Final test runs 2 2 0

What was fixed (all in b6932693, each with a test):

  • P1: cli_endpoint chose a container only without a log directory, so a Trainer-logged llm.isolation: container
    ran its CLI on the host.
  • Environment.exec keeps its limits: timeout, mounts (a stale link replaced), hidden paths and a limited network
    refused where the sandbox shows them or lets the network through (allowed hosts, a proxy, ports, sockets, a run
    without bubblewrap); a timed-out command's process tree killed, its output drained a few seconds at most; links kept
    as links on upload and download.
  • ReAct external calls: through before_tool / after_tool and the result cut; later calls of the same message wait
    behind them; stopped and the caps judged after the last result.
  • supports: Harbor's by acting agent (answer: context and context tool) from params merged as the runner merges
    them; ServeLearnBench without context_tool; a property counts as undeclared. @tool schemas: unions and list
    items (a provider refuses an array without them).
  • ChatEndpoint.start() idempotent: with cli_endpoint(...) started a second serving loop that outlived close()
    and spun on the closed socket; it slowed the whole test process tenfold and failed two timeout tests in the full
    suite only (the pattern predates this PR).
  • Tests left on the old API (RRSI, mini-swe-agent, AHE, Harness² on Pi), and docs (Direct is Layer 1 everywhere,
    the interactive diagram, what is not implemented yet, the needs check's scope).

A later review (after the dual review) found four more, all fixed in 99ea6f24 with tests that fail without the
fixes: a folder upload wrote through a link already in the workspace; ReactAgent counted its client's totals, so a
subclass's call_model on another client went uncounted; answer() was skipped on the answer-tag follow-up;
Sandboxed.exec could not start in a folder only the sandbox shows. The two moved modules forward for one release,
and CHANGELOG says what changed. Full suite on that tree (CI venv): 1987 passed, 185 skipped, 1 failed
(test_selection: the forwards' hermit/_moved.py was not marked shared), fixed in the same commit and rerun.

Left, with the reason: in a Local environment, a process that detached itself and whose parent already exited
survives a timeout (a subreaper would be needed; Sandboxed is contained by bubblewrap's PID namespace; nothing in
Hermit calls exec yet). The docstring says so.

🤖 Generated with Claude Code

haizhongzheng and others added 3 commits November 19, 2026 02:35
…supports and needs

Where an agent works is now an environment given with each call, never a setting:
hermit/agent/environment/ holds Environment (workspace, limits, isolation; exec, upload,
download, path) and Local, Sandboxed and Container (the old base/container.py is
environment/docker.py). BaseAgent.run(prompt, environment=..., tools, session, roles)
replaces run(prompt, workspace=, limits=); a path is a Local. The CLI Base Agents read
their isolation from the environment and refuse isolation, sandbox or container given as
settings; the isolation code paths themselves are unchanged.

A single model call is Layer 1: Direct moves to hermit/agent/model/direct.py and returns a
Reply; make("direct") is refused with the new place.

react gains hooks (call_model, before_tool, after_tool, stopped) and step-by-step driving
with external tools. ReactAgent is the class a benchmark subclasses: @tool methods (the
schema read from the signature), environment_for, and task hooks.

An acting agent declares what it takes of an adapter (supports); a method lists what it
needs (evolver.yaml needs: MWM tools, ASPIRE and ReHarness context_tool, AHE skills,
agents, hooks); hermit check and training refuse a run whose agent lacks one.

Docs (agent-layers, custom-evolver), AGENTS.md, the general PR checklist and the PR
template say so. No config compatibility is kept: every caller moves to the new API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…alls, capability checks

Five rounds of review (this session and an independent Codex review) on the
one-agent refactor; every confirmed finding fixed, each with a test that fails
without it.

- cli_endpoint chose a container only when no log directory was given: a
  Trainer-logged llm.isolation: container ran its CLI on the host. Placement is
  now chosen apart from logging.
- Environment.exec keeps the environment's limits: its timeout_s by default,
  readable mounts linked (Local, a stale link replaced, the workspace's own file
  refused) or bound (Sandboxed); hidden paths and a limited network refused where
  they cannot be kept (checked against everything the sandbox shows, its allowed
  hosts, proxy, ports and sockets, and a fallback that ran without bubblewrap).
  A timed-out command's whole process tree is killed and its output read for a
  few seconds at most. upload and download keep links as links.
- ReAct's external calls go through before_tool, and React.give applies
  tool_result_chars and after_tool; the calls after an external one wait behind
  it, and the turn's ends (stopped, the caps) are judged after the last result.
- supports: Harbor's from its acting agent (a CLI all, the answer agent its
  context and context tool, Harbor's own none), read from params merged as the
  runner merges them; ServeLearnBench takes no context tool; a supports that is
  a property counts as undeclared. A test walks every shipped config of a method
  with adapter needs.
- @tool schemas: runtime and postponed unions, and a list's items.
- ChatEndpoint.start() is idempotent: cli_endpoint returns a started endpoint
  that callers enter with `with`, which started a second serving loop; it
  outlived close() and spun on the closed socket, slowing the whole process
  (found as two timeout tests failing only in the full suite).
- Tests that still passed isolation as settings (RRSI, mini-swe-agent, AHE)
  or a stand-in with the old run(prompt, workspace) (Harness² on Pi).
- Docs: Direct is Layer 1 everywhere (the interactive diagram too), the
  Environments section says what is not implemented yet, the needs check's scope.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…, sandbox cwd, moved paths

- Environment.upload of a folder wrote through a link the workspace already
  had (a file outside the workspace was overwritten): entries are copied one
  by one and a link at a target is replaced, never written through.
- ReactAgent counted its client's totals: a subclass whose call_model uses
  another client had those calls left out of tokens, cost and the budget.
  The run's usage is now what its loop recorded, plus the answer-tag
  follow-up.
- The answer tag was looked for in answer()'s output, so a subclass that
  strips it triggered a follow-up whose reply skipped answer(): the tag is
  looked for in the model's reply, and answer() shapes the follow-up too.
- Sandboxed.exec started bubblewrap in the command's cwd on this machine,
  which fails for a folder only the sandbox shows (a mount's target): it
  starts from the workspace and sets the cwd inside.
- hermit.agent.base.direct and hermit.agent.base.container forward to their
  new modules for one release (hermit._moved, as earlier moves did);
  CHANGELOG's Breaking section and the agent-layers table say what changed.
- Docs: docker/README.md's module path, cli.py's "Local", the needs check's
  message for an agent that takes no adapter export.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@haizhongzheng
haizhongzheng merged commit 127a1f8 into main Nov 19, 2026
1 check passed
@haizhongzheng
haizhongzheng deleted the agent-environment-refactor branch November 20, 2026 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0