@@ -616,32 +616,77 @@ export class Platform {
616616 /**
617617 * Calculate precise Linux process start time in milliseconds since Unix epoch
618618 * using /proc/<pid>/stat starttime ticks, /proc/stat btime, and system clock ticks (100 Hz).
619- * Unlike file inode ctime, this represents the actual kernel process creation timestamp .
619+ * Fallback to /proc/<pid> directory inode ctime if /proc/stat btime is unavailable .
620620 * Handles process comm names with spaces and parentheses by indexing from the last ')'.
621621 */
622622 public static getLinuxProcessStartTimeMs ( pid : number ) : number | null {
623623 try {
624- const statContent = fs . readFileSync ( `/proc/${ pid } /stat` , 'utf-8' ) ;
625- const closeParenIdx = statContent . lastIndexOf ( ')' ) ;
626- if ( closeParenIdx === - 1 ) return null ;
627- const rest = statContent . slice ( closeParenIdx + 1 ) . trim ( ) . split ( / \s + / ) ;
628- // field 22 is index 19 (0-based) after closing parenthesis
629- const starttimeTicks = Number ( rest [ 19 ] ) ;
630- if ( ! Number . isFinite ( starttimeTicks ) ) return null ;
631-
632- const statFile = fs . readFileSync ( '/proc/stat' , 'utf-8' ) ;
633- const btimeLine = statFile . split ( '\n' ) . find ( ( l ) => l . startsWith ( 'btime ' ) ) ;
634- if ( ! btimeLine ) return null ;
635- const btimeSeconds = Number ( btimeLine . split ( ' ' ) [ 1 ] ) ;
636- if ( ! Number . isFinite ( btimeSeconds ) ) return null ;
637-
638- // Standard Linux clock ticks frequency is 100 Hz
639- return btimeSeconds * 1000 + Math . round ( ( starttimeTicks / 100 ) * 1000 ) ;
624+ if ( fs . existsSync ( `/proc/${ pid } /stat` ) ) {
625+ const statContent = fs . readFileSync ( `/proc/${ pid } /stat` , 'utf-8' ) ;
626+ const closeParenIdx = statContent . lastIndexOf ( ')' ) ;
627+ if ( closeParenIdx !== - 1 ) {
628+ const rest = statContent . slice ( closeParenIdx + 1 ) . trim ( ) . split ( / \s + / ) ;
629+ // field 22 is index 19 (0-based) after closing parenthesis
630+ const starttimeTicks = Number ( rest [ 19 ] ) ;
631+ if ( Number . isFinite ( starttimeTicks ) ) {
632+ try {
633+ const statFile = fs . readFileSync ( '/proc/stat' , 'utf-8' ) ;
634+ const btimeLine = statFile . split ( '\n' ) . find ( ( l ) => l . startsWith ( 'btime ' ) ) ;
635+ if ( btimeLine ) {
636+ const btimeSeconds = Number ( btimeLine . split ( ' ' ) [ 1 ] ) ;
637+ if ( Number . isFinite ( btimeSeconds ) ) {
638+ // Standard Linux clock ticks frequency is 100 Hz
639+ return btimeSeconds * 1000 + Math . round ( ( starttimeTicks / 100 ) * 1000 ) ;
640+ }
641+ }
642+ } catch { }
643+ }
644+ }
645+ }
646+
647+ // Robust fallback: Kernel sets /proc/<pid> directory ctime to process creation time
648+ if ( fs . existsSync ( `/proc/${ pid } ` ) ) {
649+ const stat = fs . statSync ( `/proc/${ pid } ` ) ;
650+ return Math . round ( stat . ctimeMs || stat . mtimeMs ) ;
651+ }
652+ return null ;
640653 } catch {
654+ try {
655+ if ( fs . existsSync ( `/proc/${ pid } ` ) ) {
656+ return Math . round ( fs . statSync ( `/proc/${ pid } ` ) . ctimeMs ) ;
657+ }
658+ } catch { }
641659 return null ;
642660 }
643661 }
644662
663+ /**
664+ * Cross-platform helper to retrieve process start time in milliseconds since Unix epoch.
665+ * On Windows, uses Win32_Process CreationDate.
666+ * On Linux, uses /proc/<pid>/stat kernel starttime or /proc/<pid> ctime.
667+ */
668+ public static getProcessStartTimeMs ( pid : number ) : number | null {
669+ if ( this . isWindows ( ) ) {
670+ try {
671+ const psCmd = `Get-CimInstance Win32_Process -Filter "ProcessId = ${ pid } " | Select-Object @{N='Epoch';E={[DateTimeOffset]::new($_.CreationDate).ToUnixTimeMilliseconds()}} | ConvertTo-Json -Compress` ;
672+ const out = execSync ( `powershell -NoProfile -NonInteractive -Command "${ psCmd } "` , {
673+ encoding : 'utf-8' ,
674+ timeout : 4000 ,
675+ windowsHide : true ,
676+ stdio : [ 'ignore' , 'pipe' , 'ignore' ]
677+ } ) . trim ( ) ;
678+ if ( out ) {
679+ const parsed = JSON . parse ( out ) ;
680+ const epoch = typeof parsed . Epoch === 'number' ? parsed . Epoch : Number ( parsed . Epoch ) ;
681+ if ( Number . isFinite ( epoch ) ) return epoch ;
682+ }
683+ } catch { }
684+ return null ;
685+ } else {
686+ return this . getLinuxProcessStartTimeMs ( pid ) ;
687+ }
688+ }
689+
645690 /**
646691 * Structurally verify process ownership using full executable path,
647692 * process creation timestamp, and normalized config path.
@@ -694,10 +739,10 @@ export class Platform {
694739 return false ;
695740 }
696741
697- // C. Compare process creation time against persisted timestamp (bounded tolerance: ±15000ms )
742+ // C. Compare process creation time against persisted timestamp (bounded tolerance: ±30000ms )
698743 if ( info . CreationEpoch && typeof info . CreationEpoch === 'number' ) {
699744 const timeDiff = Math . abs ( info . CreationEpoch - record . startedAt ) ;
700- if ( timeDiff > 15000 ) {
745+ if ( timeDiff > 30000 ) {
701746 return false ;
702747 }
703748 } else {
@@ -708,7 +753,26 @@ export class Platform {
708753 const cmdLine = ( info . CommandLine || '' ) . toLowerCase ( ) ;
709754 if ( record . configPath ) {
710755 const normalizedConfig = path . normalize ( path . resolve ( record . configPath ) ) . toLowerCase ( ) ;
711- if ( ! cmdLine . includes ( normalizedConfig ) ) {
756+ const normalizedConfigForward = normalizedConfig . replace ( / \\ / g, '/' ) ;
757+ const normalizedConfigBack = normalizedConfig . replace ( / \/ / g, '\\' ) ;
758+ const relConfig = path . normalize ( record . configPath ) . toLowerCase ( ) ;
759+ const relConfigForward = relConfig . replace ( / \\ / g, '/' ) ;
760+ const relConfigBack = relConfig . replace ( / \/ / g, '\\' ) ;
761+ const relToRoot = path . relative ( this . getProjectRoot ( ) , record . configPath ) . toLowerCase ( ) ;
762+ const relToRootForward = relToRoot . replace ( / \\ / g, '/' ) ;
763+ const relToRootBack = relToRoot . replace ( / \/ / g, '\\' ) ;
764+
765+ const cmdLineForward = cmdLine . replace ( / \\ / g, '/' ) ;
766+
767+ const configMatched =
768+ cmdLineForward . includes ( normalizedConfigForward ) ||
769+ cmdLine . includes ( normalizedConfigBack ) ||
770+ cmdLineForward . includes ( relConfigForward ) ||
771+ cmdLine . includes ( relConfigBack ) ||
772+ cmdLineForward . includes ( relToRootForward ) ||
773+ cmdLine . includes ( relToRootBack ) ;
774+
775+ if ( ! configMatched ) {
712776 return false ;
713777 }
714778 }
@@ -746,7 +810,7 @@ export class Platform {
746810 const kernelStartMs = this . getLinuxProcessStartTimeMs ( record . pid ) ;
747811 if ( ! kernelStartMs ) return false ;
748812 const timeDiff = Math . abs ( kernelStartMs - record . startedAt ) ;
749- if ( timeDiff > 15000 ) {
813+ if ( timeDiff > 30000 ) {
750814 return false ;
751815 }
752816
@@ -763,21 +827,35 @@ export class Platform {
763827
764828 if ( record . configPath ) {
765829 const normalizedConfig = path . resolve ( record . configPath ) ;
830+ let realConfig = normalizedConfig ;
831+ try {
832+ if ( fs . existsSync ( normalizedConfig ) ) realConfig = fs . realpathSync ( normalizedConfig ) ;
833+ } catch { }
834+
766835 const configArgFound = cmdlineArgs . some ( ( arg ) => {
767836 try {
837+ const cleanedArg = arg . replace ( / ^ - - ? [ a - z A - Z 0 - 9 _ - ] + = / , '' ) ;
838+ const resolvedArg = path . resolve ( cleanedArg ) ;
839+ let realArg = resolvedArg ;
840+ try {
841+ if ( fs . existsSync ( resolvedArg ) ) realArg = fs . realpathSync ( resolvedArg ) ;
842+ } catch { }
768843 return (
769- path . resolve ( arg ) === normalizedConfig ||
844+ resolvedArg === normalizedConfig ||
845+ realArg === realConfig ||
846+ arg === record . configPath ||
770847 arg . includes ( normalizedConfig ) ||
771- normalizedConfig . includes ( arg ) ||
772- arg . includes ( path . basename ( normalizedConfig ) )
848+ arg . includes ( realConfig )
773849 ) ;
774850 } catch {
775851 return false ;
776852 }
777853 } ) ;
854+
778855 if (
779856 ! configArgFound &&
780857 ! cmdlineJoined . includes ( normalizedConfig ) &&
858+ ! cmdlineJoined . includes ( realConfig ) &&
781859 ! cmdlineJoined . includes ( record . configPath )
782860 ) {
783861 return false ;
0 commit comments