1+ import { randomUUID } from "node:crypto" ;
12import { chmodSync , closeSync , existsSync , fsyncSync , linkSync , openSync , readFileSync , renameSync , rmSync , writeFileSync } from "node:fs" ;
23import { homedir } from "node:os" ;
34import { isAbsolute , join } from "node:path" ;
45import { Database } from "bun:sqlite" ;
56
67const DEFAULT_EXPIRES_MS = 3600_000 ;
78const KIRO_CLI_RECOVERY_SUFFIX = ".opencodex-recovery" ;
8- const KIRO_CLI_RECOVERY_HEADER = Buffer . from ( "opencodex-kiro-session-v1\n" , "utf8" ) ;
9+ const KIRO_CLI_RECOVERY_HEADER_V1 = Buffer . from ( "opencodex-kiro-session-v1\n" , "utf8" ) ;
10+ const KIRO_CLI_RECOVERY_HEADER = Buffer . from ( "opencodex-kiro-session-v2\n" , "utf8" ) ;
11+ const KIRO_CLI_RECOVERY_PROCESS_INSTANCE = randomUUID ( ) ;
12+ const KIRO_CLI_RECOVERY_PROCESS_INSTANCE_PATTERN = / ^ [ A - Z a - z 0 - 9 . _ - ] { 1 , 128 } $ / ;
913const SQLITE_DATABASE_HEADER = Buffer . from ( "SQLite format 3\0" , "binary" ) ;
1014const KIRO_REGION_PATTERN = / ^ [ a - z ] { 2 } (?: - [ a - z ] + ) + - \d $ / ;
1115const CLIENT_ID_HASH_PATTERN = / ^ [ A - Z a - z 0 - 9 _ - ] { 1 , 128 } $ / ;
@@ -378,6 +382,7 @@ export function persistKiroCliSessionRecovery(snapshot: KiroCliSessionSnapshot):
378382 writeFileSync ( staged , Buffer . concat ( [
379383 KIRO_CLI_RECOVERY_HEADER ,
380384 Buffer . from ( `${ process . pid } \n` , "utf8" ) ,
385+ Buffer . from ( `${ KIRO_CLI_RECOVERY_PROCESS_INSTANCE } \n` , "utf8" ) ,
381386 snapshot . database ,
382387 ] ) , { flag : "wx" , mode : 0o600 } ) ;
383388 try { chmodSync ( staged , 0o600 ) ; } catch { /* platform may ignore chmod */ }
@@ -396,8 +401,43 @@ export function discardKiroCliSessionRecovery(snapshot: KiroCliSessionSnapshot):
396401 rmSync ( snapshot . recoveryPath , { force : true } ) ;
397402}
398403
399- function isKiroRecoveryOwnerAlive ( ownerPid : number ) : boolean {
400- if ( ownerPid === process . pid ) return true ;
404+ interface ParsedKiroCliSessionRecovery {
405+ ownerPid : number ;
406+ ownerProcessInstance ?: string ;
407+ database : Buffer ;
408+ }
409+
410+ function parseKiroCliSessionRecovery ( payload : Buffer ) : ParsedKiroCliSessionRecovery | null {
411+ const isV2 = payload . subarray ( 0 , KIRO_CLI_RECOVERY_HEADER . length ) . equals ( KIRO_CLI_RECOVERY_HEADER ) ;
412+ const isV1 = payload . subarray ( 0 , KIRO_CLI_RECOVERY_HEADER_V1 . length ) . equals ( KIRO_CLI_RECOVERY_HEADER_V1 ) ;
413+ if ( ! isV2 && ! isV1 ) return null ;
414+ const headerLength = isV2 ? KIRO_CLI_RECOVERY_HEADER . length : KIRO_CLI_RECOVERY_HEADER_V1 . length ;
415+ const ownerEnd = payload . indexOf ( 0x0a , headerLength ) ;
416+ if ( ownerEnd <= headerLength ) return null ;
417+ const ownerPid = Number ( payload . subarray ( headerLength , ownerEnd ) . toString ( "utf8" ) ) ;
418+ let databaseStart = ownerEnd + 1 ;
419+ let ownerProcessInstance : string | undefined ;
420+ if ( isV2 ) {
421+ const instanceEnd = payload . indexOf ( 0x0a , databaseStart ) ;
422+ if ( instanceEnd <= databaseStart ) return null ;
423+ ownerProcessInstance = payload . subarray ( databaseStart , instanceEnd ) . toString ( "utf8" ) ;
424+ if ( ! KIRO_CLI_RECOVERY_PROCESS_INSTANCE_PATTERN . test ( ownerProcessInstance ) ) return null ;
425+ databaseStart = instanceEnd + 1 ;
426+ }
427+ const database = payload . subarray ( databaseStart ) ;
428+ if (
429+ ! Number . isSafeInteger ( ownerPid ) || ownerPid <= 0 ||
430+ ! database . subarray ( 0 , SQLITE_DATABASE_HEADER . length ) . equals ( SQLITE_DATABASE_HEADER )
431+ ) {
432+ return null ;
433+ }
434+ return { ownerPid, ...( ownerProcessInstance ? { ownerProcessInstance } : { } ) , database } ;
435+ }
436+
437+ function isKiroRecoveryOwnerAlive ( ownerPid : number , ownerProcessInstance ?: string ) : boolean {
438+ // A restarted supervisor may reuse the exact same PID (commonly PID 1). Only this process
439+ // instance's nonce proves that a same-PID recovery transaction is still live.
440+ if ( ownerPid === process . pid ) return ownerProcessInstance === KIRO_CLI_RECOVERY_PROCESS_INSTANCE ;
401441 try {
402442 process . kill ( ownerPid , 0 ) ;
403443 return true ;
@@ -450,28 +490,20 @@ export function restoreStaleKiroCliSessionRecovery(): boolean {
450490 const recoveryPath = `${ path } ${ KIRO_CLI_RECOVERY_SUFFIX } ` ;
451491 if ( ! existsSync ( recoveryPath ) ) continue ;
452492 const payload = readFileSync ( recoveryPath ) ;
453- const ownerEnd = payload . indexOf ( 0x0a , KIRO_CLI_RECOVERY_HEADER . length ) ;
454- const ownerPid = ownerEnd > KIRO_CLI_RECOVERY_HEADER . length
455- ? Number ( payload . subarray ( KIRO_CLI_RECOVERY_HEADER . length , ownerEnd ) . toString ( "utf8" ) )
456- : Number . NaN ;
457- const database = ownerEnd >= 0 ? payload . subarray ( ownerEnd + 1 ) : Buffer . alloc ( 0 ) ;
458- if (
459- ! payload . subarray ( 0 , KIRO_CLI_RECOVERY_HEADER . length ) . equals ( KIRO_CLI_RECOVERY_HEADER ) ||
460- ! Number . isSafeInteger ( ownerPid ) || ownerPid <= 0 ||
461- ! database . subarray ( 0 , SQLITE_DATABASE_HEADER . length ) . equals ( SQLITE_DATABASE_HEADER )
462- ) {
493+ const recovery = parseKiroCliSessionRecovery ( payload ) ;
494+ if ( ! recovery ) {
463495 throw new Error (
464496 `Kiro CLI session recovery data is invalid: ${ recoveryPath } . Remove this file to continue.` ,
465497 ) ;
466498 }
467- if ( isKiroRecoveryOwnerAlive ( ownerPid ) ) {
499+ if ( isKiroRecoveryOwnerAlive ( recovery . ownerPid , recovery . ownerProcessInstance ) ) {
468500 throw new Error (
469- `Another Kiro CLI login transaction is still in progress (pid ${ ownerPid } , ${ recoveryPath } ).` ,
501+ `Another Kiro CLI login transaction is still in progress (pid ${ recovery . ownerPid } , ${ recoveryPath } ).` ,
470502 ) ;
471503 }
472504 const snapshot : KiroCliSessionSnapshot = {
473505 path,
474- database,
506+ database : recovery . database ,
475507 recoveryPath,
476508 } ;
477509 restoreKiroCliSession ( snapshot ) ;
0 commit comments