> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Submit an external compute job

> Declare a typed job, run it on your infrastructure and receive its result.

Bijection does not manage compute. A run is a task to a compute receiver: a
launcher you operate next to your compute. Bijection commits the task with your
writes, keeps its identity and delivers its result to your continuation. Your
launcher and worker own execution, dependencies and scheduling.

## Declare the job

`computeReceiver` from `@bijectionhq/pack-compute` declares the launcher, a
worker pool [slot](https://docs.bijection.com/operate/placement.md) of `worker@1`, and `computeRuns` the
subtree that keeps the runs, places the launcher at `<runs>.receiver` and
serves the worker's callback route. `job(runs, spec)` declares a typed target
over them. Code never names the launcher's address: the installation's
bindings file does.

```ts bijection/app.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, mutation, t } from "@bijectionhq/bijection";
import { computeReceiver, computeRuns, job } from "@bijectionhq/pack-compute";

const runs = computeRuns({ receiver: computeReceiver(), site: "https://app.example.com" });

export const forecast = job(runs, {
  args: { values: t.array(t.number) },
  returns: { total: t.number },
  program: { name: "forecast", revision: "1", kind: "trustedWorker" },
});

export const forecasted = mutation(
  { args: { request: t.string, result: t.result(forecast) } },
  async (ctx, { request, result }) => {
    if (result.ok) {
      // Record result.value.total for request.
    } else {
      // result.code: RunFailed, RunUnknown, Expired, Withdrawn, …
    }
    return null;
  },
);

export const startForecast = mutation(
  { args: { request: t.string, values: t.array(t.number) } },
  async (ctx, { request, values }) => {
    await ctx.submit(forecast, { values }, {
      once: ["forecast", request],
      within: { hours: 1 },
      then: forecasted.with({ request }),
    });
    return null;
  },
);

export default app({ compute: { runs, forecast }, forecasted, startForecast });
```

No host offers `worker@1` today, so bind the pool to your launcher in the installation's bindings file, with `workerPool.aws`, `workerPool.gcp` or
`placement.endpoint`:

```ts bindings.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { placement } from "@bijectionhq/bijection";
import { workerPool } from "@bijectionhq/pack-compute";
import app from "./bijection/app";

export default placement.bindings([
  placement.bind(app.compute.runs.receiver.launcher, workerPool.aws({
    url: "https://launcher.example.com",
    account: "111122223333",
    region: "eu-west-1",
    credential: "compute.launcher.token", // bijection secret set compute.launcher.token
  })),
]);
```

An unbound pool is refused at deploy with `PlacementUnavailable`
(`NotOffered`).

Declare every job before `app(tree)` places the runs subtree, and place each
job beside it (here under `compute`): an open task is answered only by a
function in the subtree that holds the target's path. The worker route serves
`POST /compute/worker` under that subtree by default (`path` changes it).

## Submit it and receive the result

A job is a target performed by its own mutation. It checks the arguments
(arguments that do not fit, or a `within` above `maxDeadline`, one day by
default, refuse `Invalid`), launches the run, and leaves the task open until
the worker answers. Submitting the same `once` again returns the same task and
never launches a second run. The task settles `RunFailed` (the launcher
refused, the worker failed or the result was refused) or `RunUnknown` (the
launch's answer was lost), with the reason in the message.

`Expired` ends your app's acceptance window: the remote process can still be
running. `cancel(ctx, task)` from the runs subtree answers the task
`Withdrawn` and asks the launcher to stop the attempt; `inspect(ctx, task)`
reports progress and the current candidate; `replace(ctx, task, generation)`
fences the old attempt and launches again with the same inputs and task.

A launcher that answers inline within its timeout (up to 15 minutes) can also
be submitted to directly, as `ctx.submit(pool.launcher.run, launch, { once, then })`.

## Match the worker contract

The app and worker agree on a `bijection.interface/v1` function contract.
`contractDigest(forecast)` answers the app's pin; give the worker the same
digest when registering its handler, and the worker refuses a mismatch before
calling it. Change the program revision when the implementation or acceptance
policy changes. See [Implement a Python worker](https://docs.bijection.com/transforms/python.md) and
[launchers](https://docs.bijection.com/transforms/workers.md).

If the worker already has a resolved interface document, scaffold a source
file from it:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection contracts import forecast.interface.json --out bijection/forecast.ts
```

Review its value types and acceptance policy, call its
`createJobs(runs, revision)` when declaring the runs subtree, and commit the
reviewed file with the app.

Pass large inputs and outputs by reference through
[Pipelines](https://docs.bijection.com/transforms/pipelines.md). The Datasets app owns exact versions,
output labels and lineage. A successful worker reply is not a publication.

A job's `program` names a worker handler and its revision; Bijection never
sees its code. A pipeline run target is different: its programs are typed
relations, native SQL or program files (`program(path, { run, runtime, inputs })`)
that `bijection check` digests and deploy installs in the target, which runs
only installed digests.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.