diff --git a/lane-start b/lane-start index f824fbd2..99cfd910 100755 --- a/lane-start +++ b/lane-start @@ -1082,30 +1082,87 @@ fi # options at `--` like every other POSIX utility, and the step line this # function produces — `repo repocase → repoCase` — is asserted on the macOS job, # where an `ls` that refused `--` would leave it silent. -projects_dir_matches() { # - ls -- "$PROJECTS_ROOT" 2>/dev/null | - awk -v want="$1" 'BEGIN { w = tolower(want) } tolower($0) == w { print }' | - while IFS= read -r pdm_m; do - [ -d "$PROJECTS_ROOT/$pdm_m" ] && printf '%s\n' "$pdm_m" +# BEGIN GROUPED PROJECT DISCOVERY +# Self-contained installed commands share this block; a regression test checks +# byte equality. Rows carry name, logical path and physical deduplication key. +# Keep logical paths for estate recognition; never enter a repo, estate or +# supporting checkout collection while looking for grouping folders. +project_directory_walk() { + local base="$1" depth="$2" mode="$3" families="${4-}" child name physical boundary family_container + [ -d "$base" ] || return 0 + for child in "$base"/*; do + [ -d "$child" ] || continue + name="${child##*/}" + # Per-lane register clones live beside any workspace as -lanes. + case "$name" in workTrees|worktrees|laneWorkspaces|*-lanes|*-lane-registers) continue ;; esac + case "$child" in *$'\t'*|*$'\n'*) continue ;; esac + physical="$(cd -- "$child" 2>/dev/null && pwd -P)" || continue + case "${physical##*/}" in workTrees|worktrees|laneWorkspaces|*-lanes|*-lane-registers) continue ;; esac + case "$physical" in *$'\t'*|*$'\n'*) continue ;; esac + boundary=0; family_container=0 + if [ -e "$child/$name/family.yaml" ] || [ -e "$physical/${physical##*/}/family.yaml" ]; then + family_container=1 + fi + case $'\n'"$families"$'\n' in *$'\n'"$physical"$'\n'*) boundary=1 ;; esac + if [ -e "$child/.git" ] || [ -e "$child/project.yaml" ] || + [ -e "$child/family.yaml" ] || [ "$family_container" -eq 1 ] || + { [ -f "$child/HEAD" ] && [ -d "$child/objects" ]; }; then + boundary=1 + fi + if [ "$mode" = families ]; then + [ "$family_container" -eq 0 ] || printf '%s\n' "$physical" + elif [ "$depth" -eq 0 ] || [ "$boundary" -eq 1 ]; then + printf '%s\t%s\t%s\n' "$name" "$child" "$physical" + fi + if [ "$boundary" -eq 0 ] && [ "$depth" -lt 2 ]; then + project_directory_walk "$child" "$((depth + 1))" "$mode" "$families" + fi done } + +project_directory_rows() { + local base="$1" families root + shift + [ "$#" -gt 0 ] || set -- "$base" + # Resolve boundaries across every configured root before emitting rows. + # A family alias in another root must also hide this container's members. + families="$(for root in "$@"; do project_directory_walk "$root" 0 families; done)" + project_directory_walk "$base" 0 rows "$families" +} +# END GROUPED PROJECT DISCOVERY + +project_matches() { + local match_name match_dir git_dir common_dir canonical_name _logical_dir scope="${2:-grouped}" + while IFS=$'\t' read -r match_name _logical_dir match_dir; do + if [ "$scope" = flat ] && [ "${_logical_dir%/*}" != "$PROJECTS_ROOT" ]; then continue; fi + if [ -f "$match_dir/HEAD" ] && [ -d "$match_dir/objects" ]; then continue; fi + if [ -e "$match_dir/.git" ]; then + git_dir="$(git -C "$match_dir" rev-parse --git-dir 2>/dev/null)" || continue + common_dir="$(git -C "$match_dir" rev-parse --git-common-dir 2>/dev/null)" || continue + [ "$git_dir" = "$common_dir" ] || continue + [ -z "$(git -C "$match_dir" rev-parse --show-superproject-working-tree 2>/dev/null)" ] || continue + fi + canonical_name="${match_dir##*/}" + if [ "$(printf '%s' "$canonical_name" | tr '[:upper:]' '[:lower:]')" = "$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')" ]; then + match_name="$canonical_name" + fi + printf '%s\t%s\n' "$match_name" "$match_dir" + done < <(project_directory_rows "$PROJECTS_ROOT" | + awk -F '\t' -v want="$1" 'BEGIN { w = tolower(want) } tolower($1) == w') | awk -F '\t' '!seen[$2]++' +} +projects_dir_matches() { + project_matches "$1" | cut -f1 +} + if [ -n "$repo" ] && [ -d "$PROJECTS_ROOT" ]; then repo_hits="$(projects_dir_matches "$repo" || :)" repo_hit_n=0 if [ -n "$repo_hits" ]; then repo_hit_n="$(printf '%s\n' "$repo_hits" | grep -c . || :)"; fi - # THE REFUSAL IS FOR A RUN THAT HAS NO OTHER ANSWER, AND `--dir` IS THE OTHER - # ANSWER. Its own remedy names `--dir `, so refusing a run that already - # passed one is refusing the fix it printed: `lane-start --dir /chosen repotwin - # 1` was rejected before the rung that honours the flag (Copilot round 1 on - # openRepoTools#41). With the checkout named, the ambiguity that is left is - # only about the lane-s NAME, which keeps the spelling that was typed and is - # then overruled by the register row-s a few lines below — so this SAYS what - # it did not resolve and carries on. - if [ "$repo_hit_n" -gt 1 ] && [ -z "$dir_override" ]; then - die "$PROJECTS_ROOT holds $repo_hit_n directories whose names differ only by case for '$repo': $(printf '%s\n' "$repo_hits" | tr '\n' ' ')— a lane name is ONE name under any case (Amendment 15(a)), so there is no spelling of '$repo' this can take without choosing which repository the lane is in. Name the checkout yourself with --dir , or rename one of those directories." 2 - fi - if [ "$repo_hit_n" -gt 1 ]; then - note "$PROJECTS_ROOT holds $repo_hit_n directories whose names differ only by case for '$repo': $(printf '%s\n' "$repo_hits" | tr '\n' ' ')— --dir has named the checkout, so this run is not ambiguous about WHERE the lane is. Its NAME keeps the '$repo' you typed, and the register row's own spelling wins over that below (Amendment 15(a))." + # A unique physical target supplies canonical spelling. Multiple targets + # wait for the explicit/recorded directory rungs: only a new default must + # refuse to choose among them. + if [ "$repo_hit_n" -gt 1 ] && [ -n "$dir_override" ]; then + note "multiple project targets match '$repo' — --dir has named the checkout, so the explicit directory remains authoritative" fi if [ "$repo_hit_n" = 1 ] && [ "$repo_hits" != "$repo" ]; then step "repo $repo → $repo_hits (Amendment 15(a): the checkout's real directory name under $PROJECTS_ROOT)" @@ -1370,10 +1427,25 @@ if [ -n "$DIR" ] && [ ! -d "$DIR" ]; then die "no such directory: $DIR (from $DIR_SOURCE) — pass --dir if this lane's checkout is somewhere else" 1 fi if [ -z "$DIR" ]; then - if [ -d "$PROJECTS_ROOT/$repo" ]; then - DIR="$PROJECTS_ROOT/$repo"; DIR_SOURCE="the default \$PROJECTS_ROOT/" + ls_home=""; ls_rc=0 + ls_home="$("$LANES_EDIT" resolve-home "$LANE" 2>/dev/null)" || ls_rc=$? + [ "$ls_rc" = 0 ] || ls_home="" + case "$ls_home" in */*) : ;; *) ls_home="" ;; esac +fi +if [ -z "$DIR" ]; then + # Keep the historical flat default ahead of home-based recovery. Grouped + # name discovery is a new-lane default only: an older home-only record must + # reach rungs 5/6, where origin proves the checkout and rejects imposters. + default_scope=grouped + [ -z "$ls_home" ] || default_scope=flat + default_matches="$(project_matches "$repo" "$default_scope")" + if [ -n "$default_matches" ]; then + default_count="$(printf '%s\n' "$default_matches" | wc -l | tr -d ' ')" + [ "$default_count" -eq 1 ] || die "$PROJECTS_ROOT holds $default_count directories whose names differ only by case or grouping for '$repo': $default_matches — Amendment 15(a) requires one default target; pass --dir " 2 + DIR="$(printf '%s\n' "$default_matches" | cut -f2-)" + DIR_SOURCE="grouped project lookup under \$PROJECTS_ROOT" else - DIR_DEFAULT_TRIED="$PROJECTS_ROOT/$repo" + DIR_DEFAULT_TRIED="$PROJECTS_ROOT/$repo (including grouping folders)" fi fi @@ -1414,12 +1486,6 @@ fi # # BOTH ARE SKIPPED ENTIRELY WHERE THE LANE HAS NO RECORDED HOME, because there # is then nothing to prove a candidate against and a match would be a guess. -if [ -z "$DIR" ]; then - ls_home=""; ls_rc=0 - ls_home="$("$LANES_EDIT" resolve-home "$LANE" 2>/dev/null)" || ls_rc=$? - [ "$ls_rc" = 0 ] || ls_home="" - case "$ls_home" in */*) : ;; *) ls_home="" ;; esac -fi # Rung 5 — the legs. if [ -z "$DIR" ] && [ -n "$ls_home" ]; then for ls_m in "$PROJECTS_ROOT"/*/project.yaml "$PROJECTS_ROOT"/*/*/project.yaml; do @@ -1483,6 +1549,7 @@ if [ -z "$DIR" ]; then ls_where="$PROJECTS_ROOT/$repo" [ -z "$DIR_DEFAULT_TRIED" ] || ls_where="$DIR_DEFAULT_TRIED" ls_pos="${LANE##*-}" + ls_repo="${LANE%-*}" if [ -n "$ls_home" ]; then ls_tried="the default $ls_where, the project.yaml legs of $ls_home, and a checkout of $ls_home one or two levels under $PROJECTS_ROOT" else @@ -1491,7 +1558,7 @@ if [ -z "$DIR" ]; then die "lane $LANE has no recorded checkout and none could be PROVED, so nothing was started. Tried: $ls_tried. Name it once, and every later start reads it back out of the lane's own log: - lane-start --dir $repo $ls_pos + lane-start --dir $ls_repo $ls_pos Nothing here guesses. This script writes the lane HOME from the directory's origin, and a directory that merely has the right name would re-home the lane for every \`#n\` it writes afterwards (Amendment 11 clause (c); Evidence 7, diff --git a/park b/park index d558d053..58c2e30e 100755 --- a/park +++ b/park @@ -397,11 +397,85 @@ estate_walk_up() { return 1 } +# BEGIN GROUPED PROJECT DISCOVERY +# Self-contained installed commands share this block; a regression test checks +# byte equality. Rows carry name, logical path and physical deduplication key. +# Keep logical paths for estate recognition; never enter a repo, estate or +# supporting checkout collection while looking for grouping folders. +project_directory_walk() { + local base="$1" depth="$2" mode="$3" families="${4-}" child name physical boundary family_container + [ -d "$base" ] || return 0 + for child in "$base"/*; do + [ -d "$child" ] || continue + name="${child##*/}" + # Per-lane register clones live beside any workspace as -lanes. + case "$name" in workTrees|worktrees|laneWorkspaces|*-lanes|*-lane-registers) continue ;; esac + case "$child" in *$'\t'*|*$'\n'*) continue ;; esac + physical="$(cd -- "$child" 2>/dev/null && pwd -P)" || continue + case "${physical##*/}" in workTrees|worktrees|laneWorkspaces|*-lanes|*-lane-registers) continue ;; esac + case "$physical" in *$'\t'*|*$'\n'*) continue ;; esac + boundary=0; family_container=0 + if [ -e "$child/$name/family.yaml" ] || [ -e "$physical/${physical##*/}/family.yaml" ]; then + family_container=1 + fi + case $'\n'"$families"$'\n' in *$'\n'"$physical"$'\n'*) boundary=1 ;; esac + if [ -e "$child/.git" ] || [ -e "$child/project.yaml" ] || + [ -e "$child/family.yaml" ] || [ "$family_container" -eq 1 ] || + { [ -f "$child/HEAD" ] && [ -d "$child/objects" ]; }; then + boundary=1 + fi + if [ "$mode" = families ]; then + [ "$family_container" -eq 0 ] || printf '%s\n' "$physical" + elif [ "$depth" -eq 0 ] || [ "$boundary" -eq 1 ]; then + printf '%s\t%s\t%s\n' "$name" "$child" "$physical" + fi + if [ "$boundary" -eq 0 ] && [ "$depth" -lt 2 ]; then + project_directory_walk "$child" "$((depth + 1))" "$mode" "$families" + fi + done +} + +project_directory_rows() { + local base="$1" families root + shift + [ "$#" -gt 0 ] || set -- "$base" + # Resolve boundaries across every configured root before emitting rows. + # A family alias in another root must also hide this container's members. + families="$(for root in "$@"; do project_directory_walk "$root" 0 families; done)" + project_directory_walk "$base" 0 rows "$families" +} +# END GROUPED PROJECT DISCOVERY + +estate_directory_rows() { + local dir + for dir in "${PROJECTS_DIRS[@]}"; do + project_directory_rows "$dir" "${PROJECTS_DIRS[@]}" + done +} + +# Validate logical family names before deduplicating their physical containers. +recognized_estate_rows() { + local name child physical + while IFS=$'\t' read -r name child physical; do + estate_here "$child" || continue + printf '%s\t%s\t%s\n' "$name" "$child" "$physical" + done < <(estate_directory_rows) | awk -F '\t' '!seen[$3]++' +} + estate_by_name() { - local want="$1" dir + local want="$1" dir name child physical hits candidate is_safe_folder_name "$want" || return 1 for dir in "${PROJECTS_DIRS[@]}"; do - estate_here "$dir/$want" && return 0 + hits="$(while IFS=$'\t' read -r name child physical; do + [ "$name" = "$want" ] || continue + if estate_here "$child"; then printf '%s\t%s\n' "$child" "$physical"; fi + done < <(project_directory_rows "$dir" "${PROJECTS_DIRS[@]}") | awk -F '\t' '!seen[$2]++ {print $1}')" + [ -n "$hits" ] || continue + if [ "$(printf '%s\n' "$hits" | wc -l | tr -d ' ')" -gt 1 ]; then + die "multiple estates named '$want' under $dir; stand in the intended estate and run $SELF_NAME without : $hits" + fi + candidate="$hits" + estate_here "$candidate" && return 0 done return 1 } @@ -410,18 +484,15 @@ estate_by_name() { # a refusal to print: a person who typed the command in the wrong folder needs # the names, not an instruction to go and look for them. list_estates() { - local dir child - for dir in "${PROJECTS_DIRS[@]}"; do - [ -d "$dir" ] || continue - for child in "$dir"/*; do + local _grouped_name child _physical + while IFS=$'\t' read -r _grouped_name child _physical; do [ -d "$child" ] || continue if is_family_folder "$child"; then printf ' family %-20s %s\n' "${child##*/}" "$child" elif is_project_root "$child"; then printf ' project %-20s %s\n' "${child##*/}" "$child" fi - done - done + done < <(recognized_estate_rows) } estates_or_none() { @@ -454,12 +525,12 @@ estates_or_none() { # shellcheck disable=SC2329 # `park` never asks: it files the record, and # never looks a holder up by it. The block is byte-identical by design. family_folders_with_id() { - local want="$1" dir child prior dup + local want="$1" dir child prior dup _grouped_name _physical local -a found=() [ -n "$want" ] || return 0 for dir in "${PROJECTS_DIRS[@]}"; do [ -d "$dir" ] || continue - for child in "$dir"/*; do + while IFS=$'\t' read -r _grouped_name child _physical; do [ -d "$child" ] || continue is_family_folder "$child" || continue yaml_scalar "$child/${child##*/}/family.yaml" id || continue @@ -471,7 +542,7 @@ family_folders_with_id() { [ "$dup" -eq 0 ] || continue found+=("$child") printf '%s\n' "$child" - done + done < <(project_directory_rows "$dir" "${PROJECTS_DIRS[@]}") done } @@ -522,28 +593,42 @@ repo_slug_of() { printf '%s/%s\n' "$owner" "$name" } -# The clone under the projects directory whose `origin` IS that repository - -# ruling 1. Two levels deep, because the estates this standard places are -# `/` and `//`. +# Match origins only in discovered development checkouts, including a +# family's direct sibling checkouts. Never descend into pinned Git members. clone_with_origin() { - local want dir candidate origin found + local want candidate origin found hits child name physical git_dir common_dir git_physical rank want="$(lower "$1")" - for dir in "${PROJECTS_DIRS[@]}"; do - [ -d "$dir" ] || continue - for candidate in "$dir"/*/ "$dir"/*/*/; do - candidate="${candidate%/}" + hits="$(while IFS=$'\t' read -r name child physical; do + for candidate in "$child" "$child"/*; do + if [ "$candidate" != "$child" ]; then + is_family_folder "$child" || continue + fi + case "$candidate" in *$'\t'*|*$'\n'*) continue ;; esac [ -e "$candidate/.git" ] || continue + git_dir="$(git -C "$candidate" rev-parse --git-dir 2>/dev/null)" || continue + common_dir="$(git -C "$candidate" rev-parse --git-common-dir 2>/dev/null)" || continue + [ "$git_dir" = "$common_dir" ] || continue + [ -z "$(git -C "$candidate" rev-parse --show-superproject-working-tree 2>/dev/null)" ] || continue origin="$(git -C "$candidate" remote get-url origin 2>/dev/null || true)" [ -n "$origin" ] || continue found="$(repo_slug_of "$origin" || true)" [ -n "$found" ] || continue if [ "$(lower "$found")" = "$want" ]; then - printf '%s\n' "$candidate" - return 0 + git_physical="$(cd -- "$candidate" && pwd -P)" || continue + rank=1 + estate_walk_up "$candidate" && rank=0 + printf '%s\t%s\t%s\n' "$git_physical" "$rank" "$candidate" fi done - done - return 1 + done < <(estate_directory_rows) | + LC_ALL=C sort -s -t $'\t' -k1,1 -k2,2n | + awk -F '\t' '!seen[$1]++ {print $3}')" + [ -n "$hits" ] || return 1 + if [ "$(printf '%s\n' "$hits" | wc -l | tr -d ' ')" -gt 1 ]; then + printf 'multiple development checkouts have origin %s; stand in the intended checkout: %s\n' "$1" "$hits" >&2 + return 2 + fi + printf '%s\n' "$hits" } # The family's members, by the same ` - project: ` row `family.py` @@ -931,16 +1016,13 @@ run_one_estate() { # `members/` copy (three levels down) and a linked feature worktree # (rejected by `is_project_root` itself). all_estate_dirs() { - local dir child - for dir in "${PROJECTS_DIRS[@]}"; do - [ -d "$dir" ] || continue - for child in "$dir"/*; do + local _grouped_name child _physical + while IFS=$'\t' read -r _grouped_name child _physical; do [ -d "$child" ] || continue if is_family_folder "$child" || is_project_root "$child"; then printf '%s\t%s\n' "${child##*/}" "$child" fi - done - done | LC_ALL=C sort -t $'\t' -k1,1 + done < <(recognized_estate_rows) | LC_ALL=C sort -t $'\t' -k1,1 } # Does this root carry the Speckit git overlay its own `make park` requires? @@ -1243,7 +1325,9 @@ elif [ -n "$REPO_SLUG" ]; then url spelling of one (https://…//.git, git@host:/). A family's members may live in another organisation, so there is no owner to assume, and a path on disk has none to read." - MATCH="$(clone_with_origin "$REPO_SLUG" || true)" + LOOKUP_RC=0 + MATCH="$(clone_with_origin "$REPO_SLUG")" || LOOKUP_RC=$? + [ "$LOOKUP_RC" -ne 2 ] || die "ambiguous repository lookup; no checkout was selected" [ -n "$MATCH" ] || die "no clone under your projects directory has origin $REPO_SLUG. \`--repo\` names a clone you ALREADY HAVE; it never clones one (RULING 2026-09-09 ruling 1). Get it, or bring the whole estate back from its diff --git a/resume b/resume index 045d608c..5cdb5fa0 100755 --- a/resume +++ b/resume @@ -381,11 +381,85 @@ estate_walk_up() { return 1 } +# BEGIN GROUPED PROJECT DISCOVERY +# Self-contained installed commands share this block; a regression test checks +# byte equality. Rows carry name, logical path and physical deduplication key. +# Keep logical paths for estate recognition; never enter a repo, estate or +# supporting checkout collection while looking for grouping folders. +project_directory_walk() { + local base="$1" depth="$2" mode="$3" families="${4-}" child name physical boundary family_container + [ -d "$base" ] || return 0 + for child in "$base"/*; do + [ -d "$child" ] || continue + name="${child##*/}" + # Per-lane register clones live beside any workspace as -lanes. + case "$name" in workTrees|worktrees|laneWorkspaces|*-lanes|*-lane-registers) continue ;; esac + case "$child" in *$'\t'*|*$'\n'*) continue ;; esac + physical="$(cd -- "$child" 2>/dev/null && pwd -P)" || continue + case "${physical##*/}" in workTrees|worktrees|laneWorkspaces|*-lanes|*-lane-registers) continue ;; esac + case "$physical" in *$'\t'*|*$'\n'*) continue ;; esac + boundary=0; family_container=0 + if [ -e "$child/$name/family.yaml" ] || [ -e "$physical/${physical##*/}/family.yaml" ]; then + family_container=1 + fi + case $'\n'"$families"$'\n' in *$'\n'"$physical"$'\n'*) boundary=1 ;; esac + if [ -e "$child/.git" ] || [ -e "$child/project.yaml" ] || + [ -e "$child/family.yaml" ] || [ "$family_container" -eq 1 ] || + { [ -f "$child/HEAD" ] && [ -d "$child/objects" ]; }; then + boundary=1 + fi + if [ "$mode" = families ]; then + [ "$family_container" -eq 0 ] || printf '%s\n' "$physical" + elif [ "$depth" -eq 0 ] || [ "$boundary" -eq 1 ]; then + printf '%s\t%s\t%s\n' "$name" "$child" "$physical" + fi + if [ "$boundary" -eq 0 ] && [ "$depth" -lt 2 ]; then + project_directory_walk "$child" "$((depth + 1))" "$mode" "$families" + fi + done +} + +project_directory_rows() { + local base="$1" families root + shift + [ "$#" -gt 0 ] || set -- "$base" + # Resolve boundaries across every configured root before emitting rows. + # A family alias in another root must also hide this container's members. + families="$(for root in "$@"; do project_directory_walk "$root" 0 families; done)" + project_directory_walk "$base" 0 rows "$families" +} +# END GROUPED PROJECT DISCOVERY + +estate_directory_rows() { + local dir + for dir in "${PROJECTS_DIRS[@]}"; do + project_directory_rows "$dir" "${PROJECTS_DIRS[@]}" + done +} + +# Validate logical family names before deduplicating their physical containers. +recognized_estate_rows() { + local name child physical + while IFS=$'\t' read -r name child physical; do + estate_here "$child" || continue + printf '%s\t%s\t%s\n' "$name" "$child" "$physical" + done < <(estate_directory_rows) | awk -F '\t' '!seen[$3]++' +} + estate_by_name() { - local want="$1" dir + local want="$1" dir name child physical hits candidate is_safe_folder_name "$want" || return 1 for dir in "${PROJECTS_DIRS[@]}"; do - estate_here "$dir/$want" && return 0 + hits="$(while IFS=$'\t' read -r name child physical; do + [ "$name" = "$want" ] || continue + if estate_here "$child"; then printf '%s\t%s\n' "$child" "$physical"; fi + done < <(project_directory_rows "$dir" "${PROJECTS_DIRS[@]}") | awk -F '\t' '!seen[$2]++ {print $1}')" + [ -n "$hits" ] || continue + if [ "$(printf '%s\n' "$hits" | wc -l | tr -d ' ')" -gt 1 ]; then + die "multiple estates named '$want' under $dir; stand in the intended estate and run $SELF_NAME without : $hits" + fi + candidate="$hits" + estate_here "$candidate" && return 0 done return 1 } @@ -394,18 +468,15 @@ estate_by_name() { # a refusal to print: a person who typed the command in the wrong folder needs # the names, not an instruction to go and look for them. list_estates() { - local dir child - for dir in "${PROJECTS_DIRS[@]}"; do - [ -d "$dir" ] || continue - for child in "$dir"/*; do + local _grouped_name child _physical + while IFS=$'\t' read -r _grouped_name child _physical; do [ -d "$child" ] || continue if is_family_folder "$child"; then printf ' family %-20s %s\n' "${child##*/}" "$child" elif is_project_root "$child"; then printf ' project %-20s %s\n' "${child##*/}" "$child" fi - done - done + done < <(recognized_estate_rows) } estates_or_none() { @@ -438,12 +509,12 @@ estates_or_none() { # shellcheck disable=SC2329 # `park` never asks: it files the record, and # never looks a holder up by it. The block is byte-identical by design. family_folders_with_id() { - local want="$1" dir child prior dup + local want="$1" dir child prior dup _grouped_name _physical local -a found=() [ -n "$want" ] || return 0 for dir in "${PROJECTS_DIRS[@]}"; do [ -d "$dir" ] || continue - for child in "$dir"/*; do + while IFS=$'\t' read -r _grouped_name child _physical; do [ -d "$child" ] || continue is_family_folder "$child" || continue yaml_scalar "$child/${child##*/}/family.yaml" id || continue @@ -455,7 +526,7 @@ family_folders_with_id() { [ "$dup" -eq 0 ] || continue found+=("$child") printf '%s\n' "$child" - done + done < <(project_directory_rows "$dir" "${PROJECTS_DIRS[@]}") done } @@ -506,28 +577,42 @@ repo_slug_of() { printf '%s/%s\n' "$owner" "$name" } -# The clone under the projects directory whose `origin` IS that repository - -# ruling 1. Two levels deep, because the estates this standard places are -# `/` and `//`. +# Match origins only in discovered development checkouts, including a +# family's direct sibling checkouts. Never descend into pinned Git members. clone_with_origin() { - local want dir candidate origin found + local want candidate origin found hits child name physical git_dir common_dir git_physical rank want="$(lower "$1")" - for dir in "${PROJECTS_DIRS[@]}"; do - [ -d "$dir" ] || continue - for candidate in "$dir"/*/ "$dir"/*/*/; do - candidate="${candidate%/}" + hits="$(while IFS=$'\t' read -r name child physical; do + for candidate in "$child" "$child"/*; do + if [ "$candidate" != "$child" ]; then + is_family_folder "$child" || continue + fi + case "$candidate" in *$'\t'*|*$'\n'*) continue ;; esac [ -e "$candidate/.git" ] || continue + git_dir="$(git -C "$candidate" rev-parse --git-dir 2>/dev/null)" || continue + common_dir="$(git -C "$candidate" rev-parse --git-common-dir 2>/dev/null)" || continue + [ "$git_dir" = "$common_dir" ] || continue + [ -z "$(git -C "$candidate" rev-parse --show-superproject-working-tree 2>/dev/null)" ] || continue origin="$(git -C "$candidate" remote get-url origin 2>/dev/null || true)" [ -n "$origin" ] || continue found="$(repo_slug_of "$origin" || true)" [ -n "$found" ] || continue if [ "$(lower "$found")" = "$want" ]; then - printf '%s\n' "$candidate" - return 0 + git_physical="$(cd -- "$candidate" && pwd -P)" || continue + rank=1 + estate_walk_up "$candidate" && rank=0 + printf '%s\t%s\t%s\n' "$git_physical" "$rank" "$candidate" fi done - done - return 1 + done < <(estate_directory_rows) | + LC_ALL=C sort -s -t $'\t' -k1,1 -k2,2n | + awk -F '\t' '!seen[$1]++ {print $3}')" + [ -n "$hits" ] || return 1 + if [ "$(printf '%s\n' "$hits" | wc -l | tr -d ' ')" -gt 1 ]; then + printf 'multiple development checkouts have origin %s; stand in the intended checkout: %s\n' "$1" "$hits" >&2 + return 2 + fi + printf '%s\n' "$hits" } # The family's members, by the same ` - project: ` row `family.py` @@ -1126,7 +1211,9 @@ if [ -n "$REPO_SLUG" ]; then url spelling of one (https://…//.git, git@host:/). A family's members may live in another organisation, so there is no owner to assume, and a path on disk has none to read." - MATCH="$(clone_with_origin "$REPO_SLUG" || true)" + LOOKUP_RC=0 + MATCH="$(clone_with_origin "$REPO_SLUG")" || LOOKUP_RC=$? + [ "$LOOKUP_RC" -ne 2 ] || die "ambiguous repository lookup; no checkout was selected" [ -n "$MATCH" ] || die "no clone under your projects directory has origin $REPO_SLUG. \`--repo\` names a clone you ALREADY HAVE (RULING 2026-09-09 ruling 1). To bring an estate back that is not here yet, name it as your workspace diff --git a/status b/status index 4f8a7d9d..cf90df5c 100755 --- a/status +++ b/status @@ -518,11 +518,85 @@ estate_walk_up() { return 1 } +# BEGIN GROUPED PROJECT DISCOVERY +# Self-contained installed commands share this block; a regression test checks +# byte equality. Rows carry name, logical path and physical deduplication key. +# Keep logical paths for estate recognition; never enter a repo, estate or +# supporting checkout collection while looking for grouping folders. +project_directory_walk() { + local base="$1" depth="$2" mode="$3" families="${4-}" child name physical boundary family_container + [ -d "$base" ] || return 0 + for child in "$base"/*; do + [ -d "$child" ] || continue + name="${child##*/}" + # Per-lane register clones live beside any workspace as -lanes. + case "$name" in workTrees|worktrees|laneWorkspaces|*-lanes|*-lane-registers) continue ;; esac + case "$child" in *$'\t'*|*$'\n'*) continue ;; esac + physical="$(cd -- "$child" 2>/dev/null && pwd -P)" || continue + case "${physical##*/}" in workTrees|worktrees|laneWorkspaces|*-lanes|*-lane-registers) continue ;; esac + case "$physical" in *$'\t'*|*$'\n'*) continue ;; esac + boundary=0; family_container=0 + if [ -e "$child/$name/family.yaml" ] || [ -e "$physical/${physical##*/}/family.yaml" ]; then + family_container=1 + fi + case $'\n'"$families"$'\n' in *$'\n'"$physical"$'\n'*) boundary=1 ;; esac + if [ -e "$child/.git" ] || [ -e "$child/project.yaml" ] || + [ -e "$child/family.yaml" ] || [ "$family_container" -eq 1 ] || + { [ -f "$child/HEAD" ] && [ -d "$child/objects" ]; }; then + boundary=1 + fi + if [ "$mode" = families ]; then + [ "$family_container" -eq 0 ] || printf '%s\n' "$physical" + elif [ "$depth" -eq 0 ] || [ "$boundary" -eq 1 ]; then + printf '%s\t%s\t%s\n' "$name" "$child" "$physical" + fi + if [ "$boundary" -eq 0 ] && [ "$depth" -lt 2 ]; then + project_directory_walk "$child" "$((depth + 1))" "$mode" "$families" + fi + done +} + +project_directory_rows() { + local base="$1" families root + shift + [ "$#" -gt 0 ] || set -- "$base" + # Resolve boundaries across every configured root before emitting rows. + # A family alias in another root must also hide this container's members. + families="$(for root in "$@"; do project_directory_walk "$root" 0 families; done)" + project_directory_walk "$base" 0 rows "$families" +} +# END GROUPED PROJECT DISCOVERY + +estate_directory_rows() { + local dir + for dir in "${PROJECTS_DIRS[@]}"; do + project_directory_rows "$dir" "${PROJECTS_DIRS[@]}" + done +} + +# Validate logical family names before deduplicating their physical containers. +recognized_estate_rows() { + local name child physical + while IFS=$'\t' read -r name child physical; do + estate_here "$child" || continue + printf '%s\t%s\t%s\n' "$name" "$child" "$physical" + done < <(estate_directory_rows) | awk -F '\t' '!seen[$3]++' +} + estate_by_name() { - local want="$1" dir + local want="$1" dir name child physical hits candidate is_safe_folder_name "$want" || return 1 for dir in "${PROJECTS_DIRS[@]}"; do - estate_here "$dir/$want" && return 0 + hits="$(while IFS=$'\t' read -r name child physical; do + [ "$name" = "$want" ] || continue + if estate_here "$child"; then printf '%s\t%s\n' "$child" "$physical"; fi + done < <(project_directory_rows "$dir" "${PROJECTS_DIRS[@]}") | awk -F '\t' '!seen[$2]++ {print $1}')" + [ -n "$hits" ] || continue + if [ "$(printf '%s\n' "$hits" | wc -l | tr -d ' ')" -gt 1 ]; then + die "multiple estates named '$want' under $dir; stand in the intended estate and run $SELF_NAME without : $hits" + fi + candidate="$hits" + estate_here "$candidate" && return 0 done return 1 } @@ -531,18 +605,15 @@ estate_by_name() { # a refusal to print: a person who typed the command in the wrong folder needs # the names, not an instruction to go and look for them. list_estates() { - local dir child - for dir in "${PROJECTS_DIRS[@]}"; do - [ -d "$dir" ] || continue - for child in "$dir"/*; do + local _grouped_name child _physical + while IFS=$'\t' read -r _grouped_name child _physical; do [ -d "$child" ] || continue if is_family_folder "$child"; then printf ' family %-20s %s\n' "${child##*/}" "$child" elif is_project_root "$child"; then printf ' project %-20s %s\n' "${child##*/}" "$child" fi - done - done + done < <(recognized_estate_rows) } estates_or_none() { @@ -575,12 +646,12 @@ estates_or_none() { # shellcheck disable=SC2329 # `park` never asks: it files the record, and # never looks a holder up by it. The block is byte-identical by design. family_folders_with_id() { - local want="$1" dir child prior dup + local want="$1" dir child prior dup _grouped_name _physical local -a found=() [ -n "$want" ] || return 0 for dir in "${PROJECTS_DIRS[@]}"; do [ -d "$dir" ] || continue - for child in "$dir"/*; do + while IFS=$'\t' read -r _grouped_name child _physical; do [ -d "$child" ] || continue is_family_folder "$child" || continue yaml_scalar "$child/${child##*/}/family.yaml" id || continue @@ -592,7 +663,7 @@ family_folders_with_id() { [ "$dup" -eq 0 ] || continue found+=("$child") printf '%s\n' "$child" - done + done < <(project_directory_rows "$dir" "${PROJECTS_DIRS[@]}") done } @@ -643,28 +714,42 @@ repo_slug_of() { printf '%s/%s\n' "$owner" "$name" } -# The clone under the projects directory whose `origin` IS that repository - -# ruling 1. Two levels deep, because the estates this standard places are -# `/` and `//`. +# Match origins only in discovered development checkouts, including a +# family's direct sibling checkouts. Never descend into pinned Git members. clone_with_origin() { - local want dir candidate origin found + local want candidate origin found hits child name physical git_dir common_dir git_physical rank want="$(lower "$1")" - for dir in "${PROJECTS_DIRS[@]}"; do - [ -d "$dir" ] || continue - for candidate in "$dir"/*/ "$dir"/*/*/; do - candidate="${candidate%/}" + hits="$(while IFS=$'\t' read -r name child physical; do + for candidate in "$child" "$child"/*; do + if [ "$candidate" != "$child" ]; then + is_family_folder "$child" || continue + fi + case "$candidate" in *$'\t'*|*$'\n'*) continue ;; esac [ -e "$candidate/.git" ] || continue + git_dir="$(git -C "$candidate" rev-parse --git-dir 2>/dev/null)" || continue + common_dir="$(git -C "$candidate" rev-parse --git-common-dir 2>/dev/null)" || continue + [ "$git_dir" = "$common_dir" ] || continue + [ -z "$(git -C "$candidate" rev-parse --show-superproject-working-tree 2>/dev/null)" ] || continue origin="$(git -C "$candidate" remote get-url origin 2>/dev/null || true)" [ -n "$origin" ] || continue found="$(repo_slug_of "$origin" || true)" [ -n "$found" ] || continue if [ "$(lower "$found")" = "$want" ]; then - printf '%s\n' "$candidate" - return 0 + git_physical="$(cd -- "$candidate" && pwd -P)" || continue + rank=1 + estate_walk_up "$candidate" && rank=0 + printf '%s\t%s\t%s\n' "$git_physical" "$rank" "$candidate" fi done - done - return 1 + done < <(estate_directory_rows) | + LC_ALL=C sort -s -t $'\t' -k1,1 -k2,2n | + awk -F '\t' '!seen[$1]++ {print $3}')" + [ -n "$hits" ] || return 1 + if [ "$(printf '%s\n' "$hits" | wc -l | tr -d ' ')" -gt 1 ]; then + printf 'multiple development checkouts have origin %s; stand in the intended checkout: %s\n' "$1" "$hits" >&2 + return 2 + fi + printf '%s\n' "$hits" } # The family's members, by the same ` - project: ` row `family.py` @@ -5823,16 +5908,13 @@ read_one_estate() { # reason: `~/projects` and `~/Projects` can each hold estates when both # exist, and name order reads them together. all_estate_dirs() { - local dir child - for dir in "${PROJECTS_DIRS[@]}"; do - [ -d "$dir" ] || continue - for child in "$dir"/*; do + local _grouped_name child _physical + while IFS=$'\t' read -r _grouped_name child _physical; do [ -d "$child" ] || continue if is_family_folder "$child" || is_project_root "$child"; then printf '%s\t%s\n' "${child##*/}" "$child" fi - done - done | LC_ALL=C sort -t $'\t' -k1,1 + done < <(recognized_estate_rows) | LC_ALL=C sort -t $'\t' -k1,1 } # Read every estate in `$1`, in name order, and end with a summary. An estate @@ -6013,7 +6095,9 @@ elif [ -n "$REPO_SLUG" ]; then url spelling of one (https://…//.git, git@host:/). A family's members may live in another organisation, so there is no owner to assume, and a path on disk has none to read." - MATCH="$(clone_with_origin "$REPO_SLUG" || true)" + LOOKUP_RC=0 + MATCH="$(clone_with_origin "$REPO_SLUG")" || LOOKUP_RC=$? + [ "$LOOKUP_RC" -ne 2 ] || die "ambiguous repository lookup; no checkout was selected" [ -n "$MATCH" ] || die "no clone under your projects directory has origin $REPO_SLUG. \`--repo\` names a clone you ALREADY HAVE; nothing here clones one. Bring the estate back with \`resume \`, or read one of these: diff --git a/tests/test_grouped_project_lookup.py b/tests/test_grouped_project_lookup.py new file mode 100644 index 00000000..062c8976 --- /dev/null +++ b/tests/test_grouped_project_lookup.py @@ -0,0 +1,386 @@ +"""Grouped discovery through real commands; all writes use isolated registers.""" +from pathlib import Path +import subprocess +import shutil +import pytest +from conftest import NEEDS_UPSTREAM, REPO, WINDOWS_SKIP +from test_park_resume_commands import (FAMILY, WORKSPACE_SLUG, git, home, estate, remotes, offline, + probe_project, probe_family, sweepable_project, run) +from test_lane_start_claude_current import Sandbox + +pytestmark = [WINDOWS_SKIP, pytest.mark.skipif( + any(shutil.which(command) is None for command in ("bash", "git", "make")), + reason="grouped CLI tests require bash, git and make")] + + +def block(name): + text = (REPO / name).read_text() + return text.split("# BEGIN GROUPED PROJECT DISCOVERY", 1)[1].split("# END GROUPED PROJECT DISCOVERY", 1)[0] + + +def test_discovery_copies_match(): + assert len({block(name) for name in ("park", "resume", "status", "lane-start")}) == 1 + + +def rows(root): + result = subprocess.run(["bash", "-c", block("park") + '\nproject_directory_rows "$1"', "lookup", str(root)], text=True, capture_output=True, check=True) + return [[parts[0], parts[-1]] for parts in (line.split("\t") for line in result.stdout.splitlines())] + + +def test_bounds_support_collections_and_repository_boundaries(tmp_path): + for group in ("workTrees", "laneWorkspaces", "brett-wip-lanes", "wip-lanes", "personal-register-lanes", "brett-wip-lane-registers", "worktrees"): + probe_project(tmp_path / group, "Hidden") + target = probe_project(tmp_path / "business" / "Company space", "Visible") + probe_project(tmp_path / "business" / "Company space" / "too-deep", "Deep") + outer = probe_project(tmp_path / "factories", "Outer") + probe_project(outer / "members", "Pinned") + family = probe_family(tmp_path / "healthcare", "Family") + probe_project(family.parent / "members", "Sibling") + found = rows(tmp_path) + names = [r[0] for r in found] + assert ["Visible", str(target)] in found + assert not {"Hidden", "Deep", "Pinned", "Sibling"}.intersection(names) + assert "Family" in names + + +@pytest.mark.parametrize("family", [False, True]) +def test_named_grouped_estate(tmp_path, family): + home = tmp_path / "home"; home.mkdir() + group = home / "projects" / "group space" + root = (probe_family if family else probe_project)(group, "Atlas") + result = run(REPO / "park", "Atlas", home=home) + assert result.returncode == 0, result.stderr + result.stdout + assert f"PROBE park in {root}" in result.stdout + + +@pytest.mark.parametrize("command", ["park", "resume", "status"]) +@NEEDS_UPSTREAM +def test_distinct_matching_estates_refuse(tmp_path, command, remotes): + home = tmp_path / "home"; home.mkdir() + name = FAMILY if command == "resume" else "Atlas" + for group in ("one", "two"): + probe_project(home / "projects" / group, name) + args = [name] + if command == "resume": + args += ["--workspace", WORKSPACE_SLUG] + result = run(REPO / command, *args, home=home, env=offline(remotes)) + assert result.returncode != 0 + assert "multiple estates named" in result.stderr + assert "PROBE" not in result.stdout + + +def grouped_box(tmp_path): + box = Sandbox(tmp_path) + target = box.home / "projects" / "developerTools" / "repoZ" + target.parent.mkdir(); box.lane_dir.rename(target); box.lane_dir = target + box.resolver() + return box + + +@pytest.mark.parametrize("alias", [None, "repoZ", "repoz"]) +def test_new_lane_uses_grouped_canonical_target(tmp_path, alias): + box = grouped_box(tmp_path) + if alias: + (box.home / "projects" / alias).symlink_to(box.lane_dir, target_is_directory=True) + result = box.start() + assert result.returncode == 0, result.stderr + assert str(box.lane_dir) in box.lane_log() + assert box.claude_runs() + + +def test_ambiguous_new_lane_refuses_without_register_writes(tmp_path): + box = grouped_box(tmp_path) + other = box.home / "projects" / "other" / "repoZ"; other.mkdir(parents=True) + box.git("-C", str(other), "init", "-q") + before = box.commits() + result = box.start() + assert result.returncode != 0 + assert box.commits() == before + assert not box.lane_log() + assert not box.claude_runs() + + +def test_explicit_lane_path_wins_over_grouped_ambiguity(tmp_path): + box = grouped_box(tmp_path) + other = box.home / "projects" / "other" / "repoZ"; other.mkdir(parents=True) + box.git("-C", str(other), "init", "-q") + result = box.start("--dir", str(box.lane_dir)) + assert result.returncode == 0, result.stderr + assert str(box.lane_dir) in box.lane_log() + + +def test_grouped_sweep_runs_physical_estate_once(tmp_path): + home = tmp_path / "home"; home.mkdir() + projects = home / "projects" + root = sweepable_project(projects / "developerTools", "Atlas") + (projects / "Atlas").symlink_to(root, target_is_directory=True) + result = run(REPO / "park", "--all", home=home) + assert result.returncode == 0, result.stderr + result.stdout + assert result.stdout.count(f"PROBE park in {root}") == 1 + assert "1 estate(s) parked" in result.stdout + + +def test_configured_projects_directory_supports_groups(tmp_path): + home = tmp_path / "home"; home.mkdir() + elsewhere = tmp_path / "elsewhere" + root = probe_project(elsewhere / "group", "Atlas") + result = run(REPO / "park", "Atlas", home=home, env={"PROJECTS_DIR": str(elsewhere)}) + assert result.returncode == 0, result.stderr + result.stdout + assert f"PROBE park in {root}" in result.stdout + + +def test_missing_recorded_directory_does_not_fall_back_to_group(tmp_path): + box = grouped_box(tmp_path) + result = box.start() + assert result.returncode == 0, result.stderr + before = box.commits() + moved = box.lane_dir.with_name("moved") + box.lane_dir.rename(moved) + original = box.lane_dir + box.lane_dir = moved + replacement = box.home / "projects" / "elsewhere" / "repoZ" + replacement.mkdir(parents=True) + box.git("-C", str(replacement), "init", "-q") + result = box.start() + assert result.returncode != 0 + assert str(original) in result.stderr + assert box.commits() == before + + +def test_grouped_origin_lookup_and_duplicate_origin_refusal(tmp_path): + home = tmp_path / "home"; home.mkdir() + projects = home / "projects" + first = probe_project(projects / "business" / "Company", "Atlas") + def origin(root): + subprocess.run(["git", "init", "-q", str(root)], check=True) + subprocess.run(["git", "-C", str(root), "remote", "add", "origin", "https://github.com/testorg/Atlas.git"], check=True) + origin(first) + result = run(REPO / "park", "--repo", "testorg/Atlas", home=home) + assert result.returncode == 0, result.stderr + result.stdout + assert f"PROBE park in {first}" in result.stdout + second = probe_project(projects / "other", "AtlasCopy"); origin(second) + for command in ("park", "resume", "status"): + result = run(REPO / command, "--repo", "testorg/Atlas", home=home) + assert result.returncode != 0 + assert "multiple development checkouts" in result.stderr + assert "PROBE" not in result.stdout + + +@pytest.mark.parametrize("kind", ["linked", "bare"]) +def test_supporting_git_checkout_is_not_a_default_candidate(tmp_path, kind): + box = grouped_box(tmp_path) + other = box.home / "projects" / "other" / "repoZ" + other.parent.mkdir() + if kind == "linked": + box.git("-C", str(box.lane_dir), "commit", "-q", "--allow-empty", "-m", "fixture base") + box.git("-C", str(box.lane_dir), "worktree", "add", "--detach", str(other), "HEAD") + else: + box.git("clone", "--bare", str(box.lane_dir), str(other)) + result = box.start() + assert result.returncode == 0, result.stderr + assert str(box.lane_dir) in box.lane_log() + + +@NEEDS_UPSTREAM +def test_resume_grouped_existing_family(home, remotes, estate): + target = home / "projects" / "group" / FAMILY + target.parent.mkdir() + estate["folder"].rename(target) + result = run(REPO / "resume", FAMILY, "--workspace", WORKSPACE_SLUG, + home=home, env=offline(remotes)) + assert result.returncode == 0, result.stderr + result.stdout + assert str(target) in result.stdout + assert "Alpha.sh ran with:" in result.stdout + assert not estate["folder"].exists() + + +@NEEDS_UPSTREAM +def test_resume_grouped_existing_standalone(home, remotes): + target = home / "projects" / "group" / "Alpha" + target.parent.mkdir() + git("clone", "-q", str(remotes["members"]["Alpha"]), str(target), cwd=home) + result = run(REPO / "resume", "Alpha", "--workspace", "tester/solo", + home=home, env=offline(remotes)) + assert result.returncode == 0, result.stderr + result.stdout + assert str(target) in result.stdout + assert "Alpha.sh ran with:" in result.stdout + assert not (home / "projects" / "Alpha").exists() + + +def logical_family_aliases(home, storage, name): + """A logical holder name unrelated to its physical container basename.""" + projects = home / "projects"; projects.mkdir(parents=True, exist_ok=True) + physical = storage / "estate-data" + holder = probe_family(storage, name) + holder.parent.rename(physical) + alias = projects / name + alias.symlink_to(physical, target_is_directory=True) + (projects / "000-invalid").symlink_to(physical, target_is_directory=True) + group = projects / "group"; group.mkdir() + (group / name).symlink_to(physical, target_is_directory=True) + return alias, physical / name + + +@pytest.mark.parametrize("sweep", [False, True]) +def test_logical_family_alias_park_dispatch_and_dedup(tmp_path, sweep): + home = tmp_path / "home" + alias, holder = logical_family_aliases(home, tmp_path / "storage", "Atlas") + result = run(REPO / "park", "--all" if sweep else "Atlas", home=home) + assert result.returncode == 0, result.stderr + result.stdout + assert result.stdout.count(f"PROBE park in {holder}") == 1 + assert alias.is_symlink() + + +@pytest.mark.parametrize("sweep", [False, True]) +def test_logical_family_alias_status_recognition(tmp_path, sweep): + home = tmp_path / "home" + alias, holder = logical_family_aliases(home, tmp_path / "storage", "Atlas") + result = run(REPO / "status", "--all" if sweep else "Atlas", home=home) + assert result.returncode in (0, 1), result.stderr + result.stdout + assert "Atlas" in result.stdout + assert "no estate" not in result.stderr + assert alias.is_symlink() + + +def aliased_real_family(home, remotes, estate, tmp_path): + physical = tmp_path / "storage" / "estate-data"; physical.parent.mkdir() + estate["folder"].rename(physical) + estate["folder"].symlink_to(physical, target_is_directory=True) + (home / "projects" / "000-invalid").symlink_to(physical, target_is_directory=True) + config = home / ".agents" / "workspace.yaml"; config.parent.mkdir() + config.write_text(f"repository: {WORKSPACE_SLUG}\npath: ~/projects/wip\n") + git("clone", "-q", str(remotes["workspace"]), str(home / "projects" / "wip"), cwd=home) + return estate["folder"], physical + + +@pytest.mark.parametrize("by_id", [False, True]) +@NEEDS_UPSTREAM +def test_logical_family_alias_resume_name_and_family_id(home, remotes, estate, tmp_path, by_id): + alias, physical = aliased_real_family(home, remotes, estate, tmp_path) + result = run(REPO / "resume", FAMILY.lower() if by_id else FAMILY, + "--dry-run", home=home, env=offline(remotes)) + assert result.returncode == 0, result.stderr + result.stdout + assert f"present {alias} (family)" in result.stdout + assert "would clone" not in result.stdout + assert alias.is_symlink() + + +@pytest.mark.parametrize("command", ["park", "status"]) +@NEEDS_UPSTREAM +def test_logical_family_alias_origin_lookup(home, remotes, estate, tmp_path, command): + alias, physical = aliased_real_family(home, remotes, estate, tmp_path) + slug = "testorg/" + FAMILY + git("-C", str(alias / FAMILY), "remote", "set-url", "origin", "https://github.com/" + slug + ".git", cwd=home) + args = ["--repo", slug] + if command == "park": args.append("--dry-run") + result = run(REPO / command, *args, home=home, env=offline(remotes)) + assert result.returncode in (0, 1), result.stderr + result.stdout + assert "nothing at or above" not in result.stderr + assert alias.is_symlink() + + +@pytest.mark.parametrize("matching_home", [True, False]) +def test_recorded_home_without_directory_keeps_origin_proof(tmp_path, matching_home): + box = grouped_box(tmp_path) + result = box.start() + assert result.returncode == 0, result.stderr + log = box.wip / "lanes" / "log" / "repoZ-1.md" + # Model an older record: home is known, but the directory field predates it. + import re + text = re.sub(r"; dir [^;\n]+", "", log.read_text()) + assert str(box.lane_dir) not in text + log.write_text(text) + box.git("-C", str(box.wip), "add", "--", str(log)) + box.git("-C", str(box.wip), "commit", "-q", "-m", "seed old home-only record") + box.git("-C", str(box.wip), "push", "-q", "origin", "main") + other = box.home / "projects" / "imposter" / "repoZ" + other.mkdir(parents=True) + box.git("init", "-q", str(other)) + box.git("-C", str(other), "remote", "add", "origin", "https://github.com/stranger/repoZ.git") + if not matching_home: + box.git("-C", str(box.lane_dir), "remote", "set-url", "origin", "https://github.com/stranger/also-wrong.git") + moved = box.lane_dir.with_name("moved-away") + box.lane_dir.rename(moved) + box.lane_dir = moved + before = box.commits() + result = box.start("--dry-run") + assert box.commits() == before + assert f"cd {other}" not in result.stderr + if matching_home: + assert result.returncode == 0, result.stderr + assert f"cd {box.lane_dir}" in result.stderr + assert "Evidence 7" in result.stderr + else: + assert result.returncode == 2, result.stderr + assert "nothing was started" in result.stderr + + +@pytest.mark.parametrize("workspace", ["wip", "brett-wip", "personal-register"]) +@pytest.mark.parametrize("command", ["park", "resume", "status"]) +def test_workspace_lane_collections_do_not_ambiguate_origin_lookup(tmp_path, workspace, command): + home = tmp_path / "home"; home.mkdir() + projects = home / "projects" + root = probe_project(projects, workspace) + lane = probe_project(projects / (workspace + "-lanes"), "repoA-1") + (projects / "register-checkouts").symlink_to(lane.parent, target_is_directory=True) + slug = "tester/" + workspace + for checkout in (root, lane): + subprocess.run(["git", "init", "-q", str(checkout)], check=True) + subprocess.run(["git", "-C", str(checkout), "remote", "add", "origin", "https://github.com/" + slug + ".git"], check=True) + result = run(REPO / command, "--repo", slug, home=home) + assert "multiple development checkouts" not in result.stderr + assert str(lane) not in result.stdout + result.stderr + if command == "park": + assert result.returncode == 0, result.stderr + result.stdout + assert f"PROBE park in {root}" in result.stdout + + +def test_workspace_lane_collections_are_not_swept(tmp_path): + home = tmp_path / "home"; home.mkdir() + projects = home / "projects" + root = sweepable_project(projects, "wip") + lane = sweepable_project(projects / "wip-lanes", "repoA-1") + (projects / "register-checkouts").symlink_to(lane.parent, target_is_directory=True) + result = run(REPO / "park", "--all", home=home) + assert result.returncode == 0, result.stderr + result.stdout + assert f"PROBE park in {root}" in result.stdout + assert str(lane) not in result.stdout + result.stderr + assert "1 estate(s) parked" in result.stdout + + +@pytest.mark.parametrize("physical_name", ["Atlas", "estate-data"]) +def test_family_container_alias_does_not_expose_members(tmp_path, physical_name): + home = tmp_path / "home"; home.mkdir() + alias, holder = logical_family_aliases(home, tmp_path / "storage", "Atlas") + physical = holder.parent + if physical_name != physical.name: + renamed = physical.with_name(physical_name) + physical.rename(renamed) + for link in (home / "projects" / "Atlas", home / "projects" / "000-invalid", home / "projects" / "group" / "Atlas"): + link.unlink() + link.symlink_to(renamed, target_is_directory=True) + physical = renamed + probe_project(physical, "Alpha") + found = rows(home / "projects") + assert "Atlas" in [row[0] for row in found] + assert "Alpha" not in [row[0] for row in found] + + +@pytest.mark.parametrize("command", ["park", "status"]) +def test_family_container_boundaries_span_both_projects_roots(tmp_path, command): + home = tmp_path / "home"; home.mkdir() + alias, holder = logical_family_aliases(home, tmp_path / "storage", "Atlas") + other = home / "Projects"; other.mkdir() + (home / "projects" / "000-invalid").rename(other / "000-invalid") + sweepable_project(holder.parent, "Alpha") + result = run(REPO / command, "--all", home=home) + assert "Alpha" not in result.stdout + result.stderr + if command == "park": + assert result.returncode == 0, result.stderr + result.stdout + assert result.stdout.count("PROBE park in ") == 1 + else: + # The deliberately uncommitted probe holder has findings; enumeration + # must still contain only the family, never its sibling as an estate. + assert result.returncode == 1, result.stderr + result.stdout + assert result.stdout.count("=== status Atlas ===") == 1 + assert "1 with findings" in result.stdout