Skip to content

feat: run annotations#3241

Draft
myftija wants to merge 1 commit intomainfrom
run-annotations
Draft

feat: run annotations#3241
myftija wants to merge 1 commit intomainfrom
run-annotations

Conversation

@myftija
Copy link
Collaborator

@myftija myftija commented Mar 20, 2026

wip

@changeset-bot
Copy link

changeset-bot bot commented Mar 20, 2026

⚠️ No Changeset found

Latest commit: c039d65

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 20, 2026

Walkthrough

This pull request introduces trigger source and action tracking across the Trigger.dev platform. Changes include: adding an annotations JSON field to the TaskRun database model; defining new Zod schemas (TriggerSource, TriggerAction, RunAnnotations) in the core package; updating API endpoints (v1 and v2) to accept and parse an x-trigger-source header; modifying service layers (batch trigger, replay, schedule engine) to construct and propagate triggerSource and triggerAction metadata through the trigger flow; and updating the CLI API client to send source identification headers. The annotations are persisted on task runs and returned in API responses.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description contains only 'wip' and is missing all required template sections: testing details, changelog, screenshots, and the checklist. While it's marked as a draft, substantial context is absent. Complete the PR description by filling out all required template sections: testing steps, changelog summary, and checklist items. This is critical before merging.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: run annotations' clearly and concisely summarizes the main change—adding annotation support to task runs. It directly corresponds to the core changes across the codebase.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 run-annotations

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 and usage tips.

Tip

CodeRabbit can enforce grammar and style rules using `languagetool`.

Configure the reviews.tools.languagetool setting to enable/disable rules and categories. Refer to the LanguageTool Community to learn more.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

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)
packages/cli-v3/src/mcp/tools/tasks.ts (1)

101-118: ⚠️ Potential issue | 🟠 Major

Don't drop the selected environment from the trigger path.

This switch replaces the environment-aware MCP client flow with ctx.getCliApiClient(input.branch), but triggerTaskRun() only sends taskId plus the payload. input.environment no longer affects the trigger request, so the tool can now trigger a run in a different environment than the caller selected. Please keep this path environment-scoped or validate the task against input.environment before triggering.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/cli-v3/src/mcp/tools/tasks.ts` around lines 101 - 118, The current
flow uses ctx.getCliApiClient(input.branch) and then calls
cliApiClient.triggerTaskRun(input.taskId, ...) which ignores input.environment,
allowing triggers to run in the wrong environment; update the path to be
environment-scoped by either obtaining an environment-specific client (use the
environment-aware MCP client instead of ctx.getCliApiClient, e.g., call the
existing environment-scoped client factory with input.environment) or validate
the task's environment before calling triggerTaskRun, and/or include the
environment when invoking triggerTaskRun so input.environment is honored;
reference ctx.getCliApiClient, input.branch, input.environment, triggerTaskRun,
and input.taskId to locate where to change.
🧹 Nitpick comments (3)
apps/webapp/app/v3/runEngineHandlers.server.ts (1)

753-754: Consider propagating triggerSource from batch metadata.

The current logic infers triggerSource based on meta.parentRunId:

  • parentRunId present → "sdk"
  • Otherwise → "api"

This works for most cases, but batch items created from the dashboard would be misattributed as "api". Consider whether the batch metadata (meta) should carry the original triggerSource from the batch creation call site, allowing accurate attribution regardless of whether it's a nested trigger.

💡 Suggested approach
 {
   triggerVersion: meta.triggerVersion,
   traceContext: meta.traceContext as Record<string, unknown> | undefined,
   spanParentAsLink: meta.spanParentAsLink,
   batchId,
   batchIndex: itemIndex,
   realtimeStreamsVersion: meta.realtimeStreamsVersion,
   planType: meta.planType,
-  triggerSource: meta.parentRunId ? "sdk" : "api",
+  triggerSource: meta.triggerSource ?? (meta.parentRunId ? "sdk" : "api"),
   triggerAction: "trigger",
 },

This would require adding triggerSource to the batch metadata type and propagating it from batch creation endpoints.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/webapp/app/v3/runEngineHandlers.server.ts` around lines 753 - 754, The
current assignment of triggerSource using meta.parentRunId is too coarse; modify
the logic where triggerSource is set (the object with keys triggerSource and
triggerAction in run handling) to prefer a triggerSource value coming from
meta.triggerSource if present, falling back to meta.parentRunId ? "sdk" : "api"
only when meta.triggerSource is undefined; update the batch metadata type to
include triggerSource at creation points and ensure batch creation endpoints
populate meta.triggerSource so attribution is preserved across nested/batch
items (refer to meta.triggerSource, meta.parentRunId, and the triggerSource
assignment in the handler).
apps/webapp/app/presenters/v3/ApiRetrieveRunPresenter.server.ts (1)

471-471: Consider logging when annotation parsing fails.

Using safeParse(...).data silently discards validation errors. While this provides graceful degradation for API responses, consider adding a warning log when parsing fails to help detect data corruption or schema drift.

♻️ Optional: Add logging for parse failures
-    annotations: run.annotations ? RunAnnotations.safeParse(run.annotations).data : undefined,
+    annotations: run.annotations
+      ? (() => {
+          const parsed = RunAnnotations.safeParse(run.annotations);
+          if (!parsed.success) {
+            logger.warn("Failed to parse run annotations", {
+              runId: run.friendlyId,
+              error: parsed.error.message,
+            });
+          }
+          return parsed.data;
+        })()
+      : undefined,
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/webapp/app/presenters/v3/ApiRetrieveRunPresenter.server.ts` at line 471,
ApiRetrieveRunPresenter currently drops validation errors by using
RunAnnotations.safeParse(...).data; change to capture the parse result (e.g.,
const parsed = RunAnnotations.safeParse(run.annotations)) and only set
annotations to parsed.data when parsed.success is true, and when parsed.success
is false emit a warning log that includes the run identifier (e.g., run.id or
run.runId) and parsed.error to surface schema drift or corruption; use the
presenter/module logger (or console.warn if no logger exists) so failures are
recorded for investigation.
apps/webapp/app/routes/api.v1.tasks.$taskId.trigger.ts (1)

39-39: Constrain x-trigger-source to the supported values.

This header now flows into persisted run annotations, but the schema still accepts any string. That will let typos and arbitrary values leak into annotations through both the single-trigger and batch routes that reuse HeadersSchema. Please validate it against a bounded enum/union at the API boundary instead of raw z.string().

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/webapp/app/routes/api.v1.tasks`.$taskId.trigger.ts at line 39,
HeadersSchema currently allows any string for the "x-trigger-source" header,
letting typos/arbitrary values propagate to persisted run annotations; update
the HeadersSchema so "x-trigger-source" is validated against a bounded set of
supported values (e.g., replace z.string().nullish() with a
z.enum([...]).nullish() or z.union of literal()s listing the allowed trigger
source names) and ensure the same validated HeadersSchema is used by both the
single-trigger and batch routes that consume it.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/webapp/app/runEngine/services/batchTrigger.server.ts`:
- Around line 683-684: The pre-failed fallback path calls
TriggerFailedTaskService without the trigger attribution fields, causing
unannotated runs; update that fallback to compute the same triggerSource and
triggerAction values used in the normal TriggerTaskService.call() path (e.g.,
triggerSource: parentRunId ? "sdk" : options?.triggerSource ?? "api",
triggerAction: options?.triggerAction ?? "trigger") and pass them into the
TriggerFailedTaskService invocation so fallback-created runs include the same
attribution metadata.

---

Outside diff comments:
In `@packages/cli-v3/src/mcp/tools/tasks.ts`:
- Around line 101-118: The current flow uses ctx.getCliApiClient(input.branch)
and then calls cliApiClient.triggerTaskRun(input.taskId, ...) which ignores
input.environment, allowing triggers to run in the wrong environment; update the
path to be environment-scoped by either obtaining an environment-specific client
(use the environment-aware MCP client instead of ctx.getCliApiClient, e.g., call
the existing environment-scoped client factory with input.environment) or
validate the task's environment before calling triggerTaskRun, and/or include
the environment when invoking triggerTaskRun so input.environment is honored;
reference ctx.getCliApiClient, input.branch, input.environment, triggerTaskRun,
and input.taskId to locate where to change.

---

Nitpick comments:
In `@apps/webapp/app/presenters/v3/ApiRetrieveRunPresenter.server.ts`:
- Line 471: ApiRetrieveRunPresenter currently drops validation errors by using
RunAnnotations.safeParse(...).data; change to capture the parse result (e.g.,
const parsed = RunAnnotations.safeParse(run.annotations)) and only set
annotations to parsed.data when parsed.success is true, and when parsed.success
is false emit a warning log that includes the run identifier (e.g., run.id or
run.runId) and parsed.error to surface schema drift or corruption; use the
presenter/module logger (or console.warn if no logger exists) so failures are
recorded for investigation.

In `@apps/webapp/app/routes/api.v1.tasks`.$taskId.trigger.ts:
- Line 39: HeadersSchema currently allows any string for the "x-trigger-source"
header, letting typos/arbitrary values propagate to persisted run annotations;
update the HeadersSchema so "x-trigger-source" is validated against a bounded
set of supported values (e.g., replace z.string().nullish() with a
z.enum([...]).nullish() or z.union of literal()s listing the allowed trigger
source names) and ensure the same validated HeadersSchema is used by both the
single-trigger and batch routes that consume it.

In `@apps/webapp/app/v3/runEngineHandlers.server.ts`:
- Around line 753-754: The current assignment of triggerSource using
meta.parentRunId is too coarse; modify the logic where triggerSource is set (the
object with keys triggerSource and triggerAction in run handling) to prefer a
triggerSource value coming from meta.triggerSource if present, falling back to
meta.parentRunId ? "sdk" : "api" only when meta.triggerSource is undefined;
update the batch metadata type to include triggerSource at creation points and
ensure batch creation endpoints populate meta.triggerSource so attribution is
preserved across nested/batch items (refer to meta.triggerSource,
meta.parentRunId, and the triggerSource assignment in the handler).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: a02d1583-d9b9-4fe1-a917-545c50feb60c

📥 Commits

Reviewing files that changed from the base of the PR and between 35298ac and c039d65.

📒 Files selected for processing (23)
  • apps/webapp/app/presenters/v3/ApiRetrieveRunPresenter.server.ts
  • apps/webapp/app/routes/api.v1.runs.$runParam.replay.ts
  • apps/webapp/app/routes/api.v1.tasks.$taskId.trigger.ts
  • apps/webapp/app/routes/api.v1.tasks.batch.ts
  • apps/webapp/app/routes/api.v2.tasks.batch.ts
  • apps/webapp/app/routes/resources.taskruns.$runParam.replay.ts
  • apps/webapp/app/runEngine/services/batchTrigger.server.ts
  • apps/webapp/app/runEngine/services/triggerTask.server.ts
  • apps/webapp/app/v3/runEngineHandlers.server.ts
  • apps/webapp/app/v3/scheduleEngine.server.ts
  • apps/webapp/app/v3/services/batchTriggerV3.server.ts
  • apps/webapp/app/v3/services/bulk/BulkActionV2.server.ts
  • apps/webapp/app/v3/services/bulk/performBulkAction.server.ts
  • apps/webapp/app/v3/services/replayTaskRun.server.ts
  • apps/webapp/app/v3/services/testTask.server.ts
  • apps/webapp/app/v3/services/triggerTask.server.ts
  • internal-packages/database/prisma/schema.prisma
  • internal-packages/run-engine/src/engine/index.ts
  • internal-packages/run-engine/src/engine/types.ts
  • packages/cli-v3/src/apiClient.ts
  • packages/cli-v3/src/mcp/context.ts
  • packages/cli-v3/src/mcp/tools/tasks.ts
  • packages/core/src/v3/schemas/api.ts

Comment on lines +683 to +684
triggerSource: parentRunId ? "sdk" : options?.triggerSource ?? "api",
triggerAction: options?.triggerAction ?? "trigger",
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Batch fallback runs still lose trigger annotations.

These fields are only attached on the normal TriggerTaskService.call() path. The pre-failed fallback at Lines 567-580 still calls TriggerFailedTaskService without triggerSource / triggerAction, so any batch item that fails validation, entitlement, or queue checks will create an unannotated run. Please thread the same attribution fields through that fallback path too.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/webapp/app/runEngine/services/batchTrigger.server.ts` around lines 683 -
684, The pre-failed fallback path calls TriggerFailedTaskService without the
trigger attribution fields, causing unannotated runs; update that fallback to
compute the same triggerSource and triggerAction values used in the normal
TriggerTaskService.call() path (e.g., triggerSource: parentRunId ? "sdk" :
options?.triggerSource ?? "api", triggerAction: options?.triggerAction ??
"trigger") and pass them into the TriggerFailedTaskService invocation so
fallback-created runs include the same attribution metadata.

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