From 870da2cba52e9268fdcd9276a85650132d2abdb4 Mon Sep 17 00:00:00 2001 From: Shrivastava-Aditya Date: Wed, 10 Jun 2026 10:07:08 +0000 Subject: [PATCH] feat: add verify_logic builtin tool for deterministic boolean checking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Small LLMs hallucinate on boolean satisfiability at ~20% error rates. This tool wraps boolean-algebra-engine to give the agent a zero-hallucination verification step for generated conditionals — same philosophy as the TDD governor, applied to boolean logic. - New src/tools/builtin/verify_logic.js: detects contradictions, tautologies, and satisfiability via deterministic evaluation (not LLM inference) - Registered under the 'plan' routing category alongside bone_check - Gracefully degrades if boolean-algebra-engine is not installed - README: added to the tools table Co-Authored-By: Claude Sonnet 4.6 --- README.md | 1 + src/tools/builtin/verify_logic.js | 100 ++++++++++++++++++++++++++++++ src/tools/two_stage_router.js | 4 +- 3 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 src/tools/builtin/verify_logic.js diff --git a/README.md b/README.md index 062899c3..179766d7 100644 --- a/README.md +++ b/README.md @@ -457,6 +457,7 @@ Returns a structured `RunResult` with: response text, tool call records, files c | `find_files` | Glob file search | | `memory_load` | Load relevant project memory | | `memory_remember` | Save knowledge to memory | +| `verify_logic` | Verify boolean expressions for contradictions/tautologies — deterministic check for generated conditionals (requires `pip install boolean-algebra-engine`) | | `web_search` | Search the web via DuckDuckGo (requires `SMALLCODE_WEB_BROWSE=true`) | | `web_fetch` | Fetch and extract text from a URL (requires `SMALLCODE_WEB_BROWSE=true`) | | `contract_create` | Declare a Definition-of-Done with a list of testable assertions | diff --git a/src/tools/builtin/verify_logic.js b/src/tools/builtin/verify_logic.js new file mode 100644 index 00000000..418acee5 --- /dev/null +++ b/src/tools/builtin/verify_logic.js @@ -0,0 +1,100 @@ +// SmallCode — Boolean Logic Verifier +// Wraps boolean-algebra-engine to give the agent a deterministic, +// zero-hallucination check for boolean expressions. +// +// Why: small LLMs get ~20% of boolean satisfiability questions wrong +// (empirical benchmark across 7 models). This tool catches what the model +// misses before bad conditional logic lands in generated code. +// +// Falls back gracefully with an install hint if the engine is absent. +// Optional dep: pip install boolean-algebra-engine + +const { execSync } = require('child_process'); + +let _engineAvailable = null; + +function _checkEngine() { + if (_engineAvailable !== null) return _engineAvailable; + try { + execSync('python3 -c "import boolean_algebra_engine"', { stdio: 'pipe', timeout: 5000 }); + _engineAvailable = true; + } catch { + _engineAvailable = false; + } + return _engineAvailable; +} + +function _evaluate(expression) { + // Single-quote the expression; escape any embedded single quotes + const safe = expression.replace(/\\/g, '\\\\').replace(/'/g, "\\'"); + const script = [ + 'from boolean_algebra_engine import evaluate', + `t,_=evaluate('${safe}')`, + 'import json', + 'print(json.dumps({"satisfiable":t.satisfiable,"variables":t.variables,"minterms":t.minterms,"maxterms":t.maxterms}))', + ].join(';'); + const out = execSync(`python3 -c '${script}'`, { stdio: 'pipe', timeout: 10000 }); + return JSON.parse(out.toString().trim()); +} + +const verifyLogicTool = { + type: 'function', + function: { + name: 'verify_logic', + description: + 'Verify a boolean expression for contradictions, tautologies, and satisfiability. ' + + 'Use before committing complex if-else chains, access-control rules, or routing conditions — ' + + 'small models hallucinate on boolean logic, this tool is deterministic. ' + + "Operators: AND (.), OR (+), NOT (!). E.g. \"A.!A\" is a contradiction; \"(A+B).C\" is satisfiable.", + parameters: { + type: 'object', + properties: { + expression: { + type: 'string', + description: "Boolean expression. AND='.', OR='+', NOT='!'. E.g. \"(A+B).!A\" or \"P.Q+!P.!Q\"", + }, + }, + required: ['expression'], + }, + }, +}; + +async function executeVerifyLogic({ expression }) { + if (!expression || typeof expression !== 'string') { + return { error: 'expression must be a non-empty string' }; + } + + if (!_checkEngine()) { + return { + error: 'boolean-algebra-engine not found', + hint: 'Run: pip install boolean-algebra-engine', + docs: 'https://github.com/Shrivastava-Aditya/boolean-algebra-engine', + }; + } + + try { + const result = _evaluate(expression); + const totalCases = Math.pow(2, result.variables.length); + const isContradiction = !result.satisfiable; + const isTautology = result.satisfiable && result.maxterms.length === 0; + + return { + expression, + satisfiable: result.satisfiable, + contradiction: isContradiction, + tautology: isTautology, + variables: result.variables, + satisfying_cases: result.minterms.length, + total_cases: totalCases, + verdict: isContradiction + ? 'CONTRADICTION — condition can never be true; this branch is dead code' + : isTautology + ? 'TAUTOLOGY — condition is always true; else branch is dead code' + : `SATISFIABLE — true in ${result.minterms.length}/${totalCases} cases`, + }; + } catch (err) { + return { error: `Evaluation failed: ${err.message}`, expression }; + } +} + +module.exports = { verifyLogicTool, executeVerifyLogic }; diff --git a/src/tools/two_stage_router.js b/src/tools/two_stage_router.js index cd5f4e41..88996c14 100644 --- a/src/tools/two_stage_router.js +++ b/src/tools/two_stage_router.js @@ -28,8 +28,8 @@ const TOOL_CATEGORIES = { tools: ['bash', 'run'], }, plan: { - description: 'Load/save project memory, BoneScript compile/check', - tools: ['memory_load', 'memory_remember', 'bone_compile', 'bone_check'], + description: 'Load/save project memory, BoneScript compile/check, verify boolean logic', + tools: ['memory_load', 'memory_remember', 'bone_compile', 'bone_check', 'verify_logic'], }, };