Skip to content

tdfs4ds.impact_agent.reviewer

tdfs4ds.impact_agent.reviewer

The reviewer: the impact agent's main job.

The deterministic study (:mod:tdfs4ds.impact, :mod:tdfs4ds.impact_features) decides most of what it sees. Some findings it could NOT decide, or decided only in part (a computation it could not compare, a reader whose select list it could not narrow, a view it could not read), or sit in SQL that resists a parser (table operators, TD_* functions, UDFs, PIVOT...). The reviewer reads exactly those and gives an opinion, one bounded model call each. It never changes a verdict, a severity or a count: its opinion is attached beside the code's finding, marked "agent opinion, for expert review", and an opinion that contradicts the code is flagged, never substituted.

Layers (the first one and the last one are pure and need no model):

  • :func:reviewable_items - the undecided items, each with the SQL it needs and the code's verdict;
  • :func:review_items - at most IMPACT_AGENT_MAX_REVIEWS typed model calls, post-checked in code (the quoted evidence must be in the SQL that was given);
  • :func:attach_reviews - display-only review keys on the matching feature, reader, dependent and unverified entry.

OPINIONS module-attribute

OPINIONS = ('likely_affected', 'likely_unaffected', 'cannot_tell')

CONFIDENCES module-attribute

CONFIDENCES = ('low', 'medium', 'high')

OPINION_LABEL module-attribute

OPINION_LABEL = 'agent opinion, for expert review'

MARKER module-attribute

MARKER = 'Reviewed by the agent'

DISAGREES module-attribute

DISAGREES = 'disagrees with the code'

SQL_LIMIT module-attribute

SQL_LIMIT = 4000

_SQL_UNVERIFIED module-attribute

_SQL_UNVERIFIED = re.compile('(?i)\\bSQL\\b|select list|\\bview\\b|compile|probe|\\bDDL\\b|could not be (?:read|split|parsed)')

_SYSTEM module-attribute

_SYSTEM = "You review ONE finding of an impact study of a change in a Teradata feature store. You are given WHAT CHANGED (the_change: for example the column AVG_AMOUNT is removed, or the logic of a feature changed) and the SQL of a reader (current_sql, and reader_items: every output item of the reader with its expression). Read the reader's expressions and decide whether any of them uses the changed column or feature, directly or through a function you do not know: a function that takes the column as an argument uses it, whatever the function does. Use cannot_tell ONLY when the SQL text given is insufficient (dynamic SQL, text not provided), never because a function is unfamiliar. The code_verdict and its reason are a hypothesis made by a parser: confirm or contradict it from the SQL. Reason ONLY from the SQL given, never from what an object is called. Answer: opinion (likely_affected = the change probably affects this object or feature; likely_unaffected = no expression of it uses the changed column or feature; cannot_tell), confidence (low, medium, high), reason (at most two sentences), evidence (quote, exactly, the fragment of the SQL given that uses the changed column; when NO expression mentions the changed column, answer likely_unaffected (not cannot_tell) and quote any one expression of the SQL given, exactly, as evidence)."

_setting

_setting(name: str, default: int) -> int

_u

_u(v) -> str

bound_sql

bound_sql(text: Optional[str], around: Optional[List[str]] = None, limit: int = SQL_LIMIT) -> Optional[str]

text as it is when short, else about limit characters around the first mention of around (or from the start). Pure.

_ddl_for

_ddl_for(ddl: Dict[str, str], name: Optional[str]) -> Optional[str]

_target_for

_target_for(model: dict, pid) -> dict

the_change

the_change(model: dict, inputs: Optional[dict]) -> dict

What changed, for the reviewer: columns removed / added / retyped, features removed or with a changed logic, and the old and new SQL of the edited object, bounded. Pure.

_reader_items

_reader_items(text: Optional[str]) -> Optional[List[dict]]

The output items (name, expression) of a reader's select list when short enough. Pure.

reviewable_items

reviewable_items(model: dict, inputs: Optional[dict]) -> List[dict]

The items the deterministic study could not decide, or decided in part. Pure.

Each item: {id, kind: feature|reader|unverified, object, node, feature, why_undecided, old_sql, new_sql, code_verdict: {severity, reason}, constructs, priority} where priority is 0 for a breaks / values_change / may_change finding, 1 for an unverified entry and 2 for a construct alone. Items are ordered by priority then severity and numbered R1, R2...

_schema

_schema()

_norm

_norm(s: Any) -> str

_agrees

_agrees(opinion: str, severity: Optional[str]) -> Optional[bool]

check_review

check_review(item: dict, out: Any) -> dict

The review of item from a model answer, post-checked in code. Pure.

The quoted evidence must be a substring (whitespace and case normalised) of the SQL supplied with the item, else the opinion becomes cannot_tell ("evidence not found in the SQL"). An opinion that disagrees with the code's severity is flagged, never substituted.

_payload

_payload(item: dict) -> str

review_items

review_items(items: List[dict], *, llm=None, max_reviews: Optional[int] = None, progress: Optional[Callable[[str], None]] = None, thread_id: Optional[str] = None) -> Dict[str, Any]

One bounded typed call per item, at most max_reviews (default IMPACT_AGENT_MAX_REVIEWS).

Returns {"reviews": [...], "skipped": [{id, object, reason}], "calls": [{id, object, seconds, error}]}. A failed call is a skipped item with the error, never an exception. llm None builds the impact agent's model; without a model every item is skipped with the reason.

attach_reviews

attach_reviews(model: dict, reviews: List[dict]) -> dict

Set a display-only review on the matching feature, reader, dependent and unverified entry (in place). Severities, verdicts and counts are never touched. Pure.