MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / startWorkPollLoop

Function startWorkPollLoop

source code/bridge/replBridge.ts:1853–2400  ·  view source on GitHub ↗

* Persistent poll loop for work items. Runs in the background for the * lifetime of the bridge connection. * * When a work item arrives, acknowledges it and calls onWorkReceived * with the session ID and ingress token (which connects the ingress * WebSocket). Then continues polling — the server

({
  api,
  getCredentials,
  signal,
  onStateChange,
  onWorkReceived,
  onEnvironmentLost,
  getWsState,
  isAtCapacity,
  capacitySignal,
  onFatalError,
  getPollIntervalConfig = () => DEFAULT_POLL_CONFIG,
  getHeartbeatInfo,
  onHeartbeatFatal,
}: {
  api: BridgeApiClient
  getCredentials: () => { environmentId: string; environmentSecret: string }
  signal: AbortSignal
  onStateChange?: (state: BridgeState, detail?: string) => void
  onWorkReceived: (
    sessionId: string,
    ingressToken: string,
    workId: string,
    useCodeSessions: boolean,
  ) => void
  /** Called when the environment has been deleted. Returns new credentials or null. */
  onEnvironmentLost?: () => Promise<{
    environmentId: string
    environmentSecret: string
  } | null>
  /** Returns the current WebSocket readyState label for diagnostic logging. */
  getWsState?: () => string
  /**
   * Returns true when the caller cannot accept new work (transport already
   * connected). When true, the loop polls at the configured at-capacity
   * interval as a heartbeat only. Server-side BRIDGE_LAST_POLL_TTL is
   * 4 hours — anything shorter than that is sufficient for liveness.
   */
  isAtCapacity?: () => boolean
  /**
   * Produces a signal that aborts when capacity frees up (transport lost),
   * merged with the loop signal. Used to interrupt the at-capacity sleep
   * so recovery polling starts immediately.
   */
  capacitySignal?: () => CapacitySignal
  /** Called on unrecoverable errors (e.g. server-side expiry) to trigger full teardown. */
  onFatalError?: () => void
  /** Poll interval config getter — defaults to DEFAULT_POLL_CONFIG. */
  getPollIntervalConfig?: () => PollIntervalConfig
  /**
   * Returns the current work ID and session ingress token for heartbeat.
   * When null, heartbeat is not possible (no active work item).
   */
  getHeartbeatInfo?: () => {
    environmentId: string
    workId: string
    sessionToken: string
  } | null
  /**
   * Called when heartbeatWork throws BridgeFatalError (401/403/404/410 —
   * JWT expired or work item gone). Caller should tear down the transport
   * + work state so isAtCapacity() flips to false and the loop fast-polls
   * for the server's re-dispatched work item. When provided, the loop
   * SKIPS the at-capacity backoff sleep (which would otherwise cause a
   * ~10-minute dead window before recovery). When omitted, falls back to
   * the backoff sleep to avoid a tight poll+heartbeat loop.
   */
  onHeartbeatFatal?: (err: BridgeFatalError) => void
})

Source from the content-addressed store, hash-verified

1851 * reconnection without tearing down the bridge.
1852 */
1853async function startWorkPollLoop({
1854 api,
1855 getCredentials,
1856 signal,
1857 onStateChange,
1858 onWorkReceived,
1859 onEnvironmentLost,
1860 getWsState,
1861 isAtCapacity,
1862 capacitySignal,
1863 onFatalError,
1864 getPollIntervalConfig = () => DEFAULT_POLL_CONFIG,
1865 getHeartbeatInfo,
1866 onHeartbeatFatal,
1867}: {
1868 api: BridgeApiClient
1869 getCredentials: () => { environmentId: string; environmentSecret: string }
1870 signal: AbortSignal
1871 onStateChange?: (state: BridgeState, detail?: string) => void
1872 onWorkReceived: (
1873 sessionId: string,
1874 ingressToken: string,
1875 workId: string,
1876 useCodeSessions: boolean,
1877 ) => void
1878 /** Called when the environment has been deleted. Returns new credentials or null. */
1879 onEnvironmentLost?: () => Promise<{
1880 environmentId: string
1881 environmentSecret: string
1882 } | null>
1883 /** Returns the current WebSocket readyState label for diagnostic logging. */
1884 getWsState?: () => string
1885 /**
1886 * Returns true when the caller cannot accept new work (transport already
1887 * connected). When true, the loop polls at the configured at-capacity
1888 * interval as a heartbeat only. Server-side BRIDGE_LAST_POLL_TTL is
1889 * 4 hours — anything shorter than that is sufficient for liveness.
1890 */
1891 isAtCapacity?: () => boolean
1892 /**
1893 * Produces a signal that aborts when capacity frees up (transport lost),
1894 * merged with the loop signal. Used to interrupt the at-capacity sleep
1895 * so recovery polling starts immediately.
1896 */
1897 capacitySignal?: () => CapacitySignal
1898 /** Called on unrecoverable errors (e.g. server-side expiry) to trigger full teardown. */
1899 onFatalError?: () => void
1900 /** Poll interval config getter — defaults to DEFAULT_POLL_CONFIG. */
1901 getPollIntervalConfig?: () => PollIntervalConfig
1902 /**
1903 * Returns the current work ID and session ingress token for heartbeat.
1904 * When null, heartbeat is not possible (no active work item).
1905 */
1906 getHeartbeatInfo?: () => {
1907 environmentId: string
1908 workId: string
1909 sessionToken: string
1910 } | null

Callers 1

initBridgeCoreFunction · 0.85

Calls 15

logForDebuggingFunction · 0.85
getPollIntervalConfigFunction · 0.85
onStateChangeFunction · 0.85
logEventFunction · 0.85
sleepFunction · 0.85
decodeWorkSecretFunction · 0.85
validateBridgeIdFunction · 0.85
onEnvironmentLostFunction · 0.85
isExpiredErrorTypeFunction · 0.85
isSuppressible403Function · 0.85
logForDiagnosticsNoPIIFunction · 0.85
extractHttpStatusFunction · 0.85

Tested by

no test coverage detected