Repository navigation
fix(terminal): recycle dead shell-integration terminals before reuse #2862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7ff5d48
cfc9266
45b79f7
aeacc2b
874c943
ba4ab5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,287 @@ | ||
| ---- MODULE TerminalReuse ---- | ||
| (* Models TerminalRegistry.getOrCreateTerminal in | ||
| src/integrations/terminal/TerminalRegistry.ts. | ||
| Source: ShellDies and FailIntegration. ShellDies is a death VS Code | ||
| reports (Terminal.shellIntegration drops), during a command or while | ||
| idle. FailIntegration is a death the environment does not report; only | ||
| the command fails (no_shell_integration: the integration wait or the | ||
| stream start times out). MarkDead records that observed failure. | ||
| Sink: RunReused and RunNew select a terminal for a command; the Sweep | ||
| runs first. Selections overlap: a command can start while another one | ||
| runs on another terminal. | ||
| Failure: execute_command keeps reusing a dead shell-integration terminal | ||
| after long sessions, so commands fail with Shell Integration Error until | ||
| a manual restart. | ||
| Properties: | ||
| NoDeadSelection - a selection never places a command on a dead terminal | ||
| (the sweep recycles it, and the gate refuses what the sweep kept). | ||
| NoDeadIdleAtSelection - after a selection, no dead idle terminal that the | ||
| sweep may recycle stays in the registry. | ||
| BusyTerminalKept - a selection never removes a busy terminal: the | ||
| command on it keeps its terminal. | ||
| UnreadOutputKept - a selection never removes a terminal that holds | ||
| output no consumer read yet; ReadOutput releases it for the next sweep. | ||
| RunningExecutionKept - a selection never removes a terminal whose | ||
| submitted execution can still run; ExecEnd releases it. | ||
| StalledNotPinnedForever - after a selection, no terminal whose stalled | ||
| execution outlived its bounded deferral stays in the registry: the | ||
| sweep disposes it even though VS Code never reported the execution's | ||
| end. A running execution (no stall observed) never expires. | ||
| MarkedOnlyIfConfirmed - only a terminal that was proven before its | ||
| command is marked dead by a failure; a first command that fails stays | ||
| reusable (the retry path). | ||
| Expected before the run: the base cfg passes. NoRecycleSweep and | ||
| NoStallSweep fail NoDeadIdleAtSelection. NoDeadGate fails | ||
| NoDeadSelection. NoBusyGuard fails BusyTerminalKept. NoUnreadGuard | ||
| fails UnreadOutputKept. NoExecutionGuard fails RunningExecutionKept. | ||
| NoConfirmationGate fails MarkedOnlyIfConfirmed. NoBoundedRelease | ||
| fails StalledNotPinnedForever. | ||
| Run each cfg on TerminalReuseMC.tla. The MC module holds the sabotage | ||
| operators. | ||
| Result: the base cfg passes in 2,025 distinct states (under 10 s; | ||
| 1,089 before the round-3 expired field, 1,369 after it). NoRecycleSweep | ||
| fails NoDeadIdleAtSelection in 112 states. NoStallSweep fails | ||
| NoDeadIdleAtSelection in 234 states. NoDeadGate fails | ||
| NoDeadSelection in 117 states. NoBusyGuard fails BusyTerminalKept | ||
| in 50 states. NoUnreadGuard fails UnreadOutputKept in 117 states. | ||
| NoExecutionGuard fails RunningExecutionKept in 118 states. | ||
| NoConfirmationGate fails MarkedOnlyIfConfirmed in 21 states. | ||
| NoBoundedRelease fails StalledNotPinnedForever in 239 states. Counts | ||
| reproduced twice (round-5 numbers; the round-3 cfg family measured | ||
| 108/216/113/50/113/114/21/374 on the pre-round-5 model). | ||
| Review ratchet (PR #2862, CodeRabbit round 1): 2 covered, 2 added, 3 gap. | ||
| Added: overlapping commands, ShellDies during a command, the selection | ||
| gate, BusyTerminalKept, NoDeadSelection as a selection-time property | ||
| (discussion_r4808051264). Added: the unread field, ReadOutput, | ||
| UnreadOutputKept, cfg NoUnreadGuard (discussion_r4808051294). | ||
| Covered: a stream-start failure on a confirmed terminal is FailIntegration | ||
| (discussion_r4808051286). Covered: the sweep removes the entry whatever | ||
| VS Code's dispose does; dispose is an oracle (Regression Evidence 3). | ||
| Gap: the pending-call test rule (discussion_r4808051276) and the | ||
| runCommand success-path and constructor evidence tests (Regression | ||
| Evidence 1, 2) are test-evidence demands, not behavior. | ||
| Review ratchet (PR #2862, CodeRabbit round 2): 2 covered, 2 added, 1 gap. | ||
| Added: confirmed is recorded when the command starts and FailIntegration | ||
| marks only a confirmed terminal, MarkedOnlyIfConfirmed, cfg | ||
| NoConfirmationGate (discussion_r4809292609). Added: exec, ExecEnd, | ||
| RunningExecutionKept, cfg NoExecutionGuard (discussion_r4809292614). | ||
| Covered: a cmd.exe terminal is never confirmed, so FailIntegration never | ||
| marks it (Regression Evidence 1). Covered: both selection gates are | ||
| Reuseable (Regression Evidence 2). Gap: the pending-call rule for the two | ||
| stream-timeout tests (discussion_r4809292612) is a test rule. | ||
| Review ratchet (PR #2862, CodeRabbit round 3): 0 covered, 1 added, | ||
| 1 gap. Added: expired, DeferralElapses, StalledNotPinnedForever, the | ||
| sweep keeps only a non-expired execution, cfg NoBoundedRelease | ||
| (Lifecycle Resource Cleanup pre-merge check, review 6200865221). | ||
| Gap: the pending-call rule for the timeout test | ||
| (discussion_r4809359735) is a test rule, not model behavior. | ||
| Review ratchet (PR #2862, CodeRabbit round 4): 0 covered, 0 added, | ||
| 1 gap. Gap: the exact-deferral-boundary test rule (1 ms before and | ||
| after expiry, discussion_r4816998303) is a test rule, not model | ||
| behavior. | ||
| Review ratchet (PR #2862, CodeRabbit round 5): 1 added, 1 gap. | ||
| Added: DeferralElapses expires every open execution, not only a | ||
| confirmed terminal's (the cold retry path leaves an orphaned | ||
| execution that pins a later-dead terminal), and Pick restarts the | ||
| deferral clock on reuse (Lifecycle Resource Cleanup, review | ||
| 6211060141). Gap: a time-free safety invariant cannot tell a cold | ||
| orphan from a running execution before the deferral fires, so the | ||
| polarity for the cold path is the sabotage-verified unit test | ||
| (Regression Evidence, review 6211060141). *) | ||
|
|
||
| EXTENDS Naturals, FiniteSets | ||
|
|
||
| Ids == {1, 2} \* two slots: one busy terminal plus one replacement | ||
|
|
||
| VARIABLE registry \* set of entries, one per registered VS Code terminal | ||
|
|
||
| (* integ is the VS Code shell-integration report. seen records that the | ||
| integration was confirmed at least once. confirmed records that the | ||
| terminal was seen when its command started (Terminal.runCommand captures | ||
| confirmedBefore). stalled marks a terminal whose command observed a | ||
| failure the report does not show. unread marks output of a completed | ||
| command that no consumer read yet. exec marks a submitted execution that | ||
| can still run (Terminal.activeShellExecution is set). expired marks an | ||
| execution whose bounded deferral passed without an end event. | ||
| busy mirrors RooTerminal.busy. A closed terminal leaves the registry | ||
| (onDidCloseTerminal removes it). *) | ||
| Entry(id) == | ||
| [id |-> id, integ |-> "off", seen |-> FALSE, confirmed |-> FALSE, busy |-> FALSE, stalled |-> FALSE, unread |-> FALSE, | ||
| exec |-> FALSE, expired |-> FALSE] | ||
|
|
||
| IsDead(e) == e.seen /\ (e.integ = "off" \/ e.stalled) \* confirmed once, dead now | ||
|
|
||
| (* The selection gate (the find calls in getOrCreateTerminal). A dead | ||
| terminal the sweep kept (busy, unread output, or a running execution) | ||
| is not selected. *) | ||
| Reuseable(e) == ~e.busy /\ ~IsDead(e) | ||
|
|
||
| (* The sweep before a selection (recycleTerminal): recycle dead idle | ||
| terminals. Keep busy terminals, terminals that hold unread output, and | ||
| terminals whose submitted execution has not outlived its bounded | ||
| deferral. *) | ||
| Sweep(reg) == {e \in reg : ~IsDead(e) \/ e.busy \/ e.unread \/ (e.exec /\ ~e.expired)} | ||
|
|
||
| (* The marking records an observed integration failure (no_shell_integration | ||
| on a terminal that was confirmed before the command). *) | ||
| MarkDead(e) == [e EXCEPT !.stalled = TRUE] | ||
|
|
||
| (* The command starts on the terminal. confirmed captures whether the | ||
| terminal was proven before this command. The deferral clock restarts: | ||
| the new command's stall is measured on its own. *) | ||
| Pick(e) == [e EXCEPT !.busy = TRUE, !.confirmed = e.seen, !.expired = FALSE] | ||
|
|
||
| Init == registry = {} | ||
|
|
||
| (* getOrCreateTerminal spawns a fresh terminal and the command claims it. *) | ||
| RunNew == | ||
| \E id \in Ids : | ||
| /\ ~\E e \in Sweep(registry) : e.id = id | ||
| /\ registry' = Sweep(registry) \cup {Pick(Entry(id))} | ||
|
|
||
| (* getOrCreateTerminal reuses an idle terminal for the command. *) | ||
| RunReused == | ||
| \E e \in Sweep(registry) : | ||
| /\ Reuseable(e) | ||
| /\ registry' = {f \in Sweep(registry) : f.id /= e.id} \cup {Pick(e)} | ||
|
|
||
| (* shellExecutionComplete releases the terminal after the command. The | ||
| completed command can leave output that no consumer read yet. *) | ||
| CompleteCommand == | ||
| \E e \in registry : | ||
| /\ e.busy | ||
| /\ ~e.exec | ||
| /\ \E u \in BOOLEAN : | ||
| registry' = {f \in registry : f.id /= e.id} \cup {[e EXCEPT !.busy = FALSE, !.unread = e.unread \/ u]} | ||
|
|
||
| (* A consumer (getEnvironmentDetails) reads the unread output. *) | ||
| ReadOutput == | ||
| \E e \in registry : | ||
| /\ e.unread | ||
| /\ registry' = {f \in registry : f.id /= e.id} \cup {[e EXCEPT !.unread = FALSE]} | ||
|
|
||
| (* VS Code reports the end of the submitted execution | ||
| (onDidEndTerminalShellExecution clears Terminal.activeShellExecution). *) | ||
| ExecEnd == | ||
| \E e \in registry : | ||
| /\ e.exec | ||
| /\ registry' = {f \in registry : f.id /= e.id} \cup {[e EXCEPT !.exec = FALSE]} | ||
|
|
||
| (* The bounded deferral for a submitted execution expires (PR #2862 | ||
| rounds 3 and 5, Lifecycle Resource Cleanup): the stream start timed | ||
| out, VS Code never reported the execution's end, and one more | ||
| shell-integration timeout passed. The next sweep disposes the | ||
| terminal. Every stream-start timeout expires, on a cold terminal too; | ||
| a running execution never does. The deferral is consulted only for an | ||
| idle terminal (the sweep checks busy first). *) | ||
| DeferralElapses == | ||
| \E e \in registry : | ||
| /\ e.exec | ||
| /\ ~e.busy | ||
| /\ ~e.expired | ||
| /\ registry' = {f \in registry : f.id /= e.id} \cup {[e EXCEPT !.expired = TRUE]} | ||
|
|
||
| (* Shell integration activates (waitForShellIntegration resolves, or an | ||
| adopted terminal reports it): the report turns on and the terminal | ||
| counts as proven. *) | ||
| Activate == | ||
| \E e \in registry : | ||
| /\ ~e.seen | ||
| /\ ~e.stalled | ||
| /\ e.integ = "off" | ||
| /\ registry' = {f \in registry : f.id /= e.id} \cup {[e EXCEPT !.integ = "on", !.seen = TRUE]} | ||
|
|
||
| (* A long session kills the shell behind the terminal, and VS Code reports | ||
| it, during a command or while idle. The terminal object stays. *) | ||
| ShellDies == | ||
| \E e \in registry : | ||
| /\ e.seen | ||
| /\ ~e.stalled | ||
| /\ e.integ = "on" | ||
| /\ registry' = {f \in registry : f.id /= e.id} \cup {[e EXCEPT !.integ = "off"]} | ||
|
|
||
| (* A command on a proven terminal fails (no_shell_integration). The wait | ||
| timeout submitted nothing; the stream timeout submitted the command, so | ||
| an execution can still run (exec). A terminal confirmed before the | ||
| command is marked dead; a terminal proven inside this command stays | ||
| reusable (the retry path). The command ends at once. *) | ||
| FailIntegration == | ||
| \E e \in registry : | ||
| /\ e.busy | ||
| /\ e.seen | ||
|
gh311 marked this conversation as resolved.
Resolved
|
||
| /\ ~e.stalled | ||
| /\ e.integ = "on" | ||
| /\ \E s \in BOOLEAN : | ||
| IF e.confirmed | ||
| THEN registry' = {f \in registry : f.id /= e.id} \cup {[MarkDead(e) EXCEPT !.busy = FALSE, !.exec = s]} | ||
| ELSE registry' = {f \in registry : f.id /= e.id} \cup {[e EXCEPT !.busy = FALSE, !.exec = s]} | ||
|
|
||
| (* The user closes an idle terminal (onDidCloseTerminal removes it). *) | ||
| CloseTerminal == | ||
| \E e \in registry : | ||
| /\ ~e.busy | ||
| /\ ~e.exec | ||
| /\ registry' = {f \in registry : f.id /= e.id} | ||
|
|
||
| Next == RunNew \/ RunReused \/ CompleteCommand \/ ReadOutput \/ ExecEnd \/ Activate \/ ShellDies \/ FailIntegration \/ CloseTerminal \/ DeferralElapses | ||
|
|
||
| Spec == Init /\ [][Next]_registry | ||
|
|
||
| TypeOK == | ||
| /\ \A e \in registry : | ||
| /\ e.id \in Ids | ||
| /\ e.integ \in {"on", "off"} | ||
| /\ e.seen \in BOOLEAN | ||
| /\ e.confirmed \in BOOLEAN | ||
| /\ e.busy \in BOOLEAN | ||
| /\ e.stalled \in BOOLEAN | ||
| /\ e.unread \in BOOLEAN | ||
| /\ e.exec \in BOOLEAN | ||
| /\ e.expired \in BOOLEAN | ||
| /\ (e.integ = "on") => e.seen | ||
| /\ e.stalled => e.seen | ||
| /\ e.confirmed => e.seen | ||
| /\ Cardinality({f \in registry : f.id = e.id}) = 1 | ||
|
|
||
| Selection == RunNew \/ RunReused | ||
|
|
||
| (* Selection-time safety: a command is never placed on a dead terminal. The | ||
| terminal keeps its state; only busy and confirmed change (a fresh spawn | ||
| has another state, so it is not this terminal). *) | ||
| NoDeadSelection == | ||
| [][ Selection => \A e \in registry : (~e.busy /\ Pick(e) \in registry') => ~IsDead(e) ]_registry | ||
|
|
||
| (* After a selection, no dead idle terminal that the sweep may recycle | ||
| stays in the registry: the sweep recycled it, so dead terminals do not | ||
| pile up. *) | ||
| NoDeadIdleAtSelection == | ||
| [][ Selection => ~\E e \in registry' : IsDead(e) /\ ~e.busy /\ ~e.unread /\ ~e.exec ]_registry | ||
|
|
||
| (* Continued execution: the sweep never removes a busy terminal. *) | ||
| BusyTerminalKept == | ||
| [][ Selection => \A e \in registry : e.busy => e \in registry' ]_registry | ||
|
|
||
| (* The sweep never removes a terminal that holds unread output. A live | ||
| terminal with unread output can be selected; its output stays. *) | ||
| UnreadOutputKept == | ||
| [][ Selection => \A e \in registry : e.unread => \E f \in registry' : f.id = e.id /\ f.unread ]_registry | ||
|
|
||
| (* The sweep never removes a terminal whose submitted execution can still | ||
| run: a dispose can stop that command. A stalled execution whose deferral | ||
| expired is not protected (StalledNotPinnedForever). *) | ||
| RunningExecutionKept == | ||
| [][ Selection => \A e \in registry : (e.exec /\ ~e.expired) => \E f \in registry' : f.id = e.id /\ f.exec /\ ~f.expired ]_registry | ||
|
|
||
| (* After a selection, no abandoned execution pins a DEAD terminal in the | ||
| registry: the sweep disposed it. VS Code staying silent about the | ||
| execution's end pins nothing forever. An alive cold terminal whose | ||
| deferral expired stays on the retry path (MarkedOnlyIfConfirmed), and | ||
| unread output still holds the terminal for its reader | ||
| (UnreadOutputKept); the next sweep after ReadOutput disposes it. *) | ||
| StalledNotPinnedForever == | ||
| [][ Selection => ~\E e \in registry' : IsDead(e) /\ e.exec /\ e.expired /\ ~e.unread ]_registry | ||
|
|
||
| (* A failure marks only a terminal that was confirmed before its command. *) | ||
| MarkedOnlyIfConfirmed == \A e \in registry : e.stalled => e.confirmed | ||
|
|
||
| ==== | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Represent an unset stall clock separately from an unexpired clock.
Picksetsexpired=FALSE, but production clearsstalledExecutionAtinstead of starting another clock (Terminal.ts, Line 133).A cold stream timeout can leave an execution while permitting reuse. If integration drops before the retry starts, its wait fails without submitting another command. The failure releases
busyand retains the old execution (TerminalProcess.ts, Lines 43-47). No new timestamp is recorded (Terminal.ts, Lines 170-171). Recycling then keeps that terminal until an execution-end event arrives.The model cannot check this changed outcome.
FailIntegrationrequires the report to remain on and replaces the execution flag.DeferralElapsesalso permits expiry without a recorded timestamp.Track clock presence independently of integration loss. Include wait failure with a retained execution. Keep expiry available for submitted cold failures.
As per coding guidelines, model “The actions and every order of them.”
Source: Coding guidelines