Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): 压缩聊天区的拖缝按像素口径接力出账,修压不到 400px 下限 (#2905)

Lizi41e477e3647b
diff --git a/apps/desktop/src/renderer/layout/LayoutRoot.tsx b/apps/desktop/src/renderer/layout/LayoutRoot.tsx
index d9e4e9adc2..ebe70a5245 100644
--- a/apps/desktop/src/renderer/layout/LayoutRoot.tsx
+++ b/apps/desktop/src/renderer/layout/LayoutRoot.tsx
@@ -3,6 +3,7 @@ import { useEffect, useMemo, useRef, useState, type CSSProperties, type ReactNod
import {
MIN_SPLIT_CHILD_FRACTION,
transferSplitFraction,
+ transferSplitFractionRelay,
type Layout,
type LayoutNode,
type PaneNode,
@@ -381,6 +382,8 @@ interface RootDividerProps extends RootDividerPropsExtra {
splitId: string;
left: SplitChildEntry;
right: SplitChildEntry;
+ /** 同一分割的全部在场子项(含缝两侧):压缩 chat 的接力出账要按实测宽找出折叠兄弟。 */
+ visibleSiblings: SplitChildEntry[];
avail: number;
/** 拖动中的瞬时**在场份额**覆盖(paneId → share);null = 结束。 */
onLive: (live: Record<string, number> | null) => void;
@@ -388,14 +391,37 @@ interface RootDividerProps extends RootDividerPropsExtra {
onCommitted: (layout: Layout) => void;
}
+/** 压缩 chat 提交的接力计划(见 RootDivider.chatShrinkPlan)。 */
+interface RelayPlan {
+ /** 出账下标序:chat 在前,折叠兄弟按树序在后;各自保 0.05 下限。 */
+ sources: number[];
+ /** 进账方(缝的另一侧)。 */
+ receiver: number;
+}
+
+function isChatPane(node: LayoutNode): boolean {
+ return node.type === 'pane' && node.panelKind === 'chat-main';
+}
+
/** 起拖时实测某布局节点的宽;split 量自己的 grid 列壳,pane 量标准拖拽根。 */
function measuredPanePx(node: LayoutNode): number | null {
+ const width = rawPanePx(node);
+ return width !== null && width > 0 ? width : null;
+}
+
+/**
+ * 同上,但**元素缺失(返回 null)与实测为 0(返回 0)分开**:折叠成 0 宽的面板
+ * 实测恰为 0 —— 接力出账要认出它们;元素根本不在 DOM(未挂载)才是 null。
+ */
+function rawPanePx(node: LayoutNode): number | null {
const selector =
node.type === 'pane'
? `[data-panel-drag-root="${node.panelKind}"]`
: `[data-layout-node-id="${node.id}"]`;
- const width = document.querySelector(selector)?.getBoundingClientRect().width;
- return typeof width === 'number' && width > 0 ? width : null;
+ const el = document.querySelector(selector);
+ if (!el) return null;
+ const width = el.getBoundingClientRect().width;
+ return typeof width === 'number' && width >= 0 ? width : null;
}
/**
@@ -407,6 +433,7 @@ function RootDivider({
splitId,
left,
right,
+ visibleSiblings,
avail,
chatRenderedPx,
shareScale,
@@ -417,13 +444,24 @@ function RootDivider({
const draggingRef = useRef(false);
/** amountToLeft:**在场份额**增量(> 0 = 左侧变宽);写树前乘 shareScale 换成树份额。 */
- const commit = (amountToLeft: number) => {
+ const commit = (amountToLeft: number, relay: RelayPlan | null) => {
const treeAmount = amountToLeft * shareScale;
if (treeAmount === 0) return;
+ // 压缩 chat 的提交走接力:chat 先扣到 0.05 下限,差额由折叠兄弟出账 ——
+ // 拖动夹取已保证按同一来源序必可表达,不会整单被拒回弹(见 chatShrinkPlan)。
+ const compressingChat =
+ relay !== null && (relay.receiver === right.treeIndex ? treeAmount < 0 : treeAmount > 0);
try {
const current = window.electronAPI.layout.getStateSync().layout;
- const op =
- treeAmount > 0
+ const op = compressingChat
+ ? transferSplitFractionRelay(
+ current,
+ splitId,
+ relay!.sources,
+ relay!.receiver,
+ Math.abs(treeAmount),
+ )
+ : treeAmount > 0
? transferSplitFraction(current, splitId, right.treeIndex, left.treeIndex, treeAmount)
: transferSplitFraction(current, splitId, left.treeIndex, right.treeIndex, -treeAmount);
if (!op.applied) return;
@@ -461,6 +499,44 @@ function RootDivider({
return Math.max(0, Math.min(pxRoom / avail, floorRoom));
};
+ /**
+ * 压缩 chat 方向的行程与接力计划:仅缝一侧是 chat 且**量得到可信实测宽**时启用
+ * (实测 < 400 视为量不到 —— chat 在场恒 ≥ 400px,jsdom 无布局引擎恒为 0)。
+ * - 像素口径:实测宽 − 400 是唯一产品硬限(2026-07-09 定案:只有 chat 有硬下限);
+ * - 账本口径不再被 chat 自己的 0.05 地板直接封顶,而是"可表达总额" = chat 地板
+ * 余量 + 各折叠兄弟(实测宽 ≈ 0 却占着账)的地板余量;松手按同一来源序经
+ * transferSplitFractionRelay 接力写树,夹取值必可表达,不会整单被拒回弹。
+ * 画面上 chat 吸收的折叠空间本就记在这些兄弟账上 —— 由它们出账,账本随拖动
+ * 与画面重新对齐(2026-08-17 实测:chat 份额被拖到 0.05 顶死后,缝因两方转移
+ * 够不着账本地板而彻底冻住,压不到 400px)。
+ * 返回 null = 量不到可信实测,调用方回落旧口径 sideRoomShare(行为不变)。
+ */
+ const chatShrinkPlan = (
+ chatEntry: SplitChildEntry,
+ otherSide: SplitChildEntry,
+ ): { room: number; relay: RelayPlan } | null => {
+ if (!(avail > 0)) return null;
+ const measured = rawPanePx(chatEntry.node);
+ if (measured === null || measured < CHAT_MIN_PX) return null;
+ const floorRoom = (entry: SplitChildEntry): number =>
+ shareScale > 0 ? Math.max(0, (entry.fraction - MIN_SPLIT_CHILD_FRACTION) / shareScale) : 0;
+ let sourcesRoom = floorRoom(chatEntry);
+ const idleSources: number[] = [];
+ for (const sibling of visibleSiblings) {
+ if (sibling === chatEntry || sibling === otherSide) continue;
+ const width = rawPanePx(sibling.node);
+ if (width !== null && width < 1) {
+ sourcesRoom += floorRoom(sibling);
+ idleSources.push(sibling.treeIndex);
+ }
+ }
+ const pxRoom = Math.max(0, measured - CHAT_MIN_PX) / avail;
+ return {
+ room: Math.min(pxRoom, sourcesRoom),
+ relay: { sources: [chatEntry.treeIndex, ...idleSources], receiver: otherSide.treeIndex },
+ };
+ };
+
const onPointerDown = (e: React.PointerEvent) => {
if (e.button !== 0 || draggingRef.current) return;
e.preventDefault();
@@ -468,10 +544,16 @@ function RootDivider({
const startX = e.clientX;
const startL = left.share;
const startR = right.share;
- const isChat = (node: LayoutNode) => node.type === 'pane' && node.panelKind === 'chat-main';
// 起拖只量一次(拖动期间界面静止,没有失效场景);chat 量不到时回落账本估值。
- const dMin = -sideRoomShare(left, isChat(left.node) ? chatRenderedPx : null);
- const dMax = sideRoomShare(right, isChat(right.node) ? chatRenderedPx : null);
+ const chatEntry = isChatPane(left.node) ? left : isChatPane(right.node) ? right : null;
+ const shrinkPlan = chatEntry
+ ? chatShrinkPlan(chatEntry, chatEntry === left ? right : left)
+ : null;
+ let dMin = -sideRoomShare(left, isChatPane(left.node) ? chatRenderedPx : null);
+ let dMax = sideRoomShare(right, isChatPane(right.node) ? chatRenderedPx : null);
+ if (shrinkPlan && chatEntry === left) dMin = -shrinkPlan.room;
+ if (shrinkPlan && chatEntry === right) dMax = shrinkPlan.room;
+ const relay: RelayPlan | null = shrinkPlan?.relay ?? null;
let lastD = 0;
document.body.classList.add('resizing-pane');
@@ -493,7 +575,7 @@ function RootDivider({
document.removeEventListener('pointermove', onMove);
document.removeEventListener('pointerup', onUp);
document.removeEventListener('pointercancel', onCancel);
- if (commitIt && lastD !== 0) commit(lastD);
+ if (commitIt && lastD !== 0) commit(lastD, relay);
onLive(null);
};
const onUp = () => finish(true);
@@ -505,9 +587,20 @@ function RootDivider({
// 双击:两侧份额均分(把差值的一半转给少的一侧)—— 右栏旧"双击复位 50/50"
// 在两块布局下的语义等价推广。在场份额口径:均分的是**画面上**这两块的宽度。
+ // 压缩 chat 的方向同样走接力(计划即时按当前实测现算),且不得越过 chat 400px 下限。
const onDoubleClick = () => {
const total = left.share + right.share;
- commit(total / 2 - left.share);
+ const chatEntry = isChatPane(left.node) ? left : isChatPane(right.node) ? right : null;
+ const plan = chatEntry ? chatShrinkPlan(chatEntry, chatEntry === left ? right : left) : null;
+ let d = total / 2 - left.share;
+ if (plan) {
+ // 均分 delta 压缩 chat 侧时,夹取到接力计划的可压余量,防止越过 400px 下限。
+ const compressingChat = chatEntry === left ? d < 0 : d > 0;
+ if (compressingChat) {
+ d = chatEntry === left ? Math.max(d, -plan.room) : Math.min(d, plan.room);
+ }
+ }
+ commit(d, plan?.relay ?? null);
};
return (
@@ -824,6 +917,7 @@ export function LayoutRoot({ suppressNonChatPanels = false }: LayoutRootProps =
splitId={content.id}
left={prev}
right={entry}
+ visibleSiblings={visible}
avail={avail}
chatRenderedPx={chatRenderedPx}
shareScale={ledger.scale}
diff --git a/apps/desktop/src/renderer/layout/__tests__/rootDividerDrag.test.tsx b/apps/desktop/src/renderer/layout/__tests__/rootDividerDrag.test.tsx
index 91e0403b08..719641bfe2 100644
--- a/apps/desktop/src/renderer/layout/__tests__/rootDividerDrag.test.tsx
+++ b/apps/desktop/src/renderer/layout/__tests__/rootDividerDrag.test.tsx
@@ -5,7 +5,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { createDefaultLayout, type Layout, type SplitNode } from '../../../shared/layoutTree';
import { BuiltinPanelBridgeProvider, type BuiltinPanelBridge } from '../../panels/BuiltinPanelBridge';
import { __resetBuiltinPanelsForTest } from '../../panels/builtinPanels';
-import { __resetPanelRegistryForTest } from '../../panels/registry';
+import { __resetPanelRegistryForTest, registerPanelKind } from '../../panels/registry';
import { LayoutRoot } from '../LayoutRoot';
import { ContentAvailableWidthProvider, usePanelWidth } from '../paneWidths';
@@ -95,8 +95,14 @@ function mockPaneWidth(kind: string, width: number): void {
/** 把缝一路拖向左(右侧面板变宽)到抓不动为止,松手。 */
function dragDividerLeft(container: HTMLElement, byPx: number): void {
- const grab = container.querySelector('[data-testid="layout-divider"] > div');
- expect(grab).not.toBeNull();
+ dragDividerLeftAt(container, byPx, 0);
+}
+
+/** 同上,指定第几条缝(多面板在场时缝不止一条)。 */
+function dragDividerLeftAt(container: HTMLElement, byPx: number, dividerIndex: number): void {
+ const grabs = container.querySelectorAll('[data-testid="layout-divider"] > div');
+ const grab = grabs[dividerIndex];
+ expect(grab).not.toBeFalsy();
fireEvent.pointerDown(grab!, { button: 0, pointerId: 1, clientX: 1000 });
fireEvent.pointerMove(document, { pointerId: 1, clientX: 1000 - byPx });
fireEvent.pointerUp(document, { pointerId: 1, clientX: 1000 - byPx });
@@ -196,4 +202,74 @@ describe('RootDivider 拖宽 · 在场份额口径', () => {
const givenShare = (realChatPx - CHAT_MIN) / AVAIL;
expect(committedShares()['chat-main'].fraction).toBeCloseTo(0.6 - givenShare, 3);
});
+
+ // 2026-08-17 实测:装了带面板的插件(insertRootSplitPane 把 chat 份额 0.5→0.4)
+ // 且右栏折叠成 0 宽 —— 右栏的 0.4 份额还记在账上,画面那块被弹性 chat 吸收
+ // (chat 实测 1328px)。旧口径里 chat 的可让空间被自己的 0.05 账本地板提前夹死,
+ // 缝停在 chat ≈ 747px 处,压不到 400px 产品下限;修复后差额由折叠右栏接力出账。
+ it('装插件 + 右栏折叠:chat 压到 400px,差额从折叠右栏的账上接力出账', () => {
+ const layout = createDefaultLayout();
+ const split = layout.content as SplitNode;
+ split.children[0].fraction = 0.4; // chat(装入 ghost ×0.8)
+ split.children[1].fraction = 0.4; // right-tabs
+ split.children.splice(1, 0, {
+ fraction: 0.2,
+ node: { type: 'pane', id: 'ghost-repro', panelKind: 'ghost:repro' },
+ });
+ currentLayout = layout;
+ registerPanelKind({
+ kind: 'ghost:repro',
+ collapseMemory: 'global',
+ Component: () => <div data-panel-drag-root="ghost:repro" />,
+ });
+ const { container } = renderLayoutRoot();
+ mockPaneWidth('chat-main', 1328); // 吸收了折叠右栏的空间
+ mockPaneWidth('right-tabs', 0); // 折叠成 0 宽,账上仍占 0.4
+ mockPaneWidth('ghost:repro', 332);
+ dragDividerLeft(container, 1000);
+
+ expect(setCalls).toHaveLength(1); // 接力写树成功,没回弹
+ const children = (setCalls[0].content as SplitNode).children;
+ expect(children[0].fraction).toBeCloseTo(0.05, 3); // chat 先扣到账本下限
+ expect(children[1].fraction).toBeCloseTo(0.759, 3); // 收方全额进账
+ expect(children[2].fraction).toBeCloseTo(0.191, 3); // 折叠右栏接力出账,仍 ≥ 0.05
+ expect(children.reduce((s, c) => s + c.fraction, 0)).toBeCloseTo(1, 6);
+ // 画面:chat = AVAIL − ghost 1260 − 折叠右栏 0 = 400,正好产品下限。
+ expect((1 - children[1].fraction) * AVAIL).toBeCloseTo(CHAT_MIN, 0);
+ });
+
+ // 用户现场树:chat 树份额已被历史拖动顶死在 0.05 下限,右栏折叠着(0.206)。
+ // 旧口径下压缩 chat 的账本余量为 0,缝一个像素都拖不动;修复后差额全部由
+ // 折叠右栏出账,chat 保住下限、画面落到 400px。
+ it('chat 树份额已顶死 0.05 的现场树:缝解冻,差额全部由折叠右栏出账', () => {
+ const layout = createDefaultLayout();
+ (layout.content as SplitNode).children = [
+ { fraction: 0.206, node: { type: 'pane', id: 'right', panelKind: 'right-tabs' } },
+ { fraction: 0.122, node: { type: 'pane', id: 'ghost-pr', panelKind: 'ghost:pr' } },
+ { fraction: 0.05, node: { type: 'pane', id: 'chat', panelKind: 'chat-main', minWidth: 400 } },
+ { fraction: 0.622, node: { type: 'pane', id: 'ghost-canvas', panelKind: 'ghost:canvas' } },
+ ];
+ currentLayout = layout;
+ for (const kind of ['ghost:pr', 'ghost:canvas']) {
+ registerPanelKind({
+ kind,
+ collapseMemory: 'global',
+ Component: () => <div data-panel-drag-root={kind} />,
+ });
+ }
+ const { container } = renderLayoutRoot();
+ mockPaneWidth('right-tabs', 0); // 折叠
+ mockPaneWidth('ghost:pr', 202);
+ mockPaneWidth('ghost:canvas', 1033);
+ mockPaneWidth('chat-main', 425); // 高于 400,但旧口径一个像素都拖不动
+ dragDividerLeftAt(container, 1000, 2); // 第 3 条缝 = chat | ghost:canvas
+
+ expect(setCalls).toHaveLength(1);
+ const children = (setCalls[0].content as SplitNode).children;
+ const moved = 25 / AVAIL; // chat 实测 425 → 只让 25px 就到产品下限
+ expect(children[2].fraction).toBeCloseTo(0.05, 6); // chat 无账可出,保下限
+ expect(children[0].fraction).toBeCloseTo(0.206 - moved, 6); // 折叠右栏接力出账
+ expect(children[3].fraction).toBeCloseTo(0.622 + moved, 6);
+ expect(children.reduce((s, c) => s + c.fraction, 0)).toBeCloseTo(1, 6);
+ });
});
diff --git a/apps/desktop/src/shared/__tests__/layoutTree.test.ts b/apps/desktop/src/shared/__tests__/layoutTree.test.ts
index 64234b8f8e..25d3e7908b 100644
--- a/apps/desktop/src/shared/__tests__/layoutTree.test.ts
+++ b/apps/desktop/src/shared/__tests__/layoutTree.test.ts
@@ -16,6 +16,7 @@ import {
swapPanesByKind,
swapRootSplitChildrenByKind,
transferSplitFraction,
+ transferSplitFractionRelay,
setPaneCollapsed,
setSplitChildFraction,
validateLayout,
@@ -646,6 +647,64 @@ describe('transferSplitFraction(缝把手拖宽提交)', () => {
});
});
+describe('transferSplitFractionRelay(压缩 chat 的多来源接力提交)', () => {
+ /** 现场树形:chat 份额已顶 0.05 下限,右侧栏折叠着但账上还有 0.206。 */
+ function userTreeWithCollapsedRsb(): Layout {
+ const layout = createDefaultLayout();
+ const split = layout.content as SplitNode;
+ split.children = [
+ { fraction: 0.206, node: { type: 'pane', id: 'right', panelKind: 'right-tabs' } },
+ { fraction: 0.122, node: { type: 'pane', id: 'ghost-pr', panelKind: 'ghost:pr' } },
+ { fraction: 0.05, node: { type: 'pane', id: 'chat', panelKind: 'chat-main', minWidth: 400 } },
+ { fraction: 0.622, node: { type: 'pane', id: 'ghost-canvas', panelKind: 'ghost:canvas' } },
+ ];
+ return layout;
+ }
+
+ it('主来源够用时与两方转移等价:只动 chat 与收方', () => {
+ const layout = createDefaultLayout();
+ const r = transferSplitFractionRelay(layout, 'root', [0], 1, 0.1);
+ expect(r.applied).toBe(true);
+ const fractions = (r.layout.content as SplitNode).children.map((c) => c.fraction);
+ expect(fractions[0]).toBeCloseTo(0.4);
+ expect(fractions[1]).toBeCloseTo(0.6);
+ expect(validateLayout(r.layout)).toEqual({ ok: true });
+ });
+
+ it('主来源(已顶 0.05)不够:差额由后面的折叠兄弟接力,三方各保下限', () => {
+ const layout = userTreeWithCollapsedRsb();
+ // 拖 25px(share 0.0151):chat 无账可出,全部从折叠的 right-tabs 出。
+ const r = transferSplitFractionRelay(layout, 'root', [2, 0], 3, 0.0151);
+ expect(r.applied).toBe(true);
+ const fractions = (r.layout.content as SplitNode).children.map((c) => c.fraction);
+ expect(fractions[2]).toBeCloseTo(0.05, 6); // chat 仍顶下限
+ expect(fractions[0]).toBeCloseTo(0.206 - 0.0151, 6); // 折叠兄弟出账
+ expect(fractions[3]).toBeCloseTo(0.622 + 0.0151, 6); // 收方全额进账
+ expect(fractions.reduce((a, b) => a + b, 0)).toBeCloseTo(1);
+ expect(validateLayout(r.layout)).toEqual({ ok: true });
+ });
+
+ it('接力总额不够 → 整单拒绝并返回原引用(拖缝松手回弹的判据)', () => {
+ const layout = userTreeWithCollapsedRsb();
+ // chat 0(0.05 顶死)+ right-tabs 最多让 0.156,0.2 必然不够。
+ const r = transferSplitFractionRelay(layout, 'root', [2, 0], 3, 0.2);
+ expect(r.applied).toBe(false);
+ expect(r.layout).toBe(layout);
+ });
+
+ it('非法入参(空 sources / 收方在 sources 里 / 重复下标 / amount≤0 / 越界)全部拒绝', () => {
+ const layout = userTreeWithCollapsedRsb();
+ expect(transferSplitFractionRelay(layout, 'root', [], 3, 0.1).applied).toBe(false);
+ expect(transferSplitFractionRelay(layout, 'root', [2, 3], 3, 0.1).applied).toBe(false);
+ expect(transferSplitFractionRelay(layout, 'root', [0, 0], 3, 0.1).applied).toBe(false);
+ expect(transferSplitFractionRelay(layout, 'root', [2], 3, 0).applied).toBe(false);
+ expect(transferSplitFractionRelay(layout, 'root', [2], 3, -0.1).applied).toBe(false);
+ expect(transferSplitFractionRelay(layout, 'root', [9], 3, 0.1).applied).toBe(false);
+ expect(transferSplitFractionRelay(layout, 'root', [2], 9, 0.1).applied).toBe(false);
+ expect(transferSplitFractionRelay(layout, 'nope', [2], 3, 0.1).applied).toBe(false);
+ });
+});
+
describe('findSplitChildByPanelKind', () => {
it('默认树:按 kind 找到 right-tabs 的分割位置与 fraction', () => {
const layout = createDefaultLayout();
diff --git a/apps/desktop/src/shared/layoutTree.ts b/apps/desktop/src/shared/layoutTree.ts
index 53438a591a..c7533c1644 100644
--- a/apps/desktop/src/shared/layoutTree.ts
+++ b/apps/desktop/src/shared/layoutTree.ts
@@ -902,6 +902,77 @@ export function transferSplitFraction(
return finishOp(layout, next, 'transferSplitFraction rejected');
}
+/**
+ * 多来源接力的份额转移:amount 全额从 sources(按下标顺序)转移到 toIndex,每个
+ * 来源各自保住 MIN_SPLIT_CHILD_FRACTION 下限 —— 前一个扣到下限后,剩余差额由
+ * 后面的继续接;接力走完仍有缺口则整单拒绝(返回原树)。
+ *
+ * 为什么需要(2026-08-17 实测):压缩 chat 的拖缝松手要把**像素口径**的增量写回
+ * 账本,但 chat 的画面宽(flex 吸收折叠邻居让出的空间)可以远大于自己的树份额,
+ * 两方转移会被 0.05 账本地板整单拒绝 —— chat 树份额被拖到 0.05 后那条缝彻底
+ * 冻住,压不到 400px 产品下限。差额改由"渲染宽为 0 却占着账"的折叠兄弟接力出账
+ * (画面上那块本就被弹性 chat 吸收),账本与画面重新对齐。调用方(LayoutRoot)
+ * 按"实测宽为 0"挑来源;本函数只管写树合法,不理解折叠语义。
+ */
+export function transferSplitFractionRelay(
+ layout: Layout,
+ splitId: string,
+ sources: number[],
+ toIndex: number,
+ amount: number,
+): LayoutOpResult {
+ if (!(Number.isFinite(amount) && amount > 0)) {
+ return { layout, applied: false, reason: 'amount must be a positive finite number' };
+ }
+ if (!Array.isArray(sources) || sources.length === 0) {
+ return { layout, applied: false, reason: 'sources must be a non-empty array' };
+ }
+ if (!Number.isInteger(toIndex)) {
+ return { layout, applied: false, reason: `toIndex must be an integer: ${toIndex}` };
+ }
+ const seen = new Set<number>();
+ for (const index of sources) {
+ if (!Number.isInteger(index)) {
+ return { layout, applied: false, reason: `source index must be an integer: ${index}` };
+ }
+ if (index === toIndex) {
+ return { layout, applied: false, reason: 'toIndex must not appear in sources' };
+ }
+ if (seen.has(index)) {
+ return { layout, applied: false, reason: `duplicate source index: ${index}` };
+ }
+ seen.add(index);
+ }
+ const next = structuredClone(layout);
+ const split = findSplitById(next.content, splitId);
+ if (!split) return { layout, applied: false, reason: `split not found: ${splitId}` };
+ const bounds = split.children.length;
+ if (toIndex < 0 || toIndex >= bounds || sources.some((index) => index < 0 || index >= bounds)) {
+ return { layout, applied: false, reason: 'child index out of range' };
+ }
+ // 容差与两方转移同款:夹到下限的合法接力不能被浮点残差判死。
+ const floor = MIN_SPLIT_CHILD_FRACTION - FRACTION_TOLERANCE;
+ let remaining = amount;
+ for (const index of sources) {
+ if (remaining <= FRACTION_TOLERANCE) break;
+ const child = split.children[index];
+ const take = Math.min(remaining, child.fraction - floor);
+ if (take <= 0) continue;
+ child.fraction -= take;
+ remaining -= take;
+ }
+ if (remaining > FRACTION_TOLERANCE) {
+ return {
+ layout,
+ applied: false,
+ reason: `sources cannot cover the transfer, short by ${remaining}`,
+ };
+ }
+ split.children[toIndex].fraction += amount - remaining;
+ normalizeSplitFractions(split);
+ return finishOp(layout, next, 'transferSplitFractionRelay rejected');
+}
+
function findSplitById(node: LayoutNode, splitId: string): SplitNode | null {
if (node.type !== 'split') return null;
if (node.id === splitId) return node;