feat: add session-color patch for env-based prompt bar color#657
feat: add session-color patch for env-based prompt bar color#657VitalyOstanin wants to merge 5 commits intoPiebald-AI:mainfrom
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughAdds a new "session-color" patch that injects environment-driven session color initialization into serialized state code, registers the implementation, and includes tests covering injection, validation, idempotency, multi-location insertion, and failure/no-op cases. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/patches/index.ts (1)
426-432: Placesession-colorin the existing “Always Applied” block for ordering consistency.
PATCH_DEFINITIONSorder controls both apply order and listing. Keeping thisALWAYS_APPLIEDitem with the other always-applied entries will make ordering expectations clearer.♻️ Suggested reordering
{ id: 'statusline-update-throttle', name: `Statusline update throttling correction`, group: PatchGroup.ALWAYS_APPLIED, description: `Statusline updates will be properly throttled instead of queued (or debounced)`, }, + { + id: 'session-color', + name: 'Session color from env', + group: PatchGroup.ALWAYS_APPLIED, + description: + 'Set session prompt bar color via TWEAKCC_SESSION_COLOR env var', + }, // Misc Configurable @@ - { - id: 'session-color', - name: 'Session color from env', - group: PatchGroup.ALWAYS_APPLIED, - description: - 'Set session prompt bar color via TWEAKCC_SESSION_COLOR env var', - }, ] as const;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/patches/index.ts` around lines 426 - 432, The 'session-color' patch object is declared separately; move the object with id 'session-color' (name 'Session color from env', group PatchGroup.ALWAYS_APPLIED) into the existing ALWAYS_APPLIED block inside the PATCH_DEFINITIONS array so it sits alongside the other always-applied entries to preserve apply/listing order; locate the current standalone object (id 'session-color') and cut-and-paste it into the existing block where other PatchGroup.ALWAYS_APPLIED items are defined, ensuring no duplicate IDs and keeping the same properties.src/patches/sessionColor.ts (1)
25-27: Tighten the idempotency sentinel to this patch’s actual injection marker.A plain
includes('TWEAKCC_SESSION_COLOR')can false-positive if that token appears elsewhere, causing an unintended early return.🔧 Suggested guard refinement
export const writeSessionColor = (oldFile: string): string | null => { - if (oldFile.includes('TWEAKCC_SESSION_COLOR')) { + if ( + oldFile.includes( + 'standaloneAgentContext:(()=>{let __c=process.env.TWEAKCC_SESSION_COLOR;' + ) + ) { return oldFile; }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/patches/sessionColor.ts` around lines 25 - 27, The idempotency check using oldFile.includes('TWEAKCC_SESSION_COLOR') is too broad and can false-positive; tighten it by checking for the exact injected sentinel (e.g., a unique marker string you insert with the patch like '/* TWEAKCC_SESSION_COLOR_INJECTED */' or the full block snippet) or use a precise regex that matches the exact injection start/end, replacing the current includes call on oldFile with that exact-marker check so the function only returns early when the patch’s real injection is present.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/patches/index.ts`:
- Around line 426-432: The 'session-color' patch object is declared separately;
move the object with id 'session-color' (name 'Session color from env', group
PatchGroup.ALWAYS_APPLIED) into the existing ALWAYS_APPLIED block inside the
PATCH_DEFINITIONS array so it sits alongside the other always-applied entries to
preserve apply/listing order; locate the current standalone object (id
'session-color') and cut-and-paste it into the existing block where other
PatchGroup.ALWAYS_APPLIED items are defined, ensuring no duplicate IDs and
keeping the same properties.
In `@src/patches/sessionColor.ts`:
- Around line 25-27: The idempotency check using
oldFile.includes('TWEAKCC_SESSION_COLOR') is too broad and can false-positive;
tighten it by checking for the exact injected sentinel (e.g., a unique marker
string you insert with the patch like '/* TWEAKCC_SESSION_COLOR_INJECTED */' or
the full block snippet) or use a precise regex that matches the exact injection
start/end, replacing the current includes call on oldFile with that exact-marker
check so the function only returns early when the patch’s real injection is
present.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 6c0a9a3a-d57c-41eb-9844-8219b96cb972
📒 Files selected for processing (3)
src/patches/index.tssrc/patches/sessionColor.test.tssrc/patches/sessionColor.ts
6f71f32 to
6f25c6b
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/patches/sessionColor.ts`:
- Around line 51-57: The current showDiff call uses the post-patch `result` and
the full `replacement`, which can produce misleading diffs after the first
patch; update the call to use the pre-patch buffer (the buffer state before
applying the current replacement) for the index calculations and pass only the
INJECTION text as the injectedText argument to showDiff (keep the same start/end
as match.index and match.index + match[0].length but computed against the
pre-patch buffer), so replace the showDiff(oldFile, result, replacement, ...)
invocation with one that uses the pre-patch buffer for indices and INJECTION for
the injectedText parameter.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 13605414-8faf-4988-b1d2-660eda55da2a
📒 Files selected for processing (3)
src/patches/index.tssrc/patches/sessionColor.test.tssrc/patches/sessionColor.ts
✅ Files skipped from review due to trivial changes (2)
- src/patches/index.ts
- src/patches/sessionColor.test.ts
35e30f0 to
5620d5b
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/patches/index.ts (1)
899-901: Placesession-colorwith the Always Applied implementation block.Functionally correct, but moving this entry near the other Always Applied implementations will reduce maintenance drift between definition and implementation sections.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/patches/index.ts` around lines 899 - 901, The 'session-color' patch entry should be relocated into the "Always Applied" implementation block to match its implementation; move the object with key 'session-color' (fn: c => writeSessionColor(c)) so that it sits with the other Always Applied entries next to the implementation of writeSessionColor, ensuring the definition and implementation remain adjacent and reduce maintenance drift.src/patches/sessionColor.test.ts (1)
15-55: Add tests for remaining session lifecycle edge cases.Please add coverage for the two still-unverified behaviors (
/clearpersistence and resume restoring session color without env var) to lock in intended semantics.As per coding guidelines: "Test edge cases and error conditions in test files".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/patches/sessionColor.test.ts` around lines 15 - 55, Add two new unit tests inside the existing describe('writeSessionColor') block to cover the missing edge cases: one test that exercises the "/clear" persistence behavior by feeding an appropriate fixture into writeSessionColor (use or extend makeCLIState/makeBoth to include a clear action) and assert the returned patch reflects the expected persisted-clear semantics; and a second test that simulates a resume restore path without the TWEAKCC_SESSION_COLOR env var (create a fixture representing resumed state and call writeSessionColor or the resume helper) and assert the function injects/restores the session color correctly in the output. Reference writeSessionColor, makeCLIState, makeDefaultState and makeBoth to locate where to add these tests.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/patches/index.ts`:
- Around line 899-901: The 'session-color' patch entry should be relocated into
the "Always Applied" implementation block to match its implementation; move the
object with key 'session-color' (fn: c => writeSessionColor(c)) so that it sits
with the other Always Applied entries next to the implementation of
writeSessionColor, ensuring the definition and implementation remain adjacent
and reduce maintenance drift.
In `@src/patches/sessionColor.test.ts`:
- Around line 15-55: Add two new unit tests inside the existing
describe('writeSessionColor') block to cover the missing edge cases: one test
that exercises the "/clear" persistence behavior by feeding an appropriate
fixture into writeSessionColor (use or extend makeCLIState/makeBoth to include a
clear action) and assert the returned patch reflects the expected
persisted-clear semantics; and a second test that simulates a resume restore
path without the TWEAKCC_SESSION_COLOR env var (create a fixture representing
resumed state and call writeSessionColor or the resume helper) and assert the
function injects/restores the session color correctly in the output. Reference
writeSessionColor, makeCLIState, makeDefaultState and makeBoth to locate where
to add these tests.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: c34df35d-b2fa-43f1-bac8-be987e40b8b5
📒 Files selected for processing (3)
src/patches/index.tssrc/patches/sessionColor.test.tssrc/patches/sessionColor.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/patches/sessionColor.ts
Set the session prompt bar color via TWEAKCC_SESSION_COLOR env var.
Intended for use in wrapper scripts (e.g. ccw) that determine color
based on the working directory before launching Claude Code.
Valid values: red, blue, green, yellow, purple, orange, pink, cyan.
Invalid or unset values are silently ignored (no color applied).
How it works:
Claude Code has two app state initialization sites — the CLI
bootstrap (which builds initialState inline with dynamic values like
effortValue, fastMode) and the default state factory u3H() used as
fallback. Neither includes standaloneAgentContext, so it defaults to
undefined and the prompt bar shows no color.
The patch injects a standaloneAgentContext IIFE into both sites that
reads TWEAKCC_SESSION_COLOR at startup, validates it against the
hardcoded allowed list, and returns {name:"",color:value} or void 0.
This is the same state shape that /color sets interactively.
On session resume, Claude Code restores the color from the session
file, so the env var only affects new sessions.
1. Move session-color definition into the ALWAYS_APPLIED block
(next to statusline-update-throttle) for ordering consistency.
2. Tighten idempotency check: match the full injection sentinel
'standaloneAgentContext:(()=>{let __c=process.env.TWEAKCC_SESSION_COLOR;'
instead of the broad 'TWEAKCC_SESSION_COLOR' substring to avoid
false-positive early returns.
3. Rebase onto upstream/main to resolve conflict with channels-mode
patch (Piebald-AI#653).
Patch saveAgentColor to expose it via globalThis, then call it from the INJECTION via queueMicrotask so the env-based color is written to the session transcript. On resume, computeStandaloneAgentContext reads agentColor from the log and restores the prompt bar color automatically.
5620d5b to
644621a
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/patches/sessionColor.ts`:
- Around line 54-60: The showDiff call is passing the full replacement (which
contains the original matched text) as the injectedText, causing match[0] to
appear as newly injected; update the showDiff invocation in sessionColor.ts to
pass the constant INJECTION (instead of the variable replacement) as the
injectedText argument while leaving the other arguments (prePatch, result,
match.index, match.index + match[0].length) unchanged so that only the intended
INJECTION is shown as the inserted text; reference the symbols showDiff,
INJECTION, replacement, match, prePatch, and result to locate and modify the
call.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: c26753e0-d76e-4fd1-8f65-fe4125fdc038
📒 Files selected for processing (3)
src/patches/index.tssrc/patches/sessionColor.test.tssrc/patches/sessionColor.ts
✅ Files skipped from review due to trivial changes (1)
- src/patches/sessionColor.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/patches/index.ts
…ied block Pass only the actual injected text (INJECTION / injection) to showDiff instead of the full replacement string, so debug diffs don't misleadingly show original matched text as newly inserted. Move session-color implementation entry next to other Always Applied patches.
Summary
Add an always-applied patch that reads
TWEAKCC_SESSION_COLORenv var at startupand sets the session prompt bar color accordingly.
ccw) that determine color based on theworking directory before launching Claude Code
red,blue,green,yellow,purple,orange,pink,cyanenv var only affects new sessions
How it works
Claude Code has two app state initialization sites — the CLI bootstrap (builds
initialStateinline with dynamic values likeeffortValue,fastMode) andthe default state factory (used as fallback). Neither includes
standaloneAgentContext, so it defaults toundefined.The patch injects a
standaloneAgentContextIIFE into both sites that readsTWEAKCC_SESSION_COLOR, validates it against the hardcoded allowed color list,and returns
{name:"",color:value}orvoid 0. This is the same state shapethat
/colorsets interactively.Test plan
both locations, color validation, idempotency, pattern-not-found)
pnpm lintclean (tsc + eslint)TWEAKCC_SESSION_COLOR=cyan ccw-testshows cyan prompt bar/clearSummary by CodeRabbit
New Features
Tests