Skip to content

Commit 4b4a3eb

Browse files
vanceingallsclaude
andauthored
feat(studio): shadow telemetry for GSAP keyframe ops (gsap_keyframe) (#1509)
* fix(studio): suppress shadow-parity false positives in timing + text runShadowTiming: compare start/duration with a relative epsilon (1e-6) instead of exact equality so float-precision drift (3.1 vs 3.0999999999999996, 21.36 vs 21.360000000000014) no longer flags; a real difference (3.1 vs 3.5) still flags. trackIndex stays exact. property:text resolver: trim both sides (snapshot.text is already trimmed) and collapse empty-string vs absent (null) text so trailing-whitespace and empty-vs-null no longer flag. Genuine text differences are unaffected; the per-keystroke length lag is a caller-side debounce concern. Adds tests for both fixes plus regression tests documenting two REAL SDK divergences the shadow correctly surfaces (transform-origin removal no-op; duplicate-bare-id delete resolution) — flagged, not fixed here. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(studio): shadow telemetry for GSAP keyframe ops (gsap_keyframe) Wire the SDK shadow-parity telemetry to cover GSAP keyframe add/remove, the primary unwired cutover signal, plus a defensive unmapped-PatchOperation guard. New packages/studio/src/utils/sdkShadowGsapKeyframe.ts: - ShadowKeyframeOp + keyframeOpToEditOp: maps studio percentage-based keyframe ops to SDK EditOps. add -> addGsapKeyframe{position:percentage}; remove -> removeGsapKeyframe{keyframeIndex}, resolving percentage -> index against the pre-op script with ~0.001 tolerance and a no-op-on-ambiguity guard for duplicate-percentage keyframes (PR #1498 landmine). - gsapKeyframeFidelityMismatches: reuses gsapFidelityMismatches for the tween-level diff and layers a keyframe-array comparison (which the base diff doesn't inspect), matched by GSAP animation id. - runShadowGsapKeyframeFidelity: serialize-diff runner emitting op tag gsap_keyframe (no keyframe reader on ElementSnapshot, so no existence path). useGsapKeyframeOps synthesizes shadowKeyframeOp for addKeyframe / addKeyframeBatch / removeKeyframe; the commit chokepoint dispatches the keyframe-fidelity diff alongside the existing tween-fidelity path. sdkShadow.ts: runShadowDispatch now emits dispatched:false reason:unmapped_type if a future PatchOperation type ever escapes patchOpsToSdkEditOps, so the gap surfaces in telemetry instead of vanishing. Tests: sdkShadowGsapKeyframe.test.ts (18) covers index resolution, op mapping, the ambiguity guard, the keyframe-aware diff, the runner, and the unmapped-type guard. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 5aca3ad commit 4b4a3eb

6 files changed

Lines changed: 597 additions & 4 deletions

File tree

packages/studio/src/hooks/gsapScriptCommitTypes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Composition } from "@hyperframes/sdk";
33
import type { DomEditSelection } from "../components/editor/domEditingTypes";
44
import type { EditHistoryKind } from "../utils/editHistory";
55
import type { ShadowGsapOp } from "../utils/sdkShadow";
6+
import type { ShadowKeyframeOp } from "../utils/sdkShadowGsapKeyframe";
67

78
export interface MutationResult {
89
ok: boolean;
@@ -21,6 +22,8 @@ export interface CommitMutationOptions {
2122
beforeReload?: () => void;
2223
/** Stage 7 Step 3b: typed SDK equivalent of this mutation for value-fidelity shadow. */
2324
shadowGsapOp?: ShadowGsapOp;
25+
/** Typed SDK equivalent of a keyframe mutation for keyframe value-fidelity shadow (gsap_keyframe). */
26+
shadowKeyframeOp?: ShadowKeyframeOp;
2427
}
2528

2629
export type CommitMutation = (

packages/studio/src/hooks/useGsapKeyframeOps.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useCallback } from "react";
22
import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
3+
import type { ShadowKeyframeOp } from "../utils/sdkShadowGsapKeyframe";
34
import type { DomEditSelection } from "../components/editor/domEditingTypes";
45
import { executeOptimistic } from "../utils/optimisticUpdate";
56
import type { KeyframeCacheEntry } from "../player/store/playerStore";
@@ -58,6 +59,13 @@ export function useGsapKeyframeOps({
5859
percentage,
5960
properties: { [property]: value },
6061
};
62+
// Shadow op (gsap_keyframe): SDK equivalent diffed via the commit chokepoint.
63+
const shadowKeyframeOp: ShadowKeyframeOp = {
64+
kind: "add",
65+
animationId,
66+
percentage,
67+
properties: { [property]: value },
68+
};
6169
void executeOptimisticKeyframeCacheUpdate({
6270
sourceFile,
6371
elementId: selection.id,
@@ -71,6 +79,7 @@ export function useGsapKeyframeOps({
7179
commitMutation(selection, mutation, {
7280
label: `Add keyframe at ${percentage}%`,
7381
softReload: true,
82+
shadowKeyframeOp,
7483
}),
7584
}).catch((error) => {
7685
trackGsapSaveFailure(error, selection, mutation, `Add keyframe at ${percentage}%`);
@@ -86,10 +95,16 @@ export function useGsapKeyframeOps({
8695
percentage: number,
8796
properties: Record<string, number | string>,
8897
) => {
98+
const shadowKeyframeOp: ShadowKeyframeOp = {
99+
kind: "add",
100+
animationId,
101+
percentage,
102+
properties,
103+
};
89104
return commitMutation(
90105
selection,
91106
{ type: "add-keyframe", animationId, percentage, properties },
92-
{ label: `Add keyframe at ${percentage}%`, softReload: true },
107+
{ label: `Add keyframe at ${percentage}%`, softReload: true, shadowKeyframeOp },
93108
);
94109
},
95110
[commitMutation],
@@ -99,6 +114,10 @@ export function useGsapKeyframeOps({
99114
(selection: DomEditSelection, animationId: string, percentage: number) => {
100115
const sourceFile = selection.sourceFile || activeCompPath || "index.html";
101116
const mutation = { type: "remove-keyframe", animationId, percentage };
117+
// Shadow op (gsap_keyframe): SDK has no %-based removeGsapKeyframe on main,
118+
// so the runner resolves percentage → keyframeIndex against the pre-op
119+
// script and no-ops on ambiguity (duplicate-percentage keyframes).
120+
const shadowKeyframeOp: ShadowKeyframeOp = { kind: "remove", animationId, percentage };
102121
void executeOptimisticKeyframeCacheUpdate({
103122
sourceFile,
104123
elementId: selection.id,
@@ -112,6 +131,7 @@ export function useGsapKeyframeOps({
112131
commitMutation(selection, mutation, {
113132
label: `Remove keyframe at ${percentage}%`,
114133
softReload: true,
134+
shadowKeyframeOp,
115135
}),
116136
}).catch((error) => {
117137
trackGsapSaveFailure(error, selection, mutation, `Remove keyframe at ${percentage}%`);

packages/studio/src/hooks/useGsapScriptCommits.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { findUnsafeMutationValues } from "@hyperframes/core/studio-api/finite-mu
33
import type { DomEditSelection } from "../components/editor/domEditingTypes";
44
import { applySoftReload } from "../utils/gsapSoftReload";
55
import { resolveGsapFidelityArgs, runShadowGsapFidelity } from "../utils/sdkShadowGsapFidelity";
6+
import { runShadowGsapKeyframeFidelity } from "../utils/sdkShadowGsapKeyframe";
67
import { updateKeyframeCacheFromParsed } from "./gsapKeyframeCacheHelpers";
78
import {
89
GsapMutationHttpError,
@@ -70,9 +71,10 @@ export function useGsapScriptCommits({ projectIdRef, activeCompPath, previewIfra
7071
domEditSaveTimestampRef.current = Date.now();
7172
// Shadow value fidelity: diff the SDK's GSAP writer output against the
7273
// server's, from the same pre-op file. Fire-and-forget; server authoritative.
73-
// Only meta-level ops carry shadowGsapOp today (add / update-meta / delete via
74-
// useGsapAnimationOps). Per-property and keyframe handlers (useGsapPropertyDebounce,
75-
// useGsapKeyframeOps) intentionally don't synthesize one yet — deferred follow-up.
74+
// Meta-level ops carry shadowGsapOp (add / update-meta / delete via
75+
// useGsapAnimationOps); keyframe ops carry shadowKeyframeOp (add/remove via
76+
// useGsapKeyframeOps, handled by the gsap_keyframe block below). Per-property
77+
// handlers (useGsapPropertyDebounce) don't synthesize one yet — deferred follow-up.
7678
// scriptText is null when the composition has no GSAP script; nothing to diff.
7779
const fidelityArgs = resolveGsapFidelityArgs(
7880
sdkSession,
@@ -83,6 +85,12 @@ export function useGsapScriptCommits({ projectIdRef, activeCompPath, previewIfra
8385
if (fidelityArgs) {
8486
void runShadowGsapFidelity(fidelityArgs.before, fidelityArgs.op, fidelityArgs.serverScript);
8587
}
88+
// Keyframe value fidelity (gsap_keyframe): same serialize-diff approach, but
89+
// the SDK has no keyframe reader so there is no live-existence path — the diff
90+
// is the only signal. Guarded on a live session + both scripts to diff.
91+
if (sdkSession && options.shadowKeyframeOp && result.before != null && result.scriptText != null) {
92+
void runShadowGsapKeyframeFidelity(result.before, options.shadowKeyframeOp, result.scriptText);
93+
}
8694
if (result.before != null && result.after != null) {
8795
await editHistory.recordEdit({ label: options.label, kind: "manual", coalesceKey: options.coalesceKey, files: { [targetPath]: { before: result.before, after: result.after } } });
8896
}

packages/studio/src/utils/sdkShadow.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ function isShadowableOp(op: PatchOperation): boolean {
3939
return true;
4040
}
4141

42+
// PatchOperation types patchOpsToSdkEditOps knows how to map. Used by
43+
// runShadowDispatch to flag any unmapped type as visible telemetry rather than
44+
// silently dropping it (see the unmapped_type guard there).
45+
const MAPPED_PATCH_OP_TYPES: ReadonlySet<string> = new Set([
46+
"inline-style",
47+
"text-content",
48+
"attribute",
49+
"html-attribute",
50+
]);
51+
4252
export function patchOpsToSdkEditOps(hfId: string, ops: PatchOperation[]): EditOp[] {
4353
const result: EditOp[] = [];
4454
const styles: Record<string, string | null> = {};
@@ -260,6 +270,23 @@ export function runShadowDispatch(
260270
});
261271
return;
262272
}
273+
// Defensive: patchOpsToSdkEditOps silently drops PatchOperation types it
274+
// doesn't map. PatchOperation.type is a closed union today, but emit a visible
275+
// unmapped_type event if a future type ever slips through, so the gap surfaces
276+
// in telemetry instead of vanishing.
277+
// Map to the type string before find, so a future unmapped type is read as a
278+
// plain string (no object cast; find on the closed union narrows to never).
279+
const unmappedType = ops.map((op) => op.type).find((t) => !MAPPED_PATCH_OP_TYPES.has(t));
280+
if (unmappedType !== undefined) {
281+
trackStudioEvent("sdk_shadow_dispatch", {
282+
op: "property",
283+
dispatched: false,
284+
reason: "unmapped_type",
285+
type: unmappedType,
286+
mismatchCount: 0,
287+
});
288+
return;
289+
}
263290
const result = sdkShadowDispatch(session, hfId, ops);
264291
trackStudioEvent("sdk_shadow_dispatch", {
265292
op: "property",

0 commit comments

Comments
 (0)