refactor: Align database transactions with Frappe standards to improve performance#404
Merged
Conversation
-Added safe_commit, commit_if_background, and transaction_checkpoint helpers to standardize database persistence across HTTP and background worker contexts without breaking Frappe atomicity.
-Delegated transaction management to Frappe's automatic request lifecycle.
-Delegated transaction management to Frappe's automatic request lifecycle.
- Delegated transaction management to Frappe's automatic request lifecycle.
-Delegated transaction management to Frappe's automatic request lifecycle.
-Delegated transaction management to Frappe's automatic request lifecycle.
-Delegated transaction management to Frappe's automatic request lifecycle.
-Replaced raw db commits with commit_if_background to support both sync API execution and future enqueued background jobs safely.
- Replaced raw db commits with transaction_checkpoint helpers. - Removed unreachable dead code at line 467. - Added # nosemgrep flags to justified background summarization jobs.
-Replaced raw db commits with transaction_checkpoint(reason='agent_streaming_progress') to safely expose UI progress during long-running streams.
-Introduced a semgrep rule (huf-no-explicit-frappe-commit) that will fail CI if any developer adds a raw frappe.db.commit() in non-allowlisted files.Requires the use of transaction helpers or a explicit '# nosemgrep' flag.
- Created scripts/check_explicit_commits.py to mirror the semgrep logic locally. - Added hook to .pre-commit-config.yaml to automatically check for explicit frappe.db.commit() usage before developers can commit code.
Sanjusha-tridz
marked this pull request as draft
July 17, 2026 12:52
This was referenced Jul 20, 2026
Contributor
|
Intake review complete (dossier on file) — disposition: technically sound, mergeable after review of risks R1–R5. The two one-line follow-ups the dossier flagged are implemented as stacked draft #410 (webhook POST-only per R1; consistent permission enforcement per R2). Recommend landing #404 before the overlapping flow/agent PRs (#397/#362/#403) to minimize rebase churn. |
Sanjusha-tridz
marked this pull request as ready for review
July 20, 2026 12:47
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR addresses recent audit feedback regarding excessive and redundant
frappe.db.commit()calls throughout the HUF codebase.Previously, explicit commits were used inside
@frappe.whitelist()API routes and tool handlers. Because the Frappe framework automatically manages transaction boundaries and commits at the end of every HTTP request, these explicit calls resulted in "two commits for a single transaction." As the platform scales, this double-writing creates significant lock contention, unnecessary database I/O, and degrades API latency.This PR strictly aligns the codebase with Frappe transaction standards while preserving the intermediate persistence required for real-time UI polling in our Agent streaming and Flow Engine loops.
Key Changes
frappe.db.commit()calls from@frappe.whitelist()endpoints (e.g.,permissions_api,prompt_api,flow_api,mcp_oauth).huf/ai/transaction.pycontainingcommit_if_background()andtransaction_checkpoint().commit_if_background(). This ensures that when tools run synchronously via API, they do not break transaction boundaries, but when enqueued in background workers, they safely persist state.transaction_checkpoint(reason="agent_streaming_progress")to explicitly document intentional partial writes for UI polling.frappe.local._realtime_login the newsafe_commit()helper to prevent a known Frappe background-workerAttributeError.agent_integration.py.check_explicit_commits.pyscript and wired it to.pre-commit-config.yamlto block developers from adding new raw commits locally..semgrep.ymlrules to enforce transaction standards in the CI pipeline.Impact & Performance
Testing Performed
frappe.db.commit()additions.