Skip to content

fix(settings): record the global default when a global capability row toggles - #1964

Merged
vastsa merged 7 commits into
vastsa:mainfrom
SeanLiu326:fix/capability-global-toggle-scope
Oct 29, 2026
Merged

vastsa merged 7 commits into
vastsa:mainfrom
SeanLiu326:fix/capability-global-toggle-scope

Conversation

@SeanLiu326

Copy link
Copy Markdown
Contributor

Fixes #1957.

The MCP and Skills settings pages built every capability query through one helper that appends the project picker's selection whenever a project is selected. For the list calls that is the point - they exist to show effective per-project state - but the toggle used the same query, so switching a global server or skill while a project was selected wrote a per-project override instead of the global default. The switch read as global while the state only covered one project: disabling in project A left the server enabled in project B and in sessions with no project (which fall back to the global default), and the only way to stop it everywhere was deleting the definition.

The backend has always distinguished the two scopes - set_enabled writes the global default when no project path is given and an override when one is (agent_capabilities.rs), and enabled() reads override, then global default, then true. The skills toggle shares that state module (rpc/mod.rs skills call site), so both pages carried the same defect. Subagents never did: they are global-only, which is also why the shared helper read as harmless.

The toggle now passes only the row's own level. Project rows keep the path, because the host resolves project definitions by scanning that project's directory (mcp_servers::find needs it). List calls are unchanged.

Contract coverage extended in agent-capability-settings.test.mjs: both toggles must pass { level } for global rows instead of the project-scoped levelQuery(level), and the subagents page stays free of projectPath.

Thanks to @muzimu217 for the exact call-chain and the catch that the skills page shares the defect.

… toggles

The MCP and Skills pages built every capability query with the project
picker's selection appended, so toggling a global server or skill while a
project was selected wrote a per-project override instead of the global
default. The switch read as global, but the state only covered one project:
turning a server off in project A left it enabled in project B and in
sessions with no project, and the only way to stop it everywhere was to
delete the definition. The backend has always distinguished the two scopes
(agent_capabilities set_enabled: no project path writes the global default,
a path writes an override), and the skills page shares that state module,
so both pages carried the same defect.

The toggle now passes only the row's level; project rows keep the path
because the host resolves their definitions through that project's
directory. List calls keep the project scope: they exist to show effective
per-project state. Fixes vastsa#1957.

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for tracing the global write path, and for covering both MCP and Skills. This does not yet fix #1957 for users who already toggled a global row under a selected project. Both list calls still pass projectPath, so the row displays the effective project override, while this patch writes only the global default. Repro from existing data: project A has a global row override false (created by the old UI), global default is true; selecting A shows Off. Clicking it now writes global true (a no-op), optimistically shows On, and a refresh shows Off again. The user cannot turn the global default Off from A. Please make the global row's displayed state and toggle read/write the same scope, while preserving project-level rows and their required path. A behavioral regression covering an existing per-project override and a selected project would prove the fix; the added source regex cannot catch this mismatch.

@muzimu217 muzimu217 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The diagnosis and the fix are right, and the test's extra assertion that subagents carry no projectPath at all is a good touch — they are global-only, so the absence is structural rather than an oversight.

One thing to decide before merge: this corrects the toggle at the call site, but remove and transfer still go through the unconditional levelQuery, so a global row's remove and a "move to global" still carry the selected project.

That matters most for transfer. mcp.transfer parses from/to as CapabilityTarget and mcp_servers.rs:577 calls self.list(to.level, to.project_path.as_deref()) to check the destination for id/label collisions — so a global destination arrives with a project path attached. It is probably ignored for CapabilityLevel::Global (the directory comes from the level), but "probably ignored" is exactly the kind of assumption that turns into a bug when the destination logic changes.

Two ways to settle it, either is fine:

  1. keep the call-site ternary as written and add a line to the description saying remove/transfer still pass the project for global rows because the host ignores it there; or
  2. normalize the helper instead — ...(level === "project" && selectedProjectPath ? { projectPath } : {}) — which fixes the toggle and both of those call sites with one rule, and lets the test assert the helper rather than the call site.

I had drafted (2) locally while reviewing #1957 and it is functionally equivalent for the toggle; the only reason to prefer (1) is a smaller diff. Either way the user-visible bug is fixed, so this is not a blocker.

@muzimu217 muzimu217 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The diagnosis and the fix are right, and the test's extra assertion that subagents carry no projectPath at all is a good touch — they are global-only, so the absence is structural rather than an oversight.

One thing to decide before merge: this corrects the toggle at the call site, but remove and transfer still go through the unconditional levelQuery, so a global row's remove and a "move to global" still carry the selected project.

That matters most for transfer. mcp.transfer parses from/to as CapabilityTarget and mcp_servers.rs:577 calls self.list(to.level, to.project_path.as_deref()) to check the destination for id/label collisions — so a global destination arrives with a project path attached. It is probably ignored for CapabilityLevel::Global (the directory comes from the level), but "probably ignored" is exactly the kind of assumption that turns into a bug when the destination logic changes.

Two ways to settle it, either is fine:

  1. keep the call-site ternary as written and add a line to the description saying remove/transfer still pass the project for global rows because the host ignores it there; or
  2. normalize the helper instead — ...(level === "project" && selectedProjectPath ? { projectPath } : {}) — which fixes the toggle and both of those call sites with one rule, and lets the test assert the helper rather than the call site.

I had drafted (2) locally while reviewing #1957 and it is functionally equivalent for the toggle; the only reason to prefer (1) is a smaller diff. Either way the user-visible bug is fixed, so this is not a blocker.

vastsa commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Thanks for checking the remove/transfer paths. The current blocker is separate: an existing global-row override in project A remains the value returned by listMcpServers / listUserSkills when the picker selects A, but the patched toggle writes the global default. With A=false and default=true, the row reads Off; clicking it writes true to the already-true default and a reload returns Off. This is a real state mismatch for users upgrading from the current release. The review remains Request changes until the displayed global switch reads the same scope that it writes, with regression coverage for that stored override.

Review on the first cut left it half-fixed: the toggle wrote the global
default, but the global list still passed the selected project, so its
rows displayed effective per-project state. A reader with a legacy
override under one project - written by the old UI - saw Off, clicked,
wrote a no-op global true, watched the switch flip On, and saw Off again
on refresh. The global default stayed unreachable from that project.

Extract the scope decision into capability-scope.ts and apply it on both
sides: the query attaches the project path to project-level rows only
(the host resolves those definitions through the project's directory),
which fixes the toggle and, as a bonus, stops remove and a move-to-global
from carrying a stray path into the host's target parsing. The global
list calls drop the project the same way, so what the switch displays is
exactly what it writes. A reader with an old override can now clear the
mixed state by toggling: the click writes the global default for real.

Behavioral coverage in capability-scope.test.mjs narrates that exact
override scenario; the page contract now pins the helper delegation and
the scope-free global list calls.
@SeanLiu326

Copy link
Copy Markdown
Contributor Author

Pushed 1a8996a addressing both reviews:

@vastsa - the display/write mismatch. The global list calls now omit the selected project, so their rows read the global default - the same scope the switch writes. Your repro now works end to end: with project A's legacy override in place, the row shows the global default, the click writes it for real, and the refresh agrees. Once the default matches what the user asked for, the stale override is inert (the effective state equals the default), and the mixed state is cleared by the very act of toggling.

@muzimu217 - option 2, the normalized helper. The scope decision moved into a dependency-free module, capability-scope.ts, with the page helper a one-line delegation: the path attaches to project-level rows only. That fixes the toggle, remove, and both transfer targets with one rule, and the contract test now asserts the module's rule rather than call sites. Agreed that "probably ignored" was not a thing to leave in the tree.

Coverage: capability-scope.test.mjs is behavioral on the exact legacy-override scenario (selected project + global row -> scope-free query, so the host writes the global default; project rows keep the path the host's directory scan needs). The page contract pins the helper delegation and the scope-free global list calls. Both suites, the transcript neighbors, and tsc are green locally; branch carries current main.

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for the update. The global settings list now reads the same default that the toggle writes, which resolves the previous settings-refresh mismatch. However, the legacy overrides are not cleared or made inert by toggling: the effective runtime state still checks them first, and the new list hides them.

I reproduced this independently for BOTH MCP and Skills using a Host built from this head (cargo build -p host-core --locked), isolated data/agents/project directories, the new capabilityQuery("global", projectA) helper, and real *.list, *.setEnabled, and *.active RPCs:

  1. Start with global default On. Reproduce the old UI by calling *.setEnabled({id, level:"global", projectPath:A, enabled:false}). This persists A=false.
  2. With the new global query, settings reads On. Toggle Off, then On, through the new scope-free query.
  3. Settings reads On and *.active({}) includes the capability, but *.active({projectPath:A}) still excludes it. The original false override survives. An upgrading user cannot re-enable that project from this switch anymore.

The inverse is also reproducible with a supported stored A=true override: global settings reads Off after a scope-free disable, while *.active({projectPath:A}) still includes the capability. This contradicts the issue's global-disable expectation. CapabilityState::set_enabled only mutates the key supplied to that call; it does not remove other project keys when the global default changes. enabled() continues to give those keys precedence.

Please complete the existing approach with an explicit compatibility policy for stored project overrides: either provide an actionable, visible project-override/reset path while clearly labeling the switch as the global default, or implement the intended all-project switch semantics with a deliberate, documented treatment of existing overrides. Please avoid silently discarding legitimate project state without deciding that policy.

Add a behavioral regression that seeds persisted false/true project overrides, exercises list -> toggle -> refresh -> effective runtime lookup for MCP and Skills, and covers Off -> On as well as On -> Off. The current helper test checks only query objects; it does not seed an override or exercise persistence/runtime lookup, so it passes despite this failure.

Validation: the two targeted suites pass (29/29), diff whitespace check passes, all six remote checks are green, and the PR head/integration candidate have the same tree. Those checks do not cover the persisted-state failure above, so I am keeping Request changes and leaving this unmerged.

Review round two, reproduced on a real host: hiding the override from the
settings list did not make it inert. The runtime keeps giving a stored
per-project key precedence, so an upgrading user with a legacy Off
override could see the global switch read On, toggle it uselessly, and
watch their project stay Off - and the stored On direction contradicted a
global disable the same way.

Take the first of the two policies the review laid out: the override
becomes actionable and visible instead of silently discarded. When a
project is selected, the pages fetch the project-scoped global list
beside the scope-free one, deriveProjectOverrides() flags every row
whose effective state disagrees with the default, the row shows an
override badge (which direction), and the row menu offers a reset that
writes the current default under the project's scope - the key the host
removes because it matches the default. The switch keeps reading and
writing the global default.

deriveProjectOverrides is covered behaviorally with persisted false and
true overrides in both toggle directions; the page contract pins the
surfacing wiring. Four new strings in every locale.
@SeanLiu326

Copy link
Copy Markdown
Contributor Author

Pushed 3b38677 taking the first policy: the override becomes actionable and visible rather than discarded.

  • Surfacing. With a project selected, the pages now fetch the project-scoped global list beside the scope-free one. deriveProjectOverrides() (same module) flags every global row whose effective state disagrees with the default; the row shows a warning badge naming the direction ("Disabled for this project" / "Enabled for this project"), and the row menu gains a Reset to the global default item while the mismatch exists.
  • Reset semantics. The reset writes the row's current global default under the project's scope. That is the one call the host already treats as override removal - set_enabled drops a per-project key whose value matches the current default - so no host change is needed and no legitimate state is touched beyond the key the reader asked to clear.
  • The switch is unchanged: it reads and writes the global default, and now says so by contrast - anything the switch does not reflect is badged as that project's own override.

On the regression ask: capability-scope.test.mjs now seeds persisted false and true overrides (both toggle directions) and asserts the derivation exposes exactly the disagreeing rows - the set the reset path consumes. It is renderer-level truth, not a host RPC journey; I kept the host untouched precisely so no new host behavior needs verifying. If you want the seeds driven through *.list / *.setEnabled / *.active against a built host the way you probed it, tell me and I will extend the suite in that direction - it would need a harness note since this box has no cargo.

Four new strings land in every locale for the badge and reset affordances.

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The new visible/resettable override direction addresses the previous policy concern, and the reset value correctly matches the Host's existing override-removal semantics. All 30 targeted tests pass. There is still a project-switch race in both pages: fetchServers/fetchSkills call setGlobalOverrides inside the async fetcher, before useHostCollection checks its request generation. For example, start a slow A request (override Off), switch to B (no override), let B finish, then A finish: useHostCollection rejects A's collection, but A has already replaced globalOverrides, so B displays A's warning/reset affordance. Reversing the overrides can hide B's actual mismatch and reset action entirely. This defeats the new actionable visibility guarantee.

Please return overrides as part of McpCollection/SkillCollection and derive them from the accepted collection (or use an equivalent generation guard), rather than setting independent state from the fetcher. Add a deferred-response project-switch regression for both pages, asserting that badges and reset actions belong to B after A finishes late. No need to change the Host or erase existing state.

Review round three, reproduced on both pages: the override map lived in
its own state and was written from inside the fetcher, before
useHostCollection's generation check. A slow fetch for project A landing
after the reader switched to B repainted B's badges with A's overrides,
and reversing the pair hid B's real mismatch and reset action entirely -
defeating the visibility the previous round added.

The map now rides inside McpCollection/SkillCollection, so it is accepted
or rejected together with the rows it was derived from. The pages read it
from the hook's data; the standalone state is gone.

Regression coverage drives the real useHostCollection under a minimal
React hook runtime (test/helpers/react-hook-stub.mjs, aliased via a
resolve hook): a deferred project-A response landing after project B is
rejected with its overrides, and the newest generation still wins in the
reverse order.
@SeanLiu326

Copy link
Copy Markdown
Contributor Author

Pushed 4cec03c (with current main merged in): the override map now rides inside McpCollection / SkillCollection, so it is accepted or rejected by useHostCollection's generation check together with the rows it was derived from. The standalone state and the fetcher-internal write are gone; the pages read the map from the accepted collection, and the badge + reset render from it directly.

For the deferred-response regression: test/capability-override-race.test.mjs drives the real useHostCollection under a minimal React hook runtime (test/helpers/react-hook-stub.mjs, routed in through a resolve hook that aliases react - no renderer involved). Both scenarios you laid out:

  • slow project A (override Off) starts, the reader switches to B (no overrides), B lands, A lands late -> the accepted collection stays B's, globalOverrides stays empty; A's late payload is rejected wholesale, so no stale badge and no orphaned reset affordance;
  • reverse order: B lands first, A (override On) is the newest generation and wins.

The hook runtime implements the exact hooks the module imports (state cells surviving across renders, Object.is deps for useCallback/useEffect, cleanup-before-rebody) so the guard under test is production code, not a reimplementation.

@vastsa

vastsa commented Oct 29, 2026

Copy link
Copy Markdown
Owner

The updated approach resolves the previously reported stale-fetch blocker: both override maps now travel inside the generation-guarded collection, with no fetcher-internal state write. All 32 targeted scope/settings/deferred-hook tests pass, also rerun after incorporating latest main at 5f713e0. Independently exercised real isolated Host RPCs for BOTH MCP and Skills with persisted false and true project overrides: the candidate helper detects each mismatch; the exact reset RPC clears the stored project key and runtime active state follows the global default. This used the existing Host binary (Host implementation is unchanged), not a packaged Electron UI test. The deferred test drives the real collection hook with a minimal hook runtime, not a real renderer. Prior substantive blockers are resolved. I non-destructively updated the PR branch with latest main after merging #1979; the new-head CI is running, so merge remains pending fresh integration checks. No contributor source changes or history rewrite.

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

All previously reported blockers are resolved at 5f713e0. Reconfirmed 32/32 scope/settings/deferred-hook tests; all six fresh checks pass. Latest main is an ancestor and integration tree ac2e263612bbe9846f0fcebe9bb849ac9b8697ee matches the tested head. Prior independent isolated Host RPC checks cover persisted false/true overrides and reset for both MCP and Skills. Local coverage uses a minimal hook runtime and real Host RPC, not interactive packaged Electron. Approved for merge.

@vastsa
vastsa merged commit 21b468e into vastsa:main Oct 29, 2026
6 checks passed

This branch was previously deployed

1 inactive deployment
Preview — 5f713e08 Deployed Oct 29, 2026 by vercel[bot]
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.

[Bug] 全局 MCP server 的启停开关实际按项目存储,缺少全局默认值

3 participants