File size: 9,485 Bytes
9646e24 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | import { nanoid } from "nanoid";
import {
CommitmentContract,
CommitmentDeliverable,
CommitmentMetrics,
RoleType,
} from "@/types";
import { getRoleTwin } from "@/lib/roles/twins";
import { computeConfidenceBreakdown, estimateCompletionTimes } from "@/lib/commitment/confidence";
import { recordExecutionOutcome } from "@/lib/commitment/store";
import { generateSampleAccounts, generateOpportunityMap } from "@/lib/territory/scorer";
import { optimizeRoute } from "@/lib/field/optimizer";
export type ExecutionResult = {
contract: CommitmentContract;
staged: boolean;
deliverables: CommitmentDeliverable[];
metrics: CommitmentMetrics;
};
function nowIso(): string {
return new Date().toISOString();
}
function clamp01(value: number): number {
if (!Number.isFinite(value)) return 0;
return Math.max(0, Math.min(1, value));
}
function includesAny(haystack: string, needles: string[]): boolean {
const lower = haystack.toLowerCase();
return needles.some((n) => lower.includes(n.toLowerCase()));
}
function computeCapabilityOk(contract: CommitmentContract): boolean {
const twin = getRoleTwin(contract.recipientRole as RoleType);
const outcome = (contract.requestedOutcome || "").toLowerCase();
// Ensure we are not executing prohibited outcomes.
const prohibited = twin.prohibitedActions.some((p) => outcome.includes(p.toLowerCase().split(" ")[0]));
if (prohibited) return false;
// For this demo executor, we can only complete internally-verifiable doc/report work.
const supportedSignals = ["report", "analysis", "summary", "brief", "territory", "route", "formulary", "compliance"];
return includesAny(outcome, supportedSignals);
}
function computeInputsOk(contract: CommitmentContract): boolean {
// For now: treat attachments/dependencies as partial risk, not a hard blocker.
// If a dependency has high block probability, we consider inputs not fully available.
const deps = Array.isArray(contract.dependencies) ? contract.dependencies : [];
const highBlocker = deps.some((d) => (d.blocksProbability ?? 0) >= 0.5);
return !highBlocker;
}
function computeToolsOk(contract: CommitmentContract): boolean {
// Tools are virtual in this demo. Treat required tools existence as ok if present.
return Array.isArray(contract.permittedTools) && contract.permittedTools.length > 0;
}
function computeQaOk(deliverables: CommitmentDeliverable[]): boolean {
if (!Array.isArray(deliverables) || deliverables.length === 0) return false;
// Basic QA: each deliverable must have content and confidence in [0,1].
return deliverables.every((d) => {
if (!d.content || typeof d.content !== "string" || d.content.trim().length === 0) return false;
const c = d.confidence;
return typeof c === "number" && c >= 0 && c <= 1;
});
}
function computeAcceptanceOk(contract: CommitmentContract): boolean {
// No real external feedback loop in this demo.
// Treat internal deliveries as accepted unless contract is declined/escalated/failed.
return !["failed", "declined"].includes(contract.status);
}
function determineStageOnly(contract: CommitmentContract): boolean {
// Class 2+: stage only.
if (contract.autonomyClass >= 2) return true;
// Role contract: if the twin cannot send external email, always stage.
const twin = getRoleTwin(contract.recipientRole as RoleType);
if (!twin.authorityLimits.canSendExternalEmail && contract.externalSendAllowed) return true;
return false;
}
function createExactDeliverable(contract: CommitmentContract): CommitmentDeliverable {
const title = contract.requestedOutcome || contract.emailSubject || "Requested deliverable";
const content = [
`Title: ${title}`,
`Requester: ${contract.requester}`,
`Deadline: ${contract.deadline}`,
`Role: ${contract.recipientRole}`,
"",
"Delivered items:",
...contract.mandatoryOutputs.map((o) => `- ${o}`),
"",
`Source: Email thread (id=${contract.emailId})`,
].join("\n");
return {
type: "exact",
name: "Exact fulfillment",
format: "report",
content,
confidence: 0.88,
approved: contract.autonomyClass === 1,
};
}
function createVerifiedDeliverable(contract: CommitmentContract): CommitmentDeliverable {
const assumptions = Array.isArray(contract.assumptions) ? contract.assumptions : [];
const deps = Array.isArray(contract.dependencies) ? contract.dependencies : [];
const content = [
"Verified fulfillment package",
"",
"Assumptions:",
...(assumptions.length ? assumptions.map((a) => `- ${a}`) : ["- (none recorded)"]),
"",
"Dependencies:",
...(deps.length
? deps.map((d) => `- ${d.description} (block probability ${Math.round((d.blocksProbability ?? 0) * 100)}%)`)
: ["- (none)"]),
"",
"Quality checks:",
"- Structure: OK",
"- Content present: OK",
"- Confidence fields present: OK",
].join("\n");
return {
type: "decision_enhancement",
name: "Verified package (assumptions + dependencies + QA)",
format: "report",
content,
confidence: 0.9,
approved: contract.autonomyClass === 1,
};
}
function createEnhancedDeliverables(contract: CommitmentContract): CommitmentDeliverable[] {
const outcome = (contract.requestedOutcome || "").toLowerCase();
// Territory-ish: generate opportunity map + route optimization.
if (includesAny(outcome, ["territory", "route", "coverage"])) {
const accounts = generateSampleAccounts();
const map = generateOpportunityMap(accounts);
const route = optimizeRoute(accounts, new Date().toISOString());
const opportunity = {
type: "execution_acceleration" as const,
name: "Territory opportunity map (structured JSON)",
format: "data_reconciliation" as const,
content: JSON.stringify(map, null, 2),
confidence: 0.86,
approved: contract.autonomyClass === 1,
};
const routePlan = {
type: "execution_acceleration" as const,
name: "Optimized field route (structured JSON)",
format: "data_reconciliation" as const,
content: JSON.stringify(route, null, 2),
confidence: 0.84,
approved: contract.autonomyClass === 1,
};
return [opportunity, routePlan];
}
// Default: a light executive compression.
const exec = {
type: "execution_acceleration" as const,
name: "Executive summary",
format: "report" as const,
content: [
"Executive summary",
"",
`Completed: ${contract.requestedOutcome}`,
`Deadline: ${contract.deadline}`,
"",
"Next actions:",
...contract.inferredOutputs.slice(0, 5).map((o) => `- ${o}`),
].join("\n"),
confidence: 0.83,
approved: contract.autonomyClass === 1,
};
return [exec];
}
function applyConfidenceModel(contract: CommitmentContract, metrics: CommitmentMetrics): CommitmentContract {
const breakdown = computeConfidenceBreakdown(metrics);
const times = estimateCompletionTimes(metrics);
// Keep backward compatible completionProbability while adding breakdown.
const completionProbability = clamp01(breakdown.overall);
return {
...contract,
completionProbability,
confidenceBreakdown: breakdown,
confidenceModelVersion: metrics.modelVersion,
p50Completion: times.p50Ms ? new Date(Date.now() + times.p50Ms).toISOString() : contract.p50Completion,
p90Completion: times.p90Ms ? new Date(Date.now() + times.p90Ms).toISOString() : contract.p90Completion,
};
}
export function executeCommitment(contract: CommitmentContract, metrics: CommitmentMetrics): ExecutionResult {
const start = Date.now();
const staged = determineStageOnly(contract);
const withConfidence = applyConfidenceModel(contract, metrics);
const exact = createExactDeliverable(withConfidence);
const verified = createVerifiedDeliverable(withConfidence);
const enhanced = createEnhancedDeliverables(withConfidence);
const deliverables = [exact, verified, ...enhanced].map((d) => ({
...d,
name: d.name || `deliverable-${nanoid(6)}`,
}));
const qaOk = computeQaOk(deliverables);
const capabilityOk = computeCapabilityOk(withConfidence);
const inputsOk = computeInputsOk(withConfidence);
const toolsOk = computeToolsOk(withConfidence);
const status: CommitmentContract["status"] =
withConfidence.autonomyClass === 4
? "declined"
: staged
? "awaiting_approval"
: capabilityOk && inputsOk && toolsOk && qaOk
? "completed"
: "escalated";
const finished: CommitmentContract = {
...withConfidence,
status,
deliverables,
executedAt: withConfidence.executedAt || nowIso(),
completedAt: status === "completed" ? nowIso() : withConfidence.completedAt,
auditEvents: [
...(Array.isArray(withConfidence.auditEvents) ? withConfidence.auditEvents : []),
{
timestamp: nowIso(),
event: "execute",
detail: staged
? "Executed work and staged output for approval"
: "Executed work and completed output",
},
{
timestamp: nowIso(),
event: "quality_gate",
detail: `capability=${capabilityOk} inputs=${inputsOk} tools=${toolsOk} qa=${qaOk}`,
},
],
};
const durationMs = Date.now() - start;
const updatedMetrics = recordExecutionOutcome({
capabilityOk,
inputsOk,
toolsOk,
qaOk,
durationMs,
});
return {
contract: finished,
staged,
deliverables,
metrics: updatedMetrics,
};
}
|