|
1 | 1 | # AuthorityLayer |
2 | 2 |
|
3 | | -[](https://github.com/032383justin/authority-layer/actions/workflows/ci.yml) |
4 | | -[](https://www.npmjs.com/package/authority-layer) |
5 | | -[](https://opensource.org/licenses/MIT) |
6 | | -[](https://nodejs.org) |
| 3 | + |
7 | 4 |
|
8 | | -Hard execution and budget limits for autonomous agents — enforced locally. |
| 5 | +AuthorityLayer enforces hard runtime limits for AI agents so budgets, loop counts, and tool-call rates fail closed instead of drifting until after damage is done. |
9 | 6 |
|
10 | | -✔ No telemetry |
11 | | -✔ Works fully offline |
12 | | -✔ Fail-closed by default |
13 | | -✔ Zero runtime dependencies |
| 7 | +## What This Project Does |
14 | 8 |
|
15 | | ---- |
| 9 | +AuthorityLayer gives developers a minimal runtime control layer for autonomous or tool-using agents. It wraps an execution loop, tracks spend, limits tool-call volume, and throws a typed halt when an agent crosses a configured boundary. The goal is not observability after the fact. The goal is deterministic enforcement during execution. |
16 | 10 |
|
17 | | -## Why AuthorityLayer Exists |
| 11 | +The library is aimed at developers building agent runtimes, internal automation, LLM-backed services, and safety-sensitive tools that need explicit operational limits. If a workflow should stop after a dollar budget, after a certain number of tool calls, or after a rate cap is reached, AuthorityLayer provides those controls directly in code. |
18 | 12 |
|
19 | | -Autonomous AI agents can fail in expensive, hard-to-detect ways: |
| 13 | +## Why It Exists |
20 | 14 |
|
21 | | -- **Runaway token spend** — a looping agent burns thousands of dollars before anyone notices |
22 | | -- **Infinite tool loops** — agents retry the same failing call indefinitely |
23 | | -- **Retry storms** — cascading failures hammer external APIs with no ceiling |
24 | | -- **Cascading tool call explosions** — one agent spawns sub-calls that spawn more |
| 15 | +Many agent systems fail in ordinary ways long before they fail in exotic ones. A loop retries a tool forever. A model call path consumes more tokens than expected. A badly bounded automation script keeps hitting an external API because nothing in the runtime says stop. Post-run dashboards can describe the incident, but they do not prevent it. |
25 | 16 |
|
26 | | -Most tooling detects these problems after they happen — in dashboards, alerts, or post-run analytics. |
| 17 | +AuthorityLayer was created to make those boundaries local, explicit, and enforceable. It treats spend caps, loop limits, and rate limits as runtime invariants rather than optional monitoring. That keeps the implementation small, easy to audit, and practical for teams that want guardrails without adopting a full platform. |
27 | 18 |
|
28 | | -AuthorityLayer prevents them inside the runtime, before cost or damage accumulates. |
| 19 | +## Quickstart |
29 | 20 |
|
30 | | -It helps developers: |
31 | | - |
32 | | -- prevent runaway LLM costs |
33 | | -- stop infinite agent loops |
34 | | -- limit AI agent tool calls per run and per minute |
35 | | -- enforce runtime safety for autonomous agents |
36 | | - |
37 | | ---- |
38 | | - |
39 | | -## Live Enforcement Demo (10-second example) |
40 | | - |
41 | | - |
42 | | - |
43 | | ---- |
44 | | - |
45 | | -## Quick Start |
| 21 | +Install the package and run the environment check: |
46 | 22 |
|
47 | 23 | ```bash |
48 | 24 | npm install authority-layer |
49 | | -``` |
50 | | - |
51 | | -Verify the install: |
52 | | - |
53 | | -```bash |
54 | 25 | npx authority-layer doctor |
55 | 26 | ``` |
56 | 27 |
|
57 | | -``` |
58 | | -AuthorityLayer Doctor authority-layer@0.1.2 |
59 | | -
|
60 | | - ✔ Node.js version >= 18 pass |
61 | | - ✔ crypto module (sha256) pass |
62 | | - ✔ AUTHORITY_LAYER_DISABLE not set pass |
63 | | - ✔ core module loads offline pass |
64 | | - ✔ AuthorityLayer instantiates pass |
65 | | -
|
66 | | -All checks passed. AuthorityLayer is ready. |
67 | | -``` |
68 | | - |
69 | | -## CLI Tools |
| 28 | +If the doctor command passes, you can create an `AuthorityLayer` instance and wrap the part of your agent loop that must stay within budget and call limits. |
70 | 29 |
|
71 | | -| Command | What it does | |
72 | | -|---------|-------------| |
73 | | -| `npx authority-layer doctor` | Verify your installation passes all environment checks | |
74 | | -| `npx authority-layer simulate` | Run a live enforcement simulation — see a halt in action without writing any code | |
| 30 | +## Example |
75 | 31 |
|
76 | | ---- |
77 | | - |
78 | | -## Minimal Integration |
| 32 | +Minimal example: |
79 | 33 |
|
80 | 34 | ```typescript |
81 | 35 | import { AuthorityLayer, EnforcementHalt } from "authority-layer"; |
82 | 36 |
|
83 | 37 | const authority = new AuthorityLayer({ |
84 | | - budget: { dailyUSD: 50 }, // Hard USD spend cap |
85 | | - loopGuard: { maxToolCallsPerRun: 25 }, // Max tool calls per run |
86 | | - toolThrottle: { maxCallsPerMinute: 60 }, // Sliding-window rate cap |
| 38 | + budget: { dailyUSD: 25 }, |
| 39 | + loopGuard: { maxToolCallsPerRun: 12 }, |
| 40 | + toolThrottle: { maxCallsPerMinute: 30 }, |
87 | 41 | }); |
88 | 42 |
|
89 | 43 | try { |
90 | 44 | await authority.wrap(async () => { |
91 | | - const result = await authority.tool("llm.chat", () => |
92 | | - callYourModel(prompt) |
93 | | - ); |
| 45 | + const result = await authority.tool("llm.chat", () => callModel(prompt)); |
94 | 46 | authority.recordSpend(calculateCostUSD(result)); |
95 | 47 | }); |
96 | | -} catch (err) { |
97 | | - if (err instanceof EnforcementHalt) { |
98 | | - console.error(err.enforcement); |
99 | | - // { status: "halted", reason: "budget_exceeded", limit: 50, spent: 52.14, event_id: "evt_..." } |
| 48 | +} catch (error) { |
| 49 | + if (error instanceof EnforcementHalt) { |
| 50 | + console.error(error.enforcement); |
100 | 51 | } |
101 | 52 | } |
102 | 53 | ``` |
103 | 54 |
|
104 | | ---- |
| 55 | +For a runnable project example, use the bundled demo in `examples/` and the workspace script: |
105 | 56 |
|
106 | | -## Enforcement Primitives |
| 57 | +```bash |
| 58 | +npm run example |
| 59 | +``` |
107 | 60 |
|
108 | | -AuthorityLayer V1 provides three composable enforcement primitives. Each is opt-in — omit a config key to disable it. |
| 61 | +That flow exercises the same enforcement surface used by the package: `wrap()` defines the run boundary, `tool()` tracks tool usage and rate limits, and `recordSpend()` reports explicit provider cost data. |
109 | 62 |
|
110 | | -These primitives enforce boundaries directly inside the execution loop — not in dashboards or external monitoring. |
| 63 | +## How It Works |
111 | 64 |
|
112 | | -| Primitive | Config key | What it enforces | |
113 | | -|-----------|------------|------------------| |
114 | | -| **Budget cap** | `budget.dailyUSD` | Cumulative USD spend across the process lifetime. Halts when spend exceeds the cap. → [docs](./docs/enforcement.md#1-budget-cap) | |
115 | | -| **Loop guard** | `loopGuard.maxToolCallsPerRun` | Total tool calls per `wrap()` invocation. Counter resets each run. → [docs](./docs/enforcement.md#2-loop-guard) | |
116 | | -| **Tool throttle** | `toolThrottle.maxCallsPerMinute` | Rate of tool calls using a sliding 60-second window — no fixed buckets. → [docs](./docs/enforcement.md#3-tool-throttle) | |
| 65 | +AuthorityLayer is organized as a small workspace with the package implementation, runnable examples, and reference docs kept close together. |
117 | 66 |
|
118 | | -When a primitive breaches, AuthorityLayer throws a typed `EnforcementHalt` error with a structured `.enforcement` object. Execution never crashes silently. |
| 67 | +- `packages/core/src/` contains the runtime implementation, CLI entrypoint, enforcement primitives, integrity helpers, and exported types. |
| 68 | +- `examples/` contains runnable examples for quick integration checks and demos. |
| 69 | +- `docs/` contains concept notes, API reference, enforcement details, and integrity-chain documentation. |
| 70 | +- `tests/` contains workspace-level smoke tests that keep the top-level layout and package metadata honest. |
| 71 | +- `src/` documents the workspace source map so contributors can find the package entrypoints quickly. |
119 | 72 |
|
120 | | ---- |
| 73 | +There are exactly three enforcement primitives: a budget cap, a loop guard, and a tool throttle. Each primitive is opt-in. If you omit a config block, that guard is not instantiated. When a configured limit is breached, AuthorityLayer throws `EnforcementHalt` with structured data instead of forcing callers to parse log output. |
121 | 74 |
|
122 | | -## How AuthorityLayer Is Different |
| 75 | +Operational boundaries are explicit. AuthorityLayer does not claim to sandbox arbitrary code, but it does define clear permission and approval boundaries inside an agent runtime: only wrapped runs are counted, only calls made through `authority.tool()` are throttled, and spend is recorded only when the host reports it. Evaluation should happen before release using repeatable tests and demos so halt behavior is visible under controlled conditions. |
123 | 76 |
|
124 | | -Most AI guardrail tools focus on moderation or observability. AuthorityLayer focuses on **runtime enforcement**. |
| 77 | +## Use Cases |
125 | 78 |
|
126 | | -| Tool type | What it does | |
127 | | -|-----------|-------------| |
128 | | -| Prompt guardrails | Filter or rewrite prompts and outputs | |
129 | | -| Observability platforms | Analyze agent behavior after execution | |
130 | | -| Cost analytics | Track and report token usage | |
131 | | -| **AuthorityLayer** | Enforces hard limits **during** execution — halts immediately when a boundary is crossed | |
| 79 | +- Stop an internal agent after a daily spend budget is exhausted. |
| 80 | +- Prevent a tool-using workflow from entering an unbounded retry loop. |
| 81 | +- Enforce a maximum number of external tool calls per run. |
| 82 | +- Rate-limit calls to a provider or internal service from an autonomous workflow. |
| 83 | +- Add fail-closed controls to a prototype agent before exposing it to users. |
| 84 | +- Demonstrate runtime guardrail behavior in a reproducible example or CI pipeline. |
132 | 85 |
|
133 | | ---- |
| 86 | +## Installation |
134 | 87 |
|
135 | | -## Documentation |
| 88 | +For package consumers: |
136 | 89 |
|
137 | | -| Topic | File | |
138 | | -|------|------| |
139 | | -| Concepts & philosophy | [docs/concepts.md](./docs/concepts.md) | |
140 | | -| Enforcement primitives | [docs/enforcement.md](./docs/enforcement.md) | |
141 | | -| API reference | [docs/api.md](./docs/api.md) | |
142 | | -| Integrity chain | [docs/integrity.md](./docs/integrity.md) | |
143 | | -| Example run | `npm run example` | |
| 90 | +```bash |
| 91 | +npm install authority-layer |
| 92 | +``` |
144 | 93 |
|
145 | | ---- |
| 94 | +For contributors working on the repository: |
146 | 95 |
|
147 | | -AuthorityLayer is designed as a minimal enforcement primitive — not a platform, dashboard, or governance system. |
| 96 | +```bash |
| 97 | +git clone https://github.com/repoforge-dev/authority-layer.git |
| 98 | +cd authority-layer |
| 99 | +npm install |
| 100 | +npm test |
| 101 | +npm run -w authority-layer build |
| 102 | +npm run -w authority-layer typecheck |
| 103 | +``` |
148 | 104 |
|
149 | | ---- |
| 105 | +The workspace keeps contributor commands small. The root package coordinates examples and tests, while `packages/core` contains the published package source and build scripts. |
150 | 106 |
|
151 | | -## License |
| 107 | +## Why RepoScore Matters |
| 108 | + |
| 109 | +Many GitHub repositories have thousands of stars but still lack strong documentation, onboarding clarity, or discoverability. |
| 110 | + |
| 111 | +RepoScore automatically analyzes repositories and identifies practical improvements that make projects easier for developers and AI agents to use. |
| 112 | + |
| 113 | +## RepoScore |
| 114 | + |
| 115 | +Badge: |
152 | 116 |
|
153 | | -MIT © 2025 AuthorityLayer Contributors |
| 117 | +```md |
| 118 | + |
| 119 | +``` |
| 120 | + |
| 121 | +Analysis page: |
| 122 | + |
| 123 | +- `https://repoforge.dev/repos/repoforge-dev/authority-layer` |
| 124 | + |
| 125 | +The RepoScore page is useful here because AuthorityLayer is a runtime-safety package. A public analysis page makes it easy to inspect documentation quality, discoverability, and onboarding clarity from the perspective of a developer evaluating whether to trust the project in a real workflow. |
| 126 | + |
| 127 | +## Contributing |
| 128 | + |
| 129 | +Contributions should keep the runtime surface explicit and easy to audit. Favor small changes, typed interfaces, and examples that demonstrate enforcement behavior clearly. When you change runtime semantics, update the docs and example flows in the same pull request so contributors can evaluate the impact without reverse-engineering the source. |
| 130 | + |
| 131 | +Start with [CONTRIBUTING.md](./CONTRIBUTING.md). It documents the workspace layout, test commands, and expectations for changes that affect halt behavior, permissions, or approval boundaries. |
| 132 | + |
| 133 | +## License |
154 | 134 |
|
155 | | -[GitHub](https://github.com/032383justin/authority-layer) · [npm](https://www.npmjs.com/package/authority-layer) · [Issues](https://github.com/032383justin/authority-layer/issues) |
| 135 | +MIT. See [LICENSE](./LICENSE). |
0 commit comments