Skip to content

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

TypeScript

fix(desktop): med-ownership re-check gesture ownership before each move dispatch

M5016a633e4ee
diff --git a/desktop-app/ui/src/components/SidebarNav/FileExplorerTree/index.tsx b/desktop-app/ui/src/components/SidebarNav/FileExplorerTree/index.tsx
index 20bc67151..9b044c6f9 100644
--- a/desktop-app/ui/src/components/SidebarNav/FileExplorerTree/index.tsx
+++ b/desktop-app/ui/src/components/SidebarNav/FileExplorerTree/index.tsx
@@ -698,7 +698,8 @@ export function FileExplorerTree({
async (
source: GfsDragSource,
destination: { resourceId: string; name: string },
- allowNumberedRename: boolean
+ allowNumberedRename: boolean,
+ isCurrent?: () => boolean
) => {
movingResourceIdRef.current = source.resourceId
setMovingResourceId(source.resourceId)
@@ -729,6 +730,7 @@ export function FileExplorerTree({
error instanceof Error ? error.message : String(error),
'operation'
),
+ ...(isCurrent ? { isCurrent } : {}),
},
{
source: {
@@ -795,7 +797,8 @@ export function FileExplorerTree({
await commitMove(
source,
{ resourceId: node.resourceId, name: node.name },
- decision.allowNumberedRename
+ decision.allowNumberedRename,
+ () => ownsDrop(source, epoch, sessionGeneration)
)
} finally {
finishNodeDropCleanup(source, epoch, sessionGeneration)
@@ -972,7 +975,9 @@ export function FileExplorerTree({
pending &&
ownsDrop(pending.source, pending.gestureGeneration, pending.sessionGeneration)
) {
- void commitMove(pending.source, pending.destination, allowNumberedRename)
+ void commitMove(pending.source, pending.destination, allowNumberedRename, () =>
+ ownsDrop(pending.source, pending.gestureGeneration, pending.sessionGeneration)
+ )
}
}}
/>
@@ -1007,7 +1012,7 @@ export function FileExplorerTree({
{ resourceId: destinationId, name: destinationName },
allowNumberedRename
)
- if (outcome.status === 'session-lost') {
+ if (outcome.status === 'session-lost' || outcome.status === 'gesture-lost') {
setMoveDialogTarget(null)
return
}
diff --git a/desktop-app/ui/src/gfs/__tests__/dragOwnership.test.tsx b/desktop-app/ui/src/gfs/__tests__/dragOwnership.test.tsx
index 7dccff4b9..ba95f3dae 100644
--- a/desktop-app/ui/src/gfs/__tests__/dragOwnership.test.tsx
+++ b/desktop-app/ui/src/gfs/__tests__/dragOwnership.test.tsx
@@ -147,7 +147,8 @@ for (const surface of ['sidebar', 'files'] as const) {
async function setup(
sourceKind: 'file' | 'directory' = 'directory',
delayed = false,
- denyTarget = false
+ denyTarget = false,
+ parkResolve = false
) {
const bits = await affordancesResult(heldAffordances(['read', 'write', 'delete']))
let release!: () => void
@@ -162,6 +163,29 @@ for (const surface of ['sidebar', 'files'] as const) {
}
return denyTarget && id === 'target' ? affordancesResult(heldAffordances(['read'])) : bits
})
+ // The COMMIT-TIME resolve is the last read before the PATCH. Parking it
+ // reproduces the window the ownership re-check exists for: the gesture
+ // can be superseded while the source is being re-resolved.
+ let resolveGate: Promise<void> | null = null
+ let releaseResolve!: () => void
+ if (parkResolve) {
+ resolveGate = new Promise<void>(resolve => {
+ releaseResolve = resolve
+ })
+ }
+ const resolve = vi.fn(async (uri: string) => {
+ if (resolveGate) await resolveGate
+ const id = uri.split('/').pop()!
+ return resolveResource(
+ id === 'external' || (id === 'source' && sourceKind === 'file')
+ ? resolvedFile(id, id === 'external' ? 'external.txt' : 'Source', {
+ parentResourceId: 'grand',
+ })
+ : resolvedDirectory(id, id === 'source' ? 'Source' : id, {
+ parentResourceId: id === 'grand' ? null : 'grand',
+ })
+ )
+ })
const moveResource = vi.fn(async (_id: string, _destination: string) => ({}))
Object.defineProperty(window, 'clerum', {
configurable: true,
@@ -175,18 +199,7 @@ for (const surface of ['sidebar', 'files'] as const) {
])
),
affordances,
- resolve: vi.fn(async (uri: string) => {
- const id = uri.split('/').pop()!
- return resolveResource(
- id === 'external' || (id === 'source' && sourceKind === 'file')
- ? resolvedFile(id, id === 'external' ? 'external.txt' : 'Source', {
- parentResourceId: 'grand',
- })
- : resolvedDirectory(id, id === 'source' ? 'Source' : id, {
- parentResourceId: id === 'grand' ? null : 'grand',
- })
- )
- }),
+ resolve,
},
},
})
@@ -254,6 +267,8 @@ for (const surface of ['sidebar', 'files'] as const) {
drop,
dataTransfer,
release,
+ releaseResolve,
+ resolve,
moveResource,
affordances,
toast,
@@ -427,5 +442,22 @@ for (const surface of ['sidebar', 'files'] as const) {
expect(h.moveResource.mock.calls[0]![0]).toBe('source')
expect(h.moveResource.mock.calls[0]![1]).toBe('target')
})
+
+ it('never dispatches when the gesture is superseded during the commit-time resolve', async () => {
+ // The drop is admitted, then the shared session takes a NEW gesture
+ // while the commit-time resolve of the source is still in flight. The
+ // PATCH must not go out: the superseded session no longer owns it.
+ const h = await setup('file', false, false, true)
+ h.start()
+ h.drop()
+ await waitFor(() => expect(h.resolve).toHaveBeenCalled())
+ expect(h.api().beginDrag(externalSource())).toBe(true)
+ await act(async () => {
+ h.releaseResolve()
+ })
+ expect(h.moveResource).not.toHaveBeenCalled()
+ expect(h.toast).not.toHaveBeenCalled()
+ expect(h.api().activeSource()?.resourceId).toBe('external')
+ })
})
}
diff --git a/desktop-app/ui/src/gfs/__tests__/moveCommit.test.ts b/desktop-app/ui/src/gfs/__tests__/moveCommit.test.ts
index e3efb288d..a808331ee 100644
--- a/desktop-app/ui/src/gfs/__tests__/moveCommit.test.ts
+++ b/desktop-app/ui/src/gfs/__tests__/moveCommit.test.ts
@@ -218,6 +218,46 @@ describe('commitGfsMove — server verdicts', () => {
})
})
+describe('commitGfsMove — gesture ownership (rev-2 §4.1)', () => {
+ it('never dispatches when the gesture is no longer current', async () => {
+ const moveResource = vi.fn()
+ const outcome = await commitGfsMove(deps({ moveResource, isCurrent: () => false }), plan())
+ expect(outcome).toEqual({ status: 'gesture-lost' })
+ expect(moveResource).not.toHaveBeenCalled()
+ })
+
+ it('re-checks ownership before every retry, so a gesture lost mid-ladder never re-PATCHes', async () => {
+ // The first attempt fails with a name conflict; the gesture is superseded
+ // while the numbered-name ladder is being prepared. The second attempt
+ // must not reach the server.
+ let checks = 0
+ const moveResource = vi.fn(async () => {
+ throw new Error(
+ 'Error invoking remote method: Error: 409 Conflict: a resource with this name already exists httpStatus=409'
+ )
+ })
+ const outcome = await commitGfsMove(
+ deps({
+ moveResource,
+ isCurrent: () => {
+ checks += 1
+ return checks === 1
+ },
+ }),
+ plan({ allowNumberedRename: true })
+ )
+ expect(outcome).toEqual({ status: 'gesture-lost' })
+ expect(moveResource).toHaveBeenCalledTimes(1)
+ })
+
+ it('dispatches when the gesture is still current', async () => {
+ const moveResource = vi.fn(async () => ({ resourceId: 'r-1', version: 8 }))
+ const outcome = await commitGfsMove(deps({ moveResource, isCurrent: () => true }), plan())
+ expect(outcome).toEqual({ status: 'committed', version: 8 })
+ expect(moveResource).toHaveBeenCalledTimes(1)
+ })
+})
+
describe('suggestNumberedMoveName', () => {
it('proposes the first free numbered name only when the plain name is taken', () => {
expect(suggestNumberedMoveName('report.md', new Set(['other.md']))).toBeUndefined()
diff --git a/desktop-app/ui/src/gfs/moveCommit.ts b/desktop-app/ui/src/gfs/moveCommit.ts
index 70c5f7c31..5d33fda7e 100644
--- a/desktop-app/ui/src/gfs/moveCommit.ts
+++ b/desktop-app/ui/src/gfs/moveCommit.ts
@@ -37,6 +37,8 @@ export type MoveCommitOutcome =
| { status: 'rate-limited'; message: string; retryAfterSeconds?: number }
| { status: 'unconfirmed'; message: string }
| { status: 'session-lost' }
+ /** The gesture that asked for this move is gone; nothing was dispatched. */
+ | { status: 'gesture-lost' }
export interface MoveCommitDeps {
/** Resolve by stable URI; throws on 403/404/transport like the read plane. */
@@ -50,6 +52,15 @@ export interface MoveCommitDeps {
) => Promise<{ resourceId: string; version: number }>
/** Central fail-closed classifier; returns true when the session died. */
isSessionAuthorityFailure: (error: unknown) => boolean
+ /**
+ * Gesture-ownership predicate, absent on paths that own no gesture (the
+ * explicit Move dialog). A drag can be superseded WHILE the commit-time
+ * resolve is in flight — a newer drag on another surface, a scope change, a
+ * revoked session — and the PATCH would then move the item with a session
+ * the user no longer owns. Checked immediately before every dispatch
+ * attempt (so it also covers the numbered-name retries), never after.
+ */
+ isCurrent?: () => boolean
}
function messageOf(error: unknown): string {
@@ -101,6 +112,12 @@ export async function commitGfsMove(
let moveName = snapshot.name
for (let attempt = 0; attempt < GFS_MOVE_MAX_NETWORK_ATTEMPTS; attempt += 1) {
+ // Ownership re-check (rev-2 §4.1): the gesture that asked for this move
+ // may have been superseded while the commit-time resolve — or a previous
+ // attempt — was in flight. A PATCH dispatched after that would move the
+ // item under a session the user no longer owns, so the gesture is
+ // re-validated at the dispatch boundary instead of only at admission.
+ if (deps.isCurrent?.() === false) return { status: 'gesture-lost' }
let error: unknown
try {
const receipt = await deps.moveResource(
@@ -246,6 +263,9 @@ export function toastMoveOutcome(
pushToast?.(outcome.message, 'info')
break
case 'session-lost':
+ // A superseded gesture never reached the server: there is nothing to
+ // report and nothing to apologize for.
+ case 'gesture-lost':
break
default:
pushToast?.(outcome.message, 'error')
diff --git a/desktop-app/ui/src/pages/FilesPage.tsx b/desktop-app/ui/src/pages/FilesPage.tsx
index e1c2b1dda..7ecdc4def 100644
--- a/desktop-app/ui/src/pages/FilesPage.tsx
+++ b/desktop-app/ui/src/pages/FilesPage.tsx
@@ -1317,11 +1317,17 @@ export function FilesPage({
}
/** Shared move dispatch (rev-2 §5) used by the drag path, the folder
- * confirmation and the Move dialog — one service, one code path. */
+ * confirmation and the Move dialog — one service, one code path.
+ *
+ * `isCurrent` is the gesture-ownership predicate: the drag path passes the
+ * ownership check it already runs at admission, so the commit re-validates
+ * it at the dispatch boundary too (the explicit Move dialog owns no gesture
+ * and passes none). */
const runMoveCommit = async (
source: GfsDragSource,
destination: { resourceId: string; name: string },
- allowNumberedRename: boolean
+ allowNumberedRename: boolean,
+ isCurrent?: () => boolean
): Promise<MoveCommitOutcome> => {
movingResourceRef.current = source.resourceId
setMovingResourceId(source.resourceId)
@@ -1348,6 +1354,7 @@ export function FilesPage({
source.parentKnown ? source.parentResourceId : null
),
isSessionAuthorityFailure: error => failClosedOnAuthorizationError(error),
+ ...(isCurrent ? { isCurrent } : {}),
},
{
source: {
@@ -1380,6 +1387,8 @@ export function FilesPage({
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')
@@ -1437,7 +1446,8 @@ export function FilesPage({
await runMoveCommit(
source,
{ resourceId: destination.resourceId, name: destination.name },
- decision.allowNumberedRename
+ decision.allowNumberedRename,
+ () => ownsDrop(source, dragGeneration, sessionGeneration)
)
} finally {
finishDropCleanup(source, dragGeneration, sessionGeneration)
@@ -1622,7 +1632,7 @@ export function FilesPage({
{ resourceId: destinationId, name: destinationName },
allowNumberedRename
)
- if (outcome.status === 'session-lost') {
+ if (outcome.status === 'session-lost' || outcome.status === 'gesture-lost') {
setMoveTarget(null)
return
}
@@ -2638,7 +2648,9 @@ export function FilesPage({
pending &&
ownsDrop(pending.source, pending.gestureGeneration, pending.sessionGeneration)
) {
- void runMoveCommit(pending.source, pending.destination, allowNumberedRename)
+ void runMoveCommit(pending.source, pending.destination, allowNumberedRename, () =>
+ ownsDrop(pending.source, pending.gestureGeneration, pending.sessionGeneration)
+ )
}
}}
/>