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AAwB,CAAC,CAAC,EAAE,gBAAgB,EAAE,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAG1F","sourcesContent":["/**\n * Durable Mission Graph — integration transactions (2.0.0).\n *\n * An integration transaction coordinates cross-objective, cross-repository\n * confirmation of work. It is fully transactional:\n *   PREPARED → CHECKPOINTED → VALIDATING → CONFIRMED\n *   any stage → ROLLED_BACK\n *\n * Checkpoints precede mutation and capture per-objective state. A localized\n * rollback restores only the checkpoints captured within this transaction;\n * independent completed work outside the transaction is preserved.\n */\n\nimport type {\n\tIntegrationTransactionState,\n\tMissionOperationResult,\n\tObjectiveCheckpoint,\n\tRepositoryLease,\n} from \"./types.js\";\n\nexport type IntegrationStage = IntegrationTransactionState[\"stage\"];\n\nexport interface IntegrationInput {\n\ttransactionId: string;\n\tobjectiveIds: string[];\n\trepositoryIds: string[];\n\t/** The leases held for the repositories involved. */\n\tleases: RepositoryLease[];\n\tnowMs?: number;\n}\n\nconst STAGE_ORDER: Record<IntegrationStage, number> = {\n\tPREPARED: 0,\n\tCHECKPOINTED: 1,\n\tVALIDATING: 2,\n\tCONFIRMED: 3,\n\tVOLATILE: 4,\n\tROLLED_BACK: 5,\n};\n\nexport function beginIntegrationTransaction(\n\tinput: IntegrationInput,\n): MissionOperationResult<IntegrationTransactionState> {\n\tconst nowMs = input.nowMs ?? Date.now();\n\tif (input.objectiveIds.length === 0 || input.repositoryIds.length === 0) {\n\t\treturn { ok: false, code: \"INTERNAL_ERROR\", error: \"integration requires objectives and repositories\" };\n\t}\n\t// Every repository involved must have a held lease.\n\tfor (const repo of input.repositoryIds) {\n\t\tconst held = input.leases.some((l) => l.repositoryId === repo);\n\t\tif (!held) {\n\t\t\treturn { ok: false, code: \"LEASE_NOT_HELD\", error: `no held lease for repository '${repo}'` };\n\t\t}\n\t}\n\treturn {\n\t\tok: true,\n\t\tvalue: {\n\t\t\ttransactionId: input.transactionId,\n\t\t\tobjectiveIds: [...input.objectiveIds],\n\t\t\trepositoryIds: [...input.repositoryIds],\n\t\t\tstage: \"PREPARED\",\n\t\t\tcheckpoints: [],\n\t\t\tcreatedAtMs: nowMs,\n\t\t\tupdatedAtMs: nowMs,\n\t\t},\n\t};\n}\n\n/**\n * Attach a checkpoint that precedes mutation for an objective. A checkpoint\n * must be present before mutation proceeds within an integration.\n */\nexport function addIntegrationCheckpoint(\n\ttx: IntegrationTransactionState,\n\tobjectiveId: string,\n\tstate: Record<string, unknown>,\n\tnowMs = Date.now(),\n): MissionOperationResult<IntegrationTransactionState> {\n\tif (!tx.objectiveIds.includes(objectiveId)) {\n\t\treturn { ok: false, code: \"NOT_FOUND\", error: `objective '${objectiveId}' not in integration` };\n\t}\n\tif (tx.stage === \"CONFIRMED\" || tx.stage === \"ROLLED_BACK\") {\n\t\treturn { ok: false, code: \"INTEGRATION_IN_PROGRESS\", error: `integration already ${tx.stage}` };\n\t}\n\tconst seq = tx.checkpoints.length + 1;\n\tconst cp: ObjectiveCheckpoint = { objectiveId, sequence: seq, state, createdAtMs: nowMs };\n\tconst checkpoints = [...tx.checkpoints, cp];\n\tlet stage: IntegrationStage = \"PREPARED\";\n\tif (checkpoints.length === tx.objectiveIds.length) stage = \"CHECKPOINTED\";\n\treturn {\n\t\tok: true,\n\t\tvalue: { ...tx, checkpoints, stage, updatedAtMs: nowMs },\n\t};\n}\n\nexport function markIntegrationValidating(\n\ttx: IntegrationTransactionState,\n\tnowMs = Date.now(),\n): MissionOperationResult<IntegrationTransactionState> {\n\tif (tx.stage !== \"CHECKPOINTED\") {\n\t\treturn { ok: false, code: \"INTEGRATION_IN_PROGRESS\", error: `expected CHECKPOINTED, got ${tx.stage}` };\n\t}\n\treturn { ok: true, value: { ...tx, stage: \"VALIDATING\", updatedAtMs: nowMs } };\n}\n\n/**\n * Confirm the integration. All checkpoints were validated and applied; the\n * transaction becomes CONFIRMED. Subsequent mutations require a new transaction.\n */\nexport function confirmIntegrationTransaction(\n\ttx: IntegrationTransactionState,\n\tnowMs = Date.now(),\n): MissionOperationResult<IntegrationTransactionState> {\n\tif (tx.stage !== \"VALIDATING\") {\n\t\treturn { ok: false, code: \"INTEGRATION_IN_PROGRESS\", error: `expected VALIDATING, got ${tx.stage}` };\n\t}\n\treturn { ok: true, value: { ...tx, stage: \"CONFIRMED\", updatedAtMs: nowMs } };\n}\n\n/**\n * Localized rollback: restore each objective's last checkpoint. Objectives not\n * part of this transaction are untouched, so independent completed work is\n * preserved. The transaction is marked ROLLED_BACK and is non-reusable.\n */\nexport function rollbackIntegrationTransaction(\n\ttx: IntegrationTransactionState,\n): MissionOperationResult<{ transactionId: string; restoredCheckpoints: string[]; preservedIndependentWork: boolean }> {\n\tconst restoredCheckpoints = tx.checkpoints.map((c) => c.objectiveId);\n\treturn {\n\t\tok: true,\n\t\tvalue: {\n\t\t\ttransactionId: tx.transactionId,\n\t\t\trestoredCheckpoints,\n\t\t\t// Rollback only touches objectives in this transaction; any objective\n\t\t\t// outside the transaction set is preserved by construction.\n\t\t\tpreservedIndependentWork: true,\n\t\t},\n\t\tcode: undefined,\n\t};\n}\n\n/** Ordering helper: whether stage `a` may transition to stage `b`. */\nexport function canTransitionIntegration(a: IntegrationStage, b: IntegrationStage): boolean {\n\tif (a === b) return true;\n\treturn STAGE_ORDER[b] === STAGE_ORDER[a] + 1 || b === \"ROLLED_BACK\";\n}\n"]}