firewatcher 1.67__tar.gz → 1.69__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. {firewatcher-1.67 → firewatcher-1.69}/MANIFEST.in +1 -0
  2. {firewatcher-1.67/firewatcher.egg-info → firewatcher-1.69}/PKG-INFO +34 -13
  3. {firewatcher-1.67 → firewatcher-1.69}/README.md +31 -9
  4. firewatcher-1.69/docs/lessons-full.md +68 -0
  5. firewatcher-1.69/docs/lessons.md +18 -0
  6. firewatcher-1.69/docs/limitations.md +80 -0
  7. {firewatcher-1.67 → firewatcher-1.69/firewatcher.egg-info}/PKG-INFO +34 -13
  8. {firewatcher-1.67 → firewatcher-1.69}/firewatcher.egg-info/SOURCES.txt +1 -0
  9. {firewatcher-1.67 → firewatcher-1.69}/firewatcher.py +375 -275
  10. firewatcher-1.69/setup.py +38 -0
  11. {firewatcher-1.67 → firewatcher-1.69}/tests/conftest.py +14 -0
  12. {firewatcher-1.67 → firewatcher-1.69}/tests/test_capture.py +50 -7
  13. {firewatcher-1.67 → firewatcher-1.69}/tests/test_cli.py +28 -26
  14. {firewatcher-1.67 → firewatcher-1.69}/tests/test_config.py +13 -14
  15. {firewatcher-1.67 → firewatcher-1.69}/tests/test_events.py +1 -1
  16. firewatcher-1.69/tests/test_locale.py +187 -0
  17. {firewatcher-1.67 → firewatcher-1.69}/tests/test_log_source.py +16 -0
  18. {firewatcher-1.67 → firewatcher-1.69}/tests/test_notify.py +29 -2
  19. {firewatcher-1.67 → firewatcher-1.69}/tests/test_patterns.py +11 -0
  20. {firewatcher-1.67 → firewatcher-1.69}/tests/test_position.py +343 -60
  21. {firewatcher-1.67 → firewatcher-1.69}/tests/test_slugify.py +3 -6
  22. {firewatcher-1.67 → firewatcher-1.69}/tests/test_systemd.py +396 -40
  23. firewatcher-1.67/docs/lessons-full.md +0 -37
  24. firewatcher-1.67/docs/lessons.md +0 -13
  25. firewatcher-1.67/docs/limitations.md +0 -40
  26. firewatcher-1.67/setup.py +0 -28
  27. {firewatcher-1.67 → firewatcher-1.69}/LICENSE +0 -0
  28. {firewatcher-1.67 → firewatcher-1.69}/examples/firewatcher.conf +0 -0
  29. {firewatcher-1.67 → firewatcher-1.69}/examples/nvme_failure.regex +0 -0
  30. {firewatcher-1.67 → firewatcher-1.69}/examples/sys_msg.txt +0 -0
  31. {firewatcher-1.67 → firewatcher-1.69}/firewatcher.egg-info/dependency_links.txt +0 -0
  32. {firewatcher-1.67 → firewatcher-1.69}/firewatcher.egg-info/entry_points.txt +0 -0
  33. {firewatcher-1.67 → firewatcher-1.69}/firewatcher.egg-info/top_level.txt +0 -0
  34. {firewatcher-1.67 → firewatcher-1.69}/setup.cfg +0 -0
  35. {firewatcher-1.67 → firewatcher-1.69}/tests/test_compressor.py +0 -0
  36. {firewatcher-1.67 → firewatcher-1.69}/tests/test_matching.py +0 -0
  37. {firewatcher-1.67 → firewatcher-1.69}/tests/test_redirects.py +0 -0
@@ -3,3 +3,4 @@ include README.md
3
3
  include examples/*
4
4
  include docs/*.md
5
5
  recursive-include tests *.py
6
+ exclude docs/progress.md
@@ -1,13 +1,12 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: firewatcher
3
- Version: 1.67
3
+ Version: 1.69
4
4
  Summary: Watch logs for incident patterns and persist surrounding context off-box
5
5
  Home-page: https://github.com/yufei-pan/firewatcher
6
6
  Author: Yufei Pan
7
7
  Author-email: pan@zopyr.us
8
- License: GPLv3+
8
+ License-Expression: GPL-3.0-or-later
9
9
  Classifier: Programming Language :: Python :: 3
10
- Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
11
10
  Classifier: Operating System :: POSIX :: Linux
12
11
  Classifier: Topic :: System :: Logging
13
12
  Classifier: Topic :: System :: Monitoring
@@ -20,8 +19,8 @@ Dynamic: classifier
20
19
  Dynamic: description
21
20
  Dynamic: description-content-type
22
21
  Dynamic: home-page
23
- Dynamic: license
24
22
  Dynamic: license-file
23
+ Dynamic: license-expression
25
24
  Dynamic: requires-python
26
25
  Dynamic: summary
27
26
 
@@ -58,7 +57,7 @@ A directory argument loads every non-hidden pattern file inside it. Files ending
58
57
 
59
58
  `firewatcher` exits with an error when no pattern loads, for example when a pattern path is mistyped or every regex is invalid, instead of running while matching nothing.
60
59
 
61
- `--install-service` seeds `/etc/firewatcher/patterns.d` with example patterns when that directory is empty. The `examples/` directory in the source tree is not installed onto `PATH` by `pip install`; copy the pattern files from a clone, or let `--install-service` write them.
60
+ `--install-service` seeds `/etc/firewatcher/patterns.d` with example patterns when it holds no pattern file. The `examples/` directory in the source tree is not installed onto `PATH` by `pip install`; copy the pattern files from a clone, or let `--install-service` write them.
62
61
 
63
62
  On a match, `firewatcher` writes:
64
63
 
@@ -89,13 +88,13 @@ When firewatcher follows the journal (not a log file), it saves how far it has r
89
88
 
90
89
  To tell where it stopped, firewatcher reads the journal with `journalctl -o short-iso`, so journal lines in captures carry the year and time zone: `2026-10-23T17:52:41+00:00 host sshd[812]: …` instead of `Oct 23 17:52:41 host sshd[812]: …`. Lines logged while firewatcher was stopped arrive all at once. A capture opened by one of them takes its context from the `--capture-time` before that line by the time each line was logged, and holds the lines after it as they are read, up to `--capture-line-count-max`.
91
90
 
92
- The position is the second of the last line read, which line of that second it was, and a digest of that line. When unsure, firewatcher reads a line again rather than skip it. A run that started from `--tail-lines` in the middle of a second numbers that second's lines from where it started, so identical lines logged in that second can be captured again after a restart. A save that fails is logged once, and a restart then reads again from the last position saved. If the clock is set back after a position is saved, for example at a boot with a wrong hardware clock, new lines stamped before that second count as already read. A position more than a minute in the future is ignored.
91
+ The position is the second of the last line read, its place in that second, and a digest of it. When unsure, firewatcher reads a line again rather than skip it: identical lines logged in one second, and lines that journald stored in another order than the one they show, can be captured again after a restart, and when the journal no longer holds the saved line (its file was rotated away while firewatcher was stopped, or a crash or reboot lost it), every line from `--capture-time` before it is matched, and no further back than `--resume-max`. One case can skip lines: if the journal lost the saved line, or lines before it in its second, and holds a later one of that second with the same text, the replay can end there and the lines between are not matched ([limitations](https://github.com/yufei-pan/firewatcher/blob/master/docs/limitations.md#a-restart-after-the-journal-lost-the-saved-line-can-skip-lines-that-follow-it)). A failed save is logged once, and a restart then reads again from the last position saved. If the clock is set back after a save by up to a minute (a boot with a wrong hardware clock) and the journal still holds the saved line, the lines after it are still matched, even when they are stamped before the saved second. A position more than a minute ahead of the clock is ignored, and the last `--tail-lines` lines are read.
93
92
 
94
93
  Only one firewatcher uses a position file at a time: another one started with the same file, such as a test run in a terminal with the default settings, reads the last `--tail-lines` lines and saves nothing. An empty `--position-file` turns resuming off, and so does a journalctl that does not accept `-o short-iso` and `--since=@`, which firewatcher checks at start.
95
94
 
96
95
  ### Disk and memory limits
97
96
 
98
- firewatcher keeps the filesystems it writes captures to above `--min-free-percent` free (default 1%, counted as `df` does, so the root-reserved blocks are left alone). Only what the filesystem reports authorizes a write. firewatcher checks when a capture opens, about once a second, and again before what it has written since the last check could bring it within 64 KiB of the floor (and at least every 8 MiB), fsyncing first so the filesystem, an NFS server included, counts every byte. A line that would cross that margin is not written there. Another program filling the same disk between two checks can still push it past the floor; firewatcher's own writes do not.
97
+ firewatcher keeps the filesystems it writes captures to above `--min-free-percent` free (default 1%, counted as `df` does, so the root-reserved blocks are left alone). It asks the filesystem when a capture opens, about once a second, and before its own writes could come within 64 KiB of the floor (at least every 8 MiB), fsyncing first so an NFS server counts every byte; a line that would cross that margin is not written there. firewatcher's own writes never take the disk past the floor; another program filling it between two checks can.
99
98
 
100
99
  Near the floor, firewatcher may delete its own oldest months in that folder, so a new incident can be recorded, under these rules:
101
100
 
@@ -104,11 +103,11 @@ Near the floor, firewatcher may delete its own oldest months in that folder, so
104
103
  - **How much:** only as many months as it takes, counted by their size on disk, and nothing at all if deleting every one of them still could not make room. That happens when something else filled the disk, and then the history is kept.
105
104
  - **How often:** at most once per `--purge-interval` per folder (default an hour), so a disk that something else keeps filling drains firewatcher's history slowly. A purge that could not help is not tried again for five minutes (or the interval, if shorter). Each deletion is logged.
106
105
 
107
- After deleting, firewatcher syncs and reads the free space again. A filesystem that shows freed space late costs a move to the fallback folder, never the floor. If there is still no room, the capture moves to the fallback folder with a note, or, with no folder left, stops being written, which is logged; the incident is still pushed. Compression keeps to the floor too: a month whose archive could cross it is left uncompressed for now. Purging and compression never wait for each other, so log reading never waits for `tar`. `--delete-after-months 0` means nothing is deleted by age; the floor can still delete the oldest months. Set `--min-free-percent 0` to turn all of this off.
106
+ After deleting, firewatcher syncs that filesystem alone (syncfs; none on macOS, where APFS shows freed space at once) and reads the free space again. A filesystem that shows freed space late costs a move to the fallback folder, never the floor. If there is still no room, the capture moves to the fallback folder with a note, or, with no folder left, stops being written, which is logged; the incident is still pushed. Compression keeps to the floor too: a month whose archive could cross it is left uncompressed for now. Purging and compression never wait for each other, so log reading never waits for `tar`. `--delete-after-months 0` means nothing is deleted by age; the floor can still delete the oldest months. Set `--min-free-percent 0` to turn all of this off.
108
107
 
109
- Memory is bounded too. The `--capture-time` seconds before a match are kept in memory compressed (zlib, in blocks of about 64 KiB of text; with its bookkeeping, about 3 times smaller than the text for a busy host's varied lines, more for repetitive ones), within `--buffer-max-mb` (default 16 MiB). Past the limit the oldest block is dropped and the capture says so. The newest lines, up to 256 KiB of them before they are compressed, are always kept, so the buffer can go over the limit by about that much, or by one very long line. Compression costs well under a microsecond per line. A line longer than 1 MiB is read in pieces, and each piece is matched on its own, so a pattern split across the 1 MiB boundary is missed. The LLM excerpt reads at most `max_bytes` of a capture.
108
+ Memory is bounded too. The `--capture-time` seconds before a match are kept compressed (zlib, about 3 times smaller for a busy host's varied lines) within `--buffer-max-mb` (default 16 MiB); past it the oldest lines are dropped, and the capture says so. The newest lines, up to 256 KiB before they are compressed, are always kept, so the buffer can go over the limit by about that much. A line longer than 1 MiB is read and matched in pieces, so a pattern split across a 1 MiB boundary is missed.
110
109
 
111
- Monthly directories are compressed on a background thread, so log reading never waits for `tar`. Each month becomes `YYYY-MM.tar.xz` holding `YYYY-MM/…`. The directory is removed only after `tar` succeeds and the archive is synced to disk. An existing archive is never overwritten: the new one becomes `YYYY-MM.1.tar.xz`, and so on.
110
+ Monthly directories are compressed on a background thread, so log reading never waits for `tar`. Each month becomes `YYYY-MM.tar.xz` holding `YYYY-MM/…`. The directory is removed only after `tar` succeeds and the archive and its folder are synced to disk; if a sync fails, the month stays and is archived again later, nothing lost. An existing archive is never overwritten: the new one becomes `YYYY-MM.1.tar.xz`, and so on.
112
111
 
113
112
  ## systemd service
114
113
 
@@ -118,7 +117,7 @@ sudo firewatcher --print-unit
118
117
  sudo firewatcher --uninstall-service
119
118
  ```
120
119
 
121
- `--install-service` writes `/etc/systemd/system/firewatcher.service`, creates the output directory, seeds `/etc/firewatcher/patterns.d/` with example patterns **only if that directory is empty**, writes `/etc/firewatcher/firewatcher.conf` **only if that file is missing**, then `systemctl daemon-reload && systemctl enable --now firewatcher`.
120
+ `--install-service` writes `/etc/systemd/system/firewatcher.service`, creates the output directory, seeds `/etc/firewatcher/patterns.d/` with example patterns **only if it holds no pattern file**, writes `/etc/firewatcher/firewatcher.conf` **only if that file is missing**, then `systemctl daemon-reload && systemctl enable --now firewatcher`. It checks first, and writes nothing (exit 2) for a unit name systemd cannot load or a path or value that is not UTF-8. If writing the unit or a `systemctl` step fails (or `systemctl` cannot be run), it removes the config and the unit it created, puts back a unit it replaced as it was, and disables a unit it enabled, so the same command can run again. The output folder and the example patterns stay.
122
121
 
123
122
  `systemctl reload firewatcher` reloads the pattern files without a restart; see SIGHUP above.
124
123
 
@@ -214,7 +213,7 @@ always_notify =
214
213
 
215
214
  `patterns` and `always_notify` are one path per line, or comma-separated. Positional pattern arguments replace the `patterns` list.
216
215
 
217
- Out-of-range values stop `firewatcher` at startup: a negative time or count, `capture_time` over 7 days or `capture_time_max` over 30 days, `min_free_percent` outside 0–50, `purge_interval` or `resume_max` over 30 days, `buffer_max_mb` below 1, a timeout of 0 or less or over a day, an ntfy priority outside 1–5, or an LLM, ntfy or events URL that does not start with `http://` or `https://` or that contains spaces, or whose server part (after any `user:password@`) is not a host name with no `/`, `?`, `#`, `@`, `[`, `]`, `:` or `%` in it, or an IPv6 address in brackets, then an optional `:port` of up to 5 digits (0–65535); so a `user:password` holding a `/`, `?` or `#` is refused (`[` and `]` are fine there, since the `user:password` is stripped before the request). Also refused is a URL with a `user:password` that has another `@` later in its path, query or fragment (a password holding an `@` before a `/`, `?` or `#`). A password that starts with digits and a `/` still reads as a host and port, and those digits show as the port in logs ([limitations](https://github.com/yufei-pan/firewatcher/blob/master/docs/limitations.md#a-url-whose-password-starts-with-digits-and-a--shows-those-digits-as-the-port)).
216
+ Out-of-range values stop `firewatcher` at startup: a negative time or count, `capture_time` over 7 days or `capture_time_max` over 30 days, `min_free_percent` outside 0–50, `purge_interval` or `resume_max` over 30 days, `buffer_max_mb` outside 1–65536, a timeout of 0 or less or over a day, or an ntfy priority outside 1–5. So does an LLM, ntfy or events URL that is not `http://` or `https://`, has spaces, or whose server part is not a host name or a bracketed IPv6 address with an optional port (0–65535), as firewatcher reads it by its own strict rule. A `user:password` in a URL is never sent; one that would make the URL read two ways is refused, with [one exception](https://github.com/yufei-pan/firewatcher/blob/master/docs/limitations.md#a-url-whose-password-starts-with-digits-and-a--shows-those-digits-as-the-port).
218
217
 
219
218
  `api_key` under `[llm]` and `token` under `[notify]` are not command-line flags. `FIREWATCHER_LLM_API_KEY` and `FIREWATCHER_NTFY_TOKEN` override them when set. `events_token` under `[notify]` is likewise config-only, overridden by `FIREWATCHER_EVENTS_TOKEN`. `--install-service` creates `/etc/firewatcher/firewatcher.conf` with mode `0600` only when it is missing, and the unit runs `firewatcher --config` that path. An existing file is left as it is. Flags passed to a later install are appended on `ExecStart` so they override the file.
220
219
 
@@ -237,7 +236,7 @@ A capture that fits in `max_bytes` (default 48000) is sent whole. A bigger one i
237
236
 
238
237
  Any user or program on the host can write log lines, and those lines reach the model. The request marks the matched line and the excerpt as untrusted data between `BEGIN`/`END` lines that carry a random token, and the system prompt tells the model never to follow instructions inside them. That makes it harder for a log line to talk the model out of notifying, but cannot rule it out. For incidents that must always reach you, list patterns under `[notify] always_notify` (or `--always-notify`): when a line that matched a watch pattern also matches one of them, the capture is pushed even if the model says not to notify. That push uses the generated title and the line that matched, followed by the model's summary, since log text can steer the summary as well. If no always_notify pattern loads, firewatcher logs an error and keeps capturing without it; `--always-notify ''` clears a list set in the config. Only the line that opened the capture and later lines that matched a watch pattern while it was open count. Other lines in the window do not, and neither do matches from an earlier capture that appear as context. The capture's color codes are removed before it is sent to the model.
239
238
 
240
- `[llm] notes` (or `--llm-notes TEXT`; `note` also works as the key; the flag wins over the file, and `--llm-notes ''` clears a note set in the config) is a short free-text note about this host, for example `loop/dm devices from pytest and /tmp/claude-*/scratchpad and OOMs in capped test containers are expected`. It is sent with every capture as a line `Operator notes about this host: ...` before the untrusted log data, outside the `BEGIN`/`END` block, and the system prompt tells the model that notes describe what is normal there. Any run of whitespace in a note, newlines included, becomes one space. Only you can set it, so unlike the log lines it is trusted text; keep it short, since it is sent with every capture. When the config exists, `--install-service --llm-notes ...` puts the note on the unit's `ExecStart` (as one `--llm-notes=...` word, with `%` and `$` doubled for systemd), like any flag given there: local users can read it with `systemctl cat`, and it overrides `notes` in the config until the unit is reinstalled. To keep it private and editable, set it in the config instead.
239
+ `[llm] notes` (or `--llm-notes TEXT`; `note` also works as the key, and `--llm-notes ''` clears a note set in the config) is a short note about this host, for example `loop/dm devices from pytest and /tmp/claude-*/scratchpad and OOMs in capped test containers are expected`. It is sent with every capture as `Operator notes about this host: ...`, outside the untrusted `BEGIN`/`END` block, and the system prompt tells the model that notes describe what is normal there. Any run of whitespace in it, newlines included, becomes one space. Given to `--install-service` while the config exists, the note goes on the unit's `ExecStart`, like any flag given there: local users can read it, and it overrides the config until the unit is reinstalled. Set it in the config to keep it private.
241
240
 
242
241
  The ntfy topic URL is not printed at startup or in error messages, since on a public ntfy server whoever knows the topic can subscribe to it. Only the server is shown. Keep the topic in the config file (mode `0600`) rather than on the command line: `--ntfy-url` shows in the process list, and when the config already exists, `--install-service --ntfy-url` puts it on the unit's `ExecStart`, which any user can read. `--install-service` warns when that happens.
243
242
 
@@ -265,6 +264,28 @@ Captures are posted whatever `push` says, so the service sees them all. Any 2xx
265
264
  Lines firewatcher writes back to the journal are a single physical line containing `firewatcher-self`, so a summary that quotes `panic` or `I/O error` does not start another capture. In journalctl and syslog output, firewatcher recognizes its own lines by the identifier field after the timestamp and host (`firewatcher[PID]:`), not by text elsewhere in the line, so a process or user name chosen to look like firewatcher cannot hide a kernel or daemon message. Lines in other formats are checked for `firewatcher-self` or `firewatcher[PID]:` anywhere. The capture files themselves stay in the log stream's original format.
266
265
 
267
266
 
267
+ ## Upgrading to 1.69 from 1.68
268
+
269
+ Nothing to change under a UTF-8 or C locale. What is different:
270
+
271
+ - After deleting old months to keep the free-space floor, firewatcher syncs only that filesystem (none on macOS), so a hard-mounted NFS share that is not answering, other than the one the captures are on, no longer stalls log reading. 1.68 synced every filesystem.
272
+ - When writing the unit, `daemon-reload` or `enable --now` fails, `--install-service` removes the config and the unit it created, puts back a unit it replaced as it was, and disables a unit it enabled, so the same command can run again. 1.68 left them, and the rerun put the command-line flags, the ntfy URL among them, on the unit's `ExecStart`. A config that does not fit on the disk is the same kind of failure; 1.68 wrote part of it and went on.
273
+ - At a restart, firewatcher first finds the saved line in the journal as it is now. When the journal lost the start of that second, the lines after the saved one are still matched; when it lost the line itself, every line from `--capture-time` before it is matched (no further back than `--resume-max`). 1.68 replayed them as context only, and with the clock set back by up to a minute, every line stamped before the saved second.
274
+ - `--tail-lines ''` is as if the flag were not given (the config's `tail_lines`, else 20), as an empty `tail_lines` in the config is. 1.68 passed it on, and `tail` or `journalctl` exited at once.
275
+ - Under a locale that is neither UTF-8 nor C (such as `en_US.ISO-8859-1`), a non-ASCII path in the config now names the file by its UTF-8 bytes, and a pattern file is read as UTF-8, where 1.68 used the locale's encoding for both: rename such a folder or file to its UTF-8 name, and save a pattern file as UTF-8 (one that is not is skipped, with a message). `--install-service` refuses a path or note typed there in the locale's own encoding (exit 2); 1.68 wrote it.
276
+ - On Python 3.6 under a C locale, non-ASCII paths (from the config, the command line or the unit), matched log lines and pattern files no longer stop firewatcher with `UnicodeEncodeError` or go unread, and `--install-service` writes a non-ASCII path or note given there as UTF-8, where 1.68 refused it. `--print-unit` writes the unit as UTF-8 in every locale (1.68 stopped on a non-ASCII `--requires-mounts-for` path) and refuses a value that is not UTF-8 (exit 2), which 1.68 printed or stopped on. On any Python, a path that is not UTF-8 no longer stops it at the first capture.
277
+
278
+ ## Upgrading to 1.68 from 1.67
279
+
280
+ Nothing to change. What is different:
281
+
282
+ - `--install-service` refuses a unit name systemd cannot load, and a path or value that is not UTF-8, before it writes anything (exit 2). 1.67 could write the config first and then stop with a traceback, or install a unit under a name systemd skips. A blank `--unit-name` now means `firewatcher`, as an empty one did.
283
+ - `systemctl enable --now` and `disable --now` get `--` before the unit name, so a name starting with `-` works.
284
+ - On Python 3.6 under a C locale, `--install-service` refuses a non-ASCII path or note given on the command line (exit 2, nothing written) and says to run under a UTF-8 locale; 1.67 stopped there with `UnicodeEncodeError`. 1.69 installs it.
285
+ - The package declares its license as the SPDX expression `GPL-3.0-or-later` when built with setuptools 77 or later.
286
+ - `--uninstall-service --unit-name` with a path (`a/b`) now prints a message and exits 2; 1.67 stopped with a traceback. It still takes any other name an older firewatcher may have installed.
287
+ - Dead code and duplicated rules are gone; nothing else an operator sees changed.
288
+
268
289
  ## Development
269
290
 
270
291
  ```bash
@@ -31,7 +31,7 @@ A directory argument loads every non-hidden pattern file inside it. Files ending
31
31
 
32
32
  `firewatcher` exits with an error when no pattern loads, for example when a pattern path is mistyped or every regex is invalid, instead of running while matching nothing.
33
33
 
34
- `--install-service` seeds `/etc/firewatcher/patterns.d` with example patterns when that directory is empty. The `examples/` directory in the source tree is not installed onto `PATH` by `pip install`; copy the pattern files from a clone, or let `--install-service` write them.
34
+ `--install-service` seeds `/etc/firewatcher/patterns.d` with example patterns when it holds no pattern file. The `examples/` directory in the source tree is not installed onto `PATH` by `pip install`; copy the pattern files from a clone, or let `--install-service` write them.
35
35
 
36
36
  On a match, `firewatcher` writes:
37
37
 
@@ -62,13 +62,13 @@ When firewatcher follows the journal (not a log file), it saves how far it has r
62
62
 
63
63
  To tell where it stopped, firewatcher reads the journal with `journalctl -o short-iso`, so journal lines in captures carry the year and time zone: `2026-10-23T17:52:41+00:00 host sshd[812]: …` instead of `Oct 23 17:52:41 host sshd[812]: …`. Lines logged while firewatcher was stopped arrive all at once. A capture opened by one of them takes its context from the `--capture-time` before that line by the time each line was logged, and holds the lines after it as they are read, up to `--capture-line-count-max`.
64
64
 
65
- The position is the second of the last line read, which line of that second it was, and a digest of that line. When unsure, firewatcher reads a line again rather than skip it. A run that started from `--tail-lines` in the middle of a second numbers that second's lines from where it started, so identical lines logged in that second can be captured again after a restart. A save that fails is logged once, and a restart then reads again from the last position saved. If the clock is set back after a position is saved, for example at a boot with a wrong hardware clock, new lines stamped before that second count as already read. A position more than a minute in the future is ignored.
65
+ The position is the second of the last line read, its place in that second, and a digest of it. When unsure, firewatcher reads a line again rather than skip it: identical lines logged in one second, and lines that journald stored in another order than the one they show, can be captured again after a restart, and when the journal no longer holds the saved line (its file was rotated away while firewatcher was stopped, or a crash or reboot lost it), every line from `--capture-time` before it is matched, and no further back than `--resume-max`. One case can skip lines: if the journal lost the saved line, or lines before it in its second, and holds a later one of that second with the same text, the replay can end there and the lines between are not matched ([limitations](https://github.com/yufei-pan/firewatcher/blob/master/docs/limitations.md#a-restart-after-the-journal-lost-the-saved-line-can-skip-lines-that-follow-it)). A failed save is logged once, and a restart then reads again from the last position saved. If the clock is set back after a save by up to a minute (a boot with a wrong hardware clock) and the journal still holds the saved line, the lines after it are still matched, even when they are stamped before the saved second. A position more than a minute ahead of the clock is ignored, and the last `--tail-lines` lines are read.
66
66
 
67
67
  Only one firewatcher uses a position file at a time: another one started with the same file, such as a test run in a terminal with the default settings, reads the last `--tail-lines` lines and saves nothing. An empty `--position-file` turns resuming off, and so does a journalctl that does not accept `-o short-iso` and `--since=@`, which firewatcher checks at start.
68
68
 
69
69
  ### Disk and memory limits
70
70
 
71
- firewatcher keeps the filesystems it writes captures to above `--min-free-percent` free (default 1%, counted as `df` does, so the root-reserved blocks are left alone). Only what the filesystem reports authorizes a write. firewatcher checks when a capture opens, about once a second, and again before what it has written since the last check could bring it within 64 KiB of the floor (and at least every 8 MiB), fsyncing first so the filesystem, an NFS server included, counts every byte. A line that would cross that margin is not written there. Another program filling the same disk between two checks can still push it past the floor; firewatcher's own writes do not.
71
+ firewatcher keeps the filesystems it writes captures to above `--min-free-percent` free (default 1%, counted as `df` does, so the root-reserved blocks are left alone). It asks the filesystem when a capture opens, about once a second, and before its own writes could come within 64 KiB of the floor (at least every 8 MiB), fsyncing first so an NFS server counts every byte; a line that would cross that margin is not written there. firewatcher's own writes never take the disk past the floor; another program filling it between two checks can.
72
72
 
73
73
  Near the floor, firewatcher may delete its own oldest months in that folder, so a new incident can be recorded, under these rules:
74
74
 
@@ -77,11 +77,11 @@ Near the floor, firewatcher may delete its own oldest months in that folder, so
77
77
  - **How much:** only as many months as it takes, counted by their size on disk, and nothing at all if deleting every one of them still could not make room. That happens when something else filled the disk, and then the history is kept.
78
78
  - **How often:** at most once per `--purge-interval` per folder (default an hour), so a disk that something else keeps filling drains firewatcher's history slowly. A purge that could not help is not tried again for five minutes (or the interval, if shorter). Each deletion is logged.
79
79
 
80
- After deleting, firewatcher syncs and reads the free space again. A filesystem that shows freed space late costs a move to the fallback folder, never the floor. If there is still no room, the capture moves to the fallback folder with a note, or, with no folder left, stops being written, which is logged; the incident is still pushed. Compression keeps to the floor too: a month whose archive could cross it is left uncompressed for now. Purging and compression never wait for each other, so log reading never waits for `tar`. `--delete-after-months 0` means nothing is deleted by age; the floor can still delete the oldest months. Set `--min-free-percent 0` to turn all of this off.
80
+ After deleting, firewatcher syncs that filesystem alone (syncfs; none on macOS, where APFS shows freed space at once) and reads the free space again. A filesystem that shows freed space late costs a move to the fallback folder, never the floor. If there is still no room, the capture moves to the fallback folder with a note, or, with no folder left, stops being written, which is logged; the incident is still pushed. Compression keeps to the floor too: a month whose archive could cross it is left uncompressed for now. Purging and compression never wait for each other, so log reading never waits for `tar`. `--delete-after-months 0` means nothing is deleted by age; the floor can still delete the oldest months. Set `--min-free-percent 0` to turn all of this off.
81
81
 
82
- Memory is bounded too. The `--capture-time` seconds before a match are kept in memory compressed (zlib, in blocks of about 64 KiB of text; with its bookkeeping, about 3 times smaller than the text for a busy host's varied lines, more for repetitive ones), within `--buffer-max-mb` (default 16 MiB). Past the limit the oldest block is dropped and the capture says so. The newest lines, up to 256 KiB of them before they are compressed, are always kept, so the buffer can go over the limit by about that much, or by one very long line. Compression costs well under a microsecond per line. A line longer than 1 MiB is read in pieces, and each piece is matched on its own, so a pattern split across the 1 MiB boundary is missed. The LLM excerpt reads at most `max_bytes` of a capture.
82
+ Memory is bounded too. The `--capture-time` seconds before a match are kept compressed (zlib, about 3 times smaller for a busy host's varied lines) within `--buffer-max-mb` (default 16 MiB); past it the oldest lines are dropped, and the capture says so. The newest lines, up to 256 KiB before they are compressed, are always kept, so the buffer can go over the limit by about that much. A line longer than 1 MiB is read and matched in pieces, so a pattern split across a 1 MiB boundary is missed.
83
83
 
84
- Monthly directories are compressed on a background thread, so log reading never waits for `tar`. Each month becomes `YYYY-MM.tar.xz` holding `YYYY-MM/…`. The directory is removed only after `tar` succeeds and the archive is synced to disk. An existing archive is never overwritten: the new one becomes `YYYY-MM.1.tar.xz`, and so on.
84
+ Monthly directories are compressed on a background thread, so log reading never waits for `tar`. Each month becomes `YYYY-MM.tar.xz` holding `YYYY-MM/…`. The directory is removed only after `tar` succeeds and the archive and its folder are synced to disk; if a sync fails, the month stays and is archived again later, nothing lost. An existing archive is never overwritten: the new one becomes `YYYY-MM.1.tar.xz`, and so on.
85
85
 
86
86
  ## systemd service
87
87
 
@@ -91,7 +91,7 @@ sudo firewatcher --print-unit
91
91
  sudo firewatcher --uninstall-service
92
92
  ```
93
93
 
94
- `--install-service` writes `/etc/systemd/system/firewatcher.service`, creates the output directory, seeds `/etc/firewatcher/patterns.d/` with example patterns **only if that directory is empty**, writes `/etc/firewatcher/firewatcher.conf` **only if that file is missing**, then `systemctl daemon-reload && systemctl enable --now firewatcher`.
94
+ `--install-service` writes `/etc/systemd/system/firewatcher.service`, creates the output directory, seeds `/etc/firewatcher/patterns.d/` with example patterns **only if it holds no pattern file**, writes `/etc/firewatcher/firewatcher.conf` **only if that file is missing**, then `systemctl daemon-reload && systemctl enable --now firewatcher`. It checks first, and writes nothing (exit 2) for a unit name systemd cannot load or a path or value that is not UTF-8. If writing the unit or a `systemctl` step fails (or `systemctl` cannot be run), it removes the config and the unit it created, puts back a unit it replaced as it was, and disables a unit it enabled, so the same command can run again. The output folder and the example patterns stay.
95
95
 
96
96
  `systemctl reload firewatcher` reloads the pattern files without a restart; see SIGHUP above.
97
97
 
@@ -187,7 +187,7 @@ always_notify =
187
187
 
188
188
  `patterns` and `always_notify` are one path per line, or comma-separated. Positional pattern arguments replace the `patterns` list.
189
189
 
190
- Out-of-range values stop `firewatcher` at startup: a negative time or count, `capture_time` over 7 days or `capture_time_max` over 30 days, `min_free_percent` outside 0–50, `purge_interval` or `resume_max` over 30 days, `buffer_max_mb` below 1, a timeout of 0 or less or over a day, an ntfy priority outside 1–5, or an LLM, ntfy or events URL that does not start with `http://` or `https://` or that contains spaces, or whose server part (after any `user:password@`) is not a host name with no `/`, `?`, `#`, `@`, `[`, `]`, `:` or `%` in it, or an IPv6 address in brackets, then an optional `:port` of up to 5 digits (0–65535); so a `user:password` holding a `/`, `?` or `#` is refused (`[` and `]` are fine there, since the `user:password` is stripped before the request). Also refused is a URL with a `user:password` that has another `@` later in its path, query or fragment (a password holding an `@` before a `/`, `?` or `#`). A password that starts with digits and a `/` still reads as a host and port, and those digits show as the port in logs ([limitations](https://github.com/yufei-pan/firewatcher/blob/master/docs/limitations.md#a-url-whose-password-starts-with-digits-and-a--shows-those-digits-as-the-port)).
190
+ Out-of-range values stop `firewatcher` at startup: a negative time or count, `capture_time` over 7 days or `capture_time_max` over 30 days, `min_free_percent` outside 0–50, `purge_interval` or `resume_max` over 30 days, `buffer_max_mb` outside 1–65536, a timeout of 0 or less or over a day, or an ntfy priority outside 1–5. So does an LLM, ntfy or events URL that is not `http://` or `https://`, has spaces, or whose server part is not a host name or a bracketed IPv6 address with an optional port (0–65535), as firewatcher reads it by its own strict rule. A `user:password` in a URL is never sent; one that would make the URL read two ways is refused, with [one exception](https://github.com/yufei-pan/firewatcher/blob/master/docs/limitations.md#a-url-whose-password-starts-with-digits-and-a--shows-those-digits-as-the-port).
191
191
 
192
192
  `api_key` under `[llm]` and `token` under `[notify]` are not command-line flags. `FIREWATCHER_LLM_API_KEY` and `FIREWATCHER_NTFY_TOKEN` override them when set. `events_token` under `[notify]` is likewise config-only, overridden by `FIREWATCHER_EVENTS_TOKEN`. `--install-service` creates `/etc/firewatcher/firewatcher.conf` with mode `0600` only when it is missing, and the unit runs `firewatcher --config` that path. An existing file is left as it is. Flags passed to a later install are appended on `ExecStart` so they override the file.
193
193
 
@@ -210,7 +210,7 @@ A capture that fits in `max_bytes` (default 48000) is sent whole. A bigger one i
210
210
 
211
211
  Any user or program on the host can write log lines, and those lines reach the model. The request marks the matched line and the excerpt as untrusted data between `BEGIN`/`END` lines that carry a random token, and the system prompt tells the model never to follow instructions inside them. That makes it harder for a log line to talk the model out of notifying, but cannot rule it out. For incidents that must always reach you, list patterns under `[notify] always_notify` (or `--always-notify`): when a line that matched a watch pattern also matches one of them, the capture is pushed even if the model says not to notify. That push uses the generated title and the line that matched, followed by the model's summary, since log text can steer the summary as well. If no always_notify pattern loads, firewatcher logs an error and keeps capturing without it; `--always-notify ''` clears a list set in the config. Only the line that opened the capture and later lines that matched a watch pattern while it was open count. Other lines in the window do not, and neither do matches from an earlier capture that appear as context. The capture's color codes are removed before it is sent to the model.
212
212
 
213
- `[llm] notes` (or `--llm-notes TEXT`; `note` also works as the key; the flag wins over the file, and `--llm-notes ''` clears a note set in the config) is a short free-text note about this host, for example `loop/dm devices from pytest and /tmp/claude-*/scratchpad and OOMs in capped test containers are expected`. It is sent with every capture as a line `Operator notes about this host: ...` before the untrusted log data, outside the `BEGIN`/`END` block, and the system prompt tells the model that notes describe what is normal there. Any run of whitespace in a note, newlines included, becomes one space. Only you can set it, so unlike the log lines it is trusted text; keep it short, since it is sent with every capture. When the config exists, `--install-service --llm-notes ...` puts the note on the unit's `ExecStart` (as one `--llm-notes=...` word, with `%` and `$` doubled for systemd), like any flag given there: local users can read it with `systemctl cat`, and it overrides `notes` in the config until the unit is reinstalled. To keep it private and editable, set it in the config instead.
213
+ `[llm] notes` (or `--llm-notes TEXT`; `note` also works as the key, and `--llm-notes ''` clears a note set in the config) is a short note about this host, for example `loop/dm devices from pytest and /tmp/claude-*/scratchpad and OOMs in capped test containers are expected`. It is sent with every capture as `Operator notes about this host: ...`, outside the untrusted `BEGIN`/`END` block, and the system prompt tells the model that notes describe what is normal there. Any run of whitespace in it, newlines included, becomes one space. Given to `--install-service` while the config exists, the note goes on the unit's `ExecStart`, like any flag given there: local users can read it, and it overrides the config until the unit is reinstalled. Set it in the config to keep it private.
214
214
 
215
215
  The ntfy topic URL is not printed at startup or in error messages, since on a public ntfy server whoever knows the topic can subscribe to it. Only the server is shown. Keep the topic in the config file (mode `0600`) rather than on the command line: `--ntfy-url` shows in the process list, and when the config already exists, `--install-service --ntfy-url` puts it on the unit's `ExecStart`, which any user can read. `--install-service` warns when that happens.
216
216
 
@@ -238,6 +238,28 @@ Captures are posted whatever `push` says, so the service sees them all. Any 2xx
238
238
  Lines firewatcher writes back to the journal are a single physical line containing `firewatcher-self`, so a summary that quotes `panic` or `I/O error` does not start another capture. In journalctl and syslog output, firewatcher recognizes its own lines by the identifier field after the timestamp and host (`firewatcher[PID]:`), not by text elsewhere in the line, so a process or user name chosen to look like firewatcher cannot hide a kernel or daemon message. Lines in other formats are checked for `firewatcher-self` or `firewatcher[PID]:` anywhere. The capture files themselves stay in the log stream's original format.
239
239
 
240
240
 
241
+ ## Upgrading to 1.69 from 1.68
242
+
243
+ Nothing to change under a UTF-8 or C locale. What is different:
244
+
245
+ - After deleting old months to keep the free-space floor, firewatcher syncs only that filesystem (none on macOS), so a hard-mounted NFS share that is not answering, other than the one the captures are on, no longer stalls log reading. 1.68 synced every filesystem.
246
+ - When writing the unit, `daemon-reload` or `enable --now` fails, `--install-service` removes the config and the unit it created, puts back a unit it replaced as it was, and disables a unit it enabled, so the same command can run again. 1.68 left them, and the rerun put the command-line flags, the ntfy URL among them, on the unit's `ExecStart`. A config that does not fit on the disk is the same kind of failure; 1.68 wrote part of it and went on.
247
+ - At a restart, firewatcher first finds the saved line in the journal as it is now. When the journal lost the start of that second, the lines after the saved one are still matched; when it lost the line itself, every line from `--capture-time` before it is matched (no further back than `--resume-max`). 1.68 replayed them as context only, and with the clock set back by up to a minute, every line stamped before the saved second.
248
+ - `--tail-lines ''` is as if the flag were not given (the config's `tail_lines`, else 20), as an empty `tail_lines` in the config is. 1.68 passed it on, and `tail` or `journalctl` exited at once.
249
+ - Under a locale that is neither UTF-8 nor C (such as `en_US.ISO-8859-1`), a non-ASCII path in the config now names the file by its UTF-8 bytes, and a pattern file is read as UTF-8, where 1.68 used the locale's encoding for both: rename such a folder or file to its UTF-8 name, and save a pattern file as UTF-8 (one that is not is skipped, with a message). `--install-service` refuses a path or note typed there in the locale's own encoding (exit 2); 1.68 wrote it.
250
+ - On Python 3.6 under a C locale, non-ASCII paths (from the config, the command line or the unit), matched log lines and pattern files no longer stop firewatcher with `UnicodeEncodeError` or go unread, and `--install-service` writes a non-ASCII path or note given there as UTF-8, where 1.68 refused it. `--print-unit` writes the unit as UTF-8 in every locale (1.68 stopped on a non-ASCII `--requires-mounts-for` path) and refuses a value that is not UTF-8 (exit 2), which 1.68 printed or stopped on. On any Python, a path that is not UTF-8 no longer stops it at the first capture.
251
+
252
+ ## Upgrading to 1.68 from 1.67
253
+
254
+ Nothing to change. What is different:
255
+
256
+ - `--install-service` refuses a unit name systemd cannot load, and a path or value that is not UTF-8, before it writes anything (exit 2). 1.67 could write the config first and then stop with a traceback, or install a unit under a name systemd skips. A blank `--unit-name` now means `firewatcher`, as an empty one did.
257
+ - `systemctl enable --now` and `disable --now` get `--` before the unit name, so a name starting with `-` works.
258
+ - On Python 3.6 under a C locale, `--install-service` refuses a non-ASCII path or note given on the command line (exit 2, nothing written) and says to run under a UTF-8 locale; 1.67 stopped there with `UnicodeEncodeError`. 1.69 installs it.
259
+ - The package declares its license as the SPDX expression `GPL-3.0-or-later` when built with setuptools 77 or later.
260
+ - `--uninstall-service --unit-name` with a path (`a/b`) now prints a message and exits 2; 1.67 stopped with a traceback. It still takes any other name an older firewatcher may have installed.
261
+ - Dead code and duplicated rules are gone; nothing else an operator sees changed.
262
+
241
263
  ## Development
242
264
 
243
265
  ```bash
@@ -0,0 +1,68 @@
1
+ # Lessons: full entries
2
+
3
+ Why this project's APIs, formats and agreed behaviours had to change. `lessons.md` has one line per
4
+ entry: read that first, and look entries up here by date and name rather than reading this whole. New
5
+ entries go last, in the same commit as the change:
6
+
7
+ ```markdown
8
+ ## YYYY-MM-DD <short name>
9
+ - Changed: ...
10
+ - Why we had to change: ...
11
+ - Original design was wrong because: ...
12
+ - Lesson: ...
13
+ ```
14
+
15
+ ## 2026-10-27 firewatcher: the model's excerpt is built from where the events are, not from the file's ends
16
+ - Changed: the excerpt of a large capture sent to the LLM is the header, the lines before the match, the lines after it and the end of the capture, located by byte offsets capture_messages records as it writes (GitHub 1.61/1.62 of 2026-10-23, first published to PyPI as 1.63). 1.60 sent the first 2 KB and the last ~46 KB.
17
+ - Why we had to change: captures hold 5 minutes before and after the match, so in 35 of 49 captures on one server in October the matched event was in the gap and the model judged an incident it never saw. Keeping the end matters too: on 2026-10-23 a failed backup's retry succeeded in the last minutes of the capture, and that is what kept the model from pushing.
18
+ - Original design was wrong because: it cut by position (start and end) a record whose important part sits at a known place in the middle, and it located nothing by what the writer already knew (where it wrote the match).
19
+ - Lesson: when a record must be cut to a budget, record where its important parts are while writing it, cut around those, and keep the end where outcomes show. A budget split must never shrink a piece when the budget grows (a superseded attempt that did kept producing new edge cases).
20
+
21
+ ## 2026-10-27 firewatcher: --requires-mounts-for paths are normalized as text, never resolved on disk
22
+ - Changed: in 1.64 each `--requires-mounts-for` path is made absolute and normalized as text (`.`, `..`, `//`, a trailing `/`), from its bytes, and quoted with `%` doubled. A path with a newline or carriage return, or one that is not UTF-8, is refused before anything is installed. Before, the path was written raw. Symlinks are not followed and the file system is not touched.
23
+ - Why we had to change: systemd splits an unquoted path at spaces, expands `%`, and ignores relative and unnormalized paths. So a unit could wait on the wrong mount, or on none, and start before an NFS share was up, with only a warning in the journal.
24
+ - Original design was wrong because: it passed operator text into a file with its own syntax (unit-file quoting, specifiers, path rules) as if it were plain text, and nothing checked what systemd read back. Two later attempts were wrong as well. Refusing `..` broke input that was accepted before. Resolving with realpath could follow a symlink off the remote share, hang on a dead hard-NFS mount, trigger automounts, and make the result depend on what was mounted at install time.
25
+ - Lesson: when you write a value into another program's file format, test what that program reads back for each odd input, against the real program. Transform the value only as the reading program would (here as text, like systemd), never by consulting state that may be absent or hung at the time you write.
26
+
27
+ ## 2026-11-03 firewatcher 1.65: the verdict moved to the receiver
28
+ - Changed: with `[notify] events_url` set, every finished capture is POSTed as JSON (with an excerpt of up to 16 KiB) and firewatcher's own LLM analysis is off, whatever `[llm] enabled` says; a startup line says so. The event's `push` is the rule-based floor, `always_notify`. `model_notify` stays in the event, always `null`. A 2xx answer means the receiver has the capture and nothing goes to ntfy. When the POST fails, only `always_notify` captures are pushed to ntfy; the others stay in the capture folder and the journal.
29
+ - Why we had to change: the first events_url design (before 1.65 was released) kept firewatcher's own LLM judging the push, and housekeepd followed it, so behaviour stayed the same while events_url was new. Once housekeepd had its own triage, with the host's tools and history, there were two judges, two verdicts, double LLM traffic, and an outside LLM service saw every capture.
30
+ - Original design was wrong because: it kept the judgment at the sender after a better-informed stage existed downstream.
31
+ - Lesson: when a pipeline gains a smarter stage downstream, move the judgment there whole, and keep only the rule-based floor (`always_notify`) at the source.
32
+
33
+ ## 2026-11-03 firewatcher 1.65: a URL is parsed once, by our own strict rule
34
+ - Changed: one strict check (`_is_http_url`) decides whether an LLM, ntfy or events URL is usable. The request and every log line use only what that check matched; a `user:password` is stripped and never sent or logged, and a URL is shown only as its server (`scheme://host[:port]`). Before 1.65 the check only needed an `http://` or `https://` start with no whitespace or control characters. Now firewatcher also stops at startup for: a server part that is not a host name (no `/`, `?`, `#`, `@`, `[`, `]`, `:` or `%`) or a bracketed IPv6 address, followed by an optional port of up to 5 digits (0 to 65535), judged also after Unicode NFKC normalization; a `user:password` that holds a `/`, `?` or `#`; and a URL with a `user:password` and another `@` later in its path, query or fragment. A URL without `user:password` may still hold an `@` in its path. With events_url set, a bad `[llm]` URL no longer stops firewatcher, since the LLM does not run.
35
+ - Why we had to change: a password holding an `@` before a `/`, `?` or `#` left the text between them as the host, and the host is logged. Review took five fix rounds on this check (credentials in requests and logs, unparseable URLs echoed, a password holding an `@`), and the last one replaced the library's view of the authority with our own check that gives the same answer on every Python.
36
+ - Original design was wrong because: it used a lenient library parser for a value that is both sent in requests and written to logs, and the old check accepted nearly anything with an http prefix.
37
+ - Lesson: when a value is both used for requests and written to logs, parse it once with our own strict rule that behaves the same on every runtime, not a lenient library parser. Record what that refuses that used to be accepted, as the entry above does.
38
+
39
+ ## 2026-11-14 firewatcher 1.68: --install-service checks and renders everything before it writes
40
+ - Changed: `--install-service` refuses a unit name systemd cannot load or enable (characters other than letters, digits and `: _ . @ - \`, more than 255 with the `.service` suffix, an `@` without a name before it or an instance after it) and a path or value that is not UTF-8 (on Python 3.6 under a C locale that is every non-ASCII one: it is refused, not restored, because the service under the same locale could not encode it either), with exit 2, before it creates the output folder, seeds patterns or writes the config or the unit. It renders the unit and the new config first and writes the text it rendered. A blank `--unit-name` means the default. 1.67 took any name as a file name and wrote the config before it had rendered the unit.
41
+ - Why we had to change: a name such as `a/b` stopped the install with a traceback after the config was written, and that left-over config made the next install render a different unit (flags on `ExecStart` instead of in the config). A name such as `fwé` was written as a file systemd skips, so `enable` failed, and with `--no-enable` the install reported success. A non-UTF-8 value stopped it with `UnicodeEncodeError`, again after the output folder and patterns were made.
42
+ - Original design was wrong because: it treated an install as a series of independent writes, so a step that failed late left the earlier ones behind as state the next run read (1.68 closes that for the checks and the rendering; a failed `daemon-reload` or `enable --now` after the writes still leaves the config and the unit, which a rerun reads: deferred to 1.69); and it checked the unit name only as a file name, not by the rules of systemd, which is what loads it.
43
+ - Lesson: an install that writes several files checks and renders all of them, by the rules of the program that will read each one, before it writes any, so a refusal leaves nothing behind. A step that fails after the writes (a systemctl call) still leaves them: a half-done install is state that changes what the next install does.
44
+
45
+ ## 2026-11-23 firewatcher 1.69: the service reads UTF-8 paths in any locale, so the install no longer refuses a non-ASCII one
46
+ - Changed: on Python 3.6 under a C locale, `--install-service` writes a non-ASCII path or note given on the command line (which Python hands over as escaped bytes) into the config or unit as the UTF-8 text its bytes spell; 1.68 refused it with exit 2. Bytes that are not UTF-8 are still refused. The service in turn takes each path from the config as the string Python's file calls take for its UTF-8 bytes, logs and sends escaped bytes as UTF-8 text, writes paths into captures and `journal.log` as their own bytes, prints what stdout cannot encode with backslash escapes, and reads pattern files as UTF-8 in every locale.
47
+ - Why we had to change: 1.68's refusal stopped one writer, an install from a C locale, but the service still died with `UnicodeEncodeError` on a config written under a UTF-8 locale and started under a C one, on a hand-written config, on a path given by hand, and on any matched log line that was not ASCII.
48
+ - Original design was wrong because: it refused the value where it was written because one reader, the service under that locale, could not use it, instead of fixing that reader. The refusal covered one path into the reader and left the others.
49
+ - Lesson: when a value is refused only because a later reader cannot handle it, fix the reader and drop the refusal: the refusal closes one way in and leaves every other way the value reaches that reader failing. A value that is not UTF-8 is still refused.
50
+
51
+ ## 2026-11-23 firewatcher 1.69: the replay finds the saved line in the journal as it is now
52
+ - Changed: before resuming, firewatcher reads the saved second of the journal once. The replay ends at the saved line by its number in that second when the journal still has it there, else, while the journal holds a line with its digest after that number, at that line, as in 1.68; else at the first line of that second with its digest; with no such line nothing is replayed and every line from `--capture-time` before it (no further back than `--resume-max`) is matched. 1.68 needed the number and the digest together, counted from the first line of that second it was shown, so a lost start of the second never reached the number.
53
+ - Why we had to change: when the journal lost the start of the saved second while firewatcher was stopped (a journal file rotated away), the number was never reached and the rest of that second was context only, never matched. When it lost the line itself and the clock came back behind it by up to a minute (a reboot with a volatile journal and a wrong hardware clock; beyond a minute the position is ignored and the last lines are read), every line stamped before the saved second was context, until the clock passed it.
54
+ - Original design was wrong because: it took a position counted from the start of a group (the lines of one second) as stable, in a log another program rotates while firewatcher is stopped, and its only way out of the replay was a later timestamp.
55
+ - Lesson: a position counted within a log that another program trims is checked against the log as it is now before it is trusted; when it cannot be found, read again rather than skip.
56
+
57
+ ## 2026-11-23 firewatcher 1.69: the saved second ends at a distance from it, not at the first line of another second
58
+ - Changed: reading the saved second of the journal (`locate`), firewatcher collects the lines shown as that second and stops at the first line shown more than `JOURNAL_STAMP_SKEW` (30) seconds after it. A line shown before it, however far, is passed over, because a stall of journald stores the lines that waited for it among the lines of the second: an earlier version of this rule stopped at any far line, and on the real journal's stalls 312 of 7,597 saved lines then ended the read before the saved line, the note said the journal no longer held it, and the whole window was matched again where 1.68 resumed exactly. 1.69 as built stopped at the first line of any other second. The replay itself (`replay_time`) still ends at the first line shown later than the saved second, as in 1.68: a line stored before the saved one and shown a second later ends it early, and lines read before the stop match again (a second capture, never a skip). Its stop rule is the 1.70 plan of the deferral in `limitations.md`, designed with the numbering that entry plans.
59
+ - Why we had to change: journalctl shows a line with the time it was sent but seeks and orders by the time journald stored it, a little later. So `--since=@S` can start with a line shown as the second before, and a line shown as S can follow one shown as S+1. Reading stopped at the first of them, the saved line was not found, and an ordinary restart matched the whole `--capture-time` again (a second capture and push of the last incident). On the journal of the host it was built on, 90 of 187,902 seconds that held lines were read that way, and 1,303 of 1,819 lines in them would have matched the whole window again. In 34.7 days of that journal 4.5 million entries carry a sent time: 99.995% were stored within a second of it, the largest gap outside two stalls of the host (370 and 458 s) was 15.7 s, and 30 covers that about twice.
60
+ - Original design was wrong because: it read the time a line shows as the order the lines come in, which is the stored time. The tests' fake journalctl sought by the time a line shows, so it could not tell the two apart.
61
+ - Lesson: a reader that stops on a timestamp it reads from the lines must know which time the program that orders the lines uses; stop on a distance from it on the later side only (a stall stores lines shown far earlier among the ones you want), and test against a source that keeps the two apart and against the real journal's stalls. Apply it to every reader of the same lines: the replay still stops at the first later line (1.70).
62
+
63
+ ## 2026-11-23 firewatcher 1.69: a failed install undoes what it did, in the order the tools need
64
+ - Changed: when a step of `--install-service` fails after the writes, it disables the unit if this run enabled it (it asks `systemctl is-enabled` before `enable --now`), then removes the config and the unit it created, puts back the old text of a unit it replaced, and reloads systemd. A `systemctl` that cannot be run, a file that cannot be removed and a config that does not fit on the disk are the same kind of failure, with a message. The config is written to its end or removed: 1.68 and 1.69 as built took a short write as done.
65
+ - Why we had to change: removing only what was created left a replaced unit pointing at the config that was removed (a restart loop on `config file not found`); `disable` came after the unit file was removed, which systemd 219 refuses (the wants link stayed); a unit this run replaced and its failed `enable --now` had enabled stayed enabled (the disable ran only for a unit the run created), and a disable after every failure would have disabled a unit that was enabled before the install; and systemd kept the removed unit's new text loaded until the next reload.
66
+ - Original design was wrong because: it undid the files, not the steps, and took a unit the install overwrote for something it had not touched.
67
+ - Lesson: an install that can fail part-way records what each step changes before it runs it (the old text, whether the unit was enabled), and undoes only that, in the order the program that reads the files needs.
68
+
@@ -0,0 +1,18 @@
1
+ # Lessons
2
+
3
+ What this project's past changes taught us. Read all of this before changing an API, a format or an
4
+ agreed behaviour here. For the full story of a line, find the entry with the same date and name in
5
+ `lessons-full.md`.
6
+
7
+ When one has to change, add the full entry to `lessons-full.md` and one line here, newest last, in the
8
+ same commit as the change. Never merge lines to save space; before this file passes 80 KB (what one Claude Code read returns, measured 2026-11-15), ask the user.
9
+
10
+ - **2026-10-27 firewatcher: the model's excerpt is built from where the events are, not from the file's ends**: when a record must be cut to a budget, record where its important parts are while writing it, cut around those, and keep the end where outcomes show. A budget split must never shrink a piece when the budget grows (a superseded attempt that did kept producing new edge cases).
11
+ - **2026-10-27 firewatcher: --requires-mounts-for paths are normalized as text, never resolved on disk**: when you write a value into another program's file format, test what that program reads back for each odd input, against the real program. Transform the value only as the reading program would (here as text, like systemd), never by consulting state that may be absent or hung at the time you write.
12
+ - **2026-11-03 firewatcher 1.65: the verdict moved to the receiver**: when a pipeline gains a smarter stage downstream, move the judgment there whole, and keep only the rule-based floor (`always_notify`) at the source.
13
+ - **2026-11-03 firewatcher 1.65: a URL is parsed once, by our own strict rule**: when a value is both used for requests and written to logs, parse it once with our own strict rule that behaves the same on every runtime, not a lenient library parser. Record what that refuses that used to be accepted, as the entry above does.
14
+ - **2026-11-14 firewatcher 1.68: --install-service checks and renders everything before it writes**: an install that writes several files checks and renders all of them, by the rules of the program that will read each one, before writing any, so a refusal leaves nothing behind; a step that fails after the writes (a systemctl call) still leaves them, and a half-done install is state that changes what the next install does.
15
+ - **2026-11-23 firewatcher 1.69: the service reads UTF-8 paths in any locale, so the install no longer refuses a non-ASCII one**: when a value is refused only because a later reader cannot handle it, fix the reader and drop the refusal: the refusal closes one way in and leaves every other way the value reaches that reader failing. A value that is not UTF-8 is still refused.
16
+ - **2026-11-23 firewatcher 1.69: the replay finds the saved line in the journal as it is now**: a position counted within a log that another program trims is checked against the log as it is now before it is trusted; when it cannot be found, read again rather than skip.
17
+ - **2026-11-23 firewatcher 1.69: the saved second ends at a distance from it, not at the first line of another second**: a reader that stops on a timestamp it reads from the lines must know which time the program that orders the lines uses; stop on a distance from it on the later side only (a stall stores lines shown far earlier among the ones you want), and test against a source that keeps the two apart and against the real journal's stalls. Apply it to every reader of the same lines: the replay still stops at the first later line (1.70).
18
+ - **2026-11-23 firewatcher 1.69: a failed install undoes what it did, in the order the tools need**: an install that can fail part-way records what each step changes before it runs it (the old text, whether the unit was enabled), and undoes only that, in the order the program that reads the files needs.
@@ -0,0 +1,80 @@
1
+ # Limitations
2
+
3
+ Approved known limitations and deferrals. Reviewers: read this first, and don't raise these again.
4
+
5
+ ### On systemd 229 and older, some mount paths are written wrong
6
+ - Kind: known limitation
7
+ - What the operator sees: on systemd 229 and older (CentOS/RHEL 7's 219, Ubuntu 16.04's 229), a
8
+ `--requires-mounts-for` path holding `%`, `"` or `\` is written into `RequiresMountsFor=` in a form those
9
+ versions read as a different path: they keep 1.64's `%%` and its backslash escapes as they are. The unit then
10
+ does not wait for that mount. 1.63 wrote such a path unchanged, which those versions read correctly. Paths
11
+ with spaces work there too.
12
+ - Why: those versions neither expand `%` specifiers nor unescape in `RequiresMountsFor=` (systemd 219's
13
+ `config_parse_unit_requires_mounts_for` splits on whitespace and quotes, nothing more); newer ones (239 and
14
+ later) do both, and 1.64 writes for them. Writing per systemd version would mean detecting the version, for
15
+ an end-of-life systemd and a mount path that holds one of these three characters.
16
+ - Revisit when: someone runs firewatcher on such a systemd with such a mount path.
17
+ - Approved: 2026-11-01, the user chose "Known limitation (Recommended)".
18
+
19
+ ### While the events receiver is down, each alert waits one timeout before ntfy
20
+ - Kind: known limitation
21
+ - What the operator sees: with `events_url` set and its receiver not answering (the host down, its packets
22
+ dropped), each alert (only `always_notify` captures fall back to ntfy) is pushed one `ntfy_timeout` late (10 s by default; twice that for a capture that
23
+ overflows), because firewatcher tries the POST first and pushes to ntfy only when it fails. No alert is lost.
24
+ - Why: the POST and the push run one after the other. A separate short timeout, or skipping the POST for a while
25
+ after a failure, would shorten the wait at the cost of a second setting or of state; a few seconds' delay while
26
+ the receiver is down was judged acceptable.
27
+ - Revisit when: the delay matters in practice (a burst of alerts while the receiver is down).
28
+ - Approved: 2026-11-01, the user chose "Known limitation".
29
+
30
+ ### A URL whose password starts with digits and a `/` shows those digits as the port
31
+ - Kind: known limitation
32
+ - What the operator sees: in a URL such as `http://u:2024/<rest>@h/e` (a `user:password` whose password starts
33
+ with digits followed by `/`), the text up to the `/` is a valid host and port (`u`, port 2024), so firewatcher
34
+ accepts it and its log lines show `http://u:2024`: the leading digits of the password appear as the port, and the
35
+ request goes to host `u`. The same holds for a password that starts with `/`, `?` or `#` (`http://usr:/…@h/e`
36
+ goes to host `usr`). Because such a URL reads as a host and port, firewatcher gives it no user:password warning.
37
+ - Why: such a URL is valid as written; refusing it would mean refusing an `@` in a URL's path, which valid LLM URLs
38
+ use. Credentials in a URL are never sent anyway (README: use `events_token`, `token` or `api_key`).
39
+ - Revisit when: a way to tell such a URL from a real host and path appears, or someone hits it.
40
+ - Approved: 2026-11-01, the user ticked "fw: digits shown as port". The no-warning sentence added 2026-11-04, the user: "Add it to the wording".
41
+
42
+ ### A restart after the journal lost the saved line can skip lines that follow it
43
+ - Kind: deferred
44
+ - What the operator sees: when the journal lost the saved line, or lines before it in its second (a rotated file, a crash or a reboot), but still holds a later line of that second with the same text, the replay can end at that twin. The lines between the lost line and the twin were not read before the stop, and on the restart they are read as context only, not matched. 1.68 did the same. A message logged several times in one second is the usual twin.
45
+ - Also the replay's own stop: it ends at the first line shown later than the saved second, as in 1.68 (the 30 s distance rule collects the saved second for `locate` only). journalctl orders lines by the time journald stored them, so a line stored before the saved one and shown a second later ends the replay there, and the lines stored from it up to the saved one, read before the stop, match again: a second capture and push of an incident, never a skip.
46
+ - Where `locate` cannot tell which of identical lines is the saved one it takes the first, so 1.69 reads a line again where 1.68 was exact, in two rare views: the journal lost the start of the saved second, holds an identical line before the saved one and nothing of that second after it (1.69 reads the lines after that earlier line again, 1.68 resumed exactly; with a line after the saved one 1.68 skipped it, and 1.69 reads it as new); and a line shown as the saved second but stored a second before it, with an identical line before the saved one (`locate` reads the second from its start and does not see that line, so the saved line is read again). 1.69 takes a duplicate over a skip, and the 1.70 design below settles all three.
47
+ - Why: the position holds the second of the saved line, its number in that second and a digest of its text, and a twin has the same digest and can have the same number. The replay stops on a time shown in the lines, which is not the order the lines come in.
48
+ - Plan (1.70), one change to `locate` and `replay_time` together, tested against a journalctl that orders lines by the time they were stored:
49
+ - Save the digest of the line before the saved one as well, number lines within a run of lines with the same stamp (as `saw()` does), and take a line as the saved one only where its number and the digest of its line before agree; otherwise treat the saved line as lost and read the window again. A burst of identical lines has identical lines before it, so the number tells them apart where the digest cannot.
50
+ - Where `locate` found the saved line, end the replay at that line (counted among all lines of its second, as `replay_time` counts) and not at the first line shown later, up to JOURNAL_STAMP_SKEW seconds past the saved second; where it could not tell, end as now, at the first line shown later (a later end there would skip lines).
51
+ - Design check first (1.70): weigh resuming by journal cursor before adding any of this saved state. journalctl 219 (CentOS 7) has `--after-cursor` and `--cursor`; a cursor for every line needs `-o json` or `-o export`, so firewatcher would format each line itself, which is a real cost. A cursor names the line itself, so the saved number and digest, and with them the twin, would go. Say why not if not.
52
+ - Revisit when: 1.70.
53
+
54
+ ### A restart where a line shown far later was stored first says the journal no longer holds the last line
55
+ - Kind: deferred
56
+ - What the operator sees: when journald stored a line shown more than 30 seconds (JOURNAL_STAMP_SKEW) after the saved second before one of that second's lines (a source whose clock runs ahead; none in 34.7 days of the journal this was measured on), `locate` stops reading there, may not reach the saved line, and the note says "The journal no longer holds the last line read", though it does. Every line from `--capture-time` before the saved second is matched again: a second capture and push of every incident in that window, never a skip. 1.68 ended its replay at that line, and read the lines from it again. A stall of journald does not do it: it stores lines shown far earlier (370 and 458 s on that journal) among the lines of the second, `locate` reads past them, and the restart resumes exactly, as in 1.68.
57
+ - Why: `_journal_second` returns only the lines it collected, so `locate` cannot tell a stop at a far later line from the end of the journal.
58
+ - Plan (1.70): `_journal_second` says that it stopped at a far line, and `locate` writes a note of its own ("stopped reading at a line N seconds after the saved second; matching every line from ..."). The cursor design of the entry above would take `locate` away.
59
+ - Revisit when: 1.70.
60
+
61
+ ### `--print-unit` does not run the install's checks of the unit name and the config text
62
+ - Kind: deferred
63
+ - What the operator sees: `--print-unit --unit-name 'a b'` prints a unit and exits 0, where `--install-service` refuses the name (exit 2); a config text the install would refuse (not UTF-8) is never rendered by `--print-unit`. It refuses a value that is not UTF-8 in the unit itself, as the install does. 1.68 did the same.
64
+ - Why: the install runs its checks in `install_service`; the print path calls `render_unit_file` only.
65
+ - Plan (1.70): one function runs the checks and renders the unit and the config text, and both commands call it.
66
+ - Revisit when: 1.70.
67
+
68
+ ### A dangling symlink at the unit path is written through, and its target is kept when the install fails
69
+ - Kind: deferred
70
+ - What the operator sees: with a dangling symlink at `/etc/systemd/system/firewatcher.service`, `--install-service` writes the unit at the link's target (the open follows the link), and a step that fails afterwards leaves that file, naming the config the clean-up removed. Older than 1.69.
71
+ - Why: the clean-up counts a path that exists as a link as a unit that was there before, and keeps nothing of it to put back.
72
+ - Plan (1.70): a path that is a link (`os.path.lexists`) but no file (`os.path.isfile`) counts as created, and a failure removes what the install wrote through it.
73
+ - Revisit when: 1.70.
74
+
75
+ ### A byte-order mark at the start of a pattern file stays in its first pattern
76
+ - Kind: deferred
77
+ - What the operator sees: a pattern file saved with a UTF-8 byte-order mark (some Windows editors do) has U+FEFF at the start of its first pattern, which then never matches, with no message. Older than 1.69.
78
+ - Why: pattern files are read as `utf-8`, which keeps the mark.
79
+ - Plan (1.70): read them as `utf-8-sig`.
80
+ - Revisit when: 1.70.