truthguard-ai 0.1.4 → 0.2.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.
Files changed (2) hide show
  1. package/README.md +154 -41
  2. package/package.json +4 -6
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # TruthGuard
2
2
 
3
- **Grounding validation for tool-calling AI agents.**
3
+ **Standardized grounding validation for tool-calling AI agents.**
4
4
 
5
- > Detect when an agent's response contradicts the data returned by the tools it called — without LLM-as-judge overhead.
5
+ > Detect when an agent's response contradicts the data returned by the tools it called — deterministically, without LLM-as-judge overhead.
6
6
 
7
7
  [![npm version](https://img.shields.io/npm/v/truthguard-ai.svg)](https://www.npmjs.com/package/truthguard-ai)
8
8
  [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
@@ -11,21 +11,23 @@
11
11
 
12
12
  ## The Problem
13
13
 
14
- Most "hallucinations" in tool-calling agents are **grounding failures** — the agent calls a tool, gets accurate data, then ignores it, miscalculates, or fabricates from empty results.
14
+ Most "hallucinations" in tool-calling agents are **grounding failures** — the agent calls a tool, gets accurate data, and then ignores it, miscalculates, or fabricates from empty results. The source of truth is already in the trace.
15
15
 
16
16
  ## The Solution
17
17
 
18
- TruthGuard extracts factual claims from the agent's response and cross-references them against tool outputs. Deterministic. No LLM calls. Runs in <50ms.
18
+ TruthGuard extracts factual claims from the agent's response, cross-references them against tool outputs, and reports grounding failures with standardized codes — like OBD diagnostic codes for AI.
19
19
 
20
- ```bash
20
+ ```
21
21
  npm install truthguard-ai
22
22
  ```
23
23
 
24
+ **Zero LLM calls.** 30+ deterministic failure detectors. Runs in <50ms.
25
+
24
26
  ---
25
27
 
26
- ## Quick Start
28
+ ## Quick Start — 3 Minutes
27
29
 
28
- ### Evaluate a trace
30
+ ### 1. Evaluate a trace
29
31
 
30
32
  ```typescript
31
33
  import { TraceBuilder, GroundingEngine, generateReport } from 'truthguard-ai';
@@ -37,7 +39,7 @@ const trace = new TraceBuilder({ traceId: 'run-001' })
37
39
  { employeeId: 'E01', name: 'Ana Jovic', status: 'on_leave' },
38
40
  { employeeId: 'E02', name: 'Ivan Petrovic', status: 'on_leave' },
39
41
  ])
40
- .addFinalResponse('There are 3 employees on leave today.')
42
+ .addFinalResponse('There are 3 employees on leave today.') // ← Bug: says 3, data shows 2
41
43
  .build();
42
44
 
43
45
  const engine = new GroundingEngine();
@@ -45,31 +47,28 @@ const report = engine.evaluate(trace);
45
47
 
46
48
  console.log(report.groundingScore); // 0.5
47
49
  console.log(report.detectedFailures[0]); // { type: 'grounding.data_ignored', severity: 'high' }
50
+
51
+ const { text } = generateReport(report);
52
+ console.log(text);
48
53
  ```
49
54
 
50
- ### SDK Wrappers (OpenAI & Anthropic)
55
+ ### 2. Add a CI quality gate
51
56
 
52
57
  ```typescript
53
- import { wrapOpenAI } from 'truthguard-ai';
54
- import OpenAI from 'openai';
58
+ import { loadDataset, runDataset, evaluateGate, loadGateConfig } from 'truthguard-ai';
55
59
 
56
- const openai = wrapOpenAI(new OpenAI(), {
57
- mode: 'block',
58
- threshold: 0.85,
59
- });
60
- ```
61
-
62
- ```typescript
63
- import { wrapAnthropic } from 'truthguard-ai';
64
- import Anthropic from '@anthropic-ai/sdk';
60
+ const entries = loadDataset('./test-cases.jsonl');
61
+ const result = runDataset(entries);
62
+ const gate = loadGateConfig('.ai-rcp-gate.yml');
63
+ const verdict = evaluateGate(result, gate);
65
64
 
66
- const anthropic = wrapAnthropic(new Anthropic(), {
67
- mode: 'warn',
68
- threshold: 0.80,
69
- });
65
+ if (!verdict.pass) {
66
+ console.error(verdict.report);
67
+ process.exit(1);
68
+ }
70
69
  ```
71
70
 
72
- ### Production Monitoring (Proxy Mode)
71
+ ### 3. Monitor in production (proxy mode)
73
72
 
74
73
  Works with **any language** — PHP, Python, Go, Java, Ruby, C#:
75
74
 
@@ -77,7 +76,7 @@ Works with **any language** — PHP, Python, Go, Java, Ruby, C#:
77
76
  npx truthguard-ai observe --port 3001
78
77
  ```
79
78
 
80
- Point your AI SDK base URL to the proxy:
79
+ Change your AI base URL:
81
80
  ```
82
81
  # OpenAI
83
82
  OPENAI_BASE_URL=http://localhost:3001/proxy/openai
@@ -86,17 +85,72 @@ OPENAI_BASE_URL=http://localhost:3001/proxy/openai
86
85
  ANTHROPIC_BASE_URL=http://localhost:3001/proxy/anthropic
87
86
  ```
88
87
 
89
- Your app works identically. TruthGuard evaluates grounding in the background.
88
+ Your app works exactly the same. TruthGuard transparently proxies requests and evaluates grounding in the background.
90
89
 
91
- ### CI Quality Gate
90
+ ---
92
91
 
93
- ```bash
94
- npx truthguard-ai run test-cases.jsonl --gate .ai-rcp-gate.yml
92
+ ## Detection
93
+
94
+ 30+ deterministic failure detectors across grounding, orchestration, and reasoning categories.
95
+
96
+ Examples:
97
+ - Fabrication from empty tool results
98
+ - Math errors against correct tool data
99
+ - Ignored or altered tool data in the response
100
+ - Entity mismatches and mix-ups
101
+
102
+ ---
103
+
104
+ ## Features
105
+
106
+ ### Diagnostic Advisor
107
+
108
+ Every detected failure includes actionable diagnostics — root cause identification, evidence, and remediation guidance.
109
+
110
+ ### Policy Engine
111
+
112
+ Configure per-failure actions — block, warn, or observe:
113
+
114
+ ```typescript
115
+ import { wrapOpenAI, GroundingError } from 'truthguard-ai';
116
+ import OpenAI from 'openai';
117
+
118
+ const openai = wrapOpenAI(new OpenAI(), {
119
+ mode: 'block',
120
+ threshold: 0.85,
121
+ policy: {
122
+ rules: {
123
+ 'grounding.empty_fabrication': 'block',
124
+ 'grounding.math_error': 'warn',
125
+ 'reasoning.overconfident_language': 'observe',
126
+ },
127
+ },
128
+ });
129
+ ```
130
+
131
+ ### Baseline Regression Detection
132
+
133
+ ```typescript
134
+ import { createSnapshot, saveBaseline, loadBaseline, compareToBaseline } from 'truthguard-ai';
135
+
136
+ // Save after a known-good run
137
+ const snapshot = createSnapshot(result, 'v1.2-main');
138
+ saveBaseline('.ai-rcp-baseline.json', snapshot);
139
+
140
+ // Compare after changes
141
+ const comparison = compareToBaseline(newResult, snapshot);
142
+ if (!comparison.withinTolerance) {
143
+ console.error('Regression detected:', comparison.report);
144
+ }
95
145
  ```
96
146
 
97
147
  ### MCP Server (VS Code, Cursor)
98
148
 
99
- Use TruthGuard from your IDE — add to `.vscode/mcp.json`:
149
+ Use TruthGuard directly from your IDE — no terminal needed.
150
+
151
+ **Setup (one time):**
152
+ 1. In VS Code: `Ctrl+Shift+P` → **"MCP: Open User Configuration"**
153
+ 2. Add this to `mcp.json`:
100
154
 
101
155
  ```json
102
156
  {
@@ -110,11 +164,23 @@ Use TruthGuard from your IDE — add to `.vscode/mcp.json`:
110
164
  }
111
165
  ```
112
166
 
167
+ 3. Restart VS Code
168
+
169
+ **Usage:** In Copilot Chat, say: *"Call truthguard verify_response with this trace: {...}"*
170
+
171
+ 8 tools available: `verify_response`, `quick_check`, `check_trace_quality`, `list_rules`, `get_failure_info`, `evaluate_with_policy`, `get_live_traces`, `get_trace_report`
172
+
173
+ The last two tools bridge proxy results to your IDE — ask Copilot *"Call get_live_traces"* to see recent production evaluations.
174
+
175
+ Full setup guide: [docs/getting-started.md](docs/getting-started.md#ide--mcp-server-vs-code-cursor)
176
+
113
177
  ### Express Middleware
114
178
 
115
179
  ```typescript
180
+ import express from 'express';
116
181
  import { groundingMiddleware, FileStore } from 'truthguard-ai';
117
182
 
183
+ const app = express();
118
184
  app.post('/api/chat', groundingMiddleware({
119
185
  mode: 'warn',
120
186
  store: new FileStore('./traces/grounding.jsonl'),
@@ -124,21 +190,68 @@ app.post('/api/chat', groundingMiddleware({
124
190
 
125
191
  ---
126
192
 
127
- ## Key Features
193
+ ## CLI
194
+
195
+ ```bash
196
+ npx truthguard-ai debug trace.json # Evaluate one trace
197
+ npx truthguard-ai run dataset.jsonl # Batch dataset evaluation
198
+ npx truthguard-ai run dataset.jsonl --gate gate.yml # CI quality gate
199
+ npx truthguard-ai observe --port 3001 # Start observe server + proxy
200
+ ```
201
+
202
+ ---
203
+
204
+ ## CI/CD Integration
205
+
206
+ ### GitHub Actions
207
+
208
+ ```yaml
209
+ # .github/workflows/truthguard-gate.yml
210
+ name: TruthGuard Quality Gate
211
+ on: [push, pull_request]
212
+
213
+ jobs:
214
+ grounding-gate:
215
+ runs-on: ubuntu-latest
216
+ steps:
217
+ - uses: actions/checkout@v4
218
+ - uses: actions/setup-node@v4
219
+ with:
220
+ node-version: '20'
221
+ - run: npm ci
222
+ - run: npx truthguard-ai run test-cases.jsonl --gate .ai-rcp-gate.yml
223
+ ```
128
224
 
129
- - **30+ deterministic failure detectors** — fabrication from empty results, math errors, ignored tool data, entity mismatches, and more
130
- - **Policy engine** — per-failure enforcement (block / warn / observe)
131
- - **Diagnostic reports** — actionable failure reports with severity levels
132
- - **Baseline regression** — snapshot comparison to catch quality regressions
133
- - **Multi-language support** — works with responses in 13+ languages
134
- - **Configurable thresholds** — tune sensitivity to match your use case
225
+ ### Gate config (`.ai-rcp-gate.yml`)
226
+
227
+ ```yaml
228
+ name: "Grounding Quality Gate"
229
+ assertions:
230
+ - metric: grounding_score
231
+ operator: ">="
232
+ threshold: 0.90
233
+ - metric: failure_count
234
+ operator: "<="
235
+ threshold: 0
236
+ - metric: pass_rate
237
+ operator: ">="
238
+ threshold: 1.0
239
+ ```
135
240
 
136
241
  ---
137
242
 
138
- ## Documentation
243
+ ## How It Works
139
244
 
140
- Full docs, guides, and architecture details: [truthguard.dev](https://truthguard.dev)
245
+ 1. **Extract** factual claims from the agent's response
246
+ 2. **Match** each claim against tool output data
247
+ 3. **Detect** failure patterns across grounding, orchestration, and reasoning
248
+ 4. **Score** overall grounding quality
249
+ 5. **Diagnose** root causes with actionable remediation
250
+
251
+ **No LLM calls.** 100% deterministic. Configurable tolerances and multi-language support (13+ languages).
252
+
253
+ ---
141
254
 
142
255
  ## License
143
256
 
144
- MIT
257
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "truthguard-ai",
3
- "version": "0.1.4",
3
+ "version": "0.2.0",
4
4
  "description": "TruthGuard — Standardized grounding validation for tool-calling AI agents. Detect, diagnose, and prevent grounding failures.",
5
5
  "main": "dist-npm/thin.js",
6
6
  "types": "dist-npm/thin.d.ts",
@@ -77,10 +77,13 @@
77
77
  "@types/node": "^20.12.7",
78
78
  "@typescript-eslint/eslint-plugin": "^7.7.1",
79
79
  "@typescript-eslint/parser": "^7.7.1",
80
+ "bcryptjs": "^3.0.3",
80
81
  "cors": "^2.8.6",
81
82
  "eslint": "^8.57.0",
82
83
  "express": "^5.2.1",
83
84
  "jest": "^29.7.0",
85
+ "js-yaml": "^4.1.1",
86
+ "jsonwebtoken": "^9.0.3",
84
87
  "ts-jest": "^29.1.2",
85
88
  "typescript": "^5.4.5"
86
89
  },
@@ -98,11 +101,6 @@
98
101
  "!src/index.ts"
99
102
  ]
100
103
  },
101
- "dependencies": {
102
- "bcryptjs": "^3.0.3",
103
- "js-yaml": "^4.1.1",
104
- "jsonwebtoken": "^9.0.3"
105
- },
106
104
  "peerDependencies": {
107
105
  "express": ">=4.0.0"
108
106
  },