@principal-ai/subsystems-react 0.56.33 → 0.56.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,7 +21,9 @@ import {
21
21
  ReactFlow,
22
22
  ReactFlowProvider,
23
23
  useReactFlow,
24
+ useStore,
24
25
  useViewport,
26
+ getViewportForBounds,
25
27
  type NodeTypes,
26
28
  type EdgeTypes,
27
29
  type Node,
@@ -103,7 +105,12 @@ import { ShareButton } from './ShareButton';
103
105
  import { DescriptionOverlay } from './DescriptionOverlay';
104
106
  import { DescriptionPreview } from './DescriptionPreview';
105
107
  import type { GraphSidebarTab } from './GraphSidebar';
106
- import { TrailsPanel, TRAIL_PLAY_PAUSE_MS, TRAIL_HOVER_PREVIEW_MS } from './TrailsPanel';
108
+ import {
109
+ TrailsPanel,
110
+ TRAIL_PLAY_PAUSE_MS,
111
+ TRAIL_HOVER_PREVIEW_MS,
112
+ type TrailsStepChange,
113
+ } from './TrailsPanel';
107
114
  import type { StepEmphasisMap } from './stepEmphasis';
108
115
  import {
109
116
  EDGE_LABEL_WIDTH,
@@ -335,6 +342,56 @@ export interface SubsystemComponentGraphProps {
335
342
  * trail.
336
343
  */
337
344
  onTrailSelect?: (ref: { trailId: string; stepIndices?: number[] } | null) => void;
345
+ /**
346
+ * PROTOTYPE: host-controlled trail focus. Changing this expands the trail
347
+ * and drives the camera. With a `stepIndex`, it focuses that step's edge;
348
+ * without one, it frames the whole trail. `showFile` controls whether the
349
+ * step's file/trail drawer opens — omit it for whole-trail focus, or set it
350
+ * to `false` to select a step without showing its file.
351
+ *
352
+ * `undefined` leaves in-graph focus untouched; `null` clears host control.
353
+ */
354
+ focusedTrailStep?: {
355
+ trailId: string;
356
+ stepIndex?: number;
357
+ showFile?: boolean;
358
+ } | null;
359
+ /**
360
+ * PROTOTYPE: host-local ids of trails awaiting confirmation. Their Trails
361
+ * panel icon uses the proposal color instead of labelling the title.
362
+ */
363
+ proposedTrailIds?: Set<string>;
364
+ /**
365
+ * PROTOTYPE: host-local ids of steps awaiting confirmation, keyed by
366
+ * `emphasisKey(trailId, step)`. Their Trails panel text uses proposal color.
367
+ */
368
+ proposedStepKeys?: Set<string>;
369
+ /**
370
+ * PROTOTYPE: host-local before/after view for an accepted step replacement.
371
+ * The previous step stays out of the trails array; the panel renders it as a
372
+ * ghost beneath the committed step.
373
+ */
374
+ stepChange?: TrailsStepChange | null;
375
+ /** Whether the previous-state ghost is visible. @default true */
376
+ showPreviousStep?: boolean;
377
+ /** Dismiss or restore just the previous-state ghost. */
378
+ onTogglePreviousStep?: () => void;
379
+ /**
380
+ * PROTOTYPE: host-local component aliases awaiting confirmation. Their boxes
381
+ * use the proposal border, name colour and `PROPOSED` badge.
382
+ */
383
+ proposedComponentAliases?: Set<string>;
384
+ /**
385
+ * PROTOTYPE: host-local component aliases the agent is currently working on.
386
+ * Their boxes get a static ring and a sparkling WandSparkles icon.
387
+ */
388
+ workingComponentAliases?: Set<string>;
389
+ /**
390
+ * PROTOTYPE: host-local component aliases removed from the model. Their
391
+ * nodes stay in the layout, dimmed and `REMOVED`-badged, so the change is
392
+ * legible instead of silently disappearing.
393
+ */
394
+ removedComponentAliases?: Set<string>;
338
395
  /**
339
396
  * Reports which trail step is currently hovered in the Trails panel —
340
397
  * the transient preview mirror of `onTrailSelect`. `null` when nothing is
@@ -404,6 +461,11 @@ export interface SubsystemComponentGraphProps {
404
461
  * subsystem name visible as an overlay on the canvas.
405
462
  */
406
463
  hideSidebar?: boolean;
464
+ /**
465
+ * Suppress only the standard full-width header (title + sidebar-view switch)
466
+ * while keeping the sidebar and canvas. Hosts can render their own header.
467
+ */
468
+ hideHeader?: boolean;
407
469
  /**
408
470
  * How trail step highlighting behaves on the canvas.
409
471
  * - `focus` (default): zoom to the step, hide non-participants, open drawer
@@ -435,6 +497,12 @@ export interface SubsystemComponentGraphProps {
435
497
  * @default 300
436
498
  */
437
499
  trailFocusDurationMs?: number;
500
+ /**
501
+ * Downward screen-pixel offset applied when the camera frames the graph or
502
+ * a focused trail step. Useful for hosts with chrome over the canvas top.
503
+ * @default 0
504
+ */
505
+ focusVerticalBiasPx?: number;
438
506
  /**
439
507
  * Subsystem title rendered as a non-interactive overlay chip on the graph
440
508
  * canvas (top-center). Does not trigger the sidebar — for graph-only
@@ -630,7 +698,9 @@ export interface SubsystemComponentGraphProps {
630
698
  * with its members folded away and their lines rolled up onto it, the way a
631
699
  * C4 container view reads. The user opens frames from the card, by
632
700
  * double-click, or from the Containers list; a `persistKey` remembers the
633
- * result. Default true; pass `false` to start with every frame laid out.
701
+ * result. Default true, except a model with a single container frame starts
702
+ * open (there is nothing to compare collapsed cards against); pass `false`
703
+ * to start with every frame laid out, `true` to force even a lone frame shut.
634
704
  */
635
705
  defaultFramesCollapsed?: boolean;
636
706
  }
@@ -747,9 +817,13 @@ interface InnerProps extends SubsystemComponentGraphProps {
747
817
  onSidebarTrailChange: (trailId: string | null) => void;
748
818
  }
749
819
 
750
- function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirection: layoutDirectionProp, defaultLayoutDirection = 'horizontal', onLayoutDirectionChange, hideLayoutToggle = false, refocusOnLayoutFlip = true, initialTrailId, onTrailSelect, onTrailStepHover, onReorderTrails, stepEmphasis, onRemoveStepEmphasis, onSelect, onEdgeSelect, measured: _measured, maxNodeWidth, showEdgeLabels, showSingletonFrames = true, moduleNesting, edgeView, hideSidebar, trailStepMode = 'focus', autoPlayTrails = false, trailAutoPlayIntervalMs = TRAIL_PLAY_PAUSE_MS, onAutoPlayStep, zoomOnTrailFocus = true, trailFocusDurationMs = 300, graphTitle, showTrailTitle = false, description, notes, canvasOverlay, interactive = true, canvasBackground = 'theme', onLayoutReady, onNodeRects, sidebarExtra, sidebarAfterDescription, issues, showIssues, filesActive, sidebarScope, onSidebarTrailChange, focusIssueCategory, laneStatus, onSelectIssue, onApplyIssueFix, onHoverIssue, renderFileViewer, renderTrailViewer, onFileSelect, componentVerification, onInspectSymbol, boundaryColors, codeProcessKeys, regionDetails, defaultFramesCollapsed = true, containersActive, hideDrawer = false, persistKey, liveEvents, agentsPanel }: InnerProps) {
820
+ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirection: layoutDirectionProp, defaultLayoutDirection = 'horizontal', onLayoutDirectionChange, hideLayoutToggle = false, refocusOnLayoutFlip = true, initialTrailId, onTrailSelect, focusedTrailStep, proposedTrailIds, proposedStepKeys, stepChange, showPreviousStep, onTogglePreviousStep, proposedComponentAliases, workingComponentAliases, removedComponentAliases, onTrailStepHover, onReorderTrails, stepEmphasis, onRemoveStepEmphasis, onSelect, onEdgeSelect, measured: _measured, maxNodeWidth, showEdgeLabels, showSingletonFrames = true, moduleNesting, edgeView, hideSidebar, trailStepMode = 'focus', autoPlayTrails = false, trailAutoPlayIntervalMs = TRAIL_PLAY_PAUSE_MS, onAutoPlayStep, zoomOnTrailFocus = true, trailFocusDurationMs = 300, focusVerticalBiasPx = 0, graphTitle, showTrailTitle = false, description, notes, canvasOverlay, interactive = true, canvasBackground = 'theme', onLayoutReady, onNodeRects, sidebarExtra, sidebarAfterDescription, issues, showIssues, filesActive, sidebarScope, onSidebarTrailChange, focusIssueCategory, laneStatus, onSelectIssue, onApplyIssueFix, onHoverIssue, renderFileViewer, renderTrailViewer, onFileSelect, componentVerification, onInspectSymbol, boundaryColors, codeProcessKeys, regionDetails, defaultFramesCollapsed, containersActive, hideDrawer = false, persistKey, liveEvents, agentsPanel }: InnerProps) {
751
821
  const { theme } = useTheme();
752
- const { fitView, fitBounds } = useReactFlow();
822
+ const { fitView, fitBounds, setViewport } = useReactFlow();
823
+ const flowWidth = useStore((state) => state.width);
824
+ const flowHeight = useStore((state) => state.height);
825
+ const minZoom = useStore((state) => state.minZoom);
826
+ const maxZoom = useStore((state) => state.maxZoom);
753
827
  const [descriptionOpen, setDescriptionOpen] = useState(false);
754
828
  const rawDescription = description?.trim();
755
829
  // The host caps the authored description; trim defensively for other hosts.
@@ -779,6 +853,47 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
779
853
  edges: [],
780
854
  absoluteRects: new Map(),
781
855
  });
856
+
857
+ // React Flow's `fitBounds` always centres the bounds. A host may need the
858
+ // diagram deliberately lower (e.g. to leave room for status chrome over the
859
+ // canvas), so recompute the same viewport and shift only its screen-space y.
860
+ const fitBoundsWithBias = useCallback(
861
+ (
862
+ bounds: { x: number; y: number; width: number; height: number },
863
+ options: { padding: number; duration?: number },
864
+ ) => {
865
+ if (
866
+ !focusVerticalBiasPx ||
867
+ flowWidth <= 0 ||
868
+ flowHeight <= 0
869
+ ) {
870
+ void fitBounds(bounds, options);
871
+ return;
872
+ }
873
+ const viewport = getViewportForBounds(
874
+ bounds,
875
+ flowWidth,
876
+ flowHeight,
877
+ minZoom,
878
+ maxZoom,
879
+ options.padding,
880
+ );
881
+ void setViewport(
882
+ { ...viewport, y: viewport.y + focusVerticalBiasPx },
883
+ { duration: options.duration },
884
+ );
885
+ },
886
+ [
887
+ fitBounds,
888
+ setViewport,
889
+ flowWidth,
890
+ flowHeight,
891
+ minZoom,
892
+ maxZoom,
893
+ focusVerticalBiasPx,
894
+ ],
895
+ );
896
+
782
897
  const fitGraphContents = useCallback(
783
898
  (duration = 300) => {
784
899
  const edges = built.edges.map((edge) => {
@@ -812,9 +927,9 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
812
927
  });
813
928
  return;
814
929
  }
815
- fitBounds(bounds, { padding: 0.1, duration });
930
+ fitBoundsWithBias(bounds, { padding: 0.1, duration });
816
931
  },
817
- [built.absoluteRects, built.edges, fitBounds, fitView],
932
+ [built.absoluteRects, built.edges, fitBoundsWithBias, fitView],
818
933
  );
819
934
  const [layoutReady, setLayoutReady] = useState(false);
820
935
  const transparentCanvas = canvasBackground === 'transparent';
@@ -867,6 +982,12 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
867
982
  const flipFadeTimerRef = useRef<number | null>(null);
868
983
  /** Set when a layout flip starts; the camera refits once layout lands. */
869
984
  const refitAfterLayoutRef = useRef(false);
985
+ /** A host-driven step focus owns the camera across frame-layout flips. */
986
+ const stepFocusActiveRef = useRef(false);
987
+ const focusEdgeIdsRef = useRef<ReadonlySet<string> | null>(null);
988
+ const fitFocusBoundsRef = useRef<(ids: ReadonlySet<string>) => void>(
989
+ () => {},
990
+ );
870
991
 
871
992
  /** Solo layout: only this trail's components and edges are laid out. */
872
993
  const [layoutTrailId, setLayoutTrailId] = useState<string | null>(null);
@@ -879,8 +1000,23 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
879
1000
  // Stored as a default plus exceptions so "close all" needs no knowledge of
880
1001
  // which frames the layout will produce: `framesCollapsed` is the default
881
1002
  // every card frame takes, `toggledGroupIds` the frames flipped away from it.
1003
+ // A lone container starts open — a single collapsed card hides the whole
1004
+ // model behind one box with nothing to compare it against. An explicit
1005
+ // `defaultFramesCollapsed` still wins; so does a persisted choice.
1006
+ const resolvedDefaultFramesCollapsed = useMemo(() => {
1007
+ if (defaultFramesCollapsed !== undefined) return defaultFramesCollapsed;
1008
+ try {
1009
+ const count = buildBoundaryLayoutGroups(
1010
+ { components },
1011
+ { showSingletonFrames, moduleNesting, codeProcessKeys },
1012
+ ).filter((g) => isCardFrame(g.region)).length;
1013
+ return count <= 1 ? false : true;
1014
+ } catch {
1015
+ return true;
1016
+ }
1017
+ }, [components, showSingletonFrames, moduleNesting, codeProcessKeys, defaultFramesCollapsed]);
882
1018
  const [framesCollapsed, setFramesCollapsed] = useState<boolean>(
883
- () => persisted.framesCollapsed ?? defaultFramesCollapsed,
1019
+ () => persisted.framesCollapsed ?? resolvedDefaultFramesCollapsed,
884
1020
  );
885
1021
  const [toggledGroupIds, setToggledGroupIds] = useState<ReadonlySet<string>>(
886
1022
  () => new Set(persisted.toggledFrames ?? []),
@@ -1031,7 +1167,14 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
1031
1167
  if (!layoutReady || !refitAfterLayoutRef.current) return;
1032
1168
  refitAfterLayoutRef.current = false;
1033
1169
  requestAnimationFrame(() => {
1034
- fitGraphContents(LAYOUT_FLIP_NODE_MS);
1170
+ // Focused trail/step edges (e.g. an agent walkthrough step that just
1171
+ // opened a different trail's frames) must not be replaced by a
1172
+ // whole-graph fit — that would zoom back out to the entire model.
1173
+ if (focusEdgeIdsRef.current) {
1174
+ fitFocusBoundsRef.current(focusEdgeIdsRef.current);
1175
+ } else {
1176
+ fitGraphContents(LAYOUT_FLIP_NODE_MS);
1177
+ }
1035
1178
  });
1036
1179
  }, [built, layoutReady, fitGraphContents]);
1037
1180
 
@@ -1302,7 +1445,10 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
1302
1445
  if (!committedTrailId) return { components, trails };
1303
1446
  const trail = (trails ?? []).find((t) => t.id === committedTrailId);
1304
1447
  if (!trail) return { components, trails };
1305
- const aliases = new Set<string>();
1448
+ // Host-local removal ghosts stay in the layout even when their steps are
1449
+ // gone; otherwise accepting a removal would make the node vanish from a
1450
+ // solo-focused flow instead of leaving the dim record the user asked for.
1451
+ const aliases = new Set<string>(removedComponentAliases);
1306
1452
  for (const step of trail.steps) {
1307
1453
  aliases.add(step.from);
1308
1454
  aliases.add(step.to);
@@ -1311,7 +1457,7 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
1311
1457
  components: components.filter((c) => aliases.has(c.alias)),
1312
1458
  trails: [trail],
1313
1459
  };
1314
- }, [components, trails, committedTrailId]);
1460
+ }, [components, trails, committedTrailId, removedComponentAliases]);
1315
1461
  const layoutRelations = committedTrailId ? undefined : graphifyRelations;
1316
1462
 
1317
1463
  const layoutKey = useMemo(
@@ -1807,7 +1953,8 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
1807
1953
 
1808
1954
  // Edge ids in trail focus (an active flow's edge set, or a single
1809
1955
  // step's edge). Used to frame the camera. `null` = no trail focus.
1810
- const focusEdgeIds = useMemo(() => { if (autoPlayFocus) {
1956
+ const focusEdgeIds = useMemo(() => {
1957
+ if (autoPlayFocus) {
1811
1958
  const tl = trails?.find((t) => t.id === autoPlayFocus.trailId);
1812
1959
  const step = tl?.steps[autoPlayFocus.stepIndex];
1813
1960
  return step ? new Set([trailStepGraphEdgeId(step)]) : null;
@@ -1822,6 +1969,13 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
1822
1969
  return new Set(tl.steps.map((s) => trailStepGraphEdgeId(s)));
1823
1970
  }, [trails, focusedTrailId, focusedStepIndex, autoPlayFocus]);
1824
1971
 
1972
+ // Keep these refs current during render, not in a later effect: the
1973
+ // post-layout refit effect is declared above and must see the focus that
1974
+ // triggered this layout swap, or it falls back to a whole-graph fit.
1975
+ stepFocusActiveRef.current =
1976
+ autoPlayFocus == null && focusedTrailId != null && focusedStepIndex != null;
1977
+ focusEdgeIdsRef.current = focusEdgeIds;
1978
+
1825
1979
  // 1-based step numbers per edge of the active flow (focused or
1826
1980
  // hover/autoplay-highlighted). An edge can appear in more than one step.
1827
1981
  const selectedFlowStepNos = useMemo(() => {
@@ -2002,6 +2156,15 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2002
2156
  };
2003
2157
  }
2004
2158
  const comp = (n.data as { component?: SubsystemComponent } | undefined)?.component;
2159
+ const proposed =
2160
+ comp?.alias != null &&
2161
+ proposedComponentAliases?.has(comp.alias) === true;
2162
+ const working =
2163
+ comp?.alias != null &&
2164
+ workingComponentAliases?.has(comp.alias) === true;
2165
+ const removed =
2166
+ comp?.alias != null &&
2167
+ removedComponentAliases?.has(comp.alias) === true;
2005
2168
  const fileMatch = fileMatchForNode(comp?.file, openFile, focusNodeIds?.has(n.id) === true);
2006
2169
  // Bases selection on the focused step, not the hover preview, so
2007
2170
  // hovering doesn't strip the frame's `isSelected`.
@@ -2009,7 +2172,7 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2009
2172
  // `brightNodeIds` (the focused step's endpoints) is a spotlight member as
2010
2173
  // well as a bright set — see the group branch above.
2011
2174
  const dimSource = previewNodeIds ?? brightNodeIds ?? issueFocusNodeIds;
2012
- const { hidden, dimmed } = flowNodeVisibility({
2175
+ const flowVisibility = flowNodeVisibility({
2013
2176
  inOpened: openedNodeIds?.has(n.id) === true,
2014
2177
  inSelected: brightNodeIds?.has(n.id) === true,
2015
2178
  inSpotlight: dimSource?.has(n.id) === true || (hoverTrailAliases?.has(n.id) === true),
@@ -2017,13 +2180,25 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2017
2180
  anySelected: brightNodeIds != null,
2018
2181
  anySpotlight: dimSource != null || hoverTrailAliases != null,
2019
2182
  });
2183
+ // A removed ghost is never hidden by flow focus — the whole point is to
2184
+ // keep it visible as a dim record of what the agent took out.
2185
+ const hidden = removed ? false : flowVisibility.hidden;
2186
+ const dimmed = removed ? true : flowVisibility.dimmed;
2020
2187
  // Component findings key by alias, boundary findings by region node id.
2021
2188
  // The two id spaces are disjoint (`module:` / `process:` are prefixed), so
2022
2189
  // one lookup covers both node kinds.
2023
2190
  const issueBadge =
2024
2191
  issueBadgeByAlias.get(n.id) ?? regionIssueByNodeId.get(n.id);
2025
- if (fileMatch === undefined && !isSelected && !dimmed && !issueBadge) {
2026
- const { fileMatch: _f, isSelected: _s, dimmed: _d, ...rest } = n.data as Record<string, unknown>;
2192
+ if (
2193
+ fileMatch === undefined &&
2194
+ !isSelected &&
2195
+ !dimmed &&
2196
+ !issueBadge &&
2197
+ !proposed &&
2198
+ !working &&
2199
+ !removed
2200
+ ) {
2201
+ const { fileMatch: _f, isSelected: _s, dimmed: _d, proposed: _p, working: _w, removed: _r, ...rest } = n.data as Record<string, unknown>;
2027
2202
  return { ...n, hidden, data: rest };
2028
2203
  }
2029
2204
  return {
@@ -2034,11 +2209,14 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2034
2209
  ...(fileMatch !== undefined && { fileMatch }),
2035
2210
  ...(isSelected && { isSelected }),
2036
2211
  ...(dimmed && { dimmed: true }),
2212
+ ...(proposed && { proposed: true }),
2213
+ ...(working && { working: true }),
2214
+ ...(removed && { removed: true }),
2037
2215
  ...(issueBadge && { issue: issueBadge }),
2038
2216
  },
2039
2217
  };
2040
2218
  });
2041
- }, [xyflowNodesBase, openFile, selected, focusNodeIds, openedNodeIds, brightNodeIds, previewNodeIds, hoverTrailAliases, aliasToComponent, issueFocusNodeIds, issueBadgeByAlias, regionIssueByNodeId, boundaryColors, toggleCollapsed]);
2219
+ }, [xyflowNodesBase, openFile, selected, focusNodeIds, openedNodeIds, brightNodeIds, previewNodeIds, hoverTrailAliases, aliasToComponent, issueFocusNodeIds, issueBadgeByAlias, regionIssueByNodeId, boundaryColors, proposedComponentAliases, workingComponentAliases, removedComponentAliases, toggleCollapsed]);
2042
2220
 
2043
2221
  const baseNodesKey = useMemo(() => nodes.map((n) => n.id).sort().join(','), [nodes]);
2044
2222
  const baseEdgesKey = useMemo(
@@ -2133,8 +2311,9 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2133
2311
 
2134
2312
  useEffect(() => {
2135
2313
  // A layout flip runs its own camera refit when the new layout lands. This
2136
- // mount/edit fit would otherwise fire mid-flip and fight it.
2137
- if (layoutFlipping) return;
2314
+ // mount/edit fit would otherwise fire mid-flip and fight it. A focused
2315
+ // step also owns the camera — a frame flip must not restore whole-graph fit.
2316
+ if (layoutFlipping || stepFocusActiveRef.current) return;
2138
2317
  const t = setTimeout(() => {
2139
2318
  fitGraphContents(200);
2140
2319
  }, 200);
@@ -2373,7 +2552,7 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2373
2552
  width: Math.max(1, maxX - minX),
2374
2553
  height: Math.max(1, maxY - minY),
2375
2554
  };
2376
- fitBounds(bounds, {
2555
+ fitBoundsWithBias(bounds, {
2377
2556
  padding: 0.15,
2378
2557
  duration: trailFocusDurationMs,
2379
2558
  });
@@ -2381,12 +2560,16 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2381
2560
  [
2382
2561
  baseEdges,
2383
2562
  built.absoluteRects,
2384
- fitBounds,
2563
+ fitBoundsWithBias,
2385
2564
  zoomOnTrailFocus,
2386
2565
  trailFocusDurationMs,
2387
2566
  ],
2388
2567
  );
2389
2568
 
2569
+ useEffect(() => {
2570
+ fitFocusBoundsRef.current = fitFocusBounds;
2571
+ }, [fitFocusBounds]);
2572
+
2390
2573
  // A node's laid-out rect in absolute flow coords. `absoluteRects` is the
2391
2574
  // authority; the fallback covers an unparented group node, whose `position` is
2392
2575
  // already absolute. Nested groups are parent-relative, so they are skipped
@@ -2434,7 +2617,7 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2434
2617
  maxY = Math.max(maxY, r.y + r.height);
2435
2618
  }
2436
2619
  if (!Number.isFinite(minX) || !Number.isFinite(minY)) return;
2437
- fitBounds(
2620
+ fitBoundsWithBias(
2438
2621
  {
2439
2622
  x: minX,
2440
2623
  y: minY,
@@ -2444,7 +2627,7 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2444
2627
  { padding, duration: trailFocusDurationMs },
2445
2628
  );
2446
2629
  },
2447
- [rectForNode, fitBounds, trailFocusDurationMs],
2630
+ [rectForNode, fitBoundsWithBias, trailFocusDurationMs],
2448
2631
  );
2449
2632
 
2450
2633
  // The Containers list: every card frame in the laid-out graph, parents before
@@ -2521,11 +2704,12 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2521
2704
  // frame the flow on the canvas. Selection state is cleared — the graph now
2522
2705
  // reads as the narrative. No drawer: the code view only opens on a step
2523
2706
  // click. A stale trail drawer (from a previously focused flow's step)
2524
- // closes; an explicitly opened file drawer stays.
2707
+ // closes; an explicitly opened file drawer stays unless a host asks for
2708
+ // file-free focus (e.g. a walkthrough step that only selects the trail).
2525
2709
  // In `dim` mode: clear step highlight and leave the full graph visible
2526
2710
  // (no zoom / hide) — matching "step away from a hovered step".
2527
2711
  const focusTrailEdges = useCallback(
2528
- (tl: SubsystemTrail) => {
2712
+ (tl: SubsystemTrail, closeFile = false) => {
2529
2713
  setSelected(null);
2530
2714
  setSelectedEdgeId(null);
2531
2715
  setHoveredTrailStep(null);
@@ -2542,10 +2726,34 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2542
2726
  setFocusedStepIndex(null);
2543
2727
  setFocusedTrailId(tl.id);
2544
2728
  onTrailSelect?.({ trailId: tl.id });
2545
- fitFocusBounds(new Set(tl.steps.map((s) => trailStepGraphEdgeId(s))));
2546
- setDrawerTarget((prev) => (prev?.kind === 'trail' ? null : prev));
2729
+ const edgeIds = new Set(tl.steps.map((s) => trailStepGraphEdgeId(s)));
2730
+ const drawerWillClose =
2731
+ drawerOpenRef.current && (closeFile || drawerTarget?.kind === 'trail');
2732
+ setDrawerTarget((prev) =>
2733
+ closeFile || prev?.kind === 'trail' ? null : prev,
2734
+ );
2735
+ if (pendingFocusFitRef.current != null) {
2736
+ window.clearTimeout(pendingFocusFitRef.current);
2737
+ pendingFocusFitRef.current = null;
2738
+ }
2739
+ if (drawerWillClose) {
2740
+ // Wait for the canvas to reclaim the drawer's height, or the fit uses
2741
+ // the temporarily shortened viewport and settles too far off-centre.
2742
+ pendingFocusFitRef.current = window.setTimeout(() => {
2743
+ pendingFocusFitRef.current = null;
2744
+ requestAnimationFrame(() => fitFocusBounds(edgeIds));
2745
+ }, FILE_DRAWER_HEIGHT_MS + 20);
2746
+ } else {
2747
+ fitFocusBounds(edgeIds);
2748
+ }
2547
2749
  },
2548
- [fitFocusBounds, trailStepMode, onTrailSelect, onSidebarTrailChange],
2750
+ [
2751
+ fitFocusBounds,
2752
+ trailStepMode,
2753
+ onTrailSelect,
2754
+ onSidebarTrailChange,
2755
+ drawerTarget,
2756
+ ],
2549
2757
  );
2550
2758
 
2551
2759
  const clearTrailFocus = useCallback(() => {
@@ -2597,7 +2805,7 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2597
2805
  // In `dim` mode: only dim non-participants (same as hovering a step) —
2598
2806
  // no camera move, no drawer, no hide.
2599
2807
  const focusTrailStep = useCallback(
2600
- (tl: SubsystemTrail, stepIndex: number) => {
2808
+ (tl: SubsystemTrail, stepIndex: number, showFile = true) => {
2601
2809
  const step = tl.steps[stepIndex];
2602
2810
  if (!step) return;
2603
2811
  setSelected(null);
@@ -2610,6 +2818,11 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2610
2818
  onTrailSelect?.(null);
2611
2819
  return;
2612
2820
  }
2821
+ // Opening the focused step's containing frames is what makes an
2822
+ // intra-process edge (e.g. api → store) renderable at all. Without this,
2823
+ // host-driven step focus can only frame cross-boundary hops.
2824
+ setExpandedTrails(new Set([tl.id]));
2825
+ setLayoutTrailId(tl.id);
2613
2826
  // Toggle: clicking the selected step unselects it (back to whole flow).
2614
2827
  onSidebarTrailChange(tl.id);
2615
2828
  if (focusedTrailId === tl.id && focusedStepIndex === stepIndex) {
@@ -2617,7 +2830,9 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2617
2830
  // Pointer is still over the row — keep hover preview so dimming doesn't
2618
2831
  // flash off until mouseleave.
2619
2832
  setHoveredTrailStep({ trailId: tl.id, stepIndex });
2620
- setDrawerTarget((prev) => (prev?.kind === 'trail' ? null : prev));
2833
+ setDrawerTarget((prev) =>
2834
+ !showFile ? null : prev?.kind === 'trail' ? null : prev,
2835
+ );
2621
2836
  if (pendingFocusFitRef.current != null) {
2622
2837
  window.clearTimeout(pendingFocusFitRef.current);
2623
2838
  pendingFocusFitRef.current = null;
@@ -2629,10 +2844,12 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2629
2844
  setFocusedTrailId(tl.id);
2630
2845
  setHoveredTrailStep(null);
2631
2846
  onTrailSelect?.({ trailId: tl.id, stepIndices: [stepIndex] });
2632
- // Open the drawer before fitting. If it was closed, wait for its height
2633
- // transition so fitView uses the reduced canvas — not full height.
2847
+ // The drawer changes the usable canvas height, so wait for its height
2848
+ // transition whenever we open or close it — not just when opening.
2634
2849
  const drawerWasOpen = drawerOpenRef.current;
2635
- if (renderTrailViewer) {
2850
+ if (!showFile) {
2851
+ setDrawerTarget(null);
2852
+ } else if (renderTrailViewer) {
2636
2853
  setDrawerTarget({
2637
2854
  kind: 'trail',
2638
2855
  trailId: tl.id,
@@ -2651,7 +2868,7 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2651
2868
  window.clearTimeout(pendingFocusFitRef.current);
2652
2869
  pendingFocusFitRef.current = null;
2653
2870
  }
2654
- if (drawerWasOpen) {
2871
+ if (drawerWasOpen === showFile) {
2655
2872
  fitFocusBounds(edgeIds);
2656
2873
  } else {
2657
2874
  pendingFocusFitRef.current = window.setTimeout(() => {
@@ -2671,6 +2888,43 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
2671
2888
  ],
2672
2889
  );
2673
2890
 
2891
+ // Host-controlled trail focus for agent-driven walkthroughs. Unlike
2892
+ // `initialTrailId`, this is repeatable. A `stepIndex` focuses one step;
2893
+ // omitting it frames the whole trail. `showFile: false` selects without
2894
+ // opening the file/trail drawer. `undefined` leaves graph-owned focus alone;
2895
+ // `null` hands control back to the graph.
2896
+ const appliedFocusedTrailStepRef = useRef<string | null>(null);
2897
+ useEffect(() => {
2898
+ if (focusedTrailStep === undefined) return;
2899
+ if (focusedTrailStep === null) {
2900
+ if (appliedFocusedTrailStepRef.current == null) return;
2901
+ appliedFocusedTrailStepRef.current = null;
2902
+ clearTrailFocus();
2903
+ return;
2904
+ }
2905
+ const { trailId, stepIndex } = focusedTrailStep;
2906
+ const showFile = focusedTrailStep.showFile ?? stepIndex != null;
2907
+ const key = `${trailId}:${stepIndex ?? 'trail'}:${showFile ? 'file' : 'nofile'}`;
2908
+ if (appliedFocusedTrailStepRef.current === key) return;
2909
+ if (!layoutReady || !trails?.length) return;
2910
+ const tl = trails.find((t) => t.id === trailId);
2911
+ if (!tl) return;
2912
+ if (stepIndex != null && !tl.steps[stepIndex]) return;
2913
+ appliedFocusedTrailStepRef.current = key;
2914
+ if (stepIndex == null) {
2915
+ focusTrailEdges(tl, !showFile);
2916
+ } else {
2917
+ focusTrailStep(tl, stepIndex, showFile);
2918
+ }
2919
+ }, [
2920
+ focusedTrailStep,
2921
+ layoutReady,
2922
+ trails,
2923
+ focusTrailEdges,
2924
+ focusTrailStep,
2925
+ clearTrailFocus,
2926
+ ]);
2927
+
2674
2928
  useEffect(() => {
2675
2929
  return () => {
2676
2930
  if (pendingFocusFitRef.current != null) {
@@ -3200,6 +3454,11 @@ function Inner({ components, trails, graphifyRelations, orderByLine, layoutDirec
3200
3454
  expandedTrails={expandedTrails}
3201
3455
  focusedTrailId={sidebarTrail?.id ?? focusedTrailId}
3202
3456
  focusedStepIndex={!sidebarTrail || focusedTrailId === sidebarTrail.id ? focusedStepIndex : null}
3457
+ proposedTrailIds={proposedTrailIds}
3458
+ proposedStepKeys={proposedStepKeys}
3459
+ stepChange={stepChange}
3460
+ showPreviousStep={showPreviousStep}
3461
+ onTogglePreviousStep={onTogglePreviousStep}
3203
3462
  detailContent={sidebarPanel}
3204
3463
  hoveredTrailStep={hoveredTrailStep}
3205
3464
  onToggleCollapsed={toggleTrailCollapsed}
@@ -3979,7 +4238,7 @@ export function SubsystemComponentGraph(props: SubsystemComponentGraphProps) {
3979
4238
  flexDirection: 'column',
3980
4239
  }}
3981
4240
  >
3982
- {!constructsOnly && !props.hideSidebar && (
4241
+ {!constructsOnly && !props.hideSidebar && !props.hideHeader && (
3983
4242
  <header
3984
4243
  style={{
3985
4244
  flexShrink: 0,
@@ -56,6 +56,8 @@ export interface TrailStepsNavProps {
56
56
  renderSteps?: (trail: SubsystemTrail) => ReactNode;
57
57
  /** Replace the step body without replacing the selected trail's header. */
58
58
  detailContent?: ReactNode;
59
+ /** Host-local trails awaiting confirmation; their header icon uses proposal color. */
60
+ proposedTrailIds?: Set<string>;
59
61
  }
60
62
 
61
63
  const PANE: CSSProperties = {
@@ -83,6 +85,7 @@ export function TrailStepsNav({
83
85
  listContent,
84
86
  renderSteps,
85
87
  detailContent,
88
+ proposedTrailIds,
86
89
  }: TrailStepsNavProps) {
87
90
  const { theme } = useTheme();
88
91
  const muted = theme.colors.textMuted ?? theme.colors.textSecondary;
@@ -113,6 +116,9 @@ export function TrailStepsNav({
113
116
  const timer = setTimeout(() => setDisplayedTrail(null), transitionMs);
114
117
  return () => clearTimeout(timer);
115
118
  }, [focused, transitionMs]);
119
+ const proposed = displayedTrail
120
+ ? (proposedTrailIds?.has(displayedTrail.id) ?? false)
121
+ : false;
116
122
 
117
123
  // A focused trail (from the graph side) always lands on the step pane.
118
124
  const leaveList = useCallback(() => {
@@ -234,9 +240,15 @@ export function TrailStepsNav({
234
240
  size={14}
235
241
  strokeWidth={2}
236
242
  aria-hidden
237
- // The focused trail: the icon stays primary, matching the row's
238
- // hover colour in the list it came from.
239
- style={{ flexShrink: 0, marginTop: 3, color: theme.colors.primary }}
243
+ // A proposed trail keeps the proposal colour here too, matching
244
+ // its row icon in the list it came from.
245
+ style={{
246
+ flexShrink: 0,
247
+ marginTop: 3,
248
+ color: proposed
249
+ ? (theme.colors.warning ?? '#d4a017')
250
+ : theme.colors.primary,
251
+ }}
240
252
  />
241
253
  {/* The title is the back affordance too: clicking it goes back to
242
254
  the list, like the trailing close. */}