---
title: "Payload encryption"
description: "How the Dex Agents CLI commands encrypt session values with a key file, what encryption changes, and the errors it reports."
canonical_url: "https://docs.superdurable.io/zh-Hans/reference/agents/cli-payload-encryption/"
source: "docs/reference/agents/cli-payload-encryption.mdx"
locale: "en"
site_locale: "zh-Hans"
---

> 供 AI Agent 使用的全部页面索引：https://docs.superdurable.io/zh-Hans/llms.txt

> 本页内容是英文原文，中文译文还在准备中。

<a id="payload-encryption"></a>

# Payload encryption

Since 0.6.19. A command can encrypt the session payloads it writes with a
payload key file, so the Dex server stores those payloads as ciphertext.
Existing plaintext values and indexed titles keep their stored form. Point the
command at the file:

```yaml
dex:
  payload_encryption:
    key_file: ~/.dex/payload-keys.json
```

Create the file with `dexcli codec-server keygen -o FILE`. Every Worker and
Client of the command's sessions then encrypts and decrypts with its keys, as
the Dex SDKs do with the same file. See
[Payload encryption](https://docs.superdurable.io/zh-Hans/reference/server/payload-encryption.md) for the key file,
the encryption, and what stays plaintext.

| Setting                           | Meaning                                                                                                                                               |
| --------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| `dex.payload_encryption.key_file` | The payload key file. It applies to every session of the configuration file, and session settings cannot change it.                                   |
| `DEX_CODEC_KEY_FILE`              | Overrides `key_file`, or turns encryption on without it. A value that is empty after trimming is not set. It works with any `dex.server`, `http` too. |

Both settings expand `~` and `~/` paths to your home directory. A relative
path in the configuration file is relative to the file's directory. A relative
path in the environment variable is relative to the working directory.

The command reads and checks the file once at startup, before it starts or
reuses a Dex server. A missing or invalid file stops the command with exit
code 2. The line never holds key bytes. It has one of two forms:

```text
cannot read the payload key file FILE that dex.payload_encryption.key_file or DEX_CODEC_KEY_FILE names: REASON
the payload key file FILE that dex.payload_encryption.key_file or DEX_CODEC_KEY_FILE names is not a valid key file: REASON
```

If `DEX_CODEC_KEY_FILE` alone turns encryption on, `run` and `fork` print the
key file and the target Dex server before they write. The line informs you. It
does not ask for confirmation.

The Worker host, the terminal UI, and a local `dexcli dev` that the command
starts receive the key file's path, never a key. The command removes
`DEX_CODEC_KEY_FILE` from the environment of tool subprocesses and from MCP
header variable expansion.

<a id="payload-key-file-care"></a>

## Key file care

- Keep the key file and its directory owned by you and not writable by others.
  Give the file mode `0600`.
- Keep the file in place while a command runs. The Worker host and the
  terminal UI read it again when they start.
- After you replace or rotate the file, restart every process that uses it.
  These are the command, `dexcli dev`, and every other Worker and Client of the
  sessions.

<a id="payload-encryption-effects"></a>

## What encryption changes

- **Local Dex server.** With `dex.server: auto`, a `dexcli dev` that the
  command starts gets `-codec-key-file` with the same file. Dex Web then
  decrypts the sessions through its local codec server.
- **Reusing a running `dexcli dev`.** The command reuses it only when its Dex
  Web reports a codec server. If Dex Web reports none, or the command cannot
  verify the answer, the command exits with code 1. After an unverified
  answer, retry. You can also stop that `dexcli dev` and run the command
  again, or restart it with `-codec-key-file`. The command cannot check that
  the running codec server has the same keys, so start it with the same file.
- **Session title.** The indexed title is the start of the first message, in
  plaintext. With encryption on, `sessions.summary: auto` stops writing the
  indexed summary. `sessions` still shows the summary fields that an existing
  session already stores. `sessions.summary: on` keeps writing the summary,
  and `run` and `fork` then print one warning that titles are plaintext.
- **Fork.** The new session copies the source's values in their stored form.
  Plaintext stays plaintext and ciphertext stays ciphertext. Payloads that the
  configured command writes afterward follow its encryption settings. Unless
  `sessions.summary` is `on`, the fork removes the new session's indexed
  summary, so the title is not copied into the search index. The source keeps
  its summary.
- **Existing sessions.** A session written before encryption was on keeps
  working. Its old values keep their stored form, and nothing is migrated.
- **Terminal UI.** The terminal UI reads and writes encrypted sessions with the
  same file when `dexcli` is 1.48.2 or later. The command finds `dexcli` 1.48.2
  or later on `PATH` or downloads 1.48.2. When `DEXCLI` points at an older
  `dexcli`, the command prints one line that says so and uses
  [plain chat](https://docs.superdurable.io/zh-Hans/reference/agents/cli-chat.md#plain-chat).
- **Dex Web.** The session pages decrypt and encrypt in the browser through a
  codec endpoint. See [Sessions in Dex Web](https://docs.superdurable.io/zh-Hans/reference/agents/cli.md#dex-web).
- **Not encrypted.** Identifiers, such as session IDs, Flow types, and
  Attribute names, stay plaintext. So do the images in `media.store`.

<a id="payload-encryption-errors"></a>

## Errors

| Situation                                                                              | What the command does                                                                                                     |
| -------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
| The key file cannot decrypt a value it reads, such as a session that another key wrote | Exits with code 1. See the first message below.                                                                           |
| The session holds encrypted values and no key file is set                              | Exits with code 1. See the second message below.                                                                          |
| The session Worker cannot read or parse the key file at startup                        | Exits with code 2. The line omits the file path.                                                                          |
| `dexcli` cannot read or parse the key file before its terminal UI opens                | The command shows the rejection without the path and falls back to plain chat.                                            |
| The key file cannot decrypt a value while the terminal UI chat is open                 | The terminal UI shows the error once per session and retries snapshots. A live-output failure also turns live output off. |

In plain chat, the first message names the key file:

```text
the payload key file FILE cannot decrypt or encrypt a session value (CATEGORY, key ID ID). Use the key file that encrypted the session; dex.payload_encryption.key_file or DEX_CODEC_KEY_FILE names it.
```

The Worker host sends the same line without `FILE`. A value without a key ID
omits `, key ID ID`. The second message is:

```text
the session holds encrypted values, but payload encryption is off. Set dex.payload_encryption.key_file or DEX_CODEC_KEY_FILE to the key file that encrypted the session.
```

Opening or forking such a session fails this way, and so does `sessions` when
it reads an encrypted value. When you resume at a terminal, the command checks
the session before it opens the terminal UI. The terminal UI never shows the
key file's path or ciphertext.

Python `fork_agent_session` and TypeScript `forkAgentSession` take an option
that omits the source summary from the new session. See
[Payload encryption in the packages](https://docs.superdurable.io/zh-Hans/reference/agents/install.md#payload-encryption).
