Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions docs/architecture/tla/RestoreProfileDecision.CacheAlias.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
\* RestoreProfileDecision.CacheAlias.cfg — sabotage: the refresh write aliases the
\* store-cached record (in-place mutation of the cached HistoryItem), so the store's
\* delta compare drops apiConfigName and the stale pin survives on disk.
\* RefreshPersists must be the violated invariant.
SPECIFICATION Spec
CHECK_DEADLOCK FALSE
CONSTANTS
NoActive = "none"
Valid <- ValidAliased
INVARIANTS TypeOK FollowActive PinNamesEffect LegacyFallback RefreshPersists WriteFailureRestores NoWritePreservesPin ChildPinStable RefreshNamesEffectiveActive
10 changes: 10 additions & 0 deletions docs/architecture/tla/RestoreProfileDecision.ChildRestore.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
\* RestoreProfileDecision.ChildRestore.cfg — sabotage: the delegated-child origin is
\* ignored, so a child's restore is decided like a user restore and the child is
\* refreshed to the global active profile. ChildPinStable must be the violated
\* invariant (PR 2649 human review, restore-provider-profile.ts:66).
SPECIFICATION Spec
CHECK_DEADLOCK FALSE
CONSTANTS
NoActive = "none"
Decide <- DecideIgnoresOrigin
INVARIANTS TypeOK FollowActive PinNamesEffect LegacyFallback RefreshPersists WriteFailureRestores NoWritePreservesPin ChildPinStable RefreshNamesEffectiveActive
10 changes: 10 additions & 0 deletions docs/architecture/tla/RestoreProfileDecision.NoQueue.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
\* RestoreProfileDecision.NoQueue.cfg — sabotage: the decision runs outside the
\* provider-profile mutation queue, so a user switch can interleave between the
\* decision's snapshot and the write. RefreshNamesEffectiveActive must be the
\* violated invariant (PR 2649 human review, ClineProvider.ts:1620).
SPECIFICATION Spec
CHECK_DEADLOCK FALSE
CONSTANTS
NoActive = "none"
Valid <- ValidUnqueued
INVARIANTS TypeOK FollowActive PinNamesEffect LegacyFallback RefreshPersists WriteFailureRestores NoWritePreservesPin ChildPinStable RefreshNamesEffectiveActive
7 changes: 7 additions & 0 deletions docs/architecture/tla/RestoreProfileDecision.Sabotage.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
\* Sabotage check. The stale-pin guard is removed. FollowActive must fail.
SPECIFICATION Spec
CHECK_DEADLOCK FALSE
CONSTANTS
NoActive = "none"
Decide <- DecideAlwaysPin
INVARIANTS TypeOK FollowActive PinNamesEffect LegacyFallback RefreshPersists WriteFailureRestores NoWritePreservesPin ChildPinStable RefreshNamesEffectiveActive
10 changes: 10 additions & 0 deletions docs/architecture/tla/RestoreProfileDecision.StalePin.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
\* RestoreProfileDecision.StalePin.cfg — sabotage: a full-copy write persists the
\* caller's history-copy pin even when no write landed, so a stale copy overwrites
\* the persisted record's newer pin. NoWritePreservesPin must be the violated
\* invariant.
SPECIFICATION Spec
CHECK_DEADLOCK FALSE
CONSTANTS
NoActive = "none"
PersistedPin <- PersistedPinFullCopy
INVARIANTS TypeOK FollowActive PinNamesEffect LegacyFallback RefreshPersists WriteFailureRestores NoWritePreservesPin ChildPinStable RefreshNamesEffectiveActive
9 changes: 9 additions & 0 deletions docs/architecture/tla/RestoreProfileDecision.WriteAbort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
\* RestoreProfileDecision.WriteAbort.cfg — sabotage: Proceed reads the write outcome,
\* so the restore aborts when the refresh pin write rejects (the un-caught await
\* before the Task constructor). WriteFailureRestores must be the violated invariant.
SPECIFICATION Spec
CHECK_DEADLOCK FALSE
CONSTANTS
NoActive = "none"
Proceed <- ProceedAbort
INVARIANTS TypeOK FollowActive PinNamesEffect LegacyFallback RefreshPersists WriteFailureRestores NoWritePreservesPin ChildPinStable RefreshNamesEffectiveActive
6 changes: 6 additions & 0 deletions docs/architecture/tla/RestoreProfileDecision.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
\* RestoreProfileDecision.cfg — the decision must satisfy every invariant.
SPECIFICATION Spec
CHECK_DEADLOCK FALSE
CONSTANTS
NoActive = "none"
INVARIANTS TypeOK FollowActive PinNamesEffect LegacyFallback RefreshPersists WriteFailureRestores NoWritePreservesPin ChildPinStable RefreshNamesEffectiveActive
209 changes: 209 additions & 0 deletions docs/architecture/tla/RestoreProfileDecision.tla
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
---- MODULE RestoreProfileDecision ----
\* Models src/core/webview/restore-provider-profile.ts resolveRestoreProfileActivation.
\* Source: HistoryItem.apiConfigName (the pin) and the currentApiConfigName global,
\* resolved against the profile list by ClineProvider.createTaskWithHistoryItemUnlocked.
\* Sink: the decision record (reactivateProfileName, refreshedApiConfigName) consumed by
\* the same method: reactivate calls activateProviderProfile, refresh rewrites the pin
\* through the repinTaskProfilePin reducer (persisted to the task file) and hands the
\* record to the Task constructor.
\* Failure: restore re-activates a stale pinned profile, so the resumed task and the
\* task header keep the old provider's context window (issue #970).
\* Invariants (first version written before the fix):
\* FollowActive. With an existing pin and a resolvable active profile different from
\* it, a user restore must not reactivate the pinned profile.
\* PinNamesEffect. A refresh names exactly the active profile, and a reactivation
\* never also refreshes.
\* LegacyFallback. With no resolvable active profile, an existing pin is reactivated.
\* Added in the PR 2649 review ratchet (CodeRabbit round on d58eaba2c):
\* RefreshPersists. A refresh whose pin write succeeds leaves the persisted pin
\* naming the active profile. The caller must hand the store a record distinct from
\* the cached one (input rule in Valid); mutating the cached record in place makes
\* the store's delta compare drop apiConfigName, so the stale pin survives on disk
\* (CR thread on ClineProvider.ts:1651).
\* WriteFailureRestores. The restore reaches Task construction with the reachability
\* it would have had if the write had succeeded: Proceed must not read the write
\* outcome, because a failed refresh write is caught (CR thread on
\* ClineProvider.ts:1652).
\* Added in the PR 2649 human review round (edelauna, 2026-11-30):
\* ChildPinStable. A delegated child's restore never refreshes and reactivates only
\* the child's own task-local pin (thread on restore-provider-profile.ts:66;
\* delegation never changes the global profile, so the global active profile is not
\* the user's choice for the child).
\* RefreshNamesEffectiveActive. A refresh names the profile active at write time.
\* The input rule switchActive = active in Valid models the provider-profile
\* mutation queue: a user switch cannot interleave between the decision's snapshot
\* and the write (thread on ClineProvider.ts:1620; RestoreProfileDecision.NoQueue.cfg
\* sabotages the rule away).
\* NoWritePreservesPin. When no write lands, the persisted record keeps its own
\* current pin (input.savedPin), not the pin on the caller's possibly stale history
\* copy (input.pin) - the old-history-copy ownership (threads on ClineProvider.ts
\* :1657/1659; a full-copy write would revert newer fields). StalePin.cfg now
\* sabotages a full-copy write.
\* Proceed is inp.withinTimeout, no longer a constant: the queue timeout is the one
\* modeled stopper, so restored varies across reachable states and
\* WriteFailureRestores has content (thread on RestoreProfileDecision.tla:103).
\* Run each config on RestoreProfileDecisionMC.tla. The MC module holds the sabotage.
\* Expected: Spec.cfg passes every invariant; Sabotage.cfg fails FollowActive;
\* CacheAlias.cfg fails RefreshPersists; WriteAbort.cfg fails WriteFailureRestores;
\* StalePin.cfg fails NoWritePreservesPin; ChildRestore.cfg fails ChildPinStable;
\* NoQueue.cfg fails RefreshNamesEffectiveActive.
\* Result: Spec.cfg checks and holds all nine invariants; each sabotage cfg violates
\* the invariant it names (state counts in the check log, see the commit message).
\* Not modeled: the profile list (pinnedProfileExists is an oracle the caller computes),
\* the activateProviderProfile settings side effects, mode restoration, and the CLI
\* runtime skip flag (checked before the decision runs). The settings capture after the
\* activation is modeled only through the serialization rule in Valid.
\* Cost: TLC finishes in under 1 second per config.
\* Review (PR 2649, round on d58eaba2c): 1 covered (LegacyFallback - the missing-profile
\* proof case of the Regression Evidence check), 2 added (RefreshPersists + the
\* distinct-record input rule, CR thread ClineProvider.ts:1651; WriteFailureRestores,
\* CR thread ClineProvider.ts:1652), 1 gap (CR thread
\* ClineProvider.restore-follows-active-profile.spec.ts:123 - test-harness settings;
\* the model has no test doubles, the harness assertions pin it directly).
\* Review (PR 2649, round on bd9eb7d82): 1 covered (RefreshPersists +
\* WriteFailureRestores were already modeled; the defect was the passing cfg
\* checking only four of the six invariants - all six are checked now, CR thread
\* RestoreProfileDecision.cfg:6), 3 gap (mutation-diff survivors on bd9eb7d82:
\* the re-pin log text and the deleted-profile else-if guard are log/branch-level,
\* pinned by unit tests in ClineProvider.restore-follows-active-profile.spec.ts;
\* the defensive profile?.name chain is an equivalent mutant documented beside
\* the condition in ClineProvider.ts).
\* Review (PR 2649, round on 6ca11ea04): 1 gap (CR thread on
\* ClineProvider.restore-follows-active-profile.spec.ts:403 - the fall-through
\* restore must construct the task from the exact unresolved-settings snapshot;
\* expressing it would mean modeling the caller's profile.apiProvider activation
\* guard, which sits outside the decision record like the activation side
\* effects above; pinned by the strengthened exact-config unit assertion).
\* Review (PR 2649, round on ea2d69b75): 1 added (NoWritePreservesPin + the sink
\* fallback it pins: the no-write path now persists the selected input's pin,
\* factored as PersistedPin so StalePin.cfg sabotages the stale form; CR thread
\* RestoreProfileDecision.tla:109), 0 covered, 0 gap.
\* Review (PR 2649, human round 1, edelauna 2026-11-30): 3 added (ChildPinStable +
\* the origin input, thread restore-provider-profile.ts:66; RefreshNamesEffectiveActive
\* + the switchActive input and the queue rule in Valid, thread ClineProvider.ts:1620;
\* the savedPin input making NoWritePreservesPin pin the persisted record's own pin
\* instead of the caller's copy, threads ClineProvider.ts:1657/1659), 1 changed
\* (Proceed is inp.withinTimeout instead of TRUE so WriteFailureRestores is a real
\* constraint; WriteAbort.cfg still fails it, thread RestoreProfileDecision.tla:103),
\* 1 covered (FollowActive and LegacyFallback were already the stale-pin and
\* missing-profile behavior the round's other threads restate).

CONSTANT NoActive \* sentinel: no resolvable active profile at restore time

VARIABLES input, decision, storedPin, restored

vars == <<input, decision, storedPin, restored>>

NoProfile == "absent" \* decision marker: reactivate nothing
Keep == "keep" \* decision marker: preserve the stored pin

Input ==
[pin: {"p1", "p2", "absent"}, active: {"p1", "p2", NoActive},
pinExists: BOOLEAN, pinWriteOk: BOOLEAN, distinctRecord: BOOLEAN,
origin: {"user", "child"}, savedPin: {"p1", "p2", "absent"},
switchActive: {"p1", "p2", NoActive}, withinTimeout: BOOLEAN]

\* Mirrors resolveRestoreProfileActivation rule for rule, origin included: a
\* delegated child's own task-local pin is authoritative and never refreshed.
Decide(inp) ==
IF inp.pin = "absent" THEN [r |-> NoProfile, f |-> Keep]
ELSE IF inp.origin = "child" THEN
IF inp.pinExists THEN [r |-> inp.pin, f |-> Keep]
ELSE [r |-> NoProfile, f |-> Keep]
ELSE IF ~inp.pinExists THEN
IF inp.active # NoActive THEN [r |-> NoProfile, f |-> inp.active]
ELSE [r |-> NoProfile, f |-> Keep]
ELSE IF inp.active = NoActive THEN [r |-> inp.pin, f |-> Keep]
ELSE IF inp.active = inp.pin THEN [r |-> inp.pin, f |-> Keep]
ELSE [r |-> NoProfile, f |-> inp.active]

\* A refresh-shaped input is one whose decision carries a new pin.
Refreshes(inp) == Decide(inp).f # Keep

\* The sink: the pin persisted after one restore. A refresh whose write succeeds on
\* a record distinct from the store-cached one patches the pin on the store's current
\* record; every other path writes nothing, so the persisted record keeps its own
\* current pin - never the pin on the caller's possibly stale history copy.
PersistedPin(dec, inp, prev) ==
IF dec.f # Keep /\ inp.pinWriteOk /\ inp.distinctRecord THEN dec.f ELSE inp.savedPin

\* Caller rules: no existing flag for an absent pin; a refresh write is built from a
\* record distinct from the store-cached one (a copy), never the cached record; and
\* the mutation queue serializes the restore, so no user switch can interleave
\* between the decision's profile snapshot and the write.
Valid(inp) ==
/\ ((inp.pin = "absent") => (~inp.pinExists))
/\ (Refreshes(inp) => inp.distinctRecord)
/\ inp.switchActive = inp.active

\* The queue timeout is the one modeled stopper: with runToCompletion a failed
\* refresh write is caught and the restore still reaches Task construction, while a
\* timeout rejects the caller before it. Not a constant, so restored varies.
Proceed(dec, inp) == inp.withinTimeout

Init ==
/\ input = [pin |-> "absent", active |-> NoActive, pinExists |-> FALSE,
pinWriteOk |-> TRUE, distinctRecord |-> TRUE,
origin |-> "user", savedPin |-> "absent",
switchActive |-> NoActive, withinTimeout |-> TRUE]
/\ decision = Decide(input)
/\ storedPin = input.savedPin
/\ restored = Proceed(Decide(input), input)

Next ==
\E inp \in Input :
/\ Valid(inp)
/\ input' = inp
/\ decision' = Decide(inp)
\* An aliased write (the cached record handed back to the store) makes the
\* delta compare omit apiConfigName, so the stale pin survives on disk.
/\ storedPin' = PersistedPin(decision', inp, storedPin)
/\ restored' = Proceed(decision', inp)

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

TypeOK ==
/\ decision.r \in {"p1", "p2", "absent"}
/\ decision.f \in {"p1", "p2", "keep"}
/\ storedPin \in {"p1", "p2", "absent"}
/\ restored \in BOOLEAN

FollowActive ==
(input.pin # "absent" /\ input.origin = "user" /\ input.pinExists
/\ input.active # NoActive /\ input.active # input.pin) => decision.r = NoProfile

PinNamesEffect ==
/\ (decision.f # Keep) => decision.f = input.active
/\ (decision.r # NoProfile) => decision.f = Keep

LegacyFallback ==
(input.pin # "absent" /\ input.origin = "user" /\ input.pinExists
/\ input.active = NoActive) => decision.r = input.pin

RefreshPersists ==
(decision.f # Keep /\ input.pinWriteOk) => storedPin = decision.f

\* No write landed (no refresh, a failed refresh write, or an aliased record the
\* store's delta compare drops): the persisted record keeps its own current pin, so
\* the caller's stale history copy cannot overwrite a newer one.
NoWritePreservesPin ==
(~(decision.f # Keep /\ input.pinWriteOk /\ input.distinctRecord)) => storedPin = input.savedPin

\* A delegated child is never refreshed to the global profile and reactivates only
\* its own task-local pin.
ChildPinStable ==
(input.origin = "child") =>
/\ decision.f = Keep
/\ (decision.r # NoProfile) => decision.r = input.pin

\* The queue serialized the restore, so a refresh names the profile active at write
\* time - the same profile the decision snapshotted.
RefreshNamesEffectiveActive ==
(decision.f # Keep) => decision.f = input.switchActive

\* The restore reaches Task construction with the reachability it would have had if
\* the refresh write had succeeded: Proceed must not read the write outcome.
WriteFailureRestores ==
restored = Proceed(decision, [input EXCEPT !.pinWriteOk = TRUE])

====
50 changes: 50 additions & 0 deletions docs/architecture/tla/RestoreProfileDecisionMC.tla
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---- MODULE RestoreProfileDecisionMC ----
\* Model-checking settings for RestoreProfileDecision.
EXTENDS RestoreProfileDecision

\* Sabotage: the stale-pin guard is removed. The pin is always re-activated when it
\* exists (the pre-fix behavior) and the pin is never refreshed. FollowActive must fail.
DecideAlwaysPin(inp) ==
IF inp.pin = "absent" THEN [r |-> NoProfile, f |-> Keep]
ELSE IF inp.pinExists THEN [r |-> inp.pin, f |-> Keep]
ELSE [r |-> NoProfile, f |-> Keep]

\* Sabotage: the delegated-child origin is ignored, so a child's restore is decided
\* like a user restore and the child is refreshed to the global active profile.
\* ChildPinStable must fail. The user rules are inlined (a cfg override of Decide
\* substitutes every occurrence, including inside this operator's own body).
DecideIgnoresOrigin(inp) ==
IF inp.pin = "absent" THEN [r |-> NoProfile, f |-> Keep]
ELSE IF ~inp.pinExists THEN
IF inp.active # NoActive THEN [r |-> NoProfile, f |-> inp.active]
ELSE [r |-> NoProfile, f |-> Keep]
ELSE IF inp.active = NoActive THEN [r |-> inp.pin, f |-> Keep]
ELSE IF inp.active = inp.pin THEN [r |-> inp.pin, f |-> Keep]
ELSE [r |-> NoProfile, f |-> inp.active]

\* Sabotage: the refresh write aliases the store-cached record (the restore mutates
\* the cached item in place before the write), so the store's delta compare
\* drops apiConfigName and the stale pin survives on disk. Only the distinct-record
\* rule is dropped, so RefreshPersists is the violated invariant.
ValidAliased(inp) ==
/\ ((inp.pin = "absent") => (~inp.pinExists))
/\ inp.switchActive = inp.active

\* Sabotage: the decision runs outside the provider-profile mutation queue, so a user
\* switch can interleave between the decision's snapshot and the write.
\* RefreshNamesEffectiveActive must fail.
ValidUnqueued(inp) ==
/\ ((inp.pin = "absent") => (~inp.pinExists))
/\ (Refreshes(inp) => inp.distinctRecord)

\* Sabotage: the restore aborts when the refresh write rejects (Proceed reads the
\* write outcome). WriteFailureRestores must fail.
ProceedAbort(dec, inp) == (dec.f = Keep) \/ inp.pinWriteOk

\* Sabotage: a full-copy write persists the caller's history-copy pin even when no
\* write landed, so a stale copy overwrites the persisted record's newer pin.
\* NoWritePreservesPin must fail.
PersistedPinFullCopy(dec, inp, prev) ==
IF dec.f # Keep /\ inp.pinWriteOk THEN dec.f ELSE inp.pin

====
Loading
Loading