Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/reference/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ Behavior:
so shared service credentials do not need to be stored in GitHub
- also redeploys during scheduled reconciliation if the saved preview head is stale
- stops previews when the label is removed
- enumerates open PRs and validates their returned label payload locally instead
of trusting GitHub's eventually consistent label-search index; scheduled
reconciliation also stops any tracked runtime whose authoritative label is gone
- deletes imported worktrees when a PR closes or merges
- comments with CPU, memory, disk, Docker, and process evidence when the runner
is too loaded to deploy
Expand Down
49 changes: 43 additions & 6 deletions python/envctl_engine/pr_preview_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2714,14 +2714,20 @@ def remove_imported_branch(self, branch: str) -> None:
)

def sweep(self) -> int:
active_prs = self.active_prs_with_label()
open_prs = self.open_prs()
active_prs = [pr for pr in open_prs if pr.has_label(self.config.label)]
if not active_prs:
return self.blast_all_if_scaled_to_zero()
exit_code = self.stop_unlabeled_tracked_previews(
open_prs,
active_pr_numbers={pr.number for pr in active_prs},
)
now = utc_now()
expired_pr_numbers, exit_code = self.expire_overdue_previews(
expired_pr_numbers, expiry_exit_code = self.expire_overdue_previews(
active_prs,
now=now,
)
exit_code = exit_code or expiry_exit_code
for pr in active_prs:
if pr.number in expired_pr_numbers:
continue
Expand All @@ -2748,6 +2754,36 @@ def sweep(self) -> int:
exit_code = exit_code or self.start(pr, reason="scheduled reconciliation")
return exit_code

def stop_unlabeled_tracked_previews(
self,
open_prs: list[PullRequestInfo],
*,
active_pr_numbers: set[int],
) -> int:
open_prs_by_number = {pr.number: pr for pr in open_prs}
exit_code = 0
for state in self.load_all_states():
if (
state.status in {"stopped", "deleted"}
or state.pr_number in active_pr_numbers
):
continue
pr = open_prs_by_number.get(state.pr_number)
if pr is None:
continue
print(
f"Stopping tracked preview PR #{pr.number}; "
f"authoritative PR labels no longer include `{self.config.label}`.",
flush=True,
)
stop_code = self.stop(
pr,
reason="scheduled reconciliation after label removal",
comment=True,
)
exit_code = exit_code or stop_code
return exit_code

def expire_overdue_previews(
self,
active_prs: list[PullRequestInfo],
Expand Down Expand Up @@ -2950,7 +2986,7 @@ def get_pr(self, number: int) -> PullRequestInfo:
)
return pr_from_gh_payload(payload)

def active_prs_with_label(self) -> list[PullRequestInfo]:
def open_prs(self) -> list[PullRequestInfo]:
payload = self.runner.json(
[
"gh",
Expand All @@ -2960,16 +2996,17 @@ def active_prs_with_label(self) -> list[PullRequestInfo]:
self.config.repo_slug,
"--state",
"open",
"--label",
self.config.label,
"--json",
("number,title,url,state,headRefName,headRefOid,headRepository,headRepositoryOwner,labels"),
"--limit",
"100",
"1000",
]
)
return [pr_from_list_payload(item) for item in payload]

def active_prs_with_label(self) -> list[PullRequestInfo]:
return [pr for pr in self.open_prs() if pr.has_label(self.config.label)]

def label_active_since(self, pr_number: int) -> datetime | None:
result = self.runner.run(
[
Expand Down
84 changes: 84 additions & 0 deletions tests/python/runtime/test_pr_preview_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ def pr_list_payload(
}


def without_preview_label(payload):
return {**payload, "labels": []}


def command_argvs(runner, *prefix):
return [call["argv"] for call in runner.calls if call["argv"][: len(prefix)] == list(prefix)]

Expand Down Expand Up @@ -3079,6 +3083,86 @@ def route_health(_endpoints):
assert "scheduled reconciliation for unhealthy public route" in runner.comments[-1]


def test_active_prs_with_label_ignores_stale_github_label_search_matches(tmp_path):
controller = load_controller()
config = make_config(controller, tmp_path)
runner = FakeRunner(
controller,
active_prs=[
without_preview_label(pr_list_payload(789, "Stale search match")),
pr_list_payload(790, "Actually labeled"),
],
)
instance = controller.PreviewController(config, runner)

active_prs = instance.active_prs_with_label()

assert [pr.number for pr in active_prs] == [790]
list_call = command_argvs(runner, "gh", "pr", "list")[-1]
assert "--label" not in list_call
assert list_call[list_call.index("--limit") + 1] == "1000"


def test_sweep_stops_unlabeled_tracked_preview_while_another_is_active(tmp_path):
controller = load_controller()
stale_root = tmp_path / "control" / "trees" / "imported" / "feature-stale"
active_root = tmp_path / "control" / "trees" / "imported" / "feature-active"
stale_root.mkdir(parents=True)
active_root.mkdir(parents=True)
config = make_config(controller, tmp_path, ttl_minutes=45)
runner = FakeRunner(
controller,
active_prs=[
without_preview_label(
pr_list_payload(789, "Stale", head_ref="feature/stale")
),
pr_list_payload(790, "Active", head_ref="feature/active"),
],
timeline="labeled\t2999-01-01T00:00:00Z\tdeploy-app\n",
projects={
"projects": [
{"name": "feature/stale", "root": str(stale_root), "running": True},
{"name": "feature/active", "root": str(active_root), "running": True},
]
},
root_to_branch={
str(stale_root): "feature/stale",
str(active_root): "feature/active",
},
)
instance = controller.PreviewController(config, runner)
for number, project, root in (
(789, "feature/stale", stale_root),
(790, "feature/active", active_root),
):
instance.save_state(
controller.PreviewState(
pr_number=number,
label="deploy-app",
project=project,
root=str(root),
head_ref=project,
head_sha="abc123456789",
status="running",
label_added_at="2999-01-01T00:00:00Z",
started_at="2999-01-01T00:00:00Z",
expires_at="2999-01-01T00:45:00Z",
updated_at="2999-01-01T00:00:00Z",
endpoints={},
)
)

exit_code = instance.sweep()

assert exit_code == 0
assert command_argvs(runner, "envctl", "stop") == [
["envctl", "stop", "--trees", "--project", "feature/stale", "--entire-system"]
]
assert instance.load_state(789).status == "stopped"
assert instance.load_state(790).status == "running"
assert "scheduled reconciliation after label removal" in runner.comments[-1]


def test_sweep_with_no_active_prs_blasts_processes_without_removing_storage(
tmp_path,
):
Expand Down
Loading