MemoryStorage.tsx

src/components/MemoryStorage.tsx

MemoryStorage.tsx390 lines← Lessons
1import React from 'react';2import capture from '../generated/icrustmem-native.json';3import {Label, Text} from './Primitives';4import {theme as t} from '../theme';56/** These coordinates describe one drawing. Cameras may scale its enclosing g;7 * the components never select a smaller or less detailed representation. */8export const memoryStorageSize = {width: 1500, height: 650} as const;9export const memoryStorageTargets = {10  heap: {page: {x: 890, y: 316}, slots: {x: 674, y: 324}, owner: {x: 1260, y: 145}, pool: {x: 112, y: 490}},11  hylix: {anchor: {x: 112, y: 310}, allocation: {x: 368, y: 310}, identity: {x: 368, y: 574}, classes: {x: 810, y: 310}},12  cache: {stable: {x: 300, y: 305}, segment: {x: 1160, y: 305}, read: {x: 720, y: 270}, flush: {x: 720, y: 410}},13} as const;1415type Origin = {x?: number; y?: number};16export type NativeStorageProps = Origin & {17  /** Index in the corresponding actual native capture. -1 holds the drawing18   * before its first native allocation/read, without fabricating an outcome. */19  stepIndex: number;20  /** Progress from the previous captured state into this one. Default: complete. */21  progress?: number;22  highlight?: string;23};24const clamp = (v: number) => Math.max(0, Math.min(1, v));25const ease = (v: number) => {const p = clamp(v); return p * p * (3 - 2 * p);};26const idx = (v: number, n: number) => Math.max(0, Math.min(n - 1, Math.floor(v)));27const commas = (v: number) => v.toLocaleString('en-US');2829const Arrow: React.FC<{points: string; color?: string; opacity?: number; dashed?: boolean}> = ({points, color = t.state, opacity = 1, dashed = false}) => {30  const list = points.split(' ').map(p => p.split(',').map(Number));31  const end = list[list.length - 1], before = list[list.length - 2];32  const angle = Math.atan2(end[1] - before[1], end[0] - before[0]);33  const wing = (sign: number) => `${end[0] - 10 * Math.cos(angle) + sign * 5 * Math.sin(angle)},${end[1] - 10 * Math.sin(angle) - sign * 5 * Math.cos(angle)}`;34  return <g opacity={opacity}>35    <polyline points={points} fill="none" stroke={color} strokeWidth={2.2} strokeLinejoin="round" strokeLinecap="round" strokeDasharray={dashed ? '5 6' : undefined}/>36    <polygon points={`${end.join(',')} ${wing(1)} ${wing(-1)}`} fill={color}/>37  </g>;38};3940const MovingLabel: React.FC<{x: number; y: number; label: string; opacity?: number; color?: string}> = ({x, y, label, opacity = 1, color = t.action}) => {41  const width = Math.max(112, label.length * 10 + 36);42  return <g transform={`translate(${x - width / 2} ${y - 23})`} opacity={opacity}>43    <rect width={width} height={46} rx={23} fill={t.actionFill} stroke={color} strokeWidth={1.8}/>44    <Text x={width / 2} y={30} size={19} mono color={color} anchor="middle">{label}</Text>45  </g>;46};4748export const memorySlotSize = {width: 124, height: 104} as const;49export type MemorySlotProps = Origin & {bytes: number; name?: string; free?: boolean; opacity?: number; highlight?: boolean};50/** One native allocation slot; a free link reuses this slot's payload bytes. */51export const MemorySlot: React.FC<MemorySlotProps> = ({x = 0, y = 0, bytes, name, free = false, opacity = 1, highlight = false}) => <g transform={`translate(${x} ${y})`} opacity={opacity}>52  <rect {...memorySlotSize} rx={8} fill={name ? t.stateFill : t.white} stroke={highlight ? t.action : name ? t.state : t.line} strokeWidth={highlight ? 3 : 1.7} strokeDasharray={name ? undefined : '4 5'}/>53  <Text x={62} y={45} size={27} mono anchor="middle" color={name ? t.ink : t.muted}>{name ?? (free ? 'free' : '—')}</Text>54  <Text x={62} y={77} size={17} mono anchor="middle" color={t.muted}>{bytes} B</Text>55  {free && !name && <path d="M44 91 H80 M75 87 L80 91 L75 95" fill="none" stroke={t.state} strokeWidth={1.5}/>}56</g>;5758export type MemorySpanProps = Origin & {59  classBytes: number;60  spanBytes: number;61  slots: Array<{name?: string; free?: boolean; opacity?: number; highlight?: boolean}>;62  active?: boolean;63  highlight?: boolean;64};65export const memorySpanSize = (slots: number) => ({width: 32 + slots * memorySlotSize.width + (slots - 1) * 12, height: 212});66/** Header and slot geometry are shared by every use of a native span. */67export const MemorySpan: React.FC<MemorySpanProps> = ({x = 0, y = 0, classBytes, spanBytes, slots, active = true, highlight = false}) => {68  const size = memorySpanSize(slots.length);69  return <g transform={`translate(${x} ${y})`} opacity={active ? 1 : .32}>70    <rect {...size} rx={12} fill={active ? t.stateFill : t.paper} fillOpacity={.4} stroke={highlight ? t.action : t.state} strokeWidth={highlight ? 2.8 : 1.8} strokeDasharray={active ? undefined : '6 6'}/>71    <Text x={16} y={28} size={17} mono color={active ? t.state : t.muted}>{spanBytes} B span</Text>72    <rect x={16} y={43} width={size.width - 32} height={29} rx={4} fill={t.reducerFill} stroke={t.reducer} strokeWidth={1}/>73    <Text x={size.width / 2} y={63} size={14} mono color={t.reducer} anchor="middle">header · {capture.heap.slabHeaderBytes} B</Text>74    {slots.map((slot, i) => <MemorySlot key={i} x={16 + i * (memorySlotSize.width + 12)} y={90} bytes={classBytes} {...slot}/>)}75  </g>;76};7778export const memoryBuddySize = {width: 180, height: 104} as const;79/** A free buddy has no live slot header or values. Different buddy orders use80 * this same tile, with byte capacity in its label rather than a new glyph. */81export const FreeBuddy: React.FC<Origin & {bytes: number; opacity?: number}> = ({x = 0, y = 0, bytes, opacity = 1}) => <g transform={`translate(${x} ${y})`} opacity={opacity}>82  <rect {...memoryBuddySize} rx={9} fill={t.paper} stroke={t.state} strokeWidth={1.7} strokeDasharray="5 5"/>83  <Text x={90} y={42} size={23} mono color={t.state} anchor="middle">{bytes} B</Text>84  <Text x={90} y={76} size={17} mono color={t.muted} anchor="middle">free buddy</Text>85</g>;8687export const memoryPageSize = {width: 1100, height: 412} as const;88export type MemoryPageProps = Origin & {page?: number; owner?: 'Nyxus' | 'Runtime' | 'free'; highlight?: boolean; children?: React.ReactNode};89export const MemoryPage: React.FC<MemoryPageProps> = ({x = 0, y = 0, page = 1, owner = 'Nyxus', highlight = false, children}) => <g transform={`translate(${x} ${y})`}>90  <rect {...memoryPageSize} rx={18} fill={t.white} stroke={highlight ? t.action : owner === 'free' ? t.muted : t.block} strokeWidth={highlight ? 3 : 2.3}/>91  <Text x={24} y={37} size={20} mono color={t.block}>page {page} · {commas(capture.heap.pageBytes)} B</Text>92  <rect x={795} y={14} width={280} height={35} rx={17.5} fill={owner === 'free' ? t.paper : owner === 'Runtime' ? t.actionFill : t.stateFill} stroke={owner === 'free' ? t.line : owner === 'Runtime' ? t.action : t.state}/>93  <Text x={935} y={38} size={19} mono color={owner === 'Runtime' ? t.action : owner === 'free' ? t.muted : t.state} anchor="middle">{owner === 'free' ? 'available' : owner}</Text>94  <line x1={20} x2={1080} y1={62} y2={62} stroke={t.line}/>95  <rect x={20} y={82} width={70} height={302} rx={6} fill={t.reducerFill} stroke={t.reducer} strokeWidth={1.3}/>96  <g transform="translate(63 234) rotate(-90)"><Text x={0} y={0} size={17} mono color={t.reducer} anchor="middle">page header · {capture.heap.pageHeaderBytes} B</Text></g>97  {children}98</g>;99100type HeapSlot = typeof capture.heap.steps[number]['liveSlots'][number];101const heapOwner = (step: typeof capture.heap.steps[number]): 'Nyxus' | 'Runtime' | 'free' => step.ownedPages.Store > 0 ? 'Nyxus' : step.ownedPages.Runtime > 0 ? 'Runtime' : 'free';102const heapPage = capture.heap.steps.flatMap(s => s.liveSlots)[0].page;103const smallSpanReleaseIndex = capture.heap.steps.findIndex((step, i) => i > 0 &&104  !step.liveSlots.some(s => s.bytes === capture.heap.smallLayout.bytes) &&105  capture.heap.steps[i - 1].liveSlots.some(s => s.bytes === capture.heap.smallLayout.bytes) &&106  step.liveSlots.some(s => s.bytes === capture.heap.otherLayout.bytes));107const heapSpanOrigin = (bytes: number) => bytes === capture.heap.smallLayout.bytes ? {x: 460, y: 200} : {x: 920, y: 200};108const heapSlotPoint = (slot: HeapSlot) => {109  const layout = slot.bytes === capture.heap.smallLayout.bytes ? capture.heap.smallLayout : capture.heap.otherLayout;110  const first = capture.heap.steps.flatMap(s => s.liveSlots).find(s => s.bytes === slot.bytes)!;111  const slotIndex = Math.round((slot.offset - first.offset) / layout.slotBytes);112  const base = heapSpanOrigin(slot.bytes);113  return {x: base.x + 78 + slotIndex * 136, y: base.y + 142};114};115116/** Actual Heap/Pool capture: three 64-byte slots, the independent 192-byte117 * class, address reuse, page release, and Runtime reclamation of that page. */118export const NativeHeap: React.FC<NativeStorageProps> = ({x = 0, y = 0, stepIndex, progress = 1, highlight}) => {119  const initial = stepIndex < 0;120  const index = idx(stepIndex, capture.heap.steps.length), step = capture.heap.steps[index];121  const previous = index ? capture.heap.steps[index - 1] : undefined;122  const p = ease(progress), owner = initial ? 'free' : heapOwner(step);123  const displayOwner = owner === 'free' && previous && p < .9 ? heapOwner(previous) : owner;124  const before = previous?.liveSlots ?? [], after = initial ? [] : step.liveSlots;125  const entered = after.find(a => !before.some(b => b.name === a.name && b.offset === a.offset));126  const exited = before.find(a => !after.some(b => b.name === a.name && b.offset === a.offset));127  const currentDomain = owner === 'Runtime' || owner === 'free' && previous && heapOwner(previous) === 'Runtime' ? 'Runtime' : 'Store';128  const lastClaim = capture.heap.steps.slice(0, index + 1).reduce((found, s, i) => s.providerEvents.some(event => event.kind === 'claim' && 'owner' in event && event.owner === currentDomain) ? i : found, -1);129  const observed = initial ? [] : capture.heap.steps.slice(Math.max(0, lastClaim), index + 1).flatMap(s => s.liveSlots);130  const renderSpan = (layout: typeof capture.heap.smallLayout) => {131    const first = capture.heap.steps.flatMap(s => s.liveSlots).find(s => s.bytes === layout.bytes)!;132    const origin = heapSpanOrigin(layout.bytes);133    const currentlyLive = after.some(s => s.bytes === layout.bytes);134    const wasLive = before.some(s => s.bytes === layout.bytes);135    const active = currentlyLive || wasLive && p < .92;136    if (!observed.some(s => s.bytes === layout.bytes) || owner === 'free' && p >= .9 || !currentlyLive && !wasLive) return null;137    if (index > smallSpanReleaseIndex && layout.bytes === capture.heap.smallLayout.bytes && owner === 'Nyxus') return null;138    const slots = Array.from({length: layout.slotsPerSpan}, (_, i) => {139      const offset = first.offset + i * layout.slotBytes;140      const current = after.find(s => s.offset === offset), old = before.find(s => s.offset === offset);141      const leaving = !current && old && p < 1;142      return {name: current?.name ?? (leaving ? old.name : undefined), free: observed.some(s => s.offset === offset), opacity: current && !old ? p : leaving ? 1 - p : 1, highlight: Boolean(current && entered?.offset === offset && (highlight === 'slots' || highlight === 'reused'))};143    });144    const firstForClass = !capture.heap.steps.slice(Math.max(0, lastClaim), index).flatMap(s => s.liveSlots).some(s => s.bytes === layout.bytes);145    const release = index === smallSpanReleaseIndex && layout.bytes === capture.heap.smallLayout.bytes;146    return <g opacity={release ? 1 - ease(p / .42) : firstForClass ? p : 1}><MemorySpan x={origin.x - 340} y={origin.y - 110} classBytes={layout.slotBytes} spanBytes={layout.spanBytes} slots={slots} active={active} highlight={highlight === 'slots' && layout.bytes === 64}/></g>;147  };148  const changed = entered ?? exited;149  const point = changed ? heapSlotPoint(changed) : {x: 1040, y: 145};150  const requestLabel = entered ? `${entered.name} · ${entered.bytes} B` : exited ? `free ${exited.name}` : owner === 'free' ? 'release page' : 'claim page';151  const requestX = changed ? 700 + (point.x - 700) * p : 250 + (1040 - 250) * p;152  const requestY = changed ? 42 + (point.y - 42) * p : 490 + (145 - 490) * p;153  return <g transform={`translate(${x} ${y})`}>154    <Text x={0} y={35} size={27} weight={600}>Heap</Text>155    {[{name: 'Nyxus', y: 110, color: t.state}, {name: 'Runtime', y: 270, color: t.action}].map(h => <g key={h.name}>156      <rect x={0} y={h.y} width={225} height={103} rx={14} fill={displayOwner === h.name ? h.name === 'Nyxus' ? t.stateFill : t.actionFill : t.white} stroke={displayOwner === h.name ? h.color : t.line} strokeWidth={displayOwner === h.name ? 2.5 : 1.7}/>157      <Text x={112} y={h.y + 43} size={24} weight={500} anchor="middle">{h.name}</Text>158      <Text x={112} y={h.y + 77} size={19} mono color={h.color} anchor="middle">Heap</Text>159    </g>)}160    <Arrow points="225,162 270,162 270,450 225,450" color={t.state} opacity={displayOwner === 'Nyxus' ? .85 : .2}/>161    <Arrow points="225,322 290,322 290,490 225,490" color={t.action} opacity={displayOwner === 'Runtime' ? .85 : .2}/>162    <rect x={0} y={430} width={225} height={112} rx={14} fill={t.white} stroke={highlight === 'owner' ? t.action : t.block} strokeWidth={2}/>163    <Text x={112} y={476} size={25} mono anchor="middle">Pool</Text>164    <Text x={112} y={511} size={17} color={t.muted} anchor="middle">page ownership</Text>165    <Arrow points="225,526 320,526 320,316 340,316" color={t.block}/>166    <g opacity={initial ? 0 : index === 0 ? p : 1}><MemoryPage x={340} y={110} page={heapPage} owner={displayOwner} highlight={highlight === 'page' || highlight === 'owner'}>167      {renderSpan(capture.heap.smallLayout)}168      {renderSpan(capture.heap.otherLayout)}169      <rect x={920} y={90} width={155} height={290} rx={10} fill={t.paper} stroke={t.line} strokeDasharray="5 5"/>170      <Text x={997} y={223} size={17} mono color={t.muted} anchor="middle">free</Text>171      <Text x={997} y={252} size={17} mono color={t.muted} anchor="middle">buddies</Text>172    </MemoryPage></g>173    {!initial && owner !== 'free' && index !== smallSpanReleaseIndex && (index < smallSpanReleaseIndex || owner === 'Runtime') && <g transform="translate(788 435) scale(.55)"><FreeBuddy bytes={capture.heap.smallLayout.spanBytes}/></g>}174    {owner === 'Nyxus' && index === smallSpanReleaseIndex && <g>175      <Text x={674} y={246} size={17} mono color={t.state} anchor="middle" opacity={ease((p - .2) / .3)}>buddy merge</Text>176      <FreeBuddy x={476 + 108 * ease((p - .5) / .5)} y={278} bytes={capture.heap.smallLayout.spanBytes} opacity={ease((p - .2) / .25) * (1 - ease((p - .76) / .24))}/>177      <g transform={`translate(${788 + (692 - 788) * ease(p / .5) - 108 * ease((p - .5) / .5)} ${435 + (278 - 435) * ease(p / .5)}) scale(${.55 + .45 * ease(p / .5)})`} opacity={1 - ease((p - .76) / .24)}><FreeBuddy bytes={capture.heap.smallLayout.spanBytes}/></g>178      <FreeBuddy x={584} y={278} bytes={capture.heap.smallLayout.spanBytes * 2} opacity={ease((p - .76) / .24)}/>179    </g>}180    {!initial && progress < .98 && <MovingLabel x={requestX} y={requestY} label={requestLabel} opacity={Math.min(1, (1 - progress) * 5)}/>}181  </g>;182};183184export const hylixBlockSize = {width: 300, height: 82} as const;185export type HylixBlockProps = Origin & {offset?: number; generation?: number; rejected?: boolean; active?: boolean; opacity?: number};186/** The nonowning Block carrier, distinct from the allocation it names. */187export const HylixBlock: React.FC<HylixBlockProps> = ({x = 0, y = 0, offset, generation, rejected = false, active = false, opacity = 1}) => <g transform={`translate(${x} ${y})`} opacity={opacity}>188  <rect {...hylixBlockSize} rx={11} fill={rejected ? t.actionFill : t.blockFill} stroke={rejected ? t.danger : active ? t.state : t.block} strokeWidth={active || rejected ? 2.5 : 1.8}/>189  <Text x={16} y={25} size={17} mono color={rejected ? t.danger : t.block}>Block</Text>190  <Text x={16} y={59} size={19} mono>{offset === undefined ? 'offset' : `offset ${commas(offset)}`}</Text>191  <Text x={282} y={59} size={19} mono color={rejected ? t.danger : t.state} anchor="end">{generation === undefined ? 'generation' : `gen ${generation}`}</Text>192</g>;193194export const stableAllocationSize = {width: 264, height: 180} as const;195type StableAllocationProps = Origin & {name?: string; offset: number; generation: number; length?: number; spanBytes: number; live?: boolean; highlight?: boolean; opacity?: number};196/** Canonical arena allocation: checked 16-byte header, initialized payload,197 * and spare capacity. The tile is schematic rather than a byte-width scale. */198export const StableAllocation: React.FC<StableAllocationProps> = ({x = 0, y = 0, name, offset, generation, length = 0, spanBytes, live = true, highlight = false, opacity = 1}) => {199  const capacity = spanBytes - capture.hylix.headerBytes;200  return <g transform={`translate(${x} ${y})`} opacity={opacity}>201    <rect {...stableAllocationSize} rx={10} fill={live ? t.blockFill : t.paper} stroke={highlight ? t.action : live ? t.block : t.line} strokeWidth={highlight ? 2.8 : 1.8} strokeDasharray={live ? undefined : '5 5'}/>202    <rect x={1} y={1} width={262} height={38} rx={9} fill={t.reducerFill}/>203    <line x1={0} x2={264} y1={40} y2={40} stroke={t.reducer} opacity={.45}/>204    <Text x={13} y={26} size={15} mono color={t.reducer}>{capture.hylix.headerBytes} B header</Text>205    <Text x={249} y={26} size={15} mono color={t.reducer} anchor="end">gen {generation}</Text>206    <Text x={18} y={84} size={26} mono color={live ? t.ink : t.muted}>{live ? name : 'free'}</Text>207    <Text x={18} y={117} size={19} mono color={live ? t.ink : t.muted}>{live ? `${length} B` : `${spanBytes} B span`}</Text>208    <rect x={16} y={139} width={232} height={17} rx={3} fill={t.white} stroke={t.line}/>209    {live && <rect x={16} y={139} width={232 * Math.min(1, length / capacity)} height={17} rx={3} fill={t.block} opacity={.65}/>}210    <Text x={264 / 2} y={205} size={16} mono color={t.muted} anchor="middle">@{commas(offset)}</Text>211  </g>;212};213214type HylixLive = typeof capture.hylix.steps[number]['liveBlocks'][number];215const historyBlock = (index: number, offset: number) => capture.hylix.steps.slice(0, index + 1).flatMap(s => s.liveBlocks).filter(b => b.identity.offset === offset).at(-1);216const liveAt = (blocks: HylixLive[], offset: number) => blocks.find(b => b.identity.offset === offset);217const firstIdentity = capture.hylix.steps[0].liveBlocks[0].identity;218const stableLocations = [219  {offset: 4112, x: 236, y: 220, spanBytes: 256},220  {offset: 4368, x: 518, y: 220, spanBytes: 256},221  {offset: 8208, x: 834, y: 220, spanBytes: 32},222  {offset: 12304, x: 1148, y: 220, spanBytes: 512},223] as const;224225/** Actual Arena capture. Empty class slabs remain where they were allocated;226 * free spans never merge, and the fixed arena anchor survives reopen. */227export const HylixArena: React.FC<NativeStorageProps> = ({x = 0, y = 0, stepIndex, progress = 1, highlight}) => {228  const initial = stepIndex < 0;229  const index = idx(stepIndex, capture.hylix.steps.length), step = capture.hylix.steps[index];230  const previous = index ? capture.hylix.steps[index - 1] : undefined, p = ease(progress);231  const before = previous?.liveBlocks ?? [], after = initial ? [] : step.liveBlocks;232  const entered = after.find(a => !before.some(b => b.identity.offset === a.identity.offset && b.identity.generation === a.identity.generation));233  const staleObserved = (at: number) => capture.hylix.steps.slice(0, at + 1).some(s => 'staleReadRejected' in s.details && s.details.staleReadRejected === true || 'staleWriteRejected' in s.details && s.details.staleWriteRejected === true);234  const firstNow = liveAt(after, firstIdentity.offset), rejected = staleObserved(index) && (staleObserved(index - 1) || p >= .6);235  const lastOwner = after.find(b => b.name === 'grown') ?? firstNow;236  const showSecond = step.frontier > 8192, showThird = step.frontier > 12288;237  const renderAllocation = (location: typeof stableLocations[number]) => {238    const current = liveAt(after, location.offset), old = liveAt(before, location.offset), historical = historyBlock(index, location.offset);239    if (initial || !historical) return null;240    const leaving = !current && old && p < 1;241    const block = current ?? (leaving ? old : historical);242    const live = Boolean(current || leaving);243    const born = Boolean(current && (!old || old.identity.generation !== current.identity.generation));244    return <StableAllocation key={location.offset} x={location.x} y={location.y} offset={location.offset} spanBytes={location.spanBytes} generation={block.identity.generation} length={block.length} name={block.name} live={live} highlight={highlight === 'generation' && location.offset === firstIdentity.offset} opacity={born ? p : leaving ? 1 - .5 * p : 1}/>;245  };246  const oldBlockX = rejected ? 220 : 218, newBlockX = 620;247  const details = step.details as {prefixPreservedBytes?: number; staleReadRejected?: boolean; staleWriteRejected?: boolean; oldIdentityRejected?: boolean};248  return <g transform={`translate(${x} ${y})`}>249    <Text x={0} y={35} size={27} weight={600}>Arena</Text>250    <rect x={0} y={100} width={1480} height={400} rx={18} fill={t.white} stroke={t.block} strokeWidth={2.2}/>251    <Label x={24} y={136} color={t.block}>stable64</Label>252    {!initial && <Text x={1455} y={136} size={17} mono color={t.muted} anchor="end">frontier {commas(step.frontier)}</Text>}253    <rect x={24} y={165} width={175} height={285} rx={10} fill={t.reducerFill} stroke={highlight === 'anchor' ? t.action : t.reducer} strokeWidth={highlight === 'anchor' ? 3 : 1.8}/>254    <Text x={111} y={208} size={22} mono color={t.reducer} anchor="middle">anchor</Text>255    <line x1={40} x2={183} y1={228} y2={228} stroke={t.reducer} opacity={.3}/>256    {['contract', 'frontier', 'generation', 'class cursors', 'free heads'].map((line, i) => <Text key={line} x={41} y={264 + i * 34} size={15} mono color={t.reducer}>{line}</Text>)}257    {!initial && <g opacity={index === 0 ? p : 1}>258      <rect x={220} y={165} width={580} height={285} rx={10} fill={t.blockFill} fillOpacity={.24} stroke={t.block} strokeWidth={1.4}/>259      <Text x={236} y={196} size={18} mono color={t.block}>256 B class</Text>260    </g>}261    {showSecond && <g opacity={previous && previous.frontier <= 8192 ? p : 1}>262      <rect x={820} y={165} width={292} height={285} rx={10} fill={t.blockFill} fillOpacity={.24} stroke={t.block} strokeWidth={1.4}/>263      <Text x={834} y={196} size={18} mono color={t.block}>32 B class</Text>264    </g>}265    {showThird && <g opacity={p}>266      <rect x={1132} y={165} width={324} height={285} rx={10} fill={t.blockFill} fillOpacity={.24} stroke={t.block} strokeWidth={1.4}/>267      <Text x={1148} y={196} size={18} mono color={t.block}>512 B class</Text>268    </g>}269    {stableLocations.map(renderAllocation)}270    {!initial && <g opacity={index === 0 ? p : 1}>271      <Arrow points={`${oldBlockX + 150},530 ${oldBlockX + 150},450`} color={rejected ? t.danger : t.block} dashed={rejected}/>272      <HylixBlock x={oldBlockX} y={533} offset={firstIdentity.offset} generation={firstIdentity.generation} rejected={rejected} active={!rejected}/>273    </g>}274    {rejected && <g>275      <path d={`M${oldBlockX + 140} 469 l20 20 M${oldBlockX + 160} 469 l-20 20`} stroke={t.danger} strokeWidth={3} strokeLinecap="round"/>276      <Text x={oldBlockX + 150} y={641} size={18} color={t.danger} mono anchor="middle">stale block</Text>277    </g>}278    {lastOwner && lastOwner.identity.generation !== firstIdentity.generation && <g opacity={entered ? p : 1}>279      <HylixBlock x={newBlockX} y={533} offset={lastOwner.identity.offset} generation={lastOwner.identity.generation} active/>280      <Arrow points={`${newBlockX + 150},530 ${newBlockX + 150},477 ${lastOwner.identity.offset === firstIdentity.offset ? 500 : 1280},477 ${lastOwner.identity.offset === firstIdentity.offset ? 500 : 1280},450`} color={t.state}/>281    </g>}282    {details.prefixPreservedBytes && <g>283      <Arrow points="500,466 1148,466" color={t.block} opacity={p}/>284      <Text x={1065} y={490} size={16} mono color={t.block} anchor="end">{details.prefixPreservedBytes} B copied</Text>285    </g>}286    {entered && progress < .98 && <MovingLabel x={600 + ((stableLocations.find(l => l.offset === entered.identity.offset)?.x ?? 236) + 132 - 600) * p} y={58 + (305 - 58) * p} label={`${entered.length} B`} opacity={Math.min(1, (1 - progress) * 5)}/>}287  </g>;288};289290const byteSegmentSize = {width: 460, height: 194} as const;291const byteStart = 44, byteWidth = 9, byteY = 112;292/** The same segment drawing is used on both sides of the provider copy. A293 * labelled payload window makes small native edits visible inside 4096 bytes. */294const ByteSegment: React.FC<Origin & {edited?: number; dirty?: number; present?: number; highlight?: boolean}> = ({x = 0, y = 0, edited = 0, dirty = 0, present = 1, highlight = false}) => <g transform={`translate(${x} ${y})`}>295  <rect {...byteSegmentSize} rx={12} fill={t.white} stroke={highlight ? t.action : t.block} strokeWidth={highlight ? 2.8 : 1.8}/>296  <g opacity={present}>297    <Text x={20} y={34} size={20} mono color={t.block}>{commas(capture.cache.segmentBytes)} B segment</Text>298    <Text x={440} y={34} size={16} mono color={t.muted} anchor="end">stable @4,096</Text>299    <line x1={20} x2={440} y1={51} y2={51} stroke={t.line}/>300    <Text x={20} y={83} size={16} mono color={t.muted}>payload @{commas(capture.cache.block.offset)}</Text>301    <Text x={440} y={83} size={15} mono color={t.muted} anchor="end">0…40 B</Text>302    {Array.from({length: 40}, (_, i) => {303      const changed = capture.cache.edits.some(edit => i >= edit.at && i < edit.at + edit.bytes);304      return <g key={i}>305        <rect x={byteStart + i * byteWidth} y={byteY} width={byteWidth - .7} height={34} rx={1} fill={t.blockFill} stroke={t.block} strokeWidth={.45}/>306        {changed && <rect x={byteStart + i * byteWidth} y={byteY} width={byteWidth - .7} height={34} rx={1} fill={t.state} opacity={edited}/>} 307      </g>;308    })}309    <rect x={byteStart + capture.cache.flushedPayloadRange.start * byteWidth - 3} y={byteY - 6} width={capture.cache.flushedPayloadRange.bytes * byteWidth + 5} height={46} rx={4} fill={t.actionFill} fillOpacity={dirty * .35} stroke={t.action} strokeWidth={2} opacity={dirty}/>310    {[0, 4, 32, 36].map(v => <Text key={v} x={byteStart + v * byteWidth} y={172} size={13} mono color={t.muted} anchor="middle">{v}</Text>)}311  </g>312</g>;313314/** Actual counted-provider events drive the read/copy and merged 32-byte315 * publication. Diagnostic cumulative dirty counters are never pending bytes. */316export const WorkingSet: React.FC<NativeStorageProps> = ({x = 0, y = 0, stepIndex, progress = 1, highlight}) => {317  const index = idx(stepIndex, capture.cache.steps.length), step = capture.cache.steps[index], p = ease(progress);318  const write = step.providerEvents.find(event => event.kind === 'write');319  const read = step.providerEvents.find(event => event.kind === 'read');320  const hasPublished = capture.cache.steps.slice(0, index).some(s => s.providerEvents.some(event => event.kind === 'write'));321  const stableEdited = hasPublished ? 1 : write ? p : 0;322  const cacheEdited = index < 2 ? 0 : index === 2 ? p : 1;323  const dirty = index === 2 ? p : index === 3 ? 1 - p : 0;324  const present = index === 0 || index === 6 ? p : index === 5 ? 1 - p : 1;325  const movingRead = Boolean(read && progress < .99), movingWrite = Boolean(write && progress < .99);326  return <g transform={`translate(${x} ${y})`}>327    <Text x={0} y={35} size={27} weight={600}>WorkingSet</Text>328    <rect x={20} y={120} width={560} height={370} rx={18} fill={t.white} stroke={t.block} strokeWidth={2}/>329    <Label x={45} y={159} color={t.block}>stable64</Label>330    <ByteSegment x={70} y={210} edited={stableEdited} highlight={highlight === 'stable'}/>331    <rect x={860} y={120} width={620} height={370} rx={18} fill={t.white} stroke={t.action} strokeWidth={2}/>332    <Label x={884} y={159} color={t.action}>transient main memory</Label>333    <ByteSegment x={940} y={210} edited={cacheEdited} dirty={dirty} present={present} highlight={highlight === 'segment' || highlight === 'dirty'}/>334    <Arrow points="590,270 850,270" color={t.block} opacity={read ? .95 : .15}/>335    <Arrow points="850,410 590,410" color={t.state} opacity={write ? .95 : .15}/>336    {read && <Text x={720} y={241} size={19} mono color={t.block} anchor="middle">{commas(read.bytes)} B</Text>}337    {write && <Text x={720} y={449} size={20} mono color={t.state} anchor="middle">{write.bytes} B</Text>}338    {movingRead && <MovingLabel x={620 + 200 * p} y={270} label={`${commas(read!.bytes)} B`} color={t.block}/>}339    {movingWrite && <MovingLabel x={820 - 200 * p} y={410} label={`${write!.bytes} B`} color={t.state}/>}340    <Text x={1170} y={532} size={18} mono color={t.muted} anchor="middle">segment cache · ≤{capture.cache.maxSegments}</Text>341    {index === 1 && <g opacity={p}>342      <path d="M1190 193 C1240 163 1310 172 1325 200" fill="none" stroke={t.state} strokeWidth={2}/>343      <path d="M1316 194 L1325 200 L1327 189" fill="none" stroke={t.state} strokeWidth={2}/>344    </g>}345    {index === 2 && <g opacity={p}>346      <Text x={1160} y={455} size={18} mono color={t.action} anchor="middle">{capture.cache.edits.map(e => `${e.bytes} B`).join(' + ')}</Text>347    </g>}348    {index === 3 && <g opacity={p}>349      <Text x={1160} y={455} size={18} mono color={t.state} anchor="middle">clean</Text>350    </g>}351    {index === 4 && <Text x={1160} y={455} size={18} mono color={t.state} anchor="middle">clean</Text>}352    {index === 5 && <MovingLabel x={1160} y={455 + 80 * p} label="query exit" opacity={1 - p}/>}353  </g>;354};355356export const nativeMapSize = {width: 600, height: 420} as const;357export const memoryMapNodeSize = {width: 240, height: 112} as const;358type MapPair = {key: string; value: string};359export const MemoryMapNode: React.FC<Origin & {pairs: MapPair[]; engine?: 'nyxus' | 'hylix'; active?: boolean}> = ({x = 0, y = 0, pairs, engine = 'nyxus', active = false}) => <g transform={`translate(${x} ${y})`}>360  <rect {...memoryMapNodeSize} rx={10} fill={engine === 'nyxus' ? t.stateFill : t.blockFill} stroke={active ? t.action : engine === 'nyxus' ? t.state : t.block} strokeWidth={active ? 2.8 : 1.8}/>361  <Text x={13} y={22} size={13} mono color={t.muted}>{engine === 'hylix' ? '8 B node header' : 'native node'}</Text>362  <line x1={12} x2={228} y1={31} y2={31} stroke={t.line}/>363  {[0, 1, 2].map(i => {364    const pair = pairs.length === 1 ? i === 1 ? pairs[0] : undefined : pairs[i];365    return <g key={i} opacity={pair ? 1 : .2}>366      <rect x={13 + i * 74} y={41} width={66} height={56} rx={4} fill={t.white} stroke={engine === 'nyxus' ? t.state : t.block} strokeWidth={.8}/>367      <Text x={46 + i * 74} y={64} size={19} mono anchor="middle">{pair?.key ?? '·'}</Text>368      <Text x={46 + i * 74} y={87} size={17} mono color={engine === 'nyxus' ? t.state : t.block} anchor="middle">{pair?.value ?? '·'}</Text>369    </g>;370  })}371</g>;372373/** A source-derived representation illustration, not a captured insertion or374 * claimed node capacity. Both engines use a B-tree: internal keys own values.375 * Nyxus follows native pointers; Hylix follows offset/generation Blocks. */376export const NativeMap: React.FC<Origin & {engine?: 'nyxus' | 'hylix'; active?: boolean; progress?: number}> = ({x = 0, y = 0, engine = 'nyxus', active = false, progress = 0}) => {377  const p = ease(progress), color = engine === 'nyxus' ? t.state : t.block;378  const pairs = (numbers: number[]) => numbers.map(n => ({key: `k${n}`, value: `v${n}`}));379  return <g transform={`translate(${x} ${y})`}>380    <Text x={0} y={23} size={22} mono color={color}>Map</Text>381    <Arrow points="300,177 300,216 130,216 130,275" color={color}/>382    <Arrow points="300,177 300,216 470,216 470,275" color={color}/>383    <Text x={112} y={251} size={14} mono color={color} anchor="middle">{engine === 'nyxus' ? 'pointer' : 'Block'}</Text>384    <Text x={494} y={251} size={14} mono color={color} anchor="middle">{engine === 'nyxus' ? 'pointer' : 'Block'}</Text>385    <MemoryMapNode x={180} y={65} pairs={pairs([4])} engine={engine} active={active && p < .4}/>386    <MemoryMapNode x={10} y={275} pairs={pairs([1, 2, 3])} engine={engine}/>387    <MemoryMapNode x={350} y={275} pairs={pairs([5, 6, 7])} engine={engine} active={active && p > .7}/>388    {active && <circle cx={p < .25 ? 300 : p < .75 ? 300 + (p - .25) * 340 : 470} cy={p < .25 ? 177 + p * 156 : p < .75 ? 216 : 216 + (p - .75) * 236} r={6} fill={t.action}/>}389  </g>;390};