Building a custom harness, part 2: Getting started
MP 181: Setting up the harness UI.
Note: This is the second post in a series about building a custom coding harness. The first post in the series is here.
Mainstream coding harnesses are quite effective in many ways, but there are also many reasons to be wary of them. Building a custom harness is easier than many people imagine, and you can learn a lot from doing so. You might find that your own harness is good enough for your current workflows. But even if you don't end up using a custom harness for real work, you'll go back to more mainstream harnesses with a much better idea of what's happening under the hood, and a better idea of your options as these tools continue to evolve.
In this post, we'll use Textual to build out the UI for a custom harness.
Planning the UI
Before writing any code, let's be clear about what we're trying to build. We're going to build a terminal app that lets the user prompt an AI model, and which lets the model interact directly with the codebase within a project directory.
Here's my sketch of what I'd like this UI to look like:

This sketch took less than a minute, but it provides a clear sense of what I want to build. The UI will have a title bar that also shows the project directory; that's critical information I want visible at all times.
The overall UI is broken into two columns. On the left there are three boxes. The first is the input box, where the user can enter their prompts. The second is a list of tool calls the model makes. The box at the bottom left is a list of alerts, which is used to notify us of any actions the model tries to take that we might not like. The large box on the right contains the running conversation between the user and the model.
Building the app
Now let's start building the app. I'm going to call this project Junco, after one of my favorite birds:
$ uv init --package junco $ cd junco junco$ uv add textual junco$ tree ├── pyproject.toml ├── README.md ├── src │ └── junco │ └── __init__.py └── uv.lock
This creates a simple project structure; the code we write will go in the src/junco/ directory. Here's src/junco/junco.py:
from textual.app import App, ComposeResult from textual.widgets import Header class JuncoApp(App): """A Textual app that serves as a simple harness for AI models.""" def compose(self) -> ComposeResult: """Create child widgets for the app.""" yield Header() if __name__ == "__main__": app = JuncoApp() app.run()
This is a minimal example of a running Textual app. The class JuncoApp extends Textual's base App. The compose() method, which is called automatically by Textual, builds a header for the app.
You can run Junco now by running the junco.py file:
$ uv run src/junco/junco.py
You'll see an empty Textual app in your terminal:

The app opens in dark mode by default; you can exit the app by pressing Ctrl-Q.
A better entry point
Let's clean things up so we can just run $ uv run junco. Here's where the entry point is defined in pyproject.toml:
[project.scripts] junco = "junco:main"
This looks for a function named main(), in the junco module. Let's write that main() function, and make sure it's accessible to the overall package.
Here are the necessary changes to junco.py:
... def main(): app = JuncoApp() app.run() if __name__ == "__main__": main()
The lines that create an instance of the app have been moved to a main() function, and that function is called when the file is run directly.
By default, running $ uv run junco prints a friendly greeting from the new project:
junco$ uv run junco Hello from junco!
This comes from the code in __init__.py. Don't delete that file, but delete its placeholder contents.
Let's update the entry point so that it uses the main() function we just wrote in junco.py:
... [project.scripts] junco = "junco.junco:main"
This now says that when the command junco is run, look at the directory src/junco/. Then look for a file named junco.py, and run the main() function in that file.
Now you can launch the app by running $ uv run junco.
Light and dark mode
I'm a light-mode person, so let's add the ability to toggle between light and dark mode:
from textual.app import App, ComposeResult from textual.widgets import Header, Footer class JuncoApp(App): """A Textual app that serves as a simple harness for AI models.""" BINDINGS = [("d", "toggle_dark", "Toggle dark mode")] def compose(self) -> ComposeResult: """Create child widgets for the app.""" yield Header() yield Footer() def action_toggle_dark(self) -> None: """Toggle light and dark mode.""" if self.theme == "textual-light": self.theme = "textual-dark" else: self.theme = "textual-light" def main(): ...
The BINDINGS attribute binds the d key to the action_toggle_dark() method. Here that method sets the value of self.theme to the opposite of its current setting. When you run junco now, you should be able to press d to toggle back and forth between light and dark mode.
I like light mode, so let's set that as the default. Even if you're sticking with dark mode, it's helpful to see how to override Textual's defaults:
class JuncoApp(App): """A Textual app that serves as a simple harness for AI models.""" BINDINGS = [("d", "toggle_dark", "Toggle dark mode")] def on_mount(self) -> None: """Start junco in light mode.""" self.theme = "textual-light" ...
The on_mount() method runs after Textual has initialized itself. That's an appropriate time to make UI-related customizations. This is also where we'll initialize the AI model later in the project. For now, if you include this method, your harness will start in light mode.
Defining the project directory
We're writing a harness that will only give the model access to files in a specified project directory. Before we do more UI work, let's pass that directory in and make it prominently visible:
import argparse from pathlib import Path ... class JuncoApp(App): """A Textual app that serves as a simple harness for AI models.""" BINDINGS = [("d", "toggle_dark", "Toggle dark mode")] def __init__(self, project_dir): """Initialize startup attributes.""" super().__init__() self.project_dir = project_dir self.title = f"Junco - {self.project_dir}" ... def main(): # Define a minimal CLI. parser = argparse.ArgumentParser() parser.add_argument( "--project-dir", required=True, help="Project directory Junco will have access to.") args = parser.parse_args() # Make sure we're given a directory. project_dir = Path(args.project_dir).expanduser().resolve() if not project_dir.is_dir(): msg = f"{project_dir} is not a valid directory." parser.error(msg) # Pass the project directory to the app. app = JuncoApp(project_dir) app.run() if __name__ == "__main__": main()
There are a number of ways to structure this; for the purposes of a demo I'm trying as much as possible to keep things simple, and in one file. Here the main() function uses argparse to define a single --project-dir CLI argument. Before handing the argument off to the app, we resolve it to an absolute path, and make sure it's a directory by calling is_dir(). The argument is then passed to Junco's __init__() method, where we use it to set a custom title for the app.
With this change, we have an app that requires a project directory and has meaningful help output:
junco$ uv run junco --help usage: junco [-h] --project-dir PROJECT_DIR options: -h, --help show this help message and exit --project-dir PROJECT_DIR Project directory Junco will have access to. junco$ uv run junco usage: junco [-h] --project-dir PROJECT_DIR junco: error: the following arguments are required: --project-dir
If you pass a valid directory, you'll see that directory in the header. Here's what it looks like when given its own directory for the --project-dir argument:

This is nice; you can tell immediately what directory the model will have access to. If you see a directory in the header that the model shouldn't have access to, you'll know to quit the app and restart it with the correct directory.
When we start making the app interactive, we'll do more validation about the project directory and its contents.
Building the UI
Now let's start building the UI, by splitting the available space into two columns. We'll start in junco.py:
... from textual.app import App, ComposeResult from textual.widgets import Header, Footer, Static from textual.containers import Horizontal class JuncoApp(App): """A Textual app that serves as a simple harness for AI models.""" CSS_PATH = "junco.tcss" BINDINGS = [("d", "toggle_dark", "Toggle dark mode")] ... def compose(self) -> ComposeResult: """Create child widgets for the app.""" yield Header() with Horizontal(classes="columns"): yield Static("Info") yield Static("Running conversation") yield Footer()
We first import Textual's Static widget and the Horizontal container. We also set CSS_PATH, which is where we'll store styling directives for various UI elements.
The only method that's involved in this change is the compose() method, where we define the overall UI. Here we add a Horizontal container, which spreads its child elements out horizontally. In that container, we build two Static widgets. We'll replace these with more specific widgets when we make them functional.
Here's the new junco.tcss file:
.columns > Static { height: 1fr; width: 1fr; border: round $primary; }
This set of style rules applies to every Static widget that's a child of a container with the class columns. This sets each column to use 1fr of height and 1fr of width. In Textual, 1fr refers to one fractional sizing unit. Effectively, this lets both columns take up all the available height, and they each share the horizontal space equally.
This excerpt from the Textual docs is probably helpful:
The unit fraction is used to represent proportional sizes.
For example, if two widgets are side by side and one haswidth: 1frand the other haswidth: 3fr, the second one will be three times as wide as the first one.
When all the sizes are 1fr, available space will be shared equally.
When you run Junco now, you'll see the two columns:

This is great; the harness is starting to take shape!
Multiple cells
Now let's break that left column into three separate boxes:
... from textual.containers import Horizontal, Vertical class JuncoApp(App): """A Textual app that serves as a simple harness for AI models.""" CSS_PATH = "junco.tcss" BINDINGS = [("d", "toggle_dark", "Toggle dark mode")] ... def compose(self) -> ComposeResult: """Create child widgets for the app.""" yield Header() with Horizontal(classes="columns"): # Left column. with Vertical(classes="rows"): yield Static("My messages") yield Static("Tool calls") yield Static("Alerts") # Right column. yield Static("Running conversation") yield Footer()
Here we use the Vertical container to break the left column into three separate widgets: one for My messages, one for Tool calls, and one for Alerts.
The style rules for the rows are the same as they are for the columns, but let's write a separate block so we're ready to customize them:
.columns > Static { ... } .rows > Static { height: 1fr; width: 1fr; border: round $primary; }
These rules share all the space in the left column equally between the three new rows in that column.
Here's what the UI looks like now:

That's the UI we were after in the initial sketch.
Cleaning up
Let's take one more step in preparation for the next phase of work, where we'll start to use these widgets. Let's make each of the widgets an attribute in JuncoApp, so we'll be set up to interact with those UI elements as the project gains functionality:
def compose(self) -> ComposeResult: """Create child widgets for the app.""" yield Header() with Horizontal(classes="columns"): # Left column. with Vertical(classes="rows"): self.prompt_box = Static() self.prompt_box.border_title = "My messages" yield self.prompt_box self.tool_call_box = Static() self.tool_call_box.border_title = "Tool calls" yield self.tool_call_box self.alert_box = Static() self.alert_box.border_title = "Alerts" yield self.alert_box # Right column. self.running_conv_box = Static() self.running_conv_box.border_title = "Running conversation" yield self.running_conv_box yield Footer()
All these changes take place in the compose() method. Each widget is now assigned to an attribute such as self.prompt_box. We'll be able to access the contents of these elements as the project becomes functional. For example, we'll be able to read from these widgets and write to them as needed. We also use the border_title attribute of each widget, so we're not using valuable space inside each widget for a label.
Here's the final UI before we focus on interactive features:

That's a fairly simple UI, but it's all we'll need to make a quite useful harness!
Conclusions
Textual is a fantastic Python library. With it, you can quickly build just about any terminal UI you can sketch.
In the next post, we'll make some of these boxes interactive. We'll start by writing some prompts, and recording them in the running conversation. Then we'll send those messages to an AI provider, and wait for a response.
You can find the code for the Junco demo project at https://github.com/ehmatthes/junco.