{"record":{"id":"c2b6f5fbefb6c1a6","repo":"mastra-ai/mastra","slug":"factory-work-item-not-found","errorCode":null,"errorMessage":"Factory work item not found.","messagePattern":"Factory work item not found\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/factory/src/rules/dispatcher.ts","lineNumber":810,"sourceCode":"    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}`);\n  }\n\n  async #requireItem(record: FactoryDeferredDecisionRecord) {\n    if (!record.workItemId) throw new Error('Factory decision is not linked to a work item.');\n    const item = await this.#storage.get({ orgId: record.orgId, id: record.workItemId });\n    if (!item) throw new Error('Factory work item not found.');\n    return item;\n  }\n\n  async #findBinding(\n    record: FactoryDeferredDecisionRecord,\n    role?: string,\n  ): Promise<FactoryRunBindingRecord | undefined> {\n    if (!record.workItemId) throw new Error('Factory decision is not linked to a work item.');\n    const bindings = await this.#storage.listRunBindings(record.orgId, record.factoryProjectId, record.workItemId);\n    return bindings\n      .filter(candidate => candidate.status === 'active' && (role === undefined || candidate.role === role))\n      .sort((left, right) => {\n        if (role === undefined && left.role === 'work' && right.role !== 'work') return -1;\n        if (role === undefined && right.role === 'work' && left.role !== 'work') return 1;\n        return right.createdAt.getTime() - left.createdAt.getTime() || left.id.localeCompare(right.id);\n      })[0];\n  }\n","sourceCodeStart":792,"sourceCodeEnd":828,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/factory/src/rules/dispatcher.ts#L792-L828","documentation":"#requireItem looks up the work item by record.workItemId in storage; if storage.get returns nothing, a plain Error 'Factory work item not found.' is thrown. The decision points at an item id that no longer exists, so dispatch cannot proceed.","triggerScenarios":"Dispatching a deferred decision whose workItemId references a deleted work item — e.g. the intake rejection rollback in the same dispatcher deleted the item, external reconciliation removed it, or manual storage cleanup purged it.","commonSituations":"Cascading deletion (item deleted after a rejected intake), retention/cleanup jobs removing items still referenced by deferred decisions, wrong orgId scoping in manual queries, storage restored from an older backup.","solutions":["Confirm the item id and orgId: fetch it directly via storage.get to distinguish deletion from scoping mistakes.","Delete or archive the orphaned decision record so the dispatcher stops retrying it.","Check cleanup/retention jobs for aggressive purging of items referenced by pending decisions.","Re-create the work item (re-materialize from the external source) and re-link the decision."],"exampleFix":"// before: blind retry loop\nsetInterval(() => dispatchAll(), 60_000);\n// after\nfor (const rec of await listDeferred()) {\n  if (!(await storage.get({ orgId: rec.orgId, id: rec.workItemId }))) await storage.deleteDecision(rec.id);\n  else await dispatch(rec);\n}","handlingStrategy":"validation","validationCode":"const item = await storage.get({ orgId: record.orgId, id: record.workItemId });\nif (!item) { await purgeDecision(record.id); return; }","typeGuard":"function itemExists(i: unknown): i is FactoryWorkItem { return typeof (i as any)?.id === 'string'; }","tryCatchPattern":"try {\n  await dispatcher.dispatch(record);\n} catch (e) {\n  if (e.message === 'Factory work item not found.') await storage.deleteDecision(record.id);\n  else throw e;\n}","preventionTips":["Check item existence before enqueueing deferred decisions for retry.","Align retention/cleanup jobs so referenced items are not purged while decisions are pending.","Scope queries with the correct orgId to avoid false 'not found' results."],"tags":["factory","storage","missing-record","dispatch"],"backgroundTag":"record-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}