CtrlK
BlogDocsLog inGet started
Tessl Logo
PluginPublic

jbaruch/herdr-policy

Herdr team coordination policy for AI coding agents

Quality

93%

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

observer_role.pyskills/herdr-foreman/foreman/

"""Read observer migration 1's binding without migrating or scanning telemetry.

Schema owner/contract: skills/herdr-observer/references/storage.md; migration 1
is owned by factory_observer/store.py. A missing, incompatible or insecure cache
establishes no observer role. This reader creates no files or SQLite journals.
"""

from contextlib import closing
import json
from itertools import islice
import os
from pathlib import Path
import sqlite3
import stat

from . import supervision
from .errors import ForemanError, StateError


MAX_BINDINGS = 4096
MAX_RECEIPT_BYTES = 16384


def delivery_owner(pane_id, state_paths=(), environment=None):
    """Current ownership, even when the foreman is offline; never migrate it.

The supervision owner validates leases/enrollments. Index files are only
locators. A reserved pane, including an unresolved native handoff, cannot be
repurposed by registering an observer. No live foreman is required to retain it.
"""
    environment = os.environ if environment is None else environment
    root = Path(environment.get("XDG_STATE_HOME", str(Path.home() / ".local/state"))) / "foreman"
    paths = {Path(path) for path in state_paths} | {root / "state.json"}
    hint = environment.get("FOREMAN_CONTEXT_STATE")
    if hint:
        if not Path(hint).is_absolute():
            raise StateError("Restore the absolute current owner locator before registering an observer.", {})
        paths.add(Path(hint))
    index = root / "supervision-bindings"
    if index.exists():
        entries = list(islice(index.iterdir(), MAX_BINDINGS + 1))
        if len(entries) > MAX_BINDINGS:
            raise StateError("Resolve the supervision binding index coverage before registering an observer.", {})
        for entry in entries:
            if entry.suffix != ".json":
                continue
            descriptor = os.open(entry, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK)
            with os.fdopen(descriptor, "rb") as stream:
                if not stat.S_ISREG(os.fstat(stream.fileno()).st_mode):
                    raise StateError("Restore regular supervision binding receipts before registering an observer.", {})
                raw = stream.read(MAX_RECEIPT_BYTES + 1)
            if len(raw) > MAX_RECEIPT_BYTES:
                raise StateError("Restore bounded supervision binding receipts before registering an observer.", {})
            receipt = json.loads(raw)
            if (not isinstance(receipt, dict) or type(receipt.get("schema_version")) is not int
                    or receipt["schema_version"] != 1 or not isinstance(receipt.get("state_path"), str)
                    or not Path(receipt["state_path"]).is_absolute()):
                raise StateError("Restore versioned supervision binding receipts before registering an observer.", {})
            paths.add(Path(receipt["state_path"]))
    for path in sorted(paths):
        data = supervision.load(path)
        if data["binding"] is not None and data["binding"]["identity"].get("pane_id") == pane_id:
            return {"role": "foreman", "state_path": str(path)}
        for member in data["members"]:
            if member["active"] and supervision.expected_assignment(member)["pane_id"] == pane_id:
                return {"role": "worker", "state_path": str(path)}
    return None


def matches(who, native, environment, *, client):
    if any(who.get(field) != native.get(field) for field in ("kind", "value")):
        return False
    root = Path(environment.get("XDG_STATE_HOME", str(Path.home() / ".local/state"))) / "herdr-observer"
    path = root / "observer.sqlite3"
    try:
        for target, directory in ((root, True), (path, False)):
            info = target.lstat()
            expected = stat.S_ISDIR(info.st_mode) if directory else stat.S_ISREG(info.st_mode)
            if not expected or info.st_uid != os.getuid() or info.st_mode & 0o077 or (not directory and info.st_nlink != 1):
                return False
        with closing(sqlite3.connect(path.as_uri() + "?mode=ro", uri=True, timeout=0.1)) as db:
            if db.execute("PRAGMA user_version").fetchone()[0] != 1:
                return False
            rows = dict(db.execute("SELECT key,value FROM meta WHERE key IN ('binding','config')"))
        binding, config = (json.loads(rows[key]) for key in ("binding", "config"))
        if not isinstance(binding, dict) or not isinstance(config, dict):
            return False
        if (type(binding.get("schema_version")) is not int or binding["schema_version"] != 1
                or type(config.get("schema_version")) is not int or config["schema_version"] != 1
                or config.get("enabled") is not True):
            return False
        saved = binding.get("identity")
        paths = config.get("states")
        if (not isinstance(saved, dict)
                or not isinstance(binding.get("name"), str) or not binding["name"]
                or any(not isinstance(saved.get(field), str) or not saved[field] for field in ("pane_id", "source", "agent", "kind", "value"))
                or saved["source"] != "herdr:" + saved["agent"] or saved["kind"] not in ("id", "path")
                or saved["kind"] == "path" and not Path(saved["value"]).is_absolute()
                or not isinstance(binding.get("cwd"), str) or not Path(binding["cwd"]).is_absolute()
                or not isinstance(paths, list) or any(not isinstance(path, str) or not Path(path).is_absolute() for path in paths)):
            return False
        identity = {"pane_id": who["pane_id"], **{k: native[k] for k in ("source", "agent", "kind", "value")}}
        if binding.get("identity") != identity or binding.get("cwd") != who["cwd"]:
            return False
        if delivery_owner(who["pane_id"], paths, environment) is not None:
            return False
        # PaneInfo has a label, not the registered agent name. Resolve only the
        # saved name through AgentInfo and match it to this exact pane/session.
        live = client.agent_get(binding["name"])
        if not isinstance(live, dict) or live.get("name") != binding["name"] or live.get("pane_id") != who["pane_id"]:
            return False
        live_native = live.get("agent_session")
        return (isinstance(live_native, dict) and live_native.get("pane_id", who["pane_id"]) == who["pane_id"]
                and all(live_native.get(field) == native[field] for field in ("source", "agent", "kind", "value")))
    except (OSError, ValueError, KeyError, TypeError, sqlite3.Error, ForemanError):
        return False

skills

herdr-foreman

foreman

__init__.py

__main__.py

antigravity_native.py

assign.py

attention_view.py

attention.py

billing.py

bootstrap_recovery.py

brief_inputs.py

capabilities.py

chronology.py

churn.py

claude_native.py

cli.py

codex_goals.py

command_context.py

composer.py

composition.py

config.py

context_evidence.py

context_output.py

coordination_metrics.py

cost_report.py

delivery_collection.py

diagnostics.py

engagement.py

errors.py

foreman_queue.py

foreman_reset.py

github_context.py

herdr.py

historical.py

home.py

host_goals.py

launch.py

lifecycle.py

load_set.py

measure.py

measurement_capture.py

measurement_retry.py

members.py

memory.py

model_unavailability.py

native_permissions.py

native_policy.py

no_work_recovery.py

observer_projection.py

observer_role.py

oracle.py

parsers.py

partition.py

plan_explanation.py

planner.py

policy_paths.py

probe_recovery.py

probe.py

process_lease.py

progression_owner.py

progression_runtime.py

progression_service.py

provision.py

recovery.py

release_activation.py

release_checkout.py

release_flight.py

release_rebind.py

remote_owner.py

remote_service.py

renderable.py

report_contract.py

report_delivery.py

report_gates.py

reset_input_hook.py

restoration.py

retrospective_runtime.py

retrospective.py

role_clear.py

role_context.py

runnable.py

selection.py

session_start_hook.py

state.py

successors.py

supervision_gate.py

supervision_hook.py

supervision_runtime.py

supervision.py

task_evidence.py

tier_routing.py

tiers.py

triggers.py

workflow_adoption.py

workflow_artifacts.py

workflow_batch.py

workflow_capacity.py

workflow_contract.py

workflow_dispatch.py

workflow_native.py

workflow_planning.py

workflow_profiles.py

workflow_readiness.py

workflow_recovery.py

workflow_release.py

workflow_rename.py

workflow_review.py

workflow_service.py

workflow_upgrade.py

workflow_workspace.py

antigravity.example.json

bootstrap-repository.py

compose-briefs.sh

config.example.json

foreman-tier-check.py

foreman.sh

label-workspaces.sh

measurements.py

native-hook.sh

observer-view.py

policy-paths.py

prepare-briefs.py

provision-worktree.sh

prune-report-caches.py

resolve-gates.sh

resolve-policy-paths.sh

review-package.sh

roster.sh

round-preflight.sh

SKILL.md

start-judge-worker.sh

state-schema.md

sweep-worktrees.sh

verify-authority.sh

wait-report.sh

workflow-review.py

README.md

tile.json