Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 19 additions & 5 deletions orchestrator/concurrent_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,15 @@ def get_worktree_branch(
# cannot see in the slice PR's diff. We honour the
# pipeline's existing branch as the issue prefix when set,
# otherwise fall back to the issue-number / pipeline id.
#
# The pipeline tip is pushed to ``egg/<id>/work`` (#2399), so
# the slice integration branch lives as a sibling of ``/work``
# under ``egg/<id>/`` — strip the trailing ``/work`` from the
# pipeline branch to get the namespace root.
issue = self.pipeline.issue_number or self.pipeline.id
issue_branch = self.pipeline.branch or f"egg/issue-{issue}"
if issue_branch.endswith("/work"):
issue_branch = issue_branch[: -len("/work")]
normalised_slice = slice_id if slice_id.startswith("slice-") else f"slice-{slice_id}"
# Defense-in-depth: re-validate the normalised slice id
# shape before embedding it in a git ref. The contract-
Expand All @@ -298,17 +305,24 @@ def get_worktree_branch(
def get_slice_integration_branch(self, slice_id: str) -> str:
"""Return the shared integration branch for a slice's BRC.

Each slice has its own integration branch under the pipeline
branch — ``egg/issue-N/slice-M`` — that the per-role work
branches rebase onto. Roots base off the pipeline branch
directly; child slices base off their parent slice's
integration branch.
Each slice has its own integration branch as a sibling of the
pipeline tip under ``egg/<id>/`` — ``egg/issue-N/slice-M`` —
that the per-role work branches rebase onto. Roots base off the
pipeline branch directly (``egg/issue-N/work``); child slices
base off their parent slice's integration branch.

The pipeline tip is pushed to ``egg/<id>/work`` (#2399), so the
slice integration branch lives as a sibling of ``/work`` under
``egg/<id>/`` — strip the trailing ``/work`` from the pipeline
branch to get the namespace root.

The slice id is regex-validated for defense-in-depth (see
``get_worktree_branch``).
"""
issue = self.pipeline.issue_number or self.pipeline.id
issue_branch = self.pipeline.branch or f"egg/issue-{issue}"
if issue_branch.endswith("/work"):
issue_branch = issue_branch[: -len("/work")]
normalised_slice = slice_id if slice_id.startswith("slice-") else f"slice-{slice_id}"
import re

Expand Down
7 changes: 6 additions & 1 deletion orchestrator/routes/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,12 @@ def _branch_read_contract(
)
return None

branch = pipeline.branch or f"egg/{pipeline_id}"
# The pipeline tip is pushed to ``egg/<id>/work`` so slice integration
# branches can coexist as siblings — see
# :func:`routes.pipelines._ensure_pipeline_work_ref` for the rationale
# (#2399). The fallback shape mirrors the actual remote ref the
# contract was committed to.
branch = pipeline.branch or f"egg/{pipeline_id}/work"
return contract_store.load_contract_from_branch(identifier, store.repo_path, branch)


Expand Down
69 changes: 63 additions & 6 deletions orchestrator/routes/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,50 @@ def _teardown_phase_overseer(
TESTER_FINDINGS_HEADER = "### tester findings"


def _ensure_pipeline_work_ref(branch: str | None) -> str | None:
"""Return the actual remote ref for an orchestrator-managed pipeline branch.

The orchestrator pushes the pipeline tip to ``<branch>/work`` so the
``<branch>/`` namespace can hold slice integration branches as
siblings (``<branch>/slice-N``) without git's ``directory file
conflict`` rejection — see #2399. A leaf ref at ``<branch>`` and a
child at ``<branch>/slice-N`` cannot coexist on origin, so the
pipeline tip is moved one level deeper into the namespace.

Idempotent and safe across modes:

* ``None`` → ``None`` (prompt-driven; the caller generates a
``/work``-shaped branch later).
* ``egg/<id>`` → ``egg/<id>/work`` (issue / CUSTOM submissions).
* ``egg/<id>/work`` → unchanged (resubmission, internal callers).
* non-``egg/`` (e.g. babysit PR head refs) → unchanged. Babysit
pipelines reuse an existing remote branch and do not own the
namespace below it, so the conflict cannot arise.
"""
if branch is None:
return None
if not branch.startswith("egg/"):
return branch
if branch.endswith("/work"):
return branch
return f"{branch}/work"


def _slice_namespace_root(pipeline_branch: str) -> str:
"""Return the slice-integration-branch namespace root for a pipeline branch.

Slice integration branches live as siblings of the pipeline tip
under ``egg/<id>/`` (see :func:`_ensure_pipeline_work_ref`). The
namespace root is the pipeline branch with the trailing ``/work``
stripped — that's the prefix slice paths (``<root>/slice-N``) are
built from. For legacy / non-normalised branches that do not end in
``/work``, the branch itself is the root.
"""
if pipeline_branch.endswith("/work"):
return pipeline_branch[: -len("/work")]
return pipeline_branch


def _pipeline_identifier(
issue_number: int | None,
pipeline_id: str,
Expand Down Expand Up @@ -1376,6 +1420,15 @@ def create_pipeline() -> tuple[Response, int]:
):
return make_error_response("Missing branch")

# #2399 — push the pipeline tip to ``<branch>/work`` so slice
# integration branches at ``<branch>/slice-N`` can coexist as
# siblings under the same namespace (git rejects a leaf ref and
# children of that ref's path with ``directory file conflict``).
# Skipped for BABYSIT (the branch is an existing PR head we don't
# own) and for non-``egg/`` branches.
if mode != PipelineMode.BABYSIT:
branch = _ensure_pipeline_work_ref(branch)

# Wait for the gateway to be ready before any gateway-dependent work.
# On fresh deploys / pod restarts the orchestrator can accept requests
# while the gateway HTTP listener is still coming up; without this gate
Expand Down Expand Up @@ -10957,16 +11010,20 @@ def _run_implement_phase_slices(
return 1, "no slices in contract"

pipeline_branch = pipeline.branch or (
f"egg/issue-{pipeline.issue_number}"
f"egg/issue-{pipeline.issue_number}/work"
if pipeline.issue_number is not None
else f"egg/{pipeline_id}/work"
)
issue_number = pipeline.issue_number
# Slice integration branches stack under ``pipeline_branch`` directly
# so any qualifier suffix (``-v3``, ``-backend``) is preserved — two
# qualified pipelines for the same issue would otherwise collide in
# the ``egg/issue-N/slice-M`` namespace (#2368).
issue_branch = pipeline_branch
# Slice integration branches stack as siblings of the pipeline tip
# under ``egg/<id>/`` (see :func:`_ensure_pipeline_work_ref` for the
# ``/work`` namespace decision in #2399). The namespace root drops the
# trailing ``/work`` so slice paths build to ``<root>/slice-M`` rather
# than ``<root>/work/slice-M``. The qualifier suffix (``-v3``,
# ``-backend``) is preserved through ``pipeline.branch`` so two
# qualified pipelines for the same issue do not collide on
# ``egg/issue-N/slice-M`` (#2368).
issue_branch = _slice_namespace_root(pipeline_branch)

# Wrap scheduler construction so the run loop doesn't crash if the
# contract bypassed plan-ingestion validation and reaches the
Expand Down
46 changes: 33 additions & 13 deletions orchestrator/stacked_pr_reconciler.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def _resolve_extant_new_base(
slice_,
slices_by_id,
extant_branches: set[str],
issue_branch: str,
slice_namespace_root: str,
pipeline_branch: str,
) -> str:
"""Walk up the slice DAG until an extant branch is found.

Expand All @@ -95,6 +96,12 @@ def _resolve_extant_new_base(
whose branch is still on origin. If the entire chain has been
deleted, fall back to the pipeline branch — root-targeted
branches are stable.

``slice_namespace_root`` is the prefix slice paths are built from
(``egg/<id>``, no ``/work`` suffix); ``pipeline_branch`` is the
actual remote ref of the umbrella pipeline tip (``egg/<id>/work``,
after #2399). They differ by exactly the ``/work`` suffix — see
:func:`routes.pipelines._ensure_pipeline_work_ref`.
"""
# ``dependencies[0]`` is the canonical parent under the forest
# constraint enforced at plan ingestion. ``serialized_chain_order``
Expand All @@ -106,17 +113,17 @@ def _resolve_extant_new_base(
parent_slice = slices_by_id.get(parent_id)
if parent_slice is None:
break
candidate = f"{issue_branch}/{parent_slice.id}"
candidate = f"{slice_namespace_root}/{parent_slice.id}"
if candidate in extant_branches:
return candidate
# This ancestor's branch is also gone (cascading merge).
# Walk one more level up.
parent_id = parent_slice.dependencies[0] if parent_slice.dependencies else None
# Either the slice has no dependencies (it's a root whose own
# PR shouldn't get here — roots target ``issue_branch`` directly,
# PR shouldn't get here — roots target ``pipeline_branch`` directly,
# which is in ``extant_branches``), or every ancestor's branch
# has been deleted. Either way, the pipeline branch is safe.
return issue_branch
return pipeline_branch


@dataclass(frozen=True)
Expand Down Expand Up @@ -181,20 +188,27 @@ def find_orphaned_child_prs(
# (``issue-N-v3``, ``issue-N-backend``), and JIRA (``ENG-1234``).
# The orchestrator's slice-integration branches preserve the
# qualifier (see ``routes/pipelines.py`` ``pipeline.branch``
# propagation), so deriving the issue branch from ``contract_key``
# keeps the reconciler's lookup shape aligned with the producer's
# branch shape. A prior ``f"egg/issue-{issue_number}"`` ternary
# here hard-coded the unqualified shape for any contract with a
# populated ``issue`` field, silently no-op'ing orphan detection
# on every qualified pipeline.
issue_branch = f"egg/{contract.contract_key}"
# propagation), so deriving the slice namespace root from
# ``contract_key`` keeps the reconciler's lookup shape aligned
# with the producer's branch shape. A prior ``f"egg/issue-{issue_number}"``
# ternary here hard-coded the unqualified shape for any contract
# with a populated ``issue`` field, silently no-op'ing orphan
# detection on every qualified pipeline.
#
# Slice integration branches live as siblings of the pipeline tip
# under ``egg/<id>/`` (#2399); the umbrella pipeline tip is at
# ``egg/<id>/work``. ``slice_namespace_root`` is the prefix slice
# paths are built from; ``pipeline_branch`` is the actual remote
# ref of the umbrella tip — used as the cascade-fallback base.
slice_namespace_root = f"egg/{contract.contract_key}"
pipeline_branch = f"{slice_namespace_root}/work"
slices_by_id = {s.id: s for s in contract.slices}

for slice_ in contract.slices:
parent = slice_.parent_branch_at_creation
if parent is None:
continue # not yet provisioned
slice_branch = f"{issue_branch}/{slice_.id}"
slice_branch = f"{slice_namespace_root}/{slice_.id}"
pr = pr_by_head.get(slice_branch)
if pr is None:
continue # slice's PR hasn't been opened yet (or is closed)
Expand All @@ -216,7 +230,13 @@ def find_orphaned_child_prs(
# cascade is the primary trigger for orphan detection, and
# in that case ``parent_branch_at_creation`` points at the
# same just-deleted branch we're trying to escape from.
new_base = _resolve_extant_new_base(slice_, slices_by_id, extant_branches, issue_branch)
new_base = _resolve_extant_new_base(
slice_,
slices_by_id,
extant_branches,
slice_namespace_root,
pipeline_branch,
)
orphans.append(
OrphanedChildPR(
slice_id=slice_.id,
Expand Down
7 changes: 4 additions & 3 deletions orchestrator/tests/test_contracts_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,13 @@ def test_get_uses_derived_branch_when_pipeline_branch_is_none(
assert response.status_code == 200, response.data
body = json.loads(response.data)
assert body["source"] == "branch"
# With branch=None the code should derive "egg/<pipeline_id>"
# and try origin/egg/<pipeline_id> as the preferred ref.
# With branch=None the code should derive "egg/<pipeline_id>/work"
# (the /work-suffixed shape from #2399) and try
# origin/egg/<pipeline_id>/work as the preferred ref.
assert run_mock.call_args_list[0].args[0] == [
"git",
"show",
f"origin/egg/{self.PIPELINE_ID}:.egg-state/contracts/{self.PIPELINE_ID}.json",
f"origin/egg/{self.PIPELINE_ID}/work:.egg-state/contracts/{self.PIPELINE_ID}.json",
]
assert run_mock.call_args_list[0].kwargs["cwd"] == store.repo_path

Expand Down
95 changes: 95 additions & 0 deletions orchestrator/tests/test_pipeline_branch_namespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""Regression tests for #2399 — pipeline-branch / slice-branch ref-namespace.

The orchestrator pushes the pipeline tip to ``<branch>/work`` so the
``<branch>/`` namespace can hold slice integration branches as siblings
(``<branch>/slice-N``) without git's ``directory file conflict``
rejection. A leaf ref at ``<branch>`` and a child at
``<branch>/slice-N`` cannot coexist on origin.

These tests pin the contract so a future refactor can't quietly
re-introduce the leaf-ref shape.
"""

from routes.pipelines import _ensure_pipeline_work_ref, _slice_namespace_root


class TestEnsurePipelineWorkRef:
def test_appends_work_to_egg_branch(self) -> None:
assert _ensure_pipeline_work_ref("egg/issue-2261-v6") == "egg/issue-2261-v6/work"

def test_appends_work_to_qualified_egg_branch(self) -> None:
# Qualifier suffixes (-v3, -backend) propagate; ``/work`` lives one
# level deeper.
assert _ensure_pipeline_work_ref("egg/issue-100-backend") == "egg/issue-100-backend/work"

def test_idempotent_when_already_work_suffixed(self) -> None:
assert _ensure_pipeline_work_ref("egg/issue-2261-v6/work") == "egg/issue-2261-v6/work"

def test_passthrough_for_none(self) -> None:
assert _ensure_pipeline_work_ref(None) is None

def test_passthrough_for_non_egg_branch(self) -> None:
# Babysit branches are arbitrary PR head refs; the orchestrator
# does not own the namespace below them and must not rewrite them.
assert _ensure_pipeline_work_ref("feature/foo") == "feature/foo"
assert _ensure_pipeline_work_ref("main") == "main"

def test_passthrough_for_egg_custom_branch(self) -> None:
# CUSTOM-mode auto-generates ``egg/custom-<hex>``; the same
# ``/work`` rule applies so slice integration branches can be
# added as siblings if a custom pipeline ever uses the slice DAG.
assert _ensure_pipeline_work_ref("egg/custom-deadbeef") == "egg/custom-deadbeef/work"


class TestSliceNamespaceRoot:
def test_strips_work_suffix(self) -> None:
assert _slice_namespace_root("egg/issue-2261-v6/work") == "egg/issue-2261-v6"

def test_passthrough_when_no_work_suffix(self) -> None:
# Legacy / non-normalised callers: the branch itself is the root.
assert _slice_namespace_root("egg/issue-2261-v6") == "egg/issue-2261-v6"

def test_strips_only_trailing_work(self) -> None:
# ``/work`` mid-path is NOT a suffix and must not be stripped.
assert _slice_namespace_root("egg/work-stream/v1/work") == "egg/work-stream/v1"

def test_qualifier_preserved(self) -> None:
assert _slice_namespace_root("egg/issue-100-backend/work") == "egg/issue-100-backend"


class TestNamespaceCoexistence:
"""Pin the design property that solves the conflict.

The pipeline tip ``<root>/work`` and slice integration branches
``<root>/slice-N`` must share a single parent path ``<root>/`` —
that's the whole point of the #2399 fix. A regression that starts
pushing the tip to ``<root>`` (a leaf ref) would re-introduce the
``directory file conflict`` from GitHub.
"""

def test_pipeline_tip_and_slice_share_namespace_parent(self) -> None:
pipeline_branch = _ensure_pipeline_work_ref("egg/issue-2261-v6")
assert pipeline_branch == "egg/issue-2261-v6/work"

namespace_root = _slice_namespace_root(pipeline_branch)
slice_branch = f"{namespace_root}/slice-1"

# Both refs share the parent ``egg/issue-2261-v6/`` and live as
# siblings — neither is a prefix of the other.
assert pipeline_branch.rsplit("/", 1)[0] == slice_branch.rsplit("/", 1)[0]
assert not slice_branch.startswith(pipeline_branch + "/")
assert not pipeline_branch.startswith(slice_branch + "/")

def test_pipeline_tip_is_not_a_prefix_of_slice_branch(self) -> None:
# Regression: the pre-fix shape had ``pipeline.branch ==
# 'egg/issue-2261-v6'`` and slice branches at
# ``'egg/issue-2261-v6/slice-N'``, making the slice path a child
# of the pipeline ref. Git's ref storage rejects that with
# ``directory file conflict``.
pipeline_branch = _ensure_pipeline_work_ref("egg/issue-2261-v6")
namespace_root = _slice_namespace_root(pipeline_branch)
slice_branch = f"{namespace_root}/slice-1"
assert not slice_branch.startswith(pipeline_branch + "/"), (
"Slice integration branch must not live under the pipeline tip's path "
"— that's the directory/file conflict #2399 fixes."
)
Loading
Loading