Skip to content

[CmdPal] Refactor ActionRuntime initialization to avoid repeated delays on failure #40229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 26, 2025

Conversation

lei9444
Copy link
Contributor

@lei9444 lei9444 commented Jun 25, 2025

Summary of the Pull Request

This PR addresses an issue where ActionRuntime initialization may repeatedly incur delays and fail during runtime usage, especially when the OS supports the API but action creation fails.

Fix:

Introduced ActionRuntimeManager to:

  • Manage a shared static ActionRuntime instance
  • Perform initialization once, at extension loading time
  • Store the result (success or failure) to avoid repeated creation attempts

Consumers now:

  • ActionRuntimeManager.InstanceAsync.GetAwaiter().GetResult()

Initialization will only be attempted up to 3 times. After that, runtime is marked as unavailable and no further attempts are made.

PR Checklist

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

Copy link

@dcq01 dcq01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! I'm a grad student working on a research project about using large language models to automate code review. Based on your commit d51e2b5 and the changes in src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Pages/ActionsListContextItem.cs, my tool generated this comment:

  1. Restore the try-catch block around the call to ActionRuntimeManager.WaitForRuntimeAsync() to maintain error handling and ensure exceptions are logged.
  2. Ensure that fullPath is validated and sanitized before use to prevent exceptions when calling System.IO.Path.GetExtension(fullPath).
  3. The line MoreCommands = [.. actions]; should be corrected to use valid syntax for assignment in C#. If MoreCommands is a list, use MoreCommands = new List<CommandContextItem>(actions);.
  4. Consider using await instead of .Wait() on ActionRuntimeManager.WaitForRuntimeAsync() to avoid blocking the thread and potential deadlocks.
  5. Enhance logging by including more context in error messages, such as the fullPath, when an error occurs while updating commands.
  6. Consider refactoring the logic for determining the entity based on the file extension into a separate method to improve clarity and maintainability.
  7. Use a HashSet<string> for supported file extensions to optimize the time complexity of checking if an extension is valid.
  8. Consider using a concurrent collection like ConcurrentBag<T> or ConcurrentQueue<T> instead of locking on actions to reduce contention and improve performance in multi-threaded scenarios.
  9. Remove the redundant null check for actionRuntime after the try-catch block. Return early if actionRuntime is still null right after the first attempt.

As part of my research, I'm trying to understand how useful these comments are in real-world development. If you have a moment, I'd be super grateful if you could quickly reply to these two yes/no questions:

  1. Does this comment provide suggestions from a dimension you hadn’t considered?
    1. Do you find this comment helpful?

Thanks a lot for your time and feedback! And sorry again if this message is a bother.

@yeelam-gordon yeelam-gordon requested a review from Copilot June 26, 2025 01:01
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors ActionRuntime initialization to reduce delays and prevent repeated failed attempts by introducing a shared static instance managed by ActionRuntimeManager.

  • Introduces ActionRuntimeManager to attempt initialization up to 3 times and cache the result.
  • Updates ActionsListContextItem to use the shared ActionRuntime and implement IDisposable for cleanup.
  • Modifies IndexerCommandsProvider to conditionally initialize the ActionRuntime based on OS API availability.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Pages/ActionsListContextItem.cs Updated to retrieve runtime from ActionRuntimeManager and implement IDisposable.
src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/IndexerCommandsProvider.cs Integration of API contract check and initialization call for ActionRuntimeManager.
src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Actions/ActionRuntimeManager.cs New static class to manage and cache ActionRuntime initialization with a maximum of 3 attempts.

Copy link
Contributor

@yeelam-gordon yeelam-gordon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's talk in case.

@lei9444
Copy link
Contributor Author

lei9444 commented Jun 26, 2025

Hi! I'm a grad student working on a research project about using large language models to automate code review. Based on your commit d51e2b5 and the changes in src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Pages/ActionsListContextItem.cs, my tool generated this comment:

  1. Restore the try-catch block around the call to ActionRuntimeManager.WaitForRuntimeAsync() to maintain error handling and ensure exceptions are logged.
  2. Ensure that fullPath is validated and sanitized before use to prevent exceptions when calling System.IO.Path.GetExtension(fullPath).
  3. The line MoreCommands = [.. actions]; should be corrected to use valid syntax for assignment in C#. If MoreCommands is a list, use MoreCommands = new List<CommandContextItem>(actions);.
  4. Consider using await instead of .Wait() on ActionRuntimeManager.WaitForRuntimeAsync() to avoid blocking the thread and potential deadlocks.
  5. Enhance logging by including more context in error messages, such as the fullPath, when an error occurs while updating commands.
  6. Consider refactoring the logic for determining the entity based on the file extension into a separate method to improve clarity and maintainability.
  7. Use a HashSet<string> for supported file extensions to optimize the time complexity of checking if an extension is valid.
  8. Consider using a concurrent collection like ConcurrentBag<T> or ConcurrentQueue<T> instead of locking on actions to reduce contention and improve performance in multi-threaded scenarios.
  9. Remove the redundant null check for actionRuntime after the try-catch block. Return early if actionRuntime is still null right after the first attempt.

As part of my research, I'm trying to understand how useful these comments are in real-world development. If you have a moment, I'd be super grateful if you could quickly reply to these two yes/no questions:

  1. Does this comment provide suggestions from a dimension you hadn’t considered?
    1. Do you find this comment helpful?

Thanks a lot for your time and feedback! And sorry again if this message is a bother.

@dcq01 Thanks.

  1. Yes — some points like using HashSet for extension checks and refactoring entity logic into a separate method are good reminders and help improve maintainability.
  2. Yes — overall it’s quite thorough. That said, some suggestions (like using await instead of .Wait()) don’t apply in our case since we're explicitly managing fire-and-forget background behavior with a non-async context.

Copy link
Contributor

@yeelam-gordon yeelam-gordon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great.

@lei9444
Copy link
Contributor Author

lei9444 commented Jun 26, 2025

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@yeelam-gordon yeelam-gordon merged commit d65ba7f into main Jun 26, 2025
14 checks passed
@yeelam-gordon yeelam-gordon added the Product-Command Palette Refers to the Command Palette utility label Jun 26, 2025
@yeelam-gordon yeelam-gordon added this to the PowerToys 0.92 milestone Jun 26, 2025
yeelam-gordon pushed a commit that referenced this pull request Jun 27, 2025
…ys on failure (#40229)

<!-- Enter a brief description/summary of your PR here. What does it
fix/what does it change/how was it tested (even manually, if necessary)?
-->
## Summary of the Pull Request
This PR addresses an issue where ActionRuntime initialization may
repeatedly incur delays and fail during runtime usage, especially when
the OS supports the API but action creation fails.

### Fix:
Introduced ActionRuntimeManager to:

- Manage a shared static ActionRuntime instance
- Perform initialization once, at extension loading time
- Store the result (success or failure) to avoid repeated creation
attempts

Consumers now:

- ActionRuntimeManager.InstanceAsync.GetAwaiter().GetResult()

Initialization will only be attempted up to 3 times. After that, runtime
is marked as unavailable and no further attempts are made.
<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist

- [x] **Closes:** #40228
- [ ] **Communication:** I've discussed this with core contributors
already. If the work hasn't been agreed, this work might be rejected
- [ ] **Tests:** Added/updated and all pass
- [ ] **Localization:** All end-user-facing strings can be localized
- [ ] **Dev docs:** Added/updated
- [ ] **New binaries:** Added on the required places
- [ ] [JSON for
signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json)
for new binaries
- [ ] [WXS for
installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs)
for new binaries and localization folder
- [ ] [YML for CI
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml)
for new test projects
- [ ] [YML for signed
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml)
- [ ] **Documentation updated:** If checked, please file a pull request
on [our docs
repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys)
and link it here: #xxx

<!-- Provide a more detailed description of the PR, other things fixed,
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments

<!-- Describe how you validated the behavior. Add automated tests
wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Product-Command Palette Refers to the Command Palette utility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants