Skip to content

fix: honor the OSC 111 background reset - #312

Merged
umputun merged 3 commits into
masterfrom
osc111-reset
Jul 28, 2026
Merged

umputun merged 3 commits into
masterfrom
osc111-reset

Conversation

@umputun

@umputun umputun commented Jul 28, 2026

Copy link
Copy Markdown
Owner

agterm honored OSC 11 (set background) but not OSC 111 (reset), so a TUI that colors the background and restores it on exit left the pane stuck on its color. Reported with Docker Sandboxes sbx, a Bubble Tea v2 app whose renderer emits ansi.ResetBackgroundColor on close.

Root cause

Self-inflicted, not a libghostty limitation. The OSC 11 handler pushed a per-surface config carrying background = <the OSC color>. That reaches ghostty's Termio.changeConfig, which re-seeds terminal.colors.background.default from the config on every surface update. DynamicRGB.reset() is override = default, so OSC 111 restored the program's own color. The follow-up COLOR_CHANGE then carried the same hex and was dropped by the dedupe guard, and an OSC 11 query still reported the program's color, because get() is override orelse default and both layers held it.

Fix

The renderer takes the background color from terminal state and the alpha from background-opacity, two independent sources. So the overlay only has to lift the opacity that window translucency pins to 0, and never restates the color. default stays at the theme and the reset reverts.

libghostty signals set and reset through the same action with no distinguishing flag, so the new host-free OSCBackgroundPolicy identifies a reset by the reported color equalling the surface's baseline, and owns the per-prompt dedupe that used to sit inline in the callback. While an OSC overlay is installed that baseline is the theme, since the overlay emits no background key.

Two adjacent font bugs surfaced while wiring the reset path and are fixed here. A --background-color overlay and a split or scratch pane each lost their live cmd-+/- zoom on a config re-apply, because those panes have onFontSizeChange unwired on purpose and their zoom is readable only from the surface. Both paths now compose from the same currentEffectiveFontSize() chain. The split/scratch case predates this branch and also fired on session.background set/clear.

Verified

A/B against a pre-fix build of the same worktree, same harness: the background query after OSC 111 reported rgb:1212/3434/5656, the program's color, before the change and the theme color after. Checked at full opacity and under translucency.

swift test 1929, make test-app 46, make lint --strict clean. 12 new host-free tests cover the overlay emitter and the set/reset/dedupe routing.

Note on scope: an explicit session.background still cannot override a live OSC 11, since the program's override layer masks the config default and the pinned libghostty exposes no API to clear it. That limitation is unchanged and documented.

Fix #309

umputun added 3 commits July 28, 2026 16:23
the OSC 11 handler pushed a per-surface config carrying `background = <the osc
color>`, which reaches ghostty's `Termio.changeConfig` and re-seeds
`terminal.colors.background.default` on every surface update. That overwrote the
layer OSC 111 restores (`DynamicRGB.reset()` is `override = default`), so a TUI
that colored the background and reset it on exit left the pane stuck on its
color, and an OSC 11 query still reported it.

the renderer takes the background color from terminal state and the alpha from
`background-opacity`, so the overlay only has to lift the opacity that window
translucency pins to 0. `WatermarkConfig.oscBackgroundOverlayText` emits no
`background` key, so `default` stays at the theme color and the reset reverts.

libghostty reports set and reset through the same action with no flag telling
them apart, so the host-free `OSCBackgroundPolicy` spots a reset by comparing
against the surface's own baseline color (a `.color` watermark, an overlay's
`--background-color`, else the theme). It also owns the per-prompt dedupe that
used to sit inline in the callback.

checked against a pre-fix build with the same harness: the background query
after OSC 111 reported the program's color before, the theme color after.

Related to #309
review follow-ups to 2ab231c.

`baselineBackgroundHex()` returned the surface's own color (a `.color` session
watermark, or an overlay's `--background-color`), but the OSC overlay emits no
`background` key, so while it is installed the config in force is the base one
and ghostty seeded `default` from the theme. A reset on those surfaces reported
the theme, matched neither the baseline nor the live OSC color, and routed to
`.apply` instead of `.reset` - leaving the overlay installed and the latch
pinned for a program that had already exited, so every reload, opacity tick and
dashboard toggle reinstalled it. Pixels were unaffected either way, since the
un-clearable `override` masks `default`. `OSCBackgroundPolicy.baseline` now owns
the rule and is unit-tested.

`applyOverlayBackgroundColor` also runs on the reset path now, so it takes the
font from `currentEffectiveFontSize()` instead of the creation size. Without
that, a `--background-color` overlay zoomed with cmd-+/- snapped back to its
opening size when a program reset the background.
`applyWatermarkFromSession` composed its font from `dashboardFontOverride ??
session.fontSize`, never reaching `currentFontSize()`. A split or scratch pane
has its `onFontSizeChange` unwired on purpose (`ControlServer+SurfaceIO.font`),
so its cmd-+/- zoom is live-only and readable nowhere else, and every re-apply
snapped it back to the session's size or the config default.

that was already true for `session.background` set/clear; the OSC reset path
added a second trigger, since it routes a session or watermarkSession surface
through the same method. Both now compose from `currentEffectiveFontSize()`,
the chain the apply path and the overlay path already use.

the zoom-clearing reload contract is unaffected: it nils `session.fontSize` and
broadcasts the shared config before `reapplySessionConfigIfNeeded` runs, so the
live read returns the freshly applied default and re-emitting it is a no-op.
Copilot AI review requested due to automatic review settings July 28, 2026 22:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@umputun
umputun merged commit 8241d6b into master Jul 28, 2026
7 checks passed
@umputun
umputun deleted the osc111-reset branch July 28, 2026 22:40
n2tr2 added a commit to n2tr2/agterm-linux that referenced this pull request Oct 1, 2026
With many sessions open, a theme change froze the app: the main thread fed
theme OSC 10/11 into every surface, each feed pushed color_change into
libghostty's 64-slot app mailbox with a .forever fallback, and only the main
thread drains that mailbox.

- Theme colors reach surfaces only through config; the host feeds, the
  feed helpers and themeOSC/themeResetOSC are removed.
- The OSC 11 latch overlay carries only opacity and font (macOS
  umputun#312), selected by the new LinuxBackgroundOverlayPolicy.
- libghostty patch: drop ghostty_surface_feed; under
  must_draw_from_app_thread, renderer_health pushes .instant and a
  PendingHealth value is retried from App.tick after the drain.
- AT-SPI scenario theme-many-sessions guards the hang and the color cases;
  control calls take a per-call timeout.
- libghostty.md records the config-only path and the app mailbox rule.

Claude-Session: https://claude.ai/code/session_01UXsFSAnftzk65uhqTTKixZ
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.

OSC 111 (reset background color) is ignored — TUIs leave the pane recolored after exit

2 participants