@@ -147,6 +147,8 @@ struct cbm_daemon_application_job {
147147 bool cancelled ;
148148 bool cancel_requested ;
149149 bool supervision_failed ;
150+ bool attempt_recorded ;
151+ char attempt_id [33 ];
150152 cbm_daemon_application_job_t * next ;
151153};
152154
@@ -1224,6 +1226,63 @@ static void application_record_cancelled(cbm_index_worker_result_t *last_result,
12241226 last_log [0 ] = '\0' ;
12251227}
12261228
1229+ static cbm_index_resource_t application_resource_from_name (const char * name ) {
1230+ for (int resource = CBM_INDEX_RESOURCE_FILES ;
1231+ resource <= CBM_INDEX_RESOURCE_DISCOVERY_DURATION_MS ; resource ++ ) {
1232+ if (name && strcmp (name , cbm_index_resource_name ((cbm_index_resource_t )resource )) == 0 ) {
1233+ return (cbm_index_resource_t )resource ;
1234+ }
1235+ }
1236+ return CBM_INDEX_RESOURCE_NONE ;
1237+ }
1238+
1239+ static bool application_response_error_metadata (const char * response , char * code , size_t code_size ,
1240+ cbm_index_resource_violation_t * violation ) {
1241+ if (code && code_size > 0 ) {
1242+ code [0 ] = '\0' ;
1243+ }
1244+ if (violation ) {
1245+ * violation = (cbm_index_resource_violation_t ){0 };
1246+ }
1247+ yyjson_doc * outer = response ? yyjson_read (response , strlen (response ), 0 ) : NULL ;
1248+ yyjson_val * outer_root = outer ? yyjson_doc_get_root (outer ) : NULL ;
1249+ yyjson_val * is_error =
1250+ outer_root && yyjson_is_obj (outer_root ) ? yyjson_obj_get (outer_root , "isError" ) : NULL ;
1251+ yyjson_val * content =
1252+ outer_root && yyjson_is_obj (outer_root ) ? yyjson_obj_get (outer_root , "content" ) : NULL ;
1253+ yyjson_val * first = content && yyjson_is_arr (content ) ? yyjson_arr_get_first (content ) : NULL ;
1254+ yyjson_val * text = first && yyjson_is_obj (first ) ? yyjson_obj_get (first , "text" ) : NULL ;
1255+ yyjson_doc * inner = text && yyjson_is_str (text )
1256+ ? yyjson_read (yyjson_get_str (text ), yyjson_get_len (text ), 0 )
1257+ : NULL ;
1258+ yyjson_val * inner_root = inner ? yyjson_doc_get_root (inner ) : NULL ;
1259+ yyjson_val * inner_status =
1260+ inner_root && yyjson_is_obj (inner_root ) ? yyjson_obj_get (inner_root , "status" ) : NULL ;
1261+ bool failed = (is_error && yyjson_is_bool (is_error ) && yyjson_get_bool (is_error )) ||
1262+ (inner_status && yyjson_is_str (inner_status ) &&
1263+ strcmp (yyjson_get_str (inner_status ), "error" ) == 0 );
1264+ if (failed && inner_root && yyjson_is_obj (inner_root )) {
1265+ yyjson_val * failure_code = yyjson_obj_get (inner_root , "code" );
1266+ if (code && code_size > 0 && failure_code && yyjson_is_str (failure_code )) {
1267+ (void )snprintf (code , code_size , "%s" , yyjson_get_str (failure_code ));
1268+ }
1269+ yyjson_val * resource = yyjson_obj_get (inner_root , "resource" );
1270+ yyjson_val * observed = yyjson_obj_get (inner_root , "observed" );
1271+ yyjson_val * limit = yyjson_obj_get (inner_root , "limit" );
1272+ if (violation && resource && yyjson_is_str (resource )) {
1273+ violation -> resource = application_resource_from_name (yyjson_get_str (resource ));
1274+ violation -> probe_failed = code && strcmp (code , "resource_probe_failed" ) == 0 ;
1275+ if (observed && yyjson_is_uint (observed ) && limit && yyjson_is_uint (limit )) {
1276+ violation -> observed = yyjson_get_uint (observed );
1277+ violation -> limit = yyjson_get_uint (limit );
1278+ }
1279+ }
1280+ }
1281+ yyjson_doc_free (inner );
1282+ yyjson_doc_free (outer );
1283+ return failed ;
1284+ }
1285+
12271286static bool application_result_is_attributable_failure (
12281287 const application_attempt_t * attempt , cbm_mcp_supervised_result_disposition_t disposition ) {
12291288 return disposition == CBM_MCP_SUPERVISED_RESULT_CONTAINED_FAILURE && attempt -> has_result &&
@@ -1239,6 +1298,7 @@ typedef enum {
12391298typedef struct {
12401299 char * response ;
12411300 bool successful ;
1301+ bool response_is_error ;
12421302 bool unsafe_terminal ;
12431303 bool supervision_failed ;
12441304 cbm_index_worker_result_t last_result ;
@@ -1268,12 +1328,17 @@ static application_attempt_decision_t application_consume_attempt(
12681328 execution -> response = attempt -> result .response ;
12691329 attempt -> result .response = NULL ;
12701330 execution -> successful = execution -> response != NULL ;
1331+ execution -> response_is_error =
1332+ execution -> response &&
1333+ application_response_error_metadata (execution -> response , NULL , 0 , NULL );
12711334 application_attempt_free (attempt );
12721335 return APPLICATION_ATTEMPT_DECISION_SUCCESS ;
12731336 }
12741337 if (disposition == CBM_MCP_SUPERVISED_RESULT_RESOURCE_FAILURE ) {
12751338 execution -> response =
12761339 cbm_mcp_index_worker_resource_response (job -> args_json , & attempt -> result );
1340+ execution -> successful = execution -> response != NULL ;
1341+ execution -> response_is_error = execution -> response != NULL ;
12771342 application_attempt_free (attempt );
12781343 return APPLICATION_ATTEMPT_DECISION_STOP ;
12791344 }
@@ -1478,6 +1543,46 @@ static void application_job_publish(cbm_daemon_application_job_t *job,
14781543 execution -> have_last_result ? & execution -> last_result : NULL ,
14791544 execution -> last_log [0 ] ? execution -> last_log : NULL );
14801545 }
1546+ if (job -> attempt_recorded ) {
1547+ bool cancelled =
1548+ execution -> have_last_result && execution -> last_result .cancellation_requested ;
1549+ bool attempt_completed = execution -> successful && !execution -> response_is_error ;
1550+ const char * state = attempt_completed ? "completed" : cancelled ? "cancelled" : "failed" ;
1551+ const char * failure_code = NULL ;
1552+ const cbm_index_resource_violation_t * violation = NULL ;
1553+ char response_failure_code [CBM_SZ_128 ] = {0 };
1554+ cbm_index_resource_violation_t response_violation = {0 };
1555+ if (!attempt_completed && !cancelled ) {
1556+ if (application_response_error_metadata (execution -> response , response_failure_code ,
1557+ sizeof (response_failure_code ),
1558+ & response_violation )) {
1559+ failure_code = response_failure_code [0 ] ? response_failure_code : "index_failed" ;
1560+ if (response_violation .resource != CBM_INDEX_RESOURCE_NONE ) {
1561+ violation = & response_violation ;
1562+ }
1563+ } else if (execution -> have_last_result &&
1564+ execution -> last_result .resource_violation .resource !=
1565+ CBM_INDEX_RESOURCE_NONE ) {
1566+ violation = & execution -> last_result .resource_violation ;
1567+ failure_code =
1568+ violation -> probe_failed ? "resource_probe_failed" : "resource_limit_exceeded" ;
1569+ } else if (execution -> supervision_failed || execution -> unsafe_terminal ) {
1570+ failure_code = "supervision_failed" ;
1571+ } else if (!execution -> have_last_result ||
1572+ execution -> last_result .outcome == CBM_PROC_SPAWN_FAILED ) {
1573+ failure_code = "worker_start_failed" ;
1574+ } else {
1575+ failure_code = "index_failed" ;
1576+ }
1577+ } else if (cancelled ) {
1578+ failure_code = "cancelled" ;
1579+ }
1580+ if (!cbm_mcp_index_attempt_transition (job -> project_key , job -> root_path , job -> attempt_id ,
1581+ state , failure_code , violation , attempt_completed )) {
1582+ cbm_log_warn ("daemon.index.attempt_record" , "project" , job -> project_key , "state" ,
1583+ "terminal_write_failed" );
1584+ }
1585+ }
14811586 cbm_daemon_application_t * application = job -> application ;
14821587 cbm_mutex_lock (& application -> mutex );
14831588 job -> response = execution -> response ;
@@ -1525,6 +1630,12 @@ static void *application_job_thread(void *opaque) {
15251630 if (!application_job_wait_for_mutations (job )) {
15261631 application_job_execution_cancel (& execution );
15271632 } else {
1633+ if (job -> attempt_recorded &&
1634+ !cbm_mcp_index_attempt_transition (job -> project_key , job -> root_path , job -> attempt_id ,
1635+ "running" , NULL , NULL , false)) {
1636+ cbm_log_warn ("daemon.index.attempt_record" , "project" , job -> project_key , "state" ,
1637+ "running_write_failed" );
1638+ }
15281639 application_attempt_t attempt ;
15291640 application_attempt_status_t status =
15301641 application_job_run_attempt (job , NULL , NULL , & attempt );
@@ -1573,6 +1684,18 @@ static char *application_index_project_key(const char *root_path, const char *ar
15731684 return key ;
15741685}
15751686
1687+ static const char * application_index_origin (const char * args_json , char * * owned_origin ) {
1688+ * owned_origin = cbm_mcp_get_string_arg (args_json , "_cbm_index_origin" );
1689+ if (* owned_origin &&
1690+ (strcmp (* owned_origin , "auto" ) == 0 || strcmp (* owned_origin , "watcher" ) == 0 ||
1691+ strcmp (* owned_origin , "explicit" ) == 0 )) {
1692+ return * owned_origin ;
1693+ }
1694+ free (* owned_origin );
1695+ * owned_origin = NULL ;
1696+ return "explicit" ;
1697+ }
1698+
15761699static size_t application_active_job_count_locked (cbm_daemon_application_t * application ) {
15771700 size_t count = 0 ;
15781701 for (cbm_daemon_application_job_t * job = application -> jobs ; job ; job = job -> next ) {
@@ -1604,6 +1727,7 @@ static bool application_index_args_normalize_defaults(yyjson_mut_val *root) {
16041727 if (name && (!yyjson_mut_is_str (name ) || yyjson_mut_get_len (name ) == 0 )) {
16051728 (void )yyjson_mut_obj_remove_key (root , "name" );
16061729 }
1730+ (void )yyjson_mut_obj_remove_key (root , "_cbm_index_origin" );
16071731 return true;
16081732}
16091733
@@ -1669,6 +1793,9 @@ static cbm_daemon_application_job_t *application_job_subscribe_locked(
16691793 if (application -> stopping ) {
16701794 return NULL ;
16711795 }
1796+ char * requested_origin = cbm_mcp_get_string_arg (args_json , "_cbm_index_origin" );
1797+ bool watcher_request = requested_origin && strcmp (requested_origin , "watcher" ) == 0 ;
1798+ free (requested_origin );
16721799 cbm_daemon_application_job_t * job =
16731800 application_find_active_job_locked (application , project_key );
16741801 if (job ) {
@@ -1681,6 +1808,9 @@ static cbm_daemon_application_job_t *application_job_subscribe_locked(
16811808 return NULL ;
16821809 }
16831810 job -> subscribers ++ ;
1811+ if (watcher_request && job -> attempt_recorded ) {
1812+ (void )cbm_mcp_index_attempt_mark_watcher_change (job -> project_key , job -> attempt_id );
1813+ }
16841814 * status_out = APPLICATION_JOB_SUBSCRIBE_OK ;
16851815 return job ;
16861816 }
@@ -1706,6 +1836,20 @@ static cbm_daemon_application_job_t *application_job_subscribe_locked(
17061836 }
17071837 job -> application = application ;
17081838 job -> subscribers = 1 ;
1839+ cbm_index_resource_policy_t policy ;
1840+ char policy_error [CBM_SZ_256 ] = {0 };
1841+ char * owned_origin = NULL ;
1842+ const char * origin = application_index_origin (args_json , & owned_origin );
1843+ if (cbm_mcp_index_policy_from_internal_args (args_json , & policy , policy_error ,
1844+ sizeof (policy_error ))) {
1845+ job -> attempt_recorded =
1846+ cbm_mcp_index_attempt_begin (project_key , root_path , origin , & policy ,
1847+ strcmp (origin , "watcher" ) == 0 , job -> attempt_id );
1848+ }
1849+ free (owned_origin );
1850+ if (!job -> attempt_recorded ) {
1851+ cbm_log_warn ("daemon.index.attempt_record" , "project" , project_key , "state" , "unavailable" );
1852+ }
17091853 job -> next = application -> jobs ;
17101854 application -> jobs = job ;
17111855 if (application_job_thread_create (& job -> thread , job ) == 0 ) {
@@ -1716,6 +1860,11 @@ static cbm_daemon_application_job_t *application_job_subscribe_locked(
17161860 * reservation back synchronously and let background callers retry. */
17171861 application -> jobs = job -> next ;
17181862 job -> next = NULL ;
1863+ if (job -> attempt_recorded ) {
1864+ (void )cbm_mcp_index_attempt_transition (job -> project_key , job -> root_path ,
1865+ job -> attempt_id , "failed" , "worker_start_failed" ,
1866+ NULL , false);
1867+ }
17191868 application_job_free (job );
17201869 * status_out = APPLICATION_JOB_SUBSCRIBE_UNAVAILABLE ;
17211870 cbm_log_warn ("daemon.index.thread_start_failed" , "action" , "retry" );
@@ -2035,6 +2184,7 @@ static char *application_auto_index_args(cbm_daemon_application_t *application,
20352184 }
20362185 yyjson_mut_doc_set_root (document , root );
20372186 char * args = yyjson_mut_obj_add_strcpy (document , root , "repo_path" , root_path ) &&
2187+ yyjson_mut_obj_add_str (document , root , "_cbm_index_origin" , "auto" ) &&
20382188 application_index_args_add_policy (application , document , root )
20392189 ? yyjson_mut_write (document , 0 , NULL )
20402190 : NULL ;
@@ -3065,6 +3215,7 @@ cbm_daemon_application_t *cbm_daemon_application_new(
30653215 application -> watcher , application_watcher_mutation_begin ,
30663216 application_watcher_mutation_end , application_watcher_project_pruned , application );
30673217 }
3218+ cbm_mcp_index_attempt_recover_abandoned ();
30683219 return application ;
30693220}
30703221
@@ -3488,6 +3639,8 @@ static int application_background_index(cbm_daemon_application_t *application,
34883639 }
34893640 yyjson_mut_doc_set_root (document , root );
34903641 bool encoded = yyjson_mut_obj_add_strcpy (document , root , "repo_path" , canonical_root ) &&
3642+ yyjson_mut_obj_add_str (document , root , "_cbm_index_origin" ,
3643+ require_live_watch ? "watcher" : "explicit" ) &&
34913644 application_index_args_add_policy (application , document , root );
34923645 char * default_project = cbm_project_name_from_path (canonical_root );
34933646 bool custom_project =
0 commit comments