-
Notifications
You must be signed in to change notification settings - Fork 0
fix(audit): make the live Phoenix-MCP calibration loop genuinely work #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| #!/usr/bin/env python3 | ||
| """Seed the ``glasshat-calibration`` Phoenix dataset with the spike-D YELLOW | ||
| optimism over-confidence prior, so the live ``PhoenixMcpConsultant`` (read over | ||
| MCP ``get-dataset-examples``) reproduces the deterministic table prior. | ||
|
|
||
| Each example is ``input={hat,criterion,bucket}`` + ``output={delta}`` — exactly | ||
| the shape ``PhoenixMcpConsultant._parse_examples`` reads. For every preset | ||
| criterion and evidence bucket we emit ``n`` examples at the measured mean_delta | ||
| (low 1.45/n7, mid 0.80/n10, high 0.31/n16), so the consultant's mean == the table | ||
| prior's mean and ``n`` matches. | ||
|
|
||
| Run: | ||
| PHOENIX_URL=https://glasshat-phoenix-...run.app \ | ||
| uv run --extra phoenix python scripts/seed_phoenix_calibration.py | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
|
|
||
| # spike-D held-out YELLOW prior (mirror of engine._YELLOW_DELTA_BY_BUCKET). | ||
| _YELLOW_DELTA_BY_BUCKET: dict[str, tuple[float, int]] = { | ||
| "low": (1.45, 7), | ||
| "mid": (0.80, 10), | ||
| "high": (0.31, 16), | ||
| } | ||
| _DATASET = "glasshat-calibration" | ||
|
|
||
|
|
||
| def main() -> None: | ||
| import pathlib | ||
| import sys | ||
|
|
||
| # uv's editable install of the PEP420 glasshat namespace isn't always picked up | ||
| # under `--extra phoenix`; add every workspace src dir so the namespace merges. | ||
| _root = pathlib.Path(__file__).resolve().parents[1] | ||
| for _p in ( | ||
| "packages/rubric/src", | ||
| "packages/shared/src", | ||
| "agents/src", | ||
| "services/code-grader/src", | ||
| "services/ingest/src", | ||
| "services/pipeline-orchestrator/src", | ||
| ): | ||
| _abs = str(_root / _p) | ||
| if _abs not in sys.path: | ||
| sys.path.insert(0, _abs) | ||
|
|
||
| from glasshat.rubric.presets import list_presets, load_preset | ||
| from phoenix.client import Client | ||
|
|
||
| base_url = os.environ.get("PHOENIX_URL") or os.environ.get("PHOENIX_COLLECTOR_ENDPOINT") | ||
| if not base_url: | ||
| raise SystemExit("PHOENIX_URL (or PHOENIX_COLLECTOR_ENDPOINT) must be set") | ||
| api_key = os.environ.get("PHOENIX_API_KEY") or None | ||
|
|
||
| inputs: list[dict[str, object]] = [] | ||
| outputs: list[dict[str, object]] = [] | ||
| seen: set[str] = set() | ||
| for preset_id in list_presets(): | ||
| for crit in load_preset(preset_id).criteria: | ||
| if crit.id in seen: # one set of anchors per criterion id (n stays exact) | ||
| continue | ||
| seen.add(crit.id) | ||
| for bucket, (mean_delta, n) in _YELLOW_DELTA_BY_BUCKET.items(): | ||
| for _ in range(n): | ||
| inputs.append({"hat": "yellow", "criterion": crit.id, "bucket": bucket}) | ||
| outputs.append({"delta": mean_delta}) | ||
|
|
||
| client = Client(base_url=base_url, api_key=api_key) | ||
|
|
||
| # Idempotent: drop any existing dataset of this name first so re-running | ||
| # produces a single clean dataset (the phoenix client exposes no delete, so | ||
| # use the REST endpoint directly). Re-seeding is the normal reset path. | ||
| import httpx | ||
|
|
||
| headers = {"authorization": f"Bearer {api_key}"} if api_key else {} | ||
| with httpx.Client(base_url=base_url, headers=headers, timeout=20.0) as http: | ||
| existing = http.get("/v1/datasets").json().get("data", []) | ||
| for d in existing: | ||
| if d.get("name") == _DATASET: | ||
| http.delete(f"/v1/datasets/{d['id']}") | ||
| print(f" removed existing dataset id={d['id']}") | ||
|
|
||
| ds = client.datasets.create_dataset( | ||
| name=_DATASET, | ||
| inputs=inputs, | ||
| outputs=outputs, | ||
| dataset_description=( | ||
| "spike-D YELLOW optimism over-confidence prior — delta by " | ||
| "(hat, criterion, evidence-bucket). Read live by PhoenixMcpConsultant." | ||
| ), | ||
| ) | ||
| print(f"created dataset '{_DATASET}' with {len(inputs)} examples") | ||
| print(f" criteria seeded: {sorted(seen)}") | ||
| print(f" dataset: {ds}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The seeding script is currently not idempotent. If you run it more than once,
client.datasets.create_datasetwill raise an HTTP conflict/duplicate error because a dataset with the nameglasshat-calibrationalready exists.To make the script safely reusable and idempotent (e.g., for local resets or CI/CD pipelines), we should check for and delete any existing dataset with the same name before creating the new one.