Caller holds application->mutex. */
| 431 | |
| 432 | /* Caller holds application->mutex. */ |
| 433 | static void application_refresh_watch_locked(cbm_daemon_application_session_t *session) { |
| 434 | cbm_daemon_application_t *application = session->application; |
| 435 | if (!application->watcher || !session->context_set || |
| 436 | session->tool_profile != CBM_MCP_TOOL_PROFILE_ALL || session->hook_event || |
| 437 | session->hook_dialect || session->auto_index_subscribed) { |
| 438 | return; |
| 439 | } |
| 440 | const char *project = cbm_mcp_server_session_project(session->mcp); |
| 441 | const char *root = cbm_mcp_server_session_root(session->mcp); |
| 442 | if (!project || !project[0] || !root || !root[0]) { |
| 443 | return; |
| 444 | } |
| 445 | bool enabled = !application->config || |
| 446 | cbm_config_get_bool(application->config, CBM_CONFIG_AUTO_WATCH, true); |
| 447 | bool db_exists = application_regular_db_exists(project); |
| 448 | |
| 449 | /* Disconnect cancellation is the logical ownership boundary. An |
| 450 | * in-flight request may reach this refresh after cancel returned but |
| 451 | * before runtime can join it and call session_close; it must never |
| 452 | * recreate that session's watch. */ |
| 453 | if (application->stopping || application_request_cancelled_locked(session)) { |
| 454 | return; |
| 455 | } |
| 456 | cbm_daemon_application_watch_t *watch = application_find_watch_locked(application, project); |
| 457 | if (!enabled || !db_exists) { |
| 458 | /* A delete/config transition is global for this project. Remove the |
| 459 | * physical watch and clear every logical subscriber in one step. */ |
| 460 | if (watch) { |
| 461 | application_remove_watch_locked(application, watch); |
| 462 | } |
| 463 | return; |
| 464 | } |
| 465 | if (session->watch) { |
| 466 | return; |
| 467 | } |
| 468 | if (watch) { |
| 469 | if (strcmp(watch->root, root) == 0) { |
| 470 | if (!application_watch_job_subscribe_late_session_locked(session, watch)) { |
| 471 | cbm_log_warn("daemon.watch.late_owner_allocation_failed", "project", project, |
| 472 | "action", "retry"); |
| 473 | return; |
| 474 | } |
| 475 | watch->subscribers++; |
| 476 | session->watch = watch; |
| 477 | } else { |
| 478 | cbm_log_warn("daemon.watch.project_collision", "project", project, "existing_root", |
| 479 | watch->root, "requested_root", root); |
| 480 | } |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | watch = calloc(1, sizeof(*watch)); |
| 485 | if (watch) { |
| 486 | watch->project = strdup(project); |
| 487 | watch->root = strdup(root); |
| 488 | } |
| 489 | if (!watch || !watch->project || !watch->root) { |
| 490 | if (watch) { |
no test coverage detected