gunk-dev/the-valley

valley-knot.sh

sourcee87fa7234439main's published tip history
28.9 KiB
1# shellcheck shell=bash
2# valley knot, end to end and unmocked: the real valley against local bare
3# repositories that run no hook, as any git remote would. Every verb, and
4# then what a write does when it does not land the first time: two writers
5# racing for the ref, a push whose outcome the command cannot tell, a claim
6# another run took first, and a field another write changed.
7# Paths and fixtures come from the derivation environment.
8# shellcheck disable=SC2154
9set -euo pipefail
10
11w="$TMPDIR/valley-knot"
12mkdir -p "$w"
13export HOME="$w"
14export XDG_CACHE_HOME="$w/cache"
15export GIT_CONFIG_NOSYSTEM=1
16git config --global user.name valley-knot-check
17git config --global user.email valley-knot-check@localhost
18git config --global init.defaultBranch main
19
20# valley is bin/valley itself, run by bash. The package's wrapper puts its
21# own git ahead of anything on PATH, and the races below need a stand-in
22# for git to run first.
23mkdir -p "$w/bin"
24printf '#!%s\nexec %q %q "$@"\n' "$BASH" "$BASH" "$cliScript" > "$w/bin/valley"
25chmod +x "$w/bin/valley"
26export PATH="$w/bin:$PATH"
27
28fail() {
29 printf 'valley-knot: %s\n' "$*" >&2
30 for f in "$w"/out "$w"/err; do
31 if [ -s "$f" ]; then cat "$f" >&2; fi
32 done
33 exit 1
34}
35
36# ok <command…>: the command succeeds, and its output is kept.
37ok() {
38 "$@" > "$w/out" 2> "$w/err" || fail "$* failed"
39}
40
41# fails <status> <ERE> <command…>: the command exits with that status,
42# and says something matching the pattern.
43fails() {
44 local want="$1" says="$2" status=0
45 shift 2
46 "$@" > "$w/out" 2> "$w/err" || status=$?
47 [ "$status" = "$want" ] || fail "$* exited $status, not $want"
48 grep -qE -- "$says" "$w/err" || fail "$* did not say $says"
49}
50
51# holds <jq condition…>: the last JSON printed holds each.
52holds() {
53 local cond
54 for cond in "$@"; do
55 jq -e "$cond" "$w/out" > /dev/null || fail "$cond does not hold"
56 done
57}
58
59line() {
60 grep -qE -- "$1" "$w/out" || fail "no line matches $1"
61}
62
63srv="$w/srv"
64git init --quiet --bare "$srv/the-valley.git"
65git init --quiet --bare "$srv/app.git"
66graph="$srv/the-valley.git"
67git init --quiet "$w/work"
68cd "$w/work"
69git remote add origin "$srv/the-valley.git"
70
71# A write names the principal; without one it is refused, and nothing is
72# pushed.
73fails 1 'valley.principal' valley knot new outcome "Slice one lands"
74git config valley.principal operator
75if git -C "$graph" rev-parse --verify --quiet refs/the-valley/knots > /dev/null; then
76 fail "a refused write created the ref"
77fi
78
79# ----------------------------------------------------------------------
80# The model, for an agent that never reads CUE.
81ok valley knot schema --json
82holds '.types | map(.name) == ["outcome","bug","idea","question"]' \
83 '.types[0].active == ["open","in-progress"]' '.types[0].terminal == ["done","abandoned"]' \
84 '[.fields[].name] | index("claimed_by") != null' '.limits.knots == 5000'
85ok valley knot schema
86line '^ idea'
87fails 1 'the types are outcome, bug, idea, question' valley knot new task "Nothing"
88fails 1 '"decision" is no type of knot; the types are outcome, bug, idea, question' valley knot new decision "Nothing"
89fails 2 'knot has no verb' valley knot frob
90
91# ----------------------------------------------------------------------
92# new: the first write creates the ref, as one parentless commit by the
93# principal, carrying its operation's trailer.
94ok valley knot new outcome "Slice one lands" --json
95milestone="$(jq -r .knot.id "$w/out")"
96holds '.schema == "valley-knot-op/v1"' '.knot.status == "open"' '.knot.created_by == "operator"' \
97 '.knot.blocked_by == []' '.op | test("^[0-9a-f]{16}$")'
98first="$(git -C "$graph" rev-parse refs/the-valley/knots)"
99[ "$(git -C "$graph" rev-list --parents -n 1 "$first")" = "$first" ] || fail "the first commit has a parent"
100[ "$(git -C "$graph" log -1 --format=%cn "$first")" = operator ] || fail "the committer is not the principal"
101[ "$(git -C "$graph" log -1 --format='%(trailers:key=Valley-Op,valueonly)' "$first")" = "$(jq -r .op "$w/out")" ] \
102 || fail "the commit carries no Valley-Op trailer naming its operation"
103[ "$milestone" = "oc-$(printf '%s' slice-one-lands | sha256sum | cut -c1-7)" ] \
104 || fail "the id $milestone is not derived from the slug"
105
106ok valley knot new bug "The hook reads too much" --parent "$milestone"
107bug="bd-$(printf '%s' the-hook-reads-too-much | sha256sum | cut -c1-7)"
108printf 'The hook case.\n\nWith a second paragraph.\n' > "$w/body"
109ok valley knot new outcome "Write the hook case" --parent "$milestone" \
110 --blocked-by "$bug" --blocked-by app:oc-0123456 --owner alice --body - < "$w/body"
111work="oc-$(printf '%s' write-the-hook-case | sha256sum | cut -c1-7)"
112# Every later commit has the previous tip as its one parent.
113second="$(git -C "$graph" rev-parse refs/the-valley/knots)"
114[ "$(git -C "$graph" rev-list --count "$second")" = 3 ] || fail "three writes are not three commits"
115[ "$(git -C "$graph" rev-list --merges --count "$second")" = 0 ] || fail "a write made a merge"
116
117# The same title again takes a suffix, and so another id.
118ok valley knot new outcome "Write the hook case" --json
119holds ".knot.id != \"$work\"" '.knot.path | endswith("-write-the-hook-case-2.md")'
120twin="$(jq -r .knot.id "$w/out")"
121
122# Edges are checked as the hook checks them.
123fails 1 'is no knot in the graph' valley knot new outcome "Dangling" --parent oc-0000000
124fails 1 'and a parent is an outcome' valley knot new idea "Misparented" --parent "$bug"
125fails 1 'carries no blocked_by' valley knot new idea "Blocked idea" --blocked-by "$bug"
126fails 1 'control character' valley knot new idea $'A bell \a here'
127
128# ----------------------------------------------------------------------
129# show and list.
130ok valley knot show "$work" --json
131holds '.schema == "valley-knot/v1"' ".knot.parent == \"$milestone\"" '.knot.owner == "alice"' \
132 ".knot.blocked_by == [\"app:oc-0123456\", \"$bug\"]" ".knot.waiting_on == [\"app:oc-0123456\", \"$bug\"]" \
133 '.knot.dangling == ["app:oc-0123456"]' '.knot.ready == false' \
134 '.knot.body == "The hook case.\n\nWith a second paragraph.\n"'
135ok valley knot show "$milestone" --json
136holds ".knot.children | sort == ([\"$bug\", \"$work\"] | sort)" '.knot.progress == {"done": 0, "of": 2}'
137ok valley knot show "$bug"
138line '^blocks: +oc-'
139ok valley knot list --json
140holds '.schema == "valley-knots/v1"' '.knots | length == 4'
141ok valley knot list --ready --json
142holds "[.knots[].id] | sort == ([\"$milestone\", \"$bug\", \"$twin\"] | sort)"
143ok valley knot list --type bug --json
144holds ".knots | map(.id) == [\"$bug\"]"
145ok valley knot list --owner alice --json
146holds ".knots | map(.id) == [\"$work\"]"
147fails 1 '--status takes active, terminal, or one of' valley knot list --status nonsense
148fails 1 'holds no knot' valley knot show oc-0000000
149
150# ----------------------------------------------------------------------
151# set: the settable fields, and the guards.
152ok valley knot set "$bug" title="The hook reads too much at once" owner=operator
153ok valley knot show "$bug" --json
154holds '.knot.title == "The hook reads too much at once"' '.knot.owner == "operator"'
155fails 1 'set changes status, title, source, owner, target, graduated_into' valley knot set "$bug" priority=high
156fails 1 'change it with valley knot link' valley knot set "$bug" parent="$work"
157fails 1 "a bug's status is one of open, closed" valley knot set "$bug" status=done
158fails 3 "requires \"closed\"" valley knot set "$bug" status=open --if status=closed
159fails 0 'nothing to change' valley knot set "$bug" owner=operator
160ok valley knot set "$bug" owner=
161
162# link and unlink, and a cycle refused.
163ok valley knot link "$twin" --blocked-by "$bug" --change valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr
164ok valley knot show "$twin" --json
165holds ".knot.blocked_by == [\"$bug\"]" '.knot.changes == ["valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr"]'
166fails 1 'blocked_by edges form a cycle' valley knot link "$bug" --blocked-by "$twin"
167fails 1 'parent edges form a cycle' valley knot link "$milestone" --parent "$work"
168fails 3 'unlink it first' valley knot link "$work" --parent "$twin"
169fails 1 'is valley:<project>/<change ID>' valley knot link "$twin" --change https://example.com/x
170ok valley knot unlink "$twin" --blocked-by "$bug"
171ok valley knot show "$twin" --json
172holds '.knot.blocked_by == []'
173
174# edit replaces the body.
175printf 'A new body.\n' | valley knot edit "$twin" --body - > "$w/out" 2> "$w/err" || fail "edit failed"
176ok valley knot show "$twin" --json
177holds '.knot.body == "A new body.\n"'
178
179# comment: by -m and by stdin, each its own file. Comments are listed by
180# the second they were written in, and by operation within one second.
181ok valley knot comment "$work" -m "A finding."
182printf 'A second finding,\nover two lines.\n' | valley knot comment "$work" --body - > "$w/out" 2> "$w/err" \
183 || fail "a comment from stdin failed"
184ok valley knot show "$work" --json
185holds '.knot.comment_count == 2' \
186 '[.knot.comments[].body] | sort == ["A finding.\n", "A second finding,\nover two lines.\n"]' \
187 '.knot.comments | all(.author == "operator")'
188[ "$(git -C "$graph" ls-tree --name-only "refs/the-valley/knots:comments/$work" | wc -l)" = 2 ] \
189 || fail "two comments are not two files"
190fails 1 'takes the comment' valley knot comment "$work"
191
192# comment --edit replaces a comment's text in place. The comment keeps its
193# id, its file, its author and its stamp, and records who edited it and
194# when. Each comment's id is its file's name without .md.
195ok valley knot show "$work" --json
196holds '.knot.comments | all(.id | test("^[0-9]{8}T[0-9]{6}Z-[0-9a-f]{16}$"))' '.knot.comments | all(has("edited") | not)'
197finding="$(jq -r '.knot.comments[] | select(.body == "A finding.\n") | .id' "$w/out")"
198found_at="$(jq -r ".knot.comments[] | select(.id == \"$finding\") | .at" "$w/out")"
199[ "$finding.md" = "$(git -C "$graph" ls-tree --name-only "refs/the-valley/knots:comments/$work" | grep -F "${finding#*-}")" ] \
200 || fail "a comment's id is not its file's name"
201unedited="$(git -C "$graph" rev-parse refs/the-valley/knots)"
202ok env VALLEY_NOW=2026-10-25T15:00:00Z valley knot comment "$work" --edit "$finding" -m "A finding, restated." --json
203holds ".knot.comments | map(select(.id == \"$finding\")) | .[0] | .body == \"A finding, restated.\\n\" and .author == \"patrick\"
204 and .at == \"$found_at\" and .edited == \"2026-10-25T15:00:00Z\" and .edited_by == \"patrick\"" '.knot.comment_count == 2'
205[ "$(git -C "$graph" ls-tree --name-only "refs/the-valley/knots:comments/$work" | wc -l)" = 2 ] \
206 || fail "an edit added a file"
207[ "$(git -C "$graph" show -s --format=%s refs/the-valley/knots)" = "knot comment $work: edit $finding" ] \
208 || fail "an edit is not one commit naming the comment"
209restated="$(git -C "$graph" rev-parse refs/the-valley/knots)"
210# The same editor edits it again, and each edit is its own commit.
211ok env VALLEY_NOW=2026-10-25T16:00:00Z valley knot comment "$work" --edit "$finding" -m "A finding, restated twice."
212ok valley knot show "$work"
213line "^— patrick, .*, edited 2026-10-25 16:00Z by patrick \\[$finding\\]$"
214fails 0 'says that already; nothing to change' valley knot comment "$work" --edit "$finding" -m "A finding, restated twice."
215fails 1 "holds no comment 20261025T000000Z-0000000000000000" valley knot comment "$work" --edit 20261025T000000Z-0000000000000000 -m "Nothing."
216fails 1 "comment $finding is on $work, not on $bug" valley knot comment "$bug" --edit "$finding" -m "Elsewhere."
217fails 2 "--edit takes a comment's id" valley knot comment "$work" --edit "../$bug/$finding" -m "Elsewhere."
218fails 1 'takes the comment' valley knot comment "$work" --edit "$finding"
219
220# log lists every commit that touched the knot or its comments, newest
221# first, in the ref's ancestry, each with its time, committer and
222# operation as the commit records them. The milestone was the first knot,
223# so its log ends at the root commit.
224ok valley knot log "$work" --json
225holds '.schema == "valley-knot-log/v1"' ".id == \"$work\"" '.commits | all(.committer == "patrick")' \
226 '.commits[0].subject == "knot comment '"$work"': edit '"$finding"'"' \
227 ".commits[0].files == [{\"path\": \"comments/$work/$finding.md\", \"change\": \"changed\", \"comment\": \"$finding\"}]" \
228 '.commits[-1].files == [{"path": .path, "change": "added"}]'
229[ "$(jq -r '.commits[].commit' "$w/out")" = "$(git -C "$graph" rev-list refs/the-valley/knots -- "$(jq -r .path "$w/out")" "comments/$work/")" ] \
230 || fail "log does not list the knot's commits in the ref's order"
231for c in $(jq -r '.commits[].commit' "$w/out"); do
232 [ "$(jq -r ".commits[] | select(.commit == \"$c\") | [.at, .op] | join(\" \")" "$w/out")" \
233 = "$(TZ=UTC git -C "$graph" log -1 --date=format-local:%Y-%m-%dT%H:%M:%SZ --format='%cd %(trailers:key=Valley-Op,valueonly)' "$c")" ] \
234 || fail "log does not give commit $c's time and operation"
235done
236ok valley knot log "$milestone" --json
237holds ".commits[-1].commit == \"$first\""
238ok valley knot log "$work"
239line "^${restated:0:7} [0-9-]+ [0-9:]+Z patrick knot comment $work: edit $finding$"
240line "^ changed comments/$work/$finding.md$"
241
242# show --at reads the knot as a commit of the graph left it, so every
243# earlier text of a comment is read back.
244ok valley knot show "$work" --at "$unedited" --json
245holds ".at == \"$unedited\"" ".tip == \"$(git -C "$graph" rev-parse refs/the-valley/knots)\"" \
246 ".knot.comments | map(select(.id == \"$finding\")) | .[0] | .body == \"A finding.\\n\" and (has(\"edited\") | not)"
247ok valley knot show "$work" --at "${restated:0:12}" --json
248holds ".knot.comments | map(select(.id == \"$finding\")) | .[0].body == \"A finding, restated.\\n\""
249ok valley knot show "$work" --at "$unedited"
250line "^as of $unedited$"
251# Only the graph's own history is read: a commit the tip does not descend
252# from is refused, and so is one from before the knot was made.
253stray="$(git commit-tree "$(git hash-object -w -t tree /dev/null)" -m stray)"
254fails 1 "is no commit of the graph" valley knot show "$work" --at "$stray"
255fails 1 "held no knot $work at" valley knot show "$work" --at "$first"
256fails 2 '--at takes a commit of the graph' valley knot show "$work" --at main
257
258# claim and unclaim.
259ok valley knot claim "$work" --run run-1 --json
260holds '.knot.claimed_by == "run-1"' '.knot.status == "in-progress"' '.knot.claim.stale == false'
261fails 3 'is claimed by run-1' valley knot claim "$work" --run run-2
262fails 0 'claimed by run-1 already' valley knot claim "$work" --run run-1
263ok valley knot set "$twin" status=abandoned
264fails 1 'only an active knot is claimed' valley knot claim "$twin" --run run-2
265fails 3 'requires "run-2"' valley knot unclaim "$work" --if claimed_by=run-2
266# A field no knot has is refused before anything is read. Read as the
267# empty value an unset field holds, this --if would hold on every knot.
268before="$(git -C "$graph" rev-parse refs/the-valley/knots)"
269fails 1 'a knot has no field claimed_byy; --if compares type, id, status, title' valley knot unclaim "$work" --if claimed_byy=
270[ "$(git -C "$graph" rev-parse refs/the-valley/knots)" = "$before" ] || fail "an --if naming no field let a write through"
271ok valley knot unclaim "$work"
272ok valley knot show "$work" --json
273holds '.knot | has("claimed_by") | not' '.knot.status == "in-progress"'
274ok valley knot claim "$work" --run run-2
275
276# ----------------------------------------------------------------------
277# Another project on the same host, by --project and by a qualified id.
278ok valley knot new outcome "Land the module" --project app --json
279holds '.project == "app"'
280other="$(jq -r .knot.id "$w/out")"
281ok valley knot link "$work" --blocked-by "app:$other"
282ok valley knot show "$work" --json
283holds ".knot.waiting_on | index(\"app:$other\") != null" ".knot.dangling | index(\"app:$other\") == null"
284ok valley knot set "$other" status=done --project app
285ok valley knot show "app:$other" --json
286holds '.project == "app"' '.knot.status == "done"'
287ok valley knot show "$work" --json
288holds ".knot.waiting_on | index(\"app:$other\") == null"
289git -C "$srv/app.git" rev-parse --verify --quiet refs/the-valley/knots > /dev/null \
290 || fail "--project wrote no graph in app"
291
292# ----------------------------------------------------------------------
293# Ready means an outcome or a bug: an idea and an open question are not.
294ok valley knot new idea "A thought, not work" --json
295idea="$(jq -r .knot.id "$w/out")"
296ok valley knot new question "Which ref holds the graph" --owner operator --json
297asked="$(jq -r .knot.id "$w/out")"
298holds '.knot.type == "question"' '.knot.status == "open"' '.knot.owner == "operator"' \
299 '.knot.path | startswith("questions/qn-")'
300fails 1 'carries no blocked_by' valley knot new question "A blocked question" --blocked-by "$bug"
301ok valley knot list --ready --json
302holds ".knots | map(.id) | index(\"$idea\") == null" ".knots | map(.id) | index(\"$asked\") == null" \
303 ".knots | map(.id) | index(\"$bug\") != null"
304ok valley knot show "$idea" --json
305holds '.knot.ready == false'
306ok valley knot show "$asked" --json
307holds '.knot.ready == false'
308
309# Work waits on a question until it is answered or dropped. An answer
310# finishes the question, so it is no ending the view reports.
311ok valley knot new outcome "Write the answer down" --blocked-by "$asked" --json
312written="$(jq -r .knot.id "$w/out")"
313ok valley knot new bug "The answer is missing" --blocked-by "$asked" --json
314missing="$(jq -r .knot.id "$w/out")"
315for k in "$written" "$missing"; do
316 ok valley knot show "$k" --json
317 holds '.knot.ready == false' ".knot.waiting_on == [\"$asked\"]"
318done
319ok valley knot set "$asked" status=answered
320ok valley knot show "$asked" --json
321holds '.knot.ready == false'
322for k in "$written" "$missing"; do
323 ok valley knot show "$k" --json
324 holds '.knot.ready == true' '.knot.waiting_on == []' '.knot.cleared_by_ending == []'
325done
326# A question can open again, and the work waits on it again.
327ok valley knot set "$asked" status=open
328for k in "$written" "$missing"; do
329 ok valley knot show "$k" --json
330 holds '.knot.ready == false' ".knot.waiting_on == [\"$asked\"]"
331done
332# A dropped question clears its blocker without being answered, and the
333# view says so.
334ok valley knot new question "Is a second ref needed" --json
335moot="$(jq -r .knot.id "$w/out")"
336ok valley knot new outcome "Mirror the second ref" --blocked-by "$moot" --json
337mirror="$(jq -r .knot.id "$w/out")"
338ok valley knot set "$moot" status=dropped
339ok valley knot show "$mirror" --json
340holds '.knot.ready == true' '.knot.waiting_on == []' ".knot.cleared_by_ending == [\"$moot\"]"
341
342# An answered question links the change that records its answer in the
343# source, and stays never ready. An idea links no change: it graduates
344# into an outcome or a question, and that links the change.
345ok valley knot set "$asked" status=answered
346ok valley knot link "$asked" --change https://github.com/example/the-valley/pull/7
347ok valley knot show "$asked" --json
348holds '.knot.changes == ["https://github.com/example/the-valley/pull/7"]' '.knot.ready == false'
349ok valley knot show "$asked"
350line '^change: +https://github.com/example/the-valley/pull/7$'
351ok valley knot list --type question --json
352holds ".knots | map(select(.id == \"$asked\")) | .[0].changes == [\"https://github.com/example/the-valley/pull/7\"]"
353before="$(git -C "$graph" rev-parse refs/the-valley/knots)"
354fails 1 'an idea does not carry changes' valley knot link "$idea" --change https://github.com/example/the-valley/pull/7
355[ "$(git -C "$graph" rev-parse refs/the-valley/knots)" = "$before" ] || fail "a change linked to an idea was pushed"
356ok valley knot schema --json
357holds '.fields[] | select(.name == "changes") | .on == ["outcome","bug","question"]'
358
359# A graduated idea names where it went: a question to settle, or an
360# outcome taken on as work, and never a bug.
361ok valley knot set "$idea" status=graduated graduated_into="$asked"
362ok valley knot show "$idea" --json
363holds '.knot.status == "graduated"' ".knot.graduated_into == \"$asked\""
364ok valley knot new idea "A thought taken on as work" --json
365taken="$(jq -r .knot.id "$w/out")"
366fails 1 "an outcome's or a question's id" valley knot set "$taken" status=graduated graduated_into="$bug"
367ok valley knot set "$taken" status=graduated graduated_into="$milestone"
368ok valley knot show "$taken" --json
369holds '.knot.status == "graduated"' ".knot.graduated_into == \"$milestone\""
370
371# Finishing a knot releases its claim, in the same commit.
372ok valley knot new outcome "Finish the claimed work" --json
373finished="$(jq -r .knot.id "$w/out")"
374ok valley knot claim "$finished" --run run-3
375ok valley knot set "$finished" status=done --json
376holds '.knot.status == "done"' '.knot | has("claimed_by") | not' '.knot | has("claimed_at") | not'
377ok valley knot show "$finished" --json
378holds '.knot | has("claim") | not' '.knot.ready == false'
379[ "$(git -C "$graph" show -s --format=%s refs/the-valley/knots)" = "knot set $finished: status=done" ] \
380 || fail "finishing was not one commit"
381
382# A reference to a project that cannot be read is shown as dangling, and
383# git's own errors stay off stderr.
384ok valley knot new outcome "Wait on nowhere" --blocked-by nowhere:oc-0123456 --json
385quiet="$(jq -r .knot.id "$w/out")"
386ok valley knot show "$quiet" --json
387holds '.knot.unresolved == ["nowhere:oc-0123456"]' '.knot.ready == false'
388[ ! -s "$w/err" ] || fail "show printed noise for an unreadable project: $(cat "$w/err")"
389ok valley knot show "$quiet"
390line 'dangling: its project could not be read'
391[ ! -s "$w/err" ] || fail "show printed noise for an unreadable project: $(cat "$w/err")"
392ok valley knot list
393[ ! -s "$w/err" ] || fail "list printed noise for an unreadable project: $(cat "$w/err")"
394
395# ----------------------------------------------------------------------
396# What happens when a push does not land the first time. A stand-in for
397# git runs first on PATH, and on its first push it does one thing given in
398# RACE before or after passing the push on, then fails or not as told.
399real_path="$PATH"
400mkdir -p "$w/shim"
401{
402 printf '#!%s\n' "$BASH"
403 cat << 'EOF'
404if [ "$1" = push ] && [ ! -e "$RACE_MARKER" ]; then
405 touch "$RACE_MARKER"
406 if [ "$RACE_WHEN" = before ]; then PATH="$REAL_PATH" "$BASH" -c "$RACE"; fi
407 if [ "$RACE_PUSH" = yes ]; then PATH="$REAL_PATH" git "$@" || exit; fi
408 if [ "$RACE_WHEN" = after ]; then PATH="$REAL_PATH" "$BASH" -c "$RACE"; fi
409 if [ "$RACE_FAIL" = yes ]; then
410 echo "fatal: the remote end hung up unexpectedly" >&2
411 exit 128
412 fi
413 [ "$RACE_PUSH" = yes ] && exit 0
414fi
415PATH="$REAL_PATH" exec git "$@"
416EOF
417} > "$w/shim/git"
418chmod +x "$w/shim/git"
419race=0
420# raced <when> <push> <fail> <other writer> -- <command…>
421raced() {
422 local when="$1" pushes="$2" fails="$3" other="$4"
423 shift 5
424 race=$((race + 1))
425 env PATH="$w/shim:$real_path" REAL_PATH="$real_path" RACE_MARKER="$w/race.$race" \
426 RACE_WHEN="$when" RACE_PUSH="$pushes" RACE_FAIL="$fails" RACE="$other" "$@"
427}
428comments_saying() {
429 local n=0 f
430 for f in $(git -C "$graph" ls-tree -r --name-only refs/the-valley/knots -- comments/); do
431 if git -C "$graph" cat-file blob "refs/the-valley/knots:$f" | grep -qxF -- "$1"; then n=$((n + 1)); fi
432 done
433 echo "$n"
434}
435
436# Two writers race: the other lands between this one's read and its push,
437# so the lease fails, and this one is formed again on the new tip. Both
438# land, each once.
439raced before no no "valley knot comment $bug -m 'The other writer.'" -- \
440 valley knot comment "$bug" -m "This writer." > "$w/out" 2> "$w/err" || fail "a writer that lost a race did not retry"
441[ "$(comments_saying 'The other writer.')" = 1 ] || fail "the other writer's comment did not land once"
442[ "$(comments_saying 'This writer.')" = 1 ] || fail "the writer that lost the race did not land once"
443
444# Two writers at once, three writes each, all land.
445pids=()
446for who in a b; do
447 (for n in 1 2 3; do valley knot comment "$milestone" -m "Writer $who, write $n." || exit 1; done) \
448 > "$w/parallel.$who" 2>&1 &
449 pids+=("$!")
450done
451wait "${pids[0]}" || fail "the first parallel writer failed: $(cat "$w/parallel.a")"
452wait "${pids[1]}" || fail "the second parallel writer failed: $(cat "$w/parallel.b")"
453for who in a b; do
454 for n in 1 2 3; do
455 [ "$(comments_saying "Writer $who, write $n.")" = 1 ] || fail "writer $who's write $n did not land once"
456 done
457done
458
459# The push lands, and the connection drops before the command hears so,
460# while another writer moves the ref on. The command finds its operation
461# on the ref and stops: nothing is written twice.
462raced after yes yes "valley knot comment $bug -m 'Landed meanwhile.'" -- \
463 valley knot comment "$bug" -m "Landed, unheard." > "$w/out" 2> "$w/err" \
464 || fail "a write that landed unheard was reported failed"
465[ "$(comments_saying 'Landed, unheard.')" = 1 ] || fail "a write that landed unheard was written again"
466[ "$(comments_saying 'Landed meanwhile.')" = 1 ] || fail "the other writer's comment is not there once"
467# The push does not land, and the command cannot tell: it looks, finds
468# nothing, and tries again, once.
469raced none no yes "" -- valley knot comment "$bug" -m "Lost, then sent." > "$w/out" 2> "$w/err" \
470 || fail "a write whose push was lost was not tried again"
471[ "$(comments_saying 'Lost, then sent.')" = 1 ] || fail "a lost write did not land once"
472
473# Two edits of one comment race: the other lands between this one's read
474# and its push. This one was written against the text before it, so on
475# the new tip it is a conflict, and the other edit stands.
476ok valley knot show "$bug" --json
477raced_comment="$(jq -r '.knot.comments[] | select(.body == "This writer.\n") | .id' "$w/out")"
478status=0
479raced before no no "valley knot comment $bug --edit $raced_comment -m 'Their edit.'" -- \
480 valley knot comment "$bug" --edit "$raced_comment" -m "My edit." > "$w/out" 2> "$w/err" || status=$?
481[ "$status" = 3 ] || fail "an edit overwrote an edit it never read (status $status)"
482grep -q "conflict: comment $raced_comment on $bug changed while this was written" "$w/err" \
483 || fail "the conflict does not name the comment"
484[ "$(comments_saying 'Their edit.')" = 1 ] || fail "the other edit does not stand"
485[ "$(comments_saying 'My edit.')" = 0 ] || fail "the edit that lost the race landed"
486# The edit lands, and the connection drops before the command hears so,
487# while another writer moves the ref on. On the new tip the comment holds
488# this edit's own text, which a retry would take for another edit's; the
489# command finds its operation on the ref first, and stops.
490raced after yes yes "valley knot comment $bug -m 'Moved on meanwhile.'" -- \
491 valley knot comment "$bug" --edit "$raced_comment" -m "Edited, unheard." > "$w/out" 2> "$w/err" \
492 || fail "an edit that landed unheard was reported failed"
493[ "$(comments_saying 'Edited, unheard.')" = 1 ] || fail "an edit that landed unheard is not there"
494[ "$(git -C "$graph" log --format=%s refs/the-valley/knots | grep -cxF "knot comment $bug: edit $raced_comment")" = 2 ] \
495 || fail "an edit that landed unheard was written again"
496
497# A claim another run took first is a conflict, never an overwrite.
498ok valley knot unclaim "$work"
499raced before no no "valley knot claim $work --run run-other" -- \
500 valley knot claim "$work" --run run-mine > "$w/out" 2> "$w/err" && fail "a claim overwrote another run's"
501grep -q 'conflict: .*is claimed by run-other' "$w/err" || fail "a lost claim was not reported as a conflict"
502ok valley knot show "$work" --json
503holds '.knot.claimed_by == "run-other"'
504
505# An unclaim releases only the claim it read. Here that claim is released,
506# and the knot claimed by another run, before this unclaim's push: on the
507# new tip it finds a claim it never read, and that is a conflict.
508status=0
509raced before no no "valley knot unclaim $work && valley knot claim $work --run run-replacement" -- \
510 valley knot unclaim "$work" > "$w/out" 2> "$w/err" || status=$?
511[ "$status" = 3 ] || fail "an unclaim released a claim it never read (status $status)"
512grep -q "conflict: $work's claim changed from run-other since .* to run-replacement since " "$w/err" \
513 || fail "the conflict does not say how the claim changed"
514ok valley knot show "$work" --json
515holds '.knot.claimed_by == "run-replacement"'
516
517# A field another write changed meanwhile is a conflict too, unless
518# --force says to set it anyway.
519status=0
520raced before no no "valley knot set $bug title='Their title'" -- \
521 valley knot set "$bug" title="My title" > "$w/out" 2> "$w/err" || status=$?
522[ "$status" = 3 ] || fail "a title changed meanwhile was overwritten (status $status)"
523grep -q 'changed from' "$w/err" || fail "the conflict does not say what changed"
524raced before no no "valley knot set $bug title='Their second title'" -- \
525 valley knot set "$bug" title="My title" --force > "$w/out" 2> "$w/err" || fail "--force did not set the title"
526ok valley knot show "$bug" --json
527holds '.knot.title == "My title"'
528
529# ----------------------------------------------------------------------
530# None of it touched the checkout.
531[ -z "$(git status --porcelain)" ] || fail "a write touched the worktree"
532if git rev-parse --verify --quiet HEAD > /dev/null; then fail "a write made a commit on the checkout"; fi
533# Every commit on the ref is one operation by the principal, with one
534# parent, and no operation appears twice.
535git -C "$graph" log --format=%cn refs/the-valley/knots | sort -u | grep -qvx operator && fail "a commit names another committer"
536[ "$(git -C "$graph" log --format=%B refs/the-valley/knots | grep -c '^Valley-Op:')" \
537 = "$(git -C "$graph" rev-list --count refs/the-valley/knots)" ] || fail "a commit carries no operation"
538dupes="$(git -C "$graph" log --format=%B refs/the-valley/knots | grep '^Valley-Op:' | sort | uniq -d)"
539[ -z "$dupes" ] || fail "an operation was recorded twice: $dupes"
540
541touch "$out"