Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
23.7 KB
Raw Blame
import { randomBytes } from "node:crypto"
import { and, asc, desc, eq, inArray, isNull } from "@omnirush-ee/den-db/drizzle"
import {
  AuditEventTable,
  AuthUserTable,
  DaytonaSandboxTable,
  MemberTable,
  WorkerBundleTable,
  WorkerInstanceTable,
  WorkerTable,
  WorkerTokenTable,
} from "@omnirush-ee/den-db/schema"
import { createDenTypeId, normalizeDenTypeId } from "@omnirush-ee/utils/typeid"
import { z } from "zod"
import { requireCloudWorkerAccess } from "../../billing/polar.js"
import { db } from "../../db.js"
import { env } from "../../env.js"
import type { UserOrganizationsContext } from "../../middleware/index.js"
import { denTypeIdSchema } from "../../openapi.js"
import { appLogger } from "../../observability/logger.js"
import type { AuthContextVariables } from "../../session.js"
import { materializeCloudWorkerProviders } from "../../llm/cloud-provider-materialization.js"
import { deprovisionWorker, provisionWorker } from "../../workers/provisioner.js"
import { withProvisionDeadline } from "../../workers/provision-deadline.js"
import { touchProvisioningWorker, withProvisioningHeartbeat } from "../../workers/provisioning-heartbeat.js"
import {
  cloudStartupFailureUpdate,
  createCloudStartupFailure,
  type CloudStartupFailure,
} from "../../workers/cloud-failure.js"
import { customDomainForWorker } from "../../workers/vanity-domain.js"
import { resolveCloudRuntimeAccess } from "../../workers/worker-access.js"
import { CLOUD_INSTANCE_BACKEND } from "../../workers/cloud-constants.js"
import { fetchPreviewNoRedirect } from "../../workers/preview-fetch.js"
import {
  getOmniRushWebRuntimeAccess,
  omniRushWebAccessRequiredPayload,
  requireOmniRushWebRuntimeAccess,
  type OmniRushWebRuntimeAccessResolver,
} from "../../omnirush-web-runtime-access.js"

const logger = appLogger.child({ component: "worker_routes" })

export const createWorkerSchema = z.object({
  name: z.string().min(1),
  description: z.string().optional(),
  destination: z.enum(["local", "cloud"]),
  workspacePath: z.string().optional(),
  sandboxBackend: z.string().optional(),
  imageVersion: z.string().optional(),
})

export const updateWorkerSchema = z.object({
  name: z.string().trim().min(1).max(255),
})

export const listWorkersQuerySchema = z.object({
  limit: z.coerce.number().int().min(1).max(50).default(20),
})

export const activityHeartbeatSchema = z.object({
  sentAt: z.string().datetime().optional(),
  isActiveRecently: z.boolean(),
  lastActivityAt: z.string().datetime().optional().nullable(),
  openSessionCount: z.number().int().min(0).optional(),
})

export const workerIdParamSchema = z.object({
  id: denTypeIdSchema("worker"),
})

export type WorkerRouteVariables = AuthContextVariables & Partial<UserOrganizationsContext>

type WorkerRow = typeof WorkerTable.$inferSelect
type WorkerInstanceRow = typeof WorkerInstanceTable.$inferSelect
type WorkerStatus = WorkerRow["status"]
export type WorkerId = WorkerRow["id"]
type OrgId = typeof MemberTable.$inferSelect.organizationId
type UserId = typeof AuthUserTable.$inferSelect.id
type ProvisionWorker = typeof provisionWorker
type ProvisionedWorker = Awaited<ReturnType<ProvisionWorker>>
type ResolveCloudRuntimeAccess = typeof resolveCloudRuntimeAccess
type LoadActiveWorkerTokens = (workerId: WorkerId) => Promise<Array<{
  scope: typeof WorkerTokenTable.$inferSelect.scope
  token: string
}>>
type CloudProvisioningStore = {
  updateWorkerStatus: (input: {
    workerId: WorkerId
    status: WorkerStatus
    imageVersion?: string | null
    failure?: CloudStartupFailure | null
    onlyWhenStatus?: WorkerStatus
    onlyWhenStatusIn?: WorkerStatus[]
  }) => Promise<void>
  insertWorkerInstance: (input: { workerId: WorkerId; provisioned: ProvisionedWorker }) => Promise<void>
  touchProvisioningWorker: (workerId: WorkerId) => Promise<void>
}
type ContinueCloudProvisioningOptions = {
  getOmniRushWebAccess?: OmniRushWebRuntimeAccessResolver
  provisionWorker?: ProvisionWorker
  store?: CloudProvisioningStore
  materializeProviders?: typeof materializeCloudWorkerProviders
  deadlineMs?: number
  heartbeatIntervalMs?: number
}

export const token = () => randomBytes(32).toString("hex")
const provisioningSuccessWritableStatuses: WorkerStatus[] = ["provisioning", "failed"]
const cloudProvisioningInFlight = new Map<WorkerId, Promise<void>>()

const databaseCloudProvisioningStore: CloudProvisioningStore = {
  async updateWorkerStatus(input) {
    const statusPredicate = input.onlyWhenStatusIn
      ? inArray(WorkerTable.status, input.onlyWhenStatusIn)
      : input.onlyWhenStatus
        ? eq(WorkerTable.status, input.onlyWhenStatus)
        : undefined

    const update = {
      status: input.status,
      ...(input.imageVersion === undefined ? {} : { image_version: input.imageVersion }),
      ...(input.failure === undefined ? {} : cloudStartupFailureUpdate(input.failure)),
    }

    await db
      .update(WorkerTable)
      .set(update)
      .where(statusPredicate
        ? and(eq(WorkerTable.id, input.workerId), statusPredicate)
        : eq(WorkerTable.id, input.workerId))
  },
  async insertWorkerInstance(input) {
    await db.insert(WorkerInstanceTable).values({
      id: createDenTypeId("workerInstance"),
      worker_id: input.workerId,
      provider: input.provisioned.provider,
      region: input.provisioned.region,
      url: persistedWorkerInstanceUrl(input.provisioned),
      status: input.provisioned.status,
    })
  },
  touchProvisioningWorker,
}

export function persistedWorkerInstanceUrl(provisioned: Pick<ProvisionedWorker, "provider" | "url">) {
  const lifecycleBaseUrl = env.apiPublicUrl ?? env.betterAuthUrl
  return provisioned.provider === "daytona"
    ? `${lifecycleBaseUrl.replace(/\/+$/, "")}/v1/cloud/instance`
    : provisioned.url
}

export function workerSandboxBackend(input: Pick<z.infer<typeof createWorkerSchema>, "destination" | "sandboxBackend">) {
  if (input.destination === "cloud" && env.provisionerMode === "daytona") return CLOUD_INSTANCE_BACKEND
  return input.sandboxBackend ?? null
}

export function parseWorkerIdParam(value: string): WorkerId {
  return normalizeDenTypeId("worker", value)
}

export function parseUserId(value: string): UserId {
  return normalizeDenTypeId("user", value)
}

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null
}

function normalizeUrl(value: string): string {
  return value.trim().replace(/\/+$/, "")
}

function parseWorkspaceSelection(payload: unknown): { workspaceId: string; omnirushUrl: string } | null {
  if (!isRecord(payload) || !Array.isArray(payload.items)) {
    return null
  }

  const activeId = typeof payload.activeId === "string" && payload.activeId.trim() ? payload.activeId.trim() : null
  let workspaceId = activeId

  if (!workspaceId) {
    for (const item of payload.items) {
      if (isRecord(item) && typeof item.id === "string" && item.id.trim()) {
        workspaceId = item.id.trim()
        break
      }
    }
  }

  const baseUrl = typeof payload.baseUrl === "string" ? normalizeUrl(payload.baseUrl) : ""
  if (!workspaceId || !baseUrl) {
    return null
  }

  return {
    workspaceId,
    omnirushUrl: `${baseUrl}/w/${encodeURIComponent(workspaceId)}`,
  }
}

async function resolveConnectUrlFromWorker(instanceUrl: string, clientToken: string, fetchImpl: typeof fetch = fetch) {
  const baseUrl = normalizeUrl(instanceUrl)
  if (!baseUrl || !clientToken.trim()) {
    return null
  }

  try {
    const response = await fetchPreviewNoRedirect(fetchImpl, `${baseUrl}/workspaces`, {
      method: "GET",
      headers: {
        Accept: "application/json",
        Authorization: `Bearer ${clientToken.trim()}`,
      },
    })

    if (!response.ok) {
      return null
    }

    const payload = (await response.json()) as unknown
    const selected = parseWorkspaceSelection({
      ...(isRecord(payload) ? payload : {}),
      baseUrl,
    })
    return selected
  } catch {
    return null
  }
}

function getConnectUrlCandidates(workerId: WorkerId, instanceUrl: string | null) {
  const candidates: string[] = []
  const vanityHostname = customDomainForWorker(workerId, env.render.workerPublicDomainSuffix)
  if (vanityHostname) {
    candidates.push(`https://${vanityHostname}`)
  }

  if (instanceUrl) {
    const normalized = normalizeUrl(instanceUrl)
    if (normalized && !candidates.includes(normalized)) {
      candidates.push(normalized)
    }
  }

  return candidates
}

export function readBearerToken(value: string | undefined) {
  const trimmed = value?.trim() ?? ""
  if (!trimmed.toLowerCase().startsWith("bearer ")) {
    return null
  }
  const tokenValue = trimmed.slice(7).trim()
  return tokenValue ? tokenValue : null
}

export function cloudWorkerCompatibilityUrl(workerId: WorkerId, apiPublicUrl: string | undefined, workspaceId?: string | null) {
  if (!apiPublicUrl) return null
  const base = apiPublicUrl.replace(/\/+$/, "")
  const route = `${base}/v1/cloud/workers/${encodeURIComponent(workerId)}`
  const workspace = workspaceId?.trim()
  return workspace ? `${route}/w/${encodeURIComponent(workspace)}` : route
}

export function parseHeartbeatTimestamp(value: string | null | undefined) {
  if (!value) {
    return null
  }
  const parsed = new Date(value)
  if (Number.isNaN(parsed.getTime())) {
    return null
  }
  return parsed
}

export function newerDate(current: Date | null | undefined, candidate: Date | null | undefined) {
  if (!candidate) {
    return current ?? null
  }
  if (!current) {
    return candidate
  }
  return candidate.getTime() > current.getTime() ? candidate : current
}

async function resolveConnectUrlFromCandidates(workerId: WorkerId, instanceUrl: string | null, clientToken: string) {
  const candidates = getConnectUrlCandidates(workerId, instanceUrl)
  for (const candidate of candidates) {
    const resolved = await resolveConnectUrlFromWorker(candidate, clientToken)
    if (resolved) {
      return resolved
    }
  }
  return null
}

async function getWorkerRuntimeAccess(worker: WorkerRow, resolveCloudAccess: ResolveCloudRuntimeAccess) {
  if (worker.destination === "cloud" && worker.sandbox_backend === CLOUD_INSTANCE_BACKEND) {
    const resolved = await resolveCloudAccess({ organizationId: worker.org_id, workerId: worker.id })
    if (resolved.status !== "ready") return null
    return { hostToken: resolved.hostToken, candidates: [resolved.url] }
  }

  const instance = await getLatestWorkerInstance(worker.id)
  const tokenRows = await db
    .select()
    .from(WorkerTokenTable)
    .where(and(eq(WorkerTokenTable.worker_id, worker.id), isNull(WorkerTokenTable.revoked_at)))
    .orderBy(asc(WorkerTokenTable.created_at))

  const hostToken = tokenRows.find((entry) => entry.scope === "host")?.token ?? null
  if (!instance?.url || !hostToken) {
    return null
  }

  return {
    instance,
    hostToken,
    candidates: getConnectUrlCandidates(worker.id, instance.url),
  }
}

export async function fetchWorkerRuntimeJson(input: {
  worker: WorkerRow
  path: string
  method?: "GET" | "POST"
  body?: unknown
}, options: {
  getOmniRushWebAccess?: OmniRushWebRuntimeAccessResolver
  resolveCloudAccess?: ResolveCloudRuntimeAccess
  fetchImpl?: typeof fetch
} = {}) {
  // Published desktops hold cloud worker tokens only after OmniRushWebAccessGate
  // (v0.18.42+) granted Web access; this recheck covers lapsed entitlement and
  // callers that bypass the gate. See omnirush-web-runtime-access.ts.
  if (input.worker.destination === "cloud") {
    const webAccess = await (options.getOmniRushWebAccess ?? getOmniRushWebRuntimeAccess)(input.worker.org_id)
    if (!webAccess.hasAccess) {
      return {
        ok: false as const,
        status: 403,
        payload: omniRushWebAccessRequiredPayload(),
      }
    }
  }
  const access = await getWorkerRuntimeAccess(input.worker, options.resolveCloudAccess ?? resolveCloudRuntimeAccess)
  if (!access) {
    return {
      ok: false as const,
      status: 409,
      payload: {
        error: "worker_runtime_unavailable",
        message: "Worker runtime access is not ready yet. Wait for provisioning to finish and try again.",
      },
    }
  }

  let lastPayload: unknown = null
  let lastStatus = 502

  for (const candidate of access.candidates) {
    try {
      const response = await fetchPreviewNoRedirect(options.fetchImpl ?? fetch, `${normalizeUrl(candidate)}${input.path}`, {
        method: input.method ?? "GET",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
          "X-OmniRush-Host-Token": access.hostToken,
        },
        body: input.body === undefined ? undefined : JSON.stringify(input.body),
      })

      const text = await response.text()
      lastStatus = response.status
      try {
        lastPayload = text ? JSON.parse(text) : null
      } catch {
        lastPayload = text ? { message: text } : null
      }

      if (response.ok) {
        return { ok: true as const, status: response.status, payload: lastPayload }
      }
    } catch (error) {
      lastPayload = { message: error instanceof Error ? error.message : "worker_request_failed" }
    }
  }

  return { ok: false as const, status: lastStatus, payload: lastPayload }
}

export async function countUserCloudWorkers(userId: UserId) {
  const rows = await db
    .select({ id: WorkerTable.id })
    .from(WorkerTable)
    .where(and(eq(WorkerTable.created_by_user_id, userId), eq(WorkerTable.destination, "cloud")))
    .limit(2)

  return rows.length
}

export async function getLatestWorkerInstance(workerId: WorkerId) {
  const rows = await db
    .select()
    .from(WorkerInstanceTable)
    .where(eq(WorkerInstanceTable.worker_id, workerId))
    .orderBy(desc(WorkerInstanceTable.created_at))
    .limit(1)

  return rows[0] ?? null
}

async function loadActiveWorkerTokens(workerId: WorkerId) {
  return db
    .select({ scope: WorkerTokenTable.scope, token: WorkerTokenTable.token })
    .from(WorkerTokenTable)
    .where(and(eq(WorkerTokenTable.worker_id, workerId), isNull(WorkerTokenTable.revoked_at)))
    .orderBy(asc(WorkerTokenTable.created_at))
}

export function toInstanceResponse(instance: WorkerInstanceRow | null) {
  if (!instance) {
    return null
  }

  return {
    provider: instance.provider,
    region: instance.region,
    url: instance.provider === "daytona" ? null : instance.url,
    status: instance.status,
    createdAt: instance.created_at,
    updatedAt: instance.updated_at,
  }
}

export function toWorkerResponse(row: WorkerRow, userId: string) {
  return {
    id: row.id,
    orgId: row.org_id,
    createdByUserId: row.created_by_user_id,
    isMine: row.created_by_user_id === userId,
    name: row.name,
    description: row.description,
    destination: row.destination,
    status: row.status,
    imageVersion: row.image_version,
    workspacePath: row.workspace_path,
    sandboxBackend: row.sandbox_backend,
    lastHeartbeatAt: row.last_heartbeat_at,
    lastActiveAt: row.last_active_at,
    createdAt: row.created_at,
    updatedAt: row.updated_at,
  }
}

async function runCloudProvisioning(input: {
  workerId: WorkerId
  orgId?: OrgId
  name: string
  hostToken: string
  clientToken: string
  activityToken: string
}, options: ContinueCloudProvisioningOptions) {
  const provision = options.provisionWorker ?? provisionWorker
  const store = options.store ?? databaseCloudProvisioningStore
  const materializeProviders = options.materializeProviders ?? materializeCloudWorkerProviders
  const deadlineMs = options.deadlineMs ?? env.cloudProvisionDeadlineMs

  try {
    if (!input.orgId) throw new Error("cloud_worker_organization_required")
    // Entitlement can lapse between claim and provisioning; a lapse is recorded
    // as the dedicated web_access_required failure (cloud-failure.ts), which the
    // published desktop renders through its existing failed-instance state.
    await requireOmniRushWebRuntimeAccess(
      input.orgId,
      options.getOmniRushWebAccess ?? getOmniRushWebRuntimeAccess,
    )
    await withProvisioningHeartbeat({
      workerId: input.workerId,
      touch: store.touchProvisioningWorker,
      intervalMs: options.heartbeatIntervalMs,
      run: async () => {
        const provisioned = await withProvisionDeadline({
          promise: provision({
            workerId: input.workerId,
            name: input.name,
            hostToken: input.hostToken,
            clientToken: input.clientToken,
            activityToken: input.activityToken,
          }),
          deadlineMs,
          label: `cloud provisioning for ${input.workerId}`,
        })

        if (provisioned.status === "healthy" && input.orgId) {
          try {
            await materializeProviders({
              organizationId: input.orgId,
              workerId: input.workerId,
              instanceUrl: provisioned.url,
              hostToken: input.hostToken,
              clientToken: input.clientToken,
              force: true,
            })
          } catch (error) {
            logger.warn("worker provisioning provider materialization warning", {
              worker_id: input.workerId,
              message: error instanceof Error ? error.message : "provider_materialization_failed",
            })
          }
        }

        await store.updateWorkerStatus({
          workerId: input.workerId,
          status: provisioned.status,
          imageVersion: provisioned.imageVersion,
          failure: null,
          onlyWhenStatusIn: provisioningSuccessWritableStatuses,
        })

        await store.insertWorkerInstance({ workerId: input.workerId, provisioned })
      },
    })
  } catch (error) {
    const failure = createCloudStartupFailure({ stage: "provisioning", error })
    await store.updateWorkerStatus({
      workerId: input.workerId,
      status: "failed",
      failure,
      onlyWhenStatus: "provisioning",
    })

    logger.error("worker provisioning failed", {
      worker_id: input.workerId,
      failure_code: failure.code,
      failure_stage: failure.stage,
      failure_reference: failure.reference,
      error,
    })
  }
}

export async function continueCloudProvisioning(input: {
  workerId: WorkerId
  orgId?: OrgId
  name: string
  hostToken: string
  clientToken: string
  activityToken: string
}, options: ContinueCloudProvisioningOptions = {}) {
  const existing = cloudProvisioningInFlight.get(input.workerId)
  if (existing) {
    return existing
  }

  // Conditional updates are the multi-replica safety; this in-process map is
  // single-replica efficiency shared by routes, reconcilers, and self-heals.
  const promise = runCloudProvisioning(input, options)
    .finally(() => {
      if (cloudProvisioningInFlight.get(input.workerId) === promise) {
        cloudProvisioningInFlight.delete(input.workerId)
      }
    })
  cloudProvisioningInFlight.set(input.workerId, promise)

  return promise
}

export async function requireCloudAccessOrPayment(input: {
  userId: UserId
  email: string
  name: string
}) {
  return requireCloudWorkerAccess(input)
}

export async function getWorkerTokensAndConnect(worker: WorkerRow, options: {
  getOmniRushWebAccess?: OmniRushWebRuntimeAccessResolver
  resolveCloudAccess?: ResolveCloudRuntimeAccess
  loadActiveTokens?: LoadActiveWorkerTokens
  fetchImpl?: typeof fetch
  includeExpiringOmniRushUrl?: boolean
  apiPublicUrl?: string
} = {}) {
  // Same rollout note as fetchWorkerRuntimeJson: the desktop gate already ran
  // before a published client asks for cloud worker tokens.
  if (worker.destination === "cloud") {
    const webAccess = await (options.getOmniRushWebAccess ?? getOmniRushWebRuntimeAccess)(worker.org_id)
    if (!webAccess.hasAccess) {
      return {
        error: {
          status: 403,
          body: omniRushWebAccessRequiredPayload(),
        },
      }
    }
  }
  if (worker.destination === "cloud" && worker.sandbox_backend === CLOUD_INSTANCE_BACKEND) {
    const tokenRows = await (options.loadActiveTokens ?? loadActiveWorkerTokens)(worker.id)
    const hostToken = tokenRows.find((entry) => entry.scope === "host")?.token ?? null
    const clientToken = tokenRows.find((entry) => entry.scope === "client")?.token ?? null
    if (!hostToken || !clientToken) {
      return {
        error: {
          status: 409,
          body: {
            error: "worker_tokens_unavailable",
            message: "Worker tokens are missing for this worker. Launch a new worker and try again.",
          },
        },
      }
    }

    const stableRootUrl = cloudWorkerCompatibilityUrl(worker.id, options.apiPublicUrl ?? env.apiPublicUrl)
    if (!options.includeExpiringOmniRushUrl) {
      return {
        tokens: { owner: hostToken, host: hostToken, client: clientToken },
        connect: stableRootUrl ? { omnirushUrl: stableRootUrl, workspaceId: null } : null,
      }
    }

    const resolved = await (options.resolveCloudAccess ?? resolveCloudRuntimeAccess)({ organizationId: worker.org_id, workerId: worker.id })
      .catch(() => null)
    const previewConnect = resolved?.status === "ready"
      ? await resolveConnectUrlFromWorker(resolved.url, clientToken, options.fetchImpl)
      : null
    const stableOmniRushUrl = cloudWorkerCompatibilityUrl(
      worker.id,
      options.apiPublicUrl ?? env.apiPublicUrl,
      previewConnect?.workspaceId,
    )
    return {
      tokens: { owner: hostToken, host: hostToken, client: clientToken },
      connect: stableOmniRushUrl
        ? { omnirushUrl: stableOmniRushUrl, workspaceId: previewConnect?.workspaceId ?? null }
        : null,
      directPreview: resolved?.status === "ready" && previewConnect
        ? {
            version: 1 as const,
            omnirushUrl: previewConnect.omnirushUrl,
            workspaceId: previewConnect.workspaceId,
            expiresAt: resolved.expiresAt.toISOString(),
          }
        : null,
    }
  }

  const tokenRows = await db
    .select()
    .from(WorkerTokenTable)
    .where(and(eq(WorkerTokenTable.worker_id, worker.id), isNull(WorkerTokenTable.revoked_at)))
    .orderBy(asc(WorkerTokenTable.created_at))

  const hostToken = tokenRows.find((entry) => entry.scope === "host")?.token ?? null
  const clientToken = tokenRows.find((entry) => entry.scope === "client")?.token ?? null

  if (!hostToken || !clientToken) {
    return {
      error: {
        status: 409,
        body: {
          error: "worker_tokens_unavailable",
          message: "Worker tokens are missing for this worker. Launch a new worker and try again.",
        },
      },
    }
  }

  const instance = await getLatestWorkerInstance(worker.id)
  const connect = await resolveConnectUrlFromCandidates(worker.id, instance?.url ?? null, clientToken)

  return {
    tokens: {
      owner: hostToken,
      host: hostToken,
      client: clientToken,
    },
    connect: connect ?? (instance?.url ? { omnirushUrl: instance.url, workspaceId: null } : null),
  }
}

export async function deleteWorkerCascade(worker: WorkerRow) {
  const instance = await getLatestWorkerInstance(worker.id)

  if (worker.destination === "cloud") {
    try {
      await deprovisionWorker({
        workerId: worker.id,
        instanceUrl: instance?.url ?? null,
      })
    } catch (error) {
      logger.warn("worker deprovision warning", { worker_id: worker.id, error })
    }
  }

  await db.transaction(async (tx) => {
    await tx.delete(WorkerTokenTable).where(eq(WorkerTokenTable.worker_id, worker.id))
    await tx.delete(DaytonaSandboxTable).where(eq(DaytonaSandboxTable.worker_id, worker.id))
    await tx.delete(WorkerInstanceTable).where(eq(WorkerInstanceTable.worker_id, worker.id))
    await tx.delete(WorkerBundleTable).where(eq(WorkerBundleTable.worker_id, worker.id))
    await tx.delete(AuditEventTable).where(eq(AuditEventTable.worker_id, worker.id))
    await tx.delete(WorkerTable).where(eq(WorkerTable.id, worker.id))
  })
}

export async function getWorkerByIdForOrg(workerId: WorkerId, orgId: OrgId) {
  const rows = await db
    .select()
    .from(WorkerTable)
    .where(and(eq(WorkerTable.id, workerId), eq(WorkerTable.org_id, orgId)))
    .limit(1)

  return rows[0] ?? null
}