Tools as Contracts, Not Functions: The Architecture Philosophy Behind AI Coding Agent Frameworks

    This article examines the architecture philosophy of a terminal AI coding tool whose source code was recently analyzed in the developer community. It contrasts a model where the framework understands each tool with one where tools declare their own behavior to the framework. It explains how declarative tool contracts shape concurrency, interruption, rendering, permissions, and the agent loop, and it closes with three design principles for teams building their own AI coding tools.

    1. Source Code Discussions Often Miss the Real Point

    Recently, the source code of a terminal-based AI coding tool was publicly analyzed in the developer community and sparked a lot of discussion. Engineers building AI systems know the frustration: the technology moves faster than project schedules. Just as you master one orchestration framework, a new graph execution paradigm appears. Once an agent solution goes live in a project, a new context protocol adds another layer. Right after a multi-agent design stabilizes, the industry starts discussing more complex group collaboration patterns. Refactors go out of date as soon as they finish.

    Most articles on this source code analysis focus on implementation details: how the main loop is written, how context is compressed, how complex the Bash execution tool is, and what easter eggs are hidden inside. These details are interesting, but the more valuable layer is the architecture philosophy behind the system. On questions that determine whether a system can keep evolving, such as tool system design, agent collaboration, and permission governance, this tool takes a path that differs fundamentally from mainstream frameworks.

    You can start building without studying these details. But the day your tool count reaches dozens and multi-environment deployments start to break, you will understand how much these design differences matter.

    2. The Core Divide: Does the Framework Understand Tools, or Do Tools Declare Themselves to the Framework?

    Let’s start with a more basic question: where exactly do mainstream agent frameworks and this system differ in design philosophy?

    In one sentence: in mainstream frameworks, the framework understands the tools. In this system, tools declare themselves to the framework.

    The cost of the first approach is a framework that keeps growing heavier. Each new tool type requires another layer of special-case logic in the framework. As the number of tools increases, maintenance costs rise exponentially. The second approach keeps the framework thin. Tools carry all their own knowledge, and the framework only handles scheduling and execution.

    This goes beyond implementation style. It is a fundamental difference in the direction of control flow, and it is the underlying logic behind every design choice discussed below. Once you understand this, you can see why the tool interface takes the shape it does.

    3. Tools Are Behavioral Contracts, Not Functions

    In most mainstream frameworks, a tool is essentially a function with a description:

    1. @tool
    2. def read_file(path: str) -> str:
    3. """Read a file from disk"""
    4. ...

    From this definition, the framework only knows the name, description, parameter schema, and return value. Everything else about the tool is unknown to the framework: whether it can run concurrently, whether it modifies the disk, how large a result is reasonable, and how errors should be displayed. These decisions are either scattered through framework code as hardcoded special cases keyed by tool name, or they are not handled at all.

    In this system, the tool interface is a large contract set. Its interface definition runs close to a thousand lines and contains thirty to forty methods. Here are several key semantic groups:

    1. type Tool = {
    2. // Concurrency semantics: the framework uses this to decide whether tools can run in parallel
    3. isConcurrencySafe(input: Input): boolean
    4. // Safety semantics: whether the tool is read-only and whether it is destructive
    5. isReadOnly(input: Input): boolean
    6. isDestructive?(input: Input): boolean
    7. // Interruption semantics: on user interrupt, cancel or run to completion
    8. interruptBehavior?(): 'cancel' | 'block'
    9. // Rendering contract: the tool itself defines how its results are displayed
    10. renderToolResultMessage?(result: Result): ReactNode
    11. }

    1. Concurrency and Safety Semantics: Give Scheduling Decisions to the Party That Knows the Tool Best

    Methods like isConcurrencySafe and isReadOnly look simple, but they solve one of the classic scheduling problems in agent frameworks. File reads can clearly run in parallel. File writes must run serially or with locks. Query commands are safe, while delete commands are dangerous. Only the tool implementer knows these properties well. Hardcoding them into the framework’s scheduler means changing the framework every time a tool is added.

    With declarative contracts, the framework’s scheduler becomes a pure “semantic interpreter.” It reads the declarations of all pending tools and automatically derives the largest set that can run in parallel, the groups of tools that must be mutually exclusive, and the list of dangerous operations that need a second user confirmation. From then on, the framework code no longer grows with the number of tools.

    2. Interruption and Lifecycle Semantics: A Must-Have for Long-Running Systems

    When an agent runs builds or tests that take minutes, deciding how to handle a user interrupt is no longer a minor detail. interruptBehavior hands this decision to the tool. A query that can be cancelled terminates immediately. A deployment that cannot be cancelled can choose to block until it finishes and report the real result. This reflects the reality that tools are long-running processes, not single function calls. A tool has a lifecycle, like a subsystem.

    3. The Rendering Contract: Moving Presentation Responsibility Down to the Tool Layer

    Mainstream frameworks usually let the front end decide how tool results are displayed. As a result, output from complex tools is often truncated bluntly or dumped into a generic JSON viewer. The rendering contract lets each tool declare how to present its execution progress and results, while the framework only provides the container. This shares its roots with declarative UI: the logic that binds state to view should live in the module that understands the state best.

    4. Permission Governance: From After-the-Fact Interception to Minimal Capability

    Another design worth examining is the permission model. A common approach adds an “approval hook” outside the framework that shows a dialog before each tool call. The problem with this pattern is that permissions are binary and coarse-grained, and they are disconnected from what the tool actually does.

    This system instead embeds permissions into the tool contract. A tool declares which kinds of resources it needs, which operations require user confirmation, and which can be automatically allowed within a session. The framework uses these declarations to build a capability model. Session-level grants, such as “writing to this directory is allowed for this session,” are recorded and reused. This is essentially the principle of least capability applied to agent systems. By default, the agent holds only the minimum set of capabilities needed for its task. Expanding capabilities requires explicit declaration and user authorization.

    5. Context and the Loop: An Extension of Thin Frameworks and Thick Tools

    The main loop design reflects the same philosophy. The framework maintains a minimal loop: collect declarations, schedule, execute, collect results, and feed them back to the model. Logic that tends to bloat, such as context compression and result trimming, is partly pushed down into the tools’ own lifecycle methods. Tools know how large their own results are, so they are best placed to decide how to summarize themselves.

    The direct benefit of this layering is testability. Each tool can run its own contract tests, such as checking whether its concurrency declarations hold and whether its destructive flags are accurate. The framework then runs integration tests through a unified semantic interpreter. The test matrix drops from “number of tools × framework logic” to linear growth.

    6. Lessons for Teams Building Their Own AI Coding Tools

    This wave of source code analysis gives teams building AI coding tools, including those in China, a rare dissection sample. The real value, however, is not in copying interfaces. It lies in three transferable design principles:

    1. Contracts before implementation: Define the semantic interface between tools and the framework before the number of tools grows. Otherwise, migration costs later become very high.
    2. Knowledge stays with the module: Whoever can make the most accurate judgments about concurrency, danger, or presentation should own that decision. The framework acts as a scheduler, not a judge.
    3. Permissions are architecture: A permission model is not a bolt-on security layer. It is a core design element, inseparable from the tool contract, and it determines whether the system can be adopted in enterprise environments.

    Competition among AI coding tools appears to be a race in model capability. At a deeper level, it is a race in engineering architecture. Frameworks that treat tools as plain functions will break under scale. Systems that treat tools as contracts have a better chance to grow a healthy ecosystem. This may be the most valuable lesson the industry can take from this source code analysis.