File size: 23,006 Bytes
36d22d6 b89f3b2 4d90ed6 b89f3b2 74b7708 b89f3b2 74b7708 b89f3b2 94039f2 4ee621f f956d64 79cd0aa 0f6846e 5daa19b 4d0158a 33e7bee 0f6846e 1ee2bb3 36d22d6 728039f 1ee2bb3 36d22d6 1ee2bb3 f6e5d82 5daa19b 74b7708 33e7bee 72fe4f4 33e7bee 7fa375a 33e7bee 74b7708 b89f3b2 d06bf19 72fe4f4 d06bf19 323d8cc d06bf19 1ee2bb3 f6e5d82 1ee2bb3 36d22d6 1ee2bb3 36d22d6 72fe4f4 36d22d6 79cd0aa d9de684 6b82faa d9de684 1ee2bb3 74b7708 1ee2bb3 74b7708 0f6846e 1ee2bb3 bd3a155 5daa19b b89f3b2 4d90ed6 94039f2 b89f3b2 4d90ed6 b89f3b2 4d90ed6 b89f3b2 4d90ed6 b89f3b2 72fe4f4 b89f3b2 f6e5d82 94039f2 f6e5d82 1ee2bb3 7fa375a 33e7bee 1ee2bb3 f6e5d82 7fa375a 33e7bee bdf2f0e 33e7bee 324791f 7fa375a 324791f 33e7bee f956d64 324791f f956d64 33e7bee 7fa375a 4ee621f 33e7bee f956d64 324791f 7fa375a 324791f 33e7bee b89f3b2 5daa19b | 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | import { Handle, Position, useReactFlow, type NodeProps } from "@xyflow/react";
import { useState } from "react";
import {
AlertTriangle,
ChevronDown,
FileText,
SlidersHorizontal,
X,
} from "lucide-react";
import {
resolveNodeSpec,
type NodeKind,
type NodeParamSpec,
type PluginNodeSpec,
} from "../lib/nodeCatalog";
import { NodeParamEditor } from "./NodeParamEditor";
import { RunStatusChips } from "./RunStatusChips";
import { SignatureTile } from "./TransformationSignature";
import { ANON } from "../lib/anon";
import { TipIcon } from "./TipIcon";
import { useApp } from "../lib/store";
import { FAMILY_HINTS } from "../lib/familyHints";
import { durationTone, headlineFor } from "../lib/headlineMetric";
export interface QNodeData extends Record<string, unknown> {
kind: NodeKind;
params?: Record<string, unknown>;
}
/**
* Visual representation of a pipeline step on the canvas.
*
* We deliberately keep each node simple (icon + label + 1-2 param hints) — the
* full parameter editor / results viewer lives in the ResultsPane on the
* right. Noisy per-node inline editors make the graph hard to read.
*
* A small × button (always rendered, dimmed until hover/focus) lets
* users delete a node without knowing the Backspace shortcut.
*/
export function QNode({ id, data, selected }: NodeProps) {
const d = data as QNodeData;
const flow = useReactFlow();
const { deleteElements } = flow;
// Subscribe to the active circuit so source/algorithm blocks can
// surface circuit-relevant context (e.g. Input shows the loaded
// sample's qubit count; Qshot warns if the circuit is outside its
// 5–8 qubit training range). Hooks must run unconditionally, so we
// pull these even when the node kind doesn't use them — Zustand's
// shallow comparison keeps the cost cheap.
const circuit = useApp((s) => s.circuit);
const sampleKey = useApp((s) => s.sampleKey);
const plugins = useApp((s) => s.plugins);
// After-run state: subscribe to the latest run so each node can
// surface its own headline metric + duration badge. Cheap because
// run only changes once per Run click.
const run = useApp((s) => s.run);
// Authoring lock (audit S2): while a run streams, node deletion and
// param edits are disabled — mutating the graph mid-run would leave
// the canvas out of sync with what is actually executing/archiving.
const running = useApp((s) => s.running);
const step = run?.steps.find((s) => s.node_id === id);
// Anytime evidence: while a run streams, sampled steps report each
// shot batch here BEFORE their StepResult exists — the node face
// renders a live-narrowing CI from these frames.
const liveProgress = useApp((s) => s.liveProgress?.[id]);
const stepDurationS =
step && step.status === "ok"
? step.finished_at - step.started_at
: null;
// Spec resolution — built-in OR user plugin. Hook order requires
// this stay above any early return.
const spec = resolveNodeSpec(d.kind, plugins);
// Per-instance disclosure state for the param editor. Default to
// collapsed so the canvas stays scannable; users click the chevron
// to reveal the controls. State lives in component-local React
// state, not in node-data, because it's UI affordance only — we
// don't want it serialised into share-links or auto-connect graphs.
const [paramsOpen, setParamsOpen] = useState(false);
if (!spec) {
// Plugin was deleted (or never installed in this browser) while a
// node referencing its kind is still on the canvas — show a
// tombstone so the user knows what happened instead of an
// invisible-but-runnable block.
return (
<div className="node-card group relative border-danger/40 bg-danger/5">
<button
type="button"
aria-label="Delete this block"
title={
running
? "Locked while a run is in progress"
: "Delete this orphaned block"
}
disabled={running}
className="nodrag absolute -top-2 -right-2 w-6 h-6 rounded-full bg-surface border border-edge text-mute hover:text-danger hover:border-danger/60 flex items-center justify-center opacity-70 hover:opacity-100 focus:opacity-100 transition-opacity z-10 disabled:opacity-30 disabled:cursor-not-allowed"
onClick={(e) => {
e.stopPropagation();
deleteElements({ nodes: [{ id }] });
}}
>
<X className="w-3 h-3" strokeWidth={2.5} />
</button>
<div className="flex items-center gap-2">
<span className="w-7 h-7 rounded-md flex items-center justify-center text-[10px] font-bold text-white bg-danger/60 shrink-0">
?
</span>
<div className="min-w-0 flex-1">
<div className="font-medium text-ink text-sm truncate">
{d.kind}
</div>
<div className="text-[10px] text-danger uppercase tracking-wider">
plugin not installed
</div>
</div>
</div>
<div className="mt-1.5 text-[10px] text-mute leading-snug">
This browser doesn't have the plugin installed — add it via
the plugin API (see PLUGIN_SDK.md), or hover and click × to
remove the block.
</div>
</div>
);
}
const Icon = spec.icon;
/** Patch this node's `data.params` and propagate to React Flow state. */
const patchParams = (patch: Record<string, unknown>) => {
flow.updateNodeData(id, {
...d,
params: { ...(d.params ?? {}), ...patch },
});
};
const hasInput = spec.family !== "source";
const hasOutput = spec.family !== "sink";
return (
<div
className={`node-card group relative transition-colors ${
selected ? "shadow-glow !border-accent/60" : ""
} ${spec.accentRing}`}
>
<button
type="button"
aria-label="Delete this block"
title={
running
? "Locked while a run is in progress"
: "Delete this block"
}
disabled={running}
className="nodrag absolute -top-2 -right-2 w-6 h-6 rounded-full bg-surface border border-edge text-mute hover:text-danger hover:border-danger/60 flex items-center justify-center opacity-70 hover:opacity-100 focus:opacity-100 transition-opacity z-10 disabled:opacity-30 disabled:cursor-not-allowed"
onClick={(e) => {
e.stopPropagation();
deleteElements({ nodes: [{ id }] });
}}
>
<X className="w-3 h-3" strokeWidth={2.5} />
</button>
{spec.paper && !ANON && (
<a
href={spec.paper.url}
target="_blank"
rel="noopener noreferrer"
className="nodrag absolute -top-2 -left-2 w-5 h-5 rounded-full bg-accent/20 border border-accent/70 text-accent hover:bg-accent/40 hover:border-accent hover:text-ink flex items-center justify-center shadow-sm transition-colors z-10"
onClick={(e) => e.stopPropagation()}
title={`Paper: ${spec.paper.title} (${spec.paper.venue})`}
aria-label={`Open paper: ${spec.paper.title}`}
>
<FileText className="w-3 h-3" strokeWidth={2} />
</a>
)}
<div className="flex items-center gap-2">
{"isPlugin" in spec && spec.isPlugin ? (
// Plugin badge: filled square in plugin's chosen color +
// first-letter(s) so users can distinguish multiple plugins
// at a glance.
<span
className="w-7 h-7 rounded-md flex items-center justify-center shrink-0 text-[11px] font-bold text-white"
style={{ backgroundColor: (spec as PluginNodeSpec).pluginColor }}
title={`User plugin (${spec.kind})`}
>
{(spec as PluginNodeSpec).initials}
</span>
) : (
<span
className={`w-7 h-7 rounded-md border ${spec.accentRing} bg-surface flex items-center justify-center ${spec.accent} shrink-0`}
>
<Icon className="w-3.5 h-3.5" strokeWidth={2} />
</span>
)}
<div className="min-w-0 flex-1">
<div className="font-medium text-ink text-sm flex items-center gap-1">
<span className="truncate">{spec.label}</span>
{"isPlugin" in spec && spec.isPlugin && (
<span
className="text-[9px] px-1 py-0.5 rounded border border-edge text-mute/70 shrink-0"
title="User-uploaded plugin"
>
plugin
</span>
)}
</div>
<div className="text-[10px] text-mute uppercase tracking-wider flex items-center gap-1">
<span>{spec.family}</span>
<TipIcon hint={FAMILY_HINTS[spec.family]} size={10} />
</div>
</div>
</div>
<div
className="mt-1.5 text-[10px] text-mute leading-snug line-clamp-2"
title={spec.description}
>
{spec.tagline}
</div>
{/* Circuit-aware context — only kinds that actually depend on the
loaded circuit render anything here. */}
{d.kind === "input_circuit" && circuit && (
<div className="mt-1.5 pt-1.5 border-t border-edge/40 text-[10px] text-mute leading-snug">
<div className="flex items-baseline gap-1.5">
<span>Loaded</span>
<span className="font-mono text-ink truncate">
{sampleKey ?? "uploaded"}
</span>
</div>
<div className="font-mono text-mute/90 mt-0.5">
{circuit.num_qubits}q · depth {circuit.depth}
{circuit.num_parameters > 0 && (
<> · {circuit.num_parameters} params</>
)}
</div>
</div>
)}
{d.kind === "qshot" && circuit && (
<QshotFitChip nq={circuit.num_qubits} />
)}
{/* Editable params (schema-driven) start collapsed so the block
stays compact on a busy canvas. Header row is a chip-like
button so it reads as obviously clickable: SlidersHorizontal
icon + uppercase "PARAMS" label, current-value summary on
the right, ChevronDown that rotates on toggle. The whole row
gets a hover background + border so newcomers see the
affordance without needing to hover over the chevron. Other
node kinds fall back to the static read-out. */}
{spec.params && spec.params.length > 0 ? (
<div className="mt-2 pt-2 border-t border-edge/60">
<button
type="button"
onClick={(e) => {
e.stopPropagation();
setParamsOpen((v) => !v);
}}
aria-expanded={paramsOpen}
aria-label={
paramsOpen ? "Hide parameters" : "Show parameters"
}
className={`nodrag w-full flex items-center gap-1.5 px-1.5 py-1 -mx-0.5 rounded-md border text-[10px] transition-colors ${
paramsOpen
? "border-edge bg-surfaceAlt text-ink"
: "border-transparent text-mute hover:text-ink hover:border-edge/60 hover:bg-surfaceAlt"
}`}
>
<SlidersHorizontal
className={`w-3 h-3 shrink-0 ${
paramsOpen ? "text-accent" : ""
}`}
strokeWidth={2.2}
/>
<span className="shrink-0 uppercase tracking-wider font-medium">
params
</span>
<span className="font-mono text-ink truncate flex-1 text-right">
{!paramsOpen && summariseParams(spec.params, d.params ?? {})}
</span>
<ChevronDown
className={`w-3 h-3 shrink-0 transition-transform ${
paramsOpen ? "rotate-180" : "rotate-0"
}`}
strokeWidth={2.5}
/>
</button>
{paramsOpen && (
<NodeParamEditor
spec={spec.params}
values={d.params ?? {}}
onChange={patchParams}
disabled={running}
/>
)}
</div>
) : (
d.params &&
Object.keys(d.params).length > 0 && (
<div className="mt-2 pt-2 border-t border-edge/60 space-y-0.5">
{Object.entries(d.params).map(([k, v]) => (
<div key={k} className="flex justify-between gap-2 text-[11px]">
<span className="text-mute">{k}</span>
<span className="font-mono text-ink truncate max-w-[120px]">
{String(v)}
</span>
</div>
))}
</div>
)
)}
{!step && liveProgress && (
<LiveEvidenceStrip progress={liveProgress} />
)}
{step && step.status === "ok" && (
<RunResultStrip
kind={d.kind}
step={step}
durationS={stepDurationS}
/>
)}
{step && step.status === "error" && (
<div className="mt-2 pt-2 border-t border-danger/30 text-[10px] text-danger flex items-start gap-1">
<AlertTriangle className="w-3 h-3 mt-0.5 shrink-0" />
<span className="truncate" title={step.message ?? ""}>
{step.message ?? "Failed"}
</span>
</div>
)}
{hasInput && (
<Handle type="target" position={Position.Left} isConnectable={true} />
)}
{hasOutput && (
<Handle type="source" position={Position.Right} isConnectable={true} />
)}
</div>
);
}
/** Live evidence strip: rendered while a sampled step is still
* accumulating shot batches (i.e. before its StepResult exists).
* Same 0-1 fixed-scale encoding as the post-run micro CI bar so the
* live band morphs seamlessly into the final one; the CSS
* left/width transition is what makes the narrowing READ as motion
* instead of flicker. The counter states the evidence honestly in
* shots, not percent-done — the whole point of anytime steering is
* that "done" is the user's call, not the progress bar's. */
function LiveEvidenceStrip({
progress,
}: {
progress: import("../lib/api").StepProgress;
}) {
const [lo, hi] = progress.ci95;
return (
<div className="mt-2 pt-1.5 border-t border-edge/40">
<div className="flex items-baseline justify-between gap-2 text-[9px] text-mute">
<span className="font-mono text-accent">
{(progress.point * 100).toFixed(1)}%
</span>
<span className="font-mono">
{progress.shots_done} shots · batch {progress.batch_i}/
{progress.n_batches}
</span>
</div>
<div
className="relative h-[3px] rounded-full bg-surfaceAlt overflow-hidden mt-1"
title={`Evidence so far: ${(progress.point * 100).toFixed(1)}% · 95% CI ${(lo * 100).toFixed(1)}–${(hi * 100).toFixed(1)}% after ${progress.shots_done} shots`}
role="img"
aria-label={`live uncertainty after ${progress.shots_done} shots: point ${(progress.point * 100).toFixed(1)} percent, interval ${(lo * 100).toFixed(1)} to ${(hi * 100).toFixed(1)} percent`}
>
<div
className="absolute inset-y-0 bg-accent/25 transition-[left,width] duration-300 ease-out"
style={{
left: `${lo * 100}%`,
width: `${Math.max(1, (hi - lo) * 100)}%`,
}}
/>
<div
className="absolute inset-y-0 w-[2px] bg-accent transition-[left] duration-300 ease-out"
style={{ left: `calc(${progress.point * 100}% - 1px)` }}
/>
</div>
</div>
);
}
/** After-run strip at the bottom of the node tile: headline metric +
* duration chip + cache indicator. None of these are interactive,
* but they make the canvas itself a glanceable dashboard once a run
* has happened.
*
* Layout contract (visual-calm audit, 2026-07-10): the strip is at
* most the stretched glyph + TWO further rows — (1) headline+chips,
* (2) the 3px micro-CI bar — at ANY tile width. The chips row cannot
* wrap: it is flex-nowrap, the headline block is min-w-0 + truncate
* (it gives way first), the chips are shrink-0, and at most three
* small chips can coexist (⏹ shots · one cached/live chip —
* RunStatusChips renders at most one — · duration). Worst case
* ≈130px of chips against the 180px min tile width, so the headline
* truncates rather than the row wrapping. If a chip is ever added
* here, re-audit against that budget. */
function RunResultStrip({
kind,
step,
durationS,
}: {
kind: NodeKind;
step: import("../lib/api").StepResult;
durationS: number | null;
}) {
const headline = headlineFor(kind, step);
const tone = durationS !== null ? durationTone(durationS) : null;
const durBadgeClass = {
fast: "border-edge/60 text-mute/80",
medium: "border-accent/40 text-accent/90",
slow: "border-warn/50 text-warn",
"very-slow": "border-danger/50 text-danger",
}[tone ?? "fast"];
const durFormatted =
durationS === null
? ""
: durationS < 1
? `${Math.round(durationS * 1000)}ms`
: `${durationS.toFixed(durationS < 10 ? 2 : 1)}s`;
// Micro CI bar: the same 0-1-scale encoding as the results card's
// uncertainty block (interval band + point tick), shrunk to 3px so
// a stochastic node face answers "how sure are we" without opening
// any panel. Only steps that emit a binomial distribution payload
// (sampled fidelity today, plugins tomorrow) render it.
const dist = step.distribution as
| {
kind?: string;
point?: number;
ci95?: [number, number];
shots?: number;
shots_requested?: number;
stopped_early?: boolean;
precision_target?: number;
}
| null
| undefined;
const ci =
dist?.kind === "binomial" && Array.isArray(dist.ci95) ? dist.ci95 : null;
const rawPoint =
typeof dist?.point === "number"
? dist.point
: Number((step.summary as Record<string, unknown>)["fidelity"]);
const ciPoint = ci && Number.isFinite(rawPoint) ? rawPoint : null;
return (
<div className="mt-2 pt-1.5 border-t border-edge/40">
<SignatureTile step={step} stretch />
<div className="flex items-baseline justify-between gap-2">
{headline ? (
<div className="min-w-0 flex-1">
<div className="text-[9px] uppercase tracking-wider text-mute/70 truncate">
{headline.label}
</div>
<div
className={`text-[12px] font-mono font-semibold leading-tight truncate ${
headline.tone ?? "text-ink"
}`}
title={`${headline.label}: ${headline.value}`}
>
{headline.value}
</div>
</div>
) : (
<div className="flex-1 text-[10px] text-ok/80">✓ ran</div>
)}
<div className="flex items-center gap-1 shrink-0">
{dist?.stopped_early && (
<span
className="text-[9px] px-1 py-0.5 rounded border border-accent/50 text-accent font-mono"
title={`Optional stopping: target ±${((dist.precision_target ?? 0) * 100).toFixed(0)}pp reached after ${dist.shots} of ${dist.shots_requested} shots — the remaining shots were not paid for.`}
>
⏹ {dist.shots}
</span>
)}
<RunStatusChips step={step} variant="tile" />
{durationS !== null && (
<span
className={`text-[9px] px-1 py-0.5 rounded border font-mono ${durBadgeClass}`}
title={
tone === "very-slow"
? "Took over 1 minute — likely the pipeline's bottleneck"
: tone === "slow"
? "Took over 10 seconds"
: tone === "medium"
? "Took 1-10 seconds"
: "Sub-second"
}
>
{durFormatted}
</span>
)}
</div>
</div>
{ci && ciPoint !== null && (
<div
className="relative h-[3px] rounded-full bg-surfaceAlt overflow-hidden mt-1.5"
title={`Sampled estimate ${(ciPoint * 100).toFixed(1)}% · 95% CI ${(
ci[0] * 100
).toFixed(1)}–${(ci[1] * 100).toFixed(1)}% (fixed 0–100% scale)`}
role="img"
aria-label={`uncertainty: point ${(ciPoint * 100).toFixed(
1,
)} percent, 95 percent interval ${(ci[0] * 100).toFixed(1)} to ${(
ci[1] * 100
).toFixed(1)} percent`}
>
<div
className="absolute inset-y-0 bg-accent/25 transition-[left,width] duration-300 ease-out"
style={{
left: `${ci[0] * 100}%`,
width: `${Math.max(1, (ci[1] - ci[0]) * 100)}%`,
}}
/>
<div
className="absolute inset-y-0 w-[2px] bg-accent"
style={{ left: `calc(${ciPoint * 100}% - 1px)` }}
/>
</div>
)}
</div>
);
}
/** Build a one-line "param₁ · param₂ · …" summary for the collapsed
* state of the param editor. Selects use the raw value (short keys
* like "pittsburgh_1") rather than the long display label so the
* whole summary stays under ~30 characters; numbers respect the
* spec's `displayPrecision`. Result is meant to be a glanceable
* cue — not a full read-out — so order in the schema matters. */
function summariseParams(
spec: NodeParamSpec[],
values: Record<string, unknown>,
): string {
const parts: string[] = [];
for (const p of spec) {
const raw = values[p.key];
if (raw === undefined || raw === null || raw === "") continue;
if (p.type === "select") {
parts.push(String(raw));
} else {
const n = Number(raw);
if (!Number.isFinite(n)) continue;
const precision = p.displayPrecision ?? (p.type === "int" ? 0 : 2);
parts.push(n.toFixed(precision));
}
}
return parts.join(" · ");
}
/** Tiny in-block status pill for the Qshot node: shows whether the
* loaded circuit's qubit count is inside the 5-8q training range or
* will trip the GNN-fallback path. Saves the user from learning the
* hard way after a 60-second pilot run that the recommendation came
* from the extrapolation path. */
function QshotFitChip({ nq }: { nq: number }) {
const inRange = nq >= 5 && nq <= 8;
if (inRange) {
return (
<div className="mt-1.5 pt-1.5 border-t border-edge/40 text-[10px] text-ok leading-snug font-mono">
in training range · {nq}q
</div>
);
}
return (
<div className="mt-1.5 pt-1.5 border-t border-warn/40 text-[10px] text-warn leading-snug">
<div className="flex items-center gap-1">
<AlertTriangle className="w-3 h-3 shrink-0" strokeWidth={2.2} />
<span className="font-mono">{nq}q · outside 5–8q range</span>
</div>
<div className="text-warn/80 mt-0.5">
will trigger GNN fallback
</div>
</div>
);
}
|