Files
orca/src/shared/localhost-worktree-labels.ts
T
a56da57bb8 Add worktree labels for localhost ports (#6424)
* Add worktree labels for localhost ports

* Fix localhost label test fixtures

* Extend localhost worktree labels to agents

* Address localhost label review feedback

* Remove localhost label page injection; stream responses untouched

Drop the title/favicon HTML injection from the localhost label proxy.
The proxy now only relabels the hostname and pipes responses straight
through, so app CSP/cookies/bodies are preserved and large/streamed
responses are no longer buffered in memory.

- Normalize wildcard bind hosts (0.0.0.0 -> 127.0.0.1, :: -> ::1) before
  using them as a proxy connect target.
- Delete the favicon SVG generator and dead repoIcon/badgeColor plumbing.
- Remove orphaned LocalhostLabelMock i18n keys.

Co-authored-by: Orca <help@stably.ai>

* Harden localhost label proxy from review

- IPC register: restrict proxy target to loopback or a matching scanned
  workspace port (close open-proxy/SSRF surface from untrusted renderer).
- Proxy: guard against ERR_HTTP_HEADERS_SENT on mid-stream upstream error;
  add error/cleanup listeners on client request/response and upgrade socket.
- labelLocalhostUrl: fall back to the raw URL when the proxy rejects a
  target (e.g. https) instead of throwing.
- Port label route: include worktreePath so button-open and terminal/CLI
  paths produce the same label.
- Terminal OSC link hover: discard stale async tooltip results via a hover
  token, matching the WebLinks path.

Co-authored-by: Orca <help@stably.ai>

* Match default-port advertised hosts in localhost label target check

Co-authored-by: Orca <help@stably.ai>

* Consolidate duplicated localhost label helpers

- Move the loopback host set, loopback-URL parser, and wildcard
  connect-host normalizer into shared/localhost-worktree-labels.ts; proxy,
  runtime, terminal link routing, and the IPC guard now share one copy.
- Extract the port -> repo -> worktree -> project label-route lookup into
  workspace-port-localhost-label-selector.ts (a hook plus an imperative
  resolver), replacing the block triplicated across the ports surfaces.

Co-authored-by: Orca <help@stably.ai>

* Extract command-code prompt-status seed to its own module

Keeps launch-agent-in-new-tab.ts under the max-lines limit after the
localhost-hint additions, without a lint disable.

Co-authored-by: Orca <help@stably.ai>

* Remove agent-facing localhost mechanism

Orca does not mutate user prompts or inject prompt snippets, so drop the
localhost-open agent hint entirely:

- Remove appendLocalhostOpeningHint / includeLocalhostOpeningHint and the
  hint constant from tui-agent-startup; agent prompts are no longer rewritten.
- Remove the ORCA_LOCALHOST_OPEN env var from local agent terminals (it was
  only discoverable via the now-removed hint).
- Remove the orca localhost label|open CLI commands, their workspacePorts RPC
  methods, and the runtime labelLocalhostUrl/openLocalhostUrl methods.

The feature is now purely structural: the loopback label proxy plus the
ports-panel and terminal-link 'Open in Browser' paths, which surface a
clickable labeled URL without touching agent prompts.

Co-authored-by: Orca <help@stably.ai>

* Make localhost worktree labels opt-in (default off)

Serving a dev app under a different host than localhost:<port> can break
apps that bind cookies/sessions to localhost, so the feature should not
change existing users' Open-in-Browser behavior on upgrade.

- Default localhostWorktreeLabelsEnabled to false.
- Flip the gates to enable only when explicitly true (=== true / !== true)
  instead of treating undefined as enabled.
- Update the setting switch to checked only when explicitly enabled.
- Drop the now-unused runtime store settings field.

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Jinwoo-H <jinwoo0825@gmail.com>
Co-authored-by: Orca <help@stably.ai>
2026-06-29 12:27:30 -07:00

106 lines
3.2 KiB
TypeScript

const HOST_LABEL_MAX_LENGTH = 48
const TRAILING_MAIN_PATTERN = /(?:^|[-_\s/])main$/i
export const LOOPBACK_LOCALHOST_HOSTS = new Set(['localhost', '127.0.0.1', '0.0.0.0', '::1', '::'])
export function normalizeLocalhostHostname(hostname: string): string {
return hostname.replace(/^\[|\]$/g, '').toLowerCase()
}
// Why: only http(s) loopback URLs with an explicit port can be attributed to a
// scanned workspace port and labeled; everything else stays as-is.
export function parseLoopbackUrlWithPort(rawUrl: string): URL | null {
let url: URL
try {
url = new URL(rawUrl)
} catch {
return null
}
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
return null
}
if (!url.port || !LOOPBACK_LOCALHOST_HOSTS.has(normalizeLocalhostHostname(url.hostname))) {
return null
}
return url
}
// Why: 0.0.0.0 and :: are wildcard bind addresses, not connectable
// destinations (connecting to them fails on Windows). Normalize them to the
// matching loopback before using the host as a proxy target.
export function connectableLoopbackHost(hostname: string): string {
const bare = normalizeLocalhostHostname(hostname)
if (bare === '0.0.0.0') {
return '127.0.0.1'
}
if (bare === '::') {
return '::1'
}
return hostname
}
export type LocalhostWorktreeLabelInput = {
projectName: string
worktreeName: string
worktreePath?: string | null
repoId?: string | null
worktreeId?: string | null
}
export type LocalhostWorktreeLabelRoute = {
targetUrl: string
projectName: string
worktreeName: string
worktreePath?: string | null
repoId?: string | null
worktreeId?: string | null
}
export type LocalhostWorktreeLabelResult = {
url: string
label: string
}
export function slugifyLocalhostWorktreeLabel(value: string): string {
const normalized = value
.trim()
.toLowerCase()
.replace(/['"]/g, '')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
.slice(0, HOST_LABEL_MAX_LENGTH)
.replace(/-+$/g, '')
return normalized || 'workspace'
}
export function getLocalhostWorktreeHostLabel(input: LocalhostWorktreeLabelInput): string {
const projectSlug = slugifyLocalhostWorktreeLabel(input.projectName)
const shortWorktreeName = getLocalhostWorktreeShortName(input.worktreePath ?? input.worktreeName)
const worktreeSlug = slugifyLocalhostWorktreeLabel(shortWorktreeName)
// Why: primary worktrees need project context so every project does not collapse into "main".
if (worktreeSlug === 'main' || TRAILING_MAIN_PATTERN.test(input.worktreeName)) {
return slugifyLocalhostWorktreeLabel(`${projectSlug}-main`)
}
return worktreeSlug
}
function getLocalhostWorktreeShortName(worktreeName: string): string {
return (
worktreeName
.split(/[\\/]/)
.map((part) => part.trim())
.filter(Boolean)
.at(-1) ?? worktreeName
)
}
export function getLocalhostWorktreeRouteKey(route: LocalhostWorktreeLabelRoute): string {
if (route.worktreeId) {
return `worktree:${route.worktreeId}:${route.targetUrl}`
}
if (route.repoId) {
return `repo:${route.repoId}:${route.worktreeName}:${route.targetUrl}`
}
return `${route.projectName}:${route.worktreeName}:${route.targetUrl}`
}