Skip to content

3.4.0: AI decision loop (Track κ)#42

Merged
chenliuyun merged 3 commits intomainfrom
worktree-feat+track-kappa-decision-loop
May 7, 2026
Merged

3.4.0: AI decision loop (Track κ)#42
chenliuyun merged 3 commits intomainfrom
worktree-feat+track-kappa-decision-loop

Conversation

@chenliuyun
Copy link
Copy Markdown
Collaborator

Summary

  • Decision traceautomation.audit.evaluate_trace records per-evaluation condition breakdown, LLM results, and throttle state to the audit log
  • LLM condition (llm:) — gates rule execution on an AI yes/no judgement with cache, budget, and on_error controls
  • rules trace-explain — inspect why a specific rule evaluation fired or was blocked (by fire ID, rule name, or time window)
  • rules simulate — replay historical events against a rule offline without starting the live engine
  • MCP toolsrules_explain + rules_simulate added to the MCP server
  • Docs & version — README updated (LLM condition, trace-explain, simulate, MCP tools, 2204 test count); CHANGELOG [3.4.0] entry; package.json bumped to 3.4.0

Test plan

  • npm test — 2204 tests pass
  • switchbot rules trace-explain --rule <name> --last — returns trace record after running engine with evaluate_trace: sampled
  • switchbot rules simulate <rule> — reports would-fire / blocked counts from audit log
  • Rule with llm: condition fires correctly when LLM returns yes; blocked when no
  • rules lint flags LLM condition with missing provider key
  • MCP rules_explain and rules_simulate tools respond correctly via switchbot mcp serve

chenliuyun added 3 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
- automation.audit.evaluate_trace records per-evaluation decisions
- llm: condition type gates rules on LLM yes/no judgement (cache, budget, on_error)
- rules trace-explain: inspect why a rule fired or was blocked
- rules simulate: replay historical events against a rule offline
- MCP: rules_explain + rules_simulate tools
- 2204 tests (+245)
@chenliuyun chenliuyun merged commit ad2e238 into main May 7, 2026
11 checks passed
@chenliuyun chenliuyun deleted the worktree-feat+track-kappa-decision-loop branch May 7, 2026 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant