CtrlK
BlogDocsLog inGet started
Tessl Logo

jbaruch/coding-policy

General-purpose coding policy for Baruch's AI agents

76

Quality

95%

Does it follow best practices?

Run evals on this skill

Adds up to 20 points to the overall score

View guide

SecuritybySnyk

Low

Low-risk findings worth noting

Overview
Quality
Evals
Security
Files

test_planner.pyskills/herdr-teamlead/tests/

"""Tests for teamlead.planner. Pure function in, pure dict out."""

import os as _os
import sys as _sys

# Run as a script (`python3 tests/test_x.py`), Python puts tests/ on sys.path
# rather than the repo root, so neither `teamlead` nor `tests.fakes` would
# resolve. Under `-m unittest` from the root this is already true and the
# insert is a no-op. The consuming repo's runner executes files as scripts.
_ROOT = _os.path.dirname(_os.path.dirname(_os.path.abspath(__file__)))
if _ROOT not in _sys.path:
    _sys.path.insert(0, _ROOT)

import unittest

from teamlead.errors import PlanError
from teamlead.planner import plan

ROLES = ["developer", "tester", "reviewer"]


def snapshot(**headrooms):
    """Build a minimal snapshot carrying only what the planner reads."""
    return {
        "schema_version": 1,
        "measured_at": "2026-02-03T10:00:00+00:00",
        "agents": {
            name: {"kind": name, "state": "idle", "headroom_pct": headroom}
            for name, headroom in headrooms.items()
        },
    }


class OrderingTest(unittest.TestCase):
    def test_heaviest_role_goes_to_the_most_headroom(self):
        result = plan(ROLES, snapshot(claude=92.0, codex=87.0, grok=100.0))
        self.assertEqual(
            result["assignments"],
            {"developer": "grok", "tester": "claude", "reviewer": "codex"},
        )

    def test_assignment_order_follows_the_roles_given(self):
        result = plan(ROLES, snapshot(claude=92.0, codex=87.0, grok=100.0))
        self.assertEqual(list(result["assignments"]), ROLES)

    def test_reordering_the_roles_no_longer_reorders_the_agents(self):
        # --roles used to decide which seat was heaviest. The cost weights do
        # now, so the lightest-first caller gets the same plan as before.
        result = plan(
            ["reviewer", "tester", "developer"], snapshot(claude=92.0, codex=87.0, grok=100.0)
        )
        self.assertEqual(result["assignments"]["developer"], "grok")
        self.assertEqual(result["assignments"]["tester"], "claude")
        self.assertEqual(result["assignments"]["reviewer"], "codex")

    def test_reordering_the_roles_still_keys_the_document_that_way(self):
        result = plan(
            ["reviewer", "tester", "developer"], snapshot(claude=92.0, codex=87.0, grok=100.0)
        )
        self.assertEqual(
            list(result["assignments"]), ["reviewer", "tester", "developer"]
        )

    def test_is_deterministic_across_repeated_calls(self):
        data = snapshot(claude=92.0, codex=87.0, grok=100.0)
        self.assertEqual(plan(ROLES, data), plan(ROLES, data))

    def test_integer_headrooms_work_the_same_as_floats(self):
        result = plan(ROLES, snapshot(claude=92, codex=87, grok=100))
        self.assertEqual(result["assignments"]["developer"], "grok")

    def test_fewer_roles_than_agents_leaves_the_rest_unassigned(self):
        result = plan(["developer"], snapshot(claude=92.0, codex=87.0, grok=100.0))
        self.assertEqual(result["assignments"], {"developer": "grok"})


class TieBreakTest(unittest.TestCase):
    def test_headroom_tie_falls_back_to_name(self):
        result = plan(["developer", "tester"], snapshot(zeta=50.0, alpha=50.0))
        self.assertEqual(result["assignments"]["developer"], "alpha")
        self.assertEqual(result["assignments"]["tester"], "zeta")

    def test_previous_role_count_beats_name(self):
        counts = {"developer": {"alpha": 3, "zeta": 1}}
        result = plan(["developer"], snapshot(zeta=50.0, alpha=50.0), counts)
        self.assertEqual(result["assignments"]["developer"], "zeta")

    def test_role_counts_are_per_role_not_global(self):
        # alpha has been developer a lot but tester never; the tester pick must
        # not be penalised by the developer history.
        counts = {"developer": {"alpha": 9}}
        result = plan(["tester"], snapshot(zeta=50.0, alpha=50.0), counts)
        self.assertEqual(result["assignments"]["tester"], "alpha")

    def test_headroom_still_outranks_role_history(self):
        counts = {"developer": {"grok": 10}}
        result = plan(["developer"], snapshot(claude=92.0, grok=100.0), counts)
        self.assertEqual(result["assignments"]["developer"], "grok")

    def test_equal_role_counts_fall_through_to_name(self):
        counts = {"developer": {"alpha": 2, "zeta": 2}}
        result = plan(["developer"], snapshot(zeta=50.0, alpha=50.0), counts)
        self.assertEqual(result["assignments"]["developer"], "alpha")


class NullHeadroomTest(unittest.TestCase):
    def test_null_headroom_agents_sort_last(self):
        result = plan(ROLES, snapshot(claude=None, codex=87.0, grok=100.0))
        self.assertEqual(
            result["assignments"],
            {"developer": "grok", "tester": "codex", "reviewer": "claude"},
        )

    def test_null_headroom_agents_are_ordered_by_name_alone(self):
        result = plan(["developer", "tester"], snapshot(zeta=None, alpha=None))
        self.assertEqual(result["assignments"], {"developer": "alpha", "tester": "zeta"})

    def test_role_history_does_not_reorder_null_headroom_agents(self):
        counts = {"developer": {"alpha": 5, "zeta": 0}}
        result = plan(["developer"], snapshot(zeta=None, alpha=None), counts)
        self.assertEqual(result["assignments"]["developer"], "alpha")

    def test_zero_headroom_still_outranks_null(self):
        result = plan(["developer", "tester"], snapshot(alpha=None, zeta=0.0))
        self.assertEqual(result["assignments"]["developer"], "zeta")

    def test_missing_headroom_key_is_treated_as_null(self):
        data = {"agents": {"alpha": {"state": "working"}, "zeta": {"headroom_pct": 10.0}}}
        result = plan(["developer"], data)
        self.assertEqual(result["assignments"]["developer"], "zeta")


class OutputShapeTest(unittest.TestCase):
    def test_carries_a_schema_version(self):
        self.assertEqual(plan(["developer"], snapshot(grok=100.0))["schema_version"], 1)

    def test_rationale_has_one_line_per_role_naming_the_field(self):
        result = plan(ROLES, snapshot(claude=92.0, codex=87.0, grok=100.0))
        self.assertEqual(len(result["rationale"]), 3)
        self.assertIn("developer -> grok", result["rationale"][0])
        self.assertIn("100% headroom", result["rationale"][0])
        self.assertIn("grok=100%", result["rationale"][0])

    def test_rationale_names_the_previous_role_count(self):
        counts = {"developer": {"grok": 4}}
        result = plan(["developer"], snapshot(grok=100.0), counts)
        self.assertIn("held this role 4x before", result["rationale"][0])

    def test_rationale_explains_a_null_headroom_pick(self):
        result = plan(["developer"], snapshot(grok=None))
        self.assertIn("no headroom reading", result["rationale"][0])

    def test_snapshot_ref_defaults_to_the_measurement_timestamp(self):
        result = plan(["developer"], snapshot(grok=100.0))
        self.assertEqual(
            result["snapshot_ref"],
            {"source": None, "measured_at": "2026-02-03T10:00:00+00:00"},
        )

    def test_snapshot_ref_is_echoed_when_supplied(self):
        ref = {"source": "/tmp/snap.json", "measured_at": "2026-02-03T10:00:00+00:00"}
        result = plan(["developer"], snapshot(grok=100.0), snapshot_ref=ref)
        self.assertEqual(result["snapshot_ref"], ref)


class CostWeightTest(unittest.TestCase):
    """The seat's weight, not the caller's --roles order, decides who fills it."""

    def test_the_heaviest_seat_is_filled_first_whatever_roles_says(self):
        # reviewer arrives first and still gets the smallest agent, because
        # developer outweighs it.
        result = plan(["reviewer", "developer"], snapshot(alpha=90.0, zeta=60.0))
        self.assertEqual(result["assignments"], {"reviewer": "zeta", "developer": "alpha"})

    def test_an_override_can_make_the_lightest_seat_the_heaviest(self):
        result = plan(
            ["developer", "reviewer"],
            snapshot(alpha=60.0, zeta=55.0),
            role_costs={"reviewer": 30.0},
        )
        self.assertEqual(result["assignments"], {"developer": "zeta", "reviewer": "alpha"})

    def test_an_override_replaces_only_the_role_it_names(self):
        result = plan(
            ["developer", "reviewer"],
            snapshot(alpha=60.0, zeta=55.0),
            role_costs={"tester": 99.0},
        )
        self.assertEqual(result["assignments"], {"developer": "alpha", "reviewer": "zeta"})

    def test_roles_nobody_weighed_keep_the_callers_order(self):
        result = plan(["scribe", "courier"], snapshot(alpha=90.0, zeta=60.0))
        self.assertEqual(result["assignments"], {"scribe": "alpha", "courier": "zeta"})

    def test_the_pick_maximises_the_rounds_minimum_projected_headroom(self):
        # developer costs 12 and tester 10, so giving the 70 to the heavier
        # seat would floor the round at 58; the other way floors it at 60.
        result = plan(["developer", "tester"], snapshot(alpha=70.0, zeta=72.0))
        self.assertEqual(result["assignments"], {"developer": "zeta", "tester": "alpha"})

    def test_the_rationale_names_the_weight_it_used(self):
        result = plan(["developer"], snapshot(grok=100.0))
        self.assertIn("weight 12", result["rationale"][0])
        self.assertIn("88% projected", result["rationale"][0])

    def test_the_rationale_names_an_overridden_weight(self):
        result = plan(["developer"], snapshot(grok=100.0), role_costs={"developer": 40.0})
        self.assertIn("weight 40", result["rationale"][0])


class ExclusionTest(unittest.TestCase):
    """Nobody reviews or verifies the branch they wrote."""

    def test_an_excluded_agent_does_not_get_that_role(self):
        result = plan(
            ROLES,
            snapshot(claude=92.0, codex=87.0, grok=100.0),
            exclude={"reviewer": ["grok"], "tester": ["grok"]},
        )
        self.assertEqual(result["assignments"]["developer"], "grok")
        self.assertNotIn(result["assignments"]["reviewer"], ["grok"])
        self.assertNotIn(result["assignments"]["tester"], ["grok"])

    def test_the_author_still_gets_the_one_seat_left_to_it(self):
        # grok has the LEAST headroom, so the plain headroom order would hand
        # developer to claude and then have nowhere to put grok.
        result = plan(
            ROLES,
            snapshot(claude=92.0, codex=87.0, grok=40.0),
            exclude={"reviewer": ["grok"], "tester": ["grok"]},
        )
        self.assertEqual(
            result["assignments"],
            {"developer": "grok", "tester": "claude", "reviewer": "codex"},
        )

    def test_several_agents_can_be_barred_from_one_role(self):
        result = plan(
            ["developer", "reviewer"],
            snapshot(alpha=90.0, zeta=60.0, mu=80.0),
            exclude={"reviewer": ["alpha", "mu"]},
        )
        self.assertEqual(result["assignments"]["reviewer"], "zeta")

    def test_an_exclusion_that_leaves_no_candidate_is_an_error(self):
        with self.assertRaises(PlanError) as caught:
            plan(
                ["developer", "reviewer"],
                snapshot(alpha=90.0, zeta=60.0),
                exclude={"reviewer": ["alpha", "zeta"]},
            )
        self.assertIn("reviewer", str(caught.exception))
        self.assertIn("alpha, zeta", str(caught.exception))

    def test_an_exclusion_set_no_assignment_satisfies_is_an_error(self):
        # Both roles can only go to alpha, and one agent cannot hold two.
        with self.assertRaises(PlanError) as caught:
            plan(
                ["developer", "reviewer"],
                snapshot(alpha=90.0, zeta=60.0),
                exclude={"reviewer": ["zeta"], "developer": ["zeta"]},
            )
        self.assertIn("Cannot fill role", str(caught.exception))
        self.assertIn("Drop an exclusion", str(caught.exception))

    def test_excluding_a_role_nobody_is_assigning_is_an_error(self):
        with self.assertRaises(PlanError) as caught:
            plan(
                ["developer", "tester"],
                snapshot(alpha=90.0, zeta=60.0),
                exclude={"reviewr": ["alpha"]},
            )
        self.assertIn("reviewr", str(caught.exception))
        self.assertIn("developer, tester", str(caught.exception))

    def test_excluding_an_agent_the_snapshot_lacks_warns_rather_than_refusing(self):
        # The author may be busy and therefore unmeasured; refusing would
        # block a round the exclusion does not actually affect.
        warnings = []
        result = plan(
            ["developer"],
            snapshot(alpha=90.0, zeta=60.0),
            exclude={"developer": ["ghost"]},
            warn=warnings.append,
        )
        self.assertEqual(result["assignments"]["developer"], "alpha")
        self.assertEqual(len(warnings), 1)
        self.assertIn("ghost", warnings[0])
        self.assertIn("ghost", result["rationale"][-1])
        self.assertIn("changed nothing", result["rationale"][-1])

    def test_the_rationale_names_the_exclusions_applied(self):
        result = plan(
            ["developer", "reviewer"],
            snapshot(alpha=90.0, zeta=60.0),
            exclude={"reviewer": ["alpha"]},
        )
        developer_line, reviewer_line = result["rationale"]
        self.assertIn("excluded: none", developer_line)
        self.assertIn("excluded: alpha", reviewer_line)

    def test_no_exclusions_is_the_same_plan_as_before(self):
        data = snapshot(claude=92.0, codex=87.0, grok=100.0)
        self.assertEqual(plan(ROLES, data), plan(ROLES, data, exclude={}))


class SkippedSnapshotTest(unittest.TestCase):
    """A worker measured as working carries the headroom of some earlier round."""

    def _snapshot(self, **skipped):
        data = snapshot(alpha=90.0, zeta=60.0)
        for name, value in skipped.items():
            data["agents"][name]["skipped"] = value
        return data

    def test_a_skipped_agent_is_named_in_the_rationale(self):
        result = plan(["developer"], self._snapshot(zeta=True))
        self.assertIn("stale headroom for zeta", result["rationale"][-1])

    def test_every_skipped_agent_is_named_once_in_one_note(self):
        result = plan(["developer"], self._snapshot(zeta=True, alpha=True))
        notes = [line for line in result["rationale"] if line.startswith("note:")]
        self.assertEqual(len(notes), 1)
        self.assertIn("alpha, zeta", notes[0])

    def test_a_snapshot_with_nothing_skipped_adds_no_note(self):
        result = plan(["developer"], self._snapshot(zeta=False))
        self.assertEqual(len(result["rationale"]), 1)

    def test_the_note_does_not_disturb_the_assignment_lines(self):
        result = plan(["developer", "reviewer"], self._snapshot(zeta=True))
        self.assertIn("developer -> alpha", result["rationale"][0])
        self.assertIn("reviewer -> zeta", result["rationale"][1])


class RefusalTest(unittest.TestCase):
    def test_no_roles_is_an_error(self):
        with self.assertRaises(PlanError):
            plan([], snapshot(grok=100.0))

    def test_duplicate_roles_are_an_error(self):
        with self.assertRaises(PlanError) as caught:
            plan(["developer", "developer"], snapshot(claude=1.0, grok=2.0))
        self.assertIn("developer", str(caught.exception))

    def test_empty_snapshot_is_an_error_naming_measure(self):
        with self.assertRaises(PlanError) as caught:
            plan(ROLES, {"agents": {}})
        self.assertIn("teamlead measure", str(caught.exception))

    def test_missing_snapshot_is_an_error(self):
        with self.assertRaises(PlanError):
            plan(ROLES, None)

    def test_agents_that_is_not_an_object_is_a_plan_error_not_a_crash(self):
        # A hand-edited snapshot with `agents` as a list used to reach
        # `.items()` and die with AttributeError.
        with self.assertRaises(PlanError) as caught:
            plan(ROLES, {"agents": ["claude", "grok", "codex"]})
        self.assertIn("not an object keyed by agent name", str(caught.exception))
        self.assertIn("teamlead measure", str(caught.exception))

    def test_a_snapshot_that_is_not_an_object_is_a_plan_error(self):
        with self.assertRaises(PlanError) as caught:
            plan(ROLES, ["claude", "grok", "codex"])
        self.assertIn("not an object", str(caught.exception))

    def test_more_roles_than_agents_is_an_error_not_a_silent_drop(self):
        with self.assertRaises(PlanError) as caught:
            plan(ROLES, snapshot(grok=100.0, claude=92.0))
        self.assertIn("3 roles", str(caught.exception))




class MalformedHeadroomTest(unittest.TestCase):
    """A snapshot is a file on disk: hand-edited, stale, or truncated.

    `float()` on whatever it happens to hold used to crash the whole plan over
    one bad field. Tracked as jbaruch/coding-policy#315.
    """

    def _plan(self, value, roles=("developer",), extra=None):
        self.warnings = []
        agents = {"broken": {"kind": "x", "state": "idle", "headroom_pct": value}}
        agents.update(extra or {"healthy": {"headroom_pct": 50.0}})
        return plan(
            list(roles),
            {"agents": agents},
            warn=self.warnings.append,
        )

    def test_a_string_that_is_not_a_number_is_unknown(self):
        result = self._plan("lots")
        self.assertEqual(result["assignments"]["developer"], "healthy")

    def test_an_object_is_unknown(self):
        result = self._plan({"pct": 90})
        self.assertEqual(result["assignments"]["developer"], "healthy")

    def test_a_list_is_unknown(self):
        result = self._plan([90])
        self.assertEqual(result["assignments"]["developer"], "healthy")

    def test_a_boolean_is_unknown_not_one_percent(self):
        # bool subclasses int, so float(True) is 1.0 -- a silently wrong
        # ranking rather than a crash, which is worse.
        result = self._plan(True)
        self.assertEqual(result["assignments"]["developer"], "healthy")

    def test_nan_is_unknown_because_it_cannot_be_ordered(self):
        result = self._plan(float("nan"))
        self.assertEqual(result["assignments"]["developer"], "healthy")

    def test_infinity_is_unknown(self):
        result = self._plan(float("inf"))
        self.assertEqual(result["assignments"]["developer"], "healthy")

    def test_a_numeric_string_is_coerced_not_discarded(self):
        result = self._plan("87")
        self.assertEqual(result["assignments"]["developer"], "broken")

    def test_an_agent_entry_that_is_not_an_object_is_unknown(self):
        result = plan(
            ["developer"],
            {"agents": {"broken": "idle", "healthy": {"headroom_pct": 50.0}}},
            warn=[].append,
        )
        self.assertEqual(result["assignments"]["developer"], "healthy")

    def test_the_warning_names_the_agent_and_the_value(self):
        self._plan("lots")
        self.assertEqual(len(self.warnings), 1)
        self.assertIn("broken", self.warnings[0])
        self.assertIn("lots", self.warnings[0])

    def test_a_good_value_warns_about_nothing(self):
        self._plan(70.0)
        self.assertEqual(self.warnings, [])

    def test_a_null_headroom_is_not_a_warning(self):
        # Busy or skipped agents legitimately have none.
        self._plan(None)
        self.assertEqual(self.warnings, [])

    def test_the_bad_value_sorts_last_and_is_named_in_the_rationale(self):
        result = self._plan("lots", roles=("developer", "tester"))
        self.assertEqual(result["assignments"]["tester"], "broken")
        self.assertIn("broken=null", result["rationale"][0])

    def test_every_agent_being_malformed_still_produces_a_plan(self):
        result = plan(
            ["developer", "tester"],
            {"agents": {"zeta": {"headroom_pct": "?"}, "alpha": {"headroom_pct": "?"}}},
            warn=[].append,
        )
        self.assertEqual(
            result["assignments"], {"developer": "alpha", "tester": "zeta"}
        )


class NoDuplicateAgentTest(unittest.TestCase):
    """The planner must never hand one agent two roles.

    `apply` briefs one pane per role, so a duplicate would mean the second
    brief overwriting the first and one role silently going undone.
    """

    def test_every_role_gets_a_different_agent(self):
        result = plan(ROLES, snapshot(claude=92.0, codex=87.0, grok=100.0))
        agents = list(result["assignments"].values())
        self.assertEqual(len(set(agents)), len(agents))

    def test_a_full_tie_still_produces_distinct_agents(self):
        result = plan(ROLES, snapshot(claude=50.0, codex=50.0, grok=50.0))
        agents = list(result["assignments"].values())
        self.assertEqual(sorted(agents), ["claude", "codex", "grok"])

    def test_all_null_headroom_still_produces_distinct_agents(self):
        result = plan(ROLES, snapshot(claude=None, codex=None, grok=None))
        agents = list(result["assignments"].values())
        self.assertEqual(sorted(agents), ["claude", "codex", "grok"])

    def test_all_malformed_headroom_still_produces_distinct_agents(self):
        result = plan(
            ROLES,
            {"agents": {n: {"headroom_pct": "?"} for n in ("claude", "codex", "grok")}},
            warn=[].append,
        )
        agents = list(result["assignments"].values())
        self.assertEqual(sorted(agents), ["claude", "codex", "grok"])

    def test_more_agents_than_roles_leaves_the_extras_unassigned(self):
        result = plan(["developer"], snapshot(claude=92.0, codex=87.0, grok=100.0))
        self.assertEqual(list(result["assignments"].values()), ["grok"])

    def test_the_planner_output_is_accepted_by_apply(self):
        # The two halves of the contract meet here: whatever the planner
        # emits, assign.normalize_assignments must not reject it.
        from teamlead.assign import normalize_assignments

        result = plan(ROLES, snapshot(claude=50.0, codex=50.0, grok=50.0))
        self.assertEqual(
            normalize_assignments(result), result["assignments"]
        )


if __name__ == "__main__":
    unittest.main()

skills

README.md

tile.json