diff --git a/docs/design.md b/docs/design.md index 49bdf50..97fe734 100644 --- a/docs/design.md +++ b/docs/design.md @@ -506,13 +506,12 @@ client ─HTTPS─▶ Caddy ─▶ server ── validate ──▶ printqueue ( the request's JSON body, never in a query parameter. The path holds only a resource's ID, or a fixed word that names the route, as `last`, `failed` and `uncertain` do in `/jobs/.../reprint`. A `GET` takes no - inputs beyond its path. Every route answers a query string with `400` - `invalid_query`, after the token and its scope under `/api/v1`, so a - caller that still sends one learns so instead of having it ignored. The - module routes' body holds the module's payload, `copies` and `printer`. - One plan breaks the rule today, and a later change makes it follow: - the filters and paging that docs/plans/job-store.md's phase 2 puts in - the query of `GET /api/v1/jobs` (its decision 16). + inputs beyond its path, so a read that takes inputs is a `POST`, as the + planned job list, `POST /api/v1/jobs/list`, is. Every route answers a + query string with `400` `invalid_query`, after the token and its scope + under `/api/v1`, so a caller that still sends one learns so instead of + having it ignored. The module routes' body holds the module's payload, + `copies` and `printer`. - **The OpenAPI document is generated (owner, 2026-11-30).** One route table drives both the server's routes and `docs/swagger.json`, so the document cannot describe a route the server lacks. Schemas come from the @@ -716,9 +715,10 @@ before anything is built. They come in this order. 1. **Job store and print previews**: `docs/plans/job-store.md` (written; 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 that became `uncertain` since the latest - start, at it or during the run. + - `POST /api/v1/jobs/list` lists the history, its filters and paging + in the JSON body. Job status shows `ref`, and `/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 diff --git a/docs/plans/job-store.md b/docs/plans/job-store.md index 65c7e88..0454326 100644 --- a/docs/plans/job-store.md +++ b/docs/plans/job-store.md @@ -96,8 +96,10 @@ For stage 1d: For phase 2: -16. **A history listing.** `GET /api/v1/jobs`, newest first, for full tokens only. A listing shows every client's jobs, so tokens limited to modules do not get it. Trade-off: more API to keep. Its details: - - `status`, `source`, `before` (a job ID; the page starts after it) and `limit` (1-100, default 50) in the query. A `before` that names a job no longer kept answers `invalid_query`, so paging never skips jobs silently. +16. **A history listing.** `POST /api/v1/jobs/list`, newest first, for full tokens only. A listing shows every client's jobs, so tokens limited to modules do not get it. Trade-off: more API to keep, and a read that is a `POST`, so a browser or a cache cannot fetch it by its URL alone. Its details: + - Its filters and paging go in the JSON body, and so the route is a `POST` (owner, 2026-12-01): every input goes in the body, and a `GET` takes none beyond its path (design.md, "Inputs go in the JSON body"). `list` is a fixed word that names the route; job IDs always start with `job_`, so it can never be one. + - The body holds `status`, `source`, `before` (a job ID; the page starts after it) and `limit` (1-100, default 50), decoded strictly. An empty body, `{}` or none means the defaults: the newest 50 jobs, whatever their status or source. The answer is `{"jobs": [...], "next": "job_..."}`. + - A bad value or an unknown field answers `400` with a new code, `invalid_body`, whose `details` name each field; the reprint routes' bodies share it (decision 24). A `before` that names a job no longer kept answers `invalid_body` too, so paging never skips jobs silently. - 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. @@ -108,7 +110,7 @@ For phase 2: - `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. + - 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. @@ -192,7 +194,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: `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. +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. 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. @@ -412,7 +414,7 @@ RETENTION API - Stage 1c: `status` gains `uncertain`, in the `State` enum's `jsonschema` tag, printqueue's schema test, docs/api.md, swagger.json and the CHANGELOG under **Breaking**. Status gains `sent_at` (name assumed), set once an attempt was about to write to the printer. A store failure in `Submit` is an unexpected error: `500` `internal_error`, logged and reported once, the job not accepted, and the daemon stops (decision 11). - Stage 1d: a job kept in the store answers its status after a restart instead of `job_not_found`. -- Phase 2 (decision 16): `GET /api/v1/jobs` with `status`, `source`, `before` and `limit` in the query. It answers `{"jobs": [...], "next": "job_..."}`, newest first by sequence number. Bad values answer `invalid_query`, whose api.md row ("On a module route") is reworded. The generator documents every query parameter as a string today, so an enum or integer parameter needs a field on `Param` and a generator change. Job status gains `ref`. `/healthz`'s `queue` gains `uncertain`; the status code still depends on the printer alone. +- Phase 2 (decision 16): `POST /api/v1/jobs/list` with `status`, `source`, `before` and `limit` in the JSON body. It answers `{"jobs": [...], "next": "job_..."}`, newest first by sequence number. Bad values answer the new code `400` `invalid_body`. The body is a Go type whose `jsonschema` tags give `status`'s enum and `limit`'s bounds, as every request body's tags do, so the generator needs no change. Job status gains `ref`. `/healthz`'s `queue` gains `uncertain`; the status code still depends on the printer alone. - Phase 2b (decision 17): `DELETE /api/v1/jobs/{id}`, for full tokens, answers `200` with the status, or `409` with the new code `job_started` when no delivery is left to cancel and none is cancelled. `status` gains `cancelled`, a new enum value, so the CHANGELOG lists it under **Breaking**, as it does `uncertain`. `DELETE /api/v1/print` still answers `405`, api.md's example for `method_not_allowed`. - Phase 3c (decision 19): `Accept: image/png` on `POST /api/v1/preview` and `POST /api/v1/preview/{module}`. Text stays the answer to `*/*`. A picture over the height cap answers `422` `preview_too_long`. The generator needs a PNG body schema (`contentMediaType: image/png`). The compress middleware sits in front of these routes; phase 3c checks that it leaves PNG alone. - Phase 4 (decision 20): `GET /api/v1/jobs/{id}/preview`, as text, JSON or PNG by `Accept`, as the POST preview answers, with the new code `410` `job_content_expired`. @@ -703,33 +705,30 @@ Crash tests use fault injection like the above, never kill loops or soaks on the **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`. +- `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. 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. +- 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 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. +- `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. 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: the route table, a "List jobs" section, the `invalid_body` 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. **Tests.** -- Filters by status and source, paging with `before`, a `before` no longer kept, the bounds of `limit`, and bad values giving `invalid_query`. +- Filters by status and source, paging with `before`, a `before` no longer kept, the bounds of `limit`, and bad values and an unknown field giving `invalid_body`. An empty body, `{}` and no body give the defaults. `GET /api/v1/jobs/list` answers `404` `job_not_found`, as any path that is not a job's ID does. - A token limited to modules gets `403` `token_scope`. -- schema_test: the new enums held to validation. +- 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. - No content in any log line. **Done when.** -- `curl` on `/api/v1/jobs` lists the history, pages through it and filters it. +- `curl` posting to `/api/v1/jobs/list` 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.