Skip to main content

Reasoning Engine Database

Overview

The Reasoning Engine uses a dedicated database schema (REASONING) to persist workflow execution state, step outputs, and error logs. This enables:

  • Workflow tracing — Track execution from start to finish
  • Step-by-step debugging — Inspect outputs from each pipeline stage
  • Error analysis — Structured logging for troubleshooting
  • Payload management — Efficient storage for large datasets
  • Audit trails — Complete execution history

Database Schema

Entity-Relationship Diagram


Table Descriptions

REASONING.log_ReasoningEngineTasks

Purpose: Parent table for reasoning workflow task runs. Each row represents one complete workflow execution.

Key Columns:

ColumnTypeDescription
IDINTAuto-incrementing primary key
TaskIdNVARCHAR(200)Unique task identifier (UUID), unique constraint
WorkflowRunIdNVARCHAR(200)Mastra workflow run ID
WorkflowNameNVARCHAR(510)Always 'reasoning-engine'
SessionIdNVARCHAR(200)User session context
UserIdNVARCHAR(200)Requesting user
StatusNVARCHAR(100)'pending', 'running', 'completed', 'failed'
MessageNVARCHAR(MAX)Human-readable status message
DetailsJsonNVARCHAR(MAX)Structured metadata (JSON)
InputDataRefNVARCHAR(200)Reference to input payload in data_ReasoningEnginePayloads
OutputDataRefNVARCHAR(200)Reference to output payload
StartedAtDATETIME2(7)Workflow start timestamp
CompletedAtDATETIME2(7)Workflow completion timestamp
DurationMsINTTotal execution time in milliseconds
CreatedAtDATETIME2(7)Record creation timestamp
ModifiedAtDATETIME2(7)Last update timestamp

Indexes:

  • Primary key on ID
  • Nonclustered index on TaskId
  • Nonclustered index on WorkflowRunId (filtered, where not null)
  • Nonclustered index on Status

DDL Location: log_ReasoningEngineTasks.sql (db/sqlproj/Mastra.Platform.ReasoningEngineDb/model/Tables/REASONING.log_ReasoningEngineTasks.sql)


REASONING.log_StepReasoningEngineTasks

Purpose: Per-step execution logs. The committed workflow has seven logged workflow steps: preflight (0) through validation (6).

Key Columns:

ColumnTypeDescription
IDINTAuto-incrementing primary key
TaskIdNVARCHAR(200)FK to parent task
WorkflowRunIdNVARCHAR(200)Mastra workflow run ID
StepNameNVARCHAR(510)Step identifier (e.g., 'temporal-analysis', 'pattern-recognition')
StepOrderINTWorkflow execution order (0 through 6 for committed steps)
StatusNVARCHAR(100)'pending', 'running', 'completed', 'failed', 'skipped'
MessageNVARCHAR(MAX)Step-specific status message
DetailsJsonNVARCHAR(MAX)Step metadata (JSON)
InputDataRefNVARCHAR(200)Reference to step input payload
OutputDataRefNVARCHAR(200)Reference to step output payload
AgentIdNVARCHAR(200)Registry ID of agent used (e.g., 'reasoning-engine-temporal-summary-agent')
AgentUsedBITWhether AI agent was invoked (default: 0)
ErrorCodeNVARCHAR(100)Error classification code
ErrorMessageNVARCHAR(MAX)Error details if step failed
StartedAtDATETIME2(7)Step start timestamp
CompletedAtDATETIME2(7)Step completion timestamp
DurationMsINTStep execution time in milliseconds
CreatedAtDATETIME2(7)Record creation timestamp
ModifiedAtDATETIME2(7)Last update timestamp

Indexes:

  • Primary key on ID
  • Nonclustered index on TaskId
  • Nonclustered index on WorkflowRunId (filtered, where not null)
  • Nonclustered index on StepName
  • Nonclustered index on Status
  • Nonclustered composite index on (TaskId, StepOrder)

DDL Location: log_StepReasoningEngineTasks.sql (db/sqlproj/Mastra.Platform.ReasoningEngineDb/model/Tables/REASONING.log_StepReasoningEngineTasks.sql)


REASONING.data_WorkflowStepOutputs

Purpose: Stores step outputs for downstream consumption. Enables steps to read prior outputs without passing through workflow state.

Key Columns:

ColumnTypeDescription
IDBIGINTAuto-incrementing primary key
TaskIdNVARCHAR(200)FK to parent task
StepNameNVARCHAR(510)Step that produced this output
StepOrderINTExecution order for reconstruction
OutputJsonNVARCHAR(MAX)Full step output (JSON)
AgentUsedBITWhether AI agent contributed (default: 0)
AgentIdNVARCHAR(200)Registry ID of agent used
DurationMsINTStep execution time
CreatedAtDATETIME2(7)Record creation timestamp

Constraints:

  • Unique constraint on (TaskId, StepName) — One output per step per task

Indexes:

  • Primary key on ID
  • Nonclustered index on TaskId

Usage Pattern:

// Steps load prior outputs from this table
const priorOutputs = await loadStepOutputs(taskId, ['temporal-analysis', 'pattern-recognition']);

The table also receives an assembled workflow-result row after validation. This row is not a separate Mastra workflow step; it is final result persistence through stepDataService.

DDL Location: data_WorkflowStepOutputs.sql (db/sqlproj/Mastra.Platform.ReasoningEngineDb/model/Tables/REASONING.data_WorkflowStepOutputs.sql)


REASONING.data_ReasoningEnginePayloads

Purpose: Large payload storage with content-addressable references. Prevents bloating of task/step tables.

Key Columns:

ColumnTypeDescription
IDINTAuto-incrementing primary key
DataIdNVARCHAR(200)Unique payload reference (UUID), unique constraint
KindNVARCHAR(100)Payload type: 'step-input', 'step-output', 'series', 'dataset', etc.
TaskIdNVARCHAR(200)FK to parent task
StepNameNVARCHAR(510)Producing step (optional)
PayloadJsonNVARCHAR(MAX)Full payload content (JSON)
ContentHashNVARCHAR(64)SHA256 hash for deduplication
SizeBytesINTPayload size in bytes
CreatedAtDATETIME2(7)Record creation timestamp
ExpiresAtDATETIME2(7)TTL for cleanup (nullable)

Indexes:

  • Primary key on ID
  • Unique nonclustered index on DataId
  • Nonclustered index on TaskId
  • Nonclustered index on Kind
  • Nonclustered index on ExpiresAt (filtered, where not null)
  • Nonclustered index on ContentHash (filtered, where not null)

Deduplication:

  • Uses ContentHash to detect identical payloads
  • Enables storage optimization for repeated datasets

DDL Location: data_ReasoningEnginePayloads.sql (db/sqlproj/Mastra.Platform.ReasoningEngineDb/model/Tables/REASONING.data_ReasoningEnginePayloads.sql)


REASONING.log_Error

Purpose: Structured error logging for debugging and alerting.

Key Columns:

ColumnTypeDescription
IDBIGINTAuto-incrementing primary key
TaskIdNVARCHAR(200)FK to parent task
WorkflowRunIdNVARCHAR(200)Mastra workflow run ID
StepNameNVARCHAR(510)Step where error occurred
StepOrderINTStep order for timeline reconstruction
ErrorCodeNVARCHAR(100)Error classification code
ErrorTypeNVARCHAR(200)Error category: 'VALIDATION', 'AGENT', 'CORRELATION', 'DATABASE', etc.
SeverityNVARCHAR(50)'ERROR', 'WARNING', 'INFO' (default: 'error')
MessageNVARCHAR(MAX)Error description
StackTraceNVARCHAR(MAX)Full stack trace for debugging
ContextJsonNVARCHAR(MAX)Contextual data (JSON)
AgentIdNVARCHAR(200)Agent registry ID if applicable
AgentNameNVARCHAR(200)Human-readable agent name
IsRecoverableBITWhether workflow can continue (default: 1)
RetryCountINTRetry attempts (default: 0)
OccurredAtDATETIME2(7)Error occurrence timestamp
CreatedAtDATETIME2(7)Record creation timestamp

Indexes:

  • Primary key on ID
  • Nonclustered index on TaskId
  • Nonclustered index on WorkflowRunId (filtered, where not null)
  • Nonclustered index on StepName (filtered, where not null)
  • Nonclustered index on ErrorType (filtered, where not null)
  • Nonclustered index on Severity
  • Nonclustered composite index on (TaskId, StepOrder)
  • Nonclustered index on OccurredAt DESC

DDL Location: log_Error.sql (db/sqlproj/Mastra.Platform.ReasoningEngineDb/model/Tables/REASONING.log_Error.sql)


Common Queries

Find recent failed tasks

SELECT TaskId, WorkflowRunId, Status, Message, DurationMs, CreatedAt
FROM REASONING.log_ReasoningEngineTasks
WHERE Status = 'failed'
AND CreatedAt >= DATEADD(day, -7, SYSUTCDATETIME())
ORDER BY CreatedAt DESC;

Get step timeline for a task

SELECT StepName, StepOrder, Status, AgentUsed, DurationMs, Message
FROM REASONING.log_StepReasoningEngineTasks
WHERE TaskId = @taskId
ORDER BY StepOrder;

Find tasks with specific error types

SELECT t.TaskId, t.Status, e.ErrorType, e.Message, e.StepName
FROM REASONING.log_ReasoningEngineTasks t
JOIN REASONING.log_Error e ON e.TaskId = t.TaskId
WHERE e.ErrorType = 'AGENT'
AND e.Severity = 'ERROR'
ORDER BY e.OccurredAt DESC;

Reconstruct full workflow result

SELECT StepName, OutputJson, AgentUsed, DurationMs
FROM REASONING.data_WorkflowStepOutputs
WHERE TaskId = @taskId
ORDER BY StepOrder;

Find workflows by user

SELECT TaskId, Status, Message, DurationMs, CreatedAt
FROM REASONING.log_ReasoningEngineTasks
WHERE UserId = @userId
AND CreatedAt >= DATEADD(day, -30, SYSUTCDATETIME())
ORDER BY CreatedAt DESC;

Analyze step performance

SELECT
StepName,
COUNT(*) AS ExecutionCount,
AVG(DurationMs) AS AvgDurationMs,
MIN(DurationMs) AS MinDurationMs,
MAX(DurationMs) AS MaxDurationMs,
SUM(CASE WHEN AgentUsed = 1 THEN 1 ELSE 0 END) AS AgentUsedCount
FROM REASONING.log_StepReasoningEngineTasks
WHERE CreatedAt >= DATEADD(day, -7, SYSUTCDATETIME())
AND Status = 'completed'
GROUP BY StepName
ORDER BY AvgDurationMs DESC;

Find large payloads

SELECT
DataId,
Kind,
TaskId,
SizeBytes,
SizeBytes / 1024 AS SizeKB,
CreatedAt
FROM REASONING.data_ReasoningEnginePayloads
WHERE SizeBytes > 1048576 -- > 1MB
ORDER BY SizeBytes DESC;

Error frequency by type

SELECT
ErrorType,
Severity,
COUNT(*) AS ErrorCount,
COUNT(DISTINCT TaskId) AS AffectedTasks
FROM REASONING.log_Error
WHERE OccurredAt >= DATEADD(day, -7, SYSUTCDATETIME())
GROUP BY ErrorType, Severity
ORDER BY ErrorCount DESC;

Database Maintenance

Payload Cleanup

Expired payloads can be cleaned up periodically:

-- Find expired payloads
SELECT DataId, Kind, TaskId, SizeBytes, ExpiresAt
FROM REASONING.data_ReasoningEnginePayloads
WHERE ExpiresAt IS NOT NULL
AND ExpiresAt < SYSUTCDATETIME();

-- Delete expired payloads (use with caution)
DELETE FROM REASONING.data_ReasoningEnginePayloads
WHERE ExpiresAt IS NOT NULL
AND ExpiresAt < SYSUTCDATETIME();

Archive Old Tasks

Archive completed tasks older than 90 days:

-- Identify tasks to archive
SELECT TaskId, Status, CompletedAt, DurationMs
FROM REASONING.log_ReasoningEngineTasks
WHERE Status IN ('completed', 'failed')
AND CompletedAt < DATEADD(day, -90, SYSUTCDATETIME());

-- Archive strategy: Export to external storage, then delete
-- (Implementation depends on archival system)

Index Maintenance

-- Rebuild fragmented indexes
ALTER INDEX ALL ON REASONING.log_ReasoningEngineTasks REBUILD;
ALTER INDEX ALL ON REASONING.log_StepReasoningEngineTasks REBUILD;
ALTER INDEX ALL ON REASONING.data_WorkflowStepOutputs REBUILD;
ALTER INDEX ALL ON REASONING.data_ReasoningEnginePayloads REBUILD;
ALTER INDEX ALL ON REASONING.log_Error REBUILD;

-- Update statistics
UPDATE STATISTICS REASONING.log_ReasoningEngineTasks;
UPDATE STATISTICS REASONING.log_StepReasoningEngineTasks;
UPDATE STATISTICS REASONING.data_WorkflowStepOutputs;
UPDATE STATISTICS REASONING.data_ReasoningEnginePayloads;
UPDATE STATISTICS REASONING.log_Error;

Configuration

Environment Variables

VariablePurposeDefault
AZURE_SQL_SERVERSQL Server hostname(required for DB features)
AZURE_SQL_DATABASEDatabase name(required for DB features)
REASONING_SQL_LOGGINGEnable/disable SQL loggingAuto (enabled if SQL configured)
REASONING_ASSUME_TABLES_EXISTSkip runtime table-existence checks unless set to falsetrue

Feature Flags

// SQL logging control
const SQL_LOGGING_ENABLED = process.env.REASONING_SQL_LOGGING
? process.env.REASONING_SQL_LOGGING !== 'false'
: HAS_SQL_CONFIG;

// Step output persistence service
const ASSUME_TABLES_EXIST = process.env.REASONING_ASSUME_TABLES_EXIST !== 'false';

Step output persistence is handled by stepDataService. The committed service does not read a REASONING_DB_STEP_DATA feature flag.


Troubleshooting

DB logging not working

Symptoms: No records in log_ReasoningEngineTasks or log_StepReasoningEngineTasks

Resolution:

  1. Verify environment variables are set:
    echo $AZURE_SQL_SERVER
    echo $AZURE_SQL_DATABASE
  2. Check SQL connectivity:
    curl -X POST "$MASTRA_BASE_URL/admin/tools/db-health-check" \
    -H "Content-Type: application/json" \
    -d '{"data":{"schema":"REASONING"}}'
  3. Ensure Managed Identity has permissions on the database

Step outputs not persisted

Symptoms: Empty results from data_WorkflowStepOutputs

Resolution:

  1. Verify SQL configuration is present:
    echo $AZURE_SQL_SERVER
    echo $AZURE_SQL_DATABASE
  2. Check that the REASONING.data_WorkflowStepOutputs table exists and is reachable.
  3. If working against a database that may not have the table yet, set REASONING_ASSUME_TABLES_EXIST=false so the service performs the table check path.
  4. Check for errors in log_Error and verify the step completed in log_StepReasoningEngineTasks.

Large payloads causing timeouts

Symptoms: Workflow timeouts, large DurationMs values

Resolution:

  1. Monitor payload sizes:
    SELECT AVG(SizeBytes), MAX(SizeBytes)
    FROM REASONING.data_ReasoningEnginePayloads;
  2. Consider data pre-aggregation in intake adapters
  3. Use stepPlan or step toggles to skip expensive analysis that is not needed for the request

Schema Deployment

DDL Location

All table DDL scripts are located in:

db/sqlproj/Mastra.Platform.ReasoningEngineDb/model/Tables/
TableDDL File
log_ReasoningEngineTasksREASONING.log_ReasoningEngineTasks.sql
log_StepReasoningEngineTasksREASONING.log_StepReasoningEngineTasks.sql
data_WorkflowStepOutputsREASONING.data_WorkflowStepOutputs.sql
data_ReasoningEnginePayloadsREASONING.data_ReasoningEnginePayloads.sql
log_ErrorREASONING.log_Error.sql

Deployment Process

See Database Deployment for DACPAC-based deployment instructions.


Next Steps