fix: Presence-Engine Stale-Recovery — complete() aus finally entfernt + PID-Tracking
5 Schritte:
1. adapter.py: complete('background_task') aus finally-Block entfernt.
Der Turn endet bevor der Background-Prozess endet — register() beim
Turn-Start bleibt, Stale-Recovery räumt tote Einträge auf.
2. subagent_tracker.py: register() speichert jetzt pid (os.getpid())
und expected_done_at (started_at + 600s). Neues remove_by_task_id()
für Stale-Recovery durch DetectionTimer.
3. presence_engine.py DetectionTimer: _stale_recovery() prüft jeden
aktiven Eintrag in subagent_status.json:
- os.kill(pid, 0) + /proc/<pid>/ Fallback
- PID tot → Eintrag entfernt
- PID lebend + expected_done_at überschritten → Eintrag bleibt
(long-running Task)
4. expected_done_at als Sekundär-Indikator: DetectionTimer nutzt es
für Logging/Diagnose, aber PID ist der primäre Indikator.
5. Lock-Order dokumentiert: _busy_honeymoon_lock vor _mode_lock.
Race-Bedingung zwischen Honeymoon-Expiry und set_pulse_target()
explizit im PresenceEngine-Docstring dokumentiert.
Tests: 40/40 passed (19 tracker + 21 presence, inkl. 5 neue Stale-Recovery-Tests)
This commit is contained in:
@@ -430,3 +430,280 @@ class TestReadSubagentCount:
|
||||
except OSError:
|
||||
pass
|
||||
assert engine._read_subagent_count() == 0
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# Test 9: Stale-Recovery — PID tot → Eintrag wird entfernt
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
class TestStaleRecoveryPidDead:
|
||||
"""Test 9: Stale-Recovery entfernt Einträge mit toter PID."""
|
||||
|
||||
def test_pid_dead_entry_removed(self):
|
||||
"""Wenn PID tot ist, wird der Eintrag aus subagent_status.json entfernt."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer
|
||||
from plugin.archivar.subagent_tracker import (
|
||||
_write_subagent_state, _get_subagent_state_path, get_tracker,
|
||||
)
|
||||
|
||||
subagent_path = _get_subagent_state_path()
|
||||
tracker = get_tracker()
|
||||
|
||||
# Schreibe einen Eintrag mit einer garantiert toten PID (99999)
|
||||
_write_subagent_state(subagent_path, {
|
||||
"active": [{
|
||||
"task_id": "t_dead_pid",
|
||||
"type": "background_task",
|
||||
"owner_room": "user:bernd",
|
||||
"pid": 99999,
|
||||
"started_at": "2025-01-01T00:00:00+00:00",
|
||||
"expected_duration_s": 600,
|
||||
"expected_done_at": "2025-01-01T00:10:00+00:00",
|
||||
"plugin_state_source": "adapter.subagent_tracker",
|
||||
}],
|
||||
"last_change_iso": "",
|
||||
"schema_version": 1,
|
||||
"source": "plugin_internal",
|
||||
})
|
||||
|
||||
# Verify entry exists
|
||||
assert tracker.count() == 1
|
||||
|
||||
# Create DetectionTimer and run stale recovery
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
gateway_path = f.name
|
||||
try:
|
||||
timer = DetectionTimer(
|
||||
gateway_state_path=gateway_path,
|
||||
detection_interval=10.0,
|
||||
)
|
||||
removed = timer._stale_recovery()
|
||||
|
||||
# PID 99999 ist tot → sollte entfernt worden sein
|
||||
assert removed == 1, f"Expected 1 removed, got {removed}"
|
||||
assert tracker.count() == 0, f"Expected 0 entries, got {tracker.count()}"
|
||||
finally:
|
||||
os.unlink(gateway_path)
|
||||
# Cleanup
|
||||
try:
|
||||
os.unlink(subagent_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def test_pid_dead_multiple_entries_cleanup(self):
|
||||
"""Mehrere tote PIDs → alle werden entfernt."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer
|
||||
from plugin.archivar.subagent_tracker import (
|
||||
_write_subagent_state, _get_subagent_state_path, get_tracker,
|
||||
)
|
||||
|
||||
subagent_path = _get_subagent_state_path()
|
||||
tracker = get_tracker()
|
||||
|
||||
_write_subagent_state(subagent_path, {
|
||||
"active": [
|
||||
{
|
||||
"task_id": "t_dead_1",
|
||||
"type": "background_task",
|
||||
"pid": 99998,
|
||||
"started_at": "2025-01-01T00:00:00+00:00",
|
||||
"expected_duration_s": 600,
|
||||
"expected_done_at": "2025-01-01T00:10:00+00:00",
|
||||
},
|
||||
{
|
||||
"task_id": "t_dead_2",
|
||||
"type": "delegate_task",
|
||||
"pid": 99997,
|
||||
"started_at": "2025-01-01T00:00:00+00:00",
|
||||
"expected_duration_s": 600,
|
||||
"expected_done_at": "2025-01-01T00:10:00+00:00",
|
||||
},
|
||||
],
|
||||
"last_change_iso": "",
|
||||
"schema_version": 1,
|
||||
"source": "plugin_internal",
|
||||
})
|
||||
|
||||
assert tracker.count() == 2
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
gateway_path = f.name
|
||||
try:
|
||||
timer = DetectionTimer(
|
||||
gateway_state_path=gateway_path,
|
||||
detection_interval=10.0,
|
||||
)
|
||||
removed = timer._stale_recovery()
|
||||
|
||||
assert removed == 2, f"Expected 2 removed, got {removed}"
|
||||
assert tracker.count() == 0
|
||||
finally:
|
||||
os.unlink(gateway_path)
|
||||
try:
|
||||
os.unlink(subagent_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# Test 10: Stale-Recovery — PID lebend → Eintrag bleibt
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
class TestStaleRecoveryPidAlive:
|
||||
"""Test 10: Stale-Recovery lässt Einträge mit lebender PID in Ruhe."""
|
||||
|
||||
def test_pid_alive_entry_kept(self):
|
||||
"""Wenn PID lebt, bleibt der Eintrag erhalten."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer
|
||||
from plugin.archivar.subagent_tracker import (
|
||||
_write_subagent_state, _get_subagent_state_path, get_tracker,
|
||||
)
|
||||
|
||||
subagent_path = _get_subagent_state_path()
|
||||
tracker = get_tracker()
|
||||
|
||||
# Nutze die eigene PID (dieser Testprozess lebt garantiert)
|
||||
own_pid = os.getpid()
|
||||
|
||||
_write_subagent_state(subagent_path, {
|
||||
"active": [{
|
||||
"task_id": "t_alive_pid",
|
||||
"type": "background_task",
|
||||
"owner_room": "user:bernd",
|
||||
"pid": own_pid,
|
||||
"started_at": "2025-01-01T00:00:00+00:00",
|
||||
"expected_duration_s": 600,
|
||||
"expected_done_at": "2099-01-01T00:00:00+00:00", # weit in der Zukunft
|
||||
"plugin_state_source": "adapter.subagent_tracker",
|
||||
}],
|
||||
"last_change_iso": "",
|
||||
"schema_version": 1,
|
||||
"source": "plugin_internal",
|
||||
})
|
||||
|
||||
assert tracker.count() == 1
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
gateway_path = f.name
|
||||
try:
|
||||
timer = DetectionTimer(
|
||||
gateway_state_path=gateway_path,
|
||||
detection_interval=10.0,
|
||||
)
|
||||
removed = timer._stale_recovery()
|
||||
|
||||
# Eigene PID lebt → kein Eintrag entfernt
|
||||
assert removed == 0, f"Expected 0 removed, got {removed}"
|
||||
assert tracker.count() == 1, f"Expected 1 entry, got {tracker.count()}"
|
||||
finally:
|
||||
os.unlink(gateway_path)
|
||||
try:
|
||||
os.unlink(subagent_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# Test 11: Stale-Recovery — expected_done_at überschritten + PID tot
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
class TestStaleRecoveryExpectedDoneAt:
|
||||
"""Test 11: expected_done_at als Sekundär-Indikator."""
|
||||
|
||||
def test_expected_done_at_exceeded_pid_dead_removed(self):
|
||||
"""expected_done_at überschritten + PID tot → Eintrag entfernt."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer
|
||||
from plugin.archivar.subagent_tracker import (
|
||||
_write_subagent_state, _get_subagent_state_path, get_tracker,
|
||||
)
|
||||
|
||||
subagent_path = _get_subagent_state_path()
|
||||
tracker = get_tracker()
|
||||
|
||||
# expected_done_at in der Vergangenheit + tote PID
|
||||
_write_subagent_state(subagent_path, {
|
||||
"active": [{
|
||||
"task_id": "t_expired_dead",
|
||||
"type": "background_task",
|
||||
"owner_room": "user:bernd",
|
||||
"pid": 99996,
|
||||
"started_at": "2025-01-01T00:00:00+00:00",
|
||||
"expected_duration_s": 600,
|
||||
"expected_done_at": "2025-01-01T00:10:00+00:00", # lange überschritten
|
||||
"plugin_state_source": "adapter.subagent_tracker",
|
||||
}],
|
||||
"last_change_iso": "",
|
||||
"schema_version": 1,
|
||||
"source": "plugin_internal",
|
||||
})
|
||||
|
||||
assert tracker.count() == 1
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
gateway_path = f.name
|
||||
try:
|
||||
timer = DetectionTimer(
|
||||
gateway_state_path=gateway_path,
|
||||
detection_interval=10.0,
|
||||
)
|
||||
removed = timer._stale_recovery()
|
||||
|
||||
# PID tot → entfernt (expected_done_at ist sekundär, PID ist primär)
|
||||
assert removed == 1, f"Expected 1 removed, got {removed}"
|
||||
assert tracker.count() == 0
|
||||
finally:
|
||||
os.unlink(gateway_path)
|
||||
try:
|
||||
os.unlink(subagent_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def test_expected_done_at_exceeded_pid_alive_kept(self):
|
||||
"""expected_done_at überschritten aber PID lebt → Eintrag bleibt (long-running)."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer
|
||||
from plugin.archivar.subagent_tracker import (
|
||||
_write_subagent_state, _get_subagent_state_path, get_tracker,
|
||||
)
|
||||
|
||||
subagent_path = _get_subagent_state_path()
|
||||
tracker = get_tracker()
|
||||
|
||||
own_pid = os.getpid()
|
||||
|
||||
# expected_done_at in der Vergangenheit, aber PID lebt
|
||||
_write_subagent_state(subagent_path, {
|
||||
"active": [{
|
||||
"task_id": "t_long_running",
|
||||
"type": "background_task",
|
||||
"owner_room": "user:bernd",
|
||||
"pid": own_pid,
|
||||
"started_at": "2025-01-01T00:00:00+00:00",
|
||||
"expected_duration_s": 600,
|
||||
"expected_done_at": "2025-01-01T00:10:00+00:00", # überschritten
|
||||
"plugin_state_source": "adapter.subagent_tracker",
|
||||
}],
|
||||
"last_change_iso": "",
|
||||
"schema_version": 1,
|
||||
"source": "plugin_internal",
|
||||
})
|
||||
|
||||
assert tracker.count() == 1
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
gateway_path = f.name
|
||||
try:
|
||||
timer = DetectionTimer(
|
||||
gateway_state_path=gateway_path,
|
||||
detection_interval=10.0,
|
||||
)
|
||||
removed = timer._stale_recovery()
|
||||
|
||||
# PID lebt → Eintrag bleibt, auch wenn expected_done_at überschritten
|
||||
assert removed == 0, f"Expected 0 removed, got {removed}"
|
||||
assert tracker.count() == 1, f"Expected 1 entry, got {tracker.count()}"
|
||||
finally:
|
||||
os.unlink(gateway_path)
|
||||
try:
|
||||
os.unlink(subagent_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user