Skip to content

API Reference parrot_tools.contracts.retrieval

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_tools.contracts.retrieval

View source

Deterministic, authorized contract retrieval (FEAT-539 M9).

Everything in this module is LLM-free. It classifies a question against ten explicit patterns, resolves entities against the authorized catalog, binds every AQL/SQL value itself and executes only allowlisted YAML patterns. There is no dynamic AQL, no LLM fallback and no client.

Two rules shape the design:

  • Authorization happens before any protected read. The gate runs on the trusted request context (never on a client- or model-supplied role or tenant) and is default-deny with contract_reader OR contract_owner; my_contracts additionally requires an authenticated identity and narrows results to the caller's management chain — including the evidence reads that follow.
  • Uncertainty fails closed. An unclassifiable question, an ambiguous entity match or a stale/incomplete graph projection returns a typed clarification or denial, never a guess.

Class AuthorizationDenied

A protected read/write was refused before it happened.

Parameter Type Default Description
reason str
pattern Optional[str] None

Class Clarification

A typed request for more information — never an invented answer.

Fields:

Field Type Default Description
reason str
pattern Optional[str] None
candidates list[str]

Class ContractRetrieval

Authorized, deterministic retrieval over catalog and graph.

Parameter Type Default Description
catalog ContractCatalogStore The tenant-bound catalog (SQL is always available).
graph_store Any None Optional ArangoOntologyGraphStore for YAML patterns.
tenant_context Any None Optional TenantContext for graph reads.
ontology Any None Optional merged ontology supplying the allowlisted AQL.
authorization Any None Optional AuthorizationChecker; the local role gate runs regardless.
today Any None Injectable clock for date windows.
ranker Any None Optional non-generative ranking callable for entity resolution; never an LLM.

Method ContractRetrieval.authorize

Run the shared entry gate before any protected read.

authorize(context: RequestContext, *, pattern: Optional[str] = None, owner_only: bool = False) -> None

Parameter Type Default Description
context RequestContext The trusted request context.
pattern Optional[str] None The pattern being served, for the denial message.
owner_only bool False Require the owner role (verify/merge/retire).

Raises:

  • AuthorizationDenied — On a missing identity, a foreign tenant or insufficient roles. Default deny.

Method ContractRetrieval.resolve_contract

Resolve a contract id or title mentioned in the question.

Matching is exact id, then exact title, then normalized fuzzy over the authorized catalog. Several equally good matches produce a :class:Clarification — never a pick.

resolve_contract(question: str, context: RequestContext) -> str | Clarification

Parameter Type Default Description
question str
context RequestContext

Returns: str | Clarification

Method ContractRetrieval.resolve_party

Resolve a counterparty mentioned in the question.

Exact name, then catalog alias, then normalized fuzzy match.

resolve_party(question: str, context: RequestContext) -> str | Clarification

Parameter Type Default Description
question str
context RequestContext

Returns: str | Clarification

Method ContractRetrieval.plan

Classify a question and bind every value it needs.

Authorization for the resolved pattern runs before any entity is read out of the catalog.

plan(question: str, context: RequestContext) -> PatternPlan | Clarification

Parameter Type Default Description
question str
context RequestContext

Returns: PatternPlan | Clarification

Method ContractRetrieval.retrieve

Plan and execute one deterministic retrieval.

retrieve(question: str, context: RequestContext) -> RetrievalResult | Clarification

Parameter Type Default Description
question str
context RequestContext

Returns: RetrievalResult | Clarification — class:RetrievalResult, or a :class:Clarification when classification or entity resolution was uncertain.

Raises:

  • AuthorizationDenied — When the gate refuses the request.

Method ContractRetrieval.execute

Execute a bound plan against SQL (always) or the graph.

SQL is authoritative and always available: search, date windows and the verification queue keep working with no ArangoDB at all.

execute(plan: PatternPlan, context: RequestContext) -> RetrievalResult

Parameter Type Default Description
plan PatternPlan
context RequestContext

Returns: RetrievalResult

Method ContractRetrieval.pattern_for

Return the allowlisted traversal pattern declared in YAML.

pattern_for(pattern: str) -> TraversalPattern

Parameter Type Default Description
pattern str

Returns: TraversalPattern

Raises:

  • AuthorizationDenied — For any pattern outside the allowlist.
  • RuntimeError — When no ontology was configured.

Method ContractRetrieval.execute_graph

Execute one allowlisted pattern against the graph.

The projection's card_revision is validated against the authoritative catalog: a stale or incomplete projection fails closed instead of answering from it.

execute_graph(plan: PatternPlan, context: RequestContext, *, collection_binds: Optional[dict[str, str]] = None) -> list[dict[str, Any]]

Parameter Type Default Description
plan PatternPlan
context RequestContext
collection_binds Optional[dict[str, str]] None

Returns: list[dict[str, Any]]

Raises:

  • AuthorizationDenied — When the gate refuses, the pattern is not allowlisted, or the projection is stale/incomplete.
  • RuntimeError — When no graph store is configured.

Attribute DEFAULT_TOP_K

Attribute FUZZY_THRESHOLD

Attribute INTERPRETATION_MARKERS

Type: tuple[str, ...]

Attribute MAX_TOP_K

Attribute OWNER_ROLE

Attribute PATTERNS

Type: tuple[str, ...]

Class PatternPlan

A classified question with every bind value already resolved.

Fields:

Field Type Default Description
pattern str
binds dict[str, Any]
entities dict[str, str]
interpretation_required bool False

Attribute READ_ROLES

Type: tuple[str, str]

Class RequestContext

The trusted identity of one request.

Built by the transport from authenticated session data. Roles, tenant and employee identity are never taken from the question, the model or a tool argument.

Fields:

Field Type Default Description
user_id Optional[str] None
roles tuple[str, ...] ()
tenant_id Optional[str] None
employee_id Optional[str] None
employee_graph_id Optional[str] None
department Optional[str] None
confirmed bool False

Attributes:

Attribute Type Description
authenticated bool True when the transport supplied an authenticated identity.

Method RequestContext.has_any_role

Whether the context carries at least one of roles.

has_any_role(roles: Sequence[str]) -> bool

Parameter Type Default Description
roles Sequence[str]

Returns: bool

Class RetrievalResult

Deterministic retrieval output handed to the answer service.

Fields:

Field Type Default Description
pattern str
cards list[ContractCard]
obligations list[Obligation]
rows list[dict[str, Any]]
binds dict[str, Any]
used_graph bool False
notes list[str]

Function classify

Return the most specific pattern a question triggers.

classify(question: str) -> Optional[str]

Parameter Type Default Description
question str The user's question.

Returns: Optional[str] — The pattern name, or None when nothing matches (which fails closed rather than guessing).

Function is_interpretation

Whether a question asks for judgement rather than a lookup.

is_interpretation(question: str) -> bool

Parameter Type Default Description
question str

Returns: bool

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally