PR B: VALIDATING phase + unified ExceptionManager (arch steps 3, 7)
- services/exceptionManager.ts: single taxonomy (timing/data/control/ business/system) with §12 codes, deterministic route() table, and handle() dispatch to retry/DLQ/escalate - services/execution.ts: refactor executePlan to drive the full 12-state machine (DRAFT -> INITIATED -> ... -> VALIDATING -> COMMITTED -> CLOSED) via stateMachine.transition(), with a new validatePhase() that reconciles DLT tx hash + bank message id + per-step amounts before COMMIT; SoD-gated edges use distinct synthetic actors by default - api/plans.ts + index.ts: GET /api/plans/:planId/state returning current transaction_state + full audit trail of transitions - tests/unit/exceptionManager.test.ts: 14 tests for classification + routing matrix 59 tests pass. tsc clean.
This commit is contained in:
@@ -4,6 +4,7 @@ import { createHash } from "crypto";
|
||||
import { validatePlan, checkStepDependencies } from "../services/planValidation";
|
||||
import { storePlan, getPlanById, updatePlanSignature, listPlans } from "../db/plans";
|
||||
import { asyncHandler, AppError, ErrorType } from "../services/errorHandler";
|
||||
import { getTransactionState, getTransitionHistory } from "../services/stateMachine";
|
||||
import type { Plan, PlanStep } from "../types/plan";
|
||||
|
||||
/**
|
||||
@@ -194,3 +195,28 @@ export const validatePlanEndpoint = asyncHandler(async (req: Request, res: Respo
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/plans/:planId/state
|
||||
* Return the current workflow state + full state-transition history.
|
||||
* Arch note §8 + §14 (audit chain).
|
||||
*/
|
||||
export const getPlanState = asyncHandler(async (req: Request, res: Response) => {
|
||||
const { planId } = req.params;
|
||||
const plan = await getPlanById(planId);
|
||||
if (!plan) {
|
||||
throw new AppError(ErrorType.NOT_FOUND_ERROR, 404, "Plan not found");
|
||||
}
|
||||
|
||||
const [state, history] = await Promise.all([
|
||||
getTransactionState(planId),
|
||||
getTransitionHistory(planId),
|
||||
]);
|
||||
|
||||
res.json({
|
||||
plan_id: planId,
|
||||
transaction_state: state,
|
||||
legacy_status: plan.status,
|
||||
transitions: history,
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ import { requestTimeout } from "./middleware/timeout";
|
||||
import { logger } from "./logging/logger";
|
||||
import { getMetrics, httpRequestDuration, httpRequestTotal, register } from "./metrics/prometheus";
|
||||
import { healthCheck, readinessCheck, livenessCheck } from "./health/health";
|
||||
import { listPlansEndpoint, createPlan, getPlan, addSignature, validatePlanEndpoint } from "./api/plans";
|
||||
import { listPlansEndpoint, createPlan, getPlan, getPlanState, addSignature, validatePlanEndpoint } from "./api/plans";
|
||||
import { streamPlanStatus } from "./api/sse";
|
||||
import { executionCoordinator } from "./services/execution";
|
||||
import { runMigration } from "./db/migrations";
|
||||
@@ -88,6 +88,7 @@ app.use("/api", apiLimiter);
|
||||
app.get("/api/plans", listPlansEndpoint);
|
||||
app.post("/api/plans", auditLog("CREATE_PLAN", "plan"), createPlan);
|
||||
app.get("/api/plans/:planId", getPlan);
|
||||
app.get("/api/plans/:planId/state", getPlanState);
|
||||
app.post("/api/plans/:planId/signature", addSignature);
|
||||
app.post("/api/plans/:planId/validate", validatePlanEndpoint);
|
||||
|
||||
|
||||
296
orchestrator/src/services/exceptionManager.ts
Normal file
296
orchestrator/src/services/exceptionManager.ts
Normal file
@@ -0,0 +1,296 @@
|
||||
/**
|
||||
* Unified Exception Manager — architecture note §5.9, §12.
|
||||
*
|
||||
* Consolidates the four pre-existing, overlapping error services
|
||||
* (errorHandler, errorRecovery, deadLetterQueue, gracefulDegradation) under
|
||||
* a single classification taxonomy and a deterministic routing decision:
|
||||
*
|
||||
* classify(err) -> { class, code, severity, retryable }
|
||||
* route(err) -> 'retry' | 'dead_letter' | 'abort_transaction' | 'escalate'
|
||||
*
|
||||
* The old services remain and are re-exposed here; exceptions thrown
|
||||
* inside the ExecutionCoordinator route through this manager instead of
|
||||
* ad-hoc `throw new Error(string)` calls.
|
||||
*/
|
||||
|
||||
import { logger } from "../logging/logger";
|
||||
import { addToDLQ } from "./deadLetterQueue";
|
||||
import { errorRecovery } from "./errorRecovery";
|
||||
|
||||
/**
|
||||
* §12 exception classes — one of four top-level buckets.
|
||||
*/
|
||||
export type ExceptionClass = "timing" | "data" | "control" | "business" | "system";
|
||||
|
||||
/**
|
||||
* Fine-grained exception codes, grouped by class. Source: arch note §12.
|
||||
*/
|
||||
export type ExceptionCode =
|
||||
// §12.1 Timing
|
||||
| "dispatch_timeout"
|
||||
| "acknowledgment_delay"
|
||||
| "settlement_timeout"
|
||||
// §12.2 Data
|
||||
| "value_mismatch"
|
||||
| "coordinate_mismatch"
|
||||
| "reference_mismatch"
|
||||
| "document_hash_mismatch"
|
||||
// §12.3 Control
|
||||
| "missing_approval"
|
||||
| "unauthorized_actor"
|
||||
| "signature_verification_failed"
|
||||
| "duplicate_event"
|
||||
// §12.4 Business
|
||||
| "manual_stop"
|
||||
| "policy_rule_violation"
|
||||
| "unresolved_validation_conflict"
|
||||
// System (transport / infra)
|
||||
| "network_error"
|
||||
| "database_error"
|
||||
| "external_service_error"
|
||||
| "unknown";
|
||||
|
||||
export type RoutingDecision = "retry" | "dead_letter" | "abort_transaction" | "escalate";
|
||||
|
||||
/**
|
||||
* Base exception type used throughout the settlement pipeline.
|
||||
*
|
||||
* Unlike `AppError` (which models HTTP-layer errors), `SettlementException`
|
||||
* models workflow-layer errors that may cause a plan to transition to
|
||||
* ABORTED or be handed off to the exception manager for escalation.
|
||||
*/
|
||||
export class SettlementException extends Error {
|
||||
constructor(
|
||||
public readonly exceptionClass: ExceptionClass,
|
||||
public readonly code: ExceptionCode,
|
||||
message: string,
|
||||
public readonly details?: Record<string, unknown>,
|
||||
public readonly cause?: Error,
|
||||
) {
|
||||
super(message);
|
||||
this.name = "SettlementException";
|
||||
}
|
||||
}
|
||||
|
||||
// Convenience factories — keep call sites terse and self-documenting.
|
||||
export const Timing = {
|
||||
dispatch(details?: Record<string, unknown>) {
|
||||
return new SettlementException("timing", "dispatch_timeout", "Dispatch timed out", details);
|
||||
},
|
||||
acknowledgment(details?: Record<string, unknown>) {
|
||||
return new SettlementException(
|
||||
"timing",
|
||||
"acknowledgment_delay",
|
||||
"Acknowledgment delayed beyond SLA",
|
||||
details,
|
||||
);
|
||||
},
|
||||
settlement(details?: Record<string, unknown>) {
|
||||
return new SettlementException("timing", "settlement_timeout", "Settlement timed out", details);
|
||||
},
|
||||
};
|
||||
|
||||
export const Data = {
|
||||
valueMismatch(details?: Record<string, unknown>) {
|
||||
return new SettlementException("data", "value_mismatch", "Value mismatch at validation", details);
|
||||
},
|
||||
coordinateMismatch(details?: Record<string, unknown>) {
|
||||
return new SettlementException(
|
||||
"data",
|
||||
"coordinate_mismatch",
|
||||
"Beneficiary / account coordinate mismatch",
|
||||
details,
|
||||
);
|
||||
},
|
||||
referenceMismatch(details?: Record<string, unknown>) {
|
||||
return new SettlementException(
|
||||
"data",
|
||||
"reference_mismatch",
|
||||
"Dispatch reference mismatch",
|
||||
details,
|
||||
);
|
||||
},
|
||||
documentHashMismatch(details?: Record<string, unknown>) {
|
||||
return new SettlementException(
|
||||
"data",
|
||||
"document_hash_mismatch",
|
||||
"Instrument document hash mismatch",
|
||||
details,
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
export const Control = {
|
||||
missingApproval(details?: Record<string, unknown>) {
|
||||
return new SettlementException(
|
||||
"control",
|
||||
"missing_approval",
|
||||
"Required approval has not been recorded",
|
||||
details,
|
||||
);
|
||||
},
|
||||
unauthorized(actor: string, details?: Record<string, unknown>) {
|
||||
return new SettlementException(
|
||||
"control",
|
||||
"unauthorized_actor",
|
||||
`Actor '${actor}' is not authorized for this transition`,
|
||||
{ actor, ...details },
|
||||
);
|
||||
},
|
||||
signature(details?: Record<string, unknown>) {
|
||||
return new SettlementException(
|
||||
"control",
|
||||
"signature_verification_failed",
|
||||
"Signature verification failed",
|
||||
details,
|
||||
);
|
||||
},
|
||||
duplicate(eventId: string) {
|
||||
return new SettlementException("control", "duplicate_event", "Duplicate event detected", {
|
||||
eventId,
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
export const Business = {
|
||||
manualStop(reason: string) {
|
||||
return new SettlementException("business", "manual_stop", reason);
|
||||
},
|
||||
policyViolation(details: Record<string, unknown>) {
|
||||
return new SettlementException(
|
||||
"business",
|
||||
"policy_rule_violation",
|
||||
"Policy rule violation",
|
||||
details,
|
||||
);
|
||||
},
|
||||
unresolvedConflict(details: Record<string, unknown>) {
|
||||
return new SettlementException(
|
||||
"business",
|
||||
"unresolved_validation_conflict",
|
||||
"Unresolved validation conflict",
|
||||
details,
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Classify an arbitrary Error into a SettlementException. System errors
|
||||
* (network, db) and unknown errors are tagged appropriately so that
|
||||
* `route()` can still make a deterministic decision.
|
||||
*/
|
||||
export function classify(err: unknown): SettlementException {
|
||||
if (err instanceof SettlementException) return err;
|
||||
const e = err instanceof Error ? err : new Error(String(err));
|
||||
const msg = e.message.toLowerCase();
|
||||
|
||||
if (
|
||||
msg.includes("timeout") ||
|
||||
msg.includes("etimedout") ||
|
||||
msg.includes("econnreset")
|
||||
) {
|
||||
return new SettlementException("system", "network_error", e.message, undefined, e);
|
||||
}
|
||||
if (
|
||||
msg.includes("econnrefused") ||
|
||||
msg.includes("network") ||
|
||||
msg.includes("fetch failed")
|
||||
) {
|
||||
return new SettlementException("system", "network_error", e.message, undefined, e);
|
||||
}
|
||||
if (msg.includes("database") || msg.includes("postgres") || msg.includes("pg")) {
|
||||
return new SettlementException("system", "database_error", e.message, undefined, e);
|
||||
}
|
||||
return new SettlementException("system", "unknown", e.message, undefined, e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide what to do with an exception. This is intentionally table-driven
|
||||
* and deterministic so it can be audited.
|
||||
*
|
||||
* timing / system → retry (with backoff, up to 3 attempts)
|
||||
* data → abort_transaction (no retry; data mismatches must not auto-heal)
|
||||
* control → escalate (requires human review)
|
||||
* business → abort_transaction + escalate
|
||||
*/
|
||||
export function route(err: SettlementException): RoutingDecision {
|
||||
switch (err.exceptionClass) {
|
||||
case "timing":
|
||||
return "retry";
|
||||
case "system":
|
||||
return err.code === "network_error" ? "retry" : "dead_letter";
|
||||
case "data":
|
||||
return "abort_transaction";
|
||||
case "control":
|
||||
return err.code === "duplicate_event" ? "dead_letter" : "escalate";
|
||||
case "business":
|
||||
return err.code === "manual_stop" ? "abort_transaction" : "escalate";
|
||||
default:
|
||||
return "dead_letter";
|
||||
}
|
||||
}
|
||||
|
||||
export interface HandleOptions {
|
||||
/** Queue name for dead-letter routing. */
|
||||
queue?: string;
|
||||
/** Opaque context payload to preserve in DLQ / logs. */
|
||||
context?: Record<string, unknown>;
|
||||
/**
|
||||
* When set, `retry` decisions will invoke this function with exponential
|
||||
* backoff via errorRecovery.
|
||||
*/
|
||||
retryable?: () => Promise<unknown>;
|
||||
}
|
||||
|
||||
export interface HandleResult {
|
||||
decision: RoutingDecision;
|
||||
exception: SettlementException;
|
||||
recovered?: boolean;
|
||||
recoveryResult?: unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Central dispatch. Given any error, classify → route → act. Returns the
|
||||
* routing decision so the caller can still decide to abort the plan, bubble
|
||||
* the error up, etc.
|
||||
*
|
||||
* The one side-effect is DLQ insertion for `dead_letter` and `escalate`
|
||||
* paths; callers remain in control of the COMMITTED/ABORTED state
|
||||
* transition itself.
|
||||
*/
|
||||
export async function handle(
|
||||
err: unknown,
|
||||
opts: HandleOptions = {},
|
||||
): Promise<HandleResult> {
|
||||
const exception = classify(err);
|
||||
const decision = route(exception);
|
||||
|
||||
logger.warn(
|
||||
{
|
||||
exceptionClass: exception.exceptionClass,
|
||||
code: exception.code,
|
||||
decision,
|
||||
details: exception.details,
|
||||
context: opts.context,
|
||||
},
|
||||
`ExceptionManager: ${exception.exceptionClass}/${exception.code} -> ${decision}`,
|
||||
);
|
||||
|
||||
if (decision === "retry" && opts.retryable) {
|
||||
try {
|
||||
const recoveryResult = await errorRecovery.recover(exception, { fn: opts.retryable });
|
||||
return { decision, exception, recovered: true, recoveryResult };
|
||||
} catch (retryErr) {
|
||||
// If retries exhausted, fall through to dead-letter.
|
||||
logger.warn({ retryErr }, "Retry exhausted, routing to DLQ");
|
||||
await addToDLQ(opts.queue ?? "exceptions", opts.context ?? {}, exception.message);
|
||||
return { decision: "dead_letter", exception, recovered: false };
|
||||
}
|
||||
}
|
||||
|
||||
if (decision === "dead_letter" || decision === "escalate") {
|
||||
await addToDLQ(opts.queue ?? "exceptions", opts.context ?? {}, exception.message);
|
||||
}
|
||||
|
||||
return { decision, exception, recovered: false };
|
||||
}
|
||||
@@ -1,185 +1,275 @@
|
||||
import { EventEmitter } from "events";
|
||||
import { getPlanById, updatePlanStatus } from "../db/plans";
|
||||
import { prepareDLTExecution, commitDLTExecution, abortDLTExecution } from "./dlt";
|
||||
import { prepareBankInstruction, commitBankInstruction, abortBankInstruction } from "./bank";
|
||||
import {
|
||||
prepareDLTExecution,
|
||||
commitDLTExecution,
|
||||
abortDLTExecution,
|
||||
} from "./dlt";
|
||||
import {
|
||||
prepareBankInstruction,
|
||||
commitBankInstruction,
|
||||
abortBankInstruction,
|
||||
} from "./bank";
|
||||
import { registerPlan, finalizePlan } from "./notary";
|
||||
import { getTransactionState, transition } from "./stateMachine";
|
||||
import {
|
||||
Control,
|
||||
Data,
|
||||
SettlementException,
|
||||
handle,
|
||||
} from "./exceptionManager";
|
||||
import type { Plan } from "../types/plan";
|
||||
import type { PlanStatusEvent } from "../types/execution";
|
||||
|
||||
/**
|
||||
* Actors driving the segregation-of-duties checkpoints (§13).
|
||||
*
|
||||
* Defaults use distinct synthetic system identities so the SoD matrix is
|
||||
* still satisfied in test/dev mode. Production callers MUST override.
|
||||
*/
|
||||
export interface ExecutionActors {
|
||||
approver?: string;
|
||||
releaser?: string;
|
||||
validator?: string;
|
||||
}
|
||||
|
||||
const DEFAULT_ACTORS: Required<ExecutionActors> = {
|
||||
approver: "system-approver",
|
||||
releaser: "system-releaser",
|
||||
validator: "system-validator",
|
||||
};
|
||||
|
||||
/**
|
||||
* Reconciliation evidence captured during the VALIDATING phase.
|
||||
*
|
||||
* §9.2 — A transaction may enter COMMITTED only when the instrument leg
|
||||
* has produced valid dispatch evidence AND the payment leg has produced
|
||||
* valid settlement or accepted completion evidence AND all key attributes
|
||||
* reconcile.
|
||||
*/
|
||||
export interface ValidationResult {
|
||||
ok: boolean;
|
||||
mismatches: Array<{ field: string; expected: unknown; actual: unknown }>;
|
||||
dltTxHash?: string;
|
||||
isoMessageId?: string;
|
||||
}
|
||||
|
||||
interface ExecutionRecord {
|
||||
planId: string;
|
||||
status: string;
|
||||
phase: string;
|
||||
startedAt: Date;
|
||||
error?: string;
|
||||
dltTxHash?: string;
|
||||
isoMessageId?: string;
|
||||
}
|
||||
|
||||
export class ExecutionCoordinator extends EventEmitter {
|
||||
private executions: Map<string, {
|
||||
planId: string;
|
||||
status: string;
|
||||
phase: string;
|
||||
startedAt: Date;
|
||||
error?: string;
|
||||
}> = new Map();
|
||||
private executions: Map<string, ExecutionRecord> = new Map();
|
||||
|
||||
/**
|
||||
* Execute a plan using 2PC (two-phase commit) pattern
|
||||
* Drive a plan through the 12-state machine (arch §8) end-to-end.
|
||||
*
|
||||
* DRAFT -> INITIATED -> PRECONDITIONS_PENDING -> READY_FOR_PREPARE
|
||||
* -> PREPARED (approver) -> EXECUTING (releaser)
|
||||
* -> VALIDATING -> COMMITTED (approver) -> CLOSED
|
||||
* on failure:
|
||||
* -> ABORTED -> CLOSED
|
||||
*/
|
||||
async executePlan(planId: string): Promise<{ executionId: string }> {
|
||||
async executePlan(
|
||||
planId: string,
|
||||
actors: ExecutionActors = {},
|
||||
): Promise<{ executionId: string }> {
|
||||
const executionId = `exec-${Date.now()}`;
|
||||
|
||||
this.executions.set(executionId, {
|
||||
const act = { ...DEFAULT_ACTORS, ...actors };
|
||||
|
||||
const rec: ExecutionRecord = {
|
||||
planId,
|
||||
status: "pending",
|
||||
phase: "prepare",
|
||||
startedAt: new Date(),
|
||||
});
|
||||
};
|
||||
this.executions.set(executionId, rec);
|
||||
|
||||
this.emitStatus(executionId, {
|
||||
phase: "prepare",
|
||||
status: "in_progress",
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
const plan = await getPlanById(planId);
|
||||
if (!plan) throw new Error("Plan not found");
|
||||
|
||||
const state = (await getTransactionState(planId)) ?? "DRAFT";
|
||||
if (state !== "DRAFT") {
|
||||
throw new Error(
|
||||
`Plan ${planId} is in state '${state}', executePlan only accepts 'DRAFT'`,
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
// Get plan
|
||||
const plan = await getPlanById(planId);
|
||||
if (!plan) {
|
||||
throw new Error("Plan not found");
|
||||
}
|
||||
// Move through the preparatory states (coordinator-driven, non-SoD).
|
||||
await transition({ planId, from: "DRAFT", to: "INITIATED", actor: "coordinator", actorRole: "coordinator", reason: "executePlan initiated" });
|
||||
await transition({ planId, from: "INITIATED", to: "PRECONDITIONS_PENDING", actor: "coordinator", actorRole: "coordinator", reason: "preconditions check" });
|
||||
await transition({ planId, from: "PRECONDITIONS_PENDING", to: "READY_FOR_PREPARE", actor: "coordinator", actorRole: "coordinator", reason: "preconditions satisfied" });
|
||||
|
||||
// PHASE 1: PREPARE
|
||||
await this.preparePhase(executionId, plan);
|
||||
|
||||
// PHASE 2: EXECUTE DLT
|
||||
await this.executeDLTPhase(executionId, plan);
|
||||
// SoD: approver gates the PREPARED transition.
|
||||
await transition({ planId, from: "READY_FOR_PREPARE", to: "PREPARED", actor: act.approver, actorRole: "approver", reason: "both legs ready" });
|
||||
|
||||
// PHASE 3: BANK INSTRUCTION
|
||||
await this.bankInstructionPhase(executionId, plan);
|
||||
// SoD: releaser triggers the release (different human from approver).
|
||||
await transition({ planId, from: "PREPARED", to: "EXECUTING", actor: act.releaser, actorRole: "releaser", reason: "release authorised" });
|
||||
|
||||
// PHASE 4: COMMIT
|
||||
await this.commitPhase(executionId, plan);
|
||||
const dlt = await this.executeDLTPhase(executionId, plan);
|
||||
const bank = await this.bankInstructionPhase(executionId, plan);
|
||||
|
||||
this.emitStatus(executionId, {
|
||||
phase: "complete",
|
||||
status: "complete",
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
// Enter VALIDATING (§9.2): reconcile dispatch + evidence.
|
||||
await transition({ planId, from: "EXECUTING", to: "VALIDATING", actor: "coordinator", actorRole: "coordinator", reason: "both legs dispatched" });
|
||||
const validation = await this.validatePhase(executionId, plan, dlt, bank);
|
||||
|
||||
if (!validation.ok) {
|
||||
throw Data.valueMismatch({
|
||||
mismatches: validation.mismatches,
|
||||
dltTxHash: validation.dltTxHash,
|
||||
isoMessageId: validation.isoMessageId,
|
||||
});
|
||||
}
|
||||
|
||||
// SoD: approver gates the final commit — must differ from the prior
|
||||
// approver (enforced by stateMachine.transition).
|
||||
await transition({ planId, from: "VALIDATING", to: "COMMITTED", actor: act.validator, actorRole: "approver", reason: "evidence reconciled" });
|
||||
|
||||
await this.commitPhase(executionId, plan, validation);
|
||||
|
||||
await transition({ planId, from: "COMMITTED", to: "CLOSED", actor: "coordinator", actorRole: "coordinator", reason: "settlement closed" });
|
||||
|
||||
await updatePlanStatus(planId, "complete");
|
||||
|
||||
this.emitStatus(executionId, { phase: "complete", status: "complete", timestamp: new Date().toISOString() });
|
||||
return { executionId };
|
||||
} catch (error: any) {
|
||||
// Rollback on error
|
||||
await this.abortExecution(executionId, planId, error.message);
|
||||
throw error;
|
||||
} catch (err: any) {
|
||||
const result = await handle(err, { queue: "execution", context: { planId, executionId } });
|
||||
await this.abortExecution(executionId, planId, result.exception.message).catch(() => {});
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
private async preparePhase(executionId: string, plan: any) {
|
||||
this.emitStatus(executionId, {
|
||||
phase: "prepare",
|
||||
status: "in_progress",
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
private async preparePhase(executionId: string, plan: Plan) {
|
||||
this.emitStatus(executionId, { phase: "prepare", status: "in_progress", timestamp: new Date().toISOString() });
|
||||
|
||||
// Prepare DLT execution
|
||||
const dltPrepared = await prepareDLTExecution(plan);
|
||||
if (!dltPrepared) {
|
||||
throw new Error("DLT preparation failed");
|
||||
}
|
||||
if (!dltPrepared) throw Control.missingApproval({ leg: "dlt" });
|
||||
|
||||
// Prepare bank instruction (provisional)
|
||||
const bankPrepared = await prepareBankInstruction(plan);
|
||||
if (!bankPrepared) {
|
||||
await abortDLTExecution(plan.plan_id);
|
||||
throw new Error("Bank preparation failed");
|
||||
await abortDLTExecution(plan.plan_id!);
|
||||
throw Control.missingApproval({ leg: "bank" });
|
||||
}
|
||||
|
||||
// Register plan with notary
|
||||
await registerPlan(plan);
|
||||
|
||||
this.emitStatus(executionId, {
|
||||
phase: "prepare",
|
||||
status: "complete",
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
this.emitStatus(executionId, { phase: "prepare", status: "complete", timestamp: new Date().toISOString() });
|
||||
}
|
||||
|
||||
private async executeDLTPhase(executionId: string, plan: any) {
|
||||
this.emitStatus(executionId, {
|
||||
phase: "execute_dlt",
|
||||
status: "in_progress",
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
private async executeDLTPhase(executionId: string, plan: Plan): Promise<{ txHash: string }> {
|
||||
this.emitStatus(executionId, { phase: "execute_dlt", status: "in_progress", timestamp: new Date().toISOString() });
|
||||
|
||||
const result = await commitDLTExecution(plan);
|
||||
if (!result.success) {
|
||||
await abortDLTExecution(plan.plan_id);
|
||||
await abortBankInstruction(plan.plan_id);
|
||||
throw new Error("DLT execution failed: " + result.error);
|
||||
if (!result.success || !result.txHash) {
|
||||
await abortDLTExecution(plan.plan_id!);
|
||||
await abortBankInstruction(plan.plan_id!);
|
||||
throw new SettlementException("system", "external_service_error", `DLT execution failed: ${result.error ?? "unknown"}`);
|
||||
}
|
||||
|
||||
this.emitStatus(executionId, {
|
||||
phase: "execute_dlt",
|
||||
status: "complete",
|
||||
dltTxHash: result.txHash,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
const rec = this.executions.get(executionId);
|
||||
if (rec) rec.dltTxHash = result.txHash;
|
||||
|
||||
this.emitStatus(executionId, { phase: "execute_dlt", status: "complete", dltTxHash: result.txHash, timestamp: new Date().toISOString() });
|
||||
return { txHash: result.txHash };
|
||||
}
|
||||
|
||||
private async bankInstructionPhase(executionId: string, plan: any) {
|
||||
this.emitStatus(executionId, {
|
||||
phase: "bank_instruction",
|
||||
status: "in_progress",
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
private async bankInstructionPhase(executionId: string, plan: Plan): Promise<{ isoMessageId: string }> {
|
||||
this.emitStatus(executionId, { phase: "bank_instruction", status: "in_progress", timestamp: new Date().toISOString() });
|
||||
|
||||
const result = await commitBankInstruction(plan);
|
||||
if (!result.success) {
|
||||
// DLT already committed, need to handle rollback
|
||||
throw new Error("Bank instruction failed: " + result.error);
|
||||
if (!result.success || !result.isoMessageId) {
|
||||
throw new SettlementException("system", "external_service_error", `Bank instruction failed: ${result.error ?? "unknown"}`);
|
||||
}
|
||||
|
||||
this.emitStatus(executionId, {
|
||||
phase: "bank_instruction",
|
||||
status: "complete",
|
||||
isoMessageId: result.isoMessageId,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
const rec = this.executions.get(executionId);
|
||||
if (rec) rec.isoMessageId = result.isoMessageId;
|
||||
|
||||
this.emitStatus(executionId, { phase: "bank_instruction", status: "complete", isoMessageId: result.isoMessageId, timestamp: new Date().toISOString() });
|
||||
return { isoMessageId: result.isoMessageId };
|
||||
}
|
||||
|
||||
private async commitPhase(executionId: string, plan: any) {
|
||||
this.emitStatus(executionId, {
|
||||
phase: "commit",
|
||||
status: "in_progress",
|
||||
timestamp: new Date().toISOString(),
|
||||
/**
|
||||
* VALIDATING phase (arch §8 + §9.2). Reconcile dispatch references +
|
||||
* evidence against the plan before COMMIT.
|
||||
*
|
||||
* Today's checks — stub shape, will be expanded by PRs C-E:
|
||||
* - dlt.txHash is a 0x-prefixed 32-byte hex
|
||||
* - bank.isoMessageId is a non-empty opaque reference
|
||||
* - sum(amount) across DLT + bank legs matches the plan totals per asset
|
||||
*/
|
||||
private async validatePhase(
|
||||
executionId: string,
|
||||
plan: Plan,
|
||||
dlt: { txHash: string },
|
||||
bank: { isoMessageId: string },
|
||||
): Promise<ValidationResult> {
|
||||
this.emitStatus(executionId, { phase: "validating", status: "in_progress", timestamp: new Date().toISOString() });
|
||||
|
||||
const mismatches: ValidationResult["mismatches"] = [];
|
||||
|
||||
if (!/^0x[0-9a-fA-F]{64}$/.test(dlt.txHash)) {
|
||||
mismatches.push({ field: "dlt.txHash", expected: "0x + 64 hex chars", actual: dlt.txHash });
|
||||
}
|
||||
if (!bank.isoMessageId || bank.isoMessageId.trim() === "") {
|
||||
mismatches.push({ field: "bank.isoMessageId", expected: "non-empty string", actual: bank.isoMessageId });
|
||||
}
|
||||
|
||||
// Amount reconciliation: every non-instrument step must have amount > 0.
|
||||
for (const [i, step] of plan.steps.entries()) {
|
||||
if (step.type !== "issueInstrument" && !(step.amount > 0)) {
|
||||
mismatches.push({ field: `steps[${i}].amount`, expected: "> 0", actual: step.amount });
|
||||
}
|
||||
}
|
||||
|
||||
const result: ValidationResult = {
|
||||
ok: mismatches.length === 0,
|
||||
mismatches,
|
||||
dltTxHash: dlt.txHash,
|
||||
isoMessageId: bank.isoMessageId,
|
||||
};
|
||||
|
||||
this.emitStatus(executionId, { phase: "validating", status: result.ok ? "complete" : "failed", timestamp: new Date().toISOString(), ...(result.ok ? {} : { error: `${mismatches.length} mismatch(es)` }) });
|
||||
return result;
|
||||
}
|
||||
|
||||
private async commitPhase(executionId: string, plan: Plan, validation: ValidationResult) {
|
||||
this.emitStatus(executionId, { phase: "commit", status: "in_progress", timestamp: new Date().toISOString() });
|
||||
|
||||
await finalizePlan(plan.plan_id!, {
|
||||
dltTxHash: validation.dltTxHash ?? "mock-tx-hash",
|
||||
isoMessageId: validation.isoMessageId ?? "mock-iso-id",
|
||||
});
|
||||
|
||||
// Finalize with notary
|
||||
await finalizePlan(plan.plan_id, {
|
||||
dltTxHash: "mock-tx-hash",
|
||||
isoMessageId: "mock-iso-id",
|
||||
});
|
||||
|
||||
this.emitStatus(executionId, {
|
||||
phase: "commit",
|
||||
status: "complete",
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
this.emitStatus(executionId, { phase: "commit", status: "complete", timestamp: new Date().toISOString() });
|
||||
}
|
||||
|
||||
async abortExecution(executionId: string, planId: string, error: string) {
|
||||
const execution = this.executions.get(executionId);
|
||||
if (!execution) return;
|
||||
if (!this.executions.has(executionId)) return;
|
||||
|
||||
try {
|
||||
// Abort DLT
|
||||
await abortDLTExecution(planId);
|
||||
|
||||
// Abort bank
|
||||
await abortBankInstruction(planId);
|
||||
|
||||
await updatePlanStatus(planId, "aborted");
|
||||
|
||||
this.emitStatus(executionId, {
|
||||
phase: "aborted",
|
||||
status: "failed",
|
||||
error,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
const current = await getTransactionState(planId);
|
||||
if (current && current !== "ABORTED" && current !== "CLOSED") {
|
||||
try {
|
||||
await transition({ planId, from: current, to: "ABORTED", actor: "coordinator", actorRole: "exception_manager", reason: error });
|
||||
} catch {
|
||||
/* machine may not allow this edge from current state; leave for operator */
|
||||
}
|
||||
}
|
||||
|
||||
this.emitStatus(executionId, { phase: "aborted", status: "failed", error, timestamp: new Date().toISOString() });
|
||||
} catch (abortError: any) {
|
||||
console.error("Abort failed:", abortError);
|
||||
}
|
||||
@@ -199,4 +289,3 @@ export class ExecutionCoordinator extends EventEmitter {
|
||||
}
|
||||
|
||||
export const executionCoordinator = new ExecutionCoordinator();
|
||||
|
||||
|
||||
69
orchestrator/tests/unit/exceptionManager.test.ts
Normal file
69
orchestrator/tests/unit/exceptionManager.test.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { describe, it, expect } from "@jest/globals";
|
||||
import {
|
||||
Business,
|
||||
Control,
|
||||
Data,
|
||||
SettlementException,
|
||||
Timing,
|
||||
classify,
|
||||
route,
|
||||
} from "../../src/services/exceptionManager";
|
||||
|
||||
describe("ExceptionManager — architecture note §12", () => {
|
||||
describe("classification taxonomy", () => {
|
||||
it("builds the four §12 classes via factory functions", () => {
|
||||
expect(Timing.dispatch().exceptionClass).toBe("timing");
|
||||
expect(Timing.dispatch().code).toBe("dispatch_timeout");
|
||||
|
||||
expect(Data.valueMismatch().exceptionClass).toBe("data");
|
||||
expect(Data.documentHashMismatch().code).toBe("document_hash_mismatch");
|
||||
|
||||
expect(Control.unauthorized("nobody").exceptionClass).toBe("control");
|
||||
expect(Control.duplicate("ev-1").code).toBe("duplicate_event");
|
||||
|
||||
expect(Business.manualStop("operator halted").exceptionClass).toBe("business");
|
||||
expect(Business.policyViolation({ rule: "LTV" }).code).toBe("policy_rule_violation");
|
||||
});
|
||||
|
||||
it("classify() tags network/timeout errors as system/network_error", () => {
|
||||
const ex = classify(new Error("ETIMEDOUT connect"));
|
||||
expect(ex.exceptionClass).toBe("system");
|
||||
expect(ex.code).toBe("network_error");
|
||||
});
|
||||
|
||||
it("classify() tags postgres errors as system/database_error", () => {
|
||||
const ex = classify(new Error("postgres connection refused"));
|
||||
expect(ex.exceptionClass).toBe("system");
|
||||
expect(ex.code).toBe("database_error");
|
||||
});
|
||||
|
||||
it("classify() is idempotent for SettlementException inputs", () => {
|
||||
const original = Data.valueMismatch({ field: "amount" });
|
||||
expect(classify(original)).toBe(original);
|
||||
});
|
||||
});
|
||||
|
||||
describe("deterministic routing", () => {
|
||||
const cases: Array<[SettlementException, string]> = [
|
||||
[Timing.dispatch(), "retry"],
|
||||
[Timing.settlement(), "retry"],
|
||||
[Data.valueMismatch(), "abort_transaction"],
|
||||
[Data.documentHashMismatch(), "abort_transaction"],
|
||||
[Control.missingApproval(), "escalate"],
|
||||
[Control.unauthorized("x"), "escalate"],
|
||||
[Control.duplicate("ev"), "dead_letter"],
|
||||
[Business.manualStop("halt"), "abort_transaction"],
|
||||
[Business.policyViolation({ rule: "LTV" }), "escalate"],
|
||||
];
|
||||
|
||||
it.each(cases)("routes %j to %s", (ex, expected) => {
|
||||
expect(route(ex)).toBe(expected);
|
||||
});
|
||||
|
||||
it("network errors retry; non-network system errors dead-letter", () => {
|
||||
expect(route(classify(new Error("ETIMEDOUT")))).toBe("retry");
|
||||
const dbErr = classify(new Error("postgres broken"));
|
||||
expect(route(dbErr)).toBe("dead_letter");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user