Policy API Reference
PolicyDescriptor
The wire format for registered governance policies:
interface PolicyDescriptor {
policyId: string; // e.g., "policy.fraud.majority-veto"
mode: string; // Target mode or "*" for mode-agnostic
description: string;
rules: string; // JSON-encoded governance rules
schemaVersion: number; // Rule schema version: 1 for quorum/proposal/task/handoff;
// 1, 2, or 3 (default) for decision, see buildDecisionPolicy below
registeredAtUnixMs?: number; // Set by runtime
}Builder Functions
Value domains (runtime ≥ 0.9.0)
Runtime 0.9.0 validates every rule object against the canonical
schemas/json/policy/*-rules.schema.json and refuses an off-domain value at
RegisterPolicy. Every builder refuses the same values client-side first,
throwing MacpSessionError with a message that names the field:
| Field | Builder | Domain |
|---|---|---|
commitment.authority | all five | one of COMMITMENT_AUTHORITIES |
voting.quorum_type | decision | count or percentage (checked only when voting.quorum is set) |
voting.quorum_value | decision | finite number >= 0 |
objection_handling.veto_threshold | decision | integer >= 1 |
objection_handling.critical_objection_action | decision | one of CRITICAL_OBJECTION_ACTIONS |
evaluation.minimum_confidence | decision | finite number in [0, 1] |
abstention.interpretation | quorum | one of ABSTENTION_INTERPRETATIONS |
acceptance.criterion | proposal | one of PROPOSAL_ACCEPTANCE_CRITERIA |
counter_proposal.max_rounds | proposal | integer >= 0 |
acceptance.implicit_accept_timeout_ms | handoff | integer >= 0 |
Booleans, NaN and ±Infinity are refused wherever a number is expected. The
four sets are exported (ReadonlySet<string>), with the same members as
macp-sdk-python's frozensets. These checks are what the runtime would refuse
anyway, so a descriptor the builders accept is unchanged byte-for-byte.
Breaking for untyped callers. A JavaScript/JSON caller passing an off-domain value used to get a descriptor that registered on a pre-0.9.0 runtime; it now throws at build time.
Extension and annotation keys
Every builder takes an optional last argument, PolicyRuleExtras. For
buildDecisionPolicy it is DecisionPolicyOptions, which extends it. It adds
the two extra top-level key classes RFC-MACP-0012 §4 allows beside the
standard sections:
const descriptor = buildQuorumPolicy(
'release-gate',
'two approvals',
{ threshold: { type: 'n_of_m', value: 2 } },
{
extensions: { 'x-com.acme.audit': { level: 2 } }, // object-valued, ^x-[a-z0-9]+(\.[a-z0-9-]+)+$
annotations: { _owner: 'release-eng' }, // any value, key starts with _ or $
},
);- Both are emitted after the standard sections, in insertion order.
- A key that collides with a standard section, has the wrong shape, or (for
an extension) has a non-object value throws
MacpSessionError. - Omitted or empty, the
rulesbytes are unchanged. A golden test pins them. - The runtime ignores annotations.
- The runtime admits an
x-key only if the embedder registered it. The stock server registers none, so an extension is refused atRegisterPolicy(ok: false,INVALID_POLICY_DEFINITION). policyRuleExtensions(await client.initialize())returns the keys a runtime advertises incapabilities.policyRegistry.ruleExtensions. It is[]on the stock server and on runtimes older than 0.9.0. It is advisory: admission decides.
When the runtime itself refuses a descriptor, registerPolicy() does not
throw: admission failures come back in-band as
{ ok: false, error: 'INVALID_POLICY_DEFINITION: ...' }, not as a gRPC
status. Check ok.
buildDecisionPolicy(policyId, description, rules, options?)
Creates a PolicyDescriptor targeting macp.mode.decision.v1. The only
builder with a runtime-selectable schema version
(RFC-MACP-0012 (Policy));
the other four modes remain schema version 1.
interface DecisionPolicyOptions {
schemaVersion?: 1 | 2 | 3; // default: 3 -- see "schemaVersion" below
}
schemaVersionselects the empty-tally semantics the runtime evaluates this policy under (RFC-MACP-0012 §8 item 4 — validated once, at admission, never re-validated on replay):
Version Empty decisive tally 1/2Fail-open: a binding algorithm (e.g. majority) passes on zero ballots unlesscommitment.requireVoteQuorumistrue(defaultfalse).3Fail-closed for every algorithm except 'none'(RFC-MACP-0012 §4.1's "vacuous participation floor", spec PR #99).The default is
3(issue #85, decided jointly withmacp-sdk-python's identicalschema_version: int = 3default — see that repo's issue #65): RFC-MACP-0012's authoring guidance is that new non-'none'policies SHOULD declareschemaVersion: 3, and the old default of2was fail-open on an empty tally for exactly the callers who opted into a binding algorithm — silently satisfying a vote that received zero ballots, which contradicts what asking for a binding algorithm means. v1/v2 semantics are preserved permanently for replay (RFC-MACP-0012 §8 item 3) and remain a supported choice, not a transitional one — pass{ schemaVersion: 1 }or{ schemaVersion: 2 }explicitly to keep fail-open empty-tally semantics. A stored policy always evaluates under its own declared version forever, so this default change carries no migration risk for existing registered policies. An out-of-range value (anything other than1,2, or3) throwsMacpSessionError.schemaVersionis descriptor metadata, not a rule — it never changes the serializedrulesJSON for the same rule input.TypeScript has no keyword arguments, so this is a fourth positional
optionsobject rather than Python'sschema_version=keyword — the two SDKs' call shapes diverge here by necessity, not oversight.
Parameters:
interface DecisionPolicyRulesInput {
voting?: {
algorithm?: 'none' | 'majority' | 'supermajority' | 'unanimous' | 'weighted' | 'plurality';
threshold?: number; // vote-share fraction, 0 < t <= 1, default: 0.5
quorum?: { type: 'count' | 'percentage'; value: number }; // percentage = integer 0–100, NOT 0–1
weights?: Record<string, number>; // participant_id → weight
};
objectionHandling?: {
criticalSeverityVetoes?: boolean; // default: false
vetoThreshold?: number; // default: 1
criticalObjectionAction?: 'deny' | 'finalize_decline' | 'hold'; // default: 'deny' (schema v2)
};
evaluation?: {
minimumConfidence?: number; // 0-1, default: 0
requiredBeforeVoting?: boolean; // default: false
};
commitment?: {
authority?: 'initiator_only' | 'any_participant' | 'designated_role'; // default: 'initiator_only'
designatedRoles?: string[]; // default: []; REQUIRED non-empty when authority is 'designated_role'
requireVoteQuorum?: boolean; // default: false
allowDeclineOverApproval?: boolean; // default: false (schema v2, decision-only)
};
}Schema-version-2 fields: criticalObjectionAction selects what happens when a
critical objection would block commitment (deny rejects it, finalize_decline
resolves the session as a negative outcome, hold leaves it open).
allowDeclineOverApproval: true lets a reject-majority resolve the session with
a committed negative outcome (outcome_positive = false) instead of denying
commitment.
buildDecisionPolicyenforces the canonicaldecision-rules.schema.jsonconstraints client-side, so a schema-invalid descriptor fails fast instead of round-tripping to a runtimeINVALID_POLICY_DEFINITION:
voting.algorithmmust be one of the six canonical values.voting.thresholdmust be0 < threshold <= 1.algorithm: 'majority'requiresthreshold >= 0.5— inclusive, deliberately, sincepolicy.std.majority(RFC-MACP-0012 §2.2) pinsthreshold: 0.5byte-identical on every runtime.algorithm: 'supermajority'requiresthreshold > 0.5— exclusive; the field's own default of0.5is a bare majority wearing the name, so an explicit threshold (e.g.0.67) is required.algorithm: 'weighted'requires a non-emptyweightsmap.weights, if supplied at all, is validated unconditionally — at every algorithm, not only'weighted': it must be non-empty, and every value must be> 0. A weight-0 participant is expressed by omission from the map, never by an explicit0— an explicit0throws. This is the weighted electorate rule: an omitted participant's vote is non-decisive (excluded from the ratio and the decisive tally) but still counts towardvoting.quorum's participation floor.
buildQuorumPolicy(policyId, description, rules)
Creates a PolicyDescriptor targeting macp.mode.quorum.v1 (RFC-MACP-0012 §4.2).
threshold.valueis the approval bar, not a participation quorum. Fortype: 'percentage'it is an integer 1–100 — the runtime computes the bar asceil(value / 100 × participants).75means "≥ 75% must approve"; a fractional value like0.75rounds to a ~1% bar and is therefore rejected (MacpSessionError). Usen_of_mfor absolute counts.
valuemust be a positive integer for everytype(exclusiveMinimum: 0in the canonicalquorum-rules.schema.json, unconditional — a zero approval bar would be trivially satisfied by any ballot set, sobuildQuorumPolicyrejects it client-side).'weighted'is reserved: it was removed from the canonical schema without ever having defined semantics (no weights vocabulary, no electorate rule) and is refused by the runtime; passing it throwsMacpSessionErrornaming the reservation.
interface QuorumPolicyRulesInput {
threshold?: {
type: 'n_of_m' | 'percentage'; // default: 'n_of_m'
value: number; // approval bar; default: 1
};
abstention?: {
countsTowardQuorum?: boolean; // default: false
interpretation?: 'neutral' | 'implicit_reject' | 'ignored'; // default: 'neutral'
};
commitment?: CommitmentRules;
}effectiveQuorumThreshold(rules, participantCount, requiredApprovals)
Returns the approval bar a Quorum session actually needs on runtime ≥ 0.9.0,
where a policy threshold replaces ApprovalRequest.requiredApprovals
(RFC-MACP-0011 §5 rule 6). A port of the runtime's QuorumThreshold::effective,
same results as macp-sdk-python's effective_quorum_threshold.
rules is a PolicyDescriptor, its JSON rules string/bytes, or the decoded
object. Returns requiredApprovals unchanged when no override applies (no
policy or threshold, value ≤ 0/NaN, non-numeric value, unparsable rules),
null when unsatisfiable (percentage with zero participants, unknown
type), otherwise max(1, ceil(...)) saturated at 2^32 − 1. Pass the result
to the QuorumProjection helpers as { effectiveThreshold } — see
Quorum mode.
buildProposalPolicy(policyId, description, rules)
Creates a PolicyDescriptor targeting macp.mode.proposal.v1 (RFC-MACP-0012 §4.3).
interface ProposalPolicyRulesInput {
acceptance?: { criterion?: 'all_parties' | 'counterparty' | 'initiator' }; // default: 'all_parties'
counterProposal?: { maxRounds?: number }; // default: 0 (unlimited)
rejection?: { terminalOnAnyReject?: boolean }; // default: false
commitment?: CommitmentRules;
}buildTaskPolicy(policyId, description, rules)
Creates a PolicyDescriptor targeting macp.mode.task.v1 (RFC-MACP-0012 §4.4).
interface TaskPolicyRulesInput {
assignment?: { allowReassignmentOnReject?: boolean }; // default: false
completion?: { requireOutput?: boolean }; // default: false
commitment?: CommitmentRules;
}buildHandoffPolicy(policyId, description, rules)
Creates a PolicyDescriptor targeting macp.mode.handoff.v1 (RFC-MACP-0012 §4.5).
interface HandoffPolicyRulesInput {
acceptance?: { implicitAcceptTimeoutMs?: number }; // default: 0 (no implicit accept)
commitment?: CommitmentRules;
}CommitmentRules (shared by all modes)
The exported input type is named CommitmentRules:
interface CommitmentRules {
authority?: 'initiator_only' | 'any_participant' | 'designated_role'; // default: 'initiator_only'
designatedRoles?: string[]; // default: []; REQUIRED non-empty when authority is 'designated_role'
requireVoteQuorum?: boolean; // default: false; emitted only by buildDecisionPolicy, dropped elsewhere
allowDeclineOverApproval?: boolean; // default: false; emitted only by buildDecisionPolicy (schema v2), dropped elsewhere
}
authority: 'designated_role'requires a non-emptydesignatedRoles. An authority rule that names no one is unsatisfiable — no sender could ever meet it — so every one of the fivebuild*Policyfunctions throwsMacpSessionErrorifdesignatedRolesis omitted or[]whileauthorityis'designated_role'. Enforced once, in the sharedserializeCommitmenthelper all five builders funnelcommitmentthrough, mirroring the canonical rule schemas' root-level conditional (minItems: 1ondesignated_roleswhenauthority == "designated_role", spec issue #116).designatedRolesis ignored, not validated, under the other two authorities — supplying it there is still serialized into the descriptor but has no effect on who may commit.
Client Methods
client.registerPolicy(descriptor, options?)
Registers a policy with the runtime. Returns { ok: boolean; error?: string }.
client.unregisterPolicy(policyId, options?)
Removes a registered policy. Returns { ok: boolean; error?: string }.
client.getPolicy(policyId, options?)
Retrieves a policy by ID. Returns the PolicyDescriptor.
client.listPolicies(mode?, options?)
Lists registered policies, optionally filtered by mode. Returns PolicyDescriptor[].
client.whatIfReplay(sessionId, candidate, options?)
Replays a session's accepted history under a candidate PolicyDescriptor and
reports how the outcome would differ (RFC-MACP-0012 §8.1). The call is read-only
and advisory: it doesn't touch the session, its history or the registry, and the
candidate is never registered. Returns a WhatIfReplayResult:
| Field | |
|---|---|
recordedPolicyId, candidatePolicyId | The session's bound policy, and the candidate's id |
recorded, candidate | Each replay's WhatIfOutcome: final state, the resolving Commitment's resolution bytes, and resolvedBy* |
firstDivergence | First history index (a decimal string; '0' is the SessionStart) the replays handled differently. Absent if they agree throughout |
denials | Every POLICY_DENIED the candidate produced; decode details with parseErrorDetails |
entries | Per history entry, the recorded and candidate WhatIfEntryOutcome (kind, state, errorCode, details) |
The RPC is optional. The client reads capabilities.policyRegistry.whatIfReplay
from initialize() (calling it once if needed; the result is cached and
readable as client.serverCapabilities). If the runtime doesn't advertise it,
the client throws MacpSdkError and sends nothing. The runtime's refusals arrive as
MacpTransportError with macpErrorCode: SESSION_NOT_FOUND/FORBIDDEN
(authorized like getSession), INVALID_POLICY_DEFINITION (the candidate fails
admission, or its mode is neither the session's nor *), HISTORY_UNAVAILABLE
(history compacted), INTERNAL_ERROR.
const candidate = buildDecisionPolicy('policy.strict', 'unanimous', { voting: { algorithm: 'unanimous' } });
const result = await client.whatIfReplay(sessionId, candidate);
if (result.firstDivergence !== undefined) {
console.log(`diverges at entry ${result.firstDivergence}: ${result.recorded?.state} → ${result.candidate?.state}`);
}PolicyWatcher
import { PolicyWatcher } from 'macp-sdk-typescript';
const watcher = new PolicyWatcher(client, { auth }); // auth required (or client.auth), runtime ≥ 0.12.0
// Async generator
for await (const change of watcher.changes(abortSignal?)) {
// change.descriptors: PolicyDescriptor[]
// change.observedAtUnixMs: number
}
// Callback-based
await watcher.watch((change) => { ... });
// One-shot
const change = await watcher.nextChange();Constants
DEFAULT_POLICY_VERSION='policy.default'