Gatekeeper loop

From fritzprix/libr-agent · View interactive version

Gatekeeper loop is an AI agent skill from the GitHub repository fritzprix/libr-agent. Reviewer loop paired with worker-loop. Each tick reviews the user''s open PRs, posts a structured verdict comment that directs the worker (changes requested with numbered findings), re-reviews after the worker responds, and iterates until a final Approve verdict — never merging. Use when the user ty

How to use this skill

Skill file

Gatekeeper Loop

The reviewer half of a two-agent loop. worker-loop writes code; gatekeeper-loop reviews it and talks to the worker only through PR comments:

worker opens PR ─► gatekeeper verdict (changes-requested, G-items)
      ▲                              │
      └── worker fixes / declines, ◄─┘
          posts response
                 ... until verdict=approve (+ CI green) ─► human merges

Worker and gatekeeper share one GitHub account, so GitHub's Approve / Request changes buttons are unavailable. Verdicts are PR comments with a hidden marker plus a gatekeeper:* label. Read references/comment-format.md before posting the first verdict.

Invocation

| Command | Behavior |

| -------------------------------- | -------------------------------------------- |

| /gatekeeper-loop | Run one tick now. |

| /gatekeeper-loop 10m | Arm a recurring loop, then run one tick now. |

| /gatekeeper-loop 10m --pr 2202 | Review only one PR. |

| /gatekeeper-loop stop | Kill the loop process. |

Arm exactly like worker-loop (Cursor loop skill mechanics): check pgrep -af AGENT_LOOP_TICK_gatekeeper_loop first, then a background shell while true; do sleep 600; echo 'AGENT_LOOP_TICK_gatekeeper_loop {"prompt":"/gatekeeper-loop"}'; done with notify_on_output pattern ^AGENT_LOOP_TICK_gatekeeper_loop. Record the PID in state.

Run it in a separate agent chat from worker-loop. Reviewer independence matters: a session that wrote the code should not judge it.

State

.libragent/work/gatekeeper-loop/state.json (git-ignored, resolved via git rev-parse --git-common-dir or $LOOP_STATE_DIR): loopPid, issueValidation (validated and rejected counts per producer task), and per PR the open findings (G2.1: open|fixed|declined-accepted|disputed) and dispute counts. A per-PR maxRounds is only a cache of the effective round limit (default 5, raised by +2 each time the repository owner removes gatekeeper:escalated on GitHub — never edit maxRounds by hand). GitHub comments and timeline events remain the source of truth; state only speeds up re-review. "Reviewed clean, waiting for CI" is not state-only: it is recorded as a verdict=clean marker comment (see comment-format.md), so a missing or lost state.json never causes a full re-review.

Tick procedure

0. Skills checkout

python3 .agents/skills/loop-shared/scripts/skills_freshness.py

Exit 1 stops the tick: merge or reset .agents/skills to match origin/<dev base> before reviewing.

1. Queue

python3 .agents/skills/gatekeeper-loop/scripts/review_queue.py [--pr N]

The script output is already sorted: priority 0 first (worker response, new head, rebase, owner comment, base change, CI failure after approve), then never-reviewed PRs, oldest first. Process every status: review PR in that order, at most 5 per tick; leftovers carry over and keep their priority.

Other statuses:

2. Review (read-only)

Do not check out branches in $MAIN or in the worker worktree. Read through git objects in read-only mode:

git fetch origin <head> --quiet
gh pr view N --json body,title,baseRefName      # claims + linked issue
gh pr view N --comments                         # full conversation (required on escalate/resume)
gh issue view <linked> --comments               # acceptance criteria + owner notes
gh pr diff N                                     # round 1: full diff
git diff <reviewedHead>..origin/<head>           # round ≥ 2: incremental diff
git show origin/<head>:<path>                    # surrounding code

If rebased: true (reviewedHead is not an ancestor of head, or no longer exists), a plain reviewedHead..head diff mixes in base-branch changes. Use git range-diff <reviewedBase>...<reviewedHead> <newBase>...origin/<head> when the old head is still available, otherwise do a full review.

ownerComments lists unmarked owner/@me comments posted after the last gatekeeper marker (worker response markers do not hide them). Treat each as a required item: answer it in the next verdict (as a G item, or in ### Checked if already satisfied). Apply references/review-rubric.md.

Check build and test results in CI; do not run them locally. However, CI does not replace code review (diff, design, acceptance criteria satisfaction, edge cases). Perform code review directly regardless of CI status. If CI failed, analyze failure logs and include root cause and fix guidance in findings — see references/pre-post-checklist.md § CI (be skeptical of worker explanations; timeout bumps on pre-existing tests need log proof and cross-PR reconciliation). Use the dedicated gatekeeper worktree only when local verification is strictly required (never run pnpm refactor:validate):

MAIN=$(git rev-parse --show-toplevel)
WT="$MAIN/../$(basename "$MAIN").worktrees/gatekeeper"
python3 -c "import subprocess, sys; from pathlib import Path; out = subprocess.run(['git', '-C', sys.argv[1], 'worktree', 'list', '--porcelain'], capture_output=True, text=True).stdout; target = Path(sys.argv[2]).resolve(); sys.exit(0 if any(line.startswith('worktree ') and Path(line[9:].strip()).resolve() == target for line in out.splitlines()) else 1)" "$MAIN" "$WT" \
  || git -C "$MAIN" worktree add --detach "$WT" "origin/<head>"
git -C "$WT" checkout --detach --force "origin/<head>" && git -C "$WT" clean -fd

Keep it between ticks so build caches stay warm; run pnpm install --frozen-lockfile there only when the lockfile changed. Local runs never lead to commits.

Queue output includes securitySensitivePolicyPaths (shell policy, tool approval, permissions, secrets, sandbox paths), securitySensitiveBehaviorHints (broader path hints such as API routes, executors, API-key modules), and the combined securitySensitive list. Path hits are only a first pass. Also read the diff for security-sensitive behavior: sending API keys or bearer tokens over HTTP, new auth or secret-prompt surfaces, credential storage, sandbox/approval policy changes — even when no path matched. For any policy hit, behavior hint, or behavior you classify as security-sensitive, add gatekeeper:needs-human, and an approve is only valid if ### Checked lists the concrete bypass scenarios you tried (e.g. quoting, env, symlink, path traversal) and why each is blocked. Never auto-approve them silently.

On round ≥ 2, read the worker response first and settle every previous G item: fixed (verify in the diff), decline accepted, or disputed (restate once; a second dispute on the same item → escalate).

3. Verdict

Quality bar (all must hold for `clean` / `approve`)
  1. Score ≥ 86/100 on the code-quality rubric (references/review-rubric.md). Score honestly; never inflate to cross the gate.
  2. Zero open required G items (blocker or major).
  3. No technical debt in the diff: no copy-pasted logic, dead code, unlinked TODO/FIXME/workarounds, unneeded fallbacks or compatibility shims, duplicated state/IDs, or lint/type suppressions.
  4. Production-path tests: new or changed behavior is tested through the real modules, and existing tests for changed logic are updated.
  5. Acceptance criteria met: every criterion of the linked issue maps to file:line evidence.
  6. Project rules: agents.md / CLAUDE.md hard rules (no as any or unsafe casts, KISS/YAGNI, pnpm rust:* only).
  7. For approve only: CI green at the reviewed head on every OS job, the PR targets dev/* or main, and it is mergeable.
Rejection criteria (any one → `changes-requested`)

| Criterion | Example |

| ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |

| Score ≤ 85 | G items name the lowest-scoring categories and the change that raises each |

| Wrong behavior or a logic bug | edge/null/error path broken, data loss |

| Security or privacy problem | secret in logs, approval/sandbox bypass, unvalidated untrusted input |

| Acceptance criteria not met | linked issue checklist item missing |

| Missing or fake tests | new logic untested, tests only hit stubs or duplicated logic |

| Technical debt in the diff | items in quality bar 3; each one is a G with a concrete refactor (debt outside the diff gets a §8 follow-up issue instead) |

| Silent failure handling | swallowed errors, fallbacks that hide failures |

| Scope violation | unrelated files, drive-by refactors, formatter churn |

| Project rule violation | as any, raw cargo, protected-path edits outside scope |

| CI failure caused by the PR | diagnosed from the logs with a fix |

Minor/nit issues alone do not reject a PR that meets the quality bar; list them as S suggestions.

Decide:

Include the mandatory 100-point code-quality breakdown in every verdict comment (### Code quality score (gate: ≥86 to approve) in references/comment-format.md, rubric in references/review-rubric.md). Per-item one-line rationales must cite file:line evidence. The score gates the verdict (≤ 85 → changes-requested); use references/pre-post-checklist.md § score calibration so totals spread across the range.

Before posting, complete references/pre-post-checklist.md: re-fetch headRefOid (abort if it moved), refuse duplicate head+verdict markers, run the severity table (major vs minor), file any §8 follow-up issues yourself and link them in the comment, and when upgrading clean → approve at the same head, align the approve body with the latest clean marker (especially ### Checked and suggestions).

Post the verdict with gh pr comment N --body-file <tmp> using the exact marker format (head= is the full headSha you reviewed, base= the PR's current base branch), then swap labels:

# changes-requested:
gh pr edit N --add-label gatekeeper:changes-requested --remove-label gatekeeper:approved,gatekeeper:escalated

# approve:
gh pr edit N --add-label gatekeeper:approved --remove-label gatekeeper:changes-requested,gatekeeper:escalated

# clean, await CI:
gh pr edit N --remove-label gatekeeper:changes-requested

4. Validate one producer issue

After the PRs, validate at most one issue filed by producer-loop (label auto:producer, no validated or rejected label):

python3 .agents/skills/gatekeeper-loop/scripts/issue_validation.py next

NONE means there is nothing to validate. Otherwise read the issue and its full comment thread (gh issue view N --comments; issue_validation.py next also includes comments with author, date, and body) and check the finding against the current code on origin/<dev base> (git show, git grep), read-only. This chat must not be the chat that filed the issue: validation needs a fresh session.

Write the reason with file:line evidence to a scratch file outside the repository, then:

python3 .agents/skills/gatekeeper-loop/scripts/issue_validation.py judge N --verdict validated|rejected --reason-file <file>

The script appends the reason to the issue body, adds the label, and counts the verdict per producer task under issueValidation in state. A human then decides whether to add approved; worker-loop takes only approved issues.

5. Report

One line per open PR using each entry's turnReport (verdict, reviewed head, current head, whose turn; fix labelMismatch before posting). Only the gatekeeper applies labelFix — workers never edit gatekeeper:* labels. For PRs you reviewed this tick, add round, required-finding count, and code-quality total (e.g. 72/100). Add one line for a validated or rejected producer issue (link and verdict).

Tests

cd .agents/skills/loop-shared/scripts && python3 -m unittest discover
cd .agents/skills/gatekeeper-loop/scripts && python3 -m unittest discover

Machine safety

Reviews and follow-up issues stay inside these rules (#2824). Do not ask a worker to break them:

  1. Never kill a process the run did not start. No Stop-Process, taskkill, kill, or pkill selected by the image name libragent or libragent-tui. Cleanup stops only recorded PIDs and their children.
  2. Smoke and hidden-WebView runs use an isolated LIBRAGENT_DATA_DIR. They do not share the user's database, port file, log, or WebView data. A same-profile coexistence test starts its own GUI.
  3. When acceptance is blocked by a defect outside the issue, the worker stops, links the blocker, and moves on. Do not request workaround loops.
  4. Readiness evidence is a fresh run marker, or log lines written after the run started. The shared app log is not proof.
  5. Evidence for a specific code path must force that path and assert it.
  6. Prefer a debug or incremental build. Repeated release rebuilds with no new information are a stop signal.
  7. An owner stop or no-push note ends the item immediately.

Hard rules

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.