Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
6602cb6
fix(mentions): restrict expansion to authorized user input
coderabbitai[bot] Nov 10, 2026
4d0fc86
fix(mentions): fail closed on mention authorization
edelauna Nov 14, 2026
03ce31a
fix(mentions): carry add-to-context authorization through the chat box
edelauna Nov 14, 2026
b817c3a
fix(mentions): scope ask response and queued message authorization
edelauna Nov 14, 2026
230c8fe
fix(mentions): scope image, queue, and generated-text authorization
edelauna Nov 14, 2026
da69d02
fix(mentions): keep scope for edits, suggestions, and queued input
edelauna Nov 15, 2026
24ff6ac
refactor(mentions): carry typed text segments instead of an authorize…
edelauna Nov 15, 2026
ebfc24d
fix(mentions): keep generated text generated across edits and history
edelauna Nov 15, 2026
9e7f793
test(mentions): cover interleaved segments and resume responses
edelauna Nov 15, 2026
64f45d1
test(mentions): cover headless sendMessage and resume source checks
edelauna Nov 15, 2026
4f891b6
fix(mentions): send segments from the API and approval feedback
edelauna Nov 15, 2026
e43d2f4
test(mentions): cover approve and reject feedback segments
edelauna Nov 15, 2026
b435890
refactor(mentions): share one segment helper for chat box sends
edelauna Nov 15, 2026
2ddf7b1
test(mentions): cover generated resume feedback segments
edelauna Nov 15, 2026
e26fa56
test(mentions): cover cloned and edited block fail-closed paths
edelauna Nov 15, 2026
a8cb7fd
test(mentions): assert returned content in rejection tests
edelauna Nov 15, 2026
3838bfa
refactor(mentions): expand user mentions where input enters the task
edelauna Nov 15, 2026
8f4b988
fix(mentions): show the request row before user mentions expand
edelauna Nov 15, 2026
6a0090b
fix(mentions): close two review gaps found with TLA+ specs
edelauna Nov 16, 2026
d25d9d6
test(task): cover cancel from a TaskStarted listener during mention e…
edelauna Nov 16, 2026
0bb8a72
fix(mentions): mark the draft generated after native undo and redo
edelauna Nov 16, 2026
8614709
test(mentions): model history restore and native undo in one spec
edelauna Nov 16, 2026
57d303c
test(mentions): model history navigation state and any undo target
edelauna Nov 16, 2026
f45c18b
test(mentions): end history navigation on native undo and redo
edelauna Nov 16, 2026
fc0a5d4
fix(mentions): tag queue edits per input event
edelauna Nov 17, 2026
dc60383
fix(mentions): tag queue edits per input event
edelauna Nov 17, 2026
5d62615
fix(mentions): let a kept image mention take no extra slot
edelauna Nov 17, 2026
599fca5
fix(mentions): keep a queued edit when the queue drains
edelauna Nov 17, 2026
a321e8e
fix(mentions): hold a queued message while an edit scans images
edelauna Nov 17, 2026
7a132cc
fix(mentions): retry a drain that an edit blocked
edelauna Nov 17, 2026
78be966
fix(mentions): retry every drain that an edit blocked
edelauna Nov 17, 2026
a5ee634
fix(mentions): bind blocked drains to the messages they found held
edelauna Nov 17, 2026
c943639
fix(mentions): let the queue own blocked drains
edelauna Nov 17, 2026
7cdd765
fix(mentions): count each blocked drain once in the drain checks
edelauna Nov 17, 2026
2680692
fix(task): write resume history after the mention expansion
edelauna Nov 17, 2026
9d9051d
fix(task): prevent cancelled resume preparation from overwriting history
edelauna Nov 18, 2026
5edeb14
test(lifecycle): model queued message landing after drain
edelauna Nov 19, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/architecture/task-lifecycle-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ Merged PR #1625 changed the confirmed downstream readers: `getEnvironmentDetails

`scripts/check-delegated-mode-readers.ts` does not execute `getEnvironmentDetails`, `presentAssistantMessage`, or `validateToolUse`. It checks the production handoff selector and a pure built-in-mode permission divergence, establishing that the wrong source is observable. It is refinement support, not exhaustive reader coverage. Other mode-sensitive consumers remain an explicit inventory gap, and this suite does not claim universal task-local reader isolation.

## Queue ask landing model

`scripts/check-queue-ask-landing.ts` is a bounded model of where a queued message lands after `MessageQueueService` drains it. It imports the real `MessageQueueService` and the `nextRunState` kernel. The `askResponse` slot and the `setTimeout(0)` hop in `Task.ts` are model abstractions, because `Task.ts` needs VS Code.

The invariant is that every added message is queued, in flight, in the slot, or consumed. Nothing is dropped. The production policy sets the slot with no ask pending, and the next `ask()` resets it. The model keeps this gap as a known witness (#2421). The requeue policy is the proposed fix and must stay clean. Remove the witness when production changes.

## Task fan-out protocol model

`scripts/check-task-fanout-protocol.ts` is a separate, optional bounded protocol model for the #369/#372 fan-out safety contract. It explores a live parent with two sibling slots, a two-permit scheduler, independent result readiness, explicit child-to-parent delivery, parent loss, orphan cancellation, and permit release. It checks scheduler capacity and exact permit ownership, one writer and at-most-once delivery per child, readiness before delivery, and no result routing after parent loss. Named landmarks require concurrent siblings beneath a live parent, out-of-order result delivery, single-writer results, parent loss while work is running, and complete orphan cleanup to remain reachable. Injected unsafe states confirm those invariants reject wrong writers, early and duplicate delivery, post-parent-loss routing, and scheduler over-allocation.
Expand Down
4 changes: 2 additions & 2 deletions docs/architecture/tla/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Write the unit tests and do the review as usual. The spec does not replace them.
| replay | Run a counterexample on the real code. It shows if the counterexample is real. |
| sabotage check | Remove a guard on purpose. The invariant must fail. If not, the invariant is vacuous. |
| vacuous | True in all states because of the model, not because of the code. |
| gate | The check an agent runs by hand on each commit. No script or CI step runs it yet. |
| gate | The check an agent runs on each commit. `check.sh` runs it. No CI step runs it yet. |

## Procedure

Expand All @@ -53,7 +53,7 @@ Write the unit tests and do the review as usual. The spec does not replace them.
9. Run TLC on the fixed code. The invariant must hold.
10. Write a property test from the same invariant sentence. Run it on the real code.
11. Simplify the spec. Use the questions below.
12. Run each TLC config and the property test by hand on each fix commit. Write the result in the spec header.
12. On each fix commit, run `TLA_JAR=<jar> ./check.sh <Spec>` for each spec you changed, and the property test. Run `./check.sh all` only for a release or a change to `check.sh`. `Spec.cfg` must pass. Each `Spec.Variant.cfg` must fail. Write the result in the spec header.

## Review ratchet

Expand Down
12 changes: 12 additions & 0 deletions docs/architecture/tla/MentionOrigin.Code.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SPECIFICATION Spec
CONSTANTS
Alphabet = {"a", "b"}
MaxLen = 2
MaxInit = 2
MaxRepl = 2
Retagger = "Code"
HistoryRestore = "Saved"
UndoRestore = "AllGenerated"
QueueEdit = "PerEvent"
INVARIANTS TypeOK NoGeneratedTagged
CHECK_DEADLOCK FALSE
12 changes: 12 additions & 0 deletions docs/architecture/tla/MentionOrigin.History.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SPECIFICATION Spec
CONSTANTS
Alphabet = {"a", "b"}
MaxLen = 2
MaxInit = 2
MaxRepl = 2
Retagger = "Both"
HistoryRestore = "AsEdit"
UndoRestore = "AllGenerated"
QueueEdit = "PerEvent"
INVARIANTS TypeOK NoGeneratedTagged
CHECK_DEADLOCK FALSE
12 changes: 12 additions & 0 deletions docs/architecture/tla/MentionOrigin.Queue.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SPECIFICATION Spec
CONSTANTS
Alphabet = {"a", "b"}
MaxLen = 3
MaxInit = 3
MaxRepl = 2
Retagger = "Both"
HistoryRestore = "Saved"
UndoRestore = "AllGenerated"
QueueEdit = "Batch"
INVARIANTS TypeOK NoGeneratedTagged
CHECK_DEADLOCK FALSE
12 changes: 12 additions & 0 deletions docs/architecture/tla/MentionOrigin.Retained.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SPECIFICATION Spec
CONSTANTS
Alphabet = {"a", "b"}
MaxLen = 2
MaxInit = 2
MaxRepl = 2
Retagger = "Both"
HistoryRestore = "Saved"
UndoRestore = "Retained"
QueueEdit = "PerEvent"
INVARIANTS TypeOK NoGeneratedTagged
CHECK_DEADLOCK FALSE
12 changes: 12 additions & 0 deletions docs/architecture/tla/MentionOrigin.Undo.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SPECIFICATION Spec
CONSTANTS
Alphabet = {"a", "b"}
MaxLen = 2
MaxInit = 2
MaxRepl = 2
Retagger = "Both"
HistoryRestore = "Saved"
UndoRestore = "AsEdit"
QueueEdit = "PerEvent"
INVARIANTS TypeOK NoGeneratedTagged
CHECK_DEADLOCK FALSE
12 changes: 12 additions & 0 deletions docs/architecture/tla/MentionOrigin.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SPECIFICATION Spec
CONSTANTS
Alphabet = {"a", "b"}
MaxLen = 2
MaxInit = 2
MaxRepl = 2
Retagger = "Both"
HistoryRestore = "Saved"
UndoRestore = "AllGenerated"
QueueEdit = "PerEvent"
INVARIANTS TypeOK NoGeneratedTagged
CHECK_DEADLOCK FALSE
148 changes: 148 additions & 0 deletions docs/architecture/tla/MentionOrigin.tla
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
---- MODULE MentionOrigin ----
\* Models applyEdit in src/shared/mentionSegments.ts. The webview calls it on each input event.
\* Source: text typed by the user. Sink: expandUserMentions expands each user-tagged letter.
\* Failure: a letter the user did not type is tagged user. Generated text then expands.
\* Invariant: NoGeneratedTagged. A letter tagged user is a letter the user typed.
\* It replaces a count check (user tags <= typed letters). This form implies the count check.
\* Expected before the run: the edits hold. A change of two ranges would fail with generated text between them.
\* Failure 2 (PR 2421 review 2): the queue editor saves only the final text. QueueSave models two edits, then one save.
\* Replay: paste "@/x" before generated "@/x", then replace the first typed "@". The generated copy turns user.
\* The fix runs the diff from both ends and keeps "u" only where both agree. The webview retags each input event.
\* The server only checks the segments. Property test: mentionSegments.spec.ts, "never tags a generated letter user".
\* Actions: Change (typing), Load (ArrowUp), HistoryRestoreAct (ArrowDown), UndoAct, QueueSave (Batch only).
\* nav is the history navigation state. Load saves the draft only when nav is false.
\* | Config | Variant | Verdict |
\* | MentionOrigin.cfg | Both, Saved, AllGenerated | holds (71,225 states at MaxLen 3) |
\* | MentionOrigin.Code.cfg | retagger of the old code | fails in 2 states |
\* | MentionOrigin.History.cfg | restore through applyEdit | fails in 3 states |
\* | MentionOrigin.Undo.cfg | undo through applyEdit | fails in 2 states |
\* | MentionOrigin.Retained.cfg| undo keeps old tags | fails in 3 states |
\* | MentionOrigin.Queue.cfg | one retag at queue save (Batch) | fails in 3 states |
\* Cost of AllGenerated: after an undo or redo the user must retype a mention that was in the draft.
\* Not modeled: normalizeSegments, merge, trim and the host expansion. Tests: QueuedMessages.spec.tsx, ChatTextArea.spec.tsx.
\* Cost: each run takes under 30 seconds. Agent time is not measured.
\* Review: 0 covered, 8 added, 0 gap (PR 2421). TLC found the equal-letter failure. No review thread named it.
\* Null result: the spec has no action for the send sites. ChatView tests and a mutation check cover them.
\* The first version came after the code and the reviews. It is not an independent first pass.

EXTENDS Naturals, Sequences, FiniteSets

CONSTANTS Alphabet, MaxLen, MaxInit, MaxRepl, Retagger, HistoryRestore, UndoRestore, QueueEdit
\* QueueEdit: "Batch" or "PerEvent". MaxRepl: the longest typed replacement in one edit.
\* Retagger: "Code" or "Both". HistoryRestore: "Saved" or "AsEdit".
\* UndoRestore: "AllGenerated", "Retained" or "AsEdit".

VARIABLES text, tags, snap, nav \* text: letters with true origin. tags: the "u" or "g" flag per letter.

vars == <<text, tags, snap, nav>>

Origins == {"u", "g"}
Cell == [c : Alphabet, o : Origins]
Letters(s) == [p \in 1..Len(s) |-> s[p].c]
Seqs(S, n) == UNION {[1..k -> S] : k \in 0..n}
Min2(x, y) == IF x < y THEN x ELSE y

\* Longest common prefix and suffix of two letter lists. The suffix cannot overlap the prefix.
Pre(a, b) == CHOOSE k \in 0..Min2(Len(a), Len(b)) :
/\ \A p \in 1..k : a[p] = b[p]
/\ k = Min2(Len(a), Len(b)) \/ a[k+1] # b[k+1]
Suf(a, b, k) == CHOOSE m \in 0..(Min2(Len(a), Len(b)) - k) :
/\ \A p \in 1..m : a[Len(a)-p+1] = b[Len(b)-p+1]
/\ m = Min2(Len(a), Len(b)) - k \/ a[Len(a)-m] # b[Len(b)-m]

\* Tags for a diff. k letters keep their tags at the front. m letters keep their tags at the back.
\* The new letters in the middle get the tag f.
TagsFor(oldT, newL, k, m, f) ==
SubSeq(oldT, 1, k)
\o [p \in 1..(Len(newL) - k - m) |-> f]
\o SubSeq(oldT, Len(oldT) - m + 1, Len(oldT))

\* Suffix first. The prefix cannot overlap the suffix.
SufAll(a, b) == Suf(a, b, 0)
PreS(a, b, m) == CHOOSE k \in 0..(Min2(Len(a), Len(b)) - m) :
/\ \A p \in 1..k : a[p] = b[p]
/\ k = Min2(Len(a), Len(b)) - m \/ a[k+1] # b[k+1]

\* The code of applyEdit takes the prefix first. "Both" also takes the suffix first. It tags a letter
\* "u" only if both diffs agree.
Retag(oldL, oldT, newL, f) ==
LET k == Pre(oldL, newL)
A == TagsFor(oldT, newL, k, Suf(oldL, newL, k), f)
m2 == SufAll(oldL, newL)
B == TagsFor(oldT, newL, PreS(oldL, newL, m2), m2, f)
IN IF Retagger = "Code" THEN A
ELSE [p \in 1..Len(A) |-> IF A[p] = "u" /\ B[p] = "u" THEN "u" ELSE "g"]

\* One edit replaces the range i+1..j with the typed letters r. Kept cells keep their origin.
Replace(s, i, j, r) ==
SubSeq(s, 1, i) \o [p \in 1..Len(r) |-> [c |-> r[p], o |-> "u"]] \o SubSeq(s, j+1, Len(s))

Rs == Seqs(Alphabet, MaxRepl)
One(s) == UNION {UNION {{Replace(s, i, j, r) : r \in Rs} : j \in i..Len(s)} : i \in 0..Len(s)}
Edits(s) == {t \in One(s) : Len(t) <= MaxLen}

Init == \E g \in Seqs(Alphabet, MaxInit) :
/\ text = [p \in 1..Len(g) |-> [c |-> g[p], o |-> "g"]]
/\ tags = [p \in 1..Len(g) |-> "g"]
/\ snap = <<>>
/\ nav = FALSE

Now == [t |-> text, g |-> tags]

\* The user types. Typing ends history navigation (resetOnInputChange).
Change == \E t \in Edits(text) :
/\ text' = t
/\ tags' = Retag(Letters(text), tags, Letters(t), "u")
/\ nav' = FALSE
/\ UNCHANGED snap

\* ArrowUp loads a stored prompt as generated text. It can have the draft's letters.
\* It saves the draft only when navigation starts. Later loads keep the first snapshot.
Load == \E g \in Seqs(Alphabet, MaxLen) :
/\ text' = [p \in 1..Len(g) |-> [c |-> g[p], o |-> "g"]]
/\ tags' = [p \in 1..Len(g) |-> "g"]
/\ snap' = IF nav THEN snap ELSE Now
/\ nav' = TRUE

\* ArrowDown restores the saved draft and ends navigation. "AsEdit" sends the restore through applyEdit.
\* The letters can match the stored prompt, so there is no guard on them.
HistoryRestoreAct ==
/\ nav
/\ text' = snap.t
/\ tags' = IF HistoryRestore = "Saved" THEN snap.g
ELSE Retag(Letters(text), tags, Letters(snap.t), "u")
/\ nav' = FALSE
/\ UNCHANGED snap

\* Native undo or redo restores any other text. This covers every older state in the undo stack.
\* The browser sends no event with the old origins, so each letter can have either origin.
\* The input event ends history navigation (ChatTextArea calls resetOnInputChange).
UndoAct == \E t \in Seqs(Cell, MaxLen) :
/\ Letters(t) # Letters(text)
/\ text' = t
/\ tags' = CASE UndoRestore = "AllGenerated" -> [p \in 1..Len(t) |-> "g"]
[] UndoRestore = "Retained" -> Retag(Letters(text), tags, Letters(t), "g")
[] OTHER -> Retag(Letters(text), tags, Letters(t), "u")
/\ nav' = FALSE
/\ UNCHANGED snap

\* "Batch": the queue editor holds two edits and retags the net change at save. "PerEvent" is two Change steps.
\* So only "Batch" adds an action. It is the old server behavior.
QueueSave == \E t1 \in Edits(text) : \E t \in Edits(t1) :
/\ QueueEdit = "Batch"
/\ text' = t
/\ tags' = Retag(Letters(text), tags, Letters(t), "u")
/\ nav' = FALSE
/\ UNCHANGED snap

Next == Change \/ Load \/ HistoryRestoreAct \/ UndoAct \/ QueueSave

Spec == Init /\ [][Next]_vars

Saved == {<<>>} \cup [t : Seqs(Cell, MaxLen), g : Seqs(Origins, MaxLen)]
TypeOK == snap \in Saved /\ nav \in BOOLEAN /\ text \in Seqs(Cell, MaxLen) /\ tags \in Seqs(Origins, MaxLen) /\ Len(tags) = Len(text)

\* Safety: a generated letter is never tagged user. The bot thread on PR 2421 asked for this per-position form.
NoGeneratedTagged == \A p \in 1..Len(tags) : tags[p] = "u" => text[p].o = "u"

====
6 changes: 6 additions & 0 deletions docs/architecture/tla/MentionRequestOrder.HistoryOff.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SPECIFICATION Spec
CONSTANTS
HistoryGuard = "Off"
Guard = "On"
Order = "RowFirst"
INVARIANTS TypeOK NoSwitchAfterCancel NoSpinnerGap NoStaleReplacement
6 changes: 6 additions & 0 deletions docs/architecture/tla/MentionRequestOrder.NoGuard.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SPECIFICATION Spec
CONSTANTS
HistoryGuard = "On"
Guard = "Off"
Order = "RowFirst"
INVARIANTS TypeOK NoSwitchAfterCancel NoSpinnerGap NoStaleReplacement
6 changes: 6 additions & 0 deletions docs/architecture/tla/MentionRequestOrder.WaitFirst.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SPECIFICATION Spec
CONSTANTS
HistoryGuard = "On"
Guard = "On"
Order = "WaitFirst"
INVARIANTS TypeOK NoSwitchAfterCancel NoSpinnerGap NoStaleReplacement
6 changes: 6 additions & 0 deletions docs/architecture/tla/MentionRequestOrder.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SPECIFICATION Spec
CONSTANTS
HistoryGuard = "On"
Guard = "On"
Order = "RowFirst"
INVARIANTS TypeOK NoSwitchAfterCancel NoSpinnerGap NoStaleReplacement
98 changes: 98 additions & 0 deletions docs/architecture/tla/MentionRequestOrder.tla
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---- MODULE MentionRequestOrder ----
\* Models startTask, userInputContent and the loop in src/core/task/Task.ts.
\* Source: startTask starts userInputContent without a wait. Sink: handleModeSwitch.
\* Failure 1: a slash command switches the mode of a task that the user cancelled.
\* Failure 2: the loop waits for the content before it shows the api_req_started row.
\* Invariants: NoSwitchAfterCancel and NoSpinnerGap.
\* Expected before the run: Guard On and RowFirst hold. Guard Off fails in 4 states. WaitFirst fails in 2.
\* Result: the fixed config holds in 60 states. NoGuard fails in 7 states. WaitFirst fails in 5 states.
\* Replay: Task.spec.ts "slash command mode switch". One test cancels from a TaskStarted listener while parseMentions waits.
\* The other tests cancel first. All of them fail without the guard.
\* The guard and the handleModeSwitch call run in one turn, so a cancel cannot fall between them.
\* Out of scope: a cancel inside handleModeSwitch itself, cross-host writes already in progress.
\* Cost: four TLC configs take about 4 seconds. Agent time is not measured.
\* Review: 0 covered, 2 added (bot thread on Task.ts, mode switch after cancel, PR 2421), 0 gap.
\* The first version came after the thread. It is not an independent first pass.

\* Review ratchet: PR 2421 discussion_r4607106857 adds NoStaleReplacement.
\* Source: resumeTaskFromHistory starts expansion. Sink: overwriteApiConversationHistory.
\* Failure: a cancelled preparation replaces the history of a newer run.
\* Expected: the ownership guard blocks replacement. HistoryOff must fail.
\* Result: HistoryOff fails in 21 states. The fixed model finds no new failure.
\* Replay: Task.persistence.spec.ts tests cancel, dispose, abandon and generation changes.
\* All four tests fail when the callback guard is removed. No false alarm was retained.
\* The first history invariant follows the review. It is not independent.
VARIABLES aborted, content, row, loop, switchedAfterCancel,
revoked, newerHistory, preparation, staleReplacement

CONSTANTS HistoryGuard, Guard, Order \* Guard: "On" or "Off". Order: "RowFirst" or "WaitFirst".

historyVars == <<revoked, newerHistory, preparation, staleReplacement>>
vars == <<aborted, content, row, loop, switchedAfterCancel, historyVars>>

TypeOK ==
/\ revoked \in BOOLEAN /\ newerHistory \in BOOLEAN
/\ preparation \in {"pending", "done"} /\ staleReplacement \in BOOLEAN
/\ aborted \in BOOLEAN
/\ content \in {"pending", "done"}
/\ row \in {"none", "shown"}
/\ loop \in {"idle", "waiting"}
/\ switchedAfterCancel \in BOOLEAN

Init ==
/\ revoked = FALSE /\ newerHistory = FALSE
/\ preparation = "pending" /\ staleReplacement = FALSE
/\ aborted = FALSE
/\ content = "pending"
/\ row = "none"
/\ loop = "idle"
/\ switchedAfterCancel = FALSE

\* The user cancels the task at any time. A TaskStarted listener can do it.
Cancel == /\ aborted' = TRUE /\ revoked' = TRUE
/\ UNCHANGED <<content, row, loop, switchedAfterCancel, newerHistory, preparation, staleReplacement>>

\* The expansion ends. A command with a mode runs handleModeSwitch. The guard reads aborted.
Expand ==
/\ content = "pending"
/\ content' = "done"
/\ switchedAfterCancel' = (switchedAfterCancel \/ (aborted /\ Guard = "Off"))
/\ UNCHANGED <<aborted, row, loop, historyVars>>

\* The loop shows the row, then waits. The loop does not run after a cancel.
ShowRow ==
/\ ~aborted /\ row = "none"
/\ row' = "shown"
/\ UNCHANGED <<aborted, content, loop, switchedAfterCancel, historyVars>>

Wait ==
/\ ~aborted /\ loop = "idle"
/\ \/ Order = "WaitFirst"
\/ row = "shown"
/\ loop' = "waiting"
/\ UNCHANGED <<aborted, content, row, switchedAfterCancel, historyVars>>

\* Disposal or a generation change revokes the preparation without an abort flag.
Revoke == /\ revoked' = TRUE
/\ UNCHANGED <<aborted, content, row, loop, switchedAfterCancel, newerHistory, preparation, staleReplacement>>

SaveNewer == /\ revoked /\ newerHistory' = TRUE
/\ UNCHANGED <<aborted, content, row, loop, switchedAfterCancel, revoked, preparation, staleReplacement>>

\* The callback checks ownership before it calls the replacement sink.
Replace == /\ preparation = "pending" /\ preparation' = "done"
/\ staleReplacement' = (staleReplacement \/
(newerHistory /\ (HistoryGuard = "Off" \/ ~revoked)))
/\ UNCHANGED <<aborted, content, row, loop, switchedAfterCancel, revoked, newerHistory>>

Next == Cancel \/ Expand \/ ShowRow \/ Wait \/ Revoke \/ SaveNewer \/ Replace \/ UNCHANGED vars
NoStaleReplacement == ~staleReplacement
Spec == Init /\ [][Next]_vars

\* Safety 1: no mode switch runs after the cancel.
NoSwitchAfterCancel == ~switchedAfterCancel

\* Safety 2: the loop waits only after the row shows.
NoSpinnerGap == loop = "waiting" => row = "shown"

====
8 changes: 8 additions & 0 deletions docs/architecture/tla/QueueDrainRetry.Count.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SPECIFICATION Spec
CONSTANTS
Mode = "Count"
MaxMsgs = 2
MaxCalls = 2
MaxLate = 1
INVARIANTS TypeOK NoEarlySend NoStranded
CHECK_DEADLOCK FALSE
Loading
Loading