Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:1ay1 Agentty Scope Resolution Algebra

From Leeroopedia
Knowledge Sources
Domains Configuration, Trust_Security
Last Updated 2026-10-28 12:00 GMT

Overview

Config-resolution algebra that answers where a piece of configuration lives, who placed it there, and whether it may execute, replacing five hand-rolled per-feature resolution implementations with one pure fold over a precedence lattice.

Description

The agentty::scope module is the single config-resolution algebra for the codebase. It replaces five partial functions (memory, skills, agents, commands, MCP) that each hand-rolled their own answer with subtly-incompatible shapes. Scope provides total functions returning `std::expected`, sum types over `enum class`, and provenance carried as a value, so illegal states — config with no source, trust inferred from locus — are unrepresentable by construction.

The core is a precedence lattice (`Locus`: Explicit → Local → Project → User) crossed with a dialect axis (`Dialect`: Agentty, Agents, Claude). The `plan()` function generates the ordered source list; two fold monoids — `resolve_first` (override, first present source wins) and `resolve_union` (merge every present source, first-key-wins shadow) — walk that list. Trust is a first-class variant bound to content hash, never inferred from locus, preventing the MCPoison-class CVE (swapping a command under an approved name).

Usage

Import this when you need to resolve config for any feature (memory, skills, agents, commands, MCP, hooks): generate the source ladder, fold over it to get a single value or a union, or determine trust for executable project config.

Code Reference

Source Location

Signature

namespace agentty::scope {

enum class Locus : std::uint8_t { Explicit, Local, Project, User };
enum class Dialect : std::uint8_t { Agentty, Agents, Claude };

// A resolved, tagged origin — the carrier of the fold.
struct Source {
    Locus    locus;
    Dialect  dialect;
    fs::path base;
    bool     writable;
};

template <class T> struct Tagged { T value; Source source; };

// The resolved edge, passed by value (purity contract).
struct Env {
    fs::path home, user_native_base, project_root;
    std::optional<fs::path> explicit_config;
    bool project_writable = false;
};

// What a feature stores, described BY the feature.
struct Layout {
    std::string_view leaf;
    std::string_view explicit_env = {};
    std::span<const Dialect> dialects = kNativeOnly;
};

// Trust: first-class, bound to CONTENT — never inferred from Locus.
struct Trusted {};
struct Pending { std::string content_sha; };
struct Blocked { std::string reason; };
using Trust = std::variant<Trusted, Pending, Blocked>;

// The single generator: ordered source list for a layout.
[[nodiscard]] std::vector<Source> plan(const Layout& layout, const Env& env);

// OVERRIDE monoid — first present source wins (single-file concerns).
template <class T, class Load>
[[nodiscard]] Result<Tagged<T>>
resolve_first(const Layout& layout, const Env& env, Load&& load);

// UNION monoid — merge every present source, first-key-wins shadow.
template <class T, class Key, class Load>
[[nodiscard]] Result<std::vector<Tagged<T>>>
resolve_union(const Layout& layout, const Env& env, Key&& key, Load&& load);

// Trust: (source, content, approvals) → Trust.
[[nodiscard]] Trust trust_of(const Source& src,
                             std::string_view content_sha,
                             const Approvals& approvals) noexcept;

// Build the process-edge Env once (the ONE impure function here).
[[nodiscard]] Env current_env(const Layout& layout);
}

Import

#include "agentty/scope/scope.hpp"

I/O Contract

Inputs

Name Type Required Description
layout Layout Yes Feature's leaf name, explicit env var, and which dialects it reads
env Env Yes Resolved edge: HOME, project_root, explicit config path, writability
load callable Yes `Source -> Result<optional<T>>` (override) or `Source -> Result<vector<T>>` (union)
key callable No Projects an item to its shadow key (for union resolution)
content_sha std::string_view No Content hash for trust evaluation
approvals Approvals No Content-hash approval store (lives under USER root)

Outputs

Name Type Description
plan() std::vector<Source> Ordered source list in precedence (Locus-major, Dialect-minor)
resolve_first() Result<Tagged<T>> First present value with its Source, or NoSource error
resolve_union() Result<std::vector<Tagged<T>>> Merged items with first-key-wins shadow
trust_of() Trust Trusted / Pending(needs approval) / Blocked(refused)

Usage Examples

Override Resolution (Single-File)

#include "agentty/scope/scope.hpp"

// Resolve memory.jsonl — the first present source wins.
agentty::scope::Layout layout{
    .leaf = "memory.jsonl",
    .explicit_env = "AGENTTY_MEMORY_CONFIG"
};
auto env = agentty::scope::current_env(layout);

auto result = agentty::scope::resolve_first<nlohmann::json>(
    layout, env,
    [](const agentty::scope::Source& src) -> agentty::scope::Result<std::optional<nlohmann::json>> {
        if (!std::filesystem::exists(src.base / "memory.jsonl"))
            return std::nullopt;
        // read and parse...
        return parsed_json;
    });
// result->value = the first memory.jsonl found
// result->source = WHERE it came from (for write-back targeting)

Union Resolution (Collection)

// Resolve skills/ — merge skills from every source, project shadows user.
agentty::scope::Layout layout{
    .leaf = "skills",
    .dialects = agentty::scope::kPortable  // read .agentty, .agents, .claude
};
auto env = agentty::scope::current_env(layout);

auto result = agentty::scope::resolve_union<Skill>(
    layout, env,
    [](const Skill& s) { return s.name; },  // shadow key
    [](const agentty::scope::Source& src) {
        // load all skills from src.base / "skills" / ...
        return std::vector<Skill>{ /* ... */ };
    });
// result = merged skills, each tagged with WHERE it came from
// A project skill shadows a user skill of the same name

Trust Gate for Project MCP Config

// Project mcp.json carries executable config — trust must be earned.
auto approvals = agentty::scope::load_approvals("mcp_approvals.json");
std::string sha = agentty::scope::content_hash(mcp_json_bytes);

auto trust = agentty::scope::trust_of(source, sha, approvals);
if (std::holds_alternative<agentty::scope::Pending>(trust)) {
    // Show approval prompt; on user accept:
    approvals.approve(sha);
    agentty::scope::save_approvals(io, file_lane, "mcp_approvals.json", approvals);
    // Next load: trust_of returns Trusted
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment