Skip to content

Commit aa6037c

Browse files
fix(webview): seed the re-pinned view with the shared profile's settings
The row on src/core/webview/webviewMessageHandler.ts:691 was right about the mechanism. The launch repair re-pinned this view to the still-valid shared profile and cleared the apiConfiguration buffer, which left the view with a name in its overlay and no settings of its own: getState merges viewLocalState over the shared values (ClineProvider.ts:4913) and a mutation carrying undefined deletes the key (_updateViewLocalStateFromMutation, ClineProvider.ts:5298-5304), so the pinned view resolved apiConfiguration from whatever the shared store names next - one sibling activation is enough - while still displaying its own pin. The repair now seeds the pinned profile's stored record without its name, which is how loadViewState pairs a name with its settings (ClineProvider.ts:1137-1140). One part of the finding is not what the code does. Nothing writes apiConfiguration into the durable view entry: _persistViewLocalStateFromMutation (ClineProvider.ts:5322-5332) persists mode and currentApiConfigName only, so the seed is an in-memory overlay write and the pin write below stays the only durable part of the repair. The seed is read before the pin write so both orderings stay safe. A failed seed write leaves the pin unwritten, so the invalid selection stays retryable on the next launch; a seed that lands while the pin write fails leaves the view holding the settings of the profile it is about to be pinned to rather than the missing profile's stale buffer. The read is wrapped because the record can vanish between the hasConfig check above and this getProfile call: falling back to an empty overlay is what a re-pin did before, so the repair survives that race instead of being swallowed by the catch around the block. Tests: webviewMessageHandler.spec.ts:971-1013 pins the seed and rejects the empty overlay, 1032-1059 keeps the seed-before-pin ordering, and 1061-1078 covers the vanished-record fallback. Control: head handler with the new spec reports 3 failed | 173 passed - two cases fail on getProfile being called zero times and the ordering case on calls[0] equal to ["apiConfiguration", undefined]; with the seed in place it is 176 passed, exit 0. The whole core/webview directory is 34 files and 1073 tests passing; src tsc --noEmit exit 0; eslint clean on both touched files with their suppressed counts unchanged (webviewMessageHandler.ts 5, its spec 35, src/eslint-suppressions.json back byte-identical to its blob at 3298). The compile-job command list in job order is 0,0,0,0,1,1,0,0,0,0,0,0,0: format:check, lint and check-types are green in this worktree, and the only reds are the two solheim record.sh suites, which need ffprobe and POSIX path separators this Windows box does not provide. Behaviour change, so the seed is what the reviewer asked for; no changeset, no CHANGELOG, no suppression change.
1 parent eab920d commit aa6037c

2 files changed

Lines changed: 74 additions & 15 deletions

File tree

‎src/core/webview/__tests__/webviewMessageHandler.spec.ts‎

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,7 @@ describe("webviewMessageHandler - webviewDidLaunch", () => {
911911
providerSettingsManager: {
912912
listConfig: () => Promise<unknown[]>
913913
hasConfig: (name: string) => Promise<boolean>
914+
getProfile: (options: { name: string }) => Promise<unknown>
914915
}
915916
activateProviderProfile: (options: { name: string }) => Promise<void>
916917
getMcpHub: () => unknown
@@ -933,6 +934,12 @@ describe("webviewMessageHandler - webviewDidLaunch", () => {
933934
.fn()
934935
.mockResolvedValue([{ name: "shared-profile", apiProvider: providerIdentifiers.anthropic }]),
935936
hasConfig: vi.fn().mockResolvedValue(false),
937+
getProfile: vi.fn().mockResolvedValue({
938+
id: "shared-profile-id",
939+
name: "shared-profile",
940+
apiProvider: providerIdentifiers.anthropic,
941+
apiKey: "sk-shared-profile",
942+
}),
936943
}
937944
double.activateProviderProfile = vi.fn().mockResolvedValue(undefined)
938945
double.getMcpHub = vi.fn().mockReturnValue(undefined)
@@ -980,10 +987,17 @@ describe("webviewMessageHandler - webviewDidLaunch", () => {
980987
// The view pin is re-pinned to the first available profile,
981988
// and the shared global selection is left untouched: no global write, no global activation.
982989
expect(mockClineProvider.saveViewState).toHaveBeenCalledWith("currentApiConfigName", "shared-profile")
983-
// The stale view-local overlay is cleared with the pin: a buffer still
984-
// holding the missing profile's settings would keep serving them on top of
985-
// the re-pinned selection.
986-
expect(mockClineProvider.saveViewState).toHaveBeenCalledWith("apiConfiguration", undefined)
990+
// A re-pin that leaves the overlay empty is half a repair: a view whose overlay carries a
991+
// name but no apiConfiguration resolves that field from the shared store, so it would
992+
// display its own pin while running whatever profile the shared selection names next. The
993+
// seed mirrors loadViewState - the stored record without its name.
994+
expect(double.providerSettingsManager.getProfile).toHaveBeenCalledWith({ name: "shared-profile" })
995+
expect(mockClineProvider.saveViewState).toHaveBeenCalledWith("apiConfiguration", {
996+
id: "shared-profile-id",
997+
apiProvider: providerIdentifiers.anthropic,
998+
apiKey: "sk-shared-profile",
999+
})
1000+
expect(mockClineProvider.saveViewState).not.toHaveBeenCalledWith("apiConfiguration", undefined)
9871001
expect(mockClineProvider.contextProxy.setValue).not.toHaveBeenCalledWith(
9881002
"currentApiConfigName",
9891003
"shared-profile",
@@ -1015,7 +1029,7 @@ describe("webviewMessageHandler - webviewDidLaunch", () => {
10151029
expect(mockClineProvider.activateProviderProfile).not.toHaveBeenCalled()
10161030
})
10171031

1018-
it("clears the stale overlay before persisting the re-pin so a failed pin stays retryable", async () => {
1032+
it("seeds the re-pinned profile's settings before persisting the re-pin so a failed pin stays retryable", async () => {
10191033
vi.mocked(mockClineProvider.providerSettingsManager.hasConfig).mockImplementation(
10201034
async (name: string) => name === "shared-profile",
10211035
)
@@ -1034,13 +1048,35 @@ describe("webviewMessageHandler - webviewDidLaunch", () => {
10341048
mockClineProvider.saveViewState = originalSaveViewState
10351049
}
10361050

1037-
// The overlay clear must land first: if the pin write then fails, the merged
1038-
// selection stays invalid and the repair retries on the next launch, instead of
1039-
// a valid pin serving the deleted profile's buffered settings.
1040-
expect(calls[0]).toEqual(["apiConfiguration", undefined])
1051+
// The seed must land before the pin write: a failed seed leaves the pin unwritten, so the
1052+
// merged selection stays invalid and the repair retries on the next launch instead of a
1053+
// valid pin serving the deleted profile's buffered settings.
1054+
expect(calls[0]).toEqual([
1055+
"apiConfiguration",
1056+
{ id: "shared-profile-id", apiProvider: providerIdentifiers.anthropic, apiKey: "sk-shared-profile" },
1057+
])
10411058
expect(calls).toContainEqual(["currentApiConfigName", "shared-profile"])
10421059
})
10431060

1061+
it("re-pins without a settings snapshot when the profile record vanishes before the seed read", async () => {
1062+
vi.mocked(mockClineProvider.providerSettingsManager.hasConfig).mockImplementation(
1063+
async (name: string) => name === "shared-profile",
1064+
)
1065+
vi.mocked(double.providerSettingsManager.getProfile).mockRejectedValue(new Error("profile vanished mid-flight"))
1066+
1067+
await webviewMessageHandler(mockClineProvider, { type: "webviewDidLaunch", viewStateId: "view-1" })
1068+
await new Promise((resolve) => setImmediate(resolve))
1069+
1070+
// A seed read that fails must not cost the view the repair: the overlay falls back to empty
1071+
// and the pin still lands, so the re-pin is not swallowed by the catch around this block.
1072+
expect(double.providerSettingsManager.getProfile).toHaveBeenCalledWith({ name: "shared-profile" })
1073+
expect(mockClineProvider.saveViewState).toHaveBeenCalledWith("apiConfiguration", undefined)
1074+
expect(mockClineProvider.saveViewState).toHaveBeenCalledWith("currentApiConfigName", "shared-profile")
1075+
expect(mockClineProvider.log).toHaveBeenCalledWith(
1076+
expect.stringContaining('Seed read failed for "shared-profile"'),
1077+
)
1078+
})
1079+
10441080
it("records the legacy repair without activating a profile when no name is listed", async () => {
10451081
double.providerSettingsManager.listConfig = vi
10461082
.fn()

‎src/core/webview/webviewMessageHandler.ts‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -674,12 +674,35 @@ export const webviewMessageHandler = async (
674674
// Re-pin this view to the still-valid shared global selection (not the
675675
// first listed profile) so the view adopts the shared choice; the
676676
// global selection itself is left untouched.
677-
// Clear the buffered overlay first: it belongs to the profile that
678-
// no longer resolves. If the pin write below then fails, the invalid
679-
// selection stays retryable on the next launch; pinning first would
680-
// leave a valid pin serving the stale profile's provider, model, and
681-
// keys with the repair skipped.
682-
await provider.saveViewState("apiConfiguration", undefined)
677+
// Seed the pinned profile's snapshot instead of only clearing the buffer: a view whose
678+
// overlay carries a name but no apiConfiguration resolves that field from the shared
679+
// ContextProxy - getState merges viewLocalState over it at ClineProvider.ts:4913 - so the
680+
// view would show its own pin while running whatever profile the shared store names next.
681+
// The snapshot is taken the way loadViewState takes it (ClineProvider.ts:1137-1140): the
682+
// stored record without its name.
683+
// Seed before the pin write. A seed write that fails leaves the pin unwritten, so the
684+
// invalid selection stays retryable on the next launch, and a seed that lands while the
685+
// pin write fails leaves the view holding the settings of the profile it is about to be
686+
// pinned to rather than the missing profile's stale buffer.
687+
let seedSettings: ProviderSettings | undefined
688+
try {
689+
const { name: _seedProfileName, ...seedProfile } =
690+
await provider.providerSettingsManager.getProfile({
691+
name: globalConfigName,
692+
})
693+
seedSettings = seedProfile as ProviderSettings
694+
} catch (error) {
695+
// The record can vanish between the hasConfig check above and this read. Fall back to
696+
// an empty overlay - what a re-pin left before the seed existed - so the pin write
697+
// still lands instead of the whole repair being lost to this error.
698+
provider.log(
699+
`[webviewDidLaunch] Seed read failed for "${globalConfigName}"; re-pinning without a settings snapshot: ${String(error)}`,
700+
)
701+
}
702+
// The durable view entry carries only mode and currentApiConfigName (see
703+
// _persistViewLocalStateFromMutation), so this seed is an in-memory overlay write; the
704+
// pin below is what persists.
705+
await provider.saveViewState("apiConfiguration", seedSettings)
683706
await provider.saveViewState("currentApiConfigName", globalConfigName)
684707
// The repair changes what this view resolves to, so the state has to be posted: the
685708
// two writes above update provider state without posting it, and the listApiConfig

0 commit comments

Comments
 (0)