Skip to content

feat(automations): resolve simple-mode tier at run time - #3307

Merged
viktormarinho merged 4 commits into
mainfrom
viktormarinho/automation-model-config
May 7, 2026
Merged

viktormarinho merged 4 commits into
mainfrom
viktormarinho/automation-model-config

Conversation

@viktormarinho

@viktormarinho viktormarinho commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • Persist a tier intent (fast / smart / thinking) inside automation.models when Simple Mode is on. The stored credentialId / thinking.id become a snapshot fallback.
  • fire.ts calls a new resolveTierOverride(ctx, automation) that reads organization_settings.simple_mode and fetches the live ModelInfo via ctx.aiProviders.listModels — so thinking.{capabilities, limits, provider, title} reflect the current slot, not the snapshot. Required because ensureModelCompatibility, the max-output-tokens cap, and telemetry all consume those fields.
  • Auto-pin only when the saved credential/model actually matches a slot, so a legacy automation isn't silently re-pinned to "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.reset cleared RHF's dirty flag before the 250ms autosave fired. (Happy to split into a separate PR if preferred.)

How to Test

  1. Enable Simple Mode in /$org/settings/ai-providers and configure the Smart tier. Confirm the toggle persists on refresh (the autosave fix).
  2. Open an automation, pick the Smart tier in the dropdown. Verify automation.models.tier === "smart" is saved.
  3. Change the Smart slot to a different model in org settings. Fire the automation (cron / Test) — it should run on the new model, even without reopening the detail page.
  4. Open a legacy automation (no tier field) with Simple Mode enabled, rename it. Confirm tier is not auto-written when its model doesn't match a slot.
  5. Toggle Simple Mode off. Tier-pinned automations fall back to the stored snapshot and run as before.

Migration Notes

None. New tier field on automation.models is optional; legacy rows without it keep the prior runtime behavior.

Review Checklist

  • PR title is clear and descriptive
  • Changes are tested and working (5 new unit tests in resolve-tier-override.test.ts + updated build-stream-request.test.ts)
  • Documentation is updated (if needed)
  • No breaking changes

@github-actions

github-actions Bot commented May 7, 2026

Copy link
Copy Markdown
Contributor

🧪 Benchmark

Should we run the Virtual MCP strategy benchmark for this PR?

React with 👍 to run the benchmark.

Reaction Action
👍 Run quick benchmark (10 & 128 tools)

Benchmark will run on the next push after you react.

@github-actions

github-actions Bot commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

Release Options

Suggested: Patch (2.308.3) — based on fix: prefix

React with an emoji to override the release type:

Reaction Type Next Version
👍 Prerelease 2.308.3-alpha.1
🎉 Patch 2.308.3
❤️ Minor 2.309.0
🚀 Major 3.0.0

Current version: 2.308.2

Note: If multiple reactions exist, the smallest bump wins. If no reactions, the suggested bump is used (default: patch).

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]>
@viktormarinho
viktormarinho force-pushed the viktormarinho/automation-model-config branch from 4df9be6 to a91e102 Compare May 7, 2026 14:10
viktormarinho and others added 2 commits May 7, 2026 11:38
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]>

@cubic-dev-ai cubic-dev-ai Bot 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.

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);

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.

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;

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.

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]>
@viktormarinho viktormarinho changed the title fix(automations): apply simple model mode to automation detail UI feat(automations): resolve simple-mode tier at run time May 7, 2026
@viktormarinho
viktormarinho merged commit 51f8464 into main May 7, 2026
16 checks passed
@viktormarinho
viktormarinho deleted the viktormarinho/automation-model-config branch May 7, 2026 15:31
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.

1 participant