Repository navigation
fix: reject unsafe duet alignment previews - #15
Conversation
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| ErrorProne | 1 high |
🟢 Metrics -328 complexity · 0 duplication
Metric Results Complexity -328 Duplication 0
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
There was a problem hiding this comment.
ℹ️ No critical issues — minor suggestions inline.
Reviewed changes
This run reviewed PR #15: "fix: reject unsafe duet alignment previews". The PR adds a plausibility guard in routes.py that rejects word/syllable alignment previews with non-monotonic timestamps or >8s gaps within a lyric line (resetting the line window when new_line is true), returning HTTP 422 with a short error list. It also adds two regression tests in tests/test_helpers.py covering the duet example and a boundary case where the gap occurs across a line break.
- Guard implementation:
_alignment_plausibility_errorschecks for empty/non-list/shape errors, enforces monotonicstarttimes (returns immediately on violation), and for word/syllable granularity flags a gap > 8.0s between consecutive segments within the same line (line window reset onnew_line). Called only fromls_alignwhen the alignment server returns a 200 response. - Test coverage: Two new tests covering (a) the reported duet case (gap > 8s within line) and (b) a legitimate large pause across a
new_lineboundary. - Integration: The 422 is surfaced only on the read-only
/alignpreview path; the UI'slsAlign()shows the error message but discardsdata.details(the guard already computesdetails: plausibility_errors, but the frontend never renders them). The write paths/saveand/save-lyricsremain independent shape validators.
Notes on efficacy (from specialist investigation)
The new guard does reject all three signatures from the linked issue (the reported How/can/you at 14.49s gap, the second singer line with ~16s gap, and the combined 68-line case). However, the heuristic has several inherent edge cases worth acknowledging:
- The 8.0s threshold is arbitrary; same defect passes if the stray word is < 8.0s away or if consecutive pairs stay under threshold even across a very long "line" (window never reset except by
new_line). - The guard trusts the server's
new_linemarkers to delimit lines; if the server mis-flags a stray word asnew_line, the check resets and the gap goes undetected. Conversely, if a legitimate multi-line stanza lacksnew_linemarkers (server omits them), the guard treats the whole thing as one line and can flag a large intentional pause as implausible. - The guard only evaluates
start(monotonicity and gap); it does not inspectend, soend < start(negative duration) orend == start(zero duration) pass the guard but produced < 0ord == 0when converted. The/saveroute writesd = seg.end - seg.startwith nod <= 0filter (unlike/save-lyricswhich dropsd <= 0), while the host loader (per docstring) tolerates non-positive durations. The guard is also bypassed entirely forgranularity == "line"(returns empty errors by design at L279-280) — non-monotonic or malformed starts at line granularity pass through unflagged by this helper. - Defense-in-depth: the guard runs only in
ls_align(preview). Hand-crafted POSTs to/saveor/save-lyricsbypass it entirely (both already have their own shape validation; only/save-lyricsnormalizes/sorts). The shipped UI cannot reach an ungated alignment-derived save because_lsAlignmentResultis only populated from a successful gated/alignresponse, so the practical risk is API surface, not the UI flow.
Recommended follow-ups (non-blocking)
- Surface the computed
detailsarray in the UI when a 422 occurs (screen.js:200-206currently only showsdata.error). Exposing which specific segment/gap triggered rejection would help users understand and recover (e.g. try line granularity). - Consider extending the guard to also sanity-check
end(e.g.end >= startand finite) and/or add a soft per-line span cap in addition to the consecutive-pair gap, or document the 8.0s assumption. At minimum, add tests for the edge cases (new_linereset, sub-8s gap, granularity='line' bypass,end < start). - Either also gate
/savewith the same plausibility check (defense-in-depth for direct API calls) or explicitly document that/alignis the only plausibility-gated path. Given/savealready validates shape/finite values and the UI flow is gated, this is a design choice rather than a correctness bug for the shipped path.
ℹ️ Nitpicks
routes.py:297-301— consider extracting the 8.0s constant (e.g.MAX_GAP_WITHIN_LINE_SECONDS = 8.0) to make it configurable/testable and self-documenting.tests/test_helpers.py:21-29— consider adding a quick assertion on the exact error string format to prevent accidental regression of the message, or add cases for the reset/bypass behaviors noted above.- The frontend drops diagnostics:
screen.js:204uses onlyesc(data.error). If you keep the 422 shape, renderdata.details?.join('\n')(escaped) in the error panel so users see which segment triggered the rejection.
Big Pickle (free) | 𝕏
|
Addressed the findings in review Applied
Deliberately not changed
|
Follow-up to the plausibility guard: the 422 already carried a `details` list that the UI dropped, and the per-segment checks skipped `end` and skipped `line` granularity entirely. - render `data.details` (escaped) in the align error panel so a rejected preview names the offending segments - reject non-numeric, non-finite, or reversed `end` values — /save writes `d = end - start` with no `d <= 0` filter - keep the within-line gap rule word/syllable-only (it needs the server's `new_line` markers) but apply the per-segment and monotonicity checks at every granularity - extract MAX_GAP_WITHIN_LINE_SECONDS and document the assumption - cover sub-threshold gaps, unusable end times, and the line-granularity split with tests
CodeFactor flagged this function as a Complex Method (routes.py#L283-330). Extract span parsing (_parse_segment_span) and the within-line gap check (_within_line_gap_error) into their own helpers so the main function reads as a sequence of checks. Pure refactor - same error strings, same behavior, verified against the existing test_helpers.py cases. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01U2BDXmZrLF1kq7RE823NHw
There was a problem hiding this comment.
ℹ️ No critical issues — one consistency nit inline. The refactor is behaviour-preserving and the prior follow-ups are all addressed.
Reviewed changes
This run reviewed the two commits pushed since the previous pullfrog review of #15 (5486526, 7551137).
- Surface the rejection details:
screen.jsnow renders the 422detailslist as an escaped<ul>inside the existing error panel, so a rejected preview names the offending segments instead of just saying "unsafe to save". - Validate segment
endtimes:_parse_segment_spanrejects non-numeric and non-finiteendas well asend < start, and these checks now run at every granularity rather than being short-circuited by the oldgranularity not in {"word", "syllable"}early return. The within-line gap rule stays word/syllable-only, which is right — it is the one rule that needs the server'snew_linemarkers. - Split the guard for readability:
_parse_segment_spanand_within_line_gap_errorextracted as module-level helpers withMAX_GAP_WITHIN_LINE_SECONDSpromoted to a named constant. I checked the extraction against the previous inline logic:gap <= 8.0is the exact complement of the oldgap > 8.0for the finitestartvalues already guaranteed by theisfinitecheck, andend == start, overlapping word spans, and equal consecutivestartvalues all still pass as before. - Test coverage: four new assertions — the segment number pinned in the duet case, a 7.5s sub-threshold gap allowed, the three
endfailure modes with exact message substrings, andlinegranularity allowing a 20s inter-line gap while still rejecting out-of-order lines. Each one can fail against a plausible regression.python3 -m pytest -q→ 33 passed.
The one thing worth a decision from you is below.
Big Pickle (free) | 𝕏
|
Addressed the one open thread from this review. The finding was correct. Fix (commit Tests: the three within-line-gap tests were written without A re-read of the review before finishing turned up no new threads. Task list (5/5 completed)
|

Fixes get-flashbacks/feedBack#98.
Summary
endbeforestart) —/savewritesd = end - startwith nod <= 0filternew_linemarkersdetailsthe 422 already carried, so a rejected preview names the offending segmentsHow/can/youat 0.031, 0.11, 14.49 seconds)Validation
python3 -m pytest -q— 33 passednode --check screen.jsBig Pickle(free) | 𝕏