利用函数式事件溯源消除业务逻辑 Bug ✍ Nadeem Bitar🕐 2026-05-04📦 4.7 KB 🟢 已读 𝕏 文章列表 文章介绍了一种基于函数式事件溯源(Functional Event Sourcing)的库,旨在让业务逻辑 Bug 在结构上不可能发生。作者通过定义可组合的状态机,在类型层统一描述状态转换,自动生成决策逻辑和演化逻辑,从而避免两者不一致。此外,该库还能自动生成 Mermaid 流程图,并利用 SMT 求解器验证业务不变性,大幅提升代码的健壮性和可维护性。 函数式编程事件溯源状态机领域驱动设计SMT形式化验证架构设计代码质量Haskell # Making Business-Logic Bugs Structurally Impossible **作者**: Nadeem Bitar **日期**: 2026-05-03T15:41:22.000Z **来源**: [https://x.com/shinzui/status/2050963939060838501](https://x.com/shinzui/status/2050963939060838501) ---  I've been building a library that aims to make business-logic bugs almost impossible to ship, even when the code is generated by a coding agent. The foundation is functional event sourcing which we've been using in production for a few years. In functional event sourcing your business logic lives in a small pure module: trivial to test, trivial to debug, trivial to reason about. ``` data Decider c e s = Decider { decide :: c -> s -> [e] , evolve :: s -> e -> s , initialState :: s , isTerminal :: s -> Bool } ``` …and you write it by hand: ``` userReg :: Decider UserCmd UserEvent UserState userReg = Decider { decide = \cmd s -> case (s, cmd) of (PotentialCustomer, Start d) -> [RegistrationStarted d.email d.confirmCode d.at] (RequiresConfirmation r, Confirm d) | d.code == r.confirmCode -> [AccountConfirmed r.email d.at] | otherwise -> [] (RequiresConfirmation r, Resend d) -> [ConfirmationResent r.email d.code d.at] (RequiresConfirmation r, Gdpr d) -> [AccountDeleted r.email d.at] _ -> [] , evolve = \s ev -> case (s, ev) of (PotentialCustomer, RegistrationStarted email code at) -> Registering { email, confirmCode = code, registeredAt = at } (Registering r, ConfirmationEmailSent _) -> RequiresConfirmation r (RequiresConfirmation r, AccountConfirmed _ at) -> Confirmed r { confirmedAt = at } (RequiresConfirmation r, ConfirmationResent _ code at) -> RequiresConfirmation r { confirmCode = code, registeredAt = at } (_, AccountDeleted _ at) -> Deleted at (s, _) -> s , initialState = PotentialCustomer , isTerminal = \case Deleted{} -> True; _ -> False } ``` The catch is that hand-written deciders carry their share of bugs. Missed branches. decide and evolve quietly drifting out of sync, so a freshly produced event no longer round-trips through replay and invariants are violated. So over the last few weekends I've been working on a library that aims to make those bugs structurally impossible to write. Workflows are composable state machines defined at the type level. You describe each edge once, and both decide and evolve are derived from that single definition, so they can never disagree. A small DSL hides the type-level machinery, so day-to-day code looks like the decider: ``` userReg = buildTransducer PotentialCustomer emptyRegs do from PotentialCustomer do onCmd inCtorStart \d -> do slot @"email" .= d.email slot @"confirmCode" .= d.confirmCode slot @"registeredAt" .= d.at emit wireRegistrationStarted goto Registering from RequiresConfirmation do onCmd inCtorConfirm \d -> do requireEq d.confirmCode #confirmCode slot @"confirmedAt" .= d.at emit wireAccountConfirmed goto Confirmed onCmd inCtorResend \d -> do slot @"confirmCode" .= d.code slot @"registeredAt" .= d.at emit wireConfirmationResent goto RequiresConfirmation onCmd inCtorGdpr \d -> do slot @"deletedAt" .= d.at goto Deleted ``` Notice what's missing: there is no separate evolve. Replay is generated from the same edge definition that produced the event, so the two directions can't fall out of sync. Two more things fall out for free. 1) Mermaid diagrams generated straight from the code, so you can visually verify what you actually wrote, in code review or a design doc:  2) An SMT-backed checker you can run on CI. Encode a business invariant ("no path reaches Confirmed without the code-equality guard firing," "every started registration eventually reaches a terminal state," "deletedAt is never set on a non-terminal vertex") and the solver either proves it or hands you the counter-example trace. The kind of bug a code review will not easily catch. Really happy with what I have so far. Still a fair bit of work before I'd use it at work, but the core is there. ## 相关链接 - [Nadeem Bitar](https://x.com/shinzui) - [@shinzui](https://x.com/shinzui) - [12K](https://x.com/shinzui/status/2050963939060838501/analytics) - [11:41 PM · May 3, 2026](https://x.com/shinzui/status/2050963939060838501) - [12.5K Views](https://x.com/shinzui/status/2050963939060838501/analytics) - [View quotes](https://x.com/shinzui/status/2050963939060838501/quotes) --- *导出时间: 2026/5/4 11:40:58* --- ## 中文翻译 # 从结构上杜绝业务逻辑 Bug **作者**: Nadeem Bitar **日期**: 2026-05-03T15:41:22.000Z **来源**: [https://x.com/shinzui/status/2050963939060838501](https://x.com/shinzui/status/2050963939060838501) ---  我正在构建一个库,旨在让业务逻辑 Bug 几乎不可能被带上线,即使代码是由编程代理生成的。其基础是函数式事件溯源,我们已经在生产环境中使用它几年了。 在函数式事件溯源中,你的业务逻辑位于一个微小的纯模块中:极易测试,极易调试,极易推理。 ``` data Decider c e s = Decider { decide :: c -> s -> [e] , evolve :: s -> e -> s , initialState :: s , isTerminal :: s -> Bool } ``` ……并且你需要手写它: ``` userReg :: Decider UserCmd UserEvent UserState userReg = Decider { decide = \cmd s -> case (s, cmd) of (PotentialCustomer, Start d) -> [RegistrationStarted d.email d.confirmCode d.at] (RequiresConfirmation r, Confirm d) | d.code == r.confirmCode -> [AccountConfirmed r.email d.at] | otherwise -> [] (RequiresConfirmation r, Resend d) -> [ConfirmationResent r.email d.code d.at] (RequiresConfirmation r, Gdpr d) -> [AccountDeleted r.email d.at] _ -> [] , evolve = \s ev -> case (s, ev) of (PotentialCustomer, RegistrationStarted email code at) -> Registering { email, confirmCode = code, registeredAt = at } (Registering r, ConfirmationEmailSent _) -> RequiresConfirmation r (RequiresConfirmation r, AccountConfirmed _ at) -> Confirmed r { confirmedAt = at } (RequiresConfirmation r, ConfirmationResent _ code at) -> RequiresConfirmation r { confirmCode = code, registeredAt = at } (_, AccountDeleted _ at) -> Deleted at (s, _) -> s , initialState = PotentialCustomer , isTerminal = \case Deleted{} -> True; _ -> False } ``` 问题在于,手写的决策器难免带有一些 Bug。遗漏分支。`decide` 和 `evolve` 悄悄地步调不一致,导致新生成的事件在重放时无法正确地进行往返,不变量因此被破坏。 所以在过去几个周末,我一直在开发一个库,旨在从结构上让这些 Bug 根本无法被写出来。工作流是在类型层级定义的可组合状态机。你只需描述一次每条边,`decide` 和 `evolve` 都是从这唯一的定义中派生出来的,所以它们永远不可能不一致。一个小型的 DSL 隐藏了类型层级的机制,使得日常代码看起来就像决策器一样: ``` userReg = buildTransducer PotentialCustomer emptyRegs do from PotentialCustomer do onCmd inCtorStart \d -> do slot @"email" .= d.email slot @"confirmCode" .= d.confirmCode slot @"registeredAt" .= d.at emit wireRegistrationStarted goto Registering from RequiresConfirmation do onCmd inCtorConfirm \d -> do requireEq d.confirmCode #confirmCode slot @"confirmedAt" .= d.at emit wireAccountConfirmed goto Confirmed onCmd inCtorResend \d -> do slot @"confirmCode" .= d.code slot @"registeredAt" .= d.at emit wireConfirmationResent goto RequiresConfirmation onCmd inCtorGdpr \d -> do slot @"deletedAt" .= d.at goto Deleted ``` 请注意少了什么:这里没有单独的 `evolve`。重放逻辑是从产生事件的同一条边定义中生成的,所以这两个方向不可能步调不一致。 此外,还免费获得了另外两个功能。 1) 直接从代码生成 Mermaid 图,这样你可以在代码审查或设计文档中直观地验证你实际写的内容:  2) 一个支持 SMT 的检查器,你可以在 CI 中运行它。编码一个业务不变量(“没有路径能在不触发 code-equality 守卫的情况下到达 Confirmed 状态”,“每个已开始的注册最终都会到达终止状态”,“deletedAt 从不会在非终止节点上被设置”),求解器要么证明它,要么给你反例轨迹。这正是代码审查很难发现的那类 Bug。 我对目前的进展非常满意。虽然距离在工作中使用还有一些工作要做,但核心功能已经实现了。 ## 相关链接 - [Nadeem Bitar](https://x.com/shinzui) - [@shinzui](https://x.com/shinzui) - [12K](https://x.com/shinzui/status/2050963939060838501/analytics) - [11:41 PM · May 3, 2026](https://x.com/shinzui/status/2050963939060838501) - [12.5K Views](https://x.com/shinzui/status/2050963939060838501/analytics) - [View quotes](https://x.com/shinzui/status/2050963939060838501/quotes) --- *导出时间: 2026/5/4 11:40:58*
S State Machines: From Loops to Graphs (Explained) 文章阐述了状态机理论及其在软件开发中的应用。作者指出Agent框架从链条到循环再到图形的演变,本质上是有限状态机(FSM)的体现。通过将分散的逻辑迁移到明确的转换函数中,状态机能消除不可能状态,避免竞态条件,并提供类型级安全保障。文章展示了基于查找表和Switch语句的实现方式,强调了其在代码审查、测试及系统可靠性方面的优势。 技术 › 后端 ✍ vixhaℓ🕐 2026-07-21 状态机FSM编程范式Agent架构设计
A Agent 工程的四个深坑:Demo 到生产没有捷径 作者分享了将 AI Agent 从 Demo 推向生产环境过程中遇到的四个“深坑”:Function Calling 的不可预测性需加代码校验兜底;多步任务必须引入状态机和 checkpoint 防止重复执行或死无对证;记忆管理需分层以解决 token 爆炸和“丢失中间”问题;权限控制是最大风险,需防范 prompt injection 并建立分级审计机制。 技术 › Agent ✍ 老金🕐 2026-05-16 AgentLLM架构设计生产环境Function Calling状态机陷阱防御性编程
从 从任务板到文件协议:两个 Agent 协作系统的边界与互补 本文探讨了多 Agent 协作系统中的架构设计问题。作者对比了基于 IPC 的“任务板”模式与自研的“文件协议+状态机”模式,阐述了为何选择文件系统作为任务真理源以实现跨 Session 的可恢复性与可审计性。文章提出了一种混合架构,利用 IPC 处理高频进程心跳,同时保留文件层作为核心状态与证据链的存储,最终实现了稳定、解耦且支持异步长任务的自动工程流。 技术 › Agent ✍ 周.乙🕐 2026-05-02 Agent多Agent协作架构设计文件协议状态机IPCDevOpsOpenAI Symphony工程实践自动化
3 35个鲜为人知的 Claude Code 技巧与工作流 文章分享了作者数月来每日使用 Claude Code 总结出的 35 个高效技巧,旨在将 Claude Code 从基础辅助工具转变为极致的生产力引擎。内容涵盖核心命令(如计划模式、压缩上下文、持久化记忆)、生产力工作流(如增量构建、测试先行、截图调试)以及架构层面的审计与模式执行技巧。作者强调这些实战技巧能显著减少 Bug 并提升代码质量。 技术 › Claude Code ✍ Khairallah AL-Awady🕐 2026-04-21 Claude CodeAI编程工作流开发效率技巧命令行架构设计Debug代码质量Prompt技巧
M MCP新规范:更灵活的协议 MCP新规范将协议从双向有状态改为请求/响应模型,服务端可部署在Serverless和边缘基础设施上。类比银行业务,以前需找固定柜员,现在任意柜员均可办理,提升了灵活性和效率。 技术 › 工具与效率 ✍ Gorden Sun🕐 2026-07-29 MCP协议Serverless边缘计算Cloudflare WorkersNetlifyHTTP服务负载均衡架构设计
做 做完一次总体设计后,我重新理解了企业级知识库 文章基于企业级智能知识库项目的实践,探讨了企业知识库的真正门槛。指出其不仅是文档存储,更是一套涵盖构建、治理、使用和优化的闭环运行机制。重点分析了知识库与知识空间的区别、知识生命周期管理、可信溯源及权限控制,强调知识库应作为支撑未来 Agent 的企业级 AI 知识引擎。 技术 › LLM ✍ 土猛的员外🕐 2026-07-29 企业知识库RAGAgent知识工程架构设计
这 这项目给Claude Code塞了28个心智模型和批判性思维技能 该项目为Claude Code集成了28个心智模型和批判性思维技能,包括第一性原理、贝叶斯推理、系统思考等。用户可按需调用,帮助结构化决策和策略分析。测试显示技能未稳定提升准确率,但作者认为作为思考脚手架仍优于裸奔。 技术 › Claude Code ✍ Amto (@XAMTO_AI)🕐 2026-07-29 心智模型批判性思维第一性原理贝叶斯推理系统思考OODA循环Agent决策辅助架构设计风险评估
R Run Your Harness Outside of the Sandbox (Why and How) 本文探讨了2026年以来关于Agent运行位置的争论,指出行业趋势是将Agent运行在沙箱之外。作者详细解释了沙箱内运行的三个主要问题:爆炸半径、信任边界和沙箱的间歇性运行,并提出了将沙箱作为工具暴露的正确架构,最后提供了基于Vercel AI SDK的实现示例和生产环境中的挑战。 技术 › Agent ✍ Nathan Flurry🕐 2026-07-28 Agent沙箱架构设计DevOps后端LLM安全性生产环境Vercel AI SDK状态管理
大 大脑对长周期 Agent 的启示 文章通过对比人脑的双速学习机制(海马体与皮层),指出当前 AI Agent 仅依赖大模型权重的局限性。真正的长周期智能需要将预测模型与经验状态结合。现有的外部记忆存储实则是模拟海马体的快速捕获功能,未来的关键在于实现高效的记忆筛选机制。 技术 › Agent ✍ Yohei🕐 2026-07-28 Agent架构设计脑科学长周期记忆CLSM理论
A Agent工程架构解析:Harness、Loop与Graph的区别 本文深入解析Agent工程中常被混淆的三个架构层级:Harness工程构建模型运行环境与基础能力;Loop工程设计工作反馈循环,通过验证与迭代提升质量;Graph工程则显式定义工作流拓扑,控制节点分支与状态转换。文章强调理清环境、反馈与流的关系对构建生产级Agent至关重要。 技术 › Harness Engineering ✍ beamnxw🕐 2026-07-26 Agent架构设计工程化工作流LangChainOpenAIAgent HarnessLoop Engineering
何 何为“掌控你的智能” 文章探讨了企业如何在通用 AI 基础上构建差异化优势。作者指出,未来五年公司需掌控智能的运作方式、管理与复利,而非仅依赖现成模型。掌控智能包括控制 Agent 系统(模型、调度逻辑、上下文)、掌控质量与风险,以及通过反馈循环积累智能,从而实现业务的深度集成与持续优化。 技术 › Agent ✍ Harrison Chase🕐 2026-07-26 AI企业应用智能体架构设计LangChain商业策略模型掌控上下文管理反馈循环
W Why Software Factories Fail 文章探讨了AI驱动的软件工厂模式面临的挑战。作者指出,尽管AI编码工具看似能提高效率,但实际应用中往往导致代码质量下降、Bug增多和事故频发。问题不在于技术技能不足,而是模型训练的根本性限制。 技术 › Harness Engineering ✍ dex🕐 2026-07-25 AI软件工厂代码质量模型训练技术挑战自动化工程实践