Skip to content

fix: privileged context state not preserved across tool calls#54

Merged
freema merged 1 commit intomainfrom
fix/privileged-context-state
Mar 30, 2026
Merged

fix: privileged context state not preserved across tool calls#54
freema merged 1 commit intomainfrom
fix/privileged-context-state

Conversation

@freema
Copy link
Copy Markdown
Collaborator

@freema freema commented Mar 29, 2026

Summary

  • select_privileged_context switched the window and Marionette context but never updated currentContextId
  • Helper tools (set_firefox_prefs, get_firefox_prefs, list_extensions) saved the stale context ID and restored to it in their finally blocks, silently reverting the user's privileged context selection

Changes

  • src/tools/privileged-context.ts: call setCurrentContextId() after successful context switch
  • src/firefox/index.ts: expose setCurrentContextId() on FirefoxClient
  • src/tools/firefox-prefs.ts: skip context restore when originalContextId matches the chrome context (2 finally blocks)
  • src/tools/webextension.ts: same fix for list_extensions
  • tests/tools/privileged-context.test.ts: 2 tests that fail without the fix, pass with it

Test plan

  • 392/392 tests pass
  • Lint + typecheck pass

select_privileged_context switched the window and Marionette context
but never updated currentContextId. Helper tools (set_firefox_prefs,
get_firefox_prefs, list_extensions) saved the stale context and
restored to it in their finally blocks, silently reverting the user's
privileged context selection.

- Add setCurrentContextId() to FirefoxClient public API
- Call setCurrentContextId in select_privileged_context
- Skip context restore in helper tools when already on the right
  chrome context

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@freema freema requested a review from juliandescottes March 29, 2026 15:21
Copy link
Copy Markdown
Collaborator

@juliandescottes juliandescottes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

The fix looks great, one nit. For the test, let's clean it up and remove the misleading BUG mentions, since the bug in question is fixed by the PR.

Comment on lines +405 to +406
setCurrentContextId(contextId: string): void {
this.core.setCurrentContextId(contextId);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we could use the new helper at L74:

  (id: string) => this.core.setCurrentContextId(id) 

Comment on lines +58 to +59
// BUG: select_privileged_context does NOT call setCurrentContextId
// so currentContextId stays as 'original-content-context'
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are those "BUG" comments describing the issue fixed in the PR? If so I think we should rephrase them, in the long run they could be confusing. Here's I'm not sure if this is actually fixed or if it's a todo to fix later.

}));
});

it('select_privileged_context should update currentContextId (BUG: it does not)', async () => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BUG: it does not mention in the title here also seems confusing

@freema freema merged commit 038793d into main Mar 30, 2026
1 check passed
@juliandescottes
Copy link
Copy Markdown
Collaborator

PR was merged without addressing or answering comments, I will send a follow up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants