Spaces:
Sleeping
Sleeping
File size: 10,505 Bytes
116524e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | """Tests for OpenClaw integration β OpenClawToTraceStep and end-to-end pipeline."""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any, Optional
import pytest
from ace.core.context import ACEStepContext, SkillbookView
from ace.core.outputs import (
AgentOutput,
ReflectorOutput,
SkillManagerOutput,
)
from ace.core.skillbook import Skillbook, UpdateBatch, UpdateOperation
from ace.integrations.openclaw import OpenClawToTraceStep
from ace.steps import learning_tail
from ace.steps.load_traces import LoadTracesStep
from pipeline import Pipeline
# ------------------------------------------------------------------ #
# Helpers β mock roles
# ------------------------------------------------------------------ #
class MockReflector:
"""Minimal mock satisfying ReflectorLike."""
def __init__(self, output: ReflectorOutput | None = None):
self.output = output or ReflectorOutput(
reasoning="test reasoning",
correct_approach="test approach",
key_insight="test insight",
)
self.calls: list[dict] = []
def reflect(
self,
*,
question: str,
agent_output: AgentOutput,
skillbook: Any,
ground_truth: Optional[str] = None,
feedback: Optional[str] = None,
**kwargs: Any,
) -> ReflectorOutput:
self.calls.append(
{
"question": question,
"agent_output": agent_output,
"ground_truth": ground_truth,
"feedback": feedback,
**kwargs,
}
)
return self.output
class MockSkillManager:
"""Minimal mock satisfying SkillManagerLike.
The real SM mutates the skillbook directly via tool calls; this mock
applies its pre-canned ``output`` to the incoming skillbook so
``UpdateStep`` behaves like the live code path.
"""
def __init__(self, output: SkillManagerOutput | None = None):
self.output = output or SkillManagerOutput(
update=UpdateBatch(reasoning="test", operations=[]),
)
self.calls: list[dict] = []
def update_skills(
self,
*,
reflections: tuple[ReflectorOutput, ...],
skillbook: Any,
question_context: str,
progress: str,
**kwargs: Any,
) -> SkillManagerOutput:
self.calls.append(
{
"reflections": reflections,
"question_context": question_context,
"progress": progress,
}
)
skillbook.apply_update(self.output.update)
return self.output
# ------------------------------------------------------------------ #
# Fixtures
# ------------------------------------------------------------------ #
@pytest.fixture
def sample_jsonl(tmp_path: Path) -> Path:
"""Create a minimal OpenClaw session JSONL file."""
events = [
{
"type": "session",
"id": "s1",
"timestamp": "2026-01-01T00:00:00Z",
"version": 1,
"cwd": "/app",
},
{
"type": "message",
"id": "m1",
"parentId": "s1",
"timestamp": "2026-01-01T00:00:01Z",
"message": {
"role": "user",
"content": [{"type": "text", "text": "Hello, help me debug this."}],
},
},
{
"type": "message",
"id": "m2",
"parentId": "m1",
"timestamp": "2026-01-01T00:00:02Z",
"message": {
"role": "assistant",
"content": [
{"type": "thinking", "thinking": "Let me analyze the issue..."},
{"type": "text", "text": "I'll help you debug this."},
{
"type": "toolCall",
"id": "tc1",
"name": "Read",
"arguments": {"file_path": "/app/main.py"},
},
],
},
},
{
"type": "message",
"id": "m3",
"parentId": "m2",
"timestamp": "2026-01-01T00:00:03Z",
"message": {
"role": "toolResult",
"content": [
{"type": "text", "text": "def main():\n print('hello')"}
],
},
},
]
path = tmp_path / "test-session.jsonl"
path.write_text("\n".join(json.dumps(e) for e in events) + "\n")
return path
# ------------------------------------------------------------------ #
# OpenClawToTraceStep tests
# ------------------------------------------------------------------ #
class TestOpenClawToTraceStep:
def test_requires_provides(self):
step = OpenClawToTraceStep()
assert step.requires == frozenset({"trace"})
assert step.provides == frozenset({"trace"})
def test_converts_to_trace_dict(self):
"""Step should convert raw events into a structured trace dict."""
raw_events = [
{"type": "session", "id": "s1", "cwd": "/app"},
{
"type": "message",
"id": "m1",
"message": {
"role": "user",
"content": [{"type": "text", "text": "Hello"}],
},
},
{
"type": "message",
"id": "m2",
"message": {
"role": "assistant",
"content": [{"type": "text", "text": "Hi there"}],
},
},
]
ctx = ACEStepContext(trace=raw_events)
result = OpenClawToTraceStep()(ctx)
trace = result.trace
assert isinstance(trace, dict)
assert trace["question"] == "User: Hello"
assert trace["answer"] == "Hi there"
assert trace["skill_ids"] == []
assert trace["ground_truth"] is None
assert "reasoning" in trace
assert "feedback" in trace
def test_none_trace(self):
"""Step should handle None trace gracefully."""
ctx = ACEStepContext(trace=None)
result = OpenClawToTraceStep()(ctx)
assert result.trace is None
def test_empty_list_trace(self):
"""Step should handle empty list trace gracefully."""
ctx = ACEStepContext(trace=[])
result = OpenClawToTraceStep()(ctx)
assert result.trace == []
# ------------------------------------------------------------------ #
# End-to-end: LoadTracesStep β OpenClawToTraceStep β learning_tail
# ------------------------------------------------------------------ #
class TestOpenClawEndToEnd:
def test_load_and_convert(self, sample_jsonl: Path):
"""LoadTracesStep β OpenClawToTraceStep should produce trace data."""
load_step = LoadTracesStep()
convert_step = OpenClawToTraceStep()
ctx = ACEStepContext(sample=str(sample_jsonl))
ctx = load_step(ctx)
assert isinstance(ctx.trace, list)
assert len(ctx.trace) == 4
ctx = convert_step(ctx)
# Converted to structured trace dict
assert isinstance(ctx.trace, dict)
assert "question" in ctx.trace
assert "reasoning" in ctx.trace
assert "answer" in ctx.trace
assert ctx.trace["skill_ids"] == []
assert ctx.trace["ground_truth"] is None
def test_full_pipeline_with_mocks(self, sample_jsonl: Path):
"""Full pipeline: load β convert β reflect β tag β update β apply."""
reflector = MockReflector()
skill_manager = MockSkillManager()
skillbook = Skillbook()
load_step = LoadTracesStep()
convert_step = OpenClawToTraceStep()
steps = [
load_step,
convert_step,
*learning_tail(reflector, skill_manager, skillbook),
]
pipeline = Pipeline(steps)
ctx = ACEStepContext(
sample=str(sample_jsonl),
skillbook=SkillbookView(skillbook),
)
result = pipeline.run([ctx])
pipeline.wait_for_background()
assert len(result) == 1
assert len(reflector.calls) == 1
assert len(skill_manager.calls) == 1
def test_pipeline_with_add_operation(self, sample_jsonl: Path):
"""Pipeline with a SkillManager that adds a skill."""
add_op = UpdateOperation(
type="ADD",
section="debugging",
issue="Use structured logging for better debug traces",
insight="Use structured logging for better debug traces",
skill_id=None,
metadata={"helpful": 1, "harmful": 0, "neutral": 0},
)
sm_output = SkillManagerOutput(
update=UpdateBatch(reasoning="Found useful pattern", operations=[add_op]),
)
reflector = MockReflector()
skill_manager = MockSkillManager(output=sm_output)
skillbook = Skillbook()
steps = [
LoadTracesStep(),
OpenClawToTraceStep(),
*learning_tail(reflector, skill_manager, skillbook),
]
pipeline = Pipeline(steps)
ctx = ACEStepContext(
sample=str(sample_jsonl),
skillbook=SkillbookView(skillbook),
)
pipeline.run([ctx])
pipeline.wait_for_background()
# Skillbook should now have one skill (legacy "debugging" β "context")
assert len(skillbook.skills()) == 1
skill = skillbook.skills()[0]
assert skill.section == "context"
assert "structured logging" in skill.insight
def test_empty_session_skipped(self, tmp_path: Path):
"""Empty JSONL should produce empty trace."""
path = tmp_path / "empty.jsonl"
path.write_text("")
load_step = LoadTracesStep()
convert_step = OpenClawToTraceStep()
ctx = ACEStepContext(sample=str(path))
ctx = load_step(ctx)
assert ctx.trace == []
ctx = convert_step(ctx)
assert ctx.trace == []
|