Zum Inhalt springen

The shared CodeCharter host

How analyze, graph, coverage, req and the MCP server share one warm per-user CodeCharter host, when a run stays in its own process, and how to inspect, stop and restart the host, open its web UI, read its log and watch its metrics.

Starting a .NET analysis engine costs time on every run: the tool has to load, and an MCP server has to load the solution again for every editor or agent that starts one. CodeCharter therefore runs one long-lived host process per user and per CLI version, and lets short-lived commands hand their work to it.

  • codecharter analyze, codecharter graph …, codecharter coverage and codecharter req … run in the host. Output, exit codes, the working directory, --workspace, environment variables and Ctrl+C behave exactly as if the command ran in its own process; the processes a command starts, such as dotnet test or git, see your environment, not the host's.
  • codecharter mcp (the stdio transport) runs its session in the host. Every MCP client on the machine then shares one analysis cache, so a second editor or agent on the same workspace starts warm. Each session still analyzes in the directory it was started from, with the license and settings of the process that started it.

analyze and graph … also use that shared cache: the first call on a workspace loads it, and every later call, from the CLI or from an agent, finds it warm. The output is the same as when the command runs in its own process. The cache is shared only for your own license and organization, and only when the command runs in the same environment as the host: variables such as Configuration, DOTNET_ROOT or CODECHARTER_ORG that differ from the host's make MSBuild evaluate the project differently. What only describes the shell, terminal, editor or agent a process was started from is ignored, such as PWD, TERM, TEMP, VSCODE_* or CLAUDE_*, and PATH counts only by the dotnet it finds first. Any other variable that differs keeps the command off the cache, since a project can read it as an MSBuild property; the host's log names those variables, never their values, so you can see why a run stayed cold. A command that names another license with --license, a different environment, rule profiles resolved on the fly for a floating config.local.yml profile, or a --rules directory that does not exist analyzes on its own, as before. The Runs page of the web UI marks each run as warm, cold or not using the cache.

The first command starts the host in the background. It talks to its clients over a named pipe (a Unix domain socket on Linux and macOS) that only your user account can open, and it stops on its own once it has had nothing to do for a while. A newly installed CLI version starts its own host; the host of the old version stops once it is idle.

The host does not run from where CodeCharter is installed. The first time a CLI version starts a host, it copies itself into the host\bin folder of the CodeCharter data directory (%USERPROFILE%\.local\state\codecharter\host\bin on Windows) and starts the host from that copy, so a running host never blocks dotnet tool update or an installer. A host removes the copies of other versions that no CodeCharter process runs from any more.

A host that already runs keeps running the binaries it was started from, even when a reinstall or rebuild has replaced the files of the same version since: restarting it on its own would end the MCP sessions of your other editors and agents. When codecharter host status or codecharter ui finds that the running host is a different build than the CLI you called, it says so on standard error:

The running CodeCharter host is a different build than this CLI. If you updated or reinstalled CodeCharter, run `codecharter host restart` to pick up the new binaries (this ends open MCP sessions). The build is told apart by file size and modification time, so a reinstall of identical files can show this hint too.

If you updated or reinstalled CodeCharter, codecharter host restart picks up the new binaries in one step. Like any host stop, it ends the open MCP sessions. CodeCharter tells builds apart by the size and modification time of their files, so a reinstall of identical files can show the hint too; a restart clears it then as well.

When a run stays in its own process

The host is meant for interactive use. A run does not use it when:

  • a CI server runs it: any of CI, GITHUB_ACTIONS, TF_BUILD, GITLAB_CI, JENKINS_URL or BUILDKITE is set (to anything but 0, false, no or off);
  • you pass --no-host, which works on every command;
  • CODECHARTER_NO_HOST=1 is set, which keeps every run in its own process;
  • the command asks for help or the version (--help, -h, -?, /?, /h, --version), which the calling process prints itself;
  • the command reads a diff from standard input (--diff -) or names a response file (@file), which only the calling process can read;
  • the host cannot be started or reached. The command then runs in its own process and says so once on standard error.

A forwarded command waits its turn on the host: one command runs per workspace at a time, and only a few run at once overall (CODECHARTER_MAX_CONCURRENT_COMMANDS, 2 by default). While it waits, the host tells the caller on standard error why, for example "Queued behind another command running in ." or "Waiting for a free command slot". A command that has printed nothing for 15 seconds, because the host is still starting or is queued behind another command, also says so once on standard error and names --no-host as the way to run it in its own process.

# One run without the host:
codecharter analyze MySolution.sln --no-host

# Never use the host on this machine or in this shell:
export CODECHARTER_NO_HOST=1

codecharter mcp --transport http is not affected: the HTTP server always runs in its own process.

Inspecting and controlling the host

The host commands address the host of the CLI version you run. They never start a host just to look at it.

Command What it does Exit code
codecharter host status Shows whether the host runs, its version, process id, uptime, memory, forwarded commands in flight, open MCP sessions and the workspaces it has cached. A host that runs but whose discovery record is missing is reported as such, with its process id and codecharter host restart as the remedy. A discovery record that cannot be trusted, for example one another account may write to, is reported with what is wrong and the file it is in; codecharter host restart removes it and replaces the host, or delete the file yourself and a running host writes a new one within seconds. host runs, host cancel, host warm, host evict and codecharter ui report it the same way and exit with 1. 0 when a host runs, 1 when none runs, when it runs without its discovery record or when its record cannot be trusted, 3 when the state directory is not private
codecharter host status --json The same as JSON, led by a running flag, for scripts. buildIdentity names the build the host runs. A host without its discovery record is written as "running": true, "responding": true, "discoveryRecord": false and its processId; a record that cannot be trusted as "running": false, "discoveryRecordRejected": true and its discoveryRecordPath. When the state directory is not private, the flag is false and an error object follows it. as above
codecharter host stop Asks the host to finish the work it is doing and exit, and waits until its process has exited. A host whose discovery record is missing is stopped the same way. A discovery record that cannot be trusted is removed first when it lies in a directory private to you, and the host it hid is stopped; in any other directory it is left alone and reported with 1. The request names the host's process, so a host that started after it had exited is not stopped in its place. Succeeds when no host runs or the host had already exited. 0, or 1 when the host did not stop in time, a host that started since answered and keeps running, or an untrusted discovery record could not be removed
codecharter host restart Stops the host if one runs, a host without its discovery record included, waits until its process has exited and starts a fresh one. A discovery record that cannot be trusted is removed first, as host stop does. When a host that started after the old one had exited answers the request, that host is kept as the fresh one. 0, or 1 when stopping or starting failed, or an untrusted discovery record could not be removed
codecharter host runs Lists the runs in progress and the most recently finished ones, newest first, with their outcome. --limit (-n, 1 to 500, default 20) sets how many finished runs, --json writes them as JSON led by a running flag. 0 when a host runs, 1 when none runs, 3 when the state directory is not private
codecharter host cancel <run> Cancels a run in progress, as the cancel button of the web UI does. <run> is the run's id or its first characters, as host runs shows them in brackets. A cancelled command stops the processes it started, such as the dotnet test of a coverage run. 0 when cancelled, 1 when no host runs or no single run in progress matches
codecharter host warm [<path>] Loads the solution, project or directory (default: the current directory) into the host's analysis cache, so agent (MCP) sessions and the host web UI find it warm. Returns at once when it is already loaded. Starts the host if none runs. Prints the finding counts. 0 when loaded, 1 when the load failed or the path names no solution or project
codecharter host evict [<path>] Unloads the workspace from the host's analysis cache and frees its memory. A workspace that is still loading is not unloaded; cancel its load instead. 0 when unloaded, 1 when no host runs or the workspace is not loaded
codecharter host logs Prints the most recent entries of the host's log, oldest first. --lines (-n, 1 to 1000, default 50) sets how many, --level leaves out entries below a level such as Warning, and --follow (-f) keeps printing new entries until Ctrl+C. Then prints, from second-instance.log, why hosts started while one already ran exited, as far back as the oldest entry printed. Works whether or not a host runs. 0

A host command line that does not parse, such as --lines 0 or a --state-dir without a directory, exits with 2, since 1 says that no host runs.

$ codecharter host status
CodeCharter host 1.7.0 is running (pid 4242, up 2h 5m).
Memory: 812 MB private, 640 MB managed heap
Forwarded commands in flight: 0
MCP sessions: 2
Cached workspaces: 1
  loaded   used 3m 12s ago  C:\repos\shop\Shop.sln
Runs in progress: 1
  analyzing  12/40  mcp load  for 25s  C:\repos\billing\Billing.sln  [3f2a9c1d]  (Billing.Api)

Every forwarded command and every workspace an MCP session loads is a run. A run is queued while it waits for its turn, then loading or analyzing while it works on a workspace, or running while it runs a command. A coverage run shows building while it builds the solution and testing with how many test projects have finished and the one it runs (3/5 (Billing.Tests)). Each run in progress shows the start of its id in brackets, which host cancel takes. A queued run also says what it waits for, for example waiting for ui coverage in C:\repos\shop. A command forwarded from the CLI that waits for a free command slot says waiting for a free command slot (CODECHARTER_MAX_CONCURRENT_COMMANDS); that variable sets how many commands the host runs at once. A forwarded command that waits for another command in the same folder says waiting for another command in C:\repos\shop. A command started from the host's web UI does not show these two waits. A coverage run that waits for another coverage run to release its results folder names that run's process, for example waiting for the coverage run in process 4711 to release C:\repos\shop\TestResults\coverage. A run ends as done, failed or cancelled. A command that could not do its work ends as failed, whether the web UI or a forwarded CLI call started it. That is every exit code its command documents for that (see Exit codes): analyze exiting 2, 3, 4, 6, 10 or 64, graph exiting 2, 3 or 6, coverage exiting 2, 3, 6 or 64, req check exiting 2, 3 or 6, req accept exiting 1, 2, 4, 5 or 6, req coverage exiting 3 or 6, update exiting 1, 2 or 6 and restore exiting 1, 2, 6 or 64, any command exiting 70 (internal error), and a coverage run whose test project did not build. Exit code 2 of req coverage stays done: it is also the code of a failed ratchet. host status lists the runs in progress; host status --json has them under runs.

Commands from the web UI that load a solution into the host (warm, analyze and graph diff) wait in one queue, so the host loads one at a time. Coverage, update and restore wait in another: a coverage run that tests for a long time does not hold up loading another workspace.

host runs also lists the finished runs. The host keeps them in a file in its data directory, so they survive a restart: the last 500, none older than 30 days. A finished command shows its exit code, a finished workspace load the number of errors, warnings and infos it found. A run that ended in an error names that error on the line below it; host runs --json has it as error.

$ codecharter host runs -n 3
Runs in progress: 0
Finished runs: 3
  done       3E 0W 12I  mcp load  took 41s  2h 0m ago  C:\repos\billing\Billing.sln
  failed     exit 2  cli graph  took 1m 0s  3h 12m ago  C:\repos\shop
  failed     -  ui analyze  took 2s  4h 5m ago  C:\repos\legacy
             error: Not a solution file.

Forwarded commands from the same directory run one after another; commands from different directories run side by side, at most two at once. Set CODECHARTER_MAX_CONCURRENT_COMMANDS to change that number. MCP sessions load at most two workspaces at once, so a burst of first requests does not slow every one of them down; set CODECHARTER_MAX_CONCURRENT_LOADS to change that number. A workspace that is already loaded answers at once and never waits. A load that reports no progress for 20 minutes may be hung: the host stops it, gives its place to the next queued workspace, and the next call for that workspace starts a fresh load and names the stopped attempt. Each project the load evaluates, builds or analyses and each rule it evaluates counts as progress, so a load that keeps making progress is never stopped, however long it takes; only a single step that takes longer than the limit, such as one project whose build runs for 20 minutes, is. Time the computer spends asleep does not count. Set CODECHARTER_MCP_LOAD_STALL_MINUTES to wait longer.

The memory figures are those of the whole host process. CodeCharter reports no size per workspace, because workspaces share compiled metadata and any per-workspace number would be an estimate. To free memory, unload a workspace with codecharter host evict or restart the host.

The cache has a memory budget: when the host grows past it, it unloads the workspace used least recently. It defaults to 2 GB, but to no more than three quarters of the memory available to the process (in a container, its memory limit) and to no less than half of it. Set CODECHARTER_MCP_MEMORY_BUDGET_MB to change it, or to 0 to turn it off; an invalid value is reported in the host log and the default applies.

The web UI

The host also serves a web UI with what it is doing right now and what it has stored: the overview, the runs and workspaces of the host, its log and settings, and, from the insights store, findings, architecture, coverage, requirements and AI sessions. The section Host web UI describes every page, how sign-in and the session work and how to start commands from the UI. A waiting or running command has a Cancel button there; the command in your terminal then ends with exit code 130 and says it was cancelled from the host UI. The settings page lists the host's files, limits and retention, each with the environment variable that changes it; the host reads them when it starts, so set the variable and run codecharter host restart.

# Open the UI in your default browser, starting the host if it is not running:
codecharter ui

# Only print the sign-in link, for another browser or a remote session:
codecharter ui --no-browser

The UI listens on 127.0.0.1 only, on a port the operating system picks, and is closed to everyone but you: the sign-in link works once, for 60 seconds, see Sign-in and session length.

codecharter ui exits with 1 when the host cannot be started or reached, and with 0 otherwise, also when no browser could be opened.

The host log

A host runs without a console, so it writes its log to files: when it starts and stops, and one line for every command it runs, with the working directory, the exit code and how long it took. Warnings and errors from the host's components end up there as well.

The files live in a logs folder in the host's data directory, one file per day, and are kept for 14 days. Each CLI version has its own data directory: %USERPROFILE%\.local\state\codecharter-<version>\host\logs on Windows, and the matching per-user data directory on Linux and macOS. On Windows the data directories are deliberately not under %LOCALAPPDATA%: Windows redirects what a program started from an MSIX-packaged app such as Claude Desktop writes there, and a host started from such an app would be invisible to your terminal. codecharter host logs reads them for you, so you rarely need the path.

A host started while another one of the same version already runs exits at once. It cannot write into the log file the running host holds open, so it notes why it exited in second-instance.log in the same logs folder, and on standard error when you started it yourself. codecharter host logs prints the notes no older than the oldest log entry it prints, after the entries; --level Error leaves them out like other warnings. A single note can come from two commands that started a host at the same time, which is harmless. Recent notes that repeat point to commands that could not find the running host, for example because its discovery record is missing; codecharter host status tells you, and codecharter host restart replaces such a host.

# The last 20 warnings and errors:
codecharter host logs -n 20 --level Warning

# Watch what the host does while you work:
codecharter host logs -f

Running an isolated host

Sometimes you want a host that shares no host state with your everyday one: to try a build of CodeCharter, to test a script against the web UI, or to run two hosts of the same version side by side. Name a state directory and CodeCharter runs a host that keeps all of its files there, and every command that names the same directory talks to that host only:

  • --state-dir <dir> on any codecharter host command, for example host run, host status, host stop or host restart. Give it on the command line after host; options before the verb, such as --workspace, are fine;
  • the CODECHARTER_HOST_STATE_DIR environment variable, for every command: forwarded analyze, graph, coverage and req runs, codecharter mcp, codecharter ui and the host commands. --state-dir wins over it.

A relative path is taken from the current directory, and a junction, a symbolic link or a short name of a directory names the same host as the directory itself. The host started for such a command keeps its single-instance lock, discovery record (discovery.json), access token, log, run history, insights database and its copies of the CLI in that directory, so nothing of it lands in the per-user data directory:

<dir>/CodeCharter-<version>-<id>/host/   discovery.json, host.token, logs/, run history
<dir>/CodeCharter/host/                  insights/insights.db, bin/ (the host's copies of the CLI)

<id> tells the directory apart from every other one, so an isolated host also gets a pipe of its own and never collides with your default host. A command that names a state directory never falls back to the default host: when no host runs there, it starts one there. A host removes only the unused copies of the CLI in its own directory. On Windows, where a copy below a long state directory would have a path of 260 characters or more, which Windows cannot start a program from, the host starts from the installed CLI instead, and so does an MCP server that codecharter mcp install set up. The CLI then says so on standard error. While either runs, an update of the CLI can fail on its locked files: stop the host with codecharter host stop and close the MCP sessions before you update. Commands that only ask, such as host status, create nothing in the directory.

What stays per user is everything that is not the host's: your license and sign-in, the telemetry client id and the salt that makes workspace ids pseudonymous, and the rule bundle cache.

The state directory must be private

A command trusts what it finds in the state directory: the discovery record names the pipe it sends your command and its environment through, and the host starts from the copies of the CLI there. So the directory must be private to your user account. Name a path that does not exist yet, and CodeCharter creates it accessible to you only. A directory that exists must belong to you, and no other account may write to it: on Windows no access entry may let another account, such as Users, Authenticated Users or Everyone, write, delete or change permissions; on Linux and macOS neither its group nor others may write to it. The Administrators, their members and SYSTEM count as you, since they can take any directory anyway.

No other account may rename or replace the directory either, through any of the directories that hold it, up to the root of the drive or file system:

  • on Windows, none of them may let another account delete it, change its permissions or take it over, and none may let another account delete what it holds. The root of a drive and the directories of your profile pass; a directory below D:\ that inherits Authenticated Users: Modify does not, since every signed-in user may rename it;
  • on Linux and macOS, each of them must belong to you or to root, and its parent may be writable by its group or others only when it is sticky, as /tmp is.

Your home directory, or a new directory in it, meets all of this. A command that reaches the host checks the directory first: the host commands, host run and codecharter ui stop with exit code 3 and say which directory is not private, and forwarded commands and codecharter mcp run in their own process instead, sending nothing to the host. host status --json and host runs --json also write the reason to standard output as {"running": false, "error": {"code": "state-dir-not-private", "message": ..., "fix": ...}}, where message names the directory and fix says what to do, and every tool result of such an MCP session carries it in its notice. Commands that run in their own process anyway, such as those with --no-host or CODECHARTER_NO_HOST, never read the directory. A --state-dir without a directory, or one in a response file, stops with exit code 2 before the command runs.

host run also takes --port <port> for the web UI: a fixed port from 1 to 65535, or 0, the default, for one the operating system picks. Either way the host writes the port to loopbackPort in its discovery.json, and codecharter ui links to it. A port that is already in use stops host run with exit code 3. The fixed port applies to that host run only: a host that host restart or any other command starts, for example after the host stopped when it was idle, gets a free port, which codecharter ui finds the same way.

# A host of its own for a test session, its web UI on port 22290.
# The directory does not exist yet, so CodeCharter creates it private.
codecharter host run --state-dir ~/codecharter-test-host --port 22290

# In another shell: every command of this shell uses that host.
export CODECHARTER_HOST_STATE_DIR=~/codecharter-test-host
codecharter ui --no-browser
codecharter host status
codecharter host stop

For an MCP client, set CODECHARTER_HOST_STATE_DIR in the environment of the MCP server entry, so its sessions run in the isolated host. codecharter mcp install always points its entries at your default host, also when CODECHARTER_HOST_STATE_DIR is set, so deleting a state directory never breaks the MCP server of your tools. To get rid of an isolated host, stop it and delete its state directory.

Metrics

The host publishes metrics that any .NET metrics listener can read, for example dotnet-counters:

Meter Instrument What it measures
CodeCharter.LocalHost codecharter.host.command.duration Seconds each forwarded command took, including the wait for its turn, tagged with command and exit_code.
CodeCharter.Mcp codecharter.workspace_cache.lookups Every time an MCP tool asked for a workspace analysis.
CodeCharter.Mcp codecharter.workspace_cache.load.duration Seconds each workspace load took, tagged with outcome (succeeded, failed or cancelled). Every load is a cache miss, so lookups minus loads are the hits.
CodeCharter.Mcp codecharter.workspace_cache.evictions Every workspace the cache let go, tagged with reason: idle (not used for longer than the idle timeout), count (more workspaces were loaded than the host keeps), budget (the host went over its memory budget), requested (unloaded on request), refreshed (unloaded to be analyzed afresh), changed (its files or rules changed on disk) or reconfigured (the rules or the configuration were changed through the MCP server).
# Find the host's process id with `codecharter host status`, then:
dotnet-counters monitor --process-id <pid> --counters CodeCharter.LocalHost,CodeCharter.Mcp