Bayesian Reasoning Agents
Three-agent system for hypothesis-driven development using Bayesian reasoning and git worktrees.
Architecture Overview
┌─────────────────────────────────────────────────────────────────────────┐
│ Bayesian Orchestrator │
├───────────────────┬─────────────────────┬───────────────────────────────┤
│ Agent 1: │ Agent 2: │ Agent 3: │
│ Hypothesis │ Evidence │ Backwards │
│ Generator │ Evaluator │ Reasoner │
├───────────────────┼─────────────────────┼───────────────────────────────┤
│ - Hypotheses │ - Evaluate evidence │ - P(Cause|Effect) │
│ - Experiments │ - Update beliefs │ - Conditional variables │
│ - Predictions │ - Verify results │ - Causal graphs │
│ - WT Schemas │ - Create worktrees │ - Suggest hypotheses │
└───────────────────┴─────────────────────┴───────────────────────────────┘
│ │ │
▼ ▼ ▼
worktree_schemas/ worktrees/branch-* Causal Network
Quick Start
bash
1uv add numpy scipy pydantic
python
1from agents.bayesian import (
2 BayesianOrchestrator,
3 OrchestratorConfig,
4)
5
6# Initialize orchestrator with all three agents
7orchestrator = BayesianOrchestrator()
8
9# Set up causal network for backwards reasoning
10orchestrator.setup_causal_network(
11 variables=[
12 {"name": "bug_in_code", "description": "A bug exists in the code"},
13 {"name": "test_failure", "description": "Tests are failing"},
14 {"name": "recent_change", "description": "Recent code change"},
15 ],
16 causal_links=[
17 {"cause": "bug_in_code", "effect": "test_failure", "strength": 0.8},
18 {"cause": "recent_change", "effect": "bug_in_code", "strength": 0.6},
19 ]
20)
21
22# Reason from observation
23result = orchestrator.reason_from_observation(
24 observation="Tests started failing after deployment",
25 observation_vars={"test_failure": "true"}
26)
27
28print(result["hypotheses"])
Agent 1: Hypothesis Generator
Responsibilities:
- Generate testable hypotheses from observations
- Assign prior probabilities using domain knowledge
- Design experiments with success criteria
- Create worktree schemas for parallel exploration
Key Methods:
python
1from agents.bayesian import HypothesisGeneratorAgent
2
3generator = HypothesisGeneratorAgent()
4
5# Generate a hypothesis with prior probability
6hypothesis = generator.generate_hypothesis(
7 statement="The performance regression is caused by the new caching layer",
8 rationale="Caching was recently modified and correlates with slowdown",
9 predictions=["Disabling cache improves performance", "Cache hit ratio is abnormal"],
10 prior=0.6, # 60% prior probability
11)
12
13# Design experiment to test hypothesis
14experiment = generator.design_experiment(
15 hypothesis=hypothesis,
16 description="Benchmark with cache disabled",
17 expected_outcome="Latency returns to baseline",
18 success_criteria="p95 latency < 100ms",
19)
20
21# Create worktree schema for parallel exploration
22schema = generator.create_worktree_schema(hypothesis)
Agent 2: Evidence Evaluator
Responsibilities:
- Evaluate experimental evidence
- Update beliefs using Bayes' theorem: P(H|E) = P(E|H) * P(H) / P(E)
- Form refined hypotheses when results are inconclusive
- Create git worktrees from schemas for parallel exploration
Key Methods:
python
1from agents.bayesian import EvidenceEvaluatorAgent
2
3evaluator = EvidenceEvaluatorAgent()
4
5# Evaluate evidence from experiment
6evidence = evaluator.evaluate_evidence(
7 experiment=experiment,
8 observation="Latency improved to 85ms with cache disabled",
9 matches_prediction=True,
10 strength=0.8, # Strong evidence
11)
12
13# Update hypothesis belief using Bayes' theorem
14updated_belief = evaluator.update_belief(hypothesis, evidence)
15print(f"Prior: {hypothesis.belief.prior:.2%}")
16print(f"Posterior: {updated_belief.posterior:.2%}")
17
18# Create worktree from schema
19worktree_path = evaluator.create_worktree_from_schema(schema)
Agent 3: Backwards Reasoner
Responsibilities:
- Calculate inverse conditional probabilities P(Cause|Effect)
- Find conditional variables for reasoning
- Build and query causal graphs
- Suggest hypotheses based on most likely causes
Key Methods:
python
1from agents.bayesian import BackwardsReasonerAgent
2
3reasoner = BackwardsReasonerAgent()
4
5# Add variables to causal network
6reasoner.add_variable("bug_in_code", description="A bug exists")
7reasoner.add_variable("test_failure", description="Tests are failing")
8reasoner.add_variable("memory_leak", description="Memory leak present")
9
10# Add causal links with conditional probabilities
11reasoner.add_causal_link(
12 cause="bug_in_code",
13 effect="test_failure",
14 strength=0.8,
15 conditional_probs={
16 ("true", "true"): 0.85, # P(test_fail|bug) = 0.85
17 ("true", "false"): 0.15,
18 ("false", "true"): 0.1, # P(test_fail|no_bug) = 0.1
19 ("false", "false"): 0.9,
20 }
21)
22
23# Calculate backwards probability
24prob = reasoner.calculate_backwards_probability(
25 cause_var="bug_in_code",
26 cause_value="true",
27 effect_var="test_failure",
28 effect_value="true",
29)
30print(f"P(bug|test_failure) = {prob:.2%}")
31
32# Query for most likely causes of an effect
33query = reasoner.query_causes("test_failure", "true")
34print(query.results) # {"bug_in_code": 0.85, "memory_leak": 0.2, ...}
35
36# Find conditional variables for reasoning
37relevant = reasoner.find_conditional_variables(
38 target_var="test_failure",
39 observed_vars=["recent_change"]
40)
41
42# Suggest hypotheses based on backwards reasoning
43suggestions = reasoner.suggest_hypotheses_from_effect("test_failure", "true")
json
1{
2 "schema_id": "wts-a1b2c3d4",
3 "hypothesis": "Feature X causes performance regression",
4 "hypothesis_id": "hyp-x1y2z3",
5 "branch_name": "experiment/hyp-x1y2z3-feature-x-causes-perfo",
6 "worktree_path": null,
7 "prior_probability": 0.6,
8 "posterior_probability": null,
9 "experiments": [
10 {
11 "id": "exp-001",
12 "description": "Benchmark with Feature X disabled",
13 "expected_outcome": "Performance returns to baseline",
14 "success_criteria": "p95 < 100ms",
15 "status": "designed"
16 }
17 ],
18 "evidence_collected": [],
19 "status": "pending",
20 "created_at": "2026-01-30T12:00:00Z"
21}
Full Workflow Example
python
1from agents.bayesian import BayesianOrchestrator
2
3# Initialize
4orchestrator = BayesianOrchestrator()
5
6# Step 1: Set up causal network (Agent 3)
7orchestrator.setup_causal_network(
8 variables=[
9 {"name": "cache_bug", "description": "Bug in caching layer"},
10 {"name": "db_slow", "description": "Database queries slow"},
11 {"name": "high_latency", "description": "High API latency observed"},
12 ],
13 causal_links=[
14 {"cause": "cache_bug", "effect": "high_latency", "strength": 0.7},
15 {"cause": "db_slow", "effect": "high_latency", "strength": 0.8},
16 ]
17)
18
19# Step 2: Reason from observation (uses all 3 agents)
20result = orchestrator.reason_from_observation(
21 observation="API latency increased by 50%",
22 observation_vars={"high_latency": "true"}
23)
24
25# Step 3: Get backwards analysis
26print("Most likely causes:", result["backwards_analysis"]["combined_most_likely_causes"])
27
28# Step 4: Hypotheses were auto-generated
29for h in result["hypotheses"]:
30 print(f"Hypothesis: {h['statement']} (prior: {h['prior']:.2%})")
31
32# Step 5: Run experiments and evaluate results
33eval_result = orchestrator.evaluate_experiment_result(
34 experiment_id=result["hypotheses"][0]["id"] + "_exp",
35 observation="Cache hit ratio dropped to 40%",
36 matches_prediction=True,
37 strength=0.75,
38)
39
40print(f"Posterior: {eval_result['posterior']:.2%}")
41print(f"Status: {eval_result['status']}")
42
43# Step 6: Create worktrees for parallel exploration
44worktrees = orchestrator.create_all_worktrees()
45
46# Step 7: Export report
47orchestrator.export_report()
Bayesian Belief Update
The system uses Bayes' theorem to update beliefs:
P(H|E) = P(E|H) * P(H) / P(E)
Where:
- P(H|E) = Posterior probability of hypothesis given evidence
- P(E|H) = Likelihood of evidence if hypothesis is true
- P(H) = Prior probability of hypothesis
- P(E) = Marginal probability of evidence
Example update:
python
1# Prior: 50% chance hypothesis is true
2# Evidence strongly supports hypothesis (strength=0.8)
3
4belief = BayesianBelief(prior=0.5)
5updated = belief.update(evidence_supports=True, strength=0.8)
6
7# Posterior: ~80% chance hypothesis is true
8print(f"Posterior: {updated.posterior:.2%}")
Backwards Reasoning (Abduction)
Agent 3 performs abductive reasoning - inferring causes from effects:
Given: Effect E is observed
Find: Most likely Cause C
P(C|E) = P(E|C) * P(C) / P(E)
Where P(E) = Σ P(E|C_i) * P(C_i) for all possible causes
Example:
python
1# Observation: Tests are failing
2# Question: What caused this?
3
4reasoner.add_causal_link("bug", "test_failure", strength=0.8)
5reasoner.add_causal_link("flaky_test", "test_failure", strength=0.3)
6reasoner.add_causal_link("env_issue", "test_failure", strength=0.5)
7
8query = reasoner.query_causes("test_failure", "true")
9
10# Results (ranked by probability):
11# 1. bug: 0.72
12# 2. env_issue: 0.45
13# 3. flaky_test: 0.28
Best Practices