Stale patch reconciliation

From AtlasOmnia/hermes-custom-pack · View interactive version

Stale patch reconciliation is an AI agent skill from the GitHub repository AtlasOmnia/hermes-custom-pack. stale-patch-reconciliation — Reconcile a stale patch/diff against a current checkout.

How to use this skill

Skill file

Stale Patch Reconciliation

Use when a patch/diff must be compared with, repaired against, or refreshed for a moving checkout — "is this patch still needed", "does upstream already contain it", "what changed since it was written", "reconcile X.diff with HEAD", or "make apply-patches pass again".

This skill has two explicit modes:

Deliverable shape: (1) behavioral invariant, (2) exact gaps/call sites, (3) upstream status, (4) drift cause, (5) minimal repair, (6) focused gate evidence, and (7) artifact/applicator verification. Coordinate writer ownership through shared-worktree-agent-orchestration.

Core sequence

  1. Read the full patch first. Paginate the whole diff; note every file touched and the old-file line numbers of each hunk. The hunks are the map for later drift attribution.
  1. Git history archaeology — does the feature exist anywhere?
  1. Snapshot-branch trap. If git merge-base HEAD <branch> returns EMPTY output, the branch is likely built on a parentless snapshot commit. Confirm with git log --format='%h parents: %P' -1 <sha> — no parents = root/snapshot commit (e.g. "chore: snapshot upstream main for private feature review"). Such a branch shares NO ancestry with HEAD, so lineage tools mislead; treat the branch's cumulative diff as the feature and ignore ancestry.
  1. Quantify staleness read-only. git apply --check <patch> validates without applying (safe). Per-file apply map:

```bash

for f in $(grep '^diff --git' <patch> | awk '{print $3}' | cut -d/ -f2-); do

git apply --check --include="$f" <patch> >/dev/null 2>&1 && echo "OK $f" || echo "FAIL $f"

done

```

CAUTION: exit code after a pipe reflects the last command (head), not git apply — read the error text, not $?. Files whose hunks apply cleanly today are "context-compatible"; failed files are where the rework lives.

  1. Attribute drift. git log --oneline <branch-or-sha>..HEAD -- <paths> lists upstream commits since the branch point. Read the ones touching failing files to explain each failed hunk (parser renames, schema field additions, UI refactors). Report drift as "hunk N fails because upstream renamed X → Y" — that is the minimal-plan input.
  1. Read current call sites. Grep current line numbers for every function the patch touches and compare signatures against the patch's expectations. Note which upstream refactors the reimplementation must target (e.g. new parser API, new CommandDef fields).
  1. Live-checkout concurrency (shared worktrees). The tree can change MID-analysis — a concurrent writer may implement the very feature being reconciled. Re-verify git status --porcelain and re-grep at the END; report a final snapshot with "may already be stale" on line numbers. Distinguish pre-existing dirty files from concurrent-writer edits so the parent doesn't lose work.

Repair mode: isolated three-way reconciliation

When the user authorizes implementation or the applicator must be repaired:

  1. Freeze ownership before mutation. Recheck Git root, branch, HEAD, complete status, OS writers/descendants, and any waiting launcher. Record pre-existing dirty files as protected. A controller that has not yet produced a report may still have mutated the tree; perform a delayed post-launch status scan before trusting its preflight.
  2. Create a clean detached worktree from the current HEAD. Never trial-apply a stale patch directly to a dirty authoritative checkout. Use git apply --3way --index <patch> there; inspect every git diff --name-only --diff-filter=U conflict and preserve current upstream behavior plus the patch's invariant. Do not resolve conflicts by taking all of ours or all of theirs.
  3. Treat conflict placement as suspect. Three-way application can match a weak context anchor inside an existing helper or test class. For additive tests, rebuild from the current HEAD file and insert only the patch's added block at a stable semantic anchor; then run the test file. This avoids duplicated commands, missing delimiters, and tests nested inside another test.
  4. Run focused gates in the isolated tree. Start with git diff --check, language syntax/type checks, and the smallest RED→GREEN tests for each invariant. Use the project's real dependency environment; if a temporary worktree lacks dependencies, reify from the lockfile in that worktree (offline when the cache is complete) rather than mutating the live checkout. Report unavailable tests as unavailable, never as passing.
  5. Regenerate the artifact for the current baseline. From a clean current-HEAD worktree, produce each patch with an explicit reviewed file allowlist. Do not include unrelated dirty files, generated output, dependencies, or broad historical branch deltas. Keep the refreshed artifact outside the source checkout when necessary.
  6. Prove applicator behavior twice. git apply --check against a clean current-HEAD worktree must pass; git apply -R --check against the repaired tree must also pass. Run the real applicator once and require zero missing/non-clean patches. Re-run it only after inspecting a failure; never loop the same command under set -e or through a pipeline that masks the true exit code.
  7. Close out independently. Re-scan the live process tree, verify the authoritative checkout contains only the intended feature changes plus protected pre-existing dirt, run final focused gates at the same tree, and report exact paths, hashes, exit codes, and any deferred lifecycle gate separately.

Pitfalls

Reference

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.