Awesome Reviewers

Reviewer · Algorithms

Generalize structural traversal logic

When writing algorithms that recurse or branch over structurally similar data (nested maps, trees, graphs), make sure the logic covers every equivalent case uniformly and bases decisions on explicit, stable properties rather than incidental ones.

Instruction

When writing algorithms that recurse or branch over structurally similar data (nested maps, trees, graphs), make sure the logic covers every equivalent case uniformly and bases decisions on explicit, stable properties rather than incidental ones.

Concretely:

  • If a recursive traversal special-cases one container shape (e.g. a properties map keyed by arbitrary names), identify every other keyword/field with the same shape ($defs, definitions, patternProperties, etc.) and apply the same recursive handling, otherwise the bug you just fixed for one key will silently reappear for the others.
  • When a condition drives a structural operation (e.g. deciding whether to compile a graph node, or whether a cycle should be treated as fatal), key the branch on the semantic/type marker that was designed for that purpose (node.type == NodeType.SUBGRAPH) instead of an incidental attribute (isinstance(node.agent, GraphWorkflow)), so the behavior stays correct as new node/agent combinations are introduced.
  • When correctness of a search/match (e.g. alternation in a regex, candidates in a lookup table) happens to work only because of a specific ordering, don’t leave that as an implicit invariant — sort/normalize the inputs (e.g. by descending length) so correctness doesn’t silently depend on how the table is edited later.
  • For graph algorithms, make sure the result semantics match what the execution/consumption model actually does: if cycle detection flags something the executor cannot correctly run (e.g. it flattens cycles into a single layer and ignores edge order), that must surface as an error, not a warning.

Example — generalizing a name-keyed-map traversal instead of special-casing one key:

_NAME_KEYED_CONTAINERS = ("properties", "$defs", "definitions", "patternProperties")

def _remove_a_key(d: dict, remove_key: str) -> None:
    for key in list(d.keys()):
        if key == remove_key and "type" in d.keys():
            del d[key]
        elif key in _NAME_KEYED_CONTAINERS and isinstance(d[key], dict):
            for prop in d[key].values():
                if isinstance(prop, dict):
                    _remove_a_key(prop, remove_key)
        elif isinstance(d[key], dict):
            _remove_a_key(d[key], remove_key)

Treat each of these as a correctness check, not a style nit: an algorithm that only works for the one case exercised by the current tests is incomplete.</description> </invoke>