# @haven_ai/signer

The Haven **edge signer**. It holds the delegate key locally and signs — and
that's very nearly all it does. It pairs with the hosted, keyless
`@haven_ai/mcp-server`: the hosted server identifies the agent, constructs
unsigned payloads and relays signatures; this one signs. **The delegate key
never leaves this process** — it is not part of any request or response, and
only signatures (and the standard x402 merchant payment header) ever come out.

Design: [`docs/architecture/07-edge-signer.md`](../../docs/architecture/07-edge-signer.md).
Contract: [`docs/architecture/06-hosted-mcp-connect-flow.md`](../../docs/architecture/06-hosted-mcp-connect-flow.md).

Requires **Node >= 22**; the signer refuses to start on anything older, before
it reads a key.

## Are you an AI agent whose user has no Haven account yet?

Read **`/for-agents.md`** on the Haven host your user gave you — or
[the copy in this repository](https://github.com/d-hinders/Haven-AI/blob/dev/packages/frontend/public/for-agents.md)
if you do not have that host yet.

Your user creates the account and the passkey: those are theirs, they need a
human, and you should never ask for their password. You can do everything else
— including running the connector command from the setup prompt they paste you,
and managing the account from the shell with `@haven_ai/cli`.

## Two ways to use it

**As a local MCP signer** (for Claude Desktop / Code / Cursor) — run it
alongside the hosted Haven connection. The supported install is the connector
the Haven dashboard hands out, which writes the MCP config and pins the
runtime:

```sh
npx @haven_ai/connect@<channel>
```

`<channel>` is a placeholder: production hands out `@alpha`, and it is the right
answer unless your dashboard hands you a different one — **run the command that dashboard shows
you**, which names the npm dist-tag that backend is paired with. This signer's
own messages do the same: since [#2423](https://github.com/d-hinders/Haven-AI/issues/2423)
every "rerun the connector" hint it prints names the channel THIS build was
published under, so a build installed from a non-production channel tells you
to reinstall from that same channel rather than sending you to production.

That command SETS UP an agent. To update a signer that has fallen behind the
backend's expected-context version on a machine that is already set up, run
the connector doctor instead — `npx -y @haven_ai/connect@<channel> --doctor` —
and then the `--repair` line it prints; a bare re-run stops at "Missing
--setup" (#3412). Every update hint this signer prints names that form. To run
the signer directly:

```sh
HAVEN_DELEGATE_KEY=0x... npx @haven_ai/signer@alpha
# or
npx @haven_ai/signer@alpha --credentials /path/to/haven-agent.json
```

On first launch, the signer prints the delegate address, any wallet/network
metadata found in the credential file, and the sign-only tool list. It refuses
to start until acknowledged with either `HAVEN_SIGNER_ACK=<hash>` or
`npx @haven_ai/signer@alpha --credentials /path/to/haven-agent.json --ack`.

It exposes five stdio MCP tools, all sign-only:

| Tool | Does | Emits |
|---|---|---|
| `haven_sign` | Sign one payment, or (#3329) one task-budget open/close, or (#3330, #3506) one sub-budget open/close. Preferred form is `{ payment_id }` alone for a payment, `{ task_budget_id }` alone for a task-budget open/close, or `{ sub_budget_id }` alone for a sub-budget this agent delegates (its own parent-child and the grant to another agent of the same account) — exactly one of the three; the signer fetches the exact payload itself. Signs only Haven-prepared payloads (#3272, #3281, #3329): a direct-payment `PackedUserOperation` from this signer's own delegate account whose only call redeems a delegation made to that account directly, through a self-delegated task-budget child under it (the two-link `[task child, budget]` chain), or as a sub-budget grant from another agent of the same account redeemed under that agent's self-narrowing (the three-link `[sub grant, parent child, budget]` chain, #3330), or — against a Haven-signed context, which it records and binds — an EIP-3009 funding leg of that same shape paying this signer's own delegate EOA, or an erc7710 settlement child from this signer's own account; a self-delegated task-budget child `Delegation` from this signer's own account (`assertOwnTaskChild`); a sub-budget child `Delegation` from this signer's own account narrowing its budget for another agent of the same account (`assertOwnSubBudgetChild`); or a task-budget or sub-budget close — a `disableDelegation` UserOp from this signer's own account revoking that child (`assertOwnTaskBudgetCloseUserOp`, `assertOwnSubBudgetCloseUserOp`); other typed data is refused (`TYPED_DATA_NOT_ALLOWED`) | `{ signature }`, `{ signature, x402_binding }`, `{ signature, task_budget_id, purpose }`, or `{ signature, sub_budget_id, purpose }` (then `haven_submit` with that id) |
| `haven_sign_x402` | One-shot x402: funding signature **and** the merchant header in a single local call (`haven_sign` + `haven_x402_sign_header`). `{ payment_id }` alone is the preferred call | `{ signature, x402_binding, payment_header, accepted }` |
| `haven_x402_sign_header` | Build + sign the EIP-3009 merchant payment header, only when the fresh merchant `payment_required` matches the recorded `x402_binding` | `{ payment_header, accepted }` |
| `haven_sign_sweep_delegate` | Sign a Haven-prepared gasless EIP-3009 sweep that recovers stranded funds from the delegate wallet back to your own account. Never broadcasts | `{ signature }` |
| `haven_sign_siwx` | Answer a merchant x402 Sign-In-With-X (SIWX) challenge (#3728): the signer composes the EIP-4361 message ITSELF from the grammar-validated `sign-in-with-x` challenge and the delegate address read from the key — never a caller-supplied message — signs it EIP-191, and returns the finished header. Refuses (`SIWX_REFUSED`) unless the challenge domain matches the `url` host (the FINAL URL after redirects — never follow a redirect with the header or a resulting session token attached), the resource uri origin matches, the URL is https, the nonce is well-formed, the expiry window is ≤ 5 minutes (plus a small clock-skew tolerance), and `supportedChains` offers the credential chain with `eip191`. Moves no funds and grants no spend authority | `{ sign_in_with_x_header }` — retry the merchant with the `SIGN-IN-WITH-X` header set to that value |

The `initialize` handshake advertises which binding versions this signer
understands, under `capabilities.experimental['haven/signer-compatibility']`
and in the MCP `instructions` string. Both are **derived** from
`SUPPORTED_X402_EXPECTED_VERSIONS` / `SUPPORTED_SWEEP_BINDING_VERSIONS` in
`src/core.ts` and `SUPPORTED_DIRECT_SIGN_CONTEXT_VERSIONS` (#3271),
`SUPPORTED_TASK_SIGN_CONTEXT_VERSIONS` (#3329) and
`SUPPORTED_SUB_BUDGET_SIGN_CONTEXT_VERSIONS` (#3506) in `src/sign-context.ts` — the same constants the signing path enforces — so this README
deliberately does not restate the numbers. Read them from the handshake, or
from those constants.

**As a library** (for SDK / autonomous agents):

```ts
import { createEdgeSigner } from '@haven_ai/signer'

const signer = createEdgeSigner(process.env.HAVEN_DELEGATE_KEY!)

// Delegation-rail direct payment: sign the EIP-712 typed data the account
// validates — not the bare ERC-4337 hash.
const signature = await signer.signDelegationTypedData(typedData)

// x402, EIP-3009 bridge: sign the funding leg, then the merchant header.
const funding = await signer.signX402FundingTypedData(fundingTypedData, expected)
const { paymentHeader } = await signer.buildX402PaymentHeader(
  paymentRequired,
  funding.x402Binding,
)
```

The signer also exposes `signSweepAuthorization(input)` for the gasless sweep and
`signSiwxMessage(message)` for #3728's composed EIP-4361 sign-in — the message
always reaches it composed in-package from grammar-validated challenge fields,
with the address taken from the key.
All five are methods on the object `createEdgeSigner` returns, not standalone
exports. There is NO raw-hash primitive: `signPaymentHash(hash)` (raw ECDSA over
the retired AllowanceModule rail's hash) was removed in #3169, and
`signX402FundingHash` (the expected-context v1 bare-hash path) in #3272 — the
signer supports expected-context versions 2 and 3 only. **Library boundary
(#3272):** `signDelegationTypedData` is a verbatim primitive for embedders —
it signs whatever typed data it is handed. The allowlist lives in the MCP tool
layer: `haven_sign` signs typed data only when it is a bound direct-payment
`PackedUserOperation` (below) or an x402 payload against a Haven-signed context,
refuses anything else with `TYPED_DATA_NOT_ALLOWED`, and answers a bare
`payload_hash` with `BARE_HASH_REFUSED`. An erc7710 settlement child has no
EIP-3009 header step: `haven_sign_x402` answers a settlement child whose
expected-context version is supported before signing with `NO_FUNDING_LEG`,
and `haven_x402_sign_header` answers its binding with `NO_FUNDING_LEG` while
that id is among the last 64 the process retired; after eviction or a restart
it gets the unknown-binding refusal (#4242/#4248; `NO_FUNDING_LEG` carries
`next_action: sign_and_submit_payment` and a `next_tool_omitted_reason` naming
BOTH settle routes — `haven_submit` with `settlement_scheme: 'erc7710'` for a
plain-HTTP merchant, `haven_settle_mcp_tool` with no `payment_header` for an MCP
merchant). Either way no header is
built. An embedder that calls
`signDelegationTypedData` directly owns that check itself. The core's
`signX402FundingTypedData` is not a verbatim primitive: since #3281 it runs
the x402 shape checks itself (a guarded funding leg or a verified settlement
child only), whoever calls it.

**Every success result names its next step (#4449).** Until #4449 only the
refusals carried the next-step family (#3103); a successful signature said
nothing about what to call next. Each of the six success sites now carries
either `next_tool_name` + `next_tool_server_role: 'hosted'` + `next_arguments`,
or a literal `next_tool_omitted_reason` — the `next_tool` family only, never
`next_action` / `safe_to_continue`, which stay on refusals. Where the signer
knows the tool it names it: a direct-payment `haven_sign` hands off to
`haven_submit` with `{ payment_id, signature }`, and
`haven_sign_sweep_delegate` hands its signature back to `haven_sweep_delegate`
with `{ authorization, signature }`. Where it cannot know, it says why instead
of guessing: `haven_sign` on an x402 context, `haven_sign_x402` and
`haven_x402_sign_header` cannot tell an MCP merchant from a plain-HTTP one, nor
a fresh signature from a funded-retry resume, so each reason names both settle
routes. `haven_sign_siwx` names no tool because no hosted tool follows a
sign-in. The change is additive — no field is removed (including
`x402_binding`, whose retirement for a settlement child the `haven_sign`
description now states), no signing decision changes, and
`SUPPORTED_X402_EXPECTED_VERSIONS` is untouched. Installed signers
(`0.10.0-alpha.0`, `0.8.1-alpha.0`, observed live 2026-10-26) never emit these
steps, so the hosted prepare and pay guidance stays authoritative.

**The unbound-branch allowlist (#3272).** Without an x402 context, `haven_sign`
signs typed data only when ALL of these hold; otherwise it refuses, with no
signature and no audit entry (`TYPED_DATA_NOT_ALLOWED`, or
`USEROP_BINDING_MISMATCH` when the #3271 binding check fails, or the #1476
refusal for an unbound `Delegation`): the primary type is
`PackedUserOperation`; it passes the #3271 binding check below; its chain has pinned delegation contracts (Base, Base Sepolia); its
sender is THIS signer's own delegate account (the counterfactual
HybridDeleGator for the delegate key, derived offline — `delegate-account.ts` in `@haven_ai/sdk`);
and its `callData` is a single `execute` to the DelegationManager calling
`redeemDelegations`, whose arguments are decoded too (`redemption-guard.ts` in `@haven_ai/sdk`):
exactly one of three chain shapes (#3329, #3330) — a single grant made to this signer's own account by a
different account, a two-link `[task child, budget]` chain whose leaf is self-delegated by this
signer's own account, or a three-link `[sub grant, parent child, budget]` chain whose leaf is granted
to this signer's own account by another account and whose middle link is that account's
self-delegation — never an empty chain or any other multi-link shape,
`SingleDefault` mode, and canonical
encoding at every level. The argument check matters: an EMPTY permission
context makes the DelegationManager run the execution as the account itself,
which would reach `transferOwnership`. A delegate-wallet `TransferWithAuthorization` or `Permit`,
a UserOp that calls the account itself (`transferOwnership`, `updateSigners`,
`upgradeToAndCall`), a UserOp for another account, and arbitrary typed data are
all refused. The audit log records the EIP-712 digest actually signed, not the
caller's `payload_hash`. Since #3283 the check itself is `@haven_ai/sdk`'s
`assertBoundDirectPaymentUserOp` (with the redemption guard, the account
derivation and the settlement-child verifier), which this signer imports and
which `HavenClient.signForData` runs as well — one implementation.

**Direct payments (#3271).** A direct payment (`POST /payments`, surfaced as
`haven_send` / `haven_pay`) is signed as the account's EIP-712
`PackedUserOperation`, and Haven returns both that typed data and
`payload_hash` — the ERC-4337 v0.7 UserOperation hash of the same operation.
Before `haven_sign` signs one, it recomputes that hash from the typed data's
own domain, types and message and refuses (`USEROP_BINDING_MISMATCH`) unless
it equals `payload_hash` exactly, in the HybridDeleGator domain of the typed
data's own sender, against the v0.7 EntryPoint. This is a **corruption**
check: the caller supplies both values, so it proves they describe the same
operation, never that Haven prepared it — provenance is `payment_id` (below).
It runs whether the typed data arrived as a tool argument or by the
`payment_id` fetch; a fetched direct context that is not a
`PackedUserOperation` at all is refused by the same check. The x402 EIP-3009
bridge's funding leg is checked against the Haven-signed expected context AND,
since #3281, by this same binding check against the declared UserOp hash, the
direct-payment allowlist and a recipient pin (a transfer of the quoted amount
to this signer's own delegate EOA). A settlement child must be re-delegated
from this signer's own account, never a ROOT grant. Anything else on the x402
arm is `TYPED_DATA_NOT_ALLOWED`, however validly Haven's binding key declared
it. The check exists because this typed data is
multi-KB and can reach the signer through a language model relaying it by
hand: one corrupted character used to produce a valid-looking signature over
the wrong digest, surfacing only as an opaque `AA24 signature error` from the
bundler, after the signature was already produced.

## Startup, CLI options and the consent screen (#3173)

**Startup cost.** The signer loads `@haven_ai/sdk/edge` — the ethers-free
subset of the SDK it actually calls (error classes, typed-next-step builder,
x402 message builders, viem-based key helpers) — never the SDK barrel, and it
loads the `x402` package only on the merchant-header leg, on first use. Measured
on the same machine (median of 5 cold runs, macOS, Node 22): `--help` 1.47 s →
0.71 s; the consent refusal 1.55 s → 0.77 s; `import('@haven_ai/sdk')` 1135 ms
versus `import('@haven_ai/sdk/edge')` 385 ms, of which viem is ~330 ms and stays
(the signer signs typed data with it). A loader hook resolving every module at
startup finds zero packages named `ethers` or `x402` (the SDK barrel, as a
positive control, resolves two). Two tests keep it so: `sdk-edge-import.test.ts`
fails if any runtime file imports the barrel or `x402/schemes` statically, and
the SDK's `edge-imports.test.ts` fails if the subpath's import graph ever
reaches ethers, `x402` or the HTTP client.

**CLI.** `--credentials <path>` (alias `--credentials-path`), `--ack`,
`--help`/`-h`. Any other option is
refused with one stderr line naming `--help` and exit code 2 — before #3173 an
unknown flag was silently ignored, so `--ack-local-tools` (the connector's
flag, which the connector's doctor tells you to pass to the *connector*)
produced only the consent wall. `--help` lists every registered tool (pinned
against `toolSchemas`, so a newly registered tool cannot drift out of the text) and names
`npx @haven_ai/connect --doctor`.

**Consent screen.** The first-launch block summarises each tool in one
human-sized line (the full agent-facing descriptions are what the runtime sees,
not what a person approves) and ends by naming the connector's doctor for the
connector-wired case, where the doctor shows this as a failed *Signer stdio
handshake* check (the connector's setup outcome reports it as
`local_signer_ack_required`) and the repair is the connector's
`--ack-local-tools`. The refusal an MCP host
relays ("Connection closed" plus this process's exit message) names the same
command. The consent hash covers identity, tool names and the surface version —
not the block's prose — so neither change re-prompts an acknowledged install.

## Several Haven pairs on one machine (#3738)

One harness can carry several Haven pairs — `haven-research` +
`haven-signer-research`, `haven-ops` + `haven-signer-ops`, alongside a bare
`haven` + `haven-signer` (Codex: `haven` + `haven_signer`). A model must act as
one agent per task and keep hosted and signer calls inside that pair. Server
names are easy to cross, so the signer's `initialize` instructions state what
it is bound to:

```
This signer is bound to agent id <agent_id> and delegate address <0x…>.
```

The agent id comes from the credential file's `agent_id` (or `HAVEN_AGENT_ID`
alongside `HAVEN_DELEGATE_KEY`); when neither is set the line says "no recorded
agent id" and still gives the delegate address, which is then compared alone.
The model compares it with `haven_get_agent`'s `id` and `delegateAddress` from
the hosted server it called, and signs nothing on a mismatch. Neither value is secret (the consent screen prints both), and
the line is advisory: what the signer refuses to sign is unchanged.

## Orchestration

Direct payment:

```
hosted:  haven_pay                -> { payment_id, payload_hash, idempotency_key, ... }
local:   haven_sign { payment_id } -> { signature }
hosted:  haven_submit             -> { status, tx_hash }
```

On the delegation rail the payload is the EIP-712 typed data the account
validates, not the bare ERC-4337 hash; since #3495 the `haven_pay` result is
compact and withholds that typed data, so the signer fetches it by
`payment_id`. Note the trust-model asymmetry: this direct leg has no
Haven-signed expected context to verify against. The signer's local check is
the shape allowlist and the #3271 hash binding above, which prove the
operation redeems this agent's own delegation and is uncorrupted, not what it
pays; the authority boundary on amount and recipient is the account's on-chain
caveat enforcers — unlike the x402 legs below.

x402 — **erc7710 direct settlement**, the preferred scheme when the account is
on the delegation rail and the merchant advertises
`extra.assetTransferMethod: "erc7710"`. There is **no funding leg**, so there is
no delegate hot balance and no `haven_x402_sign_header` step:

```
hosted:  haven_pay_x402_quote      -> settlement child + settlement_scheme: erc7710
local:   haven_sign { payment_id } -> child signature (caveats verified locally)
hosted:  haven_submit { settlement_scheme: "erc7710" } -> payment_header
agent:   retry merchant, setting PAYMENT-SIGNATURE ONLY (never X-PAYMENT:
         this header carries a delegation chain and duplicating it is
         refused with HTTP 431)
```

x402 — **EIP-3009 bridge**, the fallback for merchants without facilitator-side
erc7710 support (still most of them). Two local delegate signatures, and a
bounded funding leg:

```
hosted:  haven_pay_x402_quote     -> { payment_id, payload_hash, x402.expected }
local:   haven_sign               -> funding signature + x402_binding; pass { payment_id }
                                    (preferred), or payload_hash + x402_expected together with
                                    typed_data_b64 from a re-run quote with
                                    include_signing_payload=true
hosted:  haven_submit             -> funds account -> delegate EOA
local:   haven_x402_sign_header   -> payment header only if binding matches
agent:   retry merchant with exactly the result's retry_headers:
         PAYMENT-SIGNATURE alone for an x402 v2 payload (X-PAYMENT too only
         for a v1 one) — the v2 header echoes the merchant's extensions and a
         doubled one was refused with HTTP 431 after funding (#4754)
```

On the bridge, pass `{ payment_id }` to the local `haven_sign` call and let the
signer fetch the exact signing bytes and the expected context (preferred). The
decomposed form is `payload_hash` plus `x402_expected` together with
`typed_data_b64` from the quote re-run with `include_signing_payload=true`; a
`payload_hash` with `x402_expected` and no typed data is refused, because the
signer re-derives the funding payload's EIP-712 digest before signing. The
signer records that context and returns a process-local `x402_binding`;
pass that binding into `haven_x402_sign_header` after `haven_submit` confirms.
The signer refuses to authorize the merchant header when the fresh merchant
challenge has a different amount, merchant recipient, resource URL, token asset
or network than the recorded funding intent, refuses an expired window, and
consumes the binding after one header.

The merchant payment header's validity window starts when it is signed, not when
funding confirms — so relay it promptly.

## What the signer refuses to sign

(`USEROP_BINDING_MISMATCH`, the #3271 direct-payment refusal, is described
under [Two ways to use it](#two-ways-to-use-it) and in the
[refusal table](#sign-context-refusal-codes).)

These are local, independent checks. They do not trust Haven's assertion about
what a payload means; they re-derive it.

- **Unauthenticated context.** The expected context must carry Haven's `auth`
  signature over it. Configure `HAVEN_X402_BINDING_SIGNER` (or
  `x402_binding_signer` in the credential file) so the signer can reject
  locally invented or tampered contexts before signing anything.
- **Wrong signing mode.** The *context* selects the path, never the caller's
  arguments: a context commits to a typed-data digest and requires exactly that
  typed data. A context without one (the retired version 1) is refused as an
  unsupported version since #3272 — there is no bare-hash signing path.
- **Another agent's quote.** A context naming a `payer_delegate` that is not
  this signer's own delegate is refused.
- **An unbound delegation payload.** Typed data with `primaryType: "Delegation"`
  is never raw-signed without a context binding it.
- **An erc7710 settlement child whose caveats do not match what Haven declared.**
  The signer re-derives the child's meaning from its own pinned
  `DelegationManager` and caveat-enforcer addresses (cross-checked against
  `@metamask/smart-accounts-kit` by a test, never fetched from Haven, which
  would make the check circular): the payee pin, the exact token and amount,
  the chain, and a settlement window bounded at 600 seconds. Extra caveats are
  allowed — top-level caveats are AND-ed during redemption, so an unrecognised
  one can only add a constraint.
- **A binding version it does not understand.** The refusal is machine-readable
  — `code`, `supported_versions`, `received_version`, `fallback`, and (#3103)
  `next_tool_omitted_reason` — and names updating the signer as the fix.
- **A sign-in challenge that does not describe the origin it is sent to (#3728).**
  `haven_sign_siwx` composes the EIP-4361 message itself from the challenge's
  grammar-validated fields and the delegate address from the key; a challenge
  whose domain is not the host of the `url` being called, whose resource uri
  origin differs, that is not https, that carries a malformed or missing nonce,
  a `version` other than `"1"`, an expiry window beyond the spec's 5-minute max
  age (plus a small clock-skew tolerance), or that does not offer the credential
  chain with an `eip191` signature type is refused with no signature and no
  audit entry. A caller-supplied message, hash or byte string is never signed —
  a SIWX signature therefore cannot double as a payment authorization, a
  UserOp signature or any other Haven signature.
- **A sweep that does not move funds out of this delegate's own key** — the
  `from` check is unconditional. The **destination** check is not, and this is
  the one asymmetry in this list: the signer compares the sweep's `to` against
  the account address **only when the local credential records one**
  (`account_address`, or the pre-#2908 `safe_address` / `safeAddress`, which
  are read permanently; from the environment, `HAVEN_ACCOUNT_ADDRESS` only —
  `HAVEN_WALLET_ADDRESS` and `HAVEN_SAFE_ADDRESS` were removed by #2914, so a
  machine still configured through either records no account address and lands
  in exactly the degraded case this paragraph describes). Run with
  `HAVEN_DELEGATE_KEY` alone — or with a credential
  whose account address is absent — and there is no local value to compare
  against, so the destination is authenticated by Haven's binding signature and
  the token/chain canonicality check, but not independently re-derived. Prefer
  a credential file that carries the account address.

## Sign-context refusal codes

`{ payment_id }` calls (`haven_sign` / `haven_sign_x402`) fetch the exact
signing payload from Haven (`GET /x402/:id/sign-context`, and for a direct
payment `GET /payments/:id/sign-context` — see the #3271 paragraph below and
[Custody](#custody)). Every refusal on that fetch is a `HavenSignContextError`
(#3001) — a `HavenSigningError` subclass, so `instanceof HavenSigningError`
still holds everywhere it did before, but structured like the version-mismatch
refusal below rather than prose alone: `code`, `next_action`, and — per
refusal class — `fallback`, `retry_with_new_quote`, `http_status`,
`backend_error_code`. Since #3103 each also carries a typed next step: the
`SIGN_CONTEXT_REFUSED` (other) row names the hosted status read
(`next_tool_server_role: hosted`, `next_tool_name: haven_get_payment_status`,
`next_arguments: { payment_id }` — resolve the role against your own server
names, and with several Haven pairs against this signer's own pair); every other row carries `next_tool_omitted_reason` with the exact
remedy. `message` is unchanged.

**#3271: `haven_sign` (never `haven_sign_x402`) has one escape from this
table.** When the x402 fetch answers `SIGN_CONTEXT_REFUSED` with
`http_status: 409` and `backend_error_code: 'sign_context_unavailable'` — this
`payment_id` names a direct payment, not an x402 intent — `haven_sign` fetches
`GET /payments/:id/sign-context` instead, same auth header, timeout and
refusal structuring. That second fetch's own refusals reuse the codes in the
table below with direct-payment remedies: no quote to re-run, and the
`typed_data_b64` relay — reached by re-running whichever of `haven_send` /
`haven_pay` was called, with the SAME `idempotency_key` (echoed on its
result) plus `include_signing_payload: true` (#3495; superseding #3277's
"the relay fields stay") — is the fallback, EXCEPT the direct fetch's 404
(the table row below): that message names BOTH routes, since a 404 there is
ambiguous with a backend old enough to predate #3495 too, whose result
already carries the pair unconditionally with no opt-in to re-run.
A 409 `sign_context_unavailable` from the direct route too (an
x402 row the x402 route could not serve, or a direct row with no stored
signing payload) surfaces the x402 route's own refusal instead. `haven_sign_x402` never takes this branch: a direct payment
carries no x402 context to fund a merchant retry with, so it surfaces the
409 unchanged.

| `code` | When | `next_action` | `fallback` | extra |
|---|---|---|---|---|
| `SIGN_CONTEXT_TIMEOUT` | The fetch (or its body read) did not finish within `SIGN_CONTEXT_TIMEOUT_MS` | `stop_and_tell_user` | `typed_data_b64` | — |
| `SIGN_CONTEXT_UNREACHABLE` | The fetch failed before any response (DNS, connection refused, TLS, …) | `stop_and_tell_user` | `typed_data_b64` | — |
| `SIGN_CONTEXT_MALFORMED` | The response body was missing `sign_data.typed_data` / `x402_expected` (a pre-#1263 backend), or — on the direct-payment fetch — an unsupported `direct_sign_context_version` or a `signature_scheme` other than `eip712_userop` | `stop_and_tell_user` | `typed_data_b64` | — |
| `SIGN_CONTEXT_REFUSED` (x402: 410 or `expired`; direct: `expired` only) | x402: the quote's window closed. Direct: the payment's window closed — call `haven_send` / `haven_pay` again with the same `idempotency_key` | `payment_window_expired` | — | x402 only: `retry_with_new_quote: true`; both: `http_status`, and `backend_error_code: 'expired'` when the backend sent one |
| `SIGN_CONTEXT_REFUSED` (404, direct fetch) | The `payment_id` is not this agent's, or the backend predates #3271 and has no direct route | `stop_and_tell_user` | `typed_data_b64` | `http_status`, `backend_error_code` |
| `SIGN_CONTEXT_REFUSED` (426 `client_outdated`, #3303) | This signer is below the minimum version the Haven deployment has set (only when one is set). Nothing is signed; the prepared payment is left unsigned. Run `client_update.upgrade_command` and the repair line it prints, restart the agent runtime, then retry the same call | `stop_and_tell_user` | — | `http_status: 426`, `backend_error_code: 'client_outdated'`, `client_update` (the update command as data); carries `next_tool_omitted_reason`, not the status read |
| `SIGN_CONTEXT_REFUSED` (other) | Unknown `payment_id` (404, x402 fetch), `already_executed` / `not_signable` (409), a bare 410 retired-rail tombstone (direct fetch) — or `sign_context_unavailable` (409): always on `haven_sign_x402`; on `haven_sign` only when neither route can serve the row | `stop_and_tell_user` | — | `http_status`, `backend_error_code` |
| `USEROP_BINDING_MISMATCH` | A direct payment's `PackedUserOperation` typed data (from the direct fetch, or a tool argument) does not recompute to its own `payload_hash`, or a fetched direct context is not a `PackedUserOperation` — see [Two ways to use it](#two-ways-to-use-it) above | `stop_and_tell_user` | — | no `http_status` — this is a local recomputation, not a backend refusal |
| `TYPED_DATA_NOT_ALLOWED` | Typed data without an x402 context that is not a bound direct-payment `PackedUserOperation` (#3272): wrong chain, not this signer's own account, a call other than `execute` → DelegationManager → `redeemDelegations`, or a redemption whose delegations are empty, not made to this account, or in a non-default mode — see the allowlist paragraph above. Reachable by `payment_id` when Haven serves such a payload | `stop_and_tell_user` | — | no `http_status` — a local shape check, not a backend refusal |

`fallback: 'typed_data_b64'` appears only where signing OTHER bytes is a
remedy — a transport failure or a body this signer could not read, plus one
backend refusal: a 404 from the direct-payment fetch (an older backend with
no direct route, #3271). For x402 it is **not** in the default quote result
since #1272: obtain it by re-running the SAME quote tool with the SAME
`idempotency_key` plus `include_signing_payload: true`, then pass
`typed_data_b64` (plus `payload_hash` / `x402_expected`) instead of
`payment_id`. A direct payment's `haven_send` / `haven_pay` result is compact
by default too (#3495) — `payload_hash` is on that result unconditionally (as
is `idempotency_key`, generated fresh when the caller passed none and always
echoed); only `typed_data` / `typed_data_b64` are withheld, reached the same
way — a `include_signing_payload: true` re-run on the SAME `idempotency_key`.
Any other backend REFUSAL
carries no fallback: an expired, executed or unsignable intent cannot be
rescued by re-signing its bytes — an expired x402 one is re-quoted (the same
`payment_window_expired` + `retry_with_new_quote` the signer emits for
`PAYMENT_WINDOW_EXPIRED`), an expired direct one is re-sent with the same
`idempotency_key` (no `retry_with_new_quote`), the rest stop. These codes are signer-local,
not part of `@haven_ai/sdk`'s `AgentPaymentFailureCode` taxonomy, since they
describe a local fetch failure, not a payment-domain outcome, and never reach
the backend's REST/OpenAPI surface — only this package's MCP tool responses.

## Custody

The delegate key is read from `HAVEN_DELEGATE_KEY` or a `--credentials` file's
`delegate_key` (with a permissive-file warning). It stays in this process, and
is never transmitted.

**The signer's network calls are all reads of a signing context.** Since
[#1263](https://github.com/d-hinders/Haven-AI/issues/1263) the `{ payment_id }`
form of `haven_sign` and `haven_sign_x402` performs an authenticated,
read-only `GET /x402/:payment_id/sign-context` against Haven, so that agents
never have to relay multi-KB EIP-712 payloads through a model's context
window. Since #3271, `haven_sign` (never `haven_sign_x402`) falls back to a
second read — `GET /payments/:payment_id/sign-context` — only when that first
fetch answers the backend's 409 `sign_context_unavailable`, i.e. this
`payment_id` names a direct payment rather than an x402 intent. Since
#3329/#3330, `haven_sign` given a `task_budget_id` or `sub_budget_id` reads
`GET /task-budgets/:id/sign-context` or `GET /sub-budgets/:id/sign-context`.
**Only the Bearer API key and the signer's `X-Haven-Client` version header go
out on any read; the delegate key is never part of a request or response.** Since #2985 the two payment reads are bounded (the budget reads use the same
15 s bound): each payment read aborts
after `SIGN_CONTEXT_TIMEOUT_MS` (15 s) and reports a `HavenSignContextError`
naming the timeout and the `typed_data_b64` fallback, so a hung backend cannot
hang the signer — and the agent — past the funding window. Every refusal on
any of these fetches (timeout, unreachable host, a non-ok backend response, a
malformed body) is structured the same way, not just prose — see
[Sign-context refusal codes](#sign-context-refusal-codes) below.
Nothing else in the package reaches the
network: `haven_x402_sign_header` and `haven_sign_sweep_delegate` never fetch,
the library surface above (`createEdgeSigner` and its five methods, over
the network-free `src/core.ts`) never fetches, and passing the payload as
`typed_data_b64` instead of `payment_id` keeps even the two fetching tools
offline. It never relays, submits, or broadcasts.

The fetch needs `api_url` and `api_key` from an `identity.json` sitting **next
to the signer credential file** — the signer's own credential still needs no
`api_key`. So the network call is a property of how you start the signer, not
just of which tool you call: run it from `HAVEN_DELEGATE_KEY` alone and there is
no credential file, hence no directory to find an `identity.json` in, so the
`{ payment_id }` form refuses with a message naming the `typed_data_b64`
fallback rather than reaching out — and the process makes no network calls at
all. Egress is needed by `--credentials` / `HAVEN_CREDENTIALS` runs that use the
preferred `{ payment_id }` call, which is what the connector install above
sets up.

Fetched bytes are treated as untrusted input exactly like a tool argument: the
same digest re-derivation and Haven-binding verification apply, because what
makes them safe is the verification, not where they came from.

Connect Agent 2 may create the signer credential file locally during setup. In
that flow the connector's register call (`registerSetup` in
`packages/connect/src/api.ts`) sends Haven the setup token and challenge id,
the public signing address and its proof, the API-key hash and prefix, the
runtime labels (runtime, connector version, MCP server name, run mode,
connector context) and the install capabilities. Its other calls in the same
file — `resolveSetup`, the `/install-status` and `/retired-agents` reports,
and the `/connector-status` read — carry the setup token or setup id, install
status, runtime labels and agent ids; all but `resolveSetup` authenticate with
the new API key. (The `/machine-payments/agent` identity read in the same file
belongs to re-key, which calls it with the current and then the new key.) The
plaintext API key is sent only as the bearer credential on those calls, and
Haven stores only its hash; the delegate key never leaves the machine.

## Local audit

Every MCP signing operation appends a JSONL row locally, best-effort (see the
end of this section). File-backed runs write
next to the credential as `<credential>.signer-audit.jsonl`; env-only runs use
`~/.haven/signer-audit.jsonl`. Rows include timestamp, tool, payload hash and
delegate address, plus the account address and chain id when the credential
carries them. They never include the delegate key, the signature, or the x402
payment header.

Since #3172 the sidecar is owner-only and bounded. It is created `0600` — the
mode the credential beside it is expected to have — and a sidecar found
readable beyond its owner (every release before #3172 created it with the
default mode, `0644` under the usual `umask 022`, or an operator loosened it
later) is tightened to `0600` in place on the next
append, with one stderr line per occurrence. If `chmod` is refused, the line
names the same `chmod 600` remedy the credential warning gives. If the path
is a symlink (or anything but a regular file) nothing is chmod-ed — that
would hit the target — but note that audit rows are still written through
the link to its target, so the line says to remove the link (`rm <path>`),
not to chmod it. The check runs before rotation, so a legacy file that
rotates carries `0600` into `.1`. When the live file reaches
`AUDIT_ROTATE_BYTES` (8 MiB, roughly 30 000 rows) it is renamed to
`<path>.1`, replacing the previous `.1`, and a fresh file starts — two
generations at most. The rotation decision is not atomic across processes:
two signer processes on one credential that both hit the bound can leave the
predecessor generation discarded (the current entry is never lost). A failed
audit write (disk full, read-only, two appends racing at the bound) is
reported on stderr and never fails the signing call that already produced
its signature — so the consent block's
"appended for every signing operation" is a best-effort promise since #3172,
kept unchanged in text (editing consent copy does NOT move the consent hash —
that covers identity, tool names and the surface version, as the consent
screen section above already says; #3279 rewords the block without
re-prompting anyone). The
credential check itself is unchanged in wording and still judges a symlinked
credential by its target. The
`payload_hash` argument itself is bounded on the tool schema to a 32-byte hash
(`0x` + 64 hex), so the audit field is never caller-controlled free text; the
two object arguments that do reach the file (`payment_required`,
`authorization`) were already hashed before being written, and
`x402_expected` is never written to the sidecar.

## Hot-wallet minimization

This applies to the **EIP-3009 bridge only** — the erc7710 path above has no
funding leg and no delegate balance to strand. On the bridge, the account
briefly funds the delegate EOA before the merchant settles the EIP-3009
authorization. Keep delegate balances transient: keep budgets small and
period-bound, retry the original merchant session only after funding confirms,
and sweep stranded delegate balances (`haven_sign_sweep_delegate`) when the
merchant retry fails or does not settle before authorization expiry.
