diff --git a/frontend/src/lib/components/copilot/chat/global/sessionAccess.test.ts b/frontend/src/lib/components/copilot/chat/global/sessionAccess.test.ts index 4b0a9fe6b4..33a27424e5 100644 --- a/frontend/src/lib/components/copilot/chat/global/sessionAccess.test.ts +++ b/frontend/src/lib/components/copilot/chat/global/sessionAccess.test.ts @@ -14,7 +14,13 @@ vi.mock('$lib/workspaceProtectionRules.svelte', () => ({ rulesets.some((r) => r.rules.includes(kind)) })) -import { capabilitiesForRole, resolveSessionAccess } from './sessionAccess' +import { + capabilitiesForRole, + forgetReadOnlyVerdicts, + isReadOnlySessionWorkspace, + resolveSessionAccess, + settledReadOnlyVerdict +} from './sessionAccess' import { clearWorkspaceRoleCache } from '$lib/user' import { FULL_CAPABILITIES, legacyOperatorCapabilities } from '$lib/roleCapabilities' import type { OperatorSettings, RoleCapabilities } from '$lib/gen' @@ -263,3 +269,23 @@ describe('resolveSessionAccess', () => { expect(access.has('deploy')).toBe(true) }) }) + +describe('isReadOnlySessionWorkspace', () => { + it('keeps a read started before forgetReadOnlyVerdicts from overwriting a newer one', async () => { + forgetReadOnlyVerdicts() + protectionRules.mockClear() + let releaseOld!: (rulesets: unknown[]) => void + protectionRules.mockReturnValueOnce(new Promise((resolve) => (releaseOld = resolve))) + const old = isReadOnlySessionWorkspace('race') + + forgetReadOnlyVerdicts() + protectionRules.mockResolvedValueOnce([{ rules: ['DisableDirectDeployment'] }]) + expect(await isReadOnlySessionWorkspace('race')).toBe(true) + + releaseOld([]) + expect(await old).toBe(false) + expect(settledReadOnlyVerdict('race')).toBe(true) + expect(await isReadOnlySessionWorkspace('race')).toBe(true) + expect(protectionRules).toHaveBeenCalledTimes(2) + }) +}) diff --git a/frontend/src/lib/components/copilot/chat/global/sessionAccess.ts b/frontend/src/lib/components/copilot/chat/global/sessionAccess.ts index 27203858be..7c88aca527 100644 --- a/frontend/src/lib/components/copilot/chat/global/sessionAccess.ts +++ b/frontend/src/lib/components/copilot/chat/global/sessionAccess.ts @@ -130,6 +130,9 @@ function protects(rulesets: ProtectionRuleset[] | undefined): boolean { const READ_ONLY_TTL_MS = 60_000 const readOnlyVerdicts = new Map }>() const settledVerdicts = new Map() +// A read started before `forgetReadOnlyVerdicts` must not write its older answer back over +// the reads started after it. +let verdictGeneration = 0 /** The read-only verdict alone, for the UI around a session. Fails closed as the chat does; a * failed read is not kept, so the next ask reads the rules again. */ @@ -137,15 +140,25 @@ export function isReadOnlySessionWorkspace(workspace: string): Promise const cached = readOnlyVerdicts.get(workspace) if (cached && Date.now() - cached.at < READ_ONLY_TTL_MS) return cached.verdict const at = Date.now() + const generation = verdictGeneration const verdict = readProtectionRulesForWorkspace(workspace).then((rulesets) => { - if (!rulesets) readOnlyVerdicts.delete(workspace) - else settledVerdicts.set(workspace, { at, readOnly: protects(rulesets) }) + if (generation === verdictGeneration) { + if (!rulesets) readOnlyVerdicts.delete(workspace) + else settledVerdicts.set(workspace, { at, readOnly: protects(rulesets) }) + } return protects(rulesets) }) readOnlyVerdicts.set(workspace, { at, verdict }) return verdict } +/** Drops the memoized verdicts, so the next ask reads the rules. */ +export function forgetReadOnlyVerdicts(): void { + verdictGeneration++ + readOnlyVerdicts.clear() + settledVerdicts.clear() +} + /** The verdict read for `workspace` within the minute, without asking: what the UI stands on * while a fresh component waits for its own ask. */ export function settledReadOnlyVerdict(workspace: string): boolean | undefined { diff --git a/frontend/src/lib/components/copilot/workspaceAiHidden.svelte.ts b/frontend/src/lib/components/copilot/workspaceAiHidden.svelte.ts index 99d0cca9f0..08117674aa 100644 --- a/frontend/src/lib/components/copilot/workspaceAiHidden.svelte.ts +++ b/frontend/src/lib/components/copilot/workspaceAiHidden.svelte.ts @@ -39,6 +39,10 @@ export function useWorkspaceAiHidden(workspace: () => string | undefined) { /** Whether any verdict has landed yet, for any workspace. */ get settledOnce(): boolean { return verdict.current !== undefined + }, + /** Reads the verdict again, keeping the current one until the new one lands. */ + refresh() { + void verdict.refetch() } } } diff --git a/frontend/src/lib/components/sessions/SessionsView.svelte b/frontend/src/lib/components/sessions/SessionsView.svelte index 3361dab416..ab5b81d4c2 100644 --- a/frontend/src/lib/components/sessions/SessionsView.svelte +++ b/frontend/src/lib/components/sessions/SessionsView.svelte @@ -38,6 +38,7 @@ } from '$lib/components/sessions/PreviewRouterPicker.svelte' import { goto } from '$lib/navigation' import { useWorkspaceAiHidden } from '$lib/components/copilot/workspaceAiHidden.svelte' + import { rereadReadOnlyVerdicts } from './sessionPermissions.svelte' import SessionWrapper from '$lib/components/sessions/SessionWrapper.svelte' import type PreviewTabHost from '$lib/components/sessions/PreviewTabHost.svelte' import { useIsDarkMode } from '$lib/components/DarkModeObserver.svelte' @@ -293,12 +294,11 @@ }) ) - // Chat columns stay mounted (stacked, visibility-toggled), the view itself - // included while the user is on another page, so coming back keeps scroll - // position, MRU-capped like preview tabs: every mounted column is a full - // transcript in the DOM. Unmounting drops only the column — the runtime keeps - // the chat — so a column is kept while its composer holds unsent input - // (component-local) or its turn is running. + // Chat columns stay mounted (stacked, visibility-toggled), so switching back to a + // session keeps its scroll position, MRU-capped like preview tabs: every mounted + // column is a full transcript in the DOM. Unmounting drops only the column — the + // runtime keeps the chat — so a column is kept while its composer holds unsent + // input (component-local) or its turn is running. const MAX_MOUNTED_CHATS = 5 const mountedChatIds = new SvelteSet() function keepsChatMounted(id: string): boolean { @@ -313,6 +313,19 @@ if (oldest !== id && !keepsChatMounted(oldest)) mountedChatIds.delete(oldest) } } + // Off the route only the session shown last keeps its column, beside those + // `keepsChatMounted` holds. Hiding and showing the view restyles every column it holds, so + // each warm sibling would slow every navigation to and from this page, and hold its + // transcript in memory while the user is elsewhere. + $effect(() => { + if (shown) return + const keep = sessionId + untrack(() => { + for (const id of [...mountedChatIds]) { + if (id !== keep && !keepsChatMounted(id)) mountedChatIds.delete(id) + } + }) + }) // Off the route, an unsent session's column goes: it has no transcript to keep, its text is // on its record, and Home's composer can take that same session meanwhile, which a column // still holding the earlier text would contradict once the user is back. @@ -359,6 +372,20 @@ // that would refresh `copilotInfo`, so the verdict is read for the workspace itself. const workspaceAiHidden = useWorkspaceAiHidden(() => previewWorkspace) const aiHiddenVerdict = $derived(workspaceAiHidden.hidden) + // The AI setting and the protection rules are changed on other pages, so coming back reads + // them again: kept from before, an un-hidden workspace would stay covered and an unprotected + // one locked in Use until a reload. + let shownBefore = false + $effect(() => { + if (!shown) return + if (shownBefore) { + untrack(() => { + workspaceAiHidden.refresh() + rereadReadOnlyVerdicts() + }) + } + shownBefore = true + }) // The full loader is for the page's first verdict only. Later, picking another workspace in // "Acting on" leaves the session on screen, inert until its verdict lands: covering it for // a request reads as the page reloading. diff --git a/frontend/src/lib/components/sessions/sessionPermissions.svelte.ts b/frontend/src/lib/components/sessions/sessionPermissions.svelte.ts index c10e6215b7..1fd0e85098 100644 --- a/frontend/src/lib/components/sessions/sessionPermissions.svelte.ts +++ b/frontend/src/lib/components/sessions/sessionPermissions.svelte.ts @@ -6,6 +6,7 @@ import { useActingUser } from '$lib/actingUser.svelte' import { useEditRights } from '$lib/operatorWriteRights' import { buildsAnything } from '$lib/editRights' import { + forgetReadOnlyVerdicts, isReadOnlySessionWorkspace, settledReadOnlyVerdict } from '$lib/components/copilot/chat/global/sessionAccess' @@ -13,12 +14,23 @@ import { AIWorkMode } from '$lib/components/copilot/chat/workMode' import type { WorkspaceItem } from '$lib/components/workspacePicker' import { previewPageAllowed, type PreviewPage } from './previewRouter' +let verdictEpoch = $state(0) + +/** Every `useSessionReadOnly` reads the rules again: the sessions view stays mounted while the + * user is on other pages, where the rules can change. */ +export function rereadReadOnlyVerdicts(): void { + forgetReadOnlyVerdicts() + verdictEpoch++ +} + /** Whether a session in `workspace` is read-only because the workspace is protected: the verdict * `resolveSessionAccess` gives the chat, so the UI and the toolset agree. */ export function useSessionReadOnly(workspace: () => string | undefined) { - const verdict = resource(workspace, async (ws) => { + const verdict = resource([workspace, () => verdictEpoch], async ([ws], _prev, { signal }) => { if (!ws) return undefined const readOnly = await isReadOnlySessionWorkspace(ws) + // A superseded read answers for older rules: runed would still land it over the newer one. + if (signal.aborted) throw new DOMException('superseded', 'AbortError') // Only a successful read settles a verdict; a failed one is read-only to fail closed. return { workspace: ws, readOnly, rulesRead: settledReadOnlyVerdict(ws) !== undefined } })