From 9467ae4e5b09dc40a07da2dcfefee9cad6f94708 Mon Sep 17 00:00:00 2001 From: Roman Zaiev Date: Thu, 3 Dec 2026 16:11:07 +0000 Subject: [PATCH 1/3] Check the rules by litany and send agents to the docs make lint checked formatting, odoc and the release build, so the rules a compiler does not hold were held by reading. litany now runs there on Hypha's lint set, with the gold copy held whole in the litany file, and the code meets it: Int's max for the polymorphic one, the type named at each ignore in the tests, each description message matched by its own case where a wildcard stood, and a backtrace kept where a connection being opened closes its socket before raising again. The driver reads the system's clock once, to verify a certificate against sslrootcert, since connect is given a monotonic clock alone and taking a wall clock would change its signature. That one use is allowed with its reason beside it, and AGENTS.md names the place. An agent using postgres-eio reaches its signatures in the switch's lib/, by go-to-definition, a hover or a read, and never opens its doc/, where the catalogue and the pages are installed. So every interface names its page, where a page is about it, or its package's catalogue, by its path through opam var, and the README's With an agent says the same, in a line for a project's AGENTS.md that works for any package. --- .github/workflows/ci.yml | 4 + AGENTS.md | 13 ++- CHANGES.md | 8 ++ Makefile | 7 +- README.md | 13 ++- dune | 5 +- dune-project | 5 + litany | 223 ++++++++++++++++++++++++++++++++++++++ postgres-eio.opam | 1 + src/auth.mli | 5 +- src/column.mli | 5 +- src/connection.ml | 62 ++++++----- src/connection.mli | 3 + src/conninfo.mli | 3 + src/instant.mli | 5 +- src/interval.mli | 5 +- src/oid.mli | 4 +- src/pool.ml | 2 +- src/pool.mli | 3 + src/postgres_eio.mli | 3 + src/protocol.ml | 2 +- src/protocol.mli | 3 + src/saslprep.mli | 5 +- src/server_error.mli | 5 +- src/tag.mli | 5 +- src/text.mli | 5 +- src/value.mli | 3 + test/test_postgres_eio.ml | 95 ++++++++++------ 28 files changed, 421 insertions(+), 81 deletions(-) create mode 100644 litany diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 130977e..e09f405 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,10 @@ jobs: - uses: ocaml/setup-ocaml@v3 with: ocaml-compiler: ${{ matrix.ocaml }} + # litany, which make lint runs, is open-opam's. + opam-repositories: | + open-opam: https://github.com/open-opam/opam-repository.git + default: https://opam.ocaml.org - run: opam install . --deps-only --with-test --with-dev-setup -y # Use setup-ocaml's switch instead of the Makefile's. - run: make lint OPAM="opam exec --" diff --git a/AGENTS.md b/AGENTS.md index ca33d41..57f2d7b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -12,7 +12,7 @@ change updates the page that describes it, not the README. ```sh make setup # once: the switch hypha-postgres-eio, all dependencies make test # starts the test Postgres in Podman, runs every test -make lint # formatting, odoc, and the release build +make lint # formatting, odoc, the release build, and Hypha's rules by litany make fmt # format in place make docs # the documentation site into _build/docs, as Pages publishes it make docs-serve # the built site, served on DOCS_PORT, search included @@ -122,3 +122,14 @@ Each rule comes with why it exists and the test that catches a break. connection. A server error does not, since the server marks where its reply ends. Test: `54.2.3 a server's error keeps the connection`, `54.2.6 a producer that raises is a CopyFail`. +- **The driver reads the system's clock in one place: to verify a + certificate against `sslrootcert`**, in `tls_config` of + `src/connection.ml`, as `Ca_certs.authenticator` does for the system's + authorities. Why: a certificate is valid at an instant, and `connect` is + given a monotonic clock alone. Symptom: `make lint`'s litany reports + `restricted-dependency` for a clock anywhere else, which that one use is + allowed with its reason beside it. +- **The `litany` file holds all of Hypha's, and adds nothing of + postgres-eio's.** Why: one set of rules holds every repository, and an + exception is read with its reason. Symptom: a rule dropped from `select` + or `extend`, or a `per-path` or a `[@litany.allow]` with no reason. diff --git a/CHANGES.md b/CHANGES.md index 3722072..34d6ea7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,13 @@ # Changes +## Unreleased + +- A connection that fails while it is being opened keeps the backtrace of + the exception its socket is closed on, where closing could overwrite it. +- Every package installs the documentation for agents, `llms.txt` and every + page, in `$(opam var :doc)`, and every interface it installs + names its page there, or `llms.txt`. The README's *With an agent* says so. + ## 0.5.3 (2026-11-22) - postgres-eio installs its documentation for agents: each page of the diff --git a/Makefile b/Makefile index 814b936..61bbe63 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,7 @@ help: ## list targets setup: ## create the switch and install dependencies @podman compose version >/dev/null 2>&1 || echo "The tests need podman compose." @opam switch list --short | grep -qx $(SWITCH) || opam switch create $(SWITCH) --empty -y + opam repo add open-opam https://github.com/open-opam/opam-repository.git --switch=$(SWITCH) --rank=1 opam install . --switch=$(SWITCH) --deps-only --with-test --with-dev-setup -y build: ## build everything @@ -25,10 +26,12 @@ build: ## build everything test: db ## run the tests POSTGRES_EIO_TEST_PG=$(PG_TEST_URL) $(OPAM) dune test --force -# Release is the profile opam installs with. -lint: ## check formatting, docs and the release build +# Release is the profile opam installs with. litany holds the code to +# Hypha's rules, as the litany file selects them. +lint: ## check formatting, docs, the release build and Hypha's rules $(OPAM) dune build @all @fmt @doc $(OPAM) dune build --profile release @all + $(OPAM) litany check fmt: ## format the code $(OPAM) dune fmt diff --git a/README.md b/README.md index 6d26d1d..bc18b32 100644 --- a/README.md +++ b/README.md @@ -60,14 +60,15 @@ let () = ## With an agent -postgres-eio installs its documentation at `$(opam var postgres-eio:doc)`: -each page of the site as Markdown, and `llms.txt`, which lists them. Your -agent reads it when told to, in a line in your project's `AGENTS.md`: +Every package of postgres-eio installs its documentation for an agent with +it, at `$(opam var :doc)`: `llms.txt`, and every page in Markdown. +An agent reads it when your project's `AGENTS.md` tells it to: ```md -Before writing code with postgres-eio, read `$(opam var postgres-eio:doc)/llms.txt` and -the pages it lists for the parts you need, and use those parts rather than -writing your own. +Before working with postgres-eio, read `llms.txt` in `$(opam var +:doc)`, for any postgres-eio package the project installs, then the +pages it lists for the parts you need, and use those parts rather than writing +your own. ``` ## Contributing diff --git a/dune b/dune index 0842b01..a740d7c 100644 --- a/dune +++ b/dune @@ -8,8 +8,9 @@ (:standard -w +a-4-40-41-42-44-45-48-67-70)))) ; The documentation for a user's agent, agents/, written from the site's pages -; by `make docs`: installed whole, so $(opam var postgres-eio:doc) holds -; llms.txt and each page. +; by `make docs`: every package installs it whole, so a package installed +; alone, postgres-eio without postgres-eio, still holds llms.txt and each page in +; $(opam var :doc). (install (package postgres-eio) diff --git a/dune-project b/dune-project index d13c862..f81433a 100644 --- a/dune-project +++ b/dune-project @@ -84,6 +84,11 @@ (and :with-dev-setup (= 0.29.0))) + (hypha-litany + (and + :with-dev-setup + (>= 1.3.0) + (< 2.0.0))) (odoc (and :with-dev-setup diff --git a/litany b/litany new file mode 100644 index 0000000..34bb3b7 --- /dev/null +++ b/litany @@ -0,0 +1,223 @@ +; Hypha's lint set, as litany holds it. Every Hypha repository's litany file +; selects each rule here and keeps each forbid as it is written here; what is +; the repository's own -- a forbid more, a per-path -- is added with its +; reason. Hypha's OCAML.md says what each rule is for. + +(litany-config 1) + +(lint + (select default) + (extend + unsafe-partial-stdlib + unsafe-obj-magic + untyped-ignore + ignored-result + wildcard-over-owned-variant + open-of-ordinary-module + restricted-dependency)) + +(rule restricted-dependency + ; Refused in every file. + (forbid Stdlib.failwith + (use "a result; a test that raises on purpose declares its own exception")) + (forbid Stdlib.compare + (use "the type's own comparison: Int.compare, String.compare")) + (forbid Stdlib.min + (use "the type's own: Int.min, Float.min")) + (forbid Stdlib.max + (use "the type's own: Int.max, Float.max")) + (forbid Unix.time + (use "the clock the caller gives, read as a Ptime.t")) + (forbid Unix.gettimeofday + (use "the clock the caller gives, read as a Ptime.t")) + (forbid Stdlib.Sys.time + (use "Eio.Time.Mono.now, for time that has passed")) + (forbid Unix.sleep + (use "Eio.Time.Mono.sleep_span")) + (forbid Unix.alarm + (use "Eio.Time.Timeout.v")) + (forbid Unix.setitimer + (use "Eio.Time.Timeout.v")) + (forbid Unix.getitimer + (use "Eio.Time.Mono.now")) + (forbid Eio_unix.sleep + (use "Eio.Time.Mono.sleep_span")) + (forbid Eio.Time.sleep + (use "Eio.Time.Mono.sleep_span")) + (forbid Eio.Time.sleep_until + (use "Eio.Time.Mono.sleep_span, over the span from now to the instant")) + (forbid Eio.Time.with_timeout + (use "Eio.Time.Timeout.v, given an Mtime.Span.t")) + (forbid Eio.Time.with_timeout_exn + (use "Eio.Time.Timeout.v, given an Mtime.Span.t")) + (forbid Eio.Time.Timeout.seconds + (use "Eio.Time.Timeout.v, given an Mtime.Span.t")) + (forbid Eio.Time.Mono.sleep + (use "Eio.Time.Mono.sleep_span")) + (forbid Uri.query + (use "what the router decoded and validated")) + (forbid Uri.get_query_param + (use "what the router decoded and validated")) + ; Refused outside a library's clock module. + (forbid Eio.Time.now + (use "the library's instant.ml, which gives a Ptime.t") + (except **/instant.ml)) + (forbid Unix.sleepf + (use "the library's instant.ml, which takes an Mtime.Span.t") + (except **/instant.ml)) + ; Refused in a library: a program, a test and an example own their process, + ; wherever in the tree its bin/, test/, examples/ or bench/ is. + (forbid Stdlib.Sys.getenv + (use "a value the executable read at startup and passed in") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Sys.getenv_opt + (use "a value the executable read at startup and passed in") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Unix.getenv + (use "a value the executable read at startup and passed in") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Unix.environment + (use "a value the executable read at startup and passed in") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Unix.putenv + (use "an argument: a library sets no global state") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Sys.argv + (use "a value the executable read at startup and passed in") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Sys.signal + (use "the executable: it owns the process's signals") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Sys.set_signal + (use "the executable: it owns the process's signals") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Sys.chdir + (use "a directory capability the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Sys.command + (use "Eio.Process, on the process manager the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.at_exit + (use "Eio.Switch.on_release, on the switch the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Random.self_init + (use "a Random.State.t, or the secure random source the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Random.init + (use "a Random.State.t, or the secure random source the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Logs.set_reporter + (use "the executable: it configures the log reporter") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Logs.set_level + (use "the executable: it configures the log level") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.print_string + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.print_endline + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.print_bytes + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.print_char + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.print_int + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.print_float + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.print_newline + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.prerr_string + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.prerr_endline + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.prerr_bytes + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.prerr_char + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.prerr_int + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.prerr_float + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.prerr_newline + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.stdout + (use "a sink the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.stderr + (use "a sink the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Printf.printf + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Printf.eprintf + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Format.printf + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Format.eprintf + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Format.print_string + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Format.std_formatter + (use "a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Format.err_formatter + (use "a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Fmt.pr + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Fmt.epr + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Eio_main.run + (use "the env, switch or capability the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Ptime_clock.now + (use "the clock or ~now the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Mtime_clock.now + (use "the clock the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Mtime_clock.elapsed + (use "the clock the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Mtime_clock.counter + (use "the clock the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + ; Refused in a Melange library: the console is the page's. + (forbid Js.log + (use "a value returned to the caller") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Js.log2 + (use "a value returned to the caller") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Js.log3 + (use "a value returned to the caller") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Js.log4 + (use "a value returned to the caller") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Js.logMany + (use "a value returned to the caller") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Js.Console + (use "a value returned to the caller") + (except **/bin/** **/test/** **/examples/** **/bench/**))) diff --git a/postgres-eio.opam b/postgres-eio.opam index fdcfcef..d323905 100644 --- a/postgres-eio.opam +++ b/postgres-eio.opam @@ -34,6 +34,7 @@ depends: [ "merlin" {with-dev-setup & >= "5.6-504"} "ocaml-lsp-server" {with-dev-setup & >= "1.25.0"} "ocamlformat" {with-dev-setup & = "0.29.0"} + "hypha-litany" {with-dev-setup & >= "1.3.0" & < "2.0.0"} "odoc" {with-dev-setup & >= "3.2.1"} "odoc" {with-doc} ] diff --git a/src/auth.mli b/src/auth.mli index f83cb16..d9548b4 100644 --- a/src/auth.mli +++ b/src/auth.mli @@ -1,6 +1,9 @@ (** SCRAM-SHA-256 and MD5 sign-in, without IO or randomness: the caller supplies the nonce. Nothing is logged, and no error contains a password, proof or - message bytes. *) + message bytes. + + Its documents, a page for each part: + [$(opam var postgres-eio:doc)/llms.txt]. *) val md5 : user:string -> password:string -> salt:string -> string (** The AuthenticationMD5Password response: diff --git a/src/column.mli b/src/column.mli index 27caf54..87742e5 100644 --- a/src/column.mli +++ b/src/column.mli @@ -1,4 +1,7 @@ -(** A column of a result, as the server describes it before the first row. *) +(** A column of a result, as the server describes it before the first row. + + Its documents, a page for each part: + [$(opam var postgres-eio:doc)/llms.txt]. *) type format = | Text (** the type's text form, as [psql] prints it *) diff --git a/src/connection.ml b/src/connection.ml index b201b64..25986f5 100644 --- a/src/connection.ml +++ b/src/connection.ml @@ -381,7 +381,14 @@ let tls_config (c : C.t) = Result.map (fun cas -> X509.Authenticator.chain_of_trust - ~time:(fun () -> Some (Ptime_clock.now ())) + ~time:(fun () -> + Some + ((Ptime_clock.now + [@litany.allow + "restricted-dependency: a certificate is valid at an \ + instant of the system's clock, and connect is given a \ + monotonic clock alone"]) + ())) cas) (X509.Certificate.decode_pem_multiple (read_file "sslrootcert" file)) in @@ -852,8 +859,9 @@ let open_socket t address = | `Tcp _ when t.conninfo.keepalives -> ( try keep_alive socket with ex -> + let backtrace = Printexc.get_raw_backtrace () in close_quietly socket; - raise ex) + Printexc.raise_with_backtrace ex backtrace) | `Tcp _ | `Unix _ -> ()); socket @@ -941,8 +949,9 @@ let attempt t endpoint address ~tls = with | link -> link | exception ex -> + let backtrace = Printexc.get_raw_backtrace () in close_quietly socket; - raise ex + Printexc.raise_with_backtrace ex backtrace let connect_address t (endpoint : C.endpoint) address = match endpoint.ssl_mode with @@ -1475,22 +1484,23 @@ let as_bound described (results : P.format array) = (* 54.2.3: always read to ReadyForQuery; after an error the server skips to the Sync. *) let read_statement t link use ~results ~columns ~init ~row = + let describe m described = + match use with + | First (_, s) -> s.prepared <- Described described + | Unnamed -> columns described + | Again _ -> + out_of_turn "54.2.3 Extended Query" m + "for a statement described already" + in let rec loop acc tag failed parsed = match message t link with | P.Parse_complete -> loop acc tag failed true | P.Parameter_description _ -> loop acc tag failed parsed - | (P.Row_description _ | P.No_data) as m -> - let described = - match m with - | P.Row_description fields -> columns_of fields - | _ -> [||] - in - (match use with - | First (_, s) -> s.prepared <- Described described - | Unnamed -> columns described - | Again _ -> - out_of_turn "54.2.3 Extended Query" m - "for a statement described already"); + | P.Row_description fields as m -> + describe m (columns_of fields); + loop acc tag failed parsed + | P.No_data as m -> + describe m [||]; loop acc tag failed parsed | P.Bind_complete as m -> (match use with @@ -1634,22 +1644,22 @@ let execute t sql ~params = (* One Sync for all rows: a single implicit transaction. *) let read_many t link use = + let describe m described = + match use with + | First (_, s) -> s.prepared <- Described described + | Unnamed | Again _ -> + out_of_turn "54.2.3 Extended Query" m "for a statement not described" + in let rec loop tags failed parsed = match message t link with | P.Parse_complete -> loop tags failed true | P.Parameter_description _ | P.Bind_complete | P.Data_row _ -> loop tags failed parsed - | (P.Row_description _ | P.No_data) as m -> - let described = - match m with - | P.Row_description fields -> columns_of fields - | _ -> [||] - in - (match use with - | First (_, s) -> s.prepared <- Described described - | Unnamed | Again _ -> - out_of_turn "54.2.3 Extended Query" m - "for a statement not described"); + | P.Row_description fields as m -> + describe m (columns_of fields); + loop tags failed parsed + | P.No_data as m -> + describe m [||]; loop tags failed parsed | P.Command_complete s -> let tag = Tag.of_string s in diff --git a/src/connection.mli b/src/connection.mli index feaff3c..2c6cf12 100644 --- a/src/connection.mli +++ b/src/connection.mli @@ -1,5 +1,8 @@ (** A connection to Postgres. + Its documents, a page for each part: + [$(opam var postgres-eio:doc)/llms.txt]. + Use a connection from one fiber at a time: there is no locking. That fiber may be on any domain, not only the one that made the connection. Only {!cancel} may be called from another fiber. diff --git a/src/conninfo.mli b/src/conninfo.mli index 4830d00..9f94cc3 100644 --- a/src/conninfo.mli +++ b/src/conninfo.mli @@ -1,6 +1,9 @@ (** Connection strings, in both libpq forms: a [postgres://] or [postgresql://] URL, or keyword/value ([host=db user=app password='a b']). + Its page, with its examples: + [$(opam var postgres-eio:doc)/pages/guide/connection-strings.md]. + Supported keys: [host], [hostaddr], [port], [user], [password], [dbname], [sslmode], [sslrootcert], [sslcert], [sslkey], [sslnegotiation], [channel_binding], [require_auth], [connect_timeout], [application_name], diff --git a/src/instant.mli b/src/instant.mli index 4e0c901..48b0559 100644 --- a/src/instant.mli +++ b/src/instant.mli @@ -1,5 +1,8 @@ (** Between the driver's own arithmetic, in microseconds and days since - 1970-01-01, and Ptime's types, which its interface speaks. *) + 1970-01-01, and Ptime's types, which its interface speaks. + + Its documents, a page for each part: + [$(opam var postgres-eio:doc)/llms.txt]. *) val of_us : int -> Ptime.t option (** Microseconds since the epoch; [None] outside Ptime's years 0 to 9999. *) diff --git a/src/interval.mli b/src/interval.mli index 04c0e18..335973e 100644 --- a/src/interval.mli +++ b/src/interval.mli @@ -1,7 +1,10 @@ (** A Postgres [interval]: months, days and microseconds, each kept apart as Postgres keeps them, since a month is no fixed number of days and a day, across a change of clocks, no fixed number of hours. Each part carries its - own sign: [1 mon -1 days] is one month less a day. *) + own sign: [1 mon -1 days] is one month less a day. + + Its documents, a page for each part: + [$(opam var postgres-eio:doc)/llms.txt]. *) type t = { months : int; days : int; microseconds : int } diff --git a/src/oid.mli b/src/oid.mli index 11e5d26..1763664 100644 --- a/src/oid.mli +++ b/src/oid.mli @@ -1,6 +1,8 @@ (** An object identifier: a row of a system catalogue, such as a type in [pg_type] ([23] is [int4]) or a table in [pg_class]. Unsigned, four bytes. -*) + + Its documents, a page for each part: + [$(opam var postgres-eio:doc)/llms.txt]. *) type t diff --git a/src/pool.ml b/src/pool.ml index cdaa71d..b021d69 100644 --- a/src/pool.ml +++ b/src/pool.ml @@ -316,7 +316,7 @@ let create ~sw ~net ~mono_clock:clock ?parameters ?timeout ?statement_cache housekeeping t ~every; `Stop_daemon); t) - (go (max 1 size) []) + (go (Int.max 1 size) []) let stats (t : t) = { diff --git a/src/pool.mli b/src/pool.mli index 13abdd1..4ff8ca2 100644 --- a/src/pool.mli +++ b/src/pool.mli @@ -1,5 +1,8 @@ (** A fixed-size connection pool. + Its page, with its examples: + [$(opam var postgres-eio:doc)/pages/guide/pool.md]. + All connections are made by {!create}, so a refused connection fails at startup rather than in the middle of a request. One pool serves borrowers on every domain. diff --git a/src/postgres_eio.mli b/src/postgres_eio.mli index 0252ed8..04ac854 100644 --- a/src/postgres_eio.mli +++ b/src/postgres_eio.mli @@ -1,5 +1,8 @@ (** A Postgres driver for Eio. + Its documents, a page for each part: + [$(opam var postgres-eio:doc)/llms.txt]. + Protocol 3.0 and 3.2 over TCP, a Unix socket or TLS, with multi-host failover. Sign-in by SCRAM-SHA-256 (with channel binding when offered), MD5, client certificate, trust, or cleartext inside TLS only. diff --git a/src/protocol.ml b/src/protocol.ml index ea6c9e1..36658cb 100644 --- a/src/protocol.ml +++ b/src/protocol.ml @@ -481,7 +481,7 @@ let feed r cs = if len > 0 then begin let held = r.stop - r.start in if r.stop + len > Bytes.length r.data then begin - let room = max (Bytes.length r.data) 1 in + let room = Int.max (Bytes.length r.data) 1 in let rec size n = if n >= held + len then n else size (n * 2) in let target = size room in let data = diff --git a/src/protocol.mli b/src/protocol.mli index 360a28f..f68a95d 100644 --- a/src/protocol.mli +++ b/src/protocol.mli @@ -1,5 +1,8 @@ (** Protocol 3.0 and 3.2 messages, without IO. + Its documents, a page for each part: + [$(opam var postgres-eio:doc)/llms.txt]. + {!encode} turns a frontend message into bytes. A {!type-reader} takes backend bytes split anywhere and yields messages. Errors name the section of chapter 54 of the Postgres 18 docs that was violated. *) diff --git a/src/saslprep.mli b/src/saslprep.mli index 5534f3f..d43469b 100644 --- a/src/saslprep.mli +++ b/src/saslprep.mli @@ -1,4 +1,7 @@ -(** SASLprep (RFC 4013), as Postgres applies it to a password. *) +(** SASLprep (RFC 4013), as Postgres applies it to a password. + + Its documents, a page for each part: + [$(opam var postgres-eio:doc)/llms.txt]. *) val password : string -> string (** The prepared password, or the input unchanged where Postgres uses it as is: diff --git a/src/server_error.mli b/src/server_error.mli index 9fc2246..9f7513c 100644 --- a/src/server_error.mli +++ b/src/server_error.mli @@ -1,4 +1,7 @@ -(** A server error: every field of its ErrorResponse (protocol 54.8). *) +(** A server error: every field of its ErrorResponse (protocol 54.8). + + Its page, with its examples: + [$(opam var postgres-eio:doc)/pages/guide/errors.md]. *) type t diff --git a/src/tag.mli b/src/tag.mli index 7376cab..daf7adf 100644 --- a/src/tag.mli +++ b/src/tag.mli @@ -1,4 +1,7 @@ -(** A statement's command tag, parsed. *) +(** A statement's command tag, parsed. + + Its documents, a page for each part: + [$(opam var postgres-eio:doc)/llms.txt]. *) type t diff --git a/src/text.mli b/src/text.mli index 075c8b0..038a65a 100644 --- a/src/text.mli +++ b/src/text.mli @@ -1,6 +1,9 @@ (** Text encodings of scalars. Parameters are always sent as text; results are text unless binary was requested ({!Value} reads either). Decoders return - [None] on malformed input. *) + [None] on malformed input. + + Its documents, a page for each part: + [$(opam var postgres-eio:doc)/llms.txt]. *) val int : int -> string (** In decimal. *) diff --git a/src/value.mli b/src/value.mli index 5cbc233..f4edc82 100644 --- a/src/value.mli +++ b/src/value.mli @@ -1,5 +1,8 @@ (** Cell decoders that give the same value for text and binary results. + Its documents, a page for each part: + [$(opam var postgres-eio:doc)/llms.txt]. + Each returns [None] for a cell of another type or an unreadable form, in text as in binary. Binary forms are Postgres's send functions, unchanged since Postgres 10 for these types. diff --git a/test/test_postgres_eio.ml b/test/test_postgres_eio.ml index 2e9e12b..a4d45e0 100644 --- a/test/test_postgres_eio.ml +++ b/test/test_postgres_eio.ml @@ -298,7 +298,7 @@ let a_grown_reader_shrinks_once_empty () = grown r; Alcotest.(check int) "nothing held" 0 (P.buffered r); let held = live_bytes () - before in - ignore (Sys.opaque_identity r); + ignore (Sys.opaque_identity r : P.reader); if held >= 65536 then Alcotest.failf "the empty reader still holds %d bytes more than it began with" held @@ -1810,7 +1810,7 @@ let a_fake_server env sw ~answer = let buf = Cstruct.create 1024 in let n = Eio.Flow.single_read flow buf in - ignore n; + ignore (n : int); Option.iter (fun a -> Eio.Flow.copy_string a flow) answer; (try while true do @@ -1960,7 +1960,9 @@ let a_cancel_over_tls () = (* Until the backend has gone, so its final message has been sent. *) let terminate env sw pid = let admin = connect env sw (plain ()) in - ignore (rows admin "select pg_terminate_backend($1::int, 5000)" [ Some pid ]); + ignore + (rows admin "select pg_terminate_backend($1::int, 5000)" [ Some pid ] + : string option list list); Pg.close admin (* A terminated backend is reported, and reset restores the startup @@ -2070,7 +2072,8 @@ let a_connection_left_open_lets_its_switch_finish () = with_eio @@ fun env _ -> Eio.Time.Timeout.run_exn (Eio.Time.Timeout.v (Eio.Stdenv.mono_clock env) Mtime.Span.(5 * s)) - (fun () -> Eio.Switch.run (fun sw -> ignore (connect env sw (plain ())))) + (fun () -> + Eio.Switch.run (fun sw -> ignore (connect env sw (plain ()) : Pg.t))) (* A caller's cancellation propagates as is, never as [Timeout]. *) let a_cancellation_from_outside_is_not_a_timeout () = @@ -2114,7 +2117,9 @@ let still_running env sw pid ~within = end in let answer = wait () in - ignore (rows admin "select pg_terminate_backend($1::int, 5000)" [ Some pid ]); + ignore + (rows admin "select pg_terminate_backend($1::int, 5000)" [ Some pid ] + : string option list list); Pg.close admin; answer @@ -2139,7 +2144,10 @@ let a_cancelled_exchange_is_cancelled_at_the_server () = let t = connect env sw (plain ()) in let pid = one t "select pg_backend_pid()::text" [] in Eio.Fiber.first - (fun () -> ignore (Pg.execute t "select pg_sleep(60)" ~params:[])) + (fun () -> + ignore + (Pg.execute t "select pg_sleep(60)" ~params:[] + : (Pg.Tag.t, Pg.error) result)) (fun () -> Eio.Time.Mono.sleep_span (Eio.Stdenv.mono_clock env) Mtime.Span.(200 * ms)); Alcotest.(check bool) "closed" true (Pg.closed t); @@ -2232,7 +2240,7 @@ let transaction_status () = in script t "begin"; Alcotest.(check string) "54.7 ReadyForQuery T" "in" (status ()); - ignore (Pg.execute t "select 1/0" ~params:[]); + ignore (Pg.execute t "select 1/0" ~params:[] : (Pg.Tag.t, Pg.error) result); Alcotest.(check string) "54.7 ReadyForQuery E" "failed" (status ()); (match Pg.execute t "commit" ~params:[] with | Ok tag -> @@ -2547,7 +2555,10 @@ let a_cancelled_copy_closes () = [| Some "1" |]) in Eio.Fiber.first - (fun () -> ignore (Pg.copy_in_rows t ~table:"c" ~columns:[ "n" ] forever)) + (fun () -> + ignore + (Pg.copy_in_rows t ~table:"c" ~columns:[ "n" ] forever + : (Pg.Tag.t, Pg.error) result)) (fun () -> Eio.Time.Mono.sleep_span (Eio.Stdenv.mono_clock env) Mtime.Span.(200 * ms)); Alcotest.(check bool) "closed" true (Pg.closed t); @@ -2656,7 +2667,8 @@ let a_fake_postgres env sw ?(ssl = "N") ?(before = "") ?(reported = []) (String.sub body 0 (String.length body - 1))); until_terminate () | _ -> - ignore (Eio.Buf_read.take (take_int32 () - 4) r); + ignore + (Eio.Buf_read.take (take_int32 () - 4) r : string); until_terminate () | exception (End_of_file | Eio.Io _) -> on_end ~terminated:false @@ -2736,7 +2748,7 @@ let a_scram_server ?(iterations = 4096) env sw ~after_proof = in let write s = Eio.Flow.copy_string s flow in let message () = - ignore (Eio.Buf_read.any_char r); + ignore (Eio.Buf_read.any_char r : char); Eio.Buf_read.take (take_int32 () - 4) r in let rec startup () = @@ -2762,14 +2774,14 @@ let a_scram_server ?(iterations = 4096) env sw ~after_proof = (msg 'R' (int32 11 ^ "r=" ^ client_nonce ^ Printf.sprintf "server,s=c2FsdA==,i=%d" iterations)); - ignore (message ()); + ignore (message () : string); write (after_proof ^ msg 'R' (int32 0) ^ msg 'K' (int32 1 ^ "keys") ^ msg 'Z' "I"); while true do - ignore (message ()) + ignore (message () : string) done with End_of_file | Eio.Io _ -> ())); port @@ -2982,7 +2994,10 @@ let a_cancel_request_left_unanswered_is_given_up () = let t = connect env sw (fake port) in Pg.set_timeout t ~timeout:None; Eio.Fiber.first - (fun () -> ignore (Pg.execute t "select pg_sleep(60)" ~params:[])) + (fun () -> + ignore + (Pg.execute t "select pg_sleep(60)" ~params:[] + : (Pg.Tag.t, Pg.error) result)) (fun () -> Eio.Time.Mono.sleep_span clock Mtime.Span.(200 * ms)); Alcotest.(check bool) "closed" true (Pg.closed t); Alcotest.(check bool) "and returned before the server let go" false !let_go @@ -3094,7 +3109,9 @@ let text_is_utf8_in_any_database () = in_a_database env sw ~name:"pgeio_latin1" ~encoding:"LATIN1" @@ fun c -> let t = connect env sw c in script t "create temp table e (s text)"; - ignore (ok_pg (Pg.execute t "insert into e values ($1)" ~params:[ Some "é" ])); + ignore + (ok_pg (Pg.execute t "insert into e values ($1)" ~params:[ Some "é" ]) + : Pg.Tag.t); Alcotest.(check string) "one character" "1" (one t "select length(s)::text from e" []); @@ -3702,7 +3719,7 @@ let the_cache_is_sized_or_off () = with_eio @@ fun env sw -> let t = connect_cached ~statement_cache:2 env sw (plain ()) in List.iter - (fun n -> ignore (value t (Printf.sprintf "select %d" n) [])) + (fun n -> ignore (value t (Printf.sprintf "select %d" n) [] : string)) [ 1; 2; 3; 4; 5 ]; Alcotest.(check int) "54.2.3 the least recently used are closed" 2 (ours t); Alcotest.(check int) "its size, as made" 2 (Pg.statement_cache t); @@ -3765,8 +3782,8 @@ let the_columns_before_any_row () = (Pg.query t "select 1 as one, 'x'::text as two where $1" ~columns ~params:[ Some "false" ] ~init:0 ~row:(fun n _ -> n + 1)) in - ignore (run ()); - ignore (run ()); + ignore (run () : int * Pg.Tag.t); + ignore (run () : int * Pg.Tag.t); Alcotest.(check (list (list string))) (Printf.sprintf "54.2.3 told twice, with no rows (cache %d)" statement_cache) @@ -3780,7 +3797,8 @@ let the_columns_before_any_row () = told := true; Alcotest.(check int) "none" 0 (Array.length cs)) ~init:() - ~row:(fun () _ -> ()))); + ~row:(fun () _ -> ())) + : unit * Pg.Tag.t); Alcotest.(check bool) "and told for a statement with no result" true !told; Pg.close t) [ 256; 0 ] @@ -3988,7 +4006,7 @@ let a_server_that_answers_big env sw ~size = Int32.to_int (String.get_int32_be (Eio.Buf_read.take 4 r) 0) in let write s = Eio.Flow.copy_string s flow in - ignore (Eio.Buf_read.take (take_int32 () - 4) r); + ignore (Eio.Buf_read.take (take_int32 () - 4) r : string); write (msg 'R' (int32 0) ^ msg 'Z' "I"); let rec statements ~parsed ~copy = let kind = Eio.Buf_read.any_char r in @@ -4010,7 +4028,7 @@ let a_server_that_answers_big env sw ~size = | _ -> statements ~parsed ~copy and copying n = let kind = Eio.Buf_read.any_char r in - ignore (Eio.Buf_read.take (take_int32 () - 4) r); + ignore (Eio.Buf_read.take (take_int32 () - 4) r : string); match kind with | 'd' -> write notice; @@ -4019,7 +4037,7 @@ let a_server_that_answers_big env sw ~size = | _ -> copying n and copied n = let kind = Eio.Buf_read.any_char r in - ignore (Eio.Buf_read.take (take_int32 () - 4) r); + ignore (Eio.Buf_read.take (take_int32 () - 4) r : string); match kind with | 'S' -> write (msg 'C' (cstr (Printf.sprintf "COPY %d" n)) ^ msg 'Z' "I"); @@ -4335,7 +4353,7 @@ let binary_needs_the_statement_cache () = ~init:0 ~row:(fun _ cells -> Array.length cells)) in - ignore (ok_pg (width ())); + ignore (ok_pg (width ()) : int); script t "alter table w add column b int; insert into w values (1, 2)"; Alcotest.(check int) "parsed again, and bound again" 2 (ok_pg (width ())); Pg.close t @@ -4429,8 +4447,12 @@ let no_secret_in_the_log () = (as_role (base ()) "pgeio_scram" (Some "pw-SECRET-1")) [ ("sslmode", mode) ]) in - ignore (rows t "select $1::text" [ Some "token-SECRET-2" ]); - ignore (Pg.execute t "select $1::int" ~params:[ Some "SECRET-3" ]); + ignore + (rows t "select $1::text" [ Some "token-SECRET-2" ] + : string option list list); + ignore + (Pg.execute t "select $1::int" ~params:[ Some "SECRET-3" ] + : (Pg.Tag.t, Pg.error) result); Pg.close t) [ "disable"; "require" ]) in @@ -4643,7 +4665,7 @@ let drain_and_flush () = let t = connect_cached env sw (plain ()) in let q sql = Pg.Pipeline.query t sql ~params:[] ~init:[] ~row:one_value in let failed = q "select 1/0" in - ignore (q "begin"); + ignore (q "begin" : (string list * Pg.Tag.t) Pg.Pipeline.answer); Alcotest.(check bool) "not read yet" true (match Pg.status t with Pg.Idle -> true | _ -> false); @@ -4727,7 +4749,8 @@ let any_rows_through_copy () = ignore (ok_pg (Pg.copy_in_rows t ~table:"r" ~columns:[ "n"; "s" ] - (List.to_seq (List.map Array.of_list sent)))); + (List.to_seq (List.map Array.of_list sent))) + : Pg.Tag.t); let back, _ = ok_pg (Pg.copy_out_rows t ~select:"select n, s from r order by n" ~init:[] @@ -5091,7 +5114,8 @@ let a_statement_not_yet_sent_is_never_sent () = Pg.Pool.use p (fun t -> ignore (Pg.Pipeline.execute t "insert into pgeio_unsent values (1)" - ~params:[]); + ~params:[] + : Pg.Tag.t Pg.Pipeline.answer); Eio.Promise.resolve queue (); Eio.Time.Mono.sleep_span clock Mtime.Span.(5 * s)) with @@ -5122,7 +5146,8 @@ let a_transaction_left_open_is_rolled_back () = "and nothing kept" "0" (one t "select count(*) from pgeio_open" []); script t "begin"; - ignore (Pg.execute t "select 1/0" ~params:[])); + ignore + (Pg.execute t "select 1/0" ~params:[] : (Pg.Tag.t, Pg.error) result)); borrow p (fun t -> Alcotest.(check bool) "an aborted one too" true @@ -5140,7 +5165,9 @@ let a_pipeline_left_unread () = "create table if not exists pgeio_unread (n int); truncate pgeio_unread"); borrow p (fun t -> List.iter - (fun sql -> ignore (Pg.Pipeline.execute t sql ~params:[])) + (fun sql -> + ignore + (Pg.Pipeline.execute t sql ~params:[] : Pg.Tag.t Pg.Pipeline.answer)) [ "begin"; "insert into pgeio_unread values (1)" ]); borrow p (fun t -> Alcotest.(check bool) @@ -5164,7 +5191,8 @@ let a_row_function_that_raises_while_drained () = Pg.Pool.use p (fun t -> ignore (Pg.Pipeline.query t "select 1" ~params:[] ~init:() ~row:(fun () _ -> - raise Row_bug))) + raise Row_bug) + : (unit * Pg.Tag.t) Pg.Pipeline.answer)) with | exception Row_bug -> () | exception ex -> Alcotest.failf "raised %s" (Printexc.to_string ex) @@ -5299,7 +5327,7 @@ let a_dead_idle_connection_is_checked () = "made anew by the check" true ((Pg.Pool.stats p).replaced >= 1); for _ = 1 to 4 do - ignore (borrow p backend) + ignore (borrow p backend : string) done; Pg.Pool.close p @@ -5376,7 +5404,7 @@ let closing_a_pool_waits_for_its_borrows () = borrow p (fun t -> Eio.Time.Mono.sleep_span clock Mtime.Span.(300 * ms); during := Pg.closed t; - ignore (one t "select 1" []))) + ignore (one t "select 1" [] : string))) (fun () -> Eio.Time.Mono.sleep_span clock Mtime.Span.(100 * ms); Pg.Pool.close p); @@ -5746,7 +5774,8 @@ let a_reset_that_fails_says_why () = script admin "alter role pgeio_reset password 'after'"; Pg.close admin; terminate env sw pid; - borrow p (fun t -> ignore (Pg.script t "select 1")); + borrow p (fun t -> + ignore (Pg.script t "select 1" : (unit, Pg.error) result)); borrow p (fun t -> Alcotest.(check bool) "its statement answers that its connection is closed" true From 7f061e014e6e5310e9a3c9aaf20e7f2b820663d1 Mon Sep 17 00:00:00 2001 From: Roman Zaiev Date: Thu, 3 Dec 2026 16:18:18 +0000 Subject: [PATCH 2/3] Release 0.5.4 --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 34d6ea7..873ba1f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,6 @@ # Changes -## Unreleased +## 0.5.4 (2026-12-03) - A connection that fails while it is being opened keeps the backtrace of the exception its socket is closed on, where closing could overwrite it. From 5562f87cbc80bc4b414eaffc0cd4cab69628b758 Mon Sep 17 00:00:00 2001 From: Roman Zaiev Date: Thu, 3 Dec 2026 16:57:47 +0000 Subject: [PATCH 3/3] Keep in AGENTS.md only the rules that are postgres-eio's own Two of its rules restated OCAML.md: the litany file held whole, and a driver's failures as values with cancellation passed through. What is the driver's own stays: which failures close the connection, and how a callback's exception and Fail travel. --- AGENTS.md | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 57f2d7b..7a9baa6 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -73,7 +73,6 @@ Each rule comes with why it exists and the test that catches a break. release installs, offline, and a page it reads must be the release's. Test: the docs workflow builds the pages on every pull request and fails if `agents/` then differs. - - **Every protocol requirement the driver meets has a test case named by its section** of chapter 54 of the Postgres 18 docs (e.g. `54.2.4 ...`). Backend messages are read whole, a byte at a time, and at generated @@ -113,15 +112,14 @@ Each rule comes with why it exists and the test that catches a break. `handshake` sources are the application's to cap. Test: `no secret in the log`, `a bad escape in a password is not quoted`, `a delimiter in a password is not quoted`. -- **The driver's own failures are values; only the caller's exceptions - pass through.** Failures inside an exchange raise `Fail` internally and - are caught where the exchange began. An exception from a caller's - callback (a row function, a COPY source) and Eio's cancellation propagate - as they came. A failure that leaves the stream out of step (timeout, - broken socket, malformed bytes, a raise from a row function) closes the - connection. A server error does not, since the server marks where its - reply ends. Test: `54.2.3 a server's error keeps the connection`, - `54.2.6 a producer that raises is a CopyFail`. +- **A failure that leaves the stream out of step closes the connection; a + server error does not**, since the server marks where its reply ends. Out + of step is a timeout, a broken socket, malformed bytes or a raise from a + row function. A failure inside an exchange raises `Fail` internally and + is caught where the exchange began; an exception from a caller's callback + (a row function, a COPY source) propagates as it came. Test: `54.2.3 a + server's error keeps the connection`, `54.2.6 a producer that raises is + a CopyFail`. - **The driver reads the system's clock in one place: to verify a certificate against `sslrootcert`**, in `tls_config` of `src/connection.ml`, as `Ca_certs.authenticator` does for the system's @@ -129,7 +127,3 @@ Each rule comes with why it exists and the test that catches a break. given a monotonic clock alone. Symptom: `make lint`'s litany reports `restricted-dependency` for a clock anywhere else, which that one use is allowed with its reason beside it. -- **The `litany` file holds all of Hypha's, and adds nothing of - postgres-eio's.** Why: one set of rules holds every repository, and an - exception is read with its reason. Symptom: a rule dropped from `select` - or `extend`, or a `per-path` or a `[@litany.allow]` with no reason.