Continuous Codebase Archeology Pipeline
This pipeline automates the excavation of a codebase to reveal dead paths (unused code/branches), historical decisions (rationale behind changes from commits/PRs/comments), and opportunities for consolidation (duplicates, bloat, refactoring candidates). It's designed to run continuously via CI/CD (e.g., GitHub Actions, GitLab CI, Jenkins) on triggers like pushes, PRs, or schedules (e.g., weekly).
The pipeline is language-agnostic where possible, leveraging Git for history and portable tools, with hooks for language-specific analyzers. It outputs actionable reports, visualizations, and alerts. Estimated runtime: 10-60 mins per run on a mid-sized repo (modular for parallelism).
High-Level Architecture
[Triggers] → [Data Ingestion] → [Analysis Layers] → [Synthesis & Insights] → [Outputs & Actions]
(Push/PR/ (Git clone, (Dead Paths, (Cross-layer (Reports, DB,
Schedule) metrics) History, correlation) Alerts)
Consolidation)
Detailed Pipeline Stages
1. Triggers & Orchestration (Entry Point)
- Tools: GitHub Actions workflow YAML, GitLab CI
.gitlab-ci.yml, or Jenkinsfile.
- Schedules:
| Trigger | Frequency | Use Case |
|---|
| Push/PR | On commit/merge | Catch issues early |
| Cron | Weekly | Full archeology sweep |
| Manual | Ad-hoc | Targeted branch analysis |
- Config: Repo-specific YAML (
.codebase-archeology.yml) for tools, thresholds, exclusions (e.g., vendor/, tests).
- Parallelism: Fan out analysis to matrix jobs (e.g., per language/submodule).
2. Data Ingestion (5-10 mins)
- Clone Full History:
git clone --mirror or shallow + fetch (optimize for large repos).
- Extract Artifacts:
| Data Type | Tools/Commands | Purpose |
|---|
| Commits/PRs | git log --pretty=fuller, GitHub API | Messages, authors, dates |
| Blame | git blame --porcelain | Last touch per line |
| Coverage Baselines | Run unit/integration tests + coverage (e.g., pytest --cov, gcovr) | Dynamic usage |
| Dependencies | cargo tree (Rust), npm ls (JS), go list | Unused deps |
| Issues/Comments | GitHub/GitLab API | Context for decisions |
- Store Raw Data: S3/Artifact bucket or GitHub Artifacts for diffing across runs.
3. Analysis Layers (Parallel, 5-30 mins)
Run in parallel jobs for speed.
3.1 Dead Paths Detection
- Static:
| Tool | Languages | Detects |
|---|
| Vulture | Python | Unused functions/code |
| deadcode | Python | Dead imports/calls |
| unused | JS/TS | Unused exports |
| cargo-deadlinks | Rust | Broken links |
Generic: grep -r + AST (e.g., Tree-sitter CLI) | Any | Orphaned branches (if False:) |
- Dynamic: Test coverage < 80% → flag uncovered lines as suspects.
- Advanced: Feature flag scanner (e.g.,
grep -r 'FF_\w+'), zombie APIs (endpoints w/o calls).
- Output: JSON list of files:lines with confidence scores (e.g., 90% dead if never covered + no refs).
3.2 Historical Decisions Mining
- Timeline Reconstruction:
| Technique | Tools | Uncovers |
|---|
| Commit Graph | git log --graph --oneline + git-quick-stats | Churn hotspots, author patterns |
| Message NLP | spaCy or OpenAI API | Keywords: "hack", "temporary", "deprecated" → decisions |
| Blame Heatmap | git-heatmap | Code age (lines untouched >1yr = legacy risk) |
| PR/Issue Threads | GitHub API + LLM summarizer (e.g., GPT-4o-mini) | "Why this refactor?" summaries |
- Flag Decisions: Regex/LLM for patterns like "WIP: disable for perf" → potential dead paths.
- Output: Timeline SVG + Markdown summaries (e.g., "Feature X abandoned in 2022 due to Y").
3.3 Consolidation Opportunities
- Duplicates:
| Tool | Languages | Detects |
|---|
| PMD-CPD | Java/JS/etc. | Token-based dupes |
| jscpd | Multi-lang | AST dupes |
| cloc + diff | Any | Similar file structures |
- Other:
- Unused deps (e.g.,
depcheck for JS, cargo unused).
- Overlapping abstractions (LLM: "cluster similar classes").
- Migration candidates (e.g., deprecated libs via
pip check).
- Scoring: Opportunity score = (dupe ratio * maintenance cost) / reuse potential.
- Output: Ranked list (e.g., "Consolidate utils/auth.go + helpers/auth.py: 85% similar").
4. Synthesis & Insights (2-5 mins)
- Correlation Engine (Python script or Airflow DAG):
- Cross-link: Dead path → linked to abandoned feature (from history).
- Trends: Churn vs. age → "Refactor modules touched 50x but 80% dead."
- LLM Augmentation: Feed raw JSON to local LLM (e.g., Llama3 via Ollama) for narratives: "Historical decision: Switched to LibZ in 2021 for perf; now 60% unused."
- Thresholds: Alert if >10% dead code or >20% dupes.
- Diff from Last Run:
git diff on reports for deltas.
5. Outputs & Actions (1 min)
- Formats:
| Output | Tool | Consumers |
|---|
| Interactive Dashboard | Streamlit or Grafana (hosted on GitHub Pages) | Heatmaps, timelines |
| Markdown Report | Uploaded to PR/wiki | Humans |
| JSON/CSV | Persistent DB (e.g., BigQuery, SQLite) | Queries over time |
| Alerts | Slack/Teams webhook, GitHub Issue bot | "🚨 15% dead code in /legacy/" |
- Actions:
- Auto-comment PRs with findings.
- Create tickets for top issues (GitHub API).
- Block merges if critical (e.g., >5% new dead code).
Sample GitHub Actions Workflow
name: Codebase Archeology
on: [push, pull_request, schedule: {cron: '0 0 * * 0'}]
jobs:
archeology:
runs-on: ubuntu-latest
strategy: {matrix: {lang: [python, js]}} # Parallel per lang
steps:
- uses: actions/checkout@v4 # Full history via token
- name: Ingest Data
run: git fetch --unshallow && ./ingest.sh
- name: Dead Paths
run: vulture . --min-confidence 80 # Example
- name: History
run: git log --author-date-order | python summarize_history.py
- name: Consolidation
run: jscpd . --reporter json
- name: Synthesize
run: python synthesize.py --prev-report ${{ github.event.before.sha }}
- name: Upload Report
uses: actions/upload-artifact@v4
with: {name: archeology-report, path: report.md}
- name: Notify
if: failure()
uses: 8398a7/action-slack@v3
with: {status: ${{ job.status }}, text: 'Archeology Alert!'}
Scaling & Customization
- Large Repos: Sample subdirs, incremental analysis (
git diff HEAD~10).
- Monorepo: Per-workspace matrix.
- Cost: Free on GitHub (2k mins/mo); self-host for privacy.
- Extensibility: Plugin system for new tools (e.g., Rust analyzer).
- Metrics Tracked Over Time: Dead code %, avg code age, consolidation savings (est. LOC reduced).
This pipeline turns your codebase into a living museum—uncovering treasures and trash automatically. Deploy it, and watch tech debt surface! Questions on implementation?