Skip to content

Implement runtime chain observer with auto-flow suggestion #78

@dgenio

Description

@dgenio

Context / Problem

Users must manually define flows by inspecting their agent's behavior and hand-wiring steps. The real power of ChainWeaver's "compiled" metaphor is the ability to observe what agents do at runtime, detect repeated deterministic patterns, and suggest compiled flows automatically.

This transforms ChainWeaver from a manual flow-definition library into an intelligent optimization layer: "Your agent called fetch→validate→transform 47 times with the same routing. Would you like to compile this into a flow?"

Proposal

Implement a ChainObserver that records tool call traces, detects repeated sequences, and proposes Flow objects for approval:

from chainweaver import ChainObserver

observer = ChainObserver()

# Record tool calls as they happen (from agent runtime)
observer.record("fetch", {"url": "..."}, {"data": "..."})
observer.record("validate", {"data": "..."}, {"valid": True})
observer.record("transform", {"data": "..."}, {"result": "..."})
observer.end_trace()  # marks end of one tool-call sequence

# ... after many traces ...

# Detect repeated patterns
suggestions = observer.suggest_flows(min_occurrences=3, min_length=2)
# [FlowSuggestion(name="fetch_validate_transform", confidence=0.92, occurrences=47, flow=Flow(...))]

# Approve and register
registry.register_flow(suggestions[0].flow)

Core capabilities:

  1. Trace recording — capture tool name, inputs, outputs per call; group into traces (sequences)
  2. Pattern detection — find repeated sub-sequences across traces using sequence mining (e.g., longest common subsequences, n-gram analysis)
  3. Determinism scoring — for each detected pattern, compute a confidence score: how consistently the same inputs produce the same routing
  4. Flow suggestion — generate Flow objects from detected patterns with auto-wired input_mapping
  5. Governance gate — suggestions are proposals, NOT auto-registered; human approval required

Acceptance Criteria

  • ChainObserver class with record() and end_trace() methods
  • Traces are stored in memory (with configurable max buffer size)
  • suggest_flows(min_occurrences, min_length) returns FlowSuggestion objects
  • FlowSuggestion includes: suggested Flow, occurrence count, confidence score, example trace
  • Pattern detection finds repeated 2+ step sequences across 3+ traces
  • Confidence score reflects how consistently the pattern appears (0.0–1.0)
  • Suggested flows are NOT auto-registered — explicit registry.register_flow() required
  • Tests cover: basic detection, no patterns, overlapping patterns, confidence scoring, governance gate

Implementation Notes

Dependencies

Tasks

  • Create chainweaver/observer.py with ChainObserver class
  • Implement trace recording and buffering
  • Implement n-gram-based pattern detection
  • Implement determinism/confidence scoring
  • Implement FlowSuggestion dataclass and suggest_flows() method
  • Add governance gate documentation and example
  • Add unit tests in tests/test_observer.py
  • Add example: examples/chain_observer.py
  • Document in README under "Runtime Learning" section

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai-friendlyDesigned for AI-assisted implementationarea:executorFlow execution enginearea:observabilityLogging, tracing, and monitoringcomplexity:complexSignificant effort, design review neededpriority:lowNice-to-have, address if time permitssize:LLarge effort (3-5 days)type:featureNew feature or capability

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions