diff --git a/CLAUDE.md b/CLAUDE.md index b6aafbe..2170d00 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -9,7 +9,8 @@ MCP server (TypeScript, ESM, Express 5, `@modelcontextprotocol/sdk`) that genera - `npm run typecheck` - `npm test`: unit tests plus fixture-based browser tests. The browser tests skip if Playwright Chromium is missing. When the default cache is read-only, set `PLAYWRIGHT_BROWSERS_PATH`. - `npm run login` (`-- --check`): manual chatgpt.com login with the persistent profile. The server must be stopped first. -- `npm run selfheal` (`SELFHEAL_DRY_RUN=true`): codex repair of the scraper in a throwaway clone; opens a PR (docs/selfheal.md) +- `npm run selfheal` (`SELFHEAL_DRY_RUN=true`): codex repair of the scraper in a throwaway clone; live smoke test, auto-merge and deploy (docs/selfheal.md) +- `npm run smoke`: generate one real image on chatgpt.com with the current code (uses quota; service stopped) ## Layout diff --git a/README.md b/README.md index 5c51c74..bc081bc 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,6 @@ npm test - [docs/architecture.md](docs/architecture.md): generation flow and lineage - [docs/deployment.md](docs/deployment.md): install on Debian/Ubuntu with systemd - [docs/update.md](docs/update.md): update script and authenticated update endpoint -- [docs/selfheal.md](docs/selfheal.md): automatic scraper repair with codex, delivered as a pull request +- [docs/selfheal.md](docs/selfheal.md): automatic scraper repair with codex, verified live, then auto-merged and deployed > This automates a consumer web UI. It is unofficial, can break whenever chatgpt.com changes, and must comply with OpenAI's terms for the account used. diff --git a/docs/selfheal.md b/docs/selfheal.md index 6ce1a07..2a502a4 100644 --- a/docs/selfheal.md +++ b/docs/selfheal.md @@ -1,6 +1,6 @@ # Self-heal (codex) -chatgpt.com changes its frontend without notice. When the scraper breaks, the server can run **codex in yolo mode** to inspect the live site and patch the code. The fix arrives as a **pull request** that a human reviews and merges. Nothing is merged or deployed automatically. +chatgpt.com changes its frontend without notice. When the scraper breaks, the server can run **codex in yolo mode** to inspect the live site and patch the code. If the patch passes the tests **and generates a real image on chatgpt.com**, it is **merged automatically and deployed** to the running server with `scripts/update.sh`. Patches that fail the live check stay as open pull requests. ## Trigger @@ -22,26 +22,33 @@ The run's state and the PR URL appear in `get_usage`, `who_am_i` and `/health` ( - `scripts/selfheal/browse.py`, an inspector built on Playwright and playwright-stealth. It opens chatgpt.com with the profile copy, can send a test image prompt, and saves HTML, screenshots and a JSON summary of `data-testid`, `aria-label`, roles, inputs and images over time. 5. Discards the attempt if anything outside `src/chatgpt/*.ts`, `test/fixtures/*.html` or `test/client.test.ts` changed. 6. Runs `npm run typecheck` and `npm test` in the clone. -7. Commits, pushes the branch and opens the PR with `tea`. Codex's summary becomes the description. -8. Always deletes the clone and the profile copy. +7. Runs the **live smoke test**: `src/smoke.ts` generates one real image with the patched code and the profile copy (it uses one image of quota). +8. Commits, pushes the branch and opens a PR with `tea`. Codex's summary and the smoke result go in the description. +9. If the smoke test passed, it squash-merges the PR (`tea pr merge`, `SELFHEAL_AUTO_MERGE`). It then hands over to `scripts/update.sh` (`SELFHEAL_AUTO_UPDATE`), which pulls, builds, tests and restarts the service; its output goes to `UPDATE_LOG_FILE`, so `/admin/update/status` shows it. +10. Always deletes the clone and the profile copy. + +After the restart the server restores the last run from `SELFHEAL_LOG_FILE`. That covers its status and PR, and also its start time, so the cooldown survives the restart. If a merged patch still fails, there is no immediate retry: the next run waits at least `SELFHEAL_COOLDOWN_HOURS`. To roll back, revert the squash commit on `main` and call the update endpoint. Exit codes and the `SELFHEAL_RESULT` log line: | Code | Meaning | |---|---| -| 0 | PR created, or a dry run succeeded | +| 0 | Merged and deployed, PR created (auto-merge off), or dry run ok | | 3 | No changes | | 4 | Forbidden paths changed | | 5 | Verification failed | | 6 | Codex failed or timed out | +| 7 | Merge failed; the PR is left open | +| 8 | Live smoke test failed; the PR is left open, or nothing is pushed in a dry run | ## Manual run -Stop the service first: the profile is copied from `CHATGPT_PROFILE_DIR`. +Stop the service first: the profile is copied from `CHATGPT_PROFILE_DIR`. Without a desktop, wrap the command in `xvfb-run -a`, because the smoke test runs a headed browser. ```bash -SELFHEAL_DRY_RUN=true npm run selfheal # no push, no PR -npm run selfheal +SELFHEAL_DRY_RUN=true xvfb-run -a npm run selfheal # no push, no PR, no deploy +xvfb-run -a npm run selfheal +npm run smoke # live check of the current code (service stopped) ``` ## Server setup (once, as user `mcp`) @@ -49,7 +56,8 @@ npm run selfheal - **codex CLI:** install it and run `codex login`. Its config lives in `~/.codex`, which is `/opt/chatgpt-imagegen-mcp/.codex` with the default install. - **Python:** `python3` and `python3-venv` (apt). The venv and the Python Playwright browser are installed on the first run. - **git push:** needs a deploy key with **write** access to the repository, or credentials for the remote. -- **tea:** `tea login add` with a token that can open PRs. Set `SELFHEAL_TEA_LOGIN` when there are several logins. +- **tea:** `tea login add` with a token that can open **and merge** PRs. Branch protection on `main` must allow that user to merge. Set `SELFHEAL_TEA_LOGIN` when there are several logins. +- **Update script:** the sudoers rule from [deployment.md](deployment.md), which lets the service restart itself. - **Enable it:** set `SELFHEAL_ENABLED=true` in `.env`. ## Risks (accepted by design) @@ -60,7 +68,11 @@ Yolo mode means codex runs with the full permissions of the `mcp` user and witho - the server's secrets are removed from its environment; - a path allowlist is enforced; - the full test suite must pass; -- a human reviews every PR before merging. +- a real image must be generated with the patched code before it is merged; +- `update.sh` restores the previous build and does not restart if the build or tests fail on the server; +- there is a cooldown between runs, and every change stays traceable as a squash commit plus a PR. + +Merges are automatic, so nobody reviews the patch before production. Check the self-heal PRs afterwards. The diagnostics and live page content are untrusted: the prompt tells codex to treat them as data. Review PRs with that in mind, and check that fixtures carry no conversation text. @@ -78,4 +90,8 @@ The diagnostics and live page content are untrusted: the prompt tells codex to t | `SELFHEAL_VENV` | `.selfheal-venv` | Python venv with playwright and playwright-stealth | | `SELFHEAL_PR_ASSIGNEES` / `SELFHEAL_TEA_LOGIN` | unset | PR assignees and tea login | | `SELFHEAL_GIT_NAME` / `SELFHEAL_GIT_EMAIL` | selfheal identity | Commit author | -| `SELFHEAL_DRY_RUN` | `false` | Stop after verification, no push or PR | +| `SELFHEAL_AUTO_MERGE` | `true` | Merge the PR when the live smoke test passes | +| `SELFHEAL_AUTO_UPDATE` | `true` | Deploy with `scripts/update.sh` after merging | +| `SELFHEAL_SKIP_SMOKE` | `false` | Skip the live smoke test (tests only) | +| `SELFHEAL_SMOKE_TIMEOUT_SECONDS` | `900` | Limit for the live smoke test | +| `SELFHEAL_DRY_RUN` | `false` | Stop after verification: no push, PR or deploy |