今日已更新 80 条资讯 | 累计 20052 条内容
关于我们

Agent Series (20): Harness in Production — From Single File to Reusable Package

WonderLab 2026年06月14日 11:50 6 次阅读 来源:Dev.to

From Demo Code to a Reusable Package Article 19 used a 900-line harness_full_demo.py to demonstrate eight defense layers. That file is good for explaining concepts, but not for reuse — all layers are coupled together, nothing can be tested in isolation, and nothing can be imported by another project. A production-grade Agent project needs something you can actually import : harness/ ├── __init__.py Public API exports ├── registry.py Layer 2: ActionRegistry + PermissionLevel ├── budget.py Layer 3: PermissionBudget (with refund()) ├── sandbox.py Layer 4: sanitise_input + sandboxed_eval ├── audit.py Layer 6: ImmutableAuditLog (hash-chained) ├── rollback.py Layer 7: RollbackCoordinator └── harness.py Unified entry point: AgentHarness This article starts with package design, covers three key API decisions, and finishes with two integration styles: standalone Python and LangGraph graph embedding. Module Design registry.py — Layer 2 class PermissionLevel ( Enum ): READ = 1 WRITE = 2 ADMIN = 3 IRREVERSIBLE = 4 @dataclass class RegisteredAction : name : str level : PermissionLevel budget_cost : int description : " str " handler : Any # Callable or BaseTool class ActionRegistry : def register ( self , action : RegisteredAction ) -> None : ... def get ( self , name : str ) -> RegisteredAction : ... # not found → PermissionError def is_allowed ( self , name : str ) -> bool : ... def names ( self ) -> list [ str ]: ... get() rather than __getitem__ : raises a consistent PermissionError , without leaking the internal KeyError detail. budget.py — Layer 3 class PermissionBudget : def spend ( self , action_name : str , cost : int ) -> None : if self . remaining < cost : raise BudgetExhaustedError (...) self . remaining -= cost def refund ( self , action_name : str , cost : int ) -> None : self . remaining = min ( self . total , self . remaining + cost ) The new refund() method fixes a design flaw from Article 19: budget was deducted before approval, and never returned on rejection.

本文内容来源于互联网,版权归原作者所有
查看原文