Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

refactor(desktop): med-dedup-drag-glue share the drop ownership and move commit glue

Ma00b1f57d810
diff --git a/desktop-app/ui/src/components/SidebarNav/FileExplorerTree/index.tsx b/desktop-app/ui/src/components/SidebarNav/FileExplorerTree/index.tsx
index 5763d6acd..c6f447f9a 100644
--- a/desktop-app/ui/src/components/SidebarNav/FileExplorerTree/index.tsx
+++ b/desktop-app/ui/src/components/SidebarNav/FileExplorerTree/index.tsx
@@ -18,14 +18,11 @@ import { resolveGfsPreview } from '@lib/gfsPreview'
import { sanitizeAppTabTitle } from '@lib/sanitizeAppTabTitle'
import { isGfsChildrenDenied, purgeDeniedGfsChildren } from '@/gfs/deniedChildren'
import { useGfsDragAdjudication } from '@/gfs/dragAdjudication'
+import { ownsGfsDrop } from '@/gfs/dragOwnership'
import type { GfsDragSource } from '@/gfs/dragSession'
import { useGfsDragSession } from '@/gfs/dragSession'
-import {
- commitGfsMove,
- moveDenialMessage,
- suggestNumberedMoveName,
- toastMoveOutcome,
-} from '@/gfs/moveCommit'
+import { moveDenialMessage, suggestNumberedMoveName } from '@/gfs/moveCommit'
+import { runGfsMoveCommit } from '@/gfs/moveCommitRunner'
import { GfsMoveConfirmDialog } from '@/gfs/moveConfirmDialog'
import { GfsMoveDialog } from '@/gfs/moveDialog'
import { IconChevronRight, IconContexts } from '../icons'
@@ -494,9 +491,15 @@ export function FileExplorerTree({
const ownsDrop = useCallback(
(source: GfsDragSource, epoch: number, sharedGeneration: number | null) =>
- source.scope === liveDragScopeRef.current &&
- epoch === dragSessionEpochRef.current &&
- (!dragSession || dragSession.currentGeneration() === sharedGeneration),
+ ownsGfsDrop(
+ source,
+ { epoch, sessionGeneration: sharedGeneration },
+ {
+ epoch: dragSessionEpochRef.current,
+ scope: liveDragScopeRef.current,
+ sessionGeneration: dragSession?.currentGeneration() ?? null,
+ }
+ ),
[dragSession]
)
@@ -707,70 +710,22 @@ export function FileExplorerTree({
movingResourceIdRef.current = source.resourceId
setMovingResourceId(source.resourceId)
try {
- // A transport loss after dispatch leaves the mutation REJECTED even
- // though the PATCH landed. The commit verifies that by resolve; the
- // convergence then has to run here, because the mutation's own success
- // path never did.
- let dispatchConfirmed = false
- const outcome = await commitGfsMove(
+ return await runGfsMoveCommit(
{
- resolveSource: async gfsUri => {
- const resolved = await window.clerum.gfs.resolve(gfsUri)
- return {
- resourceId: resolved.resourceId,
- gfsUri,
- kind: resolved.kind === 'directory' ? 'directory' : 'file',
- name: resolved.name,
- version: resolved.version,
- parentResourceId: resolved.parentResourceId,
- }
- },
- moveResource: async (resourceId, destinationId, ifMatch, newName, oldParentId) => {
- const receipt = await ctrl.moveResource(
- resourceId,
- destinationId,
- ifMatch,
- newName,
- oldParentId
- )
- dispatchConfirmed = true
- return receipt
- },
- isSessionAuthorityFailure: error =>
- ctrl.handleAuthorityFailure(
- error instanceof Error ? error.message : String(error),
- 'operation'
- ),
- ...(isCurrent ? { isCurrent } : {}),
+ moveResource: (resourceId, destinationId, ifMatch, newName, oldParentId) =>
+ ctrl.moveResource(resourceId, destinationId, ifMatch, newName, oldParentId),
+ handleAuthorityFailure: message => ctrl.handleAuthorityFailure(message, 'operation'),
+ convergeCommittedMove: (input, receipt) => ctrl.convergeCommittedMove(input, receipt),
+ pushToast,
},
{
- source: {
- resourceId: source.resourceId,
- gfsUri: source.gfsUri,
- kind: source.kind,
- name: source.name,
- },
+ source,
destination,
allowNumberedRename,
occupiedDestinationNames: cachedDestinationNames(destination.resourceId),
+ ...(isCurrent ? { isCurrent } : {}),
}
)
- if (outcome.status === 'committed' && !dispatchConfirmed) {
- // The PATCH landed but the response was lost: the mutation rejected,
- // so its success path (and the convergence it owns) never ran. The
- // item HAS moved — converge the cache now so it stops rendering in
- // the old folder.
- await ctrl.convergeCommittedMove(
- {
- resourceId: source.resourceId,
- destinationId: destination.resourceId,
- oldParentId: outcome.previousParentId,
- },
- { version: outcome.version }
- )
- }
- toastMoveOutcome(pushToast, outcome, source.name, destination.name, source.kind)
- return outcome
} finally {
movingResourceIdRef.current = null
setMovingResourceId(null)
diff --git a/desktop-app/ui/src/gfs/dragOwnership.ts b/desktop-app/ui/src/gfs/dragOwnership.ts
new file mode 100644
index 000000000..09b0d4e9b
--- /dev/null
+++ b/desktop-app/ui/src/gfs/dragOwnership.ts
@@ -0,0 +1,49 @@
+import type { GfsDragSource } from './dragSession.types'
+
+/**
+ * The gesture that owns a drop: the surface's local epoch and the shared
+ * session generation captured when the drop was admitted.
+ */
+export interface GfsDropOwnership {
+ /** The surface's local gesture counter value at admission. */
+ epoch: number
+ /** The shared session generation at admission (null without a session). */
+ sessionGeneration: number | null
+}
+
+/**
+ * The live gesture facts a surface owns right now.
+ */
+export interface GfsLiveDragState {
+ /** The surface's current local gesture counter value. */
+ epoch: number
+ /** The scope the surface is live under (null while revoked). */
+ scope: string | null
+ /** The shared session's current generation (null without a session). */
+ sessionGeneration: number | null
+}
+
+/**
+ * Drop ownership (rev-2 §4.1): the gesture that asked for this move must
+ * still be the live one. The Files page and the sidebar tree used to
+ * re-implement this check — and every divergence between their drop
+ * behaviour started there. One check, one contract:
+ *
+ * - the source must belong to the surface's LIVE scope (a revoked session or
+ * a scope change ends the gesture);
+ * - the surface's local gesture counter must still carry the admitted
+ * epoch (a newer drag on this surface ends the gesture);
+ * - the shared session must still carry the admitted generation (a drag
+ * taken by another surface ends the gesture).
+ */
+export function ownsGfsDrop(
+ source: GfsDragSource,
+ ownership: GfsDropOwnership,
+ live: GfsLiveDragState
+): boolean {
+ return (
+ source.scope === live.scope &&
+ ownership.epoch === live.epoch &&
+ ownership.sessionGeneration === live.sessionGeneration
+ )
+}
diff --git a/desktop-app/ui/src/gfs/moveCommitRunner.ts b/desktop-app/ui/src/gfs/moveCommitRunner.ts
new file mode 100644
index 000000000..092de5ff7
--- /dev/null
+++ b/desktop-app/ui/src/gfs/moveCommitRunner.ts
@@ -0,0 +1,128 @@
+import type { GfsDragSource } from './dragSession.types'
+import { commitGfsMove, toastMoveOutcome } from './moveCommit'
+import type { MoveCommitOutcome, MoveToast } from './moveCommit'
+
+/**
+ * The surface-level operations the shared commit path needs. They are the
+ * controller's own operations, passed in so this module stays free of React
+ * and of the controller's shape.
+ */
+export interface GfsMoveCommitRunnerDeps {
+ /** One move PATCH attempt; ifMatch is mandatory client policy. */
+ moveResource: (
+ resourceId: string,
+ destinationId: string,
+ ifMatch?: number,
+ newName?: string,
+ oldParentId?: string | null
+ ) => Promise<{ resourceId: string; version: number }>
+ /** Central fail-closed classifier; returns true when the session died. */
+ handleAuthorityFailure: (message: string) => boolean
+ /**
+ * Convergence for a move that landed WITHOUT a receipt (transport loss):
+ * the mutation rejected, so its success path never ran.
+ */
+ convergeCommittedMove: (
+ input: { resourceId: string; destinationId: string; oldParentId: string | null },
+ receipt: { version: number }
+ ) => Promise<void> | void
+ /** Transient outcome toasts; omitted where a surface shows its own. */
+ pushToast?: MoveToast
+}
+
+/** One move request, independent of the surface that produced it. */
+export interface GfsMoveCommitRequest {
+ source: GfsDragSource
+ destination: { resourceId: string; name: string }
+ /** Numbered renaming is pre-approved (source write proved or confirmed). */
+ allowNumberedRename: boolean
+ /** Names already known to be taken in the destination, when cached. */
+ occupiedDestinationNames?: ReadonlySet<string>
+ /**
+ * Gesture-ownership predicate, absent on paths that own no gesture (the
+ * explicit Move dialog). Checked before every dispatch attempt.
+ */
+ isCurrent?: () => boolean
+}
+
+/**
+ * The ONE move commit path for every Desktop surface (rev-2 §5): the Files
+ * page, the sidebar file explorer and the Move-dialog adapters all dispatch
+ * through it. The surfaces used to re-implement the same wrapper — the
+ * deps, the plan, the outcome toasts and the transport-loss convergence —
+ * and every divergence between them started there.
+ *
+ * Behavior (unchanged, now stated once):
+ * - the source is re-resolved immediately before dispatch: its fresh version
+ * is the ifMatch, its fresh parent is the convergence's old parent;
+ * - a transport loss is verified by resolve, never re-PATCHed, and the
+ * verified-committed outcome still converges the cache;
+ * - the outcome toasts are the shared UX matrix copy.
+ */
+export async function runGfsMoveCommit(
+ deps: GfsMoveCommitRunnerDeps,
+ request: GfsMoveCommitRequest
+): Promise<MoveCommitOutcome> {
+ const { source, destination, allowNumberedRename } = request
+ // A transport loss after dispatch leaves the dispatch unconfirmed even
+ // though the PATCH may have landed.
+ let dispatchConfirmed = false
+ const outcome = await commitGfsMove(
+ {
+ resolveSource: async gfsUri => {
+ const resolved = await window.clerum.gfs.resolve(gfsUri)
+ return {
+ resourceId: resolved.resourceId,
+ gfsUri,
+ kind: resolved.kind === 'directory' ? 'directory' : 'file',
+ name: resolved.name,
+ version: resolved.version,
+ parentResourceId: resolved.parentResourceId,
+ }
+ },
+ moveResource: async (resourceId, destinationId, ifMatch, newName, oldParentId) => {
+ const receipt = await deps.moveResource(
+ resourceId,
+ destinationId,
+ ifMatch,
+ newName,
+ oldParentId
+ )
+ dispatchConfirmed = true
+ return receipt
+ },
+ isSessionAuthorityFailure: error =>
+ deps.handleAuthorityFailure(error instanceof Error ? error.message : String(error)),
+ ...(request.isCurrent ? { isCurrent: request.isCurrent } : {}),
+ },
+ {
+ source: {
+ resourceId: source.resourceId,
+ gfsUri: source.gfsUri,
+ kind: source.kind,
+ name: source.name,
+ },
+ destination,
+ allowNumberedRename,
+ ...(request.occupiedDestinationNames
+ ? { occupiedDestinationNames: request.occupiedDestinationNames }
+ : {}),
+ }
+ )
+ if (outcome.status === 'committed' && !dispatchConfirmed) {
+ // The PATCH landed but the response was lost: the dispatch never
+ // confirmed, so the convergence it would have run never ran. The item
+ // HAS moved — converge the cache now so it stops rendering in the old
+ // folder.
+ await deps.convergeCommittedMove(
+ {
+ resourceId: source.resourceId,
+ destinationId: destination.resourceId,
+ oldParentId: outcome.previousParentId,
+ },
+ { version: outcome.version }
+ )
+ }
+ toastMoveOutcome(deps.pushToast, outcome, source.name, destination.name, source.kind)
+ return outcome
+}
diff --git a/desktop-app/ui/src/pages/FilesPage.tsx b/desktop-app/ui/src/pages/FilesPage.tsx
index 147fb8049..d5426e97d 100644
--- a/desktop-app/ui/src/pages/FilesPage.tsx
+++ b/desktop-app/ui/src/pages/FilesPage.tsx
@@ -65,11 +65,13 @@ import {
} from '@/gfs/delegation'
import type { GfsInheritedAccessSource, GfsInheritedCoverageState } from '@/gfs/delegation.types'
import { useGfsDragAdjudication } from '@/gfs/dragAdjudication'
-import type { GfsDragSource, GfsDropRejection, GfsDropVerdict } from '@/gfs/dragSession'
+import { ownsGfsDrop } from '@/gfs/dragOwnership'
+import type { GfsDragSource, GfsDropVerdict } from '@/gfs/dragSession'
import { useGfsDragSession } from '@/gfs/dragSession'
import { GfsFilePicker } from '@/gfs/filePicker'
-import { commitGfsMove, suggestNumberedMoveName } from '@/gfs/moveCommit'
+import { moveDenialMessage, suggestNumberedMoveName } from '@/gfs/moveCommit'
import type { MoveCommitOutcome } from '@/gfs/moveCommit'
+import { runGfsMoveCommit } from '@/gfs/moveCommitRunner'
import { GfsMoveConfirmDialog } from '@/gfs/moveConfirmDialog'
import { GfsMoveDialog } from '@/gfs/moveDialog'
import type { TeamDirectoryResult } from '../../../src/types'
@@ -429,9 +431,15 @@ export function FilesPage({
}, [adjudication.endDragSession, liveDragScope])
const ownsDrop = (source: GfsDragSource, epoch: number, sharedGeneration: number | null) =>
- source.scope === liveDragScopeRef.current &&
- epoch === dragSessionRef.current &&
- (!dragSession || dragSession.currentGeneration() === sharedGeneration)
+ ownsGfsDrop(
+ source,
+ { epoch, sessionGeneration: sharedGeneration },
+ {
+ epoch: dragSessionRef.current,
+ scope: liveDragScopeRef.current,
+ sessionGeneration: dragSession?.currentGeneration() ?? null,
+ }
+ )
const lastRemoteGfsChangeEpochRef = useRef(remoteGfsChangeEpoch)
useEffect(() => {
@@ -1358,33 +1366,6 @@ export function FilesPage({
return new Set((cached?.pages ?? []).flatMap(page => page.items?.map(item => item.name) ?? []))
}
- const dropDenialMessage = (
- rejection: GfsDropRejection,
- destinationName: string
- ): string | null => {
- switch (rejection.reason) {
- case 'destination-write':
- return `You can’t move files to ${destinationName} because you don’t have write permission for this folder.`
- case 'source-write':
- return 'You cannot move this item out of its current folder without write access there.'
- case 'source-delete':
- return 'You do not have permission to remove this item from its current folder.'
- case 'same-parent':
- return 'This item is already in this folder.'
- case 'self':
- case 'descendant':
- return 'A folder cannot be moved into itself or one of its subfolders.'
- case 'root-source':
- return 'The top-level folder cannot be moved.'
- case 'cross-drive':
- return 'Items can’t be moved between drives.'
- case 'session-lost':
- return null
- default:
- return rejection.message ?? 'This move is not available right now. Try again in a moment.'
- }
- }
-
/** Shared move dispatch (rev-2 §5) used by the drag path, the folder
* confirmation and the Move dialog — one service, one code path.
*
@@ -1401,90 +1382,22 @@ export function FilesPage({
movingResourceRef.current = source.resourceId
setMovingResourceId(source.resourceId)
try {
- // A transport loss after dispatch leaves the mutation REJECTED even
- // though the PATCH landed. The commit verifies that by resolve; the
- // convergence then has to run here, because the mutation's own success
- // path never did.
- let dispatchConfirmed = false
- const outcome = await commitGfsMove(
+ return await runGfsMoveCommit(
{
- resolveSource: async gfsUri => {
- const resolved = await window.clerum.gfs.resolve(gfsUri)
- return {
- resourceId: resolved.resourceId,
- gfsUri,
- kind: resolved.kind === 'directory' ? 'directory' : 'file',
- name: resolved.name,
- version: resolved.version,
- parentResourceId: resolved.parentResourceId,
- }
- },
- moveResource: async (resourceId, destinationId, ifMatch, newName, oldParentId) => {
- const receipt = await ctrl.moveResource(
- resourceId,
- destinationId,
- ifMatch,
- newName,
- oldParentId
- )
- dispatchConfirmed = true
- return receipt
- },
- isSessionAuthorityFailure: error => failClosedOnAuthorizationError(error),
- ...(isCurrent ? { isCurrent } : {}),
+ moveResource: (resourceId, destinationId, ifMatch, newName, oldParentId) =>
+ ctrl.moveResource(resourceId, destinationId, ifMatch, newName, oldParentId),
+ handleAuthorityFailure: message => ctrl.handleAuthorityFailure(message, 'operation'),
+ convergeCommittedMove: (input, receipt) => ctrl.convergeCommittedMove(input, receipt),
+ ...(pushToast ? { pushToast } : {}),
},
{
- source: {
- resourceId: source.resourceId,
- gfsUri: source.gfsUri,
- kind: source.kind,
- name: source.name,
- },
+ source,
destination,
allowNumberedRename,
occupiedDestinationNames: cachedDestinationNames(destination.resourceId),
+ ...(isCurrent ? { isCurrent } : {}),
}
)
- if (outcome.status === 'committed' && !dispatchConfirmed) {
- // The PATCH landed but the response was lost: the mutation rejected,
- // so its success path (and the convergence it owns) never ran. The
- // item HAS moved — converge the cache now so it stops rendering in
- // the old folder.
- await ctrl.convergeCommittedMove(
- {
- resourceId: source.resourceId,
- destinationId: destination.resourceId,
- oldParentId: outcome.previousParentId,
- },
- { version: outcome.version }
- )
- }
- switch (outcome.status) {
- case 'committed':
- pushToast?.(
- outcome.renamedTo
- ? `Moved ${source.name} to ${destination.name} as ${outcome.renamedTo}`
- : `Moved ${source.name} to ${destination.name}`,
- 'success'
- )
- if (source.kind === 'directory') {
- pushToast?.(
- 'Moving a folder changes who can access its contents — access now follows the new location.',
- 'info'
- )
- }
- break
- case 'no-op':
- pushToast?.(outcome.message, 'info')
- break
- case 'session-lost':
- // A superseded gesture never reached the server: nothing to report.
- case 'gesture-lost':
- break
- default:
- pushToast?.(outcome.message, 'error')
- }
- return outcome
} finally {
movingResourceRef.current = null
setMovingResourceId(null)
@@ -1515,7 +1428,7 @@ export function FilesPage({
})
if (!ownsDrop(source, dragGeneration, sessionGeneration)) return
if (!decision.allowed) {
- const message = dropDenialMessage(decision, destination.name)
+ const message = moveDenialMessage(decision, destination.name)
if (message) pushToast?.(message, 'error')
return
}