K-Dense-AI/scientific-agent-skills / skills/clinical-decision-support
clinical-decision-support
VerifiedPrepares and validates research-only clinical decision-support evaluation, evidence-profile, cohort, survival, biomarker/model, privacy, and governance artifacts. Supports aggregate or synthetic research documentation and traceability, excluding patient care and live clinical operation.
scripts/decision_logic_traceability.py
View on GitHub#!/usr/bin/env python3
"""Validate and export research/governance decision-logic traceability."""
from __future__ import annotations
import argparse
import csv
import io
import re
import sys
from typing import Any
from _common import (
InputError,
IssueLog,
load_json_object,
require_list,
require_nonempty_text,
source_ids,
validate_references,
write_text,
)
MAX_NODES = 200
NODE_ID = re.compile(r"^[A-Za-z][A-Za-z0-9_-]{0,63}$")
ALLOWED_NODE_TYPES = {
"input_check",
"data_quality_gate",
"evidence_rule",
"validation_gate",
"documentation_gate",
"human_review",
"release_gate",
"monitoring_gate",
}
ALLOWED_OUTPUT_KINDS = {
"include_evidence",
"exclude_evidence",
"flag_for_review",
"validation_status",
"documentation_status",
"release_hold",
"monitoring_status",
}
PROHIBITED_LOGIC = (
"diagnose",
"prescribe",
"administer",
"dose ",
"dosing",
"triage",
"urgent",
"alarm",
"alert clinician",
"treatment selection",
"therapy recommendation",
"bedside",
"patient-specific",
)
def _safe_csv(value: Any) -> str:
text = str(value).replace("\r", " ").replace("\n", " ")
if text.lstrip().startswith(("=", "+", "-", "@")):
return "'" + text
return text
def _has_cycle(edges: dict[str, list[str]]) -> bool:
visiting: set[str] = set()
visited: set[str] = set()
def visit(node: str) -> bool:
if node in visiting:
return True
if node in visited:
return False
visiting.add(node)
for dependency in edges[node]:
if visit(dependency):
return True
visiting.remove(node)
visited.add(node)
return False
return any(visit(node) for node in edges)
def validate_matrix(
document: dict[str, Any]
) -> tuple[IssueLog, list[dict[str, str]]]:
log = IssueLog()
normalized: list[dict[str, str]] = []
try:
require_nonempty_text(document.get("schema_version"), "schema_version")
known_sources = source_ids(document)
metadata = document.get("metadata")
if not isinstance(metadata, dict):
raise InputError("metadata must be an object")
for field in (
"logic_id",
"title",
"version",
"status",
"owner",
"purpose",
"change_summary",
"monitoring_plan",
"retirement_criteria",
):
require_nonempty_text(metadata.get(field), f"metadata.{field}")
if metadata.get("decision_role") != "research_governance_only":
log.errors.append(
"metadata.decision_role must be research_governance_only"
)
if not isinstance(metadata.get("data_level"), str) or metadata.get("data_level") not in {"aggregate", "synthetic", "metadata_only"}:
log.errors.append(
"metadata.data_level must be aggregate, synthetic, or metadata_only"
)
if metadata.get("patient_care_use") is not False:
log.errors.append("metadata.patient_care_use must be false")
if metadata.get("executable_logic") is not False:
log.errors.append("metadata.executable_logic must be false")
nodes = require_list(document.get("nodes"), "nodes", maximum=MAX_NODES)
if not nodes:
raise InputError("At least one traceability node is required")
node_ids: set[str] = set()
for index, node in enumerate(nodes):
field = f"nodes[{index}]"
if not isinstance(node, dict):
raise InputError(f"{field} must be an object")
node_id = require_nonempty_text(
node.get("id"), f"{field}.id", max_length=64
)
if not NODE_ID.fullmatch(node_id):
raise InputError(f"{field}.id has an unsafe or invalid format")
if node_id in node_ids:
raise InputError(f"Duplicate node id: {node_id}")
node_ids.add(node_id)
edges: dict[str, list[str]] = {}
for index, node in enumerate(nodes):
field = f"nodes[{index}]"
node_id = require_nonempty_text(node["id"], f"{field}.id", max_length=64)
node_type = require_nonempty_text(node.get("type"), f"{field}.type")
if node_type not in ALLOWED_NODE_TYPES:
log.errors.append(f"{field}.type is unsupported")
precondition = require_nonempty_text(
node.get("precondition"), f"{field}.precondition"
)
statement = require_nonempty_text(
node.get("logic_statement"), f"{field}.logic_statement"
)
output_kind = require_nonempty_text(
node.get("output_kind"), f"{field}.output_kind"
)
if output_kind not in ALLOWED_OUTPUT_KINDS:
log.errors.append(f"{field}.output_kind is unsupported")
output_value = require_nonempty_text(
node.get("output_value"), f"{field}.output_value"
)
rationale = require_nonempty_text(
node.get("rationale"), f"{field}.rationale"
)
owner = require_nonempty_text(node.get("owner"), f"{field}.owner")
reviewer = require_nonempty_text(
node.get("reviewer_role"), f"{field}.reviewer_role"
)
status = require_nonempty_text(node.get("status"), f"{field}.status")
if status not in {"draft", "validated", "retired"}:
log.errors.append(f"{field}.status is unsupported")
combined = f"{precondition} {statement} {output_value}".lower()
found = [phrase for phrase in PROHIBITED_LOGIC if phrase in combined]
if found:
log.errors.append(
f"{field} contains prohibited clinical logic: {', '.join(found)}"
)
dependencies = require_list(
node.get("dependency_ids", []),
f"{field}.dependency_ids",
maximum=30,
)
normalized_dependencies: list[str] = []
for dep_index, dependency in enumerate(dependencies):
dep = require_nonempty_text(
dependency,
f"{field}.dependency_ids[{dep_index}]",
max_length=64,
)
if dep not in node_ids:
log.errors.append(f"{field} references unknown dependency: {dep}")
if dep == node_id:
log.errors.append(f"{field} cannot depend on itself")
if dep in node_ids and dep != node_id:
normalized_dependencies.append(dep)
edges[node_id] = normalized_dependencies
source_refs = validate_references(
node.get("source_ids"), known_sources, f"{field}.source_ids"
)
tests = require_list(
node.get("validation_tests"), f"{field}.validation_tests", maximum=30
)
if not tests:
log.errors.append(f"{field} requires at least one validation test")
normalized_tests = [
require_nonempty_text(test, f"{field}.validation_tests[{test_index}]")
for test_index, test in enumerate(tests)
]
normalized.append(
{
"node_id": node_id,
"type": node_type,
"dependencies": "; ".join(normalized_dependencies),
"precondition": precondition,
"logic_statement": statement,
"output_kind": output_kind,
"output_value": output_value,
"source_ids": "; ".join(source_refs),
"rationale": rationale,
"validation_tests": "; ".join(normalized_tests),
"owner": owner,
"reviewer_role": reviewer,
"status": status,
}
)
if _has_cycle(edges):
log.errors.append("Dependency graph contains a cycle")
review = document.get("human_review")
if not isinstance(review, dict):
raise InputError("human_review must be an object")
if review.get("required") is not True:
log.errors.append("human_review.required must be true")
require_nonempty_text(
review.get("approval_boundary"), "human_review.approval_boundary"
)
if metadata.get("status") == "validated" and review.get("completed") is not True:
log.errors.append(
"Validated matrix status requires completed human review"
)
elif review.get("completed") is not True:
log.warnings.append("Human review is not recorded as complete")
except InputError as exc:
log.errors.append(str(exc))
if log.ok:
log.info.append("Research/governance traceability matrix is structurally valid")
else:
normalized = []
return log, normalized
def _csv_text(rows: list[dict[str, str]]) -> str:
fieldnames = [
"node_id",
"type",
"dependencies",
"precondition",
"logic_statement",
"output_kind",
"output_value",
"source_ids",
"rationale",
"validation_tests",
"owner",
"reviewer_role",
"status",
]
stream = io.StringIO()
writer = csv.DictWriter(stream, fieldnames=fieldnames, lineterminator="\n")
writer.writeheader()
for row in rows:
writer.writerow({key: _safe_csv(row[key]) for key in fieldnames})
return stream.getvalue()
def main() -> int:
parser = argparse.ArgumentParser(
description=(
"Validate research/governance decision logic and emit a CSV traceability "
"matrix. The tool never executes logic."
)
)
parser.add_argument("input", help="Local matrix JSON")
parser.add_argument("-o", "--output", help="Optional local CSV output")
parser.add_argument(
"--strict", action="store_true", help="Return failure when warnings are present"
)
args = parser.parse_args()
try:
document = load_json_object(args.input)
log, rows = validate_matrix(document)
for message in log.errors:
print(f"ERROR: {message}", file=sys.stderr)
for message in log.warnings:
print(f"WARNING: {message}", file=sys.stderr)
if not log.ok or (args.strict and log.warnings):
return 1
if args.output:
write_text(args.output, _csv_text(rows), {".csv"})
else:
print(_csv_text(rows), end="")
except InputError as exc:
print(f"ERROR: {exc}", file=sys.stderr)
return 2
if not log.ok or (args.strict and log.warnings):
return 1
return 0
if __name__ == "__main__":
sys.exit(main())
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-changelog-updatemorluto/reaPrepare or rewrite REA release changelogs and GitHub release notes from pinned Git history, with verified contributor thanks and Release Please synchronization. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| academy-guideanthropics/skillsStop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials, and use cases from Claude Academy (academy.claude.com), Anthropic's learning hub. Trigger on: "how do I", "how can I", "getting started with", "what can Claude do", "teach me", "learn to use"; questions about artifacts, projects, skills, plugins, connectors, MCP; requests about rolling Claude out to a team, class, or organization; and any ask for training materials, onboarding content, or learning resources. Use it when the user is learning how to use a feature or product — not when they are mid-task and just want the task done. This skill composes with other skills: after consulting product documentation to answer how a Claude feature works, also check here for a matching course or tutorial — a docs-grounded answer and an Academy recommendation belong together. Only recommend on a strong match; never invent Academy content. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| claude-apianthropics/skillsReference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER — read BEFORE opening the target file; don't skip because it "looks like a one-liner" — whenever: the prompt names Claude/Anthropic in any form (Claude, Anthropic, Fable, Opus, Sonnet, Haiku, `anthropic`, `@anthropic-ai`, `claude-*`, `us.anthropic.*`, `[1m]`); the user asks about an LLM (pricing/model choice/limits/caching) — never answer from memory; OR the task is LLM-shaped with provider unstated (agent/MCP/tool-definition/multi-agent/RAG/LLM-judge/computer-use; generate/summarize/extract/classify/rewrite/converse over NL; debugging refusals/cutoffs/streaming/tool-calls/tokens). SKIP only when another provider is being worked on (overrides all triggers): OpenAI/GPT/Gemini/Llama/Mistral/Cohere/Ollama named in the query; OR `grep -rE 'openai|langchain_openai|google.generativeai|genai|mistralai|cohere|ollama'` over the project hits (run this grep FIRST if no provider named — don't Read the file). | Stars: 198k | Updated | Claude CodeCodex | Unverified | Install |
| doc-coauthoringanthropics/skillsGuide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision docs, or similar structured content. This workflow helps users efficiently transfer context, refine content through iteration, and verify the doc works for readers. Trigger when user mentions writing docs, creating proposals, drafting specs, or similar documentation tasks. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |