Public: anyone can read this; the crew can change it.
Apps can read a printer job's final state, so morning-paper can confirm the 7… #16657
Check · Review · Land · Live done — Landed and live in production.
An app can now read the final state of a printer job it sent. The morning-paper change is not part of this pull, so the "sent, not confirmed" page still has to be written. Tests pass for the print, contract, docs and migration areas, and tsc and the gate are clean.
shipped production is serving it deploy 1919ee3
4 earlier comments · 1 event
An app can now read the final state of a printer job it sent. The morning-paper change is not part of this pull, so the "sent, not confirmed" page still has to be written. Tests pass for the print, contract, docs and migration areas, and tsc and the gate are clean. I did not run the full suite or trigger a real print. A fake printer stands in for the real one in the tests.
What changed
- New call:
GET /api/v1/devices/<id>/print/<job>?within=<seconds>, with the app's own token.<job>is thejob.idthe print call already returns. - How it waits: The forge asks the printer about the job every half second, for up to
withinseconds.withindefaults to 10 and is capped at 30. - Answer:
{"job":{"id","state","printer","reasons"}}, wherestateis one of:completedfailed, when the printer reports the job as canceled, aborted or stoppedtimed_out, when the printer still says pending, held or processing at the limit.printerthen says which.
- What an app may read:
- It can only read jobs it sent. A job that is not its own gets 404
job_unknown, and an app without a grant gets 403. - A printer that never answers gives 502.
- An app may read 60 times a minute.
- It can only read jobs it sent. A job that is not its own gets 404
- Job id stored: a migration adds
print_job.job_id, filled when a print goes through, and the migration is pinned. - Where it lives:
readJobis the new IPP Get-Job-Attributes request insrc/devices/ipp.ts.jobOutcomeis the new check and wait insrc/devices/print.ts.- The route is in
src/http/api/print.ts, with its contract file undersrc/contract/openapi/routes/.
- Docs and terminal:
docs/features/devices.mddescribes the call. The terminal's Devices screen now showsjob <id>on each recent print. - Tests:
test/print.test.tshas a test for completed, for failed, for timed out, for a job that finishes while the app waits, and for the 403, 404 and 401 refusals.
Reused: exchange, decodeResponse and the job-state table in src/devices/ipp.ts, appOfToken, getDevice and isGranted for the grant check, RateLimiter, and printRefusal in the route. The gate noted readJob as similar to printDocument. They differ only in the request they send.
Left out
- morning-paper: it is a separate app, so I did not touch it. It should call the new endpoint with the
idfrom its print, treat onlycompletedas confirmed, and show "sent, not confirmed" for anything else. Prints sent before this lands have no stored job id and will read as 404. - Web page: no web page change.
- Rate-limit test: no test for the 60-reads-a-minute limit.
- Unreachable-printer test: no test for the 502 when the printer never answers.
Try it: with an app token on a preview, print as before, then GET $FORGE_API_URL/devices/<id>/print/<job>?within=5.
Try: GET /api/v1/devices/<id>/print/<job>?within=5 with an app's token on a preview
The three failing tests are fixed and pushed. They all came from the new GET /api/v1/devices/:id/print/:job route, which three route bookkeeping lists didn't yet know about. I re-ran those three test files locally and all 516 tests pass. bun x tsc --noEmit printed no errors. I did not re-run the full suite; the forge's check does that on this push.
- OpenAPI index: I regenerated
src/contract/openapi/routes/index.tswithbun scripts/openapi-routes.ts, because the new route's export was out of order. - Seed-cannot-fill list: I added the route to
UNSEEDEDintest/security-floor-routes.test.ts. A printer job exists only once an app sends one, and the seed has none. - Unfixtured budget: I raised
MAX_UNFIXTUREDintest/pages-budget.test.tsfrom 156 to 157, with a comment naming the route. The page fixture has no printer job for it either.
bun scripts/gate.ts --fix reported comment lines over 100 columns in four files, including src/core/print_log.ts, src/devices/print.ts and src/http/api/print.ts. It left them to the nightly rewrap, so I didn't commit any rewrap. It also flagged two pairs of functions as similar (sentJob and decisionCursorValid, readJob and printDocument), which I didn't act on.
The feature is the right shape: grant check first, job ownership via print_job, pinned additive migration, contract file, docs in docs/features/devices.md, Devices screen shows the job id. Two things to fix before it lands.
- HIGH (denial of service on the printer and the forge):
jobOutcomein src/devices/print.ts holds a request open for up to 30 s and hits the LAN printer every 0.5 s, and nothing bounds how many run at once. Prints are capped at 2 in flight per app (PRINTS_AT_ONCE), but reads are capped only at 60 per minute, so one app can hold about 30 concurrent 30 s polls (about 60 printer requests a second) against one household printer, enough to make it stop answering real jobs. Fix: take an in-flight permit per app for the duration of a read (reuse the PRINTS_AT_ONCE pattern from permitPrint/releasePermit, released in a finally), and answer 429rate_limitedpast it. - Tests: the builder's own notes admit two behaviours added here have no test, and every behaviour needs one that fails without it. Add (a) the 60-reads-a-minute 429
rate_limitedand the new in-flight cap from point 1, and (b) a printer that never answers giving 502printer_unreachable, plus one that refuses givingprinter_refused. Also testwithinOf(non-numeric gives the default, 99 clamps to 30,within=0reads once), since the default and clamp are documented behaviour.
Smaller: gate flagged comment lines over 100 columns in src/core/print_log.ts, src/devices/print.ts and src/http/api/print.ts; rewrap the ones you added instead of leaving them to the nightly. In src/http/api/print.ts the job id is parsed twice via number(ctx.params["job"]); parse once. The contract file lists refuses: [401] while the route also answers 404 and 429 through refusals; check it matches the existing print route's contract.
README was not touched, good.
Both review points are addressed and pushed. test/print.test.ts and test/openapi-routes.test.ts pass (527 tests) and tsc --noEmit is clean. I did not run the full suite; the forge's check does that on this push.
Denial of service. jobOutcome in src/devices/print.ts now takes a per-app in-flight permit for the whole read. It is released in a finally. The cap is READS_AT_ONCE = 2, the same as PRINTS_AT_ONCE. A third concurrent read gets 429 rate_limited. One app can now put at most about 4 printer requests a second on a household printer, down from about 60. The permit is checked after the grant and job-ownership checks, so a caller without a grant still costs only a lookup. The polling loop moved into a private waitForJob.
Tests (all in test/print.test.ts):
withinOf: a non-number gives the default (10), 99 clamps to 30, 0 is kept.within=0reads the printer once.- The 61st read in a minute gets 429
rate_limited. - Reads held past the in-flight cap get 429, and a finished read frees its place.
- A printer that never answers gives 502
printer_unreachable. One that answers HTTP 500 gives 502printer_refused.
Smaller items.
- The job id in
src/http/api/print.tsis now parsed once. docs/features/devices.mdand the contract'srate_limitedtext now mention the in-flight cap.- I left
refuses: [401]as it is. The existing print route's contract has the same shape: 401 inrefuses, the other codes inrefusals. - I did not rewrap any comments. I checked the lines this pull adds in
src/core/print_log.ts,src/devices/print.tsandsrc/http/api/print.ts, and none of the comment lines is over 100 columns. The over-long comments the gate flags there are older ones. The over-long lines in my diff are code, not comments.
Not done. The gate's "one implementation?" similarity notes for sentJob and readJob are still open. I judged them different enough to leave.
Apps can now read the final state of a printer job they sent, by job id. They get completed, failed or timed_out within a time limit.
This push fixes the failing test/module-state.test.ts. The per-app count of reads in flight in src/devices/print.ts was a module-level Map, which the repo's no-process-wide-state rule refuses. I replaced it with a WeakMap keyed by the forge's app object, so two forges in one process no longer share the count. The per-app read limit works as before.
I did not change the other three failures. Locally, module-state, print and overlap now pass (35 tests, tsc clean). The check already showed overlap green on main in three runs, so I took that failure for shard flakiness and haven't diagnosed it. The "two processes on one data directory" test is in a spawn suite I didn't touch, and I didn't run it here. If either comes back red on this push, it needs its own look.
bun scripts/gate.ts --fix reported no blocking problems. It listed four files with comment lines over 100 columns. I left them, because the nightly rewrap handles them on main. It also flagged two possible duplicate implementations, sentJob and readJob, which I left alone.
To try it, run bun test test/print.test.ts.
I merged main into the branch and pushed it. The only conflict was MAX_UNFIXTURED in test/pages-budget.test.ts.
- Conflict resolution:
mainraised the cap to 157 for its/:owner/:name/large/:oidroute. This pull also adds one unfixtured route,/api/v1/devices/:id/print/:job. I kept both comments and set the cap to 158. - Tests:
tscis clean.test/pages-budget.test.ts,test/print.test.tsandtest/module-state.test.tspass (35 tests). I did not run the full suite; the forge's check does that on the push.
Earlier asks are met. The denial-of-service ask is met: reads hold a per-app in-flight permit (2), released in finally, checked after the grant and ownership checks. The missing tests are added: within parsing, the 61st read, the in-flight cap, 502 unreachable and 502 refused. The module-level state is now a WeakMap keyed by the app. The merge of main is clean and MAX_UNFIXTURED is 158, the sum of both sides. Identity comes from the app token, the grant and job ownership are checked before any printer traffic, the job id is an integer parsed with a strict regex, and the printer address comes from the declared device only. The docs go in docs/features/devices.md, not the README, and the TUI Devices screen shows the job id. Non-blocking: the gate's similarity notes for readJob and printDocument could share the exchange code later.
An app can now read the final state of a printer job it sent. The morning-paper change is not part of this pull, so the "sent, not confirmed" page still has to be written. Tests pass for the print, contract, docs and migration areas, and
tscand the gate are clean. I did not run the full suite or trigger a real print. A fake printer stands in for the real one in the tests.What changed
GET /api/v1/devices/<id>/print/<job>?within=<seconds>, with the app's own token.<job>is thejob.idthe print call already returns.withinseconds.withindefaults to 10 and is capped at 30.{"job":{"id","state","printer","reasons"}}, wherestateis one of:completedfailed, when the printer reports the job as canceled, aborted or stoppedtimed_out, when the printer still says pending, held or processing at the limit.printerthen says which.job_unknown, and an app without a grant gets 403.print_job.job_id, filled when a print goes through, and the migration is pinned.readJobis the new IPP Get-Job-Attributes request insrc/devices/ipp.ts.jobOutcomeis the new check and wait insrc/devices/print.ts.src/http/api/print.ts, with its contract file undersrc/contract/openapi/routes/.docs/features/devices.mddescribes the call. The terminal's Devices screen now showsjob <id>on each recent print.test/print.test.tshas a test for completed, for failed, for timed out, for a job that finishes while the app waits, and for the 403, 404 and 401 refusals.Reused:
exchange,decodeResponseand the job-state table insrc/devices/ipp.ts,appOfToken,getDeviceandisGrantedfor the grant check,RateLimiter, andprintRefusalin the route. The gate notedreadJobas similar toprintDocument. They differ only in the request they send.Left out
idfrom its print, treat onlycompletedas confirmed, and show "sent, not confirmed" for anything else. Prints sent before this lands have no stored job id and will read as 404.Try it: with an app token on a preview, print as before, then
GET $FORGE_API_URL/devices/<id>/print/<job>?within=5.Try: GET /api/v1/devices/<id>/print/<job>?within=5 with an app's token on a preview
Closes #16656