Repository navigation
refactor(client): composers share one workspace snapshot refresh per provider and cwd #25370
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
juliusmarminge
merged 2 commits into
main
from
effect/split-client-workspace-snapshot-refresh
Oct 26, 2026
Merged
Changes from all commits
Commits
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
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
158 changes: 22 additions & 136 deletions
158
apps/web/src/components/chat/useProviderWorkspaceSnapshotRefresh.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 |
|---|---|---|
| @@ -1,146 +1,32 @@ | ||
| import { | ||
| type EnvironmentId, | ||
| PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS, | ||
| type ServerProvider, | ||
| } from "@t3tools/contracts"; | ||
| import { type ProviderInstanceEntry } from "../../providerInstances"; | ||
| import { | ||
| hasCompleteProviderWorkspaceSnapshot, | ||
| hasCurrentProviderWorkspaceSnapshot, | ||
| } from "@t3tools/client-runtime/providerSkills"; | ||
| import { useAtomRefresh } from "@effect/atom-react"; | ||
| import { type EnvironmentId, type ProviderInstanceId } from "@t3tools/contracts"; | ||
| import { createProviderWorkspaceSnapshotRefreshAtomFamily } from "@t3tools/client-runtime/providerSkills"; | ||
| import { Atom } from "effect/reactivity"; | ||
|
|
||
| import { serverEnvironment } from "../../state/server"; | ||
| import { useAtomCommand } from "../../state/use-atom-command"; | ||
| import { useEffect, useEffectEvent, useRef, useState } from "react"; | ||
| import { useStableCallback } from "../../hooks/useStableCallback"; | ||
|
|
||
| const WORKSPACE_SNAPSHOT_RETRY_COOLDOWN_MS = 10_000; | ||
| // A cwd that never scans, such as a deleted worktree, would otherwise retry forever. | ||
| const WORKSPACE_SNAPSHOT_MAX_ATTEMPTS = 3; | ||
| const providerWorkspaceSnapshotRefresh = createProviderWorkspaceSnapshotRefreshAtomFamily({ | ||
| providers: serverEnvironment.providersValueAtom, | ||
| refreshProviders: serverEnvironment.refreshProviders, | ||
| }); | ||
|
|
||
| const idleRefreshAtom: Atom.Atom<unknown> = Atom.make<void>(undefined).pipe( | ||
| Atom.withLabel("provider-workspace-snapshot-refresh:idle"), | ||
| ); | ||
|
|
||
| /** | ||
| * Keeps the selected provider's workspace snapshot fresh for the composer's | ||
| * cwd. Returns a check to run when the user types. | ||
| * cwd while mounted. Returns a check to run when the user types. | ||
| */ | ||
| export function useProviderWorkspaceSnapshotRefresh(input: { | ||
| readonly environmentId: EnvironmentId; | ||
| readonly gitCwd: string | null; | ||
| readonly selectedProviderEntry: ProviderInstanceEntry | undefined; | ||
| readonly selectedProviderStatus: ServerProvider | null; | ||
| }) { | ||
| const { environmentId, gitCwd, selectedProviderEntry, selectedProviderStatus } = input; | ||
| const refreshProviders = useAtomCommand(serverEnvironment.refreshProviders, { | ||
| reportFailure: false, | ||
| }); | ||
| // The last scan this composer asked for. A request inside the TTL is not | ||
| // repeated, so a client clock ahead of the server's cannot loop rescans. | ||
| const workspaceRefreshKeyRef = useRef<{ key: string; requestedAt: number } | null>(null); | ||
| const failedRefreshesRef = useRef<{ key: string; count: number } | null>(null); | ||
| const [workspaceRefreshRetry, setWorkspaceRefreshRetry] = useState<{ | ||
| key: string; | ||
| notBefore: number; | ||
| } | null>(null); | ||
| const workspaceRefreshScopeKey = | ||
| gitCwd && selectedProviderEntry | ||
| ? `${environmentId}:${selectedProviderEntry.instanceId}:${gitCwd}` | ||
| : null; | ||
| const workspaceSlashCommandsPending = | ||
| selectedProviderStatus?.workspaceSnapshots?.some( | ||
| (snapshot) => snapshot.cwd === gitCwd && snapshot.slashCommandsPending === true, | ||
| ) ?? false; | ||
| useEffect(() => { | ||
| if ( | ||
| !workspaceSlashCommandsPending || | ||
| !workspaceRefreshRetry || | ||
| workspaceRefreshRetry.key !== workspaceRefreshScopeKey | ||
| ) | ||
| return; | ||
| const timeout = setTimeout( | ||
| () => { | ||
| setWorkspaceRefreshRetry((current) => (current === workspaceRefreshRetry ? null : current)); | ||
| }, | ||
| Math.max(0, workspaceRefreshRetry.notBefore - Date.now()), | ||
| ); | ||
| return () => clearTimeout(timeout); | ||
| }, [workspaceRefreshRetry, workspaceRefreshScopeKey, workspaceSlashCommandsPending]); | ||
| // Losing a snapshot (the server dropped it) lifts both the TTL and the retry | ||
| // cooldown so the next check rescans at once. | ||
| const hasWorkspaceSnapshot = hasCompleteProviderWorkspaceSnapshot(selectedProviderStatus, gitCwd); | ||
| const [hadWorkspaceSnapshot, setHadWorkspaceSnapshot] = useState(hasWorkspaceSnapshot); | ||
| const [workspaceSnapshotLossCount, setWorkspaceSnapshotLossCount] = useState(0); | ||
| if (hadWorkspaceSnapshot !== hasWorkspaceSnapshot) { | ||
| setHadWorkspaceSnapshot(hasWorkspaceSnapshot); | ||
| if (!hasWorkspaceSnapshot) { | ||
| setWorkspaceSnapshotLossCount((count) => count + 1); | ||
| setWorkspaceRefreshRetry(null); | ||
| } | ||
| } | ||
| const lastWorkspaceSnapshotLossRef = useRef(workspaceSnapshotLossCount); | ||
| const refreshWorkspaceSnapshotIfStale = ( | ||
| now: number, | ||
| key: string | null, | ||
| retry: { key: string; notBefore: number } | null, | ||
| snapshotLossCount: number, | ||
| ) => { | ||
| if (lastWorkspaceSnapshotLossRef.current !== snapshotLossCount) { | ||
| lastWorkspaceSnapshotLossRef.current = snapshotLossCount; | ||
| workspaceRefreshKeyRef.current = null; | ||
| } | ||
| if (key === null || !gitCwd || !selectedProviderEntry) return; | ||
| const lastRequest = workspaceRefreshKeyRef.current; | ||
| if ( | ||
| lastRequest?.key === key && | ||
| now - lastRequest.requestedAt < PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS | ||
| ) | ||
| return; | ||
| // A pending retry needs no clearing here: its cooldown lapses long before the TTL. | ||
| if (hasCurrentProviderWorkspaceSnapshot(selectedProviderStatus, gitCwd, now)) return; | ||
| if (retry?.key === key && now < retry.notBefore) return; | ||
| const request = { key, requestedAt: now }; | ||
| workspaceRefreshKeyRef.current = request; | ||
| const retryLater = () => { | ||
| if (workspaceRefreshKeyRef.current !== request) return; | ||
| const failed = failedRefreshesRef.current; | ||
| const count = failed?.key === key ? failed.count + 1 : 1; | ||
| failedRefreshesRef.current = { key, count }; | ||
| // Keeping the request marker lets the TTL hold off the next scan. | ||
| if (count >= WORKSPACE_SNAPSHOT_MAX_ATTEMPTS) return; | ||
| workspaceRefreshKeyRef.current = null; | ||
| setWorkspaceRefreshRetry({ | ||
| key, | ||
| notBefore: Date.now() + WORKSPACE_SNAPSHOT_RETRY_COOLDOWN_MS, | ||
| }); | ||
| }; | ||
| void refreshProviders({ | ||
| environmentId, | ||
| input: { instanceId: selectedProviderEntry.instanceId, cwd: gitCwd }, | ||
| }).then((result) => { | ||
| const refreshedSnapshot = | ||
| result._tag === "Success" && | ||
| hasCompleteProviderWorkspaceSnapshot( | ||
| result.value.providers.find( | ||
| (provider) => provider.instanceId === selectedProviderEntry.instanceId, | ||
| ), | ||
| gitCwd, | ||
| ); | ||
| if (!refreshedSnapshot) retryLater(); | ||
| else failedRefreshesRef.current = null; | ||
| }, retryLater); | ||
| }; | ||
| const refreshWorkspaceSnapshotOnChange = useEffectEvent(refreshWorkspaceSnapshotIfStale); | ||
| useEffect(() => { | ||
| refreshWorkspaceSnapshotOnChange( | ||
| Date.now(), | ||
| workspaceRefreshScopeKey, | ||
| workspaceRefreshRetry, | ||
| workspaceSnapshotLossCount, | ||
| ); | ||
| }, [workspaceRefreshScopeKey, workspaceRefreshRetry, workspaceSnapshotLossCount]); | ||
| return useStableCallback(() => | ||
| refreshWorkspaceSnapshotIfStale( | ||
| Date.now(), | ||
| workspaceRefreshScopeKey, | ||
| workspaceRefreshRetry, | ||
| workspaceSnapshotLossCount, | ||
| ), | ||
| ); | ||
| readonly instanceId: ProviderInstanceId | undefined; | ||
| }): () => void { | ||
| const { environmentId, gitCwd, instanceId } = input; | ||
| const atom = | ||
| gitCwd && instanceId | ||
| ? providerWorkspaceSnapshotRefresh({ environmentId, instanceId, cwd: gitCwd }) | ||
| : idleRefreshAtom; | ||
| return useAtomRefresh(atom); | ||
| } |
Oops, something went wrong.
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.