Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(context-meter): draw the ring track from the foreground, not a border token (#16534)

george larson2e7136cb6ae6
diff --git a/__tests__/components/features/chat/components/context-window-meter.test.tsx b/__tests__/components/features/chat/components/context-window-meter.test.tsx
index 1157844ba6..b6542d2bc2 100644
--- a/__tests__/components/features/chat/components/context-window-meter.test.tsx
+++ b/__tests__/components/features/chat/components/context-window-meter.test.tsx
@@ -96,6 +96,32 @@ describe("ContextWindowMeter", () => {
);
});
+ // The trigger must share the actions-row icon-button hover fill. Its own
+ // `--oh-interactive-hover` fill was both wrong for this surface (that token
+ // is for 800 backgrounds; the composer is `--oh-surface`) and identical in
+ // value to `--oh-border`, which the ring track used, so hovering erased the
+ // track. `context-window-ring.test.tsx` asserts the resulting contrast.
+ it("uses the shared chat-input icon button styling for its hover fill", () => {
+ useMetricsStore.setState({
+ cost: null,
+ max_budget_per_task: null,
+ usage: {
+ prompt_tokens: 0,
+ completion_tokens: 0,
+ cache_read_tokens: 0,
+ cache_write_tokens: 0,
+ context_window: 1_000_000,
+ per_turn_token: 198_500,
+ },
+ });
+
+ renderWithProviders(<ContextWindowMeter />);
+
+ const trigger = screen.getByTestId("context-window-meter");
+ expect(trigger).toHaveClass("hover:bg-white/10");
+ expect(trigger.className).not.toContain("--oh-interactive-hover");
+ });
+
it("opens a popover with compact usage details", () => {
useMetricsStore.setState({
cost: null,
diff --git a/__tests__/components/features/chat/components/context-window-ring.test.tsx b/__tests__/components/features/chat/components/context-window-ring.test.tsx
new file mode 100644
index 0000000000..34e1940cc2
--- /dev/null
+++ b/__tests__/components/features/chat/components/context-window-ring.test.tsx
@@ -0,0 +1,136 @@
+import { render, screen } from "@testing-library/react";
+import { describe, expect, it } from "vitest";
+import {
+ ContextWindowRing,
+ CONTEXT_WINDOW_RING_TRACK_ALPHA,
+} from "#/components/features/chat/components/context-window-ring";
+import { COLOR_THEMES } from "#/themes/color-themes";
+
+/**
+ * WCAG 2.1 SC 1.4.11 asks 3:1 for non-text contrast. The ring's track carries
+ * information (the arc's proportion is only readable against it), so it is held
+ * to that bar rather than treated as chrome.
+ */
+const MIN_RATIO = 3;
+
+/**
+ * Themes are read from `color-themes.ts`, not from a stylesheet. `index.css`
+ * ships the deepsea scale but `DEFAULT_COLOR_THEME` overrides it at runtime, so
+ * a stylesheet-derived assertion validates a palette no default install renders.
+ */
+const SURFACE_STOP = "--cool-grey-925"; // --oh-surface, the composer background
+const FOREGROUND_STOP = "--cool-grey-100"; // --oh-foreground, the neutral arc
+
+/** `chatInputIconButtonClassName` fills the trigger with `hover:bg-white/10`. */
+const HOVER_OVERLAY = "#FFFFFF";
+const HOVER_ALPHA = 0.1;
+
+function channels(hex: string): [number, number, number] {
+ const h = hex.replace("#", "");
+ return [0, 2, 4].map((i) => parseInt(h.slice(i, i + 2), 16)) as [
+ number,
+ number,
+ number,
+ ];
+}
+
+function relativeLuminance(hex: string): number {
+ const [r, g, b] = channels(hex).map((channel) => {
+ const c = channel / 255;
+ return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
+ });
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
+}
+
+function contrastRatio(a: string, b: string): number {
+ const [high, low] = [relativeLuminance(a), relativeLuminance(b)].sort(
+ (x, y) => y - x,
+ );
+ return (high + 0.05) / (low + 0.05);
+}
+
+/** Composite a translucent overlay onto an opaque backdrop. */
+function composite(overlay: string, backdrop: string, alpha: number): string {
+ const o = channels(overlay);
+ const b = channels(backdrop);
+ return `#${o
+ .map((channel, i) =>
+ Math.round(alpha * channel + (1 - alpha) * b[i])
+ .toString(16)
+ .padStart(2, "0"),
+ )
+ .join("")}`.toUpperCase();
+}
+
+describe("ContextWindowRing", () => {
+ it("draws the track and the arc in different tones", () => {
+ render(<ContextWindowRing percentage={5} />);
+
+ const track = screen.getByTestId("context-window-ring-track");
+ const arc = screen.getByTestId("context-window-ring-arc");
+
+ expect(track.style.stroke).not.toEqual(arc.getAttribute("stroke"));
+ });
+
+ it("derives the track from the foreground rather than a scale stop", () => {
+ render(<ContextWindowRing percentage={5} />);
+
+ // A stop-pinned track cannot hold across palettes; see the constant's docs.
+ expect(screen.getByTestId("context-window-ring-track").style.stroke).toEqual(
+ expect.stringContaining("var(--oh-foreground)"),
+ );
+ });
+
+ /**
+ * Read the alpha back out of what the component actually renders, so the
+ * contrast cases below describe the shipped track rather than a constant that
+ * could drift away from it.
+ */
+ function renderedTrackAlpha(): number {
+ render(<ContextWindowRing percentage={5} />);
+ const { stroke } = screen.getByTestId("context-window-ring-track").style;
+ const percent = stroke.match(/var\(--oh-foreground\)\s+([\d.]+)%/);
+ if (!percent) {
+ throw new Error(`track is not a foreground mix: ${stroke}`);
+ }
+ return Number(percent[1]) / 100;
+ }
+
+ it("renders the alpha it documents", () => {
+ expect(renderedTrackAlpha()).toBeCloseTo(CONTEXT_WINDOW_RING_TRACK_ALPHA, 5);
+ });
+
+ describe.each(Object.entries(COLOR_THEMES))(
+ "contrast under the %s palette",
+ (_key, theme) => {
+ const surface = theme.scale[SURFACE_STOP];
+ const arc = theme.scale[FOREGROUND_STOP];
+ const hoverFill = composite(HOVER_OVERLAY, surface, HOVER_ALPHA);
+
+ it("keeps the track legible against the composer surface", () => {
+ const track = composite(arc, surface, renderedTrackAlpha());
+
+ expect(contrastRatio(track, surface)).toBeGreaterThanOrEqual(MIN_RATIO);
+ });
+
+ it("keeps the track legible under the trigger's hover fill", () => {
+ const track = composite(arc, hoverFill, renderedTrackAlpha());
+
+ expect(contrastRatio(track, hoverFill)).toBeGreaterThanOrEqual(
+ MIN_RATIO,
+ );
+ });
+
+ it("keeps a neutral arc distinguishable from the track in both states", () => {
+ const alpha = renderedTrackAlpha();
+
+ expect(
+ contrastRatio(arc, composite(arc, surface, alpha)),
+ ).toBeGreaterThanOrEqual(MIN_RATIO);
+ expect(
+ contrastRatio(arc, composite(arc, hoverFill, alpha)),
+ ).toBeGreaterThanOrEqual(MIN_RATIO);
+ });
+ },
+ );
+});
diff --git a/src/components/features/chat/components/context-window-meter.tsx b/src/components/features/chat/components/context-window-meter.tsx
index f9a66b56bc..c068312a7a 100644
--- a/src/components/features/chat/components/context-window-meter.tsx
+++ b/src/components/features/chat/components/context-window-meter.tsx
@@ -11,12 +11,16 @@ import { StyledTooltip } from "#/components/shared/buttons/styled-tooltip";
import { I18nKey } from "#/i18n/declaration";
import { Divider } from "#/ui/divider";
import { cn } from "#/utils/utils";
+import { chatInputIconButtonClassName } from "#/utils/form-control-classes";
import {
formatCompactTokenCount,
getContextWindowUsagePercentage,
} from "#/utils/format-token-count";
import { getContextFillTone } from "#/components/features/conversation/usage-panel/context-meter";
-import { ContextWindowRing } from "./context-window-ring";
+import {
+ ContextWindowRing,
+ CONTEXT_WINDOW_TRACK_COLOR,
+} from "./context-window-ring";
const TONE_BAR_CLASS = {
neutral: "bg-foreground",
@@ -72,7 +76,7 @@ export function ContextWindowMeter() {
<button
ref={triggerRef}
type="button"
- className="flex size-8 items-center justify-center rounded-full hover:bg-[var(--oh-interactive-hover)] transition-colors"
+ className={cn(chatInputIconButtonClassName, "size-8")}
aria-label={`${t(I18nKey.CHAT_INTERFACE$CONTEXT_WINDOW_METER_LABEL)}: ${usagePercentLabel}`}
aria-expanded={isPopoverOpen}
aria-haspopup="dialog"
@@ -109,7 +113,8 @@ export function ContextWindowMeter() {
<button
type="button"
data-testid="context-window-meter-bar-button"
- className="relative h-1.5 w-full rounded-full bg-[var(--oh-border)] cursor-pointer"
+ className="relative h-1.5 w-full rounded-full cursor-pointer"
+ style={{ backgroundColor: CONTEXT_WINDOW_TRACK_COLOR }}
aria-label={t(I18nKey.COMMON$USAGE)}
onClick={(event) => {
event.preventDefault();
diff --git a/src/components/features/chat/components/context-window-ring.tsx b/src/components/features/chat/components/context-window-ring.tsx
index 84c3ab610e..20c650279d 100644
--- a/src/components/features/chat/components/context-window-ring.tsx
+++ b/src/components/features/chat/components/context-window-ring.tsx
@@ -4,6 +4,28 @@ import { getContextFillTone } from "#/components/features/conversation/usage-pan
const CONTEXT_WINDOW_RING_SIZE = 16;
const CONTEXT_WINDOW_RING_STROKE = 2;
+/**
+ * Opacity of the ring's unfilled track, as a fraction of `--oh-foreground`.
+ *
+ * The track is derived from the foreground rather than pinned to a scale stop.
+ * It carries information (the arc's proportion is only readable against it), so
+ * it is a foreground element, and every stop in the surface family sits close
+ * to the surfaces it delimits. Drawing it with `--oh-border` put it in that
+ * family: it was 1.57:1 against the composer at rest, and the trigger's hover
+ * fill resolves to the same stop, taking it to 1.00:1. No stop in that family
+ * fixes it, and no fixed stop holds across the three palettes in
+ * `color-themes.ts`, whose scales differ.
+ *
+ * Compositing the foreground over whatever the active theme paints keeps the
+ * track between surface and arc by construction rather than by coincidence.
+ * 42% is the value that maximises the worst case across the shipped palettes;
+ * `context-window-ring.test.tsx` asserts it per theme.
+ */
+export const CONTEXT_WINDOW_RING_TRACK_ALPHA = 0.42;
+
+/** Shared by the ring's track and the popover's usage bar, which had the same defect. */
+export const CONTEXT_WINDOW_TRACK_COLOR = `color-mix(in srgb, var(--oh-foreground) ${CONTEXT_WINDOW_RING_TRACK_ALPHA * 100}%, transparent)`;
+
const TONE_STROKE = {
neutral: "var(--oh-foreground)",
warning: "#f59e0b", // amber-500
@@ -38,8 +60,9 @@ export function ContextWindowRing({
cy={CONTEXT_WINDOW_RING_SIZE / 2}
r={radius}
fill="none"
- stroke="var(--oh-border)"
+ style={{ stroke: CONTEXT_WINDOW_TRACK_COLOR }}
strokeWidth={CONTEXT_WINDOW_RING_STROKE}
+ data-testid="context-window-ring-track"
/>
<circle
cx={CONTEXT_WINDOW_RING_SIZE / 2}
@@ -53,6 +76,7 @@ export function ContextWindowRing({
strokeDashoffset={dashOffset}
transform={`rotate(-90 ${CONTEXT_WINDOW_RING_SIZE / 2} ${CONTEXT_WINDOW_RING_SIZE / 2})`}
className="transition-[stroke-dashoffset,stroke] duration-300"
+ data-testid="context-window-ring-arc"
/>
</svg>
);