Caller holds application->mutex. */
| 532 | |
| 533 | /* Caller holds application->mutex. */ |
| 534 | static void application_refresh_watch_locked(cbm_daemon_application_session_t *session) { |
| 535 | cbm_daemon_application_t *application = session->application; |
| 536 | if (!application->watcher || !session->context_set || |
| 537 | session->tool_profile != CBM_MCP_TOOL_PROFILE_ALL || session->hook_event || |
| 538 | session->hook_dialect || session->auto_index_subscribed) { |
| 539 | return; |
| 540 | } |
| 541 | const char *project = cbm_mcp_server_session_project(session->mcp); |
| 542 | const char *root = cbm_mcp_server_session_root(session->mcp); |
| 543 | if (!project || !project[0] || !root || !root[0]) { |
| 544 | return; |
| 545 | } |
| 546 | if (!application_session_workspace_allowed(session, "watch")) { |
| 547 | application_release_session_watch_locked(session); |
| 548 | return; |
| 549 | } |
| 550 | bool enabled = !application->config || |
| 551 | cbm_config_get_bool(application->config, CBM_CONFIG_AUTO_WATCH, true); |
| 552 | bool db_exists = application_regular_db_exists(project); |
| 553 | |
| 554 | /* Disconnect cancellation is the logical ownership boundary. An |
| 555 | * in-flight request may reach this refresh after cancel returned but |
| 556 | * before runtime can join it and call session_close; it must never |
| 557 | * recreate that session's watch. */ |
| 558 | if (application->stopping || application_request_cancelled_locked(session)) { |
| 559 | return; |
| 560 | } |
| 561 | cbm_daemon_application_watch_t *watch = application_find_watch_locked(application, project); |
| 562 | if (!enabled || !db_exists) { |
| 563 | /* A delete/config transition is global for this project. Remove the |
| 564 | * physical watch and clear every logical subscriber in one step. */ |
| 565 | if (watch) { |
| 566 | application_remove_watch_locked(application, watch); |
| 567 | } |
| 568 | return; |
| 569 | } |
| 570 | if (session->watch) { |
| 571 | return; |
| 572 | } |
| 573 | if (watch) { |
| 574 | if (strcmp(watch->root, root) == 0) { |
| 575 | if (!application_watch_job_subscribe_late_session_locked(session, watch)) { |
| 576 | cbm_log_warn("daemon.watch.late_owner_allocation_failed", "project", project, |
| 577 | "action", "retry"); |
| 578 | return; |
| 579 | } |
| 580 | watch->subscribers++; |
| 581 | session->watch = watch; |
| 582 | } else { |
| 583 | cbm_log_warn("daemon.watch.project_collision", "project", project, "existing_root", |
| 584 | watch->root, "requested_root", root); |
| 585 | } |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | watch = calloc(1, sizeof(*watch)); |
| 590 | if (watch) { |
| 591 | watch->project = strdup(project); |
no test coverage detected