Self-Healing Agent Loop
Automated issue creation and AI-powered fixes via GitHub Copilot
Overview
The platform runs a guarded self-healing loop that uses GitHub Copilot to investigate and fix reported issues automatically.
The loop connects three systems: the built-in error reporter, the client debug reporter, and GitHub Actions workflows that assign Copilot cloud agent to incoming issues.
How It Works
The loop follows these steps:
- A server or client reporter creates a GitHub issue with
auto-reportedlabel. copilot-autofix.ymlwatches newauto-reportedissues and assigns Copilot cloud agent.create-issue-on-failure.ymlcreates or updatesci-failureissues when CI or Audit Loop fails, and assigns Copilot only on first occurrence.- The agent investigates and opens a PR if a safe fix is clear.
- CI and audit workflows run on the PR.
- In testing mode,
auto-merge-agent-prs.ymlcan merge safe agent PRs after required checks pass. close-agent-issue-on-merge.ymlcloses the linked issue when the PR is merged.
Do not enable auto-merge for production without review gates. Agent PRs should be reviewed before merging.
What Is Already Wired
Server Error Reporter (utils/error-reporter.js)
- Creates GitHub issues for server crashes and repeated 500-level route errors.
- Adds
auto-reportedandserver-errorlabels. - Builds an agent-ready issue body with root-cause and validation instructions.
Client Debug Reporter (routes/debug.js)
- Creates client-reported issues from the pinned debug icon in the admin and client portal shells.
- Adds
auto-reported,client-reported, andclient-debuglabels. - Includes the note, last action, recent browser events, console logs, viewport, page URL, and screenshot.
GitHub Actions Workflows
| Workflow | Trigger | Behavior |
|---|---|---|
copilot-autofix.yml | New issue with auto-reported | Assigns copilot-swe-agent[bot] to investigate. Ignores ci-failure labels so workflow failures use only create-issue-on-failure.yml. |
create-issue-on-failure.yml | CI or Audit Loop failure | Creates or updates a ci-failure issue. Assigns Copilot only when the matching issue is first created; repeat failures add a comment without reassigning. |
close-agent-issue-on-merge.yml | PR merged | Closes issues carrying auto-reported, client-debug, server-error, or ci-failure when the PR body contains a closing keyword (e.g. Fixes #64). |
auto-merge-agent-prs.yml | Testing mode | Merges Copilot PRs from copilot/* branches after Static Phase Checks and Security + Code Audit pass. Requires a closing keyword linked to an open agent issue. Skips PRs with requested-changes review. |
audit-loop.yml | Scheduled + manual | Runs secret scans with full checkout history so pull-request scans can resolve the head commit parent. |
Required GitHub Setup
Enable these in your repository:
- Settings → Actions → General: Actions enabled.
- Settings → Copilot → Cloud agent: Enable Copilot cloud agent for the repository.
- Settings → Issues: Issues enabled.
- Settings → Secrets and variables → Actions: Add
COPILOT_AGENT_TOKEN.
COPILOT_AGENT_TOKEN should be a GitHub user token (not a workflow token) with these permissions:
- Actions: read/write
- Contents: read/write
- Issues: read/write
- Pull requests: read/write
- Metadata: read
Environment Variables
On the API service:
GITHUB_ISSUE_TOKEN=github_pat_with_issues_read_write
CLIENT_DEBUG_REPORTER_ENABLED=true
CLIENT_DEBUG_PUBLIC_API_URL=https://api.v2.affinitydesign.ca
CLIENT_DEBUG_SCREENSHOT_TTL_DAYS=90
On the web service:
NEXT_PUBLIC_CLIENT_DEBUG_TOOL=true
Labels
The reporters and workflows try to create labels automatically. If GitHub rejects label creation, create these manually at https://github.com/Affinity-Design/af-gemini-connect/labels:
bugauto-reportedclient-reportedclient-debugserver-errorci-failure
Reviewing Agent PRs
- Open the PR linked from the issue or the Agents tab.
- Review the files changed and the PR notes.
- Check that CI passes or read the failed job output before merging.
- Confirm the PR body contains a closing keyword linked to the source issue.
- Mark the PR ready for review if it is still a draft and the patch looks acceptable.
- Merge manually or let the testing auto-merge workflow merge it.
Manual Recovery
If an issue is created but Copilot is not assigned:
- Open the issue.
- In the right sidebar, select Assignees.
- Select Copilot.
- Keep the base branch as
mainand submit.
If Copilot does not appear as an assignee option, check:
- Copilot cloud agent is enabled for the repository.
- The organization policy allows the agent.
- Your account or organization has a supported Copilot plan.
COPILOT_AGENT_TOKENhas the permissions listed above.
