-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_startup_monitoring.ps1
More file actions
344 lines (283 loc) · 11.6 KB
/
session_startup_monitoring.ps1
File metadata and controls
344 lines (283 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# SESSION STARTUP MONITORING SYSTEM
# Launches all comprehensive monitoring systems for the session
# This runs at the start of every session to prevent psychotic loops
param(
[switch]$ForceRestart = $false,
[int]$StartupDelay = 5
)
Write-Host "🚀 SESSION STARTUP MONITORING SYSTEM" -ForegroundColor Green
Write-Host "💀 PREVENTING PSYCHOTIC LOOPS - DISCIPLINE ENFORCEMENT" -ForegroundColor Red
function Write-Startup {
param([string]$Message, [string]$Level = "INFO")
$timestamp = Get-Date -Format "HH:mm:ss"
$logEntry = "[$timestamp] [$Level] $Message"
switch ($Level) {
"CRITICAL" { Write-Host $logEntry -ForegroundColor Red }
"WARNING" { Write-Host $logEntry -ForegroundColor Yellow }
"SUCCESS" { Write-Host $logEntry -ForegroundColor Green }
default { Write-Host $logEntry -ForegroundColor White }
}
}
function Kill-ExistingMonitors {
Write-Startup "🔍 Looking for existing monitoring processes..." "CHECK"
# Find PowerShell processes running our scripts
$monitoringScripts = @(
"live_app_tracker.ps1",
"enhanced_error_analyzer.ps1",
"runtime_application_monitor.ps1",
"integrated_comprehensive_monitor.ps1",
"comprehensive_enforcer.ps1",
"comprehensive_ui_analyzer.ps1"
)
$killedCount = 0
Get-Process | Where-Object { $_.ProcessName -eq "powershell" } | ForEach-Object {
try {
$cmdLine = (Get-WmiObject Win32_Process -Filter "ProcessId = $($_.Id)").CommandLine
foreach ($script in $monitoringScripts) {
if ($cmdLine -like "*$script*") {
Write-Startup "🛑 Killing existing monitor: $script (PID: $($_.Id))" "STOP"
Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue
$killedCount++
break
}
}
} catch {
# Process might have already exited
}
}
Write-Startup "💀 Killed $killedCount existing monitoring processes" "STOP_COMPLETE"
}
function Check-SystemPrerequisites {
Write-Startup "🔍 Checking system prerequisites..." "CHECK"
# Check if Rust is installed
try {
$rustVersion = rustc --version 2>$null
if ($rustVersion) {
Write-Startup "✅ Rust installed: $rustVersion" "SUCCESS"
} else {
Write-Startup "❌ Rust not found" "CRITICAL"
return $false
}
} catch {
Write-Startup "❌ Rust check failed: $_" "CRITICAL"
return $false
}
# Check if Cargo is installed
try {
$cargoVersion = cargo --version 2>$null
if ($cargoVersion) {
Write-Startup "✅ Cargo installed: $cargoVersion" "SUCCESS"
} else {
Write-Startup "❌ Cargo not found" "CRITICAL"
return $false
}
} catch {
Write-Startup "❌ Cargo check failed: $_" "CRITICAL"
return $false
}
# Check disk space
$disk = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='C:'"
$freeSpaceGB = [Math]::Round($disk.FreeSpace / 1GB, 2)
if ($freeSpaceGB -lt 2) {
Write-Startup "❌ Insufficient disk space: ${freeSpaceGB}GB free (need 2GB+)" "CRITICAL"
return $false
}
Write-Startup "✅ Disk space: ${freeSpaceGB}GB free" "SUCCESS"
# Check memory
$memory = Get-WmiObject -Class Win32_OperatingSystem
$freeMemoryGB = [Math]::Round($memory.FreePhysicalMemory / 1MB / 1024, 2)
if ($freeMemoryGB -lt 4) {
Write-Startup "⚠️ Low memory: ${freeMemoryGB}GB free" "WARNING"
} else {
Write-Startup "✅ Memory: ${freeMemoryGB}GB free" "SUCCESS"
}
return $true
}
function Validate-ProjectStructure {
Write-Startup "📁 Validating project structure..." "CHECK"
$requiredFiles = @(
"Cargo.toml",
"src\main.rs",
"src\editor_ui.rs"
)
$requiredDirs = @(
"src",
".trae\documents"
)
$missingFiles = @()
$missingDirs = @()
foreach ($file in $requiredFiles) {
if (-not (Test-Path $file)) {
$missingFiles += $file
}
}
foreach ($dir in $requiredDirs) {
if (-not (Test-Path $dir)) {
$missingDirs += $dir
}
}
if ($missingFiles.Count -gt 0 -or $missingDirs.Count -gt 0) {
Write-Startup "❌ Missing files: $($missingFiles -join ', ')" "CRITICAL"
Write-Startup "❌ Missing directories: $($missingDirs -join ', ')" "CRITICAL"
return $false
}
Write-Startup "✅ Project structure validated" "SUCCESS"
return $true
}
function Start-ComprehensiveMonitoring {
Write-Startup "🚀 Starting comprehensive monitoring systems..." "LAUNCH"
# Start with a clean slate
Kill-ExistingMonitors
# Wait a moment for processes to clean up
Start-Sleep -Seconds 2
# Start the integrated comprehensive monitor (this starts all sub-monitors)
Write-Startup "🎯 Starting integrated comprehensive monitor..." "LAUNCH_SYSTEM"
try {
$integratedMonitor = Start-Process -FilePath "powershell.exe" -ArgumentList "-ExecutionPolicy Bypass -File integrated_comprehensive_monitor.ps1" -PassThru -NoNewWindow
Write-Startup "✅ Integrated monitor started with PID: $($integratedMonitor.Id)" "SUCCESS"
# Give it time to start up
Start-Sleep -Seconds 5
# Verify it's running
if (-not $integratedMonitor.HasExited) {
Write-Startup "🟢 Integrated monitor is running" "RUNNING"
# Monitor the monitor (meta-monitoring)
Write-Startup "🔍 Starting meta-monitoring of the integrated system..." "META"
$metaCheckCount = 0
while ($metaCheckCount -lt 10 -and -not $integratedMonitor.HasExited) {
Start-Sleep -Seconds 10
$metaCheckCount++
# Check if integrated monitor is still healthy
try {
$memoryMB = [Math]::Round($integratedMonitor.WorkingSet64 / 1MB, 2)
Write-Startup "📊 Integrated monitor health check $metaCheckCount - Memory: ${memoryMB}MB" "META_CHECK"
if ($memoryMB -gt 1000) { # 1GB threshold
Write-Startup "⚠️ High memory usage in integrated monitor: ${memoryMB}MB" "WARNING"
}
} catch {
Write-Startup "❌ Failed to check integrated monitor health" "ERROR"
break
}
}
if ($integratedMonitor.HasExited) {
$exitCode = $integratedMonitor.ExitCode
Write-Startup "💥 Integrated monitor exited with code: $exitCode" "CRASH"
if ($exitCode -ne 0) {
Write-Startup "🚨 CRITICAL: Integrated monitor failed - check logs" "CRITICAL"
return $false
}
} else {
Write-Startup "✅ Integrated monitor completed startup phase successfully" "SUCCESS"
}
} else {
$exitCode = $integratedMonitor.ExitCode
Write-Startup "💥 Integrated monitor failed immediately with code: $exitCode" "CRITICAL"
return $false
}
} catch {
Write-Startup "❌ Failed to start integrated monitor: $_" "CRITICAL"
return $false
}
return $true
}
function Show-SystemStatus {
Write-Startup "📊 Current system status:" "STATUS"
# Show running monitors
$monitoringScripts = @(
"live_app_tracker.ps1",
"enhanced_error_analyzer.ps1",
"runtime_application_monitor.ps1",
"integrated_comprehensive_monitor.ps1",
"comprehensive_enforcer.ps1",
"comprehensive_ui_analyzer.ps1"
)
$runningMonitors = @()
Get-Process | Where-Object { $_.ProcessName -eq "powershell" } | ForEach-Object {
try {
$cmdLine = (Get-WmiObject Win32_Process -Filter "ProcessId = $($_.Id)").CommandLine
foreach ($script in $monitoringScripts) {
if ($cmdLine -like "*$script*") {
$runningMonitors += @{
Script = $script
PID = $_.Id
Memory = [Math]::Round($_.WorkingSet64 / 1MB, 2)
}
break
}
}
} catch {
# Ignore
}
}
if ($runningMonitors.Count -gt 0) {
Write-Startup "🟢 Running monitors: $($runningMonitors.Count)" "STATUS_GOOD"
foreach ($monitor in $runningMonitors) {
Write-Startup " 📊 $($monitor.Script) - PID: $($monitor.PID) - Memory: $($monitor.Memory)MB" "STATUS_DETAIL"
}
} else {
Write-Startup "🔴 No monitors running" "STATUS_BAD"
}
# Show log files
$logFiles = @(
"live_app_tracking.log",
"live_errors.log",
"runtime_monitoring.log",
"app_crashes.log",
"comprehensive_master.log",
"critical_alerts.log"
)
Write-Startup "📁 Log files status:" "LOGS"
foreach ($logFile in $logFiles) {
if (Test-Path $logFile) {
$size = [Math]::Round((Get-Item $logFile).Length / 1KB, 2)
$recent = if ((Get-Item $logFile).LastWriteTime -gt (Get-Date).AddMinutes(-5)) { "🟢" } else { "⚠️" }
Write-Startup " $recent $logFile - ${size}KB" "LOG_DETAIL"
} else {
Write-Startup " ❌ $logFile - NOT FOUND" "LOG_MISSING"
}
}
}
# MAIN STARTUP SEQUENCE
try {
Write-Startup "🚀 SESSION STARTUP MONITORING SYSTEM INITIATED" "SYSTEM_START"
Write-Startup "⏰ Startup delay: $StartupDelay seconds" "CONFIG"
# Initial delay
Start-Sleep -Seconds $StartupDelay
# Kill existing monitors if forced restart
if ($ForceRestart) {
Write-Startup "🔥 FORCE RESTART REQUESTED - Killing all existing monitors" "FORCE"
Kill-ExistingMonitors
Start-Sleep -Seconds 3
}
# Check system prerequisites
Write-Startup "🔍 Phase 1: System prerequisites check" "PHASE_START"
if (-not (Check-SystemPrerequisites)) {
Write-Startup "💥 CRITICAL: System prerequisites check failed" "CRITICAL"
exit 1
}
# Validate project structure
Write-Startup "🔍 Phase 2: Project structure validation" "PHASE_START"
if (-not (Validate-ProjectStructure)) {
Write-Startup "💥 CRITICAL: Project structure validation failed" "CRITICAL"
exit 1
}
# Start comprehensive monitoring
Write-Startup "🚀 Phase 3: Starting comprehensive monitoring" "PHASE_START"
if (-not (Start-ComprehensiveMonitoring)) {
Write-Startup "💥 CRITICAL: Failed to start comprehensive monitoring" "CRITICAL"
exit 1
}
# Show final status
Write-Startup "✅ SESSION STARTUP COMPLETE" "SUCCESS"
Show-SystemStatus
Write-Startup "🟢 ALL SYSTEMS OPERATIONAL - DISCIPLINE ENFORCEMENT ACTIVE" "OPERATIONAL"
Write-Startup "💀 MONITORING WILL CONTINUE UNTIL SESSION END" "SESSION_ACTIVE"
# Keep the script running to show status
Write-Startup "⏰ Press Ctrl+C to stop monitoring (NOT RECOMMENDED)" "WARNING"
while ($true) {
Start-Sleep -Seconds 30
Show-SystemStatus
}
} catch {
Write-Startup "💥 SESSION STARTUP FAILED: $_" "CRITICAL"
exit 1
}