Archon icon indicating copy to clipboard operation
Archon copied to clipboard

Fix API service header merge and retry logic bugs

Open Wirasm opened this issue 3 months ago • 0 comments

Issue Description

Two bugs identified in archon-ui-main/src/services/api.ts:

1. Header Merge Order Bug

Current code in apiRequest function (lines 85-91):

const response = await fetch(url, {
  headers: {
    'Content-Type': 'application/json',
    ...options.headers,
  },
  ...options, // This overwrites the merged headers entirely!
});

Problem: Placing ...options after headers overwrites all merged headers, breaking custom header functionality.

2. Overly Aggressive Retry Logic

Current retry function retries ALL errors including:

  • 4xx client errors (should not retry)
  • POST operations with side effects (crawls, uploads - causes duplicates)
  • Non-network errors

Problem: This can cause duplicate crawls/uploads and mask legitimate client errors.

Proposed Solution

Implement smart retry with FormData detection:

// Fix header merge order + FormData detection
const isFormData = options.body instanceof FormData;
const mergedHeaders: HeadersInit = {
  Accept: 'application/json',
  ...(options.headers ?? {}),
  ...(isFormData ? {} : { 'Content-Type': 'application/json' }),
};

const response = await fetch(url, {
  ...options,
  headers: mergedHeaders,
});
// Smart retry logic
export async function retry<T>(
  fn: () => Promise<T>,
  retries = 3,
  delay = 500,
  isRetryable: (err: unknown) => boolean = (err) => {
    // Only retry network errors or 5xx server errors
    return (err as any)?.name === 'TypeError' || 
           (typeof (err as any)?.status === 'number' && (err as any).status >= 500);
  }
): Promise<T> {
  // ... implementation
}

Impact

  • High Priority: Prevents duplicate operations and fixes header customization
  • Low Risk: Changes isolated to api.ts service layer
  • Backward Compatible: No breaking changes to existing API

Related Files

  • archon-ui-main/src/services/api.ts (main fix)
  • All API methods using FormData (uploadDocument function)

Issue identified by CodeRabbit review but not related to current TanStack migration work.

Wirasm avatar Sep 05 '25 11:09 Wirasm