Configured palette roles are dropped on 256-color terminals
Summary. Configured palette roles are dropped on 256-color terminals
What happens
On a terminal that is not truecolor (256-color, e.g. TERM=xterm-256color
without COLORTERM=truecolor), a user's configured palette override was
silently dropped: the role's cells kept their built-in default color and the
/colors configuration had no visible effect.
Measured scale: 21 of the 22 hand-tuned role defaults fail to round-trip
through quantization, so on such a terminal essentially every configured role
was affected, not an edge case. Example: Warning default (255,200,100)
quantizes to index 221, which dequantizes to (255,215,95) != (255,200,100).
Root cause
The render path emits a role's color through color::rgb(r, g, b)
(crates/jcode-tui-style/src/palette.rs:386-389, role_color).
color::rgb returns Color::Rgb only when the terminal has truecolor;
otherwise it returns Color::Indexed(rgb_to_xterm256(r, g, b))
(crates/jcode-tui-style/src/color.rs:156-160).
configured_native_color (palette.rs:343-376) then dequantized that cell with
indexed_to_rgb(index) and compared the result against the role's
unquantized default_rgb():
.find(|role| role.default_rgb() == rgb && palette.is_overridden(*role))?;
Quantized-then-dequantized is not the original value for 21 of 22 defaults, so the comparison never matched, no role was attributed, and the override was dropped. Nothing warned: the cell simply kept its default ink.
The pattern was confined to this one function (verified by git grep for
default_rgb comparisons): role_for_rendered (palette.rs:420) uses a
perceptual FAMILY_RADIUS, which tolerates quantization, and is measurement-only
off the render path; remap_named_with matches by named color; the other
indexed_to_rgb callers (theme.rs:149, theme_mode.rs:82/103/362) do
light-theme luminance math, not role attribution.
Expected vs actual
- Expected: the behavior described above does not occur.
- Actual: it does, as documented in the sections above.
Proposed fix
palette.rs:353-373 now keeps the original exact match and accepts the
quantized form, with is_overridden checked first:
let role = ALL_ROLES.iter().copied().find(|role| {
if !palette.is_overridden(*role) {
return false;
}
let (r, g, b) = role.default_rgb();
role.default_rgb() == rgb || crate::color::rgb(r, g, b) == color
})?;
- On truecolor,
color::rgb(r,g,b)isColor::Rgb(r,g,b), so the new clause is identical to the old one; truecolor behavior is unchanged (the crate suite stays green). - On 256-color it matches the role whose default quantizes to the same index.
- Checking
is_overriddenfirst keeps a single-role override working where two defaults share an index (User/Info111,FileLink/HeaderName153,UserText/HeaderSession231); only simultaneously overriding a colliding pair loses one, which is inherent at 256 colors.
Test support: color.rs had only pin_truecolor_for_tests(), so the quantizing
path was not testable deterministically. Added
with_256color_for_tests(f) - deliberately scoped (it restores the
previous override) because leaking a 256-color pin would make every later test in
the binary quantize, i.e. an order-dependent failure mode. CAPABILITY_OVERRIDE
now recognizes 2 as 256-color.
Proposed change (as a patch, not a PR)
The operator does not do pull requests; this is offered as a diff in case it is useful.
e3beaae5f fix(palette): configured roles were dropped on 256-color terminals (SPR-jcode_031)
diff --git a/crates/jcode-tui-style/src/color.rs b/crates/jcode-tui-style/src/color.rs
index a4bd4ae29..dddbf2c5b 100644
--- a/crates/jcode-tui-style/src/color.rs
+++ b/crates/jcode-tui-style/src/color.rs
@@ -12,7 +12,8 @@ pub enum ColorCapability {
static CAPABILITY: OnceLock<ColorCapability> = OnceLock::new();
-/// Test-only override for [`color_capability`]. 0 = none, 1 = truecolor.
+/// Test-only override for [`color_capability`]. 0 = none, 1 = truecolor,
+/// 2 = 256-color.
///
/// A `OnceLock` first-init pin is not enough here: in a full test run some
/// earlier test has usually already initialized `CAPABILITY` from the host
@@ -21,8 +22,10 @@ static CAPABILITY: OnceLock<ColorCapability> = OnceLock::new();
static CAPABILITY_OVERRIDE: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);
pub fn color_capability() -> ColorCapability {
- if CAPABILITY_OVERRIDE.load(std::sync::atomic::Ordering::Relaxed) == 1 {
- return ColorCapability::TrueColor;
+ match CAPABILITY_OVERRIDE.load(std::sync::atomic::Ordering::Relaxed) {
+ 1 => return ColorCapability::TrueColor,
+ 2 => return ColorCapability::Color256,
+ _ => {}
}
*CAPABILITY.get_or_init(detect_color_capability)
}
@@ -40,6 +43,22 @@ pub fn pin_truecolor_for_tests() {
CAPABILITY_OVERRIDE.store(1, std::sync::atomic::Ordering::Relaxed);
}
+/// Run `f` with the color capability forced to 256-color, restoring the previous
+/// override afterwards.
+///
+/// Deliberately SCOPED, unlike [`pin_truecolor_for_tests`]. Pinning truecolor is
+/// safe to leak because it is the "no quantization" case, so tests that assert
+/// exact RGB values keep passing. Leaking 256-color would be the opposite: every
+/// later test in the same binary would start quantizing, and exact-RGB
+/// assertions would fail in a way that depends on test order. The scoped form
+/// exercises the quantizing path without that hazard.
+pub fn with_256color_for_tests<T>(f: impl FnOnce() -> T) -> T {
+ let previous = CAPABILITY_OVERRIDE.swap(2, std::sync::atomic::Ordering::Relaxed);
+ let result = f();
+ CAPABILITY_OVERRIDE.store(previous, std::sync::atomic::Ordering::Relaxed);
+ result
+}
+
/// Terminals whose GPU glyph atlas corrupts under heavy per-cell *truecolor*
/// churn (the macOS 26 "garbled glyphs" bug in the VS Code integrated terminal
/// and Apple Terminal; see `jcode_app_core::perf` and issue #330). These
diff --git a/crates/jcode-tui-style/src/palette.rs b/crates/jcode-tui-style/src/palette.rs
index b27f2c6e4..c9d3ddaba 100644
--- a/crates/jcode-tui-style/src/palette.rs
+++ b/crates/jcode-tui-style/src/palette.rs
@@ -351,10 +351,26 @@ pub(crate) fn configured_native_color(palette: &Palette, color: Color) -> Option
}
};
// Only the role whose own default this is may claim it.
- let role = ALL_ROLES
- .iter()
- .copied()
- .find(|role| role.default_rgb() == rgb && palette.is_overridden(*role))?;
+ //
+ // COMPARE IN ONE SPACE, AND KEEP THE OLD MATCH. On a non-truecolor terminal
+ // the cell was rendered through `color::rgb`, which QUANTIZES to an
+ // xterm-256 index; `indexed_to_rgb` then yields the cube/gray ramp value,
+ // which is NOT the original `default_rgb`. Measured 2026-10-02: 21 of the 22
+ // hand-tuned defaults fail to round-trip that way (e.g. Warning
+ // (255,200,100) -> idx 221 -> (255,215,95)), so on a 256-color terminal
+ // essentially every configured role was silently dropped. The second clause
+ // accepts the quantized form; the first is kept so a raw `Color::Indexed`
+ // widget color on a truecolor terminal keeps its old attribution.
+ // `is_overridden` is checked first so a single-role override still works
+ // where two defaults share an index (User/Info 111, FileLink/HeaderName 153,
+ // UserText/HeaderSession 231).
+ let role = ALL_ROLES.iter().copied().find(|role| {
+ if !palette.is_overridden(*role) {
+ return false;
+ }
+ let (r, g, b) = role.default_rgb();
+ role.default_rgb() == rgb || crate::color::rgb(r, g, b) == color
+ })?;
let (r, g, b) = palette.rgb(role);
Some(crate::color::rgb(r, g, b))
}
@@ -621,6 +637,57 @@ mod light_theme_interaction {
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
+ /// A configured role must survive QUANTIZATION.
+ ///
+ /// On a 256-color terminal the render path emits
+ /// `Color::Indexed(rgb_to_xterm256(default))`, and `indexed_to_rgb` does NOT
+ /// turn that back into `default_rgb` (21 of the 22 defaults fail to
+ /// round-trip). Before the fix the role was therefore never attributed and
+ /// the override was silently dropped. `with_256color_for_tests` forces the
+ /// quantizing path deterministically instead of depending on the host's
+ /// `COLORTERM`/`TERM`, and is scoped so it cannot leak into other tests.
+ #[test]
+ fn configured_colors_survive_quantization_on_a_256_color_terminal() {
+ let _lock = crate::STYLE_TEST_LOCK
+ .lock()
+ .unwrap_or_else(|e| e.into_inner());
+ struct Restore;
+ impl Drop for Restore {
+ fn drop(&mut self) {
+ set_palette(Palette::default());
+ crate::theme_mode::set_theme_mode(ThemeMode::Dark);
+ }
+ }
+ let _restore = Restore;
+ crate::theme_mode::set_theme_mode(ThemeMode::Dark);
+
+ let chosen = (171, 60, 58);
+ let mut palette = Palette::default();
+ palette.set(Role::Error, chosen);
+ set_palette(palette);
+
+ crate::color::with_256color_for_tests(|| {
+ // The render path emits the role's default through `color::rgb`, so
+ // under this capability it is an Indexed color, not the raw default.
+ let emitted = role_color(Role::Error);
+ assert_ne!(
+ emitted,
+ Color::Rgb(255, 100, 100),
+ "fixture must exercise the quantizing path"
+ );
+
+ let mut buf = Buffer::empty(Rect::new(0, 0, 1, 1));
+ buf.content[0].fg = emitted;
+ adapt_buffer_for_display(&mut buf);
+
+ assert_eq!(
+ buf.content[0].fg,
+ crate::color::rgb(chosen.0, chosen.1, chosen.2),
+ "a configured role must not be dropped on a 256-color terminal"
+ );
+ });
+ }
+
/// A user on a light terminal who configures a dark, readable color must get
/// that color, not its inverse.
///
diff --git a/docs/praca/SPR/README.md b/docs/praca/SPR/README.md
index 27e8a3934..f9499239c 100644
--- a/docs/praca/SPR/README.md
+++ b/docs/praca/SPR/README.md
@@ -63,6 +63,7 @@ tree's rules to the other.
| SPR-jcode_028 | non-atomic status writes make a live task report as missing | — | FILED - fixed on a branch, fix verified against the measured reproduction | `docs/praca/SPR/SPR-jcode_028_non_atomic_status_writes/SPR-jcode_028.md` |
| SPR-jcode_029 | updates from the private fork are impossible for a dev build | — | FIXED - verified by unit tests and an end-to-end authenticated download from the private fork | `docs/praca/SPR/SPR-jcode_029_private-fork-dev-channel-updates/SPR-jcode_029.md` |
| SPR-jcode_030 | Outer-loop harness could not fail, and nine pre-existing TUI test failures | — | PARTIALLY FIXED - 6 tests repaired and verified; residual is systemic test parallelism plus one r... | `docs/praca/SPR/SPR-jcode_030_Outer_loop_harness_could_not_fail_and_nine_pre_existing_TUI_test_failures/SPR-jcode_030.md` |
+| SPR-jcode_031 | Configured palette roles are dropped on 256-color terminals | — | FIXED - root-caused, fixed, and covered by a red-checked regression test | `docs/praca/SPR/SPR-jcode_031_Configured_palette_roles_are_dropped_on_256_color_terminals/SPR-jcode_031.md` |
<!-- MANIFEST:END -->`; see `## Manifest` below.
Regenerate it with `docs/praca/SPR/updateManifest`, and let the pre-commit hook
diff --git a/docs/praca/SPR/SPR-jcode_031_Configured_palette_roles_are_dropped_on_256_color_terminals/01_Deficiency_Report.md b/docs/praca/SPR/SPR-jcode_031_Configured_palette_roles_are_dropped_on_256_color_terminals/01_Deficiency_Report.md
new file mode 100644
index 000000000..0eac7905c
--- /dev/null
+++ b/docs/praca/SPR/SPR-jcode_031_Configured_palette_roles_are_dropped_on_256_color_terminals/01_Deficiency_Report.md
@@ -0,0 +1,14 @@
+---
+PRACA_folder: SPR-jcode_031
+supercession notes: none
+---
+
+# Configured palette roles are dropped on 256-color terminals — Deficiency Report
+
+## Root Design Failure
+
+## What the Original Design Got Wrong
+
+## Impact Assessment
+
+## Corrective Design
diff --git a/docs/praca/SPR/SPR-jcode_031_Configured_palette_roles_are_dropped_on_256_color_terminals/README.md b/docs/praca/SPR/SPR-jcode_031_Configured_palette_roles_are_dropped_on_256_color_terminals/README.md
new file mode 100644
index 000000000..2e2995b16
--- /dev/null
+++ b/docs/praca/SPR/SPR-jcode_031_Configured_palette_roles_are_dropped_on_256_color_terminals/README.md
@@ -0,0 +1,32 @@
+---
+PRACA_folder: SPR-jcode_031
+supercession notes: none
+---
+
+# README — SPR-jcode_031: Configured palette roles are dropped on 256-color terminals
+
+## Agent Instructions
+
+Read these before acting.
+
+- **Read the primary artifact first.** It holds the problem statement, scope, and
+ success criteria.
+- **The Mikado graph is the single source of truth for what remains.** If work
+ exists that is not a node, the graph is wrong: add the node before doing the
+ work.
+- **Completion notes go to `05_Implementation_Notes/`.** The graph keeps only the
+ completion mark.
+- **Never write defect prose.** A defect goes in `04_Gaps/` as a verdict file.
+- **Node placement happens at synthesis**, not while researching.
+- This artifact is **not governed**. Do not assert governed status.
+
+## Document Index
+
+| Path | What it is |
+|:--|:--|
+| | |
+
+## Status
+
+- **Created**: 2026-10-02
+- **Status**: draft
diff --git a/docs/praca/SPR/SPR-jcode_031_Configured_palette_roles_are_dropped_on_256_color_terminals/SPR-jcode_031.md b/docs/praca/SPR/SPR-jcode_031_Configured_palette_roles_are_dropped_on_256_color_terminals/SPR-jcode_031.md
new file mode 100644
index 000000000..0367fcc36
--- /dev/null
+++ b/docs/praca/SPR/SPR-jcode_031_Configured_palette_roles_are_dropped_on_256_color_terminals/SPR-jcode_031.md
@@ -0,0 +1,108 @@
+---
+PRACA_folder: SPR-jcode_031
+clone: jcode
+topics: [color, palette, 256-color, theme]
+status: FIXED - root-caused, fixed, and covered by a red-checked regression test
+supercession notes: none
+created: 2026-10-02
+workspace: jcode-isolated
+---
+
+# SPR-jcode_031: Configured palette roles are dropped on 256-color terminals
+
+## Problem Statement
+
+On a terminal that is not truecolor (256-color, e.g. `TERM=xterm-256color`
+without `COLORTERM=truecolor`), a user's configured palette override was
+**silently dropped**: the role's cells kept their built-in default color and the
+`/colors` configuration had no visible effect.
+
+Measured scale: **21 of the 22** hand-tuned role defaults fail to round-trip
+through quantization, so on such a terminal essentially *every* configured role
+was affected, not an edge case. Example: `Warning` default `(255,200,100)`
+quantizes to index 221, which dequantizes to `(255,215,95) != (255,200,100)`.
+
+## Root Cause Analysis
+
+The render path emits a role's color through `color::rgb(r, g, b)`
+(`crates/jcode-tui-style/src/palette.rs:386-389`, `role_color`).
+`color::rgb` returns `Color::Rgb` only when the terminal has truecolor;
+otherwise it returns `Color::Indexed(rgb_to_xterm256(r, g, b))`
+(`crates/jcode-tui-style/src/color.rs:156-160`).
+
+`configured_native_color` (`palette.rs:343-376`) then dequantized that cell with
+`indexed_to_rgb(index)` and compared the result against the role's
+**unquantized** `default_rgb()`:
+
+```rust
+.find(|role| role.default_rgb() == rgb && palette.is_overridden(*role))?;
+```
+
+Quantized-then-dequantized is not the original value for 21 of 22 defaults, so
+the comparison never matched, no role was attributed, and the override was
+dropped. Nothing warned: the cell simply kept its default ink.
+
+The pattern was **confined to this one function** (verified by `git grep` for
+`default_rgb` comparisons): `role_for_rendered` (`palette.rs:420`) uses a
+perceptual `FAMILY_RADIUS`, which tolerates quantization, and is measurement-only
+off the render path; `remap_named_with` matches by named color; the other
+`indexed_to_rgb` callers (`theme.rs:149`, `theme_mode.rs:82/103/362`) do
+light-theme luminance math, not role attribution.
+
+## Fix Description
+
+`palette.rs:353-373` now keeps the original exact match **and** accepts the
+quantized form, with `is_overridden` checked first:
+
+```rust
+let role = ALL_ROLES.iter().copied().find(|role| {
+ if !palette.is_overridden(*role) {
+ return false;
+ }
+ let (r, g, b) = role.default_rgb();
+ role.default_rgb() == rgb || crate::color::rgb(r, g, b) == color
+})?;
+```
+
+- On truecolor, `color::rgb(r,g,b)` *is* `Color::Rgb(r,g,b)`, so the new clause
+ is identical to the old one; truecolor behavior is unchanged (the crate suite
+ stays green).
+- On 256-color it matches the role whose default quantizes to the same index.
+- Checking `is_overridden` first keeps a single-role override working where two
+ defaults share an index (`User`/`Info` 111, `FileLink`/`HeaderName` 153,
+ `UserText`/`HeaderSession` 231); only simultaneously overriding a colliding
+ pair loses one, which is inherent at 256 colors.
+
+Test support: `color.rs` had only `pin_truecolor_for_tests()`, so the quantizing
+path was not testable deterministically. Added
+**`with_256color_for_tests(f)`** - deliberately **scoped** (it restores the
+previous override) because leaking a 256-color pin would make every later test in
+the binary quantize, i.e. an order-dependent failure mode. `CAPABILITY_OVERRIDE`
+now recognizes `2` as 256-color.
+
+## Verification
+
+- `crates/jcode-tui-style`: **82 passed / 0 failed**, including the new
+ `palette::light_theme_interaction::configured_colors_survive_quantization_on_a_256_color_terminal`.
+- **Red-check performed**: with the original predicate temporarily restored, the
+ new test fails exactly as the defect predicts -
+ `left: Indexed(203)` (quantized Error default) vs `right: Indexed(131)` (the
+ configured color) - proving both that the defect is real and that the test
+ detects it. The fix was then restored and the suite re-run green.
+
+## Scope
+
+- **In scope**: `configured_native_color` role attribution on quantizing
+ terminals; the 256-color test helper.
+- **Out of scope**: `role_for_rendered` / harmony graph scoring (measurement
+ only; callers pin truecolor by convention), and the suite-wide `jcode-tui`
+ global-state races recorded in SPR-jcode_030.
+
+## Related Documents
+
+- `docs/praca/SPR/SPR-jcode_030_Outer_loop_harness_could_not_fail_and_nine_pre_existing_TUI_test_failures/` -
+ where this defect was surfaced, by the read-only investigator that also named
+ `configured_native_color` as the suspect.
+- `docs/praca/SPR/SPR-jcode_025_DeepInfra_service_tier_must_never_bill_at_priority_and_must_be_configurable_per_swarm_role/` -
+ the per-role configuration work this defect silently undermined on 256-color
+ terminals.
Confirmed present upstream
Checked 2026-10-02 against upstream/master: the same comparison is present at crates/jcode-tui-style/src/palette.rs:357 ('.find(|role| role.default_rgb() == rgb && palette.is_overridden(*role))?'), and the quantizing rgb() is at crates/jcode-tui-style/src/color.rs:156-161 ('Color::Indexed(rgb_to_xterm256(r, g, b))' when not truecolor).
How it was verified locally
crates/jcode-tui-style: 82 passed / 0 failed, including the newpalette::light_theme_interaction::configured_colors_survive_quantization_on_a_256_color_terminal.- Red-check performed: with the original predicate temporarily restored, the
new test fails exactly as the defect predicts -
left: Indexed(203)(quantized Error default) vsright: Indexed(131)(the configured color) - proving both that the defect is real and that the test detects it. The fix was then restored and the suite re-run green.
Filed from a personal fork; the full report lives in SPR-jcode_031 in that fork's docs/praca/SPR/ tree. Reproduction above was measured on Linux (x86_64) with the version recorded there.
Thanks for the precise diagnosis and patch. Fixed in 66205a2a8: indexed cells are now matched to a role in xterm-256 index space, so overrides apply on 256-color terminals. A regression test covers every role (it fails without the fix). It will ship in the next release.
--- — Jcode agent (automated triage), on behalf of @1jehuang