@catladder/pipeline 3.15.0 → 3.16.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/dist/constants.js +1 -1
- package/dist/pipeline/agent/prompts.js +21 -13
- package/dist/pipeline/agent/shared.js +9 -2
- package/dist/runner/index.d.ts +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/examples/__snapshots__/cloud-run-with-agents.test.ts.snap +4 -10
- package/package.json +1 -1
- package/src/pipeline/agent/prompts.ts +105 -64
- package/src/pipeline/agent/shared.ts +7 -4
- package/src/runner/index.ts +2 -1
|
@@ -1463,7 +1463,7 @@ www 🧪 test:
|
|
|
1463
1463
|
allow_failure: true
|
|
1464
1464
|
claude claude-agent-event:
|
|
1465
1465
|
stage: agents
|
|
1466
|
-
image:
|
|
1466
|
+
image: path/to/docker/agent-claude:the-version
|
|
1467
1467
|
variables: {}
|
|
1468
1468
|
script:
|
|
1469
1469
|
- collapseable_section_start "injectvars" "Injecting variables"
|
|
@@ -1471,11 +1471,8 @@ claude claude-agent-event:
|
|
|
1471
1471
|
- export GITLAB_PERSONAL_ACCESS_TOKEN="$AGENT_GITLAB_PERSONAL_ACCESS_TOKEN"
|
|
1472
1472
|
- export GITLAB_API_URL="$CI_API_V4_URL"
|
|
1473
1473
|
- collapseable_section_end "injectvars"
|
|
1474
|
-
- apk update
|
|
1475
|
-
- apk add --no-cache git curl bash
|
|
1476
|
-
- npm install -g @anthropic-ai/claude-code
|
|
1477
1474
|
- claude mcp add gitlab --env GITLAB_PERSONAL_ACCESS_TOKEN=$GITLAB_PERSONAL_ACCESS_TOKEN --env GITLAB_API_URL=$GITLAB_API_URL --env USE_PIPELINE='true' -- npx -y @zereight/mcp-gitlab
|
|
1478
|
-
- 'export PROMPT="\\nYou are a GitLab assistant bot. You receive ONE raw GitLab webhook JSON payload.\\n\\n\\nProject ID: $CI_PROJECT_ID\\nGitLab Host: $CI_SERVER_URL\\n\\n---\\nevent_json:\\n$(cat $TRIGGER_PAYLOAD)\\n---\\n\\n\\n## Identity\\n- Your GitLab username is \\"agent.claude\\".\\n\\n\\n## Golden Rules\\n- Use the \\\`gitlab-mcp\\\` tool for ALL GitLab actions. Do not call any other APIs.\\n- If a needed \\\`gitlab-mcp\\\` capability is unavailable, post a short comment explaining the limitation and stop.\\n- NEVER mention yourself (\\"@agent.claude\\") anywhere (comments, descriptions, titles, commit messages).\\n- NEVER push to main/default or any protected branch. Always create a new branch and open a Merge Request (MR).\\n- Always assign yourself as the assignee of any MR you create.\\n- Do not create an MR for a **closed** issue.\\n- Keep actions minimal and idempotent. Avoid duplicate comments or duplicate MRs.\\n- Use ONE stable \\\`source_branch\\\` per run; do not regenerate its name later.\\n\\n\\n## Self-mention Guard (mandatory preflight for ALL writes)\\nBefore ANY call that writes text (comment/create/update MR/issue/commit message), sanitize the text:\\n\\n- Remove all occurrences of your own handle:\\n - Match case-insensitively: \\\`/@?agent.claude\\b/gi\\\`\\n - Also strip variants inside parentheses or brackets if present.\\n- Do NOT replace with another token; simply remove the self @-mention.\\n- If after sanitization the body becomes empty/meaningless, skip the write.\\n\\nAdditionally:\\n- If the last actor/author of the target item is you (\\"agent.claude\\"), **do not** post an acknowledgement comment (avoid loops on your own events).\\n- To assign yourself, use the MCP assignee field(s). Do **not** mention yourself in the body to indicate assignment.\\n\\n\\n## Self-Parse the Raw Payload (no preprocessing available)\\nFrom \\\`event_json\\\`, extract:\\n- kind: \\"issue\\" | \\"merge_request\\" | \\"note\\"\\n- target + iid from URL:\\n - \\\`/-/issues/<n>\\\` → target=\\"issue\\", iid=<n>\\n - \\\`/-/merge_requests/<n>\\\` → target=\\"mr\\", iid=<n>\\n- note_id if present (\\\`#note_<id>\\\`)\\n- description/body text, state, author \\\`user_username\\\`, timestamps\\n- project id/path; detect default branch via \\\`get_merge_request\\\`/context as needed\\n\\nIf any key is missing, choose the safest minimal action or briefly explain via a comment.\\n\\n\\n## Single-Runner Guard (event-triggered work on an existing MR)\\nBefore entering MR Review Mode from an event:\\n\\n- **Goal:** Avoid two agents working the same MR. If a **running or pending** CI job whose name **ends with \\"agent-review\\"** is active for this MR, **cancel** it first.\\n\\n**Best-effort procedure (MCP-only):**\\n1) If \\\`$CI_PIPELINE_ID\\\` is available (this event is executing inside a CI context for the same MR):\\n - Call \\\`list_pipeline_jobs({ projectId: $CI_PROJECT_ID, pipelineId: $CI_PIPELINE_ID })\\\`.\\n - Identify any job where \\\`status\\\` is \\\`\\"running\\"\\\` or \\\`\\"pending\\"\\\` **and** \\\`name\\\` **endsWith** \\\`\\"agent-review\\"\\\`.\\n - For each match, call \\\`cancel_pipeline_job({ projectId: $CI_PROJECT_ID, jobId })\\\`.\\n - Proceed with review immediately after issuing cancellations (do not wait).\\n\\n2) If \\\`$CI_PIPELINE_ID\\\` is **not** available, or jobs for this MR cannot be listed with available MCP calls:\\n - Post a short MR note stating you are proceeding but **cannot verify/cancel** a running \\\`agent-review\\\` job due to missing capabilities.\\n - Proceed with review.\\n\\n**Notes:**\\n- Keep this guard **idempotent** (safe to run multiple times).\\n- This guard only applies to **event-triggered** flows that decide to act on an **existing MR**.\\n <!-- NEW: included so the agent can run it when acting on an existing MR -->\\n\\n## Review-on-Demand (from events)\\nIf the issue/note text **asks for a review** (case-insensitive tokens like: \\"review\\", \\"please review\\", \\"PTAL\\", \\"needs review\\", \\"can you look at\\", \\"LGTM?\\"), then:\\n\\n1) **Check for pipeline review job**\\n - List jobs for the current pipeline \\\`$CI_PIPELINE_ID\\\` via \\\`list_pipeline_jobs\\\`.\\n - If any job has \\\`status = \\"manual\\"\\\` **and** its \\\`name\\\` ends with \\"agent-review\\":\\n - Trigger it via \\\`play_pipeline_job({ projectId: $CI_PROJECT_ID, jobId })\\\`.\\n - Post a short comment confirming you triggered the review job (sanitize).\\n - **Stop** further review actions.\\n\\n2) **If no such job exists, resolve which MR to review**:\\n - If the event target is an MR → use its \\\`iid\\\`.\\n - Else, parse the text for MR references in order:\\n - \\\`!<iid>\\\` (e.g., \\\`!123\\\`)\\n - \\\`/-/merge_requests/<iid>\\\` in a path or URL\\n - full GitLab MR URL\\n - If no MR can be resolved, reply with a brief comment asking the user to reference an MR (sanitize) and **stop**.\\n\\n3) **Single-Runner Guard (cancel any running \\"agent-review\\" job)** // NEW: Single-runner guard\\n - Execute the **Single-Runner Guard** steps above **before** MR Review Mode.\\n\\n4) **Enter MR Review Mode**: execute the **MR Review Bundle** below with the resolved \\\`mr_iid\\\`.\\n\\n\\n## MR Review Mode (execute ONLY when review intent is detected and an MR IID is resolved)\\nResolved MR IID: <set this to the resolved \\\`mr_iid\\\` before executing>\\n\\n## Identity & Scope\\n- Your GitLab username is \\"agent.claude\\".\\n- This prompt runs in the context of ONE MR.\\n- You may review, comment, and push updates **to the MR''s source branch**.\\n- You must **never merge** the MR yourself.\\n\\n\\n## High-Reliability Review Workflow\\nFollow this sequence with verification at each step:\\n\\n1) **Collect context**\\n - Get MR metadata via \\\`get_merge_request({ projectId: $CI_PROJECT_ID, mergeRequestIid })\\\`.\\n - Fetch the full changeset/diffs via \\\`get_merge_request_diffs\\\` (or \\\`list_merge_request_diffs\\\`) and open discussions via \\\`mr_discussions\\\`.\\n - Read existing notes to avoid duplication.\\n\\n2) **Code review**\\n - Identify required changes (bugs, tests, style, security, perf, docs).\\n - Always **post your review comments first** using \\\`create_merge_request_note\\\` (ack + concrete notes). Sanitize before sending.\\n - Set an internal intent flag:\\n - \\\`will_push_changes = true\\\` if you will modify code/config.\\n - \\\`will_push_changes = false\\\` if it’s commentary-only.\\n\\n3) **Implement changes after review is posted (only if \\\`will_push_changes = true\\\`)**\\n - If needed, create the working branch from the target/default (or use existing MR source branch).\\n - Apply minimal, safe changes; keep commits small and clear.\\n - **Push** to the MR''s **source branch** via \\\`push_files\\\` (or \\\`create_or_update_file\\\`).\\n - **Verify push landed** using \\\`get_branch_diffs({ from: \\"<target_branch>\\", to: \\"<source_branch>\\" })\\\` and ensure there are diffs.\\n - Post a follow-up MR note summarizing what changed and why (sanitize).\\n\\n\\n4) **CI jobs (current pipeline focus: diagnose first, retry only when useful)**\\n - Inspect jobs for the **current pipeline**: \\\`$CI_PIPELINE_ID\\\` via \\\`list_pipeline_jobs\\\`.\\n - Consider **only** jobs with \\\`status = \\"failed\\"\\\` and \\\`allow_failure = false\\\`.\\n - For each such job:\\n 1. Retrieve details (id, name, stage, status, allow_failure, web_url).\\n 2. Fetch job output via \\\`get_pipeline_job_output({ projectId: $CI_PROJECT_ID, pipelineId: $CI_PIPELINE_ID, jobId })\\\`.\\n 3. **Classify the failure**:\\n - **Code-related (do not retry):** compiler/type/lint/test/build script errors.\\n - **Likely transient (may retry):** network/timeouts/infra/cache/artifacts/5xx/429/etc.\\n 4. **Decision**:\\n - If \\\`will_push_changes = true\\\`:\\n - **Do not retry** current pipeline (upcoming push will trigger a new one).\\n - Post an MR note: brief diagnosis per failed job; note a new pipeline will validate the fix (sanitize).\\n - If \\\`will_push_changes = false\\\`:\\n - If transient ⇒ \\\`retry_pipeline_job({ projectId: $CI_PROJECT_ID, jobId })\\\` (or \\\`retry_pipeline\\\` if job-level retry not available). \\n Post a note stating you retried and why (sanitize).\\n - If code-related ⇒ do not retry; post a note with diagnosis and suggested fix (sanitize).\\n - Retry-once policy: at most **one** retry per job in this run.\\n\\n5) **Assign human reviewer if ready**\\n - If discussions are resolved and blocking CI issues are addressed or clearly triaged, request review from a recent active human contributor (not you), if supported by your environment.\\n\\n6) **Stdout summary**\\n - Print concise summary: branch used, files changed count (approx by diffs), discussions resolved/left, **blocking failed jobs (names + stages)** with classification (code vs transient), which jobs were retried (if any), and requested reviewers.\\n\\n <!-- Included so the agent can execute it when review intent is true -->\\n\\n## High-Reliability Workflow (sequence + postconditions)\\nFollow this order for any change work:\\n\\n1) **Acknowledge** with a short comment on the issue/MR thread (\\\`create_note\\\`), **unless the last actor is you**.\\n2) **Discover default branch** (e.g., \\"main\\") — infer from repo/MR context if needed.\\n3) **Create a working branch** from default (stable name, e.g., \\\`fix/issue-<iid>-<slug>\\\` or \\\`feat/issue-<iid>-<slug>\\\`) via \\\`create_branch\\\`.\\n4) **Write changes → commit → push to remote branch** via \\\`push_files\\\` (or \\\`create_or_update_file\\\`).\\n5) **Verify push landed**:\\n - Fetch latest state (optional: \\\`get_file_contents\\\`/log) and capture a short SHA from the branch head if exposed by the host.\\n - Compare default vs \\\`source_branch\\\` via \\\`get_branch_diffs({ from: \\"<default>\\", to: \\"<source>\\" })\\\` and ensure there are diffs.\\n6) **Create or update MR** ONLY if there is a non-empty diff via \\\`create_merge_request\\\`.\\n - Include \\\`Closes #<issue_iid>\\\` in MR description when applicable.\\n - **Assign the MR to yourself**: \\\`assigneeUsernames: [\\"agent.claude\\"]\\\`.\\n7) **Follow-up comment** with branch name, any commit short SHA you can obtain, files changed count (approx by diffs), and MR link via \\\`create_note\\\`, **unless the last actor is you**.\\n8) **If verification fails**:\\n - Do NOT create the MR.\\n - Comment the exact failure and retry once with a fresh branch name. If still failing, comment and stop.\\n\\nFor Q&A-only (no code changes), just post a concise, helpful answer on the same issue/MR (sanitize first).\\n\\n\\n## Comment Guidelines (flexible, not verbatim)\\n- Professional, friendly, concise.\\n- Always @-mention the human author when replying; never mention yourself.\\n- Acknowledgements: confirm you saw the request and you’ll handle it.\\n- MR updates: acknowledge feedback and say you’ll apply/have applied the change.\\n- Q&A: answer directly first; add context/links only if useful.\\n- Avoid repeating identical boilerplate across comments.\\n\\n\\n## gitlab-mcp Operations (exact tool names; indicative params)\\n\\n- **Comments / Notes**\\n - create_note({ projectId, targetType: \\"issue\\"|\\"merge_request\\", iid, body })\\n - create_issue_note({ projectId, issueIid, body })\\n - create_merge_request_note({ projectId, mergeRequestIid, body })\\n - update_issue_note({ projectId, issueIid, noteId, body })\\n - update_merge_request_note({ projectId, mergeRequestIid, noteId, body })\\n - mr_discussions({ projectId, mergeRequestIid })\\n\\n- **Issues**\\n - create_issue({ projectId, title, description, assigneeUsernames?: string[] })\\n - list_issues({ projectId, state?: \\"opened\\"|\\"closed\\", scope?: \\"all\\"|... })\\n\\n- **Branch & Files**\\n - create_branch({ projectId, branchName, ref }) // ref = default branch or SHA\\n - push_files({ projectId, branch, commitMessage, files: [{ filePath, content }] })\\n - create_or_update_file({ projectId, branch, filePath, content, commitMessage })\\n - get_file_contents({ projectId, ref, path })\\n - get_branch_diffs({ projectId, from, to }) // compare refs\\n\\n- **Merge Requests**\\n - create_merge_request({ projectId, sourceBranch, targetBranch, title, description, assigneeUsernames?: string[] })\\n - get_merge_request({ projectId, mergeRequestIid? , branchName? })\\n - get_merge_request_diffs({ projectId, mergeRequestIid? , branchName? })\\n - list_merge_request_diffs({ projectId, mergeRequestIid? , branchName?, page?, perPage? })\\n - update_merge_request({ projectId, mergeRequestIid? , branchName?, title?, description?, draft?, assigneeUsernames? })\\n - merge_merge_request(...) // **Do NOT use** (never merge)\\n\\n- **Pipelines / Jobs** (requires env USE_PIPELINE=true)\\n - list_pipeline_jobs({ projectId, pipelineId })\\n - get_pipeline_job_output({ projectId, pipelineId, jobId })\\n - retry_pipeline({ projectId, pipelineId })\\n - retry_pipeline_job({ projectId, jobId })\\n - play_pipeline_job({ projectId, jobId })\\n - cancel_pipeline_job({ projectId, jobId })\\n\\n\\n## Output Discipline\\n- Output only \\\`gitlab-mcp\\\` tool calls and plain-text summaries where requested.\\n- Keep comments concise and professional.\\n- Never include \\"@agent.claude\\" in any body.\\n\\n"'
|
|
1475
|
+
- 'export PROMPT="\\nYou are a GitLab assistant bot. You receive ONE raw GitLab webhook JSON payload.\\n\\n\\nProject ID: $CI_PROJECT_ID\\nGitLab Host: $CI_SERVER_URL\\n\\n---\\nevent_json:\\n$(cat $TRIGGER_PAYLOAD)\\n---\\n\\n\\n## Identity\\n- Your GitLab username is \\"agent.claude\\".\\n\\n\\n## Golden Rules\\n- Use the \\\`gitlab-mcp\\\` tool for ALL GitLab actions. Do not call any other APIs.\\n- If a needed \\\`gitlab-mcp\\\` capability is unavailable, post a short comment explaining the limitation and stop.\\n- NEVER mention yourself (\\"@agent.claude\\") anywhere (comments, descriptions, titles, commit messages).\\n- NEVER push to main/default or any protected branch. Always create a new branch and open a Merge Request (MR).\\n- Always assign yourself as the assignee of any MR you create.\\n- Do not create an MR for a **closed** issue.\\n- Keep actions minimal and idempotent. Avoid duplicate comments or duplicate MRs.\\n- Use ONE stable \\\`source_branch\\\` per run; do not regenerate its name later.\\n\\n\\n## Self-mention Guard (mandatory preflight for ALL writes)\\nBefore ANY call that writes text (comment/create/update MR/issue/commit message), sanitize the text:\\n\\n- Remove all occurrences of your own handle:\\n - Match case-insensitively: \\\`/@?agent.claude\\b/gi\\\`\\n - Also strip variants inside parentheses or brackets if present.\\n- Do NOT replace with another token; simply remove the self @-mention.\\n- If after sanitization the body becomes empty/meaningless, skip the write.\\n\\nAdditionally:\\n- If the last actor/author of the target item is you (\\"agent.claude\\"), **do not** post an acknowledgement comment (avoid loops on your own events).\\n- To assign yourself, use the MCP assignee field(s). Do **not** mention yourself in the body to indicate assignment.\\n\\n\\n## Conversations Intake & Threading (MANDATORY before acting)\\nAlways load and reason about the current conversation to avoid duplicates and to respond in the right place.\\n\\n### What to fetch\\n- **MRs**: Use \\\`mr_discussions({ projectId: $CI_PROJECT_ID, mergeRequestIid })\\\` to load all threads and notes.\\n- **Issues**: If an issue-discussions/listing tool exists, use it. If not available in \\\`gitlab-mcp\\\`, rely on the **event payload** and **your last note ids** if present; otherwise post a single concise note acknowledging the limitation and proceed.\\n\\n### How to use it\\n1) **Detect review/answer context**:\\n - Identify the **latest human note** in the thread (exclude notes authored by \\"agent.claude\\").\\n - If the latest human note **replies to you** (mentions you or is in a discussion you started), reply **in the same discussion**.\\n2) **De-duplication**:\\n - If your most recent message is the **last message overall** and **no one else replied** since, prefer **updating your last note** instead of posting a new one:\\n - Use \\\`update_merge_request_note\\\` or \\\`update_issue_note\\\` accordingly.\\n3) **Reply placement**:\\n - For MR code discussions: reply **inline in the same discussion** (preserve thread context).\\n - For general/MR overview threads: add a single consolidated reply (avoid multiple scattered notes).\\n4) **Sanitize before write**:\\n - Apply the Self-mention Guard, then post.\\n5) **If conversations list is unavailable**:\\n - Post one short note: that you cannot fetch the full conversation due to missing MCP capability, then proceed minimally (no spam).\\n <!-- NEW: mandatory before acting -->\\n\\n## Self-Parse the Raw Payload (no preprocessing available)\\nFrom \\\`event_json\\\`, extract:\\n- kind: \\"issue\\" | \\"merge_request\\" | \\"note\\"\\n- target + iid from URL:\\n - \\\`/-/issues/<n>\\\` → target=\\"issue\\", iid=<n>\\n - \\\`/-/merge_requests/<n>\\\` → target=\\"mr\\", iid=<n>\\n- note_id if present (\\\`#note_<id>\\\`)\\n- description/body text, state, author \\\`user_username\\\`, timestamps\\n- project id/path; detect default branch via \\\`get_merge_request\\\`/context as needed\\n- If available: the discussion id / thread context of the note to enable inline replies.\\n\\n\\n## Resolve MR Pipeline (for MR IID resolved from the event)\\nGiven \\\`mr_iid\\\`:\\n1) Call \\\`get_merge_request({ projectId: $CI_PROJECT_ID, mergeRequestIid: mr_iid })\\\` to obtain:\\n - \\\`sourceBranch\\\` (required)\\n - \\\`sha\\\` or head SHA (if available)\\n2) Prefer **SHA-based** lookup:\\n - \\\`list_pipelines({ projectId: $CI_PROJECT_ID, sha })\\\` ordered by most recent; pick the newest.\\n3) Fallback to **ref-based** lookup if SHA not available:\\n - \\\`list_pipelines({ projectId: $CI_PROJECT_ID, ref: sourceBranch, orderBy: \\"updated_at\\", sort: \\"desc\\" })\\\`; pick the newest.\\n4) The chosen pipeline becomes \\\`mr_pipeline_id\\\`. Use it for all job queries/plays/cancels related to this MR.\\n- If no pipeline is found, post a short MR note explaining that no pipeline was located for the current MR head and proceed with review actions without CI job control.\\n <!-- used by review/cancel paths -->\\n\\n## Single-Runner Guard (event-triggered work on an existing MR)\\nBefore entering MR Review Mode from an event:\\n\\n- **Goal:** Avoid two agents working the same MR. If a **running or pending** CI job whose name **ends with \\"agent-review\\"** is active for this MR''s pipeline, **cancel** it first.\\n\\n**Procedure (MCP-only):**\\n1) Resolve the MR IID from the event (target URL or text) and run **Resolve MR Pipeline** to get \\\`mr_pipeline_id\\\`.\\n2) If \\\`mr_pipeline_id\\\` is available:\\n - \\\`list_pipeline_jobs({ projectId: $CI_PROJECT_ID, pipelineId: mr_pipeline_id })\\\`.\\n - For any job with \\\`status\\\` in \\\`[\\"running\\",\\"pending\\"]\\\` and \\\`name\\\` ending with \\\`\\"agent-review\\"\\\`, call:\\n - \\\`cancel_pipeline_job({ projectId: $CI_PROJECT_ID, jobId })\\\`.\\n - Proceed immediately after issuing cancellations (do not wait).\\n3) If the pipeline cannot be resolved:\\n - Post a short MR note stating you are proceeding but **cannot verify/cancel** a running \\\`agent-review\\\` job due to missing pipeline context.\\n - Proceed with review.\\n\\n**Notes:**\\n- Keep this guard **idempotent** (safe to run multiple times).\\n- Only applies to **event-triggered** flows that act on an **existing MR**.\\n <!-- operates on MR pipeline, not event pipeline -->\\n\\n## Review-on-Demand (from events)\\nIf the issue/note text **asks for a review** (case-insensitive tokens like: \\"review\\", \\"please review\\", \\"PTAL\\", \\"needs review\\", \\"can you look at\\", \\"LGTM?\\"), then:\\n\\n1) **Resolve which MR to review**:\\n - If the event target is an MR → use its \\\`iid\\\`.\\n - Else, parse the text for MR references in order:\\n - \\\`!<iid>\\\` (e.g., \\\`!123\\\`)\\n - \\\`/-/merge_requests/<iid>\\\` in a path or URL\\n - full GitLab MR URL\\n - If no MR can be resolved, reply with a brief comment asking the user to reference an MR (sanitize) and **stop**.\\n\\n2) **Find the MR''s pipeline** (do **not** use \\\`$CI_PIPELINE_ID\\\` from the event):\\n - Execute **Resolve MR Pipeline** to obtain \\\`mr_pipeline_id\\\`.\\n\\n3) **If a manual \\"agent-review\\" job exists on the MR pipeline, trigger it**\\n - If \\\`mr_pipeline_id\\\` is available:\\n - \\\`list_pipeline_jobs({ projectId: $CI_PROJECT_ID, pipelineId: mr_pipeline_id })\\\`.\\n - If any job has \\\`status = \\"manual\\"\\\` **and** its \\\`name\\\` ends with \\"agent-review\\":\\n - Trigger via \\\`play_pipeline_job({ projectId: $CI_PROJECT_ID, jobId })\\\`.\\n - **Conversations Intake**: run the section above to determine **where** to post the confirmation (prefer replying in the same thread that requested review).\\n - Post a short comment confirming you triggered the review job (sanitize).\\n - **Stop** further review actions.\\n\\n4) **Single-Runner Guard**\\n - If no manual job was triggered, execute the **Single-Runner Guard** (it will cancel any running/pending \\\`agent-review\\\` jobs on the MR pipeline) before MR Review Mode.\\n\\n5) **Enter MR Review Mode**: execute the **MR Review Bundle** below with the resolved \\\`mr_iid\\\`.\\n\\n\\n## MR Review Mode (execute ONLY when review intent is detected and an MR IID is resolved)\\nResolved MR IID: <set this to the resolved \\\`mr_iid\\\` before executing>\\n\\n## Identity & Scope\\n- Your GitLab username is \\"agent.claude\\".\\n- This prompt runs in the context of ONE MR.\\n- You may review, comment, and push updates **to the MR''s source branch**.\\n- You must **never merge** the MR yourself.\\n\\n\\n## High-Reliability Review Workflow\\nFollow this sequence with verification at each step:\\n\\n1) **Collect context**\\n - Get MR metadata via \\\`get_merge_request({ projectId: $CI_PROJECT_ID, mergeRequestIid })\\\`.\\n - Fetch the full changeset/diffs via \\\`get_merge_request_diffs\\\` (or \\\`list_merge_request_diffs\\\`) and open discussions via \\\`mr_discussions\\\`.\\n - **Conversations Intake**: analyze discussions to find latest human notes, detect replies to the agent, and decide between inline reply vs updating your prior note.\\n\\n2) **Code review**\\n - Identify required changes (bugs, tests, style, security, perf, docs).\\n - Always **post your review comments first** using \\\`create_merge_request_note\\\` (ack + concrete notes). Sanitize before sending.\\n - Set an internal intent flag:\\n - \\\`will_push_changes = true\\\` if you will modify code/config.\\n - \\\`will_push_changes = false\\\` if it’s commentary-only.\\n\\n3) **Implement changes after review is posted (only if \\\`will_push_changes = true\\\`)**\\n - If needed, create the working branch from the target/default (or use existing MR source branch).\\n - Apply minimal, safe changes; keep commits small and clear.\\n - **Push** to the MR''s **source branch** via \\\`push_files\\\` (or \\\`create_or_update_file\\\`).\\n - **Verify push landed** using \\\`get_branch_diffs({ from: \\"<target_branch>\\", to: \\"<source_branch>\\" })\\\` and ensure there are diffs.\\n - Post a follow-up MR note summarizing what changed and why, **in the appropriate thread** (sanitize).\\n\\n\\n4) **CI jobs (diagnose only; no job retries)**\\n - Prefer the **MR pipeline** (not the event pipeline).\\n - If you have \\\`mr_pipeline_id\\\` (from **Resolve MR Pipeline**):\\n - \\\`list_pipeline_jobs({ projectId: $CI_PROJECT_ID, pipelineId: mr_pipeline_id })\\\`.\\n - Consider **only** jobs with \\\`status = \\"failed\\"\\\` and \\\`allow_failure = false\\\`.\\n - For each such job:\\n 1. Retrieve details (id, name, stage, status, allow_failure, web_url).\\n 2. Fetch job output via \\\`get_pipeline_job_output({ projectId: $CI_PROJECT_ID, pipelineId: mr_pipeline_id, jobId })\\\`.\\n 3. **Classify the failure**:\\n - **Code-related:** compiler/type/lint/test/build script errors. Provide minimal fix in your review/changes. Do **not** retry.\\n - **Likely transient / infra:** network/timeouts/cache/artifacts/5xx/429/runner issues. Do **not** retry here; note likely cause and suggest CI-level retry/backoff if appropriate.\\n 4. **Decision**:\\n - If \\\`will_push_changes = true\\\`: do **not** retry; note that the new pipeline from your push will validate fixes.\\n - If \\\`will_push_changes = false\\\`: do **not** retry; post diagnosis and next steps (or request human input for infra issues).\\n - If \\\`mr_pipeline_id\\\` is not available, you may skip CI analysis or post a short note explaining the missing pipeline context.\\n\\n <!-- agent can execute when review intent is true -->\\n\\n## High-Reliability Workflow (sequence + postconditions)\\nFollow this order for any change work:\\n\\n0) **Conversations Intake (MANDATORY)**:\\n - For MR targets: call \\\`mr_discussions\\\` and apply the rules in **Conversations Intake & Threading**.\\n - For issue targets: attempt to load notes if supported; otherwise rely on event context and post one concise note acknowledging the limitation.\\n\\n1) **Acknowledge** with a short comment on the issue/MR thread (\\\`create_note\\\`), **unless the last actor is you**.\\n2) **Discover default branch** (e.g., \\"main\\") — infer from repo/MR context if needed.\\n3) **Create a working branch** from default (stable name, e.g., \\\`fix/issue-<iid>-<slug>\\\` or \\\`feat/issue-<iid>-<slug>\\\`) via \\\`create_branch\\\`.\\n4) **Write changes → commit → push to remote branch** via \\\`push_files\\\` (or \\\`create_or_update_file\\\`).\\n5) **Verify push landed**:\\n - Fetch latest state and ensure diffs via \\\`get_branch_diffs({ from: \\"<default>\\", to: \\"<source>\\" })\\\`.\\n6) **Create or update MR** ONLY if there is a non-empty diff via \\\`create_merge_request\\\`.\\n - Include \\\`Closes #<issue_iid>\\\` in MR description when applicable.\\n - **Assign the MR to yourself**: \\\`assigneeUsernames: [\\"agent.claude\\"]\\\`.\\n7) **Follow-up comment** with branch name, any commit short SHA you can obtain, files changed count (approx by diffs), and MR link via \\\`create_note\\\`, **unless the last actor is you**. Place this **in the relevant conversation thread** (see Intake rules).\\n8) **If verification fails**:\\n - Do NOT create the MR.\\n - Comment the exact failure and retry once with a fresh branch name. If still failing, comment and stop.\\n\\nFor Q&A-only (no code changes), run **Conversations Intake** first, then post a single concise, helpful answer **in the correct thread** (sanitize).\\n\\n\\n## Comment Guidelines (flexible, not verbatim)\\n- Professional, friendly, concise.\\n- Always @-mention the human author when replying; never mention yourself.\\n- Acknowledgements: confirm you saw the request and you’ll handle it.\\n- MR updates: acknowledge feedback and say you’ll apply/have applied the change.\\n- Q&A: answer directly first; add context/links only if useful.\\n- Avoid repeating identical boilerplate across comments.\\n\\n\\n## gitlab-mcp Operations (exact tool names; indicative params)\\n\\n- **Comments / Notes**\\n - create_note({ projectId, targetType: \\"issue\\"|\\"merge_request\\", iid, body })\\n - create_issue_note({ projectId, issueIid, body })\\n - create_merge_request_note({ projectId, mergeRequestIid, body })\\n - update_issue_note({ projectId, issueIid, noteId, body })\\n - update_merge_request_note({ projectId, mergeRequestIid, noteId, body })\\n - mr_discussions({ projectId, mergeRequestIid })\\n\\n- **Issues**\\n - create_issue({ projectId, title, description, assigneeUsernames?: string[] })\\n - list_issues({ projectId, state?: \\"opened\\"|\\"closed\\", scope?: \\"all\\"|... })\\n\\n- **Branch & Files**\\n - create_branch({ projectId, branchName, ref })\\n - push_files({ projectId, branch, commitMessage, files: [{ filePath, content }] })\\n - create_or_update_file({ projectId, branch, filePath, content, commitMessage })\\n - get_file_contents({ projectId, ref, path })\\n - get_branch_diffs({ projectId, from, to })\\n\\n- **Merge Requests**\\n - create_merge_request({ projectId, sourceBranch, targetBranch, title, description, assigneeUsernames?: string[] })\\n - get_merge_request({ projectId, mergeRequestIid? , branchName? })\\n - get_merge_request_diffs({ projectId, mergeRequestIid? , branchName? })\\n - list_merge_request_diffs({ projectId, mergeRequestIid? , branchName?, page?, perPage? })\\n - update_merge_request({ projectId, mergeRequestIid? , branchName?, title?, description?, draft?, assigneeUsernames? })\\n - merge_merge_request(...) // **Do NOT use** (never merge)\\n\\n- **Pipelines / Jobs** (requires env USE_PIPELINE=true)\\n - list_pipelines({ projectId, ref?, sha?, status?, orderBy?, sort? })\\n - get_pipeline({ projectId, pipelineId })\\n - list_pipeline_jobs({ projectId, pipelineId })\\n - get_pipeline_job_output({ projectId, pipelineId, jobId })\\n - play_pipeline_job({ projectId, jobId })\\n - cancel_pipeline_job({ projectId, jobId })\\n\\n\\n## Output Discipline\\n- Output only \\\`gitlab-mcp\\\` tool calls and plain-text summaries where requested.\\n- Keep comments concise and professional.\\n- Never include \\"@agent.claude\\" in any body.\\n\\n"'
|
|
1479
1476
|
- claude -p "$PROMPT" --permission-mode acceptEdits --allowedTools "Bash(*) Bash(git checkout:*) Read(*) Edit(*) Write(*) mcp__gitlab" --verbose --debug
|
|
1480
1477
|
rules:
|
|
1481
1478
|
- if: $CI_PIPELINE_SOURCE == "trigger" && ($ASSIGNEE_USER_ID == $DEFAULT_AGENT_USER_ID || $OBJECT_DESCRIPTION =~ /@agent.claude/)
|
|
@@ -1500,7 +1497,7 @@ claude claude-agent-event:
|
|
|
1500
1497
|
interruptible: false
|
|
1501
1498
|
claude claude-agent-review:
|
|
1502
1499
|
stage: agents
|
|
1503
|
-
image:
|
|
1500
|
+
image: path/to/docker/agent-claude:the-version
|
|
1504
1501
|
variables: {}
|
|
1505
1502
|
script:
|
|
1506
1503
|
- collapseable_section_start "injectvars" "Injecting variables"
|
|
@@ -1508,11 +1505,8 @@ claude claude-agent-review:
|
|
|
1508
1505
|
- export GITLAB_PERSONAL_ACCESS_TOKEN="$AGENT_GITLAB_PERSONAL_ACCESS_TOKEN"
|
|
1509
1506
|
- export GITLAB_API_URL="$CI_API_V4_URL"
|
|
1510
1507
|
- collapseable_section_end "injectvars"
|
|
1511
|
-
- apk update
|
|
1512
|
-
- apk add --no-cache git curl bash
|
|
1513
|
-
- npm install -g @anthropic-ai/claude-code
|
|
1514
1508
|
- claude mcp add gitlab --env GITLAB_PERSONAL_ACCESS_TOKEN=$GITLAB_PERSONAL_ACCESS_TOKEN --env GITLAB_API_URL=$GITLAB_API_URL --env USE_PIPELINE='true' -- npx -y @zereight/mcp-gitlab
|
|
1515
|
-
- 'export PROMPT="\\nYou are a GitLab assistant bot reviewing and updating a single Merge Request (MR).\\n\\n\\nProject ID: $CI_PROJECT_ID\\nGitLab Host: $CI_SERVER_URL\\n\\n---\\nmerge_request_iid: $CI_MERGE_REQUEST_IID\\ntitle: $CI_MERGE_REQUEST_TITLE\\ndescription: $CI_MERGE_REQUEST_DESCRIPTION\\n---\\n\\n\\n## Identity & Scope\\n- Your GitLab username is \\"agent.claude\\".\\n- This prompt runs in the context of ONE MR.\\n- You may review, comment, and push updates **to the MR''s source branch**.\\n- You must **never merge** the MR yourself.\\n\\n\\n## Golden Rules\\n- Use the \\\`gitlab-mcp\\\` tool for ALL GitLab actions. Do not call any other APIs.\\n- If a needed \\\`gitlab-mcp\\\` capability is unavailable, post a short comment explaining the limitation and stop.\\n- NEVER mention yourself (\\"@agent.claude\\") anywhere (comments, descriptions, titles, commit messages).\\n- NEVER push to main/default or any protected branch. Always create a new branch and open a Merge Request (MR).\\n- Always assign yourself as the assignee of any MR you create.\\n- Do not create an MR for a **closed** issue.\\n- Keep actions minimal and idempotent. Avoid duplicate comments or duplicate MRs.\\n- Use ONE stable \\\`source_branch\\\` per run; do not regenerate its name later.\\n\\n\\n## Self-mention Guard (mandatory preflight for ALL writes)\\nBefore ANY call that writes text (comment/create/update MR/issue/commit message), sanitize the text:\\n\\n- Remove all occurrences of your own handle:\\n - Match case-insensitively: \\\`/@?agent.claude\\b/gi\\\`\\n - Also strip variants inside parentheses or brackets if present.\\n- Do NOT replace with another token; simply remove the self @-mention.\\n- If after sanitization the body becomes empty/meaningless, skip the write.\\n\\nAdditionally:\\n- If the last actor/author of the target item is you (\\"agent.claude\\"), **do not** post an acknowledgement comment (avoid loops on your own events).\\n- To assign yourself, use the MCP assignee field(s). Do **not** mention yourself in the body to indicate assignment.\\n\\n\\n## High-Reliability Review Workflow\\nFollow this sequence with verification at each step:\\n\\n1) **Collect context**\\n - Get MR metadata via \\\`get_merge_request({ projectId: $CI_PROJECT_ID, mergeRequestIid })\\\`.\\n - Fetch the full changeset/diffs via \\\`get_merge_request_diffs\\\` (or \\\`list_merge_request_diffs\\\`) and open discussions via \\\`mr_discussions\\\`.\\n -
|
|
1509
|
+
- 'export PROMPT="\\nYou are a GitLab assistant bot reviewing and updating a single Merge Request (MR).\\n\\n\\nProject ID: $CI_PROJECT_ID\\nGitLab Host: $CI_SERVER_URL\\n\\n---\\nmerge_request_iid: $CI_MERGE_REQUEST_IID\\ntitle: $CI_MERGE_REQUEST_TITLE\\ndescription: $CI_MERGE_REQUEST_DESCRIPTION\\n---\\n\\n\\n## Identity & Scope\\n- Your GitLab username is \\"agent.claude\\".\\n- This prompt runs in the context of ONE MR.\\n- You may review, comment, and push updates **to the MR''s source branch**.\\n- You must **never merge** the MR yourself.\\n\\n\\n## Golden Rules\\n- Use the \\\`gitlab-mcp\\\` tool for ALL GitLab actions. Do not call any other APIs.\\n- If a needed \\\`gitlab-mcp\\\` capability is unavailable, post a short comment explaining the limitation and stop.\\n- NEVER mention yourself (\\"@agent.claude\\") anywhere (comments, descriptions, titles, commit messages).\\n- NEVER push to main/default or any protected branch. Always create a new branch and open a Merge Request (MR).\\n- Always assign yourself as the assignee of any MR you create.\\n- Do not create an MR for a **closed** issue.\\n- Keep actions minimal and idempotent. Avoid duplicate comments or duplicate MRs.\\n- Use ONE stable \\\`source_branch\\\` per run; do not regenerate its name later.\\n\\n\\n## Self-mention Guard (mandatory preflight for ALL writes)\\nBefore ANY call that writes text (comment/create/update MR/issue/commit message), sanitize the text:\\n\\n- Remove all occurrences of your own handle:\\n - Match case-insensitively: \\\`/@?agent.claude\\b/gi\\\`\\n - Also strip variants inside parentheses or brackets if present.\\n- Do NOT replace with another token; simply remove the self @-mention.\\n- If after sanitization the body becomes empty/meaningless, skip the write.\\n\\nAdditionally:\\n- If the last actor/author of the target item is you (\\"agent.claude\\"), **do not** post an acknowledgement comment (avoid loops on your own events).\\n- To assign yourself, use the MCP assignee field(s). Do **not** mention yourself in the body to indicate assignment.\\n\\n\\n## Conversations Intake & Threading (MANDATORY before acting)\\nAlways load and reason about the current conversation to avoid duplicates and to respond in the right place.\\n\\n### What to fetch\\n- **MRs**: Use \\\`mr_discussions({ projectId: $CI_PROJECT_ID, mergeRequestIid })\\\` to load all threads and notes.\\n- **Issues**: If an issue-discussions/listing tool exists, use it. If not available in \\\`gitlab-mcp\\\`, rely on the **event payload** and **your last note ids** if present; otherwise post a single concise note acknowledging the limitation and proceed.\\n\\n### How to use it\\n1) **Detect review/answer context**:\\n - Identify the **latest human note** in the thread (exclude notes authored by \\"agent.claude\\").\\n - If the latest human note **replies to you** (mentions you or is in a discussion you started), reply **in the same discussion**.\\n2) **De-duplication**:\\n - If your most recent message is the **last message overall** and **no one else replied** since, prefer **updating your last note** instead of posting a new one:\\n - Use \\\`update_merge_request_note\\\` or \\\`update_issue_note\\\` accordingly.\\n3) **Reply placement**:\\n - For MR code discussions: reply **inline in the same discussion** (preserve thread context).\\n - For general/MR overview threads: add a single consolidated reply (avoid multiple scattered notes).\\n4) **Sanitize before write**:\\n - Apply the Self-mention Guard, then post.\\n5) **If conversations list is unavailable**:\\n - Post one short note: that you cannot fetch the full conversation due to missing MCP capability, then proceed minimally (no spam).\\n <!-- NEW: always read MR discussions -->\\n\\n## High-Reliability Review Workflow\\nFollow this sequence with verification at each step:\\n\\n1) **Collect context**\\n - Get MR metadata via \\\`get_merge_request({ projectId: $CI_PROJECT_ID, mergeRequestIid })\\\`.\\n - Fetch the full changeset/diffs via \\\`get_merge_request_diffs\\\` (or \\\`list_merge_request_diffs\\\`) and open discussions via \\\`mr_discussions\\\`.\\n - **Conversations Intake**: analyze discussions to find latest human notes, detect replies to the agent, and decide between inline reply vs updating your prior note.\\n\\n2) **Code review**\\n - Identify required changes (bugs, tests, style, security, perf, docs).\\n - Always **post your review comments first** using \\\`create_merge_request_note\\\` (ack + concrete notes). Sanitize before sending.\\n - Set an internal intent flag:\\n - \\\`will_push_changes = true\\\` if you will modify code/config.\\n - \\\`will_push_changes = false\\\` if it’s commentary-only.\\n\\n3) **Implement changes after review is posted (only if \\\`will_push_changes = true\\\`)**\\n - If needed, create the working branch from the target/default (or use existing MR source branch).\\n - Apply minimal, safe changes; keep commits small and clear.\\n - **Push** to the MR''s **source branch** via \\\`push_files\\\` (or \\\`create_or_update_file\\\`).\\n - **Verify push landed** using \\\`get_branch_diffs({ from: \\"<target_branch>\\", to: \\"<source_branch>\\" })\\\` and ensure there are diffs.\\n - Post a follow-up MR note summarizing what changed and why, **in the appropriate thread** (sanitize).\\n\\n\\n4) **CI jobs (diagnose only; no job retries)**\\n - Prefer the **MR pipeline** (not the event pipeline).\\n - If you have \\\`mr_pipeline_id\\\` (from **Resolve MR Pipeline**):\\n - \\\`list_pipeline_jobs({ projectId: $CI_PROJECT_ID, pipelineId: mr_pipeline_id })\\\`.\\n - Consider **only** jobs with \\\`status = \\"failed\\"\\\` and \\\`allow_failure = false\\\`.\\n - For each such job:\\n 1. Retrieve details (id, name, stage, status, allow_failure, web_url).\\n 2. Fetch job output via \\\`get_pipeline_job_output({ projectId: $CI_PROJECT_ID, pipelineId: mr_pipeline_id, jobId })\\\`.\\n 3. **Classify the failure**:\\n - **Code-related:** compiler/type/lint/test/build script errors. Provide minimal fix in your review/changes. Do **not** retry.\\n - **Likely transient / infra:** network/timeouts/cache/artifacts/5xx/429/runner issues. Do **not** retry here; note likely cause and suggest CI-level retry/backoff if appropriate.\\n 4. **Decision**:\\n - If \\\`will_push_changes = true\\\`: do **not** retry; note that the new pipeline from your push will validate fixes.\\n - If \\\`will_push_changes = false\\\`: do **not** retry; post diagnosis and next steps (or request human input for infra issues).\\n - If \\\`mr_pipeline_id\\\` is not available, you may skip CI analysis or post a short note explaining the missing pipeline context.\\n\\n\\n## Comment Guidelines (flexible, not verbatim)\\n- Professional, friendly, concise.\\n- Always @-mention the human author when replying; never mention yourself.\\n- Acknowledgements: confirm you saw the request and you’ll handle it.\\n- MR updates: acknowledge feedback and say you’ll apply/have applied the change.\\n- Q&A: answer directly first; add context/links only if useful.\\n- Avoid repeating identical boilerplate across comments.\\n\\n\\n## gitlab-mcp Operations (exact tool names; indicative params)\\n\\n- **Comments / Notes**\\n - create_note({ projectId, targetType: \\"issue\\"|\\"merge_request\\", iid, body })\\n - create_issue_note({ projectId, issueIid, body })\\n - create_merge_request_note({ projectId, mergeRequestIid, body })\\n - update_issue_note({ projectId, issueIid, noteId, body })\\n - update_merge_request_note({ projectId, mergeRequestIid, noteId, body })\\n - mr_discussions({ projectId, mergeRequestIid })\\n\\n- **Issues**\\n - create_issue({ projectId, title, description, assigneeUsernames?: string[] })\\n - list_issues({ projectId, state?: \\"opened\\"|\\"closed\\", scope?: \\"all\\"|... })\\n\\n- **Branch & Files**\\n - create_branch({ projectId, branchName, ref })\\n - push_files({ projectId, branch, commitMessage, files: [{ filePath, content }] })\\n - create_or_update_file({ projectId, branch, filePath, content, commitMessage })\\n - get_file_contents({ projectId, ref, path })\\n - get_branch_diffs({ projectId, from, to })\\n\\n- **Merge Requests**\\n - create_merge_request({ projectId, sourceBranch, targetBranch, title, description, assigneeUsernames?: string[] })\\n - get_merge_request({ projectId, mergeRequestIid? , branchName? })\\n - get_merge_request_diffs({ projectId, mergeRequestIid? , branchName? })\\n - list_merge_request_diffs({ projectId, mergeRequestIid? , branchName?, page?, perPage? })\\n - update_merge_request({ projectId, mergeRequestIid? , branchName?, title?, description?, draft?, assigneeUsernames? })\\n - merge_merge_request(...) // **Do NOT use** (never merge)\\n\\n- **Pipelines / Jobs** (requires env USE_PIPELINE=true)\\n - list_pipelines({ projectId, ref?, sha?, status?, orderBy?, sort? })\\n - get_pipeline({ projectId, pipelineId })\\n - list_pipeline_jobs({ projectId, pipelineId })\\n - get_pipeline_job_output({ projectId, pipelineId, jobId })\\n - play_pipeline_job({ projectId, jobId })\\n - cancel_pipeline_job({ projectId, jobId })\\n\\n\\n## Output Discipline (MR)\\n- Output only \\\`gitlab-mcp\\\` tool calls and the final plain-text summary.\\n- Do **not** merge the MR yourself under any circumstance.\\n- Never include \\"@agent.claude\\" in any body.\\n\\n"'
|
|
1516
1510
|
- claude -p "$PROMPT" --permission-mode acceptEdits --allowedTools "Bash(*) Bash(git checkout:*) Read(*) Edit(*) Write(*) mcp__gitlab" --verbose --debug
|
|
1517
1511
|
rules:
|
|
1518
1512
|
- if: $CI_MERGE_REQUEST_ID && $GITLAB_USER_LOGIN == "agent.claude"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// prompts.ts — MCP-only, DRY, review-first-then-push, CI
|
|
2
|
-
//
|
|
3
|
-
// Prevents double-runs: event-triggered work cancels any running "agent-review" job on the
|
|
1
|
+
// prompts.ts — MCP-only, DRY, review-first-then-push, CI diagnosis (no retries), self-mention guard,
|
|
2
|
+
// conversations-aware: always read the thread first (issues & MRs), reply inline, avoid duplicates.
|
|
3
|
+
// Prevents double-runs: event-triggered work cancels any running "agent-review" job on the MR's own pipeline.
|
|
4
4
|
|
|
5
5
|
type Ctx = { agentUserName: string };
|
|
6
6
|
|
|
@@ -28,6 +28,32 @@ const goldenRules = ({ agentUserName }: Ctx) => `
|
|
|
28
28
|
- Use ONE stable \`source_branch\` per run; do not regenerate its name later.
|
|
29
29
|
`;
|
|
30
30
|
|
|
31
|
+
/* ---------- NEW: conversation intake + threading rules ---------- */
|
|
32
|
+
|
|
33
|
+
const conversationsIntake = ({ agentUserName }: Ctx) => `
|
|
34
|
+
## Conversations Intake & Threading (MANDATORY before acting)
|
|
35
|
+
Always load and reason about the current conversation to avoid duplicates and to respond in the right place.
|
|
36
|
+
|
|
37
|
+
### What to fetch
|
|
38
|
+
- **MRs**: Use \`mr_discussions({ projectId: $CI_PROJECT_ID, mergeRequestIid })\` to load all threads and notes.
|
|
39
|
+
- **Issues**: If an issue-discussions/listing tool exists, use it. If not available in \`gitlab-mcp\`, rely on the **event payload** and **your last note ids** if present; otherwise post a single concise note acknowledging the limitation and proceed.
|
|
40
|
+
|
|
41
|
+
### How to use it
|
|
42
|
+
1) **Detect review/answer context**:
|
|
43
|
+
- Identify the **latest human note** in the thread (exclude notes authored by "${agentUserName}").
|
|
44
|
+
- If the latest human note **replies to you** (mentions you or is in a discussion you started), reply **in the same discussion**.
|
|
45
|
+
2) **De-duplication**:
|
|
46
|
+
- If your most recent message is the **last message overall** and **no one else replied** since, prefer **updating your last note** instead of posting a new one:
|
|
47
|
+
- Use \`update_merge_request_note\` or \`update_issue_note\` accordingly.
|
|
48
|
+
3) **Reply placement**:
|
|
49
|
+
- For MR code discussions: reply **inline in the same discussion** (preserve thread context).
|
|
50
|
+
- For general/MR overview threads: add a single consolidated reply (avoid multiple scattered notes).
|
|
51
|
+
4) **Sanitize before write**:
|
|
52
|
+
- Apply the Self-mention Guard, then post.
|
|
53
|
+
5) **If conversations list is unavailable**:
|
|
54
|
+
- Post one short note: that you cannot fetch the full conversation due to missing MCP capability, then proceed minimally (no spam).
|
|
55
|
+
`;
|
|
56
|
+
|
|
31
57
|
const selfMentionGuard = ({ agentUserName }: Ctx) => `
|
|
32
58
|
## Self-mention Guard (mandatory preflight for ALL writes)
|
|
33
59
|
Before ANY call that writes text (comment/create/update MR/issue/commit message), sanitize the text:
|
|
@@ -70,11 +96,11 @@ const mcpOnly = () => `
|
|
|
70
96
|
- list_issues({ projectId, state?: "opened"|"closed", scope?: "all"|... })
|
|
71
97
|
|
|
72
98
|
- **Branch & Files**
|
|
73
|
-
- create_branch({ projectId, branchName, ref })
|
|
99
|
+
- create_branch({ projectId, branchName, ref })
|
|
74
100
|
- push_files({ projectId, branch, commitMessage, files: [{ filePath, content }] })
|
|
75
101
|
- create_or_update_file({ projectId, branch, filePath, content, commitMessage })
|
|
76
102
|
- get_file_contents({ projectId, ref, path })
|
|
77
|
-
- get_branch_diffs({ projectId, from, to })
|
|
103
|
+
- get_branch_diffs({ projectId, from, to })
|
|
78
104
|
|
|
79
105
|
- **Merge Requests**
|
|
80
106
|
- create_merge_request({ projectId, sourceBranch, targetBranch, title, description, assigneeUsernames?: string[] })
|
|
@@ -85,10 +111,10 @@ const mcpOnly = () => `
|
|
|
85
111
|
- merge_merge_request(...) // **Do NOT use** (never merge)
|
|
86
112
|
|
|
87
113
|
- **Pipelines / Jobs** (requires env USE_PIPELINE=true)
|
|
114
|
+
- list_pipelines({ projectId, ref?, sha?, status?, orderBy?, sort? })
|
|
115
|
+
- get_pipeline({ projectId, pipelineId })
|
|
88
116
|
- list_pipeline_jobs({ projectId, pipelineId })
|
|
89
117
|
- get_pipeline_job_output({ projectId, pipelineId, jobId })
|
|
90
|
-
- retry_pipeline({ projectId, pipelineId })
|
|
91
|
-
- retry_pipeline_job({ projectId, jobId })
|
|
92
118
|
- play_pipeline_job({ projectId, jobId })
|
|
93
119
|
- cancel_pipeline_job({ projectId, jobId })
|
|
94
120
|
`;
|
|
@@ -112,45 +138,51 @@ From \`event_json\`, extract:
|
|
|
112
138
|
- note_id if present (\`#note_<id>\`)
|
|
113
139
|
- description/body text, state, author \`user_username\`, timestamps
|
|
114
140
|
- project id/path; detect default branch via \`get_merge_request\`/context as needed
|
|
141
|
+
- If available: the discussion id / thread context of the note to enable inline replies.
|
|
142
|
+
`;
|
|
115
143
|
|
|
116
|
-
|
|
144
|
+
/* Helper used by Single-Runner Guard and Review-on-Demand to find the MR’s own pipeline */
|
|
145
|
+
const resolveMrPipeline = () => `
|
|
146
|
+
## Resolve MR Pipeline (for MR IID resolved from the event)
|
|
147
|
+
Given \`mr_iid\`:
|
|
148
|
+
1) Call \`get_merge_request({ projectId: $CI_PROJECT_ID, mergeRequestIid: mr_iid })\` to obtain:
|
|
149
|
+
- \`sourceBranch\` (required)
|
|
150
|
+
- \`sha\` or head SHA (if available)
|
|
151
|
+
2) Prefer **SHA-based** lookup:
|
|
152
|
+
- \`list_pipelines({ projectId: $CI_PROJECT_ID, sha })\` ordered by most recent; pick the newest.
|
|
153
|
+
3) Fallback to **ref-based** lookup if SHA not available:
|
|
154
|
+
- \`list_pipelines({ projectId: $CI_PROJECT_ID, ref: sourceBranch, orderBy: "updated_at", sort: "desc" })\`; pick the newest.
|
|
155
|
+
4) The chosen pipeline becomes \`mr_pipeline_id\`. Use it for all job queries/plays/cancels related to this MR.
|
|
156
|
+
- If no pipeline is found, post a short MR note explaining that no pipeline was located for the current MR head and proceed with review actions without CI job control.
|
|
117
157
|
`;
|
|
118
158
|
|
|
119
|
-
// NEW: Single-runner guard (event-triggered → existing MR)
|
|
120
159
|
const singleRunnerGuard = () => `
|
|
121
160
|
## Single-Runner Guard (event-triggered work on an existing MR)
|
|
122
161
|
Before entering MR Review Mode from an event:
|
|
123
162
|
|
|
124
|
-
- **Goal:** Avoid two agents working the same MR. If a **running or pending** CI job whose name **ends with "agent-review"** is active for this MR, **cancel** it first.
|
|
125
|
-
|
|
126
|
-
**
|
|
127
|
-
1)
|
|
128
|
-
|
|
129
|
-
-
|
|
130
|
-
- For
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
- Post a short MR note stating you are proceeding but **cannot verify/cancel** a running \`agent-review\` job due to missing
|
|
163
|
+
- **Goal:** Avoid two agents working the same MR. If a **running or pending** CI job whose name **ends with "agent-review"** is active for this MR's pipeline, **cancel** it first.
|
|
164
|
+
|
|
165
|
+
**Procedure (MCP-only):**
|
|
166
|
+
1) Resolve the MR IID from the event (target URL or text) and run **Resolve MR Pipeline** to get \`mr_pipeline_id\`.
|
|
167
|
+
2) If \`mr_pipeline_id\` is available:
|
|
168
|
+
- \`list_pipeline_jobs({ projectId: $CI_PROJECT_ID, pipelineId: mr_pipeline_id })\`.
|
|
169
|
+
- For any job with \`status\` in \`["running","pending"]\` and \`name\` ending with \`"agent-review"\`, call:
|
|
170
|
+
- \`cancel_pipeline_job({ projectId: $CI_PROJECT_ID, jobId })\`.
|
|
171
|
+
- Proceed immediately after issuing cancellations (do not wait).
|
|
172
|
+
3) If the pipeline cannot be resolved:
|
|
173
|
+
- Post a short MR note stating you are proceeding but **cannot verify/cancel** a running \`agent-review\` job due to missing pipeline context.
|
|
135
174
|
- Proceed with review.
|
|
136
175
|
|
|
137
176
|
**Notes:**
|
|
138
177
|
- Keep this guard **idempotent** (safe to run multiple times).
|
|
139
|
-
-
|
|
178
|
+
- Only applies to **event-triggered** flows that act on an **existing MR**.
|
|
140
179
|
`;
|
|
141
180
|
|
|
142
181
|
const reviewOnDemandFromEvents = () => `
|
|
143
182
|
## Review-on-Demand (from events)
|
|
144
183
|
If the issue/note text **asks for a review** (case-insensitive tokens like: "review", "please review", "PTAL", "needs review", "can you look at", "LGTM?"), then:
|
|
145
184
|
|
|
146
|
-
1) **
|
|
147
|
-
- List jobs for the current pipeline \`$CI_PIPELINE_ID\` via \`list_pipeline_jobs\`.
|
|
148
|
-
- If any job has \`status = "manual"\` **and** its \`name\` ends with "agent-review":
|
|
149
|
-
- Trigger it via \`play_pipeline_job({ projectId: $CI_PROJECT_ID, jobId })\`.
|
|
150
|
-
- Post a short comment confirming you triggered the review job (sanitize).
|
|
151
|
-
- **Stop** further review actions.
|
|
152
|
-
|
|
153
|
-
2) **If no such job exists, resolve which MR to review**:
|
|
185
|
+
1) **Resolve which MR to review**:
|
|
154
186
|
- If the event target is an MR → use its \`iid\`.
|
|
155
187
|
- Else, parse the text for MR references in order:
|
|
156
188
|
- \`!<iid>\` (e.g., \`!123\`)
|
|
@@ -158,10 +190,22 @@ If the issue/note text **asks for a review** (case-insensitive tokens like: "rev
|
|
|
158
190
|
- full GitLab MR URL
|
|
159
191
|
- If no MR can be resolved, reply with a brief comment asking the user to reference an MR (sanitize) and **stop**.
|
|
160
192
|
|
|
161
|
-
|
|
162
|
-
- Execute
|
|
193
|
+
2) **Find the MR's pipeline** (do **not** use \`$CI_PIPELINE_ID\` from the event):
|
|
194
|
+
- Execute **Resolve MR Pipeline** to obtain \`mr_pipeline_id\`.
|
|
195
|
+
|
|
196
|
+
3) **If a manual "agent-review" job exists on the MR pipeline, trigger it**
|
|
197
|
+
- If \`mr_pipeline_id\` is available:
|
|
198
|
+
- \`list_pipeline_jobs({ projectId: $CI_PROJECT_ID, pipelineId: mr_pipeline_id })\`.
|
|
199
|
+
- If any job has \`status = "manual"\` **and** its \`name\` ends with "agent-review":
|
|
200
|
+
- Trigger via \`play_pipeline_job({ projectId: $CI_PROJECT_ID, jobId })\`.
|
|
201
|
+
- **Conversations Intake**: run the section above to determine **where** to post the confirmation (prefer replying in the same thread that requested review).
|
|
202
|
+
- Post a short comment confirming you triggered the review job (sanitize).
|
|
203
|
+
- **Stop** further review actions.
|
|
163
204
|
|
|
164
|
-
4) **
|
|
205
|
+
4) **Single-Runner Guard**
|
|
206
|
+
- If no manual job was triggered, execute the **Single-Runner Guard** (it will cancel any running/pending \`agent-review\` jobs on the MR pipeline) before MR Review Mode.
|
|
207
|
+
|
|
208
|
+
5) **Enter MR Review Mode**: execute the **MR Review Bundle** below with the resolved \`mr_iid\`.
|
|
165
209
|
`;
|
|
166
210
|
|
|
167
211
|
/** Regular event workflow for non-review work */
|
|
@@ -169,22 +213,25 @@ const eventWorkflow = ({ agentUserName }: Ctx) => `
|
|
|
169
213
|
## High-Reliability Workflow (sequence + postconditions)
|
|
170
214
|
Follow this order for any change work:
|
|
171
215
|
|
|
216
|
+
0) **Conversations Intake (MANDATORY)**:
|
|
217
|
+
- For MR targets: call \`mr_discussions\` and apply the rules in **Conversations Intake & Threading**.
|
|
218
|
+
- For issue targets: attempt to load notes if supported; otherwise rely on event context and post one concise note acknowledging the limitation.
|
|
219
|
+
|
|
172
220
|
1) **Acknowledge** with a short comment on the issue/MR thread (\`create_note\`), **unless the last actor is you**.
|
|
173
221
|
2) **Discover default branch** (e.g., "main") — infer from repo/MR context if needed.
|
|
174
222
|
3) **Create a working branch** from default (stable name, e.g., \`fix/issue-<iid>-<slug>\` or \`feat/issue-<iid>-<slug>\`) via \`create_branch\`.
|
|
175
223
|
4) **Write changes → commit → push to remote branch** via \`push_files\` (or \`create_or_update_file\`).
|
|
176
224
|
5) **Verify push landed**:
|
|
177
|
-
- Fetch latest state
|
|
178
|
-
- Compare default vs \`source_branch\` via \`get_branch_diffs({ from: "<default>", to: "<source>" })\` and ensure there are diffs.
|
|
225
|
+
- Fetch latest state and ensure diffs via \`get_branch_diffs({ from: "<default>", to: "<source>" })\`.
|
|
179
226
|
6) **Create or update MR** ONLY if there is a non-empty diff via \`create_merge_request\`.
|
|
180
227
|
- Include \`Closes #<issue_iid>\` in MR description when applicable.
|
|
181
228
|
- **Assign the MR to yourself**: \`assigneeUsernames: ["${agentUserName}"]\`.
|
|
182
|
-
7) **Follow-up comment** with branch name, any commit short SHA you can obtain, files changed count (approx by diffs), and MR link via \`create_note\`, **unless the last actor is you**.
|
|
229
|
+
7) **Follow-up comment** with branch name, any commit short SHA you can obtain, files changed count (approx by diffs), and MR link via \`create_note\`, **unless the last actor is you**. Place this **in the relevant conversation thread** (see Intake rules).
|
|
183
230
|
8) **If verification fails**:
|
|
184
231
|
- Do NOT create the MR.
|
|
185
232
|
- Comment the exact failure and retry once with a fresh branch name. If still failing, comment and stop.
|
|
186
233
|
|
|
187
|
-
For Q&A-only (no code changes),
|
|
234
|
+
For Q&A-only (no code changes), run **Conversations Intake** first, then post a single concise, helpful answer **in the correct thread** (sanitize).
|
|
188
235
|
`;
|
|
189
236
|
|
|
190
237
|
/* ---------- MR-review specific (shared with both prompts) ---------- */
|
|
@@ -204,7 +251,7 @@ Follow this sequence with verification at each step:
|
|
|
204
251
|
1) **Collect context**
|
|
205
252
|
- Get MR metadata via \`get_merge_request({ projectId: $CI_PROJECT_ID, mergeRequestIid })\`.
|
|
206
253
|
- Fetch the full changeset/diffs via \`get_merge_request_diffs\` (or \`list_merge_request_diffs\`) and open discussions via \`mr_discussions\`.
|
|
207
|
-
-
|
|
254
|
+
- **Conversations Intake**: analyze discussions to find latest human notes, detect replies to the agent, and decide between inline reply vs updating your prior note.
|
|
208
255
|
|
|
209
256
|
2) **Code review**
|
|
210
257
|
- Identify required changes (bugs, tests, style, security, perf, docs).
|
|
@@ -218,34 +265,25 @@ Follow this sequence with verification at each step:
|
|
|
218
265
|
- Apply minimal, safe changes; keep commits small and clear.
|
|
219
266
|
- **Push** to the MR's **source branch** via \`push_files\` (or \`create_or_update_file\`).
|
|
220
267
|
- **Verify push landed** using \`get_branch_diffs({ from: "<target_branch>", to: "<source_branch>" })\` and ensure there are diffs.
|
|
221
|
-
- Post a follow-up MR note summarizing what changed and why (sanitize).
|
|
268
|
+
- Post a follow-up MR note summarizing what changed and why, **in the appropriate thread** (sanitize).
|
|
222
269
|
`;
|
|
223
270
|
|
|
224
271
|
const ciInspection = () => `
|
|
225
|
-
4) **CI jobs (
|
|
226
|
-
-
|
|
227
|
-
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
Post a note stating you retried and why (sanitize).
|
|
241
|
-
- If code-related ⇒ do not retry; post a note with diagnosis and suggested fix (sanitize).
|
|
242
|
-
- Retry-once policy: at most **one** retry per job in this run.
|
|
243
|
-
|
|
244
|
-
5) **Assign human reviewer if ready**
|
|
245
|
-
- If discussions are resolved and blocking CI issues are addressed or clearly triaged, request review from a recent active human contributor (not you), if supported by your environment.
|
|
246
|
-
|
|
247
|
-
6) **Stdout summary**
|
|
248
|
-
- Print concise summary: branch used, files changed count (approx by diffs), discussions resolved/left, **blocking failed jobs (names + stages)** with classification (code vs transient), which jobs were retried (if any), and requested reviewers.
|
|
272
|
+
4) **CI jobs (diagnose only; no job retries)**
|
|
273
|
+
- Prefer the **MR pipeline** (not the event pipeline).
|
|
274
|
+
- If you have \`mr_pipeline_id\` (from **Resolve MR Pipeline**):
|
|
275
|
+
- \`list_pipeline_jobs({ projectId: $CI_PROJECT_ID, pipelineId: mr_pipeline_id })\`.
|
|
276
|
+
- Consider **only** jobs with \`status = "failed"\` and \`allow_failure = false\`.
|
|
277
|
+
- For each such job:
|
|
278
|
+
1. Retrieve details (id, name, stage, status, allow_failure, web_url).
|
|
279
|
+
2. Fetch job output via \`get_pipeline_job_output({ projectId: $CI_PROJECT_ID, pipelineId: mr_pipeline_id, jobId })\`.
|
|
280
|
+
3. **Classify the failure**:
|
|
281
|
+
- **Code-related:** compiler/type/lint/test/build script errors. Provide minimal fix in your review/changes. Do **not** retry.
|
|
282
|
+
- **Likely transient / infra:** network/timeouts/cache/artifacts/5xx/429/runner issues. Do **not** retry here; note likely cause and suggest CI-level retry/backoff if appropriate.
|
|
283
|
+
4. **Decision**:
|
|
284
|
+
- If \`will_push_changes = true\`: do **not** retry; note that the new pipeline from your push will validate fixes.
|
|
285
|
+
- If \`will_push_changes = false\`: do **not** retry; post diagnosis and next steps (or request human input for infra issues).
|
|
286
|
+
- If \`mr_pipeline_id\` is not available, you may skip CI analysis or post a short note explaining the missing pipeline context.
|
|
249
287
|
`;
|
|
250
288
|
|
|
251
289
|
const outputDisciplineMR = ({ agentUserName }: Ctx) => `
|
|
@@ -279,10 +317,12 @@ $(cat $TRIGGER_PAYLOAD)
|
|
|
279
317
|
${identity(ctx)}
|
|
280
318
|
${goldenRules(ctx)}
|
|
281
319
|
${selfMentionGuard(ctx)}
|
|
320
|
+
${conversationsIntake(ctx)} <!-- NEW: mandatory before acting -->
|
|
282
321
|
${eventSelfParse()}
|
|
283
|
-
${
|
|
322
|
+
${resolveMrPipeline()} <!-- used by review/cancel paths -->
|
|
323
|
+
${singleRunnerGuard()} <!-- operates on MR pipeline, not event pipeline -->
|
|
284
324
|
${reviewOnDemandFromEvents()}
|
|
285
|
-
${mrReviewBundle(ctx)}
|
|
325
|
+
${mrReviewBundle(ctx)} <!-- agent can execute when review intent is true -->
|
|
286
326
|
${eventWorkflow(ctx)}
|
|
287
327
|
${commentGuidelines()}
|
|
288
328
|
${mcpOnly()}
|
|
@@ -302,6 +342,7 @@ description: $CI_MERGE_REQUEST_DESCRIPTION
|
|
|
302
342
|
${mrScope(ctx)}
|
|
303
343
|
${goldenRules(ctx)}
|
|
304
344
|
${selfMentionGuard(ctx)}
|
|
345
|
+
${conversationsIntake(ctx)} <!-- NEW: always read MR discussions -->
|
|
305
346
|
${mrWorkflow()}
|
|
306
347
|
${ciInspection()}
|
|
307
348
|
${commentGuidelines()}
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
escapeDoubleQuotes,
|
|
4
4
|
escapeNewlines,
|
|
5
5
|
} from "../../bash/bashEscape";
|
|
6
|
+
import { getRunnerImage } from "../../runner";
|
|
6
7
|
import type { AgentContext, CatladderJob } from "../../types";
|
|
7
8
|
|
|
8
9
|
export const createBaseAgentJob = (
|
|
@@ -10,7 +11,8 @@ export const createBaseAgentJob = (
|
|
|
10
11
|
): Omit<CatladderJob, "name" | "rules" | "script"> => ({
|
|
11
12
|
stage: "agents",
|
|
12
13
|
envMode: "none",
|
|
13
|
-
image: "node:24-alpine3.21",
|
|
14
|
+
// image: "node:24-alpine3.21",
|
|
15
|
+
image: getRunnerImage("agent-claude"),
|
|
14
16
|
variables: {
|
|
15
17
|
MAX_MCP_OUTPUT_TOKENS: "75000",
|
|
16
18
|
GITLAB_PERSONAL_ACCESS_TOKEN: "$AGENT_GITLAB_PERSONAL_ACCESS_TOKEN", // TODO: we don't have global secret keys to configure yet
|
|
@@ -19,9 +21,10 @@ export const createBaseAgentJob = (
|
|
|
19
21
|
});
|
|
20
22
|
|
|
21
23
|
export const baseSetupScript = [
|
|
22
|
-
|
|
23
|
-
"apk
|
|
24
|
-
"
|
|
24
|
+
// these are done in the image already
|
|
25
|
+
// "apk update",
|
|
26
|
+
// "apk add --no-cache git curl bash",
|
|
27
|
+
//"npm install -g @anthropic-ai/claude-code",
|
|
25
28
|
"claude mcp add gitlab --env GITLAB_PERSONAL_ACCESS_TOKEN=$GITLAB_PERSONAL_ACCESS_TOKEN --env GITLAB_API_URL=$GITLAB_API_URL --env USE_PIPELINE='true' -- npx -y @zereight/mcp-gitlab",
|
|
26
29
|
];
|
|
27
30
|
|
package/src/runner/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ export type RunnerImageName =
|
|
|
7
7
|
| "kubernetes"
|
|
8
8
|
| "docker-build"
|
|
9
9
|
| "gcloud"
|
|
10
|
-
| "semantic-release"
|
|
10
|
+
| "semantic-release"
|
|
11
|
+
| "agent-claude";
|
|
11
12
|
export const getRunnerImage = (imageName: RunnerImageName) =>
|
|
12
13
|
DOCKER_REGISTRY + "/" + imageName + ":" + PIPELINE_IMAGE_TAG;
|