Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review: PS NoProfile/CRLF handling, marker cleanup on restart failure, pipe guards

Adam Eivy901823548de4
diff --git a/server/services/updateExecutor.js b/server/services/updateExecutor.js
index 1360fd7dbc..6a7cdb59bc 100644
--- a/server/services/updateExecutor.js
+++ b/server/services/updateExecutor.js
@@ -23,7 +23,9 @@ const UPDATE_PS1 = join(PATHS.root, 'update.ps1');
export async function executeUpdate(tag, emit) {
const isWindows = process.platform === 'win32';
const cmd = isWindows ? 'powershell' : 'bash';
- const args = isWindows ? ['-ExecutionPolicy', 'Bypass', '-File', UPDATE_PS1] : [UPDATE_SH];
+ const args = isWindows
+ ? ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', UPDATE_PS1]
+ : [UPDATE_SH];
emit('starting', 'running', `Starting update (target: ${tag})...`);
@@ -43,7 +45,7 @@ export async function executeUpdate(tag, emit) {
buffer += data.toString();
let newlineIdx;
while ((newlineIdx = buffer.indexOf('\n')) !== -1) {
- const line = buffer.slice(0, newlineIdx);
+ const line = buffer.slice(0, newlineIdx).replace(/\r$/, '');
buffer = buffer.slice(newlineIdx + 1);
const match = line.match(/STEP:([^:]+):([^:]+):(.+)/);
if (match) {
diff --git a/server/services/updateExecutor.test.js b/server/services/updateExecutor.test.js
index a0ace4958b..73efd0a9e7 100644
--- a/server/services/updateExecutor.test.js
+++ b/server/services/updateExecutor.test.js
@@ -44,7 +44,7 @@ describe('executeUpdate', () => {
expect(spawn).toHaveBeenCalledWith(
'powershell',
- expect.arrayContaining(['-ExecutionPolicy', 'Bypass', '-File']),
+ expect.arrayContaining(['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File']),
expect.any(Object)
);
} finally {
@@ -99,6 +99,26 @@ describe('executeUpdate', () => {
);
});
+ it('handles CRLF line endings from Windows PowerShell', async () => {
+ const child = createMockChild();
+ spawn.mockReturnValue(child);
+
+ const emits = [];
+ const promise = executeUpdate('v1.0.0', (...args) => emits.push(args));
+
+ // Simulate CRLF output (Windows PowerShell)
+ child.stdout.emit('data', Buffer.from('STEP:git-pull:running:Pulling latest changes\r\n'));
+ child.stdout.emit('data', Buffer.from('STEP:git-pull:done:Latest changes pulled\r\n'));
+
+ child.emit('close', 0);
+ await promise;
+
+ // Messages should not contain trailing \r
+ const pullRunning = emits.find(e => e[0] === 'git-pull' && e[1] === 'running');
+ expect(pullRunning[2]).toBe('Pulling latest changes');
+ expect(pullRunning[2]).not.toMatch(/\r/);
+ });
+
it('handles spawn error', async () => {
const child = createMockChild();
spawn.mockReturnValue(child);
diff --git a/update.ps1 b/update.ps1
index c5b24e5247..31fbfa7ae6 100644
--- a/update.ps1
+++ b/update.ps1
@@ -10,10 +10,26 @@ Write-Host " PortOS Update" -ForegroundColor Cyan
Write-Host "===================================" -ForegroundColor Cyan
Write-Host ""
+# Safe write helper — suppresses broken-pipe IOExceptions when the parent
+# Node process dies mid-update (mirrors the bash SIGPIPE trap)
+function Write-SafeHost {
+ param([string]$Text)
+ try {
+ Write-Host $Text
+ } catch {
+ if ($_.Exception -is [System.IO.IOException] -or
+ $_.Exception.InnerException -is [System.IO.IOException] -or
+ $_.Exception.ToString() -like "*The pipe has been ended*") {
+ return
+ }
+ throw
+ }
+}
+
# Step output helper (parsed by updateExecutor for UI progress)
function Step {
param([string]$Name, [string]$Status, [string]$Message)
- Write-Host "STEP:${Name}:${Status}:${Message}"
+ Write-SafeHost "STEP:${Name}:${Status}:${Message}"
}
# Resilient npm install — retries once after cleaning node_modules on failure
@@ -122,10 +138,15 @@ $utf8NoBom = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllText("$RootDir\data\update-complete.json.tmp", $marker, $utf8NoBom)
Move-Item -Force "$RootDir\data\update-complete.json.tmp" "$RootDir\data\update-complete.json"
-# Restart PM2 apps
+# Restart PM2 apps — remove marker if restart fails so it isn't misread on boot
Step "restart" "running" "Restarting PortOS..."
npm run pm2:restart
-if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
+if ($LASTEXITCODE -ne 0) {
+ if (Test-Path "$RootDir\data\update-complete.json") {
+ Remove-Item -Force "$RootDir\data\update-complete.json"
+ }
+ exit $LASTEXITCODE
+}
Step "restart" "done" "PortOS restarted"
Write-Host ""
diff --git a/update.sh b/update.sh
index 56c93dec0c..e05d3881c9 100755
--- a/update.sh
+++ b/update.sh
@@ -111,14 +111,23 @@ npm run build
step "build" "done" "Client built"
echo ""
+# Determine post-update version from package.json (fail if unreadable)
+TAG=$(node -e 'const pkg = JSON.parse(require("fs").readFileSync("package.json","utf8")); if (typeof pkg.version !== "string") process.exit(1); process.stdout.write(pkg.version.trim());')
+if [ -z "$TAG" ]; then
+ echo "❌ Failed to determine package version from package.json"
+ exit 1
+fi
+
# Write completion marker atomically before restart so server reads it on boot
-TAG=$(sed -n 's/.*"version": *"\([^"]*\)".*/\1/p' package.json | head -1)
echo "{\"version\":\"${TAG}\",\"completedAt\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\"}" > "$ROOT_DIR/data/update-complete.json.tmp"
mv "$ROOT_DIR/data/update-complete.json.tmp" "$ROOT_DIR/data/update-complete.json"
-# Restart PM2 apps
+# Restart PM2 apps — remove marker if restart fails so it isn't misread on boot
step "restart" "running" "Restarting PortOS..."
-npm run pm2:restart
+if ! npm run pm2:restart; then
+ rm -f "$ROOT_DIR/data/update-complete.json"
+ exit 1
+fi
step "restart" "done" "PortOS restarted"
echo ""