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: fix stale trigger return, deepPartial schema, user task guard, cleanup on delete, inline delete confirm, extract timeAgo, delete branch on worktree removal, add tests

Adam Eivy9fc90338d8d5
diff --git a/client/src/components/feature-agents/FeatureAgentCard.jsx b/client/src/components/feature-agents/FeatureAgentCard.jsx
index 18a6dc8eb0..65a19ee541 100644
--- a/client/src/components/feature-agents/FeatureAgentCard.jsx
+++ b/client/src/components/feature-agents/FeatureAgentCard.jsx
@@ -1,17 +1,10 @@
+import { useState } from 'react';
import { Link } from 'react-router-dom';
import { Play, Pause, Square, Zap, Trash2 } from 'lucide-react';
-import { STATUS_COLORS, STATUS_BG, PRIORITY_COLORS, SCHEDULE_LABELS } from './constants';
-
-function timeAgo(dateStr) {
- if (!dateStr) return 'never';
- const seconds = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1000);
- if (seconds < 60) return 'just now';
- if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`;
- if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`;
- return `${Math.floor(seconds / 86400)}d ago`;
-}
+import { STATUS_COLORS, STATUS_BG, PRIORITY_COLORS, SCHEDULE_LABELS, timeAgo } from './constants';
export default function FeatureAgentCard({ agent, onStart, onPause, onResume, onStop, onTrigger, onDelete }) {
+ const [confirmingDelete, setConfirmingDelete] = useState(false);
const statusColor = STATUS_COLORS[agent.status] || 'text-gray-400';
const statusBg = STATUS_BG[agent.status] || 'bg-gray-400/10';
const priorityColor = PRIORITY_COLORS[agent.priority] || 'text-gray-400';
@@ -82,9 +75,29 @@ export default function FeatureAgentCard({ agent, onStart, onPause, onResume, on
</button>
)}
<div className="flex-1" />
- <button onClick={() => onDelete(agent.id)} className="flex items-center gap-1 px-2 py-1 text-xs text-port-error hover:bg-port-error/10 rounded transition-colors">
- <Trash2 size={12} />
- </button>
+ {confirmingDelete ? (
+ <div className="flex items-center gap-2">
+ <span className="text-xs text-gray-400">Delete?</span>
+ <div className="inline-flex rounded-lg overflow-hidden border border-port-border">
+ <button
+ onClick={() => { onDelete(agent.id); setConfirmingDelete(false); }}
+ className="px-2 py-1 bg-port-error/20 text-port-error hover:bg-port-error/30 text-xs"
+ >
+ Yes
+ </button>
+ <button
+ onClick={() => setConfirmingDelete(false)}
+ className="px-2 py-1 bg-port-card text-gray-400 hover:bg-port-border/50 text-xs border-l border-port-border"
+ >
+ No
+ </button>
+ </div>
+ </div>
+ ) : (
+ <button onClick={() => setConfirmingDelete(true)} className="flex items-center gap-1 px-2 py-1 text-xs text-port-error hover:bg-port-error/10 rounded transition-colors">
+ <Trash2 size={12} />
+ </button>
+ )}
</div>
</div>
);
diff --git a/client/src/components/feature-agents/GitTab.jsx b/client/src/components/feature-agents/GitTab.jsx
index 1e4ad59594..28ce0c9eeb 100644
--- a/client/src/components/feature-agents/GitTab.jsx
+++ b/client/src/components/feature-agents/GitTab.jsx
@@ -1,4 +1,4 @@
-import { GitBranch, ExternalLink } from 'lucide-react';
+import { GitBranch } from 'lucide-react';
export default function GitTab({ agent }) {
const git = agent.git || {};
diff --git a/client/src/components/feature-agents/OverviewTab.jsx b/client/src/components/feature-agents/OverviewTab.jsx
index 6ad4d2eecb..a82915b3a8 100644
--- a/client/src/components/feature-agents/OverviewTab.jsx
+++ b/client/src/components/feature-agents/OverviewTab.jsx
@@ -1,14 +1,5 @@
import { Play, Pause, Square, Zap, Clock, Target, GitBranch, AlertTriangle } from 'lucide-react';
-import { STATUS_COLORS, STATUS_BG, PRIORITY_COLORS, SCHEDULE_LABELS, AUTONOMY_LABELS } from './constants';
-
-function timeAgo(dateStr) {
- if (!dateStr) return 'never';
- const seconds = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1000);
- if (seconds < 60) return 'just now';
- if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`;
- if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`;
- return `${Math.floor(seconds / 86400)}d ago`;
-}
+import { STATUS_COLORS, STATUS_BG, PRIORITY_COLORS, SCHEDULE_LABELS, AUTONOMY_LABELS, timeAgo } from './constants';
export default function OverviewTab({ agent, onStart, onPause, onResume, onStop, onTrigger }) {
const statusColor = STATUS_COLORS[agent.status] || 'text-gray-400';
diff --git a/client/src/components/feature-agents/RunsTab.jsx b/client/src/components/feature-agents/RunsTab.jsx
index 44bc1f5e19..7369724f8f 100644
--- a/client/src/components/feature-agents/RunsTab.jsx
+++ b/client/src/components/feature-agents/RunsTab.jsx
@@ -2,15 +2,7 @@ import { useState, useEffect } from 'react';
import { Play, ChevronDown, ChevronRight } from 'lucide-react';
import * as api from '../../services/api';
import BrailleSpinner from '../BrailleSpinner';
-
-function timeAgo(dateStr) {
- if (!dateStr) return 'unknown';
- const seconds = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1000);
- if (seconds < 60) return 'just now';
- if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`;
- if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`;
- return `${Math.floor(seconds / 86400)}d ago`;
-}
+import { timeAgo } from './constants';
export default function RunsTab({ agent }) {
const [runs, setRuns] = useState([]);
diff --git a/client/src/components/feature-agents/constants.js b/client/src/components/feature-agents/constants.js
index b2e19b908e..b530379e57 100644
--- a/client/src/components/feature-agents/constants.js
+++ b/client/src/components/feature-agents/constants.js
@@ -1,5 +1,14 @@
import { LayoutDashboard, Settings, Play, GitBranch, Terminal } from 'lucide-react';
+export function timeAgo(dateStr) {
+ if (!dateStr) return 'never';
+ const seconds = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1000);
+ if (seconds < 60) return 'just now';
+ if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`;
+ if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`;
+ return `${Math.floor(seconds / 86400)}d ago`;
+}
+
export const TABS = [
{ id: 'overview', label: 'Overview', icon: LayoutDashboard },
{ id: 'config', label: 'Config', icon: Settings },
diff --git a/server/lib/validation.js b/server/lib/validation.js
index a33cf2ad11..7cc26b1cc3 100644
--- a/server/lib/validation.js
+++ b/server/lib/validation.js
@@ -495,7 +495,7 @@ export const featureAgentSchema = z.object({
priority: featureAgentPrioritySchema.default('MEDIUM')
});
-export const featureAgentUpdateSchema = featureAgentSchema.partial();
+export const featureAgentUpdateSchema = featureAgentSchema.deepPartial();
/**
* Validate data against a schema
diff --git a/server/lib/validation.test.js b/server/lib/validation.test.js
index 7ebb4ac6b5..1ac6f37110 100644
--- a/server/lib/validation.test.js
+++ b/server/lib/validation.test.js
@@ -5,6 +5,8 @@ import {
appUpdateSchema,
providerSchema,
runSchema,
+ featureAgentSchema,
+ featureAgentUpdateSchema,
validate
} from './validation.js';
@@ -322,6 +324,83 @@ describe('validation.js', () => {
});
});
+ describe('featureAgentSchema', () => {
+ const validAgent = {
+ name: 'UI Polish Agent',
+ description: 'Iterates on UI improvements',
+ appId: 'app-001',
+ git: { branchName: 'feature/ui-polish' }
+ };
+
+ it('should validate a minimal feature agent', () => {
+ const result = featureAgentSchema.safeParse(validAgent);
+ expect(result.success).toBe(true);
+ expect(result.data.status).toBeUndefined(); // status is not in create schema
+ expect(result.data.git.baseBranch).toBe('main'); // default
+ expect(result.data.priority).toBe('MEDIUM'); // default
+ });
+
+ it('should require name', () => {
+ const result = featureAgentSchema.safeParse({ ...validAgent, name: '' });
+ expect(result.success).toBe(false);
+ });
+
+ it('should require description', () => {
+ const result = featureAgentSchema.safeParse({ ...validAgent, description: '' });
+ expect(result.success).toBe(false);
+ });
+
+ it('should require appId', () => {
+ const result = featureAgentSchema.safeParse({ ...validAgent, appId: '' });
+ expect(result.success).toBe(false);
+ });
+
+ it('should require git.branchName', () => {
+ const result = featureAgentSchema.safeParse({ ...validAgent, git: { branchName: '' } });
+ expect(result.success).toBe(false);
+ });
+
+ it('should apply defaults for nested objects', () => {
+ const result = featureAgentSchema.safeParse(validAgent);
+ expect(result.success).toBe(true);
+ expect(result.data.featureScope).toEqual({ directories: [], filePatterns: [], excludePatterns: [] });
+ expect(result.data.schedule.mode).toBe('continuous');
+ expect(result.data.autonomyLevel).toBe('assistant');
+ });
+
+ it('should validate priority enum', () => {
+ const result = featureAgentSchema.safeParse({ ...validAgent, priority: 'INVALID' });
+ expect(result.success).toBe(false);
+ });
+ });
+
+ describe('featureAgentUpdateSchema (deepPartial)', () => {
+ it('should allow partial top-level fields', () => {
+ const result = featureAgentUpdateSchema.safeParse({ name: 'New Name' });
+ expect(result.success).toBe(true);
+ });
+
+ it('should allow partial nested git fields', () => {
+ const result = featureAgentUpdateSchema.safeParse({ git: { baseBranch: 'develop' } });
+ expect(result.success).toBe(true);
+ });
+
+ it('should allow partial nested schedule fields', () => {
+ const result = featureAgentUpdateSchema.safeParse({ schedule: { mode: 'interval' } });
+ expect(result.success).toBe(true);
+ });
+
+ it('should allow empty update', () => {
+ const result = featureAgentUpdateSchema.safeParse({});
+ expect(result.success).toBe(true);
+ });
+
+ it('should still validate field values when provided', () => {
+ const result = featureAgentUpdateSchema.safeParse({ name: '' });
+ expect(result.success).toBe(false);
+ });
+ });
+
describe('validate function', () => {
it('should return success:true with data for valid input', () => {
const data = { name: 'Test', repoPath: '/path' };
diff --git a/server/services/cos.js b/server/services/cos.js
index 860a35372b..c959e60524 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -1087,8 +1087,8 @@ export async function evaluateTasks() {
}
}
- // Priority 3.6: Feature Agents (after autonomous jobs)
- if (tasksToSpawn.length < availableSlots) {
+ // Priority 3.6: Feature Agents (after autonomous jobs, yield to user tasks)
+ if (tasksToSpawn.length < availableSlots && !hasPendingUserTasks) {
const { getDueFeatureAgents, generateTaskFromFeatureAgent } = await import('./featureAgents.js');
const dueAgents = await getDueFeatureAgents().catch(err => {
emitLog('debug', `Feature agents check failed: ${err.message}`);
diff --git a/server/services/featureAgents.js b/server/services/featureAgents.js
index c0e3849935..69712d2e1e 100644
--- a/server/services/featureAgents.js
+++ b/server/services/featureAgents.js
@@ -6,7 +6,7 @@
* git worktree/branch and runs on a schedule.
*/
-import { writeFile, readFile, rename, mkdir, readdir } from 'fs/promises';
+import { writeFile, readFile, rename, mkdir, readdir, rm } from 'fs/promises';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { existsSync } from 'fs';
@@ -136,8 +136,9 @@ export async function deleteFeatureAgent(id) {
data.agents.splice(idx, 1);
await writeData(data);
- // Clean up worktree directory if it exists
- const worktreeDir = join(FA_DIR, id, 'worktree');
+ // Clean up worktree and data directory
+ const agentDir = join(FA_DIR, id);
+ const worktreeDir = join(agentDir, 'worktree');
if (existsSync(worktreeDir)) {
const app = await getAppById(agent.appId).catch(() => null);
if (app?.repoPath) {
@@ -147,6 +148,12 @@ export async function deleteFeatureAgent(id) {
});
}
}
+ // Remove the entire data directory (runs, etc.)
+ if (existsSync(agentDir)) {
+ await rm(agentDir, { recursive: true, force: true }).catch(err => {
+ console.log(`⚠️ Feature agent data cleanup failed: ${err.message}`);
+ });
+ }
console.log(`🤖 Feature agent deleted: ${agent.name} (${id})`);
cosEvents.emit(`${EVT}:status`, { id, status: 'deleted', name: agent.name });
@@ -259,9 +266,9 @@ export async function triggerFeatureAgent(id) {
const agent = await getFeatureAgent(id);
if (!agent) return null;
- // Temporarily mark as active if in draft/paused for this run
+ // Activate if in draft/paused, then return the updated agent
if (agent.status === 'draft' || agent.status === 'paused') {
- await activateFeatureAgent(id);
+ return await activateFeatureAgent(id);
}
return agent;
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.js
index 4f901e57c1..ee03406800 100644
--- a/server/services/worktreeManager.js
+++ b/server/services/worktreeManager.js
@@ -203,6 +203,8 @@ export async function removePersistentWorktree(featureAgentId, sourceWorkspace,
await execGit(['worktree', 'prune'], sourceWorkspace).catch(() => {});
});
+ await execGit(['branch', '-D', branchName], sourceWorkspace).catch(() => {});
+
console.log(`🌳 Removed persistent worktree for feature agent ${featureAgentId}`);
return { removed: true };
}
diff --git a/server/services/worktreeManager.test.js b/server/services/worktreeManager.test.js
index 6c0ba7cf37..17ab693157 100644
--- a/server/services/worktreeManager.test.js
+++ b/server/services/worktreeManager.test.js
@@ -110,6 +110,32 @@ detached
});
});
+describe('Persistent Worktree Path Construction', () => {
+ function buildPersistentWorktreePath(worktreesDir, featureAgentId) {
+ const path = require('path');
+ return path.join(worktreesDir, '..', 'feature-agents', featureAgentId, 'worktree');
+ }
+
+ it('should place worktree under feature-agents directory', () => {
+ const path = buildPersistentWorktreePath('/data/cos/worktrees', 'fa-abc12345');
+ expect(path).toContain('feature-agents');
+ expect(path).toContain('fa-abc12345');
+ expect(path.endsWith('worktree')).toBe(true);
+ });
+
+ it('should be separate from regular worktrees directory', () => {
+ const regularPath = '/data/cos/worktrees/agent-12345678';
+ const persistentPath = buildPersistentWorktreePath('/data/cos/worktrees', 'fa-abc12345');
+ expect(persistentPath).not.toContain('/worktrees/fa-');
+ expect(regularPath).not.toContain('feature-agents');
+ });
+
+ it('should use feature agent ID as parent directory', () => {
+ const path = buildPersistentWorktreePath('/data/cos/worktrees', 'fa-12345678');
+ expect(path).toContain('/fa-12345678/');
+ });
+});
+
describe('Orphaned Worktree Detection', () => {
function findOrphanedWorktrees(worktrees, worktreesDir, activeAgentIds) {
return worktrees.filter(wt => {