diff --git a/apps/staged/src/lib/features/projects/ProjectSection.component.test.ts b/apps/staged/src/lib/features/projects/ProjectSection.component.test.ts index ce1f39713..7c42b9ec0 100644 --- a/apps/staged/src/lib/features/projects/ProjectSection.component.test.ts +++ b/apps/staged/src/lib/features/projects/ProjectSection.component.test.ts @@ -50,6 +50,10 @@ const { ), deleteProjectNote: vi.fn(async (_noteId: string): Promise => {}), cancelSession: vi.fn(async (_sessionId: string): Promise => {}), + // "Mark as read" on a note's context menu. + setProjectNoteRead: vi.fn( + async (_noteId: string, _read: boolean): Promise => null + ), listChildSessions: vi.fn(async (_sessionId: string): Promise => []), // What the note dialog's `#note:` references are built from once it opens. listChildNotes: vi.fn(async (_projectNoteId: string) => []), @@ -149,6 +153,7 @@ vi.mock('../../commands', async (importOriginal) => ({ import ProjectSection from './ProjectSection.svelte'; import { reactiveProps } from '../sessions/testStubs/reactiveProps.svelte'; import { sessionWakeupsStore } from '../../stores/sessionWakeups.svelte'; +import { replyDraftPresence } from '../sessions/replyDrafts'; const project = { id: 'project-1', @@ -296,6 +301,23 @@ function rowFor(sessionId: string): HTMLElement { return row; } +/** What the context menu holds for a session's row, found by the row's menu key. */ +function menuActionFor(sessionId: string): HTMLElement { + const key = rowFor(sessionId).dataset.contextMenuKey; + const action = document.querySelector( + `[data-testid="context-menu-action"][data-key="${key}"]` + ); + if (!action) throw new Error(`no context-menu action for ${sessionId}`); + return action; +} + +/** The row's menu item marking its note `state`, when the menu offers it. */ +function menuMarkButton(sessionId: string, state: 'read' | 'unread'): HTMLButtonElement | null { + return menuActionFor(sessionId).querySelector( + `[data-testid="menu-mark-${state}"]` + ); +} + function resumeButton(sessionId: string): HTMLButtonElement | null { return rowFor(sessionId).querySelector('[data-testid="row-resume"]'); } @@ -926,7 +948,10 @@ describe('ProjectSection sessions waiting on repo sessions', () => { content: '', }); - it('shows a session that waits before writing its note as waiting, with the badges of what it waits on', async () => { + /** Everything a row says in words: its title and its subtitle. */ + const rowText = (row: HTMLElement) => `${row.dataset.title} ${row.dataset.secondaryMeta}`; + + it('shows a session that waits before writing its note as running, with the badges of what it waits on', async () => { listNotes(unwritten); commandMocks.getSessionWakeup.mockImplementation(async () => armedWakeup('child-1', 'child-2', 'child-3') @@ -941,25 +966,48 @@ describe('ProjectSection sessions waiting on repo sessions', () => { // Asked for once: the wake-up was armed before this section was listening. expect(commandMocks.getSessionWakeup.mock.calls).toEqual([['session-waiting']]); const row = rowFor('session-waiting'); - expect(row.dataset.type).toBe('waiting-note'); - expect(row.dataset.title).toBe('Waiting on 2 repo sessions'); + // The row a running session has: the spinner's type, and the badges + // alone as the subtitle. Nothing on it says the session is waiting. + expect(row.dataset.type).toBe('generating-note'); + expect(row.dataset.title).toBe('Generating note…'); expect(row.dataset.secondaryMeta).toBe(''); + expect(rowText(row)).not.toMatch(/waiting/i); expect(row.dataset.childBadges).toBe('child-1:running child-2:paused'); // Staged resumes it; there is nothing for the user to resume. expect(resumeButton('session-waiting')).toBeNull(); - // No note to open yet, so the row opens the chat, which says what it waits on. - expect(row.querySelector('[data-testid="row-open"]')).not.toBeNull(); + // Like a running row, it opens the chat, which says what it waits on. + clickRow('session-waiting'); + expect(noteModal.props).toMatchObject({ + noteId: 'note-waiting', + sessionId: 'session-waiting', + chatOpen: true, + }); + + unmount(section); + }); + + it('shows a waiting session as running before its repo sessions are read, and with none in flight', async () => { + listNotes(unwritten); + commandMocks.getSessionWakeup.mockImplementation(async () => armedWakeup('child-1')); + const section = mountSection(); + await settle(); + + const row = rowFor('session-waiting'); + expect(row.dataset.type).toBe('generating-note'); + expect(row.dataset.secondaryMeta).toBe('Generating note'); + expect(row.dataset.childBadges).toBe(''); + expect(rowText(row)).not.toMatch(/waiting/i); unmount(section); }); - it('drops a badge when a session being waited on ends, and recounts', async () => { + it('drops a badge when a session being waited on ends, and stays running', async () => { listNotes(unwritten); commandMocks.getSessionWakeup.mockImplementation(async () => armedWakeup('child-1', 'child-2')); listChildren({ 'session-waiting': [childSession('child-1'), childSession('child-2')] }); const section = mountSection(); await settle(); - expect(rowFor('session-waiting').dataset.title).toBe('Waiting on 2 repo sessions'); + expect(rowFor('session-waiting').dataset.childBadges).toBe('child-1:running child-2:running'); listChildren({ 'session-waiting': [childSession('child-2')] }); await emitSessionStatus({ @@ -970,20 +1018,20 @@ describe('ProjectSection sessions waiting on repo sessions', () => { }); const row = rowFor('session-waiting'); - expect(row.dataset.type).toBe('waiting-note'); - expect(row.dataset.title).toBe('Waiting on 1 repo session'); + expect(row.dataset.type).toBe('generating-note'); + expect(rowText(row)).not.toMatch(/waiting/i); expect(row.dataset.childBadges).toBe('child-2:running'); unmount(section); }); - it('stops showing a session as waiting once its wake-up is gone', async () => { + it('stops showing a session as running once its wake-up is gone', async () => { listNotes(unwritten); commandMocks.getSessionWakeup.mockImplementation(async () => armedWakeup('child-1')); listChildren({ 'session-waiting': [childSession('child-1')] }); const section = mountSection(); await settle(); - expect(rowFor('session-waiting').dataset.type).toBe('waiting-note'); + expect(rowFor('session-waiting').dataset.type).toBe('generating-note'); // Stop waiting, here or in another window: the repo session keeps running. await emitWakeup('session-waiting', NO_WAKEUP); @@ -996,7 +1044,7 @@ describe('ProjectSection sessions waiting on repo sessions', () => { unmount(section); }); - it('says what a written note’s session waits on under its title', async () => { + it('shows a written note’s session as running while it waits, with its badges and no waiting line', async () => { listNotes(completedNote); listChildren({ 'session-1': [childSession('child-1'), childSession('child-2')] }); const section = mountSection(); @@ -1009,10 +1057,165 @@ describe('ProjectSection sessions waiting on repo sessions', () => { await emitWakeup('session-1', armedWakeup('child-2')); const row = rowFor('session-1'); - expect(row.dataset.type).toBe('note'); + expect(row.dataset.type).toBe('generating-note'); expect(row.dataset.title).toBe('A finished note'); - expect(row.dataset.secondaryMeta).toBe('Waiting on 1 repo session'); + expect(row.dataset.secondaryMeta).toBe(''); + expect(rowText(row)).not.toMatch(/waiting/i); + // Every repo session it has going is badged, named in the wake-up or not. expect(row.dataset.childBadges).toBe('child-1:running child-2:running'); + expect(resumeButton('session-1')).toBeNull(); + // A running row opens on its chat, not on the note. + clickRow('session-1'); + expect(noteModal.props).toMatchObject({ sessionId: 'session-1', chatOpen: true }); + + // The wake-up is stopped: it is the finished note again. + await emitWakeup('session-1', NO_WAKEUP); + expect(rowFor('session-1').dataset.type).toBe('note'); + + unmount(section); + }); + + it('dates a written note’s waiting row as the finished note’s is when it has no badges', async () => { + listNotes(completedNote); + const section = mountSection(); + await settle(); + // The finished note's subtitle: when it was written. + const dated = rowFor('session-1').dataset.secondaryMeta; + expect(dated).toBeTruthy(); + + // It waits on a repo session that is not among its children: the + // children have not been read, or the target is between states. + await emitWakeup('session-1', armedWakeup('child-1')); + + const row = rowFor('session-1'); + expect(row.dataset.type).toBe('generating-note'); + expect(row.dataset.title).toBe('A finished note'); + expect(row.dataset.childBadges).toBe(''); + // No turn is running and the note is written: nothing is being generated. + expect(row.dataset.secondaryMeta).toBe(dated); + expect(rowText(row)).not.toMatch(/generating|waiting/i); + + unmount(section); + }); + + it('keeps a written note’s menu actions, chat state and reply draft while its session waits', async () => { + // Unread, with a reply in its chat the user has not seen and one of the + // user's own left unsent. + listNotes(noteWith({ readAt: null, lastChatMessageAt: 5, chatReadAt: null })); + listChildren({ 'session-1': [childSession('child-1')] }); + replyDraftPresence.preview('session-1', { quotes: [], text: 'An unsent reply' }); + commandMocks.setProjectNoteRead.mockImplementation(async (noteId, read) => + noteWith({ id: noteId, readAt: read ? 9 : null }) + ); + const section = mountSection(); + await settle(); + // The finished note: all of it shows, the unread dot included. + expect(rowFor('session-1').dataset).toMatchObject({ + type: 'note', + unread: 'true', + chatState: 'unread', + hasDraft: 'true', + }); + + await emitWakeup('session-1', armedWakeup('child-1')); + + // The row reads as running, and like any running row carries no unread + // dot. What belongs to the note is still there. + const row = rowFor('session-1'); + expect(row.dataset).toMatchObject({ + type: 'generating-note', + unread: 'false', + chatState: 'unread', + hasDraft: 'true', + }); + expect(row.dataset.childBadges).toBe('child-1:running'); + expect(resumeButton('session-1')).toBeNull(); + // The note is still unread, so the menu marks it read, + expect(menuMarkButton('session-1', 'unread')).toBeNull(); + menuMarkButton('session-1', 'read')!.click(); + await settle(); + expect(commandMocks.setProjectNoteRead.mock.calls).toEqual([['note-1', true]]); + // and it can be referred to from a new session. + expect(menuActionFor('session-1').dataset.hashtagRef).toBe(REF); + writer.saveNow.mockClear(); + writer.scheduleSave.mockClear(); + loadDraftMock.mockImplementation(async () => null); + clickReferring(); + await settle(); + expect(promptInput()).not.toBeNull(); + expect(savedPrompts().some((prompt) => prompt.includes(REF))).toBe(true); + + replyDraftPresence.preview('session-1', { quotes: [], text: '' }); + unmount(section); + }); + + it('offers to mark a read note unread while its session waits', async () => { + listNotes(noteWith({ readAt: 5 })); + commandMocks.getSessionWakeup.mockImplementation(async () => armedWakeup('child-1')); + const section = mountSection(); + await settle(); + + const row = rowFor('session-1'); + expect(row.dataset).toMatchObject({ + type: 'generating-note', + unread: 'false', + chatState: 'none', + hasDraft: 'false', + }); + expect(menuMarkButton('session-1', 'read')).toBeNull(); + expect(menuMarkButton('session-1', 'unread')).not.toBeNull(); + + unmount(section); + }); + + // What the waiting row's unread dot is matched to: a written note whose + // session is in a turn carries none. Unlike a waiting one it is not a + // finished note meanwhile, so its menu offers neither the reference nor + // the read marks, and its row shows no chat state or reply draft. + it('shows no unread dot or note actions on a written note whose session is in a turn', async () => { + listNotes( + noteWith({ sessionStatus: 'running', readAt: null, lastChatMessageAt: 5, chatReadAt: null }) + ); + replyDraftPresence.preview('session-1', { quotes: [], text: 'An unsent reply' }); + const section = mountSection(); + await settle(); + + expect(rowFor('session-1').dataset).toMatchObject({ + type: 'generating-note', + unread: 'false', + chatState: 'none', + hasDraft: 'false', + }); + expect(menuActionFor('session-1').dataset.hashtagRef).toBe(''); + expect(menuMarkButton('session-1', 'read')).toBeNull(); + expect(menuMarkButton('session-1', 'unread')).toBeNull(); + + replyDraftPresence.preview('session-1', { quotes: [], text: '' }); + unmount(section); + }); + + it('lists a waiting session among the running ones, after the finished notes', async () => { + // Written before the finished note, so by date alone it would come first. + listNotes( + { ...unwritten, createdAt: 1, updatedAt: 1, completedAt: 1 }, + { ...completedNote, createdAt: 2, updatedAt: 2, completedAt: 2 } + ); + commandMocks.getSessionWakeup.mockImplementation(async (sessionId) => + sessionId === 'session-waiting' ? armedWakeup('child-1') : NO_WAKEUP + ); + listChildren({ 'session-waiting': [childSession('child-1')] }); + const section = mountSection(); + await settle(); + + const order = () => + [...document.querySelectorAll('[data-testid="timeline-row"]')].map( + (row) => `${row.dataset.sessionId}:${row.dataset.type}` + ); + expect(order()).toEqual(['session-1:note', 'session-waiting:generating-note']); + + // No longer waiting: it is a finished row again, in date order. + await emitWakeup('session-waiting', NO_WAKEUP); + expect(order()).toEqual(['session-waiting:failed-note', 'session-1:note']); unmount(section); }); @@ -1034,7 +1237,7 @@ describe('ProjectSection sessions waiting on repo sessions', () => { layerHolder.layer = reactiveProps({ active: true, visible: true, activations: 0 }); const section = mountSection(); await settle(); - expect(rowFor('session-waiting').dataset.type).toBe('waiting-note'); + expect(rowFor('session-waiting').dataset.type).toBe('generating-note'); expect(rowFor('session-1').dataset.type).toBe('note'); commandMocks.getSessionWakeup.mockClear(); @@ -1053,7 +1256,8 @@ describe('ProjectSection sessions waiting on repo sessions', () => { 'session-waiting', ]); expect(rowFor('session-waiting').dataset.type).toBe('failed-note'); - expect(rowFor('session-1').dataset.secondaryMeta).toBe('Waiting on 1 repo session'); + expect(rowFor('session-1').dataset.type).toBe('generating-note'); + expect(rowFor('session-1').dataset.childBadges).toBe('child-2:running'); unmount(section); }); diff --git a/apps/staged/src/lib/features/projects/ProjectSection.svelte b/apps/staged/src/lib/features/projects/ProjectSection.svelte index 3e24d2c62..e831c1d3d 100644 --- a/apps/staged/src/lib/features/projects/ProjectSection.svelte +++ b/apps/staged/src/lib/features/projects/ProjectSection.svelte @@ -51,7 +51,7 @@ import { noteChatState } from '../sessions/noteFreshness'; import { buildReferringPrompt } from '../../shared/buildReferringPrompt'; import { createLiveSessionRows } from '../timeline/liveSessionHints'; - import { canResumeRow, noteRowPhase } from '../timeline/sessionRowState'; + import { canResumeRow, isActiveRowPhase, noteRowPhase } from '../timeline/sessionRowState'; import { resumeSessionFromRow } from '../timeline/resumeSessionFromRow'; import { buildChildSessionBadges, runningChildSessionIds } from '../timeline/childSessionBadges'; import { nextActiveSessionIds } from './activeSessionIds'; @@ -742,14 +742,29 @@ } } + /** + * The phase a note's row shows. A session waiting to be woken when its + * repo sessions end reads as running, so everything that asks whether a + * row is running or done asks here, with the session's wake-up. + */ + function projectNoteRowPhase(note: ProjectNote) { + return noteRowPhase(note, sessionWakeupsStore.get(note.sessionId)); + } + + /** Whether a note's row shows its session as at work. */ + function showsNoteRunning(note: ProjectNote): boolean { + return isActiveRowPhase(projectNoteRowPhase(note)); + } + /** * All notes: completed (oldest last-written first) followed by generating – matches - * branch timeline order. + * branch timeline order. A note whose session is waiting to be woken is + * among the generating ones, as its row shows it. */ let timelineNotes = $derived( [...projectNotes].sort((a, b) => { - const aIsActive = isSessionActive(a.sessionStatus); - const bIsActive = isSessionActive(b.sessionStatus); + const aIsActive = showsNoteRunning(a); + const bIsActive = showsNoteRunning(b); if (aIsActive !== bIsActive) return aIsActive ? 1 : -1; return noteLastWrittenAt(a) - noteLastWrittenAt(b); }) @@ -873,14 +888,33 @@ if (openNote) void loadChildHashtagItems(openNote.noteId, true); } + /** + * Whether the note itself is finished: written, by a session that is not in + * a turn. Asked without the wake-up, unlike `projectNoteRowPhase`. A + * session waiting to be woken reads as running on its row, but the note it + * wrote is as finished as any: it can be referred to and marked read or + * unread, and its chat's replies and reply draft show on its row. + */ function isCompletedProjectNote(note: ProjectNote): boolean { return noteRowPhase(note) === 'done'; } + /** The read state the note's context menu offers to toggle, if any. */ function projectNoteReadState(note: ProjectNote) { return isCompletedProjectNote(note) ? noteReadState(note) : null; } + /** + * Whether the note's row carries the unread dot. Only a row that reads as + * finished does: a written note whose session is in a turn shows none, and + * neither does one whose session reads as running while it waits to be + * woken. The note stays unread, and the dot is back when the row is the + * finished note's again. + */ + function showsNoteUnread(note: ProjectNote): boolean { + return projectNoteRowPhase(note) === 'done' && projectNoteReadState(note) === 'unread'; + } + function setProjectNoteRead(noteId: string, read: boolean) { void setNoteReadState({ kind: 'project', noteId }, read); } @@ -1228,9 +1262,11 @@ })} + the sessions that were asked. A session waiting to be woken + when they end reads as running, so a click on its row opens + its chat, which says what it waits on. What belongs to a + note it has written stays: its chat's replies and reply + draft here, and its context-menu actions. --> openProjectNote(note) diff --git a/apps/staged/src/lib/features/projects/projectNoteRow.test.ts b/apps/staged/src/lib/features/projects/projectNoteRow.test.ts index ce641ebd3..60aa19f4e 100644 --- a/apps/staged/src/lib/features/projects/projectNoteRow.test.ts +++ b/apps/staged/src/lib/features/projects/projectNoteRow.test.ts @@ -130,37 +130,124 @@ describe('projectNoteRow', () => { ); }); - it('reads an empty note whose session is waiting on repo sessions as waiting, not failed', () => { + it('reads an empty note whose session is waiting on repo sessions as running, not failed', () => { const empty = note({ content: '', title: '' }); - expect(projectNoteRow(empty, { deleting: false, nowMs: NOW, wakeup: armed })).toEqual({ - type: 'waiting-note', - title: 'Waiting on 2 repo sessions', - secondaryMeta: undefined, - }); - // The title counts the named sessions still in flight, beside their badges. + // With the repo sessions' badges the subtitle is theirs alone. expect( projectNoteRow(empty, { deleting: false, nowMs: NOW, wakeup: armed, childBadges: [badge] }) - .title - ).toBe('Waiting on 1 repo session'); + ).toEqual({ + type: 'generating-note', + title: 'Generating note…', + secondaryMeta: undefined, + }); + // Before the badges are read, or with none in flight, it is the + // placeholder a running row shows. + expect(projectNoteRow(empty, { deleting: false, nowMs: NOW, wakeup: armed })).toEqual({ + type: 'generating-note', + title: 'Generating note…', + secondaryMeta: 'Generating note', + }); // The same note with no wake-up ended without writing anything. expect(projectNoteRow(empty, { deleting: false, nowMs: NOW }).type).toBe('failed-note'); }); - it('says what a written note’s session waits on in place of its timestamp', () => { + it('reads a written note whose session is waiting as running, under the note’s title', () => { expect( projectNoteRow(note(), { deleting: false, nowMs: NOW, wakeup: armed, childBadges: [badge] }) ).toEqual({ + type: 'generating-note', + title: 'A finished note', + secondaryMeta: undefined, + }); + // Delivered or stopped, it is the finished note again. + expect(projectNoteRow(note(), { deleting: false, nowMs: NOW, childBadges: [] })).toEqual({ type: 'note', title: 'A finished note', - secondaryMeta: 'Waiting on 1 repo session', + secondaryMeta: '5m ago', + }); + }); + + it('dates a written note whose session is waiting when it has no badges to show', () => { + // Before the badges are read, or with none in flight. No turn is running + // and the note is written, so the row does not say one is being generated. + expect(projectNoteRow(note(), { deleting: false, nowMs: NOW, wakeup: armed })).toEqual({ + type: 'generating-note', + title: 'A finished note', + secondaryMeta: '5m ago', }); expect( - projectNoteRow(note(), { + projectNoteRow(note({ updatedAt: 8 * 60_000 }), { deleting: false, nowMs: NOW, - wakeup: { ...armed, mode: 'any' }, + wakeup: armed, + childBadges: [], }).secondaryMeta - ).toBe('Waiting on any of 2 repo sessions'); + ).toBe('Updated 2m ago'); + // Untitled, it is titled as the finished note's row is. + expect( + projectNoteRow(note({ title: '' }), { deleting: false, nowMs: NOW, wakeup: armed }) + ).toEqual({ + type: 'generating-note', + title: 'Untitled note', + secondaryMeta: '5m ago', + }); + // A stream title or hint left over from its turn is not shown. + expect( + projectNoteRow(note(), { + deleting: false, + nowMs: NOW, + wakeup: armed, + liveTitle: 'Plan the rollout', + liveHint: 'Reading the deploy docs', + }) + ).toEqual({ + type: 'generating-note', + title: 'A finished note', + secondaryMeta: '5m ago', + }); + // Being deleted, it says so like any row. + expect( + projectNoteRow(note(), { deleting: true, nowMs: NOW, wakeup: armed }).secondaryMeta + ).toBe('Deleting...'); + }); + + it('never says a waiting session is waiting, in its title or its subtitle', () => { + const wakeups: SessionWakeup[] = [ + armed, + { ...armed, mode: 'any' }, + { ...armed, targetSessionIds: [] }, + ]; + for (const wakeup of wakeups) { + for (const source of [note(), note({ content: '', title: '' })]) { + for (const childBadges of [[], [badge], [badge, { ...badge, key: 'child-9' }]]) { + const row = projectNoteRow(source, { deleting: false, nowMs: NOW, wakeup, childBadges }); + expect(row.type).toBe('generating-note'); + expect(row.title).not.toMatch(/waiting/i); + expect(row.secondaryMeta ?? '').not.toMatch(/waiting/i); + // Nor that a note it has already written is being generated. + if (source.content) { + expect(`${row.title} ${row.secondaryMeta ?? ''}`).not.toMatch(/generating/i); + } + } + } + } + }); + + it('leaves a session that is not completed to its own status while a wake-up is armed', () => { + // Kept across a rate-limit pause: the row is the pause's. + const paused = note({ + sessionStatus: 'paused', + content: '', + title: '', + completionReason: 'rate_limited', + }); + expect( + projectNoteRow(paused, { deleting: false, nowMs: NOW, wakeup: armed, childBadges: [badge] }) + ).toEqual({ + type: 'failed-note', + title: 'Paused by API limit — no note yet', + secondaryMeta: '1 repo session still running', + }); }); it('counts the repo sessions a finished session left running without asking to be woken', () => { diff --git a/apps/staged/src/lib/features/projects/projectNoteRow.ts b/apps/staged/src/lib/features/projects/projectNoteRow.ts index 8e75311d4..f4a031677 100644 --- a/apps/staged/src/lib/features/projects/projectNoteRow.ts +++ b/apps/staged/src/lib/features/projects/projectNoteRow.ts @@ -5,10 +5,12 @@ * project row shows the failure copy as its title with no subtitle. * * A project session can also end its turn with repo sessions still going. If - * it armed a wake-up for them it is waiting: the row says what on, and a note - * it has not written yet reads as waiting, not failed. If it did not, the row - * says how many are still running. Either line takes the subtitle's place, - * ahead of the children's badges. + * it armed a wake-up for them, Staged starts its next turn when they end, and + * the row reads as running until then: the spinner and the children's badges, + * with nothing saying it is waiting. A note it has already written keeps its + * own words under the spinner: its title, and its date when there are no + * badges to show. If it did not arm one, the row is a finished one that says + * how many are still running, ahead of the children's badges. */ import type { ProjectNote, SessionWakeup } from '../../types'; @@ -20,6 +22,7 @@ import { type ChildSessionBadgeProps, } from '../timeline/childSessionBadges'; import { failedArtifactSubtitle } from '../timeline/sessionFailureCopy'; +import { isWaitingOnWakeup } from '../sessions/sessionWakeup'; import { noteRowPhase } from '../timeline/sessionRowState'; export type ProjectNoteRow = { @@ -55,38 +58,63 @@ export type ProjectNoteRowLive = { * subtitle over, and follow a finished row's line about them. */ childBadges?: ChildSessionBadgeProps[]; - /** The session's armed wake-up, when it has one. */ + /** + * The session's armed wake-up, when it has one. A session waiting on it + * reads as running (`noteRowPhase`). + */ wakeup?: SessionWakeup | null; }; +/** The title of a note that has been written. */ +function writtenNoteTitle(note: ProjectNoteRowSource): string { + return note.title || 'Untitled note'; +} + +/** + * Whether the session is waiting to be woken with its note already written: + * without the wake-up, the row would be the finished note's. + */ +function isWaitingWithWrittenNote( + note: ProjectNoteRowSource, + wakeup: SessionWakeup | null | undefined +): boolean { + return isWaitingOnWakeup(note.sessionStatus, wakeup) && noteRowPhase(note) === 'done'; +} + export function projectNoteRow( note: ProjectNoteRowSource, live: ProjectNoteRowLive ): ProjectNoteRow { const childBadges = live.childBadges ?? []; // What a finished session's row says about its repo sessions, if anything. - const finishedMeta = finishedSecondaryMeta(note.sessionStatus, live.wakeup, childBadges); + const finishedMeta = finishedSecondaryMeta(childBadges); let row: ProjectNoteRow; switch (noteRowPhase(note, live.wakeup)) { // Project sessions start running, so a queued status never reaches a // project note today; if one did it would read as running, as before. case 'queued': case 'running': - row = { - type: 'generating-note', - title: live.liveTitle ?? 'Generating note…', - secondaryMeta: runningSecondaryMeta(childBadges, live.liveHint), - }; - break; - // Nothing written yet, and none expected until the session is woken. The - // stub is untitled, so what it waits on is the title; with badges the - // subtitle would only repeat it. - case 'waiting': - row = { - type: 'waiting-note', - title: finishedMeta ?? 'Waiting on repo sessions', - secondaryMeta: undefined, - }; + if (isWaitingWithWrittenNote(note, live.wakeup)) { + // No turn is running, so nothing is being generated and the stream + // brings no title or hint. The row says what the finished note's row + // says: its title and when it was written. The badges of the repo + // sessions it started take the date's place, as they take a running + // row's hint. + row = { + type: 'generating-note', + title: writtenNoteTitle(note), + secondaryMeta: childBadges.length > 0 ? undefined : formatNoteTimestamp(note, live.nowMs), + }; + } else { + // In a turn, or waiting to be woken before writing its note. A + // waiting session has no live title or hint, so its row reads as a + // running one does before the stream has brought any. + row = { + type: 'generating-note', + title: live.liveTitle ?? 'Generating note…', + secondaryMeta: runningSecondaryMeta(childBadges, live.liveHint), + }; + } break; case 'failed': row = { @@ -101,7 +129,7 @@ export function projectNoteRow( case 'done': row = { type: 'note', - title: note.title || 'Untitled note', + title: writtenNoteTitle(note), secondaryMeta: finishedMeta ?? formatNoteTimestamp(note, live.nowMs), }; break; diff --git a/apps/staged/src/lib/features/sessions/SessionChatPane.wakeup.component.test.ts b/apps/staged/src/lib/features/sessions/SessionChatPane.wakeup.component.test.ts index 0307c4b52..630019db2 100644 --- a/apps/staged/src/lib/features/sessions/SessionChatPane.wakeup.component.test.ts +++ b/apps/staged/src/lib/features/sessions/SessionChatPane.wakeup.component.test.ts @@ -182,9 +182,7 @@ describe('SessionChatPane for a session waiting on repo sessions', () => { it('says what a waiting session waits on, having asked for its wake-up on open', async () => { commandMocks.getSessionWakeup.mockImplementation(async () => ARMED); await mountPane(); - expect(noticeMessage()).toBe( - 'Waiting on 2 repo sessions. Staged starts a new turn here when they end.' - ); + expect(noticeMessage()).toBe('Waiting on 2 repo sessions'); }); it('shows the notice when the wake-up is announced while the pane is open, and drops it when it is delivered', async () => { diff --git a/apps/staged/src/lib/features/sessions/SessionWakeupNotice.component.test.ts b/apps/staged/src/lib/features/sessions/SessionWakeupNotice.component.test.ts index 32e7a6b4e..60f11e153 100644 --- a/apps/staged/src/lib/features/sessions/SessionWakeupNotice.component.test.ts +++ b/apps/staged/src/lib/features/sessions/SessionWakeupNotice.component.test.ts @@ -17,8 +17,7 @@ vi.mock('../../transport', () => ({ })); import SessionWakeupNotice from './SessionWakeupNotice.svelte'; -import { formatPauseTime } from '../timeline/sessionFailureCopy'; -import { CHECK_IN_DUE_NOTICE, STOP_WAITING_TITLE } from './sessionWakeup'; +import { STOP_WAITING_TITLE } from './sessionWakeup'; import { reactiveProps } from './testStubs/reactiveProps.svelte'; function wakeup(overrides: Partial = {}): SessionWakeup { @@ -64,8 +63,15 @@ async function settle() { const message = () => document.querySelector('[data-testid="session-wakeup-message"]')?.textContent?.trim(); -const checkIn = () => - document.querySelector('[data-testid="session-wakeup-check-in"]')?.textContent?.trim(); +/** Everything the notice shows, button included, with whitespace collapsed. */ +const noticeText = () => + document + .querySelector('[data-testid="session-wakeup-notice"]') + ?.textContent?.replace(/\s+/g, ' ') + .trim(); + +/** Words for how Staged ends a wait, which the notice does not show. */ +const INTERNALS = /\bturns?\b|check[- ]?in|\bStaged\b/i; function stopButton(): HTMLButtonElement { const found = [...document.querySelectorAll('button')].find( @@ -96,6 +102,16 @@ afterEach(() => { }); describe('SessionWakeupNotice', () => { + it('spins: a waiting session is still at work through its repo sessions', () => { + render({ wakeup: wakeup() }); + + const spinner = document.querySelector('[data-testid="session-wakeup-spinner"]'); + expect(spinner?.tagName.toLowerCase()).toBe('svg'); + expect(spinner?.classList.contains('animate-spin')).toBe(true); + // The alert lays itself out around an svg that is its direct child. + expect(spinner?.parentElement?.dataset.testid).toBe('session-wakeup-notice'); + }); + it('says what the session waits on, counting the named sessions still in flight', async () => { // One of the three has ended; a child it did not name is going too. listChildSessions.mockImplementation(async () => [ @@ -105,54 +121,64 @@ describe('SessionWakeupNotice', () => { ]); render({ wakeup: wakeup() }); // Every session named, until the session's children have been read. - expect(message()).toBe( - 'Waiting on 3 repo sessions. Staged starts a new turn here when they end.' - ); + expect(message()).toBe('Waiting on 3 repo sessions'); await settle(); expect(listChildSessions.mock.calls).toEqual([['session-1']]); - expect(message()).toBe( - 'Waiting on 2 repo sessions. Staged starts a new turn here when they end.' - ); - expect(checkIn()).toBeUndefined(); + expect(message()).toBe('Waiting on 2 repo sessions'); }); - it('says a wake-up for any of several starts a turn when the first one ends', async () => { + it('is one line: what is waited on and Stop waiting, nothing about how the wait ends', async () => { listChildSessions.mockImplementation(async () => [child('child-1'), child('child-2')]); - render({ wakeup: wakeup({ mode: 'any', targetSessionIds: ['child-1', 'child-2'] }) }); + render({ wakeup: wakeup({ targetSessionIds: ['child-1', 'child-2'] }) }); await settle(); - expect(message()).toBe( - 'Waiting on any of 2 repo sessions. Staged starts a new turn here when the first one ends.' - ); + + expect(noticeText()).toBe('Waiting on 2 repo sessions Stop waiting'); + expect(noticeText()).not.toContain('Staged starts a new turn here'); + expect(noticeText()).not.toContain('If that has not happened'); + expect(noticeText()).not.toMatch(INTERNALS); + // The message is the description's only child: no second line under it. + const description = document.querySelector('[data-slot="alert-description"]'); + expect(description?.children).toHaveLength(1); + expect(description?.textContent?.trim()).toBe('Waiting on 2 repo sessions'); }); - it('says when Staged checks in regardless', async () => { - const checkInAt = Date.now() + 90 * 60_000; - render({ wakeup: wakeup({ targetSessionIds: ['child-1'], checkInAt }) }); + it('counts one repo session in the singular', async () => { + listChildSessions.mockImplementation(async () => [child('child-1')]); + render({ wakeup: wakeup({ targetSessionIds: ['child-1'] }) }); + await settle(); + expect(noticeText()).toBe('Waiting on 1 repo session Stop waiting'); + }); + + it('says a wake-up for the first of several to end waits on any of them', async () => { + listChildSessions.mockImplementation(async () => [child('child-1'), child('child-2')]); + render({ wakeup: wakeup({ mode: 'any', targetSessionIds: ['child-1', 'child-2'] }) }); await settle(); - expect(message()).toBe( - 'Waiting on 1 repo session. Staged starts a new turn here when it ends.' - ); - expect(checkIn()).toBe( - `If that has not happened, Staged checks in around ${formatPauseTime(checkInAt)}.` - ); + expect(noticeText()).toBe('Waiting on any of 2 repo sessions Stop waiting'); + expect(noticeText()).not.toContain('when the first one ends'); }); - it('says a check-in is due, with no time, once the time has passed', async () => { - // Held behind the launch dialog, say. "Checks in around" a time an hour - // back read as a wait that was stuck. - const checkInAt = Date.now() - 60 * 60_000; + // The notice used to add "If that has not happened, Staged checks in around + //