@@ -160,6 +160,8 @@ struct cbm_daemon_application_job {
160160 bool async_owned ;
161161 bool worker_started ;
162162 uint64_t request_started_ms ;
163+ bool attempt_recorded ;
164+ char attempt_id [33 ];
163165 cbm_daemon_application_job_t * next ;
164166};
165167
@@ -1520,6 +1522,63 @@ static void application_record_cancelled(cbm_index_worker_result_t *last_result,
15201522 last_log [0 ] = '\0' ;
15211523}
15221524
1525+ static cbm_index_resource_t application_resource_from_name (const char * name ) {
1526+ for (int resource = CBM_INDEX_RESOURCE_FILES ;
1527+ resource <= CBM_INDEX_RESOURCE_DISCOVERY_DURATION_MS ; resource ++ ) {
1528+ if (name && strcmp (name , cbm_index_resource_name ((cbm_index_resource_t )resource )) == 0 ) {
1529+ return (cbm_index_resource_t )resource ;
1530+ }
1531+ }
1532+ return CBM_INDEX_RESOURCE_NONE ;
1533+ }
1534+
1535+ static bool application_response_error_metadata (const char * response , char * code , size_t code_size ,
1536+ cbm_index_resource_violation_t * violation ) {
1537+ if (code && code_size > 0 ) {
1538+ code [0 ] = '\0' ;
1539+ }
1540+ if (violation ) {
1541+ * violation = (cbm_index_resource_violation_t ){0 };
1542+ }
1543+ yyjson_doc * outer = response ? yyjson_read (response , strlen (response ), 0 ) : NULL ;
1544+ yyjson_val * outer_root = outer ? yyjson_doc_get_root (outer ) : NULL ;
1545+ yyjson_val * is_error =
1546+ outer_root && yyjson_is_obj (outer_root ) ? yyjson_obj_get (outer_root , "isError" ) : NULL ;
1547+ yyjson_val * content =
1548+ outer_root && yyjson_is_obj (outer_root ) ? yyjson_obj_get (outer_root , "content" ) : NULL ;
1549+ yyjson_val * first = content && yyjson_is_arr (content ) ? yyjson_arr_get_first (content ) : NULL ;
1550+ yyjson_val * text = first && yyjson_is_obj (first ) ? yyjson_obj_get (first , "text" ) : NULL ;
1551+ yyjson_doc * inner = text && yyjson_is_str (text )
1552+ ? yyjson_read (yyjson_get_str (text ), yyjson_get_len (text ), 0 )
1553+ : NULL ;
1554+ yyjson_val * inner_root = inner ? yyjson_doc_get_root (inner ) : NULL ;
1555+ yyjson_val * inner_status =
1556+ inner_root && yyjson_is_obj (inner_root ) ? yyjson_obj_get (inner_root , "status" ) : NULL ;
1557+ bool failed = (is_error && yyjson_is_bool (is_error ) && yyjson_get_bool (is_error )) ||
1558+ (inner_status && yyjson_is_str (inner_status ) &&
1559+ strcmp (yyjson_get_str (inner_status ), "error" ) == 0 );
1560+ if (failed && inner_root && yyjson_is_obj (inner_root )) {
1561+ yyjson_val * failure_code = yyjson_obj_get (inner_root , "code" );
1562+ if (code && code_size > 0 && failure_code && yyjson_is_str (failure_code )) {
1563+ (void )snprintf (code , code_size , "%s" , yyjson_get_str (failure_code ));
1564+ }
1565+ yyjson_val * resource = yyjson_obj_get (inner_root , "resource" );
1566+ yyjson_val * observed = yyjson_obj_get (inner_root , "observed" );
1567+ yyjson_val * limit = yyjson_obj_get (inner_root , "limit" );
1568+ if (violation && resource && yyjson_is_str (resource )) {
1569+ violation -> resource = application_resource_from_name (yyjson_get_str (resource ));
1570+ violation -> probe_failed = code && strcmp (code , "resource_probe_failed" ) == 0 ;
1571+ if (observed && yyjson_is_uint (observed ) && limit && yyjson_is_uint (limit )) {
1572+ violation -> observed = yyjson_get_uint (observed );
1573+ violation -> limit = yyjson_get_uint (limit );
1574+ }
1575+ }
1576+ }
1577+ yyjson_doc_free (inner );
1578+ yyjson_doc_free (outer );
1579+ return failed ;
1580+ }
1581+
15231582static bool application_result_is_attributable_failure (
15241583 const application_attempt_t * attempt , cbm_mcp_supervised_result_disposition_t disposition ) {
15251584 return disposition == CBM_MCP_SUPERVISED_RESULT_CONTAINED_FAILURE && attempt -> has_result &&
@@ -1535,6 +1594,7 @@ typedef enum {
15351594typedef struct {
15361595 char * response ;
15371596 bool successful ;
1597+ bool response_is_error ;
15381598 bool unsafe_terminal ;
15391599 bool supervision_failed ;
15401600 cbm_index_worker_result_t last_result ;
@@ -1581,12 +1641,17 @@ static application_attempt_decision_t application_consume_attempt(
15811641 execution -> response = attempt -> result .response ;
15821642 attempt -> result .response = NULL ;
15831643 execution -> successful = application_index_response_succeeded (execution -> response );
1644+ execution -> response_is_error =
1645+ execution -> response &&
1646+ application_response_error_metadata (execution -> response , NULL , 0 , NULL );
15841647 application_attempt_free (attempt );
15851648 return APPLICATION_ATTEMPT_DECISION_SUCCESS ;
15861649 }
15871650 if (disposition == CBM_MCP_SUPERVISED_RESULT_RESOURCE_FAILURE ) {
15881651 execution -> response =
15891652 cbm_mcp_index_worker_resource_response (job -> args_json , & attempt -> result );
1653+ execution -> successful = execution -> response != NULL ;
1654+ execution -> response_is_error = execution -> response != NULL ;
15901655 application_attempt_free (attempt );
15911656 return APPLICATION_ATTEMPT_DECISION_STOP ;
15921657 }
@@ -1800,6 +1865,46 @@ static void application_job_publish(cbm_daemon_application_job_t *job,
18001865 : cancelled ? "cancelled"
18011866 : "failed" ,
18021867 NULL );
1868+ if (job -> attempt_recorded ) {
1869+ bool cancelled =
1870+ execution -> have_last_result && execution -> last_result .cancellation_requested ;
1871+ bool attempt_completed = execution -> successful && !execution -> response_is_error ;
1872+ const char * state = attempt_completed ? "completed" : cancelled ? "cancelled" : "failed" ;
1873+ const char * failure_code = NULL ;
1874+ const cbm_index_resource_violation_t * violation = NULL ;
1875+ char response_failure_code [CBM_SZ_128 ] = {0 };
1876+ cbm_index_resource_violation_t response_violation = {0 };
1877+ if (!attempt_completed && !cancelled ) {
1878+ if (application_response_error_metadata (execution -> response , response_failure_code ,
1879+ sizeof (response_failure_code ),
1880+ & response_violation )) {
1881+ failure_code = response_failure_code [0 ] ? response_failure_code : "index_failed" ;
1882+ if (response_violation .resource != CBM_INDEX_RESOURCE_NONE ) {
1883+ violation = & response_violation ;
1884+ }
1885+ } else if (execution -> have_last_result &&
1886+ execution -> last_result .resource_violation .resource !=
1887+ CBM_INDEX_RESOURCE_NONE ) {
1888+ violation = & execution -> last_result .resource_violation ;
1889+ failure_code =
1890+ violation -> probe_failed ? "resource_probe_failed" : "resource_limit_exceeded" ;
1891+ } else if (execution -> supervision_failed || execution -> unsafe_terminal ) {
1892+ failure_code = "supervision_failed" ;
1893+ } else if (!execution -> have_last_result ||
1894+ execution -> last_result .outcome == CBM_PROC_SPAWN_FAILED ) {
1895+ failure_code = "worker_start_failed" ;
1896+ } else {
1897+ failure_code = "index_failed" ;
1898+ }
1899+ } else if (cancelled ) {
1900+ failure_code = "cancelled" ;
1901+ }
1902+ if (!cbm_mcp_index_attempt_transition (job -> project_key , job -> root_path , job -> attempt_id ,
1903+ state , failure_code , violation , attempt_completed )) {
1904+ cbm_log_warn ("daemon.index.attempt_record" , "project" , job -> project_key , "state" ,
1905+ "terminal_write_failed" );
1906+ }
1907+ }
18031908 cbm_daemon_application_t * application = job -> application ;
18041909 cbm_mutex_lock (& application -> mutex );
18051910 job -> response = execution -> response ;
@@ -1854,6 +1959,12 @@ static void *application_job_thread(void *opaque) {
18541959 if (!application_job_wait_for_mutations (job )) {
18551960 application_job_execution_cancel (& execution );
18561961 } else {
1962+ if (job -> attempt_recorded &&
1963+ !cbm_mcp_index_attempt_transition (job -> project_key , job -> root_path , job -> attempt_id ,
1964+ "running" , NULL , NULL , false)) {
1965+ cbm_log_warn ("daemon.index.attempt_record" , "project" , job -> project_key , "state" ,
1966+ "running_write_failed" );
1967+ }
18571968 application_attempt_t attempt ;
18581969 application_attempt_status_t status =
18591970 application_job_run_attempt (job , NULL , NULL , & attempt );
@@ -1904,6 +2015,18 @@ static char *application_index_project_key(const char *root_path, const char *ar
19042015 return key ;
19052016}
19062017
2018+ static const char * application_index_origin (const char * args_json , char * * owned_origin ) {
2019+ * owned_origin = cbm_mcp_get_string_arg (args_json , "_cbm_index_origin" );
2020+ if (* owned_origin &&
2021+ (strcmp (* owned_origin , "auto" ) == 0 || strcmp (* owned_origin , "watcher" ) == 0 ||
2022+ strcmp (* owned_origin , "explicit" ) == 0 )) {
2023+ return * owned_origin ;
2024+ }
2025+ free (* owned_origin );
2026+ * owned_origin = NULL ;
2027+ return "explicit" ;
2028+ }
2029+
19072030static size_t application_active_job_count_locked (cbm_daemon_application_t * application ) {
19082031 size_t count = 0 ;
19092032 for (cbm_daemon_application_job_t * job = application -> jobs ; job ; job = job -> next ) {
@@ -1966,6 +2089,7 @@ static bool application_index_args_normalize_defaults(yyjson_mut_val *root) {
19662089 if (name && (!yyjson_mut_is_str (name ) || yyjson_mut_get_len (name ) == 0 )) {
19672090 (void )yyjson_mut_obj_remove_key (root , "name" );
19682091 }
2092+ (void )yyjson_mut_obj_remove_key (root , "_cbm_index_origin" );
19692093 return true;
19702094}
19712095
@@ -2031,6 +2155,9 @@ static cbm_daemon_application_job_t *application_job_subscribe_locked(
20312155 if (application -> stopping ) {
20322156 return NULL ;
20332157 }
2158+ char * requested_origin = cbm_mcp_get_string_arg (args_json , "_cbm_index_origin" );
2159+ bool watcher_request = requested_origin && strcmp (requested_origin , "watcher" ) == 0 ;
2160+ free (requested_origin );
20342161 cbm_daemon_application_job_t * job =
20352162 application_find_active_job_locked (application , project_key );
20362163 if (job ) {
@@ -2043,6 +2170,9 @@ static cbm_daemon_application_job_t *application_job_subscribe_locked(
20432170 return NULL ;
20442171 }
20452172 job -> subscribers ++ ;
2173+ if (watcher_request && job -> attempt_recorded ) {
2174+ (void )cbm_mcp_index_attempt_mark_watcher_change (job -> project_key , job -> attempt_id );
2175+ }
20462176 * status_out = APPLICATION_JOB_SUBSCRIBE_OK ;
20472177 return job ;
20482178 }
@@ -2068,6 +2198,20 @@ static cbm_daemon_application_job_t *application_job_subscribe_locked(
20682198 }
20692199 job -> application = application ;
20702200 job -> subscribers = 1 ;
2201+ cbm_index_resource_policy_t policy ;
2202+ char policy_error [CBM_SZ_256 ] = {0 };
2203+ char * owned_origin = NULL ;
2204+ const char * origin = application_index_origin (args_json , & owned_origin );
2205+ if (cbm_mcp_index_policy_from_internal_args (args_json , & policy , policy_error ,
2206+ sizeof (policy_error ))) {
2207+ job -> attempt_recorded =
2208+ cbm_mcp_index_attempt_begin (project_key , root_path , origin , & policy ,
2209+ strcmp (origin , "watcher" ) == 0 , job -> attempt_id );
2210+ }
2211+ free (owned_origin );
2212+ if (!job -> attempt_recorded ) {
2213+ cbm_log_warn ("daemon.index.attempt_record" , "project" , project_key , "state" , "unavailable" );
2214+ }
20712215 job -> next = application -> jobs ;
20722216 application -> jobs = job ;
20732217 if (application_job_thread_create (& job -> thread , job ) == 0 ) {
@@ -2080,6 +2224,11 @@ static cbm_daemon_application_job_t *application_job_subscribe_locked(
20802224 * reservation back synchronously and let background callers retry. */
20812225 application -> jobs = job -> next ;
20822226 job -> next = NULL ;
2227+ if (job -> attempt_recorded ) {
2228+ (void )cbm_mcp_index_attempt_transition (job -> project_key , job -> root_path ,
2229+ job -> attempt_id , "failed" , "worker_start_failed" ,
2230+ NULL , false);
2231+ }
20832232 application_job_free (job );
20842233 * status_out = APPLICATION_JOB_SUBSCRIBE_UNAVAILABLE ;
20852234 cbm_log_warn ("daemon.index.thread_start_failed" , "action" , "retry" );
@@ -2403,6 +2552,7 @@ static char *application_auto_index_args(cbm_daemon_application_t *application,
24032552 }
24042553 yyjson_mut_doc_set_root (document , root );
24052554 char * args = yyjson_mut_obj_add_strcpy (document , root , "repo_path" , root_path ) &&
2555+ yyjson_mut_obj_add_str (document , root , "_cbm_index_origin" , "auto" ) &&
24062556 application_index_args_add_policy (application , document , root )
24072557 ? yyjson_mut_write (document , 0 , NULL )
24082558 : NULL ;
@@ -3679,6 +3829,7 @@ cbm_daemon_application_t *cbm_daemon_application_new(
36793829 application -> watcher , application_watcher_mutation_begin ,
36803830 application_watcher_mutation_end , application_watcher_project_pruned , application );
36813831 }
3832+ cbm_mcp_index_attempt_recover_abandoned ();
36823833 return application ;
36833834}
36843835
@@ -4105,6 +4256,8 @@ static int application_background_index(cbm_daemon_application_t *application,
41054256 }
41064257 yyjson_mut_doc_set_root (document , root );
41074258 bool encoded = yyjson_mut_obj_add_strcpy (document , root , "repo_path" , canonical_root ) &&
4259+ yyjson_mut_obj_add_str (document , root , "_cbm_index_origin" ,
4260+ require_live_watch ? "watcher" : "explicit" ) &&
41084261 application_index_args_add_policy (application , document , root );
41094262 char * default_project = cbm_project_name_from_path (canonical_root );
41104263 bool custom_project =
0 commit comments