diff --git a/docs/design.md b/docs/design.md index 97fe734..7716dd2 100644 --- a/docs/design.md +++ b/docs/design.md @@ -169,7 +169,7 @@ client ─HTTPS─▶ Caddy ─▶ server ── validate ──▶ printqueue ( 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). + builds the routes (its decisions 24-39). - **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 @@ -734,8 +734,23 @@ before anything is built. They come in this order. 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. + decision 24). The owner accepted its details as proposed on + 2026-12-01 (the plan's decisions 25-39): + - `last` takes the newest unmarked `done` job, `failed` the newest + unmarked `failed` one, and `uncertain` every unmarked `uncertain` + one, oldest first, with no age limit. Each category is per + printer, named by the body's `printer`, the default printer + without one. + - A job is marked when its reprint is queued, by the new job's + `reprint_of`, kept in the store. The answer is `202` with the new + job's status, or a list of them for `uncertain`. + - Nothing in a category answers `404` `job_not_found`, an + unfinished job `409` `job_not_finished`, and pruned content `410` + `job_content_expired`. + - Full tokens only, and a job with a `raw` element also needs a + `--raw` token. A reprint is never refused as a repeat, prints + unmarked, does not clear the printer first, and has no CLI + command. - 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 0454326..f8299e2 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 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. +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-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). The same day he accepted its details, decisions 25-39, as proposed, so phase 2's decisions are settled. Every check on the live printer needs the owner's OK. ## Decisions (owner, 2026-11-30) @@ -112,7 +112,23 @@ For phase 2: - 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. A body they cannot take answers `400` `invalid_body`, as the listing's does (decision 16). 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. + 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. Decisions 25-39 give its details (owner, 2026-12-01). + +25. **`last` after a reprint.** `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.** 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.** 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.** When its reprint is queued, recorded in the store so that it survives a restart, as the new job's `reprint_of` (decision 32). Trade-off: if the reprint itself fails or ends `uncertain`, the original stays marked, but the reprint falls into that category itself. +29. **No age limit for `uncertain`.** 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. The body's `printer` field names the printer, for example `{"printer": "office"}`; an empty `printer` is the same as none, as on the print routes (printers.md decision 7), and a name that is not a configured printer answers `400` `unknown_printer`, as there. Without a name, a category and a job named by its ID both mean the default printer, and the reprint names it itself (`Meta.Printer = "default"`), so routing rules never move it. 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.** `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.** `202`. For an ID, `last` and `failed`, the status of the new job, with an ID of its own and a field, `reprint_of` (name assumed), 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.** 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.** 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.** `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.** 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. **A reprint prints unmarked,** exactly as the original did. Trade-off: whoever holds both copies cannot tell which is the copy, as a `REPRINT` line would tell them on till receipts and kitchen tickets; such a line would change what prints and need a paper check. +38. **No CLI command.** `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. **The printer is not cleared first.** 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. For phase 2b: @@ -141,26 +157,6 @@ 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): @@ -194,7 +190,7 @@ 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: `POST /api/v1/jobs/list` 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. +2. The API: `POST /api/v1/jobs/list` 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`) (decisions 24-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`, an age limit, the partial-write choice. @@ -700,18 +696,18 @@ 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), 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. +**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 (decisions 24-39). **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_body`. 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. +- The reprint (decisions 24-39): `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 `POST /api/v1/jobs/list` in the table, for full tokens; its request type, with `status`'s enum and `limit`'s bounds in its `jsonschema` tags; its handler and response type; `invalid_body` in errors.go; `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, answering a body it cannot take with `invalid_body`, and each with its case in TestRoutesConform; `Jobs` gains `Reprint` and `ReprintCategory`, and `fakeJobs` with them. +- `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 decision 34 names, each reading its inputs from the JSON body alone, answering a body it cannot take with `invalid_body`, and each with its case in TestRoutesConform; `Jobs` gains `Reprint` and `ReprintCategory`, and `fakeJobs` with them. Docs - docs/api.md: the route table, a "List jobs" section, the `invalid_body` row, the Job status section (`ref`) and the health field. @@ -724,7 +720,7 @@ Docs - A token limited to modules gets `403` `token_scope`. - schema_test: the new enums and `limit`'s bounds 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. +- 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, a body naming another printer, and one naming a printer that is not configured, which gives `unknown_printer`; a job by its ID in each state decision 33 names, content and IDs no longer kept, and the token scopes of decision 34. - No content in any log line. **Done when.**