Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions runners/merit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# MERIT AutoMem adapter

This adapter connects the official [MERIT benchmark](https://github.com/smshweta/merit-bench)
to a local AutoMem endpoint. It is experimental infrastructure, not an official
AutoMem benchmark claim.

## Pinned upstream artifact

- Repository: `https://github.com/smshweta/merit-bench`
- Revision: `293933d96b1d1849e1f20d1bb324def5de9ed33f`
- License: MIT
- Released artifacts: benchmark harness, deterministic mock mode, results, and
full episode traces under the upstream `runs/` directory.

Clone the official release and verify the pin before running:

```bash
git clone https://github.com/smshweta/merit-bench.git ../merit-bench
git -C ../merit-bench checkout 293933d96b1d1849e1f20d1bb324def5de9ed33f
python3 -m unittest runners.test_merit_automem_adapter -v
python3 runners/merit/run_merit.py --merit-root ../merit-bench \
--variant plain --maturation both -- --model mock --arcs 2 --episodes 5
```

The adapter assigns one generated `merit-run-*` tag to every memory and restricts
recall to that tag. It has two write policies:

| Variant | Write policy |
| --- | --- |
| `plain` | Stores every deterministic MERIT fact observation without lifecycle mutation. |
| `supersede-on-write` | On a changed `(entity, attribute)`, stores the new observation with `supersedes_memory_id=<old>` and `INVALIDATED_BY` (old → new). |

It records server-reported usage separately from request/response token estimates.
Unknown dollar cost remains `null`; it is never silently reported as zero.

## Maturation reporting

`--maturation both` writes a cold-ready / matured-blocked report. This is
intentional: the documented mature profile needs a narrative-clock
`/consolidate reference_time` API, which AutoMem does not currently expose. The
adapter will not relabel a cold run as mature. Once that API exists, run cold and
matured separately and publish both according to
[`docs/eval/maturation.md`](../../docs/eval/maturation.md).

## Scoring boundary

No AutoMem score is included here. MERIT's published figures are paper baselines,
not AutoMem results. A future run must preserve the official task checker, model,
seed, trace revision, cost record, isolated run tag, and cold/matured labels.
5 changes: 5 additions & 0 deletions runners/merit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""AutoMem integration helpers for the official MERIT benchmark."""

from .automem_adapter import AutoMemMemory, MeteringSnapshot, is_local_endpoint

__all__ = ["AutoMemMemory", "MeteringSnapshot", "is_local_endpoint"]
240 changes: 240 additions & 0 deletions runners/merit/automem_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
"""MERIT ``MemoryBase``-compatible AutoMem adapter.

This module intentionally does not vendor MERIT. It is used with the official
``smshweta/merit-bench`` checkout pinned by :mod:`run_merit`. It implements two
write policies over the same isolated local AutoMem namespace:

* ``plain`` retains every extracted fact record; and
* ``supersede-on-write`` stores an explicit replacement with
``supersedes_memory_id`` when the same MERIT fact key changes.

The fact-key patterns mirror the public MIT-licensed MERIT ``StructuredFacts``
condition at revision 293933d96b1d1849e1f20d1bb324def5de9ed33f. They are kept
small and explicit so update semantics are measurable rather than inferred from
a model response.
"""
from __future__ import annotations

import json
import math
import re
import secrets
import time
from dataclasses import dataclass, field
from typing import Any, Callable, Literal
from urllib.parse import urlencode, urlparse
from urllib.request import Request, urlopen


Variant = Literal["plain", "supersede-on-write"]
HttpRequest = Callable[[str, str, dict[str, Any] | None], dict[str, Any]]


def is_local_endpoint(endpoint: str) -> bool:
"""Return whether an endpoint is an explicit loopback AutoMem endpoint."""
host = (urlparse(endpoint).hostname or "").lower()
return host in {"localhost", "127.0.0.1", "::1"}


@dataclass
class MeteringSnapshot:
"""Memory-operation metering, separating measured from estimated values.

AutoMem's HTTP API does not promise model-side usage in every response.
Therefore request/response character estimates are disclosed separately and
dollars remain ``None`` until the caller supplies a price or the server
returns a measured ``cost_usd`` field. This prevents an unknown cost from
silently becoming a $0 result.
"""

calls: int = 0
estimated_input_tokens: int = 0
estimated_output_tokens: int = 0
measured_input_tokens: int | None = None
measured_output_tokens: int | None = None
measured_embedding_tokens: int | None = None
measured_cost_usd: float | None = None

def record(self, request_body: dict[str, Any] | None, response: dict[str, Any]) -> None:
self.calls += 1
request_text = json.dumps(request_body or {}, sort_keys=True)
response_text = json.dumps(response, sort_keys=True)
self.estimated_input_tokens += math.ceil(len(request_text) / 4)
self.estimated_output_tokens += math.ceil(len(response_text) / 4)

usage = response.get("usage") or response.get("metering") or {}
if not isinstance(usage, dict):
return
self.measured_input_tokens = _add_optional(
self.measured_input_tokens, usage.get("prompt_tokens", usage.get("input_tokens"))
)
self.measured_output_tokens = _add_optional(
self.measured_output_tokens, usage.get("completion_tokens", usage.get("output_tokens"))
)
self.measured_embedding_tokens = _add_optional(
self.measured_embedding_tokens, usage.get("embedding_tokens")
)
cost = usage.get("cost_usd", response.get("cost_usd"))
if isinstance(cost, (int, float)):
self.measured_cost_usd = (self.measured_cost_usd or 0.0) + float(cost)

def as_dict(self) -> dict[str, Any]:
return {
"calls": self.calls,
"estimated_input_tokens": self.estimated_input_tokens,
"estimated_output_tokens": self.estimated_output_tokens,
"measured_input_tokens": self.measured_input_tokens,
"measured_output_tokens": self.measured_output_tokens,
"measured_embedding_tokens": self.measured_embedding_tokens,
"measured_cost_usd": self.measured_cost_usd,
}


def _add_optional(previous: int | None, value: Any) -> int | None:
return (previous or 0) + int(value) if isinstance(value, (int, float)) else previous


@dataclass(frozen=True)
class Fact:
key: str
value: str


_FACT_PATTERNS: tuple[tuple[re.Pattern[str], str, int | str, int], ...] = (
# D1 customer support
(re.compile(r'"customer_id":\s*"([^"]+)".*?"address":\s*"([^"]+)"'), "address", 1, 2),
(re.compile(r'"order_id":\s*"([^"]+)".*?"refunded_cents":\s*(\d+)'), "refunded_cents", 1, 2),
(re.compile(r'agreed amount of (\d+) cents for order (ORD-\d+)'), "agreed_refund_cents", 2, 1),
# D2 IT operations
(re.compile(r'planned fix: (\w+=\d+) for ([a-z-]+-api)'), "planned_fix", 2, 1),
(re.compile(r'rollback target (v[\d.]+) for ([a-z-]+-api)'), "rollback_target", 2, 1),
(re.compile(r'"service":\s*"([^"]+)".*?"version":\s*"(v[\d.]+)"'), "version", 1, 2),
# D3 personal assistant
(re.compile(r'usual room is (Room \d+[A-Z])'), "usual_room", "user", 1),
(re.compile(r'usual meeting time is (\d\d:\d\d)'), "usual_time", "user", 1),
(re.compile(r'dinner with (\w+) at (.+? on \w+ at \d\d:\d\d)'), "dinner", 1, 2),
)


def extract_merit_facts(transcript: str) -> list[Fact]:
"""Extract deterministic update keys used by the official MERIT C4 arm."""
facts: list[Fact] = []
seen: set[tuple[str, str]] = set()
for pattern, attribute, entity_group, value_group in _FACT_PATTERNS:
for match in pattern.finditer(transcript):
entity = entity_group if isinstance(entity_group, str) else match.group(entity_group)
value = match.group(value_group)
key = f"{entity}.{attribute}"
item = (key, value)
if item not in seen:
seen.add(item)
facts.append(Fact(*item))
return facts


@dataclass
class AutoMemMemory:
"""Duck-typed implementation of MERIT's ``MemoryBase`` interface."""

endpoint: str = "http://localhost:8001"
token: str = "test-token"
variant: Variant = "plain"
run_tag: str | None = None
recall_limit: int = 8
allow_remote: bool = False
request: HttpRequest | None = None
name: str = field(init=False)
meter: dict[str, int] = field(init=False)
metering: MeteringSnapshot = field(default_factory=MeteringSnapshot, init=False)
_latest_by_key: dict[str, tuple[str, str]] = field(default_factory=dict, init=False)

def __post_init__(self) -> None:
if self.variant not in {"plain", "supersede-on-write"}:
raise ValueError(f"unknown MERIT AutoMem variant: {self.variant}")
if not self.allow_remote and not is_local_endpoint(self.endpoint):
raise ValueError("MERIT adapter refuses non-local endpoint; pass allow_remote=True explicitly")
self.endpoint = self.endpoint.rstrip("/")
self.run_tag = self.run_tag or f"merit-run-{int(time.time())}-{secrets.token_hex(4)}"
self.name = f"automem_{self.variant.replace('-', '_')}"
# The official runner expects numeric counters. These only contain
# server-reported usage; estimates are available in ``metering``.
self.meter = {"prompt_tokens": 0, "completion_tokens": 0, "embedding_tokens": 0}

def write(self, episode_id: str, transcript: str) -> None:
facts = extract_merit_facts(transcript) or [Fact(f"episode.{episode_id}", transcript)]
for fact in facts:
payload: dict[str, Any] = {
"content": f"MERIT {fact.key} = {fact.value} (episode {episode_id})",
"type": "Context",
"tags": [self.run_tag, "merit", "merit-fact", fact.key],
"metadata": {
"merit_episode_id": episode_id,
"merit_fact_key": fact.key,
"merit_fact_value": fact.value,
"merit_variant": self.variant,
},
}
previous = self._latest_by_key.get(fact.key)
if self.variant == "supersede-on-write" and previous and previous[1] != fact.value:
payload["supersedes_memory_id"] = previous[0]
payload["supersede_relation"] = "INVALIDATED_BY"
Comment on lines +179 to +180

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Create supersession through the association endpoint

For supersede-on-write, these fields do not create an AutoMem graph edge: this repository's established API authors INVALIDATED_BY with a separate POST /associate containing memory1_id, memory2_id, and type (runners/run_current_state_recall_eval.py:410-421). Depending on request validation, the first changed fact will either make /memory fail or store the new memory while leaving the old one current, so the subsequent current_only=true recall still exposes stale values and invalidates the core updated-fact experiment. Store the new memory first, then explicitly associate the old ID to the returned new ID.

Useful? React with 👍 / 👎.

response = self._call("POST", "/memory", payload)
memory_id = response.get("memory_id") or response.get("id")
if not isinstance(memory_id, str) or not memory_id:
raise RuntimeError("AutoMem /memory response omitted memory_id")
self._latest_by_key[fact.key] = (memory_id, fact.value)

def read(self, current_context: str, budget_chars: int = 4000) -> str:
query = urlencode(
[("query", current_context), ("tags", self.run_tag or ""),
("limit", str(self.recall_limit)), ("current_only", "true")]
)
response = self._call("GET", f"/recall?{query}", None)
results = response.get("results", [])
if not isinstance(results, list):
raise RuntimeError("AutoMem /recall response has non-list results")
blocks: list[str] = []
used = 0
for item in results:
if not isinstance(item, dict):
continue
memory = item.get("memory", item)
content = memory.get("content") if isinstance(memory, dict) else None
if not isinstance(content, str) or used + len(content) > budget_chars:
continue
blocks.append(content)
used += len(content)
return "\n---\n".join(blocks)

def metering_record(self) -> dict[str, Any]:
"""Return an auditable record suitable for MERIT result metadata."""
return {
"run_tag": self.run_tag,
"variant": self.variant,
"server_usage_only": self.meter.copy(),
"http": self.metering.as_dict(),
}

def _call(self, method: str, path: str, body: dict[str, Any] | None) -> dict[str, Any]:
response = self.request(method, path, body) if self.request else self._http(method, path, body)
self.metering.record(body, response)
usage = response.get("usage") or response.get("metering") or {}
if isinstance(usage, dict):
for target, source in (("prompt_tokens", "prompt_tokens"),
("completion_tokens", "completion_tokens"),
("embedding_tokens", "embedding_tokens")):
if isinstance(usage.get(source), (int, float)):
self.meter[target] += int(usage[source])
return response

def _http(self, method: str, path: str, body: dict[str, Any] | None) -> dict[str, Any]:
data = json.dumps(body).encode() if body is not None else None
request = Request(
f"{self.endpoint}{path}", data=data, method=method,
headers={"X-Api-Key": self.token, "Content-Type": "application/json"},
)
with urlopen(request, timeout=30) as response: # nosec B310: endpoint guarded above
parsed = json.loads(response.read())
if not isinstance(parsed, dict):
raise RuntimeError(f"AutoMem {method} {path} returned non-object JSON")
return parsed
101 changes: 101 additions & 0 deletions runners/merit/run_merit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""Run the public MERIT harness with an isolated AutoMem memory condition.

Usage (after cloning the official source at its pinned revision)::

python3 runners/merit/run_merit.py --merit-root ../merit-bench \
--variant supersede-on-write --maturation both -- --model mock --arcs 2

``matured`` is reported but intentionally blocked until AutoMem exposes the
narrative-clock ``reference_time`` consolidation contract required by
``docs/eval/maturation.md``. The script never labels a cold execution mature.
"""
from __future__ import annotations

import argparse
import json
import subprocess
import sys
from pathlib import Path

from automem_adapter import AutoMemMemory

MERIT_REVISION = "293933d96b1d1849e1f20d1bb324def5de9ed33f"


def _revision(root: Path) -> str:
return subprocess.check_output(["git", "-C", str(root), "rev-parse", "HEAD"], text=True).strip()


def _maturation_report(requested: str) -> dict[str, object]:
report: dict[str, object] = {"cold": {"status": "ready", "profile": "cold"}}
if requested in {"matured", "both"}:
report["matured"] = {
"status": "blocked",
"profile": "matured",
"reason": "AutoMem lacks the required narrative-clock /consolidate reference_time parameter; see docs/eval/maturation.md.",
}
return report


def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--merit-root", type=Path, required=True)
parser.add_argument("--endpoint", default="http://localhost:8001")
parser.add_argument("--token", default="test-token")
parser.add_argument("--variant", choices=["plain", "supersede-on-write"], required=True)
parser.add_argument("--maturation", choices=["cold", "matured", "both"], default="both")
parser.add_argument("--allow-remote", action="store_true")
parser.add_argument("--out", type=Path, default=Path("data/results/merit"))
parser.add_argument("merit_args", nargs=argparse.REMAINDER,
help="arguments passed to official scripts/run_pilot.py after --")
args = parser.parse_args()

root = args.merit_root.resolve()
if not (root / "merit" / "memory.py").is_file():
raise SystemExit(f"not an official MERIT checkout: {root}")
actual = _revision(root)
if actual != MERIT_REVISION:
raise SystemExit(f"MERIT revision mismatch: expected {MERIT_REVISION}, got {actual}")

report = {
"upstream": "https://github.com/smshweta/merit-bench",
"revision": actual,
"license": "MIT",
"variant": args.variant,
"maturation": _maturation_report(args.maturation),
"not_scored_reason": "This command wires the official harness but has not executed an approved model/judge run.",
}
args.out.mkdir(parents=True, exist_ok=True)
report_path = args.out / f"{args.variant}-run-plan.json"
report_path.write_text(json.dumps(report, indent=2) + "\n")

if args.maturation == "matured":
raise SystemExit(f"matured execution blocked; wrote honest status to {report_path}")

# The public runner imports its condition map at module-load time. Patch it
# only in this process and preserve the rest of its deterministic pipeline.
sys.path.insert(0, str(root))
from merit import memory as merit_memory
from scripts import run_pilot

condition_key = "C6"
merit_memory.CONDITIONS[condition_key] = lambda: AutoMemMemory(
endpoint=args.endpoint, token=args.token, variant=args.variant,
allow_remote=args.allow_remote,
)
run_pilot.CONDITIONS[condition_key] = merit_memory.CONDITIONS[condition_key]
forwarded = list(args.merit_args)
if forwarded[:1] == ["--"]:
forwarded = forwarded[1:]
forwarded.extend(["--conditions", condition_key, "--out", str(args.out / "cold")])
old_argv = sys.argv
try:
sys.argv = ["run_pilot.py", *forwarded]
run_pilot.main()
finally:
sys.argv = old_argv
print(f"MERIT integration plan: {report_path}")


if __name__ == "__main__":
main()
Loading