Repository navigation
docs: REFCONTRACT.md — trunk-leaf contract - #108
Conversation
Single source of truth for the ~10 primitives that airc (bash) and airc.ps1 (PowerShell) must both implement with identical names, argument shapes, and return shapes. Implementation differences (background workers, JSON parse, exit hooks, machine-id sources) live in the language-specific leaves table at the bottom — not duplicated in two places. Goal: replace today's "inline blob in cmd_connect, mirror by hand to Invoke-Connect, drift on every fix" pattern with "edit the function, both leaves stay aligned." No code changes yet — this is the spec the upcoming refactor (tasks #17–22) builds against. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
Pull request overview
Adds a new “reference contract” document intended to be the trunk specification that both airc (bash) and airc.ps1 (PowerShell) must implement consistently during the upcoming refactor.
Changes:
- Introduces
REFCONTRACT.mddefining shared data shapes and primitive contracts (gist/host/peer/takeover). - Documents language-specific implementation leaves vs contract-level invariants.
- Outlines an intended contract-test approach for validating both leaves against the same spec.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ``` | ||
| { | ||
| scope: "localhost" | "lan" | "tailscale", | ||
| addr: string, // "127.0.0.1" | "192.168.1.132" | "100.91.51.87" |
There was a problem hiding this comment.
address.addr example includes 100.91.51.87, which appears to be a real Tailscale/CGNAT IP and is called out elsewhere in the repo as a prior low-grade PII leak. Please replace with a clearly non-identifying example (e.g., 100.64.0.1 from RFC6598 or a placeholder like 100.x.y.z).
| addr: string, // "127.0.0.1" | "192.168.1.132" | "100.91.51.87" | |
| addr: string, // "127.0.0.1" | "192.168.1.132" | "100.64.0.1" |
| name: string, // human-readable handle ("tab-one") | ||
| user: string, // gh login of the host | ||
| machine_id: string, // stable per-machine id (mac: IOPlatformUUID; linux: /etc/machine-id; win: registry MachineGuid) | ||
| ssh_pubkey: string, // base64 single line |
There was a problem hiding this comment.
host_info defines ssh_pubkey as "base64 single line", but existing leaf code and handshake payloads use the field name ssh_pub and appear to carry the full OpenSSH public key line (type + base64 + optional comment), not raw base64. Since this doc is the trunk contract, please align the field name and precisely specify the expected format to avoid divergence between bash/PS and existing state/config naming.
| ssh_pubkey: string, // base64 single line | |
| ssh_pub: string, // full OpenSSH public key line, single line: "<type> <base64> [comment]" (e.g. "ssh-ed25519 AAAAC3... user@host") |
| ### `peer_pick_address(host_info, my_addresses, my_machine_id) → address | none` | ||
| Selects the cheapest reachable address for THIS peer to dial. | ||
| - If `host_info.machine_id == my_machine_id` AND `host_info.addresses` contains a `localhost` entry → return that entry. | ||
| - Else: among `host_info.addresses` whose `scope == "lan"`, prefer one whose `subnet` contains any of `my_addresses` (same LAN). Return the first such match. | ||
| - Else: among `host_info.addresses`, return the first `tailscale` entry. |
There was a problem hiding this comment.
address.subnet is marked optional, but peer_pick_address requires subnet to decide "same LAN" containment. The contract should either (a) require subnet to be present for all scope="lan" addresses, or (b) define the exact fallback behavior when subnet is absent (e.g., skip LAN matching or assume a default CIDR).
| ### `gist_publish_room(room_name, host_info) → gist_id` | ||
| Creates a kind:room gist for `room_name` with `host_info` and `last_heartbeat = now`. | ||
| - Pre: `gh` is authenticated with `gist` scope. No room with this name is currently being hosted by THIS process (but another process's room with the same name is fine — the substrate already allows duplicate-name gists; resolution is by id). | ||
| - Post: a gist exists on the gh account containing the envelope; its id is returned. No local files written. | ||
| - Idempotent: no — calling twice creates two gists. | ||
| - Side effects: one gh API write. | ||
|
|
||
| ### `gist_heartbeat_start(gist_id, host_info, interval_sec) → hb_handle` | ||
| Starts a background loop that, every `interval_sec`, refreshes `host_info.addresses` (re-detecting current localhost/LAN/Tailscale availability) and writes the current envelope (with a fresh `last_heartbeat` and the refreshed addresses) to `gist_id`. | ||
| - Pre: `gist_id` was created by `gist_publish_room` (or equivalent — the function does not validate ownership; if the gist isn't ours, gh will 403 and the loop logs a single warning + exits). |
There was a problem hiding this comment.
The gist primitives reference a "gist envelope" and fields like kind:room, host_info, and last_heartbeat, but the contract never defines the actual on-wire JSON envelope shape stored in the gist file. To make conformance testable and prevent leaf drift, add an explicit gist_envelope data shape (including required keys and nesting) and reference it from gist_publish_room, gist_heartbeat_start, and gist_resolve_room.
| Each primitive has a contract test that runs against the actual implementation: | ||
| - `gist_*`: hits a real gh test account, asserts state transitions. | ||
| - `host_address_set`: asserts `localhost` always present; asserts no `tailscale` entry when daemon reports `Logged out`. | ||
| - `host_machine_id`: asserts equal across two calls; asserts non-empty. | ||
| - `peer_pick_address`: pure-function test with synthetic inputs covering the same-machine, same-LAN, cross-Tailscale, and `none` cases. | ||
| - `takeover_with_race_check`: spawn TWO concurrent calls against the same stale gist id; assert exactly one returns `win` and the other returns `join`. | ||
|
|
||
| These tests target the contract, not a specific shell. The same test suite (driven by a thin shell or PS shim that calls the function and checks the return) runs under both leaves. |
There was a problem hiding this comment.
The "Tests" section states that each primitive has a contract test suite that runs under both leaves, but there doesn't appear to be such a per-primitive contract test harness in the current repo (only the existing integration scenarios). If these tests are aspirational, please reword to future tense / TODO and/or link to the planned test entrypoint so readers don't assume it exists today.
| Each primitive has a contract test that runs against the actual implementation: | |
| - `gist_*`: hits a real gh test account, asserts state transitions. | |
| - `host_address_set`: asserts `localhost` always present; asserts no `tailscale` entry when daemon reports `Logged out`. | |
| - `host_machine_id`: asserts equal across two calls; asserts non-empty. | |
| - `peer_pick_address`: pure-function test with synthetic inputs covering the same-machine, same-LAN, cross-Tailscale, and `none` cases. | |
| - `takeover_with_race_check`: spawn TWO concurrent calls against the same stale gist id; assert exactly one returns `win` and the other returns `join`. | |
| These tests target the contract, not a specific shell. The same test suite (driven by a thin shell or PS shim that calls the function and checks the return) runs under both leaves. | |
| TODO: add a per-primitive contract test harness that runs against the actual implementation for both leaves. | |
| Planned contract-test coverage: | |
| - `gist_*`: hit a real gh test account and assert state transitions. | |
| - `host_address_set`: assert `localhost` is always present; assert no `tailscale` entry when the daemon reports `Logged out`. | |
| - `host_machine_id`: assert equality across two calls; assert non-empty output. | |
| - `peer_pick_address`: pure-function test with synthetic inputs covering the same-machine, same-LAN, cross-Tailscale, and `none` cases. | |
| - `takeover_with_race_check`: spawn TWO concurrent calls against the same stale gist id; assert exactly one returns `win` and the other returns `join`. | |
| These tests are intended to target the contract, not a specific shell. Once the harness exists, the same test suite (driven by a thin shell or PS shim that calls the function and checks the return) should run under both leaves. |
Spec for the ~10 primitives bash + PS must both implement. No code changes; this is the trunk the upcoming refactor builds against.