-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Labels
ai-friendlyDesigned for AI-assisted implementationDesigned for AI-assisted implementationarea:executorFlow execution engineFlow execution enginearea:observabilityLogging, tracing, and monitoringLogging, tracing, and monitoringcomplexity:complexSignificant effort, design review neededSignificant effort, design review neededpriority:lowNice-to-have, address if time permitsNice-to-have, address if time permitssize:LLarge effort (3-5 days)Large effort (3-5 days)type:featureNew feature or capabilityNew feature or capability
Description
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:
- Trace recording — capture tool name, inputs, outputs per call; group into traces (sequences)
- Pattern detection — find repeated sub-sequences across traces using sequence mining (e.g., longest common subsequences, n-gram analysis)
- Determinism scoring — for each detected pattern, compute a confidence score: how consistently the same inputs produce the same routing
- Flow suggestion — generate
Flowobjects from detected patterns with auto-wiredinput_mapping - Governance gate — suggestions are proposals, NOT auto-registered; human approval required
Acceptance Criteria
-
ChainObserverclass withrecord()andend_trace()methods - Traces are stored in memory (with configurable max buffer size)
-
suggest_flows(min_occurrences, min_length)returnsFlowSuggestionobjects -
FlowSuggestionincludes: suggestedFlow, 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
- Start with simple n-gram frequency counting on tool-name sequences
- Confidence = occurrences / total traces containing the start tool
input_mappingauto-generation can reuse logic from Offline computation of valid tool combinations from schemas #77 (ChainAnalyzer)- Consider integrating with Implement structured execution trace with trace IDs and timing #20 (structured execution traces) for richer trace data
- Future: persist traces to disk for offline analysis
Dependencies
- Offline computation of valid tool combinations from schemas #77 (ChainAnalyzer) — shares schema compatibility logic for auto-wiring
- Add determinism scoring and candidate flow proposal from traces #12, Design opt-in governance workflow for promoting observed chains #13 (existing issues) — this supersedes/encompasses their scope with a full end-to-end design
Tasks
- Create
chainweaver/observer.pywithChainObserverclass - Implement trace recording and buffering
- Implement n-gram-based pattern detection
- Implement determinism/confidence scoring
- Implement
FlowSuggestiondataclass andsuggest_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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
ai-friendlyDesigned for AI-assisted implementationDesigned for AI-assisted implementationarea:executorFlow execution engineFlow execution enginearea:observabilityLogging, tracing, and monitoringLogging, tracing, and monitoringcomplexity:complexSignificant effort, design review neededSignificant effort, design review neededpriority:lowNice-to-have, address if time permitsNice-to-have, address if time permitssize:LLarge effort (3-5 days)Large effort (3-5 days)type:featureNew feature or capabilityNew feature or capability