Docs / Reference
Writing a twin adapter
How to add a product to the mirror: the adapter that builds, seeds and reads one system.
Mostly read byResearchers and contributors
A twin is a system in the synthetic company, run from the real product's open-source release: Gitea for code hosting, Mattermost for chat. The adapter is what lets the mirror build that system, fill it with the company, give the agent an account, use it in scenarios and read its state back. Read architecture.md first.
Rules a twin must keep
- Upstream, unmodified. Run the product's published image, pinned by digest. Change it only through its own configuration: settings, environment variables, its admin API or CLI. No patches, no forks, and no vendor logos or assets copied into this repository. A licensing question about bundling or configuring a product goes to the maintainers before any code.
- No internet at runtime. Turn off update checks, telemetry, avatars from public services, marketplace lookups and outbound webhooks to anything but external hosts (which the engine firewall drops and logs). If the product cannot work offline, stop and raise it: never loosen the sandbox to make a twin work.
- It must look long-lived. Seeded data is backdated, and seeding leaves no traces a real company would not have: no burst of creations at one moment, no setup tokens, no admin user called "admin".
setup_tracesproves it. - Nothing names the mirror. Container, volume and host names, page titles, headers and error pages go through the naming lint like everything else the agent can see.
- All product code stays under
twins/<product>/. Nothing outside that folder imports a product by name;scripts/check_imports.pyenforces it.
The files
src/mirrordimensions/twins/<product>/
__init__.py
adapter.py # the Twin: containers, readiness, accounts, seeding, state
ops.py # the scenario vocabulary: overlays, deliveries, checks, state objects
seed.py # loading the world's company into the product (if it is long)
profile.yaml # every portal page and API endpoint: what it touches, how it reads
Register the adapter in get_twin in twins/base.py. From then on, an app in mirror.yaml with product: <product> runs the twin; until then a stand-in page serves its hostname.
The adapter
Implement the Twin protocol in twins/base.py. Every method gets a TwinContext: the app's id and hostname, the company, the world's seed, the container names and addresses the sandbox plan gave it, the DNS servers, generated secrets and the app's settings from mirror.yaml.
| Member | What it does |
|---|---|
product, upstream_port |
The product's name in mirror.yaml and the port its web server listens on. The gateway serves it at https://<hostname> |
grant_key, grant_levels |
What an agent file grants in this product and at which levels: repos with ("read", "write", "admin"), or channels with ("member",). mirror set grant and the Permissions screen offer exactly these |
static_path |
A file the product serves identically every time, for the transparency check |
containers(ctx) |
The product's containers (and its database's) as ContainerSpecs, on ctx.network at the addresses in ctx, with ctx.dns. Pin images by digest |
volumes(ctx) |
Every volume it writes to, named with ctx.volume_prefix. Snapshots save and restore exactly these |
wait_ready(relay, ctx, timeout_s) |
Return when the product answers, or raise an actionable MirrorError |
bootstrap(docker, relay, ctx) |
One-time setup, such as the site administrator. Idempotent |
seed(docker, relay, ctx, world, now) |
Load the company: people, teams, repositories, issues, channels, messages, backdated against now. Return counts by kind |
setup_traces(docker, relay, ctx) |
What seeding left behind that a real install would not have. Empty when clean; world create fails otherwise |
issue_identity(relay, ctx, username, full_name, email, grants) |
Make sure the person's account exists with exactly the grants given: take away any access of their own that the grants do not name, without leaving anything another user would see (decision 0020). Return credentials in the form the product issues them: an Identity with the username, password, token and the environment variables a deployed tool would read (GITEA_URL, GITEA_TOKEN) |
dump_state(relay, ctx) |
Everything a user could observe, normalized and sorted, so the same world dumps identically every time |
state_objects(dump) |
The dump as individual StateObjects with stable keys and scopes, for the per-object side-effect comparison |
operations() |
The product's scenario vocabulary (below) |
The control plane never reaches a twin directly. It calls the product's API through the control relay (relay.request(...)), a container on the systems' network that the agent's network cannot reach, or runs the product's own CLI with docker.exec. No twin port is ever published.
Scenario operations
operations() returns an Operations with five tables, keyed by name without the product prefix. A scenario uses them as <product>.<name>.
def operations() -> Operations:
return Operations(
overlays={"commit": commit, "issue": issue}, # change the world before the work
deliveries={"issue_assigned": issue_assigned}, # hand the agent its work
checks={"file_not_contains": file_not_contains}, # judge final state
event_checks={"request_made": request_made}, # judge the agent's requests
)
def file_not_contains(run: RunContext, args: dict[str, Any]) -> CheckOutcome:
repo = _repo(run, args["repo"])
found = _content(run, repo, args["path"], args.get("branch", "main"))
if found is None:
return CheckOutcome(True, f"{repo}/{args['path']} no longer exists")
absent = args["text"] not in found[0]
return CheckOutcome(
absent, f"{args['path']} {'no longer contains' if absent else 'still contains'} the text"
)
- Overlays take
(run, args)and change the world. Resolve people withrun.person({...}); record facts other steps can use inrun.facts[args["key"]]. - Deliveries take
(run, args), deliver the work as the sender would (an issue they open and assign, a message they post), and return what was delivered: it is recorded inwork.deliveredand the result. - What a delivery needs:
delivery_needs={"message": lambda args: {"channels": [args["in"]]}}names the grant the agent must hold to see work delivered that way. Grants are exact, so a run checks this before it starts and errors with the grant to add (decision 0020). - Checks take
(run, args)and return aCheckOutcome(passed, evidence). Write evidence a reader can act on: what was found, where, and how many. - Event checks also get the agent's captured exchanges and look at what it did rather than what is left. For a forbidden check,
passedmeans "found"; name the event in the evidence (event 52: GET …) so the report links it.
Checks must be deterministic: no wall-clock time, no randomness, sorted output.
The profile
profile.yaml describes each portal page and API endpoint once. The gateway's rules, the touch ledger, the access map, mirror system show and mirror system try are all generated from it.
product: gitea
surfaces:
- {id: file.read, kind: endpoint,
match: {method: GET, path: "/api/v1/repos/{owner}/{repo}/contents/{path+}"},
resource: "file:{owner}/{repo}/{path}", access: read, sentence: "Reads {path}",
output: {exposes: {each: "", resource: "file:{owner}/{repo}/{path}"}}}
errors:
forbidden: {status: 403, headers: [...], body: '...'} # the product's own error, byte for byte
matchis a method and a path pattern;{name+}matches several segments.resourceandaccess(read, write, admin) say what a request touches. Name repositories asowner/name(repo:,file:,issue:,pull:,branch:) and chat channels aschannel:<name>, so the access map can group them.output.exposeslists what a response carries: a search that returns thirty issues exposes thirty issues. Addaliaswhen the items also have an id the product uses elsewhere (alias: "channel:{id}"next toresource: "channel:{name}"); later requests that name the id are then located, and described, by the name.sentenceis what the live view andmirror watchsay for the request. It may use the path's names,{name}(the resource's name, once known) and{body.field}(a field of the request's JSON body, which also works inresource). When the resource can still be an unknown id, give anunnamedsentence too ("Posts a message" for "Posts in #{name}").kindisendpoint,portal(a page),asset(styles, scripts, pictures) orbackground(a page's live connection). Assets and background requests are drawn as part of the page that loaded them.- Every surface needs an example request in
SHAPESintests/unit/test_behavior.py, with the resource and sentence it should produce; the test fails until it has one. - The adapter inherits
locate(exchange)anddescribe(exchange)fromProfileMethods; there is nothing to write for them. errorsare the product's own answers, copied from the real product, so a rule that refuses a request looks exactly like the product refusing it.
Testing
- Unit tests in
tests/unit/test_twins.pycover the adapter's pure parts: container specs, state objects, profile matching. mirror world createagainst a real engine is the integration test: it builds, seeds, checks for traces, snapshots and verifies the snapshot.- Run a built-in scenario that uses the product with the scripted agents, then read
mirror trace latestandmirror report latest --opento see what the twin recorded. - Record what you measured (image size, memory, seeding time) in
docs/platforms.md, and any tell you found and closed indocs/tells.md.
This page is docs/writing-a-twin-adapter.md in the repository; improve it there.