Repository navigation
Trace CANNBot round knowledge in managed optimization #523
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| """Connect managed round knowledge to the approved read-only CANNBot worker.""" | ||
|
|
||
| import math | ||
| import re | ||
|
|
||
| from ...harness.records.evidence import relative_location | ||
| from ...knowledge.channels import ChannelRegistry, search_channels | ||
| from . import SCORE_SCALE, CannbotChannel, project | ||
|
|
||
| SCORE_SCALES = (SCORE_SCALE, "knowledge-search.v5") | ||
|
|
||
|
|
||
| class CannbotRoundKnowledge: | ||
| def search(self, query, *, settings, limit, timeout): | ||
| channel = CannbotChannel() | ||
| results = search_channels( | ||
| ChannelRegistry([channel]), query, backend=settings["backend"], | ||
| platform=settings["platform"], limit=limit, timeout=min(8.0, timeout), | ||
| ) | ||
| result, = results | ||
| revision = result.status.revision | ||
| if not isinstance(revision, str) or not re.fullmatch(r"[a-f0-9]{40}", revision): | ||
| revision = None | ||
| # Select metadata explicitly. The channel view also contains prose, open | ||
| # commands and worker diagnostics, none of which belongs in a receipt. | ||
| items = [ | ||
| {"locator": hit.locator, "card_sha256": hit.card_sha256, | ||
| "score": hit.score if math.isfinite(hit.score) else None} | ||
| for hit in result.items | ||
| if relative_location(hit.locator) == hit.locator | ||
| and re.fullmatch(r"[a-f0-9]{64}", hit.card_sha256) | ||
| ] | ||
| return { | ||
| "state": result.status.state, "revision": revision, "items": items, | ||
| "count": len(result.items), "elapsed_ms": result.elapsed_ms, | ||
| "score_scale": result.score_scale if result.score_scale in SCORE_SCALES else None, | ||
| } | ||
|
|
||
| def read(self, locator, *, revision, card_sha256, page, timeout): | ||
| try: | ||
| result = project.read( | ||
| locator, expected_revision=revision, expected_sha256=card_sha256, page=page, | ||
| timeout=min(15.0, timeout), | ||
| ) | ||
| except project.ChannelFailure as exc: | ||
| reason = f"CANNBot read failed ({exc.state}); check the grant or search again" | ||
| raise ValueError(reason) from None | ||
| if ( | ||
| result.get("state") != "ok" or result.get("locator") != locator | ||
| or result.get("card_sha256") != card_sha256 or result.get("revision") != revision | ||
| or result.get("page") != page or not isinstance(result.get("pages"), int) | ||
| ): | ||
| raise ValueError("CANNBot read did not match the recorded search") | ||
| if page > result["pages"]: | ||
| raise ValueError("CANNBot page is outside the card") | ||
| if not isinstance(result.get("text"), str) or not result["text"].strip(): | ||
| raise ValueError("CANNBot returned an empty card page") | ||
| return {key: result[key] for key in ("text", "page", "pages")} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -311,6 +311,61 @@ def read_searches(root: Path, campaign_id, rows, retrievals): | |
| return results | ||
|
|
||
|
|
||
| def managed_knowledge(root, contexts): | ||
| """Project managed deliveries and explicit citation targets without card bodies.""" | ||
| queries, external, citations = {}, {}, {} | ||
| for reference in contexts: | ||
| path, kind = reference["path"], reference["kind"] | ||
| record = json.loads((root / path).read_text("utf-8")) | ||
| number = reference.get("round") | ||
| if kind == "delivered_context": | ||
| delivered = {item["ref"] for item in record["contexts"] if item["resources"]} | ||
| items = [ | ||
| {**{key: item.get(key) for key in ("ref", "title", "revision")}, | ||
| "delivered": item["ref"] in delivered} | ||
| for item in record["items"] | ||
| ] | ||
| if number is not None: | ||
| request = record["request"] | ||
| queries[path] = { | ||
| "round": number, "path": path, | ||
| "query": request.get("relevance") or request.get("query"), | ||
| "items": items, | ||
| } | ||
| citation = {"source": "local", "retrieval_round": number} | ||
| citations[path] = {**citation, "ref": path} | ||
| for item in items: | ||
| if item["delivered"]: | ||
| citations[path + "#" + item["ref"]] = { | ||
| **citation, "ref": item["ref"], "revision": item["revision"], | ||
| } | ||
| elif kind == "external_search": | ||
| # The digest checks file integrity, not whether the record's | ||
| # fields are safe to disclose. Expose only known view metadata. | ||
| search = { | ||
| **{key: record.get(key) for key in ( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The metadata field allowlist does not validate the values. |
||
| "channel", "backend", "platform", "state", "revision", | ||
| "score_scale", "count", "elapsed_ms", | ||
| )}, | ||
| "path": path, "reads": [], | ||
| "items": [{key: hit.get(key) for key in ("locator", "score", "card_sha256")} | ||
| for hit in record["items"]], | ||
| } | ||
| external[path] = search | ||
| queries[record["context_ref"]]["external"] = search | ||
| elif kind == "external_read": | ||
| receipt = {key: record[key] for key in ("locator", "page")} | ||
| external[record["search_ref"]]["reads"].append(receipt) | ||
| citations[path] = { | ||
| "source": record["channel"], "retrieval_round": number, | ||
| "ref": record["locator"], "revision": record["revision"], | ||
| "card_sha256": record["card_sha256"], "page": record["page"], | ||
| } | ||
| return public_value(list(queries.values())), { | ||
| path: public_value(citation) for path, citation in citations.items() | ||
| } | ||
|
|
||
|
|
||
| def project_knowledge(root, data): | ||
| """One projection for both layouts, offline export and live refresh.""" | ||
| story = data.get("story") or {} | ||
|
|
||
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.
This resolves mutable
state.jsoncitations against the current session-wide context map._load()verifies the observation file's hash but doesn't compare its recorded citations and context snapshot with state. A completed round can therefore display a later receipt as cited while its verified observation stays unchanged. Please resolve completed-round citations from the verified observation and submitted context snapshot, or check that state matches both, with a regression for later receipts.