Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
feat(git-history): shared graph view-models + ref dedupe (ported)
enes8378b32b4ff9
diff --git a/src/shared/git-history-boundary-rows.ts b/src/shared/git-history-boundary-rows.tsnew file mode 100644index 000000000..fed590d5c--- /dev/null+++ b/src/shared/git-history-boundary-rows.ts@@ -0,0 +1,195 @@+import type { GitHistoryGraphColorId, GitHistoryItem, GitHistoryItemRef } from './git-history'+import { GIT_HISTORY_REF_COLOR, GIT_HISTORY_REMOTE_REF_COLOR } from './git-history'+import type { GitHistoryGraphNode, GitHistoryItemViewModel } from './git-history-graph'++export const GIT_HISTORY_INCOMING_CHANGES_ID = 'git-history-incoming-changes'+export const GIT_HISTORY_OUTGOING_CHANGES_ID = 'git-history-outgoing-changes'++function cloneNode(node: GitHistoryGraphNode): GitHistoryGraphNode {+ return { id: node.id, color: node.color }+}++function findLastIndex<T>(items: readonly T[], predicate: (item: T) => boolean): number {+ for (let index = items.length - 1; index >= 0; index -= 1) {+ if (predicate(items[index] as T)) {+ return index+ }+ }+ return -1+}++function hasNode(+ nodes: readonly GitHistoryGraphNode[],+ id: string,+ color?: GitHistoryGraphColorId+): boolean {+ return nodes.some((node) => node.id === id && (color === undefined || node.color === color))+}++function remoteBoundaryInputNode(+ node: GitHistoryGraphNode,+ mergeBase: string+): GitHistoryGraphNode {+ return node.id === mergeBase && node.color === GIT_HISTORY_REMOTE_REF_COLOR+ ? { ...node, id: GIT_HISTORY_INCOMING_CHANGES_ID }+ : cloneNode(node)+}++function ensureIncomingRemoteLane(+ inputSwimlanes: GitHistoryGraphNode[],+ outputSwimlanes: GitHistoryGraphNode[],+ mergeBase: string+): void {+ // Why: HEAD-only history omits upstream commits, so the graph must still+ // synthesize the remote lane that used to arrive from those hidden rows.+ if (!hasNode(outputSwimlanes, mergeBase, GIT_HISTORY_REMOTE_REF_COLOR)) {+ const localMergeBaseIndex = outputSwimlanes.findIndex(+ (node) => node.id === mergeBase && node.color === GIT_HISTORY_REF_COLOR+ )+ const remoteMergeBaseIndex =+ localMergeBaseIndex === -1 ? inputSwimlanes.length : localMergeBaseIndex + 1+ outputSwimlanes.splice(remoteMergeBaseIndex, 0, {+ id: mergeBase,+ color: GIT_HISTORY_REMOTE_REF_COLOR+ })+ }++ if (hasNode(inputSwimlanes, GIT_HISTORY_INCOMING_CHANGES_ID, GIT_HISTORY_REMOTE_REF_COLOR)) {+ return+ }++ const remoteMergeBaseIndex = outputSwimlanes.findIndex(+ (node) => node.id === mergeBase && node.color === GIT_HISTORY_REMOTE_REF_COLOR+ )+ inputSwimlanes.splice(+ remoteMergeBaseIndex === -1 ? inputSwimlanes.length : remoteMergeBaseIndex,+ 0,+ { id: GIT_HISTORY_INCOMING_CHANGES_ID, color: GIT_HISTORY_REMOTE_REF_COLOR }+ )+}++export function addIncomingOutgoingChangesHistoryItems(+ viewModels: GitHistoryItemViewModel[],+ currentRef?: GitHistoryItemRef,+ remoteRef?: GitHistoryItemRef,+ addIncomingChanges?: boolean,+ addOutgoingChanges?: boolean,+ mergeBase?: string+): void {+ if (currentRef?.revision === remoteRef?.revision || !mergeBase) {+ return+ }++ if (addIncomingChanges && remoteRef && remoteRef.revision !== mergeBase) {+ addIncomingChangesHistoryItem(viewModels, remoteRef, mergeBase)+ }++ if (addOutgoingChanges && currentRef?.revision && currentRef.revision !== mergeBase) {+ addOutgoingChangesHistoryItem(viewModels, currentRef)+ }+}++function addIncomingChangesHistoryItem(+ viewModels: GitHistoryItemViewModel[],+ remoteRef: GitHistoryItemRef,+ mergeBase: string+): void {+ const beforeHistoryItemIndex = findLastIndex(viewModels, (viewModel) =>+ viewModel.outputSwimlanes.some((node) => node.id === mergeBase)+ )+ const afterHistoryItemIndex = viewModels.findIndex(+ (viewModel) => viewModel.historyItem.id === mergeBase+ )+ if (afterHistoryItemIndex === -1) {+ return+ }++ const before = beforeHistoryItemIndex !== -1 ? viewModels[beforeHistoryItemIndex] : undefined+ const incomingChangeMerged =+ before?.historyItem.parentIds.length === 2 && before.historyItem.parentIds.includes(mergeBase)+ if (incomingChangeMerged) {+ return+ }++ const after = viewModels[afterHistoryItemIndex] as GitHistoryItemViewModel+ const inputSwimlanes =+ before?.outputSwimlanes.map((node) => remoteBoundaryInputNode(node, mergeBase)) ??+ after.inputSwimlanes.map(cloneNode)+ const outputSwimlanes = after.inputSwimlanes.map(cloneNode)+ ensureIncomingRemoteLane(inputSwimlanes, outputSwimlanes, mergeBase)++ if (before !== undefined) {+ viewModels[beforeHistoryItemIndex] = {+ ...before,+ inputSwimlanes: before.inputSwimlanes.map((node) => remoteBoundaryInputNode(node, mergeBase)),+ outputSwimlanes: inputSwimlanes.map(cloneNode)+ }+ }++ const displayIdLength = viewModels[0]?.historyItem.displayId?.length ?? 0+ const incomingChangesHistoryItem: GitHistoryItem = {+ id: GIT_HISTORY_INCOMING_CHANGES_ID,+ displayId: '0'.repeat(displayIdLength),+ parentIds: [mergeBase],+ author: remoteRef.name,+ subject: 'Incoming Changes',+ message: ''+ }++ viewModels.splice(afterHistoryItemIndex, 0, {+ historyItem: incomingChangesHistoryItem,+ kind: 'incoming-changes',+ inputSwimlanes,+ outputSwimlanes+ })++ viewModels[afterHistoryItemIndex + 1] = {+ ...after,+ inputSwimlanes: outputSwimlanes.map(cloneNode)+ }+}++function addOutgoingChangesHistoryItem(+ viewModels: GitHistoryItemViewModel[],+ currentRef: GitHistoryItemRef+): void {+ const currentRevision = currentRef.revision+ if (!currentRevision) {+ return+ }++ const currentRefIndex = viewModels.findIndex(+ (viewModel) => viewModel.kind === 'HEAD' && viewModel.historyItem.id === currentRevision+ )+ if (currentRefIndex === -1) {+ return+ }++ const displayIdLength = viewModels[0]?.historyItem.displayId?.length ?? 0+ const outgoingChangesHistoryItem: GitHistoryItem = {+ id: GIT_HISTORY_OUTGOING_CHANGES_ID,+ displayId: '0'.repeat(displayIdLength),+ parentIds: [currentRevision],+ author: currentRef.name,+ subject: 'Outgoing Changes',+ message: ''+ }++ const inputSwimlanes = viewModels[currentRefIndex]!.inputSwimlanes.map(cloneNode)+ const outputSwimlanes = inputSwimlanes.concat({+ id: currentRevision,+ color: GIT_HISTORY_REF_COLOR+ })++ viewModels.splice(currentRefIndex, 0, {+ historyItem: outgoingChangesHistoryItem,+ kind: 'outgoing-changes',+ inputSwimlanes,+ outputSwimlanes+ })++ viewModels[currentRefIndex + 1]!.inputSwimlanes.push({+ id: currentRevision,+ color: GIT_HISTORY_REF_COLOR+ })+}diff --git a/src/shared/git-history-graph.test.ts b/src/shared/git-history-graph.test.tsnew file mode 100644index 000000000..e7c6bd2d3--- /dev/null+++ b/src/shared/git-history-graph.test.ts@@ -0,0 +1,35 @@+import { describe, expect, it } from 'vitest'+import { buildGitHistoryViewModels, buildDefaultGitHistoryColorMap } from './git-history-graph'+import { dedupeRemoteTrackingRefs } from './git-history-ref-display'+import type { GitHistoryItem, GitHistoryItemRef } from './git-history-types'++const commit = (id: string, parentIds: string[], refs: GitHistoryItemRef[] = []): GitHistoryItem => ({+ id, parentIds, subject: id, message: id, references: refs+})++describe('buildGitHistoryViewModels', () => {+ it('marks the current revision as the HEAD node', () => {+ const items = [commit('aaa', ['bbb']), commit('bbb', [])]+ const currentRef: GitHistoryItemRef = { id: 'refs/heads/main', name: 'main', revision: 'aaa', category: 'branches' }+ const vms = buildGitHistoryViewModels(items, buildDefaultGitHistoryColorMap({ currentRef }), currentRef)+ expect(vms[0]!.kind).toBe('HEAD')+ expect(vms[1]!.kind).toBe('node')+ })++ it('gives a linear chain one output swimlane per non-root commit', () => {+ const items = [commit('aaa', ['bbb']), commit('bbb', [])]+ const vms = buildGitHistoryViewModels(items)+ expect(vms[0]!.outputSwimlanes).toHaveLength(1)+ expect(vms[1]!.outputSwimlanes).toHaveLength(0)+ })+})++describe('dedupeRemoteTrackingRefs', () => {+ it('drops origin/main when local main is present', () => {+ const refs: GitHistoryItemRef[] = [+ { id: 'refs/heads/main', name: 'main', category: 'branches' },+ { id: 'refs/remotes/origin/main', name: 'origin/main', category: 'remote branches' }+ ]+ expect(dedupeRemoteTrackingRefs(refs).map((r) => r.name)).toEqual(['main'])+ })+})diff --git a/src/shared/git-history-graph.ts b/src/shared/git-history-graph.tsnew file mode 100644index 000000000..0af09f200--- /dev/null+++ b/src/shared/git-history-graph.ts@@ -0,0 +1,216 @@+import type { GitHistoryGraphColorId, GitHistoryItem, GitHistoryItemRef } from './git-history'+import {+ GIT_HISTORY_INCOMING_CHANGES_ID,+ GIT_HISTORY_OUTGOING_CHANGES_ID,+ addIncomingOutgoingChangesHistoryItems+} from './git-history-boundary-rows'+import {+ GIT_HISTORY_BASE_REF_COLOR,+ GIT_HISTORY_LANE_COLORS,+ GIT_HISTORY_REF_COLOR,+ GIT_HISTORY_REMOTE_REF_COLOR+} from './git-history'++export { GIT_HISTORY_INCOMING_CHANGES_ID, GIT_HISTORY_OUTGOING_CHANGES_ID }++export type GitHistoryGraphNode = {+ id: string+ color: GitHistoryGraphColorId+}++export type GitHistoryItemViewModel = {+ historyItem: GitHistoryItem+ inputSwimlanes: GitHistoryGraphNode[]+ outputSwimlanes: GitHistoryGraphNode[]+ kind: 'HEAD' | 'node' | 'incoming-changes' | 'outgoing-changes'+}++function rotate(index: number, length: number): number {+ return ((index % length) + length) % length+}++function cloneNode(node: GitHistoryGraphNode): GitHistoryGraphNode {+ return { id: node.id, color: node.color }+}++function findLastIndex<T>(items: readonly T[], predicate: (item: T) => boolean): number {+ for (let index = items.length - 1; index >= 0; index -= 1) {+ if (predicate(items[index] as T)) {+ return index+ }+ }+ return -1+}++function findLastNodeIndex(nodes: readonly GitHistoryGraphNode[], id: string): number {+ return findLastIndex(nodes, (node) => node.id === id)+}++function getLabelColorIdentifier(+ historyItem: GitHistoryItem,+ colorMap: Map<string, GitHistoryGraphColorId | undefined>+): GitHistoryGraphColorId | undefined {+ if (historyItem.id === GIT_HISTORY_INCOMING_CHANGES_ID) {+ return GIT_HISTORY_REMOTE_REF_COLOR+ }+ if (historyItem.id === GIT_HISTORY_OUTGOING_CHANGES_ID) {+ return GIT_HISTORY_REF_COLOR+ }+ for (const ref of historyItem.references ?? []) {+ const color = colorMap.get(ref.id)+ if (color !== undefined) {+ return color+ }+ }+ return undefined+}++export function compareGitHistoryRefs(+ ref1: GitHistoryItemRef,+ ref2: GitHistoryItemRef,+ currentRef?: GitHistoryItemRef,+ remoteRef?: GitHistoryItemRef,+ baseRef?: GitHistoryItemRef+): number {+ const order = (ref: GitHistoryItemRef): number => {+ if (ref.id === currentRef?.id) {+ return 1+ }+ if (ref.id === remoteRef?.id) {+ return 2+ }+ if (ref.id === baseRef?.id) {+ return 3+ }+ if (ref.color !== undefined) {+ return 4+ }+ return 99+ }++ return order(ref1) - order(ref2)+}++export function buildGitHistoryViewModels(+ historyItems: GitHistoryItem[],+ colorMap = new Map<string, GitHistoryGraphColorId | undefined>(),+ currentRef?: GitHistoryItemRef,+ remoteRef?: GitHistoryItemRef,+ baseRef?: GitHistoryItemRef,+ addIncomingChanges?: boolean,+ addOutgoingChanges?: boolean,+ mergeBase?: string+): GitHistoryItemViewModel[] {+ let colorIndex = -1+ const viewModels: GitHistoryItemViewModel[] = []++ for (const historyItem of historyItems) {+ const kind = historyItem.id === currentRef?.revision ? 'HEAD' : 'node'+ const inputSwimlanes = (viewModels.at(-1)?.outputSwimlanes ?? []).map(cloneNode)+ const outputSwimlanes: GitHistoryGraphNode[] = []+ let firstParentAdded = false++ if (historyItem.parentIds.length > 0) {+ for (const node of inputSwimlanes) {+ if (node.id === historyItem.id) {+ if (!firstParentAdded) {+ outputSwimlanes.push({+ id: historyItem.parentIds[0]!,+ color: getLabelColorIdentifier(historyItem, colorMap) ?? node.color+ })+ firstParentAdded = true+ }+ continue+ }+ outputSwimlanes.push(cloneNode(node))+ }+ }++ for (let index = firstParentAdded ? 1 : 0; index < historyItem.parentIds.length; index += 1) {+ let colorIdentifier: GitHistoryGraphColorId | undefined+ if (index === 0) {+ colorIdentifier = getLabelColorIdentifier(historyItem, colorMap)+ } else {+ const parent = historyItems.find((item) => item.id === historyItem.parentIds[index])+ colorIdentifier = parent ? getLabelColorIdentifier(parent, colorMap) : undefined+ }++ if (!colorIdentifier) {+ colorIndex = rotate(colorIndex + 1, GIT_HISTORY_LANE_COLORS.length)+ colorIdentifier = GIT_HISTORY_LANE_COLORS[colorIndex]!+ }++ outputSwimlanes.push({+ id: historyItem.parentIds[index]!,+ color: colorIdentifier+ })+ }++ const references = (historyItem.references ?? [])+ .map((ref) => {+ let color = colorMap.get(ref.id)+ if (colorMap.has(ref.id) && color === undefined) {+ const inputIndex = inputSwimlanes.findIndex((node) => node.id === historyItem.id)+ const circleIndex = inputIndex !== -1 ? inputIndex : inputSwimlanes.length+ color =+ circleIndex < outputSwimlanes.length+ ? outputSwimlanes[circleIndex]!.color+ : circleIndex < inputSwimlanes.length+ ? inputSwimlanes[circleIndex]!.color+ : GIT_HISTORY_REF_COLOR+ }+ return { ...ref, color }+ })+ .sort((ref1, ref2) => compareGitHistoryRefs(ref1, ref2, currentRef, remoteRef, baseRef))++ viewModels.push({+ historyItem: { ...historyItem, references },+ kind,+ inputSwimlanes,+ outputSwimlanes+ })+ }++ addIncomingOutgoingChangesHistoryItems(+ viewModels,+ currentRef,+ remoteRef,+ addIncomingChanges,+ addOutgoingChanges,+ mergeBase+ )++ return viewModels+}++export function getGitHistoryItemLaneIndex(viewModel: GitHistoryItemViewModel): number {+ const inputIndex = viewModel.inputSwimlanes.findIndex(+ (node) => node.id === viewModel.historyItem.id+ )+ return inputIndex !== -1 ? inputIndex : viewModel.inputSwimlanes.length+}++export function getGitHistoryMergeParentLaneIndex(+ viewModel: GitHistoryItemViewModel,+ parentId: string+): number {+ return findLastNodeIndex(viewModel.outputSwimlanes, parentId)+}++export function buildDefaultGitHistoryColorMap(input: {+ currentRef?: GitHistoryItemRef+ remoteRef?: GitHistoryItemRef+ baseRef?: GitHistoryItemRef+}): Map<string, GitHistoryGraphColorId | undefined> {+ const colorMap = new Map<string, GitHistoryGraphColorId | undefined>()+ if (input.currentRef) {+ colorMap.set(input.currentRef.id, GIT_HISTORY_REF_COLOR)+ }+ if (input.remoteRef) {+ colorMap.set(input.remoteRef.id, GIT_HISTORY_REMOTE_REF_COLOR)+ }+ if (input.baseRef) {+ colorMap.set(input.baseRef.id, GIT_HISTORY_BASE_REF_COLOR)+ }+ return colorMap+}diff --git a/src/shared/git-history-ref-display.ts b/src/shared/git-history-ref-display.tsnew file mode 100644index 000000000..730057bbd--- /dev/null+++ b/src/shared/git-history-ref-display.ts@@ -0,0 +1,55 @@+import type { GitHistoryItemRef } from './git-history-types'++type DedupeRemoteTrackingRefsOptions = {+ preserveRefIds?: ReadonlySet<string> | readonly string[]+}++/** Split `origin/feature` into `{ remoteName, branchName }`; null if not splittable. */+function splitRemoteBranchName(refName: string): { remoteName: string; branchName: string } | null {+ const slashIndex = refName.indexOf('/')+ if (slashIndex <= 0 || slashIndex === refName.length - 1) {+ return null+ }+ return { remoteName: refName.slice(0, slashIndex), branchName: refName.slice(slashIndex + 1) }+}++function isAmbiguousRemoteTrackingRef(refName: string): boolean {+ // Without configured remote names, `foo/bar/main` is ambiguous; keep both pills.+ return refName.split('/').length > 2+}++function countUnambiguousMatchingRemoteBranches(+ refs: readonly GitHistoryItemRef[],+ localBranchNames: ReadonlySet<string>+): Map<string, number> {+ const counts = new Map<string, number>()+ for (const ref of refs) {+ if (ref.category !== 'remote branches' || isAmbiguousRemoteTrackingRef(ref.name)) continue+ const split = splitRemoteBranchName(ref.name)+ if (!split || !localBranchNames.has(split.branchName)) continue+ counts.set(split.branchName, (counts.get(split.branchName) ?? 0) + 1)+ }+ return counts+}++// Drops a remote-tracking ref (origin/feature) when the matching local branch+// (feature) sits on the same commit — the two pills are redundant there.+export function dedupeRemoteTrackingRefs(+ refs: readonly GitHistoryItemRef[],+ options: DedupeRemoteTrackingRefsOptions = {}+): GitHistoryItemRef[] {+ const localBranchNames = new Set(+ refs.filter((ref) => ref.category === 'branches').map((ref) => ref.name)+ )+ if (localBranchNames.size === 0) return [...refs]+ const preserveRefIds = new Set(options.preserveRefIds ?? [])+ const matchingRemoteCounts = countUnambiguousMatchingRemoteBranches(refs, localBranchNames)+ return refs.filter((ref) => {+ if (ref.category !== 'remote branches') return true+ if (preserveRefIds.has(ref.id)) return true+ if (isAmbiguousRemoteTrackingRef(ref.name)) return true+ const split = splitRemoteBranchName(ref.name)+ if (!split || !localBranchNames.has(split.branchName)) return true+ return matchingRemoteCounts.get(split.branchName) !== 1+ })+}