Skip to content

fix(deferredwork): keep flat appends visible after canonical entries#274

Open
Haven2026 wants to merge 4 commits into
bmad-code-org:mainfrom
Haven2026:codex/fix-deferred-flat-boundary
Open

fix(deferredwork): keep flat appends visible after canonical entries#274
Haven2026 wants to merge 4 commits into
bmad-code-org:mainfrom
Haven2026:codex/fix-deferred-flat-boundary

Conversation

@Haven2026

@Haven2026 Haven2026 commented Jul 23, 2026

Copy link
Copy Markdown

Summary

  • stop canonical entry spans before the flat - source_spec: format emitted by inner BMAD sessions
  • keep those later flat findings visible to parse_legacy() even after a canonical append
  • make append_entry() emit the canonical location: field with a safe n/a default

Verification

  • red before fix: 2 failures (flat append invisible; writer missing location)
  • uv run pytest tests/test_deferredwork.py -q — 33 passed
  • GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=core.fsmonitor GIT_CONFIG_VALUE_0=false uv run pytest tests/test_deferredwork.py tests/test_sweep.py -q — 104 passed
  • uv run ruff check src/bmad_loop/deferredwork.py tests/test_deferredwork.py — passed
  • real bmad-loop sweep --dry-run fixture reports 1 canonical open plus 1 visible legacy entry

The process-level Git config only disables a local macOS fsmonitor socket that otherwise cannot be copied by the test fixture; it does not affect parser behavior.

Summary by CodeRabbit

  • Bug Fixes
    • Improved Deferred Work ledger parsing so embedded “flat appender” blocks no longer get included in canonical ### DW-* entry bodies.
    • Newly generated Deferred Work entries now include a location field (default: “n/a”), and append_entry writes it when provided.
    • Added stricter input validation for append_entry, mark_done, and append_decision (rejects invalid status/severity and malformed/invalid dates and multiline inputs).
  • Tests
    • Added regression coverage for canonical vs flat-appender parsing boundaries, including CRLF handling.
    • Added validation tests ensuring invalid inputs are rejected without modifying the ledger.

@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 19870e1a-79bc-4f37-a337-58cbcdb0b7ea

📥 Commits

Reviewing files that changed from the base of the PR and between 582e566 and 9c6d5c8.

📒 Files selected for processing (2)
  • src/bmad_loop/deferredwork.py
  • tests/test_deferredwork.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/bmad_loop/deferredwork.py

Walkthrough

The deferred work parser now stops canonical entry bodies before flat appender blocks. Newly appended entries include an optional location field and validate fields, dates, statuses, and severities before writing.

Changes

Deferred work ledger handling

Layer / File(s) Summary
Flat finding boundary parsing
src/bmad_loop/deferredwork.py, tests/test_deferredwork.py
parse_ledger() separates flat - source_spec: blocks from canonical entries, while tests cover legacy visibility, CRLF input, and non-flat bullets.
Canonical entry output and validation
src/bmad_loop/deferredwork.py, tests/test_deferredwork.py
append_entry() accepts and writes location, defaults it to n/a, and rejects multiline, empty, noncanonical status, invalid date, and invalid severity values without writing.
Completion and decision validation
src/bmad_loop/deferredwork.py, tests/test_deferredwork.py
mark_done() and append_decision() reject line breaks and impossible calendar dates while leaving the ledger unchanged.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Suggested reviewers: pbean

Poem

A rabbit guards each ledger line,
Flat clues hop out in neat design.
Dates and fields must all behave,
So clean entries cross the stave.
Hop, hop—validation saves the day!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main parsing fix for flat appends after canonical entries.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/bmad_loop/deferredwork.py (1)

151-175: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Reject multiline location values before serialization.

location is written verbatim into a line-oriented ledger. A value such as src/foo.py\nstatus: closed can inject fields or headings, changing parse_ledger() status, spans, and idempotency behavior. Reject \r/\n values and add a regression test for the invalid case.

Suggested guard
 def append_entry(
     path: Path,
     *,
     title: str,
     origin: str,
     source_spec: str,
     reason: str,
     location: str = "n/a",
     status: str = "open",
     severity: str | None = None,
 ) -> str | None:
+    if "\r" in location or "\n" in location:
+        raise ValueError("location must be a single line")
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/bmad_loop/deferredwork.py` around lines 151 - 175, Validate the location
argument in the deferred-work entry creation function before constructing or
writing the lines, rejecting any value containing carriage-return or newline
characters. Preserve normal single-line locations and add a regression test
confirming multiline location values are rejected without serializing an entry.
🧹 Nitpick comments (1)
tests/test_deferredwork.py (1)

434-434: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover caller-supplied locations.

This assertion only verifies the "n/a" default. Add a case with location="src/foo.py:12" to ensure supplied values are emitted unchanged.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/test_deferredwork.py` at line 434, Extend the test around the existing
"location: n/a" assertion to cover a caller-supplied location, such as
"src/foo.py:12", and verify that the generated body emits that value unchanged.
Reuse the same deferred-work construction and body-generation path exercised by
the existing assertion.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/bmad_loop/deferredwork.py`:
- Around line 151-175: Validate the location argument in the deferred-work entry
creation function before constructing or writing the lines, rejecting any value
containing carriage-return or newline characters. Preserve normal single-line
locations and add a regression test confirming multiline location values are
rejected without serializing an entry.

---

Nitpick comments:
In `@tests/test_deferredwork.py`:
- Line 434: Extend the test around the existing "location: n/a" assertion to
cover a caller-supplied location, such as "src/foo.py:12", and verify that the
generated body emits that value unchanged. Reuse the same deferred-work
construction and body-generation path exercised by the existing assertion.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 58067a47-7ec1-4027-8baa-d7c1d096a730

📥 Commits

Reviewing files that changed from the base of the PR and between 2fd4f1a and fbf7541.

📒 Files selected for processing (2)
  • src/bmad_loop/deferredwork.py
  • tests/test_deferredwork.py

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/bmad_loop/deferredwork.py`:
- Around line 21-24: Update FLAT_ENTRY_RE to accept both LF and CRLF line
boundaries by using \r?\n for each required line break, while preserving the
existing end-of-input handling. Add a parse_legacy() regression covering a CRLF
flat-appender block in a mixed ledger and verify the flat finding is returned
rather than masked by the canonical span.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e198e4bb-c9ae-4b51-a4cf-c21c570295d7

📥 Commits

Reviewing files that changed from the base of the PR and between fbf7541 and 582e566.

📒 Files selected for processing (2)
  • src/bmad_loop/deferredwork.py
  • tests/test_deferredwork.py

Comment thread src/bmad_loop/deferredwork.py Outdated

@pbean pbean left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this — and welcome! You found a real one. I reproduced it on current main: a flat
appender block after the last canonical ### DW-<n> entry is absorbed into that entry's
parse_ledger() span, parse_legacy() masks it out, and the finding becomes invisible to the
sweep — silently lost work. I've filed #304 to track it, and your parse_ledger boundary
truncation is exactly the right seam for the fix (it's the single source of spans that
parse_legacy's masking consumes). The strict three-line FLAT_ENTRY_RE matching the shape
step-04-review.md instructs is a good call too — a looser boundary could truncate mid-entry and
orphan a status: line, which fails in the worse direction.

Two asks before this can land:

1. Rebase onto current main. deferredwork.py was rewritten the day after you opened this
(PR #300): mark_done is now a thin wrapper over a new mark_done_many (single atomic write via
atomic_write_text), and new parse_declaration/classify helpers sit in the middle of the
file. The PR is currently conflicting; the flat-boundary hunk and the tests apply nearly cleanly,
the validation hunks don't.

2. Trim this PR to the flat-boundary fix (+ the location: writer change). Please keep:

  • FLAT_ENTRY_RE + the parse_ledger boundary truncation, with your three boundary tests
    (including the CRLF one — nice catch);
  • the append_entry location: field with the n/a default and its tests — that closes a real
    gap against the documented canonical shape in deferred-work-format.md.

And please drop the input-validation commits (9b2efe2f, 582e5662, 9c6d5c86) from this PR.
The injection vector they target is real, but raise-style validation in the mutators conflicts
with how the orchestrator has to fail on those paths: sweep.py passes LLM-authored triage
evidence into mark_done's note and decisions.py passes triage resolution/intent as
decision details, and validate_triage only strips — so a multiline value from a session would
now crash a live sweep mid-close, where the ledger-close doctrine is "never a gate, never crash
the run". There's also a placement trap post-rebase: the engine's story-close path calls
mark_done_many directly, so guards on mark_done alone would be inert exactly where they matter
most. I've split this out as #305 with the design that fits (reject at the validate_triage
schema boundary where errors become session feedback, sanitize line breaks at the writer
chokepoints) — it credits your commits as prior art, and it's yours if you'd like to take it.

Optional polish while you're in there: the boundary's first line accepts only - while the
legacy recognizer accepts [-*][ \t]+ bullets (_BULLET_RE/_FLAT_SOURCE_RE) — widening it
keeps the two in agreement about what counts as a flat block.

If you add Fixes #304 to the PR description it'll close the tracker on merge. CI on fork PRs
needs a maintainer approval per run — ping here (or just push) once rebased and I'll approve it.

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.

2 participants