Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
73 changes: 73 additions & 0 deletions gateway/tests/test_agent_restrictions_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,23 @@ def test_cannot_write_contracts(self, pattern):
def test_can_write_agent_outputs(self, pattern):
assert pattern.can_write(".egg-state/agent-outputs/coder-out.json") is True

def test_cannot_write_github_dir(self, pattern):
"""Coder is blocked from `.github/` (branch-protection invariant)."""
assert pattern.can_write(".github/workflows/ci.yml") is False
assert pattern.can_write(".github/CODEOWNERS") is False

def test_can_write_github_staging_dir(self, pattern):
"""Coder can write to `.github-staging/` (issue #2508 staging convention).

The `.github/` blocked prefix is matched via ``startswith(".github/")``,
which doesn't match `.github-staging/...`, so the catch-all
``**`` allowlist reaches it. The PR builder turns staged files
into a manual step asking the human reviewer to move them
into `.github/` before merge.
"""
assert pattern.can_write(".github-staging/workflows/ci.yml") is True
assert pattern.can_write(".github-staging/CODEOWNERS") is True

def test_can_write_python_version(self, pattern):
"""Coder can write .python-version (common project config)."""
assert pattern.can_write(".python-version") is True
Expand Down Expand Up @@ -377,6 +394,20 @@ def test_blocks_github_codeowners(self, pattern):
""".github/ is blocked (branch-protection invariant)."""
assert pattern.can_write(".github/CODEOWNERS") is False

def test_allows_github_staging_workflow(self, pattern):
"""`.github-staging/` is the staging-dir convention from issue #2508.

The `.github/` blocked prefix is matched via ``startswith(".github/")``,
which does not match `.github-staging/...` — so the catch-all
``**`` allowlist reaches the staging dir. This regression-locks
that behavior so the convention keeps working.
"""
assert pattern.can_write(".github-staging/workflows/ci.yml") is True

def test_allows_github_staging_codeowners(self, pattern):
"""Staging-dir convention covers extensionless files like CODEOWNERS."""
assert pattern.can_write(".github-staging/CODEOWNERS") is True

def test_allows_license(self, pattern):
assert pattern.can_write("LICENSE") is True

Expand Down Expand Up @@ -615,6 +646,23 @@ def test_cannot_write_contracts(self, pattern):
def test_can_write_agent_outputs(self, pattern):
assert pattern.can_write(".egg-state/agent-outputs/doc-out.json") is True

def test_cannot_write_github_dir_md(self, pattern):
"""Documenter is blocked from markdown under `.github/` (issue #2508).

Without the `.github/` block, the documenter's ``**/*.md``
allow would let it rewrite ``.github/PULL_REQUEST_TEMPLATE.md``,
``.github/ISSUE_TEMPLATE.md``, etc. — bypassing the
branch-protection invariant the staging convention preserves.
"""
assert pattern.can_write(".github/PULL_REQUEST_TEMPLATE.md") is False
assert pattern.can_write(".github/ISSUE_TEMPLATE.md") is False

def test_cannot_write_github_dir_codeowners(self, pattern):
"""Even non-markdown under `.github/` is blocked — the documenter
couldn't reach CODEOWNERS via its allowlist anyway, but the
block makes the invariant explicit."""
assert pattern.can_write(".github/CODEOWNERS") is False


class TestArchitectRole:
"""Verify architect agent can only write drafts and agent-outputs."""
Expand Down Expand Up @@ -802,6 +850,18 @@ def test_cannot_write_contracts(self, pattern):
def test_can_write_agent_outputs(self, pattern):
assert pattern.can_write(".egg-state/agent-outputs/autofixer-out.json") is True

def test_cannot_write_github_workflows(self, pattern):
"""Autofixer is blocked from `.github/` (issue #2508).

Without this block, the autofixer's ``**/*.yml`` allow would
let it rewrite ``.github/workflows/ci.yml`` directly when
applying a YAML lint fix — bypassing the branch-protection
invariant the staging convention preserves.
"""
assert pattern.can_write(".github/workflows/ci.yml") is False
assert pattern.can_write(".github/workflows/test.yaml") is False
assert pattern.can_write(".github/dependabot.yml") is False


class TestConflictResolverRole:
"""Verify conflict resolver agent can/cannot write expected files."""
Expand Down Expand Up @@ -881,6 +941,19 @@ def test_cannot_write_reviews(self, pattern):
def test_can_write_agent_outputs(self, pattern):
assert pattern.can_write(".egg-state/agent-outputs/resolver-out.json") is True

def test_cannot_write_github_dir(self, pattern):
"""Conflict resolver is blocked from `.github/` (issue #2508).

Without this block, resolving a merge conflict that touches
``.github/workflows/*.yml`` or ``.github/CODEOWNERS`` would
commit the resolution directly into `.github/` and push it,
bypassing the branch-protection invariant the staging
convention preserves.
"""
assert pattern.can_write(".github/workflows/ci.yml") is False
assert pattern.can_write(".github/CODEOWNERS") is False
assert pattern.can_write(".github/PULL_REQUEST_TEMPLATE.md") is False


class TestAllRolesRegistered:
"""Verify all defined roles have patterns in the registry."""
Expand Down
135 changes: 133 additions & 2 deletions orchestrator/routes/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -5561,6 +5561,29 @@ def _build_role_restrictions_section() -> str:
)
lines.append("")

# Staging-dir convention for `.github/` (issue #2508).
lines.append("### `.github/` changes — use the `.github-staging/` convention")
lines.append("")
lines.append(
"Every producer role is blocked from writing under `.github/` "
"(CI workflows, CODEOWNERS, dependabot config) — this is a "
"branch-protection invariant, not a planner mistake. Tasks that "
"need to modify those files must instead write the proposed "
"end-state to top-level `.github-staging/`, mirroring the "
"`.github/` structure (e.g. a proposed change to "
"`.github/workflows/ci.yml` is staged at "
"`.github-staging/workflows/ci.yml`). The orchestrator's PR "
"builder auto-detects `.github-staging/` and emits a manual "
"step asking the human reviewer to move the staged files into "
"place before merge. Assign such tasks to `role: coder` and "
"make the staging path explicit in the task's "
"`files_affected`. `.github-staging/` must remain tracked by "
"git (do not add it to `.gitignore`); otherwise the staged "
"files won't be in the PR commit and the reviewer's `git mv` "
"will fail."
)
lines.append("")

return "\n".join(lines)


Expand Down Expand Up @@ -8464,6 +8487,83 @@ def _pr_metadata_from_plan_draft(
)


def _build_github_staging_manual_step(worktree_repo_path: Path) -> str:
"""Render the auto manual-step for `.github-staging/` files (issue #2508).

Producer agents (coder, etc.) cannot push to `.github/` because the
gateway blocks the path as a branch-protection invariant. When a
plan calls for CI workflow or CODEOWNERS changes, the agent instead
writes the proposed end-state to top-level `.github-staging/`,
mirroring the `.github/` structure. This helper scans that
directory and returns a markdown step the human reviewer must
complete before merge: review the staged files, move them into
`.github/`, delete the staging dir, and push the resulting commit.

Returns an empty string when `.github-staging/` is absent or empty.
"""
staging_dir = worktree_repo_path / ".github-staging"
if not staging_dir.is_dir():
return ""

staged_paths: list[str] = []
for path in sorted(staging_dir.rglob("*")):
# Skip symlinks: ``Path.is_file()`` follows them, so without this
# guard a staged ``.github-staging/evil.yml`` → ``/etc/passwd``
# would be surfaced in the manual-step file list, the reviewer's
# ``git mv`` would preserve it, and ``.github/evil.yml`` would
# land in the repo as a symlink. The reviewer's only mitigation
# would be the diff (where a symlink shows as a small mode
# change that's easy to skim past). Drop staged symlinks here so
# the helper is the choke point.
if path.is_symlink():
continue
if not path.is_file():
continue
try:
rel = path.relative_to(worktree_repo_path).as_posix()
except ValueError:
continue
staged_paths.append(rel)

if not staged_paths:
return ""

lines = [
"### Move staged `.github/` changes (auto-generated, issue #2508)",
"",
"This PR includes proposed `.github/` changes under `.github-staging/`. "
"Agent roles cannot push to `.github/` directly (CI workflow / CODEOWNERS "
"branch-protection invariant), so the agent staged the proposed "
"end-state for human review.",
"",
"Staged files:",
]
for rel in staged_paths:
lines.append(f"- `{rel}`")
lines.extend(
[
"",
"Before merging:",
"",
"1. Review each staged file for correctness — these are proposed "
"CI / repo-config changes that bypass the agent's normal sandbox.",
"2. Move each file from `.github-staging/<path>` to `.github/<path>`. For example:",
" ```",
" mkdir -p .github/workflows",
" git mv .github-staging/workflows/test-e2e.yml .github/workflows/test-e2e.yml",
" ```",
" After the `git mv`, `.github-staging/` is no longer tracked "
"by git (git doesn't track empty directories). Run "
"`rm -rf .github-staging` locally if you want to clear any "
"leftover empty subdirectories from your worktree.",
"3. Commit the move and push from a context with the GitHub "
"`workflow` scope (a normal user push works; the bot token may "
"not — see issue #2508 layer 2).",
]
)
return "\n".join(lines)


def _build_pr_body(
pipeline: Pipeline,
worktree_repo_path: Path,
Expand Down Expand Up @@ -8598,9 +8698,22 @@ def _build_pr_body(
else:
body_parts.append("## Test Plan\n\n_No test plan provided by the planner._")

# Manual steps section (only if there are steps)
# Auto-generated step for `.github-staging/` (issue #2508): the
# gateway blocks every producer role from pushing to `.github/`,
# so agents drop proposed CI workflow / CODEOWNERS changes into
# top-level `.github-staging/` instead. Detect them here and
# surface a step the human reviewer must complete before merge.
github_staging_step = _build_github_staging_manual_step(worktree_repo_path)

# Manual steps section: planner-supplied steps and the staging-dir
# auto-step are rendered together so reviewers see one block.
manual_step_chunks: list[str] = []
if pr_manual_steps:
body_parts.append(f"## Manual Steps\n\n{pr_manual_steps}")
manual_step_chunks.append(pr_manual_steps)
if github_staging_step:
manual_step_chunks.append(github_staging_step)
if manual_step_chunks:
body_parts.append("## Manual Steps\n\n" + "\n\n".join(manual_step_chunks))

# Add pipeline context section
if pipeline.id or pipeline.issue_number:
Expand Down Expand Up @@ -10248,6 +10361,24 @@ def _build_file_boundary_section(role_value: str) -> str:
lines.append("**Allowed:** " + ", ".join(f"`{p}`" for p in fa.allowed_write))
if fa.blocked_write:
lines.append("**Blocked:** " + ", ".join(f"`{p}`" for p in fa.blocked_write))

# `.github/` staging-dir convention (issue #2508). Surfaced for the
# coder role specifically because it's the only producer whose
# catch-all allowlist reaches `.github-staging/`; other roles can't
# write the staged files even via the convention.
if role_value == "coder":
lines.append("")
lines.append(
"**`.github/` changes**: `.github/` is blocked above. If your "
"task requires modifying CI workflows, CODEOWNERS, dependabot "
"config, or anything else under `.github/`, write the proposed "
"end-state to top-level `.github-staging/` instead, mirroring "
"the `.github/` structure (e.g. stage "
"`.github/workflows/test-e2e.yml` as "
"`.github-staging/workflows/test-e2e.yml`). The PR builder "
"auto-emits a manual step asking the human reviewer to move "
"the staged files into place before merge — see issue #2508."
)
lines.append("")
return "\n".join(lines)

Expand Down
Loading
Loading