paimon

Architecture

Paimon is a small Python package built on pydantic-ai for talking to models and Textual for the terminal UI. This page is for people reading or changing the code.

One stream of events

Agent.run() in agent.py knows nothing about any UI. It is an async generator of typed events, defined in events.py: a chunk of text or reasoning, the start and end of a tool call, a todo list update, a retry, a compaction, the end of the turn.

The terminal UI, --web and -p are three consumers of that one stream. transcript.py draws events as widgets, headless.py writes them as text or JSON lines, and a resumed session is drawn by replaying its log into the same events. A program that imports paimon.agent is a fourth consumer on equal terms.

A turn, step by step

A turn starts with a user message and ends when the model answers without calling a tool. In between it loops over steps, and each step does the same five things:

  1. Take what arrived since the last step: messages the user queued while the turn was running, and notices from agents and commands that finished.
  2. Compact the context if it is close to the model's window.
  3. Send one streamed request. Transient failures are retried with backoff, a context overflow gets one forced compaction and one retry, and a model that refuses images is sent the same request in words alone.
  4. Append the response, and with it a placeholder result for every tool call it makes.
  5. Run the calls and fill in their results. Neighbouring calls that only read are started together, while their events still go out in the order the model wrote them.

Two rules shape this code. Whoever consumes the events may stop at any one of them (the user pressed Esc while a chunk was being drawn), so everything a step adds to the history is written before its events go out. And the history in memory always equals what replaying the log on disk would give, which is why every tool call gets its placeholder up front: a turn cut short leaves a conversation the next request can still be built on.

In the TUI, turns.py sits between a pane and its agent. It serializes turns and compactions, and wakes the agent for another turn when a job it started has something to report.

Tools and the gate

A tool is one entry in REGISTRY in tools/registry.py: a description, a TypedDict of its arguments (the schema the model sees and the validation both come from it), the function that runs it, and how it is gated. Adding a tool means adding one entry.

Before a call runs, gate() in tools/permissions.py looks at the tool's access class, the permission mode and the path or command involved, and answers allow, deny or review. A call held for review goes to the reviewer model in review.py, and to the user when there is no verdict. The decision is made in one place for every caller, so a headless run or a subagent cannot get past the mode it was started in.

A few tools change state the agent itself holds, such as the todo list or its running jobs, and are handled inside the loop. run_code is one of them: the model writes a Python script that runs in a sandbox with no filesystem or network, and whose only way out is to call the agent's other tools, each through the same gate.

Agents and background commands

An agent holds the subagents and background commands it starts, in jobs.py. A subagent is another Agent running as a task in the same process, with a session of its own marked as its parent's child and a narrower set of tools: it cannot start agents or background commands of its own, or ask the user questions. Its permission prompts are answered by whoever answers for its parent.

When a job ends, its result is delivered to the parent as a message, and the parent runs another turn to react to it. A UI that wants to show a job sets the open_job hook, which is how the TUI gives each one a tab.

The session log

A session is one append-only JSONL file, written by session.py. Its records are the messages of the conversation, the result of each tool call, the system prompt each turn ran with, compaction checkpoints, and bookkeeping that is never sent to the model, such as how each turn ended. A lock keeps two agents, in this process or another, from writing the same session.

Compaction, in compaction.py, replaces the older part of the context with a summary and keeps the recent messages. Nothing is removed from the file, so paimon log and the agent's own history tools still see everything.

The header carries a format version. A log written by a newer Paimon is refused rather than misread.

Models

llm.py turns a provider:name string and the stored credentials into a pydantic-ai model. Most providers are pydantic-ai's own. Two are served by modules here, chatgpt.py and antigravity.py, which sign in through the browser with the helpers in oauth.py and spend a subscription plan in place of an API key.

A conversation can change models half way. Reasoning produced by one model is only valid sent back to that same model, so before each request the history is adapted for the model about to read it. The stored history is left as it was.

Module map

An arrow reads “uses”. Dotted arrows are jobs an agent starts while it runs.

flowchart TD
    subgraph entry["Entry points"]
        CLI["cli.py"]
        Commands["commands.py<br/>status / login / model / sessions / log / install-skill"]
        Headless["headless.py<br/>-p, one-shot"]
        App["app.py<br/>Textual TUI / --web"]
    end

    subgraph tui["TUI widgets"]
        Pane["pane.py<br/>SessionPane"]
        AgentPane["agentpane.py<br/>subagent pane"]
        CommandPane["commandpane.py<br/>background command pane"]
        Transcript["transcript.py<br/>conversation log, event renderer"]
        Tabs["tabs.py<br/>pane strip"]
        Login["login.py<br/>provider / key"]
        UIWidgets["ui.py<br/>prompt input, confirmations"]
        Diff["diff.py<br/>side-by-side diff rendering"]
    end

    subgraph core["Agent loop"]
        AgentLoop["agent.py<br/>Agent.run()"]
        LLM["llm.py<br/>build_model()"]
        PromptMod["prompt.py<br/>system prompt"]
        ToolsMod["tools/<br/>tool REGISTRY, permission gate"]
        SessionMod["session.py<br/>JSONL persistence"]
        Compaction["compaction.py"]
        Retry["retry.py"]
        Mentions["mentions.py<br/>@path expansion"]
        JobMgr["jobs.py<br/>subagents, background commands"]
        Turns["turns.py<br/>turn driver"]
    end

    subgraph support["Config & skills"]
        Config["config.py<br/>settings, credentials"]
        Skills["skills.py<br/>Agent Skills discovery"]
    end

    CLI --> Commands
    CLI --> Headless
    CLI --> App
    CLI --> Config

    Headless --> AgentLoop
    Headless --> Mentions

    App --> Pane
    App --> AgentPane
    App --> CommandPane
    App --> Tabs
    App --> Login
    App --> Config

    Pane --> Turns
    Pane --> AgentLoop
    Pane --> Diff
    Pane --> Transcript
    Pane --> UIWidgets
    Pane --> LLM
    AgentPane --> Pane
    AgentPane --> Transcript
    CommandPane --> Pane
    Transcript --> UIWidgets
    UIWidgets --> Diff

    AgentLoop --> LLM
    AgentLoop --> PromptMod
    AgentLoop --> ToolsMod
    AgentLoop --> SessionMod
    AgentLoop --> Compaction
    AgentLoop --> Retry
    AgentLoop --> Mentions
    AgentLoop --> JobMgr
    JobMgr -. "spawn_agent" .-> AgentLoop
    JobMgr -. "spawn_agent" .-> AgentPane
    JobMgr -. "run_background" .-> CommandPane

    PromptMod --> Skills

    Turns --> AgentLoop

    Skills --> Config
    Config --> LLM
        

The diagram leaves out the smaller modules: images and their drawing in the terminal (images.py, termimage.py, clipboard.py), web search, code mode, the reviewer, the themes, telemetry and the Herdr reporter.