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
5 changes: 4 additions & 1 deletion .claude/skills/impl-review-loop/steps/preparation.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ python3 .claude/skills/issue-refinement-loop/scripts/plan_issue_scope_rollup.py
**orchestrator の判断ルール**:

- `confidence: high` の候補が存在する場合: orchestrator は各候補の `suggested_action` を確認し、統合実施可否を判断してから次ステップに進む。自動実行しない。
- `security` / `auth` / `permission` / `sandbox` 関連の候補(`suggested_action: human_review_required`): 即時停止して人間が判断する(`termination_reason: human_escalation`)。
- `suggested_action: human_review_required`(`escalation_required: True` の genuine structural conflict): 即時停止して人間が判断する(`termination_reason: human_escalation`)。
- `suggested_action: proceed_with_coordination` の候補: 関連 Issue 番号を `LOOP_STATE.scope_rollup_decision.related_coordination[]` に構造化保存して次ステップへ継続する。PR body への `## Related coordination` 反映は PR 作成・更新フェーズの責務とし、preparation フェーズでは担わない。
- **`proceed_with_coordination` の意味**: 「安全に並行可能」ではなく「停止せず、coordination 証跡付きで進める」に限定する。同一ファイルの変更が独立していることは証明されていない(machine-readable anchors がなく CONFLICT_UNCERTAIN)。
- **PR 作成・更新フェーズ(open-pr skill)の責務**: `LOOP_STATE.scope_rollup_decision.related_coordination[]` を読み取り、PR body の `## Related coordination` セクションに関連 Issue 番号とメモを反映すること。`related_coordination` が空の場合はセクションを省略してよい。
- `confidence: medium` の候補: LOOP_STATE に記録し、推奨アクションを提示するが自動実行しない。
- `confidence: low` または候補なし: 記録してそのまま次ステップに進む。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
ACTION_CREATE_PARENT_ROLLUP_ISSUE = "create_parent_rollup_issue"
ACTION_KEEP_SEPARATE_WITH_REASON = "keep_separate_with_reason"
ACTION_HUMAN_REVIEW_REQUIRED = "human_review_required"
ACTION_PROCEED_WITH_COORDINATION = "proceed_with_coordination"

# Regex for security-related keyword detection (word-boundary based).
# "auth" and "token" are intentionally excluded to avoid false positives on
Expand Down Expand Up @@ -373,7 +374,8 @@ def _build_scope_context(
- same file + disjoint sub-file anchors -> same_file_disjoint_anchor -> no escalate
(this is the #547/#549 case)
- exact same file set with NO machine-readable anchors to prove disjointness ->
uncertain -> escalate (fail-safe; we cannot confirm the changes are independent)
uncertain -> proceed_with_coordination (escalation_required=False; disjointness
cannot be proven but this is not a genuine structural conflict)
- partial path intersection without anchors -> same_file_disjoint_anchor -> no escalate
- prefix-only overlap (no exact intersection) -> prefix_overlap_uncertain; the
"parent -> child" pair is recorded in anchor_paths (never collapsed to "none").
Expand Down Expand Up @@ -431,9 +433,10 @@ def _build_scope_context(
escalation_required = False
elif exact_same_files:
# Exact same file set but no machine-readable anchors to prove disjointness.
# Cannot confirm independence -> fail-safe to human review.
# Disjointness cannot be proven, but this is not a genuine structural conflict
# (no shared anchors). Proceed with coordination instead of blocking.
conflict_type = CONFLICT_UNCERTAIN
escalation_required = True
escalation_required = False
else:
# Partial path intersection without anchors — different file scopes overlap;
# treat as disjoint to avoid false escalation.
Expand Down Expand Up @@ -553,20 +556,42 @@ def _suggested_action(

Escalation logic (revised from keyword-only to scope_context-based):
- Previously: any security keyword match -> human_review_required
- Now: escalation is driven by scope_context.escalation_required (structural conflict)
OR scope_context.conflict_type == uncertain.
- Now: escalation is driven by scope_context.escalation_required (genuine structural
conflict — same_anchor_conflicting_operation with shared sub-file anchors).
Security keywords alone (without structural conflict) are recorded in
security_match_evidence for audit but do NOT trigger escalation.

Evaluation order (CONFLICT_UNCERTAIN is checked BEFORE escalation_required):
1. conflict_type == uncertain -> proceed_with_coordination
uncertain = same file set, no machine-readable anchors to prove disjointness.
This is NOT a genuine structural conflict; escalation_required is always False
for CONFLICT_UNCERTAIN. However, the order is explicit so that a stale or
erroneously-set escalation_required flag cannot override the CONFLICT_UNCERTAIN
routing to proceed_with_coordination.
2. escalation_required == True -> human_review_required
Genuine structural conflict (same_anchor_conflicting_operation).

Responsibility boundary:
- CONFLICT_UNCERTAIN: "same file set, cannot prove disjointness" — proceed with
coordination, NOT blocked. Coordination evidence is recorded in
LOOP_STATE.scope_rollup_decision.related_coordination[].
- same_anchor_conflicting_operation: both issues target the same sub-file anchor
(JSON Pointer / property name) — genuine conflict, escalate.

This prevents false positives where schema property names or metadata field names
containing security-adjacent words (e.g. "secret_policy") cause spurious stops.
The domain_flags field in scope_context serves as the audit trail for keyword matches.
"""
# Use scope_context for escalation decisions if available
# Use scope_context for escalation decisions if available.
# CONFLICT_UNCERTAIN is evaluated FIRST to prevent a stale escalation_required flag
# from overriding the proceed_with_coordination routing.
if scope_context is not None:
if scope_context.get("escalation_required", False):
return ACTION_HUMAN_REVIEW_REQUIRED
if scope_context.get("conflict_type") == CONFLICT_UNCERTAIN:
# uncertain = same file set, no machine-readable anchors to prove disjointness.
# This is NOT a genuine structural conflict (escalation_required=False).
# Proceed with coordination rather than blocking.
return ACTION_PROCEED_WITH_COORDINATION
if scope_context.get("escalation_required", False):
return ACTION_HUMAN_REVIEW_REQUIRED

if confidence == CONFIDENCE_HIGH:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ def test_scope_rollup_escalates_uncertain_or_boundary_affecting_changes(
f"Same-anchor conflict must escalate; got {cc['suggested_action']!r}"
)

# Path 2: exact same file, no anchors -> uncertain -> escalate
# Path 2: exact same file, no anchors -> uncertain -> proceed_with_coordination (not escalate)
plan_uncertain = _run(
issues_exact_same_file_no_anchor_json,
empty_prs_json,
Expand All @@ -944,8 +944,11 @@ def test_scope_rollup_escalates_uncertain_or_boundary_affecting_changes(
assert uc["scope_context"]["conflict_type"] == rollup.CONFLICT_UNCERTAIN, (
f"Expected uncertain, got {uc['scope_context']['conflict_type']!r}"
)
assert uc["suggested_action"] == rollup.ACTION_HUMAN_REVIEW_REQUIRED, (
f"Uncertain conflict must escalate; got {uc['suggested_action']!r}"
assert uc["scope_context"]["escalation_required"] is False, (
"uncertain conflict_type must have escalation_required=False"
)
assert uc["suggested_action"] == rollup.ACTION_PROCEED_WITH_COORDINATION, (
f"Uncertain conflict must return proceed_with_coordination; got {uc['suggested_action']!r}"
)

def test_scope_context_and_ordering_constraint_are_additive_fields(
Expand Down Expand Up @@ -1059,3 +1062,252 @@ def test_prefix_overlap_records_anchor_paths_and_is_not_classified_none(
assert any("->" in p for p in sc["anchor_paths"]), (
f"Expected a parent->child pair in anchor_paths, got {sc['anchor_paths']!r}"
)


# ---------------------------------------------------------------------------
# #607: proceed_with_coordination replaces uncertain escalation
# ---------------------------------------------------------------------------


class TestProceedWithCoordination:
"""#607: uncertain conflict type returns proceed_with_coordination, not human_review_required."""

def test_same_allowed_paths_no_anchor_should_proceed_with_coordination(
self, tmp_path: Path, empty_prs_json: str
) -> None:
"""GIVEN exact same Allowed Paths AND no machine-readable sub-file anchors,
WHEN plan is generated,
THEN suggested_action is proceed_with_coordination (not human_review_required).
"""
schema_path = "- `.claude/skills/some-skill/SKILL.md`\n"
issues = [
{
"number": 1100,
"title": "実装: SKILL.md を更新する X",
"body": (
"## Allowed Paths\n"
+ schema_path
+ "\n## Outcome\nSKILL.md を更新する。\n"
),
},
{
"number": 1101,
"title": "実装: SKILL.md を更新する Y",
"body": (
"## Allowed Paths\n"
+ schema_path
+ "\n## Outcome\nSKILL.md を別の観点で更新する。\n"
),
},
]
issues_path = tmp_path / "issues_same_paths_no_anchor.json"
issues_path.write_text(json.dumps(issues), encoding="utf-8")

plan = _run(str(issues_path), empty_prs_json, current_issue_number=1100)
candidates = [c for c in plan["candidates"] if c.get("number") == 1101]
assert candidates, "Expected #1101 to appear as a candidate of #1100"
c = candidates[0]

assert c["scope_context"]["conflict_type"] == rollup.CONFLICT_UNCERTAIN, (
f"Expected uncertain, got {c['scope_context']['conflict_type']!r}"
)
assert c["scope_context"]["escalation_required"] is False, (
"uncertain conflict must have escalation_required=False"
)
assert c["suggested_action"] == rollup.ACTION_PROCEED_WITH_COORDINATION, (
f"Expected proceed_with_coordination, got {c['suggested_action']!r}"
)

def test_permission_keyword_only_should_not_escalate(self) -> None:
"""GIVEN body contains standalone 'permission' (SECURITY_RE match) AND
scope_context has conflict_type=uncertain AND escalation_required=False,
WHEN _suggested_action is called,
THEN action is proceed_with_coordination (not human_review_required).

This exercises the actual word-boundary `permission` keyword path (unlike
'permissionMode' which does NOT match \\bpermission\\b). The test verifies that
security_match_evidence containing "permission" does NOT drive escalation — only
escalation_required (genuine structural conflict) drives human_review_required.
This reproduces the #569/#597 false-positive: an unrelated Issue containing
standalone 'permission' caused human_escalation due to keyword+same-paths match.
"""
# Directly test _suggested_action with a scope_context that includes the
# standalone 'permission' evidence but CONFLICT_UNCERTAIN (no shared anchors).
# CONFLICT_UNCERTAIN must be evaluated before escalation_required.
action = rollup._suggested_action(
item={},
signals=[],
confidence=rollup.CONFIDENCE_MEDIUM,
scope_context={
"conflict_type": rollup.CONFLICT_UNCERTAIN,
"security_match_evidence": ["permission"],
"escalation_required": False,
},
)
assert action == rollup.ACTION_PROCEED_WITH_COORDINATION, (
f"permission keyword in security_match_evidence must not escalate; "
f"got {action!r}"
)

def test_permission_keyword_word_boundary_triggers_security_match_evidence(
self, tmp_path: Path, empty_prs_json: str
) -> None:
"""Verify that standalone 'permission' (not 'permissionMode') is detected by
SECURITY_RE and recorded in security_match_evidence — but does NOT escalate.

Integration path: body with standalone 'permission' + same Allowed Paths + no
anchors -> CONFLICT_UNCERTAIN -> proceed_with_coordination.
"""
shared_path = "- `.claude/skills/impl-review-loop/steps/preparation.md`\n"
issues = [
{
"number": 1200,
"title": "実装: preparation.md に proceed_with_coordination を追加する",
"body": (
"## Allowed Paths\n"
+ shared_path
+ "\n## Outcome\nキーワードベース停止を廃止する。\n"
),
},
{
"number": 1201,
"title": "実装: preparation.md の permission 設定を更新する",
"body": (
"## Allowed Paths\n"
+ shared_path
+ "\n## Outcome\npermission チェックの挙動を変更する。\n"
),
},
]
issues_path = tmp_path / "issues_permission_keyword_integration.json"
issues_path.write_text(json.dumps(issues), encoding="utf-8")

plan = _run(str(issues_path), empty_prs_json, current_issue_number=1200)
candidates = [c for c in plan["candidates"] if c.get("number") == 1201]
assert candidates, "Expected #1201 to appear as a candidate of #1200"
c = candidates[0]

# The standalone 'permission' keyword IS detected by SECURITY_RE
assert "permission" in c.get("security_match_evidence", []), (
"Standalone 'permission' must be recorded in security_match_evidence for audit"
)
# But it must NOT cause escalation — CONFLICT_UNCERTAIN -> proceed_with_coordination
assert c["scope_context"]["conflict_type"] == rollup.CONFLICT_UNCERTAIN, (
f"Expected uncertain (same paths, no anchors), got {c['scope_context']['conflict_type']!r}"
)
assert c["suggested_action"] != rollup.ACTION_HUMAN_REVIEW_REQUIRED, (
f"permission keyword alone must not escalate; got {c['suggested_action']!r}"
)
assert c["suggested_action"] == rollup.ACTION_PROCEED_WITH_COORDINATION, (
f"Expected proceed_with_coordination, got {c['suggested_action']!r}"
)

def test_same_anchor_conflicting_op_should_escalate(
self, tmp_path: Path, empty_prs_json: str
) -> None:
"""GIVEN both issues target the same sub-file anchor,
WHEN plan is generated,
THEN suggested_action is human_review_required (genuine structural conflict).
"""
shared_path = "- `.claude/skills/some-skill/schema/contract.schema.json`\n"
issues = [
{
"number": 1300,
"title": "実装: schema の `/required` に field A を追加する",
"body": (
"## Allowed Paths\n"
+ shared_path
+ "\n## Outcome\n`/required` に field A を追加する。\n"
),
},
{
"number": 1301,
"title": "実装: schema の `/required` から field B を削除する",
"body": (
"## Allowed Paths\n"
+ shared_path
+ "\n## Outcome\n`/required` から field B を削除する。\n"
),
},
]
issues_path = tmp_path / "issues_same_anchor_escalate.json"
issues_path.write_text(json.dumps(issues), encoding="utf-8")

plan = _run(str(issues_path), empty_prs_json, current_issue_number=1300)
candidates = [c for c in plan["candidates"] if c.get("number") == 1301]
assert candidates, "Expected #1301 to appear as a candidate of #1300"
c = candidates[0]

assert c["scope_context"]["conflict_type"] == rollup.CONFLICT_SAME_ANCHOR_CONFLICTING_OP, (
f"Expected same_anchor_conflicting_operation, got {c['scope_context']['conflict_type']!r}"
)
assert c["scope_context"]["escalation_required"] is True
assert c["suggested_action"] == rollup.ACTION_HUMAN_REVIEW_REQUIRED, (
f"Genuine structural conflict must escalate; got {c['suggested_action']!r}"
)

def test_prefix_overlap_uncertain_should_not_escalate(
self, tmp_path: Path, empty_prs_json: str
) -> None:
"""GIVEN prefix-only path overlap (no exact intersection) AND no shared anchors,
WHEN plan is generated,
THEN conflict_type is prefix_overlap_uncertain AND suggested_action is NOT human_review_required.
"""
issues = [
{
"number": 1400,
"title": "実装: skill dir を更新する",
"body": (
"## Allowed Paths\n"
"- `.claude/skills/some-skill`\n"
"\n## Outcome\nディレクトリ全体を更新する。\n"
),
},
{
"number": 1401,
"title": "実装: skill の README を更新する",
"body": (
"## Allowed Paths\n"
"- `.claude/skills/some-skill/README.md`\n"
"\n## Outcome\nREADME.md を更新する。\n"
),
},
]
issues_path = tmp_path / "issues_prefix_overlap.json"
issues_path.write_text(json.dumps(issues), encoding="utf-8")

plan = _run(str(issues_path), empty_prs_json, current_issue_number=1400)
candidates = [c for c in plan["candidates"] if c.get("number") == 1401]
assert candidates, "Expected #1401 to appear as a candidate of #1400"
c = candidates[0]

assert c["scope_context"]["conflict_type"] == rollup.CONFLICT_PREFIX_OVERLAP_UNCERTAIN, (
f"Expected prefix_overlap_uncertain, got {c['scope_context']['conflict_type']!r}"
)
assert c["suggested_action"] != rollup.ACTION_HUMAN_REVIEW_REQUIRED, (
f"Prefix overlap without shared anchors must not escalate; got {c['suggested_action']!r}"
)

def test_uncertain_conflict_takes_precedence_over_stale_escalation_flag(self) -> None:
"""GIVEN conflict_type is CONFLICT_UNCERTAIN AND escalation_required is True
(stale / erroneously-set flag),
WHEN _suggested_action is called,
THEN action is proceed_with_coordination — CONFLICT_UNCERTAIN is evaluated first.

This guards against a regression where escalation_required=True could override
the CONFLICT_UNCERTAIN routing even though CONFLICT_UNCERTAIN is definitionally
NOT a genuine structural conflict.
"""
action = rollup._suggested_action(
item={},
signals=[],
confidence=rollup.CONFIDENCE_HIGH,
scope_context={
"conflict_type": rollup.CONFLICT_UNCERTAIN,
"escalation_required": True,
},
)
assert action == rollup.ACTION_PROCEED_WITH_COORDINATION, (
f"CONFLICT_UNCERTAIN must take precedence over stale escalation_required=True; "
f"got {action!r}"
)
Loading