Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ those changes add, alongside the breaking changes above.
(default 5 MiB, one rotated generation). `daemon.log` no longer gets its own
copies of `component.installed` and `device.slimmed`; read them from the
event file.
- **daemon:** `daemon.log` records every request that changes state, on every
frontend, with who asked, how long it took and its error code; every
background failure Simlock handled by retrying or destroying a device, with
the device and the error; and at `log.level: debug`, read requests and every
device command with its exit code. A failing event subscriber is logged as a
JSON line. `simlock daemon logs --follow` prints new lines as they are
written, across rotations.

### ADR 0005: gateway and worker modes

Expand Down
37 changes: 29 additions & 8 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ own `events.jsonl` along with its own, so `--since` reaches back across a
gateway restart. It holds only what arrived while the gateway was up — a
worker's events from before its uplink connected are not backfilled.

## `simlock daemon <start|stop|status|logs>`
## `simlock daemon <start|stop|status|logs [--follow]>`

Manage the daemon explicitly. Other commands auto-start it on demand; `daemon`
exists for operators and debugging. `start` starts whichever mode
Expand All @@ -1084,8 +1084,12 @@ stop does end is the connections to it — a running `simlock lease` cannot
reconnect, so it exits `1` with a `DAEMON_CONNECTION_LOST` line naming a lease
that is still granted; renew it from a later invocation once the daemon is
back. A lease whose deadline passed while no daemon was running expires as soon
as one is. `logs` tails daemon logs and works even when the daemon is dead — it
reads the log file directly, no connection attempted. `status` never
as one is. `logs` prints the last 100 lines of the daemon log and works even when
the daemon is dead — it reads the log file directly, no connection attempted.
`logs --follow` then keeps printing each new line as it is written, until you
press Ctrl-C (exit 0). It keeps following when the log rotates, and if no
daemon has written a log yet it waits for one. `--follow` cannot be combined
with `--json` (exit 2). `status` never
auto-starts the daemon and distinguishes two failure shapes:
`{"status":"stopped"}` when nothing is listening on the socket at all, versus
`{"status":"handshake-refused","error":{"code":...}}` (exit 1) when a daemon
Expand All @@ -1105,11 +1109,28 @@ about the config, because nothing ever answered — the reason is in `simlock
daemon logs`, which reads the log file directly and so works even though the
daemon never came up.

The daemon writes one structured JSON line per record to `~/.simlock/daemon.log`
(timestamp, level, module, message, and any fields) covering startup (version,
protocol version, socket path, effective config), socket claim/stale-endpoint
recovery, driver discovery, connection open/close, shutdown, and unexpected or
handled errors. Growth is bounded: once the file passes `log.rotateBytes` it is
The daemon writes one JSON line per record to `~/.simlock/daemon.log`
(timestamp, level, module, message, and any fields). The log says what the
daemon was asked to do and what went wrong; what happened to leases and
devices is in `simlock events`, not here. It records:

- **Startup and shutdown**: version, protocol version, socket path, effective
config, socket claim, driver discovery, and why a driver was skipped.
- **Every request that changes something**, on every frontend (CLI, MCP,
HTTP, and a gateway's dispatch): one `operation` line when it finishes,
with the operation, who asked (`principal`, and `requesterId` or `leaseId`
when the request named one), how long it took, and the error code if it
failed. A request that only reads is logged at `debug`, unless it fails.
- **Background failures** that Simlock handled by retrying, waiting, or
destroying a device: the device, the lease when there is one, the step that
failed, and the error text.
- **At `log.level: debug`**, every device command (`simctl`, `adb`,
`sdkmanager`, …): the command, its arguments, its exit code, and how long it
took. Never its environment, its input, or its output.
- Connection open and close, and errors nobody expected.

The wording and fields of a line can change in any release; don't parse them
as a contract. Growth is bounded: once the file passes `log.rotateBytes` it is
rotated to `daemon.log.1` (replacing any previous generation), so `logs` always
shows the current file with the immediately preceding one prepended.

Expand Down
2 changes: 2 additions & 0 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ a warning. Inspect the effective, merged configuration at any time with
| `diskPressure.freeBytesThreshold` | Free disk space below which Simlock treats the machine as under disk pressure. | `10 GiB` |
| `eventBuffer.capacity` | Number of recent business events kept in memory, which `simlock events` without `--since` replays. | `1000` |
| `eventLog.rotateBytes` | Size of the event file, `~/.simlock/events.jsonl`, before it rotates. Every business event is written there and survives a daemon restart. One rotated generation, `events.jsonl.1`, is kept, so the history on disk stays within about twice this size and the oldest events are dropped first. | `5 MiB` |
| `log.level` | Lowest level written to `daemon.log`: `debug`, `info`, `warn`, or `error`. `debug` adds a line for every request that only reads (such as `simlock status`) and for every device command Simlock runs (`simctl`, `adb`, …) with its arguments and exit code. | `info` |
| `log.rotateBytes` | Size at which `daemon.log` is rotated to `daemon.log.1`. One rotated file is kept. | `5 MiB` |
| `health.enabled` | Master switch for leased-device crash detection and recovery. | `true` |
| `health.probeIntervalMs` | How often the health monitor observes leased devices against driver reality. | `30 seconds` |
| `health.stableObservations` | Consecutive `stopped` observations required before a leased device is treated as crashed; guards against transient `Booting`/`Shutting Down`/adb-offline readings. | `2` |
Expand Down
67 changes: 53 additions & 14 deletions docs/internal/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1383,20 +1383,59 @@ state (`reclaiming` included), so a separate aggregate would duplicate
information already visible per-device rather than add any.

Operational logging is a separate concern from the event bus (ADR 0006): two
records, and no fact is copied from one into the other. The `Logger` port
writes durable, structured JSON lines — one per record — for startup, socket
claim/recovery, driver discovery, connection lifecycle, shutdown, and
unexpected/handled errors. `startDaemon` builds the production `Logger`
(`JsonLinesLogger` over a `NodeFileLogSink`) from `config.log` right after
config loads, then hands module-scoped children (`logger.child("server")`,
`.child("connection-host")`, `.child("driver-discovery")`) to each component
so every line is attributable. The sink tracks bytes written and rotates
`daemon.log` to `daemon.log.1` (replacing any previous generation) once
`config.log.rotateBytes` is exceeded, so growth is bounded and `simlock daemon
logs` reads the rotated generation before the current file. The one exception
is the fatal top-level handler: it cannot depend on `config.log` having loaded
successfully, so it builds its own logger straight from the default log path
at a fixed level, falling back to `console.error` only if that itself fails.
records, and no fact is copied from one into the other. `simlock events`
carries business facts (lease granted, device cleaned up, …), while the
`Logger` port writes structured JSON lines saying what the daemon was asked to
do and what went wrong. The log records:

- **One `operation` line per dispatched call**, built in exactly one place:
`runDispatch` (`src/daemon/dispatch.ts`), through the `observe` hook both
dispatchers pass (`logger.child("dispatch")`). It names the operation,
`principal`, `role`, `durationMs`, `leaseId`/`requesterId` from the
validated input when present (nothing else from the input, nothing from the
output), and on failure `code`, plus `message` once the input has passed
its schema (before that the message quotes raw wire input). Level follows the operation's
contract `effect`: a `write` success is `info`, a `read` success `debug`, a
failure `info`, an `INTERNAL` failure `error`. A call refused before its
handler (`UNKNOWN_REQUEST`, `BAD_REQUEST`, `FORBIDDEN`) gets the line too.
`codeOf` is injected (`classifyError`) so `dispatch.ts` stays free of
`src/core`. The HTTP app's own `request` line stays as the transport's
record; `daemon.stop` and `hello` are answered by the socket server and keep
their own lines.
- **One line per handled background failure** — a boot, an eviction, a
quarantine retry, a warm-pool disposition, a scheduled cleanup run, a lease
expiry — from the core module that caught it (`logger.child("<module>")`,
`NoopLogger` by default), with `deviceId`, `step`, `error`, and the lease or
requester where the site knows one. A failure whose error already travels on
an event (`device.purge-failed`, `device.recovery-failed`,
`device.quarantine-stranded`) is not logged again.
- **At `log.level: debug`, one `process` line per device command**:
`LoggingProcessRunner` wraps the daemon's one `ProcessRunner` and logs the
command, arguments, exit code and duration when it settles — never `env`,
`input`, or output. Below `debug` the runner is not wrapped at all.
- **A failing event subscriber**, through `logger.child("bus")`, as one JSON
line with the event, `seq`, message and stack.
- Startup, socket claim/recovery, driver discovery, connection lifecycle,
shutdown, and unexpected errors with their stacks.

`startDaemon` builds the production `Logger` (`JsonLinesLogger` over a
`NodeFileLogSink`) from `config.log` right after config loads, then hands
module-scoped children (`logger.child("server")`, `.child("connection-host")`,
`.child("driver-discovery")`) to each component so every line is attributable.
The sink tracks bytes written and rotates `daemon.log` to `daemon.log.1`
(replacing any previous generation) once `config.log.rotateBytes` is exceeded,
so growth is bounded and `simlock daemon logs` reads the rotated generation
before the current file. `daemon logs --follow` (`src/cli/follow-log.ts`) polls
through the `Filesystem` and `Clock` ports every 250 ms, reading from a byte
offset (`readFileFrom`). It detects a rotation from `daemon.log.1` changing
identity (`FileStat.identity`), not from the current file shrinking, because a
fresh file can outgrow the old offset within one poll; it then prints the rest
of the rotated file and restarts at offset 0. Two rotations inside one poll lose
the middle generation, which the sink has already deleted. The one exception
is the fatal top-level handler: it
cannot depend on `config.log` having loaded successfully, so it builds its own
logger straight from the default log path at a fixed level, falling back to
`console.error` only if that itself fails.

Business facts live in the bus's two records. The ring buffer
(`eventBuffer.capacity`) holds recent events in memory and resets on restart.
Expand Down
2 changes: 1 addition & 1 deletion docs/internal/adr/0006-events-and-log-are-two-records.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 0006. Events and the daemon log are two records

- **Status:** Accepted — not yet implemented
- **Status:** Accepted
- **Date:** 2026-10-01
- **Issue:** [#168](https://github.com/callstackincubator/simlock/issues/168),
[#169](https://github.com/callstackincubator/simlock/issues/169)
Expand Down
2 changes: 1 addition & 1 deletion docs/internal/adr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ the status is stale.
| [0003](0003-one-typed-daemon-contract-behind-every-frontend.md) | One typed daemon contract behind every frontend | Accepted |
| [0004](0004-ttl-first-leases-on-every-transport.md) | TTL-first leases on every transport | Accepted — not yet implemented |
| [0005](0005-gateway-and-worker-modes.md) | Gateway and worker modes | Accepted — not yet implemented |
| [0006](0006-events-and-log-are-two-records.md) | Events and the daemon log are two records | Accepted — not yet implemented |
| [0006](0006-events-and-log-are-two-records.md) | Events and the daemon log are two records | Accepted |
| [0007](0007-a-lease-request-chooses-the-device-mode.md) | A lease request chooses the device mode | Accepted — not yet implemented |
| [0008](0008-the-catalog-pairs-models-with-runtimes-and-status-carries-host-facts.md) | The catalog pairs models with runtimes, and status carries host facts | Accepted — not yet implemented |
Loading
Loading