Purpose
-------
Centralised policy loader for the ``oct-mcp`` server (Phase 5C, M-C7).
Loads policy override JSON from a local filesystem path or an HTTPS URL
at server startup. The loaded overrides are applied on top of the base
:class:`~oct.mcp.config.McpConfig` before the policy engine is
instantiated, enabling enterprise deployments to distribute a shared
security policy from a central location.
Responsibilities
----------------
- Read the policy document named by ``policy_source``, from a local path
or an HTTPS URL, within the 64 KB cap.
- Reject HTTP URLs, oversized sources and malformed JSON.
- Filter the parsed document to :data:`_ALLOWED_OVERRIDE_KEYS`, dropping
unknown keys and type mismatches.
- Apply the surviving overrides on top of the base ``McpConfig``.
- Fall back to built-in defaults on any failure, without raising.
Configuration
-------------
Set ``policy_source`` in ``~/.oct-mcp/config.json``::
{
"policy_source": "https://internal.company.com/oct-policy.json"
}
Or provide a local path::
{
"policy_source": "/etc/oct/policy.json"
}
Policy JSON schema (all fields optional)::
{
"profile": "strict",
"rate_limit_per_minute": 20,
"timeout_default_seconds": 30,
"max_output_bytes": 524288,
"dry_run_default_for_writes": true
}
Only the keys listed in :data:`_ALLOWED_OVERRIDE_KEYS` are honoured.
Unknown keys are silently dropped. Type mismatches are silently ignored.
Security
--------
- HTTPS URLs only. HTTP URLs are rejected to prevent cleartext policy
transmission.
- File size capped at 64 KB (same as ``config.json``).
- Schema is an allowlist - no arbitrary key injection possible.
- Any failure (network error, parse error, schema violation) falls back
to built-in defaults silently without crashing the server.
Diagnostics
-----------
Domain: MCP
Levels:
L1 - errors: load failures, schema violations
L2 - lifecycle: policy loaded, overrides applied
L3 - (none): resolved field detail is reported at L4
L4 - deep trace: resolved field values
Contracts
---------
- :func:`load_policy_source` never raises - returns ``{}`` on any error.
- :func:`apply_policy_overrides` never raises - returns *config* unchanged
on any error.
- No ``http://`` URLs are accepted - HTTPS only.
Dependencies
------------
- oct.core.diagnostics (_dbg structured logger)
- oct.mcp.config (lazy; VALID_MCP_PROFILES, to accept only a known profile override)