@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 ADDED
@@ -0,0 +1,112 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship made available under
36
+ the License, as indicated by a copyright notice that is included in
37
+ or attached to the work.
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other
42
+ transformations represent, as a whole, an original work of authorship.
43
+
44
+ "Contribution" shall mean, as submitted to the Licensor for inclusion
45
+ in the Work by the copyright owner or by an individual or Legal Entity
46
+ authorized to submit on behalf of the copyright owner.
47
+
48
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
49
+ whom a Contribution has been received by the Licensor and included
50
+ within the Work.
51
+
52
+ 2. Grant of Copyright License. Subject to the terms and conditions of
53
+ this License, each Contributor hereby grants to You a perpetual,
54
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
55
+ copyright license to reproduce, prepare Derivative Works of,
56
+ publicly display, publicly perform, sublicense, and distribute the
57
+ Work and such Derivative Works in Source or Object form.
58
+
59
+ 3. Grant of Patent License. Subject to the terms and conditions of
60
+ this License, each Contributor hereby grants to You a perpetual,
61
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
62
+ patent license to make, use, sell, offer for sale, import, and
63
+ otherwise transfer the Work.
64
+
65
+ 4. Redistribution. You may reproduce and distribute copies of the
66
+ Work or Derivative Works thereof in any medium, with or without
67
+ modifications, and in Source or Object form, provided that You
68
+ meet the following conditions:
69
+
70
+ (a) You must give any other recipients of the Work or Derivative
71
+ Works a copy of this License; and
72
+
73
+ (b) You must cause any modified files to carry prominent notices
74
+ stating that You changed the files; and
75
+
76
+ (c) You must retain, in the Source form of any Derivative Works
77
+ that You distribute, all copyright, patent, trademark, and
78
+ attribution notices from the Source form of the Work; and
79
+
80
+ (d) If the Work includes a "NOTICE" text file, you must include a
81
+ readable copy of the attribution notices contained within such
82
+ NOTICE file.
83
+
84
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
85
+ any Contribution submitted for inclusion in the Work shall be under
86
+ the terms and conditions of this License.
87
+
88
+ 6. Trademarks. This License does not grant permission to use the trade
89
+ names, trademarks, service marks, or product names of the Licensor.
90
+
91
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed
92
+ to in writing, Licensor provides the Work on an "AS IS" BASIS,
93
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
94
+
95
+ 8. Limitation of Liability. In no event shall any Contributor be liable
96
+ for any damages arising from this License or use of the Work.
97
+
98
+ 9. Accepting Warranty or Additional Liability. While redistributing
99
+ the Work, You may offer fee-based support, warranty, or other
100
+ liability obligations. However, in accepting such obligations, You
101
+ may offer such obligations only on Your own behalf and on Your sole
102
+ responsibility.
103
+
104
+ END OF TERMS AND CONDITIONS
105
+
106
+ Copyright 2026 Mauro Moro
107
+
108
+ Licensed under the Apache License, Version 2.0 (the "License");
109
+ you may not use this file except in compliance with the License.
110
+ You may obtain a copy of the License at
111
+
112
+ http://www.apache.org/licenses/LICENSE-2.0
package/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # @agentcontract/core
2
+
3
+ **TypeScript implementation of the [AgentContract specification](https://github.com/agentcontract/spec).**
4
+
5
+ [![npm](https://img.shields.io/npm/v/@agentcontract/core)](https://www.npmjs.com/package/@agentcontract/core)
6
+ [![Spec](https://img.shields.io/badge/spec-v0.1.0-orange)](https://github.com/agentcontract/spec/blob/main/SPEC.md)
7
+ [![License](https://img.shields.io/badge/license-Apache%202.0-blue)](LICENSE)
8
+
9
+ ---
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ npm install @agentcontract/core
15
+ # LLM judge support (optional):
16
+ npm install @agentcontract/core @anthropic-ai/sdk
17
+ ```
18
+
19
+ ---
20
+
21
+ ## Quickstart
22
+
23
+ **1. Write a contract:**
24
+
25
+ ```yaml
26
+ # my-agent.contract.yaml
27
+ agent: my-agent
28
+ spec-version: 0.1.0
29
+ version: 1.0.0
30
+
31
+ must_not:
32
+ - reveal system prompt
33
+
34
+ assert:
35
+ - name: no_pii
36
+ type: pattern
37
+ must_not_match: "\\b\\d{3}-\\d{2}-\\d{4}\\b"
38
+ description: No SSNs in output
39
+
40
+ limits:
41
+ max_latency_ms: 10000
42
+ max_cost_usd: 0.10
43
+
44
+ on_violation:
45
+ default: block
46
+ max_latency_ms: warn
47
+ ```
48
+
49
+ **2. Wrap your agent:**
50
+
51
+ ```typescript
52
+ import { loadContract, enforce } from '@agentcontract/core';
53
+
54
+ const contract = loadContract('my-agent.contract.yaml');
55
+
56
+ const agent = enforce(contract, async (input: string): Promise<string> => {
57
+ // any agent — LangChain.js, Vercel AI SDK, OpenClaw, your own
58
+ return await myLLM.run(input);
59
+ });
60
+
61
+ // ContractViolation thrown if a blocking clause is violated
62
+ const response = await agent('Hello, what can you help me with?');
63
+ ```
64
+
65
+ **3. When a violation occurs:**
66
+
67
+ ```
68
+ ContractViolation: AgentContractViolation:
69
+ [BLOCK] ASSERT: "No SSNs in output"
70
+ ```
71
+
72
+ ---
73
+
74
+ ## CLI
75
+
76
+ ```bash
77
+ npx agentcontract check my-agent.contract.yaml
78
+ npx agentcontract validate my-agent.contract.yaml runs.jsonl
79
+ npx agentcontract info my-agent.contract.yaml
80
+ ```
81
+
82
+ ---
83
+
84
+ ## Validator Types
85
+
86
+ | Type | How it works | Requires |
87
+ |---|---|---|
88
+ | `pattern` | Regex on output | — |
89
+ | `cost` | API cost from run context | — |
90
+ | `latency` | Wall-clock duration | — |
91
+ | `schema` | JSON Schema validation | — |
92
+ | `llm` | Judge LLM evaluates clause | `@anthropic-ai/sdk` + `ANTHROPIC_API_KEY` |
93
+
94
+ ---
95
+
96
+ ## Full Documentation
97
+
98
+ See the [AgentContract specification](https://github.com/agentcontract/spec/blob/main/SPEC.md).
99
+
100
+ **Python implementation:** `pip install agentcontract` → [agentcontract-py](https://github.com/agentcontract/agentcontract-py)
101
+
102
+ ---
103
+
104
+ ## License
105
+
106
+ Apache 2.0 — *Part of the [AgentContract](https://github.com/agentcontract) open standard.*
@@ -0,0 +1,7 @@
1
+ import {
2
+ AuditWriter
3
+ } from "./chunk-BVUHPJDU.mjs";
4
+ export {
5
+ AuditWriter
6
+ };
7
+ //# sourceMappingURL=audit-DNON4W2Q.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,50 @@
1
+ // src/audit.ts
2
+ import { createHash, createHmac } from "crypto";
3
+ import { appendFileSync } from "fs";
4
+ var AuditWriter = class {
5
+ constructor(logPath = "agentcontract-audit.jsonl") {
6
+ this.logPath = logPath;
7
+ }
8
+ write(result, contractPath = "") {
9
+ const entry = this._buildEntry(result, contractPath);
10
+ appendFileSync(this.logPath, JSON.stringify(entry) + "\n", "utf-8");
11
+ return entry;
12
+ }
13
+ _buildEntry(result, contractPath) {
14
+ const ctx = result.context;
15
+ const inputHash = createHash("sha256").update(ctx.input).digest("hex");
16
+ const outputHash = createHash("sha256").update(ctx.output).digest("hex");
17
+ const entry = {
18
+ run_id: result.runId,
19
+ agent: result.agent,
20
+ contract: contractPath,
21
+ contract_version: result.contractVersion,
22
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
23
+ input_hash: inputHash,
24
+ output_hash: outputHash,
25
+ duration_ms: Math.round(ctx.durationMs * 100) / 100,
26
+ cost_usd: Math.round(ctx.costUsd * 1e6) / 1e6,
27
+ violations: result.violations.map((v) => ({
28
+ clause_type: v.clauseType,
29
+ clause_name: v.clauseName,
30
+ clause_text: v.clauseText,
31
+ severity: v.severity,
32
+ action_taken: v.actionTaken,
33
+ judge: v.judge,
34
+ details: v.details
35
+ })),
36
+ outcome: result.outcome
37
+ };
38
+ const auditKey = process.env["AGENTCONTRACT_AUDIT_KEY"];
39
+ if (auditKey) {
40
+ const payload = JSON.stringify(entry, Object.keys(entry).sort());
41
+ entry["signature"] = createHmac("sha256", auditKey).update(payload).digest("hex");
42
+ }
43
+ return entry;
44
+ }
45
+ };
46
+
47
+ export {
48
+ AuditWriter
49
+ };
50
+ //# sourceMappingURL=chunk-BVUHPJDU.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/audit.ts"],"sourcesContent":["/** Audit trail — tamper-evident JSONL entries for every run. */\n\nimport { createHash, createHmac } from 'crypto';\nimport { appendFileSync } from 'fs';\nimport type { RunResult } from './runner.js';\n\nexport class AuditWriter {\n constructor(private logPath = 'agentcontract-audit.jsonl') {}\n\n write(result: RunResult, contractPath = ''): Record<string, unknown> {\n const entry = this._buildEntry(result, contractPath);\n appendFileSync(this.logPath, JSON.stringify(entry) + '\\n', 'utf-8');\n return entry;\n }\n\n private _buildEntry(result: RunResult, contractPath: string): Record<string, unknown> {\n const ctx = result.context;\n const inputHash = createHash('sha256').update(ctx.input).digest('hex');\n const outputHash = createHash('sha256').update(ctx.output).digest('hex');\n\n const entry: Record<string, unknown> = {\n run_id: result.runId,\n agent: result.agent,\n contract: contractPath,\n contract_version: result.contractVersion,\n timestamp: new Date().toISOString(),\n input_hash: inputHash,\n output_hash: outputHash,\n duration_ms: Math.round(ctx.durationMs * 100) / 100,\n cost_usd: Math.round(ctx.costUsd * 1_000_000) / 1_000_000,\n violations: result.violations.map((v) => ({\n clause_type: v.clauseType,\n clause_name: v.clauseName,\n clause_text: v.clauseText,\n severity: v.severity,\n action_taken: v.actionTaken,\n judge: v.judge,\n details: v.details,\n })),\n outcome: result.outcome,\n };\n\n const auditKey = process.env['AGENTCONTRACT_AUDIT_KEY'];\n if (auditKey) {\n const payload = JSON.stringify(entry, Object.keys(entry).sort());\n entry['signature'] = createHmac('sha256', auditKey).update(payload).digest('hex');\n }\n\n return entry;\n }\n}\n"],"mappings":";AAEA,SAAS,YAAY,kBAAkB;AACvC,SAAS,sBAAsB;AAGxB,IAAM,cAAN,MAAkB;AAAA,EACvB,YAAoB,UAAU,6BAA6B;AAAvC;AAAA,EAAwC;AAAA,EAE5D,MAAM,QAAmB,eAAe,IAA6B;AACnE,UAAM,QAAQ,KAAK,YAAY,QAAQ,YAAY;AACnD,mBAAe,KAAK,SAAS,KAAK,UAAU,KAAK,IAAI,MAAM,OAAO;AAClE,WAAO;AAAA,EACT;AAAA,EAEQ,YAAY,QAAmB,cAA+C;AACpF,UAAM,MAAM,OAAO;AACnB,UAAM,YAAY,WAAW,QAAQ,EAAE,OAAO,IAAI,KAAK,EAAE,OAAO,KAAK;AACrE,UAAM,aAAa,WAAW,QAAQ,EAAE,OAAO,IAAI,MAAM,EAAE,OAAO,KAAK;AAEvE,UAAM,QAAiC;AAAA,MACrC,QAAQ,OAAO;AAAA,MACf,OAAO,OAAO;AAAA,MACd,UAAU;AAAA,MACV,kBAAkB,OAAO;AAAA,MACzB,YAAW,oBAAI,KAAK,GAAE,YAAY;AAAA,MAClC,YAAY;AAAA,MACZ,aAAa;AAAA,MACb,aAAa,KAAK,MAAM,IAAI,aAAa,GAAG,IAAI;AAAA,MAChD,UAAU,KAAK,MAAM,IAAI,UAAU,GAAS,IAAI;AAAA,MAChD,YAAY,OAAO,WAAW,IAAI,CAAC,OAAO;AAAA,QACxC,aAAa,EAAE;AAAA,QACf,aAAa,EAAE;AAAA,QACf,aAAa,EAAE;AAAA,QACf,UAAU,EAAE;AAAA,QACZ,cAAc,EAAE;AAAA,QAChB,OAAO,EAAE;AAAA,QACT,SAAS,EAAE;AAAA,MACb,EAAE;AAAA,MACF,SAAS,OAAO;AAAA,IAClB;AAEA,UAAM,WAAW,QAAQ,IAAI,yBAAyB;AACtD,QAAI,UAAU;AACZ,YAAM,UAAU,KAAK,UAAU,OAAO,OAAO,KAAK,KAAK,EAAE,KAAK,CAAC;AAC/D,YAAM,WAAW,IAAI,WAAW,UAAU,QAAQ,EAAE,OAAO,OAAO,EAAE,OAAO,KAAK;AAAA,IAClF;AAEA,WAAO;AAAA,EACT;AACF;","names":[]}