11import fs from "node:fs" ;
22import { afterEach , describe , expect , it } from "vitest" ;
3- import { cleanupTempDirs , makeTempDir } from "../../../test/helpers/temp-dir.js" ;
3+ import {
4+ cleanupTempDirs ,
5+ makeTempDir ,
6+ useAutoCleanupTempDirTracker ,
7+ } from "../../../test/helpers/temp-dir.js" ;
48import {
59 closeOpenClawAgentDatabasesForTest ,
610 isOpenClawAgentDatabaseOpen ,
@@ -15,11 +19,13 @@ import {
1519 hasSessionEntriesByStatusReadOnly ,
1620 listSessionEntriesCore ,
1721 listSessionEntriesReadOnly ,
22+ readSessionIdentityEvidence ,
1823 resolveTranscriptSessionKeyBySessionId ,
1924 upsertSessionEntryCore ,
2025} from "./session-accessor.js" ;
2126
2227const tempDirs : string [ ] = [ ] ;
28+ const autoTempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
2329
2430function countRegisteredAgentDatabases ( env : NodeJS . ProcessEnv ) : number {
2531 const row = openOpenClawStateDatabase ( { env } )
@@ -138,6 +144,75 @@ describe("session accessor readonly listing", () => {
138144 expect ( countRegisteredAgentDatabases ( env ) ) . toBe ( 0 ) ;
139145 } ) ;
140146
147+ it ( "probes session identity by exact key and indexed current session id" , async ( ) => {
148+ const stateDir = autoTempDirs . make ( "openclaw-session-readonly-evidence-" ) ;
149+ const env = { OPENCLAW_STATE_DIR : stateDir } ;
150+ const agentId = "worker-1" ;
151+ const sessionKey = "agent:worker-1:moved" ;
152+ const sessionId = "session-1" ;
153+ await upsertSessionEntryCore ( { agentId, env, sessionKey } , { sessionId, updatedAt : 1 } ) ;
154+ const storePath = resolveOpenClawAgentSqlitePath ( { agentId, env } ) ;
155+ closeOpenClawAgentDatabasesForTest ( ) ;
156+
157+ expect ( readSessionIdentityEvidence ( { agentId, sessionId, sessionKey, storePath } ) ) . toEqual ( {
158+ status : "current" ,
159+ sessionKey,
160+ } ) ;
161+ expect (
162+ readSessionIdentityEvidence ( {
163+ agentId,
164+ sessionId,
165+ sessionKey : "agent:worker-1:old-key" ,
166+ storePath,
167+ } ) ,
168+ ) . toEqual ( { status : "current" , sessionKey } ) ;
169+ expect (
170+ readSessionIdentityEvidence ( {
171+ agentId,
172+ sessionId : "missing-session" ,
173+ sessionKey,
174+ storePath,
175+ } ) ,
176+ ) . toEqual ( { status : "absent" } ) ;
177+ } ) ;
178+
179+ it ( "reports migration-invalid session evidence as unknown" , async ( ) => {
180+ const stateDir = autoTempDirs . make ( "openclaw-session-readonly-evidence-invalid-" ) ;
181+ const env = { OPENCLAW_STATE_DIR : stateDir } ;
182+ const agentId = "worker-1" ;
183+ const sessionKey = "agent:worker-1:main" ;
184+ const sessionId = "session-1" ;
185+ await upsertSessionEntryCore ( { agentId, env, sessionKey } , { sessionId, updatedAt : 1 } ) ;
186+ const database = openOpenClawAgentDatabase ( { agentId, env } ) ;
187+ database . db . exec ( "PRAGMA user_version = 999;" ) ;
188+ const storePath = database . path ;
189+ closeOpenClawAgentDatabasesForTest ( ) ;
190+
191+ expect ( readSessionIdentityEvidence ( { agentId, sessionId, sessionKey, storePath } ) ) . toEqual ( {
192+ status : "unknown" ,
193+ reason : "read-failed" ,
194+ } ) ;
195+ } ) ;
196+
197+ it ( "uses the current-session-id index for fallback identity probes" , async ( ) => {
198+ const stateDir = autoTempDirs . make ( "openclaw-session-readonly-evidence-index-" ) ;
199+ const env = { OPENCLAW_STATE_DIR : stateDir } ;
200+ const agentId = "worker-1" ;
201+ const database = openOpenClawAgentDatabase ( { agentId, env } ) ;
202+ const detail = database . db
203+ . prepare (
204+ "EXPLAIN QUERY PLAN SELECT session_key FROM session_nodes WHERE current_session_id = ? LIMIT 2" ,
205+ )
206+ . all ( "session-1" )
207+ . map ( ( row ) => {
208+ const rowDetail = ( row as { detail ?: unknown } ) . detail ;
209+ return typeof rowDetail === "string" ? rowDetail : "" ;
210+ } )
211+ . join ( " " ) ;
212+
213+ expect ( detail ) . toContain ( "idx_agent_session_nodes_current_session_id" ) ;
214+ } ) ;
215+
141216 it ( "does not register a populated database during readonly health-style listing" , async ( ) => {
142217 const stateDir = makeTempDir ( tempDirs , "openclaw-session-readonly-registry-" ) ;
143218 const env = { OPENCLAW_STATE_DIR : stateDir } ;
0 commit comments