# API request retention and mixed render fixture — ticket20 (2026-11-15)

**Source-only repair of the retained-request counterexample. Parent20 remains
OPEN for other resource combinations and native representative acceptance.**
Authoritative integration remains `/tmp/signet-roadmap-20261112/integration`,
HEAD9876b79 plus uncommitted work. No preserved-preview installation, deployment,
commit, dependency, input-capacity, schema or configuration change.

## Reproduction and rejected approach

The unchanged opt-in `test/resource-image-probe.sh` reproduced68428KiB on the
pre-repair writer image in disposable `signet-qpdf-investigate`. It queues an
actual max4MP high-entropy PNG/loaded-font render while two admitted1120212-byte
multipart document uploads are retained at INSERTs. Snapshot assets16218599
bytes remain within16MiB. Exact decoded RGB and font bytes pass even when RSS
fails. Previous checkpoint reached72240KiB; neither that failure nor these
sampled peaks is discarded from the evidence.

Upstream qpdf12.2.0 source reads a whole stream in `QPDF::pipeStreamData()` and
buffers writer stream data in `QPDFWriter::willFilterStream()`. Its standalone
~34MiB overlay cost was already measured. Stream-preserve is not a repair.
A prototype splitting PNGs into64 internally positioned strips passed the RSS
fixture (50304KiB) and exact decoded-pixel checks, but changed rasterization and
showed strip-boundary banding. It was REJECTED: writer/test changes are reverted
byte-for-byte to the preceding checkpoint. No tiling helper remains in product
source. qpdf itself, geometry, interpolation, alpha and image samples are unchanged.

## Retained repair

1. `src/http.c:http_keep_fields()` runs immediately after parsing in BOTH APIs,
   before a handler can block on PostgreSQL. It discards unused fields/cookies,
   keeps the first occurrence of supported names (existing `http_field()`
   semantics), preserves binary file/metadata ownership and shrinks the pair
   array. The component's supported-name list is beside its FastCGI main loop;
   adding a future input requires updating that list as well as its handler.
   Unknown fields were already ignored. No accepted body/field/file limit is
   lowered, and the original35000-part requests still succeed.

   kcgi0.13.4's destructor frees key/val/file/ctype/xcode per pair. The helper
   releases exactly these owned pointers; parsed values alias val. It only
   operates on unmapped requests (`keysz==0`, matching both init calls), clears
   retained next pointers and refuses mapped storage. On failed shrink, the
   reduced logical count remains valid for ordinary kcgi cleanup and the API
   returns503 instead of dispatching with the original large allocation.

2. Repeated warm tests showed that compaction alone was insufficient:59100KiB
   still exceeded60000000 bytes. libpq caches its grown input/output buffers
   after `PQclear()`. `src/db.c` now marks the cached connection for recycling
   when a query's aggregate text parameters or `PQresultMemorySize()` reach
   256KiB. `db_request_done()` releases that connection only at the API request
   boundary. Ordinary small requests retain connection reuse; large requests
   reconnect lazily on the next DB operation.256KiB is a recycling trigger,
   NOT an input limit or exact heap bound. It uses existing libpq16 APIs.

   Query results, active transactions and worker connection lifetimes are not
   interrupted. Only API main loops call the boundary operation. The worker's
   PostgreSQL commit-before-queue-ack contract is unchanged. Operational cost:
   an API request after large DB I/O may need a fresh DB connection; no added
   service, file, install permission, config switch or migration is needed.

## Verification

Final image/container: `signet-request-retention-final-r2`, temporary loopback
18110, disposable only. Fresh build has no compiler/make warnings. The host
clock was ~15 minutes ahead of the VM: no clock was changed; only private build
staging file timestamps were normalized in the VM before the build.

- Full E2E ends `all passed`, including metadata/PIM/signature/chunked-upload,
  parser admission, allocation and exact image/font contracts.
- `test/http-fields.c` covers10000 distinct unknown names, known duplicates,
  first empty/NUL/binary values, file metadata, cookies, mapped-request refusal,
  idempotence, zero retained fields, failed realloc and real kcgi destruction.
- `test/db-cache.c` verifies backend PID reuse for small queries; fresh backend
  after large result/parameter/aggregate-parameter/error cases; SQL NULL params;
  and preservation of an active transaction until its explicit commit/boundary.
- Real HTTP tests cover both APIs' first-duplicate semantics and byte-exact PDF
  upload/source retrieval. Full E2E includes `request-fields`; standalone mode:
  `sh /src/test/e2e.sh request-fields`.
- Seven final mixed runs, including after renderer/browser tests:54212,52776,
  53064,57020,49748,55736,55896KiB. All retain the original probe/capacity and
  compare decoded RGB/font bytes. Maximum57020KiB =58388480 bytes, below60M
  FOR THESE SAMPLED FIXTURES, with limited headroom. No universal bound claimed.
- Separate render/hang/retry/replay/DB contracts pass. Chrome signing flow
  passes with two-page output and audit reviewed; synthetic document
  `123a1527-faad-448b-8d33-cb7393485856`.
- Separate three-request parser/query/cookie fixture:48132KiB. This is NOT
  proof that active dense parsing and qpdf can overlap safely.
- ASan/UBSan runtimes are absent from the existing image; no dependency was
  installed. Real destructor/failure tests are not exhaustive allocator proof.

The resource sampler sums RSS of Signet/lighttpd UIDs, including helpers and
external tools, not PostgreSQL/Redis or root test tools. Sampling is non-atomic,
with1ms sleep PLUS scan time. Successful samples do not certify peak worst case,
other colours/codecs, catalogue/history/metadata cardinality, simultaneous active
parsing/qpdf, arbitrary PDFs or a native1vCPU/1GB shared deployment. Continue20
after22 (owner reordered on2026-11-16; `.scratch/api-logging.md` records its
source-only completion); owner14 trust/DAM/namespace/backup/OpenRC gates remain separate.

## Evidence and recovery

Restricted source/evidence backup:
`/Users/saikrupa/.local/var/signet-feature-backups/qpdf-image-strips-20261115T031645Z`.
The directory name reflects the rejected first experiment, not the retained
implementation. Before source/status/diff, rejected candidate, final-r2 source,
logs, manifests, image metadata and synthetic browser evidence are retained.
It is a source backup, not a runtime DB/files/queues backup.

Logs `/tmp/signet-qpdf-{red,stream-red,strips-first,strips-green,pixel-compare}.log`,
`/tmp/signet-request-retention-{green,final-e2e,final-mixed,repeat}.log`,
`/tmp/signet-request-retention-r2-{e2e,mixed,post-browser}.log`,
`/tmp/signet-request-retention-browser.log`; screenshots under
`/tmp/signet-request-retention-browser/`. Earlier retention-only overrun59100KiB
and original72240KiB evidence are historical failures, not final candidate passes.

Host/image SHA256 manifests match all88 native/UI/schema/config/vendor/test
files plus build descriptors in the manifest. Installed binaries match the build.
`sh -n`, manual POSIX/ownership review and `git diff --check` pass. Owned trigger
and holder-backend counts are both0. All five owned investigation stacks are
stopped/retained, exit0: `signet-qpdf-investigate`, `signet-qpdf-strips-check`,
`signet-request-retention-check`, `signet-request-retention-final`, and final-r2.
No implicit replacement preview remains. Preserved18087 remains HTTP200;
18090/8080 and PHP8087 were not modified. Never invoke old PIM Resync on18087
(still FLUSHDB). No native installation or preserved-preview replacement is
implied. Source manifests and cleanup/stopped logs accompany the private backup.
