@@ -3270,6 +3270,8 @@ def _match_mixed_pause_responses(
32703270 host_items_by_occurrence [request .id ] = index
32713271
32723272 matched_content_ids : set [int ] = set ()
3273+ preexisting_response_indexes = {index for index , item in enumerate (items ) if item .get ("response" ) is not None }
3274+ current_idless_response_indexes : set [int ] = set ()
32733275 for match_idless_host_results in (False , True ):
32743276 for response in responses :
32753277 is_idless_host_result = response .type == "function_result" and response .id is None
@@ -3310,6 +3312,18 @@ def _match_mixed_pause_responses(
33103312 if items [pending_index ].get ("response" ) is None
33113313 ]
33123314 if len (unanswered_indexes ) == 1 :
3315+ if any (
3316+ pending_index in preexisting_response_indexes
3317+ and isinstance (items [pending_index ].get ("response" ), Mapping )
3318+ and _same_mixed_pause_response (
3319+ cast (Mapping [str , Any ], items [pending_index ]["response" ]),
3320+ response .to_dict (),
3321+ )
3322+ for pending_index in host_items_by_call [response .call_id ]
3323+ ):
3324+ raise RuntimeError (
3325+ f"Ambiguous id-less Host response for mixed pause call_id { response .call_id !r} ."
3326+ )
33133327 item_index = unanswered_indexes [0 ]
33143328 elif not unanswered_indexes and allow_idless_host_duplicates :
33153329 duplicate_indexes = [
@@ -3323,6 +3337,13 @@ def _match_mixed_pause_responses(
33233337 ]
33243338 if len (duplicate_indexes ) == 1 :
33253339 item_index = duplicate_indexes [0 ]
3340+ elif any (
3341+ pending_index in current_idless_response_indexes
3342+ for pending_index in host_items_by_call [response .call_id ]
3343+ ):
3344+ raise RuntimeError (
3345+ f"Conflicting id-less Host responses for mixed pause call_id { response .call_id !r} ."
3346+ )
33263347 else :
33273348 continue
33283349
@@ -3337,6 +3358,8 @@ def _match_mixed_pause_responses(
33373358 raise RuntimeError (f"Conflicting response for mixed pause occurrence { candidate .id !r} ." )
33383359 items [item_index ]["response" ] = candidate_state
33393360 matched_content_ids .add (id (response ))
3361+ if is_idless_host_result :
3362+ current_idless_response_indexes .add (item_index )
33403363
33413364 if any (item .get ("response" ) is None for item in items ):
33423365 return matched_content_ids , True , [], set ()
@@ -3383,11 +3406,84 @@ def bind_approval_response(response: Content) -> Content | None:
33833406 consume = False ,
33843407 )
33853408
3409+ host_requests = [
3410+ request
3411+ for item in items
3412+ if item .get ("kind" ) == "host"
3413+ and item .get ("response" ) is None
3414+ and (request := _content_from_state (item .get ("request" ))) is not None
3415+ and request .call_id is not None
3416+ ]
3417+ approval_request_ids = {
3418+ str (identity )
3419+ for item in items
3420+ if item .get ("kind" ) == "approval"
3421+ and item .get ("response" ) is None
3422+ and (request := _content_from_state (item .get ("request" ))) is not None
3423+ for identity in (
3424+ request .id ,
3425+ request .function_call .id if request .function_call is not None else None ,
3426+ )
3427+ if identity is not None
3428+ }
3429+ host_call_ids = {request .call_id for request in host_requests if request .call_id is not None }
3430+ host_occurrences = {
3431+ (request .call_id , request .id )
3432+ for request in host_requests
3433+ if request .call_id is not None and request .id is not None
3434+ }
3435+ approval_anchor : int | None = None
3436+ host_anchor : int | None = None
3437+ idless_host_anchor : int | None = None
3438+ indexed_contents : list [tuple [int , Content ]] = []
3439+ current_input_indexes : set [int ] = set ()
3440+ content_index = - 1
3441+ for message in messages :
3442+ message_start = content_index + 1
3443+ is_current_input = "_attribution" not in message .additional_properties
3444+ for content in message .contents :
3445+ content_index += 1
3446+ indexed_contents .append ((content_index , content ))
3447+ if not is_current_input :
3448+ continue
3449+ current_input_indexes .add (content_index )
3450+ if content .type == "function_approval_response" and approval_anchor is None :
3451+ bound_approval = bind_approval_response (content )
3452+ if bound_approval is not None and approval_request_ids .intersection (
3453+ str (identity )
3454+ for identity in (
3455+ bound_approval .additional_properties .get (_APPROVAL_REQUEST_ID_KEY ),
3456+ bound_approval .id ,
3457+ )
3458+ if identity is not None
3459+ ):
3460+ approval_anchor = message_start
3461+ elif (
3462+ content .type == "function_result"
3463+ and content .call_id is not None
3464+ and content .id is not None
3465+ and (content .call_id , content .id ) in host_occurrences
3466+ and host_anchor is None
3467+ ):
3468+ host_anchor = message_start
3469+ elif (
3470+ content .type == "function_result"
3471+ and content .id is None
3472+ and content .call_id in host_call_ids
3473+ and idless_host_anchor is None
3474+ ):
3475+ idless_host_anchor = message_start
3476+ response_start = min (
3477+ (anchor for anchor in (approval_anchor , host_anchor , idless_host_anchor ) if anchor is not None ),
3478+ default = None ,
3479+ )
33863480 responses = [
33873481 content
3388- for message in messages
3389- for content in message .contents
3390- if content .type in {"function_approval_response" , "function_result" }
3482+ for index , content in indexed_contents
3483+ if response_start is not None
3484+ and index >= response_start
3485+ and index in current_input_indexes
3486+ and content .type in {"function_approval_response" , "function_result" }
33913487 ]
33923488 matched_content_ids , incomplete , ordered_responses , host_result_ids = _match_mixed_pause_responses (
33933489 items ,
0 commit comments