Skip to content

Commit b32ba5f

Browse files
authored
fix(core,nestjs): restore scheduled workflow and prefix handling (#16637)
## Description This replaces #16113 with the two pieces that are still relevant on latest main. It loads the evented workflow registration from the public `@mastra/core/workflows` barrel so scheduled workflows created from that entry point promote correctly. It also makes the NestJS adapter treat requests outside the configured prefix as non-Mastra routes, including partial prefix matches like `/apiish/agents` when the prefix is `/api`. The query coercion changes from #16113 are intentionally not included because #16268 already fixed that behavior. ## Related issue(s) Fixes #16112 ## Type of change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Code refactoring - [x] Test update ## Checklist - [x] I have linked the related issue(s) in the description above - [x] I have made corresponding changes to the documentation (if applicable) - [x] I have added tests that prove my fix is effective or that my feature works - [ ] I have addressed all Coderabbit comments on this PR <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## ELI5 This PR fixes two bugs: scheduled workflows imported from the public workflows entry now register and promote correctly, and the NestJS adapter only treats requests that truly match the configured prefix as Mastra routes (so similar-but-different paths like `/apiish/agents` are ignored). ## Changes Overview ### Core workflow registration - Ensure evented workflow registration is triggered when consumers import from the public @mastra/core/workflows barrel by referencing the evented creator in packages/core/src/workflows/index.ts (imports createWorkflow from ./evented and keeps a live reference). - Restores promotion of scheduled workflows declared via the public barrel while avoiding ESM initialization cycles. - Tests updated: schedule-promotion.test.ts removed the transitive side-effect import and now relies on createWorkflow from ./index. ### NestJS route prefix enforcement - server-adapters/nestjs/src/utils/route-path.ts - getMastraRoutePath(path, prefix) now returns string | null and returns null when the path is outside the normalized prefix (only exact prefix or prefix + '/' are accepted). - server-adapters/nestjs/src/guards/mastra-route.guard.ts - If getMastraRoutePath returns null, the guard short-circuits (returns true) so Mastra auth/rate-limit logic is skipped for non-Mastra routes. - server-adapters/nestjs/src/controllers/mastra.controller.ts - handleRequest throws NotFoundException when getMastraRoutePath returns falsy; normal request handling proceeds for matched routes and double-slash paths are rejected. - server-adapters/nestjs/src/interceptors/tracing.interceptor.ts - Only attempts route matching when a routePath exists; otherwise matchResult is null to avoid matching on out-of-prefix requests. - Added tests: server-adapters/nestjs/src/__tests__/route-path.test.ts covering prefix normalization, exact-match-to-root mapping, and rejection of out-of-prefix / partial-prefix paths. ### Import refactoring to avoid barrel cycles - Multiple core files updated to import workflow primitives from specific modules (e.g., workflows/workflow, workflows/step, workflows/types) instead of the aggregated workflows barrel to avoid ESM import cycles while preserving runtime behavior and public APIs (several updates across agent, evals, loop, and workflow/evented files). ### Changesets and release notes - .changeset updates to publish patch releases: - @mastra/core: note that scheduled workflows created via the public workflows entry now correctly apply declared schedules. - @mastra/nestjs: note that configured route prefixes are enforced and partial prefix matches are ignored. ### Tests - schedule-promotion tests rely on the public barrel behavior (removed transitive import). - New Vitest suite for getMastraRoutePath validates strict prefix handling and normalization. ### Notes - Query coercion changes from the previously related PR are intentionally excluded because PR #16268 already addressed that behavior. - Classified as a bug fix; fixes issue #16112. <!-- review_stack_entry_start --> [![Review Change Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/mastra-ai/mastra/pull/16637) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 7bc78d9 commit b32ba5f

31 files changed

Lines changed: 109 additions & 61 deletions

.changeset/slow-icons-take.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@mastra/nestjs': patch
3+
---
4+
5+
Fixed NestJS route matching so configured prefixes are enforced and partial prefix matches are ignored. For example, a prefix of `/api` no longer matches `/apiish/agents`; only `/api` and `/api/*` are treated as Mastra routes.

.changeset/social-peaches-teach.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@mastra/core': patch
3+
---
4+
5+
Fixed scheduled workflows created from the public @mastra/core/workflows entry point so declared schedules are applied correctly.

packages/core/src/agent/agent.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ import { makeCoreTool, createMastraProxy, ensureToolProperties, deepMerge } from
7979
import type { ToolOptions } from '../utils';
8080
import type { MastraVoice } from '../voice';
8181
import { DefaultVoice } from '../voice';
82-
import { createWorkflow, createStep, isProcessor } from '../workflows';
83-
import type { AnyWorkflow, OutputWriter, Step, WorkflowResult } from '../workflows';
82+
import type { Step } from '../workflows/step';
83+
import type { OutputWriter, WorkflowResult } from '../workflows/types';
84+
import type { AnyWorkflow } from '../workflows/workflow';
85+
import { createWorkflow, createStep, isProcessor } from '../workflows/workflow';
8486
import type { AnyWorkspace } from '../workspace';
8587
import { createWorkspaceTools } from '../workspace';
8688
import { createSkillTools } from '../workspace/skills';

packages/core/src/agent/workflows/prepare-stream/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { InternalSpans } from '../../../observability';
99
import type { RequestContext } from '../../../request-context';
1010
import { MastraModelOutput } from '../../../stream';
1111
import type { ToolPayloadTransformPolicy } from '../../../tools';
12-
import { createWorkflow } from '../../../workflows';
12+
import { createWorkflow } from '../../../workflows/workflow';
1313
import type { Workspace } from '../../../workspace/workspace';
1414
import type { InnerAgentExecutionOptions } from '../../agent.types';
1515
import type { SaveQueueManager } from '../../save-queue';

packages/core/src/agent/workflows/prepare-stream/map-results-step.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { createObservabilityContext } from '../../../observability';
88
import type { Span, SpanType } from '../../../observability';
99
import { StructuredOutputProcessor } from '../../../processors';
1010
import type { RequestContext } from '../../../request-context';
11-
import type { Step } from '../../../workflows';
11+
import type { Step } from '../../../workflows/step';
1212
import type { InnerAgentExecutionOptions } from '../../agent.types';
1313
import type { SaveQueueManager } from '../../save-queue';
1414
import { getModelOutputForTripwire } from '../../trip-wire';

packages/core/src/agent/workflows/prepare-stream/prepare-memory-step.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { MemoryConfigInternal, StorageThreadType } from '../../../memory/ty
77
import { resolveObservabilityContext } from '../../../observability';
88
import type { ProcessorState } from '../../../processors/runner';
99
import type { RequestContext } from '../../../request-context';
10-
import { createStep } from '../../../workflows';
10+
import { createStep } from '../../../workflows/workflow';
1111
import type { InnerAgentExecutionOptions } from '../../agent.types';
1212
import { MessageList } from '../../message-list';
1313
import type { AgentMethodType } from '../../types';

packages/core/src/agent/workflows/prepare-stream/prepare-tools-step.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { StorageThreadType } from '../../../memory/types';
44
import type { Span, SpanType } from '../../../observability';
55
import { createObservabilityContext } from '../../../observability';
66
import type { RequestContext } from '../../../request-context';
7-
import { createStep } from '../../../workflows';
7+
import { createStep } from '../../../workflows/workflow';
88
import type { InnerAgentExecutionOptions } from '../../agent.types';
99
import type { AgentMethodType } from '../../types';
1010
import type { AgentCapabilities } from './schema';

packages/core/src/agent/workflows/prepare-stream/stream-step.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { resolveObservabilityContext } from '../../../observability';
99
import { RequestContext } from '../../../request-context';
1010
import { MastraModelOutput } from '../../../stream';
1111
import type { ToolPayloadTransformPolicy } from '../../../tools';
12-
import { createStep } from '../../../workflows';
12+
import { createStep } from '../../../workflows/workflow';
1313
import type { Workspace } from '../../../workspace/workspace';
1414
import type { SaveQueueManager } from '../../save-queue';
1515
import type { CreatedAgentSignal } from '../../signals';

packages/core/src/evals/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { executeWithContext } from '../observability/utils';
2828
import { RequestContext } from '../request-context';
2929
import type { PublicSchema } from '../schema';
3030
import { toStandardSchema, standardSchemaToJSONSchema } from '../schema';
31-
import { createWorkflow, createStep } from '../workflows';
31+
import { createWorkflow, createStep } from '../workflows/workflow';
3232
import type {
3333
ScoringSamplingConfig,
3434
ScorerRunInputForAgent,

packages/core/src/evals/run/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import type { ObservabilityContext } from '../../observability';
77
import { EntityType, resolveObservabilityContext } from '../../observability';
88
import type { RequestContext } from '../../request-context';
99
import type { MastraCompositeStore } from '../../storage';
10-
import { Workflow } from '../../workflows';
11-
import type { AnyWorkflow, WorkflowResult, WorkflowRunStartOptions, StepResult } from '../../workflows';
10+
import type { WorkflowResult, WorkflowRunStartOptions, StepResult } from '../../workflows/types';
11+
import type { AnyWorkflow } from '../../workflows/workflow';
12+
import { Workflow } from '../../workflows/workflow';
1213
import type { MastraScorer } from '../base';
1314
import { extractTrajectory, extractTrajectoryFromTrace, extractWorkflowTrajectory } from '../types';
1415
import { ScoreAccumulator } from './scorerAccumulator';

0 commit comments

Comments
 (0)