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([issue-12602]): concurrent Ollama service wakes no longer bounce the daemon the other wake started

[._.]/ Adam Eivy800c560b6f14
diff --git a/server/lib/README.md b/server/lib/README.md
index 5335cfe05f..ebaaf4693b 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -763,7 +763,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub
| `slashdoCatalog.js` | The single catalog of which bundled slashdo workflows PortOS offers as a one-click agent run — `SLASHDO_WORKFLOWS` (`{command, label, description, icon, templateName, templatePrompt?, settings, appTypes, configurable?}`), `getSlashdoWorkflow()` (the allowlist gate for `POST /api/cos/tasks/slashdo`), `SLASHDO_COMMAND_NAMES`, `slashdoWorkflowsForApp(isSwiftApp)`, `SLASHDO_APP_TYPES`, and the two run-shape postures `WORKFLOW_OWNS_ITS_OWN_GIT` (commit-shaped) / `WORKFLOW_REPORTS_NO_CODE` (report-shaped — carries `worktreeChangesExpected: false` so a filed-issue/printed-report run retains its non-code deliverable posture). Backs the Agent Operations buttons, the CoS quick templates (`taskTemplates.js`), and the route allowlist, replacing two catalogs that had drifted. `client/src/lib/slashdoCatalog.js` decorates this list with button styling rather than restating it. |
| `slashdoInvocation.js` | Resolves how a bundled slashdo workflow is invoked on a given host CLI. `resolveSlashdoInvocation({command, args, providerId, providerCommand, leanMode})` → `{command, args, style, invocation, skillName}` for the three shapes slashdo's installer produces (`slash-namespaced` `/do:x` for Claude Code, `slash-flat` `/do-x` for OpenCode, `skill` — an Agent Skill selected by name — for codex/grok/antigravity and any unidentified provider). Plus `buildSlashdoSection(resolved, body, {bodyPath, reviewWith})` (pure renderer; the caller loads the body via `loadSlashdoFile`. With a staged `bodyPath` it emits a filesystem pointer for deferred bundles or bodies over `SLASHDO_INLINE_BUDGET_CHARS` (24,000)), `unreachableReviewerIncludes({reviewers, usernames})` → the reviewer-variant lib includes a run can never reach (the `skipIncludes` set for `loadSlashdoFile`; defaults to pruning NOTHING for an unresolved/unrecognized reviewer set, since an over-pruned prompt is worse than a fat one), `SLASHDO_REVIEWER_INCLUDES`/`SLASHDO_REVIEWER_INCLUDE_NAMES`, `slashdoSkillName`, `isValidSlashdoCommand` (the one definition of the safe bare-command shape, also gating the Zod schemas), `agentOwnsPrWorkflow({providerType, leanMode})` — a strictly WEAKER question than typing a slash command: any local `cli`/`tui` harness that is not a lean `--bare` session drives its own commit → push → PR → review → merge (#3733), with `PR_OPENED_BY` (`agent-slashdo` / `agent-inline` / `portos`) naming WHO opens a run’s PR per the prompt it was given (stamped as `metadata.prOpenedBy` by `promptOpensOwnPr`, services/promptSections/completion.js) and `resolvePrOpenedBy({persistedPrOpenedBy, persistedOwnsPrWorkflow, …})` reading it back for a completed agent — mapping both the legacy #3733 `ownsPrWorkflow` boolean and a pre-#3733 record with no stamp at all; `oversizedBodyPointer(bodyPath, body)` (the shared "it is on disk, go read it" line); `resolvePrOwnership({task, isTruthyMeta, persistedPrOpenedBy, persistedOwnsPrWorkflow, providerId, providerCommand, leanMode})` → `{taskOpenPR, prOpenedBy, agentOpensOwnPr, prClaimExpected}` — the persisted-first, task-shape-aware ownership verdict plus provider descriptor that both in-process spawners (TUI `finish()`, direct-CLI `close`) hand to `runSpawnerCompletionCleanup`, so the ownership question and the cleanup acting on it cannot disagree; and `canTypeSlashCommands()` — the single predicate behind `agentPromptBuilder.js`'s completion-workflow gates (can this session TYPE `/do:pr` / `/simplify`?), which reads `resolveSlashdoStyle`'s `assumeClaudeWhenUnknown` posture because the spawners resolve a blank command to `claude`. Provider is only known at spawn time, so a task persists the BARE command name — never a rendered `/do:x` string. |
| `slashdoLoader.js` | Shared slashdo renderer adapter: `loadSlashdoBundle(cmd, {stripFrontmatter, skipIncludes, defer})` returns an entrypoint and supporting files using the bundled transformer; `loadSlashdoFile` returns a self-contained command; `loadSlashdoLib` expands explicit reads for legacy recipes without traversing see-also links. `writeResolvedSlashdoBody(cmd, body, {files})` stages immutable content-addressed bundles under `PATHS.slashdoResolved`, with relative library paths and the entrypoint written last. Missing required references fail dispatch rather than silently removing workflow gates. |
-| `singleFlight.js` | `createSingleFlight()` → `run(key, fn)` — keyed in-flight coalescer: concurrent calls for the same key share one `fn()` execution and result; the slot auto-clears on settle. Minimal by design (no TTL/result cache layered on top, doesn't reject concurrent callers). Used by `services/promptRunner.js`'s fallback mark-and-pick. |
+| `singleFlight.js` | `createSingleFlight()` → `run(key, fn)` — keyed in-flight coalescer: concurrent calls for the same key share one `fn()` execution and result; the slot auto-clears on settle. `createSingleFlight({ exclusive: true })` also keeps different keys from overlapping: a different key waits for the in-flight call, then joins or starts its own (used where the work must never run twice at once, such as starting a local runtime). Minimal by design (no TTL/result cache layered on top, doesn't reject concurrent callers). Used by `services/promptRunner.js`'s fallback mark-and-pick, `lib/managedDaemon.js`'s on-demand wake, and `services/ollamaManager.js`'s starts. |
| `staleWhileRevalidate.js` | `createStaleWhileRevalidate({ ttlMs, failureBackoffMs?, isComplete?, partialTtlMs? })` → `read(key, produce, { wait })` / `clear(key?)` — TTL cache that serves a STALE value immediately and revalidates behind the caller, for readings too slow to block on (10-20s CLI/PTY spawns). `wait`: `'fresh'` bypasses and blocks, `'cached'` (default) blocks only on a cold cache, `'never'` returns the `PENDING` symbol on a cold cache for UIs that render "still reading" and poll. Failures keep the last good value and back off; a cold cache whose producer failed throws rather than promising a reading that isn't coming. Used by `providerUsage.js` (TUI scrapes) and `claudeCodeUsage.js`. |
| `staticImportGraph.js` | Static ES-module import scanning for structural test guards. `staticImportSpecifiers(file)` → every specifier a file statically imports (verbatim, source order); `staticImportClosure(entry)` → `{files, packages}` for the whole reachable graph; `buildStaticImportGraph(rootDir)` → `Map<relPath, relPath[]>` of one directory's internal static edges (recurses into subdirectories, skips `.test.js`); `listModuleFiles(rootDir)` → every non-test `.js` under a directory, keyed by `/`-separated relative path (the same walk the graph builder uses, shared so a layering guard cannot drift from it); `findImportCycles(graph)` → each cycle rendered as `a.js -> b.js -> a.js` (depth-first, so WHICH rings it names depends on where the walk enters a component — fine for an "is this empty?" assertion, unusable as a baseline); `findImportCycleComponents(graph)` → the cyclic strongly-connected components as sorted member lists, traversal-order invariant and therefore baselineable (#5693); `specifierMatchesPackage(spec, pkg)` matches a package or any subpath; `toModuleKey(relPath)` is the one graph-key mint — always `/`-separated, because the keys are built two ways (entry-name concatenation vs `path.relative`) and Windows spells the second one with `\`, which silently dropped every edge into a subdirectory module and made the acyclicity guards pass vacuously there (#5909). Static imports only — `await import()` is deferred and can't create a load-time cycle or drag a native dep into an init graph. Shared by `serviceImportCycles.test.js` (the tree-wide cycle ratchet), `agentImportCycles.test.js` and `twinImportCycles.test.js` (per-cluster acyclicity) and `spriteAnimationTracks.test.js` (the request-validation graph reaches no sharp/ffmpeg). |
| `progressChannels.js` | `createProgressChannels({ label, describeKey })` — shared GET/POST progress channel lifecycle with late subscriber replay, idle reservation reaping, and one active stream owner per key. |
diff --git a/server/lib/managedDaemon.js b/server/lib/managedDaemon.js
index 3e5bb0dacf..b617a8a45b 100644
--- a/server/lib/managedDaemon.js
+++ b/server/lib/managedDaemon.js
@@ -1,5 +1,6 @@
import { getMemoryStats } from './memoryStats.js';
import { localEndpointPort } from './localEndpoint.js';
+import { createSingleFlight } from './singleFlight.js';
/**
* Shared plumbing for a local daemon PortOS runs as an optional PM2 process
@@ -352,13 +353,11 @@ export function createOnDemandDaemon({
* live/saved state, not from the caller. The slot clears on settle, so a later
* wake after another stop starts the daemon again.
*/
- let wakeInFlight = null;
+ const wakes = createSingleFlight();
- const ensureRunning = () => {
+ const ensureRunning = async () => {
markUsed();
- if (wakeInFlight) return wakeInFlight;
- wakeInFlight = wake().finally(() => { wakeInFlight = null; });
- return wakeInFlight;
+ return wakes.run('wake', wake);
};
const wake = async () => {
diff --git a/server/lib/singleFlight.js b/server/lib/singleFlight.js
index 1a25710d2c..a73afb1912 100644
--- a/server/lib/singleFlight.js
+++ b/server/lib/singleFlight.js
@@ -25,9 +25,16 @@
* on both settle paths and swallows the cleanup branch's copy of the rejection,
* while each caller's own `await run(...)` still observes the original reject.
*
+ * `exclusive: true` keeps different keys from running at the same time: a
+ * call whose key differs from the in-flight one waits for it to settle, then
+ * joins or starts its own. For work that must never overlap at all, such as
+ * starting one daemon, where the key only says whether a caller can share the
+ * result (`ollamaManager.startServer` keys on its launch env).
+ *
+ * @param {{ exclusive?: boolean }} [options]
* @returns {{ run: <T>(key: any, fn: () => Promise<T>) => Promise<T> }}
*/
-export function createSingleFlight() {
+export function createSingleFlight({ exclusive = false } = {}) {
const inFlight = new Map(); // key -> shared in-flight promise
function run(key, fn) {
@@ -44,5 +51,16 @@ export function createSingleFlight() {
return work;
}
- return { run };
+ // Registers synchronously when nothing is in flight: an async body runs
+ // up to its first await, so two back-to-back calls cannot both start.
+ async function runExclusive(key, fn) {
+ while (inFlight.size) {
+ const existing = inFlight.get(key);
+ if (existing) return existing;
+ await Promise.allSettled([...inFlight.values()]);
+ }
+ return run(key, fn);
+ }
+
+ return { run: exclusive ? runExclusive : run };
}
diff --git a/server/lib/singleFlight.test.js b/server/lib/singleFlight.test.js
index c4ad552ab1..55552ebcf2 100644
--- a/server/lib/singleFlight.test.js
+++ b/server/lib/singleFlight.test.js
@@ -23,6 +23,24 @@ describe('createSingleFlight', () => {
expect(await b).toBe('value');
});
+ it('exclusive mode queues a different key behind the in-flight call instead of overlapping it', async () => {
+ const sf = createSingleFlight({ exclusive: true });
+ const events = [];
+ let release;
+ const slow = () => { events.push('a:start'); return new Promise(r => { release = r; }); };
+
+ const a = sf.run('a', slow);
+ const joined = sf.run('a', slow);
+ const b = sf.run('b', async () => { events.push('b:start'); return 'b'; });
+ await tick();
+ expect(events).toEqual(['a:start']);
+
+ release('a');
+ expect(await joined).toBe(await a);
+ expect(await b).toBe('b');
+ expect(events).toEqual(['a:start', 'b:start']);
+ });
+
it('runs separate fns for distinct keys', async () => {
const sf = createSingleFlight();
const calls = [];
diff --git a/server/services/ollamaManager.js b/server/services/ollamaManager.js
index dcbf4b7cb7..9869607a60 100644
--- a/server/services/ollamaManager.js
+++ b/server/services/ollamaManager.js
@@ -38,6 +38,7 @@ import {
OLLAMA_AGENT_MIN_CONTEXT, OLLAMA_CONTEXT_ENV_VAR, managedOllamaBaseUrl, normalizeOllamaBaseUrl, resolveOllamaContextLength, withOllamaContextEnv
} from '../lib/ollamaContext.js'
import { compareSemver } from '../lib/versionUtils.js'
+import { createSingleFlight } from '../lib/singleFlight.js'
import { isSafeHfRepoRelativePath } from '../lib/hfCache.js'
import { assessDownloadPreflight, diskInsufficientError, DOWNLOAD_VERDICTS } from '../lib/downloadPreflight.js'
@@ -98,8 +99,9 @@ let lastInstalledModelsError = null
let lastCheckAt = null
let _managedProcess = null
let managedProcessPid = null
-// `{ signature, promise }` of the `startServer` spawn currently in flight.
-let startInFlight = null
+// Starts never overlap: two racing launches of one daemon kill or orphan each other.
+const startFlight = createSingleFlight({ exclusive: true })
+const ensureFlight = createSingleFlight({ exclusive: true })
// What PortOS knows about the Ollama daemon that is up RIGHT NOW. Every field
// describes ONE live process, so the whole record is written by `recordDaemon`
// and dropped by `forgetDaemon` — "which env" and "which process" can never
@@ -453,17 +455,8 @@ async function terminateManagedProcess() {
* @param {{ env?: Record<string, string> }} [options]
* @returns {Promise<{ success: boolean, running?: boolean, alreadyRunning?: boolean, pid?: number, error?: string }>}
*/
-async function startServer({ env = null } = {}) {
- const signature = envSignature(env)
- while (startInFlight) {
- if (startInFlight.signature === signature) return startInFlight.promise
- await startInFlight.promise.catch(() => {})
- }
- const promise = spawnServer(env).finally(() => {
- if (startInFlight?.promise === promise) startInFlight = null
- })
- startInFlight = { signature, promise }
- return promise
+function startServer({ env = null } = {}) {
+ return startFlight.run(envSignature(env), () => spawnServer(env))
}
async function spawnServer(env) {
@@ -642,7 +635,15 @@ async function stopPersistentService() {
}
}
-async function ensureRunning({ preferPersistent = false } = {}) {
+// Single-flight like `startServer`: concurrent `brew services start` calls
+// trip the bootstrap-conflict recovery, whose bootout stops the daemon the
+// other caller just started.
+function ensureRunning({ preferPersistent = false } = {}) {
+ const persistent = Boolean(preferPersistent)
+ return ensureFlight.run(persistent, () => bringUp(persistent))
+}
+
+async function bringUp(preferPersistent) {
if (await checkOllamaAvailable(true)) {
return { success: true, running: true, alreadyRunning: true, service: await getServiceStatus().catch(() => null) }
}
diff --git a/server/services/ollamaManager.test.js b/server/services/ollamaManager.test.js
index d9967bab60..c2a278a1c4 100644
--- a/server/services/ollamaManager.test.js
+++ b/server/services/ollamaManager.test.js
@@ -834,6 +834,39 @@ describe('ollamaManager.startPersistentService bootstrap recovery (homebrew)', (
expect(calls).toContain('brew services stop ollama')
})
+ // A second concurrent `brew services start` fails on the already-loaded job,
+ // and the recovery's bootout stops the daemon the first wake just started.
+ it('shares one service start between concurrent wakes', async () => {
+ const state = { up: false }
+ const body = { version: '0.24.0' }
+ vi.stubGlobal('fetch', vi.fn(async () => {
+ if (!state.up) throw new Error('ECONNREFUSED')
+ return { ok: true, status: 200, json: async () => body, text: async () => JSON.stringify(body) }
+ }))
+ let startAttempts = 0
+ execMock.impl = (cmd, args, opts, cb) => {
+ const a = (args || []).join(' ')
+ if (cmd === 'brew' && a === '--version') return cb(null, { stdout: 'Homebrew 4.0.0', stderr: '' })
+ if (cmd === 'brew' && a === 'services start ollama') {
+ startAttempts++
+ state.up = true
+ return cb(null, { stdout: '', stderr: '' })
+ }
+ if (cmd === 'brew' && a === 'services list') return cb(null, { stdout: 'ollama started testuser ~/Library/LaunchAgents/homebrew.mxcl.ollama.plist\n', stderr: '' })
+ return cb(new Error(`unexpected exec: ${cmd} ${a}`))
+ }
+
+ const { ensureRunning } = await loadManager()
+ const [first, second] = await Promise.all([
+ ensureRunning({ preferPersistent: true }),
+ ensureRunning({ preferPersistent: true }),
+ ])
+
+ expect(startAttempts).toBe(1)
+ expect(first).toMatchObject({ success: true, persistent: true })
+ expect(second).toBe(first)
+ })
+
it('does NOT bootout/retry when start fails for an unrelated reason', async () => {
stubReachable()
let startAttempts = 0