Skip to content

Commit 3aeab7d

Browse files
Close the five guard bypasses the M0 review council found
Three independent reviewers (deepseek-r1, openai.gpt-5.6-sol, grok) read the M0 diff. Their verified findings, each now a failing-then-passing test: - A branch named lane/m1 or lane/m1x did not match ^lane/m(\d+)- and the ownership guard SKIPPED instead of failing. A branch under lane/ that is not lane/m<n>-<slug> now raises MalformedLaneBranchError and the guard fails. - `git diff --name-only` lists only the new path of a rename, so moving another lane's file into your own directory passed. Both diffs now run --no-renames, so the old path is classified as a deletion in its real owner's lane. - The CLI-example guard cut at the first shell metacharacter anywhere in the line, truncating quoted arguments like --msg "a | b". It now tokenises with shlex in punctuation mode and cuts only at an unquoted operator; trailing comments are dropped the same way the caller's shlex.split does. - The YAML-key guard skipped any block with no key overlapping the known set, so a block whose every key was misspelled passed. Skipping is now driven by an explicit list of non-config marker keys; a self-consistency test asserts the markers never overlap real keys, and it immediately caught `hosts`, which shared.py reads from config.yaml. - The known-key derivation reads loader source statically and is tied to the identifiers those loaders use; a refactor could shrink it without any doc noticing. A sentinel test now asserts four keys that must always be present. Arbitration and the rejected findings (with evidence) are in reviews/2026-09-02-full-repo-review/SIGNOFF-M0/ARBITRATION.md. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 288a8d1 commit 3aeab7d

3 files changed

Lines changed: 121 additions & 18 deletions

File tree

‎packages/studyloop/tests/_lane_ownership.py‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,34 @@
1616
_LANE_BRANCH_RE = re.compile(r"^lane/m(\d+)-")
1717

1818

19+
class MalformedLaneBranchError(ValueError):
20+
"""A branch under ``lane/`` that is not named ``lane/m<n>-<slug>``.
21+
22+
Raised rather than returning ``None`` so the guard FAILS on such a branch
23+
instead of skipping: with a plain ``None`` a branch named ``lane/m1`` or
24+
``lane/m1x`` would have dodged the ownership check entirely (M0 council
25+
finding A1, deepseek-r1 and openai.gpt-5.6-sol independently).
26+
"""
27+
28+
1929
def lane_id_from_branch(branch: str) -> str | None:
2030
"""Return the lane id (``"m3"``) for a lane branch, or ``None`` otherwise.
2131
2232
``None`` covers everything that isn't a lane branch at all (``main``,
23-
``integration/...``, a throwaway probe branch with the wrong prefix) --
24-
the caller's job is to skip in that case, not fail.
33+
``integration/...``, a probe branch without the ``lane/`` prefix) -- the
34+
caller's job is to skip in that case, not fail. A branch that IS under
35+
``lane/`` but does not fit ``lane/m<n>-<slug>`` raises
36+
:class:`MalformedLaneBranchError`, which the caller turns into a failure.
2537
"""
2638
match = _LANE_BRANCH_RE.match(branch)
27-
if not match:
28-
return None
29-
return f"m{match.group(1)}"
39+
if match:
40+
return f"m{match.group(1)}"
41+
if branch.startswith("lane/"):
42+
raise MalformedLaneBranchError(
43+
f"branch {branch!r} is under lane/ but is not named lane/m<n>-<slug>; "
44+
"the ownership guard cannot tell which lane it is and will not guess"
45+
)
46+
return None
3047

3148

3249
def _matches_any(path: str, patterns: list[str]) -> bool:

‎packages/studyloop/tests/test_docs_drift.py‎

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,39 @@
7979

8080
_STUDYLOOP_LINE_RE = re.compile(r"^\s*\$?\s*studyloop\b")
8181
_SESSION_QUERY_LINE_RE = re.compile(r"^\s*\$?\s*session-query\b")
82-
_SHELL_CUT_RE = re.compile(r"(\||>|&&|;)")
8382

8483

85-
def _cut_at_shell_metachar(line: str) -> str:
86-
"""Cut a doc example at the first shell pipe/redirect/chain operator.
84+
_SHELL_OPERATORS = frozenset({"|", "||", ">", ">>", "&&", ";", "&", "<"})
85+
8786

88-
``#`` comments are handled by ``shlex.split(..., comments=True)`` at the
89-
call site, not here, so quoted ``#`` inside an argument survives.
87+
def _cut_at_shell_metachar(line: str) -> str:
88+
"""Cut a doc example at the first UNQUOTED shell pipe/redirect/chain operator.
89+
90+
Tokenises with ``shlex`` in punctuation mode so ``studyloop x --msg "a | b"``
91+
keeps its quoted argument intact while ``studyloop x | head`` is cut before
92+
the pipe. The earlier regex cut at the first metacharacter anywhere in the
93+
line, which false-flagged valid quoted arguments (M0 council finding A4,
94+
openai.gpt-5.6-sol). Trailing ``# comments`` are dropped here too, so a
95+
parenthesised ``(-r)`` inside a comment is never mistaken for an option.
96+
97+
Returns a string (re-joined with ``shlex.join``) so the caller's tokenizer
98+
is unchanged; on a tokenisation error the original line is returned and
99+
the caller reports the error.
90100
"""
91-
match = _SHELL_CUT_RE.search(line)
92-
return line[: match.start()] if match else line
101+
# shlex's default commenters ("#") drop a trailing `# comment (-r)` here,
102+
# exactly as the caller's ``comments=True`` would; a quoted "#" survives.
103+
lexer = shlex.shlex(line, posix=True, punctuation_chars=True)
104+
lexer.whitespace_split = True
105+
try:
106+
tokens = list(lexer)
107+
except ValueError:
108+
return line
109+
kept: list[str] = []
110+
for token in tokens:
111+
if token in _SHELL_OPERATORS or (token and set(token) <= set("|>&;<")):
112+
break
113+
kept.append(token)
114+
return shlex.join(kept)
93115

94116

95117
def _iter_fenced_lines(doc_path: Path, pattern: re.Pattern[str]) -> list[tuple[int, str]]:
@@ -446,6 +468,45 @@ def _yaml_block_params() -> list:
446468
return params
447469

448470

471+
#: Top-level keys that identify a fenced YAML block as something other than a
472+
#: studyloop config.yaml example: an Ansible playbook (name/hosts/tasks), an
473+
#: Obsidian note's frontmatter (type/id/created/tags), a GitHub workflow
474+
#: (on/jobs), an mkdocs config (site_name). A block is skipped ONLY when one of
475+
#: these is present. The earlier rule -- skip when no key overlaps a known key --
476+
#: let a block whose every key was misspelled pass silently (M0 council finding
477+
#: A5, openai.gpt-5.6-sol); now such a block fails with the unknown keys listed.
478+
_NON_CONFIG_MARKER_KEYS: frozenset[str] = frozenset(
479+
{"name", "tasks", "type", "id", "created", "on", "jobs", "site_name"}
480+
)
481+
482+
#: Keys that must ALWAYS be in the derived known-key set. The derivation reads
483+
#: loader source statically and is tied to the identifiers those loaders use; a
484+
#: refactor that renamed one (or the earlier monkeypatch-induced shrink recorded
485+
#: in evidence/M0/0.8-doc-drift/01-drift-found.md) would otherwise only surface
486+
#: when a doc happened to mention the lost key (M0 council finding A2,
487+
#: deepseek-r1). Extend this list when a new section is added to config.yaml.
488+
_SENTINEL_KNOWN_KEYS: frozenset[str] = frozenset({"web_port", "browser", "lan_password", "tts"})
489+
490+
491+
def test_non_config_markers_never_overlap_real_config_keys() -> None:
492+
"""A marker that is also a real key would silently skip real config blocks.
493+
494+
`hosts` was in the first marker list and IS a studyloop key (shared.py
495+
`_resolve_hosts`); this test is what would have caught it.
496+
"""
497+
overlap = _NON_CONFIG_MARKER_KEYS & _known_top_level_keys()
498+
assert not overlap, f"marker key(s) {sorted(overlap)} are real config keys; remove them"
499+
500+
501+
def test_known_key_derivation_has_not_shrunk() -> None:
502+
known = _known_top_level_keys()
503+
missing = _SENTINEL_KNOWN_KEYS - known
504+
assert not missing, (
505+
f"known-key derivation lost {sorted(missing)}; a loader was probably refactored "
506+
f"(see _known_top_level_keys). Derived set: {sorted(known)}"
507+
)
508+
509+
449510
@pytest.mark.parametrize(("doc_name", "block_index"), _yaml_block_params())
450511
def test_yaml_block_keys_are_known(doc_name: str, block_index: int) -> None:
451512
_idx, start_line, text = _iter_yaml_blocks(DOCS_DIR / doc_name)[block_index]
@@ -461,10 +522,11 @@ def test_yaml_block_keys_are_known(doc_name: str, block_index: int) -> None:
461522

462523
known = _known_top_level_keys()
463524
top_level = set(parsed.keys())
464-
if not (top_level & known):
525+
markers = top_level & _NON_CONFIG_MARKER_KEYS
526+
if markers:
465527
pytest.skip(
466-
f"{location}: no key overlaps a known studyloop config key "
467-
f"({sorted(top_level)}); not a studyloop config.yaml example"
528+
f"{location}: top-level key(s) {sorted(markers)} mark this block as "
529+
"not a studyloop config.yaml example (playbook / frontmatter / workflow)"
468530
)
469531

470532
unknown = top_level - known

‎packages/studyloop/tests/test_lane_ownership.py‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
from _lane_ownership import ( # noqa: E402
4040
LaneOwnershipResult,
41+
MalformedLaneBranchError,
4142
classify_files,
4243
lane_id_from_branch,
4344
)
@@ -68,6 +69,21 @@ def test_lane_id_from_branch(branch: str, expected: str | None) -> None:
6869
assert lane_id_from_branch(branch) == expected
6970

7071

72+
@pytest.mark.parametrize(
73+
"branch",
74+
[
75+
pytest.param("lane/m1", id="no-hyphen-no-slug"),
76+
pytest.param("lane/m1x", id="letter-after-digit"),
77+
pytest.param("lane/foo-bar", id="no-milestone"),
78+
pytest.param("lane/", id="bare-prefix"),
79+
],
80+
)
81+
def test_malformed_lane_branch_is_an_error_not_a_skip(branch: str) -> None:
82+
"""A1: a branch under lane/ that dodges the pattern must fail, not skip."""
83+
with pytest.raises(MalformedLaneBranchError):
84+
lane_id_from_branch(branch)
85+
86+
7187
# ---------------------------------------------------------------------------
7288
# Pure logic: path classification against a small fixture map
7389
# ---------------------------------------------------------------------------
@@ -168,8 +184,13 @@ def _changed_files() -> list[str]:
168184
plus whatever is currently staged/unstaged/untracked in the worktree."""
169185
base = _merge_base_ref()
170186
merge_base = _git("merge-base", base, "HEAD").strip()
171-
committed = _git("diff", "--name-only", f"{merge_base}..HEAD")
172-
working = _git("diff", "--name-only", "HEAD")
187+
# --no-renames: a rename would otherwise list only its NEW path, so moving
188+
# another lane's file into your own lane's directory would pass the guard
189+
# (M0 council finding A3, reproduced by openai.gpt-5.6-sol's reviewer in a
190+
# scratch repo). With renames disabled both the deleted old path and the
191+
# added new path are listed and each is classified on its own.
192+
committed = _git("diff", "--name-only", "--no-renames", f"{merge_base}..HEAD")
193+
working = _git("diff", "--name-only", "--no-renames", "HEAD")
173194
untracked = _git("ls-files", "--others", "--exclude-standard")
174195
files = {
175196
line.strip()
@@ -187,7 +208,10 @@ def _load_map() -> tuple[dict[str, list[str]], list[str]]:
187208

188209
def test_lane_stays_in_its_lane() -> None:
189210
branch = _current_branch()
190-
lane = lane_id_from_branch(branch)
211+
try:
212+
lane = lane_id_from_branch(branch)
213+
except MalformedLaneBranchError as exc:
214+
pytest.fail(str(exc))
191215
if lane is None:
192216
pytest.skip(f"not on a lane branch ({branch!r}); nothing to police here")
193217
if lane == "m0":

0 commit comments

Comments
 (0)