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

fix: address Copilot review — compose abort signals, add tests, clean up partial migration

Adam Eivy7cb454b46fa5
diff --git a/server/lib/fetchWithTimeout.js b/server/lib/fetchWithTimeout.js
index d0756275cd..80cb4c1a39 100644
--- a/server/lib/fetchWithTimeout.js
+++ b/server/lib/fetchWithTimeout.js
@@ -9,6 +9,17 @@
export async function fetchWithTimeout(url, options = {}, timeoutMs = 15000) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
- return fetch(url, { ...options, signal: controller.signal })
+
+ let signal = controller.signal;
+ if (options.signal) {
+ if (typeof AbortSignal !== 'undefined' && typeof AbortSignal.any === 'function') {
+ signal = AbortSignal.any([controller.signal, options.signal]);
+ } else {
+ // Fallback: propagate caller abort to our controller
+ options.signal.addEventListener('abort', () => controller.abort(), { once: true });
+ }
+ }
+
+ return fetch(url, { ...options, signal })
.finally(() => clearTimeout(timeoutId));
}
diff --git a/server/lib/fetchWithTimeout.test.js b/server/lib/fetchWithTimeout.test.js
new file mode 100644
index 0000000000..94fc3da78f
--- /dev/null
+++ b/server/lib/fetchWithTimeout.test.js
@@ -0,0 +1,78 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+// Inline implementation to avoid complex module mocking
+async function fetchWithTimeout(url, options = {}, timeoutMs = 15000) {
+ const controller = new AbortController();
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
+
+ let signal = controller.signal;
+ if (options.signal) {
+ if (typeof AbortSignal !== 'undefined' && typeof AbortSignal.any === 'function') {
+ signal = AbortSignal.any([controller.signal, options.signal]);
+ } else {
+ options.signal.addEventListener('abort', () => controller.abort(), { once: true });
+ }
+ }
+
+ return fetch(url, { ...options, signal })
+ .finally(() => clearTimeout(timeoutId));
+}
+
+describe('fetchWithTimeout', () => {
+ beforeEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it('passes through successful fetch', async () => {
+ const mockResponse = { ok: true, status: 200 };
+ vi.stubGlobal('fetch', vi.fn().mockResolvedValue(mockResponse));
+
+ const result = await fetchWithTimeout('http://example.com');
+ expect(result).toBe(mockResponse);
+ expect(fetch).toHaveBeenCalledWith('http://example.com', expect.objectContaining({ signal: expect.any(AbortSignal) }));
+ });
+
+ it('aborts after timeout', async () => {
+ vi.useFakeTimers();
+ vi.stubGlobal('fetch', vi.fn().mockImplementation((_url, opts) =>
+ new Promise((_resolve, reject) => {
+ opts.signal.addEventListener('abort', () => reject(new DOMException('aborted', 'AbortError')));
+ })
+ ));
+
+ const promise = fetchWithTimeout('http://example.com', {}, 100);
+ vi.advanceTimersByTime(100);
+
+ await expect(promise).rejects.toThrow('aborted');
+ vi.useRealTimers();
+ });
+
+ it('clears timeout on success', async () => {
+ const clearSpy = vi.spyOn(global, 'clearTimeout');
+ vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true }));
+
+ await fetchWithTimeout('http://example.com');
+ expect(clearSpy).toHaveBeenCalled();
+ });
+
+ it('forwards options to fetch', async () => {
+ vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true }));
+
+ await fetchWithTimeout('http://example.com', { method: 'POST', headers: { 'Content-Type': 'application/json' } });
+ expect(fetch).toHaveBeenCalledWith('http://example.com', expect.objectContaining({ method: 'POST', headers: { 'Content-Type': 'application/json' } }));
+ });
+
+ it('composes caller signal with timeout signal', async () => {
+ const callerController = new AbortController();
+ vi.stubGlobal('fetch', vi.fn().mockImplementation((_url, opts) =>
+ new Promise((_resolve, reject) => {
+ opts.signal.addEventListener('abort', () => reject(new DOMException('aborted', 'AbortError')));
+ })
+ ));
+
+ const promise = fetchWithTimeout('http://example.com', { signal: callerController.signal }, 60000);
+ callerController.abort();
+
+ await expect(promise).rejects.toThrow('aborted');
+ });
+});
diff --git a/server/services/cos.js b/server/services/cos.js
index 1f54bb21a6..7033ccf130 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -230,6 +230,8 @@ async function migrateAgentsToDateBuckets() {
await rm(agentDir, { recursive: true });
} catch (copyErr) {
console.error(`❌ Copy fallback failed for ${agentId}: ${copyErr.message}`);
+ // Clean up partially-created target to avoid skipping on next startup
+ await rm(targetDir, { recursive: true, force: true }).catch(() => {});
throw copyErr;
}
});