feat(process): add reactive capture scenarios
This commit is contained in:
@@ -5,6 +5,11 @@ import {
|
||||
LEGACY_PROCESS_CAPTURE_MESSAGE,
|
||||
normalizationSchema,
|
||||
} from "./processScenario.js";
|
||||
import type {
|
||||
ProcessReactiveOutcome,
|
||||
ProcessReactiveTransitionRecord,
|
||||
} from "./processReactiveRuntime.js";
|
||||
import { PROCESS_REACTIVE_LIMITS } from "./processReactiveScenario.js";
|
||||
import type { ReplayTransitionRecord } from "./replayMachineRuntime.js";
|
||||
|
||||
export * from "./processScenario.js";
|
||||
@@ -133,6 +138,18 @@ export interface ProcessCaptureEventJournalEntry {
|
||||
readonly index: number;
|
||||
}
|
||||
|
||||
/** Ordered control input that terminated or superseded a reactive run. */
|
||||
export interface ProcessReactiveControlRecord {
|
||||
readonly sequence: number;
|
||||
readonly kind:
|
||||
| "state_deadline"
|
||||
| "scenario_deadline"
|
||||
| "target_lost"
|
||||
| "cancelled"
|
||||
| "cleanup_failed";
|
||||
readonly after_capture_order: number;
|
||||
}
|
||||
|
||||
/** Shared observation callback used by every process-capture producer. */
|
||||
export type RecordProcessCaptureEvent = (
|
||||
collection: ProcessCaptureEventCollection,
|
||||
@@ -185,6 +202,13 @@ export interface ProcessCapture {
|
||||
readonly shim_events: readonly ShimEvent[];
|
||||
readonly protocol_events: readonly ProtocolEvent[];
|
||||
readonly replay_transitions: readonly ReplayTransitionRecord[];
|
||||
readonly reactive_run: {
|
||||
readonly status: "running" | "finished";
|
||||
readonly outcome: ProcessReactiveOutcome | null;
|
||||
readonly active_state: string;
|
||||
readonly transitions: readonly ProcessReactiveTransitionRecord[];
|
||||
readonly controls: readonly ProcessReactiveControlRecord[];
|
||||
} | null;
|
||||
/**
|
||||
* Global observation order across independently recorded collections.
|
||||
*
|
||||
@@ -363,6 +387,75 @@ export const processCaptureSchema: z.ZodType<ProcessCapture> = z.object({
|
||||
}),
|
||||
)
|
||||
.default([]),
|
||||
reactive_run: z
|
||||
.object({
|
||||
status: z.enum(["running", "finished"]),
|
||||
outcome: z
|
||||
.enum([
|
||||
"passed",
|
||||
"predicate_timeout",
|
||||
"scenario_deadline",
|
||||
"ambiguous_match",
|
||||
"action_rejected",
|
||||
"target_lost",
|
||||
"capture_incomplete",
|
||||
"cancelled",
|
||||
"cleanup_failed",
|
||||
])
|
||||
.nullable(),
|
||||
active_state: z.string().min(1).max(64),
|
||||
controls: z
|
||||
.array(
|
||||
z.object({
|
||||
sequence: z.number().int().nonnegative(),
|
||||
kind: z.enum([
|
||||
"state_deadline",
|
||||
"scenario_deadline",
|
||||
"target_lost",
|
||||
"cancelled",
|
||||
"cleanup_failed",
|
||||
]),
|
||||
after_capture_order: z.number().int().min(-1),
|
||||
}),
|
||||
)
|
||||
.default([]),
|
||||
transitions: z
|
||||
.array(
|
||||
z.object({
|
||||
sequence: z.number().int().nonnegative(),
|
||||
transition_id: z.string().min(1).max(64),
|
||||
state_before: z.string().min(1).max(64),
|
||||
state_after: z.string().min(1).max(64).nullable(),
|
||||
outcome: z
|
||||
.enum([
|
||||
"passed",
|
||||
"predicate_timeout",
|
||||
"scenario_deadline",
|
||||
"ambiguous_match",
|
||||
"action_rejected",
|
||||
"target_lost",
|
||||
"capture_incomplete",
|
||||
"cancelled",
|
||||
"cleanup_failed",
|
||||
])
|
||||
.nullable(),
|
||||
trigger_event_ids: z
|
||||
.array(z.string().min(1))
|
||||
.max(PROCESS_REACTIVE_LIMITS.predicates),
|
||||
action_event_ids: z
|
||||
.array(z.string().min(1))
|
||||
.max(PROCESS_REACTIVE_LIMITS.actionsPerTransition),
|
||||
action_types: z
|
||||
.array(
|
||||
z.enum(["send_input", "resize", "send_signal", "checkpoint"]),
|
||||
)
|
||||
.max(PROCESS_REACTIVE_LIMITS.actionsPerTransition),
|
||||
}),
|
||||
)
|
||||
.max(PROCESS_REACTIVE_LIMITS.transitions),
|
||||
})
|
||||
.nullable()
|
||||
.default(null),
|
||||
event_journal: z
|
||||
.array(
|
||||
z.object({
|
||||
|
||||
@@ -0,0 +1,273 @@
|
||||
import canonicalize from "canonicalize";
|
||||
|
||||
import type { ProcessCapture } from "./processCapture.js";
|
||||
import {
|
||||
projectProcessObservation,
|
||||
type ProcessObservation,
|
||||
} from "./processObservation.js";
|
||||
import {
|
||||
commitProcessReactiveProposal,
|
||||
createProcessReactiveSnapshot,
|
||||
reduceProcessReactiveScenario,
|
||||
type ProcessReactiveDecision,
|
||||
type ProcessReactiveInput,
|
||||
type ProcessReactiveSnapshot,
|
||||
} from "./processReactiveRuntime.js";
|
||||
import {
|
||||
processReactiveScenarioSchema,
|
||||
type ProcessReactiveScenario,
|
||||
} from "./processReactiveScenario.js";
|
||||
|
||||
type RequireInvariant = (
|
||||
condition: boolean,
|
||||
path: string,
|
||||
message: string,
|
||||
) => void;
|
||||
type ReactiveRun = NonNullable<ProcessCapture["reactive_run"]>;
|
||||
type ReactiveTransition = ReactiveRun["transitions"][number];
|
||||
|
||||
const admittedCollection = (
|
||||
collection: NonNullable<
|
||||
ProcessCapture["event_journal"]
|
||||
>[number]["collection"],
|
||||
): boolean =>
|
||||
[
|
||||
"frames",
|
||||
"interaction_events",
|
||||
"process_samples",
|
||||
"filesystem_checkpoints",
|
||||
"shim_events",
|
||||
"protocol_events",
|
||||
].includes(collection);
|
||||
|
||||
const reactiveObservations = (
|
||||
capture: ProcessCapture,
|
||||
): readonly ProcessObservation[] =>
|
||||
(capture.event_journal ?? []).flatMap((location) => {
|
||||
if (!admittedCollection(location.collection)) return [];
|
||||
const observation = projectProcessObservation(capture, location);
|
||||
return observation === null ? [] : [observation];
|
||||
});
|
||||
|
||||
const validateTransitionDeclaration = (
|
||||
transition: ReactiveTransition,
|
||||
next: ReactiveTransition | undefined,
|
||||
context: {
|
||||
readonly index: number;
|
||||
readonly scenario: ProcessReactiveScenario;
|
||||
readonly require: RequireInvariant;
|
||||
},
|
||||
): void => {
|
||||
const { index, scenario, require } = context;
|
||||
const path = `reactive_run.transitions[${String(index)}]`;
|
||||
const declared = scenario.states
|
||||
.find(({ id }) => id === transition.state_before)
|
||||
?.on.find(({ id }) => id === transition.transition_id);
|
||||
require(declared !==
|
||||
undefined, path, "reactive transition is not declared by its recorded source state");
|
||||
if (declared !== undefined) {
|
||||
const stateAfter =
|
||||
declared.target.kind === "goto" ? declared.target.state : null;
|
||||
const outcome =
|
||||
declared.target.kind === "finish" ? declared.target.outcome : null;
|
||||
require(transition.state_after === stateAfter &&
|
||||
transition.outcome ===
|
||||
outcome, path, "reactive transition target or outcome differs from the committed graph");
|
||||
require(canonicalize(transition.action_types) ===
|
||||
canonicalize(
|
||||
declared.actions.map(({ type }) => type),
|
||||
), `${path}.action_types`, "reactive transition actions differ from the committed graph");
|
||||
}
|
||||
require(transition.sequence ===
|
||||
index, `${path}.sequence`, "reactive transition sequence must be contiguous from zero");
|
||||
if (next !== undefined)
|
||||
require(transition.state_after ===
|
||||
next.state_before, `reactive_run.transitions[${String(index + 1)}].state_before`, "reactive transition states must be contiguous");
|
||||
};
|
||||
|
||||
const validateTransitionDeclarations = (
|
||||
run: ReactiveRun,
|
||||
scenario: ProcessReactiveScenario,
|
||||
require: RequireInvariant,
|
||||
): void => {
|
||||
require(run.transitions[0]?.state_before === scenario.initial_state ||
|
||||
run.transitions.length ===
|
||||
0, "reactive_run.transitions[0].state_before", "reactive transition journal must start at the declared initial state");
|
||||
for (const [index, transition] of run.transitions.entries())
|
||||
validateTransitionDeclaration(transition, run.transitions[index + 1], {
|
||||
index,
|
||||
scenario,
|
||||
require,
|
||||
});
|
||||
};
|
||||
|
||||
const commitRecordedEffects = (options: {
|
||||
readonly scenario: ProcessReactiveScenario;
|
||||
readonly proposal: Extract<
|
||||
ProcessReactiveDecision,
|
||||
{ readonly kind: "proposal" }
|
||||
>;
|
||||
readonly expected: ReactiveTransition | undefined;
|
||||
readonly observations: ReadonlyMap<string, ProcessObservation>;
|
||||
readonly recordedFailure: "action_rejected" | "target_lost" | undefined;
|
||||
}): ProcessReactiveDecision =>
|
||||
commitProcessReactiveProposal(
|
||||
options.scenario,
|
||||
options.proposal,
|
||||
options.recordedFailure === undefined
|
||||
? options.proposal.effects.map((_, index) => {
|
||||
const eventId = options.expected?.action_event_ids[index];
|
||||
const observation =
|
||||
eventId === undefined
|
||||
? undefined
|
||||
: options.observations.get(eventId);
|
||||
return observation === undefined
|
||||
? { status: "rejected" as const }
|
||||
: { status: "succeeded" as const, observation };
|
||||
})
|
||||
: [
|
||||
options.recordedFailure === "target_lost"
|
||||
? { status: "target_lost" as const }
|
||||
: { status: "rejected" as const },
|
||||
],
|
||||
{ committedSensitiveInputs: true },
|
||||
);
|
||||
|
||||
const replayTransitions = (
|
||||
scenario: ProcessReactiveScenario,
|
||||
run: ReactiveRun,
|
||||
observations: readonly ProcessObservation[],
|
||||
require: RequireInvariant,
|
||||
): ProcessReactiveSnapshot => {
|
||||
const observationsById = new Map(
|
||||
observations.map((observation) => [observation.event_id, observation]),
|
||||
);
|
||||
let snapshot = createProcessReactiveSnapshot(scenario);
|
||||
let transitionIndex = 0;
|
||||
const advance = (input: ProcessReactiveInput): void => {
|
||||
let decision = reduceProcessReactiveScenario(scenario, snapshot, input);
|
||||
if (decision.kind === "proposal") {
|
||||
const proposal = decision;
|
||||
decision = commitRecordedEffects({
|
||||
scenario,
|
||||
proposal: decision,
|
||||
expected: run.transitions[transitionIndex],
|
||||
observations: observationsById,
|
||||
recordedFailure:
|
||||
run.transitions[transitionIndex] === undefined &&
|
||||
decision.effects.length > 0 &&
|
||||
(run.outcome === "action_rejected" || run.outcome === "target_lost")
|
||||
? run.outcome
|
||||
: undefined,
|
||||
});
|
||||
require(decision.kind !== "finished" ||
|
||||
decision.outcome !== "action_rejected" ||
|
||||
run.outcome ===
|
||||
"action_rejected", `reactive_run.transitions[${String(transitionIndex)}].action_event_ids`, `recorded action observations do not match proposed effects: ${canonicalize(
|
||||
{
|
||||
effects: proposal.effects,
|
||||
observations: run.transitions[transitionIndex]?.action_event_ids.map(
|
||||
(eventId) => observationsById.get(eventId),
|
||||
),
|
||||
},
|
||||
)}`);
|
||||
}
|
||||
if (decision.kind === "transition") {
|
||||
const expected = run.transitions[transitionIndex];
|
||||
require(expected !== undefined &&
|
||||
canonicalize(decision.record) ===
|
||||
canonicalize(
|
||||
expected,
|
||||
), `reactive_run.transitions[${String(transitionIndex)}]`, "reactive transition differs from deterministic journal replay");
|
||||
transitionIndex += 1;
|
||||
}
|
||||
snapshot = decision.snapshot;
|
||||
};
|
||||
const advanceControl = (
|
||||
control: ReactiveRun["controls"][number],
|
||||
index: number,
|
||||
): void => {
|
||||
require(snapshot.status !== "finished" ||
|
||||
control.kind ===
|
||||
"cleanup_failed", `reactive_run.controls[${String(index)}]`, "reactive control cannot follow a finished run");
|
||||
advance(
|
||||
control.kind === "state_deadline"
|
||||
? {
|
||||
kind: control.kind,
|
||||
state_id: snapshot.active_state,
|
||||
state_entry_capture_order: snapshot.state_entry_capture_order,
|
||||
}
|
||||
: { kind: control.kind },
|
||||
);
|
||||
};
|
||||
let controlIndex = 0;
|
||||
for (const observation of observations) {
|
||||
while (
|
||||
(run.controls[controlIndex]?.after_capture_order ??
|
||||
Number.MAX_SAFE_INTEGER) < observation.capture_order
|
||||
) {
|
||||
const control = run.controls[controlIndex]!;
|
||||
advanceControl(control, controlIndex);
|
||||
controlIndex += 1;
|
||||
}
|
||||
advance({ kind: "observation", observation });
|
||||
}
|
||||
for (; controlIndex < run.controls.length; controlIndex += 1) {
|
||||
const control = run.controls[controlIndex]!;
|
||||
advanceControl(control, controlIndex);
|
||||
}
|
||||
require(transitionIndex ===
|
||||
run.transitions
|
||||
.length, "reactive_run.transitions", `reactive transition journal is not reproduced by deterministic replay (replayed ${String(transitionIndex)} of ${String(run.transitions.length)}; outcome ${snapshot.outcome})`);
|
||||
return snapshot;
|
||||
};
|
||||
|
||||
const validateControls = (
|
||||
run: ReactiveRun,
|
||||
observations: readonly ProcessObservation[],
|
||||
require: RequireInvariant,
|
||||
): void => {
|
||||
const admittedOrders = new Set([
|
||||
-1,
|
||||
...observations.map(({ capture_order }) => capture_order),
|
||||
]);
|
||||
for (const [index, control] of run.controls.entries()) {
|
||||
require(control.sequence ===
|
||||
index, `reactive_run.controls[${String(index)}].sequence`, "reactive control sequence must be contiguous");
|
||||
require(admittedOrders.has(
|
||||
control.after_capture_order,
|
||||
), `reactive_run.controls[${String(index)}].after_capture_order`, "reactive control cutoff must identify the last admitted observation");
|
||||
require(index === 0 ||
|
||||
control.after_capture_order >=
|
||||
run.controls[index - 1]!
|
||||
.after_capture_order, `reactive_run.controls[${String(index)}].after_capture_order`, "reactive control cutoffs must be ordered");
|
||||
}
|
||||
};
|
||||
|
||||
/** Validate one capture's reactive journal by replaying the production reducer. */
|
||||
export const validateProcessCaptureReactiveRun = (
|
||||
capture: ProcessCapture,
|
||||
require: RequireInvariant,
|
||||
): void => {
|
||||
const declared = capture.manifest.scenario["reactive"];
|
||||
const run = capture.reactive_run;
|
||||
const declaredReactive = declared !== undefined && declared !== null;
|
||||
require(declaredReactive ===
|
||||
(run !==
|
||||
null), "reactive_run", "reactive result presence must match the committed scenario");
|
||||
if (run === null) return;
|
||||
const parsed = processReactiveScenarioSchema.safeParse(declared);
|
||||
require(parsed.success, "manifest.scenario.reactive", "committed reactive scenario does not satisfy its declared schema");
|
||||
if (!parsed.success) return;
|
||||
require(run.status === "finished" &&
|
||||
run.outcome !==
|
||||
null, "reactive_run.outcome", "completed capture requires a finished reactive outcome");
|
||||
validateTransitionDeclarations(run, parsed.data, require);
|
||||
const observations = reactiveObservations(capture);
|
||||
validateControls(run, observations, require);
|
||||
const snapshot = replayTransitions(parsed.data, run, observations, require);
|
||||
require(snapshot.outcome ===
|
||||
run.outcome, "reactive_run.outcome", "reactive outcome differs from deterministic journal replay");
|
||||
require(snapshot.active_state ===
|
||||
run.active_state, "reactive_run.active_state", "reactive active state differs from deterministic journal replay");
|
||||
};
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { ProcessCapture } from "./processCapture.js";
|
||||
import { validateProcessCaptureReactiveRun } from "./processCaptureReactiveValidation.js";
|
||||
import { replayMachineSchema, type ReplayMachine } from "./replayMachine.js";
|
||||
import { digestProcessCommitment } from "./processScenario.js";
|
||||
|
||||
@@ -314,5 +315,6 @@ export const validateProcessCapture = (
|
||||
validateLifecycle(capture, require);
|
||||
validateShimEvents(capture, require);
|
||||
validateReplayEvents(capture, require);
|
||||
validateProcessCaptureReactiveRun(capture, require);
|
||||
return issues;
|
||||
};
|
||||
|
||||
@@ -109,16 +109,29 @@ const atMilliseconds = (value: unknown): number | null => {
|
||||
return null;
|
||||
};
|
||||
|
||||
const subjectFor = (
|
||||
/** Derive the stable capture-local identity for one normalized observation. */
|
||||
export const processObservationSubject = (
|
||||
source: ProcessObservationSource,
|
||||
value: JsonValue,
|
||||
value: unknown,
|
||||
): string | null => {
|
||||
if (typeof value !== "object" || value === null || Array.isArray(value))
|
||||
return null;
|
||||
if (source === "process" && typeof value["pid"] === "number")
|
||||
return `process:${String(value["pid"])}`;
|
||||
if (source === "filesystem" && typeof value["name"] === "string")
|
||||
return `checkpoint:${value["name"]}`;
|
||||
if (source === "process" && "pid" in value && typeof value.pid === "number")
|
||||
return `process:${String(value.pid)}`;
|
||||
if (
|
||||
source === "filesystem" &&
|
||||
"name" in value &&
|
||||
typeof value.name === "string"
|
||||
)
|
||||
return `checkpoint:${value.name}`;
|
||||
if (
|
||||
source === "shim" &&
|
||||
"command" in value &&
|
||||
typeof value.command === "string"
|
||||
)
|
||||
return "route_index" in value && typeof value.route_index === "number"
|
||||
? `shim:${value.command}:route:${String(value.route_index)}`
|
||||
: `shim:${value.command}:unmatched`;
|
||||
return null;
|
||||
};
|
||||
|
||||
@@ -188,7 +201,7 @@ const projectedRecord = (
|
||||
return {
|
||||
source: projected.source,
|
||||
captured_at_ms: atMilliseconds(payload),
|
||||
subject_id: subjectFor(projected.source, payload),
|
||||
subject_id: processObservationSubject(projected.source, payload),
|
||||
payload,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -65,6 +65,7 @@ export type ProcessReactiveInput =
|
||||
readonly state_entry_capture_order: number;
|
||||
}
|
||||
| { readonly kind: "scenario_deadline" }
|
||||
| { readonly kind: "target_lost" }
|
||||
| { readonly kind: "cancelled" }
|
||||
| { readonly kind: "cleanup_failed" };
|
||||
|
||||
@@ -123,6 +124,33 @@ export type ProcessReactiveDecision =
|
||||
const counterValue = (counters: readonly Counter[], id: string): number =>
|
||||
counters.find((counter) => counter.id === id)?.count ?? 0;
|
||||
|
||||
type ObservationAdmission =
|
||||
| { readonly kind: "new"; readonly snapshot: ProcessReactiveSnapshot }
|
||||
| { readonly kind: "invalid" };
|
||||
|
||||
const observeInput = (
|
||||
snapshot: ProcessReactiveSnapshot,
|
||||
observation: ProcessObservation,
|
||||
): ObservationAdmission => {
|
||||
const duplicate = snapshot.observations.find(
|
||||
({ event_id }) => event_id === observation.event_id,
|
||||
);
|
||||
if (duplicate !== undefined) return { kind: "invalid" };
|
||||
const prior = snapshot.observations.at(-1);
|
||||
if (
|
||||
(prior !== undefined && observation.capture_order <= prior.capture_order) ||
|
||||
snapshot.observations.length >= PROCESS_REACTIVE_LIMITS.retainedObservations
|
||||
)
|
||||
return { kind: "invalid" };
|
||||
return {
|
||||
kind: "new",
|
||||
snapshot: {
|
||||
...snapshot,
|
||||
observations: [...snapshot.observations, observation],
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
/** Create the initial immutable reducer state for one validated scenario. */
|
||||
export const createProcessReactiveSnapshot = (
|
||||
scenario: ProcessReactiveScenario,
|
||||
@@ -166,6 +194,8 @@ const reduceControlInput = (
|
||||
: { kind: "waiting", snapshot, evaluations: [] };
|
||||
if (input.kind === "scenario_deadline")
|
||||
return finishedDecision(snapshot, "scenario_deadline");
|
||||
if (input.kind === "target_lost")
|
||||
return finishedDecision(snapshot, "target_lost");
|
||||
return finishedDecision(snapshot, "cancelled");
|
||||
};
|
||||
|
||||
@@ -179,23 +209,10 @@ export const reduceProcessReactiveScenario = (
|
||||
if (controlDecision !== null) return controlDecision;
|
||||
if (input.kind !== "observation")
|
||||
return finishedDecision(snapshot, "capture_incomplete");
|
||||
const prior = snapshot.observations.at(-1);
|
||||
if (
|
||||
(prior !== undefined &&
|
||||
input.observation.capture_order <= prior.capture_order) ||
|
||||
snapshot.observations.some(
|
||||
({ event_id }) => event_id === input.observation.event_id,
|
||||
)
|
||||
)
|
||||
const admitted = observeInput(snapshot, input.observation);
|
||||
if (admitted.kind === "invalid")
|
||||
return finishedDecision(snapshot, "capture_incomplete");
|
||||
if (
|
||||
snapshot.observations.length >= PROCESS_REACTIVE_LIMITS.retainedObservations
|
||||
)
|
||||
return finishedDecision(snapshot, "capture_incomplete");
|
||||
const observed = {
|
||||
...snapshot,
|
||||
observations: [...snapshot.observations, input.observation],
|
||||
};
|
||||
const observed = admitted.snapshot;
|
||||
const state = scenario.states.find(({ id }) => id === observed.active_state);
|
||||
if (state === undefined) return finishedDecision(observed, "target_lost");
|
||||
const budget = { remaining: PROCESS_REACTIVE_LIMITS.evaluationWork };
|
||||
|
||||
@@ -124,6 +124,7 @@ const applyProcessReactiveTransition = (input: {
|
||||
const effectObservationMatchesAction = (
|
||||
action: ProcessReactiveScenario["states"][number]["on"][number]["actions"][number],
|
||||
observation: ProcessObservation,
|
||||
committedSensitiveInputs: boolean,
|
||||
): boolean => {
|
||||
const payload =
|
||||
typeof observation.payload === "object" &&
|
||||
@@ -153,9 +154,11 @@ const effectObservationMatchesAction = (
|
||||
return (
|
||||
payload["type"] === "input" &&
|
||||
payload["data"] ===
|
||||
(action.sensitive
|
||||
? `<redacted-input:${String(Buffer.byteLength(action.data))}-bytes>`
|
||||
: action.data)
|
||||
expectedInputEvidence(
|
||||
action.data,
|
||||
action.sensitive,
|
||||
committedSensitiveInputs,
|
||||
)
|
||||
);
|
||||
if (action.type === "resize")
|
||||
return (
|
||||
@@ -165,6 +168,16 @@ const effectObservationMatchesAction = (
|
||||
return payload["type"] === "signal" && payload["data"] === action.signal;
|
||||
};
|
||||
|
||||
const expectedInputEvidence = (
|
||||
data: string,
|
||||
sensitive: boolean,
|
||||
committedSensitiveInput: boolean,
|
||||
): string =>
|
||||
!sensitive ||
|
||||
(committedSensitiveInput && /^<redacted-input:\d+-bytes>$/.test(data))
|
||||
? data
|
||||
: `<redacted-input:${String(Buffer.byteLength(data))}-bytes>`;
|
||||
|
||||
const failedProposal = (
|
||||
proposed: Extract<ProcessReactiveDecision, { readonly kind: "proposal" }>,
|
||||
outcome: "action_rejected" | "target_lost",
|
||||
@@ -180,6 +193,7 @@ export const commitProcessReactiveProposal = (
|
||||
scenario: ProcessReactiveScenario,
|
||||
proposed: Extract<ProcessReactiveDecision, { readonly kind: "proposal" }>,
|
||||
results: readonly ProcessReactiveEffectResult[],
|
||||
options: { readonly committedSensitiveInputs?: boolean } = {},
|
||||
): ProcessReactiveDecision => {
|
||||
if (results.some(({ status }) => status === "target_lost"))
|
||||
return failedProposal(proposed, "target_lost");
|
||||
@@ -227,7 +241,11 @@ export const commitProcessReactiveProposal = (
|
||||
const effectObservation = result.observation;
|
||||
if (
|
||||
action === undefined ||
|
||||
!effectObservationMatchesAction(action, effectObservation) ||
|
||||
!effectObservationMatchesAction(
|
||||
action,
|
||||
effectObservation,
|
||||
options.committedSensitiveInputs ?? false,
|
||||
) ||
|
||||
!Number.isSafeInteger(effectObservation.capture_order) ||
|
||||
effectObservation.capture_order <= previousOrder ||
|
||||
knownEventIds.has(effectObservation.event_id)
|
||||
|
||||
@@ -2,6 +2,10 @@ import { createHash } from "node:crypto";
|
||||
import { resolve } from "node:path";
|
||||
import canonicalize from "canonicalize";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
processReactiveScenarioSchema,
|
||||
type ProcessReactiveScenario,
|
||||
} from "./processReactiveScenario.js";
|
||||
import { replayMachineSchema } from "./replayMachine.js";
|
||||
|
||||
const positiveBudget = z.number().int().positive();
|
||||
@@ -51,6 +55,29 @@ export const digestProcessCommitment = (value: unknown): string => {
|
||||
return createHash("sha256").update(serialized).digest("hex");
|
||||
};
|
||||
|
||||
const reactiveScenarioCommitment = (
|
||||
scenario: ProcessReactiveScenario | null,
|
||||
): Readonly<Record<string, unknown>> | null =>
|
||||
scenario === null
|
||||
? null
|
||||
: {
|
||||
...scenario,
|
||||
states: scenario.states.map((state) => ({
|
||||
...state,
|
||||
on: state.on.map((transition) => ({
|
||||
...transition,
|
||||
actions: transition.actions.map((action) =>
|
||||
action.type === "send_input" && action.sensitive
|
||||
? {
|
||||
...action,
|
||||
data: `<redacted-input:${String(Buffer.byteLength(action.data))}-bytes>`,
|
||||
}
|
||||
: action,
|
||||
),
|
||||
})),
|
||||
})),
|
||||
};
|
||||
|
||||
/** Build the secret-safe scenario projection committed by Process Capture v4. */
|
||||
export const processScenarioCommitment = (
|
||||
scenario: ProcessScenario,
|
||||
@@ -63,6 +90,7 @@ export const processScenarioCommitment = (
|
||||
scenario.secret_aliases.includes(name) ? "<redacted-secret>" : value,
|
||||
]),
|
||||
),
|
||||
reactive: reactiveScenarioCommitment(scenario.reactive),
|
||||
unknown_registry_approved: scenario.unknown_registry_approved === true,
|
||||
executable_sha256: executableSha256 ?? null,
|
||||
});
|
||||
@@ -86,6 +114,7 @@ export const processComparisonContract = (
|
||||
normalization: scenario.normalization,
|
||||
command_shims: scenario.command_shims,
|
||||
replay: scenario.replay,
|
||||
reactive: reactiveScenarioCommitment(scenario.reactive),
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -292,6 +321,7 @@ export const processScenarioSchema = z
|
||||
websocket_messages: [],
|
||||
websocket_connections: [],
|
||||
}),
|
||||
reactive: processReactiveScenarioSchema.nullable().default(null),
|
||||
})
|
||||
.strict()
|
||||
.superRefine((scenario, context) => {
|
||||
|
||||
Reference in New Issue
Block a user