Record a job done only once the printer confirms it

Breaking: a job reads done only once the printer has confirmed that it
ran it, and holds the printer's connection until then. A job written in
full that the printer does not confirm within printer.write_timeout now
reads uncertain and is not sent again; before, it read done. Before
each job, nothing is sent until the printer answers that it can print,
so with its cover open or no paper a job is retried and fails, with no
sent_at, as when the printer is unreachable, where before it read done.
Receipts that take longer than write_timeout to print need a larger
one. With confirmation the daemon also turns the printer's automatic
status off (GS a 0) before each job and before its confirmation, so
another client that relies on automatic status from the same printer
stops getting it. printer.confirm = false keeps the old meaning of done
and sends none of this.

The third paper check of the job store showed why: with the
TM-T88IV's cover open, Windows took all of a 128 KB receipt into its
send buffer, so the job read done; the daemon stopped, and once the
cover closed only the first third printed, without its cut. The printer
was then left inside the image command, waiting for the rest, until it
was switched off and on.

The daemon now turns on the printer client's confirmation. The queue
treats an unconfirmed send as written but unknown: it records the
delivery uncertain, with an error that says the printer did not confirm
it and that it may have printed in full, in part or not at all, logs it
at WARN with the printer, and neither retries it nor reports it as a
failure. Its commit is not synced: lost, it rolls back to sending,
which the next start turns uncertain too. A printer that is not ready
gives a plain error before the sending mark, which the queue retries as
a failed dial.

The new key printer.confirm, on by default, turns confirmation off for
a printer that does not answer GS r with bits 4 and 7 clear, or does not
answer DLE EOT on receipt; only the TM-T88IV is checked. The key's
comments in config.go and the example config say what it sends and
needs. docs/api.md warns that raw bytes which make the printer answer
can be taken for the confirmation, and says that /healthz answers 503
timeout while a job holds the connection. It and design.md say that
each job waits about 200 ms for answers still owed, for DLE EOT 1 to
say that the printer is online, and for no answer to come late.
design.md says why each connection opens with DLE EOT 1, GS a 0 and
GS r 1, and names what stays open: a raw job holding a command answered
with bits 4 and 7 clear, such as GS r, GS I, ESC v or ESC u 0, with
printing after it, and an answer taken for the ready answer that is not
it, one still queued when the fence arrived or one owed from before a
late DLE EOT answer that stood in for the fence, followed by a pause of
more than 100 ms while the printer reads online, as might happen during
the TM-m50II's feed and cut at cover close. It relies on the printer
answering DLE EOT on receipt and running GS a 0 in order,
unverified beyond the TM-T88IV until stage 4a's raw-byte probe, and now
says a send that fails part-way can print a whole receipt twice. The
unit and install.md give the stop timeout with and without state.dir:
without it the job being sent runs on from its rendering, image fetches
included, up to the job's 2-minute cap, so such hosts keep systemd's
default or set state.dir. configuration.md says the online check can
add 2 s to write_timeout. The printers plan says what Send now does
before a job, and its table of what a failed send can mean gains the
ready wait and the confirmation.

The tests' fake printers answer DLE EOT 1 and GS r 1 as an online
printer with paper does; a test runs the daemon against one that
answers that it is ready but never confirms the job. On paper, a
receipt read done 884 ms after it was written, when the printer
answered, and a receipt whose daemon was killed while it waited, with
the cover open, read uncertain after the restart and printed once, cut
short. With GS a 0 and DLE EOT 1, a receipt printed once with nothing
extra and read done 888 ms after it was written; after a power cycle,
with /healthz polled every 250 ms, one sent with the cover open waited
15.7 s with nothing sent and printed once, cut once, when the cover
closed. With the fence, a receipt printed once with nothing extra and
read done 921 ms after it was written.

Part of stage 1d of docs/plans/job-store.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
57_WolveandClaude Opus 5.5 committed 2026-12-01 21:28:38 -06:00
1 parent 9376653e1a
commit d3c3025e64
19 files changed
+429 -68

No files matched your search

+106 -9
View File
@@ -47,8 +47,102 @@ client ─HTTPS─▶ Caddy ─▶ server ── validate ──▶ printqueue (
`ref`. It is never sent again; a person checks the paper, and a
reprint is a new request. A crash between the mark and the write
leaves an `uncertain` receipt that never printed, the safe direction.
`queued` never reached the printer, `done` was written in full, and a
`failed` job reached it only if `sent_at` is set.
`queued` never reached the printer, and a `failed` job reached it only
if `sent_at` is set.
- **Done means the printer confirmed it (owner, 2026-12-01).** A write
that returned says only that the operating system holds the bytes. On
the TM-T88IV with its cover open, Windows took a whole 128 KB receipt
into its send buffer at once; the daemon stopped, and once the cover
closed only the receipt's first third printed, uncut, and the printer
waited for the rest of the image, so the next receipt would have
printed as noise until it was switched off and on. So the client asks
with `GS r 1`, which the printer answers only once it has run every
byte before it, and, on the TM-T88IV, only while it can print
(`printer.confirm`):
- Before the `sending` mark it sends `DLE EOT 1`, `GS a 0` and `GS r 1`
in one write. The printer answers `DLE EOT` on receipt, ahead of
anything it receives after it, so that answer is a fence: everything
before it left the printer before the request reached it, and is
dropped, however long a lost segment holds the request back. That
takes care of answers owed to requests the printer ran earlier, on
an earlier connection or for an earlier run of the daemon, and of the
last bytes of an automatic status block whose header went to an
earlier connection. A late answer to an earlier connection's
`DLE EOT` looks like the fence's and can stand in for it; the last
check below catches what follows from that. It then waits, up to
`printer.write_timeout`, for an answer to `GS r 1` after the fence.
Without it, as with the cover open, paper out or a printer stuck
inside a cut-short image, nothing is sent, and the job retries as
after a failed dial and fails with no `sent_at`. Requests still
queued in the printer ahead of this one are answered first, and the
answers carry nothing that tells them apart, so the wait reads every
answer until 100 ms have passed since the last and drops them all.
Only answers keep the wait open, and one that the deadline or the
connection's end cuts short counts as not ready. A printer may run
`GS r 1` while offline, as the TM-m50II's settings may allow, so it
then asks `DLE EOT 1` again, for up to 2 s beyond
`printer.write_timeout`, and sends the job only if the printer
answers that it is online. Last, it reads on for 100 ms. Once the
answers have settled none is owed, so an answer to `GS r 1` during
the online check or after it means that one taken for the ready
answer was not, as when a late `DLE EOT` answer stood in for the
fence and an owed answer followed it; then nothing is sent, and the
job is tried again. Each job waits about 200 ms for these checks.
- After the job it sends `GS a 0` and `GS r 1` again, and the job is
`done` only on that answer. Without it within
`printer.write_timeout`, the job is `uncertain`, with a WARN line,
and never sent again. A receipt that takes longer to print than that
reads `uncertain` though it printed, and the job's 2-minute bound
caps the timeout.
- Automatic status blocks: the UB-E02 and UB-E03 boards turn them on
at power-on, and a job's `ESC @` turns them off, but not on a
TM-T88VII with DIP switch 2-1 on. A block's last three bytes have the
`GS r` answer's fixed bits, and the board can send them on the next
connection. So the client sends `GS a 0` before each `GS r 1`; the
printer runs it in order, so no block follows the answer on that
connection. Bytes of blocks before the fence are dropped with the
rest; blocks after it are passed over by their header's fixed bits,
a block split across reads included, and after the confirmation the
client reads on for up to 100 ms for the rest of a block it ended
inside. `Status` sends no `GS a 0`, which the printer does not run
while offline; before it closes, it reads what has come, to the end
of any block.
- Not `DLE EOT` for the confirmation: it is answered at once, and an
image's bytes can hold it, which the printer answers too; such
answers are told from the `GS r` answer by their fixed bits (bits 4
and 7 clear). The TM-T88IV answers `0x00`; Epson's pages also give a
form with bit 4 set, which a model must not use unless it runs with
`printer.confirm = false`, unchecked on the TM-T88VII and TM-m50II.
`printer.confirm` also needs a printer that answers `DLE EOT` on
receipt.
- What remains open, which an acknowledgement that names the job,
`GS ( H` fn 48 (phase 5), would close:
- A `raw` job holding a command the printer answers with a byte
whose bits 4 and 7 are clear, such as `GS r`, `GS I`, `ESC v` or
`ESC u 0`, with printing after it, is confirmed by that answer
before it has printed. Answers it leaves owed, far enough apart,
can do the same to the next job. Only a token issued with `--raw`
can send one.
- An answer taken for the ready answer that is not it: the answer to
a request still queued in the printer when the fence arrived, or,
when a late `DLE EOT` answer stood in for the fence while this
connection's request was held back, one the printer owed from
before. The printer runs queued requests back to back, so the real
ready answer follows within the settle, which drops both, or
within 100 ms of the online answer, which sends nothing, unless
printing comes between them, which only a `raw` job puts there, or
the printer pauses longer while it reports online. The TM-m50II
feeds and cuts when its cover closes; if it reports online before
it has run the queued requests and pauses between them, the late
ready answer confirms the job before it has printed.
- The client relies on the printer answering `DLE EOT` on receipt,
ahead of the `GS r 1` sent just before it, and running `GS a 0` in
order, and on how the board routes bytes across connections. The
TM-T88IV answers `DLE EOT`; the rest is unverified, and stage 4a's
raw-byte probe (docs/plans/printers.md) checks it.
- A job holds the printer's connection until it has printed, so
`/healthz` answers `503` `timeout` during a receipt that takes more
than 3 seconds.
- **Deliveries.** A job has one delivery per printer, one for `default`
until several printers are configured, each with its own state,
attempts and times. A job's `status` derives from them: `queued` while
@@ -141,10 +235,11 @@ client ─HTTPS─▶ Caddy ─▶ server ── validate ──▶ printqueue (
untested on ARM hardware: the owner has none, modernc's CI names no
ARMv6 target, and older SIGSEGV reports for linux/arm exist. CI
cross-compiles them on every push.
- **Acknowledgement.** Epson documents one, `GS ( H` fn 48, answered
once a job has printed, which would turn most `uncertain` jobs into
confirmed prints. Whether the TM-T88IV's network board passes it to
port 9100 is unverified, so it waits for a paper test
- **Acknowledgement with an ID.** Epson also documents `GS ( H` fn 48,
answered with an ID of the host's choosing once a job has printed,
which would tell one job's answer from another's even across
connections. Whether the TM-T88IV's network board passes it to port
9100 is unverified, so it waits for a paper test
(docs/plans/job-store.md, phase 5). The state machine is safe without
it.
- **Render, then send.** The handler validates the whole job, and a `400`
@@ -153,8 +248,9 @@ client ─HTTPS─▶ Caddy ─▶ server ── validate ──▶ printqueue (
sends that stream over one connection. A failure before the send, such as a
broken image, costs no paper and leaves no half-printed receipt. A retry
resends the same bytes and does not render again. A send that fails
part-way can print part of a receipt twice. That is accepted, because
retries are what let a job survive a printer that is briefly offline.
part-way can print a receipt, or part of one, twice. That is accepted,
because retries are what let a job survive a printer that is briefly
offline.
- **Blank paper before the cut (owner, 2026-11-30).** The cut, `GS V 66 0`,
feeds the last printed line just past the cutter, so the cut fell right
under it. `printer.cut_feed` blank lines (3) now feed before every cut.
@@ -579,7 +675,8 @@ before anything is built. They come in this order.
the owner accepted every proposed default, 2026-12-01). The store is
built (Decisions, "Jobs survive a restart"); what is left:
- `GET /api/v1/jobs` lists the history, job status shows `ref`, and
`/healthz` counts the jobs found `uncertain` at the latest start.
`/healthz` counts the jobs that became `uncertain` since the latest
start, at it or during the run.
- `DELETE /api/v1/jobs/{id}` cancels a waiting job, for full tokens. An
ntfy `message_delete` cancels the waiting receipts of its
notification (`Queue.Cancel`); `message_clear` does not, and the