@alveus-ai/std 0.1.55 → 0.1.56
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/dist/agents/action.d.ts +102 -0
- package/dist/agents/action.d.ts.map +1 -0
- package/dist/agents/action.js +319 -0
- package/dist/agents/action.js.map +1 -0
- package/dist/agents/basic-chat.js.map +1 -1
- package/dist/agents/index.d.ts +1 -0
- package/dist/agents/index.d.ts.map +1 -1
- package/dist/agents/index.js +5 -0
- package/dist/agents/index.js.map +1 -1
- package/dist/blobs/730dfa7dad60e3808e32bd889b0b36d9ce9e6b40376fecbc91fac604c588fb55.js +210 -0
- package/dist/composition/errors.d.ts.map +1 -1
- package/dist/memory/history.js.map +1 -1
- package/dist/patterns/approval.d.ts +2 -2
- package/dist/patterns/approval.d.ts.map +1 -1
- package/dist/patterns/approval.js.map +1 -1
- package/examples/action-gate.ts +99 -0
- package/package.json +4 -4
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Action Gate — proof-gated side effects.
|
|
3
|
+
*
|
|
4
|
+
* An action gate takes a proposed consequential action (`{ action, input }`),
|
|
5
|
+
* freezes it, scores it against pinned **scorers**, and executes it — with the
|
|
6
|
+
* EXACT frozen payload — only when every required criterion passes. Otherwise it
|
|
7
|
+
* denies, or (when a revise agent is configured) revises the payload once and
|
|
8
|
+
* re-scores, up to a bound.
|
|
9
|
+
*
|
|
10
|
+
* The authoritative record of a gated action is the platform-served Action
|
|
11
|
+
* Receipt: which pinned scorer measured which payload, the decision, and proof
|
|
12
|
+
* that the executed payload was byte-for-byte the one that was scored — not this
|
|
13
|
+
* agent's own return value (which an agent could fake).
|
|
14
|
+
*
|
|
15
|
+
* Where a goal agent attests an OUTCOME, an action gate attests an ACTION. It
|
|
16
|
+
* reuses the same scoring substrate (scorers, `ScorerInput`/`ScorerOutput`,
|
|
17
|
+
* thresholds) as the goal controller.
|
|
18
|
+
*/
|
|
19
|
+
import type { ExecutionContext, ActionGateInput, ActionGateResult, VirtualAgent } from '@alveus-ai/core';
|
|
20
|
+
import type { NormalizedCriterion, GoalCriterionSpec } from './goal.js';
|
|
21
|
+
/**
|
|
22
|
+
* Internal input to the action-gate controller: the pinned contract (criteria
|
|
23
|
+
* with scorers, the execute agent, bounds) merged with the runtime proposal.
|
|
24
|
+
*/
|
|
25
|
+
export interface ActionGateLogicInput extends ActionGateInput {
|
|
26
|
+
criteria: NormalizedCriterion[];
|
|
27
|
+
/** Pinned code hash of the agent that performs the side effect. */
|
|
28
|
+
executeHash: string;
|
|
29
|
+
/** Optional pinned agent that revises a failed payload from feedback. */
|
|
30
|
+
reviseHash?: string;
|
|
31
|
+
/** Configuration for the revise agent instance. */
|
|
32
|
+
reviseConfig?: Record<string, unknown>;
|
|
33
|
+
/** Maximum bounded revisions (0 = evaluate once, then allow or deny). */
|
|
34
|
+
maxRevisions: number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The precompiled action-gate controller. Configured (not authored) by
|
|
38
|
+
* `actionGate`.
|
|
39
|
+
*
|
|
40
|
+
* It validates its own contract (so it cannot be tricked into executing for an
|
|
41
|
+
* empty or malformed criteria set, even when invoked directly), freezes the
|
|
42
|
+
* proposed payload, scores every criterion against that frozen payload via its
|
|
43
|
+
* pinned scorer, and executes the side effect with the **exact same frozen
|
|
44
|
+
* payload** only when all required criteria pass. On failure it denies, or
|
|
45
|
+
* revises once (when a revise agent is configured) and re-scores, up to the
|
|
46
|
+
* bound. It never self-attests — the platform authors the receipt.
|
|
47
|
+
*/
|
|
48
|
+
export declare const ActionGateLogic: import("@alveus-ai/core").Agent<{}, ActionGateLogicInput, ExecutionContext, any>;
|
|
49
|
+
/** An agent reference usable as a scorer, execute, or revise agent. */
|
|
50
|
+
type AgentRef = {
|
|
51
|
+
definition: {
|
|
52
|
+
hash: string;
|
|
53
|
+
};
|
|
54
|
+
} | VirtualAgent | string;
|
|
55
|
+
/** A gating criterion with an optional per-criterion scorer and reference. */
|
|
56
|
+
export type ActionCriterionSpec = GoalCriterionSpec;
|
|
57
|
+
/** Options for {@link actionGate}. */
|
|
58
|
+
export interface ActionGateOptions {
|
|
59
|
+
/** At least one uniquely-identified gating criterion. */
|
|
60
|
+
criteria: ActionCriterionSpec[];
|
|
61
|
+
/** Default scorer for criteria that do not name their own. */
|
|
62
|
+
scorer?: AgentRef;
|
|
63
|
+
/** Default scorer configuration. */
|
|
64
|
+
scorerConfig?: Record<string, unknown>;
|
|
65
|
+
/**
|
|
66
|
+
* The agent that performs the side effect. It receives EXACTLY the frozen
|
|
67
|
+
* proposal `{ action, input }` — nothing is merged in — so the executed
|
|
68
|
+
* payload is provably the payload that was scored. It must therefore carry no
|
|
69
|
+
* `defaultInput`/config of its own.
|
|
70
|
+
*/
|
|
71
|
+
execute: AgentRef;
|
|
72
|
+
/** Optional agent that revises a failed payload from unmet-criterion feedback. */
|
|
73
|
+
revise?: AgentRef;
|
|
74
|
+
/** Maximum bounded revisions before deciding. Defaults to 0 (no revise). */
|
|
75
|
+
maxRevisions?: number;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Create an action gate: a proof-gated wrapper around a consequential side
|
|
79
|
+
* effect. The proposal is supplied at run time; criteria, scorers, the execute
|
|
80
|
+
* agent, and bounds are fixed here. Every terminal run yields a platform-served
|
|
81
|
+
* Action Receipt (the authoritative outcome).
|
|
82
|
+
*
|
|
83
|
+
* Deploy it by composing it inside an exported agent, so the platform can
|
|
84
|
+
* recognize and attest the gated action:
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* const justified = agent$(async (_s, { subject }, ctx) => {
|
|
89
|
+
* const { input } = subject as { input: { amount: number } };
|
|
90
|
+
* return { results: [{ criterionId: 'amount_ok', verdict: input.amount <= 500, method: 'deterministic' }] };
|
|
91
|
+
* });
|
|
92
|
+
* const refundAgent = agent$(async (_s, { input }, ctx) => ctx.fetch(...));
|
|
93
|
+
*
|
|
94
|
+
* const gate = actionGate({ criteria: [{ id: 'amount_ok' }], scorer: justified, execute: refundAgent });
|
|
95
|
+
* export const refund = agent$((_s, input, ctx) => ctx.call(gate, input));
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export declare function actionGate(options: ActionGateOptions): VirtualAgent<ActionGateInput, ActionGateResult>;
|
|
99
|
+
/** Alias for {@link actionGate}. */
|
|
100
|
+
export declare const createActionAgent: typeof actionGate;
|
|
101
|
+
export {};
|
|
102
|
+
//# sourceMappingURL=action.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"action.d.ts","sourceRoot":"","sources":["../../src/agents/action.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,KAAK,EACV,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAKhB,YAAY,EACb,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAIxE;;;GAGG;AACH,MAAM,WAAW,oBAAqB,SAAQ,eAAe;IAC3D,QAAQ,EAAE,mBAAmB,EAAE,CAAC;IAChC,mEAAmE;IACnE,WAAW,EAAE,MAAM,CAAC;IACpB,yEAAyE;IACzE,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,mDAAmD;IACnD,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACvC,yEAAyE;IACzE,YAAY,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,eAAe,kFA4O3B,CAAC;AAMF,uEAAuE;AACvE,KAAK,QAAQ,GAAG;IAAE,UAAU,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,GAAG,YAAY,GAAG,MAAM,CAAC;AAoBzE,8EAA8E;AAC9E,MAAM,MAAM,mBAAmB,GAAG,iBAAiB,CAAC;AAEpD,sCAAsC;AACtC,MAAM,WAAW,iBAAiB;IAChC,yDAAyD;IACzD,QAAQ,EAAE,mBAAmB,EAAE,CAAC;IAChC,8DAA8D;IAC9D,MAAM,CAAC,EAAE,QAAQ,CAAC;IAClB,oCAAoC;IACpC,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACvC;;;;;OAKG;IACH,OAAO,EAAE,QAAQ,CAAC;IAClB,kFAAkF;IAClF,MAAM,CAAC,EAAE,QAAQ,CAAC;IAClB,4EAA4E;IAC5E,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,UAAU,CACxB,OAAO,EAAE,iBAAiB,GACzB,YAAY,CAAC,eAAe,EAAE,gBAAgB,CAAC,CA2DjD;AAED,oCAAoC;AACpC,eAAO,MAAM,iBAAiB,mBAAa,CAAC"}
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Action Gate — proof-gated side effects.
|
|
3
|
+
*
|
|
4
|
+
* An action gate takes a proposed consequential action (`{ action, input }`),
|
|
5
|
+
* freezes it, scores it against pinned **scorers**, and executes it — with the
|
|
6
|
+
* EXACT frozen payload — only when every required criterion passes. Otherwise it
|
|
7
|
+
* denies, or (when a revise agent is configured) revises the payload once and
|
|
8
|
+
* re-scores, up to a bound.
|
|
9
|
+
*
|
|
10
|
+
* The authoritative record of a gated action is the platform-served Action
|
|
11
|
+
* Receipt: which pinned scorer measured which payload, the decision, and proof
|
|
12
|
+
* that the executed payload was byte-for-byte the one that was scored — not this
|
|
13
|
+
* agent's own return value (which an agent could fake).
|
|
14
|
+
*
|
|
15
|
+
* Where a goal agent attests an OUTCOME, an action gate attests an ACTION. It
|
|
16
|
+
* reuses the same scoring substrate (scorers, `ScorerInput`/`ScorerOutput`,
|
|
17
|
+
* thresholds) as the goal controller.
|
|
18
|
+
*/
|
|
19
|
+
import { agent$ } from '@alveus-ai/core';
|
|
20
|
+
const METHODS = new Set(['deterministic', 'model_assessed', 'opaque']);
|
|
21
|
+
/**
|
|
22
|
+
* The precompiled action-gate controller. Configured (not authored) by
|
|
23
|
+
* `actionGate`.
|
|
24
|
+
*
|
|
25
|
+
* It validates its own contract (so it cannot be tricked into executing for an
|
|
26
|
+
* empty or malformed criteria set, even when invoked directly), freezes the
|
|
27
|
+
* proposed payload, scores every criterion against that frozen payload via its
|
|
28
|
+
* pinned scorer, and executes the side effect with the **exact same frozen
|
|
29
|
+
* payload** only when all required criteria pass. On failure it denies, or
|
|
30
|
+
* revises once (when a revise agent is configured) and re-scores, up to the
|
|
31
|
+
* bound. It never self-attests — the platform authors the receipt.
|
|
32
|
+
*/
|
|
33
|
+
export const ActionGateLogic = agent$(async (_state, event, ctx) => {
|
|
34
|
+
const { action, criteria, executeHash, reviseHash, reviseConfig } = event;
|
|
35
|
+
const maxRevisions = event.maxRevisions;
|
|
36
|
+
// ===== CONTRACT VALIDATION (trust boundary) =====
|
|
37
|
+
// The controller is publicly callable by hash, so it must independently
|
|
38
|
+
// reject an invalid contract rather than trust the factory. An invalid
|
|
39
|
+
// contract is an execution failure, never a silent allow.
|
|
40
|
+
if (typeof action !== 'string' || action.length === 0) {
|
|
41
|
+
throw new Error('Action contract invalid: action must be a non-empty string');
|
|
42
|
+
}
|
|
43
|
+
if (!Array.isArray(criteria) || criteria.length === 0) {
|
|
44
|
+
throw new Error('Action contract invalid: at least one criterion is required');
|
|
45
|
+
}
|
|
46
|
+
if (typeof executeHash !== 'string' || executeHash.length === 0) {
|
|
47
|
+
throw new Error('Action contract invalid: an execute agent is required');
|
|
48
|
+
}
|
|
49
|
+
if (!Number.isInteger(maxRevisions) || maxRevisions < 0) {
|
|
50
|
+
throw new Error('Action contract invalid: maxRevisions must be a non-negative integer');
|
|
51
|
+
}
|
|
52
|
+
const seenIds = new Set();
|
|
53
|
+
for (const c of criteria) {
|
|
54
|
+
if (!c || typeof c.id !== 'string' || c.id.length === 0) {
|
|
55
|
+
throw new Error('Action contract invalid: every criterion needs a non-empty id');
|
|
56
|
+
}
|
|
57
|
+
if (seenIds.has(c.id)) {
|
|
58
|
+
throw new Error(`Action contract invalid: duplicate criterion id "${c.id}"`);
|
|
59
|
+
}
|
|
60
|
+
seenIds.add(c.id);
|
|
61
|
+
if (typeof c.scorerHash !== 'string' || c.scorerHash.length === 0) {
|
|
62
|
+
throw new Error(`Action contract invalid: criterion "${c.id}" has no pinned scorer`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// ===== HELPERS (mirror the goal controller's strict scoring) =====
|
|
66
|
+
const passes = (threshold, r, id) => {
|
|
67
|
+
const t = threshold ?? { kind: 'verdict' };
|
|
68
|
+
if (t.kind === 'verdict') {
|
|
69
|
+
if (typeof r.verdict !== 'boolean') {
|
|
70
|
+
throw new Error(`Scorer did not return a boolean verdict for "${id}"`);
|
|
71
|
+
}
|
|
72
|
+
return r.verdict === true;
|
|
73
|
+
}
|
|
74
|
+
if (t.kind === 'minScore') {
|
|
75
|
+
if (typeof r.score !== 'number' || !Number.isFinite(r.score)) {
|
|
76
|
+
throw new Error(`Scorer did not return a numeric score for "${id}"`);
|
|
77
|
+
}
|
|
78
|
+
return r.score >= t.min;
|
|
79
|
+
}
|
|
80
|
+
throw new Error(`Unknown threshold kind for "${id}"`);
|
|
81
|
+
};
|
|
82
|
+
const parseScores = (raw, expected) => {
|
|
83
|
+
if (!raw ||
|
|
84
|
+
typeof raw !== 'object' ||
|
|
85
|
+
!Array.isArray(raw.results)) {
|
|
86
|
+
throw new Error('Scorer output invalid: missing results array');
|
|
87
|
+
}
|
|
88
|
+
const expectedSet = new Set(expected);
|
|
89
|
+
const byId = new Map();
|
|
90
|
+
for (const entry of raw.results) {
|
|
91
|
+
if (!entry || typeof entry !== 'object') {
|
|
92
|
+
throw new Error('Scorer output invalid: non-object result entry');
|
|
93
|
+
}
|
|
94
|
+
const { criterionId, method, verdict, score, label, reasoning, metadata, ...extra } = entry;
|
|
95
|
+
if (typeof criterionId !== 'string') {
|
|
96
|
+
throw new Error('Scorer output invalid: result missing string criterionId');
|
|
97
|
+
}
|
|
98
|
+
if (!expectedSet.has(criterionId)) {
|
|
99
|
+
throw new Error(`Scorer output invalid: unexpected criterionId "${criterionId}"`);
|
|
100
|
+
}
|
|
101
|
+
if (byId.has(criterionId)) {
|
|
102
|
+
throw new Error(`Scorer output invalid: duplicate result for "${criterionId}"`);
|
|
103
|
+
}
|
|
104
|
+
const extraKeys = Object.keys(extra);
|
|
105
|
+
if (extraKeys.length > 0) {
|
|
106
|
+
throw new Error(`Scorer output invalid: unknown field(s) for "${criterionId}": ${extraKeys.join(', ')}`);
|
|
107
|
+
}
|
|
108
|
+
if (verdict !== undefined && typeof verdict !== 'boolean') {
|
|
109
|
+
throw new Error(`Scorer output invalid: "${criterionId}" verdict must be boolean`);
|
|
110
|
+
}
|
|
111
|
+
if (score !== undefined && (typeof score !== 'number' || !Number.isFinite(score))) {
|
|
112
|
+
throw new Error(`Scorer output invalid: "${criterionId}" score must be a finite number`);
|
|
113
|
+
}
|
|
114
|
+
if (label !== undefined && typeof label !== 'string') {
|
|
115
|
+
throw new Error(`Scorer output invalid: "${criterionId}" label must be a string`);
|
|
116
|
+
}
|
|
117
|
+
if (reasoning !== undefined && typeof reasoning !== 'string') {
|
|
118
|
+
throw new Error(`Scorer output invalid: "${criterionId}" reasoning must be a string`);
|
|
119
|
+
}
|
|
120
|
+
if (method !== undefined && !METHODS.has(method)) {
|
|
121
|
+
throw new Error(`Scorer output invalid: "${criterionId}" has an unknown method`);
|
|
122
|
+
}
|
|
123
|
+
byId.set(criterionId, {
|
|
124
|
+
result: { verdict, score, label, reasoning, metadata },
|
|
125
|
+
method: method ?? 'opaque',
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
for (const id of expected) {
|
|
129
|
+
if (!byId.has(id)) {
|
|
130
|
+
throw new Error(`Scorer output invalid: missing result for "${id}"`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return byId;
|
|
134
|
+
};
|
|
135
|
+
const buildGroups = () => {
|
|
136
|
+
const groups = new Map();
|
|
137
|
+
for (const c of criteria) {
|
|
138
|
+
const key = `${c.scorerHash} ${JSON.stringify(c.scorerConfig ?? null)} ${JSON.stringify(c.reference ?? null)}`;
|
|
139
|
+
let g = groups.get(key);
|
|
140
|
+
if (!g) {
|
|
141
|
+
g = {
|
|
142
|
+
scorerHash: c.scorerHash,
|
|
143
|
+
scorerConfig: c.scorerConfig,
|
|
144
|
+
reference: c.reference,
|
|
145
|
+
ids: [],
|
|
146
|
+
};
|
|
147
|
+
groups.set(key, g);
|
|
148
|
+
}
|
|
149
|
+
g.ids.push(c.id);
|
|
150
|
+
}
|
|
151
|
+
return [...groups.values()];
|
|
152
|
+
};
|
|
153
|
+
const groups = buildGroups();
|
|
154
|
+
// Score one frozen payload against every criterion. Each scorer group is one
|
|
155
|
+
// durable step whose recorded input carries the payload as `subject`.
|
|
156
|
+
const scorePayload = async (payload) => {
|
|
157
|
+
const results = new Map();
|
|
158
|
+
for (const g of groups) {
|
|
159
|
+
const scorerInput = {
|
|
160
|
+
subject: payload,
|
|
161
|
+
criteria: g.ids.map((criterionId) => ({ criterionId })),
|
|
162
|
+
reference: g.reference,
|
|
163
|
+
config: g.scorerConfig,
|
|
164
|
+
};
|
|
165
|
+
const raw = await ctx.call(g.scorerHash, scorerInput, {
|
|
166
|
+
label: `check: ${g.ids.join(', ')}`,
|
|
167
|
+
});
|
|
168
|
+
const parsed = parseScores(raw, g.ids);
|
|
169
|
+
for (const [id, v] of parsed)
|
|
170
|
+
results.set(id, v);
|
|
171
|
+
}
|
|
172
|
+
return criteria.map((c) => {
|
|
173
|
+
const scored = results.get(c.id);
|
|
174
|
+
return {
|
|
175
|
+
id: c.id,
|
|
176
|
+
passed: passes(c.threshold, scored.result, c.id),
|
|
177
|
+
reasoning: scored.result.reasoning,
|
|
178
|
+
};
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
ctx.logger.info('Action gate started', {
|
|
182
|
+
action,
|
|
183
|
+
criteria: criteria.map((c) => c.id),
|
|
184
|
+
scorerCount: groups.length,
|
|
185
|
+
maxRevisions,
|
|
186
|
+
});
|
|
187
|
+
// ===== EVALUATE → (revise) → decide =====
|
|
188
|
+
// The frozen payload is the caller's proposal { action, input }. The SAME
|
|
189
|
+
// object is scored and, on allow, executed — no config is merged into either
|
|
190
|
+
// spawn, so the scored payload is byte-for-byte the executed payload.
|
|
191
|
+
let payload = { action, input: event.input };
|
|
192
|
+
let revisions = 0;
|
|
193
|
+
for (;;) {
|
|
194
|
+
await ctx.setState(`action.round.${revisions}.payload`, payload);
|
|
195
|
+
const evaluated = await scorePayload(payload);
|
|
196
|
+
const unmet = evaluated.filter((e) => !e.passed);
|
|
197
|
+
ctx.logger.info('Action evaluated', {
|
|
198
|
+
revisions,
|
|
199
|
+
passed: evaluated.filter((e) => e.passed).map((e) => e.id),
|
|
200
|
+
unmet: unmet.map((e) => e.id),
|
|
201
|
+
});
|
|
202
|
+
if (unmet.length === 0) {
|
|
203
|
+
// ALLOW: execute the EXACT frozen payload (the same object that was scored).
|
|
204
|
+
await ctx.call(executeHash, payload, { label: action });
|
|
205
|
+
ctx.logger.info('Action allowed and executed', { action, revisions });
|
|
206
|
+
return { decision: 'ALLOWED', revisions, executed: true };
|
|
207
|
+
}
|
|
208
|
+
// Revise once more only if a revise agent is configured and budget remains.
|
|
209
|
+
if (reviseHash && revisions < maxRevisions) {
|
|
210
|
+
const feedback = unmet.map((e) => `- Criterion "${e.id}" not met.${e.reasoning ? ` ${e.reasoning}` : ''}`);
|
|
211
|
+
const revised = await ctx.call(reviseHash, { ...(reviseConfig ?? {}), action, input: payload.input, feedback }, { label: `revise: ${action}` });
|
|
212
|
+
// The revise agent returns the new input payload (bare, or wrapped in `{ input }`).
|
|
213
|
+
const newInput = revised && typeof revised === 'object' && 'input' in revised
|
|
214
|
+
? revised.input
|
|
215
|
+
: revised;
|
|
216
|
+
payload = { action, input: newInput };
|
|
217
|
+
revisions++;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
// DENY: the side effect never runs.
|
|
221
|
+
ctx.logger.info('Action denied', {
|
|
222
|
+
action,
|
|
223
|
+
revisions,
|
|
224
|
+
unmet: unmet.map((e) => e.id),
|
|
225
|
+
});
|
|
226
|
+
return { decision: 'DENIED', revisions, executed: false };
|
|
227
|
+
}
|
|
228
|
+
}, { name: 'ActionGateLogic', hash: "730dfa7dad60e3808e32bd889b0b36d9ce9e6b40376fecbc91fac604c588fb55" });
|
|
229
|
+
/** Resolve an agent reference to its pinned code hash and (for virtual agents) config. */
|
|
230
|
+
function resolveRef(ref) {
|
|
231
|
+
if (typeof ref === 'string')
|
|
232
|
+
return { hash: ref };
|
|
233
|
+
const anyRef = ref;
|
|
234
|
+
if (anyRef.definition?.definition?.hash) {
|
|
235
|
+
return { hash: anyRef.definition.definition.hash, config: anyRef.defaultInput };
|
|
236
|
+
}
|
|
237
|
+
if (anyRef.definition?.hash)
|
|
238
|
+
return { hash: anyRef.definition.hash };
|
|
239
|
+
throw new Error('actionGate: could not resolve scorer/execute/revise agent hash');
|
|
240
|
+
}
|
|
241
|
+
function mergeConfig(...parts) {
|
|
242
|
+
const merged = Object.assign({}, ...parts.filter(Boolean));
|
|
243
|
+
return Object.keys(merged).length > 0 ? merged : undefined;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Create an action gate: a proof-gated wrapper around a consequential side
|
|
247
|
+
* effect. The proposal is supplied at run time; criteria, scorers, the execute
|
|
248
|
+
* agent, and bounds are fixed here. Every terminal run yields a platform-served
|
|
249
|
+
* Action Receipt (the authoritative outcome).
|
|
250
|
+
*
|
|
251
|
+
* Deploy it by composing it inside an exported agent, so the platform can
|
|
252
|
+
* recognize and attest the gated action:
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```ts
|
|
256
|
+
* const justified = agent$(async (_s, { subject }, ctx) => {
|
|
257
|
+
* const { input } = subject as { input: { amount: number } };
|
|
258
|
+
* return { results: [{ criterionId: 'amount_ok', verdict: input.amount <= 500, method: 'deterministic' }] };
|
|
259
|
+
* });
|
|
260
|
+
* const refundAgent = agent$(async (_s, { input }, ctx) => ctx.fetch(...));
|
|
261
|
+
*
|
|
262
|
+
* const gate = actionGate({ criteria: [{ id: 'amount_ok' }], scorer: justified, execute: refundAgent });
|
|
263
|
+
* export const refund = agent$((_s, input, ctx) => ctx.call(gate, input));
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
266
|
+
export function actionGate(options) {
|
|
267
|
+
const { criteria, scorer, scorerConfig, execute, revise, maxRevisions } = options;
|
|
268
|
+
if (!criteria || criteria.length === 0) {
|
|
269
|
+
throw new Error('actionGate: at least one criterion is required');
|
|
270
|
+
}
|
|
271
|
+
if (!execute) {
|
|
272
|
+
throw new Error('actionGate: an execute agent is required');
|
|
273
|
+
}
|
|
274
|
+
const defaultScorer = scorer ? resolveRef(scorer) : undefined;
|
|
275
|
+
const ids = new Set();
|
|
276
|
+
const normalized = criteria.map((c) => {
|
|
277
|
+
if (!c.id)
|
|
278
|
+
throw new Error('actionGate: every criterion needs an id');
|
|
279
|
+
if (ids.has(c.id))
|
|
280
|
+
throw new Error(`actionGate: duplicate criterion id "${c.id}"`);
|
|
281
|
+
ids.add(c.id);
|
|
282
|
+
const resolved = c.scorer ? resolveRef(c.scorer) : defaultScorer;
|
|
283
|
+
if (!resolved) {
|
|
284
|
+
throw new Error(`actionGate: criterion "${c.id}" has no scorer (set a default \`scorer\` or a per-criterion \`scorer\`)`);
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
id: c.id,
|
|
288
|
+
threshold: c.threshold,
|
|
289
|
+
scorerHash: resolved.hash,
|
|
290
|
+
scorerConfig: mergeConfig(resolved.config, scorerConfig, c.scorerConfig),
|
|
291
|
+
reference: c.reference,
|
|
292
|
+
};
|
|
293
|
+
});
|
|
294
|
+
const limit = maxRevisions ?? 0;
|
|
295
|
+
if (!Number.isInteger(limit) || limit < 0) {
|
|
296
|
+
throw new Error('actionGate: maxRevisions must be a non-negative integer');
|
|
297
|
+
}
|
|
298
|
+
const resolvedExecute = resolveRef(execute);
|
|
299
|
+
// The execute agent must receive exactly the frozen payload; a carried config
|
|
300
|
+
// would change what runs vs. what was scored and break the guarantee.
|
|
301
|
+
if (resolvedExecute.config && Object.keys(resolvedExecute.config).length > 0) {
|
|
302
|
+
throw new Error('actionGate: the execute agent must not carry defaultInput/config — it receives exactly the proposed payload so the executed payload provably equals the scored one');
|
|
303
|
+
}
|
|
304
|
+
const resolvedRevise = revise ? resolveRef(revise) : undefined;
|
|
305
|
+
const defaultInput = {
|
|
306
|
+
criteria: normalized,
|
|
307
|
+
executeHash: resolvedExecute.hash,
|
|
308
|
+
reviseHash: resolvedRevise?.hash,
|
|
309
|
+
reviseConfig: resolvedRevise?.config,
|
|
310
|
+
maxRevisions: limit,
|
|
311
|
+
};
|
|
312
|
+
return {
|
|
313
|
+
definition: ActionGateLogic,
|
|
314
|
+
defaultInput: defaultInput,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
/** Alias for {@link actionGate}. */
|
|
318
|
+
export const createActionAgent = actionGate;
|
|
319
|
+
//# sourceMappingURL=action.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"action.js","sourceRoot":"","sources":["../../src/agents/action.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAazC,MAAM,OAAO,GAAwB,IAAI,GAAG,CAAC,CAAC,eAAe,EAAE,gBAAgB,EAAE,QAAQ,CAAC,CAAC,CAAC;AAkB5F;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CACnC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAA6B,EAAE;IACtD,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,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K,CACb,oKAAoK,CACrK,CAAC;IACJ,CAAC;IACD,MAAM,cAAc,GAAG,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAE/D,MAAM,YAAY,GAAkC;QAClD,QAAQ,EAAE,UAAU;QACpB,WAAW,EAAE,eAAe,CAAC,IAAI;QACjC,UAAU,EAAE,cAAc,EAAE,IAAI;QAChC,YAAY,EAAE,cAAc,EAAE,MAAM;QACpC,YAAY,EAAE,KAAK;KACpB,CAAC;IAEF,OAAO;QACL,UAAU,EAAE,eAAe;QAC3B,YAAY,EAAE,YAAwC;KACvD,CAAC;AACJ,CAAC;AAED,oCAAoC;AACpC,MAAM,CAAC,MAAM,iBAAiB,GAAG,UAAU,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"basic-chat.js","sourceRoot":"","sources":["../../src/agents/basic-chat.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAsC3D;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,YAAoB,EAAE,
|
|
1
|
+
{"version":3,"file":"basic-chat.js","sourceRoot":"","sources":["../../src/agents/basic-chat.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAsC3D;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,YAAoB,EAAE,OAAO,GAAqB,EAAE;IAClF,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,GAAG,WAAW,EAAE,GAAG,OAAO,CAAC;IAEtD,OAAO,MAAM,CACX,KAAK,EACH,MAA+B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AEF,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;YAClB,QAAQ,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC;QAClC,CAAC;QAED,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QAExD,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE;YAC1C,GAAG,WAAW;YACd,cAAc,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;SACjC,CAAC,CAAC;QACH,MAAM,KAAK,GAAG,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC;QAEzE,0BAA0B;QAC1B,MAAM,IAAI,GAAG,iBAAiB,CAAI,KAAK,CAAC,CAAC;QAEzC,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;IAC9B,CAAC,EACD;QACE,IAAI,EAAE,IAAI,IAAI,iBAAiB;QAC/B,WAAW,EAAE,WAAW,IAAI,8BAA8B;KAC3D,CACF,CAAC;AACJ,CAAC"}
|
package/dist/agents/index.d.ts
CHANGED
|
@@ -6,4 +6,5 @@
|
|
|
6
6
|
export { createChatAgent, createJsonChatAgent, type ChatInput, type ChatOutput, type ChatAgentOptions, } from './basic-chat.js';
|
|
7
7
|
export { ReActLogic, createReActAgent, availableTools, type ReActInput, type ReActOutput, type ReActStep, type ReActAgentOptions, type ToolDefinition, } from './react.js';
|
|
8
8
|
export { GoalAgentLogic, createGoalAgent, type GoalAgentOptions, type GoalCriterionSpec, type GoalAgentLogicInput, type NormalizedCriterion, } from './goal.js';
|
|
9
|
+
export { ActionGateLogic, actionGate, createActionAgent, type ActionGateOptions, type ActionCriterionSpec, type ActionGateLogicInput, } from './action.js';
|
|
9
10
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,KAAK,SAAS,EACd,KAAK,UAAU,EACf,KAAK,gBAAgB,GACtB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAEL,UAAU,EAEV,gBAAgB,EAEhB,cAAc,EAEd,KAAK,UAAU,EACf,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,iBAAiB,EACtB,KAAK,cAAc,GACpB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAEL,cAAc,EAEd,eAAe,EAEf,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,KAAK,mBAAmB,EACxB,KAAK,mBAAmB,GACzB,MAAM,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,KAAK,SAAS,EACd,KAAK,UAAU,EACf,KAAK,gBAAgB,GACtB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAEL,UAAU,EAEV,gBAAgB,EAEhB,cAAc,EAEd,KAAK,UAAU,EACf,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,iBAAiB,EACtB,KAAK,cAAc,GACpB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAEL,cAAc,EAEd,eAAe,EAEf,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,KAAK,mBAAmB,EACxB,KAAK,mBAAmB,GACzB,MAAM,WAAW,CAAC;AAEnB,OAAO,EAEL,eAAe,EAEf,UAAU,EACV,iBAAiB,EAEjB,KAAK,iBAAiB,EACtB,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,GAC1B,MAAM,aAAa,CAAC"}
|
package/dist/agents/index.js
CHANGED
|
@@ -16,4 +16,9 @@ export {
|
|
|
16
16
|
GoalAgentLogic,
|
|
17
17
|
// The factory (returns VirtualAgent)
|
|
18
18
|
createGoalAgent, } from './goal.js';
|
|
19
|
+
export {
|
|
20
|
+
// The master logic (compiled controller)
|
|
21
|
+
ActionGateLogic,
|
|
22
|
+
// The factories (return VirtualAgent)
|
|
23
|
+
actionGate, createActionAgent, } from './action.js';
|
|
19
24
|
//# sourceMappingURL=index.js.map
|
package/dist/agents/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,eAAe,EACf,mBAAmB,GAIpB,MAAM,iBAAiB,CAAC;AAEzB,OAAO;AACL,oCAAoC;AACpC,UAAU;AACV,qCAAqC;AACrC,gBAAgB;AAChB,uBAAuB;AACvB,cAAc,GAOf,MAAM,YAAY,CAAC;AAEpB,OAAO;AACL,yCAAyC;AACzC,cAAc;AACd,qCAAqC;AACrC,eAAe,GAMhB,MAAM,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,eAAe,EACf,mBAAmB,GAIpB,MAAM,iBAAiB,CAAC;AAEzB,OAAO;AACL,oCAAoC;AACpC,UAAU;AACV,qCAAqC;AACrC,gBAAgB;AAChB,uBAAuB;AACvB,cAAc,GAOf,MAAM,YAAY,CAAC;AAEpB,OAAO;AACL,yCAAyC;AACzC,cAAc;AACd,qCAAqC;AACrC,eAAe,GAMhB,MAAM,WAAW,CAAC;AAEnB,OAAO;AACL,yCAAyC;AACzC,eAAe;AACf,sCAAsC;AACtC,UAAU,EACV,iBAAiB,GAKlB,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/agents/virtual-agent.ts
|
|
20
|
+
var virtual_agent_exports = {};
|
|
21
|
+
__export(virtual_agent_exports, {
|
|
22
|
+
default: () => virtual_agent_default
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(virtual_agent_exports);
|
|
25
|
+
var METHODS = /* @__PURE__ */ new Set(["deterministic", "model_assessed", "opaque"]);
|
|
26
|
+
var __alveusHandler = async (_state, event, ctx) => {
|
|
27
|
+
const { action, criteria, executeHash, reviseHash, reviseConfig } = event;
|
|
28
|
+
const maxRevisions = event.maxRevisions;
|
|
29
|
+
if (typeof action !== "string" || action.length === 0) {
|
|
30
|
+
throw new Error("Action contract invalid: action must be a non-empty string");
|
|
31
|
+
}
|
|
32
|
+
if (!Array.isArray(criteria) || criteria.length === 0) {
|
|
33
|
+
throw new Error("Action contract invalid: at least one criterion is required");
|
|
34
|
+
}
|
|
35
|
+
if (typeof executeHash !== "string" || executeHash.length === 0) {
|
|
36
|
+
throw new Error("Action contract invalid: an execute agent is required");
|
|
37
|
+
}
|
|
38
|
+
if (!Number.isInteger(maxRevisions) || maxRevisions < 0) {
|
|
39
|
+
throw new Error("Action contract invalid: maxRevisions must be a non-negative integer");
|
|
40
|
+
}
|
|
41
|
+
const seenIds = /* @__PURE__ */ new Set();
|
|
42
|
+
for (const c of criteria) {
|
|
43
|
+
if (!c || typeof c.id !== "string" || c.id.length === 0) {
|
|
44
|
+
throw new Error("Action contract invalid: every criterion needs a non-empty id");
|
|
45
|
+
}
|
|
46
|
+
if (seenIds.has(c.id)) {
|
|
47
|
+
throw new Error(`Action contract invalid: duplicate criterion id "${c.id}"`);
|
|
48
|
+
}
|
|
49
|
+
seenIds.add(c.id);
|
|
50
|
+
if (typeof c.scorerHash !== "string" || c.scorerHash.length === 0) {
|
|
51
|
+
throw new Error(`Action contract invalid: criterion "${c.id}" has no pinned scorer`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const passes = (threshold, r, id) => {
|
|
55
|
+
const t = threshold ?? { kind: "verdict" };
|
|
56
|
+
if (t.kind === "verdict") {
|
|
57
|
+
if (typeof r.verdict !== "boolean") {
|
|
58
|
+
throw new Error(`Scorer did not return a boolean verdict for "${id}"`);
|
|
59
|
+
}
|
|
60
|
+
return r.verdict === true;
|
|
61
|
+
}
|
|
62
|
+
if (t.kind === "minScore") {
|
|
63
|
+
if (typeof r.score !== "number" || !Number.isFinite(r.score)) {
|
|
64
|
+
throw new Error(`Scorer did not return a numeric score for "${id}"`);
|
|
65
|
+
}
|
|
66
|
+
return r.score >= t.min;
|
|
67
|
+
}
|
|
68
|
+
throw new Error(`Unknown threshold kind for "${id}"`);
|
|
69
|
+
};
|
|
70
|
+
const parseScores = (raw, expected) => {
|
|
71
|
+
if (!raw || typeof raw !== "object" || !Array.isArray(raw.results)) {
|
|
72
|
+
throw new Error("Scorer output invalid: missing results array");
|
|
73
|
+
}
|
|
74
|
+
const expectedSet = new Set(expected);
|
|
75
|
+
const byId = /* @__PURE__ */ new Map();
|
|
76
|
+
for (const entry of raw.results) {
|
|
77
|
+
if (!entry || typeof entry !== "object") {
|
|
78
|
+
throw new Error("Scorer output invalid: non-object result entry");
|
|
79
|
+
}
|
|
80
|
+
const { criterionId, method, verdict, score, label, reasoning, metadata, ...extra } = entry;
|
|
81
|
+
if (typeof criterionId !== "string") {
|
|
82
|
+
throw new Error("Scorer output invalid: result missing string criterionId");
|
|
83
|
+
}
|
|
84
|
+
if (!expectedSet.has(criterionId)) {
|
|
85
|
+
throw new Error(`Scorer output invalid: unexpected criterionId "${criterionId}"`);
|
|
86
|
+
}
|
|
87
|
+
if (byId.has(criterionId)) {
|
|
88
|
+
throw new Error(`Scorer output invalid: duplicate result for "${criterionId}"`);
|
|
89
|
+
}
|
|
90
|
+
const extraKeys = Object.keys(extra);
|
|
91
|
+
if (extraKeys.length > 0) {
|
|
92
|
+
throw new Error(
|
|
93
|
+
`Scorer output invalid: unknown field(s) for "${criterionId}": ${extraKeys.join(", ")}`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
if (verdict !== void 0 && typeof verdict !== "boolean") {
|
|
97
|
+
throw new Error(`Scorer output invalid: "${criterionId}" verdict must be boolean`);
|
|
98
|
+
}
|
|
99
|
+
if (score !== void 0 && (typeof score !== "number" || !Number.isFinite(score))) {
|
|
100
|
+
throw new Error(`Scorer output invalid: "${criterionId}" score must be a finite number`);
|
|
101
|
+
}
|
|
102
|
+
if (label !== void 0 && typeof label !== "string") {
|
|
103
|
+
throw new Error(`Scorer output invalid: "${criterionId}" label must be a string`);
|
|
104
|
+
}
|
|
105
|
+
if (reasoning !== void 0 && typeof reasoning !== "string") {
|
|
106
|
+
throw new Error(`Scorer output invalid: "${criterionId}" reasoning must be a string`);
|
|
107
|
+
}
|
|
108
|
+
if (method !== void 0 && !METHODS.has(method)) {
|
|
109
|
+
throw new Error(`Scorer output invalid: "${criterionId}" has an unknown method`);
|
|
110
|
+
}
|
|
111
|
+
byId.set(criterionId, {
|
|
112
|
+
result: { verdict, score, label, reasoning, metadata },
|
|
113
|
+
method: method ?? "opaque"
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
for (const id of expected) {
|
|
117
|
+
if (!byId.has(id)) {
|
|
118
|
+
throw new Error(`Scorer output invalid: missing result for "${id}"`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return byId;
|
|
122
|
+
};
|
|
123
|
+
const buildGroups = () => {
|
|
124
|
+
const groups2 = /* @__PURE__ */ new Map();
|
|
125
|
+
for (const c of criteria) {
|
|
126
|
+
const key = `${c.scorerHash} ${JSON.stringify(c.scorerConfig ?? null)} ${JSON.stringify(c.reference ?? null)}`;
|
|
127
|
+
let g = groups2.get(key);
|
|
128
|
+
if (!g) {
|
|
129
|
+
g = {
|
|
130
|
+
scorerHash: c.scorerHash,
|
|
131
|
+
scorerConfig: c.scorerConfig,
|
|
132
|
+
reference: c.reference,
|
|
133
|
+
ids: []
|
|
134
|
+
};
|
|
135
|
+
groups2.set(key, g);
|
|
136
|
+
}
|
|
137
|
+
g.ids.push(c.id);
|
|
138
|
+
}
|
|
139
|
+
return [...groups2.values()];
|
|
140
|
+
};
|
|
141
|
+
const groups = buildGroups();
|
|
142
|
+
const scorePayload = async (payload2) => {
|
|
143
|
+
const results = /* @__PURE__ */ new Map();
|
|
144
|
+
for (const g of groups) {
|
|
145
|
+
const scorerInput = {
|
|
146
|
+
subject: payload2,
|
|
147
|
+
criteria: g.ids.map((criterionId) => ({ criterionId })),
|
|
148
|
+
reference: g.reference,
|
|
149
|
+
config: g.scorerConfig
|
|
150
|
+
};
|
|
151
|
+
const raw = await ctx.call(g.scorerHash, scorerInput, {
|
|
152
|
+
label: `check: ${g.ids.join(", ")}`
|
|
153
|
+
});
|
|
154
|
+
const parsed = parseScores(raw, g.ids);
|
|
155
|
+
for (const [id, v] of parsed) results.set(id, v);
|
|
156
|
+
}
|
|
157
|
+
return criteria.map((c) => {
|
|
158
|
+
const scored = results.get(c.id);
|
|
159
|
+
return {
|
|
160
|
+
id: c.id,
|
|
161
|
+
passed: passes(c.threshold, scored.result, c.id),
|
|
162
|
+
reasoning: scored.result.reasoning
|
|
163
|
+
};
|
|
164
|
+
});
|
|
165
|
+
};
|
|
166
|
+
ctx.logger.info("Action gate started", {
|
|
167
|
+
action,
|
|
168
|
+
criteria: criteria.map((c) => c.id),
|
|
169
|
+
scorerCount: groups.length,
|
|
170
|
+
maxRevisions
|
|
171
|
+
});
|
|
172
|
+
let payload = { action, input: event.input };
|
|
173
|
+
let revisions = 0;
|
|
174
|
+
for (; ; ) {
|
|
175
|
+
await ctx.setState(`action.round.${revisions}.payload`, payload);
|
|
176
|
+
const evaluated = await scorePayload(payload);
|
|
177
|
+
const unmet = evaluated.filter((e) => !e.passed);
|
|
178
|
+
ctx.logger.info("Action evaluated", {
|
|
179
|
+
revisions,
|
|
180
|
+
passed: evaluated.filter((e) => e.passed).map((e) => e.id),
|
|
181
|
+
unmet: unmet.map((e) => e.id)
|
|
182
|
+
});
|
|
183
|
+
if (unmet.length === 0) {
|
|
184
|
+
await ctx.call(executeHash, payload, { label: action });
|
|
185
|
+
ctx.logger.info("Action allowed and executed", { action, revisions });
|
|
186
|
+
return { decision: "ALLOWED", revisions, executed: true };
|
|
187
|
+
}
|
|
188
|
+
if (reviseHash && revisions < maxRevisions) {
|
|
189
|
+
const feedback = unmet.map(
|
|
190
|
+
(e) => `- Criterion "${e.id}" not met.${e.reasoning ? ` ${e.reasoning}` : ""}`
|
|
191
|
+
);
|
|
192
|
+
const revised = await ctx.call(
|
|
193
|
+
reviseHash,
|
|
194
|
+
{ ...reviseConfig ?? {}, action, input: payload.input, feedback },
|
|
195
|
+
{ label: `revise: ${action}` }
|
|
196
|
+
);
|
|
197
|
+
const newInput = revised && typeof revised === "object" && "input" in revised ? revised.input : revised;
|
|
198
|
+
payload = { action, input: newInput };
|
|
199
|
+
revisions++;
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
ctx.logger.info("Action denied", {
|
|
203
|
+
action,
|
|
204
|
+
revisions,
|
|
205
|
+
unmet: unmet.map((e) => e.id)
|
|
206
|
+
});
|
|
207
|
+
return { decision: "DENIED", revisions, executed: false };
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
var virtual_agent_default = __alveusHandler;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/composition/errors.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,sBAAsB;IACrC,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,QAAQ,CAAC,QAAQ,EAAE,sBAAsB,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/composition/errors.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,sBAAsB;IACrC,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,QAAQ,CAAC,QAAQ,EAAE,sBAAsB,EAAE,CAAC;IAE5C,YAAY,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,sBAAsB,EAAE,EAI9D;CACF;AAED,qBAAa,qBAAsB,SAAQ,KAAK;IAC9C,YAAY,OAAO,EAAE,MAAM,EAG1B;CACF;AAED,qBAAa,6BAA8B,SAAQ,KAAK;IACtD,YAAY,OAAO,EAAE,MAAM,EAG1B;CACF;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,WAAW,CAAC,OAAO,CAAC,CAE3E"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"history.js","sourceRoot":"","sources":["../../src/memory/history.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAoCH,MAAM,eAAe,GAA6B;IAChD,gBAAgB,EAAE,IAAI,GAAG,EAAE,EAAE,OAAO;IACpC,cAAc,EAAE,IAAI;CACrB,CAAC;AAEF;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,IAAiB;IAC5C,OAAO,CACL,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,CAC9F,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,GAAqB,EACrB,QAAuB,EACvB,
|
|
1
|
+
{"version":3,"file":"history.js","sourceRoot":"","sources":["../../src/memory/history.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAoCH,MAAM,eAAe,GAA6B;IAChD,gBAAgB,EAAE,IAAI,GAAG,EAAE,EAAE,OAAO;IACpC,cAAc,EAAE,IAAI;CACrB,CAAC;AAEF;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,IAAiB;IAC5C,OAAO,CACL,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,CAC9F,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,GAAqB,EACrB,QAAuB,EACvB,OAAO,GAAmB,EAAE;IAE5B,MAAM,IAAI,GAAG,EAAE,GAAG,eAAe,EAAE,GA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IAC1E,OAAO,IAAI,CAAC,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC;AACnC,CAAC"}
|
|
@@ -65,9 +65,9 @@ export interface ApprovalResponse {
|
|
|
65
65
|
*/
|
|
66
66
|
export declare const ApprovalTopics: {
|
|
67
67
|
/** Topic for approval requests */
|
|
68
|
-
readonly NEEDED:
|
|
68
|
+
readonly NEEDED: 'approval.needed';
|
|
69
69
|
/** Topic for approval responses */
|
|
70
|
-
readonly RESPONSE:
|
|
70
|
+
readonly RESPONSE: 'approval.response';
|
|
71
71
|
};
|
|
72
72
|
/**
|
|
73
73
|
* Wait for human approval
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"approval.d.ts","sourceRoot":"","sources":["../../src/patterns/approval.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,oCAAoC;IACpC,MAAM,EAAE,MAAM,CAAC;IAEf,0CAA0C;IAC1C,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAElC,8CAA8C;IAC9C,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,kDAAkD;IAClD,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,sCAAsC;IACtC,QAAQ,EAAE,OAAO,CAAC;IAElB,uCAAuC;IACvC,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,yCAAyC;IACzC,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,gCAAgC;IAChC,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,eAAO,MAAM,cAAc;IACzB,kCAAkC
|
|
1
|
+
{"version":3,"file":"approval.d.ts","sourceRoot":"","sources":["../../src/patterns/approval.ts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AQ,KAAK,IAAI;YAC1C,UAAU,EAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,IAAI,QAAQ;YACjD,MAAM,EAAE,QAAQ,CAAC,IAAI,EAAE,MAAM;YAC7B,SAAS,EAAE,QAAQ,CAAC,SAAS;SAC9B,CAAC;QAEF,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAEzB,wCAAwC;QACxC,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC;YACvB,OAAO,SAAS,CAAC;QACnB,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Action Gate Example
|
|
3
|
+
*
|
|
4
|
+
* A proof-gated side effect: a refund is proposed, frozen, and scored against
|
|
5
|
+
* explicit criteria by a pinned scorer. The refund executes ONLY when every
|
|
6
|
+
* criterion passes, with the exact payload that was scored. When a criterion
|
|
7
|
+
* fails, the gate revises the payload once and re-scores, or denies. Every
|
|
8
|
+
* terminal run yields a platform-served Action Receipt (the authoritative
|
|
9
|
+
* outcome) that the agent itself cannot forge, proving what was allowed, on what
|
|
10
|
+
* evidence, and that the executed payload was byte-for-byte the one that scored.
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* alveus deploy
|
|
14
|
+
* alveus run @yourscope/action-gate --input '{"amount": 100, "reason": "duplicate charge"}' # ALLOWED
|
|
15
|
+
* alveus run @yourscope/action-gate --input '{"amount": 5000, "reason": "duplicate charge"}' # revised, then ALLOWED
|
|
16
|
+
* alveus run @yourscope/action-gate --input '{"amount": 100}' # DENIED (no reason)
|
|
17
|
+
* alveus receipt <execution-id>
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { agent$ } from '@alveus-ai/core';
|
|
21
|
+
import type { ExecutionContext, ScorerInput, ScorerOutput } from '@alveus-ai/core';
|
|
22
|
+
import { actionGate } from '@alveus-ai/std';
|
|
23
|
+
|
|
24
|
+
const POLICY_LIMIT = 1000;
|
|
25
|
+
|
|
26
|
+
interface RefundInput {
|
|
27
|
+
amount: number;
|
|
28
|
+
reason?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Scorer: a deterministic check over the frozen proposal. It only measures; it
|
|
33
|
+
* returns a verdict per criterion and never decides whether to execute.
|
|
34
|
+
*/
|
|
35
|
+
const refundScorer = agent$(async (_state, input: ScorerInput, _ctx: ExecutionContext) => {
|
|
36
|
+
const proposal = input.subject as { input?: RefundInput };
|
|
37
|
+
const amount = proposal?.input?.amount;
|
|
38
|
+
const reason = proposal?.input?.reason;
|
|
39
|
+
|
|
40
|
+
const check = (id: string): boolean => {
|
|
41
|
+
if (id === 'within_policy') return typeof amount === 'number' && amount <= POLICY_LIMIT;
|
|
42
|
+
if (id === 'reason_given') return typeof reason === 'string' && reason.length > 0;
|
|
43
|
+
return false;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const output: ScorerOutput = {
|
|
47
|
+
results: input.criteria.map((c) => ({
|
|
48
|
+
criterionId: c.criterionId,
|
|
49
|
+
verdict: check(c.criterionId),
|
|
50
|
+
method: 'deterministic',
|
|
51
|
+
})),
|
|
52
|
+
};
|
|
53
|
+
return output;
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Execute step: the actual side effect. It runs ONLY when every criterion
|
|
58
|
+
* passed, with the exact frozen payload `{ action, input }` that was scored. In
|
|
59
|
+
* production this would call your payments API via `ctx.fetch`; keep it
|
|
60
|
+
* idempotent so a retry never double-refunds.
|
|
61
|
+
*/
|
|
62
|
+
const issueRefund = agent$(
|
|
63
|
+
async (_state, payload: { action: string; input: RefundInput }, _ctx: ExecutionContext) => {
|
|
64
|
+
return { refunded: true, amount: payload.input.amount };
|
|
65
|
+
},
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Revise step: bring an over-policy refund within policy, then it is re-scored.
|
|
70
|
+
* It receives the current input plus the unmet-criterion feedback and returns
|
|
71
|
+
* the new input. It cannot invent a missing reason, so a no-reason proposal is
|
|
72
|
+
* denied rather than executed.
|
|
73
|
+
*/
|
|
74
|
+
const clampRefund = agent$(
|
|
75
|
+
async (
|
|
76
|
+
_state,
|
|
77
|
+
event: { action: string; input: RefundInput; feedback: string[] },
|
|
78
|
+
_ctx: ExecutionContext,
|
|
79
|
+
) => {
|
|
80
|
+
return { input: { ...event.input, amount: Math.min(event.input.amount, POLICY_LIMIT) } };
|
|
81
|
+
},
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const gate = actionGate({
|
|
85
|
+
criteria: [{ id: 'within_policy' }, { id: 'reason_given' }],
|
|
86
|
+
scorer: refundScorer,
|
|
87
|
+
execute: issueRefund,
|
|
88
|
+
revise: clampRefund,
|
|
89
|
+
maxRevisions: 1,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Deployable root: propose the action so the platform can recognize and attest
|
|
94
|
+
* the gated run. The receipt is retrieved separately, from the platform.
|
|
95
|
+
*/
|
|
96
|
+
export const refundGate = agent$((_state, input: RefundInput, ctx: ExecutionContext) =>
|
|
97
|
+
// Freeze the proposal and label the run by the action it gates.
|
|
98
|
+
ctx.call(gate, { action: 'issue-refund', input }, { label: 'issue-refund' }),
|
|
99
|
+
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alveus-ai/std",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.56",
|
|
4
4
|
"description": "Standard library of reusable agents and utilities for Alveus",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"examples"
|
|
37
37
|
],
|
|
38
38
|
"scripts": {
|
|
39
|
-
"build": "tsc && node --import tsx scripts/build-blobs.ts",
|
|
40
|
-
"dev": "tsc --watch",
|
|
39
|
+
"build": "node \"$PROJECT_CWD/scripts/native-tsc.mjs\" && node --import tsx scripts/build-blobs.ts",
|
|
40
|
+
"dev": "node \"$PROJECT_CWD/scripts/native-tsc.mjs\" --watch",
|
|
41
41
|
"test": "node --import tsx --test test/**/*.test.ts",
|
|
42
42
|
"clean": "rm -rf dist .alveus",
|
|
43
43
|
"seed": "node --import tsx scripts/seed.ts"
|
|
@@ -53,6 +53,6 @@
|
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@alveus-ai/core": "workspace:*",
|
|
56
|
-
"typescript": "^
|
|
56
|
+
"typescript": "^6.0.3"
|
|
57
57
|
}
|
|
58
58
|
}
|