Repository navigation
fix(status): say when the running daemon is another version (#46) - #54
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 barechats_cacherows).tg statussaid nothing about it.Fix
daemon_statuswhen it starts (daemon_version, plusdaemon_version_pidso the version belongs to that daemon). Stopping clears both. Starting and stopping each write their keys in one transaction, andtg statusreads 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 indaemon_pid. Access stays inside the daemon-status service: no DDL, no migration, no new direct cache-db calls.tg statuscompares 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, ornull(stopped, still starting, or started by tg 0.3.0 or older).daemon.version_mismatch:truewhen a running daemon's version differs fromcli_version, or when it was started by tg 0.3.0 or older and so recorded none.falsewhile the daemon is stopped or still starting.cli_version: this tg's version.Pretty (
tg status --format pretty) adds aVersionrow to the Daemon section and, on a mismatch: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 statusare unchanged. The docs are updated indocs/cli-commands.md,docs/daemon.md(Upgrading),docs/database-schema.md(daemon_status keys) anddocs/testing.md. Thedaemon_status.keyannotation now lists the real keys. README is unchanged because it shows notg statusoutput.Tests
src/__e2e__/status.e2e.test.ts, newE2E: status, daemon versionblock. It seedsdaemon_statusand 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 expectsdaemon.version === VERSION,version_mismatch: falseandcli_version. It was not run live for this PR.bun run check,bun run test(1558 pass) andbun run test:e2e(201 pass) all pass.Fixes #46
🤖 Generated with Claude Code