Files
gabrielvidalandClaude Opus 5.5 51517b2474 feat(projects): deduce a worker conversation's project from where it wrote
On a worker (the Orus MacBook, a guest's computer) a conversation was only
filed under a project when it edited ~/projects/<slug> — everything else
landed on the homelab root or the Work fallback. The hub can't see a
worker's disk, so read the project off the transcripts, per worker:

- parser: writtenDirsAuto, gitRootsAuto (+ the hook's new `main`),
  worktreeAddsAuto (`git worktree add`) — PARSER_VERSION 42
- projects/worker_projects.py: git roots exact, worktrees folded into their
  repo (.claude/worktrees, worktree add, hook main, <repo>-<x> siblings),
  else the folder above the first src/docs/apps… segment, else the cwd;
  containers (~/PROJETS, ~/WORK/ORUS) and ~/Downloads, ~/Desktop, temp dirs
  never count; a conversation that wrote nothing → its cwd's project
- slug <folder>@<worker id>; owner workers map onto same-named lab projects,
  guests' computers never do; one attribution rule for cards and costs
- projects/merges.py: fold a lab project into a worker one
  (orus-monorepo → orus on the Orus MacBook), GET/PUT /api/project-merges
- /api/projects + /api/project serve them (worker, path, mergedFrom);
  a guest sees their own computers'
- sidecar bash_diff: a linked worktree's record names its main checkout

On the real transcripts: fedora 13/13 → 7 projects, Orus Mac → orus (316),
gh-pr-board, gh-pr-kanban, gh-qol.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 17:52:39 +01:00

77 lines
2.7 KiB
Python

"""What a transcript says about the repos it wrote in — the raw material
``projects/workers.py`` deduces a worker conversation's project from.
The hub never sees a worker's disk, so a project there can only be read off
the transcript: the files a conversation wrote (``ParserState.written_dirs``),
the git roots the Bash hook recorded around its commands (``bashDiff`` /
``gitCommits`` → ``repos[].root``, plus ``main`` — the main checkout of a
linked worktree — from hooks that report it) and the ``git worktree add``
commands it ran, which tie a worktree's path to the repo it was cut from.
"""
from __future__ import annotations
import posixpath
import re
import shlex
# `git worktree add [opts] <path> [<commit-ish>]`, optionally `git -C <dir>`,
# up to the end of that shell command.
_ADD_RE = re.compile(r"\bgit(?P<c>(?:\s+-C\s+\S+)?)\s+worktree\s+add\b(?P<args>[^;&|\n`]*)")
# A leading `cd <dir> &&` sets where the command runs.
_CD_RE = re.compile(r"^\s*cd\s+(\S+)\s*&&")
# `git worktree add` options that take a value.
_VALUED = {"-b", "-B", "--reason", "--orphan"}
def _unquote(s: str) -> str:
try:
parts = shlex.split(s)
except ValueError:
return s.strip("'\"")
return parts[0] if parts else ""
def worktree_adds(command: str | None, cwd: str | None) -> list[dict]:
"""``[{"path", "repo"}]`` — each worktree a command created and the
directory it was created from (the repo, or a folder inside it). Paths
with a shell variable (``$W``) can't be known and are skipped."""
if not command or "worktree" not in command or not cwd:
return []
base = cwd
m = _CD_RE.match(command)
if m:
d = _unquote(m.group(1))
if d and "$" not in d:
base = posixpath.normpath(posixpath.join(cwd, d))
out: list[dict] = []
for m in _ADD_RE.finditer(command):
repo = base
c = m.group("c").split()
if len(c) == 2:
if "$" in c[1]:
continue
repo = posixpath.normpath(posixpath.join(base, _unquote(c[1])))
try:
args = shlex.split(m.group("args"), comments=False)
except ValueError:
args = m.group("args").split()
path, skip = None, False
for a in args:
if skip:
skip = False
continue
if a in _VALUED:
skip = True
continue
if a.startswith("-") or a in ("2>&1",) or a.startswith((">", "2>")):
continue
path = a
break
if not path or "$" in path or "<" in path:
continue
path = posixpath.normpath(posixpath.join(repo, path))
if path != repo:
out.append({"path": path, "repo": repo})
return out