Skip to main content

Change tracking for AI-era codebases

Project description

Selvedge

Tests PyPI License: MIT

Change tracking for AI-era codebases.


Six months ago, your AI agent added a column called user_tier_v2. You don't know why. Git blame points to a commit message that says "Update schema." The agent session that made the change is long gone.

With Selvedge, you run this instead:

$ selvedge blame user_tier_v2

  user_tier_v2
  Changed     2025-10-14 09:31:02
  Agent       claude-code
  Commit      3e7a991
  Reasoning   User asked to add a grandfathering flag for legacy free-tier
              users during the pricing migration. Stores the original tier
              so we can backfill discounts without touching billing history.

That reasoning came from the original request — captured by the agent in the moment, before the session ended.



The problem

Human-written code leaks intent everywhere — commit messages, PR descriptions, inline comments. AI-written code doesn't. The agent knows exactly why it made each decision, but that context lives in the prompt and evaporates when the conversation ends.

Six months later, your team is debugging a schema decision with no trail. git blame tells you what changed and when. It can't tell you why.

Selvedge captures the why.


What's new in v0.3.1

A hardening release — no new feature surface, but Selvedge is now safe to run in long-lived agent pools that hammer the database from multiple threads. Drop-in upgrade for anyone on 0.3.0.

Concurrent writes no longer blow up. Every storage write is wrapped in a database is locked retry with exponential backoff, on top of WAL mode and a 5-second PRAGMA busy_timeout. The new tests/test_concurrency.py spawns 8 threads writing 25 events each and asserts all 200 land — that test reliably failed before this release.

Real schema versioning. The previous try: ALTER TABLE ... except: pass pattern has been replaced with an explicit schema_migrations table that records every applied migration with version, name, and timestamp. Partial failures roll back atomically. Pre-versioning databases are bootstrapped without re-running DDL.

Structured logging. Set SELVEDGE_LOG_LEVEL=DEBUG (or INFO, WARNING, ERROR) to see what's happening inside the storage and migration layers. All library modules log under the selvedge.* namespace — entry points configure a stderr handler at startup.

Public API surface. Library users can now import the supported surface from the top-level package:

from selvedge import (
    SelvedgeStorage, ChangeEvent, ChangeType, EntityType,
    get_db_path, parse_time_string, check_reasoning_quality,
)

The frozen surface is locked in by tests/test_public_api.py — accidental removals fail CI.

MCP protocol smoke tests. A new test suite boots the real selvedge-server subprocess and round-trips every tool over the actual JSON-RPC stdio transport. Catches contract drift the in-process tests miss.

CI gates. ruff, mypy, and an 85% coverage floor are now enforced on every PR. Current coverage is 92%.

One sneaky regex fix. The reasoning-quality validator's ^fixed?$ pattern was supposed to match "fix" and "fixed" but actually matched "fixe"/"fixed" — the ? only made the trailing d optional. Same bug in the add, remove, update, change, and see ... patterns. Rewritten as ^fix(?:ed)?$ etc. Reasoning placeholders that slipped through before now get flagged.


What's new in v0.3.0

A correctness and data-quality release — no new feature surface, but several silent-wrong-answer bugs are now fixed. Recommended upgrade for everyone on 0.2.x.

Time parsing now follows every other CLI's convention. 5m means 5 minutes, not 5 months. Use 5mo (or 5mon) for months. Unparseable inputs like --since yesterday now exit with a clear error instead of silently returning empty results from a string-vs-string compare.

selvedge history --since 15m    # last 15 minutes (was: last ~15 months)
selvedge history --since 5mo    # last 5 months
selvedge history --since 1y     # last year

search() no longer treats _ as a wildcard. Searching for stripe_customer_id used to match stripeXcustomerXid, stripeYcustomerYid, and so on, because SQL LIKE treats underscore as "any single character." All LIKE queries now ESCAPE '\' and escape the input — so the literal underscore in your column name does what you expect.

selvedge import finally works for columns defined in CREATE TABLE. Previously, importing CREATE TABLE users (id INT, email TEXT) produced exactly one event — for the table — and zero for its columns. Six months later, selvedge blame users.email returned "no history found." Now every column in a CREATE TABLE (and every sa.Column(...) in op.create_table) gets its own column.add event.

Timestamps normalized to UTC on write. Mixed-timezone events (2025-01-01T09:00:00-08:00 vs 2025-01-01T10:00:00+00:00) now sort correctly by real time. Previously they sorted by ASCII order of the timezone suffix.

Two events on rename, not one. Both SQL ALTER TABLE old RENAME TO new and Alembic op.rename_table('old', 'new') now emit a rename event for the old name and a create event for the new name — so selvedge blame new_name returns the rename context instead of "no history found."

Schema migration on rename for git_commit coverage. The post-commit hook's default lookback widened from 10 to 60 minutes — long agent sessions no longer lose their git stamp. selvedge status now surfaces the count of events still missing a git_commit so unstamped events are visible at a glance.

Validation in ChangeEvent. Empty entity_path, hallucinated change_type values like "banana", and typos like "modifyed" are now rejected at write time instead of silently inserting orphan rows.

Faster bulk imports. selvedge import wraps inserts in a single transaction (storage.log_event_batch) — orders of magnitude faster on large Alembic histories, and atomic.

Better project DB resolution. get_db_path now walks up looking for the actual selvedge.db file rather than just the .selvedge/ directory. A stray empty .selvedge/ upstream no longer hijacks resolution. Falling back to the global ~/.selvedge/selvedge.db prints a one-time stderr warning so unintentional global use is visible. (Set SELVEDGE_QUIET=1 to suppress.)

Adversarial test suite. 25 new tests covering the bug classes above, so these regressions stay fixed.

See CHANGELOG.md for the full list and reasoning.


Install

pip install selvedge

Quickstart

1. Initialize in your project

cd your-project
selvedge init

2. Add to your Claude Code config

~/.claude/config.json:

{
  "mcpServers": {
    "selvedge": {
      "command": "selvedge-server"
    }
  }
}

3. Tell your agent to use it

Add to your project's CLAUDE.md:

You have access to Selvedge for change tracking.
Call selvedge.log_change immediately after adding, modifying, or removing
any DB column, table, function, API endpoint, dependency, or env variable.
Set `reasoning` to the user's original request or the problem being solved.
Set `agent` to "claude-code".
Before modifying an entity, call selvedge.blame to understand its history.

4. Query your history

selvedge status                        # recent activity + missing-commit count
selvedge diff users                    # all changes to the users table
selvedge diff users.email              # changes to a specific column
selvedge blame payments.amount         # what changed last and why
selvedge history --since 30d           # last 30 days of changes
selvedge history --since 15m           # last 15 minutes ('m' = minutes)
selvedge changeset add-stripe-billing  # all events for a feature/task
selvedge search "stripe"               # full-text search
selvedge stats                         # log_change coverage report
selvedge install-hook                  # auto-link commits to events
selvedge import migrations/            # backfill from migration files
selvedge export --format csv           # dump history to CSV

How it works

Selvedge runs as an MCP server. AI agents in tools like Claude Code call Selvedge's tools as they work — logging structured change events to a local SQLite database.

Each event records:

  • What changed (entity path, change type, diff)
  • When (timestamp)
  • Who (agent, session ID)
  • Why (reasoning — captured from the agent's context in the moment)
  • Where (git commit, project)

The diff is git's job. The why is Selvedge's.


Entity path conventions

users.email           DB column (table.column)
users                 DB table
src/auth.py::login    Function in a file (path::symbol)
src/auth.py           File
api/v1/users          API route
deps/stripe           Dependency
env/STRIPE_SECRET_KEY Environment variable

Prefix queries work everywhere: users returns users, users.email, users.created_at, and any other entity under the users. namespace.


MCP tools

When connected as an MCP server, Selvedge exposes:

Tool Description
log_change Record a change event with entity, diff, and reasoning
diff History for an entity or entity prefix
blame Most recent change + context for an exact entity
history Filtered history across all entities
changeset All events grouped under a named feature/task slug
search Full-text search across all events

CLI reference

selvedge init [--path PATH]               Initialize in project
selvedge status                           Recent activity summary
selvedge diff ENTITY [--limit N]          Change history for entity
selvedge blame ENTITY                     Most recent change + context
selvedge history [--since SINCE]          Browse all history
              [--entity ENTITY]
              [--project PROJECT]
              [--changeset CS]
              [--summarize]
              [--limit N]
selvedge changeset [CHANGESET_ID]         Show events in a changeset
                  [--list]                or list all changesets
                  [--project NAME]
                  [--since SINCE]
selvedge search QUERY [--limit N]         Full-text search
selvedge stats [--since SINCE]            Tool call coverage report
selvedge install-hook [--path PATH]       Install git post-commit hook
                     [--window MIN]       (default 60 minutes)
selvedge backfill-commit --hash HASH      Backfill git_commit on recent events
                        [--window MIN]    (default 60 minutes)
selvedge import PATH                      Import migration files (SQL / Alembic)
              [--format auto|sql|alembic]
              [--project NAME]
              [--dry-run]
selvedge export [--format json|csv]       Export history to JSON or CSV
              [--since SINCE]
              [--entity ENTITY]
              [--output FILE]
selvedge log ENTITY CHANGE_TYPE           Manually log a change
             [--diff TEXT]                CHANGE_TYPE: add, remove, modify,
             [--reasoning TEXT]           rename, retype, create, delete,
             [--agent NAME]               index_add, index_remove, migrate
             [--commit HASH]
             [--project NAME]
             [--changeset CS]

All read commands support --json for machine-readable output.

Relative time in --since:

  • 15m → last 15 minutes (m = minutes)
  • 24h → last 24 hours
  • 7d → last 7 days
  • 5mo → last 5 months (mo or mon = months)
  • 1y → last year

Unparseable inputs (e.g. --since yesterday) exit with a clear error rather than silently returning empty results. ISO 8601 timestamps are also accepted and normalized to UTC.


Configuration

Method Format Example
Env var SELVEDGE_DB=/path/to/db Per-session override
Project init selvedge init Creates .selvedge/selvedge.db in CWD
Global fallback ~/.selvedge/selvedge.db Used if no project DB found

Coverage checking

Wondering how often your agent actually calls log_change? Two ways to check:

# Quick summary in the terminal
selvedge stats

# Cross-reference against git commits
python scripts/coverage_check.py --since 30d

The coverage script compares your git log against Selvedge events and shows which commits have associated change events. Low coverage usually means the system prompt needs strengthening — see docs/fallbacks.md for guidance.


Contributing

git clone https://github.com/masondelan/selvedge
cd selvedge
pip install -e ".[dev]"
pytest

See CLAUDE.md for architecture details and the phase roadmap.


License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

selvedge-0.3.1.tar.gz (75.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

selvedge-0.3.1-py3-none-any.whl (41.5 kB view details)

Uploaded Python 3

File details

Details for the file selvedge-0.3.1.tar.gz.

File metadata

  • Download URL: selvedge-0.3.1.tar.gz
  • Upload date:
  • Size: 75.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for selvedge-0.3.1.tar.gz
Algorithm Hash digest
SHA256 77f97bdf5303f0c6ca558a3a63362ab7fabeedb91301fda1e1d400c9d9edfdf4
MD5 5f52170cf9363f5439855b160fb1cd94
BLAKE2b-256 607bc97763e4c291bbe27acc05e18fe266d529a8f6393d8432d537ec6ca7ef37

See more details on using hashes here.

Provenance

The following attestation bundles were made for selvedge-0.3.1.tar.gz:

Publisher: publish.yml on masondelan/selvedge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file selvedge-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: selvedge-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 41.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for selvedge-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 11d1d364b13995dbf2565a24f1675150007ee46caf9f7084f7a0359da23805db
MD5 14707782c45b28fb9ac4b270fc85a8be
BLAKE2b-256 6b932fd6d538a0fc23be920418c352bff0d03549b2c98b5934140fa342e54cb3

See more details on using hashes here.

Provenance

The following attestation bundles were made for selvedge-0.3.1-py3-none-any.whl:

Publisher: publish.yml on masondelan/selvedge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page