# @render-lab/tasks-core

> ⚠️ **Experimental: proof of concept.** This package is part of the [Render Tasks](https://github.com/render-lab/render-tasks) POC and is published for testing only. It is not fully tested or production ready. Task names, inputs, outputs, and behavior can change or break in any release. Pin exact versions and expect breaking changes.

Shared **mechanism** for the Render task packages. This is a plain utility
library — it defines **no tasks** and never calls `task()`, so it carries no
`TaskRegistry` risk ([ADR-0001](../../docs/adr/0001-sdk-peer-dependency-shared-registry.md)).
Task packages depend on it as a regular dependency and layer their vendor ports,
DTO mapping, and retry policy on top. See
[ADR-0014](../../docs/adr/0014-shared-task-core-mechanism-library.md) for the
mechanism-vs-policy boundary and what deliberately stays per-package.

## What's in here

| Export | Purpose |
| --- | --- |
| `createHttpClient({ baseUrl, auth, label, fetchImpl })` | A `fetch`-backed REST client: URL building, lazy auth headers, `content-type`, JSON body serialization, and a `!res.ok` throw. Per-call `rawBody` (multipart/form) and `responseType: "text"` cover non-JSON endpoints. The default for raw-fetch vendor packs. |
| `throwIfNotOk(res, label)` | The `res.text().catch(() => "")` + `.trim()` non-2xx throw, standalone. |
| `FetchLike`, `CallInit`, `HttpClient`, `HttpClientOptions` | The canonical fetch/client types (replaces the copies each package used to declare). |
| `isRetryable(status)` | `429 || status >= 500`. For the escape-hatch packs (`tasks-http`, `tasks-scrape`) that return 4xx but must still throw on transient failures. |
| `pollUntil(fn, message)` | Presence poll — return a truthy value, else throw to re-poll. For the "await until X appears" tasks. |
| `pollGate({ state, onDone, waitMessage, failMessage })` | Tri-state gate — `wait` re-polls, `fail` alerts a human, `done` runs the side effect. For merge-gate tasks. |
| `awaitRetry({ everyMs, forMs })` | Builds a fixed-interval `Retry` from a poll cadence. |

## What is deliberately NOT here

- **Per-vendor retry constants** (`GITHUB_RETRY`, `STRIPE_RETRY`, …) — the tuning
  is the value and stays each package's single source of truth
  ([ADR-0005](../../docs/adr/0005-baked-in-retry-vendor-retries-off.md)).
- **DTO mappers** (`toIssue`, `simplifyPage`, …) — the shape is a convention, the
  bodies are vendor-specific; they stay per-package.
- **SDK-backed clients** (github/Octokit, linear, stripe, notion, hubspot) don't
  use `createHttpClient`; they consume only `pollUntil`/`pollGate`/`awaitRetry`.

## Env contract

None. This package reads no credentials and makes no network calls of its own —
the `auth` closure and `fetchImpl` are supplied by the consuming package.

## Usage

```ts
import { createHttpClient, pollGate } from "@render-lab/tasks-core";

const api = createHttpClient({
  baseUrl: () => `${process.env.GITLAB_URL ?? "https://gitlab.com"}/api/v4`,
  auth: () => ({ "PRIVATE-TOKEN": requireToken() }), // read lazily, ADR-0007
  label: "GitLab API",
  fetchImpl,
});
const mr = await api.call(`/projects/${enc(project)}/merge_requests/${iid}`);
```

## Google access tokens

`googleAccessToken()` returns a token getter for the Google packs (gmail, gcal,
gdrive). `GOOGLE_ACCESS_TOKEN` still takes precedence, so an existing deployment
behaves exactly as before. Otherwise a token is minted from
`GOOGLE_CLIENT_ID`/`GOOGLE_CLIENT_SECRET`/`GOOGLE_REFRESH_TOKEN` and cached until
shortly before it expires.

This exists because a pre-issued token lasts about an hour, which does not
survive a workflow that runs on a schedule: the token is stale long before the
next run. Concurrent misses collapse onto a single exchange, and a failed
exchange is surfaced rather than cached so the durable retry decides.

`createHttpClient`'s `auth` builder may now return a promise, which is what lets
a port mint a token per call. A synchronous builder still satisfies the type.
