set-active-plan.ps1: the pointer pre-check can race a concurrent replacement during parallel named inits

Author: OthmanAdiCreated Sep 25, 2026Updated Sep 25, 2026
Labelsbughelp wanted

Problem

When several init-session.ps1 "<name>" runs start at the same moment in one project, an occasional run still exits 1 with:

Error: the active plan pointer must be a regular file within the project.

The check comes from the pointer pre-check in scripts/set-active-plan.ps1 (lines 348 to 352 in v3.20.8), which runs before the replacement loop:

powershell
$activeItem = Get-Item -LiteralPath $ActiveFile -Force -ErrorAction SilentlyContinue
if ($activeItem -and -not (Test-SafeActiveFile)) {
    Write-Error "Error: the active plan pointer must be a regular file within the project."
    exit 1
}

If another selector is in the middle of File.Replace at that moment, .planning/.active_plan can exist for Get-Item and be gone or unreadable for Test-SafeActiveFile a moment later. init-session.ps1 (lines 368 to 376) retries only the message the active plan pointer became unsafe during replacement, so this failure is final for that init.

Measurements

Windows 11, Windows PowerShell 5.1 and pwsh 7.6, rounds of 4 parallel named inits, measured during the v3.20.8 release review:

  • v3.20.7: 5 failed runs of 320
  • v3.20.8: 3 failed runs of 320

The pointer named an existing plan after every round. v3.20.8 leaves no .active_plan~RF*.TMP or backup files (#254, PR #290). test_concurrent_named_inits_all_replace_pointer_cleanly in tests/test_init_session_powershell_slug.py hits this now and then; it failed once in a focused run during the review and passed on rerun.

Suggested fix

  • In the pre-check, treat a pointer that disappears between Get-Item and Test-SafeActiveFile as transient and inspect it again within a small bound (the replacement loop already uses 20 attempts of 25 ms). Fail only when an item exists and is not a regular file inside the planning root: a link, a directory, or a path outside the project.
  • Or let init-session.ps1 retry this message with the same bound it uses for the other transient race.
  • A regression can run the 4-writer body of the existing concurrency test in a loop and assert that no run fails with this message.

Scope

Windows PowerShell named-plan mode with concurrent inits only. The shell twin replaces the pointer with a single mv -f and does not have this pre-check race. Follow-up to #254.

Source: OthmanAdi/planning-with-files