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
address review: improve spawn error mapping, icon route robustness, path traversal guard, and test coverage
Adam Eivy62e489e2ee8d
diff --git a/server/routes/apps.js b/server/routes/apps.jsindex a09a012ee4..ebc02f3714 100644--- a/server/routes/apps.js+++ b/server/routes/apps.js@@ -203,7 +203,8 @@ router.get('/:id/icon', loadApp, asyncHandler(async (req, res) => {}const contentType = getIconContentType(iconPath);- const iconStat = await stat(iconPath);+ const iconStat = await stat(iconPath).catch(e => e.code === 'ENOENT' ? null : Promise.reject(e));+ if (!iconStat) return res.status(404).json({ error: 'No app icon found' });const etag = `W/"${iconStat.mtimeMs.toString(36)}-${iconStat.size.toString(36)}"`;res.set('Content-Type', contentType);@@ -211,11 +212,15 @@ router.get('/:id/icon', loadApp, asyncHandler(async (req, res) => {res.set('ETag', etag);const ifNoneMatch = req.headers['if-none-match'];- if (ifNoneMatch === etag || ifNoneMatch === '*') {- return res.status(304).end();+ if (ifNoneMatch) {+ const tags = ifNoneMatch.split(',').map(v => v.trim());+ if (tags.includes('*') || tags.includes(etag)) {+ return res.status(304).end();+ }}- const iconData = await readFile(iconPath);+ const iconData = await readFile(iconPath).catch(e => e.code === 'ENOENT' ? null : Promise.reject(e));+ if (!iconData) return res.status(404).json({ error: 'No app icon found' });res.send(iconData);}));diff --git a/server/routes/apps.test.js b/server/routes/apps.test.jsindex 04ff3e9537..e560d03fe2 100644--- a/server/routes/apps.test.js+++ b/server/routes/apps.test.js@@ -13,6 +13,7 @@ vi.mock('../services/apps.js', () => ({archiveApp: vi.fn(),updateAppTaskTypeOverride: vi.fn(),getAppTaskTypeOverrides: vi.fn(),+ toggleAllAppTaskTypes: vi.fn(),notifyAppsChanged: vi.fn(),PORTOS_APP_ID: 'portos-default'}));@@ -589,5 +590,53 @@ describe('Apps Routes', () => {expect(second.status).toBe(304);});++ it('should return 304 when If-None-Match contains multiple ETags including match', async () => {+ const first = await request(app).get('/api/apps/app-001/icon');+ const etag = first.headers['etag'];++ const second = await request(app)+ .get('/api/apps/app-001/icon')+ .set('If-None-Match', `W/"other-etag", ${etag}, W/"another"`);++ expect(second.status).toBe(304);+ });+ });++ describe('PUT /api/apps/:id/task-types/all', () => {+ it('should toggle all task types for an app', async () => {+ appsService.getAppById.mockResolvedValue({ id: 'app-001', name: 'Test App' });+ appsService.toggleAllAppTaskTypes.mockResolvedValue({ id: 'app-001', name: 'Test App', taskTypeOverrides: { security: { enabled: true } } });++ const response = await request(app)+ .put('/api/apps/app-001/task-types/all')+ .send({ enabled: true });++ expect(response.status).toBe(200);+ expect(response.body.success).toBe(true);+ expect(response.body.appId).toBe('app-001');+ expect(appsService.toggleAllAppTaskTypes).toHaveBeenCalledWith('app-001', true);+ });++ it('should return 400 when enabled is not a boolean', async () => {+ appsService.getAppById.mockResolvedValue({ id: 'app-001', name: 'Test App' });++ const response = await request(app)+ .put('/api/apps/app-001/task-types/all')+ .send({ enabled: 'yes' });++ expect(response.status).toBe(400);+ expect(response.body.code).toBe('VALIDATION_ERROR');+ });++ it('should return 404 when app not found', async () => {+ appsService.getAppById.mockResolvedValue(null);++ const response = await request(app)+ .put('/api/apps/app-999/task-types/all')+ .send({ enabled: true });++ expect(response.status).toBe(404);+ });});});diff --git a/server/routes/cos.js b/server/routes/cos.jsindex 323bff012a..aa423fe67d 100644--- a/server/routes/cos.js+++ b/server/routes/cos.js@@ -281,7 +281,20 @@ router.post('/evaluate', asyncHandler(async (req, res) => {router.post('/tasks/:id/spawn', asyncHandler(async (req, res) => {const result = await cos.forceSpawnTask(req.params.id);if (result.error) {- throw new ServerError(result.error, { status: 400, code: 'SPAWN_FAILED' });+ const message = String(result.error);+ let status = 400;+ let code = 'SPAWN_FAILED';+ if (/not found/i.test(message)) {+ status = 404;+ code = 'NOT_FOUND';+ } else if (/not pending/i.test(message)) {+ status = 409;+ code = 'TASK_NOT_PENDING';+ } else if (/no available agent slots/i.test(message)) {+ status = 429;+ code = 'NO_CAPACITY';+ }+ throw new ServerError(result.error, { status, code });}res.json(result);}));diff --git a/server/routes/cos.test.js b/server/routes/cos.test.jsindex 18f201d1ac..bed9dc3ccc 100644--- a/server/routes/cos.test.js+++ b/server/routes/cos.test.js@@ -38,7 +38,8 @@ vi.mock('../services/cos.js', () => ({getReport: vi.fn(),generateReport: vi.fn(),listScripts: vi.fn(),- getScript: vi.fn()+ getScript: vi.fn(),+ forceSpawnTask: vi.fn()}));// Mock the taskWatcher service@@ -642,4 +643,43 @@ describe('CoS Routes', () => {expect(appActivity.clearAppCooldown).toHaveBeenCalledWith('app-001');});});++ describe('POST /api/cos/tasks/:id/spawn', () => {+ it('should force-spawn a pending task', async () => {+ cos.forceSpawnTask.mockResolvedValue({ success: true, taskId: 'task-001' });++ const response = await request(app).post('/api/cos/tasks/task-001/spawn');++ expect(response.status).toBe(200);+ expect(response.body.success).toBe(true);+ expect(cos.forceSpawnTask).toHaveBeenCalledWith('task-001');+ });++ it('should return 404 when task not found', async () => {+ cos.forceSpawnTask.mockResolvedValue({ error: 'Task not found' });++ const response = await request(app).post('/api/cos/tasks/bad-id/spawn');++ expect(response.status).toBe(404);+ expect(response.body.code).toBe('NOT_FOUND');+ });++ it('should return 409 when task is not pending', async () => {+ cos.forceSpawnTask.mockResolvedValue({ error: 'Task is completed, not pending' });++ const response = await request(app).post('/api/cos/tasks/task-002/spawn');++ expect(response.status).toBe(409);+ expect(response.body.code).toBe('TASK_NOT_PENDING');+ });++ it('should return 429 when no agent slots available', async () => {+ cos.forceSpawnTask.mockResolvedValue({ error: 'No available agent slots (3/3)' });++ const response = await request(app).post('/api/cos/tasks/task-003/spawn');++ expect(response.status).toBe(429);+ expect(response.body.code).toBe('NO_CAPACITY');+ });+ });});diff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.jsindex 63f8fd0ffe..5ff2dda2e0 100644--- a/server/services/subAgentSpawner.js+++ b/server/services/subAgentSpawner.js@@ -7,7 +7,7 @@*/import { spawn, execSync } from 'child_process';-import { join } from 'path';+import { join, resolve } from 'path';import { writeFile, mkdir, readFile, readdir, rm, stat, unlink } from 'fs/promises'; // mkdir kept for non-recursive useimport { existsSync } from 'fs';import { homedir } from 'os';@@ -1882,10 +1882,13 @@ async function handlePipelineProgression(task, agentId, success) {}, task.taskType);// Clean up pipeline artifacts (e.g., REVIEW.md left by stage 1)if (task.metadata.repoPath) {+ const repoRoot = resolve(task.metadata.repoPath);for (const stage of stages) {const file = stage.precondition?.fileNotExists;if (file) {- const filePath = join(task.metadata.repoPath, file);+ const filePath = resolve(repoRoot, file);+ // Only delete files within the repo directory (prevent path traversal)+ if (!filePath.startsWith(repoRoot + '/')) continue;await unlink(filePath).catch(() => {});}}