Skip to content

fix(status): say when the running daemon is another version (#46) - #54

Merged
neoromantic merged 5 commits into
mainfrom
fix/status-daemon-version-46
Oct 21, 2026
Merged

neoromantic merged 5 commits into
mainfrom
fix/status-daemon-version-46

Conversation

@neoromantic

@neoromantic neoromantic commented Oct 21, 2026 •

Copy link
Copy Markdown
Owner

Problem

The CLI and the daemon share cache.db. After an upgrade, a daemon of the old version left running keeps writing the old cache format until it restarts (for #18 it fails group and channel jobs and writes bare chats_cache rows). tg status said nothing about it.

Fix

  • The daemon records its version in daemon_status when it starts (daemon_version, plus daemon_version_pid so the version belongs to that daemon). Stopping clears both. Starting and stopping each write their keys in one transaction, and tg status reads them in one, so it never sees a daemon half started or half stopped. A version left by a daemon that died without cleanup is ignored when another daemon's PID is in daemon_pid. Access stays inside the daemon-status service: no DDL, no migration, no new direct cache-db calls.
  • The daemon's start log line includes its version.
  • tg status compares the recorded version with its own, but only for a running daemon whose PID matches the PID file. A daemon that is still starting (for example, waiting for the migration after an upgrade) is not compared.

JSON (tg status):

{
  "daemon": {
    "status": "running",
    "pid": 4242,
    "version": "0.3.1",
    "version_mismatch": true,
    ...
  },
  ...
  "cli_version": "0.3.2"
}
  • daemon.version: what the running daemon recorded, or null (stopped, still starting, or started by tg 0.3.0 or older).
  • daemon.version_mismatch: true when a running daemon's version differs from cli_version, or when it was started by tg 0.3.0 or older and so recorded none. false while the daemon is stopped or still starting.
  • cli_version: this tg's version.

Pretty (tg status --format pretty) adds a Version row to the Daemon section and, on a mismatch:

  ⚠ The daemon runs 0.3.1, this tg is 0.3.2. Restart the daemon to run 0.3.2:
    tg daemon stop
    tg daemon start

With no recorded version (a daemon from 0.3.0 or older) it says "The daemon was started by an older tg." instead of the first sentence. When the daemon is newer than this tg, it says to use the newer tg and prints no restart commands, because restarting would downgrade the daemon.

The exit code, stderr and tg daemon status are unchanged. The docs are updated in docs/cli-commands.md, docs/daemon.md (Upgrading), docs/database-schema.md (daemon_status keys) and docs/testing.md. The daemon_status.key annotation now lists the real keys. README is unchanged because it shows no tg status output.

Tests

  • src/__e2e__/status.e2e.test.ts, new E2E: status, daemon version block. It seeds daemon_status and a PID file holding the test runner's PID, and covers: another version (JSON field and pretty hint with restart commands), the same version (Version row, no hint), no recorded version, a newer daemon (no restart commands), a stopped daemon (one that died without cleanup, one that stopped cleanly, and none ever run), and one still starting. Every case checks exit code 0 and empty stderr.
  • src/__tests__/daemon-status.test.ts: the version is recorded and tied to the PID, stopping clears it, and a version from another PID's daemon is ignored.
  • src/__integration__/telegram.integration.test.ts: the live daemon test now expects daemon.version === VERSION, version_mismatch: false and cli_version. It was not run live for this PR.
  • Local runs: bun run check, bun run test (1558 pass) and bun run test:e2e (201 pass) all pass.

Fixes #46

🤖 Generated with Claude Code

neoromantic and others added 5 commits October 22, 2026 01:05
The CLI and the daemon share cache.db. After an upgrade, a daemon of the
old version left running keeps writing the old cache format until it
restarts (for #18 it fails group and channel jobs and writes bare
chats_cache rows), and nothing told the user to restart it.

The daemon now records its version in daemon_status when it starts, tied
to its PID, so a version left by a daemon that died without cleanup can't
hide an older daemon started after it. tg status compares it with its own
version: JSON gets daemon.version, daemon.version_mismatch and
cli_version, and pretty output shows a Version row with the restart
commands, or says to use the newer tg when the daemon is the newer one,
since restarting it would downgrade it. A running daemon that recorded no
version counts as older; one still starting is not compared.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
getDaemonInfo ran one autocommit SELECT per key. If a same-version
daemon stopped between the daemon_pid and daemon_version_pid reads,
tg status saw the PID without its version and printed "started by an
older tg, restart it". No order of writes fixes that: any fixed order
breaks either the start or the stop sequence. One read transaction
gives one WAL snapshot, and starting and stopping now write their keys
atomically, so the result no longer depends on write order.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The stopped-daemon case left a daemon_pid row of another PID, so the
PID comparison alone hid the isRunning guard: dropping it kept every
test green, while a fresh install or a cleanly stopped daemon (both
PIDs null) would get a false restart hint. The new cases fail without
the guard. testing.md also said a newer daemon gets a restart hint;
it gets "use the newer tg" and no restart commands.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… false

The JSON example showed a 0.3.0 daemon reporting "0.3.0", but daemons
from 0.3.0 and older record no version, as the text above it says. It
now uses two versions that both record one. The version_mismatch bullet
now says it is false while the daemon is stopped or still starting, so
a reader can tell that case from an old daemon's null version.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The transaction around setDaemonRunning and setDaemonStopped keeps a
reader from seeing a daemon_pid without its version, but no test
failed when it was removed. A trigger now fails the last write of each
call: start must leave no version or PID behind, and stop must keep
the running daemon's keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic merged commit 99920ca into main Oct 21, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/status-daemon-version-46 branch October 21, 2026 21:58
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.

tg status doesn't say when the running daemon is an older version

1 participant