Skip to content

Commit de086d1

Browse files
authored
Merge pull request #56 from SentienceAPI/refactor_phase1.1_1.2
Phase 1.1-1.2 Producer
2 parents 5e5a783 + 8746c6d commit de086d1

23 files changed

+2013
-26
lines changed

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.11.5

CHANGELOG.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Changelog
2+
3+
All notable changes to the Sentience Python SDK will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [0.12.0] - 2025-12-26
9+
10+
### Added
11+
12+
#### Agent Tracing & Debugging
13+
- **New Module: `sentience.tracing`** - Built-in tracing infrastructure for debugging and analyzing agent behavior
14+
- `Tracer` class for recording agent execution
15+
- `TraceSink` abstract base class for custom trace storage
16+
- `JsonlTraceSink` for saving traces to JSONL files
17+
- `TraceEvent` dataclass for structured trace events
18+
- Trace events: `step_start`, `snapshot`, `llm_query`, `action`, `step_end`, `error`
19+
- **New Module: `sentience.agent_config`** - Centralized agent configuration
20+
- `AgentConfig` dataclass with defaults for snapshot limits, LLM settings, screenshot options
21+
- **New Module: `sentience.utils`** - Snapshot digest utilities
22+
- `compute_snapshot_digests()` - Generate SHA256 fingerprints for loop detection
23+
- `canonical_snapshot_strict()` - Digest including element text
24+
- `canonical_snapshot_loose()` - Digest excluding text (layout only)
25+
- `sha256_digest()` - Hash computation helper
26+
- **New Module: `sentience.formatting`** - LLM prompt formatting
27+
- `format_snapshot_for_llm()` - Convert snapshots to LLM-friendly text format
28+
- **Schema File: `sentience/schemas/trace_v1.json`** - JSON Schema for trace events, bundled with package
29+
30+
#### Enhanced SentienceAgent
31+
- Added optional `tracer` parameter to `SentienceAgent.__init__()` for execution tracking
32+
- Added optional `config` parameter to `SentienceAgent.__init__()` for advanced configuration
33+
- Automatic tracing throughout `act()` method when tracer is provided
34+
- All tracing is **opt-in** - backward compatible with existing code
35+
36+
### Changed
37+
- Bumped version from `0.11.0` to `0.12.0`
38+
- Updated `__init__.py` to export new modules: `AgentConfig`, `Tracer`, `TraceSink`, `JsonlTraceSink`, `TraceEvent`, and utility functions
39+
- Added `MANIFEST.in` to include JSON schema files in package distribution
40+
41+
### Fixed
42+
- Fixed linting errors across multiple files:
43+
- `sentience/cli.py` - Removed unused variable `code` (F841)
44+
- `sentience/inspector.py` - Removed unused imports (F401)
45+
- `tests/test_inspector.py` - Removed unused `pytest` import (F401)
46+
- `tests/test_recorder.py` - Removed unused imports (F401)
47+
- `tests/test_smart_selector.py` - Removed unused `pytest` import (F401)
48+
- `tests/test_stealth.py` - Added `noqa` comments for intentional violations (E402, C901, F541)
49+
- `tests/test_tracing.py` - Removed unused `TraceSink` import (F401)
50+
51+
### Documentation
52+
- Updated `README.md` with comprehensive "Advanced Features" section covering tracing and utilities
53+
- Updated `docs/SDK_MANUAL.md` to v0.12.0 with new "Agent Tracing & Debugging" section
54+
- Added examples for:
55+
- Basic tracing setup
56+
- AgentConfig usage
57+
- Snapshot digests for loop detection
58+
- LLM prompt formatting
59+
- Custom trace sinks
60+
61+
### Testing
62+
- Added comprehensive test suites for new modules:
63+
- `tests/test_tracing.py` - 10 tests for tracing infrastructure
64+
- `tests/test_utils.py` - 22 tests for digest utilities
65+
- `tests/test_formatting.py` - 9 tests for LLM formatting
66+
- `tests/test_agent_config.py` - 9 tests for configuration
67+
- All 50 new tests passing ✅
68+
69+
### Migration Guide
70+
71+
#### For Existing Users
72+
No breaking changes! All new features are opt-in:
73+
74+
```python
75+
# Old code continues to work exactly the same
76+
agent = SentienceAgent(browser, llm)
77+
agent.act("Click the button")
78+
79+
# New optional features
80+
tracer = Tracer(run_id="run-123", sink=JsonlTraceSink("trace.jsonl"))
81+
config = AgentConfig(snapshot_limit=100, temperature=0.5)
82+
agent = SentienceAgent(browser, llm, tracer=tracer, config=config)
83+
agent.act("Click the button") # Now traced!
84+
```
85+
86+
#### Importing New Modules
87+
88+
```python
89+
# Tracing
90+
from sentience.tracing import Tracer, JsonlTraceSink, TraceEvent, TraceSink
91+
92+
# Configuration
93+
from sentience.agent_config import AgentConfig
94+
95+
# Utilities
96+
from sentience.utils import (
97+
compute_snapshot_digests,
98+
canonical_snapshot_strict,
99+
canonical_snapshot_loose,
100+
sha256_digest
101+
)
102+
103+
# Formatting
104+
from sentience.formatting import format_snapshot_for_llm
105+
```
106+
107+
### Notes
108+
- This release focuses on developer experience and debugging capabilities
109+
- No changes to browser automation APIs
110+
- No changes to snapshot APIs
111+
- No changes to query/action APIs
112+
- Fully backward compatible with v0.11.0
113+
114+
---
115+
116+
## [0.11.0] - Previous Release
117+
118+
(Previous changelog entries would go here)

MANIFEST.in

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
include README.md
2-
include LICENSE
3-
recursive-include spec *
4-
recursive-include sentience *.py
1+
include sentience/schemas/*.json

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,86 @@ cd sentience-chrome
456456
- Check visibility: `element.in_viewport and not element.is_occluded`
457457
- Scroll to element: `browser.page.evaluate(f"window.sentience_registry[{element.id}].scrollIntoView()")`
458458

459+
## Advanced Features (v0.12.0+)
460+
461+
### Agent Tracing & Debugging
462+
463+
The SDK now includes built-in tracing infrastructure for debugging and analyzing agent behavior:
464+
465+
```python
466+
from sentience import SentienceBrowser, SentienceAgent
467+
from sentience.llm_provider import OpenAIProvider
468+
from sentience.tracing import Tracer, JsonlTraceSink
469+
from sentience.agent_config import AgentConfig
470+
471+
# Create tracer to record agent execution
472+
tracer = Tracer(
473+
run_id="my-agent-run-123",
474+
sink=JsonlTraceSink("trace.jsonl")
475+
)
476+
477+
# Configure agent behavior
478+
config = AgentConfig(
479+
snapshot_limit=50,
480+
temperature=0.0,
481+
max_retries=1,
482+
capture_screenshots=True
483+
)
484+
485+
browser = SentienceBrowser()
486+
llm = OpenAIProvider(api_key="your-key", model="gpt-4o")
487+
488+
# Pass tracer and config to agent
489+
agent = SentienceAgent(browser, llm, tracer=tracer, config=config)
490+
491+
with browser:
492+
browser.page.goto("https://example.com")
493+
494+
# All actions are automatically traced
495+
agent.act("Click the sign in button")
496+
agent.act("Type 'user@example.com' into email field")
497+
498+
# Trace events saved to trace.jsonl
499+
# Events: step_start, snapshot, llm_query, action, step_end, error
500+
```
501+
502+
**Trace Events Captured:**
503+
- `step_start` - Agent begins executing a goal
504+
- `snapshot` - Page state captured
505+
- `llm_query` - LLM decision made (includes tokens, model, response)
506+
- `action` - Action executed (click, type, press)
507+
- `step_end` - Step completed successfully
508+
- `error` - Error occurred during execution
509+
510+
**Use Cases:**
511+
- Debug why agent failed or got stuck
512+
- Analyze token usage and costs
513+
- Replay agent sessions
514+
- Train custom models from successful runs
515+
- Monitor production agents
516+
517+
### Snapshot Utilities
518+
519+
New utility functions for working with snapshots:
520+
521+
```python
522+
from sentience import snapshot
523+
from sentience.utils import compute_snapshot_digests, canonical_snapshot_strict
524+
from sentience.formatting import format_snapshot_for_llm
525+
526+
snap = snapshot(browser)
527+
528+
# Compute snapshot fingerprints (detect page changes)
529+
digests = compute_snapshot_digests(snap.elements)
530+
print(f"Strict digest: {digests['strict']}") # Changes when text changes
531+
print(f"Loose digest: {digests['loose']}") # Only changes when layout changes
532+
533+
# Format snapshot for LLM prompts
534+
llm_context = format_snapshot_for_llm(snap, limit=50)
535+
print(llm_context)
536+
# Output: [1] <button> "Sign In" {PRIMARY,CLICKABLE} @ (100,50) (Imp:10)
537+
```
538+
459539
## Documentation
460540

461541
- **📖 [Amazon Shopping Guide](../docs/AMAZON_SHOPPING_GUIDE.md)** - Complete tutorial with real-world example

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "sentience-python"
7-
version = "0.11.0"
7+
version = "0.12.0"
88
description = "Python SDK for Sentience AI Agent Browser Automation"
99
readme = "README.md"
1010
requires-python = ">=3.11"

sentience/__init__.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
from .actions import click, click_rect, press, type_text
66
from .agent import SentienceAgent
7+
from .agent_config import AgentConfig
78

89
# Agent Layer (Phase 1 & 2)
910
from .base_agent import BaseAgent
1011
from .browser import SentienceBrowser
1112
from .conversational_agent import ConversationalAgent
1213
from .expect import expect
14+
15+
# Formatting (v0.12.0+)
16+
from .formatting import format_snapshot_for_llm
1317
from .generator import ScriptGenerator, generate
1418
from .inspector import Inspector, inspect
1519
from .llm_provider import (
@@ -39,9 +43,20 @@
3943
from .recorder import Recorder, Trace, TraceStep, record
4044
from .screenshot import screenshot
4145
from .snapshot import snapshot
46+
47+
# Tracing (v0.12.0+)
48+
from .tracing import JsonlTraceSink, TraceEvent, Tracer, TraceSink
49+
50+
# Utilities (v0.12.0+)
51+
from .utils import (
52+
canonical_snapshot_loose,
53+
canonical_snapshot_strict,
54+
compute_snapshot_digests,
55+
sha256_digest,
56+
)
4257
from .wait import wait_for
4358

44-
__version__ = "0.11.0"
59+
__version__ = "0.12.0"
4560

4661
__all__ = [
4762
# Core SDK
@@ -88,4 +103,18 @@
88103
"SnapshotOptions",
89104
"SnapshotFilter",
90105
"ScreenshotConfig",
106+
# Tracing (v0.12.0+)
107+
"Tracer",
108+
"TraceSink",
109+
"JsonlTraceSink",
110+
"TraceEvent",
111+
# Utilities (v0.12.0+)
112+
"canonical_snapshot_strict",
113+
"canonical_snapshot_loose",
114+
"compute_snapshot_digests",
115+
"sha256_digest",
116+
# Formatting (v0.12.0+)
117+
"format_snapshot_for_llm",
118+
# Agent Config (v0.12.0+)
119+
"AgentConfig",
91120
]

0 commit comments

Comments
 (0)