The user wants a draft policy or guideline document for "trunk-based development" (TBD).
The specific requirements are:
- Small PRs
- Protect main branch
- Flaky test triage
- Merge queues
I need to structure this as a coherent engineering standard or playbook entry. It should be actionable, clear, and cover the requested points.
Target Audience: Software Engineers, DevOps, Tech Leads.
Tone: Professional, directive, encouraging best practices.
Structure:
- Introduction/Philosophy: Briefly define TBD and its benefits (speed, stability).
- Core Principles:
- Small PRs (The "what" and "how")
- Branch Protection (The safety net)
- Merge Queues (The traffic control)
- Operational Processes:
- Flaky Test Triage (The maintenance/cleanliness)
- Summary/Checklist: A quick reference.
Drafting - Section by Section:
Refining the Content:
- Small PRs: Emphasize "incremental" changes.
- Protect Main: Use standard terms like "Branch Protection Rules."
- Merge Queues: Explain why this combines with Small PRs and Protected branches (serialization).
- Flaky Tests: This needs a protocol. 1. Detect. 2. Quarantine. 3. Ticket. 4. Fix.
Formatting:
Use Markdown. Use bold headings. Use bullet points.
Self-Correction/Review during drafting:
- Initial thought: Just list the rules.
- Correction: Context matters. Why do we need a merge queue? Because rebasing manually is painful. Why flaky triage? Because false positives kill velocity. I will add a "Why?" section for each.
Final Polish:
Ensure the tone strikes a balance between rigorous engineering standards and developer experience (DX).
Let's write the response based on this structure.
- Title: Trunk-Based Development Playbook
- Section 1: Code Lifecycle (Small PRs)
- Section 2: Stability Controls (Protect Main & Merge Queues)
- Section 3: Hygiene (Flaky Tests)
This looks solid. I will produce the output now.
Here is a draft policy for implementing Trunk-Based Development (TBD) focusing on velocity, stability, and code quality.
Engineering Standard: Trunk-Based Development
Objective: To maximize developer velocity and minimize integration risk by keeping branches short-lived and the main branch always deployable.
1. The Strategy: Small, Short-Lived PRs
We avoid "feature branches" that live for days or weeks. Instead, all code changes are performed in small batches.
- Scope: A Pull Request (PR) should represent a single logical change.
- Good: A new API endpoint, a refactored module, a bug fix.
- Bad: "WIP: Whole new user auth system" (this should be broken into 5-10 smaller PRs).
- Lifespan: PRs must be open for less than 24 hours. If a PR takes days to write, it is too big.
- Incremental Changes: Large features must be built behind feature flags (toggles) or "dark launches," allowing incomplete code to be merged into
main without affecting production users.
2. The Safety Net: Protect main
The main branch is the source of truth and must always be in a deployable state. Direct pushes are forbidden.
Branch Protection Rules:
- No Direct Pushes: All changes must go through a Pull Request.
- Required Reviews: At least 1 approval is required.
- Status Checks: The build must pass (compile + test suite) before merging is allowed.
- No Force Pushes: History must remain immutable.
3. The Traffic Control: Merge Queues
As the team grows, "merge skew" becomes common: Developer A’s PR passes tests, but by the time they click merge, Developer B has merged a conflicting change, breaking main.
We utilize a Merge Queue (e.g., GitHub Merge Queue, Mergify, or Travis CI):
- Approval: A PR is reviewed and approved.
- Queue: The author adds the PR to the merge queue.
- Validation: The CI system rebases the PR against the latest
main and runs the test suite.
- Auto-Merge: If tests pass, the PR is automatically merged.
- Failure: If tests fail, the PR is kicked out of the queue for the author to fix.
Rule: Do not use "Update branch" buttons manually. Let the queue handle the final validation to ensure main never breaks.
4. The Hygiene Factor: Flaky Test Triage
Trunk-Based Development relies on trusting the test suite. Flaky tests (tests that pass/fail non-deterministically) destroy that trust and clog the merge queue.
Zero-Tolerance Policy for Flakiness:
- Identification: If a test fails in the Merge Queue but passes locally or on a retry, it is flagged as flaky.
- Quarantine: The test is immediately disabled (quarantined) to unblock the merge queue.
- Ticket: A "Critical" bug ticket is created to fix the test.
- Ownership: The author of the failing PR (or the team on rotation) is responsible for fixing or re-enabling the test within 24 hours.
Summary Checklist
| Practice | Requirement |
|---|
| Branching | Branch from main, merge back to main within 1 day. |
| PR Size | Small enough to be code-reviewed in < 15 minutes. |
| Merge Method | Use Merge Queue (Rebase and Merge). |
| Broken Main | If main breaks, stop everything and fix it immediately. |
| Flaky Tests | Disable immediately, fix later. |