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
refactor([cos-runner-client-non-json-response-handling]): reuse safeJSONParse for tolerant runner parsing
[._.]/ Adam Eivy843f0a2ec8da
diff --git a/PLAN.md b/PLAN.mdindex 9b94ecd7c2..2985e59b48 100644--- a/PLAN.md+++ b/PLAN.md@@ -6,6 +6,7 @@ For project goals, see [GOALS.md](./GOALS.md). For completed work, see [.changelClaimable, well-specified, no decision needed — `/claim`-able. Unordered.+- [ ] [shared-tolerant-fetch-json-helper] **Extract a shared tolerant `response.text()`→JSON parser and adopt it across the other fetch-based clients.** `cosRunnerClient.js` now reads runner bodies via a local `readRunnerJson` (delegating to `safeJSONParse`) so a non-JSON error page no longer crashes with `Unexpected token <`. The same unguarded `await response.json()` crash risk remains in other fetch-based clients: `server/lib/aiProvider.js` (~146), `server/services/browserService.js` (~111/207/216/269/291), `server/services/memoryEmbeddings.js`, `huggingFaceCatalog.js`, `messageTokenExtractor.js`, `ollamaManager.js`, and `server/lib/aiToolkit/providers.js` (the `.catch(() => ({ data: [] }))` band-aids). Promote a `fetchJsonTolerant(response)` / `readResponseJson(response)` helper (adjacent to `server/lib/fetchWithTimeout.js`, registered in the lib barrel + README) and migrate these call sites. Deferred from the `[cos-runner-client-non-json-response-handling]` PR (out of its stated single-file scope); surfaced by /simplify altitude (2026-06-01).- [ ] [cos-agent-output-write-amplification] **`appendAgentOutput` write-amplification — already batched via `appendAgentOutputLines`, audit non-runner producers.** CLAUDE.md documents the streaming-producers convention (batched variant + ~250ms debounce + drain on completion); the runner path uses it. Verify every producer that calls `appendAgentOutput` directly (`server/services/cosAgents.js:216`) is human-pace, not a hot loop. If any hot producer is still calling the per-line variant, migrate it. Likely a no-op audit; capture it so the convention doesn't silently drift.- [ ] [cos-on-demand-mark-app-review-started-dedupe] **`evaluateTasks` on-demand loop — dedupe `markAppReviewStarted` calls per app.** When multiple on-demand requests for the same app are queued in a single evaluation cycle (`server/services/cos.js:730`), `markAppReviewStarted` is called once per request — wasted state writes. Collect unique `appId`s before the loop and call once. Nit-level priority.- [ ] [subagent-spawner-top-level-side-effects] **`subAgentSpawner.js` top-level side effects — move init into an explicit `initSpawner()`.** Module-load (`server/services/subAgentSpawner.js:153`) wires event listeners + timers, which interferes with test isolation (each test import re-arms them). Wrap the init block in `export function initSpawner()` and call it from `server/index.js` after dependencies are ready. Treat as a refactor — needs care that no consumer relies on the import-time wiring.diff --git a/server/services/cosRunnerClient.js b/server/services/cosRunnerClient.jsindex 47a780499f..a04ca88c9b 100644--- a/server/services/cosRunnerClient.js+++ b/server/services/cosRunnerClient.js@@ -7,6 +7,7 @@import { io } from 'socket.io-client';import { fetchWithTimeout } from '../lib/fetchWithTimeout.js';+import { safeJSONParse } from '../lib/fileUtils.js';const COS_RUNNER_URL = process.env.COS_RUNNER_URL || 'http://localhost:5558';@@ -16,20 +17,16 @@ const COS_RUNNER_URL = process.env.COS_RUNNER_URL || 'http://localhost:5558';* The runner can answer with an HTML error page (e.g. a 500 while PM2 is* restarting it mid-request) instead of JSON. Calling `response.json()` directly* on that body throws `Unexpected token <` and masks the runner's actual error.- * We read the raw text and parse it ourselves, falling back to- * `{ error: <raw text> }` so callers surface the runner's real message.+ * We read the raw text and parse it tolerantly via `safeJSONParse`, falling back+ * to `{ error: <raw text> }` so callers surface the runner's real message.*- * The try/catch here is intentional tolerant-parsing — it deliberately converts- * a parse failure into a value rather than letting it bubble, which is the point.+ * An empty body returns `{}` (an empty success, distinct from a parse failure),+ * so spreading callers like `getRunnerHealth` don't pick up a spurious `error`.*/async function readRunnerJson(response) {const text = await response.text();if (!text) return {};- try {- return JSON.parse(text);- } catch {- return { error: text.trim() };- }+ return safeJSONParse(text, { error: text.trim() });}// Socket.IO client for real-time events