mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-17 23:47:33 +00:00
* fix(studio): close the typecheck and fallow gaps wa-18b-reschedule opened useAutomationLanes.ts's write() assumed gesture-scoped coalescing and a preview-only commit that useDomEditAttributeCommits.ts never grew — backported that option support from its own later commit so the two sides of the API agree. The paste path and its tests were missing the box selection's v0/v1 bounds a sibling commit added to AutomationSelection. The FX panel's carve controls still edited the six mechanism numbers (maxCutDb, bands, intelligibilityBias) after carveProfile() collapsed authoring to one Strength knob, so those fields no longer existed on HfCarveSettings; UI now edits strength, and analyseCarveBands is called with carveProfile(strength). Also closes fallow's complexity, dead-code and duplication findings on this PR's diff: extracted automationLaneDragMath.ts (pure group/point-move math) and useAutomationRangeDrag.ts (the marquee-select gesture) out of useAutomationLaneGestures.ts, pulled a couple of render-loop ternaries and a resolver into named functions, dropped an export nothing outside its file used, and shared a step-simplifier between audioCarve's two envelope builders. The edge-stretch vs. box-select priority test in TimelineAutomationLane.test was still pinning the pre-box-select rule (edge wins over a point sitting on it) that a sibling commit deliberately reversed — a point inside the box is now selected content, so grabbing it drags the group instead. Updated the test to the shipped rule instead of the old one. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(core): cap the via conic's weight so an edge-clamped via point can't NaN A via point pulled out past the segment (viaX: 5, viaY: -3) clamps to (0.999, 0.001) — exactly on the steady region's edge, where edge - viaX is 0. viaConic divided by that zero to get an infinite weight, and shapeVia turned Infinity into NaN a few steps later (Infinity - Infinity in the quadratic coefficient). NaN reaching setValueCurveAtTime silences the automated parameter for the rest of the render. Capped the weight at 1e6 instead of leaving it unbounded — past that point the arc already reads as touching the via point, so nothing visible is lost. Also hardened shapeVia's existing denominator guard (`<= 0`) to `!(> 0)`, since NaN fails the original comparison and fell through it. Review by Miga (PR #3208). * fix(studio-server): fingerprint the proactive waveform cache key too The route already keys the waveform cache on the asset's size and mtime as well as its path, so a rebuilt-in-place file gets fresh peaks instead of stale ones. generateWaveformCache — the proactive path that runs on upload — still called buildWaveformCacheKey with the path alone, so it wrote to a different key than the route reads from (making the pre-generated cache never found) and kept the exact collision bug this fingerprint exists to fix on its own path. Review by Miga (PR #3211). * style(docs): run oxfmt on the /hyperframes-audio skill docs Table column widths had drifted out of alignment with oxfmt's own rules, failing format:check and blocking the Preflight gate every downstream branch inherits. Whitespace only, no content change. * fix(core): stop \b from missing underscore-separated names, guard clipsOverlap's negative duration \b treats `_` as a word character, so \bbed\b never matched bed_01, music_bed_loop, or theme_song, and \bvo\b/\bvox\b/\btts\b had the same gap — an underscore-separated bed classified as "unknown" and could end up offered as its own carve source. Replaced the short hints with a boundary that actually excludes letters and digits on both sides. clipsOverlap computed end = start + duration without guarding sign, so a negative duration put end before start — an interval that does not describe anything, and one specific case showed it silently dropping a real overlap (a shorter, earlier broken end rejected a clip that genuinely contained the point). Duration clamps to zero instead: a clip cannot un-play time, and a zero-length clip at its start is the sane reading of "duration nobody wrote down as positive." Review by Miga (PR #3212). * fix(studio): widen PropertyPanel's resetModules render timeout again The 20s margin (already once widened for the same reason) is timing out in CI's full-monorepo Test run — the resetModules()+fresh-import render this test needs is uncached and competes with every other package's test suite for the same worker pool, and the same test passes in well under 2s standalone. Went to 45s rather than re-tuning to whatever number happens to clear the current CI load, since that number moves every time CI gains a package. * fix(studio): stop the single-candidate auto-apply carve firing twice Two auto-apply effects both fire when sourceOptions.length === 1: the multi-candidate effect only guards length === 0, so a single candidate passes it too, and the single-candidate effect passes its own guard right after — both compute the same sources list and both call setCarve, so the common case (one narrator, one bed) triggered two decodes, two FFT runs, and two concurrent attribute writes for one decision. The multi-candidate effect now defers to its sibling for exactly one candidate, which already has its own detailed handling for that case. Review by Miga (PR #3213). * feat(core): carve against every voice over a bed, always (#3212) * feat(core): carve against every voice over a bed, always dynamically A bed usually runs under a whole sequence — a narrator, an interview answer, a second presenter — and carving against one of them left the others fighting it. `source` becomes `sources`, and `mixCarveSources` sums every voice onto the BED's clock before anything is measured. That is what keeps one analysis sufficient: the chain is fixed, so there is no per-voice filter to switch between, and bands drawn from all the speech there is with envelopes that rise wherever any of it happens answer the actual question — where and when is speech masking this bed. Summed rather than averaged: two people talking at once mask more than either alone. Audio before the bed starts is dropped rather than folded in at zero, since it plays over nothing and shifting it would put a cut where there is no voice. `dynamic` is gone. A fixed depth thins the bed through every pause, and once both have been heard there is no reason to want it, so every carve follows the speech. Two helpers the panel and the headless script now share instead of each carrying a copy — two definitions of "what does this name suggest" drift, and then the two disagree about which track is the voice: - `classifyAudioName` reads a track's kind from its id and filename together. `unknown` is deliberately common: treating an unrecognised name as "not a voice" would hide the one track somebody needs to pick. - `clipsOverlap` keeps out a voice that never plays while the bed does. An unwritten duration counts as unbounded, not zero — refusing a clip whose length the composition leaves to the media would drop the commonest case there is. Files written before this still load: a single `source` reads as a one-voice list, a stored `dynamic` is ignored, and an absent attribute means the defaults whole. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(core): stop \b from missing underscore-separated names, guard clipsOverlap's negative duration \b treats `_` as a word character, so \bbed\b never matched bed_01, music_bed_loop, or theme_song, and \bvo\b/\bvox\b/\btts\b had the same gap — an underscore-separated bed classified as "unknown" and could end up offered as its own carve source. Replaced the short hints with a boundary that actually excludes letters and digits on both sides. clipsOverlap computed end = start + duration without guarding sign, so a negative duration put end before start — an interval that does not describe anything, and one specific case showed it silently dropping a real overlap (a shorter, earlier broken end rejected a clip that genuinely contained the point). Duration clamps to zero instead: a clip cannot un-play time, and a zero-length clip at its start is the sane reading of "duration nobody wrote down as positive." Review by Miga (PR #3212). --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(studio): port the carve UI off the removed source/dynamic fields #3212 (accidentally squash-merged into this branch instead of main) changed HfCarveSettings from a single `source` + `dynamic` toggle to a `sources` list with dynamic mode removed outright — the multi-voice UI consumer that goes with that shape lands in the very next PR, so this branch was left with a type that no longer matched its own code. Minimal port, not the multi-voice redesign that PR does properly: the "Listen to" picker and analyse() treat sources[0] as the one voice this UI still understands, and every dynamic-mode branch (the automated envelope lanes, the toggle, the checkbox) is gone along with the field — a carve is now always the static value the analysis computes, matching what the type change made permanent. Test suite trimmed the same way: the automation-lane and toggle tests covered behavior that no longer exists. * refactor(studio): break up the FX rack's largest functions and files Fallow flagged 9 complexity findings and 2 file-size violations after the telemetry stack landed. Extracts FxPresetRun, FxAddMenu, FxRackChain, FxNodeOpenBody, FxNodeParams, and useFxAudition/useFxCarve/useFxLevelling/ useFxChainObserved out of propertyPanelFxSection.tsx and propertyPanelAudioFxGroup.tsx, splits propertyPanelFxNodeRow.tsx's open-face rendering into its own component, and dedupes a clone in studioTelemetry.ts. Pure structural move — no behavior change; full test suite still green. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
315 lines
14 KiB
TypeScript
315 lines
14 KiB
TypeScript
// ---------------------------------------------------------------------------
|
|
// CLI → Studio telemetry identity (Layer 1).
|
|
//
|
|
// The CLI owns both the Studio launch and the local server, so it seeds the
|
|
// browser with its own anonymous `config.anonymousId`. Studio adopts it as its
|
|
// distinct_id (see packages/studio/src/telemetry/distinctId.ts), so the CLI's
|
|
// `cli_command*` events and the browser's `studio:*` / `studio_*` / render
|
|
// events are attributed to one PostHog person.
|
|
//
|
|
// This uses ONLY the existing anonymous machine id (a random UUID, no PII), so
|
|
// the "no personal info" telemetry disclosure stays valid. When CLI telemetry
|
|
// is disabled (opt-out / dev / CI / DO_NOT_TRACK) nothing is seeded and Studio
|
|
// behaves exactly as if opened standalone.
|
|
//
|
|
// Kept out of studioServer.ts so it can be unit-tested without pulling in the
|
|
// server's heavy render dependencies (@hyperframes/producer, engine, …).
|
|
// ---------------------------------------------------------------------------
|
|
|
|
import { hostname, networkInterfaces } from "node:os";
|
|
import { readConfig, readConfigFresh } from "../telemetry/config.js";
|
|
import {
|
|
resetTelemetryPostureCache,
|
|
shouldTrack as telemetryShouldTrack,
|
|
} from "../telemetry/client.js";
|
|
import { canaryDecisionsForStudio, type CliCanaryDecision } from "../telemetry/canary.js";
|
|
import { detectAgentRuntime } from "../telemetry/agent_runtime.js";
|
|
|
|
/**
|
|
* The CLI's anonymous distinct id to hand to Studio, or null when CLI telemetry
|
|
* is disabled or no id is available. Fail-silent — telemetry must never break
|
|
* the preview server.
|
|
*/
|
|
export function resolveCliTelemetryDistinctId(): string | null {
|
|
try {
|
|
if (!telemetryShouldTrack()) return null;
|
|
const id = readConfig().anonymousId;
|
|
return typeof id === "string" && id.length > 0 ? id : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The CLI's canary bucket seed to hand to Studio, or null. Injected alongside
|
|
* the distinct id so a CLI-launched Studio buckets canaries on the SAME unit
|
|
* as the CLI — without it the two surfaces would agree only while the seed
|
|
* still equals whatever Studio falls back to, and a rollout spanning render
|
|
* and editor would split one user across cohorts. Same telemetry gate as the
|
|
* distinct id: seeding is part of the identity stitch, not a separate channel.
|
|
*/
|
|
function resolveCliBucketSeed(): string | null {
|
|
try {
|
|
if (!telemetryShouldTrack()) return null;
|
|
const seed = readConfig().bucketSeed;
|
|
return typeof seed === "string" && seed.length > 0 ? seed : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Is this request's `Host` a loopback name the studio server could have been
|
|
* reached on directly?
|
|
*
|
|
* A bare `[::1]`/`localhost`/dotted-quad check rather than a full parse: the
|
|
* port is irrelevant (any port on loopback is us), and anything exotic enough
|
|
* to miss here should be refused rather than guessed at.
|
|
*
|
|
* Scope, stated precisely: this is a **DNS-rebinding mitigation for browsers**,
|
|
* not access control. A browser sets `Host` from the URL it was given, so a
|
|
* page that rebinds its own hostname to 127.0.0.1 arrives carrying that
|
|
* hostname and is refused. A non-browser client sets `Host` to whatever it
|
|
* likes, so this stops nothing there — but on a loopback-bound server such a
|
|
* client is already local, and on a LAN-bound one it can read the project
|
|
* files through the unauthenticated studio API anyway. See `identityAllowed`.
|
|
*/
|
|
export function isLoopbackHost(host: string | undefined): boolean {
|
|
if (!host) return false;
|
|
// Strip the port. IPv6 literals are bracketed (`[::1]:1234`), so take the
|
|
// bracketed part when present and only split on ":" otherwise.
|
|
const bracketed = /^\[([^\]]+)\]/.exec(host);
|
|
const hostname = (bracketed ? bracketed[1] : host.split(":")[0])?.toLowerCase() ?? "";
|
|
if (hostname === "localhost" || hostname === "::1" || hostname === "0:0:0:0:0:0:0:1") return true;
|
|
// 127.0.0.0/8 — the whole loopback block, not just 127.0.0.1.
|
|
return /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(hostname);
|
|
}
|
|
|
|
// JSON.stringify does not escape "<" or "/". Escaping both means no
|
|
// "</script>" (or "</…") sequence can form in the emitted value, so it can
|
|
// never terminate the inline <script> or open a new tag. (The values are
|
|
// randomUUID()s, so this is belt-and-suspenders.)
|
|
function encodeInlineScriptValue(value: string): string {
|
|
return JSON.stringify(value).replace(/</g, "\\u003c").replace(/\//g, "\\/");
|
|
}
|
|
|
|
/**
|
|
* Same escaping, for a structured value. Separate from the string form
|
|
* because that one stringifies its argument — passing an object through it
|
|
* would double-encode into a quoted JSON blob.
|
|
*
|
|
* The keys here are registry canary names, so they are developer-authored and
|
|
* ASCII by convention rather than user input; the escaping is belt-and-braces
|
|
* for the same reason it is on the ids.
|
|
*/
|
|
function encodeInlineScriptJson(value: unknown): string {
|
|
return JSON.stringify(value).replace(/</g, "\\u003c").replace(/\//g, "\\/");
|
|
}
|
|
|
|
/**
|
|
* The CLI's resolved canary decisions for a launched Studio, or null when
|
|
* there are none to publish. Fail-silent, like everything else here.
|
|
*/
|
|
function resolveCliCanaryDecisions(): Record<string, CliCanaryDecision> | null {
|
|
try {
|
|
const decisions = canaryDecisionsForStudio();
|
|
return Object.keys(decisions).length > 0 ? decisions : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* `<script>` tag to inject into the served index.html `<head>`, publishing the
|
|
* CLI distinct id as `window.__HF_CLI_DISTINCT_ID` (plus the bucket seed and
|
|
* resolved canary decisions) before the studio bundle runs. Preferred over a
|
|
* URL param so the id never leaks into `$current_url` / `url_hash` telemetry
|
|
* or browser history. Empty string only when there is nothing at all to
|
|
* publish.
|
|
*/
|
|
export function buildCliIdentityScript(options: { includeIdentity?: boolean } = {}): string {
|
|
const { includeIdentity = true } = options;
|
|
const parts: string[] = [];
|
|
|
|
// Identity is the only part gated on a trusted Host. The decisions map below
|
|
// is not identifying, and withholding it would push a LAN/remote Studio
|
|
// (`HYPERFRAMES_PREVIEW_HOST=0.0.0.0`, an explicitly supported mode) back to
|
|
// re-deriving locally — reopening exactly the CLI/Studio disagreement this
|
|
// whole mechanism exists to close.
|
|
const cliId = includeIdentity ? resolveCliTelemetryDistinctId() : null;
|
|
if (cliId) {
|
|
parts.push(`window.__HF_CLI_DISTINCT_ID=${encodeInlineScriptValue(cliId)};`);
|
|
const seed = resolveCliBucketSeed();
|
|
if (seed) parts.push(`window.__HF_CLI_BUCKET_SEED=${encodeInlineScriptValue(seed)};`);
|
|
}
|
|
|
|
// Emitted even when telemetry is OFF and the identity block above is empty —
|
|
// that is the case it exists for. With telemetry off the CLI resolves every
|
|
// canary to `telemetry_opt_out`, but Studio's opt-out is a separate
|
|
// localStorage flag it cannot see, so left to itself Studio would evaluate
|
|
// normally and could enrol on a render the CLI had already excluded. Same
|
|
// for an `HF_CANARY_*` override, which never crosses into the browser.
|
|
//
|
|
// Safe to publish unconditionally: these are booleans about features, not
|
|
// identity, and strictly less than the seed they replace as Studio's input.
|
|
const decisions = resolveCliCanaryDecisions();
|
|
if (decisions !== null) {
|
|
parts.push(`window.__HF_CLI_CANARY_DECISIONS=${encodeInlineScriptJson(decisions)};`);
|
|
}
|
|
|
|
// Which agent, if any, is driving this CLI — and therefore the Studio it just
|
|
// opened. Studio cannot work this out for itself: the signal is entirely in
|
|
// the CLI process's environment, which the browser never sees.
|
|
//
|
|
// Published on the same terms as the decisions above, and for the same
|
|
// reason: it is a category derived from the EXISTENCE of well-known vendor
|
|
// env vars, never their values (`agent_runtime.ts` is explicit that it never
|
|
// reads a value, because some are API keys). One of a dozen fixed strings, or
|
|
// absent. Nothing identifying, so it does not belong behind the trusted-Host
|
|
// gate that gates the distinct id.
|
|
//
|
|
// Gated on telemetry being ON, unlike the decisions above. Those exist so a
|
|
// Studio whose CLI opted out does not enrol itself, so they have to survive
|
|
// the opt-out. This is only ever read to label an event, so with telemetry off
|
|
// there is nothing for it to label — publishing it anyway would leave a marker
|
|
// in the page of a user who asked not to be measured.
|
|
const agent = telemetryShouldTrack() ? detectAgentRuntime() : null;
|
|
if (agent) {
|
|
parts.push(`window.__HF_CLI_AGENT_RUNTIME=${encodeInlineScriptValue(agent)};`);
|
|
}
|
|
|
|
return parts.length === 0 ? "" : `<script>${parts.join("")}</script>`;
|
|
}
|
|
|
|
/**
|
|
* Compose the scripts injected into the served Studio `index.html` `<head>`.
|
|
* The CLI identity script MUST come first so `window.__HF_CLI_DISTINCT_ID` is
|
|
* set before the (deferred) Studio bundle runs telemetry init and reads it;
|
|
* `envScript` is the existing `window.__HF_STUDIO_ENV__` injection. Keeping the
|
|
* ordering in one pure, tested function guards against a future `<head>` inject
|
|
* silently landing ahead of the identity script and reintroducing a boot race.
|
|
*/
|
|
export function buildStudioHeadScripts(
|
|
envScript: string,
|
|
options: { includeIdentity?: boolean } = {},
|
|
): string {
|
|
return `${buildCliIdentityScript(options)}${envScript}`;
|
|
}
|
|
|
|
/**
|
|
* The `<head>` scripts for a request, given its `Host`.
|
|
*
|
|
* The Host split lives here rather than in the route so it is testable
|
|
* without a Studio bundle on disk — the route's own test can only reach the
|
|
* injection branch when `packages/studio/dist` happens to be built, which is
|
|
* true locally and false in the CI test lane.
|
|
*/
|
|
/** Hostname of a `Host` header, port and IPv6 brackets removed. */
|
|
function hostnameOf(host: string | undefined): string {
|
|
if (!host) return "";
|
|
const bracketed = /^\[([^\]]+)\]/.exec(host);
|
|
return (bracketed ? bracketed[1] : host.split(":")[0])?.toLowerCase() ?? "";
|
|
}
|
|
|
|
/**
|
|
* Names this machine legitimately answers to on a wildcard bind: every
|
|
* interface address, plus its own hostname and the `.local` mDNS form people
|
|
* actually type. Fail closed — a throw yields an empty set, which denies.
|
|
*/
|
|
function localNames(): Set<string> {
|
|
const names = new Set<string>();
|
|
try {
|
|
for (const entries of Object.values(networkInterfaces())) {
|
|
for (const entry of entries ?? []) names.add(entry.address.toLowerCase());
|
|
}
|
|
const self = hostname().toLowerCase();
|
|
if (self !== "") {
|
|
names.add(self);
|
|
// `my-box` is reachable as `my-box.local`, and a `my-box.lan.example`
|
|
// FQDN is reachable by its short form. Register both directions.
|
|
names.add(`${self}.local`);
|
|
const short = self.split(".")[0];
|
|
if (short !== undefined && short !== "") {
|
|
names.add(short);
|
|
names.add(`${short}.local`);
|
|
}
|
|
}
|
|
} catch {
|
|
/* fail closed */
|
|
}
|
|
return names;
|
|
}
|
|
|
|
/**
|
|
* Wildcard binds answer on every local name; a specific bind on itself only.
|
|
*
|
|
* This is the part that was missing: the previous rule accepted ANY Host once
|
|
* the env var was set, so a rebinding page could name itself `evil.example.com`
|
|
* and still be handed the CLI identity.
|
|
*/
|
|
function hostMatchesBind(host: string | undefined, bind: string): boolean {
|
|
const requested = hostnameOf(host);
|
|
if (requested === "") return false;
|
|
const bound = bind.toLowerCase();
|
|
if (requested === bound) return true;
|
|
return bound === "0.0.0.0" || bound === "::" || bound === "*"
|
|
? localNames().has(requested)
|
|
: false;
|
|
}
|
|
|
|
/**
|
|
* May this request receive the CLI's identity (distinct id + bucket seed)?
|
|
*
|
|
* The server binds loopback by DEFAULT and exposes the LAN only when an
|
|
* operator sets `HYPERFRAMES_PREVIEW_HOST` (portUtils.ts, F-001). A loopback
|
|
* `Host` is always fine: whatever reached us came via loopback, and the only
|
|
* interesting attacker is a rebinding browser page — which the Host check
|
|
* catches, because a browser cannot forge `Host`.
|
|
*
|
|
* For anything else the bind decides:
|
|
*
|
|
* - **Unset, or bound to loopback.** No LAN exposure was requested, so the
|
|
* Host check is a live rebinding mitigation and a non-loopback Host is
|
|
* refused. Previously ANY non-empty value disabled the check, so even
|
|
* `HYPERFRAMES_PREVIEW_HOST=127.0.0.1` — which exposes nothing — turned the
|
|
* loopback service from hostile-Host refusal into accept-everything.
|
|
* - **Bound to a LAN address.** The operator opted into exposure, so identity
|
|
* has to reach the LAN name the user browses or the CLI-to-Studio stitch
|
|
* breaks and Studio mints a second person for the same human. But it is
|
|
* still checked: the Host must name an address this machine actually
|
|
* answers on, not any attacker-chosen name a rebinding page supplies.
|
|
*/
|
|
export function identityAllowed(host: string | undefined): boolean {
|
|
if (isLoopbackHost(host)) return true;
|
|
const bind = (process.env["HYPERFRAMES_PREVIEW_HOST"] ?? "").trim();
|
|
if (bind === "" || isLoopbackHost(bind)) return false;
|
|
return hostMatchesBind(host, bind);
|
|
}
|
|
|
|
/**
|
|
* Re-read the persisted telemetry preference for this request.
|
|
*
|
|
* Both caches have to go, together. `readConfig()` memoizes the parsed config
|
|
* and `shouldTrack()` memoizes its own boolean on top of it, so clearing
|
|
* either alone still yields the stale answer: the canary layer asks
|
|
* `readConfig()`, the identity layer asks `shouldTrack()`, and they would
|
|
* disagree mid-refresh. A long-lived `hyperframes preview` otherwise keeps
|
|
* serving pre-opt-out decisions and injecting the CLI id for hours after
|
|
* `hyperframes telemetry disable` runs in another terminal.
|
|
*
|
|
* Fail-silent and once per request — a config re-read, not a hot path.
|
|
*/
|
|
export function refreshTelemetryPosture(): void {
|
|
try {
|
|
readConfigFresh();
|
|
resetTelemetryPostureCache();
|
|
} catch {
|
|
/* telemetry must never break the preview server */
|
|
}
|
|
}
|
|
|
|
export function buildStudioHeadScriptsForHost(envScript: string, host: string | undefined): string {
|
|
refreshTelemetryPosture();
|
|
return buildStudioHeadScripts(envScript, { includeIdentity: identityAllowed(host) });
|
|
}
|