Skip to content

Fix Port Detection#5

Open
Biggy1606 wants to merge 4 commits intorsvedant:masterfrom
Biggy1606:port-detection-fix
Open

Fix Port Detection#5
Biggy1606 wants to merge 4 commits intorsvedant:masterfrom
Biggy1606:port-detection-fix

Conversation

@Biggy1606
Copy link
Copy Markdown

@Biggy1606 Biggy1606 commented Jan 27, 2026

Issue: Windsurf spawns language server on random port on startup on linux, here is my approach.
Note: Changes are Linux focused but contain logic for MacOS and Windows.

Changes:

  • Fix Port Detection: Now scans Windsurf logs to find the actual dynamic port, rather than assuming a fixed offset.
  • Improve Process Discovery: Refined process matching to correctly identify the active Windsurf language server and avoid false positives.
  • Add Logging: Introduced file-based logging to ~/.local/share/opencode/windsurf-plugin.log to help debug connection issues.

Crafted with Claude Opus 4.5

Summary by CodeRabbit

  • Improvements

    • Added timestamped local logging and richer startup/connection visibility to help diagnose proxy and service startup.
    • Improved detection and port discovery for the Windsurf language server for more reliable connectivity.
  • New Features

    • Added CodeiumPlugin as an alternate public entry point for the plugin.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jan 27, 2026

📝 Walkthrough

Walkthrough

Adds file-based timestamped logging and startup observability, switches Windsurf port discovery from lsof to parsing Windsurf log files with tightened process filtering, and exposes a new public alias export CodeiumPlugin for the existing plugin.

Changes

Cohort / File(s) Summary
Plugin runtime & public export
src/plugin.ts
Added pluginLog(message) writing timestamped entries to a local log file and stdout; added startup logs around proxy server creation and Windsurf credential attempts; added CodeiumPlugin = createWindsurfPlugin('codeium') export.
Windsurf auth & discovery
src/plugin/auth.ts
Introduced WINDSURF_LOG_PATHS constants and tightened process filtering to Windsurf-specific markers; replaced lsof-based port discovery with reading/parsing the latest Windsurf log entry for "Language server listening on random port at "; preserved public function signatures and adjusted version/CSRF extraction logic and error handling for missing log dirs.

Sequence Diagram

sequenceDiagram
    participant Plugin as Plugin
    participant OS as OS / ProcessList
    participant WindsurfLog as Windsurf Log
    participant Proxy as Proxy Server
    participant Creds as Credentials Store

    Plugin->>OS: Query processes (filter for Windsurf)
    OS-->>Plugin: Return Windsurf process info
    Plugin->>WindsurfLog: Read most recent logs from WINDSURF_LOG_PATHS
    WindsurfLog-->>Plugin: Return log entries
    Note over Plugin: Parse "Language server listening on random port at <port>"
    alt Port found
        Plugin->>Creds: Extract port + CSRF/API key
        Creds-->>Plugin: Return credentials
        Plugin->>Proxy: Start/configure proxy with discovered port
        Proxy-->>Plugin: Server started
        Plugin->>WindsurfLog: Write startup/connection log entry
        WindsurfLog-->>Plugin: Acknowledge write
    else Port not found
        Plugin->>Plugin: Log error about missing or unreadable logs
        Plugin-->>Proxy: Continue with fallback/no port (non-blocking)
    end
Loading

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰
A whisper in logs, a port that I find,
I hop through the traces, precise and kind.
New alias announced, a bright little sign—
The plugin hums steady, its pathways aligned. 🥕✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 71.43% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix Port Detection' directly and accurately summarizes the primary change in the pull request—migrating from a fixed port offset assumption to log-based dynamic port discovery.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@src/plugin/auth.ts`:
- Around line 149-193: getPort currently uses findstr on Windows which returns
all matches and then applies output.match(), yielding the first port instead of
the most recent; change the Windows branch handling (where grepCmd is set for
'win32' and findstr is used) to capture the full execSync output, split it into
lines, filter out empty lines, sort the lines lexicographically (ISO timestamps
make this valid), pick the last line, and then run the existing regex (/Language
server listening on random port at (\d+)/) against that last line to extract and
return the port; keep the non-Windows path as-is and preserve existing error
handling and WindsurfError behavior in getPort.
- Around line 94-118: getLanguageServerProcess currently performs case-sensitive
checks for "/windsurf/" and "--ide_name windsurf", causing misses; update both
branches to perform case-insensitive matching: in the Unix branch, change the
ps/grep pipeline to use case-insensitive matching (e.g., add grep -i or use -i
with grep -E for "/windsurf/|--ide_name windsurf"); in the Windows branch,
lowercase the WMIC command output and perform includes checks against lowercase
literals (or use a case-insensitive regex) so checks in getLanguageServerProcess
align with WINDSURF_LOG_PATHS and ensure getCSRFToken/getWindsurfVersion detect
the process reliably; keep references to getLanguageServerPattern and preserve
existing timeout/encoding behavior.
🧹 Nitpick comments (1)
src/plugin.ts (1)

18-53: Consider platform‑specific log directories.
~/.local/share is Linux‑centric; on macOS/Windows this ends up in a non‑standard location. A small helper keeps logs consistent with OS conventions.

♻️ Suggested refactor
-const LOG_FILE = path.join(os.homedir(), '.local', 'share', 'opencode', 'windsurf-plugin.log');
+const LOG_FILE = (() => {
+  const home = os.homedir();
+  switch (process.platform) {
+    case 'darwin':
+      return path.join(home, 'Library', 'Application Support', 'opencode', 'windsurf-plugin.log');
+    case 'win32':
+      return path.join(home, 'AppData', 'Local', 'opencode', 'windsurf-plugin.log');
+    default:
+      return path.join(home, '.local', 'share', 'opencode', 'windsurf-plugin.log');
+  }
+})();

Comment thread src/plugin/auth.ts
Comment thread src/plugin/auth.ts
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.

1 participant