Class FileSessionStore
- All Implemented Interfaces:
SessionStore
- Direct Known Subclasses:
SearchableFileSessionStore
Stores sessions as JSON files organized by date:
basePath/ ├── 2026-02-05/ │ ├── session-abc123.json │ └── session-def456.json ├── 2026-02-04/ │ └── session-ghi789.json └── index.json
Durability: session files and the index are written atomically (temp file +
atomic move), so a crash mid-write cannot leave a half-written JSON file.
Session files that fail to parse on load are quarantined (renamed to
*.corrupt-<timestamp>) and treated as missing instead of failing the
whole store.
The index is only persisted when the entry set actually changes (new, removed, or bucket-moved session), not on every save.
Concurrency: index writes are synchronized and atomic within a JVM, but the store assumes a single-writer contract per directory — two store instances (in the same or different JVMs) writing to the same base path may overwrite each other's index, since each keeps its own in-memory index cache.
- Author:
- TnsAI Team
-
Constructor Summary
ConstructorsConstructorDescriptionFileSessionStore(String basePath) Creates a file session store with no retention (existing behavior).FileSessionStore(String basePath, long maxAgeDays) Creates a file session store with opt-in age-based retention. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all sessions (use with caution).intcount()Counts total sessions.booleanDeletes a session.intEvicts sessions older than the configuredmaxAgeDays(based on session start time).booleanCheap existence check: consults the in-memory index and verifies the session file exists on disk, without parsing any JSON.findByAgent(String agentName) Lists sessions for a specific agent.findByDate(LocalDate date) Lists sessions for a specific date.findByDateRange(LocalDate startDate, LocalDate endDate) Lists sessions in a date range.findByOutcome(SessionOutcome outcome) Lists sessions with a specific outcome.findByProject(String projectPath) Lists sessions for a specific project.Lists sessions with a specific tag.findRecent(int limit) Gets the most recent sessions.Lists all session IDs.Loads a session by ID.voidsave(AgentSession session) Saves a session.
-
Constructor Details
-
FileSessionStore
Creates a file session store with no retention (existing behavior).- Parameters:
basePath- the base directory path
-
FileSessionStore
Creates a file session store with opt-in age-based retention.When
maxAgeDaysis positive, sessions older than the given number of days (based on session start time) are evicted — seeevictExpired(). Retention runs once at construction and on explicitevictExpired()calls; it never runs duringsave(AgentSession). A value of0(or negative) disables retention entirely.- Parameters:
basePath- the base directory pathmaxAgeDays- maximum session age in days,0to disable retention
-
-
Method Details
-
save
Description copied from interface:SessionStoreSaves a session.- Specified by:
savein interfaceSessionStore- Parameters:
session- the session to save
-
load
Description copied from interface:SessionStoreLoads a session by ID.- Specified by:
loadin interfaceSessionStore- Parameters:
sessionId- the session ID- Returns:
- the session if found
-
delete
Description copied from interface:SessionStoreDeletes a session.- Specified by:
deletein interfaceSessionStore- Parameters:
sessionId- the session ID- Returns:
- true if deleted
-
listSessionIds
Description copied from interface:SessionStoreLists all session IDs.- Specified by:
listSessionIdsin interfaceSessionStore- Returns:
- list of session IDs
-
findByDate
Description copied from interface:SessionStoreLists sessions for a specific date.- Specified by:
findByDatein interfaceSessionStore- Parameters:
date- the date- Returns:
- list of sessions
-
findByDateRange
Description copied from interface:SessionStoreLists sessions in a date range.- Specified by:
findByDateRangein interfaceSessionStore- Parameters:
startDate- start date (inclusive)endDate- end date (inclusive)- Returns:
- list of sessions
-
findByAgent
Description copied from interface:SessionStoreLists sessions for a specific agent.- Specified by:
findByAgentin interfaceSessionStore- Parameters:
agentName- the agent name- Returns:
- list of sessions
-
findByProject
Description copied from interface:SessionStoreLists sessions for a specific project.- Specified by:
findByProjectin interfaceSessionStore- Parameters:
projectPath- the project path- Returns:
- list of sessions
-
findByTag
Description copied from interface:SessionStoreLists sessions with a specific tag.- Specified by:
findByTagin interfaceSessionStore- Parameters:
tag- the tag- Returns:
- list of sessions
-
findByOutcome
Description copied from interface:SessionStoreLists sessions with a specific outcome.- Specified by:
findByOutcomein interfaceSessionStore- Parameters:
outcome- the outcome- Returns:
- list of sessions
-
findRecent
Description copied from interface:SessionStoreGets the most recent sessions.- Specified by:
findRecentin interfaceSessionStore- Parameters:
limit- maximum number to return- Returns:
- list of sessions, newest first
-
count
public int count()Description copied from interface:SessionStoreCounts total sessions.- Specified by:
countin interfaceSessionStore- Returns:
- session count
-
exists
Cheap existence check: consults the in-memory index and verifies the session file exists on disk, without parsing any JSON. Unlike the default implementation it never throws on corrupt session files.- Specified by:
existsin interfaceSessionStore- Parameters:
sessionId- the session ID- Returns:
- true if exists
-
evictExpired
public int evictExpired()Evicts sessions older than the configuredmaxAgeDays(based on session start time). No-op when retention is disabled (maxAgeDays <= 0).Runs once at construction; call explicitly to re-apply the policy. Never runs during
save(AgentSession)to keep saves cheap.- Returns:
- number of evicted sessions
-
clear
public void clear()Description copied from interface:SessionStoreClears all sessions (use with caution).- Specified by:
clearin interfaceSessionStore
-