Skip to content

A full-featured download manager.

47.9 KB
Raw Blame
import { createHash } from 'node:crypto'
import { access, mkdir } from 'node:fs/promises'
import path from 'node:path'
import { setTimeout as delay } from 'node:timers/promises'
import { TaskCookieError } from '@core/engine/task-cookie-error'
import { getLogger } from '@core/logger'
import {
  extractAria2ProxyCredentials,
  normalizeAria2TaskProxyUrl,
  stripAria2ProxyCredentials,
} from '@core/proxy/aria2-proxy-routing'
import { admitDownloadSources } from '@core/task/source-admission'
import { AppError, ErrorCode } from '@shared/errors'
import { formatFileSelection } from '@shared/lib/file-selection-ranges'
import type {
  LegacyCheckpointImport,
  LegacyCheckpointReconcile,
} from '@shared/schemas/legacy-checkpoint'
import type {
  EngineCapability,
  EngineFeatureReport,
} from '@shared/types/engine'
import type { EngineTaskOptions } from '@shared/types/engine-task-options'
import type {
  HistoryFilter,
  HistorySearchQuery,
  RequeueResult,
} from '@shared/types/history'
import type { TaskPeer } from '@shared/types/peer'
import type { TaskPiecesResult } from '@shared/types/pieces'
import type { EngineSettings } from '@shared/types/settings'
import type { GlobalStats } from '@shared/types/stats'
import type { DownloadTask, TaskFile } from '@shared/types/task'
import type { TuningContext } from '@shared/types/tuning'
import { probeQuick } from '../../probe/disk-probe'
import type {
  AddTorrentParams,
  BtTrackerPolicy,
  CreateDownloadParams,
  DirectResourceMetadataProfile,
  DownloadCookie,
  EngineAdapter,
  LegacyBtBindingProof,
  LegacyHttpBindingProof,
} from '../engine-adapter'
import { DIRECT_RESOURCE_METADATA_PROFILE } from '../engine-adapter'
import { Aria2LegacyCheckpoint } from './aria2-legacy-checkpoint'
import {
  pinLegacyHttpOptions,
  verifyLegacyHttpBinding,
} from './aria2-legacy-http'
import { legacyWirePath } from './aria2-legacy-path'
import type { Aria2RpcClient } from './aria2-rpc-client'
import { recommend } from './aria2-tuning'
import { isConnectionLimitRangeError, isNotFoundError } from './error-utils'
import { prepareAria2TaskCookies } from './task-cookies'

/**
 * Per-multicall cap for removeDownloadResults. One unbounded batch must
 * complete inside JsonRpcProtocol's fixed request timeout, which a large
 * stopped-history cannot; bounded chunks keep each round-trip small and let
 * cleanup make progress across retries. The history can far exceed aria2's
 * in-memory window (--max-download-result, default 1000) because the fork's
 * sqlite3 persistence keeps every evicted row (--sqlite3-history-limit
 * defaults to unlimited) and merges them into tellStopped.
 */
const REMOVE_RESULT_CHUNK_SIZE = 100
const RESERVED_TASK_PROXY_OPTIONS = new Set([
  'all-proxy',
  'all-proxy-user',
  'all-proxy-passwd',
  'http-proxy',
  'http-proxy-user',
  'http-proxy-passwd',
  'https-proxy',
  'https-proxy-user',
  'https-proxy-passwd',
  'ftp-proxy',
  'ftp-proxy-user',
  'ftp-proxy-passwd',
  'no-proxy',
  'proxy-method',
])

type FileAccess = (filePath: string) => Promise<void>

const AMBIENT_HTTP_TEXT_OPTIONS = [
  'referer',
  'load-cookies',
  'http-user',
  'http-passwd',
] as const

const AMBIENT_HTTP_TRUE_OPTIONS = [
  'conditional-get',
  'dry-run',
  'http-auth-challenge',
  'http-no-cache',
  'use-head',
] as const

/**
 * Convert aria2's effective global options into a non-secret compatibility
 * decision. User values are never returned or logged. A configured netrc is
 * safe only when it is disabled or its effective file does not exist; a
 * present file may change Authorization and must preserve the user's behavior.
 */
export async function directResourceMetadataProfileFromGlobalOptions(
  options: Readonly<Record<string, string>>,
  accessFile: FileAccess = access
): Promise<DirectResourceMetadataProfile | null> {
  if (
    hasEffectiveCumulativeValue(options.header) ||
    AMBIENT_HTTP_TEXT_OPTIONS.some((name) => options[name]?.length > 0) ||
    AMBIENT_HTTP_TRUE_OPTIONS.some((name) => isAria2True(options[name])) ||
    options['http-accept-gzip'] !== 'true' ||
    options['no-want-digest-header'] !== 'false'
  ) {
    return null
  }

  if (!isAria2True(options['no-netrc'])) {
    const netrcPath = options['netrc-path']?.trim()
    if (!netrcPath) return null
    try {
      await accessFile(netrcPath)
      return null
    } catch (error) {
      if (!isMissingPathError(error)) return null
    }
  }

  return DIRECT_RESOURCE_METADATA_PROFILE
}

import {
  buildFeatureReport,
  hasDurableRemoveSemantics,
  isMotrixFork,
  STANDARD_ARIA2_CONNECTION_LIMIT,
} from './feature-report'
import {
  translateGlobalStat,
  translatePeer,
  translateRawFile,
  translateRawToTask,
} from './translate'

export class Aria2Adapter implements EngineAdapter {
  private btTrackerPolicy?: BtTrackerPolicy
  configureBtTrackerPolicy(policy: BtTrackerPolicy): void {
    this.btTrackerPolicy = policy
  }
  async setTaskBtTracker(
    engineTaskId: string,
    trackers: string[]
  ): Promise<void> {
    await this.rpc.changeOption(engineTaskId, {
      'bt-tracker': trackers.join(','),
    })
  }
  private async applyBtTrackerPolicy(
    options: Record<string, string | string[]>,
    gid?: string,
    metadata?: Uint8Array,
    isPrivate?: boolean
  ): Promise<void> {
    if (!this.btTrackerPolicy) return
    const raw = options['bt-tracker']
    const manual = (Array.isArray(raw) ? raw : (raw ?? '').split(',')).filter(
      Boolean
    )
    const policy = await this.btTrackerPolicy({
      engineGid: gid,
      metadata,
      isPrivate,
      manual,
    })
    options['bt-tracker'] = policy.trackers.join(',')
    // Subscription exclusions select supplements; never exclude native trackers.
    options['bt-exclude-tracker'] = ''
    if (policy.isPrivate) options['enable-peer-exchange'] = 'false'
  }

  private readonly log = getLogger('aria2-adapter')
  private capability: EngineCapability = {
    http: true,
    ftp: true,
    bt: false,
    magnet: false,
    metalink: false,
  }
  private featureReport: EngineFeatureReport = {
    version: 'unknown',
    features: [],
    hasSqlitePersistence: false,
    hasBtSeedUnverified: false,
    hasBtSaveMetadata: false,
    hasMoveStorage: false,
  }
  private connectionOptionLimit: number | null = null
  private httpRetryMethods: Promise<boolean> | null = null
  private httpRetryGeneration = 0
  private directResourceMetadataProfile: DirectResourceMetadataProfile | null =
    null

  private btCompleteHandlers: Array<(engineTaskId: string) => void> = []
  private downloadCompleteHandlers: Array<(engineTaskId: string) => void> = []
  private downloadErrorHandlers: Array<(engineTaskId: string) => void> = []
  private readonly rpcUnsubscribers: Array<() => void> = []
  private disposed = false

  constructor(
    private rpc: Aria2RpcClient,
    private readonly accessFile: FileAccess = access,
    private readonly getSeedingDefaults: () =>
      | Pick<EngineSettings, 'seedTime' | 'seedRatio'>
      | undefined = () => undefined
  ) {
    const unsubscribers = [
      this.rpc.onBtDownloadComplete((event) => {
        this.fanOut(this.btCompleteHandlers, event.gid)
      }),
      this.rpc.onDownloadComplete((event) => {
        this.fanOut(this.downloadCompleteHandlers, event.gid)
      }),
      this.rpc.onDownloadError((event) => {
        this.fanOut(this.downloadErrorHandlers, event.gid)
      }),
    ]
    for (const unsubscribe of unsubscribers) {
      if (typeof unsubscribe === 'function') {
        this.rpcUnsubscribers.push(unsubscribe)
      }
    }
  }

  private seedingOptions(
    overrides: Pick<AddTorrentParams, 'seedTime' | 'seedRatio'> = {}
  ): Record<string, string> {
    const defaults = this.getSeedingDefaults()
    const time = overrides.seedTime ?? defaults?.seedTime
    const ratio = overrides.seedRatio ?? defaults?.seedRatio
    // Keep time task-local: aria2 cannot unset an inherited global seed-time
    // through RPC, and both "0" and "" terminate seeding immediately.
    return {
      ...(time !== undefined && time > 0 ? { 'seed-time': String(time) } : {}),
      ...(ratio !== undefined ? { 'seed-ratio': String(ratio) } : {}),
    }
  }

  async inspectDirectResourceMetadataProfile(): Promise<DirectResourceMetadataProfile | null> {
    try {
      return await directResourceMetadataProfileFromGlobalOptions(
        await this.rpc.getGlobalOption(),
        this.accessFile
      )
    } catch (error) {
      // The profile is a safety proof, not a startup dependency. RPC or file
      // inspection failures disable optional probing/replay without exposing
      // the possibly secret global option values in logs.
      this.log.debug(
        { err: error },
        'aria2 HTTP metadata request profile is unavailable'
      )
      return null
    }
  }

  setDirectResourceMetadataProfile(
    profile: DirectResourceMetadataProfile | null
  ): void {
    this.directResourceMetadataProfile = profile
  }

  getDirectResourceMetadataProfile(): DirectResourceMetadataProfile | null {
    return this.directResourceMetadataProfile
  }

  dispose(): void {
    if (this.disposed) return
    this.disposed = true
    this.btCompleteHandlers = []
    this.downloadCompleteHandlers = []
    this.downloadErrorHandlers = []
    let firstError: unknown
    for (const unsubscribe of this.rpcUnsubscribers.splice(0)) {
      try {
        unsubscribe()
      } catch (error) {
        firstError ??= error
      }
    }
    if (firstError !== undefined) {
      throw firstError
    }
  }

  private fanOut(
    handlers: Array<(engineTaskId: string) => void>,
    gid: string
  ): void {
    for (const h of handlers) {
      try {
        h(gid)
      } catch {
        // Isolate handler errors so one throw does not block other
        // subscribers from receiving the event.
      }
    }
  }

  async connect(): Promise<void> {
    this.httpRetryMethods = null
    this.httpRetryGeneration++
    // RPC connection lifecycle is managed by EngineSupervisor.
    // The adapter uses connect() to probe engine capabilities.
    try {
      const v = await this.rpc.getVersion()
      const features = v.enabledFeatures ?? []
      this.featureReport = buildFeatureReport(v.version, features)
      this.capability = {
        http: true,
        ftp: true,
        bt: features.includes('BitTorrent'),
        magnet: features.includes('BitTorrent'),
        metalink: features.includes('Metalink'),
      }
    } catch {
      // If the probe fails (engine not ready, RPC error), keep the
      // conservative defaults. EngineSupervisor will surface the
      // underlying connection error separately.
    }
  }

  async disconnect(): Promise<void> {
    this.httpRetryMethods = null
    this.httpRetryGeneration++
    // Disconnection is managed by EngineSupervisor.
  }

  getCapabilities(): EngineCapability {
    return this.capability
  }

  getFeatureReport(): EngineFeatureReport {
    return this.featureReport
  }

  prepareDownloadCookies(cookies: readonly DownloadCookie[]) {
    return prepareAria2TaskCookies(cookies)
  }

  /**
   * Inject the feature report the EngineSupervisor already probed. Production
   * never calls `connect()`, so without this the adapter would keep its
   * conservative default (version 'unknown', no persistence) and the
   * durable-remove trust gate would silently treat every engine as safe.
   */
  setFeatureReport(report: EngineFeatureReport): void {
    this.httpRetryMethods = null
    this.httpRetryGeneration++
    this.featureReport = report
    this.connectionOptionLimit = isMotrixFork(report)
      ? null
      : STANDARD_ARIA2_CONNECTION_LIMIT
  }

  private capConnectionOptions(
    options: Record<string, string | string[]>,
    limit: number
  ): Record<string, string | string[]> {
    const compatible = { ...options }
    for (const key of ['split', 'max-connection-per-server']) {
      const value = compatible[key]
      if (typeof value !== 'string') continue
      const parsed = Number.parseInt(value, 10)
      if (Number.isFinite(parsed) && parsed > limit) {
        compatible[key] = String(limit)
      }
    }
    return compatible
  }

  private async addUriWithConnectionFallback(
    uris: string[],
    options: Record<string, string | string[]>,
    cookies?: readonly DownloadCookie[]
  ): Promise<string> {
    const submit = (requestOptions: Record<string, string | string[]>) => {
      if (cookies === undefined) return this.rpc.addUri(uris, requestOptions)
      return this.rpc
        .addUriWithCookies(uris, cookies, requestOptions)
        .catch((error: unknown) => {
          if (
            error instanceof Error &&
            /(?:no such method|method not found).*addUriWithCookies/i.test(
              error.message
            )
          ) {
            throw new AppError(
              ErrorCode.EngineFeatureUnavailable,
              'Task cookies require aria2 1.37.0-motrix.13 or a compatible engine. Update the download engine.',
              error
            )
          }
          throw error
        })
    }
    const firstOptions =
      this.connectionOptionLimit === null
        ? options
        : this.capConnectionOptions(options, this.connectionOptionLimit)

    try {
      return await submit(firstOptions)
    } catch (error) {
      const fallbackOptions = this.capConnectionOptions(
        firstOptions,
        STANDARD_ARIA2_CONNECTION_LIMIT
      )
      const changed = Object.keys(fallbackOptions).some(
        (key) => fallbackOptions[key] !== firstOptions[key]
      )
      if (!changed || !isConnectionLimitRangeError(error)) throw error

      this.connectionOptionLimit = STANDARD_ARIA2_CONNECTION_LIMIT
      this.log.warn(
        { err: error },
        'aria2 rejected connection options; retrying with compatibility limit'
      )
      return submit(fallbackOptions)
    }
  }

  async createDownload(params: CreateDownloadParams): Promise<string> {
    if (params.cookies !== undefined) {
      const prepared = prepareAria2TaskCookies(params.cookies)
      // Only admission may choose a reduced credential set. Later corruption or
      // mutation must not silently change a task after its durable intent exists.
      const incompatible = prepared.discarded.find(
        (d) => d.reason !== 'expired' && d.reason !== 'duplicate'
      )
      if (incompatible) throw new TaskCookieError(incompatible)
      params = {
        ...params,
        cookies: prepared.cookies,
      }
    }
    if (
      params.legacyCheckpointActivation &&
      !(await this.supportsLegacyHttpActivation())
    )
      throw new AppError(
        ErrorCode.EngineFeatureUnavailable,
        'legacyImport.errors.httpRequestUnsupported'
      )
    params = {
      ...params,
      uris: admitDownloadSources(params.uris, 'engine').map(
        (source) => source.requestUrl
      ),
    }
    const extraGid = params.extraEngineOptions?.gid
    if (extraGid !== undefined && typeof extraGid !== 'string') {
      throw new TypeError(
        'aria2 addUri gid must contain exactly 16 hexadecimal characters'
      )
    }
    const requestedGid =
      params.gid ?? (typeof extraGid === 'string' ? extraGid : undefined)
    if (requestedGid !== undefined && !/^[0-9a-fA-F]{16}$/.test(requestedGid)) {
      throw new TypeError(
        'aria2 addUri gid must contain exactly 16 hexadecimal characters'
      )
    }
    const metadataProfile = params.directResourceMetadataProfile
    if (
      metadataProfile !== undefined &&
      ((params.cookies?.length ?? 0) > 0 ||
        this.getDirectResourceMetadataProfile() !== metadataProfile)
    ) {
      throw new Error(
        'aria2 HTTP request profile changed before download dispatch'
      )
    }
    const options: Record<string, string | string[]> = {
      ...(params.uris.some((uri) => /^magnet:/i.test(uri))
        ? this.seedingOptions()
        : {}),
      dir: params.saveDir,
    }
    if (params.filename) options.out = params.filename
    if (params.dlLimit) {
      options['max-download-limit'] = String(params.dlLimit)
    }
    if (params.ulLimit) {
      options['max-upload-limit'] = String(params.ulLimit)
    }
    if (params.pause) {
      // Per-call override of the global --pause=false invariant. Used by
      // SessionManager to re-add a paused HTTP task after restart so the
      // engine state matches the persisted Motrix status.
      options.pause = 'true'
    }
    const requestHeaders = Object.entries(params.headers ?? {})
    if (metadataProfile === DIRECT_RESOURCE_METADATA_PROFILE) {
      // An empty Cookie field suppresses aria2's process-wide CookieStorage.
      // Authorization is omitted unless explicitly supplied: the scalar
      // options below disable ambient credentials without sending an empty
      // authentication header, which signed artifact URLs may reject.
      if (!hasRequestHeader(requestHeaders, 'cookie')) {
        requestHeaders.push(['Cookie', ''])
      }
    }
    if (
      params.uris.every((uri) => /^https?:\/\//i.test(uri)) &&
      !hasRequestHeader(requestHeaders, 'accept')
    ) {
      // A Metalink-enabled aria2 expands its built-in Accept value on the
      // first HTTP request. Pin Motrix direct downloads to the exact value
      // used by metadata validation so content negotiation cannot diverge.
      requestHeaders.push(['Accept', '*/*'])
    }
    if (requestHeaders.length > 0) {
      options.header = requestHeaders.map(([key, value]) => `${key}: ${value}`)
    }
    if (params.userAgent !== undefined) {
      if (hasC0OrDel(params.userAgent)) {
        throw new TypeError(
          'Task User-Agent must not contain control characters'
        )
      }
      options['user-agent'] = params.userAgent
    }
    if (params.connections != null) {
      options.split = String(params.connections)
      options['max-connection-per-server'] = String(params.connections)
    }
    if (metadataProfile === DIRECT_RESOURCE_METADATA_PROFILE) {
      // Pin every scalar request semantic represented by the profile before
      // passthrough options. A caller that intentionally supplies an engine
      // option keeps the historical last-write-wins behavior below; such
      // calls are excluded from metadata probing by CreateTaskHandler.
      options.referer = ''
      options['http-no-cache'] = 'false'
      options['conditional-get'] = 'false'
      options['use-head'] = 'false'
      options['no-netrc'] = 'true'
      options['http-user'] = ''
      options['http-passwd'] = ''
      options['http-auth-challenge'] = 'false'
      options['http-accept-gzip'] = 'true'
      options['no-want-digest-header'] = 'false'
    }
    if (params.extraEngineOptions) {
      for (const [k, v] of Object.entries(params.extraEngineOptions)) {
        if (RESERVED_TASK_PROXY_OPTIONS.has(k.toLowerCase())) {
          throw new TypeError(`Reserved aria2 proxy option: ${k}`)
        }
        options[k] = v
      }
    }
    const taskProxy = params.proxy?.trim()
    if (taskProxy) {
      const normalizedProxy = normalizeAria2TaskProxyUrl(taskProxy)
      if (!normalizedProxy) {
        throw new TypeError(
          'Task proxy must use aria2-compatible HTTP or HTTPS syntax'
        )
      }
      const credentials = extractAria2ProxyCredentials(taskProxy)
      const proxyWithoutCredentials = stripAria2ProxyCredentials(taskProxy)
      if (!credentials || !proxyWithoutCredentials) {
        throw new TypeError('Unsupported aria2 proxy credentials')
      }
      // Reapply the complete task proxy after passthrough options. Carry
      // credentials only in aria2's dedicated fields: duplicating them in the
      // URI also makes them more likely to appear in engine state or logs.
      options['all-proxy'] = proxyWithoutCredentials
      options['all-proxy-user'] = credentials.username
      options['all-proxy-passwd'] = credentials.password
    }

    const automaticTuning =
      params.performanceProfile === undefined ||
      params.performanceProfile === 'auto'
    if (params.fileAllocation) {
      options['file-allocation'] = params.fileAllocation
    } else if (
      automaticTuning &&
      (params.totalSizeBytes != null || params.protocol != null)
    ) {
      const probe = probeQuick(params.saveDir)
      const context: TuningContext = {
        downloadPath: params.saveDir,
        totalSizeBytes: params.totalSizeBytes ?? null,
        protocol: params.protocol ?? 'http',
        isMultiFile: params.isMultiFile ?? null,
      }
      const rec = recommend(probe, context)
      options['file-allocation'] = rec.fileAllocation
      if (!options.split) options.split = String(rec.split)
      if (!options['min-split-size']) {
        options['min-split-size'] = String(rec.minSplitSize)
      }
      // disk-cache is an aria2 instance-wide startup option, shared by all
      // downloads. EngineSupervisor applies the automatic recommendation
      // before process launch; it is intentionally not sent to addUri here.
    }
    if (params.split != null && !options.split) {
      options.split = String(params.split)
    }
    if (params.minSplitSize != null && !options['min-split-size']) {
      options['min-split-size'] = String(params.minSplitSize)
    }
    if (requestedGid !== undefined) {
      options.gid = requestedGid
    }

    // Resume controls are product safety invariants, not passthrough options.
    // Apply them last so a replay recipe cannot silently weaken or broaden
    // the recovery policy selected by SessionManager.
    const resumePolicy = params.resumePolicy ?? 'none'
    if (resumePolicy === 'checkpoint') {
      options['always-resume'] = 'true'
    } else if (resumePolicy === 'sequential-prefix') {
      options['always-resume'] = 'true'
    }
    options.continue = resumePolicy === 'sequential-prefix' ? 'true' : 'false'
    await pinLegacyHttpOptions(
      params,
      options,
      new Aria2LegacyCheckpoint(this.rpc)
    )

    // A magnet can spawn a BT payload with HTTP Web Seeds. Their 404s must
    // not halt the entire torrent before a healthy source sends any data.
    if (params.uris.some((uri) => /^magnet:/i.test(uri))) {
      options['max-file-not-found'] = '0'
      await this.applyBtTrackerPolicy(options, requestedGid)
    }

    if (params.retainRequestContext) {
      if (!(await this.supportsDirectTaskRetry()))
        throw new AppError(
          ErrorCode.EngineFeatureUnavailable,
          'Engine request retention is unavailable'
        )
      options['http-resume-context'] = 'true'
    }
    const actualGid = await this.addUriWithConnectionFallback(
      params.uris,
      options,
      params.cookies
    )
    if (
      requestedGid !== undefined &&
      actualGid.toLowerCase() !== requestedGid.toLowerCase()
    ) {
      throw new Error(
        `aria2 returned gid ${actualGid} instead of reserved gid ${requestedGid}`
      )
    }
    return requestedGid ?? actualGid
  }

  async pauseTask(engineTaskId: string): Promise<void> {
    await this.rpc.pause(engineTaskId)
  }

  async resumeTask(engineTaskId: string): Promise<void> {
    await this.rpc.unpause(engineTaskId)
  }

  async removeTask(engineTaskId: string): Promise<void> {
    await this.rpc.remove(engineTaskId)
  }

  async forceRemoveTask(engineTaskId: string): Promise<void> {
    try {
      await this.rpc.forceRemove(engineTaskId)
    } catch (err) {
      // A gid evicted between the caller's decision and this call is already
      // in the desired (gone) state — treat "not found" as success, the same
      // idempotent contract removeDownloadResult provides. Keeps aria2's
      // error-message classification inside the adapter so engine-agnostic
      // callers (stopSeedingTask, reAddTask, finalize) don't import it.
      if (isNotFoundError(err)) return
      throw err
    }
  }

  async getEngineTaskOptions(
    engineTaskId: string
  ): Promise<EngineTaskOptions | null> {
    try {
      // aria2 returns options as a struct of strings, but `header`
      // may be an array. Cast through unknown — runtime shape is
      // wider than the rpc client's Record<string, string> hint.
      const raw = (await this.rpc.getOption(
        engineTaskId
      )) as unknown as EngineTaskOptions
      return raw
    } catch (err) {
      if (isNotFoundError(err)) return null
      throw err
    }
  }

  async pauseAll(): Promise<void> {
    await this.rpc.pauseAll()
  }

  async resumeAll(): Promise<void> {
    await this.rpc.unpauseAll()
  }

  async changePosition(
    engineTaskId: string,
    pos: number,
    how: 'POS_SET' | 'POS_CUR' | 'POS_END'
  ): Promise<number> {
    return this.rpc.changePosition(engineTaskId, pos, how)
  }

  async getTaskStatus(engineTaskId: string): Promise<DownloadTask | null> {
    try {
      const raw = await this.rpc.tellStatus(engineTaskId)
      return translateRawToTask(raw)
    } catch (error) {
      if (isNotFoundError(error)) return null
      throw error
    }
  }

  async getTaskFiles(engineTaskId: string): Promise<TaskFile[]> {
    const rawFiles = await this.rpc.getFiles(engineTaskId)
    return rawFiles.map(translateRawFile)
  }

  async changeOption(
    engineTaskId: string,
    options: Record<string, string>
  ): Promise<void> {
    await this.rpc.changeOption(engineTaskId, options)
  }

  async getTaskPeers(engineTaskId: string): Promise<TaskPeer[]> {
    try {
      const raw = await this.rpc.getPeers(engineTaskId)
      return raw.map(translatePeer)
    } catch {
      // Non-BT tasks and unknown gids both raise on the aria2 side;
      // surface as an empty list so the renderer can render its
      // "no peers" placeholder without branching on adapter errors.
      return []
    }
  }

  async getTaskPieces(engineTaskId: string): Promise<TaskPiecesResult | null> {
    try {
      const raw = (await this.rpc.tellStatus(engineTaskId, [
        'pieceLength',
        'numPieces',
        'bitfield',
      ])) as {
        pieceLength?: string
        numPieces?: string
        bitfield?: string
      }
      return {
        pieceLength: Number.parseInt(raw.pieceLength ?? '0', 10) || 0,
        numPieces: Number.parseInt(raw.numPieces ?? '0', 10) || 0,
        bitfield: typeof raw.bitfield === 'string' ? raw.bitfield : '',
      }
    } catch {
      return null
    }
  }

  async getTaskBtTracker(engineTaskId: string): Promise<string[]> {
    let opts: Record<string, string>
    try {
      opts = await this.rpc.getOption(engineTaskId)
    } catch (err) {
      // BT tasks evicted from aria2 post-seeding (see
      // shouldEvictFromEngine in main/index.ts) no longer have a row,
      // so getOption raises "GID xxx is not found". The TrackersTab
      // should still render the .torrent-native announceList from
      // motrix.db without an error banner. Other failures (RPC down,
      // protocol errors) propagate untouched.
      const msg = err instanceof Error ? err.message : String(err)
      if (/is not found/i.test(msg)) return []
      throw err
    }
    const raw = opts['bt-tracker']
    if (!raw) return []
    return raw
      .split(',')
      .map((s) => s.trim())
      .filter((s) => s.length > 0)
  }

  async getGlobalStats(): Promise<GlobalStats> {
    const raw = await this.rpc.getGlobalStat()
    return translateGlobalStat(raw)
  }

  async supportsLegacyBtActivation(): Promise<boolean> {
    const report = await this.rpc.getVersion()
    return (
      report.enabledFeatures.includes('LegacyTorrentMetadataV1') &&
      (await this.supportsLegacyCheckpointImport())
    )
  }

  async verifyLegacyBtBinding(input: LegacyBtBindingProof): Promise<boolean> {
    const task = await this.getTaskStatus(input.engineTaskId)
    if (
      !task ||
      task.engineTaskId !== input.engineTaskId ||
      task.infoHash?.toLowerCase() !== input.infoHash
    )
      return false
    const [files, options, trackers] = await Promise.all([
      this.getTaskFiles(input.engineTaskId),
      this.getEngineTaskOptions(input.engineTaskId),
      this.getTaskBtTracker(input.engineTaskId),
    ])
    const selected = new Set(input.selectedFiles)
    return (
      files.length === input.files.length &&
      files.every(
        (file, index) =>
          file.index === index &&
          file.path === legacyWirePath(input.files[index].path) &&
          file.size === input.files[index].length &&
          file.selected === selected.has(index)
      ) &&
      options !== null &&
      options.dir === legacyWirePath(input.saveDir) &&
      options['bt-seed-unverified'] === 'false' &&
      options['bt-hash-check-seed'] === 'false' &&
      Number(options['seed-time']) === 0 &&
      Number(options['seed-ratio']) === 0 &&
      options['bt-remove-unselected-file'] === 'false' &&
      options['allow-overwrite'] === 'false' &&
      options['auto-file-renaming'] === 'false' &&
      options['remove-control-file'] === 'false' &&
      options['check-integrity'] === 'true' &&
      trackers.join(',') === input.trackers.join(',')
    )
  }

  async verifyLegacyHttpBinding(
    input: LegacyHttpBindingProof
  ): Promise<boolean> {
    if (!(await this.supportsLegacyHttpActivation())) return false
    return verifyLegacyHttpBinding(this.rpc, input)
  }

  async supportsLegacyHttpActivation(): Promise<boolean> {
    if (!(await this.supportsLegacyCheckpointImport())) return false
    if (
      !(await this.rpc.getVersion()).enabledFeatures.includes(
        'HttpRedirectPolicyV1'
      )
    )
      return false
    const options = await this.rpc.getGlobalOption()
    const header = options.header as unknown
    // aria2 accumulates global and per-task header options. An empty Cookie
    // suppresses CookieStorage, but cannot remove inherited header lines.
    return (
      header === undefined ||
      header === '' ||
      (Array.isArray(header) &&
        header.every((line) => typeof line === 'string' && !line.trim()))
    )
  }

  async addTorrent(params: AddTorrentParams): Promise<string> {
    const legacyMetadata = params.legacyCheckpointActivation
      ? Buffer.from(params.metadata)
      : null
    const extraGid = params.extraEngineOptions?.gid
    if (extraGid !== undefined && typeof extraGid !== 'string') {
      throw new TypeError(
        'aria2 addTorrent gid must contain exactly 16 hexadecimal characters'
      )
    }
    const requestedGid =
      params.gid ?? (typeof extraGid === 'string' ? extraGid : undefined)
    const opts: Record<string, string | string[]> = {
      dir: params.saveDir,
      pause: String(params.pause ?? false),
    }
    if (requestedGid !== undefined) {
      if (!/^[0-9a-fA-F]{16}$/.test(requestedGid)) {
        throw new TypeError(
          'aria2 addTorrent gid must contain exactly 16 hexadecimal characters'
        )
      }
    }
    if (params.selectedFiles?.length) {
      opts['select-file'] = formatFileSelection(params.selectedFiles, 1)
    }
    if (params.outputRoot !== undefined && !path.isAbsolute(params.outputRoot))
      throw new TypeError('Torrent output root must be absolute')
    if (params.outputFilePaths?.length) {
      const seen = new Set<number>()
      opts['index-out'] = params.outputFilePaths.map(
        ({ fileIndex, relativePath }) => {
          const components = relativePath.replace(/\\/g, '/').split('/')
          if (
            !Number.isSafeInteger(fileIndex) ||
            fileIndex < 0 ||
            seen.has(fileIndex) ||
            relativePath.length === 0 ||
            relativePath.includes('\0') ||
            path.isAbsolute(relativePath) ||
            /^[A-Za-z]:[\\/]/.test(relativePath) ||
            components.some(
              (component) =>
                component.length === 0 ||
                component === '.' ||
                component === '..'
            )
          ) {
            throw new TypeError('Invalid torrent output file mapping')
          }
          seen.add(fileIndex)
          const outputPath = params.outputRoot
            ? path.join(params.outputRoot, relativePath)
            : relativePath
          return `${fileIndex + 1}=${outputPath}`
        }
      )
    }
    Object.assign(opts, this.seedingOptions(params))
    if (params.btSeedUnverified) {
      opts['bt-seed-unverified'] = 'true'
    }
    if (params.checkIntegrity) {
      opts['check-integrity'] = 'true'
    }
    if (params.prioritizePreviewPieces) {
      opts['bt-prioritize-piece'] = 'head=10M,tail=10M'
    }
    if (params.dlLimit !== undefined) {
      opts['max-download-limit'] = String(params.dlLimit)
    }
    if (params.ulLimit !== undefined) {
      opts['max-upload-limit'] = String(params.ulLimit)
    }
    if (params.extraEngineOptions) {
      for (const [k, v] of Object.entries(params.extraEngineOptions)) {
        if (RESERVED_TASK_PROXY_OPTIONS.has(k.toLowerCase())) {
          throw new TypeError(`Reserved aria2 proxy option: ${k}`)
        }
        opts[k] = v
      }
    }
    if (params.preserveExistingFiles)
      Object.assign(opts, {
        'allow-overwrite': 'false',
        'auto-file-renaming': 'false',
        'check-integrity': 'true',
        'bt-seed-unverified': 'false',
        'bt-remove-unselected-file': 'false',
      })
    if (params.legacyCheckpointActivation) {
      if (
        !params.pause ||
        !requestedGid ||
        !params.selectedFiles?.length ||
        params.outputRoot ||
        params.outputFilePaths
      )
        throw new TypeError('Invalid legacy torrent activation')
      const allowed = new Set(['bt-tracker'])
      if (
        Object.keys(params.extraEngineOptions ?? {}).some(
          (key) => !allowed.has(key)
        )
      )
        throw new TypeError('Unexpected legacy torrent options')
      // Pin after extras so neither ambient settings nor global tracker policy can override consent.
      Object.assign(opts, {
        dir: params.saveDir,
        pause: 'true',
        'select-file': formatFileSelection(params.selectedFiles, 1),
        'allow-overwrite': 'false',
        'auto-file-renaming': 'false',
        'remove-control-file': 'false',
        'check-integrity': 'true',
        'bt-seed-unverified': 'false',
        'bt-hash-check-seed': 'false',
        'seed-time': '0',
        'seed-ratio': '0.0',
        'bt-remove-unselected-file': 'false',
        'file-allocation': 'none',
        'rpc-save-upload-metadata': 'false',
        'bt-save-metadata': 'false',
        'bt-exclude-tracker': '',
        'enable-peer-exchange': 'false',
        'bt-enable-lpd': 'false',
      })
    } else
      await this.applyBtTrackerPolicy(
        opts,
        requestedGid,
        params.metadata,
        params.isPrivate
      )
    if (params.isPrivate && !params.legacyCheckpointActivation) {
      // Apply after extra options: private torrents keep their own trackers.
      // A configured policy can restore explicitly owned private-task edits.
      // Without that provenance, no supplemental option is trusted.
      if (!this.btTrackerPolicy) opts['bt-tracker'] = ''
      opts['bt-exclude-tracker'] = ''
      opts['enable-peer-exchange'] = 'false'
    }
    if (requestedGid !== undefined) {
      opts.gid = requestedGid
    }
    // aria2 applies this HTTP/FTP threshold to the whole BT RequestGroup.
    // Override both user configuration and stale task options so a failed
    // Web Seed cannot abort peers or other Web Seeds before their first byte.
    opts['max-file-not-found'] = '0'
    if (params.outputRoot) await mkdir(params.saveDir, { recursive: true })
    const b64 = (legacyMetadata ?? Buffer.from(params.metadata)).toString(
      'base64'
    )
    let actualGid: string
    if (params.legacyCheckpointActivation) {
      const context = params.legacyCheckpointActivation
      const reference = params.durableMetadata
      if (
        !reference ||
        !legacyMetadata ||
        !/^[a-f0-9]{64}$/.test(reference.digest) ||
        createHash('sha256').update(legacyMetadata).digest('hex') !==
          reference.digest ||
        !/^[A-Za-z0-9_-]{16,128}$/.test(context.token) ||
        ![reference.path, context.targetPath, params.saveDir].every(
          (value) =>
            path.isAbsolute(value) &&
            path.normalize(value) === value &&
            Buffer.byteLength(value) <= 4096 &&
            [...value].every(
              (char) => char.charCodeAt(0) > 31 && char.charCodeAt(0) !== 127
            )
        )
      )
        throw new AppError(
          ErrorCode.InvalidSelection,
          'Invalid durable torrent reference'
        )
      if (!(await this.supportsLegacyBtActivation()))
        throw new AppError(
          ErrorCode.EngineFeatureUnavailable,
          'legacyImport.errors.checkpointUnavailable'
        )
      opts.dir = legacyWirePath(params.saveDir)
      const input = {
        token: context.token,
        targetPath: legacyWirePath(context.targetPath),
        metadataFile: legacyWirePath(reference.path),
        metadataDigest: reference.digest,
        metadata: b64,
        options: opts,
      }
      if (Buffer.byteLength(JSON.stringify(input)) > 2 * 1024 * 1024 - 4096)
        throw new AppError(
          ErrorCode.InvalidSelection,
          'legacyImport.errors.tooLarge'
        )
      actualGid = await this.rpc.addLegacyTorrentV1(input)
    } else {
      if (params.durableMetadata)
        throw new TypeError(
          'Durable torrent reference requires legacy activation'
        )
      actualGid = await this.rpc.addTorrent(b64, [], opts)
    }
    if (
      requestedGid !== undefined &&
      actualGid.toLowerCase() !== requestedGid.toLowerCase()
    ) {
      throw new Error(
        `aria2 returned gid ${actualGid} instead of reserved gid ${requestedGid}`
      )
    }
    return params.gid ?? actualGid
  }

  /**
   * Whether a "GID is not found" reply from this engine can be trusted as
   * durably absent. On a sqlite3-persistence engine that predates
   * 1.37.0-motrix.3, that wording also covers evicted-but-persisted gids and
   * FAILED persistent deletes — so treating it as removed would let callers
   * erase local records while the durable engine row survives. Without
   * persistence there is no durable row a not-found could be hiding, so it is
   * always safe. Shared by the single and batch remove paths.
   */
  private trustsNotFound(): boolean {
    return (
      !this.featureReport.hasSqlitePersistence ||
      hasDurableRemoveSemantics(this.featureReport.version)
    )
  }

  supportsDirectTaskRetry(): Promise<boolean> {
    if (
      !this.featureReport.hasSqlitePersistence ||
      typeof this.rpc.listMethods !== 'function'
    )
      return Promise.resolve(false)
    const generation = this.httpRetryGeneration
    this.httpRetryMethods ??= this.rpc
      .listMethods()
      .then((methods) => {
        if (generation !== this.httpRetryGeneration)
          throw new Error(
            'Engine changed during HTTP retry capability discovery'
          )
        return (
          methods.includes('aria2.getHttpRetryStatusV1') &&
          methods.includes('aria2.retryHttpDownloadV1')
        )
      })
      .catch((error) => {
        if (generation === this.httpRetryGeneration)
          this.httpRetryMethods = null
        throw error
      })
    return this.httpRetryMethods
  }

  async canRetryDirectTask(input: {
    engineTaskId: string
    uri: string
    outputPath: string
  }): Promise<boolean> {
    if (!(await this.supportsDirectTaskRetry())) return false
    const result = await this.rpc.getHttpRetryStatusV1(
      input.engineTaskId,
      input.uri,
      input.outputPath
    )
    return result.available === 'true'
  }

  async retryDirectTask(input: {
    engineTaskId: string
    uri: string
    outputPath: string
  }): Promise<void> {
    if (!(await this.supportsDirectTaskRetry()))
      throw new AppError(
        ErrorCode.EngineFeatureUnavailable,
        'HTTP continuation is unavailable'
      )
    const gid = await this.rpc.retryHttpDownloadV1(
      input.engineTaskId,
      input.uri,
      input.outputPath
    )
    if (gid !== input.engineTaskId)
      throw new Error('HTTP continuation changed the engine task identity')
  }

  async getCheckpointStatus(
    outputPath: string
  ): Promise<'present' | 'absent' | null> {
    try {
      const status = await this.rpc.getCheckpointStatus(outputPath)
      return status.exists === 'true' ? 'present' : 'absent'
    } catch (error) {
      // Official aria2 and fork builds before 1.37.0-motrix.15 lack the
      // method. The caller's control-file probe then matches what they can
      // resume: official aria2 only writes control files, and older fork
      // sqlite checkpoints are keyed by gid, which a retry never matches.
      if (
        error instanceof Error &&
        /(?:no such method|method not found).*getCheckpointStatus/i.test(
          error.message
        )
      ) {
        return null
      }
      throw error
    }
  }

  supportsLegacyCheckpointImport(): Promise<boolean> {
    return new Aria2LegacyCheckpoint(this.rpc).supported()
  }

  inspectLegacyCheckpoint(bytes: Uint8Array) {
    return new Aria2LegacyCheckpoint(this.rpc).inspect(bytes)
  }

  importLegacyCheckpoint(input: LegacyCheckpointImport) {
    return new Aria2LegacyCheckpoint(this.rpc).import(input)
  }

  reconcileLegacyCheckpoint(input: LegacyCheckpointReconcile) {
    return new Aria2LegacyCheckpoint(this.rpc).reconcile(input)
  }

  async removeDownloadResult(engineTaskId: string): Promise<void> {
    // forceRemove acknowledges a halt request before aria2 creates its stopped
    // result. Retry only that precise transition race; successful cleanups add
    // no delay, and transport/persistent-delete failures remain observable.
    for (let attempt = 0; ; attempt++) {
      try {
        await this.rpc.removeDownloadResult(engineTaskId)
        return
      } catch (err) {
        if (isNotFoundError(err) && this.trustsNotFound()) return
        const message = err instanceof Error ? err.message : String(err)
        const pendingStop =
          message === `Could not remove download result of GID#${engineTaskId}`
        if (!pendingStop || attempt >= 6) throw err
        await delay(Math.min(100 * 2 ** attempt, 1000))
      }
    }
  }

  async removeDownloadResults(
    engineTaskIds: readonly string[]
  ): Promise<PromiseSettledResult<void>[]> {
    const settled: PromiseSettledResult<void>[] = []
    // Same trust rule as the single form: an untrusted not-found (pre-.3
    // persistent fork) stays rejected so clearStoppedTasks retains and retries
    // the candidate instead of erasing a record whose durable engine row
    // survives and would resurrect as an orphan on the next restart.
    const trustNotFound = this.trustsNotFound()
    for (
      let start = 0;
      start < engineTaskIds.length;
      start += REMOVE_RESULT_CHUNK_SIZE
    ) {
      const chunk = engineTaskIds.slice(start, start + REMOVE_RESULT_CHUNK_SIZE)
      let chunkSettled: PromiseSettledResult<unknown>[]
      try {
        chunkSettled = await this.rpc.multicallSettled(
          chunk.map((gid) => ({
            method: 'aria2.removeDownloadResult',
            params: [gid],
          }))
        )
      } catch (err) {
        // Chunk-level transport failure: report this chunk AND everything
        // after it rejected instead of stacking further protocol timeouts
        // against an unresponsive engine. Already-confirmed chunks survive
        // so a retry only re-attempts what actually failed.
        const reason = err instanceof Error ? err : new Error(String(err))
        // One shared entry object for the whole failed suffix — a large
        // history would otherwise allocate a wrapper per remaining gid.
        const rejected: PromiseSettledResult<void> = {
          status: 'rejected',
          reason,
        }
        for (let i = start; i < engineTaskIds.length; i += 1) {
          settled.push(rejected)
        }
        return settled
      }
      if (chunkSettled.length !== chunk.length) {
        // A truncated response makes per-entry attribution unsafe — reject
        // the whole chunk rather than mis-mapping outcomes onto gids.
        const rejected: PromiseSettledResult<void> = {
          status: 'rejected',
          reason: new Error(
            `multicall returned ${chunkSettled.length} entries for ${chunk.length} calls`
          ),
        }
        for (const _gid of chunk) {
          settled.push(rejected)
        }
        continue
      }
      for (const entry of chunkSettled) {
        // Same not-found exemption as the single form, applied per entry —
        // but only when this engine's not-found means durably absent.
        settled.push(
          entry.status === 'rejected' &&
            !(trustNotFound && isNotFoundError(entry.reason))
            ? entry
            : { status: 'fulfilled', value: undefined }
        )
      }
    }
    return settled
  }

  async getUploadLength(engineTaskId: string): Promise<number> {
    try {
      const status = await this.rpc.tellStatus(engineTaskId, ['uploadLength'])
      return Number.parseInt(status.uploadLength ?? '0', 10)
    } catch {
      return 0
    }
  }

  async listWaitingTaskIds(): Promise<string[]> {
    const ids: string[] = []
    const pageSize = 1000
    for (let offset = 0; ; offset += pageSize) {
      const page = await this.rpc.tellWaiting(offset, pageSize, ['gid'])
      ids.push(...page.map((task) => task.gid))
      if (page.length < pageSize) return [...new Set(ids)]
    }
  }

  async listActiveAndWaiting(): Promise<
    Array<{ gid: string; infoHash?: string }>
  > {
    const [active, waiting] = await Promise.all([
      this.rpc.tellActive(['gid', 'infoHash']),
      this.rpc.tellWaiting(0, 1000, ['gid', 'infoHash']),
    ])
    return [...active, ...waiting].map((t) => ({
      gid: t.gid,
      infoHash: t.infoHash || undefined,
    }))
  }

  async listStopped(): Promise<Array<{ gid: string; infoHash?: string }>> {
    const stopped = await this.rpc.tellStopped(0, 1000, ['gid', 'infoHash'])
    return stopped.map((t) => ({
      gid: t.gid,
      infoHash: t.infoHash || undefined,
    }))
  }

  async getHistoryCount(filter?: HistoryFilter): Promise<number> {
    const result = await this.rpc.getDownloadResultCount(filter)
    const n = Number.parseInt(result.count, 10)
    if (!Number.isFinite(n)) {
      throw new AppError(
        ErrorCode.EngineProtocolError,
        `Engine returned non-numeric history count: ${result.count}`
      )
    }
    return n
  }

  async searchHistory(
    query: HistorySearchQuery,
    offset: number,
    num: number
  ): Promise<DownloadTask[]> {
    const rows = await this.rpc.searchDownloadResult(query, offset, num)
    return rows.map(translateRawToTask)
  }

  async exportSession(filePath: string): Promise<void> {
    await this.rpc.exportSession(filePath)
  }

  async requeueFromHistory(
    engineTaskId: string,
    overrides?: Record<string, string>
  ): Promise<RequeueResult> {
    const result = await this.rpc.requeueDownloadResult(engineTaskId, overrides)
    return {
      newEngineTaskId: result.gid,
      strategy: result.strategy,
    }
  }

  onBtDownloadComplete(handler: (engineTaskId: string) => void): () => void {
    this.btCompleteHandlers.push(handler)
    return () => {
      this.btCompleteHandlers = this.btCompleteHandlers.filter(
        (h) => h !== handler
      )
    }
  }

  onDownloadComplete(handler: (engineTaskId: string) => void): () => void {
    this.downloadCompleteHandlers.push(handler)
    return () => {
      this.downloadCompleteHandlers = this.downloadCompleteHandlers.filter(
        (h) => h !== handler
      )
    }
  }

  onDownloadError(handler: (engineTaskId: string) => void): () => void {
    this.downloadErrorHandlers.push(handler)
    return () => {
      this.downloadErrorHandlers = this.downloadErrorHandlers.filter(
        (h) => h !== handler
      )
    }
  }
}

function hasC0OrDel(value: string): boolean {
  for (const character of value) {
    const codePoint = character.codePointAt(0) ?? 0
    if (codePoint <= 0x1f || codePoint === 0x7f) return true
  }
  return false
}

function hasRequestHeader(
  headers: ReadonlyArray<readonly [string, string]>,
  expectedName: string
): boolean {
  return headers.some(([name]) => name.toLowerCase() === expectedName)
}

function hasEffectiveCumulativeValue(value: string | undefined): boolean {
  return Boolean(value?.split('\n').some((line) => line.trim().length > 0))
}

function isAria2True(value: string | undefined): boolean {
  return value?.toLowerCase() === 'true'
}

function isMissingPathError(error: unknown): boolean {
  return (
    typeof error === 'object' &&
    error !== null &&
    'code' in error &&
    (error.code === 'ENOENT' || error.code === 'ENOTDIR')
  )
}