MCPcopy Create free account
hub / github.com/Noumena-Network/code / startWorkPollLoop

Function startWorkPollLoop

src/bridge/replBridge.ts:1868–2415  ·  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

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

Callers 1

initBridgeCoreFunction · 0.85

Calls 15

getPollIntervalConfigFunction · 0.85
onStateChangeFunction · 0.85
decodeWorkSecretFunction · 0.85
validateBridgeIdFunction · 0.85
onEnvironmentLostFunction · 0.85
isExpiredErrorTypeFunction · 0.85
isSuppressible403Function · 0.85
logForDiagnosticsNoPIIFunction · 0.85
extractHttpStatusFunction · 0.85
describeAxiosErrorFunction · 0.85
logEventFunction · 0.70
logForDebuggingFunction · 0.50

Tested by

no test coverage detected