fix(cookbook): preserve state during lifecycle tick
Log malformed cookbook state and re-read fresh state before writing scheduled-stop mutations so concurrent UI changes are preserved.
This commit is contained in:
parent
4d070ef4cb
commit
be046dd29a
1 changed files with 22 additions and 3 deletions
|
|
@ -136,7 +136,8 @@ async def _tick() -> None:
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
state = json.loads(state_path.read_text(encoding="utf-8"))
|
state = json.loads(state_path.read_text(encoding="utf-8"))
|
||||||
except Exception:
|
except Exception as e:
|
||||||
|
logger.warning("cookbook_serve_lifecycle: state file unreadable (%s), skipping tick", e)
|
||||||
return
|
return
|
||||||
tasks = state.get("tasks") or []
|
tasks = state.get("tasks") or []
|
||||||
now_ms = int(time.time() * 1000)
|
now_ms = int(time.time() * 1000)
|
||||||
|
|
@ -178,8 +179,26 @@ async def _tick() -> None:
|
||||||
if stopped_any:
|
if stopped_any:
|
||||||
try:
|
try:
|
||||||
from core.atomic_io import atomic_write_json
|
from core.atomic_io import atomic_write_json
|
||||||
state["tasks"] = tasks
|
# Re-read the state file so concurrent UI writes (task adds,
|
||||||
atomic_write_json(state_path, state)
|
# status flips, config edits) are not silently overwritten.
|
||||||
|
# Apply only our stop mutations to the fresh snapshot.
|
||||||
|
try:
|
||||||
|
fresh = json.loads(state_path.read_text(encoding="utf-8"))
|
||||||
|
fresh_tasks = fresh.get("tasks") or []
|
||||||
|
except Exception:
|
||||||
|
fresh = state
|
||||||
|
fresh_tasks = tasks
|
||||||
|
stopped_sids = {sid for sid, _, _ in to_stop}
|
||||||
|
for ft in fresh_tasks:
|
||||||
|
if not isinstance(ft, dict):
|
||||||
|
continue
|
||||||
|
ft_sid = ft.get("sessionId") or ft.get("id")
|
||||||
|
if ft_sid in stopped_sids:
|
||||||
|
ft["status"] = "stopped"
|
||||||
|
ft["_scheduledStopAtMs"] = None
|
||||||
|
ft["_lastStatusFlipAt"] = now_ms
|
||||||
|
fresh["tasks"] = fresh_tasks
|
||||||
|
atomic_write_json(state_path, fresh)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"cookbook_serve_lifecycle: state write failed: {e}")
|
logger.warning(f"cookbook_serve_lifecycle: state write failed: {e}")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue