Repository: dotnet/runtime
File: src/tests/tracing/userevents/common/UserEventsTestRunner.cs (C#)

Code before:
        }

        private static int RunOrchestrator(
            string scenarioName,
            string traceeAssemblyPath,
            Func<EventPipeEventSource, bool> traceValidator,
            int traceeExitTimeout,
            int recordTraceExitTimeout)
        {
            // In NativeAOT, Assembly.Location returns empty string.
            // Use AppContext.BaseDirectory which points to the test scenario directory for both CoreCLR and NativeAOT.
            bool isNativeAot = string.IsNullOrEmpty(traceeAssemblyPath);
            string userEventsScenarioDir;
            if (isNativeAot)
            {
                userEventsScenarioDir = AppContext.BaseDirectory;
            }
            else
            {
                userEventsScenarioDir = Path.GetDirectoryName(traceeAssemblyPath) 
                    ?? throw new InvalidOperationException($"Unable to determine directory from assembly path: {traceeAssemblyPath}");
            }
            
            string recordTracePath = ResolveRecordTracePath(userEventsScenarioDir);
            string scriptFilePath = Path.Combine(userEventsScenarioDir, $"{scenarioName}.script");

            if (!UserEventsRequirements.IsSupported())
            {
                Console.WriteLine("Skipping test: environment does not support user events.");
                return 100;
            }
            if (!File.Exists(recordTracePath))
            {
                Console.Error.WriteLine($"record-trace not found at `{recordTracePath}`. Test cannot run.");
                return -1;
            }
            if (!File.Exists(scriptFilePath))
            {
                Console.Error.WriteLine($"record-trace script-file not found at `{scriptFilePath}`. Test cannot run.");
                return -1;
            }

            string traceFilePath = Path.GetTempFileName();
            // In the past, it's been observed that record-trace has trouble overwriting newly created temp files 
            // in `/tmp` (e.g. mktemp or Path.GetTempFileName). It's suspected to be a permissions issue.
            // As a workaround, deleting the temp file and allowing record-trace to create it works reliably.
            File.Delete(traceFilePath);
            traceFilePath = Path.ChangeExtension(traceFilePath, ".nettrace");

            ProcessStartInfo recordTraceStartInfo = new();
            recordTraceStartInfo.FileName = "sudo";

Review comment (refactor):
@copilot Both the Record-Trace executable and scenario's script still need to be resolved according todepending on `isNativeAot`.

Can you make ResolveRecordTracePath
```C#
            string usereventsRoot;
            if (isNativeAot)
            {
                // scenario dir: .../tracing/userevents/<scenario>/<scenario>/native/
                usereventsRoot = Path.GetFullPath(Path.Combine(userEventsScenarioDir, "..", "..", ".."));
            }
            else
            {
                // scenario dir: .../tracing/userevents/<scenario>/<scenario>
                usereventsRoot = Path.GetFullPath(Path.Combine(userEventsScenarioDir, "..", ".."));
            }
            // common dir: .../tracing/userevents/common/userevents_common
            string commonDir = Path.Combine(usereventsRoot, "common", "userevents_common");
            string recordTracePath = Path.Combine(commonDir, "record-trace");
            return recordTracePath;
```
And can you make another helper for resolving scriptFilePath with resolving path logic dependent on isNativeAot


Code after:
        private static int RunOrchestrator(
            string scenarioName,
            Func<EventPipeEventSource, bool> traceValidator,
            int traceeExitTimeout,
            int recordTraceExitTimeout)
        {
            // Start with AppContext.BaseDirectory and determine if we're running NativeAOT
            string baseDir = AppContext.BaseDirectory;
            bool isNativeAot = false;
            string userEventsScenarioDir = baseDir;

            if (Path.GetFileName(baseDir.TrimEnd(Path.DirectorySeparatorChar)) == "native")
            {
                // NativeAOT places its compiled test executables under a 'native' subdirectory.
                isNativeAot = true;
                userEventsScenarioDir = Path.GetFullPath(Path.Combine(baseDir, ".."));
            }

            if (Path.GetFileName(userEventsScenarioDir.TrimEnd(Path.DirectorySeparatorChar)) != scenarioName)
            {
                Console.Error.WriteLine($"Could not resolve the userevents test scenario directory. Expected directory to end with '{scenarioName}', but got: {userEventsScenarioDir}");
                return -1;
            }

            string recordTracePath = ResolveRecordTracePath(userEventsScenarioDir);
            string scriptFilePath = Path.Combine(userEventsScenarioDir, $"{scenarioName}.script");

            if (!UserEventsRequirements.IsSupported())
            {
                Console.WriteLine("Skipping test: environment does not support user events.");
                return 100;
            }
            if (!File.Exists(recordTracePath))
            {
                Console.Error.WriteLine($"record-trace not found at `{recordTracePath}`. Test cannot run.");
                return -1;
            }
            if (!File.Exists(scriptFilePath))
            {
                Console.Error.WriteLine($"record-trace script-file not found at `{scriptFilePath}`. Test cannot run.");
                return -1;
            }

            string traceFilePath = Path.GetTempFileName();
            // In the past, it's been observed that record-trace has trouble overwriting newly created temp files 
            // in `/tmp` (e.g. mktemp or Path.GetTempFileName). It's suspected to be a permissions issue.
            // As a workaround, deleting the temp file and allowing record-trace to create it works reliably.
            File.Delete(traceFilePath);
            traceFilePath = Path.ChangeExtension(traceFilePath, ".nettrace");

            ProcessStartInfo recordTraceStartInfo = new();