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

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

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.

Notes

shipped production is serving it deploy 1919ee3

opened this

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 the job.id the print call already returns.
  • How it waits: The forge asks the printer about the job every half second, for up to within seconds. within defaults to 10 and is capped at 30.
  • Answer: {"job":{"id","state","printer","reasons"}}, where state is one of:
    • completed
    • failed, when the printer reports the job as canceled, aborted or stopped
    • timed_out, when the printer still says pending, held or processing at the limit. printer then 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.
  • Job id stored: a migration adds print_job.job_id, filled when a print goes through, and the migration is pinned.
  • Where it lives:
    • readJob is the new IPP Get-Job-Attributes request in src/devices/ipp.ts.
    • jobOutcome is the new check and wait in src/devices/print.ts.
    • The route is in src/http/api/print.ts, with its contract file under src/contract/openapi/routes/.
  • Docs and terminal: docs/features/devices.md describes the call. The terminal's Devices screen now shows job <id> on each recent print.
  • Tests: test/print.test.ts has 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 id from its print, treat only completed as 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

Closes #16656

4 earlier comments · 1 event
commented

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 the job.id the print call already returns.
  • How it waits: The forge asks the printer about the job every half second, for up to within seconds. within defaults to 10 and is capped at 30.
  • Answer: {"job":{"id","state","printer","reasons"}}, where state is one of:
    • completed
    • failed, when the printer reports the job as canceled, aborted or stopped
    • timed_out, when the printer still says pending, held or processing at the limit. printer then 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.
  • Job id stored: a migration adds print_job.job_id, filled when a print goes through, and the migration is pinned.
  • Where it lives:
    • readJob is the new IPP Get-Job-Attributes request in src/devices/ipp.ts.
    • jobOutcome is the new check and wait in src/devices/print.ts.
    • The route is in src/http/api/print.ts, with its contract file under src/contract/openapi/routes/.
  • Docs and terminal: docs/features/devices.md describes the call. The terminal's Devices screen now shows job <id> on each recent print.
  • Tests: test/print.test.ts has 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 id from its print, treat only completed as 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

commented

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.ts with bun scripts/openapi-routes.ts, because the new route's export was out of order.
  • Seed-cannot-fill list: I added the route to UNSEEDED in test/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_UNFIXTURED in test/pages-budget.test.ts from 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.

Changes requested — before the latest push

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.

  1. HIGH (denial of service on the printer and the forge): jobOutcome in 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 429 rate_limited past it.
  2. 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_limited and the new in-flight cap from point 1, and (b) a printer that never answers giving 502 printer_unreachable, plus one that refuses giving printer_refused. Also test withinOf (non-numeric gives the default, 99 clamps to 30, within=0 reads 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.

commented

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=0 reads 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 502 printer_refused.

Smaller items.

  • The job id in src/http/api/print.ts is now parsed once.
  • docs/features/devices.md and the contract's rate_limited text now mention the in-flight cap.
  • I left refuses: [401] as it is. The existing print route's contract has the same shape: 401 in refuses, the other codes in refusals.
  • I did not rewrap any comments. I checked the lines this pull adds in src/core/print_log.ts, src/devices/print.ts and src/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.

commented

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.

commented

I merged main into the branch and pushed it. The only conflict was MAX_UNFIXTURED in test/pages-budget.test.ts.

  • Conflict resolution: main raised the cap to 157 for its /:owner/:name/large/:oid route. This pull also adds one unfixtured route, /api/v1/devices/:id/print/:job. I kept both comments and set the cap to 158.
  • Tests: tsc is clean. test/pages-budget.test.ts, test/print.test.ts and test/module-state.test.ts pass (35 tests). I did not run the full suite; the forge's check does that on the push.
Approved

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.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs