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
8 changes: 8 additions & 0 deletions .ast-grep/rule-tests/stage-registry-cwd-read-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
id: stage-registry-cwd-read
valid:
- "table = registry_table(checkout)"
- "table = registry_table(self._source)"
- "table = registry_table(source)"
invalid:
- "table = registry_table(Path.cwd())"
- "table = registry_table(pathlib.Path.cwd())"
10 changes: 10 additions & 0 deletions .ast-grep/rules/stage-registry-cwd-read.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
id: stage-registry-cwd-read
language: python
severity: error
files:
- "src/**"
message: registry_table called with Path.cwd(). Pass the checked-out repository path instead.
rule:
any:
- pattern: registry_table(Path.cwd())
- pattern: registry_table(pathlib.Path.cwd())
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schema: spec-driven
created: 2026-11-18
skip_specs: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Design

## Decisions

- Require `registry: Sequence[RegistryCount]` as a keyword-only parameter without a default in `check_gates`, `check_proposal`, `replay_all`, and `replay_case`. Callers must supply the registry from the inspected checkout or test context.
- Add an ast-grep structural rule `stage-registry-cwd-read` in `.ast-grep/rules/stage-registry-cwd-read.yml` targeting `src/**` to enforce that no production code invokes `registry_table(Path.cwd())`.
- In `_Planner`, construct `self._registry = registry_table(source)` once during initialization and reuse it for all candidate items and gate checks.
- In `_Judge`, construct `_registry` once via `@cached_property` from `self._source`.
- In `tests/roadmap/stage/test_roadmap_stage_eval_gates.py`, build the registry table once per pytest session using a session-scoped fixture `registry`.

## Rejected Alternatives

- Keep default `registry=None` falling back to `registry_table(Path.cwd())`: this was the source of defects when executing against external worktrees or isolated subdirectories.
- Recompute `registry_table` on every gate call: reading git and files repeatedly for every item and request wastes I/O and CPU cycles.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Proposal: #4695

## Why

The stage change gates and replays read the registry table from the process working directory via `registry_table(Path.cwd())`. When `check_gates`, `check_proposal`, `replay_all`, or `replay_case` operated in another directory or against another checkout (such as during evaluation replays or when running tests from an arbitrary working folder), they read counts from the wrong folder or failed to find the project root.

## What Changes

- Make `registry: Sequence[RegistryCount]` a required keyword-only argument in `check_gates` and `check_proposal`.
- Make `registry: Sequence[RegistryCount]` a required keyword-only argument in `replay_all` and `replay_case`.
- Add an ast-grep rule `stage-registry-cwd-read` forbidding `registry_table(Path.cwd())` in `src/**`.
- Build `registry_table` once per stage run in `_Planner.__init__` and reuse it across all items, requests, and gate checks.
- Build `registry_table` once in `_Judge._registry` via `@cached_property` in the implement job.
- Build `registry_table` once in `_replay` of `commands/roadmap_eval_stage.py` and pass it to `replay_all`.
- Build `registry_table` once in `run_cases` of `stage_eval_run.py` and pass it down.
- Update the `report` fixture in `tests/roadmap/stage/test_roadmap_stage_eval_gates.py` to use a session-scoped `registry` fixture.

## Capabilities

None: this changes internal gate parameterization and evaluation replays, not external product capabilities. `skip_specs` is set.

## Impact

- Gate checks evaluate the passed registry table rather than the process working directory.
- `registry_table` is built once per stage run instead of once per item or request.

## Feasibility

- The ast-grep scan verifies no caller in `src/**` calls `registry_table(Path.cwd())`.
- All 134 evaluation replay test cases and all unit tests pass with the session fixture and required keyword arguments.

## Existing Tool Considered

No external tool is applicable. The changes standardize existing internal registry validation APIs and eliminate process working directory dependencies.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Tasks

## 1. Behavior

- [x] 1.1 Update `check_gates` and `check_proposal` to require keyword-only `registry` parameter
- Verification: `$ uv run pytest tests/roadmap/stage/test_roadmap_stage_gates.py`
- [x] 1.2 Update `replay_all` and `replay_case` to require keyword-only `registry` parameter
- Verification: `$ uv run pytest tests/roadmap/stage/test_roadmap_stage_eval_gates.py`
- [x] 1.3 Add ast-grep rule `stage-registry-cwd-read` and register in construct gates
- Verification: `$ uv run devops ci --check --only constructs`
- [x] 1.4 Cache `registry_table` once per run in `_Planner` and `_Judge`
- Verification: `$ uv run pytest tests/roadmap/stage/test_roadmap_stage_plan.py`

## 2. Documents

- [x] 2.1 Set `skip_specs: true` in `.openspec.yaml` and document change in OpenSpec change folder
- Verification: `$ uv run devops openspec check`

## Workflow follow-up

- Pending a person: review the diff in tuicr (tuicr -r main..HEAD).
14 changes: 11 additions & 3 deletions src/devops_cli/commands/roadmap_eval_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,21 @@ def _record(cases_dir: Path | None, names: tuple[str, ...], limit: int | None) -


def _replay(
cases_dir: Path | None, names: tuple[str, ...], limit: int | None, *, as_json: bool
cases_dir: Path | None,
names: tuple[str, ...],
limit: int | None,
*,
source: Path | None = None,
as_json: bool,
) -> None:
from devops_cli.core.repo import find_repo_root
from devops_cli.roadmap.stage.stage_eval_cases import load_cases, select_cases
from devops_cli.roadmap.stage.stage_eval_gates import replay_all
from devops_cli.roadmap.stage.stage_eval_render import render_replay, replay_json
from devops_cli.roadmap.stage.stage_registry import registry_table

report = replay_all(select_cases(load_cases(cases_dir), names, limit))
registry = registry_table(source or find_repo_root())
report = replay_all(select_cases(load_cases(cases_dir), names, limit), registry=registry)
write_stdout(replay_json(report) if as_json else render_replay(report))
if report.failures:
raise typer.Exit(1)
Expand Down Expand Up @@ -292,7 +300,7 @@ def eval_stage_cmd(
_record(cases_dir, names, limit)
return
if replay:
_replay(cases_dir, names, limit, as_json=as_json)
_replay(cases_dir, names, limit, source=source, as_json=as_json)
return
_run(
cases_dir,
Expand Down
13 changes: 12 additions & 1 deletion src/devops_cli/roadmap/implement/implement.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
)
from devops_cli.roadmap.stage.stage_change import check_proposal, issue_dependencies
from devops_cli.roadmap.stage.stage_paths import StagedTree
from devops_cli.roadmap.stage.stage_registry import RegistryCount, registry_table
from devops_cli.roadmap.stage.stage_verify import FIX_PLAN_LABEL
from devops_cli.roadmap.status_sync import job_holds
from devops_cli.roadmap.step_files import parse_task_groups
Expand Down Expand Up @@ -370,6 +371,11 @@ def _items(self) -> list[Item]:
"""Every Item, open and closed, read once for the titles that a body names (#4803)."""
return self._store.items()

@cached_property
def _registry(self) -> tuple[RegistryCount, ...]:
"""The registry table of the repository, built once from its checkout (#4695)."""
return registry_table(self._source)

def _dependency(self, item: Item) -> str | None:
"""An open issue that the Dependencies section of the body names as `#N` (#2654) or by its
quoted title (#4803), from the shared check (#4009)."""
Expand Down Expand Up @@ -465,7 +471,12 @@ def _plan_reason(self, item: Item, rev: str) -> str | None:
) is not None:
return dependency
problems = check_proposal(
proposal, known, StagedTree(files), requirements=requirements, issue_body=item.body
proposal,
known,
StagedTree(files),
requirements=requirements,
issue_body=item.body,
registry=self._registry,
)
return texts.implement_left_plan.format(problem=problems[0]) if problems else None

Expand Down
9 changes: 8 additions & 1 deletion src/devops_cli/roadmap/plan_branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from __future__ import annotations

import logging
from collections.abc import Collection, Mapping
from collections.abc import Collection, Mapping, Sequence
from dataclasses import dataclass, replace
from enum import StrEnum
from pathlib import Path, PurePosixPath
Expand All @@ -52,6 +52,7 @@
from devops_cli.roadmap.plan_rewrite import PlanRewrite, rewrite_plan, write_rewrite
from devops_cli.roadmap.stage.stage_change import check_gates
from devops_cli.roadmap.stage.stage_paths import StagedTree
from devops_cli.roadmap.stage.stage_registry import RegistryCount, registry_table
from devops_cli.roadmap.stage.stage_validate import validate_change
from devops_cli.roadmap.stores.store import RoadmapStore

Expand Down Expand Up @@ -218,6 +219,7 @@ def _plan_problem(
requirements: Mapping[str, Collection[str]],
base_scenarios: Mapping[str, Collection[str]],
issue_body: str,
registry: Sequence[RegistryCount] = (),
) -> str | None:
"""What the plan reader, the gates or `openspec validate` find wrong in the rewritten plan, or
None when every check passes."""
Expand All @@ -233,6 +235,7 @@ def _plan_problem(
requirements=requirements,
base_scenarios=base_scenarios,
issue_body=issue_body,
registry=registry,
)
problems = [problem for gate in found.values() for problem in gate]
if problems:
Expand All @@ -255,6 +258,7 @@ def push_rewrite(
requirements: Mapping[str, Collection[str]],
base_scenarios: Mapping[str, Collection[str]],
issue_body: str = "",
registry: Sequence[RegistryCount] = (),
) -> BranchRewrite:
"""Rebase the branch of `target` onto `default` in a detached worktree, write `rewrite` there,
check it, and push it as one commit with a lease on the head that the pass read.
Expand Down Expand Up @@ -287,6 +291,7 @@ def push_rewrite(
requirements=requirements,
base_scenarios=base_scenarios,
issue_body=issue_body,
registry=registry,
)
if problem is not None:
reason = texts.plan_branches_invalid.format(
Expand Down Expand Up @@ -341,6 +346,7 @@ def rewrite_open_branches(
tree = StagedTree(paths)
known, requirements = specs_at(source, rev, operation=GIT_OPERATION, files=paths)
base_scenarios = scenario_names_at(source, rev, operation=GIT_OPERATION, files=paths)
registry = registry_table(source)
records: list[BranchRewrite] = []
for pull in open_plan_pulls(store, reads, repo, default):
record = branch_rewrite(source, pull, default, tree)
Expand All @@ -357,6 +363,7 @@ def rewrite_open_branches(
requirements=requirements,
base_scenarios=base_scenarios,
issue_body=item.body if item is not None else "",
registry=registry,
)
records.append(record)
return BranchRun(repo, default, confirm, tuple(records))
Expand Down
4 changes: 4 additions & 0 deletions src/devops_cli/roadmap/plan_recheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
record_module_tests,
record_test_references,
)
from devops_cli.roadmap.stage.stage_registry import RegistryCount, registry_table
from devops_cli.roadmap.stage.stage_validate import validate_change
from devops_cli.roadmap.step_files import parse_task_groups
from devops_cli.roadmap.stores.store import GitHubState, Item, RoadmapStore
Expand Down Expand Up @@ -253,6 +254,7 @@ class _Pass:
closed: frozenset[int]
tool: OpenSpecTool
test_search: Callable[[str], Sequence[str]]
registry: Sequence[RegistryCount]


def _refused(
Expand Down Expand Up @@ -368,6 +370,7 @@ def _record(shared: _Pass, issue: int, folder: str) -> PlanRecord:
issue_body=held.body if held is not None else "",
manifest_references=record_test_references(proposal.groups, shared.test_search),
module_tests=record_module_tests(proposal.groups, shared.tree, shared.test_search),
registry=shared.registry,
)
refusals = [
Refusal(folder, gate.value, problem)
Expand Down Expand Up @@ -419,6 +422,7 @@ def _judge(
closed=closed,
tool=tool,
test_search=_test_search(source, rev),
registry=registry_table(source),
)
return tuple(_record(shared, issue, folder) for issue, folder in sorted(planned.active.items()))

Expand Down
9 changes: 5 additions & 4 deletions src/devops_cli/roadmap/stage/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class leaves the count as it is.
pull_request_files,
)
from devops_cli.roadmap.stage.stage_references import reference_files
from devops_cli.roadmap.stage.stage_registry import matches, registry_table
from devops_cli.roadmap.stage.stage_registry import RegistryCount, matches, registry_table
from devops_cli.roadmap.stage.stage_rejected import (
ReadFailure,
RejectedPlan,
Expand Down Expand Up @@ -1430,6 +1430,7 @@ def __init__(
self._traces: list[RunTrace] = [] if traces is None else traces
self._source = source
self._sha = sha
self._registry: tuple[RegistryCount, ...] = registry_table(source)
self._specs = specs
self._known = frozenset(specs)
self._requirements = {name: spec.requirements for name, spec in specs.items()}
Expand Down Expand Up @@ -2354,7 +2355,7 @@ def _request(self, target: StageTarget) -> StageRequest:
item.body,
max_tokens=context_cap - max(0, comments.tokens - spare),
)
registry = registry_table(self._source)
registry = self._registry
matched = matches(registry, (item.title, item.body), tuple(self._tree.paths_in(item.body)))
if matched:
lines = [f"- {entry.path} ({entry.declaring_path}:{entry.line})" for entry in matched]
Expand Down Expand Up @@ -2630,7 +2631,7 @@ def _gate_problems(
# The citation rule reads the staged commit, the one the probe and the model read
# (#3422), and never the working tree.
read_text=partial(_read_staged_file, self._source, self._sha),
registry=registry_table(self._source),
registry=self._registry,
constants=constants,
references=references,
models=models,
Expand Down Expand Up @@ -2761,7 +2762,7 @@ def _plan_check(
item_title=target.item.title,
item_body=target.item.body,
budget=self._budget,
registry=registry_table(self._source),
registry=self._registry,
)
found = staged_problems(folder, number=target.item.number, title=title, body=body)
return [problem.line for problem in found]
Expand Down
7 changes: 3 additions & 4 deletions src/devops_cli/roadmap/stage/stage_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
RegistryCount,
matches,
missing_test_paths,
registry_table,
)
from devops_cli.roadmap.step_files import code_spans, named_files, packed_file, parse_task_groups
from devops_cli.roadmap.step_rules import rules_sections
Expand Down Expand Up @@ -2952,7 +2951,7 @@ def check_gates(
search_evidence: Mapping[str, Sequence[str]] | None = None,
plan_creates: Mapping[str, int] | None = None,
read_text: Callable[[str], str | None] | None = None,
registry: Sequence[RegistryCount] | None = None,
registry: Sequence[RegistryCount],
constants: Mapping[str, int] | None = None,
references: Sequence[str] = (),
models: Mapping[str, tuple[str, Sequence[str]]] | None = None,
Expand Down Expand Up @@ -3012,7 +3011,7 @@ def check_gates(
ProposalGate.CREATES: creates_problems(proposal.groups, plan_creates or {}),
ProposalGate.REGISTRY: registry_problems(
proposal,
registry if registry is not None else registry_table(Path.cwd()),
registry,
),
ProposalGate.CONSTANTS: constant_problems(plan_texts(proposal), constants or {}),
ProposalGate.TRACE: _trace_problems(proposal, trace, probes, judged),
Expand All @@ -3035,7 +3034,7 @@ def check_proposal(
search_evidence: Mapping[str, Sequence[str]] | None = None,
plan_creates: Mapping[str, int] | None = None,
read_text: Callable[[str], str | None] | None = None,
registry: Sequence[RegistryCount] | None = None,
registry: Sequence[RegistryCount],
constants: Mapping[str, int] | None = None,
references: Sequence[str] = (),
models: Mapping[str, tuple[str, Sequence[str]]] | None = None,
Expand Down
Loading