> ## 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.

# Retries and read-your-writes

> Give a mutation one identity with once, recover from Unknown, and read your own writes with seen

A mutation is one transaction. Two options make it safe to call over an
unreliable network: `once` names the logical mutation, and `seen` asks a
later read to include what it committed.

## One identity: `once`

`client.mutate(fn, args, { once })` names one logical mutation. The host
keeps the result of each `once`:

* the same `once` with the same arguments answers the original result, and
  runs nothing again;
* the same `once` with other arguments refuses `Invalid`.

When you give no `once`, the client mints one for that call. Give your own
when the identity belongs to something the user did, such as a form
submission you may send again after a reload.

For a signed-out caller, use the same JavaScript client for retries while
its registration can recover the request. For signed-in callers, the
installation keeps the business decision across transport registrations.
After a reload, retry as the same signed-in identity in the original world
and calling scope.

```ts apps/main/src/client.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { api, connect } from "@bijectionhq/bijection/client";
import type App from "../../../bijection/app";

export const app = api<typeof App>();
export const client = connect("https://example.bijection.cloud", { contract: "discover" });
```

```ts apps/main/src/submit.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, client } from "./client";

export async function addOnce(text: string, submission: string) {
  return await client.mutate(app.notes.add, { text }, { once: submission });
}
```

## When the answer is `Unknown`

`Unknown` means the mutation may have committed. The refusal carries its
original `once`, including when the client minted it. Send that key with the
same arguments. The server returns the original result if it committed or
executes the mutation for the first time.

```ts apps/main/src/retry.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, client } from "./client";

export async function addSurely(text: string) {
  let once: string | readonly (string | number)[] | undefined;
  for (let attempt = 0; ; attempt++) {
    const added = await client.mutate(app.notes.add, { text }, once === undefined ? {} : { once });
    if (added.ok || added.code !== "Unknown" || added.once === undefined || attempt === 4) return added;
    once = added.once;
  }
}
```

With `useMutation`, pass the refusal's `once` to the hook's callable as
`add(args, { once })` to retry. See [React](https://docs.bijection.com/clients/react#change-data).

`Failed` is different: it definitely did not happen. Retrying a `Failed`
call runs the mutation for the first time.

## Retain a submission before sending

For a signed-in client with a pinned calling contract, use
`prepareMutation` when you need recovery across a reload or process exit.
It registers the request and returns private recovery material before the
mutation is sent. Save `value.retained` in your application's private
storage and keep `value.reference` for support. Prepared mutations carry
inline arguments. Use the regular mutation path for staged File uploads.

```ts apps/main/src/recover-draft.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import type { Client, RetainedMutation } from "@bijectionhq/bijection/client";
import { app } from "./client";

export async function saveAndResume(
  client: Client,
  text: string,
  once: string,
  saveDraft: (once: string, retained: RetainedMutation) => Promise<void>,
) {
  const prepared = await client.prepareMutation(app.notes.add, { text }, { once });
  if (!prepared.ok) return prepared;
  await saveDraft(once, prepared.value.retained);
  return client.recoverMutation(app.notes.add, prepared.value.retained, { mode: "resume" });
}
```

After a restart, recover with the original function, calling contract,
world and label under the caller's current credentials. Use
`{ mode: "inspect" }` to read the original decision only. A `resume`
can send the prepared request in its original slot when that slot is
unresolved and still admits the mutation. Clear saved recovery material
after a usable decision or recovery expiry. If the result stays
unavailable, look up the business record through the app.

For a retained `bijection call`, `--inspect <path>` reads its decision
and `--recover <path>` can resume the same request. The call prints its
release-independent submission reference before it sends.

## Read your own writes: `version` and `seen`

A committed mutation's result carries its commit `version`. Pass it as
`seen` to a later call, watch or hook, and the answer comes from state at
least that new.

```ts apps/main/src/add-and-list.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, client } from "./client";

export async function addAndList(text: string) {
  const added = await client.mutate(app.notes.add, { text });
  if (!added.ok) return added;
  return await client.query(app.notes.recentNotes, {}, { seen: added.version });
}
```

`seen` is accepted by `client.query`, `client.watch`, `client.trial`,
`client.mutate`, `useWatch`, `usePages` and `useMutation`. A read without it
may answer from a slightly older state than a mutation the same client just
committed over another connection.

## Wait for application work

Some mutations start work that continues after the transaction commits.
When the author declares a `completion` query, the mutation's result
value is a portable receipt.

```ts apps/main/src/refresh-counts.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { api, type Client, type Receipt } from "@bijectionhq/bijection/client";
import type { Pipelines } from "@bijectionhq/library-pipelines";

const app = api<{ readonly etl: Pipelines }>();

export async function refreshCounts(
  client: Client,
  requestId: string,
  saveReceipt: (requestId: string, receipt: Receipt) => Promise<void>,
  signal: AbortSignal,
) {
  const started = await client.mutate(
    app.etl.refresh,
    { pipeline: "counts", rebuild: true },
    { once: requestId },
  );
  if (!started.ok) return started;
  await saveReceipt(requestId, started.value);
  return client.wait(started.value, { signal });
}
```

`wait` watches the declared query on the installation and world recorded
in the receipt and reads at or after the launch commit. It finishes when
the query reports `completed` or `refused`. A later reader uses their own
authorization and the same launch floor. An abort signal ends the wait, so
another process can resume it later. The same receipt works with Go's
`Wait`, `wait` in the Java, Kotlin, Python, Swift, and TypeScript SDKs,
and `bijection wait <receipt>`.

If the mutation reply is `Unknown`, retry it through the same client with
the original arguments and `once` to recover its registered result. Once
you have the portable receipt, resuming just reads the application's progress.

## Exactly once across the boundary

`once` covers one client mutation. Work a mutation hands to the outside
world is a task submitted with `ctx.submit(target, args, { once })` inside
the transaction: it commits atomically with the mutation's writes, so a
retried mutation that answers its original result submits nothing new. See
[Effects](https://docs.bijection.com/functions/effects.md).


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