Skip to content

Add a private field to indicate if PSReadLine is initialized and ready #4706

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 3 commits into from
Apr 16, 2025

Conversation

daxian-dbw
Copy link
Member

@daxian-dbw daxian-dbw commented Apr 14, 2025

PR Summary

When posting code from AIShell to PowerShell, we need to know if PSReadLine has finished the initialization that is required for each call to PSConsoleReadLine.ReadLine(...).

This is especially needed when one is calling Invoke-AIShell -PostCode from PowerShell to request the generated code to be posted from the AIShell side:

  • If PSReadLine hasn't finishes the initialization, the posted code may be rendered at the wrong row/position (because the new cursor position hasn't been set to _initialX and _initialY) or even cause exception due to race condition.

The fix is to add a private field to indicate if the initialization per call is done. This doesn't introduce any behavior changes to PSReadLine. Making it a private field means it's not an officially supported scenario, even though a tool like AIShell can utilize it.

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Make sure you've added one or more new tests
  • Make sure you've tested these changes in terminals that PowerShell is commonly used in (i.e. conhost.exe, Windows Terminal, Visual Studio Code Integrated Terminal, etc.)
  • User-facing changes
    • Not Applicable
    • OR
    • Documentation needed at PowerShell-Docs
      • Doc Issue filed:
Microsoft Reviewers: Open in CodeFlow

@daxian-dbw daxian-dbw merged commit b926959 into PowerShell:master Apr 16, 2025
2 checks passed
@daxian-dbw daxian-dbw deleted the readline-ready branch April 16, 2025 17:09
daxian-dbw added a commit to PowerShell/AIShell that referenced this pull request Apr 16, 2025
This PR fixes the following scenarios on macOS
1. Running `/code post` from the `AIShell` pane to post code to the `PowerShell` pane
2. Running `Invoke-AIShell -PostCode` from the `PowerShell` to request code to be posted from the `AIShell` pane.

**[NOTE]** Now the `AIShell` module depends on the **v2.4.2-beta2** PSReadLine, because a private field was added in PSReadLine to accurately indicate if PSReadLine is initialized and ready to render (PowerShell/PSReadLine#4706).

The `AIShell` module starts to depend on that field to be more deterministic about if PSReadLine is running and ready. Before this change, we relied on `Console.TreatControlCAsInput` which is not accurate and may cause race condition: `Console.TreatControlCAsInput` is set to true, but `Initialize(...)` is not called yet. Rendering the posted code before initialization could result in corrupted state -- posted code may be rendered on wrong position or even cause exception.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants