Skip to main content

Work-Wiki-Diff Schema

Complete schema for the Git diff history tracking database.
╔═════════════════════════════════════════════════════════════════════╗
║                   WORK-WIKI-DIFF.DB SCHEMA                           ║
╠═════════════════════════════════════════════════════════════════════╣
║                                                                      ║
║   ┌─────────────────────────┐  ┌─────────────────────────┐           ║
║   │      file_diffs         │  │       settings          │           ║
║   │                         │  │                         │           ║
║   │ - ts                    │  │ - key (PK)              │           ║
║   │ - abs_path              │  │ - value                 │           ║
║   │ - rel_path              │  │                         │           ║
║   │ - git_repo_path         │  │ Defaults:               │           ║
║   │ - head_hash             │  │ - retention_days: 30    │           ║
║   │ - diff_content          │  │ - max_file_size: 1MB    │           ║
║   │ - additions/deletions   │  │ - enabled: true         │           ║
║   │ - committed_hash (v2)   │  │                         │           ║
║   │ - ai_session_id (v2)    │  │                         │           ║
║   │ - agent_name (v2)       │  │                         │           ║
║   └─────────────────────────┘  └─────────────────────────┘           ║
║                                                                      ║
║   ┌─────────────────────────┐  ┌─────────────────────────┐           ║
║   │      ai_sessions        │  │  commit_work_mapping    │           ║
║   │         (v2)            │  │         (v2)            │           ║
║   │                         │  │                         │           ║
║   │ - id (PK)               │  │ - commit_hash           │           ║
║   │ - agent_name            │  │ - diff_id (FK)          │           ║
║   │ - model_id              │  │ - repo_path             │           ║
║   │ - started_at            │  │ - linked_at             │           ║
║   │ - ended_at              │  │                         │           ║
║   │ - total_saves           │  │                         │           ║
║   │ - total_additions       │  │                         │           ║
║   └─────────────────────────┘  └─────────────────────────┘           ║
║                                                                      ║
║                       work-wiki-diff.db                              ║
╚═════════════════════════════════════════════════════════════════════╝

Main Table: file_diffs

Stores every captured diff.
╔═════════════════════════════════════════════════════════════════════╗
║                      FILE_DIFFS TABLE                                ║
╠═════════════════════════════════════════════════════════════════════╣
║                                                                      ║
║   Column         │ Type     │ Description                            ║
║   ═══════════════│══════════│════════════════════════════════════════║
║   id             │ INTEGER  │ Unique ID (auto-increment)             ║
║   ts             │ INTEGER  │ Timestamp (seconds since epoch)        ║
║   abs_path       │ TEXT     │ Absolute file path                     ║
║   rel_path       │ TEXT     │ Path relative to git root              ║
║   git_repo_path  │ TEXT     │ Git repository root directory          ║
║   head_hash      │ TEXT     │ Git HEAD commit hash at save time      ║
║   diff_content   │ TEXT     │ Full diff text (unified format)        ║
║   additions      │ INTEGER  │ Number of lines added                  ║
║   deletions      │ INTEGER  │ Number of lines deleted                ║
║   file_size      │ INTEGER  │ File size in bytes                     ║
║   created_at     │ DATETIME │ When record was inserted               ║
║                                                                      ║
║   v2 COLUMNS (AI Attribution):                                       ║
║   committed_hash │ TEXT     │ Commit hash (after commit)             ║
║   parent_hash    │ TEXT     │ Parent commit hash                     ║
║   change_type    │ TEXT     │ 'save' or 'commit'                     ║
║   ai_session_id  │ TEXT     │ Reference to ai_sessions.id            ║
║   agent_name     │ TEXT     │ claude/gemini/openai/etc.              ║
║                                                                      ║
╚═════════════════════════════════════════════════════════════════════╝

Settings Table

Configuration key-value store.
╔═════════════════════════════════════════════════════════════════════╗
║                       SETTINGS TABLE                                 ║
╠═════════════════════════════════════════════════════════════════════╣
║                                                                      ║
║   Column │ Type │ Description                                        ║
║   ═══════│══════│════════════════════════════════════════════════════║
║   key    │ TEXT │ Setting key (Primary Key)                          ║
║   value  │ TEXT │ Setting value                                      ║
║                                                                      ║
║   DEFAULT VALUES                                                     ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │                                                               │  ║
║   │   Key              │ Default   │ Description                  │  ║
║   │   ═════════════════│═══════════│══════════════════════════════│  ║
║   │   retention_days   │ 30        │ Days to keep before cleanup  │  ║
║   │   max_file_size    │ 1048576   │ Max file size (1MB) for      │  ║
║   │                    │           │ storing diff content         │  ║
║   │   enabled          │ true      │ Enable/disable capture       │  ║
║   │                                                               │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║                                                                      ║
╚═════════════════════════════════════════════════════════════════════╝

AI Sessions Table (v2)

Track AI agent work sessions.
╔═════════════════════════════════════════════════════════════════════╗
║                     AI_SESSIONS TABLE (v2)                           ║
╠═════════════════════════════════════════════════════════════════════╣
║                                                                      ║
║   Column           │ Type    │ Description                           ║
║   ═════════════════│═════════│═══════════════════════════════════════║
║   id               │ TEXT    │ Session UUID (Primary Key)            ║
║   agent_name       │ TEXT    │ claude/gemini/openai/etc.             ║
║   model_id         │ TEXT    │ Specific model identifier             ║
║   started_at       │ INTEGER │ Session start timestamp               ║
║   ended_at         │ INTEGER │ Session end timestamp (nullable)      ║
║   terminal_id      │ TEXT    │ Associated terminal                   ║
║   repo_path        │ TEXT    │ Working repository                    ║
║   total_saves      │ INTEGER │ Number of file saves in session       ║
║   total_additions  │ INTEGER │ Total lines added                     ║
║   total_deletions  │ INTEGER │ Total lines deleted                   ║
║                                                                      ║
║   USE CASE: AI Attribution                                           ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │                                                               │  ║
║   │   "How much code did Claude write today?"                     │  ║
║   │                                                               │  ║
║   │   SELECT agent_name,                                          │  ║
║   │          SUM(total_additions) as lines_added,                 │  ║
║   │          SUM(total_deletions) as lines_deleted                │  ║
║   │   FROM ai_sessions                                            │  ║
║   │   WHERE started_at > (TODAY_START)                            │  ║
║   │   GROUP BY agent_name                                         │  ║
║   │                                                               │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║                                                                      ║
╚═════════════════════════════════════════════════════════════════════╝

Commit-Work Mapping (v2)

Link commits to the work that created them.
╔═════════════════════════════════════════════════════════════════════╗
║                  COMMIT_WORK_MAPPING TABLE (v2)                      ║
╠═════════════════════════════════════════════════════════════════════╣
║                                                                      ║
║   Column      │ Type    │ Description                                ║
║   ════════════│═════════│════════════════════════════════════════════║
║   commit_hash │ TEXT    │ Git commit hash (PK 1)                     ║
║   diff_id     │ INTEGER │ Reference to file_diffs.id (PK 2)          ║
║   repo_path   │ TEXT    │ Repository path                            ║
║   linked_at   │ INTEGER │ When the mapping was created               ║
║                                                                      ║
║   USE CASE: Commit Archaeology                                       ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │                                                               │  ║
║   │   "What work led to commit abc123?"                           │  ║
║   │                                                               │  ║
║   │   SELECT fd.abs_path, fd.additions, fd.deletions,             │  ║
║   │          fd.diff_content, fd.ai_session_id                    │  ║
║   │   FROM commit_work_mapping cwm                                │  ║
║   │   JOIN file_diffs fd ON cwm.diff_id = fd.id                   │  ║
║   │   WHERE cwm.commit_hash = 'abc123'                            │  ║
║   │                                                               │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║                                                                      ║
╚═════════════════════════════════════════════════════════════════════╝

Data Flow: Diff Capture

╔═════════════════════════════════════════════════════════════════════╗
║                      DIFF CAPTURE FLOW                               ║
╠═════════════════════════════════════════════════════════════════════╣
║                                                                      ║
║   File Save Event                                                    ║
║        │                                                             ║
║        ▼                                                             ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │  git2::Repository::discover()                                 │  ║
║   │  Find .git directory                                          │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║        │                                                             ║
║        │ Repository found                                            ║
║        ▼                                                             ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │  repo.diff_tree_to_workdir_with_index()                       │  ║
║   │  Calculate diff: HEAD vs working tree                         │  ║
║   │  Extract: additions, deletions, diff_content                  │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║        │                                                             ║
║        │ Diff calculated                                             ║
║        ▼                                                             ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │  Deduplication Check                                          │  ║
║   │                                                               │  ║
║   │  SELECT id FROM file_diffs                                    │  ║
║   │  WHERE abs_path = ? AND head_hash = ?                         │  ║
║   │    AND diff_content = ?                                       │  ║
║   │                                                               │  ║
║   │  If exists: SKIP (no duplicate insert)                        │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║        │                                                             ║
║        │ New diff                                                    ║
║        ▼                                                             ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │  INSERT INTO file_diffs                                       │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║                                                                      ║
╚═════════════════════════════════════════════════════════════════════╝

Data Flow: AI Attribution

╔═════════════════════════════════════════════════════════════════════╗
║                   AI ATTRIBUTION FLOW (v2)                           ║
╠═════════════════════════════════════════════════════════════════════╣
║                                                                      ║
║   AI Session Starts                                                  ║
║        │                                                             ║
║        │ start_ai_session(agent_name, terminal_id, repo_path)        ║
║        ▼                                                             ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │  INSERT INTO ai_sessions                                      │  ║
║   │  (id, agent_name, started_at, terminal_id, repo_path)         │  ║
║   │  RETURN session_id                                            │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║        │                                                             ║
║        ▼                                                             ║
║   File Saved During Session                                          ║
║        │                                                             ║
║        │ record_with_session(file_path, session_id)                  ║
║        ▼                                                             ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │  INSERT INTO file_diffs                                       │  ║
║   │  (... , ai_session_id = session_id, agent_name = ?)           │  ║
║   │                                                               │  ║
║   │  UPDATE ai_sessions                                           │  ║
║   │  SET total_saves += 1,                                        │  ║
║   │      total_additions += additions,                            │  ║
║   │      total_deletions += deletions                             │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║        │                                                             ║
║        ▼                                                             ║
║   AI Session Ends                                                    ║
║        │                                                             ║
║        │ end_ai_session(session_id)                                  ║
║        ▼                                                             ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │  UPDATE ai_sessions                                           │  ║
║   │  SET ended_at = NOW()                                         │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║                                                                      ║
╚═════════════════════════════════════════════════════════════════════╝

Retention Policy

╔═════════════════════════════════════════════════════════════════════╗
║                     RETENTION MANAGEMENT                             ║
╠═════════════════════════════════════════════════════════════════════╣
║                                                                      ║
║   CLEANUP PROCESS                                                    ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │                                                               │  ║
║   │   cleanup()                                                   │  ║
║   │        │                                                      │  ║
║   │        │ Calculate cutoff                                     │  ║
║   │        │ cutoff = NOW() - (retention_days * 86400)            │  ║
║   │        ▼                                                      │  ║
║   │   DELETE FROM file_diffs                                      │  ║
║   │   WHERE ts < cutoff                                           │  ║
║   │   AND committed_hash IS NOT NULL  -- Keep uncommitted work    │  ║
║   │        │                                                      │  ║
║   │        ▼                                                      │  ║
║   │   VACUUM  -- Reclaim disk space                               │  ║
║   │                                                               │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║                                                                      ║
║   SIZE GUARD                                                         ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │                                                               │  ║
║   │   Before recording diff:                                      │  ║
║   │                                                               │  ║
║   │   IF file_size > max_file_size:                               │  ║
║   │       Set diff_content = NULL                                 │  ║
║   │       Still record metadata (path, ts, additions/deletions)   │  ║
║   │                                                               │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║                                                                      ║
╚═════════════════════════════════════════════════════════════════════╝

Use Cases

╔═════════════════════════════════════════════════════════════════════╗
║                         USE CASES                                    ║
╠═════════════════════════════════════════════════════════════════════╣
║                                                                      ║
║   TIME-TRAVEL DEBUGGING                                              ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │  "What did this file look like 2 hours ago?"                  │  ║
║   │                                                               │  ║
║   │  SELECT diff_content                                          │  ║
║   │  FROM file_diffs                                              │  ║
║   │  WHERE abs_path = '/path/to/file.rs'                          │  ║
║   │    AND ts < (NOW - 7200)                                      │  ║
║   │  ORDER BY ts DESC                                             │  ║
║   │  LIMIT 1                                                      │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║                                                                      ║
║   PRE-COMMIT REVIEW                                                  ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │  "What changes will this commit include?"                     │  ║
║   │                                                               │  ║
║   │  SELECT abs_path, additions, deletions                        │  ║
║   │  FROM file_diffs                                              │  ║
║   │  WHERE git_repo_path = '/path/to/repo'                        │  ║
║   │    AND head_hash = 'current_head'                             │  ║
║   │    AND committed_hash IS NULL                                 │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║                                                                      ║
║   AI PRODUCTIVITY                                                    ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │  "How much code did Claude write today?"                      │  ║
║   │                                                               │  ║
║   │  SELECT SUM(total_additions), SUM(total_deletions)            │  ║
║   │  FROM ai_sessions                                             │  ║
║   │  WHERE agent_name = 'claude'                                  │  ║
║   │    AND started_at > (TODAY_START)                             │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║                                                                      ║
╚═════════════════════════════════════════════════════════════════════╝

Schema Versions

╔═════════════════════════════════════════════════════════════════════╗
║                      SCHEMA VERSIONS                                 ║
╠═════════════════════════════════════════════════════════════════════╣
║                                                                      ║
║   v1 (Original)                                                      ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │  - file_diffs (basic columns)                                 │  ║
║   │  - settings                                                   │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║                                                                      ║
║   v2 (Current) - AI Attribution                                      ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │  + file_diffs.committed_hash                                  │  ║
║   │  + file_diffs.parent_hash                                     │  ║
║   │  + file_diffs.change_type                                     │  ║
║   │  + file_diffs.ai_session_id                                   │  ║
║   │  + file_diffs.agent_name                                      │  ║
║   │  + ai_sessions table                                          │  ║
║   │  + commit_work_mapping table                                  │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║                                                                      ║
║   MIGRATION CHECK                                                    ║
║   ┌───────────────────────────────────────────────────────────────┐  ║
║   │  SELECT COUNT(*)                                              │  ║
║   │  FROM pragma_table_info('file_diffs')                         │  ║
║   │  WHERE name = 'committed_hash'                                │  ║
║   │                                                               │  ║
║   │  If count = 0: Run migration                                  │  ║
║   │  If count > 0: Already migrated                               │  ║
║   └───────────────────────────────────────────────────────────────┘  ║
║                                                                      ║
╚═════════════════════════════════════════════════════════════════════╝

Quick Reference

AspectValue
Location~/Library/Application Support/Monolex/protocols/niia/database/work-wiki-diff.db
Primary OwnerDiff History module
Main Tablesfile_diffs, ai_sessions, commit_work_mapping
Retention30 days (configurable)
Max File Size1MB (larger files skip content storage)
Schema Versionv2 (AI Attribution)