Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions scripts/test-work-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
deriveAgentWorkContext,
ingestWorktreeRawEvent,
} from '../src/renderer/src/workspace/work-context/tracker'
import {
resetWorktreeBadgeColorAssignmentsForTest,
worktreeBadgeColor,
} from '../src/renderer/src/workspace/work-context/colors'
import type { WorktreeIdentity } from '../src/shared/types/git'
import type { WorktreeActivityState } from '../src/renderer/src/workspace/work-context/types'

Expand Down Expand Up @@ -260,4 +264,54 @@ const claudeToolEntry = (
assert.equal(matchWorktree('/repo-feature-long/src/index.ts', worktrees), null)
}

{
resetWorktreeBadgeColorAssignmentsForTest()
const first = worktreeBadgeColor({
worktreePath: '/repo-feature-a',
branch: 'feat/a',
repoRoot: '/repo',
confidence: 'strong',
source: 'test',
updatedAt: 1,
})
const second = worktreeBadgeColor({
worktreePath: '/repo-feature-b',
branch: 'feat/b',
repoRoot: '/repo',
confidence: 'strong',
source: 'test',
updatedAt: 2,
})
const firstAgain = worktreeBadgeColor({
worktreePath: '/repo-feature-a',
branch: 'feat/a',
repoRoot: '/repo',
confidence: 'strong',
source: 'test',
updatedAt: 3,
})
const main = worktreeBadgeColor({
worktreePath: '/repo',
branch: 'main',
repoRoot: '/repo',
confidence: 'strong',
source: 'test',
updatedAt: 4,
})
const master = worktreeBadgeColor({
worktreePath: '/repo-master',
branch: 'master',
repoRoot: '/repo',
confidence: 'strong',
source: 'test',
updatedAt: 5,
})

assert.equal(first, '#0f766e')
assert.equal(second, '#b45309')
assert.equal(firstAgain, first)
assert.equal(main, '#2563eb')
assert.equal(master, '#475569')
}

console.log('work-context reducer tests passed')
14 changes: 3 additions & 11 deletions src/renderer/src/workspace/tile-tree/TileLeaf/ScrollIndicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
AgentWorkContext,
WorktreeActivityState,
} from '@renderer/workspace/work-context/types'
import { worktreeBadgeColor } from '@renderer/workspace/work-context/colors'

// Scroll position indicator — sits just above the composer,
// right-aligned. Shows which entry you're looking at out of the
Expand Down Expand Up @@ -65,7 +66,7 @@ function WorktreeBadge({
if (!context?.worktreePath) return null
const label = context.branch ?? shortPath(context.worktreePath)
if (!label) return null
const color = colorFor(context.worktreePath)
const color = worktreeBadgeColor(context)
const title = [
'Primary worktree',
context.branch ? `Branch: ${context.branch}` : null,
Expand All @@ -84,7 +85,7 @@ function WorktreeBadge({
return (
<span
className="max-w-[180px] truncate rounded-sm px-1.5 py-[1px] text-[10px] font-code leading-none text-white"
style={{ backgroundColor: color }}
style={{ backgroundColor: color ?? undefined }}
title={title}
>
{label}
Expand All @@ -96,12 +97,3 @@ function shortPath(path: string): string {
const parts = path.split('/').filter(Boolean)
return parts[parts.length - 1] ?? path
}

function colorFor(seed: string): string {
let hash = 0
for (let i = 0; i < seed.length; i += 1) {
hash = ((hash << 5) - hash + seed.charCodeAt(i)) | 0
}
const hue = Math.abs(hash) % 360
return `hsl(${hue} 58% 38%)`
}
63 changes: 63 additions & 0 deletions src/renderer/src/workspace/work-context/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type { AgentWorkContext } from '@renderer/workspace/work-context/types'

const MAIN_BRANCH_COLORS: Record<string, string> = {
main: '#2563eb',
master: '#475569',
}

const WORKTREE_PALETTE = [
'#0f766e',
'#b45309',
'#7c3aed',
'#be123c',
'#047857',
'#c2410c',
'#4338ca',
'#a21caf',
'#0369a1',
'#4d7c0f',
'#b91c1c',
'#6d28d9',
'#0e7490',
'#a16207',
'#15803d',
'#c026d3',
'#1d4ed8',
'#9f1239',
'#166534',
'#9333ea',
'#155e75',
'#92400e',
'#be185d',
'#065f46',
'#3730a3',
'#9a3412',
'#115e59',
'#7e22ce',
'#991b1b',
'#075985',
] as const

const assignedPaletteIndexByWorktreePath = new Map<string, number>()

export function worktreeBadgeColor(
context: AgentWorkContext | null | undefined,
): string | null {
if (!context?.worktreePath) return null

const branch = context.branch?.trim().toLowerCase()
if (branch && MAIN_BRANCH_COLORS[branch]) {
return MAIN_BRANCH_COLORS[branch]
}

let index = assignedPaletteIndexByWorktreePath.get(context.worktreePath)
if (index === undefined) {
index = assignedPaletteIndexByWorktreePath.size % WORKTREE_PALETTE.length
assignedPaletteIndexByWorktreePath.set(context.worktreePath, index)
}
return WORKTREE_PALETTE[index]
}

export function resetWorktreeBadgeColorAssignmentsForTest(): void {
assignedPaletteIndexByWorktreePath.clear()
}