|
1 | 1 | """Tests for multi-iteration campaign loop.""" |
| 2 | +import importlib.metadata as importlib_metadata |
2 | 3 | import json |
3 | 4 | import shutil |
| 5 | +import subprocess |
4 | 6 | import warnings |
5 | 7 | from pathlib import Path |
6 | | -from unittest.mock import MagicMock |
| 8 | +from unittest.mock import MagicMock, patch |
7 | 9 |
|
8 | 10 | import jsonschema |
9 | 11 | import pytest |
|
12 | 14 | from orchestrator.dispatch import StubDispatcher |
13 | 15 | from orchestrator.engine import Engine |
14 | 16 | from orchestrator.campaign import run_campaign |
15 | | -from orchestrator.iteration import IterationOutcome, _save_human_feedback |
| 17 | +from orchestrator.iteration import ( |
| 18 | + IterationOutcome, |
| 19 | + _capture_runtime_meta, |
| 20 | + _save_human_feedback, |
| 21 | + setup_work_dir, |
| 22 | +) |
16 | 23 |
|
17 | 24 | SCHEMAS_DIR = Path(__file__).resolve().parent.parent / "orchestrator" / "schemas" |
18 | 25 | TEMPLATES_DIR = Path(__file__).resolve().parent.parent / "orchestrator" / "templates" |
@@ -449,3 +456,170 @@ def test_multiple_phases_independent(self, tmp_path): |
449 | 456 | fb = json.loads((tmp_path / "human_feedback.json").read_text()) |
450 | 457 | assert len(fb["design"]) == 1 |
451 | 458 | assert len(fb["findings"]) == 1 |
| 459 | + |
| 460 | + |
| 461 | +class TestMetadataEnrichment: |
| 462 | + """Tests for campaign metadata enrichment (runtime block in campaign.yaml copy).""" |
| 463 | + |
| 464 | + CAMPAIGN_WITH_META = { |
| 465 | + **SAMPLE_CAMPAIGN, |
| 466 | + "metadata": { |
| 467 | + "tags": ["prefix-caching", "ttft"], |
| 468 | + "goal": "Determine prefix ratio effect on TTFT", |
| 469 | + }, |
| 470 | + } |
| 471 | + |
| 472 | + def test_setup_work_dir_writes_enriched_campaign_yaml(self, tmp_path): |
| 473 | + """setup_work_dir writes an enriched campaign.yaml with runtime block.""" |
| 474 | + campaign_path = tmp_path / "campaign.yaml" |
| 475 | + campaign_path.write_text(yaml.safe_dump(self.CAMPAIGN_WITH_META)) |
| 476 | + |
| 477 | + work_dir = setup_work_dir( |
| 478 | + "test-run", repo_path=None, |
| 479 | + campaign_path=campaign_path, campaign=self.CAMPAIGN_WITH_META, |
| 480 | + ) |
| 481 | + |
| 482 | + enriched_path = work_dir / "campaign.yaml" |
| 483 | + assert enriched_path.exists() |
| 484 | + |
| 485 | + enriched = yaml.safe_load(enriched_path.read_text()) |
| 486 | + assert "runtime" in enriched |
| 487 | + assert "started_at" in enriched["runtime"] |
| 488 | + assert "nous_version" in enriched["runtime"] |
| 489 | + assert "target_repo" in enriched["runtime"] |
| 490 | + assert "target_commit" in enriched["runtime"] |
| 491 | + |
| 492 | + def test_user_metadata_passes_through(self, tmp_path): |
| 493 | + """User-defined metadata from campaign.yaml appears in the enriched copy.""" |
| 494 | + campaign_path = tmp_path / "campaign.yaml" |
| 495 | + campaign_path.write_text(yaml.safe_dump(self.CAMPAIGN_WITH_META)) |
| 496 | + |
| 497 | + work_dir = setup_work_dir( |
| 498 | + "test-run", repo_path=None, |
| 499 | + campaign_path=campaign_path, campaign=self.CAMPAIGN_WITH_META, |
| 500 | + ) |
| 501 | + |
| 502 | + enriched = yaml.safe_load((work_dir / "campaign.yaml").read_text()) |
| 503 | + assert enriched["metadata"]["tags"] == ["prefix-caching", "ttft"] |
| 504 | + assert enriched["metadata"]["goal"] == "Determine prefix ratio effect on TTFT" |
| 505 | + |
| 506 | + def test_enriched_copy_not_overwritten_on_resume(self, tmp_path): |
| 507 | + """Re-calling setup_work_dir does not clobber the enriched campaign.yaml.""" |
| 508 | + campaign_path = tmp_path / "campaign.yaml" |
| 509 | + campaign_path.write_text(yaml.safe_dump(self.CAMPAIGN_WITH_META)) |
| 510 | + |
| 511 | + work_dir = setup_work_dir( |
| 512 | + "test-run", repo_path=None, |
| 513 | + campaign_path=campaign_path, campaign=self.CAMPAIGN_WITH_META, |
| 514 | + ) |
| 515 | + |
| 516 | + # Modify the enriched file to prove it's not overwritten |
| 517 | + enriched_path = work_dir / "campaign.yaml" |
| 518 | + enriched = yaml.safe_load(enriched_path.read_text()) |
| 519 | + enriched["runtime"]["marker"] = "original" |
| 520 | + enriched_path.write_text(yaml.safe_dump(enriched)) |
| 521 | + |
| 522 | + # Call setup_work_dir again (simulating resume) |
| 523 | + setup_work_dir( |
| 524 | + "test-run", repo_path=None, |
| 525 | + campaign_path=campaign_path, campaign=self.CAMPAIGN_WITH_META, |
| 526 | + ) |
| 527 | + |
| 528 | + reloaded = yaml.safe_load(enriched_path.read_text()) |
| 529 | + assert reloaded["runtime"]["marker"] == "original" |
| 530 | + |
| 531 | + def test_runtime_meta_tolerates_no_git(self, tmp_path): |
| 532 | + """_capture_runtime_meta returns nulls gracefully when git is unavailable.""" |
| 533 | + with patch("orchestrator.iteration.subprocess.check_output", side_effect=FileNotFoundError): |
| 534 | + meta = _capture_runtime_meta(str(tmp_path)) |
| 535 | + |
| 536 | + assert meta["target_repo"] is None |
| 537 | + assert meta["target_commit"] is None |
| 538 | + # nous_version may still be set via importlib.metadata |
| 539 | + assert "started_at" in meta |
| 540 | + |
| 541 | + def test_runtime_meta_captures_target_commit_from_git_repo(self, tmp_path): |
| 542 | + """_capture_runtime_meta captures target_commit from a real git repo.""" |
| 543 | + import subprocess |
| 544 | + repo = tmp_path / "target" |
| 545 | + repo.mkdir() |
| 546 | + subprocess.run(["git", "init"], cwd=repo, capture_output=True, check=True) |
| 547 | + subprocess.run(["git", "config", "user.email", "test@test.com"], cwd=repo, capture_output=True) |
| 548 | + subprocess.run(["git", "config", "user.name", "Test"], cwd=repo, capture_output=True) |
| 549 | + (repo / "f.txt").write_text("x") |
| 550 | + subprocess.run(["git", "add", "."], cwd=repo, capture_output=True, check=True) |
| 551 | + subprocess.run(["git", "commit", "-m", "init"], cwd=repo, capture_output=True, check=True) |
| 552 | + |
| 553 | + meta = _capture_runtime_meta(str(repo)) |
| 554 | + |
| 555 | + assert meta["target_commit"] is not None |
| 556 | + assert len(meta["target_commit"]) == 40 # full SHA |
| 557 | + # No remote configured, so target_repo should be None |
| 558 | + assert meta["target_repo"] is None |
| 559 | + |
| 560 | + def test_no_enriched_copy_without_campaign_path(self, tmp_path, monkeypatch): |
| 561 | + """If campaign_path is not provided, no enriched copy is written.""" |
| 562 | + monkeypatch.chdir(tmp_path) |
| 563 | + work_dir = setup_work_dir("test-run", repo_path=None) |
| 564 | + assert not (work_dir / "campaign.yaml").exists() |
| 565 | + |
| 566 | + @pytest.mark.parametrize("remote,expected", [ |
| 567 | + ("git@github.com:org/repo.git", "org/repo"), |
| 568 | + ("git@github.com:org/repo", "org/repo"), |
| 569 | + ("https://github.com/org/repo.git", "org/repo"), |
| 570 | + ("https://github.com/org/repo", "org/repo"), |
| 571 | + ("ssh://git@github.com/org/repo.git", "org/repo"), |
| 572 | + ("https://gitlab.com/org/repo.git", "https://gitlab.com/org/repo.git"), |
| 573 | + ("git@gitlab.com:org/repo.git", "git@gitlab.com:org/repo.git"), |
| 574 | + ]) |
| 575 | + def test_remote_url_parsing(self, remote, expected, monkeypatch): |
| 576 | + """_capture_runtime_meta correctly parses various remote URL formats.""" |
| 577 | + def fake_check_output(cmd, **kwargs): |
| 578 | + if "rev-parse" in cmd: |
| 579 | + return "a" * 40 + "\n" |
| 580 | + if "get-url" in cmd: |
| 581 | + return remote + "\n" |
| 582 | + raise subprocess.CalledProcessError(1, cmd) |
| 583 | + |
| 584 | + import subprocess as real_subprocess |
| 585 | + monkeypatch.setattr("orchestrator.iteration.subprocess.check_output", fake_check_output) |
| 586 | + meta = _capture_runtime_meta("/fake/repo") |
| 587 | + assert meta["target_repo"] == expected |
| 588 | + |
| 589 | + def test_nous_version_git_sha_fallback(self, monkeypatch): |
| 590 | + """When importlib.metadata fails, nous_version falls back to git SHA.""" |
| 591 | + fake_sha = "b" * 40 |
| 592 | + |
| 593 | + monkeypatch.setattr( |
| 594 | + "orchestrator.iteration.importlib_metadata.version", |
| 595 | + lambda _: (_ for _ in ()).throw(importlib_metadata.PackageNotFoundError()), |
| 596 | + ) |
| 597 | + |
| 598 | + def fake_check_output(cmd, **kwargs): |
| 599 | + if "rev-parse" in cmd: |
| 600 | + return fake_sha + "\n" |
| 601 | + raise subprocess.CalledProcessError(1, cmd) |
| 602 | + |
| 603 | + monkeypatch.setattr("orchestrator.iteration.subprocess.check_output", fake_check_output) |
| 604 | + meta = _capture_runtime_meta(None) |
| 605 | + assert meta["nous_version"] == fake_sha |
| 606 | + |
| 607 | + def test_enrichment_with_repo_path(self, tmp_path): |
| 608 | + """Enriched campaign.yaml is written inside .nous/<run_id>/ when repo_path is set.""" |
| 609 | + campaign_path = tmp_path / "campaign.yaml" |
| 610 | + campaign_path.write_text(yaml.safe_dump(self.CAMPAIGN_WITH_META)) |
| 611 | + |
| 612 | + repo = tmp_path / "target_repo" |
| 613 | + repo.mkdir() |
| 614 | + |
| 615 | + work_dir = setup_work_dir( |
| 616 | + "test-run", repo_path=str(repo), |
| 617 | + campaign_path=campaign_path, campaign=self.CAMPAIGN_WITH_META, |
| 618 | + ) |
| 619 | + |
| 620 | + assert work_dir == repo / ".nous" / "test-run" |
| 621 | + enriched_path = work_dir / "campaign.yaml" |
| 622 | + assert enriched_path.exists() |
| 623 | + enriched = yaml.safe_load(enriched_path.read_text()) |
| 624 | + assert "runtime" in enriched |
| 625 | + assert enriched["metadata"]["tags"] == ["prefix-caching", "ttft"] |
0 commit comments