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
22 changes: 22 additions & 0 deletions clerk/legacy/clerk.bash
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,19 @@ claim_ensure_worktree() { # $1=root $2=short
# CLAIM CONFLICT (exit 5): unit occupied, or the push-CAS race was lost. Its own prescriptive
# text (never backend_fail's "run clerk doctor" — the backend is not broken, someone else just
# got there first).
# Provision a worktree's host-local Beads state before exposing it to the caller. The
# tracked config identifies the project; metadata.json is deliberately host-local.
claim_provision_and_verify_beads() { # $1=root $2=worktree $3=full-id
local wt="$2" full="$3" source="$1/.beads/metadata.json"
if [ -f "$wt/.beads/config.yaml" ] && [ ! -f "$wt/.beads/metadata.json" ]; then
[ -r "$source" ] || return 1
cp "$source" "$wt/.beads/metadata.json" || return 1
[ -f "$wt/.beads/metadata.json" ] || return 1
fi
(cd "$wt" && bd show "$full" --readonly --json | jq -e --arg id "$full" \
'type == "array" and .[0].id == $id' >/dev/null 2>&1)
}

claim_conflict() { # $1=short $2=holder $3=reason-phrase, e.g. "is already claimed"/"was just claimed"
local short="$1" holder="${2:-someone else}" reason="$3"
printf 'clerk: backlog claim refused — delivery/%s %s by %s\n' "$short" "$reason" "$holder" >&2
Expand Down Expand Up @@ -1913,6 +1926,9 @@ cmd_backlog_claim() {
if ! wt=$(claim_ensure_worktree "$root" "$short"); then
backend_fail "claim failed — could not provision the worktree at $root/.worktrees/$short for delivery/$short"
fi
if [ "$backend" = bd ] && ! claim_provision_and_verify_beads "$root" "$wt" "$full"; then
backend_fail "claim failed — could not provision or verify the worktree at $wt"
fi
printf '%sclerk:%s %s already claimed by you — worktree ready\n' "$C_GRN" "$C_RST" "$full"
printf '%s\n' "$wt"
return 0
Expand Down Expand Up @@ -1968,6 +1984,9 @@ cmd_backlog_claim() {
if ! wt=$(claim_ensure_worktree "$root" "$short"); then
backend_fail "claim failed — could not provision the worktree at $root/.worktrees/$short for delivery/$short"
fi
if [ "$backend" = bd ] && ! claim_provision_and_verify_beads "$root" "$wt" "$full"; then
backend_fail "claim failed — could not provision or verify the worktree at $wt"
fi
if [ "$from_returned" -eq 1 ]; then
claim_replay_returned "$root" "$wt" "$short" "$base_sha" || return $?
elif [ "$fresh" -eq 1 ] && [ "$returned_disposition" = discard ]; then
Expand Down Expand Up @@ -2017,6 +2036,9 @@ cmd_backlog_claim() {
if ! wt=$(claim_ensure_worktree "$root" "$short"); then
backend_fail "claim failed — could not provision the worktree at $root/.worktrees/$short for delivery/$short"
fi
if [ "$backend" = bd ] && ! claim_provision_and_verify_beads "$root" "$wt" "$full"; then
backend_fail "claim failed — could not provision or verify the worktree at $wt"
fi
if [ "$from_returned" -eq 1 ]; then
claim_replay_returned "$root" "$wt" "$short" "$base_sha" || return $?
elif [ "$fresh" -eq 1 ] && [ "$returned_disposition" = discard ]; then
Expand Down
55 changes: 55 additions & 0 deletions tests/clerk/claim.bats
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ mk_ac_unit() { # $1 = title
printf '%s\n' "$id"
}

track_beads_config() { # $1 = repo
local repo="$1"
git -C "$repo" rm --cached -q --ignore-unmatch .beads/metadata.json
git -C "$repo" commit -q -m 'track beads config'
git -C "$repo" push -q origin main
}

# Points `origin` at a path that will never resolve, so `git fetch origin` fails immediately
# (no network hang) — the OFFLINE signal.
break_origin() { # $1 = repo
Expand Down Expand Up @@ -93,6 +100,54 @@ advance_main() { # $1=repo $2=file $3=content $4=subject
[ "$(bd show "$id" --readonly --json | jq -r '.[0].status')" = in_progress ]
}

@test "claim (bd): bootstraps worktree-local Beads metadata before reporting ready" {
repo=$(make_claim_repo claim_bootstrap)
track_beads_config "$repo"
cd "$repo"
id=$(mk_ac_unit "bootstrapped unit")
short="${id#*-}"

run "$CLERK" backlog claim "$id"
[ "$status" -eq 0 ]
wt="${lines[-1]}"
[ "$wt" = "$repo/.worktrees/$short" ]
[ -f "$wt/.beads/metadata.json" ]
cmp "$repo/.beads/metadata.json" "$wt/.beads/metadata.json"
run git -C "$wt" ls-files --error-unmatch .beads/metadata.json
[ "$status" -ne 0 ]
run bash -c 'cd "$1" && "$2" backlog show "$3"' -- "$wt" "$CLERK" "$id"
[ "$status" -eq 0 ]
}

@test "claim (bd): a failed worktree Beads bootstrap fails without reporting ready" {
repo=$(make_claim_repo claim_bootstrap_failure)
track_beads_config "$repo"
cd "$repo"
id=$(mk_ac_unit "bootstrap failure unit")
short="${id#*-}"
fake="$BATS_TEST_TMPDIR/fake-bin"
mkdir -p "$fake"
cat >"$fake/bd" <<'EOF'
#!/bin/sh
if [ "$1" = bootstrap ]; then
/bin/cp "$BEADS_TEST_METADATA" .beads/metadata.json
exit 0
fi
exec /usr/local/bin/bd "$@"
EOF
cat >"$fake/cp" <<'EOF'
#!/bin/sh
exit 1
EOF
chmod +x "$fake/bd" "$fake/cp"

run env PATH="$fake:$PATH" BEADS_TEST_METADATA="$repo/.beads/metadata.json" "$CLERK" backlog claim "$id"
[ "$status" -eq 5 ]
[[ "$output" == *"could not provision or verify the worktree"* ]]
[[ "$output" != *"worktree ready"* ]]
[ ! -f "$repo/.worktrees/$short/.beads/metadata.json" ]
}

@test "claim (bd): OCCUPIED — exit 5, names the holder, no orphan worktree, no bd mutation on our side" {
repo=$(make_claim_repo claim_b)
cd "$repo"
Expand Down
Loading