Skip to content
Search Engineering

Carry Policy Context

Separate trace correlation from verified authority, validate every protected receiver, and reauthorize saved state when a trajectory resumes.

Outcome

This chapter adds a small authority context, a receiver-side validation function, and resume logic that reauthorizes checkpointed evidence under current policy. The model never constructs or edits the authority object.

Keep two context channels

Send trace and span identifiers through the observability channel. Send authority through a mechanism the receiver can verify, such as a session, workload identity, scoped token, or signed request. Ordinary trace baggage can help route diagnostics, but it must not prove principal, tenant, scope, or approval.

The runnable AuthorityContext names the fields a receiver needs for this fixture:

context = AuthorityContext(
    principal_id="family",
    actor_id="agent-worker",
    audience="availability-api",
    scopes=frozenset({"film:read"}),
    policy_version="household-v17",
    expires_at=1_800_000_000,
)

Production credentials may encode fewer fields and resolve the rest through current policy data. The important property is that the receiver verifies their source and current meaning.

Decide at the receiver

validate_receiver() checks target audience before payload processing, then expiration and required scope. Extend it with issuer, signature, revocation, tenant, resource, and purpose checks appropriate to your identity system.

An upstream retrieval permit does not replace this decision. It can be referenced in the new event to preserve causality, but the cache, memory store, connector, or write service owns its boundary.

Reauthorize checkpointed evidence

A checkpoint preserves completed work. It cannot preserve credential validity or force an older policy to remain active. Resume should resolve current authority and run every protected saved artifact through current policy:

# The course excerpt is generated from region:resume_with_policy.

The fixture returns resumed, narrowed, or denied. Quarantined IDs remain available for audit and recomputation but must not reenter model context. Plans and summaries derived from removed evidence also need recomputation.

Apply the same rule to memory and cache reads. Store principal or owner reference, authorization class, purpose, policy version, source evidence references, and retention metadata beside each payload. Reject a scope mismatch before the payload is returned.

Record decision events

At each protected step, record receiver, principal and actor references, audience, requested scope, outcome, reason, policy version, and time. Never log bearer tokens. Put raw claims and protected payloads behind separate access, sampling, and retention controls when incident investigation genuinely requires them.

Run it

.venv/bin/python code/chapters/chapter_09.py
cd code/chapters && ../../.venv/bin/python -m unittest test_chapter_09.py

The next chapter applies comparable contracts to external records, identity, live reads, and effects.

Success criteria

  • A wrong target audience is rejected before payload processing.
  • Expired authority is rejected at the receiver.
  • Resumed evidence is narrowed or denied after a policy change, and quarantined IDs never reenter model context.
  • Every protected step records a decision event with references and a policy version, and no bearer tokens.

Further reading