Repository navigation
feat(automations): resolve simple-mode tier at run time - #3307
Conversation
🧪 BenchmarkShould we run the Virtual MCP strategy benchmark for this PR? React with 👍 to run the benchmark.
Benchmark will run on the next push after you react. |
Release OptionsSuggested: Patch ( React with an emoji to override the release type:
Current version:
|
Stop baking the resolved model into the automation row. When the user
saves with Simple Mode enabled, persist a `tier` intent ("fast" /
"smart" / "thinking") inside `automation.models`; `buildStreamRequest`
then looks up the live tier slot from `organization_settings.simple_mode`
and uses that instead of the stored credentialId / thinking.id snapshot.
Effects:
- Dormant automations now pick up org Simple Mode changes the next time
they fire — no UI visit needed to reconcile.
- The detail page derives the dropdown label from `models.tier` first
(truthful even after a slot reconfig), falling back to slot-match for
legacy automations.
- The form clears `tier` whenever the user explicitly picks via the
ModelSelector fallback, so an explicit override beats the tier intent.
- Legacy automations (no tier persisted) keep using the stored snapshot
until next save, at which point Simple Mode is enabled they get pinned
to the matched / default tier.
Tests cover the four resolution paths (override, simple-mode disabled,
tier slot unset, no tier on legacy row).
This builds on the UI work merged in #3305 — that PR aligned the model
selector with the chat input but kept baking the resolved model into the
row, so dormant automations stayed stale until someone opened them.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
4df9be6 to
a91e102
Compare
Address review on PR #3307: #1 — The previous override only patched `credentialId` and `thinking.id`, leaving stale `capabilities` / `limits` / `provider` / `title` from the saved snapshot. Downstream code (`ensureModelCompatibility`, the max-output-tokens cap, telemetry, prompt headers) consumes those fields, so a tier whose live slot pointed to a different model would get gated against the wrong capability set. Replace the patch with a typed `TierOverride` produced by `resolveTierOverride`, which fetches the full `ModelInfo` via `ctx.aiProviders.listModels` and translates it into the `thinking` shape — same translation as the chat path's `toMetadataModelInfo`. Falls back to a slot-only override (with stored title) if the provider call throws, so a flaky provider doesn't block the run. #2 — Saving any edit on a Simple-Mode-enabled legacy automation (e.g. just renaming it) used to silently write `tier: "smart"` because `tierToPersist` fell back to the dropdown's display fallback. After that, the run path would override the user's original snapshot model with the org's live "smart" slot. Only auto-pin when `slotMatchedTier` is truthy — i.e. the saved credential/model actually correspond to a configured slot. Legacy automations with no slot match stay unpinned on incidental saves; the user has to explicitly pick a tier via the dropdown to opt in. Tests: split build-stream-request tests into "tier override" (verifies full thinking replacement) and add a new resolve-tier-override.test.ts that exercises ModelInfo translation, the disabled / unset / no-tier short-circuits, and the listModels-throws fallback. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.7 <[email protected]>
There was a problem hiding this comment.
2 issues found across 11 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="apps/mesh/src/automations/fire.ts">
<violation number="1" location="apps/mesh/src/automations/fire.ts:130">
P1: Bound the `resolveTierOverride` await by the abort signal; otherwise pre-request provider/storage calls can hang past `runTimeoutMs` because timeout cancellation is only applied to `request.abortSignal` later.</violation>
</file>
<file name="apps/mesh/src/automations/resolve-tier-override.ts">
<violation number="1" location="apps/mesh/src/automations/resolve-tier-override.ts:43">
P2: Guard the parsed JSON shape before reading `tier`; non-object JSON (like `"null"`) can crash at runtime.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| // current Simple Mode slot — fetches fresh capabilities / limits / | ||
| // title from the AI provider so downstream gates (model-compat, | ||
| // max-tokens cap) see the right metadata. | ||
| const tierOverride = await resolveTierOverride(ctx, automation); |
There was a problem hiding this comment.
P1: Bound the resolveTierOverride await by the abort signal; otherwise pre-request provider/storage calls can hang past runTimeoutMs because timeout cancellation is only applied to request.abortSignal later.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/mesh/src/automations/fire.ts, line 130:
<comment>Bound the `resolveTierOverride` await by the abort signal; otherwise pre-request provider/storage calls can hang past `runTimeoutMs` because timeout cancellation is only applied to `request.abortSignal` later.</comment>
<file context>
@@ -122,7 +123,17 @@ export async function fireAutomation(opts: {
+ // current Simple Mode slot — fetches fresh capabilities / limits /
+ // title from the AI provider so downstream gates (model-compat,
+ // max-tokens cap) see the right metadata.
+ const tierOverride = await resolveTierOverride(ctx, automation);
+ const request = buildStreamRequest(
+ automation,
</file context>
| } catch { | ||
| return null; | ||
| } | ||
| const tier = parsed.tier; |
There was a problem hiding this comment.
P2: Guard the parsed JSON shape before reading tier; non-object JSON (like "null") can crash at runtime.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/mesh/src/automations/resolve-tier-override.ts, line 43:
<comment>Guard the parsed JSON shape before reading `tier`; non-object JSON (like `"null"`) can crash at runtime.</comment>
<file context>
@@ -0,0 +1,103 @@
+ } catch {
+ return null;
+ }
+ const tier = parsed.tier;
+ if (!tier) return null;
+
</file context>
The autosave gated on `formState.isDirty`, but RHF's dirty flag was
cleared before the 250ms debounce fired:
- `handleToggle` calls `form.reset(...)` to seed default tier slots,
which rebases the dirty baseline (even with keepDirty: true, fields
that weren't already dirty stay clean).
- `useForm({ values: simpleMode })` re-syncs whenever the React Query
cache emits a new object reference, which `useSimpleMode`'s select
does on every render.
By the time the timer fired, `isDirty` was false and `save` bailed,
so the mutation never ran and the row was never inserted. First-time
toggle-on on a fresh org silently no-op'd.
Drop the isDirty gate. Each `schedule()` call is the explicit save
intent — only user actions call it, and nothing inside `save`
re-schedules, so there's no feedback loop.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
What is this contribution about?
Builds on #3305 (which aligned the automation model selector with the chat input). That PR kept baking the resolved model into the row, so dormant automations stayed on a stale snapshot when admins reconfigured tier slots. This PR moves resolution to run time:
tierintent (fast/smart/thinking) insideautomation.modelswhen Simple Mode is on. The storedcredentialId/thinking.idbecome a snapshot fallback.fire.tscalls a newresolveTierOverride(ctx, automation)that readsorganization_settings.simple_modeand fetches the liveModelInfoviactx.aiProviders.listModels— sothinking.{capabilities, limits, provider, title}reflect the current slot, not the snapshot. Required becauseensureModelCompatibility, the max-output-tokens cap, and telemetry all consume those fields."smart"on incidental edits.Also bundles a fix for an unrelated bug uncovered while testing: the Simple Mode toggle on the AI Providers settings page failed to persist on first enable because
form.resetcleared RHF's dirty flag before the 250ms autosave fired. (Happy to split into a separate PR if preferred.)How to Test
/$org/settings/ai-providersand configure the Smart tier. Confirm the toggle persists on refresh (the autosave fix).automation.models.tier === "smart"is saved.tierfield) with Simple Mode enabled, rename it. Confirmtieris not auto-written when its model doesn't match a slot.Migration Notes
None. New
tierfield onautomation.modelsis optional; legacy rows without it keep the prior runtime behavior.Review Checklist
resolve-tier-override.test.ts+ updatedbuild-stream-request.test.ts)