{"record":{"id":"56cc68c61397c49c","repo":"mastra-ai/mastra","slug":"initial-code-initial-reason","errorCode":null,"errorMessage":"${initial.code}: ${initial.reason}","messagePattern":"\\$\\{initial\\.code\\}: \\$\\{initial\\.reason\\}","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/factory/src/rules/dispatcher.ts","lineNumber":786,"sourceCode":"    const board = decision.board;\n    let expectedRevision = result.item.revision;\n    if (materializedByDecision) {\n      const initial = await this.#transitionService.transition({\n        orgId: record.orgId,\n        factoryProjectId: record.factoryProjectId,\n        workItemId: result.item.id,\n        board,\n        stage: 'intake',\n        expectedRevision,\n        actor: deferredActor(record),\n        ingress: { type: 'rule', identity: `decision:${record.idempotencyKey}:${result.item.id}:initial-entry` },\n        cause: 'linked_item_materialized',\n        causalChain,\n        initialEntry: true,\n      });\n      if (initial.status === 'rejected') {\n        if (result.created) await this.#storage.delete({ orgId: record.orgId, id: result.item.id });\n        throw new Error(`${initial.code}: ${initial.reason}`);\n      }\n      expectedRevision = initial.revision;\n    }\n    if (decision.stage === 'intake') return;\n\n    const moved = await this.#transitionService.transition({\n      orgId: record.orgId,\n      factoryProjectId: record.factoryProjectId,\n      workItemId: result.item.id,\n      board,\n      stage: decision.stage,\n      expectedRevision,\n      actor: { type: 'system', id: 'factory-rule-dispatcher' },\n      ingress: { type: 'rule', identity: `decision:${record.idempotencyKey}:${result.item.id}:destination` },\n      cause: materializedByDecision ? 'linked_item_materialized' : 'linked_item_reconciled',\n      causalChain,\n    });\n    if (moved.status === 'rejected') throw new Error(`${moved.code}: ${moved.reason}`);","sourceCodeStart":768,"sourceCodeEnd":804,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/factory/src/rules/dispatcher.ts#L768-L804","documentation":"During deferred-decision dispatch, the dispatcher first materializes the linked item ('intake' stage). If that initial transition comes back with status 'rejected', it rolls back the just-created work item and throws a plain Error formatted as '<code>: <reason>'. This prevents the decision from proceeding when the rule engine explicitly rejects the initial stage.","triggerScenarios":"Processing a deferred factory decision whose initial intake transition is rejected by the rule evaluation — e.g. no rule matched, a guard/condition failed, or the rule outcome evaluated to rejected for the linked item.","commonSituations":"A rule version bump changed matching semantics so previously-accepted items are now rejected; a missing rule binding for the project; malformed decision payload that fails rule conditions.","solutions":["Parse the thrown message's leading code to identify which rejection reason the rules produced.","Inspect the rule definitions for the factory project and confirm one should match this decision.","Verify the rule version pinned on the factory matches the rules you intend to evaluate.","Re-run the decision after correcting rules/binding; note the created work item was deleted so no cleanup is needed."],"exampleFix":"// before: swallow the failure\ncatch (e) { /* ignore */ }\n// after\ntry { await dispatcher.dispatch(record); }\ncatch (e) {\n  const [code, ...reason] = e.message.split(': ');\n  console.warn(`decision rejected: ${code} — ${reason.join(': ')}`);\n}","handlingStrategy":"try-catch","validationCode":"const decision = await evaluateRulesFor(record);\nif (decision.status === 'rejected') skipDispatch(record, decision.reason);","typeGuard":"function isRejectionError(e: unknown): e is Error & { code: string; reason: string } {\n  return e instanceof Error && /^[a-z_]+: .+/.test(e.message);\n}","tryCatchPattern":"try {\n  await dispatcher.dispatch(record);\n} catch (e) {\n  const m = /^[^:]+: (.+)$/.exec(e.message);\n  if (m) log(`initial intake rejected: ${m[1]}`);\n  else throw e;\n}","preventionTips":["Pre-evaluate rules client-side before deferring decisions when possible.","Keep rule versions and bindings current for the project.","Alert on rejected intake codes instead of silent retry loops."],"tags":["rules-engine","dispatch","rejected-decision","factory"],"backgroundTag":"rule-rejected","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}