0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
131 lines
5.2 KiB
Python
131 lines
5.2 KiB
Python
"""``MemoryDecision``: canonical "should we inject memory context?" gate.
|
|
|
|
Input-side analog of :class:`CompressionDecision`. Pre-this-PR, the
|
|
6 memory-injection sites across four handler files computed the gate
|
|
inline with subtle drift — most notably, 3 sites (Anthropic chat,
|
|
OpenAI chat, Gemini) never gated on ``x-headroom-bypass``, so
|
|
memory injection silently mutated requests when the user explicitly
|
|
asked for byte-faithful passthrough.
|
|
|
|
This is **decision-only**. It gates whether the request bytes get
|
|
mutated (memory context appended to user-tail). It does NOT gate
|
|
background memory STORAGE — traffic-learner runs on a separate path
|
|
and continues accumulating signal even under bypass. The user's
|
|
"don't touch my bytes" signal is for the INJECTION; the user's
|
|
working memory should still grow.
|
|
|
|
Precedence (highest first):
|
|
|
|
1. ``bypass_header`` — user's explicit "do not touch my bytes"
|
|
2. ``no_handler`` — no memory backend configured
|
|
3. ``no_user_id`` — per-request user_id missing
|
|
4. ``mode_disabled`` — operator HEADROOM_MEMORY_INJECTION_MODE=disabled
|
|
5. ``mode_tool`` — operator HEADROOM_MEMORY_INJECTION_MODE=tool
|
|
(auto-inject off; the agent calls memory tools explicitly)
|
|
6. otherwise → ``inject=True``
|
|
|
|
This module exposes one value type + one factory + one helper. Pure
|
|
function; same Rust-port shape as :class:`CompressionDecision`.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Any
|
|
|
|
from headroom.proxy.memory_decision_policy import (
|
|
apply_memory_skip_reason,
|
|
decide_memory_injection,
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class MemoryDecision:
|
|
"""Immutable, value-equal snapshot of the memory-injection decision.
|
|
|
|
Construction policy: use :meth:`decide`. Direct construction is
|
|
legal but unusual — tests use it; handlers always go through
|
|
``decide``. The constituent observability booleans
|
|
(``bypass_header_set`` etc.) MUST match the inputs ``decide`` saw;
|
|
the factory enforces that invariant, and the dataclass being
|
|
frozen means downstream code can't violate it.
|
|
"""
|
|
|
|
inject: bool
|
|
# When ``inject`` is False, this is the canonical reason surfaced
|
|
# in logs and in RequestOutcome.tags["memory_skip_reason"] so the
|
|
# dashboard can slice memory-skip traffic by cause. One of:
|
|
# * "bypass_header" — user set x-headroom-bypass/x-headroom-mode
|
|
# * "no_handler" — no memory backend configured on the proxy
|
|
# * "no_user_id" — per-request user_id missing
|
|
# * "mode_disabled" — operator HEADROOM_MEMORY_INJECTION_MODE=disabled
|
|
# * "mode_tool" — operator HEADROOM_MEMORY_INJECTION_MODE=tool
|
|
# When ``inject`` is True, this is None.
|
|
skip_reason: str | None
|
|
|
|
# Observability: every constituent boolean exposed so debug tools
|
|
# answer "what did the decision see?" without re-running.
|
|
bypass_header_set: bool
|
|
memory_handler_present: bool
|
|
memory_user_id_present: bool
|
|
mode_name: str
|
|
|
|
@classmethod
|
|
def decide(
|
|
cls,
|
|
*,
|
|
headers: Any,
|
|
memory_handler: Any | None,
|
|
memory_user_id: str | None,
|
|
mode_name: str,
|
|
) -> MemoryDecision:
|
|
"""Compute the canonical memory-injection decision.
|
|
|
|
Parameters
|
|
----------
|
|
headers
|
|
Inbound request headers. Accepts any object with a
|
|
``.get(key)`` method (dict, starlette Headers, mapping).
|
|
Bypass detected via ``_headroom_bypass_enabled``.
|
|
memory_handler
|
|
The proxy's memory handler instance, or ``None`` if no
|
|
memory backend is configured. Presence only is checked —
|
|
no methods called.
|
|
memory_user_id
|
|
Per-request user_id from ``x-headroom-user-id`` header (or
|
|
env default). ``None`` or empty string treated as missing.
|
|
mode_name
|
|
One of ``"auto_tail"`` / ``"tool"`` / ``"disabled"``.
|
|
Comes from ``get_memory_injection_mode()`` which reads
|
|
``HEADROOM_MEMORY_INJECTION_MODE``.
|
|
"""
|
|
decision = decide_memory_injection(
|
|
headers=headers,
|
|
memory_handler_present=memory_handler is not None,
|
|
memory_user_id_present=bool(memory_user_id),
|
|
mode_name=mode_name,
|
|
)
|
|
|
|
return cls(
|
|
inject=decision.inject,
|
|
skip_reason=decision.skip_reason,
|
|
bypass_header_set=decision.bypass_header_set,
|
|
memory_handler_present=decision.memory_handler_present,
|
|
memory_user_id_present=decision.memory_user_id_present,
|
|
mode_name=decision.mode_name,
|
|
)
|
|
|
|
def apply_to_tags(self, tags: dict[str, str]) -> None:
|
|
"""Stamp the skip reason into a tags dict for dashboard slicing.
|
|
|
|
Mutates ``tags`` in place. No-op when ``inject=True`` —
|
|
absence vs presence of ``memory_skip_reason`` is the signal.
|
|
|
|
Mirror of :meth:`CompressionDecision.apply_to_tags`. Handlers
|
|
call this immediately after ``decide()`` so the resulting
|
|
``RequestOutcome.tags`` carries memory observability via the
|
|
same path the funnel already uses for ``client`` and
|
|
``passthrough_reason``.
|
|
"""
|
|
apply_memory_skip_reason(tags, self.skip_reason)
|