Caller holds application->mutex. */
| 453 | |
| 454 | /* Caller holds application->mutex. */ |
| 455 | static void application_refresh_watch_locked(cbm_daemon_application_session_t *session) { |
| 456 | cbm_daemon_application_t *application = session->application; |
| 457 | if (!application->watcher || !session->context_set || |
| 458 | session->tool_profile != CBM_MCP_TOOL_PROFILE_ALL || session->hook_event || |
| 459 | session->hook_dialect || session->auto_index_subscribed) { |
| 460 | return; |
| 461 | } |
| 462 | const char *project = cbm_mcp_server_session_project(session->mcp); |
| 463 | const char *root = cbm_mcp_server_session_root(session->mcp); |
| 464 | if (!project || !project[0] || !root || !root[0]) { |
| 465 | return; |
| 466 | } |
| 467 | if (!application_session_workspace_allowed(session, "watch")) { |
| 468 | application_release_session_watch_locked(session); |
| 469 | return; |
| 470 | } |
| 471 | bool enabled = !application->config || |
| 472 | cbm_config_get_bool(application->config, CBM_CONFIG_AUTO_WATCH, true); |
| 473 | bool db_exists = application_regular_db_exists(project); |
| 474 | |
| 475 | /* Disconnect cancellation is the logical ownership boundary. An |
| 476 | * in-flight request may reach this refresh after cancel returned but |
| 477 | * before runtime can join it and call session_close; it must never |
| 478 | * recreate that session's watch. */ |
| 479 | if (application->stopping || application_request_cancelled_locked(session)) { |
| 480 | return; |
| 481 | } |
| 482 | cbm_daemon_application_watch_t *watch = application_find_watch_locked(application, project); |
| 483 | if (!enabled || !db_exists) { |
| 484 | /* A delete/config transition is global for this project. Remove the |
| 485 | * physical watch and clear every logical subscriber in one step. */ |
| 486 | if (watch) { |
| 487 | application_remove_watch_locked(application, watch); |
| 488 | } |
| 489 | return; |
| 490 | } |
| 491 | if (session->watch) { |
| 492 | return; |
| 493 | } |
| 494 | if (watch) { |
| 495 | if (strcmp(watch->root, root) == 0) { |
| 496 | if (!application_watch_job_subscribe_late_session_locked(session, watch)) { |
| 497 | cbm_log_warn("daemon.watch.late_owner_allocation_failed", "project", project, |
| 498 | "action", "retry"); |
| 499 | return; |
| 500 | } |
| 501 | watch->subscribers++; |
| 502 | session->watch = watch; |
| 503 | } else { |
| 504 | cbm_log_warn("daemon.watch.project_collision", "project", project, "existing_root", |
| 505 | watch->root, "requested_root", root); |
| 506 | } |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | watch = calloc(1, sizeof(*watch)); |
| 511 | if (watch) { |
| 512 | watch->project = strdup(project); |
no test coverage detected