File size: 5,198 Bytes
fc93158 | 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 | import { describe, expect, it } from "vitest";
import type { SkynetCausalEpisode } from "../causal-valence/episode-ledger.js";
import {
encodeSkynetRuntimeTrajectoryFeatures,
type SkynetRuntimeTrajectorySample,
} from "../runtime-observer/trajectory-builder.js";
import {
createSkynetCognitiveKernelState,
observeSkynetCognitiveKernelSample,
predictSkynetCognitiveKernelLabel,
replaySkynetCognitiveKernelState,
updateSkynetCognitiveKernelState,
} from "./min-kernel.js";
function buildEpisode(params: {
id: string;
label: SkynetCausalEpisode["bootstrapLabel"];
kind: "edit" | "delete" | "create" | "noop";
status?: "ok" | "error" | "timeout";
failureStreak?: number;
}): SkynetCausalEpisode {
return {
id: params.id,
sessionKey: "agent:openskynet:main",
recordedAt: Number(params.id.replace(/\D+/g, "")) || 1,
context: {
continuityFreshness: "fresh",
failureStreak: params.failureStreak ?? 0,
targetCount: 1,
validationIntensity: params.kind === "edit" ? 0.8 : 0.3,
},
transition: {
operations: [{ path: `/tmp/${params.id}.ts`, kind: params.kind, isTarget: true }],
targetPaths: [`/tmp/${params.id}.ts`],
},
outcome: {
status: params.status ?? (params.label === "damage" ? "error" : "ok"),
failureDomain:
params.label === "damage"
? "cognitive"
: params.status === "timeout"
? "environmental"
: "none",
failureClass:
params.label === "damage"
? "validation_error"
: params.status === "timeout"
? "provider_timeout"
: "none",
targetSatisfied: params.label !== "stall" && params.label !== "damage",
validationPassed: params.label !== "damage",
continuityDelta: params.label === "progress" || params.label === "relief" ? 0.7 : 0.05,
recoveryBurden: params.label === "frustration" ? 0.6 : 0.1,
collateralDamage: params.label === "damage" ? 0.8 : 0.05,
},
bootstrapLabel: params.label,
};
}
function buildSample(
id: string,
label: SkynetCausalEpisode["bootstrapLabel"],
kind: "edit" | "delete" | "create" | "noop",
historyLabels: SkynetCausalEpisode["bootstrapLabel"][] = [],
): SkynetRuntimeTrajectorySample {
const historyEpisodes = historyLabels.map((historyLabel, index) =>
buildEpisode({
id: `${id}-history-${index}`,
label: historyLabel,
kind: historyLabel === "damage" ? "delete" : "edit",
failureStreak: historyLabel === "frustration" ? 2 : 0,
}),
);
const currentEpisode = buildEpisode({
id,
label,
kind,
failureStreak: label === "frustration" ? 2 : 0,
});
return {
id,
sessionKey: "agent:openskynet:main",
recordedAt: currentEpisode.recordedAt,
lookbackCount: historyEpisodes.length,
historyEpisodes,
currentEpisode,
targetLabel: label,
};
}
describe("skynet cognitive kernel", () => {
it("learns separable trajectory prototypes online", () => {
const progressSeed = buildSample("10", "progress", "edit", ["progress", "progress"]);
const damageSeed = buildSample("20", "damage", "delete", ["damage", "frustration"]);
const state0 = createSkynetCognitiveKernelState({
featureDimensions: encodeSkynetRuntimeTrajectoryFeatures(progressSeed).length,
});
const state1 = observeSkynetCognitiveKernelSample(state0, progressSeed);
const state2 = observeSkynetCognitiveKernelSample(state1, damageSeed);
const progressPrediction = predictSkynetCognitiveKernelLabel(
state2,
buildSample("30", "progress", "edit", ["progress", "relief"]),
);
const damagePrediction = predictSkynetCognitiveKernelLabel(
state2,
buildSample("40", "damage", "delete", ["damage", "frustration"]),
);
expect(progressPrediction.label).toBe("progress");
expect(damagePrediction.label).toBe("damage");
expect(progressPrediction.surprise).toBeLessThan(0.8);
});
it("can replay state from a sample stream", () => {
const samples = [
buildSample("10", "progress", "edit", ["progress"]),
buildSample("20", "damage", "delete", ["damage"]),
buildSample("30", "progress", "edit", ["progress", "relief"]),
];
const state = replaySkynetCognitiveKernelState({ samples });
expect(state).not.toBeNull();
expect(state?.observedCount).toBe(3);
expect(state?.prototypeCounts.progress).toBe(2);
});
it("updates incrementally without double-counting seen samples", () => {
const samples = [
buildSample("10", "progress", "edit", ["progress"]),
buildSample("20", "damage", "delete", ["damage"]),
];
const state0 = createSkynetCognitiveKernelState({
featureDimensions: encodeSkynetRuntimeTrajectoryFeatures(samples[0]!).length,
});
const first = updateSkynetCognitiveKernelState({ state: state0, samples });
const second = updateSkynetCognitiveKernelState({ state: first.state, samples });
expect(first.ingestedCount).toBe(2);
expect(first.skippedCount).toBe(0);
expect(second.ingestedCount).toBe(0);
expect(second.skippedCount).toBe(2);
expect(second.state.observedCount).toBe(2);
});
});
|