Repository navigation
Scope daemon compute per database (ADR-053): active-first embedding priority, close + idle eviction - #1603
Conversation
Introduce a process-global EmbeddingScheduler that gates the single shared embedding model and grants the active database's batches priority over other open databases. It keys on the database id as a plain String so this core type stays free of any daemon-side identifier. The scheduler exposes set_active/active_id/is_active plus an async acquire that returns a SchedulerPermit: the active database registers as a high-priority waiter and contends for the gate directly, while a non-active database defers on a notifier until no active batches remain, then contends. The permit re-evaluates priority on drop, so non-active work is never starved permanently. EmbeddingProcessor::new now takes the scheduler and this database's id, and process_until_empty acquires a permit around each batch, releasing it between batches so a higher-priority database can interleave. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…DR-053) Carry the shared EmbeddingScheduler in SharedContext and build one instance in build_shared_services. Each database's embedding processor is wired with the scheduler and its registry id; NodeServiceImpl holds the scheduler and marks its database active on a live WatchNodes stream, so the foreground database's embedding work takes priority with no protocol change. Add per-database lifecycle to DatabaseManager: - close(id): drop only that database's embedding processor and cancel its ai-chat event watcher, never releasing the process-global GPU context. - shutdown_all(): drain every open database's compute on daemon shutdown. - last_activity tracking touched on every routed request, plus an idle reaper (spawn_idle_reaper/evict_idle_databases) that evicts non-default, non-active, non-busy databases past the idle window; evicted databases reopen transparently on their next request. remove() now tears down compute as well as the registry entry. LocalAgentServiceImpl gains a shutdown CancellationToken that stops its event watcher promptly when the database is closed. main.rs spawns the reaper at startup and, on shutdown, drains all databases then releases the shared GPU context exactly once (replacing the per-default drain_gpu). The idle window is overridable via NODESPACE_DB_IDLE_SECS. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Cover idle eviction of an idle non-default database and its transparent reopen, the default and active databases never being evicted, shutdown_all closing every open database, and per-database routing keeping ai-chat nodes from leaking across databases. Active-first embedding priority is proven by the EmbeddingScheduler unit test in nodespace-core, which needs no model. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The active-first unit test registered the active batch before the deferred non-active one, so a plain FIFO gate would produce the same ordering — it did not distinguish priority from FIFO. Rewrite it so a non-active batch queues before a second active batch and assert the active batch is still granted first. Guard the high_waiters increment with an RAII reservation so an acquire future cancelled while awaiting the gate releases the count instead of leaking it (a leak would defer every non-active database forever). Treat a database with no recorded activity as freshly used, not idle, so the idle reaper cannot evict one in the sub-window between opening it and recording its first activity. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
APPROVE Reviewed independently (my own read plus an adversarial pass focused on the new concurrency primitive) with special scrutiny on the four highest-risk areas:
Follow-up fixes applied from the review (commit Validated green locally before pushing: full |
|
APPROVE — I independently ground-verified the two hardest parts (scheduler concurrency + eviction guards); both are correct and defensively coded. This closes #1534 cleanly. EmbeddingScheduler — lost-wakeup-safe, leak-free, no permanent starvationThe non-active defer loop is the textbook tokio pattern and it's exactly right: loop {
let notified = self.notify.notified();
tokio::pin!(notified);
notified.as_mut().enable(); // register BEFORE the check
if self.high_waiters.load(SeqCst) == 0 { break; }
notified.await;
}
Idle reaper — every dangerous eviction is guarded
Model/lifecycle scoping is correctOne process-global embedding model (published over Community-unaffectedSingle database is always effectively active and never evicted (default + active guards both apply to it) — behavior unchanged. ✅ Merge gate
|
Summary
ADR-053 §1/§4 compute follow-through: with N databases open in one daemon, the store-bound background machinery is now scoped per database while sharing the single loaded embedding model.
Most per-database scoping already landed with the ADR-053 daemon core — each open database already has its own
EmbeddingProcessor(draining its own store's stale queue), its ownLocalAgentServiceImplevent watcher (subscribed to that database's event bus, filtered toai-chat), its ownGraphContextAssembler, and its ownAgentSessionHandlerover the sharedPtySessionManager; the embedding model is process-global (published once over awatchchannel, shared text-keyed LRU). This change adds the remaining lifecycle and priority behavior.What's new
EmbeddingScheduler(innodespace-core, keyed on the database ULID as a plainStringso core stays free of any daemon type) that all per-databaseEmbeddingProcessors share. Each processor acquires a permit around every batch; while the active database has batches queued, non-active databases defer at batch granularity, so a background bulk import into B cannot starve foreground semantic-search freshness in A. The active-database signal is the liveWatchNodesstream — the desktop already opens exactly one on the database it displays and re-subscribes on switch, so no protocol change is needed. A non-active database is never permanently starved: it resumes the moment the active database goes idle, and with no active database (None) every database runs immediately.DatabaseManager::close(id)drops only that database'sEmbeddingProcessorand cancels its agent event watcher (a newCancellationTokenonLocalAgentServiceImpl). It never callsrelease_gpu_context()/shutdown()on the shared nlp-engine model — that is one-way and global, reserved for daemon shutdown.shutdown_all), then releases the shared GPU context exactly once.NODESPACE_DB_IDLE_SECS) local-only databases that have no pending embeddings, releasing their store connection; the next request reopens transparently via the existing lazyget_or_open.Acceptance criteria
EmbeddingScheduler+ a unit test that distinguishes active-first from a plain FIFO gate (a non-active batch queued before a later active batch is still granted second).per_db_computetest.close+get_or_openrebuild; covered by tests.bun run test:allgreen;cargo fmt+cargo clippy --workspace -- -D warningsclean.Non-goals (unchanged)
Per-database fairness beyond simple active-first; sync residency pinning (the sync issue pins the active synced database). Community single-database behavior is unchanged — one open database, always effectively active, never evicted.
Review
Independently reviewed (adversarial pass over the scheduler concurrency, GPU-teardown safety, eviction guards, and watcher cancellation) — no deadlock, no lost-wakeup, no permanent starvation, teardown once-only-on-shutdown. Follow-up fixes from that review: strengthened the active-first unit test to genuinely distinguish priority from FIFO, RAII-guarded the active-waiter count so a cancelled acquire can't leak it, and made the reaper treat a not-yet-recorded database as freshly used rather than idle.
Closes #1534.