Skip to content

Implement OptimisticOperationManager for fire-and-forget operations #580

Closed

Description

@malibio

Overview

Implement OptimisticOperationManager to handle fire-and-forget operations with automatic rollback on failure, per architecture doc section 5.

Background

  • Current operations wait for backend confirmation (slower UX)
  • Architecture doc describes fire-and-forget pattern with rollback
  • Structural changes should feel instant (<10ms perceived latency)
  • Rollback provides safety when backend transactions fail

Implementation Tasks

OptimisticOperationManager Service

Create src/lib/services/optimistic-operation-manager.svelte.ts:

class OptimisticOperationManager {
  async executeStructuralChange(
    optimisticUpdate: () => void,
    backendOperation: () => Promise<void>,
    rollback: () => void,
  ): Promise<void> {
    // Take snapshot for rollback
    const snapshot = structureTree.snapshot();

    try {
      // Apply optimistic change immediately (UI updates)
      optimisticUpdate();

      // Fire-and-forget: Persist to backend
      // Don't await - let LIVE SELECT confirm
      backendOperation().catch(error => {
        // Backend failed - rollback + notify
        structureTree.restore(snapshot);
        
        eventBus.emit({
          type: 'error:operation-failed',
          message: 'Failed to update hierarchy',
          error,
          canRetry: true,
          action: () => this.executeStructuralChange(optimisticUpdate, backendOperation, rollback)
        });
      });

    } catch (error) {
      // Optimistic update failed - rollback
      structureTree.restore(snapshot);
      throw error;
    }
  }
}

export const operationManager = new OptimisticOperationManager();

Integration with Operations

  • Update indentNode() to use operation manager
  • Update outdentNode() to use operation manager
  • Update createNode() to use operation manager
  • Update deleteNode() to use operation manager

Example Usage

async function handleIndent(nodeId: string) {
  await operationManager.executeStructuralChange(
    // Optimistic: Update local structure immediately
    () => structureTree.moveNode(nodeId, newParent),

    // Backend: Persist (fire-and-forget)
    async () => {
      await backend.moveNode(nodeId, newParent);
      // LIVE SELECT confirms success
    },

    // Rollback: Restore previous state
    () => {
      // Snapshot already taken by operation manager
    }
  );
}

Acceptance Criteria

  • OptimisticOperationManager service created
  • executeStructuralChange() method handles optimistic updates + rollback
  • All structural operations (indent/outdent/create/delete) use operation manager
  • Failed operations automatically roll back
  • Error events emitted for UI notification
  • Operations feel instant (<10ms perceived latency)
  • Tests verify rollback works correctly

Dependencies

Related Issues

Activity

  1. malibio commented on Nov 21, 2025

    @malibio
    CollaboratorAuthor

    Test Baseline

    Before starting implementation:

    • Test Files: 6 failed | 85 passed | 8 skipped (99 total)
    • Tests: 24 failed | 1659 passed | 102 skipped (1785 total)

    Failed tests:

    • HTTP adapter connection failures (expected - SurrealDB not running)
    • Integration tests requiring backend connection
    • Navigation store tests (pre-existing failures)

    Starting implementation with this baseline to ensure no new test regressions.

  2. self-assigned this
    on Nov 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions