Spaces:
Runtime error
Runtime error
File size: 13,065 Bytes
518343a | 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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | import {
createToolEnvelope,
emitReceipt,
verifyChain,
type EmitReceiptOptions,
type OperationalReceipt,
type ToolEnvelope,
type VerifyResult,
} from "@szl-holdings/a11oy-receipt-substrate";
export type AutonomousLearningEventType =
| "AUTONOMOUS_LEARNING_PROPOSAL"
| "AUTONOMOUS_LEARNING_EVALUATION"
| "HUMAN_PROMOTION";
export type AutonomousLearningClaimStatus =
| "verified-runtime"
| "release-payload"
| "lean-backed-current-green"
| "lean-backed-needs-upstream-ci"
| "thesis-anchor"
| "historical"
| "roadmap"
| "staged";
export interface ForbiddenClaimScan {
readonly passed: boolean;
readonly scanner: string;
readonly forbiddenMatches: readonly string[];
}
export interface SourceIngress {
readonly sourceUri: string;
readonly licenseClass: "public" | "licensed" | "permissioned" | "user-owned";
readonly contentHash?: string;
}
export interface AutonomousLearningProposalInput {
readonly proposalId: string;
readonly runId: string;
readonly actorId: string;
readonly proposalKind: "doc" | "json-map" | "patch" | "theorem-runtime-hook" | "benchmark-route" | "uds-hf-publication";
readonly sourceCommit: string;
readonly policyHash: string;
readonly lambdaAxes: readonly string[];
readonly toolVersions: Record<string, string>;
readonly sourceIngress: readonly SourceIngress[];
readonly intendedFiles: readonly string[];
readonly expectedBehavior: string;
readonly riskClass: "low" | "medium" | "high";
readonly proposalPayloadDigest: string;
readonly forbiddenClaimScan: ForbiddenClaimScan;
readonly stagedAdvisory: string;
readonly claimStatus: AutonomousLearningClaimStatus;
readonly details?: Record<string, unknown>;
}
export interface AutonomousLearningEvaluationInput {
readonly proposalId: string;
readonly runId: string;
readonly actorId: string;
readonly proposalReceiptId: string;
readonly proposalActorId: string;
readonly sourceCommit: string;
readonly policyHash: string;
readonly harnessCommitSha: string;
readonly replaySeeds: readonly number[];
readonly frozenTime: string;
readonly inputRoots: readonly string[];
readonly outputRoots: readonly string[];
readonly deterministicPass: boolean;
readonly varianceBounds: Record<string, number>;
readonly adversarialChecks: readonly string[];
readonly failureReceipts: readonly string[];
readonly outcome: "pass" | "fail";
readonly details?: Record<string, unknown>;
}
export interface HumanPromotionInput {
readonly proposalId: string;
readonly runId: string;
readonly actorId: string;
readonly proposalReceiptId: string;
readonly evaluationReceiptId: string;
readonly proposalActorId: string;
readonly evaluationActorId: string;
readonly humanReviewerId: string;
readonly approvedBy: string;
readonly approvalTime: string;
readonly approvalBasis: string;
readonly sourceCommit: string;
readonly policyHash: string;
readonly ciRuns: readonly string[];
readonly manifestSha256: string;
readonly bundleSha256: string;
readonly rollbackRef: string;
readonly publishedSurfaces: readonly string[];
readonly details?: Record<string, unknown>;
}
export interface AutonomousLearningChainResult extends VerifyResult {
readonly proposalCount: number;
readonly evaluationCount: number;
readonly promotionCount: number;
}
function requireNonEmpty(value: string, field: string): string {
if (!value.trim()) {
throw new Error(`${field} is required`);
}
return value;
}
function validateProposal(input: AutonomousLearningProposalInput): void {
requireNonEmpty(input.proposalId, "proposalId");
requireNonEmpty(input.runId, "runId");
requireNonEmpty(input.actorId, "actorId");
requireNonEmpty(input.sourceCommit, "sourceCommit");
requireNonEmpty(input.policyHash, "policyHash");
requireNonEmpty(input.proposalPayloadDigest, "proposalPayloadDigest");
if (!input.lambdaAxes.length) throw new Error("lambdaAxes must be non-empty");
if (!input.sourceIngress.length) throw new Error("sourceIngress must be non-empty");
if (!input.intendedFiles.length) throw new Error("intendedFiles must be non-empty");
if (!input.forbiddenClaimScan.passed || input.forbiddenClaimScan.forbiddenMatches.length > 0) {
throw new Error("forbidden claim scan must pass before proposal receipt emission");
}
}
function validateEvaluation(input: AutonomousLearningEvaluationInput): void {
requireNonEmpty(input.proposalReceiptId, "proposalReceiptId");
requireNonEmpty(input.proposalActorId, "proposalActorId");
requireNonEmpty(input.actorId, "actorId");
if (input.actorId === input.proposalActorId) {
throw new Error("evaluation actor must be distinct from proposal actor");
}
if (input.outcome === "pass" && input.replaySeeds.length < 5) {
throw new Error("passing evaluation requires at least five replay seeds");
}
if (!input.inputRoots.length || !input.outputRoots.length) {
throw new Error("evaluation requires inputRoots and outputRoots");
}
}
function validatePromotion(input: HumanPromotionInput): void {
requireNonEmpty(input.proposalReceiptId, "proposalReceiptId");
requireNonEmpty(input.evaluationReceiptId, "evaluationReceiptId");
requireNonEmpty(input.humanReviewerId, "humanReviewerId");
requireNonEmpty(input.approvedBy, "approvedBy");
requireNonEmpty(input.approvalBasis, "approvalBasis");
if (input.humanReviewerId === input.proposalActorId || input.humanReviewerId === input.evaluationActorId) {
throw new Error("human reviewer must be distinct from proposal and evaluation actors");
}
if (input.actorId !== input.humanReviewerId) {
throw new Error("promotion actor must be the named human reviewer");
}
if (!input.ciRuns.length) throw new Error("promotion requires at least one CI run");
if (!input.publishedSurfaces.length) throw new Error("promotion requires published surfaces");
}
export function createAutonomousLearningProposalEnvelope(input: AutonomousLearningProposalInput): ToolEnvelope {
validateProposal(input);
return createToolEnvelope({
protocol: "a11oy",
actor_id: input.actorId,
tool_name: "autonomous_learning_proposal",
lambda_axes: input.lambdaAxes,
payload: {
schemaVersion: "a11oy.autonomous-learning.proposal.v1",
eventType: "AUTONOMOUS_LEARNING_PROPOSAL",
...input,
},
metadata: {
doctrine: "docs/AUTONOMOUS_LEARNING_DOCTRINE.md",
mode: "dream-evaluate-propose-only",
},
});
}
export function emitAutonomousLearningProposalReceipt(
input: AutonomousLearningProposalInput,
options: EmitReceiptOptions = {},
): OperationalReceipt {
return emitReceipt(createAutonomousLearningProposalEnvelope(input), {
...options,
eventType: "AUTONOMOUS_LEARNING_PROPOSAL",
policy: {
vertical: "autonomous-learning",
regime: "doctrine-v6",
...options.policy,
},
});
}
export function createAutonomousLearningEvaluationEnvelope(input: AutonomousLearningEvaluationInput): ToolEnvelope {
validateEvaluation(input);
return createToolEnvelope({
protocol: "a11oy",
actor_id: input.actorId,
tool_name: "autonomous_learning_evaluation",
lambda_axes: ["measurabilityHonesty", "provenanceIntegrity", "operatorReversibility"],
payload: {
schemaVersion: "a11oy.autonomous-learning.evaluation.v1",
eventType: "AUTONOMOUS_LEARNING_EVALUATION",
...input,
},
metadata: {
doctrine: "docs/AUTONOMOUS_LEARNING_DOCTRINE.md",
replayRequirement: "at-least-five-replays-for-pass",
},
});
}
export function emitAutonomousLearningEvaluationReceipt(
input: AutonomousLearningEvaluationInput,
options: EmitReceiptOptions = {},
): OperationalReceipt {
return emitReceipt(createAutonomousLearningEvaluationEnvelope(input), {
...options,
eventType: "AUTONOMOUS_LEARNING_EVALUATION",
policy: {
vertical: "autonomous-learning",
regime: "doctrine-v6",
...options.policy,
},
});
}
export function createHumanPromotionEnvelope(input: HumanPromotionInput): ToolEnvelope {
validatePromotion(input);
return createToolEnvelope({
protocol: "a11oy",
actor_id: input.actorId,
tool_name: "human_promotion",
lambda_axes: ["moralGrounding", "measurabilityHonesty", "provenanceIntegrity", "operatorReversibility"],
payload: {
schemaVersion: "a11oy.autonomous-learning.human-promotion.v1",
eventType: "HUMAN_PROMOTION",
...input,
},
metadata: {
doctrine: "docs/AUTONOMOUS_LEARNING_DOCTRINE.md",
boundary: "human-promotion-required",
},
});
}
export function emitHumanPromotionReceipt(
input: HumanPromotionInput,
options: EmitReceiptOptions = {},
): OperationalReceipt {
return emitReceipt(createHumanPromotionEnvelope(input), {
...options,
eventType: "HUMAN_PROMOTION",
policy: {
vertical: "autonomous-learning",
regime: "doctrine-v6",
...options.policy,
},
});
}
export function verifyAutonomousLearningChain(chain: readonly OperationalReceipt[]): AutonomousLearningChainResult {
const errors: string[] = [...verifyChain(chain).errors];
const proposals = chain.filter((receipt) => receipt.event_type === "AUTONOMOUS_LEARNING_PROPOSAL");
const evaluations = chain.filter((receipt) => receipt.event_type === "AUTONOMOUS_LEARNING_EVALUATION");
const promotions = chain.filter((receipt) => receipt.event_type === "HUMAN_PROMOTION");
const proposalById = new Map<string, OperationalReceipt>();
for (const receipt of proposals) {
const payload = receipt.envelope.payload as { proposalId?: string };
if (!payload.proposalId) {
errors.push(`${receipt.receipt_id}: proposal missing proposalId`);
continue;
}
proposalById.set(payload.proposalId, receipt);
}
const evaluationById = new Map<string, OperationalReceipt>();
for (const receipt of evaluations) {
const payload = receipt.envelope.payload as {
proposalId?: string;
proposalReceiptId?: string;
outcome?: string;
replaySeeds?: readonly number[];
sourceCommit?: string;
policyHash?: string;
};
if (!payload.proposalId || !payload.proposalReceiptId) {
errors.push(`${receipt.receipt_id}: evaluation missing proposal reference`);
continue;
}
const proposal = proposalById.get(payload.proposalId);
if (!proposal) {
errors.push(`${receipt.receipt_id}: evaluation missing proposal ancestor`);
} else {
const proposalPayload = proposal.envelope.payload as { sourceCommit?: string; policyHash?: string };
if (payload.proposalReceiptId !== proposal.receipt_id) {
errors.push(`${receipt.receipt_id}: proposalReceiptId mismatch`);
}
if (payload.sourceCommit !== proposalPayload.sourceCommit) {
errors.push(`${receipt.receipt_id}: source commit drift requires new proposal`);
}
if (payload.policyHash !== proposalPayload.policyHash) {
errors.push(`${receipt.receipt_id}: policy hash drift requires new proposal`);
}
}
if (payload.outcome === "pass" && (payload.replaySeeds?.length ?? 0) < 5) {
errors.push(`${receipt.receipt_id}: passing evaluation has fewer than five replay seeds`);
}
evaluationById.set(receipt.receipt_id, receipt);
}
for (const receipt of promotions) {
const payload = receipt.envelope.payload as {
proposalId?: string;
proposalReceiptId?: string;
evaluationReceiptId?: string;
humanReviewerId?: string;
proposalActorId?: string;
evaluationActorId?: string;
sourceCommit?: string;
policyHash?: string;
};
const proposal = payload.proposalId ? proposalById.get(payload.proposalId) : undefined;
const evaluation = payload.evaluationReceiptId ? evaluationById.get(payload.evaluationReceiptId) : undefined;
if (!proposal) {
errors.push(`${receipt.receipt_id}: promotion missing proposal ancestor`);
}
if (!evaluation) {
errors.push(`${receipt.receipt_id}: promotion missing evaluation ancestor`);
} else {
const evaluationPayload = evaluation.envelope.payload as { outcome?: string; sourceCommit?: string; policyHash?: string };
if (evaluationPayload.outcome !== "pass") {
errors.push(`${receipt.receipt_id}: promotion requires passing evaluation`);
}
if (payload.sourceCommit !== evaluationPayload.sourceCommit) {
errors.push(`${receipt.receipt_id}: source commit drift after evaluation`);
}
if (payload.policyHash !== evaluationPayload.policyHash) {
errors.push(`${receipt.receipt_id}: policy hash drift after evaluation`);
}
}
if (payload.humanReviewerId === payload.proposalActorId || payload.humanReviewerId === payload.evaluationActorId) {
errors.push(`${receipt.receipt_id}: human reviewer must be distinct`);
}
}
return {
valid: errors.length === 0,
errors,
proposalCount: proposals.length,
evaluationCount: evaluations.length,
promotionCount: promotions.length,
};
}
|