diff --git a/docs/design.md b/docs/design.md index a207890..d880fdf 100644 --- a/docs/design.md +++ b/docs/design.md @@ -115,8 +115,10 @@ client ─HTTPS─▶ Caddy ─▶ server ── validate ──▶ printqueue ( `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: + - Two cases are accepted as they are, by design (the next decision). + In each a job can read `done` before it has printed, and the daemon + does not try to catch it; only an acknowledgement that names the + job, `GS ( H` fn 48 (phase 5), could tell it apart: - 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 @@ -135,14 +137,39 @@ client ─HTTPS─▶ Caddy ─▶ server ── validate ──▶ printqueue ( 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. + - The client relies on the printer answering `DLE EOT` on receipt, + ahead of the `GS r 1` sent just after it, running `GS a 0` in order, + and never answering `DLE EOT` between the bytes of a status block, + 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. + - **The daemon never reprints (owner, 2026-12-01).** It does not reprint + a job by itself, not when a cover closes and not at start. An + `uncertain` job, and a job that reads `done` in one of the two cases + above, are left as they are on purpose: the daemon cannot tell a + receipt that printed from one that is missing, so a reprint of its own + would risk a duplicate. A reprint when the cover closes would not even + reach the second case, whose job reads `done`; the printer resumes its + own buffer when the cover closes (paper check 3), so it would print + the receipt twice; and after a cut-short image the printer can still + be inside the image command. A person who finds a receipt missing + reprints it through the API instead. `POST /api/v1/jobs/{id}/reprint` + reprints one stored job by its ID, as often as asked. In place of the + ID, which can never be one of them, `last` reprints the last completed + job, one that read `done`, never one still waiting or printing; + `failed` retries the last failed print; and `uncertain` reprints every + `uncertain` job. A job one of these categories reprints is marked, so + no category takes it again, and each answer names the new job, so a + person can print it again by its ID. A reprint takes a job from any + source: a print or module route, an ntfy card or digest, and later a + webhook. The routes take every input in the JSON body, never in a + query parameter, and the ID or category in the path is the route + itself. No message a source receives, an ntfy message included, makes + the daemon print a job again. Phase 2 of docs/plans/job-store.md + builds the routes (its decision 24). - **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 @@ -685,6 +712,15 @@ before anything is built. They come in this order. - A PNG preview draws the paper one pixel per dot, box drawing from the cell geometry and other glyphs from a bitmap font. Stored jobs preview by ID. + - `POST /api/v1/jobs/{id}/reprint` reprints a stored job by its ID, + and by category `/jobs/last/reprint` the last completed job, + `/jobs/failed/reprint` the last failed one and + `/jobs/uncertain/reprint` every `uncertain` one, each marked so that + no category takes it again: the user-side path for `uncertain` jobs + and any receipt that went missing, since the daemon never reprints by + itself and no message asks it to (owner, 2026-12-01; the plan's + decision 24). Its details are the plan's questions 25-39, which await + the owner. - Epson's acknowledgement, ntfy state in the store and an age limit wait for phase 5. 2. **Several printers**: `docs/plans/printers.md` (written; the owner diff --git a/docs/plans/job-store.md b/docs/plans/job-store.md index a17413d..65c7e88 100644 --- a/docs/plans/job-store.md +++ b/docs/plans/job-store.md @@ -1,6 +1,6 @@ # Plan: persistent job store and image previews -Status: **in progress; phase 1 is done: stages 1a-1d measured SQLite on leil-rig, added `[state] dir`, built the state machine and the SQLite store, so jobs survive a restart and a restart never prints a receipt twice. Phases 2 to 5 are not built.** The owner accepted every proposed default on 2026-12-01 (Decisions). design.md records what phase 1 built (Decisions, "Jobs survive a restart") and, as its Planned next item 1, what is left. Each stage runs in its own session, briefed from its section: stage 1a follows decisions 1-3, stage 1b decision 4, stage 1c decisions 5-12, stage 1d decisions 13-15, phase 2 decision 16, phase 2b decisions 17 and 18, phase 3 decision 19 and phase 4 decision 20; phase 5 holds decisions 21-23. Phases 3a-3c need nothing from phases 1 and 2, so they can come first if the owner wants previews sooner. Phase 2b needs phase 1 but not phase 2, so it can come before phase 2. Every check on the live printer needs the owner's OK. +Status: **in progress; phase 1 is done: stages 1a-1d measured SQLite on leil-rig, added `[state] dir`, built the state machine and the SQLite store, so jobs survive a restart and a restart never prints a receipt twice. Phases 2 to 5 are not built.** The owner accepted every proposed default on 2026-12-01 (Decisions). design.md records what phase 1 built (Decisions, "Jobs survive a restart") and, as its Planned next item 1, what is left. Each stage runs in its own session, briefed from its section: stage 1a follows decisions 1-3, stage 1b decision 4, stage 1c decisions 5-12, stage 1d decisions 13-15, phase 2 decisions 16 and 24 and the open questions 25-39, phase 2b decisions 17 and 18, phase 3 decision 19 and phase 4 decision 20; phase 5 holds decisions 21-23. Phases 3a-3c need nothing from phases 1 and 2, so they can come first if the owner wants previews sooner. Phase 2b needs phase 1 but not phase 2, so it can come before phase 2. On 2026-12-01 the owner also decided that the daemon never reprints a job by itself and that a person reprints jobs through `POST /api/v1/jobs/{id}/reprint`, by ID or by category with `last`, `failed` or `uncertain` in place of the ID, each job a category route reprints marked so that none takes it again, and with no in-band reprint message (decision 24); its details are open questions 25-39, which await him, and phase 2 builds them once he has answered or accepted their assumed defaults. Every check on the live printer needs the owner's OK. ## Decisions (owner, 2026-11-30) @@ -101,6 +101,17 @@ For phase 2: - Job status gains `ref`, so a client can find the job an ntfy message became. - `/healthz`'s `queue` gains `uncertain`: the jobs found `uncertain` at the latest start, those with a delivery that `Load` turned from `sending`, and those the printer has not confirmed since (stage 1d). From remote-printers stage 4 it also counts the jobs a remote send has left `uncertain` since that start. A clean restart sets it back to 0, so it does not stay up for about 1,000 jobs. +24. **Reprinting by ID and by category (owner, 2026-12-01).** The daemon never reprints a job by itself (design.md, "The daemon never reprints"): it cannot tell a receipt that printed from one that is missing, so a reprint of its own would risk a duplicate. A person does, through the API, under `/api/v1/jobs/.../reprint`: + - `POST /api/v1/jobs/{id}/reprint` reprints one stored job by its ID. It is the deliberate path: it takes a job a category route has already reprinted, and as often as asked. + - `POST /api/v1/jobs/last/reprint` reprints the last completed job, one that read `done`: never a job queued, waiting or printing now. + - `POST /api/v1/jobs/failed/reprint` retries the last failed print. + - `POST /api/v1/jobs/uncertain/reprint` reprints every `uncertain` job. + - A job a category route reprints is marked as reprinted, and no category route takes it again. + - Each call answers with the new job's ID, or the new jobs' IDs for `uncertain`, so a person can print it again through `/jobs/{id}/reprint`. + - The routes take no query parameters (owner, 2026-12-01). Every input goes in the JSON body, an empty body or none means the defaults, and any input they take later goes in the body too. The job ID, or `last`, `failed` or `uncertain`, is the route itself and stays in the path. + + Job IDs always start with `job_`, so `last`, `failed` and `uncertain` can never be one. A reprint queues a new job from the stored content (decision 8), whatever the original's source: `POST /api/v1/print`, a module route, an ntfy card or digest, and later a webhook. There is no in-band reprint: no ntfy message, or any other message a source receives, makes the daemon print a job again. Its details are open questions 25-39. + For phase 2b: 17. **Cancelling over HTTP.** `DELETE /api/v1/jobs/{id}`, for full tokens only, since a token limited to modules could otherwise cancel other clients' jobs. It cancels each delivery that has not reached `sending` and answers `200` with the status. A job with nothing left to cancel, every delivery having reached `sending`, finished or been cancelled, answers `200` with its status if any delivery is cancelled, so a repeated request answers as the first did, and `409` with a new code, `job_started`, if none is; an unknown ID answers `job_not_found`. Trade-off: more API, and the first route that changes a job already queued. @@ -128,6 +139,26 @@ For phase 5, later: 22. **ntfy state stays out of the store.** The JSON files stay. They write within a second of a change, and a keepalive-only cursor at most every 10 minutes, and the subscriber is built and tested on them. On a WebSocket each save also writes every seen ID within `max_backlog`, up to some 700 KB on a busy topic with a 12-hour backlog (docs/plans/ntfy.md decision 12). Trade-off: one file and one sync policy, against coupling ntfy to the store, which could write only the IDs that changed. 23. **A write error after part of a receipt went out.** Today such a send is retried, and design.md accepts that part of a receipt can then print twice, because retries let a job survive a printer that is briefly offline. That stays. docs/plans/remote-printers.md decision 17 builds on it: a remote `partial` outcome is today's write error. Once `Send` reports how many bytes `Write` took (docs/plans/printers.md stage 3's `SendError`, or phase 5's confirmed send), the owner may choose that such a job becomes `uncertain` and is never resent. Trade-off: fewer doubled fragments, against more jobs a person must check. With several printers, docs/plans/printers.md decision 22 fails such a delivery over to the next printer of an anycast list after the retries on the first printer (Architecture, DELIVERIES AND SEVERAL PRINTERS). That is a second double-print path: the next printer gets the whole receipt, so a receipt the first printer took in full before the write error prints in full on both. +## Open questions + +Each has the default the plan assumes unless the owner says otherwise. All belong to phase 2 and decision 24; each answer goes into design.md at once and becomes a decision here under its number. + +25. **`last` after a reprint.** Assumed: `last` takes the newest `done` job not yet marked. A finished reprint is a new job, so `last` can take it, which gives another copy of the same receipt. Trade-off: skipping reprints would instead reach back to an older receipt. +26. **Which failed jobs `failed` takes.** Assumed: the newest `failed` job not yet marked, one with `sent_at` included, since a person asked for it. Trade-off: a job that failed with part of it sent may print that part twice. +27. **The order of `uncertain` reprints.** Assumed: oldest first, each as its own job, so the receipts come out in their original order. Trade-off: the most recent missing receipt, often the one wanted, comes out last. +28. **When a job is marked.** Assumed: when its reprint is queued, recorded in the store so that it survives a restart, for example as the new job's `reprint_of`. Trade-off: if the reprint itself fails or ends `uncertain`, the original stays marked, but the reprint falls into that category itself. +29. **An age limit for `uncertain`.** Assumed: none; the route takes any `uncertain` job whose status is kept. Trade-off: a call can bring back an old receipt. An age limit would be a new key. +30. **Several printers.** With docs/plans/printers.md, each category is per printer. Assumed: the body's `printer` field names the printer, for example `{"printer": "office"}`, and the default printer when it is absent; a job named by its ID goes to the default printer unless the body names another. Trade-off: a receipt that went to several printers is reprinted on one at a time, and a client must know which printer lost it. +31. **Nothing in the category.** Assumed: `404` `job_not_found` for every category route, `uncertain` included, the code an unknown or forgotten ID gets too. Trade-off: no new code, but a client cannot tell "nothing to reprint" from "that ID is gone" without reading the message, and an empty `uncertain` call is an error rather than an empty list. +32. **The answer.** Assumed: `202`. For an ID, `last` and `failed`, the status of the new job, with an ID of its own and a field, assumed `reprint_of`, naming the job it reprints, kept in the store with it; for `uncertain`, a list of such statuses. The original job is not changed beyond its mark. Trade-off: one more status field and store column, and an answer for `uncertain` whose shape differs from the others. +33. **Which jobs an ID may name.** Assumed: any finished job whose content is still kept, marked or not: `done`, `failed` and `uncertain`, and `cancelled` once phase 2b adds it. A `queued` or `printing` job answers `409` with a new code, `job_not_finished`. Trade-off: reprinting a `done` job allows a deliberate copy of a receipt that printed or went missing, and an accidental one; refusing an unfinished job keeps a reprint from racing the job it copies. +34. **Who may reprint.** Assumed: full tokens only, as decisions 16 and 17 say for listing and cancelling, since the routes print content another client sent; a token limited to modules gets `403` `token_scope`. A job with a `raw` element also needs a token issued with `--raw`, as printing it did, and an `uncertain` call that would take one is refused whole without it. Trade-off: a module's client cannot reprint its own receipts; letting it would need its token's modules matched against the job's `source`. +35. **Content that was pruned.** Assumed: `410` `job_content_expired`, as decision 20 defines for previews, when the content of the named job, or of the job `last` or `failed` picks, is no longer kept; a category route does not fall back to another job. An `uncertain` job keeps its content while its status is kept (decision 9), so `uncertain` never meets this. Trade-off: none beyond decision 9's budget; a client that may need an old receipt again keeps its own copy. +36. **Repeats and the duplicate check.** Assumed: a reprint is a new job that is not unique, so the ntfy duplicate check (decision 10) does not refuse a reprinted card or digest. A category route does not take a job twice, because of the mark, but every call by ID queues a job, since each is deliberate. Trade-off: a client that retries a call by ID whose answer it lost prints twice. Phase 5's `Idempotency-Key` would cover that. +37. **Marking a reprint on paper.** Assumed: unmarked; it prints exactly as the original did. Trade-off: a `REPRINT` line would tell whoever holds both copies which is the copy, as till receipts and kitchen tickets often do, but it changes what prints, needs a paper check, and makes the copy differ from the original. +38. **A CLI command.** Assumed: none; `curl` or a client calls the routes. Trade-off: no `uberbringer reprint`, which would be the CLI's first command that calls a running daemon and so would need its address and a token. +39. **Clearing the printer first.** Assumed: no; a reprint queues like any job. Trade-off: after a cut-short image the printer can still be inside the image command (stage 1d's third paper check), and then the reprint's bytes print as noise. A real-time clear, `DLE DC4` fn 8, sent first would unstick it, but it also drops whatever else the printer still holds, and it needs a paper check before it can be trusted. + ## Findings for phase 5 What stage 1d decided is in design.md ("Jobs survive a restart"), with stage 1a's measurements on leil-test-1. Phase 5's acknowledgement still needs what the research found on Epson's side (2026-11-30): @@ -161,10 +192,10 @@ SOURCES WHAT SHIPS WHEN 1. Stages 1a-1d: measurements on leil-rig, the `[state] dir` key, the state machine without a database, then the SQLite store. Every job is stored when it is accepted. The worker renders, connects, commits a synced `sending` mark, then writes. After a restart, queued jobs print in order, and a job caught mid-send shows `uncertain` and is never resent. -2. The API: `GET /api/v1/jobs` lists the history, job status shows `ref`, and `/healthz` counts the jobs found `uncertain` at the latest start. Phase 2b, cancelling, needs phase 1 but not phase 2: `DELETE /api/v1/jobs/{id}` cancels each delivery of a job that has not reached `sending`, and an ntfy `message_delete` cancels the waiting jobs of its notification and drops its held digest rows. A delivery that has reached `sending` goes on. +2. The API: `GET /api/v1/jobs` lists the history, job status shows `ref`, `/healthz` counts the jobs found `uncertain` at the latest start, and `POST /api/v1/jobs/{id}/reprint` reprints a stored job by its ID, or by category the last completed job (`last`), the last failed one (`failed`) or every `uncertain` one (`uncertain`) (decision 24; questions 25-39). Phase 2b, cancelling, needs phase 1 but not phase 2: `DELETE /api/v1/jobs/{id}` cancels each delivery of a job that has not reached `sending`, and an ntfy `message_delete` cancels the waiting jobs of its notification and drops its held digest rows. A delivery that has reached `sending` goes on. 3. Stages 3a-3c, the image preview: the preview routes answer `Accept: image/png` with a picture of the paper, drawn from the bytes the job renders to. They need nothing from phases 1 and 2. 4. Previews of stored jobs: `GET /api/v1/jobs/{id}/preview` as text, JSON or PNG. -5. Later, on demand: Epson's acknowledgement, ntfy state in the store, an `Idempotency-Key`, a reprint route, an age limit, the partial-write choice. +5. Later, on demand: Epson's acknowledgement, ntfy state in the store, an `Idempotency-Key`, an age limit, the partial-write choice. COMPATIBILITY - Without `state.dir` the queue stays in memory and behaves as today, waiting jobs failing at shutdown included. @@ -667,22 +698,25 @@ Crash tests use fault injection like the above, never kill loops or soaks on the ### Phase 2: The API for stored jobs -**Goal.** Clients can see the history and the jobs in doubt (decision 16). +**Goal.** Clients can see the history and the jobs in doubt (decision 16), and a person can reprint a stored job by its ID or by category (decision 24), as the answers to questions 25-39 shape it. **Work.** internal/pkg/printqueue - `List(filter, before, limit)` from memory, newest first by sequence number; a `before` no longer kept is an error the server maps to `invalid_query`. The count of jobs with a delivery that `Load` turned `uncertain` at the latest start. `Status.Ref`. +- The reprint (decision 24), as questions 25-39 settle it; under the assumed defaults: `Reprint(id, meta)` and `ReprintCategory(category, meta)` (names assumed) read a finished job's stored content and submit it as a new job that is not unique and records the one it reprints. A category picks per printer the newest unmarked `done` job for `last`, the newest unmarked `failed` one for `failed`, and every unmarked `uncertain` one, oldest first, for `uncertain`, never a job still waiting or printing; the new job's `reprint_of`, in the store, marks the original. They refuse an unfinished job, content and IDs no longer kept, and an empty category. internal/app/uberbringer/server - The route `GET /api/v1/jobs` in the table, for full tokens; its handler and response type; `Jobs` gains `List`, and `fakeJobs` with it; its case in TestRoutesConform. - `/healthz`: `queue.uncertain`. +- `POST /api/v1/jobs/{id}/reprint` and the category routes `/jobs/last/reprint`, `/jobs/failed/reprint` and `/jobs/uncertain/reprint` in the table, for the tokens question 34 names, each reading its inputs from the JSON body alone and each with its case in TestRoutesConform; `Jobs` gains `Reprint` and `ReprintCategory`, and `fakeJobs` with them. tools/genopenapi - Typed query parameters: an enum and an integer with bounds. Docs - docs/api.md: the route table, a "List jobs" section, the reworded `invalid_query` row, the Job status section (`ref`) and the health field. +- docs/api.md: a "Reprint jobs" section, by ID and by category, its new codes in the error table and the job status field that names the reprinted job. - design.md: "Jobs remember their source" says job status shows the reference too. - CHANGELOG. docs/swagger.json regenerated. @@ -691,10 +725,12 @@ Docs - A token limited to modules gets `403` `token_scope`. - schema_test: the new enums held to validation. - `/healthz` counts the `uncertain` jobs found at start, a clean restart sets the count to 0, and the status code is unchanged. +- Reprint: `last` takes only a `done` job, and never a waiting one: a job printed, then another queued behind a stopped printer, and `last` reprinting the first; `failed` takes the newest failed job, then the next older one once that is marked; `uncertain` reprints every `uncertain` job, oldest first, and a second call answers `404`; marks survive a restart; a marked job still reprints by its ID, as often as asked; each answer carries the new job's ID, or IDs; a job from each source (`/print`, a module, an ntfy card and digest), a reprinted ntfy job not refused as a repeat; an empty category, an empty body, no body and a body naming another printer; a job by its ID in each state question 33 names, content and IDs no longer kept, and the token scopes of question 34. - No content in any log line. **Done when.** - `curl` on `/api/v1/jobs` lists the history, pages through it and filters it. +- With the owner's OK, one receipt reprinted with `last` prints once on the TM-T88IV, and its new job reads `done`. - The full gate passes. ### Phase 2b: Cancelling waiting jobs @@ -910,7 +946,6 @@ Docs - The partial-write choice (decision 23), once the confirmed send reports the bytes written. - ntfy state in the store (decision 22). - An `Idempotency-Key` header on `POST /api/v1/print`. -- A reprint route for `uncertain` jobs, `POST /api/v1/jobs/{id}/reprint`, which queues a new job from the stored content. - An age limit for queued jobs or stored content (decisions 6 and 9). **Tests.** diff --git a/docs/plans/templates.md b/docs/plans/templates.md index 32b9dd2..d3d32b7 100644 --- a/docs/plans/templates.md +++ b/docs/plans/templates.md @@ -125,6 +125,7 @@ For stage 4: - Until job-store stage 1c has shipped, each hook keeps its last 512 IDs in memory. Once it has, hook jobs join its duplicate check (job-store decision 10), which runs in `Submit` under the submit lock and replaces the memory for jobs that were queued. - This follows job-store decision 10, which leaves the HTTP routes' jobs out because a client chooses their reference. A hook's ID is read only after its signature passed, which is why hook jobs may join. - What it covers: a GitHub redelivery keeps its ID, and so do a Standard Webhooks retry and GitLab's retries, so none prints twice. Gitea and Forgejo give each attempt a new ID, a manual replay included, so a replay prints again, as the person replaying asked. GitHub's ID is not signed, so someone who captured a whole request could send it again under a new ID; only `gitlab` and `standard` sign theirs. + - A replay is the sender's own resend, not a reprint command; the daemon has none in-band, and a receipt is printed again through `POST /api/v1/jobs/{id}/reprint`, or by category through `/jobs/last/reprint`, `/jobs/failed/reprint` and `/jobs/uncertain/reprint` (docs/plans/job-store.md, decision 24). - Trade-off: without the job store, a restart forgets the IDs. 23. **Webhook rules.** They are `[[webhooks.rules]]`, shaped as `[[ntfy.rules]]` (decision 12) less `bypass`, seeing decision 5's `json` environment with `hook`, `event`, `delivery` and `headers` set. Actions are `print`, which needs `template`, and `drop`. A hook's own `template` key is what prints when no rule matches; a hook without one prints nothing then. So a hook for one kind of event needs no rule, and `event == "push"` picks Gitea's pushes out of everything else. `template` names a template whose `input` is `json`. - `headers` holds a list of allowed headers by lowercased name: `content-type`, `user-agent`, and the senders' own `x-github-*`, `x-gitea-*`, `x-gogs-*`, `x-forgejo-*` and `x-gitlab-*` headers and `webhook-id`, less every signature and token header among them. The header a `header` hook compares is never in it, so no template can print a secret. Repeated headers are joined with `, `.