Drug-Safety Agent on Databricks · Desk and approval · Store and decisions
The store and the decision rules
The store holds each assessment with a status. The agent can only put a draft in. A person can then approve, edit or reject it. After that it is final. These rules live in the store, not in a prompt, so no wording of a question can bend them.
| Rule | What the code does | Proved by |
|---|---|---|
| The agent only drafts | It is offered one write tool, draft_assessment, and none to decide. The numbers in a draft are re-read from the data, whatever the model passes. | test_the_agent_is_offered_no_way_to_decide; test_the_numbers_stored_come_from_the_data |
| One assessment per pair | The same drug and event always give the same id. A waiting draft is updated; a decided pair is never redrafted. | test_a_decided_pair_is_not_redrafted |
| A decision needs a name | An empty reviewer is refused and nothing changes. | test_a_decision (8 rows) |
| A decision is final | Approving, rejecting or editing an already-decided assessment is refused. | test_a_decision_is_final (4 rows) |
| An edit must still be valid | Blank or mistyped fields are refused, so an edit cannot break the draft's shape. | test_a_decision, the edit rows |
| Every write is on a trail | Who drafted, who decided, who claimed a hand-off, and when. | test_every_write_is_on_the_audit_trail |
The scenario that used to fail. One of the ten scenarios, a draft assessment lands when every condition holds, failed because there was nowhere for it to land. It now passes, and all ten pass.
Next. Move the table to Delta, put a login in front of the desk, and tell a reviewer when something is waiting. The agent's code does not change for any of those: they sit behind the same store.
The real code
Cut from the repository each time this site is built, so it always matches what runs. Each header names the file and the line it was cut from.
src/drugsafety/store.py:78Drafts in: validated, one per pair, never over a decided one def put_draft(
self,
drug: str,
event: str,
body: dict[str, Any],
*,
created_for: str,
source: str,
steps: int = 0,
spend_usd: float = 0.0,
aid: str | None = None,
) -> tuple[str, bool]:
"""Store a draft with status `drafted`. Returns (id, created). A pair that already has a draft waiting is updated
in place; a pair that has been decided is left alone and the existing id comes back, with created False."""
problems = validate_body(body)
if problems:
raise StoreError("draft refused: " + "; ".join(problems))
aid = aid or assessment_id(drug, event)
row = self.db.execute("SELECT status FROM assessments WHERE id=?", (aid,)).fetchone()
if row is None:
self.db.execute(
"INSERT INTO assessments(id,drug,event,body,status,created_for,source,steps,spend_usd,created_at)"
" VALUES (?,?,?,?,?,?,?,?,?,?)",
(aid, drug.lower(), event.upper(), json.dumps(body), "drafted", created_for, source, steps, spend_usd, _now()),
)
self._audit(source, "drafted", aid, f"{drug}/{event}")
self.db.commit()
return aid, True
if row["status"] == "drafted":
self.db.execute(
"UPDATE assessments SET body=?, steps=?, spend_usd=?, source=? WHERE id=?",
(json.dumps(body), steps, spend_usd, source, aid),
)
self.db.commit()src/drugsafety/store.py:116Decisions out: a named person, once def decide(
self, aid: str, reviewer_id: str, decision: str, *, edited_body: dict[str, Any] | None = None, note: str = ""
) -> Assessment:
if decision not in DECISIONS:
raise StoreError(f"decision must be one of {sorted(DECISIONS)}")
if not reviewer_id.strip():
raise StoreError("a decision must name the reviewer") # P-APPROVER
row = self.db.execute("SELECT status FROM assessments WHERE id=?", (aid,)).fetchone()
if row is None:
raise StoreError(f"no assessment {aid}")
if row["status"] != "drafted":
raise StoreError(f"{aid} is already {row['status']}; a decision is final") # P-APPROVAL-FINAL
if decision == "edit":
problems = validate_body(edited_body)
if problems:
raise StoreError("the edited draft is not valid: " + "; ".join(problems))
self.db.execute(
"UPDATE assessments SET status=?, decided_by=?, decided_at=?, edited_body=?, note=? WHERE id=?",
(DECISIONS[decision], reviewer_id, _now(), json.dumps(edited_body) if decision == "edit" else None, note, aid),
)
self._audit(reviewer_id, DECISIONS[decision], aid, note)
self.db.commit()
return self.get(aid)src/drugsafety/world_tools.py:141The agent's one write: facts from the data, only the words from the caller async def _draft(self, args: dict[str, object], drug: str, event: str, identity, key) -> dict:
"""The one way a draft is written. The facts come from the data; only the words may come from the caller."""
f = await self.facts(drug, event, identity, key)
if "error" in f:
return f
label = "already mentions it" if f["on_label"] else "does not mention it"
screen = "screens as a signal" if f["screens"] else "does not screen as a signal"
body = {
"drug": f["drug"],
"event": f["event"],
"cases": f["cases"],
"prr": f["prr"],
"on_label": f["on_label"],
"assessment": str(args.get("assessment") or f"{f['drug']} and {f['event']} {screen}; the label {label}."),
"uncertainty": str(args.get("uncertainty") or "Spontaneous reports cannot show that the drug causes the event."),
"recommendation": str(args.get("recommendation") or "Review."),
}
aid = str(args.get("id") or assessment_id(drug, event))
got = await self.inner.call(
"draft_assessment",
{
"id": aid,
"drug_event_id": pair_id(drug, event),
"reviewer_id": identity.customer_id,
"status": "drafted",
"cases": body["cases"],
"prr": body["prr"],
"on_label": body["on_label"],
"text": body["assessment"],
"uncertainty": body["uncertainty"],
"recommendation": body["recommendation"],
"drug": body["drug"],
"event": body["event"],
"steps": args.get("steps", 0),