Skip to content
Open
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
23 changes: 16 additions & 7 deletions .agents/skills/java-ast-refactoring/SKILL.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
---
name: java-ast-refactoring
description: "AST-aware Java refactoring using OpenRewrite. Use when asked to structurally refactor Java code, change class names, change method signatures/overloads, replace builder patterns, modify annotations, or perform cross-file structural replacements. Note: Renaming fields or local variables/parameters is not supported natively via simple YAML recipes in the standard openrewrite modules. Don't use for: formatting-only fixes (use spotlessApply / javaIncrementalFormatApply), non-Java files, or single-line edits that a direct file edit handles safely."
description: >-
AST-aware Java refactoring using OpenRewrite. Use when asked to structurally
refactor Java code, change class names, change method signatures/overloads,
replace builder patterns, modify annotations, or perform cross-file structural
replacements. Note: Renaming fields or local variables/parameters is not
supported natively via simple YAML recipes in the standard openrewrite
modules. Don't use for formatting-only fixes (use javaIncrementalFormatApply
for Java or spotlessApply for Gradle), non-Java files, or single-line edits
that a direct file edit handles safely.
---

# AST-Aware Java Refactoring
Expand All @@ -24,26 +32,27 @@ python3 .agents/skills/java-ast-refactoring/scripts/safe_rename.py <filepath> <o
./.agents/skills/java-ast-refactoring/scripts/run_rewrite.sh rewrite.yml
```
A recipe stored elsewhere is copied to `./rewrite.yml` for the run and removed afterwards; a recipe already at `./rewrite.yml` is used in place and left untouched. Delete your own `./rewrite.yml` before committing.
4. The script will safely apply the AST transformations and then automatically run `./gradlew spotlessApply` and `./gradlew javaIncrementalFormatApply` on the modified files to automatically fix any Checkstyle line-length and import ordering issues caused by longer/shorter identifiers.
4. The script applies the AST transformations and then runs `./gradlew spotlessApply javaIncrementalFormatApply` (`spotlessApply` formats `*.gradle`; `javaIncrementalFormatApply` formats Java and sorts imports on changed lines), cleaning up temporary files automatically on exit or error.

## Verification (Mandatory)

1. Review the full diff (`git diff --stat` and `git diff`) and confirm every change is intended. Revert unrelated edits.
2. Compile everything after running OpenRewrite to ensure no compilation errors were introduced:
2. Compile and run Checkstyle on all affected modules after OpenRewrite (avoid bare `./gradlew build`, which triggers `console-webapp` `npmInstallDeps` and Docker image builds):
```bash
./gradlew build -x test -x :console-webapp:buildConsoleWebapp
./gradlew :core:compileJava :core:compileTestJava :core:checkstyleMain :core:checkstyleTest
# Add :<module>:compileJava :<module>:compileTestJava for other modified modules (e.g., :proxy, :networking, :util)
```
3. Run the tests covering the touched code, e.g. `./gradlew :core:standardTest -x :console-webapp:buildConsoleWebapp`.
3. Run the tests covering the touched code, e.g. `./gradlew :core:standardTest` (or `:<module>:test`).
4. Confirm `git status` shows no stray files (`rewrite.yml`, `rewrite-init.gradle`, or a modified `console-webapp/package-lock.json`).
5. Commit, then run the `pr-polisher` skill (`python3 .agents/skills/pr-polisher/scripts/check_diff.py`) to catch leftover fully qualified names, unused imports, and other anti-patterns.

## Error Recovery

* **Recipe not activated / "Could not extract 'name:'":** Ensure the recipe has a top-level `name:` line and `type: specs.openrewrite.org/v1beta/recipe`.
* **Recipe not activated / "Could not extract top-level 'name:'":** Ensure the recipe has a top-level `name:` line and `type: specs.openrewrite.org/v1beta/recipe`.
* **No changes made:** The `methodPattern` or type name likely does not match. Use fully qualified types, `..` for any parameters, and verify against the method patterns section of `references/rewrite_recipes.md`.
* **Gradle or plugin resolution failure:** Re-run with `--stacktrace`. The init script pins the OpenRewrite plugin version; if it is incompatible with the current Gradle version, update the version in `scripts/run_rewrite.sh`.
* **Formatter failures after the run:** Re-run `./gradlew spotlessApply javaIncrementalFormatApply`, then repeat the compile step.
* **Interrupted run:** Remove leftover `rewrite-init.gradle` (and `./rewrite.yml` if it was a temporary copy), then restart.
* **Hard-killed run (`SIGKILL`):** The script removes `rewrite-init.gradle` (and temporary `./rewrite.yml`) automatically via an `EXIT` trap; only if forcefully killed, remove leftover temporary files before restarting.
* **Bad transformation:** Discard it with `git checkout -- <paths>` (or `git stash`), refine the recipe, and re-run rather than hand-patching the output.

## Known Limitations & Troubleshooting
Expand Down
42 changes: 29 additions & 13 deletions .agents/skills/java-ast-refactoring/scripts/run_rewrite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,48 @@
# limitations under the License.

# Wrapper script to dynamically execute OpenRewrite without modifying build.gradle
set -euo pipefail

if [ -z "$1" ]; then
if [ $# -lt 1 ] || [ -z "$1" ]; then
echo "Usage: $0 <path-to-rewrite.yml>"
exit 1
fi

RECIPE_FILE=$(realpath "$1")
RECIPE_FILE=$(python3 -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$1")
if [ ! -f "$RECIPE_FILE" ]; then
echo "Error: Recipe file $RECIPE_FILE not found."
exit 1
fi

# Extract the name of the recipe from the YAML to activate it
RECIPE_NAME=$(grep -oP '(?<=name: ).*' "$RECIPE_FILE" | head -n 1)
# Extract the top-level recipe name from the YAML to activate it (portable awk).
RECIPE_NAME=$(awk '
/^name:[[:space:]]*/ {
sub(/^name:[[:space:]]*/, "")
sub(/[[:space:]]*(#.*)?$/, "")
gsub(/^["'\'']|["'\'']$/, "")
print
exit
}
' "$RECIPE_FILE")

if [ -z "$RECIPE_NAME" ]; then
echo "Error: Could not extract 'name:' from $RECIPE_FILE"
echo "Error: Could not extract top-level 'name:' from $RECIPE_FILE"
exit 1
fi

INIT_SCRIPT="rewrite-init.gradle"
CREATED_INIT=false
COPIED_RECIPE=false

cleanup() {
if [[ "$CREATED_INIT" == true ]]; then
rm -f "$INIT_SCRIPT"
fi
if [[ "$COPIED_RECIPE" == true ]]; then
rm -f ./rewrite.yml
fi
}
trap cleanup EXIT

cat << EOF > "$INIT_SCRIPT"
initscript {
Expand Down Expand Up @@ -65,12 +86,13 @@ allprojects {
apply plugin: org.openrewrite.gradle.RewritePlugin
}
EOF
CREATED_INIT=true

# OpenRewrite reads ./rewrite.yml from the workspace root. Copy the recipe there
# temporarily unless it already is that file (copying a file onto itself fails,
# and deleting it afterwards would destroy the user's recipe).
COPIED_RECIPE=false
if [[ "$RECIPE_FILE" != "$(realpath -m ./rewrite.yml)" ]]; then
ROOT_RECIPE=$(python3 -c 'import os; print(os.path.realpath("rewrite.yml"))')
if [[ "$RECIPE_FILE" != "$ROOT_RECIPE" ]]; then
cp "$RECIPE_FILE" ./rewrite.yml
COPIED_RECIPE=true
fi
Expand All @@ -80,9 +102,3 @@ echo "Executing OpenRewrite recipe: $RECIPE_NAME"

echo "Running code formatters to fix Checkstyle line-length and import ordering..."
./gradlew spotlessApply javaIncrementalFormatApply

# Clean up temporary files
rm "$INIT_SCRIPT"
if [[ "$COPIED_RECIPE" == true ]]; then
rm ./rewrite.yml
fi
41 changes: 16 additions & 25 deletions .agents/skills/pr-polisher/SKILL.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
name: pr-polisher
description: >-
Automated pre-flight checklist to polish PRs. Use this before declaring a task
or PR complete to automatically verify license headers, commit hygiene,
formatting, and codebase mandates. Don't use for non-Git changelists.
Automated pre-flight checklist to polish local PR commits. Use this before
declaring a task or commit complete to verify license headers, commit
hygiene, formatting, tests, and codebase mandates. Don't use for reading or
responding to GitHub/Reviewable PR reviews (use pr-review) or non-Git
changelists.
---

# PR Polisher
Expand All @@ -24,24 +26,22 @@ Do not ask for permission. Do not wait for the user to remind you. Run the full
python3 .agents/skills/pr-polisher/scripts/check_diff.py
```

2. **Run Formatting Validation**
Always run the project's formatting tools to ensure checkstyle and Google Java Format passes.
2. **Apply Formatting Fixes If Step 1 Reports Errors**
Step 1 (`check_diff.py`) already runs `./gradlew spotlessCheck javaIncrementalFormatCheck` (`spotless` checks `**/*.gradle`; `javaIncrementalFormat` runs Google Java Format and import sorting on changed Java lines). If it fails, apply fixes and re-run Checkstyle:
```bash
./gradlew spotlessCheck javaIncrementalFormatCheck
# OR if formatting is needed:
./gradlew spotlessApply javaIncrementalFormatApply
```

3. **Run Presubmits and Compilation**
Ensure that the project builds correctly and all presubmit checks pass. Use scoped builds and pass `-x :console-webapp:buildConsoleWebapp` when only modifying backend Java code to save time, avoid modifying `console-webapp/package-lock.json`, and prevent Angular CLI Node.js version mismatch failures.
3. **Run Presubmits, Compilation, Checkstyle, and Tests**
Ensure presubmits, compilation, Checkstyle (`checkstyleMain`/`checkstyleTest`), and tests pass. Use scoped compile targets for backend-only changes to avoid Docker image builds (`:core:buildToolImage`) and unnecessary `console-webapp` `npmInstallDeps` runs (which can dirty `console-webapp/package-lock.json` or fail on host Node.js mismatches):
```bash
# Run presubmits
# Run presubmits (also executes check_diff_test.py and release script tests)
./gradlew runPresubmits

# Verify compilation (use a scoped build if you only modified one module, e.g., :core)
./gradlew :core:build -x test -x :console-webapp:buildConsoleWebapp
# Run standard test suite if modifying core
./gradlew :core:standardTest -x :console-webapp:buildConsoleWebapp
# Verify backend compilation & Checkstyle (add :<module>:compileJava for sibling modules)
./gradlew :core:compileJava :core:compileTestJava :core:checkstyleMain :core:checkstyleTest
# Run standard test suite if modifying core (add -x :console-webapp:buildConsoleWebapp if running :core:test)
./gradlew :core:standardTest
```

4. **Verify PR Scope and Extraneous Files (Line-by-Line Inspection)**
Expand All @@ -68,19 +68,10 @@ Do not ask for permission. Do not wait for the user to remind you. Run the full
8. **Verify State-Machine and Serialization Hygiene**
If the diff adds or modifies lifecycle fields (`Status`/`State`/`Stage` enums, nullable timestamps, `TimedTransitionProperty`, Netty handlers, cron/Cloud Tasks pipelines, or Angular view/edit/loading booleans):
* Confirm multi-step states and transitions are explicit and centralized (Java 25 `sealed` projections + transitioners, `ImmutableMultimap` transition maps, or Angular Signal discriminated unions via `@switch`) rather than scattered `if` guards, ordinal `Enum.compareTo`, or orthogonal booleans.
* Confirm no redundant persisted static `state` column is added to wall-clock time-projected entities (`Domain`, `Host`), and no non-`transient` field is added, removed, reordered, or retyped on Protostuff-cached entities (`EppResource`, `DomainBase`, `Domain`, `DomainTransferData`, `GracePeriod`, `DomainDsData`, `HostBase`, `Host`) without a coordinated `@Tag` or cache key-prefix migration.
* Confirm no redundant persisted static `state` column is added to wall-clock time-projected entities (`Domain`, `Host`), and no non-`transient` field is added, removed, reordered, or retyped on Protostuff-cached entities (`UpdateAutoTimestampEntity`, `EppResource`, `DomainBase`, `Domain`, `BaseTransferObject`, `DomainTransferData`, `GracePeriodBase`, `GracePeriod`, `DomainDsDataBase`, `DomainDsData`, `HostBase`, `Host`) without a coordinated `@Tag` or cache key-prefix migration.
* Confirm async pipelines have explicit failure states, return retryable HTTP codes (`503`, not `204`) when Cloud Tasks must retry, and keep external HTTP/email side effects outside DB transactions.

9. **Read ALL Existing PR Review Comments (Paginated)**
When an open PR exists, never rely on a single unpaginated `gh api` or `gh pr view` call, which silently truncates and hides inline comments. Fetch all three endpoints with pagination:
```bash
gh api --paginate repos/google/nomulus/pulls/<n>/comments --jq '.[] | {path, line, user: .user.login, body}'
gh api --paginate repos/google/nomulus/issues/<n>/comments --jq '.[] | {user: .user.login, body}'
gh api --paginate repos/google/nomulus/pulls/<n>/reviews --jq '.[] | {user: .user.login, state, body}'
```
Confirm every comment is addressed (or explicitly replied to) before declaring the PR polished.

10. **Address Errors, Amend, and Re-Run (Iterative Checking)**
9. **Address Errors, Amend, and Re-Run (Iterative Checking)**
If any script throws an error, if scope was reduced, if the commit message was inaccurate, or if formatting changes were applied, you must stage those fixes and amend your commit:
```bash
git add -u
Expand Down
Loading
Loading