refactor(replay): simplify runtime value checks

This commit is contained in:
morluto
2026-09-29 14:56:54 +08:00
parent 271b1620e6
commit 60cb68a276
3 changed files with 136 additions and 106 deletions
+42 -105
View File
@@ -1,6 +1,7 @@
import { z } from "zod";
import type { ReplayMachine } from "./replayMachine.js";
import { isJsonCompatible, replayValuesEqual } from "./replayMachineValues.js";
type ReplayTransition = ReplayMachine["transitions"][number];
type IndexedReplayTransition = {
@@ -208,88 +209,6 @@ const triggerMatches = (
return true;
};
const replayValuesEqual = (left: unknown, right: unknown): boolean => {
const pending: [unknown, unknown][] = [[left, right]];
while (pending.length > 0) {
const pair = pending.pop();
if (pair === undefined) continue;
const [currentLeft, currentRight] = pair;
if (
currentLeft === null ||
typeof currentLeft === "string" ||
typeof currentLeft === "boolean"
) {
if (currentLeft !== currentRight) return false;
continue;
}
if (typeof currentLeft === "number") {
if (
!Number.isFinite(currentLeft) ||
currentLeft !== currentRight ||
typeof currentRight !== "number" ||
!Number.isFinite(currentRight)
)
return false;
continue;
}
if (Array.isArray(currentLeft)) {
if (
!Array.isArray(currentRight) ||
currentLeft.length !== currentRight.length
)
return false;
for (let index = 0; index < currentLeft.length; index += 1)
pending.push([currentLeft[index], currentRight[index]]);
continue;
}
if (
typeof currentLeft !== "object" ||
currentLeft === null ||
typeof currentRight !== "object" ||
currentRight === null ||
Array.isArray(currentRight)
)
return false;
const leftKeys = Object.keys(currentLeft).sort();
const rightKeys = Object.keys(currentRight).sort();
if (
leftKeys.length !== rightKeys.length ||
leftKeys.some((key, index) => key !== rightKeys[index])
)
return false;
for (const key of leftKeys)
pending.push([
Reflect.get(currentLeft, key),
Reflect.get(currentRight, key),
]);
}
return true;
};
const isJsonCompatible = (value: unknown): boolean => {
const pending: unknown[] = [value];
while (pending.length > 0) {
const candidate = pending.pop();
if (
candidate === null ||
typeof candidate === "string" ||
typeof candidate === "boolean"
)
continue;
if (typeof candidate === "number") {
if (!Number.isFinite(candidate)) return false;
continue;
}
if (Array.isArray(candidate)) {
for (const item of candidate) pending.push(item);
continue;
}
if (typeof candidate !== "object") return false;
for (const item of Object.values(candidate)) pending.push(item);
}
return true;
};
const createTransitionRecord = ({
sequence,
atMs,
@@ -413,32 +332,14 @@ export class ReplayMachineRuntime {
};
if (!this.#admitEvent(normalizedEvent))
return this.#refusal("limit_exhausted");
const matching = (
this.#transitionsByTrigger.get(
`${normalizedEvent.protocol}\0${normalizedEvent.path}`,
) ?? []
).filter(({ transition }) => triggerMatches(transition, normalizedEvent));
const eligible = matching.filter(
({ transition }) => transition.from === this.#state,
const matching = this.#matchingTransitions(normalizedEvent);
const eligible = matching.filter(({ transition }) =>
this.#transitionStartsHere(transition),
);
if (eligible.length === 0)
return this.#refusal(
matching.length === 0
? "unmatched"
: normalizedEvent.protocol === "websocket_connect" &&
normalizedEvent.connection === "reconnect"
? "unexpected_reconnect"
: "invalid_state",
);
return this.#refusal(this.#noEligibleReason(matching, normalizedEvent));
const guarded = eligible.filter(({ transition }) =>
transition.guards.every(
(guard) =>
this.#variables.has(guard.variable) &&
replayValuesEqual(
this.#variables.get(guard.variable),
readValue(guard.value, normalizedEvent, transition),
),
),
this.#transitionGuardsMatch(transition, normalizedEvent),
);
if (guarded.length === 0) return this.#refusal("guard_failed");
if (this.#timeline.length >= this.machine.max_transitions)
@@ -494,6 +395,42 @@ export class ReplayMachineRuntime {
};
}
#matchingTransitions(
event: ReplayMachineEvent,
): readonly IndexedReplayTransition[] {
return (
this.#transitionsByTrigger.get(`${event.protocol}\0${event.path}`) ?? []
).filter(({ transition }) => triggerMatches(transition, event));
}
#transitionStartsHere(transition: ReplayTransition): boolean {
return transition.from === this.#state;
}
#noEligibleReason(
matching: readonly IndexedReplayTransition[],
event: ReplayMachineEvent,
): "unmatched" | "unexpected_reconnect" | "invalid_state" {
if (matching.length === 0) return "unmatched";
return event.protocol === "websocket_connect" &&
event.connection === "reconnect"
? "unexpected_reconnect"
: "invalid_state";
}
#transitionGuardsMatch(
transition: ReplayTransition,
event: ReplayMachineEvent,
): boolean {
return transition.guards.every((guard) => {
if (!this.#variables.has(guard.variable)) return false;
return replayValuesEqual(
this.#variables.get(guard.variable),
readValue(guard.value, event, transition),
);
});
}
#admitEvent(event: ReplayMachineEvent): boolean {
const connections =
this.#connections + (event.protocol === "websocket_connect" ? 1 : 0);
+93
View File
@@ -0,0 +1,93 @@
/** Compare JSON-compatible replay values without recursive stack growth. */
export const replayValuesEqual = (left: unknown, right: unknown): boolean => {
const pending: [unknown, unknown][] = [[left, right]];
while (pending.length > 0) {
const pair = pending.pop();
if (pair === undefined) continue;
const [currentLeft, currentRight] = pair;
const primitive = equalPrimitiveReplayValues(currentLeft, currentRight);
if (primitive !== undefined) {
if (!primitive) return false;
continue;
}
const array = compareReplayArrays(currentLeft, currentRight, pending);
if (array !== undefined) {
if (!array) return false;
continue;
}
if (!compareReplayRecords(currentLeft, currentRight, pending)) return false;
}
return true;
};
/** Check that captured values can be serialized as JSON without recursion. */
export const isJsonCompatible = (value: unknown): boolean => {
const pending: unknown[] = [value];
while (pending.length > 0) {
const candidate = pending.pop();
if (
candidate === null ||
typeof candidate === "string" ||
typeof candidate === "boolean"
)
continue;
if (typeof candidate === "number") {
if (!Number.isFinite(candidate)) return false;
continue;
}
if (Array.isArray(candidate)) {
for (const item of candidate) pending.push(item);
continue;
}
if (typeof candidate !== "object") return false;
for (const item of Object.values(candidate)) pending.push(item);
}
return true;
};
const equalPrimitiveReplayValues = (
left: unknown,
right: unknown,
): boolean | undefined => {
if (left === null || typeof left === "string" || typeof left === "boolean")
return left === right;
if (typeof left !== "number") return undefined;
return (
Number.isFinite(left) &&
left === right &&
typeof right === "number" &&
Number.isFinite(right)
);
};
const compareReplayArrays = (
left: unknown,
right: unknown,
pending: [unknown, unknown][],
): boolean | undefined => {
if (!Array.isArray(left)) return undefined;
if (!Array.isArray(right) || left.length !== right.length) return false;
for (let index = 0; index < left.length; index += 1)
pending.push([left[index], right[index]]);
return true;
};
const compareReplayRecords = (
left: unknown,
right: unknown,
pending: [unknown, unknown][],
): boolean | undefined => {
if (typeof left !== "object" || left === null) return undefined;
if (typeof right !== "object" || right === null || Array.isArray(right))
return false;
const leftKeys = Object.keys(left).sort();
const rightKeys = Object.keys(right).sort();
if (
leftKeys.length !== rightKeys.length ||
leftKeys.some((key, index) => key !== rightKeys[index])
)
return false;
for (const key of leftKeys)
pending.push([Reflect.get(left, key), Reflect.get(right, key)]);
return true;
};
+1 -1
View File
@@ -7,7 +7,7 @@ import {
} from "../application/EvidenceBundleFiles.js";
import { SESSION_TOOL_CONTRACTS } from "../contracts/toolContracts.js";
import type { EvidenceBundle } from "../domain/evidenceBundle.js";
import { err, ok } from "../domain/result.js";
import { ok } from "../domain/result.js";
import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
import { toCallToolResult } from "./toolResult.js";