Skip to content

fix(timeline): preserve partially anchored regions - #307

Open
arhxam wants to merge 1 commit into
getopenscreen:mainfrom
arhxam:codex/preserve-partial-anchors
Open

fix(timeline): preserve partially anchored regions#307
arhxam wants to merge 1 commit into
getopenscreen:mainfrom
arhxam:codex/preserve-partial-anchors

Conversation

@arhxam

@arhxam arhxam commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • use the shared complete-anchor predicate when removing clip-linked modifier regions
  • preserve legacy or partially populated regions that only carry a clipId
  • cover multi-asset removal so surviving clips are resequenced and their anchored regions are rederived

Related issue

Fixes #249

Type of change

  • Bug fix
  • Feature
  • Enhancement
  • Documentation
  • Refactor / maintenance
  • Performance
  • Security

Release impact

  • Patch
  • Minor
  • Major / breaking change
  • No release note needed

Desktop impact

  • Windows
  • macOS
  • Linux
  • Installer / packaging
  • Not platform-specific

Screenshots / video

Not applicable; this changes timeline document behavior and regression coverage.

Testing

  • npx vitest --run src/lib/ai-edition/document/timeline.test.ts electron/ai-edition/document-service.test.ts (92 passed)
  • npm run test (1,679 passed, 1 skipped)
  • npx tsc --noEmit
  • npx tsc -p tsconfig.test.json --noEmit
  • npx biome check src/lib/ai-edition/document/timeline.ts src/lib/ai-edition/document/timeline.test.ts electron/ai-edition/document-service.test.ts
  • npm run docs:check
  • npm run i18n:check
  • npm run build-vite

Authored with Codex assistance and manually verified against the issue's edge cases.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed clip removal so unrelated or partially defined zoom regions remain intact.
    • Preserved raw timing and clip references for zoom regions without complete source anchors.
    • Corrected timeline resequencing and zoom timing recalculation after removing an asset.
  • Tests

    • Added regression coverage for asset and clip removal scenarios.

@arhxam
arhxam requested a review from EtienneLescot as a code owner August 8, 2026 18:11
@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 8430e415-039e-44ee-9ddc-11349c5c2aa1

📥 Commits

Reviewing files that changed from the base of the PR and between 4e7a85b and 732d2b7.

📒 Files selected for processing (3)
  • electron/ai-edition/document-service.test.ts
  • src/lib/ai-edition/document/timeline.test.ts
  • src/lib/ai-edition/document/timeline.ts

📝 Walkthrough

Walkthrough

removeClip now preserves regions without complete source anchors. New tests cover partial-anchor preservation and multi-asset removal, including survivor resequencing and adjusted zoom timing.

Changes

Clip removal behavior

Layer / File(s) Summary
Removal behavior and regression coverage
src/lib/ai-edition/document/timeline.ts, src/lib/ai-edition/document/timeline.test.ts, electron/ai-edition/document-service.test.ts
removeClip preserves partially anchored regions. Tests cover clip removal and multi-asset removal with resequenced clips and recalculated zoom timing.

Estimated code review effort: 3 (Moderate) | ~15–30 minutes

Possibly related PRs

Suggested reviewers: etiennelescot

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: preserving partially anchored timeline regions.
Description check ✅ Passed The description follows the template and includes the summary, issue, change type, release impact, platform impact, and validation details.
Linked Issues check ✅ Passed The changes satisfy both objectives in [#249] by aligning anchor filtering and testing multi-asset resequencing with timing rederivation.
Out of Scope Changes check ✅ Passed The implementation and regression tests remain within the timeline removal and multi-asset coverage scope defined by [#249].
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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.

@EtienneLescot EtienneLescot 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.

Reviewed at the PR head (732d2b75). Verdict: ship with nits.

The one-line change is the right fix and it is in the right place. I verified removeClip at src/lib/ai-edition/document/timeline.ts:939 is the only site with that shape, and — importantly — that the trim filter three lines above at :935 must not get the same treatment: a trim's complete v5 anchor is clipId plus its own startSec/endSec (trim-mapping.ts:46, schema/index.ts:225-234), so bare clipId is correct there. Good instinct not to touch it.

I also confirmed the defect is unreachable from any producer on current main — every modifier-creation path (anchorRegionsWithDerivedMs, replacePillSpan, upgradeV4DocumentToV5, useTimeline.addAnnotation/suggestZooms) writes all three anchor fields or none. So this is defensive coverage for hypothetical on-disk data, which matches how #249 described it.

Verification: 851/851 tests pass in src/lib/ai-edition + electron/ai-edition + src/components/ai-edition; both typecheck configs, biome, docs and i18n checks clean. Reverting timeline.ts to main makes timeline.test.ts > preserves a bare clipId… fail as expected — that test genuinely pins the fix.

1. Two divergent copies of the complete-anchor predicate

isAnchored (document/timeline.ts:30-39) carries the comment "One definition, so 'is this anchored?' can never be asked two different ways" — but hasCompleteClipAnchor (timeline/timelineMap.ts:418-428) is a second copy, and they are not equivalent:

  • isAnchored: !!region.clipId && region.sourceStartSec !== undefined
  • hasCompleteClipAnchor: typeof clipId === "string" && typeof sourceStartSec === "number"

A region with sourceStartSec: null — reachable for any document not freshly re-parsed by zod, and both the store and the agent tools mutate in memory — is anchored to removeClip/rederiveRegionMs and unanchored to the export path. rederiveAnchoredRegion then runs Math.max(null, clip.sourceStartSec) and silently rewrites the region to the clip's start, while the exporter keeps using its raw ms. Preview and export disagree.

The PR description says this "use[s] the shared complete-anchor predicate" — there isn't one yet. Worth exporting hasCompleteClipAnchor from timelineMap.ts (it already owns ClipAnchored/RegionClipAnchor) and deleting isAnchored, so the comment becomes true.

2. The branch #249 actually described is untested

The new test only covers newClips.length > 0. The last-clip branch changes behaviour here too and nothing pins it — I checked with a throwaway probe: single clip clip_a, one zoom {id:"partial", clipId:"clip_a", startMs:500, endMs:1500} with no source range. On main the zoom is deleted; at PR head it survives. Neither test exercises that path, so the newClips.length === 0 ? … : … ternary can be refactored back to the old semantics with a green suite.

In the existing "drops every modifier anchored to the last remaining clip" test (timeline.test.ts:1394), adding makeZoom({ id: "partial_zoom", clipId: "clip_a", sourceStartSec: undefined, sourceEndSec: undefined }) alongside legacy_zoom and asserting it survives would close this.

3. document-service.test.ts passes on main unmodified

The 85-line test is 79% of the PR's test lines and pins nothing about this fix — zoom_b_2 is fully anchored and the removed clips are a_1/a_2, so both the old and new filter keep it (verified by running the file against main's timeline.ts). It is legitimate as the characterization coverage #249 part 2 asked for, but the PR body lists it under this fix's regression coverage, which will mislead whoever next breaks the predicate. Either say so in a comment above it, or add a second zoom with clipId: "a_2" and no source range and assert it survives removeAsset — that would route the actual fix through removeAsset.

4. The asymmetry needs the comment #249 asked for

A reader sees !(isAnchored(region) && region.clipId === clipId) on :939 and (t) => t.clipId !== clipId on :935 with nothing saying the difference is deliberate, so the obvious "cleanup" reintroduces #249. #249 explicitly asked for this ("Either mirror isAnchored in the filter, or state in the comment…") and the code half shipped without the prose half. Every other non-obvious decision in this file carries a why-comment. Something like:

A region carrying only a clipId and no source range is NOT anchored (isAnchored) — it is still placed by its RAW ms, so a clip deletion does not own it. Trims are the opposite: clipId alone IS their complete anchor (trimAppliesToClip).

5. Trade-off worth stating explicitly (no code change)

A preserved partial-anchor region becomes unreachable but immortal: with clips clip_1 [0–10s] and clip_2 [10–20s] and a zoom carrying bare clipId:"clip_2" at raw 12000–14000ms, deleting clip_2 leaves the zoom at 12–14s on a 10s timeline — off the ruler, no clickable pill, dropped by projectRegionsToSource, and re-emitted by both rederiveRegionMs and reconcileRegionsAfterReplace. If the user then hits Restore full timeline, anchorRegionsWithDerivedMs re-anchors it from those stale raw ms onto whatever footage now sits at 12–14s — the "slid region" class the docblock at timeline.ts:696-717 exists to prevent.

This is the same treatment fully-unanchored legacy regions already get, so it is consistent and defensible. Flagging it so it is a decision rather than an accident.

Nits

  • document/timeline.ts:938-943 — with the predicate tightened, the pre-filter is redundant on the newClips.length > 0 path: rederiveRegionMs (:305-306) already drops every anchored region whose clipId is absent from newClips, a strict superset. Costs a second walk of all four region collections plus a second document spread per clip delete. Only worth churning if you are editing these lines for the comment above anyway.
  • document-service.test.ts:302-352 — four hand-rolled 11-line clip literals differing only in id/assetId/four numbers, for a 6-line assertion. A local const clip = (id, assetId, src, tl) => ({…}) would cut ~40 lines.
  • Pre-existing, surfacing because the changed line lives inside its callback: mapAllRegionCollections (:243-272) has no Array.isArray guard on the legacyEditor envelopes, unlike upgradeV4DocumentToV5 (schema/index.ts:577-582). Since legacyEditorSchema is z.object({}).passthrough(), a project file with legacyEditor: { speedRegions: "oops" } loads fine and then throws regions.filter is not a function on the first clip delete/move/duplicate.

Nothing blocking. Happy to merge once the comment in §4 lands — the rest can be follow-ups.

@EtienneLescot

Copy link
Copy Markdown
Collaborator

Filed the legacyEditor array-guard gap from my review as #356 — pre-existing, not blocking this PR.

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.

removeClip cascade: mirror isAnchored, and cover multi-asset removeAsset

2 participants