feat(cost-benefit/dag-propagation): implement DAG-propagation effective probability computation

Implement computeEffectiveP internal helper and workflowCost public function
that captures the structural reality that upstream failures multiply downstream
damage, per ADR-004 and the Python research model.

- computeEffectiveP: computes pEffective from intrinsic probability + upstream
  propagation using inherited quality factors (parentP + (1-parentP) × qualityRetention)
- workflowCost: processes tasks in topological order, computes per-task EV with
  degraded effective probability, includes pIntrinsic/pEffective split
- Supports independent and dag-propagate modes
- Completed tasks excluded from results but propagate p=1.0 when includeCompleted: false
- Per-edge qualityRetention overrides defaultQualityRetention option
- Throws CircularDependencyError for cyclic graphs via topologicalOrder
- 30+ new tests covering chain compounding, diamond graph, mode comparison,
  completed task semantics, cycle detection, per-edge qualityRetention
This commit is contained in:
2026-04-27 12:52:47 +00:00
parent b0d943f4e6
commit 7bfcfecc7a
3 changed files with 939 additions and 15 deletions

View File

@@ -1,5 +1,53 @@
import { describe, it, expect } from "vitest";
import { calculateTaskEv } from "../src/analysis/cost-benefit.js";
import { calculateTaskEv, computeEffectiveP, workflowCost } from "../src/analysis/cost-benefit.js";
import { TaskGraph } from "../src/graph/construction.js";
import { CircularDependencyError } from "../src/error/index.js";
// ---------------------------------------------------------------------------
// Helper: create test graphs
// ---------------------------------------------------------------------------
/**
* Create a simple chain graph: A → B → C
* All tasks have medium risk (p=0.80), narrow scope, isolated impact.
*/
function createChainGraph(): TaskGraph {
return TaskGraph.fromTasks([
{ id: "A", name: "Task A", dependsOn: [], risk: "medium", scope: "narrow", impact: "isolated" },
{ id: "B", name: "Task B", dependsOn: ["A"], risk: "medium", scope: "narrow", impact: "isolated" },
{ id: "C", name: "Task C", dependsOn: ["B"], risk: "medium", scope: "narrow", impact: "isolated" },
]);
}
/**
* Create a diamond graph: A → B, A → C, B → D, C → D
* This tests that convergence correctly multiplies both inherited factors.
*/
function createDiamondGraph(): TaskGraph {
return TaskGraph.fromTasks([
{ id: "A", name: "Task A", dependsOn: [], risk: "medium", scope: "narrow", impact: "isolated" },
{ id: "B", name: "Task B", dependsOn: ["A"], risk: "medium", scope: "narrow", impact: "isolated" },
{ id: "C", name: "Task C", dependsOn: ["A"], risk: "medium", scope: "narrow", impact: "isolated" },
{ id: "D", name: "Task D", dependsOn: ["B", "C"], risk: "medium", scope: "narrow", impact: "isolated" },
]);
}
/**
* Create a cyclic graph for testing CircularDependencyError.
*/
function createCyclicGraph(): TaskGraph {
const tg = new TaskGraph();
// We manually add nodes and edges that form a cycle
// Note: TaskGraph prevents self-loops but we can create cycles
tg.addTask("A", { name: "Task A" });
tg.addTask("B", { name: "Task B" });
tg.addTask("C", { name: "Task C" });
// Create cycle: A → B → C → A
tg.addDependency("A", "B");
tg.addDependency("B", "C");
tg.addDependency("C", "A");
return tg;
}
// ---------------------------------------------------------------------------
// calculateTaskEv — pure function tests
@@ -330,4 +378,682 @@ describe("calculateTaskEv", () => {
// EV = 9.5 * 50 = 475.0
expect(result.ev).toBeCloseTo(475.0);
});
});
// ---------------------------------------------------------------------------
// computeEffectiveP — DAG-propagation probability tests
// ---------------------------------------------------------------------------
describe("computeEffectiveP", () => {
// Helper to create a graph and compute effective probabilities
const qr0_9 = 0.9; // default qualityRetention
it("returns pIntrinsic in independent mode regardless of parents", () => {
const graph = createChainGraph();
const upstreamSuccessProbs = new Map<string, number>();
// Even with upstream data, independent mode returns pIntrinsic
upstreamSuccessProbs.set("A", 0.65);
expect(
computeEffectiveP("B", graph.raw, upstreamSuccessProbs, qr0_9, "independent", 0.80)
).toBeCloseTo(0.80);
});
it("returns pIntrinsic for root tasks (no prerequisites)", () => {
const graph = createChainGraph();
const upstreamSuccessProbs = new Map<string, number>();
// Task A has no prerequisites — pEffective should equal pIntrinsic
expect(
computeEffectiveP("A", graph.raw, upstreamSuccessProbs, qr0_9, "dag-propagate", 0.80)
).toBeCloseTo(0.80);
});
it("computes inherited quality for a simple chain: A → B", () => {
// A has p=0.65 (high risk), B has intrinsic p=0.80 (medium risk)
// qualityRetention = 0.9 (default)
// inheritedQuality from A = 0.65 + (1 - 0.65) * 0.9 = 0.65 + 0.315 = 0.965
// pEffective_B = 0.80 * 0.965 = 0.772
const graph = createChainGraph();
const upstreamSuccessProbs = new Map<string, number>();
upstreamSuccessProbs.set("A", 0.65);
const pEff_B = computeEffectiveP(
"B", graph.raw, upstreamSuccessProbs, qr0_9, "dag-propagate", 0.80
);
const inheritedFromA = 0.65 + (1 - 0.65) * 0.9; // 0.965
expect(pEff_B).toBeCloseTo(0.80 * inheritedFromA);
});
it("computes compounding for a 3-node chain: A → B → C", () => {
// A (p=0.65) → B (p=0.80) → C (p=0.80)
// Step 1: pEff_A = 0.65 (root, no parents)
// Step 2: B has parent A with p=0.65
// inheritedFromA = 0.65 + (1-0.65)*0.9 = 0.965
// pEff_B = 0.80 * 0.965 = 0.772
// Step 3: C has parent B with p=0.772
// inheritedFromB = 0.772 + (1-0.772)*0.9 = 0.772 + 0.2052 = 0.9772
// pEff_C = 0.80 * 0.9772 = 0.78176
const graph = createChainGraph();
const upstreamSuccessProbs = new Map<string, number>();
// A (root)
const pEff_A = computeEffectiveP("A", graph.raw, upstreamSuccessProbs, qr0_9, "dag-propagate", 0.65);
expect(pEff_A).toBeCloseTo(0.65);
upstreamSuccessProbs.set("A", pEff_A);
// B depends on A
const pEff_B = computeEffectiveP("B", graph.raw, upstreamSuccessProbs, qr0_9, "dag-propagate", 0.80);
const inheritedFromA = 0.65 + (1 - 0.65) * 0.9;
expect(pEff_B).toBeCloseTo(0.80 * inheritedFromA);
upstreamSuccessProbs.set("B", pEff_B);
// C depends on B
const pEff_C = computeEffectiveP("C", graph.raw, upstreamSuccessProbs, qr0_9, "dag-propagate", 0.80);
const inheritedFromB = pEff_B + (1 - pEff_B) * 0.9;
expect(pEff_C).toBeCloseTo(0.80 * inheritedFromB);
});
it("computes diamond graph: A → B, A → C, B → D, C → D", () => {
// A=0.65 (high risk), B=C=D=0.80 (medium risk)
// Step 1: pEff_A = 0.65
// Step 2 (B): parent A with p=0.65
// inheritedFromA = 0.65 + 0.35*0.9 = 0.965
// pEff_B = 0.80 * 0.965 = 0.772
// Step 3 (C): parent A with p=0.65
// pEff_C = same as B = 0.772
// Step 4 (D): parents B (p=0.772) and C (p=0.772)
// inheritedFromB = 0.772 + 0.228*0.9 = 0.772 + 0.2052 = 0.9772
// inheritedFromC = same = 0.9772
// pEff_D = 0.80 * 0.9772 * 0.9772
const graph = createDiamondGraph();
const upstreamSuccessProbs = new Map<string, number>();
// A (root)
const pEff_A = computeEffectiveP("A", graph.raw, upstreamSuccessProbs, qr0_9, "dag-propagate", 0.65);
expect(pEff_A).toBeCloseTo(0.65);
upstreamSuccessProbs.set("A", pEff_A);
// B depends on A
const pEff_B = computeEffectiveP("B", graph.raw, upstreamSuccessProbs, qr0_9, "dag-propagate", 0.80);
const inheritedFromA = 0.65 + (1 - 0.65) * 0.9;
expect(pEff_B).toBeCloseTo(0.80 * inheritedFromA);
upstreamSuccessProbs.set("B", pEff_B);
// C depends on A
const pEff_C = computeEffectiveP("C", graph.raw, upstreamSuccessProbs, qr0_9, "dag-propagate", 0.80);
expect(pEff_C).toBeCloseTo(0.80 * inheritedFromA); // same as B
upstreamSuccessProbs.set("C", pEff_C);
// D depends on B and C
const pEff_D = computeEffectiveP("D", graph.raw, upstreamSuccessProbs, qr0_9, "dag-propagate", 0.80);
const inheritedFromB = pEff_B + (1 - pEff_B) * 0.9;
const inheritedFromC = pEff_C + (1 - pEff_C) * 0.9;
expect(pEff_D).toBeCloseTo(0.80 * inheritedFromB * inheritedFromC);
});
it("uses per-edge qualityRetention from edge attributes", () => {
// Create a graph with custom qualityRetention per edge
const tg = TaskGraph.fromRecords(
[
{ id: "A", name: "Task A", dependsOn: [] },
{ id: "B", name: "Task B", dependsOn: ["A"] },
],
[
{ from: "A", to: "B", qualityRetention: 0.5 }, // lower retention
]
);
const upstreamSuccessProbs = new Map<string, number>();
upstreamSuccessProbs.set("A", 0.65);
// With qualityRetention=0.5: inheritedQuality = 0.65 + 0.35*0.5 = 0.825
// pEffective = 0.80 * 0.825 = 0.66
const pEff = computeEffectiveP("B", tg.raw, upstreamSuccessProbs, 0.9, "dag-propagate", 0.80);
expect(pEff).toBeCloseTo(0.80 * (0.65 + (1 - 0.65) * 0.5));
});
it("falls back to defaultQualityRetention when edge has no qualityRetention attribute", () => {
// Create a graph using raw graphology API so edges don't have qualityRetention
const tg = new TaskGraph();
tg.addTask("A", { name: "Task A" });
tg.addTask("B", { name: "Task B" });
// Add edge directly via raw graphology, without qualityRetention attribute
tg.raw.addEdgeWithKey("A->B", "A", "B", {});
const upstreamSuccessProbs = new Map<string, number>();
upstreamSuccessProbs.set("A", 0.65);
// Since the edge has no qualityRetention attribute, defaultQualityRetention=0.85 is used
// inheritedQuality = 0.65 + 0.35*0.85 = 0.65 + 0.2975 = 0.9475
// pEffective = 0.80 * 0.9475 = 0.758
const pEff = computeEffectiveP("B", tg.raw, upstreamSuccessProbs, 0.85, "dag-propagate", 0.80);
expect(pEff).toBeCloseTo(0.80 * (0.65 + 0.35 * 0.85));
});
it("qualityRetention=1.0 gives independent model (inherited quality = 1.0)", () => {
// With qualityRetention=1.0: inheritedQuality = parentP + (1-parentP)*1.0 = 1.0
// pEffective = pIntrinsic * 1.0 = pIntrinsic
// Create graph with edges that have qualityRetention=1.0
const tg = TaskGraph.fromRecords(
[
{ id: "A", name: "Task A", dependsOn: [] },
{ id: "B", name: "Task B", dependsOn: ["A"] },
],
[
{ from: "A", to: "B", qualityRetention: 1.0 },
]
);
const upstreamSuccessProbs = new Map<string, number>();
upstreamSuccessProbs.set("A", 0.50);
const pEff = computeEffectiveP("B", tg.raw, upstreamSuccessProbs, 0.9, "dag-propagate", 0.80);
expect(pEff).toBeCloseTo(0.80); // pIntrinsic unchanged
});
it("qualityRetention=0.0 gives full propagation (inherited quality = parentP)", () => {
// With qualityRetention=0.0: inheritedQuality = parentP + (1-parentP)*0.0 = parentP
// So pEffective = pIntrinsic * parentP
// Create graph with edges that have qualityRetention=0.0
const tg = TaskGraph.fromRecords(
[
{ id: "A", name: "Task A", dependsOn: [] },
{ id: "B", name: "Task B", dependsOn: ["A"] },
],
[
{ from: "A", to: "B", qualityRetention: 0.0 },
]
);
const upstreamSuccessProbs = new Map<string, number>();
upstreamSuccessProbs.set("A", 0.65);
const pEff = computeEffectiveP("B", tg.raw, upstreamSuccessProbs, 0.9, "dag-propagate", 0.80);
expect(pEff).toBeCloseTo(0.80 * 0.65); // pIntrinsic * parentP
});
it("skips parents not in upstream map (robustness)", () => {
// B depends on A, but A is not in upstreamSuccessProbs yet
// This shouldn't happen in normal usage (topological order ensures processing)
// but the function should gracefully skip missing parents
const graph = createChainGraph();
const upstreamSuccessProbs = new Map<string, number>();
// A not in map — should be skipped, so no inherited quality factors
// pEffective = pIntrinsic (no parents found in map)
const pEff = computeEffectiveP("B", graph.raw, upstreamSuccessProbs, qr0_9, "dag-propagate", 0.80);
// With no parents found in map, product is 1.0, so pIntrinsic
// Wait — actually B has a parent A in the graph, but A isn't in the map.
// The function skips it, so no multiplication happens.
// inheritedProduct stays at 1.0 → pEffective = pIntrinsic
expect(pEff).toBeCloseTo(0.80);
});
});
// ---------------------------------------------------------------------------
// workflowCost — integration tests
// ---------------------------------------------------------------------------
describe("workflowCost", () => {
it("computes workflow cost for a simple chain with dag-propagate mode", () => {
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Planning", dependsOn: [], risk: "high", scope: "narrow", impact: "phase" },
{ id: "B", name: "Implementation", dependsOn: ["A"], risk: "medium", scope: "broad", impact: "component" },
]);
const result = workflowCost(graph.raw);
expect(result.propagationMode).toBe("dag-propagate");
// Task A (root, high risk, narrow scope, phase impact)
// pIntrinsic_A = 0.65, pEffective_A = 0.65 (root, no parents)
// EV_A = 0.65 * (2.0 * 2.0) + 0.35 * (2.0 * 2.0) = 4.0 (default config, C_fail=C_success)
const taskA = result.tasks.find(t => t.taskId === "A")!;
expect(taskA).toBeDefined();
expect(taskA.pIntrinsic).toBeCloseTo(0.65);
expect(taskA.pEffective).toBeCloseTo(0.65);
expect(taskA.scopeCost).toBeCloseTo(2.0); // narrow
expect(taskA.impactWeight).toBeCloseTo(2.0); // phase
// Task B (depends on A, medium risk, broad scope, component impact)
// inheritedFromA = 0.65 + 0.35*0.9 = 0.965
// pEffective_B = 0.80 * 0.965 = 0.772
const taskB = result.tasks.find(t => t.taskId === "B")!;
expect(taskB).toBeDefined();
expect(taskB.pIntrinsic).toBeCloseTo(0.80);
expect(taskB.pEffective).toBeCloseTo(0.80 * (0.65 + 0.35 * 0.9));
expect(taskB.scopeCost).toBeCloseTo(4.0); // broad
expect(taskB.impactWeight).toBeCloseTo(1.5); // component
});
it("computes workflow cost in independent mode (no propagation)", () => {
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Planning", dependsOn: [], risk: "high", scope: "narrow", impact: "phase" },
{ id: "B", name: "Implementation", dependsOn: ["A"], risk: "medium", scope: "broad", impact: "component" },
]);
const result = workflowCost(graph.raw, { propagationMode: "independent" });
expect(result.propagationMode).toBe("independent");
// In independent mode, pEffective = pIntrinsic for all tasks
const taskA = result.tasks.find(t => t.taskId === "A")!;
expect(taskA.pEffective).toBeCloseTo(0.65); // high risk
expect(taskA.pIntrinsic).toBeCloseTo(0.65);
const taskB = result.tasks.find(t => t.taskId === "B")!;
expect(taskB.pEffective).toBeCloseTo(0.80); // medium risk, no propagation
expect(taskB.pIntrinsic).toBeCloseTo(0.80);
});
it("dag-propagate mode shows degradation vs independent mode", () => {
// Key insight: in dag-propagate, downstream tasks have lower pEffective
// than their pIntrinsic, because upstream quality degrades them
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Planning", dependsOn: [], risk: "critical", scope: "broad", impact: "phase" },
{ id: "B", name: "Implementation", dependsOn: ["A"], risk: "medium", scope: "moderate", impact: "component" },
{ id: "C", name: "Review", dependsOn: ["B"], risk: "low", scope: "narrow", impact: "isolated" },
]);
const dagResult = workflowCost(graph.raw, { propagationMode: "dag-propagate" });
const indepResult = workflowCost(graph.raw, { propagationMode: "independent" });
// In dag-propagate, every task that has parents should have pEffective < pIntrinsic
// (assuming qualityRetention < 1.0)
for (const task of dagResult.tasks) {
const indepTask = indepResult.tasks.find(t => t.taskId === task.taskId)!;
if (indepTask) {
// Root task has pEffective = pIntrinsic in both modes
if (task.taskId === "A") {
expect(task.pEffective).toBeCloseTo(task.pIntrinsic);
} else {
// Propagation should reduce pEffective below pIntrinsic
expect(task.pEffective).toBeLessThan(task.pIntrinsic);
}
}
}
// Total EV may be the same with default config (C_fail = C_success),
// but pEffective values differ which is the key metric
// Verify that at least one task has different pEffective between modes
let anyDifferent = false;
for (const task of dagResult.tasks) {
const indepTask = indepResult.tasks.find(t => t.taskId === task.taskId)!;
if (Math.abs(task.pEffective - indepTask.pEffective) > 0.001) {
anyDifferent = true;
}
}
expect(anyDifferent).toBe(true);
});
it("computes chain with compounding effect — each hop compounds quality loss", () => {
// A (critical, p=0.50) → B (medium, p=0.80) → C (medium, p=0.80)
// In dag-propagate: pEff degrades at each hop
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Task A", dependsOn: [], risk: "critical", scope: "narrow", impact: "isolated" },
{ id: "B", name: "Task B", dependsOn: ["A"], risk: "medium", scope: "narrow", impact: "isolated" },
{ id: "C", name: "Task C", dependsOn: ["B"], risk: "medium", scope: "narrow", impact: "isolated" },
]);
const result = workflowCost(graph.raw, { propagationMode: "dag-propagate" });
const taskA = result.tasks.find(t => t.taskId === "A")!;
const taskB = result.tasks.find(t => t.taskId === "B")!;
const taskC = result.tasks.find(t => t.taskId === "C")!;
// A is root: pEffective = pIntrinsic = 0.50
expect(taskA.pIntrinsic).toBeCloseTo(0.50);
expect(taskA.pEffective).toBeCloseTo(0.50);
// B has parent A (p=0.50):
// inheritedFromA = 0.50 + 0.50*0.9 = 0.95
// pEffective_B = 0.80 * 0.95 = 0.76
expect(taskB.pEffective).toBeCloseTo(0.80 * (0.50 + 0.50 * 0.9));
// C has parent B (p=0.76):
// inheritedFromB = 0.76 + 0.24*0.9 = 0.76 + 0.216 = 0.976
// pEffective_C = 0.80 * 0.976 = 0.7808
const pEff_B = 0.80 * (0.50 + 0.50 * 0.9);
const inheritedFromB = pEff_B + (1 - pEff_B) * 0.9;
expect(taskC.pEffective).toBeCloseTo(0.80 * inheritedFromB);
// Verify compounding: C has more degradation than B
const degradation_B = taskB.pIntrinsic - taskB.pEffective;
const degradation_C = taskC.pIntrinsic - taskC.pEffective;
// Both are degraded, and the degradation accumulates
expect(degradation_B).toBeGreaterThan(0);
expect(degradation_C).toBeGreaterThan(0);
});
it("diamond graph: convergence multiplies inherited quality factors", () => {
const graph = createDiamondGraph();
const result = workflowCost(graph.raw);
const taskA = result.tasks.find(t => t.taskId === "A")!;
const taskB = result.tasks.find(t => t.taskId === "B")!;
const taskC = result.tasks.find(t => t.taskId === "C")!;
const taskD = result.tasks.find(t => t.taskId === "D")!;
// All have medium risk (p=0.80)
expect(taskA.pIntrinsic).toBeCloseTo(0.80);
expect(taskB.pIntrinsic).toBeCloseTo(0.80);
expect(taskC.pIntrinsic).toBeCloseTo(0.80);
expect(taskD.pIntrinsic).toBeCloseTo(0.80);
// A is root, no degradation
expect(taskA.pEffective).toBeCloseTo(0.80);
// B and C both depend on A — they get the same degradation
const inheritedFromA = 0.80 + 0.20 * 0.9; // = 0.98
expect(taskB.pEffective).toBeCloseTo(0.80 * inheritedFromA);
expect(taskC.pEffective).toBeCloseTo(0.80 * inheritedFromA);
// D depends on both B and C — product of both inherited factors
const inheritedFromB = taskB.pEffective + (1 - taskB.pEffective) * 0.9;
const inheritedFromC = taskC.pEffective + (1 - taskC.pEffective) * 0.9;
expect(taskD.pEffective).toBeCloseTo(0.80 * inheritedFromB * inheritedFromC);
});
it("excludes completed tasks when includeCompleted=false but propagates with p=1.0", () => {
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Task A", dependsOn: [], risk: "high", scope: "narrow", impact: "isolated", status: "completed" },
{ id: "B", name: "Task B", dependsOn: ["A"], risk: "medium", scope: "narrow", impact: "isolated" },
]);
const result = workflowCost(graph.raw, { includeCompleted: false });
// A should not appear in the task list
expect(result.tasks.find(t => t.taskId === "A")).toBeUndefined();
// B's propagation should use p=1.0 for A (completed)
// inheritedFromA = 1.0 + (1-1.0)*0.9 = 1.0
// pEffective_B = 0.80 * 1.0 = 0.80
const taskB = result.tasks.find(t => t.taskId === "B")!;
expect(taskB).toBeDefined();
expect(taskB.pEffective).toBeCloseTo(0.80); // No degradation from completed parent
expect(taskB.pIntrinsic).toBeCloseTo(0.80);
});
it("includes completed tasks when includeCompleted=true (default)", () => {
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Task A", dependsOn: [], risk: "high", scope: "narrow", impact: "isolated", status: "completed" },
{ id: "B", name: "Task B", dependsOn: ["A"], risk: "medium", scope: "narrow", impact: "isolated" },
]);
const result = workflowCost(graph.raw); // default includeCompleted=true
// A should appear in the task list
const taskA = result.tasks.find(t => t.taskId === "A")!;
expect(taskA).toBeDefined();
// When includeCompleted=true, A propagates with its pEffective (not 1.0)
// A is a root with risk="high" → pIntrinsic = 0.65, pEffective = 0.65
expect(taskA.pEffective).toBeCloseTo(0.65);
expect(taskA.pIntrinsic).toBeCloseTo(0.65);
// B's propagation uses A's pEffective = 0.65
// inheritedFromA = 0.65 + 0.35*0.9 = 0.965
// pEffective_B = 0.80 * 0.965 = 0.772
const taskB = result.tasks.find(t => t.taskId === "B")!;
expect(taskB.pEffective).toBeCloseTo(0.80 * (0.65 + 0.35 * 0.9));
});
it("completed task with includeCompleted=false still propagates correctly to downstream", () => {
// A (completed) → B → C
// A propagates p=1.0
// B should not be degraded by A's completion
// C should receive B's degraded probability (not A's)
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Task A", dependsOn: [], risk: "critical", scope: "narrow", impact: "isolated", status: "completed" },
{ id: "B", name: "Task B", dependsOn: ["A"], risk: "high", scope: "narrow", impact: "isolated" },
{ id: "C", name: "Task C", dependsOn: ["B"], risk: "medium", scope: "narrow", impact: "isolated" },
]);
const result = workflowCost(graph.raw, { includeCompleted: false });
// A should not be in results
expect(result.tasks.find(t => t.taskId === "A")).toBeUndefined();
// B's parent A propagates with p=1.0 (completed)
// inheritedFromA = 1.0 + 0.0 * 0.9 = 1.0
// pEffective_B = 0.65 (high risk) * 1.0 = 0.65
const taskB = result.tasks.find(t => t.taskId === "B")!;
expect(taskB.pIntrinsic).toBeCloseTo(0.65); // high risk
expect(taskB.pEffective).toBeCloseTo(0.65); // no degradation from completed parent
// C depends on B (p=0.65 propagated)
// inheritedFromB = 0.65 + 0.35*0.9 = 0.965
// pEffective_C = 0.80 * 0.965 = 0.772
const taskC = result.tasks.find(t => t.taskId === "C")!;
expect(taskC.pEffective).toBeCloseTo(0.80 * (0.65 + 0.35 * 0.9));
});
it("failed and blocked tasks are always included regardless of includeCompleted", () => {
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Task A", dependsOn: [], risk: "medium", scope: "narrow", impact: "isolated", status: "failed" },
{ id: "B", name: "Task B", dependsOn: [], risk: "medium", scope: "narrow", impact: "isolated", status: "blocked" },
]);
const result = workflowCost(graph.raw, { includeCompleted: false });
// Both failed and blocked tasks should be included
expect(result.tasks.find(t => t.taskId === "A")).toBeDefined();
expect(result.tasks.find(t => t.taskId === "B")).toBeDefined();
});
it("throws CircularDependencyError for cyclic graph", () => {
const graph = createCyclicGraph();
expect(() => workflowCost(graph.raw)).toThrow(CircularDependencyError);
});
it("handles empty graph", () => {
const graph = new TaskGraph();
const result = workflowCost(graph.raw);
expect(result.tasks).toEqual([]);
expect(result.totalEv).toBe(0);
expect(result.averageEv).toBe(0);
expect(result.propagationMode).toBe("dag-propagate");
});
it("handles single node graph", () => {
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Task A", dependsOn: [], risk: "medium", scope: "narrow", impact: "isolated" },
]);
const result = workflowCost(graph.raw);
expect(result.tasks).toHaveLength(1);
expect(result.tasks[0]!.taskId).toBe("A");
expect(result.tasks[0]!.pIntrinsic).toBeCloseTo(0.80); // medium risk
expect(result.tasks[0]!.pEffective).toBeCloseTo(0.80); // root, no parents
});
it("respects defaultQualityRetention option when per-edge attribute is absent", () => {
// Create a graph using raw graphology API so edges don't have qualityRetention
const tg = new TaskGraph();
tg.addTask("A", { name: "Task A", risk: "critical", scope: "narrow", impact: "isolated" });
tg.addTask("B", { name: "Task B", risk: "medium", scope: "narrow", impact: "isolated" });
// Add edge without qualityRetention attribute
tg.raw.addEdgeWithKey("A->B", "A", "B", {});
// With defaultQualityRetention = 1.0, should behave like independent model
const result = workflowCost(tg.raw, { defaultQualityRetention: 1.0 });
const taskB = result.tasks.find(t => t.taskId === "B")!;
// inheritedQuality = parentP + (1-parentP) * 1.0 = 1.0
// pEffective = pIntrinsic * 1.0 = pIntrinsic
expect(taskB.pEffective).toBeCloseTo(taskB.pIntrinsic);
});
it("per-edge qualityRetention overrides default", () => {
// Create graph with custom qualityRetention on the edge
const graph = TaskGraph.fromRecords(
[
{ id: "A", name: "Task A", dependsOn: [], risk: "critical", scope: "narrow", impact: "isolated" },
{ id: "B", name: "Task B", dependsOn: ["A"], risk: "medium", scope: "narrow", impact: "isolated" },
],
[
{ from: "A", to: "B", qualityRetention: 0.5 },
]
);
const result = workflowCost(graph.raw); // default qualityRetention=0.9
const taskB = result.tasks.find(t => t.taskId === "B")!;
// Per-edge qualityRetention = 0.5 overrides default 0.9
// inheritedFromA = 0.50 + 0.50*0.5 = 0.75
// pEffective_B = 0.80 * 0.75 = 0.60
expect(taskB.pEffective).toBeCloseTo(0.80 * (0.50 + 0.50 * 0.5));
});
it("applies limit to task entries", () => {
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Task A", dependsOn: [], risk: "medium", scope: "narrow", impact: "isolated" },
{ id: "B", name: "Task B", dependsOn: ["A"], risk: "medium", scope: "narrow", impact: "isolated" },
{ id: "C", name: "Task C", dependsOn: ["B"], risk: "medium", scope: "narrow", impact: "isolated" },
]);
const result = workflowCost(graph.raw, { limit: 2 });
expect(result.tasks).toHaveLength(2);
// limit only affects the result list, not propagation
expect(result.tasks[0]!.taskId).toBe("A");
expect(result.tasks[1]!.taskId).toBe("B");
});
it("includes both pIntrinsic and pEffective in per-task entries", () => {
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Task A", dependsOn: [], risk: "high", scope: "narrow", impact: "isolated" },
{ id: "B", name: "Task B", dependsOn: ["A"], risk: "medium", scope: "narrow", impact: "isolated" },
]);
const result = workflowCost(graph.raw);
for (const task of result.tasks) {
expect(typeof task.pIntrinsic).toBe("number");
expect(typeof task.pEffective).toBe("number");
expect(typeof task.probability).toBe("number");
expect(task.probability).toBeCloseTo(task.pEffective);
expect(typeof task.scopeCost).toBe("number");
expect(typeof task.impactWeight).toBe("number");
expect(typeof task.taskId).toBe("string");
expect(typeof task.name).toBe("string");
}
});
it("computes totalEv and averageEv correctly", () => {
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Task A", dependsOn: [], risk: "medium", scope: "narrow", impact: "isolated" },
{ id: "B", name: "Task B", dependsOn: [], risk: "medium", scope: "narrow", impact: "isolated" },
]);
const result = workflowCost(graph.raw, { propagationMode: "independent" });
// Two independent tasks with medium risk, narrow scope, isolated impact
// p=0.80, scopeCost=2.0, impactWeight=1.0
// EV = 0.80 * 2.0 + 0.20 * 2.0 = 2.0 each
// totalEv = 4.0, averageEv = 2.0
expect(result.totalEv).toBeCloseTo(4.0);
expect(result.averageEv).toBeCloseTo(2.0);
});
it("uses defaults for tasks with null categorical fields", () => {
// Task with no risk/scope/impact specified — should use defaults
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Task A", dependsOn: [] },
]);
const result = workflowCost(graph.raw);
const taskA = result.tasks[0]!;
// defaults: risk=medium (p=0.80), scope=narrow (costEstimate=2.0), impact=isolated (weight=1.0)
expect(taskA.pIntrinsic).toBeCloseTo(0.80);
expect(taskA.pEffective).toBeCloseTo(0.80);
expect(taskA.scopeCost).toBeCloseTo(2.0);
expect(taskA.impactWeight).toBeCloseTo(1.0);
});
it("independent mode produces same pIntrinsic and pEffective for all tasks", () => {
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Task A", dependsOn: [], risk: "critical", scope: "broad", impact: "project" },
{ id: "B", name: "Task B", dependsOn: ["A"], risk: "high", scope: "moderate", impact: "component" },
{ id: "C", name: "Task C", dependsOn: ["B"], risk: "low", scope: "narrow", impact: "isolated" },
]);
const result = workflowCost(graph.raw, { propagationMode: "independent" });
for (const task of result.tasks) {
expect(task.pEffective).toBeCloseTo(task.pIntrinsic);
}
});
it("dag-propagate with high risk parent shows significant degradation vs independent", () => {
// Planning task with critical risk (p=0.50) followed by implementation
// This matches the Python research model insight
const graph = TaskGraph.fromTasks([
{ id: "planning", name: "Planning", dependsOn: [], risk: "critical", scope: "broad", impact: "phase" },
{ id: "implementation", name: "Implementation", dependsOn: ["planning"], risk: "medium", scope: "broad", impact: "component" },
]);
const dagResult = workflowCost(graph.raw, { propagationMode: "dag-propagate" });
const indepResult = workflowCost(graph.raw, { propagationMode: "independent" });
const dagImpl = dagResult.tasks.find(t => t.taskId === "implementation")!;
const indepImpl = indepResult.tasks.find(t => t.taskId === "implementation")!;
// DAG-propagate should show lower pEffective for implementation
// because the critical-risk parent degrades quality
expect(dagImpl.pEffective).toBeLessThan(indepImpl.pEffective);
// The degradation should be significant due to critical risk (p=0.50)
// inheritedQuality = 0.50 + 0.50*0.9 = 0.95
// pEffective_dag = 0.80 * 0.95 = 0.76
expect(dagImpl.pEffective).toBeCloseTo(0.80 * 0.95);
expect(dagImpl.pEffective).toBeCloseTo(0.76);
// Independent: pEffective = pIntrinsic = 0.80
expect(indepImpl.pEffective).toBeCloseTo(0.80);
});
it("parallel tasks with no shared parent have same pEffective in both modes", () => {
const graph = TaskGraph.fromTasks([
{ id: "A", name: "Task A", dependsOn: [], risk: "medium", scope: "narrow", impact: "isolated" },
{ id: "B", name: "Task B", dependsOn: [], risk: "medium", scope: "narrow", impact: "isolated" },
]);
const dagResult = workflowCost(graph.raw, { propagationMode: "dag-propagate" });
const indepResult = workflowCost(graph.raw, { propagationMode: "independent" });
// No dependencies → no propagation → same result
expect(dagResult.tasks[0]!.pEffective).toBeCloseTo(indepResult.tasks[0]!.pEffective);
expect(dagResult.tasks[1]!.pEffective).toBeCloseTo(indepResult.tasks[1]!.pEffective);
});
});
// ---------------------------------------------------------------------------
// CircularDependencyError for cyclic graphs
// ---------------------------------------------------------------------------
describe("workflowCost cycle detection", () => {
it("throws CircularDependencyError when graph has cycles", () => {
const graph = createCyclicGraph();
expect(() => workflowCost(graph.raw)).toThrow(CircularDependencyError);
});
it("CircularDependencyError contains cycle information", () => {
const graph = createCyclicGraph();
try {
workflowCost(graph.raw);
expect.fail("Should have thrown CircularDependencyError");
} catch (error) {
expect(error).toBeInstanceOf(CircularDependencyError);
const err = error as CircularDependencyError;
expect(err.cycles.length).toBeGreaterThan(0);
// The cycle should include the nodes A → B → C
const cycleFlat = err.cycles.flat();
expect(cycleFlat).toContain("A");
expect(cycleFlat).toContain("B");
expect(cycleFlat).toContain("C");
}
});
});