Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion inc/Engine/AI/Tools/ToolExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function executeTool(
): array {
$core = new WP_Agent_Tool_Execution_Core();
$execution = new ToolExecutionCore();
$tool_context = array_merge( $payload, $client_context );
$tool_context = array_merge( $client_context, $payload );
$prepared = $core->prepareWP_Agent_Tool_Call( $tool_name, $tool_parameters, $available_tools, $tool_context );
if ( empty( $prepared['ready'] ) ) {
unset( $prepared['ready'] );
Expand Down
29 changes: 29 additions & 0 deletions tests/Unit/AI/Tools/ToolExecutorValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,35 @@ public function test_execute_tool_uses_agents_api_for_declared_client_context_bi
$this->assertSame( 'from client context', $result['result']['data']['query'] ?? null );
}

public function test_execute_tool_prefers_host_payload_for_declared_context_bindings(): void {
$available_tools = array(
'bound_context_tool' => array(
'class' => TestToolHandler::class,
'method' => 'handle_tool_call',
'parameters' => array(
'query' => array(
'type' => 'string',
'required' => true,
),
),
'client_context_bindings' => array( 'query' => 'job_id' ),
),
);

$result = ToolExecutor::executeTool(
'bound_context_tool',
array(),
$available_tools,
array( 'job_id' => 42 ),
'chat',
0,
array( 'job_id' => 7 )
);

$this->assertTrue( $result['success'] );
$this->assertSame( 42, $result['result']['data']['query'] ?? null );
}

public function test_execute_tool_does_not_satisfy_required_params_from_ambient_context_keys(): void {
$available_tools = array(
'unbound_context_tool' => array(
Expand Down
17 changes: 17 additions & 0 deletions tests/tool-source-registry-smoke.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,23 @@ static function ( array $sources ): array {
assert_source_equals( true, $bound_server_execution['success'] ?? null, 'declared client_context_bindings satisfy required parameters through Agents API', $failures, $passes );
assert_source_equals( 'from client context', $bound_server_execution['result']['data']['query'] ?? null, 'bound client context value reaches server tool parameters', $failures, $passes );

$payload_host_execution = ToolExecutor::executeTool(
'bound_context_tool',
array(),
array(
'bound_context_tool' => array_merge(
$bound_server_tools['bound_context_tool'],
array( 'client_context_bindings' => array( 'query' => 'job_id' ) )
),
),
array( 'job_id' => 42 ),
ToolPolicyResolver::MODE_CHAT,
0,
array( 'job_id' => 7 )
);
assert_source_equals( true, $payload_host_execution['success'] ?? null, 'declared binding can read host payload context values', $failures, $passes );
assert_source_equals( 42, $payload_host_execution['result']['data']['query'] ?? null, 'host payload values win over same-named client_context values', $failures, $passes );

$unbound_server_execution = ToolExecutor::executeTool(
'unbound_context_tool',
array(),
Expand Down
Loading