-
Notifications
You must be signed in to change notification settings - Fork 622
fix(frontend): scope agent triggers to the revision they were added on #5182
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
Closed
+291
−20
Closed
Changes from all commits
Commits
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
126 changes: 126 additions & 0 deletions
126
web/packages/agenta-entity-ui/src/DrillInView/SchemaControls/triggerScope.ts
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,126 @@ | ||
| import type {TriggerReference} from "@agenta/entities/gatewayTrigger" | ||
| import type {Workflow} from "@agenta/entities/workflow" | ||
|
|
||
| export const AGENT_TRIGGER_ORIGIN_META_KEY = "agenta_agent_trigger_origin" | ||
|
|
||
| export interface AgentTriggerOrigin { | ||
| revision_id: string | ||
| variant_id?: string | null | ||
| revision_version?: number | null | ||
| } | ||
|
|
||
| export interface AgentTriggerContext { | ||
| revisionId: string | null | ||
| variantId: string | null | ||
| revisionVersion: number | null | ||
| } | ||
|
|
||
| export interface AgentTriggerEntity { | ||
| data?: { | ||
| references?: Record<string, TriggerReference> | null | ||
| } | null | ||
| meta?: Record<string, unknown> | null | ||
| } | ||
|
|
||
| function finiteNumber(value: unknown): number | null { | ||
| const number = typeof value === "number" ? value : Number(value) | ||
| return Number.isFinite(number) ? number : null | ||
| } | ||
|
|
||
| export function getAgentTriggerContext( | ||
| entityId: string | null, | ||
| revision: Workflow | null | undefined, | ||
| ): AgentTriggerContext { | ||
| return { | ||
| revisionId: entityId, | ||
| variantId: revision?.workflow_variant_id ?? revision?.variant_id ?? null, | ||
| revisionVersion: finiteNumber(revision?.version), | ||
| } | ||
| } | ||
|
|
||
| export function buildAgentTriggerOrigin(context: AgentTriggerContext): AgentTriggerOrigin | null { | ||
| if (!context.revisionId) return null | ||
| return { | ||
| revision_id: context.revisionId, | ||
| variant_id: context.variantId, | ||
| revision_version: context.revisionVersion, | ||
| } | ||
| } | ||
|
|
||
| export function addAgentTriggerOriginMeta( | ||
| meta: Record<string, unknown> | null | undefined, | ||
| origin: AgentTriggerOrigin | null, | ||
| ): Record<string, unknown> | null { | ||
| if (!origin) return meta ?? null | ||
| return { | ||
| ...(meta ?? {}), | ||
| [AGENT_TRIGGER_ORIGIN_META_KEY]: origin, | ||
| } | ||
| } | ||
|
|
||
| function readAgentTriggerOrigin( | ||
| meta: Record<string, unknown> | null | undefined, | ||
| ): AgentTriggerOrigin | null { | ||
| const raw = meta?.[AGENT_TRIGGER_ORIGIN_META_KEY] | ||
| if (!raw || typeof raw !== "object") return null | ||
| const value = raw as Record<string, unknown> | ||
| const revisionId = typeof value.revision_id === "string" ? value.revision_id : null | ||
| if (!revisionId) return null | ||
| return { | ||
| revision_id: revisionId, | ||
| variant_id: typeof value.variant_id === "string" ? value.variant_id : null, | ||
| revision_version: finiteNumber(value.revision_version), | ||
| } | ||
| } | ||
|
|
||
| /** Whether any id in a trigger's `data.references` matches one of the agent's ids. */ | ||
| function referencesMatch( | ||
| references: Record<string, TriggerReference> | null | undefined, | ||
| agentIds: Set<string>, | ||
| ): boolean { | ||
| if (!references || agentIds.size === 0) return false | ||
| for (const ref of Object.values(references)) { | ||
| if (ref?.id && agentIds.has(ref.id)) return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| function revisionReferenceMatches( | ||
| references: Record<string, TriggerReference> | null | undefined, | ||
| revisionId: string | null, | ||
| ): boolean { | ||
| if (!references || !revisionId) return false | ||
| return ( | ||
| references.application_revision?.id === revisionId || | ||
| references.workflow_revision?.id === revisionId || | ||
| references.evaluator_revision?.id === revisionId | ||
| ) | ||
| } | ||
|
|
||
| // Origin-scoped triggers show on the revision they were created on and any later | ||
| // revision of the same variant; never on earlier revisions or sibling variants. | ||
| function originMatchesContext(origin: AgentTriggerOrigin, context: AgentTriggerContext): boolean { | ||
| if (origin.revision_id === context.revisionId) return true | ||
| if (!origin.variant_id || origin.variant_id !== context.variantId) return false | ||
| if (origin.revision_version == null || context.revisionVersion === null) return false | ||
| return context.revisionVersion >= origin.revision_version | ||
| } | ||
|
|
||
| export function agentTriggerMatchesContext( | ||
| trigger: AgentTriggerEntity, | ||
| context: AgentTriggerContext, | ||
| agentIds: Set<string>, | ||
| ): boolean { | ||
| const references = trigger.data?.references | ||
| const origin = readAgentTriggerOrigin(trigger.meta) | ||
|
|
||
| if (origin) { | ||
| return originMatchesContext(origin, context) | ||
| } | ||
|
|
||
| if (revisionReferenceMatches(references, context.revisionId)) { | ||
| return true | ||
| } | ||
|
|
||
| return referencesMatch(references, agentIds) | ||
| } | ||
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
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.
There was a problem hiding this comment.
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)returns0, so missing/nullrevision_versionis 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