Skip to content

agents

knowledge_graph.clinical.agents

codex exec agent jobs with schema output, validation feedback, and bounded parallelism.

Used by the Synthetic-100 generation (synthetic) and annotation (annotation, hpo_annotation) stages.

read_response(path)

Return a decoded response and retry feedback for missing or malformed JSON.

Source code in src/knowledge_graph/clinical/agents.py
12
13
14
15
16
17
18
19
20
def read_response(path):
    """Return a decoded response and retry feedback for missing or malformed JSON."""
    try:
        candidate = load_json(path)
    except (json.JSONDecodeError, UnicodeDecodeError, FileNotFoundError) as error:
        return None, [f"invalid response JSON: {error}"]
    if not isinstance(candidate, dict):
        return None, ["response must be a JSON object"]
    return candidate, []

agent(job, prompt, accept, args, semaphore, *, attempts, followup) async

Run codex exec in the job directory until accept(response) reports no errors; errors go into the next prompt.

Source code in src/knowledge_graph/clinical/agents.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
async def agent(job, prompt, accept, args, semaphore, *, attempts, followup):
    """Run `codex exec` in the job directory until accept(response) reports no errors; errors go into the next prompt."""
    async with semaphore:
        feedback = ""
        for attempt in range(1, attempts + 1):
            response = job / "response.json"
            response.unlink(missing_ok=True)
            command = ["codex", "exec", "-C", str(job), "--skip-git-repo-check", "-m", args.model,
                       "-c", f'model_reasoning_effort="{args.reasoning_effort}"', "-s", "read-only", "--ephemeral",
                       "--output-schema", str(job / "schema.json"), "-o", str(response),
                       prompt + (followup(feedback) if feedback else "")]
            with (job / f"attempt_{attempt}.log").open("w", encoding="utf-8") as log:
                process = await asyncio.create_subprocess_exec(*command, stdout=log, stderr=asyncio.subprocess.STDOUT)
                code = await process.wait()
            errors = [f"agent command exited with status {code}"] if code else accept(response)
            if not errors:
                return True
            feedback = "\n".join(errors)
        return False

bullet_feedback(header, lines)

Source code in src/knowledge_graph/clinical/agents.py
44
45
def bullet_feedback(header, lines):
    return lambda feedback: f"\n\n{header}\n- " + "\n- ".join(feedback.splitlines()[:lines])

run(jobs, args)

Run job coroutine factories with at most args.max_parallel at a time; exit status 1 when any job fails.

Source code in src/knowledge_graph/clinical/agents.py
48
49
50
51
52
53
54
55
def run(jobs, args):
    """Run job coroutine factories with at most args.max_parallel at a time; exit status 1 when any job fails."""

    async def gather():
        semaphore = asyncio.Semaphore(args.max_parallel)
        return await asyncio.gather(*(job(semaphore) for job in jobs))

    return int(not all(asyncio.run(gather())))