@@ -245,6 +245,30 @@ type GetStateOptions = {
245245 includeTaskHistory ?: boolean
246246}
247247
248+ /**
249+ * How a host-minted view-state claim reached the webview that has to carry it back.
250+ *
251+ * - `delivered`: the launched webview accepted the post.
252+ * - `queued`: the durable entry carries the token and the post is held for the next
253+ * `webviewDidLaunch`, when the renderer has a listener that can receive it.
254+ * - `dropped`: the provider was disposed, so nothing will post it again.
255+ * - `none`: this operation minted no claim.
256+ */
257+ export type ViewStateClaimDelivery = "delivered" | "queued" | "dropped" | "none"
258+
259+ /**
260+ * Result of a durable view-state write: whether anything was stored, and what happened to
261+ * the claim the write minted. A save that stored an entry whose claim never reached the
262+ * webview is a partial success, and `claim` is how the caller can see it.
263+ */
264+ export type ViewStatePersistenceResult = { persisted : boolean ; claim : ViewStateClaimDelivery }
265+
266+ /**
267+ * Result of registering a stable view id: whether the id was adopted, and what happened to
268+ * the claim this registration minted or re-affirmed.
269+ */
270+ export type ViewStateRegistrationResult = { adopted : boolean ; claim : ViewStateClaimDelivery }
271+
248272export class ClineProvider
249273 extends EventEmitter < TaskProviderEvents >
250274 implements vscode . WebviewViewProvider , TelemetryPropertiesProvider , TaskProviderLike
@@ -717,13 +741,16 @@ export class ClineProvider
717741 }
718742
719743 /** Persist the non-secret selections through the serialized write queue. */
720- private async savePersistedViewState ( values : Partial < PersistedViewState > ) : Promise < void > {
744+ private async savePersistedViewState ( values : Partial < PersistedViewState > ) : Promise < ViewStatePersistenceResult > {
721745 // Capture the id at change time: a write belongs to the view that was active
722746 // when the change was made, even if a newer id is registered while it is queued.
723747 const viewStateId = this . viewStateId
724748 const generation = this . viewStateGeneration
725749 // Set by the queued write when it had to mint a claim, delivered after it lands.
726750 let mintedClaimToken : string | undefined
751+ // Set once the queued write reaches the storage call: a write the queue dropped before
752+ // its turn must not be reported as persisted.
753+ let storedEntry = false
727754 const write = ClineProvider . persistedViewStateWriteQueue . then ( async ( ) => {
728755 if ( ! this . isViewStateGenerationLive ( generation ) ) {
729756 // The queue advanced after this view was disposed: the entry belongs to a view
@@ -776,6 +803,7 @@ export class ClineProvider
776803 }
777804
778805 await this . contextProxy . setValue ( "viewStates" , this . prunePersistedViewStates ( states ) )
806+ storedEntry = true
779807
780808 if ( ! this . isViewStateGenerationLive ( generation ) ) {
781809 // The view went away while the write was in flight: the selections are durable,
@@ -794,12 +822,26 @@ export class ClineProvider
794822 ClineProvider . persistedViewStateWriteQueue = write . catch ( ( ) => { } )
795823 await write
796824
825+ if ( ! storedEntry ) {
826+ // The queue dropped this write before it reached storage: nothing was persisted, so
827+ // nothing about a claim may be reported as settled either.
828+ return { persisted : false , claim : "none" }
829+ }
830+
797831 // Delivered only once the durable write landed: a webview must never hold a claim the
798832 // host did not store, or the next launch would present a token the entry cannot match.
799833 // A provider disposed in the meantime has no webview left to deliver it to.
800834 if ( mintedClaimToken && this . isViewStateGenerationLive ( generation ) ) {
801- await this . postViewStateClaimToWebview ( viewStateId , mintedClaimToken )
835+ return {
836+ persisted : true ,
837+ claim : await this . postViewStateClaimToWebview ( viewStateId , mintedClaimToken ) ,
838+ }
802839 }
840+
841+ // The entry is durable and carries a token this webview has never been handed. Report
842+ // that explicitly: a save that silently resolves while its claim is undelivered is the
843+ // persistence-integrity gap this result exists to make visible.
844+ return { persisted : true , claim : mintedClaimToken ? "dropped" : "none" }
803845 }
804846
805847 /**
@@ -862,18 +904,34 @@ export class ClineProvider
862904 *
863905 * Awaited rather than fired and forgotten: the entry already carries this claim once the write
864906 * lands, so a view that never receives it would be missing an identity the host believes it
865- * holds. Every delivery failure is reported - no webview attached, the post rejected, or the
866- * webview resolving the post as not delivered - and the exact claim is queued for the next
867- * attach instead of dropped. The post itself stays the single place that records the
868- * webview-out diagnostic.
907+ * holds. Posts are gated on `isViewLaunched`: the renderer attaches its message listener during
908+ * `webviewDidLaunch`, and the Webview API allows an earlier message to be lost even when
909+ * postMessage resolves true. Every non-delivery is reported - renderer not launched, the post
910+ * rejected, or the webview resolving the post as not delivered - and the exact claim is queued
911+ * for the next `webviewDidLaunch` instead of dropped. The post itself stays the single place
912+ * that records the webview-out diagnostic.
869913 */
870- private async postViewStateClaimToWebview ( viewStateId : string , claimToken : string ) : Promise < boolean > {
871- if ( ! this . view || this . _disposed ) {
914+ private async postViewStateClaimToWebview (
915+ viewStateId : string ,
916+ claimToken : string ,
917+ ) : Promise < ViewStateClaimDelivery > {
918+ if ( this . _disposed ) {
919+ this . log (
920+ `[viewStateClaim] Claim for ${ viewStateId } dropped for view ${ this . viewId } : the provider is disposed.` ,
921+ )
922+ return "dropped"
923+ }
924+
925+ if ( ! this . view || ! this . isViewLaunched ) {
926+ // Held rather than posted. The renderer only attaches its message listener once the
927+ // webview launches, and the Webview API permits a message posted before that to be lost
928+ // while postMessage still resolves true - which would delete a claim the webview never
929+ // saw. Queuing keeps the exact token for the webviewDidLaunch flush.
872930 this . pendingViewStateClaims . set ( viewStateId , claimToken )
873931 this . log (
874- `[viewStateClaim] Claim for ${ viewStateId } not delivered to view ${ this . viewId } : no webview attached .` ,
932+ `[viewStateClaim] Claim for ${ viewStateId } not delivered to view ${ this . viewId } : the webview has not launched yet .` ,
875933 )
876- return false
934+ return "queued"
877935 }
878936
879937 let delivered : boolean
@@ -887,7 +945,7 @@ export class ClineProvider
887945 const reason = error instanceof Error ? error . message : String ( error )
888946 this . pendingViewStateClaims . set ( viewStateId , claimToken )
889947 this . log ( `[viewStateClaim] Delivery for ${ viewStateId } to view ${ this . viewId } failed: ${ reason } ` )
890- return false
948+ return "queued"
891949 }
892950
893951 if ( ! delivered ) {
@@ -897,21 +955,22 @@ export class ClineProvider
897955 this . log (
898956 `[viewStateClaim] Claim for ${ viewStateId } not delivered to view ${ this . viewId } : the webview rejected the message.` ,
899957 )
900- return false
958+ return "queued"
901959 }
902960
903961 this . pendingViewStateClaims . delete ( viewStateId )
904- return true
962+ return "delivered"
905963 }
906964
907965 /**
908- * Re-delivers the claims this view minted that no webview accepted yet, called when a webview
909- * attaches. Each queued entry keeps the exact token the durable entry stores, so a webview that
910- * missed a delivery still gets the identity it has to present on the next launch; an entry that
911- * fails again is re-queued for the next attach.
966+ * Flushes the claims this view minted that no webview accepted yet. Called from the
967+ * `webviewDidLaunch` handler, once the renderer has attached its message listener, and a no-op
968+ * until then: posting before that can be lost. Each queued entry keeps the exact token the
969+ * durable entry stores, so a webview that missed a delivery still gets the identity it has to
970+ * present on the next launch; an entry that fails again is re-queued for the next launch.
912971 */
913- private async deliverPendingViewStateClaims ( ) : Promise < void > {
914- if ( this . _disposed || ! this . view || this . pendingViewStateClaims . size === 0 ) {
972+ public async deliverPendingViewStateClaims ( ) : Promise < void > {
973+ if ( this . _disposed || ! this . view || ! this . isViewLaunched || this . pendingViewStateClaims . size === 0 ) {
915974 return
916975 }
917976
@@ -996,7 +1055,10 @@ export class ClineProvider
9961055 }
9971056
9981057 /** Register the stable view id and load its persisted selections; a claim must match. */
999- public async setViewStateId ( viewStateId : string | undefined , presentedClaim ?: string ) : Promise < void > {
1058+ public async setViewStateId (
1059+ viewStateId : string | undefined ,
1060+ presentedClaim ?: string ,
1061+ ) : Promise < ViewStateRegistrationResult > {
10001062 // The webview generates this id on purpose - it has to survive a webview reload,
10011063 // which recreates the view on both sides - so the host cannot mint it instead.
10021064 // What the host owns is which view may hold a given persisted key, so everything
@@ -1005,7 +1067,7 @@ export class ClineProvider
10051067 // A crafted postMessage can carry any JSON value here even though the message
10061068 // type declares a string; the persisted key must stay a string.
10071069 this . log ( `[setViewStateId] Ignoring a non-string viewStateId for view ${ this . viewId } .` )
1008- return
1070+ return { adopted : false , claim : "none" }
10091071 }
10101072
10111073 const normalizedViewStateId = viewStateId . trim ( ) . replace ( / [ ^ A - Z a - z 0 - 9 _ - ] / g, "_" )
@@ -1025,7 +1087,7 @@ export class ClineProvider
10251087 // entry under a name that looks like an inherited member.
10261088 normalizedViewStateId === "__proto__"
10271089 ) {
1028- return
1090+ return { adopted : false , claim : "none" }
10291091 }
10301092
10311093 // Bound the key: the webview generates a UUID, so a much longer value is not a
@@ -1034,7 +1096,7 @@ export class ClineProvider
10341096 this . log (
10351097 `[setViewStateId] Ignoring an oversized viewStateId (${ normalizedViewStateId . length } chars) for view ${ this . viewId } .` ,
10361098 )
1037- return
1099+ return { adopted : false , claim : "none" }
10381100 }
10391101
10401102 const claimant = ClineProvider . getAllInstances ( ) . find (
@@ -1048,14 +1110,19 @@ export class ClineProvider
10481110 this . log (
10491111 `[setViewStateId] Refusing viewStateId ${ normalizedViewStateId } for view ${ this . viewId } : already claimed by live view ${ claimant . viewId } .` ,
10501112 )
1051- return
1113+ return { adopted : false , claim : "none" }
10521114 }
10531115
10541116 // Claim binding, checked before any re-key or load so a refused registration can
10551117 // neither read nor mutate a persisted entry. Read fresh from storage rather than
10561118 // through the ContextProxy cache: a security check must not trust a stale copy.
10571119 const persistedEntry = this . getPersistedViewStates ( { fresh : true } ) [ normalizedViewStateId ]
10581120
1121+ // What became of the claim this registration mints or re-affirms. A refused registration
1122+ // never reaches a claim, and an adopted one whose claim is still queued is not the same
1123+ // outcome as one the webview received, so both are reported to the caller.
1124+ let claimDelivery : ViewStateClaimDelivery = "none"
1125+
10591126 if ( persistedEntry ) {
10601127 const storedClaim = persistedEntry . claimToken
10611128
@@ -1073,10 +1140,10 @@ export class ClineProvider
10731140 this . log (
10741141 `[setViewStateId] Refusing viewStateId ${ normalizedViewStateId } for view ${ this . viewId } : bound concurrently by another registration.` ,
10751142 )
1076- return
1143+ return { adopted : false , claim : "none" }
10771144 }
10781145
1079- await this . postViewStateClaimToWebview ( normalizedViewStateId , reboundClaim )
1146+ claimDelivery = await this . postViewStateClaimToWebview ( normalizedViewStateId , reboundClaim )
10801147 this . log (
10811148 `[setViewStateId] Re-bound claim-less viewStates entry ${ normalizedViewStateId } for view ${ this . viewId } : unbound selections were not loaded.` ,
10821149 )
@@ -1087,7 +1154,7 @@ export class ClineProvider
10871154 this . log (
10881155 `[setViewStateId] Refusing viewStateId ${ normalizedViewStateId } for view ${ this . viewId } : claim missing or mismatched.` ,
10891156 )
1090- return
1157+ return { adopted : false , claim : "none" }
10911158 }
10921159 }
10931160
@@ -1109,7 +1176,11 @@ export class ClineProvider
11091176 // A registration whose view was disposed mid-flight must not deliver a claim to
11101177 // a webview that is no longer there.
11111178 if ( adoptedClaim && this . isViewStateGenerationLive ( generation ) ) {
1112- await this . postViewStateClaimToWebview ( this . viewStateId , adoptedClaim )
1179+ claimDelivery = await this . postViewStateClaimToWebview ( this . viewStateId , adoptedClaim )
1180+ } else if ( adoptedClaim ) {
1181+ // The view was disposed mid-registration: the binding is durable but this provider
1182+ // will not post the claim again.
1183+ claimDelivery = "dropped"
11131184 }
11141185
11151186 // Registration is what loads this view's own persisted selections.
@@ -1122,6 +1193,8 @@ export class ClineProvider
11221193 this . viewStateId = previousViewStateId
11231194 throw error
11241195 }
1196+
1197+ return { adopted : true , claim : claimDelivery }
11251198 }
11261199
11271200 /**
@@ -1729,17 +1802,10 @@ export class ClineProvider
17291802 this . webviewDisposables ,
17301803 )
17311804
1732- // A claim minted while no webview was attached, or while its post was rejected, is delivered
1733- // now that this view exists: the durable entry already carries that token, and the webview
1734- // cannot present an identity the host never handed it. A failed retry must not fail
1735- // initialization, so it is contained here.
1736- try {
1737- await this . deliverPendingViewStateClaims ( )
1738- } catch ( error ) {
1739- this . log (
1740- `[viewStateClaim] Retrying pending claims for view ${ this . viewId } failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
1741- )
1742- }
1805+ // Queued claims are deliberately not posted here. The renderer attaches its message listener
1806+ // only when it launches, and the Webview API allows a message posted before that to be lost
1807+ // while postMessage still resolves true. The webviewDidLaunch handler flushes the queue once
1808+ // a listener exists to receive it.
17431809
17441810 if ( inTabMode ) {
17451811 setPanel ( webviewView , "tab" )
@@ -5980,10 +6046,12 @@ export class ClineProvider
59806046 public async saveViewState < K extends keyof PersistedViewStateValues > (
59816047 key : K ,
59826048 value : PersistedViewStateValues [ K ] | undefined ,
5983- ) : Promise < void > {
5984- await this . _saveViewLocalStateFromMutation ( { [ key ] : value } as ViewLocalStateValues )
6049+ ) : Promise < ViewStatePersistenceResult > {
6050+ const persistence = await this . _saveViewLocalStateFromMutation ( { [ key ] : value } as ViewLocalStateValues )
59856051
59866052 this . log ( `[saveViewState] Saved ${ String ( key ) } for viewId ${ this . viewId } ` )
6053+
6054+ return persistence
59876055 }
59886056
59896057 /**
@@ -5993,9 +6061,11 @@ export class ClineProvider
59936061 */
59946062 private async _saveViewLocalStateFromMutation (
59956063 values : Partial < RooCodeSettings > & Partial < ExtensionState > ,
5996- ) : Promise < void > {
5997- await this . _persistViewLocalStateFromMutation ( values )
6064+ ) : Promise < ViewStatePersistenceResult > {
6065+ const persistence = await this . _persistViewLocalStateFromMutation ( values )
59986066 this . _updateViewLocalStateFromMutation ( values )
6067+
6068+ return persistence
59996069 }
60006070
60016071 /**
@@ -6061,7 +6131,7 @@ export class ClineProvider
60616131 */
60626132 private async _persistViewLocalStateFromMutation (
60636133 values : Partial < RooCodeSettings > & Partial < ExtensionState > ,
6064- ) : Promise < void > {
6134+ ) : Promise < ViewStatePersistenceResult > {
60656135 const persistedValues : Partial < PersistedViewState > = { }
60666136
60676137 if ( "mode" in values ) {
@@ -6073,8 +6143,11 @@ export class ClineProvider
60736143 }
60746144
60756145 if ( "mode" in persistedValues || "currentApiConfigName" in persistedValues ) {
6076- await this . savePersistedViewState ( persistedValues )
6146+ return this . savePersistedViewState ( persistedValues )
60776147 }
6148+
6149+ // Nothing in this mutation belongs to the durable record, so there was nothing to write.
6150+ return { persisted : false , claim : "none" }
60786151 }
60796152
60806153 /** Load non-secret persisted selections; in-flight buffer edits are reapplied on top. */
0 commit comments