-
Notifications
You must be signed in to change notification settings - Fork 0
Fix create-song navigation race #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
johannesschiessl
wants to merge
2
commits into
main
Choose a base branch
from
codex/fix-create-song-handoff
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
| import { Schema } from "effect"; | ||
| import { Song, SongName, type ShowId, type SongId } from "@showtime/contracts"; | ||
| import { makeCreatedSongHandoff } from "./CreatedSongHandoff"; | ||
|
|
||
| const showId = "show_0123456789abcdef" as ShowId; | ||
| const song = Schema.decodeUnknownSync(Song)({ | ||
| id: "song_0123456789abcdef" as SongId, | ||
| name: "", | ||
| artist: "", | ||
| mixAssignments: [], | ||
| createdAt: "2026-07-24T12:00:00.000Z", | ||
| updatedAt: "2026-07-24T12:00:00.000Z", | ||
| }); | ||
|
|
||
| describe("CreatedSongHandoff", () => { | ||
| it("retains a confirmed creation until the streamed snapshot observes it", () => { | ||
| const handoff = makeCreatedSongHandoff(); | ||
| const baselineSnapshot = {}; | ||
|
|
||
| handoff.remember(showId, song, undefined, baselineSnapshot); | ||
| expect(handoff.find(showId, song.id)).toBe(song); | ||
|
|
||
| handoff.reconcile(showId, [], baselineSnapshot); | ||
| expect(handoff.find(showId, song.id)).toBe(song); | ||
|
|
||
| handoff.reconcile(showId, [{ ...song, pending: true }], {}); | ||
| expect(handoff.find(showId, song.id)).toBe(song); | ||
|
|
||
| handoff.reconcile(showId, [song], {}); | ||
| expect(handoff.find(showId, song.id)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("can forget a creation after it is deleted before reconciliation", () => { | ||
| const handoff = makeCreatedSongHandoff(); | ||
|
|
||
| handoff.remember(showId, song, undefined, {}); | ||
| handoff.forget(showId, song.id); | ||
|
|
||
| expect(handoff.find(showId, song.id)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("preserves the intended insertion position while the snapshot is delayed", () => { | ||
| const handoff = makeCreatedSongHandoff(); | ||
| const anchor = { | ||
| ...song, | ||
| id: "song_fedcba9876543210" as SongId, | ||
| name: SongName.make("Anchor"), | ||
| }; | ||
|
|
||
| handoff.remember(showId, song, anchor.id, {}); | ||
|
|
||
| expect(handoff.provisionalNumber(showId, song.id, [anchor])).toBe(2); | ||
| }); | ||
|
|
||
| it("expires a confirmed creation when a newer authoritative snapshot omits it", () => { | ||
| const handoff = makeCreatedSongHandoff(); | ||
| const baselineSnapshot = {}; | ||
|
|
||
| handoff.remember(showId, song, undefined, baselineSnapshot); | ||
| handoff.reconcile(showId, [], {}); | ||
|
|
||
| expect(handoff.find(showId, song.id)).toBeUndefined(); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import type { ShowId, Song, SongId } from "@showtime/contracts"; | ||
|
|
||
| const keyFor = (showId: ShowId, songId: SongId) => `${showId}:${songId}`; | ||
|
|
||
| export const makeCreatedSongHandoff = () => { | ||
| const confirmed = new Map< | ||
| string, | ||
| { | ||
| readonly song: Song; | ||
| readonly insertAfterSongId?: SongId; | ||
| readonly baselineSnapshot: object; | ||
| } | ||
| >(); | ||
|
|
||
| const remember = ( | ||
| showId: ShowId, | ||
| song: Song, | ||
| insertAfterSongId: SongId | undefined, | ||
| baselineSnapshot: object, | ||
| ) => { | ||
| confirmed.set(keyFor(showId, song.id), { song, insertAfterSongId, baselineSnapshot }); | ||
| }; | ||
|
|
||
| const find = (showId: ShowId, songId: SongId) => confirmed.get(keyFor(showId, songId))?.song; | ||
|
|
||
| const provisionalNumber = (showId: ShowId, songId: SongId, syncedSongs: ReadonlyArray<Song>) => { | ||
| const entry = confirmed.get(keyFor(showId, songId)); | ||
| if (!entry?.insertAfterSongId) return syncedSongs.length + 1; | ||
| const anchorIndex = syncedSongs.findIndex((song) => song.id === entry.insertAfterSongId); | ||
| return anchorIndex < 0 ? syncedSongs.length + 1 : anchorIndex + 2; | ||
| }; | ||
|
|
||
| const forget = (showId: ShowId, songId: SongId) => { | ||
| confirmed.delete(keyFor(showId, songId)); | ||
| }; | ||
|
|
||
| const reconcile = ( | ||
| showId: ShowId, | ||
| songs: ReadonlyArray<Song & { readonly pending?: boolean }>, | ||
| snapshot: object, | ||
| ) => { | ||
| if (songs.some((song) => song.pending)) return; | ||
|
|
||
| const syncedIds = new Set(songs.map((song) => song.id)); | ||
| for (const [key, entry] of confirmed) { | ||
| if ( | ||
| key.startsWith(`${showId}:`) && | ||
| (syncedIds.has(entry.song.id) || snapshot !== entry.baselineSnapshot) | ||
| ) { | ||
| confirmed.delete(key); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| return { remember, find, forget, provisionalNumber, reconcile } as const; | ||
| }; | ||
|
|
||
| /** | ||
| * Bridges the short interval between a successful create response and the next | ||
| * full songs snapshot. Entries are discarded as soon as that snapshot observes | ||
| * them, so later deletions cannot be hidden by stale client state. | ||
| */ | ||
| export const createdSongHandoff = makeCreatedSongHandoff(); | ||
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: Pending optimistic snapshots are not retained here:
reconcilereceives rawsyncedSongs, whilependingexists only on the separate optimisticsongsatom. This guard never returns in production, so route handoff can still be discarded on the next differing raw snapshot; pass optimistic pending state into reconciliation (or remove this ineffective guard and its misleading test).Prompt for AI agents