Repository navigation
Separate managed diagnosis and knowledge delivery in Harness #724
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
+581
−179
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d7db404
Separate bottleneck diagnosis and managed knowledge delivery
Dong1017 f96f6f5
Bind prepared service knowledge to its dispatched request
Dong1017 648d3aa
Integrate the merged Manager search layout
Dong1017 b0ed318
Validate ordinary knowledge before retaining its receipt
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| """Validate measured bottleneck evidence supplied to a managed session.""" | ||
|
|
||
| from pathlib import Path | ||
|
|
||
|
|
||
| def diagnosis_draft(bottleneck: str, evidence: Path) -> dict: | ||
| """Read the caller's measurement summary before Manager records a diagnosis.""" | ||
| bottleneck = bottleneck.strip() | ||
| if not bottleneck or len(bottleneck) > 4096: | ||
| raise ValueError("Bottleneck must be a nonempty description of at most 4096 characters") | ||
| if evidence.is_symlink() or not evidence.is_file(): | ||
| raise ValueError("Diagnosis evidence must be a regular UTF-8 measurement summary") | ||
| with evidence.open("rb") as stream: | ||
| raw = stream.read(65537) | ||
| if len(raw) > 65536: | ||
| raise ValueError("Diagnosis evidence exceeds 64 KiB; provide a measurement summary") | ||
| summary = raw.decode("utf-8").strip() | ||
| if not summary or "\x00" in summary: | ||
| raise ValueError("Diagnosis evidence must contain a nonempty UTF-8 measurement summary") | ||
| return {"bottleneck": bottleneck, "measurement_summary": summary} |
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,99 @@ | ||
| """Check the knowledge delivered for a managed request against its frozen policy.""" | ||
|
|
||
| import hashlib | ||
|
|
||
| from ..records import knowledge_capture | ||
|
|
||
|
|
||
| def require_ranking_policy(recorded, effective): | ||
| if (not isinstance(recorded, dict) or not isinstance(effective, dict) | ||
| or type(recorded.get("version")) is not int | ||
| or not isinstance(recorded.get("weights"), dict) | ||
| or any(type(value) not in (int, float) for value in recorded["weights"].values()) | ||
| or recorded != effective): | ||
| raise ValueError("Frozen ranking policy changed or is incomplete, use its original runtime") | ||
|
|
||
|
|
||
| def resolve_frozen_ranking(plan, ranking_policy_for, *, read_only=False): | ||
| if read_only and "ranking_policy" not in plan: | ||
| return None | ||
| recorded = plan.get("ranking_policy") | ||
| if not isinstance(recorded, dict) or not isinstance(recorded.get("id"), str): | ||
| raise ValueError("Missing frozen ranking policy, use the original runtime to resume") | ||
| if ranking_policy_for is None: | ||
| raise ValueError("A resolver for the frozen ranking policy is required") | ||
| policy = ranking_policy_for(recorded["id"]) | ||
| effective = (policy.configuration() if plan.get("evaluation_mode") == "service_reuse" | ||
| else policy.receipt()) | ||
| require_ranking_policy(recorded, effective) | ||
| return policy | ||
|
|
||
|
|
||
| def check_context_ranking(plan, context): | ||
| if "ranking_policy" in plan: | ||
| request = context.get("request") if isinstance(context, dict) else None | ||
| recorded = request.get("ranking_policy") if isinstance(request, dict) else None | ||
| require_ranking_policy(recorded, plan["ranking_policy"]) | ||
|
|
||
|
|
||
| def check_returned_ranking(observation, effective): | ||
| selection = observation.get("selection") if isinstance(observation, dict) else None | ||
| recorded = selection.get("ranking_policy") if isinstance(selection, dict) else None | ||
| try: | ||
| require_ranking_policy(recorded, effective) | ||
| except ValueError as error: | ||
| raise ValueError("Delivered ranking policy differs from the frozen session") from error | ||
|
|
||
|
|
||
| def _check_dispatched_request(recorded, expected, lane): | ||
| if not isinstance(recorded, dict) or any( | ||
| key not in recorded or type(recorded[key]) is not type(value) or recorded[key] != value | ||
| for key, value in expected.items() | ||
| ): | ||
| raise ValueError(f"{lane} knowledge receipt differs from its dispatched request") | ||
|
|
||
|
|
||
| def ordinary_delivery(root, plan, *, context_id, binding, reader, prepare_context, **request): | ||
| """Validate the prepared capture and its underlying retrieval before Manager retains it.""" | ||
| context = prepare_context(root, context_id=context_id, reader=reader, | ||
| diagnosis_binding=binding, **request) | ||
| if (not isinstance(context, dict) or context.get("context_id") != context_id | ||
| or context.get("diagnosis_binding") != binding): | ||
| raise ValueError("Knowledge capture differs from its dispatched request") | ||
| expected = {key: request[key] for key in ("query", "relevance", "scenario", "limit", | ||
| "context_bytes")} | ||
| expected["relevance_method"] = request["relevance_method"] or "find-lexical-v1" | ||
| _check_dispatched_request(context.get("request"), expected, "Ordinary") | ||
| observation = context.get("retrieval") | ||
| _check_dispatched_request(observation.get("request") if isinstance(observation, dict) else None, | ||
| expected, "Ordinary") | ||
| check_context_ranking(plan, context) | ||
| if "ranking_policy" in plan: | ||
| check_returned_ranking(observation, plan["ranking_policy"]) | ||
| reference = {"path": f"knowledge/{context_id}.json", | ||
| "sha256": hashlib.sha256(knowledge_capture.capture_bytes(context)).hexdigest(), | ||
| "kind": "delivered_context"} | ||
| return {"reference": reference, "context": context} | ||
|
|
||
|
|
||
| def service_delivery(plan, *, context_id, binding, reader, query, relevance_method, limit, | ||
| context_bytes, ranking_policy): | ||
| """Validate a fingerprint-free service receipt from the injected reader.""" | ||
| method = "find-lexical-v1" if relevance_method is None else relevance_method | ||
| context = reader.collect_service_context( | ||
| query, relevance_method=method, limit=limit, context_bytes=context_bytes, | ||
| ranking_policy=ranking_policy, | ||
| ) | ||
| check_context_ranking(plan, context) | ||
| expected = {"relevance": query, "relevance_method": method, "limit": limit, | ||
| "context_bytes": context_bytes} | ||
| recorded = context.get("request") if isinstance(context, dict) else None | ||
| _check_dispatched_request(recorded, expected, "Service") | ||
| if "diagnosis_binding" in context and context["diagnosis_binding"] != binding: | ||
| raise ValueError("Knowledge capture differs from its dispatched request") | ||
| if context.get("prepared_not_consumed") is not True: | ||
| raise ValueError("Service knowledge delivery requires a prepared context") | ||
| if binding: | ||
| context = {**context, "diagnosis_binding": binding} | ||
| return {"reference": {"path": f"knowledge/{context_id}.json", "kind": "delivered_context", | ||
| "prepared_not_consumed": True}, "context": context} | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.