# cplieger/web-terminal-kiro architecture

> Architecture diagram of the GitHub repository cplieger/web-terminal-kiro, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cplieger/web-terminal-kiro
- Repository: https://github.com/cplieger/web-terminal-kiro

## Overview

1. **What this repo is**
   - `web-terminal-kiro` is a small Go server that exposes a browser-based terminal for `kiro-cli`.
   - It is not a generic chat app or IDE; each browser tab gets its own live PTY session running the CLI directly.

2. **Top-level runtime shape**
   - The backend is a Go HTTP server (`main.go`, `routes.go`) that serves the web app, upgrades terminal sessions over WebSocket, and manages startup/bootstrap.
   - The frontend is a TypeScript app in `static-src/` that is built into the committed `static/` assets and served by the Go binary.

3. **Request/data path**
   - Browser → HTTP server → WebSocket terminal protocol → terminal engine / PTY session → `kiro-cli`.
   - Terminal output is rendered in the browser using real text nodes, not a canvas, which is why selection, scrolling, and touch interaction behave like native text.

4. **Session model**
   - The central unit is a per-tab session.
   - Sessions are PTY-backed and isolated from each other; reconnect logic replays screen state and scrollback so a dropped connection does not lose the terminal context.

5. **Backend responsibilities**
   - The Go service is responsible for:
     - listening on `KWEB_ADDR` (`:9848` by default),
     - creating session managers,
     - serving static files,
     - handling WebSocket connections,
     - logging client access,
     - and enforcing trusted proxy handling.
   - Most terminal semantics are delegated to shared terminal libraries rather than reimplemented here.

6. **Frontend responsibilities**
   - `static-src/app.ts` is the browser UI for tabs, keyboard toolbar, touch interactions, selection, and reconnect behavior.
   - The build/test tooling in `static-src/` indicates a modern TypeScript toolchain with unit tests, linting, and type checking.
   - The compiled output is checked in under `static/`, so the Go binary can serve a self-contained UI.

7. **Persistence and filesystem boundaries**
   - `/config` is the persistent control plane for user state: `kiro-cli` auth, settings, installed tools, SSH config, git config, and MCP settings.
   - `/workspace` is the working directory where terminal sessions start and where repos/files live.
   - The container intentionally runs as root so tools like `git`, `gh`, and SSH work without extra setup.

8. **Bootstrap and tool installation**
   - On first boot, the server ensures `kiro-cli` is installed; it is downloaded on demand rather than shipped in the image.
   - Additional tools are defined in `/config/tools.json` and installed into `/config/tools/`, which persists across restarts.
   - This makes tool availability part of runtime configuration rather than build-time packaging.

9. **Proxying and security boundary**
   - The app has no built-in authentication; the intended deployment boundary is a reverse proxy or loopback-only binding.
   - `TRUSTED_PROXIES` controls whether `X-Forwarded-For` is trusted for access logs; if unset, the direct socket peer is used.
   - This is a meaningful architectural boundary because it determines whether client IPs are derived from the proxy chain or from the transport peer.

10. **Containerization and deployment**
   - `Dockerfile`, `compose.yaml`, and `entrypoint.sh` define the standard deployment shape.
   - The image is multi-arch and intended for GHCR/Docker Hub use.
   - The healthcheck is part of the architecture: it only reports healthy when both the server and the `kiro-cli` install are usable.

11. **Testing and verification**
   - Go tests cover server behavior and trust-proxy logic (`main_test.go`, `routes_test.go`, `trusted_proxies_test.go`).
   - There are frontend tests in `static-src/app.test.ts`.
   - CI is split across linting, coverage, security, CodeQL, and release workflows, which suggests a strong emphasis on supply-chain and runtime safety.

12. **Overall system boundary**
   - The repo’s boundary is intentionally narrow: it glues together a browser UI, a PTY/session engine, and the `kiro-cli` process.
   - The main architectural diagram should show the browser on one side, the Go server and session manager in the middle, and `kiro-cli` plus persisted `/config` and `/workspace` volumes on the other side.

Main entry points: [`main.go`](https://github.com/cplieger/web-terminal-kiro/blob/main/main.go) (HTTP server), [`routes.go`](https://github.com/cplieger/web-terminal-kiro/blob/main/routes.go) (Routes), [`entrypoint.sh`](https://github.com/cplieger/web-terminal-kiro/blob/main/entrypoint.sh) (Bootstrap), [`main_test.go`](https://github.com/cplieger/web-terminal-kiro/blob/main/main_test.go) (Tests)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_browser(("Browser<br/>web client"))
  node_ui["Terminal UI<br/>TypeScript app<br/>[app.ts]"]
  node_assets["Static assets<br/>served UI<br/>[index.html]"]
end

subgraph group_backend["Go backend"]
  node_server["HTTP server<br/>Go entrypoint<br/>[main.go]"]
  node_routes["Routes<br/>HTTP/WebSocket<br/>[routes.go]"]
  node_bootstrap["Bootstrap<br/>startup setup<br/>[entrypoint.sh]"]
end

subgraph group_runtime["Terminal runtime"]
  node_sessions["Session manager<br/>per-tab sessions"]
  node_pty["PTY engine<br/>terminal I/O"]
  node_kiro["kiro-cli<br/>CLI process"]
end

subgraph group_state["Persistent state"]
  node_config["Config tools<br/>persistent config<br/>[tools.json]"]
  node_workspace[("Workspace<br/>working dir")]
  node_tooling["Tool install<br/>runtime tools<br/>[setup-tools.sh]"]
end

subgraph group_delivery["Delivery"]
  node_container["Container image<br/>deployment"]
  node_health["Healthcheck<br/>runtime probe<br/>[compose.yaml]"]
end

subgraph group_quality["Verification"]
  node_tests["Tests<br/>verification<br/>[main_test.go]"]
  node_ci["CI workflows<br/>automation<br/>[ci.yaml]"]
end

node_browser -->|"uses"| node_ui
node_ui -->|"loads from"| node_assets
node_assets -->|"served by"| node_server
node_browser -->|"HTTP/WebSocket"| node_routes
node_server -->|"hosts"| node_routes
node_routes -->|"creates"| node_sessions
node_sessions -->|"drives"| node_pty
node_pty -->|"runs"| node_kiro
node_sessions -->|"uses"| node_workspace
node_server -->|"reads/writes"| node_config
node_bootstrap -->|"installs"| node_tooling
node_bootstrap -->|"ensures"| node_kiro
node_container -->|"starts"| node_bootstrap
node_container -->|"reports"| node_health
node_tests -->|"verifies"| node_server
node_tests -->|"verifies"| node_ui
node_ci -->|"runs"| node_tests
node_routes -->|"trusts proxy config"| node_config

click node_ui "https://github.com/cplieger/web-terminal-kiro/blob/main/static-src/app.ts"
click node_assets "https://github.com/cplieger/web-terminal-kiro/blob/main/static/index.html"
click node_server "https://github.com/cplieger/web-terminal-kiro/blob/main/main.go"
click node_routes "https://github.com/cplieger/web-terminal-kiro/blob/main/routes.go"
click node_bootstrap "https://github.com/cplieger/web-terminal-kiro/blob/main/entrypoint.sh"
click node_config "https://github.com/cplieger/web-terminal-kiro/blob/main/tools.json"
click node_tooling "https://github.com/cplieger/web-terminal-kiro/blob/main/setup-tools.sh"
click node_container "https://github.com/cplieger/web-terminal-kiro/tree/main/Dockerfile"
click node_health "https://github.com/cplieger/web-terminal-kiro/blob/main/compose.yaml"
click node_tests "https://github.com/cplieger/web-terminal-kiro/blob/main/main_test.go"
click node_ci "https://github.com/cplieger/web-terminal-kiro/blob/main/.github/workflows/ci.yaml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_browser,node_ui,node_assets toneBlue
class node_server,node_routes,node_bootstrap toneAmber
class node_sessions,node_pty,node_kiro toneMint
class node_config,node_workspace,node_tooling toneRose
class node_container,node_health toneIndigo
class node_tests,node_ci toneTeal
```

## Components

### Client

Browser-facing runtime and UI

- **Browser**: web client · User opens a tab and interacts with the terminal UI
- [Terminal UI](https://github.com/cplieger/web-terminal-kiro/blob/main/static-src/app.ts): TypeScript app · Tab UI, keyboard toolbar, touch, selection, and reconnect logic · `static-src/app.ts`
- [Static assets](https://github.com/cplieger/web-terminal-kiro/blob/main/static/index.html): served UI · Built browser bundle and HTML served by the Go binary · `static/index.html`

### Go backend

HTTP server, routing, session management, and bootstrap

- [HTTP server](https://github.com/cplieger/web-terminal-kiro/blob/main/main.go): Go entrypoint · Main process that listens, serves files, and starts the app · `main.go`
- [Routes](https://github.com/cplieger/web-terminal-kiro/blob/main/routes.go): HTTP/WebSocket · Request handling, WebSocket upgrades, access logging, and proxy-aware client IPs · `routes.go`
- [Bootstrap](https://github.com/cplieger/web-terminal-kiro/blob/main/entrypoint.sh): startup setup · Startup flow that prepares the runtime and verifies prerequisites · `entrypoint.sh`

### Terminal runtime

PTY-backed terminal sessions and external CLI

- **Session manager**: per-tab sessions · Creates isolated tab sessions and reconnects dropped clients
- **PTY engine**: terminal I/O · Streams terminal input/output and preserves scrollback state
- **kiro-cli**: CLI process · The terminal application launched inside each session

### Persistent state

Config, workspace, and tool installation boundaries

- [Config tools](https://github.com/cplieger/web-terminal-kiro/blob/main/tools.json): persistent config · Auth, settings, SSH, git, MCP, and installed tool state · `tools.json`
- **Workspace**: working dir · Session working directory for repos and files
- [Tool install](https://github.com/cplieger/web-terminal-kiro/blob/main/setup-tools.sh): runtime tools · On-demand tool definitions and installs managed at runtime · `setup-tools.sh`

### Delivery

Container packaging and startup

- [Container image](https://github.com/cplieger/web-terminal-kiro/tree/main/Dockerfile): deployment · Docker and compose packaging for the runnable service · `Dockerfile`
- [Healthcheck](https://github.com/cplieger/web-terminal-kiro/blob/main/compose.yaml): runtime probe · Reports healthy only when the server and CLI install are usable · `compose.yaml`

### Verification

Tests and automation

- [Tests](https://github.com/cplieger/web-terminal-kiro/blob/main/main_test.go): verification · Go and frontend tests for server behavior and UI behavior · `main_test.go`
- [CI workflows](https://github.com/cplieger/web-terminal-kiro/blob/main/.github/workflows/ci.yaml): automation · Lint, coverage, security, CodeQL, and release automation · `.github/workflows/ci.yaml`

## Connections

- Browser → Terminal UI: uses · The browser loads and interacts with the terminal UI
- Terminal UI → Static assets: loads from · The UI is served as built static content
- Static assets → HTTP server: served by · The Go binary serves the compiled frontend assets
- Browser → Routes: HTTP/WebSocket · Tabs connect to the backend over normal requests and upgraded terminal channels
- HTTP server → Routes: hosts · Main process wires routes and startup behavior
- Routes → Session manager: creates · Each browser tab gets an isolated session
- Session manager → PTY engine: drives · Sessions are backed by PTY I/O and replayable screen state
- PTY engine → kiro-cli: runs · The terminal engine launches and communicates with kiro-cli
- Session manager → Workspace: uses · Sessions start in the shared workspace directory
- HTTP server → Config tools: reads/writes · Startup and runtime settings come from persistent config
- Bootstrap → Tool install: installs · First boot and runtime tool setup are driven by tool definitions
- Bootstrap → kiro-cli: ensures · Startup flow ensures kiro-cli is available before normal operation
- Container image → Bootstrap: starts · Deployment entrypoint launches the service inside the container
- Container image → Healthcheck: reports · Container health depends on server readiness and tool availability
- Tests → HTTP server: verifies · Backend tests cover server and proxy behavior
- Tests → Terminal UI: verifies · Frontend tests cover browser interaction logic
- CI workflows → Tests: runs · CI automates linting, coverage, security, and release checks
- Routes → Config tools: trusts proxy config · Access logging can derive client IPs from trusted proxy headers
