Skip to content

daemon: stop model-manager init failure from panicking the whole process - #2155

Merged
mstomar125 merged 2 commits into
mainfrom
worktree-issue-2043-model-manager-panic
Aug 19, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
worktree-issue-2043-model-manager-panic

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

LocalAgentServiceImpl::new used GgufModelManager::new().expect(...), turning a recoverable environmental failure ($HOME unset, an unwritable or file-occupied models directory) into a panic. LocalAgentServiceImpl::new is called from build_database_services, the per-database open path — under ADR-053 a daemon serves many databases, opened lazily on demand (and reopened after idle eviction), so this panic ran on every open, not just at startup. A single failing open took down the whole daemon process: every other open database, the sync service, and the gRPC server along with it.

Fix

model_manager is now Option<Arc<GgufModelManager>>. A failed init logs the error and degrades — the database still opens, with local GGUF model management (list/download/delete/load/unload/cancel/recommended) reporting Status::unavailable instead of failing the open. This mirrors the existing convention in assembly.rs/local_agent_service.rs for an optional per-database capability whose init failure shouldn't block the open: a missing NLP model already disables only the embedding wiring (SharedContext::has_model), not the database open. Chat turns and OpenAI-compatible models are unaffected — neither depends on the GGUF model manager.

Concretely:

  • list_models contributes an empty local catalog instead of failing outright (OpenAI-compat discovery is independent and still works).
  • download_model / delete_model / load_model / unload_model / cancel_model_download / recommended_model return Status::unavailable with a clear message.
  • The plain-GGUF branch of load_model_and_collect_events emits an error progress event instead of panicking.
  • Added LocalAgentServiceImpl::from_model_manager, a shared construction path used by both new() and tests, so the degraded shape can be exercised without touching $HOME or the real models directory.

Testing

  • gguf_model_manager_new_fails_cleanly_when_models_dir_path_is_a_file — proves the underlying failure (create_dir_all over an occupied path) is a real, non-panicking Err, using the existing GgufModelManager::with_dir test constructor.
  • model_management_rpcs_degrade_when_model_manager_init_failed — builds a LocalAgentServiceImpl with the model manager forced absent and asserts all six model-management RPCs return Status::unavailable (not a panic), and list_models still succeeds with an empty local catalog.
  • ai_chat_turn_still_works_when_model_manager_init_failed — proves the rest of the service (an ai-chat turn end to end) is unaffected in degraded mode.
  • cargo test -p nodespace-daemon: 151 unit tests + all integration suites pass.
  • cargo clippy -p nodespace-daemon --all-targets: clean (had to move a helper from Result<_, Status> to Option to satisfy clippy::result_large_err — Status is ~176 bytes and my helper's natural Ok side is a bare pointer, unlike the gRPC handlers themselves whose Response<T> Ok side keeps them under the lint's threshold).
  • Full-workspace cargo clippy --all-targets is blocked by a pre-existing, unrelated nodespace-app build failure (missing resources/skill/**/* glob target) — reproduced identically on origin/main with no changes, confirmed out of scope for this fix.

Closes #2043

…nager init failure

GgufModelManager::new() failing (e.g. $HOME unset, an unwritable or
file-occupied models directory) used to be an unwrap-via-expect in
LocalAgentServiceImpl::new. build_database_services runs on every
per-database open under ADR-053 (databases open lazily on demand and can
reopen after idle eviction), so that panic took down the whole daemon
process — every other open database, the sync service, and the gRPC
server — over a single database's model-manager failure.

model_manager is now Option<Arc<GgufModelManager>>: a failed init logs and
degrades this database's local GGUF model management (list/download/
delete/load/unload/cancel/recommended report UNAVAILABLE) instead of
failing the open, mirroring how a missing NLP model already disables only
the embedding wiring (SharedContext::has_model) rather than the database
open. Chat turns and OpenAI-compatible models are unaffected since neither
depends on the GGUF model manager.

Adds a shared from_model_manager construction path (used by both new()
and tests) and regression tests: one proving GgufModelManager::with_dir
fails cleanly (not a panic) when its directory path is occupied by a file,
and two exercising the degraded LocalAgentServiceImpl end to end — the six
model-management RPCs return Status::unavailable, and an ai-chat turn
still completes normally.

Co-Authored-By: Claude <[email protected]>
…s real failure path

The three original regression tests all constructed the degraded shape via
from_model_manager(..., None) directly, so none of them actually drove
GgufModelManager::new() to fail inside LocalAgentServiceImpl::new itself —
the exact match arm that replaced the original .expect(...). A revert of
that arm back to a panic would have passed all three tests unnoticed.

Adds local_agent_service_new_survives_a_real_model_manager_init_failure,
which points $HOME at a tempdir whose .nodespace/models path is occupied by
a file and calls the real constructor, asserting it degrades
(model_manager: None) rather than panicking. The $HOME mutation is bounded
to new()'s synchronous body (no .await in between) and a repo-wide grep
confirms no other test in this crate's unit-test binary reads $HOME, so it
does not race any other test.

Also corrects a doc comment that said degraded mode persists "until the
daemon is restarted" — ADR-053's idle-reaper eviction-then-reopen also
re-runs build_database_services and gets a fresh chance to succeed, without
a full restart.

Co-Authored-By: Claude <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

Self-approving (mstomar125) — GitHub blocks self-approval via the formal review API for the PR author, so recording it here per repo convention.

Adversarial review sub-agent pass completed: independently checked out the branch, diffed against origin/main, compiled, and re-ran the test suite; verified every model_manager call site (all six gRPC handlers + the GGUF branch of load_model_and_collect_events) is gated through the None-safe accessor with no remaining panic path, confirmed the from_model_manager refactor is behavior-preserving for the happy path, confirmed no other file in the repo assumes model_manager is infallibly present, and confirmed the "degrade" approach matches both the issue's own suggested fix and the existing has_model/embedding-wiring convention in this file. No blocking issues found.

One real gap flagged: none of the original three tests drove LocalAgentServiceImpl::new()'s own match arm (they injected the degraded state via the private from_model_manager constructor, so a revert of that arm back to a panic would have passed unnoticed). Addressed in the follow-up commit — local_agent_service_new_survives_a_real_model_manager_init_failure points $HOME at a tempdir with an occupied models path and calls the real constructor, asserting it degrades rather than panicking. Also corrected a doc comment (idle-eviction reopen also recovers under ADR-053, not just a full daemon restart).

Final independent re-verification on the pushed HEAD: cargo test -p nodespace-daemon (152 unit tests + all integration suites) and cargo clippy -p nodespace-daemon --all-targets both clean. Merging.

@mstomar125
mstomar125 merged commit d82c7e7 into main Aug 19, 2026
@mstomar125
mstomar125 deleted the worktree-issue-2043-model-manager-panic branch August 19, 2026 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Opening a database can panic the whole daemon: LocalAgentServiceImpl::new expects GgufModelManager to succeed

1 participant