feat: Track κ — AI decision loop (decision trace + rules explain + LLM condition + rules simulate)#41
Merged
chenliuyun merged 2 commits intomainfrom May 7, 2026
Conversation
added 2 commits
May 7, 2026 12:29
PR 1 — Decision Trace - src/rules/trace.ts: deepSortedJson, ruleVersion (sha256/8-char), TraceBuilder, shouldWriteTrace (full/sampled/off), filterTraceRecords - engine.ts: fireId before eval, TraceBuilder threaded, emitTrace closure - matcher.ts: trace? param, pushConditionTrace after eval, isLlmCondition guard - audit.ts: rule-evaluate/llm-condition/llm-budget-exceeded kinds; writeEvaluateTrace - schema v0.2.json: automation.audit block (evaluate_trace, evaluate_retention_days) - tests/rules/trace.test.ts: 30 tests PR 2 — rules explain - src/rules/explain.ts: loadTraceRecords, loadRelatedAudit, formatExplainText/Json - rules.ts: trace-explain subcommand (avoids collision with static explain) - mcp.ts: rules_explain tool - tests/rules/explain.test.ts: 19 tests PR 3 — LLM condition - src/rules/types.ts: LlmCondition interface + isLlmCondition guard; AutomationLlmBudgetConfig - llm/provider.ts: decide() + DecideResult/DecideOptions on LLMProvider interface - llm/providers/anthropic.ts: decide() via tool-use API - llm/providers/openai.ts: decide() via function-calling API - src/rules/llm-condition.ts: LlmConditionEvaluator (cache, budget, on_error) Cache key: sha256(JSON.stringify([ruleVersion, prompt, deepSortedJson(ctx)])) — fixes spec gap #5 - matcher.ts: llm condition branch with provider injection - schema: llm condition oneOf branch + automation.llm_budget - engine.ts: 4 lint rules (condition-llm-no-provider, no-cache-ttl-high-freq, budget-zero, on-error-pass) - tests/rules/llm-condition.test.ts: 22 tests PR 4 — rules simulate - src/rules/simulate.ts: simulateRule(), as-of state fetcher, against-file replay, ThrottleGate simulation, LLM skip marker - rules.ts: simulate subcommand - mcp.ts: rules_simulate tool - capabilities.ts: trace-explain + simulate entries - tests/rules/simulate.test.ts: 18 tests Spec gaps fixed: #1 canonicalize undefined → deepSortedJson() recursive key-sort #5 cache key raw concat → JSON.stringify([...]) structured array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
src/rules/trace.tswithruleVersion()(sha256/8-char),TraceBuilder, andshouldWriteTracesampling modes; engine threads trace through dispatch; audit gainsrule-evaluatekindrules trace-explainCLI subcommand +rules_explainMCP tool to replay why an evaluation fired or was blocked, readingrule-evaluaterecords from the audit log{ llm: { prompt, provider, cache_ttl, budget, on_error } }condition type;decide()added toLLMProvider(Anthropic tool-use + OpenAI function-calling);LlmConditionEvaluatorwith cache (SHA-256 structured key) and hourly budget; 4 lint rulessimulateRule()replays historical events (from--againstJSONL or audit log) through the rule pipeline, reports would-fire / blocked / throttled / skipped-llm counts;rules simulateCLI +rules_simulateMCP toolSpec gaps fixed
canonicalizeundefineddeepSortedJson()intrace.ts: recursiveObject.keys().sort()+JSON.stringifysha256(JSON.stringify([ruleVersion, promptTemplate, deepSortedJson(context)]))inllm-condition.ts--no-fire-onlydouble negative--allinrules trace-explainTest plan
npm test— 2204 tests, 118 files, all passtests/rules/trace.test.ts— 30 tests (canonicalize, ruleVersion, TraceBuilder, sampling)tests/rules/explain.test.ts— 19 tests (loadTraceRecords, formatExplainText/Json)tests/rules/llm-condition.test.ts— 22 tests (cache hit/miss, budget, on_error, lint rules)tests/rules/simulate.test.ts— 18 tests (against-file replay, conditions, throttle, LLM skip)