guardplane 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +167 -0
- package/dist/actions.d.ts +14 -0
- package/dist/actions.js +1 -0
- package/dist/confidence-gate.d.ts +18 -0
- package/dist/confidence-gate.js +27 -0
- package/dist/control-plane.d.ts +62 -0
- package/dist/control-plane.js +91 -0
- package/dist/detector.d.ts +30 -0
- package/dist/detector.js +82 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +10 -0
- package/dist/kill-switch.d.ts +20 -0
- package/dist/kill-switch.js +36 -0
- package/dist/signal-plane.d.ts +27 -0
- package/dist/signal-plane.js +33 -0
- package/dist/types.d.ts +45 -0
- package/dist/types.js +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mohamed Kadri
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# guardplane
|
|
2
|
+
|
|
3
|
+
[](https://github.com/mkadri85/guardplane/actions/workflows/ci.yml)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
A minimal, framework-agnostic **control plane for AI agent fleets**: a signal plane, a confidence gate, and a kill switch. Zero runtime dependencies.
|
|
9
|
+
|
|
10
|
+
AI agents have started causing the production incidents they were built to resolve. In April 2026 a coding agent deleted a company's production database and its backups in about nine seconds. The pattern is not rare, and it has the same shape every time: an agent takes a real action on an incomplete view of the world, and nothing stands between its intent and the damage.
|
|
11
|
+
|
|
12
|
+
This is a small reference implementation of the thing that stands in between. It is the companion to the write-up [**Who Operates the Operators?**](https://mkadri85.github.io/blog/agentic-sre-agent-fleets/).
|
|
13
|
+
|
|
14
|
+
It is not a framework. It is the skeleton you wrap around whatever agent you already run.
|
|
15
|
+
|
|
16
|
+
## The idea
|
|
17
|
+
|
|
18
|
+
Take the incident loop that keeps infrastructure alive and point it at the agents themselves:
|
|
19
|
+
|
|
20
|
+
- **Signal plane** - record one replayable event per agent decision. You cannot operate what you cannot see.
|
|
21
|
+
- **Reasoning layer** - detectors decide whether an agent is actually broken (a loop, a cost runaway, an error spike, quiet quality drift).
|
|
22
|
+
- **Confidence gate** - the whole safety design. High confidence in a known, reversible fix closes the loop itself; anything novel or ambiguous escalates to a human.
|
|
23
|
+
- **Action layer** - a short menu of reversible remediations: reroute, roll back, pause.
|
|
24
|
+
- **Kill switch** - a global and per-agent off switch you build before you need it.
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
CONTROL PLANE identity . policy . tool contracts . replay . kill switch
|
|
28
|
+
^ decide (escalate on low confidence)
|
|
29
|
+
ACTION LAYER reroute . roll back . pause
|
|
30
|
+
^
|
|
31
|
+
REASONING LAYER detect -> triage -> correlate (is this agent broken?)
|
|
32
|
+
^
|
|
33
|
+
SIGNAL PLANE one replayable record per decision
|
|
34
|
+
^
|
|
35
|
+
AGENT FLEET [ a1 ] [ a2 ] [ a3 ] [ a9 failing ] ...
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Try it
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
git clone https://github.com/mkadri85/guardplane
|
|
42
|
+
cd guardplane
|
|
43
|
+
npm install
|
|
44
|
+
npm run demo
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The demo runs a small fleet through the plane: a healthy agent proceeds, a looping agent is auto-rerouted, a cost runaway is auto-paused, a quietly drifting agent is escalated to a human with its full replay, and then the kill switch stops the whole fleet.
|
|
48
|
+
|
|
49
|
+
```text
|
|
50
|
+
guardplane live demo
|
|
51
|
+
|
|
52
|
+
agent-01 healthy: three clean tool calls
|
|
53
|
+
allowed call 1
|
|
54
|
+
allowed call 2
|
|
55
|
+
allowed call 3
|
|
56
|
+
|
|
57
|
+
agent-04 starts looping on a failing tool
|
|
58
|
+
allowed retry 1
|
|
59
|
+
allowed retry 2
|
|
60
|
+
allowed retry 3
|
|
61
|
+
-> reroute agent-04: known loop, switch model
|
|
62
|
+
allowed retry 4 [auto_remediate: confidence 0.90 >= 0.8 and reroute is auto-allowed]
|
|
63
|
+
|
|
64
|
+
agent-09 output quality quietly drifting
|
|
65
|
+
allowed response 1
|
|
66
|
+
allowed response 2
|
|
67
|
+
allowed response 3
|
|
68
|
+
-> pause agent-09: containing before human review
|
|
69
|
+
ESCALATE agent-09 -> human
|
|
70
|
+
why: output quality drifting (2/4 weak responses) (confidence 0.50)
|
|
71
|
+
handing over the last 4 of 4 recorded decisions:
|
|
72
|
+
model_response ok 300 tok
|
|
73
|
+
model_response ERR 300 tok
|
|
74
|
+
model_response ok 300 tok
|
|
75
|
+
model_response ERR 300 tok
|
|
76
|
+
stopped response 4 [escalate: confidence 0.50 < 0.8]
|
|
77
|
+
|
|
78
|
+
agent-07 burning tokens far past its budget
|
|
79
|
+
-> pause agent-07: token spend 12000 over budget 5000
|
|
80
|
+
stopped one very expensive step [auto_remediate: confidence 0.95 >= 0.8 and pause is auto-allowed]
|
|
81
|
+
|
|
82
|
+
kill switch tripped for the whole fleet
|
|
83
|
+
stopped agent-01 tries another call [blocked: kill switch tripped]
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Use it
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import {
|
|
90
|
+
controlPlane,
|
|
91
|
+
loopDetector,
|
|
92
|
+
costRunawayDetector,
|
|
93
|
+
errorRateDetector,
|
|
94
|
+
driftDetector,
|
|
95
|
+
type Actions,
|
|
96
|
+
} from "guardplane";
|
|
97
|
+
|
|
98
|
+
// You implement these against your own runtime.
|
|
99
|
+
const actions: Actions = {
|
|
100
|
+
reroute: (id, why) => switchModel(id),
|
|
101
|
+
rollback: (id, why) => revertAgent(id),
|
|
102
|
+
pause: (id, why) => drainToHumanQueue(id),
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const plane = controlPlane({
|
|
106
|
+
detectors: [
|
|
107
|
+
loopDetector({ repeats: 4 }),
|
|
108
|
+
costRunawayDetector({ maxTokens: 50_000 }),
|
|
109
|
+
errorRateDetector({ threshold: 0.6 }),
|
|
110
|
+
driftDetector(),
|
|
111
|
+
],
|
|
112
|
+
actions,
|
|
113
|
+
gate: { autoActThreshold: 0.8, autoAllow: ["reroute", "pause"] },
|
|
114
|
+
onEscalate: ({ agentId, health, replay }) => {
|
|
115
|
+
pageOnCall(agentId, health, replay); // hand a human the full trace
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const agent = plane.wrap("agent-09");
|
|
120
|
+
|
|
121
|
+
// Report each decision your agent makes. The plane records it, diagnoses the
|
|
122
|
+
// agent, and tells you whether it may proceed.
|
|
123
|
+
const { allowed, decision } = agent.observe({
|
|
124
|
+
type: "tool_call",
|
|
125
|
+
tool: "search",
|
|
126
|
+
ok: false,
|
|
127
|
+
tokens: 300,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
if (!allowed) {
|
|
131
|
+
// the plane paused or escalated this agent; decision.reason says why
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// The off switch, no deploy required:
|
|
135
|
+
plane.killSwitch.trip("agent-09"); // or "global" for the whole fleet
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## The confidence gate
|
|
139
|
+
|
|
140
|
+
The gate is the only interesting decision in the system, so it is worth stating plainly. An agent earns autonomy per failure type; it is not a switch you flip once.
|
|
141
|
+
|
|
142
|
+
| Diagnosis | Proposed fix | Ruling |
|
|
143
|
+
| --- | --- | --- |
|
|
144
|
+
| Confidence >= threshold, fix is known-safe and reversible | reroute / pause | **auto-remediate** |
|
|
145
|
+
| Confidence below threshold | anything | **escalate** to a human |
|
|
146
|
+
| Any confidence, but the fix is not on the auto-allow list | rollback, etc. | **escalate** to a human |
|
|
147
|
+
| Healthy | none | **allow** |
|
|
148
|
+
|
|
149
|
+
The default threshold is `0.8` and the default auto-allowed actions are `reroute` and `pause`. Both are configurable per plane.
|
|
150
|
+
|
|
151
|
+
## What this is, and is not
|
|
152
|
+
|
|
153
|
+
- It **is** a dependency-free skeleton you can read in one sitting and wire into an existing agent runtime in an afternoon.
|
|
154
|
+
- It **is not** a hosted platform, an observability product, or a replacement for a real agent framework. It has no opinion about how your agents are built.
|
|
155
|
+
- The detectors and actions are deliberately simple. They are meant to be replaced with your own.
|
|
156
|
+
|
|
157
|
+
Detection is the hard part in practice: subtle, non-deterministic quality drift is exactly what standard checks miss. The `driftDetector` here is a placeholder for the more sensitive, continuous evaluation that real fleets need.
|
|
158
|
+
|
|
159
|
+
## Design principles
|
|
160
|
+
|
|
161
|
+
- **Reversible actions only.** Every built-in remediation can be undone. The gate is allowed to act on its own precisely because it cannot do anything it cannot take back.
|
|
162
|
+
- **Contain before you page.** On escalation the plane pauses the agent first, then hands a human the full replay.
|
|
163
|
+
- **The boring parts come first.** Per-agent identity, one recorded decision per step, and a kill switch matter more than the clever autonomous loop. Build them first.
|
|
164
|
+
|
|
165
|
+
## License
|
|
166
|
+
|
|
167
|
+
MIT. Built by [Mohamed Kadri](https://mkadri85.github.io/).
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AgentId } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* The action layer. A deliberately short menu of real, reversible remediations
|
|
4
|
+
* the control plane can apply to an agent. You implement these against your own
|
|
5
|
+
* runtime (swap the model, revert a version, drain the agent's work to a queue).
|
|
6
|
+
*/
|
|
7
|
+
export interface Actions {
|
|
8
|
+
/** Send the agent's traffic to a different model or provider. */
|
|
9
|
+
reroute(agentId: AgentId, detail?: string): void | Promise<void>;
|
|
10
|
+
/** Roll the agent back to its last known-good version. */
|
|
11
|
+
rollback(agentId: AgentId, detail?: string): void | Promise<void>;
|
|
12
|
+
/** Pause the agent and drain its work to a human queue. */
|
|
13
|
+
pause(agentId: AgentId, detail?: string): void | Promise<void>;
|
|
14
|
+
}
|
package/dist/actions.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Decision, Health, Remediation, RemediationKind } from "./types.js";
|
|
2
|
+
export interface GatePolicy {
|
|
3
|
+
/** Minimum confidence to act without a human. Default 0.8. */
|
|
4
|
+
autoActThreshold?: number;
|
|
5
|
+
/**
|
|
6
|
+
* Remediations the plane may apply on its own, for known, low-risk, reversible
|
|
7
|
+
* cases. Anything not listed here always escalates. Default: reroute + pause.
|
|
8
|
+
*/
|
|
9
|
+
autoAllow?: RemediationKind[];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The confidence gate: the whole safety design in one function.
|
|
13
|
+
*
|
|
14
|
+
* High confidence in the diagnosis AND a known-safe, reversible remediation ->
|
|
15
|
+
* the loop closes itself. Anything novel, ambiguous, or risky -> escalate to a
|
|
16
|
+
* human. Autonomy is earned per failure type, not switched on globally.
|
|
17
|
+
*/
|
|
18
|
+
export declare function confidenceGate(policy?: GatePolicy): (health: Health, remediation: Remediation) => Decision;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The confidence gate: the whole safety design in one function.
|
|
3
|
+
*
|
|
4
|
+
* High confidence in the diagnosis AND a known-safe, reversible remediation ->
|
|
5
|
+
* the loop closes itself. Anything novel, ambiguous, or risky -> escalate to a
|
|
6
|
+
* human. Autonomy is earned per failure type, not switched on globally.
|
|
7
|
+
*/
|
|
8
|
+
export function confidenceGate(policy = {}) {
|
|
9
|
+
const threshold = policy.autoActThreshold ?? 0.8;
|
|
10
|
+
const autoAllow = new Set(policy.autoAllow ?? ["reroute", "pause"]);
|
|
11
|
+
return (health, remediation) => {
|
|
12
|
+
if (health.status === "healthy") {
|
|
13
|
+
return { action: "allow", reason: "healthy" };
|
|
14
|
+
}
|
|
15
|
+
if (health.confidence >= threshold && autoAllow.has(remediation.kind)) {
|
|
16
|
+
return {
|
|
17
|
+
action: "auto_remediate",
|
|
18
|
+
remediation,
|
|
19
|
+
reason: `confidence ${health.confidence.toFixed(2)} >= ${threshold} and ${remediation.kind} is auto-allowed`,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const why = health.confidence < threshold
|
|
23
|
+
? `confidence ${health.confidence.toFixed(2)} < ${threshold}`
|
|
24
|
+
: `${remediation.kind} is not in the auto-allow list`;
|
|
25
|
+
return { action: "escalate", remediation, reason: why };
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { AgentEvent, AgentId, Decision, Detector, Health, Remediation } from "./types.js";
|
|
2
|
+
import { SignalPlane, type SignalSink } from "./signal-plane.js";
|
|
3
|
+
import { KillSwitch } from "./kill-switch.js";
|
|
4
|
+
import { type GatePolicy } from "./confidence-gate.js";
|
|
5
|
+
import type { Actions } from "./actions.js";
|
|
6
|
+
/** Everything the control plane hands a human when it decides to escalate. */
|
|
7
|
+
export interface EscalationContext {
|
|
8
|
+
agentId: AgentId;
|
|
9
|
+
health: Health;
|
|
10
|
+
decision: Decision;
|
|
11
|
+
replay: AgentEvent[];
|
|
12
|
+
}
|
|
13
|
+
export interface ControlPlaneConfig {
|
|
14
|
+
/** The reasoning layer: run in order, worst finding wins. */
|
|
15
|
+
detectors: Detector[];
|
|
16
|
+
/** The action layer implementation. */
|
|
17
|
+
actions: Actions;
|
|
18
|
+
/** Called when the gate decides a human is needed. */
|
|
19
|
+
onEscalate: (ctx: EscalationContext) => void;
|
|
20
|
+
/** Confidence-gate policy (threshold, auto-allowed remediations). */
|
|
21
|
+
gate?: GatePolicy;
|
|
22
|
+
/** How far back detectors look, in ms. Default 60_000. */
|
|
23
|
+
windowMs?: number;
|
|
24
|
+
/** Optional durable sink for the signal plane. */
|
|
25
|
+
signalSink?: SignalSink;
|
|
26
|
+
/** Injectable clock, for tests and demos. Default Date.now. */
|
|
27
|
+
now?: () => number;
|
|
28
|
+
/** Map a diagnosis to a proposed remediation. Default: pause. */
|
|
29
|
+
planRemediation?: (health: Health, agentId: AgentId) => Remediation;
|
|
30
|
+
}
|
|
31
|
+
/** What `observe` tells the caller: may the agent proceed, and why. */
|
|
32
|
+
export interface Observation {
|
|
33
|
+
allowed: boolean;
|
|
34
|
+
decision: Decision;
|
|
35
|
+
}
|
|
36
|
+
/** A per-agent handle onto the control plane. */
|
|
37
|
+
export interface WrappedAgent {
|
|
38
|
+
agentId: AgentId;
|
|
39
|
+
observe(event: ObservableEvent): Observation;
|
|
40
|
+
}
|
|
41
|
+
/** An event as reported by the agent (the plane stamps agentId and ts). */
|
|
42
|
+
export type ObservableEvent = Omit<AgentEvent, "agentId" | "ts"> & {
|
|
43
|
+
ts?: number;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* The control plane. Wraps a fleet of agents so that every decision is recorded
|
|
47
|
+
* (signal plane), diagnosed (reasoning), gated (confidence gate), and either
|
|
48
|
+
* auto-remediated or escalated (action layer), with a kill switch over all of it.
|
|
49
|
+
*/
|
|
50
|
+
export declare class ControlPlane {
|
|
51
|
+
private readonly cfg;
|
|
52
|
+
readonly signals: SignalPlane;
|
|
53
|
+
readonly killSwitch: KillSwitch;
|
|
54
|
+
private readonly gate;
|
|
55
|
+
private readonly now;
|
|
56
|
+
constructor(cfg: ControlPlaneConfig);
|
|
57
|
+
/** Get a handle for one agent in the fleet. */
|
|
58
|
+
wrap(agentId: AgentId): WrappedAgent;
|
|
59
|
+
private observe;
|
|
60
|
+
private diagnose;
|
|
61
|
+
private apply;
|
|
62
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { SignalPlane } from "./signal-plane.js";
|
|
2
|
+
import { KillSwitch } from "./kill-switch.js";
|
|
3
|
+
import { confidenceGate } from "./confidence-gate.js";
|
|
4
|
+
/**
|
|
5
|
+
* The control plane. Wraps a fleet of agents so that every decision is recorded
|
|
6
|
+
* (signal plane), diagnosed (reasoning), gated (confidence gate), and either
|
|
7
|
+
* auto-remediated or escalated (action layer), with a kill switch over all of it.
|
|
8
|
+
*/
|
|
9
|
+
export class ControlPlane {
|
|
10
|
+
cfg;
|
|
11
|
+
signals;
|
|
12
|
+
killSwitch;
|
|
13
|
+
gate;
|
|
14
|
+
now;
|
|
15
|
+
constructor(cfg) {
|
|
16
|
+
this.cfg = cfg;
|
|
17
|
+
this.signals = new SignalPlane({ sink: cfg.signalSink });
|
|
18
|
+
this.killSwitch = new KillSwitch();
|
|
19
|
+
this.gate = confidenceGate(cfg.gate);
|
|
20
|
+
this.now = cfg.now ?? (() => Date.now());
|
|
21
|
+
}
|
|
22
|
+
/** Get a handle for one agent in the fleet. */
|
|
23
|
+
wrap(agentId) {
|
|
24
|
+
return { agentId, observe: (event) => this.observe(agentId, event) };
|
|
25
|
+
}
|
|
26
|
+
observe(agentId, raw) {
|
|
27
|
+
// 1. Kill switch: a hard stop that beats everything else.
|
|
28
|
+
if (this.killSwitch.isTripped(agentId)) {
|
|
29
|
+
return { allowed: false, decision: { action: "blocked", reason: "kill switch tripped" } };
|
|
30
|
+
}
|
|
31
|
+
// 2. Signal plane: record the decision as one replayable event.
|
|
32
|
+
const event = {
|
|
33
|
+
agentId,
|
|
34
|
+
ts: raw.ts ?? this.now(),
|
|
35
|
+
type: raw.type,
|
|
36
|
+
ok: raw.ok,
|
|
37
|
+
tool: raw.tool,
|
|
38
|
+
tokens: raw.tokens,
|
|
39
|
+
detail: raw.detail,
|
|
40
|
+
};
|
|
41
|
+
this.signals.record(event);
|
|
42
|
+
// 3. Reasoning layer: is this agent actually broken?
|
|
43
|
+
const windowMs = this.cfg.windowMs ?? 60_000;
|
|
44
|
+
const window = this.signals.recent(agentId, windowMs, this.now());
|
|
45
|
+
const health = this.diagnose(window);
|
|
46
|
+
if (health.status === "healthy") {
|
|
47
|
+
return { allowed: true, decision: { action: "allow", reason: "healthy" } };
|
|
48
|
+
}
|
|
49
|
+
// 4. Confidence gate: act or escalate.
|
|
50
|
+
const remediation = (this.cfg.planRemediation ?? defaultPlan)(health, agentId);
|
|
51
|
+
const decision = this.gate(health, remediation);
|
|
52
|
+
// 5. Action layer.
|
|
53
|
+
if (decision.action === "auto_remediate" && decision.remediation) {
|
|
54
|
+
void this.apply(agentId, decision.remediation);
|
|
55
|
+
// a reroute lets the agent keep working; a pause/rollback stops this step
|
|
56
|
+
return { allowed: decision.remediation.kind === "reroute", decision };
|
|
57
|
+
}
|
|
58
|
+
if (decision.action === "escalate") {
|
|
59
|
+
// contain first, then hand the full replay to a human
|
|
60
|
+
void this.cfg.actions.pause(agentId, "containing before human review");
|
|
61
|
+
this.cfg.onEscalate({ agentId, health, decision, replay: this.signals.replay(agentId) });
|
|
62
|
+
return { allowed: false, decision };
|
|
63
|
+
}
|
|
64
|
+
return { allowed: true, decision };
|
|
65
|
+
}
|
|
66
|
+
diagnose(events) {
|
|
67
|
+
const findings = this.cfg.detectors
|
|
68
|
+
.map((d) => d(events))
|
|
69
|
+
.filter((f) => f !== null);
|
|
70
|
+
if (findings.length === 0)
|
|
71
|
+
return { status: "healthy", confidence: 1, reasons: [] };
|
|
72
|
+
const failing = findings.filter((f) => f.status === "failing");
|
|
73
|
+
const chosen = failing.length ? failing : findings;
|
|
74
|
+
return {
|
|
75
|
+
status: failing.length ? "failing" : "degraded",
|
|
76
|
+
confidence: Math.max(...chosen.map((f) => f.confidence)),
|
|
77
|
+
reasons: chosen.map((f) => f.reason),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
async apply(agentId, r) {
|
|
81
|
+
if (r.kind === "reroute")
|
|
82
|
+
await this.cfg.actions.reroute(agentId, r.detail);
|
|
83
|
+
else if (r.kind === "rollback")
|
|
84
|
+
await this.cfg.actions.rollback(agentId, r.detail);
|
|
85
|
+
else
|
|
86
|
+
await this.cfg.actions.pause(agentId, r.detail);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function defaultPlan() {
|
|
90
|
+
return { kind: "pause" };
|
|
91
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Detector } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Fires when an agent repeats the same tool call several times in a row. The
|
|
4
|
+
* classic stuck-in-a-loop failure. High confidence, because a loop is unambiguous.
|
|
5
|
+
*/
|
|
6
|
+
export declare function loopDetector(opts?: {
|
|
7
|
+
repeats?: number;
|
|
8
|
+
}): Detector;
|
|
9
|
+
/**
|
|
10
|
+
* Fires when token spend in the window exceeds a budget. Confidence scales with
|
|
11
|
+
* how far over budget the agent is.
|
|
12
|
+
*/
|
|
13
|
+
export declare function costRunawayDetector(opts: {
|
|
14
|
+
maxTokens: number;
|
|
15
|
+
}): Detector;
|
|
16
|
+
/** Fires when the recent failure rate crosses a threshold. */
|
|
17
|
+
export declare function errorRateDetector(opts?: {
|
|
18
|
+
threshold?: number;
|
|
19
|
+
min?: number;
|
|
20
|
+
}): Detector;
|
|
21
|
+
/**
|
|
22
|
+
* Fires on subtle quality drift: a run of low-confidence or low-quality model
|
|
23
|
+
* responses that no single hard threshold catches. Deliberately reports MODEST
|
|
24
|
+
* confidence, so the gate escalates to a human instead of acting on a guess.
|
|
25
|
+
* This is the "silent degradation" case that standard checks miss.
|
|
26
|
+
*/
|
|
27
|
+
export declare function driftDetector(opts?: {
|
|
28
|
+
min?: number;
|
|
29
|
+
qualityFloor?: number;
|
|
30
|
+
}): Detector;
|
package/dist/detector.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fires when an agent repeats the same tool call several times in a row. The
|
|
3
|
+
* classic stuck-in-a-loop failure. High confidence, because a loop is unambiguous.
|
|
4
|
+
*/
|
|
5
|
+
export function loopDetector(opts = {}) {
|
|
6
|
+
const limit = opts.repeats ?? 4;
|
|
7
|
+
return (events) => {
|
|
8
|
+
const calls = events.filter((e) => e.type === "tool_call");
|
|
9
|
+
if (calls.length < limit)
|
|
10
|
+
return null;
|
|
11
|
+
const tail = calls.slice(-limit);
|
|
12
|
+
const tool = tail[0]?.tool;
|
|
13
|
+
if (tool && tail.every((e) => e.tool === tool)) {
|
|
14
|
+
return {
|
|
15
|
+
status: "failing",
|
|
16
|
+
confidence: 0.9,
|
|
17
|
+
reason: `looping on "${tool}" (${limit}x in a row)`,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Fires when token spend in the window exceeds a budget. Confidence scales with
|
|
25
|
+
* how far over budget the agent is.
|
|
26
|
+
*/
|
|
27
|
+
export function costRunawayDetector(opts) {
|
|
28
|
+
return (events) => {
|
|
29
|
+
const spent = events.reduce((sum, e) => sum + (e.tokens ?? 0), 0);
|
|
30
|
+
if (spent <= opts.maxTokens)
|
|
31
|
+
return null;
|
|
32
|
+
const over = spent / opts.maxTokens;
|
|
33
|
+
return {
|
|
34
|
+
status: over > 2 ? "failing" : "degraded",
|
|
35
|
+
confidence: Math.min(0.95, 0.5 + over / 4),
|
|
36
|
+
reason: `token spend ${spent} over budget ${opts.maxTokens}`,
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** Fires when the recent failure rate crosses a threshold. */
|
|
41
|
+
export function errorRateDetector(opts = {}) {
|
|
42
|
+
const threshold = opts.threshold ?? 0.5;
|
|
43
|
+
const min = opts.min ?? 4;
|
|
44
|
+
return (events) => {
|
|
45
|
+
if (events.length < min)
|
|
46
|
+
return null;
|
|
47
|
+
const errors = events.filter((e) => !e.ok).length;
|
|
48
|
+
const rate = errors / events.length;
|
|
49
|
+
if (rate < threshold)
|
|
50
|
+
return null;
|
|
51
|
+
return {
|
|
52
|
+
status: rate >= 0.8 ? "failing" : "degraded",
|
|
53
|
+
confidence: Math.min(0.9, rate),
|
|
54
|
+
reason: `error rate ${(rate * 100).toFixed(0)}% over ${events.length} steps`,
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Fires on subtle quality drift: a run of low-confidence or low-quality model
|
|
60
|
+
* responses that no single hard threshold catches. Deliberately reports MODEST
|
|
61
|
+
* confidence, so the gate escalates to a human instead of acting on a guess.
|
|
62
|
+
* This is the "silent degradation" case that standard checks miss.
|
|
63
|
+
*/
|
|
64
|
+
export function driftDetector(opts = {}) {
|
|
65
|
+
const min = opts.min ?? 3;
|
|
66
|
+
const floor = opts.qualityFloor ?? 0.5;
|
|
67
|
+
return (events) => {
|
|
68
|
+
const responses = events.filter((e) => e.type === "model_response");
|
|
69
|
+
if (responses.length < min)
|
|
70
|
+
return null;
|
|
71
|
+
// treat a failed model_response as a quality miss
|
|
72
|
+
const misses = responses.filter((e) => !e.ok).length;
|
|
73
|
+
const missRate = misses / responses.length;
|
|
74
|
+
if (missRate < floor)
|
|
75
|
+
return null;
|
|
76
|
+
return {
|
|
77
|
+
status: "degraded",
|
|
78
|
+
confidence: 0.5, // ambiguous on purpose
|
|
79
|
+
reason: `output quality drifting (${misses}/${responses.length} weak responses)`,
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type { AgentId, AgentEvent, EventType, Detector, Finding, Health, HealthStatus, Decision, GateAction, Remediation, RemediationKind, } from "./types.js";
|
|
2
|
+
export { SignalPlane } from "./signal-plane.js";
|
|
3
|
+
export type { SignalSink } from "./signal-plane.js";
|
|
4
|
+
export { KillSwitch } from "./kill-switch.js";
|
|
5
|
+
export type { KillScope } from "./kill-switch.js";
|
|
6
|
+
export { confidenceGate } from "./confidence-gate.js";
|
|
7
|
+
export type { GatePolicy } from "./confidence-gate.js";
|
|
8
|
+
export { loopDetector, costRunawayDetector, errorRateDetector, driftDetector } from "./detector.js";
|
|
9
|
+
export type { Actions } from "./actions.js";
|
|
10
|
+
export { ControlPlane } from "./control-plane.js";
|
|
11
|
+
export type { ControlPlaneConfig, WrappedAgent, Observation, ObservableEvent, EscalationContext, } from "./control-plane.js";
|
|
12
|
+
import { ControlPlane, type ControlPlaneConfig } from "./control-plane.js";
|
|
13
|
+
/** Convenience factory. `const plane = controlPlane({ ... })`. */
|
|
14
|
+
export declare function controlPlane(cfg: ControlPlaneConfig): ControlPlane;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { SignalPlane } from "./signal-plane.js";
|
|
2
|
+
export { KillSwitch } from "./kill-switch.js";
|
|
3
|
+
export { confidenceGate } from "./confidence-gate.js";
|
|
4
|
+
export { loopDetector, costRunawayDetector, errorRateDetector, driftDetector } from "./detector.js";
|
|
5
|
+
export { ControlPlane } from "./control-plane.js";
|
|
6
|
+
import { ControlPlane } from "./control-plane.js";
|
|
7
|
+
/** Convenience factory. `const plane = controlPlane({ ... })`. */
|
|
8
|
+
export function controlPlane(cfg) {
|
|
9
|
+
return new ControlPlane(cfg);
|
|
10
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AgentId } from "./types.js";
|
|
2
|
+
export type KillScope = AgentId | "global";
|
|
3
|
+
/**
|
|
4
|
+
* A global and per-agent kill switch. Build the off switch before you need it,
|
|
5
|
+
* not during the outage. When tripped, the control plane blocks the agent's
|
|
6
|
+
* next action outright, no deploy required.
|
|
7
|
+
*/
|
|
8
|
+
export declare class KillSwitch {
|
|
9
|
+
private globalTripped;
|
|
10
|
+
private readonly tripped;
|
|
11
|
+
private readonly listeners;
|
|
12
|
+
/** Trip the switch. Default scope is the whole fleet. */
|
|
13
|
+
trip(scope?: KillScope): void;
|
|
14
|
+
/** Reset the switch. Default scope is the whole fleet. */
|
|
15
|
+
reset(scope?: KillScope): void;
|
|
16
|
+
/** Is this agent (or the whole fleet) currently stopped. */
|
|
17
|
+
isTripped(agentId?: AgentId): boolean;
|
|
18
|
+
/** Notified whenever the switch is tripped. */
|
|
19
|
+
onTrip(fn: (scope: KillScope) => void): void;
|
|
20
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A global and per-agent kill switch. Build the off switch before you need it,
|
|
3
|
+
* not during the outage. When tripped, the control plane blocks the agent's
|
|
4
|
+
* next action outright, no deploy required.
|
|
5
|
+
*/
|
|
6
|
+
export class KillSwitch {
|
|
7
|
+
globalTripped = false;
|
|
8
|
+
tripped = new Set();
|
|
9
|
+
listeners = [];
|
|
10
|
+
/** Trip the switch. Default scope is the whole fleet. */
|
|
11
|
+
trip(scope = "global") {
|
|
12
|
+
if (scope === "global")
|
|
13
|
+
this.globalTripped = true;
|
|
14
|
+
else
|
|
15
|
+
this.tripped.add(scope);
|
|
16
|
+
for (const l of this.listeners)
|
|
17
|
+
l(scope);
|
|
18
|
+
}
|
|
19
|
+
/** Reset the switch. Default scope is the whole fleet. */
|
|
20
|
+
reset(scope = "global") {
|
|
21
|
+
if (scope === "global")
|
|
22
|
+
this.globalTripped = false;
|
|
23
|
+
else
|
|
24
|
+
this.tripped.delete(scope);
|
|
25
|
+
}
|
|
26
|
+
/** Is this agent (or the whole fleet) currently stopped. */
|
|
27
|
+
isTripped(agentId) {
|
|
28
|
+
if (this.globalTripped)
|
|
29
|
+
return true;
|
|
30
|
+
return agentId ? this.tripped.has(agentId) : false;
|
|
31
|
+
}
|
|
32
|
+
/** Notified whenever the switch is tripped. */
|
|
33
|
+
onTrip(fn) {
|
|
34
|
+
this.listeners.push(fn);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { AgentEvent, AgentId } from "./types.js";
|
|
2
|
+
/** Somewhere to durably ship events (a DB, a log pipeline, a queue). */
|
|
3
|
+
export interface SignalSink {
|
|
4
|
+
write(event: AgentEvent): void;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The signal plane. Records one replayable event per agent decision and lets you
|
|
8
|
+
* replay an agent's full history or query a recent window. In-memory ring buffer
|
|
9
|
+
* by default; hand it a sink to also ship events somewhere durable.
|
|
10
|
+
*/
|
|
11
|
+
export declare class SignalPlane {
|
|
12
|
+
private events;
|
|
13
|
+
private readonly capacity;
|
|
14
|
+
private readonly sink?;
|
|
15
|
+
constructor(opts?: {
|
|
16
|
+
capacity?: number;
|
|
17
|
+
sink?: SignalSink;
|
|
18
|
+
});
|
|
19
|
+
/** Record a decision. Trims the oldest event once capacity is reached. */
|
|
20
|
+
record(event: AgentEvent): void;
|
|
21
|
+
/** Every recorded decision for one agent, oldest first. */
|
|
22
|
+
replay(agentId: AgentId): AgentEvent[];
|
|
23
|
+
/** Events for one agent within the last `windowMs` relative to `now`. */
|
|
24
|
+
recent(agentId: AgentId, windowMs: number, now: number): AgentEvent[];
|
|
25
|
+
/** All events across every agent (read-only). */
|
|
26
|
+
all(): readonly AgentEvent[];
|
|
27
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The signal plane. Records one replayable event per agent decision and lets you
|
|
3
|
+
* replay an agent's full history or query a recent window. In-memory ring buffer
|
|
4
|
+
* by default; hand it a sink to also ship events somewhere durable.
|
|
5
|
+
*/
|
|
6
|
+
export class SignalPlane {
|
|
7
|
+
events = [];
|
|
8
|
+
capacity;
|
|
9
|
+
sink;
|
|
10
|
+
constructor(opts = {}) {
|
|
11
|
+
this.capacity = opts.capacity ?? 10_000;
|
|
12
|
+
this.sink = opts.sink;
|
|
13
|
+
}
|
|
14
|
+
/** Record a decision. Trims the oldest event once capacity is reached. */
|
|
15
|
+
record(event) {
|
|
16
|
+
this.events.push(event);
|
|
17
|
+
if (this.events.length > this.capacity)
|
|
18
|
+
this.events.shift();
|
|
19
|
+
this.sink?.write(event);
|
|
20
|
+
}
|
|
21
|
+
/** Every recorded decision for one agent, oldest first. */
|
|
22
|
+
replay(agentId) {
|
|
23
|
+
return this.events.filter((e) => e.agentId === agentId);
|
|
24
|
+
}
|
|
25
|
+
/** Events for one agent within the last `windowMs` relative to `now`. */
|
|
26
|
+
recent(agentId, windowMs, now) {
|
|
27
|
+
return this.events.filter((e) => e.agentId === agentId && now - e.ts <= windowMs);
|
|
28
|
+
}
|
|
29
|
+
/** All events across every agent (read-only). */
|
|
30
|
+
all() {
|
|
31
|
+
return this.events;
|
|
32
|
+
}
|
|
33
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export type AgentId = string;
|
|
2
|
+
export type EventType = "tool_call" | "model_response" | "error";
|
|
3
|
+
/**
|
|
4
|
+
* One replayable record per decision an agent makes. This is the atom of the
|
|
5
|
+
* signal plane: without it, every layer above is guessing.
|
|
6
|
+
*/
|
|
7
|
+
export interface AgentEvent {
|
|
8
|
+
agentId: AgentId;
|
|
9
|
+
ts: number;
|
|
10
|
+
type: EventType;
|
|
11
|
+
ok: boolean;
|
|
12
|
+
tool?: string;
|
|
13
|
+
tokens?: number;
|
|
14
|
+
detail?: string;
|
|
15
|
+
}
|
|
16
|
+
export type HealthStatus = "healthy" | "degraded" | "failing";
|
|
17
|
+
/** A single detector's opinion about an agent's recent behavior. */
|
|
18
|
+
export interface Finding {
|
|
19
|
+
status: HealthStatus;
|
|
20
|
+
confidence: number;
|
|
21
|
+
reason: string;
|
|
22
|
+
}
|
|
23
|
+
/** The reasoning layer's combined diagnosis for one agent. */
|
|
24
|
+
export interface Health {
|
|
25
|
+
status: HealthStatus;
|
|
26
|
+
confidence: number;
|
|
27
|
+
reasons: string[];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A detector inspects an agent's recent events and returns a Finding, or null
|
|
31
|
+
* if nothing looks wrong to it.
|
|
32
|
+
*/
|
|
33
|
+
export type Detector = (events: AgentEvent[]) => Finding | null;
|
|
34
|
+
export type RemediationKind = "reroute" | "rollback" | "pause";
|
|
35
|
+
export interface Remediation {
|
|
36
|
+
kind: RemediationKind;
|
|
37
|
+
detail?: string;
|
|
38
|
+
}
|
|
39
|
+
export type GateAction = "allow" | "auto_remediate" | "escalate" | "blocked";
|
|
40
|
+
/** The confidence gate's ruling on what to do about a diagnosis. */
|
|
41
|
+
export interface Decision {
|
|
42
|
+
action: GateAction;
|
|
43
|
+
reason: string;
|
|
44
|
+
remediation?: Remediation;
|
|
45
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "guardplane",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A minimal, framework-agnostic control plane for AI agent fleets: signal plane, confidence gate, and kill switch.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Mohamed Kadri",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"ai-agents",
|
|
10
|
+
"agentops",
|
|
11
|
+
"observability",
|
|
12
|
+
"sre",
|
|
13
|
+
"llm",
|
|
14
|
+
"kill-switch",
|
|
15
|
+
"control-plane",
|
|
16
|
+
"autonomous-agents"
|
|
17
|
+
],
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/mkadri85/guardplane.git"
|
|
21
|
+
},
|
|
22
|
+
"main": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"README.md",
|
|
33
|
+
"LICENSE"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc",
|
|
37
|
+
"prepublishOnly": "npm run build",
|
|
38
|
+
"demo": "tsx examples/demo.ts",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"typecheck": "tsc --noEmit"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"tsx": "^4.19.2",
|
|
44
|
+
"typescript": "^5.7.2",
|
|
45
|
+
"vitest": "^2.1.8"
|
|
46
|
+
}
|
|
47
|
+
}
|