Skip to content

tenets.storage Package

Storage module for persistence and caching.

This module handles all storage needs including: - File analysis caching - Tenet/session persistence - Configuration/state storage

Classes

AnalysisCache

Python
AnalysisCache(cache_dir: Path)

Specialized cache for file analysis results.

Initialize analysis cache.

PARAMETERDESCRIPTION
cache_dir

Directory for cache storage

TYPE:Path

Attributes

cache_dirinstance-attribute
Python
cache_dir = cache_dir
memoryinstance-attribute
Python
memory = MemoryCache(max_size=500)
diskinstance-attribute
Python
disk = DiskCache(cache_dir, name='analysis')
loggerinstance-attribute
Python
logger = get_logger(__name__)

Functions

get_file_analysis
Python
get_file_analysis(file_path: Path) -> Optional[FileAnalysis]

Get cached analysis for a file.

PARAMETERDESCRIPTION
file_path

Path to the file

TYPE:Path

RETURNSDESCRIPTION
Optional[FileAnalysis]

Cached FileAnalysis or None

put_file_analysis
Python
put_file_analysis(file_path: Path, analysis: FileAnalysis) -> None

Cache file analysis.

PARAMETERDESCRIPTION
file_path

Path to the file

TYPE:Path

analysis

Analysis to cache

TYPE:FileAnalysis

close
Python
close() -> None

Close underlying caches.

CacheManager

Python
CacheManager(config: TenetsConfig)

Manages all caching operations.

Initialize cache manager.

PARAMETERDESCRIPTION
config

Tenets configuration

TYPE:TenetsConfig

Attributes

configinstance-attribute
Python
config = config
cache_dirinstance-attribute
Python
cache_dir = Path(cache_dir)
loggerinstance-attribute
Python
logger = get_logger(__name__)
analysisinstance-attribute
Python
analysis = AnalysisCache(cache_dir / 'analysis')
generalinstance-attribute
Python
general = DiskCache(cache_dir / 'general')
memoryinstance-attribute
Python
memory = MemoryCache(max_size=1000)

Functions

get_or_compute
Python
get_or_compute(key: str, compute_fn: Callable[[], T], ttl: Optional[int] = None, use_memory: bool = True) -> T

Get from cache or compute if missing.

PARAMETERDESCRIPTION
key

Cache key

TYPE:str

compute_fn

Function to compute value if not cached

TYPE:Callable[[], T]

ttl

Time to live in seconds

TYPE:Optional[int]DEFAULT:None

use_memory

Whether to use memory cache

TYPE:boolDEFAULT:True

RETURNSDESCRIPTION
T

Cached or computed value

invalidate
Python
invalidate(key: str) -> None

Invalidate cache entry.

clear_all
Python
clear_all() -> None

Clear all caches.

cleanup
Python
cleanup() -> dict[str, int]

Clean up old cache entries.

RETURNSDESCRIPTION
dict[str, int]

Statistics about cleanup

SessionDB

Python
SessionDB(config: TenetsConfig)

SQLite-backed session storage.

Manages two tables
  • sessions(id, name, created_at, metadata)
  • session_context(id, session_id, kind, content, created_at)

class explicitly removes child rows where appropriate.

Attributes

configinstance-attribute
Python
config = config
loggerinstance-attribute
Python
logger = get_logger(__name__)
dbinstance-attribute
Python
db = Database(config)

Functions

create_session
Python
create_session(name: str, metadata: Optional[dict[str, Any]] = None) -> SessionRecord
get_session
Python
get_session(name: str) -> Optional[SessionRecord]
list_sessions
Python
list_sessions() -> list[SessionRecord]
get_active_session
Python
get_active_session() -> Optional[SessionRecord]

Return the currently active session, if any.

Chooses the most recently created active session if multiple are marked active.

add_context
Python
add_context(session_name: str, kind: str, content: str) -> None

Append a context artifact to a session.

PARAMETERDESCRIPTION
session_name

Friendly name of the session.

TYPE:str

kind

Type tag for the content (e.g., "context_result").

TYPE:str

content

Serialized content (JSON string or text).

TYPE:str

delete_session
Python
delete_session(name: str, purge_context: bool = True) -> bool

Delete a session record by name.

This removes the session row and, by default, all related entries from session_context.

PARAMETERDESCRIPTION
name

Session name to delete.

TYPE:str

purge_context

When True (default), also remove all associated rows from session_context.

TYPE:boolDEFAULT:True

RETURNSDESCRIPTION
bool

True if a session row was deleted; False if no session matched.

delete_all_sessions
Python
delete_all_sessions(purge_context: bool = True) -> int

Delete all sessions. Returns the number of sessions removed.

If purge_context is True, also clears all session_context rows.

update_session_metadata
Python
update_session_metadata(name: str, updates: dict[str, Any]) -> bool

Merge updates into the session's metadata JSON.

Returns True if the session exists and was updated.

set_active
Python
set_active(name: str, active: bool) -> bool

Mark a session as active/inactive via metadata.

When activating a session, all other sessions are marked inactive to guarantee there is at most one active session at a time.

Database

Python
Database(config: TenetsConfig)

SQLite database manager applying Tenets pragmas.

Use this to obtain connections to the main Tenets DB file located in the configured cache directory.

Attributes

configinstance-attribute
Python
config = config
loggerinstance-attribute
Python
logger = get_logger(__name__)
pathsinstance-attribute
Python
paths = _resolve_paths(config)

Functions

connect
Python
connect(db_path: Optional[Path] = None) -> sqlite3.Connection

Open a SQLite connection with configured PRAGMAs applied.

PARAMETERDESCRIPTION
db_path

Optional custom DB path; defaults to main DB path.

TYPE:Optional[Path]DEFAULT:None

SQLitePathsdataclass

Python
SQLitePaths(root: Path, main_db: Path)

Resolved paths for SQLite databases.

ATTRIBUTEDESCRIPTION
root

The cache directory root where DB files live.

TYPE:Path

main_db

Path to the main Tenets database file.

TYPE:Path

Attributes

rootinstance-attribute
Python
root: Path
main_dbinstance-attribute
Python
main_db: Path

Modules