Skip to content

fix(frontend): scope agent triggers to the revision they were added on - #5182

Closed
ashrafchowdury wants to merge 2 commits into
big-agentsfrom
fix/frontendagent-triggers-state
Closed

fix(frontend): scope agent triggers to the revision they were added on#5182
ashrafchowdury wants to merge 2 commits into
big-agentsfrom
fix/frontendagent-triggers-state

Conversation

@ashrafchowdury

Copy link
Copy Markdown
Contributor

Context

In the agent playground, adding a trigger on one revision made it show up on
every revision of that variant, including ones committed before it. Add a
trigger on revision X, open revision X-1, and the trigger was listed there too.

Cause: the Triggers section scoped a trigger by matching its data.references
(a variant-level id) against the agent's ids. Any trigger bound to the variant
therefore appeared on all of that variant's revisions.

Changes

Triggers created from the playground now record the revision they were added on
in the trigger's meta:

agenta_agent_trigger_origin: {revision_id, variant_id, revision_version}

The section filters on that origin instead of the variant id alone. A trigger
shows on its origin revision and on later revisions of the same variant, and
never on earlier revisions or on sibling variants.

The runtime binding is untouched. data.references still points at the variant,
which the backend resolves to the latest revision at fire time
(triggers/service.py). The origin marker is display-only metadata. We did not
switch the reference to a specific revision, because a revision-level reference
pins the trigger to an immutable snapshot and breaks that latest-tracking.

Matching (triggerScope.ts agentTriggerMatchesContext):

  • Origin present: show if the current revision is the origin, or same variant
    with current version >= origin version.
  • No origin (legacy triggers, or triggers pinned to an exact revision): fall
    back to the existing reference match, so their behavior is unchanged.

Tests

  • New triggerScope.test.ts (7 cases): origin shows on the same and later
    revisions, hides on older revisions and on sibling variants, legacy
    variant-bound triggers stay visible everywhere, and exact-revision references
    still match.
  • pnpm --filter @agenta/entity-ui exec vitest run and
    pnpm turbo run build --filter=@agenta/entities --filter=@agenta/entity-ui
    both pass.

What to QA

  • Open a variant in the agent playground and add a trigger (subscription or
    schedule) on the current revision. It appears in the Triggers section.
  • Open an older revision of the same variant. The trigger is not listed.
  • Edit the agent and commit a new revision. The trigger still shows on the new
    revision.
  • Regression: a trigger added before this change (variant-bound, no origin
    marker) still shows across the variant's revisions as before.
  • Regression: switch to a different variant. Its own triggers show; the first
    variant's do not.

@vercel

vercel Bot commented Jul 9, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agenta-documentation Ready Ready Preview, Comment Jul 9, 2026 3:59pm

Request Review

@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • New Features
    • Trigger creation now preserves extra metadata, helping new triggers remember their source revision and context.
    • Trigger visibility is now smarter for agent-based workflows, with improved matching across revisions and variants.
  • Bug Fixes
    • New triggers created from the drawer now keep their metadata in sync when submitted.
    • Trigger lists now handle older and variant-related entries more consistently.

Walkthrough

Adds trigger-origin metadata (revision id, variant id, revision version) tracked via new triggerScope.ts utilities. Replaces reference-based trigger scoping in TriggerManagementSection with agentTriggerMatchesContext. Propagates a new defaultMeta field through drawer state and create request payloads for subscriptions and schedules. Adds unit tests.

Changes

Trigger origin scoping and default metadata

Layer / File(s) Summary
Drawer state defaultMeta field
web/packages/agenta-entities/src/gatewayTrigger/state/atoms.ts
Adds optional defaultMeta property to SubscriptionDrawerState and ScheduleDrawerState.
Trigger origin/context utilities
web/packages/agenta-entity-ui/src/DrillInView/SchemaControls/triggerScope.ts
New module defining trigger-origin types, context derivation from workflow, origin building/attachment/parsing, and agentTriggerMatchesContext matching logic.
Scoping wiring in TriggerManagementSection
web/packages/agenta-entity-ui/src/DrillInView/SchemaControls/TriggerManagementSection.tsx
Removes old referencesMatch scoping, computes triggerContext/triggerOrigin, filters triggers via agentTriggerMatchesContext, and passes defaultMeta into drawer-open calls.
Create-request meta propagation
web/packages/agenta-entity-ui/src/gatewayTrigger/drawers/TriggerScheduleDrawer.tsx, .../TriggerSubscriptionDrawer.tsx
Create request bodies now include meta from state?.defaultMeta, with updated useCallback dependencies.
Trigger scope unit tests
web/packages/agenta-entity-ui/tests/unit/triggerScope.test.ts
New tests for agentTriggerMatchesContext and buildAgentTriggerOrigin/addAgentTriggerOriginMeta.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant TriggerManagementSection
    participant triggerScope
    participant SubscriptionOrScheduleDrawer
    participant BackendAPI

    User->>TriggerManagementSection: Add trigger
    TriggerManagementSection->>triggerScope: getAgentTriggerContext(entityId, workflow)
    triggerScope-->>TriggerManagementSection: AgentTriggerContext
    TriggerManagementSection->>triggerScope: buildAgentTriggerOrigin(context)
    triggerScope-->>TriggerManagementSection: AgentTriggerOrigin
    TriggerManagementSection->>triggerScope: addAgentTriggerOriginMeta(null, origin)
    triggerScope-->>TriggerManagementSection: defaultMeta
    TriggerManagementSection->>SubscriptionOrScheduleDrawer: openDrawer(defaultMeta, ...)
    User->>SubscriptionOrScheduleDrawer: Submit create form
    SubscriptionOrScheduleDrawer->>BackendAPI: create request with meta=defaultMeta
    BackendAPI-->>SubscriptionOrScheduleDrawer: created trigger

    Note over TriggerManagementSection,triggerScope: Later, listing triggers
    TriggerManagementSection->>triggerScope: agentTriggerMatchesContext(trigger, context, agentIds)
    triggerScope-->>TriggerManagementSection: boolean match result
Loading

Possibly related PRs

  • Agenta-AI/agenta#4883: Prior implementation of TriggerManagementSection.tsx/useAgentTriggers and subscription/schedule drawer create payloads that this PR directly modifies.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the main change: scoping agent triggers to the revision they were added on.
Description check ✅ Passed The description matches the PR and explains the scoping fix, metadata, matching behavior, and tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/frontendagent-triggers-state

Warning

Tools execution failed with the following error:

Failed to run tools: 13 INTERNAL: Received RST_STREAM with code 2 (Internal server error)


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ashrafchowdury

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. Frontend labels Jul 9, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
web/packages/agenta-entity-ui/src/DrillInView/SchemaControls/TriggerManagementSection.tsx (1)

878-913: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Include defaultMeta in the add-menu memo dependencies.

Line 899 and Line 913 now close over defaultMeta, but the groups useMemo dependency list omits it. If revision/version data resolves after the first render, the drawers can receive stale origin metadata.

Proposed fix
         [
             openSubscriptionDrawer,
             openScheduleDrawer,
             defaultReferences,
             defaultBoundLabel,
+            defaultMeta,
             entityId,
         ],

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 478e45cd-4cfb-4dea-8c87-a022f206ba3e

📥 Commits

Reviewing files that changed from the base of the PR and between 7264d79 and a693530.

📒 Files selected for processing (6)
  • web/packages/agenta-entities/src/gatewayTrigger/state/atoms.ts
  • web/packages/agenta-entity-ui/src/DrillInView/SchemaControls/TriggerManagementSection.tsx
  • web/packages/agenta-entity-ui/src/DrillInView/SchemaControls/triggerScope.ts
  • web/packages/agenta-entity-ui/src/gatewayTrigger/drawers/TriggerScheduleDrawer.tsx
  • web/packages/agenta-entity-ui/src/gatewayTrigger/drawers/TriggerSubscriptionDrawer.tsx
  • web/packages/agenta-entity-ui/tests/unit/triggerScope.test.ts

Comment on lines +25 to +28
function finiteNumber(value: unknown): number | null {
const number = typeof value === "number" ? value : Number(value)
return Number.isFinite(number) ? number : null
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Preserve null revision versions instead of coercing them to 0.

Number(null) returns 0, so missing/null revision_version is treated as v0 and can make origin-scoped triggers match later revisions when version metadata is unavailable.

Proposed fix
 function finiteNumber(value: unknown): number | null {
+    if (value == null || value === "") return null
     const number = typeof value === "number" ? value : Number(value)
     return Number.isFinite(number) ? number : null
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function finiteNumber(value: unknown): number | null {
const number = typeof value === "number" ? value : Number(value)
return Number.isFinite(number) ? number : null
}
function finiteNumber(value: unknown): number | null {
if (value == null || value === "") return null
const number = typeof value === "number" ? value : Number(value)
return Number.isFinite(number) ? number : null
}

@mmabrouk mmabrouk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hmmm.. not sure here @ashrafchowdury

It feels to me that it is a better UX to have the trigger show in all revisions, but maybe instead clarify in the card in the playground which revision it triggers to.

Because in many cases the trigger was set to a wrong/old revision and I wanted to update it, not create a new one. Assuming I will have many versions, and I make a mistake, now I need to search old revisions looking for my trigger to change it.

Also, say I make a mistake and I add a trigger to a previous pinned veriosn, now I create a new one, and I don't see any trigger, I might assume I don't have a trigger and add a new one. Now I have two triggers.

Maybe you can share which user flow / use case did you stumble upon that brought you to this solution

@ashrafchowdury

Copy link
Copy Markdown
Contributor Author

For me, it was not normal to see the trigger I had added in the updated variant/revision and suddenly see the trigger on the previous default revision-1/variants

https://jam.dev/c/eedf6a0d-6146-49e1-b934-958a6ee05a38

@ashrafchowdury

Copy link
Copy Markdown
Contributor Author

@mmabrouk any thoughts on this, otherwise lets close it

@mmabrouk

Copy link
Copy Markdown
Member

Thank you @ashrafchowdury for the video. It explains the issue very well. I think the solution is to 1. show the trigger 2. show in the playground card which revision the trigger triggers with a clear warning that it triggers a different version in case that is true. I will close this and create an issue based on this linking to this one

@mmabrouk

Copy link
Copy Markdown
Member

Closing in favor of #5508, which captures the full problem (show the trigger, show which revision it fires, warn on mismatch) with the jam recording and links back to this PR and the #5113 design.

@mmabrouk mmabrouk closed this Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Frontend size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants