diff options
| author | Pjotr <wrk@bosa.lan> | 2026-10-19 08:14:37 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-10-19 08:14:37 +0000 |
| commit | 848e28612eda626f64b4e4d7ee486d382d2b6b18 (patch) | |
| tree | 322fc1158b40af4bf1e171e1f1efd06290ef0098 | |
| parent | fb343b6f94776301fdb605dea87a227d5323f7c5 (diff) | |
| download | gn-machines-848e28612eda626f64b4e4d7ee486d382d2b6b18.tar.gz | |
genecup: boot check calls the model, not just the models catalogue
A key can list gemini-3-flash-preview in /v1beta/models and still not be
allowed to call it (preview gating, project/quota/region limits). In
that case the app's stress view prints "All related sentences (Gemini
classification not available or no specific stress types found)" after
three failed attempts, which reads like "nothing to classify" and hides
the API's own error -- exactly the report from a second deployment
where the very same key/container works elsewhere.
Make gemini-check do what the app does: POST
/v1beta/models/<model>:generateContent with a one-sentence
classification prompt (a handful of tokens), and on failure quote the
API's response body so the boot log names the reason (model not found,
permission denied, quota, TLS, ...). The model is a single definition
(%gemini-model, gemini-3-flash-preview) that must stay in sync with
server.py's hard-coded batch classifier, and the docstring says so.
Verdict is now
Gemini classification available: key in PATH works, MODEL answered (HTTP 200)
Gemini classification not available: MODEL call failed (HTTP 404): {...}
so the container tests assert "answered (HTTP 200)".
| -rw-r--r-- | genenetwork/services/genecup.scm | 124 | ||||
| -rwxr-xr-x | genenetwork/services/test/run_genecup_container_tests.sh | 4 |
2 files changed, 81 insertions, 47 deletions
diff --git a/genenetwork/services/genecup.scm b/genenetwork/services/genecup.scm index 64e4747..70ebf1c 100644 --- a/genenetwork/services/genecup.scm +++ b/genenetwork/services/genecup.scm @@ -140,20 +140,30 @@ ;;; Gemini API key check at boot. Mirrors the wording of the app's own ;;; diagnostics (server.py prints "Warning: ~/.config/gemini/credentials -;;; not found or empty..." and "Gemini classification not available ...") -;;; but says it at boot for every container, whether or not the app is -;;; serving yet, and -- unlike the app -- actually asks the API whether -;;; the key is accepted instead of only noting its presence. +;;; not found or empty..." and, in the stress view, "Gemini API error:", +;;; falling back to "Gemini classification not available or no specific +;;; stress types found") but says it at boot for every container, whether +;;; or not the app is serving yet, and -- unlike the app -- actually asks +;;; the API instead of only noting that a key file is present. -(define %gemini-models-url - "https://generativelanguage.googleapis.com/v1beta/models") +(define %gemini-api + "https://generativelanguage.googleapis.com/v1beta") + +;; server.py's batch stress classifier hard-codes this model. Keep the +;; two in sync: a key that lists the model in /v1beta/models may still +;; not be allowed to call it, so the check calls it rather than reading +;; the catalogue. +(define %gemini-model "gemini-3-flash-preview") (define (gemini-check-program datadir) "Return the boot-time Gemini API key check as a guile script: it reports, in the app's own words, whether the key is present, has the -0400 mode server.py insists on, and is accepted by the API -- listing -the models costs no tokens but needs a valid key. The verdict goes to -stdout (the boot log) and, as one line, to /var/log/gemini-check.log. +0400 mode server.py insists on, and whether %GEMINI-MODEL actually +answers a classification request -- the failure mode seen when a key is +valid but the model is not available to it is exactly the app's \"Gemini +classification not available\" fallback, with nothing to explain it. The +verdict goes to stdout (the boot log) and, as one line, to +/var/log/gemini-check.log. Uses core bindings only (read-char, stat, system*): a shepherd start procedure cannot rely on imports, they do not take effect in the @@ -165,6 +175,12 @@ generated config file." (define key-file #$key-file) (define body "/tmp/gemini-check.json") (define code-file "/tmp/gemini-check.code") + ;; No quotes in the prompt, so the JSON body needs no escaping. + (define prompt + (string-append + "Classify the following sentence as Cellular Stress or " + "Organismal Stress and answer with the label only: " + "Heat shock protein 70 protects cells from oxidative stress.")) ;; curl in a minimal container has no default CA bundle: the ;; app's own wrapper points SSL_CERT_FILE at the profile's @@ -195,44 +211,61 @@ generated config file." (loop chars #f))) (else (loop (cons c chars) #t)))))))) + ;; First LIMIT characters of FILE: the API's error body, which + ;; names the reason (bad key, model not available, quota, ...). + (define (excerpt file limit) + (if (not (file-exists? file)) + "(no response body)" + (call-with-input-file file + (lambda (port) + (let loop ((chars '()) (n 0)) + (let ((c (read-char port))) + (if (or (eof-object? c) (>= n limit)) + (list->string (reverse chars)) + (loop (cons c chars) (+ n 1))))))))) + (define (verdict) - (if (not (file-exists? key-file)) - (string-append "Gemini classification not available: " - key-file " not found") - (let ((perms (logand (stat:mode (stat key-file)) #o777))) - (if (not (= perms #o400)) + (cond + ((not (file-exists? key-file)) + (string-append "Gemini classification not available: " + key-file " not found")) + ((not (= (logand (stat:mode (stat key-file)) #o777) #o400)) + (string-append + "Gemini classification not available: " key-file + " has permissions 0" + (number->string (logand (stat:mode (stat key-file)) #o777) 8) + ", expected 0400 (the app exits on that)")) + ((string=? (first-token key-file) "") + (string-append "Gemini classification not available: " + key-file " is empty")) + (else + (let ((key (first-token key-file)) + (ca (ca-bundle))) + (when ca + (setenv "SSL_CERT_FILE" ca)) + (call-with-output-file code-file + (lambda (out) + (parameterize ((current-output-port out)) + (system* #$(file-append curl "/bin/curl") + "-sS" "--connect-timeout" "5" "--max-time" "30" + "-X" "POST" + "-H" "Content-Type: application/json" + "-d" (string-append + "{\"contents\":[{\"parts\":[{\"text\":\"" + prompt "\"}]}]}") + "-o" body "-w" "%{http_code}" + (string-append #$%gemini-api "/models/" + #$%gemini-model + ":generateContent?key=" key))))) + (let ((code (first-token code-file))) + (if (string=? code "200") (string-append - "Gemini classification not available: " key-file - " has permissions 0" (number->string perms 8) - ", expected 0400 (the app exits on that)") - (let ((key (first-token key-file))) - (if (string=? key "") - (string-append - "Gemini classification not available: " - key-file " is empty") - (let ((url (string-append #$%gemini-models-url - "?key=" key)) - (ca (ca-bundle))) - (when ca - (setenv "SSL_CERT_FILE" ca)) - (call-with-output-file code-file - (lambda (out) - (parameterize ((current-output-port out)) - (system* #$(file-append curl "/bin/curl") - "-sS" "--connect-timeout" "5" - "--max-time" "20" - "-o" body "-w" "%{http_code}" - url)))) - (let ((code (first-token code-file))) - (if (string=? code "200") - (string-append - "Gemini classification available: key in " - key-file " is accepted by the API " - "(HTTP 200)") - (string-append - "Gemini classification not available: " - "the API rejected the key in " key-file - " (HTTP " code "), see " body)))))))))) + "Gemini classification available: key in " key-file + " works, " #$%gemini-model " answered (HTTP 200)") + (string-append + "Gemini classification not available: " + #$%gemini-model " call failed (HTTP " code "): " + (excerpt body 300)))))))) (let ((message (verdict))) (display message) @@ -241,6 +274,7 @@ generated config file." (display message log) (newline log) (close-port log))))))) + (define (gemini-check-shepherd-service config) "Return the one-shot shepherd service that runs the Gemini API key check at boot (see gemini-check-program)." diff --git a/genenetwork/services/test/run_genecup_container_tests.sh b/genenetwork/services/test/run_genecup_container_tests.sh index 301f710..924a567 100755 --- a/genenetwork/services/test/run_genecup_container_tests.sh +++ b/genenetwork/services/test/run_genecup_container_tests.sh @@ -225,8 +225,8 @@ if [ "$EXPECT_MISSING" = 1 ]; then else check "key file is genecup-owned and mode 0400" \ "^-r-------- [0-9][0-9]* genecup *genecup .*credentials$" - check "check service says the API accepts the key" \ - "Gemini classification available: key in .* is accepted by the API" + check "check service says the API answers the model" \ + "Gemini classification available: key in .* answered (HTTP 200)" check "app loaded the key" "GEMINI_API_KEY=set" # The app exits with SystemExit(1) on a non-0400 mode and the # service then fails to start: catch that too. |
