ReMe/plugins/home/tests/test_code.py
xyf2020 fa6d8fcef7
Some checks are pending
CI / macOS sandbox / Seatbelt - clt (push) Waiting to run
CI / macOS sandbox / Seatbelt - xcode (push) Waiting to run
CI / Python packages / Build and verify distributions (push) Waiting to run
CI / Python quality / GitHub Actions (push) Waiting to run
CI / Python quality / Pre-commit (push) Waiting to run
CI / Python tests / Unit Tests - py3.11 (push) Waiting to run
CI / Python tests / Unit Tests - py3.12 (push) Waiting to run
CI / Python tests / Unit Tests - py3.13 (push) Waiting to run
CI / Python tests / Unit Tests - py3.14 (push) Waiting to run
CI / ReMe Studio / Studio checks (push) Waiting to run
CI / Windows / CLI smoke - py3.11 (push) Waiting to run
Deploy / Documentation / Build documentation (push) Waiting to run
Deploy / Documentation / deploy (push) Blocked by required conditions
CI and Release / Docker / Build and test / amd64 (push) Waiting to run
CI and Release / Docker / Build and test / arm64 (push) Waiting to run
CI and Release / Docker / Publish multi-platform tags (push) Blocked by required conditions
Security / CodeQL / Analyze javascript-typescript (push) Waiting to run
Security / CodeQL / Analyze python (push) Waiting to run
feat(home): add reproducible benchmark preparation and candidate evaluation (#693)
* Add sandboxed Python execution and home AutoBash plugin

* Harden read-only shell commands and filesystem sandboxes

* Group file sandbox backends in one package

* Fix macOS file sandbox isolation and runtime access

* Refine file sandbox limits and executor checks

* Harden AutoBash policy and macOS sandbox supervision

* fix(home): require ReMe release with file sandbox

* refactor(home): simplify sandbox calls and redundant tests

* fix(execution): bound output and clean up cancelled sandbox startup

* fix(tests): mock Seatbelt platform requirements on Linux

* fix(home): block Git help helpers and harden sandbox cleanup

* Fix sandbox runtime links and subprocess cleanup

* ci: cover Home plugin tests

* test: wait for pipeline descendants to exit after cancellation

* fix: align Home dependencies and preserve cleanup cancellation

Require ReMe 0.4.2.1 and verify isolated Home wheel installation in package CI. Share cancellation-safe cleanup with thread calls, retain AnyIO cancellation semantics, and record Bubblewrap mount targets during argument construction.

* feat(home): prepare self-evolution workspaces and benchmark inputs

* fix(home): load only selected cases for debug derivation

* feat(home): evaluate candidates with benchmark judges and prepare workspace docs

* refactor(home): organize workflows and unify debug evaluation mode

* fix(home): align Docker runtime and preserve evaluation failures

* refactor(home): align evaluation summaries with dev_meta

* feat(home): migrate agent, sampling and evidence inspection tooling

* feat(home): add case-study agent and query evidence tracing tools

- Add home_case_study job with CaseStudyAgentWrapper and a read-only
  sandboxed investigation step for per-case diagnostic reports
- Add home_trace_query_evidence job/step to locate a query's source
  evidence sessions, memory checkpoints and answer/score previews
- Compact evaluation summaries: record per-query failure errors, round
  scores to four decimals, expose scoring_enabled and observed means
- Slim run manifests: drop schema_version and trusted_judge fingerprint,
  exclude debug/sampling_policy options, keep construction_source only
  for query-only replay
- Simplify history index internals and update guide docs and tests

* feat(home): support multi-case case-study runs with dedicated LLM and Git sandbox

- Allow home_case_study to investigate up to eight ordered cases per
  invocation with a shared focus, one isolated agent per case, while
  keeping the legacy single-case selector as a deprecated alias
- Configure a dedicated home_case_study LLM component instead of the
  shared judge backend
- Wrap ReMe's OS file sandbox with a Home-specific Git runtime grant
  (developer directory on macOS) and argv-based child environment
- Tighten case-study prompts around evidence certainty, code-tool path
  scoping, citation form, and unresolved-cause reporting
- Extend evidence tracing and expand case-study/sandbox tests and docs

* feat(home): run case studies with bounded concurrency and fix sandbox test

* feat(home): guard repeated evaluations and preserve retry selections

* feat(home): migrate optimization workflows and evaluation budgets

* feat(home): add candidate admission gate and construction-equivalence replay

Add host-side candidate gate agent that reviews committed changes before
sampling, and construction-equivalence agent that compares a candidate
against its nearest reusable ancestor to select query-only replay with
60% probability when construction is unchanged. Wire both into
plugin.yaml application defaults, update optimizer prompt to explain the
new host behaviors, extend snapshot/evaluation components with reuse
support, and add focused tests.

* feat(home): add structured-report agent and gate/equivalence test coverage

Add StructuredReportAgent wrapper selectable via the
_structured_report_model option in BoundedAgentWrapper, plus its
dedicated unit tests. Extend candidate gate, construction equivalence,
evaluation history, sampling, and validation budget tests, and document
the new behavior in the plugin README.

* feat(home): add standalone benchmark test evaluation and fix compression recovery

* fix(home): preserve top-level answer failure classification

* refactor(home): rename max_validations to max_evaluations

* refactor(home): extract shared review_report workflow helper and bump version to 0.4.2.2

* fix(home): load runner credentials and localize dataset evaluation

* fix(benchmark): propagate Home sandbox memory limits

* refactor(home): consolidate Git exports and candidate validation

* fix(home): clarify branch and diff tool parameter contracts

* feat(home): bound case-study reports and migrate draft tools

* fix(home): align benchmark report limits with plugin defaults

* Fix Home evaluation recovery and cancellation boundaries

* fix(home): tolerate fenced optimizer results and finalize exhausted budgets

* fix(benchmark): align Docker runtime with AgentScope 2.0.11

* fix(home): unify history writes and cancellation cleanup

* refactor(home): reuse structured report parsing

* ci: remove redundant Home compatibility install check

* fix(home): preserve literal formatting in structured reports

---------

Co-authored-by: jinli.yl <jinli.yl@alibaba-inc.com>
2026-11-05 14:54:31 +08:00

355 lines
15 KiB
Python

"""Candidate version Jobs exercised against real isolated Git repositories."""
# pylint: disable=missing-function-docstring,protected-access,redefined-outer-name,unused-argument
import asyncio
from pathlib import Path
import subprocess
from threading import Event
import pytest
import pytest_asyncio
from reme_home.code import snapshot
from reme_home.code.models import ARGUMENTS
from reme_home.workspace import _initialization_lock
from test_preparation import ( # noqa: F401 # pylint: disable=unused-import
home_application,
preparation_inputs,
command,
)
from reme.components.agent_wrapper.as_agent_wrapper import AsAgentWrapper
@pytest_asyncio.fixture
async def prepared(app, inputs):
spec, _ = inputs
response = await app.run_job("home_prepare", spec=spec)
assert response.success, response.answer
repo = Path(app.config.workspace_dir) / "home/code/repo"
return app, repo, spec
async def run(app, job, **kwargs):
response = await app.run_job(job, **kwargs)
assert response.success, response.answer
return response.answer
async def create(app, name="candidate-branch"):
return await run(app, "home_create_candidate_branch", branch_name=name)
async def commit(app, repo, text="value = 2\n"):
(repo / "plugins/beam/src/reme_beam/__init__.py").write_text(text)
return await run(app, "home_commit_candidate", message="Improve candidate")
@pytest.mark.asyncio
async def test_branch_commit_diff_and_immutable_read(prepared):
app, repo, _ = prepared
initial = await run(app, "home_list_candidate_branches")
assert initial["current_branch"] == "init"
assert initial["candidate_branches"] == []
created = await create(app)
assert created["candidate_branch_count"] == 1
result = await commit(app, repo)
assert result["commit_sha"] != initial["commit_sha"]
assert (await run(app, "home_code_status"))["clean"]
diff = await run(app, "home_code_diff", base_revision="init", revision=result["commit_sha"])
assert "+value = 2" in diff["content"]
(repo / "plugins/beam/src/reme_beam/__init__.py").write_text("dirty = 3\n")
status = await run(app, "home_code_status")
assert status["changes"] == [{"status": " M", "path": "plugins/beam/src/reme_beam/__init__.py"}]
assert not status["clean"]
assert "+dirty = 3" in (await run(app, "home_code_diff"))["content"]
assert "+dirty = 3" in (await run(app, "home_code_diff", base_revision="init"))["content"]
rejected = await app.run_job("home_code_diff", revision=result["commit_sha"])
assert not rejected.success and "revision requires base_revision" in str(rejected.answer)
read = await run(
app,
"home_read_code_file",
path="plugins/beam/src/reme_beam/__init__.py",
revision=initial["commit_sha"],
)
assert read["content"] == "value = 1\n"
with pytest.raises(ValueError, match="clean"):
await app.context.components["home.code"]["default"].run("switch_branch", branch_name="init")
@pytest.mark.asyncio
async def test_host_pinned_job_tool_and_schema(prepared):
app, repo, _ = prepared
initial = command(repo, "rev-parse", "HEAD")
await create(app)
await commit(app, repo)
job = app.context.jobs["home_read_code_file"]
tool = AsAgentWrapper._make_tool(job, injected_job_kwargs={"revision": initial})
assert "revision" not in tool.input_schema["properties"]
# Test the actual wrapper's merge contract through its executable FunctionTool.
response = await tool(path="plugins/beam/src/reme_beam/__init__.py")
assert "value = 1" in response.content[0].text
assert "value = 2" not in response.content[0].text
with pytest.raises(ValueError, match="injected"):
await tool(path="plugins/beam/src/reme_beam/__init__.py", revision="HEAD")
assert all(model.model_json_schema()["additionalProperties"] is False for model in ARGUMENTS.values())
@pytest.mark.asyncio
@pytest.mark.parametrize(
"operation,job_name",
[
("create_branch", "home_create_candidate_branch"),
("switch_branch", "home_switch_candidate_branch"),
("merge_branch", "home_merge_candidate_branch"),
("diff", "home_code_diff"),
],
)
async def test_code_tool_parameter_descriptions(app, operation, job_name):
tool = AsAgentWrapper._make_tool(app.context.jobs[job_name])
properties = ARGUMENTS[operation].model_json_schema()["properties"]
for name, field in properties.items():
assert field["description"]
assert tool.input_schema["properties"][name]["description"] == field["description"]
@pytest.mark.asyncio
@pytest.mark.parametrize("name", ["init", "a/b", "../escape", "-bad", "a\\b", "@{-1}"])
async def test_invalid_candidate_branch(prepared, name):
app, _, _ = prepared
response = await app.run_job("home_create_candidate_branch", branch_name=name)
assert not response.success
assert (await run(app, "home_code_status"))["current_branch"] == "init"
@pytest.mark.asyncio
async def test_protected_init_and_scope(prepared):
app, repo, _ = prepared
response = await app.run_job("home_commit_candidate", message="not allowed")
assert not response.success
await create(app)
baseline = command(repo, "rev-parse", "HEAD")
readme = repo / "plugins/beam/README.md"
readme.write_text("outside declared editable roots")
response = await app.run_job("home_commit_candidate", message="outside scope")
assert not response.success
assert command(repo, "rev-parse", "HEAD") == baseline
assert not command(repo, "diff", "--cached", "--name-only")
readme.write_text("Plugin installation instructions\n")
target = repo / "plugins/beam/src/link"
target.symlink_to("/etc/passwd")
response = await app.run_job("home_commit_candidate", message="link")
assert not response.success
target.unlink()
result = await commit(app, repo)
command(repo, "update-ref", "refs/heads/init", result["commit_sha"])
response = await app.run_job("home_code_status")
assert not response.success
@pytest.mark.asyncio
async def test_merge_then_explicit_commit(prepared):
app, repo, _ = prepared
await create(app, "left")
left = await commit(app, repo)
await run(app, "home_switch_candidate_branch", branch_name="init")
await create(app, "right")
(repo / "plugins/beam/src/new.py").write_text("another mechanism\n")
right = await run(app, "home_commit_candidate", message="right")
merged = await run(app, "home_merge_candidate_branch", branch_name="left")
assert merged["status"] == "merged_not_committed"
assert merged["commit_sha"] == right["commit_sha"]
assert (await run(app, "home_code_status"))["merge_pending"]
response = await app.run_job("home_switch_candidate_branch", branch_name="init")
assert not response.success
merged = await run(app, "home_commit_candidate", message="Combine compatible improvements")
assert command(repo, "rev-list", "--parents", "-n", "1", "HEAD").split() == [
merged["commit_sha"],
right["commit_sha"],
left["commit_sha"],
]
@pytest.mark.asyncio
async def test_conflicting_merge_aborts_cleanly(prepared):
app, repo, _ = prepared
await create(app, "left")
await commit(app, repo, "left = 2\n")
await run(app, "home_switch_candidate_branch", branch_name="init")
await create(app, "right")
right = await commit(app, repo, "right = 2\n")
response = await app.run_job("home_merge_candidate_branch", branch_name="left")
assert not response.success
assert not (repo / ".git/MERGE_HEAD").exists()
state = await run(app, "home_code_status")
assert state["clean"] and state["commit_sha"] == right["commit_sha"]
@pytest.mark.asyncio
async def test_raw_snapshot_regex_paths_and_truncation(prepared):
app, repo, _ = prepared
await create(app)
folder = repo / "plugins/beam/src"
(folder / "space name.py").write_text("prefix\nhello\nworld\nsuffix\n" + "x\n" * 400)
(folder / ".gitattributes").write_text('"space name.py" export-ignore\n')
(folder / "binary").write_bytes(b"a\0b")
(folder / "carriage.py").write_bytes(b"first\rsecond\rthird")
(folder / "colon:name.py").write_text("hello")
# Non-portable paths fail closed instead of becoming Git pathspecs.
rejected = await app.run_job("home_commit_candidate", message="bad path")
assert not rejected.success
(folder / "colon:name.py").unlink()
result = await run(app, "home_commit_candidate", message="Navigation fixtures")
tree = await run(app, "home_list_code_tree", path="plugins/beam/src", max_results=1)
assert tree["revision"] == result["commit_sha"] and tree["truncated"]
found = await run(app, "home_search_code", pattern="hello\nworld", path="plugins/beam/src")
assert found["occurrence_count"] == 1
assert found["matches"][0]["start_line"] == 1
assert "hello" in found["matches"][0]["content"]
carriage = await run(app, "home_search_code", pattern="second", path="plugins/beam/src/carriage.py")
assert "> 2 | second" in carriage["matches"][0]["content"]
read = await run(app, "home_read_code_file", path="plugins/beam/src/space name.py")
assert read["end_line"] == 300 and read["truncated"]
for kwargs in [
{"path": "../escape"},
{"path": ".git/HEAD"},
{"path": "/etc/passwd"},
{"path": "plugins/beam/src/binary"},
{"path": "plugins/beam/src", "revision": "--help"},
]:
assert not (await app.run_job("home_read_code_file", **kwargs)).success
for pattern in ("[", "^", ""):
assert not (await app.run_job("home_search_code", pattern=pattern)).success
assert not (await app.run_job("home_list_code_tree", path=":(glob)**")).success
@pytest.mark.asyncio
async def test_lock_restrictions_and_unprepared(app):
assert not (await app.run_job("home_code_status")).success
assert not (await app.run_job("home_code_status", _allowed_paths=[])).success
component = app.context.components["home.code"]["default"]
with pytest.raises(ValueError, match="Unknown"):
await component.run("reset")
with pytest.raises(ValueError):
await component.run("create_branch", branch_name=1)
with _initialization_lock(component.workspace.state_path / "workspace.lock"):
assert not (await app.run_job("home_list_code_tree")).success
@pytest.mark.asyncio
@pytest.mark.parametrize("cancelled", [False, True])
@pytest.mark.parametrize("fail", [False, True])
async def test_git_thread_drains_and_preserves_cancellation_or_error(prepared, monkeypatch, cancelled, fail):
app, _, _ = prepared
component = app.context.components["home.code"]["default"]
entered, release, finished = Event(), Event(), Event()
original = component._run
def blocked(*args):
entered.set()
assert release.wait(5)
try:
if fail:
raise OSError("injected Git failure")
return original(*args)
finally:
finished.set()
monkeypatch.setattr(component, "_run", blocked)
task = asyncio.create_task(component.run("status"))
try:
assert await asyncio.to_thread(entered.wait, 5)
assert len(component._pending) == 1
if cancelled:
task.cancel("first cancellation")
await asyncio.sleep(0)
task.cancel("second cancellation")
await asyncio.sleep(0)
assert not task.done()
release.set()
if cancelled:
with pytest.raises(asyncio.CancelledError, match="first cancellation"):
await task
elif fail:
with pytest.raises(OSError, match="injected Git failure"):
await task
else:
assert isinstance(await task, dict)
finally:
release.set()
await asyncio.gather(task, return_exceptions=True)
assert finished.is_set() and not component._pending
@pytest.mark.asyncio
async def test_snapshot_size_and_regex_timeout(prepared, monkeypatch):
app, repo, _ = prepared
await create(app)
await commit(app, repo, 'x = "' + "a" * 10000 + '!"\n')
original = snapshot.subprocess.run
def short_timeout(*args, **kwargs):
if args[0][0] == snapshot.sys.executable:
kwargs["timeout"] = 0.05
return original(*args, **kwargs) # pylint: disable=subprocess-run-check
monkeypatch.setattr(snapshot.subprocess, "run", short_timeout)
with pytest.raises(subprocess.TimeoutExpired):
snapshot.search_code(repo, {}, pattern="(a+)+$", path="plugins/beam/src")
monkeypatch.setattr(snapshot.subprocess, "run", original)
monkeypatch.setattr(snapshot, "MAX_FILE_BYTES", 3)
response = await app.run_job("home_read_code_file", path="plugins/beam/src/reme_beam/__init__.py")
assert not response.success
@pytest.mark.asyncio
@pytest.mark.parametrize("mutation", ["staged", "untracked"])
async def test_branch_operations_refuse_dirty_inputs(prepared, mutation):
app, repo, _ = prepared
if mutation == "staged":
target = repo / "plugins/beam/src/reme_beam/__init__.py"
target.write_text("staged = True\n")
command(repo, "add", ".")
else:
target = repo / "plugins/beam/src/untracked file.py"
target.write_text("untracked = True\n")
before = target.read_bytes()
assert not (await app.run_job("home_create_candidate_branch", branch_name="dirty")).success
assert target.read_bytes() == before
assert command(repo, "branch", "--show-current") == "init"
@pytest.mark.asyncio
async def test_exact_paths_rename_deletion_and_revision_pinning(prepared):
app, repo, _ = prepared
await create(app)
original = repo / "plugins/beam/src/reme_beam/__init__.py"
name = "plugins/beam/src/space name\nand newline.py"
target = repo / name
original.rename(target)
first = await run(app, "home_commit_candidate", message="Rename a file")
assert (await run(app, "home_read_code_file", path=name))["content"] == "value = 1\n"
target.write_text("value = 9\n")
pinned = await run(app, "home_commit_candidate", message="Improve renamed code")
target.unlink()
await run(app, "home_commit_candidate", message="Remove obsolete file")
await run(app, "home_switch_candidate_branch", branch_name="init")
read = await run(app, "home_read_code_file", path=name, revision=pinned["commit_sha"])
assert read["content"] == "value = 9\n"
assert name in (await run(app, "home_list_code_tree", revision=first["commit_sha"]))["paths"]
@pytest.mark.asyncio
async def test_reject_symlink_repository_and_detached_candidate(prepared):
app, repo, _ = prepared
await create(app)
commit_sha = (await commit(app, repo))["commit_sha"]
command(repo, "checkout", "--detach", commit_sha)
assert not (await app.run_job("home_commit_candidate", message="detached")).success
assert (await run(app, "home_read_code_file", path="plugins/beam/src/reme_beam/__init__.py"))["content"]
moved = repo.with_name("moved")
repo.rename(moved)
repo.symlink_to(moved, target_is_directory=True)
assert not (await app.run_job("home_code_status")).success