Skip to content
Merged
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
24 changes: 21 additions & 3 deletions agents/fix.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ path unchanged.
- You MUST address every finding from the review body. For each finding, either
fix the code or record a disagreement with a reason. Do not silently skip items.
- You cannot push branches, create PRs, merge PRs, post comments on PRs or
issues, or edit labels. These are post-script responsibilities.
issues, edit labels, or edit PR/MR titles or descriptions. These are
post-script responsibilities. Propose title/description changes through
structured output instead (see "PR/MR title and description" below).
- You cannot run `git add -A`, `git add .`, or `git add --all`. Only stage
files you explicitly created or modified.
- You cannot use `sed`, `awk`, or other stream editors to modify source files.
Expand Down Expand Up @@ -266,6 +268,21 @@ post-script cannot communicate your work back to the reviewer. Follow the
`fix-result-contract` skill for the schema, the `fullsend-check-output`
validation loop, partial-work behavior, and failure handling.

## PR/MR title and description

You cannot edit the PR/MR yourself, but you can propose a replacement
through the optional `pr_title` and `pr_body` fields. The post-script
applies them. Propose a field only when the current value has become
materially inaccurate. This includes changes made by this run or an
earlier one: the PR now touches different files, takes a different
approach, or describes superseded behavior or stale results such as test
counts. Also propose it when a human `/fs-fix` instruction or a review
finding asks for it. In that case the run may change no code at all.
Never propose a field just to restyle or reword it. Leave out any field
that is still accurate. Treat a finding about stale PR metadata as
fixable this way, not out of scope. Follow the `fix-result-contract`
skill for the field rules, including the required base hashes.

## Iteration awareness

The fix agent may run many times on the same PR as part of the review→fix loop.
Expand All @@ -289,8 +306,9 @@ loop. This is a retry *within a single run*: when the harness
validation, the runner relaunches you with the failure text appended to
your prompt. Follow the `fix-result-contract` skill for the full
validation-retry procedure, including how to carry `rebased_onto_target`,
`merged_target`, `conflict_update`, and `history_rewritten` forward when
the runner clears the output directory between iterations.
`merged_target`, `conflict_update`, `history_rewritten`, and the PR/MR
metadata fields forward when the runner clears the output directory
between iterations.

## Detailed fix procedure

Expand Down
11 changes: 10 additions & 1 deletion docs/fix.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ The fix agent uses the `fix-verification` skill after fixes are implemented: man

### Skill: `fix-result-contract`

The fix agent uses the `fix-result-contract` skill as the canonical owner of the `agent-result.json` handoff contract: schema requirements, the `fullsend-check-output` validation loop, partial-work behavior, failure handling, and validation-retry semantics (including carrying `rebased_onto_target`, `merged_target`, `conflict_update`, and `history_rewritten` forward when the runner clears the output directory between retries).
The fix agent uses the `fix-result-contract` skill as the canonical owner of the `agent-result.json` handoff contract: schema requirements, the `fullsend-check-output` validation loop, partial-work behavior, failure handling, optional PR title/description proposals, and validation-retry semantics (including carrying `rebased_onto_target`, `merged_target`, `conflict_update`, `history_rewritten`, and the PR metadata fields forward when the runner clears the output directory between retries).

## How the agent works

Expand All @@ -108,6 +108,15 @@ The fix agent follows a similar pipeline to the [code agent](code.md), with an a
3. **Validation loop** — the output is checked against a schema, with up to 2 retry iterations if the output is malformed.
4. **Post-script** publishes the commit (on GitHub through the API when `GITHUB_SIGNED_COMMITS` is `true`, so it is signed — see [Signed commits](code.md#signed-commits)) and posts a summary comment on the PR.

### Updating the PR title and description

A fix can leave the PR title or description inaccurate, for example after content moves to a different file or a test count changes. When that happens the agent proposes a replacement in the optional `pr_title` / `pr_body` fields of `agent-result.json`, and the post-script applies it. GitHub uses `gh pr edit` with the body passed through `--body-file -`. GitLab uses the merge request update API (`title` / `description`). The update happens only when all of these hold:

- the output passed schema validation and the post-script's secret scan of `agent-result.json`;
- the live value still matches the SHA-256 the agent recorded for the value it read (`pr_title_base_sha256` / `pr_body_base_sha256`). A human edit made after the agent read the value and before the post-script's check is not overwritten; the field is skipped with a warning instead. The check and the write are separate API calls, because neither `gh pr edit` nor the GitLab MR update offers a conditional write, so an edit landing in the short window between them can still be overwritten.

A field the agent did not propose is left unchanged. When the body is replaced, the existing issue-closing reference and the `### Post-script verification` section are kept. Before the body is written, the post-script checks that the merged body still shows them outside fenced code and HTML comments. If a proposal's Markdown could hide them (for example an unclosed code fence or HTML comment), only the body update is skipped with a warning; the post-script does not close or change the agent's Markdown, and a valid title update still applies. The outcome is noted in the summary comment. A failed update is logged as a warning and does not fail the run or block the summary.

### Signed-off-by trailers

Same behaviour as the [code agent](code.md#signed-off-by-trailers): a trailer
Expand Down
39 changes: 39 additions & 0 deletions schemas/fix-result.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
"type": "object",
"required": ["pr_number", "trigger_source", "actions", "summary", "tests_passed", "files_changed"],
"additionalProperties": false,
"allOf": [
{
"if": { "required": ["pr_title"] },
"then": { "required": ["pr_title_base_sha256"] }
},
{
"if": { "required": ["pr_body"] },
"then": { "required": ["pr_body_base_sha256"] }
}
],
"properties": {
"pr_number": {
"type": "integer",
Expand Down Expand Up @@ -160,6 +170,35 @@
"type": "boolean",
"description": "Set to true whenever this run's HEAD reflects an authorized human squash or redo/reset that still needs to be published on the remote PR (see 'Rewrite fix-agent history' in the fix-history-rewrite skill), or a validation-loop retry carrying the field forward. A squash targets the whole PR range (fix-agent, code-agent, and human commits alike) and must land as exactly one commit ahead of the target branch; a redo/reset only discards and re-implements the contiguous fix-agent commit suffix, preserving every commit below it. Never set for a failed/aborted rewrite, a squash no-op (PR already a single commit), or a bot-triggered run. The post-script requires this field together with a harness-verified human squash/redo request (TRIGGER_SOURCE plus the literal /fs-fix instruction text) before skipping the replay onto the remote PR tip — for a squash it additionally refuses to publish unless exactly one commit results, and for a redo/reset it refuses to publish if a human-authored or code-agent commit below the rewrite base would be lost."
},
"pr_title": {
"type": "string",
"minLength": 1,
"maxLength": 200,
"pattern": "\\S",
"not": { "pattern": "[\\r\\n]" },
"description": "Optional replacement PR/MR title. Set only when this run made the current title materially inaccurate. Single line (no CR/LF). Requires pr_title_base_sha256. Applied by the post-script after schema validation and the secret scan pass, and only if the live title still matches pr_title_base_sha256."
},
"pr_title_base_sha256": {
"type": "string",
"minLength": 64,
"maxLength": 64,
"pattern": "^[0-9a-f]{64}$",
"description": "Lowercase hex SHA-256 of the exact PR/MR title the agent read before proposing pr_title. The post-script skips the title update when the live title no longer matches (a concurrent edit)."
},
"pr_body": {
"type": "string",
"minLength": 1,
"maxLength": 8000,
"pattern": "\\S",
"description": "Optional replacement PR/MR description. Set only when this run made the current description materially inaccurate. Requires pr_body_base_sha256. The post-script keeps the existing issue-closing reference and post-script verification section, drops any copies of them from this text, and applies it only after schema validation and the secret scan pass and the live body still matches pr_body_base_sha256."
},
"pr_body_base_sha256": {
"type": "string",
"minLength": 64,
"maxLength": 64,
"pattern": "^[0-9a-f]{64}$",
"description": "Lowercase hex SHA-256 of the exact PR/MR description the agent read before proposing pr_body (empty string when the description was empty or null). The post-script skips the body update when the live description no longer matches (a concurrent edit)."
},
"ci_inspections": {
"type": "array",
"maxItems": 50,
Expand Down
237 changes: 237 additions & 0 deletions scripts/post-fix-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6962,6 +6962,243 @@ run_postfix_budget_test "postfix-budget-human-keeps-human-cap" \

rm -rf "${FIX_BUDGET_TMPDIR}"

# ---------------------------------------------------------------------------
# PR/MR metadata update (issue #519) against the real script. A metadata-only
# run (no commit, NO_PUSH=true) proposes pr_title / pr_body. The update must
# only happen after the agent-result.json secret scan passes, must keep the
# post-script-managed body sections, and a failed edit must not block the
# summary comment or fail the run.
# ---------------------------------------------------------------------------
PRM_TMPDIR="$(mktemp -d)"

# write_prm_fixtures <dir> — the live PR (mock forge response) and an
# agent-result.json whose base hashes match it.
write_prm_fixtures() {
python3 - "$1" <<'PYEOF'
import hashlib, json, os, sys
d = sys.argv[1]
title = "docs(#484): document X in AGENTS.md"
body = ("Adds docs to AGENTS.md.\n\n---\n\nCloses #484\n\n"
"### Post-script verification\n\n- [x] Secret scan passed")
with open(os.path.join(d, "live-pr.json"), "w") as f:
json.dump({"title": title, "body": body, "description": body}, f)
h = lambda s: hashlib.sha256(s.encode()).hexdigest()
result = {
"pr_number": 99,
"trigger_source": "human",
"actions": [{"type": "fix", "finding": "Stale PR metadata",
"description": "Proposed an accurate title and body"}],
"summary": "Updated PR metadata only.",
"tests_passed": True,
"files_changed": [],
"pr_title": "docs(#484): document X in docs/code.md",
"pr_title_base_sha256": h(title),
"pr_body": "Moves the docs to `docs/code.md`.\n\nKeeps \"quotes\" and 'single' quotes.",
"pr_body_base_sha256": h(body),
}
os.makedirs(os.path.join(d, "validated-output"), exist_ok=True)
text = json.dumps(result)
# PRM_ESCAPED_SECRET_FIELD: carry a marker credential in that field, written
# with \uXXXX escapes so it never appears in plain text in the serialized file.
field = os.environ.get("PRM_ESCAPED_SECRET_FIELD", "")
if field:
result[field] = result[field] + " PRMSECRET-1234"
text = json.dumps(result).replace(
"PRMSECRET-1234", "".join("\\u%04x" % ord(c) for c in "PRMSECRET-1234"))
with open(os.path.join(d, "validated-output", "agent-result.json"), "w") as f:
f.write(text)
PYEOF
}

# run_prm_test <name> <forge> <gitleaks-rc> <edit-rc> <expect-exit-zero>
run_prm_test() {
local test_name="$1" forge="$2" gitleaks_rc="$3" edit_rc="$4" expect_ok="$5"
local run_dir="${PRM_TMPDIR}/run-${test_name}"
local mock_bin="${run_dir}/bin"
local log="${run_dir}/stdout.log"
mkdir -p "${mock_bin}" "${run_dir}/repo"
write_prm_fixtures "${run_dir}"

git init -q -b main "${run_dir}/repo"
git -C "${run_dir}/repo" config user.email "test@example.com"
git -C "${run_dir}/repo" config user.name "Test"
git -C "${run_dir}/repo" commit --allow-empty -m "init" -q

if [ "${gitleaks_rc}" = "marker" ]; then
# Fails only when the marker credential appears in plain text in a scanned
# file, as a real signature would.
cat > "${mock_bin}/gitleaks" <<'MOCKEOF'
#!/usr/bin/env bash
while [ $# -gt 0 ]; do
if [ "$1" = "--source" ]; then src="$2"; fi
shift
done
if grep -rqF 'PRMSECRET-1234' "${src}"; then exit 1; fi
exit 0
MOCKEOF
else
printf '#!/usr/bin/env bash\nexit %s\n' "${gitleaks_rc}" > "${mock_bin}/gitleaks"
fi
cat > "${mock_bin}/gh" <<MOCKEOF
#!/usr/bin/env bash
case "\$1 \$2" in
"pr view")
case "\$*" in
*"title,body"*) cat "${run_dir}/live-pr.json" ;;
esac
exit 0 ;;
"pr edit")
printf '%s\n' "\$@" > "${run_dir}/edit-args.log"
cat > "${run_dir}/edit-body.log"
exit ${edit_rc} ;;
"pr comment")
cat > "${run_dir}/comment.log"
exit 0 ;;
esac
exit 0
MOCKEOF
cat > "${mock_bin}/curl" <<MOCKEOF
#!/usr/bin/env bash
cat > /dev/null
case "\$*" in
*"--request PUT"*)
printf '%s\n' "\$@" > "${run_dir}/edit-args.log"
exit ${edit_rc} ;;
*"--request POST"*"/notes"*)
printf '%s\n' "\$@" > "${run_dir}/comment.log"
exit 0 ;;
*"--request GET"*) cat "${run_dir}/live-pr.json" ;;
esac
exit 0
MOCKEOF
chmod +x "${mock_bin}/gitleaks" "${mock_bin}/gh" "${mock_bin}/curl"

local exit_code=0
# shellcheck disable=SC2030,SC2031
(
cd "${run_dir}"
export PATH="${mock_bin}:${PATH}"
export PUSH_TOKEN="fake-token"
export REPO_FULL_NAME="test-org/test-repo"
export PR_NUMBER="99"
export TRIGGER_SOURCE="test-user"
export REPO_DIR="repo"
export FULLSEND_FORGE="${forge}"
export FULLSEND_VALIDATED_ITERATION_DIR="${run_dir}/validated-output"
export FULLSEND_OUTPUT_SCHEMA="${SCRIPT_DIR}/../schemas/fix-result.schema.json"
PRE_AGENT_HEAD="$(git -C repo rev-parse HEAD)"
export PRE_AGENT_HEAD
if [ "${forge}" = "gitlab" ]; then
export CI_SERVER_HOST="gitlab.com"
export PR_URL="https://gitlab.com/test-org/test-repo/-/merge_requests/99"
export GITLAB_TOKEN="fake-gitlab-token"
fi
bash "${POST_SCRIPT}"
) > "${log}" 2>&1 || exit_code=$?

if [ "${expect_ok}" = "true" ] && [ "${exit_code}" -ne 0 ]; then
echo "FAIL: ${test_name} — exit code ${exit_code}"
cat "${log}"
FAILURES=$((FAILURES + 1))
return 1
fi
if [ "${expect_ok}" != "true" ] && [ "${exit_code}" -eq 0 ]; then
echo "FAIL: ${test_name} — expected non-zero exit"
cat "${log}"
FAILURES=$((FAILURES + 1))
return 1
fi
PRM_RUN_DIR="${run_dir}"
return 0
}

prm_fail() {
echo "FAIL: $1 — $2"
cat "${PRM_RUN_DIR}/stdout.log"
FAILURES=$((FAILURES + 1))
}

# GitHub: both fields applied, body via stdin, managed sections kept.
if run_prm_test "metadata-github-applied-no-push" github 0 0 true; then
name="metadata-github-applied-no-push"
if ! grep -qx -- '--title=docs(#484): document X in docs/code.md' "${PRM_RUN_DIR}/edit-args.log" 2>/dev/null; then
prm_fail "${name}" "gh pr edit missing --title"
elif ! grep -qx -- '--body-file' "${PRM_RUN_DIR}/edit-args.log"; then
prm_fail "${name}" "gh pr edit did not pass the body through --body-file"
elif ! grep -qF 'Keeps "quotes" and '"'"'single'"'"' quotes.' "${PRM_RUN_DIR}/edit-body.log" \
|| ! grep -qF 'Moves the docs to `docs/code.md`.' "${PRM_RUN_DIR}/edit-body.log"; then
prm_fail "${name}" "body text not passed through intact"
elif ! grep -qx 'Closes #484' "${PRM_RUN_DIR}/edit-body.log" \
|| ! grep -qx '### Post-script verification' "${PRM_RUN_DIR}/edit-body.log"; then
prm_fail "${name}" "managed body sections not preserved"
elif grep -qF 'Adds docs to AGENTS.md.' "${PRM_RUN_DIR}/edit-body.log"; then
prm_fail "${name}" "stale description not replaced"
elif ! grep -qF '**PR metadata:** title updated; body updated' "${PRM_RUN_DIR}/comment.log" 2>/dev/null; then
prm_fail "${name}" "summary comment missing metadata outcome"
else
echo "PASS: ${name}"
fi
fi

# Failed secret scan of agent-result.json: no metadata update.
if run_prm_test "metadata-secret-scan-failure-blocks-update" github 1 0 false; then
if [ -f "${PRM_RUN_DIR}/edit-args.log" ]; then
prm_fail "metadata-secret-scan-failure-blocks-update" "gh pr edit ran despite a failed secret scan"
elif ! grep -q 'Scanning agent-result.json for secrets' "${PRM_RUN_DIR}/stdout.log" \
|| grep -q 'Processing agent-result.json' "${PRM_RUN_DIR}/stdout.log"; then
prm_fail "metadata-secret-scan-failure-blocks-update" "did not stop at the agent-result.json secret scan"
else
echo "PASS: metadata-secret-scan-failure-blocks-update"
fi
fi

# A credential written with \uXXXX escapes in the proposed title or body is
# caught by scanning the decoded values, so no metadata update happens.
for secret_field in pr_title pr_body; do
name="metadata-escaped-secret-in-${secret_field}-blocks-update"
export PRM_ESCAPED_SECRET_FIELD="${secret_field}"
if run_prm_test "${name}" github marker 0 false; then
if [ -f "${PRM_RUN_DIR}/edit-args.log" ]; then
prm_fail "${name}" "gh pr edit ran despite an escaped secret in ${secret_field}"
elif grep -q 'Processing agent-result.json' "${PRM_RUN_DIR}/stdout.log"; then
prm_fail "${name}" "did not stop at the agent-result.json secret scan"
else
echo "PASS: ${name}"
fi
fi
unset PRM_ESCAPED_SECRET_FIELD
done

# A failed edit is visible but non-fatal: the summary is still posted.
if run_prm_test "metadata-edit-failure-non-fatal" github 0 1 true; then
if ! grep -qF 'title update failed; body update failed' "${PRM_RUN_DIR}/comment.log" 2>/dev/null; then
prm_fail "metadata-edit-failure-non-fatal" "summary comment missing or lacks the failure outcome"
elif ! grep -q 'Failed to update PR title and body' "${PRM_RUN_DIR}/stdout.log"; then
prm_fail "metadata-edit-failure-non-fatal" "missing warning for the failed edit"
else
echo "PASS: metadata-edit-failure-non-fatal"
fi
fi

# GitLab: the MR update API gets title and description; gh is never used.
if run_prm_test "metadata-gitlab-applied-no-push" gitlab 0 0 true; then
name="metadata-gitlab-applied-no-push"
if ! grep -qx 'title=docs(#484): document X in docs/code.md' "${PRM_RUN_DIR}/edit-args.log" 2>/dev/null; then
prm_fail "${name}" "MR update missing title"
elif ! grep -q '^description=Moves the docs' "${PRM_RUN_DIR}/edit-args.log" \
|| ! grep -qx 'Closes #484' "${PRM_RUN_DIR}/edit-args.log"; then
prm_fail "${name}" "MR update missing description or managed sections"
elif grep -q 'fake-gitlab-token' "${PRM_RUN_DIR}/edit-args.log"; then
prm_fail "${name}" "token leaked into curl arguments"
elif ! grep -qF 'title updated; body updated' "${PRM_RUN_DIR}/comment.log" 2>/dev/null; then
prm_fail "${name}" "MR summary note missing metadata outcome"
else
echo "PASS: ${name}"
fi
fi

rm -rf "${PRM_TMPDIR}"

# --- Summary ---

echo ""
Expand Down
Loading
Loading