From 14fe907a2b6dfd4b87fc76509b2b26b1a689ec2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kova=C4=BE?= Date: Tue, 28 Jul 2026 22:48:21 +0200 Subject: [PATCH] fix(clerk): keep captured children in inbox --- clerk/legacy/clerk.bash | 13 +++- clerk/src/clerk/commands.py | 11 +++- clerk/src/clerk/work_graph.py | 3 + tests/clerk/inbox-graph.bats | 73 +++++++++++++++++++---- tests/clerk_python/test_text_mutations.py | 21 +++++++ 5 files changed, 105 insertions(+), 16 deletions(-) diff --git a/clerk/legacy/clerk.bash b/clerk/legacy/clerk.bash index 853e674..0a02f1c 100755 --- a/clerk/legacy/clerk.bash +++ b/clerk/legacy/clerk.bash @@ -643,10 +643,19 @@ cmd_capture() { fi # mutation self-verify (ADR 0015): confirm the id landed before declaring success local seen - seen=$(bd show "$id" --readonly --json 2>/dev/null | jq -r '.[0].id // empty') - if [ "$seen" != "$id" ]; then + seen=$(bd show "$id" --readonly --json 2>/dev/null | jq -c '.[0] // empty') + if [ "$(jq -r '.id // empty' <<<"$seen")" != "$id" ]; then backend_fail "capture failed — $id was not confirmed after creation" fi + if [ "$(jq -r '(.labels // []) | index("stage:ready") != null' <<<"$seen")" = true ]; then + if ! bd update "$id" --remove-label stage:ready >/dev/null; then + backend_fail "capture failed — $id could not be restored to the Inbox" + fi + seen=$(bd show "$id" --readonly --json 2>/dev/null | jq -c '.[0] // empty') + if [ "$(jq -r '.id // empty' <<<"$seen")" != "$id" ] || [ "$(jq -r '(.labels // []) | index("stage:ready") != null' <<<"$seen")" = true ]; then + backend_fail "capture failed — $id remained ready after creation" + fi + fi if [ "$issue_type" = impediment ]; then printf '%sclerk:%s filed %s (type: impediment)\n' "$C_GRN" "$C_RST" "$id" else diff --git a/clerk/src/clerk/commands.py b/clerk/src/clerk/commands.py index fafcb55..0494c61 100644 --- a/clerk/src/clerk/commands.py +++ b/clerk/src/clerk/commands.py @@ -586,7 +586,8 @@ def cmd_capture(backend: str, _root: Path, argv: Sequence[str], runner: CommandR if parent and parent_id(blocker_json) != parent: usage(f"clerk capture: --blocked-by {blocker} must be a sibling under {parent}") - result = BdWorkGraphAdapter(runner).create( + graph = BdWorkGraphAdapter(runner) + result = graph.create( title, use_stdin=use_stdin, issue_type=issue_type, @@ -600,6 +601,14 @@ def cmd_capture(backend: str, _root: Path, argv: Sequence[str], runner: CommandR seen = _bd_issue_json_or_backend(runner, id_, f"capture failed — {id_} was not confirmed after creation") if seen.get("id") != id_: backend_fail(f"capture failed — {id_} was not confirmed after creation") + if _is_ready_promoted(seen): + result = graph.remove_ready_label(id_) + _emit_stderr(result) + if result.returncode != 0: + backend_fail(f"capture failed — {id_} could not be restored to the Inbox") + seen = _bd_issue_json_or_backend(runner, id_, f"capture failed — {id_} was not confirmed after restoring it to the Inbox") + if seen.get("id") != id_ or _is_ready_promoted(seen): + backend_fail(f"capture failed — {id_} remained ready after creation") suffix = " (type: impediment)" if issue_type == "impediment" else "" _success(f"filed {id_}{suffix}", env) return 0 diff --git a/clerk/src/clerk/work_graph.py b/clerk/src/clerk/work_graph.py index fc9081e..a5070c5 100644 --- a/clerk/src/clerk/work_graph.py +++ b/clerk/src/clerk/work_graph.py @@ -289,6 +289,9 @@ def create(self, title: str, *, use_stdin: bool = False, issue_type: str = "", p args.extend(["--deps", blocker]) return self._runner.run(args) + def remove_ready_label(self, id_: str) -> CommandResult: + return self._runner.run(["bd", "update", id_, "--remove-label", "stage:ready"]) + def set_parent(self, child: str, parent: str) -> CommandResult: return self._runner.run(["bd", "update", child, "--parent", parent]) diff --git a/tests/clerk/inbox-graph.bats b/tests/clerk/inbox-graph.bats index 03b59cf..41c3de0 100644 --- a/tests/clerk/inbox-graph.bats +++ b/tests/clerk/inbox-graph.bats @@ -33,6 +33,7 @@ make_bd_repo() { [ "$(jq -r '.[0].issue_type' <<<"$json")" = decision ] [ "$(jq -r '.[0].parent' <<<"$json")" = "$parent" ] jq -e --arg blocker "$blocker" '.[0].dependencies | any(.dependency_type == "blocks" and .id == $blocker)' <<<"$json" >/dev/null + [ "$(jq -r '.[0].labels | index("stage:ready")' <<<"$json")" = null ] run "$CLERK" capture "bad" --impediment --type task [ "$status" -eq 2 ] @@ -53,20 +54,66 @@ make_bd_repo() { [ "$(bd show "$custom" --readonly --json | jq -r '.[0].issue_type')" = research ] } -@test "capture can add children under a ready parent and depend on a ready sibling" { - repo=$(make_bd_repo capture_ready_parent) - cd "$repo" - parent=$(bd create "ready parent" --acceptance "parent ac" --silent) - ready_sibling=$(bd create "ready sibling" --parent "$parent" --acceptance "sibling ac" --silent) - "$CLERK" inbox ready "$parent" >/dev/null - "$CLERK" inbox ready "$ready_sibling" >/dev/null +@test "capture keeps a child of ready Work in the Inbox in both command paths" { + for force_legacy in 0 1; do + repo=$(make_bd_repo "capture_ready_parent_$force_legacy") + cd "$repo" + parent=$(bd create "ready parent" --acceptance "parent ac" --silent) + "$CLERK" inbox ready "$parent" >/dev/null + + if [ "$force_legacy" -eq 1 ]; then + run env CLERK_FORCE_LEGACY=1 "$CLERK" capture "new child" --parent "$parent" + else + run "$CLERK" capture "new child" --parent "$parent" + fi + [ "$status" -eq 0 ] + child="${output#clerk: filed }" + json=$(bd show "$child" --readonly --json) + [ "$(jq -r '.[0].parent' <<<"$json")" = "$parent" ] + [ "$(jq -r '.[0].labels | index("stage:ready")' <<<"$json")" = null ] + + if [ "$force_legacy" -eq 1 ]; then + run env CLERK_FORCE_LEGACY=1 "$CLERK" backlog next + else + run "$CLERK" backlog next + fi + [ "$status" -eq 0 ] + [[ "$output" != *"$child"* ]] + + printf 'child design\n' >"$repo/design" + printf '%s\n' '- child acceptance' >"$repo/acceptance" + if [ "$force_legacy" -eq 1 ]; then + run env CLERK_FORCE_LEGACY=1 "$CLERK" inbox ready "$child" --design-file "$repo/design" --acceptance-file "$repo/acceptance" + else + run "$CLERK" inbox ready "$child" --design-file "$repo/design" --acceptance-file "$repo/acceptance" + fi + [ "$status" -eq 0 ] + json=$(bd show "$child" --readonly --json) + [ "$(jq -r '.[0].labels | index("stage:ready")' <<<"$json")" != null ] + done +} - run "$CLERK" capture "new blocked child" --parent "$parent" --blocked-by "$ready_sibling" - [ "$status" -eq 0 ] - child="${output#clerk: filed }" - json=$(bd show "$child" --readonly --json) - [ "$(jq -r '.[0].parent' <<<"$json")" = "$parent" ] - jq -e --arg blocker "$ready_sibling" '.[0].dependencies | any(.dependency_type == "blocks" and .id == $blocker)' <<<"$json" >/dev/null +@test "capture preserves ready-parent sibling blockers in both command paths" { + for force_legacy in 0 1; do + repo=$(make_bd_repo "capture_ready_parent_blocked_$force_legacy") + cd "$repo" + parent=$(bd create "ready parent" --acceptance "parent ac" --silent) + ready_sibling=$(bd create "ready sibling" --parent "$parent" --acceptance "sibling ac" --silent) + "$CLERK" inbox ready "$parent" >/dev/null + "$CLERK" inbox ready "$ready_sibling" >/dev/null + + if [ "$force_legacy" -eq 1 ]; then + run env CLERK_FORCE_LEGACY=1 "$CLERK" capture "new blocked child" --parent "$parent" --blocked-by "$ready_sibling" + else + run "$CLERK" capture "new blocked child" --parent "$parent" --blocked-by "$ready_sibling" + fi + [ "$status" -eq 0 ] + child="${output#clerk: filed }" + json=$(bd show "$child" --readonly --json) + [ "$(jq -r '.[0].parent' <<<"$json")" = "$parent" ] + jq -e --arg blocker "$ready_sibling" '.[0].dependencies | any(.dependency_type == "blocks" and .id == $blocker)' <<<"$json" >/dev/null + [ "$(jq -r '.[0].labels | index("stage:ready")' <<<"$json")" = null ] + done } @test "children/frontier/blockers/blocked return normalized JSON and frontier only open unblocked unclaimed direct children" { diff --git a/tests/clerk_python/test_text_mutations.py b/tests/clerk_python/test_text_mutations.py index 49f1f98..ed0b7a7 100644 --- a/tests/clerk_python/test_text_mutations.py +++ b/tests/clerk_python/test_text_mutations.py @@ -71,6 +71,27 @@ def test_capture_files_bd_capture_with_parent_blockers_type_and_stdin_then_self_ ["bd", "show", "dotfiles-new", "--readonly", "--json"], ]) + def test_capture_removes_a_ready_label_inherited_from_its_parent_before_success(self): + parent = [{"id": "p", "status": "open", "labels": ["stage:ready"]}] + inherited_ready = [{"id": "dotfiles-new", "status": "open", "parent": "p", "labels": ["stage:ready"]}] + inbox_child = [{"id": "dotfiles-new", "status": "open", "parent": "p", "labels": []}] + code, out, err, calls = self.invoke( + ("capture",), + ["title", "--parent", "p"], + [ + lambda args: ok(args, json.dumps(parent)), + lambda args: ok(args, "dotfiles-new\n"), + lambda args: ok(args, json.dumps(inherited_ready)), + lambda args: ok(args), + lambda args: ok(args, json.dumps(inbox_child)), + ], + ) + self.assertEqual((code, out, err), (0, "clerk: filed dotfiles-new\n", "")) + self.assertEqual(calls[-2:], [ + ["bd", "update", "dotfiles-new", "--remove-label", "stage:ready"], + ["bd", "show", "dotfiles-new", "--readonly", "--json"], + ]) + def test_capture_failed_self_verification_is_backend_failure_without_success_output(self): code, out, err, calls = self.invoke( ("capture",),