diff --git a/inc/Core/JobArtifacts.php b/inc/Core/JobArtifacts.php index dc7630440..4b4f27ffc 100644 --- a/inc/Core/JobArtifacts.php +++ b/inc/Core/JobArtifacts.php @@ -225,10 +225,13 @@ private function agent_memory_artifacts( array $job, array $agent, array $tool_c continue; } - $agent_id = (int) ( $tool_call['agent_id'] ?? $default_agent_id ); - $user_id = (int) ( $tool_call['user_id'] ?? ( $job['user_id'] ?? 0 ) ); - $layer = AgentMemoryFile::resolve_layer_for( $filename ); - $key = $layer . ':' . $agent_id . ':' . $user_id . ':' . $filename; + $agent_id = (int) ( $tool_call['agent_id'] ?? 0 ); + if ( $agent_id <= 0 && $default_agent_id > 0 ) { + $agent_id = $default_agent_id; + } + $user_id = (int) ( $tool_call['user_id'] ?? ( $job['user_id'] ?? 0 ) ); + $layer = AgentMemoryFile::resolve_layer_for( $filename ); + $key = $layer . ':' . $agent_id . ':' . $user_id . ':' . $filename; $files[ $key ] = array( 'agent_id' => $agent_id, diff --git a/inc/Engine/AI/Tools/Global/AgentMemory.php b/inc/Engine/AI/Tools/Global/AgentMemory.php index 43d133ea7..a4dda27f9 100644 --- a/inc/Engine/AI/Tools/Global/AgentMemory.php +++ b/inc/Engine/AI/Tools/Global/AgentMemory.php @@ -90,6 +90,7 @@ private function handleGet( array $parameters ): array { return array( 'success' => true, 'data' => $result, + 'scope' => $scope, 'tool_name' => 'agent_memory', ); } @@ -157,6 +158,7 @@ private function handleUpdate( array $parameters ): array { return array( 'success' => true, 'data' => $result, + 'scope' => $scope, 'tool_name' => 'agent_memory', ); } @@ -202,6 +204,7 @@ private function handleListSections( array $parameters = array() ): array { return array( 'success' => true, 'data' => $result, + 'scope' => $scope, 'tool_name' => 'agent_memory', ); } diff --git a/inc/Engine/AI/conversation-loop.php b/inc/Engine/AI/conversation-loop.php index c05c5f80e..77b70064f 100644 --- a/inc/Engine/AI/conversation-loop.php +++ b/inc/Engine/AI/conversation-loop.php @@ -1086,8 +1086,9 @@ function datamachine_summarize_tool_execution_results( array $tool_execution_res } if ( 'agent_memory' === $tool_name ) { - $summary['user_id'] = isset( $parameters['user_id'] ) ? (int) $parameters['user_id'] : null; - $summary['agent_id'] = isset( $parameters['agent_id'] ) ? (int) $parameters['agent_id'] : null; + $scope = is_array( $tool_result['scope'] ?? null ) ? $tool_result['scope'] : array(); + $summary['user_id'] = isset( $scope['user_id'] ) ? (int) $scope['user_id'] : ( isset( $parameters['user_id'] ) ? (int) $parameters['user_id'] : null ); + $summary['agent_id'] = isset( $scope['agent_id'] ) ? (int) $scope['agent_id'] : ( isset( $parameters['agent_id'] ) ? (int) $parameters['agent_id'] : null ); $summary['action'] = isset( $parameters['action'] ) ? sanitize_key( (string) $parameters['action'] ) : null; $summary['file'] = isset( $parameters['file'] ) ? sanitize_file_name( (string) $parameters['file'] ) : 'MEMORY.md'; $summary['section'] = isset( $parameters['section'] ) ? sanitize_text_field( (string) $parameters['section'] ) : null; diff --git a/tests/job-artifacts-daily-memory-smoke.php b/tests/job-artifacts-daily-memory-smoke.php index acca1d047..53ebe179f 100644 --- a/tests/job-artifacts-daily-memory-smoke.php +++ b/tests/job-artifacts-daily-memory-smoke.php @@ -87,10 +87,11 @@ $assert( false !== strpos( $loop, "'agent_memory' === \$tool_name" ) + && false !== strpos( $loop, "\$tool_result['scope']" ) && false !== strpos( $loop, "\$summary['file']" ) && false !== strpos( $loop, "\$summary['section']" ) && false !== strpos( $loop, "'update' === \$summary['action']" ), - 'agent memory update summaries preserve file, section, action, and content for artifact export' + 'agent memory update summaries preserve resolved scope, file, section, action, and content for artifact export' ); $assert( @@ -110,8 +111,9 @@ $assert( false !== strpos( $job_artifacts, 'new AgentMemoryFile' ) && false !== strpos( $job_artifacts, 'get_all()' ) + && false !== strpos( $job_artifacts, '$agent_id <= 0 && $default_agent_id > 0' ) && false !== strpos( $job_artifacts, 'agent_memory_fallback_content' ), - 'agent memory artifacts export the full updated file content with a write-content fallback' + 'agent memory artifacts export the full updated file content using the resolved job agent when needed' ); $assert(