claude-dev-env 1.84.0 → 1.85.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/_shared/pr-loop/scripts/pr_loop_shared_constants/CLAUDE.md +1 -1
- package/_shared/pr-loop/scripts/pr_loop_shared_constants/terminology_sweep_constants.py +8 -0
- package/_shared/pr-loop/scripts/terminology_sweep.py +85 -39
- package/_shared/pr-loop/scripts/tests/test_terminology_sweep.py +94 -4
- package/hooks/blocking/claude_md_orphan_file_blocker.py +1 -1
- package/hooks/blocking/code_rules_docstrings.py +17 -13
- package/hooks/blocking/code_rules_imports_logging.py +10 -6
- package/hooks/blocking/code_rules_naming_collection.py +5 -3
- package/hooks/blocking/code_rules_paired_test.py +3 -2
- package/hooks/blocking/code_rules_string_magic.py +1 -1
- package/hooks/blocking/code_rules_unused_imports.py +2 -2
- package/hooks/blocking/test_code_rules_enforcer_docstring_args_span_scope.py +29 -0
- package/hooks/blocking/test_code_rules_enforcer_naive_datetime.py +24 -0
- package/hooks/hooks_constants/blocking_check_limits.py +1 -0
- package/package.json +1 -1
- package/rules/docstring-prose-matches-implementation.md +1 -1
- package/rules/env-var-table-code-drift.md +1 -1
- package/rules/package-inventory-stale-entry.md +1 -1
- package/rules/paired-test-coverage.md +1 -1
- package/skills/_shared/pr-loop/CLAUDE.md +1 -0
- package/skills/_shared/pr-loop/scripts/CLAUDE.md +1 -0
- package/skills/_shared/pr-loop/scripts/skills_pr_loop_constants/CLAUDE.md +1 -0
- package/skills/_shared/pr-loop/scripts/skills_pr_loop_constants/handoff_constants.py +45 -0
- package/skills/_shared/pr-loop/scripts/test_write_handoff.py +201 -0
- package/skills/_shared/pr-loop/scripts/write_handoff.py +309 -0
- package/skills/autoconverge/SKILL.md +46 -3
- package/skills/autoconverge/workflow/converge.mjs +1 -1
- package/skills/pr-converge/SKILL.md +27 -1
- package/skills/pr-converge/reference/state-schema.md +12 -0
|
@@ -30,7 +30,7 @@ Read the body and the docstring side by side:
|
|
|
30
30
|
- **Returns-clause cardinality.** A `Returns:` clause that names a dict-key prefix family with a plural noun (`the sheen stops`) matches the count of keys in that family in the returned dict literal. When the dict holds one key in the family (`sheen_mid`), the noun is singular (`the sheen stop`); a plural noun there claims two or more entries the dict does not hold. The `check_docstring_returns_plural_cardinality` gate blocks the single-key-with-plural-noun form of this drift at Write/Edit time.
|
|
31
31
|
- **Length-constant superlative vs exact gate.** A module docstring that describes an integer `*_LENGTH` constant with a superlative or range word (`the longest color string the swatch accepts`, `no longer than`) matches how the code consumes the constant. When the only consumer compares `len(...)` against the constant with `==`/`!=` — an exact-length gate where every other length is rejected, not accepted at a shorter length — the superlative prose claims a range of accepted lengths the code never allows. State the exact required length (`the exact #AARRGGBB length`), not a longest/range form. The `check_docstring_length_constant_superlative_vs_exact_gate` gate blocks this drift at Write/Edit time, scanning the constant module's package tree so it sees a consumer that lives in a sibling module; a constant genuinely used as a ceiling (`len(x) <= LIMIT`) is left alone.
|
|
32
32
|
- **Args single-line scope vs span body.** An `Args:` entry that scopes a finding to one named line (`a finding blocks only when its block-anchor line is among the changed lines`) matches the line breadth the body scopes by. When the body builds a `range(...)` span over the finding's source lines and scopes it through a span-intersection scoper that blocks when any line of the span is among the changed lines, the single-line Args wording understates the scope: an edit touching a non-anchor line of the span still blocks. State the Args entry on the same span breadth the body uses (`a finding blocks when any line of its block span is among the changed lines`). The `check_docstring_args_single_line_scope_vs_span` gate blocks the single-line-Args-over-span-body form of this drift at Write/Edit time.
|
|
33
|
-
- **Cardinal-count enumerations.** A docstring that states a count of an outcome family (`the four outcome branches`) and lists those members names every member of that family the module references. When the module imports and exercises a fifth `OUTCOME_*` constant the summary leaves out, the count and the list both under-describe the code. The `check_docstring_cardinal_count_matches_constant_family` gate blocks this drift — a cardinal-count docstring that names two or more members of a referenced `UPPER_SNAKE` constant family, leaves at least one referenced member out, and states a count
|
|
33
|
+
- **Cardinal-count enumerations.** A docstring that states a count of an outcome family (`the four outcome branches`) and lists those members names every member of that family the module references. When the module imports and exercises a fifth `OUTCOME_*` constant the summary leaves out, the count and the list both under-describe the code. The `check_docstring_cardinal_count_matches_constant_family` gate blocks this drift — a cardinal-count docstring that names two or more members of a referenced `UPPER_SNAKE` constant family, leaves at least one referenced member out, and states a count that differs from the family size (a count above it and one below it both trip) — at Write/Edit time, on test modules as well as production modules.
|
|
34
34
|
- **Raises-clause reachability for `LargeZipFile`.** A `Raises:` clause that names `zipfile.LargeZipFile` matches a writer the body opens with ZIP64 forbidden. `zipfile` raises `LargeZipFile` only when an entry needs ZIP64 and `allowZip64` is False; a function that opens its `zipfile.ZipFile` writer in a write mode (`w`/`a`/`x`) with `allowZip64` at its default of True allows ZIP64 and never raises it, so the clause documents an unreachable exception. Drop the entry, or pass `allowZip64=False` when forbidding ZIP64 is the goal. The `check_docstring_raises_unraisable_largezipfile` gate blocks the default-ZIP64-writer form of this drift at Write/Edit time; a writer that forbids ZIP64 on any open, a read-only open, and a function that opens no writer are all left alone.
|
|
35
35
|
- **Module summary scope versus data-schema constants.** A module whose one-line docstring scopes its contents to user-facing text (`User-facing strings: CLI flag names, help text, and log messages`) names every category of constant the body holds. When the body also defines serialization field keys (`JSONL_FIELD_*`), run-metadata schema keys (`RUN_METADATA_CLI_ARG_KEY_*`), or runtime config (`STDOUT_ENCODING`, `MAIN_LOGGING_FORMAT_STRING`), the strings-only summary under-describes the module — the module-responsibility drift the repo flags. Broaden the summary to name the data-schema keys and runtime config. The `check_module_docstring_scope_omits_data_schema_constants` gate blocks this drift at Write/Edit time, and fires only when the summary claims a user-facing-text scope and names no data-schema or runtime-config category, so a summary that already names `field keys`, `schema`, or `runtime config` passes.
|
|
36
36
|
- **Field meaning: run mode versus per record.** A dataclass or `TypedDict` field documented in the class `Attributes:` block states what the field means for one record. When the code sets that field the same way for every record (a run-mode flag such as `is_dry_run = not is_execute` at each write site), the description states the run-mode meaning, not a per-record outcome. A field named `is_dry_run` documented as `True when no STP was written` reads as a per-record write result, but the value tracks the run mode, so a record that writes no file during an execute run still stores `False`. State the run-mode meaning the assignment gives the field. The `check_docstring_field_runmode_outcome` gate blocks the single-file shape of this drift at Write/Edit time — an `Attributes:` entry for a run-mode flag field (a name carrying `dry_run`) whose description carries a per-record write-outcome phrase and no run-mode phrase. The assignment that sets the field sits in another module, out of reach of that single-file gate, so any shape the gate cannot key on stays an O6 audit-lane judgment finding.
|
|
@@ -22,7 +22,7 @@ The `env_var_table_code_drift_blocker.py` hook runs on every Write, Edit, and Mu
|
|
|
22
22
|
3. Resolves the named code file under the repository root (the nearest `.git`-bearing ancestor of the markdown file) and reads its source.
|
|
23
23
|
4. Blocks the write when the file resolves yet its source never references the variable name. For an Edit, drift the file already held on an untouched row is excluded, so only drift the edit introduces is reported.
|
|
24
24
|
|
|
25
|
-
The check stays quiet for a row whose code file resolves nowhere under the repository root (it cannot prove the drift), a row whose second cell holds no code-file path, a table row inside a fenced code block
|
|
25
|
+
The check stays quiet for a row whose code file resolves nowhere under the repository root (it cannot prove the drift), a row whose second cell holds no code-file path, and a table row inside a fenced code block.
|
|
26
26
|
|
|
27
27
|
## Why this is a hook, not a lint pass
|
|
28
28
|
|
|
@@ -10,7 +10,7 @@ paths:
|
|
|
10
10
|
|
|
11
11
|
# New Production File Absent From Its Package Inventory
|
|
12
12
|
|
|
13
|
-
**When this applies:** Any Write that creates a new production code file (`.py`, `.mjs`, `.js`, `.ts`, `.ps1`, `.sh`) in a directory whose sibling `README.md` or `CLAUDE.md` already names two or more of the directory's files in backticks, or in a skill's `scripts/` subdirectory whose parent `SKILL.md` Layout table already names two or more of those scripts.
|
|
13
|
+
**When this applies:** Any Write that creates a new production code file (`.py`, `.mjs`, `.js`, `.ts`, `.ps1`, `.sh`) in a directory whose sibling `README.md` or `CLAUDE.md` already names two or more of the directory's files in backticks, in a directory whose own `SKILL.md` names two or more of the directory's files, or in a skill's `scripts/` subdirectory whose parent `SKILL.md` Layout table already names two or more of those scripts.
|
|
14
14
|
|
|
15
15
|
## Rule
|
|
16
16
|
|
|
@@ -23,7 +23,7 @@ Two complementary checks in `code_rules_paired_test.py` (both dispatched from `c
|
|
|
23
23
|
`check_public_function_missing_paired_test` runs on a production Python write or edit and flags a public function when all of these hold:
|
|
24
24
|
|
|
25
25
|
1. The target is production code — not a test module, hook infrastructure, config module, migration, workflow registry, or `__init__.py`.
|
|
26
|
-
2. A stem-matched test file
|
|
26
|
+
2. A stem-matched test file exists for the module — `test_<stem>.py` or `<stem>_test.py` beside the module, or `test_<stem>.py` under an ancestor `tests/` directory.
|
|
27
27
|
3. That suite already exercises the module — referencing at least one public function the module defines, or referencing one of its private (underscore-prefixed) helper functions by name — the signature of a maintained per-module suite rather than a placeholder or unrelated test file.
|
|
28
28
|
4. The public function is referenced by no test file in the directory that holds the stem-matched test.
|
|
29
29
|
|
|
@@ -21,6 +21,7 @@ Shared infrastructure for the PR audit-fix loop used by `bugteam` and `pr-conver
|
|
|
21
21
|
| `packages/claude-dev-env/skills/_shared/pr-loop/scripts/write_fix_outcomes.py` | Writes the per-loop fix outcome XML into the workspace. |
|
|
22
22
|
| `packages/claude-dev-env/skills/_shared/pr-loop/scripts/preflight_worktree.py` | Verifies the working directory is a healthy worktree for the target PR's repo. |
|
|
23
23
|
| `packages/claude-dev-env/skills/_shared/pr-loop/scripts/teardown_worktrees.py` | Removes loop worktrees on clean exit. |
|
|
24
|
+
| `packages/claude-dev-env/skills/_shared/pr-loop/scripts/write_handoff.py` | Writes durable resume-handoff files under `~/.claude/runtime/pr-loop/<run-name>/` at each converge checkpoint. |
|
|
24
25
|
| `packages/claude-dev-env/skills/_shared/pr-loop/scripts/_path_resolver.py` | Resolves workspace and worktree paths from PR metadata. |
|
|
25
26
|
| `packages/claude-dev-env/skills/_shared/pr-loop/scripts/_cli_utils.py` | Shared CLI argument parsing helpers. |
|
|
26
27
|
| `packages/claude-dev-env/skills/_shared/pr-loop/scripts/_xml_utils.py` | XML serialization helpers. |
|
|
@@ -13,6 +13,7 @@ Python scripts that run the PR audit-fix loop at runtime. Both `bugteam` and `pr
|
|
|
13
13
|
| `write_fix_outcomes.py` | Writes per-loop fix outcome XML into the workspace. |
|
|
14
14
|
| `preflight_worktree.py` | Verifies the working directory is a healthy git worktree for the target PR's repo. Supports `--mode strict` to abort when the repo does not match. |
|
|
15
15
|
| `teardown_worktrees.py` | Removes per-PR worktrees after a clean loop exit. |
|
|
16
|
+
| `write_handoff.py` | Writes durable resume-handoff files under the run's `~/.claude/runtime/pr-loop` directory at each converge checkpoint. |
|
|
16
17
|
| `_path_resolver.py` | Resolves workspace and worktree paths from PR owner, repo, and number. |
|
|
17
18
|
| `_cli_utils.py` | Shared CLI argument parsing helpers (argparse wrappers). |
|
|
18
19
|
| `_xml_utils.py` | XML serialization helpers for outcome files. |
|
|
@@ -9,6 +9,7 @@ Python package of named constants for the `pr-loop` shared scripts. All constant
|
|
|
9
9
|
| `__init__.py` | Package marker. |
|
|
10
10
|
| `path_resolver_constants.py` | Path template strings and format constants: workspace directory naming, worktree directory name, diff patch filename pattern, outcome XML filename patterns, fix status values, audit constraint texts, audit category entries, fix execution steps, and fix constraint texts. |
|
|
11
11
|
| `preflight_constants.py` | Constants used by `preflight_worktree.py` for exit codes and output marker strings. |
|
|
12
|
+
| `handoff_constants.py` | Filename, path-segment, and template constants for the durable handoff writer. |
|
|
12
13
|
|
|
13
14
|
## Usage
|
|
14
15
|
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Filename, path-segment, and template constants for the durable handoff writer.
|
|
2
|
+
|
|
3
|
+
Consumed by `write_handoff.py`, which writes each pr-loop run's resume-handoff
|
|
4
|
+
files under `~/.claude/runtime/pr-loop/<run-name>/`.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
ALL_HANDOFF_DIR_SEGMENTS = (".claude", "runtime", "pr-loop")
|
|
8
|
+
HANDOFF_JSON_FILENAME = "handoff.json"
|
|
9
|
+
HANDOFF_MARKDOWN_FILENAME = "HANDOFF.md"
|
|
10
|
+
STATE_COPY_FILENAME = "state-copy.json"
|
|
11
|
+
ATOMIC_STAGING_SUFFIX = ".tmp"
|
|
12
|
+
HANDOFF_JSON_INDENT = 2
|
|
13
|
+
COMPLETED_STEPS_SEPARATOR = ","
|
|
14
|
+
STEP_LINE_SEPARATOR = "\n"
|
|
15
|
+
|
|
16
|
+
NO_STATE_SNAPSHOT_LINE = (
|
|
17
|
+
"No state snapshot was captured. Rebuild loop state from the PR before resuming."
|
|
18
|
+
)
|
|
19
|
+
DEFAULT_NEXT_STEP_LINE = "Continue from the resume command above."
|
|
20
|
+
NO_STEPS_DONE_LINE = "- (none recorded yet)"
|
|
21
|
+
|
|
22
|
+
HANDOFF_MARKDOWN_TEMPLATE = """# Resume handoff — PR {pr_number} ({head_ref})
|
|
23
|
+
|
|
24
|
+
Goal: drive PR {pr_number} to convergence. This run stopped at phase `{phase}`.
|
|
25
|
+
|
|
26
|
+
## Resume command
|
|
27
|
+
|
|
28
|
+
Run this exactly:
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
{resume_command}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## State file to trust
|
|
35
|
+
|
|
36
|
+
{state_line}
|
|
37
|
+
|
|
38
|
+
## Steps already done
|
|
39
|
+
|
|
40
|
+
{steps_block}
|
|
41
|
+
|
|
42
|
+
## Next step
|
|
43
|
+
|
|
44
|
+
{next_step}
|
|
45
|
+
"""
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"""Tests for write_handoff: the durable per-run handoff writer."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import importlib.util
|
|
6
|
+
import json
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from types import ModuleType
|
|
10
|
+
|
|
11
|
+
import pytest
|
|
12
|
+
|
|
13
|
+
_SCRIPTS_DIR = Path(__file__).resolve().parent
|
|
14
|
+
if str(_SCRIPTS_DIR) not in sys.path:
|
|
15
|
+
sys.path.insert(0, str(_SCRIPTS_DIR))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _load_write_handoff_module() -> ModuleType:
|
|
19
|
+
module_path = _SCRIPTS_DIR / "write_handoff.py"
|
|
20
|
+
spec = importlib.util.spec_from_file_location("write_handoff", module_path)
|
|
21
|
+
assert spec is not None
|
|
22
|
+
assert spec.loader is not None
|
|
23
|
+
module = importlib.util.module_from_spec(spec)
|
|
24
|
+
spec.loader.exec_module(module)
|
|
25
|
+
return module
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
write_handoff = _load_write_handoff_module()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
_FROZEN_TIMESTAMP = "2026-07-03T12:00:00+00:00"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _point_base_dir_at(monkeypatch: pytest.MonkeyPatch, base: Path) -> None:
|
|
35
|
+
monkeypatch.setattr(write_handoff, "_handoff_base_dir", lambda: base)
|
|
36
|
+
monkeypatch.setattr(write_handoff, "_now_iso", lambda: _FROZEN_TIMESTAMP)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def test_resolve_handoff_dir_joins_run_name_under_base(
|
|
40
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
41
|
+
) -> None:
|
|
42
|
+
"""resolve_handoff_dir places the run's directory under the durable base."""
|
|
43
|
+
_point_base_dir_at(monkeypatch, tmp_path)
|
|
44
|
+
resolved = write_handoff.resolve_handoff_dir("bugteam-pr-4")
|
|
45
|
+
assert resolved == tmp_path / "bugteam-pr-4"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_handoff_base_dir_lives_under_home_runtime_pr_loop(
|
|
49
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
50
|
+
) -> None:
|
|
51
|
+
"""The durable base sits under the user home, not the OS temp directory."""
|
|
52
|
+
monkeypatch.setenv("USERPROFILE", str(tmp_path))
|
|
53
|
+
monkeypatch.setenv("HOME", str(tmp_path))
|
|
54
|
+
base = write_handoff._handoff_base_dir()
|
|
55
|
+
assert base == tmp_path / ".claude" / "runtime" / "pr-loop"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_write_handoff_records_resume_command_and_phase(
|
|
59
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
60
|
+
) -> None:
|
|
61
|
+
"""write_handoff writes handoff.json and HANDOFF.md carrying the resume
|
|
62
|
+
command, phase, run id, completed steps, and a timestamp."""
|
|
63
|
+
_point_base_dir_at(monkeypatch, tmp_path)
|
|
64
|
+
handoff_dir = write_handoff.write_handoff(
|
|
65
|
+
pr_number=4,
|
|
66
|
+
head_ref="feat/pr-loop-durable-handoff",
|
|
67
|
+
phase="tick",
|
|
68
|
+
resume_command="claude --resume /pr-converge",
|
|
69
|
+
run_id="wf_abc123",
|
|
70
|
+
all_completed_steps=["preflight", "audit"],
|
|
71
|
+
note="Copilot wait-gate still open.",
|
|
72
|
+
)
|
|
73
|
+
assert handoff_dir == tmp_path / "bugteam-pr-4"
|
|
74
|
+
|
|
75
|
+
payload = json.loads((handoff_dir / "handoff.json").read_text(encoding="utf-8"))
|
|
76
|
+
assert payload["resume_command"] == "claude --resume /pr-converge"
|
|
77
|
+
assert payload["phase"] == "tick"
|
|
78
|
+
assert payload["run_id"] == "wf_abc123"
|
|
79
|
+
assert payload["completed_steps"] == ["preflight", "audit"]
|
|
80
|
+
assert payload["timestamp"] == _FROZEN_TIMESTAMP
|
|
81
|
+
|
|
82
|
+
markdown = (handoff_dir / "HANDOFF.md").read_text(encoding="utf-8")
|
|
83
|
+
assert "claude --resume /pr-converge" in markdown
|
|
84
|
+
assert "preflight" in markdown
|
|
85
|
+
assert "Copilot wait-gate still open." in markdown
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def test_write_handoff_copies_state_file_when_given(
|
|
89
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
90
|
+
) -> None:
|
|
91
|
+
"""A supplied --state-file is copied to state-copy.json and the copy path is
|
|
92
|
+
recorded in handoff.json."""
|
|
93
|
+
_point_base_dir_at(monkeypatch, tmp_path)
|
|
94
|
+
source_state = tmp_path / "loop-state.json"
|
|
95
|
+
source_state.write_text('{"loop_count": 3}', encoding="utf-8")
|
|
96
|
+
|
|
97
|
+
handoff_dir = write_handoff.write_handoff(
|
|
98
|
+
pr_number=4,
|
|
99
|
+
head_ref="feat/branch",
|
|
100
|
+
phase="teardown",
|
|
101
|
+
resume_command="claude --resume /autoconverge",
|
|
102
|
+
state_file=source_state,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
state_copy = handoff_dir / "state-copy.json"
|
|
106
|
+
assert json.loads(state_copy.read_text(encoding="utf-8")) == {"loop_count": 3}
|
|
107
|
+
payload = json.loads((handoff_dir / "handoff.json").read_text(encoding="utf-8"))
|
|
108
|
+
assert payload["state_file"] == state_copy.as_posix()
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def test_write_handoff_omits_state_copy_when_absent(
|
|
112
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
113
|
+
) -> None:
|
|
114
|
+
"""With no --state-file, no state-copy.json is written and state_file is null."""
|
|
115
|
+
_point_base_dir_at(monkeypatch, tmp_path)
|
|
116
|
+
handoff_dir = write_handoff.write_handoff(
|
|
117
|
+
pr_number=4,
|
|
118
|
+
head_ref="feat/branch",
|
|
119
|
+
phase="tick",
|
|
120
|
+
resume_command="claude --resume /pr-converge",
|
|
121
|
+
)
|
|
122
|
+
assert not (handoff_dir / "state-copy.json").exists()
|
|
123
|
+
payload = json.loads((handoff_dir / "handoff.json").read_text(encoding="utf-8"))
|
|
124
|
+
assert payload["state_file"] is None
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def test_parse_arguments_reads_completed_steps_value() -> None:
|
|
128
|
+
"""The CLI captures the raw comma-separated --completed-steps value."""
|
|
129
|
+
arguments = write_handoff.parse_arguments(
|
|
130
|
+
[
|
|
131
|
+
"--pr-number",
|
|
132
|
+
"4",
|
|
133
|
+
"--head-ref",
|
|
134
|
+
"feat/branch",
|
|
135
|
+
"--phase",
|
|
136
|
+
"tick",
|
|
137
|
+
"--resume-command",
|
|
138
|
+
"claude --resume /pr-converge",
|
|
139
|
+
"--completed-steps",
|
|
140
|
+
"preflight,audit,fix",
|
|
141
|
+
]
|
|
142
|
+
)
|
|
143
|
+
assert arguments.completed_steps == "preflight,audit,fix"
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def test_main_writes_handoff_and_prints_dir(
|
|
147
|
+
tmp_path: Path,
|
|
148
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
149
|
+
capsys: pytest.CaptureFixture[str],
|
|
150
|
+
) -> None:
|
|
151
|
+
"""main writes the handoff files and prints the durable directory path."""
|
|
152
|
+
_point_base_dir_at(monkeypatch, tmp_path)
|
|
153
|
+
exit_code = write_handoff.main(
|
|
154
|
+
[
|
|
155
|
+
"--pr-number",
|
|
156
|
+
"4",
|
|
157
|
+
"--head-ref",
|
|
158
|
+
"feat/branch",
|
|
159
|
+
"--phase",
|
|
160
|
+
"tick",
|
|
161
|
+
"--resume-command",
|
|
162
|
+
"claude --resume /pr-converge",
|
|
163
|
+
"--completed-steps",
|
|
164
|
+
"preflight,audit",
|
|
165
|
+
]
|
|
166
|
+
)
|
|
167
|
+
assert exit_code == 0
|
|
168
|
+
printed = capsys.readouterr().out.strip()
|
|
169
|
+
assert "\\" not in printed
|
|
170
|
+
assert printed.endswith("bugteam-pr-4")
|
|
171
|
+
payload = json.loads(
|
|
172
|
+
(tmp_path / "bugteam-pr-4" / "handoff.json").read_text(encoding="utf-8")
|
|
173
|
+
)
|
|
174
|
+
assert payload["completed_steps"] == ["preflight", "audit"]
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def test_main_returns_one_on_non_utf8_state_file(
|
|
178
|
+
tmp_path: Path,
|
|
179
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
180
|
+
capsys: pytest.CaptureFixture[str],
|
|
181
|
+
) -> None:
|
|
182
|
+
"""main exits 1 rather than crashing when --state-file holds non-UTF-8 bytes."""
|
|
183
|
+
_point_base_dir_at(monkeypatch, tmp_path)
|
|
184
|
+
bad_state = tmp_path / "loop-state.json"
|
|
185
|
+
bad_state.write_bytes(b"\xff\xfe not valid utf-8")
|
|
186
|
+
exit_code = write_handoff.main(
|
|
187
|
+
[
|
|
188
|
+
"--pr-number",
|
|
189
|
+
"4",
|
|
190
|
+
"--head-ref",
|
|
191
|
+
"feat/branch",
|
|
192
|
+
"--phase",
|
|
193
|
+
"teardown",
|
|
194
|
+
"--resume-command",
|
|
195
|
+
"claude --resume /autoconverge",
|
|
196
|
+
"--state-file",
|
|
197
|
+
str(bad_state),
|
|
198
|
+
]
|
|
199
|
+
)
|
|
200
|
+
assert exit_code == 1
|
|
201
|
+
assert "write_handoff failed" in capsys.readouterr().err
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
"""Write durable resume-handoff files for a pr-loop run.
|
|
2
|
+
|
|
3
|
+
A converge loop can stop mid-run: a paused tick, a budget cutoff, a killed
|
|
4
|
+
session. The next session then has no pointer to where the run was. This writer
|
|
5
|
+
drops that pointer somewhere the OS temp sweep cannot purge.
|
|
6
|
+
|
|
7
|
+
::
|
|
8
|
+
|
|
9
|
+
write_handoff.py --pr-number <N> --head-ref <branch> --phase tick
|
|
10
|
+
--resume-command "claude --resume /pr-converge" [--state-file <PATH>]
|
|
11
|
+
writes: ~/.claude/runtime/pr-loop/<run-name>/handoff.json + HANDOFF.md
|
|
12
|
+
plus: state-copy.json (only when --state-file is given)
|
|
13
|
+
|
|
14
|
+
It records the resume command, the phase reached, the steps already done, and a
|
|
15
|
+
copy of the loop state, so a fresh session picks up without re-deriving it.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import argparse
|
|
21
|
+
import json
|
|
22
|
+
import os
|
|
23
|
+
import sys
|
|
24
|
+
from datetime import datetime, timezone
|
|
25
|
+
from pathlib import Path
|
|
26
|
+
|
|
27
|
+
_self_dir = Path(__file__).resolve().parent
|
|
28
|
+
if str(_self_dir) not in sys.path:
|
|
29
|
+
sys.path.insert(0, str(_self_dir))
|
|
30
|
+
|
|
31
|
+
from _path_resolver import build_run_name # noqa: E402
|
|
32
|
+
from skills_pr_loop_constants.handoff_constants import ( # noqa: E402
|
|
33
|
+
ATOMIC_STAGING_SUFFIX,
|
|
34
|
+
COMPLETED_STEPS_SEPARATOR,
|
|
35
|
+
DEFAULT_NEXT_STEP_LINE,
|
|
36
|
+
ALL_HANDOFF_DIR_SEGMENTS,
|
|
37
|
+
HANDOFF_JSON_FILENAME,
|
|
38
|
+
HANDOFF_JSON_INDENT,
|
|
39
|
+
HANDOFF_MARKDOWN_FILENAME,
|
|
40
|
+
HANDOFF_MARKDOWN_TEMPLATE,
|
|
41
|
+
NO_STATE_SNAPSHOT_LINE,
|
|
42
|
+
NO_STEPS_DONE_LINE,
|
|
43
|
+
STATE_COPY_FILENAME,
|
|
44
|
+
STEP_LINE_SEPARATOR,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _now_iso() -> str:
|
|
49
|
+
"""Return the current UTC time as an ISO-format timestamp string.
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
The stamp recorded on the handoff so a reader knows how fresh it is.
|
|
53
|
+
"""
|
|
54
|
+
return datetime.now(timezone.utc).isoformat()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _handoff_base_dir() -> Path:
|
|
58
|
+
"""Return the durable base directory that holds every run's handoff folder.
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
The ``~/.claude/runtime/pr-loop`` path under the user's home directory.
|
|
62
|
+
"""
|
|
63
|
+
return Path.home().joinpath(*ALL_HANDOFF_DIR_SEGMENTS)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def resolve_handoff_dir(run_name: str) -> Path:
|
|
67
|
+
"""Resolve the durable handoff directory for a run name.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
run_name: Run name token (from build_run_name).
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
Absolute path to the run's handoff directory under the durable base.
|
|
74
|
+
"""
|
|
75
|
+
return _handoff_base_dir() / run_name
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _write_text_atomic(target_path: Path, text: str) -> None:
|
|
79
|
+
"""Write text to a path atomically via a staging file and a rename.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
target_path: Destination file path.
|
|
83
|
+
text: Full file contents to write.
|
|
84
|
+
"""
|
|
85
|
+
target_path.parent.mkdir(parents=True, exist_ok=True)
|
|
86
|
+
staging_path = target_path.with_name(
|
|
87
|
+
f"{target_path.name}{ATOMIC_STAGING_SUFFIX}.{os.getpid()}"
|
|
88
|
+
)
|
|
89
|
+
staging_path.write_text(text, encoding="utf-8")
|
|
90
|
+
os.replace(staging_path, target_path)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _snapshot_state(handoff_dir: Path, state_file: Path | None) -> str | None:
|
|
94
|
+
"""Copy a loop state file into the handoff directory when one is supplied.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
handoff_dir: The run's durable handoff directory.
|
|
98
|
+
state_file: Loop state file to snapshot, or None to skip the copy.
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
The forward-slash path of the copy to trust, or None when no state
|
|
102
|
+
file was supplied.
|
|
103
|
+
"""
|
|
104
|
+
if state_file is None:
|
|
105
|
+
return None
|
|
106
|
+
state_copy_path = handoff_dir / STATE_COPY_FILENAME
|
|
107
|
+
_write_text_atomic(state_copy_path, state_file.read_text(encoding="utf-8"))
|
|
108
|
+
return state_copy_path.as_posix()
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _steps_markdown_block(all_completed_steps: list[str]) -> str:
|
|
112
|
+
"""Render the completed steps as a markdown bullet list.
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
all_completed_steps: Steps already finished this run.
|
|
116
|
+
|
|
117
|
+
Returns:
|
|
118
|
+
A bullet list, or a placeholder line when no steps are recorded.
|
|
119
|
+
"""
|
|
120
|
+
if not all_completed_steps:
|
|
121
|
+
return NO_STEPS_DONE_LINE
|
|
122
|
+
return STEP_LINE_SEPARATOR.join(f"- {each_step}" for each_step in all_completed_steps)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _build_handoff_markdown(
|
|
126
|
+
*,
|
|
127
|
+
pr_number: int,
|
|
128
|
+
head_ref: str,
|
|
129
|
+
phase: str,
|
|
130
|
+
resume_command: str,
|
|
131
|
+
trusted_state_path: str | None,
|
|
132
|
+
all_completed_steps: list[str],
|
|
133
|
+
note: str | None,
|
|
134
|
+
) -> str:
|
|
135
|
+
"""Build the human-facing HANDOFF.md body for a fresh session.
|
|
136
|
+
|
|
137
|
+
Args:
|
|
138
|
+
pr_number: Pull request number.
|
|
139
|
+
head_ref: Head branch ref.
|
|
140
|
+
phase: The checkpoint the run reached.
|
|
141
|
+
resume_command: The exact command a fresh session runs to resume.
|
|
142
|
+
trusted_state_path: The state-copy path to trust, or None.
|
|
143
|
+
all_completed_steps: Steps already finished this run.
|
|
144
|
+
note: Free-text next-step note, or None.
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
The rendered markdown document.
|
|
148
|
+
"""
|
|
149
|
+
state_line = (
|
|
150
|
+
f"Trust this state snapshot: {trusted_state_path}"
|
|
151
|
+
if trusted_state_path is not None
|
|
152
|
+
else NO_STATE_SNAPSHOT_LINE
|
|
153
|
+
)
|
|
154
|
+
return HANDOFF_MARKDOWN_TEMPLATE.format(
|
|
155
|
+
pr_number=pr_number,
|
|
156
|
+
head_ref=head_ref,
|
|
157
|
+
phase=phase,
|
|
158
|
+
resume_command=resume_command,
|
|
159
|
+
state_line=state_line,
|
|
160
|
+
steps_block=_steps_markdown_block(all_completed_steps),
|
|
161
|
+
next_step=note if note else DEFAULT_NEXT_STEP_LINE,
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def write_handoff(
|
|
166
|
+
*,
|
|
167
|
+
pr_number: int,
|
|
168
|
+
head_ref: str,
|
|
169
|
+
phase: str,
|
|
170
|
+
resume_command: str,
|
|
171
|
+
state_file: Path | None = None,
|
|
172
|
+
run_id: str | None = None,
|
|
173
|
+
all_completed_steps: list[str] | None = None,
|
|
174
|
+
note: str | None = None,
|
|
175
|
+
) -> Path:
|
|
176
|
+
"""Write handoff.json, HANDOFF.md, and an optional state copy for a run.
|
|
177
|
+
|
|
178
|
+
Names the run with build_run_name and places the files under the durable
|
|
179
|
+
handoff directory. When a state file is supplied, its contents are copied to
|
|
180
|
+
state-copy.json and that copy path is recorded as the state to trust.
|
|
181
|
+
|
|
182
|
+
Args:
|
|
183
|
+
pr_number: Pull request number.
|
|
184
|
+
head_ref: Head branch ref.
|
|
185
|
+
phase: The checkpoint the run reached (e.g. 'tick', 'teardown').
|
|
186
|
+
resume_command: The exact command a fresh session runs to resume.
|
|
187
|
+
state_file: Loop state file to snapshot, or None to skip the copy.
|
|
188
|
+
run_id: Workflow run id to resume from, or None.
|
|
189
|
+
all_completed_steps: Steps already finished this run, or None.
|
|
190
|
+
note: Free-text next-step note for the fresh session, or None.
|
|
191
|
+
|
|
192
|
+
Returns:
|
|
193
|
+
Path to the run's durable handoff directory.
|
|
194
|
+
"""
|
|
195
|
+
steps_done = all_completed_steps if all_completed_steps else []
|
|
196
|
+
run_name = build_run_name(pr_number, head_ref, is_multi_pr=False)
|
|
197
|
+
handoff_dir = resolve_handoff_dir(run_name)
|
|
198
|
+
handoff_dir.mkdir(parents=True, exist_ok=True)
|
|
199
|
+
|
|
200
|
+
trusted_state_path = _snapshot_state(handoff_dir, state_file)
|
|
201
|
+
payload = {
|
|
202
|
+
"pr_number": pr_number,
|
|
203
|
+
"head_ref": head_ref,
|
|
204
|
+
"phase": phase,
|
|
205
|
+
"resume_command": resume_command,
|
|
206
|
+
"run_id": run_id,
|
|
207
|
+
"state_file": trusted_state_path,
|
|
208
|
+
"completed_steps": steps_done,
|
|
209
|
+
"note": note,
|
|
210
|
+
"timestamp": _now_iso(),
|
|
211
|
+
}
|
|
212
|
+
_write_text_atomic(
|
|
213
|
+
handoff_dir / HANDOFF_JSON_FILENAME,
|
|
214
|
+
json.dumps(payload, indent=HANDOFF_JSON_INDENT) + "\n",
|
|
215
|
+
)
|
|
216
|
+
_write_text_atomic(
|
|
217
|
+
handoff_dir / HANDOFF_MARKDOWN_FILENAME,
|
|
218
|
+
_build_handoff_markdown(
|
|
219
|
+
pr_number=pr_number,
|
|
220
|
+
head_ref=head_ref,
|
|
221
|
+
phase=phase,
|
|
222
|
+
resume_command=resume_command,
|
|
223
|
+
trusted_state_path=trusted_state_path,
|
|
224
|
+
all_completed_steps=steps_done,
|
|
225
|
+
note=note,
|
|
226
|
+
),
|
|
227
|
+
)
|
|
228
|
+
return handoff_dir
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def _split_completed_steps(raw_steps: str) -> list[str]:
|
|
232
|
+
"""Split a comma-separated steps argument into a trimmed, non-empty list.
|
|
233
|
+
|
|
234
|
+
Args:
|
|
235
|
+
raw_steps: The raw --completed-steps CLI value.
|
|
236
|
+
|
|
237
|
+
Returns:
|
|
238
|
+
The individual step names with surrounding whitespace removed.
|
|
239
|
+
"""
|
|
240
|
+
return [
|
|
241
|
+
each_segment.strip()
|
|
242
|
+
for each_segment in raw_steps.split(COMPLETED_STEPS_SEPARATOR)
|
|
243
|
+
if each_segment.strip()
|
|
244
|
+
]
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def parse_arguments(all_argv: list[str]) -> argparse.Namespace:
|
|
248
|
+
"""Parse command-line arguments.
|
|
249
|
+
|
|
250
|
+
Args:
|
|
251
|
+
all_argv: Command-line argument list.
|
|
252
|
+
|
|
253
|
+
Returns:
|
|
254
|
+
Parsed namespace with the handoff fields.
|
|
255
|
+
"""
|
|
256
|
+
parser = argparse.ArgumentParser(description=__doc__)
|
|
257
|
+
parser.add_argument("--pr-number", type=int, required=True)
|
|
258
|
+
parser.add_argument("--head-ref", required=True)
|
|
259
|
+
parser.add_argument("--phase", required=True)
|
|
260
|
+
parser.add_argument("--resume-command", required=True)
|
|
261
|
+
parser.add_argument("--state-file", type=Path, default=None)
|
|
262
|
+
parser.add_argument("--run-id", default=None)
|
|
263
|
+
parser.add_argument("--completed-steps", default="")
|
|
264
|
+
parser.add_argument("--note", default=None)
|
|
265
|
+
return parser.parse_args(all_argv)
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _write_handoff_from_arguments(arguments: argparse.Namespace) -> Path:
|
|
269
|
+
"""Map parsed CLI arguments onto a write_handoff call.
|
|
270
|
+
|
|
271
|
+
Args:
|
|
272
|
+
arguments: The namespace parse_arguments produced.
|
|
273
|
+
|
|
274
|
+
Returns:
|
|
275
|
+
Path to the run's durable handoff directory.
|
|
276
|
+
"""
|
|
277
|
+
return write_handoff(
|
|
278
|
+
pr_number=arguments.pr_number,
|
|
279
|
+
head_ref=arguments.head_ref,
|
|
280
|
+
phase=arguments.phase,
|
|
281
|
+
resume_command=arguments.resume_command,
|
|
282
|
+
state_file=arguments.state_file,
|
|
283
|
+
run_id=arguments.run_id,
|
|
284
|
+
all_completed_steps=_split_completed_steps(arguments.completed_steps),
|
|
285
|
+
note=arguments.note,
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def main(all_arguments: list[str]) -> int:
|
|
290
|
+
"""Entry point: write the handoff files and print the durable directory.
|
|
291
|
+
|
|
292
|
+
Args:
|
|
293
|
+
all_arguments: Command-line arguments.
|
|
294
|
+
|
|
295
|
+
Returns:
|
|
296
|
+
0 on success, 1 on a state-file read or write failure.
|
|
297
|
+
"""
|
|
298
|
+
arguments = parse_arguments(all_arguments)
|
|
299
|
+
try:
|
|
300
|
+
handoff_dir = _write_handoff_from_arguments(arguments)
|
|
301
|
+
except (OSError, UnicodeDecodeError) as read_or_write_error:
|
|
302
|
+
print(f"write_handoff failed: {read_or_write_error}", file=sys.stderr)
|
|
303
|
+
return 1
|
|
304
|
+
print(handoff_dir.as_posix())
|
|
305
|
+
return 0
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
if __name__ == "__main__":
|
|
309
|
+
raise SystemExit(main(sys.argv[1:]))
|