Repository navigation
feat(webview): per-view ack, history and profile restore writes (N5, split 5/5 of #1546) #2237
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
f79969d
227c568
6c05508
5acad5d
8d4a5d9
cd9e19e
92569b6
87e5b67
1eae1ef
12add60
1bfdc56
47129f8
0147ea5
1d12c48
5cae4e1
fa7ba94
4612118
5bac15e
d8102b7
657b9a8
d064c0c
787dece
c23af0d
495ee1b
c6347fe
1d9bb56
edcfbc8
86d64c2
592b286
5e7dff4
672bbf0
ec3f1b7
4b1cfd2
f4ccc2c
e085904
90b61fb
826de44
4f11516
f5708aa
868be6e
43dcd51
07d099f
7c00500
4b7db60
85dcf94
f60d8a4
cbb0079
c8aa31d
229e8fe
994f0e8
bd73686
3c159d1
084356f
82a2b1e
172bf34
3721319
d59c5ed
883ff11
be51fee
afaf068
1f8d6f4
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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,14 +67,23 @@ const globalSettingsExportSchema = globalSettingsSchema.omit({ | |
| taskHistory: true, | ||
| listApiConfigMeta: true, | ||
| currentApiConfigName: true, | ||
| // Per-view selection state is machine-local: it keeps flowing through the | ||
| // normal runtime and pruning paths but must not transfer between settings. | ||
| viewStates: true, | ||
| }) | ||
|
|
||
| export class ContextProxy { | ||
| private readonly originalContext: vscode.ExtensionContext | ||
|
|
||
| private stateCache: GlobalState | ||
| // Counted for either store, so a rollback can tell its own write from a newer one of the same value. | ||
| private stateWriteGenerations = new Map<GlobalStateKey | SecretStateKey, number>() | ||
|
|
||
| private secretCache: SecretState | ||
| private _isInitialized = false | ||
| // Raised for the whole duration of a reset and cleared in a finally, so a reset that fails | ||
| // mid-way cannot leave the store refusing every later secret write; see resetAllState. | ||
| private resetting = false | ||
|
|
||
| constructor(context: vscode.ExtensionContext) { | ||
| this.originalContext = context | ||
|
|
@@ -413,6 +422,8 @@ export class ContextProxy { | |
| // writes that are in flight. An update that throws at once is turned into a rejection, so it is handled the same. | ||
| const owner = {} | ||
| this.stateWriteOwner.set(key, owner) | ||
| const generation = (this.stateWriteGenerations.get(key) ?? 0) + 1 | ||
| this.stateWriteGenerations.set(key, generation) | ||
| this.stateCache[key] = value | ||
| const release = () => { | ||
| if (this.stateWriteOwner.get(key) === owner) this.stateWriteOwner.delete(key) | ||
|
|
@@ -429,6 +440,11 @@ export class ContextProxy { | |
| }) | ||
| } | ||
|
|
||
| /** Current per-key write generation of either store: a rollback must not clobber a newer write. */ | ||
| public getStateWriteGeneration<K extends GlobalStateKey | SecretStateKey>(key: K): number { | ||
| return this.stateWriteGenerations.get(key) ?? 0 | ||
| } | ||
|
|
||
| private getAllGlobalState(): GlobalState { | ||
| return Object.fromEntries(GLOBAL_STATE_KEYS.map((key) => [key, this.getGlobalState(key)])) | ||
| } | ||
|
|
@@ -443,10 +459,20 @@ export class ContextProxy { | |
| } | ||
|
|
||
| storeSecret(key: SecretStateKey, value?: string) { | ||
| // Refused rather than deferred: a deferred secret write would re-store exactly the value the | ||
| // user asked to erase, and a silently dropped one would lose the save. | ||
| if (this.resetting) { | ||
| return Promise.reject( | ||
| new SettingsWriteError([], [key], new Error("secret writes are refused while state is being reset")), | ||
| ) | ||
| } | ||
| // Update cache. | ||
| const owner = {} | ||
| this.secretWriteOwner.set(key, owner) | ||
| this.secretWriteCount.set(key, (this.secretWriteCount.get(key) ?? 0) + 1) | ||
| // Counted here as well as in updateGlobalState: the guard that rolls back a failed per-view write reads | ||
| // this map, and a secret it cannot count is a secret whose newer same-valued write it cannot see. | ||
| this.stateWriteGenerations.set(key, (this.stateWriteGenerations.get(key) ?? 0) + 1) | ||
| this.secretCache[key] = value | ||
|
|
||
| // Write directly to context. If the write rejects, the cache is read back from the secret storage while this | ||
|
|
@@ -716,17 +742,27 @@ export class ContextProxy { | |
| * @returns A promise that resolves when all reset operations are complete | ||
| */ | ||
| public async resetAllState() { | ||
| // Clear in-memory caches | ||
| this.stateCache = {} | ||
| this.secretCache = {} | ||
| this.resetting = true | ||
| try { | ||
| // Clear in-memory caches | ||
| this.stateCache = {} | ||
| this.secretCache = {} | ||
| // A write that started before this reset must not restore its pre-reset value when it | ||
| // rejects afterwards, so advance every generation the pending writes still hold. | ||
| for (const key of this.stateWriteGenerations.keys()) { | ||
| this.stateWriteGenerations.set(key, (this.stateWriteGenerations.get(key) ?? 0) + 1) | ||
| } | ||
|
|
||
| await Promise.all([ | ||
| ...GLOBAL_STATE_KEYS.map((key) => this.originalContext.globalState.update(key, undefined)), | ||
| ...SECRET_STATE_KEYS.map((key) => this.originalContext.secrets.delete(key)), | ||
| ...GLOBAL_SECRET_KEYS.map((key) => this.originalContext.secrets.delete(key)), | ||
| ]) | ||
| await Promise.all([ | ||
| ...GLOBAL_STATE_KEYS.map((key) => this.originalContext.globalState.update(key, undefined)), | ||
| ...SECRET_STATE_KEYS.map((key) => this.originalContext.secrets.delete(key)), | ||
| ...GLOBAL_SECRET_KEYS.map((key) => this.originalContext.secrets.delete(key)), | ||
| ]) | ||
|
|
||
| await this.initialize() | ||
| await this.initialize() | ||
| } finally { | ||
| this.resetting = false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
|
|
||
| private static _instance: ContextProxy | null = null | ||
|
|
||
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.
Fail teardown when the clear methods are unavailable.
The root hook reports success if
sidebarProvideris missing. It can also log that cleanup succeeded when both method properties exist but holdundefined. In either case, the next test can inherit the old pin.Check that both methods are callable. Throw if either check fails. Then call both methods without optional chaining.