Skip to content

fix(restore-with-files): Add sanitized site config to agent payload#6441

Open
regdocs wants to merge 2 commits into
developfrom
fix-restore-with-files-flow
Open

fix(restore-with-files): Add sanitized site config to agent payload#6441
regdocs wants to merge 2 commits into
developfrom
fix-restore-with-files-flow

Conversation

@regdocs
Copy link
Copy Markdown
Member

@regdocs regdocs commented May 15, 2026

Resolves #2032

Needs to be paired with frappe/agent#509.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 15, 2026

Greptile Summary

This PR extends restore_site in agent.py to read and sanitize the backed-up site_config.json (via remote_config_file) and include it in the agent job payload, with maintenance_mode forced to 0.

  • The import path press.press.utils does not exist; the module is press.utils, so the function will raise ImportError on every call.
  • sanitized_config_content is assigned only inside the if site.remote_config_file: guard but referenced unconditionally in data, causing a NameError for every restore that has no config file.

Confidence Score: 1/5

Two runtime-breaking bugs are present: a wrong import path that raises ImportError on every call, and an uninitialized variable that raises NameError for restores without a config file. The new feature cannot work in its current state and will regress existing restores.

The wrong import path (press.press.utils vs press.utils) means the function fails immediately on every invocation. The missing sanitized_config_content = None initialisation means any restore without a remote config file — likely the majority of existing restore jobs — will also crash at the data = {...} assignment. Both issues are on the hot path.

press/agent.py — the restore_site method has the wrong import path and a missing variable initialisation that will crash every restore call.

Important Files Changed

Filename Overview
press/agent.py Adds sanitized site config to the restore-site agent payload; contains two runtime-breaking bugs: wrong import path (press.press.utils doesn't exist) and missing initialisation of sanitized_config_content before the conditional block causes a NameError on every restore without a config file.

Sequence Diagram

sequenceDiagram
    participant Press as Press (agent.py)
    participant RF as Remote File (DocType)
    participant Utils as press.utils.sanitize_config
    participant Agent as Agent Job (Restore Site)

    Press->>Press: restore_site(site)
    Press->>Press: check_space_on_server_for_restore()
    Press->>RF: get_doc("Remote File", remote_database_file)
    RF-->>Press: database_link
    Press->>RF: get_doc("Remote File", remote_public_file)
    RF-->>Press: public_link
    Press->>RF: get_doc("Remote File", remote_private_file)
    RF-->>Press: private_link
    alt site.remote_config_file exists
        Press->>RF: get("Remote File", remote_config_file).get_content()
        RF-->>Press: config dict (parsed JSON)
        Press->>Utils: sanitize_config(config_content)
        Utils-->>Press: sanitized_config (blacklisted keys removed)
        Press->>Press: "sanitized_config.update({maintenance_mode: 0})"
    end
    Press->>Agent: create_agent_job("Restore Site", data incl. sanitized_config_content)
Loading
Prompt To Fix All With AI
Fix the following 3 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 3
press/agent.py:176
The import path `press.press.utils` does not exist — there is no `press/press/utils.py` file in the repository. `sanitize_config` lives in `press/utils/__init__.py`, so the correct import is `press.utils`. This will raise an `ImportError` at the start of every `restore_site` call, completely breaking site restoration.

```suggestion
		from press.utils import sanitize_config
```

### Issue 2 of 3
press/agent.py:180
`sanitized_config_content` is only assigned inside the `if site.remote_config_file:` block, but it is referenced unconditionally in the `data` dict below. When `site.remote_config_file` is absent (the common case for restores that don't include a config file), Python will raise `NameError: name 'sanitized_config_content' is not defined`, breaking every such restore. The pattern used for `database_link`, `public_link`, and `private_link` — initialise to `None` before the conditional — should be applied here too.

```suggestion
		public_link, private_link, database_link, sanitized_config_content = None, None, None, None
```

### Issue 3 of 3
press/agent.py:188
The other three remote file lookups all use `frappe.get_doc(...)`, but this one uses `frappe.get(...)`. While both may resolve identically in some Frappe versions, using the inconsistent form can introduce subtle runtime differences and makes the code harder to follow. Aligning with the established pattern in the same function reduces that risk.

```suggestion
			config_content = frappe.get_doc("Remote File", site.remote_config_file).get_content()
```

Reviews (1): Last reviewed commit: "fix(restore-with-files): Sanitize site c..." | Re-trigger Greptile

Comment thread press/agent.py Outdated
Comment thread press/agent.py Outdated
Comment thread press/agent.py Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented May 15, 2026

Codecov Report

❌ Patch coverage is 12.50000% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 49.72%. Comparing base (05a2842) to head (f8f30d1).
⚠️ Report is 4 commits behind head on develop.

Files with missing lines Patch % Lines
press/agent.py 0.00% 6 Missing ⚠️
press/press/doctype/site/site.py 50.00% 1 Missing ⚠️

❌ Your patch check has failed because the patch coverage (12.50%) is below the target coverage (75.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #6441      +/-   ##
===========================================
- Coverage    49.79%   49.72%   -0.07%     
===========================================
  Files          945      945              
  Lines        78390    78397       +7     
  Branches       355      355              
===========================================
- Hits         39031    38980      -51     
- Misses       39336    39394      +58     
  Partials        23       23              
Flag Coverage Δ
dashboard 59.71% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@regdocs regdocs force-pushed the fix-restore-with-files-flow branch from 5303609 to 5ec67e8 Compare May 15, 2026 08:45
@regdocs regdocs force-pushed the fix-restore-with-files-flow branch from 5ec67e8 to f8f30d1 Compare May 15, 2026 08:48
@phot0n
Copy link
Copy Markdown
Member

phot0n commented May 20, 2026

related? #2213

@regdocs
Copy link
Copy Markdown
Member Author

regdocs commented May 20, 2026

@ph0ton yeah, thanks for the pointer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add config option for restore site endpoint

2 participants