#!/usr/bin/env bash# CLAIM WHAT A LANE MAKES, AT THE MOMENT IT IS MADE (the lane ledger, storage# note c7d20613, 10 Oct 2026).## scripts/lane-claim.sh <path> --kind <kind> [--rule <rule>] [--lane <lane>]# [--session <id>] [--pid <pid>]## Writes one claim into this Mac's spool, `~/.cottondb/claims/<id>.json`# (`$COTTON_CLAIMS_DIR` overrides it, for tests). The Mac app's ledger pass# (`cotton_client::ci::ledger`) reads the spool every few minutes, measures the# claim, lists it on the owner's Disk page, and removes the path when its rule# lets it go — and only then. A path nobody claims is found by that pass too,# listed as `unclaimed`, and left for the owner.## KINDS: worktree, cargo_target, derived, simulator, household, scratch,# evidence, server, database.# RULES: session_ends (when the session that made it ends), idle_hours:N,# budget_lru (build output: least recently used first when the Mac is# under its reserve), keep_last:N (one of a series of runs in one# folder — walk evidence, release-walk runs, cuts: the newest N are# kept, older ones go at once), owner_only (only the owner's Drop).## DEFAULTS. The lane is `$COTTON_LANE`, else the branch checked out where this# runs. The session is `$COTTON_LANE_SESSION`, else `$COTTON_SESSION_ROW_ID`# (set for every session Cotton starts), else none. The rule follows the kind:# build output is budget_lru, a database copy owner_only, a worktree# session_ends, anything else session_ends with a session and idle_hours:72# without one.## A WORKTREE IS session_ends WITH OR WITHOUT A SESSION NAMED. The ledger never# removes a folder something lives in — a registered git worktree, a live# process or a tmux pane working in it (`ledger::Liveness`) — so a worktree's# session is read off the Mac, not off this claim: it is kept while alive,# and once nothing lives in it it goes when the session it names has ended,# or by the budget under the reserve when it names none. It is never# idle_hours: a lane that sat idle for three days is not a lane that ended.## The same path claimed again is the same claim (its id is the path's hash, as# `claim_id` computes it), so calling this twice is harmless. It never fails# its caller: a claim that cannot be written is said on stderr and the caller# goes on — the ledger's own scan lists the path as unclaimed instead.set -upath="${1-}"shift || truekind="" rule="" lane="${COTTON_LANE-}" session="${COTTON_LANE_SESSION:-${COTTON_SESSION_ROW_ID-}}" pid=""while [ $# -gt 0 ]; docase"$1"in --kind) kind="${2-}"; shift2 ;; --rule) rule="${2-}"; shift2 ;; --lane) lane="${2-}"; shift2 ;; --session) session="${2-}"; shift2 ;; --pid) pid="${2-}"; shift2 ;; *) echo "lane-claim: unknown argument $1" >&2; exit0 ;;esacdonesay() { echo "lane-claim: $*" >&2; }if [ -z "$path" ] || [ -z "$kind" ]; then say "usage: lane-claim.sh <path> --kind <kind> [--rule <rule>] [--lane <lane>] [--session <id>] [--pid <pid>]"exit0ficase"$kind"in worktree|cargo_target|derived|simulator|household|scratch|evidence|server|database) ;; *) say "unknown kind '$kind' — not claimed"; exit0 ;;esac# The physical path, as the ledger's own scan spells it (`/tmp` is# `/private/tmp` on a Mac). A path not there yet is not claimed: the ledger# would forget it at its next pass.if [ ! -e "$path" ]; then say "$path is not there — not claimed"exit0fipath="$(cd "$path" 2>/dev/null && pwd -P || printf '%s' "$path")"# Claims are also the entry point for lanes made by hand. Generate before an# existing-claim early return, including when this claim names the lane folder.if [ "$kind" = worktree ]; then tree="$path" [ -f "$path/worktree/.git" ] && tree="$path/worktree"if [ -f "$tree/.git" ]; then bash "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/configure-worktree-cargo.sh""$tree" >&2 || { say "could not configure Cargo for $tree"exit1 }fifiif [ -z "$lane" ]; then lane="$(git -C "$path" rev-parse --abbrev-ref HEAD 2>/dev/null || git rev-parse --abbrev-ref HEAD 2>/dev/null || echo unknown)"fiif [ -z "$rule" ]; thencase"$kind"in cargo_target|derived) rule="budget_lru" ;; database) rule="owner_only" ;; worktree) rule="session_ends" ;; *) if [ -n "$session" ]; then rule="session_ends"; else rule="idle_hours:72"; fi ;;esac ruled=""else ruled=1ficase"$rule"in session_ends|budget_lru|owner_only) ;; idle_hours:[1-9]*|keep_last:[1-9]*) ;; *) say "unknown rule '$rule' — not claimed"; exit0 ;;esacif [ "$rule" = session_ends ] && [ -z "$session" ] && [ "$kind" != worktree ]; then say "session_ends needs a session (--session, or COTTON_LANE_SESSION) — claimed idle_hours:72 instead" rule="idle_hours:72"fi# JSON string: backslashes and quotes escaped, control characters dropped.json() { printf '"%s"'"$(printf '%s' "$1" | tr -d '\000-\037' | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g')"; }dir="${COTTON_CLAIMS_DIR:-$HOME/.cottondb/claims}"id="claim-$(printf '%s' "$path" | shasum -a 256 | cut -c1-24)"mkdir -p "$dir"2>/dev/null || { say "cannot make $dir — not claimed"; exit0; }# Claimed already by the same lane and session (a build that ensures its# target every run): the claim stands as the ledger last measured it — unless# a rule was asked for by name and the claim has another (a stopgap# owner_only put back to session_ends).existing="$dir/$id.json"if [ -z "$pid" ] && [ -f "$existing" ] \ && grep -qF "\"lane\": $(json "$lane")""$existing" \ && { [ -z "$ruled" ] || grep -qF "\"rule\": $(json "$rule")""$existing"; } \ && { [ -z "$session" ] || grep -qF "\"session\": $(json "$session")""$existing"; }; thenexit0fi{ printf '{\n' printf ' "path": %s,\n'"$(json "$path")" printf ' "kind": %s,\n'"$(json "$kind")" printf ' "lane": %s,\n'"$(json "$lane")" [ -n "$session" ] && printf ' "session": %s,\n'"$(json "$session")" [ -n "$pid" ] && printf ' "pid": %d,\n'"$pid" printf ' "rule": %s,\n'"$(json "$rule")" printf ' "created_at": %s\n'"$(json "$(date -u +%Y-%m-%dT%H:%M:%SZ)")" printf '}\n'} > "$dir/.$id.json.tmp" && mv "$dir/.$id.json.tmp""$dir/$id.json" || { say "cannot write the claim — not claimed"; exit0; }echo "claimed $path ($kind, $rule, lane $lane)"