fix(site-publish): retain immutable route history
Test / contract (pull_request) Successful in 6s

Authored-By: OpenAI (GPT-5) <noreply@openai.com>
This commit is contained in:
Evelyn Chen
2026-08-29 23:15:29 +00:00
parent 85a0b41380
commit 892b6e6441
3 changed files with 79 additions and 27 deletions
+36 -21
View File
@@ -178,29 +178,40 @@ def publish_route_immutables(artifact, route, site_dir, credential_env_names=Non
publish_immutable_rule(artifact, route, rule, html_dir, aws_env)
def immutable_key_prefixes(artifact, route):
"""Return immutable partitions as bucket-relative key prefixes."""
route_prefix = route["path"].strip("/")
return [
"/".join(part for part in (route_prefix, rule["path"]) if part)
for rule in artifact["cache_rules"] if _is_immutable(rule)
]
def retained_immutable_paths(artifact, route, previous_contract):
"""Carry all bucket history forward so later route moves cannot delete it."""
previous_paths = previous_contract["immutable_paths"] if previous_contract else []
return sorted(set(previous_paths) | set(immutable_key_prefixes(artifact, route)))
def retired_immutable_filters(artifact, route, html_dir, previous_contract):
"""Protect immutable keys only when an old route falls inside the new scope."""
"""Protect historical immutable keys that fall inside the current sync scope."""
if not previous_contract:
return []
current_prefix = route["path"].strip("/")
previous_prefix = previous_contract["path"].strip("/")
if current_prefix:
marker = f"{current_prefix}/"
if previous_prefix == current_prefix:
previous_prefix = ""
elif not previous_prefix.startswith(marker):
return []
else:
previous_prefix = previous_prefix[len(marker):]
filters = []
current_immutable = {
rule["path"] for rule in artifact["cache_rules"] if _is_immutable(rule)
}
current_immutable = set(immutable_key_prefixes(artifact, route))
for immutable_path in previous_contract["immutable_paths"]:
retired_path = "/".join(part for part in (previous_prefix, immutable_path) if part)
if immutable_path in current_immutable:
continue
if current_prefix:
marker = f"{current_prefix}/"
if not immutable_path.startswith(marker):
continue
retired_path = immutable_path[len(marker):]
else:
retired_path = immutable_path
collision = html_dir / retired_path
if (immutable_path not in current_immutable and collision.exists()
and any(path.is_file() for path in collision.rglob("*"))):
if collision.exists() and any(path.is_file() for path in collision.rglob("*")):
raise RuntimeError(
f"current artifact collides with retired immutable partition: {retired_path}"
)
@@ -308,18 +319,20 @@ def ensure_bucket_aliases(site_name, aliases, admin_token):
raise
def render_site_manifests(site_name, action_dir, app_dir, manifests_dir, cfg):
def render_site_manifests(
site_name, action_dir, app_dir, manifests_dir, cfg, previous_contracts=None,
):
"""Always re-render manifests from current site.yaml. Templates own
domain + aliases, so changes propagate without manual edits."""
manifests_dir.mkdir(parents=True, exist_ok=True)
artifact_by_name = {artifact["name"]: artifact for artifact in cfg["artifacts"]}
previous_contracts = previous_contracts or {}
routes = []
for route in cfg["routes"]:
artifact = artifact_by_name[route["artifact"]]
resource_name = k8s_name(site_name) if cfg["compatibility"] else f"{k8s_name(site_name)}-{route['name']}"
immutable_paths = [
rule["path"] for rule in artifact["cache_rules"] if _is_immutable(rule)
]
previous = previous_contracts.get(artifact["bucket"])
immutable_paths = retained_immutable_paths(artifact, route, previous)
routes.append({
**route, "resource_name": resource_name, "artifact_config": artifact,
"immutable_paths_json": json.dumps(immutable_paths, separators=(",", ":")),
@@ -413,7 +426,9 @@ def deploy_static(site_name, site_dir, action_dir, token, cfg):
if cfg["compatibility"]:
ensure_bucket_aliases(site_name, cfg["aliases"], os.environ.get("GARAGE_ADMIN_TOKEN"))
render_site_manifests(site_name, action_dir, app_dir, manifests_dir, cfg)
render_site_manifests(
site_name, action_dir, app_dir, manifests_dir, cfg, previous_contracts,
)
commit_and_push(apps_dir, f"Deploy {site_name}", token)