Files
Triple-C/app/src/components/projects/home/config/AuthBridgeRow.tsx
T
shadow-testandClaude Opus 5 22d142c70d Shift+Enter newline, OAuth URL truncation, and the auth bridge toggle
Three fixes that all land on the same journey: sign in, paste a prompt,
and have the terminal behave the way every other Claude Code host does.

Shift+Enter inserts a newline
-----------------------------
xterm.js does not consult `shiftKey` for Enter (`Keyboard.ts`, case 13),
so Shift+Enter was byte-identical to Enter and submitted the prompt.
Both terminals now send `\x1b\r` (ESC+CR) instead, which Claude Code
parses as return+meta — the same bytes its own `/terminal-setup` writes
into the VS Code, Cursor, Alacritty and Zed keymaps, so this is in-band
rather than a guess. Not `\n`: Claude Code accepts it, but a shell would
run the line, so the two session types would diverge. Bound in Claude
sessions only for that reason.

`entrypoint.sh` sets `shiftEnterKeyBindingInstalled` in `~/.claude.json`
so the CLI stops printing its "run /terminal-setup" tip. Purely
cosmetic — the decoding is unconditional either way.

Alt+Enter has always done the same thing (xterm ESC-prefixes on altKey)
and was simply never documented. It is now, along with the rest.

OAuth login URL truncation
--------------------------
Two producers wrote one toast slot, last-writer-wins. The OSC 7777 relay
delivers the URL base64-encoded and therefore exact; ~300 ms later the
screen-scraper's debounce fired and overwrote it with a truncated guess
at the same link — a URL that parses, points at the right host, and
authorises nothing. The user is the one who has to notice.

Why the scraper truncated: `ANSI_RE` strips OSC sequences wholesale,
including the OSC 8 hyperlink whose parameter carries the complete URL.
Claude Code slices the *visible* text of that hyperlink to the terminal
width while every emission carries the whole URL in its parameter. The
backend already knew this (`commands/auth_token_commands.rs`); the
frontend did not.

- `urlDetector` now reads OSC 8 targets out of the raw buffer before
  stripping, filtered by a port of `usable_sign_in_link`, and tags every
  candidate with its provenance.
- The prompt slot gained `supersedes`: better provenance always wins,
  worse never does, and between equals only a candidate that *extends*
  what is showing may replace it. That last rule is `extendsUrl`,
  factored out of `pickSignInUrl` rather than copied — same rule, same
  reason, one implementation.
- `flatten` splits on a bare `\r` as well as on `\r?\n`, so a
  `\r`-repainted TUI frame no longer inflates a line past the width and
  suppresses a join that should have happened; and the width is now
  sampled at `feed()` rather than read at `scan()`, so a resize inside
  the 300 ms debounce cannot reassemble 80-column text against a
  120-column rule.

Also corrects the comment claiming `acquire_claude_token` enables the
auth bridge. It deliberately does not, and the module comment in
`auth_token_commands.rs` explains at length why not.

The auth bridge toggle
----------------------
`setAuthBridgeEnabled` and `getAuthBridgeStatus` had zero call sites:
the Rust was complete, the IPC wrapper shipped, and there was nowhere to
click — so the docs told users to "enable the Auth Bridge" for a switch
that did not exist. `AuthBridgeRow` is that switch, in Config → Runtime.
It deliberately does not go through the tab's stopped-only save: the
dedicated command exists so the bridge can be flipped while a login is
hanging in a running container, which is the only moment anyone reaches
for it.

It also subscribes to `auth-bridge-changed`, which the poller has been
emitting to nobody — so a host port the bridge could not take was a
completely silent failure, indistinguishable from a login that hung.

`tunnel.rs` promotes the best-effort `::1` bind failure from debug to a
warning recorded on the port. Half-bound is the failure mode that looks
like success: the status says bridged, and a client that resolves
`localhost` to `::1` without falling back is still refused.

Finally, for a recognised Anthropic sign-in URL the toast now leads with
"In container" and demotes the host "Open". The callback listener is
inside the container, so the container-side browser closes the loop with
no host round trip and no auth bridge; the host button stays as the
fallback. Ordinary URLs are unchanged.

Tests: 402 frontend (was 359), 285 Rust (unchanged).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GBq2rGum6GX7xXgsas1fDc
2026-08-23 08:31:39 -07:00

199 lines
7.1 KiB
TypeScript

import { useCallback, useEffect, useState } from "react";
import { listen } from "@tauri-apps/api/event";
import {
getAuthBridgeStatus,
setAuthBridgeEnabled,
} from "../../../../lib/tauri-commands";
import type {
AuthBridgeChangedEvent,
AuthBridgeStatus,
Project,
} from "../../../../lib/types";
import { SwitchRow } from "../../../ui/Field";
import StatusIndicator, { type StatusTone } from "../../../ui/StatusIndicator";
import Toggle from "../../../ui/Toggle";
/** Emitted by `auth_bridge/mod.rs` whenever the port or conflict set changes. */
const AUTH_BRIDGE_EVENT = "auth-bridge-changed";
const LABEL = "Auth bridge";
/**
* What the indicator beside the switch says.
*
* Split out so the interesting part — that a conflict is a *visible* failure —
* can be tested without a container. Every branch pairs a glyph with a word;
* none of them are distinguished by colour alone.
*/
export function bridgeIndicator(
status: AuthBridgeStatus | null,
containerRunning: boolean,
): { tone: StatusTone; label: string } {
if (!status) return { tone: "unknown", label: "Checking" };
if (!status.enabled) return { tone: "off", label: "Off" };
// A conflict means a login is in progress and its port could not be taken —
// the one state where doing nothing is the wrong answer, and until now the
// one state nothing in the app reported at all.
if (status.conflicts.length > 0) {
return { tone: "error", label: "Port conflict" };
}
if (status.active_ports.some((p) => p.ipv6_warning)) {
return { tone: "busy", label: "IPv4 only" };
}
if (status.active_ports.length > 0) {
const n = status.active_ports.length;
return { tone: "running", label: `Bridging ${n} port${n === 1 ? "" : "s"}` };
}
// Enabled but holding nothing. Normal: there is only something to bridge
// while a login is actually waiting for a callback.
if (!containerRunning) {
return { tone: "stopped", label: "Waiting for the container" };
}
return { tone: "ok", label: "Watching" };
}
/**
* The switch for `auth_bridge_enabled`, and the only place it can be changed.
*
* Two things here are deliberate and easy to undo by accident:
*
* - **It does not go through the Config tab's `save`.** That path is gated on
* a stopped container, because almost everything else in the tab is baked
* into the container at creation. This is not: the bridge is entirely
* host-side, and `set_auth_bridge_enabled` exists precisely so it can be
* flipped *while a login is hanging*, which is when the user finds out they
* need it. Routing it through the generic save would make it unreachable at
* the only moment it matters.
* - **It subscribes to `auth-bridge-changed`.** The poller already emits the
* bridged-port and conflict sets on every change and, before this, nothing
* listened — so a host port the bridge could not take was a completely
* silent failure, indistinguishable from a login that simply hung.
*/
export default function AuthBridgeRow({ project }: { project: Project }) {
const projectId = project.id;
const containerRunning = project.status === "running";
const [status, setStatus] = useState<AuthBridgeStatus | null>(null);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
let cancelled = false;
setStatus(null);
setError(null);
getAuthBridgeStatus(projectId)
.then((s) => {
if (!cancelled) setStatus(s);
})
.catch((e) => {
if (!cancelled) setError(String(e));
});
return () => {
cancelled = true;
};
}, [projectId]);
useEffect(() => {
let cancelled = false;
let unlisten: (() => void) | undefined;
listen<AuthBridgeChangedEvent>(AUTH_BRIDGE_EVENT, (event) => {
if (event.payload.project_id !== projectId) return;
setStatus(event.payload.status);
})
.then((un) => {
if (cancelled) un();
else unlisten = un;
})
.catch((e) => console.error("Auth bridge event subscription failed:", e));
return () => {
cancelled = true;
unlisten?.();
};
}, [projectId]);
const toggle = useCallback(
async (next: boolean) => {
setBusy(true);
setError(null);
// Optimistic, so the switch responds even though enabling has to await a
// container probe. The command's return value replaces it either way.
setStatus((s) => (s ? { ...s, enabled: next } : s));
try {
setStatus(await setAuthBridgeEnabled(projectId, next));
} catch (e) {
setStatus((s) => (s ? { ...s, enabled: !next } : s));
setError(String(e));
} finally {
setBusy(false);
}
},
[projectId],
);
// Fall back to the persisted flag until the first status arrives, so the
// switch never renders in the wrong position.
const enabled = status?.enabled ?? project.auth_bridge_enabled;
const indicator = bridgeIndicator(status, containerRunning);
return (
<SwitchRow
label={LABEL}
hint={
<>
Mirrors a port a program inside the container is listening on onto the
host's <code>127.0.0.1</code>, so a browser OAuth callback can reach
the listener waiting inside the container {" "}
<code>claude login</code>, <code>aws sso login</code> and{" "}
<code>gh auth login</code> all work this way, and without it the
browser calls back into nothing and the login hangs. Host-side only:
it never recreates the container, and it can be switched on while one
is running. A bridged port is unauthenticated and reachable by any
local process for as long as the in-container listener exists, so
leave it off unless you need it.
<span className="mt-1 flex flex-wrap items-center gap-x-2 gap-y-1">
<StatusIndicator tone={indicator.tone} label={indicator.label} />
{status?.active_ports.map((p) => (
<span
key={p.port}
className="font-mono text-[var(--text-secondary)]"
title={p.ipv6_warning ?? `Bound on host 127.0.0.1:${p.port}`}
>
127.0.0.1:{p.port}
{p.ipv6_warning ? " (IPv4 only)" : ""}
</span>
))}
</span>
{status?.conflicts.map((c) => (
<span
key={c.port}
className="mt-1 block text-[var(--error)]"
role="status"
>
Port {c.port}: {c.reason}
</span>
))}
{status?.active_ports
.filter((p) => p.ipv6_warning)
.map((p) => (
<span key={p.port} className="mt-1 block text-[var(--warning)]">
Port {p.port}: {p.ipv6_warning}
</span>
))}
{error && (
<span className="mt-1 block text-[var(--error)]">{error}</span>
)}
</>
}
control={
<Toggle
label={LABEL}
checked={enabled}
// Never gated on the container being stopped — see the note above.
disabled={busy}
onChange={toggle}
/>
}
/>
);
}