nightytidy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +314 -0
  3. package/bin/nightytidy.js +3 -0
  4. package/package.json +55 -0
  5. package/src/checks.js +367 -0
  6. package/src/claude.js +655 -0
  7. package/src/cli.js +1012 -0
  8. package/src/consolidation.js +81 -0
  9. package/src/dashboard-html.js +496 -0
  10. package/src/dashboard-standalone.js +167 -0
  11. package/src/dashboard-tui.js +208 -0
  12. package/src/dashboard.js +427 -0
  13. package/src/env.js +100 -0
  14. package/src/executor.js +550 -0
  15. package/src/git.js +348 -0
  16. package/src/lock.js +186 -0
  17. package/src/logger.js +111 -0
  18. package/src/notifications.js +33 -0
  19. package/src/orchestrator.js +919 -0
  20. package/src/prompts/loader.js +55 -0
  21. package/src/prompts/manifest.json +138 -0
  22. package/src/prompts/specials/changelog.md +28 -0
  23. package/src/prompts/specials/consolidation.md +61 -0
  24. package/src/prompts/specials/doc-update.md +1 -0
  25. package/src/prompts/specials/report.md +95 -0
  26. package/src/prompts/steps/01-documentation.md +173 -0
  27. package/src/prompts/steps/02-test-coverage.md +181 -0
  28. package/src/prompts/steps/03-test-hardening.md +181 -0
  29. package/src/prompts/steps/04-test-architecture.md +130 -0
  30. package/src/prompts/steps/05-test-consolidation.md +165 -0
  31. package/src/prompts/steps/06-test-quality.md +211 -0
  32. package/src/prompts/steps/07-api-design.md +165 -0
  33. package/src/prompts/steps/08-security-sweep.md +207 -0
  34. package/src/prompts/steps/09-dependency-health.md +217 -0
  35. package/src/prompts/steps/10-codebase-cleanup.md +189 -0
  36. package/src/prompts/steps/11-crosscutting-concerns.md +196 -0
  37. package/src/prompts/steps/12-file-decomposition.md +263 -0
  38. package/src/prompts/steps/13-code-elegance.md +329 -0
  39. package/src/prompts/steps/14-architectural-complexity.md +297 -0
  40. package/src/prompts/steps/15-type-safety.md +192 -0
  41. package/src/prompts/steps/16-logging-error-message.md +173 -0
  42. package/src/prompts/steps/17-data-integrity.md +139 -0
  43. package/src/prompts/steps/18-performance.md +183 -0
  44. package/src/prompts/steps/19-cost-resource-optimization.md +136 -0
  45. package/src/prompts/steps/20-error-recovery.md +145 -0
  46. package/src/prompts/steps/21-race-condition-audit.md +178 -0
  47. package/src/prompts/steps/22-bug-hunt.md +229 -0
  48. package/src/prompts/steps/23-frontend-quality.md +210 -0
  49. package/src/prompts/steps/24-uiux-audit.md +284 -0
  50. package/src/prompts/steps/25-state-management.md +170 -0
  51. package/src/prompts/steps/26-perceived-performance.md +190 -0
  52. package/src/prompts/steps/27-devops.md +165 -0
  53. package/src/prompts/steps/28-scheduled-job-chron-jobs.md +141 -0
  54. package/src/prompts/steps/29-observability.md +152 -0
  55. package/src/prompts/steps/30-backup-check.md +155 -0
  56. package/src/prompts/steps/31-product-polish-ux-friction.md +122 -0
  57. package/src/prompts/steps/32-feature-discovery-opportunity.md +128 -0
  58. package/src/prompts/steps/33-strategic-opportunities.md +217 -0
  59. package/src/report.js +540 -0
  60. package/src/setup.js +133 -0
  61. package/src/sync.js +536 -0
@@ -0,0 +1,217 @@
1
+ # Dependency Health & Upgrade Pass
2
+
3
+ ## Prompt
4
+
5
+ ```
6
+ You are running an overnight dependency health audit and upgrade pass. You have several hours. Your job is to assess the health, risk, and maintainability of every external dependency in the project — then upgrade what's safe and document the rest.
7
+
8
+ Work on a branch called `dependency-health-[date]`.
9
+
10
+ ## Your Mission
11
+
12
+ ### Phase 1: Dependency Inventory
13
+
14
+ **Step 1: Catalog every dependency**
15
+ Read all dependency manifests (package.json, requirements.txt, Cargo.toml, go.mod, Gemfile, pom.xml, etc.) and create a complete inventory:
16
+
17
+ For each dependency:
18
+ - Name and current version
19
+ - Latest available version
20
+ - How far behind the project is (patch / minor / major versions behind)
21
+ - What it's used for in this project (read the code, don't guess)
22
+ - How widely it's imported (used in 1 file or 50?)
23
+ - Whether it's a direct dependency or transitive
24
+ - Whether it's a runtime dependency or dev-only
25
+
26
+ **Step 2: Catalog lock file status**
27
+ - Is there a lock file (package-lock.json, yarn.lock, poetry.lock, Cargo.lock, etc.)?
28
+ - Is it committed to the repo?
29
+ - Is it consistent with the manifest? (Run install and check for drift)
30
+ - Are there duplicate packages at different versions in the dependency tree?
31
+
32
+ ### Phase 2: Health Assessment
33
+
34
+ **Step 1: Identify abandoned or risky dependencies**
35
+ For each dependency, assess its health:
36
+
37
+ - **Last published**: When was the last release? Dependencies with no release in 2+ years are a risk.
38
+ - **Maintenance signals**: Open issue count, unmerged PRs, maintainer activity (use web search to check npm/PyPI/crates.io pages and GitHub repos)
39
+ - **Known vulnerabilities**: Run `npm audit` / `pip audit` / `cargo audit` / equivalent. For each CVE:
40
+ - Severity (critical/high/medium/low)
41
+ - Is the vulnerable code path actually used in this project?
42
+ - Is there a patched version available?
43
+ - Is the fix a simple version bump or a breaking change?
44
+ - **Bus factor**: Is this maintained by one person? Is it a critical dependency maintained by an unfunded individual? (This is a real supply chain risk)
45
+
46
+ **Step 2: License compliance scan**
47
+ For every dependency (including transitive dependencies):
48
+ - What license does it use?
49
+ - Flag any that are:
50
+ - **GPL/AGPL** in a proprietary or non-GPL project (potential copyleft risk)
51
+ - **SSPL** or **BSL** (may restrict commercial use)
52
+ - **No license specified** (legally risky — no license means no permission to use)
53
+ - **Custom or unusual licenses** that need legal review
54
+ - Generate a complete license inventory table
55
+ - If the project has a declared license, flag any dependency license that's incompatible with it
56
+
57
+ **Step 3: Dependency weight analysis**
58
+ Identify dependencies that are disproportionately heavy:
59
+ - Packages that pull in massive transitive dependency trees for minimal functionality
60
+ - Packages where only a small fraction of the library is actually used (e.g., importing all of lodash for `_.get`)
61
+ - Multiple packages that do similar things (two date libraries, two HTTP clients, two validation libraries)
62
+ - Packages that could be replaced with native language features (e.g., `is-odd`, `left-pad` style micro-packages, or libraries superseded by modern language features)
63
+
64
+ For each heavy/redundant dependency:
65
+ - What is it and what's it used for?
66
+ - How much of it is actually used?
67
+ - What's the lighter alternative? (native feature, smaller package, or inline implementation)
68
+ - Estimated effort to replace it
69
+
70
+ ### Phase 3: Safe Upgrades
71
+
72
+ **Step 1: Upgrade patch versions**
73
+ - Bump all dependencies to their latest patch version (X.Y.Z → X.Y.latest)
74
+ - Run the full test suite after each batch of upgrades
75
+ - These should be safe — patch versions are supposed to be backward compatible
76
+ - If any tests fail, revert that specific upgrade and document the failure
77
+ - Commit: `chore: bump patch versions for [scope]`
78
+
79
+ **Step 2: Upgrade minor versions**
80
+ - Bump dependencies to their latest minor version one at a time (X.Y → X.latest.latest)
81
+ - Run tests after each upgrade
82
+ - Minor versions may introduce new features but should be backward compatible
83
+ - If tests fail, revert and document
84
+ - Commit: `chore: bump [package] to [version]`
85
+
86
+ **Step 3: Document major version upgrades**
87
+ Major version upgrades are too risky for an overnight pass. For each dependency that's one or more major versions behind:
88
+ - What breaking changes were introduced? (Read the changelog/migration guide)
89
+ - What code in this project would need to change?
90
+ - Estimated effort: trivial / moderate / significant
91
+ - Priority: how important is this upgrade? (Security fix? Performance improvement? Just new features?)
92
+ - Dependencies on other upgrades (does upgrading X require also upgrading Y?)
93
+
94
+ **Step 4: Attempt low-risk major upgrades**
95
+ If any major upgrades look trivial (changelog says "renamed one function" or "dropped Node 12 support"):
96
+ - Attempt the upgrade
97
+ - Run tests
98
+ - If they pass, commit: `chore: upgrade [package] from [old] to [new]`
99
+ - If they fail, revert and add to the documentation with notes on what broke
100
+
101
+ ### Phase 4: Dependency Reduction Opportunities
102
+
103
+ **Step 1: Find removable dependencies**
104
+ - Scan for dependencies that are imported in the manifest but never actually used in the source code
105
+ - Scan for dependencies that are only used in commented-out or dead code
106
+ - Check for dependencies that duplicate built-in functionality (e.g., a polyfill for something the minimum supported runtime already supports)
107
+
108
+ **Step 2: Find replaceable dependencies**
109
+ - Identify packages that can be replaced with a few lines of utility code (especially micro-packages)
110
+ - Identify packages where only one function/feature is used and that function could be inlined
111
+ - Identify packages with lighter, actively maintained alternatives
112
+
113
+ **Step 3: Implement safe removals**
114
+ - Remove clearly unused dependencies
115
+ - Run tests
116
+ - Commit: `chore: remove unused dependency [package]`
117
+
118
+ DO NOT replace or inline dependencies in this pass unless it's trivially simple. Document replacement opportunities for the team.
119
+
120
+ ## Output Requirements
121
+
122
+ Create the `audit-reports/` directory in the project root if it doesn't already exist. Save the report as `audit-reports/09_DEPENDENCY_HEALTH_REPORT_[run-number]_[date]_[time in user's local time].md` (e.g., `09_DEPENDENCY_HEALTH_REPORT_01_2026-02-16_2129.md`). Increment the run number based on any existing reports with the same name prefix in that folder.
123
+
124
+ ### Report Structure
125
+
126
+ 1. **Executive Summary**
127
+ - Total dependencies: X (Y direct, Z transitive)
128
+ - Dependencies with known vulnerabilities: X
129
+ - Dependencies 1+ major versions behind: X
130
+ - Potentially abandoned dependencies: X
131
+ - License risks found: X
132
+ - Upgrades applied: X
133
+ - Dependencies removed: X
134
+
135
+ 2. **Vulnerability Report**
136
+ - Table: | Package | CVE | Severity | Used in Project? | Fix Available? | Fix Applied? |
137
+ - Vulnerabilities that couldn't be fixed and why
138
+
139
+ 3. **License Compliance**
140
+ - Complete license inventory: table with | Package | License | Risk Level | Notes |
141
+ - Flagged licenses that need legal review
142
+ - Recommendation for ongoing license monitoring
143
+
144
+ 4. **Staleness Report**
145
+ - Table: | Package | Current | Latest | Versions Behind | Last Published | Health |
146
+ - Sorted by risk (most behind + least maintained first)
147
+
148
+ 5. **Upgrades Applied**
149
+ - Table: | Package | From | To | Tests Pass? |
150
+ - Any issues encountered during upgrades
151
+
152
+ 6. **Major Upgrades Needed (Not Applied)**
153
+ - Table: | Package | Current | Target | Breaking Changes | Effort | Priority |
154
+ - Suggested upgrade order (accounting for dependencies between upgrades)
155
+
156
+ 7. **Dependency Weight & Reduction**
157
+ - Heavy dependencies: table with | Package | Size/Impact | Usage | Alternative | Effort |
158
+ - Unused dependencies removed
159
+ - Replacement opportunities for team review
160
+
161
+ 8. **Abandoned/At-Risk Dependencies**
162
+ - Table: | Package | Last Release | Maintainer Activity | Risk | Recommendation |
163
+
164
+ 9. **Recommendations**
165
+ - Priority-ordered action items
166
+ - Suggested tooling for ongoing dependency health (Dependabot, Renovate, Snyk, etc.)
167
+ - Suggested policy for dependency additions (criteria for adopting new dependencies)
168
+
169
+ ## Rules
170
+ - Branch: `dependency-health-[date]`
171
+ - Run full test suite after every upgrade attempt
172
+ - If tests fail after an upgrade, revert IMMEDIATELY — don't debug the upgrade, just document it
173
+ - DO NOT attempt major version upgrades unless the changelog clearly indicates the change is trivial for this project
174
+ - DO NOT replace or rewrite dependencies overnight — only remove unused ones
175
+ - For license assessment: flag risks, don't make legal determinations. The team needs to decide acceptable license policy.
176
+ - Use web search to check dependency health (npm page, GitHub repo, last release date, open issues)
177
+ - Be conservative. A working codebase with old dependencies is better than a broken codebase with new ones.
178
+ - You have all night. Be thorough. Check every dependency.
179
+ ```
180
+
181
+ ## Chat Output Requirement
182
+
183
+ In addition to writing the full report file, you MUST print a summary directly in the conversation when you finish. Do not make the user open the report to get the highlights. The chat summary should include:
184
+
185
+ ### 1. Status Line
186
+ One sentence: what you did, how long it took, and whether all tests still pass.
187
+
188
+ ### 2. Key Findings
189
+ The most important things discovered — bugs, risks, wins, or surprises. Each bullet should be specific and actionable, not vague. Lead with severity or impact.
190
+
191
+ **Good:** "CRITICAL: No backup configuration found for the primary Postgres database — total data loss risk."
192
+ **Bad:** "Found some issues with backups."
193
+
194
+ ### 3. Changes Made (if applicable)
195
+ Bullet list of what was actually modified, added, or removed. Skip this section for read-only analysis runs.
196
+
197
+ ### 4. Recommendations
198
+
199
+ If there are legitimately beneficial recommendations worth pursuing right now, present them in a table. Do **not** force recommendations — if the audit surfaced no actionable improvements, simply state that no recommendations are warranted at this time and move on.
200
+
201
+ When recommendations exist, use this table format:
202
+
203
+ | # | Recommendation | Impact | Risk if Ignored | Worth Doing? | Details |
204
+ |---|---|---|---|---|---|
205
+ | *Sequential number* | *Short description (≤10 words)* | *What improves if addressed* | *Low / Medium / High / Critical* | *Yes / Probably / Only if time allows* | *1–3 sentences explaining the reasoning, context, or implementation guidance* |
206
+
207
+ Order rows by risk descending (Critical → High → Medium → Low). Be honest in the "Worth Doing?" column — not everything flagged is worth the engineering time. If a recommendation is marginal, say so.
208
+
209
+ ### 5. Report Location
210
+ State the full path to the detailed report file for deeper review.
211
+
212
+ ---
213
+
214
+ **Formatting rules for chat output:**
215
+ - Use markdown headers, bold for severity labels, and bullet points for scannability.
216
+ - Do not duplicate the full report contents — just the highlights and recommendations.
217
+ - If you made zero findings in a phase, say so in one line rather than omitting it silently.
@@ -0,0 +1,189 @@
1
+ # The Codebase Cleanup (Updated)
2
+
3
+ ## Prompt
4
+
5
+ ```
6
+ You are running an overnight codebase cleanup. You have several hours — be thorough and methodical. Unlike a security audit, you will actually be making changes to the code. Every change must keep tests passing.
7
+
8
+ ## Your Mission
9
+
10
+ Conduct a comprehensive codebase cleanup covering five areas. Work on a branch called `codebase-cleanup-[date]`. After EVERY meaningful change, run the test suite. If tests break, revert and document the issue instead of shipping the broken change.
11
+
12
+ ### Phase 1: Dead Code Elimination
13
+ Systematically identify and remove:
14
+ - **Unused exports**: Functions, classes, constants, and types that are exported but never imported anywhere
15
+ - **Unused imports**: Imports that exist but aren't referenced in the file
16
+ - **Unreachable code**: Code after return/throw statements, permanently false conditionals, disabled feature flags that will never be re-enabled
17
+ - **Orphaned files**: Files that are never imported or referenced by any other file (be careful — check for dynamic imports, config references, and script entries)
18
+ - **Unused dependencies**: Packages in package.json (or equivalent) that are never imported in the source code
19
+ - **Commented-out code blocks**: Large blocks of commented-out code (not explanatory comments — actual dead code that's been commented out). If it's in version control, it doesn't need to be preserved as comments.
20
+
21
+ **Process for each removal:**
22
+ 1. Identify the dead code
23
+ 2. Verify it's truly unused (search for dynamic references, string-based imports, reflection, etc.)
24
+ 3. Remove it
25
+ 4. Run tests
26
+ 5. If tests pass, commit with a clear message: `chore: remove unused [description]`
27
+ 6. If tests fail, revert and note it in the report as "appears unused but tests depend on it — investigate"
28
+
29
+ ### Phase 2: Code Duplication Reduction
30
+ - Scan for duplicated or near-duplicated logic (functions that do roughly the same thing with minor variations)
31
+ - Focus on:
32
+ - Copy-pasted utility functions that exist in multiple files
33
+ - Similar data transformation logic repeated across modules
34
+ - Repeated validation patterns that could be a shared validator
35
+ - Similar API call patterns that could be a shared client method
36
+ - For each instance of significant duplication:
37
+ - If the fix is low-risk (extracting a shared utility, creating a helper function): implement it, run tests, commit
38
+ - If the fix is higher-risk (refactoring core patterns): document it in the report with a proposed approach, but don't implement
39
+
40
+ ### Phase 3: Consistency Enforcement
41
+ Scan for and fix inconsistencies in:
42
+ - **Naming conventions**: Mixed camelCase/snake_case in the same language, inconsistent file naming patterns, inconsistent component naming
43
+ - **Import ordering**: Standardize to a consistent pattern (external deps → internal modules → relative imports → types)
44
+ - **Error handling patterns**: Some functions throw, some return null, some return Result types — document the dominant pattern and flag deviations
45
+ - **Async patterns**: Mixed callbacks/promises/async-await for the same kind of operation — modernize to the dominant (usually best) pattern
46
+ - **String quotes**: Mixed single/double quotes (respect existing linter config if present)
47
+
48
+ **Important**: Only fix inconsistencies where there's a clear "right way" already dominant in the codebase. Don't impose a new convention — reinforce the existing one. If it's genuinely 50/50, document it in the report and let the team decide.
49
+
50
+ ### Phase 4: Configuration & Feature Flag Hygiene
51
+
52
+ This phase combines stale feature flag cleanup, TODO inventory, and comprehensive configuration hygiene.
53
+
54
+ **Step 1: Feature flag inventory and cleanup**
55
+ - Find every feature flag in the codebase (environment variables, config files, LaunchDarkly/Flagsmith/etc. references, hardcoded boolean switches)
56
+ - For each flag, document:
57
+ - Name and location
58
+ - Current value (always true, always false, dynamic, or unknown)
59
+ - **Owner**: Who likely owns this flag? (Infer from git blame, module ownership, or comments)
60
+ - **Age**: When was it introduced? (Check git history)
61
+ - **Type**: Is this a temporary rollout flag, a permanent operational toggle, or a kill switch?
62
+ - For always-true flags: remove the flag and keep the code
63
+ - For always-false flags: remove the flag AND the dead code it guards
64
+ - For rollout flags older than 6 months that are always-on: these are almost certainly safe to remove. Remove the flag, keep the code.
65
+ - Run tests after each removal. Commit: `chore: remove stale feature flag [name]`
66
+
67
+ **Step 2: Flag coupling analysis**
68
+ - Identify flags that depend on other flags (nested conditionals, compound flag checks)
69
+ - Document the combinatorial complexity: how many distinct code paths do the current flags create?
70
+ - Flag any combinations that are likely untested (e.g., if Flag A and Flag B are both "sometimes on," is the (A=true, B=false) path ever tested?)
71
+ - Document these in the report — don't try to fix flag coupling overnight
72
+
73
+ **Step 3: Configuration sprawl audit**
74
+ - Find every configuration value in the codebase (constants, config files, environment variable reads, settings objects)
75
+ - Identify configuration that is:
76
+ - **Set but never varied**: Config values that have only ever been set to one value across all environments. These might be candidates for becoming constants.
77
+ - **Undocumented**: Config values that have no comment, no README entry, and no `.env.example` entry explaining what they do or what valid values are
78
+ - **Duplicated**: The same conceptual setting defined in multiple places (a timeout defined in both a config file and a hardcoded fallback, with different values)
79
+ - **Unused**: Config values defined but never read by application code
80
+ - For clearly unused config: remove it. Run tests. Commit: `chore: remove unused config [name]`
81
+ - For undocumented config: add clear comments explaining what it controls, valid values, and default behavior
82
+ - For duplicated config: consolidate to a single source of truth where safe
83
+
84
+ **Step 4: Default value audit**
85
+ - For every configuration value with a default, evaluate whether the default is appropriate:
86
+ - Are there defaults appropriate for development that would be dangerous in production? (Debug mode on by default, permissive CORS by default, short token expiry in dev but what about prod?)
87
+ - Are there defaults that silently degrade behavior? (A cache TTL defaulting to 0, effectively disabling caching)
88
+ - Are there missing defaults that cause crashes if the config isn't explicitly set?
89
+ - Document concerns in the report. Fix defaults that are clearly wrong and safe to change.
90
+
91
+ **Step 5: TODO/FIXME/HACK inventory** (unchanged from original)
92
+ - Find every TODO, FIXME, HACK, XXX, and TEMP comment in the codebase. For each one:
93
+ - Categorize: bug, tech debt, feature request, optimization, or obsolete
94
+ - If it's clearly obsolete (references old code that no longer exists, mentions completed work): remove it
95
+ - For the rest: include in the report with file, line, category, and your assessment of priority
96
+
97
+ ### Phase 5: Quick Wins
98
+ As you work through the phases above, you'll notice small improvements that don't fit neatly into a category. Fix them as you go:
99
+ - Simplifying overly complex conditionals
100
+ - Replacing deprecated API usage with modern equivalents
101
+ - Removing unnecessary type assertions (TypeScript)
102
+ - Converting var to const/let where applicable
103
+ - Removing empty files, empty constructors, or no-op overrides
104
+ - Fixing obvious typos in variable names or comments
105
+
106
+ Commit these as `chore: misc cleanup in [module/file]`
107
+
108
+ ## Output Requirements
109
+
110
+ Create the `audit-reports/` directory in the project root if it doesn't already exist. Save the report as `audit-reports/10_CODEBASE_CLEANUP_REPORT_[run-number]_[date]_[time in user's local time].md` (e.g., `10_CODEBASE_CLEANUP_REPORT_01_2026-02-16_2129.md`). Increment the run number based on any existing reports with the same name prefix in that folder.
111
+
112
+ ### Report Structure
113
+
114
+ 1. **Summary**
115
+ - Total files modified
116
+ - Lines of code removed (net)
117
+ - Unused dependencies removed
118
+ - Number of commits made
119
+ - Any tests that were affected
120
+
121
+ 2. **Dead Code Removed** — list everything you removed and why you're confident it was dead
122
+
123
+ 3. **Duplication Reduced** — what you consolidated, plus higher-risk duplications you documented but didn't touch
124
+
125
+ 4. **Consistency Changes** — what patterns you enforced and where
126
+
127
+ 5. **Configuration & Feature Flags** (expanded from original)
128
+ - Flags removed: table with | Flag | Type | Age | Value | Action Taken |
129
+ - Flag coupling map (which flags interact and the combinatorial paths created)
130
+ - Configuration sprawl findings: table with | Config | Location | Issue | Action |
131
+ - Default value concerns: table with | Config | Default | Concern | Recommendation |
132
+ - Full TODO/FIXME inventory table: | File | Line | Comment | Category | Priority | Recommendation |
133
+
134
+ 6. **Couldn't Touch** — things you wanted to fix but couldn't because:
135
+ - Tests broke when you tried
136
+ - The change was too risky without team input
137
+ - You weren't sure about the intended behavior
138
+
139
+ 7. **Recommendations** — larger refactoring opportunities you noticed that deserve their own effort
140
+
141
+ ## Rules
142
+ - Branch: `codebase-cleanup-[date]`
143
+ - Run tests after EVERY change. No exceptions.
144
+ - If tests fail, revert immediately and document why
145
+ - Make small, atomic commits — one logical change per commit
146
+ - Commit messages should start with `chore:` and clearly describe what was done
147
+ - DO NOT change any business logic. If you're unsure whether something is dead code or intentional, leave it and document it
148
+ - DO NOT refactor working code just because you'd write it differently. Only fix actual issues: dead code, duplication, inconsistency.
149
+ - When in doubt, document rather than change. Conservative changes that keep tests green are infinitely more valuable than aggressive changes that might break things.
150
+ - You have all night. Be thorough. Check every directory.
151
+ ```
152
+
153
+ ## Chat Output Requirement
154
+
155
+ In addition to writing the full report file, you MUST print a summary directly in the conversation when you finish. Do not make the user open the report to get the highlights. The chat summary should include:
156
+
157
+ ### 1. Status Line
158
+ One sentence: what you did, how long it took, and whether all tests still pass.
159
+
160
+ ### 2. Key Findings
161
+ The most important things discovered — bugs, risks, wins, or surprises. Each bullet should be specific and actionable, not vague. Lead with severity or impact.
162
+
163
+ **Good:** "CRITICAL: No backup configuration found for the primary Postgres database — total data loss risk."
164
+ **Bad:** "Found some issues with backups."
165
+
166
+ ### 3. Changes Made (if applicable)
167
+ Bullet list of what was actually modified, added, or removed. Skip this section for read-only analysis runs.
168
+
169
+ ### 4. Recommendations
170
+
171
+ If there are legitimately beneficial recommendations worth pursuing right now, present them in a table. Do **not** force recommendations — if the audit surfaced no actionable improvements, simply state that no recommendations are warranted at this time and move on.
172
+
173
+ When recommendations exist, use this table format:
174
+
175
+ | # | Recommendation | Impact | Risk if Ignored | Worth Doing? | Details |
176
+ |---|---|---|---|---|---|
177
+ | *Sequential number* | *Short description (≤10 words)* | *What improves if addressed* | *Low / Medium / High / Critical* | *Yes / Probably / Only if time allows* | *1–3 sentences explaining the reasoning, context, or implementation guidance* |
178
+
179
+ Order rows by risk descending (Critical → High → Medium → Low). Be honest in the "Worth Doing?" column — not everything flagged is worth the engineering time. If a recommendation is marginal, say so.
180
+
181
+ ### 5. Report Location
182
+ State the full path to the detailed report file for deeper review.
183
+
184
+ ---
185
+
186
+ **Formatting rules for chat output:**
187
+ - Use markdown headers, bold for severity labels, and bullet points for scannability.
188
+ - Do not duplicate the full report contents — just the highlights and recommendations.
189
+ - If you made zero findings in a phase, say so in one line rather than omitting it silently.
@@ -0,0 +1,196 @@
1
+ # Cross-Cutting Concerns Consistency Audit
2
+
3
+ Find patterns that should be identical across the codebase but have drifted. Other audits check within a single module; this one checks each pattern **across every instance, file, and layer** — drift between implementations is the bug.
4
+
5
+ Branch: `cross-cutting-consistency-[date]`. Report: `audit-reports/11_CROSS_CUTTING_CONSISTENCY_REPORT_[run-number]_[date]_[time in user's local time].md`.
6
+
7
+ ---
8
+
9
+ ## Global Rules
10
+
11
+ - Run tests after every change. Commits: `fix: standardize [pattern] in [module]`
12
+ - Per concern: **(1)** find every instance, **(2)** identify dominant/best pattern, **(3)** catalog every deviation, **(4)** fix only mechanical low-risk deviations, **(5)** document everything else.
13
+ - **Only fix** when: canonical pattern is unambiguous, change is mechanical (not behavioral), code has test coverage, no API contract or user-facing behavior changes.
14
+ - **Do NOT fix**: public endpoint response shapes, business logic, untested code, or 50/50 splits (document both, recommend team decision).
15
+ - **Be exhaustive.** "37 of 40 endpoints use offset/limit, 3 use cursor-based" is valuable. "Most use offset/limit" is not. Count everything.
16
+ - For multi-tenancy and soft-delete: missing filters = potential data leak bugs. Treat as security-severity.
17
+
18
+ ---
19
+
20
+ ## Phase 1: Pagination Consistency
21
+
22
+ Find every list/collection endpoint, query, UI list, and GraphQL connection. For each, catalog:
23
+
24
+ - **Strategy**: offset/limit, page/pageSize, cursor-based, keyset, or unbounded
25
+ - **Param names**: `page`/`limit`/`per_page`/`pageSize`/`cursor`/`after`/`next_token`/etc.
26
+ - **Defaults & max page size** (flag missing maximums)
27
+ - **Response metadata shape**: `{ total, page, pageSize }` vs `{ totalCount, hasMore, nextCursor }` vs wrapped vs none
28
+ - **Where logic lives**: handler, service, shared utility, ORM scope, inline
29
+ - **Edge cases**: page 0 vs 1, negative, beyond total, pageSize=0, pageSize=999999
30
+
31
+ **Safe fixes**: Add missing max page size limits. Align internal param names. Standardize metadata on internal endpoints. Add missing defaults.
32
+
33
+ **Report table**: Location | Type | Strategy | Params | Defaults | Max Size | Metadata Shape | Canonical? | Fixed?
34
+
35
+ ---
36
+
37
+ ## Phase 2: Sorting & Filtering Consistency
38
+
39
+ Find every sortable/filterable endpoint or dynamic query. Catalog:
40
+
41
+ - **Sort format**: `?sort=name` vs `?sort_by=name&order=asc` vs `?sort=name:asc` vs others
42
+ - **Multi-field sort** support and syntax
43
+ - **Default sort** (explicit or implicit insertion order = fragile)
44
+ - **Filter format**: `?status=active` vs `?filter[status]=active` vs others
45
+ - **Filter operators**: equality only or range/contains/in? Consistent syntax?
46
+ - **Search**: `?q=` vs `?search=` vs `?query=` — type (full-text, LIKE, regex) and which fields
47
+ - **Validation**: sort fields checked against allowlist? (Flag missing allowlists)
48
+ - **SQL injection risk**: dynamic fields parameterized or concatenated? (**CRITICAL** if concatenated)
49
+
50
+ **Safe fixes**: Standardize internal param names. Add missing sort field allowlists. Add default sorts. Fix SQL injection risks.
51
+
52
+ **Report table**: Location | Sort Format | Filter Format | Search Format | Default Sort | Validated? | Canonical?
53
+
54
+ ---
55
+
56
+ ## Phase 3: Soft Delete & Data Lifecycle Consistency
57
+
58
+ Find every deletion operation (DELETE queries, `.destroy()`/`.delete()`, status→deleted/archived, `deleted_at`/`is_deleted` updates, hard deletes). Catalog:
59
+
60
+ - **Strategy**: hard delete, soft delete (timestamp vs boolean vs status-based), or mixed
61
+ - **Field used**: `deleted_at` vs `deletedAt` vs `is_deleted` vs `status` vs `active` (inverted)
62
+ - **Query filtering**: do ALL read queries on soft-delete tables exclude deleted records? (Missing filters = silent data integrity bugs)
63
+ - **Unique constraints**: can soft-deleted records block new records with same unique field?
64
+ - **Cascade**: parent soft-deleted → children soft-deleted? hard-deleted? orphaned?
65
+ - **Restoration path**, permanent purge process
66
+ - **API behavior**: DELETE returns what? GET on deleted record returns 404, 410, or flagged record?
67
+
68
+ **Most dangerous drift**: some queries filtering soft-deleted records, others not.
69
+
70
+ **Safe fixes**: Add missing `WHERE deleted_at IS NULL`. Standardize field names via migration files (don't run).
71
+
72
+ **Report table**: Entity | Strategy | Field | All Queries Filter? | Cascade | Unique Constraint Issue? | Restoration? | Purge?
73
+
74
+ ---
75
+
76
+ ## Phase 4: Audit Logging & Activity Tracking Consistency
77
+
78
+ Find every audit mechanism (audit tables, activity feeds, event tracking, `created_by`/`updated_by` fields, timestamps, change history, webhooks). For every create/update/delete on every significant entity, catalog:
79
+
80
+ - **Is it logged?** Via what mechanism?
81
+ - **What's captured?** Actor, action, target, timestamp, before/after values, IP/session context
82
+ - **Where logged?** Inline, middleware, ORM hook, DB trigger, event subscriber
83
+ - **Storage & retention**
84
+
85
+ **Flag operations with no audit trail**, especially: user data changes, permission/role changes, financial ops, admin actions, auth events, data exports, config changes, deletions.
86
+
87
+ **Safe fixes**: Add missing `updated_at` auto-update. Populate `created_by`/`updated_by` where pattern exists but was missed. Add audit entries for unlogged critical ops using existing mechanism. Do NOT introduce new audit mechanisms.
88
+
89
+ **Report table**: Entity | Create Logged? | Update Logged? (diff?) | Delete Logged? | Actor Captured? | Mechanism | Gaps
90
+
91
+ ---
92
+
93
+ ## Phase 5: Timezone & Date/Time Handling Consistency
94
+
95
+ Find every date/time operation (creation, parsing, formatting, storage, comparison, arithmetic, timezone conversion, display). Catalog:
96
+
97
+ - **Storage TZ**: UTC, server-local, user-local, mixed? Column types: `TIMESTAMP` vs `TIMESTAMPTZ` vs `DATETIME` vs `VARCHAR`?
98
+ - **Library**: `Date`, `moment`, `date-fns`, `dayjs`, `luxon` — multiple in use?
99
+ - **Server-side creation**: `new Date()` (server TZ), `Date.now()`, `moment.utc()`, DB `NOW()`?
100
+ - **User display**: converted to user TZ? Where does user TZ come from?
101
+ - **API format**: ISO 8601? Unix timestamps? Locale strings? Mixed?
102
+ - **Date-only values**: stored as datetime (midnight of which TZ?), date type, or string?
103
+ - **DST handling**: adds 24 hours (wrong) or 1 calendar day (right)?
104
+ - **Date boundaries**: "today's records" — whose today?
105
+
106
+ **Dangerous drift**: some DB columns UTC, others server-local (invisible until multi-zone or DST). Mixed API date formats.
107
+
108
+ **Safe fixes**: Replace `new Date()` with UTC equivalents per convention. Standardize internal API dates to ISO 8601. Add TZ-aware column types in migration files. Replace deprecated date library usage.
109
+
110
+ **Report table**: Location | Operation | Library | Timezone | Format | Storage Type | Canonical? | Risk
111
+
112
+ ---
113
+
114
+ ## Phase 6: Currency & Numeric Precision Consistency
115
+
116
+ **Skip if app doesn't handle money/prices/precision-sensitive numbers. State why.**
117
+
118
+ Find every monetary/precision operation. Catalog:
119
+
120
+ - **Storage**: integer cents, `DECIMAL(x,y)`, `FLOAT`, string?
121
+ - **Code representation**: float, BigDecimal, integer cents, money library?
122
+ - **Arithmetic**: float math (precision loss), integer math (truncation), library-based?
123
+ - **Rounding**: method and consistency
124
+ - **Currency**: hardcoded, per-record, configurable?
125
+ - **Display & API format**: consistent?
126
+
127
+ **Dangerous drift**: mixing float and integer cents (off-by-one-cent bugs at scale).
128
+
129
+ **Report table**: Location | Value Type | Storage | Code Rep | Arithmetic | Rounding | Display | Canonical? | Precision Risk?
130
+
131
+ ---
132
+
133
+ ## Phase 7: Multi-Tenancy & Data Isolation Consistency
134
+
135
+ **Skip if single-tenant with no org/workspace/team concept. State why.**
136
+
137
+ Identify tenancy model (row-level `tenant_id`, schema-per-tenant, etc.). For every query, endpoint, and background job, audit:
138
+
139
+ - **Tenant scoping applied?** Via middleware (automatic) or manual per-query?
140
+ - **Bypassable?** Can developers write unscoped queries?
141
+ - **Background jobs**: receive and enforce tenant context?
142
+ - **Caches, file storage, search indexes**: tenant-scoped?
143
+ - **Unique constraints**: scoped to tenant?
144
+
145
+ **Missing tenant scoping on user-data table = CRITICAL cross-tenant data exposure.**
146
+
147
+ **Report table**: Entity | Has tenant_id? | Scoping Method | All Queries Scoped? | Cache Scoped? | Files Scoped? | Gaps
148
+
149
+ ---
150
+
151
+ ## Phase 8: Error Response & Status Code Consistency
152
+
153
+ For each scenario below, find every occurrence across all endpoints and compare responses:
154
+
155
+ **Scenarios**: Validation failure (400 vs 422, shape), Not found (404), Not authorized (401 vs 403), Forbidden, Conflict/duplicate (409 vs 400), Rate limited (429 + headers), Internal error (500, detail leakage), Method not allowed (405), Request too large (413)
156
+
157
+ For each: catalog dominant response, deviations (with location), and whether deviation is intentional.
158
+
159
+ **Safe fixes**: Align error responses on internal endpoints to dominant pattern.
160
+
161
+ **Report table per scenario**: Endpoint | Status Code | Response Shape | Message | Canonical? | Fixed?
162
+
163
+ ---
164
+
165
+ ## Phase 9: Synthesis & Drift Map
166
+
167
+ ### Drift Heat Map
168
+ Rate each concern: **Consistent** (90%+), **Minor drift** (70-90%), **Significant drift** (50-70%), **No standard** (<50%).
169
+
170
+ ### Root Cause Analysis
171
+ Per area with significant drift: missing shared utility? Convention changed over time? Different developer conventions? Pattern never decided?
172
+
173
+ ### Prevention Recommendations
174
+ Per concern: shared utility to build, linter rule to enforce, code review checklist item, documentation to write.
175
+
176
+ ---
177
+
178
+ ## Chat Output Requirement
179
+
180
+ Print a summary in conversation (don't make user open the report):
181
+
182
+ 1. **Status Line** — What you did, whether tests pass.
183
+ 2. **Key Findings** — Specific, actionable bullets with severity. Lead with impact.
184
+ - ✅ "CRITICAL: 4 of 22 queries on `orders` don't filter `deleted_at` — soft-deleted orders appear in invoices."
185
+ - ❌ "Found some inconsistencies with soft deletes."
186
+ 3. **Changes Made** (if any) — Bullet list. Skip for read-only runs.
187
+ 4. **Drift Heat Map** — Summary table from report.
188
+ 5. **Recommendations** — Table (only if warranted):
189
+
190
+ | # | Recommendation | Impact | Risk if Ignored | Worth Doing? | Details |
191
+ |---|---|---|---|---|---|
192
+ | *n* | *≤10 words* | *What improves* | *Low/Med/High/Critical* | *Yes/Probably/Only if time* | *1–3 sentences* |
193
+
194
+ Order by risk descending. Be honest in "Worth Doing?" — not everything is worth the engineering time.
195
+
196
+ 6. **Report Location** — Full path to detailed report.