File size: 5,611 Bytes
c373be7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19aab86
c373be7
 
 
 
 
 
 
 
907dbd6
7aaa36c
c373be7
 
c10a376
 
cad04d1
 
 
 
c10a376
 
 
 
 
 
c373be7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cad04d1
c373be7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19aab86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c373be7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aaa36c
c10a376
c373be7
 
 
 
7aaa36c
c373be7
 
 
 
 
 
7aaa36c
c373be7
 
7aaa36c
19aab86
 
c373be7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""Classify changed paths for CI workflow decisions."""

from __future__ import annotations

import argparse
import fnmatch
import os
from pathlib import Path
from typing import Iterable

OUTPUT_NAMES = (
    "browser_changed",
    "ci_changed",
    "docs_changed",
    "docs_only",
    "graph_artifact_changed",
    "graph_changed",
    "graph_only",
    "package_changed",
    "similarity_changed",
    "source_changed",
    "telemetry_changed",
)

DOCS_PATTERNS = (
    "*.md",
    "docs/**",
    "graph/README.md",
    "LICENSE",
    "mkdocs.yml",
    "qa/bug_smoke_status.csv",
    "qa/feature_status.csv",
    "requirements-docs.txt",
)
CI_PATTERNS = (
    ".github/actions/**",
    ".github/codeql/**",
    ".github/dependabot.yml",
    ".github/pip-audit-ignore.txt",
    ".github/requirements-no-test-policy.txt",
    ".github/workflows/**",
    ".no-mistakes.yaml",
    "scripts/ci_*.py",
    "scripts/local_fast_gate.py",
    "scripts/no_mistakes_run.sh",
)
GRAPH_ARTIFACT_PATTERNS = (
    "graph/communities.json",
    "graph/entity-overlays.jsonl",
    "graph/skills-sh-catalog.json.gz",
    "graph/wiki-graph-runtime.tar.gz",
    "graph/wiki-graph.tar.gz",
    "graph/*.html",
)
BROWSER_PATTERNS = (
    ".github/workflows/test.yml",
    "dashboard/**",
    "pyproject.toml",
    "src/**/browser/**",
    "src/**/monitor/**",
    "src/ctx_monitor.py",
    "src/ctx/utils/_safe_name.py",
    "src/tests/test_ctx_monitor_browser.py",
)
PACKAGE_PATTERNS = (
    "MANIFEST.in",
    "pyproject.toml",
    "scripts/build_reproducible_dist.py",
    "src/*.py",
    "src/ctx/**",
)
SOURCE_PATTERNS = (
    "hooks/**",
    "pyproject.toml",
    "scripts/**",
    "src/**",
)
SIMILARITY_PATTERNS = (
    ".github/workflows/test.yml",
    "graph/entity-overlays.jsonl",
    "pyproject.toml",
    "src/corpus_cache.py",
    "src/config.json",
    "src/cosine_ranker.py",
    "src/ctx/adapters/claude_code/hooks/context_monitor.py",
    "src/ctx/adapters/generic/ctx_core_tools.py",
    "src/ctx_config.py",
    "src/ctx/core/graph/**",
    "src/ctx/core/resolve/**",
    "src/ctx/core/wiki/wiki_graphify.py",
    "src/embedding_backend.py",
    "src/intake_gate.py",
    "src/tests/test_similarity_precision_recall.py",
)
TELEMETRY_PATTERNS = (
    ".github/workflows/test.yml",
    "docs/telemetry.md",
    "scripts/ci_classifier.py",
    "scripts/ci_preflight.py",
    "scripts/ci_required.py",
    "src/config.json",
    "src/ctx/config.json",
    "src/ctx/adapters/generic/ctx_core_tools.py",
    "src/ctx/adapters/generic/runtime_lifecycle.py",
    "src/ctx/api.py",
    "src/ctx/cli/run.py",
    "src/ctx/cli/telemetry.py",
    "src/ctx/mcp_server/server.py",
    "src/ctx/telemetry/**",
    "src/tests/test_enterprise_telemetry.py",
    "src/tests/test_harness_cli_run.py",
    "src/tests/test_harness_ctx_core.py",
    "src/tests/test_mcp_server.py",
    "src/tests/test_public_api.py",
)


def _matches(path: str, patterns: Iterable[str]) -> bool:
    return any(fnmatch.fnmatch(path, pattern) for pattern in patterns)


def _normalize_path(path: str) -> str:
    return path.strip().lstrip("\ufeff").replace("\\", "/")


def _is_graph_artifact_path(path: str) -> bool:
    if _matches(path, GRAPH_ARTIFACT_PATTERNS):
        return True
    return _matches(path, ("graph/**",)) and path != "graph/README.md"


def classify_paths(paths: Iterable[str]) -> dict[str, bool]:
    files = [normalized for path in paths if (normalized := _normalize_path(path))]
    ci_changed = any(_matches(path, CI_PATTERNS) for path in files)
    docs_changed = any(_matches(path, DOCS_PATTERNS) for path in files)
    graph_artifact_changed = any(_is_graph_artifact_path(path) for path in files)
    graph_only = bool(files) and all(_matches(path, ("graph/**",)) for path in files)
    return {
        "browser_changed": ci_changed or any(_matches(path, BROWSER_PATTERNS) for path in files),
        "ci_changed": ci_changed,
        "docs_changed": docs_changed,
        "docs_only": bool(files) and all(_matches(path, DOCS_PATTERNS) for path in files),
        "graph_artifact_changed": graph_artifact_changed,
        "graph_changed": any(_matches(path, ("graph/**",)) for path in files),
        "graph_only": graph_only,
        "package_changed": ci_changed or any(_matches(path, PACKAGE_PATTERNS) for path in files),
        "similarity_changed": ci_changed
        or any(_matches(path, SIMILARITY_PATTERNS) for path in files),
        "source_changed": ci_changed or any(_matches(path, SOURCE_PATTERNS) for path in files),
        "telemetry_changed": ci_changed
        or any(_matches(path, TELEMETRY_PATTERNS) for path in files),
    }


def write_github_outputs(flags: dict[str, bool], output_path: Path) -> None:
    with output_path.open("a", encoding="utf-8") as output:
        for name in OUTPUT_NAMES:
            output.write(f"{name}={str(flags[name]).lower()}\n")


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("changed_files", type=Path)
    args = parser.parse_args(argv)

    files = args.changed_files.read_text(encoding="utf-8").splitlines()
    flags = classify_paths(files)
    github_output = os.environ.get("GITHUB_OUTPUT")
    if github_output:
        write_github_outputs(flags, Path(github_output))

    print("Changed files:")
    for path in [path for line in files if (path := _normalize_path(line))]:
        print(f"  {path}")
    print("Classification:")
    for name in OUTPUT_NAMES:
        print(f"  {name}={str(flags[name]).lower()}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())