Own comment already there ("what if init omp last"), but user init order isn't real fix — root cause worth confirming in code. Let go find pwsh async prompt gen code.
Assistant
Root cause found in code, no live PWSH+zoxide repro run — reasoning from source only, flagged as such below.
What happens
async: true in pwsh doesn't make rendering async — it makes loading lazy. init.go:284:
go
case PWSH:
return fmt.Sprintf("function prompt() { & %s }", quotePwshOrElvishStr(scriptPath))
Profile gets placeholder prompt function only. Heavy omp.ps1 (full New-Module/Import-Module, real prompt logic) deferred to first actual prompt render — startup stays fast.
Sequence with user's profile (omp init, then zoxide init):
First real prompt render → PSReadLine calls prompt → zoxide wrapper → calls captured placeholder → placeholder runs & <scriptPath> → full omp.ps1 executes for first time → ends with omp.ps1:989:
pwsh
$Function:prompt = $promptFunction
$Function: drive assignment writes global scope regardless of calling context (same mechanism that lets sync mode work at all). This silently overwrites zoxide's wrapper that step 2 installed.
From that point, global prompt = omp's real fast function directly. zoxide's wrapper gone, __zoxide_hook never called again. Prompt itself looks fine (that's why bug looked subtle) — only directory tracking silently dies.
Confirms user's own hypothesis exactly. sync (async: false) works because init.go:257 sources the real script directly at profile-load time (& scriptPath once, immediately) — no deferred second mutation happens after zoxide wraps it.
Order doesn't actually fix it
Jan's issue comment ("what if you init omp last?") — traced through, reordering doesn't help: whichever tool wraps prompt before the lazy placeholder fires still gets its wrapper discarded on first render, since the destructive reassignment happens inside the deferred script, not at profile-load time. Worth noting in the reply.
Blast radius
Any pwsh tool that wraps $function:prompt between the Invoke-Expression (oh-my-posh init pwsh ...) call and the first rendered prompt breaks the same way under async: true — not zoxide-specific (posh-git legacy prompt wrapping, custom user wrappers, etc. all at risk). zsh/bash/fish async paths use a different mechanism (re-source every precmd/PROMPT_COMMAND/fish_prompt cycle — init.go:286-291) — related risk shape, not verified, out of scope here since issue is pwsh-only.
Proposed fix — needs a direction call
Root cause is architectural: pwsh async's one-shot "self-replacing lazy loader" pattern is fundamentally incompatible with any prompt-wrapping tool that hooks in during the same profile. Two directions, worth deciding before implementing:
A. Stop lazy-deferring the whole module import. New-Module/Import-Module itself is cheap (no process spawn) — only the actual oh-my-posh.exe print call is expensive. Import eagerly, defer only the first render's binary call inside $promptFunction. Removes the self-mutating-global-function pattern entirely.
B. Keep lazy loading, but on first fire, don't blindly overwrite global prompt — detect if it currently differs from the placeholder (something wrapped it) and chain instead of stomp. More surgical, more fragile, doesn't fix zsh/bash/fish's related risk.
A is cleaner and likely smaller diff. Let me know which direction, or if you want a deeper look at zsh/bash/fish first since they may share the underlying pattern.
Suggested reply to issue
Confirmed the cause: async: true doesn't render async, it defers the entire pwsh init script (including the line that (re)installs the global prompt function) to the first prompt draw. Anything that wraps prompt after oh-my-posh init pwsh but before that first draw — like zoxide's hook — gets silently overwritten when that deferred import finally runs, regardless of init order. Not an order problem; tracking as a real bug in how async is implemented for pwsh.
User
rewrite my comment to the sugested reply but use /writing-clearly-and-concisely to rewrite it. Executethis witha subagent.
Assistant
Subagent running, rewrite reply via writing skill. Notify when done.
Assistant
Rewrite done:
Confirmed the cause: async: true doesn't render asynchronously — it defers the entire pwsh init script, including the line that reinstalls the global prompt function, until the first prompt draw. Anything that wraps prompt after oh-my-posh init but before that first draw, like zoxide's hook, gets silently overwritten when the deferred import finally runs. Init order doesn't matter. This is a real bug in how async is implemented for pwsh, and I'm tracking it as one.
Post to issue #7714, or want more edits first?
User
Propose a fix. I want to use option B because the user still wants to lazy load things, and I think we should be able to handle that use case. Do not change the code just yet. Just do a full analysis and a proposal on how we can solve this on the PowerShell side.
Assistant
Thought through your Option B literally (detect-and-chain) — it's not fixable that way. Here's why, and the actual mechanism that gets the same result.
Why "detect and chain" can't work
PowerShell has exactly one global prompt binding. Last write wins, no matter how deep in the call stack the write happens. Trace it:
oh-my-posh init pwsh (async) → global prompt = placeholder P0.
zoxide → captures P0 into $__zoxide_prompt_old, installs itself as global prompt = Z.
First render: PSReadLine calls prompt → resolves to Z → Z calls captured P0 → P0 runs & <scriptPath> → omp.ps1 hits $Function:prompt = $promptFunction (omp.ps1:989) → global prompt is now R (omp's real function), overwriting Z.
Even if P0 afterward tries to be clever — capture R, then set $Function:prompt = P0 again to "restore itself" — it's too late. Z (zoxide's wrapper scriptblock) was never stored anywhere except that one global binding. The instant $Function:prompt = $promptFunction runs, Z is gone — unreachable, no reference to it survives. There's no snapshot to chain back to. Detection-after-the-fact can't undo a binding that already got overwritten.
The only way to fix this: never let anything write $Function:prompt a second time after the placeholder is installed.
The actual fix: trampoline + shared pointer, not a second function assignment
Keep the lazy-load property (heavy import deferred to first render), but stop having the deferred import touch $Function:prompt at all. Instead it writes into a plain global variable — a data slot, not the Function: drive — and the placeholder becomes a permanent trampoline that dispatches to whatever's in that slot.
init.go, sourceCommandAsync for PWSH (init.go:284), conceptually:
go
User
review this proposal with Fable and have it fill in the gaps and report back.
Assistant
Fable found the mechanism sound but caught real gaps. Summary:
Correction: clobber is dual-mechanism, not just the $Function:prompt= write — omp.ps1:1371Export-ModuleMember -Function "prompt" + Import-Module -Global also installs into global. Skipping the drive write in async mode is enough (export of a nonexistent function is a silent no-op), but plan must say why.
Bugs found:
Re-init breaks — init script path is per-config, overwritten in place on feature/version change. My trampoline's if (-not $global:_ompPromptFunction) guard never re-fires once set → edited config never reloads mid-session. Fix: reset $global:_ompPromptFunction = $null in the emitted async line.
& $null crash — if the deferred import fails, _ompPromptFunction stays null, every subsequent draw throws. Fix: guard delegation with if ($global:_ompPromptFunction) { & $global:_ompPromptFunction }.
$? fragility — exact statement shape matters. A skipped if body or bare condition read preserves $?; an executed statement before delegation resets it to $true, which would mask Update-PoshErrorCode's status segment on every prompt. Plan needs to pin the literal one-liner shape, not just describe intent.
_ompAsyncInit convention trap — do NOT add it to the existing global-enablers block (omp.ps1:20-27); that block runs after the trampoline sets the flag and would stomp it back to $false before line 989 checks it. Consume into $script:AsyncInit near line 41 instead.
No existing pwsh_test.go coverage on sourceCommandAsync's PWSH branch — needs a new assertion.
OnRemove loose end — resolved, and my original tentative idea was actually broken: restoring $global:_ompPromptFunction = $script:OriginalPromptFunction would infinite-recurse, because in async mode that backup captures the wrapper chain (zoxide) at first-draw time, not the true pre-omp prompt — so restore would loop wrapper → trampoline → wrapper. Concrete fix: capture the true pre-omp prompt once, guarded (if (-not $global:_ompOriginalPromptFunction)), at trampoline-install time (before anything wraps it), store separately, and OnRemove restores from that dedicated variable — never from $script:OriginalPromptFunction in async mode.
Verdict: mechanism sound, verified live on pwsh 7 and Windows PowerShell 5.1 including the zoxide-wrapper scenario, streaming/transient/tooltip InvokePrompt() call sites, and CLM. All five gaps are concrete and resolvable, not architectural blockers.
Want this folded into an updated proposal doc, or straight into a Phase 2 implementation plan?
Trampoline + shared global pointer, per the reviewed proposal. $Function:prompt gets written exactly once, synchronously, at Invoke-Expression time. The deferred heavy import writes a plain global variable instead of the Function: drive. Confirmed sound (mechanism + edge cases) by independent Fable review — findings folded in below as hard constraints, not options.
Files touched
src/shell/init.go — sourceCommandAsync, PWSH case (currently line 284)
src/shell/scripts/omp.ps1 — three spots: near line 41 (state capture), line 989 (install), OnRemove block near line 1331
src/shell/pwsh_test.go — new test for the emitted async line
Exact changes
1. init.go, sourceCommandAsync, PWSH case — replace:
go
case PWSH:
return fmt.Sprintf("function prompt() { & %s }", quotePwshOrElvishStr(scriptPath))
Non-negotiable, exact shape — do not refactor for "readability." Reasons pinned per statement:
$global:_ompOriginalPromptFunction guarded capture — only ever captures the true pre-omp prompt, on the firstInvoke-Expression of the session. Re-running the profile mid-session must not re-capture (would capture omp's own trampoline as "original").
User
go ahead with implementation
Assistant
Now omp.ps1 edits.
Assistant
Now OnRemove restore.
Assistant
Good. Now add the Go test.
Assistant
Now run build + tests.
Assistant
Build + tests pass. Now lint.
Assistant
Fix line length.
Assistant
Lint clean, tests pass. Now the live PWSH smoke test — build the binary and simulate the zoxide-wrapper scenario.
Assistant
All checks pass, including the trap Fable flagged (OnRemove no longer recurses through the wrapper). Clean up test artifacts.
Assistant
Diff looks clean, matches plan. Staging and committing.
Assistant
Committed as 7c85d430. Build, lint, unit tests, and a live PWSH smoke test (wrapper survives first draw, re-init, and Remove-Module without recursion) all pass. Not pushed — say the word if you want it up.
User
is still loading lazily?
Assistant
Yes. Trampoline still defers heavy & <scriptPath> (full New-Module/Import-Module) to first prompt draw — startup stays fast, same as before. Only change: that deferred install now writes $global:_ompPromptFunction (plain variable) instead of $Function:prompt directly, so it never re-touches the global prompt binding after the trampoline's one-time install. Lazy-load property fully preserved — confirmed by smoke test: draw1 triggers import (wrapper counter +1), draw2 is fast path with no re-import, both render correctly.
User
previously the prompt displayed black and I could start typing, that seems to be gone now?
Assistant
Real regression — good catch. My trampoline made draw 1 also call the real render (blocking on the actual oh-my-posh.exe invocation) instead of just doing the import and returning blank like the old placeholder did. That's exactly the "start typing right away" behavior async is supposed to give you, and I broke it. Fixing now.
Assistant
The return after the import makes draw 1 blank/instant again (import only, no render call), matching old behavior exactly. Draw 2+ still goes through the trampoline so the wrapper chain stays intact. Update the test and re-verify.
Assistant
Now update the smoke test to check draw 1 is blank and re-run it live.
Assistant
All green, blank-first-draw UX restored. Clean up and amend the commit.
Assistant
Fixup into that same not-yet-pushed commit, per your fixup-first preference for uncommitted work in the same PR scope.
Assistant
Squashed clean into 8912eada. Fix confirmed: draw 1 now stays blank/instant (async's "start typing right away"), draw 2+ renders through the trampoline with the wrapper chain intact.
User
code-review
/code-review
medium the commit with fable
Assistant
Good, HEAD is exactly the commit in question. Getting the isolated diff.
Assistant
Isolated diff (123 lines) captured. Launching 8 finder agents on Fable, medium effort, in parallel.
Assistant
Now the conventions angle — checking for applicable CLAUDE.md files first.
Assistant
No CLAUDE.md files anywhere in the repo — skipping conventions angle. Waiting on the 7 finder agents now.
Assistant
Reuse angle done. Waiting on remaining 6 finders.
Assistant
Efficiency angle done, negligible cost found plus one free micro-tweak. Waiting on remaining 5.
Assistant
Simplification angle done — found a genuinely redundant guard (real bug-adjacent simplification, worth verifying/fixing). Waiting on remaining 4.
Assistant
Removed-behavior angle surfaced real gaps — module removal can resurrect itself or poison the retry pointer. Waiting on remaining 3.
Assistant
Altitude angle flagged real design-scope tension — some of it (fish/bash/zsh unfixed) is explicitly a stated non-goal, but re-init-after-wrap and the untested trampoline/omp.ps1 name coupling look genuine. Waiting on the last 2.
Assistant
F1-F5 are substantial — waiting on the cross-file tracer, then verifying the strongest candidates (with live PowerShell tests where useful, especially F2's StrictMode claim).
Assistant
All 7 finder angles complete. Deduping and verifying the strongest correctness candidates now — 4 live-testable claims, all via Fable with actual PowerShell execution.
Assistant
4 verification agents running against real PowerShell ($? masking, sync→async switch, StrictMode, Remove-Module resurrection). Will report once all land.
Assistant
REFUTED: $? masking claim — tested with the exact trampoline text, correctly propagates in every shape. Good, one down. Waiting on the other 3.
Assistant
CONFIRMED: StrictMode breaks init entirely — real bug. Waiting on the other 2.
Assistant
CONFIRMED but narrow: Remove-Module resurrection, only reachable if prompt was deleted before init. Waiting on the last one (sync→async switch).
Assistant
Review done — 6 findings reported (3 confirmed correctness bugs, 1 dead-code simplification, 2 maintenance-risk/test-coverage gaps). All correctness candidates got live PowerShell verification against the exact committed code; one candidate ($? masking) was tested and refuted. Fish/bash/zsh sharing a related-but-different clobber pattern came up in the altitude angle too, but that's the explicitly scoped-out non-goal from the plan, not a finding against this diff.
Want me to fix the three confirmed bugs now?
User
Yes
Assistant
Implementing fixes for the 3 confirmed bugs — checking if the codebase already has a StrictMode-safe variable-read convention before inventing one.
Assistant
Now omp.ps1 — the three spots.
Assistant
Now the install site.
Assistant
Now the OnRemove block.
Assistant
Braces balance correctly. Now update the test.
Assistant
Now build, test, lint.
Assistant
Now build the binary and run a comprehensive live smoke test covering the original scenarios plus the 3 newly fixed bugs.
Assistant
Let me also re-verify the re-init case specifically, since the gating variable changed from _ompPromptFunction to _ompInitialized.
Assistant
All green. Clean up and commit.
Assistant
Fixing up into the same not-yet-pushed commit again, per your fixup-first preference.
Assistant
All 3 confirmed bugs fixed (StrictMode throw, sync↔async trampoline clobber, Remove-Module resurrection), plus the dead-code guard resolved itself as a side effect. Two lower-severity items (global-flag placement doc, test asserts string-equality not behavior) left as-is — flagged but not fixed, your call if you want those too.
Build, lint, unit tests, and 12 live PowerShell scenarios (original wrapper-survival/blank-first-draw/re-init/removal, plus StrictMode, sync→async switch, and falsy-original removal) all pass. Squashed into 9f012815 via fixup+autosquash.
$global:_ompAsyncInit fits the existing convention in this file (_ompJobCount, _ompStreaming, etc. — omp.ps1:20-27), cheaper than adding a param() block to a script currently invoked via &.
Result: $Function:prompt is written exactly once, synchronously, at Invoke-Expression time — same guarantee sync mode already has. Whatever wraps prompt (zoxide, anything) keeps a live reference to the trampoline P0, and P0's behavior only gets faster after first call (skips the import branch) — it never gets replaced out from under a wrapper. This also fixes any other prompt-wrapping tool, not just zoxide, and fixes tools that wrap prompt even after the first render (there's nothing left to clobber them).
Bonus: transient-prompt repaints and streaming's InvokePrompt() calls go through the same named prompt resolution, so they'd also start correctly running through zoxide's hook — currently broken silently, same root cause.
Loose ends worth resolving before implementing
OnRemove/theme-reload cleanup (omp.ps1:1331): currently restores $Function:prompt = $script:OriginalPromptFunction. For async mode, this needs to instead reset $global:_ompPromptFunction back to that backup, since the trampoline (not the module) now owns the Function:prompt identity permanently.
$script:OriginalPromptFunction capture (omp.ps1:41): in async mode this runs deep inside the trampoline's first call, so it actually captures whatever wrapped P0 (e.g., Z/zoxide) at that instant — correct semantically, just needs the restore path above to use it.
Scope: this proposal is PWSH-only, as asked. zsh/bash/fish async paths re-source their whole init script on every prompt cycle (init.go:286-291) — different shape, not looked at here.
Want me to turn this into a real plan (Phase 2) and implement, or dig into the zsh/bash/fish shape first?
$global:_ompPromptFunction = $null unconditional reset on every Invoke-Expression — fixes re-init: editing config/features overwrites the same cache file path in place; without this reset the trampoline's if (-not $global:_ompPromptFunction) guard never re-fires and the edited script never loads.
Trampoline body: skipped if body + bare-variable if conditions preserve $? across the delegation on the steady-state (fast) path — verified empirically. Any executed statement (e.g. an assignment) ahead of the delegation resets $? to $true and silently breaks Update-PoshErrorCode's status segment (omp.ps1:301) on every prompt. Do not add anything between prompt entry and the final delegation.
Guarded delegation (if ($global:_ompPromptFunction) { & ... }) — without the guard, a failed deferred import leaves the variable $null and every subsequent draw throws.
2. omp.ps1 near line 41 — before the existing $script:OriginalPromptFunction = $Function:prompt backup, add:
powershell
# Async mode: state is threaded through $global:_ompAsyncInit (set by the
# trampoline in the profile) and consumed once here, then cleared so a later
# *sync* re-source of this same file takes the sync branch below.
$script:AsyncInit = [bool]$global:_ompAsyncInit
$global:_ompAsyncInit = $false
Do NOT add _ompAsyncInit to the existing global-enablers block (omp.ps1:20-27) — that block runs after the trampoline has already set the flag and would stomp it back to unset before this line ever sees it.
Also add a one-line comment on the existing $script:OriginalPromptFunction backup noting it's vestigial in async mode (the trampoline's $global:_ompOriginalPromptFunction is authoritative there) — prevents a future "looks unused" flag.
No change needed to Export-ModuleMember -Function "prompt" (omp.ps1:1371) — when the async branch runs, no function named prompt exists in the module, so the export is a silent no-op. Add a one-line comment there too, same reason.
Restoring from $script:OriginalPromptFunction in async mode would recurse infinitely (it captured the wrapper chain, e.g. zoxide, not the true original — verified). Must restore from $global:_ompOriginalPromptFunction instead.
5. pwsh_test.go — add a test asserting the exact literal sourceCommandAsync(PWSH, "<path>") output (pin the string from step 1 verbatim, guards against silent drift on future edits).
Constraints / style
Follow PowerShell skill and Go skill conventions already in these files.
Match existing $global:_ompXxx naming convention exactly (_ompOriginalPromptFunction, _ompPromptFunction, _ompAsyncInit).
No param() block added to omp.ps1 — state passes via global flags per existing file convention, not script parameters.
Non-goals
zsh/bash/fish async paths — different mechanism (re-source every precmd cycle), not examined, not touched.
Streaming feature — inherits the fix for free (it's just a feature line inside the now-correctly-gated deferred script); no direct changes needed, verify only.
No redesign of option A vs B — B is decided.
Verification
go build ./... and go test ./src/shell/... — must pass, including the new pwsh_test.go assertion.
golangci-lint run (per repo convention) — must pass clean, no --no-verify.
Manual PWSH smoke test (I have PowerShell tool access, will run live): simulate the zoxide-wrapper scenario — Invoke-Expression the new async output, wrap prompt with a counting stub (mimics zoxide), trigger a rendered draw, confirm the wrapper survives and its counter increments across multiple draws. Repeat for: re-running the async line mid-session (re-init doesn't wedge), a forced import failure (guarded delegation doesn't throw), and Remove-Module oh-my-posh-core (OnRemove restores the true original, wrapper chain intact, no recursion).
Single task — the three files are tightly coupled (correctness depends on exact interplay between the emitted line and the two omp.ps1 edit sites), splitting invites drift on the pinned literals. I'll execute directly (already at implementer tier), main working tree, no worktree isolation needed — no parallel work to conflict with.
Go ahead with implementation, or want changes to the plan first?