Implement the harness contract and prove it against rein-aharness (GLAS-WP-0001-T02/T03/T04)
All checks were successful
CI Smoke / host-smoke (push) Successful in 1s
CI Smoke / container-smoke (push) Successful in 3s

- src/glas_harness/contract.py: Rein ABC (start_session/dispatch_tool/
  end_session), SandboxHandle/ToolCall/ToolResult, per docs/harness-contract.md.
- reins/rein_aharness.py: adapter around the rein-aharness CLI. Collapses
  a whole `agent-harness run` into one dispatch_tool call for now (no
  per-tool hooks yet — tracked in rein-aharness HARNESS-WP-0002-T03);
  verifies success via new-commit detection, mirroring rein-aharness's
  own success signal.
- gateway.py + cli.py: resolves a sand-boxer sandbox, runs one task
  through a rein, tears the sandbox down.
- registry/reins/*.yaml (rein-aharness implemented, rein-openweights
  planned) and profiles/harness.agent-dev{,-local}.yaml, pairing with
  sand-boxer's profile.agent-dev and the new profile.bwrap-local.

Tested against a real local git repo + mocked SandboxManager/CLI
subprocess (10 tests, all passing). A real live run against the actual
Claude Code CLI is deliberately left for a human-triggered follow-up —
not executed autonomously since it spends real API credits/credentials.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
tegwick 2026-07-26 13:02:59 +02:00
parent 97a50fc780
commit 5681fce787
16 changed files with 556 additions and 4 deletions

View file

42
src/glas_harness/cli.py Normal file
View file

@ -0,0 +1,42 @@
"""glas-harness CLI — minimal gateway invocation (GLAS-WP-0001-T04)."""
from __future__ import annotations
import argparse
import json
import sys
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(prog="glas-harness")
sub = parser.add_subparsers(dest="command", required=True)
run = sub.add_parser("run", help="Run one task through a rein inside a sand-boxer sandbox")
run.add_argument("--sandbox-profile", required=True, help="e.g. profile.bwrap-local")
run.add_argument("--repo", required=True, help="Local repo path to mirror into the sandbox")
run.add_argument("--title", required=True)
run.add_argument("--description", required=True)
run.add_argument("--actor", default="agt")
run.add_argument("--project", default="glas-harness")
args = parser.parse_args(argv)
if args.command == "run":
from glas_harness.gateway import run_task_through_rein
result = run_task_through_rein(
sandbox_profile=args.sandbox_profile,
repo=args.repo,
title=args.title,
description=args.description,
actor=args.actor,
project=args.project,
)
print(json.dumps(result, indent=2))
return 0 if result["tool_ok"] else 1
return 1
if __name__ == "__main__":
sys.exit(main())

View file

@ -0,0 +1,54 @@
"""The harness contract a concrete rein implements.
See docs/harness-contract.md for the full session-lifecycle diagram and
GLAS-WP-0001-T01. glas-harness owns the outer loop (profile resolution,
sandbox request/teardown via sand-boxer, State Hub reporting, actor
attribution); a rein owns the inner agentic loop.
"""
from __future__ import annotations
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import Any
@dataclass
class SandboxHandle:
"""What glas-harness got back from sand-boxer's create()."""
sandbox_id: str
host: str
reachability: dict[str, Any] = field(default_factory=dict)
@dataclass
class ToolCall:
name: str
args: dict[str, Any] = field(default_factory=dict)
actor: str = "agt"
@dataclass
class ToolResult:
ok: bool
output: str = ""
error: str | None = None
class Rein(ABC):
"""Base class for concrete harness backends (reins)."""
@abstractmethod
def start_session(
self, profile: dict[str, Any], inputs: dict[str, str], sandbox: SandboxHandle
) -> dict[str, str]:
"""Begin an agent session bound to a sandbox. Returns a session handle."""
@abstractmethod
def dispatch_tool(self, session: dict[str, str], tool_call: ToolCall) -> ToolResult:
"""Run one tool call under the session's policy."""
@abstractmethod
def end_session(self, session: dict[str, str]) -> dict[str, str]:
"""Close the session. Returns a summary (commit sha, outcome, ...)."""

View file

@ -0,0 +1,72 @@
"""Minimal gateway proving the harness contract against rein-aharness.
GLAS-WP-0001-T04: resolve a sand-boxer profile, request a sandbox,
dispatch one rein-aharness task through the Rein contract, verify a
commit landed, tear the sandbox down. This is the parity proof gating
any later "retire rein-aharness as a standalone concern" conversation
it is not itself that conversation.
Requires the `sandbox` extra (sand-boxer installed as a sibling
editable dependency).
"""
from __future__ import annotations
from typing import Any
from sandboxer.core.manager import SandboxManager
from sandboxer.models import Consumer, SandboxCreateRequest
from glas_harness.contract import Rein, SandboxHandle, ToolCall
from glas_harness.reins.rein_aharness import ReinAharness
def run_task_through_rein(
*,
sandbox_profile: str,
repo: str,
title: str,
description: str,
rein: Rein | None = None,
actor: str = "agt",
project: str = "glas-harness",
manager: SandboxManager | None = None,
) -> dict[str, Any]:
"""Resolve `sandbox_profile`, run one task inside it via `rein`, verify, tear down.
Defaults to `ReinAharness` when no rein is supplied the only
implemented rein as of GLAS-WP-0001-T04. `rein-openweights` plugs in
the same way once REIN-OW-WP-0001 lands.
"""
manager = manager or SandboxManager()
rein = rein or ReinAharness()
request = SandboxCreateRequest(
profile=sandbox_profile,
inputs={"repo": repo},
consumer=Consumer(actor=actor, project=project),
)
status = manager.create(request)
try:
reachability = status.reachability.model_dump(mode="json") if status.reachability else {}
sandbox = SandboxHandle(
sandbox_id=status.sandbox_id, host=status.host or "", reachability=reachability
)
session = rein.start_session(
profile={"id": sandbox_profile},
inputs={"title": title, "description": description},
sandbox=sandbox,
)
tool_result = rein.dispatch_tool(session, ToolCall(name="run_task", actor=actor))
summary = rein.end_session(session)
return {
"sandbox_id": status.sandbox_id,
"tool_ok": tool_result.ok,
"tool_output": tool_result.output,
"tool_error": tool_result.error,
"summary": summary,
}
finally:
manager.destroy(status.sandbox_id)

View file

View file

@ -0,0 +1,102 @@
"""ReinAharness — glas-harness adapter around the rein-aharness CLI.
rein-aharness's `run` command performs an entire bounded agentic session
(persona load, Claude Code CLI subprocess, commit verification) as one
opaque unit it does not yet expose per-tool-call hooks from outside
(that refactor is tracked in
rein-aharness/workplans/HARNESS-WP-0002-T03). Until then, dispatch_tool
collapses the whole run into a single call rather than true per-tool
granularity; this adapter proves the contract's *shape* (start/dispatch/
end + sandbox handoff), not fine-grained tool interception yet.
"""
from __future__ import annotations
import json
import shutil
import subprocess
import tempfile
from pathlib import Path
from typing import Any
from glas_harness.contract import Rein, SandboxHandle, ToolCall, ToolResult
class ReinAharnessNotInstalled(RuntimeError):
pass
class ReinAharness(Rein):
def __init__(self, cli_bin: str = "agent-harness") -> None:
self.cli_bin = cli_bin
def _bin(self) -> str:
resolved = shutil.which(self.cli_bin)
if not resolved:
raise ReinAharnessNotInstalled(
f"'{self.cli_bin}' not found on PATH — install rein-aharness first"
)
return resolved
def start_session(
self, profile: dict[str, Any], inputs: dict[str, str], sandbox: SandboxHandle
) -> dict[str, str]:
target_repo = (
inputs.get("target_repo")
or sandbox.reachability.get("workspace_dir")
or sandbox.reachability.get("remote_dir")
)
if not target_repo:
raise ValueError("no target_repo resolvable from inputs or sandbox reachability")
task_file = inputs.get("task_file")
if not task_file:
task_spec = {
"title": inputs["title"],
"description": inputs["description"],
"target_repo": target_repo,
"agent": inputs.get("agent", "coach"),
}
fd = tempfile.NamedTemporaryFile(
mode="w", suffix=".json", prefix="glas-harness-task-", delete=False
)
json.dump(task_spec, fd)
fd.close()
task_file = fd.name
return {
"sandbox_id": sandbox.sandbox_id,
"task_file": task_file,
"target_repo": target_repo,
"head_before": self._git_head(target_repo),
}
def dispatch_tool(self, session: dict[str, str], tool_call: ToolCall) -> ToolResult:
argv = [
self._bin(),
"run",
"--task-file",
session["task_file"],
"--no-hub",
"--no-metrics",
]
proc = subprocess.run(argv, capture_output=True, text=True)
ok = proc.returncode == 0
return ToolResult(ok=ok, output=proc.stdout, error=None if ok else proc.stderr)
def end_session(self, session: dict[str, str]) -> dict[str, str]:
head_after = self._git_head(session["target_repo"])
committed = bool(head_after) and head_after != session.get("head_before")
return {
"commit_sha": head_after or "",
"committed": str(committed),
}
@staticmethod
def _git_head(repo: str) -> str:
proc = subprocess.run(
["git", "-C", str(Path(repo).expanduser()), "rev-parse", "HEAD"],
capture_output=True,
text=True,
)
return proc.stdout.strip() if proc.returncode == 0 else ""