From 39f0d95c7d926c93a4b29b9552b60be9a6141909 Mon Sep 17 00:00:00 2001 From: aloekun Date: Thu, 7 May 2026 23:22:45 +0900 Subject: [PATCH] =?UTF-8?q?feat(hooks-post-tool-linter):=20no-time-field-s?= =?UTF-8?q?trict-greater=20rule=20(=E9=A0=86=E4=BD=8D=2047=20/=20=C2=A7A-2?= =?UTF-8?q?=20P-2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit §A-2 Phase 5 dogfood P-2。PR #101 で実証された latent drift (parse_findings 系 3 関数で created_at > push_time vs >= の混在) を構造的に防止する custom lint rule を追加。Bundle Z #B-α と同じ「決定論的防止層」哲学。 ルール仕様: - pattern: \b(created_at|submitted_at|updated_at|comment_event_time| event_time|comment_created_at|published_at|posted_at|commented_at) \s*>\s*[a-zA-Z_] - direction: `>` のみ flag (`<` は is_stale 等で意図的 exclusive 境界 の正しい用途があるため scope 外) - target: .rs files only 検証: - 11 unit tests (4 detect 系 + 7 skip 系) + 1 helper 関数 で transition matrix を網羅 - 全 75 tests passed - 既存 codebase で 0 violations (created_at >= は doc コメントのみ、 is_stale の created_at < は `<` で対象外) build + deploy 済 (.claude/hooks-post-tool-linter.exe)。 --- .claude/custom-lint-rules.toml | 37 ++++++ src/hooks-post-tool-linter/src/main.rs | 164 ++++++++++++++++++++++++- 2 files changed, 200 insertions(+), 1 deletion(-) diff --git a/.claude/custom-lint-rules.toml b/.claude/custom-lint-rules.toml index ecb14037..6b3fab08 100644 --- a/.claude/custom-lint-rules.toml +++ b/.claude/custom-lint-rules.toml @@ -200,3 +200,40 @@ steps = [ [rules.example] bad = 'BLOCK_MESSAGE: &str = "詳細: docs/todoN.md (N = 数字) の \"
\" 参照";' good = 'BLOCK_MESSAGE: &str = "詳細: docs/adr/adr-NNN-feature.md 参照";' + +# ─── ルール⑦: Rust 時刻フィールドの strict `>` 比較禁止 (boundary inconsistency 防止) ─── +# +# 由来: PR #101 で `parse_listed_findings` の `c.created_at > push_time` が CodeRabbit +# Minor 指摘 → 同一ファイル内 3 関数 (parse_listed_findings / parse_new_comments / +# parse_findings) で `>` vs `>=` の latent drift が判明 (parse_rate_limit のみ既に `>=`)。 +# 時刻フィールドの strict `>` は境界 (push_time と同時刻のイベント) を silently 除外 +# するため、`>=` への揃えを構造的に強制する。 +# +# 由来: PR #101 post-merge-feedback Tier 1 #2 採用、Bundle Z #B-α と同じ +# 「決定論的防止層」哲学 (ADR-007 の正規表現層)。 +# +# Direction-specific 設計 (`>` のみ flag): +# `<` の strict 比較は別文脈 (例: is_stale の `created_at < threshold` = +# TTL 内/外判定) で意図的な exclusive 境界として正しいケースが多いため +# false positive 回避のため scope を `>` に限定。`<` 側の boundary 問題は +# 観測されたら別 rule で対応する。 + +[[rules]] +id = "no-time-field-strict-greater" +pattern = '\b(created_at|submitted_at|updated_at|comment_event_time|event_time|comment_created_at|published_at|posted_at|commented_at)\s*>\s*[a-zA-Z_]' +severity = "warning" +message = "時刻フィールドの strict `>` 比較を検出。境界 (同時刻イベント) を silently 除外するため `>=` 比較への揃えを推奨" +why = "PR #101 で同一ファイル内 3 関数で `>` vs `>=` の latent drift が判明 (parse_listed_findings の `c.created_at > push_time` が CR Minor 指摘 → 揃え修正)。時刻フィールドの strict inequality は境界の inclusive/exclusive choice を不可視にし、bug class を生む。`>=` への揃えで boundary を明示する" +extensions = ["rs"] + +[rules.fix] +strategy = "`>=` への置き換え (境界 inclusive)" +steps = [ + "`field > rhs` を `field >= rhs` に変更 (同時刻のイベントを含める)", + "意図的に exclusive にする場合: 比較対象を 1 単位ずらす (`field >= rhs.checked_add(1)?` 等) かコメントで意図を明示", + "false positive (時刻でない `created_at` 等) の場合: 変数名を時刻と紛らわしくない名前にリネーム", +] + +[rules.example] +bad = "comments.iter().filter(|c| c.created_at > push_time)" +good = "comments.iter().filter(|c| c.created_at >= push_time)" diff --git a/src/hooks-post-tool-linter/src/main.rs b/src/hooks-post-tool-linter/src/main.rs index 2a1984e4..b123b05f 100644 --- a/src/hooks-post-tool-linter/src/main.rs +++ b/src/hooks-post-tool-linter/src/main.rs @@ -1346,7 +1346,6 @@ extensions = ["ts", "js"] #[test] fn md_mutable_anchor_skips_external_url_with_fragment() { - // 外部 URL の fragment は GFM anchor ではないため誤検知すべきでない let dir = tempfile::tempdir().unwrap(); let file = write_file( dir.path(), @@ -1357,4 +1356,167 @@ extensions = ["ts", "js"] let violations = run_custom_rules(file.to_str().unwrap(), &rules); assert!(violations.is_empty()); } + + fn rs_time_field_strict_greater_rule() -> CustomRule { + make_test_rule( + "no-time-field-strict-greater", + r"\b(created_at|submitted_at|updated_at|comment_event_time|event_time|comment_created_at|published_at|posted_at|commented_at)\s*>\s*[a-zA-Z_]", + &["rs"], + ) + } + + fn build_rs_source_with_op(field_lhs: &str, op: &str, rhs: &str) -> String { + format!("fn f() {{ items.iter().filter(|c| c.{field_lhs} {op} {rhs}); }}\n") + } + + fn build_doc_comment_source(field_lhs: &str, op: &str, rhs: &str) -> String { + format!("/// `{field_lhs} {op} {rhs}` (epoch 0 で実質全件)\nfn f() {{}}\n") + } + + fn build_toml_with_field(field_lhs: &str, op: &str, rhs: &str) -> String { + format!("comment = \"{field_lhs} {op} {rhs}\"\n") + } + + #[test] + fn rs_time_field_strict_greater_detects_created_at_gt_push_time() { + let dir = tempfile::tempdir().unwrap(); + let file = write_file( + dir.path(), + "parse.rs", + &build_rs_source_with_op("created_at", ">", "push_time"), + ); + let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]); + let violations = run_custom_rules(file.to_str().unwrap(), &rules); + assert_eq!(violations.len(), 1); + } + + #[test] + fn rs_time_field_strict_greater_detects_submitted_at_gt_since() { + let dir = tempfile::tempdir().unwrap(); + let file = write_file( + dir.path(), + "parse.rs", + &build_rs_source_with_op("submitted_at", ">", "since"), + ); + let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]); + let violations = run_custom_rules(file.to_str().unwrap(), &rules); + assert_eq!(violations.len(), 1); + } + + #[test] + fn rs_time_field_strict_greater_detects_updated_at_gt_threshold() { + let dir = tempfile::tempdir().unwrap(); + let file = write_file( + dir.path(), + "parse.rs", + &build_rs_source_with_op("updated_at", ">", "threshold"), + ); + let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]); + let violations = run_custom_rules(file.to_str().unwrap(), &rules); + assert_eq!(violations.len(), 1); + } + + #[test] + fn rs_time_field_strict_greater_detects_comment_event_time() { + let dir = tempfile::tempdir().unwrap(); + let file = write_file( + dir.path(), + "parse.rs", + &build_rs_source_with_op("comment_event_time", ">", "now"), + ); + let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]); + let violations = run_custom_rules(file.to_str().unwrap(), &rules); + assert_eq!(violations.len(), 1); + } + + #[test] + fn rs_time_field_strict_greater_skips_inclusive_comparison() { + let dir = tempfile::tempdir().unwrap(); + let file = write_file( + dir.path(), + "parse.rs", + &build_rs_source_with_op("created_at", ">=", "push_time"), + ); + let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]); + let violations = run_custom_rules(file.to_str().unwrap(), &rules); + assert!(violations.is_empty()); + } + + #[test] + fn rs_time_field_strict_greater_skips_strict_less_than() { + let dir = tempfile::tempdir().unwrap(); + let file = write_file( + dir.path(), + "stale.rs", + &build_rs_source_with_op("created_at", "<", "threshold"), + ); + let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]); + let violations = run_custom_rules(file.to_str().unwrap(), &rules); + assert!(violations.is_empty()); + } + + #[test] + fn rs_time_field_strict_greater_skips_le_inclusive() { + let dir = tempfile::tempdir().unwrap(); + let file = write_file( + dir.path(), + "parse.rs", + &build_rs_source_with_op("created_at", "<=", "cutoff"), + ); + let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]); + let violations = run_custom_rules(file.to_str().unwrap(), &rules); + assert!(violations.is_empty()); + } + + #[test] + fn rs_time_field_strict_greater_skips_numeric_rhs() { + let dir = tempfile::tempdir().unwrap(); + let file = write_file( + dir.path(), + "parse.rs", + &build_rs_source_with_op("created_at", ">", "0"), + ); + let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]); + let violations = run_custom_rules(file.to_str().unwrap(), &rules); + assert!(violations.is_empty()); + } + + #[test] + fn rs_time_field_strict_greater_skips_doc_comment_with_inclusive() { + let dir = tempfile::tempdir().unwrap(); + let file = write_file( + dir.path(), + "doc.rs", + &build_doc_comment_source("created_at", ">=", "push_time"), + ); + let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]); + let violations = run_custom_rules(file.to_str().unwrap(), &rules); + assert!(violations.is_empty()); + } + + #[test] + fn rs_time_field_strict_greater_skips_unrelated_field() { + let dir = tempfile::tempdir().unwrap(); + let file = write_file( + dir.path(), + "parse.rs", + &build_rs_source_with_op("count", ">", "limit"), + ); + let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]); + let violations = run_custom_rules(file.to_str().unwrap(), &rules); + assert!(violations.is_empty()); + } + + #[test] + fn rs_time_field_strict_greater_only_targets_rs() { + let dir = tempfile::tempdir().unwrap(); + let file = write_file( + dir.path(), + "config.toml", + &build_toml_with_field("created_at", ">", "push_time"), + ); + let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]); + let violations = run_custom_rules(file.to_str().unwrap(), &rules); + assert!(violations.is_empty()); + } }