-
Notifications
You must be signed in to change notification settings - Fork 51
Resolve managed coding-agent config over local state at launch #265
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| """Resolve the effective agent settings from the managed config plus local ucode state. | ||
|
|
||
| The admin-authored manifest (``~/.ucode/managed-state.json``, written by | ||
| :mod:`ucode.managed_config`) and the developer's own ucode state (``~/.ucode/state.json``) stay | ||
| separate files — they are never merged on disk. Instead this module resolves them *per key* at | ||
| config-write time: whatever the manifest specifies wins, and anything it leaves unset falls back to | ||
| the developer's ucode state. The resolved view is what gets rendered into the agent config files | ||
| (e.g. ``~/.claude/ucode-settings.json``), so managed settings take precedence for every ``ucode`` | ||
| command without either file being rewritten. | ||
|
|
||
| Only settings the developer set *through* ucode participate in the fallback. Settings they wrote by | ||
| hand outside ucode (``~/.claude/settings.json``, etc.) are not read here — Claude Code merges | ||
| those scopes itself at launch, underneath the file ucode passes via ``--settings``. | ||
|
|
||
| Everything here is pure: no I/O, no mutation of the inputs. Fetching and persisting the manifest, | ||
| and handing the resolved state to the agent config writers, live in :mod:`ucode.managed_config`. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import cast | ||
|
|
||
| from ucode.state import MANAGED_OVERLAY_KEY | ||
|
|
||
| # Proto model-config slot -> the family key `claude.py`'s render_overlay reads. The manifest keeps | ||
| # the proto spelling (`default_opus_model`), while ucode state and render_overlay both key claude | ||
| # models by bare family (`opus`), so the two have to be bridged before the settings file is written. | ||
| _CLAUDE_FAMILY_SLOTS = { | ||
| "default_opus_model": "opus", | ||
| "default_sonnet_model": "sonnet", | ||
| "default_haiku_model": "haiku", | ||
| "default_fable_model": "fable", | ||
| } | ||
|
|
||
|
|
||
| def _as_dict(value: object) -> dict[str, object]: | ||
| """Return ``value`` as a ``dict[str, object]`` when it is a dict, else an empty dict.""" | ||
| return cast("dict[str, object]", value) if isinstance(value, dict) else {} | ||
|
|
||
|
|
||
| def _str(value: object) -> str | None: | ||
| """Return a non-empty stripped string, or None.""" | ||
| if isinstance(value, str): | ||
| stripped = value.strip() | ||
| return stripped or None | ||
| return None | ||
|
|
||
|
|
||
| def _agent_entry(managed: dict, tool: str) -> dict[str, object]: | ||
| """Return the manifest's config for ``tool``, or an empty dict when it isn't enabled.""" | ||
| enabled = _as_dict(_as_dict(managed).get("enabled_agents")) | ||
| return _as_dict(enabled.get(tool)) | ||
|
|
||
|
|
||
| def _agent_model_config(managed: dict, tool: str) -> dict[str, object]: | ||
| """Return the manifest's normalized ``model_config`` for ``tool``, if any.""" | ||
| return _as_dict(_agent_entry(managed, tool).get("model_config")) | ||
|
|
||
|
|
||
| def effective_agent_models(managed: dict, state: dict, tool: str) -> dict | list | None: | ||
| """Resolve ``tool``'s model list/slots from the manifest, falling back to ucode state. | ||
|
|
||
| Claude keys its models by family (``opus``/``sonnet``/``haiku``/``fable``) and resolves per | ||
| family, so a family the manifest omits keeps the developer's value. Every other agent stores a | ||
| flat list, which has no per-key identity — there the manifest's list replaces the local one | ||
| outright, or the local one stands when the manifest specifies none. | ||
| """ | ||
| manifest_models = _agent_model_config(managed, tool).get("models") | ||
| if tool == "claude": | ||
| local = dict(_as_dict(state.get("claude_models"))) | ||
| for slot, family in _CLAUDE_FAMILY_SLOTS.items(): | ||
|
asujithan marked this conversation as resolved.
|
||
| model = _str(_as_dict(manifest_models).get(slot)) | ||
| if model: | ||
| local[family] = model | ||
| return local or None | ||
| if isinstance(manifest_models, list): | ||
| models = [m for m in (_str(item) for item in manifest_models) if m] | ||
| if models: | ||
| return models | ||
| local_list = state.get(f"{tool}_models") | ||
| return local_list if local_list else None | ||
|
|
||
|
|
||
| def managed_provider_service(managed: dict, tool: str) -> str | None: | ||
| """Return only the provider the managed config specifies for ``tool``, ignoring local state.""" | ||
| return _str(_agent_model_config(managed, tool).get("model_provider_service")) | ||
|
|
||
|
|
||
| def resolve_state(managed: dict, state: dict, tool: str) -> dict: | ||
| """Return a copy of ``state`` with ``tool``'s managed values layered on top. | ||
|
|
||
| ``write_tool_config`` reads its models and provider out of the state dict it is handed, so | ||
| handing it this resolved copy is what makes managed settings win. Each key the managed config | ||
| displaces is recorded under :data:`~ucode.state.MANAGED_OVERLAY_KEY` with the developer's own | ||
| value (None when they had none), which ``save_state`` swaps back before writing — so the admin's | ||
| settings reach the generated agent config files without ``state.json`` losing what the developer | ||
| configured. The two files are never merged on disk. | ||
| """ | ||
| resolved = dict(state) | ||
| overlay: dict[str, object] = {} | ||
| models = effective_agent_models(managed, state, tool) | ||
| models_key = f"{tool}_models" | ||
| if models is not None and models != state.get(models_key): | ||
| overlay[models_key] = state.get(models_key) | ||
| resolved[models_key] = models | ||
| provider = managed_provider_service(managed, tool) | ||
| if provider: | ||
| providers = dict(_as_dict(state.get("provider_services"))) | ||
| if providers.get(tool) != provider: | ||
| overlay["provider_services"] = state.get("provider_services") | ||
| providers[tool] = provider | ||
| resolved["provider_services"] = providers | ||
| if overlay: | ||
| resolved[MANAGED_OVERLAY_KEY] = overlay | ||
| return resolved | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.