Debian Codegraph @

scripts/check-architecture-imports.sh

raw
1 #!/usr/bin/env bash
2
3 set -euo pipefail
4
5 [[ $# -eq 0 ]] || { echo "usage: $0" >&2; exit 2; }
6
7 module="$(go list -m)"
8 provider_actwire="$module/internal/providers/actwire"
9 provider_openaiwire="$module/internal/providers/openaiwire"
10 provider_factory="$module/internal/providerfactory"
11 internal_session="$module/internal/session"
12
13 ACTIVE_RULES=(
14 "ARCH010 frontend-provider-construction"
15 "ARCH011 frontend-session-boundary"
16 "ARCH012 cli-parser-boundary"
17 "ARCH013 frontend-providerconfig-boundary"
18 )
19
20 readonly RULE_FRONTEND_PROVIDER="ARCH010 frontend-provider-construction"
21 readonly RULE_FRONTEND_PROVIDER_TEXT="frontend coordinator must use internal/providerfactory instead of concrete provider adapters."
22 readonly RULE_FRONTEND_PROVIDER_GUIDANCE="move provider construction behind internal/providerfactory; do not import concrete provider adapters from frontend packages."
23 readonly RULE_FRONTEND_SESSION="ARCH011 frontend-session-boundary"
24 readonly RULE_FRONTEND_SESSION_TEXT="frontend adapters must use cmd/internal/clnkrapp instead of importing internal/session directly."
25 readonly RULE_FRONTEND_SESSION_GUIDANCE="move session persistence calls behind cmd/internal/clnkrapp; do not import internal/session from cmd/... outside cmd/internal/clnkrapp."
26 readonly RULE_CLI_PARSER="ARCH012 cli-parser-boundary"
27 readonly RULE_CLI_PARSER_TEXT="CLI option parsing must stay local and stdlib-only."
28 readonly RULE_CLI_PARSER_GUIDANCE="keep cmd/clnkr/cli_*.go parser-only; move app-service and config-resolution calls out of the parser."
29 readonly RULE_FRONTEND_PROVIDERCONFIG="ARCH013 frontend-providerconfig-boundary"
30 readonly RULE_FRONTEND_PROVIDERCONFIG_TEXT="frontend adapters must use cmd/internal/clnkrapp instead of importing cmd/internal/providerconfig directly."
31 readonly RULE_FRONTEND_PROVIDERCONFIG_GUIDANCE="move provider config and startup construction behind cmd/internal/clnkrapp; do not import cmd/internal/providerconfig from cmd/... outside cmd/internal/clnkrapp."
32
33 emit_violation() {
34 local rule="$1" importer="$2" target="$3" import_source="$4" trusted_rule="$5" guidance="$6"
35 {
36 echo "error: architecture import boundary violation"
37 echo "rule: $rule"
38 echo "importer: $importer"
39 echo "target: $target"
40 echo "import_source: $import_source"
41 echo "trusted_rule: $trusted_rule"
42 echo "source_fact: go list reported importer imports target."
43 echo "guidance: $guidance"
44 echo
45 } >&2
46 }
47
48 emit_file_import_violation() {
49 local rule="$1" importer="$2" target="$3" trusted_rule="$4" guidance="$5"
50 {
51 echo "error: architecture import boundary violation"
52 echo "rule: $rule"
53 echo "importer: $importer"
54 echo "target: $target"
55 echo "import_source: file_imports"
56 echo "trusted_rule: $trusted_rule"
57 echo "source_fact: file import scan reported importer imports target."
58 echo "guidance: $guidance"
59 echo
60 } >&2
61 }
62
63 kind_of() {
64 case "$1" in
65 "$module") echo root ;;
66 "$module"/internal/core/*) echo core ;;
67 "$internal_session") echo frontend_runtime ;;
68 "$provider_factory") echo provider_factory ;;
69 "$module"/internal/providers/*) echo provider ;;
70 "$module"/cmd/internal/compaction) echo compaction ;;
71 "$module"/cmd/*) echo cmd ;;
72 *) echo other ;;
73 esac
74 }
75
76 check_edge() {
77 local importer="$1" target="$2" kind
78 kind="$(kind_of "$importer")"
79 case "$target" in "$module"/cmd/internal/*) case "$importer" in "$module"/cmd/*) ;; *) echo "error: $importer -> $target: only cmd/... may import cmd/internal/..." >&2; return 1 ;; esac ;; esac
80 case "$target" in "$provider_openaiwire") case "$importer" in "$module"/internal/providers/openai|"$module"/internal/providers/openairesponses) ;; *) echo "error: $importer -> $target: only OpenAI provider packages may import internal/providers/openaiwire" >&2; return 1 ;; esac ;; esac
81 case "$kind" in
82 root) case "$target" in "$module"/internal/core/*) ;; *) echo "error: $importer -> $target: root may import only internal/core/..." >&2; return 1 ;; esac ;;
83 core) case "$target" in "$module"/internal/core/*) ;; *) echo "error: $importer -> $target: internal/core/... may import only internal/core/..." >&2; return 1 ;; esac ;;
84 provider)
85 case "$target" in
86 "$module"|"$module"/internal/core/*|"$provider_actwire") ;;
87 "$provider_openaiwire") ;;
88 *) echo "error: $importer -> $target: internal/providers/... may import only root clnkr, internal/core/..., actwire, or allowed OpenAI wire helpers..." >&2; return 1 ;;
89 esac
90 ;;
91 provider_factory)
92 case "$target" in
93 "$module"|"$module"/internal/providers/anthropic|"$module"/internal/providers/openai|"$module"/internal/providers/openairesponses|"$module"/internal/providers/providerconfig) ;;
94 *) echo "error: $importer -> $target: internal/providerfactory may import only root clnkr, concrete provider adapters, and provider request semantics" >&2; return 1 ;;
95 esac
96 ;;
97 compaction) [[ "$target" == "$module" ]] || { echo "error: $importer -> $target: cmd/internal/compaction should keep repo-local imports to root clnkr only" >&2; return 1; } ;;
98 cmd)
99 case "$target" in
100 "$module"|"$module"/cmd/internal/*|"$module"/internal/providers/providerconfig) ;;
101 "$internal_session") [[ "$importer" == "$module/cmd/internal/clnkrapp" ]] || { echo "error: $importer -> $target: only cmd/internal/clnkrapp may import internal/session" >&2; return 1; } ;;
102 "$provider_factory") [[ "$importer" == "$module/cmd/internal/clnkrapp" ]] || { echo "error: $importer -> $target: only cmd/internal/clnkrapp may import internal/providerfactory" >&2; return 1; } ;;
103 *) echo "error: $importer -> $target: cmd/... may import only root clnkr, cmd/internal/..., internal/providers/providerconfig, internal/session from cmd/internal/clnkrapp, or internal/providerfactory from clnkrapp" >&2; return 1 ;;
104 esac
105 ;;
106 other) echo "error: $importer -> $target: unclassified repo-local importer" >&2; return 1 ;;
107 esac
108 }
109
110 is_frontend_coordinator() {
111 [[ "$1" == "$module/cmd/internal/clnkrapp" ]]
112 }
113
114 is_concrete_provider_adapter() {
115 case "$1" in
116 "$module/internal/providers/anthropic"|"$module/internal/providers/openai"|"$module/internal/providers/openairesponses") return 0 ;;
117 *) return 1 ;;
118 esac
119 }
120
121 is_session_boundary_target() {
122 [[ "$1" == "$internal_session" ]]
123 }
124
125 is_providerconfig_boundary_target() {
126 [[ "$1" == "$module/cmd/internal/providerconfig" ]]
127 }
128
129 check_frontend_provider_construction() {
130 local bad=0 edge importer target import_source
131 for edge in "${import_edges[@]}"; do
132 IFS=$'\t' read -r importer target import_source <<<"$edge"
133 if is_frontend_coordinator "$importer" && is_concrete_provider_adapter "$target"; then
134 emit_violation "$RULE_FRONTEND_PROVIDER" "$importer" "$target" "$import_source" "$RULE_FRONTEND_PROVIDER_TEXT" "$RULE_FRONTEND_PROVIDER_GUIDANCE"
135 bad=1
136 fi
137 done
138 return "$bad"
139 }
140
141 check_frontend_session_boundary() {
142 local bad=0 edge importer target import_source
143 for edge in "${import_edges[@]}"; do
144 IFS=$'\t' read -r importer target import_source <<<"$edge"
145 if [[ "$importer" == "$module"/cmd/* && "$importer" != "$module/cmd/internal/clnkrapp" ]] && is_session_boundary_target "$target"; then
146 emit_violation "$RULE_FRONTEND_SESSION" "$importer" "$target" "$import_source" "$RULE_FRONTEND_SESSION_TEXT" "$RULE_FRONTEND_SESSION_GUIDANCE"
147 bad=1
148 fi
149 done
150 return "$bad"
151 }
152
153 cli_parser_imports() {
154 local scanner_dir scanner status
155 scanner_dir="$(mktemp -d "${TMPDIR:-/tmp}/cli-parser-imports.XXXXXX")"
156 scanner="$scanner_dir/scanner.go"
157 cat > "$scanner" <<'GO'
158 package main
159
160 import (
161 "fmt"
162 "go/parser"
163 "go/token"
164 "os"
165 "strconv"
166 )
167
168 func main() {
169 path := os.Getenv("CLI_PARSER_SCAN_FILE")
170 if path == "" {
171 fmt.Fprintln(os.Stderr, "CLI_PARSER_SCAN_FILE is required")
172 os.Exit(2)
173 }
174 file, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ImportsOnly)
175 if err != nil {
176 fmt.Fprintln(os.Stderr, err)
177 os.Exit(2)
178 }
179 for _, spec := range file.Imports {
180 path, err := strconv.Unquote(spec.Path.Value)
181 if err != nil {
182 fmt.Fprintln(os.Stderr, err)
183 os.Exit(2)
184 }
185 fmt.Println(path)
186 }
187 }
188 GO
189 CLI_PARSER_SCAN_FILE="$1" go run "$scanner"
190 status=$?
191 rm -rf "$scanner_dir"
192 return "$status"
193 }
194
195 is_standard_import() {
196 local target="$1" standard
197 standard="$(go list -f '{{.Standard}}' "$target" 2>/dev/null)" || return 1
198 [[ "$standard" == "true" ]]
199 }
200
201 check_frontend_providerconfig_boundary() {
202 local bad=0 edge importer target import_source
203 for edge in "${import_edges[@]}"; do
204 IFS=$' ' read -r importer target import_source <<<"$edge"
205 if [[ "$importer" == "$module"/cmd/* && "$importer" != "$module/cmd/internal/clnkrapp" ]] && is_providerconfig_boundary_target "$target"; then
206 emit_violation "$RULE_FRONTEND_PROVIDERCONFIG" "$importer" "$target" "$import_source" "$RULE_FRONTEND_PROVIDERCONFIG_TEXT" "$RULE_FRONTEND_PROVIDERCONFIG_GUIDANCE"
207 bad=1
208 fi
209 done
210 return "$bad"
211 }
212
213 check_cli_parser_boundary() {
214 local bad=0 file target imports_output
215 for file in cmd/clnkr/cli_*.go; do
216 [[ -f "$file" ]] || continue
217 if ! imports_output="$(cli_parser_imports "$file")"; then
218 echo "error: cannot scan imports for $file" >&2
219 return 1
220 fi
221 while IFS= read -r target; do
222 [[ -n "$target" ]] || continue
223 if ! is_standard_import "$target"; then
224 emit_file_import_violation "$RULE_CLI_PARSER" "$file" "$target" "$RULE_CLI_PARSER_TEXT" "$RULE_CLI_PARSER_GUIDANCE"
225 bad=1
226 fi
227 done <<<"$imports_output"
228 done
229 return "$bad"
230 }
231
232 run_active_rule() {
233 case "$1" in
234 "$RULE_FRONTEND_PROVIDER") check_frontend_provider_construction ;;
235 "$RULE_FRONTEND_SESSION") check_frontend_session_boundary ;;
236 "$RULE_CLI_PARSER") check_cli_parser_boundary ;;
237 "$RULE_FRONTEND_PROVIDERCONFIG") check_frontend_providerconfig_boundary ;;
238 *) echo "error: unknown architecture rule: $1" >&2; exit 2 ;;
239 esac
240 }
241
242 import_edges=()
243 while IFS=$'\t' read -r importer target import_source; do
244 [[ -n "$importer" && "$target" == "$module"* && "$target" != "$importer" ]] || continue
245 import_edges+=("$importer"$'\t'"$target"$'\t'"$import_source")
246 done < <(go list -json ./... | jq -r '. as $pkg | ($pkg.ImportPath // empty) as $importer |
247 [($pkg.Imports // [] | .[]? | [$importer, ., "imports"]),
248 ($pkg.TestImports // [] | .[]? | [$importer, ., "test_imports"]),
249 ($pkg.XTestImports // [] | .[]? | [$importer, ., "external_test_imports"])] |
250 .[] | @tsv')
251
252 bad=0
253 edge_count=0
254 for edge in "${import_edges[@]}"; do
255 IFS=$'\t' read -r importer target import_source <<<"$edge"
256 edge_count=$((edge_count + 1))
257 check_edge "$importer" "$target" || bad=1
258 done
259
260 for rule in "${ACTIVE_RULES[@]}"; do
261 run_active_rule "$rule" || bad=1
262 done
263
264 (( bad == 0 )) || exit 1
265 package_count="$(go list ./... | sort -u | wc -l | tr -d '[:space:]')"
266 echo "target architecture import checks passed (${package_count} packages, ${edge_count} repo-local edges)"