Repository navigation
Description
Part of #1503.
Scope
An operator sets devices.autoEnroll: true on a worker and restarts it. From then on, every phone plugged in by USB that passes device add's checks is enrolled within one interval, with no command. A phone that can't be enrolled yet is retried and logged. A phone removed while plugged in stays out until it is replugged. device.enrolled says who enrolled it. The website documents all of it.
In short
A new worker part, the auto-enroller, runs every health.probeIntervalMs (30 s by default, counted from the end of its previous run). It asks each started driver for the phones on USB; a platform with no started driver is skipped and logs nothing. It enrols each one that has no live record and is not held out, through device add's own enrolment. A held-out phone is one whose record went away while it stayed plugged in. An attachment starts at the first run that lists the ID. During it, the ID is held out once any of these is true: a run saw it with a live record; the auto-enroller enrolled it; or it has a deleted record deleted after the attachment started. That last case covers an operator's device add and device remove between two runs. The hold-out is forgotten after health.stableObservations runs (2 by default) in a row without the ID on USB. A run whose list throws counts as none of them.
flowchart LR
T["every interval (new)"] --> L["driver.listAttachedPhysical()"]
L --> K{"live record, or held out?"}
K -- yes --> S[skip]
K -- no --> A["PhysicalEnrollment.add, auto (changed)"]
A -- refused --> G["log once per reason (new)"]
A -- ok --> E["device.enrolled initiator auto (changed)"]
The log line, word for word, with <platform>, <id> and <message> filled in: auto-enroll: <platform> <id> not enrolled: <message>. <message> is the refusal's own message, the one device add prints. It is written once each time an ID's reason changes: locked → untrusted → locked writes three lines, and three runs at locked write one. A PhysicalInspectionError counts as reason unreadable, with its own message. A platform whose list throws logs auto-enroll: could not list <platform> phones: <error message>, once each time the error message changes. A list that succeeds resets this, so the next failure logs again.
Before and after, with devices.autoEnroll: true:
| Phone | Before | After |
|---|---|---|
| USB, passes the checks, no record | not enrolled | enrolled, device.enrolled initiator: "auto" |
USB, locked |
not enrolled | one log line; enrolled once unlocked |
| enrolled, any state | watcher's | watcher's |
| removed, still plugged in | not enrolled | not enrolled until missing 2 runs (about a minute), then back |
| removed, unplugged and back within one run | not enrolled | still held out |
If this goes wrong, a user sees a phone they removed come back, a phone never enrolled, or its kept apps changed.
Technical spec
Facts
Commit e4adf46 (origin/main).
| # | Fact | Source |
|---|---|---|
| F1 | PhysicalEnrollment.add(platform, id) inspects, then under decisions.run enrols a new ID or re-enrols an existing live record (reenrolled: true, with an enrollment claim). It refuses a virtual device's ID, and refuses an ID enrolled on the other platform. |
src/core/physical-enrollment.ts:75-119, :133-145 |
| F2 | A live record is one whose state is not deleted. |
src/core/physical-enrollment.ts:179-184 |
| F3 | registry.enrollPhysical writes the record ready and emits device.enrolled with appCount, class, deviceId, driverDeviceId, model, osVersion, platform, reenrolled. |
src/core/registry.ts:666-688, :762-775 |
| F4 | device.enrolled's payload type has no initiator; device.deleted has one. |
src/bus/index.ts:182-197 |
| F5 | unenrollPhysical moves a record to deleted and emits device.deleted initiator: "operator". Out of quarantine retries, a physical device is unenrolled too. |
src/core/registry.ts:733-760; src/core/AGENTS.md "Out of retries … a physical one unenrolled" |
| F6 | Core builds PhysicalEnrollment in createCore, and holds notifyAvailability, the port that wakes the queue. |
src/core/create-core.ts:295-301, :179 |
| F7 | The daemon wraps core.enrollment so device add wakes the queue after it enrols. |
src/daemon/worker-composition.ts:564, src/daemon/enrollment-wake.ts:9-21 |
| F8 | The physical-device watcher is started after convergence and disposed on stop. | src/daemon/server.ts:553, :670 |
| F9 | The watcher arms one timer at health.probeIntervalMs and re-arms when its run ends. A failed run is logged and the timer goes on. |
src/leasing/physical-device-watcher.ts (#arm, #tick, lines 97-99) |
| F10 | health.probeIntervalMs defaults to 30 000 and health.stableObservations to 2. |
src/core/config.ts:894-895 |
| F11 | The devices section has one key, deletedRetentionMs. It is validated by objectValidator; the contract schema is z.object({ deletedRetentionMs: z.number() }). |
src/core/config.ts:170-178, :855, :1010; src/contract/config-schemas.ts:99 |
| F12 | Every file under src/contract but tests, index.ts and the listed helpers counts as a shape: a change bumps PROTOCOL_VERSION_RANGE. |
src/contract/protocol.ts (PROTOCOL_VERSION_RANGE); .agents/scripts/protocol-bump-check.mjs (NOT_SHAPES) |
| F13 | config-reference.test.ts compares the config keys with the website's configuration reference. |
src/core/config-reference.test.ts |
| F14 | e2e tests run a daemon with the fake driver, scripted physical devices (present, absentReason, refusal) and config overrides such as health.probeIntervalMs: 200. Its log is daemon.log in the data directory. |
e2e/fake-driver/types.ts:47-57, e2e/physical-devices-core.test.ts:183, e2e/helpers/env.ts:266 |
| F15 | Driver.listAttachedPhysical() answers the canonical IDs on USB and throws when the platform can't be read. |
#1507 (feature Shared values) |
| F16 | A gateway refuses device.add; only a worker keeps device records. |
src/gateway/dispatcher.ts:281-287, src/core/config.ts:526 |
| F17 | booleanValue refuses a non-boolean with Invalid config value for "<path>": expected a boolean. |
src/core/validation.ts:96-98, :17-18 |
Modules touched
Searches: enrollPhysical, device.enrolled (src/, docs/, website/, e2e/), deletedRetentionMs (to find every place a devices key is listed), physicalDeviceWatcher (start and stop wiring).
flowchart LR
S["daemon/server.ts (changed: start, dispose)"] --> AE["core/physical-auto-enrollment.ts (new)"]
WC["daemon/worker-composition.ts (changed)"] --> AE
AE --> PE["core/physical-enrollment.ts (changed)"]
AE --> DR["drivers: listAttachedPhysical"]
PE --> R["core/registry.ts (changed: initiator)"]
R --> B["bus/index.ts (changed: payload)"]
CF["core/config.ts + contract/config-schemas.ts (changed)"]
src/core/physical-auto-enrollment.ts(new):PhysicalAutoEnrollmentwithstart(),settle()anddispose(). After each enrolment it callsnotifyAvailability(). It lists only the platforms whose driver started (drivers.getthrowsNoDriverErrorotherwise,src/core/driver-catalog.ts:68-71). It takes the drivers, the registry snapshot,PhysicalEnrollment,notifyAvailability, a clock, a logger,health.probeIntervalMs,health.stableObservationsanddevices.autoEnroll. Off,start()arms nothing.src/core/create-core.ts,src/core/index.ts: build it, expose it on the core handle.src/core/physical-enrollment.ts:add(platform, id, { initiator }),initiator"operator"by default. With"auto", an ID that has a live record insidedecisions.runis left alone and answers "skipped": no write, no event.src/core/registry.ts:enrollPhysicalandreenrollPhysicaltake the initiator and put it ondevice.enrolled.src/bus/index.ts:initiator: "operator" | "auto"ondevice.enrolled.src/core/config.ts,src/contract/config-schemas.ts,src/contract/protocol.ts:devices.autoEnroll, boolean, defaultfalse; protocol + 1.src/daemon/server.ts,src/daemon/worker-composition.ts: start it right after the watcher, settle it in the worker'ssettleand dispose it with the watcher.e2e/fake-driver/: script a list failure per platform, and change a phone's attachment mid-test (present).- Docs:
docs/internal/EVENTS.md(thedevice.enrolledrow),docs/internal/agent-rules/safety.mdrules 1 and 8,src/core/AGENTS.md(the auto-enroller's rules),website/src/content/docs/docs/reference/configuration.mdx(the key, the warning about personal phones),website/src/content/docs/docs/worker/enrolling-physical-devices.mdx(a section on auto-enroll, held out, the restart),website/src/content/docs/docs/using-devices/physical-devices.mdx:20,website/src/content/docs/docs/reference/events.mdx(initiator).
Contract and event changes
- Config shape:
devices.autoEnroll: z.boolean(). This PR bumps the protocol by one. A non-boolean value inconfig.jsonstops the start:Invalid config value for "devices.autoEnroll": expected a boolean(F17). device.enrolledgainsinitiator: "operator" | "auto". This is additive (the bus rule "change them additively only"). Both EVENTS.md andevents.mdxget it.
Other code on the same state
Searches: writers of physical records (enrollPhysical, reenrollPhysical, unenrollPhysical: 3 in registry.ts, called from physical-enrollment.ts and the quarantine coordinator), and readers of the USB list (listPresentPhysical: the watcher, grant, startup).
| Component | Both act | Wins |
|---|---|---|
Operator device add |
Both enrol one ID. Both run PhysicalEnrollment.add, whose commit is under decisions.run. |
The first commit. The other: an operator's add re-enrols as today (reenrolled: true); an auto add with a live record is skipped. |
Operator device remove |
It deletes the record of an attached phone. | Remove. The ID is held out by the rule in In short, also when the record was made and removed between two runs. |
| Quarantine give-up | It unenrols a phone. | The give-up, held out the same way. |
| Physical-device watcher | It reads enrolled IDs only. The auto-enroller skips every ID with a live record. | No overlap. |
The held-out memory is the auto-enroller's own. It is in memory and lost on restart (ADR 0073 §3).
Failure modes
| Call | Outcome | What happens | Limit, counted from |
|---|---|---|---|
listAttachedPhysical |
ok | IDs checked | the driver's 30 s, from the call |
listAttachedPhysical |
throws | that platform is skipped this run, logged once per message; held-out IDs are not counted as missing | 30 s |
PhysicalEnrollment.add |
enrolled | device.enrolled initiator: "auto"; then notifyAvailability() wakes the queue, as device add's wrapper does (F7), so a waiting request plans |
inspection reads, 30 s each |
add |
DeviceNotEnrollableError |
logged when the ID's reason changes; retried next run | — |
add |
inspection throws (PhysicalInspectionError) |
logged as reason unreadable with its message; retried |
— |
add |
DeviceBusyError |
skipped silently, retried next run | — |
add |
live record at commit | skipped silently | — |
| a run | longer than the interval | the next is armed when it ends; runs never overlap | — |
A restart mid-run leaves either one committed record or none: the enrolment is one registry commit (F3). A daemon stop waits for a run in flight, as it does for the watcher's. settle() awaits it, and leasing.settle's caller in worker-composition.ts calls it next to the watcher's, before the drivers are disposed and state.db is closed (src/leasing/create-leasing.ts:279-281, src/daemon/worker-composition.ts:590-608). dispose() clears the timer.
Rules in play
- Safety rule 1, "never runs a command on one that is not enrolled": ADR 0073 §5 allows
device add's read-only inspection underdevices.autoEnroll. This task edits rule 1 to say so. - Safety rule 8, "A physical device is Simlock's because an admin enrolled it": this task adds "or because the worker enrolled it under
devices.autoEnroll". - ADR 0022 §3, "a registry record an admin created with
device add": narrowed by ADR 0073 §1. Off by default. src/core/AGENTS.md, "Every verb: claim and revalidate (lock) → driver (no lock) → revalidate and commit (lock)": the auto path isadd's own, unchanged in shape.src/core/AGENTS.md, "A transition without the one event its edge calls for throws": the enrolment still emits exactly onedevice.enrolled.- Bus rule, "Payloads are self-contained; change them additively only":
initiatoris added. - Documentation rule 9: the config, the event and the enrolling pages change in this PR.
Tests
Seam: the daemon with the e2e fake driver, modelled on e2e/physical-enrollment.test.ts with health.probeIntervalMs: 200. The two lines marked otherwise use their own file.
- An operator with
devices.autoEnroll: trueplugs in a phone and it is enrolled: one table, an iPhone, an Android phone, a phone attached before the daemon starts. Each gets onedevice.enrolledwithinitiator: "auto"and its installed apps as kept apps. - An agent waiting for a phone gets the auto-enrolled one: one
Pixel 9enrolled and leased, a second--physicalPixel 9request queued, a second Pixel 9 plugged in. The queued request is granted the new phone within one interval, with no second request. A model with no record still fails at once withUNKNOWN_MODEL, as today (src/leasing/lease-acquisition-coordinator.ts:893-900), and this task does not change that. - An operator without the setting plugs in a phone and nothing happens: unset and
false. Nodevice.enrolled, and noinspectPhysicalcall on the fake after three intervals. - A phone that can't be enrolled is retried and logged once per reason change: locked, then unlocked, is enrolled; untrusted for three intervals leaves one line;
untrusted→locked→untrustedleaves three lines. Each line is word for word as In short. - An enrolled phone is left alone:
ready,absent,quarantinedandleased. Nodevice.enrolled, kept apps unchanged. - A removed phone stays out until it is replugged:
device removewhile plugged in;device removebefore the next interval after its auto-enrolment (still out); an operator'sdevice addthendevice removebetween two runs (still out); these two cases usehealth.probeIntervalMs: 5000, so both commands land between two runs; unenrolled after quarantine retries; missing one interval (still out); missing two intervals, then back (enrolled);device addwhile held out (enrolled,initiator: "operator"). - A restart forgets which phones are held out: removed, still plugged in, daemon restarted, enrolled on the first interval.
- A platform that cannot be listed is skipped for that interval: the Android list throws for three intervals, the iPhone is still enrolled, and one log line is written; a held-out Android phone stays held out across the failing runs; a worker with no Android driver lists nothing for Android and logs nothing.
- An operator's
device addreports who enrolled:device.enrolledwithinitiator: "operator"(extend the existingdevice addtest ine2e/physical-enrollment.test.ts). - Auto-enroll never enrols again an ID that got a record first, in
src/core/physical-enrollment.test.ts: an operator'saddcommits between the auto add's inspection and its commit. One record, onedevice.enrolled. - An operator sets
devices.autoEnroll, insrc/core/config.test.ts:true,false, missing (false), and"yes"refused with the message above.
Done when
pnpm test:e2e e2e/physical-enrollment.test.tspasses with the Tests lines' cases.pnpm exec vitest run --project unit src/core/physical-enrollment.test.tsand… src/core/config.test.tspass.config-reference.test.tspasses, and the website pages listed under Modules touched describe the key, the held-out rule, the restart andinitiator.PROTOCOL_VERSION_RANGEis one higher than on the base branch.
Out of scope
- Phones on the network (Enrol Android phones reached over the network (adb connect host:port) #1250).
- Showing phones that can't be enrolled in status, doctor or the console.
- A held-out list that survives a restart.
- Turning the key on without a restart.
Depends on
Written by an agent.
Activity
test: a phone that cannot be enrolled is retried and logged once per …
feat(core): the auto-enroller logs a phone it cannot enrol once per r…
test: a phone removed while plugged in stays out until it has been mi…
feat(core): a phone removed while plugged in stays out until replugged (
test: a platform that cannot be listed is skipped for that run and lo…
feat(core): a platform that cannot be listed is skipped and logged on…
test: auto-enroll never enrols again an ID that got a record first (#…
feat(core): an auto add leaves an ID that got a record first alone (#…
test: the auto-enroller wakes a waiting request, does nothing when of…
docs: safety rules 1 and 8 and the core manual cover the auto-enroller (
chore(core): Fallow entries for the auto-enroller's start and dispose (…
test: type the removed-phone test's phones (#1508)
test: the config and event fixtures carry devices.autoEnroll and init…
refactor(core): a re-enrolment is always the operator's, so it takes …
fix(core): an auto add skips a phone that got a record during its ins…
test: the auto-enrol run leaves a quarantined phone alone (#1508)
docs(core): drop the auto-enrolment line the code already says (#1508)
docs(adr): ADR 0073 is accepted (#1508)
fix(contract): the protocol range is 86 after main took 85 (#1508)
feat(core): a worker enrols the phones plugged into it under devices.…