Skip to content

🩹 [Patch]: Enable Context Sharing Across PowerShell Processes and Runspaces#88

Merged
Marius Storhaug (MariusStorhaug) merged 16 commits into
mainfrom
copilot/fix-87
Jun 3, 2025
Merged

🩹 [Patch]: Enable Context Sharing Across PowerShell Processes and Runspaces#88
Marius Storhaug (MariusStorhaug) merged 16 commits into
mainfrom
copilot/fix-87

Conversation

Copilot AI commented Jun 1, 2025

Copy link
Copy Markdown
Contributor

This release refactors context management to be entirely disk-based, enabling robust context sharing and synchronization across scripts, modules, jobs, and processes.

Key Changes

  • Shared context vault: Contexts are now stored and managed on disk. All scripts, runspaces, and processes interact with the same set of context files, ensuring that context data is always current and consistent.
  • Improved support for parallelism: Multiple PowerShell jobs or sessions can safely create, update, or remove contexts simultaneously, without risking stale or conflicting data.
  • Accurate tab completion: Argument completers now enumerate context IDs directly from disk, providing up-to-date options for tab completion.
  • Optimized metadata access: Commands like Get-ContextInfo perform quick metadata lookups by reading only the necessary file information, improving performance.
  • Lean initialization: The module no longer loads contexts into memory on import, reducing resource usage and simplifying module behavior.

Impact for Integrators

  • Modules and scripts can now rely on a single, shared context vault across all PowerShell processes.
  • There is no risk of working with outdated or inconsistent context data.
  • No changes are needed to existing command usage; standard commands (Get-Context, Set-Context, etc.) will now always reflect the true state on disk.
  • These improvements make the module suitable for use in advanced automation, CI/CD workflows, and other scenarios involving parallel or distributed execution.

This update ensures reliable, up-to-date context management for all usage patterns, including concurrent and multi-process environments.

Core Changes

  • Get-Context: Now reads and decrypts context files directly from the vault directory each time called
  • Get-ContextInfo: Reads only file metadata from disk without decryption for fast queries
  • Set-Context: No longer updates $script:Contexts memory cache, writes only to disk
  • Remove-Context: Scans disk files directly instead of iterating memory cache
  • Set-ContextVault: No longer auto-imports contexts into memory on initialization
  • Argument completers: Updated to enumerate context IDs from disk for accurate tab completion

Copilot AI and others added 2 commits June 1, 2025 20:04
Copilot AI changed the title [WIP] 🩹 [Patch]: Enable Context Sharing Across PowerShell Processes and Runspaces 🩹 [Patch]: Enable Context Sharing Across PowerShell Processes and Runspaces Jun 1, 2025
@MariusStorhaug

Copy link
Copy Markdown
Member

Would it make sense to let the completer to use Get-ContextInfo?
Would it make sense to remove the logic that uses and configures $script:Config.Initialized?

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>

Copilot AI commented Jun 1, 2025

Copy link
Copy Markdown
Contributor Author

Would it make sense to let the completer to use Get-ContextInfo?
Would it make sense to remove the logic that uses and configures $script:Config.Initialized?

Both suggestions implemented in commit 310c6a8:

  1. Completer now uses Get-ContextInfo: Replaced manual file reading with Get-ContextInfo call for better consistency and error handling
  2. Removed $script:Config.Initialized logic: Completer no longer checks initialization state since Get-ContextInfo handles it internally

Benefits: DRY principle, simpler code (7 vs 14 lines), consistent behavior across module.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • www.powershellgallery.com
    • Triggering command: pwsh -Command try { Install-Module Pester -Force -Scope CurrentUser } catch { Write-Host &#39;Pester install failed&#39; } (dns block)
    • Triggering command: pwsh -Command Get-PSRepository (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@MariusStorhaug Marius Storhaug (MariusStorhaug) added the Prerelease Make a prerelease of the package/module/repo. label Jun 1, 2025
@github-actions

github-actions Bot commented Jun 1, 2025

Copy link
Copy Markdown

Module Context - 7.0.5-copilotfix87001 published to the PowerShell Gallery.

@github-actions

github-actions Bot commented Jun 1, 2025

Copy link
Copy Markdown

GitHub release for Context v7.0.5-copilotfix87001 has been created.

@github-actions

github-actions Bot commented Jun 2, 2025

Copy link
Copy Markdown

Module Context - 7.0.5-copilotfix87002 published to the PowerShell Gallery.

@github-actions

github-actions Bot commented Jun 2, 2025

Copy link
Copy Markdown

GitHub release for Context v7.0.5-copilotfix87002 has been created.

@MariusStorhaug Marius Storhaug (MariusStorhaug) marked this pull request as ready for review June 3, 2025 16:42
Copilot AI review requested due to automatic review settings June 3, 2025 16:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This patch refactors context handling in the PowerShell module to enable true cross-process context sharing by replacing the in-memory dictionary with disk‐based operations. Key changes include:

  • Removing the in-memory context store and updating context operations to read/write directly from disk.
  • Updating module dependencies and documentation to reflect the new behavior.
  • Removing the Import-Context function and updating argument completers to enumerate contexts from disk.

Reviewed Changes

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

Show a summary per file
File Description
src/variables/private/Contexts.ps1 Removed in-memory concurrent dictionary storage.
src/functions/public/Set-Context.ps1 Refactored context creation/update to scan disk files and update docs.
src/functions/public/Remove-Context.ps1 Revised context removal to operate over files on disk.
src/functions/public/Get-ContextInfo.ps1 Changed retrieval to read only metadata from disk.
src/functions/public/Get-Context.ps1 Adjusted context decryption to work with contexts stored on disk.
src/functions/private/Set-ContextVault.ps1 Updated vault initialization and removed auto-import of contexts.
src/functions/private/Import-Context.ps1 Removed deprecated function for in-memory context import.
src/completers.ps1 Updated argument completer to enumerate contexts via Get-ContextInfo.

Comment thread src/functions/public/Set-Context.ps1
Comment thread src/functions/public/Get-Context.ps1
@MariusStorhaug Marius Storhaug (MariusStorhaug) merged commit 7041044 into main Jun 3, 2025
34 checks passed
@MariusStorhaug Marius Storhaug (MariusStorhaug) deleted the copilot/fix-87 branch June 3, 2025 20:15
@github-actions

github-actions Bot commented Jun 3, 2025

Copy link
Copy Markdown

Module Context - 7.0.5 published to the PowerShell Gallery.

@github-actions

github-actions Bot commented Jun 3, 2025

Copy link
Copy Markdown

GitHub release for Context v7.0.5 has been created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Prerelease Make a prerelease of the package/module/repo.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🩹 [Patch]: Enable Context Sharing Across PowerShell Processes and Runspaces

3 participants