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

# client/service/message-policy

> The message-policy document `sharnessd` reads and the firewall it builds from one.

# client/service/message-policy

*`packages/client/src/service/message-policy`*

## Purpose

The message-policy document `sharnessd` reads and the firewall it builds from one.

## Public surface

### [`decodeMessagePolicy`](https://github.com/chughtapan/moltzap/blob/main/packages/client/src/service/message-policy/policy.ts#L168)

*Variable*

```ts theme={null}
export const decodeMessagePolicy = Schema.decodeUnknown(
  Schema.parseJson(MessagePolicy),
  { onExcessProperty: "error" },
)
```

Decodes the text of a message-policy document, refusing any property it
does not name, so a misspelled or newer setting never silently weakens the
policy.

### [`MessagePolicy`](https://github.com/chughtapan/moltzap/blob/main/packages/client/src/service/message-policy/policy.ts#L159)

*TypeAlias*

```ts theme={null}
export type MessagePolicy = typeof MessagePolicy.Type;
```

One decoded message-policy document, with Client's defaults filled in.

### [`MessagePolicy`](https://github.com/chughtapan/moltzap/blob/main/packages/client/src/service/message-policy/policy.ts#L135)

*Variable*

```ts theme={null}
export const MessagePolicy = document.pipe(
  Schema.filter(
    (policy) =>
      policy.decisionProvider !== undefined || !needsDecisionProvider(policy),
    {
      message: () =>
        "decisionProvider is required for English rules and the collective norms",
    },
  ),
  Schema.filter(
    (policy) =>
      policy.decisionProvider?.kind !== "unavailable" ||
      !installsCollectiveNorms(policy),
    {
      message: () => "the collective norms need an available decisionProvider",
    },
  ),
  Schema.filter((policy) => uniqueEnglishRuleIds(policy), {
    message: () =>
      "english rule ids must be unique across fixedRules and initialRules",
  }),
)
```

The message policy of one daemon: its firewall mode, the built-in code
middleware of each direction in chain order, its fixed and initial
editable English rules, the decision provider those rules and the
collective norms need, and the check bounds. Absent settings take Client's
defaults. English rules or the collective norms without a decision
provider, the collective norms with the `unavailable` provider, which would
leave every one of their English checks unavailable, and an English rule
id given twice across the fixed and initial rules are refused.

### [`messagePolicyFirewall`](https://github.com/chughtapan/moltzap/blob/main/packages/client/src/service/message-policy/compose.ts#L59)

*Variable*

```ts theme={null}
export const messagePolicyFirewall = Effect.fn("messagePolicyFirewall")(
  function* (
    policy: MessagePolicy,
    provider: DecisionProvider,
  ): Effect.fn.Return<FirewallOptions, CheckInputError> {
    const bundle = yield* Effect.cached(
      collectiveNorms(
        provider,
        policy.englishSettings,
        policy.maximumHistoryRecords,
      ),
    );
    const incomingChain = yield* Effect.forEach(
      policy.incoming,
      (entry) => incoming(entry, bundle),
      { concurrency: 1 },
    );
    const outgoingChain = yield* Effect.forEach(
      policy.outgoing,
      (entry) => outgoing(entry, bundle),
      { concurrency: 1 },
    );
    return {
      mode: policy.mode,
      incoming: incomingChain,
      outgoing: outgoingChain,
      settings: policy.settings,
      facts: policy.facts,
      codeDigest: armDigest(policy, [...incomingChain, ...outgoingChain]),
      maximumHistoryRecords: policy.maximumHistoryRecords,
      maximumConcurrentChecks: policy.maximumConcurrentChecks,
      english:
        policy.english === undefined
          ? undefined
          : {
              fixedRules: policy.english.fixedRules,
              initialRules: policy.english.initialRules,
              provider,
              settings: policy.englishSettings,
            },
    };
  },
)
```

The firewall installation of one message-policy document: its built-in
middleware in chain order, its English rules assessed by `provider`, and
its settings. This is the trusted-composition hook: a composition that
brings its own decision provider installs the result with
`Daemon.withFirewall`. `codeDigest` is the SHA-256 of the Client release,
the mode, each registration's id and revision, the fixed English rules,
the decision provider and the settings. It leaves out what differs between
the agents of one arm: the facilitator role, which no revision holds, the
allowed-sender list and the initial editable rules, so every member
records the same digest.

## Files

* `compose.ts`
* `policy.ts`


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