diff --git a/.gitignore b/.gitignore index 38c1c9d..6058201 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ node_modules/ bridge/config.json .vscode/ *.log +*.pid +scripts/supervisor.old.log memory/chat-log/ memory/state.json memory/state.json.tmp diff --git a/scripts/create-desktop-shortcut.ps1 b/scripts/create-desktop-shortcut.ps1 new file mode 100644 index 0000000..04c12b4 --- /dev/null +++ b/scripts/create-desktop-shortcut.ps1 @@ -0,0 +1,26 @@ +# Creates a desktop shortcut "DSH Memory" that starts the bridge hidden and idempotently. +# Run once: powershell -NoProfile -ExecutionPolicy Bypass -File V:\Site\dsh-memory\scripts\create-desktop-shortcut.ps1 +$ErrorActionPreference = 'Stop' +$scriptsDir = $PSScriptRoot +$root = Split-Path -Parent $scriptsDir +$vbs = Join-Path $scriptsDir 'launch-dsh-memory.vbs' +if (-not (Test-Path $vbs)) { throw "launch-dsh-memory.vbs not found: $vbs" } + +$desktop = [Environment]::GetFolderPath('Desktop') +$lnk = Join-Path $desktop 'DSH Memory.lnk' + +$node = Get-Command node -ErrorAction SilentlyContinue +$iconPath = if ($node) { $node.Source } else { "$env:SystemRoot\System32\shell32.dll" } + +$ws = New-Object -ComObject WScript.Shell +$s = $ws.CreateShortcut($lnk) +$s.TargetPath = 'wscript.exe' +$s.Arguments = '"' + $vbs + '"' +$s.WorkingDirectory = $root +$s.IconLocation = "$iconPath,0" +$s.Description = 'Start DSH Memory bridge (silent, no duplicates)' +$s.WindowStyle = 1 +$s.Save() + +Write-Host "Shortcut created: $lnk" +Write-Host "Double-click it to start the bridge. If the bridge is already running, nothing happens." diff --git a/scripts/launch-dsh-memory.vbs b/scripts/launch-dsh-memory.vbs new file mode 100644 index 0000000..afbc9a7 --- /dev/null +++ b/scripts/launch-dsh-memory.vbs @@ -0,0 +1,31 @@ +Option Explicit +' Hidden launcher for the DSH memory bridge. Double-click friendly: no params, no window. +' If the bridge is already listening on 8766 it exits silently. Otherwise it starts the +' supervisor hidden and redirects output to scripts\supervisor.log. +Dim fso, sh, scriptDir, root, logPath +Set fso = CreateObject("Scripting.FileSystemObject") +Set sh = CreateObject("WScript.Shell") +scriptDir = fso.GetParentFolderName(WScript.ScriptFullName) +root = fso.GetParentFolderName(scriptDir) +logPath = scriptDir & "\supervisor.log" + +Function PortOpen() + Dim code + code = sh.Run("powershell -NoProfile -ExecutionPolicy Bypass -Command ""if (Get-NetTCPConnection -LocalPort 8766 -State Listen -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }""", 0, True) + PortOpen = (code = 0) +End Function + +If PortOpen() Then + WScript.Quit 0 +End If + +sh.CurrentDirectory = root +sh.Run "cmd /c node """ & scriptDir & "\supervisor.js"" >> """ & logPath & """ 2>&1", 0, False + +WScript.Sleep 5000 + +If Not PortOpen() Then + MsgBox "DSH Memory bridge did not start within 5 seconds." & vbCrLf & vbCrLf & _ + "Log: " & logPath & vbCrLf & vbCrLf & _ + "Possible causes: node not in PATH, port 8766 blocked, config missing.", 48, "DSH Memory" +End If diff --git a/scripts/start-dsh-memory.cmd b/scripts/start-dsh-memory.cmd index d9afdd8..55a51ec 100644 --- a/scripts/start-dsh-memory.cmd +++ b/scripts/start-dsh-memory.cmd @@ -1,7 +1,9 @@ @echo off -rem dsh-memory - idempotent bridge starter +rem dsh-memory - idempotent bridge starter (foreground, for manual/debug use) +rem For a silent desktop shortcut use launch-dsh-memory.vbs instead. setlocal set "ROOT=%~dp0.." +set "LOG=%ROOT%\scripts\supervisor.log" if not exist "%ROOT%\bridge\config.json" ( echo [dsh-memory] bridge\config.json not found. Run scripts\pair.ps1 first. @@ -12,10 +14,17 @@ if not exist "%ROOT%\bridge\config.json" ( powershell -NoProfile -Command "if (Get-NetTCPConnection -LocalPort 8766 -State Listen -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }" if %errorlevel%==0 ( echo [dsh-memory] bridge already running on port 8766. -) else ( - echo [dsh-memory] starting bridge... - start "dsh-memory bridge" /D "%ROOT%" cmd /c "title dsh-memory bridge && node scripts\supervisor.js & pause" - ping -n 3 127.0.0.1 >nul - powershell -NoProfile -Command "if (Get-NetTCPConnection -LocalPort 8766 -State Listen -ErrorAction SilentlyContinue) { Write-Host '[dsh-memory] bridge started.' } else { Write-Host '[dsh-memory] bridge NOT started. Is node in PATH? Check the bridge window.' }" + exit /b 0 ) + +rem Rotate log if it grew past 5 MB +if exist "%LOG%" ( + for %%A in ("%LOG%") do if %%~zA GTR 5242880 ( + if exist "%ROOT%\scripts\supervisor.old.log" del "%ROOT%\scripts\supervisor.old.log" + ren "%LOG%" supervisor.old.log + ) +) + +echo [dsh-memory] starting bridge (log: %LOG%) +node "%ROOT%\scripts\supervisor.js" endlocal diff --git a/scripts/supervisor.js b/scripts/supervisor.js index 8642437..199ed1f 100644 --- a/scripts/supervisor.js +++ b/scripts/supervisor.js @@ -38,6 +38,31 @@ function portOpen(port) { const ts = () => new Date().toISOString(); let fails = 0; +// PID lock: prevents a second supervisor from starting when the first is alive. +const LOCK = path.join(__dirname, '.supervisor.pid'); +function pidAlive(pid) { + try { process.kill(pid, 0); return true; } + catch (e) { return e.code === 'EPERM'; } +} +function acquireLock() { + try { + const raw = fs.readFileSync(LOCK, 'utf8').trim(); + const old = parseInt(raw, 10); + if (old && old !== process.pid && pidAlive(old)) { + console.log(ts() + ' [supervisor] another instance running (pid ' + old + '), exiting'); + process.exit(0); + } + } catch {} + try { fs.writeFileSync(LOCK, String(process.pid), 'utf8'); } + catch (e) { console.warn(ts() + ' [supervisor] lock write failed: ' + (e && e.message)); } +} +function releaseLock() { + try { + const raw = fs.readFileSync(LOCK, 'utf8').trim(); + if (parseInt(raw, 10) === process.pid) fs.unlinkSync(LOCK); + } catch {} +} + async function runOnce() { const port = readPort(); const waitStart = Date.now(); @@ -93,6 +118,8 @@ async function runOnce() { setTimeout(runOnce, DELAY_MS); } +process.on('exit', releaseLock); process.on('SIGINT', () => { console.log(ts() + ' [supervisor] interrupted'); process.exit(0); }); process.on('SIGTERM', () => { console.log(ts() + ' [supervisor] terminated'); process.exit(0); }); +acquireLock(); runOnce().catch((e) => { console.error(ts() + ' [supervisor] fatal: ' + (e && e.message)); process.exit(1); });