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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: improve Media audio detection and zombie agent handling
Adam Eivybf10cac95e4f
diff --git a/client/src/pages/Media.jsx b/client/src/pages/Media.jsxindex 26b1b3acdf..f3de7a75f6 100644--- a/client/src/pages/Media.jsx+++ b/client/src/pages/Media.jsx@@ -59,7 +59,7 @@ export default function Media() {}, [selectedVideo, selectedAudio]);// Set up audio level monitoring- const setupAudioAnalyser = useCallback((audioStream) => {+ const setupAudioAnalyser = useCallback(async (audioStream) => {// Clean up existing audio contextif (audioContextRef.current) {audioContextRef.current.close();@@ -69,6 +69,12 @@ export default function Media() {}const audioContext = new (window.AudioContext || window.webkitAudioContext)();++ // Resume AudioContext if suspended (browser autoplay policy)+ if (audioContext.state === 'suspended') {+ await audioContext.resume();+ }+const analyser = audioContext.createAnalyser();const source = audioContext.createMediaStreamSource(audioStream);@@ -78,12 +84,19 @@ export default function Media() {audioContextRef.current = audioContext;analyserRef.current = analyser;- const dataArray = new Uint8Array(analyser.frequencyBinCount);+ // Use time-domain data for better audio level detection+ const dataArray = new Uint8Array(analyser.fftSize);const updateLevel = () => {- analyser.getByteFrequencyData(dataArray);- const average = dataArray.reduce((a, b) => a + b, 0) / dataArray.length;- setAudioLevel(average / 255);+ analyser.getByteTimeDomainData(dataArray);+ // Calculate RMS (root mean square) for accurate audio level+ let sum = 0;+ for (let i = 0; i < dataArray.length; i++) {+ const normalized = (dataArray[i] - 128) / 128;+ sum += normalized * normalized;+ }+ const rms = Math.sqrt(sum / dataArray.length);+ setAudioLevel(Math.min(rms * 3, 1)); // Scale up for visibilityanimationFrameRef.current = requestAnimationFrame(updateLevel);};diff --git a/server/services/cos.js b/server/services/cos.jsindex c0adb49ff3..d3e3ee340e 100644--- a/server/services/cos.js+++ b/server/services/cos.js@@ -1861,42 +1861,44 @@ async function isPidAlive(pid) {* Cleanup zombie agents - agents marked as running but whose process is dead*/export async function cleanupZombieAgents() {- // Also check against activeAgents map from spawner+ // Check local tracking mapsconst { getActiveAgentIds } = await import('./subAgentSpawner.js');const activeIds = getActiveAgentIds();+ // Also check with the CoS runner for agents it's actively tracking+ const { getActiveAgentsFromRunner } = await import('./cosRunnerClient.js');+ const runnerAgents = await getActiveAgentsFromRunner().catch(() => []);+ const runnerAgentIds = new Set(runnerAgents.map(a => a.id));+return withStateLock(async () => {const state = await loadState();const runningAgents = Object.values(state.agents).filter(a => a.status === 'running');const cleaned = [];for (const agent of runningAgents) {- let isZombie = false;+ // Skip if tracked in local maps or runner+ if (activeIds.includes(agent.id) || runnerAgentIds.has(agent.id)) {+ continue;+ }- // If agent has a PID, check if it's still alive+ // If agent has a PID, verify the process is actually deadif (agent.pid) {const alive = await isPidAlive(agent.pid);- if (!alive) {- isZombie = true;- }- } else {- // No PID stored - check if it's in activeAgents map- // If not in map, it's a zombie from before PID tracking- if (!activeIds.includes(agent.id)) {- isZombie = true;+ if (alive) {+ // Process is still running, don't mark as zombie+ continue;}}- if (isZombie) {- console.log(`🧟 Zombie agent detected: ${agent.id} (PID ${agent.pid || 'unknown'} is dead)`);- state.agents[agent.id] = {- ...agent,- status: 'completed',- completedAt: new Date().toISOString(),- result: { success: false, error: 'Agent process was orphaned (server restart)' }- };- cleaned.push(agent.id);- }+ // Agent is not tracked anywhere and process is dead (or no PID) - it's a zombie+ console.log(`🧟 Zombie agent detected: ${agent.id} (PID ${agent.pid || 'unknown'} not running)`);+ state.agents[agent.id] = {+ ...agent,+ status: 'completed',+ completedAt: new Date().toISOString(),+ result: { success: false, error: 'Agent process terminated unexpectedly' }+ };+ cleaned.push(agent.id);}if (cleaned.length > 0) {diff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.jsindex 0bc536ce37..a7f2ca7917 100644--- a/server/services/subAgentSpawner.js+++ b/server/services/subAgentSpawner.js@@ -284,14 +284,20 @@ async function processAgentCompletion(agentId, task, success, outputBuffer) {}}-// Active agent processes+// Active agent processes (direct spawn mode)const activeAgents = new Map();+// Track runner-spawned agents (CoS Runner mode)+const runnerAgents = new Map();+/*** Get list of active agent IDs (for zombie detection)+ * Includes both direct mode and runner mode agents*/export function getActiveAgentIds() {- return Array.from(activeAgents.keys());+ const directIds = Array.from(activeAgents.keys());+ const runnerIds = Array.from(runnerAgents.keys());+ return [...directIds, ...runnerIds];}/**@@ -493,9 +499,6 @@ export async function initSpawner() {});}-// Track runner-spawned agents-const runnerAgents = new Map();-/*** Sync running agents from the runner (recovery after server restart)* This allows us to receive completion events for agents spawned before restart@@ -1221,7 +1224,7 @@ export async function cleanupOrphanedAgents() {console.log(`🧹 Cleaning up orphaned agent ${agent.id} (PID ${agent.pid || 'unknown'} not running)`);await markComplete(agent.id, {success: false,- error: 'Agent process was orphaned (server restart)',+ error: 'Agent process terminated unexpectedly',orphaned: true});cleanedCount++;