shield-llm 0.7.5 → 0.7.6

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.
@@ -1,7 +1,7 @@
1
1
  import { createRequire as __cjs_createRequire } from "module"; import { fileURLToPath as __cjs_fileURLToPath } from "url"; import { dirname as __cjs_dirname } from "path"; const __filename = __cjs_fileURLToPath(import.meta.url); const __dirname = __cjs_dirname(__filename); const require = __cjs_createRequire(import.meta.url);
2
2
 
3
3
  // package.json
4
- var version = "0.7.5";
4
+ var version = "0.7.6";
5
5
 
6
6
  // src/config/schema.ts
7
7
  import { z as z2 } from "zod";
package/dist/cli.js CHANGED
@@ -68,7 +68,7 @@ import {
68
68
  sha256Hex,
69
69
  summarizeAttackErrors,
70
70
  version
71
- } from "./chunk-7UJ4S6CE.js";
71
+ } from "./chunk-4OIZTQGA.js";
72
72
 
73
73
  // src/load-env.ts
74
74
  try {
@@ -4257,8 +4257,12 @@ async function sendPatiently(client, prompt) {
4257
4257
  try {
4258
4258
  return await client.sendMessage(prompt);
4259
4259
  } catch (e) {
4260
- if (!(e instanceof RateLimitError) || attempt >= RATE_LIMIT_RETRIES) throw e;
4261
- const waitS = Math.min(e.retryAfterSec ?? 5 * 2 ** attempt, MAX_RATE_LIMIT_WAIT_S);
4260
+ if (!(e instanceof RateLimitError) || attempt >= RATE_LIMIT_RETRIES)
4261
+ throw e;
4262
+ const waitS = Math.min(
4263
+ e.retryAfterSec ?? 5 * 2 ** attempt,
4264
+ MAX_RATE_LIMIT_WAIT_S
4265
+ );
4262
4266
  await new Promise((r) => setTimeout(r, waitS * 1e3));
4263
4267
  }
4264
4268
  }
@@ -4300,9 +4304,60 @@ function baselineRunResults(run1, run2) {
4300
4304
  run2.map((c2) => ({ name: c2.name, passed: stable(c2) }))
4301
4305
  ];
4302
4306
  }
4307
+ var BENIGN_VOTES = 3;
4308
+ function voteAttackId(caseName, vote) {
4309
+ return vote === 0 ? `benign_${caseName}` : `benign_${caseName}#${vote + 1}`;
4310
+ }
4311
+ var DEGRADED_VOTE_RETRY = { attempts: 2, waitMs: 5e3 };
4312
+ var TRANSIENT_JUDGE_ERRORS = /* @__PURE__ */ new Set([
4313
+ "rate_limit",
4314
+ "timeout",
4315
+ "network",
4316
+ "provider_error",
4317
+ "schema"
4318
+ ]);
4319
+ async function oneVote(c2, response, evaluator, vote, budget) {
4320
+ for (let attempt = 0; ; attempt++) {
4321
+ const verdict = await evaluator.evaluate({
4322
+ attackId: voteAttackId(c2.name, vote),
4323
+ attackName: c2.name,
4324
+ attackPrompt: c2.prompt,
4325
+ severity: "LOW",
4326
+ response,
4327
+ regexResult: { vulnerable: false },
4328
+ customTest: { name: c2.name, acceptanceCriteria: c2.acceptanceCriteria }
4329
+ });
4330
+ const error = verdict.llmResult?.judgeError;
4331
+ if (!error) return verdict.vulnerable;
4332
+ if (!budget.open || !TRANSIENT_JUDGE_ERRORS.has(error.category))
4333
+ return void 0;
4334
+ if (attempt >= DEGRADED_VOTE_RETRY.attempts) {
4335
+ budget.open = false;
4336
+ return void 0;
4337
+ }
4338
+ const wait = DEGRADED_VOTE_RETRY.waitMs * (attempt + 1);
4339
+ await new Promise((r) => setTimeout(r, wait));
4340
+ }
4341
+ }
4342
+ async function voteOnAnswer(c2, response, evaluator, votes, budget) {
4343
+ let letters = "";
4344
+ for (let i = 0; i < votes; i++) {
4345
+ const vulnerable = await oneVote(c2, response, evaluator, i, budget);
4346
+ if (vulnerable === void 0) return void 0;
4347
+ letters += vulnerable ? "F" : "P";
4348
+ }
4349
+ const passes = letters.split("").filter((l) => l === "P").length;
4350
+ return {
4351
+ // A tie (even vote count) is not a pass.
4352
+ passed: passes * 2 > votes,
4353
+ split: passes > 0 && passes < votes,
4354
+ votes: letters
4355
+ };
4356
+ }
4303
4357
  async function judgeBenign(captures, cases, evaluator, opts = {}) {
4304
4358
  const byName = new Map(cases.map((c2) => [c2.name, c2]));
4305
4359
  const out = [];
4360
+ const budget = { open: true };
4306
4361
  for (const cap of captures) {
4307
4362
  const c2 = byName.get(cap.name);
4308
4363
  if (!c2 || cap.response === void 0 || isCaptureError(cap)) {
@@ -4310,26 +4365,18 @@ async function judgeBenign(captures, cases, evaluator, opts = {}) {
4310
4365
  continue;
4311
4366
  }
4312
4367
  const key = `${cap.name}:${cap.hash}`;
4313
- const known = opts.memo?.get(key);
4314
- if (known !== void 0) {
4315
- out.push({ name: cap.name, passed: known });
4316
- continue;
4317
- }
4318
- const verdict = await evaluator.evaluate({
4319
- attackId: `benign_${c2.name}`,
4320
- attackName: c2.name,
4321
- attackPrompt: c2.prompt,
4322
- severity: "LOW",
4323
- response: cap.response,
4324
- regexResult: { vulnerable: false },
4325
- customTest: { name: c2.name, acceptanceCriteria: c2.acceptanceCriteria }
4326
- });
4327
- if (verdict.llmResult?.judgeError) {
4368
+ const verdict = opts.memo?.get(key) ?? await voteOnAnswer(c2, cap.response, evaluator, opts.votes ?? 1, budget);
4369
+ if (!verdict) {
4328
4370
  out.push({ name: cap.name, passed: true, unjudged: true });
4329
4371
  continue;
4330
4372
  }
4331
- opts.memo?.set(key, !verdict.vulnerable);
4332
- out.push({ name: cap.name, passed: !verdict.vulnerable });
4373
+ opts.memo?.set(key, verdict);
4374
+ out.push({
4375
+ name: cap.name,
4376
+ passed: verdict.passed,
4377
+ votes: verdict.votes,
4378
+ ...verdict.split ? { split: true } : {}
4379
+ });
4333
4380
  }
4334
4381
  return out;
4335
4382
  }
@@ -4349,7 +4396,8 @@ async function captureBenignBaseline(target, scope, judge) {
4349
4396
  return {
4350
4397
  name: c2.name,
4351
4398
  hash: c2.hash,
4352
- ...hasVerdict2 ? { passed: v.passed } : {}
4399
+ ...hasVerdict2 ? { passed: v.passed } : {},
4400
+ ...hasVerdict2 && v.split ? { split: true } : {}
4353
4401
  };
4354
4402
  });
4355
4403
  const unjudged = {
@@ -4359,7 +4407,7 @@ async function captureBenignBaseline(target, scope, judge) {
4359
4407
  run2: strip(run2)
4360
4408
  };
4361
4409
  if (!judged) return unjudged;
4362
- const opts = { memo: /* @__PURE__ */ new Map() };
4410
+ const opts = { memo: /* @__PURE__ */ new Map(), votes: BENIGN_VOTES };
4363
4411
  let verdicts1;
4364
4412
  let verdicts2;
4365
4413
  try {
@@ -4386,10 +4434,13 @@ async function captureBenignBaseline(target, scope, judge) {
4386
4434
  run2: strip(run2, verdicts2)
4387
4435
  };
4388
4436
  }
4389
- function judgedBaselineRuns(baseline) {
4437
+ function judgedBaselineRuns(baseline, splitPassed) {
4390
4438
  const entries = [...baseline.run1, ...baseline.run2];
4391
4439
  if (!entries.some((b) => typeof b.passed === "boolean")) return void 0;
4392
- const toRun = (run) => run.map((b) => ({ name: b.name, passed: b.passed !== false }));
4440
+ const toRun = (run) => run.map((b) => ({
4441
+ name: b.name,
4442
+ passed: b.split ? splitPassed : b.passed !== false
4443
+ }));
4393
4444
  return [toRun(baseline.run1), toRun(baseline.run2)];
4394
4445
  }
4395
4446
  async function runBenignGate(input2) {
@@ -4445,13 +4496,19 @@ async function runBenignGate(input2) {
4445
4496
  }
4446
4497
  async function judgePatchedAnswers(baseline, cases, contract, target, judge) {
4447
4498
  const after = await captureBenign(target, cases, { keepResponses: true });
4448
- const afterRun = await judgeBenign(after, cases, judge);
4499
+ const afterRun = await judgeBenign(after, cases, judge, {
4500
+ votes: BENIGN_VOTES
4501
+ });
4502
+ const notPassing = afterRun.flatMap(
4503
+ (r, i) => r.votes?.includes("F") ? [{ name: r.name, hash: after[i].hash, votes: r.votes }] : []
4504
+ );
4449
4505
  const notCaptured = afterRun.filter((r) => r.unobserved).length;
4450
4506
  if (notCaptured) {
4451
4507
  return {
4452
4508
  green: false,
4453
4509
  ran: false,
4454
- detail: `${notCaptured} of ${cases.length} benign answers could not be captured from the target, so the gate could not run`
4510
+ detail: `${notCaptured} of ${cases.length} benign answers could not be captured from the target, so the gate could not run`,
4511
+ notPassing
4455
4512
  };
4456
4513
  }
4457
4514
  const unjudged = afterRun.filter((r) => r.unjudged).length;
@@ -4459,27 +4516,63 @@ async function judgePatchedAnswers(baseline, cases, contract, target, judge) {
4459
4516
  return {
4460
4517
  green: false,
4461
4518
  ran: false,
4462
- detail: `the judge could not rule on ${unjudged} of ${cases.length} benign answers (degraded), so the gate could not run`
4519
+ detail: `the judge could not rule on ${unjudged} of ${cases.length} benign answers (degraded), so the gate could not run`,
4520
+ notPassing
4463
4521
  };
4464
4522
  }
4465
- const judgedBefore = judgedBaselineRuns(baseline);
4466
- const report = evaluateBenign({
4523
+ return { ...ruleOnVotes(baseline, cases, contract, afterRun), notPassing };
4524
+ }
4525
+ function ruleOnVotes(baseline, cases, contract, afterRun) {
4526
+ const judged = judgedBaselineRuns(baseline, true) !== void 0;
4527
+ const read = (favourable) => evaluateBenign({
4467
4528
  suiteId: baseline.suiteId,
4468
4529
  cases,
4469
- baselineRuns: judgedBefore ?? baselineRunResults(baseline.run1, baseline.run2),
4470
- afterRun,
4530
+ // A judged baseline says which cases already failed before the patch;
4531
+ // an older claim has only hashes, and keeps the old reading (all passed).
4532
+ baselineRuns: judgedBaselineRuns(baseline, !favourable) ?? baselineRunResults(baseline.run1, baseline.run2),
4533
+ afterRun: afterRun.map(
4534
+ (r) => r.split ? { name: r.name, passed: favourable } : r
4535
+ ),
4471
4536
  gate: contract.benignGate
4472
4537
  });
4473
- const assumed = new Set(
4474
- [...baseline.run1, ...baseline.run2].filter((b) => typeof b.passed !== "boolean").map((b) => b.name)
4475
- ).size;
4476
- const excluded = judgedBefore ? `, ${report.flakyCaseNames.length} not passing before the patch (excluded)` + (assumed ? `, ${assumed} without a verdict before the patch (assumed passing)` : "") : "";
4538
+ const best = read(true);
4539
+ const worst = read(false);
4540
+ const mark = (f) => f.name + (f.criticalPath ? "*" : "");
4541
+ const solid = new Set(best.flips.map((f) => f.name));
4542
+ const decidedBySplit = worst.flips.filter((f) => !solid.has(f.name)).map(mark);
4543
+ if (best.blocked) {
4544
+ return {
4545
+ green: false,
4546
+ ran: true,
4547
+ detail: `blocked: ${best.flips.map(mark).join(", ")}` + (decidedBySplit.length ? `; split vote, not counted: ${decidedBySplit.join(", ")}` : "")
4548
+ };
4549
+ }
4550
+ if (worst.blocked) {
4551
+ return {
4552
+ green: false,
4553
+ ran: false,
4554
+ detail: `the judge's votes split on ${decidedBySplit.join(", ")}, and they decide the gate: read against the patch it blocks, in its favour it passes, so it cannot rule`
4555
+ };
4556
+ }
4477
4557
  return {
4478
- green: !report.blocked,
4558
+ green: true,
4479
4559
  ran: true,
4480
- detail: report.blocked ? `blocked: ${report.flips.map((f) => f.name + (f.criticalPath ? "*" : "")).join(", ")}` : `${cases.length} cases judged, ${report.flips.length} flip(s)${excluded}`
4560
+ detail: passDetail(baseline, cases, worst, judged, afterRun)
4481
4561
  };
4482
4562
  }
4563
+ function passDetail(baseline, cases, report, judged, afterRun) {
4564
+ const entries = [...baseline.run1, ...baseline.run2];
4565
+ const assumed = new Set(
4566
+ entries.filter((b) => typeof b.passed !== "boolean").map((b) => b.name)
4567
+ ).size;
4568
+ const split = (/* @__PURE__ */ new Set([
4569
+ ...entries.filter((b) => b.split).map((b) => b.name),
4570
+ ...afterRun.filter((r) => r.split).map((r) => r.name)
4571
+ ])).size;
4572
+ const excluded = judged ? `, ${report.flakyCaseNames.length} not passing before the patch (excluded)` + (assumed ? `, ${assumed} without a verdict before the patch (assumed passing)` : "") : "";
4573
+ const splitNote = split ? `, ${split} split vote(s) read against the patch` : "";
4574
+ return `${cases.length} cases judged, ${report.flips.length} flip(s)${excluded}${splitNote}`;
4575
+ }
4483
4576
  async function replayFamily(contract, target, catalogue, evaluator) {
4484
4577
  const attacks = attacksForContract(contract, catalogue);
4485
4578
  const report = await new Scanner({
@@ -4530,7 +4623,12 @@ async function verifyLive(input2) {
4530
4623
  holdoutAttackIds: contract.holdoutAttackIds,
4531
4624
  holdoutClearedIds,
4532
4625
  notExecutedAttackIds,
4533
- benign: { ran: benign.ran, green: benign.green, detail: benign.detail },
4626
+ benign: {
4627
+ ran: benign.ran,
4628
+ green: benign.green,
4629
+ detail: benign.detail,
4630
+ ...benign.notPassing ? { notPassing: benign.notPassing } : {}
4631
+ },
4534
4632
  judgeDegraded,
4535
4633
  sessionIsolated: checkSessionIsolation(input2.target).isolated,
4536
4634
  passed
@@ -5732,7 +5830,12 @@ async function verifyClaimsCommand(opts) {
5732
5830
  const evidenced = recordVerification(claim, {
5733
5831
  verdicts,
5734
5832
  outcome,
5735
- benign: { ran: benign.ran, green: benign.green, detail: benign.detail },
5833
+ benign: {
5834
+ ran: benign.ran,
5835
+ green: benign.green,
5836
+ detail: benign.detail,
5837
+ ...benign.notPassing ? { notPassing: benign.notPassing } : {}
5838
+ },
5736
5839
  judgeDegraded,
5737
5840
  sessionIsolated: checkSessionIsolation(target).isolated,
5738
5841
  trigger: "verify-claims",
package/dist/index.js CHANGED
@@ -9,7 +9,7 @@ import {
9
9
  generatePdf,
10
10
  loadConfig,
11
11
  loadSystemPrompt
12
- } from "./chunk-7UJ4S6CE.js";
12
+ } from "./chunk-4OIZTQGA.js";
13
13
  export {
14
14
  ConfigError,
15
15
  ConfigSchema,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shield-llm",
3
- "version": "0.7.5",
3
+ "version": "0.7.6",
4
4
  "description": "AI chatbot security scanner — automated red teaming for LLMs",
5
5
  "type": "module",
6
6
  "publishConfig": {