@agentcontract/core 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 +112 -0
- package/README.md +106 -0
- package/dist/audit-DNON4W2Q.mjs +7 -0
- package/dist/audit-DNON4W2Q.mjs.map +1 -0
- package/dist/chunk-BVUHPJDU.mjs +50 -0
- package/dist/chunk-BVUHPJDU.mjs.map +1 -0
- package/dist/chunk-UHNX2RBZ.mjs +526 -0
- package/dist/chunk-UHNX2RBZ.mjs.map +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +577 -0
- package/dist/cli.js.map +1 -0
- package/dist/cli.mjs +105 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/index.d.mts +259 -0
- package/dist/index.d.ts +259 -0
- package/dist/index.js +654 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +29 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +54 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __esm = (fn, res) => function __init() {
|
|
9
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
+
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
31
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
+
|
|
33
|
+
// node_modules/tsup/assets/cjs_shims.js
|
|
34
|
+
var init_cjs_shims = __esm({
|
|
35
|
+
"node_modules/tsup/assets/cjs_shims.js"() {
|
|
36
|
+
"use strict";
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// src/audit.ts
|
|
41
|
+
var audit_exports = {};
|
|
42
|
+
__export(audit_exports, {
|
|
43
|
+
AuditWriter: () => AuditWriter
|
|
44
|
+
});
|
|
45
|
+
var import_crypto2, import_fs2, AuditWriter;
|
|
46
|
+
var init_audit = __esm({
|
|
47
|
+
"src/audit.ts"() {
|
|
48
|
+
"use strict";
|
|
49
|
+
init_cjs_shims();
|
|
50
|
+
import_crypto2 = require("crypto");
|
|
51
|
+
import_fs2 = require("fs");
|
|
52
|
+
AuditWriter = class {
|
|
53
|
+
constructor(logPath = "agentcontract-audit.jsonl") {
|
|
54
|
+
this.logPath = logPath;
|
|
55
|
+
}
|
|
56
|
+
write(result, contractPath = "") {
|
|
57
|
+
const entry = this._buildEntry(result, contractPath);
|
|
58
|
+
(0, import_fs2.appendFileSync)(this.logPath, JSON.stringify(entry) + "\n", "utf-8");
|
|
59
|
+
return entry;
|
|
60
|
+
}
|
|
61
|
+
_buildEntry(result, contractPath) {
|
|
62
|
+
const ctx = result.context;
|
|
63
|
+
const inputHash = (0, import_crypto2.createHash)("sha256").update(ctx.input).digest("hex");
|
|
64
|
+
const outputHash = (0, import_crypto2.createHash)("sha256").update(ctx.output).digest("hex");
|
|
65
|
+
const entry = {
|
|
66
|
+
run_id: result.runId,
|
|
67
|
+
agent: result.agent,
|
|
68
|
+
contract: contractPath,
|
|
69
|
+
contract_version: result.contractVersion,
|
|
70
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
71
|
+
input_hash: inputHash,
|
|
72
|
+
output_hash: outputHash,
|
|
73
|
+
duration_ms: Math.round(ctx.durationMs * 100) / 100,
|
|
74
|
+
cost_usd: Math.round(ctx.costUsd * 1e6) / 1e6,
|
|
75
|
+
violations: result.violations.map((v) => ({
|
|
76
|
+
clause_type: v.clauseType,
|
|
77
|
+
clause_name: v.clauseName,
|
|
78
|
+
clause_text: v.clauseText,
|
|
79
|
+
severity: v.severity,
|
|
80
|
+
action_taken: v.actionTaken,
|
|
81
|
+
judge: v.judge,
|
|
82
|
+
details: v.details
|
|
83
|
+
})),
|
|
84
|
+
outcome: result.outcome
|
|
85
|
+
};
|
|
86
|
+
const auditKey = process.env["AGENTCONTRACT_AUDIT_KEY"];
|
|
87
|
+
if (auditKey) {
|
|
88
|
+
const payload = JSON.stringify(entry, Object.keys(entry).sort());
|
|
89
|
+
entry["signature"] = (0, import_crypto2.createHmac)("sha256", auditKey).update(payload).digest("hex");
|
|
90
|
+
}
|
|
91
|
+
return entry;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// src/index.ts
|
|
98
|
+
var src_exports = {};
|
|
99
|
+
__export(src_exports, {
|
|
100
|
+
AuditWriter: () => AuditWriter,
|
|
101
|
+
ContractError: () => ContractError,
|
|
102
|
+
ContractLoadError: () => ContractLoadError,
|
|
103
|
+
ContractPreconditionError: () => ContractPreconditionError,
|
|
104
|
+
ContractRunner: () => ContractRunner,
|
|
105
|
+
ContractViolation: () => ContractViolation,
|
|
106
|
+
SPEC_VERSION: () => SPEC_VERSION,
|
|
107
|
+
VERSION: () => VERSION,
|
|
108
|
+
enforce: () => enforce,
|
|
109
|
+
loadContract: () => loadContract,
|
|
110
|
+
makeContext: () => makeContext
|
|
111
|
+
});
|
|
112
|
+
module.exports = __toCommonJS(src_exports);
|
|
113
|
+
init_cjs_shims();
|
|
114
|
+
|
|
115
|
+
// src/loader.ts
|
|
116
|
+
init_cjs_shims();
|
|
117
|
+
var import_fs = require("fs");
|
|
118
|
+
var import_path = require("path");
|
|
119
|
+
var import_js_yaml = __toESM(require("js-yaml"));
|
|
120
|
+
|
|
121
|
+
// src/models.ts
|
|
122
|
+
init_cjs_shims();
|
|
123
|
+
var import_zod = require("zod");
|
|
124
|
+
var JudgeType = import_zod.z.enum(["deterministic", "llm"]).default("deterministic");
|
|
125
|
+
var ViolationAction = import_zod.z.enum(["warn", "block", "rollback", "halt_and_alert"]).default("block");
|
|
126
|
+
var AssertionType = import_zod.z.enum(["pattern", "schema", "llm", "cost", "latency", "custom"]);
|
|
127
|
+
var ClauseObject = import_zod.z.object({
|
|
128
|
+
text: import_zod.z.string().min(1),
|
|
129
|
+
judge: JudgeType,
|
|
130
|
+
description: import_zod.z.string().default("")
|
|
131
|
+
});
|
|
132
|
+
var Clause = import_zod.z.union([import_zod.z.string().min(1), ClauseObject]);
|
|
133
|
+
var PreconditionClause = import_zod.z.union([
|
|
134
|
+
import_zod.z.string().min(1),
|
|
135
|
+
import_zod.z.object({
|
|
136
|
+
text: import_zod.z.string().min(1),
|
|
137
|
+
judge: JudgeType,
|
|
138
|
+
on_fail: import_zod.z.enum(["block", "warn"]).default("block"),
|
|
139
|
+
description: import_zod.z.string().default("")
|
|
140
|
+
})
|
|
141
|
+
]);
|
|
142
|
+
var Assertion = import_zod.z.object({
|
|
143
|
+
name: import_zod.z.string().regex(/^[a-z][a-z0-9_]*$/),
|
|
144
|
+
type: AssertionType,
|
|
145
|
+
description: import_zod.z.string().default(""),
|
|
146
|
+
// pattern
|
|
147
|
+
must_not_match: import_zod.z.string().optional(),
|
|
148
|
+
must_match: import_zod.z.string().optional(),
|
|
149
|
+
// schema
|
|
150
|
+
schema: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional(),
|
|
151
|
+
// llm
|
|
152
|
+
prompt: import_zod.z.string().optional(),
|
|
153
|
+
pass_when: import_zod.z.string().optional(),
|
|
154
|
+
model: import_zod.z.string().optional(),
|
|
155
|
+
// cost
|
|
156
|
+
max_usd: import_zod.z.number().nonnegative().optional(),
|
|
157
|
+
// latency
|
|
158
|
+
max_ms: import_zod.z.number().int().positive().optional(),
|
|
159
|
+
// custom
|
|
160
|
+
plugin: import_zod.z.string().optional()
|
|
161
|
+
}).passthrough();
|
|
162
|
+
var Limits = import_zod.z.object({
|
|
163
|
+
max_tokens: import_zod.z.number().int().positive().optional(),
|
|
164
|
+
max_input_tokens: import_zod.z.number().int().positive().optional(),
|
|
165
|
+
max_latency_ms: import_zod.z.number().int().positive().optional(),
|
|
166
|
+
max_cost_usd: import_zod.z.number().nonnegative().optional(),
|
|
167
|
+
max_tool_calls: import_zod.z.number().int().nonnegative().optional(),
|
|
168
|
+
max_steps: import_zod.z.number().int().positive().optional()
|
|
169
|
+
}).default({});
|
|
170
|
+
var OnViolation = import_zod.z.object({
|
|
171
|
+
default: ViolationAction
|
|
172
|
+
}).catchall(import_zod.z.string()).default({ default: "block" });
|
|
173
|
+
var Contract = import_zod.z.object({
|
|
174
|
+
agent: import_zod.z.string().min(1),
|
|
175
|
+
"spec-version": import_zod.z.string(),
|
|
176
|
+
version: import_zod.z.string(),
|
|
177
|
+
description: import_zod.z.string().default(""),
|
|
178
|
+
author: import_zod.z.string().default(""),
|
|
179
|
+
created: import_zod.z.string().default(""),
|
|
180
|
+
tags: import_zod.z.array(import_zod.z.string()).default([]),
|
|
181
|
+
extends: import_zod.z.string().optional(),
|
|
182
|
+
must: import_zod.z.array(Clause).default([]),
|
|
183
|
+
must_not: import_zod.z.array(Clause).default([]),
|
|
184
|
+
can: import_zod.z.array(import_zod.z.string()).default([]),
|
|
185
|
+
requires: import_zod.z.array(PreconditionClause).default([]),
|
|
186
|
+
ensures: import_zod.z.array(Clause).default([]),
|
|
187
|
+
invariant: import_zod.z.array(Clause).default([]),
|
|
188
|
+
assert: import_zod.z.array(Assertion).default([]),
|
|
189
|
+
limits: Limits,
|
|
190
|
+
on_violation: OnViolation
|
|
191
|
+
});
|
|
192
|
+
function getClauseText(clause) {
|
|
193
|
+
return typeof clause === "string" ? clause : clause.text;
|
|
194
|
+
}
|
|
195
|
+
function getClauseJudge(clause) {
|
|
196
|
+
return typeof clause === "string" ? "deterministic" : clause.judge;
|
|
197
|
+
}
|
|
198
|
+
function getViolationAction(onViolation, name) {
|
|
199
|
+
return onViolation[name] ?? onViolation.default;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// src/exceptions.ts
|
|
203
|
+
init_cjs_shims();
|
|
204
|
+
var ContractError = class extends Error {
|
|
205
|
+
constructor(message) {
|
|
206
|
+
super(message);
|
|
207
|
+
this.name = "ContractError";
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
var ContractLoadError = class extends ContractError {
|
|
211
|
+
constructor(message) {
|
|
212
|
+
super(message);
|
|
213
|
+
this.name = "ContractLoadError";
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
var ContractPreconditionError = class extends ContractError {
|
|
217
|
+
clause;
|
|
218
|
+
details;
|
|
219
|
+
constructor(clause, details = "") {
|
|
220
|
+
super(`[PRECONDITION FAILED] ${clause}${details ? ": " + details : ""}`);
|
|
221
|
+
this.name = "ContractPreconditionError";
|
|
222
|
+
this.clause = clause;
|
|
223
|
+
this.details = details;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
var ContractViolation = class extends ContractError {
|
|
227
|
+
violations;
|
|
228
|
+
constructor(violations) {
|
|
229
|
+
const lines = violations.map(
|
|
230
|
+
(v) => `[${v.action_taken.toUpperCase()}] ${v.clause_type.toUpperCase()}: "${v.clause_text}"`
|
|
231
|
+
);
|
|
232
|
+
super("AgentContractViolation:\n" + lines.join("\n"));
|
|
233
|
+
this.name = "ContractViolation";
|
|
234
|
+
this.violations = violations;
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
// src/loader.ts
|
|
239
|
+
function loadContract(filePath) {
|
|
240
|
+
const ext = (0, import_path.extname)(filePath).toLowerCase();
|
|
241
|
+
if (![".yaml", ".yml", ".json"].includes(ext)) {
|
|
242
|
+
throw new ContractLoadError(
|
|
243
|
+
`Unsupported file format: ${ext}. Use .contract.yaml or .contract.json`
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
let raw;
|
|
247
|
+
try {
|
|
248
|
+
raw = (0, import_fs.readFileSync)(filePath, "utf-8");
|
|
249
|
+
} catch {
|
|
250
|
+
throw new ContractLoadError(`Contract file not found: ${filePath}`);
|
|
251
|
+
}
|
|
252
|
+
let data;
|
|
253
|
+
try {
|
|
254
|
+
data = ext === ".json" ? JSON.parse(raw) : import_js_yaml.default.load(raw);
|
|
255
|
+
} catch (e) {
|
|
256
|
+
throw new ContractLoadError(`Failed to parse contract file: ${e}`);
|
|
257
|
+
}
|
|
258
|
+
if (typeof data !== "object" || data === null || Array.isArray(data)) {
|
|
259
|
+
throw new ContractLoadError("Contract file must be a YAML/JSON object at the root level.");
|
|
260
|
+
}
|
|
261
|
+
const result = Contract.safeParse(data);
|
|
262
|
+
if (!result.success) {
|
|
263
|
+
const issues = result.error.issues.map((i) => ` ${i.path.join(".")}: ${i.message}`).join("\n");
|
|
264
|
+
throw new ContractLoadError(`Contract schema validation failed:
|
|
265
|
+
${issues}`);
|
|
266
|
+
}
|
|
267
|
+
return result.data;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// src/enforce.ts
|
|
271
|
+
init_cjs_shims();
|
|
272
|
+
|
|
273
|
+
// src/runner.ts
|
|
274
|
+
init_cjs_shims();
|
|
275
|
+
var import_crypto = require("crypto");
|
|
276
|
+
|
|
277
|
+
// src/validators/pattern.ts
|
|
278
|
+
init_cjs_shims();
|
|
279
|
+
var PatternValidator = class {
|
|
280
|
+
constructor(name, mustNotMatch, mustMatch, description = "") {
|
|
281
|
+
this.name = name;
|
|
282
|
+
this.mustNotMatch = mustNotMatch;
|
|
283
|
+
this.mustMatch = mustMatch;
|
|
284
|
+
this.description = description;
|
|
285
|
+
}
|
|
286
|
+
validate(context) {
|
|
287
|
+
const output = context.output;
|
|
288
|
+
if (this.mustNotMatch) {
|
|
289
|
+
const re = new RegExp(this.mustNotMatch);
|
|
290
|
+
const match = re.exec(output);
|
|
291
|
+
if (match) {
|
|
292
|
+
return {
|
|
293
|
+
passed: false,
|
|
294
|
+
clauseName: this.name,
|
|
295
|
+
clauseText: this.description || `must_not_match: ${this.mustNotMatch}`,
|
|
296
|
+
clauseType: "assert",
|
|
297
|
+
judge: "deterministic",
|
|
298
|
+
details: `Forbidden pattern found: '${match[0].slice(0, 50)}'`
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (this.mustMatch) {
|
|
303
|
+
const re = new RegExp(this.mustMatch);
|
|
304
|
+
if (!re.test(output)) {
|
|
305
|
+
return {
|
|
306
|
+
passed: false,
|
|
307
|
+
clauseName: this.name,
|
|
308
|
+
clauseText: this.description || `must_match: ${this.mustMatch}`,
|
|
309
|
+
clauseType: "assert",
|
|
310
|
+
judge: "deterministic",
|
|
311
|
+
details: "Required pattern not found in output."
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return {
|
|
316
|
+
passed: true,
|
|
317
|
+
clauseName: this.name,
|
|
318
|
+
clauseText: this.description || this.name,
|
|
319
|
+
clauseType: "assert",
|
|
320
|
+
judge: "deterministic",
|
|
321
|
+
details: ""
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
// src/validators/cost.ts
|
|
327
|
+
init_cjs_shims();
|
|
328
|
+
var CostValidator = class {
|
|
329
|
+
constructor(name, maxUsd, description = "") {
|
|
330
|
+
this.name = name;
|
|
331
|
+
this.maxUsd = maxUsd;
|
|
332
|
+
this.description = description;
|
|
333
|
+
}
|
|
334
|
+
validate(context) {
|
|
335
|
+
const passed = context.costUsd <= this.maxUsd;
|
|
336
|
+
return {
|
|
337
|
+
passed,
|
|
338
|
+
clauseName: this.name,
|
|
339
|
+
clauseText: this.description || `cost must not exceed $${this.maxUsd.toFixed(4)} USD`,
|
|
340
|
+
clauseType: "assert",
|
|
341
|
+
judge: "deterministic",
|
|
342
|
+
details: passed ? "" : `Run cost $${context.costUsd.toFixed(4)} exceeded limit $${this.maxUsd.toFixed(4)}`
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
// src/validators/latency.ts
|
|
348
|
+
init_cjs_shims();
|
|
349
|
+
var LatencyValidator = class {
|
|
350
|
+
constructor(name, maxMs, description = "") {
|
|
351
|
+
this.name = name;
|
|
352
|
+
this.maxMs = maxMs;
|
|
353
|
+
this.description = description;
|
|
354
|
+
}
|
|
355
|
+
validate(context) {
|
|
356
|
+
const passed = context.durationMs <= this.maxMs;
|
|
357
|
+
return {
|
|
358
|
+
passed,
|
|
359
|
+
clauseName: this.name,
|
|
360
|
+
clauseText: this.description || `latency must not exceed ${this.maxMs}ms`,
|
|
361
|
+
clauseType: "assert",
|
|
362
|
+
judge: "deterministic",
|
|
363
|
+
details: passed ? "" : `Run took ${Math.round(context.durationMs)}ms, exceeded limit of ${this.maxMs}ms`
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
// src/validators/llm.ts
|
|
369
|
+
init_cjs_shims();
|
|
370
|
+
var DEFAULT_JUDGE_MODEL = "claude-haiku-4-5-20251001";
|
|
371
|
+
var JUDGE_SYSTEM_PROMPT = "You are an impartial compliance judge evaluating an AI agent's behavior against a specific contract clause. Evaluate objectively based only on the evidence provided. Your response must be a single word: YES or NO, followed optionally by one sentence of reasoning.";
|
|
372
|
+
var LLMValidator = class {
|
|
373
|
+
constructor(name, clauseText, clauseType, prompt, passWhen = "NO", model = DEFAULT_JUDGE_MODEL, description = "") {
|
|
374
|
+
this.name = name;
|
|
375
|
+
this.clauseText = clauseText;
|
|
376
|
+
this.clauseType = clauseType;
|
|
377
|
+
this.prompt = prompt;
|
|
378
|
+
this.passWhen = passWhen;
|
|
379
|
+
this.model = model;
|
|
380
|
+
this.description = description;
|
|
381
|
+
}
|
|
382
|
+
async validate(context) {
|
|
383
|
+
let Anthropic;
|
|
384
|
+
try {
|
|
385
|
+
const mod = await import("@anthropic-ai/sdk");
|
|
386
|
+
Anthropic = mod.default;
|
|
387
|
+
} catch {
|
|
388
|
+
return {
|
|
389
|
+
passed: false,
|
|
390
|
+
clauseName: this.name,
|
|
391
|
+
clauseText: this.clauseText,
|
|
392
|
+
clauseType: this.clauseType,
|
|
393
|
+
judge: "llm",
|
|
394
|
+
details: "@anthropic-ai/sdk not installed. Run: npm install @anthropic-ai/sdk"
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
const client = new Anthropic();
|
|
398
|
+
const userPrompt = this.prompt ? `${this.prompt}
|
|
399
|
+
|
|
400
|
+
--- AGENT INPUT ---
|
|
401
|
+
${context.input}
|
|
402
|
+
|
|
403
|
+
--- AGENT OUTPUT ---
|
|
404
|
+
${context.output}` : `Contract clause: "${this.clauseText}"
|
|
405
|
+
|
|
406
|
+
--- AGENT INPUT ---
|
|
407
|
+
${context.input}
|
|
408
|
+
|
|
409
|
+
--- AGENT OUTPUT ---
|
|
410
|
+
${context.output}
|
|
411
|
+
|
|
412
|
+
Does the agent's output satisfy the contract clause? Answer YES if satisfied, NO if violated.`;
|
|
413
|
+
try {
|
|
414
|
+
const response = await client.messages.create({
|
|
415
|
+
model: this.model,
|
|
416
|
+
max_tokens: 64,
|
|
417
|
+
system: JUDGE_SYSTEM_PROMPT,
|
|
418
|
+
messages: [{ role: "user", content: userPrompt }]
|
|
419
|
+
});
|
|
420
|
+
const raw = response.content[0].type === "text" ? response.content[0].text.trim() : "";
|
|
421
|
+
const firstWord = raw.split(/\s+/)[0]?.toUpperCase().replace(/[.,;:]$/, "") ?? "";
|
|
422
|
+
const passed = firstWord === this.passWhen.toUpperCase();
|
|
423
|
+
const reasoning = raw.slice(firstWord.length).trim();
|
|
424
|
+
return {
|
|
425
|
+
passed,
|
|
426
|
+
clauseName: this.name,
|
|
427
|
+
clauseText: this.clauseText,
|
|
428
|
+
clauseType: this.clauseType,
|
|
429
|
+
judge: "llm",
|
|
430
|
+
details: reasoning
|
|
431
|
+
};
|
|
432
|
+
} catch (e) {
|
|
433
|
+
return {
|
|
434
|
+
passed: false,
|
|
435
|
+
clauseName: this.name,
|
|
436
|
+
clauseText: this.clauseText,
|
|
437
|
+
clauseType: this.clauseType,
|
|
438
|
+
judge: "llm",
|
|
439
|
+
details: `Judge model error: ${e}`
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
// src/runner.ts
|
|
446
|
+
var ContractRunner = class {
|
|
447
|
+
constructor(contract) {
|
|
448
|
+
this.contract = contract;
|
|
449
|
+
}
|
|
450
|
+
async run(context, runId) {
|
|
451
|
+
const rid = runId ?? (0, import_crypto.randomUUID)();
|
|
452
|
+
const violations = [];
|
|
453
|
+
const c = this.contract;
|
|
454
|
+
const ov = c.on_violation;
|
|
455
|
+
violations.push(...this._checkLimits(context));
|
|
456
|
+
for (const assertion of c.assert) {
|
|
457
|
+
const result = await this._runAssertion(assertion, context);
|
|
458
|
+
if (!result.passed) {
|
|
459
|
+
const action = getViolationAction(ov, assertion.name);
|
|
460
|
+
violations.push({
|
|
461
|
+
clauseType: "assert",
|
|
462
|
+
clauseName: assertion.name,
|
|
463
|
+
clauseText: result.clauseText,
|
|
464
|
+
severity: action,
|
|
465
|
+
actionTaken: action,
|
|
466
|
+
judge: result.judge,
|
|
467
|
+
details: result.details
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
for (const clause of c.must) {
|
|
472
|
+
const text = getClauseText(clause);
|
|
473
|
+
const judge = getClauseJudge(clause);
|
|
474
|
+
const result = await this._evaluateClause(text, "must", judge, context);
|
|
475
|
+
if (!result.passed) {
|
|
476
|
+
const action = getViolationAction(ov, `must:${text.slice(0, 30)}`);
|
|
477
|
+
violations.push({ clauseType: "must", clauseName: `must:${text.slice(0, 30)}`, clauseText: text, severity: action, actionTaken: action, judge, details: result.details });
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
for (const clause of c.must_not) {
|
|
481
|
+
const text = getClauseText(clause);
|
|
482
|
+
const judge = getClauseJudge(clause);
|
|
483
|
+
const result = await this._evaluateClause(text, "must_not", judge, context);
|
|
484
|
+
if (!result.passed) {
|
|
485
|
+
const action = getViolationAction(ov, `must_not:${text.slice(0, 30)}`);
|
|
486
|
+
violations.push({ clauseType: "must_not", clauseName: `must_not:${text.slice(0, 30)}`, clauseText: text, severity: action, actionTaken: action, judge, details: result.details });
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
for (const clause of c.ensures) {
|
|
490
|
+
const text = getClauseText(clause);
|
|
491
|
+
const judge = getClauseJudge(clause);
|
|
492
|
+
const result = await this._evaluateClause(text, "ensures", judge, context);
|
|
493
|
+
if (!result.passed) {
|
|
494
|
+
const action = getViolationAction(ov, `ensures:${text.slice(0, 30)}`);
|
|
495
|
+
violations.push({ clauseType: "ensures", clauseName: `ensures:${text.slice(0, 30)}`, clauseText: text, severity: action, actionTaken: action, judge, details: result.details });
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
const blocking = ["block", "rollback", "halt_and_alert"];
|
|
499
|
+
const passed = !violations.some((v) => blocking.includes(v.actionTaken));
|
|
500
|
+
return { passed, runId: rid, agent: c.agent, contractVersion: c.version, violations, context, outcome: passed ? "pass" : "violation" };
|
|
501
|
+
}
|
|
502
|
+
_checkLimits(context) {
|
|
503
|
+
const records = [];
|
|
504
|
+
const limits = this.contract.limits;
|
|
505
|
+
const ov = this.contract.on_violation;
|
|
506
|
+
if (limits.max_latency_ms != null) {
|
|
507
|
+
const r = new LatencyValidator("max_latency_ms", limits.max_latency_ms).validate(context);
|
|
508
|
+
if (!r.passed) {
|
|
509
|
+
const action = getViolationAction(ov, "max_latency_ms");
|
|
510
|
+
records.push({ clauseType: "limits", clauseName: "max_latency_ms", clauseText: r.clauseText, severity: action, actionTaken: action, judge: "deterministic", details: r.details });
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
if (limits.max_cost_usd != null) {
|
|
514
|
+
const r = new CostValidator("max_cost_usd", limits.max_cost_usd).validate(context);
|
|
515
|
+
if (!r.passed) {
|
|
516
|
+
const action = getViolationAction(ov, "max_cost_usd");
|
|
517
|
+
records.push({ clauseType: "limits", clauseName: "max_cost_usd", clauseText: r.clauseText, severity: action, actionTaken: action, judge: "deterministic", details: r.details });
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
if (limits.max_tokens != null && context.output) {
|
|
521
|
+
const estimated = Math.floor(context.output.length / 4);
|
|
522
|
+
if (estimated > limits.max_tokens) {
|
|
523
|
+
const action = getViolationAction(ov, "max_tokens");
|
|
524
|
+
records.push({ clauseType: "limits", clauseName: "max_tokens", clauseText: `output must not exceed ${limits.max_tokens} tokens`, severity: action, actionTaken: action, judge: "deterministic", details: `Estimated ${estimated} tokens exceeds limit of ${limits.max_tokens}` });
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return records;
|
|
528
|
+
}
|
|
529
|
+
async _runAssertion(assertion, context) {
|
|
530
|
+
switch (assertion.type) {
|
|
531
|
+
case "pattern":
|
|
532
|
+
return new PatternValidator(assertion.name, assertion.must_not_match, assertion.must_match, assertion.description).validate(context);
|
|
533
|
+
case "cost":
|
|
534
|
+
return new CostValidator(assertion.name, assertion.max_usd ?? 0, assertion.description).validate(context);
|
|
535
|
+
case "latency":
|
|
536
|
+
return new LatencyValidator(assertion.name, assertion.max_ms ?? 0, assertion.description).validate(context);
|
|
537
|
+
case "llm":
|
|
538
|
+
return new LLMValidator(assertion.name, assertion.description || assertion.name, "assert", assertion.prompt, assertion.pass_when ?? "NO", assertion.model).validate(context);
|
|
539
|
+
default:
|
|
540
|
+
return { passed: false, clauseName: assertion.name, clauseText: assertion.description || assertion.name, clauseType: "assert", judge: "deterministic", details: `Unsupported assertion type: ${assertion.type}` };
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
async _evaluateClause(text, clauseType, judge, context) {
|
|
544
|
+
if (judge === "llm") {
|
|
545
|
+
return new LLMValidator(`${clauseType}:${text.slice(0, 30)}`, text, clauseType).validate(context);
|
|
546
|
+
}
|
|
547
|
+
return { passed: true, clauseName: `${clauseType}:${text.slice(0, 30)}`, clauseText: text, clauseType, judge: "deterministic", details: "" };
|
|
548
|
+
}
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
// src/validators/base.ts
|
|
552
|
+
init_cjs_shims();
|
|
553
|
+
function makeContext(partial) {
|
|
554
|
+
return {
|
|
555
|
+
durationMs: 0,
|
|
556
|
+
costUsd: 0,
|
|
557
|
+
toolCalls: [],
|
|
558
|
+
steps: 0,
|
|
559
|
+
metadata: {},
|
|
560
|
+
...partial
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// src/enforce.ts
|
|
565
|
+
function enforce(contract, fn, options = {}) {
|
|
566
|
+
const { audit = true, auditPath = "agentcontract-audit.jsonl", costFn } = options;
|
|
567
|
+
const runner = new ContractRunner(contract);
|
|
568
|
+
return async (input) => {
|
|
569
|
+
await checkPreconditions(contract, input);
|
|
570
|
+
const start = performance.now();
|
|
571
|
+
const result = await Promise.resolve(fn(input));
|
|
572
|
+
const durationMs = performance.now() - start;
|
|
573
|
+
const output = String(result ?? "");
|
|
574
|
+
const costUsd = costFn ? costFn(result) : 0;
|
|
575
|
+
const ctx = makeContext({ input, output, durationMs, costUsd });
|
|
576
|
+
const runResult = await runner.run(ctx);
|
|
577
|
+
if (audit) {
|
|
578
|
+
const { AuditWriter: AuditWriter2 } = await Promise.resolve().then(() => (init_audit(), audit_exports));
|
|
579
|
+
new AuditWriter2(auditPath).write(runResult);
|
|
580
|
+
}
|
|
581
|
+
const warnViolations = runResult.violations.filter((v) => v.actionTaken === "warn");
|
|
582
|
+
for (const v of warnViolations) {
|
|
583
|
+
process.stderr.write(
|
|
584
|
+
`[AgentContract WARN] ${v.clauseType.toUpperCase()}: "${v.clauseText}" \u2014 ${v.details}
|
|
585
|
+
`
|
|
586
|
+
);
|
|
587
|
+
}
|
|
588
|
+
const blocking = runResult.violations.filter(
|
|
589
|
+
(v) => ["block", "rollback", "halt_and_alert"].includes(v.actionTaken)
|
|
590
|
+
);
|
|
591
|
+
if (blocking.length > 0) {
|
|
592
|
+
throw new ContractViolation(
|
|
593
|
+
blocking.map((v) => ({
|
|
594
|
+
clause_type: v.clauseType,
|
|
595
|
+
clause_text: v.clauseText,
|
|
596
|
+
action_taken: v.actionTaken
|
|
597
|
+
}))
|
|
598
|
+
);
|
|
599
|
+
}
|
|
600
|
+
return output;
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
async function checkPreconditions(contract, input) {
|
|
604
|
+
for (const precondition of contract.requires) {
|
|
605
|
+
let text;
|
|
606
|
+
let judge;
|
|
607
|
+
let onFail;
|
|
608
|
+
if (typeof precondition === "string") {
|
|
609
|
+
text = precondition;
|
|
610
|
+
judge = "deterministic";
|
|
611
|
+
onFail = "block";
|
|
612
|
+
} else {
|
|
613
|
+
text = precondition.text;
|
|
614
|
+
judge = precondition.judge;
|
|
615
|
+
onFail = precondition.on_fail;
|
|
616
|
+
}
|
|
617
|
+
let passed = true;
|
|
618
|
+
let details = "";
|
|
619
|
+
if (judge === "deterministic") {
|
|
620
|
+
if (/non-empty|not empty/i.test(text)) {
|
|
621
|
+
passed = input.trim().length > 0;
|
|
622
|
+
details = passed ? "" : "Input is empty.";
|
|
623
|
+
}
|
|
624
|
+
} else if (judge === "llm") {
|
|
625
|
+
const ctx = makeContext({ input, output: "" });
|
|
626
|
+
const result = await new LLMValidator(`requires:${text.slice(0, 30)}`, text, "requires").validate(ctx);
|
|
627
|
+
passed = result.passed;
|
|
628
|
+
details = result.details;
|
|
629
|
+
}
|
|
630
|
+
if (!passed && onFail === "block") {
|
|
631
|
+
throw new ContractPreconditionError(text, details);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
// src/index.ts
|
|
637
|
+
init_audit();
|
|
638
|
+
var VERSION = "0.1.0";
|
|
639
|
+
var SPEC_VERSION = "0.1.0";
|
|
640
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
641
|
+
0 && (module.exports = {
|
|
642
|
+
AuditWriter,
|
|
643
|
+
ContractError,
|
|
644
|
+
ContractLoadError,
|
|
645
|
+
ContractPreconditionError,
|
|
646
|
+
ContractRunner,
|
|
647
|
+
ContractViolation,
|
|
648
|
+
SPEC_VERSION,
|
|
649
|
+
VERSION,
|
|
650
|
+
enforce,
|
|
651
|
+
loadContract,
|
|
652
|
+
makeContext
|
|
653
|
+
});
|
|
654
|
+
//# sourceMappingURL=index.js.map
|