Skip to content

Graph and state#

The agent topology: nodes wired by edges, entered at a hub. See Customize agents and tools for the narrative version.

adda.Graph dataclass #

Agent graph passed to :class:~agent_runtime.AgenticRun.

Declares which agents exist, which directed delegation edges connect them, and which agent starts the run. Loops are permitted.

Parameters:

Name Type Description Default
nodes dict[str, Agent]

Maps unique agent names to :class:Agent instances.

required
edges sequence of Edge

Directed delegation edges. An agent with no outgoing edges receives no Delegate tool.

()
entry str

Name of the agent that receives the initial briefing.

None

Raises:

Type Description
ValueError

If any edge endpoint names an undeclared node, or if entry is not declared.

Source code in src/adda/_src/backends/base.py
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
@dataclass
class Graph:
    """Agent graph passed to :class:`~agent_runtime.AgenticRun`.

    Declares which agents exist, which directed delegation edges connect
    them, and which agent starts the run.  Loops are permitted.

    Parameters
    ----------
    nodes : dict[str, Agent]
        Maps unique agent names to :class:`Agent` instances.
    edges : sequence of Edge
        Directed delegation edges.  An agent with no outgoing edges
        receives no ``Delegate`` tool.
    entry : str
        Name of the agent that receives the initial briefing.

    Raises
    ------
    ValueError
        If any edge endpoint names an undeclared node, or if *entry* is
        not declared.
    """

    nodes: dict  # dict[str, Agent]
    edges: tuple = ()
    entry: str = None  # type: ignore[assignment]

    def __post_init__(self) -> None:
        self.edges = tuple(self.edges)
        if self.entry is None:
            raise ValueError("Graph.entry is required and must not be None.")
        bad = [k for k, v in self.nodes.items() if not isinstance(v, Agent)]
        if bad:
            raise TypeError(
                f"Graph nodes values must be Agent instances; "
                f"got invalid values for keys: {sorted(bad)}"
            )
        names = set(self.nodes)
        for e in self.edges:
            if e.source not in names or e.target not in names:
                raise ValueError(
                    f"Edge {e!r} references undeclared node. "
                    f"Declared names: {sorted(names)}"
                )
        if self.entry not in names:
            raise ValueError(
                f"entry={self.entry!r} not in nodes (entry node undeclared). "
                f"Declared names: {sorted(names)}"
            )
        # Only a delegator reads a node's description: it is how a node that
        # MAY delegate chooses its target. A node no edge points to (the
        # entry of a one-node graph) has no reader, so it may omit one. A
        # spawned node is a copy of an edge target and gets its own from
        # its instructions (runtime/topology.py).
        targets = {e.target for e in self.edges}
        missing_desc = [n for n in targets if not self.nodes[n].description]
        if missing_desc:
            raise ValueError(
                f"A node that another node may delegate or hand off to must define a "
                f"non-empty description (its delegators read it to choose "
                f"a target). Missing in: {sorted(missing_desc)}"
            )

    def with_node(self, name: str, agent: Agent) -> Graph:
        """Return a new Graph with *agent* under *name*.

        If *name* already exists, the new agent replaces it and its edges
        are kept. The original graph is not changed. Add the delegation
        edges for a new node with :meth:`with_edge`.
        """
        return Graph(nodes={**self.nodes, name: agent},
                     edges=self.edges, entry=self.entry)

    def with_edge(self, source: str, target: str,
                  preamble: str = "", handoff: bool = False) -> Graph:
        """Return a new Graph where *source* may delegate (or, with
        ``handoff=True``, hand its job) to *target*.

        Both nodes must already exist. An existing edge of the same mode
        between them is replaced, so its preamble can be changed. The
        original graph is not changed.
        """
        kept = tuple(e for e in self.edges
                     if (e.source, e.target, e.handoff)
                     != (source, target, handoff))
        return Graph(nodes=dict(self.nodes),
                     edges=(*kept, Edge(source, target, preamble, handoff)),
                     entry=self.entry)

    def outgoing(self, name: str) -> list[str]:
        """Return the names *name* may delegate to (handoff edges excluded)."""
        return [e.target for e in self.edges
                if e.source == name and not e.handoff]

    def incoming(self, name: str) -> list[str]:
        """Return the names that may delegate to *name*."""
        return [e.source for e in self.edges
                if e.target == name and not e.handoff]

    def handoff_targets(self, name: str) -> list[str]:
        """Return the names *name* may hand its job to."""
        return [e.target for e in self.edges if e.source == name and e.handoff]

    def edge(self, source: str, target: str,
             handoff: bool = False) -> Edge | None:
        """Return the Edge of that mode from *source* to *target*, or None."""
        for e in self.edges:
            if (e.source, e.target, e.handoff) == (source, target, handoff):
                return e
        return None

    # Curated semantic colours (fill, stroke) by agent class name;
    # unknown classes cycle a fallback palette so any graph stays legible.
    _MERMAID_COLOURS = {
        "StrategizerAgent": ("#1d4ed8", "#1e3a8a"),
        "LiteratureReviewAgent": ("#6d28d9", "#4c1d95"),
        "DataGeneratorAgent": ("#b45309", "#7c2d12"),
        "F3dasmImplementerAgent": ("#15803d", "#14532d"),
        "AdversarialCritiqueAgent": ("#b91c1c", "#7f1d1d"),
        "DebuggerAgent": ("#475569", "#1e293b"),
    }
    _MERMAID_FALLBACK = [
        ("#0f766e", "#134e4a"), ("#a16207", "#713f12"),
        ("#be185d", "#831843"), ("#4338ca", "#312e81"),
    ]

    def to_mermaid(self) -> str:
        """Return a styled Mermaid flowchart for this graph.

        Generated from the live spec: node labels carry each agent's
        class, role, and a short description; nodes are coloured by agent
        class. Edges are drawn by kind, as in the viewer: a Delegate edge is
        a solid arrow, a handoff edge a dashed arrow labelled "handoff"
        (followed by the edge's preamble, if any).
        Paste at https://mermaid.live or any Mermaid-aware renderer
        (GitHub markdown, Jupyter, VS Code).
        """
        def _clean(text: str, n: int = 46) -> str:
            t = " ".join(str(text).split()).replace('"', "'")
            return (t[: n - 1] + "…") if len(t) > n else t

        lines = ["flowchart TD"]
        colours: dict = {}
        members: dict = {}
        fb = 0
        for name in self.nodes:
            agent = self.nodes[name]
            cls = type(agent).__name__
            if cls not in colours:
                if cls in self._MERMAID_COLOURS:
                    colours[cls] = self._MERMAID_COLOURS[cls]
                else:
                    colours[cls] = self._MERMAID_FALLBACK[
                        fb % len(self._MERMAID_FALLBACK)]
                    fb += 1
            members.setdefault(cls, []).append(name)
            desc = _clean(
                getattr(agent, "description", "") or "")
            label = f"<b>{name}</b><br/>{cls}<br/><i>{desc}</i>"
            if name == self.entry:
                lines.append(f'    {name}(["{label}"])')
            else:
                lines.append(f'    {name}["{label}"]')
        for e in self.edges:
            handoff = getattr(e, "handoff", False)
            arrow = "-.->" if handoff else "-->"
            label = _clean(e.preamble, 35) if e.preamble else ""
            if handoff:
                label = f"handoff: {label}" if label else "handoff"
            if label:
                lines.append(
                    f'    {e.source} {arrow}|"{label}"| {e.target}')
            else:
                lines.append(f'    {e.source} {arrow} {e.target}')
        for cls, (fill, stroke) in colours.items():
            lines.append(
                f"    classDef {cls} fill:{fill},stroke:{stroke},"
                "color:#fff,stroke-width:1px;")
        for cls, names in members.items():
            lines.append(f"    class {','.join(names)} {cls}")
        return "\n".join(lines)

    def __repr__(self) -> str:
        out_map: dict[str, list] = {}
        for e in self.edges:
            out_map.setdefault(e.source, []).append(e)
        lines = [f"Graph(entry={self.entry!r})"]
        for name in self.nodes:
            tag = " [entry]" if name == self.entry else ""
            edges = out_map.get(name, [])
            if edges:
                for e in edges:
                    preamble = f"  # {e.preamble!r}" if e.preamble else ""
                    lines.append(f"  {name}{tag}  ──▶  {e.target}{preamble}")
                    tag = ""  # only label first edge row
            else:
                lines.append(f"  {name}{tag}  (leaf)")
        return "\n".join(lines)
nodes: dict instance-attribute #
edges: tuple = () class-attribute instance-attribute #
entry: str = None class-attribute instance-attribute #
_MERMAID_COLOURS = {'StrategizerAgent': ('#1d4ed8', '#1e3a8a'), 'LiteratureReviewAgent': ('#6d28d9', '#4c1d95'), 'DataGeneratorAgent': ('#b45309', '#7c2d12'), 'F3dasmImplementerAgent': ('#15803d', '#14532d'), 'AdversarialCritiqueAgent': ('#b91c1c', '#7f1d1d'), 'DebuggerAgent': ('#475569', '#1e293b')} class-attribute instance-attribute #
_MERMAID_FALLBACK = [('#0f766e', '#134e4a'), ('#a16207', '#713f12'), ('#be185d', '#831843'), ('#4338ca', '#312e81')] class-attribute instance-attribute #
with_node(name: str, agent: Agent) -> Graph #

Return a new Graph with agent under name.

If name already exists, the new agent replaces it and its edges are kept. The original graph is not changed. Add the delegation edges for a new node with :meth:with_edge.

Source code in src/adda/_src/backends/base.py
662
663
664
665
666
667
668
669
670
def with_node(self, name: str, agent: Agent) -> Graph:
    """Return a new Graph with *agent* under *name*.

    If *name* already exists, the new agent replaces it and its edges
    are kept. The original graph is not changed. Add the delegation
    edges for a new node with :meth:`with_edge`.
    """
    return Graph(nodes={**self.nodes, name: agent},
                 edges=self.edges, entry=self.entry)
with_edge(source: str, target: str, preamble: str = '', handoff: bool = False) -> Graph #

Return a new Graph where source may delegate (or, with handoff=True, hand its job) to target.

Both nodes must already exist. An existing edge of the same mode between them is replaced, so its preamble can be changed. The original graph is not changed.

Source code in src/adda/_src/backends/base.py
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
def with_edge(self, source: str, target: str,
              preamble: str = "", handoff: bool = False) -> Graph:
    """Return a new Graph where *source* may delegate (or, with
    ``handoff=True``, hand its job) to *target*.

    Both nodes must already exist. An existing edge of the same mode
    between them is replaced, so its preamble can be changed. The
    original graph is not changed.
    """
    kept = tuple(e for e in self.edges
                 if (e.source, e.target, e.handoff)
                 != (source, target, handoff))
    return Graph(nodes=dict(self.nodes),
                 edges=(*kept, Edge(source, target, preamble, handoff)),
                 entry=self.entry)
outgoing(name: str) -> list[str] #

Return the names name may delegate to (handoff edges excluded).

Source code in src/adda/_src/backends/base.py
688
689
690
691
def outgoing(self, name: str) -> list[str]:
    """Return the names *name* may delegate to (handoff edges excluded)."""
    return [e.target for e in self.edges
            if e.source == name and not e.handoff]
incoming(name: str) -> list[str] #

Return the names that may delegate to name.

Source code in src/adda/_src/backends/base.py
693
694
695
696
def incoming(self, name: str) -> list[str]:
    """Return the names that may delegate to *name*."""
    return [e.source for e in self.edges
            if e.target == name and not e.handoff]
handoff_targets(name: str) -> list[str] #

Return the names name may hand its job to.

Source code in src/adda/_src/backends/base.py
698
699
700
def handoff_targets(self, name: str) -> list[str]:
    """Return the names *name* may hand its job to."""
    return [e.target for e in self.edges if e.source == name and e.handoff]
edge(source: str, target: str, handoff: bool = False) -> Edge | None #

Return the Edge of that mode from source to target, or None.

Source code in src/adda/_src/backends/base.py
702
703
704
705
706
707
708
def edge(self, source: str, target: str,
         handoff: bool = False) -> Edge | None:
    """Return the Edge of that mode from *source* to *target*, or None."""
    for e in self.edges:
        if (e.source, e.target, e.handoff) == (source, target, handoff):
            return e
    return None
to_mermaid() -> str #

Return a styled Mermaid flowchart for this graph.

Generated from the live spec: node labels carry each agent's class, role, and a short description; nodes are coloured by agent class. Edges are drawn by kind, as in the viewer: a Delegate edge is a solid arrow, a handoff edge a dashed arrow labelled "handoff" (followed by the edge's preamble, if any). Paste at https://mermaid.live or any Mermaid-aware renderer (GitHub markdown, Jupyter, VS Code).

Source code in src/adda/_src/backends/base.py
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
def to_mermaid(self) -> str:
    """Return a styled Mermaid flowchart for this graph.

    Generated from the live spec: node labels carry each agent's
    class, role, and a short description; nodes are coloured by agent
    class. Edges are drawn by kind, as in the viewer: a Delegate edge is
    a solid arrow, a handoff edge a dashed arrow labelled "handoff"
    (followed by the edge's preamble, if any).
    Paste at https://mermaid.live or any Mermaid-aware renderer
    (GitHub markdown, Jupyter, VS Code).
    """
    def _clean(text: str, n: int = 46) -> str:
        t = " ".join(str(text).split()).replace('"', "'")
        return (t[: n - 1] + "…") if len(t) > n else t

    lines = ["flowchart TD"]
    colours: dict = {}
    members: dict = {}
    fb = 0
    for name in self.nodes:
        agent = self.nodes[name]
        cls = type(agent).__name__
        if cls not in colours:
            if cls in self._MERMAID_COLOURS:
                colours[cls] = self._MERMAID_COLOURS[cls]
            else:
                colours[cls] = self._MERMAID_FALLBACK[
                    fb % len(self._MERMAID_FALLBACK)]
                fb += 1
        members.setdefault(cls, []).append(name)
        desc = _clean(
            getattr(agent, "description", "") or "")
        label = f"<b>{name}</b><br/>{cls}<br/><i>{desc}</i>"
        if name == self.entry:
            lines.append(f'    {name}(["{label}"])')
        else:
            lines.append(f'    {name}["{label}"]')
    for e in self.edges:
        handoff = getattr(e, "handoff", False)
        arrow = "-.->" if handoff else "-->"
        label = _clean(e.preamble, 35) if e.preamble else ""
        if handoff:
            label = f"handoff: {label}" if label else "handoff"
        if label:
            lines.append(
                f'    {e.source} {arrow}|"{label}"| {e.target}')
        else:
            lines.append(f'    {e.source} {arrow} {e.target}')
    for cls, (fill, stroke) in colours.items():
        lines.append(
            f"    classDef {cls} fill:{fill},stroke:{stroke},"
            "color:#fff,stroke-width:1px;")
    for cls, names in members.items():
        lines.append(f"    class {','.join(names)} {cls}")
    return "\n".join(lines)

adda.Edge dataclass #

A directed edge between two named agents.

Parameters:

Name Type Description Default
source str

Name of the agent that may use the edge.

required
target str

Name of the agent that receives the task or the job.

required
preamble str

Text prepended to the task message when this edge is traversed.

''
handoff bool

False (default): source may Delegate to target and gets a report back. True: source may Handoff its job to target, which then owns it; nothing is reported back. One pair may hold both.

False
Source code in src/adda/_src/backends/base.py
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
@dataclass(frozen=True)
class Edge:
    """A directed edge between two named agents.

    Parameters
    ----------
    source : str
        Name of the agent that may use the edge.
    target : str
        Name of the agent that receives the task or the job.
    preamble : str
        Text prepended to the task message when this edge is traversed.
    handoff : bool
        False (default): ``source`` may ``Delegate`` to ``target`` and gets a
        report back. True: ``source`` may ``Handoff`` its job to ``target``,
        which then owns it; nothing is reported back. One pair may hold both.
    """

    source: str
    target: str
    preamble: str = ""
    handoff: bool = False
source: str instance-attribute #
target: str instance-attribute #
preamble: str = '' class-attribute instance-attribute #
handoff: bool = False class-attribute instance-attribute #

adda.Agent #

Base class for all agentic nodes in a Graph.

Configure a node either by subclassing and overriding the class-level attributes (system_prompt, tools, etc.), or inline with the keyword constructor::

Agent(system_prompt="...", tools={"Read", "Bash"}, role="analyst",
      description="...", model="...")

A keyword that is passed overrides the class attribute on that instance; one that is left out keeps the class attribute.

Tool system — three categories:

tools: frozenset[str] declares tools from two categories:

  1. Native backend tools — names from :data:NATIVE_TOOL_NAMES ("Bash", "Read", "Write", etc.). The runtime passes these to the backend session's native tool executor.

  2. Protocol closure tools — names from :data:PROTOCOL_CLOSURE_NAMES ("Done", "WriteNote", "ReadNote"). The runtime builds Python callables for these and passes them as closure_tools to the session factory.

  3. Topology-injected tools — "Delegate", "Parallel", "Debate", "Retry" (outgoing edges), "Ask" (entry node), and "SendMessage" (every node, regardless of edges). Never declare these in Agent.tools. The runtime injects them automatically from the graph topology; any declaration here is ignored.

  4. External MCP server tools — names declared in extra_allowed_tools (e.g. "arxiv_search_papers"). The runtime passes these to the backend together with mcp_servers, a dict of {server_name: McpStdioServerConfig} that declares which external MCP servers to start.

Default is frozenset() — no tools (opt-in, conservative).

Parameters:

Name Type Description Default
model str or None

Model identifier. None delegates to the backend default.

None
system_prompt str or None

This node's system prompt.

None
tools iterable of str or None

Tool names (stored as a frozenset).

None
role str or None

Deprecated and ignored; the node name identifies a node.

None
description str or None

One-line description shown to nodes that may delegate to it.

None
Source code in src/adda/_src/backends/base.py
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
class Agent:
    """Base class for all agentic nodes in a Graph.

    Configure a node either by subclassing and overriding the class-level
    attributes (``system_prompt``, ``tools``, etc.), or inline with the
    keyword constructor::

        Agent(system_prompt="...", tools={"Read", "Bash"}, role="analyst",
              description="...", model="...")

    A keyword that is passed overrides the class attribute on that instance;
    one that is left out keeps the class attribute.

    **Tool system — three categories:**

    ``tools: frozenset[str]`` declares tools from two categories:

    1. **Native backend tools** — names from :data:`NATIVE_TOOL_NAMES`
       (``"Bash"``, ``"Read"``, ``"Write"``, etc.).  The runtime passes
       these to the backend session's native tool executor.

    2. **Protocol closure tools** — names from
       :data:`PROTOCOL_CLOSURE_NAMES` (``"Done"``, ``"WriteNote"``,
       ``"ReadNote"``).  The runtime builds Python callables for these and
       passes them as ``closure_tools`` to the session factory.

    3. **Topology-injected tools** — ``"Delegate"``, ``"Parallel"``,
       ``"Debate"``, ``"Retry"`` (outgoing edges), ``"Ask"`` (entry node),
       and ``"SendMessage"`` (every node, regardless of edges).  **Never
       declare these in** ``Agent.tools``.  The runtime injects them
       automatically from the graph topology; any declaration here is
       ignored.

    4. **External MCP server tools** — names declared in
       ``extra_allowed_tools`` (e.g.
       ``"arxiv_search_papers"``).  The runtime passes these to the
       backend together with ``mcp_servers``, a dict of
       ``{server_name: McpStdioServerConfig}`` that declares which external
       MCP servers to start.

    Default is ``frozenset()`` — no tools (opt-in, conservative).

    Parameters
    ----------
    model : str or None
        Model identifier.  ``None`` delegates to the backend default.
    system_prompt : str or None
        This node's system prompt.
    tools : iterable of str or None
        Tool names (stored as a ``frozenset``).
    role : str or None
        Deprecated and ignored; the node name identifies a node.
    description : str or None
        One-line description shown to nodes that may delegate to it.
    """

    system_prompt: str = ""
    tools: frozenset[str] = frozenset()
    #: Plain functions listed in ``tools``, by name (see :func:`_split_tools`).
    custom_tools: dict = {}
    reset_on_checkpoint: bool = True
    description: str = ""
    # Declared as a CLASS attribute, like its sibling `backend`, so the
    # documented idiom above ("behavioural differences belong in class
    # attributes") actually works for it. It previously existed only as an
    # instance attribute assigned in __init__, so a subclass setting
    # `model = "..."` had it silently overwritten with None while a subclass
    # setting `backend = "..."` was honoured — the asymmetry produced the
    # worst possible outcome, a node pinned to one backend and left on
    # another backend's default model id.
    model: str | None = None
    backend: str | None = None
    #: Endpoint override (OpenAI-compatible backends); config.yaml sets it per node.
    base_url: str | None = None
    #: ``DEFAULT_PROMPT`` keeps the backend's own system prompt and appends
    #: this node's text to it; None replaces it. config.yaml sets it per node.
    base_prompt: str | None = None
    mcp_servers: dict = {}
    extra_allowed_tools: frozenset[str] = frozenset()
    max_history_pairs: int = 5
    #: This node's tasks are derivations, not evaluation campaigns: the
    #: screen nudge never asks before one, and one done on a design family
    #: counts as that family's screen.
    derivation: bool = False
    #: This node's delegations evaluate the ground-truth oracle, so the
    #: eval-ledger guards apply to them.
    ledgered_evaluations: bool = False
    #: Handbook tags: the menu shows entries with no audience plus those
    #: sharing a tag. Set per class, never derived from the role.
    handbook_audience: tuple[str, ...] = ()
    #: Events that wake this node, by delivery mode (``sync`` or ``async``):
    #: see nodes/subscriptions.py. config.yaml sets it per node.
    subscribes: dict = {}
    report_sections: tuple[str, ...] = (
        "### Actions taken",
        "### Conclusions",
        "### Numbers",
    )

    def __init__(
        self,
        model: str | None = None,
        *,
        system_prompt: str | None = None,
        tools: Iterable[str] | None = None,
        role: str | None = None,
        description: str | None = None,
    ) -> None:
        # Only an EXPLICIT argument overrides the class attribute; assigning
        # unconditionally would re-introduce the silent clobber described
        # above, since the default is indistinguishable from "not passed".
        if model is not None:
            self.model = model
        if system_prompt is not None:
            self.system_prompt = system_prompt
        if tools is not None and isinstance(tools, str):
            raise TypeError(
                "tools must be an iterable of tool names, not a str")
        # A plain function in the set is a custom tool; the names stay in
        # ``tools``. Split here so a class attribute that lists a function
        # works the same as the argument.
        self.tools, self.custom_tools = _split_tools(
            self.tools if tools is None else tools)
        if role is not None:
            warnings.warn(ROLE_DEPRECATED, DeprecationWarning, stacklevel=2)
        if description is not None:
            self.description = description

    def __init_subclass__(cls, **kwargs: Any) -> None:
        super().__init_subclass__(**kwargs)
        if "role" in cls.__dict__:
            warnings.warn(ROLE_DEPRECATED, DeprecationWarning, stacklevel=2)

    def forward(self) -> None:
        """ADAS hook — override for inspectable Python orchestration."""

    def runtime_closures(self, study_dir: Path, **kwargs) -> dict:
        """Every Python tool this agent's adapter receives: the closures of
        :meth:`build_closure_tools` and the plain functions listed in
        ``tools``. The runtime, the run diagram and the viewer all read this
        one method, so a custom tool reaches all three the same way."""
        return {**self.build_closure_tools(study_dir, **kwargs),
                **self.custom_tools}

    def build_closure_tools(
        self,
        study_dir: Path,
        delegation_id: str | None = None,
        lit_reviewer_notes_dir: Path | None = None,
    ) -> dict:
        """Return runtime closure tools for this agent.

        Called by the runtime when constructing the worker adapter so agents can
        inject Python callables (e.g. corpus management tools) without declaring
        them in Agent.tools.

        The default gives EVERY agent read-only lookup against the study's
        persistent literature corpus (ConsultLiterature) —
        the corpus is shared, queryable infrastructure (see LiteratureCorpus),
        the same way QueryStore lets every node read the canonical evaluation
        ledger without delegating to the data generator. ACQUIRING a new paper
        (CorpusAdd, external search) stays literature_reviewer-only: finding
        and vetting a new paper needs judgment a raw tool call can't supply,
        so it stays gated behind an actual delegation — see
        LiteratureReviewAgent.build_closure_tools, which overrides this
        method entirely (the same ConsultLiterature plus CorpusAdd) and
        does not call super().

        Override in a subclass to replace this default entirely.
        """
        from pathlib import Path as _Path

        try:
            from ..literature.literature_corpus import LiteratureCorpus
        except ImportError:
            return {}

        corpus_dir = (
            _Path(lit_reviewer_notes_dir) if lit_reviewer_notes_dir is not None
            else _Path(study_dir) / "runs" / "lit_reviewer_notes"
        )
        corpus = LiteratureCorpus(corpus_dir)
        from ..agents.literature_tools.corpus import build_corpus_read_closures
        return build_corpus_read_closures(corpus)
system_prompt: str = '' class-attribute instance-attribute #
tools: frozenset[str] = frozenset() class-attribute instance-attribute #
custom_tools: dict = {} class-attribute instance-attribute #
reset_on_checkpoint: bool = True class-attribute instance-attribute #
description: str = '' class-attribute instance-attribute #
model: str | None = None class-attribute instance-attribute #
backend: str | None = None class-attribute instance-attribute #
base_url: str | None = None class-attribute instance-attribute #
base_prompt: str | None = None class-attribute instance-attribute #
mcp_servers: dict = {} class-attribute instance-attribute #
extra_allowed_tools: frozenset[str] = frozenset() class-attribute instance-attribute #
max_history_pairs: int = 5 class-attribute instance-attribute #
derivation: bool = False class-attribute instance-attribute #
ledgered_evaluations: bool = False class-attribute instance-attribute #
handbook_audience: tuple[str, ...] = () class-attribute instance-attribute #
subscribes: dict = {} class-attribute instance-attribute #
report_sections: tuple[str, ...] = ('### Actions taken', '### Conclusions', '### Numbers') class-attribute instance-attribute #
forward() -> None #

ADAS hook — override for inspectable Python orchestration.

Source code in src/adda/_src/backends/base.py
516
517
def forward(self) -> None:
    """ADAS hook — override for inspectable Python orchestration."""
runtime_closures(study_dir: Path, **kwargs) -> dict #

Every Python tool this agent's adapter receives: the closures of :meth:build_closure_tools and the plain functions listed in tools. The runtime, the run diagram and the viewer all read this one method, so a custom tool reaches all three the same way.

Source code in src/adda/_src/backends/base.py
519
520
521
522
523
524
525
def runtime_closures(self, study_dir: Path, **kwargs) -> dict:
    """Every Python tool this agent's adapter receives: the closures of
    :meth:`build_closure_tools` and the plain functions listed in
    ``tools``. The runtime, the run diagram and the viewer all read this
    one method, so a custom tool reaches all three the same way."""
    return {**self.build_closure_tools(study_dir, **kwargs),
            **self.custom_tools}
build_closure_tools(study_dir: Path, delegation_id: str | None = None, lit_reviewer_notes_dir: Path | None = None) -> dict #

Return runtime closure tools for this agent.

Called by the runtime when constructing the worker adapter so agents can inject Python callables (e.g. corpus management tools) without declaring them in Agent.tools.

The default gives EVERY agent read-only lookup against the study's persistent literature corpus (ConsultLiterature) — the corpus is shared, queryable infrastructure (see LiteratureCorpus), the same way QueryStore lets every node read the canonical evaluation ledger without delegating to the data generator. ACQUIRING a new paper (CorpusAdd, external search) stays literature_reviewer-only: finding and vetting a new paper needs judgment a raw tool call can't supply, so it stays gated behind an actual delegation — see LiteratureReviewAgent.build_closure_tools, which overrides this method entirely (the same ConsultLiterature plus CorpusAdd) and does not call super().

Override in a subclass to replace this default entirely.

Source code in src/adda/_src/backends/base.py
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
def build_closure_tools(
    self,
    study_dir: Path,
    delegation_id: str | None = None,
    lit_reviewer_notes_dir: Path | None = None,
) -> dict:
    """Return runtime closure tools for this agent.

    Called by the runtime when constructing the worker adapter so agents can
    inject Python callables (e.g. corpus management tools) without declaring
    them in Agent.tools.

    The default gives EVERY agent read-only lookup against the study's
    persistent literature corpus (ConsultLiterature) —
    the corpus is shared, queryable infrastructure (see LiteratureCorpus),
    the same way QueryStore lets every node read the canonical evaluation
    ledger without delegating to the data generator. ACQUIRING a new paper
    (CorpusAdd, external search) stays literature_reviewer-only: finding
    and vetting a new paper needs judgment a raw tool call can't supply,
    so it stays gated behind an actual delegation — see
    LiteratureReviewAgent.build_closure_tools, which overrides this
    method entirely (the same ConsultLiterature plus CorpusAdd) and
    does not call super().

    Override in a subclass to replace this default entirely.
    """
    from pathlib import Path as _Path

    try:
        from ..literature.literature_corpus import LiteratureCorpus
    except ImportError:
        return {}

    corpus_dir = (
        _Path(lit_reviewer_notes_dir) if lit_reviewer_notes_dir is not None
        else _Path(study_dir) / "runs" / "lit_reviewer_notes"
    )
    corpus = LiteratureCorpus(corpus_dir)
    from ..agents.literature_tools.corpus import build_corpus_read_closures
    return build_corpus_read_closures(corpus)

adda.Node #

One node in the agent graph.

inspect.getsource(Node._orchestrate) reads the routing logic every node runs.

Parameters:

Name Type Description Default
adapter Any

The LLM adapter this node speaks through.

required
name str

The node's name in the graph. This is what distinguishes one agent from another — there is no per-agent node class.

'node'
outgoing list[str] | tuple[str, ...]

Names of the nodes this one may delegate to. Empty means Delegate() has nowhere to send work — the only thing that differs structurally.

()
spec Any

The whole :class:~..backends.base.Graph, so a node can read its peers' roles and descriptions.

None
report_sections tuple[str, ...] | None

This agent's own declared report contract and toolset. agent_tools gates which capability closures :meth:_init_capabilities grants; report_sections is retained for callers that validate a worker's report against its own contract (parsing._classify_response).

None
agent_tools tuple[str, ...] | None

This agent's own declared report contract and toolset. agent_tools gates which capability closures :meth:_init_capabilities grants; report_sections is retained for callers that validate a worker's report against its own contract (parsing._classify_response).

None
Source code in src/adda/_src/nodes/node.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
class Node(
    RecordingMixin,
    ReviewRecordsMixin,
    LifecycleMixin,
    ReproductionGateMixin,
    OrchestrationMixin,
    StopMixin,
    TimeRulesMixin,
    WindDownMixin,
):
    """One node in the agent graph.

    ``inspect.getsource(Node._orchestrate)`` reads the routing logic every
    node runs.

    Parameters
    ----------
    adapter
        The LLM adapter this node speaks through.
    name
        The node's name in the graph. This is what distinguishes one agent
        from another — there is no per-agent node class.
    outgoing
        Names of the nodes this one may delegate to. Empty means ``Delegate()``
        has nowhere to send work — the only thing that differs structurally.
    spec
        The whole :class:`~..backends.base.Graph`, so a node can read its
        peers' roles and descriptions.
    report_sections, agent_tools
        This agent's own declared report contract and toolset. ``agent_tools``
        gates which capability closures :meth:`_init_capabilities` grants;
        ``report_sections`` is retained for callers that validate a worker's
        report against its own contract (``parsing._classify_response``).
    """

    # A nudge keeps its historical attribute; the state lives in the
    # node's Nudges (nodes/nudge.py), which records it and survives a resume.
    @property
    def _milestone_ack(self) -> set:
        from .nudge import for_node
        return for_node(self).keys("milestone_backlog")

    @_milestone_ack.setter
    def _milestone_ack(self, value: set) -> None:
        from .nudge import for_node
        reg = for_node(self)
        for key in reg.keys("milestone_backlog") - set(value or ()):
            reg.forget("milestone_backlog", key, record=False)


    _awake_slots = None  # run-wide AwakeSlots, set by graph_builder
    _topology = None  # run-wide Topology (runtime_topology knob), set by graph_builder

    def __init__(
        self,
        adapter: Any,
        *,
        name: str = "node",
        outgoing: list[str] | tuple[str, ...] = (),
        spec: Any = None,
        study_dir: Any = None,
        interactive: bool = False,
        max_ask: int = 1,
        worker_adapters: dict | None = None,
        notes_dir: Any = None,
        workspace_dir: Any = None,
        delegation_log: DelegationLog | None = None,
        report_sections: tuple[str, ...] | None = None,
        agent_tools: frozenset[str] | None = None,
    ) -> None:
        self.adapter = adapter
        self.adapter.on_retry = self._record_llm_retry
        self._name = name
        self._outgoing = list(outgoing)
        self._spec = spec
        self._init_recording()
        # The backend checks this run's flag before each model and tool call,
        # so a thread of an abandoned run stops even after a later run in the
        # same process clears the process-wide signal. A delegation's
        # adapter copy keeps the same flag.
        self.adapter.abandon_event = self._abandon
        # The entry node holds no delegation id; its pending notices (the
        # messages its workers send it) are kept under "entry", and its
        # backend's post-tool hook drains that key on every tool call.
        self.adapter.notice_key = (
            "entry" if getattr(spec, "entry", None) == name else None)
        # Universal capability setup — every node's adapter, regardless of
        # whether it has outgoing edges (see the module docstring for why
        # this matters even for a node that never takes a turn of its own).
        self._init_capabilities(
            study_dir=study_dir, workspace_dir=workspace_dir,
            delegation_log=delegation_log,
            report_sections=report_sections, agent_tools=agent_tools,
        )
        # Delegation registry, ledgers and routing closures. Harmless for a
        # node with no outgoing edges (Delegate() simply has no valid
        # target); epistemic OWNERSHIP still requires outgoing edges (see
        # _init_orchestration / _install_epistemics) — a leaf never acquires
        # the ledgers just because notes_dir was passed to every node.
        self._init_orchestration(
            name=name, outgoing=self._outgoing, spec=spec,
            study_dir=study_dir, interactive=interactive, max_ask=max_ask,
            worker_adapters=worker_adapters, notes_dir=notes_dir,
            workspace_dir=workspace_dir, delegation_log=delegation_log,
        )
        # Plain Claude Code (Default tools + Default prompt) is handed no
        # adda closure at all, whatever the topology would otherwise grant.
        from ..runtime.node_tools import is_plain_default
        #: A plain Claude Code node is run by its own session: adda sends it
        #: the statement and nothing after (no notice, no re-prompt, no
        #: wind-down). The backend ends it at a budget (``token_clock``).
        self._silent = is_plain_default(
            spec.nodes.get(name) if spec is not None else None)
        if self._silent:
            self.adapter.closure_tools.clear()

    def __call__(self, state: AgenticState) -> Any:
        """Take one turn. Every node runs the same delegate-or-close loop."""
        return self._orchestrate(state)

    def _raise_if_abandoned(self) -> None:
        """End the calling thread once the run has stopped waiting for it."""
        raise_if_stopped(self._name, self._abandon)

    def _init_recording(self) -> None:
        """Establish the state ``RecordingMixin`` writes to, on EVERY node.

        Recording is a property of a node — any node's tools can return an
        ERROR, and any node's adapter reports token usage — so its substrate
        belongs here rather than in one behaviour's setup. It used to be
        created only by the orchestration setup, which was invisible while
        leaves were a separate class that did not carry RecordingMixin at all:
        the moment a leaf reached any recording call it raised AttributeError
        on ``_registry_lock``. The orchestration setup still assigns these
        itself, to the same values, so an orchestrating node is unchanged.
        """
        self._registry_lock = threading.Lock()
        self._abandon = threading.Event()
        self._notifications: list[str] = []
        self._notifications_lock = threading.Lock()
        self._error_counts: dict[str, int] = {}
        self._token_totals: dict = {
            "input_tokens": 0,
            "output_tokens": 0,
            "cache_read_input_tokens": 0,
            "cache_creation_input_tokens": 0,
            # The backend-independent schema (infra/telemetry.py): `legacy_calls`
            # counts calls whose backend reported none of it.
            "fresh_input": 0,
            "cache_read": 0,
            "cache_write": 0,
            "output": 0,
            "normalized_calls": 0,
            "legacy_calls": 0,
            "total_cost_usd": 0.0,
        }
        self._cost_observed: bool = False
        # list-price spend of calls the SDK sent no cost for (the strategizer)
        self._estimated_cost_usd: float = 0.0
        self._current_notes_dir: Path | None = None
        self._telemetry: Any = None

    def _init_capabilities(
        self,
        *,
        study_dir: Any = None,
        workspace_dir: Any = None,
        delegation_log: DelegationLog | None = None,
        report_sections: tuple[str, ...] | None = None,
        agent_tools: frozenset[str] | None = None,
    ) -> None:
        """Wire this node's sandboxed Write and its declared read-only tools.

        Runs for EVERY node, not only one reachable as a standalone entry
        point — see the module docstring: whichever node ends up as someone
        else's ``Delegate()`` target is dispatched through a copy of THIS adapter
        (``adapter.copy()`` carries the ``closure_tools`` set here), so this is where a
        dispatched specialist's baseline capabilities actually come from,
        before ``WorkerSession.install_worker_tools``/``_sandbox_worker_writes``
        layer the per-delegation overrides (ReportEvals's own record callback,
        a delegation-scoped Write) on top.
        """
        self._study_dir = study_dir
        self._workspace_dir = Path(workspace_dir) if workspace_dir else None
        # The agent's declared tools — the single source of truth for which
        # capability closures this node is granted (read-only ledger/store
        # tools). Kept as a frozenset for membership checks.
        self._agent_tools: frozenset[str] = frozenset(agent_tools or ())
        # A node built without a declared toolset has nothing to narrow by.
        self._tools_declared = agent_tools is not None
        # This agent's declared report sections (e.g. the critic's
        # Findings/Verdict, not the implementer's Conclusions/Files touched).
        # Used to validate a worker's report against ITS OWN contract instead
        # of the implementer-shaped default — otherwise a correct critic or
        # literature report is wrongly flagged malformed (audit BF-10/O40).
        self._report_sections = report_sections
        self._delegation_log = delegation_log
        self._evals_reported: dict = {}
        self._setup_sandboxed_write()
        self.adapter.closure_tools.update(self._build_eval_closures())
        if delegation_log is not None:
            from .tools.routing import build_recall_history
            self.adapter.closure_tools["RecallHistory"] = build_recall_history(self)
        # Declaration-gated read-only ledger/store tools — the SAME builder
        # every node uses, so a specialist (e.g. the critic) gets an
        # identical, working QueryStore/HypothesisList
        # surface whenever it declares them. Resolves the run via the shared
        # Node._resolve_run_dir (delegation-log path).
        from ..runtime import features as _features
        from .tools.routing import build_declared_shared_closures
        self.adapter.closure_tools.update(
            build_declared_shared_closures(
                self, self._agent_tools - _features.disabled_tool_names()))
        # Closures from Agent.build_closure_tools() (ConsultLiterature,
        # CorpusAdd, ...) are installed on the adapter BEFORE this node
        # exists (agent_runtime.py), so they never pass through
        # orchestration.py's _wrap_closure the way routing tools do. A
        # closure tagged _adda_diagnostic_source (a fact about the run
        # environment a tool discovers, not about one call's return value —
        # see LiteratureCorpus.pop_diagnostic_event) needs that wrapper
        # regardless, so re-wrap it here, once, by the tag rather than by
        # name — any future tool needing the same reporting just carries the
        # same tag.
        for _cname, _cfn in list(self.adapter.closure_tools.items()):
            if getattr(_cfn, "_adda_diagnostic_source", None) is not None:
                self.adapter.closure_tools[_cname] = self._wrap_closure(
                    _cfn, self._name)
        # A config.yaml `nodes.<name>.tools` list is the whole tool set: the
        # always-on closures it does not name are withheld.
        from ..runtime.node_tools import withheld_closures
        _agent = self._spec.nodes.get(self._name) if self._spec is not None else None
        for _cname in withheld_closures(_agent):
            self.adapter.closure_tools.pop(_cname, None)

    def unit_folder(self, unit_id: str) -> Path | None:
        """``debug/delegations/<unit_id>/``: the folder of one unit of work
        (a delegation D###, a tenure X###, a wake W###), created here."""
        if self._workspace_dir is None:
            return None
        folder = self._workspace_dir / unit_id
        folder.mkdir(parents=True, exist_ok=True)
        return folder

    def scope_write_to(self, adapter: Any, unit_id: str) -> None:
        """Confine ``adapter``'s Write to this unit's folder, as a delegated
        worker's is: one rule for every unit of work. No-op when the node
        has no sandboxed Write."""
        if "Write" not in adapter.closure_tools:
            return
        folder = self.unit_folder(unit_id)
        if folder is None:
            return
        from .tools.routing import build_sandboxed_write
        study = Path(self._study_dir) if self._study_dir is not None else None
        adapter.closure_tools["Write"] = build_sandboxed_write(
            folder.resolve(), strip_prefix=unit_id,
            scope_label=f"{unit_id}/ ({folder.resolve()})",
            study_workspace=study / "workspace" if study else None,
            deliverable_dir=study,
            deliverable_names=lambda: getattr(
                self, "_required_deliverables", None) or [])

    def _setup_sandboxed_write(self) -> None:
        """Replace native Write with a workspace-sandboxed closure.

        Removes 'Write' from native_tools so the SDK doesn't expose it,
        then installs the same builder a dispatched worker gets
        (:func:`build_sandboxed_write`), scoped to this node's whole
        workspace instead of one delegation's subfolder — a node reached via
        real graph routing (the module docstring) has no delegation_id of
        its own to scope tighter than that.

        Bash is kept native but cwd is already set to study_dir by the adapter;
        the prompt further constrains it to the workspace.
        """
        if self._workspace_dir is None:
            return  # no sandboxing if study_dir unknown (e.g. tests)
        from ..runtime.node_tools import withheld_closures
        if "Write" in withheld_closures(
                self._spec.nodes.get(self._name) if self._spec is not None else None):
            return  # a Default node keeps the backend's native Write

        # Remove native Write so the SDK doesn't expose an unrestricted version
        if hasattr(self.adapter, "native_tools") and "Write" in self.adapter.native_tools:
            self.adapter.native_tools = [
                t for t in self.adapter.native_tools if t != "Write"
            ]

        from .tools.routing import build_sandboxed_write

        # Bound to a plain name (not inlined into the assignment) so
        # dev/tools/promptmap.py's injected_tool_docs() scanner — which
        # resolves a closure_tools["Write"] = <name> rebind to the function
        # <name> refers to — can still find this tool's docstring after the
        # move into the shared builder.
        _study_ws = (
            Path(self._study_dir) / "workspace"
            if self._study_dir is not None else None
        )
        Write = build_sandboxed_write(
            self._workspace_dir, study_workspace=_study_ws,
            deliverable_dir=(
                Path(self._study_dir) if self._study_dir is not None else None),
            deliverable_names=lambda: getattr(
                self, "_required_deliverables", None) or [])
        self.adapter.closure_tools["Write"] = Write

    def _build_eval_closures(self) -> dict:
        from .tools.routing import build_report_evals

        evals = self._evals_reported
        return {
            "ReportEvals": build_report_evals(
                record=lambda n: evals.__setitem__("count", n)
            )
        }

    # ── Run-context resolution (shared by every node) ────────────────────────
    # The read tools (QueryStore/HypothesisList) may be granted
    # to any node — the entry node, a mid-tier delegating node, or a leaf such
    # as the critic. They need the run's store/ledger paths, which are resolved
    # here so the tools work identically wherever they are granted.
    def _commit_workspace(self, message: str) -> str | None:
        """Commit the run workspace and return the sha (spec 11).

        On Node rather than on WorkerSession because a delegation is recorded
        from several places, not one: a worker finishing (ok or error), a
        tenure closing (routing/handoff.py), and the close-time
        reconciliation of a delegation still running when the run ended. Every
        one of them appends a row to the delegation log, so every one of them
        owes that row a sha — otherwise ``workspace_sha: null`` means both
        "this delegation changed nothing" and "nobody looked", which is the
        ambiguity --allow-empty exists to prevent.

        The interrupted case is the one with teeth: a delegation killed
        mid-flight never reaches _finish_ok/_finish_error, so its partial file
        writes stay uncommitted and are swept into whichever delegation
        commits NEXT — attributing one delegation's work to another. Silence
        would be better than that; a commit is better still.

        Never raises — see infra/workspace_vcs.
        """
        run_dir = self._resolve_run_dir()
        if run_dir is None:
            return None
        from ..infra.workspace_vcs import commit_workspace
        return commit_workspace(run_dir / "debug" / "delegations", message)

    def _resolve_run_dir(self) -> Path | None:
        """Best-effort run_dir, valid on any node.

        Every node's ``_current_notes_dir`` is set at construction (from
        ``notes_dir``, passed to every node — see ``graph_builder.py``) and
        re-pointed each turn by ``_absorb_state`` if the run's real notes dir
        differs. It can still be ``None`` (e.g. a bare ``Node()`` built
        without one, as in unit tests) — every node DOES hold the shared
        delegation log at run_dir/debug/delegation_log.jsonl, so derive
        run_dir from that when the notes dir is unavailable.
        """
        notes = getattr(self, "_current_notes_dir", None)
        if notes is not None:
            return Path(notes).parent.parent
        dlog = getattr(self, "_delegation_log", None)
        p = getattr(dlog, "_path", None)
        return Path(p).parent.parent if p is not None else None

    def _read_ledger(self) -> HypothesisLedger | None:
        """The hypothesis ledger for READ access, resolved on any node.

        Returns the node's own bound ledger when it has one (the entry node);
        otherwise resolves a read-only view from the run's strategizer_notes
        when hypotheses.json exists. HypothesisLedger.__init__ performs no I/O,
        and only READ callers use this, so there is no write race with the
        entry node that owns the file.
        """
        own = getattr(self, "_ledger", None)
        if own is not None:
            return own
        rd = self._resolve_run_dir()
        if rd is None:
            return None
        notes = rd / "debug" / "strategizer_notes"
        if (notes / "hypotheses.json").exists():
            from ..epistemics.hypothesis_ledger import HypothesisLedger
            return HypothesisLedger.for_dir(notes)
        return None
_current_run_dir property #

This run's root, derived from the notes dir the graph state sets.

run_dir arrives on the state, not on the node, and only _current_notes_dir (<run>/debug/strategizer_notes) is kept from it — so the run root is that path's grandparent. None before the first invoke, which callers must tolerate.

LEDGER_WRITES = ('HypothesisPropose', 'HypothesisUpdate', 'MilestoneSet') class-attribute instance-attribute #
_milestone_ack: set property writable #
_awake_slots = None class-attribute instance-attribute #
_topology = None class-attribute instance-attribute #
adapter = adapter instance-attribute #
_name = name instance-attribute #
_outgoing = list(outgoing) instance-attribute #
_spec = spec instance-attribute #
_silent = is_plain_default(spec.nodes.get(name) if spec is not None else None) instance-attribute #
_wind_down_entry() -> Any | None #
Source code in src/adda/_src/nodes/wind_down.py
121
122
123
124
def _wind_down_entry(self) -> Any | None:
    entry = self._time_entry()
    return entry if entry is not None and (
        entry._wd_started_at is not None) else None
_wind_down_active() -> bool #
Source code in src/adda/_src/nodes/wind_down.py
126
127
def _wind_down_active(self) -> bool:
    return self._wind_down_entry() is not None
_wind_down_record(**fields: Any) -> None #

Merge fields into debug/wind_down.json (atomic).

Source code in src/adda/_src/nodes/wind_down.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def _wind_down_record(self, **fields: Any) -> None:
    """Merge ``fields`` into ``debug/wind_down.json`` (atomic)."""
    run_dir = self._current_run_dir
    if run_dir is None:
        return
    path = Path(run_dir) / "debug" / RECORD_FILE
    try:
        have = json.loads(path.read_text(encoding="utf-8"))
    except (OSError, ValueError):
        have = {}
    have.update(fields)
    tmp = path.with_suffix(f".{os.getpid()}.tmp")
    try:
        path.parent.mkdir(parents=True, exist_ok=True)
        tmp.write_text(json.dumps(have, indent=2), encoding="utf-8")
        tmp.replace(path)
    except OSError:
        pass
_wind_down_begin(prog: _rules.Progress) -> None #

Entry node, once: freeze, tell every node, arm the interrupt.

Source code in src/adda/_src/nodes/wind_down.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
def _wind_down_begin(self, prog: _rules.Progress) -> None:
    """Entry node, once: freeze, tell every node, arm the interrupt."""
    with self._time_lock:
        if self._wd_started_at is not None:
            return
        self._wd_started_at = _rules.now()
    rules = self._time_rules
    limit = int(settings.get_float("wind_down_tool_calls", 50))
    wind_down.begin(limit)
    if self._current_run_dir is not None:
        wind_down.publish_eval_stop(
            Path(self._current_run_dir) / "debug", self._wd_started_at)
    fields = {"budget": rules.budget_phrase(), "limit": limit}
    owner = self._owner_node()
    with owner._notifications_lock:
        owner._notifications.append("[" + (
            HUB_NOTICE if owner._holds("Done") else
            HUB_NOTICE_HANDOFF if owner._holds("Handoff") else WORKER_NOTICE
        ).format(**fields) + "]")
    run_dir = self._current_run_dir
    live: list[str] = []
    if run_dir is not None:
        for n in self._stop_nodes():
            with n._registry_lock:
                ids = [d for d, e in n._registry.items()
                       if e.get("status") in _LIVE]
            for did in ids:
                pending_notices.post(
                    Path(run_dir) / "debug", did,
                    "[" + WORKER_NOTICE.format(**fields) + "]")
            live.extend(ids)
    after = settings.get_float("wind_down_interrupt_after_s", 300)
    t = threading.Timer(after, self._wind_down_interrupt_timer)
    t.daemon = True
    self._time_timers.append(t)
    t.start()
    self._wind_down_record(
        started_at=self._wd_started_at,
        elapsed_at_start=round(_rules.now() - self._run_start, 1),
        budget_trigger=prog.driver, budget_s=rules.wall,
        token_budget=rules.tokens, eval_budget=rules.evals,
        progress=prog.by_kind(),
        tool_call_limit=limit,
        running_at_start=sorted(live), forced_turns=0, interrupted=[])
_wind_down_interrupt_timer() -> None #
Source code in src/adda/_src/nodes/wind_down.py
197
198
199
200
201
202
203
204
def _wind_down_interrupt_timer(self) -> None:
    try:
        if not self._time_closed and not wind_down.suspended():
            self._wind_down_interrupt("not finished "
                f"{settings.get_float('wind_down_interrupt_after_s', 300):g}"
                " s after the wind-down began")
    except Exception:  # noqa: BLE001
        pass
_wind_down_interrupt(why: str) -> list[dict] #

SIGINT the work in flight that each node owns. Recorded, never silent; the nodes continue with "save results".

Source code in src/adda/_src/nodes/wind_down.py
206
207
208
209
210
211
212
213
214
215
216
217
def _wind_down_interrupt(self, why: str) -> list[dict]:
    """SIGINT the work in flight that each node owns. Recorded, never
    silent; the nodes continue with "save results"."""
    hit = interrupt.interrupt_all()
    if hit:
        self._record_intervention(
            "WIND_DOWN_INTERRUPT", "(run)",
            f"{why}; SIGINT to {[(h['pid'], h['name']) for h in hit]}",
            fault="observation")
        have = self._wind_down_read().get("interrupted", [])
        self._wind_down_record(interrupted=[*have, *hit])
    return hit
_wind_down_read() -> dict #
Source code in src/adda/_src/nodes/wind_down.py
219
220
221
222
223
224
225
226
227
def _wind_down_read(self) -> dict:
    run_dir = self._current_run_dir
    if run_dir is None:
        return {}
    try:
        return json.loads((Path(run_dir) / "debug" / RECORD_FILE)
                          .read_text(encoding="utf-8"))
    except (OSError, ValueError):
        return {}
_wind_down_route(state: Any, ai_msg: Any) -> Any | None #

The entry node's turn ended during the wind-down: say which step comes next, at most wind_down_turns times per step. Returns a Command that re-prompts, or None when the turn is to be routed as usual (the close was accepted).

Source code in src/adda/_src/nodes/wind_down.py
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
def _wind_down_route(self, state: Any, ai_msg: Any) -> Any | None:
    """The entry node's turn ended during the wind-down: say which step
    comes next, at most ``wind_down_turns`` times per step. Returns a
    ``Command`` that re-prompts, or None when the turn is to be routed as
    usual (the close was accepted)."""
    if self._wind_down_entry() is None:
        return None
    from langchain_core.messages import HumanMessage
    from langgraph.types import Command

    from .tools.routing.feedback import FeedbackTools

    k = int(settings.get_float("wind_down_turns", 2))

    def _say(text: str, *, forced: bool = True) -> Command:
        if forced:
            self._wd_turns += 1
            self._wind_down_record(forced_turns=self._wd_turns)
        return Command(goto=self._name, update=self._thread_update(
            [ai_msg, HumanMessage(content=text)]))

    if self._awaiting_retro:
        FeedbackTools(self)._capture_retrospective(
            str(ai_msg.content), "")
        return None
    if self._route.get("kind") == "done":
        return None
    working = self._pending_delegations()
    if working:
        n = self._wd_step_turns["wait"] = self._wd_step_turns["wait"] + 1
        if n >= 2:
            self._wind_down_interrupt(
                "one wind-down turn spent waiting for " + ", ".join(working))
        return _say(WAIT_TURN.format(
            n=n, ids=working,
            tail=WAIT_TAIL_INTERRUPT if n >= 2 else ""))
    if not self._wd_save_asked:
        self._wd_save_asked = True
        return _say(SAVE_TURN)
    n = self._wd_step_turns["done"] = self._wd_step_turns["done"] + 1
    if n <= k:
        return _say((DONE_TURN if self._holds("Done") else
                     DONE_TURN_HANDOFF if self._holds("Handoff") else
                     DONE_TURN_NO_DONE).format(n=n, k=k))
    text = FeedbackTools(self)._close_wound_down(str(ai_msg.content), "")
    if not self._holds("Done"):
        text = _without_done(text)
    return _say(text, forced=False)
_wind_down_finish() -> None #

At the close: the facts run_status.json reports.

Source code in src/adda/_src/nodes/wind_down.py
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
def _wind_down_finish(self) -> None:
    """At the close: the facts ``run_status.json`` reports."""
    if self._wind_down_entry() is None:
        return
    missing = self._missing_deliverables({
        "study_dir": str(self._study_dir or "."),
        "required_deliverables": getattr(
            self, "_required_deliverables", None) or []})
    if missing:
        self._record_science_drift({
            "error_type": "DELIVERABLES_MISSING", "missing": missing})
    self._wind_down_record(
        deliverables_missing=missing, forced_turns=self._wd_turns,
        tool_calls=wind_down.calls(),
        closed_at=time.time())
_time_entry() -> Any | None #

The node that holds the run clock, from any node.

Source code in src/adda/_src/nodes/time_rules.py
31
32
33
34
35
36
def _time_entry(self) -> Any | None:
    """The node that holds the run clock, from any node."""
    for n in self._stop_nodes():
        if n._run_start is not None and n._time_rules_ensure() is not None:
            return n
    return None
_time_rules_ensure() -> Any | None #

The rules for this node's clock, built once from the settings.

Source code in src/adda/_src/nodes/time_rules.py
38
39
40
41
42
43
44
45
def _time_rules_ensure(self) -> Any | None:
    """The rules for this node's clock, built once from the settings."""
    if (self._time_rules is None and self._run_start is not None
            and not self._silent and self._is_entry_thread()):
        self._time_rules = _rules.TimeRules.from_settings(
            self._budget_seconds, token_clock.budget(),
            getattr(self, "_eval_budget", None))
    return self._time_rules
_time_progress(entry: Any) -> _rules.Progress #

How far each budget that is set has run: wall seconds since the run start, output tokens generated by every node, and evaluations in the canonical store.

Source code in src/adda/_src/nodes/time_rules.py
47
48
49
50
51
52
53
54
55
def _time_progress(self, entry: Any) -> _rules.Progress:
    """How far each budget that is set has run: wall seconds since the run
    start, output tokens generated by every node, and evaluations in the
    canonical store."""
    rules = entry._time_rules
    return rules.progress(
        _rules.now() - entry._run_start,
        token_clock.used(),
        constraint_snapshot.evals_for_node(entry) if rules.evals else 0)
_time_rules_start() -> None #

Entry node, once per run: read the rules and arm what each budget needs: one timer per threshold on the wall budget, a listener on the token count, a poll of the store on the evaluation count.

Source code in src/adda/_src/nodes/time_rules.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def _time_rules_start(self) -> None:
    """Entry node, once per run: read the rules and arm what each budget
    needs: one timer per threshold on the wall budget, a listener on the
    token count, a poll of the store on the evaluation count."""
    if self._time_armed:
        return
    rules = self._time_rules_ensure()
    if rules is None:
        return
    self._time_armed = True
    if rules.tokens:
        token_clock.on_change(self._time_tokens_changed)
    if rules.evals:
        self._time_poll_arm()
    if rules.wall:
        self._time_wall_arm()
        # A usage-limit pause stops the working clock; the timers armed
        # on real time are re-armed when it ends.
        from ..infra import usage_pause
        usage_pause.on_resume(lambda _s: self._time_wall_rearm())
_time_wall_arm() -> None #

One timer per wall threshold not fired yet, on the working clock.

Source code in src/adda/_src/nodes/time_rules.py
78
79
80
81
82
83
84
85
86
def _time_wall_arm(self) -> None:
    """One timer per wall threshold not fired yet, on the working clock."""
    rules = self._time_rules
    for phase, _frac in rules.schedule():
        if phase in self._time_fired:
            continue
        delay = self._run_start + rules.wall_at(phase) - _rules.now()
        self._time_timer_start(max(delay, 0.0) + 0.2,
                               self._time_timer_fire)
_time_wall_rearm() -> None #
Source code in src/adda/_src/nodes/time_rules.py
88
89
90
91
92
93
94
95
96
97
def _time_wall_rearm(self) -> None:
    if self._time_closed or self._time_rules is None:
        return
    with self._time_lock:
        timers, self._time_timers = self._time_timers, []
    for t in timers:
        t.cancel()
    self._time_wall_arm()
    if self._time_rules.evals:
        self._time_poll_arm()
_time_timer_start(delay: float, fn: Any) -> None #
Source code in src/adda/_src/nodes/time_rules.py
 99
100
101
102
103
104
105
def _time_timer_start(self, delay: float, fn: Any) -> None:
    t = threading.Timer(delay, fn)
    t.daemon = True
    with self._time_lock:
        self._time_timers = [x for x in self._time_timers if x.is_alive()]
        self._time_timers.append(t)
    t.start()
_time_poll_arm() -> None #
Source code in src/adda/_src/nodes/time_rules.py
107
108
def _time_poll_arm(self) -> None:
    self._time_timer_start(EVAL_POLL_S, self._time_poll)
_time_poll() -> None #

Read the evaluation count and tick; look again until the wind-down has begun.

Source code in src/adda/_src/nodes/time_rules.py
110
111
112
113
114
115
116
117
def _time_poll(self) -> None:
    """Read the evaluation count and tick; look again until the
    wind-down has begun."""
    if self._time_closed:
        return
    self._time_timer_fire()
    if _rules.WIND_DOWN not in self._time_fired and not self._time_closed:
        self._time_poll_arm()
_time_tokens_changed() -> None #

A model call ended on some thread: if it crossed a threshold, fire it on its own thread so that thread's model call is not held up.

Source code in src/adda/_src/nodes/time_rules.py
119
120
121
122
123
124
125
126
127
128
129
130
def _time_tokens_changed(self) -> None:
    """A model call ended on some thread: if it crossed a threshold, fire
    it on its own thread so that thread's model call is not held up."""
    rules = self._time_rules
    if rules is None or self._time_closed:
        return
    with self._time_lock:
        new = [p for p in rules.due(self._time_progress(self).fraction)
               if p not in self._time_fired]
    if new:
        t = threading.Thread(target=self._time_timer_fire, daemon=True)
        t.start()
_time_timer_fire() -> None #
Source code in src/adda/_src/nodes/time_rules.py
132
133
134
135
136
def _time_timer_fire(self) -> None:
    try:
        self._time_rules_tick()
    except Exception:  # noqa: BLE001
        pass
_time_rules_cancel() -> None #

The run is over: no timer may act on a later run in this process.

Source code in src/adda/_src/nodes/time_rules.py
138
139
140
141
142
143
144
145
146
def _time_rules_cancel(self) -> None:
    """The run is over: no timer may act on a later run in this process."""
    self._time_closed = True
    token_clock.remove_listener(self._time_tokens_changed)
    wind_down.end()
    with self._time_lock:
        timers, self._time_timers = self._time_timers, []
    for t in timers:
        t.cancel()
_owner_node() -> Any #

The live node that owns the job (this one when it cannot tell): the run clock lives on the entry node, its notices go to the owner.

Source code in src/adda/_src/nodes/time_rules.py
148
149
150
151
152
153
154
155
def _owner_node(self) -> Any:
    """The live node that owns the job (this one when it cannot tell):
    the run clock lives on the entry node, its notices go to the owner."""
    owner = self._job_owner()
    for n in self._stop_nodes():
        if n._name == owner:
            return n
    return self
_time_elapsed() -> _rules.Progress | None #
Source code in src/adda/_src/nodes/time_rules.py
157
158
159
160
161
def _time_elapsed(self) -> _rules.Progress | None:
    entry = self._time_entry()
    if entry is None or entry._run_start is None:
        return None
    return self._time_progress(entry)
_time_rules_tick() -> None #

Fire every threshold the clock has passed, once. Idempotent.

When several are due at once (a late checkpoint, a resume) only the latest fires: its notice states the rule that holds now.

Source code in src/adda/_src/nodes/time_rules.py
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
def _time_rules_tick(self) -> None:
    """Fire every threshold the clock has passed, once. Idempotent.

    When several are due at once (a late checkpoint, a resume) only the
    latest fires: its notice states the rule that holds now.
    """
    entry = self._time_entry()
    if entry is None:
        return
    if entry is not self:
        entry._time_rules_tick()
        return
    rules = self._time_rules_ensure()
    if rules is None or self._time_closed:
        return
    prog = self._time_progress(self)
    with self._time_lock:
        due = [p for p in rules.due(prog.fraction)
               if p not in self._time_fired]
        self._time_fired.update(due)
        step = rules.eval_step(prog.evals) if prog.evals is not None \
            else None
        eval_due = step is not None and step > self._eval_step_fired
        if eval_due:
            self._eval_step_fired = step
    if due:
        self._time_fire(due[-1], prog)
    if eval_due:
        self._eval_notify(prog)
_time_fire(phase: str, prog: _rules.Progress) -> None #
Source code in src/adda/_src/nodes/time_rules.py
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
def _time_fire(self, phase: str, prog: _rules.Progress) -> None:
    from ..runtime import features

    rules = self._time_rules
    if phase == _rules.WIND_DOWN:
        self._record_intervention(
            "TIME_WIND_DOWN", "(run)",
            f"{rules.budget_phrase()} reached: the wind-down begins",
            fault="observation")
        self._wind_down_begin(prog)
        return
    self._record_intervention(
        "TIME_" + phase.split(":")[0].upper(), "(run)",
        rules.notice(phase, prog, can_call_done=self._holds("Done")),
        fault="observation")
    if features.enabled("budget_notes"):
        self._time_broadcast(phase, prog)
_eval_notify(prog: _rules.Progress) -> None #

The evaluation budget's notice (the same steps as the others).

Source code in src/adda/_src/nodes/time_rules.py
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
def _eval_notify(self, prog: _rules.Progress) -> None:
    """The evaluation budget's notice (the same steps as the others)."""
    from ..infra.pending_notices import LiveText
    from ..runtime import features

    text = self._time_rules.eval_notice(prog.evals * self._time_rules.evals)
    self._record_intervention(
        "TIME_NOTICE", "(run)", text, fault="observation")
    if features.enabled("budget_notes"):
        # The count is taken when the agent reads the notice: a campaign
        # that crossed the step keeps evaluating until its tool returns.
        def now() -> str:
            p = self._time_progress(self)
            return self._time_rules.eval_notice(
                (p.evals or 0) * self._time_rules.evals)
        self._time_post(LiveText(now), LiveText(now))
_time_broadcast(phase: str, prog: _rules.Progress) -> None #

The notice to the job's owner and to every running delegation.

Source code in src/adda/_src/nodes/time_rules.py
230
231
232
233
234
235
236
def _time_broadcast(self, phase: str, prog: _rules.Progress) -> None:
    """The notice to the job's owner and to every running delegation."""
    rules = self._time_rules
    entry_text = rules.notice(
        phase, prog, can_call_done=self._owner_node()._holds("Done"))
    worker_text = rules.notice(phase, prog, can_call_done=False)
    self._time_post(entry_text, worker_text)
_time_post(entry_text: str, worker_text: str) -> None #
Source code in src/adda/_src/nodes/time_rules.py
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
def _time_post(self, entry_text: str, worker_text: str) -> None:
    from ..infra.pending_notices import LiveText

    def tag(text):
        if isinstance(text, LiveText):
            return LiveText(lambda: f"[TIME — {text}]")
        return f"[TIME — {text}]"
    owner = self._owner_node()
    with owner._notifications_lock:
        owner._notifications.append(tag(entry_text))
    run_dir = self._current_run_dir
    if run_dir is None:
        return
    for n in self._stop_nodes():
        with n._registry_lock:
            live = [d for d, e in n._registry.items()
                    if e.get("status") in _LIVE]
        for did in live:
            pending_notices.post(
                Path(run_dir) / "debug", did, tag(worker_text))
_time_cutoff_refusal() -> str | None #

The Delegate refusal once the cutoff has passed, on any node.

Always enforced when a budget is declared; budget_notes only decides whether the earlier notices are sent.

Source code in src/adda/_src/nodes/time_rules.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
def _time_cutoff_refusal(self) -> str | None:
    """The ``Delegate`` refusal once the cutoff has passed, on any node.

    Always enforced when a budget is declared; ``budget_notes`` only
    decides whether the earlier notices are sent.
    """
    entry = self._time_entry()
    if entry is None or not entry._time_rules.keyed:
        return None
    rules = entry._time_rules
    prog = self._time_progress(entry)
    if prog.fraction < rules.fraction_of(_rules.CUTOFF):
        return None
    text = rules.cutoff_refusal(prog, can_call_done=self._holds("Done"))
    self._record_intervention(
        "DELEGATE_CUTOFF", "(refused)",
        "new delegation refused: " + text, fault="observation")
    return "ERROR: " + text
_stop_tick() -> str #

Raw notice text for the entry node, "" when there is nothing new (the caller marks it as an adda notice).

Idempotent and cheap: one small file read until a request is seen. Only the node that runs a turn (the one holding the run's start time) acts; a worker node's checkpoints leave the request alone.

Source code in src/adda/_src/nodes/stop.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def _stop_tick(self) -> str:
    """Raw notice text for the entry node, "" when there is nothing new
    (the caller marks it as an adda notice).

    Idempotent and cheap: one small file read until a request is seen.
    Only the node that runs a turn (the one holding the run's start
    time) acts; a worker node's checkpoints leave the request alone.
    """
    self._time_rules_tick()
    raise_if_stopped(getattr(self, "_name", ""))
    stop = self._stop
    if stop is None:
        run_dir = self._current_run_dir
        if run_dir is None or self._run_start is None:
            return ""
        req = read_stop_request(run_dir, since=self._run_start)
        if req is None:
            return ""
        stop = self._stop = {
            **req,
            "deadline": time.time() + req["grace_s"],
            "cancelled": False,
        }
        wound = self._stop_wind_down()
        if req.get("termination") == terminal.CRASHED:
            self._log_lost_workers()
        self._record_intervention(
            "RUN_STOP", "(run)",
            f"stop requested by {req['by']}"
            + (f": {req['reason']}" if req["reason"] else "")
            + f"; winding down {wound or 'no delegations'}, "
            f"grace {req['grace_s']:g}s",
        )
        return (
            f"[RUN STOP — requested by {req['by']}"
            + (f" ({req['reason']})" if req["reason"] else "")
            + ". New delegations are refused. "
            + (f"Winding down {', '.join(wound)}: Wait() for their "
               "reports, then " if wound else "")
            + "call Done() with what you have — it will skip the "
            "usual gates and ask only for your retrospective.]")
    if not stop["cancelled"] and time.time() >= stop["deadline"]:
        cancelled = self._stop_cancel_stragglers()
        if cancelled:
            return (
                "[RUN STOP — grace expired; cancelled "
                f"{', '.join(cancelled)} (no report). Call Done() now.]")
    return ""
_stop_nodes() -> list[Any] #

This node and its peers, each once: _peers holds every live node, this one included.

Source code in src/adda/_src/nodes/stop.py
151
152
153
154
155
156
157
158
def _stop_nodes(self) -> list[Any]:
    """This node and its peers, each once: ``_peers`` holds every live
    node, this one included."""
    out = [self]
    for n in getattr(self, "_peers", {}).values():
        if all(n is not m for m in out):
            out.append(n)
    return out
_stop_wind_down() -> list[str] #

Tell every live delegation to report now. Returns their ids.

Source code in src/adda/_src/nodes/stop.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def _stop_wind_down(self) -> list[str]:
    """Tell every live delegation to report now. Returns their ids."""
    ids: list[str] = []
    for n in self._stop_nodes():
        with n._registry_lock:
            live = [d for d, e in n._registry.items()
                    if e.get("status") in _LIVE]
        ids.extend(live)
    for did in ids:
        for n in self._stop_nodes():
            with n._pending_worker_msgs_lock:
                n._pending_worker_msgs.setdefault(did, []).append(
                    wind_down_notice(self._stop))
    return sorted(ids)
_stop_cancel_stragglers(why: str | None = None) -> list[str] #

Detach what has not reported, and say so. why replaces the grace wording in the delegation record (a wind-down uses it).

No placeholder retrospective is written for a straggler — it never gave one, and the record must say that rather than invent it.

Source code in src/adda/_src/nodes/stop.py
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
def _stop_cancel_stragglers(self, why: str | None = None) -> list[str]:
    """Detach what has not reported, and say so. ``why`` replaces the
    grace wording in the delegation record (a wind-down uses it).

    No placeholder retrospective is written for a straggler — it never
    gave one, and the record must say that rather than invent it.
    """
    if self._stop is not None:
        self._stop["cancelled"] = True
    cancelled: list[str] = []
    now = datetime.now(tz=timezone.utc).isoformat(timespec="seconds")
    for n in self._stop_nodes():
        with n._registry_lock:
            for did, e in n._registry.items():
                if e.get("status") in _LIVE:
                    e["status"] = "Cancelled"
                    cancelled.append((did, e))
    for did, e in cancelled:
        if self._delegation_log is not None:
            self._delegation_log.record(
                id=did,
                from_node=(
                    self._name if e.get("parent") in (None, "entry")
                    else e["parent"]),
                to_node=e.get("target") or "",
                task=str(e.get("task", "")),
                hypothesis_ids=list(e.get("hypothesis_ids") or []),
                deliverable=why or (
                    "run stop: cancelled after the "
                    f"{self._stop['grace_s']:g}s grace without a "
                    "report; no retrospective was given"),
                started_at=e.get("started_at") or now, completed_at=now,
                status="CANCELLED",
                tokens_in=0, tokens_out=0, cost_usd=None,
            )
    ids = sorted(d for d, _ in cancelled)
    if ids:
        self._record_intervention(
            "RUN_STOP_CANCELLED", "(run)",
            f"cancelled, no report: {', '.join(ids)}")
    return ids
_stop_refusal() -> str | None #

The Delegate refusal while a stop is active, else None.

Source code in src/adda/_src/nodes/stop.py
217
218
219
220
221
222
223
224
def _stop_refusal(self) -> str | None:
    """The ``Delegate`` refusal while a stop is active, else None."""
    if not any(n._stop is not None for n in self._stop_nodes()):
        return None
    return (
        "run stop: new delegations are refused. This delegation "
        "was NOT started. Wait() for any still reporting, then call "
        "Done().")
_stop_consume() -> None #
Source code in src/adda/_src/nodes/stop.py
226
227
228
229
def _stop_consume(self) -> None:
    run_dir = self._current_run_dir
    if run_dir is not None:
        consume_stop_request(run_dir)
_request_wind_down(*, reason: str, termination: str, run_dir: Any = None, grace_s: float = DEFAULT_GRACE_S) -> bool #

A backstop asks for the graceful path instead of halting. False when it cannot (no run directory, or the file cannot be written), in which case the caller halts as it always did.

Source code in src/adda/_src/nodes/stop.py
231
232
233
234
235
236
237
238
239
240
241
242
243
def _request_wind_down(
    self, *, reason: str, termination: str, run_dir: Any = None,
    grace_s: float = DEFAULT_GRACE_S,
) -> bool:
    """A backstop asks for the graceful path instead of halting. False when
    it cannot (no run directory, or the file cannot be written), in which
    case the caller halts as it always did."""
    run_dir = Path(run_dir) if run_dir else self._current_run_dir
    if run_dir is None or self._run_start is None:
        return False
    return write_stop_request(
        run_dir, by="backstop", reason=reason, grace_s=grace_s,
        termination=termination)
_wind_down_overdue() -> bool #

The grace for the workers plus an equal one for the entry node's own retrospective round has passed: the hard halt may fire.

Source code in src/adda/_src/nodes/stop.py
245
246
247
248
249
def _wind_down_overdue(self) -> bool:
    """The grace for the workers plus an equal one for the entry node's own
    retrospective round has passed: the hard halt may fire."""
    stop = self._stop
    return bool(stop) and time.time() > stop["deadline"] + stop["grace_s"]
_stop_termination() -> str #
Source code in src/adda/_src/nodes/stop.py
251
252
def _stop_termination(self) -> str:
    return (self._stop or {}).get("termination") or terminal.STOPPED
_recorded_retrospective_ids() -> set[str] #
Source code in src/adda/_src/nodes/stop.py
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
def _recorded_retrospective_ids(self) -> set[str]:
    import json

    run_dir = self._current_run_dir
    have: set[str] = set()
    if run_dir is None:
        return have
    try:
        with open(run_dir / "debug" / "retrospectives.jsonl",
                  encoding="utf-8") as f:
            for line in f:
                try:
                    have.add(str(json.loads(line).get("source_id")))
                except (json.JSONDecodeError, AttributeError):
                    continue
    except OSError:
        pass
    return have
_missing_retrospectives() -> list[str] #

Delegations that ended without a recorded retrospective.

Source code in src/adda/_src/nodes/stop.py
273
274
275
276
277
278
279
280
281
282
283
284
285
286
def _missing_retrospectives(self) -> list[str]:
    """Delegations that ended without a recorded retrospective."""
    run_dir = self._current_run_dir
    if run_dir is None:
        return []
    have = self._recorded_retrospective_ids()
    ids: set[str] = set()
    for n in self._stop_nodes():
        with n._registry_lock:
            ids.update(n._registry)
    missing = sorted(ids - have)
    if "DONE" not in have:
        missing.append("DONE")
    return missing
_log_lost_workers() -> None #

A resume after a crash: delegations the delegation log last saw RUNNING died with the process. They are named, never given invented retrospective text.

Source code in src/adda/_src/nodes/stop.py
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
def _log_lost_workers(self) -> None:
    """A resume after a crash: delegations the delegation log last saw
    RUNNING died with the process. They are named, never given invented
    retrospective text."""
    log = getattr(self, "_delegation_log", None)
    if log is None:
        return
    have = self._recorded_retrospective_ids()
    lost = sorted(
        r["id"] for r in log.query_all()
        if r.get("status") == "RUNNING" and r["id"] not in have)
    if lost:
        self._record_intervention(
            "RETROSPECTIVES_MISSING", "(run)",
            "process lost: no retrospective from "
            f"{', '.join(lost)} (they were running when the process "
            "died; none is synthesized)")
_log_missing_retrospectives(why: str) -> None #
Source code in src/adda/_src/nodes/stop.py
306
307
308
309
310
311
def _log_missing_retrospectives(self, why: str) -> None:
    missing = self._missing_retrospectives()
    if missing:
        self._record_intervention(
            "RETROSPECTIVES_MISSING", "(run)",
            f"{why}; no retrospective from: {', '.join(missing)}")
_init_orchestration(*, name: str, outgoing: list[str], spec: Any, study_dir: Any = None, interactive: bool = False, max_ask: int = 1, worker_adapters: dict | None = None, notes_dir: Any = None, workspace_dir: Any = None, delegation_log: DelegationLog | None = None) -> None #

Set up the delegation registry, the ledgers and the routing tools.

Runs for every node (Node.__init__ no longer branches on outgoing). The registry/routing state below is inert when outgoing is empty — Delegate() just has no valid target — so there is no harm running it unconditionally. Epistemic OWNERSHIP is the one thing that must stay gated on having outgoing edges (see _owns_epistemics below): notes_dir is passed identically to every node by graph_builder.py, and a node with nobody to delegate to must never acquire the hypothesis/milestone ledgers just because it was handed a path.

study_dir/workspace_dir/delegation_log are already set by :meth:Node._init_capabilities, which runs first for every node — not re-set here.

Source code in src/adda/_src/nodes/orchestration.py
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
def _init_orchestration(
    self,
    *,
    name: str,
    outgoing: list[str],
    spec: Any,
    study_dir: Any = None,
    interactive: bool = False,
    max_ask: int = 1,
    worker_adapters: dict | None = None,
    notes_dir: Any = None,
    workspace_dir: Any = None,
    delegation_log: DelegationLog | None = None,
) -> None:
    """Set up the delegation registry, the ledgers and the routing tools.

    Runs for every node (``Node.__init__`` no longer branches on
    ``outgoing``). The registry/routing state below is inert when
    ``outgoing`` is empty — ``Delegate()`` just has no valid target — so
    there is no harm running it unconditionally. Epistemic OWNERSHIP is
    the one thing that must stay gated on having outgoing edges (see
    ``_owns_epistemics`` below): ``notes_dir`` is passed identically to
    every node by ``graph_builder.py``, and a node with nobody to
    delegate to must never acquire the hypothesis/milestone ledgers just
    because it was handed a path.

    ``study_dir``/``workspace_dir``/``delegation_log`` are already set by
    :meth:`Node._init_capabilities`, which runs first for every node —
    not re-set here.
    """
    self._route: dict = {}
    self._owner: str | None = None  # AgenticState.owner, per turn
    self._interactive = interactive
    self._max_ask = max_ask
    self._ask_count = 0
    self._current_notes_dir: Path | None = (
        Path(notes_dir) if notes_dir is not None else None
    )
    # Parallel delegation registry: id → {"status", "result", "evals", "hypothesis_ids", "started_at"}
    self._worker_adapters: dict[str, Any] = worker_adapters or {}
    self._registry: dict[str, dict] = {}
    self._registry_lock = threading.Lock()
    self._threads: dict[str, threading.Thread] = {}
    # SendMessage/review (spec 12, peer_interaction): the live
    # WorkerSession for each OPEN-FOR-REVIEW delegation, so an
    # approving SendMessage can finalize it (call its own _finish_ok)
    # without re-deriving everything _finish_ok needs from the
    # registry alone. Popped once finalized -- not a leak over a long
    # run with many delegations.
    self._worker_sessions: dict[str, Any] = {}
    # SendMessage (spec 12, peer_interaction feature, not yet default-on):
    # one threading.Condition PER DELEGATOR IDENTITY -- keyed by that
    # delegator's OWN delegation_id, or "entry" for the orchestrating
    # node itself -- not per Node. Two concurrent delegations of the
    # SAME role sharing this Node object (e.g. two "implementer"
    # delegations D001/D002, each possibly delegating further) are
    # DIFFERENT delegator identities and must never wake each other's
    # waits or see each other's children's messages. Lazily created
    # (one dict entry per identity that ever waits), guarded by its own
    # lock since Condition creation itself must be race-free.
    self._delegator_conds: dict[str, threading.Condition] = {}
    self._delegator_conds_lock = threading.Lock()
    # Push notifications: background threads append here; tool calls drain it.
    self._notifications: list[str] = []
    self._notifications_lock = threading.Lock()
    # Budget state — set at the start of each __call__ from AgenticState
    self._budget_seconds: float | None = None
    self._run_start: float | None = None
    self._lost_reconciled = False
    self._open_reviews_restored = False
    self._quiet_checked_at = 0.0
    # Wall-clock rules (nodes/time_rules.py). Only the entry node, which
    # holds the run clock, ever sets ``_time_rules``.
    self._time_rules: Any = None
    self._time_fired: set[str] = set()
    self._eval_step_fired = -1
    self._time_lock = threading.Lock()
    self._time_timers: list[threading.Timer] = []
    self._time_closed = False
    self._time_armed = False
    # Wind-down state (nodes/wind_down.py); set on the entry node only.
    self._wd_started_at: float | None = None
    self._wd_turns = 0
    self._wd_step_turns = {"wait": 0, "done": 0}
    self._wd_save_asked = False
    self._stop: dict | None = None
    # Hard USD cost ceiling (None = inactive). Set each __call__ from state.
    self._budget_usd: float | None = None
    # True once any LLM call reports a real cost (claude). Stays False under
    # ollama (cost is None) → the USD ceiling is treated as inactive.
    self._cost_observed: bool = False
    # list-price spend of calls the SDK sent no cost for (the strategizer)
    self._estimated_cost_usd: float = 0.0
    self._usd_inactive_warned: bool = False
    # Consecutive Errored delegations per target (reset on that target's
    # next success). Drives the repeated-errors resumable halt.
    self._consecutive_errors: dict[str, int] = {}
    # Targets whose latest Errored delegation could not reach the LLM
    # endpoint: the halt then names that cause, not a generic error loop.
    self._unreachable_targets: set[str] = set()
    # Per-delegation pending messages (budget warnings) to prepend to
    # worker tool results.  Keyed by delegation_id; drained on next call.
    self._pending_worker_msgs: dict[str, list[str]] = {}
    self._pending_worker_msgs_lock = threading.Lock()
    # Whether THIS node owns the run's epistemic ledgers. Decided once,
    # here, from what graph_builder passed: it hands the real notes_dir
    # to every orchestrating node (any node with outgoing edges), not
    # just the entry node — a delegating node is a node that needs
    # help from another node, nothing more, and telemetry / the science
    # monitor / hypothesis-ledger READ access matter for every role.
    # WRITE access (HypothesisPropose/Update, Milestone*) is gated
    # separately, by each Agent's own declared `tools`. Ownership is
    # never acquired later — see _install_epistemics, which then narrows it
    # to the records whose tools the node holds. Gated on `outgoing`
    # (not just `notes_dir is not None`) because graph_builder passes
    # the same notes_dir to every node's constructor, including a node
    # with no outgoing edges — that node must not acquire ledger
    # ownership merely because a path was handed to it.
    # A node that can own the job (the entry, or a handoff target) runs
    # as a graph node and needs the run's records whether or not it
    # delegates. A node that declares a tool of a record (a subscriber
    # that annotates the ledger, with no edge at all) gets that record
    # too: access follows the tools; the tools limit what it writes.
    from ..runtime import features as _features
    _record_tools = (_features.by_key("hypothesis_ledger").tools
                     | _features.by_key("milestones_enabled").tools)
    self._owns_epistemics: bool = (
        bool(outgoing) or self._can_own_job()
        or (self._tools_declared
            and bool(self._agent_tools & _record_tools))
    ) and notes_dir is not None
    self._install_epistemics()
    # Running total of delegations at the START of the current __call__
    # Used as a seed for the delegation sequence counter.
    self._state_total_delegations: int = 0
    # Snapshot of _delegation_seq at the start of the current turn, so
    # the terminal Command counts only THIS turn's new delegations. Set
    # again by _absorb_state every turn; initialised here so the node's
    # attribute set is complete before any turn has run.
    self._seq_at_turn_start: int = 0
    # Monotonic per-node delegation counter — never reset within a
    # run.  Seeded from _state_total_delegations on first __call__
    # so checkpoint-resumed runs continue from the correct offset.
    # Because it never resets, it avoids the ID collision that
    # occurs when completed delegations are pruned from the registry
    # but _state_total_delegations has not yet accumulated them.
    self._delegation_seq: int = 0
    # Science monitor fires once per turn; reset at __call__ start.
    self._science_injected_this_turn: bool = False
    # Post-Done exit interview: set once the run is accepted; the next
    # Done() carries only the retrospective. _final_summary holds the real
    # conclusion so the recorded summary is the science, not the interview.
    self._awaiting_retro: bool = False
    self._final_summary: str | None = None
    # Eval budget for this run (stashed each turn from state).
    self._eval_budget: int | None = None
    # Cumulative cap on the "no canonical source registered" nudge (soft).
    self._no_source_nudges: int = 0
    # Bounded re-prompt counter: incremented each time the node loops back
    # due to an unaccepted termination (no Done or refused Done).  NOT reset
    # in the A1/A2 per-turn block — it persists across loopbacks within one
    # run.  After 3 loopbacks the run terminates UNGATED.
    self._finish_attempts: int = 0
    # Accumulated token usage across strategizer + all workers this run.
    self._token_totals: dict = {
        "input_tokens": 0,
        "output_tokens": 0,
        "cache_read_input_tokens": 0,
        "cache_creation_input_tokens": 0,
        # The backend-independent schema (infra/telemetry.py): `legacy_calls`
        # counts calls whose backend reported none of it.
        "fresh_input": 0,
        "cache_read": 0,
        "cache_write": 0,
        "output": 0,
        "normalized_calls": 0,
        "legacy_calls": 0,
        "total_cost_usd": 0.0,
    }
    # Per-node raw tool-call error count: any ERROR: return or raised
    # exception from any injected closure counts as one error for that node.
    self._error_counts: dict[str, int] = {}
    self.adapter.closure_tools.update(self._build_routing_closures())
    self.adapter.route_watcher = lambda: self._route.get("kind") in (
        "done", "handoff")
_install_epistemics() -> None #

Build (or rebuild at a new path) everything notes_dir owns.

THE one construction site for the hypothesis ledger, the milestone ledger, the science monitor and telemetry. There used to be two: the constructor built the ledger, and _absorb_state — which runs at the start of every turn — rebuilt it with if self._ledger is None, knowing nothing about why the constructor had left it None. Three consequences, all of them silent:

  • graph_builder used to hand notes_dir to the entry node alone, so it alone owned the ledgers; any other orchestrating node re-acquired one on its first turn, undoing that decision. (It now hands the real notes_dir to every orchestrating node, so this particular inconsistency no longer applies — kept here as the historical reason _install_epistemics exists as one site.)
  • Only the ledger was re-pointed when the run's notes dir differed from the constructor's; the milestone ledger and telemetry kept writing to the stale path.
  • The science monitor, built once in the constructor, was never rebuilt at all — so a node whose ledger was resurrected ran with the ledger ON and the monitor OFF.

Ownership is fixed at construction (_owns_epistemics); this only ever rebuilds at a corrected path, never grants ownership.

Source code in src/adda/_src/nodes/orchestration.py
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
def _install_epistemics(self) -> None:
    """Build (or rebuild at a new path) everything ``notes_dir`` owns.

    THE one construction site for the hypothesis ledger, the milestone
    ledger, the science monitor and telemetry. There used to be two: the
    constructor built the ledger, and ``_absorb_state`` — which runs at the
    start of every turn — rebuilt it with ``if self._ledger is None``,
    knowing nothing about why the constructor had left it None. Three
    consequences, all of them silent:

    * ``graph_builder`` used to hand ``notes_dir`` to the entry node
      alone, so it alone owned the ledgers; any other orchestrating node
      re-acquired one on its first turn, undoing that decision. (It now
      hands the real ``notes_dir`` to every orchestrating node, so this
      particular inconsistency no longer applies — kept here as the
      historical reason ``_install_epistemics`` exists as one site.)
    * Only the ledger was re-pointed when the run's notes dir differed from
      the constructor's; the milestone ledger and telemetry kept writing to
      the stale path.
    * The science monitor, built once in the constructor, was never
      rebuilt at all — so a node whose ledger was resurrected ran with the
      ledger ON and the monitor OFF.

    Ownership is fixed at construction (``_owns_epistemics``); this only
    ever rebuilds at a corrected path, never grants ownership.
    """
    from ..epistemics.milestones import MilestoneLedger
    from ..infra.telemetry import Telemetry
    from ..runtime import features

    notes = self._current_notes_dir
    # Per-call telemetry is bookkeeping, not an epistemic record: every
    # node that runs a turn records it, with or without outgoing edges.
    # An entry node with no edges used to get no sink, so its own turns
    # never reached debug/telemetry/ and the run had no cost.
    self._telemetry: Telemetry | None = (
        Telemetry(notes.parent) if notes is not None else None)
    if not self._owns_epistemics or notes is None:
        self._ledger = None
        self._milestones = None
        self._science_monitor = None
        return

    # A node owns an epistemic record only if it holds the tools that
    # write to it. A ledger nobody can write is not a ledger, and a
    # milestone gate on a node that cannot set a milestone blocks a close
    # it has no means to unblock. The monitor watches what those tools
    # produce, so it follows either family.
    tools = self._agent_tools
    holds_ledger = not self._tools_declared or bool(
        tools & features.by_key("hypothesis_ledger").tools)
    holds_milestones = not self._tools_declared or bool(
        tools & features.by_key("milestones_enabled").tools)

    # Hypothesis ledger — persists hypotheses.json. Switchable: its tools
    # and its prompt section are withheld by the same knob (runtime.
    # features), so turning it off does not leave the agent commanded to
    # use tools that error.
    self._ledger: HypothesisLedger | None = (
        HypothesisLedger.for_dir(notes)
        if holds_ledger and features.enabled("hypothesis_ledger") else None
    )

    # Milestone ledger (process policy) — persists milestones.json.
    # Seeded with the config default gates unless disabled. DISTINCT from
    # the hypothesis ledger (epistemics): process vs what's-true.
    self._milestones: MilestoneLedger | None = None
    if holds_milestones and features.enabled("milestones_enabled"):
        self._milestones = MilestoneLedger.for_dir(notes)
        # C3 switchable: the draft-pipeline gate seeds only when the
        # pipeline-deliverable knob is on (off = byte-identical to today).
        # The oracle-gold-state milestone likewise seeds only when the
        # reproduction gate itself is on — its whole reason to exist is
        # that gate's store-row precondition.
        self._milestones.seed_defaults(
            include_pipeline=features.enabled("pipeline_deliverable"),
            include_reproduction_gate=features.enabled(
                "reproduction_gate"))

    # Science drift monitor — needs the delegation log and nothing else.
    # It used to be gated on the hypothesis ledger too, via a constructor
    # argument it stored and never read, so disabling the ledger disabled
    # the monitor as well.
    self._science_monitor: ScienceMonitor | None = None
    if (self._delegation_log is not None
            and (holds_ledger or holds_milestones)
            and features.enabled("science_monitor")):
        self._science_monitor = ScienceMonitor(
            self._delegation_log,
            diagnostics_writer=self._record_science_drift,
        )
_log_status(delegation_id: str) -> tuple[str | None, str] #

Return (status, deliverable) for delegation_id from the persistent log, or (None, "") if the log has no such delegation.

Source code in src/adda/_src/nodes/orchestration.py
322
323
324
325
326
327
328
329
330
def _log_status(self, delegation_id: str) -> tuple[str | None, str]:
    """Return (status, deliverable) for *delegation_id* from the persistent
    log, or (None, "") if the log has no such delegation."""
    if self._delegation_log is None:
        return None, ""
    for r in self._delegation_log.query_all():
        if r.get("id") == delegation_id:
            return r.get("status"), (r.get("deliverable") or "")
    return None, ""
_pending_delegations() -> list[str] #

In-flight delegations, reconciled against the authoritative log.

A delegation the log shows terminal (DONE/FAILED) is never reported pending, even if the in-memory cache still says "Working". That stale state is what made Done()'s liveness gate refuse forever and kill run4 by watchdog after it had already found the optimum (audit BF-0/BF-2).

Source code in src/adda/_src/nodes/orchestration.py
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
def _pending_delegations(self) -> list[str]:
    """In-flight delegations, reconciled against the authoritative log.

    A delegation the log shows terminal (DONE/FAILED) is never reported
    pending, even if the in-memory cache still says "Working". That stale
    state is what made Done()'s liveness gate refuse forever and kill run4
    by watchdog after it had already found the optimum (audit BF-0/BF-2).
    """
    with self._registry_lock:
        pending = [
            d for d, e in self._registry.items()
            if e.get("status") == "Working"
        ]
    if self._delegation_log is not None:
        terminal = {
            r["id"] for r in self._delegation_log.query_all()
            if r.get("status") in ("DONE", "FAILED")
        }
        pending = [d for d in pending if d not in terminal]
    return pending
_open_reviews() -> list[str] #

This node's delegations whose report waits for its approval: OpenForReview, or Revising (a question already sent, unresolved).

Source code in src/adda/_src/nodes/orchestration.py
353
354
355
356
357
358
359
def _open_reviews(self) -> list[str]:
    """This node's delegations whose report waits for its approval:
    OpenForReview, or Revising (a question already sent, unresolved)."""
    with self._registry_lock:
        return sorted(
            d for d, e in self._registry.items()
            if e.get("status") in ("OpenForReview", "Revising"))
_reconcile_lost_delegations() -> list[str] #

First turn of a process: close the log rows its predecessor left RUNNING.

A delegation runs on a thread of the process that dispatched it. When that process ends, the thread ends, and its log row stays RUNNING for good. A resumed process holds no registry entry for such a row, so nothing in it can finish the row, and the agent would wait for a result that cannot come. Each orphan becomes a terminal FAILED row and the agent is told, once, which delegations died.

Source code in src/adda/_src/nodes/orchestration.py
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
def _reconcile_lost_delegations(self) -> list[str]:
    """First turn of a process: close the log rows its predecessor left RUNNING.

    A delegation runs on a thread of the process that dispatched it. When
    that process ends, the thread ends, and its log row stays RUNNING
    for good. A resumed process holds no registry entry for such a row,
    so nothing in it can finish the row, and the agent would wait for a
    result that cannot come. Each orphan becomes a terminal FAILED row
    and the agent is told, once, which delegations died.
    """
    if self._lost_reconciled or self._delegation_log is None:
        return []
    self._lost_reconciled = True
    with self._registry_lock:
        known = set(self._registry)
    lost = [
        r for r in self._delegation_log.query_all()
        if r.get("status") == "RUNNING"
        and r.get("from_node") == self._name and r["id"] not in known
    ]
    now = datetime.now(tz=timezone.utc).isoformat(timespec="seconds")
    for r in lost:
        self._delegation_log.record(
            id=r["id"], from_node=r["from_node"], to_node=r["to_node"],
            task=r.get("task", ""),
            deliverable=(
                "LOST: the process that ran this delegation ended before "
                "it finished. No result exists; files it wrote before "
                "then may remain in the workspace."),
            hypothesis_ids=r.get("hypothesis_ids") or [],
            started_at=r.get("started_at", now), completed_at=now,
            status="FAILED",
            is_falsification_attempt=bool(
                r.get("is_falsification_attempt")),
            phase=r.get("phase"), constraints=r.get("constraints"),
            workspace_sha=self._commit_workspace(
                f"{r['id']} {r['from_node']} -> {r['to_node']} [LOST]"))
    if not lost:
        return []
    ids = ", ".join(r["id"] for r in lost)
    return [
        f"[RESUME — the process that was running died. Delegations "
        f"{ids} were still running and are lost: none will return a "
        "result, so do not wait for them. Their rows are closed as "
        "FAILED. Re-delegate any work you still need.]"]
_restore_open_reviews() -> None #

First turn of a process: reports its predecessor left open for review stay reviewable.

An OPEN_FOR_REVIEW row is a finished report nobody has approved. A resumed process holds no registry entry and no worker session for it, so SendMessage(id, approve=True) was refused and the work could not be cited until it was redone. Each such row of this node gets its entry back, unread (a Wait hands the report over again) and approvable; its session ended with the old process, so it cannot be revised.

Source code in src/adda/_src/nodes/orchestration.py
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
def _restore_open_reviews(self) -> None:
    """First turn of a process: reports its predecessor left open for
    review stay reviewable.

    An OPEN_FOR_REVIEW row is a finished report nobody has approved. A
    resumed process holds no registry entry and no worker session for it,
    so SendMessage(id, approve=True) was refused and the work could not be
    cited until it was redone. Each such row of this node gets its entry
    back, unread (a Wait hands the report over again) and approvable; its
    session ended with the old process, so it cannot be revised.
    """
    if self._open_reviews_restored or self._delegation_log is None:
        return
    self._open_reviews_restored = True
    from .tools.routing.delegation import RestoredReview
    with self._registry_lock:
        known = set(self._registry)
    rows = [r for r in self._delegation_log.query_all()
            if r.get("status") == "OPEN_FOR_REVIEW"
            and r.get("from_node") == self._name and r["id"] not in known]
    for r in rows:
        with self._registry_lock:
            self._registry[r["id"]] = {
                "status": "OpenForReview", "result": r.get("deliverable", ""),
                "target": r.get("to_node"), "parent": "entry",
                "waited": False, "evals": r.get("evals", 0) or 0,
                "usage": {}, "reply": r.get("reply"),
                "to_worker": deque(), "worker_cond": threading.Condition(),
                "to_delegator": deque()}
        self._worker_sessions[r["id"]] = RestoredReview(self, r)
_quiet_delegation_notice(parent: str) -> str #

Raw notice for parent about its delegations that went quiet.

Activity is a new mtime on the delegation's transcript or on any file under its workspace folder. A Working delegation with none for runtime.quiet_delegation_min minutes (default 60) is named once; new activity re-arms it. A notice only: nothing is stopped.

Source code in src/adda/_src/nodes/orchestration.py
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
def _quiet_delegation_notice(self, parent: str) -> str:
    """Raw notice for ``parent`` about its delegations that went quiet.

    Activity is a new mtime on the delegation's transcript or on any file
    under its workspace folder. A Working delegation with none for
    ``runtime.quiet_delegation_min`` minutes (default 60) is named once;
    new activity re-arms it. A notice only: nothing is stopped.
    """
    from ..runtime import settings
    now_mono = time.monotonic()
    if now_mono - self._quiet_checked_at < 30.0:
        return ""
    self._quiet_checked_at = now_mono
    limit_s = settings.get_float("quiet_delegation_min", 60.0) * 60.0
    if limit_s <= 0:
        return ""
    now = time.time()
    with self._registry_lock:
        working = [
            (d, e) for d, e in self._registry.items()
            if e.get("status") == "Working" and e.get("parent") == parent
        ]
    notes = []
    for did, entry in working:
        seen = entry.get("_activity_at")
        if seen is None:
            seen = now - (now_mono - entry.get("start_time", now_mono))
        latest = self._latest_activity(did)
        if latest is not None and latest > seen:
            seen = latest
            entry["_quiet_notified"] = False
        entry["_activity_at"] = seen
        if now - seen >= limit_s and not entry.get("_quiet_notified"):
            entry["_quiet_notified"] = True
            notes.append(
                f"delegation {did} has shown no transcript or file "
                f"activity for {int((now - seen) // 60)} min; it may be "
                "blocked in a tool, or running a long computation. "
                "Poll it to check.")
    return "\n".join(notes)
_latest_activity(did: str) -> float | None #

Newest mtime of the delegation's transcript or workspace files.

Source code in src/adda/_src/nodes/orchestration.py
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
def _latest_activity(self, did: str) -> float | None:
    """Newest mtime of the delegation's transcript or workspace files."""
    paths = []
    if self._current_notes_dir is not None:
        paths.append(
            self._current_notes_dir.parent / "transcripts" / f"{did}.jsonl")
    newest = None
    for p in paths:
        try:
            newest = max(newest or 0.0, p.stat().st_mtime)
        except OSError:
            pass
    if self._workspace_dir is not None:
        for root, _dirs, files in os.walk(self._workspace_dir / did):
            for f in files:
                try:
                    m = os.stat(os.path.join(root, f)).st_mtime
                except OSError:
                    continue
                newest = max(newest or 0.0, m)
    return newest
_canonical_source_registered() -> bool #

True if a canonical ground-truth source is resolvable.

Reads run_config.json: an evaluator entrypoint OR a lookup pool counts as a registered source. Best-effort — on any read failure, assume not registered (the nudge is soft, so a false 'no' just costs one notice).

Source code in src/adda/_src/nodes/orchestration.py
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
def _canonical_source_registered(self) -> bool:
    """True if a canonical ground-truth source is resolvable.

    Reads run_config.json: an evaluator entrypoint OR a lookup pool counts
    as a registered source. Best-effort — on any read failure, assume not
    registered (the nudge is soft, so a false 'no' just costs one notice).
    """
    notes = self._current_notes_dir
    if notes is None:
        return False
    try:
        import json as _json
        cfg = _json.loads(
            (Path(notes).parent / "run_config.json").read_text())
        return bool(
            cfg.get("evaluator_entrypoint")
            or cfg.get("evaluator_lookup")
        )
    except Exception:  # noqa: BLE001
        return False
_drain_operator_notes() -> str #

Operator notes queued by a human in the viewer, marked as coming from the operator; "" when none. Notes addressed to a RUNNING delegation are routed to that worker instead. Claims the queue destructively, so whatever calls this owns delivery.

Source code in src/adda/_src/nodes/orchestration.py
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
def _drain_operator_notes(self) -> str:
    """Operator notes queued by a human in the viewer, marked as coming
    from the operator; "" when none. Notes addressed to a RUNNING
    delegation are routed to that worker instead. Claims the queue
    destructively, so whatever calls this owns delivery."""
    text = ""
    # Operator notes: messages a human queued in the viewer while the run
    # was working. Delivered here, on the same path as the budget warnings, so a note
    # reaches the agent at its next tool call rather than interrupting a
    # turn in progress. Marked as coming from the operator because the
    # agent should weigh it differently from another agent's message —
    # it is the one voice in the run that is not itself an agent.
    run_dir = self._current_run_dir
    if run_dir is not None:
        from ..infra.operator_channel import drain_note_rows
        rows = drain_note_rows(run_dir)
        mine: list[str] = []
        for _row in rows:
            _to = _row.get("to_node") or ""
            _note = (
                "[OPERATOR NOTE — from the human running this study. "
                "Weigh it as a briefing correction, not as another "
                f"agent's opinion: {_row['text']}]"
            )
            # A note addressed to a RUNNING delegation goes to that
            # worker, on the same per-delegation queue the
            # budget warnings use — so a human can correct work already
            # in flight instead of waiting for a wrong result. The queue
            # is claimed destructively, so this is the only place that
            # may drain it: routing here is what keeps an addressed note
            # from being swallowed by the orchestrator's own delivery.
            if _to:
                with self._registry_lock:
                    _entry = self._registry.get(_to)
                    _live = bool(_entry) and _entry.get("status") == "Working"
                if _live:
                    with self._pending_worker_msgs_lock:
                        self._pending_worker_msgs.setdefault(
                            _to, []).append(_note)
                    continue
                # Addressed to something not running: the human still
                # said it, so it must not vanish — hand it to the
                # orchestrator with the intended recipient named.
                _note = (
                    f"[OPERATOR NOTE addressed to {_to}, which is not "
                    f"running — delivered to you instead: "
                    f"{_row['text']}]"
                )
            mine.append(_note)
        if mine:
            text = "\n\n".join(mine) + "\n\n" + text
    return text
_drain_entry_notices() -> str #

Notices a campaign process queued for the entry node (the oracle was edited, say); "" for any other node or when none. The entry node holds no delegation id, so the backends' post-tool hook never reaches its queue; its next tool call does.

Source code in src/adda/_src/nodes/orchestration.py
587
588
589
590
591
592
593
594
595
596
597
598
def _drain_entry_notices(self) -> str:
    """Notices a campaign process queued for the entry node (the oracle
    was edited, say); "" for any other node or when none. The entry
    node holds no delegation id, so the backends' post-tool hook never
    reaches its queue; its next tool call does."""
    run_dir = self._current_run_dir
    entry = getattr(self._spec, "entry", None)
    if run_dir is None or entry is None or entry != self._name:
        return ""
    from ..infra.pending_notices import drain
    queued = drain(run_dir / "debug", "entry")
    return "\n".join(queued) + "\n\n" if queued else ""
_publish_escalation(offenders: list) -> None #

Fire monitor_escalation (handoff spec P3b): the monitor's escalation rule fired on offenders. Its record is the MONITOR_ESCALATION row; subscribers are woken.

Source code in src/adda/_src/nodes/orchestration.py
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
def _publish_escalation(self, offenders: list) -> None:
    """Fire ``monitor_escalation`` (handoff spec P3b): the monitor's
    escalation rule fired on ``offenders``. Its record is the
    ``MONITOR_ESCALATION`` row; subscribers are woken."""
    import json as _json

    from .subscriptions import publish
    ledger = {h: self._ledger.get(h) for h in offenders} if self._ledger else {}
    run_dir = self._resolve_run_dir()
    diag = run_dir / "debug" / "diagnostics.jsonl" if run_dir else "(unknown)"
    payload = ("The science monitor's escalation rule fired: repeated "
               f"drift on {', '.join(offenders)}.\n"
               f"diagnostics: {diag}\n"
               "hypotheses:\n" + _json.dumps(ledger, indent=2, default=str))
    try:
        publish(self, "monitor_escalation", payload,
                row={"message": f"escalation on {', '.join(offenders)}",
                     "hypotheses": list(offenders)})
    except Exception:  # noqa: BLE001 — an event never breaks a turn
        pass
_drain_notifications() -> str #

Return and clear any pending push notifications, or empty string.

Source code in src/adda/_src/nodes/orchestration.py
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
def _drain_notifications(self) -> str:
    """Return and clear any pending push notifications, or empty
    string."""
    with self._notifications_lock:
        if not self._notifications:
            text = ""
        else:
            msgs = list(self._notifications)
            self._notifications.clear()
            # str(): a LiveText renders at delivery; join would not.
            text = "\n".join(map(str, msgs)) + "\n\n"
    text = self._drain_entry_notices() + text
    text = self._drain_operator_notes() + text
    text = self._stop_tick() + text
    from ..backends.base import get_delegation_id
    quiet = self._quiet_delegation_notice(get_delegation_id() or "entry")
    if quiet:
        text = quiet + "\n\n" + text
    if self._science_monitor is not None:
        offenders = self._science_monitor.escalation_due()
        if offenders:
            self._science_monitor.note_escalated()
            self._publish_escalation(offenders)
        # Inject at most once per turn so the same warning does not
        # appear on every tool call.
        if not self._science_injected_this_turn:
            drift = self._actionable(self._science_monitor.drain())
            if drift:
                text += drift
                self._science_injected_this_turn = True
    # Everything accumulated above is adda speaking to the agent, not
    # a tool's output — mark it so both the agent and the viewer can
    # tell the difference (see nodes/notices.py).
    return wrap_notice(text)
_delegation_entry(delegation_id: str) -> tuple[Any, dict] | None #

(owning node, registry entry) of a delegation, or None.

The entry lives in the registry of the node that DELEGATED (its delegator), not of the node running the work; the tools a worker calls are bound to the worker's own node, so a worker looking up its own delegation must search across the graph's nodes. Delegation ids come from one graph-wide log, so at most one node owns an id.

Source code in src/adda/_src/nodes/orchestration.py
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
def _delegation_entry(self, delegation_id: str) -> tuple[Any, dict] | None:
    """``(owning node, registry entry)`` of a delegation, or None.

    The entry lives in the registry of the node that DELEGATED (its
    delegator), not of the node running the work; the tools a worker
    calls are bound to the worker's own node, so a worker looking up its
    own delegation must search across the graph's nodes. Delegation ids
    come from one graph-wide log, so at most one node owns an id.
    """
    for n in (self, *getattr(self, "_peers", {}).values()):
        with n._registry_lock:
            entry = n._registry.get(delegation_id)
        if entry is not None:
            return n, entry
    return None
_get_delegator_cond(identity: str) -> threading.Condition #

The one Condition a given delegator identity waits on and is woken through (SendMessage, spec 12). identity is a delegation_id or "entry" — see __init__'s comment on _delegator_conds. Lazily created, race-free.

Source code in src/adda/_src/nodes/orchestration.py
672
673
674
675
676
677
678
679
680
681
682
def _get_delegator_cond(self, identity: str) -> threading.Condition:
    """The one Condition a given delegator identity waits on and is
    woken through (SendMessage, spec 12). ``identity`` is a
    delegation_id or ``"entry"`` — see ``__init__``'s comment on
    ``_delegator_conds``. Lazily created, race-free."""
    with self._delegator_conds_lock:
        cond = self._delegator_conds.get(identity)
        if cond is None:
            cond = threading.Condition()
            self._delegator_conds[identity] = cond
        return cond
_truncate_for_notice(text: str, n: int = 100) -> str staticmethod #
Source code in src/adda/_src/nodes/orchestration.py
684
685
686
687
@staticmethod
def _truncate_for_notice(text: str, n: int = 100) -> str:
    text = text.strip()
    return text if len(text) <= n else text[:n] + "..."
_pending_for_you(identity: str) -> str #

What identity (a delegator: "entry" or a delegation id of a node that is ITSELF delegating further) currently owes, computed FRESH on every call -- never a drained queue, since "nothing pending" must read as silence indefinitely, not just until the first drain (spec 12 design item 10(a): "at minimum, every agent must always KNOW what it currently owes ... or nothing (in which case: silence, not a notice for its own sake)").

Two kinds, both as a DELEGATOR (entry.get("parent") == identity -- never a sibling's or a nested child's): a report open for review (OpenForReview), and a finished delegation (Done/Errored) not yet collected via Wait. A SendMessage is not listed here: messages travel on one path, the recipient's pending notices, which every tool call drains (see infra/pending_notices.post_message). Returns "" when none apply -- the common case, deliberately silent.

Source code in src/adda/_src/nodes/orchestration.py
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
def _pending_for_you(self, identity: str) -> str:
    """What ``identity`` (a delegator: ``"entry"`` or a delegation id
    of a node that is ITSELF delegating further) currently owes,
    computed FRESH on every call -- never a drained queue, since
    "nothing pending" must read as silence indefinitely, not just
    until the first drain (spec 12 design item 10(a): "at minimum,
    every agent must always KNOW what it currently owes ... or
    nothing (in which case: silence, not a notice for its own
    sake)").

    Two kinds, both as a DELEGATOR (``entry.get("parent") ==
    identity`` -- never a sibling's or a nested child's): a report open
    for review (`OpenForReview`), and a finished delegation
    (`Done`/`Errored`) not yet collected via `Wait`. A `SendMessage`
    is not listed here: messages travel on one path, the recipient's
    pending notices, which every tool call drains (see
    ``infra/pending_notices.post_message``). Returns "" when none
    apply -- the common case, deliberately silent.
    """
    with self._registry_lock:
        mine = [
            (did, dict(e)) for did, e in self._registry.items()
            if e.get("parent") == identity
        ]
    reviews = sorted(
        did for did, e in mine if e.get("status") == "OpenForReview")
    uncollected = sorted(
        did for did, e in mine
        if e.get("status") in ("Done", "Errored") and not e.get("waited")
    )

    if not (reviews or uncollected):
        return ""
    bits = []
    if reviews:
        bits.append(
            f"open for review: {', '.join(reviews)} -- SendMessage(id, "
            "..., approve=True) to finalize, or ask a question first"
        )
    if uncollected:
        bits.append(
            f"finished, not yet collected: {', '.join(uncollected)} "
            "-- Wait(id)"
        )
    return "You have pending items -- " + "; ".join(bits) + "."
_build_routing_closures() -> dict #
Source code in src/adda/_src/nodes/orchestration.py
735
736
737
def _build_routing_closures(self) -> dict:
    from .tools.routing import build_routing_tools
    return build_routing_tools(self)
_wrap_closure(fn: Any, node_name: str) -> Any #

Return a version of fn that records ERROR returns and exceptions.

Uses functools.wraps so inspect.signature() follows wrapped to the original function — _infer_schema_from_callable must see the real parameter names, not (args, *kwargs).

Also coerces string-typed arguments to int/float/bool when the function annotation requests it (handles Ollama passing "5" for an int parameter).

And it is where pending notices reach the agent: whatever _drain_notifications holds is APPENDED to the tool's result — after it, so a result's leading word stays what callers dispatch on ("Done", "Working", "ERROR:", an id like "H3"). That used to be hand-copied into 18 tool bodies and missing from the rest — the store tools never delivered a notice — so whether the agent heard about a finished delegation depended on which tool it happened to call. A tool that places notices itself (the status poll keeps its status word first; Wait drains while it blocks; Done composes its own reply) still does, and finds the queue already empty here. A worker's closures pass through here too, and are never drained into.

Source code in src/adda/_src/nodes/orchestration.py
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
def _wrap_closure(self, fn: Any, node_name: str) -> Any:
    """Return a version of *fn* that records ERROR returns and exceptions.

    Uses functools.wraps so inspect.signature() follows __wrapped__ to the
    original function — _infer_schema_from_callable must see the real
    parameter names, not (*args, **kwargs).

    Also coerces string-typed arguments to int/float/bool when the
    function annotation requests it (handles Ollama passing "5" for
    an int parameter).

    And it is where pending notices reach the agent: whatever
    ``_drain_notifications`` holds is APPENDED to the tool's result — after
    it, so a result's leading word stays what callers dispatch on ("Done",
    "Working", "ERROR:", an id like "H3"). That
    used to be hand-copied into 18 tool bodies and missing from the rest —
    the store tools never delivered a notice — so whether the agent heard
    about a finished delegation depended on which tool it happened to
    call. A tool that places notices itself (the status poll keeps its
    status word first; Wait drains while it blocks; Done composes its own
    reply) still does, and finds the queue already empty here. A
    worker's closures pass through here too, and are never drained into.
    """
    import functools as _functools
    import inspect as _inspect
    import typing as _typing

    node = self
    tool_name = getattr(fn, "__name__", repr(fn))

    # Resolve type hints once; fall back to {} if any forward ref
    # cannot be resolved (e.g. "DelegationLog | None").
    try:
        _hints = _typing.get_type_hints(fn)
    except Exception:  # noqa: BLE001
        _hints = {}
    _COERCIBLE = {int, float, bool}

    def _coerce(name: str, value: Any) -> Any:
        target = _hints.get(name)
        if target not in _COERCIBLE or not isinstance(value, str):
            return value
        if target is bool:
            low = value.strip().lower()
            if low in ("true", "1", "yes"):
                return True
            if low in ("false", "0", "no"):
                return False
            return value
        try:
            return target(value)
        except ValueError:
            return value

    _PLAIN = (str, int, float, bool, list, dict)

    def _wrong_type_arg(arguments: dict) -> bool:
        """True when the agent passed an argument whose type the tool's
        annotation rules out (a list where it declares str): then an
        exception the tool raises on it is the agent's, not a defect."""
        for name, value in arguments.items():
            hint = _hints.get(name)
            allowed = tuple(
                t for t in (_typing.get_args(hint) or (hint,))
                if t is type(None) or t in _PLAIN)
            if not allowed or value is None:
                continue
            if float in allowed and isinstance(value, int):
                continue
            if not isinstance(value, allowed):
                return True
        return False

    @_functools.wraps(fn)
    def _wrapped(*args, **kwargs):
        node._raise_if_abandoned()
        # The node's own oracle manifest takes effect at its next tool
        # call, not only at the end of its turn: for an entry node that
        # works alone, the turn's end is the run's end, so a manifest
        # registered only there could never be used (nor pass the
        # reproduction gate). A worker's calls never register here.
        try:
            from ..backends.base import get_delegation_id as _gd
            if _gd() is None:
                node._register_entry_manifest()
        except Exception:  # noqa: BLE001 — never blocks the tool call
            pass
        # Coerce string args before calling the real function.
        call_args = dict(kwargs)
        try:
            bound = _inspect.signature(fn).bind_partial(
                *args, **kwargs
            )
            for pname in list(bound.arguments):
                bound.arguments[pname] = _coerce(
                    pname, bound.arguments[pname]
                )
            args, kwargs = bound.args, bound.kwargs
            call_args = dict(bound.arguments)
        except TypeError:
            pass  # signature mismatch: let fn raise its own error

        try:
            from ..backends.base import get_delegation_id as _gdid
            _did = _gdid()
            result = _wind_down.check(
                _did or "entry", tool_name, can_call_done=not _did)
            if result is None:
                result = fn(*args, **kwargs)
            if (
                isinstance(result, str)
                and result.lstrip().startswith("ERROR:")
            ):
                node._record_tool_error(
                    node_name,
                    tool_name,
                    "ERROR_RETURN",
                    result[:2000],
                    args=call_args,
                )
            # A tool can carry a diagnostic source (e.g. ConsultLiterature
            # tags itself with its LiteratureCorpus) for facts that are
            # true of the environment, not of one call's return value —
            # e.g. the dense embedder being unavailable — and so would
            # otherwise never reach diagnostics.jsonl or the agent. This
            # is the one place with both the per-run diagnostics path
            # and, via the tag, a handle back to the source; it fires
            # once (the source pops its own event) rather than on every
            # call.
            _diag_src = getattr(fn, "_adda_diagnostic_source", None)
            if _diag_src is not None:
                _event = _diag_src.pop_diagnostic_event()
                if _event is not None:
                    _kind, _msg, *_extra = _event
                    node._record_intervention(
                        _kind, node_name, _msg, **(_extra[0] if _extra else {}))
                    if isinstance(result, str):
                        result = wrap_notice(_msg) + result
            # Only for this node's OWN tools. A dispatched worker's
            # closures are wrapped by the delegating node too (it records
            # their errors), and draining there would hand the
            # orchestrator's notices to the worker.
            if isinstance(result, str) and node_name == node._name:
                notices = node._drain_notifications()
                if notices.strip():
                    result = insert_notice(result, notices)
                # Spec 12 design item 10(a): a pending-for-you notice
                # on every tool result, scoped to THIS CALL's own
                # delegator identity (never a sibling's or a nested
                # child's). Gated behind peer_interaction (on by
                # default since the migration-sweep commit) -- with the
                # feature off (the no-peer-messaging arm),
                # OpenForReview cannot exist and this stays silent.
                from ..runtime import features as _features
                if _features.enabled("peer_interaction"):
                    from ..backends.base import get_delegation_id
                    identity = get_delegation_id() or "entry"
                    pending = node._pending_for_you(identity)
                    if pending:
                        result = insert_notice(
                            result, wrap_notice(pending))
            return result
        except Exception as exc:
            node._record_tool_error(
                node_name,
                tool_name,
                type(exc).__name__,
                str(exc)[:2000],
                tb=traceback.format_exc(),
                args=call_args,
                fault=("agent" if _wrong_type_arg(call_args) else None),
            )
            raise

    return _wrapped
_orchestrate(state: AgenticState) -> Any #

One orchestration turn, in the order it happens.

Absorb the run state onto the node, work out what the agent must be TOLD this turn, invoke it once, then route on what it did. Every step is a method below, named for its step; a turn ends in exactly one of three ways, which :meth:_route_turn states.

Source code in src/adda/_src/nodes/orchestration.py
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
def _orchestrate(self, state: AgenticState) -> Any:
    """One orchestration turn, in the order it happens.

    Absorb the run state onto the node, work out what the agent must be
    TOLD this turn, invoke it once, then route on what it did. Every step
    is a method below, named for its step; a turn ends in exactly one of
    three ways, which :meth:`_route_turn` states.
    """
    self._absorb_state(state)
    budget_warnings = self._budget_warnings(state)
    halt = self._check_unrecoverable(
        state, self._budget_seconds, self._run_start)
    if halt is not None:
        return halt
    pending_notifs = self._reset_for_turn()
    stop_notice = self._stop_tick()
    if stop_notice:
        pending_notifs.append(stop_notice)
    pending_notifs.extend(self._reconcile_lost_delegations())
    self._restore_open_reviews()
    messages = self._compose_messages(state, budget_warnings, pending_notifs)
    try:
        ai_msg = self._invoke_turn(messages)
    except Exception as exc:  # noqa: BLE001
        if self._stop is None:
            raise
        return self._close_after_wind_down_error(state, exc)
    self._register_entry_manifest()
    return self._route_turn(state, ai_msg)
_register_entry_manifest() -> None #

Register the oracle manifest an entry node wrote for its own work.

An entry node that runs evaluations itself (it holds an entry_work stamp) has no datagenerator report to carry its oracle. A registration.json it writes under debug/delegations/<entry_work id>/generators/ is registered here, at the end of its turn, through the same path a datagenerator report uses. A manifest is processed again only when it changes.

Source code in src/adda/_src/nodes/orchestration.py
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
def _register_entry_manifest(self) -> None:
    """Register the oracle manifest an entry node wrote for its own work.

    An entry node that runs evaluations itself (it holds an
    ``entry_work`` stamp) has no datagenerator report to carry its
    oracle. A ``registration.json`` it writes under
    ``debug/delegations/<entry_work id>/generators/`` is registered here,
    at the end of its turn, through the same path a datagenerator report
    uses. A manifest is processed again only when it changes.
    """
    did = getattr(self.adapter, "eval_delegation_id", None)
    notes = self._current_notes_dir
    if not did or notes is None:
        return
    run_dir = notes.parent.parent
    ws = run_dir / "debug" / "delegations" / did / "generators"
    try:
        stamp = (ws / "registration.json").stat().st_mtime_ns
    except OSError:
        return
    if getattr(self, "_entry_manifest_seen", None) == stamp:
        return
    self._entry_manifest_seen = stamp
    from .tools.routing.delegation import register_manifest
    try:
        register_manifest(self, run_dir=run_dir, ws=ws, delegation_id=did,
                          namespace=None, target=self._name)
    except Exception as exc:  # noqa: BLE001
        with self._notifications_lock:
            self._notifications.append(
                f"[Evaluator NOT registered from {ws / 'registration.json'}"
                f": {type(exc).__name__}: {exc}]")
_absorb_state(state: AgenticState) -> None #

Copy the run state the node's TOOLS read onto the node itself.

The closures reach their context through node.…, not through AgenticState, so anything a tool needs has to land here first.

Source code in src/adda/_src/nodes/orchestration.py
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
def _absorb_state(self, state: AgenticState) -> None:
    """Copy the run state the node's TOOLS read onto the node itself.

    The closures reach their context through ``node.…``, not through
    AgenticState, so anything a tool needs has to land here first.
    """
    # Update notes_dir from current state run_dir
    run_dir = state.get("run_dir")
    if run_dir:
        _notes = Path(run_dir) / "debug" / "strategizer_notes"
        if _notes != self._current_notes_dir:
            # The run's real notes dir differs from the one the
            # constructor saw. Re-point EVERYTHING that lives there, not
            # just the hypothesis ledger — the milestone ledger and
            # telemetry used to keep writing to the stale path. Nodes that
            # do not own the ledgers still track the path (they read run
            # files through it) but acquire nothing.
            self._current_notes_dir = _notes
            self._install_epistemics()
        # Wire canonical store dir into ScienceMonitor lazily.
        # store_dir is the ExperimentData *project_dir* (run_dir/
        # experiment_data), NOT the folder holding the CSVs. ExperimentData
        # appends its own EXPERIMENTDATA_SUBFOLDER ("experiment_data"), so
        # the rows live one level deeper at
        # run_dir/experiment_data/experiment_data/output.csv — hence the
        # apparent double directory is correct, not a typo.
        if self._science_monitor is not None:
            self._science_monitor.store_dir = (
                self._current_notes_dir.parent.parent
                / "experiment_data"
            )

    # Eval budget → available to the time rules and the constraint
    # snapshot (judge the best honest conclusion within evals spent).
    self._eval_budget = state.get("eval_budget")

    # Required aux deliverables (config.yaml) → on the node so WriteDeliverable
    # may write them: the gate REQUIRES them, so the writing tool must accept
    # them (else gate-vs-tool deadlock — audit run 20260624T021359).
    self._required_deliverables = state.get("required_deliverables") or []

    # Store on node so the status poll can compute delegation timeout
    self._budget_seconds = state.get("budget_seconds")
    self._run_start = state.get("start_time")
    self._owner = state.get("owner")
    # Mail to the orchestrating identity ("entry") goes to the node that
    # owns the job, whichever node that is.
    self.adapter.notice_key = (
        "entry" if self._job_owner() == self._name else None)
    self._time_rules_start()
    self._budget_usd = state.get("budget_usd")

    # Capture total_delegations so Delegate() can seed the counter.
    self._state_total_delegations = state.get("total_delegations", 0)
    # Seed the monotonic counter from state on first turn (or after a
    # checkpoint rebuild).  Never decremented — ensures IDs are unique
    # even when the registry is pruned between turns.
    if self._delegation_seq < self._state_total_delegations:
        self._delegation_seq = self._state_total_delegations
    # Snapshot seq at turn start so total_new counts only THIS turn.
    self._seq_at_turn_start: int = self._delegation_seq
_ledgered_eval_total(floor: int) -> int #

The run's eval count, preferring the ledger over an accumulator.

The canonical ledger is the source of truth: a killed/cancelled delegation flushes rows the state accumulator never sees, so the accumulator undercounts. Summed across the canonical store AND every design namespace — namespace evals were invisible to the run total and to the soft budget. floor keeps the accumulator for lookup-direct studies with no instrumented store.

Source code in src/adda/_src/nodes/orchestration.py
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
def _ledgered_eval_total(self, floor: int) -> int:
    """The run's eval count, preferring the ledger over an accumulator.

    The canonical ledger is the source of truth: a killed/cancelled
    delegation flushes rows the state accumulator never sees, so the
    accumulator undercounts. Summed across the canonical store AND every
    design namespace — namespace evals were invisible to the run total and
    to the soft budget. ``floor`` keeps the accumulator for lookup-direct
    studies with no instrumented store.
    """
    try:
        from ..evaluation.ledger_summary import total_ledgered_evals
        _nd = getattr(self, "_current_notes_dir", None)
        if _nd is not None:
            return max(
                floor,
                int(total_ledgered_evals(
                    _nd.parent.parent / "experiment_data")),
            )
    except Exception:  # noqa: BLE001
        pass
    return floor
_budget_warnings(state: AgenticState) -> list[dict] #

Advisory budget messages for this turn.

The budget rules (notices at budget_warn_from, the cutoff, the wind-down; wall, tokens and evaluations alike) are in nodes/time_rules.py and nodes/wind_down.py and reach the model as notifications.

Source code in src/adda/_src/nodes/orchestration.py
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
def _budget_warnings(self, state: AgenticState) -> list[dict]:
    """Advisory budget messages for this turn.

    The budget rules (notices at ``budget_warn_from``, the cutoff, the
    wind-down; wall, tokens and evaluations alike) are in
    ``nodes/time_rules.py`` and ``nodes/wind_down.py`` and reach the model as notifications.
    """
    warnings: list[dict] = []
    self._time_rules_tick()
    return warnings
_reset_for_turn() -> list[str] #

A1/A2: clear per-turn state; return the notifications to deliver.

Working entries are preserved so loopbacks don't orphan live delegations whose background threads are still running, and so are reports open for review (or being revised): finished work nobody has approved yet must stay approvable across a turn boundary.

Source code in src/adda/_src/nodes/orchestration.py
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
def _reset_for_turn(self) -> list[str]:
    """A1/A2: clear per-turn state; return the notifications to deliver.

    Working entries are preserved so loopbacks don't orphan live
    delegations whose background threads are still running, and so are
    reports open for review (or being revised): finished work nobody has
    approved yet must stay approvable across a turn boundary.
    """
    self._route.clear()
    self._ask_count = 0
    self._science_injected_this_turn = False
    with self._registry_lock:
        self._registry = {
            d: e for d, e in self._registry.items()
            if e["status"] in ("Working", "Done", "OpenForReview",
                               "Revising")
        }
        self._threads = {
            d: t for d, t in self._threads.items()
            if d in self._registry
        }
    with self._notifications_lock:
        pending = [str(m) for m in self._notifications]
        self._notifications.clear()
    return pending
_compose_messages(state: AgenticState, budget_warnings: list[dict], pending_notifs: list[str]) -> list[dict] #

The conversation plus everything injected in-band this turn.

Everything after the conversation itself is adda speaking to the agent — a budget warning, the no-source nudge, the milestone backlog, a pushed notification — arriving in the SAME role ("user") the human's own task arrives in. Marked, for the same reason tool-result notices are (nodes/notices.py): unmarked, neither the agent nor a reader can tell the runtime's nudge from the human's brief, and the viewer cannot style it as anything else.

Source code in src/adda/_src/nodes/orchestration.py
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
def _compose_messages(
    self,
    state: AgenticState,
    budget_warnings: list[dict],
    pending_notifs: list[str],
) -> list[dict]:
    """The conversation plus everything injected in-band this turn.

    Everything after the conversation itself is adda speaking to the
    agent — a budget warning, the no-source nudge, the milestone backlog,
    a pushed notification — arriving in the SAME role ("user") the human's
    own task arrives in. Marked, for the same reason tool-result notices
    are (nodes/notices.py): unmarked, neither the agent nor a reader can
    tell the runtime's nudge from the human's brief, and the viewer cannot
    style it as anything else.
    """
    if self._silent:
        return _to_adapter_messages(self._my_messages(state))
    injected = (
        self._constraint_refresh()
        + budget_warnings
        + self._no_source_nudge()
        + self._backlog_announcement()
        + [{"role": "user", "content": n} for n in pending_notifs]
    )
    return _to_adapter_messages(self._my_messages(state)) + [
        {**m, "content": wrap_notice(str(m.get("content", "")),
                                     trailing="")}
        for m in injected
        if str(m.get("content", "")).strip()
    ]
_constraint_refresh() -> list[dict] #

This turn's constraint snapshot, recomputed NOW.

The entry node used to be the one call site that did not re-snapshot. agent_runtime rendered a snapshot once at run start and concatenated it onto the problem statement, and that string is the standing first user turn — re-sent verbatim on every later turn, so the numbers inside it could never advance. Observed on run 20260917T141603: four consecutive strategizer turns spanning 23.5 minutes all read "2.7min/60.0min used (5%)", while the true figure at turn 4 was 26.2min (44%).

The orchestrator was not blind -- every delegation report carries a fresh snapshot (_append_budget_report) -- which made this a CONTRADICTION rather than an absence: a frozen block and live blocks in one context. Worse, the frozen copy lived in the first user turn, which the context trim pins and never evicts, so it was the one guaranteed to survive while the fresh ones aged out.

Recomputing per turn is what constraint_snapshot.py already asks of every caller: "call this at every delegation boundary rather than caching a value from earlier ... its entire purpose depends on being current". The orchestrator is the node that decides how much more to attempt, so it is the node that most needs the live clock.

Source code in src/adda/_src/nodes/orchestration.py
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
def _constraint_refresh(self) -> list[dict]:
    """This turn's constraint snapshot, recomputed NOW.

    The entry node used to be the one call site that did not re-snapshot.
    ``agent_runtime`` rendered a snapshot once at run start and
    concatenated it onto the problem statement, and that string is the
    standing first user turn — re-sent verbatim on every later turn, so
    the numbers inside it could never advance. Observed on run
    20260917T141603: four consecutive strategizer turns spanning 23.5
    minutes all read "2.7min/60.0min used (5%)", while the true figure at
    turn 4 was 26.2min (44%).

    The orchestrator was not blind -- every delegation report carries a
    fresh snapshot (``_append_budget_report``) -- which made this a
    CONTRADICTION rather than an absence: a frozen block and live blocks
    in one context. Worse, the frozen copy lived in the first user turn,
    which the context trim pins and never evicts, so it was the one
    guaranteed to survive while the fresh ones aged out.

    Recomputing per turn is what ``constraint_snapshot.py`` already asks
    of every caller: "call this at every delegation boundary rather than
    caching a value from earlier ... its entire purpose depends on being
    current". The orchestrator is the node that decides how much more to
    attempt, so it is the node that most needs the live clock.
    """
    from ..runtime import features
    from ..runtime.constraint_snapshot import snapshot_for_node
    if not features.enabled("budget_notes"):
        return []
    try:
        text = snapshot_for_node(self).as_text()
    except Exception:  # noqa: BLE001 — a missing snapshot never fails a turn
        return []
    return [{"role": "user", "content": text}] if text.strip() else []
_no_source_nudge() -> list[dict] #

Recommend registering a canonical source (soft, ≤3×).

Whenever no source is registered, whatever the graph: any agent can author and register one (a delegation's generators/registration.json, or the entry node's own), so the reminder does not depend on a role being present. Without a registered source every evaluation lands off-ledger and nothing is reproducible from the canonical store. Soft and capped — never blocks; the strategizer may ignore it for a genuinely source-free study.

Source code in src/adda/_src/nodes/orchestration.py
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
def _no_source_nudge(self) -> list[dict]:
    """Recommend registering a canonical source (soft, ≤3×).

    Whenever no source is registered, whatever the graph: any agent can
    author and register one (a delegation's generators/registration.json,
    or the entry node's own), so the reminder does not depend on a role
    being present. Without a registered source every evaluation lands
    off-ledger and nothing is reproducible from the canonical store. Soft
    and capped — never blocks; the strategizer may ignore it for a
    genuinely source-free study.
    """
    if (
        self._no_source_nudges >= 3
        or self._canonical_source_registered()
    ):
        return []
    self._no_source_nudges += 1
    self._record_intervention(
        "NO_SOURCE_NUDGE", self._name,
        "No canonical source registered; reminded that any agent can "
        "author and register one.",
        notice=self._no_source_nudges, cap=3,
    )
    return [{
        "role": "user",
        "content": (
            "[SETUP] No canonical ground-truth source is registered "
            "for this study (no evaluator entrypoint or lookup pool). "
            "Any agent can author and register one: write "
            "generators/registration.json in its delegation folder, so "
            "evaluations flow through get_evaluator(), land in the "
            "canonical store, and the result is reproducible. If this "
            "is intentionally a source-free (surrogate-only) study, "
            f"disregard this. (notice {self._no_source_nudges}/3)"
        ),
    }]
_backlog_announcement() -> list[dict] #

Announce the process backlog ONCE, at the start of the run.

As a conversation message, so the agent cannot claim it didn't know these gate the implementer. Injected the first time this node runs.

Source code in src/adda/_src/nodes/orchestration.py
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
def _backlog_announcement(self) -> list[dict]:
    """Announce the process backlog ONCE, at the start of the run.

    As a conversation message, so the agent cannot claim it didn't know
    these gate the implementer. Injected the first time this node runs.
    """
    if self._milestones is None or getattr(self, "_backlog_announced", False):
        return []
    from ..epistemics.milestones import render_backlog
    _bl = render_backlog(self._milestones)
    self._backlog_announced = True
    return [{"role": "user", "content": _bl}] if _bl else []
_invoke_turn(messages: list[dict]) -> Any #

Run one model turn and account for what it spent.

Source code in src/adda/_src/nodes/orchestration.py
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
def _invoke_turn(self, messages: list[dict]) -> Any:
    """Run one model turn and account for what it spent."""
    from langchain_core.messages import AIMessage

    # DEBUG: stream this turn's full reasoning + tool-calls to
    # debug/transcripts/<node name>/turn_NNN.jsonl.
    from ..backends.base import (
        bind_run_context as _bind_rc,
    )
    from ..backends.base import (
        debug_enabled as _dbg,
    )
    from ..backends.base import (
        set_transcript_sink as _set_sink,
    )
    from ..infra.sequence import next_number
    self._turn_count = next_number(
        getattr(self, "_turn_count", 0),
        (self._current_notes_dir.parent / "transcripts" / self._name
         if self._current_notes_dir is not None else None),
        "turn_", ".jsonl")
    import contextlib

    from ..backends.base import collect_trace
    from ..runtime import features
    _threads = (features.enabled("node_threads")
                and self._current_notes_dir is not None)
    _sink = None
    if (_dbg() or _threads) and self._current_notes_dir is not None:
        _sink = str(
            self._current_notes_dir.parent / "transcripts"
            / self._name / f"turn_{self._turn_count:03d}.jsonl")
        _set_sink(_sink)
    _notes = self._current_notes_dir
    _rc_path = (
        str(_notes.parent / "run_config.json") if _notes is not None else None
    )
    _collect = (collect_trace(_sink) if _threads and _sink
                else contextlib.nullcontext([]))
    with _bind_rc(f"{self._name}-turn-{self._turn_count:03d}", _rc_path), \
            _collect as _trace:
        text = self.adapter.invoke(messages)
    if _threads:
        self._thread_turn_block(messages, list(_trace), text)
    # Accumulate this node's own token usage.
    self._record_usage(
        getattr(self.adapter, "last_usage", {}) or {},
        model=getattr(self.adapter, "model", None),
        phase=f"{self._name}_turn",
        delegation_id=None,
        node=self._name,
    )
    return AIMessage(content=text)
_thread_turn_block(messages: list[dict], trace: list, text: str) -> None #

Append this graph-node turn to the node's thread (D1 rule 4): what arrived since its last reply, its calls and results, its reply.

Source code in src/adda/_src/nodes/orchestration.py
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
def _thread_turn_block(self, messages: list[dict], trace: list,
                       text: str) -> None:
    """Append this graph-node turn to the node's thread (D1 rule 4): what
    arrived since its last reply, its calls and results, its reply."""
    from .node_threads import NodeThread, trace_messages
    i = len(messages)
    while i > 0 and messages[i - 1].get("role") not in (
            "assistant", "ai"):
        i -= 1
    arrived = "\n\n".join(str(m.get("content", "")) for m in messages[i:])
    NodeThread(self._current_notes_dir.parent.parent, self._name).append(
        f"{self._name}-turn-{self._turn_count:03d}", 0,
        trace_messages(arrived, trace, str(text)))
_route_turn(state: AgenticState, ai_msg: Any) -> Any #

A turn ends in exactly one of three ways.

  1. Work is still in flight → re-prompt, free (no attempt spent)
  2. It stopped without closing → re-prompt, bounded (3 attempts)
  3. Otherwise → the run ends

Reproduction is owned entirely by the Done() gate (it runs the controlled gate before any close and declares a FAILED run after a bounded number of sighted attempts — see RunNotebook(gate=True)), so there is no separate post-accept repro check here; this handles only deliverable presence and un-accepted termination.

Source code in src/adda/_src/nodes/orchestration.py
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
def _route_turn(self, state: AgenticState, ai_msg: Any) -> Any:
    """A turn ends in exactly one of three ways.

    1. Work is still in flight     → re-prompt, free (no attempt spent)
    2. It stopped without closing  → re-prompt, bounded (3 attempts)
    3. Otherwise                   → the run ends

    Reproduction is owned entirely by the Done() gate (it runs the
    controlled gate before any close and declares a FAILED run after a
    bounded number of sighted attempts — see RunNotebook(gate=True)), so there is
    no separate post-accept repro check here; this handles only deliverable
    presence and un-accepted termination.
    """
    from ..runtime import terminal
    held = self._wind_down_route(state, ai_msg)
    if held is not None:
        return held
    if self._awaiting_retro and _extract_retrospective(str(ai_msg.content)):
        # The exit interview was answered in prose, not through Done():
        # the same record, so capture it rather than ask a fresh session
        # to answer again (wind_down.py does the same).
        from .tools.routing.feedback import FeedbackTools
        FeedbackTools(self)._capture_retrospective(
            str(ai_msg.content), "")
    if self._route.get("kind") == "handoff":
        return self._hand_off(ai_msg)
    accepted = self._route.get("kind") == "done"
    present, missing = self._deliverable_status(state)
    if token_clock.stop_info() is not None:
        token_clock.note_stop(
            deliverables_present=present, deliverables_missing=missing)
        return self._terminate_run(
            state, ai_msg, False, missing,
            termination=terminal.BUDGET_STOP)
    for router in (self._reprompt_while_working,
                   self._reprompt_unfinished):
        held = router(ai_msg, accepted, missing)
        if held is not None:
            return held
    return self._terminate_run(state, ai_msg, accepted, missing)
_reprompt_while_working(ai_msg: Any, accepted: bool, missing: list) -> Any | None #

Delegations still running: re-prompt WITHOUT spending an attempt.

A healthy delegation still in flight is WORK IN PROGRESS, not a failed finish: the deliverables usually depend on its result, and it WILL report. Spending a bounded finish-attempt on it means a slow-but-healthy delegation (run-4: D004 at ~2.5 evals/s, ~100s from done, with wall budget to spare) burns 3 "finish attempts" across turns and force- terminates the run UNGATED. At the time budget the wind-down (nodes/wind_down.py) takes over and handles a delegation that truly hangs.

Source code in src/adda/_src/nodes/orchestration.py
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
def _reprompt_while_working(
    self, ai_msg: Any, accepted: bool, missing: list
) -> Any | None:
    """Delegations still running: re-prompt WITHOUT spending an attempt.

    A healthy delegation still in flight is WORK IN PROGRESS, not a failed
    finish: the deliverables usually depend on its result, and it WILL
    report. Spending a bounded finish-attempt on it means a slow-but-healthy
    delegation (run-4: D004 at ~2.5 evals/s, ~100s from done, with wall
    budget to spare) burns 3 "finish attempts" across turns and force-
    terminates the run UNGATED. At the time budget the wind-down
    (nodes/wind_down.py) takes over and handles a delegation that
    truly hangs.
    """
    from langchain_core.messages import HumanMessage
    from langgraph.types import Command

    if accepted:
        return None
    working = self._working_delegations()
    if not working:
        return None
    collect = "collect them with Wait" if self._holds("Wait") else (
        "let them finish")
    close = "call Done()" if self._holds("Done") else "end your turn"
    msg = (
        f"Delegations still running: {working}. They are"
        f" progressing — {collect} and {close} only"
        " once they report (then write any remaining deliverables"
        " from their results). Do NOT close early. This wait does"
        " NOT count against your finish attempts; the wind-down at"
        " the budget ends the run."
    )
    if missing:
        msg += "\n\nStill to write AFTER they finish: " + ", ".join(missing)
    return Command(
        goto=self._name,
        update=self._thread_update([ai_msg, HumanMessage(content=msg)]),
    )
_reprompt_unfinished(ai_msg: Any, accepted: bool, missing: list) -> Any | None #

Bounded re-prompt when the turn ended without an accepted close.

Source code in src/adda/_src/nodes/orchestration.py
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
def _reprompt_unfinished(
    self, ai_msg: Any, accepted: bool, missing: list
) -> Any | None:
    """Bounded re-prompt when the turn ended without an accepted close."""
    from langchain_core.messages import HumanMessage
    from langgraph.types import Command

    from ..runtime import features
    if (accepted and not missing) or self._finish_attempts >= 3 \
            or self._silent:
        return None
    if not features.enabled("reprompt_unfinished"):
        return None
    self._finish_attempts += 1
    problems: list[str] = []
    if missing:
        missing_list = "\n".join(f"- {p}" for p in missing)
        write = ("Write them via WriteDeliverable()"
                 if self._holds("WriteDeliverable")
                 else "Write them to the study directory")
        then = ("calling Done()" if self._holds("Done")
                else "ending your turn")
        problems.append(
            "Required deliverables are missing from the"
            f" study directory:\n{missing_list}\n"
            f"{write} before {then}."
        )
    if not accepted:
        working = self._working_delegations()
        if working:
            collect = ("Collect them with Wait" if self._holds("Wait")
                       else "Let them finish")
            close = ("call Done()" if self._holds("Done")
                     else "end your turn")
            problems.append(
                f"Delegations still running: {working}."
                f" {collect} and {close} once they finish."
            )
        elif self._awaiting_retro:
            send = ("Call Done() once more" if self._holds("Done")
                    else "Reply")
            problems.append(
                "The run is waiting for your retrospective."
                f" {send} with a ### Retrospective block."
            )
        elif self._holds("Done"):
            problems.append(
                "You ended your turn without an accepted"
                " Done(). If Done() was refused (a release"
                " check, unread messages, open items, or another"
                " gate), address the refusal and call Done()"
                " again. A run only closes through an"
                " accepted Done()."
            )
        elif self._holds("Handoff"):
            problems.append(
                "You ended your turn without a Handoff. You own the"
                " job: finish the work, then pass it on with Handoff."
                " If a Handoff was refused, fix the check it names and"
                " hand off again."
            )
        else:
            problems.append(
                "You ended your turn before the work was finished."
                " Finish it, then end your turn."
            )
    return Command(
        goto=self._name,
        update=self._thread_update([
            ai_msg,
            HumanMessage(content=(
                "Run cannot complete"
                f" (attempt {self._finish_attempts}/3):\n"
                + "\n\n".join(problems)
            )),
        ]),
    )
_is_entry_thread() -> bool #

The entry node's conversation is messages; see graph_state.

Source code in src/adda/_src/nodes/orchestration.py
1450
1451
1452
1453
def _is_entry_thread(self) -> bool:
    """The entry node's conversation is ``messages``; see graph_state."""
    return self._spec is None or getattr(
        self._spec, "entry", None) == self._name
_my_messages(state: AgenticState) -> list #

This node's own conversation in the run state.

Source code in src/adda/_src/nodes/orchestration.py
1455
1456
1457
1458
1459
def _my_messages(self, state: AgenticState) -> list:
    """This node's own conversation in the run state."""
    if self._is_entry_thread():
        return state["messages"]
    return (state.get("threads") or {}).get(self._name, [])
_thread_update(msgs: list) -> dict #

A state update that appends msgs to this node's conversation.

Source code in src/adda/_src/nodes/orchestration.py
1461
1462
1463
1464
1465
def _thread_update(self, msgs: list) -> dict:
    """A state update that appends ``msgs`` to this node's conversation."""
    if self._is_entry_thread():
        return {"messages": msgs}
    return {"threads": {self._name: msgs}}
_job_owner() -> str | None #

The node that owns the job now (the entry until a handoff).

Source code in src/adda/_src/nodes/orchestration.py
1467
1468
1469
def _job_owner(self) -> str | None:
    """The node that owns the job now (the entry until a handoff)."""
    return self._owner or getattr(self._spec, "entry", None)
_handoff_body(target: str, message: str) -> str #

What the receiver reads after the handoff line. A node that holds Done receives work to accept or hand back: the message as the proposed conclusion, the job packet and the review rules, and during the wind-down what each answer does. Any other node reads the message.

Source code in src/adda/_src/nodes/orchestration.py
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
def _handoff_body(self, target: str, message: str) -> str:
    """What the receiver reads after the handoff line. A node that holds
    Done receives work to accept or hand back: the message as the
    proposed conclusion, the job packet and the review rules, and during
    the wind-down what each answer does. Any other node reads the message."""
    agent = self._spec.nodes.get(target) if self._spec else None
    if "Done" not in getattr(agent, "tools", ()):
        return message
    from ..runtime import features
    from ..runtime.constraint_snapshot import snapshot_for_node
    from .handoff_packet import handoff_packet
    from .tools.routing.feedback import _REVIEW_RULES
    body = (handoff_packet(self, message, snapshot_for_node(self))
            + features.resolve_gates(_REVIEW_RULES))
    if self._wind_down_active():
        body += ("\n\nWind-down: Done accepts; Handoff back closes the run "
                 "as not accepted.")
    return body
_can_own_job() -> bool #

Whether the job can reach this node: it is the entry or a handoff target.

Source code in src/adda/_src/nodes/orchestration.py
1490
1491
1492
1493
1494
1495
1496
1497
1498
def _can_own_job(self) -> bool:
    """Whether the job can reach this node: it is the entry or a
    handoff target."""
    spec = getattr(self, "_spec", None)
    if spec is None:
        return False
    return self._name == spec.entry or any(
        getattr(e, "handoff", False) and e.target == self._name
        for e in spec.edges)
_hand_off(ai_msg: Any) -> Any #

End this turn by passing the job to the Handoff target.

Source code in src/adda/_src/nodes/orchestration.py
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
def _hand_off(self, ai_msg: Any) -> Any:
    """End this turn by passing the job to the Handoff target."""
    from langchain_core.messages import HumanMessage
    from langgraph.types import Command

    target = self._route["target"]
    from .tools.routing.handoff import (
        close_tenure,
        open_tenure,
        subfolder_tag,
    )
    log = self._delegation_log
    receiver = next((n for n in self._stop_nodes() if n._name == target),
                    None)
    tenure = None
    if log is not None and receiver is not None:
        close_tenure(log, self, "HANDED_OFF", self._route["message"])
        tenure = open_tenure(log, receiver, self._name,
                             self._route["message"])
    tag = (subfolder_tag(tenure) if tenure and target != getattr(
        self._spec, "entry", None) else "")
    note = (f"[Handoff from {self._name}: you now own the job.]\n\n"
            + tag + self._handoff_body(target, self._route["message"]))
    # The sender's own thread keeps what it handed over (a review it
    # handed back, say): its next tenure starts from it.
    sent = HumanMessage(content=f"[You handed the job to {target}. "
                        f"Your message:]\n{self._route['message']}")
    update = {**self._thread_update([ai_msg, sent]), "owner": target}
    theirs = {target: [HumanMessage(content=note)]}
    if target == getattr(self._spec, "entry", None):
        update.setdefault("messages", []).extend(theirs[target])
    else:
        update["threads"] = {**update.get("threads", {}), **theirs}
    self._owner = target
    self._record_intervention(
        "HANDOFF", target, f"{self._name} handed the job to {target}",
        fault="observation", source=self._name)
    from .subscriptions import flush_after_tenure, wait_idle
    if receiver is not None:
        wait_idle(receiver)          # no wake runs beside its tenure
    flush_after_tenure(self)
    return Command(goto=target, update=update)
_actionable(notices: str) -> str #

Only the notice lines this node can act on: a line that asks for a ledger write reaches only a node holding that write tool. Which events fire, and their text, are the monitor's; this is delivery.

Source code in src/adda/_src/nodes/orchestration.py
1546
1547
1548
1549
1550
1551
1552
1553
1554
def _actionable(self, notices: str) -> str:
    """Only the notice lines this node can act on: a line that asks for a
    ledger write reaches only a node holding that write tool. Which
    events fire, and their text, are the monitor's; this is delivery."""
    import re
    keep = [ln for ln in notices.splitlines(keepends=True)
            if all(self._holds(t) for t in self.LEDGER_WRITES
                   if re.search(rf"\b{t}\b", ln))]
    return "".join(keep)
_holds(tool: str) -> bool #

Whether this node was handed the adda tool tool.

Every text adda writes to a node names only tools the node holds.

Source code in src/adda/_src/nodes/orchestration.py
1556
1557
1558
1559
1560
1561
def _holds(self, tool: str) -> bool:
    """Whether this node was handed the adda tool ``tool``.

    Every text adda writes to a node names only tools the node holds.
    """
    return tool in self.adapter.closure_tools
_working_delegations() -> list[str] #

Delegation ids still in flight right now.

Source code in src/adda/_src/nodes/orchestration.py
1563
1564
1565
1566
1567
1568
1569
def _working_delegations(self) -> list[str]:
    """Delegation ids still in flight right now."""
    with self._registry_lock:
        return [
            d for d, e in self._registry.items()
            if e["status"] == "Working"
        ]
_terminate_run(state: AgenticState, ai_msg: Any, accepted: bool, missing: list, termination: str | None = None) -> Any #

Close the run: final counts, banner, ghost flush, terminal Command.

Source code in src/adda/_src/nodes/orchestration.py
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
def _terminate_run(
    self, state: AgenticState, ai_msg: Any, accepted: bool, missing: list,
    termination: str | None = None,
) -> Any:
    """Close the run: final counts, banner, ghost flush, terminal Command."""
    self._run_closed = True          # no wake starts after this
    from langgraph.graph import END
    from langgraph.types import Command

    from ..runtime import terminal

    # total_new: only delegations created THIS turn (seq delta vs the
    # snapshot taken at turn start), not Done entries from prior turns.
    with self._registry_lock:
        total_new = self._delegation_seq - self._seq_at_turn_start
        evals_new = sum(e["evals"] for e in self._registry.values())

    summary = self._banner(
        self._route.get("summary") or ai_msg.content, accepted, missing,
        stopped=termination)
    self._flush_ghost_delegations()
    from .tools.routing.handoff import close_tenure
    close_tenure(self._delegation_log, self, "DONE" if accepted else "CLOSED",
                 "This node's own work while it held the job; see the store.")
    self._record_intervention(
        "RUN_CLOSED", self._name, f"{self._name} closed the run",
        fault="observation", accepted=accepted, termination=termination)

    # The persisted (reported) eval total prefers the ledger aggregate over
    # the accumulator: the accumulator can drop evals a namespace-blind
    # guard mis-flagged as off-ledger, and never saw namespace stores at
    # all. The ledger across all namespaces is authoritative → run_status.
    _evals_persist = self._ledgered_eval_total(
        state.get("evals_used", 0) + evals_new)
    # The terminal triple, decided here rather than inferred from the
    # banner later. An un-accepted close never reached a gate, and a close
    # missing deliverables was never validated against them — both are
    # UNGATED regardless of what the route recorded. terminal.resolve
    # fails safe (unrecorded → UNGATED) and refuses GATED without a review.
    _outcome, _termination, _reviewed = terminal.resolve(
        self._route.get("outcome")
        if accepted and not missing else terminal.UNGATED,
        termination or (self._route.get("termination") if accepted
                        else terminal.NO_CLOSE),
        self._reviewed_so_far(),
    )
    return Command(
        goto=END,
        update={
            **self._thread_update([ai_msg]),
            "done": True,
            "last_report": summary,
            "total_delegations": state["total_delegations"] + total_new,
            "evals_used": _evals_persist,
            "token_totals": self._run_token_totals(),
            "error_counts": dict(self._error_counts),
            "outcome": _outcome,
            "termination": _termination,
            "reviewed": _reviewed,
        },
    )
_banner(summary: str, accepted: bool, missing: list, stopped: str | None = None) -> str #

Prepend the UNGATED banner when the run ends without an accepted Done().

A FAILED-reproduction close carries its own ⛔ banner in the route summary and IS accepted=done, so it is not re-banner'd here.

Source code in src/adda/_src/nodes/orchestration.py
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
def _banner(self, summary: str, accepted: bool, missing: list,
            stopped: str | None = None) -> str:
    """Prepend the UNGATED banner when the run ends without an accepted Done().

    A FAILED-reproduction close carries its own ⛔ banner in the route
    summary and IS accepted=done, so it is not re-banner'd here.
    """
    from ..runtime import features, terminal

    if (accepted and not missing) or not features.enabled(
            "reprompt_unfinished"):
        return summary
    flags = []
    if stopped == terminal.BUDGET_STOP:
        from ..infra import token_clock
        trig = (token_clock.stop_info() or {}).get("budget_trigger")
        flags.append("the session was stopped at the "
                     + {"wall": "wall-clock"}.get(trig, "output-token")
                     + " budget")
    elif not accepted:
        flags.append(
            "the run terminated WITHOUT an accepted Done() —"
            " the final conclusions were NOT accepted by a"
            " review"
        )
    if missing:
        flags.append(f"required deliverables missing: {missing}")
    return terminal.ungated_banner(flags) + summary
_flush_ghost_delegations() -> None #

Close out delegations whose threads die with the interpreter.

Daemon threads still alive when the run closes are killed at process exit — their run() never reaches the DONE/FAILED record write, leaving orphan RUNNING entries in the log. Write an INTERRUPTED terminal record for each so query_all() (last-wins) collapses to a closed state instead of RUNNING.

Source code in src/adda/_src/nodes/orchestration.py
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
def _flush_ghost_delegations(self) -> None:
    """Close out delegations whose threads die with the interpreter.

    Daemon threads still alive when the run closes are killed at process
    exit — their run() never reaches the DONE/FAILED record write, leaving
    orphan RUNNING entries in the log. Write an INTERRUPTED terminal record
    for each so query_all() (last-wins) collapses to a closed state instead
    of RUNNING.
    """
    with self._registry_lock:
        live = [
            (did, dict(entry))
            for did, entry in self._registry.items()
            if entry.get("status") == "Working"
        ]
    if not live or self._delegation_log is None:
        return
    _now = datetime.now(tz=timezone.utc).isoformat(timespec="seconds")
    for _did, _entry in live:
        self._delegation_log.record(
            id=_did,
            from_node=self._name,
            to_node=_entry.get("target", "unknown"),
            # An interrupted delegation never reached _finish_ok/_error,
            # so its partial writes are still uncommitted. Commit them
            # HERE, against the delegation that made them, or the next
            # delegation to commit absorbs them and the history says the
            # wrong worker wrote those files.
            workspace_sha=self._commit_workspace(
                f"{_did} {self._name} -> "
                f"{_entry.get('target', 'unknown')} [INTERRUPTED]"),
            task="",
            deliverable=(
                "INTERRUPTED: run closed while this delegation was "
                "still running (background thread killed at process exit)"
            ),
            hypothesis_ids=_entry.get("hypothesis_ids") or [],
            started_at=_entry.get("started_at") or "",
            completed_at=_now,
            status="INTERRUPTED",
            tokens_in=0,
            tokens_out=0,
            cost_usd=None,
            is_falsification_attempt=bool(
                _entry.get("is_falsification_attempt")
            ),
            evals=_entry.get("evals", 0),
            phase=_entry.get("phase"),
        )
_missing_deliverables(state: AgenticState) -> list[str] #

Return required deliverable paths not present at study_dir yet.

Source code in src/adda/_src/nodes/reproduction_gate.py
54
55
56
def _missing_deliverables(self, state: AgenticState) -> list[str]:
    """Return required deliverable paths not present at study_dir yet."""
    return self._deliverable_status(state)[1]
_deliverable_status(state: AgenticState) -> tuple[list[str], list[str]] #

(present, missing): the required deliverables at study_dir.

The single deliverable (pipeline.ipynb) is required, authored before Done() is accepted, UNLESS the study turns off pipeline_deliverable — it is the human-readable recipe AND the reproduction in one notebook: the runtime executes it lazily (see _reproduction_gate) to verify the headline re-derives from the ledger with zero new evals, which makes no sense for a study with no ledger at all. Requiring it unconditionally (BACKLOG #30) meant a pure-derivation study's strategizer could never satisfy Done() regardless of what its own tools/prompt said — this was the third of three places that assumption was baked in (the other two, the deliverable [[include]] blocks and the notebook-authoring tools themselves, are already gated the same way). Additional paths can still be declared in state['required_deliverables'] regardless of this flag.

Source code in src/adda/_src/nodes/reproduction_gate.py
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def _deliverable_status(
    self, state: AgenticState
) -> tuple[list[str], list[str]]:
    """``(present, missing)``: the required deliverables at study_dir.

    The single deliverable (pipeline.ipynb) is required, authored before
    Done() is accepted, UNLESS the study turns off pipeline_deliverable —
    it is the human-readable recipe AND the reproduction in one notebook:
    the runtime executes it lazily (see _reproduction_gate) to verify the
    headline re-derives from the ledger with zero new evals, which makes
    no sense for a study with no ledger at all. Requiring it unconditionally
    (BACKLOG #30) meant a pure-derivation study's strategizer could never
    satisfy Done() regardless of what its own tools/prompt said — this was
    the third of three places that assumption was baked in (the other two,
    the deliverable [[include]] blocks and the notebook-authoring tools
    themselves, are already gated the same way). Additional
    paths can still be declared in state['required_deliverables']
    regardless of this flag.
    """
    from ..evaluation.notebook_exec import required_deliverable_name
    from ..runtime import features
    from ..runtime.features import NOTEBOOK_TOOLS
    from ..runtime.study_config import deliverable_name
    study_dir = Path(state.get("study_dir", "."))
    # WriteDeliverable writes BARE names to study_dir/ (it rejects path
    # separators). Normalise any configured path to its basename so a stray
    # 'workspace/…' prefix in a study config can't spuriously flag a present
    # deliverable as missing.
    required = list(state.get("required_deliverables") or [])
    # The notebook is required when some node in the graph can author
    # it: the closer answers for it even when another node wrote it,
    # and a handoff notice and Done check the same thing.
    if features.enabled("pipeline_deliverable") and self._graph_authors(
            NOTEBOOK_TOOLS):
        required = [required_deliverable_name()] + required
    seen: set[str] = set()
    present: list[str] = []
    missing: list[str] = []
    for p in required:
        name = deliverable_name(p)
        if name in seen:
            continue
        seen.add(name)
        (present if (study_dir / name).exists() else missing).append(name)
    return present, missing
_graph_authors(tools: frozenset) -> bool #

True when some node in the graph holds one of tools (a node with no declared toolset counts). Without a graph, this node.

Source code in src/adda/_src/nodes/reproduction_gate.py
104
105
106
107
108
109
110
111
112
113
114
def _graph_authors(self, tools: frozenset) -> bool:
    """True when some node in the graph holds one of ``tools`` (a node
    with no declared toolset counts). Without a graph, this node."""
    spec = getattr(self, "_spec", None)
    if spec is None:
        return not self._tools_declared or bool(self._agent_tools & tools)
    for agent in spec.nodes.values():
        declared = getattr(agent, "tools", None)
        if declared is None or set(declared) & tools:
            return True
    return False
_reproduction_gate(state: AgenticState | None = None) -> str | None #

Before Done() can close a run — and on every RunNotebook(gate=True) dry run — pipeline.ipynb must satisfy every one of these, checked in order. This is a MECHANICAL check this exact function executes every time, not a judgement call, and nothing narrative or intent-based can satisfy it in a check's place.

  1. The canonical store must already hold at least one oracle row. Zero rows means no campaign has been evaluated yet, so there is nothing for the notebook to reproduce FROM — the gate refuses outright, before even running the notebook.
  2. The notebook must finish cleanly within a time ceiling (no heavy from-scratch computation — a reproduction is lazy, not a re-run).
  3. It must add ZERO new oracle rows: it may only LOAD the ledger (ExperimentData.from_file) and reach the oracle through get_evaluator(), which skips every already-FINISHED row.
  4. It must NOT modify or delete any existing ledger row — no faking a zero-delta by delete-then-re-add or by rewriting a value.
  5. The notebook prints a freshly-computed REPRODUCED: <value> and the CLAIMED_HEADLINE: <value> its write-up states. The gate fails the notebook when the two differ. It does not fail a notebook for a missing print.

Passing (0)-(4) means the deliverable is a faithful, lightweight, lazy reproduction of a real, already-evaluated campaign — not a script doing something unrelated to validating the pipeline.

Source code in src/adda/_src/nodes/reproduction_gate.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
def _reproduction_gate(self, state: AgenticState | None = None) -> str | None:
    """Before Done() can close a run — and on every RunNotebook(gate=True)
    dry run — pipeline.ipynb must satisfy every one of these, checked in
    order. This is a MECHANICAL check this exact function executes every
    time, not a judgement call, and nothing narrative or intent-based can
    satisfy it in a check's place.

      0. The canonical store must already hold at least one oracle row.
         Zero rows means no campaign has been evaluated yet, so there is
         nothing for the notebook to reproduce FROM — the gate refuses
         outright, before even running the notebook.
      1. The notebook must finish cleanly within a time ceiling (no heavy
         from-scratch computation — a reproduction is lazy, not a re-run).
      2. It must add ZERO new oracle rows: it may only LOAD the ledger
         (``ExperimentData.from_file``) and reach the oracle through
         ``get_evaluator()``, which skips every already-FINISHED row.
      3. It must NOT modify or delete any existing ledger row — no faking
         a zero-delta by delete-then-re-add or by rewriting a value.
      4. The notebook prints a freshly-computed ``REPRODUCED: <value>``
         and the ``CLAIMED_HEADLINE: <value>`` its write-up states. The
         gate fails the notebook when the two differ. It does not fail
         a notebook for a missing print.

    Passing (0)-(4) means the deliverable is a faithful, lightweight,
    lazy reproduction of a real, already-evaluated campaign — not a
    script doing something unrelated to validating the pipeline.
    """
    # Developer notes (not part of the agent-facing contract above, see
    # gate_contract() below): returns None on PASS (and stashes
    # self._repro_ok_detail), else a problem string describing which
    # check failed. Skips silently when there is no run context; callable
    # without `state` (study dir comes from self._study_dir). (d)'s
    # REPRODUCED marker is informational for the critic/human — the
    # runtime does not gate on the value itself, only on (4)'s
    # self-consistency; an independent runtime extremum match used to
    # wrongly reject legitimate constrained optima.
    from ..runtime import features
    if not features.enabled("reproduction_gate"):
        return None

    import re
    import subprocess

    study_dir = (
        Path(self._study_dir) if getattr(self, "_study_dir", None) is not None
        else Path((state or {}).get("study_dir", "."))
    )
    # The deliverable is pipeline.ipynb. (A .py is still executable by the
    # executor-agnostic gate, kept only as a fallback for gate-logic tests;
    # the notebook is preferred when both are present.) Absence is left to
    # _missing_deliverables.
    deliverable = next(
        (study_dir / n for n in ("pipeline.ipynb", "pipeline.py")
         if (study_dir / n).exists()),
        None,
    )
    if deliverable is None:
        return None  # absence is handled by _missing_deliverables
    if deliverable.suffix == ".ipynb":
        # NOTEBOOK-LEDGER SYNC, by construction: refresh the hypotheses
        # cell's ledger-status block before anything else runs. This one
        # hook covers BOTH call sites that reach here — RunNotebook
        # (gate=True) and Done()'s pre-critic check — so a stale status
        # is impossible the moment either reads the notebook, rather
        # than merely detected once it's already been read.
        from .tools.routing.notebook import refresh_hypotheses_ledger_block
        # Reassigned every call (None on a no-op refresh) so a stale
        # rev from an EARLIER call is never re-surfaced by _gate_check.
        self._hypotheses_rev_after_refresh = refresh_hypotheses_ledger_block(
            study_dir, self._read_ledger())
    # One resolver for "where is this run", shared with the store tools:
    # they used to compute it separately and could disagree.
    if self._current_notes_dir is None:
        return None  # no run dir context (e.g. non-debug) — skip the gate
    run_dir = self._resolve_run_dir()          # …/runs/<id>
    store_dir = run_dir / "experiment_data"
    run_config = run_dir / "debug" / "run_config.json"

    from ..evaluation.notebook_exec import (
        ledger_snapshot as _ledger_snapshot,
    )

    # ── HERMETIC SANDBOX ──────────────────────────────────────────────────
    # CRITICAL: run the deliverable against a COPY of the canonical store, never
    # the live one. A faithful lazy pipeline adds nothing; a NON-lazy one
    # (re-evaluating) writes its evals into the THROWAWAY copy — we detect
    # that as "not lazy" while the real ledger stays pristine. Without this,
    # checking a non-lazy pipeline pollutes + inflates the canonical store
    # (and the gate check could be looped to balloon it without bound).
    before_n, before_hash = _ledger_snapshot(store_dir)
    if before_n == 0:
        return (
            "Canonical store has no rows — the campaign has not been "
            "evaluated yet. Run the delegation pipeline first so the "
            "ledger is populated, then the notebook can be reproduced "
            "lazily against those rows.")
    from ..evaluation.notebook_exec import replay_sandbox
    with replay_sandbox(
            store_dir, run_config, self._study_dir,
    ) as (sandbox, sb_store, env):
        _timeout = (
            max(0.1 * self._budget_seconds, 180.0)
            if self._budget_seconds else 300.0
        )
        try:
            # Executor-agnostic: a .ipynb runs via nbclient (in-env kernel),
            # a .py via subprocess — both return a CompletedProcess and raise
            # TimeoutExpired on timeout, so the asserts below are unchanged.
            from ..evaluation.notebook_exec import run_deliverable
            proc = run_deliverable(
                deliverable, cwd=sandbox, env=env, timeout=_timeout)
        except subprocess.TimeoutExpired:
            return (
                f"{deliverable.name} did not finish within {_timeout:.0f}s. A "
                "reproduction must be lightweight — load the ledger and skip "
                "finished evals and heavy refits (cache-or-load surrogates). "
                "Make it lazy.")
        after_n, after_hash = _ledger_snapshot(sb_store)

    # (a) clean exit — surface a generous stderr tail for sighted debugging.
    if proc.returncode != 0:
        return (
            f"{deliverable.name} FAILED to run (exit {proc.returncode}). It must "
            "load the ledger and derive the headline cleanly. Stderr:\n"
            + (proc.stderr or "")[-3000:]
            + ("\n\nStdout tail:\n" + proc.stdout[-800:]
               if proc.stdout else ""))
    # (b) zero new evals (lazy).
    if after_n != before_n:
        return (
            f"{deliverable.name} is NOT lazy: re-running it changed the ledger row "
            f"count ({before_n} → {after_n}). It must LOAD the ledger "
            "(ExperimentData.from_file) and reach the oracle only via "
            "get_evaluator() so FINISHED rows are skipped — zero new evals.")
    # (c) integrity — existing rows unchanged.
    if before_hash and after_hash and before_hash != after_hash:
        return (
            f"{deliverable.name} MODIFIED existing ledger rows. A reproduction must "
            "read the ledger READ-ONLY (it may re-store identical rows, but "
            "must not rewrite values or delete+re-add). Do not tamper with "
            "the canonical store.")
    # (d) The printed ``REPRODUCED:`` line is an informational headline
    # marker for the critic / human reader — the runtime no longer gates on
    # it. Headline GROUNDING (the value traces to a real ledger row) is
    # owned by the critic's HEADLINE PROVENANCE check; an independent
    # runtime extremum match wrongly rejected legitimate CONSTRAINED optima
    # (a constrained best is, by definition, not an objective extremum), so
    # it forced studies to headline their infeasible unconstrained extremum
    # — see audit run 20260624T021359.
    # (e) internal consistency — if the notebook declares CLAIMED_HEADLINE
    # (the value its write-up states), it must equal the freshly-computed
    # REPRODUCED. Lenient: skips when the marker is absent.
    _hc = _headline_consistency(proc.stdout or "")
    if _hc is not None:
        return _hc
    m = re.search(r"REPRODUCED:\s*([-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?)",
                  proc.stdout or "")
    headline = f", REPRODUCED={m.group(1)}" if m else ""
    self._repro_ok_detail = (
        f"reproduced cleanly ({before_n} rows, unchanged, 0 new evals, "
        f"ran in <{_timeout:.0f}s{headline})")
    return None
_halt_resumable(state: Any, *, reason: str, termination: str, status: str = 'halted', extra_update: dict | None = None) #

Checkpoint-and-halt cleanly on an unrecoverable condition.

Instead of crashing, write debug/run_status.json (so tooling and the human can see the run is resumable) and return a Command to END whose last_report is prefixed with a HALTED banner. The durable SqliteSaver checkpoint + the persisted thread_id are what make the run resumable via AgenticRun(resume_from=...) — no new serialized state is introduced here.

termination names WHICH unrecoverable condition fired. It is carried on the state so the close path records it verbatim instead of inferring an outcome from the HALTED banner — which matched none of the old banner patterns and so read as GATED, logging every backstop kill as a validated success.

Source code in src/adda/_src/nodes/lifecycle.py
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def _halt_resumable(
    self,
    state: Any,
    *,
    reason: str,
    termination: str,
    status: str = "halted",
    extra_update: dict | None = None,
):
    """Checkpoint-and-halt cleanly on an unrecoverable condition.

    Instead of crashing, write ``debug/run_status.json`` (so tooling and
    the human can see the run is resumable) and return a ``Command`` to
    ``END`` whose ``last_report`` is prefixed with a HALTED banner.  The
    durable SqliteSaver checkpoint + the persisted ``thread_id`` are what
    make the run resumable via ``AgenticRun(resume_from=...)`` — no new
    serialized state is introduced here.

    ``termination`` names WHICH unrecoverable condition fired. It is
    carried on the state so the close path records it verbatim instead of
    inferring an outcome from the HALTED banner — which matched none of the
    old banner patterns and so read as GATED, logging every backstop kill
    as a validated success.
    """
    import json as _json

    from langchain_core.messages import AIMessage
    from langgraph.graph import END
    from langgraph.types import Command

    from ..runtime import features as _features

    run_dir = state.get("run_dir")
    thread_id = None
    if run_dir:
        debug_dir = Path(run_dir) / "debug"
        tid_path = debug_dir / "thread_id"
        try:
            if tid_path.exists():
                thread_id = tid_path.read_text().strip()
        except OSError:
            thread_id = None
        try:
            debug_dir.mkdir(parents=True, exist_ok=True)
            from ..infra.usage_pause import fault_record as _fault_record
            (debug_dir / "run_status.json").write_text(
                _json.dumps(
                    {
                        "status": status,
                        "reason": reason,
                        "resumable": True,
                        "thread_id": thread_id,
                        "outcome": terminal.UNGATED,
                        "termination": termination,
                        "reviewed": self._reviewed_so_far(),
                        "arms": _features.arm_config(),
                        **({"faults": _fault_record()}
                           if _fault_record() else {}),
                    },
                    indent=2,
                ),
                encoding="utf-8",
            )
        except OSError:
            pass

    # Preserve the latest conclusion below the banner.
    prior_text = ""
    for _m in reversed(self._my_messages(state)):
        if isinstance(_m, AIMessage):
            prior_text = str(_m.content)
            break

    banner = f"## ⚠ HALTED (resumable) — {reason}\n\n"
    update = {
        "messages": [],
        "done": True,
        "last_report": banner + (prior_text or "(no prior report)"),
        "token_totals": self._run_token_totals(),
        "error_counts": dict(self._error_counts),
        "outcome": terminal.UNGATED,
        "termination": termination,
        "reviewed": self._reviewed_so_far(),
    }
    if extra_update:
        update.update(extra_update)
    return Command(goto=END, update=update)
_reviewed_so_far() -> bool #

Whether a node that was handed the job has reviewed it in this run (a review round, on any node). A close that ends the run without an acceptance (a halt, a stop, a failed reproduction) still records a review that took place.

Source code in src/adda/_src/nodes/lifecycle.py
104
105
106
107
108
109
110
def _reviewed_so_far(self) -> bool:
    """Whether a node that was handed the job has reviewed it in this
    run (a review round, on any node). A close that ends the run without
    an acceptance (a halt, a stop, a failed reproduction) still records
    a review that took place."""
    return bool(getattr(self, "_route", {}).get("reviewed")) or any(
        getattr(n, "_review_rounds", 0) for n in self._stop_nodes())
_halt_tallies(state: Any) -> dict #
Source code in src/adda/_src/nodes/lifecycle.py
112
113
114
115
116
117
118
119
def _halt_tallies(self, state: Any) -> dict:
    with self._registry_lock:
        _total_new = len(self._registry)
        _evals_new = sum(e["evals"] for e in self._registry.values())
    return {
        "total_delegations": state["total_delegations"] + _total_new,
        "evals_used": state.get("evals_used", 0) + _evals_new,
    }
_close_after_wind_down_error(state: Any, exc: Exception) #

The turn that was to close a wind-down raised. The run closes anyway, with the halt's own termination, and says which retrospectives it lacks.

Source code in src/adda/_src/nodes/lifecycle.py
121
122
123
124
125
126
127
128
129
def _close_after_wind_down_error(self, state: Any, exc: Exception):
    """The turn that was to close a wind-down raised. The run closes anyway,
    with the halt's own termination, and says which retrospectives it lacks."""
    reason = (f"the wind-down turn failed ({type(exc).__name__}: "
              f"{str(exc)[:200]})")
    self._log_missing_retrospectives(reason)
    return self._halt_resumable(
        state, reason=reason, termination=self._stop_termination(),
        extra_update=self._halt_tallies(state))
_trip(state: Any, *, drift: dict, reason: str, termination: str, tallies: dict) -> Command | None #

A backstop condition holds. Ask the run to wind down — every agent gives its retrospective, the run closes with termination — instead of jumping to END with none. While the wind-down is in progress this returns None (the condition keeps holding; that is expected). Only a wind-down that overruns its grace, or one that cannot be asked for, falls through to the hard halt, so a halt is always bounded.

Source code in src/adda/_src/nodes/lifecycle.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def _trip(
    self, state: Any, *, drift: dict, reason: str, termination: str,
    tallies: dict,
) -> Command | None:
    """A backstop condition holds. Ask the run to wind down — every agent
    gives its retrospective, the run closes with ``termination`` — instead of
    jumping to END with none. While the wind-down is in progress this
    returns None (the condition keeps holding; that is expected). Only a
    wind-down that overruns its grace, or one that cannot be asked for,
    falls through to the hard halt, so a halt is always bounded."""
    if self._stop is None:
        self._record_science_drift(drift)
        if self._request_wind_down(
            reason=reason, termination=termination,
            run_dir=state.get("run_dir")):
            self._record_intervention(
                "BACKSTOP_WIND_DOWN", "(run)",
                f"{reason}; winding down for retrospectives, closing "
                f"{termination}")
            return None
    elif not self._wind_down_overdue():
        return None
    else:
        self._record_science_drift({**drift, "wind_down": "overran"})
        self._log_missing_retrospectives(
            f"{reason}; the wind-down overran its grace")
    return self._halt_resumable(
        state, reason=reason, termination=termination,
        extra_update=tallies)
_check_unrecoverable(state: Any, budget: float | None, start: float | None) -> Command | None #

Return a halt Command if an unrecoverable condition is met, else None. Extracted verbatim from call (USD ceiling → repeated errors).

Source code in src/adda/_src/nodes/lifecycle.py
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
def _check_unrecoverable(self, state: Any, budget: float | None, start: float | None) -> Command | None:
    """Return a halt Command if an unrecoverable condition is met, else None.
    Extracted verbatim from __call__ (USD ceiling → repeated errors)."""

    # (1) USD cost ceiling. Hard, resumable (raise budget_usd and resume).
    # Inactive under ollama (no cost data): warn once, never halt.
    _budget_usd = self._budget_usd
    if _budget_usd is not None and _budget_usd > 0:
        _spent, _observed = self._run_spend_usd()
        if not _observed:
            if (
                getattr(self, "_turn_count", 0) >= 1
                and not self._usd_inactive_warned
            ):
                self._usd_inactive_warned = True
                self._record_science_drift({
                    "error_type": "USD_BUDGET_INACTIVE",
                    "budget_usd": _budget_usd,
                    "note": "no per-call cost reported (e.g. ollama); "
                            "USD ceiling treated as inactive",
                })
        elif _spent >= _budget_usd:
            return self._trip(
                state,
                drift={
                    "error_type": "USD_BACKSTOP",
                    "spent_usd": _spent,
                    "budget_usd": _budget_usd,
                },
                reason=(
                    f"USD budget exhausted "
                    f"(${_spent:.4f} / ${_budget_usd:.4f})"
                ),
                termination=terminal.BACKSTOP_USD,
                tallies=self._halt_tallies(state),
            )

    # (2) Repeated errors: a target failing N times in a row (genuine
    # worker EXCEPTIONS, not REVISE loops or poor results — those reset the
    # streak on any success) is not going to self-heal by looping the
    # strategizer at it again. The default is deliberately CONSERVATIVE: a
    # legitimate run "stuck" in the scientific process loops on critic
    # verdicts and slow delegations, none of which count here — only hard
    # consecutive crashes do. Knob: max_consecutive_errors (config.yaml
    # runtime block; F3DASM_MAX_CONSECUTIVE_ERRORS overrides); 0 disables.
    from ..runtime.settings import get_int
    _max_err = get_int("max_consecutive_errors", 12)
    if _max_err > 0:
        with self._registry_lock:
            _stuck = [
                (t, n) for t, n in self._consecutive_errors.items()
                if n >= _max_err
            ]
        if _stuck:
            _t, _n = _stuck[0]
            _down = _t in self._unreachable_targets
            return self._trip(
                state,
                drift={
                    "error_type": ("BACKEND_UNAVAILABLE" if _down
                                   else "REPEATED_ERRORS"),
                    "target": _t,
                    "consecutive": _n,
                },
                reason=(
                    f"backend endpoint unavailable: {_t} failed {_n}x "
                    "consecutively" if _down else
                    f"repeated errors: {_t} failed {_n}x consecutively"
                ),
                termination=(terminal.BACKEND_UNAVAILABLE if _down
                             else terminal.REPEATED_ERRORS),
                tallies=self._halt_tallies(state),
            )

    return None
_persist_review(text: str) -> str | None #

Record one review round by the node that closes the run: its Done summary (accepting) or its Handoff message (handing back), as the next critic_reviews/call_NNN.md. Returns the file name.

Source code in src/adda/_src/nodes/review_records.py
68
69
70
71
72
73
74
75
76
77
78
79
def _persist_review(self, text: str) -> str | None:
    """Record one review round by the node that closes the run: its Done
    summary (accepting) or its Handoff message (handing back), as the
    next ``critic_reviews/call_NNN.md``. Returns the file name."""
    from ..infra.sequence import next_number
    debug = (Path(self._current_notes_dir).parent
             if self._current_notes_dir is not None else None)
    self._review_rounds = getattr(self, "_review_rounds", 0) + 1
    self._critic_calls = next_number(
        getattr(self, "_critic_calls", 0),
        debug / "critic_reviews" if debug else None, "call_", ".md")
    return self._persist_critic_review(self._critic_calls, text)
_cited_delegation_brief(evidence: dict | None) -> str | None #

A short brief on the cited delegation — what it was asked to do and whether it was flagged a falsification attempt — so the judge can weigh §2 attempt-adequacy. The numeric result the verdict rests on is carried separately in evidence['numbers']. None if no delegation is cited or the record is absent.

Source code in src/adda/_src/nodes/review_records.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def _cited_delegation_brief(self, evidence: dict | None) -> str | None:
    """A short brief on the cited delegation — what it was asked to do and
    whether it was flagged a falsification attempt — so the judge can weigh
    §2 attempt-adequacy. The numeric result the verdict rests on is carried
    separately in ``evidence['numbers']``. None if no delegation is cited or
    the record is absent.
    """
    d = (evidence or {}).get("delegation")
    if not d or self._delegation_log is None:
        return None
    rec = next(
        (r for r in self._delegation_log.query_all() if r.get("id") == d),
        None,
    )
    if rec is None:
        return None
    return (
        f"delegation {d} (to {rec.get('to_node')}, "
        f"is_falsification_attempt={rec.get('is_falsification_attempt')}, "
        f"evals={rec.get('evals')}):\n"
        f"  task: {rec.get('task', '')}\n"
        f"  deliverable: {rec.get('deliverable', '')}"
    )
_persist_critic_brief(n: int, brief: str) -> dict | None #

Write the critic's full brief; return {file, sha256} or None.

Source code in src/adda/_src/nodes/review_records.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def _persist_critic_brief(self, n: int, brief: str) -> dict | None:
    """Write the critic's full brief; return {file, sha256} or None."""
    import hashlib
    sha = hashlib.sha256((brief or "").encode("utf-8")).hexdigest()
    try:
        notes = self._current_notes_dir
        if notes is None:
            return None
        d = Path(notes).parent / "critic_reviews" / "briefs"
        d.mkdir(parents=True, exist_ok=True)
        name = f"briefs/call_{n:03d}.md"
        (d / f"call_{n:03d}.md").write_text(brief or "", encoding="utf-8")
        return {"file": name, "sha256": sha}
    except Exception:  # noqa: BLE001
        return None
_persist_critic_review(n: int, critique_text: str) -> str | None #

Write the critic's full review to debug/critic_reviews/ so the review is auditable whether it accepted or handed back. Best-effort. Returns the file name written (the caller's delegation row records it), or None when nothing was written.

Source code in src/adda/_src/nodes/review_records.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def _persist_critic_review(self, n: int, critique_text: str) -> str | None:
    """Write the critic's full review to debug/critic_reviews/ so the
    review is auditable whether it accepted or handed back. Best-effort.
    Returns the file name written (the caller's delegation row records
    it), or None when nothing was written."""
    try:
        notes = self._current_notes_dir
        if notes is None:
            return None
        d = Path(notes).parent / "critic_reviews"
        d.mkdir(parents=True, exist_ok=True)
        name = f"call_{n:03d}.md"
        (d / name).write_text(critique_text or "", encoding="utf-8")
        return name
    except Exception:  # noqa: BLE001
        return None
_record_usage(usage: dict, *, model: str | None, phase: str | None, delegation_id: str | None, node: str | None = None) -> None #

Accumulate token totals (decision path) AND emit one telemetry row (additive, off the decision path). Telemetry failures are swallowed inside record_call so they can never break a run.

Source code in src/adda/_src/nodes/recording.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def _record_usage(
    self,
    usage: dict,
    *,
    model: str | None,
    phase: str | None,
    delegation_id: str | None,
    node: str | None = None,
) -> None:
    """Accumulate token totals (decision path) AND emit one telemetry row
    (additive, off the decision path).  Telemetry failures are swallowed
    inside ``record_call`` so they can never break a run."""
    self._accumulate_usage(usage, model)
    telemetry = self._run_telemetry()
    if telemetry is not None:
        telemetry.record_call(
            model=model, phase=phase,
            delegation_id=delegation_id, usage=usage, node=node,
        )
_run_nodes() -> list #

This node and every other live node of the run, each once.

Source code in src/adda/_src/nodes/recording.py
88
89
90
91
def _run_nodes(self) -> list:
    """This node and every other live node of the run, each once."""
    peers = getattr(self, "_peers", None) or {}
    return list({id(n): n for n in (self, *peers.values())}.values())
_run_telemetry() -> Any #

The run's telemetry sink. Only the node that owns the run's notes directory has one, but a worker's usage is recorded at ITS delegator, which is any node that delegates (a sub-delegator owns none).

Source code in src/adda/_src/nodes/recording.py
 93
 94
 95
 96
 97
 98
 99
100
def _run_telemetry(self) -> Any:
    """The run's telemetry sink. Only the node that owns the run's notes
    directory has one, but a worker's usage is recorded at ITS delegator,
    which is any node that delegates (a sub-delegator owns none)."""
    for n in (self, *self._run_nodes()):
        if n._telemetry is not None:
            return n._telemetry
    return None
_run_token_totals() -> dict #

Token totals of the whole run: every node records the usage of its own delegations, so the run's total is their sum, not one node's.

Source code in src/adda/_src/nodes/recording.py
102
103
104
105
106
107
108
109
110
def _run_token_totals(self) -> dict:
    """Token totals of the whole run: every node records the usage of its
    own delegations, so the run's total is their sum, not one node's."""
    total: dict = {}
    for n in self._run_nodes():
        with n._registry_lock:
            for k, v in n._token_totals.items():
                total[k] = total.get(k, 0) + v
    return total
_run_spend_usd() -> tuple[float, bool] #

(spend, observed) over the whole run: the SDK's cost plus the list-price estimate for calls it sent none for, and whether any node saw a cost at all.

Source code in src/adda/_src/nodes/recording.py
112
113
114
115
116
117
118
119
120
121
def _run_spend_usd(self) -> tuple[float, bool]:
    """``(spend, observed)`` over the whole run: the SDK's cost plus the
    list-price estimate for calls it sent none for, and whether any node
    saw a cost at all."""
    spent, observed = 0.0, False
    for n in self._run_nodes():
        spent += (n._token_totals.get("total_cost_usd") or 0.0) \
            + n._estimated_cost_usd
        observed = observed or n._cost_observed
    return spent, observed
_accumulate_usage(usage: dict, model: str | None = None) -> None #

Thread-safe accumulation of token counts from adapter.last_usage.

Source code in src/adda/_src/nodes/recording.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def _accumulate_usage(
    self, usage: dict, model: str | None = None,
) -> None:
    """Thread-safe accumulation of token counts from adapter.last_usage."""
    with self._registry_lock:
        self._token_totals["input_tokens"] += usage.get("input_tokens", 0) or 0
        self._token_totals["output_tokens"] += usage.get("output_tokens", 0) or 0
        self._token_totals["cache_read_input_tokens"] += usage.get("cache_read_input_tokens", 0) or 0
        self._token_totals["cache_creation_input_tokens"] += usage.get("cache_creation_input_tokens", 0) or 0
        t = self._token_totals
        if has_normalized_usage(usage):
            t["normalized_calls"] = t.get("normalized_calls", 0) + 1
            for f in NORMALIZED_FIELDS:
                t[f] = t.get(f, 0) + int(usage[f])
        else:
            t["legacy_calls"] = t.get("legacy_calls", 0) + 1
        cost = usage.get("total_cost_usd")
        if cost is not None:
            self._token_totals["total_cost_usd"] += cost
            self._cost_observed = True
        else:
            # No SDK cost (the strategizer's stream is cut before the
            # ResultMessage): count its list-price estimate toward the
            # USD ceiling. The row and total_cost_usd stay None/SDK-only.
            # Priced by the id the API served: an alias has no price.
            priced = usage.get("model_resolved") or model
            est = compute_cost_usd(priced, usage) if priced else None
            if est is not None:
                self._estimated_cost_usd += est
                self._cost_observed = True
_record_tool_error(node_name: str, tool_name: str, error_type: str, message: str, tb: str | None = None, args: dict | None = None, fault: str | None = None) -> None #

Increment error counter and append to diagnostics.jsonl (thread-safe).

fault overrides the classification (the caller knows the agent passed an argument of the wrong type).

args are the call's arguments; each value is capped at 2,000 chars.

Source code in src/adda/_src/nodes/recording.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
def _record_tool_error(
    self,
    node_name: str,
    tool_name: str,
    error_type: str,
    message: str,
    tb: str | None = None,
    args: dict | None = None,
    fault: str | None = None,
) -> None:
    """Increment error counter and append to diagnostics.jsonl (thread-safe).

    ``fault`` overrides the classification (the caller knows the agent
    passed an argument of the wrong type).

    ``args`` are the call's arguments; each value is capped at 2,000 chars.
    """
    import json as _json

    # Classify fault: system (transient API/network) vs agent (bad usage).
    fault = fault or _classify_fault(error_type, message)

    with self._registry_lock:
        self._error_counts[node_name] = self._error_counts.get(node_name, 0) + 1
    notes = self._current_notes_dir
    if notes is None:
        return
    # _current_notes_dir is debug/strategizer_notes/; parent is debug/
    debug_dir = Path(notes).parent
    record: dict = {
        "ts": datetime.now(tz=timezone.utc).isoformat(timespec="seconds"),
        "node": node_name,
        "tool": tool_name,
        "error_type": error_type,
        "fault": fault,
        "message": message,
    }
    if tb:
        record["traceback"] = tb
    if args:
        record["args"] = {k: str(v)[:2000] for k, v in args.items()}
    try:
        with (debug_dir / "diagnostics.jsonl").open("a", encoding="utf-8") as f:
            f.write(_json.dumps(record) + "\n")
    except Exception:  # noqa: BLE001
        pass
_record_llm_retry(attempt: int, max_attempts: int, exc: BaseException, delay: float, resumed: bool | None = None) -> None #

One diagnostics.jsonl row per transient-error retry of an LLM call.

Not an error (no _error_counts bump): the call is retried and may succeed. The row exists so a retry is visible -- how often, which exception, how long the backoff -- instead of silent. resumed (backends that retry a whole session): True when the retry continues the failed session, False when it starts again from the brief.

Source code in src/adda/_src/nodes/recording.py
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
def _record_llm_retry(
    self, attempt: int, max_attempts: int, exc: BaseException,
    delay: float, resumed: bool | None = None,
) -> None:
    """One diagnostics.jsonl row per transient-error retry of an LLM call.

    Not an error (no ``_error_counts`` bump): the call is retried and may
    succeed. The row exists so a retry is visible -- how often, which
    exception, how long the backoff -- instead of silent. ``resumed``
    (backends that retry a whole session): True when the retry continues
    the failed session, False when it starts again from the brief.
    """
    import json as _json

    from ..backends.base import get_delegation_id

    notes = self._current_notes_dir
    if notes is None:
        return
    record = {
        "ts": datetime.now(tz=timezone.utc).isoformat(timespec="seconds"),
        "node": self._name,
        "delegation_id": get_delegation_id(),
        "error_type": "LLM_RETRY",
        "event": "LLM_RETRY",
        "attempt": attempt,
        "max_attempts": max_attempts,
        "exception": type(exc).__name__,
        "message": str(exc)[:300],
        "delay_s": round(delay, 2),
    }
    if resumed is not None:
        record["resumed"] = resumed
    try:
        with (Path(notes).parent / "diagnostics.jsonl").open(
                "a", encoding="utf-8") as f:
            f.write(_json.dumps(record) + "\n")
    except Exception:  # noqa: BLE001
        pass
_record_retrospective(node: str, source_id: str, report_text: str, deliverable_version: int | None = None) -> None #

Capture a node's end-of-life ### Retrospective.

Every node has a 'my job is done' moment — workers at delegation completion, the strategizer (and any future orchestrator) at Done(). Parse the section, persist to retrospectives.jsonl, and surface a diagnostic + strategizer notification the instant it flags contradictory system instructions (the cheapest, highest-value failure mode to catch). Best-effort; never raises.

A Done() call that ACTUALLY ARRIVED with non-empty text is always recorded here, even when _extract_report_section fails to find a ### Retrospective heading in it — never silently dropped. Only genuinely empty text (nothing submitted at all) returns without writing anything. This matters because write_fallback_retrospective (infra/watchdog_cleanup.py) trusts "no entry for this node in retrospectives.jsonl" to mean "the retrospective never arrived" and synthesizes a claim saying so; if a real, substantive reply arrived but merely failed to parse (run 20260926T124841's regex bug, now fixed — see _extract_report_section), silently dropping it here made that downstream claim FALSE — the run's own record said a retrospective "never arrived" when one, in fact, had. Recording a parse-failure entry (with the raw text preserved) keeps that claim honest without needing write_fallback_retrospective itself to know the difference.

Source code in src/adda/_src/nodes/recording.py
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
def _record_retrospective(
    self, node: str, source_id: str, report_text: str,
    deliverable_version: int | None = None,
) -> None:
    """Capture a node's end-of-life ### Retrospective.

    Every node has a 'my job is done' moment — workers at delegation
    completion, the strategizer (and any future orchestrator) at Done().
    Parse the section, persist to retrospectives.jsonl, and surface a
    diagnostic + strategizer notification the instant it flags
    contradictory system instructions (the cheapest, highest-value
    failure mode to catch). Best-effort; never raises.

    A Done() call that ACTUALLY ARRIVED with non-empty text is always
    recorded here, even when ``_extract_report_section`` fails to find
    a ``### Retrospective`` heading in it — never silently dropped. Only
    genuinely empty text (nothing submitted at all) returns without
    writing anything. This matters because ``write_fallback_retrospective``
    (infra/watchdog_cleanup.py) trusts "no entry for this node in
    retrospectives.jsonl" to mean "the retrospective never arrived" and
    synthesizes a claim saying so; if a real, substantive reply arrived
    but merely failed to parse (run 20260926T124841's regex bug, now
    fixed — see _extract_report_section), silently dropping it here made
    that downstream claim FALSE — the run's own record said a retrospective
    "never arrived" when one, in fact, had. Recording a parse-failure
    entry (with the raw text preserved) keeps that claim honest without
    needing write_fallback_retrospective itself to know the difference.
    """
    try:
        report_text = report_text or ""
        retro = _extract_retrospective(report_text)
        parse_failed = bool(report_text.strip()) and not retro
        if not retro and not parse_failed:
            return
        notes = self._current_notes_dir
        if notes is None:
            return
        import json as _json
        import re as _re
        debug_dir = Path(notes).parent
        scan_text = retro or report_text
        flagged = bool(_re.search(r"CONSISTENCY:\s*flagged", scan_text, _re.I))
        now = datetime.now(tz=timezone.utc).isoformat(timespec="seconds")
        if parse_failed:
            body = (
                "PARSE FAILURE: the response contained no ### Retrospective "
                f"section. Raw text follows.\n\n{report_text}"
            )
        else:
            body = retro
        rec = {
            "ts": now, "source_id": source_id, "node": node,
            # ``role`` repeats the node name for one release (old readers).
            "role": node,
            "flagged": flagged, "text": body[:_RETRO_TEXT_CAP],
            "parse_failed": parse_failed,
        }
        if deliverable_version is not None:
            rec["deliverable_version"] = deliverable_version
        with (debug_dir / "retrospectives.jsonl").open(
                "a", encoding="utf-8") as f:
            f.write(_json.dumps(rec) + "\n")
        if flagged:
            drec = {
                "ts": now, "node": node, "tool": "Retrospective",
                "error_type": "CONSISTENCY_FLAG", "fault": "system",
                "message": body[:300],
            }
            with (debug_dir / "diagnostics.jsonl").open(
                    "a", encoding="utf-8") as f:
                f.write(_json.dumps(drec) + "\n")
            with self._notifications_lock:
                self._notifications.append(
                    f"[CONSISTENCY FLAG — {node} ({source_id}) reported "
                    "contradictory system instructions; see "
                    "debug/retrospectives.jsonl]"
                )
    except Exception:  # noqa: BLE001
        pass
_record_intervention(kind: str, target: str, message: str, *, fault: str = 'nudge', **extra) -> None #

Log a scientific-correction event (a nudge/bounce firing) to diagnostics.jsonl — direct evidence the self-healing layer acted, and, when extra carries before/after state, whether it worked.

Neutral classification: fault='nudge' — a nudge is a correction, not an agent error, so this must NOT bump the error/escalation counters (unlike _record_tool_error). fault='observation' is for a row that only records what was seen (adda said nothing to the agent); it also bumps no counter. Best-effort; never raises.

Source code in src/adda/_src/nodes/recording.py
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
def _record_intervention(
    self, kind: str, target: str, message: str, *,
    fault: str = "nudge", **extra
) -> None:
    """Log a scientific-correction event (a nudge/bounce firing) to
    diagnostics.jsonl — direct evidence the self-healing layer acted,
    and, when ``extra`` carries before/after state, whether it worked.

    Neutral classification: fault='nudge' — a nudge is a correction, not
    an agent error, so this must NOT bump the error/escalation counters
    (unlike _record_tool_error). ``fault='observation'`` is for a row that
    only records what was seen (adda said nothing to the agent); it also
    bumps no counter. Best-effort; never raises.
    """
    notes = self._current_notes_dir
    if notes is None:
        return
    import json as _json
    debug_dir = Path(notes).parent
    record: dict = {
        "ts": datetime.now(tz=timezone.utc).isoformat(
            timespec="seconds"),
        "node": target,
        "tool": kind,
        "error_type": kind,
        "fault": fault,
        "message": message,
    }
    record.update(extra)
    try:
        with (debug_dir / "diagnostics.jsonl").open(
                "a", encoding="utf-8") as f:
            f.write(_json.dumps(record) + "\n")
    except Exception:  # noqa: BLE001
        pass
_record_science_drift(payload: dict) -> None #

Append a SCIENCE_DRIFT record to diagnostics.jsonl.

Source code in src/adda/_src/nodes/recording.py
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
def _record_science_drift(self, payload: dict) -> None:
    """Append a SCIENCE_DRIFT record to diagnostics.jsonl."""
    import json as _json
    notes = self._current_notes_dir
    if notes is None:
        return
    record = {
        "ts": datetime.now(tz=timezone.utc).isoformat(
            timespec="seconds"),
        "node": self._name,
        "error_type": "SCIENCE_DRIFT",
        **payload,
    }
    try:
        path = Path(notes).parent / "diagnostics.jsonl"
        with path.open("a", encoding="utf-8") as f:
            f.write(_json.dumps(record) + "\n")
    except Exception:  # noqa: BLE001
        pass
_raise_if_abandoned() -> None #

End the calling thread once the run has stopped waiting for it.

Source code in src/adda/_src/nodes/node.py
187
188
189
def _raise_if_abandoned(self) -> None:
    """End the calling thread once the run has stopped waiting for it."""
    raise_if_stopped(self._name, self._abandon)
_init_recording() -> None #

Establish the state RecordingMixin writes to, on EVERY node.

Recording is a property of a node — any node's tools can return an ERROR, and any node's adapter reports token usage — so its substrate belongs here rather than in one behaviour's setup. It used to be created only by the orchestration setup, which was invisible while leaves were a separate class that did not carry RecordingMixin at all: the moment a leaf reached any recording call it raised AttributeError on _registry_lock. The orchestration setup still assigns these itself, to the same values, so an orchestrating node is unchanged.

Source code in src/adda/_src/nodes/node.py
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
def _init_recording(self) -> None:
    """Establish the state ``RecordingMixin`` writes to, on EVERY node.

    Recording is a property of a node — any node's tools can return an
    ERROR, and any node's adapter reports token usage — so its substrate
    belongs here rather than in one behaviour's setup. It used to be
    created only by the orchestration setup, which was invisible while
    leaves were a separate class that did not carry RecordingMixin at all:
    the moment a leaf reached any recording call it raised AttributeError
    on ``_registry_lock``. The orchestration setup still assigns these
    itself, to the same values, so an orchestrating node is unchanged.
    """
    self._registry_lock = threading.Lock()
    self._abandon = threading.Event()
    self._notifications: list[str] = []
    self._notifications_lock = threading.Lock()
    self._error_counts: dict[str, int] = {}
    self._token_totals: dict = {
        "input_tokens": 0,
        "output_tokens": 0,
        "cache_read_input_tokens": 0,
        "cache_creation_input_tokens": 0,
        # The backend-independent schema (infra/telemetry.py): `legacy_calls`
        # counts calls whose backend reported none of it.
        "fresh_input": 0,
        "cache_read": 0,
        "cache_write": 0,
        "output": 0,
        "normalized_calls": 0,
        "legacy_calls": 0,
        "total_cost_usd": 0.0,
    }
    self._cost_observed: bool = False
    # list-price spend of calls the SDK sent no cost for (the strategizer)
    self._estimated_cost_usd: float = 0.0
    self._current_notes_dir: Path | None = None
    self._telemetry: Any = None
_init_capabilities(*, study_dir: Any = None, workspace_dir: Any = None, delegation_log: DelegationLog | None = None, report_sections: tuple[str, ...] | None = None, agent_tools: frozenset[str] | None = None) -> None #

Wire this node's sandboxed Write and its declared read-only tools.

Runs for EVERY node, not only one reachable as a standalone entry point — see the module docstring: whichever node ends up as someone else's Delegate() target is dispatched through a copy of THIS adapter (adapter.copy() carries the closure_tools set here), so this is where a dispatched specialist's baseline capabilities actually come from, before WorkerSession.install_worker_tools/_sandbox_worker_writes layer the per-delegation overrides (ReportEvals's own record callback, a delegation-scoped Write) on top.

Source code in src/adda/_src/nodes/node.py
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
def _init_capabilities(
    self,
    *,
    study_dir: Any = None,
    workspace_dir: Any = None,
    delegation_log: DelegationLog | None = None,
    report_sections: tuple[str, ...] | None = None,
    agent_tools: frozenset[str] | None = None,
) -> None:
    """Wire this node's sandboxed Write and its declared read-only tools.

    Runs for EVERY node, not only one reachable as a standalone entry
    point — see the module docstring: whichever node ends up as someone
    else's ``Delegate()`` target is dispatched through a copy of THIS adapter
    (``adapter.copy()`` carries the ``closure_tools`` set here), so this is where a
    dispatched specialist's baseline capabilities actually come from,
    before ``WorkerSession.install_worker_tools``/``_sandbox_worker_writes``
    layer the per-delegation overrides (ReportEvals's own record callback,
    a delegation-scoped Write) on top.
    """
    self._study_dir = study_dir
    self._workspace_dir = Path(workspace_dir) if workspace_dir else None
    # The agent's declared tools — the single source of truth for which
    # capability closures this node is granted (read-only ledger/store
    # tools). Kept as a frozenset for membership checks.
    self._agent_tools: frozenset[str] = frozenset(agent_tools or ())
    # A node built without a declared toolset has nothing to narrow by.
    self._tools_declared = agent_tools is not None
    # This agent's declared report sections (e.g. the critic's
    # Findings/Verdict, not the implementer's Conclusions/Files touched).
    # Used to validate a worker's report against ITS OWN contract instead
    # of the implementer-shaped default — otherwise a correct critic or
    # literature report is wrongly flagged malformed (audit BF-10/O40).
    self._report_sections = report_sections
    self._delegation_log = delegation_log
    self._evals_reported: dict = {}
    self._setup_sandboxed_write()
    self.adapter.closure_tools.update(self._build_eval_closures())
    if delegation_log is not None:
        from .tools.routing import build_recall_history
        self.adapter.closure_tools["RecallHistory"] = build_recall_history(self)
    # Declaration-gated read-only ledger/store tools — the SAME builder
    # every node uses, so a specialist (e.g. the critic) gets an
    # identical, working QueryStore/HypothesisList
    # surface whenever it declares them. Resolves the run via the shared
    # Node._resolve_run_dir (delegation-log path).
    from ..runtime import features as _features
    from .tools.routing import build_declared_shared_closures
    self.adapter.closure_tools.update(
        build_declared_shared_closures(
            self, self._agent_tools - _features.disabled_tool_names()))
    # Closures from Agent.build_closure_tools() (ConsultLiterature,
    # CorpusAdd, ...) are installed on the adapter BEFORE this node
    # exists (agent_runtime.py), so they never pass through
    # orchestration.py's _wrap_closure the way routing tools do. A
    # closure tagged _adda_diagnostic_source (a fact about the run
    # environment a tool discovers, not about one call's return value —
    # see LiteratureCorpus.pop_diagnostic_event) needs that wrapper
    # regardless, so re-wrap it here, once, by the tag rather than by
    # name — any future tool needing the same reporting just carries the
    # same tag.
    for _cname, _cfn in list(self.adapter.closure_tools.items()):
        if getattr(_cfn, "_adda_diagnostic_source", None) is not None:
            self.adapter.closure_tools[_cname] = self._wrap_closure(
                _cfn, self._name)
    # A config.yaml `nodes.<name>.tools` list is the whole tool set: the
    # always-on closures it does not name are withheld.
    from ..runtime.node_tools import withheld_closures
    _agent = self._spec.nodes.get(self._name) if self._spec is not None else None
    for _cname in withheld_closures(_agent):
        self.adapter.closure_tools.pop(_cname, None)
unit_folder(unit_id: str) -> Path | None #

debug/delegations/<unit_id>/: the folder of one unit of work (a delegation D###, a tenure X###, a wake W###), created here.

Source code in src/adda/_src/nodes/node.py
301
302
303
304
305
306
307
308
def unit_folder(self, unit_id: str) -> Path | None:
    """``debug/delegations/<unit_id>/``: the folder of one unit of work
    (a delegation D###, a tenure X###, a wake W###), created here."""
    if self._workspace_dir is None:
        return None
    folder = self._workspace_dir / unit_id
    folder.mkdir(parents=True, exist_ok=True)
    return folder
scope_write_to(adapter: Any, unit_id: str) -> None #

Confine adapter's Write to this unit's folder, as a delegated worker's is: one rule for every unit of work. No-op when the node has no sandboxed Write.

Source code in src/adda/_src/nodes/node.py
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
def scope_write_to(self, adapter: Any, unit_id: str) -> None:
    """Confine ``adapter``'s Write to this unit's folder, as a delegated
    worker's is: one rule for every unit of work. No-op when the node
    has no sandboxed Write."""
    if "Write" not in adapter.closure_tools:
        return
    folder = self.unit_folder(unit_id)
    if folder is None:
        return
    from .tools.routing import build_sandboxed_write
    study = Path(self._study_dir) if self._study_dir is not None else None
    adapter.closure_tools["Write"] = build_sandboxed_write(
        folder.resolve(), strip_prefix=unit_id,
        scope_label=f"{unit_id}/ ({folder.resolve()})",
        study_workspace=study / "workspace" if study else None,
        deliverable_dir=study,
        deliverable_names=lambda: getattr(
            self, "_required_deliverables", None) or [])
_setup_sandboxed_write() -> None #

Replace native Write with a workspace-sandboxed closure.

Removes 'Write' from native_tools so the SDK doesn't expose it, then installs the same builder a dispatched worker gets (:func:build_sandboxed_write), scoped to this node's whole workspace instead of one delegation's subfolder — a node reached via real graph routing (the module docstring) has no delegation_id of its own to scope tighter than that.

Bash is kept native but cwd is already set to study_dir by the adapter; the prompt further constrains it to the workspace.

Source code in src/adda/_src/nodes/node.py
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
def _setup_sandboxed_write(self) -> None:
    """Replace native Write with a workspace-sandboxed closure.

    Removes 'Write' from native_tools so the SDK doesn't expose it,
    then installs the same builder a dispatched worker gets
    (:func:`build_sandboxed_write`), scoped to this node's whole
    workspace instead of one delegation's subfolder — a node reached via
    real graph routing (the module docstring) has no delegation_id of
    its own to scope tighter than that.

    Bash is kept native but cwd is already set to study_dir by the adapter;
    the prompt further constrains it to the workspace.
    """
    if self._workspace_dir is None:
        return  # no sandboxing if study_dir unknown (e.g. tests)
    from ..runtime.node_tools import withheld_closures
    if "Write" in withheld_closures(
            self._spec.nodes.get(self._name) if self._spec is not None else None):
        return  # a Default node keeps the backend's native Write

    # Remove native Write so the SDK doesn't expose an unrestricted version
    if hasattr(self.adapter, "native_tools") and "Write" in self.adapter.native_tools:
        self.adapter.native_tools = [
            t for t in self.adapter.native_tools if t != "Write"
        ]

    from .tools.routing import build_sandboxed_write

    # Bound to a plain name (not inlined into the assignment) so
    # dev/tools/promptmap.py's injected_tool_docs() scanner — which
    # resolves a closure_tools["Write"] = <name> rebind to the function
    # <name> refers to — can still find this tool's docstring after the
    # move into the shared builder.
    _study_ws = (
        Path(self._study_dir) / "workspace"
        if self._study_dir is not None else None
    )
    Write = build_sandboxed_write(
        self._workspace_dir, study_workspace=_study_ws,
        deliverable_dir=(
            Path(self._study_dir) if self._study_dir is not None else None),
        deliverable_names=lambda: getattr(
            self, "_required_deliverables", None) or [])
    self.adapter.closure_tools["Write"] = Write
_build_eval_closures() -> dict #
Source code in src/adda/_src/nodes/node.py
374
375
376
377
378
379
380
381
382
def _build_eval_closures(self) -> dict:
    from .tools.routing import build_report_evals

    evals = self._evals_reported
    return {
        "ReportEvals": build_report_evals(
            record=lambda n: evals.__setitem__("count", n)
        )
    }
_commit_workspace(message: str) -> str | None #

Commit the run workspace and return the sha (spec 11).

On Node rather than on WorkerSession because a delegation is recorded from several places, not one: a worker finishing (ok or error), a tenure closing (routing/handoff.py), and the close-time reconciliation of a delegation still running when the run ended. Every one of them appends a row to the delegation log, so every one of them owes that row a sha — otherwise workspace_sha: null means both "this delegation changed nothing" and "nobody looked", which is the ambiguity --allow-empty exists to prevent.

The interrupted case is the one with teeth: a delegation killed mid-flight never reaches _finish_ok/_finish_error, so its partial file writes stay uncommitted and are swept into whichever delegation commits NEXT — attributing one delegation's work to another. Silence would be better than that; a commit is better still.

Never raises — see infra/workspace_vcs.

Source code in src/adda/_src/nodes/node.py
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
def _commit_workspace(self, message: str) -> str | None:
    """Commit the run workspace and return the sha (spec 11).

    On Node rather than on WorkerSession because a delegation is recorded
    from several places, not one: a worker finishing (ok or error), a
    tenure closing (routing/handoff.py), and the close-time
    reconciliation of a delegation still running when the run ended. Every
    one of them appends a row to the delegation log, so every one of them
    owes that row a sha — otherwise ``workspace_sha: null`` means both
    "this delegation changed nothing" and "nobody looked", which is the
    ambiguity --allow-empty exists to prevent.

    The interrupted case is the one with teeth: a delegation killed
    mid-flight never reaches _finish_ok/_finish_error, so its partial file
    writes stay uncommitted and are swept into whichever delegation
    commits NEXT — attributing one delegation's work to another. Silence
    would be better than that; a commit is better still.

    Never raises — see infra/workspace_vcs.
    """
    run_dir = self._resolve_run_dir()
    if run_dir is None:
        return None
    from ..infra.workspace_vcs import commit_workspace
    return commit_workspace(run_dir / "debug" / "delegations", message)
_resolve_run_dir() -> Path | None #

Best-effort run_dir, valid on any node.

Every node's _current_notes_dir is set at construction (from notes_dir, passed to every node — see graph_builder.py) and re-pointed each turn by _absorb_state if the run's real notes dir differs. It can still be None (e.g. a bare Node() built without one, as in unit tests) — every node DOES hold the shared delegation log at run_dir/debug/delegation_log.jsonl, so derive run_dir from that when the notes dir is unavailable.

Source code in src/adda/_src/nodes/node.py
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
def _resolve_run_dir(self) -> Path | None:
    """Best-effort run_dir, valid on any node.

    Every node's ``_current_notes_dir`` is set at construction (from
    ``notes_dir``, passed to every node — see ``graph_builder.py``) and
    re-pointed each turn by ``_absorb_state`` if the run's real notes dir
    differs. It can still be ``None`` (e.g. a bare ``Node()`` built
    without one, as in unit tests) — every node DOES hold the shared
    delegation log at run_dir/debug/delegation_log.jsonl, so derive
    run_dir from that when the notes dir is unavailable.
    """
    notes = getattr(self, "_current_notes_dir", None)
    if notes is not None:
        return Path(notes).parent.parent
    dlog = getattr(self, "_delegation_log", None)
    p = getattr(dlog, "_path", None)
    return Path(p).parent.parent if p is not None else None
_read_ledger() -> HypothesisLedger | None #

The hypothesis ledger for READ access, resolved on any node.

Returns the node's own bound ledger when it has one (the entry node); otherwise resolves a read-only view from the run's strategizer_notes when hypotheses.json exists. HypothesisLedger.init performs no I/O, and only READ callers use this, so there is no write race with the entry node that owns the file.

Source code in src/adda/_src/nodes/node.py
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
def _read_ledger(self) -> HypothesisLedger | None:
    """The hypothesis ledger for READ access, resolved on any node.

    Returns the node's own bound ledger when it has one (the entry node);
    otherwise resolves a read-only view from the run's strategizer_notes
    when hypotheses.json exists. HypothesisLedger.__init__ performs no I/O,
    and only READ callers use this, so there is no write race with the
    entry node that owns the file.
    """
    own = getattr(self, "_ledger", None)
    if own is not None:
        return own
    rd = self._resolve_run_dir()
    if rd is None:
        return None
    notes = rd / "debug" / "strategizer_notes"
    if (notes / "hypotheses.json").exists():
        from ..epistemics.hypothesis_ledger import HypothesisLedger
        return HypothesisLedger.for_dir(notes)
    return None

adda.build_graph(spec: Graph, make_adapter: Callable[[str, Agent], Any], checkpointer: Any = None, study_dir: Any = None, interactive: bool = False, max_ask: int = 1, notes_dir: Any = None, workspace_dir: Any = None, delegation_log: DelegationLog | None = None, node_registry: dict[str, Any] | None = None, topology: Any = None, *, check: bool = True) -> Any #

Build and compile a LangGraph StateGraph from a Graph spec.

Parameters:

Name Type Description Default
spec Graph

Agent graph specification (nodes, edges, entry).

required
make_adapter callable

(name: str, agent: Agent) -> adapter — factory that produces a ClaudeAdapter or OllamaAdapter for the given node.

required
checkpointer any

LangGraph checkpointer. Defaults to an in-memory :class:MemorySaver.

None
delegation_log DelegationLog

Graph-wide delegation log for episodic memory (RecallHistory tool).

None
node_registry dict

If given, populated in place with {name: Node instance} as each node is constructed — an explicit, caller-owned way to reach the live Node objects afterward (e.g. spec 12's close-time open-review sweep) instead of reaching through the COMPILED graph's own internals (compiled.nodes[name].bound.func), which is a LangGraph implementation detail that could silently break on an upgrade.

None
topology Topology

The run's runtime topology (runtime_topology knob). It is bound to the live nodes and to the factory that builds a new one, and the applied changes of an earlier segment are replayed before the graph is compiled.

None
check bool

Run :func:check_closable first (default). Only a display build (runtime/bound_tools.py), which never runs the graph, turns it off, so it can still show what an unclosable graph's nodes hold.

True

Returns:

Type Description
CompiledGraph

A compiled LangGraph graph ready to invoke.

Source code in src/adda/_src/runtime/graph_builder.py
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def build_graph(
    spec: Graph,
    make_adapter: Callable[[str, Agent], Any],
    checkpointer: Any = None,
    study_dir: Any = None,
    interactive: bool = False,
    max_ask: int = 1,
    notes_dir: Any = None,
    workspace_dir: Any = None,
    delegation_log: DelegationLog | None = None,
    node_registry: dict[str, Any] | None = None,
    topology: Any = None,
    *,
    check: bool = True,
) -> Any:
    """Build and compile a LangGraph StateGraph from a Graph spec.

    Parameters
    ----------
    spec : Graph
        Agent graph specification (nodes, edges, entry).
    make_adapter : callable
        ``(name: str, agent: Agent) -> adapter`` — factory that produces a
        ``ClaudeAdapter`` or ``OllamaAdapter`` for the given node.
    checkpointer : any, optional
        LangGraph checkpointer.  Defaults to an in-memory :class:`MemorySaver`.
    delegation_log : DelegationLog, optional
        Graph-wide delegation log for episodic memory (RecallHistory tool).
    node_registry : dict, optional
        If given, populated in place with ``{name: Node instance}`` as each
        node is constructed — an explicit, caller-owned way to reach the
        live Node objects afterward (e.g. spec 12's close-time open-review
        sweep) instead of reaching through the COMPILED graph's own
        internals (``compiled.nodes[name].bound.func``), which is a
        LangGraph implementation detail that could silently break on an
        upgrade.

    topology : Topology, optional
        The run's runtime topology (``runtime_topology`` knob). It is bound to
        the live nodes and to the factory that builds a new one, and the
        applied changes of an earlier segment are replayed before the graph
        is compiled.
    check : bool
        Run :func:`check_closable` first (default). Only a display build
        (runtime/bound_tools.py), which never runs the graph, turns it off,
        so it can still show what an unclosable graph's nodes hold.

    Returns
    -------
    CompiledGraph
        A compiled LangGraph graph ready to invoke.
    """
    if check:
        check_closable(spec)
    from ..nodes.subscriptions import validate as _check_subscribes
    from .node_tools import REMOVED_NODE_KEYS
    for _name, _agent in spec.nodes.items():
        _check_subscribes(_name, getattr(_agent, "subscribes", None))
        for _key, _why in REMOVED_NODE_KEYS.items():
            if hasattr(_agent, _key):
                raise ValueError(f"node {_name}: {_key} {_why}")
    from ..nodes.subscriptions import check_delivery
    check_delivery(spec)
    settings.set_graph_nodes(spec.nodes)
    builder = StateGraph(AgenticState)

    # ONE adapter per named node — shared across all orchestrating nodes.
    node_adapters = {n: make_adapter(n, spec.nodes[n]) for n in spec.nodes}

    live_nodes: dict[str, Any] = (
        node_registry if node_registry is not None else {})

    def construct(name: str) -> Node:
        agent = spec.nodes[name]
        if name not in node_adapters:
            node_adapters[name] = make_adapter(name, agent)
        adapter = node_adapters[name]  # shared instance, NOT make_adapter() again
        outgoing = spec.outgoing(name)

        # ONE node class; every node runs the same turn loop
        # (Node._orchestrate). What a node may DO follows from what its
        # Agent declares in `tools` — never from its topology. notes_dir is
        # passed to every node: a delegating node is simply a node that needs
        # help from another node (CLAUDE.md "all nodes are equal"), and
        # telemetry / the science monitor / hypothesis-ledger READ access
        # matter for every role, not only the entry node. WRITE access
        # (HypothesisPropose/Update, Milestone*) stays gated separately, by
        # each Agent's own declared `tools` (see
        # nodes/tools/routing/__init__.py) — passing notes_dir here grants no
        # capability a node has not already declared. A node with no
        # outgoing edges still receives this argument (Node.__init__ has one
        # init path for every node), but never ACQUIRES ledger ownership from
        # it — Node._init_orchestration gates `_owns_epistemics` on outgoing
        # edges, being able to own the job, or declaring a record's tool,
        # not merely on notes_dir being set.
        node = Node(
            adapter,
            name=name,
            outgoing=outgoing,
            spec=spec,
            study_dir=study_dir,
            interactive=interactive,
            max_ask=max_ask,
            worker_adapters={n: node_adapters[n] for n in outgoing},
            notes_dir=notes_dir,
            workspace_dir=workspace_dir,
            delegation_log=delegation_log,
            report_sections=getattr(agent, "report_sections", None),
            agent_tools=getattr(agent, "tools", None),
        )
        return node


    for name in list(spec.nodes):
        live_nodes[name] = construct(name)
        builder.add_node(name, live_nodes[name])

    # A delegation's registry entry lives in its DELEGATOR's Node, while the
    # worker calls the tool closures bound to its OWN Node -- so every node
    # must be able to find the others (Node._delegation_entry).
    from ..nodes.slots import AwakeSlots
    from . import features
    awake_slots = AwakeSlots(features.max_awake_nodes())
    for node in live_nodes.values():
        node._peers = live_nodes
        node._awake_slots = awake_slots

    def build_node(name: str) -> Node:
        node = construct(name)
        node._peers = live_nodes
        node._awake_slots = awake_slots
        node._topology = topology
        return node

    if topology is not None:
        topology.bind(live_nodes, build_node)
        topology.replay()

    builder.set_entry_point(spec.entry)

    return builder.compile(checkpointer=checkpointer or MemorySaver())

Run state#

The types that move through the graph while it runs. You rarely construct these yourself; they are what you see in a transcript or a checkpoint.

adda.AgenticState #

LangGraph state for one agentic run.

Inherits messages: Annotated[list[AnyMessage], add_messages] from :class:~langgraph.graph.MessagesState.

Source code in src/adda/_src/runtime/graph_state.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class AgenticState(MessagesState):
    """LangGraph state for one agentic run.

    Inherits ``messages: Annotated[list[AnyMessage], add_messages]``
    from :class:`~langgraph.graph.MessagesState`.
    """

    study_dir: str
    done: bool
    last_report: str | None
    total_delegations: int
    budget_seconds: float | None
    budget_usd: float | None     # hard USD cost ceiling; None = no USD ceiling
    run_dir: str | None          # absolute path to runs/<timestamp>/
    eval_budget: int | None      # max function evaluations (from config)
    evals_used: int              # running count across delegations
    # time.time() at run start, for budget enforcement
    start_time: float | None
    # paths relative to study_dir; checked before Done accepted
    required_deliverables: list | None
    # Token usage accumulated across all agents; set by the entry node on Done
    token_totals: dict | None
    # Per-node tool-call error counts (ERROR: returns + exceptions)
    error_counts: dict | None
    # Canonical run-level ExperimentData store project_dir.
    # Set by AgenticRun.execute() so nodes and workers can locate the
    # shared store without re-deriving it from run_dir.
    experiment_data_dir: str | None
    # Structured terminal state, recorded where the decision is MADE and
    # carried out as data — never re-derived by grepping the final report's
    # banner, which is how a backstop halt used to read as a validated
    # success. Vocabulary + fail-safe resolution live in runtime.terminal.
    outcome: str | None       # GATED | UNGATED | FAILED
    termination: str | None   # done | time_budget | crashed | …
    reviewed: bool | None     # a review actually ran
    # Handoffs (handoff spec P2). ``messages`` is the entry node's
    # conversation; every other graph node keeps its own under its name.
    threads: Annotated[dict, merge_threads]
    owner: str | None         # the node that owns the job; None = the entry
study_dir: str instance-attribute #
done: bool instance-attribute #
last_report: str | None instance-attribute #
total_delegations: int instance-attribute #
budget_seconds: float | None instance-attribute #
budget_usd: float | None instance-attribute #
run_dir: str | None instance-attribute #
eval_budget: int | None instance-attribute #
evals_used: int instance-attribute #
start_time: float | None instance-attribute #
required_deliverables: list | None instance-attribute #
token_totals: dict | None instance-attribute #
error_counts: dict | None instance-attribute #
experiment_data_dir: str | None instance-attribute #
outcome: str | None instance-attribute #
termination: str | None instance-attribute #
reviewed: bool | None instance-attribute #
threads: Annotated[dict, merge_threads] instance-attribute #
owner: str | None instance-attribute #

adda.StudyConfig dataclass #

Lightweight config loaded from the study directory.

Source code in src/adda/_src/runtime/graph_state.py
 96
 97
 98
 99
100
101
102
@dataclass
class StudyConfig:
    """Lightweight config loaded from the study directory."""

    study_dir: str
    model: str = "claude-opus-4-5"
    budget_seconds: float | None = None
study_dir: str instance-attribute #
model: str = 'claude-opus-4-5' class-attribute instance-attribute #
budget_seconds: float | None = None class-attribute instance-attribute #

adda.Task dataclass #

A unit of work delegated from one agent to another.

Source code in src/adda/_src/runtime/graph_state.py
66
67
68
69
70
71
72
@dataclass
class Task:
    """A unit of work delegated from one agent to another."""

    intent: str
    expected_report: str
    target: str
intent: str instance-attribute #
expected_report: str instance-attribute #
target: str instance-attribute #

adda.Report dataclass #

The result returned by an implementer agent.

Source code in src/adda/_src/runtime/graph_state.py
75
76
77
78
79
80
@dataclass
class Report:
    """The result returned by an implementer agent."""

    content: str
    target: str | None = None
content: str instance-attribute #
target: str | None = None class-attribute instance-attribute #

adda.Delegation dataclass #

A delegation request parsed from an agent's response.

Source code in src/adda/_src/runtime/graph_state.py
83
84
85
86
87
88
89
90
91
92
93
@dataclass
class Delegation:
    """A delegation request parsed from an agent's response."""

    target: str
    task: str
    expected_report: str = ""
    # Optional design namespace this delegation operates in (Axis 3a). None →
    # the canonical single-study oracle + ledger (today's behavior). A non-None
    # value scopes the worker to that namespace's oracle via F3DASM_NAMESPACE.
    namespace: str | None = None
target: str instance-attribute #
task: str instance-attribute #
expected_report: str = '' class-attribute instance-attribute #
namespace: str | None = None class-attribute instance-attribute #