fix(server): persist worktree runtime port when ambient PORT does not match (#1849)#1930
Conversation
|
cc @devinfoley @cryppadotta: This fixes issue #1849 regarding the missing runtime port persistence during worktree spawns when bounded by an ambient PORT. Could you please take a look, approve, and merge it when you have a moment? |
Greptile SummaryThis PR fixes a bug (#1849) where worktrees would silently skip persisting their auto-assigned collision-avoiding ports to Confidence Score: 5/5Safe to merge — the change is small, well-scoped, and all relevant edge cases (empty, malformed, matching, non-matching PORT) are correctly handled. All remaining findings are P2 (description accuracy, missing thinking path). The code logic is correct and the fix directly addresses the described bug without touching unrelated paths. No files require special attention. Important Files Changed
Prompt To Fix All With AIThis is a comment left during a code review.
Path: server/src/worktree-config.ts
Line: 443-449
Comment:
**PR description mentions regex — none exists in implementation**
The PR description states _"The regex handles parsing edge cases cleanly"_, but the actual implementation uses `Number()` + `Number.isInteger()` — there is no regex involved. The code is fine as written, but the description is misleading. Consider updating the PR description to reflect the actual parsing approach (`Number()` / `Number.isInteger()`) so reviewers and future maintainers aren't confused when auditing the change.
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix(server): persist worktree runtime po..." | Re-trigger Greptile |
|
Good catch! I've updated the PR description to be accurate—thanks for catching that typo. The code implementation correctly handles parsing with |
What was done
Replaced the strict
!nonEmpty(process.env.PORT)guard inmaybePersistWorktreeRuntimePortswith a newisPortPinnedByRuntimeEnvhelper function. This function checks ifprocess.env.PORTis set, but only suppresses persisting the port to configuration if the ambientPORTmatches the newly allocatedselectedPort.Why it matters
Fixes issue #1849. Previously, if an ambient
PORTenvironment variable was exported globally (like inheriting from the shell running the parent workspace), worktrees would silently fail to write their collision-avoiding ports (e.g. 3103 instead of 3100) back to their respective localconfig.jsonfiles. This resulted in orphaned sub-worktrees and lost port tracking on reboot. With this fix, worktrees correctly persist their assigned ports even while nested under an inherited environment variables stack, while continuing to respect manual, explicit pinning.How to verify
export PORT=3100.3103).config.jsonfor that worktree inside.paperclip/worktrees/.{"server": {"port": 3103}}rather than dropping the write operation.Risks
None expected. The
Number()andNumber.isInteger()checks handle parsing edge cases cleanly, defaulting robustly to preventing writes ifprocess.env.PORTis somehow malformed (e.g., set to a non-integer), ensuring absolute safety during misconfigurations.