Skip to content

feat(workflow): item 25B 错峰 fan-out (prefix-stagger, CC 2.1.229, #121) - #122

Merged
dahai80 merged 1 commit into
mainfrom
feat/workflow-stagger-item25b
Aug 22, 2026
Merged

dahai80 merged 1 commit into
mainfrom
feat/workflow-stagger-item25b

Conversation

@dahai80

@dahai80 dahai80 commented Aug 22, 2026

Copy link
Copy Markdown
Owner

item 25B — workflow 错峰 fan-out (CC 2.1.229 PREFIX_STAGGER_MS)

Closes #121.

序列末项: 25A → 16 → 19 → 5 → 6 → 24 → 23 → 21 → 25B (全 ✅)。

背景

25A (PR #105, main 5d580d6) 落地最小 workflow runtime, runWithConcurrency(thunks, cap) 并发池 min(16, cpu-2), 无错峰。本地 MLX 无 KV 共享 → 兄弟 agent 各自重算 prompt prefill。错峰让后启 agent 在先启 agent 预热 prefix 缓存窗口内复用。

改动 (2 文件, +214/-6)

runtime.ts:

  • workflowStaggerMs() — env 解析 FUSION_WORKFLOW_PREFIX_STAGGER_MS (FUSION 优先, fusion-code 约定) 回落 CLAUDE_CODE_WORKFLOW_PREFIX_STAGGER_MS (CC 原名)。非法/负数/NaN/Infinity → fail-off = 0 + logForDebugging。default off (env 未设/空 = 0) = 25A byte-identical。
  • runWithConcurrency<T>(thunks, cap, staggerMs = 0, delay: DelayFn = defaultDelay) — worker k 启动前 await delay(k * staggerMs) (仅初始 burst, 前 cap 个 worker; worker 0 不延迟; 后续补位无延迟)。staggerMs=0 走无延迟分支 = 25A 行为。DelayFn 可注入 (测试确定性, 复用 item 16 无 fake-timer 教训)。
  • createParallelPrimitive / createPipelinePrimitive 各注入 workflowStaggerMs()。
  • 导出 runWithConcurrency / workflowStaggerMs / DelayFn 供测试。

workflowRuntime.test.ts — 14 新单测:

  • env 解析 10 case: default off / 空串 / FUSION 优先 CLAUDE_CODE_ / 回退 / 非法 fail-off / 负数 fail-off / NaN fail-off / Infinity fail-off / 有效值 / 显式 0。
  • runWithConcurrency 7 case: staggerMs=0 不调 delay (25A byte-identical) / 错峰 delay 调用 [50,100,150] 排序解耦微任务调度 / 仅前 cap 个 worker 错峰 (cap<thunks, delayCalls 仅 [50]) / cap<items 全完成 / throw → null 不连累 / 空 thunks / 单 worker 不延迟 (workerIndex 0)。

设计取舍

  • default off byte-identical: env 未设 = staggerMs=0 = worker 无延迟分支 = 25A 行为。零默认变更。
  • 仅初始 burst 错峰: 前 cap 个 worker 错开拉起; 后续 worker 抢完补位一进一出无 burst, 不延迟。避免每任务都 sleep 的吞吐损耗。
  • injectable delayFn: Bun test 无 fake-timer API (item 16 教训), 注入同步 collecting delayFn 验错峰顺序。delayCalls 集合排序后断言, 解耦 Promise.all 微任务调度不确定性。
  • 双门禁不变: stagger 在已门禁 runtime 路径内 (feature("WORKFLOW_SCRIPTS") 编译期 + FUSION_WORKFLOW_RUNTIME_ENABLED=1 运行期), 无独立 gate。standard build DCE 掉整个 runtime, stagger 随之消失。

验证

  • bun run typecheck — 0 errors
  • bun test src/__tests__/workflowRuntime.test.ts — 40 pass (14 新 + 26 原 25A)
  • bun test src/__tests__/ — 380 pass, 0 fail, 无回归
  • bun run build + build:dev + build:dev:full — 全 pass
  • biome check changed 文件 — clean (defaultDelay 多行格式 biome 要求, 改在新增块内)

Co-Authored-By: Claude Fable 5 [email protected]

并发池 worker 启动前错峰延迟, 让后启 agent 复用先启 agent 已预热的
prompt prefix 缓存 (本地 MLX 无 KV 共享时兄弟 agent 各自重算 prefill,
错峰给复用窗口)。

- workflowStaggerMs(): env 解析 FUSION_WORKFLOW_PREFIX_STAGGER_MS (优先)
  回落 CLAUDE_CODE_WORKFLOW_PREFIX_STAGGER_MS; 非法/负数/NaN/Infinity
  fail-off = 0。default off (env 未设/空 = 0) = 25A byte-identical。
- runWithConcurrency 加 staggerMs + 注入 delay 参数: worker k 启动前
  wait k*staggerMs (仅初始 burst, 前 cap 个 worker; worker 0 不延迟;
  后续补位无延迟)。staggerMs=0 走无延迟分支 = 25A 行为。
- createParallel/PipelinePrimitive 各注入 workflowStaggerMs()。
- 导出 runWithConcurrency/workflowStaggerMs/DelayFn 供测试。
- 14 新单测: env 解析 10 case (default off/FUSION 优先/回落/非法 fail-off/
  有效值) + runWithConcurrency 7 case (staggerMs=0 不调 delay byte-identical/
  错峰 [50,100,150] 排序解耦调度/仅前 cap 错峰/cap<items 全完成/throw null/
  空/单 worker 不延迟)。

双门禁不变 (feature("WORKFLOW_SCRIPTS") 编译期 + FUSION_WORKFLOW_RUNTIME_ENABLED=1
运行期), stagger 在已门禁 runtime 路径内, 无独立 gate。

验证: typecheck 0 / 40 stagger 测 pass / 380 全套无回归 / build+build:dev
+build:dev:full+build pass / biome clean。

Co-Authored-By: Claude Fable 5 <[email protected]>
@dahai80
dahai80 merged commit 304e8bc into main Aug 22, 2026
1 check passed
@dahai80
dahai80 deleted the feat/workflow-stagger-item25b branch August 22, 2026 09:29
dahai80 added a commit that referenced this pull request Aug 22, 2026
Patch release bundling 17 commits since v0.4.23:
- item 15(a) MLX OOM auto-recovery gc+retry (#129)
- #7 one-click diagnostic fixAction framework (#131)
- 维度5 background-default spawn FUSION_SUBAGENT_DEFAULT_BACKGROUND (#127)
- #4 proactive MLX OOM detection via /v1/health (#125)
- item 17 fullscreen pre-compact history + :598 synthetic decouple (#123)
- item 25B workflow stagger fan-out (#122)
- item 21 auto-mode local rule classifier test coverage (#120)
- item 23 archive plugin source + SHA-256 lock (#118)
- item 24 marketplace command source (#116)
- item 6 transcript disk trimming + crash-safe checkpoint (#114)
- item 5 normalizeMessages secondary normalize cache (#112)
- item 19 screen-reader plaintext render (#110)
- item 16 compact retry countdown + stall hint (#108)
- item 25A workflow execution runtime docs (#106)
- item 25A minimal workflow execution runtime (#105)
- item 12 subagent auto-background threshold (#103)
- item 20 vimInsertModeRemaps dual-key INSERT sequence (#101)

Co-Authored-By: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

item 25B: workflow 错峰 fan-out (prefix-stagger, CC 2.1.229)

1 participant