codeburn/app/renderer/lib/projectMatch.ts
Resham Joshi bc62059744
feat: group projects by git repository (#1691)
* feat(app): transient project selector in the desktop top bar

Scopes every report to one project path for the current session through
the same narrowing the VS Code workspace scope uses. The saved project
filter is never written; a restart is back on all projects.

* fix(app): let the name pickers ellipsize before the top bar overflows

* fix: key the daily project split per real path, not per label

A project label can span several paths: every Claude session started from the
home folder shares -Users-<name>, while its cwd puts it in a different project.
The day rollup keyed by label kept the first path it saw, so a rooted
--project headline took the whole label bucket on some days and missed it on
others. Key per (label, path), read old label-keyed days unchanged, bump the
daily cache to v68.

* fix(app): list only selectable, non-zero projects in the top bar picker

Rows with no session cwd (the Cursor and Grok Bot imports, provider label
fallbacks) report their label as the path, so the picker offered names the
bridge refuses and dash-to-slash guesses like eywa/lab. Keep only absolute
paths, and drop rows whose lifetime cost rounds to $0.00. The old test that
rooted a slashless Codex path is replaced: Codex now records the absolute cwd,
and a slashless path is always the label fallback.

* fix(app): show a toast when a project pick is refused

The pick was dropped silently. Also pin that an encoded -Users-... label
never reaches the picker list.

* feat: group projects by git repository

Clones and worktrees that share an origin remote are one project in every
project list (report JSON, By Project, the top bar picker, the terminal
dashboard), with the checkouts listed under the row. A --project path inside
a checkout scopes to the whole repository; --exact-project keeps the old
single-folder match. The origin is learned while the folder exists, kept in
git-origins.json and on each day's project entry (daily cache v69), so a
deleted scratch clone stays in its repository. The report JSON project list
now comes from the durable day set, so a row costs what selecting it reports.

* fix: a picked project row selects that row exactly

The top bar pick sends =<path>: a checkout in a repository still selects the
whole repository, and a plain folder selects itself without the folders under
it, which are rows of their own. So the picker row and the headline it opens
are the same set. Typed --project paths keep their prefix match. Days adopted
from an older cache learn the origin of every checkout that still exists, and
lookups outside a parse are saved on exit.

* fix: key the status snapshot on --exact-project

* feat: one Temporary folders row for scratch folders outside a repository

Every project path under a temp root (os.tmpdir(), /tmp, /var/folders and
their /private spellings) that is not a checkout of a known repository folds
into one row, in the report JSON, By Project, the terminal dashboard, the top
bar picker and the Settings Projects pane. Its pattern @temp selects or hides
all of them. The Projects pane leaves out rows that round to $0.00 unless a
saved pattern names them, and a saved pattern naming any checkout of a
repository row now names the whole row in the renderer too.

* fix(claude): attribute each call to the project of its own cwd

* test: fold the temp roots the tests set, compare checkout ids as identities (Windows)

* fix(claude): keep subfolder cwds in their project, count and show a split session once

* fix: an exclude path hides that checkout only, repository rows count a session once

* fix(claude): a deleted subfolder stays its own project
2026-10-08 06:45:26 +02:00

77 lines
3.9 KiB
TypeScript

/** The project filter rule, restated for the renderer: src/parser.ts reaches
* for node:fs, so the pane cannot import it. tests/project-match-parity.test.ts
* pins the two against each other, because a switch that disagrees with the
* CLI lies. It sits in the CLI suite because it needs both halves, and the
* desktop CI job installs only app/ dependencies. */
type MatchTarget = { name: string; path?: string; checkouts?: Array<{ path: string }>; temporary?: boolean }
/** The CLI's pattern (and the path of its row) for every temp-root folder
* outside a known repository. */
export const TEMPORARY_PROJECTS = '@temp'
export function isRooted(pattern: string): boolean {
const raw = pattern.trim().replace(/\\/g, '/')
return raw.startsWith('/') || /^[a-zA-Z]:\//.test(raw)
}
/** normalizeAbsProjectPathKey: identified Windows paths casefold, POSIX does not. */
export function absProjectPathKey(value: string): string | null {
const raw = value.trim().replace(/\\/g, '/')
if (!raw) return null
if (!raw.startsWith('/') && !/^[a-zA-Z]:\//.test(raw) && !(raw.includes('/') && !raw.startsWith('-'))) return null
const windows = /^[a-zA-Z]:(\/|$)/.test(raw) || raw.startsWith('//')
return (windows ? raw.toLowerCase() : raw).replace(/^\/+/, '').replace(/\/+$/, '') || null
}
export function projectMatches(project: MatchTarget, pattern: string): boolean {
const projectPath = project.path ?? ''
// "=/path": that row's folder alone (the CLI widens it to the repository).
if (pattern.startsWith('=') && isRooted(pattern.slice(1))) {
const anchor = absProjectPathKey(pattern.slice(1))
return anchor !== null && anchor === absProjectPathKey(projectPath)
}
if (isRooted(pattern)) {
const anchor = absProjectPathKey(pattern)
const target = absProjectPathKey(projectPath)
return anchor !== null && target !== null && (target === anchor || target.startsWith(anchor + '/'))
}
const needle = pattern.toLowerCase()
return project.name.toLowerCase().includes(needle) || projectPath.toLowerCase().includes(needle)
}
/** Two rows can share a name (repo and subdirectory), so the path is the key. */
export function projectPattern(project: MatchTarget): string {
const raw = (project.path ?? '').trim()
if (!raw) return project.name
// A Codex-recorded path can arrive stripped of its leading slash. Rooting it
// is what routes the pattern through the anchored branch, instead of a
// substring that would hide the siblings sharing its prefix too.
const rooted = raw.startsWith('/') || /^[a-zA-Z]:[\\/]/.test(raw) || !raw.includes('/')
return rooted ? raw : `/${raw}`
}
function checkoutsOf(project: MatchTarget): MatchTarget[] {
return !project.temporary && project.checkouts?.length ? project.checkouts.map(c => ({ name: project.name, path: c.path })) : [project]
}
/** A pattern naming any checkout of a repository row names the whole
* repository, as in the CLI. */
export function projectNamedBy(project: MatchTarget, pattern: string): boolean {
return projectMatches(project, pattern) || checkoutsOf(project).some(c => projectMatches(c, pattern))
}
/** The pattern that hides a whole row: "=" names its repository, a plain path
* would hide one checkout only. */
export function projectHidePattern(project: MatchTarget): string {
const pattern = projectPattern(project)
return checkoutsOf(project).length > 1 && isRooted(pattern) ? `=${pattern}` : pattern
}
/** Excludes hide a repository row only as a whole: through "=", or with every
* checkout excluded. An include naming any checkout shows the row. */
export function projectVisible(project: MatchTarget, filter: { project: string[]; exclude: string[] }): boolean {
if (filter.exclude.some(pattern => pattern.startsWith('=') && projectNamedBy(project, pattern))) return false
if (checkoutsOf(project).every(c => filter.exclude.some(pattern => projectMatches(c, pattern)))) return false
return filter.project.length === 0 || filter.project.some(pattern => projectNamedBy(project, pattern))
}