feat: distinguish save close and resource release facts
This commit is contained in:
1 parent
3e7ae2b76f
commit
7bd7cd3966
3 files changed
+142
-6
No files matched your search
@@ -10,7 +10,7 @@
|
||||
|
||||
**Spec:** [2026-11-06-runtime-lifecycle-safety-design.md](../specs/2026-11-06-runtime-lifecycle-safety-design.md)
|
||||
|
||||
**Status:** 计划草案。用户已确认范围;本轮明确要求完成计划,因此设计和实施清单同时供审阅,不能视为设计已经批准或代码实施已经授权。基线为 `5eb4b2d7ed09947f19310ba80ba3b9a2a09b6f7d`;本文件没有修改生产行为。
|
||||
**Status:** 用户已于 2026-11-06 授权按本计划实施,正在独立分支执行。基线为 `5eb4b2d7ed09947f19310ba80ba3b9a2a09b6f7d`;本文件没有修改生产行为。
|
||||
|
||||
## Global Constraints
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
- Produces: `stop_disposition(*, call_started: bool | None, call_returned: bool | None, identity_confirmed: bool) -> str`,返回 CANCEL_BEFORE_CALL / WAIT_FOR_RETURN / CLEANUP_RETURNED / OBSERVE_UNCONFIRMED。
|
||||
- 返回字段固定为 save_state、close_state、helper_state、resource_state、interrupted、detail,取值见设计第 4 节;其他任务不得另造不兼容枚举。
|
||||
|
||||
- [ ] **Step 1: 写失败测试,证明 Save/Close/释放不能互相替代。**
|
||||
- [x] **Step 1: 写失败测试,证明 Save/Close/释放不能互相替代。**
|
||||
|
||||
```python
|
||||
from cbb_refresh_verify.operation_lifecycle import classify_save_state, stop_disposition
|
||||
@@ -71,8 +71,8 @@ def test_an_unreturned_call_requires_waiting():
|
||||
identity_confirmed=True) == "WAIT_FOR_RETURN"
|
||||
```
|
||||
|
||||
- [ ] **Step 2: 运行并确认 RED。** `python -m pytest -q tests/test_operation_lifecycle.py`;预期缺少新模块/函数。再加取消前、SAVE_REJECTED、CLOSE_FAILED、日志截断/矛盾、forced=True 的行为断言,不把进程退出断言成保存完成。
|
||||
- [ ] **Step 3: 实现纯判据。** 核心映射从明确证据出发:
|
||||
- [x] **Step 2: 运行并确认 RED。** `python -m pytest -q tests/test_operation_lifecycle.py`;预期缺少新模块/函数。再加取消前、SAVE_REJECTED、CLOSE_FAILED、日志截断/矛盾、forced=True 的行为断言,不把进程退出断言成保存完成。
|
||||
- [x] **Step 3: 实现纯判据。** 核心映射从明确证据出发:
|
||||
|
||||
```python
|
||||
save_state = ("COMMITTED" if "SAVE_OK" in markers else
|
||||
@@ -83,8 +83,8 @@ resource_state = ("RELEASED" if native_released is True and helper_exited is Tru
|
||||
```
|
||||
|
||||
不把 UNKNOWN 当 False;矛盾标记降为 UNKNOWN 并记录原因。`forced=True` 不能把 UNKNOWN 的保存状态升级为成功。
|
||||
- [ ] **Step 4: 运行该文件及 `tests/test_runtime_timing.py`,确认已有标记消费保持兼容。**
|
||||
- [ ] **Step 5: 提交。** `git commit -m "feat: distinguish save close and resource release facts"`。
|
||||
- [x] **Step 4: 运行该文件及 `tests/test_runtime_timing.py`,确认已有标记消费保持兼容。**
|
||||
- [x] **Step 5: 提交。** `git commit -m "feat: distinguish save close and resource release facts"`。
|
||||
|
||||
### Task 2:保存 helper 的启动门和完整返回事实
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
"""Facts from one helper invocation; process exit is not a Save result."""
|
||||
from collections.abc import Sequence
|
||||
|
||||
|
||||
def classify_save_state(markers: Sequence[str], *, helper_exited: bool | None,
|
||||
native_released: bool | None, forced: bool = False) -> dict:
|
||||
names = {line.strip().split('|', 1)[0] for line in markers}
|
||||
save_results = names & {'SAVE_OK', 'SAVE_REJECTED', 'SAVE_RESULT_UNKNOWN',
|
||||
'CANCELLED_BEFORE_SAVE'}
|
||||
conflicting_save = (len(save_results) > 1 or
|
||||
('CANCELLED_BEFORE_SAVE' in names and 'SAVE_START' in names))
|
||||
conflicting_close = {'CLOSE_OK', 'CLOSE_FAILED'} <= names
|
||||
save_state = 'UNKNOWN'
|
||||
if not conflicting_save:
|
||||
for marker, state in (('SAVE_OK', 'COMMITTED'), ('SAVE_REJECTED', 'REJECTED'),
|
||||
('CANCELLED_BEFORE_SAVE', 'NOT_STARTED')):
|
||||
if marker in names:
|
||||
save_state = state
|
||||
close_state = 'UNKNOWN'
|
||||
if not conflicting_close:
|
||||
if 'CLOSE_OK' in names:
|
||||
close_state = 'CLOSED'
|
||||
elif 'CLOSE_FAILED' in names:
|
||||
close_state = 'FAILED'
|
||||
resource_state = ('RELEASED' if native_released is True and helper_exited is True else
|
||||
'RETAINED' if native_released is False or helper_exited is False else
|
||||
'UNKNOWN')
|
||||
reasons = []
|
||||
if conflicting_save or conflicting_close:
|
||||
reasons.append('本轮返回标记矛盾')
|
||||
if save_state == 'UNKNOWN':
|
||||
reasons.append('保存返回结果无法确认')
|
||||
if close_state == 'FAILED':
|
||||
reasons.append('原生关闭失败')
|
||||
if 'POST_SAVE_FAILED' in names:
|
||||
reasons.append('保存后核对失败')
|
||||
if forced:
|
||||
reasons.append('会话被中断,退出不代表保存完成')
|
||||
interrupted = (forced or conflicting_save or conflicting_close or
|
||||
'SAVE_RESULT_UNKNOWN' in names or
|
||||
(helper_exited is True and 'SAVE_START' in names and save_state == 'UNKNOWN'))
|
||||
return dict(save_state=save_state, close_state=close_state,
|
||||
helper_state='EXITED' if helper_exited is True else
|
||||
'RUNNING' if helper_exited is False else 'UNKNOWN',
|
||||
resource_state=resource_state, interrupted=interrupted,
|
||||
detail=';'.join(reasons))
|
||||
|
||||
|
||||
def stop_disposition(*, call_started: bool | None, call_returned: bool | None,
|
||||
identity_confirmed: bool) -> str:
|
||||
if not identity_confirmed or call_started is None:
|
||||
return 'OBSERVE_UNCONFIRMED'
|
||||
if call_started is False:
|
||||
return 'CANCEL_BEFORE_CALL' if call_returned is False else 'OBSERVE_UNCONFIRMED'
|
||||
return 'CLEANUP_RETURNED' if call_returned is True else 'WAIT_FOR_RETURN'
|
||||
@@ -0,0 +1,81 @@
|
||||
import pytest
|
||||
|
||||
from cbb_refresh_verify.operation_lifecycle import classify_save_state, stop_disposition
|
||||
|
||||
|
||||
def facts(markers, *, exited=True, released=True, forced=False):
|
||||
return classify_save_state(markers, helper_exited=exited,
|
||||
native_released=released, forced=forced)
|
||||
|
||||
|
||||
def test_saved_but_not_closed_retains_independent_facts():
|
||||
result = facts(['SAVE_START', 'SAVE_OK', 'CLOSE_START'], exited=False, released=False)
|
||||
assert result['save_state'] == 'COMMITTED'
|
||||
assert result['close_state'] == 'UNKNOWN'
|
||||
assert result['helper_state'] == 'RUNNING'
|
||||
assert result['resource_state'] == 'RETAINED'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('markers,expected', [
|
||||
([], 'UNKNOWN'),
|
||||
(['SAVE_START'], 'UNKNOWN'),
|
||||
(['CANCELLED_BEFORE_SAVE'], 'NOT_STARTED'),
|
||||
(['SAVE_START', 'SAVE_REJECTED'], 'REJECTED'),
|
||||
(['SAVE_START', 'SAVE_RESULT_UNKNOWN|123|broken'], 'UNKNOWN'),
|
||||
(['SAVE_OK', 'POST_SAVE_FAILED'], 'COMMITTED'),
|
||||
(['SAVE_OK', 'CLOSE_FAILED|5|busy'], 'COMMITTED'),
|
||||
])
|
||||
def test_process_exit_never_substitutes_for_save_return(markers, expected):
|
||||
result = facts(markers)
|
||||
assert result['save_state'] == expected
|
||||
assert result['resource_state'] == 'RELEASED'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('markers', [
|
||||
['SAVE_OK', 'SAVE_REJECTED'], ['CANCELLED_BEFORE_SAVE', 'SAVE_START'],
|
||||
['SAVE_OK', 'SAVE_RESULT_UNKNOWN'], ['CLOSE_OK', 'CLOSE_FAILED'],
|
||||
])
|
||||
def test_contradictory_markers_are_not_success(markers):
|
||||
result = facts(markers)
|
||||
if any(m.startswith('SAVE') or m == 'CANCELLED_BEFORE_SAVE' for m in markers):
|
||||
assert result['save_state'] == 'UNKNOWN'
|
||||
else:
|
||||
assert result['close_state'] == 'UNKNOWN'
|
||||
assert result['detail']
|
||||
|
||||
|
||||
def test_successful_save_and_failed_close_survive_force_and_later_release():
|
||||
result = facts(['SAVE_START', 'SAVE_OK', 'CLOSE_FAILED'], forced=True)
|
||||
assert result['save_state'] == 'COMMITTED'
|
||||
assert result['close_state'] == 'FAILED'
|
||||
assert result['resource_state'] == 'RELEASED'
|
||||
assert result['interrupted'] is True
|
||||
|
||||
|
||||
def test_close_and_exit_without_save_return_remains_unknown():
|
||||
result = facts(['SAVE_START', 'CLOSE_OK'], forced=True)
|
||||
assert result['save_state'] == 'UNKNOWN'
|
||||
assert result['close_state'] == 'CLOSED'
|
||||
assert result['interrupted'] is True
|
||||
|
||||
|
||||
@pytest.mark.parametrize('exited,released,expected', [
|
||||
(None, True, 'UNKNOWN'), (True, None, 'UNKNOWN'),
|
||||
(False, True, 'RETAINED'), (True, False, 'RETAINED'),
|
||||
])
|
||||
def test_resource_release_requires_both_observations(exited, released, expected):
|
||||
assert facts(['SAVE_OK'], exited=exited, released=released)['resource_state'] == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('started,returned,identity,expected', [
|
||||
(False, False, True, 'CANCEL_BEFORE_CALL'),
|
||||
(True, False, True, 'WAIT_FOR_RETURN'),
|
||||
(True, None, True, 'WAIT_FOR_RETURN'),
|
||||
(True, True, True, 'CLEANUP_RETURNED'),
|
||||
(None, None, True, 'OBSERVE_UNCONFIRMED'),
|
||||
(False, False, False, 'OBSERVE_UNCONFIRMED'),
|
||||
(False, True, True, 'OBSERVE_UNCONFIRMED'),
|
||||
])
|
||||
def test_stop_requires_positive_call_and_identity_evidence(started, returned, identity, expected):
|
||||
assert stop_disposition(call_started=started, call_returned=returned,
|
||||
identity_confirmed=identity) == expected
|
||||
Reference in new issue
Block a user