Skip to content

agent-scientific

The agent-scientific worked example.

Run it from sema/:

Terminal window
sema check examples/agent-scientific
SEMA_STRICT=1 sema run examples/agent-scientific
sema assure examples/agent-scientific --grade silver
"""A failed experiment triggers a bounded dynamically admitted proof auditor."""
from std.agents import AgentSpec, AgentEnvelope
assure silver
struct ExperimentState:
theorem: str
observation: str
struct ScientificIssue:
failed: bool
detail: str
struct ProofFinding:
correction: str
sound: bool
struct Paper:
title: str
body: str
struct Verification:
passed: bool
agent monitor(state: ExperimentState) -> ScientificIssue by monitor_model:
sem "Detect theorem/experiment mismatch and request intervention only on failure"
budget model_calls=1, tokens=512
ensure result.failed == true
agent writer(finding: ProofFinding) -> Paper by writer_model:
sem "Revise the paper from the proof finding and preserve the correction provenance"
budget model_calls=2, tokens=1024
ensure len(result.body) >= 1
agent verifier(paper: Paper) -> Verification by verifier_model:
sem "Apply deterministic verification first; never overrule a failed hard gate"
budget model_calls=1, tokens=512
ensure result.passed == true
def proof_auditor(issue: ScientificIssue) -> AgentSpec[ScientificIssue, ProofFinding] !{}:
return AgentSpec(name="proof_auditor", input_type="ScientificIssue", output_type="ProofFinding",
sem="Redo the derivation, isolate the failed premise, and return a sound correction",
model="proof_model", tools=[], completion="contract", model_calls=2, tokens=1024)
circuit publish(state: ExperimentState) -> Paper !{agent.spawn, model.invoke}:
budget agents=8, spawn_depth=2, model_calls=16, tokens=12000
issue = monitor(state)
envelope = AgentEnvelope(input_type="ScientificIssue", output_type="ProofFinding",
allowed_models=["proof_model"], tools=[], max_model_calls=2,
max_tokens=1024, max_children=0, max_spawn_depth=0)
if issue.failed:
spec = proof_auditor(issue)
specialist = Agent.build(spec, under=envelope)?
finding = (spawn specialist(issue)).join()?
draft = writer(finding)
verification = verifier(draft)
ensure verification.passed
return draft
return Paper(title="No correction required", body=state.observation)
def main() -> Paper !{agent.spawn, model.invoke}:
result = publish(ExperimentState(theorem="H", observation="counterexample"))
print(result.title)
return result

A failed experiment triggers a bounded dynamically admitted proof auditor.

Fields

fieldtypedescriptor
theoremstr
observationstr

Fields

fieldtypedescriptor
failedbool
detailstr

Fields

fieldtypedescriptor
correctionstr
soundbool

Fields

fieldtypedescriptor
titlestr
bodystr

Fields

fieldtypedescriptor
passedbool
agent monitor(state: ExperimentState) -> ScientificIssue

Parameters

nametype
stateExperimentState

Returns ScientificIssue

agent writer(finding: ProofFinding) -> Paper

Parameters

nametype
findingProofFinding

Returns Paper

agent verifier(paper: Paper) -> Verification

Parameters

nametype
paperPaper

Returns Verification

def proof_auditor(issue: ScientificIssue) -> AgentSpec[ScientificIssue, ProofFinding] !{}

Parameters

nametype
issueScientificIssue

Returns AgentSpec[ScientificIssue, ProofFinding]

Effects !{}

circuit publish(state: ExperimentState) -> Paper !{agent.spawn, model.invoke}

Parameters

nametype
stateExperimentState

Returns Paper

Effects !{agent.spawn, model.invoke}

def main() -> Paper !{agent.spawn, model.invoke}

Returns Paper

Effects !{agent.spawn, model.invoke}