@@ -789,6 +789,36 @@ static bool cli_first_nonspace_is_brace(const char *s) {
789789static char * main_local_cli_daemon_execute (const char * tool_name , const char * args_json ,
790790 bool quiet_requested );
791791
792+ /* A supervised worker runs in the DAEMON's environment, not the requesting
793+ * client's. The daemon admitted this request under the client's session policy
794+ * and re-executed us with the canonical repo_path in the args (both daemon
795+ * spawn paths rewrite it), so the request itself is the worker's workspace
796+ * scope: repository == session root == allowed root. Without this, a fresh
797+ * server fell back to the process-wide CBM_ALLOWED_ROOT inherited from whoever
798+ * started the daemon and refused every admitted session outside it. Returns
799+ * NULL once scoped, otherwise the reason to fail closed: a worker never indexes
800+ * under an ambient policy.
801+ *
802+ * A repository that cannot be canonicalized (it does not exist) can only have
803+ * been admitted by a session with no declared boundary, because containment
804+ * needs a real path. The worker mirrors that with an explicit unrestricted
805+ * policy - still never the environment fallback - so the pipeline reports the
806+ * missing repository as the tool error it always was, instead of the
807+ * supervisor misreading a refused worker as a crash. */
808+ static const char * main_index_worker_scope_request (cbm_mcp_server_t * srv , const char * args_json ) {
809+ char * repo_path = cbm_mcp_get_string_arg (args_json , "repo_path" );
810+ if (!repo_path || !repo_path [0 ]) {
811+ free (repo_path );
812+ return "request names no repo_path" ;
813+ }
814+ char canonical [MAIN_PATH_CAP ];
815+ bool exists = cbm_canonical_path (repo_path , canonical , sizeof (canonical )) != 0 ;
816+ const char * scope = exists ? canonical : repo_path ;
817+ bool scoped = cbm_mcp_server_set_session_context (srv , scope , exists ? scope : NULL );
818+ free (repo_path );
819+ return scoped ? NULL : "session context could not be installed" ;
820+ }
821+
792822static int run_cli (int argc , char * * argv , cbm_project_lock_manager_t * project_locks ,
793823 main_local_maintenance_context_t * maintenance_context ) {
794824 cbm_cli_output_flags_t output_flags ;
@@ -930,11 +960,15 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
930960 };
931961 bool maintenance_binding_failed = false;
932962 bool maintenance_cancelled = false;
963+ const char * worker_scope_refused = NULL ;
933964 if (!index_worker ) {
934965 result = main_local_cli_daemon_execute (tool_name , args_json , output_flags .quiet_requested );
935966 } else {
936967 srv = cbm_mcp_server_new (NULL );
937968 if (srv ) {
969+ worker_scope_refused = main_index_worker_scope_request (srv , args_json );
970+ }
971+ if (srv && !worker_scope_refused ) {
938972 /* The in-process worker is a standalone instance: it may not
939973 * launch MCP-session background tasks. It receives project_locks
940974 * from its own process-level coordination setup and therefore
@@ -947,8 +981,8 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
947981 main_local_cli_mutation_try_begin );
948982 }
949983 }
950- maintenance_binding_failed = srv && !maintenance_context ;
951- if (srv && maintenance_context ) {
984+ maintenance_binding_failed = srv && !worker_scope_refused && ! maintenance_context ;
985+ if (srv && ! worker_scope_refused && maintenance_context ) {
952986 main_local_maintenance_server_bind (maintenance_context , srv );
953987 result = cbm_mcp_handle_tool (srv , tool_name , args_json );
954988 /* Unbind under the same mutex used by cancellation before any
@@ -960,7 +994,10 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
960994 }
961995 }
962996 if (!result ) {
963- if (maintenance_binding_failed ) {
997+ if (worker_scope_refused ) {
998+ (void )fprintf (stderr , "error: request workspace scope invalid: %s\n" ,
999+ worker_scope_refused );
1000+ } else if (maintenance_binding_failed ) {
9641001 (void )fprintf (stderr ,
9651002 "error: local %s maintenance cancellation could not bind safely\n" ,
9661003 index_worker ? "worker" : "CLI" );
0 commit comments