Skip to content

feat(context): add intelligent context caching and diff tracking#26

Merged
foundatron merged 1 commit intomainfrom
issue-9
Mar 11, 2026
Merged

feat(context): add intelligent context caching and diff tracking#26
foundatron merged 1 commit intomainfrom
issue-9

Conversation

@foundatron
Copy link
Owner

Closes #9

Changes

1. tentacle/models.py — Add ContextEntry dataclass

  • Add ContextEntry with fields: filename: str, content: str, checksum: str, fetched_at: datetime, id: int | None = None
  • No changes from original plan.

2. tentacle/db.py — Add context_cache table and Store methods

  • Add context_cache table to _SCHEMA: (id INTEGER PRIMARY KEY AUTOINCREMENT, filename TEXT NOT NULL UNIQUE, content TEXT NOT NULL, checksum TEXT NOT NULL, fetched_at TEXT NOT NULL)
  • Add Store.upsert_context(entry: ContextEntry) -> None — INSERT OR REPLACE by filename
  • Add Store.get_context(filename: str) -> ContextEntry | None — fetch cached entry by filename
  • Add _row_to_context_entry converter following existing pattern
  • Drop get_all_context() — nothing uses it.

3. tentacle/context.py — Rewrite with caching and change detection

  • Add import hashlib and imports for Store, ContextEntry from the project
  • Add _checksum(content: str) -> str helper using SHA-256
  • Add ContextResult dataclass: context: str, changed_files: list[str]
    • Drop diff_summary field — checksums are sufficient for change detection; diffs in LLM context add noise, not signal.
  • Refactor _read_file to only handle fetching (local paths + gh CLI). Remove the filesystem cache fallback from _read_file — move it to fetch_context.
  • Rewrite fetch_context(repo_path: str | None = None, store: Store | None = None) -> ContextResult:
    1. For each file in _CONTEXT_FILES, call _read_file(filename, repo_path) to get fresh content.
    2. If content fetched and store provided: compute checksum, compare against store.get_context(filename). If different (or no cached entry), add filename to changed_files. Upsert new entry.
    3. If content fetched: write to _CACHE_DIR / filename as filesystem fallback (with mkdir(parents=True, exist_ok=True) for subdirectories like docs/).
    4. If fetch fails and store provided: fall back to store.get_context(filename) cached content. Log warning.
    5. If fetch fails and no store (or store has no entry): fall back to _CACHE_DIR / filename. Log warning.
    6. Assemble ContextResult.context using existing ### filename\n\ncontent format.
    7. If store=None, changed_files is always empty (no prior state to compare).

4. tentacle/cli.py — Pass store to fetch_context

  • Line 130: context_result = fetch_context(store=store) (note: store is created on line 115)
  • Line 183: change context to context_result.context where passed to analyze_article
  • Add log line when context_result.changed_files is non-empty

Review Findings

  • Errors: 1
  • Warnings: 4
  • Nits: 5
  • Assessment: NEEDS CHANGES

The main error is the double-patching in the test that leaves dead code and works by accident. The warnings around missing error handling on filesystem writes, no cache staleness tracking, and the encoding assumption should be addressed before merge.

…fetching

- Add ContextEntry model and context_cache table in SQLite store
- Rewrite fetch_context() to return ContextResult with context string and
  changed_files list; writes fresh content to both DB and filesystem caches
- Fallback chain: fresh fetch → DB cache → filesystem cache
- Pass store to fetch_context in cli.py; log when context files change
- Add comprehensive tests for context caching, fallbacks, and change detection

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@foundatron foundatron merged commit da93432 into main Mar 11, 2026
1 check passed
@foundatron foundatron deleted the issue-9 branch March 11, 2026 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(context): add intelligent context caching and diff tracking

1 participant