1+ // Session artifact filename classifiers and archive timestamp helpers.
2+ // Cleanup, disk-budget, and usage accounting use these predicates to avoid deleting live transcripts.
3+
14import { timestampMsToIsoFileStamp } from "@openclaw/normalization-core/number-coercion" ;
25import { escapeRegExp } from "../../shared/regexp.js" ;
36
@@ -18,6 +21,7 @@ function hasArchiveSuffix(fileName: string, reason: SessionArchiveReason): boole
1821 return ARCHIVE_TIMESTAMP_RE . test ( raw ) ;
1922}
2023
24+ /** Returns true for archived session artifacts and legacy store backup names. */
2125export function isSessionArchiveArtifactName ( fileName : string ) : boolean {
2226 if ( LEGACY_STORE_BACKUP_RE . test ( fileName ) ) {
2327 return true ;
@@ -59,6 +63,7 @@ export function isSessionStoreTempArtifactName(fileName: string, storeBasename:
5963 return sessionStoreTempPattern ( storeBasename ) . test ( fileName ) ;
6064}
6165
66+ /** Parses a compaction checkpoint transcript filename into session/checkpoint ids. */
6267export function parseCompactionCheckpointTranscriptFileName ( fileName : string ) : {
6368 sessionId : string ;
6469 checkpointId : string ;
@@ -69,22 +74,27 @@ export function parseCompactionCheckpointTranscriptFileName(fileName: string): {
6974 return sessionId && checkpointId ? { sessionId, checkpointId } : null ;
7075}
7176
77+ /** Returns true when a filename is a compaction checkpoint transcript. */
7278export function isCompactionCheckpointTranscriptFileName ( fileName : string ) : boolean {
7379 return parseCompactionCheckpointTranscriptFileName ( fileName ) !== null ;
7480}
7581
82+ /** Returns true for trajectory runtime jsonl artifacts. */
7683export function isTrajectoryRuntimeArtifactName ( fileName : string ) : boolean {
7784 return fileName . endsWith ( ".trajectory.jsonl" ) ;
7885}
7986
87+ /** Returns true for trajectory pointer artifacts. */
8088export function isTrajectoryPointerArtifactName ( fileName : string ) : boolean {
8189 return fileName . endsWith ( ".trajectory-path.json" ) ;
8290}
8391
92+ /** Returns true for any trajectory-related session artifact. */
8493export function isTrajectorySessionArtifactName ( fileName : string ) : boolean {
8594 return isTrajectoryRuntimeArtifactName ( fileName ) || isTrajectoryPointerArtifactName ( fileName ) ;
8695}
8796
97+ /** Returns true for primary session transcript files that represent live session history. */
8898export function isPrimarySessionTranscriptFileName ( fileName : string ) : boolean {
8999 if ( fileName === "sessions.json" ) {
90100 return false ;
@@ -101,13 +111,15 @@ export function isPrimarySessionTranscriptFileName(fileName: string): boolean {
101111 return ! isSessionArchiveArtifactName ( fileName ) ;
102112}
103113
114+ /** Returns true for transcript files counted in usage, including reset/deleted archives. */
104115export function isUsageCountedSessionTranscriptFileName ( fileName : string ) : boolean {
105116 if ( isPrimarySessionTranscriptFileName ( fileName ) ) {
106117 return true ;
107118 }
108119 return hasArchiveSuffix ( fileName , "reset" ) || hasArchiveSuffix ( fileName , "deleted" ) ;
109120}
110121
122+ /** Extracts the session id from a usage-counted transcript filename. */
111123export function parseUsageCountedSessionIdFromFileName ( fileName : string ) : string | null {
112124 if ( isPrimarySessionTranscriptFileName ( fileName ) ) {
113125 return fileName . slice ( 0 , - ".jsonl" . length ) ;
@@ -122,6 +134,7 @@ export function parseUsageCountedSessionIdFromFileName(fileName: string): string
122134 return null ;
123135}
124136
137+ /** Formats an archive timestamp that is safe for filenames. */
125138export function formatSessionArchiveTimestamp ( nowMs = Date . now ( ) ) : string {
126139 return timestampMsToIsoFileStamp ( nowMs ) ;
127140}
0 commit comments