Skip to content

fix(sandbox): restore port-sniffer phrase gate + cross-chunk carry - #5676

Merged
guitavano merged 2 commits into
mainfrom
guitavano/dev-server-preview-stuck
Aug 4, 2026
Merged

guitavano merged 2 commits into
mainfrom
guitavano/dev-server-preview-stuck

Conversation

@guitavano

@guitavano guitavano commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem

TanStack/Vite migration previews (e.g. granadobr-tanstack) are stuck on the boot skeleton / "Server is starting…" even though the dev terminal shows VITE ready on http://localhost:5173/.

Reading the daemon log directly (fetch("/_sandbox/events") on the preview host, bypassing the Studio /api/.../events proxy) shows the smoking gun:

[daemon] proxy error GET / port=3000  dial tcp 127.0.0.1:3000: connect: connection refused
phase: "starting"   (never "running")   ·   132× refused   ·   0× "[probe] server responded"

The daemon dials 3000 while Vite serves 5173.

Root cause — a regression in the TS→Go daemon rewrite

The Go port sniffer became the default in #5568 and the TS daemon was deleted in #5575. The Go portsniffer.go was an incomplete port of the TS port-sniffer.ts — it dropped two safeguards:

Safeguard TS (worked) Go (regressed)
Phrase gate (Local: / Listening on on the URL line) ✅ ❌ bare http://localhost:N matches
Cross-chunk carry (PTY line-split reassembly) ✅ 200-char tail per source ❌ matches each chunk in isolation

Either gap makes the sniffer miss Vite's 5173. getDevPort() then falls back to the configured application.port (DEFAULT_DEV_PORT = 3000) → the proxy (proxy.go) and probe (probe.go) dial a dead port → StartingHTML + lifecycle stuck at starting. Apps that bind the injected PORT (e.g. casaevideo-tanstack, which serves on 3000) were unaffected; apps relying on the sniffer to learn a dynamic port broke.

Fix

Restore both safeguards in portsniffer.go:

  • Phrase gate — the bind URL must follow Local: / Listening on on the same line, so an early bare localhost:3000 (a framework echoing PORT, a proxied fetch, a sibling process) can't hijack the lock.
  • Cross-chunk carry — carry a bounded, ANSI-stripped tail per source and match the concatenation. The port must also be terminated by / or whitespace, so a chunk splitting mid-number (...:51 | 73/) waits for its continuation instead of locking the truncated 51.

Tests

proc_test.go gains coverage for both failure modes:

  • TestPortSnifferIgnoresBareURLBeforeBanner — an early bare localhost:3000 does not lock; the later Local: …:5173/ does.
  • TestPortSnifferSpansChunkBoundary — a split bind line locks once completed.

Regex + carry logic validated against all cases (existing lock-in, phrase gate, mid-port split, colon split, casaevideo Local:3000).

Testing notes

Go isn't installed locally, so I couldn't run go test here — relying on the sandbox-daemon CI (go vet / go test -race / go build). Logic was validated with an equivalent-regex simulation covering every scenario.

🤖 Generated with Claude Code


Summary by cubic

Fixes dev previews getting stuck by restoring safeguards in the Go port sniffer. It now reliably detects the real dev server port (e.g., Vite 5173 and Next/Bun without a trailing slash) instead of falling back to 3000.

  • Bug Fixes
    • Reinstate phrase gate: only lock URLs on lines with “Local:” or “Listening on”; ignore bare or sibling localhost URLs.
    • Handle split PTY chunks with per-source carry; require a non-digit terminator so mid-number splits don’t lock.
    • Consume ANSI ESC in color codes and clear carry on Reset; updated portsniffer.go and expanded tests in proc_test.go (colored no-slash, non-slash terminator, reset carry, sibling-before-banner, per-source isolation).

Written for commit eb8b7f5. Summary will update on new commits.

Review in cubic

The Go daemon's port sniffer (default since #5568, TS daemon deleted in
#5575) was an incomplete port of the TypeScript one: it dropped two
safeguards, so a Vite dev server that binds a non-config port (e.g. 5173)
is no longer detected. `getDevPort()` then falls back to the configured
`application.port` (3000) — the daemon proxies/probes 127.0.0.1:3000,
gets connection-refused forever, serves "Server is starting…" and never
leaves the `starting` lifecycle phase. The preview is stuck on the boot
skeleton even though `vite dev` is ready.

Restore both safeguards from the TS sniffer:

- **Phrase gate.** The bind URL must be preceded by `Local:` / `Listening
  on` on the same line. Without it, a bare `http://localhost:3000` line
  (a framework echoing the injected PORT, a proxied fetch, a sibling
  process) hijacks the lock before Vite's real `Local: http://localhost:5173/`.

- **Cross-chunk carry.** PTY reads aren't line-buffered, so the bind line
  can split across chunks. Carry a bounded, ANSI-stripped tail per source
  and match the concatenation. Also require a `/` or whitespace after the
  port so a split mid-number ("...:51" | "73/") waits instead of locking
  the truncated "51".

Tests cover both failure modes (bare-3000 hijack, split bind line).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@github-actions github-actions Bot added the claude PR authored by a coding agent label Aug 4, 2026
Review of the previous commit found the `[/\s]` trailing requirement
regressed the no-trailing-slash frameworks (next, bun) it claimed to
support: the ANSI stripper matches the bracket body but not the ESC, so a
colored `http://localhost:3000\x1b[0m\n` strips to `...:3000\x1b\n` — the
ESC sits between the port and the newline, `[/\s]` rejects it, and the
port is never sniffed (silently falling back to the wrong configured
port — the exact failure the sniffer exists to prevent).

- Require a non-digit terminator (`[^\d]`) instead of `[/\s]`. Still fixes
  the mid-number split (a buffer ending on digits has no following char →
  waits), but locks on any non-digit — `/`, `)`, `,`, whitespace,
  newline, or a leftover ESC — matching the eager TS behavior for the
  frameworks a strict `[/\s]` would have dropped.
- Make ansiPattern consume the ESC when present (`\x1b?`) so a stripped
  color code leaves nothing between the port and its terminator.

Tests: colored no-trailing-slash, non-slash terminator, Reset clears
carry, sibling URL before the banner (line-bound `[^\n]*?`), and
per-source carry isolation.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@guitavano
guitavano merged commit ee05491 into main Aug 4, 2026
24 checks passed
@guitavano
guitavano deleted the guitavano/dev-server-preview-stuck branch August 4, 2026 18:15
decocms Bot pushed a commit that referenced this pull request Aug 4, 2026
PR: #5676 fix(sandbox): restore port-sniffer phrase gate + cross-chunk carry
Bump type: patch

- @decocms/sandbox (packages/sandbox/package.json): 1.37.0 -> 1.37.1
- deploy/helm/sandbox-env (chart 0.10.9) (deploy/helm/sandbox-env/values.yaml deploy/helm/sandbox-env/Chart.yaml): image.tag/appVersion -> 1.37.1

Deploy-Scope: both
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

claude PR authored by a coding agent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant