|
| 1 | +/** |
| 2 | + * @bitcode/asset-packs-pipelines-settle-reads |
| 3 | + * |
| 4 | + * Hierarchy: SettleReads + Simple + Pipeline |
| 5 | + * factorySettleReadsSimplePipeline → SettleReadsSimplePipeline |
| 6 | + * |
| 7 | + * After SynthesizeReadsSDIVFPipeline produces packs for a reader Need, this |
| 8 | + * Simple pipeline: |
| 9 | + * 1. validate — settlement readiness (preview, quote, payment posture) |
| 10 | + * 2. finalize-settlement — BTC finality, BTD mint/read/rights, conservation |
| 11 | + * 3. ship — open pull-request(s) against the repository supplied when reading |
| 12 | + * |
| 13 | + * Linear stages (no SDIVF DIV loop) — parity with QuickAgent vs PTRRAgent. |
| 14 | + */ |
| 15 | + |
| 16 | +import type { Executor, Execution } from '@bitcode/execution-generics'; |
| 17 | +import { |
| 18 | + factorySimplePipeline, |
| 19 | + type SimplePipeline, |
| 20 | +} from '@bitcode/generic-pipelines-simple'; |
| 21 | +import { |
| 22 | + buildAssetPackSettlementRightsDeliveryBoundary, |
| 23 | + persistAssetPackSettlementRightsDeliveryBoundary, |
| 24 | + storeCrossPhaseArtifact, |
| 25 | +} from '@bitcode/pipeline-asset-pack'; |
| 26 | + |
| 27 | +export type SettleReadsSimplePipeline = SimplePipeline<any, any>; |
| 28 | + |
| 29 | +export interface SettleReadsInput { |
| 30 | + /** Repository the reader provided (PR target). */ |
| 31 | + repository?: { |
| 32 | + url?: string | null; |
| 33 | + owner?: string | null; |
| 34 | + name?: string | null; |
| 35 | + branch?: string | null; |
| 36 | + }; |
| 37 | + /** Synthesize-reads output / selected packs ready to settle. */ |
| 38 | + synthesizedPacks?: unknown; |
| 39 | + assetPackPreviewBoundary?: unknown; |
| 40 | + shareToFeeQuote?: unknown; |
| 41 | + paymentObservation?: unknown; |
| 42 | + readerWalletId?: string | null; |
| 43 | + depositorWalletId?: string | null; |
| 44 | + [key: string]: unknown; |
| 45 | +} |
| 46 | + |
| 47 | +const validateStage: Executor<SettleReadsInput, SettleReadsInput> = async (input, execution) => { |
| 48 | + storeCrossPhaseArtifact(execution, 'settle-reads', 'stage', 'validate'); |
| 49 | + let boundary = (input as any)?.assetPackSettlementRightsDeliveryBoundary || null; |
| 50 | + if (!boundary) { |
| 51 | + try { |
| 52 | + boundary = buildAssetPackSettlementRightsDeliveryBoundary(input as any); |
| 53 | + } catch { |
| 54 | + boundary = { |
| 55 | + schema: 'bitcode.settle-reads.validation', |
| 56 | + state: 'blocked_until_worthy_preview', |
| 57 | + pipeline: 'settle-reads', |
| 58 | + }; |
| 59 | + } |
| 60 | + } |
| 61 | + persistAssetPackSettlementRightsDeliveryBoundary(execution, boundary as any); |
| 62 | + storeCrossPhaseArtifact(execution, 'settle-reads', 'validation', boundary as any); |
| 63 | + return { ...input, assetPackSettlementRightsDeliveryBoundary: boundary }; |
| 64 | +}; |
| 65 | + |
| 66 | +const finalizeSettlementStage: Executor<SettleReadsInput, SettleReadsInput> = async ( |
| 67 | + input, |
| 68 | + execution, |
| 69 | +) => { |
| 70 | + storeCrossPhaseArtifact(execution, 'settle-reads', 'stage', 'finalize-settlement'); |
| 71 | + // Boundary builder owns BTC observation, BTD mint/read/rights, conservation. |
| 72 | + let boundary = (input as any)?.assetPackSettlementRightsDeliveryBoundary || null; |
| 73 | + if (!boundary) { |
| 74 | + try { |
| 75 | + boundary = buildAssetPackSettlementRightsDeliveryBoundary(input as any); |
| 76 | + } catch { |
| 77 | + boundary = { |
| 78 | + schema: 'bitcode.settle-reads.settlement', |
| 79 | + state: 'blocked_until_payment_finality', |
| 80 | + pipeline: 'settle-reads', |
| 81 | + }; |
| 82 | + } |
| 83 | + } |
| 84 | + persistAssetPackSettlementRightsDeliveryBoundary(execution, boundary as any); |
| 85 | + storeCrossPhaseArtifact(execution, 'settle-reads', 'settlement', boundary as any); |
| 86 | + return { |
| 87 | + ...input, |
| 88 | + assetPackSettlementRightsDeliveryBoundary: boundary, |
| 89 | + settlementFinalized: true, |
| 90 | + }; |
| 91 | +}; |
| 92 | + |
| 93 | +const shipStage: Executor<SettleReadsInput, any> = async (input, execution) => { |
| 94 | + storeCrossPhaseArtifact(execution, 'settle-reads', 'stage', 'ship'); |
| 95 | + const repo = input.repository || {}; |
| 96 | + // Delivery agents under pipeline-asset-pack open PRs against the read repo. |
| 97 | + // Stage records intent + coordinates; host/agent wiring attaches real PR URLs. |
| 98 | + const shippable = { |
| 99 | + schema: 'bitcode.settle-reads.shippable', |
| 100 | + deliveryMechanism: 'pull_request', |
| 101 | + repository: { |
| 102 | + url: repo.url || null, |
| 103 | + owner: repo.owner || null, |
| 104 | + name: repo.name || null, |
| 105 | + branch: repo.branch || null, |
| 106 | + }, |
| 107 | + packs: input.synthesizedPacks ?? null, |
| 108 | + prUrl: (input as any).prUrl || null, |
| 109 | + status: (input as any).prUrl ? 'shipped' : 'ready_to_ship', |
| 110 | + }; |
| 111 | + storeCrossPhaseArtifact(execution, 'settle-reads', 'shippable', shippable as any); |
| 112 | + return { |
| 113 | + ...input, |
| 114 | + success: true, |
| 115 | + shippable, |
| 116 | + shippables: { pullRequest: { url: shippable.prUrl }, summary: 'settle-reads ship stage' }, |
| 117 | + deliveryMechanism: shippable, |
| 118 | + }; |
| 119 | +}; |
| 120 | + |
| 121 | +export function factorySettleReadsSimplePipeline( |
| 122 | + pipelineName: string = 'settle-reads', |
| 123 | +): SettleReadsSimplePipeline { |
| 124 | + return factorySimplePipeline(pipelineName, { |
| 125 | + stages: [ |
| 126 | + { id: 'validate', run: validateStage as any }, |
| 127 | + { id: 'finalize-settlement', run: finalizeSettlementStage as any }, |
| 128 | + { id: 'ship', run: shipStage as any }, |
| 129 | + ], |
| 130 | + initialize: async (execution) => { |
| 131 | + storeCrossPhaseArtifact(execution as any, 'pipeline', 'productPipeline', 'settle-reads'); |
| 132 | + storeCrossPhaseArtifact(execution as any, 'pipeline', 'pattern', 'Simple'); |
| 133 | + }, |
| 134 | + }); |
| 135 | +} |
| 136 | + |
| 137 | +export const settleReadsSimplePipeline: SettleReadsSimplePipeline = |
| 138 | + factorySettleReadsSimplePipeline(); |
| 139 | + |
| 140 | +export const runSettleReadsSimplePipeline = settleReadsSimplePipeline; |
0 commit comments