Repository navigation
feat(sessions): show background activity for shells and child sessions #419
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2e2eff0
feat(sessions): show background activity for shells and child sessions
5dbf976
fix(sessions): refresh transcript when returning to a session
aec94db
fix(sessions): address background activity review feedback
59a25cc
test(sessions): flush microtasks instead of fixed sleep in transcript…
chriswritescode-dev e6d2601
Merge remote-tracking branch 'origin/main' into feat/session-backgrou…
chriswritescode-dev 964c6c2
test(e2e): cover session background activity indicator
chriswritescode-dev 901bcd0
test(e2e): fail background activity cleanup when a shell removal is r…
chriswritescode-dev 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import type { APIRequestContext, Page } from '@playwright/test' | ||
| import { test, expect } from './fixtures' | ||
|
|
||
| const SHELL_COMMAND = 'sleep 300' | ||
|
|
||
| function locationQuery(directory: string): string { | ||
| return new URLSearchParams({ 'location[directory]': directory }).toString() | ||
| } | ||
|
|
||
| function backgroundIndicator(page: Page) { | ||
| return page.getByRole('main').getByLabel('Background tasks running') | ||
| } | ||
|
|
||
| function backgroundStatus(page: Page) { | ||
| return page.getByRole('status').filter({ hasText: 'Background tasks running' }) | ||
| } | ||
|
|
||
| async function requireOk(response: Awaited<ReturnType<APIRequestContext['post']>>, label: string): Promise<unknown> { | ||
| if (!response.ok()) { | ||
| throw new Error(`${label} failed: ${response.status()} ${await response.text()}`) | ||
| } | ||
| return response.json() | ||
| } | ||
|
|
||
| async function createSession(request: APIRequestContext, directory: string, parentID?: string): Promise<string> { | ||
| const body = await requireOk( | ||
| await request.post('/api/opencode/api/session', { | ||
| data: { location: { directory }, ...(parentID ? { parentID } : {}) }, | ||
| }), | ||
| 'create session', | ||
| ) | ||
| return (body as { data: { id: string } }).data.id | ||
| } | ||
|
|
||
| async function startBackgroundShell( | ||
| request: APIRequestContext, | ||
| directory: string, | ||
| sessionID: string, | ||
| ): Promise<string> { | ||
| const body = await requireOk( | ||
| await request.post(`/api/opencode/api/shell?${locationQuery(directory)}`, { | ||
| data: { command: SHELL_COMMAND, timeout: 0, metadata: { sessionID, background: true } }, | ||
| }), | ||
| 'start background shell', | ||
| ) | ||
| return (body as { data: { id: string } }).data.id | ||
| } | ||
|
|
||
| async function removeShell(request: APIRequestContext, directory: string, shellID: string): Promise<void> { | ||
| const response = await request.delete(`/api/opencode/api/shell/${shellID}?${locationQuery(directory)}`) | ||
| if (!response.ok() && response.status() !== 404) { | ||
| throw new Error(`remove shell failed: ${response.status()} ${await response.text()}`) | ||
| } | ||
| } | ||
|
|
||
| const createdShells: Array<{ directory: string; id: string }> = [] | ||
|
|
||
| test.afterEach(async ({ request }) => { | ||
| const shells = createdShells.splice(0) | ||
| const results = await Promise.allSettled(shells.map(({ directory, id }) => removeShell(request, directory, id))) | ||
| const failures = results.flatMap((result) => (result.status === 'rejected' ? [String(result.reason)] : [])) | ||
| expect(failures, 'background shell cleanup failed').toEqual([]) | ||
| }) | ||
|
|
||
| test.describe('session background activity', () => { | ||
| test('shows the background indicator for a shell running before page load and clears it when removed', async ({ | ||
| page, | ||
| request, | ||
| repo, | ||
| }) => { | ||
| const sessionId = await createSession(request, repo.fullPath) | ||
| const shellId = await startBackgroundShell(request, repo.fullPath, sessionId) | ||
| createdShells.push({ directory: repo.fullPath, id: shellId }) | ||
|
|
||
| await page.goto(`/repos/${repo.id}/sessions/${sessionId}`) | ||
| await expect(page.getByPlaceholder('Send a message...')).toBeVisible() | ||
|
|
||
| await expect(backgroundIndicator(page)).toBeVisible() | ||
| await expect(backgroundStatus(page)).toBeAttached() | ||
|
|
||
| await removeShell(request, repo.fullPath, shellId) | ||
|
|
||
| await expect(backgroundIndicator(page)).toBeHidden() | ||
| await expect(backgroundStatus(page)).toHaveCount(0) | ||
| }) | ||
|
|
||
| test('shows the background indicator when a shell starts while the page is open and clears it when removed', async ({ | ||
| page, | ||
| request, | ||
| repo, | ||
| }) => { | ||
| const sessionId = await createSession(request, repo.fullPath) | ||
|
|
||
| await page.goto(`/repos/${repo.id}/sessions/${sessionId}`) | ||
| await expect(page.getByPlaceholder('Send a message...')).toBeVisible() | ||
| await expect(backgroundIndicator(page)).toHaveCount(0) | ||
|
|
||
| const shellId = await startBackgroundShell(request, repo.fullPath, sessionId) | ||
| createdShells.push({ directory: repo.fullPath, id: shellId }) | ||
|
|
||
| await expect(backgroundIndicator(page)).toBeVisible() | ||
| await expect(backgroundStatus(page)).toBeAttached() | ||
|
|
||
| await removeShell(request, repo.fullPath, shellId) | ||
|
|
||
| await expect(backgroundIndicator(page)).toBeHidden() | ||
| await expect(backgroundStatus(page)).toHaveCount(0) | ||
| }) | ||
|
|
||
| test('shows the background indicator on a parent session when a child session owns the running shell', async ({ | ||
| page, | ||
| request, | ||
| repo, | ||
| }) => { | ||
| const parentId = await createSession(request, repo.fullPath) | ||
| const childId = await createSession(request, repo.fullPath, parentId) | ||
| const shellId = await startBackgroundShell(request, repo.fullPath, childId) | ||
| createdShells.push({ directory: repo.fullPath, id: shellId }) | ||
|
|
||
| await page.goto(`/repos/${repo.id}/sessions/${parentId}`) | ||
| await expect(page.getByPlaceholder('Send a message...')).toBeVisible() | ||
|
|
||
| await expect(backgroundIndicator(page)).toBeVisible() | ||
| await expect(backgroundStatus(page)).toBeAttached() | ||
|
|
||
| await removeShell(request, repo.fullPath, shellId) | ||
|
|
||
| await expect(backgroundIndicator(page)).toBeHidden() | ||
| await expect(backgroundStatus(page)).toHaveCount(0) | ||
| }) | ||
|
|
||
| test('shows no background indicator for a session with no running shells', async ({ page, request, repo }) => { | ||
| const sessionId = await createSession(request, repo.fullPath) | ||
|
|
||
| await page.goto(`/repos/${repo.id}/sessions/${sessionId}`) | ||
| await expect(page.getByPlaceholder('Send a message...')).toBeVisible() | ||
|
|
||
| await expect(backgroundIndicator(page)).toHaveCount(0) | ||
| await expect(backgroundStatus(page)).toHaveCount(0) | ||
| }) | ||
| }) |
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
37 changes: 37 additions & 0 deletions
37
frontend/src/components/ui/session-status-indicator.test.tsx
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,37 @@ | ||
| import { render, screen } from '@testing-library/react' | ||
| import { beforeEach, describe, expect, it } from 'vitest' | ||
| import { SessionStatusIndicator } from './session-status-indicator' | ||
| import { useSessionStatus } from '@/stores/sessionStatusStore' | ||
|
|
||
| const resetStore = () => { | ||
| useSessionStatus.setState({ | ||
| statuses: new Map(), | ||
| statusCache: new Map(), | ||
| statusRevisions: new Map(), | ||
| knownSessions: new Set(), | ||
| outcomes: new Map(), | ||
| parents: new Map(), | ||
| runningShells: new Map(), | ||
| revision: 0, | ||
| }) | ||
| } | ||
|
|
||
| describe('SessionStatusIndicator background activity', () => { | ||
| beforeEach(() => { | ||
| resetStore() | ||
| }) | ||
|
|
||
| it('shows the background tasks label when idle with a running shell', () => { | ||
| useSessionStatus.getState().setShellRunning('shell-1', 'session-a', '/repo') | ||
|
|
||
| render(<SessionStatusIndicator sessionID="session-a" showLabel />) | ||
|
|
||
| expect(screen.getByLabelText('Background tasks running')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('renders nothing when idle without background activity', () => { | ||
| const { container } = render(<SessionStatusIndicator sessionID="session-a" showLabel />) | ||
|
|
||
| expect(container).toBeEmptyDOMElement() | ||
| }) | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.