refactor(agent): centralize session execution ownership (#6699)

This commit is contained in:
chengyongru authored and GitHub committed 2026-11-18 13:49:41 +08:00
1 parent 53a556585e
commit 5997a714f2
16 files changed
+642 -286

No files matched your search

@@ -0,0 +1,77 @@
--------------------------- MODULE CombinedLifecycle -------------------------
CONSTANT JoinControls
Keys == {"A", "B"}
VARIABLES phase, workers, controls, controlled, submitted
vars == <<phase, workers, controls, controlled, submitted>>
Init ==
/\ phase = "open"
/\ workers = [k \in Keys |-> "running"]
/\ controls = {}
/\ controlled = {}
/\ submitted = {}
BeginControl(k) ==
/\ phase = "open"
/\ k \notin controlled
/\ controls' = controls \cup {k}
/\ controlled' = controlled \cup {k}
/\ workers' = [workers EXCEPT ![k] = IF @ = "running" THEN "cancelling" ELSE @]
/\ UNCHANGED <<phase, submitted>>
FinishControl(k) ==
/\ k \in controls
/\ workers[k] \in {"none", "queued"}
/\ controls' = controls \ {k}
/\ UNCHANGED <<phase, workers, controlled, submitted>>
SubmitReplacement(k) ==
/\ phase = "open"
/\ workers[k] = "none"
/\ k \notin submitted
/\ workers' = [workers EXCEPT ![k] = "queued"]
/\ submitted' = submitted \cup {k}
/\ UNCHANGED <<phase, controls, controlled>>
StartReplacement(k) ==
/\ workers[k] = "queued"
/\ k \notin controls
/\ workers' = [workers EXCEPT ![k] = "running"]
/\ UNCHANGED <<phase, controls, controlled, submitted>>
FinishWorker(k) ==
/\ workers[k] \in {"running", "cancelling"}
/\ workers' = [workers EXCEPT ![k] = "none"]
/\ UNCHANGED <<phase, controls, controlled, submitted>>
BeginClose ==
/\ phase = "open"
/\ phase' = "closing"
/\ workers' = [k \in Keys |-> IF workers[k] = "none" THEN "none" ELSE "cancelling"]
/\ UNCHANGED <<controls, controlled, submitted>>
TearDown ==
/\ phase = "closing"
/\ \A k \in Keys: workers[k] = "none"
/\ ~JoinControls \/ controls = {}
/\ phase' = "closed"
/\ UNCHANGED <<workers, controls, controlled, submitted>>
Next ==
\/ BeginClose
\/ TearDown
\/ \E k \in Keys:
BeginControl(k) \/ FinishControl(k) \/ SubmitReplacement(k)
\/ StartReplacement(k) \/ FinishWorker(k)
Spec ==
/\ Init
/\ [][Next]_vars
/\ WF_vars(TearDown)
/\ \A k \in Keys: WF_vars(FinishControl(k)) /\ WF_vars(FinishWorker(k))
CloseJoinsAllOwners ==
phase = "closed" => (controls = {} /\ \A k \in Keys: workers[k] = "none")
CloseEventuallyCompletes == (phase = "closing") ~> (phase = "closed")
=============================================================================
+17
View File
@@ -0,0 +1,17 @@
# Session inbox lifecycle model
`CombinedLifecycle.tla` models shutdown admission, worker lifetime, and control handoff across two session keys, with at most one control and one replacement per key. It checks that shutdown waits for all workers and controls before closing shared resources, and that shutdown eventually finishes if workers and controls can complete their cleanup.
## Run the checks
Use [TLC 2.19 (TLA+ v1.7.4)](https://github.com/tlaplus/tlaplus/releases/tag/v1.7.4). Run these commands from a scratch copy of this directory so TLC output stays out of the source tree. Replace `/path/to/tla2tools.jar` with the path to the downloaded JAR.
```sh
java -XX:+UseParallelGC -cp "/path/to/tla2tools.jar" tlc2.TLC -config combined-final.cfg CombinedLifecycle.tla
java -XX:+UseParallelGC -cp "/path/to/tla2tools.jar" tlc2.TLC -config combined-baseline.cfg CombinedLifecycle.tla
```
- `combined-final.cfg` checks the current model and exhausts 333 distinct states without a violation.
- `combined-baseline.cfg` disables joining in-flight controls. It must violate `CloseJoinsAllOwners`: shutdown can close resources while a control is still running after its worker exits.
The Python replay is `test_shutdown_joins_control_when_no_worker_remains` in [the inbox regression tests](../../tests/agent/test_session_inbox_controls.py). The same tests cover automation arriving during queue retirement and replacement workers waiting for cleanup. The model does not verify the Python implementation and excludes direct SDK calls, forced shutdown deadlines, subprocess termination, and SQLite recovery. The smaller models used during diagnosis are preserved in [the original PR snapshot](https://github.com/HKUDS/nanobot/tree/f552eccf358cd5bbe5f295e6138921fb0d425475/verification/async-session-stop).
@@ -0,0 +1,5 @@
CONSTANT JoinControls = FALSE
SPECIFICATION Spec
INVARIANT CloseJoinsAllOwners
PROPERTY CloseEventuallyCompletes
CHECK_DEADLOCK FALSE
@@ -0,0 +1,5 @@
CONSTANT JoinControls = TRUE
SPECIFICATION Spec
INVARIANT CloseJoinsAllOwners
PROPERTY CloseEventuallyCompletes
CHECK_DEADLOCK FALSE