-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvision_watchdog.vbs
More file actions
169 lines (152 loc) · 5.54 KB
/
Copy pathvision_watchdog.vbs
File metadata and controls
169 lines (152 loc) · 5.54 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
' vision_watchdog.vbs — Always-on background process manager for vision-tool.
' Copyright (C) 2026 Farhan Dhrubo
'
' Licensed under GPLv3 — see LICENSE.
'
' Monitors for ANY AI coding assistant process via WMI every 10 seconds.
' When ANY supported tool runs -> launches vision MCP server (hidden, no window).
' When ALL tools exit -> kills child process, cleans up PID file.
'
' Supported tools (13 monitored processes):
' opencode.exe — OpenCode CLI/Desktop
' claude.exe — Claude Code / Claude Desktop
' cursor.exe — Cursor AI editor
' windsurf.exe — Windsurf AI editor
' aider.exe — Aider AI pair programming
' continue.exe — Continue.dev
' code.exe — VS Code / VS Studio Code
' vscode.exe — VSCode variant (some forks)
' codium.exe — VSCodium (open-source VS Code fork)
' studio.exe — Various Studio AI editors
' antigravity.exe — Antigravity 1.x / 2.x (Google AI IDE)
' claudecode.exe — Claude Code CLI variants
' ghcopilot.exe — GitHub Copilot CLI
'
' Auto-start with Windows (Task Scheduler):
' schtasks /create /tn "vision-tool-watchdog" /tr "wscript.exe //nologo \"%PATH%\vision_watchdog.vbs\"" /sc onstart /delay 0000:30 /ru %USERNAME% /f
'
' Usage:
' wscript.exe //nologo vision_watchdog.vbs
' wscript.exe //nologo vision_watchdog.vbs "python C:\path\to\vision_mcp_server.py --http 3789"
' wscript.exe //nologo vision_watchdog.vbs "my_command" "my_pid_file.pid"
'
' For zero-flash (no wscript icon): compile vision_watchdog.cs into vision_watchdog.exe
' csc.exe /target:winexe vision_watchdog.cs
Dim args, childCmd, pidFileName, shell, fso, wmi, pidFilePath
Set shell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
' WMI connection with retry — handles boot race condition
Dim wmiConnected, retryCount, maxRetries
wmiConnected = False
maxRetries = 5
retryCount = 0
Do While Not wmiConnected And retryCount < maxRetries
On Error Resume Next
Set wmi = GetObject("winmgmts:\\.\root\cimv2")
If Err.Number = 0 Then
wmiConnected = True
On Error GoTo 0
Else
Err.Clear
On Error GoTo 0
retryCount = retryCount + 1
If retryCount < maxRetries Then
WScript.Sleep 5000
End If
End If
Loop
If Not wmiConnected Then
WScript.Echo "Error: Cannot connect to WMI (root\cimv2). Exiting."
WScript.Quit 1
End If
' -- AI tool processes to watch -----------------------------------------
' Add ANY process name here. WMI queries are case-insensitive.
Dim AI_TOOLS
AI_TOOLS = Array( _
"opencode.exe", _
"claude.exe", _
"cursor.exe", _
"windsurf.exe", _
"aider.exe", _
"continue.exe", _
"code.exe", _
"vscode.exe", _
"codium.exe", _
"studio.exe", _
"antigravity.exe", _
"claudecode.exe", _
"ghcopilot.exe" _
)
' -- Parse arguments ----------------------------------------------------
Set args = WScript.Arguments
' Default: run vision_mcp_server.py (assumes next to this script)
scriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
defaultCmd = "python """ & scriptDir & "\vision_mcp_server.py"" --http 3789"
childCmd = defaultCmd
pidFileName = "vision_watchdog.pid"
If args.Count > 0 Then childCmd = args(0)
If args.Count > 1 Then pidFileName = args(1)
pidFilePath = shell.ExpandEnvironmentStrings("%TEMP%") & "\" & pidFileName
' -- Helper: check if any AI tool process is running --------------------
Function IsAnyAiToolRunning()
Dim proc, anyRunning
anyRunning = False
On Error Resume Next
For Each tool In AI_TOOLS
Set proc = wmi.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & tool & "'")
If Err.Number = 0 And proc.Count > 0 Then
anyRunning = True
Exit For
End If
Err.Clear
Next
On Error GoTo 0
IsAnyAiToolRunning = anyRunning
End Function
' -- Main loop ----------------------------------------------------------
Do While True
Dim anyRunning
anyRunning = IsAnyAiToolRunning()
If anyRunning Then
' Start child if not already running
If Not fso.FileExists(pidFilePath) Then
Dim pidFileOut
' Launch hidden (window style 0 = invisible)
Dim procId
procId = shell.Run(childCmd, 0, False)
' Write PID file so we can kill later
Set pidFileOut = fso.CreateTextFile(pidFilePath, True)
pidFileOut.WriteLine(procId)
pidFileOut.Close
End If
Else
' Kill child if running
If fso.FileExists(pidFilePath) Then
Dim pidFileIn, pid
Set pidFileIn = fso.OpenTextFile(pidFilePath, 1)
pid = Trim(pidFileIn.ReadLine())
pidFileIn.Close
On Error Resume Next
Dim proc
Set proc = wmi.Get("Win32_Process.Handle='" & pid & "'")
If Err.Number <> 0 Then
Err.Clear
Dim procs
Set procs = wmi.ExecQuery("SELECT * FROM Win32_Process WHERE Name='python.exe' AND CommandLine LIKE '%vision_mcp_server%'")
If Err.Number = 0 Then
For Each p In procs
p.Terminate()
Next
End If
Err.Clear
Else
proc.Terminate()
End If
On Error GoTo 0
On Error Resume Next
fso.DeleteFile pidFilePath, True
On Error GoTo 0
End If
End If
WScript.Sleep 10000
Loop