Skip to content

Commit d5a398f

Browse files
author
DavidQ
committed
Enforce utility consolidation rules with CI-safe audit script - PR 11.82
1 parent bb1f6d4 commit d5a398f

5 files changed

Lines changed: 186 additions & 21 deletions

File tree

docs/dev/codex_commands.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
# Codex Commands PR 11.81
1+
# Codex Commands - PR 11.82
22

3-
Model: GPT-5.4
4-
Reasoning: high
3+
## Purpose
4+
Add CI-safe utils rule enforcement after the `src/engine/utils` to `src/shared/utils` consolidation lane.
55

6-
Run Codex with this task:
6+
## Command
7+
Run this from the repository root:
78

8-
```text
9-
You are applying PR 11.81. Expand the utilities audit only. Do not delete, move, or rewrite implementation files.
10-
11-
Inspect src/shared/utils/** and any remaining src/engine/utils/**.
12-
13-
Create these reports:
14-
- docs/dev/reports/utils_dead_usage_audit.md
15-
- docs/dev/reports/utils_duplicate_exports_audit.md
16-
- docs/dev/reports/utils_folder_category_audit.md
17-
- docs/dev/reports/utils_audit_summary.csv
9+
```powershell
10+
powershell -ExecutionPolicy Bypass -File .\scripts\PS\enforce-utils-rules.ps1 -Details
11+
```
1812

19-
Report:
20-
1. unused utility files and unused named exports
21-
2. duplicate or near-duplicate exports across utility files
22-
3. folder-category mismatches, especially generic utilities still under src/engine/utils
23-
4. every remaining literal reference to src/engine/utils/ and /src/engine/utils/
13+
For CI/regression mode:
2414

25-
Do not change implementation code. Do not create wrappers, aliases, or pass-through files. This PR is report-only.
15+
```powershell
16+
powershell -ExecutionPolicy Bypass -File .\scripts\PS\enforce-utils-rules.ps1 -Ci
2617
```
18+
19+
## Acceptance
20+
- Script runs without parser errors.
21+
- CSV report is written to `docs/dev/reports/utils_rules_audit.csv`.
22+
- `-Details` shows findings only when requested.
23+
- `-Ci` exits nonzero when findings exist.
24+
- No files are deleted or moved by this PR.

docs/dev/commit_comment.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Expand utils audit to report dead usage, duplicates, and folder placement mismatches - PR 11.81
1+
Enforce utility consolidation rules with CI-safe audit script - PR 11.82
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"Type","Path","Detail"
2+
"ENGINE_UTIL_REFERENCE","docs/pr/BUILD_PR_11_80_DEAD_UTILS_AUDIT.md","Line 40: remaining reference to src/engine/utils/"
3+
"ENGINE_UTIL_REFERENCE","docs/pr/PLAN_PR_11_80_DEAD_UTILS_AUDIT.md","Line 8: remaining reference to src/engine/utils/"
4+
"ENGINE_UTIL_REFERENCE","docs/pr/PLAN_PR_11_80_DEAD_UTILS_AUDIT.md","Line 10: remaining reference to src/engine/utils/"
5+
"ENGINE_UTIL_REFERENCE","docs/pr/PLAN_PR_11_80_DEAD_UTILS_AUDIT.md","Line 24: remaining reference to src/engine/utils/"
6+
"ENGINE_UTIL_REFERENCE","docs/pr/PR_11_81_UTILS_AUDIT_EXPANSION.md","Line 16: remaining reference to src/engine/utils/"
7+
"ENGINE_UTIL_REFERENCE","docs/pr/PR_11_81_UTILS_AUDIT_EXPANSION.md","Line 68: remaining reference to src/engine/utils/"
8+
"ENGINE_UTIL_REFERENCE","docs/pr/BUILD_PR_11_80_DEAD_UTILS_AUDIT.md","Line 40: remaining reference to /src/engine/utils/"
9+
"ENGINE_UTIL_REFERENCE","docs/pr/PLAN_PR_11_80_DEAD_UTILS_AUDIT.md","Line 10: remaining reference to /src/engine/utils/"

docs/pr/PR_11_82/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# PR 11.82 - Enforce Utils Rules
2+
3+
## Purpose
4+
Add a report-only and CI-capable enforcement script for the utility consolidation lane.
5+
6+
## Scope
7+
- Adds `scripts/PS/enforce-utils-rules.ps1`.
8+
- Reports dead/shared utility candidates.
9+
- Reports duplicate shared utility basenames.
10+
- Reports any remaining `src/engine/utils` files or import/reference paths.
11+
- Writes `docs/dev/reports/utils_rules_audit.csv` when run.
12+
13+
## Out of Scope
14+
- No deletions.
15+
- No import rewrites.
16+
- No file moves.
17+
- No wrappers or alias shims.
18+
19+
## Testing
20+
Run:
21+
22+
```powershell
23+
powershell -ExecutionPolicy Bypass -File .\scripts\PS\enforce-utils-rules.ps1 -Details
24+
```
25+
26+
CI mode:
27+
28+
```powershell
29+
powershell -ExecutionPolicy Bypass -File .\scripts\PS\enforce-utils-rules.ps1 -Ci
30+
```

scripts/PS/enforce-utils-rules.ps1

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<#
2+
.SYNOPSIS
3+
Enforce utility-folder hygiene for shared vs engine utilities.
4+
5+
.DESCRIPTION
6+
CI-safe report script for the utility consolidation lane.
7+
Fails when:
8+
- src/engine/utils contains JavaScript files
9+
- references to src/engine/utils remain
10+
- duplicate utility basenames exist under src/shared/utils
11+
- shared utility files appear unreferenced outside themselves
12+
13+
.PARAMETER Details
14+
Prints detailed findings.
15+
16+
.PARAMETER Ci
17+
Exits 1 when violations are found.
18+
#>
19+
20+
param(
21+
[switch]$Details,
22+
[switch]$Ci
23+
)
24+
25+
$ErrorActionPreference = "Stop"
26+
27+
$RepoRoot = Resolve-Path (Join-Path $PSScriptRoot "..\..")
28+
$SharedUtilsRoot = Join-Path $RepoRoot "src\shared\utils"
29+
$EngineUtilsRoot = Join-Path $RepoRoot "src\engine\utils"
30+
$ReportDir = Join-Path $RepoRoot "docs\dev\reports"
31+
$CsvPath = Join-Path $ReportDir "utils_rules_audit.csv"
32+
33+
New-Item -ItemType Directory -Path $ReportDir -Force | Out-Null
34+
35+
$Findings = New-Object System.Collections.Generic.List[object]
36+
37+
function Add-Finding {
38+
param(
39+
[string]$Type,
40+
[string]$Path,
41+
[string]$Detail
42+
)
43+
44+
$Findings.Add([PSCustomObject]@{
45+
Type = $Type
46+
Path = $Path
47+
Detail = $Detail
48+
}) | Out-Null
49+
}
50+
51+
function Get-RepoRelativePath {
52+
param([string]$Path)
53+
$resolved = Resolve-Path $Path -ErrorAction SilentlyContinue
54+
if ($null -eq $resolved) { return $Path }
55+
return $resolved.Path.Substring($RepoRoot.Path.Length).TrimStart('\','/') -replace '\\','/'
56+
}
57+
58+
$SharedUtils = @()
59+
if (Test-Path $SharedUtilsRoot) {
60+
$SharedUtils = @(Get-ChildItem -Path $SharedUtilsRoot -Recurse -File -Filter *.js)
61+
}
62+
63+
$EngineUtils = @()
64+
if (Test-Path $EngineUtilsRoot) {
65+
$EngineUtils = @(Get-ChildItem -Path $EngineUtilsRoot -Recurse -File -Filter *.js)
66+
}
67+
68+
foreach ($file in $EngineUtils) {
69+
Add-Finding -Type "ENGINE_UTIL_FILE" -Path (Get-RepoRelativePath $file.FullName) -Detail "Utility file remains under src/engine/utils; move to src/shared/utils unless it is proven engine-runtime code."
70+
}
71+
72+
$SourceFiles = @(Get-ChildItem -Path $RepoRoot -Recurse -File -Include *.js,*.mjs,*.html,*.json,*.md -ErrorAction SilentlyContinue | Where-Object {
73+
$_.FullName -notmatch '\\.git\\' -and
74+
$_.FullName -notmatch '\\node_modules\\' -and
75+
$_.FullName -notmatch '\\tmp\\' -and
76+
$_.FullName -notmatch '\\docs\\dev\\reports\\'
77+
})
78+
79+
$EngineUtilReferencePatterns = @(
80+
"src/engine/utils/",
81+
"/src/engine/utils/",
82+
"src\\engine\\utils\\",
83+
"\\src\\engine\\utils\\"
84+
)
85+
86+
foreach ($pattern in $EngineUtilReferencePatterns) {
87+
$matches = @($SourceFiles | Select-String -SimpleMatch -Pattern $pattern -ErrorAction SilentlyContinue)
88+
foreach ($match in $matches) {
89+
Add-Finding -Type "ENGINE_UTIL_REFERENCE" -Path (Get-RepoRelativePath $match.Path) -Detail "Line $($match.LineNumber): remaining reference to $pattern"
90+
}
91+
}
92+
93+
$ByBaseName = $SharedUtils | Group-Object BaseName | Where-Object { $_.Count -gt 1 }
94+
foreach ($group in $ByBaseName) {
95+
$paths = ($group.Group | ForEach-Object { Get-RepoRelativePath $_.FullName }) -join '; '
96+
Add-Finding -Type "DUPLICATE_SHARED_UTIL_BASENAME" -Path $group.Name -Detail $paths
97+
}
98+
99+
foreach ($file in $SharedUtils) {
100+
$relative = Get-RepoRelativePath $file.FullName
101+
$moduleName = $file.BaseName
102+
103+
$refs = @($SourceFiles | Where-Object { $_.FullName -ne $file.FullName } | Select-String -SimpleMatch -Pattern $moduleName -ErrorAction SilentlyContinue)
104+
105+
if ($refs.Count -eq 0) {
106+
Add-Finding -Type "POSSIBLY_DEAD_SHARED_UTIL" -Path $relative -Detail "No references to utility basename found outside the file itself. Review before deleting; dynamic usage may not be detected."
107+
}
108+
}
109+
110+
$Findings | Export-Csv -Path $CsvPath -NoTypeInformation
111+
112+
Write-Output "Utils rules audit complete."
113+
Write-Output "Shared utility files scanned: $($SharedUtils.Count)"
114+
Write-Output "Engine utility files found: $($EngineUtils.Count)"
115+
Write-Output "Findings: $($Findings.Count)"
116+
Write-Output "Report: docs/dev/reports/utils_rules_audit.csv"
117+
118+
if ($Details -and $Findings.Count -gt 0) {
119+
Write-Output ""
120+
Write-Output "Details:"
121+
foreach ($finding in $Findings) {
122+
Write-Output "$($finding.Type) - $($finding.Path) - $($finding.Detail)"
123+
}
124+
}
125+
126+
if ($Ci -and $Findings.Count -gt 0) {
127+
exit 1
128+
}

0 commit comments

Comments
 (0)