@@ -8852,7 +8852,9 @@ def _get_delegation_function_repo_path() -> Optional[Path]:
88528852 repo_path = golden_repo_manager .get_actual_repo_path (function_repo_alias )
88538853 return Path (repo_path ) if repo_path else None
88548854 except Exception as e :
8855- logger .warning (f"Function repository '{ function_repo_alias } ' not found: { e } " )
8855+ logger .warning (
8856+ f"Function repository '{ function_repo_alias } ' not found: { e } "
8857+ )
88568858 return None
88578859
88588860 except Exception as e :
@@ -8974,7 +8976,9 @@ def _get_delegation_config():
89748976 return None
89758977
89768978
8977- def _validate_function_parameters (target_function , parameters : Dict [str , Any ]) -> Optional [str ]:
8979+ def _validate_function_parameters (
8980+ target_function , parameters : Dict [str , Any ]
8981+ ) -> Optional [str ]:
89788982 """
89798983 Validate required parameters are present.
89808984
@@ -8989,7 +8993,9 @@ def _validate_function_parameters(target_function, parameters: Dict[str, Any]) -
89898993 return None
89908994
89918995
8992- async def _ensure_repos_registered (client , required_repos : List [Dict [str , Any ]]) -> List [str ]:
8996+ async def _ensure_repos_registered (
8997+ client , required_repos : List [Dict [str , Any ]]
8998+ ) -> List [str ]:
89938999 """
89949000 Ensure required repositories are registered in Claude Server.
89959001
@@ -9064,8 +9070,14 @@ async def handle_execute_delegation_function(
90649070 repo_path = _get_delegation_function_repo_path ()
90659071 delegation_config = _get_delegation_config ()
90669072
9067- if repo_path is None or delegation_config is None or not delegation_config .is_configured :
9068- return _mcp_response ({"success" : False , "error" : "Claude Delegation not configured" })
9073+ if (
9074+ repo_path is None
9075+ or delegation_config is None
9076+ or not delegation_config .is_configured
9077+ ):
9078+ return _mcp_response (
9079+ {"success" : False , "error" : "Claude Delegation not configured" }
9080+ )
90699081
90709082 function_name = args .get ("function_name" , "" )
90719083 parameters = args .get ("parameters" , {})
@@ -9074,17 +9086,27 @@ async def handle_execute_delegation_function(
90749086 # Load and find function
90759087 loader = DelegationFunctionLoader ()
90769088 all_functions = loader .load_functions (repo_path )
9077- target_function = next ((f for f in all_functions if f .name == function_name ), None )
9089+ target_function = next (
9090+ (f for f in all_functions if f .name == function_name ), None
9091+ )
90789092
90799093 if target_function is None :
9080- return _mcp_response ({"success" : False , "error" : f"Function not found: { function_name } " })
9094+ return _mcp_response (
9095+ {"success" : False , "error" : f"Function not found: { function_name } " }
9096+ )
90819097
90829098 # Access validation
9083- effective_user = session_state .effective_user if session_state and session_state .is_impersonating else user
9099+ effective_user = (
9100+ session_state .effective_user
9101+ if session_state and session_state .is_impersonating
9102+ else user
9103+ )
90849104 user_groups = _get_user_groups (effective_user )
90859105
90869106 if not (user_groups & set (target_function .allowed_groups )):
9087- return _mcp_response ({"success" : False , "error" : "Access denied: insufficient permissions" })
9107+ return _mcp_response (
9108+ {"success" : False , "error" : "Access denied: insufficient permissions" }
9109+ )
90889110
90899111 # Parameter validation
90909112 param_error = _validate_function_parameters (target_function , parameters )
@@ -9099,31 +9121,43 @@ async def handle_execute_delegation_function(
90999121 password = delegation_config .claude_server_credential ,
91009122 skip_ssl_verify = delegation_config .skip_ssl_verify ,
91019123 ) as client :
9102- repo_aliases = await _ensure_repos_registered (client , target_function .required_repos )
9124+ repo_aliases = await _ensure_repos_registered (
9125+ client , target_function .required_repos
9126+ )
91039127
91049128 # Render prompt and create job
91059129 processor = PromptTemplateProcessor ()
9106- impersonation_user = target_function .impersonation_user or effective_user .username
9130+ impersonation_user = (
9131+ target_function .impersonation_user or effective_user .username
9132+ )
91079133 rendered_prompt = processor .render (
91089134 template = target_function .prompt_template ,
91099135 parameters = parameters ,
91109136 user_prompt = user_prompt ,
91119137 impersonation_user = impersonation_user ,
91129138 )
91139139
9114- job_result = await client .create_job (prompt = rendered_prompt , repositories = repo_aliases )
9140+ job_result = await client .create_job (
9141+ prompt = rendered_prompt , repositories = repo_aliases
9142+ )
91159143 # Claude Server returns camelCase "jobId"
91169144 job_id = job_result .get ("jobId" ) or job_result .get ("job_id" )
91179145 if not job_id :
9118- return _mcp_response ({"success" : False , "error" : "Job created but no job_id returned" })
9146+ return _mcp_response (
9147+ {"success" : False , "error" : "Job created but no job_id returned" }
9148+ )
91199149
91209150 # Story #720: Register callback URL with Claude Server for completion notification
91219151 callback_base_url = _get_cidx_callback_base_url ()
91229152 if callback_base_url :
9123- callback_url = f"{ callback_base_url .rstrip ('/' )} /api/delegation/callback/{ job_id } "
9153+ callback_url = (
9154+ f"{ callback_base_url .rstrip ('/' )} /api/delegation/callback/{ job_id } "
9155+ )
91249156 try :
91259157 await client .register_callback (job_id , callback_url )
9126- logger .debug (f"Registered callback URL for job { job_id } : { callback_url } " )
9158+ logger .debug (
9159+ f"Registered callback URL for job { job_id } : { callback_url } "
9160+ )
91279161 except Exception as callback_err :
91289162 # Log but don't fail - callback registration is best-effort
91299163 logger .warning (
@@ -9142,10 +9176,15 @@ async def handle_execute_delegation_function(
91429176 return _mcp_response ({"success" : True , "job_id" : job_id })
91439177
91449178 except ClaudeServerError as e :
9145- logger .error (f"Claude Server error: { e } " , extra = {"correlation_id" : get_correlation_id ()})
9179+ logger .error (
9180+ f"Claude Server error: { e } " , extra = {"correlation_id" : get_correlation_id ()}
9181+ )
91469182 return _mcp_response ({"success" : False , "error" : f"Claude Server error: { e } " })
91479183 except Exception as e :
9148- logger .exception (f"Error in execute_delegation_function: { e } " , extra = {"correlation_id" : get_correlation_id ()})
9184+ logger .exception (
9185+ f"Error in execute_delegation_function: { e } " ,
9186+ extra = {"correlation_id" : get_correlation_id ()},
9187+ )
91499188 return _mcp_response ({"success" : False , "error" : str (e )})
91509189
91519190
@@ -9179,66 +9218,87 @@ async def handle_poll_delegation_job(
91799218 delegation_config = _get_delegation_config ()
91809219
91819220 if delegation_config is None or not delegation_config .is_configured :
9182- return _mcp_response ({"success" : False , "error" : "Claude Delegation not configured" })
9221+ return _mcp_response (
9222+ {"success" : False , "error" : "Claude Delegation not configured" }
9223+ )
91839224
91849225 job_id = args .get ("job_id" , "" )
91859226 if not job_id :
9186- return _mcp_response ({"success" : False , "error" : "Missing required parameter: job_id" })
9227+ return _mcp_response (
9228+ {"success" : False , "error" : "Missing required parameter: job_id" }
9229+ )
91879230
91889231 # Story #720: Get timeout_seconds from args (default 45s, below MCP's 60s)
91899232 # Also support legacy "timeout" parameter for backward compatibility
91909233 timeout = args .get ("timeout_seconds" , args .get ("timeout" , 45 ))
91919234 if not isinstance (timeout , (int , float )):
9192- return _mcp_response ({
9193- "success" : False ,
9194- "error" : "timeout_seconds must be a number (recommended: 5-300)"
9195- })
9235+ return _mcp_response (
9236+ {
9237+ "success" : False ,
9238+ "error" : "timeout_seconds must be a number (recommended: 5-300)" ,
9239+ }
9240+ )
91969241 # Minimum 0.01s (for testing), maximum 300s (5 minutes)
91979242 # Recommended range for production: 5-300 seconds
91989243 if timeout < 0.01 or timeout > 300 :
9199- return _mcp_response ({
9200- "success" : False ,
9201- "error" : "timeout_seconds must be between 0.01 and 300"
9202- })
9244+ return _mcp_response (
9245+ {
9246+ "success" : False ,
9247+ "error" : "timeout_seconds must be between 0.01 and 300" ,
9248+ }
9249+ )
92039250
92049251 # Check if job exists in tracker before waiting
92059252 tracker = DelegationJobTracker .get_instance ()
92069253 job_exists = await tracker .has_job (job_id )
92079254 if not job_exists :
9208- return _mcp_response ({
9209- "success" : False ,
9210- "error" : f"Job { job_id } not found or already completed" ,
9211- })
9255+ return _mcp_response (
9256+ {
9257+ "success" : False ,
9258+ "error" : f"Job { job_id } not found or already completed" ,
9259+ }
9260+ )
92129261
92139262 # Wait for callback via DelegationJobTracker
92149263 result = await tracker .wait_for_job (job_id , timeout = timeout )
92159264
92169265 if result is None :
92179266 # Timeout - job still exists, caller can try again
9218- return _mcp_response ({
9219- "status" : "waiting" ,
9220- "message" : "Job still running, callback not yet received" ,
9221- "continue_polling" : True ,
9222- })
9267+ return _mcp_response (
9268+ {
9269+ "status" : "waiting" ,
9270+ "message" : "Job still running, callback not yet received" ,
9271+ "continue_polling" : True ,
9272+ }
9273+ )
92239274
92249275 # Return result based on status from callback
92259276 if result .status == "completed" :
9226- return _mcp_response ({
9227- "status" : "completed" ,
9228- "result" : result .output ,
9229- "continue_polling" : False ,
9230- })
9277+ return _mcp_response (
9278+ {
9279+ "status" : "completed" ,
9280+ "result" : result .output ,
9281+ "continue_polling" : False ,
9282+ }
9283+ )
92319284 else :
92329285 # Failed or other status
9233- return _mcp_response ({
9234- "status" : "failed" ,
9235- "error" : result .error or result .output ,
9236- "continue_polling" : False ,
9237- })
9286+ return _mcp_response (
9287+ {
9288+ "status" : "failed" ,
9289+ "error" : result .error or result .output ,
9290+ "continue_polling" : False ,
9291+ }
9292+ )
92389293
92399294 except Exception as e :
9240- logger .error (f"Error waiting for delegation job { job_id } : { e } " , extra = {"correlation_id" : get_correlation_id ()})
9241- return _mcp_response ({"success" : False , "error" : f"Error waiting for job completion: { str (e )} " })
9295+ logger .error (
9296+ f"Error waiting for delegation job { job_id } : { e } " ,
9297+ extra = {"correlation_id" : get_correlation_id ()},
9298+ )
9299+ return _mcp_response (
9300+ {"success" : False , "error" : f"Error waiting for job completion: { str (e )} " }
9301+ )
92429302
92439303
92449304HANDLER_REGISTRY ["poll_delegation_job" ] = handle_poll_delegation_job
0 commit comments