Skip to content

Daemon log records every request and background failure, and can be followed live #169

Closed

Description

@V3RON

Request: none

Problem

daemon.log does not say what the daemon was asked to do or why background
work failed. A request over the socket (CLI, MCP) leaves no line; only HTTP
requests do. When a background step fails (a boot, a reclaim, a quarantine
retry, a cleanup run, a lease expiry) the error is dropped: the device is
retried or destroyed and the reason is recorded nowhere. Device commands
(simctl, adb, sdkmanager) are never logged. A failing event subscriber
is written to the log as raw text, not as a JSON line.

simlock daemon logs prints the log once and exits, so none of this can be
watched while it happens.

Who it is for

An operator or agent working out why a lease was slow, queued, or refused,
or why a device was destroyed, on a machine they can run simlock on.

Outcome

  • Every operation that changes state leaves one line when it finishes: the
    operation, who asked, how long it took, and the result or error code. This
    holds for every frontend: socket, MCP, HTTP, and a gateway's dispatch.
  • Read-only operations leave that line only at log.level: debug. A failed
    one leaves it at the default level.
  • Every background failure that is handled by retrying, deferring, or
    destroying a device leaves one line with the device, the lease if there is
    one, the step that failed, and the error text. The exception is a failure
    whose error text already travels on an event (device.purge-failed,
    device.recovery-failed, device.quarantine-stranded): it leaves no
    line.
  • At log.level: debug, every device command leaves one line: the command,
    its arguments, its exit code, and how long it took. Never its environment
    or its input.
  • Every line in daemon.log written by a running daemon is a JSON line.
  • simlock daemon logs --follow prints the current log, then each new line
    as it is written, until interrupted. It keeps following across a rotation,
    and it waits when no daemon is running.
  • A business fact is not repeated in daemon.log. Facts are in the event
    file; this log says what was asked and what went wrong.

Non-goals

  • Reading another machine's daemon log, over HTTP or through a gateway.
  • Filtering by level or module in daemon logs.
  • Logging from the CLI or the MCP server process. They are clients.
  • Recording what a device.exec command printed.

Completion conditions

  • Lease and release a device over the socket: daemon.log holds one line
    for each, naming the requester and the duration.
  • Ask for a lease with an unknown model: the line carries the error code.
  • At the default level, simlock status adds no line. At debug it does.
  • Make a device's boot fail: daemon.log holds a line naming the device,
    the step, and the tool's error text.
  • At debug, a lease that boots a device leaves one line per device
    command, each with an exit code.
  • Make an event subscriber throw: the log gains one JSON line and no raw
    text.
  • Run simlock daemon logs --follow, then lease a device: the request line
    appears without rerunning the command. With log.rotateBytes set small,
    lines written after a rotation still appear.
  • CLI.md describes what the log records and the --follow flag.

Open questions

None.

Decisions

  • ADR 0006 — Events and the daemon log are two records: the log records what was asked and what went wrong, never a fact, and no log line becomes an event.

Technical spec

Modules touched

  • src/contract/operations.ts: OperationDefinition gains effect: "read" | "write" | ((input) => "read" | "write"), shaped like role.
    • read: catalog.get, status.get, lease.list, list.get, config.get, driver.passthrough, events.replay, events.subscribe, events.unsubscribe, token.list, worker.list.
    • write: lease.request, lease.cancel, lease.renew, lease.release, lease.release-all, device.exec, nuke.run, daemon.stop, token.create, token.revoke, worker.drain, worker.undrain, worker.remove.
    • By input: doctor.run is write when fix is true; cleanup.run is read when dryRun is true.
  • src/daemon/dispatch.ts: DispatchPipeline gains observe?: { clock, logger, codeOf(error): string }. runDispatch writes one line, message operation, when the call settles. This is the only place the line is built.
    • Fields: operation, principal, role, durationMs. On failure also code and message. leaseId and requesterId when the validated input has a string under that key. Nothing else from the input and nothing from the output.
    • Level: success of a write is info; success of a read is debug; a failure is info; a failure with code INTERNAL is error.
    • A call refused before its handler (UNKNOWN_REQUEST, BAD_REQUEST, FORBIDDEN) is a failure and gets the line.
    • The module stays free of src/core imports; codeOf is injected.
  • src/daemon/dispatcher.ts, src/gateway/dispatcher.ts: pass observe with logger.child("dispatch"). GatewayDispatcher gains clock and errorCode options. main.ts hands classifyError to both.
  • src/daemon/server.ts: delete the Handled request error debug line; the operation line replaces it. Unhandled request error, here and in src/http/app.ts, stays: it carries the stack. daemon.stop and hello are answered by the server, not dispatched, and keep their existing lines.
  • src/http/app.ts: the request line stays. It is the transport's own record and covers requests that never reach a dispatcher.
  • Background failures: LeaseEngine passes options.logger on to DeviceProvisioner, LeaseAcquisitionCoordinator, QuarantineCoordinator, WarmPoolCoordinator and LeaseExpiryScheduler; main.ts passes it to CleanupReaper. Each takes it as optional, scopes it with child("<module>"), and defaults to NoopLogger. One line per site, with deviceId, step, error (the caught error's message), and leaseId or requesterId where the site knows one:
    • device-provisioner.ts: a new device failed to become ready (warn); destroying it afterwards failed (warn).
    • lease-acquisition-coordinator.ts: shutting down an eviction target failed; deleting an eviction target failed; booting a shut-down device for a waiter failed; destroying it afterwards failed (all warn).
    • quarantine-coordinator.ts: a reclaim retry failed; a delete retry failed (warn, with attempts); the retry itself threw (error).
    • warm-pool-coordinator.ts: shutting down a reclaimed device failed; making a reclaimed device ready failed (warn).
    • reaper.ts: a scheduled cleanup run threw (error).
    • lease-expiry-scheduler.ts: expiring a lease threw (error, with leaseId).
    • Not logged: a failure whose error text already travels on an event (device.purge-failed, device.recovery-failed, device.quarantine-stranded), and the observer-isolation catches in wait-queue.ts.
  • src/ports/logging-process-runner.ts (new, exported from src/ports/index.ts): LoggingProcessRunner wraps a ProcessRunner.
    • run: one debug line when it settles, with command, args, code, durationMs, or error when it threw. The error is rethrown unchanged.
    • spawn and spawnStreaming: the same line, once, when the process exits.
    • Never env, input, stdout or stderr.
  • src/daemon/main.ts:
    • Wrap the one ProcessRunner in LoggingProcessRunner with logger.child("process") only when config.log.level is debug.
    • Give EventBus a logger that writes Event handler failed through logger.child("bus") with event, seq, message and stack. The console.error fallback in the fatal start handler stays; it runs only when the log cannot be written.
  • src/cli/index.ts, daemon logs --follow:
    • Prints the last 100 lines as today, then each new complete line until SIGINT or SIGTERM, then exits 0 with its timer cancelled.
    • Follows by polling through the Filesystem and Clock ports, every 250 ms. No fs.watch.
    • A rotation is detected from the rotated file changing, not from the current file shrinking: a fresh file can outgrow the old offset within one poll. After a rotation it prints the unread rest of daemon.log.1, then the new file from its start.
    • A missing log file means wait.
    • --follow with --json is a usage error, exit 2.
    • If the follow needs a read from a byte offset or a file identity, add it to the Filesystem port and to MemoryFilesystem.
  • Docs:

Contract and event changes

  • OperationDefinition.effect is new in the contract module. It never goes on the wire.
  • No operation, schema, error code, config key, or event changes. Both EVENTS.md files are untouched.

Rules in play

  • architecture.md 9: logging through the Logger port, the follow through Filesystem and Clock; no console, no fs.
  • architecture.md 10: the operation line is built in runDispatch and nowhere else. No transport and no dispatcher writes a second one.
  • architecture.md 12: every exit from the follow leaves no timer and no signal handler behind.
  • architecture.md 13: the NodeFileLogSink comment in src/ports/logger.ts says volume is low because nothing logs per request. That stops being true; fix it.
  • architecture.md 1 and 2: core log lines name a device id, a step and an error text. They interpret nothing platform-specific.
  • src/gateway/boundary.test.ts: dispatch.ts must not import src/core.
  • safety.md, wire input is a claim: principal and requesterId are logged as claimed. No credential, token secret, environment or stdin is ever logged.
  • events.md: no fact becomes a log line. A site whose error already rides on an event is left alone.
  • documentation.md 3: no log message names a path in this repo.
  • testing.md: every title below is a claim; break the code and watch each fail.

Tests

Unit, src/contract/operations.test.ts:

  • Every operation declares an effect.
  • doctor.run is a write only with fix, and cleanup.run is a read only with dryRun.

Unit, src/daemon/dispatch.test.ts (new):

  • A write that succeeds logs one info line with the operation, principal, role and duration.
  • A read that succeeds logs at debug and nothing at info.
  • A read that fails logs at info with its error code.
  • An error classified INTERNAL logs at error.
  • A request refused for its role logs one line with FORBIDDEN.
  • The line carries leaseId and requesterId from the validated input and no other input field.
  • The gateway dispatcher logs the same line for the same operation.

Unit, one per site, in the module's own test file:

  • A new device that fails to become ready logs the driver's error with the device id.
  • A failed destroy after a failed boot logs the device id and the error.
  • A failed eviction shutdown logs the device id and the error, and the waiter is deferred as before.
  • A failed eviction delete logs the device id and the error.
  • A shut-down device that fails to boot for a waiter logs the driver's error.
  • A failed quarantine reclaim retry logs the error and the attempt number.
  • A failed quarantine delete retry logs the error and the attempt number.
  • A reclaimed device that fails to shut down logs the error.
  • A reclaimed device that fails to become ready logs the error.
  • A cleanup run that throws logs at error.
  • A lease expiry that throws logs at error with the lease id.
  • A purge failure logs nothing, because device.purge-failed carries the error.

Unit, src/ports/logging-process-runner.test.ts:

  • run logs the command, arguments, exit code and duration, and never the environment or the input.
  • A run that throws logs the error and rethrows it.
  • spawn logs once, when the process exits.
  • spawnStreaming logs once, when the process exits, and no chunk of its output.

Unit, src/daemon/main.test.ts:

  • At log.level: info no process line is written.
  • A throwing event subscriber writes one JSON line through the logger and nothing through console.error.

Unit, src/cli/index.test.ts:

  • daemon logs --follow prints the existing tail, then a line appended later.
  • It prints lines written after a rotation, when the new file is already larger than the old offset at the next poll.
  • It waits while the log file is missing and prints lines once it appears.
  • daemon logs --follow --json fails with USAGE, exit 2.
  • An interrupt ends the follow with exit 0 and leaves no timer armed.

E2e, fast lane, e2e/daemon-log.test.ts:

  • A lease and its release over the socket each leave one operation line naming the requester.
  • A lease request for an unknown model leaves an operation line with its error code.
  • At the default level simlock status leaves no operation line; at debug it leaves one.
  • A scripted boot failure leaves a line naming the device and the driver's error text.
  • At debug, a device.exec command leaves one process line with its exit code and no line holding its output.
  • simlock daemon logs --follow prints the operation line of a lease made after it started.

Written by an agent.

Activity

  1. added
    feature:spec
    on Oct 1, 2026
  2. V3RON commented on Oct 1, 2026

    @V3RON
    ContributorAuthor

    Spec updated: Decisions and Technical spec added.

    Written by an agent.

  3. V3RON commented on Oct 1, 2026

    @V3RON
    ContributorAuthor

    Spec updated: Outcome now excepts failures whose error already rides on an event.

    Written by an agent.

  4. added
    feature:ready
    and removed
    feature:spec
    on Oct 1, 2026
  5. self-assigned this
    on Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

feature:ready

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions