tzteyang ad2f054dd4
feat(*): add an experimental Curator that cultivates harness-of-harnesses workers (#802)
## Summary

Adds `experimental/`, a first version of worker RSI: a Curator that
shapes a
Raven worker's harness from the task, the materials handed to it and
feedback,
the way a new employee is trained.

The worker is a harness-of-harnesses. A root Raven harness faces the
user and
calls child harnesses, each a full Raven agent hosted over ACP, through
the
nodes of its playbooks. Every harness has the same four strategy facets
(memory, planning, capability, action). The Curator reads the actual
harnesses
and generates code and config on those facets at both levels: the root
candidate sets the playbooks and writes per-node requirements, each
child is
curated against the requirements of its nodes, and the set is checked on
copies and activated together.

Layout:
- `curator/`: staged generation (understand, select, design, implement,
repair), two-level composition, harness contracts, and `raven_adapter/`,
  the only layer that touches a worker's Raven runtime
- `analyst/`: turns an evaluator's words and execution records into
  observable behavior requirements
- `iteration/`: the generic loop (onboarding curation, then trial,
review and
  curation each round) and a CLI for a human evaluator
- `simulation/`: a simulated travel agency that runs the loop, plus one
  showcase case (`simulation/cases/s0925c/`: case notes, the exported
  transcript split by phase, and the round 1 and round 3 decks)
- `docs/design.md` (structure, curation flow, composition sequence, the
Raven
  connection) and `docs/rsi-iteration.md` (the loop and the Analyst)

Key decisions:
- No change under `raven/`, and `raven/` does not import
`experimental/`.
Generated artifacts land on Raven's existing extension points. The few
Raven
internals the adapter reads are listed in `docs/design.md` section 5.4.
- The Curator explores a read-only source snapshot; the caller names the
evaluation side to withhold from it, so the generic layers name no
scenario.
- The single-layer owner analysis is kept only to reproduce the showcase
case
  and is marked for removal; the two-layer Analyst is the default.
- The two decks are `.pptx` files of about 230 KB each. They pass
  `check-large-files`; flagging them in case they count as report assets
under AGENTS.md section 7, in which case they can move out of git behind
a
  link.
- One follow-up for Raven 0.2.2: playbooks in prompt and rounds modes
ship no
  fixed nodes, so node requirements are read from dag playbooks only.

## Type

- [ ] Fix
- [x] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

On the branch rebased onto the latest `main`:

- `uv run pytest tests/test_harness_curator_*.py tests/test_analyst_*.py
tests/test_iteration_*.py tests/test_simulation_*.py -q`: 368 passed
- `uv run pytest -m integration
tests/integration/test_harness_curator_*.py --deselect
tests/integration/test_harness_curator_real_llm.py -q`
  (offline, stub providers): 57 passed
- `uv run pytest -q` (whole suite, with the host proxy variables unset
and
`PYTHONTZPATH` set): 26692 passed, 2 failed. One was an order-dependent
test
  of this PR and is fixed here; the other,

`tests/test_subagent_node_runtime.py::test_what_cannot_be_read_names_nothing`,
  fails only because the machine runs as root and is unrelated.
- `uv run ruff check` and `uv run ruff format --check` on the touched
files:
  clean
- `scripts/check_large_files.py origin/main..HEAD`,
  `scripts/check_source_language.py origin/main..HEAD`,
  `scripts/check_commit_messages.py origin/main..HEAD` and
  `npx commitlint --from origin/main --to HEAD`: all exit 0

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [x] User-facing docs or screenshots are updated when needed

## Risk

No user-visible change for Raven: `experimental/` is not imported by
`raven/` and is not packaged in the wheel (`packages = ["raven"]`).
`CONTEXT.md` gains one glossary entry. The Curator's exploration offers
a
shell only under an OS sandbox executor. Rollback is a revert of the
squash
commit; nothing else depends on `experimental/`.

- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes

## Related Issues

N/A

---------

Co-authored-by: Yuqi Yang <yiqiyang524@gmail.com>
Co-authored-by: Claude (claude-opus-5-5) <noreply@anthropic.com>
2026-09-26 21:00:58 +08:00

332 lines
15 KiB
Python

"""Whether a run's employee stayed inside its own run: a scan of every tool call recorded under the run.
The host cannot confine child harnesses to their folders (the container allows no mount namespaces), so a run is
checked after the fact. Every tool call the employee or any child harness made is read from each observation log
under the run (runner tool events, and the assistant tool calls carried by provider requests and responses) and
searched for paths. A call breaches isolation when a path in it reaches what the employee must not see:
- `simulation`: this repository's `experimental/` tree (scenarios, drill cards, notes, the loop's own code);
- `loop_records`: what the loop keeps about the employee beside its home (reviews, analyses, curations, references,
settings, logs);
- `other_drill`: another drill's replica;
- `other_run`: another run's records or state;
- `forbidden`: a place the caller names (`forbid`), such as a scratch area holding scenario copies.
A search (`find`, `grep -r`, `ls -R` and the like, or a searching tool) breaches too when its root holds any of those
places (`find /`), whatever it found: `broad_search`. Path arguments are resolved as the file tools resolve them
(relative to the drill's working directory) and symlinks to their targets.
The Curator's exploration calls (kept in each curation's trace) are checked too: every path must stay in its
exploration workspace (`curator_outside`). The Curator's file tools refuse anything else and it gets no shell
without an OS sandbox, so this is a second line, as the scan is for the employee.
A call its tool refused is kept as an attempt; any other breach makes the run invalid. A run with no log or no tool
call to check is unverified (`valid` None), never valid.
"""
import argparse
import json
import os
import re
import shlex
from pathlib import Path
from ..analyst.activity import scoped
from .employee import REPLICAS
LOG = "observations.jsonl"
RESULT = "isolation.json"
LOOP_RECORDS = frozenset(
{
"iteration",
"analysis",
"curation",
"record",
"references.jsonl",
"value.json",
"settings.json",
"command.json",
"suite-summary.json",
"run.log",
RESULT,
}
)
SEARCHERS = re.compile(r"(?:^|[\s;&|(`])(?:find|grep|egrep|rg|ag|ls|tree|du|locate|fd)\s")
CURATOR_TOOLS = frozenset({"read_file", "list_dir", "grep", "find", "exec"})
WORKSPACE_PREFIX = "raven-curator-explore-"
SEARCH_TOOLS = frozenset({"list_dir", "grep", "glob", "find", "file_search", "tree_walk", "search_files"})
# A slash after a glob character is a pattern segment ("**/workdir*.py"), not the start of an absolute path.
PATH = re.compile(r"(?<![\w.~$*?\]}-])(/[\w.@+~%-]+(?:/[\w.@+~%-]*)*)")
KIND = re.compile(r'"kind": ?"([^"]+)"')
REFUSED = re.compile(
r"outside (?:the )?allowed director|outside (?:the )?working dir|blocked by safety guard|permission denied", re.I
)
PATH_KEYS = frozenset(
{"path", "paths", "file", "file_path", "filepath", "files", "directory", "dir", "target", "source", "destination"}
)
EXPERIMENTAL = Path(__file__).resolve().parents[1]
HOME = Path.home()
EXCERPT = 300
KEPT = 200
def _norm(path: str) -> Path:
return Path(os.path.normpath(path))
def _inside(path: Path, root: Path) -> bool:
return path == root or root in path.parents
def calls(log: Path):
"""(tool name, arguments text, refused) of every tool call a log records, each call once.
`refused` is True when the tool's own answer says it refused the path (outside its allowed directories, a
safety guard), False when it answered otherwise, and None when no answer was recorded.
"""
found: dict[str, list] = {}
answered: dict[str, bool] = {}
with log.open(errors="replace") as handle:
for line in handle:
match = KIND.search(line, 0, 400)
kind = match.group(1) if match else ""
if kind not in ("runner.event", "provider.request", "provider.response", "child.execution"):
continue
try:
row = json.loads(line)
except ValueError:
continue
for _, inner in scoped([row]) if isinstance(row, dict) else ():
_collect(inner, found, answered)
for key, (name, arguments) in found.items():
yield name, arguments, answered.get(key)
def _collect(row: dict, found: dict, answered: dict) -> None:
kind = row.get("kind")
if kind == "runner.event" and row.get("event_type") == "ToolEvent":
event = row.get("event") or {}
key = event.get("tool_call_id") or json.dumps(event.get("arguments"), sort_keys=True, default=str)
if event.get("phase") == "start":
found.setdefault(key, [str(event.get("name") or ""), json.dumps(event.get("arguments") or {})])
elif event.get("phase") == "complete":
answered[key] = bool(REFUSED.search(str(event.get("result_preview") or "")[:2000]))
return
if kind == "provider.request":
messages = [m for m in (row.get("parameters") or {}).get("messages") or [] if isinstance(m, dict)]
requested = [call for message in messages for call in message.get("tool_calls") or []]
for message in messages:
if message.get("role") == "tool" and message.get("tool_call_id") not in answered:
content = message.get("content")
text = content if isinstance(content, str) else json.dumps(content, ensure_ascii=False)
answered[message["tool_call_id"]] = bool(REFUSED.search(text[:2000]))
elif kind == "provider.response":
requested = list((row.get("response") or row).get("tool_calls") or [])
else:
return
for call in requested:
if not isinstance(call, dict):
continue
function = call.get("function") or call
arguments = function.get("arguments")
arguments = arguments if isinstance(arguments, str) else json.dumps(arguments or {}, ensure_ascii=False)
found.setdefault(
call.get("id") or f"{function.get('name')}:{arguments}", [str(function.get("name") or ""), arguments]
)
def _command(arguments: str) -> str:
try:
data = json.loads(arguments)
except ValueError:
return ""
return str(data.get("command") or data.get("cmd") or "") if isinstance(data, dict) else ""
def _named(value, key=None):
"""Every string an argument gives as a path: under a path-like key, or in a list of files."""
if isinstance(value, dict):
for name, item in value.items():
yield from _named(item, name)
elif isinstance(value, list):
for item in value:
yield from _named(item, key)
elif isinstance(value, str) and key in PATH_KEYS and value.strip():
yield value.strip()
def _paths(arguments: str, workdir: Path) -> list[Path]:
"""Paths an argument text reaches: absolute paths anywhere in it, path arguments resolved against the drill's
working directory (as the file tools resolve them), command words (`/`, `~`, `..`), and each symlink's target."""
found = [_norm(match) for match in PATH.findall(arguments)]
try:
data = json.loads(arguments)
except ValueError:
data = None
for value in _named(data):
expanded = str(HOME) + value[1:] if value.startswith("~") else value
found.append(_norm(expanded if expanded.startswith("/") else str(workdir / expanded)))
command = _command(arguments)
if command:
try:
words = shlex.split(command)
except ValueError:
words = command.split()
for word in words:
if word in ("/", "/*"):
found.append(Path("/"))
elif word in ("~", "$HOME") or word.startswith(("~/", "$HOME/")):
found.append(_norm(str(HOME) + word.split("~", 1)[-1].removeprefix("$HOME")))
elif ".." in Path(word).parts and not word.startswith("/"):
found.append(_norm(str(workdir / word)))
resolved = [Path(os.path.realpath(path)) for path in found if _present(path)]
return list(dict.fromkeys([*found, *resolved]))
def _present(path: Path) -> bool:
"""Whether a candidate exists; a script pasted as an argument can form a name the file system refuses."""
try:
return path.is_symlink() or path.exists()
except OSError:
return False
class Bounds:
"""What a drill may reach: its own replica (or, for the primary, the run's records and state) and anything
outside the closed places; `why` names the breach a path makes, `holds` whether a search root reaches one."""
def __init__(self, records: Path, state: Path | None, forbid=()):
self.records, self.state = records.resolve(), state.resolve() if state else None
self.replicas = self.records / REPLICAS
self.loop = [self.records / name for name in LOOP_RECORDS]
if self.state:
self.loop += [self.state / "run.log", self.state / "command.json"]
self.forbid = [Path(path).resolve() for path in forbid]
self.runs = [self.records.parent, *([self.state.parent] if self.state else [])]
def own(self, log: Path) -> Path:
relative = log.resolve().relative_to(self.records)
return self.replicas / relative.parts[1] if relative.parts[0] == REPLICAS else self.records
def why(self, path: Path, own: Path) -> str | None:
if any(_inside(path, place) for place in self.loop):
return "loop_records"
if _inside(path, self.replicas) and not _inside(path, own):
return "other_drill"
if _inside(path, self.records) or (self.state and _inside(path, self.state)):
return None
if _inside(path, EXPERIMENTAL):
return "simulation"
if any(_inside(path, place) for place in self.forbid):
return "forbidden"
if any(_inside(path, place) for place in self.runs):
return "other_run"
return None
def holds(self, root: Path, own: Path) -> bool:
places = [*self.loop, EXPERIMENTAL, *self.forbid, *self.runs]
places += [self.replicas] if own != self.records else [path for path in self._drills()]
return any(_inside(place, root) for place in places)
def _drills(self) -> list[Path]:
return sorted(self.replicas.iterdir()) if self.replicas.is_dir() else []
def curator_calls(records: Path):
"""(tool, arguments text, refused, trace file) of every exploration call the Curator made in its curations."""
for trace in sorted(Path(records).glob("curation/**/generation.json")):
try:
events = json.loads(trace.read_text()).get("trace") or []
except (OSError, ValueError):
continue
for event in events:
if isinstance(event, dict) and event.get("tool") in CURATOR_TOOLS and "arguments" in event:
result = event.get("result") or {}
refused = bool(result.get("failed") or result.get("error")) if isinstance(result, dict) else None
yield str(event["tool"]), json.dumps(event["arguments"], ensure_ascii=False), refused, trace
def _curator_outside(path: Path) -> bool:
if path.parts[:2] == ("/", "dev"):
return False
return not any(part.startswith(WORKSPACE_PREFIX) for part in path.parts)
def scan(records: Path, state: Path | None = None, forbid=()) -> dict:
"""Every breach in the run's logs and the Curator's exploration (see the module docstring).
`valid` is False when some call reached a closed place and was not refused, None when there was nothing to
check (no employee log, or no tool call at all), and True otherwise; refused calls are kept as `attempts`.
"""
records = Path(records)
bounds = Bounds(records, Path(state) if state else None, forbid)
findings, logs, checked, keys = [], 0, 0, set()
def note(kind, path, name, where, arguments, refused):
key = (kind, str(path), name, bool(refused))
if key not in keys:
keys.add(key)
findings.append(
{
"kind": kind,
"path": str(path),
"tool": name,
"log": where,
"refused": refused,
"call": arguments[:EXCERPT],
}
)
for log in sorted(records.rglob(LOG)):
logs += 1
own = bounds.own(log)
workdir = own / "workdir" if (own / "workdir").is_dir() else (bounds.state or own) / "workdir"
for name, arguments, refused in calls(log):
checked += 1
searching = name in SEARCH_TOOLS or bool(SEARCHERS.search(_command(arguments)))
for path in _paths(arguments, workdir):
kind = bounds.why(path, own)
if kind is None and searching and bounds.holds(path, own):
kind = "broad_search"
if kind:
note(kind, path, name, str(log.resolve().relative_to(bounds.records)), arguments, refused)
for name, arguments, refused, trace in curator_calls(records):
checked += 1
workspace = Path("/", WORKSPACE_PREFIX + "workspace")
for path in _paths(arguments, workspace):
if _curator_outside(path):
note("curator_outside", path, name, str(trace.relative_to(records)), arguments, refused)
breaches = [finding for finding in findings if finding["refused"] is not True]
valid = False if breaches else (None if not logs or not checked else True)
return {
"valid": valid,
"breaches": len(breaches),
"attempts": len(findings) - len(breaches),
"logs": logs,
"calls": checked,
"findings": findings[:KEPT],
}
def write(records: Path, state: Path | None = None, forbid=()) -> dict:
result = scan(records, state, forbid)
(Path(records) / RESULT).write_text(json.dumps(result, ensure_ascii=False, indent=2))
return result
def cli():
parser = argparse.ArgumentParser(description="Check that a run's employee stayed inside its own run.")
parser.add_argument("records", type=Path)
parser.add_argument("--state", type=Path, help="The run's state folder (its Raven home and workdir)")
parser.add_argument("--forbid", type=Path, nargs="*", default=[], help="Further places the employee must not reach")
parser.add_argument("--write", action="store_true", help=f"Also write {RESULT} into the records")
args = parser.parse_args()
result = (write if args.write else scan)(args.records, args.state, args.forbid)
print(json.dumps({key: value for key, value in result.items() if key != "findings"}, ensure_ascii=False))
for finding in result["findings"][:40]:
print(f"{finding['kind']:>13} {finding['tool']:<12} {finding['path']} ({finding['log']})")
if __name__ == "__main__":
cli()