-
Notifications
You must be signed in to change notification settings - Fork 125
feat: add session-color patch for env-based prompt bar color #657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VitalyOstanin
wants to merge
3
commits into
Piebald-AI:main
Choose a base branch
from
VitalyOstanin:feature/session-color
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { writeSessionColor } from './sessionColor'; | ||
|
|
||
| const makeCLIState = () => | ||
| 'effortValue:oR(w.effort),' + | ||
| 'activeOverlays:new Set,fastMode:cP8(N5),' + | ||
| '...(uF()&&d1&&{advisorModel:d1})'; | ||
|
|
||
| const makeDefaultState = () => | ||
| 'effortValue:void 0,' + 'activeOverlays:new Set,fastMode:!1}'; | ||
|
|
||
| const makeBoth = () => | ||
| 'first{' + makeDefaultState() + 'second{' + makeCLIState(); | ||
|
|
||
| describe('sessionColor', () => { | ||
| describe('writeSessionColor', () => { | ||
| it('should inject into CLI initialState', () => { | ||
| const result = writeSessionColor(makeCLIState()); | ||
| expect(result).not.toBeNull(); | ||
| expect(result).toContain('TWEAKCC_SESSION_COLOR'); | ||
| expect(result).toContain('{name:"",color:__c}'); | ||
| }); | ||
|
|
||
| it('should inject into default app state', () => { | ||
| const result = writeSessionColor(makeDefaultState()); | ||
| expect(result).not.toBeNull(); | ||
| expect(result).toContain('TWEAKCC_SESSION_COLOR'); | ||
| }); | ||
|
|
||
| it('should patch both locations when both present', () => { | ||
| const result = writeSessionColor(makeBoth())!; | ||
| expect(result).not.toBeNull(); | ||
| const count = (result.match(/TWEAKCC_SESSION_COLOR/g) || []).length; | ||
| expect(count).toBe(2); | ||
| }); | ||
|
|
||
| it('should validate color against allowed list', () => { | ||
| const result = writeSessionColor(makeCLIState())!; | ||
| expect(result).toContain('.includes(__c)'); | ||
| expect(result).toContain('"green"'); | ||
| expect(result).toContain('"cyan"'); | ||
| }); | ||
|
|
||
| it('should be idempotent', () => { | ||
| const first = writeSessionColor(makeBoth())!; | ||
| const second = writeSessionColor(first)!; | ||
| expect(second).toBe(first); | ||
| }); | ||
|
|
||
| it('should return null when no pattern found', () => { | ||
| const result = writeSessionColor('not a valid file'); | ||
| expect(result).toBeNull(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { showDiff } from './index'; | ||
| import { debug } from '../utils'; | ||
|
|
||
| const VALID_COLORS = [ | ||
| 'red', | ||
| 'blue', | ||
| 'green', | ||
| 'yellow', | ||
| 'purple', | ||
| 'orange', | ||
| 'pink', | ||
| 'cyan', | ||
| ]; | ||
|
|
||
| const INJECTION = | ||
| `,standaloneAgentContext:` + | ||
| `(()=>{` + | ||
| `let __c=process.env.TWEAKCC_SESSION_COLOR;` + | ||
| `return __c&&${JSON.stringify(VALID_COLORS)}.includes(__c)` + | ||
| `?{name:"",color:__c}` + | ||
| `:void 0` + | ||
| `})()`; | ||
|
|
||
| export const writeSessionColor = (oldFile: string): string | null => { | ||
| if ( | ||
| oldFile.includes( | ||
| 'standaloneAgentContext:(()=>{let __c=process.env.TWEAKCC_SESSION_COLOR;' | ||
| ) | ||
| ) { | ||
| return oldFile; | ||
| } | ||
|
|
||
| const patterns = [ | ||
| /,activeOverlays:new Set,fastMode:[$\w]+\([$\w]+\)/, | ||
| /,activeOverlays:new Set,fastMode:!1\}/, | ||
| ]; | ||
|
|
||
| let result = oldFile; | ||
| let patched = false; | ||
|
|
||
| for (const pattern of patterns) { | ||
| const match = result.match(pattern); | ||
| if (!match || match.index === undefined) continue; | ||
|
|
||
| const prePatch = result; | ||
| const replacement = INJECTION + match[0]; | ||
| result = | ||
| prePatch.slice(0, match.index) + | ||
| replacement + | ||
| prePatch.slice(match.index + match[0].length); | ||
|
|
||
| showDiff( | ||
| prePatch, | ||
| result, | ||
| replacement, | ||
| match.index, | ||
| match.index + match[0].length | ||
| ); | ||
| patched = true; | ||
| } | ||
|
|
||
| if (!patched) { | ||
| debug('patch: sessionColor: failed to find app state init patterns'); | ||
| return null; | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
showDiffargs are inconsistent after the first patch.Use the pre-patch buffer for indices and pass only
INJECTIONasinjectedText; current call can produce misleading diffs when both patterns are patched.Suggested fix
for (const pattern of patterns) { - const match = result.match(pattern); + const beforePatch = result; + const match = beforePatch.match(pattern); if (!match || match.index === undefined) continue; const replacement = INJECTION + match[0]; result = - result.slice(0, match.index) + + beforePatch.slice(0, match.index) + replacement + - result.slice(match.index + match[0].length); + beforePatch.slice(match.index + match[0].length); showDiff( - oldFile, + beforePatch, result, - replacement, + INJECTION, match.index, match.index + match[0].length );🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai Fixed in 35e30f0.
showDiffnow usesprePatch(pre-patch buffer) for both slicing and diff indices, ensuring consistent results when multiple patterns are patched.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@VitalyOstanin, thanks for the update! UsingprePatchas the base for both slicing and theshowDiffindices ensures that each iteration operates on a stable snapshot, so the displayed diffs remain accurate even when both patterns are matched and patched in the same run. The fix looks correct. ✅🧠 Learnings used
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!