-
Notifications
You must be signed in to change notification settings - Fork 12
fix(workbench): revalidate session and workspace lists on session.changed #526
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
Zerlight
wants to merge
5
commits into
master
Choose a base branch
from
ruocheng/code-654
base: master
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 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fef197b
fix(workbench): revalidate session and workspace lists on session.cha…
Zerlight bd9f642
fix(workbench): coalesce session.changed revalidation bursts
Zerlight 68841ed
test(workbench): verify lazy keys and coalesced list refreshes
Zerlight 66e3270
fix(workbench): respect session refresh lifecycle
Zerlight 39a8a3a
docs(workbench): clarify mock resume guarantees
Zerlight 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
72 changes: 72 additions & 0 deletions
72
packages/client/workbench/src/runtime/__tests__/coalesce.test.ts
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,72 @@ | ||
| import { expect, it } from 'vitest'; | ||
| import { coalesceRuns } from '../coalesce'; | ||
|
|
||
| function deferred(): { promise: Promise<void>; resolve: () => void } { | ||
| let resolveDeferred!: () => void; | ||
| const promise = new Promise<void>((resolve) => { | ||
| resolveDeferred = resolve; | ||
| }); | ||
| return { promise, resolve: resolveDeferred }; | ||
| } | ||
|
|
||
| it('collapses a burst arriving mid-run into a single trailing run', async () => { | ||
| const gates = [deferred(), deferred()]; | ||
| let started = 0; | ||
| const trigger = coalesceRuns(() => { | ||
| const gate = gates[started] ?? deferred(); | ||
| started += 1; | ||
| return gate.promise; | ||
| }); | ||
|
|
||
| trigger(); | ||
| expect(started).toBe(1); | ||
|
|
||
| // Three more frames while the first run is still in flight: they must collapse into one. | ||
| trigger(); | ||
| trigger(); | ||
| trigger(); | ||
| expect(started).toBe(1); | ||
|
|
||
| gates[0].resolve(); | ||
| await Promise.resolve(); | ||
| await Promise.resolve(); | ||
| expect(started).toBe(2); | ||
|
|
||
| gates[1].resolve(); | ||
| await gates[1].promise; | ||
| await Promise.resolve(); | ||
| expect(started).toBe(2); | ||
| }); | ||
|
|
||
| it('runs again for a trigger that arrives after the previous run settled', async () => { | ||
| let started = 0; | ||
| const trigger = coalesceRuns(() => { | ||
| started += 1; | ||
| return Promise.resolve(); | ||
| }); | ||
|
|
||
| trigger(); | ||
| await Promise.resolve(); | ||
| await Promise.resolve(); | ||
| trigger(); | ||
| await Promise.resolve(); | ||
| await Promise.resolve(); | ||
|
|
||
| expect(started).toBe(2); | ||
| }); | ||
|
|
||
| it('keeps draining after a failed run', async () => { | ||
| let started = 0; | ||
| const trigger = coalesceRuns(() => { | ||
| started += 1; | ||
| return started === 1 ? Promise.reject(new Error('fetch failed')) : Promise.resolve(); | ||
| }); | ||
|
|
||
| trigger(); | ||
| trigger(); | ||
| await Promise.resolve(); | ||
| await Promise.resolve(); | ||
| await Promise.resolve(); | ||
|
|
||
| expect(started).toBe(2); | ||
| }); |
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,41 @@ | ||
| import { noop } from 'foxts/noop'; | ||
|
|
||
| /** | ||
| * Collapse a burst of triggers into one in-flight run plus at most one trailing run. A trigger | ||
| * arriving mid-run must still cause another run: the one in flight may have read state older than | ||
| * the event that triggered it. A failed run does not abort the drain — the caller's own error | ||
| * pipeline reports it, and dropping the trailing run would leave exactly the staleness the caller | ||
| * is revalidating away. | ||
| */ | ||
| export function coalesceRuns(run: () => Promise<unknown>): () => void { | ||
| let running = false; | ||
| let queued = false; | ||
|
|
||
| // Read through a call, not `while (queued)`: the flag is only ever set from the closure below | ||
| // while a run is awaited, which narrowing cannot see. | ||
| const takeQueued = (): boolean => { | ||
| const wasQueued = queued; | ||
| queued = false; | ||
| return wasQueued; | ||
| }; | ||
|
|
||
| const drain = async (): Promise<void> => { | ||
| running = true; | ||
| try { | ||
| do { | ||
| // eslint-disable-next-line no-await-in-loop -- serializing is the point: one run at a time | ||
| await run().catch(noop); | ||
| } while (takeQueued()); | ||
| } finally { | ||
| running = false; | ||
| } | ||
| }; | ||
|
|
||
| return () => { | ||
| if (running) { | ||
| queued = true; | ||
| return; | ||
| } | ||
| void drain().catch(noop); | ||
| }; | ||
| } |
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
103 changes: 103 additions & 0 deletions
103
packages/client/workbench/tests/integration/session-changed-revalidation.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,103 @@ | ||
| // @vitest-environment jsdom | ||
| import { LinkCodeClient, useLinkCodeClient } from '@linkcode/client-core'; | ||
| import { listSessions, listWorkspaces } from '@linkcode/sdk'; | ||
| import { cleanup, renderHook, waitFor } from '@testing-library/react'; | ||
| import { createFixedArray } from 'foxts/create-fixed-array'; | ||
| import { afterEach, expect, it, vi } from 'vitest'; | ||
| import { createDevMockTransport } from '../../src/mock/dev-mock-transport'; | ||
| import { DebugProvider } from '../../src/runtime/debug'; | ||
| import { WorkbenchRuntimeProvider } from '../../src/runtime/provider'; | ||
| import { useData } from '../../src/runtime/tayori'; | ||
| import { useWorkspaces } from '../../src/workspace/hooks'; | ||
|
|
||
| const connectionSource = { | ||
| resolve: () => ({ endpoint: 'mock://session-changed', transport: createDevMockTransport() }), | ||
| }; | ||
|
Zerlight marked this conversation as resolved.
|
||
|
|
||
| function Runtime({ children }: React.PropsWithChildren): React.ReactNode { | ||
| return ( | ||
| <DebugProvider> | ||
| <WorkbenchRuntimeProvider connectionSource={connectionSource}> | ||
| {children} | ||
| </WorkbenchRuntimeProvider> | ||
| </DebugProvider> | ||
| ); | ||
| } | ||
|
|
||
| /** The sidebar's two inputs, read the way the workbench reads them — through the shared hooks, | ||
| * which never call `mutate()` themselves. */ | ||
| function useSidebarInputs() { | ||
| const { data: workspaces } = useWorkspaces(); | ||
| const { data: sessions } = useData(listSessions, {}); | ||
| return { client: useLinkCodeClient(), workspaces, sessions }; | ||
| } | ||
|
|
||
| function useLazySidebarInputs() { | ||
| const { data: workspaces } = useData(listWorkspaces, () => ({})); | ||
| const { data: sessions } = useData(listSessions, () => ({})); | ||
| return { client: useLinkCodeClient(), workspaces, sessions }; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| cleanup(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| // The mock host answers every control request after a scripted latency; each step here is one or | ||
| // more of those round trips. | ||
| const STEP_TIMEOUT = { timeout: 4000 }; | ||
|
|
||
| it.each([ | ||
| { keyForm: 'object', useInputs: useSidebarInputs }, | ||
| { keyForm: 'lazy', useInputs: useLazySidebarInputs }, | ||
| ])( | ||
| 'refreshes $keyForm keys when another client starts a session', | ||
| async ({ useInputs }) => { | ||
| const { result } = renderHook(useInputs, { wrapper: Runtime }); | ||
| await waitFor(() => expect(result.current.workspaces).toBeDefined(), STEP_TIMEOUT); | ||
| const cwd = '/mock/elsewhere/new-repo'; | ||
| expect(result.current.workspaces?.map((workspace) => workspace.cwd)).not.toContain(cwd); | ||
|
|
||
| // Bypassing the workbench's own create path stands in for another client: this client only | ||
| // learns about the session from the host's pushed frames. | ||
| const sessionId = await result.current.client.startSession({ kind: 'claude-code', cwd }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current.workspaces?.map((workspace) => workspace.cwd)).toContain(cwd); | ||
| expect(result.current.sessions?.map((session) => session.sessionId)).toContain(sessionId); | ||
| }, STEP_TIMEOUT); | ||
| }, | ||
| 15000, | ||
| ); | ||
|
|
||
| it('collapses a burst of pushes instead of one round trip per frame', async () => { | ||
| const listSpy = vi.spyOn(LinkCodeClient.prototype, 'listSessions'); | ||
| const workspaceSpy = vi.spyOn(LinkCodeClient.prototype, 'listWorkspaces'); | ||
| const { result } = renderHook(useSidebarInputs, { wrapper: Runtime }); | ||
| await waitFor(() => expect(result.current.workspaces).toBeDefined(), STEP_TIMEOUT); | ||
|
|
||
| const starts = 6; | ||
| listSpy.mockClear(); | ||
| workspaceSpy.mockClear(); | ||
| const ids = await Promise.all( | ||
| createFixedArray(starts).map((index) => | ||
| result.current.client.startSession({ | ||
| kind: 'claude-code', | ||
| cwd: `/mock/elsewhere/burst-${index}`, | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| await waitFor(() => { | ||
| const listed = result.current.sessions?.map((session) => session.sessionId) ?? []; | ||
| const workspaces = result.current.workspaces?.map((workspace) => workspace.cwd) ?? []; | ||
| for (let i = 0, len = ids.length; i < len; i++) { | ||
| expect(listed).toContain(ids[i]); | ||
| expect(workspaces).toContain(`/mock/elsewhere/burst-${i}`); | ||
| } | ||
| }, STEP_TIMEOUT); | ||
|
|
||
| // All starts complete within the mock's list latency: one in-flight fetch plus one trailing. | ||
| expect(listSpy.mock.calls.length).toBeLessThanOrEqual(2); | ||
| expect(workspaceSpy.mock.calls.length).toBeLessThanOrEqual(2); | ||
| }, 15000); | ||
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.