Skip to content

Latest commit

 

History

History
166 lines (119 loc) · 14 KB

design.md

File metadata and controls

166 lines (119 loc) · 14 KB

Design

This page has the design of version 0, the facts that it rests on, and what is not done.

The parts

  • One daemon process (serve) with one event loop. Each agent is a coroutine of the loop, so many agents run at one time. A model call and a command of the run tool are pipelines or thread messages with events: each wait yields.
  • One HTTP port on 127.0.0.1 for the API, the web page, and the sign-in.
  • The clients: the command line, the TUI, the window client, and the web page. The first three use the API with the token. The page uses it with a session cookie.

The state folder

File What it has
token The key of the command line, 32 random bytes as hex, mode 0600.
port The port of the running daemon.
config.json Optional: isolation, region, max_turns, max_tokens, max_out.
passkeys.json The passkeys: the credential id, the COSE algorithm, the public key, and the counter.
next-id The number of the next agent.
agents/ID/meta.json The state of the agent.
agents/ID/messages.jsonl The messages of the Converse API, one for each line. A restart goes on from them.
agents/ID/transcript.jsonl The transcript: n, t (ms), kind, text.
agents/ID/req-*.json The files of the last request to Bedrock.

The folder has the mode 0700. Each file of an agent has the mode 0600.

The API

Each route needs the token (Authorization: Bearer) or a session.

Route What it does
GET /api/agents The list.
POST /api/agents Starts an agent: model, dir, brief, level, net, run, max_turns, max_tokens, max_out.
GET /api/agents/ID The state of one agent.
GET /api/agents/ID/read?since=N&wait=S The entries after N. With wait, the reply waits up to S seconds (30 at most) for a new entry.
GET /api/agents/ID/wait?timeout=S Waits until the state is not running, up to S seconds (60 at most).
POST /api/agents/ID/send {text}. It answers a question, starts a new turn, or joins the next tool results.
POST /api/agents/ID/stop Stops the agent.

The agent loop

  1. Add the user message: the held tool results, an answer for an open ask_coordinator, a note for each other open tool use, the texts that came while the agent ran, and the input. Two user messages in a row become one.
  2. Check the caps: the turns and the tokens (input plus output of each reply).
  3. Call the model. On a failure, the agent waits; the next send tries again.
  4. Run each tool use. ask_coordinator ends the turn with the question; the next send is its answer.
  5. With no tool use, the agent waits.

A restart marks each running agent waiting. The next send starts a new loop from messages.jsonl.

Facts of the runtime

Measured with ztcl 1.0.0-beta.7 (Debug, x86-64 Linux) and ztk 1.0.0-beta.8 on 2026-12-03.

  • A script has no environment (env is empty), and a program of exec gets only the variables of --exec-env. bin/jcode gives HOME, PATH, and the AWS variables.
  • ztcl 1.0.0-beta.7 has no HTTP client of its own, and no SQLite package in this build. The pull request beetlebugorg/ztcl#1327 (stacked on #1319) adds ztcl::crypto hash and hmac. With them, lib/sigv4.tcl signs the calls of Bedrock. Without them, lib/sig.tcl uses the field script_sha256 of ztcl::meter record, and the aws program is the transport.
  • The name lookup of ztcl (sys.resolve) reads only literal addresses, localhost, and /etc/hosts. It sends no DNS query. lib/sigv4.tcl gets the address of Bedrock from getent ahostsv4 and opens it with -servername. So the daemon runs with --net any: a rule for a host name does not match an address.
  • ztcl::tls socket gives a channel with no non-blocking mode. The wait for the first byte of a reply yields, and the read of the rest blocks the daemon.
  • ztcl::meter verify checks only a meter record. ztcl has no command that checks an Ed25519 or ECDSA signature of other bytes. Pull request beetlebugorg/ztcl#1319 (issue #1318) adds ztcl::crypto verify, sign, and pubkey. With a ztcl that has it, the sign-in works; with no such ztcl, each sign-in fails.
  • file normalize does not follow a symbolic link in the middle of a path. lib/paths.tcl walks each path itself.
  • A catch that spans a yield crashes the Debug runner (an assertion in runCaughtBody) when the coroutine resumes from the event loop (beetlebugorg/ztcl#1321). The code has no catch around a yield. The agent loop has no catch; bgerror ends an agent whose loop failed.
  • expr rand() has a fixed seed. Secrets come from /dev/urandom.
  • thread::create -isolation process -files host -caps {exec channels file} -write [list DIR] works on this host (Landlock, user namespaces, cgroup v2 with cpu, memory, pids). A write outside DIR fails with "Permission denied". A TCP connection fails with no socket capability, and UDP gets "Network is unreachable" (a new network namespace with only lo). A program of exec can still look up names with the network off (beetlebugorg/ztcl#1328); beetlebugorg/ztcl#1329 closes it. A program that allocates 700 MB under --thread-mem 268435456 ends with the code 137.
  • The level vm runs with the signed image vm-image-v1 (x86-64, kernel 7.3.6, busybox). A call starts a guest in about 450 ms. The first share mounts only at /mnt/share: another folder gives "mount failed" (errno 2 or 16). The guest has busybox and no other program, so the shell gets a function for each applet. With no -net, DNS and TCP fail ("Network is unreachable"). A guest of -memory 128 keeps a write of 300 MB to its /tmp under 128 MB. The root is read-only. The close of the channel of exec -isolation vm does not end the guest (beetlebugorg/ztcl#1330): lib/isolate.tcl makes the guest with ztcl::vm create and deletes it at the end of the call or at a stop.
  • ztcl has a TLS client and no TLS server, so the daemon serves HTTP on 127.0.0.1 only. A passkey needs a secure context: http://localhost is one, and another host needs HTTPS.

Isolation

lib/isolate.tcl has one interface (check, open, run, close, kill) and a backend for each level. start calls open, and open fails when check gives a reason. The level applies to the run tool. The file tools run in the daemon with the path check at each level.

The memory limit of process is the limit of one thread of the daemon (--thread-mem), the same for each agent. ztcl has no option for the memory of one thread.

WebAuthn

The RP id is localhost and the origin is http://localhost:PORT. The checks are in lib/webauthn.tcl. The attestation statement is not checked: the one-time code of the terminal is the trust of the first passkey. A session can add more passkeys with no code.

Not done in version 0

  • The file tools of an agent at the level vm act on the host folder, with the path check, and not in the guest.
  • The credentials come from ~/.aws/credentials or from aws configure export-credentials. The aws program has the full power of the user, as each exec has.
  • The check of a path and the open of the file are two steps. A program of the agent can change a folder into a link between them.
  • No streaming of model output. The transcript gets each reply when it is complete.
  • No cap of the total spend of all agents, and no per-agent memory limit.
  • No TLS, so no access from another host.
  • tests/browser.mjs tests the page in headless Chromium. It is not part of tools/test.sh: run node tests/browser.mjs /usr/bin/chromium ZTCL.
  • The TUI is a line interface. ztcl::vt keeps the state of a terminal and does not draw on the terminal of the user.
  • No ztcl.project: its name key would be a second place of the working name. The tests run through tools/test.sh.

The level vm: what ztcl must fix

From the live runs of the level vm (issue #2):

  1. The guest image has no /bin/sh and no links of the applets, and its root is read-only. A script with #!/bin/sh failed, and a child shell found no tr or awk. jcode mounts a tmpfs on /bin with busybox and its links before each command. The image should have them.
  2. The image has busybox only: no git, no compiler, and no way to add a package. Most agents cannot build or test in the guest. A larger signed image, or a second share with tools of the host read-only, would help.
  3. Only the first share mounts (EBUSY for another folder), so --write cannot reach the guest.
  4. The close of the channel does not end the guest (ztcl#1330), and the delete of the guest gives the channel no end of file. jcode deletes the guest and wakes the waiting coroutine itself.
  5. No cgroups and no Landlock in the guest (ztcl#1315).
  6. A runner built with zig build has no download for image fetch (ZTCL VM UNAVAILABLE). This was the cause of the refused fetch in round 2. A release runner has it.

Decisions for the owner

  1. Merge ztcl#1319 and #1327. The sign-in and SigV4 wait for them.
  2. Merge ztcl#1329 (issue #1328).
  3. A DNS client in sys.resolve of ztcl would remove getent and --net any.
  4. The default level is process. Keep it, or choose none for speed.

Threat model of the web port

The daemon listens on 127.0.0.1 only. The checks are in lib/daemon.tcl and lib/web.tcl. tests/browser.mjs and tests/web.test test them.

A page of another origin, in the browser of the owner:

  • It can send a request, but it cannot read the reply: the daemon sends no CORS header.
  • A POST with a cookie needs the header X-CSRF with the token of the session. A page of another origin cannot set it with no CORS preflight, and the reply to the preflight allows nothing. The cookie is SameSite=Strict too, and each POST of a session must have the Origin of the daemon.
  • DNS rebinding: a request with a Host other than localhost:PORT or 127.0.0.1:PORT gets 421.
  • A frame of the page: frame-ancestors 'none' refuses it.
  • The sign-in: the client data must have the origin http://localhost:PORT and the challenge of the session. A challenge is good for one use.

A local process of another user:

  • It can connect to the port. It cannot read the token: the state folder is 0700 and the files are 0600.
  • With no token, no cookie, and no passkey, each route of the API gives 401. The first registration needs the one-time code of the terminal, with 5 tries.
  • It can see the traffic on the loopback only with root. The connection has no TLS.

A process of the same user is out of scope: it can read the token and the state folder.

The output of an agent in the page:

  • The page puts each text of the API into the DOM with textContent or value, and never with innerHTML. tests/browser.mjs sends agent text with <script> and an onerror attribute, and checks that no element is made and no script runs.
  • The CSP is default-src 'none'; script-src 'self', with no inline script, so an injected element could still run no script.
  • The page shows no link and no image of an agent, so agent text cannot make the browser fetch a URL.
  • The CLI and the TUI change each control character of agent text to a space, so agent text cannot send escape sequences to a terminal.

What the model found: no gap in the page. Two gaps of the host: ztcl#1328 (above) and the use of --net any for SigV4.

The review before the first public version fixed these, each with a test:

  • A byte that is not UTF-8 in the body or the query ended the read of the request with an error, and the connection stayed open until its timer. Now it becomes U+FFFD (web-7).
  • The count of open connections had no cap. Now the server closes each one past 64 (web-8).
  • /auth/register/begin and /auth/logout of a session checked the Origin but not the CSRF token. Now they check both (web-9).
  • read_file and edit_file read a file of each size into the memory of the daemon, so a sparse file of the agent could end the daemon. Now they refuse a file larger than 16 MiB (tools-9).
  • A stop killed the process group of each process with the marker of the command in its arguments. A process in the group of the daemon with that marker made the stop end the daemon. Now a stop never kills the group of the daemon (isolate-17).

Not checked: the time of a search with a hostile regular expression (it runs in the event loop of the daemon), the CBOR and COSE parser against fuzzed input, the window client, and a race of a link change between the check of a path and its open (lib/paths.tcl states it).

Planned: remote hosts

A backend host above vm: the daemon starts a host in the cloud (AWS EC2 first), runs a job there, streams the result lines, and stops the host. Nothing of it is built.

Commands:

  • jcode host start --profile P: starts a host from the profile P (an image, a type of instance, a region, the caps) and gives its id.
  • jcode host run ID --commit HASH --job NAME: runs a job on a commit and streams the result lines to the transcript.
  • jcode host stop ID.
  • start --isolation host --profile P: an agent whose run tool runs on the host.

The interface of lib/isolate.tcl stays the same: check gives a reason when the profile or the AWS access is absent, open starts or takes a host, run sends one command and yields until its end, and close stops the host.

Requirements:

  • The host has no secret: a read-only clone of the repository, no token that can write, and no AWS key that can start an instance. The daemon holds the launch rights; the host gets none.
  • The host stops itself after a time limit that it gets at the start, also when the daemon is gone (a shutdown timer in the image, and the instance setting "terminate on shutdown").
  • A cap on the count of hosts at one time and on the run time of each host, in config.json. A start past a cap fails with the reason.
  • The result names the exact commit hash and the tree hash that ran.
  • A prepared image with the tools and a warm build cache (the Zig cache of the main branch), so a job does not build from nothing.
  • The first user is the gate of ztcl in lanes: a quick phase, then the slow checks in lanes. A host for each lane runs a part of the checks of one commit, and the daemon joins the result lines in the form of gate.tcl.