一键导入
saga-architecture
Saga coordination patterns for distributed transactions with compensating actions.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Saga coordination patterns for distributed transactions with compensating actions.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Optimize token usage and context window discipline. Reduce costs and improve response quality through smart context management.
Unified context lifecycle for FlowDeck sessions — ingest, filter, prune, protect, summarize, and persist with telemetry.
Predict affected files, modules, APIs, tests, and DB paths before changes. Returns an impact map for human review.
Map architecture, conventions, and file structure into `.codebase/`. Use when onboarding or before deep feature work.
Plan differently when the agent has low certainty — ask for clarification or narrow scope instead of pretending full understanding.
Protect critical context from pruning during compaction. Preserve active plans, safety files, pending operations, and user intent anchors.
| name | saga-architecture |
| description | Saga coordination patterns for distributed transactions with compensating actions. |
| origin | FlowDeck |
When coordinating distributed operations across multiple services or data stores where ACID transactions are not available and compensating actions are needed to maintain eventual consistency.
// Saga State
interface SagaState<T> {
id: string
currentStep: number
data: T
status: 'pending' | 'in_progress' | 'completed' | 'compensating' | 'failed'
}
// Orchestrating Saga - Central coordinator manages steps
class OrderProcessingSaga {
private readonly steps: SagaStep[]
constructor(
private readonly sagaOrchestrator: SagaOrchestrator,
private readonly inventoryService: InventoryService,
private readonly paymentService: PaymentService,
private readonly shippingService: ShippingService
) {
this.steps = [
{
name: 'reserve_inventory',
execute: (state) => this.inventoryService.reserve(state.orderId, state.items),
compensate: (state) => this.inventoryService.release(state.orderId, state.items)
},
{
name: 'process_payment',
execute: (state) => this.paymentService.charge(state.orderId, state.total),
compensate: (state) => this.paymentService.refund(state.orderId, state.total)
},
{
name: 'initiate_shipping',
execute: (state) => this.shippingService.createShipment(state.orderId),
compensate: (state) => this.shippingService.cancelShipment(state.shipmentId)
}
]
}
async execute(orderId: string): Promise<void> {
const state: SagaState<OrderSagaData> = {
id: generateId(),
currentStep: 0,
data: { orderId, items: [], total: 0 },
status: 'in_progress'
}
await this.sagaOrchestrator.start(state, this.steps)
}
}
// Choreography-based Saga - Events trigger reactions
class OrderCreatedHandler {
constructor(private readonly eventBus: EventBus) {}
async handle(event: OrderCreatedEvent): Promise<void> {
// Step 1: Reserve inventory
try {
await this.inventoryService.reserve(event.orderId, event.items)
this.eventBus.publish(new InventoryReservedEvent(event.orderId))
} catch (error) {
this.eventBus.publish(new InventoryReservationFailedEvent(event.orderId, error.message))
}
}
}
class InventoryReservedHandler {
async handle(event: InventoryReservedEvent): Promise<void> {
// Step 2: Process payment
try {
await this.paymentService.charge(event.orderId, event.total)
this.eventBus.publish(new PaymentProcessedEvent(event.orderId))
} catch (error) {
// Compensate by releasing inventory
this.eventBus.publish(new InventoryReleaseRequestedEvent(event.orderId))
}
}
}
// Idempotent Step Implementation
class PaymentService {
async charge(orderId: string, amount: Money): Promise<TransactionId> {
const existingTx = await this.transactionRepo.findByOrderId(orderId)
if (existingTx) {
return existingTx.id // Idempotent: return existing instead of charging again
}
const transaction = await this.paymentGateway.charge(amount)
await this.transactionRepo.save({ orderId, transaction })
return transaction.id
}
}