From 07df53c73e975b68341671893ad64020f2733b10 Mon Sep 17 00:00:00 2001 From: Agent Date: Wed, 1 Apr 2026 22:55:15 +0000 Subject: [PATCH] feat(context): add reset() method to WorkflowContext --- gateframe/core/context.py | 15 ++++++++++++++ tests/core/test_context.py | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/gateframe/core/context.py b/gateframe/core/context.py index 2064985..034248d 100644 --- a/gateframe/core/context.py +++ b/gateframe/core/context.py @@ -119,6 +119,21 @@ def _compute_penalty(self, result: ValidationResult) -> float: penalty += self._silent_fail_penalty return penalty + def reset(self) -> None: + """Reset the workflow context to its initial state. + + This restores confidence to its initial value and clears the history. + Useful for reusing a context across multiple runs in tests or + long-running processes. + """ + self.confidence = self._initial_confidence + self._history.clear() + logger.info( + "workflow_reset", + workflow_id=self.workflow_id, + initial_confidence=self._initial_confidence, + ) + def to_dict(self) -> dict[str, Any]: return { "workflow_id": self.workflow_id, diff --git a/tests/core/test_context.py b/tests/core/test_context.py index 19550e9..84593bb 100644 --- a/tests/core/test_context.py +++ b/tests/core/test_context.py @@ -180,6 +180,46 @@ def test_passing_step_keeps_confidence(self) -> None: ctx.update(_make_result(passed=True)) assert ctx.confidence == 1.0 + def test_reset_restores_confidence(self) -> None: + ctx = WorkflowContext("wf1") + ctx.update(_make_result(passed=False, failures=[_soft_failure()])) + assert ctx.confidence == pytest.approx(0.85) + ctx.reset() + assert ctx.confidence == 1.0 + + def test_reset_clears_history(self) -> None: + ctx = WorkflowContext("wf1") + ctx.update(_make_result(passed=True)) + ctx.update(_make_result(passed=False, failures=[_soft_failure()])) + assert ctx.step_count == 2 + ctx.reset() + assert ctx.step_count == 0 + assert ctx.history == [] + + def test_reset_with_custom_initial_confidence(self) -> None: + ctx = WorkflowContext("wf1", initial_confidence=0.9) + ctx.update(_make_result(passed=False, failures=[_soft_failure()])) + assert ctx.confidence == pytest.approx(0.75) + ctx.reset() + assert ctx.confidence == pytest.approx(0.9) + + def test_reset_allows_reuse(self) -> None: + ctx = WorkflowContext("wf1") + # First run + ctx.update(_make_result(passed=False, failures=[_soft_failure()])) + ctx.update(_make_result(passed=False, failures=[_soft_failure()])) + assert ctx.confidence == pytest.approx(0.70) + assert ctx.step_count == 2 + + # Reset and second run + ctx.reset() + assert ctx.confidence == 1.0 + assert ctx.step_count == 0 + + ctx.update(_make_result(passed=False, failures=[_silent_failure()])) + assert ctx.confidence == pytest.approx(0.9) + assert ctx.step_count == 1 + class TestStepRecord: def test_to_dict(self) -> None: