Path 1 architecture: one comfyui adapter, workflows as data.
- workflow_template.go: embed.FS + token substitution with type-preserving
whole-value placeholders. ${prompt} → string, ${seed} → int64,
${cfg} → float64 — no JSON round-tripping. Partial matches ignored.
- comfyui.go: refactored to load workflow from embedded FS or filesystem
path. Back-compat preserved: workflow: defaults to flux1-schnell.
- workflows/{flux1-schnell,flux2-klein,sd35-medium}.json — bundled
templates. flux1-schnell migrated from hardcoded with identical node IDs.
- compare.go: new `imagen compare` subcommand. Sequential N-backend run
(one GPU on mRock — parallel would OOM), per-backend PNG, sidecar JSON
with per-model metadata + errors, composite contact sheet via Go image
package (no ImageMagick dep).
- Sample config gains flux2-klein-local + sd35-medium-local instances.
- docs/backends.md: architecture rationale + per-model HF download paths
+ how to add a new bundled workflow + compare-harness reference.
Live smoke verified: compare mock + flux-schnell-local at 768×768 →
both PNGs written, sidecar JSON has workflow="flux1-schnell" + full
metadata, contact sheet renders. Worker contract (Request → Generate)
unchanged, so flexsiebels /imagine UI API surface preserved.
Tests: 11 existing comfyui + 6 new workflow_template + 5 new compare
tests, all green.
Adding a new model is now yaml + JSON, never Go.
Async write path for the flexsiebels owner-mode UI: flexsiebels INSERTs into
imagen.jobs, the worker on mRiver claims pending rows via LISTEN/NOTIFY +
5s safety poll, runs the same generate pipeline imagen generate uses, and
writes the result through internal/cloud into imagen.images.
- Schema migration imagen_jobs_init: table + status CHECK + two indexes +
owner-scoped RLS + grants + AFTER INSERT trigger publishing on the
imagen_jobs channel via pg_notify.
- internal/worker: DB-agnostic loop over a Queue interface. Drains the
whole pending backlog on each wake. Job-scoped contexts are derived
from Background so SIGTERM lets the in-flight generation finish (no
half-state). ResetStaleRunning at startup unsticks rows left over from
a previous crash. Eight unit tests cover the done / failed / missing-id /
drain / NOTIFY-wake / shutdown / transient-error paths against a fake
queue (no real Postgres in CI).
- cmd/imagen/worker.go: pgx-backed Queue (one dedicated conn for LISTEN +
UPDATE), plus the workerPipeline that reuses buildBackend +
attachUsageSink + prompt.Apply + buildWriter + maybeCloudSync. The
per-job owner_user_id overrides the env-level fallback so each row in
imagen.images is attributed correctly.
- maybeCloudSync now returns (*cloud.SyncResult, error) so the worker can
link imagen.jobs.image_id to the inserted imagen.images row. The CLI
generate path keeps printing its stderr summary unchanged.
- scripts/imagen-worker.service + .env.example for the systemd --user unit
on mRiver. EnvironmentFile lives in ~/.dotfiles and is never committed.
- docs/setup-worker-mriver.md walks through installation + the spec's
SQL-INSERT smoke; docs/architecture.md grows an "async write path"
section.
- worker_integration_test.go (env-guarded by IMAGEN_WORKER_INTEGRATION=1)
drives one real job through the full pipeline against msupabase using
the mock backend, then verifies imagen.images + Storage object landed
and the row flipped to done with image_id linked. Verified end-to-end:
pickup latency ~7ms, total 74ms, failure path captures error text.
Implements the Replicate API backend (FLUX schnell / FLUX dev) per ImaGen
issue #3:
- internal/backend/replicate.go — Backend adapter. Supports model
refs as "owner/name" (uses /v1/models/{owner}/{name}/predictions) and
"owner/name:hash" (uses /v1/predictions with explicit version). Polls
/v1/predictions/{id} every 500ms with model-aware timeout (60s schnell,
120s dev). Resilience: 401 names api_token_env, 429 with exp backoff
up to 3 retries (honours Retry-After), 5xx retries once, image
download retries once on transient failure.
- internal/backend/replicate_pricing.go — hardcoded per-image USD rates
for known FLUX models, snapshotted from replicate.com/pricing with a
refresh TODO.
- internal/backend/replicate_test.go — mocked-HTTP unit tests covering
happy path (model + version-pinned), 401, 429 retry policy, failed
prediction, poll timeout, image-download retry, ctx cancel, BackendOpts
passthrough, default_steps, aspect-ratio reduction, sha256 prompt hash.
- internal/usage/usage.go — Supabase REST sink + read-side query for
mai.imagen_usage. Adapter writes are best-effort: failures warn but
the image still lands.
- cmd/imagen/usage.go — `imagen usage [--since DATE] [--raw]` reads
the table and prints a tab-aligned grouped or raw table with totals.
- cmd/imagen/backends.go — instances of type=replicate now report
"ok" or "not configured (set REPLICATE_API_TOKEN)" depending on env.
- internal/config/config.go — sample adds flux-schnell-replicate +
flux-dev-replicate; default_backend stays flux-schnell-local.
- Supabase migration mai.imagen_usage (id, created_at, backend, model,
seed, prompt_hash, latency_ms, cost_usd_estimate, caller) + indexes
on (created_at DESC) and (caller). The raw prompt is never stored.
Caller identity resolves from MAI_FROM_ID, then the tmux pane's
@mai-name option, mirroring the maimcp identity logic. Prompt hash is
sha256 of the user-facing prompt; raw prompt never reaches the table.