@mastra/pg 0.0.0-error-handler-fix-20251020202607 → 0.0.0-esbuild-bundle-worker-20260807173433

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/CHANGELOG.md +5579 -3
  2. package/LICENSE.md +15 -0
  3. package/README.md +72 -20
  4. package/dist/docs/SKILL.md +45 -0
  5. package/dist/docs/assets/SOURCE_MAP.json +6 -0
  6. package/dist/docs/references/docs-deployment-workers.md +137 -0
  7. package/dist/docs/references/docs-memory-semantic-recall.md +401 -0
  8. package/dist/docs/references/docs-memory-working-memory.md +431 -0
  9. package/dist/docs/references/docs-storage-overview.md +214 -0
  10. package/dist/docs/references/guides-rag-overview.md +74 -0
  11. package/dist/docs/references/guides-rag-retrieval.md +520 -0
  12. package/dist/docs/references/guides-rag-vector-databases.md +669 -0
  13. package/dist/docs/references/reference-memory-memory-class.md +148 -0
  14. package/dist/docs/references/reference-processors-message-history-processor.md +86 -0
  15. package/dist/docs/references/reference-processors-semantic-recall-processor.md +118 -0
  16. package/dist/docs/references/reference-processors-working-memory-processor.md +151 -0
  17. package/dist/docs/references/reference-rag-metadata-filters.md +218 -0
  18. package/dist/docs/references/reference-storage-composite.md +339 -0
  19. package/dist/docs/references/reference-storage-dynamodb.md +284 -0
  20. package/dist/docs/references/reference-storage-postgresql.md +531 -0
  21. package/dist/docs/references/reference-storage-retention.md +248 -0
  22. package/dist/docs/references/reference-tools-vector-query-tool.md +495 -0
  23. package/dist/docs/references/reference-vectors-pg.md +430 -0
  24. package/dist/index.cjs +20692 -4338
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.d.ts +1 -1
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +20640 -4310
  29. package/dist/index.js.map +1 -1
  30. package/dist/shared/config.d.ts +129 -31
  31. package/dist/shared/config.d.ts.map +1 -1
  32. package/dist/shared/pool-config.d.ts +32 -0
  33. package/dist/shared/pool-config.d.ts.map +1 -0
  34. package/dist/storage/client.d.ts +165 -0
  35. package/dist/storage/client.d.ts.map +1 -0
  36. package/dist/storage/db/constraint-utils.d.ts +16 -0
  37. package/dist/storage/db/constraint-utils.d.ts.map +1 -0
  38. package/dist/storage/db/index.d.ts +364 -0
  39. package/dist/storage/db/index.d.ts.map +1 -0
  40. package/dist/storage/db/pg-errors.d.ts +24 -0
  41. package/dist/storage/db/pg-errors.d.ts.map +1 -0
  42. package/dist/storage/db/schema-snapshot.d.ts +71 -0
  43. package/dist/storage/db/schema-snapshot.d.ts.map +1 -0
  44. package/dist/storage/domains/agents/index.d.ts +51 -0
  45. package/dist/storage/domains/agents/index.d.ts.map +1 -0
  46. package/dist/storage/domains/background-tasks/index.d.ts +43 -0
  47. package/dist/storage/domains/background-tasks/index.d.ts.map +1 -0
  48. package/dist/storage/domains/blobs/index.d.ts +18 -0
  49. package/dist/storage/domains/blobs/index.d.ts.map +1 -0
  50. package/dist/storage/domains/channels/index.d.ts +25 -0
  51. package/dist/storage/domains/channels/index.d.ts.map +1 -0
  52. package/dist/storage/domains/datasets/index.d.ts +47 -0
  53. package/dist/storage/domains/datasets/index.d.ts.map +1 -0
  54. package/dist/storage/domains/experiments/index.d.ts +70 -0
  55. package/dist/storage/domains/experiments/index.d.ts.map +1 -0
  56. package/dist/storage/domains/favorites/index.d.ts +19 -0
  57. package/dist/storage/domains/favorites/index.d.ts.map +1 -0
  58. package/dist/storage/domains/mcp-clients/index.d.ts +34 -0
  59. package/dist/storage/domains/mcp-clients/index.d.ts.map +1 -0
  60. package/dist/storage/domains/mcp-servers/index.d.ts +34 -0
  61. package/dist/storage/domains/mcp-servers/index.d.ts.map +1 -0
  62. package/dist/storage/domains/memory/index.d.ts +130 -56
  63. package/dist/storage/domains/memory/index.d.ts.map +1 -1
  64. package/dist/storage/domains/notifications/index.d.ts +43 -0
  65. package/dist/storage/domains/notifications/index.d.ts.map +1 -0
  66. package/dist/storage/domains/observability/index.d.ts +87 -39
  67. package/dist/storage/domains/observability/index.d.ts.map +1 -1
  68. package/dist/storage/domains/observability/v-next/ddl.d.ts +55 -0
  69. package/dist/storage/domains/observability/v-next/ddl.d.ts.map +1 -0
  70. package/dist/storage/domains/observability/v-next/discovery.d.ts +52 -0
  71. package/dist/storage/domains/observability/v-next/discovery.d.ts.map +1 -0
  72. package/dist/storage/domains/observability/v-next/feedback.d.ts +17 -0
  73. package/dist/storage/domains/observability/v-next/feedback.d.ts.map +1 -0
  74. package/dist/storage/domains/observability/v-next/filters.d.ts +29 -0
  75. package/dist/storage/domains/observability/v-next/filters.d.ts.map +1 -0
  76. package/dist/storage/domains/observability/v-next/helpers.d.ts +33 -0
  77. package/dist/storage/domains/observability/v-next/helpers.d.ts.map +1 -0
  78. package/dist/storage/domains/observability/v-next/index.d.ts +120 -0
  79. package/dist/storage/domains/observability/v-next/index.d.ts.map +1 -0
  80. package/dist/storage/domains/observability/v-next/listing.d.ts +53 -0
  81. package/dist/storage/domains/observability/v-next/listing.d.ts.map +1 -0
  82. package/dist/storage/domains/observability/v-next/logs.d.ts +8 -0
  83. package/dist/storage/domains/observability/v-next/logs.d.ts.map +1 -0
  84. package/dist/storage/domains/observability/v-next/metrics.d.ts +19 -0
  85. package/dist/storage/domains/observability/v-next/metrics.d.ts.map +1 -0
  86. package/dist/storage/domains/observability/v-next/olap.d.ts +93 -0
  87. package/dist/storage/domains/observability/v-next/olap.d.ts.map +1 -0
  88. package/dist/storage/domains/observability/v-next/partitioning.d.ts +76 -0
  89. package/dist/storage/domains/observability/v-next/partitioning.d.ts.map +1 -0
  90. package/dist/storage/domains/observability/v-next/pg-errors.d.ts +5 -0
  91. package/dist/storage/domains/observability/v-next/pg-errors.d.ts.map +1 -0
  92. package/dist/storage/domains/observability/v-next/polling.d.ts +28 -0
  93. package/dist/storage/domains/observability/v-next/polling.d.ts.map +1 -0
  94. package/dist/storage/domains/observability/v-next/retention.d.ts +64 -0
  95. package/dist/storage/domains/observability/v-next/retention.d.ts.map +1 -0
  96. package/dist/storage/domains/observability/v-next/scores.d.ts +17 -0
  97. package/dist/storage/domains/observability/v-next/scores.d.ts.map +1 -0
  98. package/dist/storage/domains/observability/v-next/signal-schema.d.ts +776 -0
  99. package/dist/storage/domains/observability/v-next/signal-schema.d.ts.map +1 -0
  100. package/dist/storage/domains/observability/v-next/sql.d.ts +31 -0
  101. package/dist/storage/domains/observability/v-next/sql.d.ts.map +1 -0
  102. package/dist/storage/domains/observability/v-next/traces.d.ts +16 -0
  103. package/dist/storage/domains/observability/v-next/traces.d.ts.map +1 -0
  104. package/dist/storage/domains/observability/v-next/tracing.d.ts +19 -0
  105. package/dist/storage/domains/observability/v-next/tracing.d.ts.map +1 -0
  106. package/dist/storage/domains/prompt-blocks/index.d.ts +42 -0
  107. package/dist/storage/domains/prompt-blocks/index.d.ts.map +1 -0
  108. package/dist/storage/domains/schedules/index.d.ts +52 -0
  109. package/dist/storage/domains/schedules/index.d.ts.map +1 -0
  110. package/dist/storage/domains/scorer-definitions/index.d.ts +42 -0
  111. package/dist/storage/domains/scorer-definitions/index.d.ts.map +1 -0
  112. package/dist/storage/domains/scores/index.d.ts +61 -33
  113. package/dist/storage/domains/scores/index.d.ts.map +1 -1
  114. package/dist/storage/domains/skills/index.d.ts +34 -0
  115. package/dist/storage/domains/skills/index.d.ts.map +1 -0
  116. package/dist/storage/domains/tool-provider-connections/index.d.ts +20 -0
  117. package/dist/storage/domains/tool-provider-connections/index.d.ts.map +1 -0
  118. package/dist/storage/domains/utils.d.ts +38 -5
  119. package/dist/storage/domains/utils.d.ts.map +1 -1
  120. package/dist/storage/domains/workflow-definitions/index.d.ts +20 -0
  121. package/dist/storage/domains/workflow-definitions/index.d.ts.map +1 -0
  122. package/dist/storage/domains/workflows/index.d.ts +70 -30
  123. package/dist/storage/domains/workflows/index.d.ts.map +1 -1
  124. package/dist/storage/domains/workspaces/index.d.ts +34 -0
  125. package/dist/storage/domains/workspaces/index.d.ts.map +1 -0
  126. package/dist/storage/factory-storage.d.ts +35 -0
  127. package/dist/storage/factory-storage.d.ts.map +1 -0
  128. package/dist/storage/index.d.ts +159 -253
  129. package/dist/storage/index.d.ts.map +1 -1
  130. package/dist/storage/performance-indexes/performance-test.d.ts +3 -1
  131. package/dist/storage/performance-indexes/performance-test.d.ts.map +1 -1
  132. package/dist/storage/retention.d.ts +76 -0
  133. package/dist/storage/retention.d.ts.map +1 -0
  134. package/dist/storage/test-utils.d.ts.map +1 -1
  135. package/dist/vector/index.d.ts +91 -11
  136. package/dist/vector/index.d.ts.map +1 -1
  137. package/dist/vector/sql-builder.d.ts +4 -0
  138. package/dist/vector/sql-builder.d.ts.map +1 -1
  139. package/dist/vector/types.d.ts +40 -0
  140. package/dist/vector/types.d.ts.map +1 -1
  141. package/package.json +31 -18
  142. package/dist/storage/domains/legacy-evals/index.d.ts +0 -20
  143. package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
  144. package/dist/storage/domains/operations/index.d.ts +0 -118
  145. package/dist/storage/domains/operations/index.d.ts.map +0 -1
  146. package/dist/storage/domains/traces/index.d.ts +0 -23
  147. package/dist/storage/domains/traces/index.d.ts.map +0 -1
package/LICENSE.md CHANGED
@@ -1,3 +1,18 @@
1
+ Portions of this software are licensed as follows:
2
+
3
+ - All content that resides under any directory named "ee/" within this
4
+ repository, including but not limited to:
5
+ - `packages/core/src/auth/ee/`
6
+ - `packages/server/src/server/auth/ee/`
7
+ is licensed under the license defined in `ee/LICENSE`.
8
+
9
+ - All third-party components incorporated into the Mastra Software are
10
+ licensed under the original license provided by the owner of the
11
+ applicable component.
12
+
13
+ - Content outside of the above-mentioned directories or restrictions is
14
+ available under the "Apache License 2.0" as defined below.
15
+
1
16
  # Apache License 2.0
2
17
 
3
18
  Copyright (c) 2025 Kepler Software, Inc.
package/README.md CHANGED
@@ -115,26 +115,32 @@ const store = new PostgresStore({
115
115
 
116
116
  // Create a thread
117
117
  await store.saveThread({
118
- id: 'thread-123',
119
- resourceId: 'resource-456',
120
- title: 'My Thread',
121
- metadata: { key: 'value' },
118
+ thread: {
119
+ id: 'thread-123',
120
+ resourceId: 'resource-456',
121
+ title: 'My Thread',
122
+ metadata: { key: 'value' },
123
+ createdAt: new Date(),
124
+ },
122
125
  });
123
126
 
124
127
  // Add messages to thread
125
- await store.saveMessages([
126
- {
127
- id: 'msg-789',
128
- threadId: 'thread-123',
129
- role: 'user',
130
- type: 'text',
131
- content: [{ type: 'text', text: 'Hello' }],
132
- },
133
- ]);
128
+ await store.saveMessages({
129
+ messages: [
130
+ {
131
+ id: 'msg-789',
132
+ threadId: 'thread-123',
133
+ role: 'user',
134
+ content: { content: 'Hello' },
135
+ resourceId: 'resource-456',
136
+ createdAt: new Date(),
137
+ },
138
+ ],
139
+ });
134
140
 
135
141
  // Query threads and messages
136
- const savedThread = await store.getThread('thread-123');
137
- const messages = await store.getMessages('thread-123');
142
+ const savedThread = await store.getThreadById({ threadId: 'thread-123' });
143
+ const messages = await store.listMessages({ threadId: 'thread-123' });
138
144
  ```
139
145
 
140
146
  ## Configuration
@@ -321,6 +327,9 @@ The system automatically detects configuration changes and only rebuilds indexes
321
327
  - `buildIndex({indexName, metric?, indexConfig?})`: Build or rebuild vector index
322
328
  - `upsert({indexName, vectors, metadata?, ids?})`: Add or update vectors
323
329
  - `query({indexName, queryVector, topK?, filter?, includeVector?, minScore?})`: Search for similar vectors
330
+ - `updateVector({ indexName, id?, filter?, update })`: Update a single vector by ID or metadata filter
331
+ - `deleteVector({ indexName, id })`: Delete a single vector by ID
332
+ - `deleteVectors({ indexName, ids?, filter? })`: Delete multiple vectors by IDs or metadata filter
324
333
  - `listIndexes()`: List all vector-enabled tables
325
334
  - `describeIndex(indexName)`: Get table statistics and index configuration
326
335
  - `deleteIndex(indexName)`: Delete a table
@@ -329,13 +338,56 @@ The system automatically detects configuration changes and only rebuilds indexes
329
338
 
330
339
  ## Storage Methods
331
340
 
332
- - `saveThread(thread)`: Create or update a thread
333
- - `getThread(threadId)`: Get a thread by ID
334
- - `deleteThread(threadId)`: Delete a thread and its messages
335
- - `saveMessages(messages)`: Save multiple messages in a transaction
336
- - `getMessages(threadId)`: Get all messages for a thread
341
+ ### Thread Operations
342
+
343
+ - `saveThread({ thread })`: Create or update a thread
344
+ - `getThreadById({ threadId })`: Get a thread by ID
345
+ - `updateThread({ id, title, metadata })`: Update thread title and/or metadata
346
+ - `deleteThread({ threadId })`: Delete a thread and its messages
347
+ - `listThreadsByResourceId({ resourceId, offset, limit, orderBy? })`: List paginated threads for a resource
348
+
349
+ ### Message Operations
350
+
351
+ - `saveMessages({ messages })`: Save multiple messages in a transaction
352
+ - `listMessages({ threadId, resourceId?, perPage?, page?, orderBy?, filter? })`: Get messages for a thread with pagination
353
+ - `listMessagesById({ messageIds })`: Get specific messages by their IDs
354
+ - `updateMessages({ messages })`: Update existing messages
337
355
  - `deleteMessages(messageIds)`: Delete specific messages
338
356
 
357
+ ### Resource Operations
358
+
359
+ - `getResourceById({ resourceId })`: Get a resource by ID
360
+ - `saveResource({ resource })`: Create or save a resource
361
+ - `updateResource({ resourceId, workingMemory })`: Update resource working memory
362
+
363
+ ### Workflow Operations
364
+
365
+ - `persistWorkflowSnapshot({ workflowName, runId, snapshot })`: Save workflow state
366
+ - `loadWorkflowSnapshot({ workflowName, runId })`: Load workflow state
367
+ - `listWorkflowRuns({ workflowName, pagination })`: List workflow runs with pagination
368
+ - `getWorkflowRunById({ workflowName, runId })`: Get a specific workflow run
369
+ - `updateWorkflowState({ workflowName, runId, state })`: Update workflow state
370
+ - `updateWorkflowResults({ workflowName, runId, results })`: Update workflow results
371
+
372
+ ### AI Observability Operations
373
+
374
+ - `createSpan(span)`: Create a single AI span
375
+ - `batchCreateSpans({ records })`: Create multiple AI spans
376
+ - `updateSpan({ traceId, spanId, updates })`: Update an AI span
377
+ - `batchUpdateSpans({ updates })`: Update multiple AI spans
378
+ - `getTrace(traceId)`: Get an trace by ID
379
+ - `getTracesPaginated({ ...filters, pagination })`: Get paginated traces with filtering
380
+ - `batchDeleteTraces({ traceIds })`: Delete multiple traces
381
+
382
+ ### Evaluation/Scoring Operations
383
+
384
+ - `getScoreById({ id })`: Get a score by ID
385
+ - `saveScore(score)`: Save an evaluation score
386
+ - `listScoresByScorerId({ scorerId, pagination })`: List scores by scorer with pagination
387
+ - `listScoresByRunId({ runId, pagination })`: List scores by run with pagination
388
+ - `listScoresByEntityId({ entityId, entityType, pagination })`: List scores by entity with pagination
389
+ - `listScoresBySpan({ traceId, spanId, pagination })`: List scores by span with pagination
390
+
339
391
  ## Index Management
340
392
 
341
393
  The PostgreSQL store provides comprehensive index management capabilities to optimize query performance.
@@ -0,0 +1,45 @@
1
+ ---
2
+ name: mastra-pg
3
+ description: Documentation for @mastra/pg. Use when working with @mastra/pg APIs, configuration, or implementation.
4
+ metadata:
5
+ package: "@mastra/pg"
6
+ version: "0.0.0-esbuild-bundle-worker-20260807173433"
7
+ ---
8
+
9
+ ## When to use
10
+
11
+ Use this skill whenever you are working with @mastra/pg to obtain the domain-specific knowledge.
12
+
13
+ ## How to use
14
+
15
+ Read the individual reference documents for detailed explanations and code examples.
16
+
17
+ ### Docs
18
+
19
+ - [Workers](references/docs-deployment-workers.md) - Separate background processing from the API layer by running workflow execution, cron schedules, and background tasks in dedicated worker processes.
20
+ - [Semantic recall](references/docs-memory-semantic-recall.md) - Learn how to use semantic recall in Mastra to retrieve relevant messages from past conversations using vector search and embeddings.
21
+ - [Working memory](references/docs-memory-working-memory.md) - Learn how to configure working memory in Mastra to store persistent user data, preferences.
22
+ - [Storage overview](references/docs-storage-overview.md) - Configure storage for Mastra to persist runtime state across agents, workflows, observability, evals, schedules, and memory.
23
+
24
+ ### Guides
25
+
26
+ - [RAG (Retrieval-Augmented Generation) in Mastra](references/guides-rag-overview.md) - Overview of Retrieval-Augmented Generation (RAG) in Mastra, detailing its capabilities for enhancing LLM outputs with relevant context.
27
+ - [Retrieval, semantic search, reranking](references/guides-rag-retrieval.md) - Guide on retrieval processes in Mastra's RAG systems, including semantic search, filtering, and re-ranking.
28
+ - [Storing embeddings in a vector database](references/guides-rag-vector-databases.md) - Guide on vector storage options in Mastra, including embedded and dedicated vector databases for similarity search.
29
+
30
+ ### Reference
31
+
32
+ - [Reference: Memory class](references/reference-memory-memory-class.md) - Documentation for the `Memory` class in Mastra, which provides a reliable system for managing conversation history and thread-based message storage.
33
+ - [Reference: MessageHistory](references/reference-processors-message-history-processor.md) - Documentation for the MessageHistory processor in Mastra, which handles retrieval and persistence of conversation history.
34
+ - [Reference: SemanticRecall](references/reference-processors-semantic-recall-processor.md) - Documentation for the SemanticRecall processor in Mastra, which enables semantic search over conversation history using vector embeddings.
35
+ - [Reference: WorkingMemory](references/reference-processors-working-memory-processor.md) - Documentation for the WorkingMemory processor in Mastra, which injects persistent user/context data as system instructions.
36
+ - [Reference: Metadata filters](references/reference-rag-metadata-filters.md) - Documentation for metadata filtering capabilities in Mastra, which allow for precise querying of vector search results across different vector stores.
37
+ - [Reference: Composite storage](references/reference-storage-composite.md) - Documentation for combining multiple storage backends in Mastra.
38
+ - [Reference: DynamoDB storage](references/reference-storage-dynamodb.md) - Documentation for the DynamoDB storage implementation in Mastra, using a single-table design with ElectroDB.
39
+ - [Reference: PostgreSQL storage](references/reference-storage-postgresql.md) - Documentation for the PostgreSQL storage implementation in Mastra.
40
+ - [Reference: Storage retention (prune)](references/reference-storage-retention.md) - API reference for retention policies and prune() on Mastra storage.
41
+ - [Reference: createVectorQueryTool()](references/reference-tools-vector-query-tool.md) - Documentation for the Vector Query Tool in Mastra, which facilitates semantic search over vector stores with filtering and reranking capabilities.
42
+ - [Reference: PG vector store](references/reference-vectors-pg.md) - Documentation for the PgVector class in Mastra, which provides vector search using PostgreSQL with pgvector extension.
43
+
44
+
45
+ Read [assets/SOURCE_MAP.json](assets/SOURCE_MAP.json) for source code references.
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": "0.0.0-esbuild-bundle-worker-20260807173433",
3
+ "package": "@mastra/pg",
4
+ "exports": {},
5
+ "modules": {}
6
+ }
@@ -0,0 +1,137 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # Workers
4
+
5
+ > **Beta:** This feature is in beta. The API is stable enough for production use, but some details may change. See [known limitations](#known-limitations) for current gaps.
6
+
7
+ Workers handle background processing outside the request-response cycle. Workflow step execution, cron-based scheduling, and long-running tool calls all run in workers, keeping the API responsive.
8
+
9
+ By default, workers run in the same process as the API. For production workloads, you can split them into separate processes or containers and scale each one independently.
10
+
11
+ ## When to use workers
12
+
13
+ Workers matter when any of these apply:
14
+
15
+ - Workflow steps take more than a few seconds and shouldn't block API responses
16
+ - You need event durability so in-flight work survives process restarts
17
+ - Different parts of the system need to scale independently (e.g., more orchestration capacity without more API instances)
18
+ - Background tool calls should run on dedicated compute
19
+
20
+ If your application handles light traffic and workflows complete fast, the default in-process setup works fine. Skip the worker infrastructure until you need it.
21
+
22
+ ## Worker types
23
+
24
+ Mastra has three built-in worker types. Each handles a specific kind of background processing.
25
+
26
+ ### Orchestration worker
27
+
28
+ Subscribes to workflow events on the [PubSub](https://mastra.ai/docs/server/pubsub) bus and executes workflow steps. Every `workflow.start`, step transition, and lifecycle event flows through this worker.
29
+
30
+ In a split deployment, the orchestration worker pulls events from a distributed PubSub backend and delegates step execution back to the API over HTTP. In-process, it runs steps directly.
31
+
32
+ The orchestration worker requires a PubSub backend that supports pull mode (e.g., [`RedisStreamsPubSub`](https://mastra.ai/reference/pubsub/redis-streams) or [`GoogleCloudPubSub`](https://mastra.ai/reference/pubsub/google-cloud-pubsub)).
33
+
34
+ ### Scheduler worker
35
+
36
+ Polls storage for due cron schedules and publishes `workflow.start` events. It's a producer only, meaning it creates work for the orchestration worker to pick up.
37
+
38
+ The scheduler reads declarative `schedule` fields from your workflow definitions automatically. See [Scheduled workflows](https://mastra.ai/docs/workflows/scheduled-workflows) for how to declare schedules.
39
+
40
+ **Don't run more than one scheduler instance.** Multiple schedulers polling the same storage would fire duplicate events for the same schedule.
41
+
42
+ ### Background task worker
43
+
44
+ Executes agent tool calls marked with `background: { enabled: true }`. When an agent invokes a background tool, the API dispatches the task to this worker instead of blocking the response stream.
45
+
46
+ The background task worker manages concurrency limits, task lifecycle, and result delivery through the PubSub bus.
47
+
48
+ ## How workers run
49
+
50
+ ### In-process mode (default)
51
+
52
+ With no configuration, Mastra creates and starts workers inside the API process. Events flow through an in-memory PubSub, and everything shares a single Node.js runtime.
53
+
54
+ ```typescript
55
+ import { Mastra } from '@mastra/core/mastra'
56
+
57
+ export const mastra = new Mastra({
58
+ // Workers run in-process by default.
59
+ // No pubsub or worker config needed.
60
+ })
61
+ ```
62
+
63
+ This setup needs no external infrastructure beyond your storage adapter. It doesn't survive process crashes, and you can't scale individual components.
64
+
65
+ ### Split processes
66
+
67
+ To run workers in their own processes, configure a distributed [PubSub](https://mastra.ai/docs/server/pubsub) backend and use the `MASTRA_WORKERS` environment variable to control which workers start in each process.
68
+
69
+ **Redis Streams + PostgreSQL**:
70
+
71
+ ```typescript
72
+ import { Mastra } from '@mastra/core/mastra'
73
+ import { RedisStreamsPubSub } from '@mastra/redis-streams'
74
+ import { PostgresStore } from '@mastra/pg'
75
+
76
+ export const mastra = new Mastra({
77
+ storage: new PostgresStore({
78
+ connectionString: process.env.DATABASE_URL!,
79
+ }),
80
+ pubsub: new RedisStreamsPubSub({
81
+ url: process.env.REDIS_URL!,
82
+ }),
83
+ })
84
+ ```
85
+
86
+ **Google Cloud Pub/Sub + LibSQL**:
87
+
88
+ ```typescript
89
+ import { Mastra } from '@mastra/core/mastra'
90
+ import { GoogleCloudPubSub } from '@mastra/google-cloud-pubsub'
91
+ import { LibSQLStore } from '@mastra/libsql'
92
+
93
+ export const mastra = new Mastra({
94
+ storage: new LibSQLStore({
95
+ url: process.env.DATABASE_URL!,
96
+ }),
97
+ pubsub: new GoogleCloudPubSub({
98
+ projectId: process.env.GCP_PROJECT_ID!,
99
+ }),
100
+ })
101
+ ```
102
+
103
+ Any [supported storage backend](https://mastra.ai/reference/workers/overview) works. Swap the storage adapter for your preferred database.
104
+
105
+ Run the same build artifact in multiple containers, each with a different [`MASTRA_WORKERS`](https://mastra.ai/reference/workers/overview) value to control which worker starts in each process.
106
+
107
+ Split deployments require a distributed PubSub backend ([`RedisStreamsPubSub`](https://mastra.ai/reference/pubsub/redis-streams) or [`GoogleCloudPubSub`](https://mastra.ai/reference/pubsub/google-cloud-pubsub)), a shared [storage backend](https://mastra.ai/reference/workers/overview), and network connectivity between the orchestration worker and the API.
108
+
109
+ The [worker deployment guide](https://mastra.ai/guides/deployment/mastra-workers) walks through this setup with Docker Compose and Kubernetes examples.
110
+
111
+ ## Network architecture
112
+
113
+ Workers are internal infrastructure. They're not exposed to end users and don't need their own subdomain, public URL, or inbound HTTP route.
114
+
115
+ In a split deployment:
116
+
117
+ - **The API server is the only public-facing process**: It serves all client HTTP requests, including REST endpoints, agent interactions, workflow triggers, and any custom routes.
118
+ - **Workers connect outbound only**: They pull events from the distributed PubSub backend and read/write to the shared storage database. They don't accept inbound traffic from clients.
119
+ - **The orchestration worker calls the API internally**: It sends step execution requests to the API over the container network using `MASTRA_STEP_EXECUTION_URL`. This is internal service-to-service communication, not a public endpoint.
120
+
121
+ All three worker types (orchestration, scheduler, background task) sit behind the API on a private network. They share access to the PubSub backend and storage database but never receive traffic directly from clients. If a worker-related feature needs an HTTP route (for example, token minting for a voice integration), that route runs on the API server, not on the worker process.
122
+
123
+ ## Known limitations
124
+
125
+ - **No dead-letter queue**: Failed events are nacked and retried, but there's no DLQ for events that fail after all retries.
126
+ - **No built-in health endpoint**: Workers don't expose an HTTP health check. Use container-level liveness probes or process monitoring.
127
+ - **Scheduler is single-instance**: Running multiple scheduler processes causes duplicate schedule fires.
128
+ - **Runs stuck in "running" after API crash**: If the API process crashes while executing a workflow step, the run remains in `running` status with no automatic retry. For [durable agents](https://mastra.ai/docs/long-running-agents/durable-agents), set `recovery.durableAgents` to `'auto'` in the Mastra config to automatically re-drive orphaned runs on server restart. See [Crash recovery](https://mastra.ai/docs/long-running-agents/durable-agents) for details.
129
+
130
+ ## Related
131
+
132
+ - [Worker deployment guide](https://mastra.ai/guides/deployment/mastra-workers): Docker Compose and Kubernetes examples
133
+ - [Worker authentication](https://mastra.ai/docs/server/auth/workers): Secure worker-to-API communication
134
+ - [Workers reference](https://mastra.ai/reference/workers/overview): Details about worker environment variables and types, with a list of supported storage backends
135
+ - [CLI reference](https://mastra.ai/reference/cli/mastra): `mastra worker build` and `mastra worker start`
136
+ - [PubSub](https://mastra.ai/docs/server/pubsub): Event delivery backends
137
+ - [Scheduled workflows](https://mastra.ai/docs/workflows/scheduled-workflows): Declare cron schedules on workflows