Skip to content

Co-editing across accounts: the agent serves team spaces live (after P2-2) - #66

Open
Skyfuller1303 wants to merge 245 commits into
itrycodinganditsbad:mainfrom
Skyfuller1303:team-p2-3-agent-live
Open

Skyfuller1303 wants to merge 245 commits into
itrycodinganditsbad:mainfrom
Skyfuller1303:team-p2-3-agent-live

Conversation

@Skyfuller1303

Copy link
Copy Markdown

Merge after #64 (P2-2). The diff of this PR alone: team-p2-2-server-live...team-p2-3-agent-live.

Until now the agent's live link only ever served the personal space, and no shipped agent opened a team space's live connection. This makes a synced team space live: each one gets a link of its own, a document is routed to the right link by its path, a viewer's documents are read-only, the agent follows a role change or a removal without losing a change, and apps can ask who else is there.

This is permission code on the client side. The server decides, but the agent must not send what the server would refuse, and must not lose what it could not send.

What

  • Protocol 3 when the server offers it. The agent says hello {1, 3} on every link. A server that knows protocol 2 at most answers welcome and nothing changes for the personal space; a newer one answers welcome_space (also for the personal space). A team link takes welcome_space for its own space only: a plain welcome, another space's, or a 404 for the upgrade (a server whose live service has no team spaces, or a member who was removed) ends the attempt and the link is asked again only after ten minutes, while the space's files keep syncing and its documents open as plain files. No loop.
  • One live link per synced team space. Each has its own hub (thread, replica, WebSocket with the craft-space header, documents and journals in live/<space>/), started and stopped with the space's sync by the spaces task. The personal link is as it was; a team link never changes the agent's online status.
  • Routing by path. A document is routed by the path the app opens: an absolute path inside a synced team folder goes to that space's hub, anything else to the personal one. One docs connection can have documents of several spaces open; saved, saving and close follow the document. A space that stops syncing meanwhile answers later opens of its files as plain files and leaves the app's other documents alone.
  • Viewers. opened carries read_only (viewer, or an archived space) and the space. An update an app sends to a read-only document is refused with error {read_only}, nothing of it is kept, and nothing that writes (create, update, snapshot) is ever sent on such a link: the replica gates its frames. A document the server does not have yet cannot be made live by a viewer, so it opens as a plain file.
  • Role change and revocation. The role comes from welcome_space and from the agent's list of spaces. error {access_changed} (or the close code 4403) ends the connection and the agent connects again at once, once; the answer is the new role or a 404. A removed account's link stops with the space's sync. Unacked updates stay in the document's journal through all of it: with the editor role back they go up on the first connection; as a viewer they stay, the log says how many (space ID only), and the document opens with them in waiting. Raising the role reconnects at once instead of waiting for the next ping.
  • Identity. An app that opens with identity: true gets other accounts' presence as the new binary kind 5 ([5][id][16 bytes account ID][bytes], the server's stamp) and a members {id, members: [{user, name, colour, self}]} message after opened and whenever the list changes (active members from GET /spaces/{id}/members, refreshed when presence names someone unknown; in a personal space the account alone). colour is the palette index of craft-cloud-api. An app that does not ask gets kind 3 only and no names. Debug output of the new types, the hub's config and the log carry space and account IDs, never names.
  • Blobs. PUT /v1/blobs?for=<path> and GET /v1/blobs/{hash}?for=<path>: the blob belongs to the space of the file at that path, goes up through a transport bound to the space, and every member's agent reads it. A path in no synced folder is 400, a put for a read-only space 403 read_only. Without for, as before.
  • Recents per space. subscribe {app, space?} follows a team space's list; an add with an absolute path inside a team folder goes to that space's list (relative to the folder). The personal list is untouched.
  • Review fixes. Lifting a read-only lock on a link that is up sends what the gate held back (no new connection needed), and a look at the list asked for during another look follows it. A space's live folder is held by one hub at a time (.lock), and the next hub starts after the old one has ended, so a turn-off and turn-on or a folder move cannot overwrite a journal. Documents open in a space that stops syncing are told (status stopped) and their later changes get not_found. A viewer's recents add is refused, refused opens free their numbers, role changes reach the hub through a watch channel, access_changed backs off further each time, and a blob's path is normalised first.
  • Ctrl-C. With an idle personal space, one Ctrl-C used to wait inside spaces.stop for a team pass that hangs, and the second Ctrl-C was never read. The loop now keeps reading signals during that wait and a second one ends it.
  • Client crate and docs. craft-cloud-app: OpenOptions::identity, Document::read_only and space, Event::PresenceFrom and Event::Members, Client::watch_recents_in, Blobs::put_for and get_for. The kit passes identity: false for now. docs/agent.md and docs/design/live.md describe the local API and docs-socket changes.

Tests

  • Convergence simulation of documents with two accounts and a viewer (tests/documents_team_sim.rs, the documents simulation on a team space): the owner and an editor type, a viewer tries to, frames are delivered in random order, connections drop and machines crash, the owner lowers the editor to viewer and back and removes and invites him again; then everything is delivered. Convergence on all three machines, nothing taken is lost (also what waited in a journal through a lowered role or a removal), nothing waits, nothing typed as a viewer is in the document, and the server never has to refuse a write.
  • Real server over loopback, two agents (tests/team_spaces.rs): live documents across two accounts with a change both ways; an app that asks for identity hears the sender's account and the member list (names, colours, itself marked) while an app that does not ask gets kind 3 only; a viewer's document is read_only, its update refused and neither kept nor sent, its blob put refused; a role lowered and raised while connected; a change waiting in a journal survives a removal and goes up once the account may edit again (and is not sent while it is a viewer); two spaces at once routed by path; a blob put with for lands in the team space and the other member's agent reads it (and the personal space does not have it); recents of a team space reach the other member and no personal list; a server that cannot serve the link is asked once; one Ctrl-C with a hung team pass waits and the second ends it.
  • Unit tests: the hello and both welcomes, a team link refusing a plain welcome or another space's, the gate for a viewer, access_changed and a late read_only, the new messages on the wire (and the debug output of members showing no name).
  • The existing documents simulations, the agent's other tests and tests/api.rs pass on live protocol 3 (the agent's presence is now presence_from).

Checks

On Windows, through a memory-limited runner:

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets --locked -- -D warnings
  • cargo test -p craft-cloud-proto -p craft-cloud-api -p craft-cloud-meta -p craft-cloud-server -p craft-cloud-agent -p craft-cloud-sync -p craft-cloud-app --locked --no-fail-fast against Postgres 17 and SeaweedFS 4.48, with CRAFT_CLOUD_REQUIRE_BACKENDS=1
  • cargo +1.95 check --workspace --all-targets --locked (MSRV)

All passed (on SQLite, Postgres and S3). Mutation checks: with the agent's read-only guard and frame gate removed, the viewer scenario fails ("timed out waiting until the agent refuses the change"); with the second Ctrl-C not read, the Ctrl-C scenario fails (the wait is never ended); with the unlock not resending, the folder not exclusive, or the stopped status and error removed, the matching tests of the review round fail.

Latest check of this head

Checked on Windows with Postgres 17, SeaweedFS 4.48 (S3) and CRAFT_CLOUD_REQUIRE_BACKENDS=1 under a 16 GiB memory limit at 91404c4: cargo fmt --all -- --check and cargo clippy --workspace --all-targets --locked -- -D warnings pass; cargo test --workspace --locked --no-fail-fast: 1994 passed, 0 failed, 2 ignored.

Not in this PR

  • The kit: session.people(), asking for identity itself, session.blobs() bound to the session's path, and mapping read_only to the read-only open it already has (P2-4). Until then the kit retries a change the agent refused with read_only as it does any refusal that is not invalid or too_large.
  • opened.read_only is as of the open; it is not sent again when the role changes while the document is open.
  • A way to see the changes a viewer-or-removed account kept, other than a document's waiting count and the log line.

Skyfuller1303 and others added 30 commits November 3, 2026 22:55
`craft-cloud-server healthcheck [--addr 127.0.0.1:8080]` sends GET /api/info
over a plain TcpStream and exits 0 only on a 200 status line, within 3 s.
Otherwise it exits 1 with one line on stderr. It prints nothing on stdout and
adds no dependency. A container image without a shell or curl can use it as
its health check.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ment

packaging/docker holds a Dockerfile (Debian trixie build, distroless cc
runtime as uid 65532, base images pinned by digest) and a Compose deployment:
the server behind Caddy for TLS (acme, internal or custom certificates), with
SQLite and the local disk by default and overlays for Postgres and S3.

Secrets never reach a service's environment. A one-shot secrets-init service
renders config.toml from a template and writes the secret files into a volume;
every other service mounts only its own directory read-only. Every service has
a read-only root, no capabilities and no-new-privileges. Caddy has a fixed
address that the config lists in trusted_proxies.

install.sh writes .env with random passwords, builds the image, starts the
stack and creates the first admin from a password read on stdin. The Docker
section in docs/server.md covers use, TLS, Postgres, S3, upgrading and what is
not covered. The Dockerfile uses the healthcheck subcommand.

Derived from the deployment in Skyfuller1303/craftcloud (deploy/compose),
written by the same author, MIT OR Apache-2.0, adapted to this repository's
TOML-only configuration.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Migration 0007 gives a device its client kind, platform and app version
and, once revoked, the time and the reason, and gives a user the time of
the last password change (plus a disabled time nothing reads yet). All
of it is additive, BIGINT on Postgres, and the flag stays the check.

Meta gets create_device_with and redeem_with, rename_device,
revoke_device_as, revoke_other_devices, revoke_all_devices, account,
change_password and reset_password. Flag, time and reason are set
together and only by the first revoke. A password change or reset sets
the hash, revokes the devices and deletes the account's invite and
pairings in one transaction.

Tests run on both databases, including the migration from 0006.

Co-Authored-By: Claude <noreply@anthropic.com>
New: GET /account, POST /account/password, PATCH /devices/{id} and
POST /devices/revoke-others. Sign-in and join take an optional client
kind, platform and app version, checked before a try is counted. The
device list shows them with the time and reason of a revoke; all new
fields are optional on the wire.

Changing the password answers a wrong current password with 403
wrong_password, never 401, since the agent reads every 401 on a
signed-in route as a revoked device. It is limited to five tries per
account in 15 minutes, takes a password-check slot, and ends the revoked
devices' live connections at once. Password and name bodies are read up
to the new TINY_JSON_BYTES (4 KiB).

wrong_password and forbidden move into the shared error codes; the local
API refers to them. The agent, app and desktop only change where they
build the extended types.

Co-Authored-By: Claude <noreply@anthropic.com>
user reset-password now revokes every device of the account (reason
reset) and deletes its open invite and pairings, so a stolen token does
not outlive the old password. --keep-devices restores the old behaviour
for the devices. device revoke-all signs out an account's devices
without touching the password. Both print how many devices they ended;
device revoke records the admin as the reason.

Co-Authored-By: Claude <noreply@anthropic.com>
The API table and error codes in the design document, the password and
sign-out commands, the live-connection delay of the command line and the
rollout note (an older server refuses the newer database) in the
server guide.

Co-Authored-By: Claude <noreply@anthropic.com>
…evice

A /changes long-poll that was waiting when its device was signed out
through the API now ends at once, and one that wakes for any other reason
looks the device up again before it answers, so a device signed out by
the command line gets 401 instead of the next change set.

The live endpoint listens for revocations and looks the device up before
the upgrade, so a revoke between the sign-in check and the subscription
is not missed.

Co-Authored-By: Claude <noreply@anthropic.com>
change_password checks, inside its transaction, that the calling device
is still signed in (it may have been revoked while the password was
hashed) and answers 401 if not. A password that changed meanwhile is now
409 conflict instead of 400 invalid. Both are new MetaError variants
mapped in the server.

set_password_hash had no caller outside a test and is removed. A new test
breaks the pairing deletes with a trigger and checks that a password
change, an admin reset and a revoke-others undo everything on SQLite and
Postgres.

Co-Authored-By: Claude <noreply@anthropic.com>
The request used bare line feeds instead of CRLF, and an answer that is not
HTTP at all was reported as a bad status. Send CRLF line endings and report
"not an HTTP answer" unless the first word is an HTTP version.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The long-poll now listens for revocations and looks its device up before
it waits, so a revoke between the sign-in check and the wait is not
missed, and looks it up once more after the change set is fetched, as
the last step before the answer, so a revoke during the fetch is not
answered with changes.

Co-Authored-By: Claude <noreply@anthropic.com>
…changes a password

change_password locked the caller's device row and then the user's row,
while revoke_other_devices from another device locked its own device and
then the user's, and reset_password locked the user before the devices.
On Postgres two of them at once could deadlock and fail with a database
error. All of them now take the user's row first (a no-op update), then
the devices, then the pairings.

A new scenario runs password changes, sign-outs and an admin reset at the
same moment, over and over, on both databases and expects only successes,
conflicts and signed-out callers. The test trigger helper also drops its
Postgres function.

Co-Authored-By: Claude <noreply@anthropic.com>
An answer that stopped inside the status line ("HTTP/1.1 200" and then EOF) or
filled the 64 byte buffer without a line end counted as healthy or as a bad
status. Now only a status line that ends in CRLF is read, and the other cases
are errors. Tests cover a truncated line, a line that does not end and a bare
line feed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
When the server is down, Caddy's error log entry for a failed request carried
the full request URI and headers, so an invite code (/join/<code>), a query
or a bearer token could end up in the log. The default log now drops
request>uri and request>headers. Caddy also skips installing its internal CA
into the container, which only produced an error line.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
db-init exported PGPASSWORD, which put the superuser password in the
environment of the shell and of psql. It now writes a mode 0600 password file
on a tmpfs, passes only PGPASSFILE and removes the file on exit. The
db-init service gets a small noexec tmpfs for it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…docs

The server started before Caddy and was given 172.29.215.2, the address
reserved for Caddy, so Caddy failed to start. The frontend network now hands
out dynamic addresses from CC_FRONTEND_POOL (the upper half by default). The
docs name the new setting, the error of a data directory with the wrong owner
and what was tried with S3.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
render-config.sh read only the first line of a secret file, so a database
password with a second line passed the check while db-init, which reads the
whole file, set a different password than the one in config.toml. The whole
file is read now, one trailing line feed is allowed and any other line feed is
refused. The tests cover a second line, an empty second line and the allowed
trailing line feed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Compose gives a container its secrets from .env when it is created. After
`docker compose run --rm secrets-init`, a later `up -d db-init` started the
original secrets-init container again, which wrote the old values back. The
docs and the script comment now say to recreate it (`up -d --force-recreate
secrets-init`) before restarting the services that read the files.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
…-in adds a device

redeem consumed the invite and then updated the user, while a password
reset locks the user and then deletes the invite, so the two could
deadlock on Postgres. redeem now finds the invite or pairing with a plain
read, locks the account, and consumes the code again under the lock; a
code that is gone by then is invalid_code. create_pairing locks the
account first too.

A password sign-in verified the password outside any transaction, so a
reset or change that committed meanwhile could be followed by a new
device. sign_in_device locks the account and compares the stored hash
again before it creates the device; if it differs, login answers as for
a wrong password.

New scenarios run redeems, a sign-in, a pairing and a reset at the same
moment on both databases and expect no database error and no live device
once the reset is done.

Co-Authored-By: Claude <noreply@anthropic.com>
The invite delete and the pairing update now name the account the plain
read found, so a code hash removed and made again for another account
between that read and the lock is refused as invalid_code instead of
adding a device to the wrong account.

Co-Authored-By: Claude <noreply@anthropic.com>
The admin API's server overview shows whether blobs are on disk, in S3 or in memory. A backend that does not say is reported as other, so nothing outside the store crate has to change.

Co-Authored-By: Claude <noreply@anthropic.com>
New request and answer types for the routes under /api/v1/admin, with a redacting Debug for everything that holds a password or a code, and the error codes account_disabled and last_admin. The confirmation is a body with an optional password today, so an optional totp_code can join it later without breaking a client.

Co-Authored-By: Claude <noreply@anthropic.com>
Migration 0008 adds the purpose of an invite (invite or reset), a quota per space and a removal mark per user. The new Meta methods for the admin API each re-check the acting admin inside their own transaction and take the locks in one order, with a transaction-wide lock before the last-admin count. Disabled accounts are refused by the token check, sign-in, redeem, pairing and device creation, decided under the account's row lock. A reset code always sets a new password, signs every device out and then signs the redeeming computer in. A space's quota replaces the server-wide cap in the preflight check and in the count that follows.

Co-Authored-By: Claude <noreply@anthropic.com>
The require_admin layer and the routes under /api/v1/admin: counters, a paged and searchable user list, creating accounts, changing role and quota, disabling, enabling and removing, invites, password reset codes and a user's devices. Every change asks for the acting admin's password in its body. Sign-in and join answer account_disabled for a disabled account, and a redeemed reset code ends the live connections of the devices it signed out. Documents the API, the confirmation and the rollout in the design document and the server guide.

Co-Authored-By: Claude <noreply@anthropic.com>
Limiter::count added the attempt before comparing it with the maximum, so tries refused at the limit still raised the count. A refund of an attempt that had been accepted at the same moment then left the key above its limit with fewer wrong passwords than allowed. A refused try now leaves the count alone.

Co-Authored-By: Claude <noreply@anthropic.com>
Disabling and enabling an account leaves the password hash as it was, so a sign-in that verified the password before the disable could still create a usable device after the enable. Users get an auth_gen counter (migration 0008) that a disable, an enable, a removal and every password change or reset bump. The sign-in reads it with the hash and sign_in_device compares both under the account's lock. The server's refunds test covers the limiter change.

Co-Authored-By: Claude <noreply@anthropic.com>
Compaction asks a replica for a snapshot of a live document at an older
version. Loro's SnapshotAt export checks the document out at that version,
and on a long text history that needs many GiB: the live_documents test
an_app_that_misbehaves_is_refused_or_dropped_and_the_rest_go_on, whose app
sends 60 updates of 200,000 characters, ran out of a 16 GiB limit (itrycodinganditsbad#15).

snapshot_at now exports the operations up to the version, imports them into
a fresh document and takes that document's snapshot: the same history and
state, with memory linear in the history. A version whose operations need
others past it is refused instead of silently widened, a shallow document
is refused at once (Loro can't fork one at a version), and a scratch
document that Loro panicked in is let go of without being dropped.

Co-Authored-By: Claude <noreply@anthropic.com>
The typed details of a security event, an event, a page of them and the last line of the NDJSON export. Every
field of the details is a closed list of words or a number, and fields a later release adds are ignored.

Co-Authored-By: Claude <noreply@anthropic.com>
Migration 0009 adds audit_events, with no foreign keys so that an event outlives its account. Every change that the
log names writes its event in its own transaction before the commit, so a change whose event cannot be written is
not made. A change that changes nothing writes none. A failed sign-in is one row per account and minute whose
counter goes up, written in a transaction of its own. The values of an event are typed and come from closed
lists, never from what a client typed. Old events are pruned in batches outside those transactions.

Meta methods that had no way to say who asked get one: revoke_device_by takes the origin, and admin_enable_user and
admin_revoke_invite take the time.

Co-Authored-By: Claude <noreply@anthropic.com>
GET /account/activity, GET /admin/audit and the streamed GET /admin/audit/export; a failed sign-in is written
after the answer is decided and never changes it. [audit] retention_days (1 to 3650, default 365) and
audit list and audit prune on the command line. The export reads fixed pages through a bounded channel up to a
fixed end of the log and ends with a line that says complete or truncated, or breaks the connection.

Co-Authored-By: Claude <noreply@anthropic.com>
…syncs until it is

Raising a space's generation (on access lost, on a replaced folder, on a turn-off)
changed it in memory first and saved account.json after. When the save failed,
which was only logged for a lost space, a restart before any later save found the
old generation on disk and trusted a state that was meant to be dropped if the
removal of that state had failed too.

A raise that could not be saved is now remembered and saved again at every look at
the list, and no space is started until it is saved; running syncs are not
stopped, their state is valid. The module doc and docs/agent.md say so, and the
docs also describe the folder check before deletions and the folder ID.
…very commit

When the folder check stopped a pass, the session stayed open with an engine
state that had taken in part of the other folder (a server file written into it,
deletions noted after its scan), and the pass's end saved that state under the
original folder's identity. If the link was pointed back before the next pass,
the check passed again and the file the first folder never received was taken
for deleted and removed on the server.

The check now poisons the session when it trips, and the end of a pass compares
the folder once more and saves nothing if it moved; the pass thread then drops
the session, removes the state and raises the generation. The engine also asks
the same check before every commit of a push, not only before one with
deletions, so no batch of any kind is sent after a retarget (Engine::on_guard).
A folder restored from a backup at the same path got the old state back whenever
the file system gave it the same ID, which ext4 does when it reuses an inode
number. The folder's .craft-cloud now carries a nonce that is rewritten with a
new random value at every checkpoint and at the end of every pass, and the state
saved with it records the same value. A state is used only for a folder that
carries the nonce it recorded; a folder without one suits a state without one
only, so states from before nonces are refused by a folder that has one, as with
the folder ID. A crash between writing the nonce and the state costs a first-sync
merge, which deletes nothing.
…nd say so

When account.json could not be written after a generation was raised, no space
was started until the save worked, and the only trace was a line in the run log.
Only the affected spaces are held back now; other spaces start and sync as usual.
A held-back space shows as stopped in its state, with the reason in words, in the
local API's list and in the status.
The test expected 36 choices, but the page offers six groups and every name of the closed list in craft_cloud_app::AUDIT_ACTIONS, now 44. It counts them from the list, so adding an action no longer breaks it.
With links not followed, inotify watches a link given as the synced folder and not the folder behind it, so a local change
in it raised no event and waited for the next periodic pass. The folder the link leads to is watched instead.
The watcher and the folder's roots were made once, when the space started. When a pass found that the folder leads somewhere
else (a link pointed at another folder), only the generation was raised, so changes in the new folder raised no event. The
space now makes its watcher and roots again on that event, under a new watcher generation.
List with search and paging, create, and a page for each space with its members (read only), limit, archive and unarchive, and naming an owner. Every change is CSRF-protected and asks for the administrator's confirmation, acts as the web session's administrator and wakes the space's long-polls.
The administrator's list of team spaces counted accepted owner rows without looking at the account, so a space whose only owner was disabled or marked for removal still showed one owner although nobody could manage it. The count now leaves those accounts out, and one query reads the owners of a page of spaces with the state of each account.
…s the owner before the confirmation

The page warns when a space has no owner who can sign in and shows the state of each owner's account; the list reads its owners with one query. A mistyped owner name is refused before the administrator's code is used up, as on the accounts page. The long-poll test now covers the limit, archive, unarchive and owner changes.
…olour and the palette move to craft-cloud-api

Live protocol 3 appends two server frames above the marker line: WelcomeSpace (the space and the caller's role, instead of Welcome) and PresenceFrom (the sending account, instead of Presence). Golden tests pin the bytes of every protocol 2 frame, the new frames round-trip, and versions 2 and 3 negotiate both ways.

The server still answers at most protocol 2 and the agent still asks for protocol 2 only, until the server serves team spaces. member_colour moves with a 12-colour palette (light and dark values) into craft-cloud-api, which gains the blake3 dependency.
…e_bytes), and live reset-doc takes --space

The live bytes of a space were capped by live_bytes_per_user whatever its kind. A team space now has its own cap, 4 GiB by default, set by the new optional [spaces] section (Meta::with_team_live_bytes_cap); a personal space keeps live_bytes_per_user. live reset-doc takes --space (an ID or a team space's name) for a team space's document; the user must be a member.
The space of a live connection comes from the upgrade's craft-space header (a non-member is refused with 404 before it). A team space is served on protocol 3 only: the server answers welcome_space with the caller's role, and a hello that negotiates 2 is protocol_mismatch.

The role is checked for every frame against the database: create, update and snapshot need an editor (a viewer gets error read_only and keeps the connection), subscribe a viewer. Viewers are never asked to compact. Presence is relayed as presence_from with the account of the sender's device, whatever the bytes say. A connection ends with error access_changed and close code 4403 when its account is no longer a member or its role changed: at once in this process (the members counter of the notifier, and revoke_user for a disabled or removed account), at the next ping for a change made elsewhere. The hub keeps a registry of connections: 4 per device and space, 32 per device.

Tests run over a real WebSocket on SQLite and Postgres: two accounts converging on one document, the viewer rules, stamped presence, revocation in process and from outside, the connection limits, the team cap, and a protocol 2 agent seeing byte-identical frames.
…ess changes, so a disabled account is still told it was signed out
…k at their account again before relaying and accepting presence

Review of P2-1 and P2-2:
- M1: append, create, create_document and the answer to compact run under Meta::guarded (an editor at least) and check the guard inside their transaction; a role lowered, a member removed or a space archived between the frame's check and its commit stores and announces nothing.
- M2: the in-process revocation scenarios run with a 30 s ping, so they show the members signal and not the ping.
- L1: a connection trusts its last look at its account for 5 s (Limits::live_recheck) and looks again before it relays updates or presence or accepts presence.
- L2: a write also confirms the device within that time.
- L3: a personal space's connection does not watch the members counter; the notifier forgets counters nobody watches.
- L4: a change of a space's members makes a connection look at once, then at most once a second; it reads the device and the access, not the epoch.
- L5: the disabled-account test pins the exact frame.
- L6: 256 followed documents per device and space, 1024 per device.
- I1: reset-doc --space writes a cli.live_reset event and refuses an archived space without --force. I2: dead check removed.
The per-user revocation channel is gone: a disabled account's devices are revoked with it, which ends its connections already.
…ow readers can be interrupted; a snapshot that loses the race ends with access_changed

A look at the account's access or device that fails (the database cannot be
read) used to count as "nothing changed" and renewed the time of trust. It now
confirms nothing: past live_recheck the connection relays no update or
presence and accepts no presence, the look is repeated every second, and after
three failed looks (or 15 s) the connection ends with an internal error and a
plain close, not access_changed.

A catch-up page is sent frame by frame while the connection listens to the
members signal, the device revocation and shutdown, reads the access again
between frames once live_recheck has passed, and must be out within two
minutes.

An answer to compact that finds the access gone inside its guarded transaction
is answered access_changed, and the connection ends with close code 4403.
… look fails withdraws the trust; tests for pages and presence while unconfirmed

A write or subscribe reads only the access (the device comes from the cache), but
used to renew the time that relaying trusts both. A connection now trusts the older
of its device check and its access check, so a device signed out by another
process stops receiving live_recheck after its last real look at it.

A look asked for by a signal (the members changed, a missed revocation) that cannot
be made now withdraws the remaining trust at once, so the connection relays and
accepts nothing until a look works.

A page that is being sent ends on the first look that cannot be confirmed; the
documentation says so. New tests: a page is not sent on while unconfirmed, presence
is not relayed to an unconfirmed connection (both backends), and the patience clause.
Every synced team space gets a live link of its own (hub, replica, WebSocket with the
craft-space header, documents and journals in live/<space>/); the agent says hello 1 to 3
and takes welcome_space for the space only. Documents are routed by the path an app opens.
A viewer's documents are read-only: opened says so, an update is refused with read_only and
nothing that writes is sent. access_changed ends the link and the next connection learns the
new role; unacked updates stay in the journals and go up when the account may write again.
Apps that open with identity get presence with the sender (binary kind 5) and a members
message; others get kind 3 only. Blobs (?for=) and recents (space) belong to the space of
the file. A second Ctrl-C is read while the first waits for a hung team pass.
Lifting a read-only lock on a link that is up sends the updates the gate held back; a look at the
list asked for during another look follows it. A space's live folder is held by one hub at a
time: the next hub starts after the old one has ended. Apps with documents open in a space that
stops syncing are told, and their later changes are answered with an error. A viewer's recents
add is refused, a recents subscription never falls back to the personal list, refused opens free
their numbers, role changes reach the hub through a watch channel, access_changed backs off
further each time, and a blob's path is normalised before its space is chosen.
… them

An app that sent its updates, closed the document and shut its socket down at once could lose its last changes.
Two things dropped them:

- The hub's `tell` removed the whole connection (its handles) as soon as the connection's output was closed or
  full. Changes the connection had queued before were then refused for having no handle. `tell` now only stops
  sending to that connection (its sender is dropped, so the socket ends); the handles stay until the connection's
  own `DocsCall::Gone`, which comes in order after everything it queued. The docs socket now always sends `Gone`
  when it ends.
- The docs socket stopped at the first failed write to the app, without reading the frames the app had sent
  before. A write that fails because the app is gone now only mutes the connection: it reads and forwards what the
  app sent, until its close or EOF, for at most five seconds. An app too slow to take a message is still dropped
  at once, as before.

Tests drive a real agent: an app sends several updates, closes the document and drops the socket; and one whose
writes fail (a hidden test hook) while its updates are still arriving. Every update must reach another machine.
…taking messages

When the hub found that a docs connection no longer takes messages, an import it had asked of that connection stayed
asked: the other opens waiting for the same document were only asked once the connection's `Gone` came, and with
the read-on window after a failed write that can be seconds. `tell` now asks the next open waiting that can still
be told, at once, and `ask_import` skips connections that take no messages. A connection that stops reading drops
its receiver of the hub's output, so the hub finds out right away.

The final WebSocket close of a connection that was dropped for being too slow is sent under the send timeout. A
receive error ends the connection, muted or not; that limit is noted in a comment.

A test has two apps wait for the import of one file while the first stops taking messages (hooks for one app's
writes and for the read-on window).
…nection is dropped

A connection closed while data of the agent's is still unread is reset instead of closed, and a reset can take
with it frames the agent has received but not read yet, which on Linux were the last changes before the app quit.
Dropping the connection now sends the close, waits up to a second for the agent's, and only then shuts the
stream down.
…one test goes through the app library

The test that dropped its socket with the agent's answers unread failed on Linux CI now and then: the socket was
reset, and the reset took frames with it that the agent had received but not read. That is a property of TCP, not
something the agent can repair, so the test now ends the way an app that reads its answers does. A new test quits
through craft-cloud-app, whose drop does the same.
…fuses what comes after

The hub's stop used to tell its documents they were stopped, then close the connection and flush while its request queue
stayed open. A change an app sent in that time was accepted into the queue and dropped when the hub ended: neither kept nor
refused. Now the connection is closed first, the queue is closed (a call fails with Gone, which the docs socket answers with
not_found), what was queued is handled as ever (a change is journaled and acked, a save recorded), and only then are the open
documents told the space stopped.
… join

An open that waited in the hub's lookups (for its file's node) or in its waiting list (for its document's join) got no answer
when the hub stopped: only documents already open were told. The personal hub's docs socket goes on after the hub stops, so
nothing else ever answered, and the app library waited for its own 30 second timeout. The stop now answers each of them as
not live (the file opens as a plain file) and forgets them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant